blob: b7024dcce83aa58b7a685d4a8054eb121fe536cb [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
Mian Yousaf Kaukaba285f402015-02-02 10:55:23 +010083/* Endpoint names for usb3380 advance mode */
84static const char *const ep_name_adv[] = {
85 ep0name,
86 "ep1in", "ep2out", "ep3in", "ep4out",
87 "ep1out", "ep2in", "ep3out", "ep4in",
88};
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* mode 0 == ep-{a,b,c,d} 1K fifo each
91 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
92 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
93 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +020094static ushort fifo_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* "modprobe net2280 fifo_mode=1" etc */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +020097module_param(fifo_mode, ushort, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/* enable_suspend -- When enabled, the driver will respond to
100 * USB suspend requests by powering down the NET2280. Otherwise,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300101 * USB suspend requests will be ignored. This is acceptable for
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100102 * self-powered devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Ricardo Ribalda Delgado00d4db02014-05-20 18:30:06 +0200104static bool enable_suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* "modprobe net2280 enable_suspend=1" etc */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200107module_param(enable_suspend, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
110
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200111static char *type_string(u8 bmAttributes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
114 case USB_ENDPOINT_XFER_BULK: return "bulk";
115 case USB_ENDPOINT_XFER_ISOC: return "iso";
116 case USB_ENDPOINT_XFER_INT: return "intr";
Joe Perches2b84f922013-10-08 16:01:37 -0700117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return "control";
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121#include "net2280.h"
122
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200123#define valid_bit cpu_to_le32(BIT(VALID_BIT))
124#define dma_done_ie cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/*-------------------------------------------------------------------------*/
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200127static inline void enable_pciirqenb(struct net2280_ep *ep)
128{
129 u32 tmp = readl(&ep->dev->regs->pciirqenb0);
130
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200131 if (ep->dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200132 tmp |= BIT(ep->num);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200133 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200134 tmp |= BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200135 writel(tmp, &ep->dev->regs->pciirqenb0);
136
137 return;
138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200141net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct net2280 *dev;
144 struct net2280_ep *ep;
145 u32 max, tmp;
146 unsigned long flags;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200147 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200149 ep = container_of(_ep, struct net2280_ep, ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200150 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
151 desc->bDescriptorType != USB_DT_ENDPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return -EINVAL;
153 dev = ep->dev;
154 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
155 return -ESHUTDOWN;
156
157 /* erratum 0119 workaround ties up an endpoint number */
158 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
159 return -EDOM;
160
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200161 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200162 if ((desc->bEndpointAddress & 0x0f) >= 0x0c)
163 return -EDOM;
164 ep->is_in = !!usb_endpoint_dir_in(desc);
165 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num])
166 return -EINVAL;
167 }
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* sanity check ep-e/ep-f since their fifos are small */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200170 max = usb_endpoint_maxp(desc) & 0x1fff;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200171 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -ERANGE;
173
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200174 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 _ep->maxpacket = max & 0x7ff;
176 ep->desc = desc;
177
178 /* ep_reset() has already been called */
179 ep->stopped = 0;
Alan Stern80661342008-08-14 15:49:11 -0400180 ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 ep->out_overflow = 0;
182
183 /* set speed-dependent max packet; may kick in high bandwidth */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200184 set_max_speed(ep, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* set type, direction, address; reset fifo counters */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200187 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
189 if (tmp == USB_ENDPOINT_XFER_INT) {
190 /* erratum 0105 workaround prevents hs NYET */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200191 if (dev->chiprev == 0100 &&
192 dev->gadget.speed == USB_SPEED_HIGH &&
193 !(desc->bEndpointAddress & USB_DIR_IN))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200194 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 &ep->regs->ep_rsp);
196 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
197 /* catch some particularly blatant driver bugs */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200198 if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
199 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
200 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
201 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -ERANGE;
203 }
204 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200205 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200206 /* Enable this endpoint */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200207 if (dev->quirks & PLX_LEGACY) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200208 tmp <<= ENDPOINT_TYPE;
209 tmp |= desc->bEndpointAddress;
210 /* default full fifo lines */
211 tmp |= (4 << ENDPOINT_BYTE_COUNT);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200212 tmp |= BIT(ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200213 ep->is_in = (tmp & USB_DIR_IN) != 0;
214 } else {
215 /* In Legacy mode, only OUT endpoints are used */
216 if (dev->enhanced_mode && ep->is_in) {
217 tmp <<= IN_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200218 tmp |= BIT(IN_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200219 /* Not applicable to Legacy */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200220 tmp |= BIT(ENDPOINT_DIRECTION);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200221 } else {
222 tmp <<= OUT_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200223 tmp |= BIT(OUT_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200224 tmp |= (ep->is_in << ENDPOINT_DIRECTION);
225 }
226
227 tmp |= usb_endpoint_num(desc);
228 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
229 }
230
231 /* Make sure all the registers are written before ep_rsp*/
232 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* for OUT transfers, block the rx fifo until a read is posted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200236 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200237 else if (!(dev->quirks & PLX_2280)) {
David Brownell901b3d72006-09-02 03:13:45 -0700238 /* Added for 2282, Don't use nak packets on an in endpoint,
239 * this was ignored on 2280
240 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200241 writel(BIT(CLEAR_NAK_OUT_PACKETS) |
242 BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200245 writel(tmp, &ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* enable irqs */
248 if (!ep->dma) { /* pio, per-packet */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200249 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200251 tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
252 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200253 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200254 tmp |= readl(&ep->regs->ep_irqenb);
255 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } else { /* dma, per-request */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200257 tmp = BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200258 tmp |= readl(&dev->regs->pciirqenb1);
259 writel(tmp, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* for short OUT transfers, dma completions can't
262 * advance the queue; do it pio-style, by hand.
263 * NOTE erratum 0112 workaround #2
264 */
265 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200266 tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200267 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200269 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 }
272
273 tmp = desc->bEndpointAddress;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200274 ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200275 _ep->name, tmp & 0x0f, DIR_STRING(tmp),
276 type_string(desc->bmAttributes),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 ep->dma ? "dma" : "pio", max);
278
279 /* pci writes may still be posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200280 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282}
283
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200284static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 u32 result;
287
288 do {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200289 result = readl(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (result == ~(u32)0) /* "device unplugged" */
291 return -ENODEV;
292 result &= mask;
293 if (result == done)
294 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200295 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 usec--;
297 } while (usec > 0);
298 return -ETIMEDOUT;
299}
300
David Brownell901b3d72006-09-02 03:13:45 -0700301static const struct usb_ep_ops net2280_ep_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200303static void ep_reset_228x(struct net2280_regs __iomem *regs,
304 struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 u32 tmp;
307
308 ep->desc = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200309 INIT_LIST_HEAD(&ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Robert Baldygae117e742013-12-13 12:23:38 +0100311 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ep->ep.ops = &net2280_ep_ops;
313
314 /* disable the dma, irqs, endpoint... */
315 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200316 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200317 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
318 BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
319 BIT(DMA_ABORT),
320 &ep->dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200322 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200323 tmp &= ~BIT(ep->num);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200324 writel(tmp, &regs->pciirqenb0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200326 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200327 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200328 writel(tmp, &regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200330 writel(0, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* init to our chosen defaults, notably so that we NAK OUT
333 * packets until the driver queues a read (+note erratum 0112)
334 */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200335 if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200336 tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
337 BIT(SET_NAK_OUT_PACKETS) |
338 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
339 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100340 } else {
341 /* added for 2282 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200342 tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
343 BIT(CLEAR_NAK_OUT_PACKETS) |
344 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
345 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (ep->num != 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200349 tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
350 BIT(CLEAR_ENDPOINT_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200352 writel(tmp, &ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* scrub most status bits, and flush any fifo state */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200355 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200356 tmp = BIT(FIFO_OVERFLOW) |
357 BIT(FIFO_UNDERFLOW);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100358 else
359 tmp = 0;
360
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200361 writel(tmp | BIT(TIMEOUT) |
362 BIT(USB_STALL_SENT) |
363 BIT(USB_IN_NAK_SENT) |
364 BIT(USB_IN_ACK_RCVD) |
365 BIT(USB_OUT_PING_NAK_SENT) |
366 BIT(USB_OUT_ACK_SENT) |
367 BIT(FIFO_FLUSH) |
368 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
369 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
370 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
371 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
372 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200373 BIT(DATA_IN_TOKEN_INTERRUPT),
374 &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* fifo size is handled separately */
377}
378
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200379static void ep_reset_338x(struct net2280_regs __iomem *regs,
380 struct net2280_ep *ep)
381{
382 u32 tmp, dmastat;
383
384 ep->desc = NULL;
385 INIT_LIST_HEAD(&ep->queue);
386
387 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
388 ep->ep.ops = &net2280_ep_ops;
389
390 /* disable the dma, irqs, endpoint... */
391 if (ep->dma) {
392 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200393 writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
394 BIT(DMA_PAUSE_DONE_INTERRUPT) |
395 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200396 BIT(DMA_TRANSACTION_DONE_INTERRUPT),
397 /* | BIT(DMA_ABORT), */
398 &ep->dma->dmastat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200399
400 dmastat = readl(&ep->dma->dmastat);
401 if (dmastat == 0x5002) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200402 ep_warn(ep->dev, "The dmastat return = %x!!\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200403 dmastat);
404 writel(0x5a, &ep->dma->dmastat);
405 }
406
407 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200408 tmp &= ~BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200409 writel(tmp, &regs->pciirqenb0);
410 } else {
411 if (ep->num < 5) {
412 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200413 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200414 writel(tmp, &regs->pciirqenb1);
415 }
416 }
417 writel(0, &ep->regs->ep_irqenb);
418
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200419 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
420 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
421 BIT(FIFO_OVERFLOW) |
422 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
423 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
424 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
425 BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200426}
427
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200428static void nuke(struct net2280_ep *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200430static int net2280_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct net2280_ep *ep;
433 unsigned long flags;
434
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200435 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!_ep || !ep->desc || _ep->name == ep0name)
437 return -EINVAL;
438
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200439 spin_lock_irqsave(&ep->dev->lock, flags);
440 nuke(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200441
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200442 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200443 ep_reset_338x(ep->dev->regs, ep);
444 else
445 ep_reset_228x(ep->dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200447 ep_vdbg(ep->dev, "disabled %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 ep->dma ? "dma" : "pio", _ep->name);
449
450 /* synch memory views with the device */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200451 (void)readl(&ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +0100453 if (!ep->dma && ep->num >= 1 && ep->num <= 4)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200454 ep->dma = &ep->dev->dma[ep->num - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200456 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
460/*-------------------------------------------------------------------------*/
461
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200462static struct usb_request
463*net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct net2280_ep *ep;
466 struct net2280_request *req;
467
468 if (!_ep)
469 return NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200470 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800472 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!req)
474 return NULL;
475
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200476 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* this dma descriptor may be swapped with the previous dummy */
479 if (ep->dma) {
480 struct net2280_dma *td;
481
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200482 td = pci_pool_alloc(ep->dev->requests, gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 &req->td_dma);
484 if (!td) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200485 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return NULL;
487 }
488 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 td->dmadesc = td->dmaaddr;
490 req->td = td;
491 }
492 return &req->req;
493}
494
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200495static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct net2280_ep *ep;
498 struct net2280_request *req;
499
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200500 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!_ep || !_req)
502 return;
503
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200504 req = container_of(_req, struct net2280_request, req);
505 WARN_ON(!list_empty(&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (req->td)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200507 pci_pool_free(ep->dev->requests, req->td, req->td_dma);
508 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/*-------------------------------------------------------------------------*/
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/* load a packet into the fifo we use for usb IN transfers.
514 * works for all endpoints.
515 *
516 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
517 * at a time, but this code is simpler because it knows it only writes
518 * one packet. ep-a..ep-d should use dma instead.
519 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200520static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct net2280_ep_regs __iomem *regs = ep->regs;
523 u8 *buf;
524 u32 tmp;
525 unsigned count, total;
526
527 /* INVARIANT: fifo is currently empty. (testable) */
528
529 if (req) {
530 buf = req->buf + req->actual;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200531 prefetch(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 total = req->length - req->actual;
533 } else {
534 total = 0;
535 buf = NULL;
536 }
537
538 /* write just one packet at a time */
539 count = ep->ep.maxpacket;
540 if (count > total) /* min() cannot be used on a bitfield */
541 count = total;
542
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200543 ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 ep->ep.name, count,
545 (count != ep->ep.maxpacket) ? " (short)" : "",
546 req);
547 while (count >= 4) {
548 /* NOTE be careful if you try to align these. fifo lines
549 * should normally be full (4 bytes) and successive partial
550 * lines are ok only in certain cases.
551 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200552 tmp = get_unaligned((u32 *)buf);
553 cpu_to_le32s(&tmp);
554 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 buf += 4;
556 count -= 4;
557 }
558
559 /* last fifo entry is "short" unless we wrote a full packet.
560 * also explicitly validate last word in (periodic) transfers
561 * when maxpacket is not a multiple of 4 bytes.
562 */
563 if (count || total < ep->ep.maxpacket) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200564 tmp = count ? get_unaligned((u32 *)buf) : count;
565 cpu_to_le32s(&tmp);
566 set_fifo_bytecount(ep, count & 0x03);
567 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570 /* pci writes may still be posted */
571}
572
573/* work around erratum 0106: PCI and USB race over the OUT fifo.
574 * caller guarantees chiprev 0100, out endpoint is NAKing, and
575 * there's no real data in the fifo.
576 *
577 * NOTE: also used in cases where that erratum doesn't apply:
578 * where the host wrote "too much" data to us.
579 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200580static void out_flush(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 u32 __iomem *statp;
583 u32 tmp;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 statp = &ep->regs->ep_stat;
Ricardo Ribalda Delgadod82f3db2014-11-28 14:51:01 +0100586
587 tmp = readl(statp);
588 if (tmp & BIT(NAK_OUT_PACKETS)) {
589 ep_dbg(ep->dev, "%s %s %08x !NAK\n",
590 ep->ep.name, __func__, tmp);
591 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
592 }
593
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200594 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200595 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
596 statp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200597 writel(BIT(FIFO_FLUSH), statp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200598 /* Make sure that stap is written */
599 mb();
600 tmp = readl(statp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200601 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* high speed did bulk NYET; fifo isn't filling */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200603 ep->dev->gadget.speed == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unsigned usec;
605
606 usec = 50; /* 64 byte bulk/interrupt */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200607 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
608 BIT(USB_OUT_PING_NAK_SENT), usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
610 }
611}
612
613/* unload packet(s) from the fifo we use for usb OUT transfers.
614 * returns true iff the request completed, because of short packet
615 * or the request buffer having filled with full packets.
616 *
617 * for ep-a..ep-d this will read multiple packets out when they
618 * have been accepted.
619 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200620static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct net2280_ep_regs __iomem *regs = ep->regs;
623 u8 *buf = req->req.buf + req->req.actual;
624 unsigned count, tmp, is_short;
625 unsigned cleanup = 0, prevent = 0;
626
627 /* erratum 0106 ... packets coming in during fifo reads might
628 * be incompletely rejected. not all cases have workarounds.
629 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200630 if (ep->dev->chiprev == 0x0100 &&
631 ep->dev->gadget.speed == USB_SPEED_FULL) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200632 udelay(1);
633 tmp = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200634 if ((tmp & BIT(NAK_OUT_PACKETS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 cleanup = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200636 else if ((tmp & BIT(FIFO_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200637 start_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 prevent = 1;
639 }
640 /* else: hope we don't see the problem */
641 }
642
643 /* never overflow the rx buffer. the fifo reads packets until
644 * it sees a short one; we might not be ready for them all.
645 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200646 prefetchw(buf);
647 count = readl(&regs->ep_avail);
648 if (unlikely(count == 0)) {
649 udelay(1);
650 tmp = readl(&ep->regs->ep_stat);
651 count = readl(&regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* handled that data already? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200653 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655 }
656
657 tmp = req->req.length - req->req.actual;
658 if (count > tmp) {
659 /* as with DMA, data overflow gets flushed */
660 if ((tmp % ep->ep.maxpacket) != 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200661 ep_err(ep->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 "%s out fifo %d bytes, expected %d\n",
663 ep->ep.name, count, tmp);
664 req->req.status = -EOVERFLOW;
665 cleanup = 1;
666 /* NAK_OUT_PACKETS will be set, so flushing is safe;
667 * the next read will start with the next packet
668 */
669 } /* else it's a ZLP, no worries */
670 count = tmp;
671 }
672 req->req.actual += count;
673
674 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
675
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200676 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 -0700677 ep->ep.name, count, is_short ? " (short)" : "",
678 cleanup ? " flush" : "", prevent ? " nak" : "",
679 req, req->req.actual, req->req.length);
680
681 while (count >= 4) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200682 tmp = readl(&regs->ep_data);
683 cpu_to_le32s(&tmp);
684 put_unaligned(tmp, (u32 *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 buf += 4;
686 count -= 4;
687 }
688 if (count) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200689 tmp = readl(&regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* LE conversion is implicit here: */
691 do {
692 *buf++ = (u8) tmp;
693 tmp >>= 8;
694 } while (--count);
695 }
696 if (cleanup)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200697 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (prevent) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200699 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200700 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200703 return is_short || ((req->req.actual == req->req.length) &&
704 !req->req.zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707/* fill out dma descriptor to match a given request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200708static void fill_dma_desc(struct net2280_ep *ep,
709 struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct net2280_dma *td = req->td;
712 u32 dmacount = req->req.length;
713
714 /* don't let DMA continue after a short OUT packet,
715 * so overruns can't affect the next transfer.
716 * in case of overruns on max-size packets, we can't
717 * stop the fifo from filling but we can flush it.
718 */
719 if (ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200720 dmacount |= BIT(DMA_DIRECTION);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200721 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200722 !(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200723 dmacount |= BIT(END_OF_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 req->valid = valid;
726 if (valid)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200727 dmacount |= BIT(VALID_BIT);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100728 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* td->dmadesc = previously set by caller */
731 td->dmaaddr = cpu_to_le32 (req->req.dma);
732
733 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200734 wmb();
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700735 td->dmacount = cpu_to_le32(dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738static const u32 dmactl_default =
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200739 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
740 BIT(DMA_CLEAR_COUNT_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* erratum 0116 workaround part 1 (use POLLING) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200742 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
743 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
744 BIT(DMA_VALID_BIT_ENABLE) |
745 BIT(DMA_SCATTER_GATHER_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* erratum 0116 workaround part 2 (no AUTOSTART) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200747 BIT(DMA_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200749static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200751 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200754static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200756 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200757 spin_stop_dma(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200760static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct net2280_dma_regs __iomem *dma = ep->dma;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200763 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200765 if (!(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200766 tmp |= BIT(END_OF_CHAIN);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100767
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200768 writel(tmp, &dma->dmacount);
769 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200771 writel(td_dma, &dma->dmadesc);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200772 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200773 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200774 writel(dmactl, &dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200777 (void) readl(&ep->dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200779 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (!ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200782 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200785static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 u32 tmp;
788 struct net2280_dma_regs __iomem *dma = ep->dma;
789
790 /* FIXME can't use DMA for ZLPs */
791
792 /* on this path we "know" there's no dma active (yet) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200793 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200794 writel(0, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* previous OUT packet might have been short */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200797 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
798 BIT(NAK_OUT_PACKETS))) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200799 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 &ep->regs->ep_stat);
801
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200802 tmp = readl(&ep->regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200804 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 /* transfer all/some fifo data */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200807 writel(req->req.dma, &dma->dmaaddr);
808 tmp = min(tmp, req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /* dma irq, faking scatterlist status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200811 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200812 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
813 &dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 req->td->dmadesc = 0;
815 req->valid = 1;
816
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200817 writel(BIT(DMA_ENABLE), &dma->dmactl);
818 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return;
820 }
821 }
822
823 tmp = dmactl_default;
824
825 /* force packet boundaries between dma requests, but prevent the
826 * controller from automagically writing a last "short" packet
827 * (zero length) unless the driver explicitly said to do that.
828 */
829 if (ep->is_in) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200830 if (likely((req->req.length % ep->ep.maxpacket) ||
831 req->req.zero)){
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200832 tmp |= BIT(DMA_FIFO_VALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ep->in_fifo_validate = 1;
834 } else
835 ep->in_fifo_validate = 0;
836 }
837
838 /* init req->td, pointing to the current dummy */
839 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200840 fill_dma_desc(ep, req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100842 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200844 start_queue(ep, tmp, req->td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200848queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct net2280_dma *end;
851 dma_addr_t tmp;
852
853 /* swap new dummy for old, link; fill and maybe activate */
854 end = ep->dummy;
855 ep->dummy = req->td;
856 req->td = end;
857
858 tmp = ep->td_dma;
859 ep->td_dma = req->td_dma;
860 req->td_dma = tmp;
861
862 end->dmadesc = cpu_to_le32 (ep->td_dma);
863
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200864 fill_dma_desc(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867static void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200868done(struct net2280_ep *ep, struct net2280_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct net2280 *dev;
871 unsigned stopped = ep->stopped;
872
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200873 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 if (req->req.status == -EINPROGRESS)
876 req->req.status = status;
877 else
878 status = req->req.status;
879
880 dev = ep->dev;
Felipe Balbiae4d7932011-12-19 12:09:56 +0200881 if (ep->dma)
882 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (status && status != -ESHUTDOWN)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200885 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 ep->ep.name, &req->req, status,
887 req->req.actual, req->req.length);
888
889 /* don't modify queue heads during completion callback */
890 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200891 spin_unlock(&dev->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200892 usb_gadget_giveback_request(&ep->ep, &req->req);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200893 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 ep->stopped = stopped;
895}
896
897/*-------------------------------------------------------------------------*/
898
899static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200900net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 struct net2280_request *req;
903 struct net2280_ep *ep;
904 struct net2280 *dev;
905 unsigned long flags;
906
907 /* we always require a cpu-view buffer, so that we can
908 * always use pio (as fallback or whatever).
909 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200910 req = container_of(_req, struct net2280_request, req);
911 if (!_req || !_req->complete || !_req->buf ||
912 !list_empty(&req->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -EINVAL;
914 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
915 return -EDOM;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200916 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!_ep || (!ep->desc && ep->num != 0))
918 return -EINVAL;
919 dev = ep->dev;
920 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
921 return -ESHUTDOWN;
922
923 /* FIXME implement PIO fallback for ZLPs with DMA */
924 if (ep->dma && _req->length == 0)
925 return -EOPNOTSUPP;
926
927 /* set up dma mapping in case the caller didn't */
Felipe Balbiae4d7932011-12-19 12:09:56 +0200928 if (ep->dma) {
929 int ret;
930
931 ret = usb_gadget_map_request(&dev->gadget, _req,
932 ep->is_in);
933 if (ret)
934 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200937 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200940 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 _req->status = -EINPROGRESS;
943 _req->actual = 0;
944
945 /* kickstart this i/o queue? */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100946 if (list_empty(&ep->queue) && !ep->stopped &&
947 !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
948 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* use DMA if the endpoint supports it, else pio */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100951 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200952 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 else {
954 /* maybe there's no control data, just status ack */
955 if (ep->num == 0 && _req->length == 0) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200956 allow_status(ep);
957 done(ep, req, 0);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200958 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 goto done;
960 }
961
962 /* PIO ... stuff the fifo, or unblock it. */
963 if (ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200964 write_fifo(ep, _req);
965 else if (list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 u32 s;
967
968 /* OUT FIFO might have packet(s) buffered */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200969 s = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200970 if ((s & BIT(FIFO_EMPTY)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* note: _req->short_not_ok is
972 * ignored here since PIO _always_
973 * stops queue advance here, and
974 * _req->status doesn't change for
975 * short reads (only _req->actual)
976 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200977 if (read_fifo(ep, req) &&
978 ep->num == 0) {
979 done(ep, req, 0);
980 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* don't queue it */
982 req = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200983 } else if (read_fifo(ep, req) &&
984 ep->num != 0) {
985 done(ep, req, 0);
986 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200988 s = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
991 /* don't NAK, let the fifo fill */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200992 if (req && (s & BIT(NAK_OUT_PACKETS)))
993 writel(BIT(CLEAR_NAK_OUT_PACKETS),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 &ep->regs->ep_rsp);
995 }
996 }
997
998 } else if (ep->dma) {
999 int valid = 1;
1000
1001 if (ep->is_in) {
1002 int expect;
1003
1004 /* preventing magic zlps is per-engine state, not
1005 * per-transfer; irq logic must recover hiccups.
1006 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001007 expect = likely(req->req.zero ||
1008 (req->req.length % ep->ep.maxpacket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (expect != ep->in_fifo_validate)
1010 valid = 0;
1011 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001012 queue_dma(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 } /* else the irq handler advances the queue. */
1015
Alan Stern1f26e282006-11-16 10:16:00 -05001016 ep->responded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001018 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001020 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /* pci writes may still be posted */
1023 return 0;
1024}
1025
1026static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001027dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1028 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001031 done(ep, req, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001034static void scan_dma_completions(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 /* only look at descriptors that were "naturally" retired,
1037 * so fifo and list head state won't matter
1038 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001039 while (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct net2280_request *req;
1041 u32 tmp;
1042
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001043 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct net2280_request, queue);
1045 if (!req->valid)
1046 break;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001047 rmb();
1048 tmp = le32_to_cpup(&req->td->dmacount);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001049 if ((tmp & BIT(VALID_BIT)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051
1052 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1053 * cases where DMA must be aborted; this code handles
1054 * all non-abort DMA completions.
1055 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001056 if (unlikely(req->td->dmadesc == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* paranoia */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001058 tmp = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (tmp & DMA_BYTE_COUNT_MASK)
1060 break;
1061 /* single transfer mode */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001062 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02001064 } else if (!ep->is_in &&
Ricardo Ribalda Delgado43780aa2014-11-28 14:51:00 +01001065 (req->req.length % ep->ep.maxpacket) &&
1066 !(ep->dev->quirks & PLX_SUPERSPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Mario Schuknecht18a4e652014-11-16 21:21:45 +01001068 tmp = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* AVOID TROUBLE HERE by not issuing short reads from
1070 * your gadget driver. That helps avoids errata 0121,
1071 * 0122, and 0124; not all cases trigger the warning.
1072 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001073 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001074 ep_warn(ep->dev, "%s lost packet sync!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ep->ep.name);
1076 req->req.status = -EOVERFLOW;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001077 } else {
1078 tmp = readl(&ep->regs->ep_avail);
1079 if (tmp) {
1080 /* fifo gets flushed later */
1081 ep->out_overflow = 1;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001082 ep_dbg(ep->dev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001083 "%s dma, discard %d len %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ep->ep.name, tmp,
1085 req->req.length);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001086 req->req.status = -EOVERFLOW;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001090 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092}
1093
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001094static void restart_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 struct net2280_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 if (ep->stopped)
1099 return;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001100 req = list_entry(ep->queue.next, struct net2280_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01001102 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01001105static void abort_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 /* abort the current transfer */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001108 if (likely(!list_empty(&ep->queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* FIXME work around errata 0121, 0122, 0124 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001110 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001111 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001113 stop_dma(ep->dma);
1114 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117/* dequeue ALL requests */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001118static void nuke(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 struct net2280_request *req;
1121
1122 /* called with spinlock held */
1123 ep->stopped = 1;
1124 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001125 abort_dma(ep);
1126 while (!list_empty(&ep->queue)) {
1127 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct net2280_request,
1129 queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001130 done(ep, req, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132}
1133
1134/* dequeue JUST ONE request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001135static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct net2280_ep *ep;
1138 struct net2280_request *req;
1139 unsigned long flags;
1140 u32 dmactl;
1141 int stopped;
1142
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001143 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1145 return -EINVAL;
1146
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001147 spin_lock_irqsave(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 stopped = ep->stopped;
1149
1150 /* quiesce dma while we patch the queue */
1151 dmactl = 0;
1152 ep->stopped = 1;
1153 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001154 dmactl = readl(&ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /* WARNING erratum 0127 may kick in ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001156 stop_dma(ep->dma);
1157 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
1160 /* make sure it's still queued on this endpoint */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001161 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (&req->req == _req)
1163 break;
1164 }
1165 if (&req->req != _req) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001166 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return -EINVAL;
1168 }
1169
1170 /* queue head may be partially complete. */
1171 if (ep->queue.next == &req->queue) {
1172 if (ep->dma) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001173 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 _req->status = -ECONNRESET;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001175 abort_dma(ep);
1176 if (likely(ep->queue.next == &req->queue)) {
1177 /* NOTE: misreports single-transfer mode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 req->td->dmacount = 0; /* invalidate */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001179 dma_done(ep, req,
1180 readl(&ep->dma->dmacount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 -ECONNRESET);
1182 }
1183 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001184 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001185 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
1190 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001191 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ep->stopped = stopped;
1193
1194 if (ep->dma) {
1195 /* turn off dma on inactive queues */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001196 if (list_empty(&ep->queue))
1197 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 else if (!ep->stopped) {
1199 /* resume current request, or start new one */
1200 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001201 writel(dmactl, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001203 start_dma(ep, list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct net2280_request, queue));
1205 }
1206 }
1207
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001208 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return 0;
1210}
1211
1212/*-------------------------------------------------------------------------*/
1213
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001214static int net2280_fifo_status(struct usb_ep *_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216static int
Alan Stern80661342008-08-14 15:49:11 -04001217net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct net2280_ep *ep;
1220 unsigned long flags;
1221 int retval = 0;
1222
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001223 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (!_ep || (!ep->desc && ep->num != 0))
1225 return -EINVAL;
1226 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1227 return -ESHUTDOWN;
1228 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1229 == USB_ENDPOINT_XFER_ISOC)
1230 return -EINVAL;
1231
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001232 spin_lock_irqsave(&ep->dev->lock, flags);
1233 if (!list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 retval = -EAGAIN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001235 else if (ep->is_in && value && net2280_fifo_status(_ep) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 retval = -EAGAIN;
1237 else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001238 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
Alan Stern80661342008-08-14 15:49:11 -04001239 value ? "set" : "clear",
1240 wedged ? "wedge" : "halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* set/clear, then synch memory views with the device */
1242 if (value) {
1243 if (ep->num == 0)
1244 ep->dev->protocol_stall = 1;
1245 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001246 set_halt(ep);
Alan Stern80661342008-08-14 15:49:11 -04001247 if (wedged)
1248 ep->wedged = 1;
1249 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001250 clear_halt(ep);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001251 if (ep->dev->quirks & PLX_SUPERSPEED &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001252 !list_empty(&ep->queue) && ep->td_dma)
1253 restart_dma(ep);
Alan Stern80661342008-08-14 15:49:11 -04001254 ep->wedged = 0;
1255 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001256 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001258 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 return retval;
1261}
1262
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001263static int net2280_set_halt(struct usb_ep *_ep, int value)
Alan Stern80661342008-08-14 15:49:11 -04001264{
1265 return net2280_set_halt_and_wedge(_ep, value, 0);
1266}
1267
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001268static int net2280_set_wedge(struct usb_ep *_ep)
Alan Stern80661342008-08-14 15:49:11 -04001269{
1270 if (!_ep || _ep->name == ep0name)
1271 return -EINVAL;
1272 return net2280_set_halt_and_wedge(_ep, 1, 1);
1273}
1274
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001275static int net2280_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct net2280_ep *ep;
1278 u32 avail;
1279
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001280 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (!_ep || (!ep->desc && ep->num != 0))
1282 return -ENODEV;
1283 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1284 return -ESHUTDOWN;
1285
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001286 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (avail > ep->fifo_size)
1288 return -EOVERFLOW;
1289 if (ep->is_in)
1290 avail = ep->fifo_size - avail;
1291 return avail;
1292}
1293
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001294static void net2280_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 struct net2280_ep *ep;
1297
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001298 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (!_ep || (!ep->desc && ep->num != 0))
1300 return;
1301 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1302 return;
1303
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001304 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001305 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
David Brownell901b3d72006-09-02 03:13:45 -07001308static const struct usb_ep_ops net2280_ep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 .enable = net2280_enable,
1310 .disable = net2280_disable,
1311
1312 .alloc_request = net2280_alloc_request,
1313 .free_request = net2280_free_request,
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 .queue = net2280_queue,
1316 .dequeue = net2280_dequeue,
1317
1318 .set_halt = net2280_set_halt,
Alan Stern80661342008-08-14 15:49:11 -04001319 .set_wedge = net2280_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 .fifo_status = net2280_fifo_status,
1321 .fifo_flush = net2280_fifo_flush,
1322};
1323
1324/*-------------------------------------------------------------------------*/
1325
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001326static int net2280_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct net2280 *dev;
1329 unsigned long flags;
1330 u16 retval;
1331
1332 if (!_gadget)
1333 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001334 dev = container_of(_gadget, struct net2280, gadget);
1335 spin_lock_irqsave(&dev->lock, flags);
1336 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1337 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return retval;
1339}
1340
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001341static int net2280_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct net2280 *dev;
1344 u32 tmp;
1345 unsigned long flags;
1346
1347 if (!_gadget)
1348 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001349 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001351 spin_lock_irqsave(&dev->lock, flags);
1352 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001353 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1354 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001355 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /* pci writes may still be posted */
1358 return 0;
1359}
1360
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001361static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 struct net2280 *dev;
1364 u32 tmp;
1365 unsigned long flags;
1366
1367 if (!_gadget)
1368 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001369 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001371 spin_lock_irqsave(&dev->lock, flags);
1372 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001373 if (value) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001374 tmp |= BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001375 _gadget->is_selfpowered = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001376 } else {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001377 tmp &= ~BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001378 _gadget->is_selfpowered = 0;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001379 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001380 writel(tmp, &dev->usb->usbctl);
1381 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 return 0;
1384}
1385
1386static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1387{
1388 struct net2280 *dev;
1389 u32 tmp;
1390 unsigned long flags;
1391
1392 if (!_gadget)
1393 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001394 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001396 spin_lock_irqsave(&dev->lock, flags);
1397 tmp = readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 dev->softconnect = (is_on != 0);
1399 if (is_on)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001400 tmp |= BIT(USB_DETECT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001402 tmp &= ~BIT(USB_DETECT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001403 writel(tmp, &dev->usb->usbctl);
1404 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 return 0;
1407}
1408
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001409static int net2280_start(struct usb_gadget *_gadget,
1410 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001411static int net2280_stop(struct usb_gadget *_gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413static const struct usb_gadget_ops net2280_ops = {
1414 .get_frame = net2280_get_frame,
1415 .wakeup = net2280_wakeup,
1416 .set_selfpowered = net2280_set_selfpowered,
1417 .pullup = net2280_pullup,
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001418 .udc_start = net2280_start,
1419 .udc_stop = net2280_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420};
1421
1422/*-------------------------------------------------------------------------*/
1423
Ricardo Ribalda Delgadob99b4062014-07-04 11:27:03 +02001424#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426/* FIXME move these into procfs, and use seq_file.
1427 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1428 * and also doesn't help products using this with 2.4 kernels.
1429 */
1430
1431/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001432static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1433 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001435 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001437 if (!dev->driver || !dev->driver->function ||
1438 strlen(dev->driver->function) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001440 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001442static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001444static ssize_t registers_show(struct device *_dev,
1445 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct net2280 *dev;
1448 char *next;
1449 unsigned size, t;
1450 unsigned long flags;
1451 int i;
1452 u32 t1, t2;
Andrew Morton30e69592005-06-26 17:18:46 -07001453 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001455 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 next = buf;
1457 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001458 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (dev->driver)
1461 s = dev->driver->driver.name;
1462 else
1463 s = "(none)";
1464
1465 /* Main Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001466 t = scnprintf(next, size, "%s version " DRIVER_VERSION
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001467 ", chiprev %04x\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 "devinit %03x fifoctl %08x gadget '%s'\n"
1469 "pci irqenb0 %02x irqenb1 %08x "
1470 "irqstat0 %04x irqstat1 %08x\n",
1471 driver_name, dev->chiprev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001472 readl(&dev->regs->devinit),
1473 readl(&dev->regs->fifoctl),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 s,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001475 readl(&dev->regs->pciirqenb0),
1476 readl(&dev->regs->pciirqenb1),
1477 readl(&dev->regs->irqstat0),
1478 readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 size -= t;
1480 next += t;
1481
1482 /* USB Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001483 t1 = readl(&dev->usb->usbctl);
1484 t2 = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001485 if (t1 & BIT(VBUS_PIN)) {
1486 if (t2 & BIT(HIGH_SPEED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 s = "high speed";
1488 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1489 s = "powered";
1490 else
1491 s = "full speed";
1492 /* full speed bit (6) not working?? */
1493 } else
1494 s = "not attached";
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001495 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 "stdrsp %08x usbctl %08x usbstat %08x "
1497 "addr 0x%02x (%s)\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001498 readl(&dev->usb->stdrsp), t1, t2,
1499 readl(&dev->usb->ouraddr), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 size -= t;
1501 next += t;
1502
1503 /* PCI Master Control Registers */
1504
1505 /* DMA Control Registers */
1506
1507 /* Configurable EP Control Registers */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001508 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 struct net2280_ep *ep;
1510
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001511 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (i && !ep->desc)
1513 continue;
1514
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001515 t1 = readl(&ep->cfg->ep_cfg);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001516 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1517 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1519 "irqenb %02x\n",
1520 ep->ep.name, t1, t2,
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001521 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 ? "NAK " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001523 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 ? "hide " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001525 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 ? "CRC " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001527 (t2 & BIT(CLEAR_INTERRUPT_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 ? "interrupt " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001529 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 ? "status " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001531 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 ? "NAKmode " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001533 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 ? "DATA1 " : "DATA0 ",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001535 (t2 & BIT(CLEAR_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 ? "HALT " : "",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001537 readl(&ep->regs->ep_irqenb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 size -= t;
1539 next += t;
1540
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001541 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 "\tstat %08x avail %04x "
1543 "(ep%d%s-%s)%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001544 readl(&ep->regs->ep_stat),
1545 readl(&ep->regs->ep_avail),
1546 t1 & 0x0f, DIR_STRING(t1),
1547 type_string(t1 >> 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 ep->stopped ? "*" : "");
1549 size -= t;
1550 next += t;
1551
1552 if (!ep->dma)
1553 continue;
1554
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001555 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 " dma\tctl %08x stat %08x count %08x\n"
1557 "\taddr %08x desc %08x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001558 readl(&ep->dma->dmactl),
1559 readl(&ep->dma->dmastat),
1560 readl(&ep->dma->dmacount),
1561 readl(&ep->dma->dmaaddr),
1562 readl(&ep->dma->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 size -= t;
1564 next += t;
1565
1566 }
1567
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001568 /* Indexed Registers (none yet) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /* Statistics */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001571 t = scnprintf(next, size, "\nirqs: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 size -= t;
1573 next += t;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001574 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 struct net2280_ep *ep;
1576
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001577 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (i && !ep->irqs)
1579 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001580 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 size -= t;
1582 next += t;
1583
1584 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001585 t = scnprintf(next, size, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 size -= t;
1587 next += t;
1588
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001589 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 return PAGE_SIZE - size;
1592}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001593static DEVICE_ATTR_RO(registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001595static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1596 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct net2280 *dev;
1599 char *next;
1600 unsigned size;
1601 unsigned long flags;
1602 int i;
1603
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001604 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 next = buf;
1606 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001607 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001609 for (i = 0; i < dev->n_ep; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001610 struct net2280_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 struct net2280_request *req;
1612 int t;
1613
1614 if (i != 0) {
1615 const struct usb_endpoint_descriptor *d;
1616
1617 d = ep->desc;
1618 if (!d)
1619 continue;
1620 t = d->bEndpointAddress;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001621 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1623 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1624 (t & USB_DIR_IN) ? "in" : "out",
Ricardo Ribalda Delgadoa27f37a2014-05-20 18:30:08 +02001625 type_string(d->bmAttributes),
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001626 usb_endpoint_maxp(d) & 0x1fff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ep->dma ? "dma" : "pio", ep->fifo_size
1628 );
1629 } else /* ep0 should only have one transfer queued */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001630 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 ep->is_in ? "in" : "out");
1632 if (t <= 0 || t > size)
1633 goto done;
1634 size -= t;
1635 next += t;
1636
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001637 if (list_empty(&ep->queue)) {
1638 t = scnprintf(next, size, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (t <= 0 || t > size)
1640 goto done;
1641 size -= t;
1642 next += t;
1643 continue;
1644 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001645 list_for_each_entry(req, &ep->queue, queue) {
1646 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1647 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 "\treq %p len %d/%d "
1649 "buf %p (dmacount %08x)\n",
1650 &req->req, req->req.actual,
1651 req->req.length, req->req.buf,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001652 readl(&ep->dma->dmacount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001654 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 "\treq %p len %d/%d buf %p\n",
1656 &req->req, req->req.actual,
1657 req->req.length, req->req.buf);
1658 if (t <= 0 || t > size)
1659 goto done;
1660 size -= t;
1661 next += t;
1662
1663 if (ep->dma) {
1664 struct net2280_dma *td;
1665
1666 td = req->td;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001667 t = scnprintf(next, size, "\t td %08x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 " count %08x buf %08x desc %08x\n",
1669 (u32) req->td_dma,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001670 le32_to_cpu(td->dmacount),
1671 le32_to_cpu(td->dmaaddr),
1672 le32_to_cpu(td->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (t <= 0 || t > size)
1674 goto done;
1675 size -= t;
1676 next += t;
1677 }
1678 }
1679 }
1680
1681done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001682 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return PAGE_SIZE - size;
1684}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001685static DEVICE_ATTR_RO(queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687
1688#else
1689
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001690#define device_create_file(a, b) (0)
1691#define device_remove_file(a, b) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693#endif
1694
1695/*-------------------------------------------------------------------------*/
1696
1697/* another driver-specific mode might be a request type doing dma
1698 * to/from another device fifo instead of to/from memory.
1699 */
1700
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001701static void set_fifo_mode(struct net2280 *dev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 /* keeping high bits preserves BAR2 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001704 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001707 INIT_LIST_HEAD(&dev->gadget.ep_list);
1708 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1709 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 switch (mode) {
1711 case 0:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001712 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1713 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1714 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 break;
1716 case 1:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001717 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
1719 case 2:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001720 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1721 dev->ep[1].fifo_size = 2048;
1722 dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 break;
1724 }
1725 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001726 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1727 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001730static void defect7374_disable_data_eps(struct net2280 *dev)
1731{
1732 /*
1733 * For Defect 7374, disable data EPs (and more):
1734 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1735 * returing ep regs back to normal.
1736 */
1737 struct net2280_ep *ep;
1738 int i;
1739 unsigned char ep_sel;
1740 u32 tmp_reg;
1741
1742 for (i = 1; i < 5; i++) {
1743 ep = &dev->ep[i];
1744 writel(0, &ep->cfg->ep_cfg);
1745 }
1746
1747 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1748 for (i = 0; i < 6; i++)
1749 writel(0, &dev->dep[i].dep_cfg);
1750
1751 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1752 /* Select an endpoint for subsequent operations: */
1753 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1754 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1755
1756 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1757 ep_sel == 18 || ep_sel == 20)
1758 continue;
1759
1760 /* Change settings on some selected endpoints */
1761 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001762 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001763 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1764 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001765 tmp_reg |= BIT(EP_INITIALIZED);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001766 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1767 }
1768}
1769
1770static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1771{
1772 u32 tmp = 0, tmp_reg;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001773 u32 scratch;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001774 int i;
1775 unsigned char ep_sel;
1776
1777 scratch = get_idx_reg(dev->regs, SCRATCH);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001778
1779 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1780 == DEFECT7374_FSM_SS_CONTROL_READ);
1781
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001782 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1783
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001784 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1785 ep_warn(dev, "It will operate on cold-reboot and SS connect");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001786
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001787 /*GPEPs:*/
1788 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1789 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1790 ((dev->enhanced_mode) ?
1791 BIT(OUT_ENDPOINT_ENABLE) : BIT(ENDPOINT_ENABLE)) |
1792 BIT(IN_ENDPOINT_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001793
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001794 for (i = 1; i < 5; i++)
1795 writel(tmp, &dev->ep[i].cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001796
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001797 /* CSRIN, PCIIN, STATIN, RCIN*/
1798 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1799 writel(tmp, &dev->dep[1].dep_cfg);
1800 writel(tmp, &dev->dep[3].dep_cfg);
1801 writel(tmp, &dev->dep[4].dep_cfg);
1802 writel(tmp, &dev->dep[5].dep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001803
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001804 /*Implemented for development and debug.
1805 * Can be refined/tuned later.*/
1806 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1807 /* Select an endpoint for subsequent operations: */
1808 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1809 writel(((tmp_reg & ~0x1f) | ep_sel),
1810 &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001811
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001812 if (ep_sel == 1) {
1813 tmp =
1814 (readl(&dev->plregs->pl_ep_ctrl) |
1815 BIT(CLEAR_ACK_ERROR_CODE) | 0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001816 writel(tmp, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001817 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001818 }
1819
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001820 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1821 ep_sel == 18 || ep_sel == 20)
1822 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001823
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001824 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1825 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1826 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1827
1828 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1829 ~BIT(EP_INITIALIZED);
1830 writel(tmp, &dev->plregs->pl_ep_ctrl);
1831
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001832 }
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001833
1834 /* Set FSM to focus on the first Control Read:
1835 * - Tip: Connection speed is known upon the first
1836 * setup request.*/
1837 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1838 set_idx_reg(dev->regs, SCRATCH, scratch);
1839
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001840}
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842/* keeping it simple:
1843 * - one bus driver, initted first;
1844 * - one function driver, initted second
1845 *
1846 * most of the work to support multiple net2280 controllers would
1847 * be to associate this gadget driver (yes?) with all of them, or
1848 * perhaps to bind specific drivers to specific devices.
1849 */
1850
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001851static void usb_reset_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 u32 tmp;
1854
1855 dev->gadget.speed = USB_SPEED_UNKNOWN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001856 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001858 net2280_led_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /* disable automatic responses, and irqs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001861 writel(0, &dev->usb->stdrsp);
1862 writel(0, &dev->regs->pciirqenb0);
1863 writel(0, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /* clear old dma and irq state */
1866 for (tmp = 0; tmp < 4; tmp++) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001867 struct net2280_ep *ep = &dev->ep[tmp + 1];
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001868 if (ep->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001869 abort_dma(ep);
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001870 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001871
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001872 writel(~0, &dev->regs->irqstat0),
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001873 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 /* reset, and enable pci */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001876 tmp = readl(&dev->regs->devinit) |
1877 BIT(PCI_ENABLE) |
1878 BIT(FIFO_SOFT_RESET) |
1879 BIT(USB_SOFT_RESET) |
1880 BIT(M8051_RESET);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001881 writel(tmp, &dev->regs->devinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 /* standard fifo and endpoint allocations */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001884 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001887static void usb_reset_338x(struct net2280 *dev)
1888{
1889 u32 tmp;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001890
1891 dev->gadget.speed = USB_SPEED_UNKNOWN;
1892 (void)readl(&dev->usb->usbctl);
1893
1894 net2280_led_init(dev);
1895
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001896 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001897 /* disable automatic responses, and irqs */
1898 writel(0, &dev->usb->stdrsp);
1899 writel(0, &dev->regs->pciirqenb0);
1900 writel(0, &dev->regs->pciirqenb1);
1901 }
1902
1903 /* clear old dma and irq state */
1904 for (tmp = 0; tmp < 4; tmp++) {
1905 struct net2280_ep *ep = &dev->ep[tmp + 1];
1906
1907 if (ep->dma)
1908 abort_dma(ep);
1909 }
1910
1911 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
1912
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001913 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001914 /* reset, and enable pci */
1915 tmp = readl(&dev->regs->devinit) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001916 BIT(PCI_ENABLE) |
1917 BIT(FIFO_SOFT_RESET) |
1918 BIT(USB_SOFT_RESET) |
1919 BIT(M8051_RESET);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001920
1921 writel(tmp, &dev->regs->devinit);
1922 }
1923
1924 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
1925 INIT_LIST_HEAD(&dev->gadget.ep_list);
1926
1927 for (tmp = 1; tmp < dev->n_ep; tmp++)
1928 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
1929
1930}
1931
1932static void usb_reset(struct net2280 *dev)
1933{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001934 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001935 return usb_reset_228x(dev);
1936 return usb_reset_338x(dev);
1937}
1938
1939static void usb_reinit_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 /* basic endpoint init */
1944 for (tmp = 0; tmp < 7; tmp++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001945 struct net2280_ep *ep = &dev->ep[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001947 ep->ep.name = ep_name[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 ep->dev = dev;
1949 ep->num = tmp;
1950
1951 if (tmp > 0 && tmp <= 4) {
1952 ep->fifo_size = 1024;
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001953 ep->dma = &dev->dma[tmp - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 } else
1955 ep->fifo_size = 64;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001956 ep->regs = &dev->epregs[tmp];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001957 ep->cfg = &dev->epregs[tmp];
1958 ep_reset_228x(dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001960 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
1961 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
1962 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001964 dev->gadget.ep0 = &dev->ep[0].ep;
1965 dev->ep[0].stopped = 0;
1966 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /* we want to prevent lowlevel/insecure access from the USB host,
1969 * but erratum 0119 means this enable bit is ignored
1970 */
1971 for (tmp = 0; tmp < 5; tmp++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001972 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001975static void usb_reinit_338x(struct net2280 *dev)
1976{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001977 int i;
1978 u32 tmp, val;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001979 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
1980 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
1981 0x00, 0xC0, 0x00, 0xC0 };
1982
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001983 /* basic endpoint init */
1984 for (i = 0; i < dev->n_ep; i++) {
1985 struct net2280_ep *ep = &dev->ep[i];
1986
Mian Yousaf Kaukaba285f402015-02-02 10:55:23 +01001987 ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001988 ep->dev = dev;
1989 ep->num = i;
1990
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001991 if (i > 0 && i <= 4)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001992 ep->dma = &dev->dma[i - 1];
1993
1994 if (dev->enhanced_mode) {
1995 ep->cfg = &dev->epregs[ne[i]];
1996 ep->regs = (struct net2280_ep_regs __iomem *)
Felipe Balbic43e97b2014-07-16 12:20:25 -05001997 (((void __iomem *)&dev->epregs[ne[i]]) +
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001998 ep_reg_addr[i]);
1999 ep->fiforegs = &dev->fiforegs[i];
2000 } else {
2001 ep->cfg = &dev->epregs[i];
2002 ep->regs = &dev->epregs[i];
2003 ep->fiforegs = &dev->fiforegs[i];
2004 }
2005
2006 ep->fifo_size = (i != 0) ? 2048 : 512;
2007
2008 ep_reset_338x(dev->regs, ep);
2009 }
2010 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2011
2012 dev->gadget.ep0 = &dev->ep[0].ep;
2013 dev->ep[0].stopped = 0;
2014
2015 /* Link layer set up */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002016 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002017 tmp = readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002018 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002019 writel(tmp, &dev->usb_ext->usbctl2);
2020 }
2021
2022 /* Hardware Defect and Workaround */
2023 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2024 val &= ~(0xf << TIMER_LFPS_6US);
2025 val |= 0x5 << TIMER_LFPS_6US;
2026 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2027
2028 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2029 val &= ~(0xffff << TIMER_LFPS_80US);
2030 val |= 0x0100 << TIMER_LFPS_80US;
2031 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2032
2033 /*
2034 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2035 * Hot Reset Exit Handshake may Fail in Specific Case using
2036 * Default Register Settings. Workaround for Enumeration test.
2037 */
2038 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2039 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2040 val |= 0x10 << HOT_TX_NORESET_TS2;
2041 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2042
2043 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2044 val &= ~(0x1f << HOT_RX_RESET_TS2);
2045 val |= 0x3 << HOT_RX_RESET_TS2;
2046 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2047
2048 /*
2049 * Set Recovery Idle to Recover bit:
2050 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2051 * link robustness with various hosts and hubs.
2052 * - It is safe to set for all connection speeds; all chip revisions.
2053 * - R-M-W to leave other bits undisturbed.
2054 * - Reference PLX TT-7372
2055 */
2056 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002057 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002058 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2059
2060 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2061
2062 /* disable dedicated endpoints */
2063 writel(0x0D, &dev->dep[0].dep_cfg);
2064 writel(0x0D, &dev->dep[1].dep_cfg);
2065 writel(0x0E, &dev->dep[2].dep_cfg);
2066 writel(0x0E, &dev->dep[3].dep_cfg);
2067 writel(0x0F, &dev->dep[4].dep_cfg);
2068 writel(0x0C, &dev->dep[5].dep_cfg);
2069}
2070
2071static void usb_reinit(struct net2280 *dev)
2072{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002073 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002074 return usb_reinit_228x(dev);
2075 return usb_reinit_338x(dev);
2076}
2077
2078static void ep0_start_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002080 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2081 BIT(CLEAR_NAK_OUT_PACKETS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002082 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2083 &dev->epregs[0].ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 /*
2086 * hardware optionally handles a bunch of standard requests
2087 * that the API hides from drivers anyway. have it do so.
2088 * endpoint status/features are handled in software, to
2089 * help pass tests for some dubious behavior.
2090 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002091 writel(BIT(SET_TEST_MODE) |
2092 BIT(SET_ADDRESS) |
2093 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2094 BIT(GET_DEVICE_STATUS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002095 BIT(GET_INTERFACE_STATUS),
2096 &dev->usb->stdrsp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002097 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2098 BIT(SELF_POWERED_USB_DEVICE) |
2099 BIT(REMOTE_WAKEUP_SUPPORT) |
2100 (dev->softconnect << USB_DETECT_ENABLE) |
2101 BIT(SELF_POWERED_STATUS),
2102 &dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002105 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2106 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2107 &dev->regs->pciirqenb0);
2108 writel(BIT(PCI_INTERRUPT_ENABLE) |
2109 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2110 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2111 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2112 BIT(VBUS_INTERRUPT_ENABLE) |
2113 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2114 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2115 &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 /* don't leave any writes posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002118 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002121static void ep0_start_338x(struct net2280 *dev)
2122{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002123
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002124 if (dev->bug7734_patched)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002125 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2126 BIT(SET_EP_HIDE_STATUS_PHASE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002127 &dev->epregs[0].ep_rsp);
2128
2129 /*
2130 * hardware optionally handles a bunch of standard requests
2131 * that the API hides from drivers anyway. have it do so.
2132 * endpoint status/features are handled in software, to
2133 * help pass tests for some dubious behavior.
2134 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002135 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2136 BIT(SET_SEL) |
2137 BIT(SET_TEST_MODE) |
2138 BIT(SET_ADDRESS) |
2139 BIT(GET_INTERFACE_STATUS) |
2140 BIT(GET_DEVICE_STATUS),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002141 &dev->usb->stdrsp);
2142 dev->wakeup_enable = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002143 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002144 (dev->softconnect << USB_DETECT_ENABLE) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002145 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002146 &dev->usb->usbctl);
2147
2148 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002149 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002150 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2151 &dev->regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002152 writel(BIT(PCI_INTERRUPT_ENABLE) |
2153 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2154 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2155 BIT(VBUS_INTERRUPT_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002156 &dev->regs->pciirqenb1);
2157
2158 /* don't leave any writes posted */
2159 (void)readl(&dev->usb->usbctl);
2160}
2161
2162static void ep0_start(struct net2280 *dev)
2163{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002164 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002165 return ep0_start_228x(dev);
2166 return ep0_start_338x(dev);
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169/* when a driver is successfully registered, it will receive
2170 * control requests including set_configuration(), which enables
2171 * non-control requests. then usb traffic follows until a
2172 * disconnect is reported. then a host may connect again, or
2173 * the driver might get unbound.
2174 */
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002175static int net2280_start(struct usb_gadget *_gadget,
2176 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002178 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 int retval;
2180 unsigned i;
2181
2182 /* insist on high speed support from the driver, since
2183 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2184 * "must not be used in normal operation"
2185 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002186 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2187 !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return -EINVAL;
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002189
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002190 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002192 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002193 dev->ep[i].irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 /* hook up the driver ... */
2196 dev->softconnect = 1;
2197 driver->driver.bus = NULL;
2198 dev->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002200 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2201 if (retval)
2202 goto err_unbind;
2203 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2204 if (retval)
2205 goto err_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Ricardo Ribalda Delgado7a74c482014-11-28 14:50:47 +01002207 /* enable host detection and ep0; and we're ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 * for set_configuration as well as eventual disconnect.
2209 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002210 net2280_led_active(dev, 1);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002211
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002212 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002213 defect7374_enable_data_eps_zero(dev);
2214
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002215 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /* pci writes may still be posted */
2218 return 0;
Jeff Garzikb3899da2006-10-11 21:50:24 -04002219
2220err_func:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002221 device_remove_file(&dev->pdev->dev, &dev_attr_function);
Jeff Garzikb3899da2006-10-11 21:50:24 -04002222err_unbind:
Jeff Garzikb3899da2006-10-11 21:50:24 -04002223 dev->driver = NULL;
2224 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002227static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 int i;
2230
2231 /* don't disconnect if it's not connected */
2232 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2233 driver = NULL;
2234
2235 /* stop hardware; prevent new request submissions;
2236 * and kill any outstanding requests.
2237 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002238 usb_reset(dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002239 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002240 nuke(&dev->ep[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Felipe Balbi699412d2013-03-18 10:14:47 +02002242 /* report disconnect; the driver is already quiesced */
2243 if (driver) {
2244 spin_unlock(&dev->lock);
2245 driver->disconnect(&dev->gadget);
2246 spin_lock(&dev->lock);
2247 }
2248
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002249 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
Felipe Balbi22835b82014-10-17 12:05:12 -05002252static int net2280_stop(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002254 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 unsigned long flags;
2256
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002257 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002259 spin_lock_irqsave(&dev->lock, flags);
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002260 stop_activity(dev, NULL);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002261 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002263 net2280_led_active(dev, 0);
Alan Stern2f076072013-01-30 16:40:14 -05002264
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002265 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2266 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002268 dev->driver = NULL;
Ricardo Ribalda Delgado84237bf2014-05-15 14:28:45 +02002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return 0;
2271}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273/*-------------------------------------------------------------------------*/
2274
2275/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2276 * also works for dma-capable endpoints, in pio mode or just
2277 * to manually advance the queue after short OUT transfers.
2278 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002279static void handle_ep_small(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 struct net2280_request *req;
2282 u32 t;
2283 /* 0 error, 1 mid-data, 2 done */
2284 int mode = 1;
2285
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002286 if (!list_empty(&ep->queue))
2287 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 struct net2280_request, queue);
2289 else
2290 req = NULL;
2291
2292 /* ack all, and handle what we care about */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002293 t = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 ep->irqs++;
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002295
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002296 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
Ricardo Ribalda Delgadofc12c682015-01-13 10:54:58 +01002297 ep->ep.name, t, req ? &req->req : NULL);
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002298
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002299 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002300 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002301 else
2302 /* Added for 2282 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002303 writel(t, &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 /* for ep0, monitor token irqs to catch data stage length errors
2306 * and to synchronize on status.
2307 *
2308 * also, to defer reporting of protocol stalls ... here's where
2309 * data or status first appears, handling stalls here should never
2310 * cause trouble on the host side..
2311 *
2312 * control requests could be slightly faster without token synch for
2313 * status, but status can jam up that way.
2314 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002315 if (unlikely(ep->num == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (ep->is_in) {
2317 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002318 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (ep->dev->protocol_stall) {
2320 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002321 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323 if (!req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002324 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 mode = 2;
2326 /* reply to extra IN data tokens with a zlp */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002327 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (ep->dev->protocol_stall) {
2329 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002330 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 mode = 2;
Alan Stern1f26e282006-11-16 10:16:00 -05002332 } else if (ep->responded &&
2333 !req && !ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002334 write_fifo(ep, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 } else {
2337 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002338 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (ep->dev->protocol_stall) {
2340 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002341 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343 mode = 2;
2344 /* an extra OUT token is an error */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002345 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2346 req &&
2347 req->req.actual == req->req.length) ||
2348 (ep->responded && !req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 ep->dev->protocol_stall = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002350 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 ep->stopped = 1;
2352 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002353 done(ep, req, -EOVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 req = NULL;
2355 }
2356 }
2357 }
2358
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002359 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return;
2361
2362 /* manual DMA queue advance after short OUT */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002363 if (likely(ep->dma)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002364 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 u32 count;
2366 int stopped = ep->stopped;
2367
2368 /* TRANSFERRED works around OUT_DONE erratum 0112.
2369 * we expect (N <= maxpacket) bytes; host wrote M.
2370 * iff (M < N) we won't ever see a DMA interrupt.
2371 */
2372 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002373 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 /* any preceding dma transfers must finish.
2376 * dma handles (M >= N), may empty the queue
2377 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002378 scan_dma_completions(ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002379 if (unlikely(list_empty(&ep->queue) ||
2380 ep->out_overflow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 req = NULL;
2382 break;
2383 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002384 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 struct net2280_request, queue);
2386
2387 /* here either (M < N), a "real" short rx;
2388 * or (M == N) and the queue didn't empty
2389 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002390 if (likely(t & BIT(FIFO_EMPTY))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002391 count = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 count &= DMA_BYTE_COUNT_MASK;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002393 if (readl(&ep->dma->dmadesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 != req->td_dma)
2395 req = NULL;
2396 break;
2397 }
2398 udelay(1);
2399 }
2400
2401 /* stop DMA, leave ep NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002402 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002403 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002405 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 req->td->dmacount = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002407 t = readl(&ep->regs->ep_avail);
2408 dma_done(ep, req, count,
David Brownell901b3d72006-09-02 03:13:45 -07002409 (ep->out_overflow || t)
2410 ? -EOVERFLOW : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412
2413 /* also flush to prevent erratum 0106 trouble */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002414 if (unlikely(ep->out_overflow ||
2415 (ep->dev->chiprev == 0x0100 &&
2416 ep->dev->gadget.speed
2417 == USB_SPEED_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002418 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 ep->out_overflow = 0;
2420 }
2421
2422 /* (re)start dma if needed, stop NAKing */
2423 ep->stopped = stopped;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002424 if (!list_empty(&ep->queue))
2425 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 } else
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002427 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 ep->ep.name, t);
2429 return;
2430
2431 /* data packet(s) received (in the fifo, OUT) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002432 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002433 if (read_fifo(ep, req) && ep->num != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 mode = 2;
2435
2436 /* data packet(s) transmitted (IN) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002437 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 unsigned len;
2439
2440 len = req->req.length - req->req.actual;
2441 if (len > ep->ep.maxpacket)
2442 len = ep->ep.maxpacket;
2443 req->req.actual += len;
2444
2445 /* if we wrote it all, we're usually done */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002446 /* send zlps until the status stage */
2447 if ((req->req.actual == req->req.length) &&
2448 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 mode = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /* there was nothing to do ... */
2452 } else if (mode == 1)
2453 return;
2454
2455 /* done */
2456 if (mode == 2) {
2457 /* stream endpoints often resubmit/unlink in completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002458 done(ep, req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 /* maybe advance queue to next request */
2461 if (ep->num == 0) {
2462 /* NOTE: net2280 could let gadget driver start the
2463 * status stage later. since not all controllers let
2464 * them control that, the api doesn't (yet) allow it.
2465 */
2466 if (!ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002467 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 req = NULL;
2469 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002470 if (!list_empty(&ep->queue) && !ep->stopped)
2471 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct net2280_request, queue);
2473 else
2474 req = NULL;
2475 if (req && !ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002476 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478 }
2479
2480 /* is there a buffer for the next packet?
2481 * for best streaming performance, make sure there is one.
2482 */
2483 if (req && !ep->stopped) {
2484
2485 /* load IN fifo with next packet (may be zlp) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002486 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002487 write_fifo(ep, &req->req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489}
2490
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002491static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
2493 struct net2280_ep *ep;
2494
2495 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002496 return &dev->ep[0];
2497 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 u8 bEndpointAddress;
2499
2500 if (!ep->desc)
2501 continue;
2502 bEndpointAddress = ep->desc->bEndpointAddress;
2503 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2504 continue;
2505 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2506 return ep;
2507 }
2508 return NULL;
2509}
2510
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002511static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2512{
2513 u32 scratch, fsmvalue;
2514 u32 ack_wait_timeout, state;
2515
2516 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2517 scratch = get_idx_reg(dev->regs, SCRATCH);
2518 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2519 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2520
2521 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2522 (r.bRequestType & USB_DIR_IN)))
2523 return;
2524
2525 /* This is the first Control Read for this connection: */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002526 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002527 /*
2528 * Connection is NOT SS:
2529 * - Connection must be FS or HS.
2530 * - This FSM state should allow workaround software to
2531 * run after the next USB connection.
2532 */
2533 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002534 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002535 goto restore_data_eps;
2536 }
2537
2538 /* Connection is SS: */
2539 for (ack_wait_timeout = 0;
2540 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2541 ack_wait_timeout++) {
2542
2543 state = readl(&dev->plregs->pl_ep_status_1)
2544 & (0xff << STATE);
2545 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2546 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2547 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002548 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002549 break;
2550 }
2551
2552 /*
2553 * We have not yet received host's Data Phase ACK
2554 * - Wait and try again.
2555 */
2556 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2557
2558 continue;
2559 }
2560
2561
2562 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002563 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002564 "to detect SS host's data phase ACK.");
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002565 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002566 "got 0x%2.2x.\n", state >> STATE);
2567 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002568 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002569 "%duSec for Control Read Data Phase ACK\n",
2570 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2571 }
2572
2573restore_data_eps:
2574 /*
2575 * Restore data EPs to their pre-workaround settings (disabled,
2576 * initialized, and other details).
2577 */
2578 defect7374_disable_data_eps(dev);
2579
2580 set_idx_reg(dev->regs, SCRATCH, scratch);
2581
2582 return;
2583}
2584
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002585static void ep_clear_seqnum(struct net2280_ep *ep)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002586{
2587 struct net2280 *dev = ep->dev;
2588 u32 val;
2589 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2590
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002591 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2592 val |= ep_pl[ep->num];
2593 writel(val, &dev->plregs->pl_ep_ctrl);
2594 val |= BIT(SEQUENCE_NUMBER_RESET);
2595 writel(val, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002596
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002597 return;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002598}
2599
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002600static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2601 struct net2280_ep *ep, struct usb_ctrlrequest r)
2602{
2603 int tmp = 0;
2604
2605#define w_value le16_to_cpu(r.wValue)
2606#define w_index le16_to_cpu(r.wIndex)
2607#define w_length le16_to_cpu(r.wLength)
2608
2609 switch (r.bRequest) {
2610 struct net2280_ep *e;
2611 u16 status;
2612
2613 case USB_REQ_SET_CONFIGURATION:
2614 dev->addressed_state = !w_value;
2615 goto usb3_delegate;
2616
2617 case USB_REQ_GET_STATUS:
2618 switch (r.bRequestType) {
2619 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2620 status = dev->wakeup_enable ? 0x02 : 0x00;
Peter Chenc8678d92015-01-28 16:32:35 +08002621 if (dev->gadget.is_selfpowered)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002622 status |= BIT(0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002623 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2624 dev->ltm_enable << 4);
2625 writel(0, &dev->epregs[0].ep_irqenb);
2626 set_fifo_bytecount(ep, sizeof(status));
2627 writel((__force u32) status, &dev->epregs[0].ep_data);
2628 allow_status_338x(ep);
2629 break;
2630
2631 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2632 e = get_ep_by_addr(dev, w_index);
2633 if (!e)
2634 goto do_stall3;
2635 status = readl(&e->regs->ep_rsp) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002636 BIT(CLEAR_ENDPOINT_HALT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002637 writel(0, &dev->epregs[0].ep_irqenb);
2638 set_fifo_bytecount(ep, sizeof(status));
2639 writel((__force u32) status, &dev->epregs[0].ep_data);
2640 allow_status_338x(ep);
2641 break;
2642
2643 default:
2644 goto usb3_delegate;
2645 }
2646 break;
2647
2648 case USB_REQ_CLEAR_FEATURE:
2649 switch (r.bRequestType) {
2650 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2651 if (!dev->addressed_state) {
2652 switch (w_value) {
2653 case USB_DEVICE_U1_ENABLE:
2654 dev->u1_enable = 0;
2655 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002656 ~BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002657 &dev->usb_ext->usbctl2);
2658 allow_status_338x(ep);
2659 goto next_endpoints3;
2660
2661 case USB_DEVICE_U2_ENABLE:
2662 dev->u2_enable = 0;
2663 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002664 ~BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002665 &dev->usb_ext->usbctl2);
2666 allow_status_338x(ep);
2667 goto next_endpoints3;
2668
2669 case USB_DEVICE_LTM_ENABLE:
2670 dev->ltm_enable = 0;
2671 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002672 ~BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002673 &dev->usb_ext->usbctl2);
2674 allow_status_338x(ep);
2675 goto next_endpoints3;
2676
2677 default:
2678 break;
2679 }
2680 }
2681 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2682 dev->wakeup_enable = 0;
2683 writel(readl(&dev->usb->usbctl) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002684 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002685 &dev->usb->usbctl);
2686 allow_status_338x(ep);
2687 break;
2688 }
2689 goto usb3_delegate;
2690
2691 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2692 e = get_ep_by_addr(dev, w_index);
2693 if (!e)
2694 goto do_stall3;
2695 if (w_value != USB_ENDPOINT_HALT)
2696 goto do_stall3;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002697 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002698 /*
2699 * Workaround for SS SeqNum not cleared via
2700 * Endpoint Halt (Clear) bit. select endpoint
2701 */
2702 ep_clear_seqnum(e);
2703 clear_halt(e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002704 if (!list_empty(&e->queue) && e->td_dma)
2705 restart_dma(e);
2706 allow_status(ep);
2707 ep->stopped = 1;
2708 break;
2709
2710 default:
2711 goto usb3_delegate;
2712 }
2713 break;
2714 case USB_REQ_SET_FEATURE:
2715 switch (r.bRequestType) {
2716 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2717 if (!dev->addressed_state) {
2718 switch (w_value) {
2719 case USB_DEVICE_U1_ENABLE:
2720 dev->u1_enable = 1;
2721 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002722 BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002723 &dev->usb_ext->usbctl2);
2724 allow_status_338x(ep);
2725 goto next_endpoints3;
2726
2727 case USB_DEVICE_U2_ENABLE:
2728 dev->u2_enable = 1;
2729 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002730 BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002731 &dev->usb_ext->usbctl2);
2732 allow_status_338x(ep);
2733 goto next_endpoints3;
2734
2735 case USB_DEVICE_LTM_ENABLE:
2736 dev->ltm_enable = 1;
2737 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002738 BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002739 &dev->usb_ext->usbctl2);
2740 allow_status_338x(ep);
2741 goto next_endpoints3;
2742 default:
2743 break;
2744 }
2745 }
2746
2747 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2748 dev->wakeup_enable = 1;
2749 writel(readl(&dev->usb->usbctl) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002750 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002751 &dev->usb->usbctl);
2752 allow_status_338x(ep);
2753 break;
2754 }
2755 goto usb3_delegate;
2756
2757 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2758 e = get_ep_by_addr(dev, w_index);
2759 if (!e || (w_value != USB_ENDPOINT_HALT))
2760 goto do_stall3;
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002761 ep->stopped = 1;
2762 if (ep->num == 0)
2763 ep->dev->protocol_stall = 1;
2764 else {
2765 if (ep->dma)
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01002766 abort_dma(ep);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002767 set_halt(ep);
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002768 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002769 allow_status_338x(ep);
2770 break;
2771
2772 default:
2773 goto usb3_delegate;
2774 }
2775
2776 break;
2777 default:
2778
2779usb3_delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002780 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002781 r.bRequestType, r.bRequest,
2782 w_value, w_index, w_length,
2783 readl(&ep->cfg->ep_cfg));
2784
2785 ep->responded = 0;
2786 spin_unlock(&dev->lock);
2787 tmp = dev->driver->setup(&dev->gadget, &r);
2788 spin_lock(&dev->lock);
2789 }
2790do_stall3:
2791 if (tmp < 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002792 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002793 r.bRequestType, r.bRequest, tmp);
2794 dev->protocol_stall = 1;
2795 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002796 set_halt(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002797 }
2798
2799next_endpoints3:
2800
2801#undef w_value
2802#undef w_index
2803#undef w_length
2804
2805 return;
2806}
2807
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002808static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
2810 struct net2280_ep *ep;
2811 u32 num, scratch;
2812
2813 /* most of these don't need individual acks */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002814 stat &= ~BIT(INTA_ASSERTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (!stat)
2816 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002817 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 /* starting a control request? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002820 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 union {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002822 u32 raw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 struct usb_ctrlrequest r;
2824 } u;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002825 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 struct net2280_request *req;
2827
2828 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002829 u32 val = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002830 if (val & BIT(SUPER_SPEED)) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002831 dev->gadget.speed = USB_SPEED_SUPER;
2832 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2833 EP0_SS_MAX_PACKET_SIZE);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002834 } else if (val & BIT(HIGH_SPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 dev->gadget.speed = USB_SPEED_HIGH;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002836 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2837 EP0_HS_MAX_PACKET_SIZE);
2838 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 dev->gadget.speed = USB_SPEED_FULL;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002840 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2841 EP0_HS_MAX_PACKET_SIZE);
2842 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002843 net2280_led_speed(dev, dev->gadget.speed);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002844 ep_dbg(dev, "%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002845 usb_speed_string(dev->gadget.speed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002848 ep = &dev->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 ep->irqs++;
2850
2851 /* make sure any leftover request state is cleared */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002852 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002853 while (!list_empty(&ep->queue)) {
2854 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct net2280_request, queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002856 done(ep, req, (req->req.actual == req->req.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 ? 0 : -EPROTO);
2858 }
2859 ep->stopped = 0;
2860 dev->protocol_stall = 0;
Ricardo Ribalda Delgado5d1b6842014-11-28 14:50:53 +01002861 if (!(dev->quirks & PLX_SUPERSPEED)) {
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002862 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002863 tmp = BIT(FIFO_OVERFLOW) |
2864 BIT(FIFO_UNDERFLOW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002865 else
2866 tmp = 0;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002867
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002868 writel(tmp | BIT(TIMEOUT) |
2869 BIT(USB_STALL_SENT) |
2870 BIT(USB_IN_NAK_SENT) |
2871 BIT(USB_IN_ACK_RCVD) |
2872 BIT(USB_OUT_PING_NAK_SENT) |
2873 BIT(USB_OUT_ACK_SENT) |
2874 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
2875 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
2876 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2877 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2878 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002879 BIT(DATA_IN_TOKEN_INTERRUPT),
2880 &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002881 }
2882 u.raw[0] = readl(&dev->usb->setup0123);
2883 u.raw[1] = readl(&dev->usb->setup4567);
David Brownell901b3d72006-09-02 03:13:45 -07002884
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002885 cpu_to_le32s(&u.raw[0]);
2886 cpu_to_le32s(&u.raw[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002888 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002889 defect7374_workaround(dev, u.r);
2890
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002891 tmp = 0;
2892
David Brownell01ee7d72007-05-25 20:40:14 -07002893#define w_value le16_to_cpu(u.r.wValue)
2894#define w_index le16_to_cpu(u.r.wIndex)
2895#define w_length le16_to_cpu(u.r.wLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 /* ack the irq */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002898 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
2899 stat ^= BIT(SETUP_PACKET_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 /* watch control traffic at the token level, and force
2902 * synchronization before letting the status stage happen.
2903 * FIXME ignore tokens we'll NAK, until driver responds.
2904 * that'll mean a lot less irqs for some drivers.
2905 */
2906 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2907 if (ep->is_in) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002908 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2909 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2910 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002911 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 } else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002913 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2914 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2915 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002916 writel(scratch, &dev->epregs[0].ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 /* we made the hardware handle most lowlevel requests;
2919 * everything else goes uplevel to the gadget code.
2920 */
Alan Stern1f26e282006-11-16 10:16:00 -05002921 ep->responded = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002922
2923 if (dev->gadget.speed == USB_SPEED_SUPER) {
2924 handle_stat0_irqs_superspeed(dev, ep, u.r);
2925 goto next_endpoints;
2926 }
2927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 switch (u.r.bRequest) {
2929 case USB_REQ_GET_STATUS: {
2930 struct net2280_ep *e;
David Brownell320f3452005-05-07 13:05:18 -07002931 __le32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 /* hw handles device and interface status */
2934 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2935 goto delegate;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002936 e = get_ep_by_addr(dev, w_index);
2937 if (!e || w_length > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 goto do_stall;
2939
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002940 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002941 status = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002943 status = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 /* don't bother with a request object! */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002946 writel(0, &dev->epregs[0].ep_irqenb);
2947 set_fifo_bytecount(ep, w_length);
2948 writel((__force u32)status, &dev->epregs[0].ep_data);
2949 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002950 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 goto next_endpoints;
2952 }
2953 break;
2954 case USB_REQ_CLEAR_FEATURE: {
2955 struct net2280_ep *e;
2956
2957 /* hw handles device features */
2958 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2959 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002960 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002962 e = get_ep_by_addr(dev, w_index);
2963 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04002965 if (e->wedged) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002966 ep_vdbg(dev, "%s wedged, halt not cleared\n",
Alan Stern80661342008-08-14 15:49:11 -04002967 ep->ep.name);
2968 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002969 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Alan Stern80661342008-08-14 15:49:11 -04002970 clear_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002971 if ((ep->dev->quirks & PLX_SUPERSPEED) &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002972 !list_empty(&e->queue) && e->td_dma)
2973 restart_dma(e);
Alan Stern80661342008-08-14 15:49:11 -04002974 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002975 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 goto next_endpoints;
2977 }
2978 break;
2979 case USB_REQ_SET_FEATURE: {
2980 struct net2280_ep *e;
2981
2982 /* hw handles device features */
2983 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2984 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002985 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002987 e = get_ep_by_addr(dev, w_index);
2988 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04002990 if (e->ep.name == ep0name)
2991 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002992 set_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002993 if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002994 abort_dma(e);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002995 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002996 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 goto next_endpoints;
2998 }
2999 break;
3000 default:
3001delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003002 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 "ep_cfg %08x\n",
3004 u.r.bRequestType, u.r.bRequest,
David Brownell320f3452005-05-07 13:05:18 -07003005 w_value, w_index, w_length,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003006 readl(&ep->cfg->ep_cfg));
Alan Stern1f26e282006-11-16 10:16:00 -05003007 ep->responded = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003008 spin_unlock(&dev->lock);
3009 tmp = dev->driver->setup(&dev->gadget, &u.r);
3010 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012
3013 /* stall ep0 on error */
3014 if (tmp < 0) {
3015do_stall:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003016 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 u.r.bRequestType, u.r.bRequest, tmp);
3018 dev->protocol_stall = 1;
3019 }
3020
3021 /* some in/out token irq should follow; maybe stall then.
3022 * driver must queue a request (even zlp) or halt ep0
3023 * before the host times out.
3024 */
3025 }
3026
David Brownell320f3452005-05-07 13:05:18 -07003027#undef w_value
3028#undef w_index
3029#undef w_length
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031next_endpoints:
3032 /* endpoint data irq ? */
3033 scratch = stat & 0x7f;
3034 stat &= ~0x7f;
3035 for (num = 0; scratch; num++) {
3036 u32 t;
3037
3038 /* do this endpoint's FIFO and queue need tending? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003039 t = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if ((scratch & t) == 0)
3041 continue;
3042 scratch ^= t;
3043
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003044 ep = &dev->ep[num];
3045 handle_ep_small(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047
3048 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003049 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003052#define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3053 BIT(DMA_C_INTERRUPT) | \
3054 BIT(DMA_B_INTERRUPT) | \
3055 BIT(DMA_A_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056#define PCI_ERROR_INTERRUPTS ( \
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003057 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3058 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3059 BIT(PCI_RETRY_ABORT_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003061static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 struct net2280_ep *ep;
3064 u32 tmp, num, mask, scratch;
3065
3066 /* after disconnect there's nothing else to do! */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003067 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3068 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03003071 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
David Brownell901b3d72006-09-02 03:13:45 -07003072 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 * only indicates a change in the reset state).
3074 */
3075 if (stat & tmp) {
Alan Sternb611e422014-11-06 14:27:56 +08003076 bool reset = false;
3077 bool disconnect = false;
3078
3079 /*
3080 * Ignore disconnects and resets if the speed hasn't been set.
3081 * VBUS can bounce and there's always an initial reset.
3082 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003083 writel(tmp, &dev->regs->irqstat1);
Alan Sternb611e422014-11-06 14:27:56 +08003084 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3085 if ((stat & BIT(VBUS_INTERRUPT)) &&
3086 (readl(&dev->usb->usbctl) &
3087 BIT(VBUS_PIN)) == 0) {
3088 disconnect = true;
3089 ep_dbg(dev, "disconnect %s\n",
3090 dev->driver->driver.name);
3091 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3092 (readl(&dev->usb->usbstat) & mask)
3093 == 0) {
3094 reset = true;
3095 ep_dbg(dev, "reset %s\n",
3096 dev->driver->driver.name);
3097 }
3098
3099 if (disconnect || reset) {
3100 stop_activity(dev, dev->driver);
3101 ep0_start(dev);
3102 spin_unlock(&dev->lock);
3103 if (reset)
3104 usb_gadget_udc_reset
3105 (&dev->gadget, dev->driver);
3106 else
3107 (dev->driver->disconnect)
3108 (&dev->gadget);
3109 spin_lock(&dev->lock);
3110 return;
3111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113 stat &= ~tmp;
3114
3115 /* vBUS can bounce ... one of many reasons to ignore the
3116 * notion of hotplug events on bus connect/disconnect!
3117 */
3118 if (!stat)
3119 return;
3120 }
3121
3122 /* NOTE: chip stays in PCI D0 state for now, but it could
3123 * enter D1 to save more power
3124 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003125 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (stat & tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003127 writel(tmp, &dev->regs->irqstat1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003128 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (dev->driver->suspend)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003130 dev->driver->suspend(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (!enable_suspend)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003132 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 } else {
3134 if (dev->driver->resume)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003135 dev->driver->resume(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /* at high speed, note erratum 0133 */
3137 }
3138 stat &= ~tmp;
3139 }
3140
3141 /* clear any other status/irqs */
3142 if (stat)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003143 writel(stat, &dev->regs->irqstat1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 /* some status we can just ignore */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003146 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003147 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3148 BIT(SUSPEND_REQUEST_INTERRUPT) |
3149 BIT(RESUME_INTERRUPT) |
3150 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003151 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003152 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3153 BIT(RESUME_INTERRUPT) |
3154 BIT(SOF_DOWN_INTERRUPT) |
3155 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if (!stat)
3158 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003159 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /* DMA status, for ep-{a,b,c,d} */
3162 scratch = stat & DMA_INTERRUPTS;
3163 stat &= ~DMA_INTERRUPTS;
3164 scratch >>= 9;
3165 for (num = 0; scratch; num++) {
3166 struct net2280_dma_regs __iomem *dma;
3167
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003168 tmp = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if ((tmp & scratch) == 0)
3170 continue;
3171 scratch ^= tmp;
3172
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003173 ep = &dev->ep[num + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 dma = ep->dma;
3175
3176 if (!dma)
3177 continue;
3178
3179 /* clear ep's dma status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003180 tmp = readl(&dma->dmastat);
3181 writel(tmp, &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003183 /* dma sync*/
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003184 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003185 u32 r_dmacount = readl(&dma->dmacount);
3186 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003187 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003188 continue;
3189 }
3190
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003191 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3192 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3193 ep->ep.name, tmp);
3194 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003196 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 /* OUT transfers terminate when the data from the
3199 * host is in our memory. Process whatever's done.
3200 * On this path, we know transfer's last packet wasn't
3201 * less than req->length. NAK_OUT_PACKETS may be set,
3202 * or the FIFO may already be holding new packets.
3203 *
3204 * IN transfers can linger in the FIFO for a very
3205 * long time ... we ignore that for now, accounting
3206 * precisely (like PIO does) needs per-packet irqs
3207 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003208 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 /* disable dma on inactive queues; else maybe restart */
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003211 if (!list_empty(&ep->queue)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003212 tmp = readl(&dma->dmactl);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003213 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
3215 ep->irqs++;
3216 }
3217
3218 /* NOTE: there are other PCI errors we might usefully notice.
3219 * if they appear very often, here's where to try recovering.
3220 */
3221 if (stat & PCI_ERROR_INTERRUPTS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003222 ep_err(dev, "pci dma error; stat %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 stat &= ~PCI_ERROR_INTERRUPTS;
3224 /* these are fatal errors, but "maybe" they won't
3225 * happen again ...
3226 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003227 stop_activity(dev, dev->driver);
3228 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 stat = 0;
3230 }
3231
3232 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003233 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003236static irqreturn_t net2280_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
3238 struct net2280 *dev = _dev;
3239
Alan Stern658ad5e2006-04-14 16:44:11 -04003240 /* shared interrupt, not ours */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003241 if ((dev->quirks & PLX_LEGACY) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003242 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
Alan Stern658ad5e2006-04-14 16:44:11 -04003243 return IRQ_NONE;
3244
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003245 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247 /* handle disconnect, dma, and more */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003248 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
3250 /* control requests and PIO */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003251 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003253 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003254 /* re-enable interrupt to trigger any possible new interrupt */
3255 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3256 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3257 writel(pciirqenb1, &dev->regs->pciirqenb1);
3258 }
3259
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003260 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262 return IRQ_HANDLED;
3263}
3264
3265/*-------------------------------------------------------------------------*/
3266
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003267static void gadget_release(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003269 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003271 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
3274/* tear down the binding between this driver and the pci device */
3275
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003276static void net2280_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003278 struct net2280 *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003280 usb_del_gadget_udc(&dev->gadget);
3281
David Brownell6bea4762006-12-05 03:15:33 -08003282 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 /* then clean up the resources we allocated during probe() */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003285 net2280_led_shutdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 if (dev->requests) {
3287 int i;
3288 for (i = 1; i < 5; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003289 if (!dev->ep[i].dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003291 pci_pool_free(dev->requests, dev->ep[i].dummy,
3292 dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003294 pci_pool_destroy(dev->requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
3296 if (dev->got_irq)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003297 free_irq(pdev->irq, dev);
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003298 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003299 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (dev->regs)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003301 iounmap(dev->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (dev->region)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003303 release_mem_region(pci_resource_start(pdev, 0),
3304 pci_resource_len(pdev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (dev->enabled)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003306 pci_disable_device(pdev);
3307 device_remove_file(&pdev->dev, &dev_attr_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003309 ep_info(dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
3312/* wrap this driver around the specified device, but
3313 * don't respond over USB until a gadget driver binds to us.
3314 */
3315
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003316static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
3318 struct net2280 *dev;
3319 unsigned long resource, len;
3320 void __iomem *base = NULL;
3321 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 /* alloc, and start init */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003324 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3325 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 retval = -ENOMEM;
3327 goto done;
3328 }
3329
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003330 pci_set_drvdata(pdev, dev);
3331 spin_lock_init(&dev->lock);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003332 dev->quirks = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 dev->pdev = pdev;
3334 dev->gadget.ops = &net2280_ops;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003335 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003336 USB_SPEED_SUPER : USB_SPEED_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 /* the "gadget" abstracts/virtualizes the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 dev->gadget.name = driver_name;
3340
3341 /* now all the pci goodies ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003342 if (pci_enable_device(pdev) < 0) {
3343 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 goto done;
3345 }
3346 dev->enabled = 1;
3347
3348 /* BAR 0 holds all the registers
3349 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3350 * BAR 2 is fifo memory; unused here
3351 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003352 resource = pci_resource_start(pdev, 0);
3353 len = pci_resource_len(pdev, 0);
3354 if (!request_mem_region(resource, len, driver_name)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003355 ep_dbg(dev, "controller already in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 retval = -EBUSY;
3357 goto done;
3358 }
3359 dev->region = 1;
3360
David Brownell901b3d72006-09-02 03:13:45 -07003361 /* FIXME provide firmware download interface to put
3362 * 8051 code into the chip, e.g. to turn on PCI PM.
3363 */
3364
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003365 base = ioremap_nocache(resource, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (base == NULL) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003367 ep_dbg(dev, "can't map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 retval = -EFAULT;
3369 goto done;
3370 }
3371 dev->regs = (struct net2280_regs __iomem *) base;
3372 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3373 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3374 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3375 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3376 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3377
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003378 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003379 u32 fsmvalue;
3380 u32 usbstat;
3381 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3382 (base + 0x00b4);
3383 dev->fiforegs = (struct usb338x_fifo_regs __iomem *)
3384 (base + 0x0500);
3385 dev->llregs = (struct usb338x_ll_regs __iomem *)
3386 (base + 0x0700);
3387 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3388 (base + 0x0748);
3389 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3390 (base + 0x077c);
3391 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3392 (base + 0x079c);
3393 dev->plregs = (struct usb338x_pl_regs __iomem *)
3394 (base + 0x0800);
3395 usbstat = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003396 dev->enhanced_mode = !!(usbstat & BIT(11));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003397 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3398 /* put into initial config, link up all endpoints */
3399 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3400 (0xf << DEFECT7374_FSM_FIELD);
3401 /* See if firmware needs to set up for workaround: */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003402 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3403 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003404 writel(0, &dev->usb->usbctl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003405 } else
3406 dev->bug7734_patched = 0;
3407 } else {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003408 dev->enhanced_mode = 0;
3409 dev->n_ep = 7;
3410 /* put into initial config, link up all endpoints */
3411 writel(0, &dev->usb->usbctl);
3412 }
3413
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003414 usb_reset(dev);
3415 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417 /* irq setup after old hardware is cleaned up */
3418 if (!pdev->irq) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003419 ep_err(dev, "No IRQ. Check PCI setup!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 retval = -ENODEV;
3421 goto done;
3422 }
David S. Millerc6387a42006-06-20 01:21:29 -07003423
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003424 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003425 if (pci_enable_msi(pdev))
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003426 ep_err(dev, "Failed to enable MSI mode\n");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003427
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003428 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3429 driver_name, dev)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003430 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 retval = -EBUSY;
3432 goto done;
3433 }
3434 dev->got_irq = 1;
3435
3436 /* DMA setup */
3437 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003438 dev->requests = pci_pool_create("requests", pdev,
3439 sizeof(struct net2280_dma),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 0 /* no alignment requirements */,
3441 0 /* or page-crossing issues */);
3442 if (!dev->requests) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003443 ep_dbg(dev, "can't get request pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 retval = -ENOMEM;
3445 goto done;
3446 }
3447 for (i = 1; i < 5; i++) {
3448 struct net2280_dma *td;
3449
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003450 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3451 &dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (!td) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003453 ep_dbg(dev, "can't get dummy %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 retval = -ENOMEM;
3455 goto done;
3456 }
3457 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 td->dmadesc = td->dmaaddr;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003459 dev->ep[i].dummy = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
3461
3462 /* enable lower-overhead pci memory bursts during DMA */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003463 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003464 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3465 /*
3466 * 256 write retries may not be enough...
3467 BIT(PCI_RETRY_ABORT_ENABLE) |
3468 */
3469 BIT(DMA_READ_MULTIPLE_ENABLE) |
3470 BIT(DMA_READ_LINE_ENABLE),
3471 &dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003473 pci_set_master(pdev);
3474 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 /* ... also flushes any posted pci writes */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003477 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
3479 /* done */
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003480 ep_info(dev, "%s\n", driver_desc);
3481 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
David S. Millerc6387a42006-06-20 01:21:29 -07003482 pdev->irq, base, dev->chiprev);
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01003483 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003484 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003485 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3486 if (retval)
3487 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Felipe Balbi2901df62013-02-26 15:15:27 +02003489 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3490 gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003491 if (retval)
3492 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return 0;
3494
3495done:
3496 if (dev)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003497 net2280_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return retval;
3499}
3500
Alan Stern2d61bde2006-05-05 16:23:42 -04003501/* make sure the board is quiescent; otherwise it will continue
3502 * generating IRQs across the upcoming reboot.
3503 */
3504
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003505static void net2280_shutdown(struct pci_dev *pdev)
Alan Stern2d61bde2006-05-05 16:23:42 -04003506{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003507 struct net2280 *dev = pci_get_drvdata(pdev);
Alan Stern2d61bde2006-05-05 16:23:42 -04003508
3509 /* disable IRQs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003510 writel(0, &dev->regs->pciirqenb0);
3511 writel(0, &dev->regs->pciirqenb1);
Alan Stern2d61bde2006-05-05 16:23:42 -04003512
3513 /* disable the pullup so the host will think we're gone */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003514 writel(0, &dev->usb->usbctl);
Alan Stern2f076072013-01-30 16:40:14 -05003515
Alan Stern2d61bde2006-05-05 16:23:42 -04003516}
3517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
3519/*-------------------------------------------------------------------------*/
3520
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003521static const struct pci_device_id pci_ids[] = { {
David Brownell901b3d72006-09-02 03:13:45 -07003522 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3523 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003524 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 .device = 0x2280,
3526 .subvendor = PCI_ANY_ID,
3527 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003528 .driver_data = PLX_LEGACY | PLX_2280,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003529 }, {
David Brownell901b3d72006-09-02 03:13:45 -07003530 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3531 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003532 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003533 .device = 0x2282,
3534 .subvendor = PCI_ANY_ID,
3535 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003536 .driver_data = PLX_LEGACY,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003537 },
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003538 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003539 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3540 .class_mask = ~0,
3541 .vendor = PCI_VENDOR_ID_PLX,
3542 .device = 0x3380,
3543 .subvendor = PCI_ANY_ID,
3544 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003545 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003546 },
3547 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003548 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3549 .class_mask = ~0,
3550 .vendor = PCI_VENDOR_ID_PLX,
3551 .device = 0x3382,
3552 .subvendor = PCI_ANY_ID,
3553 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003554 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003555 },
3556{ /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557};
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003558MODULE_DEVICE_TABLE(pci, pci_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560/* pci driver glue; this is a "new style" PCI driver module */
3561static struct pci_driver net2280_pci_driver = {
3562 .name = (char *) driver_name,
3563 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
3565 .probe = net2280_probe,
3566 .remove = net2280_remove,
Alan Stern2d61bde2006-05-05 16:23:42 -04003567 .shutdown = net2280_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
3569 /* FIXME add power management support */
3570};
3571
Ricardo Ribalda Delgado9a028e42014-05-20 18:30:07 +02003572module_pci_driver(net2280_pci_driver);
3573
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003574MODULE_DESCRIPTION(DRIVER_DESC);
3575MODULE_AUTHOR("David Brownell");
3576MODULE_LICENSE("GPL");