2 * amd5536.h -- header for AMD 5536 UDC high/full speed USB device controller
4 * Copyright (C) 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.
17 /* #define UDC_VERBOSE */
19 #include <linux/extcon.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
23 /* various constants */
24 #define UDC_RDE_TIMER_SECONDS 1
25 #define UDC_RDE_TIMER_DIV 10
26 #define UDC_POLLSTALL_TIMER_USECONDS 500
28 /* Hs AMD5536 chip rev. */
29 #define UDC_HSA0_REV 1
30 #define UDC_HSB1_REV 2
32 /* Broadcom chip rev. */
33 #define UDC_BCM_REV 10
37 * needed, because some SETUP's are handled in hw, but must be passed to
41 #define UDC_SETCONFIG_DWORD0 0x00000900
42 #define UDC_SETCONFIG_DWORD0_VALUE_MASK 0xffff0000
43 #define UDC_SETCONFIG_DWORD0_VALUE_OFS 16
45 #define UDC_SETCONFIG_DWORD1 0x00000000
48 #define UDC_SETINTF_DWORD0 0x00000b00
49 #define UDC_SETINTF_DWORD0_ALT_MASK 0xffff0000
50 #define UDC_SETINTF_DWORD0_ALT_OFS 16
52 #define UDC_SETINTF_DWORD1 0x00000000
53 #define UDC_SETINTF_DWORD1_INTF_MASK 0x0000ffff
54 #define UDC_SETINTF_DWORD1_INTF_OFS 0
56 /* Mass storage reset */
57 #define UDC_MSCRES_DWORD0 0x0000ff21
58 #define UDC_MSCRES_DWORD1 0x00000000
60 /* Global CSR's -------------------------------------------------------------*/
61 #define UDC_CSR_ADDR 0x500
65 #define UDC_CSR_NE_NUM_MASK 0x0000000f
66 #define UDC_CSR_NE_NUM_OFS 0
68 #define UDC_CSR_NE_DIR_MASK 0x00000010
69 #define UDC_CSR_NE_DIR_OFS 4
71 #define UDC_CSR_NE_TYPE_MASK 0x00000060
72 #define UDC_CSR_NE_TYPE_OFS 5
73 /* EP config number */
74 #define UDC_CSR_NE_CFG_MASK 0x00000780
75 #define UDC_CSR_NE_CFG_OFS 7
76 /* EP interface number */
77 #define UDC_CSR_NE_INTF_MASK 0x00007800
78 #define UDC_CSR_NE_INTF_OFS 11
80 #define UDC_CSR_NE_ALT_MASK 0x00078000
81 #define UDC_CSR_NE_ALT_OFS 15
84 #define UDC_CSR_NE_MAX_PKT_MASK 0x3ff80000
85 #define UDC_CSR_NE_MAX_PKT_OFS 19
87 /* Device Config Register ---------------------------------------------------*/
88 #define UDC_DEVCFG_ADDR 0x400
90 #define UDC_DEVCFG_SOFTRESET 31
91 #define UDC_DEVCFG_HNPSFEN 30
92 #define UDC_DEVCFG_DMARST 29
93 #define UDC_DEVCFG_SET_DESC 18
94 #define UDC_DEVCFG_CSR_PRG 17
95 #define UDC_DEVCFG_STATUS 7
96 #define UDC_DEVCFG_DIR 6
97 #define UDC_DEVCFG_PI 5
98 #define UDC_DEVCFG_SS 4
99 #define UDC_DEVCFG_SP 3
100 #define UDC_DEVCFG_RWKP 2
102 #define UDC_DEVCFG_SPD_MASK 0x3
103 #define UDC_DEVCFG_SPD_OFS 0
104 #define UDC_DEVCFG_SPD_HS 0x0
105 #define UDC_DEVCFG_SPD_FS 0x1
106 #define UDC_DEVCFG_SPD_LS 0x2
107 /*#define UDC_DEVCFG_SPD_FS 0x3*/
110 /* Device Control Register --------------------------------------------------*/
111 #define UDC_DEVCTL_ADDR 0x404
113 #define UDC_DEVCTL_THLEN_MASK 0xff000000
114 #define UDC_DEVCTL_THLEN_OFS 24
116 #define UDC_DEVCTL_BRLEN_MASK 0x00ff0000
117 #define UDC_DEVCTL_BRLEN_OFS 16
119 #define UDC_DEVCTL_SRX_FLUSH 14
120 #define UDC_DEVCTL_CSR_DONE 13
121 #define UDC_DEVCTL_DEVNAK 12
122 #define UDC_DEVCTL_SD 10
123 #define UDC_DEVCTL_MODE 9
124 #define UDC_DEVCTL_BREN 8
125 #define UDC_DEVCTL_THE 7
126 #define UDC_DEVCTL_BF 6
127 #define UDC_DEVCTL_BE 5
128 #define UDC_DEVCTL_DU 4
129 #define UDC_DEVCTL_TDE 3
130 #define UDC_DEVCTL_RDE 2
131 #define UDC_DEVCTL_RES 0
134 /* Device Status Register ---------------------------------------------------*/
135 #define UDC_DEVSTS_ADDR 0x408
137 #define UDC_DEVSTS_TS_MASK 0xfffc0000
138 #define UDC_DEVSTS_TS_OFS 18
140 #define UDC_DEVSTS_SESSVLD 17
141 #define UDC_DEVSTS_PHY_ERROR 16
142 #define UDC_DEVSTS_RXFIFO_EMPTY 15
144 #define UDC_DEVSTS_ENUM_SPEED_MASK 0x00006000
145 #define UDC_DEVSTS_ENUM_SPEED_OFS 13
146 #define UDC_DEVSTS_ENUM_SPEED_FULL 1
147 #define UDC_DEVSTS_ENUM_SPEED_HIGH 0
149 #define UDC_DEVSTS_SUSP 12
151 #define UDC_DEVSTS_ALT_MASK 0x00000f00
152 #define UDC_DEVSTS_ALT_OFS 8
154 #define UDC_DEVSTS_INTF_MASK 0x000000f0
155 #define UDC_DEVSTS_INTF_OFS 4
157 #define UDC_DEVSTS_CFG_MASK 0x0000000f
158 #define UDC_DEVSTS_CFG_OFS 0
161 /* Device Interrupt Register ------------------------------------------------*/
162 #define UDC_DEVINT_ADDR 0x40c
164 #define UDC_DEVINT_SVC 7
165 #define UDC_DEVINT_ENUM 6
166 #define UDC_DEVINT_SOF 5
167 #define UDC_DEVINT_US 4
168 #define UDC_DEVINT_UR 3
169 #define UDC_DEVINT_ES 2
170 #define UDC_DEVINT_SI 1
171 #define UDC_DEVINT_SC 0
173 /* Device Interrupt Mask Register -------------------------------------------*/
174 #define UDC_DEVINT_MSK_ADDR 0x410
176 #define UDC_DEVINT_MSK 0x7f
178 /* Endpoint Interrupt Register ----------------------------------------------*/
179 #define UDC_EPINT_ADDR 0x414
181 #define UDC_EPINT_OUT_MASK 0xffff0000
182 #define UDC_EPINT_OUT_OFS 16
183 #define UDC_EPINT_IN_MASK 0x0000ffff
184 #define UDC_EPINT_IN_OFS 0
186 #define UDC_EPINT_IN_EP0 0
187 #define UDC_EPINT_IN_EP1 1
188 #define UDC_EPINT_IN_EP2 2
189 #define UDC_EPINT_IN_EP3 3
190 #define UDC_EPINT_OUT_EP0 16
191 #define UDC_EPINT_OUT_EP1 17
192 #define UDC_EPINT_OUT_EP2 18
193 #define UDC_EPINT_OUT_EP3 19
195 #define UDC_EPINT_EP0_ENABLE_MSK 0x001e001e
197 /* Endpoint Interrupt Mask Register -----------------------------------------*/
198 #define UDC_EPINT_MSK_ADDR 0x418
200 #define UDC_EPINT_OUT_MSK_MASK 0xffff0000
201 #define UDC_EPINT_OUT_MSK_OFS 16
202 #define UDC_EPINT_IN_MSK_MASK 0x0000ffff
203 #define UDC_EPINT_IN_MSK_OFS 0
205 #define UDC_EPINT_MSK_DISABLE_ALL 0xffffffff
206 /* mask non-EP0 endpoints */
207 #define UDC_EPDATAINT_MSK_DISABLE 0xfffefffe
208 /* mask all dev interrupts */
209 #define UDC_DEV_MSK_DISABLE 0x7f
211 /* Endpoint-specific CSR's --------------------------------------------------*/
212 #define UDC_EPREGS_ADDR 0x0
213 #define UDC_EPIN_REGS_ADDR 0x0
214 #define UDC_EPOUT_REGS_ADDR 0x200
216 #define UDC_EPCTL_ADDR 0x0
218 #define UDC_EPCTL_RRDY 9
219 #define UDC_EPCTL_CNAK 8
220 #define UDC_EPCTL_SNAK 7
221 #define UDC_EPCTL_NAK 6
223 #define UDC_EPCTL_ET_MASK 0x00000030
224 #define UDC_EPCTL_ET_OFS 4
225 #define UDC_EPCTL_ET_CONTROL 0
226 #define UDC_EPCTL_ET_ISO 1
227 #define UDC_EPCTL_ET_BULK 2
228 #define UDC_EPCTL_ET_INTERRUPT 3
230 #define UDC_EPCTL_P 3
231 #define UDC_EPCTL_SN 2
232 #define UDC_EPCTL_F 1
233 #define UDC_EPCTL_S 0
235 /* Endpoint Status Registers ------------------------------------------------*/
236 #define UDC_EPSTS_ADDR 0x4
238 #define UDC_EPSTS_RX_PKT_SIZE_MASK 0x007ff800
239 #define UDC_EPSTS_RX_PKT_SIZE_OFS 11
241 #define UDC_EPSTS_TDC 10
242 #define UDC_EPSTS_HE 9
243 #define UDC_EPSTS_BNA 7
244 #define UDC_EPSTS_IN 6
246 #define UDC_EPSTS_OUT_MASK 0x00000030
247 #define UDC_EPSTS_OUT_OFS 4
248 #define UDC_EPSTS_OUT_DATA 1
249 #define UDC_EPSTS_OUT_DATA_CLEAR 0x10
250 #define UDC_EPSTS_OUT_SETUP 2
251 #define UDC_EPSTS_OUT_SETUP_CLEAR 0x20
252 #define UDC_EPSTS_OUT_CLEAR 0x30
254 /* Endpoint Buffer Size IN/ Receive Packet Frame Number OUT Registers ------*/
255 #define UDC_EPIN_BUFF_SIZE_ADDR 0x8
256 #define UDC_EPOUT_FRAME_NUMBER_ADDR 0x8
258 #define UDC_EPIN_BUFF_SIZE_MASK 0x0000ffff
259 #define UDC_EPIN_BUFF_SIZE_OFS 0
260 /* EP0in txfifo = 128 bytes*/
261 #define UDC_EPIN0_BUFF_SIZE 32
262 /* EP0in fullspeed txfifo = 128 bytes*/
263 #define UDC_FS_EPIN0_BUFF_SIZE 32
265 /* fifo size mult = fifo size / max packet */
266 #define UDC_EPIN_BUFF_SIZE_MULT 2
268 /* EPin data fifo size = 1024 bytes DOUBLE BUFFERING */
269 #define UDC_EPIN_BUFF_SIZE 256
270 /* EPin small INT data fifo size = 128 bytes */
271 #define UDC_EPIN_SMALLINT_BUFF_SIZE 32
273 /* EPin fullspeed data fifo size = 128 bytes DOUBLE BUFFERING */
274 #define UDC_FS_EPIN_BUFF_SIZE 32
276 #define UDC_EPOUT_FRAME_NUMBER_MASK 0x0000ffff
277 #define UDC_EPOUT_FRAME_NUMBER_OFS 0
279 /* Endpoint Buffer Size OUT/Max Packet Size Registers -----------------------*/
280 #define UDC_EPOUT_BUFF_SIZE_ADDR 0x0c
281 #define UDC_EP_MAX_PKT_SIZE_ADDR 0x0c
283 #define UDC_EPOUT_BUFF_SIZE_MASK 0xffff0000
284 #define UDC_EPOUT_BUFF_SIZE_OFS 16
285 #define UDC_EP_MAX_PKT_SIZE_MASK 0x0000ffff
286 #define UDC_EP_MAX_PKT_SIZE_OFS 0
287 /* EP0in max packet size = 64 bytes */
288 #define UDC_EP0IN_MAX_PKT_SIZE 64
289 /* EP0out max packet size = 64 bytes */
290 #define UDC_EP0OUT_MAX_PKT_SIZE 64
291 /* EP0in fullspeed max packet size = 64 bytes */
292 #define UDC_FS_EP0IN_MAX_PKT_SIZE 64
293 /* EP0out fullspeed max packet size = 64 bytes */
294 #define UDC_FS_EP0OUT_MAX_PKT_SIZE 64
297 * Endpoint dma descriptors ------------------------------------------------
299 * Setup data, Status dword
301 #define UDC_DMA_STP_STS_CFG_MASK 0x0fff0000
302 #define UDC_DMA_STP_STS_CFG_OFS 16
303 #define UDC_DMA_STP_STS_CFG_ALT_MASK 0x000f0000
304 #define UDC_DMA_STP_STS_CFG_ALT_OFS 16
305 #define UDC_DMA_STP_STS_CFG_INTF_MASK 0x00f00000
306 #define UDC_DMA_STP_STS_CFG_INTF_OFS 20
307 #define UDC_DMA_STP_STS_CFG_NUM_MASK 0x0f000000
308 #define UDC_DMA_STP_STS_CFG_NUM_OFS 24
309 #define UDC_DMA_STP_STS_RX_MASK 0x30000000
310 #define UDC_DMA_STP_STS_RX_OFS 28
311 #define UDC_DMA_STP_STS_BS_MASK 0xc0000000
312 #define UDC_DMA_STP_STS_BS_OFS 30
313 #define UDC_DMA_STP_STS_BS_HOST_READY 0
314 #define UDC_DMA_STP_STS_BS_DMA_BUSY 1
315 #define UDC_DMA_STP_STS_BS_DMA_DONE 2
316 #define UDC_DMA_STP_STS_BS_HOST_BUSY 3
317 /* IN data, Status dword */
318 #define UDC_DMA_IN_STS_TXBYTES_MASK 0x0000ffff
319 #define UDC_DMA_IN_STS_TXBYTES_OFS 0
320 #define UDC_DMA_IN_STS_FRAMENUM_MASK 0x07ff0000
321 #define UDC_DMA_IN_STS_FRAMENUM_OFS 0
322 #define UDC_DMA_IN_STS_L 27
323 #define UDC_DMA_IN_STS_TX_MASK 0x30000000
324 #define UDC_DMA_IN_STS_TX_OFS 28
325 #define UDC_DMA_IN_STS_BS_MASK 0xc0000000
326 #define UDC_DMA_IN_STS_BS_OFS 30
327 #define UDC_DMA_IN_STS_BS_HOST_READY 0
328 #define UDC_DMA_IN_STS_BS_DMA_BUSY 1
329 #define UDC_DMA_IN_STS_BS_DMA_DONE 2
330 #define UDC_DMA_IN_STS_BS_HOST_BUSY 3
331 /* OUT data, Status dword */
332 #define UDC_DMA_OUT_STS_RXBYTES_MASK 0x0000ffff
333 #define UDC_DMA_OUT_STS_RXBYTES_OFS 0
334 #define UDC_DMA_OUT_STS_FRAMENUM_MASK 0x07ff0000
335 #define UDC_DMA_OUT_STS_FRAMENUM_OFS 0
336 #define UDC_DMA_OUT_STS_L 27
337 #define UDC_DMA_OUT_STS_RX_MASK 0x30000000
338 #define UDC_DMA_OUT_STS_RX_OFS 28
339 #define UDC_DMA_OUT_STS_BS_MASK 0xc0000000
340 #define UDC_DMA_OUT_STS_BS_OFS 30
341 #define UDC_DMA_OUT_STS_BS_HOST_READY 0
342 #define UDC_DMA_OUT_STS_BS_DMA_BUSY 1
343 #define UDC_DMA_OUT_STS_BS_DMA_DONE 2
344 #define UDC_DMA_OUT_STS_BS_HOST_BUSY 3
345 /* max ep0in packet */
346 #define UDC_EP0IN_MAXPACKET 1000
348 #define UDC_DMA_MAXPACKET 65536
350 /* un-usable DMA address */
351 #define DMA_DONT_USE (~(dma_addr_t) 0 )
353 /* other Endpoint register addresses and values-----------------------------*/
354 #define UDC_EP_SUBPTR_ADDR 0x10
355 #define UDC_EP_DESPTR_ADDR 0x14
356 #define UDC_EP_WRITE_CONFIRM_ADDR 0x1c
358 /* EP number as layouted in AHB space */
359 #define UDC_EP_NUM 32
360 #define UDC_EPIN_NUM 16
361 #define UDC_EPIN_NUM_USED 5
362 #define UDC_EPOUT_NUM 16
363 /* EP number of EP's really used = EP0 + 8 data EP's */
364 #define UDC_USED_EP_NUM 9
365 /* UDC CSR regs are aligned but AHB regs not - offset for OUT EP's */
366 #define UDC_CSR_EP_OUT_IX_OFS 12
368 #define UDC_EP0OUT_IX 16
369 #define UDC_EP0IN_IX 0
371 /* Rx fifo address and size = 1k -------------------------------------------*/
372 #define UDC_RXFIFO_ADDR 0x800
373 #define UDC_RXFIFO_SIZE 0x400
375 /* Tx fifo address and size = 1.5k -----------------------------------------*/
376 #define UDC_TXFIFO_ADDR 0xc00
377 #define UDC_TXFIFO_SIZE 0x600
379 /* default data endpoints --------------------------------------------------*/
380 #define UDC_EPIN_STATUS_IX 1
381 #define UDC_EPIN_IX 2
382 #define UDC_EPOUT_IX 18
384 /* general constants -------------------------------------------------------*/
385 #define UDC_DWORD_BYTES 4
386 #define UDC_BITS_PER_BYTE_SHIFT 3
387 #define UDC_BYTE_MASK 0xff
388 #define UDC_BITS_PER_BYTE 8
390 /*---------------------------------------------------------------------------*/
394 /* sca - setup command address */
398 u32 ne[UDC_USED_EP_NUM];
399 } __attribute__ ((packed));
401 /* AHB subsystem CSR registers */
404 /* device configuration */
413 /* device interrupt */
416 /* device interrupt mask */
419 /* endpoint interrupt */
422 /* endpoint interrupt mask */
424 } __attribute__ ((packed));
426 /* endpoint specific registers */
429 /* endpoint control */
432 /* endpoint status */
435 /* endpoint buffer size in/ receive packet frame number out */
438 /* endpoint buffer size out/max packet size */
441 /* endpoint setup buffer pointer */
444 /* endpoint data descriptor pointer */
450 /* write/read confirmation */
453 } __attribute__ ((packed));
455 /* control data DMA desc */
461 /* first setup word */
463 /* second setup word */
465 } __attribute__ ((aligned (16)));
467 /* normal data DMA desc */
468 struct udc_data_dma {
475 /* next descriptor pointer */
477 } __attribute__ ((aligned (16)));
481 /* embedded gadget ep */
482 struct usb_request req;
485 unsigned dma_going : 1,
489 /* first dma desc. of chain */
490 struct udc_data_dma *td_data;
491 /* last dma desc. of chain */
492 struct udc_data_dma *td_data_last;
493 struct list_head queue;
500 /* UDC specific endpoint parameters */
503 struct udc_ep_regs __iomem *regs;
507 dma_addr_t td_stp_dma;
508 struct udc_stp_dma *td_stp;
509 struct udc_data_dma *td;
511 struct udc_request *req;
513 unsigned req_completed;
514 /* dummy DMA desc for BNA dummy */
515 struct udc_request *bna_dummy_req;
516 unsigned bna_occurred;
523 /* queue for requests */
524 struct list_head queue;
526 unsigned cancel_transfer;
534 struct usb_gadget gadget;
535 spinlock_t lock; /* protects all state */
537 struct udc_ep ep[UDC_EP_NUM];
538 struct usb_gadget_driver *driver;
539 /* operational flags */
540 unsigned stall_ep0in : 1,
541 waiting_zlp_ack_ep0in : 1,
542 set_cfg_not_acked : 1,
551 struct pci_dev *pdev;
552 struct udc_csrs __iomem *csr;
553 struct udc_regs __iomem *regs;
554 struct udc_ep_regs __iomem *ep_regs;
559 struct dma_pool *data_requests;
560 struct dma_pool *stp_requests;
563 unsigned long phys_addr;
564 void __iomem *virt_addr;
572 /* for platform device and extcon support */
575 struct extcon_dev *edev;
576 struct extcon_specific_cable_nb extcon_nb;
577 struct notifier_block nb;
578 struct delayed_work drd_work;
579 struct workqueue_struct *drd_wq;
583 #define to_amd5536_udc(g) (container_of((g), struct udc, gadget))
585 /* setup request data */
586 union udc_setup_data {
588 struct usb_ctrlrequest request;
591 /* Function declarations */
592 int udc_enable_dev_setup_interrupts(struct udc *dev);
593 int udc_mask_unused_interrupts(struct udc *dev);
594 irqreturn_t udc_irq(int irq, void *pdev);
595 void gadget_release(struct device *pdev);
596 void empty_req_queue(struct udc_ep *ep);
597 void udc_basic_init(struct udc *dev);
598 void free_dma_pools(struct udc *dev);
599 int init_dma_pools(struct udc *dev);
600 void udc_remove(struct udc *dev);
601 int udc_probe(struct udc *dev);
604 static bool use_dma = 1;
605 /* packet per buffer dma */
606 static bool use_dma_ppb = 1;
607 /* with per descr. update */
608 static bool use_dma_ppb_du;
609 /* full speed only mode */
610 static bool use_fullspeed;
612 /* module parameters */
613 module_param(use_dma, bool, S_IRUGO);
614 MODULE_PARM_DESC(use_dma, "true for DMA");
615 module_param(use_dma_ppb, bool, S_IRUGO);
616 MODULE_PARM_DESC(use_dma_ppb, "true for DMA in packet per buffer mode");
617 module_param(use_dma_ppb_du, bool, S_IRUGO);
618 MODULE_PARM_DESC(use_dma_ppb_du,
619 "true for DMA in packet per buffer mode with descriptor update");
620 module_param(use_fullspeed, bool, S_IRUGO);
621 MODULE_PARM_DESC(use_fullspeed, "true for fullspeed only");
623 *---------------------------------------------------------------------------
624 * SET and GET bitfields in u32 values
625 * via constants for mask/offset:
626 * <bit_field_stub_name> is the text between
627 * UDC_ and _MASK|_OFS of appropriate
630 * set bitfield value in u32 u32Val
632 #define AMD_ADDBITS(u32Val, bitfield_val, bitfield_stub_name) \
633 (((u32Val) & (((u32) ~((u32) bitfield_stub_name##_MASK)))) \
634 | (((bitfield_val) << ((u32) bitfield_stub_name##_OFS)) \
635 & ((u32) bitfield_stub_name##_MASK)))
638 * set bitfield value in zero-initialized u32 u32Val
639 * => bitfield bits in u32Val are all zero
641 #define AMD_INIT_SETBITS(u32Val, bitfield_val, bitfield_stub_name) \
643 | (((bitfield_val) << ((u32) bitfield_stub_name##_OFS)) \
644 & ((u32) bitfield_stub_name##_MASK)))
646 /* get bitfield value from u32 u32Val */
647 #define AMD_GETBITS(u32Val, bitfield_stub_name) \
648 ((u32Val & ((u32) bitfield_stub_name##_MASK)) \
649 >> ((u32) bitfield_stub_name##_OFS))
651 /* SET and GET bits in u32 values ------------------------------------------*/
652 #define AMD_BIT(bit_stub_name) (1 << bit_stub_name)
653 #define AMD_UNMASK_BIT(bit_stub_name) (~AMD_BIT(bit_stub_name))
654 #define AMD_CLEAR_BIT(bit_stub_name) (~AMD_BIT(bit_stub_name))
656 /* debug macros ------------------------------------------------------------*/
658 #define DBG(udc , args...) dev_dbg(udc->dev, args)
663 #define VDBG(udc , args...) do {} while (0)
666 #endif /* #ifdef AMD5536UDC_H */