net2280.h 8.6 KB

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