ci.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. * @debugfs: root dentry for this controller in debugfs
  124. */
  125. struct ci13xxx {
  126. struct device *dev;
  127. spinlock_t lock;
  128. struct hw_bank hw_bank;
  129. int irq;
  130. struct ci_role_driver *roles[CI_ROLE_END];
  131. enum ci_role role;
  132. bool is_otg;
  133. struct work_struct 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. struct dentry *debugfs;
  156. };
  157. static inline struct ci_role_driver *ci_role(struct ci13xxx *ci)
  158. {
  159. BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
  160. return ci->roles[ci->role];
  161. }
  162. static inline int ci_role_start(struct ci13xxx *ci, enum ci_role role)
  163. {
  164. int ret;
  165. if (role >= CI_ROLE_END)
  166. return -EINVAL;
  167. if (!ci->roles[role])
  168. return -ENXIO;
  169. ret = ci->roles[role]->start(ci);
  170. if (!ret)
  171. ci->role = role;
  172. return ret;
  173. }
  174. static inline void ci_role_stop(struct ci13xxx *ci)
  175. {
  176. enum ci_role role = ci->role;
  177. if (role == CI_ROLE_END)
  178. return;
  179. ci->role = CI_ROLE_END;
  180. ci->roles[role]->stop(ci);
  181. }
  182. /******************************************************************************
  183. * REGISTERS
  184. *****************************************************************************/
  185. /* register size */
  186. #define REG_BITS (32)
  187. /* register indices */
  188. enum ci13xxx_regs {
  189. CAP_CAPLENGTH,
  190. CAP_HCCPARAMS,
  191. CAP_DCCPARAMS,
  192. CAP_TESTMODE,
  193. CAP_LAST = CAP_TESTMODE,
  194. OP_USBCMD,
  195. OP_USBSTS,
  196. OP_USBINTR,
  197. OP_DEVICEADDR,
  198. OP_ENDPTLISTADDR,
  199. OP_PORTSC,
  200. OP_DEVLC,
  201. OP_OTGSC,
  202. OP_USBMODE,
  203. OP_ENDPTSETUPSTAT,
  204. OP_ENDPTPRIME,
  205. OP_ENDPTFLUSH,
  206. OP_ENDPTSTAT,
  207. OP_ENDPTCOMPLETE,
  208. OP_ENDPTCTRL,
  209. /* endptctrl1..15 follow */
  210. OP_LAST = OP_ENDPTCTRL + ENDPT_MAX / 2,
  211. };
  212. /**
  213. * hw_read: reads from a hw register
  214. * @reg: register index
  215. * @mask: bitfield mask
  216. *
  217. * This function returns register contents
  218. */
  219. static inline u32 hw_read(struct ci13xxx *ci, enum ci13xxx_regs reg, u32 mask)
  220. {
  221. return ioread32(ci->hw_bank.regmap[reg]) & mask;
  222. }
  223. /**
  224. * hw_write: writes to a hw register
  225. * @reg: register index
  226. * @mask: bitfield mask
  227. * @data: new value
  228. */
  229. static inline void hw_write(struct ci13xxx *ci, enum ci13xxx_regs reg,
  230. u32 mask, u32 data)
  231. {
  232. if (~mask)
  233. data = (ioread32(ci->hw_bank.regmap[reg]) & ~mask)
  234. | (data & mask);
  235. iowrite32(data, ci->hw_bank.regmap[reg]);
  236. }
  237. /**
  238. * hw_test_and_clear: tests & clears a hw register
  239. * @reg: register index
  240. * @mask: bitfield mask
  241. *
  242. * This function returns register contents
  243. */
  244. static inline u32 hw_test_and_clear(struct ci13xxx *ci, enum ci13xxx_regs reg,
  245. u32 mask)
  246. {
  247. u32 val = ioread32(ci->hw_bank.regmap[reg]) & mask;
  248. iowrite32(val, ci->hw_bank.regmap[reg]);
  249. return val;
  250. }
  251. /**
  252. * hw_test_and_write: tests & writes a hw register
  253. * @reg: register index
  254. * @mask: bitfield mask
  255. * @data: new value
  256. *
  257. * This function returns register contents
  258. */
  259. static inline u32 hw_test_and_write(struct ci13xxx *ci, enum ci13xxx_regs reg,
  260. u32 mask, u32 data)
  261. {
  262. u32 val = hw_read(ci, reg, ~0);
  263. hw_write(ci, reg, mask, data);
  264. return (val & mask) >> __ffs(mask);
  265. }
  266. int hw_device_reset(struct ci13xxx *ci, u32 mode);
  267. int hw_port_test_set(struct ci13xxx *ci, u8 mode);
  268. u8 hw_port_test_get(struct ci13xxx *ci);
  269. #endif /* __DRIVERS_USB_CHIPIDEA_CI_H */