ci.h 7.4 KB

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