ci.h 8.9 KB

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