18 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
26 #include <linux/slab.h>
29 #include <linux/list.h>
39 #include <linux/prefetch.h>
41 #include <asm/byteorder.h>
45 #include <asm/unaligned.h>
47 #include <linux/usb/ch9.h>
54 #ifdef CONFIG_ARCH_PXA
58 #ifdef CONFIG_ARCH_LUBBOCK
90 #define DRIVER_VERSION "30-June-2007"
91 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
96 static const char ep0name [] =
"ep0";
99 #ifdef CONFIG_ARCH_IXP4XX
102 #ifdef CONFIG_ARCH_PXA
103 #error "Can't configure both IXP and PXA"
107 #define clk_get(dev,name) NULL
108 #define clk_enable(clk) do { } while (0)
109 #define clk_disable(clk) do { } while (0)
110 #define clk_put(clk) do { } while (0)
117 #ifdef CONFIG_USB_PXA25X_SMALL
118 #define SIZE_STR " (small)"
129 static void pxa25x_ep_fifo_flush (
struct usb_ep *ep);
133 static void pullup_off(
void)
144 static void pullup_on(
void)
157 bEndpointAddress &= 0xf;
158 if (bEndpointAddress < 8)
161 bEndpointAddress -= 8;
166 static void pio_irq_disable(
int bEndpointAddress)
168 bEndpointAddress &= 0xf;
169 if (bEndpointAddress < 8)
172 bEndpointAddress -= 8;
180 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
182 static inline void udc_set_mask_UDCCR(
int mask)
187 static inline void udc_clear_mask_UDCCR(
int mask)
192 static inline void udc_ack_int_UDCCR(
int mask)
197 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
214 static int pxa25x_ep_enable (
struct usb_ep *_ep,
221 if (!_ep || !desc || _ep->
name == ep0name
224 || ep->
fifo_size < usb_endpoint_maxp (desc)) {
225 DMSG(
"%s, bad ep or descriptor\n", __func__);
233 DMSG(
"%s, %s type mismatch\n", __func__, _ep->
name);
239 && usb_endpoint_maxp (desc)
242 DMSG(
"%s, bad %s maxpacket\n", __func__, _ep->
name);
248 DMSG(
"%s, bogus device state\n", __func__);
255 ep->
ep.maxpacket = usb_endpoint_maxp (desc);
258 pxa25x_ep_fifo_flush (_ep);
266 static int pxa25x_ep_disable (
struct usb_ep *_ep)
272 if (!_ep || !ep->
ep.desc) {
273 DMSG(
"%s, %s not enabled\n", __func__,
274 _ep ? ep->
ep.name :
NULL);
282 pxa25x_ep_fifo_flush (_ep);
303 pxa25x_ep_alloc_request (
struct usb_ep *_ep,
gfp_t gfp_flags)
307 req = kzalloc(
sizeof(*req), gfp_flags);
311 INIT_LIST_HEAD (&req->
queue);
338 list_del_init(&req->
queue);
343 status = req->
req.status;
347 ep->
ep.name, &req->
req, status,
348 req->
req.actual, req->
req.length);
352 req->
req.complete(&ep->
ep, &req->
req);
357 static inline void ep0_idle (
struct pxa25x_udc *dev)
368 buf = req->
req.buf + req->
req.actual;
372 length =
min(req->
req.length - req->
req.actual, max);
392 max = usb_endpoint_maxp(ep->
ep.desc);
395 int is_last, is_short;
397 count = write_packet(ep->
reg_uddr, req, max);
401 is_last = is_short = 1;
414 is_last ?
"/L" :
"", is_short ?
"/S" :
"",
415 req->
req.length - req->
req.actual, req);
428 if (list_empty(&ep->
queue))
451 __func__, tag,
UDCCS0, flags);
461 ep->
dev->stats.write.bytes +=
count;
467 req->
req.length - req->
req.actual, req);
470 if (ep->
dev->req_pending)
475 count = req->
req.length;
478 #ifndef CONFIG_ARCH_IXP4XX
500 }
else if (ep->
dev->req_pending)
501 ep0start(ep->
dev, 0,
"IN");
520 unsigned bufferspace,
count, is_short;
529 buf = req->
req.buf + req->
req.actual;
531 bufferspace = req->
req.length - req->
req.actual;
535 count = 1 + (0x0ff & *ep->
reg_ubcr);
536 req->
req.actual +=
min (count, bufferspace);
539 is_short = (count < ep->
ep.maxpacket);
541 ep->
ep.name, udccs, count,
542 is_short ?
"/S" :
"",
543 req, req->
req.actual, req->
req.length);
544 while (
likely (count-- != 0)) {
553 DMSG(
"%s overflow %d\n",
573 if (is_short || req->
req.actual == req->
req.length) {
575 if (list_empty(&ep->
queue))
595 unsigned bufferspace;
597 buf = req->
req.buf + req->
req.actual;
598 bufferspace = req->
req.length - req->
req.actual;
609 DMSG(
"%s overflow\n", ep->
ep.name);
621 if (req->
req.actual >= req->
req.length)
640 || !list_empty(&req->
queue))) {
641 DMSG(
"%s, bad params\n", __func__);
646 if (
unlikely(!_ep || (!ep->
ep.desc && ep->
ep.name != ep0name))) {
647 DMSG(
"%s, bad ep\n", __func__);
654 DMSG(
"%s, bogus device state\n", __func__);
662 && req->
req.length > usb_endpoint_maxp(ep->
ep.desc)))
675 if (ep->
ep.desc ==
NULL) {
676 unsigned length = _req->
length;
680 dev->
stats.write.ops++;
681 if (write_ep0_fifo(ep, req))
686 dev->
stats.read.ops++;
701 if (length == 0 || ((
UDCCS0 & UDCCS0_RNE) != 0
702 && read_ep0_fifo(ep, req))) {
717 && write_fifo(ep, req))
720 && read_fifo(ep, req)) {
740 static void nuke(
struct pxa25x_ep *ep,
int status)
745 while (!list_empty(&ep->
queue)) {
749 done(ep, req, status);
764 if (!_ep || ep->
ep.name == ep0name)
771 if (&req->
req == _req)
774 if (&req->
req != _req) {
787 static int pxa25x_ep_set_halt(
struct usb_ep *_ep,
int value)
794 || (!ep->
ep.desc && ep->
ep.name != ep0name))
796 DMSG(
"%s, bad ep\n", __func__);
805 DMSG(
"only host can clear %s halt\n", _ep->
name);
813 || !list_empty(&ep->
queue))) {
824 ep->
dev->req_pending = 0;
830 for (i = 0; i < 1000; i += 20) {
842 static int pxa25x_ep_fifo_status(
struct usb_ep *_ep)
848 DMSG(
"%s, bad ep\n", __func__);
861 static void pxa25x_ep_fifo_flush(
struct usb_ep *_ep)
866 if (!_ep || ep->
ep.name == ep0name || !list_empty(&ep->
queue)) {
867 DMSG(
"%s, bad ep\n", __func__);
875 while (((*ep->
reg_udccs) & UDCCS_BO_RNE) != 0)
888 .enable = pxa25x_ep_enable,
889 .disable = pxa25x_ep_disable,
891 .alloc_request = pxa25x_ep_alloc_request,
892 .free_request = pxa25x_ep_free_request,
894 .queue = pxa25x_ep_queue,
895 .dequeue = pxa25x_ep_dequeue,
897 .set_halt = pxa25x_ep_set_halt,
898 .fifo_status = pxa25x_ep_fifo_status,
899 .fifo_flush = pxa25x_ep_fifo_flush,
908 static int pxa25x_udc_get_frame(
struct usb_gadget *_gadget)
910 return ((
UFNRH & 0x07) << 8) | (
UFNRL & 0xff);
913 static int pxa25x_udc_wakeup(
struct usb_gadget *_gadget)
932 DMSG(
"%s\n", is_active ?
"active" :
"inactive");
944 ? udc->
driver->driver.name
946 stop_activity(udc, udc->
driver);
959 static int pxa25x_udc_vbus_session(
struct usb_gadget *_gadget,
int is_active)
965 DMSG(
"vbus %s\n", is_active ?
"supplied" :
"inactive");
971 static int pxa25x_udc_pullup(
struct usb_gadget *_gadget,
int is_active)
978 if (!gpio_is_valid(udc->
mach->gpio_pullup) && !udc->
mach->udc_command)
981 udc->
pullup = (is_active != 0);
990 static int pxa25x_udc_vbus_draw(
struct usb_gadget *_gadget,
unsigned mA)
1006 .get_frame = pxa25x_udc_get_frame,
1007 .wakeup = pxa25x_udc_wakeup,
1008 .vbus_session = pxa25x_udc_vbus_session,
1009 .pullup = pxa25x_udc_pullup,
1010 .vbus_draw = pxa25x_udc_vbus_draw,
1011 .start = pxa25x_start,
1012 .stop = pxa25x_stop,
1017 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1023 unsigned long flags;
1031 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1038 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1043 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1055 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1057 (tmp & UDCCS0_RNE) ?
" rne" :
"",
1068 "udccfr %02X =%s%s\n", tmp,
1076 seq_printf(m,
"ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1077 dev->
stats.write.bytes, dev->
stats.write.ops,
1092 tmp = *dev->
ep [
i].reg_udccs;
1094 "%s max %d %s udccs %02x irqs %lu\n",
1095 ep->
ep.name, usb_endpoint_maxp(desc),
1103 if (list_empty(&ep->
queue)) {
1109 "\treq %p len %d/%d buf %p\n",
1110 &req->
req, req->
req.actual,
1111 req->
req.length, req->
req.buf);
1127 .
open = udc_debugfs_open,
1134 #define create_debug_files(dev) \
1136 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1137 S_IRUGO, NULL, dev, &debug_fops); \
1139 #define remove_debug_files(dev) \
1141 if (dev->debugfs_udc) \
1142 debugfs_remove(dev->debugfs_udc); \
1147 #define create_debug_files(dev) do {} while (0)
1148 #define remove_debug_files(dev) do {} while (0)
1157 static void udc_disable(
struct pxa25x_udc *dev)
1160 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1177 static void udc_reinit(
struct pxa25x_udc *dev)
1182 INIT_LIST_HEAD (&dev->
gadget.ep_list);
1183 INIT_LIST_HEAD (&dev->
gadget.ep0->ep_list);
1195 INIT_LIST_HEAD (&ep->
queue);
1205 static void udc_enable (
struct pxa25x_udc *dev)
1210 udc_ack_int_UDCCR(UDCCR_SUSIR|UDCCR_RESIR);
1214 dev->
stats.irqs = 0;
1223 if (!(
UDCCR & UDCCR_UDA))
1224 udc_ack_int_UDCCR(UDCCR_RSTIR);
1243 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1288 retval = bind(&dev->
gadget, driver);
1290 DMSG(
"bind to driver %s --> error %d\n",
1291 driver->
driver.name, retval);
1299 DMSG(
"registered gadget driver '%s'\n", driver->
driver.name);
1303 retval = otg_set_peripheral(dev->
transceiver->otg,
1306 DMSG(
"can't bind to transceiver\n");
1353 if (!driver || driver != dev->
driver || !driver->
unbind)
1359 stop_activity(dev, driver);
1371 DMSG(
"unregistered gadget driver '%s'\n", driver->
driver.name);
1378 #ifdef CONFIG_ARCH_LUBBOCK
1385 lubbock_vbus_irq(
int irq,
void *_dev)
1406 pxa25x_udc_vbus_session(&dev->
gadget, vbus);
1415 static inline void clear_ep_state (
struct pxa25x_udc *dev)
1426 static void udc_watchdog(
unsigned long _dev)
1432 && (
UDCCS0 & UDCCS0_FST) == 0
1433 && (
UDCCS0 & UDCCS0_SST) == 0) {
1441 static void handle_ep0 (
struct pxa25x_udc *dev)
1452 if (list_empty(&ep->
queue))
1458 if (udccs0 & UDCCS0_SST) {
1485 for (i = 0; i < 8; i++) {
1488 DMSG(
"SETUP %d!\n", i);
1498 u.r.bRequestType,
u.r.bRequest,
1508 switch (
u.r.bRequest) {
1517 clear_ep_state(dev);
1532 DMSG(
"broken set_interface (%d/%d)\n",
1541 ep0start(dev, 0,
"address");
1560 WARNING(
"config change %02x fail %d?\n",
1569 "%02x err %d\n",
UDCCS0, i);
1575 ep0start(dev, UDCCS0_FST|UDCCS0_FTF,
"stall");
1583 ep0start(dev, 0,
"defer");
1585 ep0start(dev, UDCCS0_IPR,
"defer/IPR");
1602 for (i = 0; i < 8; i++)
1607 if (
u.word [0] == 0 &&
u.word [1] == 0)
1629 (
void) write_ep0_fifo(ep, req);
1634 if (udccs0 & UDCCS0_OPR) {
1637 if (read_ep0_fifo(ep, req))
1654 if (udccs0 & UDCCS0_OPR)
1665 static void handle_ep(
struct pxa25x_ep *ep)
1691 completed = write_fifo(ep, req);
1704 completed = read_fifo(ep, req);
1709 }
while (completed);
1720 pxa25x_udc_irq(
int irq,
void *_dev)
1732 if (
unlikely(udccr & UDCCR_SUSIR)) {
1733 udc_ack_int_UDCCR(UDCCR_SUSIR);
1745 if (
unlikely(udccr & UDCCR_RESIR)) {
1746 udc_ack_int_UDCCR(UDCCR_RESIR);
1757 if (
unlikely(udccr & UDCCR_RSTIR)) {
1758 udc_ack_int_UDCCR(UDCCR_RSTIR);
1761 if ((
UDCCR & UDCCR_UDA) == 0) {
1767 stop_activity (dev, dev->
driver);
1788 dev->
ep[0].pio_irqs++;
1794 for (i = 0; i < 8; i++) {
1797 if (i && (usir0 & tmp)) {
1798 handle_ep(&dev->
ep[i]);
1802 #ifndef CONFIG_USB_PXA25X_SMALL
1804 handle_ep(&dev->
ep[i+8]);
1820 static void nop_release (
struct device *dev)
1822 DMSG(
"%s %s\n", __func__, dev_name(dev));
1831 .ops = &pxa25x_udc_ops,
1832 .ep0 = &memory.
ep[0].ep,
1835 .init_name =
"gadget",
1836 .release = nop_release,
1844 .ops = &pxa25x_ep_ops,
1855 .name =
"ep1in-bulk",
1856 .ops = &pxa25x_ep_ops,
1868 .name =
"ep2out-bulk",
1869 .ops = &pxa25x_ep_ops,
1874 .bEndpointAddress = 2,
1880 #ifndef CONFIG_USB_PXA25X_SMALL
1883 .name =
"ep3in-iso",
1884 .ops = &pxa25x_ep_ops,
1896 .name =
"ep4out-iso",
1897 .ops = &pxa25x_ep_ops,
1902 .bEndpointAddress = 4,
1910 .name =
"ep5in-int",
1911 .ops = &pxa25x_ep_ops,
1925 .name =
"ep6in-bulk",
1926 .ops = &pxa25x_ep_ops,
1938 .name =
"ep7out-bulk",
1939 .ops = &pxa25x_ep_ops,
1944 .bEndpointAddress = 7,
1952 .name =
"ep8in-iso",
1953 .ops = &pxa25x_ep_ops,
1965 .name =
"ep9out-iso",
1966 .ops = &pxa25x_ep_ops,
1971 .bEndpointAddress = 9,
1979 .name =
"ep10in-int",
1980 .ops = &pxa25x_ep_ops,
1994 .name =
"ep11in-bulk",
1995 .ops = &pxa25x_ep_ops,
2007 .name =
"ep12out-bulk",
2008 .ops = &pxa25x_ep_ops,
2013 .bEndpointAddress = 12,
2021 .name =
"ep13in-iso",
2022 .ops = &pxa25x_ep_ops,
2034 .name =
"ep14out-iso",
2035 .ops = &pxa25x_ep_ops,
2040 .bEndpointAddress = 14,
2048 .name =
"ep15in-int",
2049 .ops = &pxa25x_ep_ops,
2062 #define CP15R0_VENDOR_MASK 0xffffe000
2064 #if defined(CONFIG_ARCH_PXA)
2065 #define CP15R0_XSCALE_VALUE 0x69052000
2067 #elif defined(CONFIG_ARCH_IXP4XX)
2068 #define CP15R0_XSCALE_VALUE 0x69054000
2072 #define CP15R0_PROD_MASK 0x000003f0
2073 #define PXA25x 0x00000100
2074 #define PXA210 0x00000120
2076 #define CP15R0_REV_MASK 0x0000000f
2078 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2080 #define PXA255_A0 0x00000106
2081 #define PXA250_C0 0x00000105
2082 #define PXA250_B2 0x00000104
2083 #define PXA250_B1 0x00000103
2084 #define PXA250_B0 0x00000102
2085 #define PXA250_A1 0x00000101
2086 #define PXA250_A0 0x00000100
2088 #define PXA210_C0 0x00000125
2089 #define PXA210_B2 0x00000124
2090 #define PXA210_B1 0x00000123
2091 #define PXA210_B0 0x00000122
2092 #define IXP425_A0 0x000001c1
2093 #define IXP425_B0 0x000001f1
2094 #define IXP465_AD 0x00000200
2106 asm(
"mrc%? p15, 0, %0, c0, c0" :
"=r" (chiprev));
2114 #if defined(CONFIG_ARCH_PXA)
2129 #elif defined(CONFIG_ARCH_IXP4XX)
2137 pr_err(
"%s: unrecognized processor: %08x\n",
2148 if (IS_ERR(dev->
clk)) {
2149 retval = PTR_ERR(dev->
clk);
2154 dev->
has_cfr ?
"" :
" (!cfr)",
2160 dev->
mach = pdev->
dev.platform_data;
2164 if (gpio_is_valid(dev->
mach->gpio_pullup)) {
2166 "pca25x_udc GPIO PULLUP"))) {
2168 "can't get pullup gpio %d, err: %d\n",
2169 dev->
mach->gpio_pullup, retval);
2170 goto err_gpio_pullup;
2176 dev->
timer.function = udc_watchdog;
2181 dev->
gadget.dev.dma_mask = pdev->
dev.dma_mask;
2184 platform_set_drvdata(pdev, dev);
2195 pr_err(
"%s: can't get irq %d, err %d\n",
2201 #ifdef CONFIG_ARCH_LUBBOCK
2202 if (machine_is_lubbock()) {
2206 pr_err(
"%s: can't get irq %i, err %d\n",
2213 pr_err(
"%s: can't get irq %i, err %d\n",
2226 #ifdef CONFIG_ARCH_LUBBOCK
2233 if (gpio_is_valid(dev->
mach->gpio_pullup))
2252 struct pxa25x_udc *dev = platform_get_drvdata(pdev);
2267 #ifdef CONFIG_ARCH_LUBBOCK
2268 if (machine_is_lubbock()) {
2273 if (gpio_is_valid(dev->
mach->gpio_pullup))
2283 platform_set_drvdata(pdev,
NULL);
2306 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2307 unsigned long flags;
2309 if (!gpio_is_valid(udc->
mach->gpio_pullup) && !udc->
mach->udc_command)
2310 WARNING(
"USB host won't detect disconnect!\n");
2322 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2323 unsigned long flags;
2334 #define pxa25x_udc_suspend NULL
2335 #define pxa25x_udc_resume NULL
2341 .shutdown = pxa25x_udc_shutdown,
2342 .remove =
__exit_p(pxa25x_udc_remove),
2347 .name =
"pxa25x-udc",
2351 static int __init udc_init(
void)
2358 static void __exit udc_exit(
void)
2365 MODULE_AUTHOR(
"Frank Becker, Robert Schwebel, David Brownell");