2 * amd5536.c -- AMD 5536 UDC high/full speed USB device controller
4 * Copyright (C) 2005-2007 AMD (http://www.amd.com)
5 * Author: Thomas Dahlmann
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * The AMD5536 UDC is part of the x86 southbridge AMD Geode CS5536.
24 * It is a USB Highspeed DMA capable USB device controller. Beside ep0 it
25 * provides 4 IN and 4 OUT endpoints (bulk or interrupt type).
27 * Make sure that UDC is assigned to port 4 by BIOS settings (port can also
28 * be used as host port) and UOC bits PAD_EN and APU are set (should be done
31 * UDC DMA requires 32-bit aligned buffers so DMA with gadget ether does not
32 * work without updating NET_IP_ALIGN. Or PIO mode (module param "use_dma=0")
33 * can be used with gadget ether.
37 /* #define UDC_VERBOSE */
40 #define UDC_MOD_DESCRIPTION "AMD 5536 UDC - USB Device Controller"
41 #define UDC_DRIVER_VERSION_STRING "01.00.0206 - $Revision: #3 $"
44 #include <linux/module.h>
45 #include <linux/pci.h>
46 #include <linux/kernel.h>
47 #include <linux/delay.h>
48 #include <linux/ioport.h>
49 #include <linux/sched.h>
50 #include <linux/slab.h>
51 #include <linux/errno.h>
52 #include <linux/init.h>
53 #include <linux/timer.h>
54 #include <linux/list.h>
55 #include <linux/interrupt.h>
56 #include <linux/ioctl.h>
58 #include <linux/dmapool.h>
59 #include <linux/moduleparam.h>
60 #include <linux/device.h>
62 #include <linux/irq.h>
63 #include <linux/prefetch.h>
65 #include <asm/byteorder.h>
66 #include <asm/system.h>
67 #include <asm/unaligned.h>
70 #include <linux/usb/ch9.h>
71 #include <linux/usb/gadget.h>
74 #include "amd5536udc.h"
77 static void udc_tasklet_disconnect(unsigned long);
78 static void empty_req_queue(struct udc_ep *);
79 static int udc_probe(struct udc *dev);
80 static void udc_basic_init(struct udc *dev);
81 static void udc_setup_endpoints(struct udc *dev);
82 static void udc_soft_reset(struct udc *dev);
83 static struct udc_request *udc_alloc_bna_dummy(struct udc_ep *ep);
84 static void udc_free_request(struct usb_ep *usbep, struct usb_request *usbreq);
85 static int udc_free_dma_chain(struct udc *dev, struct udc_request *req);
86 static int udc_create_dma_chain(struct udc_ep *ep, struct udc_request *req,
87 unsigned long buf_len, gfp_t gfp_flags);
88 static int udc_remote_wakeup(struct udc *dev);
89 static int udc_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id);
90 static void udc_pci_remove(struct pci_dev *pdev);
93 static const char mod_desc[] = UDC_MOD_DESCRIPTION;
94 static const char name[] = "amd5536udc";
96 /* structure to hold endpoint function pointers */
97 static const struct usb_ep_ops udc_ep_ops;
99 /* received setup data */
100 static union udc_setup_data setup_data;
102 /* pointer to device object */
103 static struct udc *udc;
105 /* irq spin lock for soft reset */
106 static DEFINE_SPINLOCK(udc_irq_spinlock);
107 /* stall spin lock */
108 static DEFINE_SPINLOCK(udc_stall_spinlock);
111 * slave mode: pending bytes in rx fifo after nyet,
112 * used if EPIN irq came but no req was available
114 static unsigned int udc_rxfifo_pending;
116 /* count soft resets after suspend to avoid loop */
117 static int soft_reset_occured;
118 static int soft_reset_after_usbreset_occured;
121 static struct timer_list udc_timer;
122 static int stop_timer;
124 /* set_rde -- Is used to control enabling of RX DMA. Problem is
125 * that UDC has only one bit (RDE) to enable/disable RX DMA for
126 * all OUT endpoints. So we have to handle race conditions like
127 * when OUT data reaches the fifo but no request was queued yet.
128 * This cannot be solved by letting the RX DMA disabled until a
129 * request gets queued because there may be other OUT packets
130 * in the FIFO (important for not blocking control traffic).
131 * The value of set_rde controls the correspondig timer.
133 * set_rde -1 == not used, means it is alloed to be set to 0 or 1
134 * set_rde 0 == do not touch RDE, do no start the RDE timer
135 * set_rde 1 == timer function will look whether FIFO has data
136 * set_rde 2 == set by timer function to enable RX DMA on next call
138 static int set_rde = -1;
140 static DECLARE_COMPLETION(on_exit);
141 static struct timer_list udc_pollstall_timer;
142 static int stop_pollstall_timer;
143 static DECLARE_COMPLETION(on_pollstall_exit);
145 /* tasklet for usb disconnect */
146 static DECLARE_TASKLET(disconnect_tasklet, udc_tasklet_disconnect,
147 (unsigned long) &udc);
150 /* endpoint names used for print */
151 static const char ep0_string[] = "ep0in";
152 static const char *ep_string[] = {
154 "ep1in-int", "ep2in-bulk", "ep3in-bulk", "ep4in-bulk", "ep5in-bulk",
155 "ep6in-bulk", "ep7in-bulk", "ep8in-bulk", "ep9in-bulk", "ep10in-bulk",
156 "ep11in-bulk", "ep12in-bulk", "ep13in-bulk", "ep14in-bulk",
157 "ep15in-bulk", "ep0out", "ep1out-bulk", "ep2out-bulk", "ep3out-bulk",
158 "ep4out-bulk", "ep5out-bulk", "ep6out-bulk", "ep7out-bulk",
159 "ep8out-bulk", "ep9out-bulk", "ep10out-bulk", "ep11out-bulk",
160 "ep12out-bulk", "ep13out-bulk", "ep14out-bulk", "ep15out-bulk"
164 static int use_dma = 1;
165 /* packet per buffer dma */
166 static int use_dma_ppb = 1;
167 /* with per descr. update */
168 static int use_dma_ppb_du;
169 /* buffer fill mode */
170 static int use_dma_bufferfill_mode;
171 /* full speed only mode */
172 static int use_fullspeed;
173 /* tx buffer size for high speed */
174 static unsigned long hs_tx_buf = UDC_EPIN_BUFF_SIZE;
176 /* module parameters */
177 module_param(use_dma, bool, S_IRUGO);
178 MODULE_PARM_DESC(use_dma, "true for DMA");
179 module_param(use_dma_ppb, bool, S_IRUGO);
180 MODULE_PARM_DESC(use_dma_ppb, "true for DMA in packet per buffer mode");
181 module_param(use_dma_ppb_du, bool, S_IRUGO);
182 MODULE_PARM_DESC(use_dma_ppb_du,
183 "true for DMA in packet per buffer mode with descriptor update");
184 module_param(use_fullspeed, bool, S_IRUGO);
185 MODULE_PARM_DESC(use_fullspeed, "true for fullspeed only");
187 /*---------------------------------------------------------------------------*/
188 /* Prints UDC device registers and endpoint irq registers */
189 static void print_regs(struct udc *dev)
191 DBG(dev, "------- Device registers -------\n");
192 DBG(dev, "dev config = %08x\n", readl(&dev->regs->cfg));
193 DBG(dev, "dev control = %08x\n", readl(&dev->regs->ctl));
194 DBG(dev, "dev status = %08x\n", readl(&dev->regs->sts));
196 DBG(dev, "dev int's = %08x\n", readl(&dev->regs->irqsts));
197 DBG(dev, "dev intmask = %08x\n", readl(&dev->regs->irqmsk));
199 DBG(dev, "dev ep int's = %08x\n", readl(&dev->regs->ep_irqsts));
200 DBG(dev, "dev ep intmask = %08x\n", readl(&dev->regs->ep_irqmsk));
202 DBG(dev, "USE DMA = %d\n", use_dma);
203 if (use_dma && use_dma_ppb && !use_dma_ppb_du) {
204 DBG(dev, "DMA mode = PPBNDU (packet per buffer "
205 "WITHOUT desc. update)\n");
206 dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBNDU");
207 } else if (use_dma && use_dma_ppb && use_dma_ppb_du) {
208 DBG(dev, "DMA mode = PPBDU (packet per buffer "
209 "WITH desc. update)\n");
210 dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBDU");
212 if (use_dma && use_dma_bufferfill_mode) {
213 DBG(dev, "DMA mode = BF (buffer fill mode)\n");
214 dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "BF");
217 dev_info(&dev->pdev->dev, "FIFO mode\n");
219 DBG(dev, "-------------------------------------------------------\n");
222 /* Masks unused interrupts */
223 static int udc_mask_unused_interrupts(struct udc *dev)
227 /* mask all dev interrupts */
228 tmp = AMD_BIT(UDC_DEVINT_SVC) |
229 AMD_BIT(UDC_DEVINT_ENUM) |
230 AMD_BIT(UDC_DEVINT_US) |
231 AMD_BIT(UDC_DEVINT_UR) |
232 AMD_BIT(UDC_DEVINT_ES) |
233 AMD_BIT(UDC_DEVINT_SI) |
234 AMD_BIT(UDC_DEVINT_SOF)|
235 AMD_BIT(UDC_DEVINT_SC);
236 writel(tmp, &dev->regs->irqmsk);
238 /* mask all ep interrupts */
239 writel(UDC_EPINT_MSK_DISABLE_ALL, &dev->regs->ep_irqmsk);
244 /* Enables endpoint 0 interrupts */
245 static int udc_enable_ep0_interrupts(struct udc *dev)
249 DBG(dev, "udc_enable_ep0_interrupts()\n");
252 tmp = readl(&dev->regs->ep_irqmsk);
253 /* enable ep0 irq's */
254 tmp &= AMD_UNMASK_BIT(UDC_EPINT_IN_EP0)
255 & AMD_UNMASK_BIT(UDC_EPINT_OUT_EP0);
256 writel(tmp, &dev->regs->ep_irqmsk);
261 /* Enables device interrupts for SET_INTF and SET_CONFIG */
262 static int udc_enable_dev_setup_interrupts(struct udc *dev)
266 DBG(dev, "enable device interrupts for setup data\n");
269 tmp = readl(&dev->regs->irqmsk);
271 /* enable SET_INTERFACE, SET_CONFIG and other needed irq's */
272 tmp &= AMD_UNMASK_BIT(UDC_DEVINT_SI)
273 & AMD_UNMASK_BIT(UDC_DEVINT_SC)
274 & AMD_UNMASK_BIT(UDC_DEVINT_UR)
275 & AMD_UNMASK_BIT(UDC_DEVINT_SVC)
276 & AMD_UNMASK_BIT(UDC_DEVINT_ENUM);
277 writel(tmp, &dev->regs->irqmsk);
282 /* Calculates fifo start of endpoint based on preceding endpoints */
283 static int udc_set_txfifo_addr(struct udc_ep *ep)
289 if (!ep || !(ep->in))
293 ep->txfifo = dev->txfifo;
296 for (i = 0; i < ep->num; i++) {
297 if (dev->ep[i].regs) {
299 tmp = readl(&dev->ep[i].regs->bufin_framenum);
300 tmp = AMD_GETBITS(tmp, UDC_EPIN_BUFF_SIZE);
307 /* CNAK pending field: bit0 = ep0in, bit16 = ep0out */
308 static u32 cnak_pending;
310 static void UDC_QUEUE_CNAK(struct udc_ep *ep, unsigned num)
312 if (readl(&ep->regs->ctl) & AMD_BIT(UDC_EPCTL_NAK)) {
313 DBG(ep->dev, "NAK could not be cleared for ep%d\n", num);
314 cnak_pending |= 1 << (num);
317 cnak_pending = cnak_pending & (~(1 << (num)));
321 /* Enables endpoint, is called by gadget driver */
323 udc_ep_enable(struct usb_ep *usbep, const struct usb_endpoint_descriptor *desc)
328 unsigned long iflags;
333 || usbep->name == ep0_string
335 || desc->bDescriptorType != USB_DT_ENDPOINT)
338 ep = container_of(usbep, struct udc_ep, ep);
341 DBG(dev, "udc_ep_enable() ep %d\n", ep->num);
343 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
346 spin_lock_irqsave(&dev->lock, iflags);
351 /* set traffic type */
352 tmp = readl(&dev->ep[ep->num].regs->ctl);
353 tmp = AMD_ADDBITS(tmp, desc->bmAttributes, UDC_EPCTL_ET);
354 writel(tmp, &dev->ep[ep->num].regs->ctl);
356 /* set max packet size */
357 maxpacket = le16_to_cpu(desc->wMaxPacketSize);
358 tmp = readl(&dev->ep[ep->num].regs->bufout_maxpkt);
359 tmp = AMD_ADDBITS(tmp, maxpacket, UDC_EP_MAX_PKT_SIZE);
360 ep->ep.maxpacket = maxpacket;
361 writel(tmp, &dev->ep[ep->num].regs->bufout_maxpkt);
366 /* ep ix in UDC CSR register space */
367 udc_csr_epix = ep->num;
369 /* set buffer size (tx fifo entries) */
370 tmp = readl(&dev->ep[ep->num].regs->bufin_framenum);
371 /* double buffering: fifo size = 2 x max packet size */
374 maxpacket * UDC_EPIN_BUFF_SIZE_MULT
377 writel(tmp, &dev->ep[ep->num].regs->bufin_framenum);
379 /* calc. tx fifo base addr */
380 udc_set_txfifo_addr(ep);
383 tmp = readl(&ep->regs->ctl);
384 tmp |= AMD_BIT(UDC_EPCTL_F);
385 writel(tmp, &ep->regs->ctl);
389 /* ep ix in UDC CSR register space */
390 udc_csr_epix = ep->num - UDC_CSR_EP_OUT_IX_OFS;
392 /* set max packet size UDC CSR */
393 tmp = readl(&dev->csr->ne[ep->num - UDC_CSR_EP_OUT_IX_OFS]);
394 tmp = AMD_ADDBITS(tmp, maxpacket,
396 writel(tmp, &dev->csr->ne[ep->num - UDC_CSR_EP_OUT_IX_OFS]);
398 if (use_dma && !ep->in) {
399 /* alloc and init BNA dummy request */
400 ep->bna_dummy_req = udc_alloc_bna_dummy(ep);
401 ep->bna_occurred = 0;
404 if (ep->num != UDC_EP0OUT_IX)
405 dev->data_ep_enabled = 1;
409 tmp = readl(&dev->csr->ne[udc_csr_epix]);
411 tmp = AMD_ADDBITS(tmp, maxpacket, UDC_CSR_NE_MAX_PKT);
413 tmp = AMD_ADDBITS(tmp, desc->bEndpointAddress, UDC_CSR_NE_NUM);
415 tmp = AMD_ADDBITS(tmp, ep->in, UDC_CSR_NE_DIR);
417 tmp = AMD_ADDBITS(tmp, desc->bmAttributes, UDC_CSR_NE_TYPE);
419 tmp = AMD_ADDBITS(tmp, ep->dev->cur_config, UDC_CSR_NE_CFG);
421 tmp = AMD_ADDBITS(tmp, ep->dev->cur_intf, UDC_CSR_NE_INTF);
423 tmp = AMD_ADDBITS(tmp, ep->dev->cur_alt, UDC_CSR_NE_ALT);
425 writel(tmp, &dev->csr->ne[udc_csr_epix]);
428 tmp = readl(&dev->regs->ep_irqmsk);
429 tmp &= AMD_UNMASK_BIT(ep->num);
430 writel(tmp, &dev->regs->ep_irqmsk);
433 * clear NAK by writing CNAK
434 * avoid BNA for OUT DMA, don't clear NAK until DMA desc. written
436 if (!use_dma || ep->in) {
437 tmp = readl(&ep->regs->ctl);
438 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
439 writel(tmp, &ep->regs->ctl);
441 UDC_QUEUE_CNAK(ep, ep->num);
443 tmp = desc->bEndpointAddress;
444 DBG(dev, "%s enabled\n", usbep->name);
446 spin_unlock_irqrestore(&dev->lock, iflags);
450 /* Resets endpoint */
451 static void ep_init(struct udc_regs __iomem *regs, struct udc_ep *ep)
455 VDBG(ep->dev, "ep-%d reset\n", ep->num);
457 ep->ep.ops = &udc_ep_ops;
458 INIT_LIST_HEAD(&ep->queue);
460 ep->ep.maxpacket = (u16) ~0;
462 tmp = readl(&ep->regs->ctl);
463 tmp |= AMD_BIT(UDC_EPCTL_SNAK);
464 writel(tmp, &ep->regs->ctl);
467 /* disable interrupt */
468 tmp = readl(®s->ep_irqmsk);
469 tmp |= AMD_BIT(ep->num);
470 writel(tmp, ®s->ep_irqmsk);
473 /* unset P and IN bit of potential former DMA */
474 tmp = readl(&ep->regs->ctl);
475 tmp &= AMD_UNMASK_BIT(UDC_EPCTL_P);
476 writel(tmp, &ep->regs->ctl);
478 tmp = readl(&ep->regs->sts);
479 tmp |= AMD_BIT(UDC_EPSTS_IN);
480 writel(tmp, &ep->regs->sts);
483 tmp = readl(&ep->regs->ctl);
484 tmp |= AMD_BIT(UDC_EPCTL_F);
485 writel(tmp, &ep->regs->ctl);
488 /* reset desc pointer */
489 writel(0, &ep->regs->desptr);
492 /* Disables endpoint, is called by gadget driver */
493 static int udc_ep_disable(struct usb_ep *usbep)
495 struct udc_ep *ep = NULL;
496 unsigned long iflags;
501 ep = container_of(usbep, struct udc_ep, ep);
502 if (usbep->name == ep0_string || !ep->desc)
505 DBG(ep->dev, "Disable ep-%d\n", ep->num);
507 spin_lock_irqsave(&ep->dev->lock, iflags);
508 udc_free_request(&ep->ep, &ep->bna_dummy_req->req);
510 ep_init(ep->dev->regs, ep);
511 spin_unlock_irqrestore(&ep->dev->lock, iflags);
516 /* Allocates request packet, called by gadget driver */
517 static struct usb_request *
518 udc_alloc_request(struct usb_ep *usbep, gfp_t gfp)
520 struct udc_request *req;
521 struct udc_data_dma *dma_desc;
527 ep = container_of(usbep, struct udc_ep, ep);
529 VDBG(ep->dev, "udc_alloc_req(): ep%d\n", ep->num);
530 req = kzalloc(sizeof(struct udc_request), gfp);
534 req->req.dma = DMA_DONT_USE;
535 INIT_LIST_HEAD(&req->queue);
538 /* ep0 in requests are allocated from data pool here */
539 dma_desc = pci_pool_alloc(ep->dev->data_requests, gfp,
546 VDBG(ep->dev, "udc_alloc_req: req = %p dma_desc = %p, "
549 (unsigned long)req->td_phys);
550 /* prevent from using desc. - set HOST BUSY */
551 dma_desc->status = AMD_ADDBITS(dma_desc->status,
552 UDC_DMA_STP_STS_BS_HOST_BUSY,
554 dma_desc->bufptr = cpu_to_le32(DMA_DONT_USE);
555 req->td_data = dma_desc;
556 req->td_data_last = NULL;
563 /* Frees request packet, called by gadget driver */
565 udc_free_request(struct usb_ep *usbep, struct usb_request *usbreq)
568 struct udc_request *req;
570 if (!usbep || !usbreq)
573 ep = container_of(usbep, struct udc_ep, ep);
574 req = container_of(usbreq, struct udc_request, req);
575 VDBG(ep->dev, "free_req req=%p\n", req);
576 BUG_ON(!list_empty(&req->queue));
578 VDBG(ep->dev, "req->td_data=%p\n", req->td_data);
580 /* free dma chain if created */
581 if (req->chain_len > 1) {
582 udc_free_dma_chain(ep->dev, req);
585 pci_pool_free(ep->dev->data_requests, req->td_data,
591 /* Init BNA dummy descriptor for HOST BUSY and pointing to itself */
592 static void udc_init_bna_dummy(struct udc_request *req)
596 req->td_data->status |= AMD_BIT(UDC_DMA_IN_STS_L);
597 /* set next pointer to itself */
598 req->td_data->next = req->td_phys;
601 = AMD_ADDBITS(req->td_data->status,
602 UDC_DMA_STP_STS_BS_DMA_DONE,
605 pr_debug("bna desc = %p, sts = %08x\n",
606 req->td_data, req->td_data->status);
611 /* Allocate BNA dummy descriptor */
612 static struct udc_request *udc_alloc_bna_dummy(struct udc_ep *ep)
614 struct udc_request *req = NULL;
615 struct usb_request *_req = NULL;
617 /* alloc the dummy request */
618 _req = udc_alloc_request(&ep->ep, GFP_ATOMIC);
620 req = container_of(_req, struct udc_request, req);
621 ep->bna_dummy_req = req;
622 udc_init_bna_dummy(req);
627 /* Write data to TX fifo for IN packets */
629 udc_txfifo_write(struct udc_ep *ep, struct usb_request *req)
635 unsigned remaining = 0;
640 req_buf = req->buf + req->actual;
642 remaining = req->length - req->actual;
644 buf = (u32 *) req_buf;
646 bytes = ep->ep.maxpacket;
647 if (bytes > remaining)
651 for (i = 0; i < bytes / UDC_DWORD_BYTES; i++) {
652 writel(*(buf + i), ep->txfifo);
655 /* remaining bytes must be written by byte access */
656 for (j = 0; j < bytes % UDC_DWORD_BYTES; j++) {
657 writeb((u8)(*(buf + i) >> (j << UDC_BITS_PER_BYTE_SHIFT)),
661 /* dummy write confirm */
662 writel(0, &ep->regs->confirm);
665 /* Read dwords from RX fifo for OUT transfers */
666 static int udc_rxfifo_read_dwords(struct udc *dev, u32 *buf, int dwords)
670 VDBG(dev, "udc_read_dwords(): %d dwords\n", dwords);
672 for (i = 0; i < dwords; i++) {
673 *(buf + i) = readl(dev->rxfifo);
678 /* Read bytes from RX fifo for OUT transfers */
679 static int udc_rxfifo_read_bytes(struct udc *dev, u8 *buf, int bytes)
684 VDBG(dev, "udc_read_bytes(): %d bytes\n", bytes);
687 for (i = 0; i < bytes / UDC_DWORD_BYTES; i++) {
688 *((u32 *)(buf + (i<<2))) = readl(dev->rxfifo);
691 /* remaining bytes must be read by byte access */
692 if (bytes % UDC_DWORD_BYTES) {
693 tmp = readl(dev->rxfifo);
694 for (j = 0; j < bytes % UDC_DWORD_BYTES; j++) {
695 *(buf + (i<<2) + j) = (u8)(tmp & UDC_BYTE_MASK);
696 tmp = tmp >> UDC_BITS_PER_BYTE;
703 /* Read data from RX fifo for OUT transfers */
705 udc_rxfifo_read(struct udc_ep *ep, struct udc_request *req)
710 unsigned finished = 0;
712 /* received number bytes */
713 bytes = readl(&ep->regs->sts);
714 bytes = AMD_GETBITS(bytes, UDC_EPSTS_RX_PKT_SIZE);
716 buf_space = req->req.length - req->req.actual;
717 buf = req->req.buf + req->req.actual;
718 if (bytes > buf_space) {
719 if ((buf_space % ep->ep.maxpacket) != 0) {
721 "%s: rx %d bytes, rx-buf space = %d bytesn\n",
722 ep->ep.name, bytes, buf_space);
723 req->req.status = -EOVERFLOW;
727 req->req.actual += bytes;
730 if (((bytes % ep->ep.maxpacket) != 0) || (!bytes)
731 || ((req->req.actual == req->req.length) && !req->req.zero))
734 /* read rx fifo bytes */
735 VDBG(ep->dev, "ep %s: rxfifo read %d bytes\n", ep->ep.name, bytes);
736 udc_rxfifo_read_bytes(ep->dev, buf, bytes);
741 /* create/re-init a DMA descriptor or a DMA descriptor chain */
742 static int prep_dma(struct udc_ep *ep, struct udc_request *req, gfp_t gfp)
747 VDBG(ep->dev, "prep_dma\n");
748 VDBG(ep->dev, "prep_dma ep%d req->td_data=%p\n",
749 ep->num, req->td_data);
751 /* set buffer pointer */
752 req->td_data->bufptr = req->req.dma;
755 req->td_data->status |= AMD_BIT(UDC_DMA_IN_STS_L);
757 /* build/re-init dma chain if maxpkt scatter mode, not for EP0 */
760 retval = udc_create_dma_chain(ep, req, ep->ep.maxpacket, gfp);
762 if (retval == -ENOMEM)
763 DBG(ep->dev, "Out of DMA memory\n");
767 if (req->req.length == ep->ep.maxpacket) {
769 req->td_data->status =
770 AMD_ADDBITS(req->td_data->status,
772 UDC_DMA_IN_STS_TXBYTES);
780 VDBG(ep->dev, "IN: use_dma_ppb=%d req->req.len=%d "
781 "maxpacket=%d ep%d\n",
782 use_dma_ppb, req->req.length,
783 ep->ep.maxpacket, ep->num);
785 * if bytes < max packet then tx bytes must
786 * be written in packet per buffer mode
788 if (!use_dma_ppb || req->req.length < ep->ep.maxpacket
789 || ep->num == UDC_EP0OUT_IX
790 || ep->num == UDC_EP0IN_IX) {
792 req->td_data->status =
793 AMD_ADDBITS(req->td_data->status,
795 UDC_DMA_IN_STS_TXBYTES);
796 /* reset frame num */
797 req->td_data->status =
798 AMD_ADDBITS(req->td_data->status,
800 UDC_DMA_IN_STS_FRAMENUM);
803 req->td_data->status =
804 AMD_ADDBITS(req->td_data->status,
805 UDC_DMA_STP_STS_BS_HOST_BUSY,
808 VDBG(ep->dev, "OUT set host ready\n");
810 req->td_data->status =
811 AMD_ADDBITS(req->td_data->status,
812 UDC_DMA_STP_STS_BS_HOST_READY,
816 /* clear NAK by writing CNAK */
818 tmp = readl(&ep->regs->ctl);
819 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
820 writel(tmp, &ep->regs->ctl);
822 UDC_QUEUE_CNAK(ep, ep->num);
830 /* Completes request packet ... caller MUST hold lock */
832 complete_req(struct udc_ep *ep, struct udc_request *req, int sts)
833 __releases(ep->dev->lock)
834 __acquires(ep->dev->lock)
839 VDBG(ep->dev, "complete_req(): ep%d\n", ep->num);
843 if (req->dma_mapping) {
845 pci_unmap_single(dev->pdev,
850 pci_unmap_single(dev->pdev,
854 req->dma_mapping = 0;
855 req->req.dma = DMA_DONT_USE;
861 /* set new status if pending */
862 if (req->req.status == -EINPROGRESS)
863 req->req.status = sts;
865 /* remove from ep queue */
866 list_del_init(&req->queue);
868 VDBG(ep->dev, "req %p => complete %d bytes at %s with sts %d\n",
869 &req->req, req->req.length, ep->ep.name, sts);
871 spin_unlock(&dev->lock);
872 req->req.complete(&ep->ep, &req->req);
873 spin_lock(&dev->lock);
877 /* frees pci pool descriptors of a DMA chain */
878 static int udc_free_dma_chain(struct udc *dev, struct udc_request *req)
882 struct udc_data_dma *td;
883 struct udc_data_dma *td_last = NULL;
886 DBG(dev, "free chain req = %p\n", req);
888 /* do not free first desc., will be done by free for request */
889 td_last = req->td_data;
890 td = phys_to_virt(td_last->next);
892 for (i = 1; i < req->chain_len; i++) {
894 pci_pool_free(dev->data_requests, td,
895 (dma_addr_t) td_last->next);
897 td = phys_to_virt(td_last->next);
903 /* Iterates to the end of a DMA chain and returns last descriptor */
904 static struct udc_data_dma *udc_get_last_dma_desc(struct udc_request *req)
906 struct udc_data_dma *td;
909 while (td && !(td->status & AMD_BIT(UDC_DMA_IN_STS_L))) {
910 td = phys_to_virt(td->next);
917 /* Iterates to the end of a DMA chain and counts bytes received */
918 static u32 udc_get_ppbdu_rxbytes(struct udc_request *req)
920 struct udc_data_dma *td;
924 /* received number bytes */
925 count = AMD_GETBITS(td->status, UDC_DMA_OUT_STS_RXBYTES);
927 while (td && !(td->status & AMD_BIT(UDC_DMA_IN_STS_L))) {
928 td = phys_to_virt(td->next);
929 /* received number bytes */
931 count += AMD_GETBITS(td->status,
932 UDC_DMA_OUT_STS_RXBYTES);
940 /* Creates or re-inits a DMA chain */
941 static int udc_create_dma_chain(
943 struct udc_request *req,
944 unsigned long buf_len, gfp_t gfp_flags
947 unsigned long bytes = req->req.length;
950 struct udc_data_dma *td = NULL;
951 struct udc_data_dma *last = NULL;
952 unsigned long txbytes;
953 unsigned create_new_chain = 0;
956 VDBG(ep->dev, "udc_create_dma_chain: bytes=%ld buf_len=%ld\n",
958 dma_addr = DMA_DONT_USE;
960 /* unset L bit in first desc for OUT */
962 req->td_data->status &= AMD_CLEAR_BIT(UDC_DMA_IN_STS_L);
965 /* alloc only new desc's if not already available */
966 len = req->req.length / ep->ep.maxpacket;
967 if (req->req.length % ep->ep.maxpacket) {
971 if (len > req->chain_len) {
972 /* shorter chain already allocated before */
973 if (req->chain_len > 1) {
974 udc_free_dma_chain(ep->dev, req);
976 req->chain_len = len;
977 create_new_chain = 1;
981 /* gen. required number of descriptors and buffers */
982 for (i = buf_len; i < bytes; i += buf_len) {
983 /* create or determine next desc. */
984 if (create_new_chain) {
986 td = pci_pool_alloc(ep->dev->data_requests,
987 gfp_flags, &dma_addr);
992 } else if (i == buf_len) {
994 td = (struct udc_data_dma *) phys_to_virt(
998 td = (struct udc_data_dma *) phys_to_virt(last->next);
1004 td->bufptr = req->req.dma + i; /* assign buffer */
1008 /* short packet ? */
1009 if ((bytes - i) >= buf_len) {
1013 txbytes = bytes - i;
1016 /* link td and assign tx bytes */
1018 if (create_new_chain) {
1019 req->td_data->next = dma_addr;
1021 /* req->td_data->next = virt_to_phys(td); */
1023 /* write tx bytes */
1026 req->td_data->status =
1027 AMD_ADDBITS(req->td_data->status,
1029 UDC_DMA_IN_STS_TXBYTES);
1031 td->status = AMD_ADDBITS(td->status,
1033 UDC_DMA_IN_STS_TXBYTES);
1036 if (create_new_chain) {
1037 last->next = dma_addr;
1039 /* last->next = virt_to_phys(td); */
1042 /* write tx bytes */
1043 td->status = AMD_ADDBITS(td->status,
1045 UDC_DMA_IN_STS_TXBYTES);
1052 td->status |= AMD_BIT(UDC_DMA_IN_STS_L);
1053 /* last desc. points to itself */
1054 req->td_data_last = td;
1060 /* Enabling RX DMA */
1061 static void udc_set_rde(struct udc *dev)
1065 VDBG(dev, "udc_set_rde()\n");
1066 /* stop RDE timer */
1067 if (timer_pending(&udc_timer)) {
1069 mod_timer(&udc_timer, jiffies - 1);
1072 tmp = readl(&dev->regs->ctl);
1073 tmp |= AMD_BIT(UDC_DEVCTL_RDE);
1074 writel(tmp, &dev->regs->ctl);
1077 /* Queues a request packet, called by gadget driver */
1079 udc_queue(struct usb_ep *usbep, struct usb_request *usbreq, gfp_t gfp)
1083 unsigned long iflags;
1085 struct udc_request *req;
1089 /* check the inputs */
1090 req = container_of(usbreq, struct udc_request, req);
1092 if (!usbep || !usbreq || !usbreq->complete || !usbreq->buf
1093 || !list_empty(&req->queue))
1096 ep = container_of(usbep, struct udc_ep, ep);
1097 if (!ep->desc && (ep->num != 0 && ep->num != UDC_EP0OUT_IX))
1100 VDBG(ep->dev, "udc_queue(): ep%d-in=%d\n", ep->num, ep->in);
1103 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1106 /* map dma (usually done before) */
1107 if (ep->dma && usbreq->length != 0
1108 && (usbreq->dma == DMA_DONT_USE || usbreq->dma == 0)) {
1109 VDBG(dev, "DMA map req %p\n", req);
1111 usbreq->dma = pci_map_single(dev->pdev,
1116 usbreq->dma = pci_map_single(dev->pdev,
1119 PCI_DMA_FROMDEVICE);
1120 req->dma_mapping = 1;
1123 VDBG(dev, "%s queue req %p, len %d req->td_data=%p buf %p\n",
1124 usbep->name, usbreq, usbreq->length,
1125 req->td_data, usbreq->buf);
1127 spin_lock_irqsave(&dev->lock, iflags);
1129 usbreq->status = -EINPROGRESS;
1132 /* on empty queue just do first transfer */
1133 if (list_empty(&ep->queue)) {
1135 if (usbreq->length == 0) {
1136 /* IN zlp's are handled by hardware */
1137 complete_req(ep, req, 0);
1138 VDBG(dev, "%s: zlp\n", ep->ep.name);
1140 * if set_config or set_intf is waiting for ack by zlp
1143 if (dev->set_cfg_not_acked) {
1144 tmp = readl(&dev->regs->ctl);
1145 tmp |= AMD_BIT(UDC_DEVCTL_CSR_DONE);
1146 writel(tmp, &dev->regs->ctl);
1147 dev->set_cfg_not_acked = 0;
1149 /* setup command is ACK'ed now by zlp */
1150 if (dev->waiting_zlp_ack_ep0in) {
1151 /* clear NAK by writing CNAK in EP0_IN */
1152 tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
1153 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
1154 writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
1155 dev->ep[UDC_EP0IN_IX].naking = 0;
1156 UDC_QUEUE_CNAK(&dev->ep[UDC_EP0IN_IX],
1158 dev->waiting_zlp_ack_ep0in = 0;
1163 retval = prep_dma(ep, req, gfp);
1166 /* write desc pointer to enable DMA */
1168 /* set HOST READY */
1169 req->td_data->status =
1170 AMD_ADDBITS(req->td_data->status,
1171 UDC_DMA_IN_STS_BS_HOST_READY,
1175 /* disabled rx dma while descriptor update */
1177 /* stop RDE timer */
1178 if (timer_pending(&udc_timer)) {
1180 mod_timer(&udc_timer, jiffies - 1);
1183 tmp = readl(&dev->regs->ctl);
1184 tmp &= AMD_UNMASK_BIT(UDC_DEVCTL_RDE);
1185 writel(tmp, &dev->regs->ctl);
1189 * if BNA occurred then let BNA dummy desc.
1190 * point to current desc.
1192 if (ep->bna_occurred) {
1193 VDBG(dev, "copy to BNA dummy desc.\n");
1194 memcpy(ep->bna_dummy_req->td_data,
1196 sizeof(struct udc_data_dma));
1199 /* write desc pointer */
1200 writel(req->td_phys, &ep->regs->desptr);
1202 /* clear NAK by writing CNAK */
1204 tmp = readl(&ep->regs->ctl);
1205 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
1206 writel(tmp, &ep->regs->ctl);
1208 UDC_QUEUE_CNAK(ep, ep->num);
1213 tmp = readl(&dev->regs->ep_irqmsk);
1214 tmp &= AMD_UNMASK_BIT(ep->num);
1215 writel(tmp, &dev->regs->ep_irqmsk);
1217 } else if (ep->in) {
1219 tmp = readl(&dev->regs->ep_irqmsk);
1220 tmp &= AMD_UNMASK_BIT(ep->num);
1221 writel(tmp, &dev->regs->ep_irqmsk);
1224 } else if (ep->dma) {
1227 * prep_dma not used for OUT ep's, this is not possible
1228 * for PPB modes, because of chain creation reasons
1231 retval = prep_dma(ep, req, gfp);
1236 VDBG(dev, "list_add\n");
1237 /* add request to ep queue */
1240 list_add_tail(&req->queue, &ep->queue);
1242 /* open rxfifo if out data queued */
1247 if (ep->num != UDC_EP0OUT_IX)
1248 dev->data_ep_queued = 1;
1250 /* stop OUT naking */
1252 if (!use_dma && udc_rxfifo_pending) {
1253 DBG(dev, "udc_queue(): pending bytes in "
1254 "rxfifo after nyet\n");
1256 * read pending bytes afer nyet:
1259 if (udc_rxfifo_read(ep, req)) {
1261 complete_req(ep, req, 0);
1263 udc_rxfifo_pending = 0;
1270 spin_unlock_irqrestore(&dev->lock, iflags);
1274 /* Empty request queue of an endpoint; caller holds spinlock */
1275 static void empty_req_queue(struct udc_ep *ep)
1277 struct udc_request *req;
1280 while (!list_empty(&ep->queue)) {
1281 req = list_entry(ep->queue.next,
1284 complete_req(ep, req, -ESHUTDOWN);
1288 /* Dequeues a request packet, called by gadget driver */
1289 static int udc_dequeue(struct usb_ep *usbep, struct usb_request *usbreq)
1292 struct udc_request *req;
1294 unsigned long iflags;
1296 ep = container_of(usbep, struct udc_ep, ep);
1297 if (!usbep || !usbreq || (!ep->desc && (ep->num != 0
1298 && ep->num != UDC_EP0OUT_IX)))
1301 req = container_of(usbreq, struct udc_request, req);
1303 spin_lock_irqsave(&ep->dev->lock, iflags);
1304 halted = ep->halted;
1306 /* request in processing or next one */
1307 if (ep->queue.next == &req->queue) {
1308 if (ep->dma && req->dma_going) {
1310 ep->cancel_transfer = 1;
1314 /* stop potential receive DMA */
1315 tmp = readl(&udc->regs->ctl);
1316 writel(tmp & AMD_UNMASK_BIT(UDC_DEVCTL_RDE),
1319 * Cancel transfer later in ISR
1320 * if descriptor was touched.
1322 dma_sts = AMD_GETBITS(req->td_data->status,
1323 UDC_DMA_OUT_STS_BS);
1324 if (dma_sts != UDC_DMA_OUT_STS_BS_HOST_READY)
1325 ep->cancel_transfer = 1;
1327 udc_init_bna_dummy(ep->req);
1328 writel(ep->bna_dummy_req->td_phys,
1331 writel(tmp, &udc->regs->ctl);
1335 complete_req(ep, req, -ECONNRESET);
1336 ep->halted = halted;
1338 spin_unlock_irqrestore(&ep->dev->lock, iflags);
1342 /* Halt or clear halt of endpoint */
1344 udc_set_halt(struct usb_ep *usbep, int halt)
1348 unsigned long iflags;
1354 pr_debug("set_halt %s: halt=%d\n", usbep->name, halt);
1356 ep = container_of(usbep, struct udc_ep, ep);
1357 if (!ep->desc && (ep->num != 0 && ep->num != UDC_EP0OUT_IX))
1359 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1362 spin_lock_irqsave(&udc_stall_spinlock, iflags);
1363 /* halt or clear halt */
1366 ep->dev->stall_ep0in = 1;
1370 * rxfifo empty not taken into acount
1372 tmp = readl(&ep->regs->ctl);
1373 tmp |= AMD_BIT(UDC_EPCTL_S);
1374 writel(tmp, &ep->regs->ctl);
1377 /* setup poll timer */
1378 if (!timer_pending(&udc_pollstall_timer)) {
1379 udc_pollstall_timer.expires = jiffies +
1380 HZ * UDC_POLLSTALL_TIMER_USECONDS
1382 if (!stop_pollstall_timer) {
1383 DBG(ep->dev, "start polltimer\n");
1384 add_timer(&udc_pollstall_timer);
1389 /* ep is halted by set_halt() before */
1391 tmp = readl(&ep->regs->ctl);
1392 /* clear stall bit */
1393 tmp = tmp & AMD_CLEAR_BIT(UDC_EPCTL_S);
1394 /* clear NAK by writing CNAK */
1395 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
1396 writel(tmp, &ep->regs->ctl);
1398 UDC_QUEUE_CNAK(ep, ep->num);
1401 spin_unlock_irqrestore(&udc_stall_spinlock, iflags);
1405 /* gadget interface */
1406 static const struct usb_ep_ops udc_ep_ops = {
1407 .enable = udc_ep_enable,
1408 .disable = udc_ep_disable,
1410 .alloc_request = udc_alloc_request,
1411 .free_request = udc_free_request,
1414 .dequeue = udc_dequeue,
1416 .set_halt = udc_set_halt,
1417 /* fifo ops not implemented */
1420 /*-------------------------------------------------------------------------*/
1422 /* Get frame counter (not implemented) */
1423 static int udc_get_frame(struct usb_gadget *gadget)
1428 /* Remote wakeup gadget interface */
1429 static int udc_wakeup(struct usb_gadget *gadget)
1435 dev = container_of(gadget, struct udc, gadget);
1436 udc_remote_wakeup(dev);
1441 /* gadget operations */
1442 static const struct usb_gadget_ops udc_ops = {
1443 .wakeup = udc_wakeup,
1444 .get_frame = udc_get_frame,
1447 /* Setups endpoint parameters, adds endpoints to linked list */
1448 static void make_ep_lists(struct udc *dev)
1450 /* make gadget ep lists */
1451 INIT_LIST_HEAD(&dev->gadget.ep_list);
1452 list_add_tail(&dev->ep[UDC_EPIN_STATUS_IX].ep.ep_list,
1453 &dev->gadget.ep_list);
1454 list_add_tail(&dev->ep[UDC_EPIN_IX].ep.ep_list,
1455 &dev->gadget.ep_list);
1456 list_add_tail(&dev->ep[UDC_EPOUT_IX].ep.ep_list,
1457 &dev->gadget.ep_list);
1460 dev->ep[UDC_EPIN_STATUS_IX].fifo_depth = UDC_EPIN_SMALLINT_BUFF_SIZE;
1461 if (dev->gadget.speed == USB_SPEED_FULL)
1462 dev->ep[UDC_EPIN_IX].fifo_depth = UDC_FS_EPIN_BUFF_SIZE;
1463 else if (dev->gadget.speed == USB_SPEED_HIGH)
1464 dev->ep[UDC_EPIN_IX].fifo_depth = hs_tx_buf;
1465 dev->ep[UDC_EPOUT_IX].fifo_depth = UDC_RXFIFO_SIZE;
1468 /* init registers at driver load time */
1469 static int startup_registers(struct udc *dev)
1473 /* init controller by soft reset */
1474 udc_soft_reset(dev);
1476 /* mask not needed interrupts */
1477 udc_mask_unused_interrupts(dev);
1479 /* put into initial config */
1480 udc_basic_init(dev);
1481 /* link up all endpoints */
1482 udc_setup_endpoints(dev);
1485 tmp = readl(&dev->regs->cfg);
1486 if (use_fullspeed) {
1487 tmp = AMD_ADDBITS(tmp, UDC_DEVCFG_SPD_FS, UDC_DEVCFG_SPD);
1489 tmp = AMD_ADDBITS(tmp, UDC_DEVCFG_SPD_HS, UDC_DEVCFG_SPD);
1491 writel(tmp, &dev->regs->cfg);
1496 /* Inits UDC context */
1497 static void udc_basic_init(struct udc *dev)
1501 DBG(dev, "udc_basic_init()\n");
1503 dev->gadget.speed = USB_SPEED_UNKNOWN;
1505 /* stop RDE timer */
1506 if (timer_pending(&udc_timer)) {
1508 mod_timer(&udc_timer, jiffies - 1);
1510 /* stop poll stall timer */
1511 if (timer_pending(&udc_pollstall_timer)) {
1512 mod_timer(&udc_pollstall_timer, jiffies - 1);
1515 tmp = readl(&dev->regs->ctl);
1516 tmp &= AMD_UNMASK_BIT(UDC_DEVCTL_RDE);
1517 tmp &= AMD_UNMASK_BIT(UDC_DEVCTL_TDE);
1518 writel(tmp, &dev->regs->ctl);
1520 /* enable dynamic CSR programming */
1521 tmp = readl(&dev->regs->cfg);
1522 tmp |= AMD_BIT(UDC_DEVCFG_CSR_PRG);
1523 /* set self powered */
1524 tmp |= AMD_BIT(UDC_DEVCFG_SP);
1525 /* set remote wakeupable */
1526 tmp |= AMD_BIT(UDC_DEVCFG_RWKP);
1527 writel(tmp, &dev->regs->cfg);
1531 dev->data_ep_enabled = 0;
1532 dev->data_ep_queued = 0;
1535 /* Sets initial endpoint parameters */
1536 static void udc_setup_endpoints(struct udc *dev)
1542 DBG(dev, "udc_setup_endpoints()\n");
1544 /* read enum speed */
1545 tmp = readl(&dev->regs->sts);
1546 tmp = AMD_GETBITS(tmp, UDC_DEVSTS_ENUM_SPEED);
1547 if (tmp == UDC_DEVSTS_ENUM_SPEED_HIGH) {
1548 dev->gadget.speed = USB_SPEED_HIGH;
1549 } else if (tmp == UDC_DEVSTS_ENUM_SPEED_FULL) {
1550 dev->gadget.speed = USB_SPEED_FULL;
1553 /* set basic ep parameters */
1554 for (tmp = 0; tmp < UDC_EP_NUM; tmp++) {
1557 ep->ep.name = ep_string[tmp];
1559 /* txfifo size is calculated at enable time */
1560 ep->txfifo = dev->txfifo;
1563 if (tmp < UDC_EPIN_NUM) {
1564 ep->fifo_depth = UDC_TXFIFO_SIZE;
1567 ep->fifo_depth = UDC_RXFIFO_SIZE;
1571 ep->regs = &dev->ep_regs[tmp];
1573 * ep will be reset only if ep was not enabled before to avoid
1574 * disabling ep interrupts when ENUM interrupt occurs but ep is
1575 * not enabled by gadget driver
1578 ep_init(dev->regs, ep);
1583 * ep->dma is not really used, just to indicate that
1584 * DMA is active: remove this
1585 * dma regs = dev control regs
1587 ep->dma = &dev->regs->ctl;
1589 /* nak OUT endpoints until enable - not for ep0 */
1590 if (tmp != UDC_EP0IN_IX && tmp != UDC_EP0OUT_IX
1591 && tmp > UDC_EPIN_NUM) {
1593 reg = readl(&dev->ep[tmp].regs->ctl);
1594 reg |= AMD_BIT(UDC_EPCTL_SNAK);
1595 writel(reg, &dev->ep[tmp].regs->ctl);
1596 dev->ep[tmp].naking = 1;
1601 /* EP0 max packet */
1602 if (dev->gadget.speed == USB_SPEED_FULL) {
1603 dev->ep[UDC_EP0IN_IX].ep.maxpacket = UDC_FS_EP0IN_MAX_PKT_SIZE;
1604 dev->ep[UDC_EP0OUT_IX].ep.maxpacket =
1605 UDC_FS_EP0OUT_MAX_PKT_SIZE;
1606 } else if (dev->gadget.speed == USB_SPEED_HIGH) {
1607 dev->ep[UDC_EP0IN_IX].ep.maxpacket = UDC_EP0IN_MAX_PKT_SIZE;
1608 dev->ep[UDC_EP0OUT_IX].ep.maxpacket = UDC_EP0OUT_MAX_PKT_SIZE;
1612 * with suspend bug workaround, ep0 params for gadget driver
1613 * are set at gadget driver bind() call
1615 dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IX].ep;
1616 dev->ep[UDC_EP0IN_IX].halted = 0;
1617 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1619 /* init cfg/alt/int */
1620 dev->cur_config = 0;
1625 /* Bringup after Connect event, initial bringup to be ready for ep0 events */
1626 static void usb_connect(struct udc *dev)
1629 dev_info(&dev->pdev->dev, "USB Connect\n");
1633 /* put into initial config */
1634 udc_basic_init(dev);
1636 /* enable device setup interrupts */
1637 udc_enable_dev_setup_interrupts(dev);
1641 * Calls gadget with disconnect event and resets the UDC and makes
1642 * initial bringup to be ready for ep0 events
1644 static void usb_disconnect(struct udc *dev)
1647 dev_info(&dev->pdev->dev, "USB Disconnect\n");
1651 /* mask interrupts */
1652 udc_mask_unused_interrupts(dev);
1654 /* REVISIT there doesn't seem to be a point to having this
1655 * talk to a tasklet ... do it directly, we already hold
1656 * the spinlock needed to process the disconnect.
1659 tasklet_schedule(&disconnect_tasklet);
1662 /* Tasklet for disconnect to be outside of interrupt context */
1663 static void udc_tasklet_disconnect(unsigned long par)
1665 struct udc *dev = (struct udc *)(*((struct udc **) par));
1668 DBG(dev, "Tasklet disconnect\n");
1669 spin_lock_irq(&dev->lock);
1672 spin_unlock(&dev->lock);
1673 dev->driver->disconnect(&dev->gadget);
1674 spin_lock(&dev->lock);
1677 for (tmp = 0; tmp < UDC_EP_NUM; tmp++) {
1678 empty_req_queue(&dev->ep[tmp]);
1685 &dev->ep[UDC_EP0IN_IX]);
1688 if (!soft_reset_occured) {
1689 /* init controller by soft reset */
1690 udc_soft_reset(dev);
1691 soft_reset_occured++;
1694 /* re-enable dev interrupts */
1695 udc_enable_dev_setup_interrupts(dev);
1696 /* back to full speed ? */
1697 if (use_fullspeed) {
1698 tmp = readl(&dev->regs->cfg);
1699 tmp = AMD_ADDBITS(tmp, UDC_DEVCFG_SPD_FS, UDC_DEVCFG_SPD);
1700 writel(tmp, &dev->regs->cfg);
1703 spin_unlock_irq(&dev->lock);
1706 /* Reset the UDC core */
1707 static void udc_soft_reset(struct udc *dev)
1709 unsigned long flags;
1711 DBG(dev, "Soft reset\n");
1713 * reset possible waiting interrupts, because int.
1714 * status is lost after soft reset,
1715 * ep int. status reset
1717 writel(UDC_EPINT_MSK_DISABLE_ALL, &dev->regs->ep_irqsts);
1718 /* device int. status reset */
1719 writel(UDC_DEV_MSK_DISABLE, &dev->regs->irqsts);
1721 spin_lock_irqsave(&udc_irq_spinlock, flags);
1722 writel(AMD_BIT(UDC_DEVCFG_SOFTRESET), &dev->regs->cfg);
1723 readl(&dev->regs->cfg);
1724 spin_unlock_irqrestore(&udc_irq_spinlock, flags);
1728 /* RDE timer callback to set RDE bit */
1729 static void udc_timer_function(unsigned long v)
1733 spin_lock_irq(&udc_irq_spinlock);
1737 * open the fifo if fifo was filled on last timer call
1741 /* set RDE to receive setup data */
1742 tmp = readl(&udc->regs->ctl);
1743 tmp |= AMD_BIT(UDC_DEVCTL_RDE);
1744 writel(tmp, &udc->regs->ctl);
1746 } else if (readl(&udc->regs->sts)
1747 & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY)) {
1749 * if fifo empty setup polling, do not just
1752 udc_timer.expires = jiffies + HZ/UDC_RDE_TIMER_DIV;
1754 add_timer(&udc_timer);
1758 * fifo contains data now, setup timer for opening
1759 * the fifo when timer expires to be able to receive
1760 * setup packets, when data packets gets queued by
1761 * gadget layer then timer will forced to expire with
1762 * set_rde=0 (RDE is set in udc_queue())
1765 /* debug: lhadmot_timer_start = 221070 */
1766 udc_timer.expires = jiffies + HZ*UDC_RDE_TIMER_SECONDS;
1768 add_timer(&udc_timer);
1773 set_rde = -1; /* RDE was set by udc_queue() */
1774 spin_unlock_irq(&udc_irq_spinlock);
1780 /* Handle halt state, used in stall poll timer */
1781 static void udc_handle_halt_state(struct udc_ep *ep)
1784 /* set stall as long not halted */
1785 if (ep->halted == 1) {
1786 tmp = readl(&ep->regs->ctl);
1787 /* STALL cleared ? */
1788 if (!(tmp & AMD_BIT(UDC_EPCTL_S))) {
1790 * FIXME: MSC spec requires that stall remains
1791 * even on receivng of CLEAR_FEATURE HALT. So
1792 * we would set STALL again here to be compliant.
1793 * But with current mass storage drivers this does
1794 * not work (would produce endless host retries).
1795 * So we clear halt on CLEAR_FEATURE.
1797 DBG(ep->dev, "ep %d: set STALL again\n", ep->num);
1798 tmp |= AMD_BIT(UDC_EPCTL_S);
1799 writel(tmp, &ep->regs->ctl);*/
1801 /* clear NAK by writing CNAK */
1802 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
1803 writel(tmp, &ep->regs->ctl);
1805 UDC_QUEUE_CNAK(ep, ep->num);
1810 /* Stall timer callback to poll S bit and set it again after */
1811 static void udc_pollstall_timer_function(unsigned long v)
1816 spin_lock_irq(&udc_stall_spinlock);
1818 * only one IN and OUT endpoints are handled
1821 ep = &udc->ep[UDC_EPIN_IX];
1822 udc_handle_halt_state(ep);
1825 /* OUT poll stall */
1826 ep = &udc->ep[UDC_EPOUT_IX];
1827 udc_handle_halt_state(ep);
1831 /* setup timer again when still halted */
1832 if (!stop_pollstall_timer && halted) {
1833 udc_pollstall_timer.expires = jiffies +
1834 HZ * UDC_POLLSTALL_TIMER_USECONDS
1836 add_timer(&udc_pollstall_timer);
1838 spin_unlock_irq(&udc_stall_spinlock);
1840 if (stop_pollstall_timer)
1841 complete(&on_pollstall_exit);
1844 /* Inits endpoint 0 so that SETUP packets are processed */
1845 static void activate_control_endpoints(struct udc *dev)
1849 DBG(dev, "activate_control_endpoints\n");
1852 tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
1853 tmp |= AMD_BIT(UDC_EPCTL_F);
1854 writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
1856 /* set ep0 directions */
1857 dev->ep[UDC_EP0IN_IX].in = 1;
1858 dev->ep[UDC_EP0OUT_IX].in = 0;
1860 /* set buffer size (tx fifo entries) of EP0_IN */
1861 tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->bufin_framenum);
1862 if (dev->gadget.speed == USB_SPEED_FULL)
1863 tmp = AMD_ADDBITS(tmp, UDC_FS_EPIN0_BUFF_SIZE,
1864 UDC_EPIN_BUFF_SIZE);
1865 else if (dev->gadget.speed == USB_SPEED_HIGH)
1866 tmp = AMD_ADDBITS(tmp, UDC_EPIN0_BUFF_SIZE,
1867 UDC_EPIN_BUFF_SIZE);
1868 writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->bufin_framenum);
1870 /* set max packet size of EP0_IN */
1871 tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->bufout_maxpkt);
1872 if (dev->gadget.speed == USB_SPEED_FULL)
1873 tmp = AMD_ADDBITS(tmp, UDC_FS_EP0IN_MAX_PKT_SIZE,
1874 UDC_EP_MAX_PKT_SIZE);
1875 else if (dev->gadget.speed == USB_SPEED_HIGH)
1876 tmp = AMD_ADDBITS(tmp, UDC_EP0IN_MAX_PKT_SIZE,
1877 UDC_EP_MAX_PKT_SIZE);
1878 writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->bufout_maxpkt);
1880 /* set max packet size of EP0_OUT */
1881 tmp = readl(&dev->ep[UDC_EP0OUT_IX].regs->bufout_maxpkt);
1882 if (dev->gadget.speed == USB_SPEED_FULL)
1883 tmp = AMD_ADDBITS(tmp, UDC_FS_EP0OUT_MAX_PKT_SIZE,
1884 UDC_EP_MAX_PKT_SIZE);
1885 else if (dev->gadget.speed == USB_SPEED_HIGH)
1886 tmp = AMD_ADDBITS(tmp, UDC_EP0OUT_MAX_PKT_SIZE,
1887 UDC_EP_MAX_PKT_SIZE);
1888 writel(tmp, &dev->ep[UDC_EP0OUT_IX].regs->bufout_maxpkt);
1890 /* set max packet size of EP0 in UDC CSR */
1891 tmp = readl(&dev->csr->ne[0]);
1892 if (dev->gadget.speed == USB_SPEED_FULL)
1893 tmp = AMD_ADDBITS(tmp, UDC_FS_EP0OUT_MAX_PKT_SIZE,
1894 UDC_CSR_NE_MAX_PKT);
1895 else if (dev->gadget.speed == USB_SPEED_HIGH)
1896 tmp = AMD_ADDBITS(tmp, UDC_EP0OUT_MAX_PKT_SIZE,
1897 UDC_CSR_NE_MAX_PKT);
1898 writel(tmp, &dev->csr->ne[0]);
1901 dev->ep[UDC_EP0OUT_IX].td->status |=
1902 AMD_BIT(UDC_DMA_OUT_STS_L);
1903 /* write dma desc address */
1904 writel(dev->ep[UDC_EP0OUT_IX].td_stp_dma,
1905 &dev->ep[UDC_EP0OUT_IX].regs->subptr);
1906 writel(dev->ep[UDC_EP0OUT_IX].td_phys,
1907 &dev->ep[UDC_EP0OUT_IX].regs->desptr);
1908 /* stop RDE timer */
1909 if (timer_pending(&udc_timer)) {
1911 mod_timer(&udc_timer, jiffies - 1);
1913 /* stop pollstall timer */
1914 if (timer_pending(&udc_pollstall_timer)) {
1915 mod_timer(&udc_pollstall_timer, jiffies - 1);
1918 tmp = readl(&dev->regs->ctl);
1919 tmp |= AMD_BIT(UDC_DEVCTL_MODE)
1920 | AMD_BIT(UDC_DEVCTL_RDE)
1921 | AMD_BIT(UDC_DEVCTL_TDE);
1922 if (use_dma_bufferfill_mode) {
1923 tmp |= AMD_BIT(UDC_DEVCTL_BF);
1924 } else if (use_dma_ppb_du) {
1925 tmp |= AMD_BIT(UDC_DEVCTL_DU);
1927 writel(tmp, &dev->regs->ctl);
1930 /* clear NAK by writing CNAK for EP0IN */
1931 tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
1932 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
1933 writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
1934 dev->ep[UDC_EP0IN_IX].naking = 0;
1935 UDC_QUEUE_CNAK(&dev->ep[UDC_EP0IN_IX], UDC_EP0IN_IX);
1937 /* clear NAK by writing CNAK for EP0OUT */
1938 tmp = readl(&dev->ep[UDC_EP0OUT_IX].regs->ctl);
1939 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
1940 writel(tmp, &dev->ep[UDC_EP0OUT_IX].regs->ctl);
1941 dev->ep[UDC_EP0OUT_IX].naking = 0;
1942 UDC_QUEUE_CNAK(&dev->ep[UDC_EP0OUT_IX], UDC_EP0OUT_IX);
1945 /* Make endpoint 0 ready for control traffic */
1946 static int setup_ep0(struct udc *dev)
1948 activate_control_endpoints(dev);
1949 /* enable ep0 interrupts */
1950 udc_enable_ep0_interrupts(dev);
1951 /* enable device setup interrupts */
1952 udc_enable_dev_setup_interrupts(dev);
1957 /* Called by gadget driver to register itself */
1958 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1959 int (*bind)(struct usb_gadget *))
1961 struct udc *dev = udc;
1965 if (!driver || !bind || !driver->setup
1966 || driver->speed != USB_SPEED_HIGH)
1973 driver->driver.bus = NULL;
1974 dev->driver = driver;
1975 dev->gadget.dev.driver = &driver->driver;
1977 retval = bind(&dev->gadget);
1979 /* Some gadget drivers use both ep0 directions.
1980 * NOTE: to gadget driver, ep0 is just one endpoint...
1982 dev->ep[UDC_EP0OUT_IX].ep.driver_data =
1983 dev->ep[UDC_EP0IN_IX].ep.driver_data;
1986 DBG(dev, "binding to %s returning %d\n",
1987 driver->driver.name, retval);
1989 dev->gadget.dev.driver = NULL;
1993 /* get ready for ep0 traffic */
1997 tmp = readl(&dev->regs->ctl);
1998 tmp = tmp & AMD_CLEAR_BIT(UDC_DEVCTL_SD);
1999 writel(tmp, &dev->regs->ctl);
2005 EXPORT_SYMBOL(usb_gadget_probe_driver);
2007 /* shutdown requests and disconnect from gadget */
2009 shutdown(struct udc *dev, struct usb_gadget_driver *driver)
2010 __releases(dev->lock)
2011 __acquires(dev->lock)
2015 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
2016 spin_unlock(&dev->lock);
2017 driver->disconnect(&dev->gadget);
2018 spin_lock(&dev->lock);
2021 /* empty queues and init hardware */
2022 udc_basic_init(dev);
2023 for (tmp = 0; tmp < UDC_EP_NUM; tmp++)
2024 empty_req_queue(&dev->ep[tmp]);
2026 udc_setup_endpoints(dev);
2029 /* Called by gadget driver to unregister itself */
2030 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2032 struct udc *dev = udc;
2033 unsigned long flags;
2038 if (!driver || driver != dev->driver || !driver->unbind)
2041 spin_lock_irqsave(&dev->lock, flags);
2042 udc_mask_unused_interrupts(dev);
2043 shutdown(dev, driver);
2044 spin_unlock_irqrestore(&dev->lock, flags);
2046 driver->unbind(&dev->gadget);
2047 dev->gadget.dev.driver = NULL;
2051 tmp = readl(&dev->regs->ctl);
2052 tmp |= AMD_BIT(UDC_DEVCTL_SD);
2053 writel(tmp, &dev->regs->ctl);
2056 DBG(dev, "%s: unregistered\n", driver->driver.name);
2060 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2063 /* Clear pending NAK bits */
2064 static void udc_process_cnak_queue(struct udc *dev)
2070 DBG(dev, "CNAK pending queue processing\n");
2071 for (tmp = 0; tmp < UDC_EPIN_NUM_USED; tmp++) {
2072 if (cnak_pending & (1 << tmp)) {
2073 DBG(dev, "CNAK pending for ep%d\n", tmp);
2074 /* clear NAK by writing CNAK */
2075 reg = readl(&dev->ep[tmp].regs->ctl);
2076 reg |= AMD_BIT(UDC_EPCTL_CNAK);
2077 writel(reg, &dev->ep[tmp].regs->ctl);
2078 dev->ep[tmp].naking = 0;
2079 UDC_QUEUE_CNAK(&dev->ep[tmp], dev->ep[tmp].num);
2082 /* ... and ep0out */
2083 if (cnak_pending & (1 << UDC_EP0OUT_IX)) {
2084 DBG(dev, "CNAK pending for ep%d\n", UDC_EP0OUT_IX);
2085 /* clear NAK by writing CNAK */
2086 reg = readl(&dev->ep[UDC_EP0OUT_IX].regs->ctl);
2087 reg |= AMD_BIT(UDC_EPCTL_CNAK);
2088 writel(reg, &dev->ep[UDC_EP0OUT_IX].regs->ctl);
2089 dev->ep[UDC_EP0OUT_IX].naking = 0;
2090 UDC_QUEUE_CNAK(&dev->ep[UDC_EP0OUT_IX],
2091 dev->ep[UDC_EP0OUT_IX].num);
2095 /* Enabling RX DMA after setup packet */
2096 static void udc_ep0_set_rde(struct udc *dev)
2100 * only enable RXDMA when no data endpoint enabled
2103 if (!dev->data_ep_enabled || dev->data_ep_queued) {
2107 * setup timer for enabling RDE (to not enable
2108 * RXFIFO DMA for data endpoints to early)
2110 if (set_rde != 0 && !timer_pending(&udc_timer)) {
2112 jiffies + HZ/UDC_RDE_TIMER_DIV;
2115 add_timer(&udc_timer);
2123 /* Interrupt handler for data OUT traffic */
2124 static irqreturn_t udc_data_out_isr(struct udc *dev, int ep_ix)
2126 irqreturn_t ret_val = IRQ_NONE;
2129 struct udc_request *req;
2131 struct udc_data_dma *td = NULL;
2134 VDBG(dev, "ep%d irq\n", ep_ix);
2135 ep = &dev->ep[ep_ix];
2137 tmp = readl(&ep->regs->sts);
2140 if (tmp & AMD_BIT(UDC_EPSTS_BNA)) {
2141 DBG(dev, "BNA ep%dout occurred - DESPTR = %x \n",
2142 ep->num, readl(&ep->regs->desptr));
2144 writel(tmp | AMD_BIT(UDC_EPSTS_BNA), &ep->regs->sts);
2145 if (!ep->cancel_transfer)
2146 ep->bna_occurred = 1;
2148 ep->cancel_transfer = 0;
2149 ret_val = IRQ_HANDLED;
2154 if (tmp & AMD_BIT(UDC_EPSTS_HE)) {
2155 dev_err(&dev->pdev->dev, "HE ep%dout occurred\n", ep->num);
2158 writel(tmp | AMD_BIT(UDC_EPSTS_HE), &ep->regs->sts);
2159 ret_val = IRQ_HANDLED;
2163 if (!list_empty(&ep->queue)) {
2166 req = list_entry(ep->queue.next,
2167 struct udc_request, queue);
2170 udc_rxfifo_pending = 1;
2172 VDBG(dev, "req = %p\n", req);
2177 if (req && udc_rxfifo_read(ep, req)) {
2178 ret_val = IRQ_HANDLED;
2181 complete_req(ep, req, 0);
2183 if (!list_empty(&ep->queue) && !ep->halted) {
2184 req = list_entry(ep->queue.next,
2185 struct udc_request, queue);
2191 } else if (!ep->cancel_transfer && req != NULL) {
2192 ret_val = IRQ_HANDLED;
2194 /* check for DMA done */
2196 dma_done = AMD_GETBITS(req->td_data->status,
2197 UDC_DMA_OUT_STS_BS);
2198 /* packet per buffer mode - rx bytes */
2201 * if BNA occurred then recover desc. from
2204 if (ep->bna_occurred) {
2205 VDBG(dev, "Recover desc. from BNA dummy\n");
2206 memcpy(req->td_data, ep->bna_dummy_req->td_data,
2207 sizeof(struct udc_data_dma));
2208 ep->bna_occurred = 0;
2209 udc_init_bna_dummy(ep->req);
2211 td = udc_get_last_dma_desc(req);
2212 dma_done = AMD_GETBITS(td->status, UDC_DMA_OUT_STS_BS);
2214 if (dma_done == UDC_DMA_OUT_STS_BS_DMA_DONE) {
2215 /* buffer fill mode - rx bytes */
2217 /* received number bytes */
2218 count = AMD_GETBITS(req->td_data->status,
2219 UDC_DMA_OUT_STS_RXBYTES);
2220 VDBG(dev, "rx bytes=%u\n", count);
2221 /* packet per buffer mode - rx bytes */
2223 VDBG(dev, "req->td_data=%p\n", req->td_data);
2224 VDBG(dev, "last desc = %p\n", td);
2225 /* received number bytes */
2226 if (use_dma_ppb_du) {
2227 /* every desc. counts bytes */
2228 count = udc_get_ppbdu_rxbytes(req);
2230 /* last desc. counts bytes */
2231 count = AMD_GETBITS(td->status,
2232 UDC_DMA_OUT_STS_RXBYTES);
2233 if (!count && req->req.length
2234 == UDC_DMA_MAXPACKET) {
2236 * on 64k packets the RXBYTES
2239 count = UDC_DMA_MAXPACKET;
2242 VDBG(dev, "last desc rx bytes=%u\n", count);
2245 tmp = req->req.length - req->req.actual;
2247 if ((tmp % ep->ep.maxpacket) != 0) {
2248 DBG(dev, "%s: rx %db, space=%db\n",
2249 ep->ep.name, count, tmp);
2250 req->req.status = -EOVERFLOW;
2254 req->req.actual += count;
2256 /* complete request */
2257 complete_req(ep, req, 0);
2260 if (!list_empty(&ep->queue) && !ep->halted) {
2261 req = list_entry(ep->queue.next,
2265 * DMA may be already started by udc_queue()
2266 * called by gadget drivers completion
2267 * routine. This happens when queue
2268 * holds one request only.
2270 if (req->dma_going == 0) {
2272 if (prep_dma(ep, req, GFP_ATOMIC) != 0)
2274 /* write desc pointer */
2275 writel(req->td_phys,
2283 * implant BNA dummy descriptor to allow
2284 * RXFIFO opening by RDE
2286 if (ep->bna_dummy_req) {
2287 /* write desc pointer */
2288 writel(ep->bna_dummy_req->td_phys,
2290 ep->bna_occurred = 0;
2294 * schedule timer for setting RDE if queue
2295 * remains empty to allow ep0 packets pass
2299 && !timer_pending(&udc_timer)) {
2302 + HZ*UDC_RDE_TIMER_SECONDS;
2305 add_timer(&udc_timer);
2308 if (ep->num != UDC_EP0OUT_IX)
2309 dev->data_ep_queued = 0;
2314 * RX DMA must be reenabled for each desc in PPBDU mode
2315 * and must be enabled for PPBNDU mode in case of BNA
2320 } else if (ep->cancel_transfer) {
2321 ret_val = IRQ_HANDLED;
2322 ep->cancel_transfer = 0;
2325 /* check pending CNAKS */
2327 /* CNAk processing when rxfifo empty only */
2328 if (readl(&dev->regs->sts) & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY)) {
2329 udc_process_cnak_queue(dev);
2333 /* clear OUT bits in ep status */
2334 writel(UDC_EPSTS_OUT_CLEAR, &ep->regs->sts);
2339 /* Interrupt handler for data IN traffic */
2340 static irqreturn_t udc_data_in_isr(struct udc *dev, int ep_ix)
2342 irqreturn_t ret_val = IRQ_NONE;
2346 struct udc_request *req;
2347 struct udc_data_dma *td;
2351 ep = &dev->ep[ep_ix];
2353 epsts = readl(&ep->regs->sts);
2356 if (epsts & AMD_BIT(UDC_EPSTS_BNA)) {
2357 dev_err(&dev->pdev->dev,
2358 "BNA ep%din occurred - DESPTR = %08lx \n",
2360 (unsigned long) readl(&ep->regs->desptr));
2363 writel(epsts, &ep->regs->sts);
2364 ret_val = IRQ_HANDLED;
2369 if (epsts & AMD_BIT(UDC_EPSTS_HE)) {
2370 dev_err(&dev->pdev->dev,
2371 "HE ep%dn occurred - DESPTR = %08lx \n",
2372 ep->num, (unsigned long) readl(&ep->regs->desptr));
2375 writel(epsts | AMD_BIT(UDC_EPSTS_HE), &ep->regs->sts);
2376 ret_val = IRQ_HANDLED;
2380 /* DMA completion */
2381 if (epsts & AMD_BIT(UDC_EPSTS_TDC)) {
2382 VDBG(dev, "TDC set- completion\n");
2383 ret_val = IRQ_HANDLED;
2384 if (!ep->cancel_transfer && !list_empty(&ep->queue)) {
2385 req = list_entry(ep->queue.next,
2386 struct udc_request, queue);
2388 * length bytes transferred
2389 * check dma done of last desc. in PPBDU mode
2391 if (use_dma_ppb_du) {
2392 td = udc_get_last_dma_desc(req);
2395 AMD_GETBITS(td->status,
2397 /* don't care DMA done */
2398 req->req.actual = req->req.length;
2401 /* assume all bytes transferred */
2402 req->req.actual = req->req.length;
2405 if (req->req.actual == req->req.length) {
2407 complete_req(ep, req, 0);
2409 /* further request available ? */
2410 if (list_empty(&ep->queue)) {
2411 /* disable interrupt */
2412 tmp = readl(&dev->regs->ep_irqmsk);
2413 tmp |= AMD_BIT(ep->num);
2414 writel(tmp, &dev->regs->ep_irqmsk);
2418 ep->cancel_transfer = 0;
2422 * status reg has IN bit set and TDC not set (if TDC was handled,
2423 * IN must not be handled (UDC defect) ?
2425 if ((epsts & AMD_BIT(UDC_EPSTS_IN))
2426 && !(epsts & AMD_BIT(UDC_EPSTS_TDC))) {
2427 ret_val = IRQ_HANDLED;
2428 if (!list_empty(&ep->queue)) {
2430 req = list_entry(ep->queue.next,
2431 struct udc_request, queue);
2435 udc_txfifo_write(ep, &req->req);
2436 len = req->req.length - req->req.actual;
2437 if (len > ep->ep.maxpacket)
2438 len = ep->ep.maxpacket;
2439 req->req.actual += len;
2440 if (req->req.actual == req->req.length
2441 || (len != ep->ep.maxpacket)) {
2443 complete_req(ep, req, 0);
2446 } else if (req && !req->dma_going) {
2447 VDBG(dev, "IN DMA : req=%p req->td_data=%p\n",
2454 * unset L bit of first desc.
2457 if (use_dma_ppb && req->req.length >
2459 req->td_data->status &=
2464 /* write desc pointer */
2465 writel(req->td_phys, &ep->regs->desptr);
2467 /* set HOST READY */
2468 req->td_data->status =
2470 req->td_data->status,
2471 UDC_DMA_IN_STS_BS_HOST_READY,
2474 /* set poll demand bit */
2475 tmp = readl(&ep->regs->ctl);
2476 tmp |= AMD_BIT(UDC_EPCTL_P);
2477 writel(tmp, &ep->regs->ctl);
2481 } else if (!use_dma && ep->in) {
2482 /* disable interrupt */
2484 &dev->regs->ep_irqmsk);
2485 tmp |= AMD_BIT(ep->num);
2487 &dev->regs->ep_irqmsk);
2490 /* clear status bits */
2491 writel(epsts, &ep->regs->sts);
2498 /* Interrupt handler for Control OUT traffic */
2499 static irqreturn_t udc_control_out_isr(struct udc *dev)
2500 __releases(dev->lock)
2501 __acquires(dev->lock)
2503 irqreturn_t ret_val = IRQ_NONE;
2505 int setup_supported;
2509 struct udc_ep *ep_tmp;
2511 ep = &dev->ep[UDC_EP0OUT_IX];
2514 writel(AMD_BIT(UDC_EPINT_OUT_EP0), &dev->regs->ep_irqsts);
2516 tmp = readl(&dev->ep[UDC_EP0OUT_IX].regs->sts);
2517 /* check BNA and clear if set */
2518 if (tmp & AMD_BIT(UDC_EPSTS_BNA)) {
2519 VDBG(dev, "ep0: BNA set\n");
2520 writel(AMD_BIT(UDC_EPSTS_BNA),
2521 &dev->ep[UDC_EP0OUT_IX].regs->sts);
2522 ep->bna_occurred = 1;
2523 ret_val = IRQ_HANDLED;
2527 /* type of data: SETUP or DATA 0 bytes */
2528 tmp = AMD_GETBITS(tmp, UDC_EPSTS_OUT);
2529 VDBG(dev, "data_typ = %x\n", tmp);
2532 if (tmp == UDC_EPSTS_OUT_SETUP) {
2533 ret_val = IRQ_HANDLED;
2535 ep->dev->stall_ep0in = 0;
2536 dev->waiting_zlp_ack_ep0in = 0;
2538 /* set NAK for EP0_IN */
2539 tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
2540 tmp |= AMD_BIT(UDC_EPCTL_SNAK);
2541 writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
2542 dev->ep[UDC_EP0IN_IX].naking = 1;
2543 /* get setup data */
2546 /* clear OUT bits in ep status */
2547 writel(UDC_EPSTS_OUT_CLEAR,
2548 &dev->ep[UDC_EP0OUT_IX].regs->sts);
2550 setup_data.data[0] =
2551 dev->ep[UDC_EP0OUT_IX].td_stp->data12;
2552 setup_data.data[1] =
2553 dev->ep[UDC_EP0OUT_IX].td_stp->data34;
2554 /* set HOST READY */
2555 dev->ep[UDC_EP0OUT_IX].td_stp->status =
2556 UDC_DMA_STP_STS_BS_HOST_READY;
2559 udc_rxfifo_read_dwords(dev, setup_data.data, 2);
2562 /* determine direction of control data */
2563 if ((setup_data.request.bRequestType & USB_DIR_IN) != 0) {
2564 dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IX].ep;
2566 udc_ep0_set_rde(dev);
2569 dev->gadget.ep0 = &dev->ep[UDC_EP0OUT_IX].ep;
2571 * implant BNA dummy descriptor to allow RXFIFO opening
2574 if (ep->bna_dummy_req) {
2575 /* write desc pointer */
2576 writel(ep->bna_dummy_req->td_phys,
2577 &dev->ep[UDC_EP0OUT_IX].regs->desptr);
2578 ep->bna_occurred = 0;
2582 dev->ep[UDC_EP0OUT_IX].naking = 1;
2584 * setup timer for enabling RDE (to not enable
2585 * RXFIFO DMA for data to early)
2588 if (!timer_pending(&udc_timer)) {
2589 udc_timer.expires = jiffies +
2590 HZ/UDC_RDE_TIMER_DIV;
2592 add_timer(&udc_timer);
2598 * mass storage reset must be processed here because
2599 * next packet may be a CLEAR_FEATURE HALT which would not
2600 * clear the stall bit when no STALL handshake was received
2601 * before (autostall can cause this)
2603 if (setup_data.data[0] == UDC_MSCRES_DWORD0
2604 && setup_data.data[1] == UDC_MSCRES_DWORD1) {
2605 DBG(dev, "MSC Reset\n");
2608 * only one IN and OUT endpoints are handled
2610 ep_tmp = &udc->ep[UDC_EPIN_IX];
2611 udc_set_halt(&ep_tmp->ep, 0);
2612 ep_tmp = &udc->ep[UDC_EPOUT_IX];
2613 udc_set_halt(&ep_tmp->ep, 0);
2616 /* call gadget with setup data received */
2617 spin_unlock(&dev->lock);
2618 setup_supported = dev->driver->setup(&dev->gadget,
2619 &setup_data.request);
2620 spin_lock(&dev->lock);
2622 tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
2623 /* ep0 in returns data (not zlp) on IN phase */
2624 if (setup_supported >= 0 && setup_supported <
2625 UDC_EP0IN_MAXPACKET) {
2626 /* clear NAK by writing CNAK in EP0_IN */
2627 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
2628 writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
2629 dev->ep[UDC_EP0IN_IX].naking = 0;
2630 UDC_QUEUE_CNAK(&dev->ep[UDC_EP0IN_IX], UDC_EP0IN_IX);
2632 /* if unsupported request then stall */
2633 } else if (setup_supported < 0) {
2634 tmp |= AMD_BIT(UDC_EPCTL_S);
2635 writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
2637 dev->waiting_zlp_ack_ep0in = 1;
2640 /* clear NAK by writing CNAK in EP0_OUT */
2642 tmp = readl(&dev->ep[UDC_EP0OUT_IX].regs->ctl);
2643 tmp |= AMD_BIT(UDC_EPCTL_CNAK);
2644 writel(tmp, &dev->ep[UDC_EP0OUT_IX].regs->ctl);
2645 dev->ep[UDC_EP0OUT_IX].naking = 0;
2646 UDC_QUEUE_CNAK(&dev->ep[UDC_EP0OUT_IX], UDC_EP0OUT_IX);
2650 /* clear OUT bits in ep status */
2651 writel(UDC_EPSTS_OUT_CLEAR,
2652 &dev->ep[UDC_EP0OUT_IX].regs->sts);
2655 /* data packet 0 bytes */
2656 } else if (tmp == UDC_EPSTS_OUT_DATA) {
2657 /* clear OUT bits in ep status */
2658 writel(UDC_EPSTS_OUT_CLEAR, &dev->ep[UDC_EP0OUT_IX].regs->sts);
2660 /* get setup data: only 0 packet */
2662 /* no req if 0 packet, just reactivate */
2663 if (list_empty(&dev->ep[UDC_EP0OUT_IX].queue)) {
2666 /* set HOST READY */
2667 dev->ep[UDC_EP0OUT_IX].td->status =
2669 dev->ep[UDC_EP0OUT_IX].td->status,
2670 UDC_DMA_OUT_STS_BS_HOST_READY,
2671 UDC_DMA_OUT_STS_BS);
2673 udc_ep0_set_rde(dev);
2674 ret_val = IRQ_HANDLED;
2678 ret_val |= udc_data_out_isr(dev, UDC_EP0OUT_IX);
2679 /* re-program desc. pointer for possible ZLPs */
2680 writel(dev->ep[UDC_EP0OUT_IX].td_phys,
2681 &dev->ep[UDC_EP0OUT_IX].regs->desptr);
2683 udc_ep0_set_rde(dev);
2687 /* received number bytes */
2688 count = readl(&dev->ep[UDC_EP0OUT_IX].regs->sts);
2689 count = AMD_GETBITS(count, UDC_EPSTS_RX_PKT_SIZE);
2690 /* out data for fifo mode not working */
2693 /* 0 packet or real data ? */
2695 ret_val |= udc_data_out_isr(dev, UDC_EP0OUT_IX);
2697 /* dummy read confirm */
2698 readl(&dev->ep[UDC_EP0OUT_IX].regs->confirm);
2699 ret_val = IRQ_HANDLED;
2704 /* check pending CNAKS */
2706 /* CNAk processing when rxfifo empty only */
2707 if (readl(&dev->regs->sts) & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY)) {
2708 udc_process_cnak_queue(dev);
2716 /* Interrupt handler for Control IN traffic */
2717 static irqreturn_t udc_control_in_isr(struct udc *dev)
2719 irqreturn_t ret_val = IRQ_NONE;
2722 struct udc_request *req;
2725 ep = &dev->ep[UDC_EP0IN_IX];
2728 writel(AMD_BIT(UDC_EPINT_IN_EP0), &dev->regs->ep_irqsts);
2730 tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->sts);
2731 /* DMA completion */
2732 if (tmp & AMD_BIT(UDC_EPSTS_TDC)) {
2733 VDBG(dev, "isr: TDC clear \n");
2734 ret_val = IRQ_HANDLED;
2737 writel(AMD_BIT(UDC_EPSTS_TDC),
2738 &dev->ep[UDC_EP0IN_IX].regs->sts);
2740 /* status reg has IN bit set ? */
2741 } else if (tmp & AMD_BIT(UDC_EPSTS_IN)) {
2742 ret_val = IRQ_HANDLED;
2746 writel(AMD_BIT(UDC_EPSTS_IN),
2747 &dev->ep[UDC_EP0IN_IX].regs->sts);
2749 if (dev->stall_ep0in) {
2750 DBG(dev, "stall ep0in\n");
2752 tmp = readl(&ep->regs->ctl);
2753 tmp |= AMD_BIT(UDC_EPCTL_S);
2754 writel(tmp, &ep->regs->ctl);
2756 if (!list_empty(&ep->queue)) {
2758 req = list_entry(ep->queue.next,
2759 struct udc_request, queue);
2762 /* write desc pointer */
2763 writel(req->td_phys, &ep->regs->desptr);
2764 /* set HOST READY */
2765 req->td_data->status =
2767 req->td_data->status,
2768 UDC_DMA_STP_STS_BS_HOST_READY,
2769 UDC_DMA_STP_STS_BS);
2771 /* set poll demand bit */
2773 readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
2774 tmp |= AMD_BIT(UDC_EPCTL_P);
2776 &dev->ep[UDC_EP0IN_IX].regs->ctl);
2778 /* all bytes will be transferred */
2779 req->req.actual = req->req.length;
2782 complete_req(ep, req, 0);
2786 udc_txfifo_write(ep, &req->req);
2788 /* lengh bytes transferred */
2789 len = req->req.length - req->req.actual;
2790 if (len > ep->ep.maxpacket)
2791 len = ep->ep.maxpacket;
2793 req->req.actual += len;
2794 if (req->req.actual == req->req.length
2795 || (len != ep->ep.maxpacket)) {
2797 complete_req(ep, req, 0);
2804 dev->stall_ep0in = 0;
2807 writel(AMD_BIT(UDC_EPSTS_IN),
2808 &dev->ep[UDC_EP0IN_IX].regs->sts);
2816 /* Interrupt handler for global device events */
2817 static irqreturn_t udc_dev_isr(struct udc *dev, u32 dev_irq)
2818 __releases(dev->lock)
2819 __acquires(dev->lock)
2821 irqreturn_t ret_val = IRQ_NONE;
2828 /* SET_CONFIG irq ? */
2829 if (dev_irq & AMD_BIT(UDC_DEVINT_SC)) {
2830 ret_val = IRQ_HANDLED;
2832 /* read config value */
2833 tmp = readl(&dev->regs->sts);
2834 cfg = AMD_GETBITS(tmp, UDC_DEVSTS_CFG);
2835 DBG(dev, "SET_CONFIG interrupt: config=%d\n", cfg);
2836 dev->cur_config = cfg;
2837 dev->set_cfg_not_acked = 1;
2839 /* make usb request for gadget driver */
2840 memset(&setup_data, 0 , sizeof(union udc_setup_data));
2841 setup_data.request.bRequest = USB_REQ_SET_CONFIGURATION;
2842 setup_data.request.wValue = cpu_to_le16(dev->cur_config);
2844 /* programm the NE registers */
2845 for (i = 0; i < UDC_EP_NUM; i++) {
2849 /* ep ix in UDC CSR register space */
2850 udc_csr_epix = ep->num;
2855 /* ep ix in UDC CSR register space */
2856 udc_csr_epix = ep->num - UDC_CSR_EP_OUT_IX_OFS;
2859 tmp = readl(&dev->csr->ne[udc_csr_epix]);
2861 tmp = AMD_ADDBITS(tmp, ep->dev->cur_config,
2864 writel(tmp, &dev->csr->ne[udc_csr_epix]);
2866 /* clear stall bits */
2868 tmp = readl(&ep->regs->ctl);
2869 tmp = tmp & AMD_CLEAR_BIT(UDC_EPCTL_S);
2870 writel(tmp, &ep->regs->ctl);
2872 /* call gadget zero with setup data received */
2873 spin_unlock(&dev->lock);
2874 tmp = dev->driver->setup(&dev->gadget, &setup_data.request);
2875 spin_lock(&dev->lock);
2877 } /* SET_INTERFACE ? */
2878 if (dev_irq & AMD_BIT(UDC_DEVINT_SI)) {
2879 ret_val = IRQ_HANDLED;
2881 dev->set_cfg_not_acked = 1;
2882 /* read interface and alt setting values */
2883 tmp = readl(&dev->regs->sts);
2884 dev->cur_alt = AMD_GETBITS(tmp, UDC_DEVSTS_ALT);
2885 dev->cur_intf = AMD_GETBITS(tmp, UDC_DEVSTS_INTF);
2887 /* make usb request for gadget driver */
2888 memset(&setup_data, 0 , sizeof(union udc_setup_data));
2889 setup_data.request.bRequest = USB_REQ_SET_INTERFACE;
2890 setup_data.request.bRequestType = USB_RECIP_INTERFACE;
2891 setup_data.request.wValue = cpu_to_le16(dev->cur_alt);
2892 setup_data.request.wIndex = cpu_to_le16(dev->cur_intf);
2894 DBG(dev, "SET_INTERFACE interrupt: alt=%d intf=%d\n",
2895 dev->cur_alt, dev->cur_intf);
2897 /* programm the NE registers */
2898 for (i = 0; i < UDC_EP_NUM; i++) {
2902 /* ep ix in UDC CSR register space */
2903 udc_csr_epix = ep->num;
2908 /* ep ix in UDC CSR register space */
2909 udc_csr_epix = ep->num - UDC_CSR_EP_OUT_IX_OFS;
2914 tmp = readl(&dev->csr->ne[udc_csr_epix]);
2916 tmp = AMD_ADDBITS(tmp, ep->dev->cur_intf,
2918 /* tmp = AMD_ADDBITS(tmp, 2, UDC_CSR_NE_INTF); */
2920 tmp = AMD_ADDBITS(tmp, ep->dev->cur_alt,
2923 writel(tmp, &dev->csr->ne[udc_csr_epix]);
2925 /* clear stall bits */
2927 tmp = readl(&ep->regs->ctl);
2928 tmp = tmp & AMD_CLEAR_BIT(UDC_EPCTL_S);
2929 writel(tmp, &ep->regs->ctl);
2932 /* call gadget zero with setup data received */
2933 spin_unlock(&dev->lock);
2934 tmp = dev->driver->setup(&dev->gadget, &setup_data.request);
2935 spin_lock(&dev->lock);
2938 if (dev_irq & AMD_BIT(UDC_DEVINT_UR)) {
2939 DBG(dev, "USB Reset interrupt\n");
2940 ret_val = IRQ_HANDLED;
2942 /* allow soft reset when suspend occurs */
2943 soft_reset_occured = 0;
2945 dev->waiting_zlp_ack_ep0in = 0;
2946 dev->set_cfg_not_acked = 0;
2948 /* mask not needed interrupts */
2949 udc_mask_unused_interrupts(dev);
2951 /* call gadget to resume and reset configs etc. */
2952 spin_unlock(&dev->lock);
2953 if (dev->sys_suspended && dev->driver->resume) {
2954 dev->driver->resume(&dev->gadget);
2955 dev->sys_suspended = 0;
2957 dev->driver->disconnect(&dev->gadget);
2958 spin_lock(&dev->lock);
2960 /* disable ep0 to empty req queue */
2961 empty_req_queue(&dev->ep[UDC_EP0IN_IX]);
2962 ep_init(dev->regs, &dev->ep[UDC_EP0IN_IX]);
2964 /* soft reset when rxfifo not empty */
2965 tmp = readl(&dev->regs->sts);
2966 if (!(tmp & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY))
2967 && !soft_reset_after_usbreset_occured) {
2968 udc_soft_reset(dev);
2969 soft_reset_after_usbreset_occured++;
2973 * DMA reset to kill potential old DMA hw hang,
2974 * POLL bit is already reset by ep_init() through
2977 DBG(dev, "DMA machine reset\n");
2978 tmp = readl(&dev->regs->cfg);
2979 writel(tmp | AMD_BIT(UDC_DEVCFG_DMARST), &dev->regs->cfg);
2980 writel(tmp, &dev->regs->cfg);
2982 /* put into initial config */
2983 udc_basic_init(dev);
2985 /* enable device setup interrupts */
2986 udc_enable_dev_setup_interrupts(dev);
2988 /* enable suspend interrupt */
2989 tmp = readl(&dev->regs->irqmsk);
2990 tmp &= AMD_UNMASK_BIT(UDC_DEVINT_US);
2991 writel(tmp, &dev->regs->irqmsk);
2994 if (dev_irq & AMD_BIT(UDC_DEVINT_US)) {
2995 DBG(dev, "USB Suspend interrupt\n");
2996 ret_val = IRQ_HANDLED;
2997 if (dev->driver->suspend) {
2998 spin_unlock(&dev->lock);
2999 dev->sys_suspended = 1;
3000 dev->driver->suspend(&dev->gadget);
3001 spin_lock(&dev->lock);
3004 if (dev_irq & AMD_BIT(UDC_DEVINT_ENUM)) {
3005 DBG(dev, "ENUM interrupt\n");
3006 ret_val = IRQ_HANDLED;
3007 soft_reset_after_usbreset_occured = 0;
3009 /* disable ep0 to empty req queue */
3010 empty_req_queue(&dev->ep[UDC_EP0IN_IX]);
3011 ep_init(dev->regs, &dev->ep[UDC_EP0IN_IX]);
3013 /* link up all endpoints */
3014 udc_setup_endpoints(dev);
3015 if (dev->gadget.speed == USB_SPEED_HIGH) {
3016 dev_info(&dev->pdev->dev, "Connect: speed = %s\n",
3018 } else if (dev->gadget.speed == USB_SPEED_FULL) {
3019 dev_info(&dev->pdev->dev, "Connect: speed = %s\n",
3024 activate_control_endpoints(dev);
3026 /* enable ep0 interrupts */
3027 udc_enable_ep0_interrupts(dev);
3029 /* session valid change interrupt */
3030 if (dev_irq & AMD_BIT(UDC_DEVINT_SVC)) {
3031 DBG(dev, "USB SVC interrupt\n");
3032 ret_val = IRQ_HANDLED;
3034 /* check that session is not valid to detect disconnect */
3035 tmp = readl(&dev->regs->sts);
3036 if (!(tmp & AMD_BIT(UDC_DEVSTS_SESSVLD))) {
3037 /* disable suspend interrupt */
3038 tmp = readl(&dev->regs->irqmsk);
3039 tmp |= AMD_BIT(UDC_DEVINT_US);
3040 writel(tmp, &dev->regs->irqmsk);
3041 DBG(dev, "USB Disconnect (session valid low)\n");
3042 /* cleanup on disconnect */
3043 usb_disconnect(udc);
3051 /* Interrupt Service Routine, see Linux Kernel Doc for parameters */
3052 static irqreturn_t udc_irq(int irq, void *pdev)
3054 struct udc *dev = pdev;
3058 irqreturn_t ret_val = IRQ_NONE;
3060 spin_lock(&dev->lock);
3062 /* check for ep irq */
3063 reg = readl(&dev->regs->ep_irqsts);
3065 if (reg & AMD_BIT(UDC_EPINT_OUT_EP0))
3066 ret_val |= udc_control_out_isr(dev);
3067 if (reg & AMD_BIT(UDC_EPINT_IN_EP0))
3068 ret_val |= udc_control_in_isr(dev);
3074 for (i = 1; i < UDC_EP_NUM; i++) {
3076 if (!(reg & ep_irq) || i == UDC_EPINT_OUT_EP0)
3079 /* clear irq status */
3080 writel(ep_irq, &dev->regs->ep_irqsts);
3082 /* irq for out ep ? */
3083 if (i > UDC_EPIN_NUM)
3084 ret_val |= udc_data_out_isr(dev, i);
3086 ret_val |= udc_data_in_isr(dev, i);
3092 /* check for dev irq */
3093 reg = readl(&dev->regs->irqsts);
3096 writel(reg, &dev->regs->irqsts);
3097 ret_val |= udc_dev_isr(dev, reg);
3101 spin_unlock(&dev->lock);
3105 /* Tears down device */
3106 static void gadget_release(struct device *pdev)
3108 struct amd5536udc *dev = dev_get_drvdata(pdev);
3112 /* Cleanup on device remove */
3113 static void udc_remove(struct udc *dev)
3117 if (timer_pending(&udc_timer))
3118 wait_for_completion(&on_exit);
3120 del_timer_sync(&udc_timer);
3121 /* remove pollstall timer */
3122 stop_pollstall_timer++;
3123 if (timer_pending(&udc_pollstall_timer))
3124 wait_for_completion(&on_pollstall_exit);
3125 if (udc_pollstall_timer.data)
3126 del_timer_sync(&udc_pollstall_timer);
3130 /* Reset all pci context */
3131 static void udc_pci_remove(struct pci_dev *pdev)
3135 dev = pci_get_drvdata(pdev);
3137 /* gadget driver must not be registered */
3138 BUG_ON(dev->driver != NULL);
3140 /* dma pool cleanup */
3141 if (dev->data_requests)
3142 pci_pool_destroy(dev->data_requests);
3144 if (dev->stp_requests) {
3145 /* cleanup DMA desc's for ep0in */
3146 pci_pool_free(dev->stp_requests,
3147 dev->ep[UDC_EP0OUT_IX].td_stp,
3148 dev->ep[UDC_EP0OUT_IX].td_stp_dma);
3149 pci_pool_free(dev->stp_requests,
3150 dev->ep[UDC_EP0OUT_IX].td,
3151 dev->ep[UDC_EP0OUT_IX].td_phys);
3153 pci_pool_destroy(dev->stp_requests);
3156 /* reset controller */
3157 writel(AMD_BIT(UDC_DEVCFG_SOFTRESET), &dev->regs->cfg);
3158 if (dev->irq_registered)
3159 free_irq(pdev->irq, dev);
3162 if (dev->mem_region)
3163 release_mem_region(pci_resource_start(pdev, 0),
3164 pci_resource_len(pdev, 0));
3166 pci_disable_device(pdev);
3168 device_unregister(&dev->gadget.dev);
3169 pci_set_drvdata(pdev, NULL);
3174 /* create dma pools on init */
3175 static int init_dma_pools(struct udc *dev)
3177 struct udc_stp_dma *td_stp;
3178 struct udc_data_dma *td_data;
3181 /* consistent DMA mode setting ? */
3183 use_dma_bufferfill_mode = 0;
3186 use_dma_bufferfill_mode = 1;
3190 dev->data_requests = dma_pool_create("data_requests", NULL,
3191 sizeof(struct udc_data_dma), 0, 0);
3192 if (!dev->data_requests) {
3193 DBG(dev, "can't get request data pool\n");
3198 /* EP0 in dma regs = dev control regs */
3199 dev->ep[UDC_EP0IN_IX].dma = &dev->regs->ctl;
3201 /* dma desc for setup data */
3202 dev->stp_requests = dma_pool_create("setup requests", NULL,
3203 sizeof(struct udc_stp_dma), 0, 0);
3204 if (!dev->stp_requests) {
3205 DBG(dev, "can't get stp request pool\n");
3210 td_stp = dma_pool_alloc(dev->stp_requests, GFP_KERNEL,
3211 &dev->ep[UDC_EP0OUT_IX].td_stp_dma);
3212 if (td_stp == NULL) {
3216 dev->ep[UDC_EP0OUT_IX].td_stp = td_stp;
3218 /* data: 0 packets !? */
3219 td_data = dma_pool_alloc(dev->stp_requests, GFP_KERNEL,
3220 &dev->ep[UDC_EP0OUT_IX].td_phys);
3221 if (td_data == NULL) {
3225 dev->ep[UDC_EP0OUT_IX].td = td_data;
3232 /* Called by pci bus driver to init pci context */
3233 static int udc_pci_probe(
3234 struct pci_dev *pdev,
3235 const struct pci_device_id *id
3239 unsigned long resource;
3245 dev_dbg(&pdev->dev, "already probed\n");
3250 dev = kzalloc(sizeof(struct udc), GFP_KERNEL);
3257 if (pci_enable_device(pdev) < 0) {
3265 /* PCI resource allocation */
3266 resource = pci_resource_start(pdev, 0);
3267 len = pci_resource_len(pdev, 0);
3269 if (!request_mem_region(resource, len, name)) {
3270 dev_dbg(&pdev->dev, "pci device used already\n");
3276 dev->mem_region = 1;
3278 dev->virt_addr = ioremap_nocache(resource, len);
3279 if (dev->virt_addr == NULL) {
3280 dev_dbg(&pdev->dev, "start address cannot be mapped\n");
3288 dev_err(&dev->pdev->dev, "irq not set\n");
3295 spin_lock_init(&dev->lock);
3296 /* udc csr registers base */
3297 dev->csr = dev->virt_addr + UDC_CSR_ADDR;
3298 /* dev registers base */
3299 dev->regs = dev->virt_addr + UDC_DEVCFG_ADDR;
3300 /* ep registers base */
3301 dev->ep_regs = dev->virt_addr + UDC_EPREGS_ADDR;
3303 dev->rxfifo = (u32 __iomem *)(dev->virt_addr + UDC_RXFIFO_ADDR);
3304 dev->txfifo = (u32 __iomem *)(dev->virt_addr + UDC_TXFIFO_ADDR);
3306 if (request_irq(pdev->irq, udc_irq, IRQF_SHARED, name, dev) != 0) {
3307 dev_dbg(&dev->pdev->dev, "request_irq(%d) fail\n", pdev->irq);
3313 dev->irq_registered = 1;
3315 pci_set_drvdata(pdev, dev);
3317 /* chip revision for Hs AMD5536 */
3318 dev->chiprev = pdev->revision;
3320 pci_set_master(pdev);
3321 pci_try_set_mwi(pdev);
3323 /* init dma pools */
3325 retval = init_dma_pools(dev);
3330 dev->phys_addr = resource;
3331 dev->irq = pdev->irq;
3333 dev->gadget.dev.parent = &pdev->dev;
3334 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
3336 /* general probing */
3337 if (udc_probe(dev) == 0)
3342 udc_pci_remove(pdev);
3347 static int udc_probe(struct udc *dev)
3353 /* mark timer as not initialized */
3355 udc_pollstall_timer.data = 0;
3357 /* device struct setup */
3358 dev->gadget.ops = &udc_ops;
3360 dev_set_name(&dev->gadget.dev, "gadget");
3361 dev->gadget.dev.release = gadget_release;
3362 dev->gadget.name = name;
3363 dev->gadget.is_dualspeed = 1;
3365 /* init registers, interrupts, ... */
3366 startup_registers(dev);
3368 dev_info(&dev->pdev->dev, "%s\n", mod_desc);
3370 snprintf(tmp, sizeof tmp, "%d", dev->irq);
3371 dev_info(&dev->pdev->dev,
3372 "irq %s, pci mem %08lx, chip rev %02x(Geode5536 %s)\n",
3373 tmp, dev->phys_addr, dev->chiprev,
3374 (dev->chiprev == UDC_HSA0_REV) ? "A0" : "B1");
3375 strcpy(tmp, UDC_DRIVER_VERSION_STRING);
3376 if (dev->chiprev == UDC_HSA0_REV) {
3377 dev_err(&dev->pdev->dev, "chip revision is A0; too old\n");
3381 dev_info(&dev->pdev->dev,
3382 "driver version: %s(for Geode5536 B1)\n", tmp);
3385 retval = device_register(&dev->gadget.dev);
3387 put_device(&dev->gadget.dev);
3392 init_timer(&udc_timer);
3393 udc_timer.function = udc_timer_function;
3395 /* timer pollstall init */
3396 init_timer(&udc_pollstall_timer);
3397 udc_pollstall_timer.function = udc_pollstall_timer_function;
3398 udc_pollstall_timer.data = 1;
3401 reg = readl(&dev->regs->ctl);
3402 reg |= AMD_BIT(UDC_DEVCTL_SD);
3403 writel(reg, &dev->regs->ctl);
3405 /* print dev register info */
3414 /* Initiates a remote wakeup */
3415 static int udc_remote_wakeup(struct udc *dev)
3417 unsigned long flags;
3420 DBG(dev, "UDC initiates remote wakeup\n");
3422 spin_lock_irqsave(&dev->lock, flags);
3424 tmp = readl(&dev->regs->ctl);
3425 tmp |= AMD_BIT(UDC_DEVCTL_RES);
3426 writel(tmp, &dev->regs->ctl);
3427 tmp &= AMD_CLEAR_BIT(UDC_DEVCTL_RES);
3428 writel(tmp, &dev->regs->ctl);
3430 spin_unlock_irqrestore(&dev->lock, flags);
3434 /* PCI device parameters */
3435 static const struct pci_device_id pci_id[] = {
3437 PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x2096),
3438 .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
3439 .class_mask = 0xffffffff,
3443 MODULE_DEVICE_TABLE(pci, pci_id);
3446 static struct pci_driver udc_pci_driver = {
3447 .name = (char *) name,
3449 .probe = udc_pci_probe,
3450 .remove = udc_pci_remove,
3454 static int __init init(void)
3456 return pci_register_driver(&udc_pci_driver);
3461 static void __exit cleanup(void)
3463 pci_unregister_driver(&udc_pci_driver);
3465 module_exit(cleanup);
3467 MODULE_DESCRIPTION(UDC_MOD_DESCRIPTION);
3468 MODULE_AUTHOR("Thomas Dahlmann");
3469 MODULE_LICENSE("GPL");