1 | /*
|
---|
2 | * Copyright (c) 2009 Vineeth Pillai
|
---|
3 | * Copyright (c) 2014 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @file
|
---|
31 | */
|
---|
32 |
|
---|
33 | #include <assert.h>
|
---|
34 | #include <bitops.h>
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <ddf/driver.h>
|
---|
38 | #include <ddf/interrupt.h>
|
---|
39 | #include <ddf/log.h>
|
---|
40 | #include <device/hw_res_parsed.h>
|
---|
41 | #include <io/chardev_srv.h>
|
---|
42 | #include <irc.h>
|
---|
43 |
|
---|
44 | #include "pl050_hw.h"
|
---|
45 |
|
---|
46 | #define NAME "pl050"
|
---|
47 |
|
---|
48 | enum {
|
---|
49 | buffer_size = 64
|
---|
50 | };
|
---|
51 |
|
---|
52 | static int pl050_dev_add(ddf_dev_t *);
|
---|
53 | static int pl050_fun_online(ddf_fun_t *);
|
---|
54 | static int pl050_fun_offline(ddf_fun_t *);
|
---|
55 | static void pl050_char_conn(ipc_callid_t, ipc_call_t *, void *);
|
---|
56 | static int pl050_read(chardev_srv_t *, void *, size_t);
|
---|
57 | static int pl050_write(chardev_srv_t *, const void *, size_t);
|
---|
58 |
|
---|
59 | static driver_ops_t driver_ops = {
|
---|
60 | .dev_add = &pl050_dev_add,
|
---|
61 | .fun_online = &pl050_fun_online,
|
---|
62 | .fun_offline = &pl050_fun_offline
|
---|
63 | };
|
---|
64 |
|
---|
65 | static driver_t pl050_driver = {
|
---|
66 | .name = NAME,
|
---|
67 | .driver_ops = &driver_ops
|
---|
68 | };
|
---|
69 |
|
---|
70 | static chardev_ops_t pl050_chardev_ops = {
|
---|
71 | .read = pl050_read,
|
---|
72 | .write = pl050_write
|
---|
73 | };
|
---|
74 |
|
---|
75 | typedef struct {
|
---|
76 | async_sess_t *parent_sess;
|
---|
77 | ddf_dev_t *dev;
|
---|
78 | char *name;
|
---|
79 |
|
---|
80 | ddf_fun_t *fun_a;
|
---|
81 | chardev_srvs_t cds;
|
---|
82 |
|
---|
83 | uintptr_t iobase;
|
---|
84 | size_t iosize;
|
---|
85 | kmi_regs_t *regs;
|
---|
86 | uint8_t buffer[buffer_size];
|
---|
87 | size_t buf_rp;
|
---|
88 | size_t buf_wp;
|
---|
89 | fibril_condvar_t buf_cv;
|
---|
90 | fibril_mutex_t buf_lock;
|
---|
91 | } pl050_t;
|
---|
92 |
|
---|
93 | static irq_pio_range_t pl050_ranges[] = {
|
---|
94 | {
|
---|
95 | .base = 0,
|
---|
96 | .size = 9,
|
---|
97 | }
|
---|
98 | };
|
---|
99 |
|
---|
100 | static irq_cmd_t pl050_cmds[] = {
|
---|
101 | {
|
---|
102 | .cmd = CMD_PIO_READ_8,
|
---|
103 | .addr = NULL,
|
---|
104 | .dstarg = 1
|
---|
105 | },
|
---|
106 | {
|
---|
107 | .cmd = CMD_AND,
|
---|
108 | .value = BIT_V(uint8_t, kmi_stat_rxfull),
|
---|
109 | .srcarg = 1,
|
---|
110 | .dstarg = 3
|
---|
111 | },
|
---|
112 | {
|
---|
113 | .cmd = CMD_PREDICATE,
|
---|
114 | .value = 2,
|
---|
115 | .srcarg = 3
|
---|
116 | },
|
---|
117 | {
|
---|
118 | .cmd = CMD_PIO_READ_8,
|
---|
119 | .addr = NULL, /* Will be patched in run-time */
|
---|
120 | .dstarg = 2
|
---|
121 | },
|
---|
122 | {
|
---|
123 | .cmd = CMD_ACCEPT
|
---|
124 | }
|
---|
125 | };
|
---|
126 |
|
---|
127 | static irq_code_t pl050_irq_code = {
|
---|
128 | sizeof(pl050_ranges) / sizeof(irq_pio_range_t),
|
---|
129 | pl050_ranges,
|
---|
130 | sizeof(pl050_cmds) / sizeof(irq_cmd_t),
|
---|
131 | pl050_cmds
|
---|
132 | };
|
---|
133 |
|
---|
134 | static pl050_t *pl050_from_fun(ddf_fun_t *fun)
|
---|
135 | {
|
---|
136 | return (pl050_t *)ddf_dev_data_get(ddf_fun_get_dev(fun));
|
---|
137 | }
|
---|
138 |
|
---|
139 | static void pl050_interrupt(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
|
---|
140 | {
|
---|
141 | pl050_t *pl050 = (pl050_t *)ddf_dev_data_get(dev);
|
---|
142 | size_t nidx;
|
---|
143 |
|
---|
144 | fibril_mutex_lock(&pl050->buf_lock);
|
---|
145 | nidx = (pl050->buf_wp + 1) % buffer_size;
|
---|
146 | if (nidx == pl050->buf_rp) {
|
---|
147 | /** Buffer overrunt */
|
---|
148 | ddf_msg(LVL_WARN, "Buffer overrun.");
|
---|
149 | fibril_mutex_unlock(&pl050->buf_lock);
|
---|
150 | return;
|
---|
151 | }
|
---|
152 |
|
---|
153 | pl050->buffer[pl050->buf_wp] = IPC_GET_ARG2(*call);
|
---|
154 | pl050->buf_wp = nidx;
|
---|
155 | fibril_condvar_broadcast(&pl050->buf_cv);
|
---|
156 | fibril_mutex_unlock(&pl050->buf_lock);
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int pl050_init(pl050_t *pl050)
|
---|
160 | {
|
---|
161 | hw_res_list_parsed_t res;
|
---|
162 | void *regs;
|
---|
163 | int rc;
|
---|
164 |
|
---|
165 | fibril_mutex_initialize(&pl050->buf_lock);
|
---|
166 | fibril_condvar_initialize(&pl050->buf_cv);
|
---|
167 | pl050->buf_rp = pl050->buf_wp = 0;
|
---|
168 |
|
---|
169 | pl050->parent_sess = ddf_dev_parent_sess_create(pl050->dev,
|
---|
170 | EXCHANGE_SERIALIZE);
|
---|
171 | if (pl050->parent_sess == NULL) {
|
---|
172 | ddf_msg(LVL_ERROR, "Failed connecitng parent driver.");
|
---|
173 | rc = ENOMEM;
|
---|
174 | goto error;
|
---|
175 | }
|
---|
176 |
|
---|
177 | hw_res_list_parsed_init(&res);
|
---|
178 | rc = hw_res_get_list_parsed(pl050->parent_sess, &res, 0);
|
---|
179 | if (rc != EOK) {
|
---|
180 | ddf_msg(LVL_ERROR, "Failed getting resource list.");
|
---|
181 | goto error;
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (res.mem_ranges.count != 1) {
|
---|
185 | ddf_msg(LVL_ERROR, "Expected exactly one memory range.");
|
---|
186 | rc = EINVAL;
|
---|
187 | goto error;
|
---|
188 | }
|
---|
189 |
|
---|
190 | pl050->iobase = RNGABS(res.mem_ranges.ranges[0]);
|
---|
191 | pl050->iosize = RNGSZ(res.mem_ranges.ranges[0]);
|
---|
192 |
|
---|
193 | pl050_irq_code.ranges[0].base = pl050->iobase;
|
---|
194 | kmi_regs_t *regsphys = (kmi_regs_t *) pl050->iobase;
|
---|
195 | pl050_irq_code.cmds[0].addr = ®sphys->stat;
|
---|
196 | pl050_irq_code.cmds[3].addr = ®sphys->data;
|
---|
197 |
|
---|
198 | if (res.irqs.count != 1) {
|
---|
199 | ddf_msg(LVL_ERROR, "Expected exactly one IRQ.");
|
---|
200 | rc = EINVAL;
|
---|
201 | goto error;
|
---|
202 | }
|
---|
203 |
|
---|
204 | ddf_msg(LVL_DEBUG, "iobase=%p irq=%d", (void *)pl050->iobase,
|
---|
205 | res.irqs.irqs[0]);
|
---|
206 |
|
---|
207 | rc = pio_enable((void *)pl050->iobase, sizeof(kmi_regs_t), ®s);
|
---|
208 | if (rc != EOK) {
|
---|
209 | ddf_msg(LVL_ERROR, "Error enabling PIO");
|
---|
210 | goto error;
|
---|
211 | }
|
---|
212 |
|
---|
213 | pl050->regs = regs;
|
---|
214 |
|
---|
215 | rc = register_interrupt_handler(pl050->dev, res.irqs.irqs[0],
|
---|
216 | pl050_interrupt, &pl050_irq_code);
|
---|
217 | if (rc != EOK) {
|
---|
218 | ddf_msg(LVL_ERROR, "Failed registering interrupt handler. (%d)",
|
---|
219 | rc);
|
---|
220 | goto error;
|
---|
221 | }
|
---|
222 |
|
---|
223 | rc = irc_enable_interrupt(res.irqs.irqs[0]);
|
---|
224 | if (rc != EOK) {
|
---|
225 | ddf_msg(LVL_ERROR, "Failed enabling interrupt. (%d)", rc);
|
---|
226 | goto error;
|
---|
227 | }
|
---|
228 |
|
---|
229 | pio_write_8(&pl050->regs->cr,
|
---|
230 | BIT_V(uint8_t, kmi_cr_enable) |
|
---|
231 | BIT_V(uint8_t, kmi_cr_rxintr));
|
---|
232 |
|
---|
233 | return EOK;
|
---|
234 | error:
|
---|
235 | return rc;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static int pl050_read(chardev_srv_t *srv, void *buffer, size_t size)
|
---|
239 | {
|
---|
240 | pl050_t *pl050 = (pl050_t *)srv->srvs->sarg;
|
---|
241 | uint8_t *bp = buffer;
|
---|
242 | size_t left;
|
---|
243 |
|
---|
244 | fibril_mutex_lock(&pl050->buf_lock);
|
---|
245 |
|
---|
246 | left = size;
|
---|
247 | while (left > 0) {
|
---|
248 | while (pl050->buf_rp == pl050->buf_wp)
|
---|
249 | fibril_condvar_wait(&pl050->buf_cv, &pl050->buf_lock);
|
---|
250 | *bp++ = pl050->buffer[pl050->buf_rp];
|
---|
251 | --left;
|
---|
252 | pl050->buf_rp = (pl050->buf_rp + 1) % buffer_size;
|
---|
253 | }
|
---|
254 |
|
---|
255 | fibril_mutex_unlock(&pl050->buf_lock);
|
---|
256 |
|
---|
257 | return size;
|
---|
258 | }
|
---|
259 |
|
---|
260 | static int pl050_write(chardev_srv_t *srv, const void *data, size_t size)
|
---|
261 | {
|
---|
262 | pl050_t *pl050 = (pl050_t *)srv->srvs->sarg;
|
---|
263 | uint8_t *dp = (uint8_t *)data;
|
---|
264 | uint8_t status;
|
---|
265 | size_t i;
|
---|
266 |
|
---|
267 | ddf_msg(LVL_NOTE, "%s/pl050_write(%zu bytes)", pl050->name, size);
|
---|
268 | for (i = 0; i < size; i++) {
|
---|
269 | while (true) {
|
---|
270 | status = pio_read_8(&pl050->regs->stat);
|
---|
271 | if ((status & BIT_V(uint8_t, kmi_stat_txempty)) != 0)
|
---|
272 | break;
|
---|
273 | }
|
---|
274 | pio_write_8(&pl050->regs->data, dp[i]);
|
---|
275 | }
|
---|
276 | ddf_msg(LVL_NOTE, "%s/pl050_write() success", pl050->name);
|
---|
277 |
|
---|
278 | return size;
|
---|
279 | }
|
---|
280 |
|
---|
281 | void pl050_char_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
282 | {
|
---|
283 | pl050_t *pl050 = pl050_from_fun((ddf_fun_t *)arg);
|
---|
284 |
|
---|
285 | chardev_conn(iid, icall, &pl050->cds);
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** Add device. */
|
---|
289 | static int pl050_dev_add(ddf_dev_t *dev)
|
---|
290 | {
|
---|
291 | ddf_fun_t *fun_a;
|
---|
292 | pl050_t *pl050 = NULL;
|
---|
293 | const char *mname;
|
---|
294 | int rc;
|
---|
295 |
|
---|
296 | ddf_msg(LVL_DEBUG, "pl050_dev_add()");
|
---|
297 |
|
---|
298 | pl050 = ddf_dev_data_alloc(dev, sizeof(pl050_t));
|
---|
299 | if (pl050 == NULL) {
|
---|
300 | ddf_msg(LVL_ERROR, "Failed allocating soft state.\n");
|
---|
301 | rc = ENOMEM;
|
---|
302 | goto error;
|
---|
303 | }
|
---|
304 |
|
---|
305 | pl050->name = (char *)ddf_dev_get_name(dev);
|
---|
306 | if (pl050->name == NULL) {
|
---|
307 | rc = ENOMEM;
|
---|
308 | goto error;
|
---|
309 | }
|
---|
310 |
|
---|
311 | fun_a = ddf_fun_create(dev, fun_inner, "a");
|
---|
312 | if (fun_a == NULL) {
|
---|
313 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
---|
314 | rc = ENOMEM;
|
---|
315 | goto error;
|
---|
316 | }
|
---|
317 |
|
---|
318 | pl050->fun_a = fun_a;
|
---|
319 | pl050->dev = dev;
|
---|
320 |
|
---|
321 | rc = pl050_init(pl050);
|
---|
322 | if (rc != EOK)
|
---|
323 | goto error;
|
---|
324 |
|
---|
325 | if (str_cmp(pl050->name, "kbd") == 0)
|
---|
326 | mname = "char/xtkbd";
|
---|
327 | else
|
---|
328 | mname = "char/ps2mouse";
|
---|
329 |
|
---|
330 | rc = ddf_fun_add_match_id(fun_a, mname, 10);
|
---|
331 | if (rc != EOK) {
|
---|
332 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
---|
333 | "char/xtkbd");
|
---|
334 | goto error;
|
---|
335 | }
|
---|
336 |
|
---|
337 | chardev_srvs_init(&pl050->cds);
|
---|
338 | pl050->cds.ops = &pl050_chardev_ops;
|
---|
339 | pl050->cds.sarg = pl050;
|
---|
340 |
|
---|
341 | ddf_fun_set_conn_handler(fun_a, pl050_char_conn);
|
---|
342 |
|
---|
343 | rc = ddf_fun_bind(fun_a);
|
---|
344 | if (rc != EOK) {
|
---|
345 | ddf_msg(LVL_ERROR, "Failed binding function 'a'. (%d)", rc);
|
---|
346 | ddf_fun_destroy(fun_a);
|
---|
347 | goto error;
|
---|
348 | }
|
---|
349 |
|
---|
350 | ddf_msg(LVL_DEBUG, "Device added.");
|
---|
351 | return EOK;
|
---|
352 | error:
|
---|
353 | if (pl050 != NULL)
|
---|
354 | free(pl050->name);
|
---|
355 | return rc;
|
---|
356 | }
|
---|
357 |
|
---|
358 | static int pl050_fun_online(ddf_fun_t *fun)
|
---|
359 | {
|
---|
360 | ddf_msg(LVL_DEBUG, "pl050_fun_online()");
|
---|
361 | return ddf_fun_online(fun);
|
---|
362 | }
|
---|
363 |
|
---|
364 | static int pl050_fun_offline(ddf_fun_t *fun)
|
---|
365 | {
|
---|
366 | ddf_msg(LVL_DEBUG, "pl050_fun_offline()");
|
---|
367 | return ddf_fun_offline(fun);
|
---|
368 | }
|
---|
369 |
|
---|
370 | int main(int argc, char *argv[])
|
---|
371 | {
|
---|
372 | int rc;
|
---|
373 |
|
---|
374 | printf(NAME ": HelenOS pl050 serial device driver\n");
|
---|
375 | rc = ddf_log_init(NAME);
|
---|
376 | if (rc != EOK) {
|
---|
377 | printf(NAME ": Error connecting logging service.");
|
---|
378 | return 1;
|
---|
379 | }
|
---|
380 |
|
---|
381 | return ddf_driver_main(&pl050_driver);
|
---|
382 | }
|
---|
383 |
|
---|