[1e19a15] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Maurizio Lombardi
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * @defgroup CMOS RTC driver.
|
---|
| 31 | * @brief HelenOS RTC driver.
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[4863bb52] | 38 | #include <errno.h>
|
---|
[1e19a15] | 39 | #include <ddi.h>
|
---|
[8fde078] | 40 | #include <as.h>
|
---|
| 41 | #include <sysinfo.h>
|
---|
| 42 | #include <libarch/barrier.h>
|
---|
[1e19a15] | 43 | #include <stdio.h>
|
---|
| 44 | #include <ddf/driver.h>
|
---|
[a2e4889] | 45 | #include <ddf/log.h>
|
---|
[aeef318] | 46 | #include <ops/clock_dev.h>
|
---|
[917797f] | 47 | #include <ops/battery_dev.h>
|
---|
[6b329749] | 48 | #include <fibril_synch.h>
|
---|
[b3db669] | 49 | #include <device/hw_res.h>
|
---|
[8fde078] | 50 | #include <macros.h>
|
---|
[83298e8] | 51 | #include <time.h>
|
---|
[1e19a15] | 52 |
|
---|
[f30ee571] | 53 | #include "cmos-regs.h"
|
---|
| 54 |
|
---|
[5ef13847] | 55 | #define NAME "cmos-rtc"
|
---|
[1e19a15] | 56 |
|
---|
[0883de8] | 57 | #define REG_COUNT 2
|
---|
| 58 |
|
---|
[78ca12b] | 59 | #define REG_SEL_PORT(port) (port)
|
---|
| 60 | #define REG_RW_PORT(port) ((port) + 1)
|
---|
| 61 |
|
---|
[b3db669] | 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;
|
---|
[c47f1b6] | 69 | /** The base I/O address of the device registers */
|
---|
[971f50e7] | 70 | ioport8_t *io_addr;
|
---|
[0883de8] | 71 | /** The I/O port used to access the CMOS registers */
|
---|
| 72 | ioport8_t *port;
|
---|
[bb8f69d] | 73 | /** true if device is removed */
|
---|
| 74 | bool removed;
|
---|
[9673fd3] | 75 | /** number of connected clients */
|
---|
| 76 | int clients_connected;
|
---|
[60af6fc2] | 77 | /** time at which the system booted */
|
---|
| 78 | time_t boottime;
|
---|
[b3db669] | 79 | } rtc_t;
|
---|
| 80 |
|
---|
[6f445a67] | 81 | static rtc_t *dev_rtc(ddf_dev_t *dev);
|
---|
| 82 | static rtc_t *fun_rtc(ddf_fun_t *fun);
|
---|
[efcfab9] | 83 | static int
|
---|
| 84 | rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status);
|
---|
[2a5171db] | 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);
|
---|
[f30ee571] | 93 | static bool rtc_update_in_progress(rtc_t *rtc);
|
---|
[db8d552] | 94 | static int rtc_register_read(rtc_t *rtc, int reg);
|
---|
[1170ea3c] | 95 | static unsigned bcd2bin(unsigned bcd);
|
---|
| 96 | static unsigned bin2bcd(unsigned binary);
|
---|
[bb8f69d] | 97 | static int rtc_dev_remove(ddf_dev_t *dev);
|
---|
[a8a0d43] | 98 | static void rtc_register_write(rtc_t *rtc, int reg, int data);
|
---|
[8fde078] | 99 | static time_t uptime_get(void);
|
---|
[0a586f4c] | 100 | static bool is_battery_ok(rtc_t *rtc);
|
---|
[b801f2d] | 101 | static int rtc_fun_online(ddf_fun_t *fun);
|
---|
| 102 | static int rtc_fun_offline(ddf_fun_t *fun);
|
---|
[4863bb52] | 103 |
|
---|
[1e19a15] | 104 | static ddf_dev_ops_t rtc_dev_ops;
|
---|
| 105 |
|
---|
[a31ca11f] | 106 | /** The RTC device driver's standard operations */
|
---|
| 107 | static driver_ops_t rtc_ops = {
|
---|
| 108 | .dev_add = rtc_dev_add,
|
---|
[bb8f69d] | 109 | .dev_remove = rtc_dev_remove,
|
---|
[b801f2d] | 110 | .fun_online = rtc_fun_online,
|
---|
| 111 | .fun_offline = rtc_fun_offline,
|
---|
[a31ca11f] | 112 | };
|
---|
| 113 |
|
---|
| 114 | /** The RTC device driver structure */
|
---|
[1e19a15] | 115 | static driver_t rtc_driver = {
|
---|
| 116 | .name = NAME,
|
---|
[a31ca11f] | 117 | .driver_ops = &rtc_ops,
|
---|
[1e19a15] | 118 | };
|
---|
| 119 |
|
---|
[a31ca11f] | 120 | /** Clock interface */
|
---|
[4863bb52] | 121 | static clock_dev_ops_t rtc_clock_dev_ops = {
|
---|
| 122 | .time_get = rtc_time_get,
|
---|
| 123 | .time_set = rtc_time_set,
|
---|
| 124 | };
|
---|
| 125 |
|
---|
[917797f] | 126 | /** Battery powered device interface */
|
---|
| 127 | static battery_dev_ops_t rtc_battery_dev_ops = {
|
---|
[efcfab9] | 128 | .battery_status_get = rtc_battery_status_get,
|
---|
[917797f] | 129 | .battery_charge_level_get = NULL,
|
---|
| 130 | };
|
---|
| 131 |
|
---|
[6f445a67] | 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 |
|
---|
[4863bb52] | 146 | /** Initialize the RTC driver */
|
---|
[1e19a15] | 147 | static void
|
---|
| 148 | rtc_init(void)
|
---|
| 149 | {
|
---|
[289fa65] | 150 | ddf_log_init(NAME);
|
---|
[1e19a15] | 151 |
|
---|
[923b2eba] | 152 | rtc_dev_ops.open = rtc_open;
|
---|
[2a5171db] | 153 | rtc_dev_ops.close = rtc_close;
|
---|
[4863bb52] | 154 |
|
---|
| 155 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
---|
[917797f] | 156 | rtc_dev_ops.interfaces[BATTERY_DEV_IFACE] = &rtc_battery_dev_ops;
|
---|
| 157 | rtc_dev_ops.default_handler = NULL;
|
---|
[4863bb52] | 158 | }
|
---|
| 159 |
|
---|
[4b44de57] | 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 |
|
---|
[0883de8] | 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 | {
|
---|
[85f7369] | 178 | if (pio_enable((void *) rtc->io_addr, REG_COUNT,
|
---|
[0883de8] | 179 | (void **) &rtc->port)) {
|
---|
| 180 |
|
---|
[276e44a] | 181 | ddf_msg(LVL_ERROR, "Cannot map the port %lx"
|
---|
[7ff35c7] | 182 | " for device %s", (long unsigned int)rtc->io_addr,
|
---|
[276e44a] | 183 | ddf_dev_get_name(rtc->dev));
|
---|
[0883de8] | 184 | return false;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | return true;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[b3db669] | 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;
|
---|
[c47f1b6] | 200 | size_t i;
|
---|
| 201 | hw_resource_t *res;
|
---|
| 202 | bool ioport = false;
|
---|
[6f445a67] | 203 | async_sess_t *parent_sess;
|
---|
[b3db669] | 204 |
|
---|
[6f445a67] | 205 | ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev));
|
---|
[b3db669] | 206 |
|
---|
[762083b] | 207 | rtc->boottime = 0;
|
---|
[9673fd3] | 208 | rtc->clients_connected = 0;
|
---|
[762083b] | 209 |
|
---|
[b3db669] | 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 |
|
---|
[6f445a67] | 215 | parent_sess = ddf_dev_parent_sess_create(rtc->dev, EXCHANGE_SERIALIZE);
|
---|
| 216 | if (!parent_sess) {
|
---|
[b3db669] | 217 | ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
|
---|
[6f445a67] | 218 | of device %s.", ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 219 | rc = ENOENT;
|
---|
| 220 | goto error;
|
---|
[b3db669] | 221 | }
|
---|
| 222 |
|
---|
| 223 | /* Get the HW resources */
|
---|
[6f445a67] | 224 | rc = hw_res_get_resource_list(parent_sess, &hw_resources);
|
---|
[b3db669] | 225 | if (rc != EOK) {
|
---|
| 226 | ddf_msg(LVL_ERROR, "Failed to get HW resources\
|
---|
[6f445a67] | 227 | for device %s", ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 228 | goto error;
|
---|
[b3db669] | 229 | }
|
---|
| 230 |
|
---|
[c47f1b6] | 231 | for (i = 0; i < hw_resources.count; ++i) {
|
---|
| 232 | res = &hw_resources.resources[i];
|
---|
| 233 |
|
---|
[ae827d0] | 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;
|
---|
[c47f1b6] | 240 | }
|
---|
[ae827d0] | 241 |
|
---|
[7ff35c7] | 242 | rtc->io_addr = (ioport8_t *) (long) res->res.io_range.address;
|
---|
[ae827d0] | 243 | ioport = true;
|
---|
| 244 | ddf_msg(LVL_NOTE, "Device %s was assigned I/O address "
|
---|
[276e44a] | 245 | "0x%lx", ddf_dev_get_name(rtc->dev),
|
---|
[7ff35c7] | 246 | (unsigned long int) rtc->io_addr);
|
---|
[ae827d0] | 247 | rc = EOK;
|
---|
| 248 | break;
|
---|
[c47f1b6] | 249 | }
|
---|
| 250 |
|
---|
[ae827d0] | 251 | if (rc != EOK)
|
---|
| 252 | goto error;
|
---|
| 253 |
|
---|
[c47f1b6] | 254 | if (!ioport) {
|
---|
| 255 | /* No I/O address assigned to this device */
|
---|
| 256 | ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
|
---|
[6f445a67] | 257 | ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 258 | rc = ENOENT;
|
---|
| 259 | goto error;
|
---|
[c47f1b6] | 260 | }
|
---|
| 261 |
|
---|
| 262 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 263 |
|
---|
[b3db669] | 264 | return EOK;
|
---|
[4b44de57] | 265 |
|
---|
| 266 | error:
|
---|
| 267 | rtc_dev_cleanup(rtc);
|
---|
| 268 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 269 |
|
---|
| 270 | return rc;
|
---|
[b3db669] | 271 | }
|
---|
| 272 |
|
---|
[3b79ba5] | 273 | /** Read a register from the CMOS memory
|
---|
| 274 | *
|
---|
[1170ea3c] | 275 | * @param rtc The rtc device
|
---|
[3b79ba5] | 276 | * @param reg The index of the register to read
|
---|
| 277 | *
|
---|
| 278 | * @return The value of the register
|
---|
| 279 | */
|
---|
| 280 | static int
|
---|
[db8d552] | 281 | rtc_register_read(rtc_t *rtc, int reg)
|
---|
[3b79ba5] | 282 | {
|
---|
[78ca12b] | 283 | pio_write_8(REG_SEL_PORT(rtc->port), reg);
|
---|
| 284 | return pio_read_8(REG_RW_PORT(rtc->port));
|
---|
[3b79ba5] | 285 | }
|
---|
| 286 |
|
---|
[1170ea3c] | 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 | {
|
---|
[78ca12b] | 296 | pio_write_8(REG_SEL_PORT(rtc->port), reg);
|
---|
| 297 | pio_write_8(REG_RW_PORT(rtc->port), data);
|
---|
[1170ea3c] | 298 | }
|
---|
| 299 |
|
---|
[f30ee571] | 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 | {
|
---|
[6a3808e] | 309 | return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
|
---|
[f30ee571] | 310 | }
|
---|
| 311 |
|
---|
[0b8a3e7] | 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 | */
|
---|
[4863bb52] | 319 | static int
|
---|
[8d2963d] | 320 | rtc_time_get(ddf_fun_t *fun, struct tm *t)
|
---|
[4863bb52] | 321 | {
|
---|
[db8d552] | 322 | bool bcd_mode;
|
---|
[95060d5b] | 323 | bool pm_mode = false;
|
---|
[6f445a67] | 324 | rtc_t *rtc = fun_rtc(fun);
|
---|
[f30ee571] | 325 |
|
---|
[60af6fc2] | 326 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 327 |
|
---|
| 328 | if (rtc->boottime != 0) {
|
---|
[ea5cc5b] | 329 | /* There is no need to read the current time from the
|
---|
| 330 | * device because it has already been cached.
|
---|
| 331 | */
|
---|
| 332 |
|
---|
[60af6fc2] | 333 | time_t cur_time = rtc->boottime + uptime_get();
|
---|
| 334 |
|
---|
| 335 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 336 |
|
---|
[664fc031] | 337 | return time_local2tm(cur_time, t);
|
---|
[ea5cc5b] | 338 | }
|
---|
| 339 |
|
---|
[0a586f4c] | 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 |
|
---|
[db8d552] | 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 */
|
---|
[c9abf50] | 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));
|
---|
[db8d552] | 364 |
|
---|
[95060d5b] | 365 | /* Check if the RTC is working in 12h mode */
|
---|
| 366 | bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
|
---|
[6a3808e] | 367 | RTC_B_24H);
|
---|
[95060d5b] | 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) {
|
---|
[f004318] | 372 | /* PM flag is active, it must be cleared */
|
---|
[95060d5b] | 373 | t->tm_hour &= ~0x80;
|
---|
| 374 | pm_mode = true;
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 |
|
---|
[db8d552] | 378 | /* Check if the RTC is working in BCD mode */
|
---|
[6a3808e] | 379 | bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
|
---|
[db8d552] | 380 |
|
---|
[fc7d28e] | 381 | if (bcd_mode) {
|
---|
[1170ea3c] | 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);
|
---|
[db8d552] | 388 | }
|
---|
[f30ee571] | 389 |
|
---|
[95060d5b] | 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 |
|
---|
[05ed9d7] | 399 | /* Count the months starting from 0, not from 1 */
|
---|
| 400 | t->tm_mon--;
|
---|
| 401 |
|
---|
[fc7d28e] | 402 | if (t->tm_year < 100) {
|
---|
[e5fbe06] | 403 | /* tm_year is the number of years since 1900 but the
|
---|
| 404 | * RTC epoch is 2000.
|
---|
[fc7d28e] | 405 | */
|
---|
| 406 | t->tm_year += 100;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[af7e3d3] | 409 | /* Try to normalize the content of the tm structure */
|
---|
[fa18523] | 410 | time_t r = mktime(t);
|
---|
[508fff8] | 411 | int result;
|
---|
[fa18523] | 412 |
|
---|
[508fff8] | 413 | if (r < 0)
|
---|
| 414 | result = EINVAL;
|
---|
| 415 | else {
|
---|
| 416 | rtc->boottime = r - uptime_get();
|
---|
| 417 | result = EOK;
|
---|
| 418 | }
|
---|
[60af6fc2] | 419 |
|
---|
| 420 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[ea5cc5b] | 421 |
|
---|
[508fff8] | 422 | return result;
|
---|
[4863bb52] | 423 | }
|
---|
| 424 |
|
---|
[0b8a3e7] | 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 | */
|
---|
[4863bb52] | 432 | static int
|
---|
[8d2963d] | 433 | rtc_time_set(ddf_fun_t *fun, struct tm *t)
|
---|
[4863bb52] | 434 | {
|
---|
[1170ea3c] | 435 | bool bcd_mode;
|
---|
[8fde078] | 436 | time_t norm_time;
|
---|
| 437 | time_t uptime;
|
---|
[a8a0d43] | 438 | int reg_b;
|
---|
| 439 | int reg_a;
|
---|
[e5fbe06] | 440 | int epoch;
|
---|
[6f445a67] | 441 | rtc_t *rtc = fun_rtc(fun);
|
---|
[1170ea3c] | 442 |
|
---|
[af7e3d3] | 443 | /* Try to normalize the content of the tm structure */
|
---|
[8fde078] | 444 | if ((norm_time = mktime(t)) < 0)
|
---|
[fa18523] | 445 | return EINVAL;
|
---|
| 446 |
|
---|
[8fde078] | 447 | uptime = uptime_get();
|
---|
| 448 | if (norm_time <= uptime) {
|
---|
| 449 | /* This is not acceptable */
|
---|
| 450 | return EINVAL;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
[e5fbe06] | 453 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 454 |
|
---|
[0a586f4c] | 455 | if (!is_battery_ok(rtc)) {
|
---|
| 456 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 457 | return EIO;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[60af6fc2] | 460 | /* boottime must be recomputed */
|
---|
| 461 | rtc->boottime = 0;
|
---|
| 462 |
|
---|
[e5fbe06] | 463 | /* Detect the RTC epoch */
|
---|
| 464 | if (rtc_register_read(rtc, RTC_YEAR) < 100)
|
---|
| 465 | epoch = 2000;
|
---|
| 466 | else
|
---|
| 467 | epoch = 1900;
|
---|
| 468 |
|
---|
[fa18523] | 469 | if (epoch == 2000 && t->tm_year < 100) {
|
---|
| 470 | /* Can't set a year before the epoch */
|
---|
[e5fbe06] | 471 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[fa18523] | 472 | return EINVAL;
|
---|
[e5fbe06] | 473 | }
|
---|
[1170ea3c] | 474 |
|
---|
[a8a0d43] | 475 | t->tm_mon++; /* counts from 1, not from 0 */
|
---|
[1170ea3c] | 476 |
|
---|
[a8a0d43] | 477 | reg_b = rtc_register_read(rtc, RTC_STATUS_B);
|
---|
| 478 |
|
---|
[6a3808e] | 479 | if (!(reg_b & RTC_B_24H)) {
|
---|
[f004318] | 480 | /* Force 24h mode of operation */
|
---|
[6a3808e] | 481 | reg_b |= RTC_B_24H;
|
---|
[f004318] | 482 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
| 483 | }
|
---|
[f6af126] | 484 |
|
---|
[a44b58c] | 485 | if (epoch == 2000) {
|
---|
| 486 | /* The RTC epoch is year 2000 but the tm_year
|
---|
| 487 | * field counts years since 1900.
|
---|
| 488 | */
|
---|
[074324f1] | 489 | t->tm_year -= 100;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[1170ea3c] | 492 | /* Check if the rtc is working in bcd mode */
|
---|
[6a3808e] | 493 | bcd_mode = !(reg_b & RTC_B_BCD);
|
---|
[1170ea3c] | 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);
|
---|
[709476f4] | 500 | t->tm_mon = bin2bcd(t->tm_mon);
|
---|
[1170ea3c] | 501 | t->tm_year = bin2bcd(t->tm_year);
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[a8a0d43] | 504 | /* Inhibit updates */
|
---|
[6a3808e] | 505 | rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
|
---|
[a8a0d43] | 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);
|
---|
[6a3808e] | 517 | rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
|
---|
[a8a0d43] | 518 |
|
---|
| 519 | /* Enable updates */
|
---|
| 520 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
| 521 | rtc_register_write(rtc, RTC_STATUS_A, reg_a);
|
---|
[1170ea3c] | 522 |
|
---|
| 523 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 524 |
|
---|
[4863bb52] | 525 | return EOK;
|
---|
[1e19a15] | 526 | }
|
---|
| 527 |
|
---|
[efcfab9] | 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);
|
---|
[0a586f4c] | 539 |
|
---|
| 540 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 541 | const bool batt_ok = is_battery_ok(rtc);
|
---|
| 542 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[efcfab9] | 543 |
|
---|
| 544 | *status = batt_ok ? BATTERY_OK : BATTERY_LOW;
|
---|
| 545 |
|
---|
| 546 | return EOK;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
[0a586f4c] | 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 |
|
---|
[a31ca11f] | 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 | {
|
---|
[6b329749] | 571 | rtc_t *rtc;
|
---|
[4b44de57] | 572 | ddf_fun_t *fun = NULL;
|
---|
[b3db669] | 573 | int rc;
|
---|
[4b44de57] | 574 | bool need_cleanup = false;
|
---|
[6b329749] | 575 |
|
---|
| 576 | ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
|
---|
[6f445a67] | 577 | ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
|
---|
[6b329749] | 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 |
|
---|
[b3db669] | 586 | rc = rtc_dev_initialize(rtc);
|
---|
| 587 | if (rc != EOK)
|
---|
[4b44de57] | 588 | goto error;
|
---|
[b3db669] | 589 |
|
---|
[4b44de57] | 590 | need_cleanup = true;
|
---|
| 591 |
|
---|
| 592 | if (!rtc_pio_enable(rtc)) {
|
---|
| 593 | rc = EADDRNOTAVAIL;
|
---|
| 594 | goto error;
|
---|
| 595 | }
|
---|
[0883de8] | 596 |
|
---|
| 597 | fun = ddf_fun_create(dev, fun_exposed, "a");
|
---|
| 598 | if (!fun) {
|
---|
| 599 | ddf_msg(LVL_ERROR, "Failed creating function");
|
---|
[4b44de57] | 600 | rc = ENOENT;
|
---|
| 601 | goto error;
|
---|
[0883de8] | 602 | }
|
---|
| 603 |
|
---|
[6f445a67] | 604 | ddf_fun_set_ops(fun, &rtc_dev_ops);
|
---|
[0883de8] | 605 | rc = ddf_fun_bind(fun);
|
---|
| 606 | if (rc != EOK) {
|
---|
| 607 | ddf_msg(LVL_ERROR, "Failed binding function");
|
---|
[4b44de57] | 608 | goto error;
|
---|
[0883de8] | 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",
|
---|
[6f445a67] | 616 | ddf_dev_get_name(dev));
|
---|
[0883de8] | 617 |
|
---|
[b3db669] | 618 | return rc;
|
---|
[4b44de57] | 619 |
|
---|
| 620 | error:
|
---|
| 621 | if (fun)
|
---|
| 622 | ddf_fun_destroy(fun);
|
---|
| 623 | if (need_cleanup)
|
---|
| 624 | rtc_dev_cleanup(rtc);
|
---|
| 625 | return rc;
|
---|
[a31ca11f] | 626 | }
|
---|
| 627 |
|
---|
[bb8f69d] | 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 | {
|
---|
[6f445a67] | 637 | rtc_t *rtc = dev_rtc(dev);
|
---|
[bb8f69d] | 638 | int rc;
|
---|
| 639 |
|
---|
| 640 | fibril_mutex_lock(&rtc->mutex);
|
---|
[9673fd3] | 641 | if (rtc->clients_connected > 0) {
|
---|
| 642 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 643 | return EBUSY;
|
---|
| 644 | }
|
---|
[bb8f69d] | 645 |
|
---|
| 646 | rtc->removed = true;
|
---|
| 647 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 648 |
|
---|
[b801f2d] | 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 |
|
---|
[bb8f69d] | 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 |
|
---|
[923b2eba] | 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;
|
---|
[6f445a67] | 677 | rtc_t *rtc = fun_rtc(fun);
|
---|
[923b2eba] | 678 |
|
---|
| 679 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 680 |
|
---|
[43e660c] | 681 | if (rtc->removed)
|
---|
[bb8f69d] | 682 | rc = ENXIO;
|
---|
[9673fd3] | 683 | else {
|
---|
[923b2eba] | 684 | rc = EOK;
|
---|
[9673fd3] | 685 | rtc->clients_connected++;
|
---|
| 686 | }
|
---|
[923b2eba] | 687 |
|
---|
| 688 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 689 | return rc;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[2a5171db] | 692 | /** Close the device
|
---|
| 693 | *
|
---|
| 694 | * @param fun The function node
|
---|
| 695 | */
|
---|
| 696 | static void
|
---|
| 697 | rtc_close(ddf_fun_t *fun)
|
---|
| 698 | {
|
---|
[9673fd3] | 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);
|
---|
[2a5171db] | 707 | }
|
---|
| 708 |
|
---|
[db8d552] | 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 | */
|
---|
[1170ea3c] | 715 | static unsigned
|
---|
| 716 | bcd2bin(unsigned bcd)
|
---|
[db8d552] | 717 | {
|
---|
| 718 | return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
|
---|
| 719 | }
|
---|
| 720 |
|
---|
[1170ea3c] | 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 |
|
---|
[8fde078] | 733 | static time_t
|
---|
| 734 | uptime_get(void)
|
---|
| 735 | {
|
---|
[83298e8] | 736 | struct timeval tv;
|
---|
[8fde078] | 737 |
|
---|
[83298e8] | 738 | getuptime(&tv);
|
---|
| 739 |
|
---|
| 740 | return tv.tv_sec;
|
---|
[8fde078] | 741 | }
|
---|
| 742 |
|
---|
[b801f2d] | 743 | static int
|
---|
| 744 | rtc_fun_online(ddf_fun_t *fun)
|
---|
| 745 | {
|
---|
[a582dff] | 746 | int rc;
|
---|
| 747 |
|
---|
[b801f2d] | 748 | ddf_msg(LVL_DEBUG, "rtc_fun_online()");
|
---|
[a582dff] | 749 |
|
---|
| 750 | rc = ddf_fun_online(fun);
|
---|
| 751 | if (rc == EOK)
|
---|
| 752 | ddf_fun_add_to_category(fun, "clock");
|
---|
| 753 |
|
---|
| 754 | return rc;
|
---|
[b801f2d] | 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 |
|
---|
[1e19a15] | 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 | */
|
---|