usb: gadget: rename usb_gadget_driver::speed to max_speed
[linux/fpc-iii.git] / drivers / usb / gadget / net2272.c
blob4c81d540bc2696529a2c4a090ab0ed86037fbecd
1 /*
2 * Driver for PLX NET2272 USB device controller
4 * Copyright (C) 2005-2006 PLX Technology, Inc.
5 * Copyright (C) 2006-2011 Analog Devices, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/gpio.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/io.h>
29 #include <linux/ioport.h>
30 #include <linux/kernel.h>
31 #include <linux/list.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/pci.h>
35 #include <linux/platform_device.h>
36 #include <linux/prefetch.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/timer.h>
40 #include <linux/usb.h>
41 #include <linux/usb/ch9.h>
42 #include <linux/usb/gadget.h>
44 #include <asm/byteorder.h>
45 #include <asm/system.h>
46 #include <asm/unaligned.h>
48 #include "net2272.h"
50 #define DRIVER_DESC "PLX NET2272 USB Peripheral Controller"
52 static const char driver_name[] = "net2272";
53 static const char driver_vers[] = "2006 October 17/mainline";
54 static const char driver_desc[] = DRIVER_DESC;
56 static const char ep0name[] = "ep0";
57 static const char * const ep_name[] = {
58 ep0name,
59 "ep-a", "ep-b", "ep-c",
62 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
63 #ifdef CONFIG_USB_GADGET_NET2272_DMA
65 * use_dma: the NET2272 can use an external DMA controller.
66 * Note that since there is no generic DMA api, some functions,
67 * notably request_dma, start_dma, and cancel_dma will need to be
68 * modified for your platform's particular dma controller.
70 * If use_dma is disabled, pio will be used instead.
72 static int use_dma = 0;
73 module_param(use_dma, bool, 0644);
76 * dma_ep: selects the endpoint for use with dma (1=ep-a, 2=ep-b)
77 * The NET2272 can only use dma for a single endpoint at a time.
78 * At some point this could be modified to allow either endpoint
79 * to take control of dma as it becomes available.
81 * Note that DMA should not be used on OUT endpoints unless it can
82 * be guaranteed that no short packets will arrive on an IN endpoint
83 * while the DMA operation is pending. Otherwise the OUT DMA will
84 * terminate prematurely (See NET2272 Errata 630-0213-0101)
86 static ushort dma_ep = 1;
87 module_param(dma_ep, ushort, 0644);
90 * dma_mode: net2272 dma mode setting (see LOCCTL1 definiton):
91 * mode 0 == Slow DREQ mode
92 * mode 1 == Fast DREQ mode
93 * mode 2 == Burst mode
95 static ushort dma_mode = 2;
96 module_param(dma_mode, ushort, 0644);
97 #else
98 #define use_dma 0
99 #define dma_ep 1
100 #define dma_mode 2
101 #endif
104 * fifo_mode: net2272 buffer configuration:
105 * mode 0 == ep-{a,b,c} 512db each
106 * mode 1 == ep-a 1k, ep-{b,c} 512db
107 * mode 2 == ep-a 1k, ep-b 1k, ep-c 512db
108 * mode 3 == ep-a 1k, ep-b disabled, ep-c 512db
110 static ushort fifo_mode = 0;
111 module_param(fifo_mode, ushort, 0644);
114 * enable_suspend: When enabled, the driver will respond to
115 * USB suspend requests by powering down the NET2272. Otherwise,
116 * USB suspend requests will be ignored. This is acceptible for
117 * self-powered devices. For bus powered devices set this to 1.
119 static ushort enable_suspend = 0;
120 module_param(enable_suspend, ushort, 0644);
122 static void assert_out_naking(struct net2272_ep *ep, const char *where)
124 u8 tmp;
126 #ifndef DEBUG
127 return;
128 #endif
130 tmp = net2272_ep_read(ep, EP_STAT0);
131 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
132 dev_dbg(ep->dev->dev, "%s %s %02x !NAK\n",
133 ep->ep.name, where, tmp);
134 net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
137 #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep, __func__)
139 static void stop_out_naking(struct net2272_ep *ep)
141 u8 tmp = net2272_ep_read(ep, EP_STAT0);
143 if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
144 net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
147 #define PIPEDIR(bAddress) (usb_pipein(bAddress) ? "in" : "out")
149 static char *type_string(u8 bmAttributes)
151 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
152 case USB_ENDPOINT_XFER_BULK: return "bulk";
153 case USB_ENDPOINT_XFER_ISOC: return "iso";
154 case USB_ENDPOINT_XFER_INT: return "intr";
155 default: return "control";
159 static char *buf_state_string(unsigned state)
161 switch (state) {
162 case BUFF_FREE: return "free";
163 case BUFF_VALID: return "valid";
164 case BUFF_LCL: return "local";
165 case BUFF_USB: return "usb";
166 default: return "unknown";
170 static char *dma_mode_string(void)
172 if (!use_dma)
173 return "PIO";
174 switch (dma_mode) {
175 case 0: return "SLOW DREQ";
176 case 1: return "FAST DREQ";
177 case 2: return "BURST";
178 default: return "invalid";
182 static void net2272_dequeue_all(struct net2272_ep *);
183 static int net2272_kick_dma(struct net2272_ep *, struct net2272_request *);
184 static int net2272_fifo_status(struct usb_ep *);
186 static struct usb_ep_ops net2272_ep_ops;
188 /*---------------------------------------------------------------------------*/
190 static int
191 net2272_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
193 struct net2272 *dev;
194 struct net2272_ep *ep;
195 u32 max;
196 u8 tmp;
197 unsigned long flags;
199 ep = container_of(_ep, struct net2272_ep, ep);
200 if (!_ep || !desc || ep->desc || _ep->name == ep0name
201 || desc->bDescriptorType != USB_DT_ENDPOINT)
202 return -EINVAL;
203 dev = ep->dev;
204 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
205 return -ESHUTDOWN;
207 max = usb_endpoint_maxp(desc) & 0x1fff;
209 spin_lock_irqsave(&dev->lock, flags);
210 _ep->maxpacket = max & 0x7fff;
211 ep->desc = desc;
213 /* net2272_ep_reset() has already been called */
214 ep->stopped = 0;
215 ep->wedged = 0;
217 /* set speed-dependent max packet */
218 net2272_ep_write(ep, EP_MAXPKT0, max & 0xff);
219 net2272_ep_write(ep, EP_MAXPKT1, (max & 0xff00) >> 8);
221 /* set type, direction, address; reset fifo counters */
222 net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
223 tmp = usb_endpoint_type(desc);
224 if (usb_endpoint_xfer_bulk(desc)) {
225 /* catch some particularly blatant driver bugs */
226 if ((dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
227 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
228 spin_unlock_irqrestore(&dev->lock, flags);
229 return -ERANGE;
232 ep->is_iso = usb_endpoint_xfer_isoc(desc) ? 1 : 0;
233 tmp <<= ENDPOINT_TYPE;
234 tmp |= ((desc->bEndpointAddress & 0x0f) << ENDPOINT_NUMBER);
235 tmp |= usb_endpoint_dir_in(desc) << ENDPOINT_DIRECTION;
236 tmp |= (1 << ENDPOINT_ENABLE);
238 /* for OUT transfers, block the rx fifo until a read is posted */
239 ep->is_in = usb_endpoint_dir_in(desc);
240 if (!ep->is_in)
241 net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
243 net2272_ep_write(ep, EP_CFG, tmp);
245 /* enable irqs */
246 tmp = (1 << ep->num) | net2272_read(dev, IRQENB0);
247 net2272_write(dev, IRQENB0, tmp);
249 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
250 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
251 | net2272_ep_read(ep, EP_IRQENB);
252 net2272_ep_write(ep, EP_IRQENB, tmp);
254 tmp = desc->bEndpointAddress;
255 dev_dbg(dev->dev, "enabled %s (ep%d%s-%s) max %04x cfg %02x\n",
256 _ep->name, tmp & 0x0f, PIPEDIR(tmp),
257 type_string(desc->bmAttributes), max,
258 net2272_ep_read(ep, EP_CFG));
260 spin_unlock_irqrestore(&dev->lock, flags);
261 return 0;
264 static void net2272_ep_reset(struct net2272_ep *ep)
266 u8 tmp;
268 ep->desc = NULL;
269 INIT_LIST_HEAD(&ep->queue);
271 ep->ep.maxpacket = ~0;
272 ep->ep.ops = &net2272_ep_ops;
274 /* disable irqs, endpoint */
275 net2272_ep_write(ep, EP_IRQENB, 0);
277 /* init to our chosen defaults, notably so that we NAK OUT
278 * packets until the driver queues a read.
280 tmp = (1 << NAK_OUT_PACKETS_MODE) | (1 << ALT_NAK_OUT_PACKETS);
281 net2272_ep_write(ep, EP_RSPSET, tmp);
283 tmp = (1 << INTERRUPT_MODE) | (1 << HIDE_STATUS_PHASE);
284 if (ep->num != 0)
285 tmp |= (1 << ENDPOINT_TOGGLE) | (1 << ENDPOINT_HALT);
287 net2272_ep_write(ep, EP_RSPCLR, tmp);
289 /* scrub most status bits, and flush any fifo state */
290 net2272_ep_write(ep, EP_STAT0,
291 (1 << DATA_IN_TOKEN_INTERRUPT)
292 | (1 << DATA_OUT_TOKEN_INTERRUPT)
293 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
294 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
295 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT));
297 net2272_ep_write(ep, EP_STAT1,
298 (1 << TIMEOUT)
299 | (1 << USB_OUT_ACK_SENT)
300 | (1 << USB_OUT_NAK_SENT)
301 | (1 << USB_IN_ACK_RCVD)
302 | (1 << USB_IN_NAK_SENT)
303 | (1 << USB_STALL_SENT)
304 | (1 << LOCAL_OUT_ZLP)
305 | (1 << BUFFER_FLUSH));
307 /* fifo size is handled seperately */
310 static int net2272_disable(struct usb_ep *_ep)
312 struct net2272_ep *ep;
313 unsigned long flags;
315 ep = container_of(_ep, struct net2272_ep, ep);
316 if (!_ep || !ep->desc || _ep->name == ep0name)
317 return -EINVAL;
319 spin_lock_irqsave(&ep->dev->lock, flags);
320 net2272_dequeue_all(ep);
321 net2272_ep_reset(ep);
323 dev_vdbg(ep->dev->dev, "disabled %s\n", _ep->name);
325 spin_unlock_irqrestore(&ep->dev->lock, flags);
326 return 0;
329 /*---------------------------------------------------------------------------*/
331 static struct usb_request *
332 net2272_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
334 struct net2272_ep *ep;
335 struct net2272_request *req;
337 if (!_ep)
338 return NULL;
339 ep = container_of(_ep, struct net2272_ep, ep);
341 req = kzalloc(sizeof(*req), gfp_flags);
342 if (!req)
343 return NULL;
345 req->req.dma = DMA_ADDR_INVALID;
346 INIT_LIST_HEAD(&req->queue);
348 return &req->req;
351 static void
352 net2272_free_request(struct usb_ep *_ep, struct usb_request *_req)
354 struct net2272_ep *ep;
355 struct net2272_request *req;
357 ep = container_of(_ep, struct net2272_ep, ep);
358 if (!_ep || !_req)
359 return;
361 req = container_of(_req, struct net2272_request, req);
362 WARN_ON(!list_empty(&req->queue));
363 kfree(req);
366 static void
367 net2272_done(struct net2272_ep *ep, struct net2272_request *req, int status)
369 struct net2272 *dev;
370 unsigned stopped = ep->stopped;
372 if (ep->num == 0) {
373 if (ep->dev->protocol_stall) {
374 ep->stopped = 1;
375 set_halt(ep);
377 allow_status(ep);
380 list_del_init(&req->queue);
382 if (req->req.status == -EINPROGRESS)
383 req->req.status = status;
384 else
385 status = req->req.status;
387 dev = ep->dev;
388 if (use_dma && req->mapped) {
389 dma_unmap_single(dev->dev, req->req.dma, req->req.length,
390 ep->is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
391 req->req.dma = DMA_ADDR_INVALID;
392 req->mapped = 0;
395 if (status && status != -ESHUTDOWN)
396 dev_vdbg(dev->dev, "complete %s req %p stat %d len %u/%u buf %p\n",
397 ep->ep.name, &req->req, status,
398 req->req.actual, req->req.length, req->req.buf);
400 /* don't modify queue heads during completion callback */
401 ep->stopped = 1;
402 spin_unlock(&dev->lock);
403 req->req.complete(&ep->ep, &req->req);
404 spin_lock(&dev->lock);
405 ep->stopped = stopped;
408 static int
409 net2272_write_packet(struct net2272_ep *ep, u8 *buf,
410 struct net2272_request *req, unsigned max)
412 u16 __iomem *ep_data = net2272_reg_addr(ep->dev, EP_DATA);
413 u16 *bufp;
414 unsigned length, count;
415 u8 tmp;
417 length = min(req->req.length - req->req.actual, max);
418 req->req.actual += length;
420 dev_vdbg(ep->dev->dev, "write packet %s req %p max %u len %u avail %u\n",
421 ep->ep.name, req, max, length,
422 (net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0));
424 count = length;
425 bufp = (u16 *)buf;
427 while (likely(count >= 2)) {
428 /* no byte-swap required; chip endian set during init */
429 writew(*bufp++, ep_data);
430 count -= 2;
432 buf = (u8 *)bufp;
434 /* write final byte by placing the NET2272 into 8-bit mode */
435 if (unlikely(count)) {
436 tmp = net2272_read(ep->dev, LOCCTL);
437 net2272_write(ep->dev, LOCCTL, tmp & ~(1 << DATA_WIDTH));
438 writeb(*buf, ep_data);
439 net2272_write(ep->dev, LOCCTL, tmp);
441 return length;
444 /* returns: 0: still running, 1: completed, negative: errno */
445 static int
446 net2272_write_fifo(struct net2272_ep *ep, struct net2272_request *req)
448 u8 *buf;
449 unsigned count, max;
450 int status;
452 dev_vdbg(ep->dev->dev, "write_fifo %s actual %d len %d\n",
453 ep->ep.name, req->req.actual, req->req.length);
456 * Keep loading the endpoint until the final packet is loaded,
457 * or the endpoint buffer is full.
459 top:
461 * Clear interrupt status
462 * - Packet Transmitted interrupt will become set again when the
463 * host successfully takes another packet
465 net2272_ep_write(ep, EP_STAT0, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT));
466 while (!(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_FULL))) {
467 buf = req->req.buf + req->req.actual;
468 prefetch(buf);
470 /* force pagesel */
471 net2272_ep_read(ep, EP_STAT0);
473 max = (net2272_ep_read(ep, EP_AVAIL1) << 8) |
474 (net2272_ep_read(ep, EP_AVAIL0));
476 if (max < ep->ep.maxpacket)
477 max = (net2272_ep_read(ep, EP_AVAIL1) << 8)
478 | (net2272_ep_read(ep, EP_AVAIL0));
480 count = net2272_write_packet(ep, buf, req, max);
481 /* see if we are done */
482 if (req->req.length == req->req.actual) {
483 /* validate short or zlp packet */
484 if (count < ep->ep.maxpacket)
485 set_fifo_bytecount(ep, 0);
486 net2272_done(ep, req, 0);
488 if (!list_empty(&ep->queue)) {
489 req = list_entry(ep->queue.next,
490 struct net2272_request,
491 queue);
492 status = net2272_kick_dma(ep, req);
494 if (status < 0)
495 if ((net2272_ep_read(ep, EP_STAT0)
496 & (1 << BUFFER_EMPTY)))
497 goto top;
499 return 1;
501 net2272_ep_write(ep, EP_STAT0, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT));
503 return 0;
506 static void
507 net2272_out_flush(struct net2272_ep *ep)
509 ASSERT_OUT_NAKING(ep);
511 net2272_ep_write(ep, EP_STAT0, (1 << DATA_OUT_TOKEN_INTERRUPT)
512 | (1 << DATA_PACKET_RECEIVED_INTERRUPT));
513 net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
516 static int
517 net2272_read_packet(struct net2272_ep *ep, u8 *buf,
518 struct net2272_request *req, unsigned avail)
520 u16 __iomem *ep_data = net2272_reg_addr(ep->dev, EP_DATA);
521 unsigned is_short;
522 u16 *bufp;
524 req->req.actual += avail;
526 dev_vdbg(ep->dev->dev, "read packet %s req %p len %u avail %u\n",
527 ep->ep.name, req, avail,
528 (net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0));
530 is_short = (avail < ep->ep.maxpacket);
532 if (unlikely(avail == 0)) {
533 /* remove any zlp from the buffer */
534 (void)readw(ep_data);
535 return is_short;
538 /* Ensure we get the final byte */
539 if (unlikely(avail % 2))
540 avail++;
541 bufp = (u16 *)buf;
543 do {
544 *bufp++ = readw(ep_data);
545 avail -= 2;
546 } while (avail);
549 * To avoid false endpoint available race condition must read
550 * ep stat0 twice in the case of a short transfer
552 if (net2272_ep_read(ep, EP_STAT0) & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT))
553 net2272_ep_read(ep, EP_STAT0);
555 return is_short;
558 static int
559 net2272_read_fifo(struct net2272_ep *ep, struct net2272_request *req)
561 u8 *buf;
562 unsigned is_short;
563 int count;
564 int tmp;
565 int cleanup = 0;
566 int status = -1;
568 dev_vdbg(ep->dev->dev, "read_fifo %s actual %d len %d\n",
569 ep->ep.name, req->req.actual, req->req.length);
571 top:
572 do {
573 buf = req->req.buf + req->req.actual;
574 prefetchw(buf);
576 count = (net2272_ep_read(ep, EP_AVAIL1) << 8)
577 | net2272_ep_read(ep, EP_AVAIL0);
579 net2272_ep_write(ep, EP_STAT0,
580 (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT) |
581 (1 << DATA_PACKET_RECEIVED_INTERRUPT));
583 tmp = req->req.length - req->req.actual;
585 if (count > tmp) {
586 if ((tmp % ep->ep.maxpacket) != 0) {
587 dev_err(ep->dev->dev,
588 "%s out fifo %d bytes, expected %d\n",
589 ep->ep.name, count, tmp);
590 cleanup = 1;
592 count = (tmp > 0) ? tmp : 0;
595 is_short = net2272_read_packet(ep, buf, req, count);
597 /* completion */
598 if (unlikely(cleanup || is_short ||
599 ((req->req.actual == req->req.length)
600 && !req->req.zero))) {
602 if (cleanup) {
603 net2272_out_flush(ep);
604 net2272_done(ep, req, -EOVERFLOW);
605 } else
606 net2272_done(ep, req, 0);
608 /* re-initialize endpoint transfer registers
609 * otherwise they may result in erroneous pre-validation
610 * for subsequent control reads
612 if (unlikely(ep->num == 0)) {
613 net2272_ep_write(ep, EP_TRANSFER2, 0);
614 net2272_ep_write(ep, EP_TRANSFER1, 0);
615 net2272_ep_write(ep, EP_TRANSFER0, 0);
618 if (!list_empty(&ep->queue)) {
619 req = list_entry(ep->queue.next,
620 struct net2272_request, queue);
621 status = net2272_kick_dma(ep, req);
622 if ((status < 0) &&
623 !(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_EMPTY)))
624 goto top;
626 return 1;
628 } while (!(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_EMPTY)));
630 return 0;
633 static void
634 net2272_pio_advance(struct net2272_ep *ep)
636 struct net2272_request *req;
638 if (unlikely(list_empty(&ep->queue)))
639 return;
641 req = list_entry(ep->queue.next, struct net2272_request, queue);
642 (ep->is_in ? net2272_write_fifo : net2272_read_fifo)(ep, req);
645 /* returns 0 on success, else negative errno */
646 static int
647 net2272_request_dma(struct net2272 *dev, unsigned ep, u32 buf,
648 unsigned len, unsigned dir)
650 dev_vdbg(dev->dev, "request_dma ep %d buf %08x len %d dir %d\n",
651 ep, buf, len, dir);
653 /* The NET2272 only supports a single dma channel */
654 if (dev->dma_busy)
655 return -EBUSY;
657 * EP_TRANSFER (used to determine the number of bytes received
658 * in an OUT transfer) is 24 bits wide; don't ask for more than that.
660 if ((dir == 1) && (len > 0x1000000))
661 return -EINVAL;
663 dev->dma_busy = 1;
665 /* initialize platform's dma */
666 #ifdef CONFIG_PCI
667 /* NET2272 addr, buffer addr, length, etc. */
668 switch (dev->dev_id) {
669 case PCI_DEVICE_ID_RDK1:
670 /* Setup PLX 9054 DMA mode */
671 writel((1 << LOCAL_BUS_WIDTH) |
672 (1 << TA_READY_INPUT_ENABLE) |
673 (0 << LOCAL_BURST_ENABLE) |
674 (1 << DONE_INTERRUPT_ENABLE) |
675 (1 << LOCAL_ADDRESSING_MODE) |
676 (1 << DEMAND_MODE) |
677 (1 << DMA_EOT_ENABLE) |
678 (1 << FAST_SLOW_TERMINATE_MODE_SELECT) |
679 (1 << DMA_CHANNEL_INTERRUPT_SELECT),
680 dev->rdk1.plx9054_base_addr + DMAMODE0);
682 writel(0x100000, dev->rdk1.plx9054_base_addr + DMALADR0);
683 writel(buf, dev->rdk1.plx9054_base_addr + DMAPADR0);
684 writel(len, dev->rdk1.plx9054_base_addr + DMASIZ0);
685 writel((dir << DIRECTION_OF_TRANSFER) |
686 (1 << INTERRUPT_AFTER_TERMINAL_COUNT),
687 dev->rdk1.plx9054_base_addr + DMADPR0);
688 writel((1 << LOCAL_DMA_CHANNEL_0_INTERRUPT_ENABLE) |
689 readl(dev->rdk1.plx9054_base_addr + INTCSR),
690 dev->rdk1.plx9054_base_addr + INTCSR);
692 break;
694 #endif
696 net2272_write(dev, DMAREQ,
697 (0 << DMA_BUFFER_VALID) |
698 (1 << DMA_REQUEST_ENABLE) |
699 (1 << DMA_CONTROL_DACK) |
700 (dev->dma_eot_polarity << EOT_POLARITY) |
701 (dev->dma_dack_polarity << DACK_POLARITY) |
702 (dev->dma_dreq_polarity << DREQ_POLARITY) |
703 ((ep >> 1) << DMA_ENDPOINT_SELECT));
705 (void) net2272_read(dev, SCRATCH);
707 return 0;
710 static void
711 net2272_start_dma(struct net2272 *dev)
713 /* start platform's dma controller */
714 #ifdef CONFIG_PCI
715 switch (dev->dev_id) {
716 case PCI_DEVICE_ID_RDK1:
717 writeb((1 << CHANNEL_ENABLE) | (1 << CHANNEL_START),
718 dev->rdk1.plx9054_base_addr + DMACSR0);
719 break;
721 #endif
724 /* returns 0 on success, else negative errno */
725 static int
726 net2272_kick_dma(struct net2272_ep *ep, struct net2272_request *req)
728 unsigned size;
729 u8 tmp;
731 if (!use_dma || (ep->num < 1) || (ep->num > 2) || !ep->dma)
732 return -EINVAL;
734 /* don't use dma for odd-length transfers
735 * otherwise, we'd need to deal with the last byte with pio
737 if (req->req.length & 1)
738 return -EINVAL;
740 dev_vdbg(ep->dev->dev, "kick_dma %s req %p dma %08llx\n",
741 ep->ep.name, req, (unsigned long long) req->req.dma);
743 net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
745 /* The NET2272 can only use DMA on one endpoint at a time */
746 if (ep->dev->dma_busy)
747 return -EBUSY;
749 /* Make sure we only DMA an even number of bytes (we'll use
750 * pio to complete the transfer)
752 size = req->req.length;
753 size &= ~1;
755 /* device-to-host transfer */
756 if (ep->is_in) {
757 /* initialize platform's dma controller */
758 if (net2272_request_dma(ep->dev, ep->num, req->req.dma, size, 0))
759 /* unable to obtain DMA channel; return error and use pio mode */
760 return -EBUSY;
761 req->req.actual += size;
763 /* host-to-device transfer */
764 } else {
765 tmp = net2272_ep_read(ep, EP_STAT0);
767 /* initialize platform's dma controller */
768 if (net2272_request_dma(ep->dev, ep->num, req->req.dma, size, 1))
769 /* unable to obtain DMA channel; return error and use pio mode */
770 return -EBUSY;
772 if (!(tmp & (1 << BUFFER_EMPTY)))
773 ep->not_empty = 1;
774 else
775 ep->not_empty = 0;
778 /* allow the endpoint's buffer to fill */
779 net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
781 /* this transfer completed and data's already in the fifo
782 * return error so pio gets used.
784 if (tmp & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
786 /* deassert dreq */
787 net2272_write(ep->dev, DMAREQ,
788 (0 << DMA_BUFFER_VALID) |
789 (0 << DMA_REQUEST_ENABLE) |
790 (1 << DMA_CONTROL_DACK) |
791 (ep->dev->dma_eot_polarity << EOT_POLARITY) |
792 (ep->dev->dma_dack_polarity << DACK_POLARITY) |
793 (ep->dev->dma_dreq_polarity << DREQ_POLARITY) |
794 ((ep->num >> 1) << DMA_ENDPOINT_SELECT));
796 return -EBUSY;
800 /* Don't use per-packet interrupts: use dma interrupts only */
801 net2272_ep_write(ep, EP_IRQENB, 0);
803 net2272_start_dma(ep->dev);
805 return 0;
808 static void net2272_cancel_dma(struct net2272 *dev)
810 #ifdef CONFIG_PCI
811 switch (dev->dev_id) {
812 case PCI_DEVICE_ID_RDK1:
813 writeb(0, dev->rdk1.plx9054_base_addr + DMACSR0);
814 writeb(1 << CHANNEL_ABORT, dev->rdk1.plx9054_base_addr + DMACSR0);
815 while (!(readb(dev->rdk1.plx9054_base_addr + DMACSR0) &
816 (1 << CHANNEL_DONE)))
817 continue; /* wait for dma to stabalize */
819 /* dma abort generates an interrupt */
820 writeb(1 << CHANNEL_CLEAR_INTERRUPT,
821 dev->rdk1.plx9054_base_addr + DMACSR0);
822 break;
824 #endif
826 dev->dma_busy = 0;
829 /*---------------------------------------------------------------------------*/
831 static int
832 net2272_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
834 struct net2272_request *req;
835 struct net2272_ep *ep;
836 struct net2272 *dev;
837 unsigned long flags;
838 int status = -1;
839 u8 s;
841 req = container_of(_req, struct net2272_request, req);
842 if (!_req || !_req->complete || !_req->buf
843 || !list_empty(&req->queue))
844 return -EINVAL;
845 ep = container_of(_ep, struct net2272_ep, ep);
846 if (!_ep || (!ep->desc && ep->num != 0))
847 return -EINVAL;
848 dev = ep->dev;
849 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
850 return -ESHUTDOWN;
852 /* set up dma mapping in case the caller didn't */
853 if (use_dma && ep->dma && _req->dma == DMA_ADDR_INVALID) {
854 _req->dma = dma_map_single(dev->dev, _req->buf, _req->length,
855 ep->is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
856 req->mapped = 1;
859 dev_vdbg(dev->dev, "%s queue req %p, len %d buf %p dma %08llx %s\n",
860 _ep->name, _req, _req->length, _req->buf,
861 (unsigned long long) _req->dma, _req->zero ? "zero" : "!zero");
863 spin_lock_irqsave(&dev->lock, flags);
865 _req->status = -EINPROGRESS;
866 _req->actual = 0;
868 /* kickstart this i/o queue? */
869 if (list_empty(&ep->queue) && !ep->stopped) {
870 /* maybe there's no control data, just status ack */
871 if (ep->num == 0 && _req->length == 0) {
872 net2272_done(ep, req, 0);
873 dev_vdbg(dev->dev, "%s status ack\n", ep->ep.name);
874 goto done;
877 /* Return zlp, don't let it block subsequent packets */
878 s = net2272_ep_read(ep, EP_STAT0);
879 if (s & (1 << BUFFER_EMPTY)) {
880 /* Buffer is empty check for a blocking zlp, handle it */
881 if ((s & (1 << NAK_OUT_PACKETS)) &&
882 net2272_ep_read(ep, EP_STAT1) & (1 << LOCAL_OUT_ZLP)) {
883 dev_dbg(dev->dev, "WARNING: returning ZLP short packet termination!\n");
885 * Request is going to terminate with a short packet ...
886 * hope the client is ready for it!
888 status = net2272_read_fifo(ep, req);
889 /* clear short packet naking */
890 net2272_ep_write(ep, EP_STAT0, (1 << NAK_OUT_PACKETS));
891 goto done;
895 /* try dma first */
896 status = net2272_kick_dma(ep, req);
898 if (status < 0) {
899 /* dma failed (most likely in use by another endpoint)
900 * fallback to pio
902 status = 0;
904 if (ep->is_in)
905 status = net2272_write_fifo(ep, req);
906 else {
907 s = net2272_ep_read(ep, EP_STAT0);
908 if ((s & (1 << BUFFER_EMPTY)) == 0)
909 status = net2272_read_fifo(ep, req);
912 if (unlikely(status != 0)) {
913 if (status > 0)
914 status = 0;
915 req = NULL;
919 if (likely(req != 0))
920 list_add_tail(&req->queue, &ep->queue);
922 if (likely(!list_empty(&ep->queue)))
923 net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
924 done:
925 spin_unlock_irqrestore(&dev->lock, flags);
927 return 0;
930 /* dequeue ALL requests */
931 static void
932 net2272_dequeue_all(struct net2272_ep *ep)
934 struct net2272_request *req;
936 /* called with spinlock held */
937 ep->stopped = 1;
939 while (!list_empty(&ep->queue)) {
940 req = list_entry(ep->queue.next,
941 struct net2272_request,
942 queue);
943 net2272_done(ep, req, -ESHUTDOWN);
947 /* dequeue JUST ONE request */
948 static int
949 net2272_dequeue(struct usb_ep *_ep, struct usb_request *_req)
951 struct net2272_ep *ep;
952 struct net2272_request *req;
953 unsigned long flags;
954 int stopped;
956 ep = container_of(_ep, struct net2272_ep, ep);
957 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
958 return -EINVAL;
960 spin_lock_irqsave(&ep->dev->lock, flags);
961 stopped = ep->stopped;
962 ep->stopped = 1;
964 /* make sure it's still queued on this endpoint */
965 list_for_each_entry(req, &ep->queue, queue) {
966 if (&req->req == _req)
967 break;
969 if (&req->req != _req) {
970 spin_unlock_irqrestore(&ep->dev->lock, flags);
971 return -EINVAL;
974 /* queue head may be partially complete */
975 if (ep->queue.next == &req->queue) {
976 dev_dbg(ep->dev->dev, "unlink (%s) pio\n", _ep->name);
977 net2272_done(ep, req, -ECONNRESET);
979 req = NULL;
980 ep->stopped = stopped;
982 spin_unlock_irqrestore(&ep->dev->lock, flags);
983 return 0;
986 /*---------------------------------------------------------------------------*/
988 static int
989 net2272_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
991 struct net2272_ep *ep;
992 unsigned long flags;
993 int ret = 0;
995 ep = container_of(_ep, struct net2272_ep, ep);
996 if (!_ep || (!ep->desc && ep->num != 0))
997 return -EINVAL;
998 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
999 return -ESHUTDOWN;
1000 if (ep->desc /* not ep0 */ && usb_endpoint_xfer_isoc(ep->desc))
1001 return -EINVAL;
1003 spin_lock_irqsave(&ep->dev->lock, flags);
1004 if (!list_empty(&ep->queue))
1005 ret = -EAGAIN;
1006 else if (ep->is_in && value && net2272_fifo_status(_ep) != 0)
1007 ret = -EAGAIN;
1008 else {
1009 dev_vdbg(ep->dev->dev, "%s %s %s\n", _ep->name,
1010 value ? "set" : "clear",
1011 wedged ? "wedge" : "halt");
1012 /* set/clear */
1013 if (value) {
1014 if (ep->num == 0)
1015 ep->dev->protocol_stall = 1;
1016 else
1017 set_halt(ep);
1018 if (wedged)
1019 ep->wedged = 1;
1020 } else {
1021 clear_halt(ep);
1022 ep->wedged = 0;
1025 spin_unlock_irqrestore(&ep->dev->lock, flags);
1027 return ret;
1030 static int
1031 net2272_set_halt(struct usb_ep *_ep, int value)
1033 return net2272_set_halt_and_wedge(_ep, value, 0);
1036 static int
1037 net2272_set_wedge(struct usb_ep *_ep)
1039 if (!_ep || _ep->name == ep0name)
1040 return -EINVAL;
1041 return net2272_set_halt_and_wedge(_ep, 1, 1);
1044 static int
1045 net2272_fifo_status(struct usb_ep *_ep)
1047 struct net2272_ep *ep;
1048 u16 avail;
1050 ep = container_of(_ep, struct net2272_ep, ep);
1051 if (!_ep || (!ep->desc && ep->num != 0))
1052 return -ENODEV;
1053 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1054 return -ESHUTDOWN;
1056 avail = net2272_ep_read(ep, EP_AVAIL1) << 8;
1057 avail |= net2272_ep_read(ep, EP_AVAIL0);
1058 if (avail > ep->fifo_size)
1059 return -EOVERFLOW;
1060 if (ep->is_in)
1061 avail = ep->fifo_size - avail;
1062 return avail;
1065 static void
1066 net2272_fifo_flush(struct usb_ep *_ep)
1068 struct net2272_ep *ep;
1070 ep = container_of(_ep, struct net2272_ep, ep);
1071 if (!_ep || (!ep->desc && ep->num != 0))
1072 return;
1073 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1074 return;
1076 net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
1079 static struct usb_ep_ops net2272_ep_ops = {
1080 .enable = net2272_enable,
1081 .disable = net2272_disable,
1083 .alloc_request = net2272_alloc_request,
1084 .free_request = net2272_free_request,
1086 .queue = net2272_queue,
1087 .dequeue = net2272_dequeue,
1089 .set_halt = net2272_set_halt,
1090 .set_wedge = net2272_set_wedge,
1091 .fifo_status = net2272_fifo_status,
1092 .fifo_flush = net2272_fifo_flush,
1095 /*---------------------------------------------------------------------------*/
1097 static int
1098 net2272_get_frame(struct usb_gadget *_gadget)
1100 struct net2272 *dev;
1101 unsigned long flags;
1102 u16 ret;
1104 if (!_gadget)
1105 return -ENODEV;
1106 dev = container_of(_gadget, struct net2272, gadget);
1107 spin_lock_irqsave(&dev->lock, flags);
1109 ret = net2272_read(dev, FRAME1) << 8;
1110 ret |= net2272_read(dev, FRAME0);
1112 spin_unlock_irqrestore(&dev->lock, flags);
1113 return ret;
1116 static int
1117 net2272_wakeup(struct usb_gadget *_gadget)
1119 struct net2272 *dev;
1120 u8 tmp;
1121 unsigned long flags;
1123 if (!_gadget)
1124 return 0;
1125 dev = container_of(_gadget, struct net2272, gadget);
1127 spin_lock_irqsave(&dev->lock, flags);
1128 tmp = net2272_read(dev, USBCTL0);
1129 if (tmp & (1 << IO_WAKEUP_ENABLE))
1130 net2272_write(dev, USBCTL1, (1 << GENERATE_RESUME));
1132 spin_unlock_irqrestore(&dev->lock, flags);
1134 return 0;
1137 static int
1138 net2272_set_selfpowered(struct usb_gadget *_gadget, int value)
1140 struct net2272 *dev;
1142 if (!_gadget)
1143 return -ENODEV;
1144 dev = container_of(_gadget, struct net2272, gadget);
1146 dev->is_selfpowered = value;
1148 return 0;
1151 static int
1152 net2272_pullup(struct usb_gadget *_gadget, int is_on)
1154 struct net2272 *dev;
1155 u8 tmp;
1156 unsigned long flags;
1158 if (!_gadget)
1159 return -ENODEV;
1160 dev = container_of(_gadget, struct net2272, gadget);
1162 spin_lock_irqsave(&dev->lock, flags);
1163 tmp = net2272_read(dev, USBCTL0);
1164 dev->softconnect = (is_on != 0);
1165 if (is_on)
1166 tmp |= (1 << USB_DETECT_ENABLE);
1167 else
1168 tmp &= ~(1 << USB_DETECT_ENABLE);
1169 net2272_write(dev, USBCTL0, tmp);
1170 spin_unlock_irqrestore(&dev->lock, flags);
1172 return 0;
1175 static int net2272_start(struct usb_gadget *_gadget,
1176 struct usb_gadget_driver *driver);
1177 static int net2272_stop(struct usb_gadget *_gadget,
1178 struct usb_gadget_driver *driver);
1180 static const struct usb_gadget_ops net2272_ops = {
1181 .get_frame = net2272_get_frame,
1182 .wakeup = net2272_wakeup,
1183 .set_selfpowered = net2272_set_selfpowered,
1184 .pullup = net2272_pullup,
1185 .udc_start = net2272_start,
1186 .udc_stop = net2272_stop,
1189 /*---------------------------------------------------------------------------*/
1191 static ssize_t
1192 net2272_show_registers(struct device *_dev, struct device_attribute *attr, char *buf)
1194 struct net2272 *dev;
1195 char *next;
1196 unsigned size, t;
1197 unsigned long flags;
1198 u8 t1, t2;
1199 int i;
1200 const char *s;
1202 dev = dev_get_drvdata(_dev);
1203 next = buf;
1204 size = PAGE_SIZE;
1205 spin_lock_irqsave(&dev->lock, flags);
1207 if (dev->driver)
1208 s = dev->driver->driver.name;
1209 else
1210 s = "(none)";
1212 /* Main Control Registers */
1213 t = scnprintf(next, size, "%s version %s,"
1214 "chiprev %02x, locctl %02x\n"
1215 "irqenb0 %02x irqenb1 %02x "
1216 "irqstat0 %02x irqstat1 %02x\n",
1217 driver_name, driver_vers, dev->chiprev,
1218 net2272_read(dev, LOCCTL),
1219 net2272_read(dev, IRQENB0),
1220 net2272_read(dev, IRQENB1),
1221 net2272_read(dev, IRQSTAT0),
1222 net2272_read(dev, IRQSTAT1));
1223 size -= t;
1224 next += t;
1226 /* DMA */
1227 t1 = net2272_read(dev, DMAREQ);
1228 t = scnprintf(next, size, "\ndmareq %02x: %s %s%s%s%s\n",
1229 t1, ep_name[(t1 & 0x01) + 1],
1230 t1 & (1 << DMA_CONTROL_DACK) ? "dack " : "",
1231 t1 & (1 << DMA_REQUEST_ENABLE) ? "reqenb " : "",
1232 t1 & (1 << DMA_REQUEST) ? "req " : "",
1233 t1 & (1 << DMA_BUFFER_VALID) ? "valid " : "");
1234 size -= t;
1235 next += t;
1237 /* USB Control Registers */
1238 t1 = net2272_read(dev, USBCTL1);
1239 if (t1 & (1 << VBUS_PIN)) {
1240 if (t1 & (1 << USB_HIGH_SPEED))
1241 s = "high speed";
1242 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1243 s = "powered";
1244 else
1245 s = "full speed";
1246 } else
1247 s = "not attached";
1248 t = scnprintf(next, size,
1249 "usbctl0 %02x usbctl1 %02x addr 0x%02x (%s)\n",
1250 net2272_read(dev, USBCTL0), t1,
1251 net2272_read(dev, OURADDR), s);
1252 size -= t;
1253 next += t;
1255 /* Endpoint Registers */
1256 for (i = 0; i < 4; ++i) {
1257 struct net2272_ep *ep;
1259 ep = &dev->ep[i];
1260 if (i && !ep->desc)
1261 continue;
1263 t1 = net2272_ep_read(ep, EP_CFG);
1264 t2 = net2272_ep_read(ep, EP_RSPSET);
1265 t = scnprintf(next, size,
1266 "\n%s\tcfg %02x rsp (%02x) %s%s%s%s%s%s%s%s"
1267 "irqenb %02x\n",
1268 ep->ep.name, t1, t2,
1269 (t2 & (1 << ALT_NAK_OUT_PACKETS)) ? "NAK " : "",
1270 (t2 & (1 << HIDE_STATUS_PHASE)) ? "hide " : "",
1271 (t2 & (1 << AUTOVALIDATE)) ? "auto " : "",
1272 (t2 & (1 << INTERRUPT_MODE)) ? "interrupt " : "",
1273 (t2 & (1 << CONTROL_STATUS_PHASE_HANDSHAKE)) ? "status " : "",
1274 (t2 & (1 << NAK_OUT_PACKETS_MODE)) ? "NAKmode " : "",
1275 (t2 & (1 << ENDPOINT_TOGGLE)) ? "DATA1 " : "DATA0 ",
1276 (t2 & (1 << ENDPOINT_HALT)) ? "HALT " : "",
1277 net2272_ep_read(ep, EP_IRQENB));
1278 size -= t;
1279 next += t;
1281 t = scnprintf(next, size,
1282 "\tstat0 %02x stat1 %02x avail %04x "
1283 "(ep%d%s-%s)%s\n",
1284 net2272_ep_read(ep, EP_STAT0),
1285 net2272_ep_read(ep, EP_STAT1),
1286 (net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0),
1287 t1 & 0x0f,
1288 ep->is_in ? "in" : "out",
1289 type_string(t1 >> 5),
1290 ep->stopped ? "*" : "");
1291 size -= t;
1292 next += t;
1294 t = scnprintf(next, size,
1295 "\tep_transfer %06x\n",
1296 ((net2272_ep_read(ep, EP_TRANSFER2) & 0xff) << 16) |
1297 ((net2272_ep_read(ep, EP_TRANSFER1) & 0xff) << 8) |
1298 ((net2272_ep_read(ep, EP_TRANSFER0) & 0xff)));
1299 size -= t;
1300 next += t;
1302 t1 = net2272_ep_read(ep, EP_BUFF_STATES) & 0x03;
1303 t2 = (net2272_ep_read(ep, EP_BUFF_STATES) >> 2) & 0x03;
1304 t = scnprintf(next, size,
1305 "\tbuf-a %s buf-b %s\n",
1306 buf_state_string(t1),
1307 buf_state_string(t2));
1308 size -= t;
1309 next += t;
1312 spin_unlock_irqrestore(&dev->lock, flags);
1314 return PAGE_SIZE - size;
1316 static DEVICE_ATTR(registers, S_IRUGO, net2272_show_registers, NULL);
1318 /*---------------------------------------------------------------------------*/
1320 static void
1321 net2272_set_fifo_mode(struct net2272 *dev, int mode)
1323 u8 tmp;
1325 tmp = net2272_read(dev, LOCCTL) & 0x3f;
1326 tmp |= (mode << 6);
1327 net2272_write(dev, LOCCTL, tmp);
1329 INIT_LIST_HEAD(&dev->gadget.ep_list);
1331 /* always ep-a, ep-c ... maybe not ep-b */
1332 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1334 switch (mode) {
1335 case 0:
1336 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1337 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 512;
1338 break;
1339 case 1:
1340 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1341 dev->ep[1].fifo_size = 1024;
1342 dev->ep[2].fifo_size = 512;
1343 break;
1344 case 2:
1345 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1346 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
1347 break;
1348 case 3:
1349 dev->ep[1].fifo_size = 1024;
1350 break;
1353 /* ep-c is always 2 512 byte buffers */
1354 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1355 dev->ep[3].fifo_size = 512;
1358 /*---------------------------------------------------------------------------*/
1360 static void
1361 net2272_usb_reset(struct net2272 *dev)
1363 dev->gadget.speed = USB_SPEED_UNKNOWN;
1365 net2272_cancel_dma(dev);
1367 net2272_write(dev, IRQENB0, 0);
1368 net2272_write(dev, IRQENB1, 0);
1370 /* clear irq state */
1371 net2272_write(dev, IRQSTAT0, 0xff);
1372 net2272_write(dev, IRQSTAT1, ~(1 << SUSPEND_REQUEST_INTERRUPT));
1374 net2272_write(dev, DMAREQ,
1375 (0 << DMA_BUFFER_VALID) |
1376 (0 << DMA_REQUEST_ENABLE) |
1377 (1 << DMA_CONTROL_DACK) |
1378 (dev->dma_eot_polarity << EOT_POLARITY) |
1379 (dev->dma_dack_polarity << DACK_POLARITY) |
1380 (dev->dma_dreq_polarity << DREQ_POLARITY) |
1381 ((dma_ep >> 1) << DMA_ENDPOINT_SELECT));
1383 net2272_cancel_dma(dev);
1384 net2272_set_fifo_mode(dev, (fifo_mode <= 3) ? fifo_mode : 0);
1386 /* Set the NET2272 ep fifo data width to 16-bit mode and for correct byte swapping
1387 * note that the higher level gadget drivers are expected to convert data to little endian.
1388 * Enable byte swap for your local bus/cpu if needed by setting BYTE_SWAP in LOCCTL here
1390 net2272_write(dev, LOCCTL, net2272_read(dev, LOCCTL) | (1 << DATA_WIDTH));
1391 net2272_write(dev, LOCCTL1, (dma_mode << DMA_MODE));
1394 static void
1395 net2272_usb_reinit(struct net2272 *dev)
1397 int i;
1399 /* basic endpoint init */
1400 for (i = 0; i < 4; ++i) {
1401 struct net2272_ep *ep = &dev->ep[i];
1403 ep->ep.name = ep_name[i];
1404 ep->dev = dev;
1405 ep->num = i;
1406 ep->not_empty = 0;
1408 if (use_dma && ep->num == dma_ep)
1409 ep->dma = 1;
1411 if (i > 0 && i <= 3)
1412 ep->fifo_size = 512;
1413 else
1414 ep->fifo_size = 64;
1415 net2272_ep_reset(ep);
1417 dev->ep[0].ep.maxpacket = 64;
1419 dev->gadget.ep0 = &dev->ep[0].ep;
1420 dev->ep[0].stopped = 0;
1421 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1424 static void
1425 net2272_ep0_start(struct net2272 *dev)
1427 struct net2272_ep *ep0 = &dev->ep[0];
1429 net2272_ep_write(ep0, EP_RSPSET,
1430 (1 << NAK_OUT_PACKETS_MODE) |
1431 (1 << ALT_NAK_OUT_PACKETS));
1432 net2272_ep_write(ep0, EP_RSPCLR,
1433 (1 << HIDE_STATUS_PHASE) |
1434 (1 << CONTROL_STATUS_PHASE_HANDSHAKE));
1435 net2272_write(dev, USBCTL0,
1436 (dev->softconnect << USB_DETECT_ENABLE) |
1437 (1 << USB_ROOT_PORT_WAKEUP_ENABLE) |
1438 (1 << IO_WAKEUP_ENABLE));
1439 net2272_write(dev, IRQENB0,
1440 (1 << SETUP_PACKET_INTERRUPT_ENABLE) |
1441 (1 << ENDPOINT_0_INTERRUPT_ENABLE) |
1442 (1 << DMA_DONE_INTERRUPT_ENABLE));
1443 net2272_write(dev, IRQENB1,
1444 (1 << VBUS_INTERRUPT_ENABLE) |
1445 (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE) |
1446 (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE));
1449 /* when a driver is successfully registered, it will receive
1450 * control requests including set_configuration(), which enables
1451 * non-control requests. then usb traffic follows until a
1452 * disconnect is reported. then a host may connect again, or
1453 * the driver might get unbound.
1455 static int net2272_start(struct usb_gadget *_gadget,
1456 struct usb_gadget_driver *driver)
1458 struct net2272 *dev;
1459 unsigned i;
1461 if (!driver || !driver->unbind || !driver->setup ||
1462 driver->max_speed != USB_SPEED_HIGH)
1463 return -EINVAL;
1465 dev = container_of(_gadget, struct net2272, gadget);
1467 for (i = 0; i < 4; ++i)
1468 dev->ep[i].irqs = 0;
1469 /* hook up the driver ... */
1470 dev->softconnect = 1;
1471 driver->driver.bus = NULL;
1472 dev->driver = driver;
1473 dev->gadget.dev.driver = &driver->driver;
1475 /* ... then enable host detection and ep0; and we're ready
1476 * for set_configuration as well as eventual disconnect.
1478 net2272_ep0_start(dev);
1480 dev_dbg(dev->dev, "%s ready\n", driver->driver.name);
1482 return 0;
1485 static void
1486 stop_activity(struct net2272 *dev, struct usb_gadget_driver *driver)
1488 int i;
1490 /* don't disconnect if it's not connected */
1491 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1492 driver = NULL;
1494 /* stop hardware; prevent new request submissions;
1495 * and kill any outstanding requests.
1497 net2272_usb_reset(dev);
1498 for (i = 0; i < 4; ++i)
1499 net2272_dequeue_all(&dev->ep[i]);
1501 net2272_usb_reinit(dev);
1504 static int net2272_stop(struct usb_gadget *_gadget,
1505 struct usb_gadget_driver *driver)
1507 struct net2272 *dev;
1508 unsigned long flags;
1510 dev = container_of(_gadget, struct net2272, gadget);
1512 spin_lock_irqsave(&dev->lock, flags);
1513 stop_activity(dev, driver);
1514 spin_unlock_irqrestore(&dev->lock, flags);
1516 dev->gadget.dev.driver = NULL;
1517 dev->driver = NULL;
1519 dev_dbg(dev->dev, "unregistered driver '%s'\n", driver->driver.name);
1520 return 0;
1523 /*---------------------------------------------------------------------------*/
1524 /* handle ep-a/ep-b dma completions */
1525 static void
1526 net2272_handle_dma(struct net2272_ep *ep)
1528 struct net2272_request *req;
1529 unsigned len;
1530 int status;
1532 if (!list_empty(&ep->queue))
1533 req = list_entry(ep->queue.next,
1534 struct net2272_request, queue);
1535 else
1536 req = NULL;
1538 dev_vdbg(ep->dev->dev, "handle_dma %s req %p\n", ep->ep.name, req);
1540 /* Ensure DREQ is de-asserted */
1541 net2272_write(ep->dev, DMAREQ,
1542 (0 << DMA_BUFFER_VALID)
1543 | (0 << DMA_REQUEST_ENABLE)
1544 | (1 << DMA_CONTROL_DACK)
1545 | (ep->dev->dma_eot_polarity << EOT_POLARITY)
1546 | (ep->dev->dma_dack_polarity << DACK_POLARITY)
1547 | (ep->dev->dma_dreq_polarity << DREQ_POLARITY)
1548 | ((ep->dma >> 1) << DMA_ENDPOINT_SELECT));
1550 ep->dev->dma_busy = 0;
1552 net2272_ep_write(ep, EP_IRQENB,
1553 (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
1554 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
1555 | net2272_ep_read(ep, EP_IRQENB));
1557 /* device-to-host transfer completed */
1558 if (ep->is_in) {
1559 /* validate a short packet or zlp if necessary */
1560 if ((req->req.length % ep->ep.maxpacket != 0) ||
1561 req->req.zero)
1562 set_fifo_bytecount(ep, 0);
1564 net2272_done(ep, req, 0);
1565 if (!list_empty(&ep->queue)) {
1566 req = list_entry(ep->queue.next,
1567 struct net2272_request, queue);
1568 status = net2272_kick_dma(ep, req);
1569 if (status < 0)
1570 net2272_pio_advance(ep);
1573 /* host-to-device transfer completed */
1574 } else {
1575 /* terminated with a short packet? */
1576 if (net2272_read(ep->dev, IRQSTAT0) &
1577 (1 << DMA_DONE_INTERRUPT)) {
1578 /* abort system dma */
1579 net2272_cancel_dma(ep->dev);
1582 /* EP_TRANSFER will contain the number of bytes
1583 * actually received.
1584 * NOTE: There is no overflow detection on EP_TRANSFER:
1585 * We can't deal with transfers larger than 2^24 bytes!
1587 len = (net2272_ep_read(ep, EP_TRANSFER2) << 16)
1588 | (net2272_ep_read(ep, EP_TRANSFER1) << 8)
1589 | (net2272_ep_read(ep, EP_TRANSFER0));
1591 if (ep->not_empty)
1592 len += 4;
1594 req->req.actual += len;
1596 /* get any remaining data */
1597 net2272_pio_advance(ep);
1601 /*---------------------------------------------------------------------------*/
1603 static void
1604 net2272_handle_ep(struct net2272_ep *ep)
1606 struct net2272_request *req;
1607 u8 stat0, stat1;
1609 if (!list_empty(&ep->queue))
1610 req = list_entry(ep->queue.next,
1611 struct net2272_request, queue);
1612 else
1613 req = NULL;
1615 /* ack all, and handle what we care about */
1616 stat0 = net2272_ep_read(ep, EP_STAT0);
1617 stat1 = net2272_ep_read(ep, EP_STAT1);
1618 ep->irqs++;
1620 dev_vdbg(ep->dev->dev, "%s ack ep_stat0 %02x, ep_stat1 %02x, req %p\n",
1621 ep->ep.name, stat0, stat1, req ? &req->req : 0);
1623 net2272_ep_write(ep, EP_STAT0, stat0 &
1624 ~((1 << NAK_OUT_PACKETS)
1625 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)));
1626 net2272_ep_write(ep, EP_STAT1, stat1);
1628 /* data packet(s) received (in the fifo, OUT)
1629 * direction must be validated, otherwise control read status phase
1630 * could be interpreted as a valid packet
1632 if (!ep->is_in && (stat0 & (1 << DATA_PACKET_RECEIVED_INTERRUPT)))
1633 net2272_pio_advance(ep);
1634 /* data packet(s) transmitted (IN) */
1635 else if (stat0 & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
1636 net2272_pio_advance(ep);
1639 static struct net2272_ep *
1640 net2272_get_ep_by_addr(struct net2272 *dev, u16 wIndex)
1642 struct net2272_ep *ep;
1644 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
1645 return &dev->ep[0];
1647 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
1648 u8 bEndpointAddress;
1650 if (!ep->desc)
1651 continue;
1652 bEndpointAddress = ep->desc->bEndpointAddress;
1653 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
1654 continue;
1655 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
1656 return ep;
1658 return NULL;
1662 * USB Test Packet:
1663 * JKJKJKJK * 9
1664 * JJKKJJKK * 8
1665 * JJJJKKKK * 8
1666 * JJJJJJJKKKKKKK * 8
1667 * JJJJJJJK * 8
1668 * {JKKKKKKK * 10}, JK
1670 static const u8 net2272_test_packet[] = {
1671 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1672 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1673 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1674 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1675 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1676 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFD, 0x7E
1679 static void
1680 net2272_set_test_mode(struct net2272 *dev, int mode)
1682 int i;
1684 /* Disable all net2272 interrupts:
1685 * Nothing but a power cycle should stop the test.
1687 net2272_write(dev, IRQENB0, 0x00);
1688 net2272_write(dev, IRQENB1, 0x00);
1690 /* Force tranceiver to high-speed */
1691 net2272_write(dev, XCVRDIAG, 1 << FORCE_HIGH_SPEED);
1693 net2272_write(dev, PAGESEL, 0);
1694 net2272_write(dev, EP_STAT0, 1 << DATA_PACKET_TRANSMITTED_INTERRUPT);
1695 net2272_write(dev, EP_RSPCLR,
1696 (1 << CONTROL_STATUS_PHASE_HANDSHAKE)
1697 | (1 << HIDE_STATUS_PHASE));
1698 net2272_write(dev, EP_CFG, 1 << ENDPOINT_DIRECTION);
1699 net2272_write(dev, EP_STAT1, 1 << BUFFER_FLUSH);
1701 /* wait for status phase to complete */
1702 while (!(net2272_read(dev, EP_STAT0) &
1703 (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)))
1706 /* Enable test mode */
1707 net2272_write(dev, USBTEST, mode);
1709 /* load test packet */
1710 if (mode == TEST_PACKET) {
1711 /* switch to 8 bit mode */
1712 net2272_write(dev, LOCCTL, net2272_read(dev, LOCCTL) &
1713 ~(1 << DATA_WIDTH));
1715 for (i = 0; i < sizeof(net2272_test_packet); ++i)
1716 net2272_write(dev, EP_DATA, net2272_test_packet[i]);
1718 /* Validate test packet */
1719 net2272_write(dev, EP_TRANSFER0, 0);
1723 static void
1724 net2272_handle_stat0_irqs(struct net2272 *dev, u8 stat)
1726 struct net2272_ep *ep;
1727 u8 num, scratch;
1729 /* starting a control request? */
1730 if (unlikely(stat & (1 << SETUP_PACKET_INTERRUPT))) {
1731 union {
1732 u8 raw[8];
1733 struct usb_ctrlrequest r;
1734 } u;
1735 int tmp = 0;
1736 struct net2272_request *req;
1738 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
1739 if (net2272_read(dev, USBCTL1) & (1 << USB_HIGH_SPEED))
1740 dev->gadget.speed = USB_SPEED_HIGH;
1741 else
1742 dev->gadget.speed = USB_SPEED_FULL;
1743 dev_dbg(dev->dev, "%s\n",
1744 usb_speed_string(dev->gadget.speed));
1747 ep = &dev->ep[0];
1748 ep->irqs++;
1750 /* make sure any leftover interrupt state is cleared */
1751 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
1752 while (!list_empty(&ep->queue)) {
1753 req = list_entry(ep->queue.next,
1754 struct net2272_request, queue);
1755 net2272_done(ep, req,
1756 (req->req.actual == req->req.length) ? 0 : -EPROTO);
1758 ep->stopped = 0;
1759 dev->protocol_stall = 0;
1760 net2272_ep_write(ep, EP_STAT0,
1761 (1 << DATA_IN_TOKEN_INTERRUPT)
1762 | (1 << DATA_OUT_TOKEN_INTERRUPT)
1763 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
1764 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
1765 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT));
1766 net2272_ep_write(ep, EP_STAT1,
1767 (1 << TIMEOUT)
1768 | (1 << USB_OUT_ACK_SENT)
1769 | (1 << USB_OUT_NAK_SENT)
1770 | (1 << USB_IN_ACK_RCVD)
1771 | (1 << USB_IN_NAK_SENT)
1772 | (1 << USB_STALL_SENT)
1773 | (1 << LOCAL_OUT_ZLP));
1776 * Ensure Control Read pre-validation setting is beyond maximum size
1777 * - Control Writes can leave non-zero values in EP_TRANSFER. If
1778 * an EP0 transfer following the Control Write is a Control Read,
1779 * the NET2272 sees the non-zero EP_TRANSFER as an unexpected
1780 * pre-validation count.
1781 * - Setting EP_TRANSFER beyond the maximum EP0 transfer size ensures
1782 * the pre-validation count cannot cause an unexpected validatation
1784 net2272_write(dev, PAGESEL, 0);
1785 net2272_write(dev, EP_TRANSFER2, 0xff);
1786 net2272_write(dev, EP_TRANSFER1, 0xff);
1787 net2272_write(dev, EP_TRANSFER0, 0xff);
1789 u.raw[0] = net2272_read(dev, SETUP0);
1790 u.raw[1] = net2272_read(dev, SETUP1);
1791 u.raw[2] = net2272_read(dev, SETUP2);
1792 u.raw[3] = net2272_read(dev, SETUP3);
1793 u.raw[4] = net2272_read(dev, SETUP4);
1794 u.raw[5] = net2272_read(dev, SETUP5);
1795 u.raw[6] = net2272_read(dev, SETUP6);
1796 u.raw[7] = net2272_read(dev, SETUP7);
1798 * If you have a big endian cpu make sure le16_to_cpus
1799 * performs the proper byte swapping here...
1801 le16_to_cpus(&u.r.wValue);
1802 le16_to_cpus(&u.r.wIndex);
1803 le16_to_cpus(&u.r.wLength);
1805 /* ack the irq */
1806 net2272_write(dev, IRQSTAT0, 1 << SETUP_PACKET_INTERRUPT);
1807 stat ^= (1 << SETUP_PACKET_INTERRUPT);
1809 /* watch control traffic at the token level, and force
1810 * synchronization before letting the status phase happen.
1812 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1813 if (ep->is_in) {
1814 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
1815 | (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE)
1816 | (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE);
1817 stop_out_naking(ep);
1818 } else
1819 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
1820 | (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE)
1821 | (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE);
1822 net2272_ep_write(ep, EP_IRQENB, scratch);
1824 if ((u.r.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1825 goto delegate;
1826 switch (u.r.bRequest) {
1827 case USB_REQ_GET_STATUS: {
1828 struct net2272_ep *e;
1829 u16 status = 0;
1831 switch (u.r.bRequestType & USB_RECIP_MASK) {
1832 case USB_RECIP_ENDPOINT:
1833 e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1834 if (!e || u.r.wLength > 2)
1835 goto do_stall;
1836 if (net2272_ep_read(e, EP_RSPSET) & (1 << ENDPOINT_HALT))
1837 status = __constant_cpu_to_le16(1);
1838 else
1839 status = __constant_cpu_to_le16(0);
1841 /* don't bother with a request object! */
1842 net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1843 writew(status, net2272_reg_addr(dev, EP_DATA));
1844 set_fifo_bytecount(&dev->ep[0], 0);
1845 allow_status(ep);
1846 dev_vdbg(dev->dev, "%s stat %02x\n",
1847 ep->ep.name, status);
1848 goto next_endpoints;
1849 case USB_RECIP_DEVICE:
1850 if (u.r.wLength > 2)
1851 goto do_stall;
1852 if (dev->is_selfpowered)
1853 status = (1 << USB_DEVICE_SELF_POWERED);
1855 /* don't bother with a request object! */
1856 net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1857 writew(status, net2272_reg_addr(dev, EP_DATA));
1858 set_fifo_bytecount(&dev->ep[0], 0);
1859 allow_status(ep);
1860 dev_vdbg(dev->dev, "device stat %02x\n", status);
1861 goto next_endpoints;
1862 case USB_RECIP_INTERFACE:
1863 if (u.r.wLength > 2)
1864 goto do_stall;
1866 /* don't bother with a request object! */
1867 net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1868 writew(status, net2272_reg_addr(dev, EP_DATA));
1869 set_fifo_bytecount(&dev->ep[0], 0);
1870 allow_status(ep);
1871 dev_vdbg(dev->dev, "interface status %02x\n", status);
1872 goto next_endpoints;
1875 break;
1877 case USB_REQ_CLEAR_FEATURE: {
1878 struct net2272_ep *e;
1880 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1881 goto delegate;
1882 if (u.r.wValue != USB_ENDPOINT_HALT ||
1883 u.r.wLength != 0)
1884 goto do_stall;
1885 e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1886 if (!e)
1887 goto do_stall;
1888 if (e->wedged) {
1889 dev_vdbg(dev->dev, "%s wedged, halt not cleared\n",
1890 ep->ep.name);
1891 } else {
1892 dev_vdbg(dev->dev, "%s clear halt\n", ep->ep.name);
1893 clear_halt(e);
1895 allow_status(ep);
1896 goto next_endpoints;
1898 case USB_REQ_SET_FEATURE: {
1899 struct net2272_ep *e;
1901 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1902 if (u.r.wIndex != NORMAL_OPERATION)
1903 net2272_set_test_mode(dev, (u.r.wIndex >> 8));
1904 allow_status(ep);
1905 dev_vdbg(dev->dev, "test mode: %d\n", u.r.wIndex);
1906 goto next_endpoints;
1907 } else if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1908 goto delegate;
1909 if (u.r.wValue != USB_ENDPOINT_HALT ||
1910 u.r.wLength != 0)
1911 goto do_stall;
1912 e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1913 if (!e)
1914 goto do_stall;
1915 set_halt(e);
1916 allow_status(ep);
1917 dev_vdbg(dev->dev, "%s set halt\n", ep->ep.name);
1918 goto next_endpoints;
1920 case USB_REQ_SET_ADDRESS: {
1921 net2272_write(dev, OURADDR, u.r.wValue & 0xff);
1922 allow_status(ep);
1923 break;
1925 default:
1926 delegate:
1927 dev_vdbg(dev->dev, "setup %02x.%02x v%04x i%04x "
1928 "ep_cfg %08x\n",
1929 u.r.bRequestType, u.r.bRequest,
1930 u.r.wValue, u.r.wIndex,
1931 net2272_ep_read(ep, EP_CFG));
1932 spin_unlock(&dev->lock);
1933 tmp = dev->driver->setup(&dev->gadget, &u.r);
1934 spin_lock(&dev->lock);
1937 /* stall ep0 on error */
1938 if (tmp < 0) {
1939 do_stall:
1940 dev_vdbg(dev->dev, "req %02x.%02x protocol STALL; stat %d\n",
1941 u.r.bRequestType, u.r.bRequest, tmp);
1942 dev->protocol_stall = 1;
1944 /* endpoint dma irq? */
1945 } else if (stat & (1 << DMA_DONE_INTERRUPT)) {
1946 net2272_cancel_dma(dev);
1947 net2272_write(dev, IRQSTAT0, 1 << DMA_DONE_INTERRUPT);
1948 stat &= ~(1 << DMA_DONE_INTERRUPT);
1949 num = (net2272_read(dev, DMAREQ) & (1 << DMA_ENDPOINT_SELECT))
1950 ? 2 : 1;
1952 ep = &dev->ep[num];
1953 net2272_handle_dma(ep);
1956 next_endpoints:
1957 /* endpoint data irq? */
1958 scratch = stat & 0x0f;
1959 stat &= ~0x0f;
1960 for (num = 0; scratch; num++) {
1961 u8 t;
1963 /* does this endpoint's FIFO and queue need tending? */
1964 t = 1 << num;
1965 if ((scratch & t) == 0)
1966 continue;
1967 scratch ^= t;
1969 ep = &dev->ep[num];
1970 net2272_handle_ep(ep);
1973 /* some interrupts we can just ignore */
1974 stat &= ~(1 << SOF_INTERRUPT);
1976 if (stat)
1977 dev_dbg(dev->dev, "unhandled irqstat0 %02x\n", stat);
1980 static void
1981 net2272_handle_stat1_irqs(struct net2272 *dev, u8 stat)
1983 u8 tmp, mask;
1985 /* after disconnect there's nothing else to do! */
1986 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
1987 mask = (1 << USB_HIGH_SPEED) | (1 << USB_FULL_SPEED);
1989 if (stat & tmp) {
1990 net2272_write(dev, IRQSTAT1, tmp);
1991 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
1992 ((net2272_read(dev, USBCTL1) & mask) == 0))
1993 || ((net2272_read(dev, USBCTL1) & (1 << VBUS_PIN))
1994 == 0))
1995 && (dev->gadget.speed != USB_SPEED_UNKNOWN)) {
1996 dev_dbg(dev->dev, "disconnect %s\n",
1997 dev->driver->driver.name);
1998 stop_activity(dev, dev->driver);
1999 net2272_ep0_start(dev);
2000 return;
2002 stat &= ~tmp;
2004 if (!stat)
2005 return;
2008 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2009 if (stat & tmp) {
2010 net2272_write(dev, IRQSTAT1, tmp);
2011 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2012 if (dev->driver->suspend)
2013 dev->driver->suspend(&dev->gadget);
2014 if (!enable_suspend) {
2015 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2016 dev_dbg(dev->dev, "Suspend disabled, ignoring\n");
2018 } else {
2019 if (dev->driver->resume)
2020 dev->driver->resume(&dev->gadget);
2022 stat &= ~tmp;
2025 /* clear any other status/irqs */
2026 if (stat)
2027 net2272_write(dev, IRQSTAT1, stat);
2029 /* some status we can just ignore */
2030 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2031 | (1 << SUSPEND_REQUEST_INTERRUPT)
2032 | (1 << RESUME_INTERRUPT));
2033 if (!stat)
2034 return;
2035 else
2036 dev_dbg(dev->dev, "unhandled irqstat1 %02x\n", stat);
2039 static irqreturn_t net2272_irq(int irq, void *_dev)
2041 struct net2272 *dev = _dev;
2042 #if defined(PLX_PCI_RDK) || defined(PLX_PCI_RDK2)
2043 u32 intcsr;
2044 #endif
2045 #if defined(PLX_PCI_RDK)
2046 u8 dmareq;
2047 #endif
2048 spin_lock(&dev->lock);
2049 #if defined(PLX_PCI_RDK)
2050 intcsr = readl(dev->rdk1.plx9054_base_addr + INTCSR);
2052 if ((intcsr & LOCAL_INTERRUPT_TEST) == LOCAL_INTERRUPT_TEST) {
2053 writel(intcsr & ~(1 << PCI_INTERRUPT_ENABLE),
2054 dev->rdk1.plx9054_base_addr + INTCSR);
2055 net2272_handle_stat1_irqs(dev, net2272_read(dev, IRQSTAT1));
2056 net2272_handle_stat0_irqs(dev, net2272_read(dev, IRQSTAT0));
2057 intcsr = readl(dev->rdk1.plx9054_base_addr + INTCSR);
2058 writel(intcsr | (1 << PCI_INTERRUPT_ENABLE),
2059 dev->rdk1.plx9054_base_addr + INTCSR);
2061 if ((intcsr & DMA_CHANNEL_0_TEST) == DMA_CHANNEL_0_TEST) {
2062 writeb((1 << CHANNEL_CLEAR_INTERRUPT | (0 << CHANNEL_ENABLE)),
2063 dev->rdk1.plx9054_base_addr + DMACSR0);
2065 dmareq = net2272_read(dev, DMAREQ);
2066 if (dmareq & 0x01)
2067 net2272_handle_dma(&dev->ep[2]);
2068 else
2069 net2272_handle_dma(&dev->ep[1]);
2071 #endif
2072 #if defined(PLX_PCI_RDK2)
2073 /* see if PCI int for us by checking irqstat */
2074 intcsr = readl(dev->rdk2.fpga_base_addr + RDK2_IRQSTAT);
2075 if (!intcsr & (1 << NET2272_PCI_IRQ))
2076 return IRQ_NONE;
2077 /* check dma interrupts */
2078 #endif
2079 /* Platform/devcice interrupt handler */
2080 #if !defined(PLX_PCI_RDK)
2081 net2272_handle_stat1_irqs(dev, net2272_read(dev, IRQSTAT1));
2082 net2272_handle_stat0_irqs(dev, net2272_read(dev, IRQSTAT0));
2083 #endif
2084 spin_unlock(&dev->lock);
2086 return IRQ_HANDLED;
2089 static int net2272_present(struct net2272 *dev)
2092 * Quick test to see if CPU can communicate properly with the NET2272.
2093 * Verifies connection using writes and reads to write/read and
2094 * read-only registers.
2096 * This routine is strongly recommended especially during early bring-up
2097 * of new hardware, however for designs that do not apply Power On System
2098 * Tests (POST) it may discarded (or perhaps minimized).
2100 unsigned int ii;
2101 u8 val, refval;
2103 /* Verify NET2272 write/read SCRATCH register can write and read */
2104 refval = net2272_read(dev, SCRATCH);
2105 for (ii = 0; ii < 0x100; ii += 7) {
2106 net2272_write(dev, SCRATCH, ii);
2107 val = net2272_read(dev, SCRATCH);
2108 if (val != ii) {
2109 dev_dbg(dev->dev,
2110 "%s: write/read SCRATCH register test failed: "
2111 "wrote:0x%2.2x, read:0x%2.2x\n",
2112 __func__, ii, val);
2113 return -EINVAL;
2116 /* To be nice, we write the original SCRATCH value back: */
2117 net2272_write(dev, SCRATCH, refval);
2119 /* Verify NET2272 CHIPREV register is read-only: */
2120 refval = net2272_read(dev, CHIPREV_2272);
2121 for (ii = 0; ii < 0x100; ii += 7) {
2122 net2272_write(dev, CHIPREV_2272, ii);
2123 val = net2272_read(dev, CHIPREV_2272);
2124 if (val != refval) {
2125 dev_dbg(dev->dev,
2126 "%s: write/read CHIPREV register test failed: "
2127 "wrote 0x%2.2x, read:0x%2.2x expected:0x%2.2x\n",
2128 __func__, ii, val, refval);
2129 return -EINVAL;
2134 * Verify NET2272's "NET2270 legacy revision" register
2135 * - NET2272 has two revision registers. The NET2270 legacy revision
2136 * register should read the same value, regardless of the NET2272
2137 * silicon revision. The legacy register applies to NET2270
2138 * firmware being applied to the NET2272.
2140 val = net2272_read(dev, CHIPREV_LEGACY);
2141 if (val != NET2270_LEGACY_REV) {
2143 * Unexpected legacy revision value
2144 * - Perhaps the chip is a NET2270?
2146 dev_dbg(dev->dev,
2147 "%s: WARNING: UNEXPECTED NET2272 LEGACY REGISTER VALUE:\n"
2148 " - CHIPREV_LEGACY: expected 0x%2.2x, got:0x%2.2x. (Not NET2272?)\n",
2149 __func__, NET2270_LEGACY_REV, val);
2150 return -EINVAL;
2154 * Verify NET2272 silicon revision
2155 * - This revision register is appropriate for the silicon version
2156 * of the NET2272
2158 val = net2272_read(dev, CHIPREV_2272);
2159 switch (val) {
2160 case CHIPREV_NET2272_R1:
2162 * NET2272 Rev 1 has DMA related errata:
2163 * - Newer silicon (Rev 1A or better) required
2165 dev_dbg(dev->dev,
2166 "%s: Rev 1 detected: newer silicon recommended for DMA support\n",
2167 __func__);
2168 break;
2169 case CHIPREV_NET2272_R1A:
2170 break;
2171 default:
2172 /* NET2272 silicon version *may* not work with this firmware */
2173 dev_dbg(dev->dev,
2174 "%s: unexpected silicon revision register value: "
2175 " CHIPREV_2272: 0x%2.2x\n",
2176 __func__, val);
2178 * Return Success, even though the chip rev is not an expected value
2179 * - Older, pre-built firmware can attempt to operate on newer silicon
2180 * - Often, new silicon is perfectly compatible
2184 /* Success: NET2272 checks out OK */
2185 return 0;
2188 static void
2189 net2272_gadget_release(struct device *_dev)
2191 struct net2272 *dev = dev_get_drvdata(_dev);
2192 kfree(dev);
2195 /*---------------------------------------------------------------------------*/
2197 static void __devexit
2198 net2272_remove(struct net2272 *dev)
2200 usb_del_gadget_udc(&dev->gadget);
2202 /* start with the driver above us */
2203 if (dev->driver) {
2204 /* should have been done already by driver model core */
2205 dev_warn(dev->dev, "pci remove, driver '%s' is still registered\n",
2206 dev->driver->driver.name);
2207 usb_gadget_unregister_driver(dev->driver);
2210 free_irq(dev->irq, dev);
2211 iounmap(dev->base_addr);
2213 device_unregister(&dev->gadget.dev);
2214 device_remove_file(dev->dev, &dev_attr_registers);
2216 dev_info(dev->dev, "unbind\n");
2219 static struct net2272 * __devinit
2220 net2272_probe_init(struct device *dev, unsigned int irq)
2222 struct net2272 *ret;
2224 if (!irq) {
2225 dev_dbg(dev, "No IRQ!\n");
2226 return ERR_PTR(-ENODEV);
2229 /* alloc, and start init */
2230 ret = kzalloc(sizeof(*ret), GFP_KERNEL);
2231 if (!ret)
2232 return ERR_PTR(-ENOMEM);
2234 spin_lock_init(&ret->lock);
2235 ret->irq = irq;
2236 ret->dev = dev;
2237 ret->gadget.ops = &net2272_ops;
2238 ret->gadget.max_speed = USB_SPEED_HIGH;
2240 /* the "gadget" abstracts/virtualizes the controller */
2241 dev_set_name(&ret->gadget.dev, "gadget");
2242 ret->gadget.dev.parent = dev;
2243 ret->gadget.dev.dma_mask = dev->dma_mask;
2244 ret->gadget.dev.release = net2272_gadget_release;
2245 ret->gadget.name = driver_name;
2247 return ret;
2250 static int __devinit
2251 net2272_probe_fin(struct net2272 *dev, unsigned int irqflags)
2253 int ret;
2255 /* See if there... */
2256 if (net2272_present(dev)) {
2257 dev_warn(dev->dev, "2272 not found!\n");
2258 ret = -ENODEV;
2259 goto err;
2262 net2272_usb_reset(dev);
2263 net2272_usb_reinit(dev);
2265 ret = request_irq(dev->irq, net2272_irq, irqflags, driver_name, dev);
2266 if (ret) {
2267 dev_err(dev->dev, "request interrupt %i failed\n", dev->irq);
2268 goto err;
2271 dev->chiprev = net2272_read(dev, CHIPREV_2272);
2273 /* done */
2274 dev_info(dev->dev, "%s\n", driver_desc);
2275 dev_info(dev->dev, "irq %i, mem %p, chip rev %04x, dma %s\n",
2276 dev->irq, dev->base_addr, dev->chiprev,
2277 dma_mode_string());
2278 dev_info(dev->dev, "version: %s\n", driver_vers);
2280 ret = device_register(&dev->gadget.dev);
2281 if (ret)
2282 goto err_irq;
2283 ret = device_create_file(dev->dev, &dev_attr_registers);
2284 if (ret)
2285 goto err_dev_reg;
2287 ret = usb_add_gadget_udc(dev->dev, &dev->gadget);
2288 if (ret)
2289 goto err_add_udc;
2291 return 0;
2293 err_add_udc:
2294 device_remove_file(dev->dev, &dev_attr_registers);
2295 err_dev_reg:
2296 device_unregister(&dev->gadget.dev);
2297 err_irq:
2298 free_irq(dev->irq, dev);
2299 err:
2300 return ret;
2303 #ifdef CONFIG_PCI
2306 * wrap this driver around the specified device, but
2307 * don't respond over USB until a gadget driver binds to us
2310 static int __devinit
2311 net2272_rdk1_probe(struct pci_dev *pdev, struct net2272 *dev)
2313 unsigned long resource, len, tmp;
2314 void __iomem *mem_mapped_addr[4];
2315 int ret, i;
2318 * BAR 0 holds PLX 9054 config registers
2319 * BAR 1 is i/o memory; unused here
2320 * BAR 2 holds EPLD config registers
2321 * BAR 3 holds NET2272 registers
2324 /* Find and map all address spaces */
2325 for (i = 0; i < 4; ++i) {
2326 if (i == 1)
2327 continue; /* BAR1 unused */
2329 resource = pci_resource_start(pdev, i);
2330 len = pci_resource_len(pdev, i);
2332 if (!request_mem_region(resource, len, driver_name)) {
2333 dev_dbg(dev->dev, "controller already in use\n");
2334 ret = -EBUSY;
2335 goto err;
2338 mem_mapped_addr[i] = ioremap_nocache(resource, len);
2339 if (mem_mapped_addr[i] == NULL) {
2340 release_mem_region(resource, len);
2341 dev_dbg(dev->dev, "can't map memory\n");
2342 ret = -EFAULT;
2343 goto err;
2347 dev->rdk1.plx9054_base_addr = mem_mapped_addr[0];
2348 dev->rdk1.epld_base_addr = mem_mapped_addr[2];
2349 dev->base_addr = mem_mapped_addr[3];
2351 /* Set PLX 9054 bus width (16 bits) */
2352 tmp = readl(dev->rdk1.plx9054_base_addr + LBRD1);
2353 writel((tmp & ~(3 << MEMORY_SPACE_LOCAL_BUS_WIDTH)) | W16_BIT,
2354 dev->rdk1.plx9054_base_addr + LBRD1);
2356 /* Enable PLX 9054 Interrupts */
2357 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) |
2358 (1 << PCI_INTERRUPT_ENABLE) |
2359 (1 << LOCAL_INTERRUPT_INPUT_ENABLE),
2360 dev->rdk1.plx9054_base_addr + INTCSR);
2362 writeb((1 << CHANNEL_CLEAR_INTERRUPT | (0 << CHANNEL_ENABLE)),
2363 dev->rdk1.plx9054_base_addr + DMACSR0);
2365 /* reset */
2366 writeb((1 << EPLD_DMA_ENABLE) |
2367 (1 << DMA_CTL_DACK) |
2368 (1 << DMA_TIMEOUT_ENABLE) |
2369 (1 << USER) |
2370 (0 << MPX_MODE) |
2371 (1 << BUSWIDTH) |
2372 (1 << NET2272_RESET),
2373 dev->base_addr + EPLD_IO_CONTROL_REGISTER);
2375 mb();
2376 writeb(readb(dev->base_addr + EPLD_IO_CONTROL_REGISTER) &
2377 ~(1 << NET2272_RESET),
2378 dev->base_addr + EPLD_IO_CONTROL_REGISTER);
2379 udelay(200);
2381 return 0;
2383 err:
2384 while (--i >= 0) {
2385 iounmap(mem_mapped_addr[i]);
2386 release_mem_region(pci_resource_start(pdev, i),
2387 pci_resource_len(pdev, i));
2390 return ret;
2393 static int __devinit
2394 net2272_rdk2_probe(struct pci_dev *pdev, struct net2272 *dev)
2396 unsigned long resource, len;
2397 void __iomem *mem_mapped_addr[2];
2398 int ret, i;
2401 * BAR 0 holds FGPA config registers
2402 * BAR 1 holds NET2272 registers
2405 /* Find and map all address spaces, bar2-3 unused in rdk 2 */
2406 for (i = 0; i < 2; ++i) {
2407 resource = pci_resource_start(pdev, i);
2408 len = pci_resource_len(pdev, i);
2410 if (!request_mem_region(resource, len, driver_name)) {
2411 dev_dbg(dev->dev, "controller already in use\n");
2412 ret = -EBUSY;
2413 goto err;
2416 mem_mapped_addr[i] = ioremap_nocache(resource, len);
2417 if (mem_mapped_addr[i] == NULL) {
2418 release_mem_region(resource, len);
2419 dev_dbg(dev->dev, "can't map memory\n");
2420 ret = -EFAULT;
2421 goto err;
2425 dev->rdk2.fpga_base_addr = mem_mapped_addr[0];
2426 dev->base_addr = mem_mapped_addr[1];
2428 mb();
2429 /* Set 2272 bus width (16 bits) and reset */
2430 writel((1 << CHIP_RESET), dev->rdk2.fpga_base_addr + RDK2_LOCCTLRDK);
2431 udelay(200);
2432 writel((1 << BUS_WIDTH), dev->rdk2.fpga_base_addr + RDK2_LOCCTLRDK);
2433 /* Print fpga version number */
2434 dev_info(dev->dev, "RDK2 FPGA version %08x\n",
2435 readl(dev->rdk2.fpga_base_addr + RDK2_FPGAREV));
2436 /* Enable FPGA Interrupts */
2437 writel((1 << NET2272_PCI_IRQ), dev->rdk2.fpga_base_addr + RDK2_IRQENB);
2439 return 0;
2441 err:
2442 while (--i >= 0) {
2443 iounmap(mem_mapped_addr[i]);
2444 release_mem_region(pci_resource_start(pdev, i),
2445 pci_resource_len(pdev, i));
2448 return ret;
2451 static int __devinit
2452 net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2454 struct net2272 *dev;
2455 int ret;
2457 dev = net2272_probe_init(&pdev->dev, pdev->irq);
2458 if (IS_ERR(dev))
2459 return PTR_ERR(dev);
2460 dev->dev_id = pdev->device;
2462 if (pci_enable_device(pdev) < 0) {
2463 ret = -ENODEV;
2464 goto err_free;
2467 pci_set_master(pdev);
2469 switch (pdev->device) {
2470 case PCI_DEVICE_ID_RDK1: ret = net2272_rdk1_probe(pdev, dev); break;
2471 case PCI_DEVICE_ID_RDK2: ret = net2272_rdk2_probe(pdev, dev); break;
2472 default: BUG();
2474 if (ret)
2475 goto err_pci;
2477 ret = net2272_probe_fin(dev, 0);
2478 if (ret)
2479 goto err_pci;
2481 pci_set_drvdata(pdev, dev);
2483 return 0;
2485 err_pci:
2486 pci_disable_device(pdev);
2487 err_free:
2488 kfree(dev);
2490 return ret;
2493 static void __devexit
2494 net2272_rdk1_remove(struct pci_dev *pdev, struct net2272 *dev)
2496 int i;
2498 /* disable PLX 9054 interrupts */
2499 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2500 ~(1 << PCI_INTERRUPT_ENABLE),
2501 dev->rdk1.plx9054_base_addr + INTCSR);
2503 /* clean up resources allocated during probe() */
2504 iounmap(dev->rdk1.plx9054_base_addr);
2505 iounmap(dev->rdk1.epld_base_addr);
2507 for (i = 0; i < 4; ++i) {
2508 if (i == 1)
2509 continue; /* BAR1 unused */
2510 release_mem_region(pci_resource_start(pdev, i),
2511 pci_resource_len(pdev, i));
2515 static void __devexit
2516 net2272_rdk2_remove(struct pci_dev *pdev, struct net2272 *dev)
2518 int i;
2520 /* disable fpga interrupts
2521 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2522 ~(1 << PCI_INTERRUPT_ENABLE),
2523 dev->rdk1.plx9054_base_addr + INTCSR);
2526 /* clean up resources allocated during probe() */
2527 iounmap(dev->rdk2.fpga_base_addr);
2529 for (i = 0; i < 2; ++i)
2530 release_mem_region(pci_resource_start(pdev, i),
2531 pci_resource_len(pdev, i));
2534 static void __devexit
2535 net2272_pci_remove(struct pci_dev *pdev)
2537 struct net2272 *dev = pci_get_drvdata(pdev);
2539 net2272_remove(dev);
2541 switch (pdev->device) {
2542 case PCI_DEVICE_ID_RDK1: net2272_rdk1_remove(pdev, dev); break;
2543 case PCI_DEVICE_ID_RDK2: net2272_rdk2_remove(pdev, dev); break;
2544 default: BUG();
2547 pci_disable_device(pdev);
2549 kfree(dev);
2552 /* Table of matching PCI IDs */
2553 static struct pci_device_id __devinitdata pci_ids[] = {
2554 { /* RDK 1 card */
2555 .class = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2556 .class_mask = 0,
2557 .vendor = PCI_VENDOR_ID_PLX,
2558 .device = PCI_DEVICE_ID_RDK1,
2559 .subvendor = PCI_ANY_ID,
2560 .subdevice = PCI_ANY_ID,
2562 { /* RDK 2 card */
2563 .class = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2564 .class_mask = 0,
2565 .vendor = PCI_VENDOR_ID_PLX,
2566 .device = PCI_DEVICE_ID_RDK2,
2567 .subvendor = PCI_ANY_ID,
2568 .subdevice = PCI_ANY_ID,
2572 MODULE_DEVICE_TABLE(pci, pci_ids);
2574 static struct pci_driver net2272_pci_driver = {
2575 .name = driver_name,
2576 .id_table = pci_ids,
2578 .probe = net2272_pci_probe,
2579 .remove = __devexit_p(net2272_pci_remove),
2582 static int net2272_pci_register(void)
2584 return pci_register_driver(&net2272_pci_driver);
2587 static void net2272_pci_unregister(void)
2589 pci_unregister_driver(&net2272_pci_driver);
2592 #else
2593 static inline int net2272_pci_register(void) { return 0; }
2594 static inline void net2272_pci_unregister(void) { }
2595 #endif
2597 /*---------------------------------------------------------------------------*/
2599 static int __devinit
2600 net2272_plat_probe(struct platform_device *pdev)
2602 struct net2272 *dev;
2603 int ret;
2604 unsigned int irqflags;
2605 resource_size_t base, len;
2606 struct resource *iomem, *iomem_bus, *irq_res;
2608 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2609 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2610 iomem_bus = platform_get_resource(pdev, IORESOURCE_BUS, 0);
2611 if (!irq_res || !iomem) {
2612 dev_err(&pdev->dev, "must provide irq/base addr");
2613 return -EINVAL;
2616 dev = net2272_probe_init(&pdev->dev, irq_res->start);
2617 if (IS_ERR(dev))
2618 return PTR_ERR(dev);
2620 irqflags = 0;
2621 if (irq_res->flags & IORESOURCE_IRQ_HIGHEDGE)
2622 irqflags |= IRQF_TRIGGER_RISING;
2623 if (irq_res->flags & IORESOURCE_IRQ_LOWEDGE)
2624 irqflags |= IRQF_TRIGGER_FALLING;
2625 if (irq_res->flags & IORESOURCE_IRQ_HIGHLEVEL)
2626 irqflags |= IRQF_TRIGGER_HIGH;
2627 if (irq_res->flags & IORESOURCE_IRQ_LOWLEVEL)
2628 irqflags |= IRQF_TRIGGER_LOW;
2630 base = iomem->start;
2631 len = resource_size(iomem);
2632 if (iomem_bus)
2633 dev->base_shift = iomem_bus->start;
2635 if (!request_mem_region(base, len, driver_name)) {
2636 dev_dbg(dev->dev, "get request memory region!\n");
2637 ret = -EBUSY;
2638 goto err;
2640 dev->base_addr = ioremap_nocache(base, len);
2641 if (!dev->base_addr) {
2642 dev_dbg(dev->dev, "can't map memory\n");
2643 ret = -EFAULT;
2644 goto err_req;
2647 ret = net2272_probe_fin(dev, IRQF_TRIGGER_LOW);
2648 if (ret)
2649 goto err_io;
2651 platform_set_drvdata(pdev, dev);
2652 dev_info(&pdev->dev, "running in 16-bit, %sbyte swap local bus mode\n",
2653 (net2272_read(dev, LOCCTL) & (1 << BYTE_SWAP)) ? "" : "no ");
2655 return 0;
2657 err_io:
2658 iounmap(dev->base_addr);
2659 err_req:
2660 release_mem_region(base, len);
2661 err:
2662 return ret;
2665 static int __devexit
2666 net2272_plat_remove(struct platform_device *pdev)
2668 struct net2272 *dev = platform_get_drvdata(pdev);
2670 net2272_remove(dev);
2672 release_mem_region(pdev->resource[0].start,
2673 resource_size(&pdev->resource[0]));
2675 kfree(dev);
2677 return 0;
2680 static struct platform_driver net2272_plat_driver = {
2681 .probe = net2272_plat_probe,
2682 .remove = __devexit_p(net2272_plat_remove),
2683 .driver = {
2684 .name = driver_name,
2685 .owner = THIS_MODULE,
2687 /* FIXME .suspend, .resume */
2689 MODULE_ALIAS("platform:net2272");
2691 static int __init net2272_init(void)
2693 int ret;
2695 ret = net2272_pci_register();
2696 if (ret)
2697 return ret;
2698 ret = platform_driver_register(&net2272_plat_driver);
2699 if (ret)
2700 goto err_pci;
2701 return ret;
2703 err_pci:
2704 net2272_pci_unregister();
2705 return ret;
2707 module_init(net2272_init);
2709 static void __exit net2272_cleanup(void)
2711 net2272_pci_unregister();
2712 platform_driver_unregister(&net2272_plat_driver);
2714 module_exit(net2272_cleanup);
2716 MODULE_DESCRIPTION(DRIVER_DESC);
2717 MODULE_AUTHOR("PLX Technology, Inc.");
2718 MODULE_LICENSE("GPL");