[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>
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 | #include <ddf/driver.h>
|
---|
[a2e4889] | 42 | #include <ddf/log.h>
|
---|
[4863bb52] | 43 | #include <ops/clock.h>
|
---|
[6b329749] | 44 | #include <fibril_synch.h>
|
---|
[b3db669] | 45 | #include <device/hw_res.h>
|
---|
| 46 | #include <devman.h>
|
---|
[1e19a15] | 47 |
|
---|
[5ef13847] | 48 | #define NAME "cmos-rtc"
|
---|
[1e19a15] | 49 |
|
---|
[b3db669] | 50 | typedef struct rtc {
|
---|
| 51 | /** DDF device node */
|
---|
| 52 | ddf_dev_t *dev;
|
---|
| 53 | /** DDF function node */
|
---|
| 54 | ddf_fun_t *fun;
|
---|
| 55 | /** The fibril mutex for synchronizing the access to the device */
|
---|
| 56 | fibril_mutex_t mutex;
|
---|
[c47f1b6] | 57 | /** The base I/O address of the device registers */
|
---|
| 58 | uint32_t io_addr;
|
---|
[b3db669] | 59 | } rtc_t;
|
---|
| 60 |
|
---|
| 61 |
|
---|
[4863bb52] | 62 | static int
|
---|
| 63 | rtc_time_get(ddf_fun_t *fun, time_t *t);
|
---|
| 64 |
|
---|
| 65 | static int
|
---|
| 66 | rtc_time_set(ddf_fun_t *fun, time_t t);
|
---|
| 67 |
|
---|
[a31ca11f] | 68 | static int
|
---|
| 69 | rtc_dev_add(ddf_dev_t *dev);
|
---|
| 70 |
|
---|
[b3db669] | 71 | static int
|
---|
| 72 | rtc_dev_initialize(rtc_t *rtc);
|
---|
| 73 |
|
---|
[4863bb52] | 74 |
|
---|
[1e19a15] | 75 | static ddf_dev_ops_t rtc_dev_ops;
|
---|
| 76 |
|
---|
[a31ca11f] | 77 | /** The RTC device driver's standard operations */
|
---|
| 78 | static driver_ops_t rtc_ops = {
|
---|
| 79 | .dev_add = rtc_dev_add,
|
---|
[c47f1b6] | 80 | .dev_remove = NULL, /* XXX */
|
---|
[a31ca11f] | 81 | };
|
---|
| 82 |
|
---|
| 83 | /** The RTC device driver structure */
|
---|
[1e19a15] | 84 | static driver_t rtc_driver = {
|
---|
| 85 | .name = NAME,
|
---|
[a31ca11f] | 86 | .driver_ops = &rtc_ops,
|
---|
[1e19a15] | 87 | };
|
---|
| 88 |
|
---|
[a31ca11f] | 89 | /** Clock interface */
|
---|
[4863bb52] | 90 | static clock_dev_ops_t rtc_clock_dev_ops = {
|
---|
| 91 | .time_get = rtc_time_get,
|
---|
| 92 | .time_set = rtc_time_set,
|
---|
| 93 | };
|
---|
| 94 |
|
---|
| 95 | /** Initialize the RTC driver */
|
---|
[1e19a15] | 96 | static void
|
---|
| 97 | rtc_init(void)
|
---|
| 98 | {
|
---|
| 99 | ddf_log_init(NAME, LVL_ERROR);
|
---|
| 100 |
|
---|
[c47f1b6] | 101 | rtc_dev_ops.open = NULL; /* XXX */
|
---|
| 102 | rtc_dev_ops.close = NULL; /* XXX */
|
---|
[4863bb52] | 103 |
|
---|
| 104 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
---|
[c47f1b6] | 105 | rtc_dev_ops.default_handler = NULL; /* XXX */
|
---|
[4863bb52] | 106 | }
|
---|
| 107 |
|
---|
[b3db669] | 108 | /** Initialize the RTC device
|
---|
| 109 | *
|
---|
| 110 | * @param rtc Pointer to the RTC device
|
---|
| 111 | *
|
---|
| 112 | * @return EOK on success or a negative error code
|
---|
| 113 | */
|
---|
| 114 | static int
|
---|
| 115 | rtc_dev_initialize(rtc_t *rtc)
|
---|
| 116 | {
|
---|
[c47f1b6] | 117 | /* XXX Do cleanup in case of failure */
|
---|
[b3db669] | 118 | int rc;
|
---|
[c47f1b6] | 119 | size_t i;
|
---|
| 120 | hw_resource_t *res;
|
---|
| 121 | bool ioport = false;
|
---|
[b3db669] | 122 |
|
---|
| 123 | ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", rtc->dev->name);
|
---|
| 124 |
|
---|
| 125 | hw_resource_list_t hw_resources;
|
---|
| 126 | memset(&hw_resources, 0, sizeof(hw_resource_list_t));
|
---|
| 127 |
|
---|
| 128 | /* Connect to the parent's driver */
|
---|
| 129 |
|
---|
| 130 | rtc->dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
|
---|
| 131 | rtc->dev->handle, IPC_FLAG_BLOCKING);
|
---|
| 132 | if (!rtc->dev->parent_sess) {
|
---|
| 133 | ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
|
---|
| 134 | of device %s.", rtc->dev->name);
|
---|
| 135 | return ENOENT;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /* Get the HW resources */
|
---|
| 139 | rc = hw_res_get_resource_list(rtc->dev->parent_sess, &hw_resources);
|
---|
| 140 | if (rc != EOK) {
|
---|
| 141 | ddf_msg(LVL_ERROR, "Failed to get HW resources\
|
---|
| 142 | for device %s", rtc->dev->name);
|
---|
| 143 | return rc;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[c47f1b6] | 146 | for (i = 0; i < hw_resources.count; ++i) {
|
---|
| 147 | res = &hw_resources.resources[i];
|
---|
| 148 |
|
---|
| 149 | if (res->type == IO_RANGE) {
|
---|
| 150 | rtc->io_addr = res->res.io_range.address;
|
---|
| 151 | ioport = true;
|
---|
| 152 | ddf_msg(LVL_NOTE, "Device %s was assigned I/O address \
|
---|
| 153 | 0x%x", rtc->dev->name, rtc->io_addr);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | if (!ioport) {
|
---|
| 158 | /* No I/O address assigned to this device */
|
---|
| 159 | ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
|
---|
| 160 | rtc->dev->name);
|
---|
| 161 | return ENOENT;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 165 |
|
---|
[b3db669] | 166 | return EOK;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[0b8a3e7] | 169 | /** Read the current time from the CMOS
|
---|
| 170 | *
|
---|
| 171 | * @param fun The RTC function
|
---|
| 172 | * @param t Pointer to the time variable
|
---|
| 173 | *
|
---|
| 174 | * @return EOK on success or a negative error code
|
---|
| 175 | */
|
---|
[4863bb52] | 176 | static int
|
---|
| 177 | rtc_time_get(ddf_fun_t *fun, time_t *t)
|
---|
| 178 | {
|
---|
| 179 | return EOK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[0b8a3e7] | 182 | /** Set the time in the RTC
|
---|
| 183 | *
|
---|
| 184 | * @param fun The RTC function
|
---|
| 185 | * @param t The time value to set
|
---|
| 186 | *
|
---|
| 187 | * @return EOK or a negative error code
|
---|
| 188 | */
|
---|
[4863bb52] | 189 | static int
|
---|
| 190 | rtc_time_set(ddf_fun_t *fun, time_t t)
|
---|
| 191 | {
|
---|
| 192 | return EOK;
|
---|
[1e19a15] | 193 | }
|
---|
| 194 |
|
---|
[a31ca11f] | 195 | /** The dev_add callback of the rtc driver
|
---|
| 196 | *
|
---|
| 197 | * @param dev The RTC device
|
---|
| 198 | *
|
---|
| 199 | * @return EOK on success or a negative error code
|
---|
| 200 | */
|
---|
| 201 | static int
|
---|
| 202 | rtc_dev_add(ddf_dev_t *dev)
|
---|
| 203 | {
|
---|
[6b329749] | 204 | rtc_t *rtc;
|
---|
[b3db669] | 205 | int rc;
|
---|
[6b329749] | 206 |
|
---|
| 207 | ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
|
---|
| 208 | dev->name, (int) dev->handle);
|
---|
| 209 |
|
---|
| 210 | rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
|
---|
| 211 | if (!rtc)
|
---|
| 212 | return ENOMEM;
|
---|
| 213 |
|
---|
| 214 | rtc->dev = dev;
|
---|
| 215 | fibril_mutex_initialize(&rtc->mutex);
|
---|
| 216 |
|
---|
[b3db669] | 217 | rc = rtc_dev_initialize(rtc);
|
---|
| 218 | if (rc != EOK)
|
---|
| 219 | return rc;
|
---|
| 220 |
|
---|
| 221 | return rc;
|
---|
[a31ca11f] | 222 | }
|
---|
| 223 |
|
---|
[1e19a15] | 224 | int
|
---|
| 225 | main(int argc, char **argv)
|
---|
| 226 | {
|
---|
| 227 | printf(NAME ": HelenOS RTC driver\n");
|
---|
| 228 | rtc_init();
|
---|
| 229 | return ddf_driver_main(&rtc_driver);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /**
|
---|
| 233 | * @}
|
---|
| 234 | */
|
---|