1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for PLX NET2272 USB device controller
5 * Copyright (C) 2005-2006 PLX Technology, Inc.
6 * Copyright (C) 2006-2011 Analog Devices, Inc.
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/gpio.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/pci.h>
22 #include <linux/platform_device.h>
23 #include <linux/prefetch.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/timer.h>
27 #include <linux/usb.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
31 #include <asm/byteorder.h>
32 #include <asm/unaligned.h>
36 #define DRIVER_DESC "PLX NET2272 USB Peripheral Controller"
38 static const char driver_name
[] = "net2272";
39 static const char driver_vers
[] = "2006 October 17/mainline";
40 static const char driver_desc
[] = DRIVER_DESC
;
42 static const char ep0name
[] = "ep0";
43 static const char * const ep_name
[] = {
45 "ep-a", "ep-b", "ep-c",
48 #ifdef CONFIG_USB_NET2272_DMA
50 * use_dma: the NET2272 can use an external DMA controller.
51 * Note that since there is no generic DMA api, some functions,
52 * notably request_dma, start_dma, and cancel_dma will need to be
53 * modified for your platform's particular dma controller.
55 * If use_dma is disabled, pio will be used instead.
57 static bool use_dma
= 0;
58 module_param(use_dma
, bool, 0644);
61 * dma_ep: selects the endpoint for use with dma (1=ep-a, 2=ep-b)
62 * The NET2272 can only use dma for a single endpoint at a time.
63 * At some point this could be modified to allow either endpoint
64 * to take control of dma as it becomes available.
66 * Note that DMA should not be used on OUT endpoints unless it can
67 * be guaranteed that no short packets will arrive on an IN endpoint
68 * while the DMA operation is pending. Otherwise the OUT DMA will
69 * terminate prematurely (See NET2272 Errata 630-0213-0101)
71 static ushort dma_ep
= 1;
72 module_param(dma_ep
, ushort
, 0644);
75 * dma_mode: net2272 dma mode setting (see LOCCTL1 definiton):
76 * mode 0 == Slow DREQ mode
77 * mode 1 == Fast DREQ mode
78 * mode 2 == Burst mode
80 static ushort dma_mode
= 2;
81 module_param(dma_mode
, ushort
, 0644);
89 * fifo_mode: net2272 buffer configuration:
90 * mode 0 == ep-{a,b,c} 512db each
91 * mode 1 == ep-a 1k, ep-{b,c} 512db
92 * mode 2 == ep-a 1k, ep-b 1k, ep-c 512db
93 * mode 3 == ep-a 1k, ep-b disabled, ep-c 512db
95 static ushort fifo_mode
= 0;
96 module_param(fifo_mode
, ushort
, 0644);
99 * enable_suspend: When enabled, the driver will respond to
100 * USB suspend requests by powering down the NET2272. Otherwise,
101 * USB suspend requests will be ignored. This is acceptible for
102 * self-powered devices. For bus powered devices set this to 1.
104 static ushort enable_suspend
= 0;
105 module_param(enable_suspend
, ushort
, 0644);
107 static void assert_out_naking(struct net2272_ep
*ep
, const char *where
)
115 tmp
= net2272_ep_read(ep
, EP_STAT0
);
116 if ((tmp
& (1 << NAK_OUT_PACKETS
)) == 0) {
117 dev_dbg(ep
->dev
->dev
, "%s %s %02x !NAK\n",
118 ep
->ep
.name
, where
, tmp
);
119 net2272_ep_write(ep
, EP_RSPSET
, 1 << ALT_NAK_OUT_PACKETS
);
122 #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep, __func__)
124 static void stop_out_naking(struct net2272_ep
*ep
)
126 u8 tmp
= net2272_ep_read(ep
, EP_STAT0
);
128 if ((tmp
& (1 << NAK_OUT_PACKETS
)) != 0)
129 net2272_ep_write(ep
, EP_RSPCLR
, 1 << ALT_NAK_OUT_PACKETS
);
132 #define PIPEDIR(bAddress) (usb_pipein(bAddress) ? "in" : "out")
134 static char *type_string(u8 bmAttributes
)
136 switch ((bmAttributes
) & USB_ENDPOINT_XFERTYPE_MASK
) {
137 case USB_ENDPOINT_XFER_BULK
: return "bulk";
138 case USB_ENDPOINT_XFER_ISOC
: return "iso";
139 case USB_ENDPOINT_XFER_INT
: return "intr";
140 default: return "control";
144 static char *buf_state_string(unsigned state
)
147 case BUFF_FREE
: return "free";
148 case BUFF_VALID
: return "valid";
149 case BUFF_LCL
: return "local";
150 case BUFF_USB
: return "usb";
151 default: return "unknown";
155 static char *dma_mode_string(void)
160 case 0: return "SLOW DREQ";
161 case 1: return "FAST DREQ";
162 case 2: return "BURST";
163 default: return "invalid";
167 static void net2272_dequeue_all(struct net2272_ep
*);
168 static int net2272_kick_dma(struct net2272_ep
*, struct net2272_request
*);
169 static int net2272_fifo_status(struct usb_ep
*);
171 static const struct usb_ep_ops net2272_ep_ops
;
173 /*---------------------------------------------------------------------------*/
176 net2272_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
179 struct net2272_ep
*ep
;
184 ep
= container_of(_ep
, struct net2272_ep
, ep
);
185 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
186 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
189 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
192 max
= usb_endpoint_maxp(desc
);
194 spin_lock_irqsave(&dev
->lock
, flags
);
195 _ep
->maxpacket
= max
;
198 /* net2272_ep_reset() has already been called */
202 /* set speed-dependent max packet */
203 net2272_ep_write(ep
, EP_MAXPKT0
, max
& 0xff);
204 net2272_ep_write(ep
, EP_MAXPKT1
, (max
& 0xff00) >> 8);
206 /* set type, direction, address; reset fifo counters */
207 net2272_ep_write(ep
, EP_STAT1
, 1 << BUFFER_FLUSH
);
208 tmp
= usb_endpoint_type(desc
);
209 if (usb_endpoint_xfer_bulk(desc
)) {
210 /* catch some particularly blatant driver bugs */
211 if ((dev
->gadget
.speed
== USB_SPEED_HIGH
&& max
!= 512) ||
212 (dev
->gadget
.speed
== USB_SPEED_FULL
&& max
> 64)) {
213 spin_unlock_irqrestore(&dev
->lock
, flags
);
217 ep
->is_iso
= usb_endpoint_xfer_isoc(desc
) ? 1 : 0;
218 tmp
<<= ENDPOINT_TYPE
;
219 tmp
|= ((desc
->bEndpointAddress
& 0x0f) << ENDPOINT_NUMBER
);
220 tmp
|= usb_endpoint_dir_in(desc
) << ENDPOINT_DIRECTION
;
221 tmp
|= (1 << ENDPOINT_ENABLE
);
223 /* for OUT transfers, block the rx fifo until a read is posted */
224 ep
->is_in
= usb_endpoint_dir_in(desc
);
226 net2272_ep_write(ep
, EP_RSPSET
, 1 << ALT_NAK_OUT_PACKETS
);
228 net2272_ep_write(ep
, EP_CFG
, tmp
);
231 tmp
= (1 << ep
->num
) | net2272_read(dev
, IRQENB0
);
232 net2272_write(dev
, IRQENB0
, tmp
);
234 tmp
= (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
)
235 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
)
236 | net2272_ep_read(ep
, EP_IRQENB
);
237 net2272_ep_write(ep
, EP_IRQENB
, tmp
);
239 tmp
= desc
->bEndpointAddress
;
240 dev_dbg(dev
->dev
, "enabled %s (ep%d%s-%s) max %04x cfg %02x\n",
241 _ep
->name
, tmp
& 0x0f, PIPEDIR(tmp
),
242 type_string(desc
->bmAttributes
), max
,
243 net2272_ep_read(ep
, EP_CFG
));
245 spin_unlock_irqrestore(&dev
->lock
, flags
);
249 static void net2272_ep_reset(struct net2272_ep
*ep
)
254 INIT_LIST_HEAD(&ep
->queue
);
256 usb_ep_set_maxpacket_limit(&ep
->ep
, ~0);
257 ep
->ep
.ops
= &net2272_ep_ops
;
259 /* disable irqs, endpoint */
260 net2272_ep_write(ep
, EP_IRQENB
, 0);
262 /* init to our chosen defaults, notably so that we NAK OUT
263 * packets until the driver queues a read.
265 tmp
= (1 << NAK_OUT_PACKETS_MODE
) | (1 << ALT_NAK_OUT_PACKETS
);
266 net2272_ep_write(ep
, EP_RSPSET
, tmp
);
268 tmp
= (1 << INTERRUPT_MODE
) | (1 << HIDE_STATUS_PHASE
);
270 tmp
|= (1 << ENDPOINT_TOGGLE
) | (1 << ENDPOINT_HALT
);
272 net2272_ep_write(ep
, EP_RSPCLR
, tmp
);
274 /* scrub most status bits, and flush any fifo state */
275 net2272_ep_write(ep
, EP_STAT0
,
276 (1 << DATA_IN_TOKEN_INTERRUPT
)
277 | (1 << DATA_OUT_TOKEN_INTERRUPT
)
278 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
279 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
280 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
));
282 net2272_ep_write(ep
, EP_STAT1
,
284 | (1 << USB_OUT_ACK_SENT
)
285 | (1 << USB_OUT_NAK_SENT
)
286 | (1 << USB_IN_ACK_RCVD
)
287 | (1 << USB_IN_NAK_SENT
)
288 | (1 << USB_STALL_SENT
)
289 | (1 << LOCAL_OUT_ZLP
)
290 | (1 << BUFFER_FLUSH
));
292 /* fifo size is handled seperately */
295 static int net2272_disable(struct usb_ep
*_ep
)
297 struct net2272_ep
*ep
;
300 ep
= container_of(_ep
, struct net2272_ep
, ep
);
301 if (!_ep
|| !ep
->desc
|| _ep
->name
== ep0name
)
304 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
305 net2272_dequeue_all(ep
);
306 net2272_ep_reset(ep
);
308 dev_vdbg(ep
->dev
->dev
, "disabled %s\n", _ep
->name
);
310 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
314 /*---------------------------------------------------------------------------*/
316 static struct usb_request
*
317 net2272_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
319 struct net2272_request
*req
;
324 req
= kzalloc(sizeof(*req
), gfp_flags
);
328 INIT_LIST_HEAD(&req
->queue
);
334 net2272_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
336 struct net2272_request
*req
;
341 req
= container_of(_req
, struct net2272_request
, req
);
342 WARN_ON(!list_empty(&req
->queue
));
347 net2272_done(struct net2272_ep
*ep
, struct net2272_request
*req
, int status
)
350 unsigned stopped
= ep
->stopped
;
353 if (ep
->dev
->protocol_stall
) {
360 list_del_init(&req
->queue
);
362 if (req
->req
.status
== -EINPROGRESS
)
363 req
->req
.status
= status
;
365 status
= req
->req
.status
;
368 if (use_dma
&& ep
->dma
)
369 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
,
372 if (status
&& status
!= -ESHUTDOWN
)
373 dev_vdbg(dev
->dev
, "complete %s req %p stat %d len %u/%u buf %p\n",
374 ep
->ep
.name
, &req
->req
, status
,
375 req
->req
.actual
, req
->req
.length
, req
->req
.buf
);
377 /* don't modify queue heads during completion callback */
379 spin_unlock(&dev
->lock
);
380 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
381 spin_lock(&dev
->lock
);
382 ep
->stopped
= stopped
;
386 net2272_write_packet(struct net2272_ep
*ep
, u8
*buf
,
387 struct net2272_request
*req
, unsigned max
)
389 u16 __iomem
*ep_data
= net2272_reg_addr(ep
->dev
, EP_DATA
);
391 unsigned length
, count
;
394 length
= min(req
->req
.length
- req
->req
.actual
, max
);
395 req
->req
.actual
+= length
;
397 dev_vdbg(ep
->dev
->dev
, "write packet %s req %p max %u len %u avail %u\n",
398 ep
->ep
.name
, req
, max
, length
,
399 (net2272_ep_read(ep
, EP_AVAIL1
) << 8) | net2272_ep_read(ep
, EP_AVAIL0
));
404 while (likely(count
>= 2)) {
405 /* no byte-swap required; chip endian set during init */
406 writew(*bufp
++, ep_data
);
411 /* write final byte by placing the NET2272 into 8-bit mode */
412 if (unlikely(count
)) {
413 tmp
= net2272_read(ep
->dev
, LOCCTL
);
414 net2272_write(ep
->dev
, LOCCTL
, tmp
& ~(1 << DATA_WIDTH
));
415 writeb(*buf
, ep_data
);
416 net2272_write(ep
->dev
, LOCCTL
, tmp
);
421 /* returns: 0: still running, 1: completed, negative: errno */
423 net2272_write_fifo(struct net2272_ep
*ep
, struct net2272_request
*req
)
429 dev_vdbg(ep
->dev
->dev
, "write_fifo %s actual %d len %d\n",
430 ep
->ep
.name
, req
->req
.actual
, req
->req
.length
);
433 * Keep loading the endpoint until the final packet is loaded,
434 * or the endpoint buffer is full.
438 * Clear interrupt status
439 * - Packet Transmitted interrupt will become set again when the
440 * host successfully takes another packet
442 net2272_ep_write(ep
, EP_STAT0
, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
));
443 while (!(net2272_ep_read(ep
, EP_STAT0
) & (1 << BUFFER_FULL
))) {
444 buf
= req
->req
.buf
+ req
->req
.actual
;
448 net2272_ep_read(ep
, EP_STAT0
);
450 max
= (net2272_ep_read(ep
, EP_AVAIL1
) << 8) |
451 (net2272_ep_read(ep
, EP_AVAIL0
));
453 if (max
< ep
->ep
.maxpacket
)
454 max
= (net2272_ep_read(ep
, EP_AVAIL1
) << 8)
455 | (net2272_ep_read(ep
, EP_AVAIL0
));
457 count
= net2272_write_packet(ep
, buf
, req
, max
);
458 /* see if we are done */
459 if (req
->req
.length
== req
->req
.actual
) {
460 /* validate short or zlp packet */
461 if (count
< ep
->ep
.maxpacket
)
462 set_fifo_bytecount(ep
, 0);
463 net2272_done(ep
, req
, 0);
465 if (!list_empty(&ep
->queue
)) {
466 req
= list_entry(ep
->queue
.next
,
467 struct net2272_request
,
469 status
= net2272_kick_dma(ep
, req
);
472 if ((net2272_ep_read(ep
, EP_STAT0
)
473 & (1 << BUFFER_EMPTY
)))
478 net2272_ep_write(ep
, EP_STAT0
, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
));
484 net2272_out_flush(struct net2272_ep
*ep
)
486 ASSERT_OUT_NAKING(ep
);
488 net2272_ep_write(ep
, EP_STAT0
, (1 << DATA_OUT_TOKEN_INTERRUPT
)
489 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
));
490 net2272_ep_write(ep
, EP_STAT1
, 1 << BUFFER_FLUSH
);
494 net2272_read_packet(struct net2272_ep
*ep
, u8
*buf
,
495 struct net2272_request
*req
, unsigned avail
)
497 u16 __iomem
*ep_data
= net2272_reg_addr(ep
->dev
, EP_DATA
);
501 req
->req
.actual
+= avail
;
503 dev_vdbg(ep
->dev
->dev
, "read packet %s req %p len %u avail %u\n",
504 ep
->ep
.name
, req
, avail
,
505 (net2272_ep_read(ep
, EP_AVAIL1
) << 8) | net2272_ep_read(ep
, EP_AVAIL0
));
507 is_short
= (avail
< ep
->ep
.maxpacket
);
509 if (unlikely(avail
== 0)) {
510 /* remove any zlp from the buffer */
511 (void)readw(ep_data
);
515 /* Ensure we get the final byte */
516 if (unlikely(avail
% 2))
521 *bufp
++ = readw(ep_data
);
526 * To avoid false endpoint available race condition must read
527 * ep stat0 twice in the case of a short transfer
529 if (net2272_ep_read(ep
, EP_STAT0
) & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
))
530 net2272_ep_read(ep
, EP_STAT0
);
536 net2272_read_fifo(struct net2272_ep
*ep
, struct net2272_request
*req
)
545 dev_vdbg(ep
->dev
->dev
, "read_fifo %s actual %d len %d\n",
546 ep
->ep
.name
, req
->req
.actual
, req
->req
.length
);
550 buf
= req
->req
.buf
+ req
->req
.actual
;
553 count
= (net2272_ep_read(ep
, EP_AVAIL1
) << 8)
554 | net2272_ep_read(ep
, EP_AVAIL0
);
556 net2272_ep_write(ep
, EP_STAT0
,
557 (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
) |
558 (1 << DATA_PACKET_RECEIVED_INTERRUPT
));
560 tmp
= req
->req
.length
- req
->req
.actual
;
563 if ((tmp
% ep
->ep
.maxpacket
) != 0) {
564 dev_err(ep
->dev
->dev
,
565 "%s out fifo %d bytes, expected %d\n",
566 ep
->ep
.name
, count
, tmp
);
569 count
= (tmp
> 0) ? tmp
: 0;
572 is_short
= net2272_read_packet(ep
, buf
, req
, count
);
575 if (unlikely(cleanup
|| is_short
||
576 ((req
->req
.actual
== req
->req
.length
)
577 && !req
->req
.zero
))) {
580 net2272_out_flush(ep
);
581 net2272_done(ep
, req
, -EOVERFLOW
);
583 net2272_done(ep
, req
, 0);
585 /* re-initialize endpoint transfer registers
586 * otherwise they may result in erroneous pre-validation
587 * for subsequent control reads
589 if (unlikely(ep
->num
== 0)) {
590 net2272_ep_write(ep
, EP_TRANSFER2
, 0);
591 net2272_ep_write(ep
, EP_TRANSFER1
, 0);
592 net2272_ep_write(ep
, EP_TRANSFER0
, 0);
595 if (!list_empty(&ep
->queue
)) {
596 req
= list_entry(ep
->queue
.next
,
597 struct net2272_request
, queue
);
598 status
= net2272_kick_dma(ep
, req
);
600 !(net2272_ep_read(ep
, EP_STAT0
) & (1 << BUFFER_EMPTY
)))
605 } while (!(net2272_ep_read(ep
, EP_STAT0
) & (1 << BUFFER_EMPTY
)));
611 net2272_pio_advance(struct net2272_ep
*ep
)
613 struct net2272_request
*req
;
615 if (unlikely(list_empty(&ep
->queue
)))
618 req
= list_entry(ep
->queue
.next
, struct net2272_request
, queue
);
619 (ep
->is_in
? net2272_write_fifo
: net2272_read_fifo
)(ep
, req
);
622 /* returns 0 on success, else negative errno */
624 net2272_request_dma(struct net2272
*dev
, unsigned ep
, u32 buf
,
625 unsigned len
, unsigned dir
)
627 dev_vdbg(dev
->dev
, "request_dma ep %d buf %08x len %d dir %d\n",
630 /* The NET2272 only supports a single dma channel */
634 * EP_TRANSFER (used to determine the number of bytes received
635 * in an OUT transfer) is 24 bits wide; don't ask for more than that.
637 if ((dir
== 1) && (len
> 0x1000000))
642 /* initialize platform's dma */
643 #ifdef CONFIG_USB_PCI
644 /* NET2272 addr, buffer addr, length, etc. */
645 switch (dev
->dev_id
) {
646 case PCI_DEVICE_ID_RDK1
:
647 /* Setup PLX 9054 DMA mode */
648 writel((1 << LOCAL_BUS_WIDTH
) |
649 (1 << TA_READY_INPUT_ENABLE
) |
650 (0 << LOCAL_BURST_ENABLE
) |
651 (1 << DONE_INTERRUPT_ENABLE
) |
652 (1 << LOCAL_ADDRESSING_MODE
) |
654 (1 << DMA_EOT_ENABLE
) |
655 (1 << FAST_SLOW_TERMINATE_MODE_SELECT
) |
656 (1 << DMA_CHANNEL_INTERRUPT_SELECT
),
657 dev
->rdk1
.plx9054_base_addr
+ DMAMODE0
);
659 writel(0x100000, dev
->rdk1
.plx9054_base_addr
+ DMALADR0
);
660 writel(buf
, dev
->rdk1
.plx9054_base_addr
+ DMAPADR0
);
661 writel(len
, dev
->rdk1
.plx9054_base_addr
+ DMASIZ0
);
662 writel((dir
<< DIRECTION_OF_TRANSFER
) |
663 (1 << INTERRUPT_AFTER_TERMINAL_COUNT
),
664 dev
->rdk1
.plx9054_base_addr
+ DMADPR0
);
665 writel((1 << LOCAL_DMA_CHANNEL_0_INTERRUPT_ENABLE
) |
666 readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
),
667 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
673 net2272_write(dev
, DMAREQ
,
674 (0 << DMA_BUFFER_VALID
) |
675 (1 << DMA_REQUEST_ENABLE
) |
676 (1 << DMA_CONTROL_DACK
) |
677 (dev
->dma_eot_polarity
<< EOT_POLARITY
) |
678 (dev
->dma_dack_polarity
<< DACK_POLARITY
) |
679 (dev
->dma_dreq_polarity
<< DREQ_POLARITY
) |
680 ((ep
>> 1) << DMA_ENDPOINT_SELECT
));
682 (void) net2272_read(dev
, SCRATCH
);
688 net2272_start_dma(struct net2272
*dev
)
690 /* start platform's dma controller */
691 #ifdef CONFIG_USB_PCI
692 switch (dev
->dev_id
) {
693 case PCI_DEVICE_ID_RDK1
:
694 writeb((1 << CHANNEL_ENABLE
) | (1 << CHANNEL_START
),
695 dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
701 /* returns 0 on success, else negative errno */
703 net2272_kick_dma(struct net2272_ep
*ep
, struct net2272_request
*req
)
708 if (!use_dma
|| (ep
->num
< 1) || (ep
->num
> 2) || !ep
->dma
)
711 /* don't use dma for odd-length transfers
712 * otherwise, we'd need to deal with the last byte with pio
714 if (req
->req
.length
& 1)
717 dev_vdbg(ep
->dev
->dev
, "kick_dma %s req %p dma %08llx\n",
718 ep
->ep
.name
, req
, (unsigned long long) req
->req
.dma
);
720 net2272_ep_write(ep
, EP_RSPSET
, 1 << ALT_NAK_OUT_PACKETS
);
722 /* The NET2272 can only use DMA on one endpoint at a time */
723 if (ep
->dev
->dma_busy
)
726 /* Make sure we only DMA an even number of bytes (we'll use
727 * pio to complete the transfer)
729 size
= req
->req
.length
;
732 /* device-to-host transfer */
734 /* initialize platform's dma controller */
735 if (net2272_request_dma(ep
->dev
, ep
->num
, req
->req
.dma
, size
, 0))
736 /* unable to obtain DMA channel; return error and use pio mode */
738 req
->req
.actual
+= size
;
740 /* host-to-device transfer */
742 tmp
= net2272_ep_read(ep
, EP_STAT0
);
744 /* initialize platform's dma controller */
745 if (net2272_request_dma(ep
->dev
, ep
->num
, req
->req
.dma
, size
, 1))
746 /* unable to obtain DMA channel; return error and use pio mode */
749 if (!(tmp
& (1 << BUFFER_EMPTY
)))
755 /* allow the endpoint's buffer to fill */
756 net2272_ep_write(ep
, EP_RSPCLR
, 1 << ALT_NAK_OUT_PACKETS
);
758 /* this transfer completed and data's already in the fifo
759 * return error so pio gets used.
761 if (tmp
& (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)) {
764 net2272_write(ep
->dev
, DMAREQ
,
765 (0 << DMA_BUFFER_VALID
) |
766 (0 << DMA_REQUEST_ENABLE
) |
767 (1 << DMA_CONTROL_DACK
) |
768 (ep
->dev
->dma_eot_polarity
<< EOT_POLARITY
) |
769 (ep
->dev
->dma_dack_polarity
<< DACK_POLARITY
) |
770 (ep
->dev
->dma_dreq_polarity
<< DREQ_POLARITY
) |
771 ((ep
->num
>> 1) << DMA_ENDPOINT_SELECT
));
777 /* Don't use per-packet interrupts: use dma interrupts only */
778 net2272_ep_write(ep
, EP_IRQENB
, 0);
780 net2272_start_dma(ep
->dev
);
785 static void net2272_cancel_dma(struct net2272
*dev
)
787 #ifdef CONFIG_USB_PCI
788 switch (dev
->dev_id
) {
789 case PCI_DEVICE_ID_RDK1
:
790 writeb(0, dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
791 writeb(1 << CHANNEL_ABORT
, dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
792 while (!(readb(dev
->rdk1
.plx9054_base_addr
+ DMACSR0
) &
793 (1 << CHANNEL_DONE
)))
794 continue; /* wait for dma to stabalize */
796 /* dma abort generates an interrupt */
797 writeb(1 << CHANNEL_CLEAR_INTERRUPT
,
798 dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
806 /*---------------------------------------------------------------------------*/
809 net2272_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
811 struct net2272_request
*req
;
812 struct net2272_ep
*ep
;
818 req
= container_of(_req
, struct net2272_request
, req
);
819 if (!_req
|| !_req
->complete
|| !_req
->buf
820 || !list_empty(&req
->queue
))
822 ep
= container_of(_ep
, struct net2272_ep
, ep
);
823 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
826 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
829 /* set up dma mapping in case the caller didn't */
830 if (use_dma
&& ep
->dma
) {
831 status
= usb_gadget_map_request(&dev
->gadget
, _req
,
837 dev_vdbg(dev
->dev
, "%s queue req %p, len %d buf %p dma %08llx %s\n",
838 _ep
->name
, _req
, _req
->length
, _req
->buf
,
839 (unsigned long long) _req
->dma
, _req
->zero
? "zero" : "!zero");
841 spin_lock_irqsave(&dev
->lock
, flags
);
843 _req
->status
= -EINPROGRESS
;
846 /* kickstart this i/o queue? */
847 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
848 /* maybe there's no control data, just status ack */
849 if (ep
->num
== 0 && _req
->length
== 0) {
850 net2272_done(ep
, req
, 0);
851 dev_vdbg(dev
->dev
, "%s status ack\n", ep
->ep
.name
);
855 /* Return zlp, don't let it block subsequent packets */
856 s
= net2272_ep_read(ep
, EP_STAT0
);
857 if (s
& (1 << BUFFER_EMPTY
)) {
858 /* Buffer is empty check for a blocking zlp, handle it */
859 if ((s
& (1 << NAK_OUT_PACKETS
)) &&
860 net2272_ep_read(ep
, EP_STAT1
) & (1 << LOCAL_OUT_ZLP
)) {
861 dev_dbg(dev
->dev
, "WARNING: returning ZLP short packet termination!\n");
863 * Request is going to terminate with a short packet ...
864 * hope the client is ready for it!
866 status
= net2272_read_fifo(ep
, req
);
867 /* clear short packet naking */
868 net2272_ep_write(ep
, EP_STAT0
, (1 << NAK_OUT_PACKETS
));
874 status
= net2272_kick_dma(ep
, req
);
877 /* dma failed (most likely in use by another endpoint)
883 status
= net2272_write_fifo(ep
, req
);
885 s
= net2272_ep_read(ep
, EP_STAT0
);
886 if ((s
& (1 << BUFFER_EMPTY
)) == 0)
887 status
= net2272_read_fifo(ep
, req
);
890 if (unlikely(status
!= 0)) {
898 list_add_tail(&req
->queue
, &ep
->queue
);
900 if (likely(!list_empty(&ep
->queue
)))
901 net2272_ep_write(ep
, EP_RSPCLR
, 1 << ALT_NAK_OUT_PACKETS
);
903 spin_unlock_irqrestore(&dev
->lock
, flags
);
908 /* dequeue ALL requests */
910 net2272_dequeue_all(struct net2272_ep
*ep
)
912 struct net2272_request
*req
;
914 /* called with spinlock held */
917 while (!list_empty(&ep
->queue
)) {
918 req
= list_entry(ep
->queue
.next
,
919 struct net2272_request
,
921 net2272_done(ep
, req
, -ESHUTDOWN
);
925 /* dequeue JUST ONE request */
927 net2272_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
929 struct net2272_ep
*ep
;
930 struct net2272_request
*req
;
934 ep
= container_of(_ep
, struct net2272_ep
, ep
);
935 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0) || !_req
)
938 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
939 stopped
= ep
->stopped
;
942 /* make sure it's still queued on this endpoint */
943 list_for_each_entry(req
, &ep
->queue
, queue
) {
944 if (&req
->req
== _req
)
947 if (&req
->req
!= _req
) {
948 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
952 /* queue head may be partially complete */
953 if (ep
->queue
.next
== &req
->queue
) {
954 dev_dbg(ep
->dev
->dev
, "unlink (%s) pio\n", _ep
->name
);
955 net2272_done(ep
, req
, -ECONNRESET
);
958 ep
->stopped
= stopped
;
960 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
964 /*---------------------------------------------------------------------------*/
967 net2272_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
969 struct net2272_ep
*ep
;
973 ep
= container_of(_ep
, struct net2272_ep
, ep
);
974 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
976 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
978 if (ep
->desc
/* not ep0 */ && usb_endpoint_xfer_isoc(ep
->desc
))
981 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
982 if (!list_empty(&ep
->queue
))
984 else if (ep
->is_in
&& value
&& net2272_fifo_status(_ep
) != 0)
987 dev_vdbg(ep
->dev
->dev
, "%s %s %s\n", _ep
->name
,
988 value
? "set" : "clear",
989 wedged
? "wedge" : "halt");
993 ep
->dev
->protocol_stall
= 1;
1003 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1009 net2272_set_halt(struct usb_ep
*_ep
, int value
)
1011 return net2272_set_halt_and_wedge(_ep
, value
, 0);
1015 net2272_set_wedge(struct usb_ep
*_ep
)
1017 if (!_ep
|| _ep
->name
== ep0name
)
1019 return net2272_set_halt_and_wedge(_ep
, 1, 1);
1023 net2272_fifo_status(struct usb_ep
*_ep
)
1025 struct net2272_ep
*ep
;
1028 ep
= container_of(_ep
, struct net2272_ep
, ep
);
1029 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1031 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1034 avail
= net2272_ep_read(ep
, EP_AVAIL1
) << 8;
1035 avail
|= net2272_ep_read(ep
, EP_AVAIL0
);
1036 if (avail
> ep
->fifo_size
)
1039 avail
= ep
->fifo_size
- avail
;
1044 net2272_fifo_flush(struct usb_ep
*_ep
)
1046 struct net2272_ep
*ep
;
1048 ep
= container_of(_ep
, struct net2272_ep
, ep
);
1049 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1051 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1054 net2272_ep_write(ep
, EP_STAT1
, 1 << BUFFER_FLUSH
);
1057 static const struct usb_ep_ops net2272_ep_ops
= {
1058 .enable
= net2272_enable
,
1059 .disable
= net2272_disable
,
1061 .alloc_request
= net2272_alloc_request
,
1062 .free_request
= net2272_free_request
,
1064 .queue
= net2272_queue
,
1065 .dequeue
= net2272_dequeue
,
1067 .set_halt
= net2272_set_halt
,
1068 .set_wedge
= net2272_set_wedge
,
1069 .fifo_status
= net2272_fifo_status
,
1070 .fifo_flush
= net2272_fifo_flush
,
1073 /*---------------------------------------------------------------------------*/
1076 net2272_get_frame(struct usb_gadget
*_gadget
)
1078 struct net2272
*dev
;
1079 unsigned long flags
;
1084 dev
= container_of(_gadget
, struct net2272
, gadget
);
1085 spin_lock_irqsave(&dev
->lock
, flags
);
1087 ret
= net2272_read(dev
, FRAME1
) << 8;
1088 ret
|= net2272_read(dev
, FRAME0
);
1090 spin_unlock_irqrestore(&dev
->lock
, flags
);
1095 net2272_wakeup(struct usb_gadget
*_gadget
)
1097 struct net2272
*dev
;
1099 unsigned long flags
;
1103 dev
= container_of(_gadget
, struct net2272
, gadget
);
1105 spin_lock_irqsave(&dev
->lock
, flags
);
1106 tmp
= net2272_read(dev
, USBCTL0
);
1107 if (tmp
& (1 << IO_WAKEUP_ENABLE
))
1108 net2272_write(dev
, USBCTL1
, (1 << GENERATE_RESUME
));
1110 spin_unlock_irqrestore(&dev
->lock
, flags
);
1116 net2272_set_selfpowered(struct usb_gadget
*_gadget
, int value
)
1121 _gadget
->is_selfpowered
= (value
!= 0);
1127 net2272_pullup(struct usb_gadget
*_gadget
, int is_on
)
1129 struct net2272
*dev
;
1131 unsigned long flags
;
1135 dev
= container_of(_gadget
, struct net2272
, gadget
);
1137 spin_lock_irqsave(&dev
->lock
, flags
);
1138 tmp
= net2272_read(dev
, USBCTL0
);
1139 dev
->softconnect
= (is_on
!= 0);
1141 tmp
|= (1 << USB_DETECT_ENABLE
);
1143 tmp
&= ~(1 << USB_DETECT_ENABLE
);
1144 net2272_write(dev
, USBCTL0
, tmp
);
1145 spin_unlock_irqrestore(&dev
->lock
, flags
);
1150 static int net2272_start(struct usb_gadget
*_gadget
,
1151 struct usb_gadget_driver
*driver
);
1152 static int net2272_stop(struct usb_gadget
*_gadget
);
1154 static const struct usb_gadget_ops net2272_ops
= {
1155 .get_frame
= net2272_get_frame
,
1156 .wakeup
= net2272_wakeup
,
1157 .set_selfpowered
= net2272_set_selfpowered
,
1158 .pullup
= net2272_pullup
,
1159 .udc_start
= net2272_start
,
1160 .udc_stop
= net2272_stop
,
1163 /*---------------------------------------------------------------------------*/
1166 registers_show(struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1168 struct net2272
*dev
;
1171 unsigned long flags
;
1176 dev
= dev_get_drvdata(_dev
);
1179 spin_lock_irqsave(&dev
->lock
, flags
);
1182 s
= dev
->driver
->driver
.name
;
1186 /* Main Control Registers */
1187 t
= scnprintf(next
, size
, "%s version %s,"
1188 "chiprev %02x, locctl %02x\n"
1189 "irqenb0 %02x irqenb1 %02x "
1190 "irqstat0 %02x irqstat1 %02x\n",
1191 driver_name
, driver_vers
, dev
->chiprev
,
1192 net2272_read(dev
, LOCCTL
),
1193 net2272_read(dev
, IRQENB0
),
1194 net2272_read(dev
, IRQENB1
),
1195 net2272_read(dev
, IRQSTAT0
),
1196 net2272_read(dev
, IRQSTAT1
));
1201 t1
= net2272_read(dev
, DMAREQ
);
1202 t
= scnprintf(next
, size
, "\ndmareq %02x: %s %s%s%s%s\n",
1203 t1
, ep_name
[(t1
& 0x01) + 1],
1204 t1
& (1 << DMA_CONTROL_DACK
) ? "dack " : "",
1205 t1
& (1 << DMA_REQUEST_ENABLE
) ? "reqenb " : "",
1206 t1
& (1 << DMA_REQUEST
) ? "req " : "",
1207 t1
& (1 << DMA_BUFFER_VALID
) ? "valid " : "");
1211 /* USB Control Registers */
1212 t1
= net2272_read(dev
, USBCTL1
);
1213 if (t1
& (1 << VBUS_PIN
)) {
1214 if (t1
& (1 << USB_HIGH_SPEED
))
1216 else if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1222 t
= scnprintf(next
, size
,
1223 "usbctl0 %02x usbctl1 %02x addr 0x%02x (%s)\n",
1224 net2272_read(dev
, USBCTL0
), t1
,
1225 net2272_read(dev
, OURADDR
), s
);
1229 /* Endpoint Registers */
1230 for (i
= 0; i
< 4; ++i
) {
1231 struct net2272_ep
*ep
;
1237 t1
= net2272_ep_read(ep
, EP_CFG
);
1238 t2
= net2272_ep_read(ep
, EP_RSPSET
);
1239 t
= scnprintf(next
, size
,
1240 "\n%s\tcfg %02x rsp (%02x) %s%s%s%s%s%s%s%s"
1242 ep
->ep
.name
, t1
, t2
,
1243 (t2
& (1 << ALT_NAK_OUT_PACKETS
)) ? "NAK " : "",
1244 (t2
& (1 << HIDE_STATUS_PHASE
)) ? "hide " : "",
1245 (t2
& (1 << AUTOVALIDATE
)) ? "auto " : "",
1246 (t2
& (1 << INTERRUPT_MODE
)) ? "interrupt " : "",
1247 (t2
& (1 << CONTROL_STATUS_PHASE_HANDSHAKE
)) ? "status " : "",
1248 (t2
& (1 << NAK_OUT_PACKETS_MODE
)) ? "NAKmode " : "",
1249 (t2
& (1 << ENDPOINT_TOGGLE
)) ? "DATA1 " : "DATA0 ",
1250 (t2
& (1 << ENDPOINT_HALT
)) ? "HALT " : "",
1251 net2272_ep_read(ep
, EP_IRQENB
));
1255 t
= scnprintf(next
, size
,
1256 "\tstat0 %02x stat1 %02x avail %04x "
1258 net2272_ep_read(ep
, EP_STAT0
),
1259 net2272_ep_read(ep
, EP_STAT1
),
1260 (net2272_ep_read(ep
, EP_AVAIL1
) << 8) | net2272_ep_read(ep
, EP_AVAIL0
),
1262 ep
->is_in
? "in" : "out",
1263 type_string(t1
>> 5),
1264 ep
->stopped
? "*" : "");
1268 t
= scnprintf(next
, size
,
1269 "\tep_transfer %06x\n",
1270 ((net2272_ep_read(ep
, EP_TRANSFER2
) & 0xff) << 16) |
1271 ((net2272_ep_read(ep
, EP_TRANSFER1
) & 0xff) << 8) |
1272 ((net2272_ep_read(ep
, EP_TRANSFER0
) & 0xff)));
1276 t1
= net2272_ep_read(ep
, EP_BUFF_STATES
) & 0x03;
1277 t2
= (net2272_ep_read(ep
, EP_BUFF_STATES
) >> 2) & 0x03;
1278 t
= scnprintf(next
, size
,
1279 "\tbuf-a %s buf-b %s\n",
1280 buf_state_string(t1
),
1281 buf_state_string(t2
));
1286 spin_unlock_irqrestore(&dev
->lock
, flags
);
1288 return PAGE_SIZE
- size
;
1290 static DEVICE_ATTR_RO(registers
);
1292 /*---------------------------------------------------------------------------*/
1295 net2272_set_fifo_mode(struct net2272
*dev
, int mode
)
1299 tmp
= net2272_read(dev
, LOCCTL
) & 0x3f;
1301 net2272_write(dev
, LOCCTL
, tmp
);
1303 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
1305 /* always ep-a, ep-c ... maybe not ep-b */
1306 list_add_tail(&dev
->ep
[1].ep
.ep_list
, &dev
->gadget
.ep_list
);
1310 list_add_tail(&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1311 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 512;
1314 list_add_tail(&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1315 dev
->ep
[1].fifo_size
= 1024;
1316 dev
->ep
[2].fifo_size
= 512;
1319 list_add_tail(&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1320 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 1024;
1323 dev
->ep
[1].fifo_size
= 1024;
1327 /* ep-c is always 2 512 byte buffers */
1328 list_add_tail(&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1329 dev
->ep
[3].fifo_size
= 512;
1332 /*---------------------------------------------------------------------------*/
1335 net2272_usb_reset(struct net2272
*dev
)
1337 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1339 net2272_cancel_dma(dev
);
1341 net2272_write(dev
, IRQENB0
, 0);
1342 net2272_write(dev
, IRQENB1
, 0);
1344 /* clear irq state */
1345 net2272_write(dev
, IRQSTAT0
, 0xff);
1346 net2272_write(dev
, IRQSTAT1
, ~(1 << SUSPEND_REQUEST_INTERRUPT
));
1348 net2272_write(dev
, DMAREQ
,
1349 (0 << DMA_BUFFER_VALID
) |
1350 (0 << DMA_REQUEST_ENABLE
) |
1351 (1 << DMA_CONTROL_DACK
) |
1352 (dev
->dma_eot_polarity
<< EOT_POLARITY
) |
1353 (dev
->dma_dack_polarity
<< DACK_POLARITY
) |
1354 (dev
->dma_dreq_polarity
<< DREQ_POLARITY
) |
1355 ((dma_ep
>> 1) << DMA_ENDPOINT_SELECT
));
1357 net2272_cancel_dma(dev
);
1358 net2272_set_fifo_mode(dev
, (fifo_mode
<= 3) ? fifo_mode
: 0);
1360 /* Set the NET2272 ep fifo data width to 16-bit mode and for correct byte swapping
1361 * note that the higher level gadget drivers are expected to convert data to little endian.
1362 * Enable byte swap for your local bus/cpu if needed by setting BYTE_SWAP in LOCCTL here
1364 net2272_write(dev
, LOCCTL
, net2272_read(dev
, LOCCTL
) | (1 << DATA_WIDTH
));
1365 net2272_write(dev
, LOCCTL1
, (dma_mode
<< DMA_MODE
));
1369 net2272_usb_reinit(struct net2272
*dev
)
1373 /* basic endpoint init */
1374 for (i
= 0; i
< 4; ++i
) {
1375 struct net2272_ep
*ep
= &dev
->ep
[i
];
1377 ep
->ep
.name
= ep_name
[i
];
1382 if (use_dma
&& ep
->num
== dma_ep
)
1385 if (i
> 0 && i
<= 3)
1386 ep
->fifo_size
= 512;
1389 net2272_ep_reset(ep
);
1392 ep
->ep
.caps
.type_control
= true;
1394 ep
->ep
.caps
.type_iso
= true;
1395 ep
->ep
.caps
.type_bulk
= true;
1396 ep
->ep
.caps
.type_int
= true;
1399 ep
->ep
.caps
.dir_in
= true;
1400 ep
->ep
.caps
.dir_out
= true;
1402 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
, 64);
1404 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1405 dev
->ep
[0].stopped
= 0;
1406 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
1410 net2272_ep0_start(struct net2272
*dev
)
1412 struct net2272_ep
*ep0
= &dev
->ep
[0];
1414 net2272_ep_write(ep0
, EP_RSPSET
,
1415 (1 << NAK_OUT_PACKETS_MODE
) |
1416 (1 << ALT_NAK_OUT_PACKETS
));
1417 net2272_ep_write(ep0
, EP_RSPCLR
,
1418 (1 << HIDE_STATUS_PHASE
) |
1419 (1 << CONTROL_STATUS_PHASE_HANDSHAKE
));
1420 net2272_write(dev
, USBCTL0
,
1421 (dev
->softconnect
<< USB_DETECT_ENABLE
) |
1422 (1 << USB_ROOT_PORT_WAKEUP_ENABLE
) |
1423 (1 << IO_WAKEUP_ENABLE
));
1424 net2272_write(dev
, IRQENB0
,
1425 (1 << SETUP_PACKET_INTERRUPT_ENABLE
) |
1426 (1 << ENDPOINT_0_INTERRUPT_ENABLE
) |
1427 (1 << DMA_DONE_INTERRUPT_ENABLE
));
1428 net2272_write(dev
, IRQENB1
,
1429 (1 << VBUS_INTERRUPT_ENABLE
) |
1430 (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE
) |
1431 (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
));
1434 /* when a driver is successfully registered, it will receive
1435 * control requests including set_configuration(), which enables
1436 * non-control requests. then usb traffic follows until a
1437 * disconnect is reported. then a host may connect again, or
1438 * the driver might get unbound.
1440 static int net2272_start(struct usb_gadget
*_gadget
,
1441 struct usb_gadget_driver
*driver
)
1443 struct net2272
*dev
;
1446 if (!driver
|| !driver
->setup
||
1447 driver
->max_speed
!= USB_SPEED_HIGH
)
1450 dev
= container_of(_gadget
, struct net2272
, gadget
);
1452 for (i
= 0; i
< 4; ++i
)
1453 dev
->ep
[i
].irqs
= 0;
1454 /* hook up the driver ... */
1455 dev
->softconnect
= 1;
1456 driver
->driver
.bus
= NULL
;
1457 dev
->driver
= driver
;
1459 /* ... then enable host detection and ep0; and we're ready
1460 * for set_configuration as well as eventual disconnect.
1462 net2272_ep0_start(dev
);
1468 stop_activity(struct net2272
*dev
, struct usb_gadget_driver
*driver
)
1472 /* don't disconnect if it's not connected */
1473 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1476 /* stop hardware; prevent new request submissions;
1477 * and kill any outstanding requests.
1479 net2272_usb_reset(dev
);
1480 for (i
= 0; i
< 4; ++i
)
1481 net2272_dequeue_all(&dev
->ep
[i
]);
1483 /* report disconnect; the driver is already quiesced */
1485 spin_unlock(&dev
->lock
);
1486 driver
->disconnect(&dev
->gadget
);
1487 spin_lock(&dev
->lock
);
1490 net2272_usb_reinit(dev
);
1493 static int net2272_stop(struct usb_gadget
*_gadget
)
1495 struct net2272
*dev
;
1496 unsigned long flags
;
1498 dev
= container_of(_gadget
, struct net2272
, gadget
);
1500 spin_lock_irqsave(&dev
->lock
, flags
);
1501 stop_activity(dev
, NULL
);
1502 spin_unlock_irqrestore(&dev
->lock
, flags
);
1509 /*---------------------------------------------------------------------------*/
1510 /* handle ep-a/ep-b dma completions */
1512 net2272_handle_dma(struct net2272_ep
*ep
)
1514 struct net2272_request
*req
;
1518 if (!list_empty(&ep
->queue
))
1519 req
= list_entry(ep
->queue
.next
,
1520 struct net2272_request
, queue
);
1524 dev_vdbg(ep
->dev
->dev
, "handle_dma %s req %p\n", ep
->ep
.name
, req
);
1526 /* Ensure DREQ is de-asserted */
1527 net2272_write(ep
->dev
, DMAREQ
,
1528 (0 << DMA_BUFFER_VALID
)
1529 | (0 << DMA_REQUEST_ENABLE
)
1530 | (1 << DMA_CONTROL_DACK
)
1531 | (ep
->dev
->dma_eot_polarity
<< EOT_POLARITY
)
1532 | (ep
->dev
->dma_dack_polarity
<< DACK_POLARITY
)
1533 | (ep
->dev
->dma_dreq_polarity
<< DREQ_POLARITY
)
1534 | (ep
->dma
<< DMA_ENDPOINT_SELECT
));
1536 ep
->dev
->dma_busy
= 0;
1538 net2272_ep_write(ep
, EP_IRQENB
,
1539 (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
)
1540 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
)
1541 | net2272_ep_read(ep
, EP_IRQENB
));
1543 /* device-to-host transfer completed */
1545 /* validate a short packet or zlp if necessary */
1546 if ((req
->req
.length
% ep
->ep
.maxpacket
!= 0) ||
1548 set_fifo_bytecount(ep
, 0);
1550 net2272_done(ep
, req
, 0);
1551 if (!list_empty(&ep
->queue
)) {
1552 req
= list_entry(ep
->queue
.next
,
1553 struct net2272_request
, queue
);
1554 status
= net2272_kick_dma(ep
, req
);
1556 net2272_pio_advance(ep
);
1559 /* host-to-device transfer completed */
1561 /* terminated with a short packet? */
1562 if (net2272_read(ep
->dev
, IRQSTAT0
) &
1563 (1 << DMA_DONE_INTERRUPT
)) {
1564 /* abort system dma */
1565 net2272_cancel_dma(ep
->dev
);
1568 /* EP_TRANSFER will contain the number of bytes
1569 * actually received.
1570 * NOTE: There is no overflow detection on EP_TRANSFER:
1571 * We can't deal with transfers larger than 2^24 bytes!
1573 len
= (net2272_ep_read(ep
, EP_TRANSFER2
) << 16)
1574 | (net2272_ep_read(ep
, EP_TRANSFER1
) << 8)
1575 | (net2272_ep_read(ep
, EP_TRANSFER0
));
1580 req
->req
.actual
+= len
;
1582 /* get any remaining data */
1583 net2272_pio_advance(ep
);
1587 /*---------------------------------------------------------------------------*/
1590 net2272_handle_ep(struct net2272_ep
*ep
)
1592 struct net2272_request
*req
;
1595 if (!list_empty(&ep
->queue
))
1596 req
= list_entry(ep
->queue
.next
,
1597 struct net2272_request
, queue
);
1601 /* ack all, and handle what we care about */
1602 stat0
= net2272_ep_read(ep
, EP_STAT0
);
1603 stat1
= net2272_ep_read(ep
, EP_STAT1
);
1606 dev_vdbg(ep
->dev
->dev
, "%s ack ep_stat0 %02x, ep_stat1 %02x, req %p\n",
1607 ep
->ep
.name
, stat0
, stat1
, req
? &req
->req
: NULL
);
1609 net2272_ep_write(ep
, EP_STAT0
, stat0
&
1610 ~((1 << NAK_OUT_PACKETS
)
1611 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)));
1612 net2272_ep_write(ep
, EP_STAT1
, stat1
);
1614 /* data packet(s) received (in the fifo, OUT)
1615 * direction must be validated, otherwise control read status phase
1616 * could be interpreted as a valid packet
1618 if (!ep
->is_in
&& (stat0
& (1 << DATA_PACKET_RECEIVED_INTERRUPT
)))
1619 net2272_pio_advance(ep
);
1620 /* data packet(s) transmitted (IN) */
1621 else if (stat0
& (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
))
1622 net2272_pio_advance(ep
);
1625 static struct net2272_ep
*
1626 net2272_get_ep_by_addr(struct net2272
*dev
, u16 wIndex
)
1628 struct net2272_ep
*ep
;
1630 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
1633 list_for_each_entry(ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
1634 u8 bEndpointAddress
;
1638 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
1639 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
1641 if ((wIndex
& 0x0f) == (bEndpointAddress
& 0x0f))
1652 * JJJJJJJKKKKKKK * 8
1654 * {JKKKKKKK * 10}, JK
1656 static const u8 net2272_test_packet
[] = {
1657 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1658 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1659 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1660 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1661 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1662 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFD, 0x7E
1666 net2272_set_test_mode(struct net2272
*dev
, int mode
)
1670 /* Disable all net2272 interrupts:
1671 * Nothing but a power cycle should stop the test.
1673 net2272_write(dev
, IRQENB0
, 0x00);
1674 net2272_write(dev
, IRQENB1
, 0x00);
1676 /* Force tranceiver to high-speed */
1677 net2272_write(dev
, XCVRDIAG
, 1 << FORCE_HIGH_SPEED
);
1679 net2272_write(dev
, PAGESEL
, 0);
1680 net2272_write(dev
, EP_STAT0
, 1 << DATA_PACKET_TRANSMITTED_INTERRUPT
);
1681 net2272_write(dev
, EP_RSPCLR
,
1682 (1 << CONTROL_STATUS_PHASE_HANDSHAKE
)
1683 | (1 << HIDE_STATUS_PHASE
));
1684 net2272_write(dev
, EP_CFG
, 1 << ENDPOINT_DIRECTION
);
1685 net2272_write(dev
, EP_STAT1
, 1 << BUFFER_FLUSH
);
1687 /* wait for status phase to complete */
1688 while (!(net2272_read(dev
, EP_STAT0
) &
1689 (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)))
1692 /* Enable test mode */
1693 net2272_write(dev
, USBTEST
, mode
);
1695 /* load test packet */
1696 if (mode
== TEST_PACKET
) {
1697 /* switch to 8 bit mode */
1698 net2272_write(dev
, LOCCTL
, net2272_read(dev
, LOCCTL
) &
1699 ~(1 << DATA_WIDTH
));
1701 for (i
= 0; i
< sizeof(net2272_test_packet
); ++i
)
1702 net2272_write(dev
, EP_DATA
, net2272_test_packet
[i
]);
1704 /* Validate test packet */
1705 net2272_write(dev
, EP_TRANSFER0
, 0);
1710 net2272_handle_stat0_irqs(struct net2272
*dev
, u8 stat
)
1712 struct net2272_ep
*ep
;
1715 /* starting a control request? */
1716 if (unlikely(stat
& (1 << SETUP_PACKET_INTERRUPT
))) {
1719 struct usb_ctrlrequest r
;
1722 struct net2272_request
*req
;
1724 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1725 if (net2272_read(dev
, USBCTL1
) & (1 << USB_HIGH_SPEED
))
1726 dev
->gadget
.speed
= USB_SPEED_HIGH
;
1728 dev
->gadget
.speed
= USB_SPEED_FULL
;
1729 dev_dbg(dev
->dev
, "%s\n",
1730 usb_speed_string(dev
->gadget
.speed
));
1736 /* make sure any leftover interrupt state is cleared */
1737 stat
&= ~(1 << ENDPOINT_0_INTERRUPT
);
1738 while (!list_empty(&ep
->queue
)) {
1739 req
= list_entry(ep
->queue
.next
,
1740 struct net2272_request
, queue
);
1741 net2272_done(ep
, req
,
1742 (req
->req
.actual
== req
->req
.length
) ? 0 : -EPROTO
);
1745 dev
->protocol_stall
= 0;
1746 net2272_ep_write(ep
, EP_STAT0
,
1747 (1 << DATA_IN_TOKEN_INTERRUPT
)
1748 | (1 << DATA_OUT_TOKEN_INTERRUPT
)
1749 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
1750 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
1751 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
));
1752 net2272_ep_write(ep
, EP_STAT1
,
1754 | (1 << USB_OUT_ACK_SENT
)
1755 | (1 << USB_OUT_NAK_SENT
)
1756 | (1 << USB_IN_ACK_RCVD
)
1757 | (1 << USB_IN_NAK_SENT
)
1758 | (1 << USB_STALL_SENT
)
1759 | (1 << LOCAL_OUT_ZLP
));
1762 * Ensure Control Read pre-validation setting is beyond maximum size
1763 * - Control Writes can leave non-zero values in EP_TRANSFER. If
1764 * an EP0 transfer following the Control Write is a Control Read,
1765 * the NET2272 sees the non-zero EP_TRANSFER as an unexpected
1766 * pre-validation count.
1767 * - Setting EP_TRANSFER beyond the maximum EP0 transfer size ensures
1768 * the pre-validation count cannot cause an unexpected validatation
1770 net2272_write(dev
, PAGESEL
, 0);
1771 net2272_write(dev
, EP_TRANSFER2
, 0xff);
1772 net2272_write(dev
, EP_TRANSFER1
, 0xff);
1773 net2272_write(dev
, EP_TRANSFER0
, 0xff);
1775 u
.raw
[0] = net2272_read(dev
, SETUP0
);
1776 u
.raw
[1] = net2272_read(dev
, SETUP1
);
1777 u
.raw
[2] = net2272_read(dev
, SETUP2
);
1778 u
.raw
[3] = net2272_read(dev
, SETUP3
);
1779 u
.raw
[4] = net2272_read(dev
, SETUP4
);
1780 u
.raw
[5] = net2272_read(dev
, SETUP5
);
1781 u
.raw
[6] = net2272_read(dev
, SETUP6
);
1782 u
.raw
[7] = net2272_read(dev
, SETUP7
);
1784 * If you have a big endian cpu make sure le16_to_cpus
1785 * performs the proper byte swapping here...
1787 le16_to_cpus(&u
.r
.wValue
);
1788 le16_to_cpus(&u
.r
.wIndex
);
1789 le16_to_cpus(&u
.r
.wLength
);
1792 net2272_write(dev
, IRQSTAT0
, 1 << SETUP_PACKET_INTERRUPT
);
1793 stat
^= (1 << SETUP_PACKET_INTERRUPT
);
1795 /* watch control traffic at the token level, and force
1796 * synchronization before letting the status phase happen.
1798 ep
->is_in
= (u
.r
.bRequestType
& USB_DIR_IN
) != 0;
1800 scratch
= (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
)
1801 | (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE
)
1802 | (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE
);
1803 stop_out_naking(ep
);
1805 scratch
= (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
)
1806 | (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE
)
1807 | (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE
);
1808 net2272_ep_write(ep
, EP_IRQENB
, scratch
);
1810 if ((u
.r
.bRequestType
& USB_TYPE_MASK
) != USB_TYPE_STANDARD
)
1812 switch (u
.r
.bRequest
) {
1813 case USB_REQ_GET_STATUS
: {
1814 struct net2272_ep
*e
;
1817 switch (u
.r
.bRequestType
& USB_RECIP_MASK
) {
1818 case USB_RECIP_ENDPOINT
:
1819 e
= net2272_get_ep_by_addr(dev
, u
.r
.wIndex
);
1820 if (!e
|| u
.r
.wLength
> 2)
1822 if (net2272_ep_read(e
, EP_RSPSET
) & (1 << ENDPOINT_HALT
))
1823 status
= cpu_to_le16(1);
1825 status
= cpu_to_le16(0);
1827 /* don't bother with a request object! */
1828 net2272_ep_write(&dev
->ep
[0], EP_IRQENB
, 0);
1829 writew(status
, net2272_reg_addr(dev
, EP_DATA
));
1830 set_fifo_bytecount(&dev
->ep
[0], 0);
1832 dev_vdbg(dev
->dev
, "%s stat %02x\n",
1833 ep
->ep
.name
, status
);
1834 goto next_endpoints
;
1835 case USB_RECIP_DEVICE
:
1836 if (u
.r
.wLength
> 2)
1838 if (dev
->gadget
.is_selfpowered
)
1839 status
= (1 << USB_DEVICE_SELF_POWERED
);
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
, "device stat %02x\n", status
);
1847 goto next_endpoints
;
1848 case USB_RECIP_INTERFACE
:
1849 if (u
.r
.wLength
> 2)
1852 /* don't bother with a request object! */
1853 net2272_ep_write(&dev
->ep
[0], EP_IRQENB
, 0);
1854 writew(status
, net2272_reg_addr(dev
, EP_DATA
));
1855 set_fifo_bytecount(&dev
->ep
[0], 0);
1857 dev_vdbg(dev
->dev
, "interface status %02x\n", status
);
1858 goto next_endpoints
;
1863 case USB_REQ_CLEAR_FEATURE
: {
1864 struct net2272_ep
*e
;
1866 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
1868 if (u
.r
.wValue
!= USB_ENDPOINT_HALT
||
1871 e
= net2272_get_ep_by_addr(dev
, u
.r
.wIndex
);
1875 dev_vdbg(dev
->dev
, "%s wedged, halt not cleared\n",
1878 dev_vdbg(dev
->dev
, "%s clear halt\n", ep
->ep
.name
);
1882 goto next_endpoints
;
1884 case USB_REQ_SET_FEATURE
: {
1885 struct net2272_ep
*e
;
1887 if (u
.r
.bRequestType
== USB_RECIP_DEVICE
) {
1888 if (u
.r
.wIndex
!= NORMAL_OPERATION
)
1889 net2272_set_test_mode(dev
, (u
.r
.wIndex
>> 8));
1891 dev_vdbg(dev
->dev
, "test mode: %d\n", u
.r
.wIndex
);
1892 goto next_endpoints
;
1893 } else if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
1895 if (u
.r
.wValue
!= USB_ENDPOINT_HALT
||
1898 e
= net2272_get_ep_by_addr(dev
, u
.r
.wIndex
);
1903 dev_vdbg(dev
->dev
, "%s set halt\n", ep
->ep
.name
);
1904 goto next_endpoints
;
1906 case USB_REQ_SET_ADDRESS
: {
1907 net2272_write(dev
, OURADDR
, u
.r
.wValue
& 0xff);
1913 dev_vdbg(dev
->dev
, "setup %02x.%02x v%04x i%04x "
1915 u
.r
.bRequestType
, u
.r
.bRequest
,
1916 u
.r
.wValue
, u
.r
.wIndex
,
1917 net2272_ep_read(ep
, EP_CFG
));
1918 spin_unlock(&dev
->lock
);
1919 tmp
= dev
->driver
->setup(&dev
->gadget
, &u
.r
);
1920 spin_lock(&dev
->lock
);
1923 /* stall ep0 on error */
1926 dev_vdbg(dev
->dev
, "req %02x.%02x protocol STALL; stat %d\n",
1927 u
.r
.bRequestType
, u
.r
.bRequest
, tmp
);
1928 dev
->protocol_stall
= 1;
1930 /* endpoint dma irq? */
1931 } else if (stat
& (1 << DMA_DONE_INTERRUPT
)) {
1932 net2272_cancel_dma(dev
);
1933 net2272_write(dev
, IRQSTAT0
, 1 << DMA_DONE_INTERRUPT
);
1934 stat
&= ~(1 << DMA_DONE_INTERRUPT
);
1935 num
= (net2272_read(dev
, DMAREQ
) & (1 << DMA_ENDPOINT_SELECT
))
1939 net2272_handle_dma(ep
);
1943 /* endpoint data irq? */
1944 scratch
= stat
& 0x0f;
1946 for (num
= 0; scratch
; num
++) {
1949 /* does this endpoint's FIFO and queue need tending? */
1951 if ((scratch
& t
) == 0)
1956 net2272_handle_ep(ep
);
1959 /* some interrupts we can just ignore */
1960 stat
&= ~(1 << SOF_INTERRUPT
);
1963 dev_dbg(dev
->dev
, "unhandled irqstat0 %02x\n", stat
);
1967 net2272_handle_stat1_irqs(struct net2272
*dev
, u8 stat
)
1971 /* after disconnect there's nothing else to do! */
1972 tmp
= (1 << VBUS_INTERRUPT
) | (1 << ROOT_PORT_RESET_INTERRUPT
);
1973 mask
= (1 << USB_HIGH_SPEED
) | (1 << USB_FULL_SPEED
);
1977 bool disconnect
= false;
1980 * Ignore disconnects and resets if the speed hasn't been set.
1981 * VBUS can bounce and there's always an initial reset.
1983 net2272_write(dev
, IRQSTAT1
, tmp
);
1984 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
1985 if ((stat
& (1 << VBUS_INTERRUPT
)) &&
1986 (net2272_read(dev
, USBCTL1
) &
1987 (1 << VBUS_PIN
)) == 0) {
1989 dev_dbg(dev
->dev
, "disconnect %s\n",
1990 dev
->driver
->driver
.name
);
1991 } else if ((stat
& (1 << ROOT_PORT_RESET_INTERRUPT
)) &&
1992 (net2272_read(dev
, USBCTL1
) & mask
)
1995 dev_dbg(dev
->dev
, "reset %s\n",
1996 dev
->driver
->driver
.name
);
1999 if (disconnect
|| reset
) {
2000 stop_activity(dev
, dev
->driver
);
2001 net2272_ep0_start(dev
);
2002 spin_unlock(&dev
->lock
);
2004 usb_gadget_udc_reset
2005 (&dev
->gadget
, dev
->driver
);
2007 (dev
->driver
->disconnect
)
2009 spin_lock(&dev
->lock
);
2019 tmp
= (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT
);
2021 net2272_write(dev
, IRQSTAT1
, tmp
);
2022 if (stat
& (1 << SUSPEND_REQUEST_INTERRUPT
)) {
2023 if (dev
->driver
->suspend
)
2024 dev
->driver
->suspend(&dev
->gadget
);
2025 if (!enable_suspend
) {
2026 stat
&= ~(1 << SUSPEND_REQUEST_INTERRUPT
);
2027 dev_dbg(dev
->dev
, "Suspend disabled, ignoring\n");
2030 if (dev
->driver
->resume
)
2031 dev
->driver
->resume(&dev
->gadget
);
2036 /* clear any other status/irqs */
2038 net2272_write(dev
, IRQSTAT1
, stat
);
2040 /* some status we can just ignore */
2041 stat
&= ~((1 << CONTROL_STATUS_INTERRUPT
)
2042 | (1 << SUSPEND_REQUEST_INTERRUPT
)
2043 | (1 << RESUME_INTERRUPT
));
2047 dev_dbg(dev
->dev
, "unhandled irqstat1 %02x\n", stat
);
2050 static irqreturn_t
net2272_irq(int irq
, void *_dev
)
2052 struct net2272
*dev
= _dev
;
2053 #if defined(PLX_PCI_RDK) || defined(PLX_PCI_RDK2)
2056 #if defined(PLX_PCI_RDK)
2059 spin_lock(&dev
->lock
);
2060 #if defined(PLX_PCI_RDK)
2061 intcsr
= readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2063 if ((intcsr
& LOCAL_INTERRUPT_TEST
) == LOCAL_INTERRUPT_TEST
) {
2064 writel(intcsr
& ~(1 << PCI_INTERRUPT_ENABLE
),
2065 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2066 net2272_handle_stat1_irqs(dev
, net2272_read(dev
, IRQSTAT1
));
2067 net2272_handle_stat0_irqs(dev
, net2272_read(dev
, IRQSTAT0
));
2068 intcsr
= readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2069 writel(intcsr
| (1 << PCI_INTERRUPT_ENABLE
),
2070 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2072 if ((intcsr
& DMA_CHANNEL_0_TEST
) == DMA_CHANNEL_0_TEST
) {
2073 writeb((1 << CHANNEL_CLEAR_INTERRUPT
| (0 << CHANNEL_ENABLE
)),
2074 dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
2076 dmareq
= net2272_read(dev
, DMAREQ
);
2078 net2272_handle_dma(&dev
->ep
[2]);
2080 net2272_handle_dma(&dev
->ep
[1]);
2083 #if defined(PLX_PCI_RDK2)
2084 /* see if PCI int for us by checking irqstat */
2085 intcsr
= readl(dev
->rdk2
.fpga_base_addr
+ RDK2_IRQSTAT
);
2086 if (!intcsr
& (1 << NET2272_PCI_IRQ
)) {
2087 spin_unlock(&dev
->lock
);
2090 /* check dma interrupts */
2092 /* Platform/devcice interrupt handler */
2093 #if !defined(PLX_PCI_RDK)
2094 net2272_handle_stat1_irqs(dev
, net2272_read(dev
, IRQSTAT1
));
2095 net2272_handle_stat0_irqs(dev
, net2272_read(dev
, IRQSTAT0
));
2097 spin_unlock(&dev
->lock
);
2102 static int net2272_present(struct net2272
*dev
)
2105 * Quick test to see if CPU can communicate properly with the NET2272.
2106 * Verifies connection using writes and reads to write/read and
2107 * read-only registers.
2109 * This routine is strongly recommended especially during early bring-up
2110 * of new hardware, however for designs that do not apply Power On System
2111 * Tests (POST) it may discarded (or perhaps minimized).
2116 /* Verify NET2272 write/read SCRATCH register can write and read */
2117 refval
= net2272_read(dev
, SCRATCH
);
2118 for (ii
= 0; ii
< 0x100; ii
+= 7) {
2119 net2272_write(dev
, SCRATCH
, ii
);
2120 val
= net2272_read(dev
, SCRATCH
);
2123 "%s: write/read SCRATCH register test failed: "
2124 "wrote:0x%2.2x, read:0x%2.2x\n",
2129 /* To be nice, we write the original SCRATCH value back: */
2130 net2272_write(dev
, SCRATCH
, refval
);
2132 /* Verify NET2272 CHIPREV register is read-only: */
2133 refval
= net2272_read(dev
, CHIPREV_2272
);
2134 for (ii
= 0; ii
< 0x100; ii
+= 7) {
2135 net2272_write(dev
, CHIPREV_2272
, ii
);
2136 val
= net2272_read(dev
, CHIPREV_2272
);
2137 if (val
!= refval
) {
2139 "%s: write/read CHIPREV register test failed: "
2140 "wrote 0x%2.2x, read:0x%2.2x expected:0x%2.2x\n",
2141 __func__
, ii
, val
, refval
);
2147 * Verify NET2272's "NET2270 legacy revision" register
2148 * - NET2272 has two revision registers. The NET2270 legacy revision
2149 * register should read the same value, regardless of the NET2272
2150 * silicon revision. The legacy register applies to NET2270
2151 * firmware being applied to the NET2272.
2153 val
= net2272_read(dev
, CHIPREV_LEGACY
);
2154 if (val
!= NET2270_LEGACY_REV
) {
2156 * Unexpected legacy revision value
2157 * - Perhaps the chip is a NET2270?
2160 "%s: WARNING: UNEXPECTED NET2272 LEGACY REGISTER VALUE:\n"
2161 " - CHIPREV_LEGACY: expected 0x%2.2x, got:0x%2.2x. (Not NET2272?)\n",
2162 __func__
, NET2270_LEGACY_REV
, val
);
2167 * Verify NET2272 silicon revision
2168 * - This revision register is appropriate for the silicon version
2171 val
= net2272_read(dev
, CHIPREV_2272
);
2173 case CHIPREV_NET2272_R1
:
2175 * NET2272 Rev 1 has DMA related errata:
2176 * - Newer silicon (Rev 1A or better) required
2179 "%s: Rev 1 detected: newer silicon recommended for DMA support\n",
2182 case CHIPREV_NET2272_R1A
:
2185 /* NET2272 silicon version *may* not work with this firmware */
2187 "%s: unexpected silicon revision register value: "
2188 " CHIPREV_2272: 0x%2.2x\n",
2191 * Return Success, even though the chip rev is not an expected value
2192 * - Older, pre-built firmware can attempt to operate on newer silicon
2193 * - Often, new silicon is perfectly compatible
2197 /* Success: NET2272 checks out OK */
2202 net2272_gadget_release(struct device
*_dev
)
2204 struct net2272
*dev
= dev_get_drvdata(_dev
);
2208 /*---------------------------------------------------------------------------*/
2211 net2272_remove(struct net2272
*dev
)
2213 usb_del_gadget_udc(&dev
->gadget
);
2214 free_irq(dev
->irq
, dev
);
2215 iounmap(dev
->base_addr
);
2216 device_remove_file(dev
->dev
, &dev_attr_registers
);
2218 dev_info(dev
->dev
, "unbind\n");
2221 static struct net2272
*net2272_probe_init(struct device
*dev
, unsigned int irq
)
2223 struct net2272
*ret
;
2226 dev_dbg(dev
, "No IRQ!\n");
2227 return ERR_PTR(-ENODEV
);
2230 /* alloc, and start init */
2231 ret
= kzalloc(sizeof(*ret
), GFP_KERNEL
);
2233 return ERR_PTR(-ENOMEM
);
2235 spin_lock_init(&ret
->lock
);
2238 ret
->gadget
.ops
= &net2272_ops
;
2239 ret
->gadget
.max_speed
= USB_SPEED_HIGH
;
2241 /* the "gadget" abstracts/virtualizes the controller */
2242 ret
->gadget
.name
= driver_name
;
2248 net2272_probe_fin(struct net2272
*dev
, unsigned int irqflags
)
2252 /* See if there... */
2253 if (net2272_present(dev
)) {
2254 dev_warn(dev
->dev
, "2272 not found!\n");
2259 net2272_usb_reset(dev
);
2260 net2272_usb_reinit(dev
);
2262 ret
= request_irq(dev
->irq
, net2272_irq
, irqflags
, driver_name
, dev
);
2264 dev_err(dev
->dev
, "request interrupt %i failed\n", dev
->irq
);
2268 dev
->chiprev
= net2272_read(dev
, CHIPREV_2272
);
2271 dev_info(dev
->dev
, "%s\n", driver_desc
);
2272 dev_info(dev
->dev
, "irq %i, mem %p, chip rev %04x, dma %s\n",
2273 dev
->irq
, dev
->base_addr
, dev
->chiprev
,
2275 dev_info(dev
->dev
, "version: %s\n", driver_vers
);
2277 ret
= device_create_file(dev
->dev
, &dev_attr_registers
);
2281 ret
= usb_add_gadget_udc_release(dev
->dev
, &dev
->gadget
,
2282 net2272_gadget_release
);
2289 device_remove_file(dev
->dev
, &dev_attr_registers
);
2291 free_irq(dev
->irq
, dev
);
2296 #ifdef CONFIG_USB_PCI
2299 * wrap this driver around the specified device, but
2300 * don't respond over USB until a gadget driver binds to us
2304 net2272_rdk1_probe(struct pci_dev
*pdev
, struct net2272
*dev
)
2306 unsigned long resource
, len
, tmp
;
2307 void __iomem
*mem_mapped_addr
[4];
2311 * BAR 0 holds PLX 9054 config registers
2312 * BAR 1 is i/o memory; unused here
2313 * BAR 2 holds EPLD config registers
2314 * BAR 3 holds NET2272 registers
2317 /* Find and map all address spaces */
2318 for (i
= 0; i
< 4; ++i
) {
2320 continue; /* BAR1 unused */
2322 resource
= pci_resource_start(pdev
, i
);
2323 len
= pci_resource_len(pdev
, i
);
2325 if (!request_mem_region(resource
, len
, driver_name
)) {
2326 dev_dbg(dev
->dev
, "controller already in use\n");
2331 mem_mapped_addr
[i
] = ioremap_nocache(resource
, len
);
2332 if (mem_mapped_addr
[i
] == NULL
) {
2333 release_mem_region(resource
, len
);
2334 dev_dbg(dev
->dev
, "can't map memory\n");
2340 dev
->rdk1
.plx9054_base_addr
= mem_mapped_addr
[0];
2341 dev
->rdk1
.epld_base_addr
= mem_mapped_addr
[2];
2342 dev
->base_addr
= mem_mapped_addr
[3];
2344 /* Set PLX 9054 bus width (16 bits) */
2345 tmp
= readl(dev
->rdk1
.plx9054_base_addr
+ LBRD1
);
2346 writel((tmp
& ~(3 << MEMORY_SPACE_LOCAL_BUS_WIDTH
)) | W16_BIT
,
2347 dev
->rdk1
.plx9054_base_addr
+ LBRD1
);
2349 /* Enable PLX 9054 Interrupts */
2350 writel(readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
) |
2351 (1 << PCI_INTERRUPT_ENABLE
) |
2352 (1 << LOCAL_INTERRUPT_INPUT_ENABLE
),
2353 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2355 writeb((1 << CHANNEL_CLEAR_INTERRUPT
| (0 << CHANNEL_ENABLE
)),
2356 dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
2359 writeb((1 << EPLD_DMA_ENABLE
) |
2360 (1 << DMA_CTL_DACK
) |
2361 (1 << DMA_TIMEOUT_ENABLE
) |
2365 (1 << NET2272_RESET
),
2366 dev
->base_addr
+ EPLD_IO_CONTROL_REGISTER
);
2369 writeb(readb(dev
->base_addr
+ EPLD_IO_CONTROL_REGISTER
) &
2370 ~(1 << NET2272_RESET
),
2371 dev
->base_addr
+ EPLD_IO_CONTROL_REGISTER
);
2378 iounmap(mem_mapped_addr
[i
]);
2379 release_mem_region(pci_resource_start(pdev
, i
),
2380 pci_resource_len(pdev
, i
));
2387 net2272_rdk2_probe(struct pci_dev
*pdev
, struct net2272
*dev
)
2389 unsigned long resource
, len
;
2390 void __iomem
*mem_mapped_addr
[2];
2394 * BAR 0 holds FGPA config registers
2395 * BAR 1 holds NET2272 registers
2398 /* Find and map all address spaces, bar2-3 unused in rdk 2 */
2399 for (i
= 0; i
< 2; ++i
) {
2400 resource
= pci_resource_start(pdev
, i
);
2401 len
= pci_resource_len(pdev
, i
);
2403 if (!request_mem_region(resource
, len
, driver_name
)) {
2404 dev_dbg(dev
->dev
, "controller already in use\n");
2409 mem_mapped_addr
[i
] = ioremap_nocache(resource
, len
);
2410 if (mem_mapped_addr
[i
] == NULL
) {
2411 release_mem_region(resource
, len
);
2412 dev_dbg(dev
->dev
, "can't map memory\n");
2418 dev
->rdk2
.fpga_base_addr
= mem_mapped_addr
[0];
2419 dev
->base_addr
= mem_mapped_addr
[1];
2422 /* Set 2272 bus width (16 bits) and reset */
2423 writel((1 << CHIP_RESET
), dev
->rdk2
.fpga_base_addr
+ RDK2_LOCCTLRDK
);
2425 writel((1 << BUS_WIDTH
), dev
->rdk2
.fpga_base_addr
+ RDK2_LOCCTLRDK
);
2426 /* Print fpga version number */
2427 dev_info(dev
->dev
, "RDK2 FPGA version %08x\n",
2428 readl(dev
->rdk2
.fpga_base_addr
+ RDK2_FPGAREV
));
2429 /* Enable FPGA Interrupts */
2430 writel((1 << NET2272_PCI_IRQ
), dev
->rdk2
.fpga_base_addr
+ RDK2_IRQENB
);
2436 iounmap(mem_mapped_addr
[i
]);
2437 release_mem_region(pci_resource_start(pdev
, i
),
2438 pci_resource_len(pdev
, i
));
2445 net2272_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2447 struct net2272
*dev
;
2450 dev
= net2272_probe_init(&pdev
->dev
, pdev
->irq
);
2452 return PTR_ERR(dev
);
2453 dev
->dev_id
= pdev
->device
;
2455 if (pci_enable_device(pdev
) < 0) {
2460 pci_set_master(pdev
);
2462 switch (pdev
->device
) {
2463 case PCI_DEVICE_ID_RDK1
: ret
= net2272_rdk1_probe(pdev
, dev
); break;
2464 case PCI_DEVICE_ID_RDK2
: ret
= net2272_rdk2_probe(pdev
, dev
); break;
2470 ret
= net2272_probe_fin(dev
, 0);
2474 pci_set_drvdata(pdev
, dev
);
2479 pci_disable_device(pdev
);
2487 net2272_rdk1_remove(struct pci_dev
*pdev
, struct net2272
*dev
)
2491 /* disable PLX 9054 interrupts */
2492 writel(readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
) &
2493 ~(1 << PCI_INTERRUPT_ENABLE
),
2494 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2496 /* clean up resources allocated during probe() */
2497 iounmap(dev
->rdk1
.plx9054_base_addr
);
2498 iounmap(dev
->rdk1
.epld_base_addr
);
2500 for (i
= 0; i
< 4; ++i
) {
2502 continue; /* BAR1 unused */
2503 release_mem_region(pci_resource_start(pdev
, i
),
2504 pci_resource_len(pdev
, i
));
2509 net2272_rdk2_remove(struct pci_dev
*pdev
, struct net2272
*dev
)
2513 /* disable fpga interrupts
2514 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2515 ~(1 << PCI_INTERRUPT_ENABLE),
2516 dev->rdk1.plx9054_base_addr + INTCSR);
2519 /* clean up resources allocated during probe() */
2520 iounmap(dev
->rdk2
.fpga_base_addr
);
2522 for (i
= 0; i
< 2; ++i
)
2523 release_mem_region(pci_resource_start(pdev
, i
),
2524 pci_resource_len(pdev
, i
));
2528 net2272_pci_remove(struct pci_dev
*pdev
)
2530 struct net2272
*dev
= pci_get_drvdata(pdev
);
2532 net2272_remove(dev
);
2534 switch (pdev
->device
) {
2535 case PCI_DEVICE_ID_RDK1
: net2272_rdk1_remove(pdev
, dev
); break;
2536 case PCI_DEVICE_ID_RDK2
: net2272_rdk2_remove(pdev
, dev
); break;
2540 pci_disable_device(pdev
);
2545 /* Table of matching PCI IDs */
2546 static struct pci_device_id pci_ids
[] = {
2548 .class = ((PCI_CLASS_BRIDGE_OTHER
<< 8) | 0xfe),
2550 .vendor
= PCI_VENDOR_ID_PLX
,
2551 .device
= PCI_DEVICE_ID_RDK1
,
2552 .subvendor
= PCI_ANY_ID
,
2553 .subdevice
= PCI_ANY_ID
,
2556 .class = ((PCI_CLASS_BRIDGE_OTHER
<< 8) | 0xfe),
2558 .vendor
= PCI_VENDOR_ID_PLX
,
2559 .device
= PCI_DEVICE_ID_RDK2
,
2560 .subvendor
= PCI_ANY_ID
,
2561 .subdevice
= PCI_ANY_ID
,
2565 MODULE_DEVICE_TABLE(pci
, pci_ids
);
2567 static struct pci_driver net2272_pci_driver
= {
2568 .name
= driver_name
,
2569 .id_table
= pci_ids
,
2571 .probe
= net2272_pci_probe
,
2572 .remove
= net2272_pci_remove
,
2575 static int net2272_pci_register(void)
2577 return pci_register_driver(&net2272_pci_driver
);
2580 static void net2272_pci_unregister(void)
2582 pci_unregister_driver(&net2272_pci_driver
);
2586 static inline int net2272_pci_register(void) { return 0; }
2587 static inline void net2272_pci_unregister(void) { }
2590 /*---------------------------------------------------------------------------*/
2593 net2272_plat_probe(struct platform_device
*pdev
)
2595 struct net2272
*dev
;
2597 unsigned int irqflags
;
2598 resource_size_t base
, len
;
2599 struct resource
*iomem
, *iomem_bus
, *irq_res
;
2601 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
2602 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2603 iomem_bus
= platform_get_resource(pdev
, IORESOURCE_BUS
, 0);
2604 if (!irq_res
|| !iomem
) {
2605 dev_err(&pdev
->dev
, "must provide irq/base addr");
2609 dev
= net2272_probe_init(&pdev
->dev
, irq_res
->start
);
2611 return PTR_ERR(dev
);
2614 if (irq_res
->flags
& IORESOURCE_IRQ_HIGHEDGE
)
2615 irqflags
|= IRQF_TRIGGER_RISING
;
2616 if (irq_res
->flags
& IORESOURCE_IRQ_LOWEDGE
)
2617 irqflags
|= IRQF_TRIGGER_FALLING
;
2618 if (irq_res
->flags
& IORESOURCE_IRQ_HIGHLEVEL
)
2619 irqflags
|= IRQF_TRIGGER_HIGH
;
2620 if (irq_res
->flags
& IORESOURCE_IRQ_LOWLEVEL
)
2621 irqflags
|= IRQF_TRIGGER_LOW
;
2623 base
= iomem
->start
;
2624 len
= resource_size(iomem
);
2626 dev
->base_shift
= iomem_bus
->start
;
2628 if (!request_mem_region(base
, len
, driver_name
)) {
2629 dev_dbg(dev
->dev
, "get request memory region!\n");
2633 dev
->base_addr
= ioremap_nocache(base
, len
);
2634 if (!dev
->base_addr
) {
2635 dev_dbg(dev
->dev
, "can't map memory\n");
2640 ret
= net2272_probe_fin(dev
, IRQF_TRIGGER_LOW
);
2644 platform_set_drvdata(pdev
, dev
);
2645 dev_info(&pdev
->dev
, "running in 16-bit, %sbyte swap local bus mode\n",
2646 (net2272_read(dev
, LOCCTL
) & (1 << BYTE_SWAP
)) ? "" : "no ");
2651 iounmap(dev
->base_addr
);
2653 release_mem_region(base
, len
);
2659 net2272_plat_remove(struct platform_device
*pdev
)
2661 struct net2272
*dev
= platform_get_drvdata(pdev
);
2663 net2272_remove(dev
);
2665 release_mem_region(pdev
->resource
[0].start
,
2666 resource_size(&pdev
->resource
[0]));
2673 static struct platform_driver net2272_plat_driver
= {
2674 .probe
= net2272_plat_probe
,
2675 .remove
= net2272_plat_remove
,
2677 .name
= driver_name
,
2679 /* FIXME .suspend, .resume */
2681 MODULE_ALIAS("platform:net2272");
2683 static int __init
net2272_init(void)
2687 ret
= net2272_pci_register();
2690 ret
= platform_driver_register(&net2272_plat_driver
);
2696 net2272_pci_unregister();
2699 module_init(net2272_init
);
2701 static void __exit
net2272_cleanup(void)
2703 net2272_pci_unregister();
2704 platform_driver_unregister(&net2272_plat_driver
);
2706 module_exit(net2272_cleanup
);
2708 MODULE_DESCRIPTION(DRIVER_DESC
);
2709 MODULE_AUTHOR("PLX Technology, Inc.");
2710 MODULE_LICENSE("GPL");