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
RevLine 
[1e19a15]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
[4863bb52]38#include <errno.h>
[1e19a15]39#include <ddi.h>
[8fde078]40#include <as.h>
41#include <sysinfo.h>
[f30ee571]42#include <libarch/ddi.h>
[8fde078]43#include <libarch/barrier.h>
[1e19a15]44#include <stdio.h>
45#include <ddf/driver.h>
[a2e4889]46#include <ddf/log.h>
[aeef318]47#include <ops/clock_dev.h>
[917797f]48#include <ops/battery_dev.h>
[6b329749]49#include <fibril_synch.h>
[b3db669]50#include <device/hw_res.h>
[8fde078]51#include <macros.h>
[83298e8]52#include <time.h>
[1e19a15]53
[f30ee571]54#include "cmos-regs.h"
55
[5ef13847]56#define NAME "cmos-rtc"
[1e19a15]57
[0883de8]58#define REG_COUNT 2
59
[78ca12b]60#define REG_SEL_PORT(port) (port)
61#define REG_RW_PORT(port) ((port) + 1)
62
[b3db669]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;
[c47f1b6]70 /** The base I/O address of the device registers */
[971f50e7]71 ioport8_t *io_addr;
[0883de8]72 /** The I/O port used to access the CMOS registers */
73 ioport8_t *port;
[bb8f69d]74 /** true if device is removed */
75 bool removed;
[9673fd3]76 /** number of connected clients */
77 int clients_connected;
[60af6fc2]78 /** time at which the system booted */
79 time_t boottime;
[b3db669]80} rtc_t;
81
[6f445a67]82static rtc_t *dev_rtc(ddf_dev_t *dev);
83static rtc_t *fun_rtc(ddf_fun_t *fun);
[efcfab9]84static int
85rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status);
[2a5171db]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);
[f30ee571]94static bool rtc_update_in_progress(rtc_t *rtc);
[db8d552]95static int rtc_register_read(rtc_t *rtc, int reg);
[1170ea3c]96static unsigned bcd2bin(unsigned bcd);
97static unsigned bin2bcd(unsigned binary);
[bb8f69d]98static int rtc_dev_remove(ddf_dev_t *dev);
[a8a0d43]99static void rtc_register_write(rtc_t *rtc, int reg, int data);
[8fde078]100static time_t uptime_get(void);
[4863bb52]101
[1e19a15]102static ddf_dev_ops_t rtc_dev_ops;
103
[a31ca11f]104/** The RTC device driver's standard operations */
105static driver_ops_t rtc_ops = {
106 .dev_add = rtc_dev_add,
[bb8f69d]107 .dev_remove = rtc_dev_remove,
[a31ca11f]108};
109
110/** The RTC device driver structure */
[1e19a15]111static driver_t rtc_driver = {
112 .name = NAME,
[a31ca11f]113 .driver_ops = &rtc_ops,
[1e19a15]114};
115
[a31ca11f]116/** Clock interface */
[4863bb52]117static clock_dev_ops_t rtc_clock_dev_ops = {
118 .time_get = rtc_time_get,
119 .time_set = rtc_time_set,
120};
121
[917797f]122/** Battery powered device interface */
123static battery_dev_ops_t rtc_battery_dev_ops = {
[efcfab9]124 .battery_status_get = rtc_battery_status_get,
[917797f]125 .battery_charge_level_get = NULL,
126};
127
[6f445a67]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
[4863bb52]142/** Initialize the RTC driver */
[1e19a15]143static void
144rtc_init(void)
145{
146 ddf_log_init(NAME, LVL_ERROR);
147
[923b2eba]148 rtc_dev_ops.open = rtc_open;
[2a5171db]149 rtc_dev_ops.close = rtc_close;
[4863bb52]150
151 rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
[917797f]152 rtc_dev_ops.interfaces[BATTERY_DEV_IFACE] = &rtc_battery_dev_ops;
153 rtc_dev_ops.default_handler = NULL;
[4863bb52]154}
155
[4b44de57]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
[0883de8]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{
[85f7369]174 if (pio_enable((void *) rtc->io_addr, REG_COUNT,
[0883de8]175 (void **) &rtc->port)) {
176
[276e44a]177 ddf_msg(LVL_ERROR, "Cannot map the port %lx"
[7ff35c7]178 " for device %s", (long unsigned int)rtc->io_addr,
[276e44a]179 ddf_dev_get_name(rtc->dev));
[0883de8]180 return false;
181 }
182
183 return true;
184}
185
[b3db669]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;
[c47f1b6]196 size_t i;
197 hw_resource_t *res;
198 bool ioport = false;
[6f445a67]199 async_sess_t *parent_sess;
[b3db669]200
[6f445a67]201 ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev));
[b3db669]202
[762083b]203 rtc->boottime = 0;
[9673fd3]204 rtc->clients_connected = 0;
[762083b]205
[b3db669]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
[6f445a67]211 parent_sess = ddf_dev_parent_sess_create(rtc->dev, EXCHANGE_SERIALIZE);
212 if (!parent_sess) {
[b3db669]213 ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
[6f445a67]214 of device %s.", ddf_dev_get_name(rtc->dev));
[4b44de57]215 rc = ENOENT;
216 goto error;
[b3db669]217 }
218
219 /* Get the HW resources */
[6f445a67]220 rc = hw_res_get_resource_list(parent_sess, &hw_resources);
[b3db669]221 if (rc != EOK) {
222 ddf_msg(LVL_ERROR, "Failed to get HW resources\
[6f445a67]223 for device %s", ddf_dev_get_name(rtc->dev));
[4b44de57]224 goto error;
[b3db669]225 }
226
[c47f1b6]227 for (i = 0; i < hw_resources.count; ++i) {
228 res = &hw_resources.resources[i];
229
[ae827d0]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;
[c47f1b6]236 }
[ae827d0]237
[7ff35c7]238 rtc->io_addr = (ioport8_t *) (long) res->res.io_range.address;
[ae827d0]239 ioport = true;
240 ddf_msg(LVL_NOTE, "Device %s was assigned I/O address "
[276e44a]241 "0x%lx", ddf_dev_get_name(rtc->dev),
[7ff35c7]242 (unsigned long int) rtc->io_addr);
[ae827d0]243 rc = EOK;
244 break;
[c47f1b6]245 }
246
[ae827d0]247 if (rc != EOK)
248 goto error;
249
[c47f1b6]250 if (!ioport) {
251 /* No I/O address assigned to this device */
252 ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
[6f445a67]253 ddf_dev_get_name(rtc->dev));
[4b44de57]254 rc = ENOENT;
255 goto error;
[c47f1b6]256 }
257
258 hw_res_clean_resource_list(&hw_resources);
259
[b3db669]260 return EOK;
[4b44de57]261
262error:
263 rtc_dev_cleanup(rtc);
264 hw_res_clean_resource_list(&hw_resources);
265
266 return rc;
[b3db669]267}
268
[3b79ba5]269/** Read a register from the CMOS memory
270 *
[1170ea3c]271 * @param rtc The rtc device
[3b79ba5]272 * @param reg The index of the register to read
273 *
274 * @return The value of the register
275 */
276static int
[db8d552]277rtc_register_read(rtc_t *rtc, int reg)
[3b79ba5]278{
[78ca12b]279 pio_write_8(REG_SEL_PORT(rtc->port), reg);
280 return pio_read_8(REG_RW_PORT(rtc->port));
[3b79ba5]281}
282
[1170ea3c]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{
[78ca12b]292 pio_write_8(REG_SEL_PORT(rtc->port), reg);
293 pio_write_8(REG_RW_PORT(rtc->port), data);
[1170ea3c]294}
295
[f30ee571]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{
[6a3808e]305 return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
[f30ee571]306}
307
[0b8a3e7]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 */
[4863bb52]315static int
[8d2963d]316rtc_time_get(ddf_fun_t *fun, struct tm *t)
[4863bb52]317{
[db8d552]318 bool bcd_mode;
[95060d5b]319 bool pm_mode = false;
[6f445a67]320 rtc_t *rtc = fun_rtc(fun);
[f30ee571]321
[60af6fc2]322 fibril_mutex_lock(&rtc->mutex);
323
324 if (rtc->boottime != 0) {
[ea5cc5b]325 /* There is no need to read the current time from the
326 * device because it has already been cached.
327 */
328
[60af6fc2]329 time_t cur_time = rtc->boottime + uptime_get();
330
331 fibril_mutex_unlock(&rtc->mutex);
332
[664fc031]333 return time_local2tm(cur_time, t);
[ea5cc5b]334 }
335
[db8d552]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 */
[c9abf50]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));
[db8d552]354
[95060d5b]355 /* Check if the RTC is working in 12h mode */
356 bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
[6a3808e]357 RTC_B_24H);
[95060d5b]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) {
[f004318]362 /* PM flag is active, it must be cleared */
[95060d5b]363 t->tm_hour &= ~0x80;
364 pm_mode = true;
365 }
366 }
367
[db8d552]368 /* Check if the RTC is working in BCD mode */
[6a3808e]369 bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
[db8d552]370
[fc7d28e]371 if (bcd_mode) {
[1170ea3c]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);
[db8d552]378 }
[f30ee571]379
[95060d5b]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
[05ed9d7]389 /* Count the months starting from 0, not from 1 */
390 t->tm_mon--;
391
[fc7d28e]392 if (t->tm_year < 100) {
[e5fbe06]393 /* tm_year is the number of years since 1900 but the
394 * RTC epoch is 2000.
[fc7d28e]395 */
396 t->tm_year += 100;
397 }
398
[af7e3d3]399 /* Try to normalize the content of the tm structure */
[fa18523]400 time_t r = mktime(t);
401
[60af6fc2]402 rtc->boottime = r - uptime_get();
403
404 fibril_mutex_unlock(&rtc->mutex);
[ea5cc5b]405
[af7e3d3]406 return r < 0 ? EINVAL : EOK;
[4863bb52]407}
408
[0b8a3e7]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 */
[4863bb52]416static int
[8d2963d]417rtc_time_set(ddf_fun_t *fun, struct tm *t)
[4863bb52]418{
[1170ea3c]419 bool bcd_mode;
[8fde078]420 time_t norm_time;
421 time_t uptime;
[a8a0d43]422 int reg_b;
423 int reg_a;
[e5fbe06]424 int epoch;
[6f445a67]425 rtc_t *rtc = fun_rtc(fun);
[1170ea3c]426
[af7e3d3]427 /* Try to normalize the content of the tm structure */
[8fde078]428 if ((norm_time = mktime(t)) < 0)
[fa18523]429 return EINVAL;
430
[8fde078]431 uptime = uptime_get();
432 if (norm_time <= uptime) {
433 /* This is not acceptable */
434 return EINVAL;
435 }
436
[e5fbe06]437 fibril_mutex_lock(&rtc->mutex);
438
[60af6fc2]439 /* boottime must be recomputed */
440 rtc->boottime = 0;
441
[e5fbe06]442 /* Detect the RTC epoch */
443 if (rtc_register_read(rtc, RTC_YEAR) < 100)
444 epoch = 2000;
445 else
446 epoch = 1900;
447
[fa18523]448 if (epoch == 2000 && t->tm_year < 100) {
449 /* Can't set a year before the epoch */
[e5fbe06]450 fibril_mutex_unlock(&rtc->mutex);
[fa18523]451 return EINVAL;
[e5fbe06]452 }
[1170ea3c]453
[a8a0d43]454 t->tm_mon++; /* counts from 1, not from 0 */
[1170ea3c]455
[a8a0d43]456 reg_b = rtc_register_read(rtc, RTC_STATUS_B);
457
[6a3808e]458 if (!(reg_b & RTC_B_24H)) {
[f004318]459 /* Force 24h mode of operation */
[6a3808e]460 reg_b |= RTC_B_24H;
[f004318]461 rtc_register_write(rtc, RTC_STATUS_B, reg_b);
462 }
[f6af126]463
[a44b58c]464 if (epoch == 2000) {
465 /* The RTC epoch is year 2000 but the tm_year
466 * field counts years since 1900.
467 */
[074324f1]468 t->tm_year -= 100;
469 }
470
[1170ea3c]471 /* Check if the rtc is working in bcd mode */
[6a3808e]472 bcd_mode = !(reg_b & RTC_B_BCD);
[1170ea3c]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);
[709476f4]479 t->tm_mon = bin2bcd(t->tm_mon);
[1170ea3c]480 t->tm_year = bin2bcd(t->tm_year);
481 }
482
[a8a0d43]483 /* Inhibit updates */
[6a3808e]484 rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
[a8a0d43]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);
[6a3808e]496 rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
[a8a0d43]497
498 /* Enable updates */
499 rtc_register_write(rtc, RTC_STATUS_B, reg_b);
500 rtc_register_write(rtc, RTC_STATUS_A, reg_a);
[1170ea3c]501
502 fibril_mutex_unlock(&rtc->mutex);
503
[4863bb52]504 return EOK;
[1e19a15]505}
506
[efcfab9]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
[a31ca11f]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{
[6b329749]535 rtc_t *rtc;
[4b44de57]536 ddf_fun_t *fun = NULL;
[b3db669]537 int rc;
[4b44de57]538 bool need_cleanup = false;
[6b329749]539
540 ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
[6f445a67]541 ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
[6b329749]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
[b3db669]550 rc = rtc_dev_initialize(rtc);
551 if (rc != EOK)
[4b44de57]552 goto error;
[b3db669]553
[4b44de57]554 need_cleanup = true;
555
556 if (!rtc_pio_enable(rtc)) {
557 rc = EADDRNOTAVAIL;
558 goto error;
559 }
[0883de8]560
561 fun = ddf_fun_create(dev, fun_exposed, "a");
562 if (!fun) {
563 ddf_msg(LVL_ERROR, "Failed creating function");
[4b44de57]564 rc = ENOENT;
565 goto error;
[0883de8]566 }
567
[6f445a67]568 ddf_fun_set_ops(fun, &rtc_dev_ops);
[0883de8]569 rc = ddf_fun_bind(fun);
570 if (rc != EOK) {
571 ddf_msg(LVL_ERROR, "Failed binding function");
[4b44de57]572 goto error;
[0883de8]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",
[6f445a67]580 ddf_dev_get_name(dev));
[0883de8]581
[b3db669]582 return rc;
[4b44de57]583
584error:
585 if (fun)
586 ddf_fun_destroy(fun);
587 if (need_cleanup)
588 rtc_dev_cleanup(rtc);
589 return rc;
[a31ca11f]590}
591
[bb8f69d]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{
[6f445a67]601 rtc_t *rtc = dev_rtc(dev);
[bb8f69d]602 int rc;
603
604 fibril_mutex_lock(&rtc->mutex);
[9673fd3]605 if (rtc->clients_connected > 0) {
606 fibril_mutex_unlock(&rtc->mutex);
607 return EBUSY;
608 }
[bb8f69d]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
[923b2eba]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;
[6f445a67]635 rtc_t *rtc = fun_rtc(fun);
[923b2eba]636
637 fibril_mutex_lock(&rtc->mutex);
638
[43e660c]639 if (rtc->removed)
[bb8f69d]640 rc = ENXIO;
[9673fd3]641 else {
[923b2eba]642 rc = EOK;
[9673fd3]643 rtc->clients_connected++;
644 }
[923b2eba]645
646 fibril_mutex_unlock(&rtc->mutex);
647 return rc;
648}
649
[2a5171db]650/** Close the device
651 *
652 * @param fun The function node
653 */
654static void
655rtc_close(ddf_fun_t *fun)
656{
[9673fd3]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);
[2a5171db]665}
666
[db8d552]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 */
[1170ea3c]673static unsigned
674bcd2bin(unsigned bcd)
[db8d552]675{
676 return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
677}
678
[1170ea3c]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
[8fde078]691static time_t
692uptime_get(void)
693{
[83298e8]694 struct timeval tv;
[8fde078]695
[83298e8]696 getuptime(&tv);
697
698 return tv.tv_sec;
[8fde078]699}
700
[1e19a15]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.