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>
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/unaligned.h>
49 #define DRIVER_DESC "PLX NET2272 USB Peripheral Controller"
51 static const char driver_name[] = "net2272";
52 static const char driver_vers[] = "2006 October 17/mainline";
53 static const char driver_desc[] = DRIVER_DESC;
55 static const char ep0name[] = "ep0";
56 static const char * const ep_name[] = {
58 "ep-a", "ep-b", "ep-c",
61 #ifdef CONFIG_USB_NET2272_DMA
63 * use_dma: the NET2272 can use an external DMA controller.
64 * Note that since there is no generic DMA api, some functions,
65 * notably request_dma, start_dma, and cancel_dma will need to be
66 * modified for your platform's particular dma controller.
68 * If use_dma is disabled, pio will be used instead.
70 static bool use_dma = 0;
71 module_param(use_dma, bool, 0644);
74 * dma_ep: selects the endpoint for use with dma (1=ep-a, 2=ep-b)
75 * The NET2272 can only use dma for a single endpoint at a time.
76 * At some point this could be modified to allow either endpoint
77 * to take control of dma as it becomes available.
79 * Note that DMA should not be used on OUT endpoints unless it can
80 * be guaranteed that no short packets will arrive on an IN endpoint
81 * while the DMA operation is pending. Otherwise the OUT DMA will
82 * terminate prematurely (See NET2272 Errata 630-0213-0101)
84 static ushort dma_ep = 1;
85 module_param(dma_ep, ushort, 0644);
88 * dma_mode: net2272 dma mode setting (see LOCCTL1 definiton):
89 * mode 0 == Slow DREQ mode
90 * mode 1 == Fast DREQ mode
91 * mode 2 == Burst mode
93 static ushort dma_mode = 2;
94 module_param(dma_mode, ushort, 0644);
102 * fifo_mode: net2272 buffer configuration:
103 * mode 0 == ep-{a,b,c} 512db each
104 * mode 1 == ep-a 1k, ep-{b,c} 512db
105 * mode 2 == ep-a 1k, ep-b 1k, ep-c 512db
106 * mode 3 == ep-a 1k, ep-b disabled, ep-c 512db
108 static ushort fifo_mode = 0;
109 module_param(fifo_mode, ushort, 0644);
112 * enable_suspend: When enabled, the driver will respond to
113 * USB suspend requests by powering down the NET2272. Otherwise,
114 * USB suspend requests will be ignored. This is acceptible for
115 * self-powered devices. For bus powered devices set this to 1.
117 static ushort enable_suspend = 0;
118 module_param(enable_suspend, ushort, 0644);
120 static void assert_out_naking(struct net2272_ep *ep, const char *where)
128 tmp = net2272_ep_read(ep, EP_STAT0);
129 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
130 dev_dbg(ep->dev->dev, "%s %s %02x !NAK\n",
131 ep->ep.name, where, tmp);
132 net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
135 #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep, __func__)
137 static void stop_out_naking(struct net2272_ep *ep)
139 u8 tmp = net2272_ep_read(ep, EP_STAT0);
141 if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
142 net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
145 #define PIPEDIR(bAddress) (usb_pipein(bAddress) ? "in" : "out")
147 static char *type_string(u8 bmAttributes)
149 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
150 case USB_ENDPOINT_XFER_BULK: return "bulk";
151 case USB_ENDPOINT_XFER_ISOC: return "iso";
152 case USB_ENDPOINT_XFER_INT: return "intr";
153 default: return "control";
157 static char *buf_state_string(unsigned state)
160 case BUFF_FREE: return "free";
161 case BUFF_VALID: return "valid";
162 case BUFF_LCL: return "local";
163 case BUFF_USB: return "usb";
164 default: return "unknown";
168 static char *dma_mode_string(void)
173 case 0: return "SLOW DREQ";
174 case 1: return "FAST DREQ";
175 case 2: return "BURST";
176 default: return "invalid";
180 static void net2272_dequeue_all(struct net2272_ep *);
181 static int net2272_kick_dma(struct net2272_ep *, struct net2272_request *);
182 static int net2272_fifo_status(struct usb_ep *);
184 static struct usb_ep_ops net2272_ep_ops;
186 /*---------------------------------------------------------------------------*/
189 net2272_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
192 struct net2272_ep *ep;
197 ep = container_of(_ep, struct net2272_ep, ep);
198 if (!_ep || !desc || ep->desc || _ep->name == ep0name
199 || desc->bDescriptorType != USB_DT_ENDPOINT)
202 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
205 max = usb_endpoint_maxp(desc) & 0x1fff;
207 spin_lock_irqsave(&dev->lock, flags);
208 _ep->maxpacket = max & 0x7fff;
211 /* net2272_ep_reset() has already been called */
215 /* set speed-dependent max packet */
216 net2272_ep_write(ep, EP_MAXPKT0, max & 0xff);
217 net2272_ep_write(ep, EP_MAXPKT1, (max & 0xff00) >> 8);
219 /* set type, direction, address; reset fifo counters */
220 net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
221 tmp = usb_endpoint_type(desc);
222 if (usb_endpoint_xfer_bulk(desc)) {
223 /* catch some particularly blatant driver bugs */
224 if ((dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
225 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
226 spin_unlock_irqrestore(&dev->lock, flags);
230 ep->is_iso = usb_endpoint_xfer_isoc(desc) ? 1 : 0;
231 tmp <<= ENDPOINT_TYPE;
232 tmp |= ((desc->bEndpointAddress & 0x0f) << ENDPOINT_NUMBER);
233 tmp |= usb_endpoint_dir_in(desc) << ENDPOINT_DIRECTION;
234 tmp |= (1 << ENDPOINT_ENABLE);
236 /* for OUT transfers, block the rx fifo until a read is posted */
237 ep->is_in = usb_endpoint_dir_in(desc);
239 net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
241 net2272_ep_write(ep, EP_CFG, tmp);
244 tmp = (1 << ep->num) | net2272_read(dev, IRQENB0);
245 net2272_write(dev, IRQENB0, tmp);
247 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
248 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
249 | net2272_ep_read(ep, EP_IRQENB);
250 net2272_ep_write(ep, EP_IRQENB, tmp);
252 tmp = desc->bEndpointAddress;
253 dev_dbg(dev->dev, "enabled %s (ep%d%s-%s) max %04x cfg %02x\n",
254 _ep->name, tmp & 0x0f, PIPEDIR(tmp),
255 type_string(desc->bmAttributes), max,
256 net2272_ep_read(ep, EP_CFG));
258 spin_unlock_irqrestore(&dev->lock, flags);
262 static void net2272_ep_reset(struct net2272_ep *ep)
267 INIT_LIST_HEAD(&ep->queue);
269 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
270 ep->ep.ops = &net2272_ep_ops;
272 /* disable irqs, endpoint */
273 net2272_ep_write(ep, EP_IRQENB, 0);
275 /* init to our chosen defaults, notably so that we NAK OUT
276 * packets until the driver queues a read.
278 tmp = (1 << NAK_OUT_PACKETS_MODE) | (1 << ALT_NAK_OUT_PACKETS);
279 net2272_ep_write(ep, EP_RSPSET, tmp);
281 tmp = (1 << INTERRUPT_MODE) | (1 << HIDE_STATUS_PHASE);
283 tmp |= (1 << ENDPOINT_TOGGLE) | (1 << ENDPOINT_HALT);
285 net2272_ep_write(ep, EP_RSPCLR, tmp);
287 /* scrub most status bits, and flush any fifo state */
288 net2272_ep_write(ep, EP_STAT0,
289 (1 << DATA_IN_TOKEN_INTERRUPT)
290 | (1 << DATA_OUT_TOKEN_INTERRUPT)
291 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
292 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
293 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT));
295 net2272_ep_write(ep, EP_STAT1,
297 | (1 << USB_OUT_ACK_SENT)
298 | (1 << USB_OUT_NAK_SENT)
299 | (1 << USB_IN_ACK_RCVD)
300 | (1 << USB_IN_NAK_SENT)
301 | (1 << USB_STALL_SENT)
302 | (1 << LOCAL_OUT_ZLP)
303 | (1 << BUFFER_FLUSH));
305 /* fifo size is handled seperately */
308 static int net2272_disable(struct usb_ep *_ep)
310 struct net2272_ep *ep;
313 ep = container_of(_ep, struct net2272_ep, ep);
314 if (!_ep || !ep->desc || _ep->name == ep0name)
317 spin_lock_irqsave(&ep->dev->lock, flags);
318 net2272_dequeue_all(ep);
319 net2272_ep_reset(ep);
321 dev_vdbg(ep->dev->dev, "disabled %s\n", _ep->name);
323 spin_unlock_irqrestore(&ep->dev->lock, flags);
327 /*---------------------------------------------------------------------------*/
329 static struct usb_request *
330 net2272_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
332 struct net2272_request *req;
337 req = kzalloc(sizeof(*req), gfp_flags);
341 INIT_LIST_HEAD(&req->queue);
347 net2272_free_request(struct usb_ep *_ep, struct usb_request *_req)
349 struct net2272_request *req;
354 req = container_of(_req, struct net2272_request, req);
355 WARN_ON(!list_empty(&req->queue));
360 net2272_done(struct net2272_ep *ep, struct net2272_request *req, int status)
363 unsigned stopped = ep->stopped;
366 if (ep->dev->protocol_stall) {
373 list_del_init(&req->queue);
375 if (req->req.status == -EINPROGRESS)
376 req->req.status = status;
378 status = req->req.status;
381 if (use_dma && ep->dma)
382 usb_gadget_unmap_request(&dev->gadget, &req->req,
385 if (status && status != -ESHUTDOWN)
386 dev_vdbg(dev->dev, "complete %s req %p stat %d len %u/%u buf %p\n",
387 ep->ep.name, &req->req, status,
388 req->req.actual, req->req.length, req->req.buf);
390 /* don't modify queue heads during completion callback */
392 spin_unlock(&dev->lock);
393 usb_gadget_giveback_request(&ep->ep, &req->req);
394 spin_lock(&dev->lock);
395 ep->stopped = stopped;
399 net2272_write_packet(struct net2272_ep *ep, u8 *buf,
400 struct net2272_request *req, unsigned max)
402 u16 __iomem *ep_data = net2272_reg_addr(ep->dev, EP_DATA);
404 unsigned length, count;
407 length = min(req->req.length - req->req.actual, max);
408 req->req.actual += length;
410 dev_vdbg(ep->dev->dev, "write packet %s req %p max %u len %u avail %u\n",
411 ep->ep.name, req, max, length,
412 (net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0));
417 while (likely(count >= 2)) {
418 /* no byte-swap required; chip endian set during init */
419 writew(*bufp++, ep_data);
424 /* write final byte by placing the NET2272 into 8-bit mode */
425 if (unlikely(count)) {
426 tmp = net2272_read(ep->dev, LOCCTL);
427 net2272_write(ep->dev, LOCCTL, tmp & ~(1 << DATA_WIDTH));
428 writeb(*buf, ep_data);
429 net2272_write(ep->dev, LOCCTL, tmp);
434 /* returns: 0: still running, 1: completed, negative: errno */
436 net2272_write_fifo(struct net2272_ep *ep, struct net2272_request *req)
442 dev_vdbg(ep->dev->dev, "write_fifo %s actual %d len %d\n",
443 ep->ep.name, req->req.actual, req->req.length);
446 * Keep loading the endpoint until the final packet is loaded,
447 * or the endpoint buffer is full.
451 * Clear interrupt status
452 * - Packet Transmitted interrupt will become set again when the
453 * host successfully takes another packet
455 net2272_ep_write(ep, EP_STAT0, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT));
456 while (!(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_FULL))) {
457 buf = req->req.buf + req->req.actual;
461 net2272_ep_read(ep, EP_STAT0);
463 max = (net2272_ep_read(ep, EP_AVAIL1) << 8) |
464 (net2272_ep_read(ep, EP_AVAIL0));
466 if (max < ep->ep.maxpacket)
467 max = (net2272_ep_read(ep, EP_AVAIL1) << 8)
468 | (net2272_ep_read(ep, EP_AVAIL0));
470 count = net2272_write_packet(ep, buf, req, max);
471 /* see if we are done */
472 if (req->req.length == req->req.actual) {
473 /* validate short or zlp packet */
474 if (count < ep->ep.maxpacket)
475 set_fifo_bytecount(ep, 0);
476 net2272_done(ep, req, 0);
478 if (!list_empty(&ep->queue)) {
479 req = list_entry(ep->queue.next,
480 struct net2272_request,
482 status = net2272_kick_dma(ep, req);
485 if ((net2272_ep_read(ep, EP_STAT0)
486 & (1 << BUFFER_EMPTY)))
491 net2272_ep_write(ep, EP_STAT0, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT));
497 net2272_out_flush(struct net2272_ep *ep)
499 ASSERT_OUT_NAKING(ep);
501 net2272_ep_write(ep, EP_STAT0, (1 << DATA_OUT_TOKEN_INTERRUPT)
502 | (1 << DATA_PACKET_RECEIVED_INTERRUPT));
503 net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
507 net2272_read_packet(struct net2272_ep *ep, u8 *buf,
508 struct net2272_request *req, unsigned avail)
510 u16 __iomem *ep_data = net2272_reg_addr(ep->dev, EP_DATA);
514 req->req.actual += avail;
516 dev_vdbg(ep->dev->dev, "read packet %s req %p len %u avail %u\n",
517 ep->ep.name, req, avail,
518 (net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0));
520 is_short = (avail < ep->ep.maxpacket);
522 if (unlikely(avail == 0)) {
523 /* remove any zlp from the buffer */
524 (void)readw(ep_data);
528 /* Ensure we get the final byte */
529 if (unlikely(avail % 2))
534 *bufp++ = readw(ep_data);
539 * To avoid false endpoint available race condition must read
540 * ep stat0 twice in the case of a short transfer
542 if (net2272_ep_read(ep, EP_STAT0) & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT))
543 net2272_ep_read(ep, EP_STAT0);
549 net2272_read_fifo(struct net2272_ep *ep, struct net2272_request *req)
558 dev_vdbg(ep->dev->dev, "read_fifo %s actual %d len %d\n",
559 ep->ep.name, req->req.actual, req->req.length);
563 buf = req->req.buf + req->req.actual;
566 count = (net2272_ep_read(ep, EP_AVAIL1) << 8)
567 | net2272_ep_read(ep, EP_AVAIL0);
569 net2272_ep_write(ep, EP_STAT0,
570 (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT) |
571 (1 << DATA_PACKET_RECEIVED_INTERRUPT));
573 tmp = req->req.length - req->req.actual;
576 if ((tmp % ep->ep.maxpacket) != 0) {
577 dev_err(ep->dev->dev,
578 "%s out fifo %d bytes, expected %d\n",
579 ep->ep.name, count, tmp);
582 count = (tmp > 0) ? tmp : 0;
585 is_short = net2272_read_packet(ep, buf, req, count);
588 if (unlikely(cleanup || is_short ||
589 ((req->req.actual == req->req.length)
590 && !req->req.zero))) {
593 net2272_out_flush(ep);
594 net2272_done(ep, req, -EOVERFLOW);
596 net2272_done(ep, req, 0);
598 /* re-initialize endpoint transfer registers
599 * otherwise they may result in erroneous pre-validation
600 * for subsequent control reads
602 if (unlikely(ep->num == 0)) {
603 net2272_ep_write(ep, EP_TRANSFER2, 0);
604 net2272_ep_write(ep, EP_TRANSFER1, 0);
605 net2272_ep_write(ep, EP_TRANSFER0, 0);
608 if (!list_empty(&ep->queue)) {
609 req = list_entry(ep->queue.next,
610 struct net2272_request, queue);
611 status = net2272_kick_dma(ep, req);
613 !(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_EMPTY)))
618 } while (!(net2272_ep_read(ep, EP_STAT0) & (1 << BUFFER_EMPTY)));
624 net2272_pio_advance(struct net2272_ep *ep)
626 struct net2272_request *req;
628 if (unlikely(list_empty(&ep->queue)))
631 req = list_entry(ep->queue.next, struct net2272_request, queue);
632 (ep->is_in ? net2272_write_fifo : net2272_read_fifo)(ep, req);
635 /* returns 0 on success, else negative errno */
637 net2272_request_dma(struct net2272 *dev, unsigned ep, u32 buf,
638 unsigned len, unsigned dir)
640 dev_vdbg(dev->dev, "request_dma ep %d buf %08x len %d dir %d\n",
643 /* The NET2272 only supports a single dma channel */
647 * EP_TRANSFER (used to determine the number of bytes received
648 * in an OUT transfer) is 24 bits wide; don't ask for more than that.
650 if ((dir == 1) && (len > 0x1000000))
655 /* initialize platform's dma */
657 /* NET2272 addr, buffer addr, length, etc. */
658 switch (dev->dev_id) {
659 case PCI_DEVICE_ID_RDK1:
660 /* Setup PLX 9054 DMA mode */
661 writel((1 << LOCAL_BUS_WIDTH) |
662 (1 << TA_READY_INPUT_ENABLE) |
663 (0 << LOCAL_BURST_ENABLE) |
664 (1 << DONE_INTERRUPT_ENABLE) |
665 (1 << LOCAL_ADDRESSING_MODE) |
667 (1 << DMA_EOT_ENABLE) |
668 (1 << FAST_SLOW_TERMINATE_MODE_SELECT) |
669 (1 << DMA_CHANNEL_INTERRUPT_SELECT),
670 dev->rdk1.plx9054_base_addr + DMAMODE0);
672 writel(0x100000, dev->rdk1.plx9054_base_addr + DMALADR0);
673 writel(buf, dev->rdk1.plx9054_base_addr + DMAPADR0);
674 writel(len, dev->rdk1.plx9054_base_addr + DMASIZ0);
675 writel((dir << DIRECTION_OF_TRANSFER) |
676 (1 << INTERRUPT_AFTER_TERMINAL_COUNT),
677 dev->rdk1.plx9054_base_addr + DMADPR0);
678 writel((1 << LOCAL_DMA_CHANNEL_0_INTERRUPT_ENABLE) |
679 readl(dev->rdk1.plx9054_base_addr + INTCSR),
680 dev->rdk1.plx9054_base_addr + INTCSR);
686 net2272_write(dev, DMAREQ,
687 (0 << DMA_BUFFER_VALID) |
688 (1 << DMA_REQUEST_ENABLE) |
689 (1 << DMA_CONTROL_DACK) |
690 (dev->dma_eot_polarity << EOT_POLARITY) |
691 (dev->dma_dack_polarity << DACK_POLARITY) |
692 (dev->dma_dreq_polarity << DREQ_POLARITY) |
693 ((ep >> 1) << DMA_ENDPOINT_SELECT));
695 (void) net2272_read(dev, SCRATCH);
701 net2272_start_dma(struct net2272 *dev)
703 /* start platform's dma controller */
705 switch (dev->dev_id) {
706 case PCI_DEVICE_ID_RDK1:
707 writeb((1 << CHANNEL_ENABLE) | (1 << CHANNEL_START),
708 dev->rdk1.plx9054_base_addr + DMACSR0);
714 /* returns 0 on success, else negative errno */
716 net2272_kick_dma(struct net2272_ep *ep, struct net2272_request *req)
721 if (!use_dma || (ep->num < 1) || (ep->num > 2) || !ep->dma)
724 /* don't use dma for odd-length transfers
725 * otherwise, we'd need to deal with the last byte with pio
727 if (req->req.length & 1)
730 dev_vdbg(ep->dev->dev, "kick_dma %s req %p dma %08llx\n",
731 ep->ep.name, req, (unsigned long long) req->req.dma);
733 net2272_ep_write(ep, EP_RSPSET, 1 << ALT_NAK_OUT_PACKETS);
735 /* The NET2272 can only use DMA on one endpoint at a time */
736 if (ep->dev->dma_busy)
739 /* Make sure we only DMA an even number of bytes (we'll use
740 * pio to complete the transfer)
742 size = req->req.length;
745 /* device-to-host transfer */
747 /* initialize platform's dma controller */
748 if (net2272_request_dma(ep->dev, ep->num, req->req.dma, size, 0))
749 /* unable to obtain DMA channel; return error and use pio mode */
751 req->req.actual += size;
753 /* host-to-device transfer */
755 tmp = net2272_ep_read(ep, EP_STAT0);
757 /* initialize platform's dma controller */
758 if (net2272_request_dma(ep->dev, ep->num, req->req.dma, size, 1))
759 /* unable to obtain DMA channel; return error and use pio mode */
762 if (!(tmp & (1 << BUFFER_EMPTY)))
768 /* allow the endpoint's buffer to fill */
769 net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
771 /* this transfer completed and data's already in the fifo
772 * return error so pio gets used.
774 if (tmp & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
777 net2272_write(ep->dev, DMAREQ,
778 (0 << DMA_BUFFER_VALID) |
779 (0 << DMA_REQUEST_ENABLE) |
780 (1 << DMA_CONTROL_DACK) |
781 (ep->dev->dma_eot_polarity << EOT_POLARITY) |
782 (ep->dev->dma_dack_polarity << DACK_POLARITY) |
783 (ep->dev->dma_dreq_polarity << DREQ_POLARITY) |
784 ((ep->num >> 1) << DMA_ENDPOINT_SELECT));
790 /* Don't use per-packet interrupts: use dma interrupts only */
791 net2272_ep_write(ep, EP_IRQENB, 0);
793 net2272_start_dma(ep->dev);
798 static void net2272_cancel_dma(struct net2272 *dev)
801 switch (dev->dev_id) {
802 case PCI_DEVICE_ID_RDK1:
803 writeb(0, dev->rdk1.plx9054_base_addr + DMACSR0);
804 writeb(1 << CHANNEL_ABORT, dev->rdk1.plx9054_base_addr + DMACSR0);
805 while (!(readb(dev->rdk1.plx9054_base_addr + DMACSR0) &
806 (1 << CHANNEL_DONE)))
807 continue; /* wait for dma to stabalize */
809 /* dma abort generates an interrupt */
810 writeb(1 << CHANNEL_CLEAR_INTERRUPT,
811 dev->rdk1.plx9054_base_addr + DMACSR0);
819 /*---------------------------------------------------------------------------*/
822 net2272_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
824 struct net2272_request *req;
825 struct net2272_ep *ep;
831 req = container_of(_req, struct net2272_request, req);
832 if (!_req || !_req->complete || !_req->buf
833 || !list_empty(&req->queue))
835 ep = container_of(_ep, struct net2272_ep, ep);
836 if (!_ep || (!ep->desc && ep->num != 0))
839 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
842 /* set up dma mapping in case the caller didn't */
843 if (use_dma && ep->dma) {
844 status = usb_gadget_map_request(&dev->gadget, _req,
850 dev_vdbg(dev->dev, "%s queue req %p, len %d buf %p dma %08llx %s\n",
851 _ep->name, _req, _req->length, _req->buf,
852 (unsigned long long) _req->dma, _req->zero ? "zero" : "!zero");
854 spin_lock_irqsave(&dev->lock, flags);
856 _req->status = -EINPROGRESS;
859 /* kickstart this i/o queue? */
860 if (list_empty(&ep->queue) && !ep->stopped) {
861 /* maybe there's no control data, just status ack */
862 if (ep->num == 0 && _req->length == 0) {
863 net2272_done(ep, req, 0);
864 dev_vdbg(dev->dev, "%s status ack\n", ep->ep.name);
868 /* Return zlp, don't let it block subsequent packets */
869 s = net2272_ep_read(ep, EP_STAT0);
870 if (s & (1 << BUFFER_EMPTY)) {
871 /* Buffer is empty check for a blocking zlp, handle it */
872 if ((s & (1 << NAK_OUT_PACKETS)) &&
873 net2272_ep_read(ep, EP_STAT1) & (1 << LOCAL_OUT_ZLP)) {
874 dev_dbg(dev->dev, "WARNING: returning ZLP short packet termination!\n");
876 * Request is going to terminate with a short packet ...
877 * hope the client is ready for it!
879 status = net2272_read_fifo(ep, req);
880 /* clear short packet naking */
881 net2272_ep_write(ep, EP_STAT0, (1 << NAK_OUT_PACKETS));
887 status = net2272_kick_dma(ep, req);
890 /* dma failed (most likely in use by another endpoint)
896 status = net2272_write_fifo(ep, req);
898 s = net2272_ep_read(ep, EP_STAT0);
899 if ((s & (1 << BUFFER_EMPTY)) == 0)
900 status = net2272_read_fifo(ep, req);
903 if (unlikely(status != 0)) {
911 list_add_tail(&req->queue, &ep->queue);
913 if (likely(!list_empty(&ep->queue)))
914 net2272_ep_write(ep, EP_RSPCLR, 1 << ALT_NAK_OUT_PACKETS);
916 spin_unlock_irqrestore(&dev->lock, flags);
921 /* dequeue ALL requests */
923 net2272_dequeue_all(struct net2272_ep *ep)
925 struct net2272_request *req;
927 /* called with spinlock held */
930 while (!list_empty(&ep->queue)) {
931 req = list_entry(ep->queue.next,
932 struct net2272_request,
934 net2272_done(ep, req, -ESHUTDOWN);
938 /* dequeue JUST ONE request */
940 net2272_dequeue(struct usb_ep *_ep, struct usb_request *_req)
942 struct net2272_ep *ep;
943 struct net2272_request *req;
947 ep = container_of(_ep, struct net2272_ep, ep);
948 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
951 spin_lock_irqsave(&ep->dev->lock, flags);
952 stopped = ep->stopped;
955 /* make sure it's still queued on this endpoint */
956 list_for_each_entry(req, &ep->queue, queue) {
957 if (&req->req == _req)
960 if (&req->req != _req) {
961 ep->stopped = stopped;
962 spin_unlock_irqrestore(&ep->dev->lock, flags);
966 /* queue head may be partially complete */
967 if (ep->queue.next == &req->queue) {
968 dev_dbg(ep->dev->dev, "unlink (%s) pio\n", _ep->name);
969 net2272_done(ep, req, -ECONNRESET);
972 ep->stopped = stopped;
974 spin_unlock_irqrestore(&ep->dev->lock, flags);
978 /*---------------------------------------------------------------------------*/
981 net2272_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
983 struct net2272_ep *ep;
987 ep = container_of(_ep, struct net2272_ep, ep);
988 if (!_ep || (!ep->desc && ep->num != 0))
990 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
992 if (ep->desc /* not ep0 */ && usb_endpoint_xfer_isoc(ep->desc))
995 spin_lock_irqsave(&ep->dev->lock, flags);
996 if (!list_empty(&ep->queue))
998 else if (ep->is_in && value && net2272_fifo_status(_ep) != 0)
1001 dev_vdbg(ep->dev->dev, "%s %s %s\n", _ep->name,
1002 value ? "set" : "clear",
1003 wedged ? "wedge" : "halt");
1007 ep->dev->protocol_stall = 1;
1017 spin_unlock_irqrestore(&ep->dev->lock, flags);
1023 net2272_set_halt(struct usb_ep *_ep, int value)
1025 return net2272_set_halt_and_wedge(_ep, value, 0);
1029 net2272_set_wedge(struct usb_ep *_ep)
1031 if (!_ep || _ep->name == ep0name)
1033 return net2272_set_halt_and_wedge(_ep, 1, 1);
1037 net2272_fifo_status(struct usb_ep *_ep)
1039 struct net2272_ep *ep;
1042 ep = container_of(_ep, struct net2272_ep, ep);
1043 if (!_ep || (!ep->desc && ep->num != 0))
1045 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1048 avail = net2272_ep_read(ep, EP_AVAIL1) << 8;
1049 avail |= net2272_ep_read(ep, EP_AVAIL0);
1050 if (avail > ep->fifo_size)
1053 avail = ep->fifo_size - avail;
1058 net2272_fifo_flush(struct usb_ep *_ep)
1060 struct net2272_ep *ep;
1062 ep = container_of(_ep, struct net2272_ep, ep);
1063 if (!_ep || (!ep->desc && ep->num != 0))
1065 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1068 net2272_ep_write(ep, EP_STAT1, 1 << BUFFER_FLUSH);
1071 static struct usb_ep_ops net2272_ep_ops = {
1072 .enable = net2272_enable,
1073 .disable = net2272_disable,
1075 .alloc_request = net2272_alloc_request,
1076 .free_request = net2272_free_request,
1078 .queue = net2272_queue,
1079 .dequeue = net2272_dequeue,
1081 .set_halt = net2272_set_halt,
1082 .set_wedge = net2272_set_wedge,
1083 .fifo_status = net2272_fifo_status,
1084 .fifo_flush = net2272_fifo_flush,
1087 /*---------------------------------------------------------------------------*/
1090 net2272_get_frame(struct usb_gadget *_gadget)
1092 struct net2272 *dev;
1093 unsigned long flags;
1098 dev = container_of(_gadget, struct net2272, gadget);
1099 spin_lock_irqsave(&dev->lock, flags);
1101 ret = net2272_read(dev, FRAME1) << 8;
1102 ret |= net2272_read(dev, FRAME0);
1104 spin_unlock_irqrestore(&dev->lock, flags);
1109 net2272_wakeup(struct usb_gadget *_gadget)
1111 struct net2272 *dev;
1113 unsigned long flags;
1117 dev = container_of(_gadget, struct net2272, gadget);
1119 spin_lock_irqsave(&dev->lock, flags);
1120 tmp = net2272_read(dev, USBCTL0);
1121 if (tmp & (1 << IO_WAKEUP_ENABLE))
1122 net2272_write(dev, USBCTL1, (1 << GENERATE_RESUME));
1124 spin_unlock_irqrestore(&dev->lock, flags);
1130 net2272_set_selfpowered(struct usb_gadget *_gadget, int value)
1135 _gadget->is_selfpowered = (value != 0);
1141 net2272_pullup(struct usb_gadget *_gadget, int is_on)
1143 struct net2272 *dev;
1145 unsigned long flags;
1149 dev = container_of(_gadget, struct net2272, gadget);
1151 spin_lock_irqsave(&dev->lock, flags);
1152 tmp = net2272_read(dev, USBCTL0);
1153 dev->softconnect = (is_on != 0);
1155 tmp |= (1 << USB_DETECT_ENABLE);
1157 tmp &= ~(1 << USB_DETECT_ENABLE);
1158 net2272_write(dev, USBCTL0, tmp);
1159 spin_unlock_irqrestore(&dev->lock, flags);
1164 static int net2272_start(struct usb_gadget *_gadget,
1165 struct usb_gadget_driver *driver);
1166 static int net2272_stop(struct usb_gadget *_gadget);
1168 static const struct usb_gadget_ops net2272_ops = {
1169 .get_frame = net2272_get_frame,
1170 .wakeup = net2272_wakeup,
1171 .set_selfpowered = net2272_set_selfpowered,
1172 .pullup = net2272_pullup,
1173 .udc_start = net2272_start,
1174 .udc_stop = net2272_stop,
1177 /*---------------------------------------------------------------------------*/
1180 registers_show(struct device *_dev, struct device_attribute *attr, char *buf)
1182 struct net2272 *dev;
1185 unsigned long flags;
1190 dev = dev_get_drvdata(_dev);
1193 spin_lock_irqsave(&dev->lock, flags);
1196 s = dev->driver->driver.name;
1200 /* Main Control Registers */
1201 t = scnprintf(next, size, "%s version %s,"
1202 "chiprev %02x, locctl %02x\n"
1203 "irqenb0 %02x irqenb1 %02x "
1204 "irqstat0 %02x irqstat1 %02x\n",
1205 driver_name, driver_vers, dev->chiprev,
1206 net2272_read(dev, LOCCTL),
1207 net2272_read(dev, IRQENB0),
1208 net2272_read(dev, IRQENB1),
1209 net2272_read(dev, IRQSTAT0),
1210 net2272_read(dev, IRQSTAT1));
1215 t1 = net2272_read(dev, DMAREQ);
1216 t = scnprintf(next, size, "\ndmareq %02x: %s %s%s%s%s\n",
1217 t1, ep_name[(t1 & 0x01) + 1],
1218 t1 & (1 << DMA_CONTROL_DACK) ? "dack " : "",
1219 t1 & (1 << DMA_REQUEST_ENABLE) ? "reqenb " : "",
1220 t1 & (1 << DMA_REQUEST) ? "req " : "",
1221 t1 & (1 << DMA_BUFFER_VALID) ? "valid " : "");
1225 /* USB Control Registers */
1226 t1 = net2272_read(dev, USBCTL1);
1227 if (t1 & (1 << VBUS_PIN)) {
1228 if (t1 & (1 << USB_HIGH_SPEED))
1230 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1236 t = scnprintf(next, size,
1237 "usbctl0 %02x usbctl1 %02x addr 0x%02x (%s)\n",
1238 net2272_read(dev, USBCTL0), t1,
1239 net2272_read(dev, OURADDR), s);
1243 /* Endpoint Registers */
1244 for (i = 0; i < 4; ++i) {
1245 struct net2272_ep *ep;
1251 t1 = net2272_ep_read(ep, EP_CFG);
1252 t2 = net2272_ep_read(ep, EP_RSPSET);
1253 t = scnprintf(next, size,
1254 "\n%s\tcfg %02x rsp (%02x) %s%s%s%s%s%s%s%s"
1256 ep->ep.name, t1, t2,
1257 (t2 & (1 << ALT_NAK_OUT_PACKETS)) ? "NAK " : "",
1258 (t2 & (1 << HIDE_STATUS_PHASE)) ? "hide " : "",
1259 (t2 & (1 << AUTOVALIDATE)) ? "auto " : "",
1260 (t2 & (1 << INTERRUPT_MODE)) ? "interrupt " : "",
1261 (t2 & (1 << CONTROL_STATUS_PHASE_HANDSHAKE)) ? "status " : "",
1262 (t2 & (1 << NAK_OUT_PACKETS_MODE)) ? "NAKmode " : "",
1263 (t2 & (1 << ENDPOINT_TOGGLE)) ? "DATA1 " : "DATA0 ",
1264 (t2 & (1 << ENDPOINT_HALT)) ? "HALT " : "",
1265 net2272_ep_read(ep, EP_IRQENB));
1269 t = scnprintf(next, size,
1270 "\tstat0 %02x stat1 %02x avail %04x "
1272 net2272_ep_read(ep, EP_STAT0),
1273 net2272_ep_read(ep, EP_STAT1),
1274 (net2272_ep_read(ep, EP_AVAIL1) << 8) | net2272_ep_read(ep, EP_AVAIL0),
1276 ep->is_in ? "in" : "out",
1277 type_string(t1 >> 5),
1278 ep->stopped ? "*" : "");
1282 t = scnprintf(next, size,
1283 "\tep_transfer %06x\n",
1284 ((net2272_ep_read(ep, EP_TRANSFER2) & 0xff) << 16) |
1285 ((net2272_ep_read(ep, EP_TRANSFER1) & 0xff) << 8) |
1286 ((net2272_ep_read(ep, EP_TRANSFER0) & 0xff)));
1290 t1 = net2272_ep_read(ep, EP_BUFF_STATES) & 0x03;
1291 t2 = (net2272_ep_read(ep, EP_BUFF_STATES) >> 2) & 0x03;
1292 t = scnprintf(next, size,
1293 "\tbuf-a %s buf-b %s\n",
1294 buf_state_string(t1),
1295 buf_state_string(t2));
1300 spin_unlock_irqrestore(&dev->lock, flags);
1302 return PAGE_SIZE - size;
1304 static DEVICE_ATTR_RO(registers);
1306 /*---------------------------------------------------------------------------*/
1309 net2272_set_fifo_mode(struct net2272 *dev, int mode)
1313 tmp = net2272_read(dev, LOCCTL) & 0x3f;
1315 net2272_write(dev, LOCCTL, tmp);
1317 INIT_LIST_HEAD(&dev->gadget.ep_list);
1319 /* always ep-a, ep-c ... maybe not ep-b */
1320 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1324 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1325 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 512;
1328 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1329 dev->ep[1].fifo_size = 1024;
1330 dev->ep[2].fifo_size = 512;
1333 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1334 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
1337 dev->ep[1].fifo_size = 1024;
1341 /* ep-c is always 2 512 byte buffers */
1342 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1343 dev->ep[3].fifo_size = 512;
1346 /*---------------------------------------------------------------------------*/
1349 net2272_usb_reset(struct net2272 *dev)
1351 dev->gadget.speed = USB_SPEED_UNKNOWN;
1353 net2272_cancel_dma(dev);
1355 net2272_write(dev, IRQENB0, 0);
1356 net2272_write(dev, IRQENB1, 0);
1358 /* clear irq state */
1359 net2272_write(dev, IRQSTAT0, 0xff);
1360 net2272_write(dev, IRQSTAT1, ~(1 << SUSPEND_REQUEST_INTERRUPT));
1362 net2272_write(dev, DMAREQ,
1363 (0 << DMA_BUFFER_VALID) |
1364 (0 << DMA_REQUEST_ENABLE) |
1365 (1 << DMA_CONTROL_DACK) |
1366 (dev->dma_eot_polarity << EOT_POLARITY) |
1367 (dev->dma_dack_polarity << DACK_POLARITY) |
1368 (dev->dma_dreq_polarity << DREQ_POLARITY) |
1369 ((dma_ep >> 1) << DMA_ENDPOINT_SELECT));
1371 net2272_cancel_dma(dev);
1372 net2272_set_fifo_mode(dev, (fifo_mode <= 3) ? fifo_mode : 0);
1374 /* Set the NET2272 ep fifo data width to 16-bit mode and for correct byte swapping
1375 * note that the higher level gadget drivers are expected to convert data to little endian.
1376 * Enable byte swap for your local bus/cpu if needed by setting BYTE_SWAP in LOCCTL here
1378 net2272_write(dev, LOCCTL, net2272_read(dev, LOCCTL) | (1 << DATA_WIDTH));
1379 net2272_write(dev, LOCCTL1, (dma_mode << DMA_MODE));
1383 net2272_usb_reinit(struct net2272 *dev)
1387 /* basic endpoint init */
1388 for (i = 0; i < 4; ++i) {
1389 struct net2272_ep *ep = &dev->ep[i];
1391 ep->ep.name = ep_name[i];
1396 if (use_dma && ep->num == dma_ep)
1399 if (i > 0 && i <= 3)
1400 ep->fifo_size = 512;
1403 net2272_ep_reset(ep);
1406 ep->ep.caps.type_control = true;
1408 ep->ep.caps.type_iso = true;
1409 ep->ep.caps.type_bulk = true;
1410 ep->ep.caps.type_int = true;
1413 ep->ep.caps.dir_in = true;
1414 ep->ep.caps.dir_out = true;
1416 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
1418 dev->gadget.ep0 = &dev->ep[0].ep;
1419 dev->ep[0].stopped = 0;
1420 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1424 net2272_ep0_start(struct net2272 *dev)
1426 struct net2272_ep *ep0 = &dev->ep[0];
1428 net2272_ep_write(ep0, EP_RSPSET,
1429 (1 << NAK_OUT_PACKETS_MODE) |
1430 (1 << ALT_NAK_OUT_PACKETS));
1431 net2272_ep_write(ep0, EP_RSPCLR,
1432 (1 << HIDE_STATUS_PHASE) |
1433 (1 << CONTROL_STATUS_PHASE_HANDSHAKE));
1434 net2272_write(dev, USBCTL0,
1435 (dev->softconnect << USB_DETECT_ENABLE) |
1436 (1 << USB_ROOT_PORT_WAKEUP_ENABLE) |
1437 (1 << IO_WAKEUP_ENABLE));
1438 net2272_write(dev, IRQENB0,
1439 (1 << SETUP_PACKET_INTERRUPT_ENABLE) |
1440 (1 << ENDPOINT_0_INTERRUPT_ENABLE) |
1441 (1 << DMA_DONE_INTERRUPT_ENABLE));
1442 net2272_write(dev, IRQENB1,
1443 (1 << VBUS_INTERRUPT_ENABLE) |
1444 (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE) |
1445 (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE));
1448 /* when a driver is successfully registered, it will receive
1449 * control requests including set_configuration(), which enables
1450 * non-control requests. then usb traffic follows until a
1451 * disconnect is reported. then a host may connect again, or
1452 * the driver might get unbound.
1454 static int net2272_start(struct usb_gadget *_gadget,
1455 struct usb_gadget_driver *driver)
1457 struct net2272 *dev;
1460 if (!driver || !driver->setup ||
1461 driver->max_speed != USB_SPEED_HIGH)
1464 dev = container_of(_gadget, struct net2272, gadget);
1466 for (i = 0; i < 4; ++i)
1467 dev->ep[i].irqs = 0;
1468 /* hook up the driver ... */
1469 dev->softconnect = 1;
1470 driver->driver.bus = NULL;
1471 dev->driver = driver;
1473 /* ... then enable host detection and ep0; and we're ready
1474 * for set_configuration as well as eventual disconnect.
1476 net2272_ep0_start(dev);
1482 stop_activity(struct net2272 *dev, struct usb_gadget_driver *driver)
1486 /* don't disconnect if it's not connected */
1487 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1490 /* stop hardware; prevent new request submissions;
1491 * and kill any outstanding requests.
1493 net2272_usb_reset(dev);
1494 for (i = 0; i < 4; ++i)
1495 net2272_dequeue_all(&dev->ep[i]);
1497 /* report disconnect; the driver is already quiesced */
1499 spin_unlock(&dev->lock);
1500 driver->disconnect(&dev->gadget);
1501 spin_lock(&dev->lock);
1504 net2272_usb_reinit(dev);
1507 static int net2272_stop(struct usb_gadget *_gadget)
1509 struct net2272 *dev;
1510 unsigned long flags;
1512 dev = container_of(_gadget, struct net2272, gadget);
1514 spin_lock_irqsave(&dev->lock, flags);
1515 stop_activity(dev, NULL);
1516 spin_unlock_irqrestore(&dev->lock, flags);
1523 /*---------------------------------------------------------------------------*/
1524 /* handle ep-a/ep-b dma completions */
1526 net2272_handle_dma(struct net2272_ep *ep)
1528 struct net2272_request *req;
1532 if (!list_empty(&ep->queue))
1533 req = list_entry(ep->queue.next,
1534 struct net2272_request, queue);
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 << 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 */
1559 /* validate a short packet or zlp if necessary */
1560 if ((req->req.length % ep->ep.maxpacket != 0) ||
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);
1570 net2272_pio_advance(ep);
1573 /* host-to-device transfer completed */
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));
1594 req->req.actual += len;
1596 /* get any remaining data */
1597 net2272_pio_advance(ep);
1601 /*---------------------------------------------------------------------------*/
1604 net2272_handle_ep(struct net2272_ep *ep)
1606 struct net2272_request *req;
1609 if (!list_empty(&ep->queue))
1610 req = list_entry(ep->queue.next,
1611 struct net2272_request, queue);
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);
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 : NULL);
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)
1647 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
1648 u8 bEndpointAddress;
1652 bEndpointAddress = ep->desc->bEndpointAddress;
1653 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
1655 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
1666 * JJJJJJJKKKKKKK * 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
1680 net2272_set_test_mode(struct net2272 *dev, int mode)
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);
1724 net2272_handle_stat0_irqs(struct net2272 *dev, u8 stat)
1726 struct net2272_ep *ep;
1729 /* starting a control request? */
1730 if (unlikely(stat & (1 << SETUP_PACKET_INTERRUPT))) {
1733 struct usb_ctrlrequest r;
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;
1742 dev->gadget.speed = USB_SPEED_FULL;
1743 dev_dbg(dev->dev, "%s\n",
1744 usb_speed_string(dev->gadget.speed));
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);
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,
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);
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;
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);
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)
1826 switch (u.r.bRequest) {
1827 case USB_REQ_GET_STATUS: {
1828 struct net2272_ep *e;
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)
1836 if (net2272_ep_read(e, EP_RSPSET) & (1 << ENDPOINT_HALT))
1837 status = cpu_to_le16(1);
1839 status = 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);
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)
1852 if (dev->gadget.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);
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)
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);
1871 dev_vdbg(dev->dev, "interface status %02x\n", status);
1872 goto next_endpoints;
1877 case USB_REQ_CLEAR_FEATURE: {
1878 struct net2272_ep *e;
1880 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1882 if (u.r.wValue != USB_ENDPOINT_HALT ||
1885 e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1889 dev_vdbg(dev->dev, "%s wedged, halt not cleared\n",
1892 dev_vdbg(dev->dev, "%s clear halt\n", ep->ep.name);
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));
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)
1909 if (u.r.wValue != USB_ENDPOINT_HALT ||
1912 e = net2272_get_ep_by_addr(dev, u.r.wIndex);
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);
1927 dev_vdbg(dev->dev, "setup %02x.%02x v%04x i%04x "
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 */
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))
1953 net2272_handle_dma(ep);
1957 /* endpoint data irq? */
1958 scratch = stat & 0x0f;
1960 for (num = 0; scratch; num++) {
1963 /* does this endpoint's FIFO and queue need tending? */
1965 if ((scratch & t) == 0)
1970 net2272_handle_ep(ep);
1973 /* some interrupts we can just ignore */
1974 stat &= ~(1 << SOF_INTERRUPT);
1977 dev_dbg(dev->dev, "unhandled irqstat0 %02x\n", stat);
1981 net2272_handle_stat1_irqs(struct net2272 *dev, u8 stat)
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);
1991 bool disconnect = false;
1994 * Ignore disconnects and resets if the speed hasn't been set.
1995 * VBUS can bounce and there's always an initial reset.
1997 net2272_write(dev, IRQSTAT1, tmp);
1998 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
1999 if ((stat & (1 << VBUS_INTERRUPT)) &&
2000 (net2272_read(dev, USBCTL1) &
2001 (1 << VBUS_PIN)) == 0) {
2003 dev_dbg(dev->dev, "disconnect %s\n",
2004 dev->driver->driver.name);
2005 } else if ((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2006 (net2272_read(dev, USBCTL1) & mask)
2009 dev_dbg(dev->dev, "reset %s\n",
2010 dev->driver->driver.name);
2013 if (disconnect || reset) {
2014 stop_activity(dev, dev->driver);
2015 net2272_ep0_start(dev);
2016 spin_unlock(&dev->lock);
2018 usb_gadget_udc_reset
2019 (&dev->gadget, dev->driver);
2021 (dev->driver->disconnect)
2023 spin_lock(&dev->lock);
2033 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2035 net2272_write(dev, IRQSTAT1, tmp);
2036 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2037 if (dev->driver->suspend)
2038 dev->driver->suspend(&dev->gadget);
2039 if (!enable_suspend) {
2040 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2041 dev_dbg(dev->dev, "Suspend disabled, ignoring\n");
2044 if (dev->driver->resume)
2045 dev->driver->resume(&dev->gadget);
2050 /* clear any other status/irqs */
2052 net2272_write(dev, IRQSTAT1, stat);
2054 /* some status we can just ignore */
2055 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2056 | (1 << SUSPEND_REQUEST_INTERRUPT)
2057 | (1 << RESUME_INTERRUPT));
2061 dev_dbg(dev->dev, "unhandled irqstat1 %02x\n", stat);
2064 static irqreturn_t net2272_irq(int irq, void *_dev)
2066 struct net2272 *dev = _dev;
2067 #if defined(PLX_PCI_RDK) || defined(PLX_PCI_RDK2)
2070 #if defined(PLX_PCI_RDK)
2073 spin_lock(&dev->lock);
2074 #if defined(PLX_PCI_RDK)
2075 intcsr = readl(dev->rdk1.plx9054_base_addr + INTCSR);
2077 if ((intcsr & LOCAL_INTERRUPT_TEST) == LOCAL_INTERRUPT_TEST) {
2078 writel(intcsr & ~(1 << PCI_INTERRUPT_ENABLE),
2079 dev->rdk1.plx9054_base_addr + INTCSR);
2080 net2272_handle_stat1_irqs(dev, net2272_read(dev, IRQSTAT1));
2081 net2272_handle_stat0_irqs(dev, net2272_read(dev, IRQSTAT0));
2082 intcsr = readl(dev->rdk1.plx9054_base_addr + INTCSR);
2083 writel(intcsr | (1 << PCI_INTERRUPT_ENABLE),
2084 dev->rdk1.plx9054_base_addr + INTCSR);
2086 if ((intcsr & DMA_CHANNEL_0_TEST) == DMA_CHANNEL_0_TEST) {
2087 writeb((1 << CHANNEL_CLEAR_INTERRUPT | (0 << CHANNEL_ENABLE)),
2088 dev->rdk1.plx9054_base_addr + DMACSR0);
2090 dmareq = net2272_read(dev, DMAREQ);
2092 net2272_handle_dma(&dev->ep[2]);
2094 net2272_handle_dma(&dev->ep[1]);
2097 #if defined(PLX_PCI_RDK2)
2098 /* see if PCI int for us by checking irqstat */
2099 intcsr = readl(dev->rdk2.fpga_base_addr + RDK2_IRQSTAT);
2100 if (!(intcsr & (1 << NET2272_PCI_IRQ))) {
2101 spin_unlock(&dev->lock);
2104 /* check dma interrupts */
2106 /* Platform/devcice interrupt handler */
2107 #if !defined(PLX_PCI_RDK)
2108 net2272_handle_stat1_irqs(dev, net2272_read(dev, IRQSTAT1));
2109 net2272_handle_stat0_irqs(dev, net2272_read(dev, IRQSTAT0));
2111 spin_unlock(&dev->lock);
2116 static int net2272_present(struct net2272 *dev)
2119 * Quick test to see if CPU can communicate properly with the NET2272.
2120 * Verifies connection using writes and reads to write/read and
2121 * read-only registers.
2123 * This routine is strongly recommended especially during early bring-up
2124 * of new hardware, however for designs that do not apply Power On System
2125 * Tests (POST) it may discarded (or perhaps minimized).
2130 /* Verify NET2272 write/read SCRATCH register can write and read */
2131 refval = net2272_read(dev, SCRATCH);
2132 for (ii = 0; ii < 0x100; ii += 7) {
2133 net2272_write(dev, SCRATCH, ii);
2134 val = net2272_read(dev, SCRATCH);
2137 "%s: write/read SCRATCH register test failed: "
2138 "wrote:0x%2.2x, read:0x%2.2x\n",
2143 /* To be nice, we write the original SCRATCH value back: */
2144 net2272_write(dev, SCRATCH, refval);
2146 /* Verify NET2272 CHIPREV register is read-only: */
2147 refval = net2272_read(dev, CHIPREV_2272);
2148 for (ii = 0; ii < 0x100; ii += 7) {
2149 net2272_write(dev, CHIPREV_2272, ii);
2150 val = net2272_read(dev, CHIPREV_2272);
2151 if (val != refval) {
2153 "%s: write/read CHIPREV register test failed: "
2154 "wrote 0x%2.2x, read:0x%2.2x expected:0x%2.2x\n",
2155 __func__, ii, val, refval);
2161 * Verify NET2272's "NET2270 legacy revision" register
2162 * - NET2272 has two revision registers. The NET2270 legacy revision
2163 * register should read the same value, regardless of the NET2272
2164 * silicon revision. The legacy register applies to NET2270
2165 * firmware being applied to the NET2272.
2167 val = net2272_read(dev, CHIPREV_LEGACY);
2168 if (val != NET2270_LEGACY_REV) {
2170 * Unexpected legacy revision value
2171 * - Perhaps the chip is a NET2270?
2174 "%s: WARNING: UNEXPECTED NET2272 LEGACY REGISTER VALUE:\n"
2175 " - CHIPREV_LEGACY: expected 0x%2.2x, got:0x%2.2x. (Not NET2272?)\n",
2176 __func__, NET2270_LEGACY_REV, val);
2181 * Verify NET2272 silicon revision
2182 * - This revision register is appropriate for the silicon version
2185 val = net2272_read(dev, CHIPREV_2272);
2187 case CHIPREV_NET2272_R1:
2189 * NET2272 Rev 1 has DMA related errata:
2190 * - Newer silicon (Rev 1A or better) required
2193 "%s: Rev 1 detected: newer silicon recommended for DMA support\n",
2196 case CHIPREV_NET2272_R1A:
2199 /* NET2272 silicon version *may* not work with this firmware */
2201 "%s: unexpected silicon revision register value: "
2202 " CHIPREV_2272: 0x%2.2x\n",
2205 * Return Success, even though the chip rev is not an expected value
2206 * - Older, pre-built firmware can attempt to operate on newer silicon
2207 * - Often, new silicon is perfectly compatible
2211 /* Success: NET2272 checks out OK */
2216 net2272_gadget_release(struct device *_dev)
2218 struct net2272 *dev = dev_get_drvdata(_dev);
2222 /*---------------------------------------------------------------------------*/
2225 net2272_remove(struct net2272 *dev)
2227 usb_del_gadget_udc(&dev->gadget);
2228 free_irq(dev->irq, dev);
2229 iounmap(dev->base_addr);
2230 device_remove_file(dev->dev, &dev_attr_registers);
2232 dev_info(dev->dev, "unbind\n");
2235 static struct net2272 *net2272_probe_init(struct device *dev, unsigned int irq)
2237 struct net2272 *ret;
2240 dev_dbg(dev, "No IRQ!\n");
2241 return ERR_PTR(-ENODEV);
2244 /* alloc, and start init */
2245 ret = kzalloc(sizeof(*ret), GFP_KERNEL);
2247 return ERR_PTR(-ENOMEM);
2249 spin_lock_init(&ret->lock);
2252 ret->gadget.ops = &net2272_ops;
2253 ret->gadget.max_speed = USB_SPEED_HIGH;
2255 /* the "gadget" abstracts/virtualizes the controller */
2256 ret->gadget.name = driver_name;
2262 net2272_probe_fin(struct net2272 *dev, unsigned int irqflags)
2266 /* See if there... */
2267 if (net2272_present(dev)) {
2268 dev_warn(dev->dev, "2272 not found!\n");
2273 net2272_usb_reset(dev);
2274 net2272_usb_reinit(dev);
2276 ret = request_irq(dev->irq, net2272_irq, irqflags, driver_name, dev);
2278 dev_err(dev->dev, "request interrupt %i failed\n", dev->irq);
2282 dev->chiprev = net2272_read(dev, CHIPREV_2272);
2285 dev_info(dev->dev, "%s\n", driver_desc);
2286 dev_info(dev->dev, "irq %i, mem %p, chip rev %04x, dma %s\n",
2287 dev->irq, dev->base_addr, dev->chiprev,
2289 dev_info(dev->dev, "version: %s\n", driver_vers);
2291 ret = device_create_file(dev->dev, &dev_attr_registers);
2295 ret = usb_add_gadget_udc_release(dev->dev, &dev->gadget,
2296 net2272_gadget_release);
2303 device_remove_file(dev->dev, &dev_attr_registers);
2305 free_irq(dev->irq, dev);
2313 * wrap this driver around the specified device, but
2314 * don't respond over USB until a gadget driver binds to us
2318 net2272_rdk1_probe(struct pci_dev *pdev, struct net2272 *dev)
2320 unsigned long resource, len, tmp;
2321 void __iomem *mem_mapped_addr[4];
2325 * BAR 0 holds PLX 9054 config registers
2326 * BAR 1 is i/o memory; unused here
2327 * BAR 2 holds EPLD config registers
2328 * BAR 3 holds NET2272 registers
2331 /* Find and map all address spaces */
2332 for (i = 0; i < 4; ++i) {
2334 continue; /* BAR1 unused */
2336 resource = pci_resource_start(pdev, i);
2337 len = pci_resource_len(pdev, i);
2339 if (!request_mem_region(resource, len, driver_name)) {
2340 dev_dbg(dev->dev, "controller already in use\n");
2345 mem_mapped_addr[i] = ioremap_nocache(resource, len);
2346 if (mem_mapped_addr[i] == NULL) {
2347 release_mem_region(resource, len);
2348 dev_dbg(dev->dev, "can't map memory\n");
2354 dev->rdk1.plx9054_base_addr = mem_mapped_addr[0];
2355 dev->rdk1.epld_base_addr = mem_mapped_addr[2];
2356 dev->base_addr = mem_mapped_addr[3];
2358 /* Set PLX 9054 bus width (16 bits) */
2359 tmp = readl(dev->rdk1.plx9054_base_addr + LBRD1);
2360 writel((tmp & ~(3 << MEMORY_SPACE_LOCAL_BUS_WIDTH)) | W16_BIT,
2361 dev->rdk1.plx9054_base_addr + LBRD1);
2363 /* Enable PLX 9054 Interrupts */
2364 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) |
2365 (1 << PCI_INTERRUPT_ENABLE) |
2366 (1 << LOCAL_INTERRUPT_INPUT_ENABLE),
2367 dev->rdk1.plx9054_base_addr + INTCSR);
2369 writeb((1 << CHANNEL_CLEAR_INTERRUPT | (0 << CHANNEL_ENABLE)),
2370 dev->rdk1.plx9054_base_addr + DMACSR0);
2373 writeb((1 << EPLD_DMA_ENABLE) |
2374 (1 << DMA_CTL_DACK) |
2375 (1 << DMA_TIMEOUT_ENABLE) |
2379 (1 << NET2272_RESET),
2380 dev->base_addr + EPLD_IO_CONTROL_REGISTER);
2383 writeb(readb(dev->base_addr + EPLD_IO_CONTROL_REGISTER) &
2384 ~(1 << NET2272_RESET),
2385 dev->base_addr + EPLD_IO_CONTROL_REGISTER);
2392 iounmap(mem_mapped_addr[i]);
2393 release_mem_region(pci_resource_start(pdev, i),
2394 pci_resource_len(pdev, i));
2401 net2272_rdk2_probe(struct pci_dev *pdev, struct net2272 *dev)
2403 unsigned long resource, len;
2404 void __iomem *mem_mapped_addr[2];
2408 * BAR 0 holds FGPA config registers
2409 * BAR 1 holds NET2272 registers
2412 /* Find and map all address spaces, bar2-3 unused in rdk 2 */
2413 for (i = 0; i < 2; ++i) {
2414 resource = pci_resource_start(pdev, i);
2415 len = pci_resource_len(pdev, i);
2417 if (!request_mem_region(resource, len, driver_name)) {
2418 dev_dbg(dev->dev, "controller already in use\n");
2423 mem_mapped_addr[i] = ioremap_nocache(resource, len);
2424 if (mem_mapped_addr[i] == NULL) {
2425 release_mem_region(resource, len);
2426 dev_dbg(dev->dev, "can't map memory\n");
2432 dev->rdk2.fpga_base_addr = mem_mapped_addr[0];
2433 dev->base_addr = mem_mapped_addr[1];
2436 /* Set 2272 bus width (16 bits) and reset */
2437 writel((1 << CHIP_RESET), dev->rdk2.fpga_base_addr + RDK2_LOCCTLRDK);
2439 writel((1 << BUS_WIDTH), dev->rdk2.fpga_base_addr + RDK2_LOCCTLRDK);
2440 /* Print fpga version number */
2441 dev_info(dev->dev, "RDK2 FPGA version %08x\n",
2442 readl(dev->rdk2.fpga_base_addr + RDK2_FPGAREV));
2443 /* Enable FPGA Interrupts */
2444 writel((1 << NET2272_PCI_IRQ), dev->rdk2.fpga_base_addr + RDK2_IRQENB);
2450 iounmap(mem_mapped_addr[i]);
2451 release_mem_region(pci_resource_start(pdev, i),
2452 pci_resource_len(pdev, i));
2459 net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2461 struct net2272 *dev;
2464 dev = net2272_probe_init(&pdev->dev, pdev->irq);
2466 return PTR_ERR(dev);
2467 dev->dev_id = pdev->device;
2469 if (pci_enable_device(pdev) < 0) {
2474 pci_set_master(pdev);
2476 switch (pdev->device) {
2477 case PCI_DEVICE_ID_RDK1: ret = net2272_rdk1_probe(pdev, dev); break;
2478 case PCI_DEVICE_ID_RDK2: ret = net2272_rdk2_probe(pdev, dev); break;
2484 ret = net2272_probe_fin(dev, 0);
2488 pci_set_drvdata(pdev, dev);
2493 pci_disable_device(pdev);
2501 net2272_rdk1_remove(struct pci_dev *pdev, struct net2272 *dev)
2505 /* disable PLX 9054 interrupts */
2506 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2507 ~(1 << PCI_INTERRUPT_ENABLE),
2508 dev->rdk1.plx9054_base_addr + INTCSR);
2510 /* clean up resources allocated during probe() */
2511 iounmap(dev->rdk1.plx9054_base_addr);
2512 iounmap(dev->rdk1.epld_base_addr);
2514 for (i = 0; i < 4; ++i) {
2516 continue; /* BAR1 unused */
2517 release_mem_region(pci_resource_start(pdev, i),
2518 pci_resource_len(pdev, i));
2523 net2272_rdk2_remove(struct pci_dev *pdev, struct net2272 *dev)
2527 /* disable fpga interrupts
2528 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2529 ~(1 << PCI_INTERRUPT_ENABLE),
2530 dev->rdk1.plx9054_base_addr + INTCSR);
2533 /* clean up resources allocated during probe() */
2534 iounmap(dev->rdk2.fpga_base_addr);
2536 for (i = 0; i < 2; ++i)
2537 release_mem_region(pci_resource_start(pdev, i),
2538 pci_resource_len(pdev, i));
2542 net2272_pci_remove(struct pci_dev *pdev)
2544 struct net2272 *dev = pci_get_drvdata(pdev);
2546 net2272_remove(dev);
2548 switch (pdev->device) {
2549 case PCI_DEVICE_ID_RDK1: net2272_rdk1_remove(pdev, dev); break;
2550 case PCI_DEVICE_ID_RDK2: net2272_rdk2_remove(pdev, dev); break;
2554 pci_disable_device(pdev);
2559 /* Table of matching PCI IDs */
2560 static struct pci_device_id pci_ids[] = {
2562 .class = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2564 .vendor = PCI_VENDOR_ID_PLX,
2565 .device = PCI_DEVICE_ID_RDK1,
2566 .subvendor = PCI_ANY_ID,
2567 .subdevice = PCI_ANY_ID,
2570 .class = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2572 .vendor = PCI_VENDOR_ID_PLX,
2573 .device = PCI_DEVICE_ID_RDK2,
2574 .subvendor = PCI_ANY_ID,
2575 .subdevice = PCI_ANY_ID,
2579 MODULE_DEVICE_TABLE(pci, pci_ids);
2581 static struct pci_driver net2272_pci_driver = {
2582 .name = driver_name,
2583 .id_table = pci_ids,
2585 .probe = net2272_pci_probe,
2586 .remove = net2272_pci_remove,
2589 static int net2272_pci_register(void)
2591 return pci_register_driver(&net2272_pci_driver);
2594 static void net2272_pci_unregister(void)
2596 pci_unregister_driver(&net2272_pci_driver);
2600 static inline int net2272_pci_register(void) { return 0; }
2601 static inline void net2272_pci_unregister(void) { }
2604 /*---------------------------------------------------------------------------*/
2607 net2272_plat_probe(struct platform_device *pdev)
2609 struct net2272 *dev;
2611 unsigned int irqflags;
2612 resource_size_t base, len;
2613 struct resource *iomem, *iomem_bus, *irq_res;
2615 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2616 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2617 iomem_bus = platform_get_resource(pdev, IORESOURCE_BUS, 0);
2618 if (!irq_res || !iomem) {
2619 dev_err(&pdev->dev, "must provide irq/base addr");
2623 dev = net2272_probe_init(&pdev->dev, irq_res->start);
2625 return PTR_ERR(dev);
2628 if (irq_res->flags & IORESOURCE_IRQ_HIGHEDGE)
2629 irqflags |= IRQF_TRIGGER_RISING;
2630 if (irq_res->flags & IORESOURCE_IRQ_LOWEDGE)
2631 irqflags |= IRQF_TRIGGER_FALLING;
2632 if (irq_res->flags & IORESOURCE_IRQ_HIGHLEVEL)
2633 irqflags |= IRQF_TRIGGER_HIGH;
2634 if (irq_res->flags & IORESOURCE_IRQ_LOWLEVEL)
2635 irqflags |= IRQF_TRIGGER_LOW;
2637 base = iomem->start;
2638 len = resource_size(iomem);
2640 dev->base_shift = iomem_bus->start;
2642 if (!request_mem_region(base, len, driver_name)) {
2643 dev_dbg(dev->dev, "get request memory region!\n");
2647 dev->base_addr = ioremap_nocache(base, len);
2648 if (!dev->base_addr) {
2649 dev_dbg(dev->dev, "can't map memory\n");
2654 ret = net2272_probe_fin(dev, IRQF_TRIGGER_LOW);
2658 platform_set_drvdata(pdev, dev);
2659 dev_info(&pdev->dev, "running in 16-bit, %sbyte swap local bus mode\n",
2660 (net2272_read(dev, LOCCTL) & (1 << BYTE_SWAP)) ? "" : "no ");
2665 iounmap(dev->base_addr);
2667 release_mem_region(base, len);
2675 net2272_plat_remove(struct platform_device *pdev)
2677 struct net2272 *dev = platform_get_drvdata(pdev);
2679 net2272_remove(dev);
2681 release_mem_region(pdev->resource[0].start,
2682 resource_size(&pdev->resource[0]));
2689 static struct platform_driver net2272_plat_driver = {
2690 .probe = net2272_plat_probe,
2691 .remove = net2272_plat_remove,
2693 .name = driver_name,
2695 /* FIXME .suspend, .resume */
2697 MODULE_ALIAS("platform:net2272");
2699 static int __init net2272_init(void)
2703 ret = net2272_pci_register();
2706 ret = platform_driver_register(&net2272_plat_driver);
2712 net2272_pci_unregister();
2715 module_init(net2272_init);
2717 static void __exit net2272_cleanup(void)
2719 net2272_pci_unregister();
2720 platform_driver_unregister(&net2272_plat_driver);
2722 module_exit(net2272_cleanup);
2724 MODULE_DESCRIPTION(DRIVER_DESC);
2725 MODULE_AUTHOR("PLX Technology, Inc.");
2726 MODULE_LICENSE("GPL");