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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  */
15 
16 #include <linux/usb/net2280.h>
17 
18 /*-------------------------------------------------------------------------*/
19 
20 #ifdef	__KERNEL__
21 
22 /* indexed registers [11.10] are accessed indirectly
23  * caller must own the device lock.
24  */
25 
26 static inline u32
get_idx_reg(struct net2280_regs __iomem * regs,u32 index)27 get_idx_reg (struct net2280_regs __iomem *regs, u32 index)
28 {
29 	writel (index, &regs->idxaddr);
30 	/* NOTE:  synchs device/cpu memory views */
31 	return readl (&regs->idxdata);
32 }
33 
34 static inline void
set_idx_reg(struct net2280_regs __iomem * regs,u32 index,u32 value)35 set_idx_reg (struct net2280_regs __iomem *regs, u32 index, u32 value)
36 {
37 	writel (index, &regs->idxaddr);
38 	writel (value, &regs->idxdata);
39 	/* posted, may not be visible yet */
40 }
41 
42 #endif	/* __KERNEL__ */
43 
44 
45 #define REG_DIAG		0x0
46 #define     RETRY_COUNTER                                       16
47 #define     FORCE_PCI_SERR                                      11
48 #define     FORCE_PCI_INTERRUPT                                 10
49 #define     FORCE_USB_INTERRUPT                                 9
50 #define     FORCE_CPU_INTERRUPT                                 8
51 #define     ILLEGAL_BYTE_ENABLES                                5
52 #define     FAST_TIMES                                          4
53 #define     FORCE_RECEIVE_ERROR                                 2
54 #define     FORCE_TRANSMIT_CRC_ERROR                            0
55 #define REG_FRAME		0x02	/* from last sof */
56 #define REG_CHIPREV		0x03	/* in bcd */
57 #define	REG_HS_NAK_RATE		0x0a	/* NAK per N uframes */
58 
59 #define	CHIPREV_1	0x0100
60 #define	CHIPREV_1A	0x0110
61 
62 #ifdef	__KERNEL__
63 
64 /* ep a-f highspeed and fullspeed maxpacket, addresses
65  * computed from ep->num
66  */
67 #define REG_EP_MAXPKT(dev,num) (((num) + 1) * 0x10 + \
68 		(((dev)->gadget.speed == USB_SPEED_HIGH) ? 0 : 1))
69 
70 /*-------------------------------------------------------------------------*/
71 
72 /* [8.3] for scatter/gather i/o
73  * use struct net2280_dma_regs bitfields
74  */
75 struct net2280_dma {
76 	__le32		dmacount;
77 	__le32		dmaaddr;		/* the buffer */
78 	__le32		dmadesc;		/* next dma descriptor */
79 	__le32		_reserved;
80 } __attribute__ ((aligned (16)));
81 
82 /*-------------------------------------------------------------------------*/
83 
84 /* DRIVER DATA STRUCTURES and UTILITIES */
85 
86 struct net2280_ep {
87 	struct usb_ep				ep;
88 	struct net2280_ep_regs			__iomem *regs;
89 	struct net2280_dma_regs			__iomem *dma;
90 	struct net2280_dma			*dummy;
91 	dma_addr_t				td_dma;	/* of dummy */
92 	struct net2280				*dev;
93 	unsigned long				irqs;
94 
95 	/* analogous to a host-side qh */
96 	struct list_head			queue;
97 	const struct usb_endpoint_descriptor	*desc;
98 	unsigned				num : 8,
99 						fifo_size : 12,
100 						in_fifo_validate : 1,
101 						out_overflow : 1,
102 						stopped : 1,
103 						wedged : 1,
104 						is_in : 1,
105 						is_iso : 1,
106 						responded : 1;
107 };
108 
allow_status(struct net2280_ep * ep)109 static inline void allow_status (struct net2280_ep *ep)
110 {
111 	/* ep0 only */
112 	writel (  (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
113 		| (1 << CLEAR_NAK_OUT_PACKETS)
114 		| (1 << CLEAR_NAK_OUT_PACKETS_MODE)
115 		, &ep->regs->ep_rsp);
116 	ep->stopped = 1;
117 }
118 
119 /* count (<= 4) bytes in the next fifo write will be valid */
set_fifo_bytecount(struct net2280_ep * ep,unsigned count)120 static inline void set_fifo_bytecount (struct net2280_ep *ep, unsigned count)
121 {
122 	writeb (count, 2 + (u8 __iomem *) &ep->regs->ep_cfg);
123 }
124 
125 struct net2280_request {
126 	struct usb_request		req;
127 	struct net2280_dma		*td;
128 	dma_addr_t			td_dma;
129 	struct list_head		queue;
130 	unsigned			mapped : 1,
131 					valid : 1;
132 };
133 
134 struct net2280 {
135 	/* each pci device provides one gadget, several endpoints */
136 	struct usb_gadget		gadget;
137 	spinlock_t			lock;
138 	struct net2280_ep		ep [7];
139 	struct usb_gadget_driver 	*driver;
140 	unsigned			enabled : 1,
141 					protocol_stall : 1,
142 					softconnect : 1,
143 					got_irq : 1,
144 					region : 1;
145 	u16				chiprev;
146 
147 	/* pci state used to access those endpoints */
148 	struct pci_dev			*pdev;
149 	struct net2280_regs		__iomem *regs;
150 	struct net2280_usb_regs		__iomem *usb;
151 	struct net2280_pci_regs		__iomem *pci;
152 	struct net2280_dma_regs		__iomem *dma;
153 	struct net2280_dep_regs		__iomem *dep;
154 	struct net2280_ep_regs		__iomem *epregs;
155 
156 	struct pci_pool			*requests;
157 	// statistics...
158 };
159 
set_halt(struct net2280_ep * ep)160 static inline void set_halt (struct net2280_ep *ep)
161 {
162 	/* ep0 and bulk/intr endpoints */
163 	writel (  (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
164 		    /* set NAK_OUT for erratum 0114 */
165 		| ((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS)
166 		| (1 << SET_ENDPOINT_HALT)
167 		, &ep->regs->ep_rsp);
168 }
169 
clear_halt(struct net2280_ep * ep)170 static inline void clear_halt (struct net2280_ep *ep)
171 {
172 	/* ep0 and bulk/intr endpoints */
173 	writel (  (1 << CLEAR_ENDPOINT_HALT)
174 		| (1 << CLEAR_ENDPOINT_TOGGLE)
175 		    /* unless the gadget driver left a short packet in the
176 		     * fifo, this reverses the erratum 0114 workaround.
177 		     */
178 		| ((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS)
179 		, &ep->regs->ep_rsp);
180 }
181 
182 #ifdef USE_RDK_LEDS
183 
net2280_led_init(struct net2280 * dev)184 static inline void net2280_led_init (struct net2280 *dev)
185 {
186 	/* LED3 (green) is on during USB activity. note erratum 0113. */
187 	writel ((1 << GPIO3_LED_SELECT)
188 		| (1 << GPIO3_OUTPUT_ENABLE)
189 		| (1 << GPIO2_OUTPUT_ENABLE)
190 		| (1 << GPIO1_OUTPUT_ENABLE)
191 		| (1 << GPIO0_OUTPUT_ENABLE)
192 		, &dev->regs->gpioctl);
193 }
194 
195 /* indicate speed with bi-color LED 0/1 */
196 static inline
net2280_led_speed(struct net2280 * dev,enum usb_device_speed speed)197 void net2280_led_speed (struct net2280 *dev, enum usb_device_speed speed)
198 {
199 	u32	val = readl (&dev->regs->gpioctl);
200 	switch (speed) {
201 	case USB_SPEED_HIGH:		/* green */
202 		val &= ~(1 << GPIO0_DATA);
203 		val |= (1 << GPIO1_DATA);
204 		break;
205 	case USB_SPEED_FULL:		/* red */
206 		val &= ~(1 << GPIO1_DATA);
207 		val |= (1 << GPIO0_DATA);
208 		break;
209 	default:			/* (off/black) */
210 		val &= ~((1 << GPIO1_DATA) | (1 << GPIO0_DATA));
211 		break;
212 	}
213 	writel (val, &dev->regs->gpioctl);
214 }
215 
216 /* indicate power with LED 2 */
net2280_led_active(struct net2280 * dev,int is_active)217 static inline void net2280_led_active (struct net2280 *dev, int is_active)
218 {
219 	u32	val = readl (&dev->regs->gpioctl);
220 
221 	// FIXME this LED never seems to turn on.
222 	if (is_active)
223 		val |= GPIO2_DATA;
224 	else
225 		val &= ~GPIO2_DATA;
226 	writel (val, &dev->regs->gpioctl);
227 }
net2280_led_shutdown(struct net2280 * dev)228 static inline void net2280_led_shutdown (struct net2280 *dev)
229 {
230 	/* turn off all four GPIO*_DATA bits */
231 	writel (readl (&dev->regs->gpioctl) & ~0x0f,
232 			&dev->regs->gpioctl);
233 }
234 
235 #else
236 
237 #define net2280_led_init(dev)		do { } while (0)
238 #define net2280_led_speed(dev, speed)	do { } while (0)
239 #define net2280_led_shutdown(dev)	do { } while (0)
240 
241 #endif
242 
243 /*-------------------------------------------------------------------------*/
244 
245 #define xprintk(dev,level,fmt,args...) \
246 	printk(level "%s %s: " fmt , driver_name , \
247 			pci_name(dev->pdev) , ## args)
248 
249 #ifdef DEBUG
250 #undef DEBUG
251 #define DEBUG(dev,fmt,args...) \
252 	xprintk(dev , KERN_DEBUG , fmt , ## args)
253 #else
254 #define DEBUG(dev,fmt,args...) \
255 	do { } while (0)
256 #endif /* DEBUG */
257 
258 #ifdef VERBOSE
259 #define VDEBUG DEBUG
260 #else
261 #define VDEBUG(dev,fmt,args...) \
262 	do { } while (0)
263 #endif	/* VERBOSE */
264 
265 #define ERROR(dev,fmt,args...) \
266 	xprintk(dev , KERN_ERR , fmt , ## args)
267 #define WARNING(dev,fmt,args...) \
268 	xprintk(dev , KERN_WARNING , fmt , ## args)
269 #define INFO(dev,fmt,args...) \
270 	xprintk(dev , KERN_INFO , fmt , ## args)
271 
272 /*-------------------------------------------------------------------------*/
273 
start_out_naking(struct net2280_ep * ep)274 static inline void start_out_naking (struct net2280_ep *ep)
275 {
276 	/* NOTE:  hardware races lurk here, and PING protocol issues */
277 	writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
278 	/* synch with device */
279 	readl (&ep->regs->ep_rsp);
280 }
281 
282 #ifdef DEBUG
assert_out_naking(struct net2280_ep * ep,const char * where)283 static inline void assert_out_naking (struct net2280_ep *ep, const char *where)
284 {
285 	u32	tmp = readl (&ep->regs->ep_stat);
286 
287 	if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
288 		DEBUG (ep->dev, "%s %s %08x !NAK\n",
289 				ep->ep.name, where, tmp);
290 		writel ((1 << SET_NAK_OUT_PACKETS),
291 			&ep->regs->ep_rsp);
292 	}
293 }
294 #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep,__func__)
295 #else
296 #define ASSERT_OUT_NAKING(ep) do {} while (0)
297 #endif
298 
stop_out_naking(struct net2280_ep * ep)299 static inline void stop_out_naking (struct net2280_ep *ep)
300 {
301 	u32	tmp;
302 
303 	tmp = readl (&ep->regs->ep_stat);
304 	if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
305 		writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
306 }
307 
308 #endif	/* __KERNEL__ */
309