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

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

rtc: make the driver - port IO / memory mapped IO - neutral.

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