ci.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 TD_PAGE_COUNT 5
  22. #define CI_HDRC_PAGE_SIZE 4096ul /* page size for TD's */
  23. #define ENDPT_MAX 32
  24. /******************************************************************************
  25. * REGISTERS
  26. *****************************************************************************/
  27. /* register indices */
  28. enum ci_hw_regs {
  29. CAP_CAPLENGTH,
  30. CAP_HCCPARAMS,
  31. CAP_DCCPARAMS,
  32. CAP_TESTMODE,
  33. CAP_LAST = CAP_TESTMODE,
  34. OP_USBCMD,
  35. OP_USBSTS,
  36. OP_USBINTR,
  37. OP_DEVICEADDR,
  38. OP_ENDPTLISTADDR,
  39. OP_PORTSC,
  40. OP_DEVLC,
  41. OP_OTGSC,
  42. OP_USBMODE,
  43. OP_ENDPTSETUPSTAT,
  44. OP_ENDPTPRIME,
  45. OP_ENDPTFLUSH,
  46. OP_ENDPTSTAT,
  47. OP_ENDPTCOMPLETE,
  48. OP_ENDPTCTRL,
  49. /* endptctrl1..15 follow */
  50. OP_LAST = OP_ENDPTCTRL + ENDPT_MAX / 2,
  51. };
  52. /******************************************************************************
  53. * STRUCTURES
  54. *****************************************************************************/
  55. /**
  56. * struct ci_hw_ep - endpoint representation
  57. * @ep: endpoint structure for gadget drivers
  58. * @dir: endpoint direction (TX/RX)
  59. * @num: endpoint number
  60. * @type: endpoint type
  61. * @name: string description of the endpoint
  62. * @qh: queue head for this endpoint
  63. * @wedge: is the endpoint wedged
  64. * @ci: pointer to the controller
  65. * @lock: pointer to controller's spinlock
  66. * @td_pool: pointer to controller's TD pool
  67. */
  68. struct ci_hw_ep {
  69. struct usb_ep ep;
  70. u8 dir;
  71. u8 num;
  72. u8 type;
  73. char name[16];
  74. struct {
  75. struct list_head queue;
  76. struct ci_hw_qh *ptr;
  77. dma_addr_t dma;
  78. } qh;
  79. int wedge;
  80. /* global resources */
  81. struct ci_hdrc *ci;
  82. spinlock_t *lock;
  83. struct dma_pool *td_pool;
  84. struct td_node *pending_td;
  85. };
  86. enum ci_role {
  87. CI_ROLE_HOST = 0,
  88. CI_ROLE_GADGET,
  89. CI_ROLE_END,
  90. };
  91. /**
  92. * struct ci_role_driver - host/gadget role driver
  93. * start: start this role
  94. * stop: stop this role
  95. * irq: irq handler for this role
  96. * name: role name string (host/gadget)
  97. */
  98. struct ci_role_driver {
  99. int (*start)(struct ci_hdrc *);
  100. void (*stop)(struct ci_hdrc *);
  101. irqreturn_t (*irq)(struct ci_hdrc *);
  102. const char *name;
  103. };
  104. /**
  105. * struct hw_bank - hardware register mapping representation
  106. * @lpm: set if the device is LPM capable
  107. * @phys: physical address of the controller's registers
  108. * @abs: absolute address of the beginning of register window
  109. * @cap: capability registers
  110. * @op: operational registers
  111. * @size: size of the register window
  112. * @regmap: register lookup table
  113. */
  114. struct hw_bank {
  115. unsigned lpm;
  116. resource_size_t phys;
  117. void __iomem *abs;
  118. void __iomem *cap;
  119. void __iomem *op;
  120. size_t size;
  121. void __iomem *regmap[OP_LAST + 1];
  122. };
  123. /**
  124. * struct ci_hdrc - chipidea device representation
  125. * @dev: pointer to parent device
  126. * @lock: access synchronization
  127. * @hw_bank: hardware register mapping
  128. * @irq: IRQ number
  129. * @roles: array of supported roles for this controller
  130. * @role: current role
  131. * @is_otg: if the device is otg-capable
  132. * @work: work for role changing
  133. * @wq: workqueue thread
  134. * @qh_pool: allocation pool for queue heads
  135. * @td_pool: allocation pool for transfer descriptors
  136. * @gadget: device side representation for peripheral controller
  137. * @driver: gadget driver
  138. * @hw_ep_max: total number of endpoints supported by hardware
  139. * @ci_hw_ep: array of endpoints
  140. * @ep0_dir: ep0 direction
  141. * @ep0out: pointer to ep0 OUT endpoint
  142. * @ep0in: pointer to ep0 IN endpoint
  143. * @status: ep0 status request
  144. * @setaddr: if we should set the address on status completion
  145. * @address: usb address received from the host
  146. * @remote_wakeup: host-enabled remote wakeup
  147. * @suspended: suspended by host
  148. * @test_mode: the selected test mode
  149. * @platdata: platform specific information supplied by parent device
  150. * @vbus_active: is VBUS active
  151. * @transceiver: pointer to USB PHY, if any
  152. * @hcd: pointer to usb_hcd for ehci host driver
  153. * @debugfs: root dentry for this controller in debugfs
  154. * @id_event: indicates there is an id event, and handled at ci_otg_work
  155. * @b_sess_valid_event: indicates there is a vbus event, and handled
  156. * at ci_otg_work
  157. * @imx28_write_fix: Freescale imx28 needs swp instruction for writing
  158. */
  159. struct ci_hdrc {
  160. struct device *dev;
  161. spinlock_t lock;
  162. struct hw_bank hw_bank;
  163. int irq;
  164. struct ci_role_driver *roles[CI_ROLE_END];
  165. enum ci_role role;
  166. bool is_otg;
  167. struct work_struct work;
  168. struct workqueue_struct *wq;
  169. struct dma_pool *qh_pool;
  170. struct dma_pool *td_pool;
  171. struct usb_gadget gadget;
  172. struct usb_gadget_driver *driver;
  173. unsigned hw_ep_max;
  174. struct ci_hw_ep ci_hw_ep[ENDPT_MAX];
  175. u32 ep0_dir;
  176. struct ci_hw_ep *ep0out, *ep0in;
  177. struct usb_request *status;
  178. bool setaddr;
  179. u8 address;
  180. u8 remote_wakeup;
  181. u8 suspended;
  182. u8 test_mode;
  183. struct ci_hdrc_platform_data *platdata;
  184. int vbus_active;
  185. /* FIXME: some day, we'll not use global phy */
  186. bool global_phy;
  187. struct usb_phy *transceiver;
  188. struct usb_hcd *hcd;
  189. struct dentry *debugfs;
  190. bool id_event;
  191. bool b_sess_valid_event;
  192. bool imx28_write_fix;
  193. };
  194. static inline struct ci_role_driver *ci_role(struct ci_hdrc *ci)
  195. {
  196. BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
  197. return ci->roles[ci->role];
  198. }
  199. static inline int ci_role_start(struct ci_hdrc *ci, enum ci_role role)
  200. {
  201. int ret;
  202. if (role >= CI_ROLE_END)
  203. return -EINVAL;
  204. if (!ci->roles[role])
  205. return -ENXIO;
  206. ret = ci->roles[role]->start(ci);
  207. if (!ret)
  208. ci->role = role;
  209. return ret;
  210. }
  211. static inline void ci_role_stop(struct ci_hdrc *ci)
  212. {
  213. enum ci_role role = ci->role;
  214. if (role == CI_ROLE_END)
  215. return;
  216. ci->role = CI_ROLE_END;
  217. ci->roles[role]->stop(ci);
  218. }
  219. /**
  220. * hw_read: reads from a hw register
  221. * @reg: register index
  222. * @mask: bitfield mask
  223. *
  224. * This function returns register contents
  225. */
  226. static inline u32 hw_read(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask)
  227. {
  228. return ioread32(ci->hw_bank.regmap[reg]) & mask;
  229. }
  230. #ifdef CONFIG_SOC_IMX28
  231. static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
  232. {
  233. __asm__ ("swp %0, %0, [%1]" : : "r"(val), "r"(addr));
  234. }
  235. #else
  236. static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
  237. {
  238. }
  239. #endif
  240. static inline void __hw_write(struct ci_hdrc *ci, u32 val,
  241. void __iomem *addr)
  242. {
  243. if (ci->imx28_write_fix)
  244. imx28_ci_writel(val, addr);
  245. else
  246. iowrite32(val, addr);
  247. }
  248. /**
  249. * hw_write: writes to a hw register
  250. * @reg: register index
  251. * @mask: bitfield mask
  252. * @data: new value
  253. */
  254. static inline void hw_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
  255. u32 mask, u32 data)
  256. {
  257. if (~mask)
  258. data = (ioread32(ci->hw_bank.regmap[reg]) & ~mask)
  259. | (data & mask);
  260. __hw_write(ci, data, ci->hw_bank.regmap[reg]);
  261. }
  262. /**
  263. * hw_test_and_clear: tests & clears a hw register
  264. * @reg: register index
  265. * @mask: bitfield mask
  266. *
  267. * This function returns register contents
  268. */
  269. static inline u32 hw_test_and_clear(struct ci_hdrc *ci, enum ci_hw_regs reg,
  270. u32 mask)
  271. {
  272. u32 val = ioread32(ci->hw_bank.regmap[reg]) & mask;
  273. __hw_write(ci, val, ci->hw_bank.regmap[reg]);
  274. return val;
  275. }
  276. /**
  277. * hw_test_and_write: tests & writes a hw register
  278. * @reg: register index
  279. * @mask: bitfield mask
  280. * @data: new value
  281. *
  282. * This function returns register contents
  283. */
  284. static inline u32 hw_test_and_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
  285. u32 mask, u32 data)
  286. {
  287. u32 val = hw_read(ci, reg, ~0);
  288. hw_write(ci, reg, mask, data);
  289. return (val & mask) >> __ffs(mask);
  290. }
  291. int hw_device_reset(struct ci_hdrc *ci, u32 mode);
  292. int hw_port_test_set(struct ci_hdrc *ci, u8 mode);
  293. u8 hw_port_test_get(struct ci_hdrc *ci);
  294. int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask,
  295. u32 value, unsigned int timeout_ms);
  296. #endif /* __DRIVERS_USB_CHIPIDEA_CI_H */