ci.h 11 KB

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