source: mainline/uspace/drv/time/cmos-rtc/cmos-rtc.c@ 83298e8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 83298e8 was 83298e8, checked in by Maurizio Lombardi <m.lombardi85@…>, 13 years ago

rtc: make use of the getuptime() function

  • Property mode set to 100644
File size: 15.0 KB
Line 
1/*
2 * Copyright (c) 2012 Maurizio Lombardi
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @defgroup CMOS RTC driver.
31 * @brief HelenOS RTC driver.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <errno.h>
39#include <ddi.h>
40#include <as.h>
41#include <sysinfo.h>
42#include <libarch/ddi.h>
43#include <libarch/barrier.h>
44#include <stdio.h>
45#include <ddf/driver.h>
46#include <ddf/log.h>
47#include <ops/clock_dev.h>
48#include <fibril_synch.h>
49#include <device/hw_res.h>
50#include <devman.h>
51#include <macros.h>
52#include <ipc/clock_ctl.h>
53#include <time.h>
54
55#include "cmos-regs.h"
56
57#define NAME "cmos-rtc"
58
59#define REG_COUNT 2
60
61#define RTC_FROM_FNODE(fnode) ((rtc_t *) ((fnode)->dev->driver_data))
62#define RTC_FROM_DEV(devnode) ((rtc_t *) ((devnode)->driver_data))
63
64typedef struct rtc {
65 /** DDF device node */
66 ddf_dev_t *dev;
67 /** DDF function node */
68 ddf_fun_t *fun;
69 /** The fibril mutex for synchronizing the access to the device */
70 fibril_mutex_t mutex;
71 /** The base I/O address of the device registers */
72 uint32_t io_addr;
73 /** The I/O port used to access the CMOS registers */
74 ioport8_t *port;
75 /** true if device is removed */
76 bool removed;
77} rtc_t;
78
79static int rtc_time_get(ddf_fun_t *fun, struct tm *t);
80static int rtc_time_set(ddf_fun_t *fun, struct tm *t);
81static int rtc_dev_add(ddf_dev_t *dev);
82static int rtc_dev_initialize(rtc_t *rtc);
83static bool rtc_pio_enable(rtc_t *rtc);
84static void rtc_dev_cleanup(rtc_t *rtc);
85static int rtc_open(ddf_fun_t *fun);
86static void rtc_close(ddf_fun_t *fun);
87static bool rtc_update_in_progress(rtc_t *rtc);
88static int rtc_register_read(rtc_t *rtc, int reg);
89static unsigned bcd2bin(unsigned bcd);
90static unsigned bin2bcd(unsigned binary);
91static void rtc_default_handler(ddf_fun_t *fun,
92 ipc_callid_t callid, ipc_call_t *call);
93static int rtc_dev_remove(ddf_dev_t *dev);
94static void rtc_register_write(rtc_t *rtc, int reg, int data);
95static time_t uptime_get(void);
96
97static ddf_dev_ops_t rtc_dev_ops;
98static time_t boottime = 0;
99
100/** The RTC device driver's standard operations */
101static driver_ops_t rtc_ops = {
102 .dev_add = rtc_dev_add,
103 .dev_remove = rtc_dev_remove,
104};
105
106/** The RTC device driver structure */
107static driver_t rtc_driver = {
108 .name = NAME,
109 .driver_ops = &rtc_ops,
110};
111
112/** Clock interface */
113static clock_dev_ops_t rtc_clock_dev_ops = {
114 .time_get = rtc_time_get,
115 .time_set = rtc_time_set,
116};
117
118/** Initialize the RTC driver */
119static void
120rtc_init(void)
121{
122 ddf_log_init(NAME, LVL_ERROR);
123
124 rtc_dev_ops.open = rtc_open;
125 rtc_dev_ops.close = rtc_close;
126
127 rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
128 rtc_dev_ops.default_handler = &rtc_default_handler;
129}
130
131/** Clean up the RTC soft state
132 *
133 * @param rtc The RTC device
134 */
135static void
136rtc_dev_cleanup(rtc_t *rtc)
137{
138 if (rtc->dev->parent_sess) {
139 async_hangup(rtc->dev->parent_sess);
140 rtc->dev->parent_sess = NULL;
141 }
142}
143
144/** Enable the I/O ports of the device
145 *
146 * @param rtc The real time clock device
147 *
148 * @return true in case of success, false otherwise
149 */
150static bool
151rtc_pio_enable(rtc_t *rtc)
152{
153 if (pio_enable((void *)(uintptr_t) rtc->io_addr, REG_COUNT,
154 (void **) &rtc->port)) {
155
156 ddf_msg(LVL_ERROR, "Cannot map the port %#" PRIx32
157 " for device %s", rtc->io_addr, rtc->dev->name);
158 return false;
159 }
160
161 return true;
162}
163
164/** Initialize the RTC device
165 *
166 * @param rtc Pointer to the RTC device
167 *
168 * @return EOK on success or a negative error code
169 */
170static int
171rtc_dev_initialize(rtc_t *rtc)
172{
173 int rc;
174 size_t i;
175 hw_resource_t *res;
176 bool ioport = false;
177
178 ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", rtc->dev->name);
179
180 hw_resource_list_t hw_resources;
181 memset(&hw_resources, 0, sizeof(hw_resource_list_t));
182
183 /* Connect to the parent's driver */
184
185 rtc->dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
186 rtc->dev->handle, IPC_FLAG_BLOCKING);
187 if (!rtc->dev->parent_sess) {
188 ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
189 of device %s.", rtc->dev->name);
190 rc = ENOENT;
191 goto error;
192 }
193
194 /* Get the HW resources */
195 rc = hw_res_get_resource_list(rtc->dev->parent_sess, &hw_resources);
196 if (rc != EOK) {
197 ddf_msg(LVL_ERROR, "Failed to get HW resources\
198 for device %s", rtc->dev->name);
199 goto error;
200 }
201
202 for (i = 0; i < hw_resources.count; ++i) {
203 res = &hw_resources.resources[i];
204
205 if (res->type == IO_RANGE) {
206 if (res->res.io_range.size < REG_COUNT) {
207 ddf_msg(LVL_ERROR, "I/O range assigned to \
208 device %s is too small", rtc->dev->name);
209 rc = ELIMIT;
210 goto error;
211 }
212 rtc->io_addr = res->res.io_range.address;
213 ioport = true;
214 ddf_msg(LVL_NOTE, "Device %s was assigned I/O address \
215 0x%x", rtc->dev->name, rtc->io_addr);
216 }
217 }
218
219 if (!ioport) {
220 /* No I/O address assigned to this device */
221 ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
222 rtc->dev->name);
223 rc = ENOENT;
224 goto error;
225 }
226
227 hw_res_clean_resource_list(&hw_resources);
228
229 return EOK;
230
231error:
232 rtc_dev_cleanup(rtc);
233 hw_res_clean_resource_list(&hw_resources);
234
235 return rc;
236}
237
238/** Read a register from the CMOS memory
239 *
240 * @param rtc The rtc device
241 * @param reg The index of the register to read
242 *
243 * @return The value of the register
244 */
245static int
246rtc_register_read(rtc_t *rtc, int reg)
247{
248 pio_write_8(rtc->port, reg);
249 return pio_read_8(rtc->port + 1);
250}
251
252/** Write a register to the CMOS memory
253 *
254 * @param rtc The rtc device
255 * @param reg The index of the register to write
256 * @param data The data to write
257 */
258static void
259rtc_register_write(rtc_t *rtc, int reg, int data)
260{
261 pio_write_8(rtc->port, reg);
262 pio_write_8(rtc->port + 1, data);
263}
264
265/** Check if an update is in progress
266 *
267 * @param rtc The rtc device
268 *
269 * @return true if an update is in progress, false otherwise
270 */
271static bool
272rtc_update_in_progress(rtc_t *rtc)
273{
274 return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
275}
276
277/** Read the current time from the CMOS
278 *
279 * @param fun The RTC function
280 * @param t Pointer to the time variable
281 *
282 * @return EOK on success or a negative error code
283 */
284static int
285rtc_time_get(ddf_fun_t *fun, struct tm *t)
286{
287 bool bcd_mode;
288 bool pm_mode = false;
289 rtc_t *rtc = RTC_FROM_FNODE(fun);
290
291 if (boottime != 0) {
292 /* There is no need to read the current time from the
293 * device because it has already been cached.
294 */
295
296 time_t cur_time = boottime + uptime_get();
297 return localtime2tm(cur_time, t);
298 }
299
300 fibril_mutex_lock(&rtc->mutex);
301
302 /* now read the registers */
303 do {
304 /* Suspend until the update process has finished */
305 while (rtc_update_in_progress(rtc));
306
307 t->tm_sec = rtc_register_read(rtc, RTC_SEC);
308 t->tm_min = rtc_register_read(rtc, RTC_MIN);
309 t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
310 t->tm_mday = rtc_register_read(rtc, RTC_DAY);
311 t->tm_mon = rtc_register_read(rtc, RTC_MON);
312 t->tm_year = rtc_register_read(rtc, RTC_YEAR);
313
314 /* Now check if it is stable */
315 } while( t->tm_sec != rtc_register_read(rtc, RTC_SEC) ||
316 t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
317 t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
318 t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
319 t->tm_year != rtc_register_read(rtc, RTC_YEAR));
320
321 /* Check if the RTC is working in 12h mode */
322 bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
323 RTC_B_24H);
324
325 if (_12h_mode) {
326 /* The RTC is working in 12h mode, check if it is AM or PM */
327 if (t->tm_hour & 0x80) {
328 /* PM flag is active, it must be cleared */
329 t->tm_hour &= ~0x80;
330 pm_mode = true;
331 }
332 }
333
334 /* Check if the RTC is working in BCD mode */
335 bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
336
337 if (bcd_mode) {
338 t->tm_sec = bcd2bin(t->tm_sec);
339 t->tm_min = bcd2bin(t->tm_min);
340 t->tm_hour = bcd2bin(t->tm_hour);
341 t->tm_mday = bcd2bin(t->tm_mday);
342 t->tm_mon = bcd2bin(t->tm_mon);
343 t->tm_year = bcd2bin(t->tm_year);
344 }
345
346 if (_12h_mode) {
347 /* Convert to 24h mode */
348 if (pm_mode) {
349 if (t->tm_hour < 12)
350 t->tm_hour += 12;
351 } else if (t->tm_hour == 12)
352 t->tm_hour = 0;
353 }
354
355 /* Count the months starting from 0, not from 1 */
356 t->tm_mon--;
357
358 if (t->tm_year < 100) {
359 /* tm_year is the number of years since 1900 but the
360 * RTC epoch is 2000.
361 */
362 t->tm_year += 100;
363 }
364
365 fibril_mutex_unlock(&rtc->mutex);
366
367 /* Try to normalize the content of the tm structure */
368 time_t r = mktime(t);
369
370 boottime = r - uptime_get();
371
372 return r < 0 ? EINVAL : EOK;
373}
374
375/** Set the time in the RTC
376 *
377 * @param fun The RTC function
378 * @param t The time value to set
379 *
380 * @return EOK or a negative error code
381 */
382static int
383rtc_time_set(ddf_fun_t *fun, struct tm *t)
384{
385 bool bcd_mode;
386 time_t norm_time;
387 time_t uptime;
388 int reg_b;
389 int reg_a;
390 int epoch;
391 rtc_t *rtc = RTC_FROM_FNODE(fun);
392
393 /* Try to normalize the content of the tm structure */
394 if ((norm_time = mktime(t)) < 0)
395 return EINVAL;
396
397 uptime = uptime_get();
398 if (norm_time <= uptime) {
399 /* This is not acceptable */
400 return EINVAL;
401 }
402
403 /* boottime must be recomputed */
404 boottime = 0;
405
406 fibril_mutex_lock(&rtc->mutex);
407
408 /* Detect the RTC epoch */
409 if (rtc_register_read(rtc, RTC_YEAR) < 100)
410 epoch = 2000;
411 else
412 epoch = 1900;
413
414 if (epoch == 2000 && t->tm_year < 100) {
415 /* Can't set a year before the epoch */
416 fibril_mutex_unlock(&rtc->mutex);
417 return EINVAL;
418 }
419
420 t->tm_mon++; /* counts from 1, not from 0 */
421
422 reg_b = rtc_register_read(rtc, RTC_STATUS_B);
423
424 if (!(reg_b & RTC_B_24H)) {
425 /* Force 24h mode of operation */
426 reg_b |= RTC_B_24H;
427 rtc_register_write(rtc, RTC_STATUS_B, reg_b);
428 }
429
430 if (epoch == 2000) {
431 /* The RTC epoch is year 2000 but the tm_year
432 * field counts years since 1900.
433 */
434 t->tm_year -= 100;
435 }
436
437 /* Check if the rtc is working in bcd mode */
438 bcd_mode = !(reg_b & RTC_B_BCD);
439 if (bcd_mode) {
440 /* Convert the tm struct fields in BCD mode */
441 t->tm_sec = bin2bcd(t->tm_sec);
442 t->tm_min = bin2bcd(t->tm_min);
443 t->tm_hour = bin2bcd(t->tm_hour);
444 t->tm_mday = bin2bcd(t->tm_mday);
445 t->tm_mon = bin2bcd(t->tm_mon);
446 t->tm_year = bin2bcd(t->tm_year);
447 }
448
449 /* Inhibit updates */
450 rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
451
452 /* Write current time to RTC */
453 rtc_register_write(rtc, RTC_SEC, t->tm_sec);
454 rtc_register_write(rtc, RTC_MIN, t->tm_min);
455 rtc_register_write(rtc, RTC_HOUR, t->tm_hour);
456 rtc_register_write(rtc, RTC_DAY, t->tm_mday);
457 rtc_register_write(rtc, RTC_MON, t->tm_mon);
458 rtc_register_write(rtc, RTC_YEAR, t->tm_year);
459
460 /* Stop the clock */
461 reg_a = rtc_register_read(rtc, RTC_STATUS_A);
462 rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
463
464 /* Enable updates */
465 rtc_register_write(rtc, RTC_STATUS_B, reg_b);
466 rtc_register_write(rtc, RTC_STATUS_A, reg_a);
467
468 fibril_mutex_unlock(&rtc->mutex);
469
470 return EOK;
471}
472
473/** The dev_add callback of the rtc driver
474 *
475 * @param dev The RTC device
476 *
477 * @return EOK on success or a negative error code
478 */
479static int
480rtc_dev_add(ddf_dev_t *dev)
481{
482 rtc_t *rtc;
483 ddf_fun_t *fun = NULL;
484 int rc;
485 bool need_cleanup = false;
486
487 ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
488 dev->name, (int) dev->handle);
489
490 rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
491 if (!rtc)
492 return ENOMEM;
493
494 rtc->dev = dev;
495 fibril_mutex_initialize(&rtc->mutex);
496
497 rc = rtc_dev_initialize(rtc);
498 if (rc != EOK)
499 goto error;
500
501 need_cleanup = true;
502
503 if (!rtc_pio_enable(rtc)) {
504 rc = EADDRNOTAVAIL;
505 goto error;
506 }
507
508 fun = ddf_fun_create(dev, fun_exposed, "a");
509 if (!fun) {
510 ddf_msg(LVL_ERROR, "Failed creating function");
511 rc = ENOENT;
512 goto error;
513 }
514
515 fun->ops = &rtc_dev_ops;
516 rc = ddf_fun_bind(fun);
517 if (rc != EOK) {
518 ddf_msg(LVL_ERROR, "Failed binding function");
519 goto error;
520 }
521
522 rtc->fun = fun;
523
524 ddf_fun_add_to_category(fun, "clock");
525
526 ddf_msg(LVL_NOTE, "Device %s successfully initialized",
527 dev->name);
528
529 return rc;
530
531error:
532 if (fun)
533 ddf_fun_destroy(fun);
534 if (need_cleanup)
535 rtc_dev_cleanup(rtc);
536 return rc;
537}
538
539/** The dev_remove callback for the rtc driver
540 *
541 * @param dev The RTC device
542 *
543 * @return EOK on success or a negative error code
544 */
545static int
546rtc_dev_remove(ddf_dev_t *dev)
547{
548 rtc_t *rtc = RTC_FROM_DEV(dev);
549 int rc;
550
551 fibril_mutex_lock(&rtc->mutex);
552
553 rtc->removed = true;
554 fibril_mutex_unlock(&rtc->mutex);
555
556 rc = ddf_fun_unbind(rtc->fun);
557 if (rc != EOK) {
558 ddf_msg(LVL_ERROR, "Failed to unbind function");
559 return rc;
560 }
561
562 ddf_fun_destroy(rtc->fun);
563 rtc_dev_cleanup(rtc);
564
565 return rc;
566}
567
568/** Default handler for client requests not handled
569 * by the standard interface
570 */
571static void
572rtc_default_handler(ddf_fun_t *fun, ipc_callid_t callid, ipc_call_t *call)
573{
574 sysarg_t method = IPC_GET_IMETHOD(*call);
575 rtc_t *rtc = RTC_FROM_FNODE(fun);
576 bool batt_ok;
577
578 switch (method) {
579 case CLOCK_GET_BATTERY_STATUS:
580 /* Get the RTC battery status */
581 batt_ok = rtc_register_read(rtc, RTC_STATUS_D) &
582 RTC_D_BATTERY_OK;
583 async_answer_1(callid, EOK, batt_ok);
584 break;
585 default:
586 async_answer_0(callid, ENOTSUP);
587 }
588}
589
590/** Open the device
591 *
592 * @param fun The function node
593 *
594 * @return EOK on success or a negative error code
595 */
596static int
597rtc_open(ddf_fun_t *fun)
598{
599 int rc;
600 rtc_t *rtc = RTC_FROM_FNODE(fun);
601
602 fibril_mutex_lock(&rtc->mutex);
603
604 if (rtc->removed)
605 rc = ENXIO;
606 else
607 rc = EOK;
608
609 fibril_mutex_unlock(&rtc->mutex);
610 return rc;
611}
612
613/** Close the device
614 *
615 * @param fun The function node
616 */
617static void
618rtc_close(ddf_fun_t *fun)
619{
620}
621
622/** Convert from BCD mode to binary mode
623 *
624 * @param bcd The number in BCD format to convert
625 *
626 * @return The converted value
627 */
628static unsigned
629bcd2bin(unsigned bcd)
630{
631 return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
632}
633
634/** Convert from binary mode to BCD mode
635 *
636 * @param bcd The number in binary mode to convert
637 *
638 * @return The converted value
639 */
640static unsigned
641bin2bcd(unsigned binary)
642{
643 return ((binary / 10) << 4) + (binary % 10);
644}
645
646static time_t
647uptime_get(void)
648{
649 struct timeval tv;
650
651 getuptime(&tv);
652
653 return tv.tv_sec;
654}
655
656int
657main(int argc, char **argv)
658{
659 printf(NAME ": HelenOS RTC driver\n");
660 rtc_init();
661 return ddf_driver_main(&rtc_driver);
662}
663
664/**
665 * @}
666 */
Note: See TracBrowser for help on using the repository browser.