| 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 | time_t boottime;
|
|---|
| 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 time_t uptime_get(void);
|
|---|
| 100 | static bool is_battery_ok(rtc_t *rtc);
|
|---|
| 101 | static int rtc_fun_online(ddf_fun_t *fun);
|
|---|
| 102 | static int rtc_fun_offline(ddf_fun_t *fun);
|
|---|
| 103 |
|
|---|
| 104 | static ddf_dev_ops_t rtc_dev_ops;
|
|---|
| 105 |
|
|---|
| 106 | /** The RTC device driver's standard operations */
|
|---|
| 107 | static driver_ops_t rtc_ops = {
|
|---|
| 108 | .dev_add = rtc_dev_add,
|
|---|
| 109 | .dev_remove = rtc_dev_remove,
|
|---|
| 110 | .fun_online = rtc_fun_online,
|
|---|
| 111 | .fun_offline = rtc_fun_offline,
|
|---|
| 112 | };
|
|---|
| 113 |
|
|---|
| 114 | /** The RTC device driver structure */
|
|---|
| 115 | static driver_t rtc_driver = {
|
|---|
| 116 | .name = NAME,
|
|---|
| 117 | .driver_ops = &rtc_ops,
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 | /** Clock interface */
|
|---|
| 121 | static clock_dev_ops_t rtc_clock_dev_ops = {
|
|---|
| 122 | .time_get = rtc_time_get,
|
|---|
| 123 | .time_set = rtc_time_set,
|
|---|
| 124 | };
|
|---|
| 125 |
|
|---|
| 126 | /** Battery powered device interface */
|
|---|
| 127 | static battery_dev_ops_t rtc_battery_dev_ops = {
|
|---|
| 128 | .battery_status_get = rtc_battery_status_get,
|
|---|
| 129 | .battery_charge_level_get = NULL,
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | /** Obtain soft state structure from device node */
|
|---|
| 133 | static rtc_t *
|
|---|
| 134 | dev_rtc(ddf_dev_t *dev)
|
|---|
| 135 | {
|
|---|
| 136 | return ddf_dev_data_get(dev);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /** Obtain soft state structure from function node */
|
|---|
| 140 | static rtc_t *
|
|---|
| 141 | fun_rtc(ddf_fun_t *fun)
|
|---|
| 142 | {
|
|---|
| 143 | return dev_rtc(ddf_fun_get_dev(fun));
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /** Initialize the RTC driver */
|
|---|
| 147 | static void
|
|---|
| 148 | rtc_init(void)
|
|---|
| 149 | {
|
|---|
| 150 | ddf_log_init(NAME);
|
|---|
| 151 |
|
|---|
| 152 | rtc_dev_ops.open = rtc_open;
|
|---|
| 153 | rtc_dev_ops.close = rtc_close;
|
|---|
| 154 |
|
|---|
| 155 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
|---|
| 156 | rtc_dev_ops.interfaces[BATTERY_DEV_IFACE] = &rtc_battery_dev_ops;
|
|---|
| 157 | rtc_dev_ops.default_handler = NULL;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Clean up the RTC soft state
|
|---|
| 161 | *
|
|---|
| 162 | * @param rtc The RTC device
|
|---|
| 163 | */
|
|---|
| 164 | static void
|
|---|
| 165 | rtc_dev_cleanup(rtc_t *rtc)
|
|---|
| 166 | {
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /** Enable the I/O ports of the device
|
|---|
| 170 | *
|
|---|
| 171 | * @param rtc The real time clock device
|
|---|
| 172 | *
|
|---|
| 173 | * @return true in case of success, false otherwise
|
|---|
| 174 | */
|
|---|
| 175 | static bool
|
|---|
| 176 | rtc_pio_enable(rtc_t *rtc)
|
|---|
| 177 | {
|
|---|
| 178 | if (pio_enable((void *) rtc->io_addr, REG_COUNT,
|
|---|
| 179 | (void **) &rtc->port)) {
|
|---|
| 180 |
|
|---|
| 181 | ddf_msg(LVL_ERROR, "Cannot map the port %lx"
|
|---|
| 182 | " for device %s", (long unsigned int)rtc->io_addr,
|
|---|
| 183 | ddf_dev_get_name(rtc->dev));
|
|---|
| 184 | return false;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | return true;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /** Initialize the RTC device
|
|---|
| 191 | *
|
|---|
| 192 | * @param rtc Pointer to the RTC device
|
|---|
| 193 | *
|
|---|
| 194 | * @return EOK on success or a negative error code
|
|---|
| 195 | */
|
|---|
| 196 | static int
|
|---|
| 197 | rtc_dev_initialize(rtc_t *rtc)
|
|---|
| 198 | {
|
|---|
| 199 | int rc;
|
|---|
| 200 | size_t i;
|
|---|
| 201 | hw_resource_t *res;
|
|---|
| 202 | bool ioport = false;
|
|---|
| 203 | async_sess_t *parent_sess;
|
|---|
| 204 |
|
|---|
| 205 | ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev));
|
|---|
| 206 |
|
|---|
| 207 | rtc->boottime = 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->boottime != 0) {
|
|---|
| 329 | /* There is no need to read the current time from the
|
|---|
| 330 | * device because it has already been cached.
|
|---|
| 331 | */
|
|---|
| 332 |
|
|---|
| 333 | time_t cur_time = rtc->boottime + uptime_get();
|
|---|
| 334 |
|
|---|
| 335 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 336 |
|
|---|
| 337 | return time_local2tm(cur_time, t);
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /* Check if the RTC battery is OK */
|
|---|
| 341 | if (!is_battery_ok(rtc)) {
|
|---|
| 342 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 343 | return EIO;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | /* now read the registers */
|
|---|
| 347 | do {
|
|---|
| 348 | /* Suspend until the update process has finished */
|
|---|
| 349 | while (rtc_update_in_progress(rtc));
|
|---|
| 350 |
|
|---|
| 351 | t->tm_sec = rtc_register_read(rtc, RTC_SEC);
|
|---|
| 352 | t->tm_min = rtc_register_read(rtc, RTC_MIN);
|
|---|
| 353 | t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
|
|---|
| 354 | t->tm_mday = rtc_register_read(rtc, RTC_DAY);
|
|---|
| 355 | t->tm_mon = rtc_register_read(rtc, RTC_MON);
|
|---|
| 356 | t->tm_year = rtc_register_read(rtc, RTC_YEAR);
|
|---|
| 357 |
|
|---|
| 358 | /* Now check if it is stable */
|
|---|
| 359 | } while(t->tm_sec != rtc_register_read(rtc, RTC_SEC) ||
|
|---|
| 360 | t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
|
|---|
| 361 | t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
|
|---|
| 362 | t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
|
|---|
| 363 | t->tm_year != rtc_register_read(rtc, RTC_YEAR));
|
|---|
| 364 |
|
|---|
| 365 | /* Check if the RTC is working in 12h mode */
|
|---|
| 366 | bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
|
|---|
| 367 | RTC_B_24H);
|
|---|
| 368 |
|
|---|
| 369 | if (_12h_mode) {
|
|---|
| 370 | /* The RTC is working in 12h mode, check if it is AM or PM */
|
|---|
| 371 | if (t->tm_hour & 0x80) {
|
|---|
| 372 | /* PM flag is active, it must be cleared */
|
|---|
| 373 | t->tm_hour &= ~0x80;
|
|---|
| 374 | pm_mode = true;
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | /* Check if the RTC is working in BCD mode */
|
|---|
| 379 | bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
|
|---|
| 380 |
|
|---|
| 381 | if (bcd_mode) {
|
|---|
| 382 | t->tm_sec = bcd2bin(t->tm_sec);
|
|---|
| 383 | t->tm_min = bcd2bin(t->tm_min);
|
|---|
| 384 | t->tm_hour = bcd2bin(t->tm_hour);
|
|---|
| 385 | t->tm_mday = bcd2bin(t->tm_mday);
|
|---|
| 386 | t->tm_mon = bcd2bin(t->tm_mon);
|
|---|
| 387 | t->tm_year = bcd2bin(t->tm_year);
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | if (_12h_mode) {
|
|---|
| 391 | /* Convert to 24h mode */
|
|---|
| 392 | if (pm_mode) {
|
|---|
| 393 | if (t->tm_hour < 12)
|
|---|
| 394 | t->tm_hour += 12;
|
|---|
| 395 | } else if (t->tm_hour == 12)
|
|---|
| 396 | t->tm_hour = 0;
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | /* Count the months starting from 0, not from 1 */
|
|---|
| 400 | t->tm_mon--;
|
|---|
| 401 |
|
|---|
| 402 | if (t->tm_year < 100) {
|
|---|
| 403 | /* tm_year is the number of years since 1900 but the
|
|---|
| 404 | * RTC epoch is 2000.
|
|---|
| 405 | */
|
|---|
| 406 | t->tm_year += 100;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | /* Try to normalize the content of the tm structure */
|
|---|
| 410 | time_t r = mktime(t);
|
|---|
| 411 | int result;
|
|---|
| 412 |
|
|---|
| 413 | if (r < 0)
|
|---|
| 414 | result = EINVAL;
|
|---|
| 415 | else {
|
|---|
| 416 | rtc->boottime = r - uptime_get();
|
|---|
| 417 | result = EOK;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 421 |
|
|---|
| 422 | return result;
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | /** Set the time in the RTC
|
|---|
| 426 | *
|
|---|
| 427 | * @param fun The RTC function
|
|---|
| 428 | * @param t The time value to set
|
|---|
| 429 | *
|
|---|
| 430 | * @return EOK or a negative error code
|
|---|
| 431 | */
|
|---|
| 432 | static int
|
|---|
| 433 | rtc_time_set(ddf_fun_t *fun, struct tm *t)
|
|---|
| 434 | {
|
|---|
| 435 | bool bcd_mode;
|
|---|
| 436 | time_t norm_time;
|
|---|
| 437 | time_t uptime;
|
|---|
| 438 | int reg_b;
|
|---|
| 439 | int reg_a;
|
|---|
| 440 | int epoch;
|
|---|
| 441 | rtc_t *rtc = fun_rtc(fun);
|
|---|
| 442 |
|
|---|
| 443 | /* Try to normalize the content of the tm structure */
|
|---|
| 444 | if ((norm_time = mktime(t)) < 0)
|
|---|
| 445 | return EINVAL;
|
|---|
| 446 |
|
|---|
| 447 | uptime = uptime_get();
|
|---|
| 448 | if (norm_time <= uptime) {
|
|---|
| 449 | /* This is not acceptable */
|
|---|
| 450 | return EINVAL;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | fibril_mutex_lock(&rtc->mutex);
|
|---|
| 454 |
|
|---|
| 455 | if (!is_battery_ok(rtc)) {
|
|---|
| 456 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 457 | return EIO;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /* boottime must be recomputed */
|
|---|
| 461 | rtc->boottime = 0;
|
|---|
| 462 |
|
|---|
| 463 | /* Detect the RTC epoch */
|
|---|
| 464 | if (rtc_register_read(rtc, RTC_YEAR) < 100)
|
|---|
| 465 | epoch = 2000;
|
|---|
| 466 | else
|
|---|
| 467 | epoch = 1900;
|
|---|
| 468 |
|
|---|
| 469 | if (epoch == 2000 && t->tm_year < 100) {
|
|---|
| 470 | /* Can't set a year before the epoch */
|
|---|
| 471 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 472 | return EINVAL;
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | t->tm_mon++; /* counts from 1, not from 0 */
|
|---|
| 476 |
|
|---|
| 477 | reg_b = rtc_register_read(rtc, RTC_STATUS_B);
|
|---|
| 478 |
|
|---|
| 479 | if (!(reg_b & RTC_B_24H)) {
|
|---|
| 480 | /* Force 24h mode of operation */
|
|---|
| 481 | reg_b |= RTC_B_24H;
|
|---|
| 482 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | if (epoch == 2000) {
|
|---|
| 486 | /* The RTC epoch is year 2000 but the tm_year
|
|---|
| 487 | * field counts years since 1900.
|
|---|
| 488 | */
|
|---|
| 489 | t->tm_year -= 100;
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | /* Check if the rtc is working in bcd mode */
|
|---|
| 493 | bcd_mode = !(reg_b & RTC_B_BCD);
|
|---|
| 494 | if (bcd_mode) {
|
|---|
| 495 | /* Convert the tm struct fields in BCD mode */
|
|---|
| 496 | t->tm_sec = bin2bcd(t->tm_sec);
|
|---|
| 497 | t->tm_min = bin2bcd(t->tm_min);
|
|---|
| 498 | t->tm_hour = bin2bcd(t->tm_hour);
|
|---|
| 499 | t->tm_mday = bin2bcd(t->tm_mday);
|
|---|
| 500 | t->tm_mon = bin2bcd(t->tm_mon);
|
|---|
| 501 | t->tm_year = bin2bcd(t->tm_year);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | /* Inhibit updates */
|
|---|
| 505 | rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
|
|---|
| 506 |
|
|---|
| 507 | /* Write current time to RTC */
|
|---|
| 508 | rtc_register_write(rtc, RTC_SEC, t->tm_sec);
|
|---|
| 509 | rtc_register_write(rtc, RTC_MIN, t->tm_min);
|
|---|
| 510 | rtc_register_write(rtc, RTC_HOUR, t->tm_hour);
|
|---|
| 511 | rtc_register_write(rtc, RTC_DAY, t->tm_mday);
|
|---|
| 512 | rtc_register_write(rtc, RTC_MON, t->tm_mon);
|
|---|
| 513 | rtc_register_write(rtc, RTC_YEAR, t->tm_year);
|
|---|
| 514 |
|
|---|
| 515 | /* Stop the clock */
|
|---|
| 516 | reg_a = rtc_register_read(rtc, RTC_STATUS_A);
|
|---|
| 517 | rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
|
|---|
| 518 |
|
|---|
| 519 | /* Enable updates */
|
|---|
| 520 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
|---|
| 521 | rtc_register_write(rtc, RTC_STATUS_A, reg_a);
|
|---|
| 522 |
|
|---|
| 523 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 524 |
|
|---|
| 525 | return EOK;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | /** Get the status of the real time clock battery
|
|---|
| 529 | *
|
|---|
| 530 | * @param fun The RTC function
|
|---|
| 531 | * @param status The status of the battery
|
|---|
| 532 | *
|
|---|
| 533 | * @return EOK on success or a negative error code
|
|---|
| 534 | */
|
|---|
| 535 | static int
|
|---|
| 536 | rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status)
|
|---|
| 537 | {
|
|---|
| 538 | rtc_t *rtc = fun_rtc(fun);
|
|---|
| 539 |
|
|---|
| 540 | fibril_mutex_lock(&rtc->mutex);
|
|---|
| 541 | const bool batt_ok = is_battery_ok(rtc);
|
|---|
| 542 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 543 |
|
|---|
| 544 | *status = batt_ok ? BATTERY_OK : BATTERY_LOW;
|
|---|
| 545 |
|
|---|
| 546 | return EOK;
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | /** Check if the battery is working properly or not.
|
|---|
| 550 | * The caller already holds the rtc->mutex lock.
|
|---|
| 551 | *
|
|---|
| 552 | * @param rtc The RTC instance.
|
|---|
| 553 | *
|
|---|
| 554 | * @return true if the battery is ok, false otherwise.
|
|---|
| 555 | */
|
|---|
| 556 | static bool
|
|---|
| 557 | is_battery_ok(rtc_t *rtc)
|
|---|
| 558 | {
|
|---|
| 559 | return rtc_register_read(rtc, RTC_STATUS_D) & RTC_D_BATTERY_OK;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | /** The dev_add callback of the rtc driver
|
|---|
| 563 | *
|
|---|
| 564 | * @param dev The RTC device
|
|---|
| 565 | *
|
|---|
| 566 | * @return EOK on success or a negative error code
|
|---|
| 567 | */
|
|---|
| 568 | static int
|
|---|
| 569 | rtc_dev_add(ddf_dev_t *dev)
|
|---|
| 570 | {
|
|---|
| 571 | rtc_t *rtc;
|
|---|
| 572 | ddf_fun_t *fun = NULL;
|
|---|
| 573 | int rc;
|
|---|
| 574 | bool need_cleanup = false;
|
|---|
| 575 |
|
|---|
| 576 | ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
|
|---|
| 577 | ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
|
|---|
| 578 |
|
|---|
| 579 | rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
|
|---|
| 580 | if (!rtc)
|
|---|
| 581 | return ENOMEM;
|
|---|
| 582 |
|
|---|
| 583 | rtc->dev = dev;
|
|---|
| 584 | fibril_mutex_initialize(&rtc->mutex);
|
|---|
| 585 |
|
|---|
| 586 | rc = rtc_dev_initialize(rtc);
|
|---|
| 587 | if (rc != EOK)
|
|---|
| 588 | goto error;
|
|---|
| 589 |
|
|---|
| 590 | need_cleanup = true;
|
|---|
| 591 |
|
|---|
| 592 | if (!rtc_pio_enable(rtc)) {
|
|---|
| 593 | rc = EADDRNOTAVAIL;
|
|---|
| 594 | goto error;
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | fun = ddf_fun_create(dev, fun_exposed, "a");
|
|---|
| 598 | if (!fun) {
|
|---|
| 599 | ddf_msg(LVL_ERROR, "Failed creating function");
|
|---|
| 600 | rc = ENOENT;
|
|---|
| 601 | goto error;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | ddf_fun_set_ops(fun, &rtc_dev_ops);
|
|---|
| 605 | rc = ddf_fun_bind(fun);
|
|---|
| 606 | if (rc != EOK) {
|
|---|
| 607 | ddf_msg(LVL_ERROR, "Failed binding function");
|
|---|
| 608 | goto error;
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | rtc->fun = fun;
|
|---|
| 612 |
|
|---|
| 613 | ddf_fun_add_to_category(fun, "clock");
|
|---|
| 614 |
|
|---|
| 615 | ddf_msg(LVL_NOTE, "Device %s successfully initialized",
|
|---|
| 616 | ddf_dev_get_name(dev));
|
|---|
| 617 |
|
|---|
| 618 | return rc;
|
|---|
| 619 |
|
|---|
| 620 | error:
|
|---|
| 621 | if (fun)
|
|---|
| 622 | ddf_fun_destroy(fun);
|
|---|
| 623 | if (need_cleanup)
|
|---|
| 624 | rtc_dev_cleanup(rtc);
|
|---|
| 625 | return rc;
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | /** The dev_remove callback for the rtc driver
|
|---|
| 629 | *
|
|---|
| 630 | * @param dev The RTC device
|
|---|
| 631 | *
|
|---|
| 632 | * @return EOK on success or a negative error code
|
|---|
| 633 | */
|
|---|
| 634 | static int
|
|---|
| 635 | rtc_dev_remove(ddf_dev_t *dev)
|
|---|
| 636 | {
|
|---|
| 637 | rtc_t *rtc = dev_rtc(dev);
|
|---|
| 638 | int rc;
|
|---|
| 639 |
|
|---|
| 640 | fibril_mutex_lock(&rtc->mutex);
|
|---|
| 641 | if (rtc->clients_connected > 0) {
|
|---|
| 642 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 643 | return EBUSY;
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | rtc->removed = true;
|
|---|
| 647 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 648 |
|
|---|
| 649 | rc = rtc_fun_offline(rtc->fun);
|
|---|
| 650 | if (rc != EOK) {
|
|---|
| 651 | ddf_msg(LVL_ERROR, "Failed to offline function");
|
|---|
| 652 | return rc;
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | rc = ddf_fun_unbind(rtc->fun);
|
|---|
| 656 | if (rc != EOK) {
|
|---|
| 657 | ddf_msg(LVL_ERROR, "Failed to unbind function");
|
|---|
| 658 | return rc;
|
|---|
| 659 | }
|
|---|
| 660 |
|
|---|
| 661 | ddf_fun_destroy(rtc->fun);
|
|---|
| 662 | rtc_dev_cleanup(rtc);
|
|---|
| 663 |
|
|---|
| 664 | return rc;
|
|---|
| 665 | }
|
|---|
| 666 |
|
|---|
| 667 | /** Open the device
|
|---|
| 668 | *
|
|---|
| 669 | * @param fun The function node
|
|---|
| 670 | *
|
|---|
| 671 | * @return EOK on success or a negative error code
|
|---|
| 672 | */
|
|---|
| 673 | static int
|
|---|
| 674 | rtc_open(ddf_fun_t *fun)
|
|---|
| 675 | {
|
|---|
| 676 | int rc;
|
|---|
| 677 | rtc_t *rtc = fun_rtc(fun);
|
|---|
| 678 |
|
|---|
| 679 | fibril_mutex_lock(&rtc->mutex);
|
|---|
| 680 |
|
|---|
| 681 | if (rtc->removed)
|
|---|
| 682 | rc = ENXIO;
|
|---|
| 683 | else {
|
|---|
| 684 | rc = EOK;
|
|---|
| 685 | rtc->clients_connected++;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 689 | return rc;
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | /** Close the device
|
|---|
| 693 | *
|
|---|
| 694 | * @param fun The function node
|
|---|
| 695 | */
|
|---|
| 696 | static void
|
|---|
| 697 | rtc_close(ddf_fun_t *fun)
|
|---|
| 698 | {
|
|---|
| 699 | rtc_t *rtc = fun_rtc(fun);
|
|---|
| 700 |
|
|---|
| 701 | fibril_mutex_lock(&rtc->mutex);
|
|---|
| 702 |
|
|---|
| 703 | rtc->clients_connected--;
|
|---|
| 704 | assert(rtc->clients_connected >= 0);
|
|---|
| 705 |
|
|---|
| 706 | fibril_mutex_unlock(&rtc->mutex);
|
|---|
| 707 | }
|
|---|
| 708 |
|
|---|
| 709 | /** Convert from BCD mode to binary mode
|
|---|
| 710 | *
|
|---|
| 711 | * @param bcd The number in BCD format to convert
|
|---|
| 712 | *
|
|---|
| 713 | * @return The converted value
|
|---|
| 714 | */
|
|---|
| 715 | static unsigned
|
|---|
| 716 | bcd2bin(unsigned bcd)
|
|---|
| 717 | {
|
|---|
| 718 | return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
|
|---|
| 719 | }
|
|---|
| 720 |
|
|---|
| 721 | /** Convert from binary mode to BCD mode
|
|---|
| 722 | *
|
|---|
| 723 | * @param bcd The number in binary mode to convert
|
|---|
| 724 | *
|
|---|
| 725 | * @return The converted value
|
|---|
| 726 | */
|
|---|
| 727 | static unsigned
|
|---|
| 728 | bin2bcd(unsigned binary)
|
|---|
| 729 | {
|
|---|
| 730 | return ((binary / 10) << 4) + (binary % 10);
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | static time_t
|
|---|
| 734 | uptime_get(void)
|
|---|
| 735 | {
|
|---|
| 736 | struct timeval tv;
|
|---|
| 737 |
|
|---|
| 738 | getuptime(&tv);
|
|---|
| 739 |
|
|---|
| 740 | return tv.tv_sec;
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | static int
|
|---|
| 744 | rtc_fun_online(ddf_fun_t *fun)
|
|---|
| 745 | {
|
|---|
| 746 | int rc;
|
|---|
| 747 |
|
|---|
| 748 | ddf_msg(LVL_DEBUG, "rtc_fun_online()");
|
|---|
| 749 |
|
|---|
| 750 | rc = ddf_fun_online(fun);
|
|---|
| 751 | if (rc == EOK)
|
|---|
| 752 | ddf_fun_add_to_category(fun, "clock");
|
|---|
| 753 |
|
|---|
| 754 | return rc;
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | static int
|
|---|
| 758 | rtc_fun_offline(ddf_fun_t *fun)
|
|---|
| 759 | {
|
|---|
| 760 | ddf_msg(LVL_DEBUG, "rtc_fun_offline()");
|
|---|
| 761 | return ddf_fun_offline(fun);
|
|---|
| 762 | }
|
|---|
| 763 |
|
|---|
| 764 | int
|
|---|
| 765 | main(int argc, char **argv)
|
|---|
| 766 | {
|
|---|
| 767 | printf(NAME ": HelenOS RTC driver\n");
|
|---|
| 768 | rtc_init();
|
|---|
| 769 | return ddf_driver_main(&rtc_driver);
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | /**
|
|---|
| 773 | * @}
|
|---|
| 774 | */
|
|---|