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 <devman.h>
|
---|
51 | #include <macros.h>
|
---|
52 | #include <ipc/clock_ctl.h>
|
---|
53 |
|
---|
54 | #include "cmos-regs.h"
|
---|
55 |
|
---|
56 | #define NAME "cmos-rtc"
|
---|
57 |
|
---|
58 | #define REG_COUNT 2
|
---|
59 |
|
---|
60 | #define RTC_FROM_FNODE(fnode) ((rtc_t *) ((fnode)->dev->driver_data))
|
---|
61 | #define RTC_FROM_DEV(devnode) ((rtc_t *) ((devnode)->driver_data))
|
---|
62 |
|
---|
63 | typedef 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 | uint32_t io_addr;
|
---|
72 | /** The I/O port used to access the CMOS registers */
|
---|
73 | ioport8_t *port;
|
---|
74 | /** true if a client is connected to the device */
|
---|
75 | bool client_connected;
|
---|
76 | /** true if device is removed */
|
---|
77 | bool removed;
|
---|
78 | } rtc_t;
|
---|
79 |
|
---|
80 | /** Pointer to the kernel shared variables with time */
|
---|
81 | struct {
|
---|
82 | volatile sysarg_t seconds1;
|
---|
83 | volatile sysarg_t useconds;
|
---|
84 | volatile sysarg_t seconds2;
|
---|
85 | } *kuptime = NULL;
|
---|
86 |
|
---|
87 | static int rtc_time_get(ddf_fun_t *fun, struct tm *t);
|
---|
88 | static int rtc_time_set(ddf_fun_t *fun, struct tm *t);
|
---|
89 | static int rtc_dev_add(ddf_dev_t *dev);
|
---|
90 | static int rtc_dev_initialize(rtc_t *rtc);
|
---|
91 | static bool rtc_pio_enable(rtc_t *rtc);
|
---|
92 | static void rtc_dev_cleanup(rtc_t *rtc);
|
---|
93 | static int rtc_open(ddf_fun_t *fun);
|
---|
94 | static void rtc_close(ddf_fun_t *fun);
|
---|
95 | static bool rtc_update_in_progress(rtc_t *rtc);
|
---|
96 | static int rtc_register_read(rtc_t *rtc, int reg);
|
---|
97 | static unsigned bcd2bin(unsigned bcd);
|
---|
98 | static unsigned bin2bcd(unsigned binary);
|
---|
99 | static void rtc_default_handler(ddf_fun_t *fun,
|
---|
100 | ipc_callid_t callid, ipc_call_t *call);
|
---|
101 | static int rtc_dev_remove(ddf_dev_t *dev);
|
---|
102 | static void rtc_register_write(rtc_t *rtc, int reg, int data);
|
---|
103 | static time_t uptime_get(void);
|
---|
104 |
|
---|
105 | static ddf_dev_ops_t rtc_dev_ops;
|
---|
106 | static time_t boottime = 0;
|
---|
107 |
|
---|
108 | /** The RTC device driver's standard operations */
|
---|
109 | static driver_ops_t rtc_ops = {
|
---|
110 | .dev_add = rtc_dev_add,
|
---|
111 | .dev_remove = rtc_dev_remove,
|
---|
112 | };
|
---|
113 |
|
---|
114 | /** The RTC device driver structure */
|
---|
115 | static driver_t rtc_driver = {
|
---|
116 | .name = NAME,
|
---|
117 | .driver_ops = &rtc_ops,
|
---|
118 | };
|
---|
119 |
|
---|
120 | /** Clock interface */
|
---|
121 | static 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 */
|
---|
127 | static void
|
---|
128 | rtc_init(void)
|
---|
129 | {
|
---|
130 | ddf_log_init(NAME, LVL_ERROR);
|
---|
131 |
|
---|
132 | rtc_dev_ops.open = rtc_open;
|
---|
133 | rtc_dev_ops.close = rtc_close;
|
---|
134 |
|
---|
135 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
---|
136 | rtc_dev_ops.default_handler = &rtc_default_handler;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Clean up the RTC soft state
|
---|
140 | *
|
---|
141 | * @param rtc The RTC device
|
---|
142 | */
|
---|
143 | static void
|
---|
144 | rtc_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 |
|
---|
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 | */
|
---|
158 | static bool
|
---|
159 | rtc_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 |
|
---|
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 | */
|
---|
178 | static int
|
---|
179 | rtc_dev_initialize(rtc_t *rtc)
|
---|
180 | {
|
---|
181 | int rc;
|
---|
182 | size_t i;
|
---|
183 | hw_resource_t *res;
|
---|
184 | bool ioport = false;
|
---|
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);
|
---|
198 | rc = ENOENT;
|
---|
199 | goto error;
|
---|
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);
|
---|
207 | goto error;
|
---|
208 | }
|
---|
209 |
|
---|
210 | for (i = 0; i < hw_resources.count; ++i) {
|
---|
211 | res = &hw_resources.resources[i];
|
---|
212 |
|
---|
213 | if (res->type == IO_RANGE) {
|
---|
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 | }
|
---|
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);
|
---|
231 | rc = ENOENT;
|
---|
232 | goto error;
|
---|
233 | }
|
---|
234 |
|
---|
235 | hw_res_clean_resource_list(&hw_resources);
|
---|
236 |
|
---|
237 | return EOK;
|
---|
238 |
|
---|
239 | error:
|
---|
240 | rtc_dev_cleanup(rtc);
|
---|
241 | hw_res_clean_resource_list(&hw_resources);
|
---|
242 |
|
---|
243 | return rc;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /** Read a register from the CMOS memory
|
---|
247 | *
|
---|
248 | * @param rtc The rtc device
|
---|
249 | * @param reg The index of the register to read
|
---|
250 | *
|
---|
251 | * @return The value of the register
|
---|
252 | */
|
---|
253 | static int
|
---|
254 | rtc_register_read(rtc_t *rtc, int reg)
|
---|
255 | {
|
---|
256 | pio_write_8(rtc->port, reg);
|
---|
257 | return pio_read_8(rtc->port + 1);
|
---|
258 | }
|
---|
259 |
|
---|
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 | */
|
---|
266 | static void
|
---|
267 | rtc_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 |
|
---|
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 | */
|
---|
279 | static bool
|
---|
280 | rtc_update_in_progress(rtc_t *rtc)
|
---|
281 | {
|
---|
282 | return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
|
---|
283 | }
|
---|
284 |
|
---|
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 | */
|
---|
292 | static int
|
---|
293 | rtc_time_get(ddf_fun_t *fun, struct tm *t)
|
---|
294 | {
|
---|
295 | bool bcd_mode;
|
---|
296 | bool pm_mode = false;
|
---|
297 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
298 |
|
---|
299 | fibril_mutex_lock(&rtc->mutex);
|
---|
300 |
|
---|
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 */
|
---|
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));
|
---|
319 |
|
---|
320 | /* Check if the RTC is working in 12h mode */
|
---|
321 | bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
|
---|
322 | RTC_B_24H);
|
---|
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) {
|
---|
327 | /* PM flag is active, it must be cleared */
|
---|
328 | t->tm_hour &= ~0x80;
|
---|
329 | pm_mode = true;
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | /* Check if the RTC is working in BCD mode */
|
---|
334 | bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
|
---|
335 |
|
---|
336 | if (bcd_mode) {
|
---|
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);
|
---|
343 | }
|
---|
344 |
|
---|
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 |
|
---|
354 | /* Count the months starting from 0, not from 1 */
|
---|
355 | t->tm_mon--;
|
---|
356 |
|
---|
357 | if (t->tm_year < 100) {
|
---|
358 | /* tm_year is the number of years since 1900 but the
|
---|
359 | * RTC epoch is 2000.
|
---|
360 | */
|
---|
361 | t->tm_year += 100;
|
---|
362 | }
|
---|
363 |
|
---|
364 | fibril_mutex_unlock(&rtc->mutex);
|
---|
365 |
|
---|
366 | /* Try to normalize the content of the tm structure */
|
---|
367 | time_t r = mktime(t);
|
---|
368 |
|
---|
369 | return r < 0 ? EINVAL : EOK;
|
---|
370 | }
|
---|
371 |
|
---|
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 | */
|
---|
379 | static int
|
---|
380 | rtc_time_set(ddf_fun_t *fun, struct tm *t)
|
---|
381 | {
|
---|
382 | bool bcd_mode;
|
---|
383 | time_t norm_time;
|
---|
384 | time_t uptime;
|
---|
385 | int reg_b;
|
---|
386 | int reg_a;
|
---|
387 | int epoch;
|
---|
388 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
389 |
|
---|
390 | /* Try to normalize the content of the tm structure */
|
---|
391 | if ((norm_time = mktime(t)) < 0)
|
---|
392 | return EINVAL;
|
---|
393 |
|
---|
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 |
|
---|
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 |
|
---|
411 | if (epoch == 2000 && t->tm_year < 100) {
|
---|
412 | /* Can't set a year before the epoch */
|
---|
413 | fibril_mutex_unlock(&rtc->mutex);
|
---|
414 | return EINVAL;
|
---|
415 | }
|
---|
416 |
|
---|
417 | t->tm_mon++; /* counts from 1, not from 0 */
|
---|
418 |
|
---|
419 | reg_b = rtc_register_read(rtc, RTC_STATUS_B);
|
---|
420 |
|
---|
421 | if (!(reg_b & RTC_B_24H)) {
|
---|
422 | /* Force 24h mode of operation */
|
---|
423 | reg_b |= RTC_B_24H;
|
---|
424 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
425 | }
|
---|
426 |
|
---|
427 | if (epoch == 2000) {
|
---|
428 | /* The RTC epoch is year 2000 but the tm_year
|
---|
429 | * field counts years since 1900.
|
---|
430 | */
|
---|
431 | t->tm_year -= 100;
|
---|
432 | }
|
---|
433 |
|
---|
434 | /* Check if the rtc is working in bcd mode */
|
---|
435 | bcd_mode = !(reg_b & RTC_B_BCD);
|
---|
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);
|
---|
442 | t->tm_mon = bin2bcd(t->tm_mon);
|
---|
443 | t->tm_year = bin2bcd(t->tm_year);
|
---|
444 | }
|
---|
445 |
|
---|
446 | /* Inhibit updates */
|
---|
447 | rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
|
---|
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);
|
---|
459 | rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
|
---|
460 |
|
---|
461 | /* Enable updates */
|
---|
462 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
463 | rtc_register_write(rtc, RTC_STATUS_A, reg_a);
|
---|
464 |
|
---|
465 | fibril_mutex_unlock(&rtc->mutex);
|
---|
466 |
|
---|
467 | return EOK;
|
---|
468 | }
|
---|
469 |
|
---|
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 | */
|
---|
476 | static int
|
---|
477 | rtc_dev_add(ddf_dev_t *dev)
|
---|
478 | {
|
---|
479 | rtc_t *rtc;
|
---|
480 | ddf_fun_t *fun = NULL;
|
---|
481 | int rc;
|
---|
482 | bool need_cleanup = false;
|
---|
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 |
|
---|
494 | rc = rtc_dev_initialize(rtc);
|
---|
495 | if (rc != EOK)
|
---|
496 | goto error;
|
---|
497 |
|
---|
498 | need_cleanup = true;
|
---|
499 |
|
---|
500 | if (!rtc_pio_enable(rtc)) {
|
---|
501 | rc = EADDRNOTAVAIL;
|
---|
502 | goto error;
|
---|
503 | }
|
---|
504 |
|
---|
505 | fun = ddf_fun_create(dev, fun_exposed, "a");
|
---|
506 | if (!fun) {
|
---|
507 | ddf_msg(LVL_ERROR, "Failed creating function");
|
---|
508 | rc = ENOENT;
|
---|
509 | goto error;
|
---|
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");
|
---|
516 | goto error;
|
---|
517 | }
|
---|
518 |
|
---|
519 | rtc->fun = fun;
|
---|
520 |
|
---|
521 | ddf_fun_add_to_category(fun, "clock");
|
---|
522 |
|
---|
523 | rtc->client_connected = false;
|
---|
524 |
|
---|
525 | ddf_msg(LVL_NOTE, "Device %s successfully initialized",
|
---|
526 | dev->name);
|
---|
527 |
|
---|
528 | return rc;
|
---|
529 |
|
---|
530 | error:
|
---|
531 | if (fun)
|
---|
532 | ddf_fun_destroy(fun);
|
---|
533 | if (need_cleanup)
|
---|
534 | rtc_dev_cleanup(rtc);
|
---|
535 | return rc;
|
---|
536 | }
|
---|
537 |
|
---|
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 | */
|
---|
544 | static int
|
---|
545 | rtc_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 |
|
---|
571 | /** Default handler for client requests not handled
|
---|
572 | * by the standard interface
|
---|
573 | */
|
---|
574 | static void
|
---|
575 | rtc_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;
|
---|
580 | sysarg_t r = EOK;
|
---|
581 |
|
---|
582 | switch (method) {
|
---|
583 | case CLOCK_GET_BATTERY_STATUS:
|
---|
584 | batt_ok = rtc_register_read(rtc, RTC_STATUS_D) &
|
---|
585 | RTC_D_BATTERY_OK;
|
---|
586 | async_answer_1(callid, EOK, batt_ok);
|
---|
587 | break;
|
---|
588 | case CLOCK_GET_BOOTTIME:
|
---|
589 | if (boottime == 0) {
|
---|
590 | struct tm cur_tm;
|
---|
591 | time_t uptime;
|
---|
592 |
|
---|
593 | uptime = uptime_get();
|
---|
594 | r = rtc_time_get(fun, &cur_tm);
|
---|
595 | if (r == EOK) {
|
---|
596 | time_t current_time = mktime(&cur_tm);
|
---|
597 | if (current_time < uptime)
|
---|
598 | r = EINVAL;
|
---|
599 | else
|
---|
600 | boottime = current_time - uptime;
|
---|
601 | }
|
---|
602 | }
|
---|
603 | async_answer_1(callid, r, boottime);
|
---|
604 | break;
|
---|
605 | default:
|
---|
606 | async_answer_0(callid, ENOTSUP);
|
---|
607 | }
|
---|
608 | }
|
---|
609 |
|
---|
610 | /** Open the device
|
---|
611 | *
|
---|
612 | * @param fun The function node
|
---|
613 | *
|
---|
614 | * @return EOK on success or a negative error code
|
---|
615 | */
|
---|
616 | static int
|
---|
617 | rtc_open(ddf_fun_t *fun)
|
---|
618 | {
|
---|
619 | int rc;
|
---|
620 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
621 |
|
---|
622 | fibril_mutex_lock(&rtc->mutex);
|
---|
623 |
|
---|
624 | if (rtc->client_connected)
|
---|
625 | rc = ELIMIT;
|
---|
626 | else if (rtc->removed)
|
---|
627 | rc = ENXIO;
|
---|
628 | else {
|
---|
629 | rc = EOK;
|
---|
630 | rtc->client_connected = true;
|
---|
631 | }
|
---|
632 |
|
---|
633 | fibril_mutex_unlock(&rtc->mutex);
|
---|
634 | return rc;
|
---|
635 | }
|
---|
636 |
|
---|
637 | /** Close the device
|
---|
638 | *
|
---|
639 | * @param fun The function node
|
---|
640 | */
|
---|
641 | static void
|
---|
642 | rtc_close(ddf_fun_t *fun)
|
---|
643 | {
|
---|
644 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
645 |
|
---|
646 | fibril_mutex_lock(&rtc->mutex);
|
---|
647 |
|
---|
648 | assert(rtc->client_connected);
|
---|
649 | rtc->client_connected = false;
|
---|
650 |
|
---|
651 | fibril_mutex_unlock(&rtc->mutex);
|
---|
652 | }
|
---|
653 |
|
---|
654 | /** Convert from BCD mode to binary mode
|
---|
655 | *
|
---|
656 | * @param bcd The number in BCD format to convert
|
---|
657 | *
|
---|
658 | * @return The converted value
|
---|
659 | */
|
---|
660 | static unsigned
|
---|
661 | bcd2bin(unsigned bcd)
|
---|
662 | {
|
---|
663 | return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
|
---|
664 | }
|
---|
665 |
|
---|
666 | /** Convert from binary mode to BCD mode
|
---|
667 | *
|
---|
668 | * @param bcd The number in binary mode to convert
|
---|
669 | *
|
---|
670 | * @return The converted value
|
---|
671 | */
|
---|
672 | static unsigned
|
---|
673 | bin2bcd(unsigned binary)
|
---|
674 | {
|
---|
675 | return ((binary / 10) << 4) + (binary % 10);
|
---|
676 | }
|
---|
677 |
|
---|
678 | /** Get the current uptime
|
---|
679 | *
|
---|
680 | * The time variables are memory mapped (read-only) from kernel which
|
---|
681 | * updates them periodically.
|
---|
682 | *
|
---|
683 | * As it is impossible to read 2 values atomically, we use a trick:
|
---|
684 | * First we read the seconds, then we read the microseconds, then we
|
---|
685 | * read the seconds again. If a second elapsed in the meantime, set
|
---|
686 | * the microseconds to zero.
|
---|
687 | *
|
---|
688 | * This assures that the values returned by two subsequent calls
|
---|
689 | * to gettimeofday() are monotonous.
|
---|
690 | *
|
---|
691 | */
|
---|
692 | static time_t
|
---|
693 | uptime_get(void)
|
---|
694 | {
|
---|
695 | if (kuptime == NULL) {
|
---|
696 | uintptr_t faddr;
|
---|
697 | int rc = sysinfo_get_value("clock.faddr", &faddr);
|
---|
698 | if (rc != EOK) {
|
---|
699 | errno = rc;
|
---|
700 | return -1;
|
---|
701 | }
|
---|
702 |
|
---|
703 | void *addr;
|
---|
704 | rc = physmem_map((void *) faddr, 1,
|
---|
705 | AS_AREA_READ | AS_AREA_CACHEABLE, &addr);
|
---|
706 | if (rc != EOK) {
|
---|
707 | as_area_destroy(addr);
|
---|
708 | errno = rc;
|
---|
709 | return -1;
|
---|
710 | }
|
---|
711 |
|
---|
712 | kuptime = addr;
|
---|
713 | }
|
---|
714 |
|
---|
715 | sysarg_t s2 = kuptime->seconds2;
|
---|
716 |
|
---|
717 | read_barrier();
|
---|
718 | sysarg_t s1 = kuptime->seconds1;
|
---|
719 |
|
---|
720 | return max(s1, s2);
|
---|
721 | }
|
---|
722 |
|
---|
723 | int
|
---|
724 | main(int argc, char **argv)
|
---|
725 | {
|
---|
726 | printf(NAME ": HelenOS RTC driver\n");
|
---|
727 | rtc_init();
|
---|
728 | return ddf_driver_main(&rtc_driver);
|
---|
729 | }
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * @}
|
---|
733 | */
|
---|