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