net2280.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. };
  96. static inline void allow_status (struct net2280_ep *ep)
  97. {
  98. /* ep0 only */
  99. writel ( (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
  100. | (1 << CLEAR_NAK_OUT_PACKETS)
  101. | (1 << CLEAR_NAK_OUT_PACKETS_MODE)
  102. , &ep->regs->ep_rsp);
  103. ep->stopped = 1;
  104. }
  105. /* count (<= 4) bytes in the next fifo write will be valid */
  106. static inline void set_fifo_bytecount (struct net2280_ep *ep, unsigned count)
  107. {
  108. writeb (count, 2 + (u8 __iomem *) &ep->regs->ep_cfg);
  109. }
  110. struct net2280_request {
  111. struct usb_request req;
  112. struct net2280_dma *td;
  113. dma_addr_t td_dma;
  114. struct list_head queue;
  115. unsigned mapped : 1,
  116. valid : 1;
  117. };
  118. struct net2280 {
  119. /* each pci device provides one gadget, several endpoints */
  120. struct usb_gadget gadget;
  121. spinlock_t lock;
  122. struct net2280_ep ep [7];
  123. struct usb_gadget_driver *driver;
  124. unsigned enabled : 1,
  125. protocol_stall : 1,
  126. softconnect : 1,
  127. got_irq : 1,
  128. region : 1;
  129. u16 chiprev;
  130. /* pci state used to access those endpoints */
  131. struct pci_dev *pdev;
  132. struct net2280_regs __iomem *regs;
  133. struct net2280_usb_regs __iomem *usb;
  134. struct net2280_pci_regs __iomem *pci;
  135. struct net2280_dma_regs __iomem *dma;
  136. struct net2280_dep_regs __iomem *dep;
  137. struct net2280_ep_regs __iomem *epregs;
  138. struct pci_pool *requests;
  139. // statistics...
  140. };
  141. static inline void set_halt (struct net2280_ep *ep)
  142. {
  143. /* ep0 and bulk/intr endpoints */
  144. writel ( (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
  145. /* set NAK_OUT for erratum 0114 */
  146. | ((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS)
  147. | (1 << SET_ENDPOINT_HALT)
  148. , &ep->regs->ep_rsp);
  149. }
  150. static inline void clear_halt (struct net2280_ep *ep)
  151. {
  152. /* ep0 and bulk/intr endpoints */
  153. writel ( (1 << CLEAR_ENDPOINT_HALT)
  154. | (1 << CLEAR_ENDPOINT_TOGGLE)
  155. /* unless the gadget driver left a short packet in the
  156. * fifo, this reverses the erratum 0114 workaround.
  157. */
  158. | ((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS)
  159. , &ep->regs->ep_rsp);
  160. }
  161. #ifdef USE_RDK_LEDS
  162. static inline void net2280_led_init (struct net2280 *dev)
  163. {
  164. /* LED3 (green) is on during USB activity. note erratum 0113. */
  165. writel ((1 << GPIO3_LED_SELECT)
  166. | (1 << GPIO3_OUTPUT_ENABLE)
  167. | (1 << GPIO2_OUTPUT_ENABLE)
  168. | (1 << GPIO1_OUTPUT_ENABLE)
  169. | (1 << GPIO0_OUTPUT_ENABLE)
  170. , &dev->regs->gpioctl);
  171. }
  172. /* indicate speed with bi-color LED 0/1 */
  173. static inline
  174. void net2280_led_speed (struct net2280 *dev, enum usb_device_speed speed)
  175. {
  176. u32 val = readl (&dev->regs->gpioctl);
  177. switch (speed) {
  178. case USB_SPEED_HIGH: /* green */
  179. val &= ~(1 << GPIO0_DATA);
  180. val |= (1 << GPIO1_DATA);
  181. break;
  182. case USB_SPEED_FULL: /* red */
  183. val &= ~(1 << GPIO1_DATA);
  184. val |= (1 << GPIO0_DATA);
  185. break;
  186. default: /* (off/black) */
  187. val &= ~((1 << GPIO1_DATA) | (1 << GPIO0_DATA));
  188. break;
  189. }
  190. writel (val, &dev->regs->gpioctl);
  191. }
  192. /* indicate power with LED 2 */
  193. static inline void net2280_led_active (struct net2280 *dev, int is_active)
  194. {
  195. u32 val = readl (&dev->regs->gpioctl);
  196. // FIXME this LED never seems to turn on.
  197. if (is_active)
  198. val |= GPIO2_DATA;
  199. else
  200. val &= ~GPIO2_DATA;
  201. writel (val, &dev->regs->gpioctl);
  202. }
  203. static inline void net2280_led_shutdown (struct net2280 *dev)
  204. {
  205. /* turn off all four GPIO*_DATA bits */
  206. writel (readl (&dev->regs->gpioctl) & ~0x0f,
  207. &dev->regs->gpioctl);
  208. }
  209. #else
  210. #define net2280_led_init(dev) do { } while (0)
  211. #define net2280_led_speed(dev, speed) do { } while (0)
  212. #define net2280_led_shutdown(dev) do { } while (0)
  213. #endif
  214. /*-------------------------------------------------------------------------*/
  215. #define xprintk(dev,level,fmt,args...) \
  216. printk(level "%s %s: " fmt , driver_name , \
  217. pci_name(dev->pdev) , ## args)
  218. #ifdef DEBUG
  219. #undef DEBUG
  220. #define DEBUG(dev,fmt,args...) \
  221. xprintk(dev , KERN_DEBUG , fmt , ## args)
  222. #else
  223. #define DEBUG(dev,fmt,args...) \
  224. do { } while (0)
  225. #endif /* DEBUG */
  226. #ifdef VERBOSE
  227. #define VDEBUG DEBUG
  228. #else
  229. #define VDEBUG(dev,fmt,args...) \
  230. do { } while (0)
  231. #endif /* VERBOSE */
  232. #define ERROR(dev,fmt,args...) \
  233. xprintk(dev , KERN_ERR , fmt , ## args)
  234. #define WARN(dev,fmt,args...) \
  235. xprintk(dev , KERN_WARNING , fmt , ## args)
  236. #define INFO(dev,fmt,args...) \
  237. xprintk(dev , KERN_INFO , fmt , ## args)
  238. /*-------------------------------------------------------------------------*/
  239. static inline void start_out_naking (struct net2280_ep *ep)
  240. {
  241. /* NOTE: hardware races lurk here, and PING protocol issues */
  242. writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  243. /* synch with device */
  244. readl (&ep->regs->ep_rsp);
  245. }
  246. #ifdef DEBUG
  247. static inline void assert_out_naking (struct net2280_ep *ep, const char *where)
  248. {
  249. u32 tmp = readl (&ep->regs->ep_stat);
  250. if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
  251. DEBUG (ep->dev, "%s %s %08x !NAK\n",
  252. ep->ep.name, where, tmp);
  253. writel ((1 << SET_NAK_OUT_PACKETS),
  254. &ep->regs->ep_rsp);
  255. }
  256. }
  257. #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep,__FUNCTION__)
  258. #else
  259. #define ASSERT_OUT_NAKING(ep) do {} while (0)
  260. #endif
  261. static inline void stop_out_naking (struct net2280_ep *ep)
  262. {
  263. u32 tmp;
  264. tmp = readl (&ep->regs->ep_stat);
  265. if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
  266. writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  267. }
  268. #endif /* __KERNEL__ */