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

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

rtc: Add an option to the default handler to get the status of the clock's battery

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