ci.h 13 KB

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