ci.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * ci.h - common structures, functions, and macros of the ChipIdea driver
  4. *
  5. * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
  6. *
  7. * Author: David Lopo
  8. */
  9. #ifndef __DRIVERS_USB_CHIPIDEA_CI_H
  10. #define __DRIVERS_USB_CHIPIDEA_CI_H
  11. #include <linux/list.h>
  12. #include <linux/irqreturn.h>
  13. #include <linux/usb.h>
  14. #include <linux/usb/gadget.h>
  15. #include <linux/usb/otg-fsm.h>
  16. #include <linux/usb/otg.h>
  17. #include <linux/usb/role.h>
  18. #include <linux/ulpi/interface.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. #define CI_MAX_BUF_SIZE (TD_PAGE_COUNT * CI_HDRC_PAGE_SIZE)
  26. /******************************************************************************
  27. * REGISTERS
  28. *****************************************************************************/
  29. /* Identification Registers */
  30. #define ID_ID 0x0
  31. #define ID_HWGENERAL 0x4
  32. #define ID_HWHOST 0x8
  33. #define ID_HWDEVICE 0xc
  34. #define ID_HWTXBUF 0x10
  35. #define ID_HWRXBUF 0x14
  36. #define ID_SBUSCFG 0x90
  37. /* register indices */
  38. enum ci_hw_regs {
  39. CAP_CAPLENGTH,
  40. CAP_HCCPARAMS,
  41. CAP_DCCPARAMS,
  42. CAP_TESTMODE,
  43. CAP_LAST = CAP_TESTMODE,
  44. OP_USBCMD,
  45. OP_USBSTS,
  46. OP_USBINTR,
  47. OP_FRINDEX,
  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. * @id_event: indicates there is an id event, and handled at ci_otg_work
  188. * @b_sess_valid_event: indicates there is a vbus event, and handled
  189. * at ci_otg_work
  190. * @imx28_write_fix: Freescale imx28 needs swp instruction for writing
  191. * @supports_runtime_pm: if runtime pm is supported
  192. * @in_lpm: if the core in low power mode
  193. * @wakeup_int: if wakeup interrupt occur
  194. * @rev: The revision number for controller
  195. * @mutex: protect code from concorrent running when doing role switch
  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 usb_role_switch *role_switch;
  212. struct work_struct work;
  213. struct workqueue_struct *wq;
  214. struct dma_pool *qh_pool;
  215. struct dma_pool *td_pool;
  216. struct usb_gadget gadget;
  217. struct usb_gadget_driver *driver;
  218. enum usb_device_state resume_state;
  219. unsigned hw_ep_max;
  220. struct ci_hw_ep ci_hw_ep[ENDPT_MAX];
  221. u32 ep0_dir;
  222. struct ci_hw_ep *ep0out, *ep0in;
  223. struct usb_request *status;
  224. bool setaddr;
  225. u8 address;
  226. u8 remote_wakeup;
  227. u8 suspended;
  228. u8 test_mode;
  229. struct ci_hdrc_platform_data *platdata;
  230. int vbus_active;
  231. struct ulpi *ulpi;
  232. struct ulpi_ops ulpi_ops;
  233. struct phy *phy;
  234. /* old usb_phy interface */
  235. struct usb_phy *usb_phy;
  236. struct usb_hcd *hcd;
  237. bool id_event;
  238. bool b_sess_valid_event;
  239. bool imx28_write_fix;
  240. bool has_portsc_pec_bug;
  241. bool supports_runtime_pm;
  242. bool in_lpm;
  243. bool wakeup_int;
  244. enum ci_revision rev;
  245. struct mutex mutex;
  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. static inline enum usb_role ci_role_to_usb_role(struct ci_hdrc *ci)
  273. {
  274. if (ci->role == CI_ROLE_HOST)
  275. return USB_ROLE_HOST;
  276. else if (ci->role == CI_ROLE_GADGET && ci->vbus_active)
  277. return USB_ROLE_DEVICE;
  278. else
  279. return USB_ROLE_NONE;
  280. }
  281. static inline enum ci_role usb_role_to_ci_role(enum usb_role role)
  282. {
  283. if (role == USB_ROLE_HOST)
  284. return CI_ROLE_HOST;
  285. else if (role == USB_ROLE_DEVICE)
  286. return CI_ROLE_GADGET;
  287. else
  288. return CI_ROLE_END;
  289. }
  290. /**
  291. * hw_read_id_reg: reads from a identification register
  292. * @ci: the controller
  293. * @offset: offset from the beginning of identification registers region
  294. * @mask: bitfield mask
  295. *
  296. * This function returns register contents
  297. */
  298. static inline u32 hw_read_id_reg(struct ci_hdrc *ci, u32 offset, u32 mask)
  299. {
  300. return ioread32(ci->hw_bank.abs + offset) & mask;
  301. }
  302. /**
  303. * hw_write_id_reg: writes to a identification register
  304. * @ci: the controller
  305. * @offset: offset from the beginning of identification registers region
  306. * @mask: bitfield mask
  307. * @data: new value
  308. */
  309. static inline void hw_write_id_reg(struct ci_hdrc *ci, u32 offset,
  310. u32 mask, u32 data)
  311. {
  312. if (~mask)
  313. data = (ioread32(ci->hw_bank.abs + offset) & ~mask)
  314. | (data & mask);
  315. iowrite32(data, ci->hw_bank.abs + offset);
  316. }
  317. /**
  318. * hw_read: reads from a hw register
  319. * @ci: the controller
  320. * @reg: register index
  321. * @mask: bitfield mask
  322. *
  323. * This function returns register contents
  324. */
  325. static inline u32 hw_read(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask)
  326. {
  327. return ioread32(ci->hw_bank.regmap[reg]) & mask;
  328. }
  329. #ifdef CONFIG_SOC_IMX28
  330. static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
  331. {
  332. __asm__ ("swp %0, %0, [%1]" : : "r"(val), "r"(addr));
  333. }
  334. #else
  335. static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
  336. {
  337. }
  338. #endif
  339. static inline void __hw_write(struct ci_hdrc *ci, u32 val,
  340. void __iomem *addr)
  341. {
  342. if (ci->imx28_write_fix)
  343. imx28_ci_writel(val, addr);
  344. else
  345. iowrite32(val, addr);
  346. }
  347. /**
  348. * hw_write: writes to a hw register
  349. * @ci: the controller
  350. * @reg: register index
  351. * @mask: bitfield mask
  352. * @data: new value
  353. */
  354. static inline void hw_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
  355. u32 mask, u32 data)
  356. {
  357. if (~mask)
  358. data = (ioread32(ci->hw_bank.regmap[reg]) & ~mask)
  359. | (data & mask);
  360. __hw_write(ci, data, ci->hw_bank.regmap[reg]);
  361. }
  362. /**
  363. * hw_test_and_clear: tests & clears a hw register
  364. * @ci: the controller
  365. * @reg: register index
  366. * @mask: bitfield mask
  367. *
  368. * This function returns register contents
  369. */
  370. static inline u32 hw_test_and_clear(struct ci_hdrc *ci, enum ci_hw_regs reg,
  371. u32 mask)
  372. {
  373. u32 val = ioread32(ci->hw_bank.regmap[reg]) & mask;
  374. __hw_write(ci, val, ci->hw_bank.regmap[reg]);
  375. return val;
  376. }
  377. /**
  378. * hw_test_and_write: tests & writes a hw register
  379. * @ci: the controller
  380. * @reg: register index
  381. * @mask: bitfield mask
  382. * @data: new value
  383. *
  384. * This function returns register contents
  385. */
  386. static inline u32 hw_test_and_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
  387. u32 mask, u32 data)
  388. {
  389. u32 val = hw_read(ci, reg, ~0);
  390. hw_write(ci, reg, mask, data);
  391. return (val & mask) >> __ffs(mask);
  392. }
  393. /**
  394. * ci_otg_is_fsm_mode: runtime check if otg controller
  395. * is in otg fsm mode.
  396. *
  397. * @ci: chipidea device
  398. */
  399. static inline bool ci_otg_is_fsm_mode(struct ci_hdrc *ci)
  400. {
  401. #ifdef CONFIG_USB_OTG_FSM
  402. struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
  403. return ci->is_otg && ci->roles[CI_ROLE_HOST] &&
  404. ci->roles[CI_ROLE_GADGET] && (otg_caps->srp_support ||
  405. otg_caps->hnp_support || otg_caps->adp_support);
  406. #else
  407. return false;
  408. #endif
  409. }
  410. int ci_ulpi_init(struct ci_hdrc *ci);
  411. void ci_ulpi_exit(struct ci_hdrc *ci);
  412. int ci_ulpi_resume(struct ci_hdrc *ci);
  413. u32 hw_read_intr_enable(struct ci_hdrc *ci);
  414. u32 hw_read_intr_status(struct ci_hdrc *ci);
  415. int hw_device_reset(struct ci_hdrc *ci);
  416. int hw_port_test_set(struct ci_hdrc *ci, u8 mode);
  417. u8 hw_port_test_get(struct ci_hdrc *ci);
  418. void hw_phymode_configure(struct ci_hdrc *ci);
  419. void ci_platform_configure(struct ci_hdrc *ci);
  420. void dbg_create_files(struct ci_hdrc *ci);
  421. void dbg_remove_files(struct ci_hdrc *ci);
  422. #endif /* __DRIVERS_USB_CHIPIDEA_CI_H */