source: mainline/uspace/srv/hw/bus/cuda_adb/cuda_adb.c@ 6886705

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

Move CUDA hw-related definitions to a separate header file.

  • Property mode set to 100644
File size: 11.1 KB
Line 
1/*
2 * Copyright (c) 2010 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/** @addtogroup genarch
30 * @{
31 */
32/** @file VIA-CUDA Apple Desktop Bus driver
33 *
34 * Note: We should really do a full bus scan at the beginning and resolve
35 * address conflicts. Also we should consider the handler ID in r3. Instead
36 * we just assume a keyboard at address 2 or 8 and a mouse at address 9.
37 */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <stddef.h>
42#include <stdint.h>
43#include <stdbool.h>
44#include <ddi.h>
45#include <libarch/ddi.h>
46#include <loc.h>
47#include <sysinfo.h>
48#include <errno.h>
49#include <ipc/adb.h>
50#include <assert.h>
51#include "cuda_adb.h"
52#include "cuda_hw.h"
53
54#define NAME "cuda_adb"
55
56static void cuda_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
57static int cuda_init(void);
58static void cuda_irq_handler(ipc_callid_t iid, ipc_call_t *call, void *arg);
59
60static void cuda_irq_listen(void);
61static void cuda_irq_receive(void);
62static void cuda_irq_rcv_end(void *buf, size_t *len);
63static void cuda_irq_send_start(void);
64static void cuda_irq_send(void);
65
66static void cuda_packet_handle(uint8_t *buf, size_t len);
67static void cuda_send_start(void);
68static void cuda_autopoll_set(bool enable);
69
70static void adb_packet_handle(uint8_t *data, size_t size, bool autopoll);
71
72static irq_pio_range_t cuda_ranges[] = {
73 {
74 .base = 0,
75 .size = sizeof(cuda_t)
76 }
77};
78
79static irq_cmd_t cuda_cmds[] = {
80 {
81 .cmd = CMD_PIO_READ_8,
82 .addr = NULL,
83 .dstarg = 1
84 },
85 {
86 .cmd = CMD_AND,
87 .value = SR_INT,
88 .srcarg = 1,
89 .dstarg = 2
90 },
91 {
92 .cmd = CMD_PREDICATE,
93 .value = 1,
94 .srcarg = 2
95 },
96 {
97 .cmd = CMD_ACCEPT
98 }
99};
100
101
102static irq_code_t cuda_irq_code = {
103 sizeof(cuda_ranges) / sizeof(irq_pio_range_t),
104 cuda_ranges,
105 sizeof(cuda_cmds) / sizeof(irq_cmd_t),
106 cuda_cmds
107};
108
109static cuda_instance_t cinst;
110
111static cuda_instance_t *instance = &cinst;
112static cuda_t *dev;
113
114static adb_dev_t adb_dev[ADB_MAX_ADDR];
115
116int main(int argc, char *argv[])
117{
118 service_id_t service_id;
119 int rc;
120 int i;
121
122 printf(NAME ": VIA-CUDA Apple Desktop Bus driver\n");
123
124 for (i = 0; i < ADB_MAX_ADDR; ++i) {
125 adb_dev[i].client_sess = NULL;
126 adb_dev[i].service_id = 0;
127 }
128
129 async_set_fallback_port_handler(cuda_connection, NULL);
130 rc = loc_server_register(NAME);
131 if (rc < 0) {
132 printf(NAME ": Unable to register server.\n");
133 return rc;
134 }
135
136 rc = loc_service_register("adb/kbd", &service_id);
137 if (rc != EOK) {
138 printf(NAME ": Unable to register service %s.\n", "adb/kbd");
139 return rc;
140 }
141
142 adb_dev[2].service_id = service_id;
143 adb_dev[8].service_id = service_id;
144
145 rc = loc_service_register("adb/mouse", &service_id);
146 if (rc != EOK) {
147 printf(NAME ": Unable to register service %s.\n", "adb/mouse");
148 return rc;
149 }
150
151 adb_dev[9].service_id = service_id;
152
153 if (cuda_init() < 0) {
154 printf("cuda_init() failed\n");
155 return 1;
156 }
157
158 task_retval(0);
159 async_manager();
160
161 return 0;
162}
163
164/** Character device connection handler */
165static void cuda_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
166{
167 ipc_callid_t callid;
168 ipc_call_t call;
169 sysarg_t method;
170 service_id_t dsid;
171 int dev_addr, i;
172
173 /* Get the device handle. */
174 dsid = IPC_GET_ARG2(*icall);
175
176 /* Determine which disk device is the client connecting to. */
177 dev_addr = -1;
178 for (i = 0; i < ADB_MAX_ADDR; i++) {
179 if (adb_dev[i].service_id == dsid)
180 dev_addr = i;
181 }
182
183 if (dev_addr < 0) {
184 async_answer_0(iid, EINVAL);
185 return;
186 }
187
188 /* Answer the IPC_M_CONNECT_ME_TO call. */
189 async_answer_0(iid, EOK);
190
191 while (true) {
192 callid = async_get_call(&call);
193 method = IPC_GET_IMETHOD(call);
194
195 if (!method) {
196 /* The other side has hung up. */
197 async_answer_0(callid, EOK);
198 return;
199 }
200
201 async_sess_t *sess =
202 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
203 if (sess != NULL) {
204 if (adb_dev[dev_addr].client_sess == NULL) {
205 adb_dev[dev_addr].client_sess = sess;
206
207 /*
208 * A hack so that we send the data to the session
209 * regardless of which address the device is on.
210 */
211 for (i = 0; i < ADB_MAX_ADDR; ++i) {
212 if (adb_dev[i].service_id == dsid)
213 adb_dev[i].client_sess = sess;
214 }
215
216 async_answer_0(callid, EOK);
217 } else
218 async_answer_0(callid, ELIMIT);
219 } else
220 async_answer_0(callid, EINVAL);
221 }
222}
223
224
225static int cuda_init(void)
226{
227 if (sysinfo_get_value("cuda.address.physical", &(instance->cuda_physical)) != EOK)
228 return -1;
229
230 void *vaddr;
231 if (pio_enable((void *) instance->cuda_physical, sizeof(cuda_t), &vaddr) != 0)
232 return -1;
233
234 dev = vaddr;
235
236 instance->cuda = dev;
237 instance->xstate = cx_listen;
238 instance->bidx = 0;
239 instance->snd_bytes = 0;
240
241 fibril_mutex_initialize(&instance->dev_lock);
242
243 /* Disable all interrupts from CUDA. */
244 pio_write_8(&dev->ier, IER_CLR | ALL_INT);
245
246 cuda_irq_code.ranges[0].base = (uintptr_t) instance->cuda_physical;
247 cuda_irq_code.cmds[0].addr = (void *) &((cuda_t *) instance->cuda_physical)->ifr;
248 async_irq_subscribe(10, cuda_irq_handler, NULL, &cuda_irq_code);
249
250 /* Enable SR interrupt. */
251 pio_write_8(&dev->ier, TIP | TREQ);
252 pio_write_8(&dev->ier, IER_SET | SR_INT);
253
254 /* Enable ADB autopolling. */
255 cuda_autopoll_set(true);
256
257 return 0;
258}
259
260static void cuda_irq_handler(ipc_callid_t iid, ipc_call_t *call, void *arg)
261{
262 uint8_t rbuf[CUDA_RCV_BUF_SIZE];
263 size_t len;
264 bool handle;
265
266 handle = false;
267 len = 0;
268
269 fibril_mutex_lock(&instance->dev_lock);
270
271 switch (instance->xstate) {
272 case cx_listen:
273 cuda_irq_listen();
274 break;
275 case cx_receive:
276 cuda_irq_receive();
277 break;
278 case cx_rcv_end:
279 cuda_irq_rcv_end(rbuf, &len);
280 handle = true;
281 break;
282 case cx_send_start:
283 cuda_irq_send_start();
284 break;
285 case cx_send:
286 cuda_irq_send();
287 break;
288 }
289
290 /* Lower IFR.SR_INT so that CUDA can generate next int by raising it. */
291 pio_write_8(&instance->cuda->ifr, SR_INT);
292
293 fibril_mutex_unlock(&instance->dev_lock);
294
295 /* Handle an incoming packet. */
296 if (handle)
297 cuda_packet_handle(rbuf, len);
298}
299
300/** Interrupt in listen state.
301 *
302 * Start packet reception.
303 */
304static void cuda_irq_listen(void)
305{
306 uint8_t b = pio_read_8(&dev->b);
307
308 if ((b & TREQ) != 0) {
309 printf("cuda_irq_listen: no TREQ?!\n");
310 return;
311 }
312
313 pio_write_8(&dev->b, b & ~TIP);
314 instance->xstate = cx_receive;
315}
316
317/** Interrupt in receive state.
318 *
319 * Receive next byte of packet.
320 */
321static void cuda_irq_receive(void)
322{
323 uint8_t data = pio_read_8(&dev->sr);
324 if (instance->bidx < CUDA_RCV_BUF_SIZE)
325 instance->rcv_buf[instance->bidx++] = data;
326
327 uint8_t b = pio_read_8(&dev->b);
328
329 if ((b & TREQ) == 0) {
330 pio_write_8(&dev->b, b ^ TACK);
331 } else {
332 pio_write_8(&dev->b, b | TACK | TIP);
333 instance->xstate = cx_rcv_end;
334 }
335}
336
337/** Interrupt in rcv_end state.
338 *
339 * Terminate packet reception. Either go back to listen state or start
340 * receiving another packet if CUDA has one for us.
341 */
342static void cuda_irq_rcv_end(void *buf, size_t *len)
343{
344 uint8_t b = pio_read_8(&dev->b);
345
346 if ((b & TREQ) == 0) {
347 instance->xstate = cx_receive;
348 pio_write_8(&dev->b, b & ~TIP);
349 } else {
350 instance->xstate = cx_listen;
351 cuda_send_start();
352 }
353
354 memcpy(buf, instance->rcv_buf, instance->bidx);
355 *len = instance->bidx;
356 instance->bidx = 0;
357}
358
359/** Interrupt in send_start state.
360 *
361 * Process result of sending first byte (and send second on success).
362 */
363static void cuda_irq_send_start(void)
364{
365 uint8_t b;
366
367 b = pio_read_8(&dev->b);
368
369 if ((b & TREQ) == 0) {
370 /* Collision */
371 pio_write_8(&dev->acr, pio_read_8(&dev->acr) & ~SR_OUT);
372 pio_read_8(&dev->sr);
373 pio_write_8(&dev->b, pio_read_8(&dev->b) | TIP | TACK);
374 instance->xstate = cx_listen;
375 return;
376 }
377
378 pio_write_8(&dev->sr, instance->snd_buf[1]);
379 pio_write_8(&dev->b, pio_read_8(&dev->b) ^ TACK);
380 instance->bidx = 2;
381
382 instance->xstate = cx_send;
383}
384
385/** Interrupt in send state.
386 *
387 * Send next byte or terminate transmission.
388 */
389static void cuda_irq_send(void)
390{
391 if (instance->bidx < instance->snd_bytes) {
392 /* Send next byte. */
393 pio_write_8(&dev->sr, instance->snd_buf[instance->bidx++]);
394 pio_write_8(&dev->b, pio_read_8(&dev->b) ^ TACK);
395 return;
396 }
397
398 /* End transfer. */
399 instance->snd_bytes = 0;
400 instance->bidx = 0;
401
402 pio_write_8(&dev->acr, pio_read_8(&dev->acr) & ~SR_OUT);
403 pio_read_8(&dev->sr);
404 pio_write_8(&dev->b, pio_read_8(&dev->b) | TACK | TIP);
405
406 instance->xstate = cx_listen;
407 /* TODO: Match reply with request. */
408}
409
410static void cuda_packet_handle(uint8_t *data, size_t len)
411{
412 if (data[0] != PT_ADB)
413 return;
414 if (len < 2)
415 return;
416
417 adb_packet_handle(data + 2, len - 2, (data[1] & 0x40) != 0);
418}
419
420static void adb_packet_handle(uint8_t *data, size_t size, bool autopoll)
421{
422 uint8_t dev_addr;
423 uint8_t reg_no;
424 uint16_t reg_val;
425 unsigned i;
426
427 dev_addr = data[0] >> 4;
428 reg_no = data[0] & 0x03;
429
430 if (size != 3) {
431 printf("unrecognized packet, size=%zu\n", size);
432 for (i = 0; i < size; ++i) {
433 printf(" 0x%02x", data[i]);
434 }
435 putchar('\n');
436 return;
437 }
438
439 if (reg_no != 0) {
440 printf("unrecognized packet, size=%zu\n", size);
441 for (i = 0; i < size; ++i) {
442 printf(" 0x%02x", data[i]);
443 }
444 putchar('\n');
445 return;
446 }
447
448 reg_val = ((uint16_t) data[1] << 8) | (uint16_t) data[2];
449
450 if (adb_dev[dev_addr].client_sess == NULL)
451 return;
452
453 async_exch_t *exch =
454 async_exchange_begin(adb_dev[dev_addr].client_sess);
455 async_msg_1(exch, ADB_REG_NOTIF, reg_val);
456 async_exchange_end(exch);
457}
458
459static void cuda_autopoll_set(bool enable)
460{
461 instance->snd_buf[0] = PT_CUDA;
462 instance->snd_buf[1] = CPT_AUTOPOLL;
463 instance->snd_buf[2] = enable ? 0x01 : 0x00;
464 instance->snd_bytes = 3;
465 instance->bidx = 0;
466
467 cuda_send_start();
468}
469
470static void cuda_send_start(void)
471{
472 cuda_t *dev = instance->cuda;
473
474 assert(instance->xstate == cx_listen);
475
476 if (instance->snd_bytes == 0)
477 return;
478
479 /* Check for incoming data. */
480 if ((pio_read_8(&dev->b) & TREQ) == 0)
481 return;
482
483 pio_write_8(&dev->acr, pio_read_8(&dev->acr) | SR_OUT);
484 pio_write_8(&dev->sr, instance->snd_buf[0]);
485 pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP);
486
487 instance->xstate = cx_send_start;
488}
489
490/** @}
491 */
Note: See TracBrowser for help on using the repository browser.