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

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

rtc: Fix cmos battery status reading

  • Property mode set to 100644
File size: 10.9 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 t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
285 t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
286 t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
287 t->tm_year != rtc_register_read(rtc, RTC_YEAR));
288
289 /* Check if the RTC is working in BCD mode */
290 bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_MASK_BCD);
291
292 if (bcd_mode) {
293 t->tm_sec = bcd2dec(t->tm_sec);
294 t->tm_min = bcd2dec(t->tm_min);
295 t->tm_hour = bcd2dec(t->tm_hour);
296 t->tm_mday = bcd2dec(t->tm_mday);
297 t->tm_mon = bcd2dec(t->tm_mon);
298 t->tm_year = bcd2dec(t->tm_year);
299 }
300
301 /* Count the months starting from 0, not from 1 */
302 t->tm_mon--;
303
304 if (t->tm_year < 100) {
305 /* tm_year is the number of years since 1900, it is not
306 * possible it is < 100.
307 */
308 t->tm_year += 100;
309 }
310
311 fibril_mutex_unlock(&rtc->mutex);
312 return EOK;
313}
314
315/** Set the time in the RTC
316 *
317 * @param fun The RTC function
318 * @param t The time value to set
319 *
320 * @return EOK or a negative error code
321 */
322static int
323rtc_time_set(ddf_fun_t *fun, struct tm *t)
324{
325 return EOK;
326}
327
328/** The dev_add callback of the rtc driver
329 *
330 * @param dev The RTC device
331 *
332 * @return EOK on success or a negative error code
333 */
334static int
335rtc_dev_add(ddf_dev_t *dev)
336{
337 rtc_t *rtc;
338 ddf_fun_t *fun = NULL;
339 int rc;
340 bool need_cleanup = false;
341
342 ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
343 dev->name, (int) dev->handle);
344
345 rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
346 if (!rtc)
347 return ENOMEM;
348
349 rtc->dev = dev;
350 fibril_mutex_initialize(&rtc->mutex);
351
352 rc = rtc_dev_initialize(rtc);
353 if (rc != EOK)
354 goto error;
355
356 need_cleanup = true;
357
358 if (!rtc_pio_enable(rtc)) {
359 rc = EADDRNOTAVAIL;
360 goto error;
361 }
362
363 fun = ddf_fun_create(dev, fun_exposed, "a");
364 if (!fun) {
365 ddf_msg(LVL_ERROR, "Failed creating function");
366 rc = ENOENT;
367 goto error;
368 }
369
370 fun->ops = &rtc_dev_ops;
371 rc = ddf_fun_bind(fun);
372 if (rc != EOK) {
373 ddf_msg(LVL_ERROR, "Failed binding function");
374 goto error;
375 }
376
377 rtc->fun = fun;
378
379 ddf_fun_add_to_category(fun, "clock");
380
381 rtc->client_connected = false;
382
383 ddf_msg(LVL_NOTE, "Device %s successfully initialized",
384 dev->name);
385
386 return rc;
387
388error:
389 if (fun)
390 ddf_fun_destroy(fun);
391 if (need_cleanup)
392 rtc_dev_cleanup(rtc);
393 return rc;
394}
395
396/** Default handler for client requests not handled
397 * by the standard interface
398 */
399static void
400rtc_default_handler(ddf_fun_t *fun, ipc_callid_t callid, ipc_call_t *call)
401{
402 sysarg_t method = IPC_GET_IMETHOD(*call);
403 rtc_t *rtc = RTC_FROM_FNODE(fun);
404 bool batt_ok;
405
406 switch (method) {
407 case CLOCK_GET_BATTERY_STATUS:
408 batt_ok = rtc_register_read(rtc, RTC_STATUS_D) &
409 RTC_BATTERY_OK;
410 async_answer_1(callid, EOK, batt_ok);
411 break;
412 default:
413 async_answer_0(callid, ENOTSUP);
414 }
415}
416
417/** Open the device
418 *
419 * @param fun The function node
420 *
421 * @return EOK on success or a negative error code
422 */
423static int
424rtc_open(ddf_fun_t *fun)
425{
426 int rc;
427 rtc_t *rtc = RTC_FROM_FNODE(fun);
428
429 fibril_mutex_lock(&rtc->mutex);
430
431 if (rtc->client_connected)
432 rc = EBUSY;
433 else {
434 rc = EOK;
435 rtc->client_connected = true;
436 }
437
438 fibril_mutex_unlock(&rtc->mutex);
439 return rc;
440}
441
442/** Close the device
443 *
444 * @param fun The function node
445 */
446static void
447rtc_close(ddf_fun_t *fun)
448{
449 rtc_t *rtc = RTC_FROM_FNODE(fun);
450
451 fibril_mutex_lock(&rtc->mutex);
452
453 assert(rtc->client_connected);
454 rtc->client_connected = false;
455
456 fibril_mutex_unlock(&rtc->mutex);
457}
458
459/** Convert from BCD mode to binary mode
460 *
461 * @param bcd The number in BCD format to convert
462 *
463 * @return The converted value
464 */
465static int
466bcd2dec(int bcd)
467{
468 return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
469}
470
471int
472main(int argc, char **argv)
473{
474 printf(NAME ": HelenOS RTC driver\n");
475 rtc_init();
476 return ddf_driver_main(&rtc_driver);
477}
478
479/**
480 * @}
481 */
Note: See TracBrowser for help on using the repository browser.