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

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

rtc: add some comments

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