net2280.h 11 KB

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