ci.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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_TTCTRL,
  49. OP_BURSTSIZE,
  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. * @hw_ep_max: total number of endpoints supported by hardware
  166. * @ci_hw_ep: array of endpoints
  167. * @ep0_dir: ep0 direction
  168. * @ep0out: pointer to ep0 OUT endpoint
  169. * @ep0in: pointer to ep0 IN endpoint
  170. * @status: ep0 status request
  171. * @setaddr: if we should set the address on status completion
  172. * @address: usb address received from the host
  173. * @remote_wakeup: host-enabled remote wakeup
  174. * @suspended: suspended by host
  175. * @test_mode: the selected test mode
  176. * @platdata: platform specific information supplied by parent device
  177. * @vbus_active: is VBUS active
  178. * @phy: pointer to PHY, if any
  179. * @usb_phy: pointer to USB PHY, if any and if using the USB PHY framework
  180. * @hcd: pointer to usb_hcd for ehci host driver
  181. * @debugfs: root dentry for this controller in debugfs
  182. * @id_event: indicates there is an id event, and handled at ci_otg_work
  183. * @b_sess_valid_event: indicates there is a vbus event, and handled
  184. * at ci_otg_work
  185. * @imx28_write_fix: Freescale imx28 needs swp instruction for writing
  186. * @supports_runtime_pm: if runtime pm is supported
  187. * @in_lpm: if the core in low power mode
  188. * @wakeup_int: if wakeup interrupt occur
  189. * @rev: The revision number for controller
  190. */
  191. struct ci_hdrc {
  192. struct device *dev;
  193. spinlock_t lock;
  194. struct hw_bank hw_bank;
  195. int irq;
  196. struct ci_role_driver *roles[CI_ROLE_END];
  197. enum ci_role role;
  198. bool is_otg;
  199. struct usb_otg otg;
  200. struct otg_fsm fsm;
  201. struct hrtimer otg_fsm_hrtimer;
  202. ktime_t hr_timeouts[NUM_OTG_FSM_TIMERS];
  203. unsigned enabled_otg_timer_bits;
  204. enum otg_fsm_timer next_otg_timer;
  205. struct work_struct work;
  206. struct workqueue_struct *wq;
  207. struct dma_pool *qh_pool;
  208. struct dma_pool *td_pool;
  209. struct usb_gadget gadget;
  210. struct usb_gadget_driver *driver;
  211. unsigned hw_ep_max;
  212. struct ci_hw_ep ci_hw_ep[ENDPT_MAX];
  213. u32 ep0_dir;
  214. struct ci_hw_ep *ep0out, *ep0in;
  215. struct usb_request *status;
  216. bool setaddr;
  217. u8 address;
  218. u8 remote_wakeup;
  219. u8 suspended;
  220. u8 test_mode;
  221. struct ci_hdrc_platform_data *platdata;
  222. int vbus_active;
  223. struct phy *phy;
  224. /* old usb_phy interface */
  225. struct usb_phy *usb_phy;
  226. struct usb_hcd *hcd;
  227. struct dentry *debugfs;
  228. bool id_event;
  229. bool b_sess_valid_event;
  230. bool imx28_write_fix;
  231. bool supports_runtime_pm;
  232. bool in_lpm;
  233. bool wakeup_int;
  234. enum ci_revision rev;
  235. };
  236. static inline struct ci_role_driver *ci_role(struct ci_hdrc *ci)
  237. {
  238. BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
  239. return ci->roles[ci->role];
  240. }
  241. static inline int ci_role_start(struct ci_hdrc *ci, enum ci_role role)
  242. {
  243. int ret;
  244. if (role >= CI_ROLE_END)
  245. return -EINVAL;
  246. if (!ci->roles[role])
  247. return -ENXIO;
  248. ret = ci->roles[role]->start(ci);
  249. if (!ret)
  250. ci->role = role;
  251. return ret;
  252. }
  253. static inline void ci_role_stop(struct ci_hdrc *ci)
  254. {
  255. enum ci_role role = ci->role;
  256. if (role == CI_ROLE_END)
  257. return;
  258. ci->role = CI_ROLE_END;
  259. ci->roles[role]->stop(ci);
  260. }
  261. /**
  262. * hw_read_id_reg: reads from a identification register
  263. * @ci: the controller
  264. * @offset: offset from the beginning of identification registers region
  265. * @mask: bitfield mask
  266. *
  267. * This function returns register contents
  268. */
  269. static inline u32 hw_read_id_reg(struct ci_hdrc *ci, u32 offset, u32 mask)
  270. {
  271. return ioread32(ci->hw_bank.abs + offset) & mask;
  272. }
  273. /**
  274. * hw_write_id_reg: writes to a identification register
  275. * @ci: the controller
  276. * @offset: offset from the beginning of identification registers region
  277. * @mask: bitfield mask
  278. * @data: new value
  279. */
  280. static inline void hw_write_id_reg(struct ci_hdrc *ci, u32 offset,
  281. u32 mask, u32 data)
  282. {
  283. if (~mask)
  284. data = (ioread32(ci->hw_bank.abs + offset) & ~mask)
  285. | (data & mask);
  286. iowrite32(data, ci->hw_bank.abs + offset);
  287. }
  288. /**
  289. * hw_read: reads from a hw register
  290. * @ci: the controller
  291. * @reg: register index
  292. * @mask: bitfield mask
  293. *
  294. * This function returns register contents
  295. */
  296. static inline u32 hw_read(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask)
  297. {
  298. return ioread32(ci->hw_bank.regmap[reg]) & mask;
  299. }
  300. #ifdef CONFIG_SOC_IMX28
  301. static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
  302. {
  303. __asm__ ("swp %0, %0, [%1]" : : "r"(val), "r"(addr));
  304. }
  305. #else
  306. static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
  307. {
  308. }
  309. #endif
  310. static inline void __hw_write(struct ci_hdrc *ci, u32 val,
  311. void __iomem *addr)
  312. {
  313. if (ci->imx28_write_fix)
  314. imx28_ci_writel(val, addr);
  315. else
  316. iowrite32(val, addr);
  317. }
  318. /**
  319. * hw_write: writes to a hw register
  320. * @ci: the controller
  321. * @reg: register index
  322. * @mask: bitfield mask
  323. * @data: new value
  324. */
  325. static inline void hw_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
  326. u32 mask, u32 data)
  327. {
  328. if (~mask)
  329. data = (ioread32(ci->hw_bank.regmap[reg]) & ~mask)
  330. | (data & mask);
  331. __hw_write(ci, data, ci->hw_bank.regmap[reg]);
  332. }
  333. /**
  334. * hw_test_and_clear: tests & clears a hw register
  335. * @ci: the controller
  336. * @reg: register index
  337. * @mask: bitfield mask
  338. *
  339. * This function returns register contents
  340. */
  341. static inline u32 hw_test_and_clear(struct ci_hdrc *ci, enum ci_hw_regs reg,
  342. u32 mask)
  343. {
  344. u32 val = ioread32(ci->hw_bank.regmap[reg]) & mask;
  345. __hw_write(ci, val, ci->hw_bank.regmap[reg]);
  346. return val;
  347. }
  348. /**
  349. * hw_test_and_write: tests & writes a hw register
  350. * @ci: the controller
  351. * @reg: register index
  352. * @mask: bitfield mask
  353. * @data: new value
  354. *
  355. * This function returns register contents
  356. */
  357. static inline u32 hw_test_and_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
  358. u32 mask, u32 data)
  359. {
  360. u32 val = hw_read(ci, reg, ~0);
  361. hw_write(ci, reg, mask, data);
  362. return (val & mask) >> __ffs(mask);
  363. }
  364. /**
  365. * ci_otg_is_fsm_mode: runtime check if otg controller
  366. * is in otg fsm mode.
  367. *
  368. * @ci: chipidea device
  369. */
  370. static inline bool ci_otg_is_fsm_mode(struct ci_hdrc *ci)
  371. {
  372. #ifdef CONFIG_USB_OTG_FSM
  373. struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
  374. return ci->is_otg && ci->roles[CI_ROLE_HOST] &&
  375. ci->roles[CI_ROLE_GADGET] && (otg_caps->srp_support ||
  376. otg_caps->hnp_support || otg_caps->adp_support);
  377. #else
  378. return false;
  379. #endif
  380. }
  381. u32 hw_read_intr_enable(struct ci_hdrc *ci);
  382. u32 hw_read_intr_status(struct ci_hdrc *ci);
  383. int hw_device_reset(struct ci_hdrc *ci);
  384. int hw_port_test_set(struct ci_hdrc *ci, u8 mode);
  385. u8 hw_port_test_get(struct ci_hdrc *ci);
  386. void ci_platform_configure(struct ci_hdrc *ci);
  387. #endif /* __DRIVERS_USB_CHIPIDEA_CI_H */