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