source: mainline/uspace/drv/platform/icp/icp.c@ d51838f

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

Let leaf drivers enable/disable/clear interrupts via hw_res instead of directly using irc.

  • Property mode set to 100644
File size: 6.0 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 pio_window_t *icp_get_pio_window(ddf_fun_t *fnode)
171{
172 return &icp_pio_window;
173}
174
175static hw_res_ops_t icp_hw_res_ops = {
176 .get_resource_list = &icp_get_resources,
177 .enable_interrupt = &icp_fun_enable_interrupt,
178};
179
180static pio_window_ops_t icp_pio_window_ops = {
181 .get_pio_window = &icp_get_pio_window
182};
183
184static ddf_dev_ops_t icp_fun_ops = {
185 .interfaces = {
186 [HW_RES_DEV_IFACE] = &icp_hw_res_ops,
187 [PIO_WINDOW_DEV_IFACE] = &icp_pio_window_ops
188 }
189};
190
191static int icp_add_fun(ddf_dev_t *dev, const char *name, const char *str_match_id,
192 icp_fun_t *fun_proto)
193{
194 ddf_msg(LVL_NOTE, "Adding function '%s'.", name);
195
196 ddf_fun_t *fnode = NULL;
197 int rc;
198
199 /* Create new device. */
200 fnode = ddf_fun_create(dev, fun_inner, name);
201 if (fnode == NULL) {
202 ddf_msg(LVL_ERROR, "Error creating function '%s'", name);
203 rc = ENOMEM;
204 goto error;
205 }
206
207 icp_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(icp_fun_t));
208 *fun = *fun_proto;
209
210 /* Add match ID */
211 rc = ddf_fun_add_match_id(fnode, str_match_id, 100);
212 if (rc != EOK) {
213 ddf_msg(LVL_ERROR, "Error adding match ID");
214 goto error;
215 }
216
217 /* Set provided operations to the device. */
218 ddf_fun_set_ops(fnode, &icp_fun_ops);
219
220 /* Register function. */
221 if (ddf_fun_bind(fnode) != EOK) {
222 ddf_msg(LVL_ERROR, "Failed binding function %s.", name);
223 goto error;
224 }
225
226 return EOK;
227
228error:
229 if (fnode != NULL)
230 ddf_fun_destroy(fnode);
231
232 return rc;
233}
234
235static int icp_add_functions(ddf_dev_t *dev)
236{
237 int rc;
238
239 rc = icp_add_fun(dev, "kbd", "arm/pl050", &icp_kbd_fun_proto);
240 if (rc != EOK)
241 return rc;
242
243 rc = icp_add_fun(dev, "mouse", "arm/pl050", &icp_mouse_fun_proto);
244 if (rc != EOK)
245 return rc;
246
247 return EOK;
248}
249
250/** Add device. */
251static int icp_dev_add(ddf_dev_t *dev)
252{
253 ddf_msg(LVL_NOTE, "icp_dev_add, device handle = %d",
254 (int)ddf_dev_get_handle(dev));
255
256 /* Register functions. */
257 if (icp_add_functions(dev)) {
258 ddf_msg(LVL_ERROR, "Failed to add functions for ICP platform.");
259 }
260
261 return EOK;
262}
263
264int main(int argc, char *argv[])
265{
266 int rc;
267
268 printf(NAME ": HelenOS IntegratorCP platform driver\n");
269
270 rc = ddf_log_init(NAME);
271 if (rc != EOK) {
272 printf(NAME ": Failed initializing logging service");
273 return 1;
274 }
275
276 return ddf_driver_main(&icp_driver);
277}
278
279/**
280 * @}
281 */
Note: See TracBrowser for help on using the repository browser.