23 #include <linux/module.h>
24 #include <linux/kernel.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/list.h>
38 #include <linux/usb/ch9.h>
47 #define DRIVER_DESC "Freescale QE/CPM USB Device Controller driver"
48 #define DRIVER_AUTHOR "Xie XiaoBo"
49 #define DRIVER_VERSION "1.0"
51 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
57 static const char *
const ep_name[] = {
69 .bEndpointAddress = 0,
89 list_del_init(&req->
queue);
95 status = req->
req.status;
99 req->
req.dma, req->
req.length,
107 req->
req.dma, req->
req.length,
113 dev_vdbg(udc->
dev,
"complete %s req %p stat %d len %u/%u\n",
114 ep->
ep.name, &req->
req, status,
115 req->
req.actual, req->
req.length);
119 spin_unlock(&udc->
lock);
123 if (req->
req.complete)
124 req->
req.complete(&ep->
ep, &req->
req);
126 spin_lock(&udc->
lock);
134 static void nuke(
struct qe_ep *ep,
int status)
137 while (!list_empty(&ep->
queue)) {
141 done(ep, req, status);
149 static int qe_eprx_stall_change(
struct qe_ep *ep,
int value)
166 static int qe_eptx_stall_change(
struct qe_ep *ep,
int value)
184 static int qe_ep0_stall(
struct qe_udc *udc)
186 qe_eptx_stall_change(&udc->
eps[0], 1);
187 qe_eprx_stall_change(&udc->
eps[0], 1);
193 static int qe_eprx_nack(
struct qe_ep *ep)
200 clrsetbits_be16(&udc->
usb_regs->usb_usep[epnum],
204 clrbits16(&udc->
usb_regs->usb_usbmr,
212 static int qe_eprx_normal(
struct qe_ep *ep)
223 setbits16(&udc->
usb_regs->usb_usbmr,
233 static int qe_ep_cmd_stoptx(
struct qe_ep *ep)
245 static int qe_ep_cmd_restarttx(
struct qe_ep *ep)
257 static int qe_ep_flushtxfifo(
struct qe_ep *ep)
264 qe_ep_cmd_stoptx(ep);
273 qe_ep_cmd_restarttx(ep);
277 static int qe_ep_filltxfifo(
struct qe_ep *ep)
286 static int qe_epbds_reset(
struct qe_udc *udc,
int pipe_num)
293 ep = &udc->
eps[pipe_num];
301 for (i = 0; i < (bdring_len - 1); i++) {
318 static int qe_ep_reset(
struct qe_udc *udc,
int pipe_num)
323 ep = &udc->
eps[pipe_num];
329 qe_ep_flushtxfifo(ep);
335 qe_ep_flushtxfifo(ep);
343 qe_epbds_reset(udc, pipe_num);
348 static int qe_ep_toggledata01(
struct qe_ep *ep)
354 static int qe_ep_bd_init(
struct qe_udc *udc,
unsigned char pipe_num)
356 struct qe_ep *ep = &udc->
eps[pipe_num];
357 unsigned long tmp_addr = 0;
371 USB_BDRING_LEN_TX), QE_ALIGNMENT_OF_BD);
377 (
sizeof(
struct qe_bd) * bdring_len)));
393 for (i = 0; i < bdring_len - 1; i++) {
402 for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) {
413 static int qe_ep_rxbd_update(
struct qe_ep *ep)
419 unsigned int bdring_len;
428 dev_err(ep->
udc->dev,
"malloc rxframe failed\n");
442 dev_err(ep->
udc->dev,
"malloc rxbuffer failed,size=%d\n",
464 tmp = (
u32)(((tmp >> 2) << 2) + 4);
466 for (i = 0; i < bdring_len - 1; i++) {
478 static int qe_ep_register_init(
struct qe_udc *udc,
unsigned char pipe_num)
480 struct qe_ep *ep = &udc->
eps[pipe_num];
492 switch (ep->
ep.desc->bmAttributes & 0x03) {
525 tmp = (
u16)(((tmp >> 2) << 2) + 4);
531 static int qe_ep_init(
struct qe_udc *udc,
532 unsigned char pipe_num,
535 struct qe_ep *ep = &udc->
eps[pipe_num];
540 max = usb_endpoint_maxp(desc);
551 switch (udc->
gadget.speed) {
553 if ((max == 128) || (max == 256) || (max == 512))
572 switch (udc->
gadget.speed) {
589 switch (udc->
gadget.speed) {
604 switch (udc->
gadget.speed) {
642 ep->
ep.maxpacket =
max;
665 qe_ep_bd_init(udc, pipe_num);
667 reval = qe_ep_rxbd_update(ep);
681 qe_ep_register_init(udc, pipe_num);
685 spin_unlock_irqrestore(&udc->
lock, flags);
692 spin_unlock_irqrestore(&udc->
lock, flags);
694 dev_err(udc->
dev,
"failed to initialize %s\n", ep->
ep.name);
698 static inline void qe_usb_enable(
struct qe_udc *udc)
703 static inline void qe_usb_disable(
struct qe_udc *udc)
716 static void recycle_one_rxbd(
struct qe_ep *ep)
730 static void recycle_rxbds(
struct qe_ep *ep,
unsigned char stopatnext)
733 struct qe_bd
__iomem *bd, *nextbd;
734 unsigned char stop = 0;
741 bdstatus = R_E |
R_I | (bdstatus &
R_W);
750 if (stopatnext && (bd == nextbd))
757 static void ep_recycle_rxbds(
struct qe_ep *ep)
765 if (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK)) {
778 recycle_rxbds(ep, 0);
781 recycle_rxbds(ep, 1);
792 static void setup_received_handle(
struct qe_udc *udc,
794 static int qe_ep_rxframe_handle(
struct qe_ep *ep);
795 static void ep0_req_complete(
struct qe_udc *udc,
struct qe_req *req);
797 static int ep0_setup_handle(
struct qe_udc *udc)
821 static int qe_ep0_rx(
struct qe_udc *udc)
832 dev_err(udc->
dev,
"ep0 not a control endpoint\n");
840 while (!(bdstatus & R_E) && length) {
841 if ((bdstatus &
R_F) && (bdstatus &
R_L)
846 "receive a ZLP in status phase\n");
848 qe_frame_clean(pframe);
854 switch (bdstatus &
R_PID) {
867 ep0_setup_handle(udc);
869 qe_ep_rxframe_handle(ep);
872 dev_err(udc->
dev,
"The receive frame with error!\n");
876 recycle_one_rxbd(ep);
894 static int qe_ep_rxframe_handle(
struct qe_ep *ep)
907 if (framepid != ep->
data01) {
913 if (list_empty(&ep->
queue)) {
918 cp = (
u8 *)(req->
req.buf) + req->
req.actual;
921 req->
req.actual += fsize;
922 if ((fsize < ep->ep.maxpacket) ||
923 (req->
req.actual >= req->
req.length)) {
925 ep0_req_complete(ep->
udc, req);
934 qe_ep_toggledata01(ep);
939 static void ep_rx_tasklet(
unsigned long data)
956 "This is a transmit ep or disable tasklet!\n");
965 while (!(bdstatus & R_E) && length) {
966 if (list_empty(&ep->
queue)) {
969 "The rxep have noreq %d\n",
974 if ((bdstatus & R_F) && (bdstatus & R_L)
975 && !(bdstatus & R_ERROR)) {
976 qe_frame_clean(pframe);
982 switch (bdstatus & R_PID) {
994 qe_ep_rxframe_handle(ep);
997 "error in received frame\n");
1004 recycle_one_rxbd(ep);
1019 ep_recycle_rxbds(ep);
1024 spin_unlock_irqrestore(&udc->
lock, flags);
1027 static int qe_ep_rx(
struct qe_ep *ep)
1032 u16 swoffs, ucoffs, emptybds;
1038 dev_err(udc->
dev,
"transmit ep in rx function\n");
1047 if (swoffs < ucoffs)
1050 emptybds = swoffs - ucoffs;
1055 dev_vdbg(udc->
dev,
"%d empty bds, send NACK\n", emptybds);
1059 if (list_empty(&ep->
queue)) {
1061 dev_vdbg(udc->
dev,
"The rxep have no req queued with %d BDs\n",
1078 u32 bdstatus, pidmask;
1082 dev_err(udc->
dev,
"receive ep passed to tx function\n");
1094 if (!(bdstatus & (
T_R | BD_LENGTH_MASK))) {
1103 bdstatus = (bdstatus&
T_W);
1122 bdstatus |= pidmask;
1124 qe_ep_filltxfifo(ep);
1129 qe_ep_toggledata01(ep);
1145 static int txcomplete(
struct qe_ep *ep,
unsigned char restart)
1149 unsigned zlp = 0, last_len = 0;
1155 int asent = ep->
last;
1163 if (req->
req.zero) {
1164 if (last_len == 0 ||
1165 (req->
req.length % ep->
ep.maxpacket) != 0)
1173 if (((ep->
tx_req->req.length - ep->
sent) <= 0) && !zlp) {
1183 if (!list_empty(&ep->
queue)) {
1195 static int qe_usb_senddata(
struct qe_ep *ep,
struct qe_frame *frame)
1200 qe_frame_clean(frame);
1211 return qe_ep_tx(ep, frame);
1224 qe_frame_clean(frame);
1230 return qe_ep_tx(ep, frame);
1233 static int frame_create_tx(
struct qe_ep *ep,
struct qe_frame *frame)
1241 if ((req->
req.length - ep->
sent) > 0)
1242 reval = qe_usb_senddata(ep, frame);
1244 reval = sendnulldata(ep, frame, 0);
1270 static void ep0_req_complete(
struct qe_udc *udc,
struct qe_req *req)
1310 static int ep0_txcomplete(
struct qe_ep *ep,
unsigned char restart)
1324 if (tx_req !=
NULL) {
1326 int asent = ep->
last;
1334 if ((ep->
tx_req->req.length - ep->
sent) <= 0) {
1342 dev_vdbg(ep->
udc->dev,
"the ep0_controller have no req\n");
1348 static int ep0_txframe_handle(
struct qe_ep *ep)
1352 qe_ep_flushtxfifo(ep);
1353 dev_vdbg(ep->
udc->dev,
"The EP0 transmit data have error!\n");
1359 ep0_txcomplete(ep, 1);
1361 ep0_txcomplete(ep, 0);
1363 frame_create_tx(ep, ep->
txframe);
1367 static int qe_ep0_txconf(
struct qe_ep *ep)
1375 while (!(bdstatus &
T_R) && (bdstatus & ~
T_W)) {
1389 if (bdstatus &
T_TO)
1391 if (bdstatus &
T_UN)
1394 ep0_txframe_handle(ep);
1404 static int ep_txframe_handle(
struct qe_ep *ep)
1407 qe_ep_flushtxfifo(ep);
1408 dev_vdbg(ep->
udc->dev,
"The EP0 transmit data have error!\n");
1418 frame_create_tx(ep, ep->
txframe);
1423 static int qe_ep_txconf(
struct qe_ep *ep)
1428 unsigned char breakonrxinterrupt = 0;
1432 while (!(bdstatus & T_R) && (bdstatus & ~T_W)) {
1434 if (bdstatus & DEVICE_T_ERROR) {
1436 if (bdstatus &
T_TO)
1438 if (bdstatus &
T_UN)
1451 ep_txframe_handle(ep);
1455 if (breakonrxinterrupt)
1462 static int ep_req_send(
struct qe_ep *ep,
struct qe_req *req)
1470 reval = frame_create_tx(ep, ep->
txframe);
1476 static int ep_req_rx(
struct qe_ep *ep,
struct qe_req *req)
1487 if (list_empty(&ep->
queue)) {
1497 while (!(bdstatus & R_E) && length) {
1500 if ((bdstatus & R_F) && (bdstatus & R_L)
1501 && !(bdstatus & R_ERROR)) {
1502 qe_frame_clean(pframe);
1507 switch (bdstatus & R_PID) {
1520 if (framepid != ep->
data01) {
1525 cp = (
u8 *)(req->
req.buf) + req->
req.actual;
1528 req->
req.actual += fsize;
1529 if ((fsize < ep->ep.maxpacket)
1530 || (req->
req.actual >=
1534 if (list_empty(&ep->
queue))
1538 qe_ep_toggledata01(ep);
1541 dev_err(udc->
dev,
"The receive frame with error!\n");
1560 ep_recycle_rxbds(ep);
1566 static int ep_req_receive(
struct qe_ep *ep,
struct qe_req *req)
1588 static int qe_ep_enable(
struct usb_ep *_ep,
1594 unsigned char epnum;
1599 if (!_ep || !desc || _ep->
name == ep_name[0] ||
1609 retval = qe_ep_init(udc, epnum, desc);
1619 static int qe_ep_disable(
struct usb_ep *_ep)
1623 unsigned long flags;
1629 if (!_ep || !ep->
ep.desc) {
1640 qe_ep_reset(udc, ep->
epnum);
1641 spin_unlock_irqrestore(&udc->
lock, flags);
1679 req = kzalloc(
sizeof(*req), gfp_flags);
1685 INIT_LIST_HEAD(&req->
queue);
1709 if (!_req || !req->
req.complete || !req->
req.buf
1710 || !list_empty(&req->
queue)) {
1714 if (!_ep || (!ep->
ep.desc &&
ep_index(ep))) {
1735 req->
req.dma, req->
req.length,
1743 req->
req.actual = 0;
1746 dev_vdbg(udc->
dev,
"gadget have request in %s! %d\n",
1751 reval = ep_req_send(ep, req);
1762 reval = ep_req_receive(ep, req);
1773 unsigned long flags;
1777 ret = __qe_ep_queue(_ep, _req);
1778 spin_unlock_irqrestore(&udc->
lock, flags);
1787 unsigned long flags;
1796 if (&req->
req == _req)
1800 if (&req->
req != _req) {
1801 spin_unlock_irqrestore(&ep->
udc->lock, flags);
1807 spin_unlock_irqrestore(&ep->
udc->lock, flags);
1817 static int qe_ep_set_halt(
struct usb_ep *_ep,
int value)
1820 unsigned long flags;
1825 if (!_ep || !ep->
ep.desc) {
1840 qe_eptx_stall_change(ep, value);
1841 qe_eprx_stall_change(ep, value);
1842 spin_unlock_irqrestore(&ep->
udc->lock, flags);
1844 if (ep->
epnum == 0) {
1854 value ?
"set" :
"clear", status);
1860 .enable = qe_ep_enable,
1861 .disable = qe_ep_disable,
1863 .alloc_request = qe_alloc_request,
1864 .free_request = qe_free_request,
1866 .queue = qe_ep_queue,
1867 .dequeue = qe_ep_dequeue,
1869 .set_halt = qe_ep_set_halt,
1877 static int qe_get_frame(
struct usb_gadget *gadget)
1891 static int fsl_qe_start(
struct usb_gadget *gadget,
1893 static int fsl_qe_stop(
struct usb_gadget *gadget,
1898 .get_frame = qe_get_frame,
1899 .udc_start = fsl_qe_start,
1900 .udc_stop = fsl_qe_stop,
1906 static int udc_reset_ep_queue(
struct qe_udc *udc,
u8 pipe)
1915 static int reset_queues(
struct qe_udc *udc)
1920 udc_reset_ep_queue(udc, pipe);
1923 spin_unlock(&udc->
lock);
1925 spin_lock(&udc->
lock);
1974 if (!target_ep->
ep.desc)
1979 if (target_ep->
dir != USB_DIR_IN)
1993 req->
req.length = 2;
1997 req->
req.actual = 0;
1998 req->
req.complete = ownercomplete;
2003 status = __qe_ep_queue(&ep->
ep, &req->
req);
2008 dev_err(udc->
dev,
"Can't respond to getstatus request \n");
2013 static void setup_received_handle(
struct qe_udc *udc,
2022 udc_reset_ep_queue(udc, 0);
2044 ch9setaddress(udc, wValue, wIndex, wLength);
2059 if (wValue != 0 || wLength != 0
2060 || pipe > USB_MAX_ENDPOINTS)
2064 spin_unlock(&udc->
lock);
2065 qe_ep_set_halt(&ep->
ep,
2068 spin_lock(&udc->
lock);
2071 ep0_prime_status(udc, USB_DIR_IN);
2088 spin_unlock(&udc->
lock);
2092 spin_lock(&udc->
lock);
2096 spin_unlock(&udc->
lock);
2100 spin_lock(&udc->
lock);
2108 static void suspend_irq(
struct qe_udc *udc)
2114 if (udc->
driver->suspend)
2118 static void resume_irq(
struct qe_udc *udc)
2128 static void idle_irq(
struct qe_udc *udc)
2142 static int reset_irq(
struct qe_udc *udc)
2149 qe_usb_disable(udc);
2153 if (udc->
eps[i].init)
2154 qe_ep_reset(udc, i);
2165 static int bsy_irq(
struct qe_udc *udc)
2170 static int txe_irq(
struct qe_udc *udc)
2176 static int tx_irq(
struct qe_udc *udc)
2186 for (i = (USB_MAX_ENDPOINTS-1); ((i >= 0) && (res == 0)); i--) {
2194 res = qe_ep0_txconf(ep);
2196 res = qe_ep_txconf(ep);
2205 static void rx_irq(
struct qe_udc *udc)
2213 if (ep && ep->
init && (ep->
dir != USB_DIR_IN)) {
2217 if (ep->
epnum == 0) {
2228 static irqreturn_t qe_udc_irq(
int irq,
void *_udc)
2233 unsigned long flags;
2244 irq_src &= ~USB_E_IDLE_MASK;
2250 irq_src &= ~USB_E_TXB_MASK;
2256 irq_src &= ~USB_E_RXB_MASK;
2262 irq_src &= ~USB_E_RESET_MASK;
2268 irq_src &= ~USB_E_BSY_MASK;
2274 irq_src &= ~USB_E_TXE_MASK;
2278 spin_unlock_irqrestore(&udc->
lock, flags);
2286 static int fsl_qe_start(
struct usb_gadget *gadget,
2290 unsigned long flags;
2310 spin_unlock_irqrestore(&udc->
lock, flags);
2317 static int fsl_qe_stop(
struct usb_gadget *gadget,
2321 struct qe_ep *loop_ep;
2322 unsigned long flags;
2326 qe_usb_disable(udc);
2339 spin_unlock_irqrestore(&udc->lock, flags);
2341 udc->gadget.
dev.driver =
NULL;
2345 driver->driver.
name);
2354 unsigned int tmp_addr = 0;
2362 dev_err(&ofdev->dev,
"malloc udc failed\n");
2366 udc->
dev = &ofdev->dev;
2423 static int __devinit qe_ep_config(
struct qe_udc *udc,
unsigned char pipe_num)
2425 struct qe_ep *ep = &udc->
eps[pipe_num];
2429 ep->
ep.name = ep_name[pipe_num];
2431 ep->
ep.ops = &qe_ep_ops;
2433 ep->
ep.maxpacket = (
unsigned short) ~0;
2447 INIT_LIST_HEAD(&ep->
queue);
2461 static void qe_udc_release(
struct device *dev)
2482 unsigned int ret = 0;
2491 if (!prop ||
strcmp(prop,
"peripheral"))
2495 udc = qe_udc_config(ofdev);
2497 dev_err(&ofdev->
dev,
"failed to initialize\n");
2510 qe_udc_reg_init(udc);
2514 udc->
gadget.ops = &qe_gadget_ops;
2519 INIT_LIST_HEAD(&udc->
gadget.ep_list);
2531 udc->
gadget.dev.release = qe_udc_release;
2541 qe_ep_config(udc, (
unsigned char)i);
2545 ret = qe_ep_init(udc, 0, &qe_ep0_desc);
2579 (
unsigned long)udc);
2590 dev_err(udc->
dev,
"cannot request irq %d err %d\n",
2605 "%s USB controller initialized as device\n",
2715 .compatible =
"fsl,mpc8323-qe-usb",
2719 .compatible =
"fsl,mpc8360-qe-usb",
2723 .compatible =
"fsl,mpc8272-cpm-usb",
2735 .of_match_table = qe_udc_match,
2737 .
probe = qe_udc_probe,
2740 .suspend = qe_udc_suspend,
2741 .resume = qe_udc_resume,