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 |
|
---|
38 | #include <errno.h>
|
---|
39 | #include <ddi.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <ddf/driver.h>
|
---|
42 | #include <ddf/log.h>
|
---|
43 | #include <ops/clock.h>
|
---|
44 | #include <fibril_synch.h>
|
---|
45 | #include <device/hw_res.h>
|
---|
46 | #include <devman.h>
|
---|
47 |
|
---|
48 | #define NAME "cmos-rtc"
|
---|
49 |
|
---|
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;
|
---|
57 | /** The base I/O address of the device registers */
|
---|
58 | uint32_t io_addr;
|
---|
59 | } rtc_t;
|
---|
60 |
|
---|
61 |
|
---|
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 |
|
---|
68 | static int
|
---|
69 | rtc_dev_add(ddf_dev_t *dev);
|
---|
70 |
|
---|
71 | static int
|
---|
72 | rtc_dev_initialize(rtc_t *rtc);
|
---|
73 |
|
---|
74 |
|
---|
75 | static ddf_dev_ops_t rtc_dev_ops;
|
---|
76 |
|
---|
77 | /** The RTC device driver's standard operations */
|
---|
78 | static driver_ops_t rtc_ops = {
|
---|
79 | .dev_add = rtc_dev_add,
|
---|
80 | .dev_remove = NULL, /* XXX */
|
---|
81 | };
|
---|
82 |
|
---|
83 | /** The RTC device driver structure */
|
---|
84 | static driver_t rtc_driver = {
|
---|
85 | .name = NAME,
|
---|
86 | .driver_ops = &rtc_ops,
|
---|
87 | };
|
---|
88 |
|
---|
89 | /** Clock interface */
|
---|
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 */
|
---|
96 | static void
|
---|
97 | rtc_init(void)
|
---|
98 | {
|
---|
99 | ddf_log_init(NAME, LVL_ERROR);
|
---|
100 |
|
---|
101 | rtc_dev_ops.open = NULL; /* XXX */
|
---|
102 | rtc_dev_ops.close = NULL; /* XXX */
|
---|
103 |
|
---|
104 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
---|
105 | rtc_dev_ops.default_handler = NULL; /* XXX */
|
---|
106 | }
|
---|
107 |
|
---|
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 | {
|
---|
117 | /* XXX Do cleanup in case of failure */
|
---|
118 | int rc;
|
---|
119 | size_t i;
|
---|
120 | hw_resource_t *res;
|
---|
121 | bool ioport = false;
|
---|
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 |
|
---|
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 |
|
---|
166 | return EOK;
|
---|
167 | }
|
---|
168 |
|
---|
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 | */
|
---|
176 | static int
|
---|
177 | rtc_time_get(ddf_fun_t *fun, time_t *t)
|
---|
178 | {
|
---|
179 | return EOK;
|
---|
180 | }
|
---|
181 |
|
---|
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 | */
|
---|
189 | static int
|
---|
190 | rtc_time_set(ddf_fun_t *fun, time_t t)
|
---|
191 | {
|
---|
192 | return EOK;
|
---|
193 | }
|
---|
194 |
|
---|
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 | {
|
---|
204 | rtc_t *rtc;
|
---|
205 | int rc;
|
---|
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 |
|
---|
217 | rc = rtc_dev_initialize(rtc);
|
---|
218 | if (rc != EOK)
|
---|
219 | return rc;
|
---|
220 |
|
---|
221 | return rc;
|
---|
222 | }
|
---|
223 |
|
---|
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 | */
|
---|