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