net2280.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/usb/net2280.h>
  15. /*-------------------------------------------------------------------------*/
  16. #ifdef __KERNEL__
  17. /* indexed registers [11.10] are accessed indirectly
  18. * caller must own the device lock.
  19. */
  20. static inline u32
  21. get_idx_reg (struct net2280_regs __iomem *regs, u32 index)
  22. {
  23. writel (index, &regs->idxaddr);
  24. /* NOTE: synchs device/cpu memory views */
  25. return readl (&regs->idxdata);
  26. }
  27. static inline void
  28. set_idx_reg (struct net2280_regs __iomem *regs, u32 index, u32 value)
  29. {
  30. writel (index, &regs->idxaddr);
  31. writel (value, &regs->idxdata);
  32. /* posted, may not be visible yet */
  33. }
  34. #endif /* __KERNEL__ */
  35. #define REG_DIAG 0x0
  36. #define RETRY_COUNTER 16
  37. #define FORCE_PCI_SERR 11
  38. #define FORCE_PCI_INTERRUPT 10
  39. #define FORCE_USB_INTERRUPT 9
  40. #define FORCE_CPU_INTERRUPT 8
  41. #define ILLEGAL_BYTE_ENABLES 5
  42. #define FAST_TIMES 4
  43. #define FORCE_RECEIVE_ERROR 2
  44. #define FORCE_TRANSMIT_CRC_ERROR 0
  45. #define REG_FRAME 0x02 /* from last sof */
  46. #define REG_CHIPREV 0x03 /* in bcd */
  47. #define REG_HS_NAK_RATE 0x0a /* NAK per N uframes */
  48. #define CHIPREV_1 0x0100
  49. #define CHIPREV_1A 0x0110
  50. #ifdef __KERNEL__
  51. /* ep a-f highspeed and fullspeed maxpacket, addresses
  52. * computed from ep->num
  53. */
  54. #define REG_EP_MAXPKT(dev,num) (((num) + 1) * 0x10 + \
  55. (((dev)->gadget.speed == USB_SPEED_HIGH) ? 0 : 1))
  56. /*-------------------------------------------------------------------------*/
  57. /* [8.3] for scatter/gather i/o
  58. * use struct net2280_dma_regs bitfields
  59. */
  60. struct net2280_dma {
  61. __le32 dmacount;
  62. __le32 dmaaddr; /* the buffer */
  63. __le32 dmadesc; /* next dma descriptor */
  64. __le32 _reserved;
  65. } __attribute__ ((aligned (16)));
  66. /*-------------------------------------------------------------------------*/
  67. /* DRIVER DATA STRUCTURES and UTILITIES */
  68. struct net2280_ep {
  69. struct usb_ep ep;
  70. struct net2280_ep_regs __iomem *regs;
  71. struct net2280_dma_regs __iomem *dma;
  72. struct net2280_dma *dummy;
  73. dma_addr_t td_dma; /* of dummy */
  74. struct net2280 *dev;
  75. unsigned long irqs;
  76. /* analogous to a host-side qh */
  77. struct list_head queue;
  78. const struct usb_endpoint_descriptor *desc;
  79. unsigned num : 8,
  80. fifo_size : 12,
  81. in_fifo_validate : 1,
  82. out_overflow : 1,
  83. stopped : 1,
  84. wedged : 1,
  85. is_in : 1,
  86. is_iso : 1,
  87. responded : 1;
  88. };
  89. static inline void allow_status (struct net2280_ep *ep)
  90. {
  91. /* ep0 only */
  92. writel ( (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
  93. | (1 << CLEAR_NAK_OUT_PACKETS)
  94. | (1 << CLEAR_NAK_OUT_PACKETS_MODE)
  95. , &ep->regs->ep_rsp);
  96. ep->stopped = 1;
  97. }
  98. /* count (<= 4) bytes in the next fifo write will be valid */
  99. static inline void set_fifo_bytecount (struct net2280_ep *ep, unsigned count)
  100. {
  101. writeb (count, 2 + (u8 __iomem *) &ep->regs->ep_cfg);
  102. }
  103. struct net2280_request {
  104. struct usb_request req;
  105. struct net2280_dma *td;
  106. dma_addr_t td_dma;
  107. struct list_head queue;
  108. unsigned mapped : 1,
  109. valid : 1;
  110. };
  111. struct net2280 {
  112. /* each pci device provides one gadget, several endpoints */
  113. struct usb_gadget gadget;
  114. spinlock_t lock;
  115. struct net2280_ep ep [7];
  116. struct usb_gadget_driver *driver;
  117. unsigned enabled : 1,
  118. protocol_stall : 1,
  119. softconnect : 1,
  120. got_irq : 1,
  121. region : 1;
  122. u16 chiprev;
  123. /* pci state used to access those endpoints */
  124. struct pci_dev *pdev;
  125. struct net2280_regs __iomem *regs;
  126. struct net2280_usb_regs __iomem *usb;
  127. struct net2280_pci_regs __iomem *pci;
  128. struct net2280_dma_regs __iomem *dma;
  129. struct net2280_dep_regs __iomem *dep;
  130. struct net2280_ep_regs __iomem *epregs;
  131. struct pci_pool *requests;
  132. // statistics...
  133. };
  134. static inline void set_halt (struct net2280_ep *ep)
  135. {
  136. /* ep0 and bulk/intr endpoints */
  137. writel ( (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
  138. /* set NAK_OUT for erratum 0114 */
  139. | ((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS)
  140. | (1 << SET_ENDPOINT_HALT)
  141. , &ep->regs->ep_rsp);
  142. }
  143. static inline void clear_halt (struct net2280_ep *ep)
  144. {
  145. /* ep0 and bulk/intr endpoints */
  146. writel ( (1 << CLEAR_ENDPOINT_HALT)
  147. | (1 << CLEAR_ENDPOINT_TOGGLE)
  148. /* unless the gadget driver left a short packet in the
  149. * fifo, this reverses the erratum 0114 workaround.
  150. */
  151. | ((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS)
  152. , &ep->regs->ep_rsp);
  153. }
  154. #ifdef USE_RDK_LEDS
  155. static inline void net2280_led_init (struct net2280 *dev)
  156. {
  157. /* LED3 (green) is on during USB activity. note erratum 0113. */
  158. writel ((1 << GPIO3_LED_SELECT)
  159. | (1 << GPIO3_OUTPUT_ENABLE)
  160. | (1 << GPIO2_OUTPUT_ENABLE)
  161. | (1 << GPIO1_OUTPUT_ENABLE)
  162. | (1 << GPIO0_OUTPUT_ENABLE)
  163. , &dev->regs->gpioctl);
  164. }
  165. /* indicate speed with bi-color LED 0/1 */
  166. static inline
  167. void net2280_led_speed (struct net2280 *dev, enum usb_device_speed speed)
  168. {
  169. u32 val = readl (&dev->regs->gpioctl);
  170. switch (speed) {
  171. case USB_SPEED_HIGH: /* green */
  172. val &= ~(1 << GPIO0_DATA);
  173. val |= (1 << GPIO1_DATA);
  174. break;
  175. case USB_SPEED_FULL: /* red */
  176. val &= ~(1 << GPIO1_DATA);
  177. val |= (1 << GPIO0_DATA);
  178. break;
  179. default: /* (off/black) */
  180. val &= ~((1 << GPIO1_DATA) | (1 << GPIO0_DATA));
  181. break;
  182. }
  183. writel (val, &dev->regs->gpioctl);
  184. }
  185. /* indicate power with LED 2 */
  186. static inline void net2280_led_active (struct net2280 *dev, int is_active)
  187. {
  188. u32 val = readl (&dev->regs->gpioctl);
  189. // FIXME this LED never seems to turn on.
  190. if (is_active)
  191. val |= GPIO2_DATA;
  192. else
  193. val &= ~GPIO2_DATA;
  194. writel (val, &dev->regs->gpioctl);
  195. }
  196. static inline void net2280_led_shutdown (struct net2280 *dev)
  197. {
  198. /* turn off all four GPIO*_DATA bits */
  199. writel (readl (&dev->regs->gpioctl) & ~0x0f,
  200. &dev->regs->gpioctl);
  201. }
  202. #else
  203. #define net2280_led_init(dev) do { } while (0)
  204. #define net2280_led_speed(dev, speed) do { } while (0)
  205. #define net2280_led_shutdown(dev) do { } while (0)
  206. #endif
  207. /*-------------------------------------------------------------------------*/
  208. #define xprintk(dev,level,fmt,args...) \
  209. printk(level "%s %s: " fmt , driver_name , \
  210. pci_name(dev->pdev) , ## args)
  211. #ifdef DEBUG
  212. #undef DEBUG
  213. #define DEBUG(dev,fmt,args...) \
  214. xprintk(dev , KERN_DEBUG , fmt , ## args)
  215. #else
  216. #define DEBUG(dev,fmt,args...) \
  217. do { } while (0)
  218. #endif /* DEBUG */
  219. #ifdef VERBOSE
  220. #define VDEBUG DEBUG
  221. #else
  222. #define VDEBUG(dev,fmt,args...) \
  223. do { } while (0)
  224. #endif /* VERBOSE */
  225. #define ERROR(dev,fmt,args...) \
  226. xprintk(dev , KERN_ERR , fmt , ## args)
  227. #define WARNING(dev,fmt,args...) \
  228. xprintk(dev , KERN_WARNING , fmt , ## args)
  229. #define INFO(dev,fmt,args...) \
  230. xprintk(dev , KERN_INFO , fmt , ## args)
  231. /*-------------------------------------------------------------------------*/
  232. static inline void start_out_naking (struct net2280_ep *ep)
  233. {
  234. /* NOTE: hardware races lurk here, and PING protocol issues */
  235. writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  236. /* synch with device */
  237. readl (&ep->regs->ep_rsp);
  238. }
  239. #ifdef DEBUG
  240. static inline void assert_out_naking (struct net2280_ep *ep, const char *where)
  241. {
  242. u32 tmp = readl (&ep->regs->ep_stat);
  243. if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
  244. DEBUG (ep->dev, "%s %s %08x !NAK\n",
  245. ep->ep.name, where, tmp);
  246. writel ((1 << SET_NAK_OUT_PACKETS),
  247. &ep->regs->ep_rsp);
  248. }
  249. }
  250. #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep,__func__)
  251. #else
  252. #define ASSERT_OUT_NAKING(ep) do {} while (0)
  253. #endif
  254. static inline void stop_out_naking (struct net2280_ep *ep)
  255. {
  256. u32 tmp;
  257. tmp = readl (&ep->regs->ep_stat);
  258. if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
  259. writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  260. }
  261. #endif /* __KERNEL__ */