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