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_ep
*ep
;
333 struct net2272_request
*req
;
337 ep
= container_of(_ep
, struct net2272_ep
, ep
);
339 req
= kzalloc(sizeof(*req
), gfp_flags
);
343 INIT_LIST_HEAD(&req
->queue
);
349 net2272_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
351 struct net2272_ep
*ep
;
352 struct net2272_request
*req
;
354 ep
= container_of(_ep
, struct net2272_ep
, ep
);
358 req
= container_of(_req
, struct net2272_request
, req
);
359 WARN_ON(!list_empty(&req
->queue
));
364 net2272_done(struct net2272_ep
*ep
, struct net2272_request
*req
, int status
)
367 unsigned stopped
= ep
->stopped
;
370 if (ep
->dev
->protocol_stall
) {
377 list_del_init(&req
->queue
);
379 if (req
->req
.status
== -EINPROGRESS
)
380 req
->req
.status
= status
;
382 status
= req
->req
.status
;
385 if (use_dma
&& ep
->dma
)
386 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
,
389 if (status
&& status
!= -ESHUTDOWN
)
390 dev_vdbg(dev
->dev
, "complete %s req %p stat %d len %u/%u buf %p\n",
391 ep
->ep
.name
, &req
->req
, status
,
392 req
->req
.actual
, req
->req
.length
, req
->req
.buf
);
394 /* don't modify queue heads during completion callback */
396 spin_unlock(&dev
->lock
);
397 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
398 spin_lock(&dev
->lock
);
399 ep
->stopped
= stopped
;
403 net2272_write_packet(struct net2272_ep
*ep
, u8
*buf
,
404 struct net2272_request
*req
, unsigned max
)
406 u16 __iomem
*ep_data
= net2272_reg_addr(ep
->dev
, EP_DATA
);
408 unsigned length
, count
;
411 length
= min(req
->req
.length
- req
->req
.actual
, max
);
412 req
->req
.actual
+= length
;
414 dev_vdbg(ep
->dev
->dev
, "write packet %s req %p max %u len %u avail %u\n",
415 ep
->ep
.name
, req
, max
, length
,
416 (net2272_ep_read(ep
, EP_AVAIL1
) << 8) | net2272_ep_read(ep
, EP_AVAIL0
));
421 while (likely(count
>= 2)) {
422 /* no byte-swap required; chip endian set during init */
423 writew(*bufp
++, ep_data
);
428 /* write final byte by placing the NET2272 into 8-bit mode */
429 if (unlikely(count
)) {
430 tmp
= net2272_read(ep
->dev
, LOCCTL
);
431 net2272_write(ep
->dev
, LOCCTL
, tmp
& ~(1 << DATA_WIDTH
));
432 writeb(*buf
, ep_data
);
433 net2272_write(ep
->dev
, LOCCTL
, tmp
);
438 /* returns: 0: still running, 1: completed, negative: errno */
440 net2272_write_fifo(struct net2272_ep
*ep
, struct net2272_request
*req
)
446 dev_vdbg(ep
->dev
->dev
, "write_fifo %s actual %d len %d\n",
447 ep
->ep
.name
, req
->req
.actual
, req
->req
.length
);
450 * Keep loading the endpoint until the final packet is loaded,
451 * or the endpoint buffer is full.
455 * Clear interrupt status
456 * - Packet Transmitted interrupt will become set again when the
457 * host successfully takes another packet
459 net2272_ep_write(ep
, EP_STAT0
, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
));
460 while (!(net2272_ep_read(ep
, EP_STAT0
) & (1 << BUFFER_FULL
))) {
461 buf
= req
->req
.buf
+ req
->req
.actual
;
465 net2272_ep_read(ep
, EP_STAT0
);
467 max
= (net2272_ep_read(ep
, EP_AVAIL1
) << 8) |
468 (net2272_ep_read(ep
, EP_AVAIL0
));
470 if (max
< ep
->ep
.maxpacket
)
471 max
= (net2272_ep_read(ep
, EP_AVAIL1
) << 8)
472 | (net2272_ep_read(ep
, EP_AVAIL0
));
474 count
= net2272_write_packet(ep
, buf
, req
, max
);
475 /* see if we are done */
476 if (req
->req
.length
== req
->req
.actual
) {
477 /* validate short or zlp packet */
478 if (count
< ep
->ep
.maxpacket
)
479 set_fifo_bytecount(ep
, 0);
480 net2272_done(ep
, req
, 0);
482 if (!list_empty(&ep
->queue
)) {
483 req
= list_entry(ep
->queue
.next
,
484 struct net2272_request
,
486 status
= net2272_kick_dma(ep
, req
);
489 if ((net2272_ep_read(ep
, EP_STAT0
)
490 & (1 << BUFFER_EMPTY
)))
495 net2272_ep_write(ep
, EP_STAT0
, (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
));
501 net2272_out_flush(struct net2272_ep
*ep
)
503 ASSERT_OUT_NAKING(ep
);
505 net2272_ep_write(ep
, EP_STAT0
, (1 << DATA_OUT_TOKEN_INTERRUPT
)
506 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
));
507 net2272_ep_write(ep
, EP_STAT1
, 1 << BUFFER_FLUSH
);
511 net2272_read_packet(struct net2272_ep
*ep
, u8
*buf
,
512 struct net2272_request
*req
, unsigned avail
)
514 u16 __iomem
*ep_data
= net2272_reg_addr(ep
->dev
, EP_DATA
);
518 req
->req
.actual
+= avail
;
520 dev_vdbg(ep
->dev
->dev
, "read packet %s req %p len %u avail %u\n",
521 ep
->ep
.name
, req
, avail
,
522 (net2272_ep_read(ep
, EP_AVAIL1
) << 8) | net2272_ep_read(ep
, EP_AVAIL0
));
524 is_short
= (avail
< ep
->ep
.maxpacket
);
526 if (unlikely(avail
== 0)) {
527 /* remove any zlp from the buffer */
528 (void)readw(ep_data
);
532 /* Ensure we get the final byte */
533 if (unlikely(avail
% 2))
538 *bufp
++ = readw(ep_data
);
543 * To avoid false endpoint available race condition must read
544 * ep stat0 twice in the case of a short transfer
546 if (net2272_ep_read(ep
, EP_STAT0
) & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
))
547 net2272_ep_read(ep
, EP_STAT0
);
553 net2272_read_fifo(struct net2272_ep
*ep
, struct net2272_request
*req
)
562 dev_vdbg(ep
->dev
->dev
, "read_fifo %s actual %d len %d\n",
563 ep
->ep
.name
, req
->req
.actual
, req
->req
.length
);
567 buf
= req
->req
.buf
+ req
->req
.actual
;
570 count
= (net2272_ep_read(ep
, EP_AVAIL1
) << 8)
571 | net2272_ep_read(ep
, EP_AVAIL0
);
573 net2272_ep_write(ep
, EP_STAT0
,
574 (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
) |
575 (1 << DATA_PACKET_RECEIVED_INTERRUPT
));
577 tmp
= req
->req
.length
- req
->req
.actual
;
580 if ((tmp
% ep
->ep
.maxpacket
) != 0) {
581 dev_err(ep
->dev
->dev
,
582 "%s out fifo %d bytes, expected %d\n",
583 ep
->ep
.name
, count
, tmp
);
586 count
= (tmp
> 0) ? tmp
: 0;
589 is_short
= net2272_read_packet(ep
, buf
, req
, count
);
592 if (unlikely(cleanup
|| is_short
||
593 ((req
->req
.actual
== req
->req
.length
)
594 && !req
->req
.zero
))) {
597 net2272_out_flush(ep
);
598 net2272_done(ep
, req
, -EOVERFLOW
);
600 net2272_done(ep
, req
, 0);
602 /* re-initialize endpoint transfer registers
603 * otherwise they may result in erroneous pre-validation
604 * for subsequent control reads
606 if (unlikely(ep
->num
== 0)) {
607 net2272_ep_write(ep
, EP_TRANSFER2
, 0);
608 net2272_ep_write(ep
, EP_TRANSFER1
, 0);
609 net2272_ep_write(ep
, EP_TRANSFER0
, 0);
612 if (!list_empty(&ep
->queue
)) {
613 req
= list_entry(ep
->queue
.next
,
614 struct net2272_request
, queue
);
615 status
= net2272_kick_dma(ep
, req
);
617 !(net2272_ep_read(ep
, EP_STAT0
) & (1 << BUFFER_EMPTY
)))
622 } while (!(net2272_ep_read(ep
, EP_STAT0
) & (1 << BUFFER_EMPTY
)));
628 net2272_pio_advance(struct net2272_ep
*ep
)
630 struct net2272_request
*req
;
632 if (unlikely(list_empty(&ep
->queue
)))
635 req
= list_entry(ep
->queue
.next
, struct net2272_request
, queue
);
636 (ep
->is_in
? net2272_write_fifo
: net2272_read_fifo
)(ep
, req
);
639 /* returns 0 on success, else negative errno */
641 net2272_request_dma(struct net2272
*dev
, unsigned ep
, u32 buf
,
642 unsigned len
, unsigned dir
)
644 dev_vdbg(dev
->dev
, "request_dma ep %d buf %08x len %d dir %d\n",
647 /* The NET2272 only supports a single dma channel */
651 * EP_TRANSFER (used to determine the number of bytes received
652 * in an OUT transfer) is 24 bits wide; don't ask for more than that.
654 if ((dir
== 1) && (len
> 0x1000000))
659 /* initialize platform's dma */
661 /* NET2272 addr, buffer addr, length, etc. */
662 switch (dev
->dev_id
) {
663 case PCI_DEVICE_ID_RDK1
:
664 /* Setup PLX 9054 DMA mode */
665 writel((1 << LOCAL_BUS_WIDTH
) |
666 (1 << TA_READY_INPUT_ENABLE
) |
667 (0 << LOCAL_BURST_ENABLE
) |
668 (1 << DONE_INTERRUPT_ENABLE
) |
669 (1 << LOCAL_ADDRESSING_MODE
) |
671 (1 << DMA_EOT_ENABLE
) |
672 (1 << FAST_SLOW_TERMINATE_MODE_SELECT
) |
673 (1 << DMA_CHANNEL_INTERRUPT_SELECT
),
674 dev
->rdk1
.plx9054_base_addr
+ DMAMODE0
);
676 writel(0x100000, dev
->rdk1
.plx9054_base_addr
+ DMALADR0
);
677 writel(buf
, dev
->rdk1
.plx9054_base_addr
+ DMAPADR0
);
678 writel(len
, dev
->rdk1
.plx9054_base_addr
+ DMASIZ0
);
679 writel((dir
<< DIRECTION_OF_TRANSFER
) |
680 (1 << INTERRUPT_AFTER_TERMINAL_COUNT
),
681 dev
->rdk1
.plx9054_base_addr
+ DMADPR0
);
682 writel((1 << LOCAL_DMA_CHANNEL_0_INTERRUPT_ENABLE
) |
683 readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
),
684 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
690 net2272_write(dev
, DMAREQ
,
691 (0 << DMA_BUFFER_VALID
) |
692 (1 << DMA_REQUEST_ENABLE
) |
693 (1 << DMA_CONTROL_DACK
) |
694 (dev
->dma_eot_polarity
<< EOT_POLARITY
) |
695 (dev
->dma_dack_polarity
<< DACK_POLARITY
) |
696 (dev
->dma_dreq_polarity
<< DREQ_POLARITY
) |
697 ((ep
>> 1) << DMA_ENDPOINT_SELECT
));
699 (void) net2272_read(dev
, SCRATCH
);
705 net2272_start_dma(struct net2272
*dev
)
707 /* start platform's dma controller */
709 switch (dev
->dev_id
) {
710 case PCI_DEVICE_ID_RDK1
:
711 writeb((1 << CHANNEL_ENABLE
) | (1 << CHANNEL_START
),
712 dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
718 /* returns 0 on success, else negative errno */
720 net2272_kick_dma(struct net2272_ep
*ep
, struct net2272_request
*req
)
725 if (!use_dma
|| (ep
->num
< 1) || (ep
->num
> 2) || !ep
->dma
)
728 /* don't use dma for odd-length transfers
729 * otherwise, we'd need to deal with the last byte with pio
731 if (req
->req
.length
& 1)
734 dev_vdbg(ep
->dev
->dev
, "kick_dma %s req %p dma %08llx\n",
735 ep
->ep
.name
, req
, (unsigned long long) req
->req
.dma
);
737 net2272_ep_write(ep
, EP_RSPSET
, 1 << ALT_NAK_OUT_PACKETS
);
739 /* The NET2272 can only use DMA on one endpoint at a time */
740 if (ep
->dev
->dma_busy
)
743 /* Make sure we only DMA an even number of bytes (we'll use
744 * pio to complete the transfer)
746 size
= req
->req
.length
;
749 /* device-to-host transfer */
751 /* initialize platform's dma controller */
752 if (net2272_request_dma(ep
->dev
, ep
->num
, req
->req
.dma
, size
, 0))
753 /* unable to obtain DMA channel; return error and use pio mode */
755 req
->req
.actual
+= size
;
757 /* host-to-device transfer */
759 tmp
= net2272_ep_read(ep
, EP_STAT0
);
761 /* initialize platform's dma controller */
762 if (net2272_request_dma(ep
->dev
, ep
->num
, req
->req
.dma
, size
, 1))
763 /* unable to obtain DMA channel; return error and use pio mode */
766 if (!(tmp
& (1 << BUFFER_EMPTY
)))
772 /* allow the endpoint's buffer to fill */
773 net2272_ep_write(ep
, EP_RSPCLR
, 1 << ALT_NAK_OUT_PACKETS
);
775 /* this transfer completed and data's already in the fifo
776 * return error so pio gets used.
778 if (tmp
& (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)) {
781 net2272_write(ep
->dev
, DMAREQ
,
782 (0 << DMA_BUFFER_VALID
) |
783 (0 << DMA_REQUEST_ENABLE
) |
784 (1 << DMA_CONTROL_DACK
) |
785 (ep
->dev
->dma_eot_polarity
<< EOT_POLARITY
) |
786 (ep
->dev
->dma_dack_polarity
<< DACK_POLARITY
) |
787 (ep
->dev
->dma_dreq_polarity
<< DREQ_POLARITY
) |
788 ((ep
->num
>> 1) << DMA_ENDPOINT_SELECT
));
794 /* Don't use per-packet interrupts: use dma interrupts only */
795 net2272_ep_write(ep
, EP_IRQENB
, 0);
797 net2272_start_dma(ep
->dev
);
802 static void net2272_cancel_dma(struct net2272
*dev
)
805 switch (dev
->dev_id
) {
806 case PCI_DEVICE_ID_RDK1
:
807 writeb(0, dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
808 writeb(1 << CHANNEL_ABORT
, dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
809 while (!(readb(dev
->rdk1
.plx9054_base_addr
+ DMACSR0
) &
810 (1 << CHANNEL_DONE
)))
811 continue; /* wait for dma to stabalize */
813 /* dma abort generates an interrupt */
814 writeb(1 << CHANNEL_CLEAR_INTERRUPT
,
815 dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
823 /*---------------------------------------------------------------------------*/
826 net2272_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
828 struct net2272_request
*req
;
829 struct net2272_ep
*ep
;
835 req
= container_of(_req
, struct net2272_request
, req
);
836 if (!_req
|| !_req
->complete
|| !_req
->buf
837 || !list_empty(&req
->queue
))
839 ep
= container_of(_ep
, struct net2272_ep
, ep
);
840 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
843 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
846 /* set up dma mapping in case the caller didn't */
847 if (use_dma
&& ep
->dma
) {
848 status
= usb_gadget_map_request(&dev
->gadget
, _req
,
854 dev_vdbg(dev
->dev
, "%s queue req %p, len %d buf %p dma %08llx %s\n",
855 _ep
->name
, _req
, _req
->length
, _req
->buf
,
856 (unsigned long long) _req
->dma
, _req
->zero
? "zero" : "!zero");
858 spin_lock_irqsave(&dev
->lock
, flags
);
860 _req
->status
= -EINPROGRESS
;
863 /* kickstart this i/o queue? */
864 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
865 /* maybe there's no control data, just status ack */
866 if (ep
->num
== 0 && _req
->length
== 0) {
867 net2272_done(ep
, req
, 0);
868 dev_vdbg(dev
->dev
, "%s status ack\n", ep
->ep
.name
);
872 /* Return zlp, don't let it block subsequent packets */
873 s
= net2272_ep_read(ep
, EP_STAT0
);
874 if (s
& (1 << BUFFER_EMPTY
)) {
875 /* Buffer is empty check for a blocking zlp, handle it */
876 if ((s
& (1 << NAK_OUT_PACKETS
)) &&
877 net2272_ep_read(ep
, EP_STAT1
) & (1 << LOCAL_OUT_ZLP
)) {
878 dev_dbg(dev
->dev
, "WARNING: returning ZLP short packet termination!\n");
880 * Request is going to terminate with a short packet ...
881 * hope the client is ready for it!
883 status
= net2272_read_fifo(ep
, req
);
884 /* clear short packet naking */
885 net2272_ep_write(ep
, EP_STAT0
, (1 << NAK_OUT_PACKETS
));
891 status
= net2272_kick_dma(ep
, req
);
894 /* dma failed (most likely in use by another endpoint)
900 status
= net2272_write_fifo(ep
, req
);
902 s
= net2272_ep_read(ep
, EP_STAT0
);
903 if ((s
& (1 << BUFFER_EMPTY
)) == 0)
904 status
= net2272_read_fifo(ep
, req
);
907 if (unlikely(status
!= 0)) {
915 list_add_tail(&req
->queue
, &ep
->queue
);
917 if (likely(!list_empty(&ep
->queue
)))
918 net2272_ep_write(ep
, EP_RSPCLR
, 1 << ALT_NAK_OUT_PACKETS
);
920 spin_unlock_irqrestore(&dev
->lock
, flags
);
925 /* dequeue ALL requests */
927 net2272_dequeue_all(struct net2272_ep
*ep
)
929 struct net2272_request
*req
;
931 /* called with spinlock held */
934 while (!list_empty(&ep
->queue
)) {
935 req
= list_entry(ep
->queue
.next
,
936 struct net2272_request
,
938 net2272_done(ep
, req
, -ESHUTDOWN
);
942 /* dequeue JUST ONE request */
944 net2272_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
946 struct net2272_ep
*ep
;
947 struct net2272_request
*req
;
951 ep
= container_of(_ep
, struct net2272_ep
, ep
);
952 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0) || !_req
)
955 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
956 stopped
= ep
->stopped
;
959 /* make sure it's still queued on this endpoint */
960 list_for_each_entry(req
, &ep
->queue
, queue
) {
961 if (&req
->req
== _req
)
964 if (&req
->req
!= _req
) {
965 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
969 /* queue head may be partially complete */
970 if (ep
->queue
.next
== &req
->queue
) {
971 dev_dbg(ep
->dev
->dev
, "unlink (%s) pio\n", _ep
->name
);
972 net2272_done(ep
, req
, -ECONNRESET
);
975 ep
->stopped
= stopped
;
977 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
981 /*---------------------------------------------------------------------------*/
984 net2272_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
986 struct net2272_ep
*ep
;
990 ep
= container_of(_ep
, struct net2272_ep
, ep
);
991 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
993 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
995 if (ep
->desc
/* not ep0 */ && usb_endpoint_xfer_isoc(ep
->desc
))
998 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
999 if (!list_empty(&ep
->queue
))
1001 else if (ep
->is_in
&& value
&& net2272_fifo_status(_ep
) != 0)
1004 dev_vdbg(ep
->dev
->dev
, "%s %s %s\n", _ep
->name
,
1005 value
? "set" : "clear",
1006 wedged
? "wedge" : "halt");
1010 ep
->dev
->protocol_stall
= 1;
1020 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1026 net2272_set_halt(struct usb_ep
*_ep
, int value
)
1028 return net2272_set_halt_and_wedge(_ep
, value
, 0);
1032 net2272_set_wedge(struct usb_ep
*_ep
)
1034 if (!_ep
|| _ep
->name
== ep0name
)
1036 return net2272_set_halt_and_wedge(_ep
, 1, 1);
1040 net2272_fifo_status(struct usb_ep
*_ep
)
1042 struct net2272_ep
*ep
;
1045 ep
= container_of(_ep
, struct net2272_ep
, ep
);
1046 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1048 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1051 avail
= net2272_ep_read(ep
, EP_AVAIL1
) << 8;
1052 avail
|= net2272_ep_read(ep
, EP_AVAIL0
);
1053 if (avail
> ep
->fifo_size
)
1056 avail
= ep
->fifo_size
- avail
;
1061 net2272_fifo_flush(struct usb_ep
*_ep
)
1063 struct net2272_ep
*ep
;
1065 ep
= container_of(_ep
, struct net2272_ep
, ep
);
1066 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1068 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1071 net2272_ep_write(ep
, EP_STAT1
, 1 << BUFFER_FLUSH
);
1074 static struct usb_ep_ops net2272_ep_ops
= {
1075 .enable
= net2272_enable
,
1076 .disable
= net2272_disable
,
1078 .alloc_request
= net2272_alloc_request
,
1079 .free_request
= net2272_free_request
,
1081 .queue
= net2272_queue
,
1082 .dequeue
= net2272_dequeue
,
1084 .set_halt
= net2272_set_halt
,
1085 .set_wedge
= net2272_set_wedge
,
1086 .fifo_status
= net2272_fifo_status
,
1087 .fifo_flush
= net2272_fifo_flush
,
1090 /*---------------------------------------------------------------------------*/
1093 net2272_get_frame(struct usb_gadget
*_gadget
)
1095 struct net2272
*dev
;
1096 unsigned long flags
;
1101 dev
= container_of(_gadget
, struct net2272
, gadget
);
1102 spin_lock_irqsave(&dev
->lock
, flags
);
1104 ret
= net2272_read(dev
, FRAME1
) << 8;
1105 ret
|= net2272_read(dev
, FRAME0
);
1107 spin_unlock_irqrestore(&dev
->lock
, flags
);
1112 net2272_wakeup(struct usb_gadget
*_gadget
)
1114 struct net2272
*dev
;
1116 unsigned long flags
;
1120 dev
= container_of(_gadget
, struct net2272
, gadget
);
1122 spin_lock_irqsave(&dev
->lock
, flags
);
1123 tmp
= net2272_read(dev
, USBCTL0
);
1124 if (tmp
& (1 << IO_WAKEUP_ENABLE
))
1125 net2272_write(dev
, USBCTL1
, (1 << GENERATE_RESUME
));
1127 spin_unlock_irqrestore(&dev
->lock
, flags
);
1133 net2272_set_selfpowered(struct usb_gadget
*_gadget
, int value
)
1138 _gadget
->is_selfpowered
= (value
!= 0);
1144 net2272_pullup(struct usb_gadget
*_gadget
, int is_on
)
1146 struct net2272
*dev
;
1148 unsigned long flags
;
1152 dev
= container_of(_gadget
, struct net2272
, gadget
);
1154 spin_lock_irqsave(&dev
->lock
, flags
);
1155 tmp
= net2272_read(dev
, USBCTL0
);
1156 dev
->softconnect
= (is_on
!= 0);
1158 tmp
|= (1 << USB_DETECT_ENABLE
);
1160 tmp
&= ~(1 << USB_DETECT_ENABLE
);
1161 net2272_write(dev
, USBCTL0
, tmp
);
1162 spin_unlock_irqrestore(&dev
->lock
, flags
);
1167 static int net2272_start(struct usb_gadget
*_gadget
,
1168 struct usb_gadget_driver
*driver
);
1169 static int net2272_stop(struct usb_gadget
*_gadget
);
1171 static const struct usb_gadget_ops net2272_ops
= {
1172 .get_frame
= net2272_get_frame
,
1173 .wakeup
= net2272_wakeup
,
1174 .set_selfpowered
= net2272_set_selfpowered
,
1175 .pullup
= net2272_pullup
,
1176 .udc_start
= net2272_start
,
1177 .udc_stop
= net2272_stop
,
1180 /*---------------------------------------------------------------------------*/
1183 registers_show(struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1185 struct net2272
*dev
;
1188 unsigned long flags
;
1193 dev
= dev_get_drvdata(_dev
);
1196 spin_lock_irqsave(&dev
->lock
, flags
);
1199 s
= dev
->driver
->driver
.name
;
1203 /* Main Control Registers */
1204 t
= scnprintf(next
, size
, "%s version %s,"
1205 "chiprev %02x, locctl %02x\n"
1206 "irqenb0 %02x irqenb1 %02x "
1207 "irqstat0 %02x irqstat1 %02x\n",
1208 driver_name
, driver_vers
, dev
->chiprev
,
1209 net2272_read(dev
, LOCCTL
),
1210 net2272_read(dev
, IRQENB0
),
1211 net2272_read(dev
, IRQENB1
),
1212 net2272_read(dev
, IRQSTAT0
),
1213 net2272_read(dev
, IRQSTAT1
));
1218 t1
= net2272_read(dev
, DMAREQ
);
1219 t
= scnprintf(next
, size
, "\ndmareq %02x: %s %s%s%s%s\n",
1220 t1
, ep_name
[(t1
& 0x01) + 1],
1221 t1
& (1 << DMA_CONTROL_DACK
) ? "dack " : "",
1222 t1
& (1 << DMA_REQUEST_ENABLE
) ? "reqenb " : "",
1223 t1
& (1 << DMA_REQUEST
) ? "req " : "",
1224 t1
& (1 << DMA_BUFFER_VALID
) ? "valid " : "");
1228 /* USB Control Registers */
1229 t1
= net2272_read(dev
, USBCTL1
);
1230 if (t1
& (1 << VBUS_PIN
)) {
1231 if (t1
& (1 << USB_HIGH_SPEED
))
1233 else if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1239 t
= scnprintf(next
, size
,
1240 "usbctl0 %02x usbctl1 %02x addr 0x%02x (%s)\n",
1241 net2272_read(dev
, USBCTL0
), t1
,
1242 net2272_read(dev
, OURADDR
), s
);
1246 /* Endpoint Registers */
1247 for (i
= 0; i
< 4; ++i
) {
1248 struct net2272_ep
*ep
;
1254 t1
= net2272_ep_read(ep
, EP_CFG
);
1255 t2
= net2272_ep_read(ep
, EP_RSPSET
);
1256 t
= scnprintf(next
, size
,
1257 "\n%s\tcfg %02x rsp (%02x) %s%s%s%s%s%s%s%s"
1259 ep
->ep
.name
, t1
, t2
,
1260 (t2
& (1 << ALT_NAK_OUT_PACKETS
)) ? "NAK " : "",
1261 (t2
& (1 << HIDE_STATUS_PHASE
)) ? "hide " : "",
1262 (t2
& (1 << AUTOVALIDATE
)) ? "auto " : "",
1263 (t2
& (1 << INTERRUPT_MODE
)) ? "interrupt " : "",
1264 (t2
& (1 << CONTROL_STATUS_PHASE_HANDSHAKE
)) ? "status " : "",
1265 (t2
& (1 << NAK_OUT_PACKETS_MODE
)) ? "NAKmode " : "",
1266 (t2
& (1 << ENDPOINT_TOGGLE
)) ? "DATA1 " : "DATA0 ",
1267 (t2
& (1 << ENDPOINT_HALT
)) ? "HALT " : "",
1268 net2272_ep_read(ep
, EP_IRQENB
));
1272 t
= scnprintf(next
, size
,
1273 "\tstat0 %02x stat1 %02x avail %04x "
1275 net2272_ep_read(ep
, EP_STAT0
),
1276 net2272_ep_read(ep
, EP_STAT1
),
1277 (net2272_ep_read(ep
, EP_AVAIL1
) << 8) | net2272_ep_read(ep
, EP_AVAIL0
),
1279 ep
->is_in
? "in" : "out",
1280 type_string(t1
>> 5),
1281 ep
->stopped
? "*" : "");
1285 t
= scnprintf(next
, size
,
1286 "\tep_transfer %06x\n",
1287 ((net2272_ep_read(ep
, EP_TRANSFER2
) & 0xff) << 16) |
1288 ((net2272_ep_read(ep
, EP_TRANSFER1
) & 0xff) << 8) |
1289 ((net2272_ep_read(ep
, EP_TRANSFER0
) & 0xff)));
1293 t1
= net2272_ep_read(ep
, EP_BUFF_STATES
) & 0x03;
1294 t2
= (net2272_ep_read(ep
, EP_BUFF_STATES
) >> 2) & 0x03;
1295 t
= scnprintf(next
, size
,
1296 "\tbuf-a %s buf-b %s\n",
1297 buf_state_string(t1
),
1298 buf_state_string(t2
));
1303 spin_unlock_irqrestore(&dev
->lock
, flags
);
1305 return PAGE_SIZE
- size
;
1307 static DEVICE_ATTR_RO(registers
);
1309 /*---------------------------------------------------------------------------*/
1312 net2272_set_fifo_mode(struct net2272
*dev
, int mode
)
1316 tmp
= net2272_read(dev
, LOCCTL
) & 0x3f;
1318 net2272_write(dev
, LOCCTL
, tmp
);
1320 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
1322 /* always ep-a, ep-c ... maybe not ep-b */
1323 list_add_tail(&dev
->ep
[1].ep
.ep_list
, &dev
->gadget
.ep_list
);
1327 list_add_tail(&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1328 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 512;
1331 list_add_tail(&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1332 dev
->ep
[1].fifo_size
= 1024;
1333 dev
->ep
[2].fifo_size
= 512;
1336 list_add_tail(&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1337 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 1024;
1340 dev
->ep
[1].fifo_size
= 1024;
1344 /* ep-c is always 2 512 byte buffers */
1345 list_add_tail(&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1346 dev
->ep
[3].fifo_size
= 512;
1349 /*---------------------------------------------------------------------------*/
1352 net2272_usb_reset(struct net2272
*dev
)
1354 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1356 net2272_cancel_dma(dev
);
1358 net2272_write(dev
, IRQENB0
, 0);
1359 net2272_write(dev
, IRQENB1
, 0);
1361 /* clear irq state */
1362 net2272_write(dev
, IRQSTAT0
, 0xff);
1363 net2272_write(dev
, IRQSTAT1
, ~(1 << SUSPEND_REQUEST_INTERRUPT
));
1365 net2272_write(dev
, DMAREQ
,
1366 (0 << DMA_BUFFER_VALID
) |
1367 (0 << DMA_REQUEST_ENABLE
) |
1368 (1 << DMA_CONTROL_DACK
) |
1369 (dev
->dma_eot_polarity
<< EOT_POLARITY
) |
1370 (dev
->dma_dack_polarity
<< DACK_POLARITY
) |
1371 (dev
->dma_dreq_polarity
<< DREQ_POLARITY
) |
1372 ((dma_ep
>> 1) << DMA_ENDPOINT_SELECT
));
1374 net2272_cancel_dma(dev
);
1375 net2272_set_fifo_mode(dev
, (fifo_mode
<= 3) ? fifo_mode
: 0);
1377 /* Set the NET2272 ep fifo data width to 16-bit mode and for correct byte swapping
1378 * note that the higher level gadget drivers are expected to convert data to little endian.
1379 * Enable byte swap for your local bus/cpu if needed by setting BYTE_SWAP in LOCCTL here
1381 net2272_write(dev
, LOCCTL
, net2272_read(dev
, LOCCTL
) | (1 << DATA_WIDTH
));
1382 net2272_write(dev
, LOCCTL1
, (dma_mode
<< DMA_MODE
));
1386 net2272_usb_reinit(struct net2272
*dev
)
1390 /* basic endpoint init */
1391 for (i
= 0; i
< 4; ++i
) {
1392 struct net2272_ep
*ep
= &dev
->ep
[i
];
1394 ep
->ep
.name
= ep_name
[i
];
1399 if (use_dma
&& ep
->num
== dma_ep
)
1402 if (i
> 0 && i
<= 3)
1403 ep
->fifo_size
= 512;
1406 net2272_ep_reset(ep
);
1409 ep
->ep
.caps
.type_control
= true;
1411 ep
->ep
.caps
.type_iso
= true;
1412 ep
->ep
.caps
.type_bulk
= true;
1413 ep
->ep
.caps
.type_int
= true;
1416 ep
->ep
.caps
.dir_in
= true;
1417 ep
->ep
.caps
.dir_out
= true;
1419 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
, 64);
1421 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1422 dev
->ep
[0].stopped
= 0;
1423 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
1427 net2272_ep0_start(struct net2272
*dev
)
1429 struct net2272_ep
*ep0
= &dev
->ep
[0];
1431 net2272_ep_write(ep0
, EP_RSPSET
,
1432 (1 << NAK_OUT_PACKETS_MODE
) |
1433 (1 << ALT_NAK_OUT_PACKETS
));
1434 net2272_ep_write(ep0
, EP_RSPCLR
,
1435 (1 << HIDE_STATUS_PHASE
) |
1436 (1 << CONTROL_STATUS_PHASE_HANDSHAKE
));
1437 net2272_write(dev
, USBCTL0
,
1438 (dev
->softconnect
<< USB_DETECT_ENABLE
) |
1439 (1 << USB_ROOT_PORT_WAKEUP_ENABLE
) |
1440 (1 << IO_WAKEUP_ENABLE
));
1441 net2272_write(dev
, IRQENB0
,
1442 (1 << SETUP_PACKET_INTERRUPT_ENABLE
) |
1443 (1 << ENDPOINT_0_INTERRUPT_ENABLE
) |
1444 (1 << DMA_DONE_INTERRUPT_ENABLE
));
1445 net2272_write(dev
, IRQENB1
,
1446 (1 << VBUS_INTERRUPT_ENABLE
) |
1447 (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE
) |
1448 (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
));
1451 /* when a driver is successfully registered, it will receive
1452 * control requests including set_configuration(), which enables
1453 * non-control requests. then usb traffic follows until a
1454 * disconnect is reported. then a host may connect again, or
1455 * the driver might get unbound.
1457 static int net2272_start(struct usb_gadget
*_gadget
,
1458 struct usb_gadget_driver
*driver
)
1460 struct net2272
*dev
;
1463 if (!driver
|| !driver
->setup
||
1464 driver
->max_speed
!= USB_SPEED_HIGH
)
1467 dev
= container_of(_gadget
, struct net2272
, gadget
);
1469 for (i
= 0; i
< 4; ++i
)
1470 dev
->ep
[i
].irqs
= 0;
1471 /* hook up the driver ... */
1472 dev
->softconnect
= 1;
1473 driver
->driver
.bus
= NULL
;
1474 dev
->driver
= driver
;
1476 /* ... then enable host detection and ep0; and we're ready
1477 * for set_configuration as well as eventual disconnect.
1479 net2272_ep0_start(dev
);
1485 stop_activity(struct net2272
*dev
, struct usb_gadget_driver
*driver
)
1489 /* don't disconnect if it's not connected */
1490 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1493 /* stop hardware; prevent new request submissions;
1494 * and kill any outstanding requests.
1496 net2272_usb_reset(dev
);
1497 for (i
= 0; i
< 4; ++i
)
1498 net2272_dequeue_all(&dev
->ep
[i
]);
1500 /* report disconnect; the driver is already quiesced */
1502 spin_unlock(&dev
->lock
);
1503 driver
->disconnect(&dev
->gadget
);
1504 spin_lock(&dev
->lock
);
1507 net2272_usb_reinit(dev
);
1510 static int net2272_stop(struct usb_gadget
*_gadget
)
1512 struct net2272
*dev
;
1513 unsigned long flags
;
1515 dev
= container_of(_gadget
, struct net2272
, gadget
);
1517 spin_lock_irqsave(&dev
->lock
, flags
);
1518 stop_activity(dev
, NULL
);
1519 spin_unlock_irqrestore(&dev
->lock
, flags
);
1526 /*---------------------------------------------------------------------------*/
1527 /* handle ep-a/ep-b dma completions */
1529 net2272_handle_dma(struct net2272_ep
*ep
)
1531 struct net2272_request
*req
;
1535 if (!list_empty(&ep
->queue
))
1536 req
= list_entry(ep
->queue
.next
,
1537 struct net2272_request
, queue
);
1541 dev_vdbg(ep
->dev
->dev
, "handle_dma %s req %p\n", ep
->ep
.name
, req
);
1543 /* Ensure DREQ is de-asserted */
1544 net2272_write(ep
->dev
, DMAREQ
,
1545 (0 << DMA_BUFFER_VALID
)
1546 | (0 << DMA_REQUEST_ENABLE
)
1547 | (1 << DMA_CONTROL_DACK
)
1548 | (ep
->dev
->dma_eot_polarity
<< EOT_POLARITY
)
1549 | (ep
->dev
->dma_dack_polarity
<< DACK_POLARITY
)
1550 | (ep
->dev
->dma_dreq_polarity
<< DREQ_POLARITY
)
1551 | (ep
->dma
<< DMA_ENDPOINT_SELECT
));
1553 ep
->dev
->dma_busy
= 0;
1555 net2272_ep_write(ep
, EP_IRQENB
,
1556 (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
)
1557 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
)
1558 | net2272_ep_read(ep
, EP_IRQENB
));
1560 /* device-to-host transfer completed */
1562 /* validate a short packet or zlp if necessary */
1563 if ((req
->req
.length
% ep
->ep
.maxpacket
!= 0) ||
1565 set_fifo_bytecount(ep
, 0);
1567 net2272_done(ep
, req
, 0);
1568 if (!list_empty(&ep
->queue
)) {
1569 req
= list_entry(ep
->queue
.next
,
1570 struct net2272_request
, queue
);
1571 status
= net2272_kick_dma(ep
, req
);
1573 net2272_pio_advance(ep
);
1576 /* host-to-device transfer completed */
1578 /* terminated with a short packet? */
1579 if (net2272_read(ep
->dev
, IRQSTAT0
) &
1580 (1 << DMA_DONE_INTERRUPT
)) {
1581 /* abort system dma */
1582 net2272_cancel_dma(ep
->dev
);
1585 /* EP_TRANSFER will contain the number of bytes
1586 * actually received.
1587 * NOTE: There is no overflow detection on EP_TRANSFER:
1588 * We can't deal with transfers larger than 2^24 bytes!
1590 len
= (net2272_ep_read(ep
, EP_TRANSFER2
) << 16)
1591 | (net2272_ep_read(ep
, EP_TRANSFER1
) << 8)
1592 | (net2272_ep_read(ep
, EP_TRANSFER0
));
1597 req
->req
.actual
+= len
;
1599 /* get any remaining data */
1600 net2272_pio_advance(ep
);
1604 /*---------------------------------------------------------------------------*/
1607 net2272_handle_ep(struct net2272_ep
*ep
)
1609 struct net2272_request
*req
;
1612 if (!list_empty(&ep
->queue
))
1613 req
= list_entry(ep
->queue
.next
,
1614 struct net2272_request
, queue
);
1618 /* ack all, and handle what we care about */
1619 stat0
= net2272_ep_read(ep
, EP_STAT0
);
1620 stat1
= net2272_ep_read(ep
, EP_STAT1
);
1623 dev_vdbg(ep
->dev
->dev
, "%s ack ep_stat0 %02x, ep_stat1 %02x, req %p\n",
1624 ep
->ep
.name
, stat0
, stat1
, req
? &req
->req
: NULL
);
1626 net2272_ep_write(ep
, EP_STAT0
, stat0
&
1627 ~((1 << NAK_OUT_PACKETS
)
1628 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)));
1629 net2272_ep_write(ep
, EP_STAT1
, stat1
);
1631 /* data packet(s) received (in the fifo, OUT)
1632 * direction must be validated, otherwise control read status phase
1633 * could be interpreted as a valid packet
1635 if (!ep
->is_in
&& (stat0
& (1 << DATA_PACKET_RECEIVED_INTERRUPT
)))
1636 net2272_pio_advance(ep
);
1637 /* data packet(s) transmitted (IN) */
1638 else if (stat0
& (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
))
1639 net2272_pio_advance(ep
);
1642 static struct net2272_ep
*
1643 net2272_get_ep_by_addr(struct net2272
*dev
, u16 wIndex
)
1645 struct net2272_ep
*ep
;
1647 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
1650 list_for_each_entry(ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
1651 u8 bEndpointAddress
;
1655 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
1656 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
1658 if ((wIndex
& 0x0f) == (bEndpointAddress
& 0x0f))
1669 * JJJJJJJKKKKKKK * 8
1671 * {JKKKKKKK * 10}, JK
1673 static const u8 net2272_test_packet
[] = {
1674 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1675 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1676 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1677 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1678 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1679 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFD, 0x7E
1683 net2272_set_test_mode(struct net2272
*dev
, int mode
)
1687 /* Disable all net2272 interrupts:
1688 * Nothing but a power cycle should stop the test.
1690 net2272_write(dev
, IRQENB0
, 0x00);
1691 net2272_write(dev
, IRQENB1
, 0x00);
1693 /* Force tranceiver to high-speed */
1694 net2272_write(dev
, XCVRDIAG
, 1 << FORCE_HIGH_SPEED
);
1696 net2272_write(dev
, PAGESEL
, 0);
1697 net2272_write(dev
, EP_STAT0
, 1 << DATA_PACKET_TRANSMITTED_INTERRUPT
);
1698 net2272_write(dev
, EP_RSPCLR
,
1699 (1 << CONTROL_STATUS_PHASE_HANDSHAKE
)
1700 | (1 << HIDE_STATUS_PHASE
));
1701 net2272_write(dev
, EP_CFG
, 1 << ENDPOINT_DIRECTION
);
1702 net2272_write(dev
, EP_STAT1
, 1 << BUFFER_FLUSH
);
1704 /* wait for status phase to complete */
1705 while (!(net2272_read(dev
, EP_STAT0
) &
1706 (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)))
1709 /* Enable test mode */
1710 net2272_write(dev
, USBTEST
, mode
);
1712 /* load test packet */
1713 if (mode
== TEST_PACKET
) {
1714 /* switch to 8 bit mode */
1715 net2272_write(dev
, LOCCTL
, net2272_read(dev
, LOCCTL
) &
1716 ~(1 << DATA_WIDTH
));
1718 for (i
= 0; i
< sizeof(net2272_test_packet
); ++i
)
1719 net2272_write(dev
, EP_DATA
, net2272_test_packet
[i
]);
1721 /* Validate test packet */
1722 net2272_write(dev
, EP_TRANSFER0
, 0);
1727 net2272_handle_stat0_irqs(struct net2272
*dev
, u8 stat
)
1729 struct net2272_ep
*ep
;
1732 /* starting a control request? */
1733 if (unlikely(stat
& (1 << SETUP_PACKET_INTERRUPT
))) {
1736 struct usb_ctrlrequest r
;
1739 struct net2272_request
*req
;
1741 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1742 if (net2272_read(dev
, USBCTL1
) & (1 << USB_HIGH_SPEED
))
1743 dev
->gadget
.speed
= USB_SPEED_HIGH
;
1745 dev
->gadget
.speed
= USB_SPEED_FULL
;
1746 dev_dbg(dev
->dev
, "%s\n",
1747 usb_speed_string(dev
->gadget
.speed
));
1753 /* make sure any leftover interrupt state is cleared */
1754 stat
&= ~(1 << ENDPOINT_0_INTERRUPT
);
1755 while (!list_empty(&ep
->queue
)) {
1756 req
= list_entry(ep
->queue
.next
,
1757 struct net2272_request
, queue
);
1758 net2272_done(ep
, req
,
1759 (req
->req
.actual
== req
->req
.length
) ? 0 : -EPROTO
);
1762 dev
->protocol_stall
= 0;
1763 net2272_ep_write(ep
, EP_STAT0
,
1764 (1 << DATA_IN_TOKEN_INTERRUPT
)
1765 | (1 << DATA_OUT_TOKEN_INTERRUPT
)
1766 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
1767 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
1768 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
));
1769 net2272_ep_write(ep
, EP_STAT1
,
1771 | (1 << USB_OUT_ACK_SENT
)
1772 | (1 << USB_OUT_NAK_SENT
)
1773 | (1 << USB_IN_ACK_RCVD
)
1774 | (1 << USB_IN_NAK_SENT
)
1775 | (1 << USB_STALL_SENT
)
1776 | (1 << LOCAL_OUT_ZLP
));
1779 * Ensure Control Read pre-validation setting is beyond maximum size
1780 * - Control Writes can leave non-zero values in EP_TRANSFER. If
1781 * an EP0 transfer following the Control Write is a Control Read,
1782 * the NET2272 sees the non-zero EP_TRANSFER as an unexpected
1783 * pre-validation count.
1784 * - Setting EP_TRANSFER beyond the maximum EP0 transfer size ensures
1785 * the pre-validation count cannot cause an unexpected validatation
1787 net2272_write(dev
, PAGESEL
, 0);
1788 net2272_write(dev
, EP_TRANSFER2
, 0xff);
1789 net2272_write(dev
, EP_TRANSFER1
, 0xff);
1790 net2272_write(dev
, EP_TRANSFER0
, 0xff);
1792 u
.raw
[0] = net2272_read(dev
, SETUP0
);
1793 u
.raw
[1] = net2272_read(dev
, SETUP1
);
1794 u
.raw
[2] = net2272_read(dev
, SETUP2
);
1795 u
.raw
[3] = net2272_read(dev
, SETUP3
);
1796 u
.raw
[4] = net2272_read(dev
, SETUP4
);
1797 u
.raw
[5] = net2272_read(dev
, SETUP5
);
1798 u
.raw
[6] = net2272_read(dev
, SETUP6
);
1799 u
.raw
[7] = net2272_read(dev
, SETUP7
);
1801 * If you have a big endian cpu make sure le16_to_cpus
1802 * performs the proper byte swapping here...
1804 le16_to_cpus(&u
.r
.wValue
);
1805 le16_to_cpus(&u
.r
.wIndex
);
1806 le16_to_cpus(&u
.r
.wLength
);
1809 net2272_write(dev
, IRQSTAT0
, 1 << SETUP_PACKET_INTERRUPT
);
1810 stat
^= (1 << SETUP_PACKET_INTERRUPT
);
1812 /* watch control traffic at the token level, and force
1813 * synchronization before letting the status phase happen.
1815 ep
->is_in
= (u
.r
.bRequestType
& USB_DIR_IN
) != 0;
1817 scratch
= (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
)
1818 | (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE
)
1819 | (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE
);
1820 stop_out_naking(ep
);
1822 scratch
= (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
)
1823 | (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE
)
1824 | (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE
);
1825 net2272_ep_write(ep
, EP_IRQENB
, scratch
);
1827 if ((u
.r
.bRequestType
& USB_TYPE_MASK
) != USB_TYPE_STANDARD
)
1829 switch (u
.r
.bRequest
) {
1830 case USB_REQ_GET_STATUS
: {
1831 struct net2272_ep
*e
;
1834 switch (u
.r
.bRequestType
& USB_RECIP_MASK
) {
1835 case USB_RECIP_ENDPOINT
:
1836 e
= net2272_get_ep_by_addr(dev
, u
.r
.wIndex
);
1837 if (!e
|| u
.r
.wLength
> 2)
1839 if (net2272_ep_read(e
, EP_RSPSET
) & (1 << ENDPOINT_HALT
))
1840 status
= cpu_to_le16(1);
1842 status
= cpu_to_le16(0);
1844 /* don't bother with a request object! */
1845 net2272_ep_write(&dev
->ep
[0], EP_IRQENB
, 0);
1846 writew(status
, net2272_reg_addr(dev
, EP_DATA
));
1847 set_fifo_bytecount(&dev
->ep
[0], 0);
1849 dev_vdbg(dev
->dev
, "%s stat %02x\n",
1850 ep
->ep
.name
, status
);
1851 goto next_endpoints
;
1852 case USB_RECIP_DEVICE
:
1853 if (u
.r
.wLength
> 2)
1855 if (dev
->gadget
.is_selfpowered
)
1856 status
= (1 << USB_DEVICE_SELF_POWERED
);
1858 /* don't bother with a request object! */
1859 net2272_ep_write(&dev
->ep
[0], EP_IRQENB
, 0);
1860 writew(status
, net2272_reg_addr(dev
, EP_DATA
));
1861 set_fifo_bytecount(&dev
->ep
[0], 0);
1863 dev_vdbg(dev
->dev
, "device stat %02x\n", status
);
1864 goto next_endpoints
;
1865 case USB_RECIP_INTERFACE
:
1866 if (u
.r
.wLength
> 2)
1869 /* don't bother with a request object! */
1870 net2272_ep_write(&dev
->ep
[0], EP_IRQENB
, 0);
1871 writew(status
, net2272_reg_addr(dev
, EP_DATA
));
1872 set_fifo_bytecount(&dev
->ep
[0], 0);
1874 dev_vdbg(dev
->dev
, "interface status %02x\n", status
);
1875 goto next_endpoints
;
1880 case USB_REQ_CLEAR_FEATURE
: {
1881 struct net2272_ep
*e
;
1883 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
1885 if (u
.r
.wValue
!= USB_ENDPOINT_HALT
||
1888 e
= net2272_get_ep_by_addr(dev
, u
.r
.wIndex
);
1892 dev_vdbg(dev
->dev
, "%s wedged, halt not cleared\n",
1895 dev_vdbg(dev
->dev
, "%s clear halt\n", ep
->ep
.name
);
1899 goto next_endpoints
;
1901 case USB_REQ_SET_FEATURE
: {
1902 struct net2272_ep
*e
;
1904 if (u
.r
.bRequestType
== USB_RECIP_DEVICE
) {
1905 if (u
.r
.wIndex
!= NORMAL_OPERATION
)
1906 net2272_set_test_mode(dev
, (u
.r
.wIndex
>> 8));
1908 dev_vdbg(dev
->dev
, "test mode: %d\n", u
.r
.wIndex
);
1909 goto next_endpoints
;
1910 } else if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
1912 if (u
.r
.wValue
!= USB_ENDPOINT_HALT
||
1915 e
= net2272_get_ep_by_addr(dev
, u
.r
.wIndex
);
1920 dev_vdbg(dev
->dev
, "%s set halt\n", ep
->ep
.name
);
1921 goto next_endpoints
;
1923 case USB_REQ_SET_ADDRESS
: {
1924 net2272_write(dev
, OURADDR
, u
.r
.wValue
& 0xff);
1930 dev_vdbg(dev
->dev
, "setup %02x.%02x v%04x i%04x "
1932 u
.r
.bRequestType
, u
.r
.bRequest
,
1933 u
.r
.wValue
, u
.r
.wIndex
,
1934 net2272_ep_read(ep
, EP_CFG
));
1935 spin_unlock(&dev
->lock
);
1936 tmp
= dev
->driver
->setup(&dev
->gadget
, &u
.r
);
1937 spin_lock(&dev
->lock
);
1940 /* stall ep0 on error */
1943 dev_vdbg(dev
->dev
, "req %02x.%02x protocol STALL; stat %d\n",
1944 u
.r
.bRequestType
, u
.r
.bRequest
, tmp
);
1945 dev
->protocol_stall
= 1;
1947 /* endpoint dma irq? */
1948 } else if (stat
& (1 << DMA_DONE_INTERRUPT
)) {
1949 net2272_cancel_dma(dev
);
1950 net2272_write(dev
, IRQSTAT0
, 1 << DMA_DONE_INTERRUPT
);
1951 stat
&= ~(1 << DMA_DONE_INTERRUPT
);
1952 num
= (net2272_read(dev
, DMAREQ
) & (1 << DMA_ENDPOINT_SELECT
))
1956 net2272_handle_dma(ep
);
1960 /* endpoint data irq? */
1961 scratch
= stat
& 0x0f;
1963 for (num
= 0; scratch
; num
++) {
1966 /* does this endpoint's FIFO and queue need tending? */
1968 if ((scratch
& t
) == 0)
1973 net2272_handle_ep(ep
);
1976 /* some interrupts we can just ignore */
1977 stat
&= ~(1 << SOF_INTERRUPT
);
1980 dev_dbg(dev
->dev
, "unhandled irqstat0 %02x\n", stat
);
1984 net2272_handle_stat1_irqs(struct net2272
*dev
, u8 stat
)
1988 /* after disconnect there's nothing else to do! */
1989 tmp
= (1 << VBUS_INTERRUPT
) | (1 << ROOT_PORT_RESET_INTERRUPT
);
1990 mask
= (1 << USB_HIGH_SPEED
) | (1 << USB_FULL_SPEED
);
1994 bool disconnect
= false;
1997 * Ignore disconnects and resets if the speed hasn't been set.
1998 * VBUS can bounce and there's always an initial reset.
2000 net2272_write(dev
, IRQSTAT1
, tmp
);
2001 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
2002 if ((stat
& (1 << VBUS_INTERRUPT
)) &&
2003 (net2272_read(dev
, USBCTL1
) &
2004 (1 << VBUS_PIN
)) == 0) {
2006 dev_dbg(dev
->dev
, "disconnect %s\n",
2007 dev
->driver
->driver
.name
);
2008 } else if ((stat
& (1 << ROOT_PORT_RESET_INTERRUPT
)) &&
2009 (net2272_read(dev
, USBCTL1
) & mask
)
2012 dev_dbg(dev
->dev
, "reset %s\n",
2013 dev
->driver
->driver
.name
);
2016 if (disconnect
|| reset
) {
2017 stop_activity(dev
, dev
->driver
);
2018 net2272_ep0_start(dev
);
2019 spin_unlock(&dev
->lock
);
2021 usb_gadget_udc_reset
2022 (&dev
->gadget
, dev
->driver
);
2024 (dev
->driver
->disconnect
)
2026 spin_lock(&dev
->lock
);
2036 tmp
= (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT
);
2038 net2272_write(dev
, IRQSTAT1
, tmp
);
2039 if (stat
& (1 << SUSPEND_REQUEST_INTERRUPT
)) {
2040 if (dev
->driver
->suspend
)
2041 dev
->driver
->suspend(&dev
->gadget
);
2042 if (!enable_suspend
) {
2043 stat
&= ~(1 << SUSPEND_REQUEST_INTERRUPT
);
2044 dev_dbg(dev
->dev
, "Suspend disabled, ignoring\n");
2047 if (dev
->driver
->resume
)
2048 dev
->driver
->resume(&dev
->gadget
);
2053 /* clear any other status/irqs */
2055 net2272_write(dev
, IRQSTAT1
, stat
);
2057 /* some status we can just ignore */
2058 stat
&= ~((1 << CONTROL_STATUS_INTERRUPT
)
2059 | (1 << SUSPEND_REQUEST_INTERRUPT
)
2060 | (1 << RESUME_INTERRUPT
));
2064 dev_dbg(dev
->dev
, "unhandled irqstat1 %02x\n", stat
);
2067 static irqreturn_t
net2272_irq(int irq
, void *_dev
)
2069 struct net2272
*dev
= _dev
;
2070 #if defined(PLX_PCI_RDK) || defined(PLX_PCI_RDK2)
2073 #if defined(PLX_PCI_RDK)
2076 spin_lock(&dev
->lock
);
2077 #if defined(PLX_PCI_RDK)
2078 intcsr
= readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2080 if ((intcsr
& LOCAL_INTERRUPT_TEST
) == LOCAL_INTERRUPT_TEST
) {
2081 writel(intcsr
& ~(1 << PCI_INTERRUPT_ENABLE
),
2082 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2083 net2272_handle_stat1_irqs(dev
, net2272_read(dev
, IRQSTAT1
));
2084 net2272_handle_stat0_irqs(dev
, net2272_read(dev
, IRQSTAT0
));
2085 intcsr
= readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2086 writel(intcsr
| (1 << PCI_INTERRUPT_ENABLE
),
2087 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2089 if ((intcsr
& DMA_CHANNEL_0_TEST
) == DMA_CHANNEL_0_TEST
) {
2090 writeb((1 << CHANNEL_CLEAR_INTERRUPT
| (0 << CHANNEL_ENABLE
)),
2091 dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
2093 dmareq
= net2272_read(dev
, DMAREQ
);
2095 net2272_handle_dma(&dev
->ep
[2]);
2097 net2272_handle_dma(&dev
->ep
[1]);
2100 #if defined(PLX_PCI_RDK2)
2101 /* see if PCI int for us by checking irqstat */
2102 intcsr
= readl(dev
->rdk2
.fpga_base_addr
+ RDK2_IRQSTAT
);
2103 if (!intcsr
& (1 << NET2272_PCI_IRQ
)) {
2104 spin_unlock(&dev
->lock
);
2107 /* check dma interrupts */
2109 /* Platform/devcice interrupt handler */
2110 #if !defined(PLX_PCI_RDK)
2111 net2272_handle_stat1_irqs(dev
, net2272_read(dev
, IRQSTAT1
));
2112 net2272_handle_stat0_irqs(dev
, net2272_read(dev
, IRQSTAT0
));
2114 spin_unlock(&dev
->lock
);
2119 static int net2272_present(struct net2272
*dev
)
2122 * Quick test to see if CPU can communicate properly with the NET2272.
2123 * Verifies connection using writes and reads to write/read and
2124 * read-only registers.
2126 * This routine is strongly recommended especially during early bring-up
2127 * of new hardware, however for designs that do not apply Power On System
2128 * Tests (POST) it may discarded (or perhaps minimized).
2133 /* Verify NET2272 write/read SCRATCH register can write and read */
2134 refval
= net2272_read(dev
, SCRATCH
);
2135 for (ii
= 0; ii
< 0x100; ii
+= 7) {
2136 net2272_write(dev
, SCRATCH
, ii
);
2137 val
= net2272_read(dev
, SCRATCH
);
2140 "%s: write/read SCRATCH register test failed: "
2141 "wrote:0x%2.2x, read:0x%2.2x\n",
2146 /* To be nice, we write the original SCRATCH value back: */
2147 net2272_write(dev
, SCRATCH
, refval
);
2149 /* Verify NET2272 CHIPREV register is read-only: */
2150 refval
= net2272_read(dev
, CHIPREV_2272
);
2151 for (ii
= 0; ii
< 0x100; ii
+= 7) {
2152 net2272_write(dev
, CHIPREV_2272
, ii
);
2153 val
= net2272_read(dev
, CHIPREV_2272
);
2154 if (val
!= refval
) {
2156 "%s: write/read CHIPREV register test failed: "
2157 "wrote 0x%2.2x, read:0x%2.2x expected:0x%2.2x\n",
2158 __func__
, ii
, val
, refval
);
2164 * Verify NET2272's "NET2270 legacy revision" register
2165 * - NET2272 has two revision registers. The NET2270 legacy revision
2166 * register should read the same value, regardless of the NET2272
2167 * silicon revision. The legacy register applies to NET2270
2168 * firmware being applied to the NET2272.
2170 val
= net2272_read(dev
, CHIPREV_LEGACY
);
2171 if (val
!= NET2270_LEGACY_REV
) {
2173 * Unexpected legacy revision value
2174 * - Perhaps the chip is a NET2270?
2177 "%s: WARNING: UNEXPECTED NET2272 LEGACY REGISTER VALUE:\n"
2178 " - CHIPREV_LEGACY: expected 0x%2.2x, got:0x%2.2x. (Not NET2272?)\n",
2179 __func__
, NET2270_LEGACY_REV
, val
);
2184 * Verify NET2272 silicon revision
2185 * - This revision register is appropriate for the silicon version
2188 val
= net2272_read(dev
, CHIPREV_2272
);
2190 case CHIPREV_NET2272_R1
:
2192 * NET2272 Rev 1 has DMA related errata:
2193 * - Newer silicon (Rev 1A or better) required
2196 "%s: Rev 1 detected: newer silicon recommended for DMA support\n",
2199 case CHIPREV_NET2272_R1A
:
2202 /* NET2272 silicon version *may* not work with this firmware */
2204 "%s: unexpected silicon revision register value: "
2205 " CHIPREV_2272: 0x%2.2x\n",
2208 * Return Success, even though the chip rev is not an expected value
2209 * - Older, pre-built firmware can attempt to operate on newer silicon
2210 * - Often, new silicon is perfectly compatible
2214 /* Success: NET2272 checks out OK */
2219 net2272_gadget_release(struct device
*_dev
)
2221 struct net2272
*dev
= dev_get_drvdata(_dev
);
2225 /*---------------------------------------------------------------------------*/
2228 net2272_remove(struct net2272
*dev
)
2230 usb_del_gadget_udc(&dev
->gadget
);
2231 free_irq(dev
->irq
, dev
);
2232 iounmap(dev
->base_addr
);
2233 device_remove_file(dev
->dev
, &dev_attr_registers
);
2235 dev_info(dev
->dev
, "unbind\n");
2238 static struct net2272
*net2272_probe_init(struct device
*dev
, unsigned int irq
)
2240 struct net2272
*ret
;
2243 dev_dbg(dev
, "No IRQ!\n");
2244 return ERR_PTR(-ENODEV
);
2247 /* alloc, and start init */
2248 ret
= kzalloc(sizeof(*ret
), GFP_KERNEL
);
2250 return ERR_PTR(-ENOMEM
);
2252 spin_lock_init(&ret
->lock
);
2255 ret
->gadget
.ops
= &net2272_ops
;
2256 ret
->gadget
.max_speed
= USB_SPEED_HIGH
;
2258 /* the "gadget" abstracts/virtualizes the controller */
2259 ret
->gadget
.name
= driver_name
;
2265 net2272_probe_fin(struct net2272
*dev
, unsigned int irqflags
)
2269 /* See if there... */
2270 if (net2272_present(dev
)) {
2271 dev_warn(dev
->dev
, "2272 not found!\n");
2276 net2272_usb_reset(dev
);
2277 net2272_usb_reinit(dev
);
2279 ret
= request_irq(dev
->irq
, net2272_irq
, irqflags
, driver_name
, dev
);
2281 dev_err(dev
->dev
, "request interrupt %i failed\n", dev
->irq
);
2285 dev
->chiprev
= net2272_read(dev
, CHIPREV_2272
);
2288 dev_info(dev
->dev
, "%s\n", driver_desc
);
2289 dev_info(dev
->dev
, "irq %i, mem %p, chip rev %04x, dma %s\n",
2290 dev
->irq
, dev
->base_addr
, dev
->chiprev
,
2292 dev_info(dev
->dev
, "version: %s\n", driver_vers
);
2294 ret
= device_create_file(dev
->dev
, &dev_attr_registers
);
2298 ret
= usb_add_gadget_udc_release(dev
->dev
, &dev
->gadget
,
2299 net2272_gadget_release
);
2306 device_remove_file(dev
->dev
, &dev_attr_registers
);
2308 free_irq(dev
->irq
, dev
);
2316 * wrap this driver around the specified device, but
2317 * don't respond over USB until a gadget driver binds to us
2321 net2272_rdk1_probe(struct pci_dev
*pdev
, struct net2272
*dev
)
2323 unsigned long resource
, len
, tmp
;
2324 void __iomem
*mem_mapped_addr
[4];
2328 * BAR 0 holds PLX 9054 config registers
2329 * BAR 1 is i/o memory; unused here
2330 * BAR 2 holds EPLD config registers
2331 * BAR 3 holds NET2272 registers
2334 /* Find and map all address spaces */
2335 for (i
= 0; i
< 4; ++i
) {
2337 continue; /* BAR1 unused */
2339 resource
= pci_resource_start(pdev
, i
);
2340 len
= pci_resource_len(pdev
, i
);
2342 if (!request_mem_region(resource
, len
, driver_name
)) {
2343 dev_dbg(dev
->dev
, "controller already in use\n");
2348 mem_mapped_addr
[i
] = ioremap_nocache(resource
, len
);
2349 if (mem_mapped_addr
[i
] == NULL
) {
2350 release_mem_region(resource
, len
);
2351 dev_dbg(dev
->dev
, "can't map memory\n");
2357 dev
->rdk1
.plx9054_base_addr
= mem_mapped_addr
[0];
2358 dev
->rdk1
.epld_base_addr
= mem_mapped_addr
[2];
2359 dev
->base_addr
= mem_mapped_addr
[3];
2361 /* Set PLX 9054 bus width (16 bits) */
2362 tmp
= readl(dev
->rdk1
.plx9054_base_addr
+ LBRD1
);
2363 writel((tmp
& ~(3 << MEMORY_SPACE_LOCAL_BUS_WIDTH
)) | W16_BIT
,
2364 dev
->rdk1
.plx9054_base_addr
+ LBRD1
);
2366 /* Enable PLX 9054 Interrupts */
2367 writel(readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
) |
2368 (1 << PCI_INTERRUPT_ENABLE
) |
2369 (1 << LOCAL_INTERRUPT_INPUT_ENABLE
),
2370 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2372 writeb((1 << CHANNEL_CLEAR_INTERRUPT
| (0 << CHANNEL_ENABLE
)),
2373 dev
->rdk1
.plx9054_base_addr
+ DMACSR0
);
2376 writeb((1 << EPLD_DMA_ENABLE
) |
2377 (1 << DMA_CTL_DACK
) |
2378 (1 << DMA_TIMEOUT_ENABLE
) |
2382 (1 << NET2272_RESET
),
2383 dev
->base_addr
+ EPLD_IO_CONTROL_REGISTER
);
2386 writeb(readb(dev
->base_addr
+ EPLD_IO_CONTROL_REGISTER
) &
2387 ~(1 << NET2272_RESET
),
2388 dev
->base_addr
+ EPLD_IO_CONTROL_REGISTER
);
2395 iounmap(mem_mapped_addr
[i
]);
2396 release_mem_region(pci_resource_start(pdev
, i
),
2397 pci_resource_len(pdev
, i
));
2404 net2272_rdk2_probe(struct pci_dev
*pdev
, struct net2272
*dev
)
2406 unsigned long resource
, len
;
2407 void __iomem
*mem_mapped_addr
[2];
2411 * BAR 0 holds FGPA config registers
2412 * BAR 1 holds NET2272 registers
2415 /* Find and map all address spaces, bar2-3 unused in rdk 2 */
2416 for (i
= 0; i
< 2; ++i
) {
2417 resource
= pci_resource_start(pdev
, i
);
2418 len
= pci_resource_len(pdev
, i
);
2420 if (!request_mem_region(resource
, len
, driver_name
)) {
2421 dev_dbg(dev
->dev
, "controller already in use\n");
2426 mem_mapped_addr
[i
] = ioremap_nocache(resource
, len
);
2427 if (mem_mapped_addr
[i
] == NULL
) {
2428 release_mem_region(resource
, len
);
2429 dev_dbg(dev
->dev
, "can't map memory\n");
2435 dev
->rdk2
.fpga_base_addr
= mem_mapped_addr
[0];
2436 dev
->base_addr
= mem_mapped_addr
[1];
2439 /* Set 2272 bus width (16 bits) and reset */
2440 writel((1 << CHIP_RESET
), dev
->rdk2
.fpga_base_addr
+ RDK2_LOCCTLRDK
);
2442 writel((1 << BUS_WIDTH
), dev
->rdk2
.fpga_base_addr
+ RDK2_LOCCTLRDK
);
2443 /* Print fpga version number */
2444 dev_info(dev
->dev
, "RDK2 FPGA version %08x\n",
2445 readl(dev
->rdk2
.fpga_base_addr
+ RDK2_FPGAREV
));
2446 /* Enable FPGA Interrupts */
2447 writel((1 << NET2272_PCI_IRQ
), dev
->rdk2
.fpga_base_addr
+ RDK2_IRQENB
);
2453 iounmap(mem_mapped_addr
[i
]);
2454 release_mem_region(pci_resource_start(pdev
, i
),
2455 pci_resource_len(pdev
, i
));
2462 net2272_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2464 struct net2272
*dev
;
2467 dev
= net2272_probe_init(&pdev
->dev
, pdev
->irq
);
2469 return PTR_ERR(dev
);
2470 dev
->dev_id
= pdev
->device
;
2472 if (pci_enable_device(pdev
) < 0) {
2477 pci_set_master(pdev
);
2479 switch (pdev
->device
) {
2480 case PCI_DEVICE_ID_RDK1
: ret
= net2272_rdk1_probe(pdev
, dev
); break;
2481 case PCI_DEVICE_ID_RDK2
: ret
= net2272_rdk2_probe(pdev
, dev
); break;
2487 ret
= net2272_probe_fin(dev
, 0);
2491 pci_set_drvdata(pdev
, dev
);
2496 pci_disable_device(pdev
);
2504 net2272_rdk1_remove(struct pci_dev
*pdev
, struct net2272
*dev
)
2508 /* disable PLX 9054 interrupts */
2509 writel(readl(dev
->rdk1
.plx9054_base_addr
+ INTCSR
) &
2510 ~(1 << PCI_INTERRUPT_ENABLE
),
2511 dev
->rdk1
.plx9054_base_addr
+ INTCSR
);
2513 /* clean up resources allocated during probe() */
2514 iounmap(dev
->rdk1
.plx9054_base_addr
);
2515 iounmap(dev
->rdk1
.epld_base_addr
);
2517 for (i
= 0; i
< 4; ++i
) {
2519 continue; /* BAR1 unused */
2520 release_mem_region(pci_resource_start(pdev
, i
),
2521 pci_resource_len(pdev
, i
));
2526 net2272_rdk2_remove(struct pci_dev
*pdev
, struct net2272
*dev
)
2530 /* disable fpga interrupts
2531 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2532 ~(1 << PCI_INTERRUPT_ENABLE),
2533 dev->rdk1.plx9054_base_addr + INTCSR);
2536 /* clean up resources allocated during probe() */
2537 iounmap(dev
->rdk2
.fpga_base_addr
);
2539 for (i
= 0; i
< 2; ++i
)
2540 release_mem_region(pci_resource_start(pdev
, i
),
2541 pci_resource_len(pdev
, i
));
2545 net2272_pci_remove(struct pci_dev
*pdev
)
2547 struct net2272
*dev
= pci_get_drvdata(pdev
);
2549 net2272_remove(dev
);
2551 switch (pdev
->device
) {
2552 case PCI_DEVICE_ID_RDK1
: net2272_rdk1_remove(pdev
, dev
); break;
2553 case PCI_DEVICE_ID_RDK2
: net2272_rdk2_remove(pdev
, dev
); break;
2557 pci_disable_device(pdev
);
2562 /* Table of matching PCI IDs */
2563 static struct pci_device_id pci_ids
[] = {
2565 .class = ((PCI_CLASS_BRIDGE_OTHER
<< 8) | 0xfe),
2567 .vendor
= PCI_VENDOR_ID_PLX
,
2568 .device
= PCI_DEVICE_ID_RDK1
,
2569 .subvendor
= PCI_ANY_ID
,
2570 .subdevice
= PCI_ANY_ID
,
2573 .class = ((PCI_CLASS_BRIDGE_OTHER
<< 8) | 0xfe),
2575 .vendor
= PCI_VENDOR_ID_PLX
,
2576 .device
= PCI_DEVICE_ID_RDK2
,
2577 .subvendor
= PCI_ANY_ID
,
2578 .subdevice
= PCI_ANY_ID
,
2582 MODULE_DEVICE_TABLE(pci
, pci_ids
);
2584 static struct pci_driver net2272_pci_driver
= {
2585 .name
= driver_name
,
2586 .id_table
= pci_ids
,
2588 .probe
= net2272_pci_probe
,
2589 .remove
= net2272_pci_remove
,
2592 static int net2272_pci_register(void)
2594 return pci_register_driver(&net2272_pci_driver
);
2597 static void net2272_pci_unregister(void)
2599 pci_unregister_driver(&net2272_pci_driver
);
2603 static inline int net2272_pci_register(void) { return 0; }
2604 static inline void net2272_pci_unregister(void) { }
2607 /*---------------------------------------------------------------------------*/
2610 net2272_plat_probe(struct platform_device
*pdev
)
2612 struct net2272
*dev
;
2614 unsigned int irqflags
;
2615 resource_size_t base
, len
;
2616 struct resource
*iomem
, *iomem_bus
, *irq_res
;
2618 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
2619 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2620 iomem_bus
= platform_get_resource(pdev
, IORESOURCE_BUS
, 0);
2621 if (!irq_res
|| !iomem
) {
2622 dev_err(&pdev
->dev
, "must provide irq/base addr");
2626 dev
= net2272_probe_init(&pdev
->dev
, irq_res
->start
);
2628 return PTR_ERR(dev
);
2631 if (irq_res
->flags
& IORESOURCE_IRQ_HIGHEDGE
)
2632 irqflags
|= IRQF_TRIGGER_RISING
;
2633 if (irq_res
->flags
& IORESOURCE_IRQ_LOWEDGE
)
2634 irqflags
|= IRQF_TRIGGER_FALLING
;
2635 if (irq_res
->flags
& IORESOURCE_IRQ_HIGHLEVEL
)
2636 irqflags
|= IRQF_TRIGGER_HIGH
;
2637 if (irq_res
->flags
& IORESOURCE_IRQ_LOWLEVEL
)
2638 irqflags
|= IRQF_TRIGGER_LOW
;
2640 base
= iomem
->start
;
2641 len
= resource_size(iomem
);
2643 dev
->base_shift
= iomem_bus
->start
;
2645 if (!request_mem_region(base
, len
, driver_name
)) {
2646 dev_dbg(dev
->dev
, "get request memory region!\n");
2650 dev
->base_addr
= ioremap_nocache(base
, len
);
2651 if (!dev
->base_addr
) {
2652 dev_dbg(dev
->dev
, "can't map memory\n");
2657 ret
= net2272_probe_fin(dev
, IRQF_TRIGGER_LOW
);
2661 platform_set_drvdata(pdev
, dev
);
2662 dev_info(&pdev
->dev
, "running in 16-bit, %sbyte swap local bus mode\n",
2663 (net2272_read(dev
, LOCCTL
) & (1 << BYTE_SWAP
)) ? "" : "no ");
2668 iounmap(dev
->base_addr
);
2670 release_mem_region(base
, len
);
2676 net2272_plat_remove(struct platform_device
*pdev
)
2678 struct net2272
*dev
= platform_get_drvdata(pdev
);
2680 net2272_remove(dev
);
2682 release_mem_region(pdev
->resource
[0].start
,
2683 resource_size(&pdev
->resource
[0]));
2690 static struct platform_driver net2272_plat_driver
= {
2691 .probe
= net2272_plat_probe
,
2692 .remove
= net2272_plat_remove
,
2694 .name
= driver_name
,
2696 /* FIXME .suspend, .resume */
2698 MODULE_ALIAS("platform:net2272");
2700 static int __init
net2272_init(void)
2704 ret
= net2272_pci_register();
2707 ret
= platform_driver_register(&net2272_plat_driver
);
2713 net2272_pci_unregister();
2716 module_init(net2272_init
);
2718 static void __exit
net2272_cleanup(void)
2720 net2272_pci_unregister();
2721 platform_driver_unregister(&net2272_plat_driver
);
2723 module_exit(net2272_cleanup
);
2725 MODULE_DESCRIPTION(DRIVER_DESC
);
2726 MODULE_AUTHOR("PLX Technology, Inc.");
2727 MODULE_LICENSE("GPL");