mtu3.h 12 KB

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