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

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

rtc: add support to the dev_remove driver operation

  • Property mode set to 100644
File size: 11.7 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 <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
59typedef 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
77static int rtc_time_get(ddf_fun_t *fun, struct tm *t);
78static int rtc_time_set(ddf_fun_t *fun, struct tm *t);
79static int rtc_dev_add(ddf_dev_t *dev);
80static int rtc_dev_initialize(rtc_t *rtc);
81static bool rtc_pio_enable(rtc_t *rtc);
82static void rtc_dev_cleanup(rtc_t *rtc);
83static int rtc_open(ddf_fun_t *fun);
84static void rtc_close(ddf_fun_t *fun);
85static bool rtc_update_in_progress(rtc_t *rtc);
86static int rtc_register_read(rtc_t *rtc, int reg);
87static int bcd2dec(int bcd);
88static void rtc_default_handler(ddf_fun_t *fun,
89 ipc_callid_t callid, ipc_call_t *call);
90static int rtc_dev_remove(ddf_dev_t *dev);
91
92
93static ddf_dev_ops_t rtc_dev_ops;
94
95/** The RTC device driver's standard operations */
96static driver_ops_t rtc_ops = {
97 .dev_add = rtc_dev_add,
98 .dev_remove = rtc_dev_remove,
99};
100
101/** The RTC device driver structure */
102static driver_t rtc_driver = {
103 .name = NAME,
104 .driver_ops = &rtc_ops,
105};
106
107/** Clock interface */
108static clock_dev_ops_t rtc_clock_dev_ops = {
109 .time_get = rtc_time_get,
110 .time_set = rtc_time_set,
111};
112
113/** Initialize the RTC driver */
114static void
115rtc_init(void)
116{
117 ddf_log_init(NAME, LVL_ERROR);
118
119 rtc_dev_ops.open = rtc_open;
120 rtc_dev_ops.close = rtc_close;
121
122 rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
123 rtc_dev_ops.default_handler = &rtc_default_handler;
124}
125
126/** Clean up the RTC soft state
127 *
128 * @param rtc The RTC device
129 */
130static void
131rtc_dev_cleanup(rtc_t *rtc)
132{
133 if (rtc->dev->parent_sess) {
134 async_hangup(rtc->dev->parent_sess);
135 rtc->dev->parent_sess = NULL;
136 }
137}
138
139/** Enable the I/O ports of the device
140 *
141 * @param rtc The real time clock device
142 *
143 * @return true in case of success, false otherwise
144 */
145static bool
146rtc_pio_enable(rtc_t *rtc)
147{
148 if (pio_enable((void *)(uintptr_t) rtc->io_addr, REG_COUNT,
149 (void **) &rtc->port)) {
150
151 ddf_msg(LVL_ERROR, "Cannot map the port %#" PRIx32
152 " for device %s", rtc->io_addr, rtc->dev->name);
153 return false;
154 }
155
156 return true;
157}
158
159/** Initialize the RTC device
160 *
161 * @param rtc Pointer to the RTC device
162 *
163 * @return EOK on success or a negative error code
164 */
165static int
166rtc_dev_initialize(rtc_t *rtc)
167{
168 int rc;
169 size_t i;
170 hw_resource_t *res;
171 bool ioport = false;
172
173 ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", rtc->dev->name);
174
175 hw_resource_list_t hw_resources;
176 memset(&hw_resources, 0, sizeof(hw_resource_list_t));
177
178 /* Connect to the parent's driver */
179
180 rtc->dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
181 rtc->dev->handle, IPC_FLAG_BLOCKING);
182 if (!rtc->dev->parent_sess) {
183 ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
184 of device %s.", rtc->dev->name);
185 rc = ENOENT;
186 goto error;
187 }
188
189 /* Get the HW resources */
190 rc = hw_res_get_resource_list(rtc->dev->parent_sess, &hw_resources);
191 if (rc != EOK) {
192 ddf_msg(LVL_ERROR, "Failed to get HW resources\
193 for device %s", rtc->dev->name);
194 goto error;
195 }
196
197 for (i = 0; i < hw_resources.count; ++i) {
198 res = &hw_resources.resources[i];
199
200 if (res->type == IO_RANGE) {
201 if (res->res.io_range.size < REG_COUNT) {
202 ddf_msg(LVL_ERROR, "I/O range assigned to \
203 device %s is too small", rtc->dev->name);
204 rc = ELIMIT;
205 goto error;
206 }
207 rtc->io_addr = res->res.io_range.address;
208 ioport = true;
209 ddf_msg(LVL_NOTE, "Device %s was assigned I/O address \
210 0x%x", rtc->dev->name, rtc->io_addr);
211 }
212 }
213
214 if (!ioport) {
215 /* No I/O address assigned to this device */
216 ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
217 rtc->dev->name);
218 rc = ENOENT;
219 goto error;
220 }
221
222 hw_res_clean_resource_list(&hw_resources);
223
224 return EOK;
225
226error:
227 rtc_dev_cleanup(rtc);
228 hw_res_clean_resource_list(&hw_resources);
229
230 return rc;
231}
232
233/** Read a register from the CMOS memory
234 *
235 * @param port The I/O port assigned to the device
236 * @param reg The index of the register to read
237 *
238 * @return The value of the register
239 */
240static int
241rtc_register_read(rtc_t *rtc, int reg)
242{
243 pio_write_8(rtc->port, reg);
244 return pio_read_8(rtc->port + 1);
245}
246
247/** Check if an update is in progress
248 *
249 * @param rtc The rtc device
250 *
251 * @return true if an update is in progress, false otherwise
252 */
253static bool
254rtc_update_in_progress(rtc_t *rtc)
255{
256 return rtc_register_read(rtc, RTC_UPDATE) & RTC_MASK_UPDATE;
257}
258
259/** Read the current time from the CMOS
260 *
261 * @param fun The RTC function
262 * @param t Pointer to the time variable
263 *
264 * @return EOK on success or a negative error code
265 */
266static int
267rtc_time_get(ddf_fun_t *fun, struct tm *t)
268{
269 bool bcd_mode;
270 rtc_t *rtc = RTC_FROM_FNODE(fun);
271
272 fibril_mutex_lock(&rtc->mutex);
273
274 /* now read the registers */
275 do {
276 /* Suspend until the update process has finished */
277 while (rtc_update_in_progress(rtc));
278
279 t->tm_sec = rtc_register_read(rtc, RTC_SEC);
280 t->tm_min = rtc_register_read(rtc, RTC_MIN);
281 t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
282 t->tm_mday = rtc_register_read(rtc, RTC_DAY);
283 t->tm_mon = rtc_register_read(rtc, RTC_MON);
284 t->tm_year = rtc_register_read(rtc, RTC_YEAR);
285
286 /* Now check if it is stable */
287 } while( t->tm_sec != rtc_register_read(rtc, RTC_SEC) ||
288 t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
289 t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
290 t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
291 t->tm_year != rtc_register_read(rtc, RTC_YEAR));
292
293 /* Check if the RTC is working in BCD mode */
294 bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_MASK_BCD);
295
296 if (bcd_mode) {
297 t->tm_sec = bcd2dec(t->tm_sec);
298 t->tm_min = bcd2dec(t->tm_min);
299 t->tm_hour = bcd2dec(t->tm_hour);
300 t->tm_mday = bcd2dec(t->tm_mday);
301 t->tm_mon = bcd2dec(t->tm_mon);
302 t->tm_year = bcd2dec(t->tm_year);
303 }
304
305 /* Count the months starting from 0, not from 1 */
306 t->tm_mon--;
307
308 if (t->tm_year < 100) {
309 /* tm_year is the number of years since 1900, it is not
310 * possible it is < 100.
311 */
312 t->tm_year += 100;
313 }
314
315 fibril_mutex_unlock(&rtc->mutex);
316 return EOK;
317}
318
319/** Set the time in the RTC
320 *
321 * @param fun The RTC function
322 * @param t The time value to set
323 *
324 * @return EOK or a negative error code
325 */
326static int
327rtc_time_set(ddf_fun_t *fun, struct tm *t)
328{
329 return EOK;
330}
331
332/** The dev_add callback of the rtc driver
333 *
334 * @param dev The RTC device
335 *
336 * @return EOK on success or a negative error code
337 */
338static int
339rtc_dev_add(ddf_dev_t *dev)
340{
341 rtc_t *rtc;
342 ddf_fun_t *fun = NULL;
343 int rc;
344 bool need_cleanup = false;
345
346 ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
347 dev->name, (int) dev->handle);
348
349 rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
350 if (!rtc)
351 return ENOMEM;
352
353 rtc->dev = dev;
354 fibril_mutex_initialize(&rtc->mutex);
355
356 rc = rtc_dev_initialize(rtc);
357 if (rc != EOK)
358 goto error;
359
360 need_cleanup = true;
361
362 if (!rtc_pio_enable(rtc)) {
363 rc = EADDRNOTAVAIL;
364 goto error;
365 }
366
367 fun = ddf_fun_create(dev, fun_exposed, "a");
368 if (!fun) {
369 ddf_msg(LVL_ERROR, "Failed creating function");
370 rc = ENOENT;
371 goto error;
372 }
373
374 fun->ops = &rtc_dev_ops;
375 rc = ddf_fun_bind(fun);
376 if (rc != EOK) {
377 ddf_msg(LVL_ERROR, "Failed binding function");
378 goto error;
379 }
380
381 rtc->fun = fun;
382
383 ddf_fun_add_to_category(fun, "clock");
384
385 rtc->client_connected = false;
386
387 ddf_msg(LVL_NOTE, "Device %s successfully initialized",
388 dev->name);
389
390 return rc;
391
392error:
393 if (fun)
394 ddf_fun_destroy(fun);
395 if (need_cleanup)
396 rtc_dev_cleanup(rtc);
397 return rc;
398}
399
400/** The dev_remove callback for the rtc driver
401 *
402 * @param dev The RTC device
403 *
404 * @return EOK on success or a negative error code
405 */
406static int
407rtc_dev_remove(ddf_dev_t *dev)
408{
409 rtc_t *rtc = RTC_FROM_DEV(dev);
410 int rc;
411
412 fibril_mutex_lock(&rtc->mutex);
413 if (rtc->client_connected) {
414 fibril_mutex_unlock(&rtc->mutex);
415 return EBUSY;
416 }
417
418 rtc->removed = true;
419 fibril_mutex_unlock(&rtc->mutex);
420
421 rc = ddf_fun_unbind(rtc->fun);
422 if (rc != EOK) {
423 ddf_msg(LVL_ERROR, "Failed to unbind function");
424 return rc;
425 }
426
427 ddf_fun_destroy(rtc->fun);
428 rtc_dev_cleanup(rtc);
429
430 return rc;
431}
432
433/** Default handler for client requests not handled
434 * by the standard interface
435 */
436static void
437rtc_default_handler(ddf_fun_t *fun, ipc_callid_t callid, ipc_call_t *call)
438{
439 sysarg_t method = IPC_GET_IMETHOD(*call);
440 rtc_t *rtc = RTC_FROM_FNODE(fun);
441 bool batt_ok;
442
443 switch (method) {
444 case CLOCK_GET_BATTERY_STATUS:
445 batt_ok = rtc_register_read(rtc, RTC_STATUS_D) &
446 RTC_BATTERY_OK;
447 async_answer_1(callid, EOK, batt_ok);
448 break;
449 default:
450 async_answer_0(callid, ENOTSUP);
451 }
452}
453
454/** Open the device
455 *
456 * @param fun The function node
457 *
458 * @return EOK on success or a negative error code
459 */
460static int
461rtc_open(ddf_fun_t *fun)
462{
463 int rc;
464 rtc_t *rtc = RTC_FROM_FNODE(fun);
465
466 fibril_mutex_lock(&rtc->mutex);
467
468 if (rtc->client_connected)
469 rc = ELIMIT;
470 else if (rtc->removed)
471 rc = ENXIO;
472 else {
473 rc = EOK;
474 rtc->client_connected = true;
475 }
476
477 fibril_mutex_unlock(&rtc->mutex);
478 return rc;
479}
480
481/** Close the device
482 *
483 * @param fun The function node
484 */
485static void
486rtc_close(ddf_fun_t *fun)
487{
488 rtc_t *rtc = RTC_FROM_FNODE(fun);
489
490 fibril_mutex_lock(&rtc->mutex);
491
492 assert(rtc->client_connected);
493 rtc->client_connected = false;
494
495 fibril_mutex_unlock(&rtc->mutex);
496}
497
498/** Convert from BCD mode to binary mode
499 *
500 * @param bcd The number in BCD format to convert
501 *
502 * @return The converted value
503 */
504static int
505bcd2dec(int bcd)
506{
507 return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
508}
509
510int
511main(int argc, char **argv)
512{
513 printf(NAME ": HelenOS RTC driver\n");
514 rtc_init();
515 return ddf_driver_main(&rtc_driver);
516}
517
518/**
519 * @}
520 */
Note: See TracBrowser for help on using the repository browser.