mtu3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * mtu3.h - MediaTek USB3 DRD header
  3. *
  4. * Copyright (C) 2016 MediaTek Inc.
  5. *
  6. * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #ifndef __MTU3_H__
  19. #define __MTU3_H__
  20. #include <linux/device.h>
  21. #include <linux/dmapool.h>
  22. #include <linux/extcon.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/list.h>
  25. #include <linux/phy/phy.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include <linux/usb/otg.h>
  31. struct mtu3;
  32. struct mtu3_ep;
  33. struct mtu3_request;
  34. #include "mtu3_hw_regs.h"
  35. #include "mtu3_qmu.h"
  36. #define MU3D_EP_TXCR0(epnum) (U3D_TX1CSR0 + (((epnum) - 1) * 0x10))
  37. #define MU3D_EP_TXCR1(epnum) (U3D_TX1CSR1 + (((epnum) - 1) * 0x10))
  38. #define MU3D_EP_TXCR2(epnum) (U3D_TX1CSR2 + (((epnum) - 1) * 0x10))
  39. #define MU3D_EP_RXCR0(epnum) (U3D_RX1CSR0 + (((epnum) - 1) * 0x10))
  40. #define MU3D_EP_RXCR1(epnum) (U3D_RX1CSR1 + (((epnum) - 1) * 0x10))
  41. #define MU3D_EP_RXCR2(epnum) (U3D_RX1CSR2 + (((epnum) - 1) * 0x10))
  42. #define USB_QMU_RQCSR(epnum) (U3D_RXQCSR1 + (((epnum) - 1) * 0x10))
  43. #define USB_QMU_RQSAR(epnum) (U3D_RXQSAR1 + (((epnum) - 1) * 0x10))
  44. #define USB_QMU_RQCPR(epnum) (U3D_RXQCPR1 + (((epnum) - 1) * 0x10))
  45. #define USB_QMU_TQCSR(epnum) (U3D_TXQCSR1 + (((epnum) - 1) * 0x10))
  46. #define USB_QMU_TQSAR(epnum) (U3D_TXQSAR1 + (((epnum) - 1) * 0x10))
  47. #define USB_QMU_TQCPR(epnum) (U3D_TXQCPR1 + (((epnum) - 1) * 0x10))
  48. #define SSUSB_U3_CTRL(p) (U3D_SSUSB_U3_CTRL_0P + ((p) * 0x08))
  49. #define SSUSB_U2_CTRL(p) (U3D_SSUSB_U2_CTRL_0P + ((p) * 0x08))
  50. #define MTU3_DRIVER_NAME "mtu3"
  51. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  52. #define MTU3_EP_ENABLED BIT(0)
  53. #define MTU3_EP_STALL BIT(1)
  54. #define MTU3_EP_WEDGE BIT(2)
  55. #define MTU3_EP_BUSY BIT(3)
  56. #define MTU3_U3_IP_SLOT_DEFAULT 2
  57. #define MTU3_U2_IP_SLOT_DEFAULT 1
  58. /**
  59. * Normally the device works on HS or SS, to simplify fifo management,
  60. * devide fifo into some 512B parts, use bitmap to manage it; And
  61. * 128 bits size of bitmap is large enough, that means it can manage
  62. * up to 64KB fifo size.
  63. * NOTE: MTU3_EP_FIFO_UNIT should be power of two
  64. */
  65. #define MTU3_EP_FIFO_UNIT (1 << 9)
  66. #define MTU3_FIFO_BIT_SIZE 128
  67. #define MTU3_U2_IP_EP0_FIFO_SIZE 64
  68. /**
  69. * Maximum size of ep0 response buffer for ch9 requests,
  70. * the SET_SEL request uses 6 so far, and GET_STATUS is 2
  71. */
  72. #define EP0_RESPONSE_BUF 6
  73. /* device operated link and speed got from DEVICE_CONF register */
  74. enum mtu3_speed {
  75. MTU3_SPEED_INACTIVE = 0,
  76. MTU3_SPEED_FULL = 1,
  77. MTU3_SPEED_HIGH = 3,
  78. MTU3_SPEED_SUPER = 4,
  79. };
  80. /**
  81. * @MU3D_EP0_STATE_SETUP: waits for SETUP or received a SETUP
  82. * without data stage.
  83. * @MU3D_EP0_STATE_TX: IN data stage
  84. * @MU3D_EP0_STATE_RX: OUT data stage
  85. * @MU3D_EP0_STATE_TX_END: the last IN data is transferred, and
  86. * waits for its completion interrupt
  87. * @MU3D_EP0_STATE_STALL: ep0 is in stall status, will be auto-cleared
  88. * after receives a SETUP.
  89. */
  90. enum mtu3_g_ep0_state {
  91. MU3D_EP0_STATE_SETUP = 1,
  92. MU3D_EP0_STATE_TX,
  93. MU3D_EP0_STATE_RX,
  94. MU3D_EP0_STATE_TX_END,
  95. MU3D_EP0_STATE_STALL,
  96. };
  97. /**
  98. * @base: the base address of fifo
  99. * @limit: the bitmap size in bits
  100. * @bitmap: fifo bitmap in unit of @MTU3_EP_FIFO_UNIT
  101. */
  102. struct mtu3_fifo_info {
  103. u32 base;
  104. u32 limit;
  105. DECLARE_BITMAP(bitmap, MTU3_FIFO_BIT_SIZE);
  106. };
  107. /**
  108. * General Purpose Descriptor (GPD):
  109. * The format of TX GPD is a little different from RX one.
  110. * And the size of GPD is 16 bytes.
  111. *
  112. * @flag:
  113. * bit0: Hardware Own (HWO)
  114. * bit1: Buffer Descriptor Present (BDP), always 0, BD is not supported
  115. * bit2: Bypass (BPS), 1: HW skips this GPD if HWO = 1
  116. * bit7: Interrupt On Completion (IOC)
  117. * @chksum: This is used to validate the contents of this GPD;
  118. * If TXQ_CS_EN / RXQ_CS_EN bit is set, an interrupt is issued
  119. * when checksum validation fails;
  120. * Checksum value is calculated over the 16 bytes of the GPD by default;
  121. * @data_buf_len (RX ONLY): This value indicates the length of
  122. * the assigned data buffer
  123. * @next_gpd: Physical address of the next GPD
  124. * @buffer: Physical address of the data buffer
  125. * @buf_len:
  126. * (TX): This value indicates the length of the assigned data buffer
  127. * (RX): The total length of data received
  128. * @ext_len: reserved
  129. * @ext_flag:
  130. * bit5 (TX ONLY): Zero Length Packet (ZLP),
  131. */
  132. struct qmu_gpd {
  133. __u8 flag;
  134. __u8 chksum;
  135. __le16 data_buf_len;
  136. __le32 next_gpd;
  137. __le32 buffer;
  138. __le16 buf_len;
  139. __u8 ext_len;
  140. __u8 ext_flag;
  141. } __packed;
  142. /**
  143. * dma: physical base address of GPD segment
  144. * start: virtual base address of GPD segment
  145. * end: the last GPD element
  146. * enqueue: the first empty GPD to use
  147. * dequeue: the first completed GPD serviced by ISR
  148. * NOTE: the size of GPD ring should be >= 2
  149. */
  150. struct mtu3_gpd_ring {
  151. dma_addr_t dma;
  152. struct qmu_gpd *start;
  153. struct qmu_gpd *end;
  154. struct qmu_gpd *enqueue;
  155. struct qmu_gpd *dequeue;
  156. };
  157. /**
  158. * @vbus: vbus 5V used by host mode
  159. * @edev: external connector used to detect vbus and iddig changes
  160. * @vbus_nb: notifier for vbus detection
  161. * @vbus_nb: notifier for iddig(idpin) detection
  162. * @extcon_reg_dwork: delay work for extcon notifier register, waiting for
  163. * xHCI driver initialization, it's necessary for system bootup
  164. * as device.
  165. * @is_u3_drd: whether port0 supports usb3.0 dual-role device or not
  166. * @id_*: used to maually switch between host and device modes by idpin
  167. * @manual_drd_enabled: it's true when supports dual-role device by debugfs
  168. * to switch host/device modes depending on user input.
  169. */
  170. struct otg_switch_mtk {
  171. struct regulator *vbus;
  172. struct extcon_dev *edev;
  173. struct notifier_block vbus_nb;
  174. struct notifier_block id_nb;
  175. struct delayed_work extcon_reg_dwork;
  176. bool is_u3_drd;
  177. /* dual-role switch by debugfs */
  178. struct pinctrl *id_pinctrl;
  179. struct pinctrl_state *id_float;
  180. struct pinctrl_state *id_ground;
  181. bool manual_drd_enabled;
  182. };
  183. /**
  184. * @mac_base: register base address of device MAC, exclude xHCI's
  185. * @ippc_base: register base address of IP Power and Clock interface (IPPC)
  186. * @vusb33: usb3.3V shared by device/host IP
  187. * @sys_clk: system clock of mtu3, shared by device/host IP
  188. * @dr_mode: works in which mode:
  189. * host only, device only or dual-role mode
  190. * @u2_ports: number of usb2.0 host ports
  191. * @u3_ports: number of usb3.0 host ports
  192. * @dbgfs_root: only used when supports manual dual-role switch via debugfs
  193. * @wakeup_en: it's true when supports remote wakeup in host mode
  194. * @wk_deb_p0: port0's wakeup debounce clock
  195. * @wk_deb_p1: it's optional, and depends on port1 is supported or not
  196. */
  197. struct ssusb_mtk {
  198. struct device *dev;
  199. struct mtu3 *u3d;
  200. void __iomem *mac_base;
  201. void __iomem *ippc_base;
  202. struct phy **phys;
  203. int num_phys;
  204. /* common power & clock */
  205. struct regulator *vusb33;
  206. struct clk *sys_clk;
  207. /* otg */
  208. struct otg_switch_mtk otg_switch;
  209. enum usb_dr_mode dr_mode;
  210. bool is_host;
  211. int u2_ports;
  212. int u3_ports;
  213. struct dentry *dbgfs_root;
  214. /* usb wakeup for host mode */
  215. bool wakeup_en;
  216. struct clk *wk_deb_p0;
  217. struct clk *wk_deb_p1;
  218. struct regmap *pericfg;
  219. };
  220. /**
  221. * @fifo_size: it is (@slot + 1) * @fifo_seg_size
  222. * @fifo_seg_size: it is roundup_pow_of_two(@maxp)
  223. */
  224. struct mtu3_ep {
  225. struct usb_ep ep;
  226. char name[12];
  227. struct mtu3 *mtu;
  228. u8 epnum;
  229. u8 type;
  230. u8 is_in;
  231. u16 maxp;
  232. int slot;
  233. u32 fifo_size;
  234. u32 fifo_addr;
  235. u32 fifo_seg_size;
  236. struct mtu3_fifo_info *fifo;
  237. struct list_head req_list;
  238. struct mtu3_gpd_ring gpd_ring;
  239. const struct usb_ss_ep_comp_descriptor *comp_desc;
  240. const struct usb_endpoint_descriptor *desc;
  241. int flags;
  242. u8 wedged;
  243. u8 busy;
  244. };
  245. struct mtu3_request {
  246. struct usb_request request;
  247. struct list_head list;
  248. struct mtu3_ep *mep;
  249. struct mtu3 *mtu;
  250. struct qmu_gpd *gpd;
  251. int epnum;
  252. };
  253. static inline struct ssusb_mtk *dev_to_ssusb(struct device *dev)
  254. {
  255. return dev_get_drvdata(dev);
  256. }
  257. /**
  258. * struct mtu3 - device driver instance data.
  259. * @slot: MTU3_U2_IP_SLOT_DEFAULT for U2 IP only,
  260. * MTU3_U3_IP_SLOT_DEFAULT for U3 IP
  261. * @may_wakeup: means device's remote wakeup is enabled
  262. * @is_self_powered: is reported in device status and the config descriptor
  263. * @ep0_req: dummy request used while handling standard USB requests
  264. * for GET_STATUS and SET_SEL
  265. * @setup_buf: ep0 response buffer for GET_STATUS and SET_SEL requests
  266. */
  267. struct mtu3 {
  268. spinlock_t lock;
  269. struct ssusb_mtk *ssusb;
  270. struct device *dev;
  271. void __iomem *mac_base;
  272. void __iomem *ippc_base;
  273. int irq;
  274. struct mtu3_fifo_info tx_fifo;
  275. struct mtu3_fifo_info rx_fifo;
  276. struct mtu3_ep *ep_array;
  277. struct mtu3_ep *in_eps;
  278. struct mtu3_ep *out_eps;
  279. struct mtu3_ep *ep0;
  280. int num_eps;
  281. int slot;
  282. int active_ep;
  283. struct dma_pool *qmu_gpd_pool;
  284. enum mtu3_g_ep0_state ep0_state;
  285. struct usb_gadget g; /* the gadget */
  286. struct usb_gadget_driver *gadget_driver;
  287. struct mtu3_request ep0_req;
  288. u8 setup_buf[EP0_RESPONSE_BUF];
  289. u32 max_speed;
  290. unsigned is_active:1;
  291. unsigned may_wakeup:1;
  292. unsigned is_self_powered:1;
  293. unsigned test_mode:1;
  294. unsigned softconnect:1;
  295. unsigned u1_enable:1;
  296. unsigned u2_enable:1;
  297. unsigned is_u3_ip:1;
  298. u8 address;
  299. u8 test_mode_nr;
  300. u32 hw_version;
  301. };
  302. static inline struct mtu3 *gadget_to_mtu3(struct usb_gadget *g)
  303. {
  304. return container_of(g, struct mtu3, g);
  305. }
  306. static inline int is_first_entry(const struct list_head *list,
  307. const struct list_head *head)
  308. {
  309. return list_is_last(head, list);
  310. }
  311. static inline struct mtu3_request *to_mtu3_request(struct usb_request *req)
  312. {
  313. return req ? container_of(req, struct mtu3_request, request) : NULL;
  314. }
  315. static inline struct mtu3_ep *to_mtu3_ep(struct usb_ep *ep)
  316. {
  317. return ep ? container_of(ep, struct mtu3_ep, ep) : NULL;
  318. }
  319. static inline struct mtu3_request *next_request(struct mtu3_ep *mep)
  320. {
  321. struct list_head *queue = &mep->req_list;
  322. if (list_empty(queue))
  323. return NULL;
  324. return list_first_entry(queue, struct mtu3_request, list);
  325. }
  326. static inline void mtu3_writel(void __iomem *base, u32 offset, u32 data)
  327. {
  328. writel(data, base + offset);
  329. }
  330. static inline u32 mtu3_readl(void __iomem *base, u32 offset)
  331. {
  332. return readl(base + offset);
  333. }
  334. static inline void mtu3_setbits(void __iomem *base, u32 offset, u32 bits)
  335. {
  336. void __iomem *addr = base + offset;
  337. u32 tmp = readl(addr);
  338. writel((tmp | (bits)), addr);
  339. }
  340. static inline void mtu3_clrbits(void __iomem *base, u32 offset, u32 bits)
  341. {
  342. void __iomem *addr = base + offset;
  343. u32 tmp = readl(addr);
  344. writel((tmp & ~(bits)), addr);
  345. }
  346. int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks);
  347. struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
  348. void mtu3_free_request(struct usb_ep *ep, struct usb_request *req);
  349. void mtu3_req_complete(struct mtu3_ep *mep,
  350. struct usb_request *req, int status);
  351. int mtu3_config_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
  352. int interval, int burst, int mult);
  353. void mtu3_deconfig_ep(struct mtu3 *mtu, struct mtu3_ep *mep);
  354. void mtu3_ep_stall_set(struct mtu3_ep *mep, bool set);
  355. void mtu3_ep0_setup(struct mtu3 *mtu);
  356. void mtu3_start(struct mtu3 *mtu);
  357. void mtu3_stop(struct mtu3 *mtu);
  358. void mtu3_dev_on_off(struct mtu3 *mtu, int is_on);
  359. int mtu3_gadget_setup(struct mtu3 *mtu);
  360. void mtu3_gadget_cleanup(struct mtu3 *mtu);
  361. void mtu3_gadget_reset(struct mtu3 *mtu);
  362. void mtu3_gadget_suspend(struct mtu3 *mtu);
  363. void mtu3_gadget_resume(struct mtu3 *mtu);
  364. void mtu3_gadget_disconnect(struct mtu3 *mtu);
  365. irqreturn_t mtu3_ep0_isr(struct mtu3 *mtu);
  366. extern const struct usb_ep_ops mtu3_ep0_ops;
  367. #endif