blob: ef2dc92a2109057b75761dac24ea747ebbd8b339 [file] [log] [blame]
Chunfeng Yundf2069a2016-10-19 10:28:23 +08001/*
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
19#ifndef __MTU3_H__
20#define __MTU3_H__
21
22#include <linux/device.h>
23#include <linux/dmapool.h>
Chunfeng Yund0ed0622016-10-19 10:28:26 +080024#include <linux/extcon.h>
Chunfeng Yundf2069a2016-10-19 10:28:23 +080025#include <linux/interrupt.h>
26#include <linux/list.h>
27#include <linux/phy/phy.h>
28#include <linux/regulator/consumer.h>
29#include <linux/usb.h>
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32#include <linux/usb/otg.h>
33
34struct mtu3;
35struct mtu3_ep;
36struct mtu3_request;
37
38#include "mtu3_hw_regs.h"
39#include "mtu3_qmu.h"
40
41#define MU3D_EP_TXCR0(epnum) (U3D_TX1CSR0 + (((epnum) - 1) * 0x10))
42#define MU3D_EP_TXCR1(epnum) (U3D_TX1CSR1 + (((epnum) - 1) * 0x10))
43#define MU3D_EP_TXCR2(epnum) (U3D_TX1CSR2 + (((epnum) - 1) * 0x10))
44
45#define MU3D_EP_RXCR0(epnum) (U3D_RX1CSR0 + (((epnum) - 1) * 0x10))
46#define MU3D_EP_RXCR1(epnum) (U3D_RX1CSR1 + (((epnum) - 1) * 0x10))
47#define MU3D_EP_RXCR2(epnum) (U3D_RX1CSR2 + (((epnum) - 1) * 0x10))
48
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +080049#define USB_QMU_TQHIAR(epnum) (U3D_TXQHIAR1 + (((epnum) - 1) * 0x4))
50#define USB_QMU_RQHIAR(epnum) (U3D_RXQHIAR1 + (((epnum) - 1) * 0x4))
51
Chunfeng Yundf2069a2016-10-19 10:28:23 +080052#define USB_QMU_RQCSR(epnum) (U3D_RXQCSR1 + (((epnum) - 1) * 0x10))
53#define USB_QMU_RQSAR(epnum) (U3D_RXQSAR1 + (((epnum) - 1) * 0x10))
54#define USB_QMU_RQCPR(epnum) (U3D_RXQCPR1 + (((epnum) - 1) * 0x10))
55
56#define USB_QMU_TQCSR(epnum) (U3D_TXQCSR1 + (((epnum) - 1) * 0x10))
57#define USB_QMU_TQSAR(epnum) (U3D_TXQSAR1 + (((epnum) - 1) * 0x10))
58#define USB_QMU_TQCPR(epnum) (U3D_TXQCPR1 + (((epnum) - 1) * 0x10))
59
Chunfeng Yuna29de312016-10-19 10:28:24 +080060#define SSUSB_U3_CTRL(p) (U3D_SSUSB_U3_CTRL_0P + ((p) * 0x08))
Chunfeng Yundf2069a2016-10-19 10:28:23 +080061#define SSUSB_U2_CTRL(p) (U3D_SSUSB_U2_CTRL_0P + ((p) * 0x08))
62
63#define MTU3_DRIVER_NAME "mtu3"
64#define DMA_ADDR_INVALID (~(dma_addr_t)0)
65
66#define MTU3_EP_ENABLED BIT(0)
67#define MTU3_EP_STALL BIT(1)
68#define MTU3_EP_WEDGE BIT(2)
69#define MTU3_EP_BUSY BIT(3)
70
Chunfeng Yuna29de312016-10-19 10:28:24 +080071#define MTU3_U3_IP_SLOT_DEFAULT 2
Chunfeng Yundf2069a2016-10-19 10:28:23 +080072#define MTU3_U2_IP_SLOT_DEFAULT 1
73
74/**
75 * Normally the device works on HS or SS, to simplify fifo management,
76 * devide fifo into some 512B parts, use bitmap to manage it; And
77 * 128 bits size of bitmap is large enough, that means it can manage
78 * up to 64KB fifo size.
79 * NOTE: MTU3_EP_FIFO_UNIT should be power of two
80 */
81#define MTU3_EP_FIFO_UNIT (1 << 9)
82#define MTU3_FIFO_BIT_SIZE 128
83#define MTU3_U2_IP_EP0_FIFO_SIZE 64
84
85/**
86 * Maximum size of ep0 response buffer for ch9 requests,
87 * the SET_SEL request uses 6 so far, and GET_STATUS is 2
88 */
89#define EP0_RESPONSE_BUF 6
90
91/* device operated link and speed got from DEVICE_CONF register */
92enum mtu3_speed {
93 MTU3_SPEED_INACTIVE = 0,
94 MTU3_SPEED_FULL = 1,
95 MTU3_SPEED_HIGH = 3,
Chunfeng Yuna29de312016-10-19 10:28:24 +080096 MTU3_SPEED_SUPER = 4,
Chunfeng Yundf2069a2016-10-19 10:28:23 +080097};
98
99/**
100 * @MU3D_EP0_STATE_SETUP: waits for SETUP or received a SETUP
101 * without data stage.
102 * @MU3D_EP0_STATE_TX: IN data stage
103 * @MU3D_EP0_STATE_RX: OUT data stage
104 * @MU3D_EP0_STATE_TX_END: the last IN data is transferred, and
105 * waits for its completion interrupt
106 * @MU3D_EP0_STATE_STALL: ep0 is in stall status, will be auto-cleared
107 * after receives a SETUP.
108 */
109enum mtu3_g_ep0_state {
110 MU3D_EP0_STATE_SETUP = 1,
111 MU3D_EP0_STATE_TX,
112 MU3D_EP0_STATE_RX,
113 MU3D_EP0_STATE_TX_END,
114 MU3D_EP0_STATE_STALL,
115};
116
117/**
118 * @base: the base address of fifo
119 * @limit: the bitmap size in bits
120 * @bitmap: fifo bitmap in unit of @MTU3_EP_FIFO_UNIT
121 */
122struct mtu3_fifo_info {
123 u32 base;
124 u32 limit;
125 DECLARE_BITMAP(bitmap, MTU3_FIFO_BIT_SIZE);
126};
127
128/**
129 * General Purpose Descriptor (GPD):
130 * The format of TX GPD is a little different from RX one.
131 * And the size of GPD is 16 bytes.
132 *
133 * @flag:
134 * bit0: Hardware Own (HWO)
135 * bit1: Buffer Descriptor Present (BDP), always 0, BD is not supported
136 * bit2: Bypass (BPS), 1: HW skips this GPD if HWO = 1
137 * bit7: Interrupt On Completion (IOC)
138 * @chksum: This is used to validate the contents of this GPD;
139 * If TXQ_CS_EN / RXQ_CS_EN bit is set, an interrupt is issued
140 * when checksum validation fails;
141 * Checksum value is calculated over the 16 bytes of the GPD by default;
142 * @data_buf_len (RX ONLY): This value indicates the length of
143 * the assigned data buffer
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +0800144 * @tx_ext_addr (TX ONLY): [3:0] are 4 extension bits of @buffer,
145 * [7:4] are 4 extension bits of @next_gpd
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800146 * @next_gpd: Physical address of the next GPD
147 * @buffer: Physical address of the data buffer
148 * @buf_len:
149 * (TX): This value indicates the length of the assigned data buffer
150 * (RX): The total length of data received
151 * @ext_len: reserved
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +0800152 * @rx_ext_addr(RX ONLY): [3:0] are 4 extension bits of @buffer,
153 * [7:4] are 4 extension bits of @next_gpd
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800154 * @ext_flag:
155 * bit5 (TX ONLY): Zero Length Packet (ZLP),
156 */
157struct qmu_gpd {
158 __u8 flag;
159 __u8 chksum;
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +0800160 union {
161 __le16 data_buf_len;
162 __le16 tx_ext_addr;
163 };
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800164 __le32 next_gpd;
165 __le32 buffer;
166 __le16 buf_len;
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +0800167 union {
168 __u8 ext_len;
169 __u8 rx_ext_addr;
170 };
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800171 __u8 ext_flag;
172} __packed;
173
174/**
175* dma: physical base address of GPD segment
176* start: virtual base address of GPD segment
177* end: the last GPD element
178* enqueue: the first empty GPD to use
179* dequeue: the first completed GPD serviced by ISR
180* NOTE: the size of GPD ring should be >= 2
181*/
182struct mtu3_gpd_ring {
183 dma_addr_t dma;
184 struct qmu_gpd *start;
185 struct qmu_gpd *end;
186 struct qmu_gpd *enqueue;
187 struct qmu_gpd *dequeue;
188};
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800189
190/**
191* @vbus: vbus 5V used by host mode
192* @edev: external connector used to detect vbus and iddig changes
193* @vbus_nb: notifier for vbus detection
194* @vbus_nb: notifier for iddig(idpin) detection
195* @extcon_reg_dwork: delay work for extcon notifier register, waiting for
196* xHCI driver initialization, it's necessary for system bootup
197* as device.
198* @is_u3_drd: whether port0 supports usb3.0 dual-role device or not
199* @id_*: used to maually switch between host and device modes by idpin
200* @manual_drd_enabled: it's true when supports dual-role device by debugfs
201* to switch host/device modes depending on user input.
202*/
203struct otg_switch_mtk {
204 struct regulator *vbus;
205 struct extcon_dev *edev;
206 struct notifier_block vbus_nb;
207 struct notifier_block id_nb;
208 struct delayed_work extcon_reg_dwork;
209 bool is_u3_drd;
210 /* dual-role switch by debugfs */
211 struct pinctrl *id_pinctrl;
212 struct pinctrl_state *id_float;
213 struct pinctrl_state *id_ground;
214 bool manual_drd_enabled;
215};
216
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800217/**
218 * @mac_base: register base address of device MAC, exclude xHCI's
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800219 * @ippc_base: register base address of IP Power and Clock interface (IPPC)
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800220 * @vusb33: usb3.3V shared by device/host IP
221 * @sys_clk: system clock of mtu3, shared by device/host IP
Chunfeng Yuna316da82017-10-13 17:10:40 +0800222 * @ref_clk: reference clock
223 * @mcu_clk: mcu_bus_ck clock for AHB bus etc
224 * @dma_clk: dma_bus_ck clock for AXI bus etc
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800225 * @dr_mode: works in which mode:
226 * host only, device only or dual-role mode
227 * @u2_ports: number of usb2.0 host ports
228 * @u3_ports: number of usb3.0 host ports
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800229 * @u3p_dis_msk: mask of disabling usb3 ports, for example, bit0==1 to
230 * disable u3port0, bit1==1 to disable u3port1,... etc
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800231 * @dbgfs_root: only used when supports manual dual-role switch via debugfs
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800232 * @wakeup_en: it's true when supports remote wakeup in host mode
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800233 */
234struct ssusb_mtk {
235 struct device *dev;
236 struct mtu3 *u3d;
237 void __iomem *mac_base;
238 void __iomem *ippc_base;
239 struct phy **phys;
240 int num_phys;
241 /* common power & clock */
242 struct regulator *vusb33;
243 struct clk *sys_clk;
Chunfeng Yun4d70d0c2017-01-18 14:08:23 +0800244 struct clk *ref_clk;
Chunfeng Yuna316da82017-10-13 17:10:40 +0800245 struct clk *mcu_clk;
246 struct clk *dma_clk;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800247 /* otg */
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800248 struct otg_switch_mtk otg_switch;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800249 enum usb_dr_mode dr_mode;
250 bool is_host;
251 int u2_ports;
252 int u3_ports;
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800253 int u3p_dis_msk;
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800254 struct dentry *dbgfs_root;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800255 /* usb wakeup for host mode */
256 bool wakeup_en;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800257 struct regmap *pericfg;
258};
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800259
260/**
261 * @fifo_size: it is (@slot + 1) * @fifo_seg_size
262 * @fifo_seg_size: it is roundup_pow_of_two(@maxp)
263 */
264struct mtu3_ep {
265 struct usb_ep ep;
266 char name[12];
267 struct mtu3 *mtu;
268 u8 epnum;
269 u8 type;
270 u8 is_in;
271 u16 maxp;
272 int slot;
273 u32 fifo_size;
274 u32 fifo_addr;
275 u32 fifo_seg_size;
276 struct mtu3_fifo_info *fifo;
277
278 struct list_head req_list;
279 struct mtu3_gpd_ring gpd_ring;
Chunfeng Yuna29de312016-10-19 10:28:24 +0800280 const struct usb_ss_ep_comp_descriptor *comp_desc;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800281 const struct usb_endpoint_descriptor *desc;
282
283 int flags;
284 u8 wedged;
285 u8 busy;
286};
287
288struct mtu3_request {
289 struct usb_request request;
290 struct list_head list;
291 struct mtu3_ep *mep;
292 struct mtu3 *mtu;
293 struct qmu_gpd *gpd;
294 int epnum;
295};
296
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800297static inline struct ssusb_mtk *dev_to_ssusb(struct device *dev)
298{
299 return dev_get_drvdata(dev);
300}
301
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800302/**
303 * struct mtu3 - device driver instance data.
Chunfeng Yuna29de312016-10-19 10:28:24 +0800304 * @slot: MTU3_U2_IP_SLOT_DEFAULT for U2 IP only,
305 * MTU3_U3_IP_SLOT_DEFAULT for U3 IP
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800306 * @may_wakeup: means device's remote wakeup is enabled
307 * @is_self_powered: is reported in device status and the config descriptor
Chunfeng Yunfe7c9942017-07-25 16:10:22 +0800308 * @delayed_status: true when function drivers ask for delayed status
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800309 * @ep0_req: dummy request used while handling standard USB requests
310 * for GET_STATUS and SET_SEL
311 * @setup_buf: ep0 response buffer for GET_STATUS and SET_SEL requests
312 */
313struct mtu3 {
314 spinlock_t lock;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800315 struct ssusb_mtk *ssusb;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800316 struct device *dev;
317 void __iomem *mac_base;
318 void __iomem *ippc_base;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800319 int irq;
320
321 struct mtu3_fifo_info tx_fifo;
322 struct mtu3_fifo_info rx_fifo;
323
324 struct mtu3_ep *ep_array;
325 struct mtu3_ep *in_eps;
326 struct mtu3_ep *out_eps;
327 struct mtu3_ep *ep0;
328 int num_eps;
329 int slot;
330 int active_ep;
331
332 struct dma_pool *qmu_gpd_pool;
333 enum mtu3_g_ep0_state ep0_state;
334 struct usb_gadget g; /* the gadget */
335 struct usb_gadget_driver *gadget_driver;
336 struct mtu3_request ep0_req;
337 u8 setup_buf[EP0_RESPONSE_BUF];
Chunfeng Yuna29de312016-10-19 10:28:24 +0800338 u32 max_speed;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800339
340 unsigned is_active:1;
341 unsigned may_wakeup:1;
342 unsigned is_self_powered:1;
343 unsigned test_mode:1;
344 unsigned softconnect:1;
Chunfeng Yuna29de312016-10-19 10:28:24 +0800345 unsigned u1_enable:1;
346 unsigned u2_enable:1;
347 unsigned is_u3_ip:1;
Chunfeng Yunfe7c9942017-07-25 16:10:22 +0800348 unsigned delayed_status:1;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800349
350 u8 address;
351 u8 test_mode_nr;
352 u32 hw_version;
353};
354
355static inline struct mtu3 *gadget_to_mtu3(struct usb_gadget *g)
356{
357 return container_of(g, struct mtu3, g);
358}
359
360static inline int is_first_entry(const struct list_head *list,
361 const struct list_head *head)
362{
363 return list_is_last(head, list);
364}
365
366static inline struct mtu3_request *to_mtu3_request(struct usb_request *req)
367{
368 return req ? container_of(req, struct mtu3_request, request) : NULL;
369}
370
371static inline struct mtu3_ep *to_mtu3_ep(struct usb_ep *ep)
372{
373 return ep ? container_of(ep, struct mtu3_ep, ep) : NULL;
374}
375
376static inline struct mtu3_request *next_request(struct mtu3_ep *mep)
377{
Masahiro Yamada9b4632e2017-05-21 02:05:31 +0900378 return list_first_entry_or_null(&mep->req_list, struct mtu3_request,
379 list);
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800380}
381
382static inline void mtu3_writel(void __iomem *base, u32 offset, u32 data)
383{
384 writel(data, base + offset);
385}
386
387static inline u32 mtu3_readl(void __iomem *base, u32 offset)
388{
389 return readl(base + offset);
390}
391
392static inline void mtu3_setbits(void __iomem *base, u32 offset, u32 bits)
393{
394 void __iomem *addr = base + offset;
395 u32 tmp = readl(addr);
396
397 writel((tmp | (bits)), addr);
398}
399
400static inline void mtu3_clrbits(void __iomem *base, u32 offset, u32 bits)
401{
402 void __iomem *addr = base + offset;
403 u32 tmp = readl(addr);
404
405 writel((tmp & ~(bits)), addr);
406}
407
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800408int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks);
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800409struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
410void mtu3_free_request(struct usb_ep *ep, struct usb_request *req);
411void mtu3_req_complete(struct mtu3_ep *mep,
412 struct usb_request *req, int status);
413
414int mtu3_config_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
415 int interval, int burst, int mult);
416void mtu3_deconfig_ep(struct mtu3 *mtu, struct mtu3_ep *mep);
417void mtu3_ep_stall_set(struct mtu3_ep *mep, bool set);
418void mtu3_ep0_setup(struct mtu3 *mtu);
419void mtu3_start(struct mtu3 *mtu);
420void mtu3_stop(struct mtu3 *mtu);
Chunfeng Yuna29de312016-10-19 10:28:24 +0800421void mtu3_dev_on_off(struct mtu3 *mtu, int is_on);
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800422
423int mtu3_gadget_setup(struct mtu3 *mtu);
424void mtu3_gadget_cleanup(struct mtu3 *mtu);
425void mtu3_gadget_reset(struct mtu3 *mtu);
426void mtu3_gadget_suspend(struct mtu3 *mtu);
427void mtu3_gadget_resume(struct mtu3 *mtu);
428void mtu3_gadget_disconnect(struct mtu3 *mtu);
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800429
430irqreturn_t mtu3_ep0_isr(struct mtu3 *mtu);
431extern const struct usb_ep_ops mtu3_ep0_ops;
432
433#endif