]>
Commit | Line | Data |
---|---|---|
aa69a809 DL |
1 | /* |
2 | * ci13xxx_udc.c - MIPS USB IP core family device controller | |
3 | * | |
4 | * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved. | |
5 | * | |
6 | * Author: David Lopo | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | /* | |
14 | * Description: MIPS USB IP core family device controller | |
15 | * Currently it only supports IP part number CI13412 | |
16 | * | |
17 | * This driver is composed of several blocks: | |
18 | * - HW: hardware interface | |
19 | * - DBG: debug facilities (optional) | |
20 | * - UTIL: utilities | |
21 | * - ISR: interrupts handling | |
22 | * - ENDPT: endpoint operations (Gadget API) | |
23 | * - GADGET: gadget operations (Gadget API) | |
24 | * - BUS: bus glue code, bus abstraction layer | |
aa69a809 DL |
25 | * |
26 | * Compile Options | |
27 | * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities | |
28 | * - STALL_IN: non-empty bulk-in pipes cannot be halted | |
29 | * if defined mass storage compliance succeeds but with warnings | |
30 | * => case 4: Hi > Dn | |
31 | * => case 5: Hi > Di | |
32 | * => case 8: Hi <> Do | |
33 | * if undefined usbtest 13 fails | |
34 | * - TRACE: enable function tracing (depends on DEBUG) | |
35 | * | |
36 | * Main Features | |
37 | * - Chapter 9 & Mass Storage Compliance with Gadget File Storage | |
38 | * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined) | |
39 | * - Normal & LPM support | |
40 | * | |
41 | * USBTEST Report | |
42 | * - OK: 0-12, 13 (STALL_IN defined) & 14 | |
43 | * - Not Supported: 15 & 16 (ISO) | |
44 | * | |
45 | * TODO List | |
46 | * - OTG | |
47 | * - Isochronous & Interrupt Traffic | |
48 | * - Handle requests which spawns into several TDs | |
49 | * - GET_STATUS(device) - always reports 0 | |
50 | * - Gadget API (majority of optional features) | |
51 | * - Suspend & Remote Wakeup | |
52 | */ | |
36825a2d | 53 | #include <linux/delay.h> |
aa69a809 DL |
54 | #include <linux/device.h> |
55 | #include <linux/dmapool.h> | |
56 | #include <linux/dma-mapping.h> | |
57 | #include <linux/init.h> | |
58 | #include <linux/interrupt.h> | |
aa69a809 DL |
59 | #include <linux/io.h> |
60 | #include <linux/irq.h> | |
61 | #include <linux/kernel.h> | |
5a0e3ad6 | 62 | #include <linux/slab.h> |
c036019e | 63 | #include <linux/pm_runtime.h> |
aa69a809 DL |
64 | #include <linux/usb/ch9.h> |
65 | #include <linux/usb/gadget.h> | |
f01ef574 | 66 | #include <linux/usb/otg.h> |
aa69a809 DL |
67 | |
68 | #include "ci13xxx_udc.h" | |
69 | ||
70 | ||
71 | /****************************************************************************** | |
72 | * DEFINE | |
73 | *****************************************************************************/ | |
74 | /* ctrl register bank access */ | |
75 | static DEFINE_SPINLOCK(udc_lock); | |
76 | ||
aa69a809 DL |
77 | /* control endpoint description */ |
78 | static const struct usb_endpoint_descriptor | |
ca9cfea0 | 79 | ctrl_endpt_out_desc = { |
aa69a809 DL |
80 | .bLength = USB_DT_ENDPOINT_SIZE, |
81 | .bDescriptorType = USB_DT_ENDPOINT, | |
82 | ||
ca9cfea0 PK |
83 | .bEndpointAddress = USB_DIR_OUT, |
84 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, | |
85 | .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), | |
86 | }; | |
87 | ||
88 | static const struct usb_endpoint_descriptor | |
89 | ctrl_endpt_in_desc = { | |
90 | .bLength = USB_DT_ENDPOINT_SIZE, | |
91 | .bDescriptorType = USB_DT_ENDPOINT, | |
92 | ||
93 | .bEndpointAddress = USB_DIR_IN, | |
aa69a809 DL |
94 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
95 | .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), | |
96 | }; | |
97 | ||
98 | /* UDC descriptor */ | |
99 | static struct ci13xxx *_udc; | |
100 | ||
101 | /* Interrupt statistics */ | |
102 | #define ISR_MASK 0x1F | |
103 | static struct { | |
104 | u32 test; | |
105 | u32 ui; | |
106 | u32 uei; | |
107 | u32 pci; | |
108 | u32 uri; | |
109 | u32 sli; | |
110 | u32 none; | |
111 | struct { | |
112 | u32 cnt; | |
113 | u32 buf[ISR_MASK+1]; | |
114 | u32 idx; | |
115 | } hndl; | |
116 | } isr_statistics; | |
117 | ||
118 | /** | |
119 | * ffs_nr: find first (least significant) bit set | |
120 | * @x: the word to search | |
121 | * | |
122 | * This function returns bit number (instead of position) | |
123 | */ | |
124 | static int ffs_nr(u32 x) | |
125 | { | |
126 | int n = ffs(x); | |
127 | ||
128 | return n ? n-1 : 32; | |
129 | } | |
130 | ||
131 | /****************************************************************************** | |
132 | * HW block | |
133 | *****************************************************************************/ | |
134 | /* register bank descriptor */ | |
135 | static struct { | |
136 | unsigned lpm; /* is LPM? */ | |
137 | void __iomem *abs; /* bus map offset */ | |
138 | void __iomem *cap; /* bus map offset + CAP offset + CAP data */ | |
139 | size_t size; /* bank size */ | |
140 | } hw_bank; | |
141 | ||
f01ef574 PK |
142 | /* MSM specific */ |
143 | #define ABS_AHBBURST (0x0090UL) | |
144 | #define ABS_AHBMODE (0x0098UL) | |
aa69a809 DL |
145 | /* UDC register map */ |
146 | #define ABS_CAPLENGTH (0x100UL) | |
147 | #define ABS_HCCPARAMS (0x108UL) | |
148 | #define ABS_DCCPARAMS (0x124UL) | |
149 | #define ABS_TESTMODE (hw_bank.lpm ? 0x0FCUL : 0x138UL) | |
150 | /* offset to CAPLENTGH (addr + data) */ | |
151 | #define CAP_USBCMD (0x000UL) | |
152 | #define CAP_USBSTS (0x004UL) | |
153 | #define CAP_USBINTR (0x008UL) | |
154 | #define CAP_DEVICEADDR (0x014UL) | |
155 | #define CAP_ENDPTLISTADDR (0x018UL) | |
156 | #define CAP_PORTSC (0x044UL) | |
f23e649b | 157 | #define CAP_DEVLC (0x084UL) |
aa69a809 DL |
158 | #define CAP_USBMODE (hw_bank.lpm ? 0x0C8UL : 0x068UL) |
159 | #define CAP_ENDPTSETUPSTAT (hw_bank.lpm ? 0x0D8UL : 0x06CUL) | |
160 | #define CAP_ENDPTPRIME (hw_bank.lpm ? 0x0DCUL : 0x070UL) | |
161 | #define CAP_ENDPTFLUSH (hw_bank.lpm ? 0x0E0UL : 0x074UL) | |
162 | #define CAP_ENDPTSTAT (hw_bank.lpm ? 0x0E4UL : 0x078UL) | |
163 | #define CAP_ENDPTCOMPLETE (hw_bank.lpm ? 0x0E8UL : 0x07CUL) | |
164 | #define CAP_ENDPTCTRL (hw_bank.lpm ? 0x0ECUL : 0x080UL) | |
165 | #define CAP_LAST (hw_bank.lpm ? 0x12CUL : 0x0C0UL) | |
166 | ||
167 | /* maximum number of enpoints: valid only after hw_device_reset() */ | |
168 | static unsigned hw_ep_max; | |
169 | ||
170 | /** | |
171 | * hw_ep_bit: calculates the bit number | |
172 | * @num: endpoint number | |
173 | * @dir: endpoint direction | |
174 | * | |
175 | * This function returns bit number | |
176 | */ | |
177 | static inline int hw_ep_bit(int num, int dir) | |
178 | { | |
179 | return num + (dir ? 16 : 0); | |
180 | } | |
181 | ||
182 | /** | |
183 | * hw_aread: reads from register bitfield | |
184 | * @addr: address relative to bus map | |
185 | * @mask: bitfield mask | |
186 | * | |
187 | * This function returns register bitfield data | |
188 | */ | |
189 | static u32 hw_aread(u32 addr, u32 mask) | |
190 | { | |
191 | return ioread32(addr + hw_bank.abs) & mask; | |
192 | } | |
193 | ||
194 | /** | |
195 | * hw_awrite: writes to register bitfield | |
196 | * @addr: address relative to bus map | |
197 | * @mask: bitfield mask | |
198 | * @data: new data | |
199 | */ | |
200 | static void hw_awrite(u32 addr, u32 mask, u32 data) | |
201 | { | |
202 | iowrite32(hw_aread(addr, ~mask) | (data & mask), | |
203 | addr + hw_bank.abs); | |
204 | } | |
205 | ||
206 | /** | |
207 | * hw_cread: reads from register bitfield | |
208 | * @addr: address relative to CAP offset plus content | |
209 | * @mask: bitfield mask | |
210 | * | |
211 | * This function returns register bitfield data | |
212 | */ | |
213 | static u32 hw_cread(u32 addr, u32 mask) | |
214 | { | |
215 | return ioread32(addr + hw_bank.cap) & mask; | |
216 | } | |
217 | ||
218 | /** | |
219 | * hw_cwrite: writes to register bitfield | |
220 | * @addr: address relative to CAP offset plus content | |
221 | * @mask: bitfield mask | |
222 | * @data: new data | |
223 | */ | |
224 | static void hw_cwrite(u32 addr, u32 mask, u32 data) | |
225 | { | |
226 | iowrite32(hw_cread(addr, ~mask) | (data & mask), | |
227 | addr + hw_bank.cap); | |
228 | } | |
229 | ||
230 | /** | |
231 | * hw_ctest_and_clear: tests & clears register bitfield | |
232 | * @addr: address relative to CAP offset plus content | |
233 | * @mask: bitfield mask | |
234 | * | |
235 | * This function returns register bitfield data | |
236 | */ | |
237 | static u32 hw_ctest_and_clear(u32 addr, u32 mask) | |
238 | { | |
239 | u32 reg = hw_cread(addr, mask); | |
240 | ||
241 | iowrite32(reg, addr + hw_bank.cap); | |
242 | return reg; | |
243 | } | |
244 | ||
245 | /** | |
246 | * hw_ctest_and_write: tests & writes register bitfield | |
247 | * @addr: address relative to CAP offset plus content | |
248 | * @mask: bitfield mask | |
249 | * @data: new data | |
250 | * | |
251 | * This function returns register bitfield data | |
252 | */ | |
253 | static u32 hw_ctest_and_write(u32 addr, u32 mask, u32 data) | |
254 | { | |
255 | u32 reg = hw_cread(addr, ~0); | |
256 | ||
257 | iowrite32((reg & ~mask) | (data & mask), addr + hw_bank.cap); | |
258 | return (reg & mask) >> ffs_nr(mask); | |
259 | } | |
260 | ||
f01ef574 | 261 | static int hw_device_init(void __iomem *base) |
aa69a809 DL |
262 | { |
263 | u32 reg; | |
264 | ||
265 | /* bank is a module variable */ | |
266 | hw_bank.abs = base; | |
267 | ||
268 | hw_bank.cap = hw_bank.abs; | |
269 | hw_bank.cap += ABS_CAPLENGTH; | |
270 | hw_bank.cap += ioread8(hw_bank.cap); | |
271 | ||
272 | reg = hw_aread(ABS_HCCPARAMS, HCCPARAMS_LEN) >> ffs_nr(HCCPARAMS_LEN); | |
273 | hw_bank.lpm = reg; | |
274 | hw_bank.size = hw_bank.cap - hw_bank.abs; | |
275 | hw_bank.size += CAP_LAST; | |
276 | hw_bank.size /= sizeof(u32); | |
277 | ||
f01ef574 | 278 | reg = hw_aread(ABS_DCCPARAMS, DCCPARAMS_DEN) >> ffs_nr(DCCPARAMS_DEN); |
ca9cfea0 | 279 | hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */ |
f01ef574 | 280 | |
ca9cfea0 PK |
281 | if (hw_ep_max == 0 || hw_ep_max > ENDPT_MAX) |
282 | return -ENODEV; | |
f01ef574 PK |
283 | |
284 | /* setup lock mode ? */ | |
285 | ||
286 | /* ENDPTSETUPSTAT is '0' by default */ | |
287 | ||
288 | /* HCSPARAMS.bf.ppc SHOULD BE zero for device */ | |
289 | ||
290 | return 0; | |
291 | } | |
292 | /** | |
293 | * hw_device_reset: resets chip (execute without interruption) | |
294 | * @base: register base address | |
295 | * | |
296 | * This function returns an error code | |
297 | */ | |
298 | static int hw_device_reset(struct ci13xxx *udc) | |
299 | { | |
aa69a809 DL |
300 | /* should flush & stop before reset */ |
301 | hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0); | |
302 | hw_cwrite(CAP_USBCMD, USBCMD_RS, 0); | |
303 | ||
304 | hw_cwrite(CAP_USBCMD, USBCMD_RST, USBCMD_RST); | |
305 | while (hw_cread(CAP_USBCMD, USBCMD_RST)) | |
306 | udelay(10); /* not RTOS friendly */ | |
307 | ||
f01ef574 PK |
308 | |
309 | if (udc->udc_driver->notify_event) | |
310 | udc->udc_driver->notify_event(udc, | |
311 | CI13XXX_CONTROLLER_RESET_EVENT); | |
312 | ||
8c2387a7 | 313 | if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING) |
f01ef574 PK |
314 | hw_cwrite(CAP_USBMODE, USBMODE_SDIS, USBMODE_SDIS); |
315 | ||
aa69a809 DL |
316 | /* USBMODE should be configured step by step */ |
317 | hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE); | |
318 | hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE); | |
319 | hw_cwrite(CAP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); /* HW >= 2.3 */ | |
320 | ||
321 | if (hw_cread(CAP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) { | |
322 | pr_err("cannot enter in device mode"); | |
323 | pr_err("lpm = %i", hw_bank.lpm); | |
324 | return -ENODEV; | |
325 | } | |
326 | ||
aa69a809 DL |
327 | return 0; |
328 | } | |
329 | ||
330 | /** | |
331 | * hw_device_state: enables/disables interrupts & starts/stops device (execute | |
332 | * without interruption) | |
333 | * @dma: 0 => disable, !0 => enable and set dma engine | |
334 | * | |
335 | * This function returns an error code | |
336 | */ | |
337 | static int hw_device_state(u32 dma) | |
338 | { | |
339 | if (dma) { | |
340 | hw_cwrite(CAP_ENDPTLISTADDR, ~0, dma); | |
341 | /* interrupt, error, port change, reset, sleep/suspend */ | |
342 | hw_cwrite(CAP_USBINTR, ~0, | |
343 | USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI); | |
344 | hw_cwrite(CAP_USBCMD, USBCMD_RS, USBCMD_RS); | |
345 | } else { | |
346 | hw_cwrite(CAP_USBCMD, USBCMD_RS, 0); | |
347 | hw_cwrite(CAP_USBINTR, ~0, 0); | |
348 | } | |
349 | return 0; | |
350 | } | |
351 | ||
352 | /** | |
353 | * hw_ep_flush: flush endpoint fifo (execute without interruption) | |
354 | * @num: endpoint number | |
355 | * @dir: endpoint direction | |
356 | * | |
357 | * This function returns an error code | |
358 | */ | |
359 | static int hw_ep_flush(int num, int dir) | |
360 | { | |
361 | int n = hw_ep_bit(num, dir); | |
362 | ||
363 | do { | |
364 | /* flush any pending transfer */ | |
365 | hw_cwrite(CAP_ENDPTFLUSH, BIT(n), BIT(n)); | |
366 | while (hw_cread(CAP_ENDPTFLUSH, BIT(n))) | |
367 | cpu_relax(); | |
368 | } while (hw_cread(CAP_ENDPTSTAT, BIT(n))); | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
373 | /** | |
374 | * hw_ep_disable: disables endpoint (execute without interruption) | |
375 | * @num: endpoint number | |
376 | * @dir: endpoint direction | |
377 | * | |
378 | * This function returns an error code | |
379 | */ | |
380 | static int hw_ep_disable(int num, int dir) | |
381 | { | |
382 | hw_ep_flush(num, dir); | |
383 | hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32), | |
384 | dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0); | |
385 | return 0; | |
386 | } | |
387 | ||
388 | /** | |
389 | * hw_ep_enable: enables endpoint (execute without interruption) | |
390 | * @num: endpoint number | |
391 | * @dir: endpoint direction | |
392 | * @type: endpoint type | |
393 | * | |
394 | * This function returns an error code | |
395 | */ | |
396 | static int hw_ep_enable(int num, int dir, int type) | |
397 | { | |
398 | u32 mask, data; | |
399 | ||
400 | if (dir) { | |
401 | mask = ENDPTCTRL_TXT; /* type */ | |
402 | data = type << ffs_nr(mask); | |
403 | ||
404 | mask |= ENDPTCTRL_TXS; /* unstall */ | |
405 | mask |= ENDPTCTRL_TXR; /* reset data toggle */ | |
406 | data |= ENDPTCTRL_TXR; | |
407 | mask |= ENDPTCTRL_TXE; /* enable */ | |
408 | data |= ENDPTCTRL_TXE; | |
409 | } else { | |
410 | mask = ENDPTCTRL_RXT; /* type */ | |
411 | data = type << ffs_nr(mask); | |
412 | ||
413 | mask |= ENDPTCTRL_RXS; /* unstall */ | |
414 | mask |= ENDPTCTRL_RXR; /* reset data toggle */ | |
415 | data |= ENDPTCTRL_RXR; | |
416 | mask |= ENDPTCTRL_RXE; /* enable */ | |
417 | data |= ENDPTCTRL_RXE; | |
418 | } | |
419 | hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32), mask, data); | |
420 | return 0; | |
421 | } | |
422 | ||
423 | /** | |
424 | * hw_ep_get_halt: return endpoint halt status | |
425 | * @num: endpoint number | |
426 | * @dir: endpoint direction | |
427 | * | |
428 | * This function returns 1 if endpoint halted | |
429 | */ | |
430 | static int hw_ep_get_halt(int num, int dir) | |
431 | { | |
432 | u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS; | |
433 | ||
434 | return hw_cread(CAP_ENDPTCTRL + num * sizeof(u32), mask) ? 1 : 0; | |
435 | } | |
436 | ||
aa69a809 DL |
437 | /** |
438 | * hw_test_and_clear_setup_status: test & clear setup status (execute without | |
439 | * interruption) | |
440 | * @n: bit number (endpoint) | |
441 | * | |
442 | * This function returns setup status | |
443 | */ | |
444 | static int hw_test_and_clear_setup_status(int n) | |
445 | { | |
446 | return hw_ctest_and_clear(CAP_ENDPTSETUPSTAT, BIT(n)); | |
447 | } | |
448 | ||
449 | /** | |
450 | * hw_ep_prime: primes endpoint (execute without interruption) | |
451 | * @num: endpoint number | |
452 | * @dir: endpoint direction | |
453 | * @is_ctrl: true if control endpoint | |
454 | * | |
455 | * This function returns an error code | |
456 | */ | |
457 | static int hw_ep_prime(int num, int dir, int is_ctrl) | |
458 | { | |
459 | int n = hw_ep_bit(num, dir); | |
460 | ||
aa69a809 DL |
461 | if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num))) |
462 | return -EAGAIN; | |
463 | ||
464 | hw_cwrite(CAP_ENDPTPRIME, BIT(n), BIT(n)); | |
465 | ||
466 | while (hw_cread(CAP_ENDPTPRIME, BIT(n))) | |
467 | cpu_relax(); | |
468 | if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num))) | |
469 | return -EAGAIN; | |
470 | ||
471 | /* status shoult be tested according with manual but it doesn't work */ | |
472 | return 0; | |
473 | } | |
474 | ||
475 | /** | |
476 | * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute | |
477 | * without interruption) | |
478 | * @num: endpoint number | |
479 | * @dir: endpoint direction | |
480 | * @value: true => stall, false => unstall | |
481 | * | |
482 | * This function returns an error code | |
483 | */ | |
484 | static int hw_ep_set_halt(int num, int dir, int value) | |
485 | { | |
486 | if (value != 0 && value != 1) | |
487 | return -EINVAL; | |
488 | ||
489 | do { | |
490 | u32 addr = CAP_ENDPTCTRL + num * sizeof(u32); | |
491 | u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS; | |
492 | u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR; | |
493 | ||
494 | /* data toggle - reserved for EP0 but it's in ESS */ | |
495 | hw_cwrite(addr, mask_xs|mask_xr, value ? mask_xs : mask_xr); | |
496 | ||
497 | } while (value != hw_ep_get_halt(num, dir)); | |
498 | ||
499 | return 0; | |
500 | } | |
501 | ||
502 | /** | |
503 | * hw_intr_clear: disables interrupt & clears interrupt status (execute without | |
504 | * interruption) | |
505 | * @n: interrupt bit | |
506 | * | |
507 | * This function returns an error code | |
508 | */ | |
509 | static int hw_intr_clear(int n) | |
510 | { | |
511 | if (n >= REG_BITS) | |
512 | return -EINVAL; | |
513 | ||
514 | hw_cwrite(CAP_USBINTR, BIT(n), 0); | |
515 | hw_cwrite(CAP_USBSTS, BIT(n), BIT(n)); | |
516 | return 0; | |
517 | } | |
518 | ||
519 | /** | |
520 | * hw_intr_force: enables interrupt & forces interrupt status (execute without | |
521 | * interruption) | |
522 | * @n: interrupt bit | |
523 | * | |
524 | * This function returns an error code | |
525 | */ | |
526 | static int hw_intr_force(int n) | |
527 | { | |
528 | if (n >= REG_BITS) | |
529 | return -EINVAL; | |
530 | ||
531 | hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE); | |
532 | hw_cwrite(CAP_USBINTR, BIT(n), BIT(n)); | |
533 | hw_cwrite(CAP_USBSTS, BIT(n), BIT(n)); | |
534 | hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, 0); | |
535 | return 0; | |
536 | } | |
537 | ||
538 | /** | |
539 | * hw_is_port_high_speed: test if port is high speed | |
540 | * | |
541 | * This function returns true if high speed port | |
542 | */ | |
543 | static int hw_port_is_high_speed(void) | |
544 | { | |
545 | return hw_bank.lpm ? hw_cread(CAP_DEVLC, DEVLC_PSPD) : | |
546 | hw_cread(CAP_PORTSC, PORTSC_HSP); | |
547 | } | |
548 | ||
549 | /** | |
550 | * hw_port_test_get: reads port test mode value | |
551 | * | |
552 | * This function returns port test mode value | |
553 | */ | |
554 | static u8 hw_port_test_get(void) | |
555 | { | |
556 | return hw_cread(CAP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC); | |
557 | } | |
558 | ||
559 | /** | |
560 | * hw_port_test_set: writes port test mode (execute without interruption) | |
561 | * @mode: new value | |
562 | * | |
563 | * This function returns an error code | |
564 | */ | |
565 | static int hw_port_test_set(u8 mode) | |
566 | { | |
567 | const u8 TEST_MODE_MAX = 7; | |
568 | ||
569 | if (mode > TEST_MODE_MAX) | |
570 | return -EINVAL; | |
571 | ||
572 | hw_cwrite(CAP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC)); | |
573 | return 0; | |
574 | } | |
575 | ||
576 | /** | |
577 | * hw_read_intr_enable: returns interrupt enable register | |
578 | * | |
579 | * This function returns register data | |
580 | */ | |
581 | static u32 hw_read_intr_enable(void) | |
582 | { | |
583 | return hw_cread(CAP_USBINTR, ~0); | |
584 | } | |
585 | ||
586 | /** | |
587 | * hw_read_intr_status: returns interrupt status register | |
588 | * | |
589 | * This function returns register data | |
590 | */ | |
591 | static u32 hw_read_intr_status(void) | |
592 | { | |
593 | return hw_cread(CAP_USBSTS, ~0); | |
594 | } | |
595 | ||
596 | /** | |
597 | * hw_register_read: reads all device registers (execute without interruption) | |
598 | * @buf: destination buffer | |
599 | * @size: buffer size | |
600 | * | |
601 | * This function returns number of registers read | |
602 | */ | |
603 | static size_t hw_register_read(u32 *buf, size_t size) | |
604 | { | |
605 | unsigned i; | |
606 | ||
607 | if (size > hw_bank.size) | |
608 | size = hw_bank.size; | |
609 | ||
610 | for (i = 0; i < size; i++) | |
611 | buf[i] = hw_aread(i * sizeof(u32), ~0); | |
612 | ||
613 | return size; | |
614 | } | |
615 | ||
616 | /** | |
617 | * hw_register_write: writes to register | |
618 | * @addr: register address | |
619 | * @data: register value | |
620 | * | |
621 | * This function returns an error code | |
622 | */ | |
623 | static int hw_register_write(u16 addr, u32 data) | |
624 | { | |
625 | /* align */ | |
626 | addr /= sizeof(u32); | |
627 | ||
628 | if (addr >= hw_bank.size) | |
629 | return -EINVAL; | |
630 | ||
631 | /* align */ | |
632 | addr *= sizeof(u32); | |
633 | ||
634 | hw_awrite(addr, ~0, data); | |
635 | return 0; | |
636 | } | |
637 | ||
638 | /** | |
639 | * hw_test_and_clear_complete: test & clear complete status (execute without | |
640 | * interruption) | |
641 | * @n: bit number (endpoint) | |
642 | * | |
643 | * This function returns complete status | |
644 | */ | |
645 | static int hw_test_and_clear_complete(int n) | |
646 | { | |
647 | return hw_ctest_and_clear(CAP_ENDPTCOMPLETE, BIT(n)); | |
648 | } | |
649 | ||
650 | /** | |
651 | * hw_test_and_clear_intr_active: test & clear active interrupts (execute | |
652 | * without interruption) | |
653 | * | |
654 | * This function returns active interrutps | |
655 | */ | |
656 | static u32 hw_test_and_clear_intr_active(void) | |
657 | { | |
658 | u32 reg = hw_read_intr_status() & hw_read_intr_enable(); | |
659 | ||
660 | hw_cwrite(CAP_USBSTS, ~0, reg); | |
661 | return reg; | |
662 | } | |
663 | ||
664 | /** | |
665 | * hw_test_and_clear_setup_guard: test & clear setup guard (execute without | |
666 | * interruption) | |
667 | * | |
668 | * This function returns guard value | |
669 | */ | |
670 | static int hw_test_and_clear_setup_guard(void) | |
671 | { | |
672 | return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, 0); | |
673 | } | |
674 | ||
675 | /** | |
676 | * hw_test_and_set_setup_guard: test & set setup guard (execute without | |
677 | * interruption) | |
678 | * | |
679 | * This function returns guard value | |
680 | */ | |
681 | static int hw_test_and_set_setup_guard(void) | |
682 | { | |
683 | return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, USBCMD_SUTW); | |
684 | } | |
685 | ||
686 | /** | |
687 | * hw_usb_set_address: configures USB address (execute without interruption) | |
688 | * @value: new USB address | |
689 | * | |
690 | * This function returns an error code | |
691 | */ | |
692 | static int hw_usb_set_address(u8 value) | |
693 | { | |
694 | /* advance */ | |
695 | hw_cwrite(CAP_DEVICEADDR, DEVICEADDR_USBADR | DEVICEADDR_USBADRA, | |
696 | value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA); | |
697 | return 0; | |
698 | } | |
699 | ||
700 | /** | |
701 | * hw_usb_reset: restart device after a bus reset (execute without | |
702 | * interruption) | |
703 | * | |
704 | * This function returns an error code | |
705 | */ | |
706 | static int hw_usb_reset(void) | |
707 | { | |
708 | hw_usb_set_address(0); | |
709 | ||
710 | /* ESS flushes only at end?!? */ | |
711 | hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0); /* flush all EPs */ | |
712 | ||
713 | /* clear setup token semaphores */ | |
714 | hw_cwrite(CAP_ENDPTSETUPSTAT, 0, 0); /* writes its content */ | |
715 | ||
716 | /* clear complete status */ | |
717 | hw_cwrite(CAP_ENDPTCOMPLETE, 0, 0); /* writes its content */ | |
718 | ||
719 | /* wait until all bits cleared */ | |
720 | while (hw_cread(CAP_ENDPTPRIME, ~0)) | |
721 | udelay(10); /* not RTOS friendly */ | |
722 | ||
723 | /* reset all endpoints ? */ | |
724 | ||
725 | /* reset internal status and wait for further instructions | |
726 | no need to verify the port reset status (ESS does it) */ | |
727 | ||
728 | return 0; | |
729 | } | |
730 | ||
731 | /****************************************************************************** | |
732 | * DBG block | |
733 | *****************************************************************************/ | |
734 | /** | |
735 | * show_device: prints information about device capabilities and status | |
736 | * | |
737 | * Check "device.h" for details | |
738 | */ | |
739 | static ssize_t show_device(struct device *dev, struct device_attribute *attr, | |
740 | char *buf) | |
741 | { | |
742 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); | |
743 | struct usb_gadget *gadget = &udc->gadget; | |
744 | int n = 0; | |
745 | ||
746 | dbg_trace("[%s] %p\n", __func__, buf); | |
747 | if (attr == NULL || buf == NULL) { | |
748 | dev_err(dev, "[%s] EINVAL\n", __func__); | |
749 | return 0; | |
750 | } | |
751 | ||
752 | n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n", | |
753 | gadget->speed); | |
754 | n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n", | |
755 | gadget->is_dualspeed); | |
756 | n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n", | |
757 | gadget->is_otg); | |
758 | n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n", | |
759 | gadget->is_a_peripheral); | |
760 | n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n", | |
761 | gadget->b_hnp_enable); | |
762 | n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n", | |
763 | gadget->a_hnp_support); | |
764 | n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n", | |
765 | gadget->a_alt_hnp_support); | |
766 | n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n", | |
767 | (gadget->name ? gadget->name : "")); | |
768 | ||
769 | return n; | |
770 | } | |
771 | static DEVICE_ATTR(device, S_IRUSR, show_device, NULL); | |
772 | ||
773 | /** | |
774 | * show_driver: prints information about attached gadget (if any) | |
775 | * | |
776 | * Check "device.h" for details | |
777 | */ | |
778 | static ssize_t show_driver(struct device *dev, struct device_attribute *attr, | |
779 | char *buf) | |
780 | { | |
781 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); | |
782 | struct usb_gadget_driver *driver = udc->driver; | |
783 | int n = 0; | |
784 | ||
785 | dbg_trace("[%s] %p\n", __func__, buf); | |
786 | if (attr == NULL || buf == NULL) { | |
787 | dev_err(dev, "[%s] EINVAL\n", __func__); | |
788 | return 0; | |
789 | } | |
790 | ||
791 | if (driver == NULL) | |
792 | return scnprintf(buf, PAGE_SIZE, | |
793 | "There is no gadget attached!\n"); | |
794 | ||
795 | n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n", | |
796 | (driver->function ? driver->function : "")); | |
797 | n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n", | |
798 | driver->speed); | |
799 | ||
800 | return n; | |
801 | } | |
802 | static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL); | |
803 | ||
804 | /* Maximum event message length */ | |
805 | #define DBG_DATA_MSG 64UL | |
806 | ||
807 | /* Maximum event messages */ | |
808 | #define DBG_DATA_MAX 128UL | |
809 | ||
810 | /* Event buffer descriptor */ | |
811 | static struct { | |
812 | char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */ | |
813 | unsigned idx; /* index */ | |
814 | unsigned tty; /* print to console? */ | |
815 | rwlock_t lck; /* lock */ | |
816 | } dbg_data = { | |
817 | .idx = 0, | |
818 | .tty = 0, | |
819 | .lck = __RW_LOCK_UNLOCKED(lck) | |
820 | }; | |
821 | ||
822 | /** | |
823 | * dbg_dec: decrements debug event index | |
824 | * @idx: buffer index | |
825 | */ | |
826 | static void dbg_dec(unsigned *idx) | |
827 | { | |
828 | *idx = (*idx - 1) & (DBG_DATA_MAX-1); | |
829 | } | |
830 | ||
831 | /** | |
832 | * dbg_inc: increments debug event index | |
833 | * @idx: buffer index | |
834 | */ | |
835 | static void dbg_inc(unsigned *idx) | |
836 | { | |
837 | *idx = (*idx + 1) & (DBG_DATA_MAX-1); | |
838 | } | |
839 | ||
840 | /** | |
841 | * dbg_print: prints the common part of the event | |
842 | * @addr: endpoint address | |
843 | * @name: event name | |
844 | * @status: status | |
845 | * @extra: extra information | |
846 | */ | |
847 | static void dbg_print(u8 addr, const char *name, int status, const char *extra) | |
848 | { | |
849 | struct timeval tval; | |
850 | unsigned int stamp; | |
851 | unsigned long flags; | |
852 | ||
853 | write_lock_irqsave(&dbg_data.lck, flags); | |
854 | ||
855 | do_gettimeofday(&tval); | |
856 | stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */ | |
857 | stamp = stamp * 1000000 + tval.tv_usec; | |
858 | ||
859 | scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG, | |
860 |