ci.h 12 KB

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