net2280.h 8.6 KB

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