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.
14 * The AMD5536 UDC is part of the x86 southbridge AMD Geode CS5536.
15 * It is a USB Highspeed DMA capable USB device controller. Beside ep0 it
16 * provides 4 IN and 4 OUT endpoints (bulk or interrupt type).
18 * Make sure that UDC is assigned to port 4 by BIOS settings (port can also
19 * be used as host port) and UOC bits PAD_EN and APU are set (should be done
22 * UDC DMA requires 32-bit aligned buffers so DMA with gadget ether does not
23 * work without updating NET_IP_ALIGN. Or PIO mode (module param "use_dma=0")
24 * can be used with gadget ether.
28 /* #define UDC_VERBOSE */
31 #define UDC_MOD_DESCRIPTION "AMD 5536 UDC - USB Device Controller"
32 #define UDC_DRIVER_VERSION_STRING "01.00.0206"
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/kernel.h>
38 #include <linux/delay.h>
39 #include <linux/ioport.h>
40 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/list.h>
45 #include <linux/interrupt.h>
46 #include <linux/ioctl.h>
48 #include <linux/dmapool.h>
49 #include <linux/moduleparam.h>
50 #include <linux/device.h>
52 #include <linux/irq.h>
53 #include <linux/prefetch.h>
55 #include <asm/byteorder.h>
56 #include <asm/unaligned.h>
59 #include <linux/usb/ch9.h>
60 #include <linux/usb/gadget.h>
63 #include "amd5536udc.h"
66 static void udc_tasklet_disconnect(unsigned long);
67 static void empty_req_queue(struct udc_ep
*);
68 static int udc_probe(struct udc
*dev
);
69 static void udc_basic_init(struct udc
*dev
);
70 static void udc_setup_endpoints(struct udc
*dev
);
71 static void udc_soft_reset(struct udc
*dev
);
72 static struct udc_request
*udc_alloc_bna_dummy(struct udc_ep
*ep
);
73 static void udc_free_request(struct usb_ep
*usbep
, struct usb_request
*usbreq
);
74 static int udc_free_dma_chain(struct udc
*dev
, struct udc_request
*req
);
75 static int udc_create_dma_chain(struct udc_ep
*ep
, struct udc_request
*req
,
76 unsigned long buf_len
, gfp_t gfp_flags
);
77 static int udc_remote_wakeup(struct udc
*dev
);
78 static int udc_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
);
79 static void udc_pci_remove(struct pci_dev
*pdev
);
82 static const char mod_desc
[] = UDC_MOD_DESCRIPTION
;
83 static const char name
[] = "amd5536udc";
85 /* structure to hold endpoint function pointers */
86 static const struct usb_ep_ops udc_ep_ops
;
88 /* received setup data */
89 static union udc_setup_data setup_data
;
91 /* pointer to device object */
92 static struct udc
*udc
;
94 /* irq spin lock for soft reset */
95 static DEFINE_SPINLOCK(udc_irq_spinlock
);
97 static DEFINE_SPINLOCK(udc_stall_spinlock
);
100 * slave mode: pending bytes in rx fifo after nyet,
101 * used if EPIN irq came but no req was available
103 static unsigned int udc_rxfifo_pending
;
105 /* count soft resets after suspend to avoid loop */
106 static int soft_reset_occured
;
107 static int soft_reset_after_usbreset_occured
;
110 static struct timer_list udc_timer
;
111 static int stop_timer
;
113 /* set_rde -- Is used to control enabling of RX DMA. Problem is
114 * that UDC has only one bit (RDE) to enable/disable RX DMA for
115 * all OUT endpoints. So we have to handle race conditions like
116 * when OUT data reaches the fifo but no request was queued yet.
117 * This cannot be solved by letting the RX DMA disabled until a
118 * request gets queued because there may be other OUT packets
119 * in the FIFO (important for not blocking control traffic).
120 * The value of set_rde controls the correspondig timer.
122 * set_rde -1 == not used, means it is alloed to be set to 0 or 1
123 * set_rde 0 == do not touch RDE, do no start the RDE timer
124 * set_rde 1 == timer function will look whether FIFO has data
125 * set_rde 2 == set by timer function to enable RX DMA on next call
127 static int set_rde
= -1;
129 static DECLARE_COMPLETION(on_exit
);
130 static struct timer_list udc_pollstall_timer
;
131 static int stop_pollstall_timer
;
132 static DECLARE_COMPLETION(on_pollstall_exit
);
134 /* tasklet for usb disconnect */
135 static DECLARE_TASKLET(disconnect_tasklet
, udc_tasklet_disconnect
,
136 (unsigned long) &udc
);
139 /* endpoint names used for print */
140 static const char ep0_string
[] = "ep0in";
141 static const char *const ep_string
[] = {
143 "ep1in-int", "ep2in-bulk", "ep3in-bulk", "ep4in-bulk", "ep5in-bulk",
144 "ep6in-bulk", "ep7in-bulk", "ep8in-bulk", "ep9in-bulk", "ep10in-bulk",
145 "ep11in-bulk", "ep12in-bulk", "ep13in-bulk", "ep14in-bulk",
146 "ep15in-bulk", "ep0out", "ep1out-bulk", "ep2out-bulk", "ep3out-bulk",
147 "ep4out-bulk", "ep5out-bulk", "ep6out-bulk", "ep7out-bulk",
148 "ep8out-bulk", "ep9out-bulk", "ep10out-bulk", "ep11out-bulk",
149 "ep12out-bulk", "ep13out-bulk", "ep14out-bulk", "ep15out-bulk"
153 static bool use_dma
= 1;
154 /* packet per buffer dma */
155 static bool use_dma_ppb
= 1;
156 /* with per descr. update */
157 static bool use_dma_ppb_du
;
158 /* buffer fill mode */
159 static int use_dma_bufferfill_mode
;
160 /* full speed only mode */
161 static bool use_fullspeed
;
162 /* tx buffer size for high speed */
163 static unsigned long hs_tx_buf
= UDC_EPIN_BUFF_SIZE
;
165 /* module parameters */
166 module_param(use_dma
, bool, S_IRUGO
);
167 MODULE_PARM_DESC(use_dma
, "true for DMA");
168 module_param(use_dma_ppb
, bool, S_IRUGO
);
169 MODULE_PARM_DESC(use_dma_ppb
, "true for DMA in packet per buffer mode");
170 module_param(use_dma_ppb_du
, bool, S_IRUGO
);
171 MODULE_PARM_DESC(use_dma_ppb_du
,
172 "true for DMA in packet per buffer mode with descriptor update");
173 module_param(use_fullspeed
, bool, S_IRUGO
);
174 MODULE_PARM_DESC(use_fullspeed
, "true for fullspeed only");
176 /*---------------------------------------------------------------------------*/
177 /* Prints UDC device registers and endpoint irq registers */
178 static void print_regs(struct udc
*dev
)
180 DBG(dev
, "------- Device registers -------\n");
181 DBG(dev
, "dev config = %08x\n", readl(&dev
->regs
->cfg
));
182 DBG(dev
, "dev control = %08x\n", readl(&dev
->regs
->ctl
));
183 DBG(dev
, "dev status = %08x\n", readl(&dev
->regs
->sts
));
185 DBG(dev
, "dev int's = %08x\n", readl(&dev
->regs
->irqsts
));
186 DBG(dev
, "dev intmask = %08x\n", readl(&dev
->regs
->irqmsk
));
188 DBG(dev
, "dev ep int's = %08x\n", readl(&dev
->regs
->ep_irqsts
));
189 DBG(dev
, "dev ep intmask = %08x\n", readl(&dev
->regs
->ep_irqmsk
));
191 DBG(dev
, "USE DMA = %d\n", use_dma
);
192 if (use_dma
&& use_dma_ppb
&& !use_dma_ppb_du
) {
193 DBG(dev
, "DMA mode = PPBNDU (packet per buffer "
194 "WITHOUT desc. update)\n");
195 dev_info(&dev
->pdev
->dev
, "DMA mode (%s)\n", "PPBNDU");
196 } else if (use_dma
&& use_dma_ppb
&& use_dma_ppb_du
) {
197 DBG(dev
, "DMA mode = PPBDU (packet per buffer "
198 "WITH desc. update)\n");
199 dev_info(&dev
->pdev
->dev
, "DMA mode (%s)\n", "PPBDU");
201 if (use_dma
&& use_dma_bufferfill_mode
) {
202 DBG(dev
, "DMA mode = BF (buffer fill mode)\n");
203 dev_info(&dev
->pdev
->dev
, "DMA mode (%s)\n", "BF");
206 dev_info(&dev
->pdev
->dev
, "FIFO mode\n");
207 DBG(dev
, "-------------------------------------------------------\n");
210 /* Masks unused interrupts */
211 static int udc_mask_unused_interrupts(struct udc
*dev
)
215 /* mask all dev interrupts */
216 tmp
= AMD_BIT(UDC_DEVINT_SVC
) |
217 AMD_BIT(UDC_DEVINT_ENUM
) |
218 AMD_BIT(UDC_DEVINT_US
) |
219 AMD_BIT(UDC_DEVINT_UR
) |
220 AMD_BIT(UDC_DEVINT_ES
) |
221 AMD_BIT(UDC_DEVINT_SI
) |
222 AMD_BIT(UDC_DEVINT_SOF
)|
223 AMD_BIT(UDC_DEVINT_SC
);
224 writel(tmp
, &dev
->regs
->irqmsk
);
226 /* mask all ep interrupts */
227 writel(UDC_EPINT_MSK_DISABLE_ALL
, &dev
->regs
->ep_irqmsk
);
232 /* Enables endpoint 0 interrupts */
233 static int udc_enable_ep0_interrupts(struct udc
*dev
)
237 DBG(dev
, "udc_enable_ep0_interrupts()\n");
240 tmp
= readl(&dev
->regs
->ep_irqmsk
);
241 /* enable ep0 irq's */
242 tmp
&= AMD_UNMASK_BIT(UDC_EPINT_IN_EP0
)
243 & AMD_UNMASK_BIT(UDC_EPINT_OUT_EP0
);
244 writel(tmp
, &dev
->regs
->ep_irqmsk
);
249 /* Enables device interrupts for SET_INTF and SET_CONFIG */
250 static int udc_enable_dev_setup_interrupts(struct udc
*dev
)
254 DBG(dev
, "enable device interrupts for setup data\n");
257 tmp
= readl(&dev
->regs
->irqmsk
);
259 /* enable SET_INTERFACE, SET_CONFIG and other needed irq's */
260 tmp
&= AMD_UNMASK_BIT(UDC_DEVINT_SI
)
261 & AMD_UNMASK_BIT(UDC_DEVINT_SC
)
262 & AMD_UNMASK_BIT(UDC_DEVINT_UR
)
263 & AMD_UNMASK_BIT(UDC_DEVINT_SVC
)
264 & AMD_UNMASK_BIT(UDC_DEVINT_ENUM
);
265 writel(tmp
, &dev
->regs
->irqmsk
);
270 /* Calculates fifo start of endpoint based on preceding endpoints */
271 static int udc_set_txfifo_addr(struct udc_ep
*ep
)
277 if (!ep
|| !(ep
->in
))
281 ep
->txfifo
= dev
->txfifo
;
284 for (i
= 0; i
< ep
->num
; i
++) {
285 if (dev
->ep
[i
].regs
) {
287 tmp
= readl(&dev
->ep
[i
].regs
->bufin_framenum
);
288 tmp
= AMD_GETBITS(tmp
, UDC_EPIN_BUFF_SIZE
);
295 /* CNAK pending field: bit0 = ep0in, bit16 = ep0out */
296 static u32 cnak_pending
;
298 static void UDC_QUEUE_CNAK(struct udc_ep
*ep
, unsigned num
)
300 if (readl(&ep
->regs
->ctl
) & AMD_BIT(UDC_EPCTL_NAK
)) {
301 DBG(ep
->dev
, "NAK could not be cleared for ep%d\n", num
);
302 cnak_pending
|= 1 << (num
);
305 cnak_pending
= cnak_pending
& (~(1 << (num
)));
309 /* Enables endpoint, is called by gadget driver */
311 udc_ep_enable(struct usb_ep
*usbep
, const struct usb_endpoint_descriptor
*desc
)
316 unsigned long iflags
;
321 || usbep
->name
== ep0_string
323 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
326 ep
= container_of(usbep
, struct udc_ep
, ep
);
329 DBG(dev
, "udc_ep_enable() ep %d\n", ep
->num
);
331 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
334 spin_lock_irqsave(&dev
->lock
, iflags
);
339 /* set traffic type */
340 tmp
= readl(&dev
->ep
[ep
->num
].regs
->ctl
);
341 tmp
= AMD_ADDBITS(tmp
, desc
->bmAttributes
, UDC_EPCTL_ET
);
342 writel(tmp
, &dev
->ep
[ep
->num
].regs
->ctl
);
344 /* set max packet size */
345 maxpacket
= usb_endpoint_maxp(desc
);
346 tmp
= readl(&dev
->ep
[ep
->num
].regs
->bufout_maxpkt
);
347 tmp
= AMD_ADDBITS(tmp
, maxpacket
, UDC_EP_MAX_PKT_SIZE
);
348 ep
->ep
.maxpacket
= maxpacket
;
349 writel(tmp
, &dev
->ep
[ep
->num
].regs
->bufout_maxpkt
);
354 /* ep ix in UDC CSR register space */
355 udc_csr_epix
= ep
->num
;
357 /* set buffer size (tx fifo entries) */
358 tmp
= readl(&dev
->ep
[ep
->num
].regs
->bufin_framenum
);
359 /* double buffering: fifo size = 2 x max packet size */
362 maxpacket
* UDC_EPIN_BUFF_SIZE_MULT
365 writel(tmp
, &dev
->ep
[ep
->num
].regs
->bufin_framenum
);
367 /* calc. tx fifo base addr */
368 udc_set_txfifo_addr(ep
);
371 tmp
= readl(&ep
->regs
->ctl
);
372 tmp
|= AMD_BIT(UDC_EPCTL_F
);
373 writel(tmp
, &ep
->regs
->ctl
);
377 /* ep ix in UDC CSR register space */
378 udc_csr_epix
= ep
->num
- UDC_CSR_EP_OUT_IX_OFS
;
380 /* set max packet size UDC CSR */
381 tmp
= readl(&dev
->csr
->ne
[ep
->num
- UDC_CSR_EP_OUT_IX_OFS
]);
382 tmp
= AMD_ADDBITS(tmp
, maxpacket
,
384 writel(tmp
, &dev
->csr
->ne
[ep
->num
- UDC_CSR_EP_OUT_IX_OFS
]);
386 if (use_dma
&& !ep
->in
) {
387 /* alloc and init BNA dummy request */
388 ep
->bna_dummy_req
= udc_alloc_bna_dummy(ep
);
389 ep
->bna_occurred
= 0;
392 if (ep
->num
!= UDC_EP0OUT_IX
)
393 dev
->data_ep_enabled
= 1;
397 tmp
= readl(&dev
->csr
->ne
[udc_csr_epix
]);
399 tmp
= AMD_ADDBITS(tmp
, maxpacket
, UDC_CSR_NE_MAX_PKT
);
401 tmp
= AMD_ADDBITS(tmp
, desc
->bEndpointAddress
, UDC_CSR_NE_NUM
);
403 tmp
= AMD_ADDBITS(tmp
, ep
->in
, UDC_CSR_NE_DIR
);
405 tmp
= AMD_ADDBITS(tmp
, desc
->bmAttributes
, UDC_CSR_NE_TYPE
);
407 tmp
= AMD_ADDBITS(tmp
, ep
->dev
->cur_config
, UDC_CSR_NE_CFG
);
409 tmp
= AMD_ADDBITS(tmp
, ep
->dev
->cur_intf
, UDC_CSR_NE_INTF
);
411 tmp
= AMD_ADDBITS(tmp
, ep
->dev
->cur_alt
, UDC_CSR_NE_ALT
);
413 writel(tmp
, &dev
->csr
->ne
[udc_csr_epix
]);
416 tmp
= readl(&dev
->regs
->ep_irqmsk
);
417 tmp
&= AMD_UNMASK_BIT(ep
->num
);
418 writel(tmp
, &dev
->regs
->ep_irqmsk
);
421 * clear NAK by writing CNAK
422 * avoid BNA for OUT DMA, don't clear NAK until DMA desc. written
424 if (!use_dma
|| ep
->in
) {
425 tmp
= readl(&ep
->regs
->ctl
);
426 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
427 writel(tmp
, &ep
->regs
->ctl
);
429 UDC_QUEUE_CNAK(ep
, ep
->num
);
431 tmp
= desc
->bEndpointAddress
;
432 DBG(dev
, "%s enabled\n", usbep
->name
);
434 spin_unlock_irqrestore(&dev
->lock
, iflags
);
438 /* Resets endpoint */
439 static void ep_init(struct udc_regs __iomem
*regs
, struct udc_ep
*ep
)
443 VDBG(ep
->dev
, "ep-%d reset\n", ep
->num
);
445 ep
->ep
.ops
= &udc_ep_ops
;
446 INIT_LIST_HEAD(&ep
->queue
);
448 usb_ep_set_maxpacket_limit(&ep
->ep
,(u16
) ~0);
450 tmp
= readl(&ep
->regs
->ctl
);
451 tmp
|= AMD_BIT(UDC_EPCTL_SNAK
);
452 writel(tmp
, &ep
->regs
->ctl
);
455 /* disable interrupt */
456 tmp
= readl(®s
->ep_irqmsk
);
457 tmp
|= AMD_BIT(ep
->num
);
458 writel(tmp
, ®s
->ep_irqmsk
);
461 /* unset P and IN bit of potential former DMA */
462 tmp
= readl(&ep
->regs
->ctl
);
463 tmp
&= AMD_UNMASK_BIT(UDC_EPCTL_P
);
464 writel(tmp
, &ep
->regs
->ctl
);
466 tmp
= readl(&ep
->regs
->sts
);
467 tmp
|= AMD_BIT(UDC_EPSTS_IN
);
468 writel(tmp
, &ep
->regs
->sts
);
471 tmp
= readl(&ep
->regs
->ctl
);
472 tmp
|= AMD_BIT(UDC_EPCTL_F
);
473 writel(tmp
, &ep
->regs
->ctl
);
476 /* reset desc pointer */
477 writel(0, &ep
->regs
->desptr
);
480 /* Disables endpoint, is called by gadget driver */
481 static int udc_ep_disable(struct usb_ep
*usbep
)
483 struct udc_ep
*ep
= NULL
;
484 unsigned long iflags
;
489 ep
= container_of(usbep
, struct udc_ep
, ep
);
490 if (usbep
->name
== ep0_string
|| !ep
->ep
.desc
)
493 DBG(ep
->dev
, "Disable ep-%d\n", ep
->num
);
495 spin_lock_irqsave(&ep
->dev
->lock
, iflags
);
496 udc_free_request(&ep
->ep
, &ep
->bna_dummy_req
->req
);
498 ep_init(ep
->dev
->regs
, ep
);
499 spin_unlock_irqrestore(&ep
->dev
->lock
, iflags
);
504 /* Allocates request packet, called by gadget driver */
505 static struct usb_request
*
506 udc_alloc_request(struct usb_ep
*usbep
, gfp_t gfp
)
508 struct udc_request
*req
;
509 struct udc_data_dma
*dma_desc
;
515 ep
= container_of(usbep
, struct udc_ep
, ep
);
517 VDBG(ep
->dev
, "udc_alloc_req(): ep%d\n", ep
->num
);
518 req
= kzalloc(sizeof(struct udc_request
), gfp
);
522 req
->req
.dma
= DMA_DONT_USE
;
523 INIT_LIST_HEAD(&req
->queue
);
526 /* ep0 in requests are allocated from data pool here */
527 dma_desc
= pci_pool_alloc(ep
->dev
->data_requests
, gfp
,
534 VDBG(ep
->dev
, "udc_alloc_req: req = %p dma_desc = %p, "
537 (unsigned long)req
->td_phys
);
538 /* prevent from using desc. - set HOST BUSY */
539 dma_desc
->status
= AMD_ADDBITS(dma_desc
->status
,
540 UDC_DMA_STP_STS_BS_HOST_BUSY
,
542 dma_desc
->bufptr
= cpu_to_le32(DMA_DONT_USE
);
543 req
->td_data
= dma_desc
;
544 req
->td_data_last
= NULL
;
551 /* Frees request packet, called by gadget driver */
553 udc_free_request(struct usb_ep
*usbep
, struct usb_request
*usbreq
)
556 struct udc_request
*req
;
558 if (!usbep
|| !usbreq
)
561 ep
= container_of(usbep
, struct udc_ep
, ep
);
562 req
= container_of(usbreq
, struct udc_request
, req
);
563 VDBG(ep
->dev
, "free_req req=%p\n", req
);
564 BUG_ON(!list_empty(&req
->queue
));
566 VDBG(ep
->dev
, "req->td_data=%p\n", req
->td_data
);
568 /* free dma chain if created */
569 if (req
->chain_len
> 1)
570 udc_free_dma_chain(ep
->dev
, req
);
572 pci_pool_free(ep
->dev
->data_requests
, req
->td_data
,
578 /* Init BNA dummy descriptor for HOST BUSY and pointing to itself */
579 static void udc_init_bna_dummy(struct udc_request
*req
)
583 req
->td_data
->status
|= AMD_BIT(UDC_DMA_IN_STS_L
);
584 /* set next pointer to itself */
585 req
->td_data
->next
= req
->td_phys
;
588 = AMD_ADDBITS(req
->td_data
->status
,
589 UDC_DMA_STP_STS_BS_DMA_DONE
,
592 pr_debug("bna desc = %p, sts = %08x\n",
593 req
->td_data
, req
->td_data
->status
);
598 /* Allocate BNA dummy descriptor */
599 static struct udc_request
*udc_alloc_bna_dummy(struct udc_ep
*ep
)
601 struct udc_request
*req
= NULL
;
602 struct usb_request
*_req
= NULL
;
604 /* alloc the dummy request */
605 _req
= udc_alloc_request(&ep
->ep
, GFP_ATOMIC
);
607 req
= container_of(_req
, struct udc_request
, req
);
608 ep
->bna_dummy_req
= req
;
609 udc_init_bna_dummy(req
);
614 /* Write data to TX fifo for IN packets */
616 udc_txfifo_write(struct udc_ep
*ep
, struct usb_request
*req
)
622 unsigned remaining
= 0;
627 req_buf
= req
->buf
+ req
->actual
;
629 remaining
= req
->length
- req
->actual
;
631 buf
= (u32
*) req_buf
;
633 bytes
= ep
->ep
.maxpacket
;
634 if (bytes
> remaining
)
638 for (i
= 0; i
< bytes
/ UDC_DWORD_BYTES
; i
++)
639 writel(*(buf
+ i
), ep
->txfifo
);
641 /* remaining bytes must be written by byte access */
642 for (j
= 0; j
< bytes
% UDC_DWORD_BYTES
; j
++) {
643 writeb((u8
)(*(buf
+ i
) >> (j
<< UDC_BITS_PER_BYTE_SHIFT
)),
647 /* dummy write confirm */
648 writel(0, &ep
->regs
->confirm
);
651 /* Read dwords from RX fifo for OUT transfers */
652 static int udc_rxfifo_read_dwords(struct udc
*dev
, u32
*buf
, int dwords
)
656 VDBG(dev
, "udc_read_dwords(): %d dwords\n", dwords
);
658 for (i
= 0; i
< dwords
; i
++)
659 *(buf
+ i
) = readl(dev
->rxfifo
);
663 /* Read bytes from RX fifo for OUT transfers */
664 static int udc_rxfifo_read_bytes(struct udc
*dev
, u8
*buf
, int bytes
)
669 VDBG(dev
, "udc_read_bytes(): %d bytes\n", bytes
);
672 for (i
= 0; i
< bytes
/ UDC_DWORD_BYTES
; i
++)
673 *((u32
*)(buf
+ (i
<<2))) = readl(dev
->rxfifo
);
675 /* remaining bytes must be read by byte access */
676 if (bytes
% UDC_DWORD_BYTES
) {
677 tmp
= readl(dev
->rxfifo
);
678 for (j
= 0; j
< bytes
% UDC_DWORD_BYTES
; j
++) {
679 *(buf
+ (i
<<2) + j
) = (u8
)(tmp
& UDC_BYTE_MASK
);
680 tmp
= tmp
>> UDC_BITS_PER_BYTE
;
687 /* Read data from RX fifo for OUT transfers */
689 udc_rxfifo_read(struct udc_ep
*ep
, struct udc_request
*req
)
694 unsigned finished
= 0;
696 /* received number bytes */
697 bytes
= readl(&ep
->regs
->sts
);
698 bytes
= AMD_GETBITS(bytes
, UDC_EPSTS_RX_PKT_SIZE
);
700 buf_space
= req
->req
.length
- req
->req
.actual
;
701 buf
= req
->req
.buf
+ req
->req
.actual
;
702 if (bytes
> buf_space
) {
703 if ((buf_space
% ep
->ep
.maxpacket
) != 0) {
705 "%s: rx %d bytes, rx-buf space = %d bytesn\n",
706 ep
->ep
.name
, bytes
, buf_space
);
707 req
->req
.status
= -EOVERFLOW
;
711 req
->req
.actual
+= bytes
;
714 if (((bytes
% ep
->ep
.maxpacket
) != 0) || (!bytes
)
715 || ((req
->req
.actual
== req
->req
.length
) && !req
->req
.zero
))
718 /* read rx fifo bytes */
719 VDBG(ep
->dev
, "ep %s: rxfifo read %d bytes\n", ep
->ep
.name
, bytes
);
720 udc_rxfifo_read_bytes(ep
->dev
, buf
, bytes
);
725 /* create/re-init a DMA descriptor or a DMA descriptor chain */
726 static int prep_dma(struct udc_ep
*ep
, struct udc_request
*req
, gfp_t gfp
)
731 VDBG(ep
->dev
, "prep_dma\n");
732 VDBG(ep
->dev
, "prep_dma ep%d req->td_data=%p\n",
733 ep
->num
, req
->td_data
);
735 /* set buffer pointer */
736 req
->td_data
->bufptr
= req
->req
.dma
;
739 req
->td_data
->status
|= AMD_BIT(UDC_DMA_IN_STS_L
);
741 /* build/re-init dma chain if maxpkt scatter mode, not for EP0 */
744 retval
= udc_create_dma_chain(ep
, req
, ep
->ep
.maxpacket
, gfp
);
746 if (retval
== -ENOMEM
)
747 DBG(ep
->dev
, "Out of DMA memory\n");
751 if (req
->req
.length
== ep
->ep
.maxpacket
) {
753 req
->td_data
->status
=
754 AMD_ADDBITS(req
->td_data
->status
,
756 UDC_DMA_IN_STS_TXBYTES
);
764 VDBG(ep
->dev
, "IN: use_dma_ppb=%d req->req.len=%d "
765 "maxpacket=%d ep%d\n",
766 use_dma_ppb
, req
->req
.length
,
767 ep
->ep
.maxpacket
, ep
->num
);
769 * if bytes < max packet then tx bytes must
770 * be written in packet per buffer mode
772 if (!use_dma_ppb
|| req
->req
.length
< ep
->ep
.maxpacket
773 || ep
->num
== UDC_EP0OUT_IX
774 || ep
->num
== UDC_EP0IN_IX
) {
776 req
->td_data
->status
=
777 AMD_ADDBITS(req
->td_data
->status
,
779 UDC_DMA_IN_STS_TXBYTES
);
780 /* reset frame num */
781 req
->td_data
->status
=
782 AMD_ADDBITS(req
->td_data
->status
,
784 UDC_DMA_IN_STS_FRAMENUM
);
787 req
->td_data
->status
=
788 AMD_ADDBITS(req
->td_data
->status
,
789 UDC_DMA_STP_STS_BS_HOST_BUSY
,
792 VDBG(ep
->dev
, "OUT set host ready\n");
794 req
->td_data
->status
=
795 AMD_ADDBITS(req
->td_data
->status
,
796 UDC_DMA_STP_STS_BS_HOST_READY
,
800 /* clear NAK by writing CNAK */
802 tmp
= readl(&ep
->regs
->ctl
);
803 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
804 writel(tmp
, &ep
->regs
->ctl
);
806 UDC_QUEUE_CNAK(ep
, ep
->num
);
814 /* Completes request packet ... caller MUST hold lock */
816 complete_req(struct udc_ep
*ep
, struct udc_request
*req
, int sts
)
817 __releases(ep
->dev
->lock
)
818 __acquires(ep
->dev
->lock
)
823 VDBG(ep
->dev
, "complete_req(): ep%d\n", ep
->num
);
828 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->in
);
833 /* set new status if pending */
834 if (req
->req
.status
== -EINPROGRESS
)
835 req
->req
.status
= sts
;
837 /* remove from ep queue */
838 list_del_init(&req
->queue
);
840 VDBG(ep
->dev
, "req %p => complete %d bytes at %s with sts %d\n",
841 &req
->req
, req
->req
.length
, ep
->ep
.name
, sts
);
843 spin_unlock(&dev
->lock
);
844 req
->req
.complete(&ep
->ep
, &req
->req
);
845 spin_lock(&dev
->lock
);
849 /* frees pci pool descriptors of a DMA chain */
850 static int udc_free_dma_chain(struct udc
*dev
, struct udc_request
*req
)
854 struct udc_data_dma
*td
;
855 struct udc_data_dma
*td_last
= NULL
;
858 DBG(dev
, "free chain req = %p\n", req
);
860 /* do not free first desc., will be done by free for request */
861 td_last
= req
->td_data
;
862 td
= phys_to_virt(td_last
->next
);
864 for (i
= 1; i
< req
->chain_len
; i
++) {
866 pci_pool_free(dev
->data_requests
, td
,
867 (dma_addr_t
) td_last
->next
);
869 td
= phys_to_virt(td_last
->next
);
875 /* Iterates to the end of a DMA chain and returns last descriptor */
876 static struct udc_data_dma
*udc_get_last_dma_desc(struct udc_request
*req
)
878 struct udc_data_dma
*td
;
881 while (td
&& !(td
->status
& AMD_BIT(UDC_DMA_IN_STS_L
)))
882 td
= phys_to_virt(td
->next
);
888 /* Iterates to the end of a DMA chain and counts bytes received */
889 static u32
udc_get_ppbdu_rxbytes(struct udc_request
*req
)
891 struct udc_data_dma
*td
;
895 /* received number bytes */
896 count
= AMD_GETBITS(td
->status
, UDC_DMA_OUT_STS_RXBYTES
);
898 while (td
&& !(td
->status
& AMD_BIT(UDC_DMA_IN_STS_L
))) {
899 td
= phys_to_virt(td
->next
);
900 /* received number bytes */
902 count
+= AMD_GETBITS(td
->status
,
903 UDC_DMA_OUT_STS_RXBYTES
);
911 /* Creates or re-inits a DMA chain */
912 static int udc_create_dma_chain(
914 struct udc_request
*req
,
915 unsigned long buf_len
, gfp_t gfp_flags
918 unsigned long bytes
= req
->req
.length
;
921 struct udc_data_dma
*td
= NULL
;
922 struct udc_data_dma
*last
= NULL
;
923 unsigned long txbytes
;
924 unsigned create_new_chain
= 0;
927 VDBG(ep
->dev
, "udc_create_dma_chain: bytes=%ld buf_len=%ld\n",
929 dma_addr
= DMA_DONT_USE
;
931 /* unset L bit in first desc for OUT */
933 req
->td_data
->status
&= AMD_CLEAR_BIT(UDC_DMA_IN_STS_L
);
935 /* alloc only new desc's if not already available */
936 len
= req
->req
.length
/ ep
->ep
.maxpacket
;
937 if (req
->req
.length
% ep
->ep
.maxpacket
)
940 if (len
> req
->chain_len
) {
941 /* shorter chain already allocated before */
942 if (req
->chain_len
> 1)
943 udc_free_dma_chain(ep
->dev
, req
);
944 req
->chain_len
= len
;
945 create_new_chain
= 1;
949 /* gen. required number of descriptors and buffers */
950 for (i
= buf_len
; i
< bytes
; i
+= buf_len
) {
951 /* create or determine next desc. */
952 if (create_new_chain
) {
954 td
= pci_pool_alloc(ep
->dev
->data_requests
,
955 gfp_flags
, &dma_addr
);
960 } else if (i
== buf_len
) {
962 td
= (struct udc_data_dma
*) phys_to_virt(
966 td
= (struct udc_data_dma
*) phys_to_virt(last
->next
);
972 td
->bufptr
= req
->req
.dma
+ i
; /* assign buffer */
977 if ((bytes
- i
) >= buf_len
) {
984 /* link td and assign tx bytes */
986 if (create_new_chain
)
987 req
->td_data
->next
= dma_addr
;
990 req->td_data->next = virt_to_phys(td);
995 req
->td_data
->status
=
996 AMD_ADDBITS(req
->td_data
->status
,
998 UDC_DMA_IN_STS_TXBYTES
);
1000 td
->status
= AMD_ADDBITS(td
->status
,
1002 UDC_DMA_IN_STS_TXBYTES
);
1005 if (create_new_chain
)
1006 last
->next
= dma_addr
;
1009 last->next = virt_to_phys(td);
1012 /* write tx bytes */
1013 td
->status
= AMD_ADDBITS(td
->status
,
1015 UDC_DMA_IN_STS_TXBYTES
);
1022 td
->status
|= AMD_BIT(UDC_DMA_IN_STS_L
);
1023 /* last desc. points to itself */
1024 req
->td_data_last
= td
;
1030 /* Enabling RX DMA */
1031 static void udc_set_rde(struct udc
*dev
)
1035 VDBG(dev
, "udc_set_rde()\n");
1036 /* stop RDE timer */
1037 if (timer_pending(&udc_timer
)) {
1039 mod_timer(&udc_timer
, jiffies
- 1);
1042 tmp
= readl(&dev
->regs
->ctl
);
1043 tmp
|= AMD_BIT(UDC_DEVCTL_RDE
);
1044 writel(tmp
, &dev
->regs
->ctl
);
1047 /* Queues a request packet, called by gadget driver */
1049 udc_queue(struct usb_ep
*usbep
, struct usb_request
*usbreq
, gfp_t gfp
)
1053 unsigned long iflags
;
1055 struct udc_request
*req
;
1059 /* check the inputs */
1060 req
= container_of(usbreq
, struct udc_request
, req
);
1062 if (!usbep
|| !usbreq
|| !usbreq
->complete
|| !usbreq
->buf
1063 || !list_empty(&req
->queue
))
1066 ep
= container_of(usbep
, struct udc_ep
, ep
);
1067 if (!ep
->ep
.desc
&& (ep
->num
!= 0 && ep
->num
!= UDC_EP0OUT_IX
))
1070 VDBG(ep
->dev
, "udc_queue(): ep%d-in=%d\n", ep
->num
, ep
->in
);
1073 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1076 /* map dma (usually done before) */
1078 VDBG(dev
, "DMA map req %p\n", req
);
1079 retval
= usb_gadget_map_request(&udc
->gadget
, usbreq
, ep
->in
);
1084 VDBG(dev
, "%s queue req %p, len %d req->td_data=%p buf %p\n",
1085 usbep
->name
, usbreq
, usbreq
->length
,
1086 req
->td_data
, usbreq
->buf
);
1088 spin_lock_irqsave(&dev
->lock
, iflags
);
1090 usbreq
->status
= -EINPROGRESS
;
1093 /* on empty queue just do first transfer */
1094 if (list_empty(&ep
->queue
)) {
1096 if (usbreq
->length
== 0) {
1097 /* IN zlp's are handled by hardware */
1098 complete_req(ep
, req
, 0);
1099 VDBG(dev
, "%s: zlp\n", ep
->ep
.name
);
1101 * if set_config or set_intf is waiting for ack by zlp
1104 if (dev
->set_cfg_not_acked
) {
1105 tmp
= readl(&dev
->regs
->ctl
);
1106 tmp
|= AMD_BIT(UDC_DEVCTL_CSR_DONE
);
1107 writel(tmp
, &dev
->regs
->ctl
);
1108 dev
->set_cfg_not_acked
= 0;
1110 /* setup command is ACK'ed now by zlp */
1111 if (dev
->waiting_zlp_ack_ep0in
) {
1112 /* clear NAK by writing CNAK in EP0_IN */
1113 tmp
= readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
1114 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
1115 writel(tmp
, &dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
1116 dev
->ep
[UDC_EP0IN_IX
].naking
= 0;
1117 UDC_QUEUE_CNAK(&dev
->ep
[UDC_EP0IN_IX
],
1119 dev
->waiting_zlp_ack_ep0in
= 0;
1124 retval
= prep_dma(ep
, req
, GFP_ATOMIC
);
1127 /* write desc pointer to enable DMA */
1129 /* set HOST READY */
1130 req
->td_data
->status
=
1131 AMD_ADDBITS(req
->td_data
->status
,
1132 UDC_DMA_IN_STS_BS_HOST_READY
,
1136 /* disabled rx dma while descriptor update */
1138 /* stop RDE timer */
1139 if (timer_pending(&udc_timer
)) {
1141 mod_timer(&udc_timer
, jiffies
- 1);
1144 tmp
= readl(&dev
->regs
->ctl
);
1145 tmp
&= AMD_UNMASK_BIT(UDC_DEVCTL_RDE
);
1146 writel(tmp
, &dev
->regs
->ctl
);
1150 * if BNA occurred then let BNA dummy desc.
1151 * point to current desc.
1153 if (ep
->bna_occurred
) {
1154 VDBG(dev
, "copy to BNA dummy desc.\n");
1155 memcpy(ep
->bna_dummy_req
->td_data
,
1157 sizeof(struct udc_data_dma
));
1160 /* write desc pointer */
1161 writel(req
->td_phys
, &ep
->regs
->desptr
);
1163 /* clear NAK by writing CNAK */
1165 tmp
= readl(&ep
->regs
->ctl
);
1166 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
1167 writel(tmp
, &ep
->regs
->ctl
);
1169 UDC_QUEUE_CNAK(ep
, ep
->num
);
1174 tmp
= readl(&dev
->regs
->ep_irqmsk
);
1175 tmp
&= AMD_UNMASK_BIT(ep
->num
);
1176 writel(tmp
, &dev
->regs
->ep_irqmsk
);
1178 } else if (ep
->in
) {
1180 tmp
= readl(&dev
->regs
->ep_irqmsk
);
1181 tmp
&= AMD_UNMASK_BIT(ep
->num
);
1182 writel(tmp
, &dev
->regs
->ep_irqmsk
);
1185 } else if (ep
->dma
) {
1188 * prep_dma not used for OUT ep's, this is not possible
1189 * for PPB modes, because of chain creation reasons
1192 retval
= prep_dma(ep
, req
, GFP_ATOMIC
);
1197 VDBG(dev
, "list_add\n");
1198 /* add request to ep queue */
1201 list_add_tail(&req
->queue
, &ep
->queue
);
1203 /* open rxfifo if out data queued */
1208 if (ep
->num
!= UDC_EP0OUT_IX
)
1209 dev
->data_ep_queued
= 1;
1211 /* stop OUT naking */
1213 if (!use_dma
&& udc_rxfifo_pending
) {
1214 DBG(dev
, "udc_queue(): pending bytes in "
1215 "rxfifo after nyet\n");
1217 * read pending bytes afer nyet:
1220 if (udc_rxfifo_read(ep
, req
)) {
1222 complete_req(ep
, req
, 0);
1224 udc_rxfifo_pending
= 0;
1231 spin_unlock_irqrestore(&dev
->lock
, iflags
);
1235 /* Empty request queue of an endpoint; caller holds spinlock */
1236 static void empty_req_queue(struct udc_ep
*ep
)
1238 struct udc_request
*req
;
1241 while (!list_empty(&ep
->queue
)) {
1242 req
= list_entry(ep
->queue
.next
,
1245 complete_req(ep
, req
, -ESHUTDOWN
);
1249 /* Dequeues a request packet, called by gadget driver */
1250 static int udc_dequeue(struct usb_ep
*usbep
, struct usb_request
*usbreq
)
1253 struct udc_request
*req
;
1255 unsigned long iflags
;
1257 ep
= container_of(usbep
, struct udc_ep
, ep
);
1258 if (!usbep
|| !usbreq
|| (!ep
->ep
.desc
&& (ep
->num
!= 0
1259 && ep
->num
!= UDC_EP0OUT_IX
)))
1262 req
= container_of(usbreq
, struct udc_request
, req
);
1264 spin_lock_irqsave(&ep
->dev
->lock
, iflags
);
1265 halted
= ep
->halted
;
1267 /* request in processing or next one */
1268 if (ep
->queue
.next
== &req
->queue
) {
1269 if (ep
->dma
&& req
->dma_going
) {
1271 ep
->cancel_transfer
= 1;
1275 /* stop potential receive DMA */
1276 tmp
= readl(&udc
->regs
->ctl
);
1277 writel(tmp
& AMD_UNMASK_BIT(UDC_DEVCTL_RDE
),
1280 * Cancel transfer later in ISR
1281 * if descriptor was touched.
1283 dma_sts
= AMD_GETBITS(req
->td_data
->status
,
1284 UDC_DMA_OUT_STS_BS
);
1285 if (dma_sts
!= UDC_DMA_OUT_STS_BS_HOST_READY
)
1286 ep
->cancel_transfer
= 1;
1288 udc_init_bna_dummy(ep
->req
);
1289 writel(ep
->bna_dummy_req
->td_phys
,
1292 writel(tmp
, &udc
->regs
->ctl
);
1296 complete_req(ep
, req
, -ECONNRESET
);
1297 ep
->halted
= halted
;
1299 spin_unlock_irqrestore(&ep
->dev
->lock
, iflags
);
1303 /* Halt or clear halt of endpoint */
1305 udc_set_halt(struct usb_ep
*usbep
, int halt
)
1309 unsigned long iflags
;
1315 pr_debug("set_halt %s: halt=%d\n", usbep
->name
, halt
);
1317 ep
= container_of(usbep
, struct udc_ep
, ep
);
1318 if (!ep
->ep
.desc
&& (ep
->num
!= 0 && ep
->num
!= UDC_EP0OUT_IX
))
1320 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1323 spin_lock_irqsave(&udc_stall_spinlock
, iflags
);
1324 /* halt or clear halt */
1327 ep
->dev
->stall_ep0in
= 1;
1331 * rxfifo empty not taken into acount
1333 tmp
= readl(&ep
->regs
->ctl
);
1334 tmp
|= AMD_BIT(UDC_EPCTL_S
);
1335 writel(tmp
, &ep
->regs
->ctl
);
1338 /* setup poll timer */
1339 if (!timer_pending(&udc_pollstall_timer
)) {
1340 udc_pollstall_timer
.expires
= jiffies
+
1341 HZ
* UDC_POLLSTALL_TIMER_USECONDS
1343 if (!stop_pollstall_timer
) {
1344 DBG(ep
->dev
, "start polltimer\n");
1345 add_timer(&udc_pollstall_timer
);
1350 /* ep is halted by set_halt() before */
1352 tmp
= readl(&ep
->regs
->ctl
);
1353 /* clear stall bit */
1354 tmp
= tmp
& AMD_CLEAR_BIT(UDC_EPCTL_S
);
1355 /* clear NAK by writing CNAK */
1356 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
1357 writel(tmp
, &ep
->regs
->ctl
);
1359 UDC_QUEUE_CNAK(ep
, ep
->num
);
1362 spin_unlock_irqrestore(&udc_stall_spinlock
, iflags
);
1366 /* gadget interface */
1367 static const struct usb_ep_ops udc_ep_ops
= {
1368 .enable
= udc_ep_enable
,
1369 .disable
= udc_ep_disable
,
1371 .alloc_request
= udc_alloc_request
,
1372 .free_request
= udc_free_request
,
1375 .dequeue
= udc_dequeue
,
1377 .set_halt
= udc_set_halt
,
1378 /* fifo ops not implemented */
1381 /*-------------------------------------------------------------------------*/
1383 /* Get frame counter (not implemented) */
1384 static int udc_get_frame(struct usb_gadget
*gadget
)
1389 /* Remote wakeup gadget interface */
1390 static int udc_wakeup(struct usb_gadget
*gadget
)
1396 dev
= container_of(gadget
, struct udc
, gadget
);
1397 udc_remote_wakeup(dev
);
1402 static int amd5536_udc_start(struct usb_gadget
*g
,
1403 struct usb_gadget_driver
*driver
);
1404 static int amd5536_udc_stop(struct usb_gadget
*g
,
1405 struct usb_gadget_driver
*driver
);
1406 /* gadget operations */
1407 static const struct usb_gadget_ops udc_ops
= {
1408 .wakeup
= udc_wakeup
,
1409 .get_frame
= udc_get_frame
,
1410 .udc_start
= amd5536_udc_start
,
1411 .udc_stop
= amd5536_udc_stop
,
1414 /* Setups endpoint parameters, adds endpoints to linked list */
1415 static void make_ep_lists(struct udc
*dev
)
1417 /* make gadget ep lists */
1418 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
1419 list_add_tail(&dev
->ep
[UDC_EPIN_STATUS_IX
].ep
.ep_list
,
1420 &dev
->gadget
.ep_list
);
1421 list_add_tail(&dev
->ep
[UDC_EPIN_IX
].ep
.ep_list
,
1422 &dev
->gadget
.ep_list
);
1423 list_add_tail(&dev
->ep
[UDC_EPOUT_IX
].ep
.ep_list
,
1424 &dev
->gadget
.ep_list
);
1427 dev
->ep
[UDC_EPIN_STATUS_IX
].fifo_depth
= UDC_EPIN_SMALLINT_BUFF_SIZE
;
1428 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1429 dev
->ep
[UDC_EPIN_IX
].fifo_depth
= UDC_FS_EPIN_BUFF_SIZE
;
1430 else if (dev
->gadget
.speed
== USB_SPEED_HIGH
)
1431 dev
->ep
[UDC_EPIN_IX
].fifo_depth
= hs_tx_buf
;
1432 dev
->ep
[UDC_EPOUT_IX
].fifo_depth
= UDC_RXFIFO_SIZE
;
1435 /* init registers at driver load time */
1436 static int startup_registers(struct udc
*dev
)
1440 /* init controller by soft reset */
1441 udc_soft_reset(dev
);
1443 /* mask not needed interrupts */
1444 udc_mask_unused_interrupts(dev
);
1446 /* put into initial config */
1447 udc_basic_init(dev
);
1448 /* link up all endpoints */
1449 udc_setup_endpoints(dev
);
1452 tmp
= readl(&dev
->regs
->cfg
);
1454 tmp
= AMD_ADDBITS(tmp
, UDC_DEVCFG_SPD_FS
, UDC_DEVCFG_SPD
);
1456 tmp
= AMD_ADDBITS(tmp
, UDC_DEVCFG_SPD_HS
, UDC_DEVCFG_SPD
);
1457 writel(tmp
, &dev
->regs
->cfg
);
1462 /* Inits UDC context */
1463 static void udc_basic_init(struct udc
*dev
)
1467 DBG(dev
, "udc_basic_init()\n");
1469 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1471 /* stop RDE timer */
1472 if (timer_pending(&udc_timer
)) {
1474 mod_timer(&udc_timer
, jiffies
- 1);
1476 /* stop poll stall timer */
1477 if (timer_pending(&udc_pollstall_timer
))
1478 mod_timer(&udc_pollstall_timer
, jiffies
- 1);
1480 tmp
= readl(&dev
->regs
->ctl
);
1481 tmp
&= AMD_UNMASK_BIT(UDC_DEVCTL_RDE
);
1482 tmp
&= AMD_UNMASK_BIT(UDC_DEVCTL_TDE
);
1483 writel(tmp
, &dev
->regs
->ctl
);
1485 /* enable dynamic CSR programming */
1486 tmp
= readl(&dev
->regs
->cfg
);
1487 tmp
|= AMD_BIT(UDC_DEVCFG_CSR_PRG
);
1488 /* set self powered */
1489 tmp
|= AMD_BIT(UDC_DEVCFG_SP
);
1490 /* set remote wakeupable */
1491 tmp
|= AMD_BIT(UDC_DEVCFG_RWKP
);
1492 writel(tmp
, &dev
->regs
->cfg
);
1496 dev
->data_ep_enabled
= 0;
1497 dev
->data_ep_queued
= 0;
1500 /* Sets initial endpoint parameters */
1501 static void udc_setup_endpoints(struct udc
*dev
)
1507 DBG(dev
, "udc_setup_endpoints()\n");
1509 /* read enum speed */
1510 tmp
= readl(&dev
->regs
->sts
);
1511 tmp
= AMD_GETBITS(tmp
, UDC_DEVSTS_ENUM_SPEED
);
1512 if (tmp
== UDC_DEVSTS_ENUM_SPEED_HIGH
)
1513 dev
->gadget
.speed
= USB_SPEED_HIGH
;
1514 else if (tmp
== UDC_DEVSTS_ENUM_SPEED_FULL
)
1515 dev
->gadget
.speed
= USB_SPEED_FULL
;
1517 /* set basic ep parameters */
1518 for (tmp
= 0; tmp
< UDC_EP_NUM
; tmp
++) {
1521 ep
->ep
.name
= ep_string
[tmp
];
1523 /* txfifo size is calculated at enable time */
1524 ep
->txfifo
= dev
->txfifo
;
1527 if (tmp
< UDC_EPIN_NUM
) {
1528 ep
->fifo_depth
= UDC_TXFIFO_SIZE
;
1531 ep
->fifo_depth
= UDC_RXFIFO_SIZE
;
1535 ep
->regs
= &dev
->ep_regs
[tmp
];
1537 * ep will be reset only if ep was not enabled before to avoid
1538 * disabling ep interrupts when ENUM interrupt occurs but ep is
1539 * not enabled by gadget driver
1542 ep_init(dev
->regs
, ep
);
1546 * ep->dma is not really used, just to indicate that
1547 * DMA is active: remove this
1548 * dma regs = dev control regs
1550 ep
->dma
= &dev
->regs
->ctl
;
1552 /* nak OUT endpoints until enable - not for ep0 */
1553 if (tmp
!= UDC_EP0IN_IX
&& tmp
!= UDC_EP0OUT_IX
1554 && tmp
> UDC_EPIN_NUM
) {
1556 reg
= readl(&dev
->ep
[tmp
].regs
->ctl
);
1557 reg
|= AMD_BIT(UDC_EPCTL_SNAK
);
1558 writel(reg
, &dev
->ep
[tmp
].regs
->ctl
);
1559 dev
->ep
[tmp
].naking
= 1;
1564 /* EP0 max packet */
1565 if (dev
->gadget
.speed
== USB_SPEED_FULL
) {
1566 usb_ep_set_maxpacket_limit(&dev
->ep
[UDC_EP0IN_IX
].ep
,
1567 UDC_FS_EP0IN_MAX_PKT_SIZE
);
1568 usb_ep_set_maxpacket_limit(&dev
->ep
[UDC_EP0OUT_IX
].ep
,
1569 UDC_FS_EP0OUT_MAX_PKT_SIZE
);
1570 } else if (dev
->gadget
.speed
== USB_SPEED_HIGH
) {
1571 usb_ep_set_maxpacket_limit(&dev
->ep
[UDC_EP0IN_IX
].ep
,
1572 UDC_EP0IN_MAX_PKT_SIZE
);
1573 usb_ep_set_maxpacket_limit(&dev
->ep
[UDC_EP0OUT_IX
].ep
,
1574 UDC_EP0OUT_MAX_PKT_SIZE
);
1578 * with suspend bug workaround, ep0 params for gadget driver
1579 * are set at gadget driver bind() call
1581 dev
->gadget
.ep0
= &dev
->ep
[UDC_EP0IN_IX
].ep
;
1582 dev
->ep
[UDC_EP0IN_IX
].halted
= 0;
1583 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
1585 /* init cfg/alt/int */
1586 dev
->cur_config
= 0;
1591 /* Bringup after Connect event, initial bringup to be ready for ep0 events */
1592 static void usb_connect(struct udc
*dev
)
1595 dev_info(&dev
->pdev
->dev
, "USB Connect\n");
1599 /* put into initial config */
1600 udc_basic_init(dev
);
1602 /* enable device setup interrupts */
1603 udc_enable_dev_setup_interrupts(dev
);
1607 * Calls gadget with disconnect event and resets the UDC and makes
1608 * initial bringup to be ready for ep0 events
1610 static void usb_disconnect(struct udc
*dev
)
1613 dev_info(&dev
->pdev
->dev
, "USB Disconnect\n");
1617 /* mask interrupts */
1618 udc_mask_unused_interrupts(dev
);
1620 /* REVISIT there doesn't seem to be a point to having this
1621 * talk to a tasklet ... do it directly, we already hold
1622 * the spinlock needed to process the disconnect.
1625 tasklet_schedule(&disconnect_tasklet
);
1628 /* Tasklet for disconnect to be outside of interrupt context */
1629 static void udc_tasklet_disconnect(unsigned long par
)
1631 struct udc
*dev
= (struct udc
*)(*((struct udc
**) par
));
1634 DBG(dev
, "Tasklet disconnect\n");
1635 spin_lock_irq(&dev
->lock
);
1638 spin_unlock(&dev
->lock
);
1639 dev
->driver
->disconnect(&dev
->gadget
);
1640 spin_lock(&dev
->lock
);
1643 for (tmp
= 0; tmp
< UDC_EP_NUM
; tmp
++)
1644 empty_req_queue(&dev
->ep
[tmp
]);
1650 &dev
->ep
[UDC_EP0IN_IX
]);
1653 if (!soft_reset_occured
) {
1654 /* init controller by soft reset */
1655 udc_soft_reset(dev
);
1656 soft_reset_occured
++;
1659 /* re-enable dev interrupts */
1660 udc_enable_dev_setup_interrupts(dev
);
1661 /* back to full speed ? */
1662 if (use_fullspeed
) {
1663 tmp
= readl(&dev
->regs
->cfg
);
1664 tmp
= AMD_ADDBITS(tmp
, UDC_DEVCFG_SPD_FS
, UDC_DEVCFG_SPD
);
1665 writel(tmp
, &dev
->regs
->cfg
);
1668 spin_unlock_irq(&dev
->lock
);
1671 /* Reset the UDC core */
1672 static void udc_soft_reset(struct udc
*dev
)
1674 unsigned long flags
;
1676 DBG(dev
, "Soft reset\n");
1678 * reset possible waiting interrupts, because int.
1679 * status is lost after soft reset,
1680 * ep int. status reset
1682 writel(UDC_EPINT_MSK_DISABLE_ALL
, &dev
->regs
->ep_irqsts
);
1683 /* device int. status reset */
1684 writel(UDC_DEV_MSK_DISABLE
, &dev
->regs
->irqsts
);
1686 spin_lock_irqsave(&udc_irq_spinlock
, flags
);
1687 writel(AMD_BIT(UDC_DEVCFG_SOFTRESET
), &dev
->regs
->cfg
);
1688 readl(&dev
->regs
->cfg
);
1689 spin_unlock_irqrestore(&udc_irq_spinlock
, flags
);
1693 /* RDE timer callback to set RDE bit */
1694 static void udc_timer_function(unsigned long v
)
1698 spin_lock_irq(&udc_irq_spinlock
);
1702 * open the fifo if fifo was filled on last timer call
1706 /* set RDE to receive setup data */
1707 tmp
= readl(&udc
->regs
->ctl
);
1708 tmp
|= AMD_BIT(UDC_DEVCTL_RDE
);
1709 writel(tmp
, &udc
->regs
->ctl
);
1711 } else if (readl(&udc
->regs
->sts
)
1712 & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY
)) {
1714 * if fifo empty setup polling, do not just
1717 udc_timer
.expires
= jiffies
+ HZ
/UDC_RDE_TIMER_DIV
;
1719 add_timer(&udc_timer
);
1722 * fifo contains data now, setup timer for opening
1723 * the fifo when timer expires to be able to receive
1724 * setup packets, when data packets gets queued by
1725 * gadget layer then timer will forced to expire with
1726 * set_rde=0 (RDE is set in udc_queue())
1729 /* debug: lhadmot_timer_start = 221070 */
1730 udc_timer
.expires
= jiffies
+ HZ
*UDC_RDE_TIMER_SECONDS
;
1732 add_timer(&udc_timer
);
1736 set_rde
= -1; /* RDE was set by udc_queue() */
1737 spin_unlock_irq(&udc_irq_spinlock
);
1743 /* Handle halt state, used in stall poll timer */
1744 static void udc_handle_halt_state(struct udc_ep
*ep
)
1747 /* set stall as long not halted */
1748 if (ep
->halted
== 1) {
1749 tmp
= readl(&ep
->regs
->ctl
);
1750 /* STALL cleared ? */
1751 if (!(tmp
& AMD_BIT(UDC_EPCTL_S
))) {
1753 * FIXME: MSC spec requires that stall remains
1754 * even on receivng of CLEAR_FEATURE HALT. So
1755 * we would set STALL again here to be compliant.
1756 * But with current mass storage drivers this does
1757 * not work (would produce endless host retries).
1758 * So we clear halt on CLEAR_FEATURE.
1760 DBG(ep->dev, "ep %d: set STALL again\n", ep->num);
1761 tmp |= AMD_BIT(UDC_EPCTL_S);
1762 writel(tmp, &ep->regs->ctl);*/
1764 /* clear NAK by writing CNAK */
1765 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
1766 writel(tmp
, &ep
->regs
->ctl
);
1768 UDC_QUEUE_CNAK(ep
, ep
->num
);
1773 /* Stall timer callback to poll S bit and set it again after */
1774 static void udc_pollstall_timer_function(unsigned long v
)
1779 spin_lock_irq(&udc_stall_spinlock
);
1781 * only one IN and OUT endpoints are handled
1784 ep
= &udc
->ep
[UDC_EPIN_IX
];
1785 udc_handle_halt_state(ep
);
1788 /* OUT poll stall */
1789 ep
= &udc
->ep
[UDC_EPOUT_IX
];
1790 udc_handle_halt_state(ep
);
1794 /* setup timer again when still halted */
1795 if (!stop_pollstall_timer
&& halted
) {
1796 udc_pollstall_timer
.expires
= jiffies
+
1797 HZ
* UDC_POLLSTALL_TIMER_USECONDS
1799 add_timer(&udc_pollstall_timer
);
1801 spin_unlock_irq(&udc_stall_spinlock
);
1803 if (stop_pollstall_timer
)
1804 complete(&on_pollstall_exit
);
1807 /* Inits endpoint 0 so that SETUP packets are processed */
1808 static void activate_control_endpoints(struct udc
*dev
)
1812 DBG(dev
, "activate_control_endpoints\n");
1815 tmp
= readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
1816 tmp
|= AMD_BIT(UDC_EPCTL_F
);
1817 writel(tmp
, &dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
1819 /* set ep0 directions */
1820 dev
->ep
[UDC_EP0IN_IX
].in
= 1;
1821 dev
->ep
[UDC_EP0OUT_IX
].in
= 0;
1823 /* set buffer size (tx fifo entries) of EP0_IN */
1824 tmp
= readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->bufin_framenum
);
1825 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1826 tmp
= AMD_ADDBITS(tmp
, UDC_FS_EPIN0_BUFF_SIZE
,
1827 UDC_EPIN_BUFF_SIZE
);
1828 else if (dev
->gadget
.speed
== USB_SPEED_HIGH
)
1829 tmp
= AMD_ADDBITS(tmp
, UDC_EPIN0_BUFF_SIZE
,
1830 UDC_EPIN_BUFF_SIZE
);
1831 writel(tmp
, &dev
->ep
[UDC_EP0IN_IX
].regs
->bufin_framenum
);
1833 /* set max packet size of EP0_IN */
1834 tmp
= readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->bufout_maxpkt
);
1835 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1836 tmp
= AMD_ADDBITS(tmp
, UDC_FS_EP0IN_MAX_PKT_SIZE
,
1837 UDC_EP_MAX_PKT_SIZE
);
1838 else if (dev
->gadget
.speed
== USB_SPEED_HIGH
)
1839 tmp
= AMD_ADDBITS(tmp
, UDC_EP0IN_MAX_PKT_SIZE
,
1840 UDC_EP_MAX_PKT_SIZE
);
1841 writel(tmp
, &dev
->ep
[UDC_EP0IN_IX
].regs
->bufout_maxpkt
);
1843 /* set max packet size of EP0_OUT */
1844 tmp
= readl(&dev
->ep
[UDC_EP0OUT_IX
].regs
->bufout_maxpkt
);
1845 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1846 tmp
= AMD_ADDBITS(tmp
, UDC_FS_EP0OUT_MAX_PKT_SIZE
,
1847 UDC_EP_MAX_PKT_SIZE
);
1848 else if (dev
->gadget
.speed
== USB_SPEED_HIGH
)
1849 tmp
= AMD_ADDBITS(tmp
, UDC_EP0OUT_MAX_PKT_SIZE
,
1850 UDC_EP_MAX_PKT_SIZE
);
1851 writel(tmp
, &dev
->ep
[UDC_EP0OUT_IX
].regs
->bufout_maxpkt
);
1853 /* set max packet size of EP0 in UDC CSR */
1854 tmp
= readl(&dev
->csr
->ne
[0]);
1855 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1856 tmp
= AMD_ADDBITS(tmp
, UDC_FS_EP0OUT_MAX_PKT_SIZE
,
1857 UDC_CSR_NE_MAX_PKT
);
1858 else if (dev
->gadget
.speed
== USB_SPEED_HIGH
)
1859 tmp
= AMD_ADDBITS(tmp
, UDC_EP0OUT_MAX_PKT_SIZE
,
1860 UDC_CSR_NE_MAX_PKT
);
1861 writel(tmp
, &dev
->csr
->ne
[0]);
1864 dev
->ep
[UDC_EP0OUT_IX
].td
->status
|=
1865 AMD_BIT(UDC_DMA_OUT_STS_L
);
1866 /* write dma desc address */
1867 writel(dev
->ep
[UDC_EP0OUT_IX
].td_stp_dma
,
1868 &dev
->ep
[UDC_EP0OUT_IX
].regs
->subptr
);
1869 writel(dev
->ep
[UDC_EP0OUT_IX
].td_phys
,
1870 &dev
->ep
[UDC_EP0OUT_IX
].regs
->desptr
);
1871 /* stop RDE timer */
1872 if (timer_pending(&udc_timer
)) {
1874 mod_timer(&udc_timer
, jiffies
- 1);
1876 /* stop pollstall timer */
1877 if (timer_pending(&udc_pollstall_timer
))
1878 mod_timer(&udc_pollstall_timer
, jiffies
- 1);
1880 tmp
= readl(&dev
->regs
->ctl
);
1881 tmp
|= AMD_BIT(UDC_DEVCTL_MODE
)
1882 | AMD_BIT(UDC_DEVCTL_RDE
)
1883 | AMD_BIT(UDC_DEVCTL_TDE
);
1884 if (use_dma_bufferfill_mode
)
1885 tmp
|= AMD_BIT(UDC_DEVCTL_BF
);
1886 else if (use_dma_ppb_du
)
1887 tmp
|= AMD_BIT(UDC_DEVCTL_DU
);
1888 writel(tmp
, &dev
->regs
->ctl
);
1891 /* clear NAK by writing CNAK for EP0IN */
1892 tmp
= readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
1893 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
1894 writel(tmp
, &dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
1895 dev
->ep
[UDC_EP0IN_IX
].naking
= 0;
1896 UDC_QUEUE_CNAK(&dev
->ep
[UDC_EP0IN_IX
], UDC_EP0IN_IX
);
1898 /* clear NAK by writing CNAK for EP0OUT */
1899 tmp
= readl(&dev
->ep
[UDC_EP0OUT_IX
].regs
->ctl
);
1900 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
1901 writel(tmp
, &dev
->ep
[UDC_EP0OUT_IX
].regs
->ctl
);
1902 dev
->ep
[UDC_EP0OUT_IX
].naking
= 0;
1903 UDC_QUEUE_CNAK(&dev
->ep
[UDC_EP0OUT_IX
], UDC_EP0OUT_IX
);
1906 /* Make endpoint 0 ready for control traffic */
1907 static int setup_ep0(struct udc
*dev
)
1909 activate_control_endpoints(dev
);
1910 /* enable ep0 interrupts */
1911 udc_enable_ep0_interrupts(dev
);
1912 /* enable device setup interrupts */
1913 udc_enable_dev_setup_interrupts(dev
);
1918 /* Called by gadget driver to register itself */
1919 static int amd5536_udc_start(struct usb_gadget
*g
,
1920 struct usb_gadget_driver
*driver
)
1922 struct udc
*dev
= to_amd5536_udc(g
);
1925 driver
->driver
.bus
= NULL
;
1926 dev
->driver
= driver
;
1928 /* Some gadget drivers use both ep0 directions.
1929 * NOTE: to gadget driver, ep0 is just one endpoint...
1931 dev
->ep
[UDC_EP0OUT_IX
].ep
.driver_data
=
1932 dev
->ep
[UDC_EP0IN_IX
].ep
.driver_data
;
1934 /* get ready for ep0 traffic */
1938 tmp
= readl(&dev
->regs
->ctl
);
1939 tmp
= tmp
& AMD_CLEAR_BIT(UDC_DEVCTL_SD
);
1940 writel(tmp
, &dev
->regs
->ctl
);
1947 /* shutdown requests and disconnect from gadget */
1949 shutdown(struct udc
*dev
, struct usb_gadget_driver
*driver
)
1950 __releases(dev
->lock
)
1951 __acquires(dev
->lock
)
1955 /* empty queues and init hardware */
1956 udc_basic_init(dev
);
1958 for (tmp
= 0; tmp
< UDC_EP_NUM
; tmp
++)
1959 empty_req_queue(&dev
->ep
[tmp
]);
1961 udc_setup_endpoints(dev
);
1964 /* Called by gadget driver to unregister itself */
1965 static int amd5536_udc_stop(struct usb_gadget
*g
,
1966 struct usb_gadget_driver
*driver
)
1968 struct udc
*dev
= to_amd5536_udc(g
);
1969 unsigned long flags
;
1972 spin_lock_irqsave(&dev
->lock
, flags
);
1973 udc_mask_unused_interrupts(dev
);
1974 shutdown(dev
, driver
);
1975 spin_unlock_irqrestore(&dev
->lock
, flags
);
1980 tmp
= readl(&dev
->regs
->ctl
);
1981 tmp
|= AMD_BIT(UDC_DEVCTL_SD
);
1982 writel(tmp
, &dev
->regs
->ctl
);
1987 /* Clear pending NAK bits */
1988 static void udc_process_cnak_queue(struct udc
*dev
)
1994 DBG(dev
, "CNAK pending queue processing\n");
1995 for (tmp
= 0; tmp
< UDC_EPIN_NUM_USED
; tmp
++) {
1996 if (cnak_pending
& (1 << tmp
)) {
1997 DBG(dev
, "CNAK pending for ep%d\n", tmp
);
1998 /* clear NAK by writing CNAK */
1999 reg
= readl(&dev
->ep
[tmp
].regs
->ctl
);
2000 reg
|= AMD_BIT(UDC_EPCTL_CNAK
);
2001 writel(reg
, &dev
->ep
[tmp
].regs
->ctl
);
2002 dev
->ep
[tmp
].naking
= 0;
2003 UDC_QUEUE_CNAK(&dev
->ep
[tmp
], dev
->ep
[tmp
].num
);
2006 /* ... and ep0out */
2007 if (cnak_pending
& (1 << UDC_EP0OUT_IX
)) {
2008 DBG(dev
, "CNAK pending for ep%d\n", UDC_EP0OUT_IX
);
2009 /* clear NAK by writing CNAK */
2010 reg
= readl(&dev
->ep
[UDC_EP0OUT_IX
].regs
->ctl
);
2011 reg
|= AMD_BIT(UDC_EPCTL_CNAK
);
2012 writel(reg
, &dev
->ep
[UDC_EP0OUT_IX
].regs
->ctl
);
2013 dev
->ep
[UDC_EP0OUT_IX
].naking
= 0;
2014 UDC_QUEUE_CNAK(&dev
->ep
[UDC_EP0OUT_IX
],
2015 dev
->ep
[UDC_EP0OUT_IX
].num
);
2019 /* Enabling RX DMA after setup packet */
2020 static void udc_ep0_set_rde(struct udc
*dev
)
2024 * only enable RXDMA when no data endpoint enabled
2027 if (!dev
->data_ep_enabled
|| dev
->data_ep_queued
) {
2031 * setup timer for enabling RDE (to not enable
2032 * RXFIFO DMA for data endpoints to early)
2034 if (set_rde
!= 0 && !timer_pending(&udc_timer
)) {
2036 jiffies
+ HZ
/UDC_RDE_TIMER_DIV
;
2039 add_timer(&udc_timer
);
2046 /* Interrupt handler for data OUT traffic */
2047 static irqreturn_t
udc_data_out_isr(struct udc
*dev
, int ep_ix
)
2049 irqreturn_t ret_val
= IRQ_NONE
;
2052 struct udc_request
*req
;
2054 struct udc_data_dma
*td
= NULL
;
2057 VDBG(dev
, "ep%d irq\n", ep_ix
);
2058 ep
= &dev
->ep
[ep_ix
];
2060 tmp
= readl(&ep
->regs
->sts
);
2063 if (tmp
& AMD_BIT(UDC_EPSTS_BNA
)) {
2064 DBG(dev
, "BNA ep%dout occurred - DESPTR = %x\n",
2065 ep
->num
, readl(&ep
->regs
->desptr
));
2067 writel(tmp
| AMD_BIT(UDC_EPSTS_BNA
), &ep
->regs
->sts
);
2068 if (!ep
->cancel_transfer
)
2069 ep
->bna_occurred
= 1;
2071 ep
->cancel_transfer
= 0;
2072 ret_val
= IRQ_HANDLED
;
2077 if (tmp
& AMD_BIT(UDC_EPSTS_HE
)) {
2078 dev_err(&dev
->pdev
->dev
, "HE ep%dout occurred\n", ep
->num
);
2081 writel(tmp
| AMD_BIT(UDC_EPSTS_HE
), &ep
->regs
->sts
);
2082 ret_val
= IRQ_HANDLED
;
2086 if (!list_empty(&ep
->queue
)) {
2089 req
= list_entry(ep
->queue
.next
,
2090 struct udc_request
, queue
);
2093 udc_rxfifo_pending
= 1;
2095 VDBG(dev
, "req = %p\n", req
);
2100 if (req
&& udc_rxfifo_read(ep
, req
)) {
2101 ret_val
= IRQ_HANDLED
;
2104 complete_req(ep
, req
, 0);
2106 if (!list_empty(&ep
->queue
) && !ep
->halted
) {
2107 req
= list_entry(ep
->queue
.next
,
2108 struct udc_request
, queue
);
2114 } else if (!ep
->cancel_transfer
&& req
!= NULL
) {
2115 ret_val
= IRQ_HANDLED
;
2117 /* check for DMA done */
2119 dma_done
= AMD_GETBITS(req
->td_data
->status
,
2120 UDC_DMA_OUT_STS_BS
);
2121 /* packet per buffer mode - rx bytes */
2124 * if BNA occurred then recover desc. from
2127 if (ep
->bna_occurred
) {
2128 VDBG(dev
, "Recover desc. from BNA dummy\n");
2129 memcpy(req
->td_data
, ep
->bna_dummy_req
->td_data
,
2130 sizeof(struct udc_data_dma
));
2131 ep
->bna_occurred
= 0;
2132 udc_init_bna_dummy(ep
->req
);
2134 td
= udc_get_last_dma_desc(req
);
2135 dma_done
= AMD_GETBITS(td
->status
, UDC_DMA_OUT_STS_BS
);
2137 if (dma_done
== UDC_DMA_OUT_STS_BS_DMA_DONE
) {
2138 /* buffer fill mode - rx bytes */
2140 /* received number bytes */
2141 count
= AMD_GETBITS(req
->td_data
->status
,
2142 UDC_DMA_OUT_STS_RXBYTES
);
2143 VDBG(dev
, "rx bytes=%u\n", count
);
2144 /* packet per buffer mode - rx bytes */
2146 VDBG(dev
, "req->td_data=%p\n", req
->td_data
);
2147 VDBG(dev
, "last desc = %p\n", td
);
2148 /* received number bytes */
2149 if (use_dma_ppb_du
) {
2150 /* every desc. counts bytes */
2151 count
= udc_get_ppbdu_rxbytes(req
);
2153 /* last desc. counts bytes */
2154 count
= AMD_GETBITS(td
->status
,
2155 UDC_DMA_OUT_STS_RXBYTES
);
2156 if (!count
&& req
->req
.length
2157 == UDC_DMA_MAXPACKET
) {
2159 * on 64k packets the RXBYTES
2162 count
= UDC_DMA_MAXPACKET
;
2165 VDBG(dev
, "last desc rx bytes=%u\n", count
);
2168 tmp
= req
->req
.length
- req
->req
.actual
;
2170 if ((tmp
% ep
->ep
.maxpacket
) != 0) {
2171 DBG(dev
, "%s: rx %db, space=%db\n",
2172 ep
->ep
.name
, count
, tmp
);
2173 req
->req
.status
= -EOVERFLOW
;
2177 req
->req
.actual
+= count
;
2179 /* complete request */
2180 complete_req(ep
, req
, 0);
2183 if (!list_empty(&ep
->queue
) && !ep
->halted
) {
2184 req
= list_entry(ep
->queue
.next
,
2188 * DMA may be already started by udc_queue()
2189 * called by gadget drivers completion
2190 * routine. This happens when queue
2191 * holds one request only.
2193 if (req
->dma_going
== 0) {
2195 if (prep_dma(ep
, req
, GFP_ATOMIC
) != 0)
2197 /* write desc pointer */
2198 writel(req
->td_phys
,
2206 * implant BNA dummy descriptor to allow
2207 * RXFIFO opening by RDE
2209 if (ep
->bna_dummy_req
) {
2210 /* write desc pointer */
2211 writel(ep
->bna_dummy_req
->td_phys
,
2213 ep
->bna_occurred
= 0;
2217 * schedule timer for setting RDE if queue
2218 * remains empty to allow ep0 packets pass
2222 && !timer_pending(&udc_timer
)) {
2225 + HZ
*UDC_RDE_TIMER_SECONDS
;
2228 add_timer(&udc_timer
);
2230 if (ep
->num
!= UDC_EP0OUT_IX
)
2231 dev
->data_ep_queued
= 0;
2236 * RX DMA must be reenabled for each desc in PPBDU mode
2237 * and must be enabled for PPBNDU mode in case of BNA
2242 } else if (ep
->cancel_transfer
) {
2243 ret_val
= IRQ_HANDLED
;
2244 ep
->cancel_transfer
= 0;
2247 /* check pending CNAKS */
2249 /* CNAk processing when rxfifo empty only */
2250 if (readl(&dev
->regs
->sts
) & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY
))
2251 udc_process_cnak_queue(dev
);
2254 /* clear OUT bits in ep status */
2255 writel(UDC_EPSTS_OUT_CLEAR
, &ep
->regs
->sts
);
2260 /* Interrupt handler for data IN traffic */
2261 static irqreturn_t
udc_data_in_isr(struct udc
*dev
, int ep_ix
)
2263 irqreturn_t ret_val
= IRQ_NONE
;
2267 struct udc_request
*req
;
2268 struct udc_data_dma
*td
;
2272 ep
= &dev
->ep
[ep_ix
];
2274 epsts
= readl(&ep
->regs
->sts
);
2277 if (epsts
& AMD_BIT(UDC_EPSTS_BNA
)) {
2278 dev_err(&dev
->pdev
->dev
,
2279 "BNA ep%din occurred - DESPTR = %08lx\n",
2281 (unsigned long) readl(&ep
->regs
->desptr
));
2284 writel(epsts
, &ep
->regs
->sts
);
2285 ret_val
= IRQ_HANDLED
;
2290 if (epsts
& AMD_BIT(UDC_EPSTS_HE
)) {
2291 dev_err(&dev
->pdev
->dev
,
2292 "HE ep%dn occurred - DESPTR = %08lx\n",
2293 ep
->num
, (unsigned long) readl(&ep
->regs
->desptr
));
2296 writel(epsts
| AMD_BIT(UDC_EPSTS_HE
), &ep
->regs
->sts
);
2297 ret_val
= IRQ_HANDLED
;
2301 /* DMA completion */
2302 if (epsts
& AMD_BIT(UDC_EPSTS_TDC
)) {
2303 VDBG(dev
, "TDC set- completion\n");
2304 ret_val
= IRQ_HANDLED
;
2305 if (!ep
->cancel_transfer
&& !list_empty(&ep
->queue
)) {
2306 req
= list_entry(ep
->queue
.next
,
2307 struct udc_request
, queue
);
2309 * length bytes transferred
2310 * check dma done of last desc. in PPBDU mode
2312 if (use_dma_ppb_du
) {
2313 td
= udc_get_last_dma_desc(req
);
2316 AMD_GETBITS(td
->status
,
2318 /* don't care DMA done */
2319 req
->req
.actual
= req
->req
.length
;
2322 /* assume all bytes transferred */
2323 req
->req
.actual
= req
->req
.length
;
2326 if (req
->req
.actual
== req
->req
.length
) {
2328 complete_req(ep
, req
, 0);
2330 /* further request available ? */
2331 if (list_empty(&ep
->queue
)) {
2332 /* disable interrupt */
2333 tmp
= readl(&dev
->regs
->ep_irqmsk
);
2334 tmp
|= AMD_BIT(ep
->num
);
2335 writel(tmp
, &dev
->regs
->ep_irqmsk
);
2339 ep
->cancel_transfer
= 0;
2343 * status reg has IN bit set and TDC not set (if TDC was handled,
2344 * IN must not be handled (UDC defect) ?
2346 if ((epsts
& AMD_BIT(UDC_EPSTS_IN
))
2347 && !(epsts
& AMD_BIT(UDC_EPSTS_TDC
))) {
2348 ret_val
= IRQ_HANDLED
;
2349 if (!list_empty(&ep
->queue
)) {
2351 req
= list_entry(ep
->queue
.next
,
2352 struct udc_request
, queue
);
2356 udc_txfifo_write(ep
, &req
->req
);
2357 len
= req
->req
.length
- req
->req
.actual
;
2358 if (len
> ep
->ep
.maxpacket
)
2359 len
= ep
->ep
.maxpacket
;
2360 req
->req
.actual
+= len
;
2361 if (req
->req
.actual
== req
->req
.length
2362 || (len
!= ep
->ep
.maxpacket
)) {
2364 complete_req(ep
, req
, 0);
2367 } else if (req
&& !req
->dma_going
) {
2368 VDBG(dev
, "IN DMA : req=%p req->td_data=%p\n",
2375 * unset L bit of first desc.
2378 if (use_dma_ppb
&& req
->req
.length
>
2380 req
->td_data
->status
&=
2385 /* write desc pointer */
2386 writel(req
->td_phys
, &ep
->regs
->desptr
);
2388 /* set HOST READY */
2389 req
->td_data
->status
=
2391 req
->td_data
->status
,
2392 UDC_DMA_IN_STS_BS_HOST_READY
,
2395 /* set poll demand bit */
2396 tmp
= readl(&ep
->regs
->ctl
);
2397 tmp
|= AMD_BIT(UDC_EPCTL_P
);
2398 writel(tmp
, &ep
->regs
->ctl
);
2402 } else if (!use_dma
&& ep
->in
) {
2403 /* disable interrupt */
2405 &dev
->regs
->ep_irqmsk
);
2406 tmp
|= AMD_BIT(ep
->num
);
2408 &dev
->regs
->ep_irqmsk
);
2411 /* clear status bits */
2412 writel(epsts
, &ep
->regs
->sts
);
2419 /* Interrupt handler for Control OUT traffic */
2420 static irqreturn_t
udc_control_out_isr(struct udc
*dev
)
2421 __releases(dev
->lock
)
2422 __acquires(dev
->lock
)
2424 irqreturn_t ret_val
= IRQ_NONE
;
2426 int setup_supported
;
2430 struct udc_ep
*ep_tmp
;
2432 ep
= &dev
->ep
[UDC_EP0OUT_IX
];
2435 writel(AMD_BIT(UDC_EPINT_OUT_EP0
), &dev
->regs
->ep_irqsts
);
2437 tmp
= readl(&dev
->ep
[UDC_EP0OUT_IX
].regs
->sts
);
2438 /* check BNA and clear if set */
2439 if (tmp
& AMD_BIT(UDC_EPSTS_BNA
)) {
2440 VDBG(dev
, "ep0: BNA set\n");
2441 writel(AMD_BIT(UDC_EPSTS_BNA
),
2442 &dev
->ep
[UDC_EP0OUT_IX
].regs
->sts
);
2443 ep
->bna_occurred
= 1;
2444 ret_val
= IRQ_HANDLED
;
2448 /* type of data: SETUP or DATA 0 bytes */
2449 tmp
= AMD_GETBITS(tmp
, UDC_EPSTS_OUT
);
2450 VDBG(dev
, "data_typ = %x\n", tmp
);
2453 if (tmp
== UDC_EPSTS_OUT_SETUP
) {
2454 ret_val
= IRQ_HANDLED
;
2456 ep
->dev
->stall_ep0in
= 0;
2457 dev
->waiting_zlp_ack_ep0in
= 0;
2459 /* set NAK for EP0_IN */
2460 tmp
= readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
2461 tmp
|= AMD_BIT(UDC_EPCTL_SNAK
);
2462 writel(tmp
, &dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
2463 dev
->ep
[UDC_EP0IN_IX
].naking
= 1;
2464 /* get setup data */
2467 /* clear OUT bits in ep status */
2468 writel(UDC_EPSTS_OUT_CLEAR
,
2469 &dev
->ep
[UDC_EP0OUT_IX
].regs
->sts
);
2471 setup_data
.data
[0] =
2472 dev
->ep
[UDC_EP0OUT_IX
].td_stp
->data12
;
2473 setup_data
.data
[1] =
2474 dev
->ep
[UDC_EP0OUT_IX
].td_stp
->data34
;
2475 /* set HOST READY */
2476 dev
->ep
[UDC_EP0OUT_IX
].td_stp
->status
=
2477 UDC_DMA_STP_STS_BS_HOST_READY
;
2480 udc_rxfifo_read_dwords(dev
, setup_data
.data
, 2);
2483 /* determine direction of control data */
2484 if ((setup_data
.request
.bRequestType
& USB_DIR_IN
) != 0) {
2485 dev
->gadget
.ep0
= &dev
->ep
[UDC_EP0IN_IX
].ep
;
2487 udc_ep0_set_rde(dev
);
2490 dev
->gadget
.ep0
= &dev
->ep
[UDC_EP0OUT_IX
].ep
;
2492 * implant BNA dummy descriptor to allow RXFIFO opening
2495 if (ep
->bna_dummy_req
) {
2496 /* write desc pointer */
2497 writel(ep
->bna_dummy_req
->td_phys
,
2498 &dev
->ep
[UDC_EP0OUT_IX
].regs
->desptr
);
2499 ep
->bna_occurred
= 0;
2503 dev
->ep
[UDC_EP0OUT_IX
].naking
= 1;
2505 * setup timer for enabling RDE (to not enable
2506 * RXFIFO DMA for data to early)
2509 if (!timer_pending(&udc_timer
)) {
2510 udc_timer
.expires
= jiffies
+
2511 HZ
/UDC_RDE_TIMER_DIV
;
2513 add_timer(&udc_timer
);
2518 * mass storage reset must be processed here because
2519 * next packet may be a CLEAR_FEATURE HALT which would not
2520 * clear the stall bit when no STALL handshake was received
2521 * before (autostall can cause this)
2523 if (setup_data
.data
[0] == UDC_MSCRES_DWORD0
2524 && setup_data
.data
[1] == UDC_MSCRES_DWORD1
) {
2525 DBG(dev
, "MSC Reset\n");
2528 * only one IN and OUT endpoints are handled
2530 ep_tmp
= &udc
->ep
[UDC_EPIN_IX
];
2531 udc_set_halt(&ep_tmp
->ep
, 0);
2532 ep_tmp
= &udc
->ep
[UDC_EPOUT_IX
];
2533 udc_set_halt(&ep_tmp
->ep
, 0);
2536 /* call gadget with setup data received */
2537 spin_unlock(&dev
->lock
);
2538 setup_supported
= dev
->driver
->setup(&dev
->gadget
,
2539 &setup_data
.request
);
2540 spin_lock(&dev
->lock
);
2542 tmp
= readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
2543 /* ep0 in returns data (not zlp) on IN phase */
2544 if (setup_supported
>= 0 && setup_supported
<
2545 UDC_EP0IN_MAXPACKET
) {
2546 /* clear NAK by writing CNAK in EP0_IN */
2547 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
2548 writel(tmp
, &dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
2549 dev
->ep
[UDC_EP0IN_IX
].naking
= 0;
2550 UDC_QUEUE_CNAK(&dev
->ep
[UDC_EP0IN_IX
], UDC_EP0IN_IX
);
2552 /* if unsupported request then stall */
2553 } else if (setup_supported
< 0) {
2554 tmp
|= AMD_BIT(UDC_EPCTL_S
);
2555 writel(tmp
, &dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
2557 dev
->waiting_zlp_ack_ep0in
= 1;
2560 /* clear NAK by writing CNAK in EP0_OUT */
2562 tmp
= readl(&dev
->ep
[UDC_EP0OUT_IX
].regs
->ctl
);
2563 tmp
|= AMD_BIT(UDC_EPCTL_CNAK
);
2564 writel(tmp
, &dev
->ep
[UDC_EP0OUT_IX
].regs
->ctl
);
2565 dev
->ep
[UDC_EP0OUT_IX
].naking
= 0;
2566 UDC_QUEUE_CNAK(&dev
->ep
[UDC_EP0OUT_IX
], UDC_EP0OUT_IX
);
2570 /* clear OUT bits in ep status */
2571 writel(UDC_EPSTS_OUT_CLEAR
,
2572 &dev
->ep
[UDC_EP0OUT_IX
].regs
->sts
);
2575 /* data packet 0 bytes */
2576 } else if (tmp
== UDC_EPSTS_OUT_DATA
) {
2577 /* clear OUT bits in ep status */
2578 writel(UDC_EPSTS_OUT_CLEAR
, &dev
->ep
[UDC_EP0OUT_IX
].regs
->sts
);
2580 /* get setup data: only 0 packet */
2582 /* no req if 0 packet, just reactivate */
2583 if (list_empty(&dev
->ep
[UDC_EP0OUT_IX
].queue
)) {
2586 /* set HOST READY */
2587 dev
->ep
[UDC_EP0OUT_IX
].td
->status
=
2589 dev
->ep
[UDC_EP0OUT_IX
].td
->status
,
2590 UDC_DMA_OUT_STS_BS_HOST_READY
,
2591 UDC_DMA_OUT_STS_BS
);
2593 udc_ep0_set_rde(dev
);
2594 ret_val
= IRQ_HANDLED
;
2598 ret_val
|= udc_data_out_isr(dev
, UDC_EP0OUT_IX
);
2599 /* re-program desc. pointer for possible ZLPs */
2600 writel(dev
->ep
[UDC_EP0OUT_IX
].td_phys
,
2601 &dev
->ep
[UDC_EP0OUT_IX
].regs
->desptr
);
2603 udc_ep0_set_rde(dev
);
2607 /* received number bytes */
2608 count
= readl(&dev
->ep
[UDC_EP0OUT_IX
].regs
->sts
);
2609 count
= AMD_GETBITS(count
, UDC_EPSTS_RX_PKT_SIZE
);
2610 /* out data for fifo mode not working */
2613 /* 0 packet or real data ? */
2615 ret_val
|= udc_data_out_isr(dev
, UDC_EP0OUT_IX
);
2617 /* dummy read confirm */
2618 readl(&dev
->ep
[UDC_EP0OUT_IX
].regs
->confirm
);
2619 ret_val
= IRQ_HANDLED
;
2624 /* check pending CNAKS */
2626 /* CNAk processing when rxfifo empty only */
2627 if (readl(&dev
->regs
->sts
) & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY
))
2628 udc_process_cnak_queue(dev
);
2635 /* Interrupt handler for Control IN traffic */
2636 static irqreturn_t
udc_control_in_isr(struct udc
*dev
)
2638 irqreturn_t ret_val
= IRQ_NONE
;
2641 struct udc_request
*req
;
2644 ep
= &dev
->ep
[UDC_EP0IN_IX
];
2647 writel(AMD_BIT(UDC_EPINT_IN_EP0
), &dev
->regs
->ep_irqsts
);
2649 tmp
= readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->sts
);
2650 /* DMA completion */
2651 if (tmp
& AMD_BIT(UDC_EPSTS_TDC
)) {
2652 VDBG(dev
, "isr: TDC clear\n");
2653 ret_val
= IRQ_HANDLED
;
2656 writel(AMD_BIT(UDC_EPSTS_TDC
),
2657 &dev
->ep
[UDC_EP0IN_IX
].regs
->sts
);
2659 /* status reg has IN bit set ? */
2660 } else if (tmp
& AMD_BIT(UDC_EPSTS_IN
)) {
2661 ret_val
= IRQ_HANDLED
;
2665 writel(AMD_BIT(UDC_EPSTS_IN
),
2666 &dev
->ep
[UDC_EP0IN_IX
].regs
->sts
);
2668 if (dev
->stall_ep0in
) {
2669 DBG(dev
, "stall ep0in\n");
2671 tmp
= readl(&ep
->regs
->ctl
);
2672 tmp
|= AMD_BIT(UDC_EPCTL_S
);
2673 writel(tmp
, &ep
->regs
->ctl
);
2675 if (!list_empty(&ep
->queue
)) {
2677 req
= list_entry(ep
->queue
.next
,
2678 struct udc_request
, queue
);
2681 /* write desc pointer */
2682 writel(req
->td_phys
, &ep
->regs
->desptr
);
2683 /* set HOST READY */
2684 req
->td_data
->status
=
2686 req
->td_data
->status
,
2687 UDC_DMA_STP_STS_BS_HOST_READY
,
2688 UDC_DMA_STP_STS_BS
);
2690 /* set poll demand bit */
2692 readl(&dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
2693 tmp
|= AMD_BIT(UDC_EPCTL_P
);
2695 &dev
->ep
[UDC_EP0IN_IX
].regs
->ctl
);
2697 /* all bytes will be transferred */
2698 req
->req
.actual
= req
->req
.length
;
2701 complete_req(ep
, req
, 0);
2705 udc_txfifo_write(ep
, &req
->req
);
2707 /* lengh bytes transferred */
2708 len
= req
->req
.length
- req
->req
.actual
;
2709 if (len
> ep
->ep
.maxpacket
)
2710 len
= ep
->ep
.maxpacket
;
2712 req
->req
.actual
+= len
;
2713 if (req
->req
.actual
== req
->req
.length
2714 || (len
!= ep
->ep
.maxpacket
)) {
2716 complete_req(ep
, req
, 0);
2723 dev
->stall_ep0in
= 0;
2726 writel(AMD_BIT(UDC_EPSTS_IN
),
2727 &dev
->ep
[UDC_EP0IN_IX
].regs
->sts
);
2735 /* Interrupt handler for global device events */
2736 static irqreturn_t
udc_dev_isr(struct udc
*dev
, u32 dev_irq
)
2737 __releases(dev
->lock
)
2738 __acquires(dev
->lock
)
2740 irqreturn_t ret_val
= IRQ_NONE
;
2747 /* SET_CONFIG irq ? */
2748 if (dev_irq
& AMD_BIT(UDC_DEVINT_SC
)) {
2749 ret_val
= IRQ_HANDLED
;
2751 /* read config value */
2752 tmp
= readl(&dev
->regs
->sts
);
2753 cfg
= AMD_GETBITS(tmp
, UDC_DEVSTS_CFG
);
2754 DBG(dev
, "SET_CONFIG interrupt: config=%d\n", cfg
);
2755 dev
->cur_config
= cfg
;
2756 dev
->set_cfg_not_acked
= 1;
2758 /* make usb request for gadget driver */
2759 memset(&setup_data
, 0 , sizeof(union udc_setup_data
));
2760 setup_data
.request
.bRequest
= USB_REQ_SET_CONFIGURATION
;
2761 setup_data
.request
.wValue
= cpu_to_le16(dev
->cur_config
);
2763 /* programm the NE registers */
2764 for (i
= 0; i
< UDC_EP_NUM
; i
++) {
2768 /* ep ix in UDC CSR register space */
2769 udc_csr_epix
= ep
->num
;
2774 /* ep ix in UDC CSR register space */
2775 udc_csr_epix
= ep
->num
- UDC_CSR_EP_OUT_IX_OFS
;
2778 tmp
= readl(&dev
->csr
->ne
[udc_csr_epix
]);
2780 tmp
= AMD_ADDBITS(tmp
, ep
->dev
->cur_config
,
2783 writel(tmp
, &dev
->csr
->ne
[udc_csr_epix
]);
2785 /* clear stall bits */
2787 tmp
= readl(&ep
->regs
->ctl
);
2788 tmp
= tmp
& AMD_CLEAR_BIT(UDC_EPCTL_S
);
2789 writel(tmp
, &ep
->regs
->ctl
);
2791 /* call gadget zero with setup data received */
2792 spin_unlock(&dev
->lock
);
2793 tmp
= dev
->driver
->setup(&dev
->gadget
, &setup_data
.request
);
2794 spin_lock(&dev
->lock
);
2796 } /* SET_INTERFACE ? */
2797 if (dev_irq
& AMD_BIT(UDC_DEVINT_SI
)) {
2798 ret_val
= IRQ_HANDLED
;
2800 dev
->set_cfg_not_acked
= 1;
2801 /* read interface and alt setting values */
2802 tmp
= readl(&dev
->regs
->sts
);
2803 dev
->cur_alt
= AMD_GETBITS(tmp
, UDC_DEVSTS_ALT
);
2804 dev
->cur_intf
= AMD_GETBITS(tmp
, UDC_DEVSTS_INTF
);
2806 /* make usb request for gadget driver */
2807 memset(&setup_data
, 0 , sizeof(union udc_setup_data
));
2808 setup_data
.request
.bRequest
= USB_REQ_SET_INTERFACE
;
2809 setup_data
.request
.bRequestType
= USB_RECIP_INTERFACE
;
2810 setup_data
.request
.wValue
= cpu_to_le16(dev
->cur_alt
);
2811 setup_data
.request
.wIndex
= cpu_to_le16(dev
->cur_intf
);
2813 DBG(dev
, "SET_INTERFACE interrupt: alt=%d intf=%d\n",
2814 dev
->cur_alt
, dev
->cur_intf
);
2816 /* programm the NE registers */
2817 for (i
= 0; i
< UDC_EP_NUM
; i
++) {
2821 /* ep ix in UDC CSR register space */
2822 udc_csr_epix
= ep
->num
;
2827 /* ep ix in UDC CSR register space */
2828 udc_csr_epix
= ep
->num
- UDC_CSR_EP_OUT_IX_OFS
;
2833 tmp
= readl(&dev
->csr
->ne
[udc_csr_epix
]);
2835 tmp
= AMD_ADDBITS(tmp
, ep
->dev
->cur_intf
,
2837 /* tmp = AMD_ADDBITS(tmp, 2, UDC_CSR_NE_INTF); */
2839 tmp
= AMD_ADDBITS(tmp
, ep
->dev
->cur_alt
,
2842 writel(tmp
, &dev
->csr
->ne
[udc_csr_epix
]);
2844 /* clear stall bits */
2846 tmp
= readl(&ep
->regs
->ctl
);
2847 tmp
= tmp
& AMD_CLEAR_BIT(UDC_EPCTL_S
);
2848 writel(tmp
, &ep
->regs
->ctl
);
2851 /* call gadget zero with setup data received */
2852 spin_unlock(&dev
->lock
);
2853 tmp
= dev
->driver
->setup(&dev
->gadget
, &setup_data
.request
);
2854 spin_lock(&dev
->lock
);
2857 if (dev_irq
& AMD_BIT(UDC_DEVINT_UR
)) {
2858 DBG(dev
, "USB Reset interrupt\n");
2859 ret_val
= IRQ_HANDLED
;
2861 /* allow soft reset when suspend occurs */
2862 soft_reset_occured
= 0;
2864 dev
->waiting_zlp_ack_ep0in
= 0;
2865 dev
->set_cfg_not_acked
= 0;
2867 /* mask not needed interrupts */
2868 udc_mask_unused_interrupts(dev
);
2870 /* call gadget to resume and reset configs etc. */
2871 spin_unlock(&dev
->lock
);
2872 if (dev
->sys_suspended
&& dev
->driver
->resume
) {
2873 dev
->driver
->resume(&dev
->gadget
);
2874 dev
->sys_suspended
= 0;
2876 dev
->driver
->disconnect(&dev
->gadget
);
2877 spin_lock(&dev
->lock
);
2879 /* disable ep0 to empty req queue */
2880 empty_req_queue(&dev
->ep
[UDC_EP0IN_IX
]);
2881 ep_init(dev
->regs
, &dev
->ep
[UDC_EP0IN_IX
]);
2883 /* soft reset when rxfifo not empty */
2884 tmp
= readl(&dev
->regs
->sts
);
2885 if (!(tmp
& AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY
))
2886 && !soft_reset_after_usbreset_occured
) {
2887 udc_soft_reset(dev
);
2888 soft_reset_after_usbreset_occured
++;
2892 * DMA reset to kill potential old DMA hw hang,
2893 * POLL bit is already reset by ep_init() through
2896 DBG(dev
, "DMA machine reset\n");
2897 tmp
= readl(&dev
->regs
->cfg
);
2898 writel(tmp
| AMD_BIT(UDC_DEVCFG_DMARST
), &dev
->regs
->cfg
);
2899 writel(tmp
, &dev
->regs
->cfg
);
2901 /* put into initial config */
2902 udc_basic_init(dev
);
2904 /* enable device setup interrupts */
2905 udc_enable_dev_setup_interrupts(dev
);
2907 /* enable suspend interrupt */
2908 tmp
= readl(&dev
->regs
->irqmsk
);
2909 tmp
&= AMD_UNMASK_BIT(UDC_DEVINT_US
);
2910 writel(tmp
, &dev
->regs
->irqmsk
);
2913 if (dev_irq
& AMD_BIT(UDC_DEVINT_US
)) {
2914 DBG(dev
, "USB Suspend interrupt\n");
2915 ret_val
= IRQ_HANDLED
;
2916 if (dev
->driver
->suspend
) {
2917 spin_unlock(&dev
->lock
);
2918 dev
->sys_suspended
= 1;
2919 dev
->driver
->suspend(&dev
->gadget
);
2920 spin_lock(&dev
->lock
);
2923 if (dev_irq
& AMD_BIT(UDC_DEVINT_ENUM
)) {
2924 DBG(dev
, "ENUM interrupt\n");
2925 ret_val
= IRQ_HANDLED
;
2926 soft_reset_after_usbreset_occured
= 0;
2928 /* disable ep0 to empty req queue */
2929 empty_req_queue(&dev
->ep
[UDC_EP0IN_IX
]);
2930 ep_init(dev
->regs
, &dev
->ep
[UDC_EP0IN_IX
]);
2932 /* link up all endpoints */
2933 udc_setup_endpoints(dev
);
2934 dev_info(&dev
->pdev
->dev
, "Connect: %s\n",
2935 usb_speed_string(dev
->gadget
.speed
));
2938 activate_control_endpoints(dev
);
2940 /* enable ep0 interrupts */
2941 udc_enable_ep0_interrupts(dev
);
2943 /* session valid change interrupt */
2944 if (dev_irq
& AMD_BIT(UDC_DEVINT_SVC
)) {
2945 DBG(dev
, "USB SVC interrupt\n");
2946 ret_val
= IRQ_HANDLED
;
2948 /* check that session is not valid to detect disconnect */
2949 tmp
= readl(&dev
->regs
->sts
);
2950 if (!(tmp
& AMD_BIT(UDC_DEVSTS_SESSVLD
))) {
2951 /* disable suspend interrupt */
2952 tmp
= readl(&dev
->regs
->irqmsk
);
2953 tmp
|= AMD_BIT(UDC_DEVINT_US
);
2954 writel(tmp
, &dev
->regs
->irqmsk
);
2955 DBG(dev
, "USB Disconnect (session valid low)\n");
2956 /* cleanup on disconnect */
2957 usb_disconnect(udc
);
2965 /* Interrupt Service Routine, see Linux Kernel Doc for parameters */
2966 static irqreturn_t
udc_irq(int irq
, void *pdev
)
2968 struct udc
*dev
= pdev
;
2972 irqreturn_t ret_val
= IRQ_NONE
;
2974 spin_lock(&dev
->lock
);
2976 /* check for ep irq */
2977 reg
= readl(&dev
->regs
->ep_irqsts
);
2979 if (reg
& AMD_BIT(UDC_EPINT_OUT_EP0
))
2980 ret_val
|= udc_control_out_isr(dev
);
2981 if (reg
& AMD_BIT(UDC_EPINT_IN_EP0
))
2982 ret_val
|= udc_control_in_isr(dev
);
2988 for (i
= 1; i
< UDC_EP_NUM
; i
++) {
2990 if (!(reg
& ep_irq
) || i
== UDC_EPINT_OUT_EP0
)
2993 /* clear irq status */
2994 writel(ep_irq
, &dev
->regs
->ep_irqsts
);
2996 /* irq for out ep ? */
2997 if (i
> UDC_EPIN_NUM
)
2998 ret_val
|= udc_data_out_isr(dev
, i
);
3000 ret_val
|= udc_data_in_isr(dev
, i
);
3006 /* check for dev irq */
3007 reg
= readl(&dev
->regs
->irqsts
);
3010 writel(reg
, &dev
->regs
->irqsts
);
3011 ret_val
|= udc_dev_isr(dev
, reg
);
3015 spin_unlock(&dev
->lock
);
3019 /* Tears down device */
3020 static void gadget_release(struct device
*pdev
)
3022 struct amd5536udc
*dev
= dev_get_drvdata(pdev
);
3026 /* Cleanup on device remove */
3027 static void udc_remove(struct udc
*dev
)
3031 if (timer_pending(&udc_timer
))
3032 wait_for_completion(&on_exit
);
3034 del_timer_sync(&udc_timer
);
3035 /* remove pollstall timer */
3036 stop_pollstall_timer
++;
3037 if (timer_pending(&udc_pollstall_timer
))
3038 wait_for_completion(&on_pollstall_exit
);
3039 if (udc_pollstall_timer
.data
)
3040 del_timer_sync(&udc_pollstall_timer
);
3044 /* Reset all pci context */
3045 static void udc_pci_remove(struct pci_dev
*pdev
)
3049 dev
= pci_get_drvdata(pdev
);
3051 usb_del_gadget_udc(&udc
->gadget
);
3052 /* gadget driver must not be registered */
3053 BUG_ON(dev
->driver
!= NULL
);
3055 /* dma pool cleanup */
3056 if (dev
->data_requests
)
3057 pci_pool_destroy(dev
->data_requests
);
3059 if (dev
->stp_requests
) {
3060 /* cleanup DMA desc's for ep0in */
3061 pci_pool_free(dev
->stp_requests
,
3062 dev
->ep
[UDC_EP0OUT_IX
].td_stp
,
3063 dev
->ep
[UDC_EP0OUT_IX
].td_stp_dma
);
3064 pci_pool_free(dev
->stp_requests
,
3065 dev
->ep
[UDC_EP0OUT_IX
].td
,
3066 dev
->ep
[UDC_EP0OUT_IX
].td_phys
);
3068 pci_pool_destroy(dev
->stp_requests
);
3071 /* reset controller */
3072 writel(AMD_BIT(UDC_DEVCFG_SOFTRESET
), &dev
->regs
->cfg
);
3073 if (dev
->irq_registered
)
3074 free_irq(pdev
->irq
, dev
);
3077 if (dev
->mem_region
)
3078 release_mem_region(pci_resource_start(pdev
, 0),
3079 pci_resource_len(pdev
, 0));
3081 pci_disable_device(pdev
);
3086 /* create dma pools on init */
3087 static int init_dma_pools(struct udc
*dev
)
3089 struct udc_stp_dma
*td_stp
;
3090 struct udc_data_dma
*td_data
;
3093 /* consistent DMA mode setting ? */
3095 use_dma_bufferfill_mode
= 0;
3098 use_dma_bufferfill_mode
= 1;
3102 dev
->data_requests
= dma_pool_create("data_requests", NULL
,
3103 sizeof(struct udc_data_dma
), 0, 0);
3104 if (!dev
->data_requests
) {
3105 DBG(dev
, "can't get request data pool\n");
3110 /* EP0 in dma regs = dev control regs */
3111 dev
->ep
[UDC_EP0IN_IX
].dma
= &dev
->regs
->ctl
;
3113 /* dma desc for setup data */
3114 dev
->stp_requests
= dma_pool_create("setup requests", NULL
,
3115 sizeof(struct udc_stp_dma
), 0, 0);
3116 if (!dev
->stp_requests
) {
3117 DBG(dev
, "can't get stp request pool\n");
3122 td_stp
= dma_pool_alloc(dev
->stp_requests
, GFP_KERNEL
,
3123 &dev
->ep
[UDC_EP0OUT_IX
].td_stp_dma
);
3124 if (td_stp
== NULL
) {
3128 dev
->ep
[UDC_EP0OUT_IX
].td_stp
= td_stp
;
3130 /* data: 0 packets !? */
3131 td_data
= dma_pool_alloc(dev
->stp_requests
, GFP_KERNEL
,
3132 &dev
->ep
[UDC_EP0OUT_IX
].td_phys
);
3133 if (td_data
== NULL
) {
3137 dev
->ep
[UDC_EP0OUT_IX
].td
= td_data
;
3144 /* Called by pci bus driver to init pci context */
3145 static int udc_pci_probe(
3146 struct pci_dev
*pdev
,
3147 const struct pci_device_id
*id
3151 unsigned long resource
;
3157 dev_dbg(&pdev
->dev
, "already probed\n");
3162 dev
= kzalloc(sizeof(struct udc
), GFP_KERNEL
);
3169 if (pci_enable_device(pdev
) < 0) {
3177 /* PCI resource allocation */
3178 resource
= pci_resource_start(pdev
, 0);
3179 len
= pci_resource_len(pdev
, 0);
3181 if (!request_mem_region(resource
, len
, name
)) {
3182 dev_dbg(&pdev
->dev
, "pci device used already\n");
3188 dev
->mem_region
= 1;
3190 dev
->virt_addr
= ioremap_nocache(resource
, len
);
3191 if (dev
->virt_addr
== NULL
) {
3192 dev_dbg(&pdev
->dev
, "start address cannot be mapped\n");
3200 dev_err(&pdev
->dev
, "irq not set\n");
3207 spin_lock_init(&dev
->lock
);
3208 /* udc csr registers base */
3209 dev
->csr
= dev
->virt_addr
+ UDC_CSR_ADDR
;
3210 /* dev registers base */
3211 dev
->regs
= dev
->virt_addr
+ UDC_DEVCFG_ADDR
;
3212 /* ep registers base */
3213 dev
->ep_regs
= dev
->virt_addr
+ UDC_EPREGS_ADDR
;
3215 dev
->rxfifo
= (u32 __iomem
*)(dev
->virt_addr
+ UDC_RXFIFO_ADDR
);
3216 dev
->txfifo
= (u32 __iomem
*)(dev
->virt_addr
+ UDC_TXFIFO_ADDR
);
3218 if (request_irq(pdev
->irq
, udc_irq
, IRQF_SHARED
, name
, dev
) != 0) {
3219 dev_dbg(&pdev
->dev
, "request_irq(%d) fail\n", pdev
->irq
);
3225 dev
->irq_registered
= 1;
3227 pci_set_drvdata(pdev
, dev
);
3229 /* chip revision for Hs AMD5536 */
3230 dev
->chiprev
= pdev
->revision
;
3232 pci_set_master(pdev
);
3233 pci_try_set_mwi(pdev
);
3235 /* init dma pools */
3237 retval
= init_dma_pools(dev
);
3242 dev
->phys_addr
= resource
;
3243 dev
->irq
= pdev
->irq
;
3246 /* general probing */
3247 if (udc_probe(dev
) == 0)
3252 udc_pci_remove(pdev
);
3257 static int udc_probe(struct udc
*dev
)
3263 /* mark timer as not initialized */
3265 udc_pollstall_timer
.data
= 0;
3267 /* device struct setup */
3268 dev
->gadget
.ops
= &udc_ops
;
3270 dev_set_name(&dev
->gadget
.dev
, "gadget");
3271 dev
->gadget
.name
= name
;
3272 dev
->gadget
.max_speed
= USB_SPEED_HIGH
;
3274 /* init registers, interrupts, ... */
3275 startup_registers(dev
);
3277 dev_info(&dev
->pdev
->dev
, "%s\n", mod_desc
);
3279 snprintf(tmp
, sizeof tmp
, "%d", dev
->irq
);
3280 dev_info(&dev
->pdev
->dev
,
3281 "irq %s, pci mem %08lx, chip rev %02x(Geode5536 %s)\n",
3282 tmp
, dev
->phys_addr
, dev
->chiprev
,
3283 (dev
->chiprev
== UDC_HSA0_REV
) ? "A0" : "B1");
3284 strcpy(tmp
, UDC_DRIVER_VERSION_STRING
);
3285 if (dev
->chiprev
== UDC_HSA0_REV
) {
3286 dev_err(&dev
->pdev
->dev
, "chip revision is A0; too old\n");
3290 dev_info(&dev
->pdev
->dev
,
3291 "driver version: %s(for Geode5536 B1)\n", tmp
);
3294 retval
= usb_add_gadget_udc_release(&udc
->pdev
->dev
, &dev
->gadget
,
3300 init_timer(&udc_timer
);
3301 udc_timer
.function
= udc_timer_function
;
3303 /* timer pollstall init */
3304 init_timer(&udc_pollstall_timer
);
3305 udc_pollstall_timer
.function
= udc_pollstall_timer_function
;
3306 udc_pollstall_timer
.data
= 1;
3309 reg
= readl(&dev
->regs
->ctl
);
3310 reg
|= AMD_BIT(UDC_DEVCTL_SD
);
3311 writel(reg
, &dev
->regs
->ctl
);
3313 /* print dev register info */
3322 /* Initiates a remote wakeup */
3323 static int udc_remote_wakeup(struct udc
*dev
)
3325 unsigned long flags
;
3328 DBG(dev
, "UDC initiates remote wakeup\n");
3330 spin_lock_irqsave(&dev
->lock
, flags
);
3332 tmp
= readl(&dev
->regs
->ctl
);
3333 tmp
|= AMD_BIT(UDC_DEVCTL_RES
);
3334 writel(tmp
, &dev
->regs
->ctl
);
3335 tmp
&= AMD_CLEAR_BIT(UDC_DEVCTL_RES
);
3336 writel(tmp
, &dev
->regs
->ctl
);
3338 spin_unlock_irqrestore(&dev
->lock
, flags
);
3342 /* PCI device parameters */
3343 static const struct pci_device_id pci_id
[] = {
3345 PCI_DEVICE(PCI_VENDOR_ID_AMD
, 0x2096),
3346 .class = (PCI_CLASS_SERIAL_USB
<< 8) | 0xfe,
3347 .class_mask
= 0xffffffff,
3351 MODULE_DEVICE_TABLE(pci
, pci_id
);
3354 static struct pci_driver udc_pci_driver
= {
3355 .name
= (char *) name
,
3357 .probe
= udc_pci_probe
,
3358 .remove
= udc_pci_remove
,
3361 module_pci_driver(udc_pci_driver
);
3363 MODULE_DESCRIPTION(UDC_MOD_DESCRIPTION
);
3364 MODULE_AUTHOR("Thomas Dahlmann");
3365 MODULE_LICENSE("GPL");