[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>
|
---|
[f30ee571] | 40 | #include <libarch/ddi.h>
|
---|
[1e19a15] | 41 | #include <stdio.h>
|
---|
| 42 | #include <ddf/driver.h>
|
---|
[a2e4889] | 43 | #include <ddf/log.h>
|
---|
[aeef318] | 44 | #include <ops/clock_dev.h>
|
---|
[6b329749] | 45 | #include <fibril_synch.h>
|
---|
[b3db669] | 46 | #include <device/hw_res.h>
|
---|
| 47 | #include <devman.h>
|
---|
[d8a4e79] | 48 | #include <ipc/clock_ctl.h>
|
---|
[1e19a15] | 49 |
|
---|
[f30ee571] | 50 | #include "cmos-regs.h"
|
---|
| 51 |
|
---|
[5ef13847] | 52 | #define NAME "cmos-rtc"
|
---|
[1e19a15] | 53 |
|
---|
[0883de8] | 54 | #define REG_COUNT 2
|
---|
| 55 |
|
---|
[923b2eba] | 56 | #define RTC_FROM_FNODE(fnode) ((rtc_t *) ((fnode)->dev->driver_data))
|
---|
[bb8f69d] | 57 | #define RTC_FROM_DEV(devnode) ((rtc_t *) ((devnode)->driver_data))
|
---|
[923b2eba] | 58 |
|
---|
[b3db669] | 59 | typedef struct rtc {
|
---|
| 60 | /** DDF device node */
|
---|
| 61 | ddf_dev_t *dev;
|
---|
| 62 | /** DDF function node */
|
---|
| 63 | ddf_fun_t *fun;
|
---|
| 64 | /** The fibril mutex for synchronizing the access to the device */
|
---|
| 65 | fibril_mutex_t mutex;
|
---|
[c47f1b6] | 66 | /** The base I/O address of the device registers */
|
---|
| 67 | uint32_t io_addr;
|
---|
[0883de8] | 68 | /** The I/O port used to access the CMOS registers */
|
---|
| 69 | ioport8_t *port;
|
---|
[923b2eba] | 70 | /** true if a client is connected to the device */
|
---|
| 71 | bool client_connected;
|
---|
[bb8f69d] | 72 | /** true if device is removed */
|
---|
| 73 | bool removed;
|
---|
[b3db669] | 74 | } rtc_t;
|
---|
| 75 |
|
---|
| 76 |
|
---|
[2a5171db] | 77 | static int rtc_time_get(ddf_fun_t *fun, struct tm *t);
|
---|
| 78 | static int rtc_time_set(ddf_fun_t *fun, struct tm *t);
|
---|
| 79 | static int rtc_dev_add(ddf_dev_t *dev);
|
---|
| 80 | static int rtc_dev_initialize(rtc_t *rtc);
|
---|
| 81 | static bool rtc_pio_enable(rtc_t *rtc);
|
---|
| 82 | static void rtc_dev_cleanup(rtc_t *rtc);
|
---|
| 83 | static int rtc_open(ddf_fun_t *fun);
|
---|
| 84 | static void rtc_close(ddf_fun_t *fun);
|
---|
[f30ee571] | 85 | static bool rtc_update_in_progress(rtc_t *rtc);
|
---|
[db8d552] | 86 | static int rtc_register_read(rtc_t *rtc, int reg);
|
---|
[1170ea3c] | 87 | static unsigned bcd2bin(unsigned bcd);
|
---|
| 88 | static unsigned bin2bcd(unsigned binary);
|
---|
[d8a4e79] | 89 | static void rtc_default_handler(ddf_fun_t *fun,
|
---|
| 90 | ipc_callid_t callid, ipc_call_t *call);
|
---|
[bb8f69d] | 91 | static int rtc_dev_remove(ddf_dev_t *dev);
|
---|
[1170ea3c] | 92 | static int rtc_tm_sanity_check(struct tm *t);
|
---|
[a8a0d43] | 93 | static void rtc_register_write(rtc_t *rtc, int reg, int data);
|
---|
[80d3f846] | 94 | static bool is_leap_year(int year);
|
---|
[923b2eba] | 95 |
|
---|
[80d3f846] | 96 | static int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
---|
[4863bb52] | 97 |
|
---|
[1e19a15] | 98 | static ddf_dev_ops_t rtc_dev_ops;
|
---|
| 99 |
|
---|
[a31ca11f] | 100 | /** The RTC device driver's standard operations */
|
---|
| 101 | static driver_ops_t rtc_ops = {
|
---|
| 102 | .dev_add = rtc_dev_add,
|
---|
[bb8f69d] | 103 | .dev_remove = rtc_dev_remove,
|
---|
[a31ca11f] | 104 | };
|
---|
| 105 |
|
---|
| 106 | /** The RTC device driver structure */
|
---|
[1e19a15] | 107 | static driver_t rtc_driver = {
|
---|
| 108 | .name = NAME,
|
---|
[a31ca11f] | 109 | .driver_ops = &rtc_ops,
|
---|
[1e19a15] | 110 | };
|
---|
| 111 |
|
---|
[a31ca11f] | 112 | /** Clock interface */
|
---|
[4863bb52] | 113 | static clock_dev_ops_t rtc_clock_dev_ops = {
|
---|
| 114 | .time_get = rtc_time_get,
|
---|
| 115 | .time_set = rtc_time_set,
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | /** Initialize the RTC driver */
|
---|
[1e19a15] | 119 | static void
|
---|
| 120 | rtc_init(void)
|
---|
| 121 | {
|
---|
| 122 | ddf_log_init(NAME, LVL_ERROR);
|
---|
| 123 |
|
---|
[923b2eba] | 124 | rtc_dev_ops.open = rtc_open;
|
---|
[2a5171db] | 125 | rtc_dev_ops.close = rtc_close;
|
---|
[4863bb52] | 126 |
|
---|
| 127 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
---|
[d8a4e79] | 128 | rtc_dev_ops.default_handler = &rtc_default_handler;
|
---|
[4863bb52] | 129 | }
|
---|
| 130 |
|
---|
[4b44de57] | 131 | /** Clean up the RTC soft state
|
---|
| 132 | *
|
---|
| 133 | * @param rtc The RTC device
|
---|
| 134 | */
|
---|
| 135 | static void
|
---|
| 136 | rtc_dev_cleanup(rtc_t *rtc)
|
---|
| 137 | {
|
---|
| 138 | if (rtc->dev->parent_sess) {
|
---|
| 139 | async_hangup(rtc->dev->parent_sess);
|
---|
| 140 | rtc->dev->parent_sess = NULL;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[0883de8] | 144 | /** Enable the I/O ports of the device
|
---|
| 145 | *
|
---|
| 146 | * @param rtc The real time clock device
|
---|
| 147 | *
|
---|
| 148 | * @return true in case of success, false otherwise
|
---|
| 149 | */
|
---|
| 150 | static bool
|
---|
| 151 | rtc_pio_enable(rtc_t *rtc)
|
---|
| 152 | {
|
---|
| 153 | if (pio_enable((void *)(uintptr_t) rtc->io_addr, REG_COUNT,
|
---|
| 154 | (void **) &rtc->port)) {
|
---|
| 155 |
|
---|
| 156 | ddf_msg(LVL_ERROR, "Cannot map the port %#" PRIx32
|
---|
| 157 | " for device %s", rtc->io_addr, rtc->dev->name);
|
---|
| 158 | return false;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | return true;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[b3db669] | 164 | /** Initialize the RTC device
|
---|
| 165 | *
|
---|
| 166 | * @param rtc Pointer to the RTC device
|
---|
| 167 | *
|
---|
| 168 | * @return EOK on success or a negative error code
|
---|
| 169 | */
|
---|
| 170 | static int
|
---|
| 171 | rtc_dev_initialize(rtc_t *rtc)
|
---|
| 172 | {
|
---|
| 173 | int rc;
|
---|
[c47f1b6] | 174 | size_t i;
|
---|
| 175 | hw_resource_t *res;
|
---|
| 176 | bool ioport = false;
|
---|
[b3db669] | 177 |
|
---|
| 178 | ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", rtc->dev->name);
|
---|
| 179 |
|
---|
| 180 | hw_resource_list_t hw_resources;
|
---|
| 181 | memset(&hw_resources, 0, sizeof(hw_resource_list_t));
|
---|
| 182 |
|
---|
| 183 | /* Connect to the parent's driver */
|
---|
| 184 |
|
---|
| 185 | rtc->dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
|
---|
| 186 | rtc->dev->handle, IPC_FLAG_BLOCKING);
|
---|
| 187 | if (!rtc->dev->parent_sess) {
|
---|
| 188 | ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
|
---|
| 189 | of device %s.", rtc->dev->name);
|
---|
[4b44de57] | 190 | rc = ENOENT;
|
---|
| 191 | goto error;
|
---|
[b3db669] | 192 | }
|
---|
| 193 |
|
---|
| 194 | /* Get the HW resources */
|
---|
| 195 | rc = hw_res_get_resource_list(rtc->dev->parent_sess, &hw_resources);
|
---|
| 196 | if (rc != EOK) {
|
---|
| 197 | ddf_msg(LVL_ERROR, "Failed to get HW resources\
|
---|
| 198 | for device %s", rtc->dev->name);
|
---|
[4b44de57] | 199 | goto error;
|
---|
[b3db669] | 200 | }
|
---|
| 201 |
|
---|
[c47f1b6] | 202 | for (i = 0; i < hw_resources.count; ++i) {
|
---|
| 203 | res = &hw_resources.resources[i];
|
---|
| 204 |
|
---|
| 205 | if (res->type == IO_RANGE) {
|
---|
[28ca043f] | 206 | if (res->res.io_range.size < REG_COUNT) {
|
---|
| 207 | ddf_msg(LVL_ERROR, "I/O range assigned to \
|
---|
| 208 | device %s is too small", rtc->dev->name);
|
---|
| 209 | rc = ELIMIT;
|
---|
| 210 | goto error;
|
---|
| 211 | }
|
---|
[c47f1b6] | 212 | rtc->io_addr = res->res.io_range.address;
|
---|
| 213 | ioport = true;
|
---|
| 214 | ddf_msg(LVL_NOTE, "Device %s was assigned I/O address \
|
---|
| 215 | 0x%x", rtc->dev->name, rtc->io_addr);
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | if (!ioport) {
|
---|
| 220 | /* No I/O address assigned to this device */
|
---|
| 221 | ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
|
---|
| 222 | rtc->dev->name);
|
---|
[4b44de57] | 223 | rc = ENOENT;
|
---|
| 224 | goto error;
|
---|
[c47f1b6] | 225 | }
|
---|
| 226 |
|
---|
| 227 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 228 |
|
---|
[b3db669] | 229 | return EOK;
|
---|
[4b44de57] | 230 |
|
---|
| 231 | error:
|
---|
| 232 | rtc_dev_cleanup(rtc);
|
---|
| 233 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 234 |
|
---|
| 235 | return rc;
|
---|
[b3db669] | 236 | }
|
---|
| 237 |
|
---|
[3b79ba5] | 238 | /** Read a register from the CMOS memory
|
---|
| 239 | *
|
---|
[1170ea3c] | 240 | * @param rtc The rtc device
|
---|
[3b79ba5] | 241 | * @param reg The index of the register to read
|
---|
| 242 | *
|
---|
| 243 | * @return The value of the register
|
---|
| 244 | */
|
---|
| 245 | static int
|
---|
[db8d552] | 246 | rtc_register_read(rtc_t *rtc, int reg)
|
---|
[3b79ba5] | 247 | {
|
---|
[db8d552] | 248 | pio_write_8(rtc->port, reg);
|
---|
| 249 | return pio_read_8(rtc->port + 1);
|
---|
[3b79ba5] | 250 | }
|
---|
| 251 |
|
---|
[1170ea3c] | 252 | /** Write a register to the CMOS memory
|
---|
| 253 | *
|
---|
| 254 | * @param rtc The rtc device
|
---|
| 255 | * @param reg The index of the register to write
|
---|
| 256 | * @param data The data to write
|
---|
| 257 | */
|
---|
| 258 | static void
|
---|
| 259 | rtc_register_write(rtc_t *rtc, int reg, int data)
|
---|
| 260 | {
|
---|
| 261 | pio_write_8(rtc->port, reg);
|
---|
| 262 | pio_write_8(rtc->port + 1, data);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[f30ee571] | 265 | /** Check if an update is in progress
|
---|
| 266 | *
|
---|
| 267 | * @param rtc The rtc device
|
---|
| 268 | *
|
---|
| 269 | * @return true if an update is in progress, false otherwise
|
---|
| 270 | */
|
---|
| 271 | static bool
|
---|
| 272 | rtc_update_in_progress(rtc_t *rtc)
|
---|
| 273 | {
|
---|
[cf72943] | 274 | return rtc_register_read(rtc, RTC_STATUS_A) & RTC_MASK_UPDATE;
|
---|
[f30ee571] | 275 | }
|
---|
| 276 |
|
---|
[0b8a3e7] | 277 | /** Read the current time from the CMOS
|
---|
| 278 | *
|
---|
| 279 | * @param fun The RTC function
|
---|
| 280 | * @param t Pointer to the time variable
|
---|
| 281 | *
|
---|
| 282 | * @return EOK on success or a negative error code
|
---|
| 283 | */
|
---|
[4863bb52] | 284 | static int
|
---|
[8d2963d] | 285 | rtc_time_get(ddf_fun_t *fun, struct tm *t)
|
---|
[4863bb52] | 286 | {
|
---|
[db8d552] | 287 | bool bcd_mode;
|
---|
[95060d5b] | 288 | bool pm_mode = false;
|
---|
[f30ee571] | 289 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
| 290 |
|
---|
| 291 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 292 |
|
---|
[db8d552] | 293 | /* now read the registers */
|
---|
| 294 | do {
|
---|
| 295 | /* Suspend until the update process has finished */
|
---|
| 296 | while (rtc_update_in_progress(rtc));
|
---|
| 297 |
|
---|
| 298 | t->tm_sec = rtc_register_read(rtc, RTC_SEC);
|
---|
| 299 | t->tm_min = rtc_register_read(rtc, RTC_MIN);
|
---|
| 300 | t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
|
---|
| 301 | t->tm_mday = rtc_register_read(rtc, RTC_DAY);
|
---|
| 302 | t->tm_mon = rtc_register_read(rtc, RTC_MON);
|
---|
| 303 | t->tm_year = rtc_register_read(rtc, RTC_YEAR);
|
---|
| 304 |
|
---|
| 305 | /* Now check if it is stable */
|
---|
[2a4c22d] | 306 | } while( t->tm_sec != rtc_register_read(rtc, RTC_SEC) ||
|
---|
| 307 | t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
|
---|
| 308 | t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
|
---|
| 309 | t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
|
---|
| 310 | t->tm_year != rtc_register_read(rtc, RTC_YEAR));
|
---|
[db8d552] | 311 |
|
---|
[95060d5b] | 312 | /* Check if the RTC is working in 12h mode */
|
---|
| 313 | bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
|
---|
| 314 | RTC_MASK_24H);
|
---|
| 315 |
|
---|
| 316 | if (_12h_mode) {
|
---|
| 317 | /* The RTC is working in 12h mode, check if it is AM or PM */
|
---|
| 318 | if (t->tm_hour & 0x80) {
|
---|
| 319 | /* PM flag is active, it must to be cleared
|
---|
| 320 | * or the BCD conversion will fail.
|
---|
| 321 | */
|
---|
| 322 | t->tm_hour &= ~0x80;
|
---|
| 323 | pm_mode = true;
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[db8d552] | 327 | /* Check if the RTC is working in BCD mode */
|
---|
[fc7d28e] | 328 | bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_MASK_BCD);
|
---|
[db8d552] | 329 |
|
---|
[fc7d28e] | 330 | if (bcd_mode) {
|
---|
[1170ea3c] | 331 | t->tm_sec = bcd2bin(t->tm_sec);
|
---|
| 332 | t->tm_min = bcd2bin(t->tm_min);
|
---|
| 333 | t->tm_hour = bcd2bin(t->tm_hour);
|
---|
| 334 | t->tm_mday = bcd2bin(t->tm_mday);
|
---|
| 335 | t->tm_mon = bcd2bin(t->tm_mon);
|
---|
| 336 | t->tm_year = bcd2bin(t->tm_year);
|
---|
[db8d552] | 337 | }
|
---|
[f30ee571] | 338 |
|
---|
[95060d5b] | 339 | if (_12h_mode) {
|
---|
| 340 | /* Convert to 24h mode */
|
---|
| 341 | if (pm_mode) {
|
---|
| 342 | if (t->tm_hour < 12)
|
---|
| 343 | t->tm_hour += 12;
|
---|
| 344 | } else if (t->tm_hour == 12)
|
---|
| 345 | t->tm_hour = 0;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
[05ed9d7] | 348 | /* Count the months starting from 0, not from 1 */
|
---|
| 349 | t->tm_mon--;
|
---|
| 350 |
|
---|
[fc7d28e] | 351 | if (t->tm_year < 100) {
|
---|
| 352 | /* tm_year is the number of years since 1900, it is not
|
---|
| 353 | * possible it is < 100.
|
---|
| 354 | */
|
---|
| 355 | t->tm_year += 100;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[f30ee571] | 358 | fibril_mutex_unlock(&rtc->mutex);
|
---|
[4863bb52] | 359 | return EOK;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[0b8a3e7] | 362 | /** Set the time in the RTC
|
---|
| 363 | *
|
---|
| 364 | * @param fun The RTC function
|
---|
| 365 | * @param t The time value to set
|
---|
| 366 | *
|
---|
| 367 | * @return EOK or a negative error code
|
---|
| 368 | */
|
---|
[4863bb52] | 369 | static int
|
---|
[8d2963d] | 370 | rtc_time_set(ddf_fun_t *fun, struct tm *t)
|
---|
[4863bb52] | 371 | {
|
---|
[a8a0d43] | 372 | int rc;
|
---|
[1170ea3c] | 373 | bool bcd_mode;
|
---|
[a8a0d43] | 374 | int reg_b;
|
---|
| 375 | int reg_a;
|
---|
[1170ea3c] | 376 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
| 377 |
|
---|
| 378 | rc = rtc_tm_sanity_check(t);
|
---|
| 379 | if (rc != EOK)
|
---|
| 380 | return rc;
|
---|
| 381 |
|
---|
[a8a0d43] | 382 | t->tm_mon++; /* counts from 1, not from 0 */
|
---|
[1170ea3c] | 383 |
|
---|
| 384 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 385 |
|
---|
[a8a0d43] | 386 | reg_b = rtc_register_read(rtc, RTC_STATUS_B);
|
---|
| 387 |
|
---|
[f6af126] | 388 | /* Force 24h mode of operation */
|
---|
| 389 | rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_MASK_24H);
|
---|
| 390 |
|
---|
[074324f1] | 391 | if (rtc_register_read(rtc, RTC_YEAR) < 100) {
|
---|
| 392 | /* The RTC epoch is year 2000 */
|
---|
| 393 | t->tm_year -= 100;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
[1170ea3c] | 396 | /* Check if the rtc is working in bcd mode */
|
---|
[a8a0d43] | 397 | bcd_mode = !(reg_b & RTC_MASK_BCD);
|
---|
[1170ea3c] | 398 | if (bcd_mode) {
|
---|
| 399 | /* Convert the tm struct fields in BCD mode */
|
---|
| 400 | t->tm_sec = bin2bcd(t->tm_sec);
|
---|
| 401 | t->tm_min = bin2bcd(t->tm_min);
|
---|
| 402 | t->tm_hour = bin2bcd(t->tm_hour);
|
---|
| 403 | t->tm_mday = bin2bcd(t->tm_mday);
|
---|
[709476f4] | 404 | t->tm_mon = bin2bcd(t->tm_mon);
|
---|
[1170ea3c] | 405 | t->tm_year = bin2bcd(t->tm_year);
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[a8a0d43] | 408 | /* Inhibit updates */
|
---|
| 409 | rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_MASK_INH);
|
---|
| 410 |
|
---|
| 411 | /* Write current time to RTC */
|
---|
| 412 | rtc_register_write(rtc, RTC_SEC, t->tm_sec);
|
---|
| 413 | rtc_register_write(rtc, RTC_MIN, t->tm_min);
|
---|
| 414 | rtc_register_write(rtc, RTC_HOUR, t->tm_hour);
|
---|
| 415 | rtc_register_write(rtc, RTC_DAY, t->tm_mday);
|
---|
| 416 | rtc_register_write(rtc, RTC_MON, t->tm_mon);
|
---|
| 417 | rtc_register_write(rtc, RTC_YEAR, t->tm_year);
|
---|
| 418 |
|
---|
| 419 | /* Stop the clock */
|
---|
| 420 | reg_a = rtc_register_read(rtc, RTC_STATUS_A);
|
---|
| 421 | rtc_register_write(rtc, RTC_STATUS_A, RTC_MASK_CLK_STOP | reg_a);
|
---|
| 422 |
|
---|
| 423 | /* Enable updates */
|
---|
| 424 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
| 425 | rtc_register_write(rtc, RTC_STATUS_A, reg_a);
|
---|
[1170ea3c] | 426 |
|
---|
| 427 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 428 |
|
---|
| 429 | return rc;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | /** Check if the tm structure contains valid values
|
---|
| 433 | *
|
---|
| 434 | * @param t The tm structure to check
|
---|
| 435 | *
|
---|
| 436 | * @return EOK on success or EINVAL
|
---|
| 437 | */
|
---|
| 438 | static int
|
---|
| 439 | rtc_tm_sanity_check(struct tm *t)
|
---|
| 440 | {
|
---|
[80d3f846] | 441 | int ndays;
|
---|
| 442 |
|
---|
[1170ea3c] | 443 | if (t->tm_sec < 0 || t->tm_sec > 59)
|
---|
| 444 | return EINVAL;
|
---|
| 445 | else if (t->tm_min < 0 || t->tm_min > 59)
|
---|
| 446 | return EINVAL;
|
---|
| 447 | else if (t->tm_hour < 0 || t->tm_hour > 23)
|
---|
| 448 | return EINVAL;
|
---|
| 449 | else if (t->tm_mday < 1 || t->tm_mday > 31)
|
---|
| 450 | return EINVAL;
|
---|
| 451 | else if (t->tm_mon < 0 || t->tm_mon > 11)
|
---|
| 452 | return EINVAL;
|
---|
| 453 | else if (t->tm_year < 0)
|
---|
| 454 | return EINVAL;
|
---|
| 455 |
|
---|
[80d3f846] | 456 | if (t->tm_mon == 1/* FEB */ && is_leap_year(t->tm_year))
|
---|
| 457 | ndays = 29;
|
---|
| 458 | else
|
---|
| 459 | ndays = days_month[t->tm_mon];
|
---|
| 460 |
|
---|
| 461 | if (t->tm_mday > ndays)
|
---|
| 462 | return EINVAL;
|
---|
| 463 |
|
---|
[4863bb52] | 464 | return EOK;
|
---|
[1e19a15] | 465 | }
|
---|
| 466 |
|
---|
[80d3f846] | 467 | /** Check if a year is a leap year
|
---|
| 468 | *
|
---|
| 469 | * @param year The year to check
|
---|
| 470 | *
|
---|
| 471 | * @return true if it is a leap year, false otherwise
|
---|
| 472 | */
|
---|
| 473 | static bool
|
---|
| 474 | is_leap_year(int year)
|
---|
| 475 | {
|
---|
| 476 | bool r = false;
|
---|
| 477 |
|
---|
| 478 | if (year % 4 == 0) {
|
---|
| 479 | if (year % 100 == 0)
|
---|
| 480 | r = year % 400 == 0;
|
---|
| 481 | else
|
---|
| 482 | r = true;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | return r;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
[a31ca11f] | 488 | /** The dev_add callback of the rtc driver
|
---|
| 489 | *
|
---|
| 490 | * @param dev The RTC device
|
---|
| 491 | *
|
---|
| 492 | * @return EOK on success or a negative error code
|
---|
| 493 | */
|
---|
| 494 | static int
|
---|
| 495 | rtc_dev_add(ddf_dev_t *dev)
|
---|
| 496 | {
|
---|
[6b329749] | 497 | rtc_t *rtc;
|
---|
[4b44de57] | 498 | ddf_fun_t *fun = NULL;
|
---|
[b3db669] | 499 | int rc;
|
---|
[4b44de57] | 500 | bool need_cleanup = false;
|
---|
[6b329749] | 501 |
|
---|
| 502 | ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
|
---|
| 503 | dev->name, (int) dev->handle);
|
---|
| 504 |
|
---|
| 505 | rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
|
---|
| 506 | if (!rtc)
|
---|
| 507 | return ENOMEM;
|
---|
| 508 |
|
---|
| 509 | rtc->dev = dev;
|
---|
| 510 | fibril_mutex_initialize(&rtc->mutex);
|
---|
| 511 |
|
---|
[b3db669] | 512 | rc = rtc_dev_initialize(rtc);
|
---|
| 513 | if (rc != EOK)
|
---|
[4b44de57] | 514 | goto error;
|
---|
[b3db669] | 515 |
|
---|
[4b44de57] | 516 | need_cleanup = true;
|
---|
| 517 |
|
---|
| 518 | if (!rtc_pio_enable(rtc)) {
|
---|
| 519 | rc = EADDRNOTAVAIL;
|
---|
| 520 | goto error;
|
---|
| 521 | }
|
---|
[0883de8] | 522 |
|
---|
| 523 | fun = ddf_fun_create(dev, fun_exposed, "a");
|
---|
| 524 | if (!fun) {
|
---|
| 525 | ddf_msg(LVL_ERROR, "Failed creating function");
|
---|
[4b44de57] | 526 | rc = ENOENT;
|
---|
| 527 | goto error;
|
---|
[0883de8] | 528 | }
|
---|
| 529 |
|
---|
| 530 | fun->ops = &rtc_dev_ops;
|
---|
| 531 | rc = ddf_fun_bind(fun);
|
---|
| 532 | if (rc != EOK) {
|
---|
| 533 | ddf_msg(LVL_ERROR, "Failed binding function");
|
---|
[4b44de57] | 534 | goto error;
|
---|
[0883de8] | 535 | }
|
---|
| 536 |
|
---|
| 537 | rtc->fun = fun;
|
---|
| 538 |
|
---|
| 539 | ddf_fun_add_to_category(fun, "clock");
|
---|
| 540 |
|
---|
[923b2eba] | 541 | rtc->client_connected = false;
|
---|
| 542 |
|
---|
[0883de8] | 543 | ddf_msg(LVL_NOTE, "Device %s successfully initialized",
|
---|
| 544 | dev->name);
|
---|
| 545 |
|
---|
[b3db669] | 546 | return rc;
|
---|
[4b44de57] | 547 |
|
---|
| 548 | error:
|
---|
| 549 | if (fun)
|
---|
| 550 | ddf_fun_destroy(fun);
|
---|
| 551 | if (need_cleanup)
|
---|
| 552 | rtc_dev_cleanup(rtc);
|
---|
| 553 | return rc;
|
---|
[a31ca11f] | 554 | }
|
---|
| 555 |
|
---|
[bb8f69d] | 556 | /** The dev_remove callback for the rtc driver
|
---|
| 557 | *
|
---|
| 558 | * @param dev The RTC device
|
---|
| 559 | *
|
---|
| 560 | * @return EOK on success or a negative error code
|
---|
| 561 | */
|
---|
| 562 | static int
|
---|
| 563 | rtc_dev_remove(ddf_dev_t *dev)
|
---|
| 564 | {
|
---|
| 565 | rtc_t *rtc = RTC_FROM_DEV(dev);
|
---|
| 566 | int rc;
|
---|
| 567 |
|
---|
| 568 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 569 | if (rtc->client_connected) {
|
---|
| 570 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 571 | return EBUSY;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | rtc->removed = true;
|
---|
| 575 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 576 |
|
---|
| 577 | rc = ddf_fun_unbind(rtc->fun);
|
---|
| 578 | if (rc != EOK) {
|
---|
| 579 | ddf_msg(LVL_ERROR, "Failed to unbind function");
|
---|
| 580 | return rc;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | ddf_fun_destroy(rtc->fun);
|
---|
| 584 | rtc_dev_cleanup(rtc);
|
---|
| 585 |
|
---|
| 586 | return rc;
|
---|
| 587 | }
|
---|
| 588 |
|
---|
[d8a4e79] | 589 | /** Default handler for client requests not handled
|
---|
| 590 | * by the standard interface
|
---|
| 591 | */
|
---|
| 592 | static void
|
---|
| 593 | rtc_default_handler(ddf_fun_t *fun, ipc_callid_t callid, ipc_call_t *call)
|
---|
| 594 | {
|
---|
| 595 | sysarg_t method = IPC_GET_IMETHOD(*call);
|
---|
| 596 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
| 597 | bool batt_ok;
|
---|
| 598 |
|
---|
| 599 | switch (method) {
|
---|
| 600 | case CLOCK_GET_BATTERY_STATUS:
|
---|
[f61a326] | 601 | batt_ok = rtc_register_read(rtc, RTC_STATUS_D) &
|
---|
| 602 | RTC_BATTERY_OK;
|
---|
[d8a4e79] | 603 | async_answer_1(callid, EOK, batt_ok);
|
---|
| 604 | break;
|
---|
| 605 | default:
|
---|
| 606 | async_answer_0(callid, ENOTSUP);
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[923b2eba] | 610 | /** Open the device
|
---|
| 611 | *
|
---|
| 612 | * @param fun The function node
|
---|
| 613 | *
|
---|
| 614 | * @return EOK on success or a negative error code
|
---|
| 615 | */
|
---|
| 616 | static int
|
---|
| 617 | rtc_open(ddf_fun_t *fun)
|
---|
| 618 | {
|
---|
| 619 | int rc;
|
---|
| 620 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
| 621 |
|
---|
| 622 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 623 |
|
---|
| 624 | if (rtc->client_connected)
|
---|
[bb8f69d] | 625 | rc = ELIMIT;
|
---|
| 626 | else if (rtc->removed)
|
---|
| 627 | rc = ENXIO;
|
---|
[923b2eba] | 628 | else {
|
---|
| 629 | rc = EOK;
|
---|
| 630 | rtc->client_connected = true;
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 634 | return rc;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
[2a5171db] | 637 | /** Close the device
|
---|
| 638 | *
|
---|
| 639 | * @param fun The function node
|
---|
| 640 | */
|
---|
| 641 | static void
|
---|
| 642 | rtc_close(ddf_fun_t *fun)
|
---|
| 643 | {
|
---|
| 644 | rtc_t *rtc = RTC_FROM_FNODE(fun);
|
---|
| 645 |
|
---|
| 646 | fibril_mutex_lock(&rtc->mutex);
|
---|
| 647 |
|
---|
| 648 | assert(rtc->client_connected);
|
---|
| 649 | rtc->client_connected = false;
|
---|
| 650 |
|
---|
| 651 | fibril_mutex_unlock(&rtc->mutex);
|
---|
| 652 | }
|
---|
| 653 |
|
---|
[db8d552] | 654 | /** Convert from BCD mode to binary mode
|
---|
| 655 | *
|
---|
| 656 | * @param bcd The number in BCD format to convert
|
---|
| 657 | *
|
---|
| 658 | * @return The converted value
|
---|
| 659 | */
|
---|
[1170ea3c] | 660 | static unsigned
|
---|
| 661 | bcd2bin(unsigned bcd)
|
---|
[db8d552] | 662 | {
|
---|
| 663 | return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
|
---|
| 664 | }
|
---|
| 665 |
|
---|
[1170ea3c] | 666 | /** Convert from binary mode to BCD mode
|
---|
| 667 | *
|
---|
| 668 | * @param bcd The number in binary mode to convert
|
---|
| 669 | *
|
---|
| 670 | * @return The converted value
|
---|
| 671 | */
|
---|
| 672 | static unsigned
|
---|
| 673 | bin2bcd(unsigned binary)
|
---|
| 674 | {
|
---|
| 675 | return ((binary / 10) << 4) + (binary % 10);
|
---|
| 676 | }
|
---|
| 677 |
|
---|
[1e19a15] | 678 | int
|
---|
| 679 | main(int argc, char **argv)
|
---|
| 680 | {
|
---|
| 681 | printf(NAME ": HelenOS RTC driver\n");
|
---|
| 682 | rtc_init();
|
---|
| 683 | return ddf_driver_main(&rtc_driver);
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | /**
|
---|
| 687 | * @}
|
---|
| 688 | */
|
---|