blob: 878a98ed84f58432816b55661132f493ab0be6cc [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
Mian Yousaf Kaukabe6ac4bb2015-05-16 22:33:31 +0200126static void ep_clear_seqnum(struct net2280_ep *ep);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*-------------------------------------------------------------------------*/
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200129static inline void enable_pciirqenb(struct net2280_ep *ep)
130{
131 u32 tmp = readl(&ep->dev->regs->pciirqenb0);
132
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200133 if (ep->dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200134 tmp |= BIT(ep->num);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200135 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200136 tmp |= BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200137 writel(tmp, &ep->dev->regs->pciirqenb0);
138
139 return;
140}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200143net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct net2280 *dev;
146 struct net2280_ep *ep;
147 u32 max, tmp;
148 unsigned long flags;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200149 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100150 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200152 ep = container_of(_ep, struct net2280_ep, ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200153 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100154 desc->bDescriptorType != USB_DT_ENDPOINT) {
155 pr_err("%s: failed at line=%d\n", __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 dev = ep->dev;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100159 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
160 ret = -ESHUTDOWN;
161 goto print_err;
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /* erratum 0119 workaround ties up an endpoint number */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100165 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) {
166 ret = -EDOM;
167 goto print_err;
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200170 if (dev->quirks & PLX_SUPERSPEED) {
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100171 if ((desc->bEndpointAddress & 0x0f) >= 0x0c) {
172 ret = -EDOM;
173 goto print_err;
174 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200175 ep->is_in = !!usb_endpoint_dir_in(desc);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100176 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) {
177 ret = -EINVAL;
178 goto print_err;
179 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200180 }
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* sanity check ep-e/ep-f since their fifos are small */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200183 max = usb_endpoint_maxp(desc) & 0x1fff;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100184 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) {
185 ret = -ERANGE;
186 goto print_err;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200189 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 _ep->maxpacket = max & 0x7ff;
191 ep->desc = desc;
192
193 /* ep_reset() has already been called */
194 ep->stopped = 0;
Alan Stern80661342008-08-14 15:49:11 -0400195 ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ep->out_overflow = 0;
197
198 /* set speed-dependent max packet; may kick in high bandwidth */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200199 set_max_speed(ep, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* set type, direction, address; reset fifo counters */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200202 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
204 if (tmp == USB_ENDPOINT_XFER_INT) {
205 /* erratum 0105 workaround prevents hs NYET */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200206 if (dev->chiprev == 0100 &&
207 dev->gadget.speed == USB_SPEED_HIGH &&
208 !(desc->bEndpointAddress & USB_DIR_IN))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200209 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 &ep->regs->ep_rsp);
211 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
212 /* catch some particularly blatant driver bugs */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200213 if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
214 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
215 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
216 spin_unlock_irqrestore(&dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100217 ret = -ERANGE;
218 goto print_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200221 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200222 /* Enable this endpoint */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200223 if (dev->quirks & PLX_LEGACY) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200224 tmp <<= ENDPOINT_TYPE;
225 tmp |= desc->bEndpointAddress;
226 /* default full fifo lines */
227 tmp |= (4 << ENDPOINT_BYTE_COUNT);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200228 tmp |= BIT(ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200229 ep->is_in = (tmp & USB_DIR_IN) != 0;
230 } else {
231 /* In Legacy mode, only OUT endpoints are used */
232 if (dev->enhanced_mode && ep->is_in) {
233 tmp <<= IN_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200234 tmp |= BIT(IN_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200235 } else {
236 tmp <<= OUT_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200237 tmp |= BIT(OUT_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200238 tmp |= (ep->is_in << ENDPOINT_DIRECTION);
239 }
240
Mian Yousaf Kaukab463e1042015-05-16 22:33:34 +0200241 tmp |= (4 << ENDPOINT_BYTE_COUNT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200242 tmp |= usb_endpoint_num(desc);
243 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
244 }
245
246 /* Make sure all the registers are written before ep_rsp*/
247 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* for OUT transfers, block the rx fifo until a read is posted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200251 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200252 else if (!(dev->quirks & PLX_2280)) {
David Brownell901b3d72006-09-02 03:13:45 -0700253 /* Added for 2282, Don't use nak packets on an in endpoint,
254 * this was ignored on 2280
255 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200256 writel(BIT(CLEAR_NAK_OUT_PACKETS) |
257 BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Mian Yousaf Kaukabe6ac4bb2015-05-16 22:33:31 +0200260 if (dev->quirks & PLX_SUPERSPEED)
261 ep_clear_seqnum(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200262 writel(tmp, &ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* enable irqs */
265 if (!ep->dma) { /* pio, per-packet */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200266 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200268 tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
269 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200270 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200271 tmp |= readl(&ep->regs->ep_irqenb);
272 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 } else { /* dma, per-request */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200274 tmp = BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200275 tmp |= readl(&dev->regs->pciirqenb1);
276 writel(tmp, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /* for short OUT transfers, dma completions can't
279 * advance the queue; do it pio-style, by hand.
280 * NOTE erratum 0112 workaround #2
281 */
282 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200283 tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200284 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200286 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 }
289
290 tmp = desc->bEndpointAddress;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200291 ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200292 _ep->name, tmp & 0x0f, DIR_STRING(tmp),
293 type_string(desc->bmAttributes),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 ep->dma ? "dma" : "pio", max);
295
296 /* pci writes may still be posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200297 spin_unlock_irqrestore(&dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100298 return ret;
299
300print_err:
301 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200305static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 u32 result;
308
309 do {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200310 result = readl(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (result == ~(u32)0) /* "device unplugged" */
312 return -ENODEV;
313 result &= mask;
314 if (result == done)
315 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200316 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 usec--;
318 } while (usec > 0);
319 return -ETIMEDOUT;
320}
321
David Brownell901b3d72006-09-02 03:13:45 -0700322static const struct usb_ep_ops net2280_ep_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200324static void ep_reset_228x(struct net2280_regs __iomem *regs,
325 struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 u32 tmp;
328
329 ep->desc = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200330 INIT_LIST_HEAD(&ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Robert Baldygae117e742013-12-13 12:23:38 +0100332 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 ep->ep.ops = &net2280_ep_ops;
334
335 /* disable the dma, irqs, endpoint... */
336 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200337 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200338 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
339 BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
340 BIT(DMA_ABORT),
341 &ep->dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200343 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200344 tmp &= ~BIT(ep->num);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200345 writel(tmp, &regs->pciirqenb0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200347 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200348 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200349 writel(tmp, &regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200351 writel(0, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* init to our chosen defaults, notably so that we NAK OUT
354 * packets until the driver queues a read (+note erratum 0112)
355 */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200356 if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200357 tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
358 BIT(SET_NAK_OUT_PACKETS) |
359 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
360 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100361 } else {
362 /* added for 2282 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200363 tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
364 BIT(CLEAR_NAK_OUT_PACKETS) |
365 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
366 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (ep->num != 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200370 tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
371 BIT(CLEAR_ENDPOINT_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200373 writel(tmp, &ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* scrub most status bits, and flush any fifo state */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200376 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200377 tmp = BIT(FIFO_OVERFLOW) |
378 BIT(FIFO_UNDERFLOW);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100379 else
380 tmp = 0;
381
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200382 writel(tmp | BIT(TIMEOUT) |
383 BIT(USB_STALL_SENT) |
384 BIT(USB_IN_NAK_SENT) |
385 BIT(USB_IN_ACK_RCVD) |
386 BIT(USB_OUT_PING_NAK_SENT) |
387 BIT(USB_OUT_ACK_SENT) |
388 BIT(FIFO_FLUSH) |
389 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
390 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
391 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
392 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
393 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200394 BIT(DATA_IN_TOKEN_INTERRUPT),
395 &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* fifo size is handled separately */
398}
399
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200400static void ep_reset_338x(struct net2280_regs __iomem *regs,
401 struct net2280_ep *ep)
402{
403 u32 tmp, dmastat;
404
405 ep->desc = NULL;
406 INIT_LIST_HEAD(&ep->queue);
407
408 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
409 ep->ep.ops = &net2280_ep_ops;
410
411 /* disable the dma, irqs, endpoint... */
412 if (ep->dma) {
413 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200414 writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
415 BIT(DMA_PAUSE_DONE_INTERRUPT) |
416 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200417 BIT(DMA_TRANSACTION_DONE_INTERRUPT),
418 /* | BIT(DMA_ABORT), */
419 &ep->dma->dmastat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200420
421 dmastat = readl(&ep->dma->dmastat);
422 if (dmastat == 0x5002) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200423 ep_warn(ep->dev, "The dmastat return = %x!!\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200424 dmastat);
425 writel(0x5a, &ep->dma->dmastat);
426 }
427
428 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200429 tmp &= ~BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200430 writel(tmp, &regs->pciirqenb0);
431 } else {
432 if (ep->num < 5) {
433 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200434 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200435 writel(tmp, &regs->pciirqenb1);
436 }
437 }
438 writel(0, &ep->regs->ep_irqenb);
439
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200440 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
441 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
442 BIT(FIFO_OVERFLOW) |
443 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
444 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
445 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
446 BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200447}
448
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200449static void nuke(struct net2280_ep *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200451static int net2280_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct net2280_ep *ep;
454 unsigned long flags;
455
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200456 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100457 if (!_ep || !ep->desc || _ep->name == ep0name) {
458 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100460 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200461 spin_lock_irqsave(&ep->dev->lock, flags);
462 nuke(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200463
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200464 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200465 ep_reset_338x(ep->dev->regs, ep);
466 else
467 ep_reset_228x(ep->dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200469 ep_vdbg(ep->dev, "disabled %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ep->dma ? "dma" : "pio", _ep->name);
471
472 /* synch memory views with the device */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200473 (void)readl(&ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +0100475 if (!ep->dma && ep->num >= 1 && ep->num <= 4)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200476 ep->dma = &ep->dev->dma[ep->num - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200478 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481
482/*-------------------------------------------------------------------------*/
483
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200484static struct usb_request
485*net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct net2280_ep *ep;
488 struct net2280_request *req;
489
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100490 if (!_ep) {
491 pr_err("%s: Invalid ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return NULL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100493 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200494 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800496 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!req)
498 return NULL;
499
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200500 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* this dma descriptor may be swapped with the previous dummy */
503 if (ep->dma) {
504 struct net2280_dma *td;
505
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200506 td = pci_pool_alloc(ep->dev->requests, gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 &req->td_dma);
508 if (!td) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200509 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return NULL;
511 }
512 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 td->dmadesc = td->dmaaddr;
514 req->td = td;
515 }
516 return &req->req;
517}
518
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200519static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct net2280_ep *ep;
522 struct net2280_request *req;
523
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200524 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100525 if (!_ep || !_req) {
526 dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n",
527 __func__, _ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200531 req = container_of(_req, struct net2280_request, req);
532 WARN_ON(!list_empty(&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (req->td)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200534 pci_pool_free(ep->dev->requests, req->td, req->td_dma);
535 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/*-------------------------------------------------------------------------*/
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/* load a packet into the fifo we use for usb IN transfers.
541 * works for all endpoints.
542 *
543 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
544 * at a time, but this code is simpler because it knows it only writes
545 * one packet. ep-a..ep-d should use dma instead.
546 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200547static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct net2280_ep_regs __iomem *regs = ep->regs;
550 u8 *buf;
551 u32 tmp;
552 unsigned count, total;
553
554 /* INVARIANT: fifo is currently empty. (testable) */
555
556 if (req) {
557 buf = req->buf + req->actual;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200558 prefetch(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 total = req->length - req->actual;
560 } else {
561 total = 0;
562 buf = NULL;
563 }
564
565 /* write just one packet at a time */
566 count = ep->ep.maxpacket;
567 if (count > total) /* min() cannot be used on a bitfield */
568 count = total;
569
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200570 ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ep->ep.name, count,
572 (count != ep->ep.maxpacket) ? " (short)" : "",
573 req);
574 while (count >= 4) {
575 /* NOTE be careful if you try to align these. fifo lines
576 * should normally be full (4 bytes) and successive partial
577 * lines are ok only in certain cases.
578 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200579 tmp = get_unaligned((u32 *)buf);
580 cpu_to_le32s(&tmp);
581 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 buf += 4;
583 count -= 4;
584 }
585
586 /* last fifo entry is "short" unless we wrote a full packet.
587 * also explicitly validate last word in (periodic) transfers
588 * when maxpacket is not a multiple of 4 bytes.
589 */
590 if (count || total < ep->ep.maxpacket) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200591 tmp = count ? get_unaligned((u32 *)buf) : count;
592 cpu_to_le32s(&tmp);
593 set_fifo_bytecount(ep, count & 0x03);
594 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 /* pci writes may still be posted */
598}
599
600/* work around erratum 0106: PCI and USB race over the OUT fifo.
601 * caller guarantees chiprev 0100, out endpoint is NAKing, and
602 * there's no real data in the fifo.
603 *
604 * NOTE: also used in cases where that erratum doesn't apply:
605 * where the host wrote "too much" data to us.
606 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200607static void out_flush(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 u32 __iomem *statp;
610 u32 tmp;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 statp = &ep->regs->ep_stat;
Ricardo Ribalda Delgadod82f3db2014-11-28 14:51:01 +0100613
614 tmp = readl(statp);
615 if (tmp & BIT(NAK_OUT_PACKETS)) {
616 ep_dbg(ep->dev, "%s %s %08x !NAK\n",
617 ep->ep.name, __func__, tmp);
618 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
619 }
620
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200621 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200622 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
623 statp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200624 writel(BIT(FIFO_FLUSH), statp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200625 /* Make sure that stap is written */
626 mb();
627 tmp = readl(statp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200628 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* high speed did bulk NYET; fifo isn't filling */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200630 ep->dev->gadget.speed == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 unsigned usec;
632
633 usec = 50; /* 64 byte bulk/interrupt */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200634 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
635 BIT(USB_OUT_PING_NAK_SENT), usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
637 }
638}
639
640/* unload packet(s) from the fifo we use for usb OUT transfers.
641 * returns true iff the request completed, because of short packet
642 * or the request buffer having filled with full packets.
643 *
644 * for ep-a..ep-d this will read multiple packets out when they
645 * have been accepted.
646 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200647static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct net2280_ep_regs __iomem *regs = ep->regs;
650 u8 *buf = req->req.buf + req->req.actual;
651 unsigned count, tmp, is_short;
652 unsigned cleanup = 0, prevent = 0;
653
654 /* erratum 0106 ... packets coming in during fifo reads might
655 * be incompletely rejected. not all cases have workarounds.
656 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200657 if (ep->dev->chiprev == 0x0100 &&
658 ep->dev->gadget.speed == USB_SPEED_FULL) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200659 udelay(1);
660 tmp = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200661 if ((tmp & BIT(NAK_OUT_PACKETS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 cleanup = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200663 else if ((tmp & BIT(FIFO_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200664 start_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 prevent = 1;
666 }
667 /* else: hope we don't see the problem */
668 }
669
670 /* never overflow the rx buffer. the fifo reads packets until
671 * it sees a short one; we might not be ready for them all.
672 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200673 prefetchw(buf);
674 count = readl(&regs->ep_avail);
675 if (unlikely(count == 0)) {
676 udelay(1);
677 tmp = readl(&ep->regs->ep_stat);
678 count = readl(&regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* handled that data already? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200680 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682 }
683
684 tmp = req->req.length - req->req.actual;
685 if (count > tmp) {
686 /* as with DMA, data overflow gets flushed */
687 if ((tmp % ep->ep.maxpacket) != 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200688 ep_err(ep->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 "%s out fifo %d bytes, expected %d\n",
690 ep->ep.name, count, tmp);
691 req->req.status = -EOVERFLOW;
692 cleanup = 1;
693 /* NAK_OUT_PACKETS will be set, so flushing is safe;
694 * the next read will start with the next packet
695 */
696 } /* else it's a ZLP, no worries */
697 count = tmp;
698 }
699 req->req.actual += count;
700
701 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
702
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200703 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 -0700704 ep->ep.name, count, is_short ? " (short)" : "",
705 cleanup ? " flush" : "", prevent ? " nak" : "",
706 req, req->req.actual, req->req.length);
707
708 while (count >= 4) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200709 tmp = readl(&regs->ep_data);
710 cpu_to_le32s(&tmp);
711 put_unaligned(tmp, (u32 *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 buf += 4;
713 count -= 4;
714 }
715 if (count) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200716 tmp = readl(&regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* LE conversion is implicit here: */
718 do {
719 *buf++ = (u8) tmp;
720 tmp >>= 8;
721 } while (--count);
722 }
723 if (cleanup)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200724 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (prevent) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200726 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200727 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200730 return is_short || ((req->req.actual == req->req.length) &&
731 !req->req.zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/* fill out dma descriptor to match a given request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200735static void fill_dma_desc(struct net2280_ep *ep,
736 struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct net2280_dma *td = req->td;
739 u32 dmacount = req->req.length;
740
741 /* don't let DMA continue after a short OUT packet,
742 * so overruns can't affect the next transfer.
743 * in case of overruns on max-size packets, we can't
744 * stop the fifo from filling but we can flush it.
745 */
746 if (ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200747 dmacount |= BIT(DMA_DIRECTION);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200748 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200749 !(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200750 dmacount |= BIT(END_OF_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 req->valid = valid;
753 if (valid)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200754 dmacount |= BIT(VALID_BIT);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100755 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /* td->dmadesc = previously set by caller */
758 td->dmaaddr = cpu_to_le32 (req->req.dma);
759
760 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200761 wmb();
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700762 td->dmacount = cpu_to_le32(dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765static const u32 dmactl_default =
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200766 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
767 BIT(DMA_CLEAR_COUNT_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* erratum 0116 workaround part 1 (use POLLING) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200769 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
770 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
771 BIT(DMA_VALID_BIT_ENABLE) |
772 BIT(DMA_SCATTER_GATHER_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* erratum 0116 workaround part 2 (no AUTOSTART) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200774 BIT(DMA_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200776static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200778 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200781static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200783 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200784 spin_stop_dma(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200787static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct net2280_dma_regs __iomem *dma = ep->dma;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200790 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200792 if (!(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200793 tmp |= BIT(END_OF_CHAIN);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100794
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200795 writel(tmp, &dma->dmacount);
796 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200798 writel(td_dma, &dma->dmadesc);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200799 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200800 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200801 writel(dmactl, &dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200804 (void) readl(&ep->dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200806 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (!ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200809 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200812static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 u32 tmp;
815 struct net2280_dma_regs __iomem *dma = ep->dma;
816
817 /* FIXME can't use DMA for ZLPs */
818
819 /* on this path we "know" there's no dma active (yet) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200820 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200821 writel(0, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* previous OUT packet might have been short */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200824 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
825 BIT(NAK_OUT_PACKETS))) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200826 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 &ep->regs->ep_stat);
828
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200829 tmp = readl(&ep->regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200831 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* transfer all/some fifo data */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200834 writel(req->req.dma, &dma->dmaaddr);
835 tmp = min(tmp, req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /* dma irq, faking scatterlist status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200838 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200839 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
840 &dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 req->td->dmadesc = 0;
842 req->valid = 1;
843
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200844 writel(BIT(DMA_ENABLE), &dma->dmactl);
845 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return;
847 }
848 }
849
850 tmp = dmactl_default;
851
852 /* force packet boundaries between dma requests, but prevent the
853 * controller from automagically writing a last "short" packet
854 * (zero length) unless the driver explicitly said to do that.
855 */
856 if (ep->is_in) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200857 if (likely((req->req.length % ep->ep.maxpacket) ||
858 req->req.zero)){
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200859 tmp |= BIT(DMA_FIFO_VALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 ep->in_fifo_validate = 1;
861 } else
862 ep->in_fifo_validate = 0;
863 }
864
865 /* init req->td, pointing to the current dummy */
866 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200867 fill_dma_desc(ep, req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100869 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200871 start_queue(ep, tmp, req->td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200875queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 struct net2280_dma *end;
878 dma_addr_t tmp;
879
880 /* swap new dummy for old, link; fill and maybe activate */
881 end = ep->dummy;
882 ep->dummy = req->td;
883 req->td = end;
884
885 tmp = ep->td_dma;
886 ep->td_dma = req->td_dma;
887 req->td_dma = tmp;
888
889 end->dmadesc = cpu_to_le32 (ep->td_dma);
890
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200891 fill_dma_desc(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894static void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200895done(struct net2280_ep *ep, struct net2280_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct net2280 *dev;
898 unsigned stopped = ep->stopped;
899
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200900 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (req->req.status == -EINPROGRESS)
903 req->req.status = status;
904 else
905 status = req->req.status;
906
907 dev = ep->dev;
Felipe Balbiae4d7932011-12-19 12:09:56 +0200908 if (ep->dma)
909 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (status && status != -ESHUTDOWN)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200912 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 ep->ep.name, &req->req, status,
914 req->req.actual, req->req.length);
915
916 /* don't modify queue heads during completion callback */
917 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200918 spin_unlock(&dev->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200919 usb_gadget_giveback_request(&ep->ep, &req->req);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200920 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ep->stopped = stopped;
922}
923
924/*-------------------------------------------------------------------------*/
925
926static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200927net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 struct net2280_request *req;
930 struct net2280_ep *ep;
931 struct net2280 *dev;
932 unsigned long flags;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100933 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* we always require a cpu-view buffer, so that we can
936 * always use pio (as fallback or whatever).
937 */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100938 ep = container_of(_ep, struct net2280_ep, ep);
939 if (!_ep || (!ep->desc && ep->num != 0)) {
940 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
941 return -EINVAL;
942 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200943 req = container_of(_req, struct net2280_request, req);
944 if (!_req || !_req->complete || !_req->buf ||
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100945 !list_empty(&req->queue)) {
946 ret = -EINVAL;
947 goto print_err;
948 }
949 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
950 ret = -EDOM;
951 goto print_err;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 dev = ep->dev;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100954 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
955 ret = -ESHUTDOWN;
956 goto print_err;
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* FIXME implement PIO fallback for ZLPs with DMA */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100960 if (ep->dma && _req->length == 0) {
961 ret = -EOPNOTSUPP;
962 goto print_err;
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /* set up dma mapping in case the caller didn't */
Felipe Balbiae4d7932011-12-19 12:09:56 +0200966 if (ep->dma) {
Felipe Balbiae4d7932011-12-19 12:09:56 +0200967 ret = usb_gadget_map_request(&dev->gadget, _req,
968 ep->is_in);
969 if (ret)
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100970 goto print_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200973 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200976 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 _req->status = -EINPROGRESS;
979 _req->actual = 0;
980
981 /* kickstart this i/o queue? */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100982 if (list_empty(&ep->queue) && !ep->stopped &&
983 !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
984 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* use DMA if the endpoint supports it, else pio */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100987 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200988 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 else {
990 /* maybe there's no control data, just status ack */
991 if (ep->num == 0 && _req->length == 0) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200992 allow_status(ep);
993 done(ep, req, 0);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200994 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto done;
996 }
997
998 /* PIO ... stuff the fifo, or unblock it. */
999 if (ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001000 write_fifo(ep, _req);
1001 else if (list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 u32 s;
1003
1004 /* OUT FIFO might have packet(s) buffered */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001005 s = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001006 if ((s & BIT(FIFO_EMPTY)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /* note: _req->short_not_ok is
1008 * ignored here since PIO _always_
1009 * stops queue advance here, and
1010 * _req->status doesn't change for
1011 * short reads (only _req->actual)
1012 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001013 if (read_fifo(ep, req) &&
1014 ep->num == 0) {
1015 done(ep, req, 0);
1016 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /* don't queue it */
1018 req = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001019 } else if (read_fifo(ep, req) &&
1020 ep->num != 0) {
1021 done(ep, req, 0);
1022 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001024 s = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
1027 /* don't NAK, let the fifo fill */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001028 if (req && (s & BIT(NAK_OUT_PACKETS)))
1029 writel(BIT(CLEAR_NAK_OUT_PACKETS),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 &ep->regs->ep_rsp);
1031 }
1032 }
1033
1034 } else if (ep->dma) {
1035 int valid = 1;
1036
1037 if (ep->is_in) {
1038 int expect;
1039
1040 /* preventing magic zlps is per-engine state, not
1041 * per-transfer; irq logic must recover hiccups.
1042 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001043 expect = likely(req->req.zero ||
1044 (req->req.length % ep->ep.maxpacket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (expect != ep->in_fifo_validate)
1046 valid = 0;
1047 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001048 queue_dma(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 } /* else the irq handler advances the queue. */
1051
Alan Stern1f26e282006-11-16 10:16:00 -05001052 ep->responded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001054 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001056 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 /* pci writes may still be posted */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001059 return ret;
1060
1061print_err:
1062 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1063 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001067dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1068 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001071 done(ep, req, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001074static void scan_dma_completions(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 /* only look at descriptors that were "naturally" retired,
1077 * so fifo and list head state won't matter
1078 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001079 while (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct net2280_request *req;
1081 u32 tmp;
1082
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001083 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct net2280_request, queue);
1085 if (!req->valid)
1086 break;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001087 rmb();
1088 tmp = le32_to_cpup(&req->td->dmacount);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001089 if ((tmp & BIT(VALID_BIT)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 break;
1091
1092 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1093 * cases where DMA must be aborted; this code handles
1094 * all non-abort DMA completions.
1095 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001096 if (unlikely(req->td->dmadesc == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /* paranoia */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001098 tmp = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (tmp & DMA_BYTE_COUNT_MASK)
1100 break;
1101 /* single transfer mode */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001102 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02001104 } else if (!ep->is_in &&
Ricardo Ribalda Delgado43780aa2014-11-28 14:51:00 +01001105 (req->req.length % ep->ep.maxpacket) &&
1106 !(ep->dev->quirks & PLX_SUPERSPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Mario Schuknecht18a4e652014-11-16 21:21:45 +01001108 tmp = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* AVOID TROUBLE HERE by not issuing short reads from
1110 * your gadget driver. That helps avoids errata 0121,
1111 * 0122, and 0124; not all cases trigger the warning.
1112 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001113 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001114 ep_warn(ep->dev, "%s lost packet sync!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 ep->ep.name);
1116 req->req.status = -EOVERFLOW;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001117 } else {
1118 tmp = readl(&ep->regs->ep_avail);
1119 if (tmp) {
1120 /* fifo gets flushed later */
1121 ep->out_overflow = 1;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001122 ep_dbg(ep->dev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001123 "%s dma, discard %d len %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ep->ep.name, tmp,
1125 req->req.length);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001126 req->req.status = -EOVERFLOW;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001130 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132}
1133
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001134static void restart_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct net2280_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (ep->stopped)
1139 return;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001140 req = list_entry(ep->queue.next, struct net2280_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01001142 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01001145static void abort_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 /* abort the current transfer */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001148 if (likely(!list_empty(&ep->queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /* FIXME work around errata 0121, 0122, 0124 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001150 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001151 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001153 stop_dma(ep->dma);
1154 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157/* dequeue ALL requests */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001158static void nuke(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 struct net2280_request *req;
1161
1162 /* called with spinlock held */
1163 ep->stopped = 1;
1164 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001165 abort_dma(ep);
1166 while (!list_empty(&ep->queue)) {
1167 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct net2280_request,
1169 queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001170 done(ep, req, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172}
1173
1174/* dequeue JUST ONE request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001175static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct net2280_ep *ep;
1178 struct net2280_request *req;
1179 unsigned long flags;
1180 u32 dmactl;
1181 int stopped;
1182
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001183 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001184 if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1185 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1186 __func__, _ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001190 spin_lock_irqsave(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 stopped = ep->stopped;
1192
1193 /* quiesce dma while we patch the queue */
1194 dmactl = 0;
1195 ep->stopped = 1;
1196 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001197 dmactl = readl(&ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* WARNING erratum 0127 may kick in ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001199 stop_dma(ep->dma);
1200 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
1203 /* make sure it's still queued on this endpoint */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001204 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (&req->req == _req)
1206 break;
1207 }
1208 if (&req->req != _req) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001209 spin_unlock_irqrestore(&ep->dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001210 dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
1211 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return -EINVAL;
1213 }
1214
1215 /* queue head may be partially complete. */
1216 if (ep->queue.next == &req->queue) {
1217 if (ep->dma) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001218 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 _req->status = -ECONNRESET;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001220 abort_dma(ep);
1221 if (likely(ep->queue.next == &req->queue)) {
1222 /* NOTE: misreports single-transfer mode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 req->td->dmacount = 0; /* invalidate */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001224 dma_done(ep, req,
1225 readl(&ep->dma->dmacount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 -ECONNRESET);
1227 }
1228 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001229 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001230 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001236 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 ep->stopped = stopped;
1238
1239 if (ep->dma) {
1240 /* turn off dma on inactive queues */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001241 if (list_empty(&ep->queue))
1242 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 else if (!ep->stopped) {
1244 /* resume current request, or start new one */
1245 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001246 writel(dmactl, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001248 start_dma(ep, list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct net2280_request, queue));
1250 }
1251 }
1252
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001253 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255}
1256
1257/*-------------------------------------------------------------------------*/
1258
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001259static int net2280_fifo_status(struct usb_ep *_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261static int
Alan Stern80661342008-08-14 15:49:11 -04001262net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct net2280_ep *ep;
1265 unsigned long flags;
1266 int retval = 0;
1267
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001268 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001269 if (!_ep || (!ep->desc && ep->num != 0)) {
1270 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001272 }
1273 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1274 retval = -ESHUTDOWN;
1275 goto print_err;
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001278 == USB_ENDPOINT_XFER_ISOC) {
1279 retval = -EINVAL;
1280 goto print_err;
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001283 spin_lock_irqsave(&ep->dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001284 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 retval = -EAGAIN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001286 goto print_unlock;
1287 } else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 retval = -EAGAIN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001289 goto print_unlock;
1290 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001291 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
Alan Stern80661342008-08-14 15:49:11 -04001292 value ? "set" : "clear",
1293 wedged ? "wedge" : "halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* set/clear, then synch memory views with the device */
1295 if (value) {
1296 if (ep->num == 0)
1297 ep->dev->protocol_stall = 1;
1298 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001299 set_halt(ep);
Alan Stern80661342008-08-14 15:49:11 -04001300 if (wedged)
1301 ep->wedged = 1;
1302 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001303 clear_halt(ep);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001304 if (ep->dev->quirks & PLX_SUPERSPEED &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001305 !list_empty(&ep->queue) && ep->td_dma)
1306 restart_dma(ep);
Alan Stern80661342008-08-14 15:49:11 -04001307 ep->wedged = 0;
1308 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001309 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001311 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 return retval;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001314
1315print_unlock:
1316 spin_unlock_irqrestore(&ep->dev->lock, flags);
1317print_err:
1318 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1319 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001322static int net2280_set_halt(struct usb_ep *_ep, int value)
Alan Stern80661342008-08-14 15:49:11 -04001323{
1324 return net2280_set_halt_and_wedge(_ep, value, 0);
1325}
1326
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001327static int net2280_set_wedge(struct usb_ep *_ep)
Alan Stern80661342008-08-14 15:49:11 -04001328{
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001329 if (!_ep || _ep->name == ep0name) {
1330 pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
Alan Stern80661342008-08-14 15:49:11 -04001331 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001332 }
Alan Stern80661342008-08-14 15:49:11 -04001333 return net2280_set_halt_and_wedge(_ep, 1, 1);
1334}
1335
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001336static int net2280_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 struct net2280_ep *ep;
1339 u32 avail;
1340
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001341 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001342 if (!_ep || (!ep->desc && ep->num != 0)) {
1343 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return -ENODEV;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001345 }
1346 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1347 dev_err(&ep->dev->pdev->dev,
1348 "%s: Invalid driver=%p or speed=%d\n",
1349 __func__, ep->dev->driver, ep->dev->gadget.speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -ESHUTDOWN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001353 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001354 if (avail > ep->fifo_size) {
1355 dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EOVERFLOW;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (ep->is_in)
1359 avail = ep->fifo_size - avail;
1360 return avail;
1361}
1362
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001363static void net2280_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct net2280_ep *ep;
1366
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001367 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001368 if (!_ep || (!ep->desc && ep->num != 0)) {
1369 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001371 }
1372 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1373 dev_err(&ep->dev->pdev->dev,
1374 "%s: Invalid driver=%p or speed=%d\n",
1375 __func__, ep->dev->driver, ep->dev->gadget.speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001379 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001380 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
David Brownell901b3d72006-09-02 03:13:45 -07001383static const struct usb_ep_ops net2280_ep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 .enable = net2280_enable,
1385 .disable = net2280_disable,
1386
1387 .alloc_request = net2280_alloc_request,
1388 .free_request = net2280_free_request,
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 .queue = net2280_queue,
1391 .dequeue = net2280_dequeue,
1392
1393 .set_halt = net2280_set_halt,
Alan Stern80661342008-08-14 15:49:11 -04001394 .set_wedge = net2280_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 .fifo_status = net2280_fifo_status,
1396 .fifo_flush = net2280_fifo_flush,
1397};
1398
1399/*-------------------------------------------------------------------------*/
1400
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001401static int net2280_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct net2280 *dev;
1404 unsigned long flags;
1405 u16 retval;
1406
1407 if (!_gadget)
1408 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001409 dev = container_of(_gadget, struct net2280, gadget);
1410 spin_lock_irqsave(&dev->lock, flags);
1411 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1412 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return retval;
1414}
1415
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001416static int net2280_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct net2280 *dev;
1419 u32 tmp;
1420 unsigned long flags;
1421
1422 if (!_gadget)
1423 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001424 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001426 spin_lock_irqsave(&dev->lock, flags);
1427 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001428 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1429 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001430 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 /* pci writes may still be posted */
1433 return 0;
1434}
1435
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001436static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct net2280 *dev;
1439 u32 tmp;
1440 unsigned long flags;
1441
1442 if (!_gadget)
1443 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001444 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001446 spin_lock_irqsave(&dev->lock, flags);
1447 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001448 if (value) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001449 tmp |= BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001450 _gadget->is_selfpowered = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001451 } else {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001452 tmp &= ~BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001453 _gadget->is_selfpowered = 0;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001454 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001455 writel(tmp, &dev->usb->usbctl);
1456 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 return 0;
1459}
1460
1461static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1462{
1463 struct net2280 *dev;
1464 u32 tmp;
1465 unsigned long flags;
1466
1467 if (!_gadget)
1468 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001469 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001471 spin_lock_irqsave(&dev->lock, flags);
1472 tmp = readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 dev->softconnect = (is_on != 0);
1474 if (is_on)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001475 tmp |= BIT(USB_DETECT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001477 tmp &= ~BIT(USB_DETECT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001478 writel(tmp, &dev->usb->usbctl);
1479 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 return 0;
1482}
1483
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001484static int net2280_start(struct usb_gadget *_gadget,
1485 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001486static int net2280_stop(struct usb_gadget *_gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488static const struct usb_gadget_ops net2280_ops = {
1489 .get_frame = net2280_get_frame,
1490 .wakeup = net2280_wakeup,
1491 .set_selfpowered = net2280_set_selfpowered,
1492 .pullup = net2280_pullup,
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001493 .udc_start = net2280_start,
1494 .udc_stop = net2280_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495};
1496
1497/*-------------------------------------------------------------------------*/
1498
Ricardo Ribalda Delgadob99b4062014-07-04 11:27:03 +02001499#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501/* FIXME move these into procfs, and use seq_file.
1502 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1503 * and also doesn't help products using this with 2.4 kernels.
1504 */
1505
1506/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001507static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1508 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001510 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001512 if (!dev->driver || !dev->driver->function ||
1513 strlen(dev->driver->function) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001515 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001517static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001519static ssize_t registers_show(struct device *_dev,
1520 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct net2280 *dev;
1523 char *next;
1524 unsigned size, t;
1525 unsigned long flags;
1526 int i;
1527 u32 t1, t2;
Andrew Morton30e69592005-06-26 17:18:46 -07001528 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001530 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 next = buf;
1532 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001533 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (dev->driver)
1536 s = dev->driver->driver.name;
1537 else
1538 s = "(none)";
1539
1540 /* Main Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001541 t = scnprintf(next, size, "%s version " DRIVER_VERSION
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001542 ", chiprev %04x\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 "devinit %03x fifoctl %08x gadget '%s'\n"
1544 "pci irqenb0 %02x irqenb1 %08x "
1545 "irqstat0 %04x irqstat1 %08x\n",
1546 driver_name, dev->chiprev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001547 readl(&dev->regs->devinit),
1548 readl(&dev->regs->fifoctl),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 s,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001550 readl(&dev->regs->pciirqenb0),
1551 readl(&dev->regs->pciirqenb1),
1552 readl(&dev->regs->irqstat0),
1553 readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 size -= t;
1555 next += t;
1556
1557 /* USB Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001558 t1 = readl(&dev->usb->usbctl);
1559 t2 = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001560 if (t1 & BIT(VBUS_PIN)) {
1561 if (t2 & BIT(HIGH_SPEED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 s = "high speed";
1563 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1564 s = "powered";
1565 else
1566 s = "full speed";
1567 /* full speed bit (6) not working?? */
1568 } else
1569 s = "not attached";
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001570 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 "stdrsp %08x usbctl %08x usbstat %08x "
1572 "addr 0x%02x (%s)\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001573 readl(&dev->usb->stdrsp), t1, t2,
1574 readl(&dev->usb->ouraddr), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 size -= t;
1576 next += t;
1577
1578 /* PCI Master Control Registers */
1579
1580 /* DMA Control Registers */
1581
1582 /* Configurable EP Control Registers */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001583 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct net2280_ep *ep;
1585
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001586 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (i && !ep->desc)
1588 continue;
1589
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001590 t1 = readl(&ep->cfg->ep_cfg);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001591 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1592 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1594 "irqenb %02x\n",
1595 ep->ep.name, t1, t2,
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001596 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 ? "NAK " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001598 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ? "hide " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001600 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 ? "CRC " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001602 (t2 & BIT(CLEAR_INTERRUPT_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 ? "interrupt " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001604 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ? "status " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001606 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ? "NAKmode " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001608 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 ? "DATA1 " : "DATA0 ",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001610 (t2 & BIT(CLEAR_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 ? "HALT " : "",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001612 readl(&ep->regs->ep_irqenb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 size -= t;
1614 next += t;
1615
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001616 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 "\tstat %08x avail %04x "
1618 "(ep%d%s-%s)%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001619 readl(&ep->regs->ep_stat),
1620 readl(&ep->regs->ep_avail),
1621 t1 & 0x0f, DIR_STRING(t1),
1622 type_string(t1 >> 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 ep->stopped ? "*" : "");
1624 size -= t;
1625 next += t;
1626
1627 if (!ep->dma)
1628 continue;
1629
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001630 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 " dma\tctl %08x stat %08x count %08x\n"
1632 "\taddr %08x desc %08x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001633 readl(&ep->dma->dmactl),
1634 readl(&ep->dma->dmastat),
1635 readl(&ep->dma->dmacount),
1636 readl(&ep->dma->dmaaddr),
1637 readl(&ep->dma->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 size -= t;
1639 next += t;
1640
1641 }
1642
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001643 /* Indexed Registers (none yet) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /* Statistics */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001646 t = scnprintf(next, size, "\nirqs: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 size -= t;
1648 next += t;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001649 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 struct net2280_ep *ep;
1651
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001652 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (i && !ep->irqs)
1654 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001655 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 size -= t;
1657 next += t;
1658
1659 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001660 t = scnprintf(next, size, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 size -= t;
1662 next += t;
1663
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001664 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 return PAGE_SIZE - size;
1667}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001668static DEVICE_ATTR_RO(registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001670static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1671 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct net2280 *dev;
1674 char *next;
1675 unsigned size;
1676 unsigned long flags;
1677 int i;
1678
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001679 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 next = buf;
1681 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001682 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001684 for (i = 0; i < dev->n_ep; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001685 struct net2280_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 struct net2280_request *req;
1687 int t;
1688
1689 if (i != 0) {
1690 const struct usb_endpoint_descriptor *d;
1691
1692 d = ep->desc;
1693 if (!d)
1694 continue;
1695 t = d->bEndpointAddress;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001696 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1698 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1699 (t & USB_DIR_IN) ? "in" : "out",
Ricardo Ribalda Delgadoa27f37a2014-05-20 18:30:08 +02001700 type_string(d->bmAttributes),
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001701 usb_endpoint_maxp(d) & 0x1fff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 ep->dma ? "dma" : "pio", ep->fifo_size
1703 );
1704 } else /* ep0 should only have one transfer queued */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001705 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 ep->is_in ? "in" : "out");
1707 if (t <= 0 || t > size)
1708 goto done;
1709 size -= t;
1710 next += t;
1711
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001712 if (list_empty(&ep->queue)) {
1713 t = scnprintf(next, size, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (t <= 0 || t > size)
1715 goto done;
1716 size -= t;
1717 next += t;
1718 continue;
1719 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001720 list_for_each_entry(req, &ep->queue, queue) {
1721 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1722 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 "\treq %p len %d/%d "
1724 "buf %p (dmacount %08x)\n",
1725 &req->req, req->req.actual,
1726 req->req.length, req->req.buf,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001727 readl(&ep->dma->dmacount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001729 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 "\treq %p len %d/%d buf %p\n",
1731 &req->req, req->req.actual,
1732 req->req.length, req->req.buf);
1733 if (t <= 0 || t > size)
1734 goto done;
1735 size -= t;
1736 next += t;
1737
1738 if (ep->dma) {
1739 struct net2280_dma *td;
1740
1741 td = req->td;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001742 t = scnprintf(next, size, "\t td %08x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 " count %08x buf %08x desc %08x\n",
1744 (u32) req->td_dma,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001745 le32_to_cpu(td->dmacount),
1746 le32_to_cpu(td->dmaaddr),
1747 le32_to_cpu(td->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (t <= 0 || t > size)
1749 goto done;
1750 size -= t;
1751 next += t;
1752 }
1753 }
1754 }
1755
1756done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001757 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return PAGE_SIZE - size;
1759}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001760static DEVICE_ATTR_RO(queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762
1763#else
1764
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001765#define device_create_file(a, b) (0)
1766#define device_remove_file(a, b) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768#endif
1769
1770/*-------------------------------------------------------------------------*/
1771
1772/* another driver-specific mode might be a request type doing dma
1773 * to/from another device fifo instead of to/from memory.
1774 */
1775
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001776static void set_fifo_mode(struct net2280 *dev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 /* keeping high bits preserves BAR2 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001779 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001782 INIT_LIST_HEAD(&dev->gadget.ep_list);
1783 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1784 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 switch (mode) {
1786 case 0:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001787 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1788 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1789 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 break;
1791 case 1:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001792 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 break;
1794 case 2:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001795 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1796 dev->ep[1].fifo_size = 2048;
1797 dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 break;
1799 }
1800 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001801 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1802 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001805static void defect7374_disable_data_eps(struct net2280 *dev)
1806{
1807 /*
1808 * For Defect 7374, disable data EPs (and more):
1809 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1810 * returing ep regs back to normal.
1811 */
1812 struct net2280_ep *ep;
1813 int i;
1814 unsigned char ep_sel;
1815 u32 tmp_reg;
1816
1817 for (i = 1; i < 5; i++) {
1818 ep = &dev->ep[i];
1819 writel(0, &ep->cfg->ep_cfg);
1820 }
1821
1822 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1823 for (i = 0; i < 6; i++)
1824 writel(0, &dev->dep[i].dep_cfg);
1825
1826 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1827 /* Select an endpoint for subsequent operations: */
1828 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1829 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1830
1831 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1832 ep_sel == 18 || ep_sel == 20)
1833 continue;
1834
1835 /* Change settings on some selected endpoints */
1836 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001837 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001838 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1839 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001840 tmp_reg |= BIT(EP_INITIALIZED);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001841 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1842 }
1843}
1844
1845static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1846{
1847 u32 tmp = 0, tmp_reg;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001848 u32 scratch;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001849 int i;
1850 unsigned char ep_sel;
1851
1852 scratch = get_idx_reg(dev->regs, SCRATCH);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001853
1854 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1855 == DEFECT7374_FSM_SS_CONTROL_READ);
1856
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001857 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1858
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001859 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1860 ep_warn(dev, "It will operate on cold-reboot and SS connect");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001861
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001862 /*GPEPs:*/
1863 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1864 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1865 ((dev->enhanced_mode) ?
Mian Yousaf Kaukab25d40ee2015-05-16 22:33:30 +02001866 BIT(OUT_ENDPOINT_ENABLE) | BIT(IN_ENDPOINT_ENABLE) :
1867 BIT(ENDPOINT_ENABLE)));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001868
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001869 for (i = 1; i < 5; i++)
1870 writel(tmp, &dev->ep[i].cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001871
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001872 /* CSRIN, PCIIN, STATIN, RCIN*/
1873 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1874 writel(tmp, &dev->dep[1].dep_cfg);
1875 writel(tmp, &dev->dep[3].dep_cfg);
1876 writel(tmp, &dev->dep[4].dep_cfg);
1877 writel(tmp, &dev->dep[5].dep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001878
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001879 /*Implemented for development and debug.
1880 * Can be refined/tuned later.*/
1881 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1882 /* Select an endpoint for subsequent operations: */
1883 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1884 writel(((tmp_reg & ~0x1f) | ep_sel),
1885 &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001886
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001887 if (ep_sel == 1) {
1888 tmp =
1889 (readl(&dev->plregs->pl_ep_ctrl) |
1890 BIT(CLEAR_ACK_ERROR_CODE) | 0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001891 writel(tmp, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001892 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001893 }
1894
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001895 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1896 ep_sel == 18 || ep_sel == 20)
1897 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001898
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001899 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1900 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1901 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1902
1903 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1904 ~BIT(EP_INITIALIZED);
1905 writel(tmp, &dev->plregs->pl_ep_ctrl);
1906
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001907 }
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001908
1909 /* Set FSM to focus on the first Control Read:
1910 * - Tip: Connection speed is known upon the first
1911 * setup request.*/
1912 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1913 set_idx_reg(dev->regs, SCRATCH, scratch);
1914
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001915}
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917/* keeping it simple:
1918 * - one bus driver, initted first;
1919 * - one function driver, initted second
1920 *
1921 * most of the work to support multiple net2280 controllers would
1922 * be to associate this gadget driver (yes?) with all of them, or
1923 * perhaps to bind specific drivers to specific devices.
1924 */
1925
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001926static void usb_reset_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 u32 tmp;
1929
1930 dev->gadget.speed = USB_SPEED_UNKNOWN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001931 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001933 net2280_led_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* disable automatic responses, and irqs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001936 writel(0, &dev->usb->stdrsp);
1937 writel(0, &dev->regs->pciirqenb0);
1938 writel(0, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 /* clear old dma and irq state */
1941 for (tmp = 0; tmp < 4; tmp++) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001942 struct net2280_ep *ep = &dev->ep[tmp + 1];
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001943 if (ep->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001944 abort_dma(ep);
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001945 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001946
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001947 writel(~0, &dev->regs->irqstat0),
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001948 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 /* reset, and enable pci */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001951 tmp = readl(&dev->regs->devinit) |
1952 BIT(PCI_ENABLE) |
1953 BIT(FIFO_SOFT_RESET) |
1954 BIT(USB_SOFT_RESET) |
1955 BIT(M8051_RESET);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001956 writel(tmp, &dev->regs->devinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* standard fifo and endpoint allocations */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001959 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001962static void usb_reset_338x(struct net2280 *dev)
1963{
1964 u32 tmp;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001965
1966 dev->gadget.speed = USB_SPEED_UNKNOWN;
1967 (void)readl(&dev->usb->usbctl);
1968
1969 net2280_led_init(dev);
1970
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001971 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001972 /* disable automatic responses, and irqs */
1973 writel(0, &dev->usb->stdrsp);
1974 writel(0, &dev->regs->pciirqenb0);
1975 writel(0, &dev->regs->pciirqenb1);
1976 }
1977
1978 /* clear old dma and irq state */
1979 for (tmp = 0; tmp < 4; tmp++) {
1980 struct net2280_ep *ep = &dev->ep[tmp + 1];
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02001981 struct net2280_dma_regs __iomem *dma;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001982
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02001983 if (ep->dma) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001984 abort_dma(ep);
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02001985 } else {
1986 dma = &dev->dma[tmp];
1987 writel(BIT(DMA_ABORT), &dma->dmastat);
1988 writel(0, &dma->dmactl);
1989 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001990 }
1991
1992 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
1993
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001994 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001995 /* reset, and enable pci */
1996 tmp = readl(&dev->regs->devinit) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001997 BIT(PCI_ENABLE) |
1998 BIT(FIFO_SOFT_RESET) |
1999 BIT(USB_SOFT_RESET) |
2000 BIT(M8051_RESET);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002001
2002 writel(tmp, &dev->regs->devinit);
2003 }
2004
2005 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2006 INIT_LIST_HEAD(&dev->gadget.ep_list);
2007
2008 for (tmp = 1; tmp < dev->n_ep; tmp++)
2009 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2010
2011}
2012
2013static void usb_reset(struct net2280 *dev)
2014{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002015 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002016 return usb_reset_228x(dev);
2017 return usb_reset_338x(dev);
2018}
2019
2020static void usb_reinit_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /* basic endpoint init */
2025 for (tmp = 0; tmp < 7; tmp++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002026 struct net2280_ep *ep = &dev->ep[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002028 ep->ep.name = ep_name[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 ep->dev = dev;
2030 ep->num = tmp;
2031
2032 if (tmp > 0 && tmp <= 4) {
2033 ep->fifo_size = 1024;
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01002034 ep->dma = &dev->dma[tmp - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 } else
2036 ep->fifo_size = 64;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002037 ep->regs = &dev->epregs[tmp];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002038 ep->cfg = &dev->epregs[tmp];
2039 ep_reset_228x(dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002041 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
2042 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
2043 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002045 dev->gadget.ep0 = &dev->ep[0].ep;
2046 dev->ep[0].stopped = 0;
2047 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 /* we want to prevent lowlevel/insecure access from the USB host,
2050 * but erratum 0119 means this enable bit is ignored
2051 */
2052 for (tmp = 0; tmp < 5; tmp++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002053 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002056static void usb_reinit_338x(struct net2280 *dev)
2057{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002058 int i;
2059 u32 tmp, val;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002060 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2061 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2062 0x00, 0xC0, 0x00, 0xC0 };
2063
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002064 /* basic endpoint init */
2065 for (i = 0; i < dev->n_ep; i++) {
2066 struct net2280_ep *ep = &dev->ep[i];
2067
Mian Yousaf Kaukaba285f402015-02-02 10:55:23 +01002068 ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002069 ep->dev = dev;
2070 ep->num = i;
2071
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01002072 if (i > 0 && i <= 4)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002073 ep->dma = &dev->dma[i - 1];
2074
2075 if (dev->enhanced_mode) {
2076 ep->cfg = &dev->epregs[ne[i]];
2077 ep->regs = (struct net2280_ep_regs __iomem *)
Felipe Balbic43e97b2014-07-16 12:20:25 -05002078 (((void __iomem *)&dev->epregs[ne[i]]) +
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002079 ep_reg_addr[i]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002080 } else {
2081 ep->cfg = &dev->epregs[i];
2082 ep->regs = &dev->epregs[i];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002083 }
2084
2085 ep->fifo_size = (i != 0) ? 2048 : 512;
2086
2087 ep_reset_338x(dev->regs, ep);
2088 }
2089 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2090
2091 dev->gadget.ep0 = &dev->ep[0].ep;
2092 dev->ep[0].stopped = 0;
2093
2094 /* Link layer set up */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002095 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002096 tmp = readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002097 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002098 writel(tmp, &dev->usb_ext->usbctl2);
2099 }
2100
2101 /* Hardware Defect and Workaround */
2102 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2103 val &= ~(0xf << TIMER_LFPS_6US);
2104 val |= 0x5 << TIMER_LFPS_6US;
2105 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2106
2107 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2108 val &= ~(0xffff << TIMER_LFPS_80US);
2109 val |= 0x0100 << TIMER_LFPS_80US;
2110 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2111
2112 /*
2113 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2114 * Hot Reset Exit Handshake may Fail in Specific Case using
2115 * Default Register Settings. Workaround for Enumeration test.
2116 */
2117 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2118 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2119 val |= 0x10 << HOT_TX_NORESET_TS2;
2120 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2121
2122 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2123 val &= ~(0x1f << HOT_RX_RESET_TS2);
2124 val |= 0x3 << HOT_RX_RESET_TS2;
2125 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2126
2127 /*
2128 * Set Recovery Idle to Recover bit:
2129 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2130 * link robustness with various hosts and hubs.
2131 * - It is safe to set for all connection speeds; all chip revisions.
2132 * - R-M-W to leave other bits undisturbed.
2133 * - Reference PLX TT-7372
2134 */
2135 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002136 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002137 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2138
2139 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2140
2141 /* disable dedicated endpoints */
2142 writel(0x0D, &dev->dep[0].dep_cfg);
2143 writel(0x0D, &dev->dep[1].dep_cfg);
2144 writel(0x0E, &dev->dep[2].dep_cfg);
2145 writel(0x0E, &dev->dep[3].dep_cfg);
2146 writel(0x0F, &dev->dep[4].dep_cfg);
2147 writel(0x0C, &dev->dep[5].dep_cfg);
2148}
2149
2150static void usb_reinit(struct net2280 *dev)
2151{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002152 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002153 return usb_reinit_228x(dev);
2154 return usb_reinit_338x(dev);
2155}
2156
2157static void ep0_start_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002159 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2160 BIT(CLEAR_NAK_OUT_PACKETS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002161 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2162 &dev->epregs[0].ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 /*
2165 * hardware optionally handles a bunch of standard requests
2166 * that the API hides from drivers anyway. have it do so.
2167 * endpoint status/features are handled in software, to
2168 * help pass tests for some dubious behavior.
2169 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002170 writel(BIT(SET_TEST_MODE) |
2171 BIT(SET_ADDRESS) |
2172 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2173 BIT(GET_DEVICE_STATUS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002174 BIT(GET_INTERFACE_STATUS),
2175 &dev->usb->stdrsp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002176 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2177 BIT(SELF_POWERED_USB_DEVICE) |
2178 BIT(REMOTE_WAKEUP_SUPPORT) |
2179 (dev->softconnect << USB_DETECT_ENABLE) |
2180 BIT(SELF_POWERED_STATUS),
2181 &dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002184 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2185 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2186 &dev->regs->pciirqenb0);
2187 writel(BIT(PCI_INTERRUPT_ENABLE) |
2188 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2189 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2190 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2191 BIT(VBUS_INTERRUPT_ENABLE) |
2192 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2193 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2194 &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 /* don't leave any writes posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002197 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002200static void ep0_start_338x(struct net2280 *dev)
2201{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002202
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002203 if (dev->bug7734_patched)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002204 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2205 BIT(SET_EP_HIDE_STATUS_PHASE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002206 &dev->epregs[0].ep_rsp);
2207
2208 /*
2209 * hardware optionally handles a bunch of standard requests
2210 * that the API hides from drivers anyway. have it do so.
2211 * endpoint status/features are handled in software, to
2212 * help pass tests for some dubious behavior.
2213 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002214 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2215 BIT(SET_SEL) |
2216 BIT(SET_TEST_MODE) |
2217 BIT(SET_ADDRESS) |
2218 BIT(GET_INTERFACE_STATUS) |
2219 BIT(GET_DEVICE_STATUS),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002220 &dev->usb->stdrsp);
2221 dev->wakeup_enable = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002222 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002223 (dev->softconnect << USB_DETECT_ENABLE) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002224 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002225 &dev->usb->usbctl);
2226
2227 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002228 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002229 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2230 &dev->regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002231 writel(BIT(PCI_INTERRUPT_ENABLE) |
2232 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2233 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2234 BIT(VBUS_INTERRUPT_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002235 &dev->regs->pciirqenb1);
2236
2237 /* don't leave any writes posted */
2238 (void)readl(&dev->usb->usbctl);
2239}
2240
2241static void ep0_start(struct net2280 *dev)
2242{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002243 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002244 return ep0_start_228x(dev);
2245 return ep0_start_338x(dev);
2246}
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/* when a driver is successfully registered, it will receive
2249 * control requests including set_configuration(), which enables
2250 * non-control requests. then usb traffic follows until a
2251 * disconnect is reported. then a host may connect again, or
2252 * the driver might get unbound.
2253 */
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002254static int net2280_start(struct usb_gadget *_gadget,
2255 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002257 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 int retval;
2259 unsigned i;
2260
2261 /* insist on high speed support from the driver, since
2262 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2263 * "must not be used in normal operation"
2264 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002265 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2266 !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return -EINVAL;
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002268
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002269 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002271 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002272 dev->ep[i].irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 /* hook up the driver ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 driver->driver.bus = NULL;
2276 dev->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002278 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2279 if (retval)
2280 goto err_unbind;
2281 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2282 if (retval)
2283 goto err_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Ricardo Ribalda Delgado7a74c482014-11-28 14:50:47 +01002285 /* enable host detection and ep0; and we're ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 * for set_configuration as well as eventual disconnect.
2287 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002288 net2280_led_active(dev, 1);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002289
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002290 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002291 defect7374_enable_data_eps_zero(dev);
2292
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002293 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* pci writes may still be posted */
2296 return 0;
Jeff Garzikb3899da2006-10-11 21:50:24 -04002297
2298err_func:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002299 device_remove_file(&dev->pdev->dev, &dev_attr_function);
Jeff Garzikb3899da2006-10-11 21:50:24 -04002300err_unbind:
Jeff Garzikb3899da2006-10-11 21:50:24 -04002301 dev->driver = NULL;
2302 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002305static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 int i;
2308
2309 /* don't disconnect if it's not connected */
2310 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2311 driver = NULL;
2312
2313 /* stop hardware; prevent new request submissions;
2314 * and kill any outstanding requests.
2315 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002316 usb_reset(dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002317 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002318 nuke(&dev->ep[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Felipe Balbi699412d2013-03-18 10:14:47 +02002320 /* report disconnect; the driver is already quiesced */
2321 if (driver) {
2322 spin_unlock(&dev->lock);
2323 driver->disconnect(&dev->gadget);
2324 spin_lock(&dev->lock);
2325 }
2326
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002327 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Felipe Balbi22835b82014-10-17 12:05:12 -05002330static int net2280_stop(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002332 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 unsigned long flags;
2334
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002335 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002337 spin_lock_irqsave(&dev->lock, flags);
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002338 stop_activity(dev, NULL);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002339 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002341 net2280_led_active(dev, 0);
Alan Stern2f076072013-01-30 16:40:14 -05002342
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002343 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2344 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002346 dev->driver = NULL;
Ricardo Ribalda Delgado84237bf2014-05-15 14:28:45 +02002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
2349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351/*-------------------------------------------------------------------------*/
2352
2353/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2354 * also works for dma-capable endpoints, in pio mode or just
2355 * to manually advance the queue after short OUT transfers.
2356 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002357static void handle_ep_small(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 struct net2280_request *req;
2360 u32 t;
2361 /* 0 error, 1 mid-data, 2 done */
2362 int mode = 1;
2363
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002364 if (!list_empty(&ep->queue))
2365 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct net2280_request, queue);
2367 else
2368 req = NULL;
2369
2370 /* ack all, and handle what we care about */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002371 t = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 ep->irqs++;
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002373
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002374 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
Ricardo Ribalda Delgadofc12c682015-01-13 10:54:58 +01002375 ep->ep.name, t, req ? &req->req : NULL);
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002376
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002377 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002378 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002379 else
2380 /* Added for 2282 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002381 writel(t, &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /* for ep0, monitor token irqs to catch data stage length errors
2384 * and to synchronize on status.
2385 *
2386 * also, to defer reporting of protocol stalls ... here's where
2387 * data or status first appears, handling stalls here should never
2388 * cause trouble on the host side..
2389 *
2390 * control requests could be slightly faster without token synch for
2391 * status, but status can jam up that way.
2392 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002393 if (unlikely(ep->num == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (ep->is_in) {
2395 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002396 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (ep->dev->protocol_stall) {
2398 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002399 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401 if (!req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002402 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 mode = 2;
2404 /* reply to extra IN data tokens with a zlp */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002405 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (ep->dev->protocol_stall) {
2407 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002408 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 mode = 2;
Alan Stern1f26e282006-11-16 10:16:00 -05002410 } else if (ep->responded &&
2411 !req && !ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002412 write_fifo(ep, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
2414 } else {
2415 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002416 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (ep->dev->protocol_stall) {
2418 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002419 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421 mode = 2;
2422 /* an extra OUT token is an error */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002423 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2424 req &&
2425 req->req.actual == req->req.length) ||
2426 (ep->responded && !req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 ep->dev->protocol_stall = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002428 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 ep->stopped = 1;
2430 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002431 done(ep, req, -EOVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 req = NULL;
2433 }
2434 }
2435 }
2436
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002437 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return;
2439
2440 /* manual DMA queue advance after short OUT */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002441 if (likely(ep->dma)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002442 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 u32 count;
2444 int stopped = ep->stopped;
2445
2446 /* TRANSFERRED works around OUT_DONE erratum 0112.
2447 * we expect (N <= maxpacket) bytes; host wrote M.
2448 * iff (M < N) we won't ever see a DMA interrupt.
2449 */
2450 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002451 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 /* any preceding dma transfers must finish.
2454 * dma handles (M >= N), may empty the queue
2455 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002456 scan_dma_completions(ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002457 if (unlikely(list_empty(&ep->queue) ||
2458 ep->out_overflow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 req = NULL;
2460 break;
2461 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002462 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 struct net2280_request, queue);
2464
2465 /* here either (M < N), a "real" short rx;
2466 * or (M == N) and the queue didn't empty
2467 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002468 if (likely(t & BIT(FIFO_EMPTY))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002469 count = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 count &= DMA_BYTE_COUNT_MASK;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002471 if (readl(&ep->dma->dmadesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 != req->td_dma)
2473 req = NULL;
2474 break;
2475 }
2476 udelay(1);
2477 }
2478
2479 /* stop DMA, leave ep NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002480 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002481 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002483 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 req->td->dmacount = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002485 t = readl(&ep->regs->ep_avail);
2486 dma_done(ep, req, count,
David Brownell901b3d72006-09-02 03:13:45 -07002487 (ep->out_overflow || t)
2488 ? -EOVERFLOW : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490
2491 /* also flush to prevent erratum 0106 trouble */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002492 if (unlikely(ep->out_overflow ||
2493 (ep->dev->chiprev == 0x0100 &&
2494 ep->dev->gadget.speed
2495 == USB_SPEED_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002496 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 ep->out_overflow = 0;
2498 }
2499
2500 /* (re)start dma if needed, stop NAKing */
2501 ep->stopped = stopped;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002502 if (!list_empty(&ep->queue))
2503 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 } else
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002505 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 ep->ep.name, t);
2507 return;
2508
2509 /* data packet(s) received (in the fifo, OUT) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002510 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002511 if (read_fifo(ep, req) && ep->num != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 mode = 2;
2513
2514 /* data packet(s) transmitted (IN) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002515 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 unsigned len;
2517
2518 len = req->req.length - req->req.actual;
2519 if (len > ep->ep.maxpacket)
2520 len = ep->ep.maxpacket;
2521 req->req.actual += len;
2522
2523 /* if we wrote it all, we're usually done */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002524 /* send zlps until the status stage */
2525 if ((req->req.actual == req->req.length) &&
2526 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 mode = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 /* there was nothing to do ... */
2530 } else if (mode == 1)
2531 return;
2532
2533 /* done */
2534 if (mode == 2) {
2535 /* stream endpoints often resubmit/unlink in completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002536 done(ep, req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 /* maybe advance queue to next request */
2539 if (ep->num == 0) {
2540 /* NOTE: net2280 could let gadget driver start the
2541 * status stage later. since not all controllers let
2542 * them control that, the api doesn't (yet) allow it.
2543 */
2544 if (!ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002545 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 req = NULL;
2547 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002548 if (!list_empty(&ep->queue) && !ep->stopped)
2549 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 struct net2280_request, queue);
2551 else
2552 req = NULL;
2553 if (req && !ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002554 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 }
2556 }
2557
2558 /* is there a buffer for the next packet?
2559 * for best streaming performance, make sure there is one.
2560 */
2561 if (req && !ep->stopped) {
2562
2563 /* load IN fifo with next packet (may be zlp) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002564 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002565 write_fifo(ep, &req->req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567}
2568
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002569static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
2571 struct net2280_ep *ep;
2572
2573 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002574 return &dev->ep[0];
2575 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 u8 bEndpointAddress;
2577
2578 if (!ep->desc)
2579 continue;
2580 bEndpointAddress = ep->desc->bEndpointAddress;
2581 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2582 continue;
2583 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2584 return ep;
2585 }
2586 return NULL;
2587}
2588
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002589static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2590{
2591 u32 scratch, fsmvalue;
2592 u32 ack_wait_timeout, state;
2593
2594 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2595 scratch = get_idx_reg(dev->regs, SCRATCH);
2596 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2597 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2598
2599 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2600 (r.bRequestType & USB_DIR_IN)))
2601 return;
2602
2603 /* This is the first Control Read for this connection: */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002604 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002605 /*
2606 * Connection is NOT SS:
2607 * - Connection must be FS or HS.
2608 * - This FSM state should allow workaround software to
2609 * run after the next USB connection.
2610 */
2611 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002612 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002613 goto restore_data_eps;
2614 }
2615
2616 /* Connection is SS: */
2617 for (ack_wait_timeout = 0;
2618 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2619 ack_wait_timeout++) {
2620
2621 state = readl(&dev->plregs->pl_ep_status_1)
2622 & (0xff << STATE);
2623 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2624 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2625 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002626 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002627 break;
2628 }
2629
2630 /*
2631 * We have not yet received host's Data Phase ACK
2632 * - Wait and try again.
2633 */
2634 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2635
2636 continue;
2637 }
2638
2639
2640 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002641 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002642 "to detect SS host's data phase ACK.");
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002643 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002644 "got 0x%2.2x.\n", state >> STATE);
2645 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002646 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002647 "%duSec for Control Read Data Phase ACK\n",
2648 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2649 }
2650
2651restore_data_eps:
2652 /*
2653 * Restore data EPs to their pre-workaround settings (disabled,
2654 * initialized, and other details).
2655 */
2656 defect7374_disable_data_eps(dev);
2657
2658 set_idx_reg(dev->regs, SCRATCH, scratch);
2659
2660 return;
2661}
2662
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002663static void ep_clear_seqnum(struct net2280_ep *ep)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002664{
2665 struct net2280 *dev = ep->dev;
2666 u32 val;
2667 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2668
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002669 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2670 val |= ep_pl[ep->num];
2671 writel(val, &dev->plregs->pl_ep_ctrl);
2672 val |= BIT(SEQUENCE_NUMBER_RESET);
2673 writel(val, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002674
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002675 return;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002676}
2677
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002678static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2679 struct net2280_ep *ep, struct usb_ctrlrequest r)
2680{
2681 int tmp = 0;
2682
2683#define w_value le16_to_cpu(r.wValue)
2684#define w_index le16_to_cpu(r.wIndex)
2685#define w_length le16_to_cpu(r.wLength)
2686
2687 switch (r.bRequest) {
2688 struct net2280_ep *e;
2689 u16 status;
2690
2691 case USB_REQ_SET_CONFIGURATION:
2692 dev->addressed_state = !w_value;
2693 goto usb3_delegate;
2694
2695 case USB_REQ_GET_STATUS:
2696 switch (r.bRequestType) {
2697 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2698 status = dev->wakeup_enable ? 0x02 : 0x00;
Peter Chenc8678d92015-01-28 16:32:35 +08002699 if (dev->gadget.is_selfpowered)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002700 status |= BIT(0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002701 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2702 dev->ltm_enable << 4);
2703 writel(0, &dev->epregs[0].ep_irqenb);
2704 set_fifo_bytecount(ep, sizeof(status));
2705 writel((__force u32) status, &dev->epregs[0].ep_data);
2706 allow_status_338x(ep);
2707 break;
2708
2709 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2710 e = get_ep_by_addr(dev, w_index);
2711 if (!e)
2712 goto do_stall3;
2713 status = readl(&e->regs->ep_rsp) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002714 BIT(CLEAR_ENDPOINT_HALT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002715 writel(0, &dev->epregs[0].ep_irqenb);
2716 set_fifo_bytecount(ep, sizeof(status));
2717 writel((__force u32) status, &dev->epregs[0].ep_data);
2718 allow_status_338x(ep);
2719 break;
2720
2721 default:
2722 goto usb3_delegate;
2723 }
2724 break;
2725
2726 case USB_REQ_CLEAR_FEATURE:
2727 switch (r.bRequestType) {
2728 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2729 if (!dev->addressed_state) {
2730 switch (w_value) {
2731 case USB_DEVICE_U1_ENABLE:
2732 dev->u1_enable = 0;
2733 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002734 ~BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002735 &dev->usb_ext->usbctl2);
2736 allow_status_338x(ep);
2737 goto next_endpoints3;
2738
2739 case USB_DEVICE_U2_ENABLE:
2740 dev->u2_enable = 0;
2741 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002742 ~BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002743 &dev->usb_ext->usbctl2);
2744 allow_status_338x(ep);
2745 goto next_endpoints3;
2746
2747 case USB_DEVICE_LTM_ENABLE:
2748 dev->ltm_enable = 0;
2749 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002750 ~BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002751 &dev->usb_ext->usbctl2);
2752 allow_status_338x(ep);
2753 goto next_endpoints3;
2754
2755 default:
2756 break;
2757 }
2758 }
2759 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2760 dev->wakeup_enable = 0;
2761 writel(readl(&dev->usb->usbctl) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002762 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002763 &dev->usb->usbctl);
2764 allow_status_338x(ep);
2765 break;
2766 }
2767 goto usb3_delegate;
2768
2769 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2770 e = get_ep_by_addr(dev, w_index);
2771 if (!e)
2772 goto do_stall3;
2773 if (w_value != USB_ENDPOINT_HALT)
2774 goto do_stall3;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002775 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002776 /*
2777 * Workaround for SS SeqNum not cleared via
2778 * Endpoint Halt (Clear) bit. select endpoint
2779 */
2780 ep_clear_seqnum(e);
2781 clear_halt(e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002782 if (!list_empty(&e->queue) && e->td_dma)
2783 restart_dma(e);
2784 allow_status(ep);
2785 ep->stopped = 1;
2786 break;
2787
2788 default:
2789 goto usb3_delegate;
2790 }
2791 break;
2792 case USB_REQ_SET_FEATURE:
2793 switch (r.bRequestType) {
2794 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2795 if (!dev->addressed_state) {
2796 switch (w_value) {
2797 case USB_DEVICE_U1_ENABLE:
2798 dev->u1_enable = 1;
2799 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002800 BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002801 &dev->usb_ext->usbctl2);
2802 allow_status_338x(ep);
2803 goto next_endpoints3;
2804
2805 case USB_DEVICE_U2_ENABLE:
2806 dev->u2_enable = 1;
2807 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002808 BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002809 &dev->usb_ext->usbctl2);
2810 allow_status_338x(ep);
2811 goto next_endpoints3;
2812
2813 case USB_DEVICE_LTM_ENABLE:
2814 dev->ltm_enable = 1;
2815 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002816 BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002817 &dev->usb_ext->usbctl2);
2818 allow_status_338x(ep);
2819 goto next_endpoints3;
2820 default:
2821 break;
2822 }
2823 }
2824
2825 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2826 dev->wakeup_enable = 1;
2827 writel(readl(&dev->usb->usbctl) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002828 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002829 &dev->usb->usbctl);
2830 allow_status_338x(ep);
2831 break;
2832 }
2833 goto usb3_delegate;
2834
2835 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2836 e = get_ep_by_addr(dev, w_index);
2837 if (!e || (w_value != USB_ENDPOINT_HALT))
2838 goto do_stall3;
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002839 ep->stopped = 1;
2840 if (ep->num == 0)
2841 ep->dev->protocol_stall = 1;
2842 else {
2843 if (ep->dma)
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01002844 abort_dma(ep);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002845 set_halt(ep);
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002846 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002847 allow_status_338x(ep);
2848 break;
2849
2850 default:
2851 goto usb3_delegate;
2852 }
2853
2854 break;
2855 default:
2856
2857usb3_delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002858 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002859 r.bRequestType, r.bRequest,
2860 w_value, w_index, w_length,
2861 readl(&ep->cfg->ep_cfg));
2862
2863 ep->responded = 0;
2864 spin_unlock(&dev->lock);
2865 tmp = dev->driver->setup(&dev->gadget, &r);
2866 spin_lock(&dev->lock);
2867 }
2868do_stall3:
2869 if (tmp < 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002870 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002871 r.bRequestType, r.bRequest, tmp);
2872 dev->protocol_stall = 1;
2873 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002874 set_halt(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002875 }
2876
2877next_endpoints3:
2878
2879#undef w_value
2880#undef w_index
2881#undef w_length
2882
2883 return;
2884}
2885
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002886static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 struct net2280_ep *ep;
2889 u32 num, scratch;
2890
2891 /* most of these don't need individual acks */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002892 stat &= ~BIT(INTA_ASSERTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (!stat)
2894 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002895 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 /* starting a control request? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002898 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 union {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002900 u32 raw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 struct usb_ctrlrequest r;
2902 } u;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002903 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 struct net2280_request *req;
2905
2906 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002907 u32 val = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002908 if (val & BIT(SUPER_SPEED)) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002909 dev->gadget.speed = USB_SPEED_SUPER;
2910 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2911 EP0_SS_MAX_PACKET_SIZE);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002912 } else if (val & BIT(HIGH_SPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 dev->gadget.speed = USB_SPEED_HIGH;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002914 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2915 EP0_HS_MAX_PACKET_SIZE);
2916 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 dev->gadget.speed = USB_SPEED_FULL;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002918 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2919 EP0_HS_MAX_PACKET_SIZE);
2920 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002921 net2280_led_speed(dev, dev->gadget.speed);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002922 ep_dbg(dev, "%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002923 usb_speed_string(dev->gadget.speed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002926 ep = &dev->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 ep->irqs++;
2928
2929 /* make sure any leftover request state is cleared */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002930 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002931 while (!list_empty(&ep->queue)) {
2932 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 struct net2280_request, queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002934 done(ep, req, (req->req.actual == req->req.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 ? 0 : -EPROTO);
2936 }
2937 ep->stopped = 0;
2938 dev->protocol_stall = 0;
Ricardo Ribalda Delgado5d1b6842014-11-28 14:50:53 +01002939 if (!(dev->quirks & PLX_SUPERSPEED)) {
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002940 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002941 tmp = BIT(FIFO_OVERFLOW) |
2942 BIT(FIFO_UNDERFLOW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002943 else
2944 tmp = 0;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002945
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002946 writel(tmp | BIT(TIMEOUT) |
2947 BIT(USB_STALL_SENT) |
2948 BIT(USB_IN_NAK_SENT) |
2949 BIT(USB_IN_ACK_RCVD) |
2950 BIT(USB_OUT_PING_NAK_SENT) |
2951 BIT(USB_OUT_ACK_SENT) |
2952 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
2953 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
2954 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2955 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2956 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002957 BIT(DATA_IN_TOKEN_INTERRUPT),
2958 &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002959 }
2960 u.raw[0] = readl(&dev->usb->setup0123);
2961 u.raw[1] = readl(&dev->usb->setup4567);
David Brownell901b3d72006-09-02 03:13:45 -07002962
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002963 cpu_to_le32s(&u.raw[0]);
2964 cpu_to_le32s(&u.raw[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002966 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002967 defect7374_workaround(dev, u.r);
2968
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002969 tmp = 0;
2970
David Brownell01ee7d72007-05-25 20:40:14 -07002971#define w_value le16_to_cpu(u.r.wValue)
2972#define w_index le16_to_cpu(u.r.wIndex)
2973#define w_length le16_to_cpu(u.r.wLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 /* ack the irq */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002976 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
2977 stat ^= BIT(SETUP_PACKET_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 /* watch control traffic at the token level, and force
2980 * synchronization before letting the status stage happen.
2981 * FIXME ignore tokens we'll NAK, until driver responds.
2982 * that'll mean a lot less irqs for some drivers.
2983 */
2984 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2985 if (ep->is_in) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002986 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2987 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2988 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002989 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 } else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002991 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2992 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2993 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002994 writel(scratch, &dev->epregs[0].ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 /* we made the hardware handle most lowlevel requests;
2997 * everything else goes uplevel to the gadget code.
2998 */
Alan Stern1f26e282006-11-16 10:16:00 -05002999 ep->responded = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003000
3001 if (dev->gadget.speed == USB_SPEED_SUPER) {
3002 handle_stat0_irqs_superspeed(dev, ep, u.r);
3003 goto next_endpoints;
3004 }
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 switch (u.r.bRequest) {
3007 case USB_REQ_GET_STATUS: {
3008 struct net2280_ep *e;
David Brownell320f3452005-05-07 13:05:18 -07003009 __le32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 /* hw handles device and interface status */
3012 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
3013 goto delegate;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003014 e = get_ep_by_addr(dev, w_index);
3015 if (!e || w_length > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 goto do_stall;
3017
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003018 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003019 status = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003021 status = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 /* don't bother with a request object! */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003024 writel(0, &dev->epregs[0].ep_irqenb);
3025 set_fifo_bytecount(ep, w_length);
3026 writel((__force u32)status, &dev->epregs[0].ep_data);
3027 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003028 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 goto next_endpoints;
3030 }
3031 break;
3032 case USB_REQ_CLEAR_FEATURE: {
3033 struct net2280_ep *e;
3034
3035 /* hw handles device features */
3036 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3037 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003038 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003040 e = get_ep_by_addr(dev, w_index);
3041 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003043 if (e->wedged) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003044 ep_vdbg(dev, "%s wedged, halt not cleared\n",
Alan Stern80661342008-08-14 15:49:11 -04003045 ep->ep.name);
3046 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003047 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Alan Stern80661342008-08-14 15:49:11 -04003048 clear_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003049 if ((ep->dev->quirks & PLX_SUPERSPEED) &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003050 !list_empty(&e->queue) && e->td_dma)
3051 restart_dma(e);
Alan Stern80661342008-08-14 15:49:11 -04003052 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003053 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 goto next_endpoints;
3055 }
3056 break;
3057 case USB_REQ_SET_FEATURE: {
3058 struct net2280_ep *e;
3059
3060 /* hw handles device features */
3061 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3062 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003063 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003065 e = get_ep_by_addr(dev, w_index);
3066 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003068 if (e->ep.name == ep0name)
3069 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003070 set_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003071 if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003072 abort_dma(e);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003073 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003074 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 goto next_endpoints;
3076 }
3077 break;
3078 default:
3079delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003080 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 "ep_cfg %08x\n",
3082 u.r.bRequestType, u.r.bRequest,
David Brownell320f3452005-05-07 13:05:18 -07003083 w_value, w_index, w_length,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003084 readl(&ep->cfg->ep_cfg));
Alan Stern1f26e282006-11-16 10:16:00 -05003085 ep->responded = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003086 spin_unlock(&dev->lock);
3087 tmp = dev->driver->setup(&dev->gadget, &u.r);
3088 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090
3091 /* stall ep0 on error */
3092 if (tmp < 0) {
3093do_stall:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003094 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 u.r.bRequestType, u.r.bRequest, tmp);
3096 dev->protocol_stall = 1;
3097 }
3098
3099 /* some in/out token irq should follow; maybe stall then.
3100 * driver must queue a request (even zlp) or halt ep0
3101 * before the host times out.
3102 */
3103 }
3104
David Brownell320f3452005-05-07 13:05:18 -07003105#undef w_value
3106#undef w_index
3107#undef w_length
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109next_endpoints:
3110 /* endpoint data irq ? */
3111 scratch = stat & 0x7f;
3112 stat &= ~0x7f;
3113 for (num = 0; scratch; num++) {
3114 u32 t;
3115
3116 /* do this endpoint's FIFO and queue need tending? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003117 t = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if ((scratch & t) == 0)
3119 continue;
3120 scratch ^= t;
3121
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003122 ep = &dev->ep[num];
3123 handle_ep_small(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 }
3125
3126 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003127 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003130#define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3131 BIT(DMA_C_INTERRUPT) | \
3132 BIT(DMA_B_INTERRUPT) | \
3133 BIT(DMA_A_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134#define PCI_ERROR_INTERRUPTS ( \
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003135 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3136 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3137 BIT(PCI_RETRY_ABORT_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003139static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
Felipe Balbiccf5fb62015-02-27 09:44:51 -06003140__releases(dev->lock)
3141__acquires(dev->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
3143 struct net2280_ep *ep;
3144 u32 tmp, num, mask, scratch;
3145
3146 /* after disconnect there's nothing else to do! */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003147 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3148 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03003151 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
David Brownell901b3d72006-09-02 03:13:45 -07003152 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 * only indicates a change in the reset state).
3154 */
3155 if (stat & tmp) {
Alan Sternb611e422014-11-06 14:27:56 +08003156 bool reset = false;
3157 bool disconnect = false;
3158
3159 /*
3160 * Ignore disconnects and resets if the speed hasn't been set.
3161 * VBUS can bounce and there's always an initial reset.
3162 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003163 writel(tmp, &dev->regs->irqstat1);
Alan Sternb611e422014-11-06 14:27:56 +08003164 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3165 if ((stat & BIT(VBUS_INTERRUPT)) &&
3166 (readl(&dev->usb->usbctl) &
3167 BIT(VBUS_PIN)) == 0) {
3168 disconnect = true;
3169 ep_dbg(dev, "disconnect %s\n",
3170 dev->driver->driver.name);
3171 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3172 (readl(&dev->usb->usbstat) & mask)
3173 == 0) {
3174 reset = true;
3175 ep_dbg(dev, "reset %s\n",
3176 dev->driver->driver.name);
3177 }
3178
3179 if (disconnect || reset) {
3180 stop_activity(dev, dev->driver);
3181 ep0_start(dev);
3182 spin_unlock(&dev->lock);
3183 if (reset)
3184 usb_gadget_udc_reset
3185 (&dev->gadget, dev->driver);
3186 else
3187 (dev->driver->disconnect)
3188 (&dev->gadget);
3189 spin_lock(&dev->lock);
3190 return;
3191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 }
3193 stat &= ~tmp;
3194
3195 /* vBUS can bounce ... one of many reasons to ignore the
3196 * notion of hotplug events on bus connect/disconnect!
3197 */
3198 if (!stat)
3199 return;
3200 }
3201
3202 /* NOTE: chip stays in PCI D0 state for now, but it could
3203 * enter D1 to save more power
3204 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003205 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (stat & tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003207 writel(tmp, &dev->regs->irqstat1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003208 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 if (dev->driver->suspend)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003210 dev->driver->suspend(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (!enable_suspend)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003212 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 } else {
3214 if (dev->driver->resume)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003215 dev->driver->resume(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 /* at high speed, note erratum 0133 */
3217 }
3218 stat &= ~tmp;
3219 }
3220
3221 /* clear any other status/irqs */
3222 if (stat)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003223 writel(stat, &dev->regs->irqstat1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225 /* some status we can just ignore */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003226 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003227 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3228 BIT(SUSPEND_REQUEST_INTERRUPT) |
3229 BIT(RESUME_INTERRUPT) |
3230 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003231 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003232 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3233 BIT(RESUME_INTERRUPT) |
3234 BIT(SOF_DOWN_INTERRUPT) |
3235 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (!stat)
3238 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003239 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
3241 /* DMA status, for ep-{a,b,c,d} */
3242 scratch = stat & DMA_INTERRUPTS;
3243 stat &= ~DMA_INTERRUPTS;
3244 scratch >>= 9;
3245 for (num = 0; scratch; num++) {
3246 struct net2280_dma_regs __iomem *dma;
3247
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003248 tmp = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 if ((tmp & scratch) == 0)
3250 continue;
3251 scratch ^= tmp;
3252
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003253 ep = &dev->ep[num + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 dma = ep->dma;
3255
3256 if (!dma)
3257 continue;
3258
3259 /* clear ep's dma status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003260 tmp = readl(&dma->dmastat);
3261 writel(tmp, &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003263 /* dma sync*/
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003264 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003265 u32 r_dmacount = readl(&dma->dmacount);
3266 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003267 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003268 continue;
3269 }
3270
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003271 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3272 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3273 ep->ep.name, tmp);
3274 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003276 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
3278 /* OUT transfers terminate when the data from the
3279 * host is in our memory. Process whatever's done.
3280 * On this path, we know transfer's last packet wasn't
3281 * less than req->length. NAK_OUT_PACKETS may be set,
3282 * or the FIFO may already be holding new packets.
3283 *
3284 * IN transfers can linger in the FIFO for a very
3285 * long time ... we ignore that for now, accounting
3286 * precisely (like PIO does) needs per-packet irqs
3287 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003288 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 /* disable dma on inactive queues; else maybe restart */
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003291 if (!list_empty(&ep->queue)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003292 tmp = readl(&dma->dmactl);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003293 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
3295 ep->irqs++;
3296 }
3297
3298 /* NOTE: there are other PCI errors we might usefully notice.
3299 * if they appear very often, here's where to try recovering.
3300 */
3301 if (stat & PCI_ERROR_INTERRUPTS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003302 ep_err(dev, "pci dma error; stat %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 stat &= ~PCI_ERROR_INTERRUPTS;
3304 /* these are fatal errors, but "maybe" they won't
3305 * happen again ...
3306 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003307 stop_activity(dev, dev->driver);
3308 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 stat = 0;
3310 }
3311
3312 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003313 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003316static irqreturn_t net2280_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
3318 struct net2280 *dev = _dev;
3319
Alan Stern658ad5e2006-04-14 16:44:11 -04003320 /* shared interrupt, not ours */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003321 if ((dev->quirks & PLX_LEGACY) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003322 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
Alan Stern658ad5e2006-04-14 16:44:11 -04003323 return IRQ_NONE;
3324
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003325 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
3327 /* handle disconnect, dma, and more */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003328 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 /* control requests and PIO */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003331 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003333 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003334 /* re-enable interrupt to trigger any possible new interrupt */
3335 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3336 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3337 writel(pciirqenb1, &dev->regs->pciirqenb1);
3338 }
3339
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003340 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 return IRQ_HANDLED;
3343}
3344
3345/*-------------------------------------------------------------------------*/
3346
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003347static void gadget_release(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003349 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003351 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
3354/* tear down the binding between this driver and the pci device */
3355
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003356static void net2280_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003358 struct net2280 *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003360 usb_del_gadget_udc(&dev->gadget);
3361
David Brownell6bea4762006-12-05 03:15:33 -08003362 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
3364 /* then clean up the resources we allocated during probe() */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003365 net2280_led_shutdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (dev->requests) {
3367 int i;
3368 for (i = 1; i < 5; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003369 if (!dev->ep[i].dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003371 pci_pool_free(dev->requests, dev->ep[i].dummy,
3372 dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003374 pci_pool_destroy(dev->requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 if (dev->got_irq)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003377 free_irq(pdev->irq, dev);
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003378 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003379 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (dev->regs)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003381 iounmap(dev->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 if (dev->region)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003383 release_mem_region(pci_resource_start(pdev, 0),
3384 pci_resource_len(pdev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (dev->enabled)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003386 pci_disable_device(pdev);
3387 device_remove_file(&pdev->dev, &dev_attr_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003389 ep_info(dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
3392/* wrap this driver around the specified device, but
3393 * don't respond over USB until a gadget driver binds to us.
3394 */
3395
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003396static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 struct net2280 *dev;
3399 unsigned long resource, len;
3400 void __iomem *base = NULL;
3401 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 /* alloc, and start init */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003404 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3405 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 retval = -ENOMEM;
3407 goto done;
3408 }
3409
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003410 pci_set_drvdata(pdev, dev);
3411 spin_lock_init(&dev->lock);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003412 dev->quirks = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 dev->pdev = pdev;
3414 dev->gadget.ops = &net2280_ops;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003415 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003416 USB_SPEED_SUPER : USB_SPEED_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
3418 /* the "gadget" abstracts/virtualizes the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 dev->gadget.name = driver_name;
3420
3421 /* now all the pci goodies ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003422 if (pci_enable_device(pdev) < 0) {
3423 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 goto done;
3425 }
3426 dev->enabled = 1;
3427
3428 /* BAR 0 holds all the registers
3429 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3430 * BAR 2 is fifo memory; unused here
3431 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003432 resource = pci_resource_start(pdev, 0);
3433 len = pci_resource_len(pdev, 0);
3434 if (!request_mem_region(resource, len, driver_name)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003435 ep_dbg(dev, "controller already in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 retval = -EBUSY;
3437 goto done;
3438 }
3439 dev->region = 1;
3440
David Brownell901b3d72006-09-02 03:13:45 -07003441 /* FIXME provide firmware download interface to put
3442 * 8051 code into the chip, e.g. to turn on PCI PM.
3443 */
3444
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003445 base = ioremap_nocache(resource, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 if (base == NULL) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003447 ep_dbg(dev, "can't map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 retval = -EFAULT;
3449 goto done;
3450 }
3451 dev->regs = (struct net2280_regs __iomem *) base;
3452 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3453 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3454 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3455 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3456 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3457
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003458 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003459 u32 fsmvalue;
3460 u32 usbstat;
3461 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3462 (base + 0x00b4);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003463 dev->llregs = (struct usb338x_ll_regs __iomem *)
3464 (base + 0x0700);
3465 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3466 (base + 0x0748);
3467 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3468 (base + 0x077c);
3469 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3470 (base + 0x079c);
3471 dev->plregs = (struct usb338x_pl_regs __iomem *)
3472 (base + 0x0800);
3473 usbstat = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003474 dev->enhanced_mode = !!(usbstat & BIT(11));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003475 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3476 /* put into initial config, link up all endpoints */
3477 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3478 (0xf << DEFECT7374_FSM_FIELD);
3479 /* See if firmware needs to set up for workaround: */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003480 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3481 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003482 writel(0, &dev->usb->usbctl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003483 } else
3484 dev->bug7734_patched = 0;
3485 } else {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003486 dev->enhanced_mode = 0;
3487 dev->n_ep = 7;
3488 /* put into initial config, link up all endpoints */
3489 writel(0, &dev->usb->usbctl);
3490 }
3491
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003492 usb_reset(dev);
3493 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495 /* irq setup after old hardware is cleaned up */
3496 if (!pdev->irq) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003497 ep_err(dev, "No IRQ. Check PCI setup!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 retval = -ENODEV;
3499 goto done;
3500 }
David S. Millerc6387a42006-06-20 01:21:29 -07003501
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003502 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003503 if (pci_enable_msi(pdev))
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003504 ep_err(dev, "Failed to enable MSI mode\n");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003505
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003506 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3507 driver_name, dev)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003508 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 retval = -EBUSY;
3510 goto done;
3511 }
3512 dev->got_irq = 1;
3513
3514 /* DMA setup */
3515 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003516 dev->requests = pci_pool_create("requests", pdev,
3517 sizeof(struct net2280_dma),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 0 /* no alignment requirements */,
3519 0 /* or page-crossing issues */);
3520 if (!dev->requests) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003521 ep_dbg(dev, "can't get request pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 retval = -ENOMEM;
3523 goto done;
3524 }
3525 for (i = 1; i < 5; i++) {
3526 struct net2280_dma *td;
3527
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003528 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3529 &dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 if (!td) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003531 ep_dbg(dev, "can't get dummy %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 retval = -ENOMEM;
3533 goto done;
3534 }
3535 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 td->dmadesc = td->dmaaddr;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003537 dev->ep[i].dummy = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
3539
3540 /* enable lower-overhead pci memory bursts during DMA */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003541 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003542 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3543 /*
3544 * 256 write retries may not be enough...
3545 BIT(PCI_RETRY_ABORT_ENABLE) |
3546 */
3547 BIT(DMA_READ_MULTIPLE_ENABLE) |
3548 BIT(DMA_READ_LINE_ENABLE),
3549 &dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003551 pci_set_master(pdev);
3552 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554 /* ... also flushes any posted pci writes */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003555 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 /* done */
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003558 ep_info(dev, "%s\n", driver_desc);
3559 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
David S. Millerc6387a42006-06-20 01:21:29 -07003560 pdev->irq, base, dev->chiprev);
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01003561 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003562 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003563 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3564 if (retval)
3565 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Felipe Balbi2901df62013-02-26 15:15:27 +02003567 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3568 gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003569 if (retval)
3570 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return 0;
3572
3573done:
3574 if (dev)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003575 net2280_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 return retval;
3577}
3578
Alan Stern2d61bde2006-05-05 16:23:42 -04003579/* make sure the board is quiescent; otherwise it will continue
3580 * generating IRQs across the upcoming reboot.
3581 */
3582
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003583static void net2280_shutdown(struct pci_dev *pdev)
Alan Stern2d61bde2006-05-05 16:23:42 -04003584{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003585 struct net2280 *dev = pci_get_drvdata(pdev);
Alan Stern2d61bde2006-05-05 16:23:42 -04003586
3587 /* disable IRQs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003588 writel(0, &dev->regs->pciirqenb0);
3589 writel(0, &dev->regs->pciirqenb1);
Alan Stern2d61bde2006-05-05 16:23:42 -04003590
3591 /* disable the pullup so the host will think we're gone */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003592 writel(0, &dev->usb->usbctl);
Alan Stern2f076072013-01-30 16:40:14 -05003593
Alan Stern2d61bde2006-05-05 16:23:42 -04003594}
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597/*-------------------------------------------------------------------------*/
3598
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003599static const struct pci_device_id pci_ids[] = { {
David Brownell901b3d72006-09-02 03:13:45 -07003600 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3601 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003602 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 .device = 0x2280,
3604 .subvendor = PCI_ANY_ID,
3605 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003606 .driver_data = PLX_LEGACY | PLX_2280,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003607 }, {
David Brownell901b3d72006-09-02 03:13:45 -07003608 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3609 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003610 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003611 .device = 0x2282,
3612 .subvendor = PCI_ANY_ID,
3613 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003614 .driver_data = PLX_LEGACY,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003615 },
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003616 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003617 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3618 .class_mask = ~0,
3619 .vendor = PCI_VENDOR_ID_PLX,
3620 .device = 0x3380,
3621 .subvendor = PCI_ANY_ID,
3622 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003623 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003624 },
3625 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003626 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3627 .class_mask = ~0,
3628 .vendor = PCI_VENDOR_ID_PLX,
3629 .device = 0x3382,
3630 .subvendor = PCI_ANY_ID,
3631 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003632 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003633 },
3634{ /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635};
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003636MODULE_DEVICE_TABLE(pci, pci_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638/* pci driver glue; this is a "new style" PCI driver module */
3639static struct pci_driver net2280_pci_driver = {
3640 .name = (char *) driver_name,
3641 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
3643 .probe = net2280_probe,
3644 .remove = net2280_remove,
Alan Stern2d61bde2006-05-05 16:23:42 -04003645 .shutdown = net2280_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 /* FIXME add power management support */
3648};
3649
Ricardo Ribalda Delgado9a028e42014-05-20 18:30:07 +02003650module_pci_driver(net2280_pci_driver);
3651
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003652MODULE_DESCRIPTION(DRIVER_DESC);
3653MODULE_AUTHOR("David Brownell");
3654MODULE_LICENSE("GPL");