xref: /openbmc/linux/drivers/usb/gadget/udc/net2280.h (revision 8730046c)
1 /*
2  * NetChip 2280 high/full speed USB device controller.
3  * Unlike many such controllers, this one talks PCI.
4  */
5 
6 /*
7  * Copyright (C) 2002 NetChip Technology, Inc. (http://www.netchip.com)
8  * Copyright (C) 2003 David Brownell
9  * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  */
16 
17 #include <linux/usb/net2280.h>
18 #include <linux/usb/usb338x.h>
19 
20 /*-------------------------------------------------------------------------*/
21 
22 #ifdef	__KERNEL__
23 
24 /* indexed registers [11.10] are accessed indirectly
25  * caller must own the device lock.
26  */
27 
28 static inline u32 get_idx_reg(struct net2280_regs __iomem *regs, u32 index)
29 {
30 	writel(index, &regs->idxaddr);
31 	/* NOTE:  synchs device/cpu memory views */
32 	return readl(&regs->idxdata);
33 }
34 
35 static inline void
36 set_idx_reg(struct net2280_regs __iomem *regs, u32 index, u32 value)
37 {
38 	writel(index, &regs->idxaddr);
39 	writel(value, &regs->idxdata);
40 	/* posted, may not be visible yet */
41 }
42 
43 #endif	/* __KERNEL__ */
44 
45 #define PCI_VENDOR_ID_PLX_LEGACY 0x17cc
46 
47 #define PLX_LEGACY		BIT(0)
48 #define PLX_2280		BIT(1)
49 #define PLX_SUPERSPEED		BIT(2)
50 #define PLX_PCIE		BIT(3)
51 
52 #define REG_DIAG		0x0
53 #define     RETRY_COUNTER                                       16
54 #define     FORCE_PCI_SERR                                      11
55 #define     FORCE_PCI_INTERRUPT                                 10
56 #define     FORCE_USB_INTERRUPT                                 9
57 #define     FORCE_CPU_INTERRUPT                                 8
58 #define     ILLEGAL_BYTE_ENABLES                                5
59 #define     FAST_TIMES                                          4
60 #define     FORCE_RECEIVE_ERROR                                 2
61 #define     FORCE_TRANSMIT_CRC_ERROR                            0
62 #define REG_FRAME		0x02	/* from last sof */
63 #define REG_CHIPREV		0x03	/* in bcd */
64 #define	REG_HS_NAK_RATE		0x0a	/* NAK per N uframes */
65 
66 #define	CHIPREV_1	0x0100
67 #define	CHIPREV_1A	0x0110
68 
69 /* DEFECT 7374 */
70 #define DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS         200
71 #define DEFECT_7374_PROCESSOR_WAIT_TIME             10
72 
73 /* ep0 max packet size */
74 #define EP0_SS_MAX_PACKET_SIZE  0x200
75 #define EP0_HS_MAX_PACKET_SIZE  0x40
76 #ifdef	__KERNEL__
77 
78 /*-------------------------------------------------------------------------*/
79 
80 /* [8.3] for scatter/gather i/o
81  * use struct net2280_dma_regs bitfields
82  */
83 struct net2280_dma {
84 	__le32		dmacount;
85 	__le32		dmaaddr;		/* the buffer */
86 	__le32		dmadesc;		/* next dma descriptor */
87 	__le32		_reserved;
88 } __aligned(16);
89 
90 /*-------------------------------------------------------------------------*/
91 
92 /* DRIVER DATA STRUCTURES and UTILITIES */
93 
94 struct net2280_ep {
95 	struct usb_ep				ep;
96 	struct net2280_ep_regs __iomem *cfg;
97 	struct net2280_ep_regs			__iomem *regs;
98 	struct net2280_dma_regs			__iomem *dma;
99 	struct net2280_dma			*dummy;
100 	dma_addr_t				td_dma;	/* of dummy */
101 	struct net2280				*dev;
102 	unsigned long				irqs;
103 
104 	/* analogous to a host-side qh */
105 	struct list_head			queue;
106 	const struct usb_endpoint_descriptor	*desc;
107 	unsigned				num : 8,
108 						fifo_size : 12,
109 						in_fifo_validate : 1,
110 						out_overflow : 1,
111 						stopped : 1,
112 						wedged : 1,
113 						is_in : 1,
114 						is_iso : 1,
115 						responded : 1;
116 };
117 
118 static inline void allow_status(struct net2280_ep *ep)
119 {
120 	/* ep0 only */
121 	writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE) |
122 		BIT(CLEAR_NAK_OUT_PACKETS) |
123 		BIT(CLEAR_NAK_OUT_PACKETS_MODE),
124 		&ep->regs->ep_rsp);
125 	ep->stopped = 1;
126 }
127 
128 static inline void allow_status_338x(struct net2280_ep *ep)
129 {
130 	/*
131 	 * Control Status Phase Handshake was set by the chip when the setup
132 	 * packet arrived. While set, the chip automatically NAKs the host's
133 	 * Status Phase tokens.
134 	 */
135 	writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE), &ep->regs->ep_rsp);
136 
137 	ep->stopped = 1;
138 
139 	/* TD 9.9 Halt Endpoint test.  TD 9.22 set feature test. */
140 	ep->responded = 0;
141 }
142 
143 struct net2280_request {
144 	struct usb_request		req;
145 	struct net2280_dma		*td;
146 	dma_addr_t			td_dma;
147 	struct list_head		queue;
148 	unsigned			mapped : 1,
149 					valid : 1;
150 };
151 
152 struct net2280 {
153 	/* each pci device provides one gadget, several endpoints */
154 	struct usb_gadget		gadget;
155 	spinlock_t			lock;
156 	struct net2280_ep		ep[9];
157 	struct usb_gadget_driver	*driver;
158 	unsigned			enabled : 1,
159 					protocol_stall : 1,
160 					softconnect : 1,
161 					got_irq : 1,
162 					region:1,
163 					u1_enable:1,
164 					u2_enable:1,
165 					ltm_enable:1,
166 					wakeup_enable:1,
167 					addressed_state:1,
168 					bug7734_patched:1;
169 	u16				chiprev;
170 	int enhanced_mode;
171 	int n_ep;
172 	kernel_ulong_t			quirks;
173 
174 
175 	/* pci state used to access those endpoints */
176 	struct pci_dev			*pdev;
177 	struct net2280_regs		__iomem *regs;
178 	struct net2280_usb_regs		__iomem *usb;
179 	struct usb338x_usb_ext_regs	__iomem *usb_ext;
180 	struct net2280_pci_regs		__iomem *pci;
181 	struct net2280_dma_regs		__iomem *dma;
182 	struct net2280_dep_regs		__iomem *dep;
183 	struct net2280_ep_regs		__iomem *epregs;
184 	struct usb338x_ll_regs		__iomem *llregs;
185 	struct usb338x_ll_lfps_regs	__iomem *ll_lfps_regs;
186 	struct usb338x_ll_tsn_regs	__iomem *ll_tsn_regs;
187 	struct usb338x_ll_chi_regs	__iomem *ll_chicken_reg;
188 	struct usb338x_pl_regs		__iomem *plregs;
189 
190 	struct pci_pool			*requests;
191 	/* statistics...*/
192 };
193 
194 static inline void set_halt(struct net2280_ep *ep)
195 {
196 	/* ep0 and bulk/intr endpoints */
197 	writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE) |
198 		/* set NAK_OUT for erratum 0114 */
199 		((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS) |
200 		BIT(SET_ENDPOINT_HALT),
201 		&ep->regs->ep_rsp);
202 }
203 
204 static inline void clear_halt(struct net2280_ep *ep)
205 {
206 	/* ep0 and bulk/intr endpoints */
207 	writel(BIT(CLEAR_ENDPOINT_HALT) |
208 		BIT(CLEAR_ENDPOINT_TOGGLE) |
209 		    /*
210 		     * unless the gadget driver left a short packet in the
211 		     * fifo, this reverses the erratum 0114 workaround.
212 		     */
213 		((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS),
214 		&ep->regs->ep_rsp);
215 }
216 
217 /*
218  * FSM value for Defect 7374 (U1U2 Test) is managed in
219  * chip's SCRATCH register:
220  */
221 #define DEFECT7374_FSM_FIELD    28
222 
223 /* Waiting for Control Read:
224  *  - A transition to this state indicates a fresh USB connection,
225  *    before the first Setup Packet. The connection speed is not
226  *    known. Firmware is waiting for the first Control Read.
227  *  - Starting state: This state can be thought of as the FSM's typical
228  *    starting state.
229  *  - Tip: Upon the first SS Control Read the FSM never
230  *    returns to this state.
231  */
232 #define DEFECT7374_FSM_WAITING_FOR_CONTROL_READ BIT(DEFECT7374_FSM_FIELD)
233 
234 /* Non-SS Control Read:
235  *  - A transition to this state indicates detection of the first HS
236  *    or FS Control Read.
237  *  - Tip: Upon the first SS Control Read the FSM never
238  *    returns to this state.
239  */
240 #define	DEFECT7374_FSM_NON_SS_CONTROL_READ (2 << DEFECT7374_FSM_FIELD)
241 
242 /* SS Control Read:
243  *  - A transition to this state indicates detection of the
244  *    first SS Control Read.
245  *  - This state indicates workaround completion. Workarounds no longer
246  *    need to be applied (as long as the chip remains powered up).
247  *  - Tip: Once in this state the FSM state does not change (until
248  *    the chip's power is lost and restored).
249  *  - This can be thought of as the final state of the FSM;
250  *    the FSM 'locks-up' in this state until the chip loses power.
251  */
252 #define DEFECT7374_FSM_SS_CONTROL_READ (3 << DEFECT7374_FSM_FIELD)
253 
254 #ifdef USE_RDK_LEDS
255 
256 static inline void net2280_led_init(struct net2280 *dev)
257 {
258 	/* LED3 (green) is on during USB activity. note erratum 0113. */
259 	writel(BIT(GPIO3_LED_SELECT) |
260 		BIT(GPIO3_OUTPUT_ENABLE) |
261 		BIT(GPIO2_OUTPUT_ENABLE) |
262 		BIT(GPIO1_OUTPUT_ENABLE) |
263 		BIT(GPIO0_OUTPUT_ENABLE),
264 		&dev->regs->gpioctl);
265 }
266 
267 /* indicate speed with bi-color LED 0/1 */
268 static inline
269 void net2280_led_speed(struct net2280 *dev, enum usb_device_speed speed)
270 {
271 	u32	val = readl(&dev->regs->gpioctl);
272 	switch (speed) {
273 	case USB_SPEED_SUPER:		/* green + red */
274 		val |= BIT(GPIO0_DATA) | BIT(GPIO1_DATA);
275 		break;
276 	case USB_SPEED_HIGH:		/* green */
277 		val &= ~BIT(GPIO0_DATA);
278 		val |= BIT(GPIO1_DATA);
279 		break;
280 	case USB_SPEED_FULL:		/* red */
281 		val &= ~BIT(GPIO1_DATA);
282 		val |= BIT(GPIO0_DATA);
283 		break;
284 	default:			/* (off/black) */
285 		val &= ~(BIT(GPIO1_DATA) | BIT(GPIO0_DATA));
286 		break;
287 	}
288 	writel(val, &dev->regs->gpioctl);
289 }
290 
291 /* indicate power with LED 2 */
292 static inline void net2280_led_active(struct net2280 *dev, int is_active)
293 {
294 	u32	val = readl(&dev->regs->gpioctl);
295 
296 	/* FIXME this LED never seems to turn on.*/
297 	if (is_active)
298 		val |= GPIO2_DATA;
299 	else
300 		val &= ~GPIO2_DATA;
301 	writel(val, &dev->regs->gpioctl);
302 }
303 
304 static inline void net2280_led_shutdown(struct net2280 *dev)
305 {
306 	/* turn off all four GPIO*_DATA bits */
307 	writel(readl(&dev->regs->gpioctl) & ~0x0f,
308 			&dev->regs->gpioctl);
309 }
310 
311 #else
312 
313 #define net2280_led_init(dev)		do { } while (0)
314 #define net2280_led_speed(dev, speed)	do { } while (0)
315 #define net2280_led_shutdown(dev)	do { } while (0)
316 
317 #endif
318 
319 /*-------------------------------------------------------------------------*/
320 
321 #define ep_dbg(ndev, fmt, args...) \
322 	dev_dbg((&((ndev)->pdev->dev)), fmt, ##args)
323 
324 #define ep_vdbg(ndev, fmt, args...) \
325 	dev_vdbg((&((ndev)->pdev->dev)), fmt, ##args)
326 
327 #define ep_info(ndev, fmt, args...) \
328 	dev_info((&((ndev)->pdev->dev)), fmt, ##args)
329 
330 #define ep_warn(ndev, fmt, args...) \
331 	dev_warn((&((ndev)->pdev->dev)), fmt, ##args)
332 
333 #define ep_err(ndev, fmt, args...) \
334 	dev_err((&((ndev)->pdev->dev)), fmt, ##args)
335 
336 /*-------------------------------------------------------------------------*/
337 
338 static inline void set_fifo_bytecount(struct net2280_ep *ep, unsigned count)
339 {
340 	if (ep->dev->pdev->vendor == 0x17cc)
341 		writeb(count, 2 + (u8 __iomem *) &ep->regs->ep_cfg);
342 	else{
343 		u32 tmp = readl(&ep->cfg->ep_cfg) &
344 					(~(0x07 << EP_FIFO_BYTE_COUNT));
345 		writel(tmp | (count << EP_FIFO_BYTE_COUNT), &ep->cfg->ep_cfg);
346 	}
347 }
348 
349 static inline void start_out_naking(struct net2280_ep *ep)
350 {
351 	/* NOTE:  hardware races lurk here, and PING protocol issues */
352 	writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
353 	/* synch with device */
354 	readl(&ep->regs->ep_rsp);
355 }
356 
357 static inline void stop_out_naking(struct net2280_ep *ep)
358 {
359 	u32	tmp;
360 
361 	tmp = readl(&ep->regs->ep_stat);
362 	if ((tmp & BIT(NAK_OUT_PACKETS)) != 0)
363 		writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
364 }
365 
366 
367 static inline void set_max_speed(struct net2280_ep *ep, u32 max)
368 {
369 	u32 reg;
370 	static const u32 ep_enhanced[9] = { 0x10, 0x60, 0x30, 0x80,
371 					  0x50, 0x20, 0x70, 0x40, 0x90 };
372 
373 	if (ep->dev->enhanced_mode) {
374 		reg = ep_enhanced[ep->num];
375 		switch (ep->dev->gadget.speed) {
376 		case USB_SPEED_SUPER:
377 			reg += 2;
378 			break;
379 		case USB_SPEED_FULL:
380 			reg += 1;
381 			break;
382 		case USB_SPEED_HIGH:
383 		default:
384 			break;
385 		}
386 	} else {
387 		reg = (ep->num + 1) * 0x10;
388 		if (ep->dev->gadget.speed != USB_SPEED_HIGH)
389 			reg += 1;
390 	}
391 
392 	set_idx_reg(ep->dev->regs, reg, max);
393 }
394 
395 #endif	/* __KERNEL__ */
396