ci.h 7.5 KB

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