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