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