source: mainline/uspace/drv/time/cmos-rtc/cmos-rtc.c@ 971f50e7

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

rtc: use ioport8_t* instead of uint32_t* as the type of the io_addr pointer.

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