1 /* SPDX-License-Identifier: GPL-2.0+ */
2 #ifndef __ASPEED_VHUB_H
3 #define __ASPEED_VHUB_H
5 /*****************************
7 * VHUB register definitions *
9 *****************************/
11 #define AST_VHUB_CTRL 0x00 /* Root Function Control & Status Register */
12 #define AST_VHUB_CONF 0x04 /* Root Configuration Setting Register */
13 #define AST_VHUB_IER 0x08 /* Interrupt Ctrl Register */
14 #define AST_VHUB_ISR 0x0C /* Interrupt Status Register */
15 #define AST_VHUB_EP_ACK_IER 0x10 /* Programmable Endpoint Pool ACK Interrupt Enable Register */
16 #define AST_VHUB_EP_NACK_IER 0x14 /* Programmable Endpoint Pool NACK Interrupt Enable Register */
17 #define AST_VHUB_EP_ACK_ISR 0x18 /* Programmable Endpoint Pool ACK Interrupt Status Register */
18 #define AST_VHUB_EP_NACK_ISR 0x1C /* Programmable Endpoint Pool NACK Interrupt Status Register */
19 #define AST_VHUB_SW_RESET 0x20 /* Device Controller Soft Reset Enable Register */
20 #define AST_VHUB_USBSTS 0x24 /* USB Status Register */
21 #define AST_VHUB_EP_TOGGLE 0x28 /* Programmable Endpoint Pool Data Toggle Value Set */
22 #define AST_VHUB_ISO_FAIL_ACC 0x2C /* Isochronous Transaction Fail Accumulator */
23 #define AST_VHUB_EP0_CTRL 0x30 /* Endpoint 0 Contrl/Status Register */
24 #define AST_VHUB_EP0_DATA 0x34 /* Base Address of Endpoint 0 In/OUT Data Buffer Register */
25 #define AST_VHUB_EP1_CTRL 0x38 /* Endpoint 1 Contrl/Status Register */
26 #define AST_VHUB_EP1_STS_CHG 0x3C /* Endpoint 1 Status Change Bitmap Data */
27 #define AST_VHUB_SETUP0 0x80 /* Root Device Setup Data Buffer0 */
28 #define AST_VHUB_SETUP1 0x84 /* Root Device Setup Data Buffer1 */
30 /* Main control reg */
31 #define VHUB_CTRL_PHY_CLK (1 << 31)
32 #define VHUB_CTRL_PHY_LOOP_TEST (1 << 25)
33 #define VHUB_CTRL_DN_PWN (1 << 24)
34 #define VHUB_CTRL_DP_PWN (1 << 23)
35 #define VHUB_CTRL_LONG_DESC (1 << 18)
36 #define VHUB_CTRL_ISO_RSP_CTRL (1 << 17)
37 #define VHUB_CTRL_SPLIT_IN (1 << 16)
38 #define VHUB_CTRL_LOOP_T_RESULT (1 << 15)
39 #define VHUB_CTRL_LOOP_T_STS (1 << 14)
40 #define VHUB_CTRL_PHY_BIST_RESULT (1 << 13)
41 #define VHUB_CTRL_PHY_BIST_CTRL (1 << 12)
42 #define VHUB_CTRL_PHY_RESET_DIS (1 << 11)
43 #define VHUB_CTRL_SET_TEST_MODE(x) ((x) << 8)
44 #define VHUB_CTRL_MANUAL_REMOTE_WAKEUP (1 << 4)
45 #define VHUB_CTRL_AUTO_REMOTE_WAKEUP (1 << 3)
46 #define VHUB_CTRL_CLK_STOP_SUSPEND (1 << 2)
47 #define VHUB_CTRL_FULL_SPEED_ONLY (1 << 1)
48 #define VHUB_CTRL_UPSTREAM_CONNECT (1 << 0)
51 #define VHUB_IRQ_USB_CMD_DEADLOCK (1 << 18)
52 #define VHUB_IRQ_EP_POOL_NAK (1 << 17)
53 #define VHUB_IRQ_EP_POOL_ACK_STALL (1 << 16)
54 #define VHUB_IRQ_DEVICE5 (1 << 13)
55 #define VHUB_IRQ_DEVICE4 (1 << 12)
56 #define VHUB_IRQ_DEVICE3 (1 << 11)
57 #define VHUB_IRQ_DEVICE2 (1 << 10)
58 #define VHUB_IRQ_DEVICE1 (1 << 9)
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
71 #define VHUB_SW_RESET_EP_POOL (1 << 9)
72 #define VHUB_SW_RESET_DMA_CONTROLLER (1 << 8)
73 #define VHUB_SW_RESET_DEVICE5 (1 << 5)
74 #define VHUB_SW_RESET_DEVICE4 (1 << 4)
75 #define VHUB_SW_RESET_DEVICE3 (1 << 3)
76 #define VHUB_SW_RESET_DEVICE2 (1 << 2)
77 #define VHUB_SW_RESET_DEVICE1 (1 << 1)
78 #define VHUB_SW_RESET_ROOT_HUB (1 << 0)
79 #define VHUB_SW_RESET_ALL (VHUB_SW_RESET_EP_POOL | \
80 VHUB_SW_RESET_DMA_CONTROLLER | \
81 VHUB_SW_RESET_DEVICE5 | \
82 VHUB_SW_RESET_DEVICE4 | \
83 VHUB_SW_RESET_DEVICE3 | \
84 VHUB_SW_RESET_DEVICE2 | \
85 VHUB_SW_RESET_DEVICE1 | \
86 VHUB_SW_RESET_ROOT_HUB)
87 /* EP ACK/NACK IRQ masks */
88 #define VHUB_EP_IRQ(n) (1 << (n))
89 #define VHUB_EP_IRQ_ALL 0x7fff /* 15 EPs */
92 #define VHUB_USBSTS_HISPEED (1 << 27)
95 #define VHUB_EP_TOGGLE_VALUE (1 << 8)
96 #define VHUB_EP_TOGGLE_SET_EPNUM(x) ((x) & 0x1f)
99 #define VHUB_EP0_CTRL_STALL (1 << 0)
100 #define VHUB_EP0_TX_BUFF_RDY (1 << 1)
101 #define VHUB_EP0_RX_BUFF_RDY (1 << 2)
102 #define VHUB_EP0_RX_LEN(x) (((x) >> 16) & 0x7f)
103 #define VHUB_EP0_SET_TX_LEN(x) (((x) & 0x7f) << 8)
105 /* HUB EP1 control */
106 #define VHUB_EP1_CTRL_RESET_TOGGLE (1 << 2)
107 #define VHUB_EP1_CTRL_STALL (1 << 1)
108 #define VHUB_EP1_CTRL_ENABLE (1 << 0)
110 /***********************************
112 * per-device register definitions *
114 ***********************************/
115 #define AST_VHUB_DEV_EN_CTRL 0x00
116 #define AST_VHUB_DEV_ISR 0x04
117 #define AST_VHUB_DEV_EP0_CTRL 0x08
118 #define AST_VHUB_DEV_EP0_DATA 0x0c
120 /* Device enable control */
121 #define VHUB_DEV_EN_SET_ADDR(x) ((x) << 8)
122 #define VHUB_DEV_EN_ADDR_MASK ((0xff) << 8)
123 #define VHUB_DEV_EN_EP0_NAK_IRQEN (1 << 6)
124 #define VHUB_DEV_EN_EP0_IN_ACK_IRQEN (1 << 5)
125 #define VHUB_DEV_EN_EP0_OUT_NAK_IRQEN (1 << 4)
126 #define VHUB_DEV_EN_EP0_OUT_ACK_IRQEN (1 << 3)
127 #define VHUB_DEV_EN_EP0_SETUP_IRQEN (1 << 2)
128 #define VHUB_DEV_EN_SPEED_SEL_HIGH (1 << 1)
129 #define VHUB_DEV_EN_ENABLE_PORT (1 << 0)
131 /* Interrupt status */
132 #define VHUV_DEV_IRQ_EP0_IN_DATA_NACK (1 << 4)
133 #define VHUV_DEV_IRQ_EP0_IN_ACK_STALL (1 << 3)
134 #define VHUV_DEV_IRQ_EP0_OUT_DATA_NACK (1 << 2)
135 #define VHUV_DEV_IRQ_EP0_OUT_ACK_STALL (1 << 1)
136 #define VHUV_DEV_IRQ_EP0_SETUP (1 << 0)
140 * Note: The driver relies on the bulk of those bits
141 * matching corresponding vHub EP0 control bits
143 #define VHUB_DEV_EP0_CTRL_STALL VHUB_EP0_CTRL_STALL
144 #define VHUB_DEV_EP0_TX_BUFF_RDY VHUB_EP0_TX_BUFF_RDY
145 #define VHUB_DEV_EP0_RX_BUFF_RDY VHUB_EP0_RX_BUFF_RDY
146 #define VHUB_DEV_EP0_RX_LEN(x) VHUB_EP0_RX_LEN(x)
147 #define VHUB_DEV_EP0_SET_TX_LEN(x) VHUB_EP0_SET_TX_LEN(x)
149 /*************************************
151 * per-endpoint register definitions *
153 *************************************/
155 #define AST_VHUB_EP_CONFIG 0x00
156 #define AST_VHUB_EP_DMA_CTLSTAT 0x04
157 #define AST_VHUB_EP_DESC_BASE 0x08
158 #define AST_VHUB_EP_DESC_STATUS 0x0C
161 #define VHUB_EP_CFG_SET_MAX_PKT(x) (((x) & 0x3ff) << 16)
162 #define VHUB_EP_CFG_AUTO_DATA_DISABLE (1 << 13)
163 #define VHUB_EP_CFG_STALL_CTRL (1 << 12)
164 #define VHUB_EP_CFG_SET_EP_NUM(x) (((x) & 0xf) << 8)
165 #define VHUB_EP_CFG_SET_TYPE(x) ((x) << 5)
166 #define EP_TYPE_OFF 0
167 #define EP_TYPE_BULK 1
168 #define EP_TYPE_INT 2
169 #define EP_TYPE_ISO 3
170 #define VHUB_EP_CFG_DIR_OUT (1 << 4)
171 #define VHUB_EP_CFG_SET_DEV(x) ((x) << 1)
172 #define VHUB_EP_CFG_ENABLE (1 << 0)
175 #define VHUB_EP_DMA_PROC_STATUS(x) (((x) >> 4) & 0xf)
176 #define EP_DMA_PROC_RX_IDLE 0
177 #define EP_DMA_PROC_TX_IDLE 8
178 #define VHUB_EP_DMA_IN_LONG_MODE (1 << 3)
179 #define VHUB_EP_DMA_OUT_CONTIG_MODE (1 << 3)
180 #define VHUB_EP_DMA_CTRL_RESET (1 << 2)
181 #define VHUB_EP_DMA_SINGLE_STAGE (1 << 1)
182 #define VHUB_EP_DMA_DESC_MODE (1 << 0)
185 #define VHUB_EP_DMA_SET_TX_SIZE(x) ((x) << 16)
186 #define VHUB_EP_DMA_TX_SIZE(x) (((x) >> 16) & 0x7ff)
187 #define VHUB_EP_DMA_RPTR(x) (((x) >> 8) & 0xff)
188 #define VHUB_EP_DMA_SET_RPTR(x) (((x) & 0xff) << 8)
189 #define VHUB_EP_DMA_SET_CPU_WPTR(x) (x)
190 #define VHUB_EP_DMA_SINGLE_KICK (1 << 0) /* WPTR = 1 for single mode */
192 /*******************************
194 * DMA descriptors definitions *
196 *******************************/
199 #define VHUB_DSC1_IN_INTERRUPT (1 << 31)
200 #define VHUB_DSC1_IN_SPID_DATA0 (0 << 14)
201 #define VHUB_DSC1_IN_SPID_DATA2 (1 << 14)
202 #define VHUB_DSC1_IN_SPID_DATA1 (2 << 14)
203 #define VHUB_DSC1_IN_SPID_MDATA (3 << 14)
204 #define VHUB_DSC1_IN_SET_LEN(x) ((x) & 0xfff)
205 #define VHUB_DSC1_IN_LEN(x) ((x) & 0xfff)
207 /****************************************
209 * Data structures and misc definitions *
211 ****************************************/
213 #define AST_VHUB_NUM_GEN_EPs 15 /* Generic non-0 EPs */
214 #define AST_VHUB_NUM_PORTS 5 /* vHub ports */
215 #define AST_VHUB_EP0_MAX_PACKET 64 /* EP0's max packet size */
216 #define AST_VHUB_EPn_MAX_PACKET 1024 /* Generic EPs max packet size */
217 #define AST_VHUB_DESCS_COUNT 256 /* Use 256 descriptor mode (valid
218 * values are 256 and 32)
225 * DMA descriptor (generic EPs only, currently only used
228 struct ast_vhub_desc {
233 /* A transfer request, either core-originated or internal */
234 struct ast_vhub_req {
235 struct usb_request req;
236 struct list_head queue;
238 /* Actual count written to descriptors (desc mode only) */
239 unsigned int act_count;
242 * Desc number of the final packet or -1. For non-desc
243 * mode (or ep0), any >= 0 value means "last packet"
247 /* Request active (pending DMAs) */
250 /* Internal request (don't call back core) */
253 #define to_ast_req(__ureq) container_of(__ureq, struct ast_vhub_req, req)
255 /* Current state of an EP0 */
263 * An endpoint, either generic, ep0, actual gadget EP
264 * or internal use vhub EP0. vhub EP1 doesn't have an
265 * associated structure as it's mostly HW managed.
271 struct list_head queue;
273 /* EP index in the device, 0 means this is an EP0 */
276 /* Dev pointer or NULL for vHub EP0 */
277 struct ast_vhub_dev *dev;
280 struct ast_vhub *vhub;
283 * DMA buffer for EP0, fallback DMA buffer for misaligned
284 * OUT transfers for generic EPs
289 /* The rest depends on the EP type */
291 /* EP0 (either device or vhub) */
294 * EP0 registers are "similar" for
295 * vHub and devices but located in
298 void __iomem *ctlstat;
301 /* Current state & direction */
302 enum ep0_state state;
305 /* Internal use request */
306 struct ast_vhub_req req;
309 /* Generic endpoint (aka EPn) */
314 /* Index in global pool (0..14) */
317 /* DMA Descriptors */
318 struct ast_vhub_desc *descs;
319 dma_addr_t descs_dma;
322 unsigned int dma_conf;
324 /* Max chunk size for IN EPs */
325 unsigned int chunk_max;
337 #define to_ast_ep(__uep) container_of(__uep, struct ast_vhub_ep, ep)
339 /* A device attached to a vHub port */
340 struct ast_vhub_dev {
341 struct ast_vhub *vhub;
344 /* Device index (0...4) and name string */
348 /* sysfs enclosure for the gadget gunk */
349 struct device *port_dev;
351 /* Link to gadget core */
352 struct usb_gadget gadget;
353 struct usb_gadget_driver *driver;
359 /* Endpoint structures */
360 struct ast_vhub_ep ep0;
361 struct ast_vhub_ep *epns[AST_VHUB_NUM_GEN_EPs];
364 #define to_ast_dev(__g) container_of(__g, struct ast_vhub_dev, gadget)
366 /* Per vhub port stateinfo structure */
367 struct ast_vhub_port {
368 /* Port status & status change registers */
372 /* Associated device slot */
373 struct ast_vhub_dev dev;
376 /* Global vhub structure */
378 struct platform_device *pdev;
382 struct work_struct wake_work;
385 /* EP0 DMA buffers allocated in one chunk */
387 dma_addr_t ep0_bufs_dma;
389 /* EP0 of the vhub itself */
390 struct ast_vhub_ep ep0;
392 /* State of vhub ep1 */
393 bool ep1_stalled : 1;
396 struct ast_vhub_port ports[AST_VHUB_NUM_PORTS];
398 /* Generic EP data structures */
399 struct ast_vhub_ep epns[AST_VHUB_NUM_GEN_EPs];
401 /* Upstream bus is suspended ? */
404 /* Hub itself can signal remote wakeup */
407 /* Force full speed only */
410 /* Upstream bus speed captured at bus reset */
414 /* Standard request handlers result codes */
416 std_req_stall = -1, /* Stall requested */
417 std_req_complete = 0, /* Request completed with no data */
418 std_req_data = 1, /* Request completed with data */
419 std_req_driver = 2, /* Pass to driver pls */
422 #ifdef CONFIG_USB_GADGET_VERBOSE
423 #define UDCVDBG(u, fmt...) dev_dbg(&(u)->pdev->dev, fmt)
425 #define EPVDBG(ep, fmt, ...) do { \
426 dev_dbg(&(ep)->vhub->pdev->dev, \
428 (ep)->dev ? (ep)->dev->name : "hub", \
429 (ep)->d_idx, ##__VA_ARGS__); \
432 #define DVDBG(d, fmt, ...) do { \
433 dev_dbg(&(d)->vhub->pdev->dev, \
434 "%s " fmt, (d)->name, \
439 #define UDCVDBG(u, fmt...) do { } while(0)
440 #define EPVDBG(ep, fmt, ...) do { } while(0)
441 #define DVDBG(d, fmt, ...) do { } while(0)
444 #ifdef CONFIG_USB_GADGET_DEBUG
445 #define UDCDBG(u, fmt...) dev_dbg(&(u)->pdev->dev, fmt)
447 #define EPDBG(ep, fmt, ...) do { \
448 dev_dbg(&(ep)->vhub->pdev->dev, \
450 (ep)->dev ? (ep)->dev->name : "hub", \
451 (ep)->d_idx, ##__VA_ARGS__); \
454 #define DDBG(d, fmt, ...) do { \
455 dev_dbg(&(d)->vhub->pdev->dev, \
456 "%s " fmt, (d)->name, \
460 #define UDCDBG(u, fmt...) do { } while(0)
461 #define EPDBG(ep, fmt, ...) do { } while(0)
462 #define DDBG(d, fmt, ...) do { } while(0)
466 void ast_vhub_done(struct ast_vhub_ep *ep, struct ast_vhub_req *req,
468 void ast_vhub_nuke(struct ast_vhub_ep *ep, int status);
469 struct usb_request *ast_vhub_alloc_request(struct usb_ep *u_ep,
471 void ast_vhub_free_request(struct usb_ep *u_ep, struct usb_request *u_req);
472 void ast_vhub_init_hw(struct ast_vhub *vhub);
475 void ast_vhub_ep0_handle_ack(struct ast_vhub_ep *ep, bool in_ack);
476 void ast_vhub_ep0_handle_setup(struct ast_vhub_ep *ep);
477 void ast_vhub_init_ep0(struct ast_vhub *vhub, struct ast_vhub_ep *ep,
478 struct ast_vhub_dev *dev);
479 int ast_vhub_reply(struct ast_vhub_ep *ep, char *ptr, int len);
480 int __ast_vhub_simple_reply(struct ast_vhub_ep *ep, int len, ...);
481 #define ast_vhub_simple_reply(udc, ...) \
482 __ast_vhub_simple_reply((udc), \
483 sizeof((u8[]) { __VA_ARGS__ })/sizeof(u8), \
487 void ast_vhub_init_hub(struct ast_vhub *vhub);
488 enum std_req_rc ast_vhub_std_hub_request(struct ast_vhub_ep *ep,
489 struct usb_ctrlrequest *crq);
490 enum std_req_rc ast_vhub_class_hub_request(struct ast_vhub_ep *ep,
491 struct usb_ctrlrequest *crq);
492 void ast_vhub_device_connect(struct ast_vhub *vhub, unsigned int port,
494 void ast_vhub_hub_suspend(struct ast_vhub *vhub);
495 void ast_vhub_hub_resume(struct ast_vhub *vhub);
496 void ast_vhub_hub_reset(struct ast_vhub *vhub);
497 void ast_vhub_hub_wake_all(struct ast_vhub *vhub);
500 int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx);
501 void ast_vhub_del_dev(struct ast_vhub_dev *d);
502 void ast_vhub_dev_irq(struct ast_vhub_dev *d);
503 int ast_vhub_std_dev_request(struct ast_vhub_ep *ep,
504 struct usb_ctrlrequest *crq);
507 void ast_vhub_epn_ack_irq(struct ast_vhub_ep *ep);
508 void ast_vhub_update_epn_stall(struct ast_vhub_ep *ep);
509 struct ast_vhub_ep *ast_vhub_alloc_epn(struct ast_vhub_dev *d, u8 addr);
510 void ast_vhub_dev_suspend(struct ast_vhub_dev *d);
511 void ast_vhub_dev_resume(struct ast_vhub_dev *d);
512 void ast_vhub_dev_reset(struct ast_vhub_dev *d);
514 #endif /* __ASPEED_VHUB_H */