ci.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. /******************************************************************************
  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. * STRUCTURES
  26. *****************************************************************************/
  27. /**
  28. * struct ci_hw_ep - endpoint representation
  29. * @ep: endpoint structure for gadget drivers
  30. * @dir: endpoint direction (TX/RX)
  31. * @num: endpoint number
  32. * @type: endpoint type
  33. * @name: string description of the endpoint
  34. * @qh: queue head for this endpoint
  35. * @wedge: is the endpoint wedged
  36. * @ci: pointer to the controller
  37. * @lock: pointer to controller's spinlock
  38. * @td_pool: pointer to controller's TD pool
  39. */
  40. struct ci_hw_ep {
  41. struct usb_ep ep;
  42. u8 dir;
  43. u8 num;
  44. u8 type;
  45. char name[16];
  46. struct {
  47. struct list_head queue;
  48. struct ci_hw_qh *ptr;
  49. dma_addr_t dma;
  50. } qh;
  51. int wedge;
  52. /* global resources */
  53. struct ci_hdrc *ci;
  54. spinlock_t *lock;
  55. struct dma_pool *td_pool;
  56. struct td_node *pending_td;
  57. };
  58. enum ci_role {
  59. CI_ROLE_HOST = 0,
  60. CI_ROLE_GADGET,
  61. CI_ROLE_END,
  62. };
  63. /**
  64. * struct ci_role_driver - host/gadget role driver
  65. * start: start this role
  66. * stop: stop this role
  67. * irq: irq handler for this role
  68. * name: role name string (host/gadget)
  69. */
  70. struct ci_role_driver {
  71. int (*start)(struct ci_hdrc *);
  72. void (*stop)(struct ci_hdrc *);
  73. irqreturn_t (*irq)(struct ci_hdrc *);
  74. const char *name;
  75. };
  76. /**
  77. * struct hw_bank - hardware register mapping representation
  78. * @lpm: set if the device is LPM capable
  79. * @phys: physical address of the controller's registers
  80. * @abs: absolute address of the beginning of register window
  81. * @cap: capability registers
  82. * @op: operational registers
  83. * @size: size of the register window
  84. * @regmap: register lookup table
  85. */
  86. struct hw_bank {
  87. unsigned lpm;
  88. resource_size_t phys;
  89. void __iomem *abs;
  90. void __iomem *cap;
  91. void __iomem *op;
  92. size_t size;
  93. void __iomem **regmap;
  94. };
  95. /**
  96. * struct ci_hdrc - chipidea device representation
  97. * @dev: pointer to parent device
  98. * @lock: access synchronization
  99. * @hw_bank: hardware register mapping
  100. * @irq: IRQ number
  101. * @roles: array of supported roles for this controller
  102. * @role: current role
  103. * @is_otg: if the device is otg-capable
  104. * @work: work for role changing
  105. * @wq: workqueue thread
  106. * @qh_pool: allocation pool for queue heads
  107. * @td_pool: allocation pool for transfer descriptors
  108. * @gadget: device side representation for peripheral controller
  109. * @driver: gadget driver
  110. * @hw_ep_max: total number of endpoints supported by hardware
  111. * @ci_hw_ep: array of endpoints
  112. * @ep0_dir: ep0 direction
  113. * @ep0out: pointer to ep0 OUT endpoint
  114. * @ep0in: pointer to ep0 IN endpoint
  115. * @status: ep0 status request
  116. * @setaddr: if we should set the address on status completion
  117. * @address: usb address received from the host
  118. * @remote_wakeup: host-enabled remote wakeup
  119. * @suspended: suspended by host
  120. * @test_mode: the selected test mode
  121. * @platdata: platform specific information supplied by parent device
  122. * @vbus_active: is VBUS active
  123. * @transceiver: pointer to USB PHY, if any
  124. * @hcd: pointer to usb_hcd for ehci host driver
  125. * @debugfs: root dentry for this controller in debugfs
  126. * @id_event: indicates there is an id event, and handled at ci_otg_work
  127. * @b_sess_valid_event: indicates there is a vbus event, and handled
  128. * at ci_otg_work
  129. */
  130. struct ci_hdrc {
  131. struct device *dev;
  132. spinlock_t lock;
  133. struct hw_bank hw_bank;
  134. int irq;
  135. struct ci_role_driver *roles[CI_ROLE_END];
  136. enum ci_role role;
  137. bool is_otg;
  138. struct work_struct work;
  139. struct workqueue_struct *wq;
  140. struct dma_pool *qh_pool;
  141. struct dma_pool *td_pool;
  142. struct usb_gadget gadget;
  143. struct usb_gadget_driver *driver;
  144. unsigned hw_ep_max;
  145. struct ci_hw_ep ci_hw_ep[ENDPT_MAX];
  146. u32 ep0_dir;
  147. struct ci_hw_ep *ep0out, *ep0in;
  148. struct usb_request *status;
  149. bool setaddr;
  150. u8 address;
  151. u8 remote_wakeup;
  152. u8 suspended;
  153. u8 test_mode;
  154. struct ci_hdrc_platform_data *platdata;
  155. int vbus_active;
  156. /* FIXME: some day, we'll not use global phy */
  157. bool global_phy;
  158. struct usb_phy *transceiver;
  159. struct usb_hcd *hcd;
  160. struct dentry *debugfs;
  161. bool id_event;
  162. bool b_sess_valid_event;
  163. };
  164. static inline struct ci_role_driver *ci_role(struct ci_hdrc *ci)
  165. {
  166. BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
  167. return ci->roles[ci->role];
  168. }
  169. static inline int ci_role_start(struct ci_hdrc *ci, enum ci_role role)
  170. {
  171. int ret;
  172. if (role >= CI_ROLE_END)
  173. return -EINVAL;
  174. if (!ci->roles[role])
  175. return -ENXIO;
  176. ret = ci->roles[role]->start(ci);
  177. if (!ret)
  178. ci->role = role;
  179. return ret;
  180. }
  181. static inline void ci_role_stop(struct ci_hdrc *ci)
  182. {
  183. enum ci_role role = ci->role;
  184. if (role == CI_ROLE_END)
  185. return;
  186. ci->role = CI_ROLE_END;
  187. ci->roles[role]->stop(ci);
  188. }
  189. /******************************************************************************
  190. * REGISTERS
  191. *****************************************************************************/
  192. /* register size */
  193. #define REG_BITS (32)
  194. /* register indices */
  195. enum ci_hw_regs {
  196. CAP_CAPLENGTH,
  197. CAP_HCCPARAMS,
  198. CAP_DCCPARAMS,
  199. CAP_TESTMODE,
  200. CAP_LAST = CAP_TESTMODE,
  201. OP_USBCMD,
  202. OP_USBSTS,
  203. OP_USBINTR,
  204. OP_DEVICEADDR,
  205. OP_ENDPTLISTADDR,
  206. OP_PORTSC,
  207. OP_DEVLC,
  208. OP_OTGSC,
  209. OP_USBMODE,
  210. OP_ENDPTSETUPSTAT,
  211. OP_ENDPTPRIME,
  212. OP_ENDPTFLUSH,
  213. OP_ENDPTSTAT,
  214. OP_ENDPTCOMPLETE,
  215. OP_ENDPTCTRL,
  216. /* endptctrl1..15 follow */
  217. OP_LAST = OP_ENDPTCTRL + ENDPT_MAX / 2,
  218. };
  219. /**
  220. * hw_read: reads from a hw register
  221. * @reg: register index
  222. * @mask: bitfield mask
  223. *
  224. * This function returns register contents
  225. */
  226. static inline u32 hw_read(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask)
  227. {
  228. return ioread32(ci->hw_bank.regmap[reg]) & mask;
  229. }
  230. /**
  231. * hw_write: writes to a hw register
  232. * @reg: register index
  233. * @mask: bitfield mask
  234. * @data: new value
  235. */
  236. static inline void hw_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
  237. u32 mask, u32 data)
  238. {
  239. if (~mask)
  240. data = (ioread32(ci->hw_bank.regmap[reg]) & ~mask)
  241. | (data & mask);
  242. iowrite32(data, ci->hw_bank.regmap[reg]);
  243. }
  244. /**
  245. * hw_test_and_clear: tests & clears a hw register
  246. * @reg: register index
  247. * @mask: bitfield mask
  248. *
  249. * This function returns register contents
  250. */
  251. static inline u32 hw_test_and_clear(struct ci_hdrc *ci, enum ci_hw_regs reg,
  252. u32 mask)
  253. {
  254. u32 val = ioread32(ci->hw_bank.regmap[reg]) & mask;
  255. iowrite32(val, ci->hw_bank.regmap[reg]);
  256. return val;
  257. }
  258. /**
  259. * hw_test_and_write: tests & writes a hw register
  260. * @reg: register index
  261. * @mask: bitfield mask
  262. * @data: new value
  263. *
  264. * This function returns register contents
  265. */
  266. static inline u32 hw_test_and_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
  267. u32 mask, u32 data)
  268. {
  269. u32 val = hw_read(ci, reg, ~0);
  270. hw_write(ci, reg, mask, data);
  271. return (val & mask) >> __ffs(mask);
  272. }
  273. int hw_device_reset(struct ci_hdrc *ci, u32 mode);
  274. int hw_port_test_set(struct ci_hdrc *ci, u8 mode);
  275. u8 hw_port_test_get(struct ci_hdrc *ci);
  276. int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask,
  277. u32 value, unsigned int timeout_ms);
  278. #endif /* __DRIVERS_USB_CHIPIDEA_CI_H */