net2280.h 10 KB

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