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

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

rtc: Add support to the battery_status_get() request

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