1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef __LINUX_BDC_H__
16#define __LINUX_BDC_H__
17
18#include <linux/kernel.h>
19#include <linux/usb.h>
20#include <linux/device.h>
21#include <linux/spinlock.h>
22#include <linux/list.h>
23#include <linux/dma-mapping.h>
24#include <linux/mm.h>
25#include <linux/debugfs.h>
26#include <linux/usb/ch9.h>
27#include <linux/usb/gadget.h>
28#include <asm/unaligned.h>
29
30#define BRCM_BDC_NAME "bdc_usb3"
31#define BRCM_BDC_DESC "BDC device controller driver"
32
33#define DMA_ADDR_INVALID (~(dma_addr_t)0)
34
35
36#define BDC_CMD_TIMEOUT 1000
37
38#define BDC_COP_TIMEOUT 500
39
40
41
42
43
44#define EP0_RESPONSE_BUFF 6
45
46#define EP0_MAX_PKT_SIZE 512
47
48
49#define NUM_SR_ENTRIES 64
50
51
52#define NUM_BDS_PER_TABLE 32
53
54
55#define NUM_TABLES 2
56
57
58#define NUM_TABLES_ISOCH 6
59
60
61#define U1_TIMEOUT 0xf8
62
63
64#define INT_CLS 500
65
66
67
68#define BDC_BDCCFG0 0x00
69#define BDC_BDCCFG1 0x04
70#define BDC_BDCCAP0 0x08
71#define BDC_BDCCAP1 0x0c
72#define BDC_CMDPAR0 0x10
73#define BDC_CMDPAR1 0x14
74#define BDC_CMDPAR2 0x18
75#define BDC_CMDSC 0x1c
76#define BDC_USPC 0x20
77#define BDC_USPPMS 0x28
78#define BDC_USPPM2 0x2c
79#define BDC_SPBBAL 0x38
80#define BDC_SPBBAH 0x3c
81#define BDC_BDCSC 0x40
82#define BDC_XSFNTF 0x4c
83
84#define BDC_DVCSA 0x50
85#define BDC_DVCSB 0x54
86#define BDC_EPSTS0(n) (0x60 + (n * 0x10))
87#define BDC_EPSTS1(n) (0x64 + (n * 0x10))
88#define BDC_EPSTS2(n) (0x68 + (n * 0x10))
89#define BDC_EPSTS3(n) (0x6c + (n * 0x10))
90#define BDC_EPSTS4(n) (0x70 + (n * 0x10))
91#define BDC_EPSTS5(n) (0x74 + (n * 0x10))
92#define BDC_EPSTS6(n) (0x78 + (n * 0x10))
93#define BDC_EPSTS7(n) (0x7c + (n * 0x10))
94#define BDC_SRRBAL(n) (0x200 + (n * 0x10))
95#define BDC_SRRBAH(n) (0x204 + (n * 0x10))
96#define BDC_SRRINT(n) (0x208 + (n * 0x10))
97#define BDC_INTCTLS(n) (0x20c + (n * 0x10))
98
99
100#define BDC_FSCNOC 0xcd4
101#define BDC_FSCNIC 0xce4
102#define NUM_NCS(p) (p >> 28)
103
104
105
106#define BDC_PGS(p) (((p) & (0x7 << 8)) >> 8)
107#define BDC_SPB(p) (p & 0x7)
108
109
110#define BDC_P64 (1 << 0)
111
112
113#define BDC_CMD_FH 0xe
114#define BDC_CMD_DNC 0x6
115#define BDC_CMD_EPO 0x4
116#define BDC_CMD_BLA 0x3
117#define BDC_CMD_EPC 0x2
118#define BDC_CMD_DVC 0x1
119#define BDC_CMD_CWS (0x1 << 5)
120#define BDC_CMD_CST(p) (((p) & (0xf << 6))>>6)
121#define BDC_CMD_EPN(p) ((p & 0x1f) << 10)
122#define BDC_SUB_CMD_ADD (0x1 << 17)
123#define BDC_SUB_CMD_FWK (0x4 << 17)
124
125#define BDC_CMD_EPO_RST_SN (0x1 << 16)
126#define BDC_CMD_EP0_XSD (0x1 << 16)
127#define BDC_SUB_CMD_ADD_EP (0x1 << 17)
128#define BDC_SUB_CMD_DRP_EP (0x2 << 17)
129#define BDC_SUB_CMD_EP_STP (0x2 << 17)
130#define BDC_SUB_CMD_EP_STL (0x4 << 17)
131#define BDC_SUB_CMD_EP_RST (0x1 << 17)
132#define BDC_CMD_SRD (1 << 27)
133
134
135#define BDC_CMDS_SUCC 0x1
136#define BDC_CMDS_PARA 0x3
137#define BDC_CMDS_STAT 0x4
138#define BDC_CMDS_FAIL 0x5
139#define BDC_CMDS_INTL 0x6
140#define BDC_CMDS_BUSY 0xf
141
142
143#define EPT_SHIFT 22
144#define MP_SHIFT 10
145#define MB_SHIFT 6
146#define EPM_SHIFT 4
147
148
149#define BDC_VBC (1 << 31)
150#define BDC_PRC (1 << 30)
151#define BDC_PCE (1 << 29)
152#define BDC_CFC (1 << 28)
153#define BDC_PCC (1 << 27)
154#define BDC_PSC (1 << 26)
155#define BDC_VBS (1 << 25)
156#define BDC_PRS (1 << 24)
157#define BDC_PCS (1 << 23)
158#define BDC_PSP(p) (((p) & (0x7 << 20))>>20)
159#define BDC_SCN (1 << 8)
160#define BDC_SDC (1 << 7)
161#define BDC_SWS (1 << 4)
162
163#define BDC_USPSC_RW (BDC_SCN|BDC_SDC|BDC_SWS|0xf)
164#define BDC_PSP(p) (((p) & (0x7 << 20))>>20)
165
166#define BDC_SPEED_FS 0x1
167#define BDC_SPEED_LS 0x2
168#define BDC_SPEED_HS 0x3
169#define BDC_SPEED_SS 0x4
170
171#define BDC_PST(p) (p & 0xf)
172#define BDC_PST_MASK 0xf
173
174
175#define BDC_U2E (0x1 << 31)
176#define BDC_U1E (0x1 << 30)
177#define BDC_U2A (0x1 << 29)
178#define BDC_PORT_W1S (0x1 << 17)
179#define BDC_U1T(p) ((p) & 0xff)
180#define BDC_U2T(p) (((p) & 0xff) << 8)
181#define BDC_U1T_MASK 0xff
182
183
184
185#define BDC_HLE (1 << 16)
186
187
188#define BDC_COP_RST (1 << 29)
189#define BDC_COP_RUN (2 << 29)
190#define BDC_COP_STP (4 << 29)
191
192#define BDC_COP_MASK (BDC_COP_RST|BDC_COP_RUN|BDC_COP_STP)
193
194#define BDC_COS (1 << 28)
195#define BDC_CSTS(p) (((p) & (0x7 << 20)) >> 20)
196#define BDC_MASK_MCW (1 << 7)
197#define BDC_GIE (1 << 1)
198#define BDC_GIP (1 << 0)
199
200#define BDC_HLT 1
201#define BDC_NOR 2
202#define BDC_OIP 7
203
204
205#define BD_TYPE_BITMASK (0xf)
206#define BD_CHAIN 0xf
207
208#define BD_TFS_SHIFT 4
209#define BD_SOT (1 << 26)
210#define BD_EOT (1 << 27)
211#define BD_ISP (1 << 29)
212#define BD_IOC (1 << 30)
213#define BD_SBF (1 << 31)
214
215#define BD_INTR_TARGET(p) (((p) & 0x1f) << 27)
216
217#define BDC_SRR_RWS (1 << 4)
218#define BDC_SRR_RST (1 << 3)
219#define BDC_SRR_ISR (1 << 2)
220#define BDC_SRR_IE (1 << 1)
221#define BDC_SRR_IP (1 << 0)
222#define BDC_SRR_EPI(p) (((p) & (0xff << 24)) >> 24)
223#define BDC_SRR_DPI(p) (((p) & (0xff << 16)) >> 16)
224#define BDC_SRR_DPI_MASK 0x00ff0000
225
226#define MARK_CHAIN_BD (BD_CHAIN|BD_EOT|BD_SOT)
227
228
229#define BD_DIR_IN (1 << 25)
230
231#define BDC_PTC_MASK 0xf0000000
232
233
234#define SR_XSF 0
235#define SR_USPC 4
236#define SR_BD_LEN(p) (p & 0xffffff)
237
238#define XSF_SUCC 0x1
239#define XSF_SHORT 0x3
240#define XSF_BABB 0x4
241#define XSF_SETUP_RECV 0x6
242#define XSF_DATA_START 0x7
243#define XSF_STATUS_START 0x8
244
245#define XSF_STS(p) (((p) >> 28) & 0xf)
246
247
248#define BD_LEN(p) ((p) & 0x1ffff)
249#define BD_LTF (1 << 25)
250#define BD_TYPE_DS 0x1
251#define BD_TYPE_SS 0x2
252
253#define BDC_EP_ENABLED (1 << 0)
254#define BDC_EP_STALL (1 << 1)
255#define BDC_EP_STOP (1 << 2)
256
257
258#define BD_MAX_BUFF_SIZE (1 << 16)
259
260#define MAX_XFR_LEN 16777215
261
262
263#define DEV_NOTF_TYPE 6
264#define FWK_SUBTYPE 1
265#define TRA_PACKET 4
266
267#define to_bdc_ep(e) container_of(e, struct bdc_ep, usb_ep)
268#define to_bdc_req(r) container_of(r, struct bdc_req, usb_req)
269#define gadget_to_bdc(g) container_of(g, struct bdc, gadget)
270
271
272#define BDC_TNOTIFY 2500
273
274#define REMOTE_WAKEUP_ISSUED (1 << 16)
275#define DEVICE_SUSPENDED (1 << 17)
276#define FUNC_WAKE_ISSUED (1 << 18)
277#define REMOTE_WAKE_ENABLE (1 << USB_DEVICE_REMOTE_WAKEUP)
278
279
280#define DEVSTATUS_CLEAR (1 << USB_DEVICE_SELF_POWERED)
281
282
283
284struct bdc_bd {
285 __le32 offset[4];
286};
287
288
289struct bdc_sr {
290 __le32 offset[4];
291};
292
293
294struct bd_table {
295 struct bdc_bd *start_bd;
296
297 dma_addr_t dma;
298};
299
300
301
302
303
304
305
306struct bd_list {
307
308 struct bd_table **bd_table_array;
309
310 int num_tabs;
311
312 int max_bdi;
313
314 int eqp_bdi;
315
316 int hwd_bdi;
317
318 int num_bds_table;
319};
320
321struct bdc_req;
322
323
324struct bd_transfer {
325 struct bdc_req *req;
326
327 int start_bdi;
328
329 int next_hwd_bdi;
330
331 int num_bds;
332};
333
334
335
336
337
338struct bdc_req {
339 struct usb_request usb_req;
340 struct list_head queue;
341 struct bdc_ep *ep;
342
343 struct bd_transfer bd_xfr;
344 int epnum;
345};
346
347
348struct bdc_scratchpad {
349 dma_addr_t sp_dma;
350 void *buff;
351 u32 size;
352};
353
354
355struct bdc_ep {
356 struct usb_ep usb_ep;
357 struct list_head queue;
358 struct bdc *bdc;
359 u8 ep_type;
360 u8 dir;
361 u8 ep_num;
362 const struct usb_ss_ep_comp_descriptor *comp_desc;
363 const struct usb_endpoint_descriptor *desc;
364 unsigned int flags;
365 char name[20];
366
367 struct bd_list bd_list;
368
369
370
371
372 bool ignore_next_sr;
373};
374
375
376struct bdc_cmd_params {
377 u32 param2;
378 u32 param1;
379 u32 param0;
380};
381
382
383struct srr {
384 struct bdc_sr *sr_bds;
385 u16 eqp_index;
386 u16 dqp_index;
387 dma_addr_t dma_addr;
388};
389
390
391enum bdc_ep0_state {
392 WAIT_FOR_SETUP = 0,
393 WAIT_FOR_DATA_START,
394 WAIT_FOR_DATA_XMIT,
395 WAIT_FOR_STATUS_START,
396 WAIT_FOR_STATUS_XMIT,
397 STATUS_PENDING
398};
399
400
401enum bdc_link_state {
402 BDC_LINK_STATE_U0 = 0x00,
403 BDC_LINK_STATE_U3 = 0x03,
404 BDC_LINK_STATE_RX_DET = 0x05,
405 BDC_LINK_STATE_RESUME = 0x0f
406};
407
408
409struct bdc {
410 struct usb_gadget gadget;
411 struct usb_gadget_driver *gadget_driver;
412 struct device *dev;
413
414 spinlock_t lock;
415
416
417 unsigned int num_eps;
418
419
420
421
422 struct bdc_ep **bdc_ep_array;
423 void __iomem *regs;
424 struct bdc_scratchpad scratchpad;
425 u32 sp_buff_size;
426
427 struct srr srr;
428
429 struct usb_ctrlrequest setup_pkt;
430 struct bdc_req ep0_req;
431 struct bdc_req status_req;
432 enum bdc_ep0_state ep0_state;
433 bool delayed_status;
434 bool zlp_needed;
435 bool reinit;
436 bool pullup;
437
438 u32 devstatus;
439 int irq;
440 void *mem;
441 u32 dev_addr;
442
443 struct dma_pool *bd_table_pool;
444 u8 test_mode;
445
446 void (*sr_handler[2])(struct bdc *, struct bdc_sr *);
447
448 void (*sr_xsf_ep0[3])(struct bdc *, struct bdc_sr *);
449
450 unsigned char ep0_response_buff[EP0_RESPONSE_BUFF];
451
452
453
454
455
456 struct delayed_work func_wake_notify;
457};
458
459static inline u32 bdc_readl(void __iomem *base, u32 offset)
460{
461 return readl(base + offset);
462}
463
464static inline void bdc_writel(void __iomem *base, u32 offset, u32 value)
465{
466 writel(value, base + offset);
467}
468
469
470void bdc_notify_xfr(struct bdc *, u32);
471void bdc_softconn(struct bdc *);
472void bdc_softdisconn(struct bdc *);
473int bdc_run(struct bdc *);
474int bdc_stop(struct bdc *);
475int bdc_reset(struct bdc *);
476int bdc_udc_init(struct bdc *);
477void bdc_udc_exit(struct bdc *);
478int bdc_reinit(struct bdc *);
479
480
481
482void bdc_sr_uspc(struct bdc *, struct bdc_sr *);
483
484void bdc_sr_xsf(struct bdc *, struct bdc_sr *);
485
486void bdc_xsf_ep0_setup_recv(struct bdc *, struct bdc_sr *);
487void bdc_xsf_ep0_data_start(struct bdc *, struct bdc_sr *);
488void bdc_xsf_ep0_status_start(struct bdc *, struct bdc_sr *);
489
490#endif
491