[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>
|
---|
[f30ee571] | 42 | #include <libarch/ddi.h>
|
---|
[8fde078] | 43 | #include <libarch/barrier.h>
|
---|
[1e19a15] | 44 | #include <stdio.h>
|
---|
| 45 | #include <ddf/driver.h>
|
---|
[a2e4889] | 46 | #include <ddf/log.h>
|
---|
[aeef318] | 47 | #include <ops/clock_dev.h>
|
---|
[917797f] | 48 | #include <ops/battery_dev.h>
|
---|
[6b329749] | 49 | #include <fibril_synch.h>
|
---|
[b3db669] | 50 | #include <device/hw_res.h>
|
---|
[8fde078] | 51 | #include <macros.h>
|
---|
[83298e8] | 52 | #include <time.h>
|
---|
[1e19a15] | 53 |
|
---|
[f30ee571] | 54 | #include "cmos-regs.h"
|
---|
| 55 |
|
---|
[5ef13847] | 56 | #define NAME "cmos-rtc"
|
---|
[1e19a15] | 57 |
|
---|
[0883de8] | 58 | #define REG_COUNT 2
|
---|
| 59 |
|
---|
[78ca12b] | 60 | #define REG_SEL_PORT(port) (port)
|
---|
| 61 | #define REG_RW_PORT(port) ((port) + 1)
|
---|
| 62 |
|
---|
[b3db669] | 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;
|
---|
[c47f1b6] | 70 | /** The base I/O address of the device registers */
|
---|
[971f50e7] | 71 | ioport8_t *io_addr;
|
---|
[0883de8] | 72 | /** The I/O port used to access the CMOS registers */
|
---|
| 73 | ioport8_t *port;
|
---|
[bb8f69d] | 74 | /** true if device is removed */
|
---|
| 75 | bool removed;
|
---|
[9673fd3] | 76 | /** number of connected clients */
|
---|
| 77 | int clients_connected;
|
---|
[60af6fc2] | 78 | /** time at which the system booted */
|
---|
| 79 | time_t boottime;
|
---|
[b3db669] | 80 | } rtc_t;
|
---|
| 81 |
|
---|
[6f445a67] | 82 | static rtc_t *dev_rtc(ddf_dev_t *dev);
|
---|
| 83 | static rtc_t *fun_rtc(ddf_fun_t *fun);
|
---|
[efcfab9] | 84 | static int
|
---|
| 85 | rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status);
|
---|
[2a5171db] | 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);
|
---|
[f30ee571] | 94 | static bool rtc_update_in_progress(rtc_t *rtc);
|
---|
[db8d552] | 95 | static int rtc_register_read(rtc_t *rtc, int reg);
|
---|
[1170ea3c] | 96 | static unsigned bcd2bin(unsigned bcd);
|
---|
| 97 | static unsigned bin2bcd(unsigned binary);
|
---|
[bb8f69d] | 98 | static int rtc_dev_remove(ddf_dev_t *dev);
|
---|
[a8a0d43] | 99 | static void rtc_register_write(rtc_t *rtc, int reg, int data);
|
---|
[8fde078] | 100 | static time_t uptime_get(void);
|
---|
[0a586f4c] | 101 | static bool is_battery_ok(rtc_t *rtc);
|
---|
[b801f2d] | 102 | static int rtc_fun_online(ddf_fun_t *fun);
|
---|
| 103 | static int rtc_fun_offline(ddf_fun_t *fun);
|
---|
[4863bb52] | 104 |
|
---|
[1e19a15] | 105 | static ddf_dev_ops_t rtc_dev_ops;
|
---|
| 106 |
|
---|
[a31ca11f] | 107 | /** The RTC device driver's standard operations */
|
---|
| 108 | static driver_ops_t rtc_ops = {
|
---|
| 109 | .dev_add = rtc_dev_add,
|
---|
[bb8f69d] | 110 | .dev_remove = rtc_dev_remove,
|
---|
[b801f2d] | 111 | .fun_online = rtc_fun_online,
|
---|
| 112 | .fun_offline = rtc_fun_offline,
|
---|
[a31ca11f] | 113 | };
|
---|
| 114 |
|
---|
| 115 | /** The RTC device driver structure */
|
---|
[1e19a15] | 116 | static driver_t rtc_driver = {
|
---|
| 117 | .name = NAME,
|
---|
[a31ca11f] | 118 | .driver_ops = &rtc_ops,
|
---|
[1e19a15] | 119 | };
|
---|
| 120 |
|
---|
[a31ca11f] | 121 | /** Clock interface */
|
---|
[4863bb52] | 122 | static clock_dev_ops_t rtc_clock_dev_ops = {
|
---|
| 123 | .time_get = rtc_time_get,
|
---|
| 124 | .time_set = rtc_time_set,
|
---|
| 125 | };
|
---|
| 126 |
|
---|
[917797f] | 127 | /** Battery powered device interface */
|
---|
| 128 | static battery_dev_ops_t rtc_battery_dev_ops = {
|
---|
[efcfab9] | 129 | .battery_status_get = rtc_battery_status_get,
|
---|
[917797f] | 130 | .battery_charge_level_get = NULL,
|
---|
| 131 | };
|
---|
| 132 |
|
---|
[6f445a67] | 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 |
|
---|
[4863bb52] | 147 | /** Initialize the RTC driver */
|
---|
[1e19a15] | 148 | static void
|
---|
| 149 | rtc_init(void)
|
---|
| 150 | {
|
---|
[289fa65] | 151 | ddf_log_init(NAME);
|
---|
[1e19a15] | 152 |
|
---|
[923b2eba] | 153 | rtc_dev_ops.open = rtc_open;
|
---|
[2a5171db] | 154 | rtc_dev_ops.close = rtc_close;
|
---|
[4863bb52] | 155 |
|
---|
| 156 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
---|
[917797f] | 157 | rtc_dev_ops.interfaces[BATTERY_DEV_IFACE] = &rtc_battery_dev_ops;
|
---|
| 158 | rtc_dev_ops.default_handler = NULL;
|
---|
[4863bb52] | 159 | }
|
---|
| 160 |
|
---|
[4b44de57] | 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 |
|
---|
[0883de8] | 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 | {
|
---|
[85f7369] | 179 | if (pio_enable((void *) rtc->io_addr, REG_COUNT,
|
---|
[0883de8] | 180 | (void **) &rtc->port)) {
|
---|
| 181 |
|
---|
[276e44a] | 182 | ddf_msg(LVL_ERROR, "Cannot map the port %lx"
|
---|
[7ff35c7] | 183 | " for device %s", (long unsigned int)rtc->io_addr,
|
---|
[276e44a] | 184 | ddf_dev_get_name(rtc->dev));
|
---|
[0883de8] | 185 | return false;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | return true;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[b3db669] | 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;
|
---|
[c47f1b6] | 201 | size_t i;
|
---|
| 202 | hw_resource_t *res;
|
---|
| 203 | bool ioport = false;
|
---|
[6f445a67] | 204 | async_sess_t *parent_sess;
|
---|
[b3db669] | 205 |
|
---|
[6f445a67] | 206 | ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev));
|
---|
[b3db669] | 207 |
|
---|
[762083b] | 208 | rtc->boottime = 0;
|
---|
[9673fd3] | 209 | rtc->clients_connected = 0;
|
---|
[762083b] | 210 |
|
---|
[b3db669] | 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 |
|
---|
[6f445a67] | 216 | parent_sess = ddf_dev_parent_sess_create(rtc->dev, EXCHANGE_SERIALIZE);
|
---|
| 217 | if (!parent_sess) {
|
---|
[b3db669] | 218 | ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
|
---|
[6f445a67] | 219 | of device %s.", ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 220 | rc = ENOENT;
|
---|
| 221 | goto error;
|
---|
[b3db669] | 222 | }
|
---|
| 223 |
|
---|
| 224 | /* Get the HW resources */
|
---|
[6f445a67] | 225 | rc = hw_res_get_resource_list(parent_sess, &hw_resources);
|
---|
[b3db669] | 226 | if (rc != EOK) {
|
---|
| 227 | ddf_msg(LVL_ERROR, "Failed to get HW resources\
|
---|
[6f445a67] | 228 | for device %s", ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 229 | goto error;
|
---|
[b3db669] | 230 | }
|
---|
| 231 |
|
---|
[c47f1b6] | 232 | for (i = 0; i < hw_resources.count; ++i) {
|
---|
| 233 | res = &hw_resources.resources[i];
|
---|
| 234 |
|
---|
[ae827d0] | 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;
|
---|
[c47f1b6] | 241 | }
|
---|
[ae827d0] | 242 |
|
---|
[7ff35c7] | 243 | rtc->io_addr = (ioport8_t *) (long) res->res.io_range.address;
|
---|
[ae827d0] | 244 | ioport = true;
|
---|
| 245 | ddf_msg(LVL_NOTE, "Device %s was assigned I/O address "
|
---|
[276e44a] | 246 | "0x%lx", ddf_dev_get_name(rtc->dev),
|
---|
[7ff35c7] | 247 | (unsigned long int) rtc->io_addr);
|
---|
[ae827d0] | 248 | rc = EOK;
|
---|
| 249 | break;
|
---|
[c47f1b6] | 250 | }
|
---|
| 251 |
|
---|
[ae827d0] | 252 | if (rc != EOK)
|
---|
| 253 | goto error;
|
---|
| 254 |
|
---|
[c47f1b6] | 255 | if (!ioport) {
|
---|
| 256 | /* No I/O address assigned to this device */
|
---|
| 257 | ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
|
---|
[6f445a67] | 258 | ddf_dev_get_name(rtc->dev));
|
---|
[4b44de57] | 259 | rc = ENOENT;
|
---|
| 260 | goto error;
|
---|
[c47f1b6] | 261 | }
|
---|
| 262 |
|
---|
| 263 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 264 |
|
---|
[b3db669] | 265 | return EOK;
|
---|
[4b44de57] | 266 |
|
---|
| 267 | error:
|
---|
| 268 | rtc_dev_cleanup(rtc);
|
---|
| 269 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 270 |
|
---|
| 271 | return rc;
|
---|
[b3db669] | 272 | }
|
---|
| 273 |
|
---|
[3b79ba5] | 274 | /** Read a register from the CMOS memory
|
---|
| 275 | *
|
---|
[1170ea3c] | 276 | * @param rtc The rtc device
|
---|
[3b79ba5] | 277 | * @param reg The index of the register to read
|
---|
| 278 | *
|
---|
| 279 | * @return The value of the register
|
---|
| 280 | */
|
---|
| 281 | static int
|
---|
[db8d552] | 282 | rtc_register_read(rtc_t *rtc, int reg)
|
---|
[3b79ba5] | 283 | {
|
---|
[78ca12b] | 284 | pio_write_8(REG_SEL_PORT(rtc->port), reg);
|
---|
| 285 | return pio_read_8(REG_RW_PORT(rtc->port));
|
---|
[3b79ba5] | 286 | }
|
---|
| 287 |
|
---|
[1170ea3c] | 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 | {
|
---|
[78ca12b] | 297 | pio_write_8(REG_SEL_PORT(rtc->port), reg);
|
---|
| 298 | pio_write_8(REG_RW_PORT(rtc->port), data);
|
---|
[1170ea3c] | 299 | }
|
---|
| 300 |
|
---|
[f30ee571] | 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 | {
|
---|
[6a3808e] | 310 | return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
|
---|
[f30ee571] | 311 | }
|
---|
| 312 |
|
---|
[0b8a3e7] | 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 | */
|
---|
[4863bb52] | 320 | static int
|
---|
[8d2963d] | 321 | rtc_time_get(ddf_fun_t *fun, struct tm *t)
|
---|
[4863bb52] | 322 | {
|
---|
[db8d552] | 323 | bool bcd_mode;
|
---|
[95060d5b] | 324 | bool pm_mode = false;
|
---|
[6f445a67] | 325 | rtc_t *rtc = fun_rtc(fun);
|
---|
[f30ee571] | 326 |
|
---|
[60af6fc2] | 327 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 328 |
|
---|
| 329 | if (rtc->boottime != 0) {
|
---|
[ea5cc5b] | 330 | /* There is no need to read the current time from the
|
---|
| 331 | * device because it has already been cached.
|
---|
| 332 | */
|
---|
| 333 |
|
---|
[60af6fc2] | 334 | time_t cur_time = rtc->boottime + uptime_get();
|
---|
| 335 |
|
---|
| 336 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 337 |
|
---|
[664fc031] | 338 | return time_local2tm(cur_time, t);
|
---|
[ea5cc5b] | 339 | }
|
---|
| 340 |
|
---|
[0a586f4c] | 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 |
|
---|
[db8d552] | 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 */
|
---|
[c9abf50] | 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));
|
---|
[db8d552] | 365 |
|
---|
[95060d5b] | 366 | /* Check if the RTC is working in 12h mode */
|
---|
| 367 | bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
|
---|
[6a3808e] | 368 | RTC_B_24H);
|
---|
[95060d5b] | 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) {
|
---|
[f004318] | 373 | /* PM flag is active, it must be cleared */
|
---|
[95060d5b] | 374 | t->tm_hour &= ~0x80;
|
---|
| 375 | pm_mode = true;
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 |
|
---|
[db8d552] | 379 | /* Check if the RTC is working in BCD mode */
|
---|
[6a3808e] | 380 | bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
|
---|
[db8d552] | 381 |
|
---|
[fc7d28e] | 382 | if (bcd_mode) {
|
---|
[1170ea3c] | 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);
|
---|
[db8d552] | 389 | }
|
---|
[f30ee571] | 390 |
|
---|
[95060d5b] | 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 |
|
---|
[05ed9d7] | 400 | /* Count the months starting from 0, not from 1 */
|
---|
| 401 | t->tm_mon--;
|
---|
| 402 |
|
---|
[fc7d28e] | 403 | if (t->tm_year < 100) {
|
---|
[e5fbe06] | 404 | /* tm_year is the number of years since 1900 but the
|
---|
| 405 | * RTC epoch is 2000.
|
---|
[fc7d28e] | 406 | */
|
---|
| 407 | t->tm_year += 100;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[af7e3d3] | 410 | /* Try to normalize the content of the tm structure */
|
---|
[fa18523] | 411 | time_t r = mktime(t);
|
---|
[508fff8] | 412 | int result;
|
---|
[fa18523] | 413 |
|
---|
[508fff8] | 414 | if (r < 0)
|
---|
| 415 | result = EINVAL;
|
---|
| 416 | else {
|
---|
| 417 | rtc->boottime = r - uptime_get();
|
---|
| 418 | result = EOK;
|
---|
| 419 | }
|
---|
[60af6fc2] | 420 |
|
---|
| 421 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[ea5cc5b] | 422 |
|
---|
[508fff8] | 423 | return result;
|
---|
[4863bb52] | 424 | }
|
---|
| 425 |
|
---|
[0b8a3e7] | 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 | */
|
---|
[4863bb52] | 433 | static int
|
---|
[8d2963d] | 434 | rtc_time_set(ddf_fun_t *fun, struct tm *t)
|
---|
[4863bb52] | 435 | {
|
---|
[1170ea3c] | 436 | bool bcd_mode;
|
---|
[8fde078] | 437 | time_t norm_time;
|
---|
| 438 | time_t uptime;
|
---|
[a8a0d43] | 439 | int reg_b;
|
---|
| 440 | int reg_a;
|
---|
[e5fbe06] | 441 | int epoch;
|
---|
[6f445a67] | 442 | rtc_t *rtc = fun_rtc(fun);
|
---|
[1170ea3c] | 443 |
|
---|
[af7e3d3] | 444 | /* Try to normalize the content of the tm structure */
|
---|
[8fde078] | 445 | if ((norm_time = mktime(t)) < 0)
|
---|
[fa18523] | 446 | return EINVAL;
|
---|
| 447 |
|
---|
[8fde078] | 448 | uptime = uptime_get();
|
---|
| 449 | if (norm_time <= uptime) {
|
---|
| 450 | /* This is not acceptable */
|
---|
| 451 | return EINVAL;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
[e5fbe06] | 454 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 455 |
|
---|
[0a586f4c] | 456 | if (!is_battery_ok(rtc)) {
|
---|
| 457 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 458 | return EIO;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
[60af6fc2] | 461 | /* boottime must be recomputed */
|
---|
| 462 | rtc->boottime = 0;
|
---|
| 463 |
|
---|
[e5fbe06] | 464 | /* Detect the RTC epoch */
|
---|
| 465 | if (rtc_register_read(rtc, RTC_YEAR) < 100)
|
---|
| 466 | epoch = 2000;
|
---|
| 467 | else
|
---|
| 468 | epoch = 1900;
|
---|
| 469 |
|
---|
[fa18523] | 470 | if (epoch == 2000 && t->tm_year < 100) {
|
---|
| 471 | /* Can't set a year before the epoch */
|
---|
[e5fbe06] | 472 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[fa18523] | 473 | return EINVAL;
|
---|
[e5fbe06] | 474 | }
|
---|
[1170ea3c] | 475 |
|
---|
[a8a0d43] | 476 | t->tm_mon++; /* counts from 1, not from 0 */
|
---|
[1170ea3c] | 477 |
|
---|
[a8a0d43] | 478 | reg_b = rtc_register_read(rtc, RTC_STATUS_B);
|
---|
| 479 |
|
---|
[6a3808e] | 480 | if (!(reg_b & RTC_B_24H)) {
|
---|
[f004318] | 481 | /* Force 24h mode of operation */
|
---|
[6a3808e] | 482 | reg_b |= RTC_B_24H;
|
---|
[f004318] | 483 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
| 484 | }
|
---|
[f6af126] | 485 |
|
---|
[a44b58c] | 486 | if (epoch == 2000) {
|
---|
| 487 | /* The RTC epoch is year 2000 but the tm_year
|
---|
| 488 | * field counts years since 1900.
|
---|
| 489 | */
|
---|
[074324f1] | 490 | t->tm_year -= 100;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[1170ea3c] | 493 | /* Check if the rtc is working in bcd mode */
|
---|
[6a3808e] | 494 | bcd_mode = !(reg_b & RTC_B_BCD);
|
---|
[1170ea3c] | 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);
|
---|
[709476f4] | 501 | t->tm_mon = bin2bcd(t->tm_mon);
|
---|
[1170ea3c] | 502 | t->tm_year = bin2bcd(t->tm_year);
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[a8a0d43] | 505 | /* Inhibit updates */
|
---|
[6a3808e] | 506 | rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
|
---|
[a8a0d43] | 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);
|
---|
[6a3808e] | 518 | rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
|
---|
[a8a0d43] | 519 |
|
---|
| 520 | /* Enable updates */
|
---|
| 521 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
| 522 | rtc_register_write(rtc, RTC_STATUS_A, reg_a);
|
---|
[1170ea3c] | 523 |
|
---|
| 524 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 525 |
|
---|
[4863bb52] | 526 | return EOK;
|
---|
[1e19a15] | 527 | }
|
---|
| 528 |
|
---|
[efcfab9] | 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);
|
---|
[0a586f4c] | 540 |
|
---|
| 541 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 542 | const bool batt_ok = is_battery_ok(rtc);
|
---|
| 543 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[efcfab9] | 544 |
|
---|
| 545 | *status = batt_ok ? BATTERY_OK : BATTERY_LOW;
|
---|
| 546 |
|
---|
| 547 | return EOK;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
[0a586f4c] | 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 |
|
---|
[a31ca11f] | 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 | {
|
---|
[6b329749] | 572 | rtc_t *rtc;
|
---|
[4b44de57] | 573 | ddf_fun_t *fun = NULL;
|
---|
[b3db669] | 574 | int rc;
|
---|
[4b44de57] | 575 | bool need_cleanup = false;
|
---|
[6b329749] | 576 |
|
---|
| 577 | ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
|
---|
[6f445a67] | 578 | ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
|
---|
[6b329749] | 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 |
|
---|
[b3db669] | 587 | rc = rtc_dev_initialize(rtc);
|
---|
| 588 | if (rc != EOK)
|
---|
[4b44de57] | 589 | goto error;
|
---|
[b3db669] | 590 |
|
---|
[4b44de57] | 591 | need_cleanup = true;
|
---|
| 592 |
|
---|
| 593 | if (!rtc_pio_enable(rtc)) {
|
---|
| 594 | rc = EADDRNOTAVAIL;
|
---|
| 595 | goto error;
|
---|
| 596 | }
|
---|
[0883de8] | 597 |
|
---|
| 598 | fun = ddf_fun_create(dev, fun_exposed, "a");
|
---|
| 599 | if (!fun) {
|
---|
| 600 | ddf_msg(LVL_ERROR, "Failed creating function");
|
---|
[4b44de57] | 601 | rc = ENOENT;
|
---|
| 602 | goto error;
|
---|
[0883de8] | 603 | }
|
---|
| 604 |
|
---|
[6f445a67] | 605 | ddf_fun_set_ops(fun, &rtc_dev_ops);
|
---|
[0883de8] | 606 | rc = ddf_fun_bind(fun);
|
---|
| 607 | if (rc != EOK) {
|
---|
| 608 | ddf_msg(LVL_ERROR, "Failed binding function");
|
---|
[4b44de57] | 609 | goto error;
|
---|
[0883de8] | 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",
|
---|
[6f445a67] | 617 | ddf_dev_get_name(dev));
|
---|
[0883de8] | 618 |
|
---|
[b3db669] | 619 | return rc;
|
---|
[4b44de57] | 620 |
|
---|
| 621 | error:
|
---|
| 622 | if (fun)
|
---|
| 623 | ddf_fun_destroy(fun);
|
---|
| 624 | if (need_cleanup)
|
---|
| 625 | rtc_dev_cleanup(rtc);
|
---|
| 626 | return rc;
|
---|
[a31ca11f] | 627 | }
|
---|
| 628 |
|
---|
[bb8f69d] | 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 | {
|
---|
[6f445a67] | 638 | rtc_t *rtc = dev_rtc(dev);
|
---|
[bb8f69d] | 639 | int rc;
|
---|
| 640 |
|
---|
| 641 | fibril_mutex_lock(&rtc->mutex);
|
---|
[9673fd3] | 642 | if (rtc->clients_connected > 0) {
|
---|
| 643 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 644 | return EBUSY;
|
---|
| 645 | }
|
---|
[bb8f69d] | 646 |
|
---|
| 647 | rtc->removed = true;
|
---|
| 648 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 649 |
|
---|
[b801f2d] | 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 |
|
---|
[bb8f69d] | 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 |
|
---|
[923b2eba] | 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;
|
---|
[6f445a67] | 678 | rtc_t *rtc = fun_rtc(fun);
|
---|
[923b2eba] | 679 |
|
---|
| 680 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 681 |
|
---|
[43e660c] | 682 | if (rtc->removed)
|
---|
[bb8f69d] | 683 | rc = ENXIO;
|
---|
[9673fd3] | 684 | else {
|
---|
[923b2eba] | 685 | rc = EOK;
|
---|
[9673fd3] | 686 | rtc->clients_connected++;
|
---|
| 687 | }
|
---|
[923b2eba] | 688 |
|
---|
| 689 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 690 | return rc;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
[2a5171db] | 693 | /** Close the device
|
---|
| 694 | *
|
---|
| 695 | * @param fun The function node
|
---|
| 696 | */
|
---|
| 697 | static void
|
---|
| 698 | rtc_close(ddf_fun_t *fun)
|
---|
| 699 | {
|
---|
[9673fd3] | 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);
|
---|
[2a5171db] | 708 | }
|
---|
| 709 |
|
---|
[db8d552] | 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 | */
|
---|
[1170ea3c] | 716 | static unsigned
|
---|
| 717 | bcd2bin(unsigned bcd)
|
---|
[db8d552] | 718 | {
|
---|
| 719 | return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
|
---|
| 720 | }
|
---|
| 721 |
|
---|
[1170ea3c] | 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 |
|
---|
[8fde078] | 734 | static time_t
|
---|
| 735 | uptime_get(void)
|
---|
| 736 | {
|
---|
[83298e8] | 737 | struct timeval tv;
|
---|
[8fde078] | 738 |
|
---|
[83298e8] | 739 | getuptime(&tv);
|
---|
| 740 |
|
---|
| 741 | return tv.tv_sec;
|
---|
[8fde078] | 742 | }
|
---|
| 743 |
|
---|
[b801f2d] | 744 | static int
|
---|
| 745 | rtc_fun_online(ddf_fun_t *fun)
|
---|
| 746 | {
|
---|
| 747 | ddf_msg(LVL_DEBUG, "rtc_fun_online()");
|
---|
| 748 | return ddf_fun_online(fun);
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | static int
|
---|
| 752 | rtc_fun_offline(ddf_fun_t *fun)
|
---|
| 753 | {
|
---|
| 754 | ddf_msg(LVL_DEBUG, "rtc_fun_offline()");
|
---|
| 755 | return ddf_fun_offline(fun);
|
---|
| 756 | }
|
---|
| 757 |
|
---|
[1e19a15] | 758 | int
|
---|
| 759 | main(int argc, char **argv)
|
---|
| 760 | {
|
---|
| 761 | printf(NAME ": HelenOS RTC driver\n");
|
---|
| 762 | rtc_init();
|
---|
| 763 | return ddf_driver_main(&rtc_driver);
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | /**
|
---|
| 767 | * @}
|
---|
| 768 | */
|
---|