xref: /linux/drivers/usb/gadget/udc/net2272.c (revision 0b8061c340b643e01da431dd60c75a41bb1d31ec)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for PLX NET2272 USB device controller
4  *
5  * Copyright (C) 2005-2006 PLX Technology, Inc.
6  * Copyright (C) 2006-2011 Analog Devices, Inc.
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/ioport.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/pci.h>
21 #include <linux/platform_device.h>
22 #include <linux/prefetch.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
26 #include <linux/usb.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
29 
30 #include <asm/byteorder.h>
31 #include <asm/unaligned.h>
32 
33 #include "net2272.h"
34 
35 #define DRIVER_DESC "PLX NET2272 USB Peripheral Controller"
36 
37 static const char driver_name[] = "net2272";
38 static const char driver_vers[] = "2006 October 17/mainline";
39 static const char driver_desc[] = DRIVER_DESC;
40 
41 static const char ep0name[] = "ep0";
42 static const char * const ep_name[] = {
43 	ep0name,
44 	"ep-a", "ep-b", "ep-c",
45 };
46 
47 #ifdef CONFIG_USB_NET2272_DMA
48 /*
49  * use_dma: the NET2272 can use an external DMA controller.
50  * Note that since there is no generic DMA api, some functions,
51  * notably request_dma, start_dma, and cancel_dma will need to be
52  * modified for your platform's particular dma controller.
53  *
54  * If use_dma is disabled, pio will be used instead.
55  */
56 static bool use_dma = false;
57 module_param(use_dma, bool, 0644);
58 
59 /*
60  * dma_ep: selects the endpoint for use with dma (1=ep-a, 2=ep-b)
61  * The NET2272 can only use dma for a single endpoint at a time.
62  * At some point this could be modified to allow either endpoint
63  * to take control of dma as it becomes available.
64  *
65  * Note that DMA should not be used on OUT endpoints unless it can
66  * be guaranteed that no short packets will arrive on an IN endpoint
67  * while the DMA operation is pending.  Otherwise the OUT DMA will
68  * terminate prematurely (See NET2272 Errata 630-0213-0101)
69  */
70 static ushort dma_ep = 1;
71 module_param(dma_ep, ushort, 0644);
72 
73 /*
74  * dma_mode: net2272 dma mode setting (see LOCCTL1 definiton):
75  *	mode 0 == Slow DREQ mode
76  *	mode 1 == Fast DREQ mode
77  *	mode 2 == Burst mode
78  */
79 static ushort dma_mode = 2;
80 module_param(dma_mode, ushort, 0644);
81 #else
82 #define use_dma 0
83 #define dma_ep 1
84 #define dma_mode 2
85 #endif
86 
87 /*
88  * fifo_mode: net2272 buffer configuration:
89  *      mode 0 == ep-{a,b,c} 512db each
90  *      mode 1 == ep-a 1k, ep-{b,c} 512db
91  *      mode 2 == ep-a 1k, ep-b 1k, ep-c 512db
92  *      mode 3 == ep-a 1k, ep-b disabled, ep-c 512db
93  */
94 static ushort fifo_mode = 0;
95 module_param(fifo_mode, ushort, 0644);
96 
97 /*
98  * enable_suspend: When enabled, the driver will respond to
99  * USB suspend requests by powering down the NET2272.  Otherwise,
100  * USB suspend requests will be ignored.  This is acceptible for
101  * self-powered devices.  For bus powered devices set this to 1.
102  */
103 static ushort enable_suspend = 0;
104 module_param(enable_suspend, ushort, 0644);
105 
106 static void assert_out_naking(struct net2272_ep *ep, const char *where)
107 {
108 	u8 tmp;
109 
110 #ifndef DEBUG
111 	return;
112 #endif
113 
114 	tmp = net2272_ep_read(ep, EP_STAT0);
115 	if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
116 		dev_dbg(ep->dev->dev, "%s %s %02x !NAK\n",
117 			ep->ep.name, where, tmp);
118 		net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
119 	}
120 }
121 #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep, __func__)
122 
123 static void stop_out_naking(struct net2272_ep *ep)
124 {
125 	u8 tmp = net2272_ep_read(ep, EP_STAT0);
126 
127 	if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
128 		net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
129 }
130 
131 #define PIPEDIR(bAddress) (usb_pipein(bAddress) ? "in" : "out")
132 
133 static char *type_string(u8 bmAttributes)
134 {
135 	switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
136 	case USB_ENDPOINT_XFER_BULK: return "bulk";
137 	case USB_ENDPOINT_XFER_ISOC: return "iso";
138 	case USB_ENDPOINT_XFER_INT:  return "intr";
139 	default:                     return "control";
140 	}
141 }
142 
143 static char *buf_state_string(unsigned state)
144 {
145 	switch (state) {
146 	case BUFF_FREE:  return "free";
147 	case BUFF_VALID: return "valid";
148 	case BUFF_LCL:   return "local";
149 	case BUFF_USB:   return "usb";
150 	default:         return "unknown";
151 	}
152 }
153 
154 static char *dma_mode_string(void)
155 {
156 	if (!use_dma)
157 		return "PIO";
158 	switch (dma_mode) {
159 	case 0:  return "SLOW DREQ";
160 	case 1:  return "FAST DREQ";
161 	case 2:  return "BURST";
162 	default: return "invalid";
163 	}
164 }
165 
166 static void net2272_dequeue_all(struct net2272_ep *);
167 static int net2272_kick_dma(struct net2272_ep *, struct net2272_request *);
168 static int net2272_fifo_status(struct usb_ep *);
169 
170 static const struct usb_ep_ops net2272_ep_ops;
171 
172 /*---------------------------------------------------------------------------*/
173 
174 static int
175 net2272_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
176 {
177 	struct net2272 *dev;
178 	struct net2272_ep *ep;
179 	u32 max;
180 	u8 tmp;
181 	unsigned long flags;
182 
183 	ep = container_of(_ep, struct net2272_ep, ep);
184 	if (!_ep || !desc || ep->desc || _ep->name == ep0name
185 			|| desc->bDescriptorType != USB_DT_ENDPOINT)
186 		return -EINVAL;
187 	dev = ep->dev;
188 	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
189 		return -ESHUTDOWN;
190 
191 	max = usb_endpoint_maxp(desc);
192 
193 	spin_lock_irqsave(&dev->lock, flags);
194 	_ep->maxpacket = max;
195 	ep->desc = desc;
196 
197 	/* net2272_ep_reset() has already been called */
198 	ep->stopped = 0;
199 	ep->wedged = 0;
200 
201 	/* set speed-dependent max packet */
202 	net2272_ep_write(ep, EP_MAXPKT0, max & 0xff);
203 	net2272_ep_write(ep, EP_MAXPKT1, (max & 0xff00) >> 8);
204 
205 	/* set type, direction, address; reset fifo counters */
206 	net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
207 	tmp = usb_endpoint_type(desc);
208 	if (usb_endpoint_xfer_bulk(desc)) {
209 		/* catch some particularly blatant driver bugs */
210 		if ((dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
211 		    (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
212 			spin_unlock_irqrestore(&dev->lock, flags);
213 			return -ERANGE;
214 		}
215 	}
216 	ep->is_iso = usb_endpoint_xfer_isoc(desc) ? 1 : 0;
217 	tmp <<= ENDPOINT_TYPE;
218 	tmp |= ((desc->bEndpointAddress & 0x0f) << ENDPOINT_NUMBER);
219 	tmp |= usb_endpoint_dir_in(desc) << ENDPOINT_DIRECTION;
220 	tmp |= (1 << ENDPOINT_ENABLE);
221 
222 	/* for OUT transfers, block the rx fifo until a read is posted */
223 	ep->is_in = usb_endpoint_dir_in(desc);
224 	if (!ep->is_in)
225 		net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
226 
227 	net2272_ep_write(ep, EP_CFG, tmp);
228 
229 	/* enable irqs */
230 	tmp = (1 << ep->num) | net2272_read(dev, IRQENB0);
231 	net2272_write(dev, IRQENB0, tmp);
232 
233 	tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
234 		| (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
235 		| net2272_ep_read(ep, EP_IRQENB);
236 	net2272_ep_write(ep, EP_IRQENB, tmp);
237 
238 	tmp = desc->bEndpointAddress;
239 	dev_dbg(dev->dev, "enabled %s (ep%d%s-%s) max %04x cfg %02x\n",
240 		_ep->name, tmp & 0x0f, PIPEDIR(tmp),
241 		type_string(desc->bmAttributes), max,
242 		net2272_ep_read(ep, EP_CFG));
243 
244 	spin_unlock_irqrestore(&dev->lock, flags);
245 	return 0;
246 }
247 
248 static void net2272_ep_reset(struct net2272_ep *ep)
249 {
250 	u8 tmp;
251 
252 	ep->desc = NULL;
253 	INIT_LIST_HEAD(&ep->queue);
254 
255 	usb_ep_set_maxpacket_limit(&ep->ep, ~0);
256 	ep->ep.ops = &net2272_ep_ops;
257 
258 	/* disable irqs, endpoint */
259 	net2272_ep_write(ep, EP_IRQENB, 0);
260 
261 	/* init to our chosen defaults, notably so that we NAK OUT
262 	 * packets until the driver queues a read.
263 	 */
264 	tmp = (1 << NAK_OUT_PACKETS_MODE) | (1 << ALT_NAK_OUT_PACKETS);
265 	net2272_ep_write(ep, EP_RSPSET, tmp);
266 
267 	tmp = (1 << INTERRUPT_MODE) | (1 << HIDE_STATUS_PHASE);
268 	if (ep->num != 0)
269 		tmp |= (1 << ENDPOINT_TOGGLE) | (1 << ENDPOINT_HALT);
270 
271 	net2272_ep_write(ep, EP_RSPCLR, tmp);
272 
273 	/* scrub most status bits, and flush any fifo state */
274 	net2272_ep_write(ep, EP_STAT0,
275 			  (1 << DATA_IN_TOKEN_INTERRUPT)
276 			| (1 << DATA_OUT_TOKEN_INTERRUPT)
277 			| (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
278 			| (1 << DATA_PACKET_RECEIVED_INTERRUPT)
279 			| (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT));
280 
281 	net2272_ep_write(ep, EP_STAT1,
282 			    (1 << TIMEOUT)
283 			  | (1 << USB_OUT_ACK_SENT)
284 			  | (1 << USB_OUT_NAK_SENT)
285 			  | (1 << USB_IN_ACK_RCVD)
286 			  | (1 << USB_IN_NAK_SENT)
287 			  | (1 << USB_STALL_SENT)
288 			  | (1 << LOCAL_OUT_ZLP)
289 			  | (1 << BUFFER_FLUSH));
290 
291 	/* fifo size is handled seperately */
292 }
293 
294 static int net2272_disable(struct usb_ep *_ep)
295 {
296 	struct net2272_ep *ep;
297 	unsigned long flags;
298 
299 	ep = container_of(_ep, struct net2272_ep, ep);
300 	if (!_ep || !ep->desc || _ep->name == ep0name)
301 		return -EINVAL;
302 
303 	spin_lock_irqsave(&ep->dev->lock, flags);
304 	net2272_dequeue_all(ep);
305 	net2272_ep_reset(ep);
306 
307 	dev_vdbg(ep->dev->dev, "disabled %s\n", _ep->name);
308 
309 	spin_unlock_irqrestore(&ep->dev->lock, flags);
310 	return 0;
311 }
312 
313 /*---------------------------------------------------------------------------*/
314 
315 static struct usb_request *
316 net2272_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
317 {
318 	struct net2272_request *req;
319 
320 	if (!_ep)
321 		return NULL;
322 
323 	req = kzalloc(sizeof(*req), gfp_flags);
324 	if (!req)
325 		return NULL;
326 
327 	INIT_LIST_HEAD(&req->queue);
328 
329 	return &req->req;
330 }
331 
332 static void
333 net2272_free_request(struct usb_ep *_ep, struct usb_request *_req)
334 {
335 	struct net2272_request *req;
336 
337 	if (!_ep || !_req)
338 		return;
339 
340 	req = container_of(_req, struct net2272_request, req);
341 	WARN_ON(!list_empty(&req->queue));
342 	kfree(req);
343 }
344 
345 static void
346 net2272_done(struct net2272_ep *ep, struct net2272_request *req, int status)
347 {
348 	struct net2272 *dev;
349 	unsigned stopped = ep->stopped;
350 
351 	if (ep->num == 0) {
352 		if (ep->dev->protocol_stall) {
353 			ep->stopped = 1;
354 			set_halt(ep);
355 		}
356 		allow_status(ep);
357 	}
358 
359 	list_del_init(&req->queue);
360 
361 	if (req->req.status == -EINPROGRESS)
362 		req->req.status = status;
363 	else
364 		status = req->req.status;
365 
366 	dev = ep->dev;
367 	if (use_dma && ep->dma)
368 		usb_gadget_unmap_request(&dev->gadget, &req->req,
369 				ep->is_in);
370 
371 	if (status && status != -ESHUTDOWN)
372 		dev_vdbg(dev->dev, "complete %s req %p stat %d len %u/%u buf %p\n",
373 			ep->ep.name, &req->req, status,
374 			req->req.actual, req->req.length, req->req.buf);
375 
376 	/* don't modify queue heads during completion callback */
377 	ep->stopped = 1;
378 	spin_unlock(&dev->lock);
379 	usb_gadget_giveback_request(&ep->ep, &req->req);
380 	spin_lock(&dev->lock);
381 	ep->stopped = stopped;
382 }
383 
384 static int
385 net2272_write_packet(struct net2272_ep *ep, u8 *buf,
386 	struct net2272_request *req, unsigned max)
387 {
388 	u16 __iomem *ep_data = net2272_reg_addr(ep->dev, EP_DATA);
389 	u16 *bufp;
390 	unsigned length, count;
391 	u8 tmp;
392 
393 	length = min(req->req.length - req->req.actual, max);
394 	req->req.actual += length;
395 
396 	dev_vdbg(ep->dev->dev, "write packet %s req %p max %u len %u avail %u\n",
397 		ep->ep.name, req, max, length,
398 		(net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0));
399 
400 	count = length;
401 	bufp = (u16 *)buf;
402 
403 	while (likely(count >= 2)) {
404 		/* no byte-swap required; chip endian set during init */
405 		writew(*bufp++, ep_data);
406 		count -= 2;
407 	}
408 	buf = (u8 *)bufp;
409 
410 	/* write final byte by placing the NET2272 into 8-bit mode */
411 	if (unlikely(count)) {
412 		tmp = net2272_read(ep->dev, LOCCTL);
413 		net2272_write(ep->dev, LOCCTL, tmp & ~(1 << DATA_WIDTH));
414 		writeb(*buf, ep_data);
415 		net2272_write(ep->dev, LOCCTL, tmp);
416 	}
417 	return length;
418 }
419 
420 /* returns: 0: still running, 1: completed, negative: errno */
421 static int
422 net2272_write_fifo(struct net2272_ep *ep, struct net2272_request *req)
423 {
424 	u8 *buf;
425 	unsigned count, max;
426 	int status;
427 
428 	dev_vdbg(ep->dev->dev, "write_fifo %s actual %d len %d\n",
429 		ep->ep.name, req->req.actual, req->req.length);
430 
431 	/*
432 	 * Keep loading the endpoint until the final packet is loaded,
433 	 * or the endpoint buffer is full.
434 	 */
435  top:
436 	/*
437 	 * Clear interrupt status
438 	 *  - Packet Transmitted interrupt will become set again when the
439 	 *    host successfully takes another packet
440 	 */
441 	net2272_ep_write(ep, EP_STAT0, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT));
442 	while (!(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_FULL))) {
443 		buf = req->req.buf + req->req.actual;
444 		prefetch(buf);
445 
446 		/* force pagesel */
447 		net2272_ep_read(ep, EP_STAT0);
448 
449 		max = (net2272_ep_read(ep, EP_AVAIL1) << 8) |
450 			(net2272_ep_read(ep, EP_AVAIL0));
451 
452 		if (max < ep->ep.maxpacket)
453 			max = (net2272_ep_read(ep, EP_AVAIL1) << 8)
454 				| (net2272_ep_read(ep, EP_AVAIL0));
455 
456 		count = net2272_write_packet(ep, buf, req, max);
457 		/* see if we are done */
458 		if (req->req.length == req->req.actual) {
459 			/* validate short or zlp packet */
460 			if (count < ep->ep.maxpacket)
461 				set_fifo_bytecount(ep, 0);
462 			net2272_done(ep, req, 0);
463 
464 			if (!list_empty(&ep->queue)) {
465 				req = list_entry(ep->queue.next,
466 						struct net2272_request,
467 						queue);
468 				status = net2272_kick_dma(ep, req);
469 
470 				if (status < 0)
471 					if ((net2272_ep_read(ep, EP_STAT0)
472 							& (1 << BUFFER_EMPTY)))
473 						goto top;
474 			}
475 			return 1;
476 		}
477 		net2272_ep_write(ep, EP_STAT0, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT));
478 	}
479 	return 0;
480 }
481 
482 static void
483 net2272_out_flush(struct net2272_ep *ep)
484 {
485 	ASSERT_OUT_NAKING(ep);
486 
487 	net2272_ep_write(ep, EP_STAT0, (1 << DATA_OUT_TOKEN_INTERRUPT)
488 			| (1 << DATA_PACKET_RECEIVED_INTERRUPT));
489 	net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
490 }
491 
492 static int
493 net2272_read_packet(struct net2272_ep *ep, u8 *buf,
494 	struct net2272_request *req, unsigned avail)
495 {
496 	u16 __iomem *ep_data = net2272_reg_addr(ep->dev, EP_DATA);
497 	unsigned is_short;
498 	u16 *bufp;
499 
500 	req->req.actual += avail;
501 
502 	dev_vdbg(ep->dev->dev, "read packet %s req %p len %u avail %u\n",
503 		ep->ep.name, req, avail,
504 		(net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0));
505 
506 	is_short = (avail < ep->ep.maxpacket);
507 
508 	if (unlikely(avail == 0)) {
509 		/* remove any zlp from the buffer */
510 		(void)readw(ep_data);
511 		return is_short;
512 	}
513 
514 	/* Ensure we get the final byte */
515 	if (unlikely(avail % 2))
516 		avail++;
517 	bufp = (u16 *)buf;
518 
519 	do {
520 		*bufp++ = readw(ep_data);
521 		avail -= 2;
522 	} while (avail);
523 
524 	/*
525 	 * To avoid false endpoint available race condition must read
526 	 * ep stat0 twice in the case of a short transfer
527 	 */
528 	if (net2272_ep_read(ep, EP_STAT0) & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT))
529 		net2272_ep_read(ep, EP_STAT0);
530 
531 	return is_short;
532 }
533 
534 static int
535 net2272_read_fifo(struct net2272_ep *ep, struct net2272_request *req)
536 {
537 	u8 *buf;
538 	unsigned is_short;
539 	int count;
540 	int tmp;
541 	int cleanup = 0;
542 	int status = -1;
543 
544 	dev_vdbg(ep->dev->dev, "read_fifo %s actual %d len %d\n",
545 		ep->ep.name, req->req.actual, req->req.length);
546 
547  top:
548 	do {
549 		buf = req->req.buf + req->req.actual;
550 		prefetchw(buf);
551 
552 		count = (net2272_ep_read(ep, EP_AVAIL1) << 8)
553 			| net2272_ep_read(ep, EP_AVAIL0);
554 
555 		net2272_ep_write(ep, EP_STAT0,
556 			(1 << SHORT_PACKET_TRANSFERRED_INTERRUPT) |
557 			(1 << DATA_PACKET_RECEIVED_INTERRUPT));
558 
559 		tmp = req->req.length - req->req.actual;
560 
561 		if (count > tmp) {
562 			if ((tmp % ep->ep.maxpacket) != 0) {
563 				dev_err(ep->dev->dev,
564 					"%s out fifo %d bytes, expected %d\n",
565 					ep->ep.name, count, tmp);
566 				cleanup = 1;
567 			}
568 			count = (tmp > 0) ? tmp : 0;
569 		}
570 
571 		is_short = net2272_read_packet(ep, buf, req, count);
572 
573 		/* completion */
574 		if (unlikely(cleanup || is_short ||
575 				req->req.actual == req->req.length)) {
576 
577 			if (cleanup) {
578 				net2272_out_flush(ep);
579 				net2272_done(ep, req, -EOVERFLOW);
580 			} else
581 				net2272_done(ep, req, 0);
582 
583 			/* re-initialize endpoint transfer registers
584 			 * otherwise they may result in erroneous pre-validation
585 			 * for subsequent control reads
586 			 */
587 			if (unlikely(ep->num == 0)) {
588 				net2272_ep_write(ep, EP_TRANSFER2, 0);
589 				net2272_ep_write(ep, EP_TRANSFER1, 0);
590 				net2272_ep_write(ep, EP_TRANSFER0, 0);
591 			}
592 
593 			if (!list_empty(&ep->queue)) {
594 				req = list_entry(ep->queue.next,
595 					struct net2272_request, queue);
596 				status = net2272_kick_dma(ep, req);
597 				if ((status < 0) &&
598 				    !(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_EMPTY)))
599 					goto top;
600 			}
601 			return 1;
602 		}
603 	} while (!(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_EMPTY)));
604 
605 	return 0;
606 }
607 
608 static void
609 net2272_pio_advance(struct net2272_ep *ep)
610 {
611 	struct net2272_request *req;
612 
613 	if (unlikely(list_empty(&ep->queue)))
614 		return;
615 
616 	req = list_entry(ep->queue.next, struct net2272_request, queue);
617 	(ep->is_in ? net2272_write_fifo : net2272_read_fifo)(ep, req);
618 }
619 
620 /* returns 0 on success, else negative errno */
621 static int
622 net2272_request_dma(struct net2272 *dev, unsigned ep, u32 buf,
623 	unsigned len, unsigned dir)
624 {
625 	dev_vdbg(dev->dev, "request_dma ep %d buf %08x len %d dir %d\n",
626 		ep, buf, len, dir);
627 
628 	/* The NET2272 only supports a single dma channel */
629 	if (dev->dma_busy)
630 		return -EBUSY;
631 	/*
632 	 * EP_TRANSFER (used to determine the number of bytes received
633 	 * in an OUT transfer) is 24 bits wide; don't ask for more than that.
634 	 */
635 	if ((dir == 1) && (len > 0x1000000))
636 		return -EINVAL;
637 
638 	dev->dma_busy = 1;
639 
640 	/* initialize platform's dma */
641 #ifdef CONFIG_USB_PCI
642 	/* NET2272 addr, buffer addr, length, etc. */
643 	switch (dev->dev_id) {
644 	case PCI_DEVICE_ID_RDK1:
645 		/* Setup PLX 9054 DMA mode */
646 		writel((1 << LOCAL_BUS_WIDTH) |
647 			(1 << TA_READY_INPUT_ENABLE) |
648 			(0 << LOCAL_BURST_ENABLE) |
649 			(1 << DONE_INTERRUPT_ENABLE) |
650 			(1 << LOCAL_ADDRESSING_MODE) |
651 			(1 << DEMAND_MODE) |
652 			(1 << DMA_EOT_ENABLE) |
653 			(1 << FAST_SLOW_TERMINATE_MODE_SELECT) |
654 			(1 << DMA_CHANNEL_INTERRUPT_SELECT),
655 			dev->rdk1.plx9054_base_addr + DMAMODE0);
656 
657 		writel(0x100000, dev->rdk1.plx9054_base_addr + DMALADR0);
658 		writel(buf, dev->rdk1.plx9054_base_addr + DMAPADR0);
659 		writel(len, dev->rdk1.plx9054_base_addr + DMASIZ0);
660 		writel((dir << DIRECTION_OF_TRANSFER) |
661 			(1 << INTERRUPT_AFTER_TERMINAL_COUNT),
662 			dev->rdk1.plx9054_base_addr + DMADPR0);
663 		writel((1 << LOCAL_DMA_CHANNEL_0_INTERRUPT_ENABLE) |
664 			readl(dev->rdk1.plx9054_base_addr + INTCSR),
665 			dev->rdk1.plx9054_base_addr + INTCSR);
666 
667 		break;
668 	}
669 #endif
670 
671 	net2272_write(dev, DMAREQ,
672 		(0 << DMA_BUFFER_VALID) |
673 		(1 << DMA_REQUEST_ENABLE) |
674 		(1 << DMA_CONTROL_DACK) |
675 		(dev->dma_eot_polarity << EOT_POLARITY) |
676 		(dev->dma_dack_polarity << DACK_POLARITY) |
677 		(dev->dma_dreq_polarity << DREQ_POLARITY) |
678 		((ep >> 1) << DMA_ENDPOINT_SELECT));
679 
680 	(void) net2272_read(dev, SCRATCH);
681 
682 	return 0;
683 }
684 
685 static void
686 net2272_start_dma(struct net2272 *dev)
687 {
688 	/* start platform's dma controller */
689 #ifdef CONFIG_USB_PCI
690 	switch (dev->dev_id) {
691 	case PCI_DEVICE_ID_RDK1:
692 		writeb((1 << CHANNEL_ENABLE) | (1 << CHANNEL_START),
693 			dev->rdk1.plx9054_base_addr + DMACSR0);
694 		break;
695 	}
696 #endif
697 }
698 
699 /* returns 0 on success, else negative errno */
700 static int
701 net2272_kick_dma(struct net2272_ep *ep, struct net2272_request *req)
702 {
703 	unsigned size;
704 	u8 tmp;
705 
706 	if (!use_dma || (ep->num < 1) || (ep->num > 2) || !ep->dma)
707 		return -EINVAL;
708 
709 	/* don't use dma for odd-length transfers
710 	 * otherwise, we'd need to deal with the last byte with pio
711 	 */
712 	if (req->req.length & 1)
713 		return -EINVAL;
714 
715 	dev_vdbg(ep->dev->dev, "kick_dma %s req %p dma %08llx\n",
716 		ep->ep.name, req, (unsigned long long) req->req.dma);
717 
718 	net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
719 
720 	/* The NET2272 can only use DMA on one endpoint at a time */
721 	if (ep->dev->dma_busy)
722 		return -EBUSY;
723 
724 	/* Make sure we only DMA an even number of bytes (we'll use
725 	 * pio to complete the transfer)
726 	 */
727 	size = req->req.length;
728 	size &= ~1;
729 
730 	/* device-to-host transfer */
731 	if (ep->is_in) {
732 		/* initialize platform's dma controller */
733 		if (net2272_request_dma(ep->dev, ep->num, req->req.dma, size, 0))
734 			/* unable to obtain DMA channel; return error and use pio mode */
735 			return -EBUSY;
736 		req->req.actual += size;
737 
738 	/* host-to-device transfer */
739 	} else {
740 		tmp = net2272_ep_read(ep, EP_STAT0);
741 
742 		/* initialize platform's dma controller */
743 		if (net2272_request_dma(ep->dev, ep->num, req->req.dma, size, 1))
744 			/* unable to obtain DMA channel; return error and use pio mode */
745 			return -EBUSY;
746 
747 		if (!(tmp & (1 << BUFFER_EMPTY)))
748 			ep->not_empty = 1;
749 		else
750 			ep->not_empty = 0;
751 
752 
753 		/* allow the endpoint's buffer to fill */
754 		net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
755 
756 		/* this transfer completed and data's already in the fifo
757 		 * return error so pio gets used.
758 		 */
759 		if (tmp & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
760 
761 			/* deassert dreq */
762 			net2272_write(ep->dev, DMAREQ,
763 				(0 << DMA_BUFFER_VALID) |
764 				(0 << DMA_REQUEST_ENABLE) |
765 				(1 << DMA_CONTROL_DACK) |
766 				(ep->dev->dma_eot_polarity << EOT_POLARITY) |
767 				(ep->dev->dma_dack_polarity << DACK_POLARITY) |
768 				(ep->dev->dma_dreq_polarity << DREQ_POLARITY) |
769 				((ep->num >> 1) << DMA_ENDPOINT_SELECT));
770 
771 			return -EBUSY;
772 		}
773 	}
774 
775 	/* Don't use per-packet interrupts: use dma interrupts only */
776 	net2272_ep_write(ep, EP_IRQENB, 0);
777 
778 	net2272_start_dma(ep->dev);
779 
780 	return 0;
781 }
782 
783 static void net2272_cancel_dma(struct net2272 *dev)
784 {
785 #ifdef CONFIG_USB_PCI
786 	switch (dev->dev_id) {
787 	case PCI_DEVICE_ID_RDK1:
788 		writeb(0, dev->rdk1.plx9054_base_addr + DMACSR0);
789 		writeb(1 << CHANNEL_ABORT, dev->rdk1.plx9054_base_addr + DMACSR0);
790 		while (!(readb(dev->rdk1.plx9054_base_addr + DMACSR0) &
791 		         (1 << CHANNEL_DONE)))
792 			continue;	/* wait for dma to stabalize */
793 
794 		/* dma abort generates an interrupt */
795 		writeb(1 << CHANNEL_CLEAR_INTERRUPT,
796 			dev->rdk1.plx9054_base_addr + DMACSR0);
797 		break;
798 	}
799 #endif
800 
801 	dev->dma_busy = 0;
802 }
803 
804 /*---------------------------------------------------------------------------*/
805 
806 static int
807 net2272_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
808 {
809 	struct net2272_request *req;
810 	struct net2272_ep *ep;
811 	struct net2272 *dev;
812 	unsigned long flags;
813 	int status = -1;
814 	u8 s;
815 
816 	req = container_of(_req, struct net2272_request, req);
817 	if (!_req || !_req->complete || !_req->buf
818 			|| !list_empty(&req->queue))
819 		return -EINVAL;
820 	ep = container_of(_ep, struct net2272_ep, ep);
821 	if (!_ep || (!ep->desc && ep->num != 0))
822 		return -EINVAL;
823 	dev = ep->dev;
824 	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
825 		return -ESHUTDOWN;
826 
827 	/* set up dma mapping in case the caller didn't */
828 	if (use_dma && ep->dma) {
829 		status = usb_gadget_map_request(&dev->gadget, _req,
830 				ep->is_in);
831 		if (status)
832 			return status;
833 	}
834 
835 	dev_vdbg(dev->dev, "%s queue req %p, len %d buf %p dma %08llx %s\n",
836 		_ep->name, _req, _req->length, _req->buf,
837 		(unsigned long long) _req->dma, _req->zero ? "zero" : "!zero");
838 
839 	spin_lock_irqsave(&dev->lock, flags);
840 
841 	_req->status = -EINPROGRESS;
842 	_req->actual = 0;
843 
844 	/* kickstart this i/o queue? */
845 	if (list_empty(&ep->queue) && !ep->stopped) {
846 		/* maybe there's no control data, just status ack */
847 		if (ep->num == 0 && _req->length == 0) {
848 			net2272_done(ep, req, 0);
849 			dev_vdbg(dev->dev, "%s status ack\n", ep->ep.name);
850 			goto done;
851 		}
852 
853 		/* Return zlp, don't let it block subsequent packets */
854 		s = net2272_ep_read(ep, EP_STAT0);
855 		if (s & (1 << BUFFER_EMPTY)) {
856 			/* Buffer is empty check for a blocking zlp, handle it */
857 			if ((s & (1 << NAK_OUT_PACKETS)) &&
858 			    net2272_ep_read(ep, EP_STAT1) & (1 << LOCAL_OUT_ZLP)) {
859 				dev_dbg(dev->dev, "WARNING: returning ZLP short packet termination!\n");
860 				/*
861 				 * Request is going to terminate with a short packet ...
862 				 * hope the client is ready for it!
863 				 */
864 				status = net2272_read_fifo(ep, req);
865 				/* clear short packet naking */
866 				net2272_ep_write(ep, EP_STAT0, (1 << NAK_OUT_PACKETS));
867 				goto done;
868 			}
869 		}
870 
871 		/* try dma first */
872 		status = net2272_kick_dma(ep, req);
873 
874 		if (status < 0) {
875 			/* dma failed (most likely in use by another endpoint)
876 			 * fallback to pio
877 			 */
878 			status = 0;
879 
880 			if (ep->is_in)
881 				status = net2272_write_fifo(ep, req);
882 			else {
883 				s = net2272_ep_read(ep, EP_STAT0);
884 				if ((s & (1 << BUFFER_EMPTY)) == 0)
885 					status = net2272_read_fifo(ep, req);
886 			}
887 
888 			if (unlikely(status != 0)) {
889 				if (status > 0)
890 					status = 0;
891 				req = NULL;
892 			}
893 		}
894 	}
895 	if (likely(req))
896 		list_add_tail(&req->queue, &ep->queue);
897 
898 	if (likely(!list_empty(&ep->queue)))
899 		net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
900  done:
901 	spin_unlock_irqrestore(&dev->lock, flags);
902 
903 	return 0;
904 }
905 
906 /* dequeue ALL requests */
907 static void
908 net2272_dequeue_all(struct net2272_ep *ep)
909 {
910 	struct net2272_request *req;
911 
912 	/* called with spinlock held */
913 	ep->stopped = 1;
914 
915 	while (!list_empty(&ep->queue)) {
916 		req = list_entry(ep->queue.next,
917 				struct net2272_request,
918 				queue);
919 		net2272_done(ep, req, -ESHUTDOWN);
920 	}
921 }
922 
923 /* dequeue JUST ONE request */
924 static int
925 net2272_dequeue(struct usb_ep *_ep, struct usb_request *_req)
926 {
927 	struct net2272_ep *ep;
928 	struct net2272_request *req;
929 	unsigned long flags;
930 	int stopped;
931 
932 	ep = container_of(_ep, struct net2272_ep, ep);
933 	if (!_ep || (!ep->desc && ep->num != 0) || !_req)
934 		return -EINVAL;
935 
936 	spin_lock_irqsave(&ep->dev->lock, flags);
937 	stopped = ep->stopped;
938 	ep->stopped = 1;
939 
940 	/* make sure it's still queued on this endpoint */
941 	list_for_each_entry(req, &ep->queue, queue) {
942 		if (&req->req == _req)
943 			break;
944 	}
945 	if (&req->req != _req) {
946 		ep->stopped = stopped;
947 		spin_unlock_irqrestore(&ep->dev->lock, flags);
948 		return -EINVAL;
949 	}
950 
951 	/* queue head may be partially complete */
952 	if (ep->queue.next == &req->queue) {
953 		dev_dbg(ep->dev->dev, "unlink (%s) pio\n", _ep->name);
954 		net2272_done(ep, req, -ECONNRESET);
955 	}
956 	req = NULL;
957 	ep->stopped = stopped;
958 
959 	spin_unlock_irqrestore(&ep->dev->lock, flags);
960 	return 0;
961 }
962 
963 /*---------------------------------------------------------------------------*/
964 
965 static int
966 net2272_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
967 {
968 	struct net2272_ep *ep;
969 	unsigned long flags;
970 	int ret = 0;
971 
972 	ep = container_of(_ep, struct net2272_ep, ep);
973 	if (!_ep || (!ep->desc && ep->num != 0))
974 		return -EINVAL;
975 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
976 		return -ESHUTDOWN;
977 	if (ep->desc /* not ep0 */ && usb_endpoint_xfer_isoc(ep->desc))
978 		return -EINVAL;
979 
980 	spin_lock_irqsave(&ep->dev->lock, flags);
981 	if (!list_empty(&ep->queue))
982 		ret = -EAGAIN;
983 	else if (ep->is_in && value && net2272_fifo_status(_ep) != 0)
984 		ret = -EAGAIN;
985 	else {
986 		dev_vdbg(ep->dev->dev, "%s %s %s\n", _ep->name,
987 			value ? "set" : "clear",
988 			wedged ? "wedge" : "halt");
989 		/* set/clear */
990 		if (value) {
991 			if (ep->num == 0)
992 				ep->dev->protocol_stall = 1;
993 			else
994 				set_halt(ep);
995 			if (wedged)
996 				ep->wedged = 1;
997 		} else {
998 			clear_halt(ep);
999 			ep->wedged = 0;
1000 		}
1001 	}
1002 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1003 
1004 	return ret;
1005 }
1006 
1007 static int
1008 net2272_set_halt(struct usb_ep *_ep, int value)
1009 {
1010 	return net2272_set_halt_and_wedge(_ep, value, 0);
1011 }
1012 
1013 static int
1014 net2272_set_wedge(struct usb_ep *_ep)
1015 {
1016 	if (!_ep || _ep->name == ep0name)
1017 		return -EINVAL;
1018 	return net2272_set_halt_and_wedge(_ep, 1, 1);
1019 }
1020 
1021 static int
1022 net2272_fifo_status(struct usb_ep *_ep)
1023 {
1024 	struct net2272_ep *ep;
1025 	u16 avail;
1026 
1027 	ep = container_of(_ep, struct net2272_ep, ep);
1028 	if (!_ep || (!ep->desc && ep->num != 0))
1029 		return -ENODEV;
1030 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1031 		return -ESHUTDOWN;
1032 
1033 	avail = net2272_ep_read(ep, EP_AVAIL1) << 8;
1034 	avail |= net2272_ep_read(ep, EP_AVAIL0);
1035 	if (avail > ep->fifo_size)
1036 		return -EOVERFLOW;
1037 	if (ep->is_in)
1038 		avail = ep->fifo_size - avail;
1039 	return avail;
1040 }
1041 
1042 static void
1043 net2272_fifo_flush(struct usb_ep *_ep)
1044 {
1045 	struct net2272_ep *ep;
1046 
1047 	ep = container_of(_ep, struct net2272_ep, ep);
1048 	if (!_ep || (!ep->desc && ep->num != 0))
1049 		return;
1050 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1051 		return;
1052 
1053 	net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
1054 }
1055 
1056 static const struct usb_ep_ops net2272_ep_ops = {
1057 	.enable        = net2272_enable,
1058 	.disable       = net2272_disable,
1059 
1060 	.alloc_request = net2272_alloc_request,
1061 	.free_request  = net2272_free_request,
1062 
1063 	.queue         = net2272_queue,
1064 	.dequeue       = net2272_dequeue,
1065 
1066 	.set_halt      = net2272_set_halt,
1067 	.set_wedge     = net2272_set_wedge,
1068 	.fifo_status   = net2272_fifo_status,
1069 	.fifo_flush    = net2272_fifo_flush,
1070 };
1071 
1072 /*---------------------------------------------------------------------------*/
1073 
1074 static int
1075 net2272_get_frame(struct usb_gadget *_gadget)
1076 {
1077 	struct net2272 *dev;
1078 	unsigned long flags;
1079 	u16 ret;
1080 
1081 	if (!_gadget)
1082 		return -ENODEV;
1083 	dev = container_of(_gadget, struct net2272, gadget);
1084 	spin_lock_irqsave(&dev->lock, flags);
1085 
1086 	ret = net2272_read(dev, FRAME1) << 8;
1087 	ret |= net2272_read(dev, FRAME0);
1088 
1089 	spin_unlock_irqrestore(&dev->lock, flags);
1090 	return ret;
1091 }
1092 
1093 static int
1094 net2272_wakeup(struct usb_gadget *_gadget)
1095 {
1096 	struct net2272 *dev;
1097 	u8 tmp;
1098 	unsigned long flags;
1099 
1100 	if (!_gadget)
1101 		return 0;
1102 	dev = container_of(_gadget, struct net2272, gadget);
1103 
1104 	spin_lock_irqsave(&dev->lock, flags);
1105 	tmp = net2272_read(dev, USBCTL0);
1106 	if (tmp & (1 << IO_WAKEUP_ENABLE))
1107 		net2272_write(dev, USBCTL1, (1 << GENERATE_RESUME));
1108 
1109 	spin_unlock_irqrestore(&dev->lock, flags);
1110 
1111 	return 0;
1112 }
1113 
1114 static int
1115 net2272_set_selfpowered(struct usb_gadget *_gadget, int value)
1116 {
1117 	if (!_gadget)
1118 		return -ENODEV;
1119 
1120 	_gadget->is_selfpowered = (value != 0);
1121 
1122 	return 0;
1123 }
1124 
1125 static int
1126 net2272_pullup(struct usb_gadget *_gadget, int is_on)
1127 {
1128 	struct net2272 *dev;
1129 	u8 tmp;
1130 	unsigned long flags;
1131 
1132 	if (!_gadget)
1133 		return -ENODEV;
1134 	dev = container_of(_gadget, struct net2272, gadget);
1135 
1136 	spin_lock_irqsave(&dev->lock, flags);
1137 	tmp = net2272_read(dev, USBCTL0);
1138 	dev->softconnect = (is_on != 0);
1139 	if (is_on)
1140 		tmp |= (1 << USB_DETECT_ENABLE);
1141 	else
1142 		tmp &= ~(1 << USB_DETECT_ENABLE);
1143 	net2272_write(dev, USBCTL0, tmp);
1144 	spin_unlock_irqrestore(&dev->lock, flags);
1145 
1146 	return 0;
1147 }
1148 
1149 static int net2272_start(struct usb_gadget *_gadget,
1150 		struct usb_gadget_driver *driver);
1151 static int net2272_stop(struct usb_gadget *_gadget);
1152 
1153 static const struct usb_gadget_ops net2272_ops = {
1154 	.get_frame	= net2272_get_frame,
1155 	.wakeup		= net2272_wakeup,
1156 	.set_selfpowered = net2272_set_selfpowered,
1157 	.pullup		= net2272_pullup,
1158 	.udc_start	= net2272_start,
1159 	.udc_stop	= net2272_stop,
1160 };
1161 
1162 /*---------------------------------------------------------------------------*/
1163 
1164 static ssize_t
1165 registers_show(struct device *_dev, struct device_attribute *attr, char *buf)
1166 {
1167 	struct net2272 *dev;
1168 	char *next;
1169 	unsigned size, t;
1170 	unsigned long flags;
1171 	u8 t1, t2;
1172 	int i;
1173 	const char *s;
1174 
1175 	dev = dev_get_drvdata(_dev);
1176 	next = buf;
1177 	size = PAGE_SIZE;
1178 	spin_lock_irqsave(&dev->lock, flags);
1179 
1180 	/* Main Control Registers */
1181 	t = scnprintf(next, size, "%s version %s,"
1182 		"chiprev %02x, locctl %02x\n"
1183 		"irqenb0 %02x irqenb1 %02x "
1184 		"irqstat0 %02x irqstat1 %02x\n",
1185 		driver_name, driver_vers, dev->chiprev,
1186 		net2272_read(dev, LOCCTL),
1187 		net2272_read(dev, IRQENB0),
1188 		net2272_read(dev, IRQENB1),
1189 		net2272_read(dev, IRQSTAT0),
1190 		net2272_read(dev, IRQSTAT1));
1191 	size -= t;
1192 	next += t;
1193 
1194 	/* DMA */
1195 	t1 = net2272_read(dev, DMAREQ);
1196 	t = scnprintf(next, size, "\ndmareq %02x: %s %s%s%s%s\n",
1197 		t1, ep_name[(t1 & 0x01) + 1],
1198 		t1 & (1 << DMA_CONTROL_DACK) ? "dack " : "",
1199 		t1 & (1 << DMA_REQUEST_ENABLE) ? "reqenb " : "",
1200 		t1 & (1 << DMA_REQUEST) ? "req " : "",
1201 		t1 & (1 << DMA_BUFFER_VALID) ? "valid " : "");
1202 	size -= t;
1203 	next += t;
1204 
1205 	/* USB Control Registers */
1206 	t1 = net2272_read(dev, USBCTL1);
1207 	if (t1 & (1 << VBUS_PIN)) {
1208 		if (t1 & (1 << USB_HIGH_SPEED))
1209 			s = "high speed";
1210 		else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1211 			s = "powered";
1212 		else
1213 			s = "full speed";
1214 	} else
1215 		s = "not attached";
1216 	t = scnprintf(next, size,
1217 		"usbctl0 %02x usbctl1 %02x addr 0x%02x (%s)\n",
1218 		net2272_read(dev, USBCTL0), t1,
1219 		net2272_read(dev, OURADDR), s);
1220 	size -= t;
1221 	next += t;
1222 
1223 	/* Endpoint Registers */
1224 	for (i = 0; i < 4; ++i) {
1225 		struct net2272_ep *ep;
1226 
1227 		ep = &dev->ep[i];
1228 		if (i && !ep->desc)
1229 			continue;
1230 
1231 		t1 = net2272_ep_read(ep, EP_CFG);
1232 		t2 = net2272_ep_read(ep, EP_RSPSET);
1233 		t = scnprintf(next, size,
1234 			"\n%s\tcfg %02x rsp (%02x) %s%s%s%s%s%s%s%s"
1235 			"irqenb %02x\n",
1236 			ep->ep.name, t1, t2,
1237 			(t2 & (1 << ALT_NAK_OUT_PACKETS)) ? "NAK " : "",
1238 			(t2 & (1 << HIDE_STATUS_PHASE)) ? "hide " : "",
1239 			(t2 & (1 << AUTOVALIDATE)) ? "auto " : "",
1240 			(t2 & (1 << INTERRUPT_MODE)) ? "interrupt " : "",
1241 			(t2 & (1 << CONTROL_STATUS_PHASE_HANDSHAKE)) ? "status " : "",
1242 			(t2 & (1 << NAK_OUT_PACKETS_MODE)) ? "NAKmode " : "",
1243 			(t2 & (1 << ENDPOINT_TOGGLE)) ? "DATA1 " : "DATA0 ",
1244 			(t2 & (1 << ENDPOINT_HALT)) ? "HALT " : "",
1245 			net2272_ep_read(ep, EP_IRQENB));
1246 		size -= t;
1247 		next += t;
1248 
1249 		t = scnprintf(next, size,
1250 			"\tstat0 %02x stat1 %02x avail %04x "
1251 			"(ep%d%s-%s)%s\n",
1252 			net2272_ep_read(ep, EP_STAT0),
1253 			net2272_ep_read(ep, EP_STAT1),
1254 			(net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0),
1255 			t1 & 0x0f,
1256 			ep->is_in ? "in" : "out",
1257 			type_string(t1 >> 5),
1258 			ep->stopped ? "*" : "");
1259 		size -= t;
1260 		next += t;
1261 
1262 		t = scnprintf(next, size,
1263 			"\tep_transfer %06x\n",
1264 			((net2272_ep_read(ep, EP_TRANSFER2) & 0xff) << 16) |
1265 			((net2272_ep_read(ep, EP_TRANSFER1) & 0xff) << 8) |
1266 			((net2272_ep_read(ep, EP_TRANSFER0) & 0xff)));
1267 		size -= t;
1268 		next += t;
1269 
1270 		t1 = net2272_ep_read(ep, EP_BUFF_STATES) & 0x03;
1271 		t2 = (net2272_ep_read(ep, EP_BUFF_STATES) >> 2) & 0x03;
1272 		t = scnprintf(next, size,
1273 			"\tbuf-a %s buf-b %s\n",
1274 			buf_state_string(t1),
1275 			buf_state_string(t2));
1276 		size -= t;
1277 		next += t;
1278 	}
1279 
1280 	spin_unlock_irqrestore(&dev->lock, flags);
1281 
1282 	return PAGE_SIZE - size;
1283 }
1284 static DEVICE_ATTR_RO(registers);
1285 
1286 /*---------------------------------------------------------------------------*/
1287 
1288 static void
1289 net2272_set_fifo_mode(struct net2272 *dev, int mode)
1290 {
1291 	u8 tmp;
1292 
1293 	tmp = net2272_read(dev, LOCCTL) & 0x3f;
1294 	tmp |= (mode << 6);
1295 	net2272_write(dev, LOCCTL, tmp);
1296 
1297 	INIT_LIST_HEAD(&dev->gadget.ep_list);
1298 
1299 	/* always ep-a, ep-c ... maybe not ep-b */
1300 	list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1301 
1302 	switch (mode) {
1303 	case 0:
1304 		list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1305 		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 512;
1306 		break;
1307 	case 1:
1308 		list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1309 		dev->ep[1].fifo_size = 1024;
1310 		dev->ep[2].fifo_size = 512;
1311 		break;
1312 	case 2:
1313 		list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1314 		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
1315 		break;
1316 	case 3:
1317 		dev->ep[1].fifo_size = 1024;
1318 		break;
1319 	}
1320 
1321 	/* ep-c is always 2 512 byte buffers */
1322 	list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1323 	dev->ep[3].fifo_size = 512;
1324 }
1325 
1326 /*---------------------------------------------------------------------------*/
1327 
1328 static void
1329 net2272_usb_reset(struct net2272 *dev)
1330 {
1331 	dev->gadget.speed = USB_SPEED_UNKNOWN;
1332 
1333 	net2272_cancel_dma(dev);
1334 
1335 	net2272_write(dev, IRQENB0, 0);
1336 	net2272_write(dev, IRQENB1, 0);
1337 
1338 	/* clear irq state */
1339 	net2272_write(dev, IRQSTAT0, 0xff);
1340 	net2272_write(dev, IRQSTAT1, ~(1 << SUSPEND_REQUEST_INTERRUPT));
1341 
1342 	net2272_write(dev, DMAREQ,
1343 		(0 << DMA_BUFFER_VALID) |
1344 		(0 << DMA_REQUEST_ENABLE) |
1345 		(1 << DMA_CONTROL_DACK) |
1346 		(dev->dma_eot_polarity << EOT_POLARITY) |
1347 		(dev->dma_dack_polarity << DACK_POLARITY) |
1348 		(dev->dma_dreq_polarity << DREQ_POLARITY) |
1349 		((dma_ep >> 1) << DMA_ENDPOINT_SELECT));
1350 
1351 	net2272_cancel_dma(dev);
1352 	net2272_set_fifo_mode(dev, (fifo_mode <= 3) ? fifo_mode : 0);
1353 
1354 	/* Set the NET2272 ep fifo data width to 16-bit mode and for correct byte swapping
1355 	 * note that the higher level gadget drivers are expected to convert data to little endian.
1356 	 * Enable byte swap for your local bus/cpu if needed by setting BYTE_SWAP in LOCCTL here
1357 	 */
1358 	net2272_write(dev, LOCCTL, net2272_read(dev, LOCCTL) | (1 << DATA_WIDTH));
1359 	net2272_write(dev, LOCCTL1, (dma_mode << DMA_MODE));
1360 }
1361 
1362 static void
1363 net2272_usb_reinit(struct net2272 *dev)
1364 {
1365 	int i;
1366 
1367 	/* basic endpoint init */
1368 	for (i = 0; i < 4; ++i) {
1369 		struct net2272_ep *ep = &dev->ep[i];
1370 
1371 		ep->ep.name = ep_name[i];
1372 		ep->dev = dev;
1373 		ep->num = i;
1374 		ep->not_empty = 0;
1375 
1376 		if (use_dma && ep->num == dma_ep)
1377 			ep->dma = 1;
1378 
1379 		if (i > 0 && i <= 3)
1380 			ep->fifo_size = 512;
1381 		else
1382 			ep->fifo_size = 64;
1383 		net2272_ep_reset(ep);
1384 
1385 		if (i == 0) {
1386 			ep->ep.caps.type_control = true;
1387 		} else {
1388 			ep->ep.caps.type_iso = true;
1389 			ep->ep.caps.type_bulk = true;
1390 			ep->ep.caps.type_int = true;
1391 		}
1392 
1393 		ep->ep.caps.dir_in = true;
1394 		ep->ep.caps.dir_out = true;
1395 	}
1396 	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
1397 
1398 	dev->gadget.ep0 = &dev->ep[0].ep;
1399 	dev->ep[0].stopped = 0;
1400 	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1401 }
1402 
1403 static void
1404 net2272_ep0_start(struct net2272 *dev)
1405 {
1406 	struct net2272_ep *ep0 = &dev->ep[0];
1407 
1408 	net2272_ep_write(ep0, EP_RSPSET,
1409 		(1 << NAK_OUT_PACKETS_MODE) |
1410 		(1 << ALT_NAK_OUT_PACKETS));
1411 	net2272_ep_write(ep0, EP_RSPCLR,
1412 		(1 << HIDE_STATUS_PHASE) |
1413 		(1 << CONTROL_STATUS_PHASE_HANDSHAKE));
1414 	net2272_write(dev, USBCTL0,
1415 		(dev->softconnect << USB_DETECT_ENABLE) |
1416 		(1 << USB_ROOT_PORT_WAKEUP_ENABLE) |
1417 		(1 << IO_WAKEUP_ENABLE));
1418 	net2272_write(dev, IRQENB0,
1419 		(1 << SETUP_PACKET_INTERRUPT_ENABLE) |
1420 		(1 << ENDPOINT_0_INTERRUPT_ENABLE) |
1421 		(1 << DMA_DONE_INTERRUPT_ENABLE));
1422 	net2272_write(dev, IRQENB1,
1423 		(1 << VBUS_INTERRUPT_ENABLE) |
1424 		(1 << ROOT_PORT_RESET_INTERRUPT_ENABLE) |
1425 		(1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE));
1426 }
1427 
1428 /* when a driver is successfully registered, it will receive
1429  * control requests including set_configuration(), which enables
1430  * non-control requests.  then usb traffic follows until a
1431  * disconnect is reported.  then a host may connect again, or
1432  * the driver might get unbound.
1433  */
1434 static int net2272_start(struct usb_gadget *_gadget,
1435 		struct usb_gadget_driver *driver)
1436 {
1437 	struct net2272 *dev;
1438 	unsigned i;
1439 
1440 	if (!driver || !driver->setup ||
1441 	    driver->max_speed != USB_SPEED_HIGH)
1442 		return -EINVAL;
1443 
1444 	dev = container_of(_gadget, struct net2272, gadget);
1445 
1446 	for (i = 0; i < 4; ++i)
1447 		dev->ep[i].irqs = 0;
1448 	/* hook up the driver ... */
1449 	dev->softconnect = 1;
1450 	driver->driver.bus = NULL;
1451 	dev->driver = driver;
1452 
1453 	/* ... then enable host detection and ep0; and we're ready
1454 	 * for set_configuration as well as eventual disconnect.
1455 	 */
1456 	net2272_ep0_start(dev);
1457 
1458 	return 0;
1459 }
1460 
1461 static void
1462 stop_activity(struct net2272 *dev, struct usb_gadget_driver *driver)
1463 {
1464 	int i;
1465 
1466 	/* don't disconnect if it's not connected */
1467 	if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1468 		driver = NULL;
1469 
1470 	/* stop hardware; prevent new request submissions;
1471 	 * and kill any outstanding requests.
1472 	 */
1473 	net2272_usb_reset(dev);
1474 	for (i = 0; i < 4; ++i)
1475 		net2272_dequeue_all(&dev->ep[i]);
1476 
1477 	/* report disconnect; the driver is already quiesced */
1478 	if (driver) {
1479 		spin_unlock(&dev->lock);
1480 		driver->disconnect(&dev->gadget);
1481 		spin_lock(&dev->lock);
1482 	}
1483 
1484 	net2272_usb_reinit(dev);
1485 }
1486 
1487 static int net2272_stop(struct usb_gadget *_gadget)
1488 {
1489 	struct net2272 *dev;
1490 	unsigned long flags;
1491 
1492 	dev = container_of(_gadget, struct net2272, gadget);
1493 
1494 	spin_lock_irqsave(&dev->lock, flags);
1495 	stop_activity(dev, NULL);
1496 	spin_unlock_irqrestore(&dev->lock, flags);
1497 
1498 	dev->driver = NULL;
1499 
1500 	return 0;
1501 }
1502 
1503 /*---------------------------------------------------------------------------*/
1504 /* handle ep-a/ep-b dma completions */
1505 static void
1506 net2272_handle_dma(struct net2272_ep *ep)
1507 {
1508 	struct net2272_request *req;
1509 	unsigned len;
1510 	int status;
1511 
1512 	if (!list_empty(&ep->queue))
1513 		req = list_entry(ep->queue.next,
1514 				struct net2272_request, queue);
1515 	else
1516 		req = NULL;
1517 
1518 	dev_vdbg(ep->dev->dev, "handle_dma %s req %p\n", ep->ep.name, req);
1519 
1520 	/* Ensure DREQ is de-asserted */
1521 	net2272_write(ep->dev, DMAREQ,
1522 		(0 << DMA_BUFFER_VALID)
1523 	      | (0 << DMA_REQUEST_ENABLE)
1524 	      | (1 << DMA_CONTROL_DACK)
1525 	      | (ep->dev->dma_eot_polarity << EOT_POLARITY)
1526 	      | (ep->dev->dma_dack_polarity << DACK_POLARITY)
1527 	      | (ep->dev->dma_dreq_polarity << DREQ_POLARITY)
1528 	      | (ep->dma << DMA_ENDPOINT_SELECT));
1529 
1530 	ep->dev->dma_busy = 0;
1531 
1532 	net2272_ep_write(ep, EP_IRQENB,
1533 		  (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
1534 		| (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
1535 		| net2272_ep_read(ep, EP_IRQENB));
1536 
1537 	/* device-to-host transfer completed */
1538 	if (ep->is_in) {
1539 		/* validate a short packet or zlp if necessary */
1540 		if ((req->req.length % ep->ep.maxpacket != 0) ||
1541 				req->req.zero)
1542 			set_fifo_bytecount(ep, 0);
1543 
1544 		net2272_done(ep, req, 0);
1545 		if (!list_empty(&ep->queue)) {
1546 			req = list_entry(ep->queue.next,
1547 					struct net2272_request, queue);
1548 			status = net2272_kick_dma(ep, req);
1549 			if (status < 0)
1550 				net2272_pio_advance(ep);
1551 		}
1552 
1553 	/* host-to-device transfer completed */
1554 	} else {
1555 		/* terminated with a short packet? */
1556 		if (net2272_read(ep->dev, IRQSTAT0) &
1557 				(1 << DMA_DONE_INTERRUPT)) {
1558 			/* abort system dma */
1559 			net2272_cancel_dma(ep->dev);
1560 		}
1561 
1562 		/* EP_TRANSFER will contain the number of bytes
1563 		 * actually received.
1564 		 * NOTE: There is no overflow detection on EP_TRANSFER:
1565 		 * We can't deal with transfers larger than 2^24 bytes!
1566 		 */
1567 		len = (net2272_ep_read(ep, EP_TRANSFER2) << 16)
1568 			| (net2272_ep_read(ep, EP_TRANSFER1) << 8)
1569 			| (net2272_ep_read(ep, EP_TRANSFER0));
1570 
1571 		if (ep->not_empty)
1572 			len += 4;
1573 
1574 		req->req.actual += len;
1575 
1576 		/* get any remaining data */
1577 		net2272_pio_advance(ep);
1578 	}
1579 }
1580 
1581 /*---------------------------------------------------------------------------*/
1582 
1583 static void
1584 net2272_handle_ep(struct net2272_ep *ep)
1585 {
1586 	struct net2272_request *req;
1587 	u8 stat0, stat1;
1588 
1589 	if (!list_empty(&ep->queue))
1590 		req = list_entry(ep->queue.next,
1591 			struct net2272_request, queue);
1592 	else
1593 		req = NULL;
1594 
1595 	/* ack all, and handle what we care about */
1596 	stat0 = net2272_ep_read(ep, EP_STAT0);
1597 	stat1 = net2272_ep_read(ep, EP_STAT1);
1598 	ep->irqs++;
1599 
1600 	dev_vdbg(ep->dev->dev, "%s ack ep_stat0 %02x, ep_stat1 %02x, req %p\n",
1601 		ep->ep.name, stat0, stat1, req ? &req->req : NULL);
1602 
1603 	net2272_ep_write(ep, EP_STAT0, stat0 &
1604 		~((1 << NAK_OUT_PACKETS)
1605 		| (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)));
1606 	net2272_ep_write(ep, EP_STAT1, stat1);
1607 
1608 	/* data packet(s) received (in the fifo, OUT)
1609 	 * direction must be validated, otherwise control read status phase
1610 	 * could be interpreted as a valid packet
1611 	 */
1612 	if (!ep->is_in && (stat0 & (1 << DATA_PACKET_RECEIVED_INTERRUPT)))
1613 		net2272_pio_advance(ep);
1614 	/* data packet(s) transmitted (IN) */
1615 	else if (stat0 & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
1616 		net2272_pio_advance(ep);
1617 }
1618 
1619 static struct net2272_ep *
1620 net2272_get_ep_by_addr(struct net2272 *dev, u16 wIndex)
1621 {
1622 	struct net2272_ep *ep;
1623 
1624 	if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
1625 		return &dev->ep[0];
1626 
1627 	list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
1628 		u8 bEndpointAddress;
1629 
1630 		if (!ep->desc)
1631 			continue;
1632 		bEndpointAddress = ep->desc->bEndpointAddress;
1633 		if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
1634 			continue;
1635 		if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
1636 			return ep;
1637 	}
1638 	return NULL;
1639 }
1640 
1641 /*
1642  * USB Test Packet:
1643  * JKJKJKJK * 9
1644  * JJKKJJKK * 8
1645  * JJJJKKKK * 8
1646  * JJJJJJJKKKKKKK * 8
1647  * JJJJJJJK * 8
1648  * {JKKKKKKK * 10}, JK
1649  */
1650 static const u8 net2272_test_packet[] = {
1651 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1652 	0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1653 	0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1654 	0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1655 	0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1656 	0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFD, 0x7E
1657 };
1658 
1659 static void
1660 net2272_set_test_mode(struct net2272 *dev, int mode)
1661 {
1662 	int i;
1663 
1664 	/* Disable all net2272 interrupts:
1665 	 * Nothing but a power cycle should stop the test.
1666 	 */
1667 	net2272_write(dev, IRQENB0, 0x00);
1668 	net2272_write(dev, IRQENB1, 0x00);
1669 
1670 	/* Force tranceiver to high-speed */
1671 	net2272_write(dev, XCVRDIAG, 1 << FORCE_HIGH_SPEED);
1672 
1673 	net2272_write(dev, PAGESEL, 0);
1674 	net2272_write(dev, EP_STAT0, 1 << DATA_PACKET_TRANSMITTED_INTERRUPT);
1675 	net2272_write(dev, EP_RSPCLR,
1676 			  (1 << CONTROL_STATUS_PHASE_HANDSHAKE)
1677 			| (1 << HIDE_STATUS_PHASE));
1678 	net2272_write(dev, EP_CFG, 1 << ENDPOINT_DIRECTION);
1679 	net2272_write(dev, EP_STAT1, 1 << BUFFER_FLUSH);
1680 
1681 	/* wait for status phase to complete */
1682 	while (!(net2272_read(dev, EP_STAT0) &
1683 				(1 << DATA_PACKET_TRANSMITTED_INTERRUPT)))
1684 		;
1685 
1686 	/* Enable test mode */
1687 	net2272_write(dev, USBTEST, mode);
1688 
1689 	/* load test packet */
1690 	if (mode == USB_TEST_PACKET) {
1691 		/* switch to 8 bit mode */
1692 		net2272_write(dev, LOCCTL, net2272_read(dev, LOCCTL) &
1693 				~(1 << DATA_WIDTH));
1694 
1695 		for (i = 0; i < sizeof(net2272_test_packet); ++i)
1696 			net2272_write(dev, EP_DATA, net2272_test_packet[i]);
1697 
1698 		/* Validate test packet */
1699 		net2272_write(dev, EP_TRANSFER0, 0);
1700 	}
1701 }
1702 
1703 static void
1704 net2272_handle_stat0_irqs(struct net2272 *dev, u8 stat)
1705 {
1706 	struct net2272_ep *ep;
1707 	u8 num, scratch;
1708 
1709 	/* starting a control request? */
1710 	if (unlikely(stat & (1 << SETUP_PACKET_INTERRUPT))) {
1711 		union {
1712 			u8 raw[8];
1713 			struct usb_ctrlrequest	r;
1714 		} u;
1715 		int tmp = 0;
1716 		struct net2272_request *req;
1717 
1718 		if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
1719 			if (net2272_read(dev, USBCTL1) & (1 << USB_HIGH_SPEED))
1720 				dev->gadget.speed = USB_SPEED_HIGH;
1721 			else
1722 				dev->gadget.speed = USB_SPEED_FULL;
1723 			dev_dbg(dev->dev, "%s\n",
1724 				usb_speed_string(dev->gadget.speed));
1725 		}
1726 
1727 		ep = &dev->ep[0];
1728 		ep->irqs++;
1729 
1730 		/* make sure any leftover interrupt state is cleared */
1731 		stat &= ~(1 << ENDPOINT_0_INTERRUPT);
1732 		while (!list_empty(&ep->queue)) {
1733 			req = list_entry(ep->queue.next,
1734 				struct net2272_request, queue);
1735 			net2272_done(ep, req,
1736 				(req->req.actual == req->req.length) ? 0 : -EPROTO);
1737 		}
1738 		ep->stopped = 0;
1739 		dev->protocol_stall = 0;
1740 		net2272_ep_write(ep, EP_STAT0,
1741 			    (1 << DATA_IN_TOKEN_INTERRUPT)
1742 			  | (1 << DATA_OUT_TOKEN_INTERRUPT)
1743 			  | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
1744 			  | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
1745 			  | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT));
1746 		net2272_ep_write(ep, EP_STAT1,
1747 			    (1 << TIMEOUT)
1748 			  | (1 << USB_OUT_ACK_SENT)
1749 			  | (1 << USB_OUT_NAK_SENT)
1750 			  | (1 << USB_IN_ACK_RCVD)
1751 			  | (1 << USB_IN_NAK_SENT)
1752 			  | (1 << USB_STALL_SENT)
1753 			  | (1 << LOCAL_OUT_ZLP));
1754 
1755 		/*
1756 		 * Ensure Control Read pre-validation setting is beyond maximum size
1757 		 *  - Control Writes can leave non-zero values in EP_TRANSFER. If
1758 		 *    an EP0 transfer following the Control Write is a Control Read,
1759 		 *    the NET2272 sees the non-zero EP_TRANSFER as an unexpected
1760 		 *    pre-validation count.
1761 		 *  - Setting EP_TRANSFER beyond the maximum EP0 transfer size ensures
1762 		 *    the pre-validation count cannot cause an unexpected validatation
1763 		 */
1764 		net2272_write(dev, PAGESEL, 0);
1765 		net2272_write(dev, EP_TRANSFER2, 0xff);
1766 		net2272_write(dev, EP_TRANSFER1, 0xff);
1767 		net2272_write(dev, EP_TRANSFER0, 0xff);
1768 
1769 		u.raw[0] = net2272_read(dev, SETUP0);
1770 		u.raw[1] = net2272_read(dev, SETUP1);
1771 		u.raw[2] = net2272_read(dev, SETUP2);
1772 		u.raw[3] = net2272_read(dev, SETUP3);
1773 		u.raw[4] = net2272_read(dev, SETUP4);
1774 		u.raw[5] = net2272_read(dev, SETUP5);
1775 		u.raw[6] = net2272_read(dev, SETUP6);
1776 		u.raw[7] = net2272_read(dev, SETUP7);
1777 		/*
1778 		 * If you have a big endian cpu make sure le16_to_cpus
1779 		 * performs the proper byte swapping here...
1780 		 */
1781 		le16_to_cpus(&u.r.wValue);
1782 		le16_to_cpus(&u.r.wIndex);
1783 		le16_to_cpus(&u.r.wLength);
1784 
1785 		/* ack the irq */
1786 		net2272_write(dev, IRQSTAT0, 1 << SETUP_PACKET_INTERRUPT);
1787 		stat ^= (1 << SETUP_PACKET_INTERRUPT);
1788 
1789 		/* watch control traffic at the token level, and force
1790 		 * synchronization before letting the status phase happen.
1791 		 */
1792 		ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1793 		if (ep->is_in) {
1794 			scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
1795 				| (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE)
1796 				| (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE);
1797 			stop_out_naking(ep);
1798 		} else
1799 			scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
1800 				| (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE)
1801 				| (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE);
1802 		net2272_ep_write(ep, EP_IRQENB, scratch);
1803 
1804 		if ((u.r.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1805 			goto delegate;
1806 		switch (u.r.bRequest) {
1807 		case USB_REQ_GET_STATUS: {
1808 			struct net2272_ep *e;
1809 			u16 status = 0;
1810 
1811 			switch (u.r.bRequestType & USB_RECIP_MASK) {
1812 			case USB_RECIP_ENDPOINT:
1813 				e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1814 				if (!e || u.r.wLength > 2)
1815 					goto do_stall;
1816 				if (net2272_ep_read(e, EP_RSPSET) & (1 << ENDPOINT_HALT))
1817 					status = cpu_to_le16(1);
1818 				else
1819 					status = cpu_to_le16(0);
1820 
1821 				/* don't bother with a request object! */
1822 				net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1823 				writew(status, net2272_reg_addr(dev, EP_DATA));
1824 				set_fifo_bytecount(&dev->ep[0], 0);
1825 				allow_status(ep);
1826 				dev_vdbg(dev->dev, "%s stat %02x\n",
1827 					ep->ep.name, status);
1828 				goto next_endpoints;
1829 			case USB_RECIP_DEVICE:
1830 				if (u.r.wLength > 2)
1831 					goto do_stall;
1832 				if (dev->gadget.is_selfpowered)
1833 					status = (1 << USB_DEVICE_SELF_POWERED);
1834 
1835 				/* don't bother with a request object! */
1836 				net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1837 				writew(status, net2272_reg_addr(dev, EP_DATA));
1838 				set_fifo_bytecount(&dev->ep[0], 0);
1839 				allow_status(ep);
1840 				dev_vdbg(dev->dev, "device stat %02x\n", status);
1841 				goto next_endpoints;
1842 			case USB_RECIP_INTERFACE:
1843 				if (u.r.wLength > 2)
1844 					goto do_stall;
1845 
1846 				/* don't bother with a request object! */
1847 				net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1848 				writew(status, net2272_reg_addr(dev, EP_DATA));
1849 				set_fifo_bytecount(&dev->ep[0], 0);
1850 				allow_status(ep);
1851 				dev_vdbg(dev->dev, "interface status %02x\n", status);
1852 				goto next_endpoints;
1853 			}
1854 
1855 			break;
1856 		}
1857 		case USB_REQ_CLEAR_FEATURE: {
1858 			struct net2272_ep *e;
1859 
1860 			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1861 				goto delegate;
1862 			if (u.r.wValue != USB_ENDPOINT_HALT ||
1863 			    u.r.wLength != 0)
1864 				goto do_stall;
1865 			e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1866 			if (!e)
1867 				goto do_stall;
1868 			if (e->wedged) {
1869 				dev_vdbg(dev->dev, "%s wedged, halt not cleared\n",
1870 					ep->ep.name);
1871 			} else {
1872 				dev_vdbg(dev->dev, "%s clear halt\n", ep->ep.name);
1873 				clear_halt(e);
1874 			}
1875 			allow_status(ep);
1876 			goto next_endpoints;
1877 		}
1878 		case USB_REQ_SET_FEATURE: {
1879 			struct net2272_ep *e;
1880 
1881 			if (u.r.bRequestType == USB_RECIP_DEVICE) {
1882 				if (u.r.wIndex != NORMAL_OPERATION)
1883 					net2272_set_test_mode(dev, (u.r.wIndex >> 8));
1884 				allow_status(ep);
1885 				dev_vdbg(dev->dev, "test mode: %d\n", u.r.wIndex);
1886 				goto next_endpoints;
1887 			} else if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1888 				goto delegate;
1889 			if (u.r.wValue != USB_ENDPOINT_HALT ||
1890 			    u.r.wLength != 0)
1891 				goto do_stall;
1892 			e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1893 			if (!e)
1894 				goto do_stall;
1895 			set_halt(e);
1896 			allow_status(ep);
1897 			dev_vdbg(dev->dev, "%s set halt\n", ep->ep.name);
1898 			goto next_endpoints;
1899 		}
1900 		case USB_REQ_SET_ADDRESS: {
1901 			net2272_write(dev, OURADDR, u.r.wValue & 0xff);
1902 			allow_status(ep);
1903 			break;
1904 		}
1905 		default:
1906  delegate:
1907 			dev_vdbg(dev->dev, "setup %02x.%02x v%04x i%04x "
1908 				"ep_cfg %08x\n",
1909 				u.r.bRequestType, u.r.bRequest,
1910 				u.r.wValue, u.r.wIndex,
1911 				net2272_ep_read(ep, EP_CFG));
1912 			spin_unlock(&dev->lock);
1913 			tmp = dev->driver->setup(&dev->gadget, &u.r);
1914 			spin_lock(&dev->lock);
1915 		}
1916 
1917 		/* stall ep0 on error */
1918 		if (tmp < 0) {
1919  do_stall:
1920 			dev_vdbg(dev->dev, "req %02x.%02x protocol STALL; stat %d\n",
1921 				u.r.bRequestType, u.r.bRequest, tmp);
1922 			dev->protocol_stall = 1;
1923 		}
1924 	/* endpoint dma irq? */
1925 	} else if (stat & (1 << DMA_DONE_INTERRUPT)) {
1926 		net2272_cancel_dma(dev);
1927 		net2272_write(dev, IRQSTAT0, 1 << DMA_DONE_INTERRUPT);
1928 		stat &= ~(1 << DMA_DONE_INTERRUPT);
1929 		num = (net2272_read(dev, DMAREQ) & (1 << DMA_ENDPOINT_SELECT))
1930 			? 2 : 1;
1931 
1932 		ep = &dev->ep[num];
1933 		net2272_handle_dma(ep);
1934 	}
1935 
1936  next_endpoints:
1937 	/* endpoint data irq? */
1938 	scratch = stat & 0x0f;
1939 	stat &= ~0x0f;
1940 	for (num = 0; scratch; num++) {
1941 		u8 t;
1942 
1943 		/* does this endpoint's FIFO and queue need tending? */
1944 		t = 1 << num;
1945 		if ((scratch & t) == 0)
1946 			continue;
1947 		scratch ^= t;
1948 
1949 		ep = &dev->ep[num];
1950 		net2272_handle_ep(ep);
1951 	}
1952 
1953 	/* some interrupts we can just ignore */
1954 	stat &= ~(1 << SOF_INTERRUPT);
1955 
1956 	if (stat)
1957 		dev_dbg(dev->dev, "unhandled irqstat0 %02x\n", stat);
1958 }
1959 
1960 static void
1961 net2272_handle_stat1_irqs(struct net2272 *dev, u8 stat)
1962 {
1963 	u8 tmp, mask;
1964 
1965 	/* after disconnect there's nothing else to do! */
1966 	tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
1967 	mask = (1 << USB_HIGH_SPEED) | (1 << USB_FULL_SPEED);
1968 
1969 	if (stat & tmp) {
1970 		bool	reset = false;
1971 		bool	disconnect = false;
1972 
1973 		/*
1974 		 * Ignore disconnects and resets if the speed hasn't been set.
1975 		 * VBUS can bounce and there's always an initial reset.
1976 		 */
1977 		net2272_write(dev, IRQSTAT1, tmp);
1978 		if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
1979 			if ((stat & (1 << VBUS_INTERRUPT)) &&
1980 					(net2272_read(dev, USBCTL1) &
1981 						(1 << VBUS_PIN)) == 0) {
1982 				disconnect = true;
1983 				dev_dbg(dev->dev, "disconnect %s\n",
1984 					dev->driver->driver.name);
1985 			} else if ((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
1986 					(net2272_read(dev, USBCTL1) & mask)
1987 						== 0) {
1988 				reset = true;
1989 				dev_dbg(dev->dev, "reset %s\n",
1990 					dev->driver->driver.name);
1991 			}
1992 
1993 			if (disconnect || reset) {
1994 				stop_activity(dev, dev->driver);
1995 				net2272_ep0_start(dev);
1996 				spin_unlock(&dev->lock);
1997 				if (reset)
1998 					usb_gadget_udc_reset
1999 						(&dev->gadget, dev->driver);
2000 				else
2001 					(dev->driver->disconnect)
2002 						(&dev->gadget);
2003 				spin_lock(&dev->lock);
2004 				return;
2005 			}
2006 		}
2007 		stat &= ~tmp;
2008 
2009 		if (!stat)
2010 			return;
2011 	}
2012 
2013 	tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2014 	if (stat & tmp) {
2015 		net2272_write(dev, IRQSTAT1, tmp);
2016 		if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2017 			if (dev->driver->suspend)
2018 				dev->driver->suspend(&dev->gadget);
2019 			if (!enable_suspend) {
2020 				stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2021 				dev_dbg(dev->dev, "Suspend disabled, ignoring\n");
2022 			}
2023 		} else {
2024 			if (dev->driver->resume)
2025 				dev->driver->resume(&dev->gadget);
2026 		}
2027 		stat &= ~tmp;
2028 	}
2029 
2030 	/* clear any other status/irqs */
2031 	if (stat)
2032 		net2272_write(dev, IRQSTAT1, stat);
2033 
2034 	/* some status we can just ignore */
2035 	stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2036 			| (1 << SUSPEND_REQUEST_INTERRUPT)
2037 			| (1 << RESUME_INTERRUPT));
2038 	if (!stat)
2039 		return;
2040 	else
2041 		dev_dbg(dev->dev, "unhandled irqstat1 %02x\n", stat);
2042 }
2043 
2044 static irqreturn_t net2272_irq(int irq, void *_dev)
2045 {
2046 	struct net2272 *dev = _dev;
2047 #if defined(PLX_PCI_RDK) || defined(PLX_PCI_RDK2)
2048 	u32 intcsr;
2049 #endif
2050 #if defined(PLX_PCI_RDK)
2051 	u8 dmareq;
2052 #endif
2053 	spin_lock(&dev->lock);
2054 #if defined(PLX_PCI_RDK)
2055 	intcsr = readl(dev->rdk1.plx9054_base_addr + INTCSR);
2056 
2057 	if ((intcsr & LOCAL_INTERRUPT_TEST) == LOCAL_INTERRUPT_TEST) {
2058 		writel(intcsr & ~(1 << PCI_INTERRUPT_ENABLE),
2059 				dev->rdk1.plx9054_base_addr + INTCSR);
2060 		net2272_handle_stat1_irqs(dev, net2272_read(dev, IRQSTAT1));
2061 		net2272_handle_stat0_irqs(dev, net2272_read(dev, IRQSTAT0));
2062 		intcsr = readl(dev->rdk1.plx9054_base_addr + INTCSR);
2063 		writel(intcsr | (1 << PCI_INTERRUPT_ENABLE),
2064 			dev->rdk1.plx9054_base_addr + INTCSR);
2065 	}
2066 	if ((intcsr & DMA_CHANNEL_0_TEST) == DMA_CHANNEL_0_TEST) {
2067 		writeb((1 << CHANNEL_CLEAR_INTERRUPT | (0 << CHANNEL_ENABLE)),
2068 				dev->rdk1.plx9054_base_addr + DMACSR0);
2069 
2070 		dmareq = net2272_read(dev, DMAREQ);
2071 		if (dmareq & 0x01)
2072 			net2272_handle_dma(&dev->ep[2]);
2073 		else
2074 			net2272_handle_dma(&dev->ep[1]);
2075 	}
2076 #endif
2077 #if defined(PLX_PCI_RDK2)
2078 	/* see if PCI int for us by checking irqstat */
2079 	intcsr = readl(dev->rdk2.fpga_base_addr + RDK2_IRQSTAT);
2080 	if (!(intcsr & (1 << NET2272_PCI_IRQ))) {
2081 		spin_unlock(&dev->lock);
2082 		return IRQ_NONE;
2083 	}
2084 	/* check dma interrupts */
2085 #endif
2086 	/* Platform/devcice interrupt handler */
2087 #if !defined(PLX_PCI_RDK)
2088 	net2272_handle_stat1_irqs(dev, net2272_read(dev, IRQSTAT1));
2089 	net2272_handle_stat0_irqs(dev, net2272_read(dev, IRQSTAT0));
2090 #endif
2091 	spin_unlock(&dev->lock);
2092 
2093 	return IRQ_HANDLED;
2094 }
2095 
2096 static int net2272_present(struct net2272 *dev)
2097 {
2098 	/*
2099 	 * Quick test to see if CPU can communicate properly with the NET2272.
2100 	 * Verifies connection using writes and reads to write/read and
2101 	 * read-only registers.
2102 	 *
2103 	 * This routine is strongly recommended especially during early bring-up
2104 	 * of new hardware, however for designs that do not apply Power On System
2105 	 * Tests (POST) it may discarded (or perhaps minimized).
2106 	 */
2107 	unsigned int ii;
2108 	u8 val, refval;
2109 
2110 	/* Verify NET2272 write/read SCRATCH register can write and read */
2111 	refval = net2272_read(dev, SCRATCH);
2112 	for (ii = 0; ii < 0x100; ii += 7) {
2113 		net2272_write(dev, SCRATCH, ii);
2114 		val = net2272_read(dev, SCRATCH);
2115 		if (val != ii) {
2116 			dev_dbg(dev->dev,
2117 				"%s: write/read SCRATCH register test failed: "
2118 				"wrote:0x%2.2x, read:0x%2.2x\n",
2119 				__func__, ii, val);
2120 			return -EINVAL;
2121 		}
2122 	}
2123 	/* To be nice, we write the original SCRATCH value back: */
2124 	net2272_write(dev, SCRATCH, refval);
2125 
2126 	/* Verify NET2272 CHIPREV register is read-only: */
2127 	refval = net2272_read(dev, CHIPREV_2272);
2128 	for (ii = 0; ii < 0x100; ii += 7) {
2129 		net2272_write(dev, CHIPREV_2272, ii);
2130 		val = net2272_read(dev, CHIPREV_2272);
2131 		if (val != refval) {
2132 			dev_dbg(dev->dev,
2133 				"%s: write/read CHIPREV register test failed: "
2134 				"wrote 0x%2.2x, read:0x%2.2x expected:0x%2.2x\n",
2135 				__func__, ii, val, refval);
2136 			return -EINVAL;
2137 		}
2138 	}
2139 
2140 	/*
2141 	 * Verify NET2272's "NET2270 legacy revision" register
2142 	 *  - NET2272 has two revision registers. The NET2270 legacy revision
2143 	 *    register should read the same value, regardless of the NET2272
2144 	 *    silicon revision.  The legacy register applies to NET2270
2145 	 *    firmware being applied to the NET2272.
2146 	 */
2147 	val = net2272_read(dev, CHIPREV_LEGACY);
2148 	if (val != NET2270_LEGACY_REV) {
2149 		/*
2150 		 * Unexpected legacy revision value
2151 		 * - Perhaps the chip is a NET2270?
2152 		 */
2153 		dev_dbg(dev->dev,
2154 			"%s: WARNING: UNEXPECTED NET2272 LEGACY REGISTER VALUE:\n"
2155 			" - CHIPREV_LEGACY: expected 0x%2.2x, got:0x%2.2x. (Not NET2272?)\n",
2156 			__func__, NET2270_LEGACY_REV, val);
2157 		return -EINVAL;
2158 	}
2159 
2160 	/*
2161 	 * Verify NET2272 silicon revision
2162 	 *  - This revision register is appropriate for the silicon version
2163 	 *    of the NET2272
2164 	 */
2165 	val = net2272_read(dev, CHIPREV_2272);
2166 	switch (val) {
2167 	case CHIPREV_NET2272_R1:
2168 		/*
2169 		 * NET2272 Rev 1 has DMA related errata:
2170 		 *  - Newer silicon (Rev 1A or better) required
2171 		 */
2172 		dev_dbg(dev->dev,
2173 			"%s: Rev 1 detected: newer silicon recommended for DMA support\n",
2174 			__func__);
2175 		break;
2176 	case CHIPREV_NET2272_R1A:
2177 		break;
2178 	default:
2179 		/* NET2272 silicon version *may* not work with this firmware */
2180 		dev_dbg(dev->dev,
2181 			"%s: unexpected silicon revision register value: "
2182 			" CHIPREV_2272: 0x%2.2x\n",
2183 			__func__, val);
2184 		/*
2185 		 * Return Success, even though the chip rev is not an expected value
2186 		 *  - Older, pre-built firmware can attempt to operate on newer silicon
2187 		 *  - Often, new silicon is perfectly compatible
2188 		 */
2189 	}
2190 
2191 	/* Success: NET2272 checks out OK */
2192 	return 0;
2193 }
2194 
2195 static void
2196 net2272_gadget_release(struct device *_dev)
2197 {
2198 	struct net2272 *dev = container_of(_dev, struct net2272, gadget.dev);
2199 
2200 	kfree(dev);
2201 }
2202 
2203 /*---------------------------------------------------------------------------*/
2204 
2205 static void
2206 net2272_remove(struct net2272 *dev)
2207 {
2208 	if (dev->added)
2209 		usb_del_gadget(&dev->gadget);
2210 	free_irq(dev->irq, dev);
2211 	iounmap(dev->base_addr);
2212 	device_remove_file(dev->dev, &dev_attr_registers);
2213 
2214 	dev_info(dev->dev, "unbind\n");
2215 }
2216 
2217 static struct net2272 *net2272_probe_init(struct device *dev, unsigned int irq)
2218 {
2219 	struct net2272 *ret;
2220 
2221 	if (!irq) {
2222 		dev_dbg(dev, "No IRQ!\n");
2223 		return ERR_PTR(-ENODEV);
2224 	}
2225 
2226 	/* alloc, and start init */
2227 	ret = kzalloc(sizeof(*ret), GFP_KERNEL);
2228 	if (!ret)
2229 		return ERR_PTR(-ENOMEM);
2230 
2231 	spin_lock_init(&ret->lock);
2232 	ret->irq = irq;
2233 	ret->dev = dev;
2234 	ret->gadget.ops = &net2272_ops;
2235 	ret->gadget.max_speed = USB_SPEED_HIGH;
2236 
2237 	/* the "gadget" abstracts/virtualizes the controller */
2238 	ret->gadget.name = driver_name;
2239 	usb_initialize_gadget(dev, &ret->gadget, net2272_gadget_release);
2240 
2241 	return ret;
2242 }
2243 
2244 static int
2245 net2272_probe_fin(struct net2272 *dev, unsigned int irqflags)
2246 {
2247 	int ret;
2248 
2249 	/* See if there... */
2250 	if (net2272_present(dev)) {
2251 		dev_warn(dev->dev, "2272 not found!\n");
2252 		ret = -ENODEV;
2253 		goto err;
2254 	}
2255 
2256 	net2272_usb_reset(dev);
2257 	net2272_usb_reinit(dev);
2258 
2259 	ret = request_irq(dev->irq, net2272_irq, irqflags, driver_name, dev);
2260 	if (ret) {
2261 		dev_err(dev->dev, "request interrupt %i failed\n", dev->irq);
2262 		goto err;
2263 	}
2264 
2265 	dev->chiprev = net2272_read(dev, CHIPREV_2272);
2266 
2267 	/* done */
2268 	dev_info(dev->dev, "%s\n", driver_desc);
2269 	dev_info(dev->dev, "irq %i, mem %p, chip rev %04x, dma %s\n",
2270 		dev->irq, dev->base_addr, dev->chiprev,
2271 		dma_mode_string());
2272 	dev_info(dev->dev, "version: %s\n", driver_vers);
2273 
2274 	ret = device_create_file(dev->dev, &dev_attr_registers);
2275 	if (ret)
2276 		goto err_irq;
2277 
2278 	ret = usb_add_gadget(&dev->gadget);
2279 	if (ret)
2280 		goto err_add_udc;
2281 	dev->added = 1;
2282 
2283 	return 0;
2284 
2285 err_add_udc:
2286 	device_remove_file(dev->dev, &dev_attr_registers);
2287  err_irq:
2288 	free_irq(dev->irq, dev);
2289  err:
2290 	return ret;
2291 }
2292 
2293 #ifdef CONFIG_USB_PCI
2294 
2295 /*
2296  * wrap this driver around the specified device, but
2297  * don't respond over USB until a gadget driver binds to us
2298  */
2299 
2300 static int
2301 net2272_rdk1_probe(struct pci_dev *pdev, struct net2272 *dev)
2302 {
2303 	unsigned long resource, len, tmp;
2304 	void __iomem *mem_mapped_addr[4];
2305 	int ret, i;
2306 
2307 	/*
2308 	 * BAR 0 holds PLX 9054 config registers
2309 	 * BAR 1 is i/o memory; unused here
2310 	 * BAR 2 holds EPLD config registers
2311 	 * BAR 3 holds NET2272 registers
2312 	 */
2313 
2314 	/* Find and map all address spaces */
2315 	for (i = 0; i < 4; ++i) {
2316 		if (i == 1)
2317 			continue;	/* BAR1 unused */
2318 
2319 		resource = pci_resource_start(pdev, i);
2320 		len = pci_resource_len(pdev, i);
2321 
2322 		if (!request_mem_region(resource, len, driver_name)) {
2323 			dev_dbg(dev->dev, "controller already in use\n");
2324 			ret = -EBUSY;
2325 			goto err;
2326 		}
2327 
2328 		mem_mapped_addr[i] = ioremap(resource, len);
2329 		if (mem_mapped_addr[i] == NULL) {
2330 			release_mem_region(resource, len);
2331 			dev_dbg(dev->dev, "can't map memory\n");
2332 			ret = -EFAULT;
2333 			goto err;
2334 		}
2335 	}
2336 
2337 	dev->rdk1.plx9054_base_addr = mem_mapped_addr[0];
2338 	dev->rdk1.epld_base_addr = mem_mapped_addr[2];
2339 	dev->base_addr = mem_mapped_addr[3];
2340 
2341 	/* Set PLX 9054 bus width (16 bits) */
2342 	tmp = readl(dev->rdk1.plx9054_base_addr + LBRD1);
2343 	writel((tmp & ~(3 << MEMORY_SPACE_LOCAL_BUS_WIDTH)) | W16_BIT,
2344 			dev->rdk1.plx9054_base_addr + LBRD1);
2345 
2346 	/* Enable PLX 9054 Interrupts */
2347 	writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) |
2348 			(1 << PCI_INTERRUPT_ENABLE) |
2349 			(1 << LOCAL_INTERRUPT_INPUT_ENABLE),
2350 			dev->rdk1.plx9054_base_addr + INTCSR);
2351 
2352 	writeb((1 << CHANNEL_CLEAR_INTERRUPT | (0 << CHANNEL_ENABLE)),
2353 			dev->rdk1.plx9054_base_addr + DMACSR0);
2354 
2355 	/* reset */
2356 	writeb((1 << EPLD_DMA_ENABLE) |
2357 		(1 << DMA_CTL_DACK) |
2358 		(1 << DMA_TIMEOUT_ENABLE) |
2359 		(1 << USER) |
2360 		(0 << MPX_MODE) |
2361 		(1 << BUSWIDTH) |
2362 		(1 << NET2272_RESET),
2363 		dev->base_addr + EPLD_IO_CONTROL_REGISTER);
2364 
2365 	mb();
2366 	writeb(readb(dev->base_addr + EPLD_IO_CONTROL_REGISTER) &
2367 		~(1 << NET2272_RESET),
2368 		dev->base_addr + EPLD_IO_CONTROL_REGISTER);
2369 	udelay(200);
2370 
2371 	return 0;
2372 
2373  err:
2374 	while (--i >= 0) {
2375 		if (i == 1)
2376 			continue;	/* BAR1 unused */
2377 		iounmap(mem_mapped_addr[i]);
2378 		release_mem_region(pci_resource_start(pdev, i),
2379 			pci_resource_len(pdev, i));
2380 	}
2381 
2382 	return ret;
2383 }
2384 
2385 static int
2386 net2272_rdk2_probe(struct pci_dev *pdev, struct net2272 *dev)
2387 {
2388 	unsigned long resource, len;
2389 	void __iomem *mem_mapped_addr[2];
2390 	int ret, i;
2391 
2392 	/*
2393 	 * BAR 0 holds FGPA config registers
2394 	 * BAR 1 holds NET2272 registers
2395 	 */
2396 
2397 	/* Find and map all address spaces, bar2-3 unused in rdk 2 */
2398 	for (i = 0; i < 2; ++i) {
2399 		resource = pci_resource_start(pdev, i);
2400 		len = pci_resource_len(pdev, i);
2401 
2402 		if (!request_mem_region(resource, len, driver_name)) {
2403 			dev_dbg(dev->dev, "controller already in use\n");
2404 			ret = -EBUSY;
2405 			goto err;
2406 		}
2407 
2408 		mem_mapped_addr[i] = ioremap(resource, len);
2409 		if (mem_mapped_addr[i] == NULL) {
2410 			release_mem_region(resource, len);
2411 			dev_dbg(dev->dev, "can't map memory\n");
2412 			ret = -EFAULT;
2413 			goto err;
2414 		}
2415 	}
2416 
2417 	dev->rdk2.fpga_base_addr = mem_mapped_addr[0];
2418 	dev->base_addr = mem_mapped_addr[1];
2419 
2420 	mb();
2421 	/* Set 2272 bus width (16 bits) and reset */
2422 	writel((1 << CHIP_RESET), dev->rdk2.fpga_base_addr + RDK2_LOCCTLRDK);
2423 	udelay(200);
2424 	writel((1 << BUS_WIDTH), dev->rdk2.fpga_base_addr + RDK2_LOCCTLRDK);
2425 	/* Print fpga version number */
2426 	dev_info(dev->dev, "RDK2 FPGA version %08x\n",
2427 		readl(dev->rdk2.fpga_base_addr + RDK2_FPGAREV));
2428 	/* Enable FPGA Interrupts */
2429 	writel((1 << NET2272_PCI_IRQ), dev->rdk2.fpga_base_addr + RDK2_IRQENB);
2430 
2431 	return 0;
2432 
2433  err:
2434 	while (--i >= 0) {
2435 		iounmap(mem_mapped_addr[i]);
2436 		release_mem_region(pci_resource_start(pdev, i),
2437 			pci_resource_len(pdev, i));
2438 	}
2439 
2440 	return ret;
2441 }
2442 
2443 static int
2444 net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2445 {
2446 	struct net2272 *dev;
2447 	int ret;
2448 
2449 	dev = net2272_probe_init(&pdev->dev, pdev->irq);
2450 	if (IS_ERR(dev))
2451 		return PTR_ERR(dev);
2452 	dev->dev_id = pdev->device;
2453 
2454 	if (pci_enable_device(pdev) < 0) {
2455 		ret = -ENODEV;
2456 		goto err_put;
2457 	}
2458 
2459 	pci_set_master(pdev);
2460 
2461 	switch (pdev->device) {
2462 	case PCI_DEVICE_ID_RDK1: ret = net2272_rdk1_probe(pdev, dev); break;
2463 	case PCI_DEVICE_ID_RDK2: ret = net2272_rdk2_probe(pdev, dev); break;
2464 	default: BUG();
2465 	}
2466 	if (ret)
2467 		goto err_pci;
2468 
2469 	ret = net2272_probe_fin(dev, 0);
2470 	if (ret)
2471 		goto err_pci;
2472 
2473 	pci_set_drvdata(pdev, dev);
2474 
2475 	return 0;
2476 
2477  err_pci:
2478 	pci_disable_device(pdev);
2479  err_put:
2480 	usb_put_gadget(&dev->gadget);
2481 
2482 	return ret;
2483 }
2484 
2485 static void
2486 net2272_rdk1_remove(struct pci_dev *pdev, struct net2272 *dev)
2487 {
2488 	int i;
2489 
2490 	/* disable PLX 9054 interrupts */
2491 	writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2492 		~(1 << PCI_INTERRUPT_ENABLE),
2493 		dev->rdk1.plx9054_base_addr + INTCSR);
2494 
2495 	/* clean up resources allocated during probe() */
2496 	iounmap(dev->rdk1.plx9054_base_addr);
2497 	iounmap(dev->rdk1.epld_base_addr);
2498 
2499 	for (i = 0; i < 4; ++i) {
2500 		if (i == 1)
2501 			continue;	/* BAR1 unused */
2502 		release_mem_region(pci_resource_start(pdev, i),
2503 			pci_resource_len(pdev, i));
2504 	}
2505 }
2506 
2507 static void
2508 net2272_rdk2_remove(struct pci_dev *pdev, struct net2272 *dev)
2509 {
2510 	int i;
2511 
2512 	/* disable fpga interrupts
2513 	writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2514 			~(1 << PCI_INTERRUPT_ENABLE),
2515 			dev->rdk1.plx9054_base_addr + INTCSR);
2516 	*/
2517 
2518 	/* clean up resources allocated during probe() */
2519 	iounmap(dev->rdk2.fpga_base_addr);
2520 
2521 	for (i = 0; i < 2; ++i)
2522 		release_mem_region(pci_resource_start(pdev, i),
2523 			pci_resource_len(pdev, i));
2524 }
2525 
2526 static void
2527 net2272_pci_remove(struct pci_dev *pdev)
2528 {
2529 	struct net2272 *dev = pci_get_drvdata(pdev);
2530 
2531 	net2272_remove(dev);
2532 
2533 	switch (pdev->device) {
2534 	case PCI_DEVICE_ID_RDK1: net2272_rdk1_remove(pdev, dev); break;
2535 	case PCI_DEVICE_ID_RDK2: net2272_rdk2_remove(pdev, dev); break;
2536 	default: BUG();
2537 	}
2538 
2539 	pci_disable_device(pdev);
2540 
2541 	usb_put_gadget(&dev->gadget);
2542 }
2543 
2544 /* Table of matching PCI IDs */
2545 static struct pci_device_id pci_ids[] = {
2546 	{	/* RDK 1 card */
2547 		.class       = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2548 		.class_mask  = 0,
2549 		.vendor      = PCI_VENDOR_ID_PLX,
2550 		.device      = PCI_DEVICE_ID_RDK1,
2551 		.subvendor   = PCI_ANY_ID,
2552 		.subdevice   = PCI_ANY_ID,
2553 	},
2554 	{	/* RDK 2 card */
2555 		.class       = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2556 		.class_mask  = 0,
2557 		.vendor      = PCI_VENDOR_ID_PLX,
2558 		.device      = PCI_DEVICE_ID_RDK2,
2559 		.subvendor   = PCI_ANY_ID,
2560 		.subdevice   = PCI_ANY_ID,
2561 	},
2562 	{ }
2563 };
2564 MODULE_DEVICE_TABLE(pci, pci_ids);
2565 
2566 static struct pci_driver net2272_pci_driver = {
2567 	.name     = driver_name,
2568 	.id_table = pci_ids,
2569 
2570 	.probe    = net2272_pci_probe,
2571 	.remove   = net2272_pci_remove,
2572 };
2573 
2574 static int net2272_pci_register(void)
2575 {
2576 	return pci_register_driver(&net2272_pci_driver);
2577 }
2578 
2579 static void net2272_pci_unregister(void)
2580 {
2581 	pci_unregister_driver(&net2272_pci_driver);
2582 }
2583 
2584 #else
2585 static inline int net2272_pci_register(void) { return 0; }
2586 static inline void net2272_pci_unregister(void) { }
2587 #endif
2588 
2589 /*---------------------------------------------------------------------------*/
2590 
2591 static int
2592 net2272_plat_probe(struct platform_device *pdev)
2593 {
2594 	struct net2272 *dev;
2595 	int ret;
2596 	unsigned int irqflags;
2597 	resource_size_t base, len;
2598 	struct resource *iomem, *iomem_bus, *irq_res;
2599 
2600 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2601 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2602 	iomem_bus = platform_get_resource(pdev, IORESOURCE_BUS, 0);
2603 	if (!irq_res || !iomem) {
2604 		dev_err(&pdev->dev, "must provide irq/base addr");
2605 		return -EINVAL;
2606 	}
2607 
2608 	dev = net2272_probe_init(&pdev->dev, irq_res->start);
2609 	if (IS_ERR(dev))
2610 		return PTR_ERR(dev);
2611 
2612 	irqflags = 0;
2613 	if (irq_res->flags & IORESOURCE_IRQ_HIGHEDGE)
2614 		irqflags |= IRQF_TRIGGER_RISING;
2615 	if (irq_res->flags & IORESOURCE_IRQ_LOWEDGE)
2616 		irqflags |= IRQF_TRIGGER_FALLING;
2617 	if (irq_res->flags & IORESOURCE_IRQ_HIGHLEVEL)
2618 		irqflags |= IRQF_TRIGGER_HIGH;
2619 	if (irq_res->flags & IORESOURCE_IRQ_LOWLEVEL)
2620 		irqflags |= IRQF_TRIGGER_LOW;
2621 
2622 	base = iomem->start;
2623 	len = resource_size(iomem);
2624 	if (iomem_bus)
2625 		dev->base_shift = iomem_bus->start;
2626 
2627 	if (!request_mem_region(base, len, driver_name)) {
2628 		dev_dbg(dev->dev, "get request memory region!\n");
2629 		ret = -EBUSY;
2630 		goto err;
2631 	}
2632 	dev->base_addr = ioremap(base, len);
2633 	if (!dev->base_addr) {
2634 		dev_dbg(dev->dev, "can't map memory\n");
2635 		ret = -EFAULT;
2636 		goto err_req;
2637 	}
2638 
2639 	ret = net2272_probe_fin(dev, IRQF_TRIGGER_LOW);
2640 	if (ret)
2641 		goto err_io;
2642 
2643 	platform_set_drvdata(pdev, dev);
2644 	dev_info(&pdev->dev, "running in 16-bit, %sbyte swap local bus mode\n",
2645 		(net2272_read(dev, LOCCTL) & (1 << BYTE_SWAP)) ? "" : "no ");
2646 
2647 	return 0;
2648 
2649  err_io:
2650 	iounmap(dev->base_addr);
2651  err_req:
2652 	release_mem_region(base, len);
2653  err:
2654 	usb_put_gadget(&dev->gadget);
2655 
2656 	return ret;
2657 }
2658 
2659 static int
2660 net2272_plat_remove(struct platform_device *pdev)
2661 {
2662 	struct net2272 *dev = platform_get_drvdata(pdev);
2663 
2664 	net2272_remove(dev);
2665 
2666 	release_mem_region(pdev->resource[0].start,
2667 		resource_size(&pdev->resource[0]));
2668 
2669 	usb_put_gadget(&dev->gadget);
2670 
2671 	return 0;
2672 }
2673 
2674 static struct platform_driver net2272_plat_driver = {
2675 	.probe   = net2272_plat_probe,
2676 	.remove  = net2272_plat_remove,
2677 	.driver  = {
2678 		.name  = driver_name,
2679 	},
2680 	/* FIXME .suspend, .resume */
2681 };
2682 MODULE_ALIAS("platform:net2272");
2683 
2684 static int __init net2272_init(void)
2685 {
2686 	int ret;
2687 
2688 	ret = net2272_pci_register();
2689 	if (ret)
2690 		return ret;
2691 	ret = platform_driver_register(&net2272_plat_driver);
2692 	if (ret)
2693 		goto err_pci;
2694 	return ret;
2695 
2696 err_pci:
2697 	net2272_pci_unregister();
2698 	return ret;
2699 }
2700 module_init(net2272_init);
2701 
2702 static void __exit net2272_cleanup(void)
2703 {
2704 	net2272_pci_unregister();
2705 	platform_driver_unregister(&net2272_plat_driver);
2706 }
2707 module_exit(net2272_cleanup);
2708 
2709 MODULE_DESCRIPTION(DRIVER_DESC);
2710 MODULE_AUTHOR("PLX Technology, Inc.");
2711 MODULE_LICENSE("GPL");
2712