blob: 8112d9140a90fb6d658384d320bb9e82052df6fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
4 *
David Brownell901b3d72006-09-02 03:13:45 -07005 * PLX Technology Inc. (formerly NetChip Technology) supported the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * development of this driver.
7 *
8 *
9 * CODE STATUS HIGHLIGHTS
10 *
11 * This driver should work well with most "gadget" drivers, including
Michal Nazarewiczfa069202012-11-06 22:52:36 +010012 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * as well as Gadget Zero and Gadgetfs.
14 *
15 * DMA is enabled by default. Drivers using transfer queues might use
16 * DMA chaining to remove IRQ latencies between transfers. (Except when
17 * short OUT transfers happen.) Drivers can use the req->no_interrupt
18 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
19 * and DMA chaining is enabled.
20 *
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020021 * MSI is enabled by default. The legacy IRQ is used if MSI couldn't
22 * be enabled.
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * Note that almost all the errata workarounds here are only needed for
25 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
26 */
27
28/*
29 * Copyright (C) 2003 David Brownell
30 * Copyright (C) 2003-2005 PLX Technology, Inc.
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020031 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
David Brownell901b3d72006-09-02 03:13:45 -070033 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
34 * with 2282 chip
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +010035 *
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020036 * Modified Ricardo Ribalda Qtechnology AS to provide compatibility
37 * with usb 338x chip. Based on PLX driver
38 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
45#undef DEBUG /* messages on error and most fault paths */
46#undef VERBOSE /* extra debug messages (success too) */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/module.h>
49#include <linux/pci.h>
David Brownell682d4c82006-01-18 23:55:08 -080050#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/kernel.h>
52#include <linux/delay.h>
53#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/errno.h>
56#include <linux/init.h>
57#include <linux/timer.h>
58#include <linux/list.h>
59#include <linux/interrupt.h>
60#include <linux/moduleparam.h>
61#include <linux/device.h>
David Brownell5f848132006-12-16 15:34:53 -080062#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070063#include <linux/usb/gadget.h>
Bryan Wub38b03b2011-06-02 12:51:29 +080064#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <asm/byteorder.h>
67#include <asm/io.h>
68#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/unaligned.h>
70
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020071#define DRIVER_DESC "PLX NET228x/USB338x USB Peripheral Controller"
72#define DRIVER_VERSION "2005 Sept 27/v3.0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define EP_DONTUSE 13 /* nonzero */
75
76#define USE_RDK_LEDS /* GPIO pins control three LEDs */
77
78
79static const char driver_name [] = "net2280";
80static const char driver_desc [] = DRIVER_DESC;
81
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020082static const u32 ep_bit[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static const char ep0name [] = "ep0";
David Brownell901b3d72006-09-02 03:13:45 -070084static const char *const ep_name [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 ep0name,
86 "ep-a", "ep-b", "ep-c", "ep-d",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020087 "ep-e", "ep-f", "ep-g", "ep-h",
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90/* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
91 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
92 *
93 * The net2280 DMA engines are not tightly integrated with their FIFOs;
94 * not all cases are (yet) handled well in this driver or the silicon.
95 * Some gadget drivers work better with the dma support here than others.
96 * These two parameters let you use PIO or more aggressive DMA.
97 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103098static bool use_dma = 1;
99static bool use_dma_chaining = 0;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200100static bool use_msi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* "modprobe net2280 use_dma=n" etc */
103module_param (use_dma, bool, S_IRUGO);
104module_param (use_dma_chaining, bool, S_IRUGO);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200105module_param(use_msi, bool, S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* mode 0 == ep-{a,b,c,d} 1K fifo each
108 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
109 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
110 */
111static ushort fifo_mode = 0;
112
113/* "modprobe net2280 fifo_mode=1" etc */
114module_param (fifo_mode, ushort, 0644);
115
116/* enable_suspend -- When enabled, the driver will respond to
117 * USB suspend requests by powering down the NET2280. Otherwise,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300118 * USB suspend requests will be ignored. This is acceptable for
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100119 * self-powered devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030121static bool enable_suspend = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/* "modprobe net2280 enable_suspend=1" etc */
124module_param (enable_suspend, bool, S_IRUGO);
125
Alan Stern2f076072013-01-30 16:40:14 -0500126/* force full-speed operation */
127static bool full_speed;
128module_param(full_speed, bool, 0444);
129MODULE_PARM_DESC(full_speed, "force full-speed mode -- for testing only!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131#define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
132
133#if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
134static char *type_string (u8 bmAttributes)
135{
136 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
137 case USB_ENDPOINT_XFER_BULK: return "bulk";
138 case USB_ENDPOINT_XFER_ISOC: return "iso";
139 case USB_ENDPOINT_XFER_INT: return "intr";
Joe Perches2b84f922013-10-08 16:01:37 -0700140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return "control";
142}
143#endif
144
145#include "net2280.h"
146
Harvey Harrison551509d2009-02-11 14:11:36 -0800147#define valid_bit cpu_to_le32 (1 << VALID_BIT)
148#define dma_done_ie cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/*-------------------------------------------------------------------------*/
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200151static inline void enable_pciirqenb(struct net2280_ep *ep)
152{
153 u32 tmp = readl(&ep->dev->regs->pciirqenb0);
154
155 if (ep->dev->pdev->vendor == 0x17cc)
156 tmp |= 1 << ep->num;
157 else
158 tmp |= 1 << ep_bit[ep->num];
159 writel(tmp, &ep->dev->regs->pciirqenb0);
160
161 return;
162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164static int
165net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
166{
167 struct net2280 *dev;
168 struct net2280_ep *ep;
169 u32 max, tmp;
170 unsigned long flags;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200171 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 ep = container_of (_ep, struct net2280_ep, ep);
174 if (!_ep || !desc || ep->desc || _ep->name == ep0name
175 || desc->bDescriptorType != USB_DT_ENDPOINT)
176 return -EINVAL;
177 dev = ep->dev;
178 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
179 return -ESHUTDOWN;
180
181 /* erratum 0119 workaround ties up an endpoint number */
182 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
183 return -EDOM;
184
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200185 if (dev->pdev->vendor == 0x10b5) {
186 if ((desc->bEndpointAddress & 0x0f) >= 0x0c)
187 return -EDOM;
188 ep->is_in = !!usb_endpoint_dir_in(desc);
189 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num])
190 return -EINVAL;
191 }
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* sanity check ep-e/ep-f since their fifos are small */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700194 max = usb_endpoint_maxp (desc) & 0x1fff;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200195 if (ep->num > 4 && max > 64 && (dev->pdev->vendor == 0x17cc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -ERANGE;
197
198 spin_lock_irqsave (&dev->lock, flags);
199 _ep->maxpacket = max & 0x7ff;
200 ep->desc = desc;
201
202 /* ep_reset() has already been called */
203 ep->stopped = 0;
Alan Stern80661342008-08-14 15:49:11 -0400204 ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ep->out_overflow = 0;
206
207 /* set speed-dependent max packet; may kick in high bandwidth */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200208 set_max_speed(ep, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* FIFO lines can't go to different packets. PIO is ok, so
211 * use it instead of troublesome (non-bulk) multi-packet DMA.
212 */
213 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
214 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
215 ep->ep.name, ep->ep.maxpacket);
216 ep->dma = NULL;
217 }
218
219 /* set type, direction, address; reset fifo counters */
220 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
221 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
222 if (tmp == USB_ENDPOINT_XFER_INT) {
223 /* erratum 0105 workaround prevents hs NYET */
224 if (dev->chiprev == 0100
225 && dev->gadget.speed == USB_SPEED_HIGH
226 && !(desc->bEndpointAddress & USB_DIR_IN))
227 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
228 &ep->regs->ep_rsp);
229 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
230 /* catch some particularly blatant driver bugs */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200231 if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
232 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
233 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
234 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -ERANGE;
236 }
237 }
238 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200239 /* Enable this endpoint */
240 if (dev->pdev->vendor == 0x17cc) {
241 tmp <<= ENDPOINT_TYPE;
242 tmp |= desc->bEndpointAddress;
243 /* default full fifo lines */
244 tmp |= (4 << ENDPOINT_BYTE_COUNT);
245 tmp |= 1 << ENDPOINT_ENABLE;
246 ep->is_in = (tmp & USB_DIR_IN) != 0;
247 } else {
248 /* In Legacy mode, only OUT endpoints are used */
249 if (dev->enhanced_mode && ep->is_in) {
250 tmp <<= IN_ENDPOINT_TYPE;
251 tmp |= (1 << IN_ENDPOINT_ENABLE);
252 /* Not applicable to Legacy */
253 tmp |= (1 << ENDPOINT_DIRECTION);
254 } else {
255 tmp <<= OUT_ENDPOINT_TYPE;
256 tmp |= (1 << OUT_ENDPOINT_ENABLE);
257 tmp |= (ep->is_in << ENDPOINT_DIRECTION);
258 }
259
260 tmp |= usb_endpoint_num(desc);
261 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
262 }
263
264 /* Make sure all the registers are written before ep_rsp*/
265 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* for OUT transfers, block the rx fifo until a read is posted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!ep->is_in)
269 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100270 else if (dev->pdev->device != 0x2280) {
David Brownell901b3d72006-09-02 03:13:45 -0700271 /* Added for 2282, Don't use nak packets on an in endpoint,
272 * this was ignored on 2280
273 */
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100274 writel ((1 << CLEAR_NAK_OUT_PACKETS)
275 | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200278 writel(tmp, &ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* enable irqs */
281 if (!ep->dma) { /* pio, per-packet */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200282 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100285 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
286 if (dev->pdev->device == 0x2280)
287 tmp |= readl (&ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 writel (tmp, &ep->regs->ep_irqenb);
289 } else { /* dma, per-request */
290 tmp = (1 << (8 + ep->num)); /* completion */
291 tmp |= readl (&dev->regs->pciirqenb1);
292 writel (tmp, &dev->regs->pciirqenb1);
293
294 /* for short OUT transfers, dma completions can't
295 * advance the queue; do it pio-style, by hand.
296 * NOTE erratum 0112 workaround #2
297 */
298 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
299 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
300 writel (tmp, &ep->regs->ep_irqenb);
301
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200302 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 }
305
306 tmp = desc->bEndpointAddress;
307 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
308 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
309 type_string (desc->bmAttributes),
310 ep->dma ? "dma" : "pio", max);
311
312 /* pci writes may still be posted */
313 spin_unlock_irqrestore (&dev->lock, flags);
314 return 0;
315}
316
317static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
318{
319 u32 result;
320
321 do {
322 result = readl (ptr);
323 if (result == ~(u32)0) /* "device unplugged" */
324 return -ENODEV;
325 result &= mask;
326 if (result == done)
327 return 0;
328 udelay (1);
329 usec--;
330 } while (usec > 0);
331 return -ETIMEDOUT;
332}
333
David Brownell901b3d72006-09-02 03:13:45 -0700334static const struct usb_ep_ops net2280_ep_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200336static void ep_reset_228x(struct net2280_regs __iomem *regs,
337 struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 u32 tmp;
340
341 ep->desc = NULL;
342 INIT_LIST_HEAD (&ep->queue);
343
Robert Baldygae117e742013-12-13 12:23:38 +0100344 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ep->ep.ops = &net2280_ep_ops;
346
347 /* disable the dma, irqs, endpoint... */
348 if (ep->dma) {
349 writel (0, &ep->dma->dmactl);
350 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
351 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
352 | (1 << DMA_ABORT)
353 , &ep->dma->dmastat);
354
355 tmp = readl (&regs->pciirqenb0);
356 tmp &= ~(1 << ep->num);
357 writel (tmp, &regs->pciirqenb0);
358 } else {
359 tmp = readl (&regs->pciirqenb1);
360 tmp &= ~(1 << (8 + ep->num)); /* completion */
361 writel (tmp, &regs->pciirqenb1);
362 }
363 writel (0, &ep->regs->ep_irqenb);
364
365 /* init to our chosen defaults, notably so that we NAK OUT
366 * packets until the driver queues a read (+note erratum 0112)
367 */
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100368 if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
369 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 | (1 << SET_NAK_OUT_PACKETS)
371 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
372 | (1 << CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100373 } else {
374 /* added for 2282 */
375 tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
376 | (1 << CLEAR_NAK_OUT_PACKETS)
377 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
378 | (1 << CLEAR_INTERRUPT_MODE);
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (ep->num != 0) {
382 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
383 | (1 << CLEAR_ENDPOINT_HALT);
384 }
385 writel (tmp, &ep->regs->ep_rsp);
386
387 /* scrub most status bits, and flush any fifo state */
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100388 if (ep->dev->pdev->device == 0x2280)
389 tmp = (1 << FIFO_OVERFLOW)
390 | (1 << FIFO_UNDERFLOW);
391 else
392 tmp = 0;
393
394 writel (tmp | (1 << TIMEOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 | (1 << USB_STALL_SENT)
396 | (1 << USB_IN_NAK_SENT)
397 | (1 << USB_IN_ACK_RCVD)
398 | (1 << USB_OUT_PING_NAK_SENT)
399 | (1 << USB_OUT_ACK_SENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 | (1 << FIFO_FLUSH)
401 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
402 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
403 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
404 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
405 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
406 | (1 << DATA_IN_TOKEN_INTERRUPT)
407 , &ep->regs->ep_stat);
408
409 /* fifo size is handled separately */
410}
411
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200412static void ep_reset_338x(struct net2280_regs __iomem *regs,
413 struct net2280_ep *ep)
414{
415 u32 tmp, dmastat;
416
417 ep->desc = NULL;
418 INIT_LIST_HEAD(&ep->queue);
419
420 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
421 ep->ep.ops = &net2280_ep_ops;
422
423 /* disable the dma, irqs, endpoint... */
424 if (ep->dma) {
425 writel(0, &ep->dma->dmactl);
426 writel((1 << DMA_ABORT_DONE_INTERRUPT) |
427 (1 << DMA_PAUSE_DONE_INTERRUPT) |
428 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT) |
429 (1 << DMA_TRANSACTION_DONE_INTERRUPT)
430 /* | (1 << DMA_ABORT) */
431 , &ep->dma->dmastat);
432
433 dmastat = readl(&ep->dma->dmastat);
434 if (dmastat == 0x5002) {
435 WARNING(ep->dev, "The dmastat return = %x!!\n",
436 dmastat);
437 writel(0x5a, &ep->dma->dmastat);
438 }
439
440 tmp = readl(&regs->pciirqenb0);
441 tmp &= ~(1 << ep_bit[ep->num]);
442 writel(tmp, &regs->pciirqenb0);
443 } else {
444 if (ep->num < 5) {
445 tmp = readl(&regs->pciirqenb1);
446 tmp &= ~(1 << (8 + ep->num)); /* completion */
447 writel(tmp, &regs->pciirqenb1);
448 }
449 }
450 writel(0, &ep->regs->ep_irqenb);
451
452 writel((1 << SHORT_PACKET_OUT_DONE_INTERRUPT) |
453 (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT) |
454 (1 << FIFO_OVERFLOW) |
455 (1 << DATA_PACKET_RECEIVED_INTERRUPT) |
456 (1 << DATA_PACKET_TRANSMITTED_INTERRUPT) |
457 (1 << DATA_OUT_PING_TOKEN_INTERRUPT) |
458 (1 << DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static void nuke (struct net2280_ep *);
462
463static int net2280_disable (struct usb_ep *_ep)
464{
465 struct net2280_ep *ep;
466 unsigned long flags;
467
468 ep = container_of (_ep, struct net2280_ep, ep);
469 if (!_ep || !ep->desc || _ep->name == ep0name)
470 return -EINVAL;
471
472 spin_lock_irqsave (&ep->dev->lock, flags);
473 nuke (ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200474
475 if (ep->dev->pdev->vendor == 0x10b5)
476 ep_reset_338x(ep->dev->regs, ep);
477 else
478 ep_reset_228x(ep->dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 VDEBUG (ep->dev, "disabled %s %s\n",
481 ep->dma ? "dma" : "pio", _ep->name);
482
483 /* synch memory views with the device */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200484 (void)readl(&ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
487 ep->dma = &ep->dev->dma [ep->num - 1];
488
489 spin_unlock_irqrestore (&ep->dev->lock, flags);
490 return 0;
491}
492
493/*-------------------------------------------------------------------------*/
494
495static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400496net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct net2280_ep *ep;
499 struct net2280_request *req;
500
501 if (!_ep)
502 return NULL;
503 ep = container_of (_ep, struct net2280_ep, ep);
504
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800505 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!req)
507 return NULL;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 INIT_LIST_HEAD (&req->queue);
510
511 /* this dma descriptor may be swapped with the previous dummy */
512 if (ep->dma) {
513 struct net2280_dma *td;
514
515 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
516 &req->td_dma);
517 if (!td) {
518 kfree (req);
519 return NULL;
520 }
521 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 td->dmadesc = td->dmaaddr;
523 req->td = td;
524 }
525 return &req->req;
526}
527
528static void
529net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
530{
531 struct net2280_ep *ep;
532 struct net2280_request *req;
533
534 ep = container_of (_ep, struct net2280_ep, ep);
535 if (!_ep || !_req)
536 return;
537
538 req = container_of (_req, struct net2280_request, req);
539 WARN_ON (!list_empty (&req->queue));
540 if (req->td)
541 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
542 kfree (req);
543}
544
545/*-------------------------------------------------------------------------*/
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/* load a packet into the fifo we use for usb IN transfers.
548 * works for all endpoints.
549 *
550 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
551 * at a time, but this code is simpler because it knows it only writes
552 * one packet. ep-a..ep-d should use dma instead.
553 */
554static void
555write_fifo (struct net2280_ep *ep, struct usb_request *req)
556{
557 struct net2280_ep_regs __iomem *regs = ep->regs;
558 u8 *buf;
559 u32 tmp;
560 unsigned count, total;
561
562 /* INVARIANT: fifo is currently empty. (testable) */
563
564 if (req) {
565 buf = req->buf + req->actual;
566 prefetch (buf);
567 total = req->length - req->actual;
568 } else {
569 total = 0;
570 buf = NULL;
571 }
572
573 /* write just one packet at a time */
574 count = ep->ep.maxpacket;
575 if (count > total) /* min() cannot be used on a bitfield */
576 count = total;
577
578 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
579 ep->ep.name, count,
580 (count != ep->ep.maxpacket) ? " (short)" : "",
581 req);
582 while (count >= 4) {
583 /* NOTE be careful if you try to align these. fifo lines
584 * should normally be full (4 bytes) and successive partial
585 * lines are ok only in certain cases.
586 */
587 tmp = get_unaligned ((u32 *)buf);
588 cpu_to_le32s (&tmp);
589 writel (tmp, &regs->ep_data);
590 buf += 4;
591 count -= 4;
592 }
593
594 /* last fifo entry is "short" unless we wrote a full packet.
595 * also explicitly validate last word in (periodic) transfers
596 * when maxpacket is not a multiple of 4 bytes.
597 */
598 if (count || total < ep->ep.maxpacket) {
599 tmp = count ? get_unaligned ((u32 *)buf) : count;
600 cpu_to_le32s (&tmp);
601 set_fifo_bytecount (ep, count & 0x03);
602 writel (tmp, &regs->ep_data);
603 }
604
605 /* pci writes may still be posted */
606}
607
608/* work around erratum 0106: PCI and USB race over the OUT fifo.
609 * caller guarantees chiprev 0100, out endpoint is NAKing, and
610 * there's no real data in the fifo.
611 *
612 * NOTE: also used in cases where that erratum doesn't apply:
613 * where the host wrote "too much" data to us.
614 */
615static void out_flush (struct net2280_ep *ep)
616{
617 u32 __iomem *statp;
618 u32 tmp;
619
620 ASSERT_OUT_NAKING (ep);
621
622 statp = &ep->regs->ep_stat;
623 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
624 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
625 , statp);
626 writel ((1 << FIFO_FLUSH), statp);
627 mb ();
628 tmp = readl (statp);
629 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
630 /* high speed did bulk NYET; fifo isn't filling */
631 && ep->dev->gadget.speed == USB_SPEED_FULL) {
632 unsigned usec;
633
634 usec = 50; /* 64 byte bulk/interrupt */
635 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
636 (1 << USB_OUT_PING_NAK_SENT), usec);
637 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
638 }
639}
640
641/* unload packet(s) from the fifo we use for usb OUT transfers.
642 * returns true iff the request completed, because of short packet
643 * or the request buffer having filled with full packets.
644 *
645 * for ep-a..ep-d this will read multiple packets out when they
646 * have been accepted.
647 */
648static int
649read_fifo (struct net2280_ep *ep, struct net2280_request *req)
650{
651 struct net2280_ep_regs __iomem *regs = ep->regs;
652 u8 *buf = req->req.buf + req->req.actual;
653 unsigned count, tmp, is_short;
654 unsigned cleanup = 0, prevent = 0;
655
656 /* erratum 0106 ... packets coming in during fifo reads might
657 * be incompletely rejected. not all cases have workarounds.
658 */
659 if (ep->dev->chiprev == 0x0100
660 && ep->dev->gadget.speed == USB_SPEED_FULL) {
661 udelay (1);
662 tmp = readl (&ep->regs->ep_stat);
663 if ((tmp & (1 << NAK_OUT_PACKETS)))
664 cleanup = 1;
665 else if ((tmp & (1 << FIFO_FULL))) {
666 start_out_naking (ep);
667 prevent = 1;
668 }
669 /* else: hope we don't see the problem */
670 }
671
672 /* never overflow the rx buffer. the fifo reads packets until
673 * it sees a short one; we might not be ready for them all.
674 */
675 prefetchw (buf);
676 count = readl (&regs->ep_avail);
677 if (unlikely (count == 0)) {
678 udelay (1);
679 tmp = readl (&ep->regs->ep_stat);
680 count = readl (&regs->ep_avail);
681 /* handled that data already? */
682 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
683 return 0;
684 }
685
686 tmp = req->req.length - req->req.actual;
687 if (count > tmp) {
688 /* as with DMA, data overflow gets flushed */
689 if ((tmp % ep->ep.maxpacket) != 0) {
690 ERROR (ep->dev,
691 "%s out fifo %d bytes, expected %d\n",
692 ep->ep.name, count, tmp);
693 req->req.status = -EOVERFLOW;
694 cleanup = 1;
695 /* NAK_OUT_PACKETS will be set, so flushing is safe;
696 * the next read will start with the next packet
697 */
698 } /* else it's a ZLP, no worries */
699 count = tmp;
700 }
701 req->req.actual += count;
702
703 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
704
705 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
706 ep->ep.name, count, is_short ? " (short)" : "",
707 cleanup ? " flush" : "", prevent ? " nak" : "",
708 req, req->req.actual, req->req.length);
709
710 while (count >= 4) {
711 tmp = readl (&regs->ep_data);
712 cpu_to_le32s (&tmp);
713 put_unaligned (tmp, (u32 *)buf);
714 buf += 4;
715 count -= 4;
716 }
717 if (count) {
718 tmp = readl (&regs->ep_data);
719 /* LE conversion is implicit here: */
720 do {
721 *buf++ = (u8) tmp;
722 tmp >>= 8;
723 } while (--count);
724 }
725 if (cleanup)
726 out_flush (ep);
727 if (prevent) {
728 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
729 (void) readl (&ep->regs->ep_rsp);
730 }
731
732 return is_short || ((req->req.actual == req->req.length)
733 && !req->req.zero);
734}
735
736/* fill out dma descriptor to match a given request */
737static void
738fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
739{
740 struct net2280_dma *td = req->td;
741 u32 dmacount = req->req.length;
742
743 /* don't let DMA continue after a short OUT packet,
744 * so overruns can't affect the next transfer.
745 * in case of overruns on max-size packets, we can't
746 * stop the fifo from filling but we can flush it.
747 */
748 if (ep->is_in)
749 dmacount |= (1 << DMA_DIRECTION);
David Brownell901b3d72006-09-02 03:13:45 -0700750 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
751 || ep->dev->pdev->device != 0x2280)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 dmacount |= (1 << END_OF_CHAIN);
753
754 req->valid = valid;
755 if (valid)
756 dmacount |= (1 << VALID_BIT);
757 if (likely(!req->req.no_interrupt || !use_dma_chaining))
758 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
759
760 /* td->dmadesc = previously set by caller */
761 td->dmaaddr = cpu_to_le32 (req->req.dma);
762
763 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
764 wmb ();
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700765 td->dmacount = cpu_to_le32(dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768static const u32 dmactl_default =
769 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
770 | (1 << DMA_CLEAR_COUNT_ENABLE)
771 /* erratum 0116 workaround part 1 (use POLLING) */
772 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
773 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
774 | (1 << DMA_VALID_BIT_ENABLE)
775 | (1 << DMA_SCATTER_GATHER_ENABLE)
776 /* erratum 0116 workaround part 2 (no AUTOSTART) */
777 | (1 << DMA_ENABLE);
778
779static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
780{
781 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
782}
783
784static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
785{
786 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
787 spin_stop_dma (dma);
788}
789
790static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
791{
792 struct net2280_dma_regs __iomem *dma = ep->dma;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100793 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100795 if (ep->dev->pdev->device != 0x2280)
796 tmp |= (1 << END_OF_CHAIN);
797
798 writel (tmp, &dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 writel (readl (&dma->dmastat), &dma->dmastat);
800
801 writel (td_dma, &dma->dmadesc);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200802 if (ep->dev->pdev->vendor == 0x10b5)
803 dmactl |= (0x01 << DMA_REQUEST_OUTSTANDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 writel (dmactl, &dma->dmactl);
805
806 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
807 (void) readl (&ep->dev->pci->pcimstctl);
808
809 writel ((1 << DMA_START), &dma->dmastat);
810
811 if (!ep->is_in)
812 stop_out_naking (ep);
813}
814
815static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
816{
817 u32 tmp;
818 struct net2280_dma_regs __iomem *dma = ep->dma;
819
820 /* FIXME can't use DMA for ZLPs */
821
822 /* on this path we "know" there's no dma active (yet) */
823 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
824 writel (0, &ep->dma->dmactl);
825
826 /* previous OUT packet might have been short */
827 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
David Brownell901b3d72006-09-02 03:13:45 -0700828 & (1 << NAK_OUT_PACKETS)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
830 &ep->regs->ep_stat);
831
832 tmp = readl (&ep->regs->ep_avail);
833 if (tmp) {
834 writel (readl (&dma->dmastat), &dma->dmastat);
835
836 /* transfer all/some fifo data */
837 writel (req->req.dma, &dma->dmaaddr);
838 tmp = min (tmp, req->req.length);
839
840 /* dma irq, faking scatterlist status */
841 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
842 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
843 | tmp, &dma->dmacount);
844 req->td->dmadesc = 0;
845 req->valid = 1;
846
847 writel ((1 << DMA_ENABLE), &dma->dmactl);
848 writel ((1 << DMA_START), &dma->dmastat);
849 return;
850 }
851 }
852
853 tmp = dmactl_default;
854
855 /* force packet boundaries between dma requests, but prevent the
856 * controller from automagically writing a last "short" packet
857 * (zero length) unless the driver explicitly said to do that.
858 */
859 if (ep->is_in) {
860 if (likely ((req->req.length % ep->ep.maxpacket) != 0
861 || req->req.zero)) {
862 tmp |= (1 << DMA_FIFO_VALIDATE);
863 ep->in_fifo_validate = 1;
864 } else
865 ep->in_fifo_validate = 0;
866 }
867
868 /* init req->td, pointing to the current dummy */
869 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
870 fill_dma_desc (ep, req, 1);
871
872 if (!use_dma_chaining)
Harvey Harrison551509d2009-02-11 14:11:36 -0800873 req->td->dmacount |= cpu_to_le32 (1 << END_OF_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 start_queue (ep, tmp, req->td_dma);
876}
877
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200878static inline void resume_dma(struct net2280_ep *ep)
879{
880 writel(readl(&ep->dma->dmactl) | (1 << DMA_ENABLE), &ep->dma->dmactl);
881
882 ep->dma_started = true;
883}
884
885static inline void ep_stop_dma(struct net2280_ep *ep)
886{
887 writel(readl(&ep->dma->dmactl) & ~(1 << DMA_ENABLE), &ep->dma->dmactl);
888 spin_stop_dma(ep->dma);
889
890 ep->dma_started = false;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static inline void
894queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
895{
896 struct net2280_dma *end;
897 dma_addr_t tmp;
898
899 /* swap new dummy for old, link; fill and maybe activate */
900 end = ep->dummy;
901 ep->dummy = req->td;
902 req->td = end;
903
904 tmp = ep->td_dma;
905 ep->td_dma = req->td_dma;
906 req->td_dma = tmp;
907
908 end->dmadesc = cpu_to_le32 (ep->td_dma);
909
910 fill_dma_desc (ep, req, valid);
911}
912
913static void
914done (struct net2280_ep *ep, struct net2280_request *req, int status)
915{
916 struct net2280 *dev;
917 unsigned stopped = ep->stopped;
918
919 list_del_init (&req->queue);
920
921 if (req->req.status == -EINPROGRESS)
922 req->req.status = status;
923 else
924 status = req->req.status;
925
926 dev = ep->dev;
Felipe Balbiae4d7932011-12-19 12:09:56 +0200927 if (ep->dma)
928 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (status && status != -ESHUTDOWN)
931 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
932 ep->ep.name, &req->req, status,
933 req->req.actual, req->req.length);
934
935 /* don't modify queue heads during completion callback */
936 ep->stopped = 1;
937 spin_unlock (&dev->lock);
938 req->req.complete (&ep->ep, &req->req);
939 spin_lock (&dev->lock);
940 ep->stopped = stopped;
941}
942
943/*-------------------------------------------------------------------------*/
944
945static int
Al Viro55016f12005-10-21 03:21:58 -0400946net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 struct net2280_request *req;
949 struct net2280_ep *ep;
950 struct net2280 *dev;
951 unsigned long flags;
952
953 /* we always require a cpu-view buffer, so that we can
954 * always use pio (as fallback or whatever).
955 */
956 req = container_of (_req, struct net2280_request, req);
957 if (!_req || !_req->complete || !_req->buf
958 || !list_empty (&req->queue))
959 return -EINVAL;
960 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
961 return -EDOM;
962 ep = container_of (_ep, struct net2280_ep, ep);
963 if (!_ep || (!ep->desc && ep->num != 0))
964 return -EINVAL;
965 dev = ep->dev;
966 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
967 return -ESHUTDOWN;
968
969 /* FIXME implement PIO fallback for ZLPs with DMA */
970 if (ep->dma && _req->length == 0)
971 return -EOPNOTSUPP;
972
973 /* set up dma mapping in case the caller didn't */
Felipe Balbiae4d7932011-12-19 12:09:56 +0200974 if (ep->dma) {
975 int ret;
976
977 ret = usb_gadget_map_request(&dev->gadget, _req,
978 ep->is_in);
979 if (ret)
980 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
983#if 0
984 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
985 _ep->name, _req, _req->length, _req->buf);
986#endif
987
988 spin_lock_irqsave (&dev->lock, flags);
989
990 _req->status = -EINPROGRESS;
991 _req->actual = 0;
992
993 /* kickstart this i/o queue? */
994 if (list_empty (&ep->queue) && !ep->stopped) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200995 /* DMA request while EP halted */
996 if (ep->dma &&
997 (readl(&ep->regs->ep_rsp) & (1 << CLEAR_ENDPOINT_HALT)) &&
998 (dev->pdev->vendor == 0x10b5)) {
999 int valid = 1;
1000 if (ep->is_in) {
1001 int expect;
1002 expect = likely(req->req.zero ||
1003 ((req->req.length %
1004 ep->ep.maxpacket) != 0));
1005 if (expect != ep->in_fifo_validate)
1006 valid = 0;
1007 }
1008 queue_dma(ep, req, valid);
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* use DMA if the endpoint supports it, else pio */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001011 else if (ep->dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 start_dma (ep, req);
1013 else {
1014 /* maybe there's no control data, just status ack */
1015 if (ep->num == 0 && _req->length == 0) {
1016 allow_status (ep);
1017 done (ep, req, 0);
1018 VDEBUG (dev, "%s status ack\n", ep->ep.name);
1019 goto done;
1020 }
1021
1022 /* PIO ... stuff the fifo, or unblock it. */
1023 if (ep->is_in)
1024 write_fifo (ep, _req);
1025 else if (list_empty (&ep->queue)) {
1026 u32 s;
1027
1028 /* OUT FIFO might have packet(s) buffered */
1029 s = readl (&ep->regs->ep_stat);
1030 if ((s & (1 << FIFO_EMPTY)) == 0) {
1031 /* note: _req->short_not_ok is
1032 * ignored here since PIO _always_
1033 * stops queue advance here, and
1034 * _req->status doesn't change for
1035 * short reads (only _req->actual)
1036 */
1037 if (read_fifo (ep, req)) {
1038 done (ep, req, 0);
1039 if (ep->num == 0)
1040 allow_status (ep);
1041 /* don't queue it */
1042 req = NULL;
1043 } else
1044 s = readl (&ep->regs->ep_stat);
1045 }
1046
1047 /* don't NAK, let the fifo fill */
1048 if (req && (s & (1 << NAK_OUT_PACKETS)))
1049 writel ((1 << CLEAR_NAK_OUT_PACKETS),
1050 &ep->regs->ep_rsp);
1051 }
1052 }
1053
1054 } else if (ep->dma) {
1055 int valid = 1;
1056
1057 if (ep->is_in) {
1058 int expect;
1059
1060 /* preventing magic zlps is per-engine state, not
1061 * per-transfer; irq logic must recover hiccups.
1062 */
1063 expect = likely (req->req.zero
1064 || (req->req.length % ep->ep.maxpacket) != 0);
1065 if (expect != ep->in_fifo_validate)
1066 valid = 0;
1067 }
1068 queue_dma (ep, req, valid);
1069
1070 } /* else the irq handler advances the queue. */
1071
Alan Stern1f26e282006-11-16 10:16:00 -05001072 ep->responded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (req)
1074 list_add_tail (&req->queue, &ep->queue);
1075done:
1076 spin_unlock_irqrestore (&dev->lock, flags);
1077
1078 /* pci writes may still be posted */
1079 return 0;
1080}
1081
1082static inline void
1083dma_done (
1084 struct net2280_ep *ep,
1085 struct net2280_request *req,
1086 u32 dmacount,
1087 int status
1088)
1089{
1090 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1091 done (ep, req, status);
1092}
1093
1094static void restart_dma (struct net2280_ep *ep);
1095
1096static void scan_dma_completions (struct net2280_ep *ep)
1097{
1098 /* only look at descriptors that were "naturally" retired,
1099 * so fifo and list head state won't matter
1100 */
1101 while (!list_empty (&ep->queue)) {
1102 struct net2280_request *req;
1103 u32 tmp;
1104
1105 req = list_entry (ep->queue.next,
1106 struct net2280_request, queue);
1107 if (!req->valid)
1108 break;
1109 rmb ();
1110 tmp = le32_to_cpup (&req->td->dmacount);
1111 if ((tmp & (1 << VALID_BIT)) != 0)
1112 break;
1113
1114 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1115 * cases where DMA must be aborted; this code handles
1116 * all non-abort DMA completions.
1117 */
1118 if (unlikely (req->td->dmadesc == 0)) {
1119 /* paranoia */
1120 tmp = readl (&ep->dma->dmacount);
1121 if (tmp & DMA_BYTE_COUNT_MASK)
1122 break;
1123 /* single transfer mode */
1124 dma_done (ep, req, tmp, 0);
1125 break;
1126 } else if (!ep->is_in
1127 && (req->req.length % ep->ep.maxpacket) != 0) {
1128 tmp = readl (&ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001129 if (ep->dev->pdev->vendor == 0x10b5)
1130 return dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /* AVOID TROUBLE HERE by not issuing short reads from
1133 * your gadget driver. That helps avoids errata 0121,
1134 * 0122, and 0124; not all cases trigger the warning.
1135 */
1136 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001137 WARNING (ep->dev, "%s lost packet sync!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 ep->ep.name);
1139 req->req.status = -EOVERFLOW;
1140 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1141 /* fifo gets flushed later */
1142 ep->out_overflow = 1;
1143 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1144 ep->ep.name, tmp,
1145 req->req.length);
1146 req->req.status = -EOVERFLOW;
1147 }
1148 }
1149 dma_done (ep, req, tmp, 0);
1150 }
1151}
1152
1153static void restart_dma (struct net2280_ep *ep)
1154{
1155 struct net2280_request *req;
1156 u32 dmactl = dmactl_default;
1157
1158 if (ep->stopped)
1159 return;
1160 req = list_entry (ep->queue.next, struct net2280_request, queue);
1161
1162 if (!use_dma_chaining) {
1163 start_dma (ep, req);
1164 return;
1165 }
1166
1167 /* the 2280 will be processing the queue unless queue hiccups after
1168 * the previous transfer:
1169 * IN: wanted automagic zlp, head doesn't (or vice versa)
1170 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1171 * OUT: was "usb-short", we must restart.
1172 */
1173 if (ep->is_in && !req->valid) {
1174 struct net2280_request *entry, *prev = NULL;
1175 int reqmode, done = 0;
1176
1177 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1178 ep->in_fifo_validate = likely (req->req.zero
1179 || (req->req.length % ep->ep.maxpacket) != 0);
1180 if (ep->in_fifo_validate)
1181 dmactl |= (1 << DMA_FIFO_VALIDATE);
1182 list_for_each_entry (entry, &ep->queue, queue) {
David Brownell320f3452005-05-07 13:05:18 -07001183 __le32 dmacount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (entry == req)
1186 continue;
1187 dmacount = entry->td->dmacount;
1188 if (!done) {
1189 reqmode = likely (entry->req.zero
1190 || (entry->req.length
1191 % ep->ep.maxpacket) != 0);
1192 if (reqmode == ep->in_fifo_validate) {
1193 entry->valid = 1;
1194 dmacount |= valid_bit;
1195 entry->td->dmacount = dmacount;
1196 prev = entry;
1197 continue;
1198 } else {
1199 /* force a hiccup */
1200 prev->td->dmacount |= dma_done_ie;
1201 done = 1;
1202 }
1203 }
1204
1205 /* walk the rest of the queue so unlinks behave */
1206 entry->valid = 0;
1207 dmacount &= ~valid_bit;
1208 entry->td->dmacount = dmacount;
1209 prev = entry;
1210 }
1211 }
1212
1213 writel (0, &ep->dma->dmactl);
1214 start_queue (ep, dmactl, req->td_dma);
1215}
1216
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001217static void abort_dma_228x(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 /* abort the current transfer */
1220 if (likely (!list_empty (&ep->queue))) {
1221 /* FIXME work around errata 0121, 0122, 0124 */
1222 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1223 spin_stop_dma (ep->dma);
1224 } else
1225 stop_dma (ep->dma);
1226 scan_dma_completions (ep);
1227}
1228
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001229static void abort_dma_338x(struct net2280_ep *ep)
1230{
1231 writel((1 << DMA_ABORT), &ep->dma->dmastat);
1232 spin_stop_dma(ep->dma);
1233}
1234
1235static void abort_dma(struct net2280_ep *ep)
1236{
1237 if (ep->dev->pdev->vendor == 0x17cc)
1238 return abort_dma_228x(ep);
1239 return abort_dma_338x(ep);
1240}
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242/* dequeue ALL requests */
1243static void nuke (struct net2280_ep *ep)
1244{
1245 struct net2280_request *req;
1246
1247 /* called with spinlock held */
1248 ep->stopped = 1;
1249 if (ep->dma)
1250 abort_dma (ep);
1251 while (!list_empty (&ep->queue)) {
1252 req = list_entry (ep->queue.next,
1253 struct net2280_request,
1254 queue);
1255 done (ep, req, -ESHUTDOWN);
1256 }
1257}
1258
1259/* dequeue JUST ONE request */
1260static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1261{
1262 struct net2280_ep *ep;
1263 struct net2280_request *req;
1264 unsigned long flags;
1265 u32 dmactl;
1266 int stopped;
1267
1268 ep = container_of (_ep, struct net2280_ep, ep);
1269 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1270 return -EINVAL;
1271
1272 spin_lock_irqsave (&ep->dev->lock, flags);
1273 stopped = ep->stopped;
1274
1275 /* quiesce dma while we patch the queue */
1276 dmactl = 0;
1277 ep->stopped = 1;
1278 if (ep->dma) {
1279 dmactl = readl (&ep->dma->dmactl);
1280 /* WARNING erratum 0127 may kick in ... */
1281 stop_dma (ep->dma);
1282 scan_dma_completions (ep);
1283 }
1284
1285 /* make sure it's still queued on this endpoint */
1286 list_for_each_entry (req, &ep->queue, queue) {
1287 if (&req->req == _req)
1288 break;
1289 }
1290 if (&req->req != _req) {
1291 spin_unlock_irqrestore (&ep->dev->lock, flags);
1292 return -EINVAL;
1293 }
1294
1295 /* queue head may be partially complete. */
1296 if (ep->queue.next == &req->queue) {
1297 if (ep->dma) {
1298 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1299 _req->status = -ECONNRESET;
1300 abort_dma (ep);
1301 if (likely (ep->queue.next == &req->queue)) {
1302 // NOTE: misreports single-transfer mode
1303 req->td->dmacount = 0; /* invalidate */
1304 dma_done (ep, req,
1305 readl (&ep->dma->dmacount),
1306 -ECONNRESET);
1307 }
1308 } else {
1309 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1310 done (ep, req, -ECONNRESET);
1311 }
1312 req = NULL;
1313
1314 /* patch up hardware chaining data */
1315 } else if (ep->dma && use_dma_chaining) {
1316 if (req->queue.prev == ep->queue.next) {
1317 writel (le32_to_cpu (req->td->dmadesc),
1318 &ep->dma->dmadesc);
1319 if (req->td->dmacount & dma_done_ie)
1320 writel (readl (&ep->dma->dmacount)
David Brownell320f3452005-05-07 13:05:18 -07001321 | le32_to_cpu(dma_done_ie),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 &ep->dma->dmacount);
1323 } else {
1324 struct net2280_request *prev;
1325
1326 prev = list_entry (req->queue.prev,
1327 struct net2280_request, queue);
1328 prev->td->dmadesc = req->td->dmadesc;
1329 if (req->td->dmacount & dma_done_ie)
1330 prev->td->dmacount |= dma_done_ie;
1331 }
1332 }
1333
1334 if (req)
1335 done (ep, req, -ECONNRESET);
1336 ep->stopped = stopped;
1337
1338 if (ep->dma) {
1339 /* turn off dma on inactive queues */
1340 if (list_empty (&ep->queue))
1341 stop_dma (ep->dma);
1342 else if (!ep->stopped) {
1343 /* resume current request, or start new one */
1344 if (req)
1345 writel (dmactl, &ep->dma->dmactl);
1346 else
1347 start_dma (ep, list_entry (ep->queue.next,
1348 struct net2280_request, queue));
1349 }
1350 }
1351
1352 spin_unlock_irqrestore (&ep->dev->lock, flags);
1353 return 0;
1354}
1355
1356/*-------------------------------------------------------------------------*/
1357
1358static int net2280_fifo_status (struct usb_ep *_ep);
1359
1360static int
Alan Stern80661342008-08-14 15:49:11 -04001361net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 struct net2280_ep *ep;
1364 unsigned long flags;
1365 int retval = 0;
1366
1367 ep = container_of (_ep, struct net2280_ep, ep);
1368 if (!_ep || (!ep->desc && ep->num != 0))
1369 return -EINVAL;
1370 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1371 return -ESHUTDOWN;
1372 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1373 == USB_ENDPOINT_XFER_ISOC)
1374 return -EINVAL;
1375
1376 spin_lock_irqsave (&ep->dev->lock, flags);
1377 if (!list_empty (&ep->queue))
1378 retval = -EAGAIN;
1379 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1380 retval = -EAGAIN;
1381 else {
Alan Stern80661342008-08-14 15:49:11 -04001382 VDEBUG (ep->dev, "%s %s %s\n", _ep->name,
1383 value ? "set" : "clear",
1384 wedged ? "wedge" : "halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* set/clear, then synch memory views with the device */
1386 if (value) {
1387 if (ep->num == 0)
1388 ep->dev->protocol_stall = 1;
1389 else
1390 set_halt (ep);
Alan Stern80661342008-08-14 15:49:11 -04001391 if (wedged)
1392 ep->wedged = 1;
1393 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 clear_halt (ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001395 if (ep->dev->pdev->vendor == 0x10b5 &&
1396 !list_empty(&ep->queue) && ep->td_dma)
1397 restart_dma(ep);
Alan Stern80661342008-08-14 15:49:11 -04001398 ep->wedged = 0;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 (void) readl (&ep->regs->ep_rsp);
1401 }
1402 spin_unlock_irqrestore (&ep->dev->lock, flags);
1403
1404 return retval;
1405}
1406
1407static int
Alan Stern80661342008-08-14 15:49:11 -04001408net2280_set_halt(struct usb_ep *_ep, int value)
1409{
1410 return net2280_set_halt_and_wedge(_ep, value, 0);
1411}
1412
1413static int
1414net2280_set_wedge(struct usb_ep *_ep)
1415{
1416 if (!_ep || _ep->name == ep0name)
1417 return -EINVAL;
1418 return net2280_set_halt_and_wedge(_ep, 1, 1);
1419}
1420
1421static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422net2280_fifo_status (struct usb_ep *_ep)
1423{
1424 struct net2280_ep *ep;
1425 u32 avail;
1426
1427 ep = container_of (_ep, struct net2280_ep, ep);
1428 if (!_ep || (!ep->desc && ep->num != 0))
1429 return -ENODEV;
1430 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1431 return -ESHUTDOWN;
1432
1433 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1434 if (avail > ep->fifo_size)
1435 return -EOVERFLOW;
1436 if (ep->is_in)
1437 avail = ep->fifo_size - avail;
1438 return avail;
1439}
1440
1441static void
1442net2280_fifo_flush (struct usb_ep *_ep)
1443{
1444 struct net2280_ep *ep;
1445
1446 ep = container_of (_ep, struct net2280_ep, ep);
1447 if (!_ep || (!ep->desc && ep->num != 0))
1448 return;
1449 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1450 return;
1451
1452 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1453 (void) readl (&ep->regs->ep_rsp);
1454}
1455
David Brownell901b3d72006-09-02 03:13:45 -07001456static const struct usb_ep_ops net2280_ep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 .enable = net2280_enable,
1458 .disable = net2280_disable,
1459
1460 .alloc_request = net2280_alloc_request,
1461 .free_request = net2280_free_request,
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 .queue = net2280_queue,
1464 .dequeue = net2280_dequeue,
1465
1466 .set_halt = net2280_set_halt,
Alan Stern80661342008-08-14 15:49:11 -04001467 .set_wedge = net2280_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 .fifo_status = net2280_fifo_status,
1469 .fifo_flush = net2280_fifo_flush,
1470};
1471
1472/*-------------------------------------------------------------------------*/
1473
1474static int net2280_get_frame (struct usb_gadget *_gadget)
1475{
1476 struct net2280 *dev;
1477 unsigned long flags;
1478 u16 retval;
1479
1480 if (!_gadget)
1481 return -ENODEV;
1482 dev = container_of (_gadget, struct net2280, gadget);
1483 spin_lock_irqsave (&dev->lock, flags);
1484 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1485 spin_unlock_irqrestore (&dev->lock, flags);
1486 return retval;
1487}
1488
1489static int net2280_wakeup (struct usb_gadget *_gadget)
1490{
1491 struct net2280 *dev;
1492 u32 tmp;
1493 unsigned long flags;
1494
1495 if (!_gadget)
1496 return 0;
1497 dev = container_of (_gadget, struct net2280, gadget);
1498
1499 spin_lock_irqsave (&dev->lock, flags);
1500 tmp = readl (&dev->usb->usbctl);
1501 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1502 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1503 spin_unlock_irqrestore (&dev->lock, flags);
1504
1505 /* pci writes may still be posted */
1506 return 0;
1507}
1508
1509static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1510{
1511 struct net2280 *dev;
1512 u32 tmp;
1513 unsigned long flags;
1514
1515 if (!_gadget)
1516 return 0;
1517 dev = container_of (_gadget, struct net2280, gadget);
1518
1519 spin_lock_irqsave (&dev->lock, flags);
1520 tmp = readl (&dev->usb->usbctl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001521 if (value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 tmp |= (1 << SELF_POWERED_STATUS);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001523 dev->selfpowered = 1;
1524 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 tmp &= ~(1 << SELF_POWERED_STATUS);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001526 dev->selfpowered = 0;
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 writel (tmp, &dev->usb->usbctl);
1529 spin_unlock_irqrestore (&dev->lock, flags);
1530
1531 return 0;
1532}
1533
1534static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1535{
1536 struct net2280 *dev;
1537 u32 tmp;
1538 unsigned long flags;
1539
1540 if (!_gadget)
1541 return -ENODEV;
1542 dev = container_of (_gadget, struct net2280, gadget);
1543
1544 spin_lock_irqsave (&dev->lock, flags);
1545 tmp = readl (&dev->usb->usbctl);
1546 dev->softconnect = (is_on != 0);
1547 if (is_on)
1548 tmp |= (1 << USB_DETECT_ENABLE);
1549 else
1550 tmp &= ~(1 << USB_DETECT_ENABLE);
1551 writel (tmp, &dev->usb->usbctl);
1552 spin_unlock_irqrestore (&dev->lock, flags);
1553
1554 return 0;
1555}
1556
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001557static int net2280_start(struct usb_gadget *_gadget,
1558 struct usb_gadget_driver *driver);
1559static int net2280_stop(struct usb_gadget *_gadget,
1560 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562static const struct usb_gadget_ops net2280_ops = {
1563 .get_frame = net2280_get_frame,
1564 .wakeup = net2280_wakeup,
1565 .set_selfpowered = net2280_set_selfpowered,
1566 .pullup = net2280_pullup,
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001567 .udc_start = net2280_start,
1568 .udc_stop = net2280_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569};
1570
1571/*-------------------------------------------------------------------------*/
1572
1573#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1574
1575/* FIXME move these into procfs, and use seq_file.
1576 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1577 * and also doesn't help products using this with 2.4 kernels.
1578 */
1579
1580/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001581static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1582 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 struct net2280 *dev = dev_get_drvdata (_dev);
1585
1586 if (!dev->driver
1587 || !dev->driver->function
1588 || strlen (dev->driver->function) > PAGE_SIZE)
1589 return 0;
1590 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1591}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001592static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001594static ssize_t registers_show(struct device *_dev,
1595 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 struct net2280 *dev;
1598 char *next;
1599 unsigned size, t;
1600 unsigned long flags;
1601 int i;
1602 u32 t1, t2;
Andrew Morton30e69592005-06-26 17:18:46 -07001603 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 dev = dev_get_drvdata (_dev);
1606 next = buf;
1607 size = PAGE_SIZE;
1608 spin_lock_irqsave (&dev->lock, flags);
1609
1610 if (dev->driver)
1611 s = dev->driver->driver.name;
1612 else
1613 s = "(none)";
1614
1615 /* Main Control Registers */
1616 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1617 ", chiprev %04x, dma %s\n\n"
1618 "devinit %03x fifoctl %08x gadget '%s'\n"
1619 "pci irqenb0 %02x irqenb1 %08x "
1620 "irqstat0 %04x irqstat1 %08x\n",
1621 driver_name, dev->chiprev,
1622 use_dma
1623 ? (use_dma_chaining ? "chaining" : "enabled")
1624 : "disabled",
1625 readl (&dev->regs->devinit),
1626 readl (&dev->regs->fifoctl),
1627 s,
1628 readl (&dev->regs->pciirqenb0),
1629 readl (&dev->regs->pciirqenb1),
1630 readl (&dev->regs->irqstat0),
1631 readl (&dev->regs->irqstat1));
1632 size -= t;
1633 next += t;
1634
1635 /* USB Control Registers */
1636 t1 = readl (&dev->usb->usbctl);
1637 t2 = readl (&dev->usb->usbstat);
1638 if (t1 & (1 << VBUS_PIN)) {
1639 if (t2 & (1 << HIGH_SPEED))
1640 s = "high speed";
1641 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1642 s = "powered";
1643 else
1644 s = "full speed";
1645 /* full speed bit (6) not working?? */
1646 } else
1647 s = "not attached";
1648 t = scnprintf (next, size,
1649 "stdrsp %08x usbctl %08x usbstat %08x "
1650 "addr 0x%02x (%s)\n",
1651 readl (&dev->usb->stdrsp), t1, t2,
1652 readl (&dev->usb->ouraddr), s);
1653 size -= t;
1654 next += t;
1655
1656 /* PCI Master Control Registers */
1657
1658 /* DMA Control Registers */
1659
1660 /* Configurable EP Control Registers */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001661 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct net2280_ep *ep;
1663
1664 ep = &dev->ep [i];
1665 if (i && !ep->desc)
1666 continue;
1667
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001668 t1 = readl(&ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1670 t = scnprintf (next, size,
1671 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1672 "irqenb %02x\n",
1673 ep->ep.name, t1, t2,
1674 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1675 ? "NAK " : "",
1676 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1677 ? "hide " : "",
1678 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1679 ? "CRC " : "",
1680 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1681 ? "interrupt " : "",
1682 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1683 ? "status " : "",
1684 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1685 ? "NAKmode " : "",
1686 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1687 ? "DATA1 " : "DATA0 ",
1688 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1689 ? "HALT " : "",
1690 readl (&ep->regs->ep_irqenb));
1691 size -= t;
1692 next += t;
1693
1694 t = scnprintf (next, size,
1695 "\tstat %08x avail %04x "
1696 "(ep%d%s-%s)%s\n",
1697 readl (&ep->regs->ep_stat),
1698 readl (&ep->regs->ep_avail),
1699 t1 & 0x0f, DIR_STRING (t1),
1700 type_string (t1 >> 8),
1701 ep->stopped ? "*" : "");
1702 size -= t;
1703 next += t;
1704
1705 if (!ep->dma)
1706 continue;
1707
1708 t = scnprintf (next, size,
1709 " dma\tctl %08x stat %08x count %08x\n"
1710 "\taddr %08x desc %08x\n",
1711 readl (&ep->dma->dmactl),
1712 readl (&ep->dma->dmastat),
1713 readl (&ep->dma->dmacount),
1714 readl (&ep->dma->dmaaddr),
1715 readl (&ep->dma->dmadesc));
1716 size -= t;
1717 next += t;
1718
1719 }
1720
1721 /* Indexed Registers */
David Brownell901b3d72006-09-02 03:13:45 -07001722 // none yet
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* Statistics */
1725 t = scnprintf (next, size, "\nirqs: ");
1726 size -= t;
1727 next += t;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001728 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 struct net2280_ep *ep;
1730
1731 ep = &dev->ep [i];
1732 if (i && !ep->irqs)
1733 continue;
1734 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1735 size -= t;
1736 next += t;
1737
1738 }
1739 t = scnprintf (next, size, "\n");
1740 size -= t;
1741 next += t;
1742
1743 spin_unlock_irqrestore (&dev->lock, flags);
1744
1745 return PAGE_SIZE - size;
1746}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001747static DEVICE_ATTR_RO(registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001749static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1750 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 struct net2280 *dev;
1753 char *next;
1754 unsigned size;
1755 unsigned long flags;
1756 int i;
1757
1758 dev = dev_get_drvdata (_dev);
1759 next = buf;
1760 size = PAGE_SIZE;
1761 spin_lock_irqsave (&dev->lock, flags);
1762
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001763 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 struct net2280_ep *ep = &dev->ep [i];
1765 struct net2280_request *req;
1766 int t;
1767
1768 if (i != 0) {
1769 const struct usb_endpoint_descriptor *d;
1770
1771 d = ep->desc;
1772 if (!d)
1773 continue;
1774 t = d->bEndpointAddress;
1775 t = scnprintf (next, size,
1776 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1777 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1778 (t & USB_DIR_IN) ? "in" : "out",
1779 ({ char *val;
1780 switch (d->bmAttributes & 0x03) {
1781 case USB_ENDPOINT_XFER_BULK:
David Brownell901b3d72006-09-02 03:13:45 -07001782 val = "bulk"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 case USB_ENDPOINT_XFER_INT:
David Brownell901b3d72006-09-02 03:13:45 -07001784 val = "intr"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 default:
David Brownell901b3d72006-09-02 03:13:45 -07001786 val = "iso"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001787 } val; }),
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001788 usb_endpoint_maxp (d) & 0x1fff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ep->dma ? "dma" : "pio", ep->fifo_size
1790 );
1791 } else /* ep0 should only have one transfer queued */
1792 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1793 ep->is_in ? "in" : "out");
1794 if (t <= 0 || t > size)
1795 goto done;
1796 size -= t;
1797 next += t;
1798
1799 if (list_empty (&ep->queue)) {
1800 t = scnprintf (next, size, "\t(nothing queued)\n");
1801 if (t <= 0 || t > size)
1802 goto done;
1803 size -= t;
1804 next += t;
1805 continue;
1806 }
1807 list_for_each_entry (req, &ep->queue, queue) {
1808 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1809 t = scnprintf (next, size,
1810 "\treq %p len %d/%d "
1811 "buf %p (dmacount %08x)\n",
1812 &req->req, req->req.actual,
1813 req->req.length, req->req.buf,
1814 readl (&ep->dma->dmacount));
1815 else
1816 t = scnprintf (next, size,
1817 "\treq %p len %d/%d buf %p\n",
1818 &req->req, req->req.actual,
1819 req->req.length, req->req.buf);
1820 if (t <= 0 || t > size)
1821 goto done;
1822 size -= t;
1823 next += t;
1824
1825 if (ep->dma) {
1826 struct net2280_dma *td;
1827
1828 td = req->td;
1829 t = scnprintf (next, size, "\t td %08x "
1830 " count %08x buf %08x desc %08x\n",
1831 (u32) req->td_dma,
1832 le32_to_cpu (td->dmacount),
1833 le32_to_cpu (td->dmaaddr),
1834 le32_to_cpu (td->dmadesc));
1835 if (t <= 0 || t > size)
1836 goto done;
1837 size -= t;
1838 next += t;
1839 }
1840 }
1841 }
1842
1843done:
1844 spin_unlock_irqrestore (&dev->lock, flags);
1845 return PAGE_SIZE - size;
1846}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001847static DEVICE_ATTR_RO(queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849
1850#else
1851
Linus Torvalds99504212006-10-17 18:03:33 -07001852#define device_create_file(a,b) (0)
1853#define device_remove_file(a,b) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855#endif
1856
1857/*-------------------------------------------------------------------------*/
1858
1859/* another driver-specific mode might be a request type doing dma
1860 * to/from another device fifo instead of to/from memory.
1861 */
1862
1863static void set_fifo_mode (struct net2280 *dev, int mode)
1864{
1865 /* keeping high bits preserves BAR2 */
1866 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1867
1868 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1869 INIT_LIST_HEAD (&dev->gadget.ep_list);
1870 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1871 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1872 switch (mode) {
1873 case 0:
1874 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1875 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1876 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1877 break;
1878 case 1:
1879 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1880 break;
1881 case 2:
1882 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1883 dev->ep [1].fifo_size = 2048;
1884 dev->ep [2].fifo_size = 1024;
1885 break;
1886 }
1887 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1888 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1889 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1890}
1891
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001892static void defect7374_disable_data_eps(struct net2280 *dev)
1893{
1894 /*
1895 * For Defect 7374, disable data EPs (and more):
1896 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1897 * returing ep regs back to normal.
1898 */
1899 struct net2280_ep *ep;
1900 int i;
1901 unsigned char ep_sel;
1902 u32 tmp_reg;
1903
1904 for (i = 1; i < 5; i++) {
1905 ep = &dev->ep[i];
1906 writel(0, &ep->cfg->ep_cfg);
1907 }
1908
1909 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1910 for (i = 0; i < 6; i++)
1911 writel(0, &dev->dep[i].dep_cfg);
1912
1913 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1914 /* Select an endpoint for subsequent operations: */
1915 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1916 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1917
1918 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1919 ep_sel == 18 || ep_sel == 20)
1920 continue;
1921
1922 /* Change settings on some selected endpoints */
1923 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
1924 tmp_reg &= ~(1 << NON_CTRL_IN_TOLERATE_BAD_DIR);
1925 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1926 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1927 tmp_reg |= (1 << EP_INITIALIZED);
1928 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1929 }
1930}
1931
1932static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1933{
1934 u32 tmp = 0, tmp_reg;
1935 u32 fsmvalue, scratch;
1936 int i;
1937 unsigned char ep_sel;
1938
1939 scratch = get_idx_reg(dev->regs, SCRATCH);
1940 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
1941 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1942
1943 /*See if firmware needs to set up for workaround*/
1944 if (fsmvalue != DEFECT7374_FSM_SS_CONTROL_READ) {
1945 WARNING(dev, "Operate Defect 7374 workaround soft this time");
1946 WARNING(dev, "It will operate on cold-reboot and SS connect");
1947
1948 /*GPEPs:*/
1949 tmp = ((0 << ENDPOINT_NUMBER) | (1 << ENDPOINT_DIRECTION) |
1950 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1951 ((dev->enhanced_mode) ?
1952 1 << OUT_ENDPOINT_ENABLE : 1 << ENDPOINT_ENABLE) |
1953 (1 << IN_ENDPOINT_ENABLE));
1954
1955 for (i = 1; i < 5; i++)
1956 writel(tmp, &dev->ep[i].cfg->ep_cfg);
1957
1958 /* CSRIN, PCIIN, STATIN, RCIN*/
1959 tmp = ((0 << ENDPOINT_NUMBER) | (1 << ENDPOINT_ENABLE));
1960 writel(tmp, &dev->dep[1].dep_cfg);
1961 writel(tmp, &dev->dep[3].dep_cfg);
1962 writel(tmp, &dev->dep[4].dep_cfg);
1963 writel(tmp, &dev->dep[5].dep_cfg);
1964
1965 /*Implemented for development and debug.
1966 * Can be refined/tuned later.*/
1967 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1968 /* Select an endpoint for subsequent operations: */
1969 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1970 writel(((tmp_reg & ~0x1f) | ep_sel),
1971 &dev->plregs->pl_ep_ctrl);
1972
1973 if (ep_sel == 1) {
1974 tmp =
1975 (readl(&dev->plregs->pl_ep_ctrl) |
1976 (1 << CLEAR_ACK_ERROR_CODE) | 0);
1977 writel(tmp, &dev->plregs->pl_ep_ctrl);
1978 continue;
1979 }
1980
1981 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1982 ep_sel == 18 || ep_sel == 20)
1983 continue;
1984
1985 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1986 (1 << NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1987 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1988
1989 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1990 ~(1 << EP_INITIALIZED);
1991 writel(tmp, &dev->plregs->pl_ep_ctrl);
1992
1993 }
1994
1995 /* Set FSM to focus on the first Control Read:
1996 * - Tip: Connection speed is known upon the first
1997 * setup request.*/
1998 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1999 set_idx_reg(dev->regs, SCRATCH, scratch);
2000
2001 } else{
2002 WARNING(dev, "Defect 7374 workaround soft will NOT operate");
2003 WARNING(dev, "It will operate on cold-reboot and SS connect");
2004 }
2005}
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007/* keeping it simple:
2008 * - one bus driver, initted first;
2009 * - one function driver, initted second
2010 *
2011 * most of the work to support multiple net2280 controllers would
2012 * be to associate this gadget driver (yes?) with all of them, or
2013 * perhaps to bind specific drivers to specific devices.
2014 */
2015
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002016static void usb_reset_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 u32 tmp;
2019
2020 dev->gadget.speed = USB_SPEED_UNKNOWN;
2021 (void) readl (&dev->usb->usbctl);
2022
2023 net2280_led_init (dev);
2024
2025 /* disable automatic responses, and irqs */
2026 writel (0, &dev->usb->stdrsp);
2027 writel (0, &dev->regs->pciirqenb0);
2028 writel (0, &dev->regs->pciirqenb1);
2029
2030 /* clear old dma and irq state */
2031 for (tmp = 0; tmp < 4; tmp++) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002032 struct net2280_ep *ep = &dev->ep[tmp + 1];
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07002033 if (ep->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002034 abort_dma(ep);
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07002035 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 writel (~0, &dev->regs->irqstat0),
2038 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
2039
2040 /* reset, and enable pci */
2041 tmp = readl (&dev->regs->devinit)
2042 | (1 << PCI_ENABLE)
2043 | (1 << FIFO_SOFT_RESET)
2044 | (1 << USB_SOFT_RESET)
2045 | (1 << M8051_RESET);
2046 writel (tmp, &dev->regs->devinit);
2047
2048 /* standard fifo and endpoint allocations */
2049 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
2050}
2051
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002052static void usb_reset_338x(struct net2280 *dev)
2053{
2054 u32 tmp;
2055 u32 fsmvalue;
2056
2057 dev->gadget.speed = USB_SPEED_UNKNOWN;
2058 (void)readl(&dev->usb->usbctl);
2059
2060 net2280_led_init(dev);
2061
2062 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
2063 (0xf << DEFECT7374_FSM_FIELD);
2064
2065 /* See if firmware needs to set up for workaround: */
2066 if (fsmvalue != DEFECT7374_FSM_SS_CONTROL_READ) {
2067 INFO(dev, "%s: Defect 7374 FsmValue 0x%08x\n", __func__,
2068 fsmvalue);
2069 } else {
2070 /* disable automatic responses, and irqs */
2071 writel(0, &dev->usb->stdrsp);
2072 writel(0, &dev->regs->pciirqenb0);
2073 writel(0, &dev->regs->pciirqenb1);
2074 }
2075
2076 /* clear old dma and irq state */
2077 for (tmp = 0; tmp < 4; tmp++) {
2078 struct net2280_ep *ep = &dev->ep[tmp + 1];
2079
2080 if (ep->dma)
2081 abort_dma(ep);
2082 }
2083
2084 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
2085
2086 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
2087 /* reset, and enable pci */
2088 tmp = readl(&dev->regs->devinit) |
2089 (1 << PCI_ENABLE) |
2090 (1 << FIFO_SOFT_RESET) |
2091 (1 << USB_SOFT_RESET) |
2092 (1 << M8051_RESET);
2093
2094 writel(tmp, &dev->regs->devinit);
2095 }
2096
2097 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2098 INIT_LIST_HEAD(&dev->gadget.ep_list);
2099
2100 for (tmp = 1; tmp < dev->n_ep; tmp++)
2101 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2102
2103}
2104
2105static void usb_reset(struct net2280 *dev)
2106{
2107 if (dev->pdev->vendor == 0x17cc)
2108 return usb_reset_228x(dev);
2109 return usb_reset_338x(dev);
2110}
2111
2112static void usb_reinit_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 u32 tmp;
2115 int init_dma;
2116
2117 /* use_dma changes are ignored till next device re-init */
2118 init_dma = use_dma;
2119
2120 /* basic endpoint init */
2121 for (tmp = 0; tmp < 7; tmp++) {
2122 struct net2280_ep *ep = &dev->ep [tmp];
2123
2124 ep->ep.name = ep_name [tmp];
2125 ep->dev = dev;
2126 ep->num = tmp;
2127
2128 if (tmp > 0 && tmp <= 4) {
2129 ep->fifo_size = 1024;
2130 if (init_dma)
2131 ep->dma = &dev->dma [tmp - 1];
2132 } else
2133 ep->fifo_size = 64;
2134 ep->regs = &dev->epregs [tmp];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002135 ep->cfg = &dev->epregs[tmp];
2136 ep_reset_228x(dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
Robert Baldygae117e742013-12-13 12:23:38 +01002138 usb_ep_set_maxpacket_limit(&dev->ep [0].ep, 64);
2139 usb_ep_set_maxpacket_limit(&dev->ep [5].ep, 64);
2140 usb_ep_set_maxpacket_limit(&dev->ep [6].ep, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 dev->gadget.ep0 = &dev->ep [0].ep;
2143 dev->ep [0].stopped = 0;
2144 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
2145
2146 /* we want to prevent lowlevel/insecure access from the USB host,
2147 * but erratum 0119 means this enable bit is ignored
2148 */
2149 for (tmp = 0; tmp < 5; tmp++)
2150 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
2151}
2152
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002153static void usb_reinit_338x(struct net2280 *dev)
2154{
2155 int init_dma;
2156 int i;
2157 u32 tmp, val;
2158 u32 fsmvalue;
2159 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2160 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2161 0x00, 0xC0, 0x00, 0xC0 };
2162
2163 /* use_dma changes are ignored till next device re-init */
2164 init_dma = use_dma;
2165
2166 /* basic endpoint init */
2167 for (i = 0; i < dev->n_ep; i++) {
2168 struct net2280_ep *ep = &dev->ep[i];
2169
2170 ep->ep.name = ep_name[i];
2171 ep->dev = dev;
2172 ep->num = i;
2173
2174 if (i > 0 && i <= 4 && init_dma)
2175 ep->dma = &dev->dma[i - 1];
2176
2177 if (dev->enhanced_mode) {
2178 ep->cfg = &dev->epregs[ne[i]];
2179 ep->regs = (struct net2280_ep_regs __iomem *)
2180 (((void *)&dev->epregs[ne[i]]) +
2181 ep_reg_addr[i]);
2182 ep->fiforegs = &dev->fiforegs[i];
2183 } else {
2184 ep->cfg = &dev->epregs[i];
2185 ep->regs = &dev->epregs[i];
2186 ep->fiforegs = &dev->fiforegs[i];
2187 }
2188
2189 ep->fifo_size = (i != 0) ? 2048 : 512;
2190
2191 ep_reset_338x(dev->regs, ep);
2192 }
2193 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2194
2195 dev->gadget.ep0 = &dev->ep[0].ep;
2196 dev->ep[0].stopped = 0;
2197
2198 /* Link layer set up */
2199 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
2200 (0xf << DEFECT7374_FSM_FIELD);
2201
2202 /* See if driver needs to set up for workaround: */
2203 if (fsmvalue != DEFECT7374_FSM_SS_CONTROL_READ)
2204 INFO(dev, "%s: Defect 7374 FsmValue %08x\n",
2205 __func__, fsmvalue);
2206 else {
2207 tmp = readl(&dev->usb_ext->usbctl2) &
2208 ~((1 << U1_ENABLE) | (1 << U2_ENABLE) | (1 << LTM_ENABLE));
2209 writel(tmp, &dev->usb_ext->usbctl2);
2210 }
2211
2212 /* Hardware Defect and Workaround */
2213 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2214 val &= ~(0xf << TIMER_LFPS_6US);
2215 val |= 0x5 << TIMER_LFPS_6US;
2216 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2217
2218 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2219 val &= ~(0xffff << TIMER_LFPS_80US);
2220 val |= 0x0100 << TIMER_LFPS_80US;
2221 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2222
2223 /*
2224 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2225 * Hot Reset Exit Handshake may Fail in Specific Case using
2226 * Default Register Settings. Workaround for Enumeration test.
2227 */
2228 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2229 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2230 val |= 0x10 << HOT_TX_NORESET_TS2;
2231 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2232
2233 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2234 val &= ~(0x1f << HOT_RX_RESET_TS2);
2235 val |= 0x3 << HOT_RX_RESET_TS2;
2236 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2237
2238 /*
2239 * Set Recovery Idle to Recover bit:
2240 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2241 * link robustness with various hosts and hubs.
2242 * - It is safe to set for all connection speeds; all chip revisions.
2243 * - R-M-W to leave other bits undisturbed.
2244 * - Reference PLX TT-7372
2245 */
2246 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
2247 val |= (1 << RECOVERY_IDLE_TO_RECOVER_FMW);
2248 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2249
2250 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2251
2252 /* disable dedicated endpoints */
2253 writel(0x0D, &dev->dep[0].dep_cfg);
2254 writel(0x0D, &dev->dep[1].dep_cfg);
2255 writel(0x0E, &dev->dep[2].dep_cfg);
2256 writel(0x0E, &dev->dep[3].dep_cfg);
2257 writel(0x0F, &dev->dep[4].dep_cfg);
2258 writel(0x0C, &dev->dep[5].dep_cfg);
2259}
2260
2261static void usb_reinit(struct net2280 *dev)
2262{
2263 if (dev->pdev->vendor == 0x17cc)
2264 return usb_reinit_228x(dev);
2265 return usb_reinit_338x(dev);
2266}
2267
2268static void ep0_start_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
2271 | (1 << CLEAR_NAK_OUT_PACKETS)
2272 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
2273 , &dev->epregs [0].ep_rsp);
2274
2275 /*
2276 * hardware optionally handles a bunch of standard requests
2277 * that the API hides from drivers anyway. have it do so.
2278 * endpoint status/features are handled in software, to
2279 * help pass tests for some dubious behavior.
2280 */
2281 writel ( (1 << SET_TEST_MODE)
2282 | (1 << SET_ADDRESS)
2283 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
2284 | (1 << GET_DEVICE_STATUS)
2285 | (1 << GET_INTERFACE_STATUS)
2286 , &dev->usb->stdrsp);
2287 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
2288 | (1 << SELF_POWERED_USB_DEVICE)
2289 | (1 << REMOTE_WAKEUP_SUPPORT)
2290 | (dev->softconnect << USB_DETECT_ENABLE)
2291 | (1 << SELF_POWERED_STATUS)
2292 , &dev->usb->usbctl);
2293
2294 /* enable irqs so we can see ep0 and general operation */
2295 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
2296 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
2297 , &dev->regs->pciirqenb0);
2298 writel ( (1 << PCI_INTERRUPT_ENABLE)
2299 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
2300 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
2301 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
2302 | (1 << VBUS_INTERRUPT_ENABLE)
2303 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
2304 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
2305 , &dev->regs->pciirqenb1);
2306
2307 /* don't leave any writes posted */
2308 (void) readl (&dev->usb->usbctl);
2309}
2310
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002311static void ep0_start_338x(struct net2280 *dev)
2312{
2313 u32 fsmvalue;
2314
2315 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
2316 (0xf << DEFECT7374_FSM_FIELD);
2317
2318 if (fsmvalue != DEFECT7374_FSM_SS_CONTROL_READ)
2319 INFO(dev, "%s: Defect 7374 FsmValue %08x\n", __func__,
2320 fsmvalue);
2321 else
2322 writel((1 << CLEAR_NAK_OUT_PACKETS_MODE) |
2323 (1 << SET_EP_HIDE_STATUS_PHASE),
2324 &dev->epregs[0].ep_rsp);
2325
2326 /*
2327 * hardware optionally handles a bunch of standard requests
2328 * that the API hides from drivers anyway. have it do so.
2329 * endpoint status/features are handled in software, to
2330 * help pass tests for some dubious behavior.
2331 */
2332 writel((1 << SET_ISOCHRONOUS_DELAY) |
2333 (1 << SET_SEL) |
2334 (1 << SET_TEST_MODE) |
2335 (1 << SET_ADDRESS) |
2336 (1 << GET_INTERFACE_STATUS) |
2337 (1 << GET_DEVICE_STATUS),
2338 &dev->usb->stdrsp);
2339 dev->wakeup_enable = 1;
2340 writel((1 << USB_ROOT_PORT_WAKEUP_ENABLE) |
2341 (dev->softconnect << USB_DETECT_ENABLE) |
2342 (1 << DEVICE_REMOTE_WAKEUP_ENABLE),
2343 &dev->usb->usbctl);
2344
2345 /* enable irqs so we can see ep0 and general operation */
2346 writel((1 << SETUP_PACKET_INTERRUPT_ENABLE) |
2347 (1 << ENDPOINT_0_INTERRUPT_ENABLE)
2348 , &dev->regs->pciirqenb0);
2349 writel((1 << PCI_INTERRUPT_ENABLE) |
2350 (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2351 (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2352 (1 << VBUS_INTERRUPT_ENABLE),
2353 &dev->regs->pciirqenb1);
2354
2355 /* don't leave any writes posted */
2356 (void)readl(&dev->usb->usbctl);
2357}
2358
2359static void ep0_start(struct net2280 *dev)
2360{
2361 if (dev->pdev->vendor == 0x17cc)
2362 return ep0_start_228x(dev);
2363 return ep0_start_338x(dev);
2364}
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366/* when a driver is successfully registered, it will receive
2367 * control requests including set_configuration(), which enables
2368 * non-control requests. then usb traffic follows until a
2369 * disconnect is reported. then a host may connect again, or
2370 * the driver might get unbound.
2371 */
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002372static int net2280_start(struct usb_gadget *_gadget,
2373 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002375 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 int retval;
2377 unsigned i;
2378
2379 /* insist on high speed support from the driver, since
2380 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2381 * "must not be used in normal operation"
2382 */
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002383 if (!driver || driver->max_speed < USB_SPEED_HIGH
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002384 || !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return -EINVAL;
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002386
2387 dev = container_of (_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002389 for (i = 0; i < dev->n_ep; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 dev->ep [i].irqs = 0;
2391
2392 /* hook up the driver ... */
2393 dev->softconnect = 1;
2394 driver->driver.bus = NULL;
2395 dev->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Jeff Garzikb3899da2006-10-11 21:50:24 -04002397 retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
2398 if (retval) goto err_unbind;
2399 retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
2400 if (retval) goto err_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Alan Stern2f076072013-01-30 16:40:14 -05002402 /* Enable force-full-speed testing mode, if desired */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002403 if (full_speed && dev->pdev->vendor == 0x17cc)
Alan Stern2f076072013-01-30 16:40:14 -05002404 writel(1 << FORCE_FULL_SPEED_MODE, &dev->usb->xcvrdiag);
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* ... then enable host detection and ep0; and we're ready
2407 * for set_configuration as well as eventual disconnect.
2408 */
2409 net2280_led_active (dev, 1);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002410
2411 if (dev->pdev->vendor == 0x10b5)
2412 defect7374_enable_data_eps_zero(dev);
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 ep0_start (dev);
2415
2416 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2417 driver->driver.name,
2418 readl (&dev->usb->usbctl),
2419 readl (&dev->usb->stdrsp));
2420
2421 /* pci writes may still be posted */
2422 return 0;
Jeff Garzikb3899da2006-10-11 21:50:24 -04002423
2424err_func:
2425 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2426err_unbind:
Jeff Garzikb3899da2006-10-11 21:50:24 -04002427 dev->driver = NULL;
2428 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431static void
2432stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2433{
2434 int i;
2435
2436 /* don't disconnect if it's not connected */
2437 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2438 driver = NULL;
2439
2440 /* stop hardware; prevent new request submissions;
2441 * and kill any outstanding requests.
2442 */
2443 usb_reset (dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002444 for (i = 0; i < dev->n_ep; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 nuke (&dev->ep [i]);
2446
Felipe Balbi699412d2013-03-18 10:14:47 +02002447 /* report disconnect; the driver is already quiesced */
2448 if (driver) {
2449 spin_unlock(&dev->lock);
2450 driver->disconnect(&dev->gadget);
2451 spin_lock(&dev->lock);
2452 }
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 usb_reinit (dev);
2455}
2456
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002457static int net2280_stop(struct usb_gadget *_gadget,
2458 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002460 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 unsigned long flags;
2462
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002463 dev = container_of (_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 spin_lock_irqsave (&dev->lock, flags);
2466 stop_activity (dev, driver);
2467 spin_unlock_irqrestore (&dev->lock, flags);
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 dev->driver = NULL;
2470
2471 net2280_led_active (dev, 0);
Alan Stern2f076072013-01-30 16:40:14 -05002472
2473 /* Disable full-speed test mode */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002474 if (dev->pdev->vendor == 0x17cc)
2475 writel(0, &dev->usb->xcvrdiag);
Alan Stern2f076072013-01-30 16:40:14 -05002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2478 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2479
Ricardo Ribalda Delgado84237bf2014-05-15 14:28:45 +02002480 DEBUG(dev, "unregistered driver '%s'\n",
2481 driver ? driver->driver.name : "");
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return 0;
2484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486/*-------------------------------------------------------------------------*/
2487
2488/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2489 * also works for dma-capable endpoints, in pio mode or just
2490 * to manually advance the queue after short OUT transfers.
2491 */
2492static void handle_ep_small (struct net2280_ep *ep)
2493{
2494 struct net2280_request *req;
2495 u32 t;
2496 /* 0 error, 1 mid-data, 2 done */
2497 int mode = 1;
2498
2499 if (!list_empty (&ep->queue))
2500 req = list_entry (ep->queue.next,
2501 struct net2280_request, queue);
2502 else
2503 req = NULL;
2504
2505 /* ack all, and handle what we care about */
2506 t = readl (&ep->regs->ep_stat);
2507 ep->irqs++;
2508#if 0
2509 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2510 ep->ep.name, t, req ? &req->req : 0);
2511#endif
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002512 if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2513 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2514 else
2515 /* Added for 2282 */
2516 writel (t, &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 /* for ep0, monitor token irqs to catch data stage length errors
2519 * and to synchronize on status.
2520 *
2521 * also, to defer reporting of protocol stalls ... here's where
2522 * data or status first appears, handling stalls here should never
2523 * cause trouble on the host side..
2524 *
2525 * control requests could be slightly faster without token synch for
2526 * status, but status can jam up that way.
2527 */
2528 if (unlikely (ep->num == 0)) {
2529 if (ep->is_in) {
2530 /* status; stop NAKing */
2531 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2532 if (ep->dev->protocol_stall) {
2533 ep->stopped = 1;
2534 set_halt (ep);
2535 }
2536 if (!req)
2537 allow_status (ep);
2538 mode = 2;
2539 /* reply to extra IN data tokens with a zlp */
2540 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2541 if (ep->dev->protocol_stall) {
2542 ep->stopped = 1;
2543 set_halt (ep);
2544 mode = 2;
Alan Stern1f26e282006-11-16 10:16:00 -05002545 } else if (ep->responded &&
2546 !req && !ep->stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 write_fifo (ep, NULL);
2548 }
2549 } else {
2550 /* status; stop NAKing */
2551 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2552 if (ep->dev->protocol_stall) {
2553 ep->stopped = 1;
2554 set_halt (ep);
2555 }
2556 mode = 2;
2557 /* an extra OUT token is an error */
2558 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2559 && req
2560 && req->req.actual == req->req.length)
Alan Stern1f26e282006-11-16 10:16:00 -05002561 || (ep->responded && !req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 ep->dev->protocol_stall = 1;
2563 set_halt (ep);
2564 ep->stopped = 1;
2565 if (req)
2566 done (ep, req, -EOVERFLOW);
2567 req = NULL;
2568 }
2569 }
2570 }
2571
2572 if (unlikely (!req))
2573 return;
2574
2575 /* manual DMA queue advance after short OUT */
Felipe Balbiad303db2013-03-22 16:47:52 +02002576 if (likely (ep->dma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2578 u32 count;
2579 int stopped = ep->stopped;
2580
2581 /* TRANSFERRED works around OUT_DONE erratum 0112.
2582 * we expect (N <= maxpacket) bytes; host wrote M.
2583 * iff (M < N) we won't ever see a DMA interrupt.
2584 */
2585 ep->stopped = 1;
2586 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2587
2588 /* any preceding dma transfers must finish.
2589 * dma handles (M >= N), may empty the queue
2590 */
2591 scan_dma_completions (ep);
2592 if (unlikely (list_empty (&ep->queue)
2593 || ep->out_overflow)) {
2594 req = NULL;
2595 break;
2596 }
2597 req = list_entry (ep->queue.next,
2598 struct net2280_request, queue);
2599
2600 /* here either (M < N), a "real" short rx;
2601 * or (M == N) and the queue didn't empty
2602 */
2603 if (likely (t & (1 << FIFO_EMPTY))) {
2604 count = readl (&ep->dma->dmacount);
2605 count &= DMA_BYTE_COUNT_MASK;
2606 if (readl (&ep->dma->dmadesc)
2607 != req->td_dma)
2608 req = NULL;
2609 break;
2610 }
2611 udelay(1);
2612 }
2613
2614 /* stop DMA, leave ep NAKing */
2615 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2616 spin_stop_dma (ep->dma);
2617
2618 if (likely (req)) {
2619 req->td->dmacount = 0;
2620 t = readl (&ep->regs->ep_avail);
David Brownell68dcc682006-04-02 10:18:53 -08002621 dma_done (ep, req, count,
David Brownell901b3d72006-09-02 03:13:45 -07002622 (ep->out_overflow || t)
2623 ? -EOVERFLOW : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
2625
2626 /* also flush to prevent erratum 0106 trouble */
2627 if (unlikely (ep->out_overflow
2628 || (ep->dev->chiprev == 0x0100
2629 && ep->dev->gadget.speed
2630 == USB_SPEED_FULL))) {
2631 out_flush (ep);
2632 ep->out_overflow = 0;
2633 }
2634
2635 /* (re)start dma if needed, stop NAKing */
2636 ep->stopped = stopped;
2637 if (!list_empty (&ep->queue))
2638 restart_dma (ep);
2639 } else
2640 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2641 ep->ep.name, t);
2642 return;
2643
2644 /* data packet(s) received (in the fifo, OUT) */
2645 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2646 if (read_fifo (ep, req) && ep->num != 0)
2647 mode = 2;
2648
2649 /* data packet(s) transmitted (IN) */
2650 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2651 unsigned len;
2652
2653 len = req->req.length - req->req.actual;
2654 if (len > ep->ep.maxpacket)
2655 len = ep->ep.maxpacket;
2656 req->req.actual += len;
2657
2658 /* if we wrote it all, we're usually done */
2659 if (req->req.actual == req->req.length) {
2660 if (ep->num == 0) {
Alan Stern317e83b2006-04-14 16:42:03 -04002661 /* send zlps until the status stage */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 } else if (!req->req.zero || len != ep->ep.maxpacket)
2663 mode = 2;
2664 }
2665
2666 /* there was nothing to do ... */
2667 } else if (mode == 1)
2668 return;
2669
2670 /* done */
2671 if (mode == 2) {
2672 /* stream endpoints often resubmit/unlink in completion */
2673 done (ep, req, 0);
2674
2675 /* maybe advance queue to next request */
2676 if (ep->num == 0) {
2677 /* NOTE: net2280 could let gadget driver start the
2678 * status stage later. since not all controllers let
2679 * them control that, the api doesn't (yet) allow it.
2680 */
2681 if (!ep->stopped)
2682 allow_status (ep);
2683 req = NULL;
2684 } else {
2685 if (!list_empty (&ep->queue) && !ep->stopped)
2686 req = list_entry (ep->queue.next,
2687 struct net2280_request, queue);
2688 else
2689 req = NULL;
2690 if (req && !ep->is_in)
2691 stop_out_naking (ep);
2692 }
2693 }
2694
2695 /* is there a buffer for the next packet?
2696 * for best streaming performance, make sure there is one.
2697 */
2698 if (req && !ep->stopped) {
2699
2700 /* load IN fifo with next packet (may be zlp) */
2701 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2702 write_fifo (ep, &req->req);
2703 }
2704}
2705
2706static struct net2280_ep *
2707get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2708{
2709 struct net2280_ep *ep;
2710
2711 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2712 return &dev->ep [0];
2713 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2714 u8 bEndpointAddress;
2715
2716 if (!ep->desc)
2717 continue;
2718 bEndpointAddress = ep->desc->bEndpointAddress;
2719 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2720 continue;
2721 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2722 return ep;
2723 }
2724 return NULL;
2725}
2726
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002727static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2728{
2729 u32 scratch, fsmvalue;
2730 u32 ack_wait_timeout, state;
2731
2732 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2733 scratch = get_idx_reg(dev->regs, SCRATCH);
2734 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2735 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2736
2737 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2738 (r.bRequestType & USB_DIR_IN)))
2739 return;
2740
2741 /* This is the first Control Read for this connection: */
2742 if (!(readl(&dev->usb->usbstat) & (1 << SUPER_SPEED_MODE))) {
2743 /*
2744 * Connection is NOT SS:
2745 * - Connection must be FS or HS.
2746 * - This FSM state should allow workaround software to
2747 * run after the next USB connection.
2748 */
2749 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
2750 goto restore_data_eps;
2751 }
2752
2753 /* Connection is SS: */
2754 for (ack_wait_timeout = 0;
2755 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2756 ack_wait_timeout++) {
2757
2758 state = readl(&dev->plregs->pl_ep_status_1)
2759 & (0xff << STATE);
2760 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2761 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2762 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
2763 break;
2764 }
2765
2766 /*
2767 * We have not yet received host's Data Phase ACK
2768 * - Wait and try again.
2769 */
2770 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2771
2772 continue;
2773 }
2774
2775
2776 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
2777 ERROR(dev, "FAIL: Defect 7374 workaround waited but failed "
2778 "to detect SS host's data phase ACK.");
2779 ERROR(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
2780 "got 0x%2.2x.\n", state >> STATE);
2781 } else {
2782 WARNING(dev, "INFO: Defect 7374 workaround waited about\n"
2783 "%duSec for Control Read Data Phase ACK\n",
2784 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2785 }
2786
2787restore_data_eps:
2788 /*
2789 * Restore data EPs to their pre-workaround settings (disabled,
2790 * initialized, and other details).
2791 */
2792 defect7374_disable_data_eps(dev);
2793
2794 set_idx_reg(dev->regs, SCRATCH, scratch);
2795
2796 return;
2797}
2798
2799static void ep_stall(struct net2280_ep *ep, int stall)
2800{
2801 struct net2280 *dev = ep->dev;
2802 u32 val;
2803 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2804
2805 if (stall) {
2806 writel((1 << SET_ENDPOINT_HALT) |
2807 /* (1 << SET_NAK_PACKETS) | */
2808 (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2809 &ep->regs->ep_rsp);
2810 ep->is_halt = 1;
2811 } else {
2812 if (dev->gadget.speed == USB_SPEED_SUPER) {
2813 /*
2814 * Workaround for SS SeqNum not cleared via
2815 * Endpoint Halt (Clear) bit. select endpoint
2816 */
2817 val = readl(&dev->plregs->pl_ep_ctrl);
2818 val = (val & ~0x1f) | ep_pl[ep->num];
2819 writel(val, &dev->plregs->pl_ep_ctrl);
2820
2821 val |= (1 << SEQUENCE_NUMBER_RESET);
2822 writel(val, &dev->plregs->pl_ep_ctrl);
2823 }
2824 val = readl(&ep->regs->ep_rsp);
2825 val |= (1 << CLEAR_ENDPOINT_HALT) |
2826 (1 << CLEAR_ENDPOINT_TOGGLE);
2827 writel(val
2828 /* | (1 << CLEAR_NAK_PACKETS)*/
2829 , &ep->regs->ep_rsp);
2830 ep->is_halt = 0;
2831 val = readl(&ep->regs->ep_rsp);
2832 }
2833}
2834
2835static void ep_stdrsp(struct net2280_ep *ep, int value, int wedged)
2836{
2837 /* set/clear, then synch memory views with the device */
2838 if (value) {
2839 ep->stopped = 1;
2840 if (ep->num == 0)
2841 ep->dev->protocol_stall = 1;
2842 else {
2843 if (ep->dma)
2844 ep_stop_dma(ep);
2845 ep_stall(ep, true);
2846 }
2847
2848 if (wedged)
2849 ep->wedged = 1;
2850 } else {
2851 ep->stopped = 0;
2852 ep->wedged = 0;
2853
2854 ep_stall(ep, false);
2855
2856 /* Flush the queue */
2857 if (!list_empty(&ep->queue)) {
2858 struct net2280_request *req =
2859 list_entry(ep->queue.next, struct net2280_request,
2860 queue);
2861 if (ep->dma)
2862 resume_dma(ep);
2863 else {
2864 if (ep->is_in)
2865 write_fifo(ep, &req->req);
2866 else {
2867 if (read_fifo(ep, req))
2868 done(ep, req, 0);
2869 }
2870 }
2871 }
2872 }
2873}
2874
2875static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2876 struct net2280_ep *ep, struct usb_ctrlrequest r)
2877{
2878 int tmp = 0;
2879
2880#define w_value le16_to_cpu(r.wValue)
2881#define w_index le16_to_cpu(r.wIndex)
2882#define w_length le16_to_cpu(r.wLength)
2883
2884 switch (r.bRequest) {
2885 struct net2280_ep *e;
2886 u16 status;
2887
2888 case USB_REQ_SET_CONFIGURATION:
2889 dev->addressed_state = !w_value;
2890 goto usb3_delegate;
2891
2892 case USB_REQ_GET_STATUS:
2893 switch (r.bRequestType) {
2894 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2895 status = dev->wakeup_enable ? 0x02 : 0x00;
2896 if (dev->selfpowered)
2897 status |= 1 << 0;
2898 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2899 dev->ltm_enable << 4);
2900 writel(0, &dev->epregs[0].ep_irqenb);
2901 set_fifo_bytecount(ep, sizeof(status));
2902 writel((__force u32) status, &dev->epregs[0].ep_data);
2903 allow_status_338x(ep);
2904 break;
2905
2906 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2907 e = get_ep_by_addr(dev, w_index);
2908 if (!e)
2909 goto do_stall3;
2910 status = readl(&e->regs->ep_rsp) &
2911 (1 << CLEAR_ENDPOINT_HALT);
2912 writel(0, &dev->epregs[0].ep_irqenb);
2913 set_fifo_bytecount(ep, sizeof(status));
2914 writel((__force u32) status, &dev->epregs[0].ep_data);
2915 allow_status_338x(ep);
2916 break;
2917
2918 default:
2919 goto usb3_delegate;
2920 }
2921 break;
2922
2923 case USB_REQ_CLEAR_FEATURE:
2924 switch (r.bRequestType) {
2925 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2926 if (!dev->addressed_state) {
2927 switch (w_value) {
2928 case USB_DEVICE_U1_ENABLE:
2929 dev->u1_enable = 0;
2930 writel(readl(&dev->usb_ext->usbctl2) &
2931 ~(1 << U1_ENABLE),
2932 &dev->usb_ext->usbctl2);
2933 allow_status_338x(ep);
2934 goto next_endpoints3;
2935
2936 case USB_DEVICE_U2_ENABLE:
2937 dev->u2_enable = 0;
2938 writel(readl(&dev->usb_ext->usbctl2) &
2939 ~(1 << U2_ENABLE),
2940 &dev->usb_ext->usbctl2);
2941 allow_status_338x(ep);
2942 goto next_endpoints3;
2943
2944 case USB_DEVICE_LTM_ENABLE:
2945 dev->ltm_enable = 0;
2946 writel(readl(&dev->usb_ext->usbctl2) &
2947 ~(1 << LTM_ENABLE),
2948 &dev->usb_ext->usbctl2);
2949 allow_status_338x(ep);
2950 goto next_endpoints3;
2951
2952 default:
2953 break;
2954 }
2955 }
2956 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2957 dev->wakeup_enable = 0;
2958 writel(readl(&dev->usb->usbctl) &
2959 ~(1 << DEVICE_REMOTE_WAKEUP_ENABLE),
2960 &dev->usb->usbctl);
2961 allow_status_338x(ep);
2962 break;
2963 }
2964 goto usb3_delegate;
2965
2966 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2967 e = get_ep_by_addr(dev, w_index);
2968 if (!e)
2969 goto do_stall3;
2970 if (w_value != USB_ENDPOINT_HALT)
2971 goto do_stall3;
2972 VDEBUG(dev, "%s clear halt\n", e->ep.name);
2973 ep_stall(e, false);
2974 if (!list_empty(&e->queue) && e->td_dma)
2975 restart_dma(e);
2976 allow_status(ep);
2977 ep->stopped = 1;
2978 break;
2979
2980 default:
2981 goto usb3_delegate;
2982 }
2983 break;
2984 case USB_REQ_SET_FEATURE:
2985 switch (r.bRequestType) {
2986 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2987 if (!dev->addressed_state) {
2988 switch (w_value) {
2989 case USB_DEVICE_U1_ENABLE:
2990 dev->u1_enable = 1;
2991 writel(readl(&dev->usb_ext->usbctl2) |
2992 (1 << U1_ENABLE),
2993 &dev->usb_ext->usbctl2);
2994 allow_status_338x(ep);
2995 goto next_endpoints3;
2996
2997 case USB_DEVICE_U2_ENABLE:
2998 dev->u2_enable = 1;
2999 writel(readl(&dev->usb_ext->usbctl2) |
3000 (1 << U2_ENABLE),
3001 &dev->usb_ext->usbctl2);
3002 allow_status_338x(ep);
3003 goto next_endpoints3;
3004
3005 case USB_DEVICE_LTM_ENABLE:
3006 dev->ltm_enable = 1;
3007 writel(readl(&dev->usb_ext->usbctl2) |
3008 (1 << LTM_ENABLE),
3009 &dev->usb_ext->usbctl2);
3010 allow_status_338x(ep);
3011 goto next_endpoints3;
3012 default:
3013 break;
3014 }
3015 }
3016
3017 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
3018 dev->wakeup_enable = 1;
3019 writel(readl(&dev->usb->usbctl) |
3020 (1 << DEVICE_REMOTE_WAKEUP_ENABLE),
3021 &dev->usb->usbctl);
3022 allow_status_338x(ep);
3023 break;
3024 }
3025 goto usb3_delegate;
3026
3027 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
3028 e = get_ep_by_addr(dev, w_index);
3029 if (!e || (w_value != USB_ENDPOINT_HALT))
3030 goto do_stall3;
3031 ep_stdrsp(e, true, false);
3032 allow_status_338x(ep);
3033 break;
3034
3035 default:
3036 goto usb3_delegate;
3037 }
3038
3039 break;
3040 default:
3041
3042usb3_delegate:
3043 VDEBUG(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
3044 r.bRequestType, r.bRequest,
3045 w_value, w_index, w_length,
3046 readl(&ep->cfg->ep_cfg));
3047
3048 ep->responded = 0;
3049 spin_unlock(&dev->lock);
3050 tmp = dev->driver->setup(&dev->gadget, &r);
3051 spin_lock(&dev->lock);
3052 }
3053do_stall3:
3054 if (tmp < 0) {
3055 VDEBUG(dev, "req %02x.%02x protocol STALL; stat %d\n",
3056 r.bRequestType, r.bRequest, tmp);
3057 dev->protocol_stall = 1;
3058 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
3059 ep_stall(ep, true);
3060 }
3061
3062next_endpoints3:
3063
3064#undef w_value
3065#undef w_index
3066#undef w_length
3067
3068 return;
3069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
3072{
3073 struct net2280_ep *ep;
3074 u32 num, scratch;
3075
3076 /* most of these don't need individual acks */
3077 stat &= ~(1 << INTA_ASSERTED);
3078 if (!stat)
3079 return;
3080 // DEBUG (dev, "irqstat0 %04x\n", stat);
3081
3082 /* starting a control request? */
3083 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
3084 union {
3085 u32 raw [2];
3086 struct usb_ctrlrequest r;
3087 } u;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003088 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 struct net2280_request *req;
3090
3091 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003092 u32 val = readl(&dev->usb->usbstat);
3093 if (val & (1 << SUPER_SPEED)) {
3094 dev->gadget.speed = USB_SPEED_SUPER;
3095 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3096 EP0_SS_MAX_PACKET_SIZE);
3097 } else if (val & (1 << HIGH_SPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 dev->gadget.speed = USB_SPEED_HIGH;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003099 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3100 EP0_HS_MAX_PACKET_SIZE);
3101 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 dev->gadget.speed = USB_SPEED_FULL;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003103 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3104 EP0_HS_MAX_PACKET_SIZE);
3105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 net2280_led_speed (dev, dev->gadget.speed);
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003107 DEBUG(dev, "%s\n", usb_speed_string(dev->gadget.speed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109
3110 ep = &dev->ep [0];
3111 ep->irqs++;
3112
3113 /* make sure any leftover request state is cleared */
3114 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
3115 while (!list_empty (&ep->queue)) {
3116 req = list_entry (ep->queue.next,
3117 struct net2280_request, queue);
3118 done (ep, req, (req->req.actual == req->req.length)
3119 ? 0 : -EPROTO);
3120 }
3121 ep->stopped = 0;
3122 dev->protocol_stall = 0;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003123 if (dev->pdev->vendor == 0x10b5)
3124 ep->is_halt = 0;
3125 else{
3126 if (ep->dev->pdev->device == 0x2280)
3127 tmp = (1 << FIFO_OVERFLOW) |
3128 (1 << FIFO_UNDERFLOW);
3129 else
3130 tmp = 0;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003131
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003132 writel(tmp | (1 << TIMEOUT) |
3133 (1 << USB_STALL_SENT) |
3134 (1 << USB_IN_NAK_SENT) |
3135 (1 << USB_IN_ACK_RCVD) |
3136 (1 << USB_OUT_PING_NAK_SENT) |
3137 (1 << USB_OUT_ACK_SENT) |
3138 (1 << SHORT_PACKET_OUT_DONE_INTERRUPT) |
3139 (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT) |
3140 (1 << DATA_PACKET_RECEIVED_INTERRUPT) |
3141 (1 << DATA_PACKET_TRANSMITTED_INTERRUPT) |
3142 (1 << DATA_OUT_PING_TOKEN_INTERRUPT) |
3143 (1 << DATA_IN_TOKEN_INTERRUPT)
3144 , &ep->regs->ep_stat);
3145 }
3146 u.raw[0] = readl(&dev->usb->setup0123);
3147 u.raw[1] = readl(&dev->usb->setup4567);
David Brownell901b3d72006-09-02 03:13:45 -07003148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 cpu_to_le32s (&u.raw [0]);
3150 cpu_to_le32s (&u.raw [1]);
3151
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003152 if (dev->pdev->vendor == 0x10b5)
3153 defect7374_workaround(dev, u.r);
3154
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003155 tmp = 0;
3156
David Brownell01ee7d72007-05-25 20:40:14 -07003157#define w_value le16_to_cpu(u.r.wValue)
3158#define w_index le16_to_cpu(u.r.wIndex)
3159#define w_length le16_to_cpu(u.r.wLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /* ack the irq */
3162 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
3163 stat ^= (1 << SETUP_PACKET_INTERRUPT);
3164
3165 /* watch control traffic at the token level, and force
3166 * synchronization before letting the status stage happen.
3167 * FIXME ignore tokens we'll NAK, until driver responds.
3168 * that'll mean a lot less irqs for some drivers.
3169 */
3170 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
3171 if (ep->is_in) {
3172 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
3173 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
3174 | (1 << DATA_IN_TOKEN_INTERRUPT);
3175 stop_out_naking (ep);
3176 } else
3177 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
3178 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
3179 | (1 << DATA_IN_TOKEN_INTERRUPT);
3180 writel (scratch, &dev->epregs [0].ep_irqenb);
3181
3182 /* we made the hardware handle most lowlevel requests;
3183 * everything else goes uplevel to the gadget code.
3184 */
Alan Stern1f26e282006-11-16 10:16:00 -05003185 ep->responded = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003186
3187 if (dev->gadget.speed == USB_SPEED_SUPER) {
3188 handle_stat0_irqs_superspeed(dev, ep, u.r);
3189 goto next_endpoints;
3190 }
3191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 switch (u.r.bRequest) {
3193 case USB_REQ_GET_STATUS: {
3194 struct net2280_ep *e;
David Brownell320f3452005-05-07 13:05:18 -07003195 __le32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 /* hw handles device and interface status */
3198 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
3199 goto delegate;
Felipe Balbiad303db2013-03-22 16:47:52 +02003200 if ((e = get_ep_by_addr (dev, w_index)) == NULL
David Brownell320f3452005-05-07 13:05:18 -07003201 || w_length > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 goto do_stall;
3203
3204 if (readl (&e->regs->ep_rsp)
3205 & (1 << SET_ENDPOINT_HALT))
Harvey Harrison551509d2009-02-11 14:11:36 -08003206 status = cpu_to_le32 (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 else
Harvey Harrison551509d2009-02-11 14:11:36 -08003208 status = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 /* don't bother with a request object! */
3211 writel (0, &dev->epregs [0].ep_irqenb);
David Brownell320f3452005-05-07 13:05:18 -07003212 set_fifo_bytecount (ep, w_length);
3213 writel ((__force u32)status, &dev->epregs [0].ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 allow_status (ep);
3215 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
3216 goto next_endpoints;
3217 }
3218 break;
3219 case USB_REQ_CLEAR_FEATURE: {
3220 struct net2280_ep *e;
3221
3222 /* hw handles device features */
3223 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3224 goto delegate;
David Brownell320f3452005-05-07 13:05:18 -07003225 if (w_value != USB_ENDPOINT_HALT
3226 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 goto do_stall;
Felipe Balbiad303db2013-03-22 16:47:52 +02003228 if ((e = get_ep_by_addr (dev, w_index)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003230 if (e->wedged) {
3231 VDEBUG(dev, "%s wedged, halt not cleared\n",
3232 ep->ep.name);
3233 } else {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003234 VDEBUG(dev, "%s clear halt\n", e->ep.name);
Alan Stern80661342008-08-14 15:49:11 -04003235 clear_halt(e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003236 if (ep->dev->pdev->vendor == 0x10b5 &&
3237 !list_empty(&e->queue) && e->td_dma)
3238 restart_dma(e);
Alan Stern80661342008-08-14 15:49:11 -04003239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 allow_status (ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 goto next_endpoints;
3242 }
3243 break;
3244 case USB_REQ_SET_FEATURE: {
3245 struct net2280_ep *e;
3246
3247 /* hw handles device features */
3248 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3249 goto delegate;
David Brownell320f3452005-05-07 13:05:18 -07003250 if (w_value != USB_ENDPOINT_HALT
3251 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 goto do_stall;
Felipe Balbiad303db2013-03-22 16:47:52 +02003253 if ((e = get_ep_by_addr (dev, w_index)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003255 if (e->ep.name == ep0name)
3256 goto do_stall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 set_halt (e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003258 if (dev->pdev->vendor == 0x10b5 && e->dma)
3259 abort_dma(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 allow_status (ep);
3261 VDEBUG (dev, "%s set halt\n", ep->ep.name);
3262 goto next_endpoints;
3263 }
3264 break;
3265 default:
3266delegate:
Joe Perchesfec8de32007-11-19 17:53:33 -08003267 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 "ep_cfg %08x\n",
3269 u.r.bRequestType, u.r.bRequest,
David Brownell320f3452005-05-07 13:05:18 -07003270 w_value, w_index, w_length,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003271 readl(&ep->cfg->ep_cfg));
Alan Stern1f26e282006-11-16 10:16:00 -05003272 ep->responded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 spin_unlock (&dev->lock);
3274 tmp = dev->driver->setup (&dev->gadget, &u.r);
3275 spin_lock (&dev->lock);
3276 }
3277
3278 /* stall ep0 on error */
3279 if (tmp < 0) {
3280do_stall:
3281 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
3282 u.r.bRequestType, u.r.bRequest, tmp);
3283 dev->protocol_stall = 1;
3284 }
3285
3286 /* some in/out token irq should follow; maybe stall then.
3287 * driver must queue a request (even zlp) or halt ep0
3288 * before the host times out.
3289 */
3290 }
3291
David Brownell320f3452005-05-07 13:05:18 -07003292#undef w_value
3293#undef w_index
3294#undef w_length
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296next_endpoints:
3297 /* endpoint data irq ? */
3298 scratch = stat & 0x7f;
3299 stat &= ~0x7f;
3300 for (num = 0; scratch; num++) {
3301 u32 t;
3302
3303 /* do this endpoint's FIFO and queue need tending? */
3304 t = 1 << num;
3305 if ((scratch & t) == 0)
3306 continue;
3307 scratch ^= t;
3308
3309 ep = &dev->ep [num];
3310 handle_ep_small (ep);
3311 }
3312
3313 if (stat)
3314 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
3315}
3316
3317#define DMA_INTERRUPTS ( \
3318 (1 << DMA_D_INTERRUPT) \
3319 | (1 << DMA_C_INTERRUPT) \
3320 | (1 << DMA_B_INTERRUPT) \
3321 | (1 << DMA_A_INTERRUPT))
3322#define PCI_ERROR_INTERRUPTS ( \
3323 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
3324 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
3325 | (1 << PCI_RETRY_ABORT_INTERRUPT))
3326
3327static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
3328{
3329 struct net2280_ep *ep;
3330 u32 tmp, num, mask, scratch;
3331
3332 /* after disconnect there's nothing else to do! */
3333 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003334 mask = (1 << SUPER_SPEED) | (1 << HIGH_SPEED) | (1 << FULL_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03003337 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
David Brownell901b3d72006-09-02 03:13:45 -07003338 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 * only indicates a change in the reset state).
3340 */
3341 if (stat & tmp) {
3342 writel (tmp, &dev->regs->irqstat1);
David Brownell901b3d72006-09-02 03:13:45 -07003343 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
3344 && ((readl (&dev->usb->usbstat) & mask)
3345 == 0))
3346 || ((readl (&dev->usb->usbctl)
3347 & (1 << VBUS_PIN)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
3349 DEBUG (dev, "disconnect %s\n",
3350 dev->driver->driver.name);
3351 stop_activity (dev, dev->driver);
3352 ep0_start (dev);
3353 return;
3354 }
3355 stat &= ~tmp;
3356
3357 /* vBUS can bounce ... one of many reasons to ignore the
3358 * notion of hotplug events on bus connect/disconnect!
3359 */
3360 if (!stat)
3361 return;
3362 }
3363
3364 /* NOTE: chip stays in PCI D0 state for now, but it could
3365 * enter D1 to save more power
3366 */
3367 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
3368 if (stat & tmp) {
3369 writel (tmp, &dev->regs->irqstat1);
3370 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
3371 if (dev->driver->suspend)
3372 dev->driver->suspend (&dev->gadget);
3373 if (!enable_suspend)
3374 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
3375 } else {
3376 if (dev->driver->resume)
3377 dev->driver->resume (&dev->gadget);
3378 /* at high speed, note erratum 0133 */
3379 }
3380 stat &= ~tmp;
3381 }
3382
3383 /* clear any other status/irqs */
3384 if (stat)
3385 writel (stat, &dev->regs->irqstat1);
3386
3387 /* some status we can just ignore */
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003388 if (dev->pdev->device == 0x2280)
3389 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
3390 | (1 << SUSPEND_REQUEST_INTERRUPT)
3391 | (1 << RESUME_INTERRUPT)
3392 | (1 << SOF_INTERRUPT));
3393 else
3394 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
3395 | (1 << RESUME_INTERRUPT)
3396 | (1 << SOF_DOWN_INTERRUPT)
3397 | (1 << SOF_INTERRUPT));
3398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 if (!stat)
3400 return;
3401 // DEBUG (dev, "irqstat1 %08x\n", stat);
3402
3403 /* DMA status, for ep-{a,b,c,d} */
3404 scratch = stat & DMA_INTERRUPTS;
3405 stat &= ~DMA_INTERRUPTS;
3406 scratch >>= 9;
3407 for (num = 0; scratch; num++) {
3408 struct net2280_dma_regs __iomem *dma;
3409
3410 tmp = 1 << num;
3411 if ((tmp & scratch) == 0)
3412 continue;
3413 scratch ^= tmp;
3414
3415 ep = &dev->ep [num + 1];
3416 dma = ep->dma;
3417
3418 if (!dma)
3419 continue;
3420
3421 /* clear ep's dma status */
3422 tmp = readl (&dma->dmastat);
3423 writel (tmp, &dma->dmastat);
3424
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003425 /* dma sync*/
3426 if (dev->pdev->vendor == 0x10b5) {
3427 u32 r_dmacount = readl(&dma->dmacount);
3428 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
3429 (tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT)))
3430 continue;
3431 }
3432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 /* chaining should stop on abort, short OUT from fifo,
3434 * or (stat0 codepath) short OUT transfer.
3435 */
3436 if (!use_dma_chaining) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003437 if (!(tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 DEBUG (ep->dev, "%s no xact done? %08x\n",
3439 ep->ep.name, tmp);
3440 continue;
3441 }
3442 stop_dma (ep->dma);
3443 }
3444
3445 /* OUT transfers terminate when the data from the
3446 * host is in our memory. Process whatever's done.
3447 * On this path, we know transfer's last packet wasn't
3448 * less than req->length. NAK_OUT_PACKETS may be set,
3449 * or the FIFO may already be holding new packets.
3450 *
3451 * IN transfers can linger in the FIFO for a very
3452 * long time ... we ignore that for now, accounting
3453 * precisely (like PIO does) needs per-packet irqs
3454 */
3455 scan_dma_completions (ep);
3456
3457 /* disable dma on inactive queues; else maybe restart */
3458 if (list_empty (&ep->queue)) {
3459 if (use_dma_chaining)
3460 stop_dma (ep->dma);
3461 } else {
3462 tmp = readl (&dma->dmactl);
3463 if (!use_dma_chaining
3464 || (tmp & (1 << DMA_ENABLE)) == 0)
3465 restart_dma (ep);
3466 else if (ep->is_in && use_dma_chaining) {
3467 struct net2280_request *req;
David Brownell320f3452005-05-07 13:05:18 -07003468 __le32 dmacount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 /* the descriptor at the head of the chain
3471 * may still have VALID_BIT clear; that's
3472 * used to trigger changing DMA_FIFO_VALIDATE
3473 * (affects automagic zlp writes).
3474 */
3475 req = list_entry (ep->queue.next,
3476 struct net2280_request, queue);
3477 dmacount = req->td->dmacount;
Harvey Harrison551509d2009-02-11 14:11:36 -08003478 dmacount &= cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 (1 << VALID_BIT)
3480 | DMA_BYTE_COUNT_MASK);
3481 if (dmacount && (dmacount & valid_bit) == 0)
3482 restart_dma (ep);
3483 }
3484 }
3485 ep->irqs++;
3486 }
3487
3488 /* NOTE: there are other PCI errors we might usefully notice.
3489 * if they appear very often, here's where to try recovering.
3490 */
3491 if (stat & PCI_ERROR_INTERRUPTS) {
3492 ERROR (dev, "pci dma error; stat %08x\n", stat);
3493 stat &= ~PCI_ERROR_INTERRUPTS;
3494 /* these are fatal errors, but "maybe" they won't
3495 * happen again ...
3496 */
3497 stop_activity (dev, dev->driver);
3498 ep0_start (dev);
3499 stat = 0;
3500 }
3501
3502 if (stat)
3503 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
3504}
3505
David Howells7d12e782006-10-05 14:55:46 +01003506static irqreturn_t net2280_irq (int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507{
3508 struct net2280 *dev = _dev;
3509
Alan Stern658ad5e2006-04-14 16:44:11 -04003510 /* shared interrupt, not ours */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003511 if (dev->pdev->vendor == 0x17cc &&
3512 (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED))))
Alan Stern658ad5e2006-04-14 16:44:11 -04003513 return IRQ_NONE;
3514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 spin_lock (&dev->lock);
3516
3517 /* handle disconnect, dma, and more */
3518 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
3519
3520 /* control requests and PIO */
3521 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
3522
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003523 if (dev->pdev->vendor == 0x10b5) {
3524 /* re-enable interrupt to trigger any possible new interrupt */
3525 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3526 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3527 writel(pciirqenb1, &dev->regs->pciirqenb1);
3528 }
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 spin_unlock (&dev->lock);
3531
3532 return IRQ_HANDLED;
3533}
3534
3535/*-------------------------------------------------------------------------*/
3536
3537static void gadget_release (struct device *_dev)
3538{
3539 struct net2280 *dev = dev_get_drvdata (_dev);
3540
3541 kfree (dev);
3542}
3543
3544/* tear down the binding between this driver and the pci device */
3545
3546static void net2280_remove (struct pci_dev *pdev)
3547{
3548 struct net2280 *dev = pci_get_drvdata (pdev);
3549
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003550 usb_del_gadget_udc(&dev->gadget);
3551
David Brownell6bea4762006-12-05 03:15:33 -08003552 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554 /* then clean up the resources we allocated during probe() */
3555 net2280_led_shutdown (dev);
3556 if (dev->requests) {
3557 int i;
3558 for (i = 1; i < 5; i++) {
3559 if (!dev->ep [i].dummy)
3560 continue;
3561 pci_pool_free (dev->requests, dev->ep [i].dummy,
3562 dev->ep [i].td_dma);
3563 }
3564 pci_pool_destroy (dev->requests);
3565 }
3566 if (dev->got_irq)
3567 free_irq (pdev->irq, dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003568 if (use_msi && dev->pdev->vendor == 0x10b5)
3569 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (dev->regs)
3571 iounmap (dev->regs);
3572 if (dev->region)
3573 release_mem_region (pci_resource_start (pdev, 0),
3574 pci_resource_len (pdev, 0));
3575 if (dev->enabled)
3576 pci_disable_device (pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 device_remove_file (&pdev->dev, &dev_attr_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579 INFO (dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
3582/* wrap this driver around the specified device, but
3583 * don't respond over USB until a gadget driver binds to us.
3584 */
3585
3586static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
3587{
3588 struct net2280 *dev;
3589 unsigned long resource, len;
3590 void __iomem *base = NULL;
3591 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 /* alloc, and start init */
Christoph Lametere94b1762006-12-06 20:33:17 -08003594 dev = kzalloc (sizeof *dev, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (dev == NULL){
3596 retval = -ENOMEM;
3597 goto done;
3598 }
3599
Alan Stern9fb81ce2006-04-14 16:46:28 -04003600 pci_set_drvdata (pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 spin_lock_init (&dev->lock);
3602 dev->pdev = pdev;
3603 dev->gadget.ops = &net2280_ops;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003604 dev->gadget.max_speed = (dev->pdev->vendor == 0x10b5) ?
3605 USB_SPEED_SUPER : USB_SPEED_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
3607 /* the "gadget" abstracts/virtualizes the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 dev->gadget.name = driver_name;
3609
3610 /* now all the pci goodies ... */
3611 if (pci_enable_device (pdev) < 0) {
David Brownell901b3d72006-09-02 03:13:45 -07003612 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 goto done;
3614 }
3615 dev->enabled = 1;
3616
3617 /* BAR 0 holds all the registers
3618 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3619 * BAR 2 is fifo memory; unused here
3620 */
3621 resource = pci_resource_start (pdev, 0);
3622 len = pci_resource_len (pdev, 0);
3623 if (!request_mem_region (resource, len, driver_name)) {
3624 DEBUG (dev, "controller already in use\n");
3625 retval = -EBUSY;
3626 goto done;
3627 }
3628 dev->region = 1;
3629
David Brownell901b3d72006-09-02 03:13:45 -07003630 /* FIXME provide firmware download interface to put
3631 * 8051 code into the chip, e.g. to turn on PCI PM.
3632 */
3633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 base = ioremap_nocache (resource, len);
3635 if (base == NULL) {
3636 DEBUG (dev, "can't map memory\n");
3637 retval = -EFAULT;
3638 goto done;
3639 }
3640 dev->regs = (struct net2280_regs __iomem *) base;
3641 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3642 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3643 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3644 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3645 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3646
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003647 if (dev->pdev->vendor == 0x10b5) {
3648 u32 fsmvalue;
3649 u32 usbstat;
3650 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3651 (base + 0x00b4);
3652 dev->fiforegs = (struct usb338x_fifo_regs __iomem *)
3653 (base + 0x0500);
3654 dev->llregs = (struct usb338x_ll_regs __iomem *)
3655 (base + 0x0700);
3656 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3657 (base + 0x0748);
3658 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3659 (base + 0x077c);
3660 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3661 (base + 0x079c);
3662 dev->plregs = (struct usb338x_pl_regs __iomem *)
3663 (base + 0x0800);
3664 usbstat = readl(&dev->usb->usbstat);
3665 dev->enhanced_mode = (usbstat & (1 << 11)) ? 1 : 0;
3666 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3667 /* put into initial config, link up all endpoints */
3668 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3669 (0xf << DEFECT7374_FSM_FIELD);
3670 /* See if firmware needs to set up for workaround: */
3671 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ)
3672 writel(0, &dev->usb->usbctl);
3673 } else{
3674 dev->enhanced_mode = 0;
3675 dev->n_ep = 7;
3676 /* put into initial config, link up all endpoints */
3677 writel(0, &dev->usb->usbctl);
3678 }
3679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 usb_reset (dev);
3681 usb_reinit (dev);
3682
3683 /* irq setup after old hardware is cleaned up */
3684 if (!pdev->irq) {
3685 ERROR (dev, "No IRQ. Check PCI setup!\n");
3686 retval = -ENODEV;
3687 goto done;
3688 }
David S. Millerc6387a42006-06-20 01:21:29 -07003689
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003690 if (use_msi && dev->pdev->vendor == 0x10b5)
3691 if (pci_enable_msi(pdev))
3692 ERROR(dev, "Failed to enable MSI mode\n");
3693
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -07003694 if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 != 0) {
David S. Millerc6387a42006-06-20 01:21:29 -07003696 ERROR (dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 retval = -EBUSY;
3698 goto done;
3699 }
3700 dev->got_irq = 1;
3701
3702 /* DMA setup */
3703 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
3704 dev->requests = pci_pool_create ("requests", pdev,
3705 sizeof (struct net2280_dma),
3706 0 /* no alignment requirements */,
3707 0 /* or page-crossing issues */);
3708 if (!dev->requests) {
3709 DEBUG (dev, "can't get request pool\n");
3710 retval = -ENOMEM;
3711 goto done;
3712 }
3713 for (i = 1; i < 5; i++) {
3714 struct net2280_dma *td;
3715
3716 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
3717 &dev->ep [i].td_dma);
3718 if (!td) {
3719 DEBUG (dev, "can't get dummy %d\n", i);
3720 retval = -ENOMEM;
3721 goto done;
3722 }
3723 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 td->dmadesc = td->dmaaddr;
3725 dev->ep [i].dummy = td;
3726 }
3727
3728 /* enable lower-overhead pci memory bursts during DMA */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003729 if (dev->pdev->vendor == 0x17cc)
3730 writel((1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 // 256 write retries may not be enough...
3732 // | (1 << PCI_RETRY_ABORT_ENABLE)
3733 | (1 << DMA_READ_MULTIPLE_ENABLE)
3734 | (1 << DMA_READ_LINE_ENABLE)
3735 , &dev->pci->pcimstctl);
3736 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3737 pci_set_master (pdev);
Randy Dunlap694625c2007-07-09 11:55:54 -07003738 pci_try_set_mwi (pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
3740 /* ... also flushes any posted pci writes */
3741 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
3742
3743 /* done */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 INFO (dev, "%s\n", driver_desc);
David S. Millerc6387a42006-06-20 01:21:29 -07003745 INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
3746 pdev->irq, base, dev->chiprev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003747 INFO(dev, "version: " DRIVER_VERSION "; dma %s %s\n",
3748 use_dma ? (use_dma_chaining ? "chaining" : "enabled")
3749 : "disabled",
3750 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
Jeff Garzikb3899da2006-10-11 21:50:24 -04003751 retval = device_create_file (&pdev->dev, &dev_attr_registers);
3752 if (retval) goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Felipe Balbi2901df62013-02-26 15:15:27 +02003754 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3755 gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003756 if (retval)
3757 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 return 0;
3759
3760done:
3761 if (dev)
3762 net2280_remove (pdev);
3763 return retval;
3764}
3765
Alan Stern2d61bde2006-05-05 16:23:42 -04003766/* make sure the board is quiescent; otherwise it will continue
3767 * generating IRQs across the upcoming reboot.
3768 */
3769
3770static void net2280_shutdown (struct pci_dev *pdev)
3771{
3772 struct net2280 *dev = pci_get_drvdata (pdev);
3773
3774 /* disable IRQs */
3775 writel (0, &dev->regs->pciirqenb0);
3776 writel (0, &dev->regs->pciirqenb1);
3777
3778 /* disable the pullup so the host will think we're gone */
3779 writel (0, &dev->usb->usbctl);
Alan Stern2f076072013-01-30 16:40:14 -05003780
3781 /* Disable full-speed test mode */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003782 if (dev->pdev->vendor == 0x17cc)
3783 writel(0, &dev->usb->xcvrdiag);
Alan Stern2d61bde2006-05-05 16:23:42 -04003784}
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787/*-------------------------------------------------------------------------*/
3788
David Brownell901b3d72006-09-02 03:13:45 -07003789static const struct pci_device_id pci_ids [] = { {
3790 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3791 .class_mask = ~0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 .vendor = 0x17cc,
3793 .device = 0x2280,
3794 .subvendor = PCI_ANY_ID,
3795 .subdevice = PCI_ANY_ID,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003796}, {
David Brownell901b3d72006-09-02 03:13:45 -07003797 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3798 .class_mask = ~0,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003799 .vendor = 0x17cc,
3800 .device = 0x2282,
3801 .subvendor = PCI_ANY_ID,
3802 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003803},
3804 {
3805 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3806 .class_mask = ~0,
3807 .vendor = 0x10b5,
3808 .device = 0x3380,
3809 .subvendor = PCI_ANY_ID,
3810 .subdevice = PCI_ANY_ID,
3811 },
3812 {
3813 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3814 .class_mask = ~0,
3815 .vendor = 0x10b5,
3816 .device = 0x3382,
3817 .subvendor = PCI_ANY_ID,
3818 .subdevice = PCI_ANY_ID,
3819 },
3820{ /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821};
3822MODULE_DEVICE_TABLE (pci, pci_ids);
3823
3824/* pci driver glue; this is a "new style" PCI driver module */
3825static struct pci_driver net2280_pci_driver = {
3826 .name = (char *) driver_name,
3827 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 .probe = net2280_probe,
3830 .remove = net2280_remove,
Alan Stern2d61bde2006-05-05 16:23:42 -04003831 .shutdown = net2280_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 /* FIXME add power management support */
3834};
3835
3836MODULE_DESCRIPTION (DRIVER_DESC);
3837MODULE_AUTHOR ("David Brownell");
3838MODULE_LICENSE ("GPL");
3839
3840static int __init init (void)
3841{
3842 if (!use_dma)
3843 use_dma_chaining = 0;
3844 return pci_register_driver (&net2280_pci_driver);
3845}
3846module_init (init);
3847
3848static void __exit cleanup (void)
3849{
3850 pci_unregister_driver (&net2280_pci_driver);
3851}
3852module_exit (cleanup);