9 #include <linux/module.h>
12 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
20 #include <linux/list.h>
24 #include <linux/device.h>
25 #include <linux/usb/ch9.h>
33 #include <asm/system.h>
34 #include <asm/unaligned.h>
35 #include <asm/byteorder.h>
39 #define DRIVER_DESC "Marvell PXA USB3.0 Device Controller driver"
45 static void mv_u3d_stop_activity(
struct mv_u3d *u3d,
52 .bEndpointAddress = 0,
57 static void mv_u3d_ep0_reset(
struct mv_u3d *u3d)
63 for (i = 0; i < 2; i++) {
103 static void mv_u3d_ep0_stall(
struct mv_u3d *u3d)
122 static int mv_u3d_process_ep_req(
struct mv_u3d *u3d,
int index,
128 int trb_complete, actual, remaining_length;
134 direction = index % 2;
138 actual = curr_req->
req.length;
140 while (!list_empty(&curr_req->
trb_list)) {
143 if (!curr_trb->
trb_hw->ctrl.own) {
145 u3d->
eps[index].name);
149 curr_trb->
trb_hw->ctrl.own = 0;
166 remaining_length +=
length;
167 actual -= remaining_length;
170 "complete_tr error: ep=%d %s: error = 0x%x\n",
171 index >> 1, direction ?
"SEND" :
"RECV",
181 curr_req->
req.actual = actual;
195 dev_dbg(u3d->
dev,
"mv_u3d_done: remove req->queue\n");
197 list_del_init(&req->
queue);
203 status = req->
req.status;
221 dev_dbg(u3d->
dev,
"complete %s req %p stat %d len %u/%u",
222 ep->
ep.name, &req->
req, status,
223 req->
req.actual, req->
req.length);
226 spin_unlock(&ep->
u3d->lock);
231 if (req->
req.complete)
232 req->
req.complete(&ep->
ep, &req->
req);
234 spin_lock(&ep->
u3d->lock);
254 if (!list_empty(&ep->
queue)) {
255 dev_err(u3d->
dev,
"add trb to non-empty queue!\n");
260 ep_context->
rsvd1 = 0;
296 *length = req->
req.length - req->
req.actual;
303 dev_err(u3d->
dev,
"%s, trb alloc fail\n", __func__);
315 "%s, dma_pool_alloc fail\n", __func__);
322 temp = (
u32)(req->
req.dma + req->
req.actual);
327 trb_hw->
ctrl.own = 1;
329 if (req->
ep->ep_num == 0)
338 trb_hw->
ctrl.dir = 1;
340 trb_hw->
ctrl.dir = 0;
343 if (!req->
req.no_interrupt)
344 trb_hw->
ctrl.ioc = 1;
346 trb_hw->
ctrl.chain = 0;
352 static int mv_u3d_build_trb_chain(
struct mv_u3d_req *req,
unsigned *length,
360 *length =
min(req->
req.length - req->
req.actual,
368 temp = (
u32)(req->
req.dma + req->
req.actual);
371 trb->
trb_hw->buf_addr_hi = 0;
373 trb->
trb_hw->ctrl.own = 1;
375 if (req->
ep->ep_num == 0)
384 trb->
trb_hw->ctrl.dir = 1;
386 trb->
trb_hw->ctrl.dir = 0;
390 if (*length == 0 || (*length % req->
ep->ep.maxpacket) != 0)
394 }
else if (req->
req.length == req->
req.actual)
400 if (*is_last && !req->
req.no_interrupt)
401 trb->
trb_hw->ctrl.ioc = 1;
404 trb->
trb_hw->ctrl.chain = 0;
406 trb->
trb_hw->ctrl.chain = 1;
419 static int mv_u3d_req_to_trb(
struct mv_u3d_req *req)
434 length = req->
req.length - req->
req.actual;
439 trb = mv_u3d_build_trb_one(req, &count, &dma);
446 if (length % MV_U3D_EP_MAX_LENGTH_TRANSFER)
449 trb = kcalloc(trb_num,
sizeof(*trb),
GFP_ATOMIC);
452 "%s, trb alloc fail\n", __func__);
456 trb_hw = kcalloc(trb_num,
sizeof(*trb_hw),
GFP_ATOMIC);
459 "%s, trb_hw alloc fail\n", __func__);
465 if (mv_u3d_build_trb_chain(req, &count,
468 "%s, mv_u3d_build_trb_chain fail\n",
483 trb_num *
sizeof(*trb_hw),
517 if (!mv_u3d_req_to_trb(req)) {
518 ret = mv_u3d_queue_trb(ep, req);
525 dev_err(u3d->
dev,
"%s, mv_u3d_req_to_trb fail\n", __func__);
536 static int mv_u3d_ep_enable(
struct usb_ep *_ep,
543 unsigned maxburst = 0;
570 "max burst should not be greater "
571 "than 16 on bulk ep\n");
576 "maxburst: %d on bulk %s\n", maxburst, ep->
name);
586 "max burst should be 1 on int ep "
587 "if transfer size is not 1024\n");
595 "max burst should be 1 on isoc ep "
596 "if transfer size is not 1024\n");
605 ep->
ep.maxpacket =
max;
643 static int mv_u3d_ep_disable(
struct usb_ep *_ep)
687 mv_u3d_alloc_request(
struct usb_ep *_ep,
gfp_t gfp_flags)
691 req = kzalloc(
sizeof *req, gfp_flags);
695 INIT_LIST_HEAD(&req->
queue);
707 static void mv_u3d_ep_fifo_flush(
struct usb_ep *_ep)
757 "EP FLUSH TIMEOUT for ep%d%s\n", ep->
ep_num,
758 direction ?
"in" :
"out");
779 "EP FLUSH TIMEOUT for ep%d%s\n", ep->
ep_num,
780 direction ?
"in" :
"out");
797 int is_first_req = 0;
816 __func__, _ep->
name, (
u32)req);
819 if (!req->
req.complete || !req->
req.buf
820 || !list_empty(&req->
queue)) {
822 "%s, bad params, _req: 0x%x,"
823 "req->req.complete: 0x%x, req->req.buf: 0x%x,"
824 "list_empty: 0x%x\n",
835 if (req->
req.length > ep->
ep.maxpacket)
841 "bad params of driver/speed\n");
849 is_first_req = list_empty(&ep->
req_list);
851 spin_unlock_irqrestore(&ep->
req_lock, flags);
857 dev_dbg(u3d->
dev,
"call mv_u3d_start_queue from usb_ep_queue\n");
859 mv_u3d_start_queue(ep);
860 spin_unlock_irqrestore(&u3d->
lock, flags);
886 if (&req->
req == _req)
889 if (&req->
req != _req) {
897 mv_u3d_ep_fifo_flush(_ep);
902 "it is the last request in this ep queue\n");
927 if (curr_req == req) {
928 list_del_init(&req->
list);
934 spin_unlock_irqrestore(&ep->
u3d->lock, flags);
939 mv_u3d_ep_set_stall(
struct mv_u3d *u3d,
u8 ep_num,
u8 direction,
int stall)
962 static int mv_u3d_ep_set_halt_wedge(
struct usb_ep *_ep,
int halt,
int wedge)
965 unsigned long flags = 0;
986 && !list_empty(&ep->
queue)) {
997 spin_unlock_irqrestore(&ep->
u3d->lock, flags);
1005 static int mv_u3d_ep_set_halt(
struct usb_ep *_ep,
int halt)
1007 return mv_u3d_ep_set_halt_wedge(_ep, halt, 0);
1010 static int mv_u3d_ep_set_wedge(
struct usb_ep *_ep)
1012 return mv_u3d_ep_set_halt_wedge(_ep, 1, 1);
1016 .enable = mv_u3d_ep_enable,
1017 .disable = mv_u3d_ep_disable,
1019 .alloc_request = mv_u3d_alloc_request,
1020 .free_request = mv_u3d_free_request,
1022 .queue = mv_u3d_ep_queue,
1023 .dequeue = mv_u3d_ep_dequeue,
1025 .set_wedge = mv_u3d_ep_set_wedge,
1026 .set_halt = mv_u3d_ep_set_halt,
1027 .fifo_flush = mv_u3d_ep_fifo_flush,
1030 static void mv_u3d_controller_stop(
struct mv_u3d *u3d)
1049 dev_dbg(u3d->
dev,
"after u3d_stop, USBCMD 0x%x\n",
1053 static void mv_u3d_controller_start(
struct mv_u3d *u3d)
1075 dev_dbg(u3d->
dev,
"after u3d_start, USBCMD 0x%x\n",
1079 static int mv_u3d_controller_reset(
struct mv_u3d *u3d)
1097 "Wait for RESET completed TIMEOUT\n");
1111 static int mv_u3d_enable(
struct mv_u3d *u3d)
1130 "init phy error %d\n", retval);
1140 static void mv_u3d_disable(
struct mv_u3d *u3d)
1155 unsigned long flags;
1163 dev_dbg(u3d->
dev,
"%s: softconnect %d, vbus_active %d\n",
1171 retval = mv_u3d_enable(u3d);
1177 mv_u3d_controller_reset(u3d);
1178 mv_u3d_ep0_reset(u3d);
1179 mv_u3d_controller_start(u3d);
1186 mv_u3d_stop_activity(u3d, u3d->
driver);
1187 mv_u3d_controller_stop(u3d);
1188 mv_u3d_disable(u3d);
1192 spin_unlock_irqrestore(&u3d->
lock, flags);
1203 static int mv_u3d_vbus_draw(
struct usb_gadget *gadget,
unsigned mA)
1212 static int mv_u3d_pullup(
struct usb_gadget *gadget,
int is_on)
1215 unsigned long flags;
1220 dev_dbg(u3d->
dev,
"%s: softconnect %d, vbus_active %d\n",
1224 retval = mv_u3d_enable(u3d);
1230 mv_u3d_controller_reset(u3d);
1231 mv_u3d_ep0_reset(u3d);
1232 mv_u3d_controller_start(u3d);
1236 mv_u3d_stop_activity(u3d, u3d->
driver);
1237 mv_u3d_controller_stop(u3d);
1238 mv_u3d_disable(u3d);
1241 spin_unlock_irqrestore(&u3d->
lock, flags);
1246 static int mv_u3d_start(
struct usb_gadget *g,
1251 unsigned long flags;
1271 spin_unlock_irqrestore(&u3d->
lock, flags);
1283 unsigned long flags;
1293 mv_u3d_controller_stop(u3d);
1296 mv_u3d_stop_activity(u3d, driver);
1297 mv_u3d_disable(u3d);
1303 spin_unlock_irqrestore(&u3d->
lock, flags);
1314 .vbus_session = mv_u3d_vbus_session,
1317 .vbus_draw = mv_u3d_vbus_draw,
1319 .pullup = mv_u3d_pullup,
1320 .udc_start = mv_u3d_start,
1321 .udc_stop = mv_u3d_stop,
1324 static int mv_u3d_eps_init(
struct mv_u3d *u3d)
1335 ep->
ep.ops = &mv_u3d_ep_ops;
1339 ep->
ep.desc = &mv_u3d_ep0_desc;
1340 INIT_LIST_HEAD(&ep->
queue);
1348 for (i = 2; i < u3d->
max_eps * 2; i++) {
1351 snprintf(name,
sizeof(name),
"ep%din", i >> 1);
1354 snprintf(name,
sizeof(name),
"ep%dout", i >> 1);
1361 ep->
ep.ops = &mv_u3d_ep_ops;
1362 ep->
ep.maxpacket = (
unsigned short) ~0;
1365 INIT_LIST_HEAD(&ep->
queue);
1377 static void mv_u3d_nuke(
struct mv_u3d_ep *ep,
int status)
1380 mv_u3d_ep_fifo_flush(&ep->
ep);
1382 while (!list_empty(&ep->
queue)) {
1385 mv_u3d_done(ep, req, status);
1403 spin_unlock(&u3d->
lock);
1405 spin_lock(&u3d->
lock);
1409 static void mv_u3d_irq_process_error(
struct mv_u3d *u3d)
1416 static void mv_u3d_irq_process_link_change(
struct mv_u3d *u3d)
1423 dev_dbg(u3d->
dev,
"linkchange: 0x%x\n", linkchange);
1426 dev_dbg(u3d->
dev,
"link up: ltssm state: 0x%x\n",
1473 spin_unlock(&u3d->
lock);
1474 mv_u3d_vbus_session(&u3d->
gadget, 0);
1475 spin_lock(&u3d->
lock);
1480 static void mv_u3d_ch9setaddress(
struct mv_u3d *u3d,
1487 "%s, cannot setaddr in this state (%d)\n",
1498 "%s, u3d address is wrong (out of range)\n", __func__);
1514 mv_u3d_ep0_stall(u3d);
1526 static void mv_u3d_handle_setup_packet(
struct mv_u3d *u3d,
u8 ep_num,
1529 bool delegate =
false;
1533 dev_dbg(u3d->
dev,
"SETUP %02x.%02x v%04x i%04x l%04x\n",
1545 mv_u3d_ch9setaddress(u3d, setup);
1563 if (delegate ==
true) {
1569 spin_unlock(&u3d->
lock);
1573 mv_u3d_ep0_stall(u3d);
1575 spin_lock(&u3d->
lock);
1580 spin_unlock(&u3d->
lock);
1583 mv_u3d_ep0_stall(u3d);
1584 spin_lock(&u3d->
lock);
1587 if (mv_u3d_is_set_configuration(setup)) {
1594 static void mv_u3d_get_setup_data(
struct mv_u3d *u3d,
u8 ep_num,
u8 *buffer_ptr)
1604 static void mv_u3d_irq_process_setup(
struct mv_u3d *u3d)
1610 for (i = 0; i < u3d->
max_eps; i++) {
1611 if (tmp & (1 << i)) {
1612 mv_u3d_get_setup_data(u3d, i,
1614 mv_u3d_handle_setup_packet(u3d, i,
1623 static void mv_u3d_irq_process_tr_complete(
struct mv_u3d *u3d)
1626 int i, ep_num = 0, direction = 0;
1633 dev_dbg(u3d->
dev,
"tr_complete: ep: 0x%x\n", tmp);
1638 for (i = 0; i < u3d->
max_eps * 2; i++) {
1642 bit_pos = 1 << (ep_num + 16 *
direction);
1644 if (!(bit_pos & tmp))
1648 curr_ep = &u3d->
eps[1];
1650 curr_ep = &u3d->
eps[
i];
1653 dev_dbg(u3d->
dev,
"tr comp: check req_list\n");
1655 if (!list_empty(&curr_ep->
req_list)) {
1659 list_del_init(&req->
list);
1667 status = mv_u3d_process_ep_req(u3d, i, curr_req);
1675 mv_u3d_done(curr_ep, curr_req, 0);
1678 mv_u3d_done(curr_ep, curr_req, status);
1682 dev_dbg(u3d->
dev,
"call mv_u3d_start_queue from ep complete\n");
1683 mv_u3d_start_queue(curr_ep);
1694 spin_lock(&u3d->
lock);
1701 spin_unlock(&u3d->
lock);
1720 spin_unlock(&u3d->
lock);
1721 mv_u3d_vbus_session(&u3d->
gadget, 1);
1722 spin_lock(&u3d->
lock);
1731 dev_err(u3d->
dev,
"under run, ep%d\n", trbunderrun);
1733 mv_u3d_irq_process_error(u3d);
1741 dev_err(u3d->
dev,
"desc err 0x%x\n", status);
1742 mv_u3d_irq_process_error(u3d);
1746 mv_u3d_irq_process_link_change(u3d);
1749 mv_u3d_irq_process_tr_complete(u3d);
1752 mv_u3d_irq_process_tr_complete(u3d);
1755 mv_u3d_irq_process_setup(u3d);
1757 spin_unlock(&u3d->
lock);
1761 static void mv_u3d_gadget_release(
struct device *dev)
1763 dev_dbg(dev,
"%s\n", __func__);
1768 struct mv_u3d *u3d = platform_get_drvdata(dev);
1797 platform_set_drvdata(dev,
NULL);
1812 if (!dev->
dev.platform_data) {
1813 dev_err(&dev->
dev,
"missing platform_data\n");
1820 dev_err(&dev->
dev,
"failed to allocate memory for u3d\n");
1822 goto err_alloc_private;
1827 platform_set_drvdata(dev, u3d);
1833 if (IS_ERR(u3d->
clk)) {
1834 retval = PTR_ERR(u3d->
clk);
1840 dev_err(&dev->
dev,
"no I/O memory resource defined\n");
1842 goto err_get_cap_regs;
1848 dev_err(&dev->
dev,
"failed to map I/O memory\n");
1850 goto err_map_cap_regs;
1852 dev_dbg(&dev->
dev,
"cap_regs address: 0x%x/0x%x\n",
1862 dev_err(&dev->
dev,
"init phy error %d\n", retval);
1863 goto err_u3d_enable;
1879 mv_u3d_controller_stop(u3d);
1892 dev_err(&dev->
dev,
"allocate ep context memory failed\n");
1894 goto err_alloc_ep_context;
1907 goto err_alloc_trb_pool;
1913 dev_err(&dev->
dev,
"allocate ep memory failed\n");
1921 dev_err(&dev->
dev,
"allocate status_req memory failed\n");
1923 goto err_alloc_status_req;
1939 dev_err(&dev->
dev,
"no IRQ resource defined\n");
1947 dev_err(&dev->
dev,
"Request irq %d for u3d failed\n",
1950 goto err_request_irq;
1954 u3d->
gadget.ops = &mv_u3d_ops;
1956 INIT_LIST_HEAD(&u3d->
gadget.ep_list);
1962 u3d->
gadget.dev.dma_mask = dev->
dev.dma_mask;
1963 u3d->
gadget.dev.release = mv_u3d_gadget_release;
1968 goto err_register_gadget_device;
1970 mv_u3d_eps_init(u3d);
1975 dev_err(&dev->
dev,
"external vbus detection\n");
1986 goto err_unregister;
1988 dev_dbg(&dev->
dev,
"successful probe usb3 device %s clock gating.\n",
1995 err_register_gadget_device:
2000 err_alloc_status_req:
2007 err_alloc_ep_context:
2017 platform_set_drvdata(dev,
NULL);
2025 static int mv_u3d_suspend(
struct device *dev)
2035 mv_u3d_controller_stop(u3d);
2037 spin_lock_irq(&u3d->
lock);
2039 mv_u3d_stop_activity(u3d, u3d->
driver);
2040 spin_unlock_irq(&u3d->
lock);
2042 mv_u3d_disable(u3d);
2048 static int mv_u3d_resume(
struct device *dev)
2054 retval = mv_u3d_enable(u3d);
2059 mv_u3d_controller_reset(u3d);
2060 mv_u3d_ep0_reset(u3d);
2061 mv_u3d_controller_start(u3d);
2082 .probe = mv_u3d_probe,
2084 .shutdown = mv_u3d_shutdown,
2089 .pm = &mv_u3d_pm_ops,