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