1
2#ifndef __ASPEED_VHUB_H
3#define __ASPEED_VHUB_H
4
5#include <linux/usb.h>
6#include <linux/usb/ch11.h>
7
8
9
10
11
12
13
14#define AST_VHUB_CTRL 0x00
15#define AST_VHUB_CONF 0x04
16#define AST_VHUB_IER 0x08
17#define AST_VHUB_ISR 0x0C
18#define AST_VHUB_EP_ACK_IER 0x10
19#define AST_VHUB_EP_NACK_IER 0x14
20#define AST_VHUB_EP_ACK_ISR 0x18
21#define AST_VHUB_EP_NACK_ISR 0x1C
22#define AST_VHUB_SW_RESET 0x20
23#define AST_VHUB_USBSTS 0x24
24#define AST_VHUB_EP_TOGGLE 0x28
25#define AST_VHUB_ISO_FAIL_ACC 0x2C
26#define AST_VHUB_EP0_CTRL 0x30
27#define AST_VHUB_EP0_DATA 0x34
28#define AST_VHUB_EP1_CTRL 0x38
29#define AST_VHUB_EP1_STS_CHG 0x3C
30#define AST_VHUB_SETUP0 0x80
31#define AST_VHUB_SETUP1 0x84
32
33
34#define VHUB_CTRL_PHY_CLK (1 << 31)
35#define VHUB_CTRL_PHY_LOOP_TEST (1 << 25)
36#define VHUB_CTRL_DN_PWN (1 << 24)
37#define VHUB_CTRL_DP_PWN (1 << 23)
38#define VHUB_CTRL_LONG_DESC (1 << 18)
39#define VHUB_CTRL_ISO_RSP_CTRL (1 << 17)
40#define VHUB_CTRL_SPLIT_IN (1 << 16)
41#define VHUB_CTRL_LOOP_T_RESULT (1 << 15)
42#define VHUB_CTRL_LOOP_T_STS (1 << 14)
43#define VHUB_CTRL_PHY_BIST_RESULT (1 << 13)
44#define VHUB_CTRL_PHY_BIST_CTRL (1 << 12)
45#define VHUB_CTRL_PHY_RESET_DIS (1 << 11)
46#define VHUB_CTRL_SET_TEST_MODE(x) ((x) << 8)
47#define VHUB_CTRL_MANUAL_REMOTE_WAKEUP (1 << 4)
48#define VHUB_CTRL_AUTO_REMOTE_WAKEUP (1 << 3)
49#define VHUB_CTRL_CLK_STOP_SUSPEND (1 << 2)
50#define VHUB_CTRL_FULL_SPEED_ONLY (1 << 1)
51#define VHUB_CTRL_UPSTREAM_CONNECT (1 << 0)
52
53
54#define VHUB_IRQ_DEV1_BIT 9
55#define VHUB_IRQ_USB_CMD_DEADLOCK (1 << 18)
56#define VHUB_IRQ_EP_POOL_NAK (1 << 17)
57#define VHUB_IRQ_EP_POOL_ACK_STALL (1 << 16)
58#define VHUB_IRQ_DEVICE1 (1 << (VHUB_IRQ_DEV1_BIT))
59#define VHUB_IRQ_BUS_RESUME (1 << 8)
60#define VHUB_IRQ_BUS_SUSPEND (1 << 7)
61#define VHUB_IRQ_BUS_RESET (1 << 6)
62#define VHUB_IRQ_HUB_EP1_IN_DATA_ACK (1 << 5)
63#define VHUB_IRQ_HUB_EP0_IN_DATA_NAK (1 << 4)
64#define VHUB_IRQ_HUB_EP0_IN_ACK_STALL (1 << 3)
65#define VHUB_IRQ_HUB_EP0_OUT_NAK (1 << 2)
66#define VHUB_IRQ_HUB_EP0_OUT_ACK_STALL (1 << 1)
67#define VHUB_IRQ_HUB_EP0_SETUP (1 << 0)
68#define VHUB_IRQ_ACK_ALL 0x1ff
69
70
71#define VHUB_DEV_IRQ(n) (VHUB_IRQ_DEVICE1 << (n))
72
73
74#define VHUB_SW_RESET_EP_POOL (1 << 9)
75#define VHUB_SW_RESET_DMA_CONTROLLER (1 << 8)
76#define VHUB_SW_RESET_DEVICE5 (1 << 5)
77#define VHUB_SW_RESET_DEVICE4 (1 << 4)
78#define VHUB_SW_RESET_DEVICE3 (1 << 3)
79#define VHUB_SW_RESET_DEVICE2 (1 << 2)
80#define VHUB_SW_RESET_DEVICE1 (1 << 1)
81#define VHUB_SW_RESET_ROOT_HUB (1 << 0)
82
83
84#define VHUB_EP_IRQ(n) (1 << (n))
85
86
87#define VHUB_USBSTS_HISPEED (1 << 27)
88
89
90#define VHUB_EP_TOGGLE_VALUE (1 << 8)
91#define VHUB_EP_TOGGLE_SET_EPNUM(x) ((x) & 0x1f)
92
93
94#define VHUB_EP0_CTRL_STALL (1 << 0)
95#define VHUB_EP0_TX_BUFF_RDY (1 << 1)
96#define VHUB_EP0_RX_BUFF_RDY (1 << 2)
97#define VHUB_EP0_RX_LEN(x) (((x) >> 16) & 0x7f)
98#define VHUB_EP0_SET_TX_LEN(x) (((x) & 0x7f) << 8)
99
100
101#define VHUB_EP1_CTRL_RESET_TOGGLE (1 << 2)
102#define VHUB_EP1_CTRL_STALL (1 << 1)
103#define VHUB_EP1_CTRL_ENABLE (1 << 0)
104
105
106
107
108
109
110#define AST_VHUB_DEV_EN_CTRL 0x00
111#define AST_VHUB_DEV_ISR 0x04
112#define AST_VHUB_DEV_EP0_CTRL 0x08
113#define AST_VHUB_DEV_EP0_DATA 0x0c
114
115
116#define VHUB_DEV_EN_SET_ADDR(x) ((x) << 8)
117#define VHUB_DEV_EN_ADDR_MASK ((0xff) << 8)
118#define VHUB_DEV_EN_EP0_NAK_IRQEN (1 << 6)
119#define VHUB_DEV_EN_EP0_IN_ACK_IRQEN (1 << 5)
120#define VHUB_DEV_EN_EP0_OUT_NAK_IRQEN (1 << 4)
121#define VHUB_DEV_EN_EP0_OUT_ACK_IRQEN (1 << 3)
122#define VHUB_DEV_EN_EP0_SETUP_IRQEN (1 << 2)
123#define VHUB_DEV_EN_SPEED_SEL_HIGH (1 << 1)
124#define VHUB_DEV_EN_ENABLE_PORT (1 << 0)
125
126
127#define VHUV_DEV_IRQ_EP0_IN_DATA_NACK (1 << 4)
128#define VHUV_DEV_IRQ_EP0_IN_ACK_STALL (1 << 3)
129#define VHUV_DEV_IRQ_EP0_OUT_DATA_NACK (1 << 2)
130#define VHUV_DEV_IRQ_EP0_OUT_ACK_STALL (1 << 1)
131#define VHUV_DEV_IRQ_EP0_SETUP (1 << 0)
132
133
134
135
136
137
138#define VHUB_DEV_EP0_CTRL_STALL VHUB_EP0_CTRL_STALL
139#define VHUB_DEV_EP0_TX_BUFF_RDY VHUB_EP0_TX_BUFF_RDY
140#define VHUB_DEV_EP0_RX_BUFF_RDY VHUB_EP0_RX_BUFF_RDY
141#define VHUB_DEV_EP0_RX_LEN(x) VHUB_EP0_RX_LEN(x)
142#define VHUB_DEV_EP0_SET_TX_LEN(x) VHUB_EP0_SET_TX_LEN(x)
143
144
145
146
147
148
149
150#define AST_VHUB_EP_CONFIG 0x00
151#define AST_VHUB_EP_DMA_CTLSTAT 0x04
152#define AST_VHUB_EP_DESC_BASE 0x08
153#define AST_VHUB_EP_DESC_STATUS 0x0C
154
155
156#define VHUB_EP_CFG_SET_MAX_PKT(x) (((x) & 0x3ff) << 16)
157#define VHUB_EP_CFG_AUTO_DATA_DISABLE (1 << 13)
158#define VHUB_EP_CFG_STALL_CTRL (1 << 12)
159#define VHUB_EP_CFG_SET_EP_NUM(x) (((x) & 0xf) << 8)
160#define VHUB_EP_CFG_SET_TYPE(x) ((x) << 5)
161#define EP_TYPE_OFF 0
162#define EP_TYPE_BULK 1
163#define EP_TYPE_INT 2
164#define EP_TYPE_ISO 3
165#define VHUB_EP_CFG_DIR_OUT (1 << 4)
166#define VHUB_EP_CFG_SET_DEV(x) ((x) << 1)
167#define VHUB_EP_CFG_ENABLE (1 << 0)
168
169
170#define VHUB_EP_DMA_PROC_STATUS(x) (((x) >> 4) & 0xf)
171#define EP_DMA_PROC_RX_IDLE 0
172#define EP_DMA_PROC_TX_IDLE 8
173#define VHUB_EP_DMA_IN_LONG_MODE (1 << 3)
174#define VHUB_EP_DMA_OUT_CONTIG_MODE (1 << 3)
175#define VHUB_EP_DMA_CTRL_RESET (1 << 2)
176#define VHUB_EP_DMA_SINGLE_STAGE (1 << 1)
177#define VHUB_EP_DMA_DESC_MODE (1 << 0)
178
179
180#define VHUB_EP_DMA_SET_TX_SIZE(x) ((x) << 16)
181#define VHUB_EP_DMA_TX_SIZE(x) (((x) >> 16) & 0x7ff)
182#define VHUB_EP_DMA_RPTR(x) (((x) >> 8) & 0xff)
183#define VHUB_EP_DMA_SET_RPTR(x) (((x) & 0xff) << 8)
184#define VHUB_EP_DMA_SET_CPU_WPTR(x) (x)
185#define VHUB_EP_DMA_SINGLE_KICK (1 << 0)
186
187
188
189
190
191
192
193
194#define VHUB_DSC1_IN_INTERRUPT (1 << 31)
195#define VHUB_DSC1_IN_SPID_DATA0 (0 << 14)
196#define VHUB_DSC1_IN_SPID_DATA2 (1 << 14)
197#define VHUB_DSC1_IN_SPID_DATA1 (2 << 14)
198#define VHUB_DSC1_IN_SPID_MDATA (3 << 14)
199#define VHUB_DSC1_IN_SET_LEN(x) ((x) & 0xfff)
200#define VHUB_DSC1_IN_LEN(x) ((x) & 0xfff)
201
202
203
204
205
206
207
208
209
210
211
212
213#define AST_VHUB_NUM_GEN_EPs 15
214#define AST_VHUB_NUM_PORTS 5
215#define AST_VHUB_EP0_MAX_PACKET 64
216#define AST_VHUB_EPn_MAX_PACKET 1024
217#define AST_VHUB_DESCS_COUNT 256
218
219
220
221struct ast_vhub;
222struct ast_vhub_dev;
223
224
225
226
227
228struct ast_vhub_desc {
229 __le32 w0;
230 __le32 w1;
231};
232
233
234struct ast_vhub_req {
235 struct usb_request req;
236 struct list_head queue;
237
238
239 unsigned int act_count;
240
241
242
243
244
245 int last_desc;
246
247
248 bool active : 1;
249
250
251 bool internal : 1;
252};
253#define to_ast_req(__ureq) container_of(__ureq, struct ast_vhub_req, req)
254
255
256enum ep0_state {
257 ep0_state_token,
258 ep0_state_data,
259 ep0_state_status,
260 ep0_state_stall,
261};
262
263
264
265
266
267
268struct ast_vhub_ep {
269 struct usb_ep ep;
270
271
272 struct list_head queue;
273
274
275 unsigned int d_idx;
276
277
278 struct ast_vhub_dev *dev;
279
280
281 struct ast_vhub *vhub;
282
283
284
285
286
287 void *buf;
288 dma_addr_t buf_dma;
289
290
291 union {
292
293 struct {
294
295
296
297
298
299 void __iomem *ctlstat;
300 void __iomem *setup;
301
302
303 enum ep0_state state;
304 bool dir_in;
305
306
307 struct ast_vhub_req req;
308 } ep0;
309
310
311 struct {
312
313 void __iomem *regs;
314
315
316 unsigned int g_idx;
317
318
319 struct ast_vhub_desc *descs;
320 dma_addr_t descs_dma;
321 unsigned int d_next;
322 unsigned int d_last;
323 unsigned int dma_conf;
324
325
326 unsigned int chunk_max;
327
328
329 bool is_in : 1;
330 bool is_iso : 1;
331 bool stalled : 1;
332 bool wedged : 1;
333 bool enabled : 1;
334 bool desc_mode : 1;
335 } epn;
336 };
337};
338#define to_ast_ep(__uep) container_of(__uep, struct ast_vhub_ep, ep)
339
340
341struct ast_vhub_dev {
342 struct ast_vhub *vhub;
343 void __iomem *regs;
344
345
346 unsigned int index;
347 const char *name;
348
349
350 struct device *port_dev;
351
352
353 struct usb_gadget gadget;
354 struct usb_gadget_driver *driver;
355 bool registered : 1;
356 bool wakeup_en : 1;
357 bool enabled : 1;
358
359
360 struct ast_vhub_ep ep0;
361 struct ast_vhub_ep **epns;
362 u32 max_epns;
363
364};
365#define to_ast_dev(__g) container_of(__g, struct ast_vhub_dev, gadget)
366
367
368struct ast_vhub_port {
369
370 u16 status;
371 u16 change;
372
373
374 struct ast_vhub_dev dev;
375};
376
377struct ast_vhub_full_cdesc {
378 struct usb_config_descriptor cfg;
379 struct usb_interface_descriptor intf;
380 struct usb_endpoint_descriptor ep;
381} __packed;
382
383
384struct ast_vhub {
385 struct platform_device *pdev;
386 void __iomem *regs;
387 int irq;
388 spinlock_t lock;
389 struct work_struct wake_work;
390 struct clk *clk;
391
392
393 void *ep0_bufs;
394 dma_addr_t ep0_bufs_dma;
395
396
397 struct ast_vhub_ep ep0;
398
399
400 bool ep1_stalled : 1;
401
402
403 struct ast_vhub_port *ports;
404 u32 max_ports;
405 u32 port_irq_mask;
406
407
408 struct ast_vhub_ep *epns;
409 u32 max_epns;
410
411
412 bool suspended : 1;
413
414
415 bool wakeup_en : 1;
416
417
418 bool force_usb1 : 1;
419
420
421 unsigned int speed;
422
423
424 struct usb_device_descriptor vhub_dev_desc;
425 struct ast_vhub_full_cdesc vhub_conf_desc;
426 struct usb_hub_descriptor vhub_hub_desc;
427 struct list_head vhub_str_desc;
428};
429
430
431enum std_req_rc {
432 std_req_stall = -1,
433 std_req_complete = 0,
434 std_req_data = 1,
435 std_req_driver = 2,
436};
437
438#ifdef CONFIG_USB_GADGET_VERBOSE
439#define UDCVDBG(u, fmt...) dev_dbg(&(u)->pdev->dev, fmt)
440
441#define EPVDBG(ep, fmt, ...) do { \
442 dev_dbg(&(ep)->vhub->pdev->dev, \
443 "%s:EP%d " fmt, \
444 (ep)->dev ? (ep)->dev->name : "hub", \
445 (ep)->d_idx, ##__VA_ARGS__); \
446 } while(0)
447
448#define DVDBG(d, fmt, ...) do { \
449 dev_dbg(&(d)->vhub->pdev->dev, \
450 "%s " fmt, (d)->name, \
451 ##__VA_ARGS__); \
452 } while(0)
453
454#else
455#define UDCVDBG(u, fmt...) do { } while(0)
456#define EPVDBG(ep, fmt, ...) do { } while(0)
457#define DVDBG(d, fmt, ...) do { } while(0)
458#endif
459
460#ifdef CONFIG_USB_GADGET_DEBUG
461#define UDCDBG(u, fmt...) dev_dbg(&(u)->pdev->dev, fmt)
462
463#define EPDBG(ep, fmt, ...) do { \
464 dev_dbg(&(ep)->vhub->pdev->dev, \
465 "%s:EP%d " fmt, \
466 (ep)->dev ? (ep)->dev->name : "hub", \
467 (ep)->d_idx, ##__VA_ARGS__); \
468 } while(0)
469
470#define DDBG(d, fmt, ...) do { \
471 dev_dbg(&(d)->vhub->pdev->dev, \
472 "%s " fmt, (d)->name, \
473 ##__VA_ARGS__); \
474 } while(0)
475#else
476#define UDCDBG(u, fmt...) do { } while(0)
477#define EPDBG(ep, fmt, ...) do { } while(0)
478#define DDBG(d, fmt, ...) do { } while(0)
479#endif
480
481static inline void vhub_dma_workaround(void *addr)
482{
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510 mb();
511 (void)__raw_readl((void __iomem *)addr);
512}
513
514
515void ast_vhub_done(struct ast_vhub_ep *ep, struct ast_vhub_req *req,
516 int status);
517void ast_vhub_nuke(struct ast_vhub_ep *ep, int status);
518struct usb_request *ast_vhub_alloc_request(struct usb_ep *u_ep,
519 gfp_t gfp_flags);
520void ast_vhub_free_request(struct usb_ep *u_ep, struct usb_request *u_req);
521void ast_vhub_init_hw(struct ast_vhub *vhub);
522
523
524void ast_vhub_ep0_handle_ack(struct ast_vhub_ep *ep, bool in_ack);
525void ast_vhub_ep0_handle_setup(struct ast_vhub_ep *ep);
526void ast_vhub_reset_ep0(struct ast_vhub_dev *dev);
527void ast_vhub_init_ep0(struct ast_vhub *vhub, struct ast_vhub_ep *ep,
528 struct ast_vhub_dev *dev);
529int ast_vhub_reply(struct ast_vhub_ep *ep, char *ptr, int len);
530int __ast_vhub_simple_reply(struct ast_vhub_ep *ep, int len, ...);
531#define ast_vhub_simple_reply(udc, ...) \
532 __ast_vhub_simple_reply((udc), \
533 sizeof((u8[]) { __VA_ARGS__ })/sizeof(u8), \
534 __VA_ARGS__)
535
536
537int ast_vhub_init_hub(struct ast_vhub *vhub);
538enum std_req_rc ast_vhub_std_hub_request(struct ast_vhub_ep *ep,
539 struct usb_ctrlrequest *crq);
540enum std_req_rc ast_vhub_class_hub_request(struct ast_vhub_ep *ep,
541 struct usb_ctrlrequest *crq);
542void ast_vhub_device_connect(struct ast_vhub *vhub, unsigned int port,
543 bool on);
544void ast_vhub_hub_suspend(struct ast_vhub *vhub);
545void ast_vhub_hub_resume(struct ast_vhub *vhub);
546void ast_vhub_hub_reset(struct ast_vhub *vhub);
547void ast_vhub_hub_wake_all(struct ast_vhub *vhub);
548
549
550int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx);
551void ast_vhub_del_dev(struct ast_vhub_dev *d);
552void ast_vhub_dev_irq(struct ast_vhub_dev *d);
553int ast_vhub_std_dev_request(struct ast_vhub_ep *ep,
554 struct usb_ctrlrequest *crq);
555
556
557void ast_vhub_epn_ack_irq(struct ast_vhub_ep *ep);
558void ast_vhub_update_epn_stall(struct ast_vhub_ep *ep);
559struct ast_vhub_ep *ast_vhub_alloc_epn(struct ast_vhub_dev *d, u8 addr);
560void ast_vhub_dev_suspend(struct ast_vhub_dev *d);
561void ast_vhub_dev_resume(struct ast_vhub_dev *d);
562void ast_vhub_dev_reset(struct ast_vhub_dev *d);
563
564#endif
565