blob: d4255f95c92f9e99a48acd9b51f148a1a7632d67 [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
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200578 ASSERT_OUT_NAKING(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 statp = &ep->regs->ep_stat;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200581 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200582 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
583 statp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200584 writel(BIT(FIFO_FLUSH), statp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200585 /* Make sure that stap is written */
586 mb();
587 tmp = readl(statp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200588 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* high speed did bulk NYET; fifo isn't filling */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200590 ep->dev->gadget.speed == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 unsigned usec;
592
593 usec = 50; /* 64 byte bulk/interrupt */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200594 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
595 BIT(USB_OUT_PING_NAK_SENT), usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
597 }
598}
599
600/* unload packet(s) from the fifo we use for usb OUT transfers.
601 * returns true iff the request completed, because of short packet
602 * or the request buffer having filled with full packets.
603 *
604 * for ep-a..ep-d this will read multiple packets out when they
605 * have been accepted.
606 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200607static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct net2280_ep_regs __iomem *regs = ep->regs;
610 u8 *buf = req->req.buf + req->req.actual;
611 unsigned count, tmp, is_short;
612 unsigned cleanup = 0, prevent = 0;
613
614 /* erratum 0106 ... packets coming in during fifo reads might
615 * be incompletely rejected. not all cases have workarounds.
616 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200617 if (ep->dev->chiprev == 0x0100 &&
618 ep->dev->gadget.speed == USB_SPEED_FULL) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200619 udelay(1);
620 tmp = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200621 if ((tmp & BIT(NAK_OUT_PACKETS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 cleanup = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200623 else if ((tmp & BIT(FIFO_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200624 start_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 prevent = 1;
626 }
627 /* else: hope we don't see the problem */
628 }
629
630 /* never overflow the rx buffer. the fifo reads packets until
631 * it sees a short one; we might not be ready for them all.
632 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200633 prefetchw(buf);
634 count = readl(&regs->ep_avail);
635 if (unlikely(count == 0)) {
636 udelay(1);
637 tmp = readl(&ep->regs->ep_stat);
638 count = readl(&regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* handled that data already? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200640 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642 }
643
644 tmp = req->req.length - req->req.actual;
645 if (count > tmp) {
646 /* as with DMA, data overflow gets flushed */
647 if ((tmp % ep->ep.maxpacket) != 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200648 ep_err(ep->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 "%s out fifo %d bytes, expected %d\n",
650 ep->ep.name, count, tmp);
651 req->req.status = -EOVERFLOW;
652 cleanup = 1;
653 /* NAK_OUT_PACKETS will be set, so flushing is safe;
654 * the next read will start with the next packet
655 */
656 } /* else it's a ZLP, no worries */
657 count = tmp;
658 }
659 req->req.actual += count;
660
661 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
662
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200663 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 -0700664 ep->ep.name, count, is_short ? " (short)" : "",
665 cleanup ? " flush" : "", prevent ? " nak" : "",
666 req, req->req.actual, req->req.length);
667
668 while (count >= 4) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200669 tmp = readl(&regs->ep_data);
670 cpu_to_le32s(&tmp);
671 put_unaligned(tmp, (u32 *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 buf += 4;
673 count -= 4;
674 }
675 if (count) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200676 tmp = readl(&regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* LE conversion is implicit here: */
678 do {
679 *buf++ = (u8) tmp;
680 tmp >>= 8;
681 } while (--count);
682 }
683 if (cleanup)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200684 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (prevent) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200686 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200687 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200690 return is_short || ((req->req.actual == req->req.length) &&
691 !req->req.zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694/* fill out dma descriptor to match a given request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200695static void fill_dma_desc(struct net2280_ep *ep,
696 struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 struct net2280_dma *td = req->td;
699 u32 dmacount = req->req.length;
700
701 /* don't let DMA continue after a short OUT packet,
702 * so overruns can't affect the next transfer.
703 * in case of overruns on max-size packets, we can't
704 * stop the fifo from filling but we can flush it.
705 */
706 if (ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200707 dmacount |= BIT(DMA_DIRECTION);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200708 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200709 !(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200710 dmacount |= BIT(END_OF_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 req->valid = valid;
713 if (valid)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200714 dmacount |= BIT(VALID_BIT);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100715 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* td->dmadesc = previously set by caller */
718 td->dmaaddr = cpu_to_le32 (req->req.dma);
719
720 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200721 wmb();
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700722 td->dmacount = cpu_to_le32(dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725static const u32 dmactl_default =
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200726 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
727 BIT(DMA_CLEAR_COUNT_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* erratum 0116 workaround part 1 (use POLLING) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200729 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
730 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
731 BIT(DMA_VALID_BIT_ENABLE) |
732 BIT(DMA_SCATTER_GATHER_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* erratum 0116 workaround part 2 (no AUTOSTART) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200734 BIT(DMA_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200736static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200738 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200741static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200743 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200744 spin_stop_dma(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200747static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct net2280_dma_regs __iomem *dma = ep->dma;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200750 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200752 if (!(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200753 tmp |= BIT(END_OF_CHAIN);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100754
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200755 writel(tmp, &dma->dmacount);
756 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200758 writel(td_dma, &dma->dmadesc);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200759 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200760 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200761 writel(dmactl, &dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200764 (void) readl(&ep->dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200766 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (!ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200769 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200772static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 u32 tmp;
775 struct net2280_dma_regs __iomem *dma = ep->dma;
776
777 /* FIXME can't use DMA for ZLPs */
778
779 /* on this path we "know" there's no dma active (yet) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200780 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200781 writel(0, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* previous OUT packet might have been short */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200784 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
785 BIT(NAK_OUT_PACKETS))) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200786 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 &ep->regs->ep_stat);
788
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200789 tmp = readl(&ep->regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200791 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* transfer all/some fifo data */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200794 writel(req->req.dma, &dma->dmaaddr);
795 tmp = min(tmp, req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* dma irq, faking scatterlist status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200798 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200799 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
800 &dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 req->td->dmadesc = 0;
802 req->valid = 1;
803
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200804 writel(BIT(DMA_ENABLE), &dma->dmactl);
805 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return;
807 }
808 }
809
810 tmp = dmactl_default;
811
812 /* force packet boundaries between dma requests, but prevent the
813 * controller from automagically writing a last "short" packet
814 * (zero length) unless the driver explicitly said to do that.
815 */
816 if (ep->is_in) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200817 if (likely((req->req.length % ep->ep.maxpacket) ||
818 req->req.zero)){
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200819 tmp |= BIT(DMA_FIFO_VALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 ep->in_fifo_validate = 1;
821 } else
822 ep->in_fifo_validate = 0;
823 }
824
825 /* init req->td, pointing to the current dummy */
826 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200827 fill_dma_desc(ep, req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100829 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200831 start_queue(ep, tmp, req->td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200834static inline void resume_dma(struct net2280_ep *ep)
835{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200836 writel(readl(&ep->dma->dmactl) | BIT(DMA_ENABLE), &ep->dma->dmactl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200840queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct net2280_dma *end;
843 dma_addr_t tmp;
844
845 /* swap new dummy for old, link; fill and maybe activate */
846 end = ep->dummy;
847 ep->dummy = req->td;
848 req->td = end;
849
850 tmp = ep->td_dma;
851 ep->td_dma = req->td_dma;
852 req->td_dma = tmp;
853
854 end->dmadesc = cpu_to_le32 (ep->td_dma);
855
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200856 fill_dma_desc(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859static void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200860done(struct net2280_ep *ep, struct net2280_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct net2280 *dev;
863 unsigned stopped = ep->stopped;
864
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200865 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (req->req.status == -EINPROGRESS)
868 req->req.status = status;
869 else
870 status = req->req.status;
871
872 dev = ep->dev;
Felipe Balbiae4d7932011-12-19 12:09:56 +0200873 if (ep->dma)
874 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 if (status && status != -ESHUTDOWN)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200877 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ep->ep.name, &req->req, status,
879 req->req.actual, req->req.length);
880
881 /* don't modify queue heads during completion callback */
882 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200883 spin_unlock(&dev->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200884 usb_gadget_giveback_request(&ep->ep, &req->req);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200885 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 ep->stopped = stopped;
887}
888
889/*-------------------------------------------------------------------------*/
890
891static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200892net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 struct net2280_request *req;
895 struct net2280_ep *ep;
896 struct net2280 *dev;
897 unsigned long flags;
898
899 /* we always require a cpu-view buffer, so that we can
900 * always use pio (as fallback or whatever).
901 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200902 req = container_of(_req, struct net2280_request, req);
903 if (!_req || !_req->complete || !_req->buf ||
904 !list_empty(&req->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
907 return -EDOM;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200908 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!_ep || (!ep->desc && ep->num != 0))
910 return -EINVAL;
911 dev = ep->dev;
912 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
913 return -ESHUTDOWN;
914
915 /* FIXME implement PIO fallback for ZLPs with DMA */
916 if (ep->dma && _req->length == 0)
917 return -EOPNOTSUPP;
918
919 /* set up dma mapping in case the caller didn't */
Felipe Balbiae4d7932011-12-19 12:09:56 +0200920 if (ep->dma) {
921 int ret;
922
923 ret = usb_gadget_map_request(&dev->gadget, _req,
924 ep->is_in);
925 if (ret)
926 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929#if 0
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);
932#endif
933
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200934 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 _req->status = -EINPROGRESS;
937 _req->actual = 0;
938
939 /* kickstart this i/o queue? */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100940 if (list_empty(&ep->queue) && !ep->stopped &&
941 !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
942 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* use DMA if the endpoint supports it, else pio */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100945 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200946 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 else {
948 /* maybe there's no control data, just status ack */
949 if (ep->num == 0 && _req->length == 0) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200950 allow_status(ep);
951 done(ep, req, 0);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200952 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 goto done;
954 }
955
956 /* PIO ... stuff the fifo, or unblock it. */
957 if (ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200958 write_fifo(ep, _req);
959 else if (list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 u32 s;
961
962 /* OUT FIFO might have packet(s) buffered */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200963 s = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200964 if ((s & BIT(FIFO_EMPTY)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* note: _req->short_not_ok is
966 * ignored here since PIO _always_
967 * stops queue advance here, and
968 * _req->status doesn't change for
969 * short reads (only _req->actual)
970 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200971 if (read_fifo(ep, req) &&
972 ep->num == 0) {
973 done(ep, req, 0);
974 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* don't queue it */
976 req = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200977 } else if (read_fifo(ep, req) &&
978 ep->num != 0) {
979 done(ep, req, 0);
980 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200982 s = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
985 /* don't NAK, let the fifo fill */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200986 if (req && (s & BIT(NAK_OUT_PACKETS)))
987 writel(BIT(CLEAR_NAK_OUT_PACKETS),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 &ep->regs->ep_rsp);
989 }
990 }
991
992 } else if (ep->dma) {
993 int valid = 1;
994
995 if (ep->is_in) {
996 int expect;
997
998 /* preventing magic zlps is per-engine state, not
999 * per-transfer; irq logic must recover hiccups.
1000 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001001 expect = likely(req->req.zero ||
1002 (req->req.length % ep->ep.maxpacket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (expect != ep->in_fifo_validate)
1004 valid = 0;
1005 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001006 queue_dma(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 } /* else the irq handler advances the queue. */
1009
Alan Stern1f26e282006-11-16 10:16:00 -05001010 ep->responded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001012 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001014 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /* pci writes may still be posted */
1017 return 0;
1018}
1019
1020static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001021dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1022 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001025 done(ep, req, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001028static void scan_dma_completions(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 /* only look at descriptors that were "naturally" retired,
1031 * so fifo and list head state won't matter
1032 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001033 while (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct net2280_request *req;
1035 u32 tmp;
1036
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001037 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct net2280_request, queue);
1039 if (!req->valid)
1040 break;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001041 rmb();
1042 tmp = le32_to_cpup(&req->td->dmacount);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001043 if ((tmp & BIT(VALID_BIT)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 break;
1045
1046 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1047 * cases where DMA must be aborted; this code handles
1048 * all non-abort DMA completions.
1049 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001050 if (unlikely(req->td->dmadesc == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* paranoia */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001052 tmp = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (tmp & DMA_BYTE_COUNT_MASK)
1054 break;
1055 /* single transfer mode */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001056 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02001058 } else if (!ep->is_in &&
1059 (req->req.length % ep->ep.maxpacket) != 0) {
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001060 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001061 return dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Mario Schuknecht18a4e652014-11-16 21:21:45 +01001063 tmp = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* AVOID TROUBLE HERE by not issuing short reads from
1065 * your gadget driver. That helps avoids errata 0121,
1066 * 0122, and 0124; not all cases trigger the warning.
1067 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001068 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001069 ep_warn(ep->dev, "%s lost packet sync!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ep->ep.name);
1071 req->req.status = -EOVERFLOW;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001072 } else {
1073 tmp = readl(&ep->regs->ep_avail);
1074 if (tmp) {
1075 /* fifo gets flushed later */
1076 ep->out_overflow = 1;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001077 ep_dbg(ep->dev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001078 "%s dma, discard %d len %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ep->ep.name, tmp,
1080 req->req.length);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001081 req->req.status = -EOVERFLOW;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001085 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087}
1088
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001089static void restart_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct net2280_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (ep->stopped)
1094 return;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001095 req = list_entry(ep->queue.next, struct net2280_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01001097 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01001100static void abort_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 /* abort the current transfer */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001103 if (likely(!list_empty(&ep->queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* FIXME work around errata 0121, 0122, 0124 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001105 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001106 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001108 stop_dma(ep->dma);
1109 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112/* dequeue ALL requests */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001113static void nuke(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct net2280_request *req;
1116
1117 /* called with spinlock held */
1118 ep->stopped = 1;
1119 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001120 abort_dma(ep);
1121 while (!list_empty(&ep->queue)) {
1122 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct net2280_request,
1124 queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001125 done(ep, req, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127}
1128
1129/* dequeue JUST ONE request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001130static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct net2280_ep *ep;
1133 struct net2280_request *req;
1134 unsigned long flags;
1135 u32 dmactl;
1136 int stopped;
1137
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001138 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1140 return -EINVAL;
1141
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001142 spin_lock_irqsave(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 stopped = ep->stopped;
1144
1145 /* quiesce dma while we patch the queue */
1146 dmactl = 0;
1147 ep->stopped = 1;
1148 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001149 dmactl = readl(&ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* WARNING erratum 0127 may kick in ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001151 stop_dma(ep->dma);
1152 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
1155 /* make sure it's still queued on this endpoint */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001156 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (&req->req == _req)
1158 break;
1159 }
1160 if (&req->req != _req) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001161 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -EINVAL;
1163 }
1164
1165 /* queue head may be partially complete. */
1166 if (ep->queue.next == &req->queue) {
1167 if (ep->dma) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001168 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 _req->status = -ECONNRESET;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001170 abort_dma(ep);
1171 if (likely(ep->queue.next == &req->queue)) {
1172 /* NOTE: misreports single-transfer mode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 req->td->dmacount = 0; /* invalidate */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001174 dma_done(ep, req,
1175 readl(&ep->dma->dmacount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 -ECONNRESET);
1177 }
1178 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001179 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001180 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
1185 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001186 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 ep->stopped = stopped;
1188
1189 if (ep->dma) {
1190 /* turn off dma on inactive queues */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001191 if (list_empty(&ep->queue))
1192 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 else if (!ep->stopped) {
1194 /* resume current request, or start new one */
1195 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001196 writel(dmactl, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001198 start_dma(ep, list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 struct net2280_request, queue));
1200 }
1201 }
1202
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001203 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return 0;
1205}
1206
1207/*-------------------------------------------------------------------------*/
1208
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001209static int net2280_fifo_status(struct usb_ep *_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211static int
Alan Stern80661342008-08-14 15:49:11 -04001212net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct net2280_ep *ep;
1215 unsigned long flags;
1216 int retval = 0;
1217
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001218 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (!_ep || (!ep->desc && ep->num != 0))
1220 return -EINVAL;
1221 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1222 return -ESHUTDOWN;
1223 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1224 == USB_ENDPOINT_XFER_ISOC)
1225 return -EINVAL;
1226
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001227 spin_lock_irqsave(&ep->dev->lock, flags);
1228 if (!list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 retval = -EAGAIN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001230 else if (ep->is_in && value && net2280_fifo_status(_ep) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 retval = -EAGAIN;
1232 else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001233 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
Alan Stern80661342008-08-14 15:49:11 -04001234 value ? "set" : "clear",
1235 wedged ? "wedge" : "halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* set/clear, then synch memory views with the device */
1237 if (value) {
1238 if (ep->num == 0)
1239 ep->dev->protocol_stall = 1;
1240 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001241 set_halt(ep);
Alan Stern80661342008-08-14 15:49:11 -04001242 if (wedged)
1243 ep->wedged = 1;
1244 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001245 clear_halt(ep);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001246 if (ep->dev->quirks & PLX_SUPERSPEED &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001247 !list_empty(&ep->queue) && ep->td_dma)
1248 restart_dma(ep);
Alan Stern80661342008-08-14 15:49:11 -04001249 ep->wedged = 0;
1250 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001251 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001253 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 return retval;
1256}
1257
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001258static int net2280_set_halt(struct usb_ep *_ep, int value)
Alan Stern80661342008-08-14 15:49:11 -04001259{
1260 return net2280_set_halt_and_wedge(_ep, value, 0);
1261}
1262
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001263static int net2280_set_wedge(struct usb_ep *_ep)
Alan Stern80661342008-08-14 15:49:11 -04001264{
1265 if (!_ep || _ep->name == ep0name)
1266 return -EINVAL;
1267 return net2280_set_halt_and_wedge(_ep, 1, 1);
1268}
1269
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001270static int net2280_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 struct net2280_ep *ep;
1273 u32 avail;
1274
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001275 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (!_ep || (!ep->desc && ep->num != 0))
1277 return -ENODEV;
1278 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1279 return -ESHUTDOWN;
1280
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001281 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (avail > ep->fifo_size)
1283 return -EOVERFLOW;
1284 if (ep->is_in)
1285 avail = ep->fifo_size - avail;
1286 return avail;
1287}
1288
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001289static void net2280_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct net2280_ep *ep;
1292
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001293 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (!_ep || (!ep->desc && ep->num != 0))
1295 return;
1296 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1297 return;
1298
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001299 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001300 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
David Brownell901b3d72006-09-02 03:13:45 -07001303static const struct usb_ep_ops net2280_ep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 .enable = net2280_enable,
1305 .disable = net2280_disable,
1306
1307 .alloc_request = net2280_alloc_request,
1308 .free_request = net2280_free_request,
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 .queue = net2280_queue,
1311 .dequeue = net2280_dequeue,
1312
1313 .set_halt = net2280_set_halt,
Alan Stern80661342008-08-14 15:49:11 -04001314 .set_wedge = net2280_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 .fifo_status = net2280_fifo_status,
1316 .fifo_flush = net2280_fifo_flush,
1317};
1318
1319/*-------------------------------------------------------------------------*/
1320
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001321static int net2280_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct net2280 *dev;
1324 unsigned long flags;
1325 u16 retval;
1326
1327 if (!_gadget)
1328 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001329 dev = container_of(_gadget, struct net2280, gadget);
1330 spin_lock_irqsave(&dev->lock, flags);
1331 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1332 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return retval;
1334}
1335
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001336static int net2280_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 struct net2280 *dev;
1339 u32 tmp;
1340 unsigned long flags;
1341
1342 if (!_gadget)
1343 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001344 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001346 spin_lock_irqsave(&dev->lock, flags);
1347 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001348 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1349 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001350 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /* pci writes may still be posted */
1353 return 0;
1354}
1355
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001356static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct net2280 *dev;
1359 u32 tmp;
1360 unsigned long flags;
1361
1362 if (!_gadget)
1363 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001364 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001366 spin_lock_irqsave(&dev->lock, flags);
1367 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001368 if (value) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001369 tmp |= BIT(SELF_POWERED_STATUS);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001370 dev->selfpowered = 1;
1371 } else {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001372 tmp &= ~BIT(SELF_POWERED_STATUS);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001373 dev->selfpowered = 0;
1374 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001375 writel(tmp, &dev->usb->usbctl);
1376 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 return 0;
1379}
1380
1381static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1382{
1383 struct net2280 *dev;
1384 u32 tmp;
1385 unsigned long flags;
1386
1387 if (!_gadget)
1388 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001389 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001391 spin_lock_irqsave(&dev->lock, flags);
1392 tmp = readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 dev->softconnect = (is_on != 0);
1394 if (is_on)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001395 tmp |= BIT(USB_DETECT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001397 tmp &= ~BIT(USB_DETECT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001398 writel(tmp, &dev->usb->usbctl);
1399 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 return 0;
1402}
1403
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001404static int net2280_start(struct usb_gadget *_gadget,
1405 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001406static int net2280_stop(struct usb_gadget *_gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408static const struct usb_gadget_ops net2280_ops = {
1409 .get_frame = net2280_get_frame,
1410 .wakeup = net2280_wakeup,
1411 .set_selfpowered = net2280_set_selfpowered,
1412 .pullup = net2280_pullup,
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001413 .udc_start = net2280_start,
1414 .udc_stop = net2280_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415};
1416
1417/*-------------------------------------------------------------------------*/
1418
Ricardo Ribalda Delgadob99b4062014-07-04 11:27:03 +02001419#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421/* FIXME move these into procfs, and use seq_file.
1422 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1423 * and also doesn't help products using this with 2.4 kernels.
1424 */
1425
1426/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001427static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1428 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001430 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001432 if (!dev->driver || !dev->driver->function ||
1433 strlen(dev->driver->function) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001435 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001437static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001439static ssize_t registers_show(struct device *_dev,
1440 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct net2280 *dev;
1443 char *next;
1444 unsigned size, t;
1445 unsigned long flags;
1446 int i;
1447 u32 t1, t2;
Andrew Morton30e69592005-06-26 17:18:46 -07001448 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001450 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 next = buf;
1452 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001453 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (dev->driver)
1456 s = dev->driver->driver.name;
1457 else
1458 s = "(none)";
1459
1460 /* Main Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001461 t = scnprintf(next, size, "%s version " DRIVER_VERSION
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001462 ", chiprev %04x\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 "devinit %03x fifoctl %08x gadget '%s'\n"
1464 "pci irqenb0 %02x irqenb1 %08x "
1465 "irqstat0 %04x irqstat1 %08x\n",
1466 driver_name, dev->chiprev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001467 readl(&dev->regs->devinit),
1468 readl(&dev->regs->fifoctl),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 s,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001470 readl(&dev->regs->pciirqenb0),
1471 readl(&dev->regs->pciirqenb1),
1472 readl(&dev->regs->irqstat0),
1473 readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 size -= t;
1475 next += t;
1476
1477 /* USB Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001478 t1 = readl(&dev->usb->usbctl);
1479 t2 = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001480 if (t1 & BIT(VBUS_PIN)) {
1481 if (t2 & BIT(HIGH_SPEED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 s = "high speed";
1483 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1484 s = "powered";
1485 else
1486 s = "full speed";
1487 /* full speed bit (6) not working?? */
1488 } else
1489 s = "not attached";
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001490 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 "stdrsp %08x usbctl %08x usbstat %08x "
1492 "addr 0x%02x (%s)\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001493 readl(&dev->usb->stdrsp), t1, t2,
1494 readl(&dev->usb->ouraddr), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 size -= t;
1496 next += t;
1497
1498 /* PCI Master Control Registers */
1499
1500 /* DMA Control Registers */
1501
1502 /* Configurable EP Control Registers */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001503 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 struct net2280_ep *ep;
1505
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001506 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (i && !ep->desc)
1508 continue;
1509
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001510 t1 = readl(&ep->cfg->ep_cfg);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001511 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1512 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1514 "irqenb %02x\n",
1515 ep->ep.name, t1, t2,
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001516 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 ? "NAK " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001518 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 ? "hide " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001520 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 ? "CRC " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001522 (t2 & BIT(CLEAR_INTERRUPT_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 ? "interrupt " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001524 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 ? "status " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001526 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 ? "NAKmode " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001528 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 ? "DATA1 " : "DATA0 ",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001530 (t2 & BIT(CLEAR_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 ? "HALT " : "",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001532 readl(&ep->regs->ep_irqenb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 size -= t;
1534 next += t;
1535
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001536 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 "\tstat %08x avail %04x "
1538 "(ep%d%s-%s)%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001539 readl(&ep->regs->ep_stat),
1540 readl(&ep->regs->ep_avail),
1541 t1 & 0x0f, DIR_STRING(t1),
1542 type_string(t1 >> 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 ep->stopped ? "*" : "");
1544 size -= t;
1545 next += t;
1546
1547 if (!ep->dma)
1548 continue;
1549
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001550 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 " dma\tctl %08x stat %08x count %08x\n"
1552 "\taddr %08x desc %08x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001553 readl(&ep->dma->dmactl),
1554 readl(&ep->dma->dmastat),
1555 readl(&ep->dma->dmacount),
1556 readl(&ep->dma->dmaaddr),
1557 readl(&ep->dma->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 size -= t;
1559 next += t;
1560
1561 }
1562
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001563 /* Indexed Registers (none yet) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 /* Statistics */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001566 t = scnprintf(next, size, "\nirqs: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 size -= t;
1568 next += t;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001569 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 struct net2280_ep *ep;
1571
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001572 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (i && !ep->irqs)
1574 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001575 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 size -= t;
1577 next += t;
1578
1579 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001580 t = scnprintf(next, size, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 size -= t;
1582 next += t;
1583
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001584 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 return PAGE_SIZE - size;
1587}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001588static DEVICE_ATTR_RO(registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001590static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1591 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 struct net2280 *dev;
1594 char *next;
1595 unsigned size;
1596 unsigned long flags;
1597 int i;
1598
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001599 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 next = buf;
1601 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001602 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001604 for (i = 0; i < dev->n_ep; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001605 struct net2280_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct net2280_request *req;
1607 int t;
1608
1609 if (i != 0) {
1610 const struct usb_endpoint_descriptor *d;
1611
1612 d = ep->desc;
1613 if (!d)
1614 continue;
1615 t = d->bEndpointAddress;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001616 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1618 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1619 (t & USB_DIR_IN) ? "in" : "out",
Ricardo Ribalda Delgadoa27f37a2014-05-20 18:30:08 +02001620 type_string(d->bmAttributes),
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001621 usb_endpoint_maxp(d) & 0x1fff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 ep->dma ? "dma" : "pio", ep->fifo_size
1623 );
1624 } else /* ep0 should only have one transfer queued */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001625 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 ep->is_in ? "in" : "out");
1627 if (t <= 0 || t > size)
1628 goto done;
1629 size -= t;
1630 next += t;
1631
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001632 if (list_empty(&ep->queue)) {
1633 t = scnprintf(next, size, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (t <= 0 || t > size)
1635 goto done;
1636 size -= t;
1637 next += t;
1638 continue;
1639 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001640 list_for_each_entry(req, &ep->queue, queue) {
1641 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1642 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 "\treq %p len %d/%d "
1644 "buf %p (dmacount %08x)\n",
1645 &req->req, req->req.actual,
1646 req->req.length, req->req.buf,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001647 readl(&ep->dma->dmacount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001649 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 "\treq %p len %d/%d buf %p\n",
1651 &req->req, req->req.actual,
1652 req->req.length, req->req.buf);
1653 if (t <= 0 || t > size)
1654 goto done;
1655 size -= t;
1656 next += t;
1657
1658 if (ep->dma) {
1659 struct net2280_dma *td;
1660
1661 td = req->td;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001662 t = scnprintf(next, size, "\t td %08x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 " count %08x buf %08x desc %08x\n",
1664 (u32) req->td_dma,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001665 le32_to_cpu(td->dmacount),
1666 le32_to_cpu(td->dmaaddr),
1667 le32_to_cpu(td->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (t <= 0 || t > size)
1669 goto done;
1670 size -= t;
1671 next += t;
1672 }
1673 }
1674 }
1675
1676done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001677 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return PAGE_SIZE - size;
1679}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001680static DEVICE_ATTR_RO(queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682
1683#else
1684
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001685#define device_create_file(a, b) (0)
1686#define device_remove_file(a, b) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688#endif
1689
1690/*-------------------------------------------------------------------------*/
1691
1692/* another driver-specific mode might be a request type doing dma
1693 * to/from another device fifo instead of to/from memory.
1694 */
1695
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001696static void set_fifo_mode(struct net2280 *dev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 /* keeping high bits preserves BAR2 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001699 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001702 INIT_LIST_HEAD(&dev->gadget.ep_list);
1703 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1704 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 switch (mode) {
1706 case 0:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001707 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1708 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1709 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 break;
1711 case 1:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001712 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714 case 2:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001715 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1716 dev->ep[1].fifo_size = 2048;
1717 dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
1719 }
1720 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001721 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1722 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001725static void defect7374_disable_data_eps(struct net2280 *dev)
1726{
1727 /*
1728 * For Defect 7374, disable data EPs (and more):
1729 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1730 * returing ep regs back to normal.
1731 */
1732 struct net2280_ep *ep;
1733 int i;
1734 unsigned char ep_sel;
1735 u32 tmp_reg;
1736
1737 for (i = 1; i < 5; i++) {
1738 ep = &dev->ep[i];
1739 writel(0, &ep->cfg->ep_cfg);
1740 }
1741
1742 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1743 for (i = 0; i < 6; i++)
1744 writel(0, &dev->dep[i].dep_cfg);
1745
1746 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1747 /* Select an endpoint for subsequent operations: */
1748 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1749 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1750
1751 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1752 ep_sel == 18 || ep_sel == 20)
1753 continue;
1754
1755 /* Change settings on some selected endpoints */
1756 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001757 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001758 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1759 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001760 tmp_reg |= BIT(EP_INITIALIZED);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001761 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1762 }
1763}
1764
1765static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1766{
1767 u32 tmp = 0, tmp_reg;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001768 u32 scratch;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001769 int i;
1770 unsigned char ep_sel;
1771
1772 scratch = get_idx_reg(dev->regs, SCRATCH);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001773
1774 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1775 == DEFECT7374_FSM_SS_CONTROL_READ);
1776
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001777 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1778
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001779 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1780 ep_warn(dev, "It will operate on cold-reboot and SS connect");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001781
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001782 /*GPEPs:*/
1783 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1784 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1785 ((dev->enhanced_mode) ?
1786 BIT(OUT_ENDPOINT_ENABLE) : BIT(ENDPOINT_ENABLE)) |
1787 BIT(IN_ENDPOINT_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001788
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001789 for (i = 1; i < 5; i++)
1790 writel(tmp, &dev->ep[i].cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001791
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001792 /* CSRIN, PCIIN, STATIN, RCIN*/
1793 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1794 writel(tmp, &dev->dep[1].dep_cfg);
1795 writel(tmp, &dev->dep[3].dep_cfg);
1796 writel(tmp, &dev->dep[4].dep_cfg);
1797 writel(tmp, &dev->dep[5].dep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001798
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001799 /*Implemented for development and debug.
1800 * Can be refined/tuned later.*/
1801 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1802 /* Select an endpoint for subsequent operations: */
1803 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1804 writel(((tmp_reg & ~0x1f) | ep_sel),
1805 &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001806
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001807 if (ep_sel == 1) {
1808 tmp =
1809 (readl(&dev->plregs->pl_ep_ctrl) |
1810 BIT(CLEAR_ACK_ERROR_CODE) | 0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001811 writel(tmp, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001812 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001813 }
1814
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001815 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1816 ep_sel == 18 || ep_sel == 20)
1817 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001818
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001819 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1820 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1821 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1822
1823 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1824 ~BIT(EP_INITIALIZED);
1825 writel(tmp, &dev->plregs->pl_ep_ctrl);
1826
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001827 }
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001828
1829 /* Set FSM to focus on the first Control Read:
1830 * - Tip: Connection speed is known upon the first
1831 * setup request.*/
1832 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1833 set_idx_reg(dev->regs, SCRATCH, scratch);
1834
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837/* keeping it simple:
1838 * - one bus driver, initted first;
1839 * - one function driver, initted second
1840 *
1841 * most of the work to support multiple net2280 controllers would
1842 * be to associate this gadget driver (yes?) with all of them, or
1843 * perhaps to bind specific drivers to specific devices.
1844 */
1845
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001846static void usb_reset_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 u32 tmp;
1849
1850 dev->gadget.speed = USB_SPEED_UNKNOWN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001851 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001853 net2280_led_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 /* disable automatic responses, and irqs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001856 writel(0, &dev->usb->stdrsp);
1857 writel(0, &dev->regs->pciirqenb0);
1858 writel(0, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /* clear old dma and irq state */
1861 for (tmp = 0; tmp < 4; tmp++) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001862 struct net2280_ep *ep = &dev->ep[tmp + 1];
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001863 if (ep->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001864 abort_dma(ep);
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001865 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001866
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001867 writel(~0, &dev->regs->irqstat0),
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001868 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* reset, and enable pci */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001871 tmp = readl(&dev->regs->devinit) |
1872 BIT(PCI_ENABLE) |
1873 BIT(FIFO_SOFT_RESET) |
1874 BIT(USB_SOFT_RESET) |
1875 BIT(M8051_RESET);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001876 writel(tmp, &dev->regs->devinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 /* standard fifo and endpoint allocations */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001879 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001882static void usb_reset_338x(struct net2280 *dev)
1883{
1884 u32 tmp;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001885
1886 dev->gadget.speed = USB_SPEED_UNKNOWN;
1887 (void)readl(&dev->usb->usbctl);
1888
1889 net2280_led_init(dev);
1890
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001891 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001892 /* disable automatic responses, and irqs */
1893 writel(0, &dev->usb->stdrsp);
1894 writel(0, &dev->regs->pciirqenb0);
1895 writel(0, &dev->regs->pciirqenb1);
1896 }
1897
1898 /* clear old dma and irq state */
1899 for (tmp = 0; tmp < 4; tmp++) {
1900 struct net2280_ep *ep = &dev->ep[tmp + 1];
1901
1902 if (ep->dma)
1903 abort_dma(ep);
1904 }
1905
1906 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
1907
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001908 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001909 /* reset, and enable pci */
1910 tmp = readl(&dev->regs->devinit) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001911 BIT(PCI_ENABLE) |
1912 BIT(FIFO_SOFT_RESET) |
1913 BIT(USB_SOFT_RESET) |
1914 BIT(M8051_RESET);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001915
1916 writel(tmp, &dev->regs->devinit);
1917 }
1918
1919 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
1920 INIT_LIST_HEAD(&dev->gadget.ep_list);
1921
1922 for (tmp = 1; tmp < dev->n_ep; tmp++)
1923 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
1924
1925}
1926
1927static void usb_reset(struct net2280 *dev)
1928{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001929 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001930 return usb_reset_228x(dev);
1931 return usb_reset_338x(dev);
1932}
1933
1934static void usb_reinit_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* basic endpoint init */
1939 for (tmp = 0; tmp < 7; tmp++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001940 struct net2280_ep *ep = &dev->ep[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001942 ep->ep.name = ep_name[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 ep->dev = dev;
1944 ep->num = tmp;
1945
1946 if (tmp > 0 && tmp <= 4) {
1947 ep->fifo_size = 1024;
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001948 ep->dma = &dev->dma[tmp - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 } else
1950 ep->fifo_size = 64;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001951 ep->regs = &dev->epregs[tmp];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001952 ep->cfg = &dev->epregs[tmp];
1953 ep_reset_228x(dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001955 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
1956 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
1957 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001959 dev->gadget.ep0 = &dev->ep[0].ep;
1960 dev->ep[0].stopped = 0;
1961 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /* we want to prevent lowlevel/insecure access from the USB host,
1964 * but erratum 0119 means this enable bit is ignored
1965 */
1966 for (tmp = 0; tmp < 5; tmp++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001967 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001970static void usb_reinit_338x(struct net2280 *dev)
1971{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001972 int i;
1973 u32 tmp, val;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001974 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
1975 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
1976 0x00, 0xC0, 0x00, 0xC0 };
1977
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001978 /* basic endpoint init */
1979 for (i = 0; i < dev->n_ep; i++) {
1980 struct net2280_ep *ep = &dev->ep[i];
1981
1982 ep->ep.name = ep_name[i];
1983 ep->dev = dev;
1984 ep->num = i;
1985
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001986 if (i > 0 && i <= 4)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001987 ep->dma = &dev->dma[i - 1];
1988
1989 if (dev->enhanced_mode) {
1990 ep->cfg = &dev->epregs[ne[i]];
1991 ep->regs = (struct net2280_ep_regs __iomem *)
Felipe Balbic43e97b2014-07-16 12:20:25 -05001992 (((void __iomem *)&dev->epregs[ne[i]]) +
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001993 ep_reg_addr[i]);
1994 ep->fiforegs = &dev->fiforegs[i];
1995 } else {
1996 ep->cfg = &dev->epregs[i];
1997 ep->regs = &dev->epregs[i];
1998 ep->fiforegs = &dev->fiforegs[i];
1999 }
2000
2001 ep->fifo_size = (i != 0) ? 2048 : 512;
2002
2003 ep_reset_338x(dev->regs, ep);
2004 }
2005 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2006
2007 dev->gadget.ep0 = &dev->ep[0].ep;
2008 dev->ep[0].stopped = 0;
2009
2010 /* Link layer set up */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002011 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002012 tmp = readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002013 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002014 writel(tmp, &dev->usb_ext->usbctl2);
2015 }
2016
2017 /* Hardware Defect and Workaround */
2018 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2019 val &= ~(0xf << TIMER_LFPS_6US);
2020 val |= 0x5 << TIMER_LFPS_6US;
2021 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2022
2023 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2024 val &= ~(0xffff << TIMER_LFPS_80US);
2025 val |= 0x0100 << TIMER_LFPS_80US;
2026 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2027
2028 /*
2029 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2030 * Hot Reset Exit Handshake may Fail in Specific Case using
2031 * Default Register Settings. Workaround for Enumeration test.
2032 */
2033 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2034 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2035 val |= 0x10 << HOT_TX_NORESET_TS2;
2036 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2037
2038 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2039 val &= ~(0x1f << HOT_RX_RESET_TS2);
2040 val |= 0x3 << HOT_RX_RESET_TS2;
2041 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2042
2043 /*
2044 * Set Recovery Idle to Recover bit:
2045 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2046 * link robustness with various hosts and hubs.
2047 * - It is safe to set for all connection speeds; all chip revisions.
2048 * - R-M-W to leave other bits undisturbed.
2049 * - Reference PLX TT-7372
2050 */
2051 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002052 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002053 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2054
2055 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2056
2057 /* disable dedicated endpoints */
2058 writel(0x0D, &dev->dep[0].dep_cfg);
2059 writel(0x0D, &dev->dep[1].dep_cfg);
2060 writel(0x0E, &dev->dep[2].dep_cfg);
2061 writel(0x0E, &dev->dep[3].dep_cfg);
2062 writel(0x0F, &dev->dep[4].dep_cfg);
2063 writel(0x0C, &dev->dep[5].dep_cfg);
2064}
2065
2066static void usb_reinit(struct net2280 *dev)
2067{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002068 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002069 return usb_reinit_228x(dev);
2070 return usb_reinit_338x(dev);
2071}
2072
2073static void ep0_start_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002075 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2076 BIT(CLEAR_NAK_OUT_PACKETS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002077 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2078 &dev->epregs[0].ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /*
2081 * hardware optionally handles a bunch of standard requests
2082 * that the API hides from drivers anyway. have it do so.
2083 * endpoint status/features are handled in software, to
2084 * help pass tests for some dubious behavior.
2085 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002086 writel(BIT(SET_TEST_MODE) |
2087 BIT(SET_ADDRESS) |
2088 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2089 BIT(GET_DEVICE_STATUS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002090 BIT(GET_INTERFACE_STATUS),
2091 &dev->usb->stdrsp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002092 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2093 BIT(SELF_POWERED_USB_DEVICE) |
2094 BIT(REMOTE_WAKEUP_SUPPORT) |
2095 (dev->softconnect << USB_DETECT_ENABLE) |
2096 BIT(SELF_POWERED_STATUS),
2097 &dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002100 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2101 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2102 &dev->regs->pciirqenb0);
2103 writel(BIT(PCI_INTERRUPT_ENABLE) |
2104 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2105 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2106 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2107 BIT(VBUS_INTERRUPT_ENABLE) |
2108 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2109 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2110 &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /* don't leave any writes posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002113 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002116static void ep0_start_338x(struct net2280 *dev)
2117{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002118
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002119 if (dev->bug7734_patched)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002120 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2121 BIT(SET_EP_HIDE_STATUS_PHASE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002122 &dev->epregs[0].ep_rsp);
2123
2124 /*
2125 * hardware optionally handles a bunch of standard requests
2126 * that the API hides from drivers anyway. have it do so.
2127 * endpoint status/features are handled in software, to
2128 * help pass tests for some dubious behavior.
2129 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002130 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2131 BIT(SET_SEL) |
2132 BIT(SET_TEST_MODE) |
2133 BIT(SET_ADDRESS) |
2134 BIT(GET_INTERFACE_STATUS) |
2135 BIT(GET_DEVICE_STATUS),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002136 &dev->usb->stdrsp);
2137 dev->wakeup_enable = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002138 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002139 (dev->softconnect << USB_DETECT_ENABLE) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002140 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002141 &dev->usb->usbctl);
2142
2143 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002144 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002145 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2146 &dev->regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002147 writel(BIT(PCI_INTERRUPT_ENABLE) |
2148 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2149 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2150 BIT(VBUS_INTERRUPT_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002151 &dev->regs->pciirqenb1);
2152
2153 /* don't leave any writes posted */
2154 (void)readl(&dev->usb->usbctl);
2155}
2156
2157static void ep0_start(struct net2280 *dev)
2158{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002159 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002160 return ep0_start_228x(dev);
2161 return ep0_start_338x(dev);
2162}
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164/* when a driver is successfully registered, it will receive
2165 * control requests including set_configuration(), which enables
2166 * non-control requests. then usb traffic follows until a
2167 * disconnect is reported. then a host may connect again, or
2168 * the driver might get unbound.
2169 */
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002170static int net2280_start(struct usb_gadget *_gadget,
2171 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002173 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 int retval;
2175 unsigned i;
2176
2177 /* insist on high speed support from the driver, since
2178 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2179 * "must not be used in normal operation"
2180 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002181 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2182 !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -EINVAL;
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002184
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002185 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002187 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002188 dev->ep[i].irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 /* hook up the driver ... */
2191 dev->softconnect = 1;
2192 driver->driver.bus = NULL;
2193 dev->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002195 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2196 if (retval)
2197 goto err_unbind;
2198 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2199 if (retval)
2200 goto err_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Ricardo Ribalda Delgado7a74c482014-11-28 14:50:47 +01002202 /* enable host detection and ep0; and we're ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 * for set_configuration as well as eventual disconnect.
2204 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002205 net2280_led_active(dev, 1);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002206
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002207 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002208 defect7374_enable_data_eps_zero(dev);
2209
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002210 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /* pci writes may still be posted */
2213 return 0;
Jeff Garzikb3899da2006-10-11 21:50:24 -04002214
2215err_func:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002216 device_remove_file(&dev->pdev->dev, &dev_attr_function);
Jeff Garzikb3899da2006-10-11 21:50:24 -04002217err_unbind:
Jeff Garzikb3899da2006-10-11 21:50:24 -04002218 dev->driver = NULL;
2219 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002222static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 int i;
2225
2226 /* don't disconnect if it's not connected */
2227 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2228 driver = NULL;
2229
2230 /* stop hardware; prevent new request submissions;
2231 * and kill any outstanding requests.
2232 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002233 usb_reset(dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002234 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002235 nuke(&dev->ep[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Felipe Balbi699412d2013-03-18 10:14:47 +02002237 /* report disconnect; the driver is already quiesced */
2238 if (driver) {
2239 spin_unlock(&dev->lock);
2240 driver->disconnect(&dev->gadget);
2241 spin_lock(&dev->lock);
2242 }
2243
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002244 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Felipe Balbi22835b82014-10-17 12:05:12 -05002247static int net2280_stop(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002249 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 unsigned long flags;
2251
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002252 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002254 spin_lock_irqsave(&dev->lock, flags);
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002255 stop_activity(dev, NULL);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002256 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002258 net2280_led_active(dev, 0);
Alan Stern2f076072013-01-30 16:40:14 -05002259
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002260 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2261 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002263 dev->driver = NULL;
Ricardo Ribalda Delgado84237bf2014-05-15 14:28:45 +02002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return 0;
2266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268/*-------------------------------------------------------------------------*/
2269
2270/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2271 * also works for dma-capable endpoints, in pio mode or just
2272 * to manually advance the queue after short OUT transfers.
2273 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002274static void handle_ep_small(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 struct net2280_request *req;
2277 u32 t;
2278 /* 0 error, 1 mid-data, 2 done */
2279 int mode = 1;
2280
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002281 if (!list_empty(&ep->queue))
2282 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 struct net2280_request, queue);
2284 else
2285 req = NULL;
2286
2287 /* ack all, and handle what we care about */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002288 t = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 ep->irqs++;
2290#if 0
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002291 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 ep->ep.name, t, req ? &req->req : 0);
2293#endif
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002294 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002295 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002296 else
2297 /* Added for 2282 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002298 writel(t, &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 /* for ep0, monitor token irqs to catch data stage length errors
2301 * and to synchronize on status.
2302 *
2303 * also, to defer reporting of protocol stalls ... here's where
2304 * data or status first appears, handling stalls here should never
2305 * cause trouble on the host side..
2306 *
2307 * control requests could be slightly faster without token synch for
2308 * status, but status can jam up that way.
2309 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002310 if (unlikely(ep->num == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (ep->is_in) {
2312 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002313 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (ep->dev->protocol_stall) {
2315 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002316 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318 if (!req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002319 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 mode = 2;
2321 /* reply to extra IN data tokens with a zlp */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002322 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (ep->dev->protocol_stall) {
2324 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002325 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 mode = 2;
Alan Stern1f26e282006-11-16 10:16:00 -05002327 } else if (ep->responded &&
2328 !req && !ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002329 write_fifo(ep, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331 } else {
2332 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002333 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (ep->dev->protocol_stall) {
2335 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002336 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338 mode = 2;
2339 /* an extra OUT token is an error */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002340 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2341 req &&
2342 req->req.actual == req->req.length) ||
2343 (ep->responded && !req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 ep->dev->protocol_stall = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002345 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 ep->stopped = 1;
2347 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002348 done(ep, req, -EOVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 req = NULL;
2350 }
2351 }
2352 }
2353
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002354 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return;
2356
2357 /* manual DMA queue advance after short OUT */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002358 if (likely(ep->dma)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002359 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 u32 count;
2361 int stopped = ep->stopped;
2362
2363 /* TRANSFERRED works around OUT_DONE erratum 0112.
2364 * we expect (N <= maxpacket) bytes; host wrote M.
2365 * iff (M < N) we won't ever see a DMA interrupt.
2366 */
2367 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002368 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 /* any preceding dma transfers must finish.
2371 * dma handles (M >= N), may empty the queue
2372 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002373 scan_dma_completions(ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002374 if (unlikely(list_empty(&ep->queue) ||
2375 ep->out_overflow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 req = NULL;
2377 break;
2378 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002379 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 struct net2280_request, queue);
2381
2382 /* here either (M < N), a "real" short rx;
2383 * or (M == N) and the queue didn't empty
2384 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002385 if (likely(t & BIT(FIFO_EMPTY))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002386 count = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 count &= DMA_BYTE_COUNT_MASK;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002388 if (readl(&ep->dma->dmadesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 != req->td_dma)
2390 req = NULL;
2391 break;
2392 }
2393 udelay(1);
2394 }
2395
2396 /* stop DMA, leave ep NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002397 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002398 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002400 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 req->td->dmacount = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002402 t = readl(&ep->regs->ep_avail);
2403 dma_done(ep, req, count,
David Brownell901b3d72006-09-02 03:13:45 -07002404 (ep->out_overflow || t)
2405 ? -EOVERFLOW : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407
2408 /* also flush to prevent erratum 0106 trouble */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002409 if (unlikely(ep->out_overflow ||
2410 (ep->dev->chiprev == 0x0100 &&
2411 ep->dev->gadget.speed
2412 == USB_SPEED_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002413 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 ep->out_overflow = 0;
2415 }
2416
2417 /* (re)start dma if needed, stop NAKing */
2418 ep->stopped = stopped;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002419 if (!list_empty(&ep->queue))
2420 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 } else
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002422 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 ep->ep.name, t);
2424 return;
2425
2426 /* data packet(s) received (in the fifo, OUT) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002427 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002428 if (read_fifo(ep, req) && ep->num != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 mode = 2;
2430
2431 /* data packet(s) transmitted (IN) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002432 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 unsigned len;
2434
2435 len = req->req.length - req->req.actual;
2436 if (len > ep->ep.maxpacket)
2437 len = ep->ep.maxpacket;
2438 req->req.actual += len;
2439
2440 /* if we wrote it all, we're usually done */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002441 /* send zlps until the status stage */
2442 if ((req->req.actual == req->req.length) &&
2443 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 mode = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 /* there was nothing to do ... */
2447 } else if (mode == 1)
2448 return;
2449
2450 /* done */
2451 if (mode == 2) {
2452 /* stream endpoints often resubmit/unlink in completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002453 done(ep, req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 /* maybe advance queue to next request */
2456 if (ep->num == 0) {
2457 /* NOTE: net2280 could let gadget driver start the
2458 * status stage later. since not all controllers let
2459 * them control that, the api doesn't (yet) allow it.
2460 */
2461 if (!ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002462 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 req = NULL;
2464 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002465 if (!list_empty(&ep->queue) && !ep->stopped)
2466 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct net2280_request, queue);
2468 else
2469 req = NULL;
2470 if (req && !ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002471 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
2473 }
2474
2475 /* is there a buffer for the next packet?
2476 * for best streaming performance, make sure there is one.
2477 */
2478 if (req && !ep->stopped) {
2479
2480 /* load IN fifo with next packet (may be zlp) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002481 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002482 write_fifo(ep, &req->req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484}
2485
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002486static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
2488 struct net2280_ep *ep;
2489
2490 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002491 return &dev->ep[0];
2492 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 u8 bEndpointAddress;
2494
2495 if (!ep->desc)
2496 continue;
2497 bEndpointAddress = ep->desc->bEndpointAddress;
2498 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2499 continue;
2500 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2501 return ep;
2502 }
2503 return NULL;
2504}
2505
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002506static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2507{
2508 u32 scratch, fsmvalue;
2509 u32 ack_wait_timeout, state;
2510
2511 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2512 scratch = get_idx_reg(dev->regs, SCRATCH);
2513 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2514 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2515
2516 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2517 (r.bRequestType & USB_DIR_IN)))
2518 return;
2519
2520 /* This is the first Control Read for this connection: */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002521 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002522 /*
2523 * Connection is NOT SS:
2524 * - Connection must be FS or HS.
2525 * - This FSM state should allow workaround software to
2526 * run after the next USB connection.
2527 */
2528 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002529 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002530 goto restore_data_eps;
2531 }
2532
2533 /* Connection is SS: */
2534 for (ack_wait_timeout = 0;
2535 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2536 ack_wait_timeout++) {
2537
2538 state = readl(&dev->plregs->pl_ep_status_1)
2539 & (0xff << STATE);
2540 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2541 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2542 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002543 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002544 break;
2545 }
2546
2547 /*
2548 * We have not yet received host's Data Phase ACK
2549 * - Wait and try again.
2550 */
2551 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2552
2553 continue;
2554 }
2555
2556
2557 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002558 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002559 "to detect SS host's data phase ACK.");
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002560 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002561 "got 0x%2.2x.\n", state >> STATE);
2562 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002563 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002564 "%duSec for Control Read Data Phase ACK\n",
2565 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2566 }
2567
2568restore_data_eps:
2569 /*
2570 * Restore data EPs to their pre-workaround settings (disabled,
2571 * initialized, and other details).
2572 */
2573 defect7374_disable_data_eps(dev);
2574
2575 set_idx_reg(dev->regs, SCRATCH, scratch);
2576
2577 return;
2578}
2579
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002580static void ep_clear_seqnum(struct net2280_ep *ep)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002581{
2582 struct net2280 *dev = ep->dev;
2583 u32 val;
2584 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2585
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002586 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2587 val |= ep_pl[ep->num];
2588 writel(val, &dev->plregs->pl_ep_ctrl);
2589 val |= BIT(SEQUENCE_NUMBER_RESET);
2590 writel(val, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002591
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002592 return;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002593}
2594
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002595static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2596 struct net2280_ep *ep, struct usb_ctrlrequest r)
2597{
2598 int tmp = 0;
2599
2600#define w_value le16_to_cpu(r.wValue)
2601#define w_index le16_to_cpu(r.wIndex)
2602#define w_length le16_to_cpu(r.wLength)
2603
2604 switch (r.bRequest) {
2605 struct net2280_ep *e;
2606 u16 status;
2607
2608 case USB_REQ_SET_CONFIGURATION:
2609 dev->addressed_state = !w_value;
2610 goto usb3_delegate;
2611
2612 case USB_REQ_GET_STATUS:
2613 switch (r.bRequestType) {
2614 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2615 status = dev->wakeup_enable ? 0x02 : 0x00;
2616 if (dev->selfpowered)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002617 status |= BIT(0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002618 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2619 dev->ltm_enable << 4);
2620 writel(0, &dev->epregs[0].ep_irqenb);
2621 set_fifo_bytecount(ep, sizeof(status));
2622 writel((__force u32) status, &dev->epregs[0].ep_data);
2623 allow_status_338x(ep);
2624 break;
2625
2626 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2627 e = get_ep_by_addr(dev, w_index);
2628 if (!e)
2629 goto do_stall3;
2630 status = readl(&e->regs->ep_rsp) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002631 BIT(CLEAR_ENDPOINT_HALT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002632 writel(0, &dev->epregs[0].ep_irqenb);
2633 set_fifo_bytecount(ep, sizeof(status));
2634 writel((__force u32) status, &dev->epregs[0].ep_data);
2635 allow_status_338x(ep);
2636 break;
2637
2638 default:
2639 goto usb3_delegate;
2640 }
2641 break;
2642
2643 case USB_REQ_CLEAR_FEATURE:
2644 switch (r.bRequestType) {
2645 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2646 if (!dev->addressed_state) {
2647 switch (w_value) {
2648 case USB_DEVICE_U1_ENABLE:
2649 dev->u1_enable = 0;
2650 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002651 ~BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002652 &dev->usb_ext->usbctl2);
2653 allow_status_338x(ep);
2654 goto next_endpoints3;
2655
2656 case USB_DEVICE_U2_ENABLE:
2657 dev->u2_enable = 0;
2658 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002659 ~BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002660 &dev->usb_ext->usbctl2);
2661 allow_status_338x(ep);
2662 goto next_endpoints3;
2663
2664 case USB_DEVICE_LTM_ENABLE:
2665 dev->ltm_enable = 0;
2666 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002667 ~BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002668 &dev->usb_ext->usbctl2);
2669 allow_status_338x(ep);
2670 goto next_endpoints3;
2671
2672 default:
2673 break;
2674 }
2675 }
2676 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2677 dev->wakeup_enable = 0;
2678 writel(readl(&dev->usb->usbctl) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002679 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002680 &dev->usb->usbctl);
2681 allow_status_338x(ep);
2682 break;
2683 }
2684 goto usb3_delegate;
2685
2686 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2687 e = get_ep_by_addr(dev, w_index);
2688 if (!e)
2689 goto do_stall3;
2690 if (w_value != USB_ENDPOINT_HALT)
2691 goto do_stall3;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002692 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002693 /*
2694 * Workaround for SS SeqNum not cleared via
2695 * Endpoint Halt (Clear) bit. select endpoint
2696 */
2697 ep_clear_seqnum(e);
2698 clear_halt(e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002699 if (!list_empty(&e->queue) && e->td_dma)
2700 restart_dma(e);
2701 allow_status(ep);
2702 ep->stopped = 1;
2703 break;
2704
2705 default:
2706 goto usb3_delegate;
2707 }
2708 break;
2709 case USB_REQ_SET_FEATURE:
2710 switch (r.bRequestType) {
2711 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2712 if (!dev->addressed_state) {
2713 switch (w_value) {
2714 case USB_DEVICE_U1_ENABLE:
2715 dev->u1_enable = 1;
2716 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002717 BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002718 &dev->usb_ext->usbctl2);
2719 allow_status_338x(ep);
2720 goto next_endpoints3;
2721
2722 case USB_DEVICE_U2_ENABLE:
2723 dev->u2_enable = 1;
2724 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002725 BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002726 &dev->usb_ext->usbctl2);
2727 allow_status_338x(ep);
2728 goto next_endpoints3;
2729
2730 case USB_DEVICE_LTM_ENABLE:
2731 dev->ltm_enable = 1;
2732 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002733 BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002734 &dev->usb_ext->usbctl2);
2735 allow_status_338x(ep);
2736 goto next_endpoints3;
2737 default:
2738 break;
2739 }
2740 }
2741
2742 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2743 dev->wakeup_enable = 1;
2744 writel(readl(&dev->usb->usbctl) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002745 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002746 &dev->usb->usbctl);
2747 allow_status_338x(ep);
2748 break;
2749 }
2750 goto usb3_delegate;
2751
2752 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2753 e = get_ep_by_addr(dev, w_index);
2754 if (!e || (w_value != USB_ENDPOINT_HALT))
2755 goto do_stall3;
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002756 ep->stopped = 1;
2757 if (ep->num == 0)
2758 ep->dev->protocol_stall = 1;
2759 else {
2760 if (ep->dma)
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01002761 abort_dma(ep);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002762 set_halt(ep);
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002763 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002764 allow_status_338x(ep);
2765 break;
2766
2767 default:
2768 goto usb3_delegate;
2769 }
2770
2771 break;
2772 default:
2773
2774usb3_delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002775 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002776 r.bRequestType, r.bRequest,
2777 w_value, w_index, w_length,
2778 readl(&ep->cfg->ep_cfg));
2779
2780 ep->responded = 0;
2781 spin_unlock(&dev->lock);
2782 tmp = dev->driver->setup(&dev->gadget, &r);
2783 spin_lock(&dev->lock);
2784 }
2785do_stall3:
2786 if (tmp < 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002787 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002788 r.bRequestType, r.bRequest, tmp);
2789 dev->protocol_stall = 1;
2790 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002791 set_halt(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002792 }
2793
2794next_endpoints3:
2795
2796#undef w_value
2797#undef w_index
2798#undef w_length
2799
2800 return;
2801}
2802
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002803static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
2805 struct net2280_ep *ep;
2806 u32 num, scratch;
2807
2808 /* most of these don't need individual acks */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002809 stat &= ~BIT(INTA_ASSERTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!stat)
2811 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002812 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 /* starting a control request? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002815 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 union {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002817 u32 raw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 struct usb_ctrlrequest r;
2819 } u;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002820 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct net2280_request *req;
2822
2823 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002824 u32 val = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002825 if (val & BIT(SUPER_SPEED)) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002826 dev->gadget.speed = USB_SPEED_SUPER;
2827 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2828 EP0_SS_MAX_PACKET_SIZE);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002829 } else if (val & BIT(HIGH_SPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 dev->gadget.speed = USB_SPEED_HIGH;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002831 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2832 EP0_HS_MAX_PACKET_SIZE);
2833 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 dev->gadget.speed = USB_SPEED_FULL;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002835 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2836 EP0_HS_MAX_PACKET_SIZE);
2837 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002838 net2280_led_speed(dev, dev->gadget.speed);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002839 ep_dbg(dev, "%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002840 usb_speed_string(dev->gadget.speed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002843 ep = &dev->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 ep->irqs++;
2845
2846 /* make sure any leftover request state is cleared */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002847 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002848 while (!list_empty(&ep->queue)) {
2849 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 struct net2280_request, queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002851 done(ep, req, (req->req.actual == req->req.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 ? 0 : -EPROTO);
2853 }
2854 ep->stopped = 0;
2855 dev->protocol_stall = 0;
Ricardo Ribalda Delgado5d1b6842014-11-28 14:50:53 +01002856 if (!(dev->quirks & PLX_SUPERSPEED)) {
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002857 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002858 tmp = BIT(FIFO_OVERFLOW) |
2859 BIT(FIFO_UNDERFLOW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002860 else
2861 tmp = 0;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002862
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002863 writel(tmp | BIT(TIMEOUT) |
2864 BIT(USB_STALL_SENT) |
2865 BIT(USB_IN_NAK_SENT) |
2866 BIT(USB_IN_ACK_RCVD) |
2867 BIT(USB_OUT_PING_NAK_SENT) |
2868 BIT(USB_OUT_ACK_SENT) |
2869 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
2870 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
2871 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2872 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2873 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002874 BIT(DATA_IN_TOKEN_INTERRUPT),
2875 &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002876 }
2877 u.raw[0] = readl(&dev->usb->setup0123);
2878 u.raw[1] = readl(&dev->usb->setup4567);
David Brownell901b3d72006-09-02 03:13:45 -07002879
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002880 cpu_to_le32s(&u.raw[0]);
2881 cpu_to_le32s(&u.raw[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002883 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002884 defect7374_workaround(dev, u.r);
2885
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002886 tmp = 0;
2887
David Brownell01ee7d72007-05-25 20:40:14 -07002888#define w_value le16_to_cpu(u.r.wValue)
2889#define w_index le16_to_cpu(u.r.wIndex)
2890#define w_length le16_to_cpu(u.r.wLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 /* ack the irq */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002893 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
2894 stat ^= BIT(SETUP_PACKET_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 /* watch control traffic at the token level, and force
2897 * synchronization before letting the status stage happen.
2898 * FIXME ignore tokens we'll NAK, until driver responds.
2899 * that'll mean a lot less irqs for some drivers.
2900 */
2901 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2902 if (ep->is_in) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002903 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2904 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2905 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002906 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 } else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002908 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2909 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2910 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002911 writel(scratch, &dev->epregs[0].ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 /* we made the hardware handle most lowlevel requests;
2914 * everything else goes uplevel to the gadget code.
2915 */
Alan Stern1f26e282006-11-16 10:16:00 -05002916 ep->responded = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002917
2918 if (dev->gadget.speed == USB_SPEED_SUPER) {
2919 handle_stat0_irqs_superspeed(dev, ep, u.r);
2920 goto next_endpoints;
2921 }
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 switch (u.r.bRequest) {
2924 case USB_REQ_GET_STATUS: {
2925 struct net2280_ep *e;
David Brownell320f3452005-05-07 13:05:18 -07002926 __le32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 /* hw handles device and interface status */
2929 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2930 goto delegate;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002931 e = get_ep_by_addr(dev, w_index);
2932 if (!e || w_length > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 goto do_stall;
2934
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002935 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002936 status = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002938 status = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 /* don't bother with a request object! */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002941 writel(0, &dev->epregs[0].ep_irqenb);
2942 set_fifo_bytecount(ep, w_length);
2943 writel((__force u32)status, &dev->epregs[0].ep_data);
2944 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002945 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 goto next_endpoints;
2947 }
2948 break;
2949 case USB_REQ_CLEAR_FEATURE: {
2950 struct net2280_ep *e;
2951
2952 /* hw handles device features */
2953 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2954 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002955 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002957 e = get_ep_by_addr(dev, w_index);
2958 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04002960 if (e->wedged) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002961 ep_vdbg(dev, "%s wedged, halt not cleared\n",
Alan Stern80661342008-08-14 15:49:11 -04002962 ep->ep.name);
2963 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002964 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Alan Stern80661342008-08-14 15:49:11 -04002965 clear_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002966 if ((ep->dev->quirks & PLX_SUPERSPEED) &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002967 !list_empty(&e->queue) && e->td_dma)
2968 restart_dma(e);
Alan Stern80661342008-08-14 15:49:11 -04002969 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002970 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 goto next_endpoints;
2972 }
2973 break;
2974 case USB_REQ_SET_FEATURE: {
2975 struct net2280_ep *e;
2976
2977 /* hw handles device features */
2978 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2979 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002980 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002982 e = get_ep_by_addr(dev, w_index);
2983 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04002985 if (e->ep.name == ep0name)
2986 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002987 set_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002988 if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002989 abort_dma(e);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002990 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002991 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 goto next_endpoints;
2993 }
2994 break;
2995 default:
2996delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002997 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 "ep_cfg %08x\n",
2999 u.r.bRequestType, u.r.bRequest,
David Brownell320f3452005-05-07 13:05:18 -07003000 w_value, w_index, w_length,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003001 readl(&ep->cfg->ep_cfg));
Alan Stern1f26e282006-11-16 10:16:00 -05003002 ep->responded = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003003 spin_unlock(&dev->lock);
3004 tmp = dev->driver->setup(&dev->gadget, &u.r);
3005 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
3007
3008 /* stall ep0 on error */
3009 if (tmp < 0) {
3010do_stall:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003011 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 u.r.bRequestType, u.r.bRequest, tmp);
3013 dev->protocol_stall = 1;
3014 }
3015
3016 /* some in/out token irq should follow; maybe stall then.
3017 * driver must queue a request (even zlp) or halt ep0
3018 * before the host times out.
3019 */
3020 }
3021
David Brownell320f3452005-05-07 13:05:18 -07003022#undef w_value
3023#undef w_index
3024#undef w_length
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026next_endpoints:
3027 /* endpoint data irq ? */
3028 scratch = stat & 0x7f;
3029 stat &= ~0x7f;
3030 for (num = 0; scratch; num++) {
3031 u32 t;
3032
3033 /* do this endpoint's FIFO and queue need tending? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003034 t = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if ((scratch & t) == 0)
3036 continue;
3037 scratch ^= t;
3038
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003039 ep = &dev->ep[num];
3040 handle_ep_small(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042
3043 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003044 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003047#define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3048 BIT(DMA_C_INTERRUPT) | \
3049 BIT(DMA_B_INTERRUPT) | \
3050 BIT(DMA_A_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051#define PCI_ERROR_INTERRUPTS ( \
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003052 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3053 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3054 BIT(PCI_RETRY_ABORT_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003056static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct net2280_ep *ep;
3059 u32 tmp, num, mask, scratch;
3060
3061 /* after disconnect there's nothing else to do! */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003062 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3063 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03003066 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
David Brownell901b3d72006-09-02 03:13:45 -07003067 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 * only indicates a change in the reset state).
3069 */
3070 if (stat & tmp) {
Alan Sternb611e422014-11-06 14:27:56 +08003071 bool reset = false;
3072 bool disconnect = false;
3073
3074 /*
3075 * Ignore disconnects and resets if the speed hasn't been set.
3076 * VBUS can bounce and there's always an initial reset.
3077 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003078 writel(tmp, &dev->regs->irqstat1);
Alan Sternb611e422014-11-06 14:27:56 +08003079 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3080 if ((stat & BIT(VBUS_INTERRUPT)) &&
3081 (readl(&dev->usb->usbctl) &
3082 BIT(VBUS_PIN)) == 0) {
3083 disconnect = true;
3084 ep_dbg(dev, "disconnect %s\n",
3085 dev->driver->driver.name);
3086 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3087 (readl(&dev->usb->usbstat) & mask)
3088 == 0) {
3089 reset = true;
3090 ep_dbg(dev, "reset %s\n",
3091 dev->driver->driver.name);
3092 }
3093
3094 if (disconnect || reset) {
3095 stop_activity(dev, dev->driver);
3096 ep0_start(dev);
3097 spin_unlock(&dev->lock);
3098 if (reset)
3099 usb_gadget_udc_reset
3100 (&dev->gadget, dev->driver);
3101 else
3102 (dev->driver->disconnect)
3103 (&dev->gadget);
3104 spin_lock(&dev->lock);
3105 return;
3106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
3108 stat &= ~tmp;
3109
3110 /* vBUS can bounce ... one of many reasons to ignore the
3111 * notion of hotplug events on bus connect/disconnect!
3112 */
3113 if (!stat)
3114 return;
3115 }
3116
3117 /* NOTE: chip stays in PCI D0 state for now, but it could
3118 * enter D1 to save more power
3119 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003120 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (stat & tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003122 writel(tmp, &dev->regs->irqstat1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003123 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (dev->driver->suspend)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003125 dev->driver->suspend(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (!enable_suspend)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003127 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 } else {
3129 if (dev->driver->resume)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003130 dev->driver->resume(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /* at high speed, note erratum 0133 */
3132 }
3133 stat &= ~tmp;
3134 }
3135
3136 /* clear any other status/irqs */
3137 if (stat)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003138 writel(stat, &dev->regs->irqstat1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 /* some status we can just ignore */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003141 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003142 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3143 BIT(SUSPEND_REQUEST_INTERRUPT) |
3144 BIT(RESUME_INTERRUPT) |
3145 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003146 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003147 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3148 BIT(RESUME_INTERRUPT) |
3149 BIT(SOF_DOWN_INTERRUPT) |
3150 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (!stat)
3153 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003154 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
3156 /* DMA status, for ep-{a,b,c,d} */
3157 scratch = stat & DMA_INTERRUPTS;
3158 stat &= ~DMA_INTERRUPTS;
3159 scratch >>= 9;
3160 for (num = 0; scratch; num++) {
3161 struct net2280_dma_regs __iomem *dma;
3162
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003163 tmp = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if ((tmp & scratch) == 0)
3165 continue;
3166 scratch ^= tmp;
3167
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003168 ep = &dev->ep[num + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 dma = ep->dma;
3170
3171 if (!dma)
3172 continue;
3173
3174 /* clear ep's dma status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003175 tmp = readl(&dma->dmastat);
3176 writel(tmp, &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003178 /* dma sync*/
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003179 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003180 u32 r_dmacount = readl(&dma->dmacount);
3181 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003182 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003183 continue;
3184 }
3185
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003186 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3187 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3188 ep->ep.name, tmp);
3189 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003191 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 /* OUT transfers terminate when the data from the
3194 * host is in our memory. Process whatever's done.
3195 * On this path, we know transfer's last packet wasn't
3196 * less than req->length. NAK_OUT_PACKETS may be set,
3197 * or the FIFO may already be holding new packets.
3198 *
3199 * IN transfers can linger in the FIFO for a very
3200 * long time ... we ignore that for now, accounting
3201 * precisely (like PIO does) needs per-packet irqs
3202 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003203 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 /* disable dma on inactive queues; else maybe restart */
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003206 if (!list_empty(&ep->queue)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003207 tmp = readl(&dma->dmactl);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003208 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 ep->irqs++;
3211 }
3212
3213 /* NOTE: there are other PCI errors we might usefully notice.
3214 * if they appear very often, here's where to try recovering.
3215 */
3216 if (stat & PCI_ERROR_INTERRUPTS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003217 ep_err(dev, "pci dma error; stat %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 stat &= ~PCI_ERROR_INTERRUPTS;
3219 /* these are fatal errors, but "maybe" they won't
3220 * happen again ...
3221 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003222 stop_activity(dev, dev->driver);
3223 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 stat = 0;
3225 }
3226
3227 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003228 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003231static irqreturn_t net2280_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
3233 struct net2280 *dev = _dev;
3234
Alan Stern658ad5e2006-04-14 16:44:11 -04003235 /* shared interrupt, not ours */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003236 if ((dev->quirks & PLX_LEGACY) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003237 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
Alan Stern658ad5e2006-04-14 16:44:11 -04003238 return IRQ_NONE;
3239
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003240 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 /* handle disconnect, dma, and more */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003243 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 /* control requests and PIO */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003246 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003248 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003249 /* re-enable interrupt to trigger any possible new interrupt */
3250 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3251 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3252 writel(pciirqenb1, &dev->regs->pciirqenb1);
3253 }
3254
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003255 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 return IRQ_HANDLED;
3258}
3259
3260/*-------------------------------------------------------------------------*/
3261
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003262static void gadget_release(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003264 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003266 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
3269/* tear down the binding between this driver and the pci device */
3270
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003271static void net2280_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003273 struct net2280 *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003275 usb_del_gadget_udc(&dev->gadget);
3276
David Brownell6bea4762006-12-05 03:15:33 -08003277 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279 /* then clean up the resources we allocated during probe() */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003280 net2280_led_shutdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (dev->requests) {
3282 int i;
3283 for (i = 1; i < 5; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003284 if (!dev->ep[i].dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003286 pci_pool_free(dev->requests, dev->ep[i].dummy,
3287 dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003289 pci_pool_destroy(dev->requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 }
3291 if (dev->got_irq)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003292 free_irq(pdev->irq, dev);
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003293 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003294 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (dev->regs)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003296 iounmap(dev->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 if (dev->region)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003298 release_mem_region(pci_resource_start(pdev, 0),
3299 pci_resource_len(pdev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (dev->enabled)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003301 pci_disable_device(pdev);
3302 device_remove_file(&pdev->dev, &dev_attr_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003304 ep_info(dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}
3306
3307/* wrap this driver around the specified device, but
3308 * don't respond over USB until a gadget driver binds to us.
3309 */
3310
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003311static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 struct net2280 *dev;
3314 unsigned long resource, len;
3315 void __iomem *base = NULL;
3316 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 /* alloc, and start init */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003319 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3320 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 retval = -ENOMEM;
3322 goto done;
3323 }
3324
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003325 pci_set_drvdata(pdev, dev);
3326 spin_lock_init(&dev->lock);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003327 dev->quirks = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 dev->pdev = pdev;
3329 dev->gadget.ops = &net2280_ops;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003330 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003331 USB_SPEED_SUPER : USB_SPEED_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
3333 /* the "gadget" abstracts/virtualizes the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 dev->gadget.name = driver_name;
3335
3336 /* now all the pci goodies ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003337 if (pci_enable_device(pdev) < 0) {
3338 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 goto done;
3340 }
3341 dev->enabled = 1;
3342
3343 /* BAR 0 holds all the registers
3344 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3345 * BAR 2 is fifo memory; unused here
3346 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003347 resource = pci_resource_start(pdev, 0);
3348 len = pci_resource_len(pdev, 0);
3349 if (!request_mem_region(resource, len, driver_name)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003350 ep_dbg(dev, "controller already in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 retval = -EBUSY;
3352 goto done;
3353 }
3354 dev->region = 1;
3355
David Brownell901b3d72006-09-02 03:13:45 -07003356 /* FIXME provide firmware download interface to put
3357 * 8051 code into the chip, e.g. to turn on PCI PM.
3358 */
3359
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003360 base = ioremap_nocache(resource, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 if (base == NULL) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003362 ep_dbg(dev, "can't map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 retval = -EFAULT;
3364 goto done;
3365 }
3366 dev->regs = (struct net2280_regs __iomem *) base;
3367 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3368 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3369 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3370 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3371 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3372
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003373 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003374 u32 fsmvalue;
3375 u32 usbstat;
3376 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3377 (base + 0x00b4);
3378 dev->fiforegs = (struct usb338x_fifo_regs __iomem *)
3379 (base + 0x0500);
3380 dev->llregs = (struct usb338x_ll_regs __iomem *)
3381 (base + 0x0700);
3382 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3383 (base + 0x0748);
3384 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3385 (base + 0x077c);
3386 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3387 (base + 0x079c);
3388 dev->plregs = (struct usb338x_pl_regs __iomem *)
3389 (base + 0x0800);
3390 usbstat = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003391 dev->enhanced_mode = !!(usbstat & BIT(11));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003392 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3393 /* put into initial config, link up all endpoints */
3394 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3395 (0xf << DEFECT7374_FSM_FIELD);
3396 /* See if firmware needs to set up for workaround: */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003397 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3398 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003399 writel(0, &dev->usb->usbctl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003400 } else
3401 dev->bug7734_patched = 0;
3402 } else {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003403 dev->enhanced_mode = 0;
3404 dev->n_ep = 7;
3405 /* put into initial config, link up all endpoints */
3406 writel(0, &dev->usb->usbctl);
3407 }
3408
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003409 usb_reset(dev);
3410 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
3412 /* irq setup after old hardware is cleaned up */
3413 if (!pdev->irq) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003414 ep_err(dev, "No IRQ. Check PCI setup!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 retval = -ENODEV;
3416 goto done;
3417 }
David S. Millerc6387a42006-06-20 01:21:29 -07003418
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003419 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003420 if (pci_enable_msi(pdev))
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003421 ep_err(dev, "Failed to enable MSI mode\n");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003422
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003423 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3424 driver_name, dev)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003425 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 retval = -EBUSY;
3427 goto done;
3428 }
3429 dev->got_irq = 1;
3430
3431 /* DMA setup */
3432 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003433 dev->requests = pci_pool_create("requests", pdev,
3434 sizeof(struct net2280_dma),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 0 /* no alignment requirements */,
3436 0 /* or page-crossing issues */);
3437 if (!dev->requests) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003438 ep_dbg(dev, "can't get request pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 retval = -ENOMEM;
3440 goto done;
3441 }
3442 for (i = 1; i < 5; i++) {
3443 struct net2280_dma *td;
3444
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003445 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3446 &dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 if (!td) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003448 ep_dbg(dev, "can't get dummy %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 retval = -ENOMEM;
3450 goto done;
3451 }
3452 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 td->dmadesc = td->dmaaddr;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003454 dev->ep[i].dummy = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
3456
3457 /* enable lower-overhead pci memory bursts during DMA */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003458 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003459 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3460 /*
3461 * 256 write retries may not be enough...
3462 BIT(PCI_RETRY_ABORT_ENABLE) |
3463 */
3464 BIT(DMA_READ_MULTIPLE_ENABLE) |
3465 BIT(DMA_READ_LINE_ENABLE),
3466 &dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003468 pci_set_master(pdev);
3469 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 /* ... also flushes any posted pci writes */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003472 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474 /* done */
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003475 ep_info(dev, "%s\n", driver_desc);
3476 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
David S. Millerc6387a42006-06-20 01:21:29 -07003477 pdev->irq, base, dev->chiprev);
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01003478 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003479 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003480 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3481 if (retval)
3482 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Felipe Balbi2901df62013-02-26 15:15:27 +02003484 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3485 gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003486 if (retval)
3487 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return 0;
3489
3490done:
3491 if (dev)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003492 net2280_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return retval;
3494}
3495
Alan Stern2d61bde2006-05-05 16:23:42 -04003496/* make sure the board is quiescent; otherwise it will continue
3497 * generating IRQs across the upcoming reboot.
3498 */
3499
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003500static void net2280_shutdown(struct pci_dev *pdev)
Alan Stern2d61bde2006-05-05 16:23:42 -04003501{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003502 struct net2280 *dev = pci_get_drvdata(pdev);
Alan Stern2d61bde2006-05-05 16:23:42 -04003503
3504 /* disable IRQs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003505 writel(0, &dev->regs->pciirqenb0);
3506 writel(0, &dev->regs->pciirqenb1);
Alan Stern2d61bde2006-05-05 16:23:42 -04003507
3508 /* disable the pullup so the host will think we're gone */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003509 writel(0, &dev->usb->usbctl);
Alan Stern2f076072013-01-30 16:40:14 -05003510
Alan Stern2d61bde2006-05-05 16:23:42 -04003511}
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514/*-------------------------------------------------------------------------*/
3515
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003516static const struct pci_device_id pci_ids[] = { {
David Brownell901b3d72006-09-02 03:13:45 -07003517 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3518 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003519 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 .device = 0x2280,
3521 .subvendor = PCI_ANY_ID,
3522 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003523 .driver_data = PLX_LEGACY | PLX_2280,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003524 }, {
David Brownell901b3d72006-09-02 03:13:45 -07003525 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3526 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003527 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003528 .device = 0x2282,
3529 .subvendor = PCI_ANY_ID,
3530 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003531 .driver_data = PLX_LEGACY,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003532 },
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003533 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003534 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3535 .class_mask = ~0,
3536 .vendor = PCI_VENDOR_ID_PLX,
3537 .device = 0x3380,
3538 .subvendor = PCI_ANY_ID,
3539 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003540 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003541 },
3542 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003543 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3544 .class_mask = ~0,
3545 .vendor = PCI_VENDOR_ID_PLX,
3546 .device = 0x3382,
3547 .subvendor = PCI_ANY_ID,
3548 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003549 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003550 },
3551{ /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552};
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003553MODULE_DEVICE_TABLE(pci, pci_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
3555/* pci driver glue; this is a "new style" PCI driver module */
3556static struct pci_driver net2280_pci_driver = {
3557 .name = (char *) driver_name,
3558 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560 .probe = net2280_probe,
3561 .remove = net2280_remove,
Alan Stern2d61bde2006-05-05 16:23:42 -04003562 .shutdown = net2280_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
3564 /* FIXME add power management support */
3565};
3566
Ricardo Ribalda Delgado9a028e42014-05-20 18:30:07 +02003567module_pci_driver(net2280_pci_driver);
3568
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003569MODULE_DESCRIPTION(DRIVER_DESC);
3570MODULE_AUTHOR("David Brownell");
3571MODULE_LICENSE("GPL");