blob: 6411ed8e14bcd4a4830889a90b01a06ab9932253 [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 *
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +010015 * DMA is enabled by default.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020017 * MSI is enabled by default. The legacy IRQ is used if MSI couldn't
18 * be enabled.
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Note that almost all the errata workarounds here are only needed for
21 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
22 */
23
24/*
25 * Copyright (C) 2003 David Brownell
26 * Copyright (C) 2003-2005 PLX Technology, Inc.
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020027 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
David Brownell901b3d72006-09-02 03:13:45 -070029 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
30 * with 2282 chip
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +010031 *
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020032 * Modified Ricardo Ribalda Qtechnology AS to provide compatibility
33 * with usb 338x chip. Based on PLX driver
34 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/pci.h>
David Brownell682d4c82006-01-18 23:55:08 -080043#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/kernel.h>
45#include <linux/delay.h>
46#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/timer.h>
51#include <linux/list.h>
52#include <linux/interrupt.h>
53#include <linux/moduleparam.h>
54#include <linux/device.h>
David Brownell5f848132006-12-16 15:34:53 -080055#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070056#include <linux/usb/gadget.h>
Bryan Wub38b03b2011-06-02 12:51:29 +080057#include <linux/prefetch.h>
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +020058#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/unaligned.h>
63
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020064#define DRIVER_DESC "PLX NET228x/USB338x USB Peripheral Controller"
65#define DRIVER_VERSION "2005 Sept 27/v3.0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define EP_DONTUSE 13 /* nonzero */
68
69#define USE_RDK_LEDS /* GPIO pins control three LEDs */
70
71
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +020072static const char driver_name[] = "net2280";
73static const char driver_desc[] = DRIVER_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020075static const u32 ep_bit[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +020076static const char ep0name[] = "ep0";
77static const char *const ep_name[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 ep0name,
79 "ep-a", "ep-b", "ep-c", "ep-d",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020080 "ep-e", "ep-f", "ep-g", "ep-h",
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* mode 0 == ep-{a,b,c,d} 1K fifo each
84 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
85 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
86 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +020087static ushort fifo_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* "modprobe net2280 fifo_mode=1" etc */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +020090module_param(fifo_mode, ushort, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* enable_suspend -- When enabled, the driver will respond to
93 * USB suspend requests by powering down the NET2280. Otherwise,
Lucas De Marchi25985ed2011-03-30 22:57:33 -030094 * USB suspend requests will be ignored. This is acceptable for
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +010095 * self-powered devices
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Ricardo Ribalda Delgado00d4db02014-05-20 18:30:06 +020097static bool enable_suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/* "modprobe net2280 enable_suspend=1" etc */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200100module_param(enable_suspend, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
103
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200104static char *type_string(u8 bmAttributes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
107 case USB_ENDPOINT_XFER_BULK: return "bulk";
108 case USB_ENDPOINT_XFER_ISOC: return "iso";
109 case USB_ENDPOINT_XFER_INT: return "intr";
Joe Perches2b84f922013-10-08 16:01:37 -0700110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return "control";
112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#include "net2280.h"
115
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200116#define valid_bit cpu_to_le32(BIT(VALID_BIT))
117#define dma_done_ie cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/*-------------------------------------------------------------------------*/
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200120static inline void enable_pciirqenb(struct net2280_ep *ep)
121{
122 u32 tmp = readl(&ep->dev->regs->pciirqenb0);
123
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200124 if (ep->dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200125 tmp |= BIT(ep->num);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200126 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200127 tmp |= BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200128 writel(tmp, &ep->dev->regs->pciirqenb0);
129
130 return;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200134net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct net2280 *dev;
137 struct net2280_ep *ep;
138 u32 max, tmp;
139 unsigned long flags;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200140 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200142 ep = container_of(_ep, struct net2280_ep, ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200143 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
144 desc->bDescriptorType != USB_DT_ENDPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EINVAL;
146 dev = ep->dev;
147 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
148 return -ESHUTDOWN;
149
150 /* erratum 0119 workaround ties up an endpoint number */
151 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
152 return -EDOM;
153
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200154 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200155 if ((desc->bEndpointAddress & 0x0f) >= 0x0c)
156 return -EDOM;
157 ep->is_in = !!usb_endpoint_dir_in(desc);
158 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num])
159 return -EINVAL;
160 }
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* sanity check ep-e/ep-f since their fifos are small */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200163 max = usb_endpoint_maxp(desc) & 0x1fff;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200164 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -ERANGE;
166
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200167 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 _ep->maxpacket = max & 0x7ff;
169 ep->desc = desc;
170
171 /* ep_reset() has already been called */
172 ep->stopped = 0;
Alan Stern80661342008-08-14 15:49:11 -0400173 ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ep->out_overflow = 0;
175
176 /* set speed-dependent max packet; may kick in high bandwidth */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200177 set_max_speed(ep, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /* set type, direction, address; reset fifo counters */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200180 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
182 if (tmp == USB_ENDPOINT_XFER_INT) {
183 /* erratum 0105 workaround prevents hs NYET */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200184 if (dev->chiprev == 0100 &&
185 dev->gadget.speed == USB_SPEED_HIGH &&
186 !(desc->bEndpointAddress & USB_DIR_IN))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200187 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 &ep->regs->ep_rsp);
189 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
190 /* catch some particularly blatant driver bugs */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200191 if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
192 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
193 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
194 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -ERANGE;
196 }
197 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200198 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200199 /* Enable this endpoint */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200200 if (dev->quirks & PLX_LEGACY) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200201 tmp <<= ENDPOINT_TYPE;
202 tmp |= desc->bEndpointAddress;
203 /* default full fifo lines */
204 tmp |= (4 << ENDPOINT_BYTE_COUNT);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200205 tmp |= BIT(ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200206 ep->is_in = (tmp & USB_DIR_IN) != 0;
207 } else {
208 /* In Legacy mode, only OUT endpoints are used */
209 if (dev->enhanced_mode && ep->is_in) {
210 tmp <<= IN_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200211 tmp |= BIT(IN_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200212 /* Not applicable to Legacy */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200213 tmp |= BIT(ENDPOINT_DIRECTION);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200214 } else {
215 tmp <<= OUT_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200216 tmp |= BIT(OUT_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200217 tmp |= (ep->is_in << ENDPOINT_DIRECTION);
218 }
219
220 tmp |= usb_endpoint_num(desc);
221 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
222 }
223
224 /* Make sure all the registers are written before ep_rsp*/
225 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* for OUT transfers, block the rx fifo until a read is posted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200229 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200230 else if (!(dev->quirks & PLX_2280)) {
David Brownell901b3d72006-09-02 03:13:45 -0700231 /* Added for 2282, Don't use nak packets on an in endpoint,
232 * this was ignored on 2280
233 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200234 writel(BIT(CLEAR_NAK_OUT_PACKETS) |
235 BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200238 writel(tmp, &ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* enable irqs */
241 if (!ep->dma) { /* pio, per-packet */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200242 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200244 tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
245 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200246 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200247 tmp |= readl(&ep->regs->ep_irqenb);
248 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 } else { /* dma, per-request */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200250 tmp = BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200251 tmp |= readl(&dev->regs->pciirqenb1);
252 writel(tmp, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* for short OUT transfers, dma completions can't
255 * advance the queue; do it pio-style, by hand.
256 * NOTE erratum 0112 workaround #2
257 */
258 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200259 tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200260 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200262 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 }
265
266 tmp = desc->bEndpointAddress;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200267 ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200268 _ep->name, tmp & 0x0f, DIR_STRING(tmp),
269 type_string(desc->bmAttributes),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ep->dma ? "dma" : "pio", max);
271
272 /* pci writes may still be posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200273 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 0;
275}
276
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200277static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 u32 result;
280
281 do {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200282 result = readl(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (result == ~(u32)0) /* "device unplugged" */
284 return -ENODEV;
285 result &= mask;
286 if (result == done)
287 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200288 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 usec--;
290 } while (usec > 0);
291 return -ETIMEDOUT;
292}
293
David Brownell901b3d72006-09-02 03:13:45 -0700294static const struct usb_ep_ops net2280_ep_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200296static void ep_reset_228x(struct net2280_regs __iomem *regs,
297 struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 u32 tmp;
300
301 ep->desc = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200302 INIT_LIST_HEAD(&ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Robert Baldygae117e742013-12-13 12:23:38 +0100304 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 ep->ep.ops = &net2280_ep_ops;
306
307 /* disable the dma, irqs, endpoint... */
308 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200309 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200310 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
311 BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
312 BIT(DMA_ABORT),
313 &ep->dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200315 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200316 tmp &= ~BIT(ep->num);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200317 writel(tmp, &regs->pciirqenb0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200319 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200320 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200321 writel(tmp, &regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200323 writel(0, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* init to our chosen defaults, notably so that we NAK OUT
326 * packets until the driver queues a read (+note erratum 0112)
327 */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200328 if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200329 tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
330 BIT(SET_NAK_OUT_PACKETS) |
331 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
332 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100333 } else {
334 /* added for 2282 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200335 tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
336 BIT(CLEAR_NAK_OUT_PACKETS) |
337 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
338 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (ep->num != 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200342 tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
343 BIT(CLEAR_ENDPOINT_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200345 writel(tmp, &ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* scrub most status bits, and flush any fifo state */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200348 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200349 tmp = BIT(FIFO_OVERFLOW) |
350 BIT(FIFO_UNDERFLOW);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100351 else
352 tmp = 0;
353
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200354 writel(tmp | BIT(TIMEOUT) |
355 BIT(USB_STALL_SENT) |
356 BIT(USB_IN_NAK_SENT) |
357 BIT(USB_IN_ACK_RCVD) |
358 BIT(USB_OUT_PING_NAK_SENT) |
359 BIT(USB_OUT_ACK_SENT) |
360 BIT(FIFO_FLUSH) |
361 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
362 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
363 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
364 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
365 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200366 BIT(DATA_IN_TOKEN_INTERRUPT),
367 &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* fifo size is handled separately */
370}
371
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200372static void ep_reset_338x(struct net2280_regs __iomem *regs,
373 struct net2280_ep *ep)
374{
375 u32 tmp, dmastat;
376
377 ep->desc = NULL;
378 INIT_LIST_HEAD(&ep->queue);
379
380 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
381 ep->ep.ops = &net2280_ep_ops;
382
383 /* disable the dma, irqs, endpoint... */
384 if (ep->dma) {
385 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200386 writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
387 BIT(DMA_PAUSE_DONE_INTERRUPT) |
388 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200389 BIT(DMA_TRANSACTION_DONE_INTERRUPT),
390 /* | BIT(DMA_ABORT), */
391 &ep->dma->dmastat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200392
393 dmastat = readl(&ep->dma->dmastat);
394 if (dmastat == 0x5002) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200395 ep_warn(ep->dev, "The dmastat return = %x!!\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200396 dmastat);
397 writel(0x5a, &ep->dma->dmastat);
398 }
399
400 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200401 tmp &= ~BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200402 writel(tmp, &regs->pciirqenb0);
403 } else {
404 if (ep->num < 5) {
405 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200406 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200407 writel(tmp, &regs->pciirqenb1);
408 }
409 }
410 writel(0, &ep->regs->ep_irqenb);
411
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200412 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
413 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
414 BIT(FIFO_OVERFLOW) |
415 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
416 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
417 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
418 BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200419}
420
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200421static void nuke(struct net2280_ep *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200423static int net2280_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct net2280_ep *ep;
426 unsigned long flags;
427
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200428 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (!_ep || !ep->desc || _ep->name == ep0name)
430 return -EINVAL;
431
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200432 spin_lock_irqsave(&ep->dev->lock, flags);
433 nuke(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200434
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200435 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200436 ep_reset_338x(ep->dev->regs, ep);
437 else
438 ep_reset_228x(ep->dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200440 ep_vdbg(ep->dev, "disabled %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 ep->dma ? "dma" : "pio", _ep->name);
442
443 /* synch memory views with the device */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200444 (void)readl(&ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +0100446 if (!ep->dma && ep->num >= 1 && ep->num <= 4)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200447 ep->dma = &ep->dev->dma[ep->num - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200449 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451}
452
453/*-------------------------------------------------------------------------*/
454
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200455static struct usb_request
456*net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct net2280_ep *ep;
459 struct net2280_request *req;
460
461 if (!_ep)
462 return NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200463 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800465 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!req)
467 return NULL;
468
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200469 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* this dma descriptor may be swapped with the previous dummy */
472 if (ep->dma) {
473 struct net2280_dma *td;
474
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200475 td = pci_pool_alloc(ep->dev->requests, gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 &req->td_dma);
477 if (!td) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200478 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return NULL;
480 }
481 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 td->dmadesc = td->dmaaddr;
483 req->td = td;
484 }
485 return &req->req;
486}
487
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200488static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct net2280_ep *ep;
491 struct net2280_request *req;
492
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200493 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!_ep || !_req)
495 return;
496
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200497 req = container_of(_req, struct net2280_request, req);
498 WARN_ON(!list_empty(&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (req->td)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200500 pci_pool_free(ep->dev->requests, req->td, req->td_dma);
501 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504/*-------------------------------------------------------------------------*/
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/* load a packet into the fifo we use for usb IN transfers.
507 * works for all endpoints.
508 *
509 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
510 * at a time, but this code is simpler because it knows it only writes
511 * one packet. ep-a..ep-d should use dma instead.
512 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200513static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct net2280_ep_regs __iomem *regs = ep->regs;
516 u8 *buf;
517 u32 tmp;
518 unsigned count, total;
519
520 /* INVARIANT: fifo is currently empty. (testable) */
521
522 if (req) {
523 buf = req->buf + req->actual;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200524 prefetch(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 total = req->length - req->actual;
526 } else {
527 total = 0;
528 buf = NULL;
529 }
530
531 /* write just one packet at a time */
532 count = ep->ep.maxpacket;
533 if (count > total) /* min() cannot be used on a bitfield */
534 count = total;
535
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200536 ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ep->ep.name, count,
538 (count != ep->ep.maxpacket) ? " (short)" : "",
539 req);
540 while (count >= 4) {
541 /* NOTE be careful if you try to align these. fifo lines
542 * should normally be full (4 bytes) and successive partial
543 * lines are ok only in certain cases.
544 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200545 tmp = get_unaligned((u32 *)buf);
546 cpu_to_le32s(&tmp);
547 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 buf += 4;
549 count -= 4;
550 }
551
552 /* last fifo entry is "short" unless we wrote a full packet.
553 * also explicitly validate last word in (periodic) transfers
554 * when maxpacket is not a multiple of 4 bytes.
555 */
556 if (count || total < ep->ep.maxpacket) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200557 tmp = count ? get_unaligned((u32 *)buf) : count;
558 cpu_to_le32s(&tmp);
559 set_fifo_bytecount(ep, count & 0x03);
560 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
563 /* pci writes may still be posted */
564}
565
566/* work around erratum 0106: PCI and USB race over the OUT fifo.
567 * caller guarantees chiprev 0100, out endpoint is NAKing, and
568 * there's no real data in the fifo.
569 *
570 * NOTE: also used in cases where that erratum doesn't apply:
571 * where the host wrote "too much" data to us.
572 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200573static void out_flush(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 u32 __iomem *statp;
576 u32 tmp;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 statp = &ep->regs->ep_stat;
Ricardo Ribalda Delgadod82f3db2014-11-28 14:51:01 +0100579
580 tmp = readl(statp);
581 if (tmp & BIT(NAK_OUT_PACKETS)) {
582 ep_dbg(ep->dev, "%s %s %08x !NAK\n",
583 ep->ep.name, __func__, tmp);
584 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
585 }
586
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200587 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200588 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
589 statp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200590 writel(BIT(FIFO_FLUSH), statp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200591 /* Make sure that stap is written */
592 mb();
593 tmp = readl(statp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200594 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* high speed did bulk NYET; fifo isn't filling */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200596 ep->dev->gadget.speed == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned usec;
598
599 usec = 50; /* 64 byte bulk/interrupt */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200600 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
601 BIT(USB_OUT_PING_NAK_SENT), usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
603 }
604}
605
606/* unload packet(s) from the fifo we use for usb OUT transfers.
607 * returns true iff the request completed, because of short packet
608 * or the request buffer having filled with full packets.
609 *
610 * for ep-a..ep-d this will read multiple packets out when they
611 * have been accepted.
612 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200613static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct net2280_ep_regs __iomem *regs = ep->regs;
616 u8 *buf = req->req.buf + req->req.actual;
617 unsigned count, tmp, is_short;
618 unsigned cleanup = 0, prevent = 0;
619
620 /* erratum 0106 ... packets coming in during fifo reads might
621 * be incompletely rejected. not all cases have workarounds.
622 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200623 if (ep->dev->chiprev == 0x0100 &&
624 ep->dev->gadget.speed == USB_SPEED_FULL) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200625 udelay(1);
626 tmp = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200627 if ((tmp & BIT(NAK_OUT_PACKETS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 cleanup = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200629 else if ((tmp & BIT(FIFO_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200630 start_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 prevent = 1;
632 }
633 /* else: hope we don't see the problem */
634 }
635
636 /* never overflow the rx buffer. the fifo reads packets until
637 * it sees a short one; we might not be ready for them all.
638 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200639 prefetchw(buf);
640 count = readl(&regs->ep_avail);
641 if (unlikely(count == 0)) {
642 udelay(1);
643 tmp = readl(&ep->regs->ep_stat);
644 count = readl(&regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* handled that data already? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200646 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
648 }
649
650 tmp = req->req.length - req->req.actual;
651 if (count > tmp) {
652 /* as with DMA, data overflow gets flushed */
653 if ((tmp % ep->ep.maxpacket) != 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200654 ep_err(ep->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 "%s out fifo %d bytes, expected %d\n",
656 ep->ep.name, count, tmp);
657 req->req.status = -EOVERFLOW;
658 cleanup = 1;
659 /* NAK_OUT_PACKETS will be set, so flushing is safe;
660 * the next read will start with the next packet
661 */
662 } /* else it's a ZLP, no worries */
663 count = tmp;
664 }
665 req->req.actual += count;
666
667 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
668
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200669 ep_vdbg(ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ep->ep.name, count, is_short ? " (short)" : "",
671 cleanup ? " flush" : "", prevent ? " nak" : "",
672 req, req->req.actual, req->req.length);
673
674 while (count >= 4) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200675 tmp = readl(&regs->ep_data);
676 cpu_to_le32s(&tmp);
677 put_unaligned(tmp, (u32 *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 buf += 4;
679 count -= 4;
680 }
681 if (count) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200682 tmp = readl(&regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* LE conversion is implicit here: */
684 do {
685 *buf++ = (u8) tmp;
686 tmp >>= 8;
687 } while (--count);
688 }
689 if (cleanup)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200690 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (prevent) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200692 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200693 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200696 return is_short || ((req->req.actual == req->req.length) &&
697 !req->req.zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/* fill out dma descriptor to match a given request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200701static void fill_dma_desc(struct net2280_ep *ep,
702 struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct net2280_dma *td = req->td;
705 u32 dmacount = req->req.length;
706
707 /* don't let DMA continue after a short OUT packet,
708 * so overruns can't affect the next transfer.
709 * in case of overruns on max-size packets, we can't
710 * stop the fifo from filling but we can flush it.
711 */
712 if (ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200713 dmacount |= BIT(DMA_DIRECTION);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200714 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200715 !(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200716 dmacount |= BIT(END_OF_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 req->valid = valid;
719 if (valid)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200720 dmacount |= BIT(VALID_BIT);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100721 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /* td->dmadesc = previously set by caller */
724 td->dmaaddr = cpu_to_le32 (req->req.dma);
725
726 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200727 wmb();
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700728 td->dmacount = cpu_to_le32(dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731static const u32 dmactl_default =
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200732 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
733 BIT(DMA_CLEAR_COUNT_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* erratum 0116 workaround part 1 (use POLLING) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200735 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
736 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
737 BIT(DMA_VALID_BIT_ENABLE) |
738 BIT(DMA_SCATTER_GATHER_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* erratum 0116 workaround part 2 (no AUTOSTART) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200740 BIT(DMA_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200742static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200744 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200747static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200749 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200750 spin_stop_dma(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200753static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 struct net2280_dma_regs __iomem *dma = ep->dma;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200756 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200758 if (!(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200759 tmp |= BIT(END_OF_CHAIN);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100760
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200761 writel(tmp, &dma->dmacount);
762 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200764 writel(td_dma, &dma->dmadesc);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200765 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200766 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200767 writel(dmactl, &dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200770 (void) readl(&ep->dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200772 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (!ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200775 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200778static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 u32 tmp;
781 struct net2280_dma_regs __iomem *dma = ep->dma;
782
783 /* FIXME can't use DMA for ZLPs */
784
785 /* on this path we "know" there's no dma active (yet) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200786 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200787 writel(0, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* previous OUT packet might have been short */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200790 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
791 BIT(NAK_OUT_PACKETS))) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200792 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 &ep->regs->ep_stat);
794
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200795 tmp = readl(&ep->regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200797 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* transfer all/some fifo data */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200800 writel(req->req.dma, &dma->dmaaddr);
801 tmp = min(tmp, req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /* dma irq, faking scatterlist status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200804 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200805 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
806 &dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 req->td->dmadesc = 0;
808 req->valid = 1;
809
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200810 writel(BIT(DMA_ENABLE), &dma->dmactl);
811 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return;
813 }
814 }
815
816 tmp = dmactl_default;
817
818 /* force packet boundaries between dma requests, but prevent the
819 * controller from automagically writing a last "short" packet
820 * (zero length) unless the driver explicitly said to do that.
821 */
822 if (ep->is_in) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200823 if (likely((req->req.length % ep->ep.maxpacket) ||
824 req->req.zero)){
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200825 tmp |= BIT(DMA_FIFO_VALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ep->in_fifo_validate = 1;
827 } else
828 ep->in_fifo_validate = 0;
829 }
830
831 /* init req->td, pointing to the current dummy */
832 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200833 fill_dma_desc(ep, req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100835 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200837 start_queue(ep, tmp, req->td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200841queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct net2280_dma *end;
844 dma_addr_t tmp;
845
846 /* swap new dummy for old, link; fill and maybe activate */
847 end = ep->dummy;
848 ep->dummy = req->td;
849 req->td = end;
850
851 tmp = ep->td_dma;
852 ep->td_dma = req->td_dma;
853 req->td_dma = tmp;
854
855 end->dmadesc = cpu_to_le32 (ep->td_dma);
856
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200857 fill_dma_desc(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860static void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200861done(struct net2280_ep *ep, struct net2280_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct net2280 *dev;
864 unsigned stopped = ep->stopped;
865
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200866 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (req->req.status == -EINPROGRESS)
869 req->req.status = status;
870 else
871 status = req->req.status;
872
873 dev = ep->dev;
Felipe Balbiae4d7932011-12-19 12:09:56 +0200874 if (ep->dma)
875 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (status && status != -ESHUTDOWN)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200878 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 ep->ep.name, &req->req, status,
880 req->req.actual, req->req.length);
881
882 /* don't modify queue heads during completion callback */
883 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200884 spin_unlock(&dev->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200885 usb_gadget_giveback_request(&ep->ep, &req->req);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200886 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ep->stopped = stopped;
888}
889
890/*-------------------------------------------------------------------------*/
891
892static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200893net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 struct net2280_request *req;
896 struct net2280_ep *ep;
897 struct net2280 *dev;
898 unsigned long flags;
899
900 /* we always require a cpu-view buffer, so that we can
901 * always use pio (as fallback or whatever).
902 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200903 req = container_of(_req, struct net2280_request, req);
904 if (!_req || !_req->complete || !_req->buf ||
905 !list_empty(&req->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -EINVAL;
907 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
908 return -EDOM;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200909 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!_ep || (!ep->desc && ep->num != 0))
911 return -EINVAL;
912 dev = ep->dev;
913 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
914 return -ESHUTDOWN;
915
916 /* FIXME implement PIO fallback for ZLPs with DMA */
917 if (ep->dma && _req->length == 0)
918 return -EOPNOTSUPP;
919
920 /* set up dma mapping in case the caller didn't */
Felipe Balbiae4d7932011-12-19 12:09:56 +0200921 if (ep->dma) {
922 int ret;
923
924 ret = usb_gadget_map_request(&dev->gadget, _req,
925 ep->is_in);
926 if (ret)
927 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200930 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200933 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 _req->status = -EINPROGRESS;
936 _req->actual = 0;
937
938 /* kickstart this i/o queue? */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100939 if (list_empty(&ep->queue) && !ep->stopped &&
940 !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
941 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* use DMA if the endpoint supports it, else pio */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100944 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200945 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 else {
947 /* maybe there's no control data, just status ack */
948 if (ep->num == 0 && _req->length == 0) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200949 allow_status(ep);
950 done(ep, req, 0);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200951 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto done;
953 }
954
955 /* PIO ... stuff the fifo, or unblock it. */
956 if (ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200957 write_fifo(ep, _req);
958 else if (list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 u32 s;
960
961 /* OUT FIFO might have packet(s) buffered */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200962 s = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200963 if ((s & BIT(FIFO_EMPTY)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /* note: _req->short_not_ok is
965 * ignored here since PIO _always_
966 * stops queue advance here, and
967 * _req->status doesn't change for
968 * short reads (only _req->actual)
969 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200970 if (read_fifo(ep, req) &&
971 ep->num == 0) {
972 done(ep, req, 0);
973 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* don't queue it */
975 req = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200976 } else if (read_fifo(ep, req) &&
977 ep->num != 0) {
978 done(ep, req, 0);
979 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200981 s = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983
984 /* don't NAK, let the fifo fill */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200985 if (req && (s & BIT(NAK_OUT_PACKETS)))
986 writel(BIT(CLEAR_NAK_OUT_PACKETS),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 &ep->regs->ep_rsp);
988 }
989 }
990
991 } else if (ep->dma) {
992 int valid = 1;
993
994 if (ep->is_in) {
995 int expect;
996
997 /* preventing magic zlps is per-engine state, not
998 * per-transfer; irq logic must recover hiccups.
999 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001000 expect = likely(req->req.zero ||
1001 (req->req.length % ep->ep.maxpacket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (expect != ep->in_fifo_validate)
1003 valid = 0;
1004 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001005 queue_dma(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 } /* else the irq handler advances the queue. */
1008
Alan Stern1f26e282006-11-16 10:16:00 -05001009 ep->responded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001011 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001013 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* pci writes may still be posted */
1016 return 0;
1017}
1018
1019static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001020dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1021 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001024 done(ep, req, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001027static void scan_dma_completions(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 /* only look at descriptors that were "naturally" retired,
1030 * so fifo and list head state won't matter
1031 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001032 while (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct net2280_request *req;
1034 u32 tmp;
1035
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001036 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct net2280_request, queue);
1038 if (!req->valid)
1039 break;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001040 rmb();
1041 tmp = le32_to_cpup(&req->td->dmacount);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001042 if ((tmp & BIT(VALID_BIT)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044
1045 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1046 * cases where DMA must be aborted; this code handles
1047 * all non-abort DMA completions.
1048 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001049 if (unlikely(req->td->dmadesc == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* paranoia */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001051 tmp = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (tmp & DMA_BYTE_COUNT_MASK)
1053 break;
1054 /* single transfer mode */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001055 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 break;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02001057 } else if (!ep->is_in &&
Ricardo Ribalda Delgado43780aa2014-11-28 14:51:00 +01001058 (req->req.length % ep->ep.maxpacket) &&
1059 !(ep->dev->quirks & PLX_SUPERSPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Mario Schuknecht18a4e652014-11-16 21:21:45 +01001061 tmp = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* AVOID TROUBLE HERE by not issuing short reads from
1063 * your gadget driver. That helps avoids errata 0121,
1064 * 0122, and 0124; not all cases trigger the warning.
1065 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001066 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001067 ep_warn(ep->dev, "%s lost packet sync!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ep->ep.name);
1069 req->req.status = -EOVERFLOW;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001070 } else {
1071 tmp = readl(&ep->regs->ep_avail);
1072 if (tmp) {
1073 /* fifo gets flushed later */
1074 ep->out_overflow = 1;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001075 ep_dbg(ep->dev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001076 "%s dma, discard %d len %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ep->ep.name, tmp,
1078 req->req.length);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001079 req->req.status = -EOVERFLOW;
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001083 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085}
1086
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001087static void restart_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct net2280_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 if (ep->stopped)
1092 return;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001093 req = list_entry(ep->queue.next, struct net2280_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01001095 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01001098static void abort_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 /* abort the current transfer */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001101 if (likely(!list_empty(&ep->queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* FIXME work around errata 0121, 0122, 0124 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001103 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001104 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001106 stop_dma(ep->dma);
1107 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
1110/* dequeue ALL requests */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001111static void nuke(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct net2280_request *req;
1114
1115 /* called with spinlock held */
1116 ep->stopped = 1;
1117 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001118 abort_dma(ep);
1119 while (!list_empty(&ep->queue)) {
1120 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct net2280_request,
1122 queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001123 done(ep, req, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125}
1126
1127/* dequeue JUST ONE request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001128static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct net2280_ep *ep;
1131 struct net2280_request *req;
1132 unsigned long flags;
1133 u32 dmactl;
1134 int stopped;
1135
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001136 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1138 return -EINVAL;
1139
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001140 spin_lock_irqsave(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 stopped = ep->stopped;
1142
1143 /* quiesce dma while we patch the queue */
1144 dmactl = 0;
1145 ep->stopped = 1;
1146 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001147 dmactl = readl(&ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* WARNING erratum 0127 may kick in ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001149 stop_dma(ep->dma);
1150 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
1153 /* make sure it's still queued on this endpoint */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001154 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (&req->req == _req)
1156 break;
1157 }
1158 if (&req->req != _req) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001159 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -EINVAL;
1161 }
1162
1163 /* queue head may be partially complete. */
1164 if (ep->queue.next == &req->queue) {
1165 if (ep->dma) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001166 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 _req->status = -ECONNRESET;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001168 abort_dma(ep);
1169 if (likely(ep->queue.next == &req->queue)) {
1170 /* NOTE: misreports single-transfer mode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 req->td->dmacount = 0; /* invalidate */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001172 dma_done(ep, req,
1173 readl(&ep->dma->dmacount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 -ECONNRESET);
1175 }
1176 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001177 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001178 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
1183 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001184 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ep->stopped = stopped;
1186
1187 if (ep->dma) {
1188 /* turn off dma on inactive queues */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001189 if (list_empty(&ep->queue))
1190 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 else if (!ep->stopped) {
1192 /* resume current request, or start new one */
1193 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001194 writel(dmactl, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001196 start_dma(ep, list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct net2280_request, queue));
1198 }
1199 }
1200
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001201 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203}
1204
1205/*-------------------------------------------------------------------------*/
1206
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001207static int net2280_fifo_status(struct usb_ep *_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209static int
Alan Stern80661342008-08-14 15:49:11 -04001210net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct net2280_ep *ep;
1213 unsigned long flags;
1214 int retval = 0;
1215
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001216 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!_ep || (!ep->desc && ep->num != 0))
1218 return -EINVAL;
1219 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1220 return -ESHUTDOWN;
1221 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1222 == USB_ENDPOINT_XFER_ISOC)
1223 return -EINVAL;
1224
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001225 spin_lock_irqsave(&ep->dev->lock, flags);
1226 if (!list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 retval = -EAGAIN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001228 else if (ep->is_in && value && net2280_fifo_status(_ep) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 retval = -EAGAIN;
1230 else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001231 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
Alan Stern80661342008-08-14 15:49:11 -04001232 value ? "set" : "clear",
1233 wedged ? "wedge" : "halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /* set/clear, then synch memory views with the device */
1235 if (value) {
1236 if (ep->num == 0)
1237 ep->dev->protocol_stall = 1;
1238 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001239 set_halt(ep);
Alan Stern80661342008-08-14 15:49:11 -04001240 if (wedged)
1241 ep->wedged = 1;
1242 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001243 clear_halt(ep);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001244 if (ep->dev->quirks & PLX_SUPERSPEED &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001245 !list_empty(&ep->queue) && ep->td_dma)
1246 restart_dma(ep);
Alan Stern80661342008-08-14 15:49:11 -04001247 ep->wedged = 0;
1248 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001249 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001251 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 return retval;
1254}
1255
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001256static int net2280_set_halt(struct usb_ep *_ep, int value)
Alan Stern80661342008-08-14 15:49:11 -04001257{
1258 return net2280_set_halt_and_wedge(_ep, value, 0);
1259}
1260
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001261static int net2280_set_wedge(struct usb_ep *_ep)
Alan Stern80661342008-08-14 15:49:11 -04001262{
1263 if (!_ep || _ep->name == ep0name)
1264 return -EINVAL;
1265 return net2280_set_halt_and_wedge(_ep, 1, 1);
1266}
1267
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001268static int net2280_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct net2280_ep *ep;
1271 u32 avail;
1272
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001273 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!_ep || (!ep->desc && ep->num != 0))
1275 return -ENODEV;
1276 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1277 return -ESHUTDOWN;
1278
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001279 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (avail > ep->fifo_size)
1281 return -EOVERFLOW;
1282 if (ep->is_in)
1283 avail = ep->fifo_size - avail;
1284 return avail;
1285}
1286
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001287static void net2280_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 struct net2280_ep *ep;
1290
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001291 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (!_ep || (!ep->desc && ep->num != 0))
1293 return;
1294 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1295 return;
1296
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001297 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001298 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
David Brownell901b3d72006-09-02 03:13:45 -07001301static const struct usb_ep_ops net2280_ep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 .enable = net2280_enable,
1303 .disable = net2280_disable,
1304
1305 .alloc_request = net2280_alloc_request,
1306 .free_request = net2280_free_request,
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 .queue = net2280_queue,
1309 .dequeue = net2280_dequeue,
1310
1311 .set_halt = net2280_set_halt,
Alan Stern80661342008-08-14 15:49:11 -04001312 .set_wedge = net2280_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 .fifo_status = net2280_fifo_status,
1314 .fifo_flush = net2280_fifo_flush,
1315};
1316
1317/*-------------------------------------------------------------------------*/
1318
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001319static int net2280_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct net2280 *dev;
1322 unsigned long flags;
1323 u16 retval;
1324
1325 if (!_gadget)
1326 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001327 dev = container_of(_gadget, struct net2280, gadget);
1328 spin_lock_irqsave(&dev->lock, flags);
1329 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1330 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return retval;
1332}
1333
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001334static int net2280_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 struct net2280 *dev;
1337 u32 tmp;
1338 unsigned long flags;
1339
1340 if (!_gadget)
1341 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001342 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001344 spin_lock_irqsave(&dev->lock, flags);
1345 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001346 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1347 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001348 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 /* pci writes may still be posted */
1351 return 0;
1352}
1353
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001354static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct net2280 *dev;
1357 u32 tmp;
1358 unsigned long flags;
1359
1360 if (!_gadget)
1361 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001362 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001364 spin_lock_irqsave(&dev->lock, flags);
1365 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001366 if (value) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001367 tmp |= BIT(SELF_POWERED_STATUS);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001368 dev->selfpowered = 1;
1369 } else {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001370 tmp &= ~BIT(SELF_POWERED_STATUS);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001371 dev->selfpowered = 0;
1372 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001373 writel(tmp, &dev->usb->usbctl);
1374 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 return 0;
1377}
1378
1379static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1380{
1381 struct net2280 *dev;
1382 u32 tmp;
1383 unsigned long flags;
1384
1385 if (!_gadget)
1386 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001387 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001389 spin_lock_irqsave(&dev->lock, flags);
1390 tmp = readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 dev->softconnect = (is_on != 0);
1392 if (is_on)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001393 tmp |= BIT(USB_DETECT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001395 tmp &= ~BIT(USB_DETECT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001396 writel(tmp, &dev->usb->usbctl);
1397 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 return 0;
1400}
1401
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001402static int net2280_start(struct usb_gadget *_gadget,
1403 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001404static int net2280_stop(struct usb_gadget *_gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406static const struct usb_gadget_ops net2280_ops = {
1407 .get_frame = net2280_get_frame,
1408 .wakeup = net2280_wakeup,
1409 .set_selfpowered = net2280_set_selfpowered,
1410 .pullup = net2280_pullup,
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001411 .udc_start = net2280_start,
1412 .udc_stop = net2280_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413};
1414
1415/*-------------------------------------------------------------------------*/
1416
Ricardo Ribalda Delgadob99b4062014-07-04 11:27:03 +02001417#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419/* FIXME move these into procfs, and use seq_file.
1420 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1421 * and also doesn't help products using this with 2.4 kernels.
1422 */
1423
1424/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001425static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1426 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001428 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001430 if (!dev->driver || !dev->driver->function ||
1431 strlen(dev->driver->function) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001433 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001435static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001437static ssize_t registers_show(struct device *_dev,
1438 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct net2280 *dev;
1441 char *next;
1442 unsigned size, t;
1443 unsigned long flags;
1444 int i;
1445 u32 t1, t2;
Andrew Morton30e69592005-06-26 17:18:46 -07001446 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001448 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 next = buf;
1450 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001451 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (dev->driver)
1454 s = dev->driver->driver.name;
1455 else
1456 s = "(none)";
1457
1458 /* Main Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001459 t = scnprintf(next, size, "%s version " DRIVER_VERSION
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001460 ", chiprev %04x\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 "devinit %03x fifoctl %08x gadget '%s'\n"
1462 "pci irqenb0 %02x irqenb1 %08x "
1463 "irqstat0 %04x irqstat1 %08x\n",
1464 driver_name, dev->chiprev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001465 readl(&dev->regs->devinit),
1466 readl(&dev->regs->fifoctl),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 s,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001468 readl(&dev->regs->pciirqenb0),
1469 readl(&dev->regs->pciirqenb1),
1470 readl(&dev->regs->irqstat0),
1471 readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 size -= t;
1473 next += t;
1474
1475 /* USB Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001476 t1 = readl(&dev->usb->usbctl);
1477 t2 = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001478 if (t1 & BIT(VBUS_PIN)) {
1479 if (t2 & BIT(HIGH_SPEED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 s = "high speed";
1481 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1482 s = "powered";
1483 else
1484 s = "full speed";
1485 /* full speed bit (6) not working?? */
1486 } else
1487 s = "not attached";
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001488 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 "stdrsp %08x usbctl %08x usbstat %08x "
1490 "addr 0x%02x (%s)\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001491 readl(&dev->usb->stdrsp), t1, t2,
1492 readl(&dev->usb->ouraddr), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 size -= t;
1494 next += t;
1495
1496 /* PCI Master Control Registers */
1497
1498 /* DMA Control Registers */
1499
1500 /* Configurable EP Control Registers */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001501 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 struct net2280_ep *ep;
1503
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001504 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (i && !ep->desc)
1506 continue;
1507
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001508 t1 = readl(&ep->cfg->ep_cfg);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001509 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1510 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1512 "irqenb %02x\n",
1513 ep->ep.name, t1, t2,
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001514 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 ? "NAK " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001516 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 ? "hide " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001518 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 ? "CRC " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001520 (t2 & BIT(CLEAR_INTERRUPT_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 ? "interrupt " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001522 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 ? "status " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001524 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 ? "NAKmode " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001526 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 ? "DATA1 " : "DATA0 ",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001528 (t2 & BIT(CLEAR_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 ? "HALT " : "",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001530 readl(&ep->regs->ep_irqenb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 size -= t;
1532 next += t;
1533
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001534 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 "\tstat %08x avail %04x "
1536 "(ep%d%s-%s)%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001537 readl(&ep->regs->ep_stat),
1538 readl(&ep->regs->ep_avail),
1539 t1 & 0x0f, DIR_STRING(t1),
1540 type_string(t1 >> 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ep->stopped ? "*" : "");
1542 size -= t;
1543 next += t;
1544
1545 if (!ep->dma)
1546 continue;
1547
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001548 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 " dma\tctl %08x stat %08x count %08x\n"
1550 "\taddr %08x desc %08x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001551 readl(&ep->dma->dmactl),
1552 readl(&ep->dma->dmastat),
1553 readl(&ep->dma->dmacount),
1554 readl(&ep->dma->dmaaddr),
1555 readl(&ep->dma->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 size -= t;
1557 next += t;
1558
1559 }
1560
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001561 /* Indexed Registers (none yet) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 /* Statistics */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001564 t = scnprintf(next, size, "\nirqs: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 size -= t;
1566 next += t;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001567 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 struct net2280_ep *ep;
1569
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001570 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (i && !ep->irqs)
1572 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001573 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 size -= t;
1575 next += t;
1576
1577 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001578 t = scnprintf(next, size, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 size -= t;
1580 next += t;
1581
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001582 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 return PAGE_SIZE - size;
1585}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001586static DEVICE_ATTR_RO(registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001588static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1589 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct net2280 *dev;
1592 char *next;
1593 unsigned size;
1594 unsigned long flags;
1595 int i;
1596
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001597 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 next = buf;
1599 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001600 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001602 for (i = 0; i < dev->n_ep; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001603 struct net2280_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 struct net2280_request *req;
1605 int t;
1606
1607 if (i != 0) {
1608 const struct usb_endpoint_descriptor *d;
1609
1610 d = ep->desc;
1611 if (!d)
1612 continue;
1613 t = d->bEndpointAddress;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001614 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1616 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1617 (t & USB_DIR_IN) ? "in" : "out",
Ricardo Ribalda Delgadoa27f37a2014-05-20 18:30:08 +02001618 type_string(d->bmAttributes),
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001619 usb_endpoint_maxp(d) & 0x1fff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ep->dma ? "dma" : "pio", ep->fifo_size
1621 );
1622 } else /* ep0 should only have one transfer queued */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001623 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 ep->is_in ? "in" : "out");
1625 if (t <= 0 || t > size)
1626 goto done;
1627 size -= t;
1628 next += t;
1629
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001630 if (list_empty(&ep->queue)) {
1631 t = scnprintf(next, size, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (t <= 0 || t > size)
1633 goto done;
1634 size -= t;
1635 next += t;
1636 continue;
1637 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001638 list_for_each_entry(req, &ep->queue, queue) {
1639 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1640 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 "\treq %p len %d/%d "
1642 "buf %p (dmacount %08x)\n",
1643 &req->req, req->req.actual,
1644 req->req.length, req->req.buf,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001645 readl(&ep->dma->dmacount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001647 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 "\treq %p len %d/%d buf %p\n",
1649 &req->req, req->req.actual,
1650 req->req.length, req->req.buf);
1651 if (t <= 0 || t > size)
1652 goto done;
1653 size -= t;
1654 next += t;
1655
1656 if (ep->dma) {
1657 struct net2280_dma *td;
1658
1659 td = req->td;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001660 t = scnprintf(next, size, "\t td %08x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 " count %08x buf %08x desc %08x\n",
1662 (u32) req->td_dma,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001663 le32_to_cpu(td->dmacount),
1664 le32_to_cpu(td->dmaaddr),
1665 le32_to_cpu(td->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (t <= 0 || t > size)
1667 goto done;
1668 size -= t;
1669 next += t;
1670 }
1671 }
1672 }
1673
1674done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001675 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return PAGE_SIZE - size;
1677}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001678static DEVICE_ATTR_RO(queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680
1681#else
1682
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001683#define device_create_file(a, b) (0)
1684#define device_remove_file(a, b) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686#endif
1687
1688/*-------------------------------------------------------------------------*/
1689
1690/* another driver-specific mode might be a request type doing dma
1691 * to/from another device fifo instead of to/from memory.
1692 */
1693
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001694static void set_fifo_mode(struct net2280 *dev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 /* keeping high bits preserves BAR2 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001697 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001700 INIT_LIST_HEAD(&dev->gadget.ep_list);
1701 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1702 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 switch (mode) {
1704 case 0:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001705 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1706 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1707 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709 case 1:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001710 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 break;
1712 case 2:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001713 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1714 dev->ep[1].fifo_size = 2048;
1715 dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 break;
1717 }
1718 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001719 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1720 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001723static void defect7374_disable_data_eps(struct net2280 *dev)
1724{
1725 /*
1726 * For Defect 7374, disable data EPs (and more):
1727 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1728 * returing ep regs back to normal.
1729 */
1730 struct net2280_ep *ep;
1731 int i;
1732 unsigned char ep_sel;
1733 u32 tmp_reg;
1734
1735 for (i = 1; i < 5; i++) {
1736 ep = &dev->ep[i];
1737 writel(0, &ep->cfg->ep_cfg);
1738 }
1739
1740 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1741 for (i = 0; i < 6; i++)
1742 writel(0, &dev->dep[i].dep_cfg);
1743
1744 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1745 /* Select an endpoint for subsequent operations: */
1746 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1747 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1748
1749 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1750 ep_sel == 18 || ep_sel == 20)
1751 continue;
1752
1753 /* Change settings on some selected endpoints */
1754 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001755 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001756 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1757 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001758 tmp_reg |= BIT(EP_INITIALIZED);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001759 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1760 }
1761}
1762
1763static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1764{
1765 u32 tmp = 0, tmp_reg;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001766 u32 scratch;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001767 int i;
1768 unsigned char ep_sel;
1769
1770 scratch = get_idx_reg(dev->regs, SCRATCH);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001771
1772 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1773 == DEFECT7374_FSM_SS_CONTROL_READ);
1774
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001775 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1776
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001777 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1778 ep_warn(dev, "It will operate on cold-reboot and SS connect");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001779
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001780 /*GPEPs:*/
1781 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1782 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1783 ((dev->enhanced_mode) ?
1784 BIT(OUT_ENDPOINT_ENABLE) : BIT(ENDPOINT_ENABLE)) |
1785 BIT(IN_ENDPOINT_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001786
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001787 for (i = 1; i < 5; i++)
1788 writel(tmp, &dev->ep[i].cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001789
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001790 /* CSRIN, PCIIN, STATIN, RCIN*/
1791 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1792 writel(tmp, &dev->dep[1].dep_cfg);
1793 writel(tmp, &dev->dep[3].dep_cfg);
1794 writel(tmp, &dev->dep[4].dep_cfg);
1795 writel(tmp, &dev->dep[5].dep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001796
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001797 /*Implemented for development and debug.
1798 * Can be refined/tuned later.*/
1799 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1800 /* Select an endpoint for subsequent operations: */
1801 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1802 writel(((tmp_reg & ~0x1f) | ep_sel),
1803 &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001804
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001805 if (ep_sel == 1) {
1806 tmp =
1807 (readl(&dev->plregs->pl_ep_ctrl) |
1808 BIT(CLEAR_ACK_ERROR_CODE) | 0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001809 writel(tmp, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001810 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001811 }
1812
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001813 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1814 ep_sel == 18 || ep_sel == 20)
1815 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001816
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001817 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1818 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1819 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1820
1821 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1822 ~BIT(EP_INITIALIZED);
1823 writel(tmp, &dev->plregs->pl_ep_ctrl);
1824
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001825 }
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001826
1827 /* Set FSM to focus on the first Control Read:
1828 * - Tip: Connection speed is known upon the first
1829 * setup request.*/
1830 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1831 set_idx_reg(dev->regs, SCRATCH, scratch);
1832
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/* keeping it simple:
1836 * - one bus driver, initted first;
1837 * - one function driver, initted second
1838 *
1839 * most of the work to support multiple net2280 controllers would
1840 * be to associate this gadget driver (yes?) with all of them, or
1841 * perhaps to bind specific drivers to specific devices.
1842 */
1843
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001844static void usb_reset_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 u32 tmp;
1847
1848 dev->gadget.speed = USB_SPEED_UNKNOWN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001849 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001851 net2280_led_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 /* disable automatic responses, and irqs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001854 writel(0, &dev->usb->stdrsp);
1855 writel(0, &dev->regs->pciirqenb0);
1856 writel(0, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* clear old dma and irq state */
1859 for (tmp = 0; tmp < 4; tmp++) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001860 struct net2280_ep *ep = &dev->ep[tmp + 1];
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001861 if (ep->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001862 abort_dma(ep);
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001863 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001864
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001865 writel(~0, &dev->regs->irqstat0),
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001866 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /* reset, and enable pci */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001869 tmp = readl(&dev->regs->devinit) |
1870 BIT(PCI_ENABLE) |
1871 BIT(FIFO_SOFT_RESET) |
1872 BIT(USB_SOFT_RESET) |
1873 BIT(M8051_RESET);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001874 writel(tmp, &dev->regs->devinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /* standard fifo and endpoint allocations */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001877 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001880static void usb_reset_338x(struct net2280 *dev)
1881{
1882 u32 tmp;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001883
1884 dev->gadget.speed = USB_SPEED_UNKNOWN;
1885 (void)readl(&dev->usb->usbctl);
1886
1887 net2280_led_init(dev);
1888
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001889 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001890 /* disable automatic responses, and irqs */
1891 writel(0, &dev->usb->stdrsp);
1892 writel(0, &dev->regs->pciirqenb0);
1893 writel(0, &dev->regs->pciirqenb1);
1894 }
1895
1896 /* clear old dma and irq state */
1897 for (tmp = 0; tmp < 4; tmp++) {
1898 struct net2280_ep *ep = &dev->ep[tmp + 1];
1899
1900 if (ep->dma)
1901 abort_dma(ep);
1902 }
1903
1904 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
1905
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001906 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001907 /* reset, and enable pci */
1908 tmp = readl(&dev->regs->devinit) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001909 BIT(PCI_ENABLE) |
1910 BIT(FIFO_SOFT_RESET) |
1911 BIT(USB_SOFT_RESET) |
1912 BIT(M8051_RESET);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001913
1914 writel(tmp, &dev->regs->devinit);
1915 }
1916
1917 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
1918 INIT_LIST_HEAD(&dev->gadget.ep_list);
1919
1920 for (tmp = 1; tmp < dev->n_ep; tmp++)
1921 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
1922
1923}
1924
1925static void usb_reset(struct net2280 *dev)
1926{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001927 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001928 return usb_reset_228x(dev);
1929 return usb_reset_338x(dev);
1930}
1931
1932static void usb_reinit_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 /* basic endpoint init */
1937 for (tmp = 0; tmp < 7; tmp++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001938 struct net2280_ep *ep = &dev->ep[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001940 ep->ep.name = ep_name[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 ep->dev = dev;
1942 ep->num = tmp;
1943
1944 if (tmp > 0 && tmp <= 4) {
1945 ep->fifo_size = 1024;
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001946 ep->dma = &dev->dma[tmp - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 } else
1948 ep->fifo_size = 64;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001949 ep->regs = &dev->epregs[tmp];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001950 ep->cfg = &dev->epregs[tmp];
1951 ep_reset_228x(dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001953 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
1954 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
1955 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001957 dev->gadget.ep0 = &dev->ep[0].ep;
1958 dev->ep[0].stopped = 0;
1959 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 /* we want to prevent lowlevel/insecure access from the USB host,
1962 * but erratum 0119 means this enable bit is ignored
1963 */
1964 for (tmp = 0; tmp < 5; tmp++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001965 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001968static void usb_reinit_338x(struct net2280 *dev)
1969{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001970 int i;
1971 u32 tmp, val;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001972 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
1973 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
1974 0x00, 0xC0, 0x00, 0xC0 };
1975
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001976 /* basic endpoint init */
1977 for (i = 0; i < dev->n_ep; i++) {
1978 struct net2280_ep *ep = &dev->ep[i];
1979
1980 ep->ep.name = ep_name[i];
1981 ep->dev = dev;
1982 ep->num = i;
1983
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001984 if (i > 0 && i <= 4)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001985 ep->dma = &dev->dma[i - 1];
1986
1987 if (dev->enhanced_mode) {
1988 ep->cfg = &dev->epregs[ne[i]];
1989 ep->regs = (struct net2280_ep_regs __iomem *)
Felipe Balbic43e97b2014-07-16 12:20:25 -05001990 (((void __iomem *)&dev->epregs[ne[i]]) +
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001991 ep_reg_addr[i]);
1992 ep->fiforegs = &dev->fiforegs[i];
1993 } else {
1994 ep->cfg = &dev->epregs[i];
1995 ep->regs = &dev->epregs[i];
1996 ep->fiforegs = &dev->fiforegs[i];
1997 }
1998
1999 ep->fifo_size = (i != 0) ? 2048 : 512;
2000
2001 ep_reset_338x(dev->regs, ep);
2002 }
2003 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2004
2005 dev->gadget.ep0 = &dev->ep[0].ep;
2006 dev->ep[0].stopped = 0;
2007
2008 /* Link layer set up */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002009 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002010 tmp = readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002011 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002012 writel(tmp, &dev->usb_ext->usbctl2);
2013 }
2014
2015 /* Hardware Defect and Workaround */
2016 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2017 val &= ~(0xf << TIMER_LFPS_6US);
2018 val |= 0x5 << TIMER_LFPS_6US;
2019 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2020
2021 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2022 val &= ~(0xffff << TIMER_LFPS_80US);
2023 val |= 0x0100 << TIMER_LFPS_80US;
2024 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2025
2026 /*
2027 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2028 * Hot Reset Exit Handshake may Fail in Specific Case using
2029 * Default Register Settings. Workaround for Enumeration test.
2030 */
2031 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2032 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2033 val |= 0x10 << HOT_TX_NORESET_TS2;
2034 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2035
2036 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2037 val &= ~(0x1f << HOT_RX_RESET_TS2);
2038 val |= 0x3 << HOT_RX_RESET_TS2;
2039 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2040
2041 /*
2042 * Set Recovery Idle to Recover bit:
2043 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2044 * link robustness with various hosts and hubs.
2045 * - It is safe to set for all connection speeds; all chip revisions.
2046 * - R-M-W to leave other bits undisturbed.
2047 * - Reference PLX TT-7372
2048 */
2049 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002050 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002051 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2052
2053 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2054
2055 /* disable dedicated endpoints */
2056 writel(0x0D, &dev->dep[0].dep_cfg);
2057 writel(0x0D, &dev->dep[1].dep_cfg);
2058 writel(0x0E, &dev->dep[2].dep_cfg);
2059 writel(0x0E, &dev->dep[3].dep_cfg);
2060 writel(0x0F, &dev->dep[4].dep_cfg);
2061 writel(0x0C, &dev->dep[5].dep_cfg);
2062}
2063
2064static void usb_reinit(struct net2280 *dev)
2065{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002066 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002067 return usb_reinit_228x(dev);
2068 return usb_reinit_338x(dev);
2069}
2070
2071static void ep0_start_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002073 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2074 BIT(CLEAR_NAK_OUT_PACKETS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002075 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2076 &dev->epregs[0].ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 /*
2079 * hardware optionally handles a bunch of standard requests
2080 * that the API hides from drivers anyway. have it do so.
2081 * endpoint status/features are handled in software, to
2082 * help pass tests for some dubious behavior.
2083 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002084 writel(BIT(SET_TEST_MODE) |
2085 BIT(SET_ADDRESS) |
2086 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2087 BIT(GET_DEVICE_STATUS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002088 BIT(GET_INTERFACE_STATUS),
2089 &dev->usb->stdrsp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002090 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2091 BIT(SELF_POWERED_USB_DEVICE) |
2092 BIT(REMOTE_WAKEUP_SUPPORT) |
2093 (dev->softconnect << USB_DETECT_ENABLE) |
2094 BIT(SELF_POWERED_STATUS),
2095 &dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002098 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2099 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2100 &dev->regs->pciirqenb0);
2101 writel(BIT(PCI_INTERRUPT_ENABLE) |
2102 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2103 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2104 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2105 BIT(VBUS_INTERRUPT_ENABLE) |
2106 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2107 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2108 &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /* don't leave any writes posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002111 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002114static void ep0_start_338x(struct net2280 *dev)
2115{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002116
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002117 if (dev->bug7734_patched)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002118 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2119 BIT(SET_EP_HIDE_STATUS_PHASE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002120 &dev->epregs[0].ep_rsp);
2121
2122 /*
2123 * hardware optionally handles a bunch of standard requests
2124 * that the API hides from drivers anyway. have it do so.
2125 * endpoint status/features are handled in software, to
2126 * help pass tests for some dubious behavior.
2127 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002128 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2129 BIT(SET_SEL) |
2130 BIT(SET_TEST_MODE) |
2131 BIT(SET_ADDRESS) |
2132 BIT(GET_INTERFACE_STATUS) |
2133 BIT(GET_DEVICE_STATUS),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002134 &dev->usb->stdrsp);
2135 dev->wakeup_enable = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002136 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002137 (dev->softconnect << USB_DETECT_ENABLE) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002138 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002139 &dev->usb->usbctl);
2140
2141 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002142 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002143 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2144 &dev->regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002145 writel(BIT(PCI_INTERRUPT_ENABLE) |
2146 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2147 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2148 BIT(VBUS_INTERRUPT_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002149 &dev->regs->pciirqenb1);
2150
2151 /* don't leave any writes posted */
2152 (void)readl(&dev->usb->usbctl);
2153}
2154
2155static void ep0_start(struct net2280 *dev)
2156{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002157 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002158 return ep0_start_228x(dev);
2159 return ep0_start_338x(dev);
2160}
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162/* when a driver is successfully registered, it will receive
2163 * control requests including set_configuration(), which enables
2164 * non-control requests. then usb traffic follows until a
2165 * disconnect is reported. then a host may connect again, or
2166 * the driver might get unbound.
2167 */
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002168static int net2280_start(struct usb_gadget *_gadget,
2169 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002171 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 int retval;
2173 unsigned i;
2174
2175 /* insist on high speed support from the driver, since
2176 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2177 * "must not be used in normal operation"
2178 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002179 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2180 !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return -EINVAL;
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002182
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002183 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002185 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002186 dev->ep[i].irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 /* hook up the driver ... */
2189 dev->softconnect = 1;
2190 driver->driver.bus = NULL;
2191 dev->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002193 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2194 if (retval)
2195 goto err_unbind;
2196 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2197 if (retval)
2198 goto err_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Ricardo Ribalda Delgado7a74c482014-11-28 14:50:47 +01002200 /* enable host detection and ep0; and we're ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 * for set_configuration as well as eventual disconnect.
2202 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002203 net2280_led_active(dev, 1);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002204
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002205 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002206 defect7374_enable_data_eps_zero(dev);
2207
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002208 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* pci writes may still be posted */
2211 return 0;
Jeff Garzikb3899da2006-10-11 21:50:24 -04002212
2213err_func:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002214 device_remove_file(&dev->pdev->dev, &dev_attr_function);
Jeff Garzikb3899da2006-10-11 21:50:24 -04002215err_unbind:
Jeff Garzikb3899da2006-10-11 21:50:24 -04002216 dev->driver = NULL;
2217 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002220static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 int i;
2223
2224 /* don't disconnect if it's not connected */
2225 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2226 driver = NULL;
2227
2228 /* stop hardware; prevent new request submissions;
2229 * and kill any outstanding requests.
2230 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002231 usb_reset(dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002232 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002233 nuke(&dev->ep[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Felipe Balbi699412d2013-03-18 10:14:47 +02002235 /* report disconnect; the driver is already quiesced */
2236 if (driver) {
2237 spin_unlock(&dev->lock);
2238 driver->disconnect(&dev->gadget);
2239 spin_lock(&dev->lock);
2240 }
2241
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002242 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Felipe Balbi22835b82014-10-17 12:05:12 -05002245static int net2280_stop(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002247 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 unsigned long flags;
2249
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002250 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002252 spin_lock_irqsave(&dev->lock, flags);
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002253 stop_activity(dev, NULL);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002254 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002256 net2280_led_active(dev, 0);
Alan Stern2f076072013-01-30 16:40:14 -05002257
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002258 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2259 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002261 dev->driver = NULL;
Ricardo Ribalda Delgado84237bf2014-05-15 14:28:45 +02002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return 0;
2264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266/*-------------------------------------------------------------------------*/
2267
2268/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2269 * also works for dma-capable endpoints, in pio mode or just
2270 * to manually advance the queue after short OUT transfers.
2271 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002272static void handle_ep_small(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 struct net2280_request *req;
2275 u32 t;
2276 /* 0 error, 1 mid-data, 2 done */
2277 int mode = 1;
2278
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002279 if (!list_empty(&ep->queue))
2280 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 struct net2280_request, queue);
2282 else
2283 req = NULL;
2284
2285 /* ack all, and handle what we care about */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002286 t = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 ep->irqs++;
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002288
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002289 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
Ricardo Ribalda Delgadofc12c682015-01-13 10:54:58 +01002290 ep->ep.name, t, req ? &req->req : NULL);
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002291
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002292 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002293 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002294 else
2295 /* Added for 2282 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002296 writel(t, &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 /* for ep0, monitor token irqs to catch data stage length errors
2299 * and to synchronize on status.
2300 *
2301 * also, to defer reporting of protocol stalls ... here's where
2302 * data or status first appears, handling stalls here should never
2303 * cause trouble on the host side..
2304 *
2305 * control requests could be slightly faster without token synch for
2306 * status, but status can jam up that way.
2307 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002308 if (unlikely(ep->num == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (ep->is_in) {
2310 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002311 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (ep->dev->protocol_stall) {
2313 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002314 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316 if (!req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002317 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 mode = 2;
2319 /* reply to extra IN data tokens with a zlp */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002320 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (ep->dev->protocol_stall) {
2322 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002323 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 mode = 2;
Alan Stern1f26e282006-11-16 10:16:00 -05002325 } else if (ep->responded &&
2326 !req && !ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002327 write_fifo(ep, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329 } else {
2330 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002331 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (ep->dev->protocol_stall) {
2333 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002334 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 mode = 2;
2337 /* an extra OUT token is an error */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002338 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2339 req &&
2340 req->req.actual == req->req.length) ||
2341 (ep->responded && !req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 ep->dev->protocol_stall = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002343 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 ep->stopped = 1;
2345 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002346 done(ep, req, -EOVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 req = NULL;
2348 }
2349 }
2350 }
2351
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002352 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return;
2354
2355 /* manual DMA queue advance after short OUT */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002356 if (likely(ep->dma)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002357 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 u32 count;
2359 int stopped = ep->stopped;
2360
2361 /* TRANSFERRED works around OUT_DONE erratum 0112.
2362 * we expect (N <= maxpacket) bytes; host wrote M.
2363 * iff (M < N) we won't ever see a DMA interrupt.
2364 */
2365 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002366 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 /* any preceding dma transfers must finish.
2369 * dma handles (M >= N), may empty the queue
2370 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002371 scan_dma_completions(ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002372 if (unlikely(list_empty(&ep->queue) ||
2373 ep->out_overflow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 req = NULL;
2375 break;
2376 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002377 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct net2280_request, queue);
2379
2380 /* here either (M < N), a "real" short rx;
2381 * or (M == N) and the queue didn't empty
2382 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002383 if (likely(t & BIT(FIFO_EMPTY))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002384 count = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 count &= DMA_BYTE_COUNT_MASK;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002386 if (readl(&ep->dma->dmadesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 != req->td_dma)
2388 req = NULL;
2389 break;
2390 }
2391 udelay(1);
2392 }
2393
2394 /* stop DMA, leave ep NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002395 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002396 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002398 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 req->td->dmacount = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002400 t = readl(&ep->regs->ep_avail);
2401 dma_done(ep, req, count,
David Brownell901b3d72006-09-02 03:13:45 -07002402 (ep->out_overflow || t)
2403 ? -EOVERFLOW : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405
2406 /* also flush to prevent erratum 0106 trouble */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002407 if (unlikely(ep->out_overflow ||
2408 (ep->dev->chiprev == 0x0100 &&
2409 ep->dev->gadget.speed
2410 == USB_SPEED_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002411 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 ep->out_overflow = 0;
2413 }
2414
2415 /* (re)start dma if needed, stop NAKing */
2416 ep->stopped = stopped;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002417 if (!list_empty(&ep->queue))
2418 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 } else
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002420 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 ep->ep.name, t);
2422 return;
2423
2424 /* data packet(s) received (in the fifo, OUT) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002425 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002426 if (read_fifo(ep, req) && ep->num != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 mode = 2;
2428
2429 /* data packet(s) transmitted (IN) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002430 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 unsigned len;
2432
2433 len = req->req.length - req->req.actual;
2434 if (len > ep->ep.maxpacket)
2435 len = ep->ep.maxpacket;
2436 req->req.actual += len;
2437
2438 /* if we wrote it all, we're usually done */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002439 /* send zlps until the status stage */
2440 if ((req->req.actual == req->req.length) &&
2441 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 mode = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 /* there was nothing to do ... */
2445 } else if (mode == 1)
2446 return;
2447
2448 /* done */
2449 if (mode == 2) {
2450 /* stream endpoints often resubmit/unlink in completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002451 done(ep, req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 /* maybe advance queue to next request */
2454 if (ep->num == 0) {
2455 /* NOTE: net2280 could let gadget driver start the
2456 * status stage later. since not all controllers let
2457 * them control that, the api doesn't (yet) allow it.
2458 */
2459 if (!ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002460 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 req = NULL;
2462 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002463 if (!list_empty(&ep->queue) && !ep->stopped)
2464 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct net2280_request, queue);
2466 else
2467 req = NULL;
2468 if (req && !ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002469 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471 }
2472
2473 /* is there a buffer for the next packet?
2474 * for best streaming performance, make sure there is one.
2475 */
2476 if (req && !ep->stopped) {
2477
2478 /* load IN fifo with next packet (may be zlp) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002479 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002480 write_fifo(ep, &req->req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482}
2483
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002484static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
2486 struct net2280_ep *ep;
2487
2488 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002489 return &dev->ep[0];
2490 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 u8 bEndpointAddress;
2492
2493 if (!ep->desc)
2494 continue;
2495 bEndpointAddress = ep->desc->bEndpointAddress;
2496 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2497 continue;
2498 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2499 return ep;
2500 }
2501 return NULL;
2502}
2503
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002504static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2505{
2506 u32 scratch, fsmvalue;
2507 u32 ack_wait_timeout, state;
2508
2509 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2510 scratch = get_idx_reg(dev->regs, SCRATCH);
2511 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2512 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2513
2514 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2515 (r.bRequestType & USB_DIR_IN)))
2516 return;
2517
2518 /* This is the first Control Read for this connection: */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002519 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002520 /*
2521 * Connection is NOT SS:
2522 * - Connection must be FS or HS.
2523 * - This FSM state should allow workaround software to
2524 * run after the next USB connection.
2525 */
2526 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002527 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002528 goto restore_data_eps;
2529 }
2530
2531 /* Connection is SS: */
2532 for (ack_wait_timeout = 0;
2533 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2534 ack_wait_timeout++) {
2535
2536 state = readl(&dev->plregs->pl_ep_status_1)
2537 & (0xff << STATE);
2538 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2539 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2540 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002541 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002542 break;
2543 }
2544
2545 /*
2546 * We have not yet received host's Data Phase ACK
2547 * - Wait and try again.
2548 */
2549 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2550
2551 continue;
2552 }
2553
2554
2555 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002556 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002557 "to detect SS host's data phase ACK.");
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002558 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002559 "got 0x%2.2x.\n", state >> STATE);
2560 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002561 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002562 "%duSec for Control Read Data Phase ACK\n",
2563 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2564 }
2565
2566restore_data_eps:
2567 /*
2568 * Restore data EPs to their pre-workaround settings (disabled,
2569 * initialized, and other details).
2570 */
2571 defect7374_disable_data_eps(dev);
2572
2573 set_idx_reg(dev->regs, SCRATCH, scratch);
2574
2575 return;
2576}
2577
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002578static void ep_clear_seqnum(struct net2280_ep *ep)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002579{
2580 struct net2280 *dev = ep->dev;
2581 u32 val;
2582 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2583
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002584 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2585 val |= ep_pl[ep->num];
2586 writel(val, &dev->plregs->pl_ep_ctrl);
2587 val |= BIT(SEQUENCE_NUMBER_RESET);
2588 writel(val, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002589
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002590 return;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002591}
2592
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002593static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2594 struct net2280_ep *ep, struct usb_ctrlrequest r)
2595{
2596 int tmp = 0;
2597
2598#define w_value le16_to_cpu(r.wValue)
2599#define w_index le16_to_cpu(r.wIndex)
2600#define w_length le16_to_cpu(r.wLength)
2601
2602 switch (r.bRequest) {
2603 struct net2280_ep *e;
2604 u16 status;
2605
2606 case USB_REQ_SET_CONFIGURATION:
2607 dev->addressed_state = !w_value;
2608 goto usb3_delegate;
2609
2610 case USB_REQ_GET_STATUS:
2611 switch (r.bRequestType) {
2612 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2613 status = dev->wakeup_enable ? 0x02 : 0x00;
2614 if (dev->selfpowered)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002615 status |= BIT(0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002616 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2617 dev->ltm_enable << 4);
2618 writel(0, &dev->epregs[0].ep_irqenb);
2619 set_fifo_bytecount(ep, sizeof(status));
2620 writel((__force u32) status, &dev->epregs[0].ep_data);
2621 allow_status_338x(ep);
2622 break;
2623
2624 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2625 e = get_ep_by_addr(dev, w_index);
2626 if (!e)
2627 goto do_stall3;
2628 status = readl(&e->regs->ep_rsp) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002629 BIT(CLEAR_ENDPOINT_HALT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002630 writel(0, &dev->epregs[0].ep_irqenb);
2631 set_fifo_bytecount(ep, sizeof(status));
2632 writel((__force u32) status, &dev->epregs[0].ep_data);
2633 allow_status_338x(ep);
2634 break;
2635
2636 default:
2637 goto usb3_delegate;
2638 }
2639 break;
2640
2641 case USB_REQ_CLEAR_FEATURE:
2642 switch (r.bRequestType) {
2643 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2644 if (!dev->addressed_state) {
2645 switch (w_value) {
2646 case USB_DEVICE_U1_ENABLE:
2647 dev->u1_enable = 0;
2648 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002649 ~BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002650 &dev->usb_ext->usbctl2);
2651 allow_status_338x(ep);
2652 goto next_endpoints3;
2653
2654 case USB_DEVICE_U2_ENABLE:
2655 dev->u2_enable = 0;
2656 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002657 ~BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002658 &dev->usb_ext->usbctl2);
2659 allow_status_338x(ep);
2660 goto next_endpoints3;
2661
2662 case USB_DEVICE_LTM_ENABLE:
2663 dev->ltm_enable = 0;
2664 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002665 ~BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002666 &dev->usb_ext->usbctl2);
2667 allow_status_338x(ep);
2668 goto next_endpoints3;
2669
2670 default:
2671 break;
2672 }
2673 }
2674 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2675 dev->wakeup_enable = 0;
2676 writel(readl(&dev->usb->usbctl) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002677 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002678 &dev->usb->usbctl);
2679 allow_status_338x(ep);
2680 break;
2681 }
2682 goto usb3_delegate;
2683
2684 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2685 e = get_ep_by_addr(dev, w_index);
2686 if (!e)
2687 goto do_stall3;
2688 if (w_value != USB_ENDPOINT_HALT)
2689 goto do_stall3;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002690 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002691 /*
2692 * Workaround for SS SeqNum not cleared via
2693 * Endpoint Halt (Clear) bit. select endpoint
2694 */
2695 ep_clear_seqnum(e);
2696 clear_halt(e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002697 if (!list_empty(&e->queue) && e->td_dma)
2698 restart_dma(e);
2699 allow_status(ep);
2700 ep->stopped = 1;
2701 break;
2702
2703 default:
2704 goto usb3_delegate;
2705 }
2706 break;
2707 case USB_REQ_SET_FEATURE:
2708 switch (r.bRequestType) {
2709 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2710 if (!dev->addressed_state) {
2711 switch (w_value) {
2712 case USB_DEVICE_U1_ENABLE:
2713 dev->u1_enable = 1;
2714 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002715 BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002716 &dev->usb_ext->usbctl2);
2717 allow_status_338x(ep);
2718 goto next_endpoints3;
2719
2720 case USB_DEVICE_U2_ENABLE:
2721 dev->u2_enable = 1;
2722 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002723 BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002724 &dev->usb_ext->usbctl2);
2725 allow_status_338x(ep);
2726 goto next_endpoints3;
2727
2728 case USB_DEVICE_LTM_ENABLE:
2729 dev->ltm_enable = 1;
2730 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002731 BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002732 &dev->usb_ext->usbctl2);
2733 allow_status_338x(ep);
2734 goto next_endpoints3;
2735 default:
2736 break;
2737 }
2738 }
2739
2740 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2741 dev->wakeup_enable = 1;
2742 writel(readl(&dev->usb->usbctl) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002743 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002744 &dev->usb->usbctl);
2745 allow_status_338x(ep);
2746 break;
2747 }
2748 goto usb3_delegate;
2749
2750 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2751 e = get_ep_by_addr(dev, w_index);
2752 if (!e || (w_value != USB_ENDPOINT_HALT))
2753 goto do_stall3;
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002754 ep->stopped = 1;
2755 if (ep->num == 0)
2756 ep->dev->protocol_stall = 1;
2757 else {
2758 if (ep->dma)
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01002759 abort_dma(ep);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002760 set_halt(ep);
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002761 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002762 allow_status_338x(ep);
2763 break;
2764
2765 default:
2766 goto usb3_delegate;
2767 }
2768
2769 break;
2770 default:
2771
2772usb3_delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002773 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002774 r.bRequestType, r.bRequest,
2775 w_value, w_index, w_length,
2776 readl(&ep->cfg->ep_cfg));
2777
2778 ep->responded = 0;
2779 spin_unlock(&dev->lock);
2780 tmp = dev->driver->setup(&dev->gadget, &r);
2781 spin_lock(&dev->lock);
2782 }
2783do_stall3:
2784 if (tmp < 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002785 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002786 r.bRequestType, r.bRequest, tmp);
2787 dev->protocol_stall = 1;
2788 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002789 set_halt(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002790 }
2791
2792next_endpoints3:
2793
2794#undef w_value
2795#undef w_index
2796#undef w_length
2797
2798 return;
2799}
2800
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002801static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
2803 struct net2280_ep *ep;
2804 u32 num, scratch;
2805
2806 /* most of these don't need individual acks */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002807 stat &= ~BIT(INTA_ASSERTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (!stat)
2809 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002810 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 /* starting a control request? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002813 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 union {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002815 u32 raw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct usb_ctrlrequest r;
2817 } u;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002818 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 struct net2280_request *req;
2820
2821 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002822 u32 val = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002823 if (val & BIT(SUPER_SPEED)) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002824 dev->gadget.speed = USB_SPEED_SUPER;
2825 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2826 EP0_SS_MAX_PACKET_SIZE);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002827 } else if (val & BIT(HIGH_SPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 dev->gadget.speed = USB_SPEED_HIGH;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002829 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2830 EP0_HS_MAX_PACKET_SIZE);
2831 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 dev->gadget.speed = USB_SPEED_FULL;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002833 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2834 EP0_HS_MAX_PACKET_SIZE);
2835 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002836 net2280_led_speed(dev, dev->gadget.speed);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002837 ep_dbg(dev, "%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002838 usb_speed_string(dev->gadget.speed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
2840
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002841 ep = &dev->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 ep->irqs++;
2843
2844 /* make sure any leftover request state is cleared */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002845 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002846 while (!list_empty(&ep->queue)) {
2847 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct net2280_request, queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002849 done(ep, req, (req->req.actual == req->req.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 ? 0 : -EPROTO);
2851 }
2852 ep->stopped = 0;
2853 dev->protocol_stall = 0;
Ricardo Ribalda Delgado5d1b6842014-11-28 14:50:53 +01002854 if (!(dev->quirks & PLX_SUPERSPEED)) {
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002855 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002856 tmp = BIT(FIFO_OVERFLOW) |
2857 BIT(FIFO_UNDERFLOW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002858 else
2859 tmp = 0;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002860
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002861 writel(tmp | BIT(TIMEOUT) |
2862 BIT(USB_STALL_SENT) |
2863 BIT(USB_IN_NAK_SENT) |
2864 BIT(USB_IN_ACK_RCVD) |
2865 BIT(USB_OUT_PING_NAK_SENT) |
2866 BIT(USB_OUT_ACK_SENT) |
2867 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
2868 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
2869 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2870 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2871 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002872 BIT(DATA_IN_TOKEN_INTERRUPT),
2873 &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002874 }
2875 u.raw[0] = readl(&dev->usb->setup0123);
2876 u.raw[1] = readl(&dev->usb->setup4567);
David Brownell901b3d72006-09-02 03:13:45 -07002877
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002878 cpu_to_le32s(&u.raw[0]);
2879 cpu_to_le32s(&u.raw[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002881 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002882 defect7374_workaround(dev, u.r);
2883
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002884 tmp = 0;
2885
David Brownell01ee7d72007-05-25 20:40:14 -07002886#define w_value le16_to_cpu(u.r.wValue)
2887#define w_index le16_to_cpu(u.r.wIndex)
2888#define w_length le16_to_cpu(u.r.wLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 /* ack the irq */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002891 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
2892 stat ^= BIT(SETUP_PACKET_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 /* watch control traffic at the token level, and force
2895 * synchronization before letting the status stage happen.
2896 * FIXME ignore tokens we'll NAK, until driver responds.
2897 * that'll mean a lot less irqs for some drivers.
2898 */
2899 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2900 if (ep->is_in) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002901 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2902 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2903 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002904 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 } else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002906 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2907 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2908 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002909 writel(scratch, &dev->epregs[0].ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 /* we made the hardware handle most lowlevel requests;
2912 * everything else goes uplevel to the gadget code.
2913 */
Alan Stern1f26e282006-11-16 10:16:00 -05002914 ep->responded = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002915
2916 if (dev->gadget.speed == USB_SPEED_SUPER) {
2917 handle_stat0_irqs_superspeed(dev, ep, u.r);
2918 goto next_endpoints;
2919 }
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 switch (u.r.bRequest) {
2922 case USB_REQ_GET_STATUS: {
2923 struct net2280_ep *e;
David Brownell320f3452005-05-07 13:05:18 -07002924 __le32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 /* hw handles device and interface status */
2927 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2928 goto delegate;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002929 e = get_ep_by_addr(dev, w_index);
2930 if (!e || w_length > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 goto do_stall;
2932
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002933 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002934 status = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002936 status = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938 /* don't bother with a request object! */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002939 writel(0, &dev->epregs[0].ep_irqenb);
2940 set_fifo_bytecount(ep, w_length);
2941 writel((__force u32)status, &dev->epregs[0].ep_data);
2942 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002943 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 goto next_endpoints;
2945 }
2946 break;
2947 case USB_REQ_CLEAR_FEATURE: {
2948 struct net2280_ep *e;
2949
2950 /* hw handles device features */
2951 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2952 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002953 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002955 e = get_ep_by_addr(dev, w_index);
2956 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04002958 if (e->wedged) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002959 ep_vdbg(dev, "%s wedged, halt not cleared\n",
Alan Stern80661342008-08-14 15:49:11 -04002960 ep->ep.name);
2961 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002962 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Alan Stern80661342008-08-14 15:49:11 -04002963 clear_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002964 if ((ep->dev->quirks & PLX_SUPERSPEED) &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002965 !list_empty(&e->queue) && e->td_dma)
2966 restart_dma(e);
Alan Stern80661342008-08-14 15:49:11 -04002967 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002968 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 goto next_endpoints;
2970 }
2971 break;
2972 case USB_REQ_SET_FEATURE: {
2973 struct net2280_ep *e;
2974
2975 /* hw handles device features */
2976 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2977 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002978 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002980 e = get_ep_by_addr(dev, w_index);
2981 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04002983 if (e->ep.name == ep0name)
2984 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002985 set_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002986 if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002987 abort_dma(e);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002988 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002989 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 goto next_endpoints;
2991 }
2992 break;
2993 default:
2994delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002995 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 "ep_cfg %08x\n",
2997 u.r.bRequestType, u.r.bRequest,
David Brownell320f3452005-05-07 13:05:18 -07002998 w_value, w_index, w_length,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002999 readl(&ep->cfg->ep_cfg));
Alan Stern1f26e282006-11-16 10:16:00 -05003000 ep->responded = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003001 spin_unlock(&dev->lock);
3002 tmp = dev->driver->setup(&dev->gadget, &u.r);
3003 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005
3006 /* stall ep0 on error */
3007 if (tmp < 0) {
3008do_stall:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003009 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 u.r.bRequestType, u.r.bRequest, tmp);
3011 dev->protocol_stall = 1;
3012 }
3013
3014 /* some in/out token irq should follow; maybe stall then.
3015 * driver must queue a request (even zlp) or halt ep0
3016 * before the host times out.
3017 */
3018 }
3019
David Brownell320f3452005-05-07 13:05:18 -07003020#undef w_value
3021#undef w_index
3022#undef w_length
3023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024next_endpoints:
3025 /* endpoint data irq ? */
3026 scratch = stat & 0x7f;
3027 stat &= ~0x7f;
3028 for (num = 0; scratch; num++) {
3029 u32 t;
3030
3031 /* do this endpoint's FIFO and queue need tending? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003032 t = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if ((scratch & t) == 0)
3034 continue;
3035 scratch ^= t;
3036
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003037 ep = &dev->ep[num];
3038 handle_ep_small(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
3040
3041 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003042 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003045#define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3046 BIT(DMA_C_INTERRUPT) | \
3047 BIT(DMA_B_INTERRUPT) | \
3048 BIT(DMA_A_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049#define PCI_ERROR_INTERRUPTS ( \
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003050 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3051 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3052 BIT(PCI_RETRY_ABORT_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003054static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 struct net2280_ep *ep;
3057 u32 tmp, num, mask, scratch;
3058
3059 /* after disconnect there's nothing else to do! */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003060 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3061 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03003064 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
David Brownell901b3d72006-09-02 03:13:45 -07003065 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 * only indicates a change in the reset state).
3067 */
3068 if (stat & tmp) {
Alan Sternb611e422014-11-06 14:27:56 +08003069 bool reset = false;
3070 bool disconnect = false;
3071
3072 /*
3073 * Ignore disconnects and resets if the speed hasn't been set.
3074 * VBUS can bounce and there's always an initial reset.
3075 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003076 writel(tmp, &dev->regs->irqstat1);
Alan Sternb611e422014-11-06 14:27:56 +08003077 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3078 if ((stat & BIT(VBUS_INTERRUPT)) &&
3079 (readl(&dev->usb->usbctl) &
3080 BIT(VBUS_PIN)) == 0) {
3081 disconnect = true;
3082 ep_dbg(dev, "disconnect %s\n",
3083 dev->driver->driver.name);
3084 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3085 (readl(&dev->usb->usbstat) & mask)
3086 == 0) {
3087 reset = true;
3088 ep_dbg(dev, "reset %s\n",
3089 dev->driver->driver.name);
3090 }
3091
3092 if (disconnect || reset) {
3093 stop_activity(dev, dev->driver);
3094 ep0_start(dev);
3095 spin_unlock(&dev->lock);
3096 if (reset)
3097 usb_gadget_udc_reset
3098 (&dev->gadget, dev->driver);
3099 else
3100 (dev->driver->disconnect)
3101 (&dev->gadget);
3102 spin_lock(&dev->lock);
3103 return;
3104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106 stat &= ~tmp;
3107
3108 /* vBUS can bounce ... one of many reasons to ignore the
3109 * notion of hotplug events on bus connect/disconnect!
3110 */
3111 if (!stat)
3112 return;
3113 }
3114
3115 /* NOTE: chip stays in PCI D0 state for now, but it could
3116 * enter D1 to save more power
3117 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003118 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (stat & tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003120 writel(tmp, &dev->regs->irqstat1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003121 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (dev->driver->suspend)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003123 dev->driver->suspend(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (!enable_suspend)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003125 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 } else {
3127 if (dev->driver->resume)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003128 dev->driver->resume(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /* at high speed, note erratum 0133 */
3130 }
3131 stat &= ~tmp;
3132 }
3133
3134 /* clear any other status/irqs */
3135 if (stat)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003136 writel(stat, &dev->regs->irqstat1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 /* some status we can just ignore */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003139 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003140 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3141 BIT(SUSPEND_REQUEST_INTERRUPT) |
3142 BIT(RESUME_INTERRUPT) |
3143 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003144 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003145 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3146 BIT(RESUME_INTERRUPT) |
3147 BIT(SOF_DOWN_INTERRUPT) |
3148 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (!stat)
3151 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003152 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 /* DMA status, for ep-{a,b,c,d} */
3155 scratch = stat & DMA_INTERRUPTS;
3156 stat &= ~DMA_INTERRUPTS;
3157 scratch >>= 9;
3158 for (num = 0; scratch; num++) {
3159 struct net2280_dma_regs __iomem *dma;
3160
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003161 tmp = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if ((tmp & scratch) == 0)
3163 continue;
3164 scratch ^= tmp;
3165
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003166 ep = &dev->ep[num + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 dma = ep->dma;
3168
3169 if (!dma)
3170 continue;
3171
3172 /* clear ep's dma status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003173 tmp = readl(&dma->dmastat);
3174 writel(tmp, &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003176 /* dma sync*/
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003177 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003178 u32 r_dmacount = readl(&dma->dmacount);
3179 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003180 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003181 continue;
3182 }
3183
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003184 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3185 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3186 ep->ep.name, tmp);
3187 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003189 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 /* OUT transfers terminate when the data from the
3192 * host is in our memory. Process whatever's done.
3193 * On this path, we know transfer's last packet wasn't
3194 * less than req->length. NAK_OUT_PACKETS may be set,
3195 * or the FIFO may already be holding new packets.
3196 *
3197 * IN transfers can linger in the FIFO for a very
3198 * long time ... we ignore that for now, accounting
3199 * precisely (like PIO does) needs per-packet irqs
3200 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003201 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 /* disable dma on inactive queues; else maybe restart */
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003204 if (!list_empty(&ep->queue)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003205 tmp = readl(&dma->dmactl);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003206 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
3208 ep->irqs++;
3209 }
3210
3211 /* NOTE: there are other PCI errors we might usefully notice.
3212 * if they appear very often, here's where to try recovering.
3213 */
3214 if (stat & PCI_ERROR_INTERRUPTS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003215 ep_err(dev, "pci dma error; stat %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 stat &= ~PCI_ERROR_INTERRUPTS;
3217 /* these are fatal errors, but "maybe" they won't
3218 * happen again ...
3219 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003220 stop_activity(dev, dev->driver);
3221 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 stat = 0;
3223 }
3224
3225 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003226 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003229static irqreturn_t net2280_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 struct net2280 *dev = _dev;
3232
Alan Stern658ad5e2006-04-14 16:44:11 -04003233 /* shared interrupt, not ours */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003234 if ((dev->quirks & PLX_LEGACY) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003235 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
Alan Stern658ad5e2006-04-14 16:44:11 -04003236 return IRQ_NONE;
3237
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003238 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
3240 /* handle disconnect, dma, and more */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003241 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 /* control requests and PIO */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003244 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003246 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003247 /* re-enable interrupt to trigger any possible new interrupt */
3248 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3249 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3250 writel(pciirqenb1, &dev->regs->pciirqenb1);
3251 }
3252
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003253 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255 return IRQ_HANDLED;
3256}
3257
3258/*-------------------------------------------------------------------------*/
3259
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003260static void gadget_release(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003262 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003264 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
3267/* tear down the binding between this driver and the pci device */
3268
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003269static void net2280_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003271 struct net2280 *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003273 usb_del_gadget_udc(&dev->gadget);
3274
David Brownell6bea4762006-12-05 03:15:33 -08003275 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 /* then clean up the resources we allocated during probe() */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003278 net2280_led_shutdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (dev->requests) {
3280 int i;
3281 for (i = 1; i < 5; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003282 if (!dev->ep[i].dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003284 pci_pool_free(dev->requests, dev->ep[i].dummy,
3285 dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003287 pci_pool_destroy(dev->requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
3289 if (dev->got_irq)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003290 free_irq(pdev->irq, dev);
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003291 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003292 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (dev->regs)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003294 iounmap(dev->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (dev->region)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003296 release_mem_region(pci_resource_start(pdev, 0),
3297 pci_resource_len(pdev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (dev->enabled)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003299 pci_disable_device(pdev);
3300 device_remove_file(&pdev->dev, &dev_attr_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003302 ep_info(dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
3305/* wrap this driver around the specified device, but
3306 * don't respond over USB until a gadget driver binds to us.
3307 */
3308
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003309static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
3311 struct net2280 *dev;
3312 unsigned long resource, len;
3313 void __iomem *base = NULL;
3314 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /* alloc, and start init */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003317 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3318 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 retval = -ENOMEM;
3320 goto done;
3321 }
3322
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003323 pci_set_drvdata(pdev, dev);
3324 spin_lock_init(&dev->lock);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003325 dev->quirks = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 dev->pdev = pdev;
3327 dev->gadget.ops = &net2280_ops;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003328 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003329 USB_SPEED_SUPER : USB_SPEED_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
3331 /* the "gadget" abstracts/virtualizes the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 dev->gadget.name = driver_name;
3333
3334 /* now all the pci goodies ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003335 if (pci_enable_device(pdev) < 0) {
3336 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 goto done;
3338 }
3339 dev->enabled = 1;
3340
3341 /* BAR 0 holds all the registers
3342 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3343 * BAR 2 is fifo memory; unused here
3344 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003345 resource = pci_resource_start(pdev, 0);
3346 len = pci_resource_len(pdev, 0);
3347 if (!request_mem_region(resource, len, driver_name)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003348 ep_dbg(dev, "controller already in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 retval = -EBUSY;
3350 goto done;
3351 }
3352 dev->region = 1;
3353
David Brownell901b3d72006-09-02 03:13:45 -07003354 /* FIXME provide firmware download interface to put
3355 * 8051 code into the chip, e.g. to turn on PCI PM.
3356 */
3357
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003358 base = ioremap_nocache(resource, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if (base == NULL) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003360 ep_dbg(dev, "can't map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 retval = -EFAULT;
3362 goto done;
3363 }
3364 dev->regs = (struct net2280_regs __iomem *) base;
3365 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3366 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3367 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3368 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3369 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3370
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003371 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003372 u32 fsmvalue;
3373 u32 usbstat;
3374 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3375 (base + 0x00b4);
3376 dev->fiforegs = (struct usb338x_fifo_regs __iomem *)
3377 (base + 0x0500);
3378 dev->llregs = (struct usb338x_ll_regs __iomem *)
3379 (base + 0x0700);
3380 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3381 (base + 0x0748);
3382 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3383 (base + 0x077c);
3384 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3385 (base + 0x079c);
3386 dev->plregs = (struct usb338x_pl_regs __iomem *)
3387 (base + 0x0800);
3388 usbstat = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003389 dev->enhanced_mode = !!(usbstat & BIT(11));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003390 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3391 /* put into initial config, link up all endpoints */
3392 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3393 (0xf << DEFECT7374_FSM_FIELD);
3394 /* See if firmware needs to set up for workaround: */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003395 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3396 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003397 writel(0, &dev->usb->usbctl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003398 } else
3399 dev->bug7734_patched = 0;
3400 } else {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003401 dev->enhanced_mode = 0;
3402 dev->n_ep = 7;
3403 /* put into initial config, link up all endpoints */
3404 writel(0, &dev->usb->usbctl);
3405 }
3406
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003407 usb_reset(dev);
3408 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
3410 /* irq setup after old hardware is cleaned up */
3411 if (!pdev->irq) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003412 ep_err(dev, "No IRQ. Check PCI setup!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 retval = -ENODEV;
3414 goto done;
3415 }
David S. Millerc6387a42006-06-20 01:21:29 -07003416
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003417 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003418 if (pci_enable_msi(pdev))
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003419 ep_err(dev, "Failed to enable MSI mode\n");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003420
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003421 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3422 driver_name, dev)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003423 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 retval = -EBUSY;
3425 goto done;
3426 }
3427 dev->got_irq = 1;
3428
3429 /* DMA setup */
3430 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003431 dev->requests = pci_pool_create("requests", pdev,
3432 sizeof(struct net2280_dma),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 0 /* no alignment requirements */,
3434 0 /* or page-crossing issues */);
3435 if (!dev->requests) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003436 ep_dbg(dev, "can't get request pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 retval = -ENOMEM;
3438 goto done;
3439 }
3440 for (i = 1; i < 5; i++) {
3441 struct net2280_dma *td;
3442
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003443 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3444 &dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 if (!td) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003446 ep_dbg(dev, "can't get dummy %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 retval = -ENOMEM;
3448 goto done;
3449 }
3450 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 td->dmadesc = td->dmaaddr;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003452 dev->ep[i].dummy = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
3454
3455 /* enable lower-overhead pci memory bursts during DMA */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003456 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003457 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3458 /*
3459 * 256 write retries may not be enough...
3460 BIT(PCI_RETRY_ABORT_ENABLE) |
3461 */
3462 BIT(DMA_READ_MULTIPLE_ENABLE) |
3463 BIT(DMA_READ_LINE_ENABLE),
3464 &dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003466 pci_set_master(pdev);
3467 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
3469 /* ... also flushes any posted pci writes */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003470 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 /* done */
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003473 ep_info(dev, "%s\n", driver_desc);
3474 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
David S. Millerc6387a42006-06-20 01:21:29 -07003475 pdev->irq, base, dev->chiprev);
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01003476 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003477 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003478 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3479 if (retval)
3480 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Felipe Balbi2901df62013-02-26 15:15:27 +02003482 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3483 gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003484 if (retval)
3485 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return 0;
3487
3488done:
3489 if (dev)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003490 net2280_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return retval;
3492}
3493
Alan Stern2d61bde2006-05-05 16:23:42 -04003494/* make sure the board is quiescent; otherwise it will continue
3495 * generating IRQs across the upcoming reboot.
3496 */
3497
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003498static void net2280_shutdown(struct pci_dev *pdev)
Alan Stern2d61bde2006-05-05 16:23:42 -04003499{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003500 struct net2280 *dev = pci_get_drvdata(pdev);
Alan Stern2d61bde2006-05-05 16:23:42 -04003501
3502 /* disable IRQs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003503 writel(0, &dev->regs->pciirqenb0);
3504 writel(0, &dev->regs->pciirqenb1);
Alan Stern2d61bde2006-05-05 16:23:42 -04003505
3506 /* disable the pullup so the host will think we're gone */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003507 writel(0, &dev->usb->usbctl);
Alan Stern2f076072013-01-30 16:40:14 -05003508
Alan Stern2d61bde2006-05-05 16:23:42 -04003509}
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512/*-------------------------------------------------------------------------*/
3513
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003514static const struct pci_device_id pci_ids[] = { {
David Brownell901b3d72006-09-02 03:13:45 -07003515 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3516 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003517 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 .device = 0x2280,
3519 .subvendor = PCI_ANY_ID,
3520 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003521 .driver_data = PLX_LEGACY | PLX_2280,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003522 }, {
David Brownell901b3d72006-09-02 03:13:45 -07003523 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3524 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003525 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003526 .device = 0x2282,
3527 .subvendor = PCI_ANY_ID,
3528 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003529 .driver_data = PLX_LEGACY,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003530 },
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003531 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003532 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3533 .class_mask = ~0,
3534 .vendor = PCI_VENDOR_ID_PLX,
3535 .device = 0x3380,
3536 .subvendor = PCI_ANY_ID,
3537 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003538 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003539 },
3540 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003541 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3542 .class_mask = ~0,
3543 .vendor = PCI_VENDOR_ID_PLX,
3544 .device = 0x3382,
3545 .subvendor = PCI_ANY_ID,
3546 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003547 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003548 },
3549{ /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550};
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003551MODULE_DEVICE_TABLE(pci, pci_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553/* pci driver glue; this is a "new style" PCI driver module */
3554static struct pci_driver net2280_pci_driver = {
3555 .name = (char *) driver_name,
3556 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 .probe = net2280_probe,
3559 .remove = net2280_remove,
Alan Stern2d61bde2006-05-05 16:23:42 -04003560 .shutdown = net2280_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562 /* FIXME add power management support */
3563};
3564
Ricardo Ribalda Delgado9a028e42014-05-20 18:30:07 +02003565module_pci_driver(net2280_pci_driver);
3566
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003567MODULE_DESCRIPTION(DRIVER_DESC);
3568MODULE_AUTHOR("David Brownell");
3569MODULE_LICENSE("GPL");