[1e19a15] | 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 | /**
|
---|
[4122410] | 30 | * @addtogroup cmos-rtc
|
---|
[1e19a15] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /** @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[4863bb52] | 37 | #include <errno.h>
|
---|
[1e19a15] | 38 | #include <ddi.h>
|
---|
[8fde078] | 39 | #include <as.h>
|
---|
[05882233] | 40 | #include <barrier.h>
|
---|
[1e19a15] | 41 | #include <stdio.h>
|
---|
| 42 | #include <ddf/driver.h>
|
---|
[a2e4889] | 43 | #include <ddf/log.h>
|
---|
[aeef318] | 44 | #include <ops/clock_dev.h>
|
---|
[917797f] | 45 | #include <ops/battery_dev.h>
|
---|
[6b329749] | 46 | #include <fibril_synch.h>
|
---|
[b3db669] | 47 | #include <device/hw_res.h>
|
---|
[8fde078] | 48 | #include <macros.h>
|
---|
[83298e8] | 49 | #include <time.h>
|
---|
[1e19a15] | 50 |
|
---|
[f30ee571] | 51 | #include "cmos-regs.h"
|
---|
| 52 |
|
---|
[5ef13847] | 53 | #define NAME "cmos-rtc"
|
---|
[1e19a15] | 54 |
|
---|
[0883de8] | 55 | #define REG_COUNT 2
|
---|
| 56 |
|
---|
[78ca12b] | 57 | #define REG_SEL_PORT(port) (port)
|
---|
| 58 | #define REG_RW_PORT(port) ((port) + 1)
|
---|
| 59 |
|
---|
[b3db669] | 60 | typedef struct rtc {
|
---|
| 61 | /** DDF device node */
|
---|
| 62 | ddf_dev_t *dev;
|
---|
| 63 | /** DDF function node */
|
---|
| 64 | ddf_fun_t *fun;
|
---|
| 65 | /** The fibril mutex for synchronizing the access to the device */
|
---|
| 66 | fibril_mutex_t mutex;
|
---|
[c47f1b6] | 67 | /** The base I/O address of the device registers */
|
---|
[971f50e7] | 68 | ioport8_t *io_addr;
|
---|
[0883de8] | 69 | /** The I/O port used to access the CMOS registers */
|
---|
| 70 | ioport8_t *port;
|
---|
[bb8f69d] | 71 | /** true if device is removed */
|
---|
| 72 | bool removed;
|
---|
[9673fd3] | 73 | /** number of connected clients */
|
---|
| 74 | int clients_connected;
|
---|
[60af6fc2] | 75 | /** time at which the system booted */
|
---|
[bd41ac52] | 76 | struct timespec boot_time;
|
---|
[b3db669] | 77 | } rtc_t;
|
---|
| 78 |
|
---|
[6f445a67] | 79 | static rtc_t *dev_rtc(ddf_dev_t *dev);
|
---|
| 80 | static rtc_t *fun_rtc(ddf_fun_t *fun);
|
---|
[b7fd2a0] | 81 | static errno_t
|
---|
[efcfab9] | 82 | rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status);
|
---|
[b7fd2a0] | 83 | static errno_t rtc_time_get(ddf_fun_t *fun, struct tm *t);
|
---|
| 84 | static errno_t rtc_time_set(ddf_fun_t *fun, struct tm *t);
|
---|
| 85 | static errno_t rtc_dev_add(ddf_dev_t *dev);
|
---|
| 86 | static errno_t rtc_dev_initialize(rtc_t *rtc);
|
---|
[2a5171db] | 87 | static bool rtc_pio_enable(rtc_t *rtc);
|
---|
| 88 | static void rtc_dev_cleanup(rtc_t *rtc);
|
---|
[b7fd2a0] | 89 | static errno_t rtc_open(ddf_fun_t *fun);
|
---|
[2a5171db] | 90 | static void rtc_close(ddf_fun_t *fun);
|
---|
[f30ee571] | 91 | static bool rtc_update_in_progress(rtc_t *rtc);
|
---|
[db8d552] | 92 | static int rtc_register_read(rtc_t *rtc, int reg);
|
---|
[1170ea3c] | 93 | static unsigned bcd2bin(unsigned bcd);
|
---|
| 94 | static unsigned bin2bcd(unsigned binary);
|
---|
[b7fd2a0] | 95 | static errno_t rtc_dev_remove(ddf_dev_t *dev);
|
---|
[a8a0d43] | 96 | static void rtc_register_write(rtc_t *rtc, int reg, int data);
|
---|
[0a586f4c] | 97 | static bool is_battery_ok(rtc_t *rtc);
|
---|
[b7fd2a0] | 98 | static errno_t rtc_fun_online(ddf_fun_t *fun);
|
---|
| 99 | static errno_t rtc_fun_offline(ddf_fun_t *fun);
|
---|
[4863bb52] | 100 |
|
---|
[1e19a15] | 101 | static ddf_dev_ops_t rtc_dev_ops;
|
---|
| 102 |
|
---|
[a31ca11f] | 103 | /** The RTC device driver's standard operations */
|
---|
| 104 | static driver_ops_t rtc_ops = {
|
---|
| 105 | .dev_add = rtc_dev_add,
|
---|
[bb8f69d] | 106 | .dev_remove = rtc_dev_remove,
|
---|
[b801f2d] | 107 | .fun_online = rtc_fun_online,
|
---|
| 108 | .fun_offline = rtc_fun_offline,
|
---|
[a31ca11f] | 109 | };
|
---|
| 110 |
|
---|
| 111 | /** The RTC device driver structure */
|
---|
[1e19a15] | 112 | static driver_t rtc_driver = {
|
---|
| 113 | .name = NAME,
|
---|
[a31ca11f] | 114 | .driver_ops = &rtc_ops,
|
---|
[1e19a15] | 115 | };
|
---|
| 116 |
|
---|
[a31ca11f] | 117 | /** Clock interface */
|
---|
[4863bb52] | 118 | static clock_dev_ops_t rtc_clock_dev_ops = {
|
---|
| 119 | .time_get = rtc_time_get,
|
---|
| 120 | .time_set = rtc_time_set,
|
---|
| 121 | };
|
---|
| 122 |
|
---|
[917797f] | 123 | /** Battery powered device interface */
|
---|
| 124 | static battery_dev_ops_t rtc_battery_dev_ops = {
|
---|
[efcfab9] | 125 | .battery_status_get = rtc_battery_status_get,
|
---|
[917797f] | 126 | .battery_charge_level_get = NULL,
|
---|
| 127 | };
|
---|
| 128 |
|
---|
[6f445a67] | 129 | /** Obtain soft state structure from device node */
|
---|
| 130 | static rtc_t *
|
---|
| 131 | dev_rtc(ddf_dev_t *dev)
|
---|
| 132 | {
|
---|
| 133 | return ddf_dev_data_get(dev);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /** Obtain soft state structure from function node */
|
---|
| 137 | static rtc_t *
|
---|
| 138 | fun_rtc(ddf_fun_t *fun)
|
---|
| 139 | {
|
---|
| 140 | return dev_rtc(ddf_fun_get_dev(fun));
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[4863bb52] | 143 | /** Initialize the RTC driver */
|
---|
[1e19a15] | 144 | static void
|
---|
| 145 | rtc_init(void)
|
---|
| 146 | {
|
---|
[289fa65] | 147 | ddf_log_init(NAME);
|
---|
[1e19a15] | 148 |
|
---|
[923b2eba] | 149 | rtc_dev_ops.open = rtc_open;
|
---|
[2a5171db] | 150 | rtc_dev_ops.close = rtc_close;
|
---|
[4863bb52] | 151 |
|
---|
| 152 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
---|
[917797f] | 153 | rtc_dev_ops.interfaces[BATTERY_DEV_IFACE] = &rtc_battery_dev_ops;
|
---|
| 154 | rtc_dev_ops.default_handler = NULL;
|
---|
[4863bb52] | 155 | }
|
---|
| 156 |
|
---|
[4b44de57] | 157 | /** Clean up the RTC soft state
|
---|
| 158 | *
|
---|
| 159 | * @param rtc The RTC device
|
---|
| 160 | */
|
---|
| 161 | static void
|
---|
| 162 | rtc_dev_cleanup(rtc_t *rtc)
|
---|
| 163 | {
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[0883de8] | 166 | /** Enable the I/O ports of the device
|
---|
| 167 | *
|
---|
| 168 | * @param rtc The real time clock device
|
---|
| 169 | *
|
---|
| 170 | * @return true in case of success, false otherwise
|
---|
| 171 | */
|
---|
| 172 | static bool
|
---|
| 173 | rtc_pio_enable(rtc_t *rtc)
|
---|
| 174 | {
|
---|
[85f7369] | 175 | if (pio_enable((void *) rtc->io_addr, REG_COUNT,
|
---|
[0883de8] | 176 | (void **) &rtc->port)) {
|
---|
| 177 |
|
---|
[276e44a] | 178 | ddf_msg(LVL_ERROR, "Cannot map the port %lx"
|
---|
[7ff35c7] | 179 | " for device %s", (long unsigned int)rtc->io_addr,
|
---|
[276e44a] | 180 | ddf_dev_get_name(rtc->dev));
|
---|
[0883de8] | 181 | return false;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | return true;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[b3db669] | 187 | /** Initialize the RTC device
|
---|
| 188 | *
|
---|
| 189 | * @param rtc Pointer to the RTC device
|
---|
| 190 | *
|
---|
[cde999a] | 191 | * @return EOK on success or an error code
|
---|
[1b20da0] | 192 | */
|
---|
[b7fd2a0] | 193 | static errno_t
|
---|
[b3db669] | 194 | rtc_dev_initialize(rtc_t *rtc)
|
---|
| 195 | {
|
---|
[b7fd2a0] | 196 | errno_t rc;
|
---|
[c47f1b6] | 197 | size_t i;
|
---|
| 198 | hw_resource_t *res;
|
---|
| 199 | bool ioport = false;
|
---|
[6f445a67] | 200 | async_sess_t *parent_sess;
|
---|
[b3db669] | 201 |
|
---|
[6f445a67] | 202 | ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev));
|
---|
[b3db669] | 203 |
|
---|
[7f9d97f3] | 204 | rtc->boot_time.tv_sec = 0;
|
---|
[bd41ac52] | 205 | rtc->boot_time.tv_nsec = 0;
|
---|
[9673fd3] | 206 | rtc->clients_connected = 0;
|
---|
[762083b] | 207 |
|
---|
[b3db669] | 208 | hw_resource_list_t hw_resources;
|
---|
| 209 | memset(&hw_resources, 0, sizeof(hw_resource_list_t));
|
---|
| 210 |
|
---|
| 211 | /* Connect to the parent's driver */
|
---|
| 212 |
|
---|
[2fd26bb] | 213 | parent_sess = ddf_dev_parent_sess_get(rtc->dev);
|
---|
| 214 | if (parent_sess == NULL) {
|
---|
[b3db669] | 215 | ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
|
---|
[6f445a67] | 216 | of device %s.", ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 217 | rc = ENOENT;
|
---|
| 218 | goto error;
|
---|
[b3db669] | 219 | }
|
---|
| 220 |
|
---|
| 221 | /* Get the HW resources */
|
---|
[6f445a67] | 222 | rc = hw_res_get_resource_list(parent_sess, &hw_resources);
|
---|
[b3db669] | 223 | if (rc != EOK) {
|
---|
| 224 | ddf_msg(LVL_ERROR, "Failed to get HW resources\
|
---|
[6f445a67] | 225 | for device %s", ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 226 | goto error;
|
---|
[b3db669] | 227 | }
|
---|
| 228 |
|
---|
[c47f1b6] | 229 | for (i = 0; i < hw_resources.count; ++i) {
|
---|
| 230 | res = &hw_resources.resources[i];
|
---|
| 231 |
|
---|
[ae827d0] | 232 | if (res->res.io_range.size < REG_COUNT) {
|
---|
| 233 | ddf_msg(LVL_ERROR, "I/O range assigned to \
|
---|
| 234 | device %s is too small",
|
---|
| 235 | ddf_dev_get_name(rtc->dev));
|
---|
| 236 | rc = ELIMIT;
|
---|
| 237 | continue;
|
---|
[c47f1b6] | 238 | }
|
---|
[ae827d0] | 239 |
|
---|
[7ff35c7] | 240 | rtc->io_addr = (ioport8_t *) (long) res->res.io_range.address;
|
---|
[ae827d0] | 241 | ioport = true;
|
---|
| 242 | ddf_msg(LVL_NOTE, "Device %s was assigned I/O address "
|
---|
[276e44a] | 243 | "0x%lx", ddf_dev_get_name(rtc->dev),
|
---|
[7ff35c7] | 244 | (unsigned long int) rtc->io_addr);
|
---|
[ae827d0] | 245 | rc = EOK;
|
---|
| 246 | break;
|
---|
[c47f1b6] | 247 | }
|
---|
| 248 |
|
---|
[ae827d0] | 249 | if (rc != EOK)
|
---|
| 250 | goto error;
|
---|
| 251 |
|
---|
[c47f1b6] | 252 | if (!ioport) {
|
---|
| 253 | /* No I/O address assigned to this device */
|
---|
| 254 | ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
|
---|
[6f445a67] | 255 | ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 256 | rc = ENOENT;
|
---|
| 257 | goto error;
|
---|
[c47f1b6] | 258 | }
|
---|
| 259 |
|
---|
| 260 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 261 |
|
---|
[b3db669] | 262 | return EOK;
|
---|
[4b44de57] | 263 |
|
---|
| 264 | error:
|
---|
| 265 | rtc_dev_cleanup(rtc);
|
---|
| 266 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 267 |
|
---|
| 268 | return rc;
|
---|
[b3db669] | 269 | }
|
---|
| 270 |
|
---|
[3b79ba5] | 271 | /** Read a register from the CMOS memory
|
---|
| 272 | *
|
---|
[1170ea3c] | 273 | * @param rtc The rtc device
|
---|
[3b79ba5] | 274 | * @param reg The index of the register to read
|
---|
| 275 | *
|
---|
| 276 | * @return The value of the register
|
---|
| 277 | */
|
---|
| 278 | static int
|
---|
[db8d552] | 279 | rtc_register_read(rtc_t *rtc, int reg)
|
---|
[3b79ba5] | 280 | {
|
---|
[78ca12b] | 281 | pio_write_8(REG_SEL_PORT(rtc->port), reg);
|
---|
| 282 | return pio_read_8(REG_RW_PORT(rtc->port));
|
---|
[3b79ba5] | 283 | }
|
---|
| 284 |
|
---|
[1170ea3c] | 285 | /** Write a register to the CMOS memory
|
---|
| 286 | *
|
---|
| 287 | * @param rtc The rtc device
|
---|
| 288 | * @param reg The index of the register to write
|
---|
| 289 | * @param data The data to write
|
---|
| 290 | */
|
---|
| 291 | static void
|
---|
| 292 | rtc_register_write(rtc_t *rtc, int reg, int data)
|
---|
| 293 | {
|
---|
[78ca12b] | 294 | pio_write_8(REG_SEL_PORT(rtc->port), reg);
|
---|
| 295 | pio_write_8(REG_RW_PORT(rtc->port), data);
|
---|
[1170ea3c] | 296 | }
|
---|
| 297 |
|
---|
[f30ee571] | 298 | /** Check if an update is in progress
|
---|
| 299 | *
|
---|
| 300 | * @param rtc The rtc device
|
---|
| 301 | *
|
---|
| 302 | * @return true if an update is in progress, false otherwise
|
---|
| 303 | */
|
---|
| 304 | static bool
|
---|
| 305 | rtc_update_in_progress(rtc_t *rtc)
|
---|
| 306 | {
|
---|
[6a3808e] | 307 | return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
|
---|
[f30ee571] | 308 | }
|
---|
| 309 |
|
---|
[0b8a3e7] | 310 | /** Read the current time from the CMOS
|
---|
| 311 | *
|
---|
| 312 | * @param fun The RTC function
|
---|
| 313 | * @param t Pointer to the time variable
|
---|
| 314 | *
|
---|
[cde999a] | 315 | * @return EOK on success or an error code
|
---|
[0b8a3e7] | 316 | */
|
---|
[b7fd2a0] | 317 | static errno_t
|
---|
[8d2963d] | 318 | rtc_time_get(ddf_fun_t *fun, struct tm *t)
|
---|
[4863bb52] | 319 | {
|
---|
[db8d552] | 320 | bool bcd_mode;
|
---|
[95060d5b] | 321 | bool pm_mode = false;
|
---|
[6f445a67] | 322 | rtc_t *rtc = fun_rtc(fun);
|
---|
[f30ee571] | 323 |
|
---|
[60af6fc2] | 324 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 325 |
|
---|
[7f9d97f3] | 326 | if (rtc->boot_time.tv_sec) {
|
---|
[7c3fb9b] | 327 | /*
|
---|
| 328 | * There is no need to read the current time from the
|
---|
[ea5cc5b] | 329 | * device because it has already been cached.
|
---|
| 330 | */
|
---|
| 331 |
|
---|
[bd41ac52] | 332 | struct timespec curtime;
|
---|
[a35b458] | 333 |
|
---|
[7f9d97f3] | 334 | getuptime(&curtime);
|
---|
[bd41ac52] | 335 | ts_add(&curtime, &rtc->boot_time);
|
---|
[60af6fc2] | 336 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 337 |
|
---|
[bd41ac52] | 338 | return time_ts2tm(&curtime, t);
|
---|
[ea5cc5b] | 339 | }
|
---|
| 340 |
|
---|
[0a586f4c] | 341 | /* Check if the RTC battery is OK */
|
---|
| 342 | if (!is_battery_ok(rtc)) {
|
---|
| 343 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 344 | return EIO;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[bd41ac52] | 347 | /* Nanoseconds are below RTC's resolution, assume 0. */
|
---|
| 348 | t->tm_nsec = 0;
|
---|
[7f9d97f3] | 349 |
|
---|
[db8d552] | 350 | /* now read the registers */
|
---|
| 351 | do {
|
---|
| 352 | /* Suspend until the update process has finished */
|
---|
[7f9d97f3] | 353 | while (rtc_update_in_progress(rtc))
|
---|
| 354 | ;
|
---|
[db8d552] | 355 |
|
---|
[7f9d97f3] | 356 | t->tm_sec = rtc_register_read(rtc, RTC_SEC);
|
---|
| 357 | t->tm_min = rtc_register_read(rtc, RTC_MIN);
|
---|
[db8d552] | 358 | t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
|
---|
| 359 | t->tm_mday = rtc_register_read(rtc, RTC_DAY);
|
---|
[7f9d97f3] | 360 | t->tm_mon = rtc_register_read(rtc, RTC_MON);
|
---|
[db8d552] | 361 | t->tm_year = rtc_register_read(rtc, RTC_YEAR);
|
---|
| 362 |
|
---|
| 363 | /* Now check if it is stable */
|
---|
[18b6a88] | 364 | } while (t->tm_sec != rtc_register_read(rtc, RTC_SEC) ||
|
---|
[7f9d97f3] | 365 | t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
|
---|
[c9abf50] | 366 | t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
|
---|
[7f9d97f3] | 367 | t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
|
---|
[c9abf50] | 368 | t->tm_year != rtc_register_read(rtc, RTC_YEAR));
|
---|
[db8d552] | 369 |
|
---|
[95060d5b] | 370 | /* Check if the RTC is working in 12h mode */
|
---|
| 371 | bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
|
---|
[6a3808e] | 372 | RTC_B_24H);
|
---|
[95060d5b] | 373 | if (_12h_mode) {
|
---|
| 374 | /* The RTC is working in 12h mode, check if it is AM or PM */
|
---|
| 375 | if (t->tm_hour & 0x80) {
|
---|
[f004318] | 376 | /* PM flag is active, it must be cleared */
|
---|
[95060d5b] | 377 | t->tm_hour &= ~0x80;
|
---|
| 378 | pm_mode = true;
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[db8d552] | 382 | /* Check if the RTC is working in BCD mode */
|
---|
[6a3808e] | 383 | bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
|
---|
[fc7d28e] | 384 | if (bcd_mode) {
|
---|
[7f9d97f3] | 385 | t->tm_sec = bcd2bin(t->tm_sec);
|
---|
| 386 | t->tm_min = bcd2bin(t->tm_min);
|
---|
[1170ea3c] | 387 | t->tm_hour = bcd2bin(t->tm_hour);
|
---|
| 388 | t->tm_mday = bcd2bin(t->tm_mday);
|
---|
[7f9d97f3] | 389 | t->tm_mon = bcd2bin(t->tm_mon);
|
---|
[1170ea3c] | 390 | t->tm_year = bcd2bin(t->tm_year);
|
---|
[db8d552] | 391 | }
|
---|
[f30ee571] | 392 |
|
---|
[95060d5b] | 393 | if (_12h_mode) {
|
---|
| 394 | /* Convert to 24h mode */
|
---|
| 395 | if (pm_mode) {
|
---|
| 396 | if (t->tm_hour < 12)
|
---|
| 397 | t->tm_hour += 12;
|
---|
| 398 | } else if (t->tm_hour == 12)
|
---|
| 399 | t->tm_hour = 0;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[05ed9d7] | 402 | /* Count the months starting from 0, not from 1 */
|
---|
| 403 | t->tm_mon--;
|
---|
| 404 |
|
---|
[fc7d28e] | 405 | if (t->tm_year < 100) {
|
---|
[7c3fb9b] | 406 | /*
|
---|
| 407 | * tm_year is the number of years since 1900 but the
|
---|
[e5fbe06] | 408 | * RTC epoch is 2000.
|
---|
[fc7d28e] | 409 | */
|
---|
| 410 | t->tm_year += 100;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
[af7e3d3] | 413 | /* Try to normalize the content of the tm structure */
|
---|
[fa18523] | 414 | time_t r = mktime(t);
|
---|
[b7fd2a0] | 415 | errno_t result;
|
---|
[fa18523] | 416 |
|
---|
[508fff8] | 417 | if (r < 0)
|
---|
| 418 | result = EINVAL;
|
---|
| 419 | else {
|
---|
[bd41ac52] | 420 | struct timespec uptime;
|
---|
[7f9d97f3] | 421 |
|
---|
| 422 | getuptime(&uptime);
|
---|
| 423 | rtc->boot_time.tv_sec = r;
|
---|
[bd41ac52] | 424 | rtc->boot_time.tv_nsec = t->tm_nsec; /* normalized */
|
---|
| 425 | ts_sub(&rtc->boot_time, &uptime);
|
---|
[508fff8] | 426 | result = EOK;
|
---|
| 427 | }
|
---|
[60af6fc2] | 428 |
|
---|
| 429 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[ea5cc5b] | 430 |
|
---|
[508fff8] | 431 | return result;
|
---|
[4863bb52] | 432 | }
|
---|
| 433 |
|
---|
[0b8a3e7] | 434 | /** Set the time in the RTC
|
---|
| 435 | *
|
---|
| 436 | * @param fun The RTC function
|
---|
| 437 | * @param t The time value to set
|
---|
| 438 | *
|
---|
[cde999a] | 439 | * @return EOK or an error code
|
---|
[0b8a3e7] | 440 | */
|
---|
[b7fd2a0] | 441 | static errno_t
|
---|
[8d2963d] | 442 | rtc_time_set(ddf_fun_t *fun, struct tm *t)
|
---|
[4863bb52] | 443 | {
|
---|
[1170ea3c] | 444 | bool bcd_mode;
|
---|
[8fde078] | 445 | time_t norm_time;
|
---|
[bd41ac52] | 446 | struct timespec uptime;
|
---|
| 447 | struct timespec ntv;
|
---|
[a8a0d43] | 448 | int reg_b;
|
---|
| 449 | int reg_a;
|
---|
[e5fbe06] | 450 | int epoch;
|
---|
[6f445a67] | 451 | rtc_t *rtc = fun_rtc(fun);
|
---|
[1170ea3c] | 452 |
|
---|
[af7e3d3] | 453 | /* Try to normalize the content of the tm structure */
|
---|
[8fde078] | 454 | if ((norm_time = mktime(t)) < 0)
|
---|
[fa18523] | 455 | return EINVAL;
|
---|
| 456 |
|
---|
[7f9d97f3] | 457 | ntv.tv_sec = norm_time;
|
---|
[bd41ac52] | 458 | ntv.tv_nsec = t->tm_nsec;
|
---|
[7f9d97f3] | 459 | getuptime(&uptime);
|
---|
| 460 |
|
---|
[bd41ac52] | 461 | if (ts_gteq(&uptime, &ntv)) {
|
---|
[8fde078] | 462 | /* This is not acceptable */
|
---|
| 463 | return EINVAL;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
[e5fbe06] | 466 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 467 |
|
---|
[0a586f4c] | 468 | if (!is_battery_ok(rtc)) {
|
---|
| 469 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 470 | return EIO;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[7f9d97f3] | 473 | /* boot_time must be recomputed */
|
---|
| 474 | rtc->boot_time.tv_sec = 0;
|
---|
[bd41ac52] | 475 | rtc->boot_time.tv_nsec = 0;
|
---|
[60af6fc2] | 476 |
|
---|
[e5fbe06] | 477 | /* Detect the RTC epoch */
|
---|
| 478 | if (rtc_register_read(rtc, RTC_YEAR) < 100)
|
---|
| 479 | epoch = 2000;
|
---|
| 480 | else
|
---|
| 481 | epoch = 1900;
|
---|
| 482 |
|
---|
[fa18523] | 483 | if (epoch == 2000 && t->tm_year < 100) {
|
---|
| 484 | /* Can't set a year before the epoch */
|
---|
[e5fbe06] | 485 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[fa18523] | 486 | return EINVAL;
|
---|
[e5fbe06] | 487 | }
|
---|
[1170ea3c] | 488 |
|
---|
[a8a0d43] | 489 | t->tm_mon++; /* counts from 1, not from 0 */
|
---|
[1170ea3c] | 490 |
|
---|
[a8a0d43] | 491 | reg_b = rtc_register_read(rtc, RTC_STATUS_B);
|
---|
| 492 |
|
---|
[6a3808e] | 493 | if (!(reg_b & RTC_B_24H)) {
|
---|
[f004318] | 494 | /* Force 24h mode of operation */
|
---|
[6a3808e] | 495 | reg_b |= RTC_B_24H;
|
---|
[f004318] | 496 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
| 497 | }
|
---|
[f6af126] | 498 |
|
---|
[a44b58c] | 499 | if (epoch == 2000) {
|
---|
[7c3fb9b] | 500 | /*
|
---|
| 501 | * The RTC epoch is year 2000 but the tm_year
|
---|
[a44b58c] | 502 | * field counts years since 1900.
|
---|
| 503 | */
|
---|
[074324f1] | 504 | t->tm_year -= 100;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[1170ea3c] | 507 | /* Check if the rtc is working in bcd mode */
|
---|
[6a3808e] | 508 | bcd_mode = !(reg_b & RTC_B_BCD);
|
---|
[1170ea3c] | 509 | if (bcd_mode) {
|
---|
| 510 | /* Convert the tm struct fields in BCD mode */
|
---|
| 511 | t->tm_sec = bin2bcd(t->tm_sec);
|
---|
| 512 | t->tm_min = bin2bcd(t->tm_min);
|
---|
| 513 | t->tm_hour = bin2bcd(t->tm_hour);
|
---|
| 514 | t->tm_mday = bin2bcd(t->tm_mday);
|
---|
[709476f4] | 515 | t->tm_mon = bin2bcd(t->tm_mon);
|
---|
[1170ea3c] | 516 | t->tm_year = bin2bcd(t->tm_year);
|
---|
| 517 | }
|
---|
| 518 |
|
---|
[a8a0d43] | 519 | /* Inhibit updates */
|
---|
[6a3808e] | 520 | rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
|
---|
[a8a0d43] | 521 |
|
---|
| 522 | /* Write current time to RTC */
|
---|
| 523 | rtc_register_write(rtc, RTC_SEC, t->tm_sec);
|
---|
| 524 | rtc_register_write(rtc, RTC_MIN, t->tm_min);
|
---|
| 525 | rtc_register_write(rtc, RTC_HOUR, t->tm_hour);
|
---|
| 526 | rtc_register_write(rtc, RTC_DAY, t->tm_mday);
|
---|
| 527 | rtc_register_write(rtc, RTC_MON, t->tm_mon);
|
---|
| 528 | rtc_register_write(rtc, RTC_YEAR, t->tm_year);
|
---|
| 529 |
|
---|
| 530 | /* Stop the clock */
|
---|
| 531 | reg_a = rtc_register_read(rtc, RTC_STATUS_A);
|
---|
[6a3808e] | 532 | rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
|
---|
[a35b458] | 533 |
|
---|
[a8a0d43] | 534 | /* Enable updates */
|
---|
| 535 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
| 536 | rtc_register_write(rtc, RTC_STATUS_A, reg_a);
|
---|
[1170ea3c] | 537 |
|
---|
| 538 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 539 |
|
---|
[4863bb52] | 540 | return EOK;
|
---|
[1e19a15] | 541 | }
|
---|
| 542 |
|
---|
[efcfab9] | 543 | /** Get the status of the real time clock battery
|
---|
| 544 | *
|
---|
| 545 | * @param fun The RTC function
|
---|
| 546 | * @param status The status of the battery
|
---|
| 547 | *
|
---|
[cde999a] | 548 | * @return EOK on success or an error code
|
---|
[efcfab9] | 549 | */
|
---|
[b7fd2a0] | 550 | static errno_t
|
---|
[efcfab9] | 551 | rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status)
|
---|
| 552 | {
|
---|
| 553 | rtc_t *rtc = fun_rtc(fun);
|
---|
[0a586f4c] | 554 |
|
---|
| 555 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 556 | const bool batt_ok = is_battery_ok(rtc);
|
---|
| 557 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[efcfab9] | 558 |
|
---|
| 559 | *status = batt_ok ? BATTERY_OK : BATTERY_LOW;
|
---|
| 560 |
|
---|
| 561 | return EOK;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
[0a586f4c] | 564 | /** Check if the battery is working properly or not.
|
---|
| 565 | * The caller already holds the rtc->mutex lock.
|
---|
| 566 | *
|
---|
| 567 | * @param rtc The RTC instance.
|
---|
| 568 | *
|
---|
| 569 | * @return true if the battery is ok, false otherwise.
|
---|
| 570 | */
|
---|
| 571 | static bool
|
---|
| 572 | is_battery_ok(rtc_t *rtc)
|
---|
| 573 | {
|
---|
| 574 | return rtc_register_read(rtc, RTC_STATUS_D) & RTC_D_BATTERY_OK;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
[a31ca11f] | 577 | /** The dev_add callback of the rtc driver
|
---|
| 578 | *
|
---|
| 579 | * @param dev The RTC device
|
---|
| 580 | *
|
---|
[cde999a] | 581 | * @return EOK on success or an error code
|
---|
[a31ca11f] | 582 | */
|
---|
[b7fd2a0] | 583 | static errno_t
|
---|
[a31ca11f] | 584 | rtc_dev_add(ddf_dev_t *dev)
|
---|
| 585 | {
|
---|
[6b329749] | 586 | rtc_t *rtc;
|
---|
[4b44de57] | 587 | ddf_fun_t *fun = NULL;
|
---|
[b7fd2a0] | 588 | errno_t rc;
|
---|
[4b44de57] | 589 | bool need_cleanup = false;
|
---|
[4f87a85a] | 590 | bool bound = false;
|
---|
[6b329749] | 591 |
|
---|
| 592 | ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
|
---|
[6f445a67] | 593 | ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
|
---|
[6b329749] | 594 |
|
---|
| 595 | rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
|
---|
| 596 | if (!rtc)
|
---|
| 597 | return ENOMEM;
|
---|
| 598 |
|
---|
| 599 | rtc->dev = dev;
|
---|
| 600 | fibril_mutex_initialize(&rtc->mutex);
|
---|
| 601 |
|
---|
[b3db669] | 602 | rc = rtc_dev_initialize(rtc);
|
---|
| 603 | if (rc != EOK)
|
---|
[4b44de57] | 604 | goto error;
|
---|
[b3db669] | 605 |
|
---|
[4b44de57] | 606 | need_cleanup = true;
|
---|
| 607 |
|
---|
| 608 | if (!rtc_pio_enable(rtc)) {
|
---|
| 609 | rc = EADDRNOTAVAIL;
|
---|
| 610 | goto error;
|
---|
| 611 | }
|
---|
[0883de8] | 612 |
|
---|
| 613 | fun = ddf_fun_create(dev, fun_exposed, "a");
|
---|
| 614 | if (!fun) {
|
---|
| 615 | ddf_msg(LVL_ERROR, "Failed creating function");
|
---|
[4b44de57] | 616 | rc = ENOENT;
|
---|
| 617 | goto error;
|
---|
[0883de8] | 618 | }
|
---|
| 619 |
|
---|
[6f445a67] | 620 | ddf_fun_set_ops(fun, &rtc_dev_ops);
|
---|
[0883de8] | 621 | rc = ddf_fun_bind(fun);
|
---|
| 622 | if (rc != EOK) {
|
---|
| 623 | ddf_msg(LVL_ERROR, "Failed binding function");
|
---|
[4b44de57] | 624 | goto error;
|
---|
[0883de8] | 625 | }
|
---|
| 626 |
|
---|
[4f87a85a] | 627 | bound = true;
|
---|
[0883de8] | 628 | rtc->fun = fun;
|
---|
| 629 |
|
---|
[4f87a85a] | 630 | rc = ddf_fun_add_to_category(fun, "clock");
|
---|
| 631 | if (rc != EOK) {
|
---|
| 632 | ddf_msg(LVL_ERROR, "Failed adding service to clock category.");
|
---|
| 633 | goto error;
|
---|
| 634 | }
|
---|
[0883de8] | 635 |
|
---|
| 636 | ddf_msg(LVL_NOTE, "Device %s successfully initialized",
|
---|
[6f445a67] | 637 | ddf_dev_get_name(dev));
|
---|
[0883de8] | 638 |
|
---|
[b3db669] | 639 | return rc;
|
---|
[4b44de57] | 640 |
|
---|
| 641 | error:
|
---|
[4f87a85a] | 642 | if (bound)
|
---|
| 643 | ddf_fun_unbind(fun);
|
---|
[4b44de57] | 644 | if (fun)
|
---|
| 645 | ddf_fun_destroy(fun);
|
---|
| 646 | if (need_cleanup)
|
---|
| 647 | rtc_dev_cleanup(rtc);
|
---|
| 648 | return rc;
|
---|
[a31ca11f] | 649 | }
|
---|
| 650 |
|
---|
[bb8f69d] | 651 | /** The dev_remove callback for the rtc driver
|
---|
| 652 | *
|
---|
| 653 | * @param dev The RTC device
|
---|
| 654 | *
|
---|
[cde999a] | 655 | * @return EOK on success or an error code
|
---|
[bb8f69d] | 656 | */
|
---|
[b7fd2a0] | 657 | static errno_t
|
---|
[bb8f69d] | 658 | rtc_dev_remove(ddf_dev_t *dev)
|
---|
| 659 | {
|
---|
[6f445a67] | 660 | rtc_t *rtc = dev_rtc(dev);
|
---|
[b7fd2a0] | 661 | errno_t rc;
|
---|
[bb8f69d] | 662 |
|
---|
| 663 | fibril_mutex_lock(&rtc->mutex);
|
---|
[9673fd3] | 664 | if (rtc->clients_connected > 0) {
|
---|
| 665 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 666 | return EBUSY;
|
---|
| 667 | }
|
---|
[bb8f69d] | 668 |
|
---|
| 669 | rtc->removed = true;
|
---|
| 670 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 671 |
|
---|
[b801f2d] | 672 | rc = rtc_fun_offline(rtc->fun);
|
---|
| 673 | if (rc != EOK) {
|
---|
| 674 | ddf_msg(LVL_ERROR, "Failed to offline function");
|
---|
| 675 | return rc;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
[bb8f69d] | 678 | rc = ddf_fun_unbind(rtc->fun);
|
---|
| 679 | if (rc != EOK) {
|
---|
| 680 | ddf_msg(LVL_ERROR, "Failed to unbind function");
|
---|
| 681 | return rc;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | ddf_fun_destroy(rtc->fun);
|
---|
| 685 | rtc_dev_cleanup(rtc);
|
---|
| 686 |
|
---|
| 687 | return rc;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
[923b2eba] | 690 | /** Open the device
|
---|
| 691 | *
|
---|
| 692 | * @param fun The function node
|
---|
| 693 | *
|
---|
[cde999a] | 694 | * @return EOK on success or an error code
|
---|
[923b2eba] | 695 | */
|
---|
[b7fd2a0] | 696 | static errno_t
|
---|
[923b2eba] | 697 | rtc_open(ddf_fun_t *fun)
|
---|
| 698 | {
|
---|
[b7fd2a0] | 699 | errno_t rc;
|
---|
[6f445a67] | 700 | rtc_t *rtc = fun_rtc(fun);
|
---|
[923b2eba] | 701 |
|
---|
| 702 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 703 |
|
---|
[43e660c] | 704 | if (rtc->removed)
|
---|
[bb8f69d] | 705 | rc = ENXIO;
|
---|
[9673fd3] | 706 | else {
|
---|
[923b2eba] | 707 | rc = EOK;
|
---|
[9673fd3] | 708 | rtc->clients_connected++;
|
---|
| 709 | }
|
---|
[923b2eba] | 710 |
|
---|
| 711 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 712 | return rc;
|
---|
| 713 | }
|
---|
| 714 |
|
---|
[2a5171db] | 715 | /** Close the device
|
---|
| 716 | *
|
---|
| 717 | * @param fun The function node
|
---|
| 718 | */
|
---|
| 719 | static void
|
---|
| 720 | rtc_close(ddf_fun_t *fun)
|
---|
| 721 | {
|
---|
[9673fd3] | 722 | rtc_t *rtc = fun_rtc(fun);
|
---|
| 723 |
|
---|
| 724 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 725 |
|
---|
| 726 | rtc->clients_connected--;
|
---|
| 727 | assert(rtc->clients_connected >= 0);
|
---|
| 728 |
|
---|
| 729 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[2a5171db] | 730 | }
|
---|
| 731 |
|
---|
[db8d552] | 732 | /** Convert from BCD mode to binary mode
|
---|
| 733 | *
|
---|
| 734 | * @param bcd The number in BCD format to convert
|
---|
| 735 | *
|
---|
| 736 | * @return The converted value
|
---|
| 737 | */
|
---|
[1b20da0] | 738 | static unsigned
|
---|
[1170ea3c] | 739 | bcd2bin(unsigned bcd)
|
---|
[db8d552] | 740 | {
|
---|
| 741 | return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
|
---|
| 742 | }
|
---|
| 743 |
|
---|
[1170ea3c] | 744 | /** Convert from binary mode to BCD mode
|
---|
| 745 | *
|
---|
| 746 | * @param bcd The number in binary mode to convert
|
---|
| 747 | *
|
---|
| 748 | * @return The converted value
|
---|
| 749 | */
|
---|
| 750 | static unsigned
|
---|
| 751 | bin2bcd(unsigned binary)
|
---|
| 752 | {
|
---|
| 753 | return ((binary / 10) << 4) + (binary % 10);
|
---|
| 754 | }
|
---|
| 755 |
|
---|
[b7fd2a0] | 756 | static errno_t
|
---|
[b801f2d] | 757 | rtc_fun_online(ddf_fun_t *fun)
|
---|
| 758 | {
|
---|
[b7fd2a0] | 759 | errno_t rc;
|
---|
[a582dff] | 760 |
|
---|
[b801f2d] | 761 | ddf_msg(LVL_DEBUG, "rtc_fun_online()");
|
---|
[a582dff] | 762 |
|
---|
| 763 | rc = ddf_fun_online(fun);
|
---|
[4f87a85a] | 764 | if (rc == EOK) {
|
---|
| 765 | // XXX This should be probably handled by the framework
|
---|
| 766 | rc = ddf_fun_add_to_category(fun, "clock");
|
---|
| 767 | }
|
---|
[a582dff] | 768 |
|
---|
| 769 | return rc;
|
---|
[b801f2d] | 770 | }
|
---|
| 771 |
|
---|
[b7fd2a0] | 772 | static errno_t
|
---|
[b801f2d] | 773 | rtc_fun_offline(ddf_fun_t *fun)
|
---|
| 774 | {
|
---|
| 775 | ddf_msg(LVL_DEBUG, "rtc_fun_offline()");
|
---|
| 776 | return ddf_fun_offline(fun);
|
---|
| 777 | }
|
---|
| 778 |
|
---|
[1e19a15] | 779 | int
|
---|
| 780 | main(int argc, char **argv)
|
---|
| 781 | {
|
---|
| 782 | printf(NAME ": HelenOS RTC driver\n");
|
---|
| 783 | rtc_init();
|
---|
| 784 | return ddf_driver_main(&rtc_driver);
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | /**
|
---|
| 788 | * @}
|
---|
| 789 | */
|
---|