12 #include <linux/module.h>
13 #include <linux/pci.h>
16 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
25 #include <linux/list.h>
28 #include <linux/device.h>
29 #include <linux/usb/ch9.h>
38 #include <asm/unaligned.h>
42 #define DRIVER_DESC "Marvell PXA USB Device Controller driver"
43 #define DRIVER_VERSION "8 Nov 2010"
45 #define ep_dir(ep) (((ep)->ep_num == 0) ? \
46 ((ep)->udc->ep0_dir) : ((ep)->direction))
49 #define RESET_TIMEOUT 10000
50 #define FLUSH_TIMEOUT 10000
51 #define EPSTATUS_TIMEOUT 10000
52 #define PRIME_TIMEOUT 10000
53 #define READSAFE_TIMEOUT 1000
55 #define LOOPS_USEC_SHIFT 1
56 #define LOOPS_USEC (1 << LOOPS_USEC_SHIFT)
57 #define LOOPS(timeout) ((timeout) >> LOOPS_USEC_SHIFT)
74 .bEndpointAddress = 0,
86 for (i = 0; i < 2; i++) {
94 ep->
dqh->max_packet_length =
117 static void ep0_stall(
struct mv_udc *udc)
131 static int process_ep_req(
struct mv_udc *udc,
int index,
136 int td_complete, actual, remaining_length;
143 direction = index % 2;
145 curr_dtd = curr_req->
head;
147 actual = curr_req->
req.length;
149 for (i = 0; i < curr_req->
dtd_count; i++) {
151 dev_dbg(&udc->
dev->dev,
"%s, dTD not completed\n",
152 udc->
eps[index].name);
161 actual -= remaining_length;
163 if (remaining_length) {
166 "TX dTD remains data\n");
174 "complete_tr error: ep=%d %s: error = 0x%x\n",
175 index >> 1, direction ?
"SEND" :
"RECV",
194 bit_pos = 1 << curr_req->
ep->ep_num;
196 bit_pos = 1 << (16 + curr_req->
ep->ep_num);
207 curr_req->
req.actual = actual;
221 struct mv_dtd *curr_td, *next_td;
226 list_del_init(&req->
queue);
232 status = req->
req.status;
245 req->
req.dma, req->
req.length,
252 req->
req.dma, req->
req.length,
257 dev_info(&udc->
dev->dev,
"complete %s req %p stat %d len %u/%u",
258 ep->
ep.name, &req->
req, status,
259 req->
req.actual, req->
req.length);
263 spin_unlock(&ep->
udc->lock);
268 if (req->
req.complete)
269 req->
req.complete(&ep->
ep, &req->
req);
271 spin_lock(&ep->
udc->lock);
275 static int queue_dtd(
struct mv_ep *ep,
struct mv_req *req)
280 u32 usbcmd, epstatus;
290 if (!(list_empty(&ep->
queue))) {
293 lastreq->
tail->dtd_next =
325 "Timeout for ATDTW_TRIPWIRE...\n");
367 if (usb_endpoint_xfer_isoc(req->
ep->ep.desc)) {
371 *length =
min(req->
req.length - req->
req.actual,
372 (
unsigned)(mult * req->
ep->ep.maxpacket));
374 *length =
min(req->
req.length - req->
req.actual,
389 temp = (
u32)(req->
req.dma + req->
req.actual);
401 if (*length == 0 || (*length % req->
ep->ep.maxpacket) != 0)
405 }
else if (req->
req.length == req->
req.actual)
414 if (*is_last && !req->
req.no_interrupt)
427 static int req_to_dtd(
struct mv_req *req)
430 int is_last, is_first = 1;
438 dtd = build_dtd(req, &count, &dma, &is_last);
461 static int mv_ep_enable(
struct usb_ep *_ep,
469 unsigned char zlt = 0, ios = 0, mult = 0;
483 max = usb_endpoint_maxp(desc);
497 "ep=%d %s: Init ERROR: ENDPTPRIME=0x%x,"
498 " ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
499 (
unsigned)ep->
ep_num, direction ?
"SEND" :
"RECV",
518 mult = (
unsigned char)(1 + ((max >> 11) & 0x03));
538 ep->
ep.maxpacket =
max;
575 spin_unlock_irqrestore(&udc->
lock, flags);
582 static int mv_ep_disable(
struct usb_ep *_ep)
591 if ((_ep ==
NULL) || !ep->
ep.desc)
620 spin_unlock_irqrestore(&udc->
lock, flags);
626 mv_alloc_request(
struct usb_ep *_ep,
gfp_t gfp_flags)
630 req = kzalloc(
sizeof *req, gfp_flags);
635 INIT_LIST_HEAD(&req->
queue);
650 static void mv_ep_fifo_flush(
struct usb_ep *_ep)
668 bit_pos = (1 << 16) | 1;
670 bit_pos = 1 << ep->
ep_num;
672 bit_pos = 1 << (16 + ep->
ep_num);
676 unsigned int inter_loops;
680 "TIMEOUT for ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
695 if (inter_loops == 0) {
697 "TIMEOUT for ENDPTFLUSH=0x%x,"
721 if (!_req || !req->
req.complete || !req->
req.buf
722 || !list_empty(&req->
queue)) {
723 dev_err(&udc->
dev->dev,
"%s, bad params", __func__);
727 dev_err(&udc->
dev->dev,
"%s, bad ep", __func__);
747 req->
req.dma, req->
req.length,
761 if (!req_to_dtd(req)) {
762 retval = queue_dtd(ep, req);
764 spin_unlock_irqrestore(&udc->
lock, flags);
765 dev_err(&udc->
dev->dev,
"Failed to queue dtd\n");
769 spin_unlock_irqrestore(&udc->
lock, flags);
770 dev_err(&udc->
dev->dev,
"Failed to dma_pool_alloc\n");
781 spin_unlock_irqrestore(&udc->
lock, flags);
788 req->
req.dma, req->
req.length,
795 req->
req.dma, req->
req.length,
802 static void mv_prime_ep(
struct mv_ep *ep,
struct mv_req *req)
820 writel(bit_pos, &ep->
udc->op_regs->epprime);
845 epctrlx &= ~EPCTRL_RX_ENABLE;
850 if (&req->
req == _req)
853 if (&req->
req != _req) {
861 mv_ep_fifo_flush(_ep);
871 mv_prime_ep(ep, next_req);
886 &prev_req->
tail->dtd_next);
902 spin_unlock_irqrestore(&ep->
udc->lock, flags);
906 static void ep_set_stall(
struct mv_udc *udc,
u8 ep_num,
u8 direction,
int stall)
929 static int ep_is_stall(
struct mv_udc *udc,
u8 ep_num,
u8 direction)
941 static int mv_ep_set_halt_wedge(
struct usb_ep *_ep,
int halt,
int wedge)
944 unsigned long flags = 0;
950 if (!_ep || !ep->
ep.desc) {
975 spin_unlock_irqrestore(&ep->
udc->lock, flags);
985 static int mv_ep_set_halt(
struct usb_ep *_ep,
int halt)
987 return mv_ep_set_halt_wedge(_ep, halt, 0);
990 static int mv_ep_set_wedge(
struct usb_ep *_ep)
992 return mv_ep_set_halt_wedge(_ep, 1, 1);
996 .enable = mv_ep_enable,
997 .disable = mv_ep_disable,
999 .alloc_request = mv_alloc_request,
1000 .free_request = mv_free_request,
1002 .queue = mv_ep_queue,
1003 .dequeue = mv_ep_dequeue,
1005 .set_wedge = mv_ep_set_wedge,
1006 .set_halt = mv_ep_set_halt,
1007 .fifo_flush = mv_ep_fifo_flush,
1010 static void udc_clock_enable(
struct mv_udc *udc)
1014 for (i = 0; i < udc->
clknum; i++)
1018 static void udc_clock_disable(
struct mv_udc *udc)
1022 for (i = 0; i < udc->
clknum; i++)
1026 static void udc_stop(
struct mv_udc *udc)
1044 static void udc_start(
struct mv_udc *udc)
1060 static int udc_reset(
struct mv_udc *udc)
1078 "Wait for RESET completed TIMEOUT\n");
1118 static int mv_udc_enable_internal(
struct mv_udc *udc)
1126 udc_clock_enable(udc);
1127 if (udc->
pdata->phy_init) {
1131 "init phy error %d\n", retval);
1132 udc_clock_disable(udc);
1141 static int mv_udc_enable(
struct mv_udc *udc)
1144 return mv_udc_enable_internal(udc);
1149 static void mv_udc_disable_internal(
struct mv_udc *udc)
1153 if (udc->
pdata->phy_deinit)
1155 udc_clock_disable(udc);
1160 static void mv_udc_disable(
struct mv_udc *udc)
1163 mv_udc_disable_internal(udc);
1166 static int mv_udc_get_frame(
struct usb_gadget *gadget)
1182 static int mv_udc_wakeup(
struct usb_gadget *gadget)
1204 unsigned long flags;
1212 dev_dbg(&udc->
dev->dev,
"%s: softconnect %d, vbus_active %d\n",
1216 retval = mv_udc_enable(udc);
1228 stop_activity(udc, udc->
driver);
1230 mv_udc_disable(udc);
1234 spin_unlock_irqrestore(&udc->
lock, flags);
1238 static int mv_udc_pullup(
struct usb_gadget *gadget,
int is_on)
1241 unsigned long flags;
1249 dev_dbg(&udc->
dev->dev,
"%s: softconnect %d, vbus_active %d\n",
1253 retval = mv_udc_enable(udc);
1262 stop_activity(udc, udc->
driver);
1264 mv_udc_disable(udc);
1267 spin_unlock_irqrestore(&udc->
lock, flags);
1278 .get_frame = mv_udc_get_frame,
1281 .wakeup = mv_udc_wakeup,
1284 .vbus_session = mv_udc_vbus_session,
1287 .pullup = mv_udc_pullup,
1288 .start = mv_udc_start,
1289 .stop = mv_udc_stop,
1292 static int eps_init(
struct mv_udc *udc)
1303 ep->
ep.ops = &mv_ep_ops;
1308 ep->
ep.desc = &mv_ep0_desc;
1309 INIT_LIST_HEAD(&ep->
queue);
1314 for (i = 2; i < udc->
max_eps * 2; i++) {
1317 snprintf(name,
sizeof(name),
"ep%din", i / 2);
1320 snprintf(name,
sizeof(name),
"ep%dout", i / 2);
1327 ep->
ep.ops = &mv_ep_ops;
1329 ep->
ep.maxpacket = (
unsigned short) ~0;
1332 INIT_LIST_HEAD(&ep->
queue);
1342 static void nuke(
struct mv_ep *ep,
int status)
1348 mv_ep_fifo_flush(&ep->
ep);
1350 while (!list_empty(&ep->
queue)) {
1353 done(ep, req, status);
1370 spin_unlock(&udc->
lock);
1372 spin_lock(&udc->
lock);
1381 unsigned long flags;
1400 spin_unlock_irqrestore(&udc->
lock, flags);
1402 retval = bind(&udc->
gadget, driver);
1404 dev_err(&udc->
dev->dev,
"bind to driver %s --> %d\n",
1405 driver->
driver.name, retval);
1412 retval = otg_set_peripheral(udc->
transceiver->otg,
1416 "unable to register peripheral to otg\n");
1427 mv_udc_pullup(&udc->
gadget, 1);
1439 unsigned long flags;
1451 stop_activity(udc, driver);
1452 mv_udc_disable(udc);
1454 spin_unlock_irqrestore(&udc->
lock, flags);
1469 portsc |= mode << 16;
1477 unsigned long flags;
1486 spin_unlock_irqrestore(&udc->
lock, flags);
1503 if (empty ==
false) {
1505 req->
req.length = 2;
1507 req->
req.length = 0;
1511 req->
req.actual = 0;
1513 req->
req.complete = prime_status_complete;
1522 req->
req.buf, req->
req.length,
1528 if (!req_to_dtd(req)) {
1529 retval = queue_dtd(ep, req);
1532 "Failed to queue dtd when prime status\n");
1538 "Failed to dma_pool_alloc when prime status\n");
1548 req->
req.dma, req->
req.length,
1558 static void mv_udc_testmode(
struct mv_udc *udc,
u16 index)
1562 if (udc_prime_status(udc,
EP_DIR_IN, 0,
true))
1566 "This test mode(%d) is not supported\n", index);
1580 static void ch9getstatus(
struct mv_udc *udc,
u8 ep_num,
1604 status = ep_is_stall(udc, ep_num, direction)
1608 retval = udc_prime_status(udc,
EP_DIR_IN, status,
false);
1643 spin_unlock(&udc->
lock);
1644 ep_set_stall(udc, ep_num, direction, 0);
1645 spin_lock(&udc->
lock);
1653 if (udc_prime_status(udc,
EP_DIR_IN, 0,
true))
1680 mv_udc_testmode(udc, (setup->
wIndex >> 8));
1695 spin_unlock(&udc->
lock);
1696 ep_set_stall(udc, ep_num, direction, 1);
1697 spin_lock(&udc->
lock);
1705 if (udc_prime_status(udc,
EP_DIR_IN, 0,
true))
1711 static void handle_setup_packet(
struct mv_udc *udc,
u8 ep_num,
1714 bool delegate =
false;
1718 dev_dbg(&udc->
dev->dev,
"SETUP %02x.%02x v%04x i%04x l%04x\n",
1725 ch9getstatus(udc, ep_num, setup);
1729 ch9setaddress(udc, setup);
1733 ch9clearfeature(udc, setup);
1737 ch9setfeature(udc, setup);
1747 if (delegate ==
true) {
1753 spin_unlock(&udc->
lock);
1757 spin_lock(&udc->
lock);
1763 spin_unlock(&udc->
lock);
1767 spin_lock(&udc->
lock);
1774 static void ep0_req_complete(
struct mv_udc *udc,
1791 if (udc_prime_status(udc,
EP_DIR_OUT, 0,
true))
1796 if (udc_prime_status(udc,
EP_DIR_IN, 0 ,
true))
1803 dev_err(&udc->
dev->dev,
"unexpect ep0 packets\n");
1811 static void get_setup_data(
struct mv_udc *udc,
u8 ep_num,
u8 *buffer_ptr)
1836 static void irq_process_tr_complete(
struct mv_udc *udc)
1839 int i, ep_num = 0, direction = 0;
1840 struct mv_ep *curr_ep;
1841 struct mv_req *curr_req, *temp_req;
1853 for (i = 0; i < udc->
max_eps; i++) {
1854 if (tmp & (1 << i)) {
1855 get_setup_data(udc, i,
1857 handle_setup_packet(udc, i,
1875 for (i = 0; i < udc->
max_eps * 2; i++) {
1879 bit_pos = 1 << (ep_num + 16 *
direction);
1881 if (!(bit_pos & tmp))
1885 curr_ep = &udc->
eps[0];
1887 curr_ep = &udc->
eps[
i];
1891 status = process_ep_req(udc, i, curr_req);
1900 ep0_req_complete(udc, curr_ep, curr_req);
1903 done(curr_ep, curr_req, status);
1936 "Timeout for ENDPTPRIME = 0x%x\n",
1951 stop_activity(udc, udc->
driver);
1953 dev_info(&udc->
dev->dev,
"USB reset portsc 0x%x\n",
1963 stop_activity(udc, udc->
driver);
1975 static void handle_bus_resume(
struct mv_udc *udc)
1982 if (udc->
driver->resume) {
1983 spin_unlock(&udc->
lock);
1985 spin_lock(&udc->
lock);
1990 static void irq_process_suspend(
struct mv_udc *udc)
1995 if (udc->
driver->suspend) {
1996 spin_unlock(&udc->
lock);
1998 spin_lock(&udc->
lock);
2002 static void irq_process_port_change(
struct mv_udc *udc)
2029 if (udc->
driver->suspend) {
2030 spin_unlock(&udc->
lock);
2032 spin_lock(&udc->
lock);
2036 if (!(portsc & PORTSCX_PORT_SUSPEND)
2038 handle_bus_resume(udc);
2045 static void irq_process_error(
struct mv_udc *udc)
2060 spin_lock(&udc->
lock);
2067 spin_unlock(&udc->
lock);
2075 irq_process_error(udc);
2081 irq_process_port_change(udc);
2084 irq_process_tr_complete(udc);
2087 irq_process_suspend(udc);
2089 spin_unlock(&udc->
lock);
2094 static irqreturn_t mv_udc_vbus_irq(
int irq,
void *dev)
2111 if (!udc->
pdata->vbus)
2114 vbus = udc->
pdata->vbus->poll();
2118 mv_udc_vbus_session(&udc->
gadget, 1);
2120 mv_udc_vbus_session(&udc->
gadget, 0);
2124 static void gadget_release(
struct device *_dev)
2164 mv_udc_disable(udc);
2177 for (clk_i = 0; clk_i <= udc->
clknum; clk_i++)
2186 the_controller =
NULL;
2200 if (pdata ==
NULL) {
2201 dev_err(&dev->
dev,
"missing platform_data\n");
2205 size =
sizeof(*udc) +
sizeof(
struct clk *) * pdata->
clknum;
2208 dev_err(&dev->
dev,
"failed to allocate memory for udc\n");
2212 the_controller = udc;
2213 udc->
done = &release_done;
2214 udc->
pdata = dev->
dev.platform_data;
2219 #ifdef CONFIG_USB_OTG_UTILS
2225 for (clk_i = 0; clk_i < udc->
clknum; clk_i++) {
2227 if (IS_ERR(udc->
clk[clk_i])) {
2228 retval = PTR_ERR(udc->
clk[clk_i]);
2235 dev_err(&dev->
dev,
"no I/O memory resource defined\n");
2243 dev_err(&dev->
dev,
"failed to map I/O memory\n");
2250 dev_err(&dev->
dev,
"no phy I/O memory resource defined\n");
2252 goto err_iounmap_capreg;
2257 dev_err(&dev->
dev,
"failed to map phy I/O memory\n");
2259 goto err_iounmap_capreg;
2263 retval = mv_udc_enable_internal(udc);
2265 goto err_iounmap_phyreg;
2286 dev_err(&dev->
dev,
"allocate dQH memory failed\n");
2288 goto err_disable_clock;
2307 dev_err(&dev->
dev,
"allocate ep memory failed\n");
2309 goto err_destroy_dma;
2315 dev_err(&dev->
dev,
"allocate status_req memory failed\n");
2332 dev_err(&dev->
dev,
"no IRQ resource defined\n");
2334 goto err_free_status_req;
2339 dev_err(&dev->
dev,
"Request irq %d for UDC failed\n",
2342 goto err_free_status_req;
2346 udc->
gadget.ops = &mv_ops;
2348 INIT_LIST_HEAD(&udc->
gadget.ep_list);
2355 udc->
gadget.dev.dma_mask = dev->
dev.dma_mask;
2356 udc->
gadget.dev.release = gadget_release;
2368 else if (pdata->
vbus) {
2374 "Can not request irq for VBUS, "
2375 "disable clock gating\n");
2381 dev_err(&dev->
dev,
"cannot create workqueue\n");
2383 goto err_unregister;
2395 mv_udc_disable_internal(udc);
2401 goto err_unregister;
2403 dev_info(&dev->
dev,
"successful probe UDC device %s clock gating.\n",
2415 err_free_status_req:
2426 mv_udc_disable_internal(udc);
2432 for (clk_i--; clk_i >= 0; clk_i--)
2434 the_controller =
NULL;
2440 static int mv_udc_suspend(
struct device *_dev)
2448 if (udc->
pdata->vbus && udc->
pdata->vbus->poll)
2450 dev_info(&udc->
dev->dev,
"USB cable is connected!\n");
2461 spin_lock_irq(&udc->
lock);
2463 stop_activity(udc, udc->
driver);
2464 spin_unlock_irq(&udc->
lock);
2466 mv_udc_disable_internal(udc);
2472 static int mv_udc_resume(
struct device *_dev)
2482 retval = mv_udc_enable_internal(udc);
2496 static const struct dev_pm_ops mv_udc_pm_ops = {
2498 .resume = mv_udc_resume,
2512 mv_udc_disable(udc);
2516 .probe = mv_udc_probe,
2518 .shutdown = mv_udc_shutdown,
2523 .pm = &mv_udc_pm_ops,