net2280.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * NetChip 2280 high/full speed USB device controller.
  3. * Unlike many such controllers, this one talks PCI.
  4. */
  5. /*
  6. * Copyright (C) 2002 NetChip Technology, Inc. (http://www.netchip.com)
  7. * Copyright (C) 2003 David Brownell
  8. * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/usb/net2280.h>
  16. #include <linux/usb/usb338x.h>
  17. /*-------------------------------------------------------------------------*/
  18. #ifdef __KERNEL__
  19. /* indexed registers [11.10] are accessed indirectly
  20. * caller must own the device lock.
  21. */
  22. static inline u32 get_idx_reg(struct net2280_regs __iomem *regs, u32 index)
  23. {
  24. writel(index, &regs->idxaddr);
  25. /* NOTE: synchs device/cpu memory views */
  26. return readl(&regs->idxdata);
  27. }
  28. static inline void
  29. set_idx_reg(struct net2280_regs __iomem *regs, u32 index, u32 value)
  30. {
  31. writel(index, &regs->idxaddr);
  32. writel(value, &regs->idxdata);
  33. /* posted, may not be visible yet */
  34. }
  35. #endif /* __KERNEL__ */
  36. #define PCI_VENDOR_ID_PLX_LEGACY 0x17cc
  37. #define PLX_LEGACY BIT(0)
  38. #define PLX_2280 BIT(1)
  39. #define PLX_SUPERSPEED BIT(2)
  40. #define REG_DIAG 0x0
  41. #define RETRY_COUNTER 16
  42. #define FORCE_PCI_SERR 11
  43. #define FORCE_PCI_INTERRUPT 10
  44. #define FORCE_USB_INTERRUPT 9
  45. #define FORCE_CPU_INTERRUPT 8
  46. #define ILLEGAL_BYTE_ENABLES 5
  47. #define FAST_TIMES 4
  48. #define FORCE_RECEIVE_ERROR 2
  49. #define FORCE_TRANSMIT_CRC_ERROR 0
  50. #define REG_FRAME 0x02 /* from last sof */
  51. #define REG_CHIPREV 0x03 /* in bcd */
  52. #define REG_HS_NAK_RATE 0x0a /* NAK per N uframes */
  53. #define CHIPREV_1 0x0100
  54. #define CHIPREV_1A 0x0110
  55. /* DEFECT 7374 */
  56. #define DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS 200
  57. #define DEFECT_7374_PROCESSOR_WAIT_TIME 10
  58. /* ep0 max packet size */
  59. #define EP0_SS_MAX_PACKET_SIZE 0x200
  60. #define EP0_HS_MAX_PACKET_SIZE 0x40
  61. #ifdef __KERNEL__
  62. /*-------------------------------------------------------------------------*/
  63. /* [8.3] for scatter/gather i/o
  64. * use struct net2280_dma_regs bitfields
  65. */
  66. struct net2280_dma {
  67. __le32 dmacount;
  68. __le32 dmaaddr; /* the buffer */
  69. __le32 dmadesc; /* next dma descriptor */
  70. __le32 _reserved;
  71. } __aligned(16);
  72. /*-------------------------------------------------------------------------*/
  73. /* DRIVER DATA STRUCTURES and UTILITIES */
  74. struct net2280_ep {
  75. struct usb_ep ep;
  76. struct net2280_ep_regs __iomem *cfg;
  77. struct net2280_ep_regs __iomem *regs;
  78. struct net2280_dma_regs __iomem *dma;
  79. struct net2280_dma *dummy;
  80. struct usb338x_fifo_regs __iomem *fiforegs;
  81. dma_addr_t td_dma; /* of dummy */
  82. struct net2280 *dev;
  83. unsigned long irqs;
  84. unsigned is_halt:1, dma_started:1;
  85. /* analogous to a host-side qh */
  86. struct list_head queue;
  87. const struct usb_endpoint_descriptor *desc;
  88. unsigned num : 8,
  89. fifo_size : 12,
  90. in_fifo_validate : 1,
  91. out_overflow : 1,
  92. stopped : 1,
  93. wedged : 1,
  94. is_in : 1,
  95. is_iso : 1,
  96. responded : 1;
  97. };
  98. static inline void allow_status(struct net2280_ep *ep)
  99. {
  100. /* ep0 only */
  101. writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE) |
  102. BIT(CLEAR_NAK_OUT_PACKETS) |
  103. BIT(CLEAR_NAK_OUT_PACKETS_MODE),
  104. &ep->regs->ep_rsp);
  105. ep->stopped = 1;
  106. }
  107. static void allow_status_338x(struct net2280_ep *ep)
  108. {
  109. /*
  110. * Control Status Phase Handshake was set by the chip when the setup
  111. * packet arrived. While set, the chip automatically NAKs the host's
  112. * Status Phase tokens.
  113. */
  114. writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE), &ep->regs->ep_rsp);
  115. ep->stopped = 1;
  116. /* TD 9.9 Halt Endpoint test. TD 9.22 set feature test. */
  117. ep->responded = 0;
  118. }
  119. struct net2280_request {
  120. struct usb_request req;
  121. struct net2280_dma *td;
  122. dma_addr_t td_dma;
  123. struct list_head queue;
  124. unsigned mapped : 1,
  125. valid : 1;
  126. };
  127. struct net2280 {
  128. /* each pci device provides one gadget, several endpoints */
  129. struct usb_gadget gadget;
  130. spinlock_t lock;
  131. struct net2280_ep ep[9];
  132. struct usb_gadget_driver *driver;
  133. unsigned enabled : 1,
  134. protocol_stall : 1,
  135. softconnect : 1,
  136. got_irq : 1,
  137. region:1,
  138. u1_enable:1,
  139. u2_enable:1,
  140. ltm_enable:1,
  141. wakeup_enable:1,
  142. selfpowered:1,
  143. addressed_state:1;
  144. u16 chiprev;
  145. int enhanced_mode;
  146. int n_ep;
  147. kernel_ulong_t quirks;
  148. /* pci state used to access those endpoints */
  149. struct pci_dev *pdev;
  150. struct net2280_regs __iomem *regs;
  151. struct net2280_usb_regs __iomem *usb;
  152. struct usb338x_usb_ext_regs __iomem *usb_ext;
  153. struct net2280_pci_regs __iomem *pci;
  154. struct net2280_dma_regs __iomem *dma;
  155. struct net2280_dep_regs __iomem *dep;
  156. struct net2280_ep_regs __iomem *epregs;
  157. struct usb338x_fifo_regs __iomem *fiforegs;
  158. struct usb338x_ll_regs __iomem *llregs;
  159. struct usb338x_ll_lfps_regs __iomem *ll_lfps_regs;
  160. struct usb338x_ll_tsn_regs __iomem *ll_tsn_regs;
  161. struct usb338x_ll_chi_regs __iomem *ll_chicken_reg;
  162. struct usb338x_pl_regs __iomem *plregs;
  163. struct pci_pool *requests;
  164. /* statistics...*/
  165. };
  166. static inline void set_halt(struct net2280_ep *ep)
  167. {
  168. /* ep0 and bulk/intr endpoints */
  169. writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE) |
  170. /* set NAK_OUT for erratum 0114 */
  171. ((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS) |
  172. BIT(SET_ENDPOINT_HALT),
  173. &ep->regs->ep_rsp);
  174. }
  175. static inline void clear_halt(struct net2280_ep *ep)
  176. {
  177. /* ep0 and bulk/intr endpoints */
  178. writel(BIT(CLEAR_ENDPOINT_HALT) |
  179. BIT(CLEAR_ENDPOINT_TOGGLE) |
  180. /*
  181. * unless the gadget driver left a short packet in the
  182. * fifo, this reverses the erratum 0114 workaround.
  183. */
  184. ((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS),
  185. &ep->regs->ep_rsp);
  186. }
  187. /*
  188. * FSM value for Defect 7374 (U1U2 Test) is managed in
  189. * chip's SCRATCH register:
  190. */
  191. #define DEFECT7374_FSM_FIELD 28
  192. /* Waiting for Control Read:
  193. * - A transition to this state indicates a fresh USB connection,
  194. * before the first Setup Packet. The connection speed is not
  195. * known. Firmware is waiting for the first Control Read.
  196. * - Starting state: This state can be thought of as the FSM's typical
  197. * starting state.
  198. * - Tip: Upon the first SS Control Read the FSM never
  199. * returns to this state.
  200. */
  201. #define DEFECT7374_FSM_WAITING_FOR_CONTROL_READ BIT(DEFECT7374_FSM_FIELD)
  202. /* Non-SS Control Read:
  203. * - A transition to this state indicates detection of the first HS
  204. * or FS Control Read.
  205. * - Tip: Upon the first SS Control Read the FSM never
  206. * returns to this state.
  207. */
  208. #define DEFECT7374_FSM_NON_SS_CONTROL_READ (2 << DEFECT7374_FSM_FIELD)
  209. /* SS Control Read:
  210. * - A transition to this state indicates detection of the
  211. * first SS Control Read.
  212. * - This state indicates workaround completion. Workarounds no longer
  213. * need to be applied (as long as the chip remains powered up).
  214. * - Tip: Once in this state the FSM state does not change (until
  215. * the chip's power is lost and restored).
  216. * - This can be thought of as the final state of the FSM;
  217. * the FSM 'locks-up' in this state until the chip loses power.
  218. */
  219. #define DEFECT7374_FSM_SS_CONTROL_READ (3 << DEFECT7374_FSM_FIELD)
  220. #ifdef USE_RDK_LEDS
  221. static inline void net2280_led_init(struct net2280 *dev)
  222. {
  223. /* LED3 (green) is on during USB activity. note erratum 0113. */
  224. writel(BIT(GPIO3_LED_SELECT) |
  225. BIT(GPIO3_OUTPUT_ENABLE) |
  226. BIT(GPIO2_OUTPUT_ENABLE) |
  227. BIT(GPIO1_OUTPUT_ENABLE) |
  228. BIT(GPIO0_OUTPUT_ENABLE),
  229. &dev->regs->gpioctl);
  230. }
  231. /* indicate speed with bi-color LED 0/1 */
  232. static inline
  233. void net2280_led_speed(struct net2280 *dev, enum usb_device_speed speed)
  234. {
  235. u32 val = readl(&dev->regs->gpioctl);
  236. switch (speed) {
  237. case USB_SPEED_SUPER: /* green + red */
  238. val |= BIT(GPIO0_DATA) | BIT(GPIO1_DATA);
  239. break;
  240. case USB_SPEED_HIGH: /* green */
  241. val &= ~BIT(GPIO0_DATA);
  242. val |= BIT(GPIO1_DATA);
  243. break;
  244. case USB_SPEED_FULL: /* red */
  245. val &= ~BIT(GPIO1_DATA);
  246. val |= BIT(GPIO0_DATA);
  247. break;
  248. default: /* (off/black) */
  249. val &= ~(BIT(GPIO1_DATA) | BIT(GPIO0_DATA));
  250. break;
  251. }
  252. writel(val, &dev->regs->gpioctl);
  253. }
  254. /* indicate power with LED 2 */
  255. static inline void net2280_led_active(struct net2280 *dev, int is_active)
  256. {
  257. u32 val = readl(&dev->regs->gpioctl);
  258. /* FIXME this LED never seems to turn on.*/
  259. if (is_active)
  260. val |= GPIO2_DATA;
  261. else
  262. val &= ~GPIO2_DATA;
  263. writel(val, &dev->regs->gpioctl);
  264. }
  265. static inline void net2280_led_shutdown(struct net2280 *dev)
  266. {
  267. /* turn off all four GPIO*_DATA bits */
  268. writel(readl(&dev->regs->gpioctl) & ~0x0f,
  269. &dev->regs->gpioctl);
  270. }
  271. #else
  272. #define net2280_led_init(dev) do { } while (0)
  273. #define net2280_led_speed(dev, speed) do { } while (0)
  274. #define net2280_led_shutdown(dev) do { } while (0)
  275. #endif
  276. /*-------------------------------------------------------------------------*/
  277. #define ep_dbg(ndev, fmt, args...) \
  278. dev_dbg((&((ndev)->pdev->dev)), fmt, ##args)
  279. #define ep_vdbg(ndev, fmt, args...) \
  280. dev_vdbg((&((ndev)->pdev->dev)), fmt, ##args)
  281. #define ep_info(ndev, fmt, args...) \
  282. dev_info((&((ndev)->pdev->dev)), fmt, ##args)
  283. #define ep_warn(ndev, fmt, args...) \
  284. dev_warn((&((ndev)->pdev->dev)), fmt, ##args)
  285. #define ep_err(ndev, fmt, args...) \
  286. dev_err((&((ndev)->pdev->dev)), fmt, ##args)
  287. /*-------------------------------------------------------------------------*/
  288. static inline void set_fifo_bytecount(struct net2280_ep *ep, unsigned count)
  289. {
  290. if (ep->dev->pdev->vendor == 0x17cc)
  291. writeb(count, 2 + (u8 __iomem *) &ep->regs->ep_cfg);
  292. else{
  293. u32 tmp = readl(&ep->cfg->ep_cfg) &
  294. (~(0x07 << EP_FIFO_BYTE_COUNT));
  295. writel(tmp | (count << EP_FIFO_BYTE_COUNT), &ep->cfg->ep_cfg);
  296. }
  297. }
  298. static inline void start_out_naking(struct net2280_ep *ep)
  299. {
  300. /* NOTE: hardware races lurk here, and PING protocol issues */
  301. writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  302. /* synch with device */
  303. readl(&ep->regs->ep_rsp);
  304. }
  305. #ifdef DEBUG
  306. static inline void assert_out_naking(struct net2280_ep *ep, const char *where)
  307. {
  308. u32 tmp = readl(&ep->regs->ep_stat);
  309. if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
  310. ep_dbg(ep->dev, "%s %s %08x !NAK\n",
  311. ep->ep.name, where, tmp);
  312. writel(BIT(SET_NAK_OUT_PACKETS),
  313. &ep->regs->ep_rsp);
  314. }
  315. }
  316. #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep, __func__)
  317. #else
  318. #define ASSERT_OUT_NAKING(ep) do {} while (0)
  319. #endif
  320. static inline void stop_out_naking(struct net2280_ep *ep)
  321. {
  322. u32 tmp;
  323. tmp = readl(&ep->regs->ep_stat);
  324. if ((tmp & BIT(NAK_OUT_PACKETS)) != 0)
  325. writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  326. }
  327. static inline void set_max_speed(struct net2280_ep *ep, u32 max)
  328. {
  329. u32 reg;
  330. static const u32 ep_enhanced[9] = { 0x10, 0x60, 0x30, 0x80,
  331. 0x50, 0x20, 0x70, 0x40, 0x90 };
  332. if (ep->dev->enhanced_mode)
  333. reg = ep_enhanced[ep->num];
  334. else{
  335. reg = (ep->num + 1) * 0x10;
  336. if (ep->dev->gadget.speed != USB_SPEED_HIGH)
  337. reg += 1;
  338. }
  339. set_idx_reg(ep->dev->regs, reg, max);
  340. }
  341. #endif /* __KERNEL__ */