blob: 8dcbe770e2d4b586266c8fbce15199de47ea1e46 [file] [log] [blame]
Seth Levyceb80362011-06-06 19:42:44 -04001/*
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>
Seth Levyceb80362011-06-06 19:42:44 -040030#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>
Geert Uytterhoevend84d6612011-08-08 11:36:51 +020036#include <linux/prefetch.h>
Seth Levyceb80362011-06-06 19:42:44 -040037#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>
Seth Levyceb80362011-06-06 19:42:44 -040045#include <asm/unaligned.h>
46
47#include "net2272.h"
48
49#define DRIVER_DESC "PLX NET2272 USB Peripheral Controller"
50
51static const char driver_name[] = "net2272";
52static const char driver_vers[] = "2006 October 17/mainline";
53static const char driver_desc[] = DRIVER_DESC;
54
55static const char ep0name[] = "ep0";
56static 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_GADGET_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 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103071static bool use_dma = 0;
Seth Levyceb80362011-06-06 19:42:44 -040072module_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 */
85static ushort dma_ep = 1;
86module_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 */
94static ushort dma_mode = 2;
95module_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 */
109static ushort fifo_mode = 0;
110module_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 */
118static ushort enable_suspend = 0;
119module_param(enable_suspend, ushort, 0644);
120
121static 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
138static 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
148static 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
158static 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
169static 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
181static void net2272_dequeue_all(struct net2272_ep *);
182static int net2272_kick_dma(struct net2272_ep *, struct net2272_request *);
183static int net2272_fifo_status(struct usb_ep *);
184
185static struct usb_ep_ops net2272_ep_ops;
186
187/*---------------------------------------------------------------------------*/
188
189static int
190net2272_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
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700206 max = usb_endpoint_maxp(desc) & 0x1fff;
Seth Levyceb80362011-06-06 19:42:44 -0400207
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
263static 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
309static 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
330static struct usb_request *
331net2272_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
350static void
351net2272_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
365static void
366net2272_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;
Felipe Balbiaf93f2c2011-12-19 12:07:40 +0200387 if (use_dma && ep->dma)
388 usb_gadget_unmap_request(&dev->gadget, &req->req,
389 ep->is_in);
Seth Levyceb80362011-06-06 19:42:44 -0400390
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
404static int
405net2272_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 */
441static int
442net2272_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
502static void
503net2272_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
512static int
513net2272_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
554static int
555net2272_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
629static void
630net2272_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 */
642static int
643net2272_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
706static void
707net2272_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 */
721static int
722net2272_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
Felipe Balbi7b30d192011-07-04 11:16:06 +0300736 dev_vdbg(ep->dev->dev, "kick_dma %s req %p dma %08llx\n",
737 ep->ep.name, req, (unsigned long long) req->req.dma);
Seth Levyceb80362011-06-06 19:42:44 -0400738
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
804static 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
827static int
828net2272_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 */
Felipe Balbiaf93f2c2011-12-19 12:07:40 +0200849 if (use_dma && ep->dma) {
850 status = usb_gadget_map_request(&dev->gadget, _req,
851 ep->is_in);
852 if (status)
853 return status;
Seth Levyceb80362011-06-06 19:42:44 -0400854 }
855
Felipe Balbi7b30d192011-07-04 11:16:06 +0300856 dev_vdbg(dev->dev, "%s queue req %p, len %d buf %p dma %08llx %s\n",
Seth Levyceb80362011-06-06 19:42:44 -0400857 _ep->name, _req, _req->length, _req->buf,
Felipe Balbi7b30d192011-07-04 11:16:06 +0300858 (unsigned long long) _req->dma, _req->zero ? "zero" : "!zero");
Seth Levyceb80362011-06-06 19:42:44 -0400859
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 */
928static void
929net2272_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 */
945static int
946net2272_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
985static int
986net2272_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
1027static int
1028net2272_set_halt(struct usb_ep *_ep, int value)
1029{
1030 return net2272_set_halt_and_wedge(_ep, value, 0);
1031}
1032
1033static int
1034net2272_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
1041static int
1042net2272_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
1062static void
1063net2272_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
1076static 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
1094static int
1095net2272_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
1113static int
1114net2272_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
1134static int
1135net2272_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
1148static int
1149net2272_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
Felipe Balbi96f8db62011-10-10 10:33:47 +03001172static int net2272_start(struct usb_gadget *_gadget,
1173 struct usb_gadget_driver *driver);
1174static int net2272_stop(struct usb_gadget *_gadget,
1175 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001176
Seth Levyceb80362011-06-06 19:42:44 -04001177static const struct usb_gadget_ops net2272_ops = {
Felipe Balbi96f8db62011-10-10 10:33:47 +03001178 .get_frame = net2272_get_frame,
1179 .wakeup = net2272_wakeup,
Seth Levyceb80362011-06-06 19:42:44 -04001180 .set_selfpowered = net2272_set_selfpowered,
Felipe Balbi96f8db62011-10-10 10:33:47 +03001181 .pullup = net2272_pullup,
1182 .udc_start = net2272_start,
1183 .udc_stop = net2272_stop,
Seth Levyceb80362011-06-06 19:42:44 -04001184};
1185
1186/*---------------------------------------------------------------------------*/
1187
1188static ssize_t
1189net2272_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}
1313static DEVICE_ATTR(registers, S_IRUGO, net2272_show_registers, NULL);
1314
1315/*---------------------------------------------------------------------------*/
1316
1317static void
1318net2272_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
Seth Levyceb80362011-06-06 19:42:44 -04001357static void
1358net2272_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
1391static void
1392net2272_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
1421static void
1422net2272_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 */
Felipe Balbi96f8db62011-10-10 10:33:47 +03001452static int net2272_start(struct usb_gadget *_gadget,
1453 struct usb_gadget_driver *driver)
Seth Levyceb80362011-06-06 19:42:44 -04001454{
Felipe Balbi96f8db62011-10-10 10:33:47 +03001455 struct net2272 *dev;
Seth Levyceb80362011-06-06 19:42:44 -04001456 unsigned i;
1457
Felipe Balbi96f8db62011-10-10 10:33:47 +03001458 if (!driver || !driver->unbind || !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01001459 driver->max_speed != USB_SPEED_HIGH)
Seth Levyceb80362011-06-06 19:42:44 -04001460 return -EINVAL;
Felipe Balbi96f8db62011-10-10 10:33:47 +03001461
1462 dev = container_of(_gadget, struct net2272, gadget);
Seth Levyceb80362011-06-06 19:42:44 -04001463
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;
Seth Levyceb80362011-06-06 19:42:44 -04001470
1471 /* ... then enable host detection and ep0; and we're ready
1472 * for set_configuration as well as eventual disconnect.
1473 */
1474 net2272_ep0_start(dev);
1475
1476 dev_dbg(dev->dev, "%s ready\n", driver->driver.name);
1477
1478 return 0;
1479}
Seth Levyceb80362011-06-06 19:42:44 -04001480
1481static void
1482stop_activity(struct net2272 *dev, struct usb_gadget_driver *driver)
1483{
1484 int i;
1485
1486 /* don't disconnect if it's not connected */
1487 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1488 driver = NULL;
1489
1490 /* stop hardware; prevent new request submissions;
1491 * and kill any outstanding requests.
1492 */
1493 net2272_usb_reset(dev);
1494 for (i = 0; i < 4; ++i)
1495 net2272_dequeue_all(&dev->ep[i]);
1496
Seth Levyceb80362011-06-06 19:42:44 -04001497 net2272_usb_reinit(dev);
1498}
1499
Felipe Balbi96f8db62011-10-10 10:33:47 +03001500static int net2272_stop(struct usb_gadget *_gadget,
1501 struct usb_gadget_driver *driver)
Seth Levyceb80362011-06-06 19:42:44 -04001502{
Felipe Balbi96f8db62011-10-10 10:33:47 +03001503 struct net2272 *dev;
Seth Levyceb80362011-06-06 19:42:44 -04001504 unsigned long flags;
1505
Felipe Balbi96f8db62011-10-10 10:33:47 +03001506 dev = container_of(_gadget, struct net2272, gadget);
Seth Levyceb80362011-06-06 19:42:44 -04001507
1508 spin_lock_irqsave(&dev->lock, flags);
1509 stop_activity(dev, driver);
1510 spin_unlock_irqrestore(&dev->lock, flags);
1511
Seth Levyceb80362011-06-06 19:42:44 -04001512 dev->driver = NULL;
1513
1514 dev_dbg(dev->dev, "unregistered driver '%s'\n", driver->driver.name);
1515 return 0;
1516}
Seth Levyceb80362011-06-06 19:42:44 -04001517
1518/*---------------------------------------------------------------------------*/
1519/* handle ep-a/ep-b dma completions */
1520static void
1521net2272_handle_dma(struct net2272_ep *ep)
1522{
1523 struct net2272_request *req;
1524 unsigned len;
1525 int status;
1526
1527 if (!list_empty(&ep->queue))
1528 req = list_entry(ep->queue.next,
1529 struct net2272_request, queue);
1530 else
1531 req = NULL;
1532
1533 dev_vdbg(ep->dev->dev, "handle_dma %s req %p\n", ep->ep.name, req);
1534
1535 /* Ensure DREQ is de-asserted */
1536 net2272_write(ep->dev, DMAREQ,
1537 (0 << DMA_BUFFER_VALID)
1538 | (0 << DMA_REQUEST_ENABLE)
1539 | (1 << DMA_CONTROL_DACK)
1540 | (ep->dev->dma_eot_polarity << EOT_POLARITY)
1541 | (ep->dev->dma_dack_polarity << DACK_POLARITY)
1542 | (ep->dev->dma_dreq_polarity << DREQ_POLARITY)
1543 | ((ep->dma >> 1) << DMA_ENDPOINT_SELECT));
1544
1545 ep->dev->dma_busy = 0;
1546
1547 net2272_ep_write(ep, EP_IRQENB,
1548 (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
1549 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
1550 | net2272_ep_read(ep, EP_IRQENB));
1551
1552 /* device-to-host transfer completed */
1553 if (ep->is_in) {
1554 /* validate a short packet or zlp if necessary */
1555 if ((req->req.length % ep->ep.maxpacket != 0) ||
1556 req->req.zero)
1557 set_fifo_bytecount(ep, 0);
1558
1559 net2272_done(ep, req, 0);
1560 if (!list_empty(&ep->queue)) {
1561 req = list_entry(ep->queue.next,
1562 struct net2272_request, queue);
1563 status = net2272_kick_dma(ep, req);
1564 if (status < 0)
1565 net2272_pio_advance(ep);
1566 }
1567
1568 /* host-to-device transfer completed */
1569 } else {
1570 /* terminated with a short packet? */
1571 if (net2272_read(ep->dev, IRQSTAT0) &
1572 (1 << DMA_DONE_INTERRUPT)) {
1573 /* abort system dma */
1574 net2272_cancel_dma(ep->dev);
1575 }
1576
1577 /* EP_TRANSFER will contain the number of bytes
1578 * actually received.
1579 * NOTE: There is no overflow detection on EP_TRANSFER:
1580 * We can't deal with transfers larger than 2^24 bytes!
1581 */
1582 len = (net2272_ep_read(ep, EP_TRANSFER2) << 16)
1583 | (net2272_ep_read(ep, EP_TRANSFER1) << 8)
1584 | (net2272_ep_read(ep, EP_TRANSFER0));
1585
1586 if (ep->not_empty)
1587 len += 4;
1588
1589 req->req.actual += len;
1590
1591 /* get any remaining data */
1592 net2272_pio_advance(ep);
1593 }
1594}
1595
1596/*---------------------------------------------------------------------------*/
1597
1598static void
1599net2272_handle_ep(struct net2272_ep *ep)
1600{
1601 struct net2272_request *req;
1602 u8 stat0, stat1;
1603
1604 if (!list_empty(&ep->queue))
1605 req = list_entry(ep->queue.next,
1606 struct net2272_request, queue);
1607 else
1608 req = NULL;
1609
1610 /* ack all, and handle what we care about */
1611 stat0 = net2272_ep_read(ep, EP_STAT0);
1612 stat1 = net2272_ep_read(ep, EP_STAT1);
1613 ep->irqs++;
1614
1615 dev_vdbg(ep->dev->dev, "%s ack ep_stat0 %02x, ep_stat1 %02x, req %p\n",
1616 ep->ep.name, stat0, stat1, req ? &req->req : 0);
1617
1618 net2272_ep_write(ep, EP_STAT0, stat0 &
1619 ~((1 << NAK_OUT_PACKETS)
1620 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)));
1621 net2272_ep_write(ep, EP_STAT1, stat1);
1622
1623 /* data packet(s) received (in the fifo, OUT)
1624 * direction must be validated, otherwise control read status phase
1625 * could be interpreted as a valid packet
1626 */
1627 if (!ep->is_in && (stat0 & (1 << DATA_PACKET_RECEIVED_INTERRUPT)))
1628 net2272_pio_advance(ep);
1629 /* data packet(s) transmitted (IN) */
1630 else if (stat0 & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
1631 net2272_pio_advance(ep);
1632}
1633
1634static struct net2272_ep *
1635net2272_get_ep_by_addr(struct net2272 *dev, u16 wIndex)
1636{
1637 struct net2272_ep *ep;
1638
1639 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
1640 return &dev->ep[0];
1641
1642 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
1643 u8 bEndpointAddress;
1644
1645 if (!ep->desc)
1646 continue;
1647 bEndpointAddress = ep->desc->bEndpointAddress;
1648 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
1649 continue;
1650 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
1651 return ep;
1652 }
1653 return NULL;
1654}
1655
1656/*
1657 * USB Test Packet:
1658 * JKJKJKJK * 9
1659 * JJKKJJKK * 8
1660 * JJJJKKKK * 8
1661 * JJJJJJJKKKKKKK * 8
1662 * JJJJJJJK * 8
1663 * {JKKKKKKK * 10}, JK
1664 */
1665static const u8 net2272_test_packet[] = {
1666 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1667 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1668 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1669 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1670 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1671 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFD, 0x7E
1672};
1673
1674static void
1675net2272_set_test_mode(struct net2272 *dev, int mode)
1676{
1677 int i;
1678
1679 /* Disable all net2272 interrupts:
1680 * Nothing but a power cycle should stop the test.
1681 */
1682 net2272_write(dev, IRQENB0, 0x00);
1683 net2272_write(dev, IRQENB1, 0x00);
1684
1685 /* Force tranceiver to high-speed */
1686 net2272_write(dev, XCVRDIAG, 1 << FORCE_HIGH_SPEED);
1687
1688 net2272_write(dev, PAGESEL, 0);
1689 net2272_write(dev, EP_STAT0, 1 << DATA_PACKET_TRANSMITTED_INTERRUPT);
1690 net2272_write(dev, EP_RSPCLR,
1691 (1 << CONTROL_STATUS_PHASE_HANDSHAKE)
1692 | (1 << HIDE_STATUS_PHASE));
1693 net2272_write(dev, EP_CFG, 1 << ENDPOINT_DIRECTION);
1694 net2272_write(dev, EP_STAT1, 1 << BUFFER_FLUSH);
1695
1696 /* wait for status phase to complete */
1697 while (!(net2272_read(dev, EP_STAT0) &
1698 (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)))
1699 ;
1700
1701 /* Enable test mode */
1702 net2272_write(dev, USBTEST, mode);
1703
1704 /* load test packet */
1705 if (mode == TEST_PACKET) {
1706 /* switch to 8 bit mode */
1707 net2272_write(dev, LOCCTL, net2272_read(dev, LOCCTL) &
1708 ~(1 << DATA_WIDTH));
1709
1710 for (i = 0; i < sizeof(net2272_test_packet); ++i)
1711 net2272_write(dev, EP_DATA, net2272_test_packet[i]);
1712
1713 /* Validate test packet */
1714 net2272_write(dev, EP_TRANSFER0, 0);
1715 }
1716}
1717
1718static void
1719net2272_handle_stat0_irqs(struct net2272 *dev, u8 stat)
1720{
1721 struct net2272_ep *ep;
1722 u8 num, scratch;
1723
1724 /* starting a control request? */
1725 if (unlikely(stat & (1 << SETUP_PACKET_INTERRUPT))) {
1726 union {
1727 u8 raw[8];
1728 struct usb_ctrlrequest r;
1729 } u;
1730 int tmp = 0;
1731 struct net2272_request *req;
1732
1733 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
1734 if (net2272_read(dev, USBCTL1) & (1 << USB_HIGH_SPEED))
1735 dev->gadget.speed = USB_SPEED_HIGH;
1736 else
1737 dev->gadget.speed = USB_SPEED_FULL;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02001738 dev_dbg(dev->dev, "%s\n",
1739 usb_speed_string(dev->gadget.speed));
Seth Levyceb80362011-06-06 19:42:44 -04001740 }
1741
1742 ep = &dev->ep[0];
1743 ep->irqs++;
1744
1745 /* make sure any leftover interrupt state is cleared */
1746 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
1747 while (!list_empty(&ep->queue)) {
1748 req = list_entry(ep->queue.next,
1749 struct net2272_request, queue);
1750 net2272_done(ep, req,
1751 (req->req.actual == req->req.length) ? 0 : -EPROTO);
1752 }
1753 ep->stopped = 0;
1754 dev->protocol_stall = 0;
1755 net2272_ep_write(ep, EP_STAT0,
1756 (1 << DATA_IN_TOKEN_INTERRUPT)
1757 | (1 << DATA_OUT_TOKEN_INTERRUPT)
1758 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
1759 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
1760 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT));
1761 net2272_ep_write(ep, EP_STAT1,
1762 (1 << TIMEOUT)
1763 | (1 << USB_OUT_ACK_SENT)
1764 | (1 << USB_OUT_NAK_SENT)
1765 | (1 << USB_IN_ACK_RCVD)
1766 | (1 << USB_IN_NAK_SENT)
1767 | (1 << USB_STALL_SENT)
1768 | (1 << LOCAL_OUT_ZLP));
1769
1770 /*
1771 * Ensure Control Read pre-validation setting is beyond maximum size
1772 * - Control Writes can leave non-zero values in EP_TRANSFER. If
1773 * an EP0 transfer following the Control Write is a Control Read,
1774 * the NET2272 sees the non-zero EP_TRANSFER as an unexpected
1775 * pre-validation count.
1776 * - Setting EP_TRANSFER beyond the maximum EP0 transfer size ensures
1777 * the pre-validation count cannot cause an unexpected validatation
1778 */
1779 net2272_write(dev, PAGESEL, 0);
1780 net2272_write(dev, EP_TRANSFER2, 0xff);
1781 net2272_write(dev, EP_TRANSFER1, 0xff);
1782 net2272_write(dev, EP_TRANSFER0, 0xff);
1783
1784 u.raw[0] = net2272_read(dev, SETUP0);
1785 u.raw[1] = net2272_read(dev, SETUP1);
1786 u.raw[2] = net2272_read(dev, SETUP2);
1787 u.raw[3] = net2272_read(dev, SETUP3);
1788 u.raw[4] = net2272_read(dev, SETUP4);
1789 u.raw[5] = net2272_read(dev, SETUP5);
1790 u.raw[6] = net2272_read(dev, SETUP6);
1791 u.raw[7] = net2272_read(dev, SETUP7);
1792 /*
1793 * If you have a big endian cpu make sure le16_to_cpus
1794 * performs the proper byte swapping here...
1795 */
1796 le16_to_cpus(&u.r.wValue);
1797 le16_to_cpus(&u.r.wIndex);
1798 le16_to_cpus(&u.r.wLength);
1799
1800 /* ack the irq */
1801 net2272_write(dev, IRQSTAT0, 1 << SETUP_PACKET_INTERRUPT);
1802 stat ^= (1 << SETUP_PACKET_INTERRUPT);
1803
1804 /* watch control traffic at the token level, and force
1805 * synchronization before letting the status phase happen.
1806 */
1807 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1808 if (ep->is_in) {
1809 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
1810 | (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE)
1811 | (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE);
1812 stop_out_naking(ep);
1813 } else
1814 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
1815 | (1 << DATA_OUT_TOKEN_INTERRUPT_ENABLE)
1816 | (1 << DATA_IN_TOKEN_INTERRUPT_ENABLE);
1817 net2272_ep_write(ep, EP_IRQENB, scratch);
1818
1819 if ((u.r.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1820 goto delegate;
1821 switch (u.r.bRequest) {
1822 case USB_REQ_GET_STATUS: {
1823 struct net2272_ep *e;
1824 u16 status = 0;
1825
1826 switch (u.r.bRequestType & USB_RECIP_MASK) {
1827 case USB_RECIP_ENDPOINT:
1828 e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1829 if (!e || u.r.wLength > 2)
1830 goto do_stall;
1831 if (net2272_ep_read(e, EP_RSPSET) & (1 << ENDPOINT_HALT))
1832 status = __constant_cpu_to_le16(1);
1833 else
1834 status = __constant_cpu_to_le16(0);
1835
1836 /* don't bother with a request object! */
1837 net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1838 writew(status, net2272_reg_addr(dev, EP_DATA));
1839 set_fifo_bytecount(&dev->ep[0], 0);
1840 allow_status(ep);
1841 dev_vdbg(dev->dev, "%s stat %02x\n",
1842 ep->ep.name, status);
1843 goto next_endpoints;
1844 case USB_RECIP_DEVICE:
1845 if (u.r.wLength > 2)
1846 goto do_stall;
1847 if (dev->is_selfpowered)
1848 status = (1 << USB_DEVICE_SELF_POWERED);
1849
1850 /* don't bother with a request object! */
1851 net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1852 writew(status, net2272_reg_addr(dev, EP_DATA));
1853 set_fifo_bytecount(&dev->ep[0], 0);
1854 allow_status(ep);
1855 dev_vdbg(dev->dev, "device stat %02x\n", status);
1856 goto next_endpoints;
1857 case USB_RECIP_INTERFACE:
1858 if (u.r.wLength > 2)
1859 goto do_stall;
1860
1861 /* don't bother with a request object! */
1862 net2272_ep_write(&dev->ep[0], EP_IRQENB, 0);
1863 writew(status, net2272_reg_addr(dev, EP_DATA));
1864 set_fifo_bytecount(&dev->ep[0], 0);
1865 allow_status(ep);
1866 dev_vdbg(dev->dev, "interface status %02x\n", status);
1867 goto next_endpoints;
1868 }
1869
1870 break;
1871 }
1872 case USB_REQ_CLEAR_FEATURE: {
1873 struct net2272_ep *e;
1874
1875 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1876 goto delegate;
1877 if (u.r.wValue != USB_ENDPOINT_HALT ||
1878 u.r.wLength != 0)
1879 goto do_stall;
1880 e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1881 if (!e)
1882 goto do_stall;
1883 if (e->wedged) {
1884 dev_vdbg(dev->dev, "%s wedged, halt not cleared\n",
1885 ep->ep.name);
1886 } else {
1887 dev_vdbg(dev->dev, "%s clear halt\n", ep->ep.name);
1888 clear_halt(e);
1889 }
1890 allow_status(ep);
1891 goto next_endpoints;
1892 }
1893 case USB_REQ_SET_FEATURE: {
1894 struct net2272_ep *e;
1895
1896 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1897 if (u.r.wIndex != NORMAL_OPERATION)
1898 net2272_set_test_mode(dev, (u.r.wIndex >> 8));
1899 allow_status(ep);
1900 dev_vdbg(dev->dev, "test mode: %d\n", u.r.wIndex);
1901 goto next_endpoints;
1902 } else if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1903 goto delegate;
1904 if (u.r.wValue != USB_ENDPOINT_HALT ||
1905 u.r.wLength != 0)
1906 goto do_stall;
1907 e = net2272_get_ep_by_addr(dev, u.r.wIndex);
1908 if (!e)
1909 goto do_stall;
1910 set_halt(e);
1911 allow_status(ep);
1912 dev_vdbg(dev->dev, "%s set halt\n", ep->ep.name);
1913 goto next_endpoints;
1914 }
1915 case USB_REQ_SET_ADDRESS: {
1916 net2272_write(dev, OURADDR, u.r.wValue & 0xff);
1917 allow_status(ep);
1918 break;
1919 }
1920 default:
1921 delegate:
1922 dev_vdbg(dev->dev, "setup %02x.%02x v%04x i%04x "
1923 "ep_cfg %08x\n",
1924 u.r.bRequestType, u.r.bRequest,
1925 u.r.wValue, u.r.wIndex,
1926 net2272_ep_read(ep, EP_CFG));
1927 spin_unlock(&dev->lock);
1928 tmp = dev->driver->setup(&dev->gadget, &u.r);
1929 spin_lock(&dev->lock);
1930 }
1931
1932 /* stall ep0 on error */
1933 if (tmp < 0) {
1934 do_stall:
1935 dev_vdbg(dev->dev, "req %02x.%02x protocol STALL; stat %d\n",
1936 u.r.bRequestType, u.r.bRequest, tmp);
1937 dev->protocol_stall = 1;
1938 }
1939 /* endpoint dma irq? */
1940 } else if (stat & (1 << DMA_DONE_INTERRUPT)) {
1941 net2272_cancel_dma(dev);
1942 net2272_write(dev, IRQSTAT0, 1 << DMA_DONE_INTERRUPT);
1943 stat &= ~(1 << DMA_DONE_INTERRUPT);
1944 num = (net2272_read(dev, DMAREQ) & (1 << DMA_ENDPOINT_SELECT))
1945 ? 2 : 1;
1946
1947 ep = &dev->ep[num];
1948 net2272_handle_dma(ep);
1949 }
1950
1951 next_endpoints:
1952 /* endpoint data irq? */
1953 scratch = stat & 0x0f;
1954 stat &= ~0x0f;
1955 for (num = 0; scratch; num++) {
1956 u8 t;
1957
1958 /* does this endpoint's FIFO and queue need tending? */
1959 t = 1 << num;
1960 if ((scratch & t) == 0)
1961 continue;
1962 scratch ^= t;
1963
1964 ep = &dev->ep[num];
1965 net2272_handle_ep(ep);
1966 }
1967
1968 /* some interrupts we can just ignore */
1969 stat &= ~(1 << SOF_INTERRUPT);
1970
1971 if (stat)
1972 dev_dbg(dev->dev, "unhandled irqstat0 %02x\n", stat);
1973}
1974
1975static void
1976net2272_handle_stat1_irqs(struct net2272 *dev, u8 stat)
1977{
1978 u8 tmp, mask;
1979
1980 /* after disconnect there's nothing else to do! */
1981 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
1982 mask = (1 << USB_HIGH_SPEED) | (1 << USB_FULL_SPEED);
1983
1984 if (stat & tmp) {
1985 net2272_write(dev, IRQSTAT1, tmp);
1986 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
1987 ((net2272_read(dev, USBCTL1) & mask) == 0))
1988 || ((net2272_read(dev, USBCTL1) & (1 << VBUS_PIN))
1989 == 0))
1990 && (dev->gadget.speed != USB_SPEED_UNKNOWN)) {
1991 dev_dbg(dev->dev, "disconnect %s\n",
1992 dev->driver->driver.name);
1993 stop_activity(dev, dev->driver);
1994 net2272_ep0_start(dev);
1995 return;
1996 }
1997 stat &= ~tmp;
1998
1999 if (!stat)
2000 return;
2001 }
2002
2003 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2004 if (stat & tmp) {
2005 net2272_write(dev, IRQSTAT1, tmp);
2006 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2007 if (dev->driver->suspend)
2008 dev->driver->suspend(&dev->gadget);
2009 if (!enable_suspend) {
2010 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2011 dev_dbg(dev->dev, "Suspend disabled, ignoring\n");
2012 }
2013 } else {
2014 if (dev->driver->resume)
2015 dev->driver->resume(&dev->gadget);
2016 }
2017 stat &= ~tmp;
2018 }
2019
2020 /* clear any other status/irqs */
2021 if (stat)
2022 net2272_write(dev, IRQSTAT1, stat);
2023
2024 /* some status we can just ignore */
2025 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2026 | (1 << SUSPEND_REQUEST_INTERRUPT)
2027 | (1 << RESUME_INTERRUPT));
2028 if (!stat)
2029 return;
2030 else
2031 dev_dbg(dev->dev, "unhandled irqstat1 %02x\n", stat);
2032}
2033
2034static irqreturn_t net2272_irq(int irq, void *_dev)
2035{
2036 struct net2272 *dev = _dev;
2037#if defined(PLX_PCI_RDK) || defined(PLX_PCI_RDK2)
2038 u32 intcsr;
2039#endif
2040#if defined(PLX_PCI_RDK)
2041 u8 dmareq;
2042#endif
2043 spin_lock(&dev->lock);
2044#if defined(PLX_PCI_RDK)
2045 intcsr = readl(dev->rdk1.plx9054_base_addr + INTCSR);
2046
2047 if ((intcsr & LOCAL_INTERRUPT_TEST) == LOCAL_INTERRUPT_TEST) {
2048 writel(intcsr & ~(1 << PCI_INTERRUPT_ENABLE),
2049 dev->rdk1.plx9054_base_addr + INTCSR);
2050 net2272_handle_stat1_irqs(dev, net2272_read(dev, IRQSTAT1));
2051 net2272_handle_stat0_irqs(dev, net2272_read(dev, IRQSTAT0));
2052 intcsr = readl(dev->rdk1.plx9054_base_addr + INTCSR);
2053 writel(intcsr | (1 << PCI_INTERRUPT_ENABLE),
2054 dev->rdk1.plx9054_base_addr + INTCSR);
2055 }
2056 if ((intcsr & DMA_CHANNEL_0_TEST) == DMA_CHANNEL_0_TEST) {
2057 writeb((1 << CHANNEL_CLEAR_INTERRUPT | (0 << CHANNEL_ENABLE)),
2058 dev->rdk1.plx9054_base_addr + DMACSR0);
2059
2060 dmareq = net2272_read(dev, DMAREQ);
2061 if (dmareq & 0x01)
2062 net2272_handle_dma(&dev->ep[2]);
2063 else
2064 net2272_handle_dma(&dev->ep[1]);
2065 }
2066#endif
2067#if defined(PLX_PCI_RDK2)
2068 /* see if PCI int for us by checking irqstat */
2069 intcsr = readl(dev->rdk2.fpga_base_addr + RDK2_IRQSTAT);
Wei Yongjun000b7f52012-10-22 13:51:11 +08002070 if (!intcsr & (1 << NET2272_PCI_IRQ)) {
2071 spin_unlock(&dev->lock);
Seth Levyceb80362011-06-06 19:42:44 -04002072 return IRQ_NONE;
Wei Yongjun000b7f52012-10-22 13:51:11 +08002073 }
Seth Levyceb80362011-06-06 19:42:44 -04002074 /* check dma interrupts */
2075#endif
2076 /* Platform/devcice interrupt handler */
2077#if !defined(PLX_PCI_RDK)
2078 net2272_handle_stat1_irqs(dev, net2272_read(dev, IRQSTAT1));
2079 net2272_handle_stat0_irqs(dev, net2272_read(dev, IRQSTAT0));
2080#endif
2081 spin_unlock(&dev->lock);
2082
2083 return IRQ_HANDLED;
2084}
2085
2086static int net2272_present(struct net2272 *dev)
2087{
2088 /*
2089 * Quick test to see if CPU can communicate properly with the NET2272.
2090 * Verifies connection using writes and reads to write/read and
2091 * read-only registers.
2092 *
2093 * This routine is strongly recommended especially during early bring-up
2094 * of new hardware, however for designs that do not apply Power On System
2095 * Tests (POST) it may discarded (or perhaps minimized).
2096 */
2097 unsigned int ii;
2098 u8 val, refval;
2099
2100 /* Verify NET2272 write/read SCRATCH register can write and read */
2101 refval = net2272_read(dev, SCRATCH);
2102 for (ii = 0; ii < 0x100; ii += 7) {
2103 net2272_write(dev, SCRATCH, ii);
2104 val = net2272_read(dev, SCRATCH);
2105 if (val != ii) {
2106 dev_dbg(dev->dev,
2107 "%s: write/read SCRATCH register test failed: "
2108 "wrote:0x%2.2x, read:0x%2.2x\n",
2109 __func__, ii, val);
2110 return -EINVAL;
2111 }
2112 }
2113 /* To be nice, we write the original SCRATCH value back: */
2114 net2272_write(dev, SCRATCH, refval);
2115
2116 /* Verify NET2272 CHIPREV register is read-only: */
2117 refval = net2272_read(dev, CHIPREV_2272);
2118 for (ii = 0; ii < 0x100; ii += 7) {
2119 net2272_write(dev, CHIPREV_2272, ii);
2120 val = net2272_read(dev, CHIPREV_2272);
2121 if (val != refval) {
2122 dev_dbg(dev->dev,
2123 "%s: write/read CHIPREV register test failed: "
2124 "wrote 0x%2.2x, read:0x%2.2x expected:0x%2.2x\n",
2125 __func__, ii, val, refval);
2126 return -EINVAL;
2127 }
2128 }
2129
2130 /*
2131 * Verify NET2272's "NET2270 legacy revision" register
2132 * - NET2272 has two revision registers. The NET2270 legacy revision
2133 * register should read the same value, regardless of the NET2272
2134 * silicon revision. The legacy register applies to NET2270
2135 * firmware being applied to the NET2272.
2136 */
2137 val = net2272_read(dev, CHIPREV_LEGACY);
2138 if (val != NET2270_LEGACY_REV) {
2139 /*
2140 * Unexpected legacy revision value
2141 * - Perhaps the chip is a NET2270?
2142 */
2143 dev_dbg(dev->dev,
2144 "%s: WARNING: UNEXPECTED NET2272 LEGACY REGISTER VALUE:\n"
2145 " - CHIPREV_LEGACY: expected 0x%2.2x, got:0x%2.2x. (Not NET2272?)\n",
2146 __func__, NET2270_LEGACY_REV, val);
2147 return -EINVAL;
2148 }
2149
2150 /*
2151 * Verify NET2272 silicon revision
2152 * - This revision register is appropriate for the silicon version
2153 * of the NET2272
2154 */
2155 val = net2272_read(dev, CHIPREV_2272);
2156 switch (val) {
2157 case CHIPREV_NET2272_R1:
2158 /*
2159 * NET2272 Rev 1 has DMA related errata:
2160 * - Newer silicon (Rev 1A or better) required
2161 */
2162 dev_dbg(dev->dev,
2163 "%s: Rev 1 detected: newer silicon recommended for DMA support\n",
2164 __func__);
2165 break;
2166 case CHIPREV_NET2272_R1A:
2167 break;
2168 default:
2169 /* NET2272 silicon version *may* not work with this firmware */
2170 dev_dbg(dev->dev,
2171 "%s: unexpected silicon revision register value: "
2172 " CHIPREV_2272: 0x%2.2x\n",
2173 __func__, val);
2174 /*
2175 * Return Success, even though the chip rev is not an expected value
2176 * - Older, pre-built firmware can attempt to operate on newer silicon
2177 * - Often, new silicon is perfectly compatible
2178 */
2179 }
2180
2181 /* Success: NET2272 checks out OK */
2182 return 0;
2183}
2184
2185static void
2186net2272_gadget_release(struct device *_dev)
2187{
2188 struct net2272 *dev = dev_get_drvdata(_dev);
2189 kfree(dev);
2190}
2191
2192/*---------------------------------------------------------------------------*/
2193
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002194static void
Seth Levyceb80362011-06-06 19:42:44 -04002195net2272_remove(struct net2272 *dev)
2196{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002197 usb_del_gadget_udc(&dev->gadget);
2198
Seth Levyceb80362011-06-06 19:42:44 -04002199 /* start with the driver above us */
2200 if (dev->driver) {
2201 /* should have been done already by driver model core */
2202 dev_warn(dev->dev, "pci remove, driver '%s' is still registered\n",
2203 dev->driver->driver.name);
2204 usb_gadget_unregister_driver(dev->driver);
2205 }
2206
2207 free_irq(dev->irq, dev);
2208 iounmap(dev->base_addr);
2209
Seth Levyceb80362011-06-06 19:42:44 -04002210 device_remove_file(dev->dev, &dev_attr_registers);
2211
2212 dev_info(dev->dev, "unbind\n");
Seth Levyceb80362011-06-06 19:42:44 -04002213}
2214
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002215static struct net2272 *net2272_probe_init(struct device *dev, unsigned int irq)
Seth Levyceb80362011-06-06 19:42:44 -04002216{
2217 struct net2272 *ret;
2218
Seth Levyceb80362011-06-06 19:42:44 -04002219 if (!irq) {
2220 dev_dbg(dev, "No IRQ!\n");
2221 return ERR_PTR(-ENODEV);
2222 }
2223
2224 /* alloc, and start init */
2225 ret = kzalloc(sizeof(*ret), GFP_KERNEL);
2226 if (!ret)
2227 return ERR_PTR(-ENOMEM);
2228
2229 spin_lock_init(&ret->lock);
2230 ret->irq = irq;
2231 ret->dev = dev;
2232 ret->gadget.ops = &net2272_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002233 ret->gadget.max_speed = USB_SPEED_HIGH;
Seth Levyceb80362011-06-06 19:42:44 -04002234
2235 /* the "gadget" abstracts/virtualizes the controller */
Seth Levyceb80362011-06-06 19:42:44 -04002236 ret->gadget.name = driver_name;
2237
2238 return ret;
2239}
2240
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002241static int
Seth Levyceb80362011-06-06 19:42:44 -04002242net2272_probe_fin(struct net2272 *dev, unsigned int irqflags)
2243{
2244 int ret;
2245
2246 /* See if there... */
2247 if (net2272_present(dev)) {
2248 dev_warn(dev->dev, "2272 not found!\n");
2249 ret = -ENODEV;
2250 goto err;
2251 }
2252
2253 net2272_usb_reset(dev);
2254 net2272_usb_reinit(dev);
2255
2256 ret = request_irq(dev->irq, net2272_irq, irqflags, driver_name, dev);
2257 if (ret) {
2258 dev_err(dev->dev, "request interrupt %i failed\n", dev->irq);
2259 goto err;
2260 }
2261
2262 dev->chiprev = net2272_read(dev, CHIPREV_2272);
2263
2264 /* done */
2265 dev_info(dev->dev, "%s\n", driver_desc);
2266 dev_info(dev->dev, "irq %i, mem %p, chip rev %04x, dma %s\n",
2267 dev->irq, dev->base_addr, dev->chiprev,
2268 dma_mode_string());
2269 dev_info(dev->dev, "version: %s\n", driver_vers);
2270
Seth Levyceb80362011-06-06 19:42:44 -04002271 ret = device_create_file(dev->dev, &dev_attr_registers);
2272 if (ret)
Felipe Balbic9f9c842013-01-24 16:48:12 +02002273 goto err_irq;
Seth Levyceb80362011-06-06 19:42:44 -04002274
Felipe Balbi8efeeef2013-02-26 15:15:27 +02002275 ret = usb_add_gadget_udc_release(dev->dev, &dev->gadget,
2276 net2272_gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002277 if (ret)
2278 goto err_add_udc;
2279
Seth Levyceb80362011-06-06 19:42:44 -04002280 return 0;
2281
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002282err_add_udc:
2283 device_remove_file(dev->dev, &dev_attr_registers);
Seth Levyceb80362011-06-06 19:42:44 -04002284 err_irq:
2285 free_irq(dev->irq, dev);
2286 err:
2287 return ret;
2288}
2289
2290#ifdef CONFIG_PCI
2291
2292/*
2293 * wrap this driver around the specified device, but
2294 * don't respond over USB until a gadget driver binds to us
2295 */
2296
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002297static int
Seth Levyceb80362011-06-06 19:42:44 -04002298net2272_rdk1_probe(struct pci_dev *pdev, struct net2272 *dev)
2299{
2300 unsigned long resource, len, tmp;
2301 void __iomem *mem_mapped_addr[4];
2302 int ret, i;
2303
2304 /*
2305 * BAR 0 holds PLX 9054 config registers
2306 * BAR 1 is i/o memory; unused here
2307 * BAR 2 holds EPLD config registers
2308 * BAR 3 holds NET2272 registers
2309 */
2310
2311 /* Find and map all address spaces */
2312 for (i = 0; i < 4; ++i) {
2313 if (i == 1)
2314 continue; /* BAR1 unused */
2315
2316 resource = pci_resource_start(pdev, i);
2317 len = pci_resource_len(pdev, i);
2318
2319 if (!request_mem_region(resource, len, driver_name)) {
2320 dev_dbg(dev->dev, "controller already in use\n");
2321 ret = -EBUSY;
2322 goto err;
2323 }
2324
2325 mem_mapped_addr[i] = ioremap_nocache(resource, len);
2326 if (mem_mapped_addr[i] == NULL) {
2327 release_mem_region(resource, len);
2328 dev_dbg(dev->dev, "can't map memory\n");
2329 ret = -EFAULT;
2330 goto err;
2331 }
2332 }
2333
2334 dev->rdk1.plx9054_base_addr = mem_mapped_addr[0];
2335 dev->rdk1.epld_base_addr = mem_mapped_addr[2];
2336 dev->base_addr = mem_mapped_addr[3];
2337
2338 /* Set PLX 9054 bus width (16 bits) */
2339 tmp = readl(dev->rdk1.plx9054_base_addr + LBRD1);
2340 writel((tmp & ~(3 << MEMORY_SPACE_LOCAL_BUS_WIDTH)) | W16_BIT,
2341 dev->rdk1.plx9054_base_addr + LBRD1);
2342
2343 /* Enable PLX 9054 Interrupts */
2344 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) |
2345 (1 << PCI_INTERRUPT_ENABLE) |
2346 (1 << LOCAL_INTERRUPT_INPUT_ENABLE),
2347 dev->rdk1.plx9054_base_addr + INTCSR);
2348
2349 writeb((1 << CHANNEL_CLEAR_INTERRUPT | (0 << CHANNEL_ENABLE)),
2350 dev->rdk1.plx9054_base_addr + DMACSR0);
2351
2352 /* reset */
2353 writeb((1 << EPLD_DMA_ENABLE) |
2354 (1 << DMA_CTL_DACK) |
2355 (1 << DMA_TIMEOUT_ENABLE) |
2356 (1 << USER) |
2357 (0 << MPX_MODE) |
2358 (1 << BUSWIDTH) |
2359 (1 << NET2272_RESET),
2360 dev->base_addr + EPLD_IO_CONTROL_REGISTER);
2361
2362 mb();
2363 writeb(readb(dev->base_addr + EPLD_IO_CONTROL_REGISTER) &
2364 ~(1 << NET2272_RESET),
2365 dev->base_addr + EPLD_IO_CONTROL_REGISTER);
2366 udelay(200);
2367
2368 return 0;
2369
2370 err:
2371 while (--i >= 0) {
2372 iounmap(mem_mapped_addr[i]);
2373 release_mem_region(pci_resource_start(pdev, i),
2374 pci_resource_len(pdev, i));
2375 }
2376
2377 return ret;
2378}
2379
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002380static int
Seth Levyceb80362011-06-06 19:42:44 -04002381net2272_rdk2_probe(struct pci_dev *pdev, struct net2272 *dev)
2382{
2383 unsigned long resource, len;
2384 void __iomem *mem_mapped_addr[2];
2385 int ret, i;
2386
2387 /*
2388 * BAR 0 holds FGPA config registers
2389 * BAR 1 holds NET2272 registers
2390 */
2391
2392 /* Find and map all address spaces, bar2-3 unused in rdk 2 */
2393 for (i = 0; i < 2; ++i) {
2394 resource = pci_resource_start(pdev, i);
2395 len = pci_resource_len(pdev, i);
2396
2397 if (!request_mem_region(resource, len, driver_name)) {
2398 dev_dbg(dev->dev, "controller already in use\n");
2399 ret = -EBUSY;
2400 goto err;
2401 }
2402
2403 mem_mapped_addr[i] = ioremap_nocache(resource, len);
2404 if (mem_mapped_addr[i] == NULL) {
2405 release_mem_region(resource, len);
2406 dev_dbg(dev->dev, "can't map memory\n");
2407 ret = -EFAULT;
2408 goto err;
2409 }
2410 }
2411
2412 dev->rdk2.fpga_base_addr = mem_mapped_addr[0];
2413 dev->base_addr = mem_mapped_addr[1];
2414
2415 mb();
2416 /* Set 2272 bus width (16 bits) and reset */
2417 writel((1 << CHIP_RESET), dev->rdk2.fpga_base_addr + RDK2_LOCCTLRDK);
2418 udelay(200);
2419 writel((1 << BUS_WIDTH), dev->rdk2.fpga_base_addr + RDK2_LOCCTLRDK);
2420 /* Print fpga version number */
2421 dev_info(dev->dev, "RDK2 FPGA version %08x\n",
2422 readl(dev->rdk2.fpga_base_addr + RDK2_FPGAREV));
2423 /* Enable FPGA Interrupts */
2424 writel((1 << NET2272_PCI_IRQ), dev->rdk2.fpga_base_addr + RDK2_IRQENB);
2425
2426 return 0;
2427
2428 err:
2429 while (--i >= 0) {
2430 iounmap(mem_mapped_addr[i]);
2431 release_mem_region(pci_resource_start(pdev, i),
2432 pci_resource_len(pdev, i));
2433 }
2434
2435 return ret;
2436}
2437
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002438static int
Seth Levyceb80362011-06-06 19:42:44 -04002439net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2440{
2441 struct net2272 *dev;
2442 int ret;
2443
2444 dev = net2272_probe_init(&pdev->dev, pdev->irq);
2445 if (IS_ERR(dev))
2446 return PTR_ERR(dev);
2447 dev->dev_id = pdev->device;
2448
2449 if (pci_enable_device(pdev) < 0) {
2450 ret = -ENODEV;
2451 goto err_free;
2452 }
2453
2454 pci_set_master(pdev);
2455
2456 switch (pdev->device) {
2457 case PCI_DEVICE_ID_RDK1: ret = net2272_rdk1_probe(pdev, dev); break;
2458 case PCI_DEVICE_ID_RDK2: ret = net2272_rdk2_probe(pdev, dev); break;
2459 default: BUG();
2460 }
2461 if (ret)
2462 goto err_pci;
2463
2464 ret = net2272_probe_fin(dev, 0);
2465 if (ret)
2466 goto err_pci;
2467
2468 pci_set_drvdata(pdev, dev);
2469
2470 return 0;
2471
2472 err_pci:
2473 pci_disable_device(pdev);
2474 err_free:
2475 kfree(dev);
2476
2477 return ret;
2478}
2479
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002480static void
Seth Levyceb80362011-06-06 19:42:44 -04002481net2272_rdk1_remove(struct pci_dev *pdev, struct net2272 *dev)
2482{
2483 int i;
2484
2485 /* disable PLX 9054 interrupts */
2486 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2487 ~(1 << PCI_INTERRUPT_ENABLE),
2488 dev->rdk1.plx9054_base_addr + INTCSR);
2489
2490 /* clean up resources allocated during probe() */
2491 iounmap(dev->rdk1.plx9054_base_addr);
2492 iounmap(dev->rdk1.epld_base_addr);
2493
2494 for (i = 0; i < 4; ++i) {
2495 if (i == 1)
2496 continue; /* BAR1 unused */
2497 release_mem_region(pci_resource_start(pdev, i),
2498 pci_resource_len(pdev, i));
2499 }
2500}
2501
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002502static void
Seth Levyceb80362011-06-06 19:42:44 -04002503net2272_rdk2_remove(struct pci_dev *pdev, struct net2272 *dev)
2504{
2505 int i;
2506
2507 /* disable fpga interrupts
2508 writel(readl(dev->rdk1.plx9054_base_addr + INTCSR) &
2509 ~(1 << PCI_INTERRUPT_ENABLE),
2510 dev->rdk1.plx9054_base_addr + INTCSR);
2511 */
2512
2513 /* clean up resources allocated during probe() */
2514 iounmap(dev->rdk2.fpga_base_addr);
2515
2516 for (i = 0; i < 2; ++i)
2517 release_mem_region(pci_resource_start(pdev, i),
2518 pci_resource_len(pdev, i));
2519}
2520
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002521static void
Seth Levyceb80362011-06-06 19:42:44 -04002522net2272_pci_remove(struct pci_dev *pdev)
2523{
2524 struct net2272 *dev = pci_get_drvdata(pdev);
2525
2526 net2272_remove(dev);
2527
2528 switch (pdev->device) {
2529 case PCI_DEVICE_ID_RDK1: net2272_rdk1_remove(pdev, dev); break;
2530 case PCI_DEVICE_ID_RDK2: net2272_rdk2_remove(pdev, dev); break;
2531 default: BUG();
2532 }
2533
2534 pci_disable_device(pdev);
2535
2536 kfree(dev);
2537}
2538
2539/* Table of matching PCI IDs */
Bill Pembertond3608b62012-11-19 13:24:34 -05002540static struct pci_device_id pci_ids[] = {
Seth Levyceb80362011-06-06 19:42:44 -04002541 { /* RDK 1 card */
2542 .class = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2543 .class_mask = 0,
2544 .vendor = PCI_VENDOR_ID_PLX,
2545 .device = PCI_DEVICE_ID_RDK1,
2546 .subvendor = PCI_ANY_ID,
2547 .subdevice = PCI_ANY_ID,
2548 },
2549 { /* RDK 2 card */
2550 .class = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2551 .class_mask = 0,
2552 .vendor = PCI_VENDOR_ID_PLX,
2553 .device = PCI_DEVICE_ID_RDK2,
2554 .subvendor = PCI_ANY_ID,
2555 .subdevice = PCI_ANY_ID,
2556 },
2557 { }
2558};
2559MODULE_DEVICE_TABLE(pci, pci_ids);
2560
2561static struct pci_driver net2272_pci_driver = {
2562 .name = driver_name,
2563 .id_table = pci_ids,
2564
2565 .probe = net2272_pci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05002566 .remove = net2272_pci_remove,
Seth Levyceb80362011-06-06 19:42:44 -04002567};
2568
Sebastian Andrzej Siewiore4fe0562011-06-29 16:41:54 +03002569static int net2272_pci_register(void)
2570{
2571 return pci_register_driver(&net2272_pci_driver);
2572}
2573
2574static void net2272_pci_unregister(void)
2575{
2576 pci_unregister_driver(&net2272_pci_driver);
2577}
2578
Seth Levyceb80362011-06-06 19:42:44 -04002579#else
Sebastian Andrzej Siewiore4fe0562011-06-29 16:41:54 +03002580static inline int net2272_pci_register(void) { return 0; }
2581static inline void net2272_pci_unregister(void) { }
Seth Levyceb80362011-06-06 19:42:44 -04002582#endif
2583
2584/*---------------------------------------------------------------------------*/
2585
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002586static int
Seth Levyceb80362011-06-06 19:42:44 -04002587net2272_plat_probe(struct platform_device *pdev)
2588{
2589 struct net2272 *dev;
2590 int ret;
2591 unsigned int irqflags;
2592 resource_size_t base, len;
2593 struct resource *iomem, *iomem_bus, *irq_res;
2594
2595 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2596 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2597 iomem_bus = platform_get_resource(pdev, IORESOURCE_BUS, 0);
2598 if (!irq_res || !iomem) {
2599 dev_err(&pdev->dev, "must provide irq/base addr");
2600 return -EINVAL;
2601 }
2602
2603 dev = net2272_probe_init(&pdev->dev, irq_res->start);
2604 if (IS_ERR(dev))
2605 return PTR_ERR(dev);
2606
2607 irqflags = 0;
2608 if (irq_res->flags & IORESOURCE_IRQ_HIGHEDGE)
2609 irqflags |= IRQF_TRIGGER_RISING;
2610 if (irq_res->flags & IORESOURCE_IRQ_LOWEDGE)
2611 irqflags |= IRQF_TRIGGER_FALLING;
2612 if (irq_res->flags & IORESOURCE_IRQ_HIGHLEVEL)
2613 irqflags |= IRQF_TRIGGER_HIGH;
2614 if (irq_res->flags & IORESOURCE_IRQ_LOWLEVEL)
2615 irqflags |= IRQF_TRIGGER_LOW;
2616
2617 base = iomem->start;
2618 len = resource_size(iomem);
2619 if (iomem_bus)
2620 dev->base_shift = iomem_bus->start;
2621
2622 if (!request_mem_region(base, len, driver_name)) {
2623 dev_dbg(dev->dev, "get request memory region!\n");
2624 ret = -EBUSY;
2625 goto err;
2626 }
2627 dev->base_addr = ioremap_nocache(base, len);
2628 if (!dev->base_addr) {
2629 dev_dbg(dev->dev, "can't map memory\n");
2630 ret = -EFAULT;
2631 goto err_req;
2632 }
2633
2634 ret = net2272_probe_fin(dev, IRQF_TRIGGER_LOW);
2635 if (ret)
2636 goto err_io;
2637
2638 platform_set_drvdata(pdev, dev);
2639 dev_info(&pdev->dev, "running in 16-bit, %sbyte swap local bus mode\n",
2640 (net2272_read(dev, LOCCTL) & (1 << BYTE_SWAP)) ? "" : "no ");
2641
Seth Levyceb80362011-06-06 19:42:44 -04002642 return 0;
2643
2644 err_io:
2645 iounmap(dev->base_addr);
2646 err_req:
2647 release_mem_region(base, len);
2648 err:
2649 return ret;
2650}
2651
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002652static int
Seth Levyceb80362011-06-06 19:42:44 -04002653net2272_plat_remove(struct platform_device *pdev)
2654{
2655 struct net2272 *dev = platform_get_drvdata(pdev);
2656
2657 net2272_remove(dev);
2658
2659 release_mem_region(pdev->resource[0].start,
2660 resource_size(&pdev->resource[0]));
2661
2662 kfree(dev);
2663
2664 return 0;
2665}
2666
2667static struct platform_driver net2272_plat_driver = {
2668 .probe = net2272_plat_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05002669 .remove = net2272_plat_remove,
Seth Levyceb80362011-06-06 19:42:44 -04002670 .driver = {
2671 .name = driver_name,
2672 .owner = THIS_MODULE,
2673 },
2674 /* FIXME .suspend, .resume */
2675};
Sebastian Andrzej Siewior86081d72011-06-29 16:41:55 +03002676MODULE_ALIAS("platform:net2272");
Seth Levyceb80362011-06-06 19:42:44 -04002677
2678static int __init net2272_init(void)
2679{
Sebastian Andrzej Siewiore4fe0562011-06-29 16:41:54 +03002680 int ret;
2681
2682 ret = net2272_pci_register();
2683 if (ret)
2684 return ret;
2685 ret = platform_driver_register(&net2272_plat_driver);
2686 if (ret)
2687 goto err_pci;
2688 return ret;
2689
2690err_pci:
2691 net2272_pci_unregister();
2692 return ret;
Seth Levyceb80362011-06-06 19:42:44 -04002693}
2694module_init(net2272_init);
2695
2696static void __exit net2272_cleanup(void)
2697{
Sebastian Andrzej Siewiore4fe0562011-06-29 16:41:54 +03002698 net2272_pci_unregister();
Seth Levyceb80362011-06-06 19:42:44 -04002699 platform_driver_unregister(&net2272_plat_driver);
2700}
2701module_exit(net2272_cleanup);
2702
2703MODULE_DESCRIPTION(DRIVER_DESC);
2704MODULE_AUTHOR("PLX Technology, Inc.");
2705MODULE_LICENSE("GPL");