Rev 4610 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
982 | decky | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Martin Decky |
4610 | svoboda | 3 | * Copyright (c) 2009 Jiri Svoboda |
982 | decky | 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 | |||
4311 | decky | 30 | /** @addtogroup genarch |
1702 | cejka | 31 | * @{ |
32 | */ |
||
33 | /** @file |
||
34 | */ |
||
35 | |||
4311 | decky | 36 | #include <genarch/drivers/via-cuda/cuda.h> |
37 | #include <console/chardev.h> |
||
38 | #include <ddi/irq.h> |
||
982 | decky | 39 | #include <arch/asm.h> |
4311 | decky | 40 | #include <mm/slab.h> |
4148 | decky | 41 | #include <ddi/device.h> |
4610 | svoboda | 42 | #include <synch/spinlock.h> |
982 | decky | 43 | |
4626 | svoboda | 44 | static void cuda_packet_handle(cuda_instance_t *instance, uint8_t *buf, size_t len); |
45 | static void cuda_irq_listen(irq_t *irq); |
||
46 | static void cuda_irq_receive(irq_t *irq); |
||
47 | static void cuda_irq_rcv_end(irq_t *irq, void *buf, size_t *len); |
||
4610 | svoboda | 48 | |
49 | /** B register fields */ |
||
50 | enum { |
||
51 | TREQ = 0x08, |
||
52 | TACK = 0x10, |
||
53 | TIP = 0x20 |
||
54 | }; |
||
55 | |||
56 | /** IER register fields */ |
||
57 | enum { |
||
4626 | svoboda | 58 | IER_CLR = 0x00, |
4610 | svoboda | 59 | IER_SET = 0x80, |
4626 | svoboda | 60 | |
61 | SR_INT = 0x04, |
||
62 | ALL_INT = 0x7f |
||
4610 | svoboda | 63 | }; |
64 | |||
4626 | svoboda | 65 | /** ACR register fields */ |
66 | enum { |
||
67 | SR_OUT = 0x10 |
||
68 | }; |
||
69 | |||
70 | #include <print.h> |
||
4311 | decky | 71 | static irq_ownership_t cuda_claim(irq_t *irq) |
1619 | decky | 72 | { |
4610 | svoboda | 73 | cuda_instance_t *instance = irq->instance; |
74 | cuda_t *dev = instance->cuda; |
||
75 | uint8_t ifr; |
||
76 | |||
77 | ifr = pio_read_8(&dev->ifr); |
||
78 | |||
4626 | svoboda | 79 | if ((ifr & SR_INT) == 0) |
4610 | svoboda | 80 | return IRQ_DECLINE; |
4626 | svoboda | 81 | |
82 | return IRQ_ACCEPT; |
||
1619 | decky | 83 | } |
84 | |||
4311 | decky | 85 | static void cuda_irq_handler(irq_t *irq) |
1480 | palkovsky | 86 | { |
4610 | svoboda | 87 | cuda_instance_t *instance = irq->instance; |
4626 | svoboda | 88 | uint8_t rbuf[CUDA_RCV_BUF_SIZE]; |
89 | size_t len; |
||
90 | bool handle; |
||
4610 | svoboda | 91 | |
4626 | svoboda | 92 | handle = false; |
93 | len = 0; |
||
94 | |||
4610 | svoboda | 95 | spinlock_lock(&instance->dev_lock); |
96 | |||
4626 | svoboda | 97 | /* Lower IFR.SR_INT so that CUDA can generate next int by raising it. */ |
98 | pio_write_8(&instance->cuda->ifr, SR_INT); |
||
4610 | svoboda | 99 | |
4626 | svoboda | 100 | switch (instance->xstate) { |
101 | case cx_listen: cuda_irq_listen(irq); break; |
||
102 | case cx_receive: cuda_irq_receive(irq); break; |
||
103 | case cx_rcv_end: cuda_irq_rcv_end(irq, rbuf, &len); |
||
104 | handle = true; break; |
||
105 | } |
||
4610 | svoboda | 106 | |
4626 | svoboda | 107 | spinlock_unlock(&instance->dev_lock); |
4610 | svoboda | 108 | |
4626 | svoboda | 109 | /* Handle an incoming packet. */ |
110 | if (handle) |
||
111 | cuda_packet_handle(instance, rbuf, len); |
||
112 | } |
||
4610 | svoboda | 113 | |
4626 | svoboda | 114 | /** Interrupt in listen state. |
115 | * |
||
116 | * Start packet reception. |
||
117 | */ |
||
118 | static void cuda_irq_listen(irq_t *irq) |
||
119 | { |
||
120 | cuda_instance_t *instance = irq->instance; |
||
121 | cuda_t *dev = instance->cuda; |
||
122 | uint8_t b; |
||
4610 | svoboda | 123 | |
4626 | svoboda | 124 | b = pio_read_8(&dev->b); |
4610 | svoboda | 125 | |
4626 | svoboda | 126 | if ((b & TREQ) != 0) { |
127 | printf("cuda_irq_listen: no TREQ?!\n"); |
||
128 | return; |
||
129 | } |
||
130 | |||
131 | pio_read_8(&dev->sr); |
||
132 | pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP); |
||
133 | instance->xstate = cx_receive; |
||
134 | } |
||
135 | |||
136 | /** Interrupt in receive state. |
||
137 | * |
||
138 | * Receive next byte of packet. |
||
139 | */ |
||
140 | static void cuda_irq_receive(irq_t *irq) |
||
141 | { |
||
142 | cuda_instance_t *instance = irq->instance; |
||
143 | cuda_t *dev = instance->cuda; |
||
144 | uint8_t b, data; |
||
145 | |||
146 | data = pio_read_8(&dev->sr); |
||
147 | if (instance->bidx < CUDA_RCV_BUF_SIZE) |
||
148 | instance->rcv_buf[instance->bidx++] = data; |
||
149 | |||
150 | b = pio_read_8(&dev->b); |
||
151 | |||
152 | if ((b & TREQ) == 0) { |
||
153 | pio_write_8(&dev->b, b ^ TACK); |
||
154 | } else { |
||
4610 | svoboda | 155 | pio_write_8(&dev->b, b | TACK | TIP); |
4626 | svoboda | 156 | instance->xstate = cx_rcv_end; |
157 | } |
||
158 | } |
||
4610 | svoboda | 159 | |
4626 | svoboda | 160 | /** Interrupt in rcv_end state. |
161 | * |
||
162 | * Terminate packet reception. Either go back to listen state or start |
||
163 | * receiving another packet if CUDA has one for us. |
||
164 | */ |
||
165 | static void cuda_irq_rcv_end(irq_t *irq, void *buf, size_t *len) |
||
166 | { |
||
167 | cuda_instance_t *instance = irq->instance; |
||
168 | cuda_t *dev = instance->cuda; |
||
169 | uint8_t data, b; |
||
170 | |||
171 | b = pio_read_8(&dev->b); |
||
172 | data = pio_read_8(&dev->sr); |
||
173 | |||
174 | instance->xstate = cx_listen; |
||
175 | |||
176 | if ((b & TREQ) == 0) { |
||
177 | instance->xstate = cx_receive; |
||
178 | pio_write_8(&dev->b, b & ~TIP); |
||
179 | } else { |
||
180 | instance->xstate = cx_listen; |
||
4610 | svoboda | 181 | } |
182 | |||
4626 | svoboda | 183 | memcpy(buf, instance->rcv_buf, instance->bidx); |
184 | *len = instance->bidx; |
||
185 | instance->bidx = 0; |
||
1480 | palkovsky | 186 | } |
982 | decky | 187 | |
4626 | svoboda | 188 | static void cuda_packet_handle(cuda_instance_t *instance, uint8_t *data, size_t len) |
4610 | svoboda | 189 | { |
4626 | svoboda | 190 | if (data[0] != 0x00 || data[1] != 0x40 || (data[2] != 0x2c |
191 | && data[2] != 0x8c)) |
||
4610 | svoboda | 192 | return; |
193 | |||
194 | /* The packet contains one or two scancodes. */ |
||
195 | if (data[3] != 0xff) |
||
196 | indev_push_character(instance->kbrdin, data[3]); |
||
197 | if (data[4] != 0xff) |
||
198 | indev_push_character(instance->kbrdin, data[4]); |
||
199 | } |
||
200 | |||
4311 | decky | 201 | cuda_instance_t *cuda_init(cuda_t *dev, inr_t inr, cir_t cir, void *cir_arg) |
1628 | decky | 202 | { |
4311 | decky | 203 | cuda_instance_t *instance |
204 | = malloc(sizeof(cuda_instance_t), FRAME_ATOMIC); |
||
205 | if (instance) { |
||
206 | instance->cuda = dev; |
||
207 | instance->kbrdin = NULL; |
||
4626 | svoboda | 208 | instance->xstate = cx_listen; |
209 | instance->bidx = 0; |
||
4610 | svoboda | 210 | |
211 | spinlock_initialize(&instance->dev_lock, "cuda_dev"); |
||
212 | |||
4626 | svoboda | 213 | /* Disable all interrupts from CUDA. */ |
214 | pio_write_8(&dev->ier, IER_CLR | ALL_INT); |
||
215 | |||
4311 | decky | 216 | irq_initialize(&instance->irq); |
217 | instance->irq.devno = device_assign_devno(); |
||
218 | instance->irq.inr = inr; |
||
219 | instance->irq.claim = cuda_claim; |
||
220 | instance->irq.handler = cuda_irq_handler; |
||
221 | instance->irq.instance = instance; |
||
222 | instance->irq.cir = cir; |
||
223 | instance->irq.cir_arg = cir_arg; |
||
4626 | svoboda | 224 | instance->irq.preack = true; |
1633 | decky | 225 | } |
1619 | decky | 226 | |
4311 | decky | 227 | return instance; |
990 | decky | 228 | } |
229 | |||
4311 | decky | 230 | void cuda_wire(cuda_instance_t *instance, indev_t *kbrdin) |
1628 | decky | 231 | { |
4626 | svoboda | 232 | cuda_t *dev = instance->cuda; |
233 | |||
4610 | svoboda | 234 | ASSERT(instance); |
235 | ASSERT(kbrdin); |
||
236 | |||
237 | instance->kbrdin = kbrdin; |
||
238 | irq_register(&instance->irq); |
||
239 | |||
240 | /* Enable SR interrupt. */ |
||
4626 | svoboda | 241 | pio_write_8(&dev->ier, TIP | TREQ); |
242 | pio_write_8(&dev->ier, IER_SET | SR_INT); |
||
1628 | decky | 243 | } |
244 | |||
1734 | decky | 245 | /** @} |
1702 | cejka | 246 | */ |