ci.h 11 KB

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