12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
19 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <linux/prefetch.h>
28 #include <asm/byteorder.h>
29 #include <mach/hardware.h>
32 #include <linux/usb/ch9.h>
75 #define DRIVER_VERSION "2008-04-18"
76 #define DRIVER_DESC "PXA 27x USB Device Controller driver"
81 static void handle_ep(
struct pxa_ep *ep);
86 #ifdef CONFIG_USB_GADGET_DEBUG_FS
92 static int state_dbg_show(
struct seq_file *
s,
void *
p)
104 "%s version: %s\nGadget driver: %s\n",
110 "udccr=0x%0x(%s%s%s%s%s%s%s%s%s%s), "
111 "con=%d,inter=%d,altinter=%d\n", tmp,
126 pos +=
seq_printf(s,
"udcicr0=0x%08x udcicr1=0x%08x\n",
128 pos +=
seq_printf(s,
"udcisr0=0x%08x udcisr1=0x%08x\n",
131 pos +=
seq_printf(s,
"irqs: reset=%lu, suspend=%lu, resume=%lu, "
133 udc->
stats.irqs_reset, udc->
stats.irqs_suspend,
134 udc->
stats.irqs_resume, udc->
stats.irqs_reconfig);
141 static int queues_dbg_show(
struct seq_file *s,
void *p)
146 int pos = 0,
i, maxpkt,
ret;
157 EPNAME(ep), maxpkt,
"pio");
159 if (list_empty(&ep->
queue)) {
165 pos +=
seq_printf(s,
"\treq %p len %d/%d buf %p\n",
166 &req->
req, req->
req.actual,
167 req->
req.length, req->
req.buf);
176 static int eps_dbg_show(
struct seq_file *s,
void *p)
189 pos +=
seq_printf(s,
"udccsr0=0x%03x(%s%s%s%s%s%s%s)\n", tmp,
201 "IN %lu(%lu reqs), OUT %lu(%lu reqs), "
202 "irqs=%lu, udccr=0x%08x, udccsr=0x%03x, "
208 tmp, udc_ep_readl(ep, UDCCSR),
234 .open = state_dbg_open,
242 .open = queues_dbg_open,
250 .open = eps_dbg_open,
256 static void pxa_init_debugfs(
struct pxa_udc *udc)
261 if (IS_ERR(root) || !root)
277 udc->debugfs_root = root;
278 udc->debugfs_state =
state;
279 udc->debugfs_queues = queues;
280 udc->debugfs_eps = eps;
289 dev_err(udc->
dev,
"debugfs is not available\n");
292 static void pxa_cleanup_debugfs(
struct pxa_udc *udc)
298 udc->debugfs_eps =
NULL;
299 udc->debugfs_queues =
NULL;
300 udc->debugfs_state =
NULL;
301 udc->debugfs_root =
NULL;
305 static inline void pxa_init_debugfs(
struct pxa_udc *udc)
309 static inline void pxa_cleanup_debugfs(
struct pxa_udc *udc)
327 if (usb_endpoint_num(&udc_usb_ep->
desc) != ep->
addr)
329 if (usb_endpoint_dir_in(&udc_usb_ep->
desc) != ep->
dir_in)
331 if (usb_endpoint_type(&udc_usb_ep->
desc) != ep->
type)
365 struct udc_usb_ep *udc_usb_ep)
378 if (is_match_usb_pxa(udc_usb_ep, ep, cfg, iface, alt))
394 static void update_pxa_ep_matches(
struct pxa_udc *udc)
397 struct udc_usb_ep *udc_usb_ep;
402 udc_usb_ep->
pxa_ep = find_pxa_ep(udc, udc_usb_ep);
410 static void pio_irq_enable(
struct pxa_ep *ep)
427 static void pio_irq_disable(
struct pxa_ep *ep)
430 int index =
EPIDX(ep);
447 static inline void udc_set_mask_UDCCR(
struct pxa_udc *udc,
int mask)
461 static inline void udc_clear_mask_UDCCR(
struct pxa_udc *udc,
int mask)
478 static inline void ep_write_UDCCSR(
struct pxa_ep *ep,
int mask)
491 static int ep_count_bytes_remain(
struct pxa_ep *ep)
508 static int ep_is_empty(
struct pxa_ep *ep)
530 static int ep_is_full(
struct pxa_ep *ep)
545 static int epout_has_pkt(
struct pxa_ep *ep)
559 static void set_ep0state(
struct pxa_udc *udc,
int state)
565 ep_dbg(ep,
"state=%s->%s, udccsr0=0x%03x, udcbcr=%d\n", old_stname,
586 static void inc_ep_stats_reqs(
struct pxa_ep *ep,
int is_in)
600 static void inc_ep_stats_bytes(
struct pxa_ep *ep,
int count,
int is_in)
640 dev_dbg(dev->
dev,
"%s: dev=%p\n", __func__, dev);
643 pxa_ep_setup(&dev->
pxa_ep[i]);
656 pxa_ep_alloc_request(
struct usb_ep *_ep,
gfp_t gfp_flags)
660 req = kzalloc(
sizeof *req, gfp_flags);
664 INIT_LIST_HEAD(&req->
queue);
701 ep_vdbg(ep,
"req:%p, lg=%d, udccsr=0x%03x\n", req,
702 req->
req.length, udc_ep_readl(ep, UDCCSR));
724 ep_vdbg(ep,
"req:%p, lg=%d, udccsr=0x%03x\n", req,
725 req->
req.length, udc_ep_readl(ep, UDCCSR));
727 list_del_init(&req->
queue);
745 unsigned long *pflags)
749 ep_del_request(ep, req);
753 status = req->
req.status;
756 ep_dbg(ep,
"complete req %p stat %d len %u/%u\n",
758 req->
req.actual, req->
req.length);
761 spin_unlock_irqrestore(&ep->
lock, *pflags);
780 unsigned long *pflags)
783 req_done(ep, req, 0, pflags);
798 unsigned long *pflags)
801 ep_end_out_req(ep, req, pflags);
816 unsigned long *pflags)
819 req_done(ep, req, 0, pflags);
834 unsigned long *pflags)
837 ep_end_in_req(ep, req, pflags);
850 static void nuke(
struct pxa_ep *ep,
int status)
856 while (!list_empty(&ep->
queue)) {
858 req_done(ep, req, status, &flags);
860 spin_unlock_irqrestore(&ep->
lock, flags);
877 int bytes_ep, bufferspace,
count,
i;
879 bytes_ep = ep_count_bytes_remain(ep);
880 bufferspace = req->
req.length - req->
req.actual;
882 buf = (
u32 *)(req->
req.buf + req->
req.actual);
885 if (
likely(!ep_is_empty(ep)))
886 count =
min(bytes_ep, bufferspace);
890 for (i = count; i > 0; i -= 4)
918 buf = (
u32 *)(req->
req.buf + req->
req.actual);
921 length =
min(req->
req.length - req->
req.actual, max);
924 remain = length & 0x3;
925 count = length & ~(0x3);
926 for (i = count; i > 0 ; i -= 4)
930 for (i = remain; i > 0; i--)
933 ep_vdbg(ep,
"length=%d+%d, udccsr=0x%03x\n", count, remain,
957 while (epout_has_pkt(ep)) {
958 count = read_packet(ep, req);
962 ep_dbg(ep,
"read udccsr:%03x, count:%d bytes%s req %p %d/%d\n",
964 &req->
req, req->
req.actual, req->
req.length);
967 if (is_short || req->
req.actual == req->
req.length) {
991 int count, is_short, is_last = 0, completed = 0, totcount = 0;
1000 ep_vdbg(ep,
"Clearing Transmit Complete, udccsr=%x\n",
1002 ep_write_UDCCSR(ep, UDCCSR_PC);
1005 ep_vdbg(ep,
"Clearing Underrun on, udccsr=%x\n",
1007 ep_write_UDCCSR(ep, UDCCSR_TRN);
1010 count = write_packet(ep, req, max);
1036 }
while (!ep_is_full(ep));
1038 ep_dbg(ep,
"wrote count:%d bytes%s%s, left:%d req=%p\n",
1039 totcount, is_last ?
"/L" :
"", is_short ?
"/S" :
"",
1040 req->
req.length - req->
req.actual, &req->
req);
1058 int count, is_short, completed = 0;
1060 while (epout_has_pkt(ep)) {
1061 count = read_packet(ep, req);
1062 ep_write_UDCCSR(ep, UDCCSR0_OPC);
1066 ep_dbg(ep,
"read udccsr:%03x, count:%d bytes%s req %p %d/%d\n",
1068 &req->
req, req->
req.actual, req->
req.length);
1070 if (is_short || req->
req.actual >= req->
req.length) {
1097 int is_last, is_short;
1107 ep_write_UDCCSR(ep, UDCCSR0_IPR);
1109 ep_dbg(ep,
"in %d bytes%s%s, %d left, req=%p, udccsr0=0x%03x\n",
1110 count, is_short ?
"/S" :
"", is_last ?
"/L" :
"",
1111 req->
req.length - req->
req.actual,
1112 &req->
req, udc_ep_readl(ep, UDCCSR));
1132 struct udc_usb_ep *udc_usb_ep;
1136 unsigned long flags;
1140 int recursion_detected;
1151 dev = udc_usb_ep->
dev;
1158 ep_dbg(ep,
"bogus device state\n");
1172 is_first_req = list_empty(&ep->
queue);
1173 ep_dbg(ep,
"queue req %p(first=%s), len %d buf %p\n",
1174 _req, is_first_req ?
"yes" :
"no",
1184 ep_err(ep,
"refusing to queue req %p (already queued)\n", req);
1192 ep_add_request(ep, req);
1193 spin_unlock_irqrestore(&ep->
lock, flags);
1199 ep_end_in_req(ep, req,
NULL);
1201 ep_err(ep,
"got a request of %d bytes while"
1202 "in state WAIT_ACK_SET_CONF_INTERF\n",
1204 ep_del_request(ep, req);
1210 if (!ep_is_full(ep))
1211 if (write_ep0_fifo(ep, req))
1212 ep0_end_in_req(ep, req,
NULL);
1215 if ((length == 0) || !epout_has_pkt(ep))
1216 if (read_ep0_fifo(ep, req))
1217 ep0_end_out_req(ep, req,
NULL);
1220 ep_err(ep,
"odd state %s to send me a request\n",
1222 ep_del_request(ep, req);
1227 if (!recursion_detected)
1234 spin_unlock_irqrestore(&ep->
lock, flags);
1248 struct udc_usb_ep *udc_usb_ep;
1250 unsigned long flags;
1264 if (&req->
req == _req) {
1270 spin_unlock_irqrestore(&ep->
lock, flags);
1283 static int pxa_ep_set_halt(
struct usb_ep *_ep,
int value)
1286 struct udc_usb_ep *udc_usb_ep;
1287 unsigned long flags;
1305 ep_dbg(ep,
"only host can clear halt\n");
1312 if (ep->
dir_in && (ep_is_full(ep) || !list_empty(&ep->
queue)))
1322 spin_unlock_irqrestore(&ep->
lock, flags);
1332 static int pxa_ep_fifo_status(
struct usb_ep *_ep)
1335 struct udc_usb_ep *udc_usb_ep;
1349 return ep_count_bytes_remain(ep) + 1;
1358 static void pxa_ep_fifo_flush(
struct usb_ep *_ep)
1361 struct udc_usb_ep *udc_usb_ep;
1362 unsigned long flags;
1374 ep_dbg(ep,
"called while queue list not empty\n");
1379 while (!ep_is_empty(ep))
1388 spin_unlock_irqrestore(&ep->
lock, flags);
1401 static int pxa_ep_enable(
struct usb_ep *_ep,
1405 struct udc_usb_ep *udc_usb_ep;
1412 if (udc_usb_ep->
pxa_ep) {
1414 ep_warn(ep,
"usb_ep %s already enabled, doing nothing\n",
1417 ep = find_pxa_ep(udc_usb_ep->
dev, udc_usb_ep);
1422 "unable to match pxa_ep for ep %s\n",
1428 || (ep->
type != usb_endpoint_type(desc))) {
1429 ep_err(ep,
"type mismatch\n");
1433 if (ep->
fifo_size < usb_endpoint_maxp(desc)) {
1434 ep_err(ep,
"bad maxpacket\n");
1442 ep_err(ep,
"bogus device state\n");
1449 pxa_ep_fifo_flush(_ep);
1463 static int pxa_ep_disable(
struct usb_ep *_ep)
1466 struct udc_usb_ep *udc_usb_ep;
1479 pxa_ep_fifo_flush(_ep);
1482 ep_dbg(ep,
"disabled\n");
1487 .enable = pxa_ep_enable,
1488 .disable = pxa_ep_disable,
1490 .alloc_request = pxa_ep_alloc_request,
1491 .free_request = pxa_ep_free_request,
1493 .queue = pxa_ep_queue,
1494 .dequeue = pxa_ep_dequeue,
1496 .set_halt = pxa_ep_set_halt,
1497 .fifo_status = pxa_ep_fifo_status,
1498 .fifo_flush = pxa_ep_fifo_flush,
1510 static void dplus_pullup(
struct pxa_udc *udc,
int on)
1513 if (gpio_is_valid(udc->
mach->gpio_pullup))
1515 !udc->
mach->gpio_pullup_inverted);
1516 if (udc->
mach->udc_command)
1519 if (gpio_is_valid(udc->
mach->gpio_pullup))
1521 udc->
mach->gpio_pullup_inverted);
1522 if (udc->
mach->udc_command)
1532 static int pxa_udc_get_frame(
struct usb_gadget *_gadget)
1534 struct pxa_udc *udc = to_gadget_udc(_gadget);
1545 static int pxa_udc_wakeup(
struct usb_gadget *_gadget)
1547 struct pxa_udc *udc = to_gadget_udc(_gadget);
1552 udc_set_mask_UDCCR(udc, UDCCR_UDR);
1556 static void udc_enable(
struct pxa_udc *udc);
1557 static void udc_disable(
struct pxa_udc *udc);
1572 static int should_enable_udc(
struct pxa_udc *udc)
1593 static int should_disable_udc(
struct pxa_udc *udc)
1612 struct pxa_udc *udc = to_gadget_udc(_gadget);
1614 if (!gpio_is_valid(udc->
mach->gpio_pullup) && !udc->
mach->udc_command)
1617 dplus_pullup(udc, is_active);
1619 if (should_enable_udc(udc))
1621 if (should_disable_udc(udc))
1626 static void udc_enable(
struct pxa_udc *udc);
1627 static void udc_disable(
struct pxa_udc *udc);
1639 static int pxa_udc_vbus_session(
struct usb_gadget *_gadget,
int is_active)
1641 struct pxa_udc *udc = to_gadget_udc(_gadget);
1644 if (should_enable_udc(udc))
1646 if (should_disable_udc(udc))
1664 static int pxa_udc_vbus_draw(
struct usb_gadget *_gadget,
unsigned mA)
1668 udc = to_gadget_udc(_gadget);
1679 .get_frame = pxa_udc_get_frame,
1680 .wakeup = pxa_udc_wakeup,
1681 .pullup = pxa_udc_pullup,
1682 .vbus_session = pxa_udc_vbus_session,
1683 .vbus_draw = pxa_udc_vbus_draw,
1684 .start = pxa27x_udc_start,
1685 .stop = pxa27x_udc_stop,
1696 static void udc_disable(
struct pxa_udc *udc)
1704 udc_clear_mask_UDCCR(udc, UDCCR_UDE);
1726 INIT_LIST_HEAD(&dev->
gadget.ep_list);
1727 INIT_LIST_HEAD(&dev->
gadget.ep0->ep_list);
1736 INIT_LIST_HEAD(&ep->
queue);
1753 static void udc_enable(
struct pxa_udc *udc)
1760 udc_clear_mask_UDCCR(udc, UDCCR_UDE);
1768 udc_set_mask_UDCCR(udc, UDCCR_UDE);
1772 dev_err(udc->
dev,
"Configuration errors, udc disabled\n");
1785 pio_irq_enable(&udc->
pxa_ep[0]);
1822 dplus_pullup(udc, 1);
1826 dev_err(udc->
dev,
"device_add error %d\n", retval);
1829 retval = bind(&udc->
gadget, driver);
1831 dev_err(udc->
dev,
"bind to driver %s --> error %d\n",
1832 driver->
driver.name, retval);
1835 dev_dbg(udc->
dev,
"registered gadget driver '%s'\n",
1839 retval = otg_set_peripheral(udc->
transceiver->otg,
1842 dev_err(udc->
dev,
"can't bind to transceiver\n");
1843 goto transceiver_fail;
1847 if (should_enable_udc(udc))
1898 if (!driver || driver != udc->
driver || !driver->
unbind)
1901 stop_activity(udc, driver);
1903 dplus_pullup(udc, 0);
1909 dev_info(udc->
dev,
"unregistered gadget driver '%s'\n",
1922 static void handle_ep0_ctrl_req(
struct pxa_udc *udc,
1931 int have_extrabytes = 0;
1932 unsigned long flags;
1943 if (epout_has_pkt(ep) && (ep_count_bytes_remain(ep) == 0))
1944 ep_write_UDCCSR(ep, UDCCSR0_OPC);
1947 for (i = 0; i < 2; i++) {
1953 have_extrabytes = !ep_is_empty(ep);
1954 while (!ep_is_empty(ep)) {
1956 ep_err(ep,
"wrong to have extra bytes for setup : 0x%08x\n", i);
1959 ep_dbg(ep,
"SETUP %02x.%02x v%04x i%04x l%04x\n",
1960 u.r.bRequestType,
u.r.bRequest,
1972 ep_write_UDCCSR(ep, UDCCSR0_SA | UDCCSR0_OPC);
1974 spin_unlock_irqrestore(&ep->
lock, flags);
1980 spin_unlock_irqrestore(&ep->
lock, flags);
1983 ep_dbg(ep,
"protocol STALL, udccsr0=%03x err %d\n",
1986 set_ep0state(udc,
STALL);
2038 static void handle_ep0(
struct pxa_udc *udc,
int fifo_irq,
int opc_irq)
2045 if (!list_empty(&ep->
queue))
2049 ep_dbg(ep,
"state=%s, req=%p, udccsr0=0x%03x, udcbcr=%d, irq_msk=%x\n",
2051 (fifo_irq << 1 | opc_irq));
2053 if (udccsr0 & UDCCSR0_SST) {
2054 ep_dbg(ep,
"clearing stall status\n");
2056 ep_write_UDCCSR(ep, UDCCSR0_SST);
2060 if (udccsr0 & UDCCSR0_SA) {
2077 handle_ep0_ctrl_req(udc, req);
2080 if (epout_has_pkt(ep))
2081 ep_write_UDCCSR(ep, UDCCSR0_OPC);
2082 if (req && !ep_is_full(ep))
2083 completed = write_ep0_fifo(ep, req);
2085 ep0_end_in_req(ep, req,
NULL);
2088 if (epout_has_pkt(ep) && req)
2089 completed = read_ep0_fifo(ep, req);
2091 ep0_end_out_req(ep, req,
NULL);
2094 ep_write_UDCCSR(ep, UDCCSR0_FST);
2107 ep_warn(ep,
"should never get in %s state here!!!\n",
2123 static void handle_ep(
struct pxa_ep *ep)
2130 unsigned long flags;
2134 goto recursion_detected;
2147 ep_dbg(ep,
"req:%p, udccsr 0x%03x loop=%d\n",
2148 req, udccsr, loop++);
2157 if (
likely(!ep_is_full(ep)))
2158 completed = write_fifo(ep, req);
2160 if (
likely(epout_has_pkt(ep)))
2161 completed = read_fifo(ep, req);
2166 ep_end_in_req(ep, req, &flags);
2168 ep_end_out_req(ep, req, &flags);
2170 }
while (completed);
2174 spin_unlock_irqrestore(&ep->
lock, flags);
2185 static void pxa27x_change_configuration(
struct pxa_udc *udc,
int config)
2195 req.bRequestType = 0;
2215 static void pxa27x_change_interface(
struct pxa_udc *udc,
int iface,
int alt)
2219 dev_dbg(udc->
dev,
"interface=%d, alternate setting=%d\n", iface, alt);
2242 static void irq_handle_data(
int irq,
struct pxa_udc *udc)
2250 udc->
pxa_ep[0].stats.irqs++;
2257 for (i = 1; udcisr0 != 0 && i < 16; udcisr0 >>= 2, i++) {
2258 if (!(udcisr0 & UDCISR_INT_MASK))
2271 for (i = 16; udcisr1 != 0 && i < 24; udcisr1 >>= 2, i++) {
2273 if (!(udcisr1 & UDCISR_INT_MASK))
2290 static void irq_udc_suspend(
struct pxa_udc *udc)
2293 udc->
stats.irqs_suspend++;
2305 static void irq_udc_resume(
struct pxa_udc *udc)
2308 udc->
stats.irqs_resume++;
2319 static void irq_udc_reconfig(
struct pxa_udc *udc)
2325 udc->
stats.irqs_reconfig++;
2328 config_change = (config != udc->
config);
2329 pxa27x_change_configuration(udc, config);
2333 pxa27x_change_interface(udc, interface, alternate);
2336 update_pxa_ep_matches(udc);
2337 udc_set_mask_UDCCR(udc, UDCCR_SMAC);
2344 static void irq_udc_reset(
struct pxa_udc *udc)
2351 udc->
stats.irqs_reset++;
2353 if ((udccr & UDCCR_UDA) == 0) {
2355 stop_activity(udc, udc->
driver);
2372 static irqreturn_t pxa_udc_irq(
int irq,
void *_dev)
2380 dev_vdbg(udc->
dev,
"Interrupt, UDCISR0:0x%08x, UDCISR1:0x%08x, "
2381 "UDCCR:0x%08x\n", udcisr0, udcisr1, udccr);
2383 udcisr1_spec = udcisr1 & 0xf8000000;
2385 irq_udc_suspend(udc);
2387 irq_udc_resume(udc);
2389 irq_udc_reconfig(udc);
2394 irq_handle_data(irq, udc);
2401 .ops = &pxa_udc_ops,
2405 .init_name =
"gadget",
2457 int retval = 0,
gpio;
2467 udc->
mach = pdev->
dev.platform_data;
2471 if (gpio_is_valid(
gpio)) {
2475 udc->
mach->gpio_pullup_inverted);
2478 dev_err(&pdev->
dev,
"Couldn't request gpio %d : %d\n",
2484 if (IS_ERR(udc->
clk)) {
2485 retval = PTR_ERR(udc->
clk);
2492 dev_err(&pdev->
dev,
"Unable to map UDC I/O memory\n");
2501 the_controller = udc;
2502 platform_set_drvdata(pdev, udc);
2510 dev_err(udc->
dev,
"%s: can't get irq %i, err %d\n",
2518 pxa_init_debugfs(udc);
2537 struct pxa_udc *udc = platform_get_drvdata(_dev);
2543 pxa_cleanup_debugfs(udc);
2544 if (gpio_is_valid(gpio))
2550 platform_set_drvdata(_dev,
NULL);
2551 the_controller =
NULL;
2560 struct pxa_udc *udc = platform_get_drvdata(_dev);
2566 #ifdef CONFIG_PXA27x
2569 #define pxa27x_clear_otgph() do {} while (0)
2584 struct pxa_udc *udc = platform_get_drvdata(_dev);
2593 ep_dbg(ep,
"udccsr:0x%03x, udccr:0x%x\n",
2594 ep->udccsr_value, ep->udccr_value);
2599 dplus_pullup(udc, 0);
2614 struct pxa_udc *udc = platform_get_drvdata(_dev);
2618 udc_ep_writel(ep, UDCCSR, udc->udccsr0 & (UDCCSR0_FST | UDCCSR0_DME));
2623 ep_dbg(ep,
"udccsr:0x%03x, udccr:0x%x\n",
2624 ep->udccsr_value, ep->udccr_value);
2628 if (should_enable_udc(udc))
2650 .name =
"pxa27x-udc",
2653 .remove =
__exit_p(pxa_udc_remove),
2654 .shutdown = pxa_udc_shutdown,
2656 .suspend = pxa_udc_suspend,
2657 .resume = pxa_udc_resume
2661 static int __init udc_init(
void)
2672 static void __exit udc_exit(
void)