ci.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * ci.h - common structures, functions, and macros of the ChipIdea driver
  3. *
  4. * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
  5. *
  6. * Author: David Lopo
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef __DRIVERS_USB_CHIPIDEA_CI_H
  13. #define __DRIVERS_USB_CHIPIDEA_CI_H
  14. #include <linux/list.h>
  15. #include <linux/irqreturn.h>
  16. #include <linux/usb.h>
  17. #include <linux/usb/gadget.h>
  18. /******************************************************************************
  19. * DEFINE
  20. *****************************************************************************/
  21. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  22. #define CI13XXX_PAGE_SIZE 4096ul /* page size for TD's */
  23. #define ENDPT_MAX 32
  24. /******************************************************************************
  25. * STRUCTURES
  26. *****************************************************************************/
  27. /**
  28. * struct ci13xxx_ep - endpoint representation
  29. * @ep: endpoint structure for gadget drivers
  30. * @dir: endpoint direction (TX/RX)
  31. * @num: endpoint number
  32. * @type: endpoint type
  33. * @name: string description of the endpoint
  34. * @qh: queue head for this endpoint
  35. * @wedge: is the endpoint wedged
  36. * @udc: pointer to the controller
  37. * @lock: pointer to controller's spinlock
  38. * @device: pointer to gadget's struct device
  39. * @td_pool: pointer to controller's TD pool
  40. */
  41. struct ci13xxx_ep {
  42. struct usb_ep ep;
  43. u8 dir;
  44. u8 num;
  45. u8 type;
  46. char name[16];
  47. struct {
  48. struct list_head queue;
  49. struct ci13xxx_qh *ptr;
  50. dma_addr_t dma;
  51. } qh;
  52. int wedge;
  53. /* global resources */
  54. struct ci13xxx *udc;
  55. spinlock_t *lock;
  56. struct device *device;
  57. struct dma_pool *td_pool;
  58. };
  59. enum ci_role {
  60. CI_ROLE_HOST = 0,
  61. CI_ROLE_GADGET,
  62. CI_ROLE_END,
  63. };
  64. /**
  65. * struct ci_role_driver - host/gadget role driver
  66. * start: start this role
  67. * stop: stop this role
  68. * irq: irq handler for this role
  69. * name: role name string (host/gadget)
  70. */
  71. struct ci_role_driver {
  72. int (*start)(struct ci13xxx *);
  73. void (*stop)(struct ci13xxx *);
  74. irqreturn_t (*irq)(struct ci13xxx *);
  75. const char *name;
  76. };
  77. /**
  78. * struct hw_bank - hardware register mapping representation
  79. * @lpm: set if the device is LPM capable
  80. * @phys: physical address of the controller's registers
  81. * @abs: absolute address of the beginning of register window
  82. * @cap: capability registers
  83. * @op: operational registers
  84. * @size: size of the register window
  85. * @regmap: register lookup table
  86. */
  87. struct hw_bank {
  88. unsigned lpm;
  89. resource_size_t phys;
  90. void __iomem *abs;
  91. void __iomem *cap;
  92. void __iomem *op;
  93. size_t size;
  94. void __iomem **regmap;
  95. };
  96. /**
  97. * struct ci13xxx - chipidea device representation
  98. * @dev: pointer to parent device
  99. * @lock: access synchronization
  100. * @hw_bank: hardware register mapping
  101. * @irq: IRQ number
  102. * @roles: array of supported roles for this controller
  103. * @role: current role
  104. * @is_otg: if the device is otg-capable
  105. * @work: work for role changing
  106. * @wq: workqueue thread
  107. * @qh_pool: allocation pool for queue heads
  108. * @td_pool: allocation pool for transfer descriptors
  109. * @gadget: device side representation for peripheral controller
  110. * @driver: gadget driver
  111. * @hw_ep_max: total number of endpoints supported by hardware
  112. * @ci13xxx_ep: array of endpoints
  113. * @ep0_dir: ep0 direction
  114. * @ep0out: pointer to ep0 OUT endpoint
  115. * @ep0in: pointer to ep0 IN endpoint
  116. * @status: ep0 status request
  117. * @setaddr: if we should set the address on status completion
  118. * @address: usb address received from the host
  119. * @remote_wakeup: host-enabled remote wakeup
  120. * @suspended: suspended by host
  121. * @test_mode: the selected test mode
  122. * @udc_driver: platform specific information supplied by parent device
  123. * @vbus_active: is VBUS active
  124. * @transceiver: pointer to USB PHY, if any
  125. * @hcd: pointer to usb_hcd for ehci host driver
  126. */
  127. struct ci13xxx {
  128. struct device *dev;
  129. spinlock_t lock;
  130. struct hw_bank hw_bank;
  131. int irq;
  132. struct ci_role_driver *roles[CI_ROLE_END];
  133. enum ci_role role;
  134. bool is_otg;
  135. struct work_struct work;
  136. struct workqueue_struct *wq;
  137. struct dma_pool *qh_pool;
  138. struct dma_pool *td_pool;
  139. struct usb_gadget gadget;
  140. struct usb_gadget_driver *driver;
  141. unsigned hw_ep_max;
  142. struct ci13xxx_ep ci13xxx_ep[ENDPT_MAX];
  143. u32 ep0_dir;
  144. struct ci13xxx_ep *ep0out, *ep0in;
  145. struct usb_request *status;
  146. bool setaddr;
  147. u8 address;
  148. u8 remote_wakeup;
  149. u8 suspended;
  150. u8 test_mode;
  151. struct ci13xxx_udc_driver *udc_driver;
  152. int vbus_active;
  153. struct usb_phy *transceiver;
  154. struct usb_hcd *hcd;
  155. };
  156. static inline struct ci_role_driver *ci_role(struct ci13xxx *ci)
  157. {
  158. BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
  159. return ci->roles[ci->role];
  160. }
  161. static inline int ci_role_start(struct ci13xxx *ci, enum ci_role role)
  162. {
  163. int ret;
  164. if (role >= CI_ROLE_END)
  165. return -EINVAL;
  166. if (!ci->roles[role])
  167. return -ENXIO;
  168. ret = ci->roles[role]->start(ci);
  169. if (!ret)
  170. ci->role = role;
  171. return ret;
  172. }
  173. static inline void ci_role_stop(struct ci13xxx *ci)
  174. {
  175. enum ci_role role = ci->role;
  176. if (role == CI_ROLE_END)
  177. return;
  178. ci->role = CI_ROLE_END;
  179. ci->roles[role]->stop(ci);
  180. }
  181. /******************************************************************************
  182. * REGISTERS
  183. *****************************************************************************/
  184. /* register size */
  185. #define REG_BITS (32)
  186. /* register indices */
  187. enum ci13xxx_regs {
  188. CAP_CAPLENGTH,
  189. CAP_HCCPARAMS,
  190. CAP_DCCPARAMS,
  191. CAP_TESTMODE,
  192. CAP_LAST = CAP_TESTMODE,
  193. OP_USBCMD,
  194. OP_USBSTS,
  195. OP_USBINTR,
  196. OP_DEVICEADDR,
  197. OP_ENDPTLISTADDR,
  198. OP_PORTSC,
  199. OP_DEVLC,
  200. OP_OTGSC,
  201. OP_USBMODE,
  202. OP_ENDPTSETUPSTAT,
  203. OP_ENDPTPRIME,
  204. OP_ENDPTFLUSH,
  205. OP_ENDPTSTAT,
  206. OP_ENDPTCOMPLETE,
  207. OP_ENDPTCTRL,
  208. /* endptctrl1..15 follow */
  209. OP_LAST = OP_ENDPTCTRL + ENDPT_MAX / 2,
  210. };
  211. /**
  212. * ffs_nr: find first (least significant) bit set
  213. * @x: the word to search
  214. *
  215. * This function returns bit number (instead of position)
  216. */
  217. static inline int ffs_nr(u32 x)
  218. {
  219. int n = ffs(x);
  220. return n ? n-1 : 32;
  221. }
  222. /**
  223. * hw_read: reads from a hw register
  224. * @reg: register index
  225. * @mask: bitfield mask
  226. *
  227. * This function returns register contents
  228. */
  229. static inline u32 hw_read(struct ci13xxx *udc, enum ci13xxx_regs reg, u32 mask)
  230. {
  231. return ioread32(udc->hw_bank.regmap[reg]) & mask;
  232. }
  233. /**
  234. * hw_write: writes to a hw register
  235. * @reg: register index
  236. * @mask: bitfield mask
  237. * @data: new value
  238. */
  239. static inline void hw_write(struct ci13xxx *udc, enum ci13xxx_regs reg,
  240. u32 mask, u32 data)
  241. {
  242. if (~mask)
  243. data = (ioread32(udc->hw_bank.regmap[reg]) & ~mask)
  244. | (data & mask);
  245. iowrite32(data, udc->hw_bank.regmap[reg]);
  246. }
  247. /**
  248. * hw_test_and_clear: tests & clears a hw register
  249. * @reg: register index
  250. * @mask: bitfield mask
  251. *
  252. * This function returns register contents
  253. */
  254. static inline u32 hw_test_and_clear(struct ci13xxx *udc, enum ci13xxx_regs reg,
  255. u32 mask)
  256. {
  257. u32 val = ioread32(udc->hw_bank.regmap[reg]) & mask;
  258. iowrite32(val, udc->hw_bank.regmap[reg]);
  259. return val;
  260. }
  261. /**
  262. * hw_test_and_write: tests & writes a hw register
  263. * @reg: register index
  264. * @mask: bitfield mask
  265. * @data: new value
  266. *
  267. * This function returns register contents
  268. */
  269. static inline u32 hw_test_and_write(struct ci13xxx *udc, enum ci13xxx_regs reg,
  270. u32 mask, u32 data)
  271. {
  272. u32 val = hw_read(udc, reg, ~0);
  273. hw_write(udc, reg, mask, data);
  274. return (val & mask) >> ffs_nr(mask);
  275. }
  276. int hw_device_reset(struct ci13xxx *ci, u32 mode);
  277. int hw_port_test_set(struct ci13xxx *ci, u8 mode);
  278. u8 hw_port_test_get(struct ci13xxx *ci);
  279. #endif /* __DRIVERS_USB_CHIPIDEA_CI_H */