source: mainline/uspace/drv/platform/icp/icp.c@ 75911d24

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 75911d24 was 75911d24, checked in by Jiri Svoboda <jiri@…>, 8 years ago

ICP could relay disable/clear interrupt.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * Copyright (c) 2014 Jiri Svoboda
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 icp IntegratorCP platform driver.
31 * @brief HelenOS IntegratorCP platform driver.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <stdio.h>
40#include <errno.h>
41#include <irc.h>
42#include <stdbool.h>
43#include <stdlib.h>
44
45#include <ddf/driver.h>
46#include <ddf/log.h>
47#include <ops/hw_res.h>
48#include <ops/pio_window.h>
49
50#define NAME "icp"
51
52enum {
53 icp_kbd_base = 0x18000000,
54 icp_kbd_irq = 3,
55 icp_mouse_base = 0x19000000,
56 icp_mouse_irq = 4
57};
58
59typedef struct icp_fun {
60 hw_resource_list_t hw_resources;
61} icp_fun_t;
62
63static int icp_dev_add(ddf_dev_t *dev);
64
65static driver_ops_t icp_ops = {
66 .dev_add = &icp_dev_add
67};
68
69static driver_t icp_driver = {
70 .name = NAME,
71 .driver_ops = &icp_ops
72};
73
74static hw_resource_t icp_kbd_res[] = {
75 {
76 .type = MEM_RANGE,
77 .res.mem_range = {
78 .address = icp_kbd_base,
79 .size = 9,
80 .relative = false,
81 .endianness = LITTLE_ENDIAN
82 }
83 },
84 {
85 .type = INTERRUPT,
86 .res.interrupt = {
87 .irq = icp_kbd_irq
88 }
89 }
90};
91
92static hw_resource_t icp_mouse_res[] = {
93 {
94 .type = MEM_RANGE,
95 .res.mem_range = {
96 .address = icp_mouse_base,
97 .size = 9,
98 .relative = false,
99 .endianness = LITTLE_ENDIAN
100 }
101 },
102 {
103 .type = INTERRUPT,
104 .res.interrupt = {
105 .irq = icp_mouse_irq
106 }
107 }
108};
109
110static pio_window_t icp_pio_window = {
111 .mem = {
112 .base = 0,
113 .size = -1
114 }
115};
116
117static icp_fun_t icp_kbd_fun_proto = {
118 .hw_resources = {
119 sizeof(icp_kbd_res) / sizeof(icp_kbd_res[0]),
120 icp_kbd_res
121 },
122};
123
124static icp_fun_t icp_mouse_fun_proto = {
125 .hw_resources = {
126 sizeof(icp_mouse_res) / sizeof(icp_mouse_res[0]),
127 icp_mouse_res
128 },
129};
130
131/** Obtain function soft-state from DDF function node */
132static icp_fun_t *icp_fun(ddf_fun_t *fnode)
133{
134 return ddf_fun_data_get(fnode);
135}
136
137static hw_resource_list_t *icp_get_resources(ddf_fun_t *fnode)
138{
139 icp_fun_t *fun = icp_fun(fnode);
140
141 assert(fun != NULL);
142 return &fun->hw_resources;
143}
144
145static bool icp_fun_owns_interrupt(icp_fun_t *fun, int irq)
146{
147 const hw_resource_list_t *res = &fun->hw_resources;
148
149 /* Check that specified irq really belongs to the function */
150 for (size_t i = 0; i < res->count; ++i) {
151 if (res->resources[i].type == INTERRUPT &&
152 res->resources[i].res.interrupt.irq == irq) {
153 return true;
154 }
155 }
156
157 return false;
158}
159
160static int icp_fun_enable_interrupt(ddf_fun_t *fnode, int irq)
161{
162 icp_fun_t *fun = icp_fun(fnode);
163
164 if (!icp_fun_owns_interrupt(fun, irq))
165 return EINVAL;
166
167 return irc_enable_interrupt(irq);
168}
169
170static int icp_fun_disable_interrupt(ddf_fun_t *fnode, int irq)
171{
172 icp_fun_t *fun = icp_fun(fnode);
173
174 if (!icp_fun_owns_interrupt(fun, irq))
175 return EINVAL;
176
177 return irc_disable_interrupt(irq);
178}
179
180static int icp_fun_clear_interrupt(ddf_fun_t *fnode, int irq)
181{
182 icp_fun_t *fun = icp_fun(fnode);
183
184 if (!icp_fun_owns_interrupt(fun, irq))
185 return EINVAL;
186
187 return irc_clear_interrupt(irq);
188}
189
190static pio_window_t *icp_get_pio_window(ddf_fun_t *fnode)
191{
192 return &icp_pio_window;
193}
194
195static hw_res_ops_t icp_hw_res_ops = {
196 .get_resource_list = &icp_get_resources,
197 .enable_interrupt = &icp_fun_enable_interrupt,
198 .disable_interrupt = &icp_fun_disable_interrupt,
199 .clear_interrupt = &icp_fun_clear_interrupt
200};
201
202static pio_window_ops_t icp_pio_window_ops = {
203 .get_pio_window = &icp_get_pio_window
204};
205
206static ddf_dev_ops_t icp_fun_ops = {
207 .interfaces = {
208 [HW_RES_DEV_IFACE] = &icp_hw_res_ops,
209 [PIO_WINDOW_DEV_IFACE] = &icp_pio_window_ops
210 }
211};
212
213static int icp_add_fun(ddf_dev_t *dev, const char *name, const char *str_match_id,
214 icp_fun_t *fun_proto)
215{
216 ddf_msg(LVL_NOTE, "Adding function '%s'.", name);
217
218 ddf_fun_t *fnode = NULL;
219 int rc;
220
221 /* Create new device. */
222 fnode = ddf_fun_create(dev, fun_inner, name);
223 if (fnode == NULL) {
224 ddf_msg(LVL_ERROR, "Error creating function '%s'", name);
225 rc = ENOMEM;
226 goto error;
227 }
228
229 icp_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(icp_fun_t));
230 *fun = *fun_proto;
231
232 /* Add match ID */
233 rc = ddf_fun_add_match_id(fnode, str_match_id, 100);
234 if (rc != EOK) {
235 ddf_msg(LVL_ERROR, "Error adding match ID");
236 goto error;
237 }
238
239 /* Set provided operations to the device. */
240 ddf_fun_set_ops(fnode, &icp_fun_ops);
241
242 /* Register function. */
243 if (ddf_fun_bind(fnode) != EOK) {
244 ddf_msg(LVL_ERROR, "Failed binding function %s.", name);
245 goto error;
246 }
247
248 return EOK;
249
250error:
251 if (fnode != NULL)
252 ddf_fun_destroy(fnode);
253
254 return rc;
255}
256
257static int icp_add_functions(ddf_dev_t *dev)
258{
259 int rc;
260
261 rc = icp_add_fun(dev, "kbd", "arm/pl050", &icp_kbd_fun_proto);
262 if (rc != EOK)
263 return rc;
264
265 rc = icp_add_fun(dev, "mouse", "arm/pl050", &icp_mouse_fun_proto);
266 if (rc != EOK)
267 return rc;
268
269 return EOK;
270}
271
272/** Add device. */
273static int icp_dev_add(ddf_dev_t *dev)
274{
275 ddf_msg(LVL_NOTE, "icp_dev_add, device handle = %d",
276 (int)ddf_dev_get_handle(dev));
277
278 /* Register functions. */
279 if (icp_add_functions(dev)) {
280 ddf_msg(LVL_ERROR, "Failed to add functions for ICP platform.");
281 }
282
283 return EOK;
284}
285
286int main(int argc, char *argv[])
287{
288 int rc;
289
290 printf(NAME ": HelenOS IntegratorCP platform driver\n");
291
292 rc = ddf_log_init(NAME);
293 if (rc != EOK) {
294 printf(NAME ": Failed initializing logging service");
295 return 1;
296 }
297
298 return ddf_driver_main(&icp_driver);
299}
300
301/**
302 * @}
303 */
Note: See TracBrowser for help on using the repository browser.