blob: dfdace0d81a0a381b9051168df31a4ff8c825cf4 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Chunfeng Yundf2069a2016-10-19 10:28:23 +08002/*
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 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20#ifndef __MTU3_H__
21#define __MTU3_H__
22
23#include <linux/device.h>
24#include <linux/dmapool.h>
Chunfeng Yund0ed0622016-10-19 10:28:26 +080025#include <linux/extcon.h>
Chunfeng Yundf2069a2016-10-19 10:28:23 +080026#include <linux/interrupt.h>
27#include <linux/list.h>
28#include <linux/phy/phy.h>
29#include <linux/regulator/consumer.h>
30#include <linux/usb.h>
31#include <linux/usb/ch9.h>
32#include <linux/usb/gadget.h>
33#include <linux/usb/otg.h>
34
35struct mtu3;
36struct mtu3_ep;
37struct mtu3_request;
38
39#include "mtu3_hw_regs.h"
40#include "mtu3_qmu.h"
41
42#define MU3D_EP_TXCR0(epnum) (U3D_TX1CSR0 + (((epnum) - 1) * 0x10))
43#define MU3D_EP_TXCR1(epnum) (U3D_TX1CSR1 + (((epnum) - 1) * 0x10))
44#define MU3D_EP_TXCR2(epnum) (U3D_TX1CSR2 + (((epnum) - 1) * 0x10))
45
46#define MU3D_EP_RXCR0(epnum) (U3D_RX1CSR0 + (((epnum) - 1) * 0x10))
47#define MU3D_EP_RXCR1(epnum) (U3D_RX1CSR1 + (((epnum) - 1) * 0x10))
48#define MU3D_EP_RXCR2(epnum) (U3D_RX1CSR2 + (((epnum) - 1) * 0x10))
49
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +080050#define USB_QMU_TQHIAR(epnum) (U3D_TXQHIAR1 + (((epnum) - 1) * 0x4))
51#define USB_QMU_RQHIAR(epnum) (U3D_RXQHIAR1 + (((epnum) - 1) * 0x4))
52
Chunfeng Yundf2069a2016-10-19 10:28:23 +080053#define USB_QMU_RQCSR(epnum) (U3D_RXQCSR1 + (((epnum) - 1) * 0x10))
54#define USB_QMU_RQSAR(epnum) (U3D_RXQSAR1 + (((epnum) - 1) * 0x10))
55#define USB_QMU_RQCPR(epnum) (U3D_RXQCPR1 + (((epnum) - 1) * 0x10))
56
57#define USB_QMU_TQCSR(epnum) (U3D_TXQCSR1 + (((epnum) - 1) * 0x10))
58#define USB_QMU_TQSAR(epnum) (U3D_TXQSAR1 + (((epnum) - 1) * 0x10))
59#define USB_QMU_TQCPR(epnum) (U3D_TXQCPR1 + (((epnum) - 1) * 0x10))
60
Chunfeng Yuna29de312016-10-19 10:28:24 +080061#define SSUSB_U3_CTRL(p) (U3D_SSUSB_U3_CTRL_0P + ((p) * 0x08))
Chunfeng Yundf2069a2016-10-19 10:28:23 +080062#define SSUSB_U2_CTRL(p) (U3D_SSUSB_U2_CTRL_0P + ((p) * 0x08))
63
64#define MTU3_DRIVER_NAME "mtu3"
65#define DMA_ADDR_INVALID (~(dma_addr_t)0)
66
67#define MTU3_EP_ENABLED BIT(0)
68#define MTU3_EP_STALL BIT(1)
69#define MTU3_EP_WEDGE BIT(2)
70#define MTU3_EP_BUSY BIT(3)
71
Chunfeng Yuna29de312016-10-19 10:28:24 +080072#define MTU3_U3_IP_SLOT_DEFAULT 2
Chunfeng Yundf2069a2016-10-19 10:28:23 +080073#define MTU3_U2_IP_SLOT_DEFAULT 1
74
75/**
76 * Normally the device works on HS or SS, to simplify fifo management,
77 * devide fifo into some 512B parts, use bitmap to manage it; And
78 * 128 bits size of bitmap is large enough, that means it can manage
79 * up to 64KB fifo size.
80 * NOTE: MTU3_EP_FIFO_UNIT should be power of two
81 */
82#define MTU3_EP_FIFO_UNIT (1 << 9)
83#define MTU3_FIFO_BIT_SIZE 128
84#define MTU3_U2_IP_EP0_FIFO_SIZE 64
85
86/**
87 * Maximum size of ep0 response buffer for ch9 requests,
88 * the SET_SEL request uses 6 so far, and GET_STATUS is 2
89 */
90#define EP0_RESPONSE_BUF 6
91
92/* device operated link and speed got from DEVICE_CONF register */
93enum mtu3_speed {
94 MTU3_SPEED_INACTIVE = 0,
95 MTU3_SPEED_FULL = 1,
96 MTU3_SPEED_HIGH = 3,
Chunfeng Yuna29de312016-10-19 10:28:24 +080097 MTU3_SPEED_SUPER = 4,
Chunfeng Yun4d79e042017-10-13 17:10:43 +080098 MTU3_SPEED_SUPER_PLUS = 5,
Chunfeng Yundf2069a2016-10-19 10:28:23 +080099};
100
101/**
102 * @MU3D_EP0_STATE_SETUP: waits for SETUP or received a SETUP
103 * without data stage.
104 * @MU3D_EP0_STATE_TX: IN data stage
105 * @MU3D_EP0_STATE_RX: OUT data stage
106 * @MU3D_EP0_STATE_TX_END: the last IN data is transferred, and
107 * waits for its completion interrupt
108 * @MU3D_EP0_STATE_STALL: ep0 is in stall status, will be auto-cleared
109 * after receives a SETUP.
110 */
111enum mtu3_g_ep0_state {
112 MU3D_EP0_STATE_SETUP = 1,
113 MU3D_EP0_STATE_TX,
114 MU3D_EP0_STATE_RX,
115 MU3D_EP0_STATE_TX_END,
116 MU3D_EP0_STATE_STALL,
117};
118
119/**
Chunfeng Yunc776f2c2017-10-13 17:10:42 +0800120 * MTU3_DR_FORCE_NONE: automatically switch host and periperal mode
121 * by IDPIN signal.
122 * MTU3_DR_FORCE_HOST: force to enter host mode and override OTG
123 * IDPIN signal.
124 * MTU3_DR_FORCE_DEVICE: force to enter peripheral mode.
125 */
126enum mtu3_dr_force_mode {
127 MTU3_DR_FORCE_NONE = 0,
128 MTU3_DR_FORCE_HOST,
129 MTU3_DR_FORCE_DEVICE,
130};
131
132/**
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800133 * @base: the base address of fifo
134 * @limit: the bitmap size in bits
135 * @bitmap: fifo bitmap in unit of @MTU3_EP_FIFO_UNIT
136 */
137struct mtu3_fifo_info {
138 u32 base;
139 u32 limit;
140 DECLARE_BITMAP(bitmap, MTU3_FIFO_BIT_SIZE);
141};
142
143/**
144 * General Purpose Descriptor (GPD):
145 * The format of TX GPD is a little different from RX one.
146 * And the size of GPD is 16 bytes.
147 *
148 * @flag:
149 * bit0: Hardware Own (HWO)
150 * bit1: Buffer Descriptor Present (BDP), always 0, BD is not supported
151 * bit2: Bypass (BPS), 1: HW skips this GPD if HWO = 1
152 * bit7: Interrupt On Completion (IOC)
153 * @chksum: This is used to validate the contents of this GPD;
154 * If TXQ_CS_EN / RXQ_CS_EN bit is set, an interrupt is issued
155 * when checksum validation fails;
156 * Checksum value is calculated over the 16 bytes of the GPD by default;
157 * @data_buf_len (RX ONLY): This value indicates the length of
158 * the assigned data buffer
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +0800159 * @tx_ext_addr (TX ONLY): [3:0] are 4 extension bits of @buffer,
160 * [7:4] are 4 extension bits of @next_gpd
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800161 * @next_gpd: Physical address of the next GPD
162 * @buffer: Physical address of the data buffer
163 * @buf_len:
164 * (TX): This value indicates the length of the assigned data buffer
165 * (RX): The total length of data received
166 * @ext_len: reserved
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +0800167 * @rx_ext_addr(RX ONLY): [3:0] are 4 extension bits of @buffer,
168 * [7:4] are 4 extension bits of @next_gpd
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800169 * @ext_flag:
170 * bit5 (TX ONLY): Zero Length Packet (ZLP),
171 */
172struct qmu_gpd {
173 __u8 flag;
174 __u8 chksum;
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +0800175 union {
176 __le16 data_buf_len;
177 __le16 tx_ext_addr;
178 };
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800179 __le32 next_gpd;
180 __le32 buffer;
181 __le16 buf_len;
Chunfeng Yun1a46dfe2017-10-13 17:10:41 +0800182 union {
183 __u8 ext_len;
184 __u8 rx_ext_addr;
185 };
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800186 __u8 ext_flag;
187} __packed;
188
189/**
190* dma: physical base address of GPD segment
191* start: virtual base address of GPD segment
192* end: the last GPD element
193* enqueue: the first empty GPD to use
194* dequeue: the first completed GPD serviced by ISR
195* NOTE: the size of GPD ring should be >= 2
196*/
197struct mtu3_gpd_ring {
198 dma_addr_t dma;
199 struct qmu_gpd *start;
200 struct qmu_gpd *end;
201 struct qmu_gpd *enqueue;
202 struct qmu_gpd *dequeue;
203};
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800204
205/**
206* @vbus: vbus 5V used by host mode
207* @edev: external connector used to detect vbus and iddig changes
208* @vbus_nb: notifier for vbus detection
209* @vbus_nb: notifier for iddig(idpin) detection
210* @extcon_reg_dwork: delay work for extcon notifier register, waiting for
211* xHCI driver initialization, it's necessary for system bootup
212* as device.
213* @is_u3_drd: whether port0 supports usb3.0 dual-role device or not
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800214* @manual_drd_enabled: it's true when supports dual-role device by debugfs
215* to switch host/device modes depending on user input.
216*/
217struct otg_switch_mtk {
218 struct regulator *vbus;
219 struct extcon_dev *edev;
220 struct notifier_block vbus_nb;
221 struct notifier_block id_nb;
222 struct delayed_work extcon_reg_dwork;
223 bool is_u3_drd;
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800224 bool manual_drd_enabled;
225};
226
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800227/**
228 * @mac_base: register base address of device MAC, exclude xHCI's
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800229 * @ippc_base: register base address of IP Power and Clock interface (IPPC)
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800230 * @vusb33: usb3.3V shared by device/host IP
231 * @sys_clk: system clock of mtu3, shared by device/host IP
Chunfeng Yuna316da82017-10-13 17:10:40 +0800232 * @ref_clk: reference clock
233 * @mcu_clk: mcu_bus_ck clock for AHB bus etc
234 * @dma_clk: dma_bus_ck clock for AXI bus etc
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800235 * @dr_mode: works in which mode:
236 * host only, device only or dual-role mode
237 * @u2_ports: number of usb2.0 host ports
238 * @u3_ports: number of usb3.0 host ports
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800239 * @u3p_dis_msk: mask of disabling usb3 ports, for example, bit0==1 to
240 * disable u3port0, bit1==1 to disable u3port1,... etc
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800241 * @dbgfs_root: only used when supports manual dual-role switch via debugfs
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800242 * @wakeup_en: it's true when supports remote wakeup in host mode
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800243 */
244struct ssusb_mtk {
245 struct device *dev;
246 struct mtu3 *u3d;
247 void __iomem *mac_base;
248 void __iomem *ippc_base;
249 struct phy **phys;
250 int num_phys;
251 /* common power & clock */
252 struct regulator *vusb33;
253 struct clk *sys_clk;
Chunfeng Yun4d70d0c2017-01-18 14:08:23 +0800254 struct clk *ref_clk;
Chunfeng Yuna316da82017-10-13 17:10:40 +0800255 struct clk *mcu_clk;
256 struct clk *dma_clk;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800257 /* otg */
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800258 struct otg_switch_mtk otg_switch;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800259 enum usb_dr_mode dr_mode;
260 bool is_host;
261 int u2_ports;
262 int u3_ports;
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800263 int u3p_dis_msk;
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800264 struct dentry *dbgfs_root;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800265 /* usb wakeup for host mode */
266 bool wakeup_en;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800267 struct regmap *pericfg;
268};
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800269
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;
Chunfeng Yuna29de312016-10-19 10:28:24 +0800290 const struct usb_ss_ep_comp_descriptor *comp_desc;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800291 const struct usb_endpoint_descriptor *desc;
292
293 int flags;
294 u8 wedged;
295 u8 busy;
296};
297
298struct mtu3_request {
299 struct usb_request request;
300 struct list_head list;
301 struct mtu3_ep *mep;
302 struct mtu3 *mtu;
303 struct qmu_gpd *gpd;
304 int epnum;
305};
306
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800307static inline struct ssusb_mtk *dev_to_ssusb(struct device *dev)
308{
309 return dev_get_drvdata(dev);
310}
311
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800312/**
313 * struct mtu3 - device driver instance data.
Chunfeng Yuna29de312016-10-19 10:28:24 +0800314 * @slot: MTU3_U2_IP_SLOT_DEFAULT for U2 IP only,
315 * MTU3_U3_IP_SLOT_DEFAULT for U3 IP
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800316 * @may_wakeup: means device's remote wakeup is enabled
317 * @is_self_powered: is reported in device status and the config descriptor
Chunfeng Yunfe7c9942017-07-25 16:10:22 +0800318 * @delayed_status: true when function drivers ask for delayed status
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800319 * @ep0_req: dummy request used while handling standard USB requests
320 * for GET_STATUS and SET_SEL
321 * @setup_buf: ep0 response buffer for GET_STATUS and SET_SEL requests
322 */
323struct mtu3 {
324 spinlock_t lock;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800325 struct ssusb_mtk *ssusb;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800326 struct device *dev;
327 void __iomem *mac_base;
328 void __iomem *ippc_base;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800329 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];
Chunfeng Yuna29de312016-10-19 10:28:24 +0800348 u32 max_speed;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800349
350 unsigned is_active:1;
351 unsigned may_wakeup:1;
352 unsigned is_self_powered:1;
353 unsigned test_mode:1;
354 unsigned softconnect:1;
Chunfeng Yuna29de312016-10-19 10:28:24 +0800355 unsigned u1_enable:1;
356 unsigned u2_enable:1;
357 unsigned is_u3_ip:1;
Chunfeng Yunfe7c9942017-07-25 16:10:22 +0800358 unsigned delayed_status:1;
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800359
360 u8 address;
361 u8 test_mode_nr;
362 u32 hw_version;
363};
364
365static inline struct mtu3 *gadget_to_mtu3(struct usb_gadget *g)
366{
367 return container_of(g, struct mtu3, g);
368}
369
370static inline int is_first_entry(const struct list_head *list,
371 const struct list_head *head)
372{
373 return list_is_last(head, list);
374}
375
376static inline struct mtu3_request *to_mtu3_request(struct usb_request *req)
377{
378 return req ? container_of(req, struct mtu3_request, request) : NULL;
379}
380
381static inline struct mtu3_ep *to_mtu3_ep(struct usb_ep *ep)
382{
383 return ep ? container_of(ep, struct mtu3_ep, ep) : NULL;
384}
385
386static inline struct mtu3_request *next_request(struct mtu3_ep *mep)
387{
Masahiro Yamada9b4632e2017-05-21 02:05:31 +0900388 return list_first_entry_or_null(&mep->req_list, struct mtu3_request,
389 list);
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800390}
391
392static inline void mtu3_writel(void __iomem *base, u32 offset, u32 data)
393{
394 writel(data, base + offset);
395}
396
397static inline u32 mtu3_readl(void __iomem *base, u32 offset)
398{
399 return readl(base + offset);
400}
401
402static inline void mtu3_setbits(void __iomem *base, u32 offset, u32 bits)
403{
404 void __iomem *addr = base + offset;
405 u32 tmp = readl(addr);
406
407 writel((tmp | (bits)), addr);
408}
409
410static inline void mtu3_clrbits(void __iomem *base, u32 offset, u32 bits)
411{
412 void __iomem *addr = base + offset;
413 u32 tmp = readl(addr);
414
415 writel((tmp & ~(bits)), addr);
416}
417
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800418int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks);
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800419struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
420void mtu3_free_request(struct usb_ep *ep, struct usb_request *req);
421void mtu3_req_complete(struct mtu3_ep *mep,
422 struct usb_request *req, int status);
423
424int mtu3_config_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
425 int interval, int burst, int mult);
426void mtu3_deconfig_ep(struct mtu3 *mtu, struct mtu3_ep *mep);
427void mtu3_ep_stall_set(struct mtu3_ep *mep, bool set);
428void mtu3_ep0_setup(struct mtu3 *mtu);
429void mtu3_start(struct mtu3 *mtu);
430void mtu3_stop(struct mtu3 *mtu);
Chunfeng Yuna29de312016-10-19 10:28:24 +0800431void mtu3_dev_on_off(struct mtu3 *mtu, int is_on);
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800432
433int mtu3_gadget_setup(struct mtu3 *mtu);
434void mtu3_gadget_cleanup(struct mtu3 *mtu);
435void mtu3_gadget_reset(struct mtu3 *mtu);
436void mtu3_gadget_suspend(struct mtu3 *mtu);
437void mtu3_gadget_resume(struct mtu3 *mtu);
438void mtu3_gadget_disconnect(struct mtu3 *mtu);
Chunfeng Yundf2069a2016-10-19 10:28:23 +0800439
440irqreturn_t mtu3_ep0_isr(struct mtu3 *mtu);
441extern const struct usb_ep_ops mtu3_ep0_ops;
442
443#endif