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