blob: 4a90ae6f9bb89f203d7971babd0fb9d154a9e8b4 [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
241 tmp |= usb_endpoint_num(desc);
242 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
243 }
244
245 /* Make sure all the registers are written before ep_rsp*/
246 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* for OUT transfers, block the rx fifo until a read is posted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200250 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200251 else if (!(dev->quirks & PLX_2280)) {
David Brownell901b3d72006-09-02 03:13:45 -0700252 /* Added for 2282, Don't use nak packets on an in endpoint,
253 * this was ignored on 2280
254 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200255 writel(BIT(CLEAR_NAK_OUT_PACKETS) |
256 BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Mian Yousaf Kaukabe6ac4bb2015-05-16 22:33:31 +0200259 if (dev->quirks & PLX_SUPERSPEED)
260 ep_clear_seqnum(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200261 writel(tmp, &ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* enable irqs */
264 if (!ep->dma) { /* pio, per-packet */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200265 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200267 tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
268 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200269 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200270 tmp |= readl(&ep->regs->ep_irqenb);
271 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 } else { /* dma, per-request */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200273 tmp = BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200274 tmp |= readl(&dev->regs->pciirqenb1);
275 writel(tmp, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* for short OUT transfers, dma completions can't
278 * advance the queue; do it pio-style, by hand.
279 * NOTE erratum 0112 workaround #2
280 */
281 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200282 tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200283 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200285 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 }
288
289 tmp = desc->bEndpointAddress;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200290 ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200291 _ep->name, tmp & 0x0f, DIR_STRING(tmp),
292 type_string(desc->bmAttributes),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 ep->dma ? "dma" : "pio", max);
294
295 /* pci writes may still be posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200296 spin_unlock_irqrestore(&dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100297 return ret;
298
299print_err:
300 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200304static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 u32 result;
307
308 do {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200309 result = readl(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (result == ~(u32)0) /* "device unplugged" */
311 return -ENODEV;
312 result &= mask;
313 if (result == done)
314 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200315 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 usec--;
317 } while (usec > 0);
318 return -ETIMEDOUT;
319}
320
David Brownell901b3d72006-09-02 03:13:45 -0700321static const struct usb_ep_ops net2280_ep_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200323static void ep_reset_228x(struct net2280_regs __iomem *regs,
324 struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 u32 tmp;
327
328 ep->desc = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200329 INIT_LIST_HEAD(&ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Robert Baldygae117e742013-12-13 12:23:38 +0100331 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ep->ep.ops = &net2280_ep_ops;
333
334 /* disable the dma, irqs, endpoint... */
335 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200336 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200337 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
338 BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
339 BIT(DMA_ABORT),
340 &ep->dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200342 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200343 tmp &= ~BIT(ep->num);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200344 writel(tmp, &regs->pciirqenb0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200346 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200347 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200348 writel(tmp, &regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200350 writel(0, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* init to our chosen defaults, notably so that we NAK OUT
353 * packets until the driver queues a read (+note erratum 0112)
354 */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200355 if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200356 tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
357 BIT(SET_NAK_OUT_PACKETS) |
358 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
359 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100360 } else {
361 /* added for 2282 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200362 tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
363 BIT(CLEAR_NAK_OUT_PACKETS) |
364 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
365 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (ep->num != 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200369 tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
370 BIT(CLEAR_ENDPOINT_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200372 writel(tmp, &ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* scrub most status bits, and flush any fifo state */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200375 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200376 tmp = BIT(FIFO_OVERFLOW) |
377 BIT(FIFO_UNDERFLOW);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100378 else
379 tmp = 0;
380
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200381 writel(tmp | BIT(TIMEOUT) |
382 BIT(USB_STALL_SENT) |
383 BIT(USB_IN_NAK_SENT) |
384 BIT(USB_IN_ACK_RCVD) |
385 BIT(USB_OUT_PING_NAK_SENT) |
386 BIT(USB_OUT_ACK_SENT) |
387 BIT(FIFO_FLUSH) |
388 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
389 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
390 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
391 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
392 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200393 BIT(DATA_IN_TOKEN_INTERRUPT),
394 &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* fifo size is handled separately */
397}
398
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200399static void ep_reset_338x(struct net2280_regs __iomem *regs,
400 struct net2280_ep *ep)
401{
402 u32 tmp, dmastat;
403
404 ep->desc = NULL;
405 INIT_LIST_HEAD(&ep->queue);
406
407 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
408 ep->ep.ops = &net2280_ep_ops;
409
410 /* disable the dma, irqs, endpoint... */
411 if (ep->dma) {
412 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200413 writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
414 BIT(DMA_PAUSE_DONE_INTERRUPT) |
415 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200416 BIT(DMA_TRANSACTION_DONE_INTERRUPT),
417 /* | BIT(DMA_ABORT), */
418 &ep->dma->dmastat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200419
420 dmastat = readl(&ep->dma->dmastat);
421 if (dmastat == 0x5002) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200422 ep_warn(ep->dev, "The dmastat return = %x!!\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200423 dmastat);
424 writel(0x5a, &ep->dma->dmastat);
425 }
426
427 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200428 tmp &= ~BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200429 writel(tmp, &regs->pciirqenb0);
430 } else {
431 if (ep->num < 5) {
432 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200433 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200434 writel(tmp, &regs->pciirqenb1);
435 }
436 }
437 writel(0, &ep->regs->ep_irqenb);
438
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200439 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
440 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
441 BIT(FIFO_OVERFLOW) |
442 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
443 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
444 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
445 BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200446}
447
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200448static void nuke(struct net2280_ep *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200450static int net2280_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct net2280_ep *ep;
453 unsigned long flags;
454
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200455 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100456 if (!_ep || !ep->desc || _ep->name == ep0name) {
457 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100459 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200460 spin_lock_irqsave(&ep->dev->lock, flags);
461 nuke(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200462
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200463 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200464 ep_reset_338x(ep->dev->regs, ep);
465 else
466 ep_reset_228x(ep->dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200468 ep_vdbg(ep->dev, "disabled %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ep->dma ? "dma" : "pio", _ep->name);
470
471 /* synch memory views with the device */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200472 (void)readl(&ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +0100474 if (!ep->dma && ep->num >= 1 && ep->num <= 4)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200475 ep->dma = &ep->dev->dma[ep->num - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200477 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479}
480
481/*-------------------------------------------------------------------------*/
482
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200483static struct usb_request
484*net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct net2280_ep *ep;
487 struct net2280_request *req;
488
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100489 if (!_ep) {
490 pr_err("%s: Invalid ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return NULL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100492 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200493 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800495 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!req)
497 return NULL;
498
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200499 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* this dma descriptor may be swapped with the previous dummy */
502 if (ep->dma) {
503 struct net2280_dma *td;
504
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200505 td = pci_pool_alloc(ep->dev->requests, gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 &req->td_dma);
507 if (!td) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200508 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return NULL;
510 }
511 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 td->dmadesc = td->dmaaddr;
513 req->td = td;
514 }
515 return &req->req;
516}
517
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200518static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct net2280_ep *ep;
521 struct net2280_request *req;
522
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200523 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100524 if (!_ep || !_req) {
525 dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n",
526 __func__, _ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200530 req = container_of(_req, struct net2280_request, req);
531 WARN_ON(!list_empty(&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (req->td)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200533 pci_pool_free(ep->dev->requests, req->td, req->td_dma);
534 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537/*-------------------------------------------------------------------------*/
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/* load a packet into the fifo we use for usb IN transfers.
540 * works for all endpoints.
541 *
542 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
543 * at a time, but this code is simpler because it knows it only writes
544 * one packet. ep-a..ep-d should use dma instead.
545 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200546static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct net2280_ep_regs __iomem *regs = ep->regs;
549 u8 *buf;
550 u32 tmp;
551 unsigned count, total;
552
553 /* INVARIANT: fifo is currently empty. (testable) */
554
555 if (req) {
556 buf = req->buf + req->actual;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200557 prefetch(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 total = req->length - req->actual;
559 } else {
560 total = 0;
561 buf = NULL;
562 }
563
564 /* write just one packet at a time */
565 count = ep->ep.maxpacket;
566 if (count > total) /* min() cannot be used on a bitfield */
567 count = total;
568
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200569 ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ep->ep.name, count,
571 (count != ep->ep.maxpacket) ? " (short)" : "",
572 req);
573 while (count >= 4) {
574 /* NOTE be careful if you try to align these. fifo lines
575 * should normally be full (4 bytes) and successive partial
576 * lines are ok only in certain cases.
577 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200578 tmp = get_unaligned((u32 *)buf);
579 cpu_to_le32s(&tmp);
580 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 buf += 4;
582 count -= 4;
583 }
584
585 /* last fifo entry is "short" unless we wrote a full packet.
586 * also explicitly validate last word in (periodic) transfers
587 * when maxpacket is not a multiple of 4 bytes.
588 */
589 if (count || total < ep->ep.maxpacket) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200590 tmp = count ? get_unaligned((u32 *)buf) : count;
591 cpu_to_le32s(&tmp);
592 set_fifo_bytecount(ep, count & 0x03);
593 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 /* pci writes may still be posted */
597}
598
599/* work around erratum 0106: PCI and USB race over the OUT fifo.
600 * caller guarantees chiprev 0100, out endpoint is NAKing, and
601 * there's no real data in the fifo.
602 *
603 * NOTE: also used in cases where that erratum doesn't apply:
604 * where the host wrote "too much" data to us.
605 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200606static void out_flush(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 u32 __iomem *statp;
609 u32 tmp;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 statp = &ep->regs->ep_stat;
Ricardo Ribalda Delgadod82f3db2014-11-28 14:51:01 +0100612
613 tmp = readl(statp);
614 if (tmp & BIT(NAK_OUT_PACKETS)) {
615 ep_dbg(ep->dev, "%s %s %08x !NAK\n",
616 ep->ep.name, __func__, tmp);
617 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
618 }
619
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200620 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200621 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
622 statp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200623 writel(BIT(FIFO_FLUSH), statp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200624 /* Make sure that stap is written */
625 mb();
626 tmp = readl(statp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200627 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* high speed did bulk NYET; fifo isn't filling */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200629 ep->dev->gadget.speed == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 unsigned usec;
631
632 usec = 50; /* 64 byte bulk/interrupt */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200633 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
634 BIT(USB_OUT_PING_NAK_SENT), usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
636 }
637}
638
639/* unload packet(s) from the fifo we use for usb OUT transfers.
640 * returns true iff the request completed, because of short packet
641 * or the request buffer having filled with full packets.
642 *
643 * for ep-a..ep-d this will read multiple packets out when they
644 * have been accepted.
645 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200646static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct net2280_ep_regs __iomem *regs = ep->regs;
649 u8 *buf = req->req.buf + req->req.actual;
650 unsigned count, tmp, is_short;
651 unsigned cleanup = 0, prevent = 0;
652
653 /* erratum 0106 ... packets coming in during fifo reads might
654 * be incompletely rejected. not all cases have workarounds.
655 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200656 if (ep->dev->chiprev == 0x0100 &&
657 ep->dev->gadget.speed == USB_SPEED_FULL) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200658 udelay(1);
659 tmp = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200660 if ((tmp & BIT(NAK_OUT_PACKETS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 cleanup = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200662 else if ((tmp & BIT(FIFO_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200663 start_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 prevent = 1;
665 }
666 /* else: hope we don't see the problem */
667 }
668
669 /* never overflow the rx buffer. the fifo reads packets until
670 * it sees a short one; we might not be ready for them all.
671 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200672 prefetchw(buf);
673 count = readl(&regs->ep_avail);
674 if (unlikely(count == 0)) {
675 udelay(1);
676 tmp = readl(&ep->regs->ep_stat);
677 count = readl(&regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* handled that data already? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200679 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681 }
682
683 tmp = req->req.length - req->req.actual;
684 if (count > tmp) {
685 /* as with DMA, data overflow gets flushed */
686 if ((tmp % ep->ep.maxpacket) != 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200687 ep_err(ep->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 "%s out fifo %d bytes, expected %d\n",
689 ep->ep.name, count, tmp);
690 req->req.status = -EOVERFLOW;
691 cleanup = 1;
692 /* NAK_OUT_PACKETS will be set, so flushing is safe;
693 * the next read will start with the next packet
694 */
695 } /* else it's a ZLP, no worries */
696 count = tmp;
697 }
698 req->req.actual += count;
699
700 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
701
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200702 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 -0700703 ep->ep.name, count, is_short ? " (short)" : "",
704 cleanup ? " flush" : "", prevent ? " nak" : "",
705 req, req->req.actual, req->req.length);
706
707 while (count >= 4) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200708 tmp = readl(&regs->ep_data);
709 cpu_to_le32s(&tmp);
710 put_unaligned(tmp, (u32 *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 buf += 4;
712 count -= 4;
713 }
714 if (count) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200715 tmp = readl(&regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* LE conversion is implicit here: */
717 do {
718 *buf++ = (u8) tmp;
719 tmp >>= 8;
720 } while (--count);
721 }
722 if (cleanup)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200723 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (prevent) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200725 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200726 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200729 return is_short || ((req->req.actual == req->req.length) &&
730 !req->req.zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733/* fill out dma descriptor to match a given request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200734static void fill_dma_desc(struct net2280_ep *ep,
735 struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct net2280_dma *td = req->td;
738 u32 dmacount = req->req.length;
739
740 /* don't let DMA continue after a short OUT packet,
741 * so overruns can't affect the next transfer.
742 * in case of overruns on max-size packets, we can't
743 * stop the fifo from filling but we can flush it.
744 */
745 if (ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200746 dmacount |= BIT(DMA_DIRECTION);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200747 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200748 !(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200749 dmacount |= BIT(END_OF_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 req->valid = valid;
752 if (valid)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200753 dmacount |= BIT(VALID_BIT);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100754 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* td->dmadesc = previously set by caller */
757 td->dmaaddr = cpu_to_le32 (req->req.dma);
758
759 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200760 wmb();
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700761 td->dmacount = cpu_to_le32(dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764static const u32 dmactl_default =
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200765 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
766 BIT(DMA_CLEAR_COUNT_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* erratum 0116 workaround part 1 (use POLLING) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200768 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
769 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
770 BIT(DMA_VALID_BIT_ENABLE) |
771 BIT(DMA_SCATTER_GATHER_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* erratum 0116 workaround part 2 (no AUTOSTART) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200773 BIT(DMA_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200775static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200777 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200780static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200782 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200783 spin_stop_dma(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200786static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct net2280_dma_regs __iomem *dma = ep->dma;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200789 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200791 if (!(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200792 tmp |= BIT(END_OF_CHAIN);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100793
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200794 writel(tmp, &dma->dmacount);
795 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200797 writel(td_dma, &dma->dmadesc);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200798 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200799 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200800 writel(dmactl, &dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200803 (void) readl(&ep->dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200805 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (!ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200808 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200811static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 u32 tmp;
814 struct net2280_dma_regs __iomem *dma = ep->dma;
815
816 /* FIXME can't use DMA for ZLPs */
817
818 /* on this path we "know" there's no dma active (yet) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200819 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200820 writel(0, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* previous OUT packet might have been short */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200823 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
824 BIT(NAK_OUT_PACKETS))) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200825 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 &ep->regs->ep_stat);
827
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200828 tmp = readl(&ep->regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200830 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /* transfer all/some fifo data */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200833 writel(req->req.dma, &dma->dmaaddr);
834 tmp = min(tmp, req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* dma irq, faking scatterlist status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200837 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200838 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
839 &dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 req->td->dmadesc = 0;
841 req->valid = 1;
842
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200843 writel(BIT(DMA_ENABLE), &dma->dmactl);
844 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return;
846 }
847 }
848
849 tmp = dmactl_default;
850
851 /* force packet boundaries between dma requests, but prevent the
852 * controller from automagically writing a last "short" packet
853 * (zero length) unless the driver explicitly said to do that.
854 */
855 if (ep->is_in) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200856 if (likely((req->req.length % ep->ep.maxpacket) ||
857 req->req.zero)){
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200858 tmp |= BIT(DMA_FIFO_VALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 ep->in_fifo_validate = 1;
860 } else
861 ep->in_fifo_validate = 0;
862 }
863
864 /* init req->td, pointing to the current dummy */
865 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200866 fill_dma_desc(ep, req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100868 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200870 start_queue(ep, tmp, req->td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200874queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct net2280_dma *end;
877 dma_addr_t tmp;
878
879 /* swap new dummy for old, link; fill and maybe activate */
880 end = ep->dummy;
881 ep->dummy = req->td;
882 req->td = end;
883
884 tmp = ep->td_dma;
885 ep->td_dma = req->td_dma;
886 req->td_dma = tmp;
887
888 end->dmadesc = cpu_to_le32 (ep->td_dma);
889
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200890 fill_dma_desc(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893static void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200894done(struct net2280_ep *ep, struct net2280_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 struct net2280 *dev;
897 unsigned stopped = ep->stopped;
898
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200899 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (req->req.status == -EINPROGRESS)
902 req->req.status = status;
903 else
904 status = req->req.status;
905
906 dev = ep->dev;
Felipe Balbiae4d7932011-12-19 12:09:56 +0200907 if (ep->dma)
908 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (status && status != -ESHUTDOWN)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200911 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 ep->ep.name, &req->req, status,
913 req->req.actual, req->req.length);
914
915 /* don't modify queue heads during completion callback */
916 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200917 spin_unlock(&dev->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200918 usb_gadget_giveback_request(&ep->ep, &req->req);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200919 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 ep->stopped = stopped;
921}
922
923/*-------------------------------------------------------------------------*/
924
925static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200926net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 struct net2280_request *req;
929 struct net2280_ep *ep;
930 struct net2280 *dev;
931 unsigned long flags;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100932 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /* we always require a cpu-view buffer, so that we can
935 * always use pio (as fallback or whatever).
936 */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100937 ep = container_of(_ep, struct net2280_ep, ep);
938 if (!_ep || (!ep->desc && ep->num != 0)) {
939 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
940 return -EINVAL;
941 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200942 req = container_of(_req, struct net2280_request, req);
943 if (!_req || !_req->complete || !_req->buf ||
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100944 !list_empty(&req->queue)) {
945 ret = -EINVAL;
946 goto print_err;
947 }
948 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
949 ret = -EDOM;
950 goto print_err;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dev = ep->dev;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100953 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
954 ret = -ESHUTDOWN;
955 goto print_err;
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* FIXME implement PIO fallback for ZLPs with DMA */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100959 if (ep->dma && _req->length == 0) {
960 ret = -EOPNOTSUPP;
961 goto print_err;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* set up dma mapping in case the caller didn't */
Felipe Balbiae4d7932011-12-19 12:09:56 +0200965 if (ep->dma) {
Felipe Balbiae4d7932011-12-19 12:09:56 +0200966 ret = usb_gadget_map_request(&dev->gadget, _req,
967 ep->is_in);
968 if (ret)
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100969 goto print_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200972 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200975 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 _req->status = -EINPROGRESS;
978 _req->actual = 0;
979
980 /* kickstart this i/o queue? */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100981 if (list_empty(&ep->queue) && !ep->stopped &&
982 !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
983 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* use DMA if the endpoint supports it, else pio */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +0100986 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200987 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 else {
989 /* maybe there's no control data, just status ack */
990 if (ep->num == 0 && _req->length == 0) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200991 allow_status(ep);
992 done(ep, req, 0);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200993 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto done;
995 }
996
997 /* PIO ... stuff the fifo, or unblock it. */
998 if (ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200999 write_fifo(ep, _req);
1000 else if (list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 u32 s;
1002
1003 /* OUT FIFO might have packet(s) buffered */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001004 s = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001005 if ((s & BIT(FIFO_EMPTY)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* note: _req->short_not_ok is
1007 * ignored here since PIO _always_
1008 * stops queue advance here, and
1009 * _req->status doesn't change for
1010 * short reads (only _req->actual)
1011 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001012 if (read_fifo(ep, req) &&
1013 ep->num == 0) {
1014 done(ep, req, 0);
1015 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* don't queue it */
1017 req = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001018 } else if (read_fifo(ep, req) &&
1019 ep->num != 0) {
1020 done(ep, req, 0);
1021 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001023 s = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
1026 /* don't NAK, let the fifo fill */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001027 if (req && (s & BIT(NAK_OUT_PACKETS)))
1028 writel(BIT(CLEAR_NAK_OUT_PACKETS),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 &ep->regs->ep_rsp);
1030 }
1031 }
1032
1033 } else if (ep->dma) {
1034 int valid = 1;
1035
1036 if (ep->is_in) {
1037 int expect;
1038
1039 /* preventing magic zlps is per-engine state, not
1040 * per-transfer; irq logic must recover hiccups.
1041 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001042 expect = likely(req->req.zero ||
1043 (req->req.length % ep->ep.maxpacket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (expect != ep->in_fifo_validate)
1045 valid = 0;
1046 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001047 queue_dma(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 } /* else the irq handler advances the queue. */
1050
Alan Stern1f26e282006-11-16 10:16:00 -05001051 ep->responded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001053 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001055 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /* pci writes may still be posted */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001058 return ret;
1059
1060print_err:
1061 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1062 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001066dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1067 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001070 done(ep, req, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001073static void scan_dma_completions(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 /* only look at descriptors that were "naturally" retired,
1076 * so fifo and list head state won't matter
1077 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001078 while (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct net2280_request *req;
1080 u32 tmp;
1081
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001082 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct net2280_request, queue);
1084 if (!req->valid)
1085 break;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001086 rmb();
1087 tmp = le32_to_cpup(&req->td->dmacount);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001088 if ((tmp & BIT(VALID_BIT)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 break;
1090
1091 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1092 * cases where DMA must be aborted; this code handles
1093 * all non-abort DMA completions.
1094 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001095 if (unlikely(req->td->dmadesc == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* paranoia */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001097 tmp = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (tmp & DMA_BYTE_COUNT_MASK)
1099 break;
1100 /* single transfer mode */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001101 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 break;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02001103 } else if (!ep->is_in &&
Ricardo Ribalda Delgado43780aa2014-11-28 14:51:00 +01001104 (req->req.length % ep->ep.maxpacket) &&
1105 !(ep->dev->quirks & PLX_SUPERSPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Mario Schuknecht18a4e652014-11-16 21:21:45 +01001107 tmp = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* AVOID TROUBLE HERE by not issuing short reads from
1109 * your gadget driver. That helps avoids errata 0121,
1110 * 0122, and 0124; not all cases trigger the warning.
1111 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001112 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001113 ep_warn(ep->dev, "%s lost packet sync!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ep->ep.name);
1115 req->req.status = -EOVERFLOW;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001116 } else {
1117 tmp = readl(&ep->regs->ep_avail);
1118 if (tmp) {
1119 /* fifo gets flushed later */
1120 ep->out_overflow = 1;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001121 ep_dbg(ep->dev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001122 "%s dma, discard %d len %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 ep->ep.name, tmp,
1124 req->req.length);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001125 req->req.status = -EOVERFLOW;
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001129 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131}
1132
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001133static void restart_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 struct net2280_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 if (ep->stopped)
1138 return;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001139 req = list_entry(ep->queue.next, struct net2280_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01001141 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01001144static void abort_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 /* abort the current transfer */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001147 if (likely(!list_empty(&ep->queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* FIXME work around errata 0121, 0122, 0124 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001149 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001150 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001152 stop_dma(ep->dma);
1153 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156/* dequeue ALL requests */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001157static void nuke(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct net2280_request *req;
1160
1161 /* called with spinlock held */
1162 ep->stopped = 1;
1163 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001164 abort_dma(ep);
1165 while (!list_empty(&ep->queue)) {
1166 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct net2280_request,
1168 queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001169 done(ep, req, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171}
1172
1173/* dequeue JUST ONE request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001174static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 struct net2280_ep *ep;
1177 struct net2280_request *req;
1178 unsigned long flags;
1179 u32 dmactl;
1180 int stopped;
1181
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001182 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001183 if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1184 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1185 __func__, _ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001189 spin_lock_irqsave(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 stopped = ep->stopped;
1191
1192 /* quiesce dma while we patch the queue */
1193 dmactl = 0;
1194 ep->stopped = 1;
1195 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001196 dmactl = readl(&ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* WARNING erratum 0127 may kick in ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001198 stop_dma(ep->dma);
1199 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
1202 /* make sure it's still queued on this endpoint */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001203 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (&req->req == _req)
1205 break;
1206 }
1207 if (&req->req != _req) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001208 spin_unlock_irqrestore(&ep->dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001209 dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
1210 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return -EINVAL;
1212 }
1213
1214 /* queue head may be partially complete. */
1215 if (ep->queue.next == &req->queue) {
1216 if (ep->dma) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001217 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 _req->status = -ECONNRESET;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001219 abort_dma(ep);
1220 if (likely(ep->queue.next == &req->queue)) {
1221 /* NOTE: misreports single-transfer mode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 req->td->dmacount = 0; /* invalidate */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001223 dma_done(ep, req,
1224 readl(&ep->dma->dmacount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 -ECONNRESET);
1226 }
1227 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001228 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001229 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
1234 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001235 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ep->stopped = stopped;
1237
1238 if (ep->dma) {
1239 /* turn off dma on inactive queues */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001240 if (list_empty(&ep->queue))
1241 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 else if (!ep->stopped) {
1243 /* resume current request, or start new one */
1244 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001245 writel(dmactl, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001247 start_dma(ep, list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct net2280_request, queue));
1249 }
1250 }
1251
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001252 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return 0;
1254}
1255
1256/*-------------------------------------------------------------------------*/
1257
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001258static int net2280_fifo_status(struct usb_ep *_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260static int
Alan Stern80661342008-08-14 15:49:11 -04001261net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct net2280_ep *ep;
1264 unsigned long flags;
1265 int retval = 0;
1266
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001267 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001268 if (!_ep || (!ep->desc && ep->num != 0)) {
1269 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001271 }
1272 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1273 retval = -ESHUTDOWN;
1274 goto print_err;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001277 == USB_ENDPOINT_XFER_ISOC) {
1278 retval = -EINVAL;
1279 goto print_err;
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001282 spin_lock_irqsave(&ep->dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001283 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 retval = -EAGAIN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001285 goto print_unlock;
1286 } else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 retval = -EAGAIN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001288 goto print_unlock;
1289 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001290 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
Alan Stern80661342008-08-14 15:49:11 -04001291 value ? "set" : "clear",
1292 wedged ? "wedge" : "halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /* set/clear, then synch memory views with the device */
1294 if (value) {
1295 if (ep->num == 0)
1296 ep->dev->protocol_stall = 1;
1297 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001298 set_halt(ep);
Alan Stern80661342008-08-14 15:49:11 -04001299 if (wedged)
1300 ep->wedged = 1;
1301 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001302 clear_halt(ep);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001303 if (ep->dev->quirks & PLX_SUPERSPEED &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001304 !list_empty(&ep->queue) && ep->td_dma)
1305 restart_dma(ep);
Alan Stern80661342008-08-14 15:49:11 -04001306 ep->wedged = 0;
1307 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001308 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001310 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 return retval;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001313
1314print_unlock:
1315 spin_unlock_irqrestore(&ep->dev->lock, flags);
1316print_err:
1317 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1318 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001321static int net2280_set_halt(struct usb_ep *_ep, int value)
Alan Stern80661342008-08-14 15:49:11 -04001322{
1323 return net2280_set_halt_and_wedge(_ep, value, 0);
1324}
1325
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001326static int net2280_set_wedge(struct usb_ep *_ep)
Alan Stern80661342008-08-14 15:49:11 -04001327{
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001328 if (!_ep || _ep->name == ep0name) {
1329 pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
Alan Stern80661342008-08-14 15:49:11 -04001330 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001331 }
Alan Stern80661342008-08-14 15:49:11 -04001332 return net2280_set_halt_and_wedge(_ep, 1, 1);
1333}
1334
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001335static int net2280_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct net2280_ep *ep;
1338 u32 avail;
1339
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001340 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001341 if (!_ep || (!ep->desc && ep->num != 0)) {
1342 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -ENODEV;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001344 }
1345 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1346 dev_err(&ep->dev->pdev->dev,
1347 "%s: Invalid driver=%p or speed=%d\n",
1348 __func__, ep->dev->driver, ep->dev->gadget.speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return -ESHUTDOWN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001352 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001353 if (avail > ep->fifo_size) {
1354 dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EOVERFLOW;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (ep->is_in)
1358 avail = ep->fifo_size - avail;
1359 return avail;
1360}
1361
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001362static void net2280_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct net2280_ep *ep;
1365
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001366 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001367 if (!_ep || (!ep->desc && ep->num != 0)) {
1368 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001370 }
1371 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1372 dev_err(&ep->dev->pdev->dev,
1373 "%s: Invalid driver=%p or speed=%d\n",
1374 __func__, ep->dev->driver, ep->dev->gadget.speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001378 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001379 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
David Brownell901b3d72006-09-02 03:13:45 -07001382static const struct usb_ep_ops net2280_ep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 .enable = net2280_enable,
1384 .disable = net2280_disable,
1385
1386 .alloc_request = net2280_alloc_request,
1387 .free_request = net2280_free_request,
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 .queue = net2280_queue,
1390 .dequeue = net2280_dequeue,
1391
1392 .set_halt = net2280_set_halt,
Alan Stern80661342008-08-14 15:49:11 -04001393 .set_wedge = net2280_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 .fifo_status = net2280_fifo_status,
1395 .fifo_flush = net2280_fifo_flush,
1396};
1397
1398/*-------------------------------------------------------------------------*/
1399
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001400static int net2280_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct net2280 *dev;
1403 unsigned long flags;
1404 u16 retval;
1405
1406 if (!_gadget)
1407 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001408 dev = container_of(_gadget, struct net2280, gadget);
1409 spin_lock_irqsave(&dev->lock, flags);
1410 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1411 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return retval;
1413}
1414
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001415static int net2280_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct net2280 *dev;
1418 u32 tmp;
1419 unsigned long flags;
1420
1421 if (!_gadget)
1422 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001423 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001425 spin_lock_irqsave(&dev->lock, flags);
1426 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001427 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1428 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001429 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* pci writes may still be posted */
1432 return 0;
1433}
1434
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001435static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct net2280 *dev;
1438 u32 tmp;
1439 unsigned long flags;
1440
1441 if (!_gadget)
1442 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001443 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001445 spin_lock_irqsave(&dev->lock, flags);
1446 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001447 if (value) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001448 tmp |= BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001449 _gadget->is_selfpowered = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001450 } else {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001451 tmp &= ~BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001452 _gadget->is_selfpowered = 0;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001453 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001454 writel(tmp, &dev->usb->usbctl);
1455 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 return 0;
1458}
1459
1460static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1461{
1462 struct net2280 *dev;
1463 u32 tmp;
1464 unsigned long flags;
1465
1466 if (!_gadget)
1467 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001468 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001470 spin_lock_irqsave(&dev->lock, flags);
1471 tmp = readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 dev->softconnect = (is_on != 0);
1473 if (is_on)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001474 tmp |= BIT(USB_DETECT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001476 tmp &= ~BIT(USB_DETECT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001477 writel(tmp, &dev->usb->usbctl);
1478 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 return 0;
1481}
1482
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001483static int net2280_start(struct usb_gadget *_gadget,
1484 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001485static int net2280_stop(struct usb_gadget *_gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static const struct usb_gadget_ops net2280_ops = {
1488 .get_frame = net2280_get_frame,
1489 .wakeup = net2280_wakeup,
1490 .set_selfpowered = net2280_set_selfpowered,
1491 .pullup = net2280_pullup,
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001492 .udc_start = net2280_start,
1493 .udc_stop = net2280_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494};
1495
1496/*-------------------------------------------------------------------------*/
1497
Ricardo Ribalda Delgadob99b4062014-07-04 11:27:03 +02001498#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500/* FIXME move these into procfs, and use seq_file.
1501 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1502 * and also doesn't help products using this with 2.4 kernels.
1503 */
1504
1505/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001506static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1507 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001509 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001511 if (!dev->driver || !dev->driver->function ||
1512 strlen(dev->driver->function) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001514 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001516static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001518static ssize_t registers_show(struct device *_dev,
1519 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct net2280 *dev;
1522 char *next;
1523 unsigned size, t;
1524 unsigned long flags;
1525 int i;
1526 u32 t1, t2;
Andrew Morton30e69592005-06-26 17:18:46 -07001527 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001529 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 next = buf;
1531 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001532 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (dev->driver)
1535 s = dev->driver->driver.name;
1536 else
1537 s = "(none)";
1538
1539 /* Main Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001540 t = scnprintf(next, size, "%s version " DRIVER_VERSION
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001541 ", chiprev %04x\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 "devinit %03x fifoctl %08x gadget '%s'\n"
1543 "pci irqenb0 %02x irqenb1 %08x "
1544 "irqstat0 %04x irqstat1 %08x\n",
1545 driver_name, dev->chiprev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001546 readl(&dev->regs->devinit),
1547 readl(&dev->regs->fifoctl),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 s,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001549 readl(&dev->regs->pciirqenb0),
1550 readl(&dev->regs->pciirqenb1),
1551 readl(&dev->regs->irqstat0),
1552 readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 size -= t;
1554 next += t;
1555
1556 /* USB Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001557 t1 = readl(&dev->usb->usbctl);
1558 t2 = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001559 if (t1 & BIT(VBUS_PIN)) {
1560 if (t2 & BIT(HIGH_SPEED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 s = "high speed";
1562 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1563 s = "powered";
1564 else
1565 s = "full speed";
1566 /* full speed bit (6) not working?? */
1567 } else
1568 s = "not attached";
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001569 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 "stdrsp %08x usbctl %08x usbstat %08x "
1571 "addr 0x%02x (%s)\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001572 readl(&dev->usb->stdrsp), t1, t2,
1573 readl(&dev->usb->ouraddr), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 size -= t;
1575 next += t;
1576
1577 /* PCI Master Control Registers */
1578
1579 /* DMA Control Registers */
1580
1581 /* Configurable EP Control Registers */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001582 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct net2280_ep *ep;
1584
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001585 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (i && !ep->desc)
1587 continue;
1588
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001589 t1 = readl(&ep->cfg->ep_cfg);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001590 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1591 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1593 "irqenb %02x\n",
1594 ep->ep.name, t1, t2,
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001595 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 ? "NAK " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001597 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 ? "hide " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001599 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ? "CRC " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001601 (t2 & BIT(CLEAR_INTERRUPT_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 ? "interrupt " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001603 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 ? "status " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001605 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 ? "NAKmode " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001607 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 ? "DATA1 " : "DATA0 ",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001609 (t2 & BIT(CLEAR_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ? "HALT " : "",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001611 readl(&ep->regs->ep_irqenb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 size -= t;
1613 next += t;
1614
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001615 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 "\tstat %08x avail %04x "
1617 "(ep%d%s-%s)%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001618 readl(&ep->regs->ep_stat),
1619 readl(&ep->regs->ep_avail),
1620 t1 & 0x0f, DIR_STRING(t1),
1621 type_string(t1 >> 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 ep->stopped ? "*" : "");
1623 size -= t;
1624 next += t;
1625
1626 if (!ep->dma)
1627 continue;
1628
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001629 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 " dma\tctl %08x stat %08x count %08x\n"
1631 "\taddr %08x desc %08x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001632 readl(&ep->dma->dmactl),
1633 readl(&ep->dma->dmastat),
1634 readl(&ep->dma->dmacount),
1635 readl(&ep->dma->dmaaddr),
1636 readl(&ep->dma->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 size -= t;
1638 next += t;
1639
1640 }
1641
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001642 /* Indexed Registers (none yet) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /* Statistics */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001645 t = scnprintf(next, size, "\nirqs: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 size -= t;
1647 next += t;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001648 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct net2280_ep *ep;
1650
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001651 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (i && !ep->irqs)
1653 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001654 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 size -= t;
1656 next += t;
1657
1658 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001659 t = scnprintf(next, size, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 size -= t;
1661 next += t;
1662
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001663 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 return PAGE_SIZE - size;
1666}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001667static DEVICE_ATTR_RO(registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001669static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1670 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct net2280 *dev;
1673 char *next;
1674 unsigned size;
1675 unsigned long flags;
1676 int i;
1677
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001678 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 next = buf;
1680 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001681 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001683 for (i = 0; i < dev->n_ep; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001684 struct net2280_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 struct net2280_request *req;
1686 int t;
1687
1688 if (i != 0) {
1689 const struct usb_endpoint_descriptor *d;
1690
1691 d = ep->desc;
1692 if (!d)
1693 continue;
1694 t = d->bEndpointAddress;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001695 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1697 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1698 (t & USB_DIR_IN) ? "in" : "out",
Ricardo Ribalda Delgadoa27f37a2014-05-20 18:30:08 +02001699 type_string(d->bmAttributes),
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001700 usb_endpoint_maxp(d) & 0x1fff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 ep->dma ? "dma" : "pio", ep->fifo_size
1702 );
1703 } else /* ep0 should only have one transfer queued */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001704 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 ep->is_in ? "in" : "out");
1706 if (t <= 0 || t > size)
1707 goto done;
1708 size -= t;
1709 next += t;
1710
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001711 if (list_empty(&ep->queue)) {
1712 t = scnprintf(next, size, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (t <= 0 || t > size)
1714 goto done;
1715 size -= t;
1716 next += t;
1717 continue;
1718 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001719 list_for_each_entry(req, &ep->queue, queue) {
1720 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1721 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 "\treq %p len %d/%d "
1723 "buf %p (dmacount %08x)\n",
1724 &req->req, req->req.actual,
1725 req->req.length, req->req.buf,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001726 readl(&ep->dma->dmacount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001728 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 "\treq %p len %d/%d buf %p\n",
1730 &req->req, req->req.actual,
1731 req->req.length, req->req.buf);
1732 if (t <= 0 || t > size)
1733 goto done;
1734 size -= t;
1735 next += t;
1736
1737 if (ep->dma) {
1738 struct net2280_dma *td;
1739
1740 td = req->td;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001741 t = scnprintf(next, size, "\t td %08x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 " count %08x buf %08x desc %08x\n",
1743 (u32) req->td_dma,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001744 le32_to_cpu(td->dmacount),
1745 le32_to_cpu(td->dmaaddr),
1746 le32_to_cpu(td->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (t <= 0 || t > size)
1748 goto done;
1749 size -= t;
1750 next += t;
1751 }
1752 }
1753 }
1754
1755done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001756 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return PAGE_SIZE - size;
1758}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001759static DEVICE_ATTR_RO(queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761
1762#else
1763
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001764#define device_create_file(a, b) (0)
1765#define device_remove_file(a, b) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767#endif
1768
1769/*-------------------------------------------------------------------------*/
1770
1771/* another driver-specific mode might be a request type doing dma
1772 * to/from another device fifo instead of to/from memory.
1773 */
1774
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001775static void set_fifo_mode(struct net2280 *dev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 /* keeping high bits preserves BAR2 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001778 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001781 INIT_LIST_HEAD(&dev->gadget.ep_list);
1782 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1783 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 switch (mode) {
1785 case 0:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001786 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1787 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1788 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 break;
1790 case 1:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001791 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793 case 2:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001794 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1795 dev->ep[1].fifo_size = 2048;
1796 dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 break;
1798 }
1799 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001800 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1801 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001804static void defect7374_disable_data_eps(struct net2280 *dev)
1805{
1806 /*
1807 * For Defect 7374, disable data EPs (and more):
1808 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1809 * returing ep regs back to normal.
1810 */
1811 struct net2280_ep *ep;
1812 int i;
1813 unsigned char ep_sel;
1814 u32 tmp_reg;
1815
1816 for (i = 1; i < 5; i++) {
1817 ep = &dev->ep[i];
1818 writel(0, &ep->cfg->ep_cfg);
1819 }
1820
1821 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1822 for (i = 0; i < 6; i++)
1823 writel(0, &dev->dep[i].dep_cfg);
1824
1825 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1826 /* Select an endpoint for subsequent operations: */
1827 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1828 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1829
1830 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1831 ep_sel == 18 || ep_sel == 20)
1832 continue;
1833
1834 /* Change settings on some selected endpoints */
1835 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001836 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001837 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1838 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001839 tmp_reg |= BIT(EP_INITIALIZED);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001840 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1841 }
1842}
1843
1844static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1845{
1846 u32 tmp = 0, tmp_reg;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001847 u32 scratch;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001848 int i;
1849 unsigned char ep_sel;
1850
1851 scratch = get_idx_reg(dev->regs, SCRATCH);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001852
1853 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1854 == DEFECT7374_FSM_SS_CONTROL_READ);
1855
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001856 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1857
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001858 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1859 ep_warn(dev, "It will operate on cold-reboot and SS connect");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001860
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001861 /*GPEPs:*/
1862 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1863 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1864 ((dev->enhanced_mode) ?
Mian Yousaf Kaukab25d40ee2015-05-16 22:33:30 +02001865 BIT(OUT_ENDPOINT_ENABLE) | BIT(IN_ENDPOINT_ENABLE) :
1866 BIT(ENDPOINT_ENABLE)));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001867
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001868 for (i = 1; i < 5; i++)
1869 writel(tmp, &dev->ep[i].cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001870
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001871 /* CSRIN, PCIIN, STATIN, RCIN*/
1872 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1873 writel(tmp, &dev->dep[1].dep_cfg);
1874 writel(tmp, &dev->dep[3].dep_cfg);
1875 writel(tmp, &dev->dep[4].dep_cfg);
1876 writel(tmp, &dev->dep[5].dep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001877
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001878 /*Implemented for development and debug.
1879 * Can be refined/tuned later.*/
1880 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1881 /* Select an endpoint for subsequent operations: */
1882 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1883 writel(((tmp_reg & ~0x1f) | ep_sel),
1884 &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001885
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001886 if (ep_sel == 1) {
1887 tmp =
1888 (readl(&dev->plregs->pl_ep_ctrl) |
1889 BIT(CLEAR_ACK_ERROR_CODE) | 0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001890 writel(tmp, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001891 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001892 }
1893
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001894 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1895 ep_sel == 18 || ep_sel == 20)
1896 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001897
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001898 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1899 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1900 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1901
1902 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1903 ~BIT(EP_INITIALIZED);
1904 writel(tmp, &dev->plregs->pl_ep_ctrl);
1905
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001906 }
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001907
1908 /* Set FSM to focus on the first Control Read:
1909 * - Tip: Connection speed is known upon the first
1910 * setup request.*/
1911 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1912 set_idx_reg(dev->regs, SCRATCH, scratch);
1913
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916/* keeping it simple:
1917 * - one bus driver, initted first;
1918 * - one function driver, initted second
1919 *
1920 * most of the work to support multiple net2280 controllers would
1921 * be to associate this gadget driver (yes?) with all of them, or
1922 * perhaps to bind specific drivers to specific devices.
1923 */
1924
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001925static void usb_reset_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 u32 tmp;
1928
1929 dev->gadget.speed = USB_SPEED_UNKNOWN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001930 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001932 net2280_led_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /* disable automatic responses, and irqs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001935 writel(0, &dev->usb->stdrsp);
1936 writel(0, &dev->regs->pciirqenb0);
1937 writel(0, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* clear old dma and irq state */
1940 for (tmp = 0; tmp < 4; tmp++) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001941 struct net2280_ep *ep = &dev->ep[tmp + 1];
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001942 if (ep->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001943 abort_dma(ep);
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001944 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001945
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001946 writel(~0, &dev->regs->irqstat0),
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001947 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 /* reset, and enable pci */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001950 tmp = readl(&dev->regs->devinit) |
1951 BIT(PCI_ENABLE) |
1952 BIT(FIFO_SOFT_RESET) |
1953 BIT(USB_SOFT_RESET) |
1954 BIT(M8051_RESET);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001955 writel(tmp, &dev->regs->devinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 /* standard fifo and endpoint allocations */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001958 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001961static void usb_reset_338x(struct net2280 *dev)
1962{
1963 u32 tmp;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001964
1965 dev->gadget.speed = USB_SPEED_UNKNOWN;
1966 (void)readl(&dev->usb->usbctl);
1967
1968 net2280_led_init(dev);
1969
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001970 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001971 /* disable automatic responses, and irqs */
1972 writel(0, &dev->usb->stdrsp);
1973 writel(0, &dev->regs->pciirqenb0);
1974 writel(0, &dev->regs->pciirqenb1);
1975 }
1976
1977 /* clear old dma and irq state */
1978 for (tmp = 0; tmp < 4; tmp++) {
1979 struct net2280_ep *ep = &dev->ep[tmp + 1];
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02001980 struct net2280_dma_regs __iomem *dma;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001981
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02001982 if (ep->dma) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001983 abort_dma(ep);
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02001984 } else {
1985 dma = &dev->dma[tmp];
1986 writel(BIT(DMA_ABORT), &dma->dmastat);
1987 writel(0, &dma->dmactl);
1988 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001989 }
1990
1991 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
1992
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001993 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001994 /* reset, and enable pci */
1995 tmp = readl(&dev->regs->devinit) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001996 BIT(PCI_ENABLE) |
1997 BIT(FIFO_SOFT_RESET) |
1998 BIT(USB_SOFT_RESET) |
1999 BIT(M8051_RESET);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002000
2001 writel(tmp, &dev->regs->devinit);
2002 }
2003
2004 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2005 INIT_LIST_HEAD(&dev->gadget.ep_list);
2006
2007 for (tmp = 1; tmp < dev->n_ep; tmp++)
2008 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2009
2010}
2011
2012static void usb_reset(struct net2280 *dev)
2013{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002014 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002015 return usb_reset_228x(dev);
2016 return usb_reset_338x(dev);
2017}
2018
2019static void usb_reinit_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 /* basic endpoint init */
2024 for (tmp = 0; tmp < 7; tmp++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002025 struct net2280_ep *ep = &dev->ep[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002027 ep->ep.name = ep_name[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 ep->dev = dev;
2029 ep->num = tmp;
2030
2031 if (tmp > 0 && tmp <= 4) {
2032 ep->fifo_size = 1024;
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01002033 ep->dma = &dev->dma[tmp - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 } else
2035 ep->fifo_size = 64;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002036 ep->regs = &dev->epregs[tmp];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002037 ep->cfg = &dev->epregs[tmp];
2038 ep_reset_228x(dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002040 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
2041 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
2042 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002044 dev->gadget.ep0 = &dev->ep[0].ep;
2045 dev->ep[0].stopped = 0;
2046 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* we want to prevent lowlevel/insecure access from the USB host,
2049 * but erratum 0119 means this enable bit is ignored
2050 */
2051 for (tmp = 0; tmp < 5; tmp++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002052 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002055static void usb_reinit_338x(struct net2280 *dev)
2056{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002057 int i;
2058 u32 tmp, val;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002059 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2060 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2061 0x00, 0xC0, 0x00, 0xC0 };
2062
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002063 /* basic endpoint init */
2064 for (i = 0; i < dev->n_ep; i++) {
2065 struct net2280_ep *ep = &dev->ep[i];
2066
Mian Yousaf Kaukaba285f402015-02-02 10:55:23 +01002067 ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002068 ep->dev = dev;
2069 ep->num = i;
2070
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01002071 if (i > 0 && i <= 4)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002072 ep->dma = &dev->dma[i - 1];
2073
2074 if (dev->enhanced_mode) {
2075 ep->cfg = &dev->epregs[ne[i]];
2076 ep->regs = (struct net2280_ep_regs __iomem *)
Felipe Balbic43e97b2014-07-16 12:20:25 -05002077 (((void __iomem *)&dev->epregs[ne[i]]) +
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002078 ep_reg_addr[i]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002079 } else {
2080 ep->cfg = &dev->epregs[i];
2081 ep->regs = &dev->epregs[i];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002082 }
2083
2084 ep->fifo_size = (i != 0) ? 2048 : 512;
2085
2086 ep_reset_338x(dev->regs, ep);
2087 }
2088 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2089
2090 dev->gadget.ep0 = &dev->ep[0].ep;
2091 dev->ep[0].stopped = 0;
2092
2093 /* Link layer set up */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002094 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002095 tmp = readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002096 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002097 writel(tmp, &dev->usb_ext->usbctl2);
2098 }
2099
2100 /* Hardware Defect and Workaround */
2101 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2102 val &= ~(0xf << TIMER_LFPS_6US);
2103 val |= 0x5 << TIMER_LFPS_6US;
2104 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2105
2106 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2107 val &= ~(0xffff << TIMER_LFPS_80US);
2108 val |= 0x0100 << TIMER_LFPS_80US;
2109 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2110
2111 /*
2112 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2113 * Hot Reset Exit Handshake may Fail in Specific Case using
2114 * Default Register Settings. Workaround for Enumeration test.
2115 */
2116 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2117 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2118 val |= 0x10 << HOT_TX_NORESET_TS2;
2119 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2120
2121 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2122 val &= ~(0x1f << HOT_RX_RESET_TS2);
2123 val |= 0x3 << HOT_RX_RESET_TS2;
2124 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2125
2126 /*
2127 * Set Recovery Idle to Recover bit:
2128 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2129 * link robustness with various hosts and hubs.
2130 * - It is safe to set for all connection speeds; all chip revisions.
2131 * - R-M-W to leave other bits undisturbed.
2132 * - Reference PLX TT-7372
2133 */
2134 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002135 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002136 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2137
2138 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2139
2140 /* disable dedicated endpoints */
2141 writel(0x0D, &dev->dep[0].dep_cfg);
2142 writel(0x0D, &dev->dep[1].dep_cfg);
2143 writel(0x0E, &dev->dep[2].dep_cfg);
2144 writel(0x0E, &dev->dep[3].dep_cfg);
2145 writel(0x0F, &dev->dep[4].dep_cfg);
2146 writel(0x0C, &dev->dep[5].dep_cfg);
2147}
2148
2149static void usb_reinit(struct net2280 *dev)
2150{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002151 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002152 return usb_reinit_228x(dev);
2153 return usb_reinit_338x(dev);
2154}
2155
2156static void ep0_start_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002158 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2159 BIT(CLEAR_NAK_OUT_PACKETS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002160 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2161 &dev->epregs[0].ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 /*
2164 * hardware optionally handles a bunch of standard requests
2165 * that the API hides from drivers anyway. have it do so.
2166 * endpoint status/features are handled in software, to
2167 * help pass tests for some dubious behavior.
2168 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002169 writel(BIT(SET_TEST_MODE) |
2170 BIT(SET_ADDRESS) |
2171 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2172 BIT(GET_DEVICE_STATUS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002173 BIT(GET_INTERFACE_STATUS),
2174 &dev->usb->stdrsp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002175 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2176 BIT(SELF_POWERED_USB_DEVICE) |
2177 BIT(REMOTE_WAKEUP_SUPPORT) |
2178 (dev->softconnect << USB_DETECT_ENABLE) |
2179 BIT(SELF_POWERED_STATUS),
2180 &dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002183 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2184 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2185 &dev->regs->pciirqenb0);
2186 writel(BIT(PCI_INTERRUPT_ENABLE) |
2187 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2188 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2189 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2190 BIT(VBUS_INTERRUPT_ENABLE) |
2191 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2192 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2193 &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 /* don't leave any writes posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002196 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002199static void ep0_start_338x(struct net2280 *dev)
2200{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002201
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002202 if (dev->bug7734_patched)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002203 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2204 BIT(SET_EP_HIDE_STATUS_PHASE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002205 &dev->epregs[0].ep_rsp);
2206
2207 /*
2208 * hardware optionally handles a bunch of standard requests
2209 * that the API hides from drivers anyway. have it do so.
2210 * endpoint status/features are handled in software, to
2211 * help pass tests for some dubious behavior.
2212 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002213 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2214 BIT(SET_SEL) |
2215 BIT(SET_TEST_MODE) |
2216 BIT(SET_ADDRESS) |
2217 BIT(GET_INTERFACE_STATUS) |
2218 BIT(GET_DEVICE_STATUS),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002219 &dev->usb->stdrsp);
2220 dev->wakeup_enable = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002221 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002222 (dev->softconnect << USB_DETECT_ENABLE) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002223 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002224 &dev->usb->usbctl);
2225
2226 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002227 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002228 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2229 &dev->regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002230 writel(BIT(PCI_INTERRUPT_ENABLE) |
2231 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2232 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2233 BIT(VBUS_INTERRUPT_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002234 &dev->regs->pciirqenb1);
2235
2236 /* don't leave any writes posted */
2237 (void)readl(&dev->usb->usbctl);
2238}
2239
2240static void ep0_start(struct net2280 *dev)
2241{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002242 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002243 return ep0_start_228x(dev);
2244 return ep0_start_338x(dev);
2245}
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247/* when a driver is successfully registered, it will receive
2248 * control requests including set_configuration(), which enables
2249 * non-control requests. then usb traffic follows until a
2250 * disconnect is reported. then a host may connect again, or
2251 * the driver might get unbound.
2252 */
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002253static int net2280_start(struct usb_gadget *_gadget,
2254 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002256 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 int retval;
2258 unsigned i;
2259
2260 /* insist on high speed support from the driver, since
2261 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2262 * "must not be used in normal operation"
2263 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002264 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2265 !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return -EINVAL;
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002267
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002268 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002270 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002271 dev->ep[i].irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /* hook up the driver ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 driver->driver.bus = NULL;
2275 dev->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002277 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2278 if (retval)
2279 goto err_unbind;
2280 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2281 if (retval)
2282 goto err_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Ricardo Ribalda Delgado7a74c482014-11-28 14:50:47 +01002284 /* enable host detection and ep0; and we're ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * for set_configuration as well as eventual disconnect.
2286 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002287 net2280_led_active(dev, 1);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002288
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002289 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002290 defect7374_enable_data_eps_zero(dev);
2291
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002292 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /* pci writes may still be posted */
2295 return 0;
Jeff Garzikb3899da2006-10-11 21:50:24 -04002296
2297err_func:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002298 device_remove_file(&dev->pdev->dev, &dev_attr_function);
Jeff Garzikb3899da2006-10-11 21:50:24 -04002299err_unbind:
Jeff Garzikb3899da2006-10-11 21:50:24 -04002300 dev->driver = NULL;
2301 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002304static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
2306 int i;
2307
2308 /* don't disconnect if it's not connected */
2309 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2310 driver = NULL;
2311
2312 /* stop hardware; prevent new request submissions;
2313 * and kill any outstanding requests.
2314 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002315 usb_reset(dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002316 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002317 nuke(&dev->ep[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Felipe Balbi699412d2013-03-18 10:14:47 +02002319 /* report disconnect; the driver is already quiesced */
2320 if (driver) {
2321 spin_unlock(&dev->lock);
2322 driver->disconnect(&dev->gadget);
2323 spin_lock(&dev->lock);
2324 }
2325
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002326 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Felipe Balbi22835b82014-10-17 12:05:12 -05002329static int net2280_stop(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002331 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 unsigned long flags;
2333
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002334 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002336 spin_lock_irqsave(&dev->lock, flags);
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002337 stop_activity(dev, NULL);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002338 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002340 net2280_led_active(dev, 0);
Alan Stern2f076072013-01-30 16:40:14 -05002341
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002342 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2343 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002345 dev->driver = NULL;
Ricardo Ribalda Delgado84237bf2014-05-15 14:28:45 +02002346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return 0;
2348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350/*-------------------------------------------------------------------------*/
2351
2352/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2353 * also works for dma-capable endpoints, in pio mode or just
2354 * to manually advance the queue after short OUT transfers.
2355 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002356static void handle_ep_small(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 struct net2280_request *req;
2359 u32 t;
2360 /* 0 error, 1 mid-data, 2 done */
2361 int mode = 1;
2362
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002363 if (!list_empty(&ep->queue))
2364 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct net2280_request, queue);
2366 else
2367 req = NULL;
2368
2369 /* ack all, and handle what we care about */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002370 t = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 ep->irqs++;
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002372
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002373 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
Ricardo Ribalda Delgadofc12c682015-01-13 10:54:58 +01002374 ep->ep.name, t, req ? &req->req : NULL);
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002375
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002376 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002377 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002378 else
2379 /* Added for 2282 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002380 writel(t, &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 /* for ep0, monitor token irqs to catch data stage length errors
2383 * and to synchronize on status.
2384 *
2385 * also, to defer reporting of protocol stalls ... here's where
2386 * data or status first appears, handling stalls here should never
2387 * cause trouble on the host side..
2388 *
2389 * control requests could be slightly faster without token synch for
2390 * status, but status can jam up that way.
2391 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002392 if (unlikely(ep->num == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (ep->is_in) {
2394 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002395 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (ep->dev->protocol_stall) {
2397 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002398 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400 if (!req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002401 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 mode = 2;
2403 /* reply to extra IN data tokens with a zlp */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002404 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if (ep->dev->protocol_stall) {
2406 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002407 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 mode = 2;
Alan Stern1f26e282006-11-16 10:16:00 -05002409 } else if (ep->responded &&
2410 !req && !ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002411 write_fifo(ep, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413 } else {
2414 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002415 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (ep->dev->protocol_stall) {
2417 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002418 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420 mode = 2;
2421 /* an extra OUT token is an error */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002422 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2423 req &&
2424 req->req.actual == req->req.length) ||
2425 (ep->responded && !req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 ep->dev->protocol_stall = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002427 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 ep->stopped = 1;
2429 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002430 done(ep, req, -EOVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 req = NULL;
2432 }
2433 }
2434 }
2435
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002436 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return;
2438
2439 /* manual DMA queue advance after short OUT */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002440 if (likely(ep->dma)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002441 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 u32 count;
2443 int stopped = ep->stopped;
2444
2445 /* TRANSFERRED works around OUT_DONE erratum 0112.
2446 * we expect (N <= maxpacket) bytes; host wrote M.
2447 * iff (M < N) we won't ever see a DMA interrupt.
2448 */
2449 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002450 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 /* any preceding dma transfers must finish.
2453 * dma handles (M >= N), may empty the queue
2454 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002455 scan_dma_completions(ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002456 if (unlikely(list_empty(&ep->queue) ||
2457 ep->out_overflow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 req = NULL;
2459 break;
2460 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002461 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 struct net2280_request, queue);
2463
2464 /* here either (M < N), a "real" short rx;
2465 * or (M == N) and the queue didn't empty
2466 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002467 if (likely(t & BIT(FIFO_EMPTY))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002468 count = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 count &= DMA_BYTE_COUNT_MASK;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002470 if (readl(&ep->dma->dmadesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 != req->td_dma)
2472 req = NULL;
2473 break;
2474 }
2475 udelay(1);
2476 }
2477
2478 /* stop DMA, leave ep NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002479 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002480 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002482 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 req->td->dmacount = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002484 t = readl(&ep->regs->ep_avail);
2485 dma_done(ep, req, count,
David Brownell901b3d72006-09-02 03:13:45 -07002486 (ep->out_overflow || t)
2487 ? -EOVERFLOW : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489
2490 /* also flush to prevent erratum 0106 trouble */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002491 if (unlikely(ep->out_overflow ||
2492 (ep->dev->chiprev == 0x0100 &&
2493 ep->dev->gadget.speed
2494 == USB_SPEED_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002495 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 ep->out_overflow = 0;
2497 }
2498
2499 /* (re)start dma if needed, stop NAKing */
2500 ep->stopped = stopped;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002501 if (!list_empty(&ep->queue))
2502 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 } else
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002504 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 ep->ep.name, t);
2506 return;
2507
2508 /* data packet(s) received (in the fifo, OUT) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002509 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002510 if (read_fifo(ep, req) && ep->num != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 mode = 2;
2512
2513 /* data packet(s) transmitted (IN) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002514 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 unsigned len;
2516
2517 len = req->req.length - req->req.actual;
2518 if (len > ep->ep.maxpacket)
2519 len = ep->ep.maxpacket;
2520 req->req.actual += len;
2521
2522 /* if we wrote it all, we're usually done */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002523 /* send zlps until the status stage */
2524 if ((req->req.actual == req->req.length) &&
2525 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 mode = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 /* there was nothing to do ... */
2529 } else if (mode == 1)
2530 return;
2531
2532 /* done */
2533 if (mode == 2) {
2534 /* stream endpoints often resubmit/unlink in completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002535 done(ep, req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 /* maybe advance queue to next request */
2538 if (ep->num == 0) {
2539 /* NOTE: net2280 could let gadget driver start the
2540 * status stage later. since not all controllers let
2541 * them control that, the api doesn't (yet) allow it.
2542 */
2543 if (!ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002544 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 req = NULL;
2546 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002547 if (!list_empty(&ep->queue) && !ep->stopped)
2548 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 struct net2280_request, queue);
2550 else
2551 req = NULL;
2552 if (req && !ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002553 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555 }
2556
2557 /* is there a buffer for the next packet?
2558 * for best streaming performance, make sure there is one.
2559 */
2560 if (req && !ep->stopped) {
2561
2562 /* load IN fifo with next packet (may be zlp) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002563 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002564 write_fifo(ep, &req->req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
2566}
2567
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002568static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 struct net2280_ep *ep;
2571
2572 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002573 return &dev->ep[0];
2574 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 u8 bEndpointAddress;
2576
2577 if (!ep->desc)
2578 continue;
2579 bEndpointAddress = ep->desc->bEndpointAddress;
2580 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2581 continue;
2582 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2583 return ep;
2584 }
2585 return NULL;
2586}
2587
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002588static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2589{
2590 u32 scratch, fsmvalue;
2591 u32 ack_wait_timeout, state;
2592
2593 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2594 scratch = get_idx_reg(dev->regs, SCRATCH);
2595 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2596 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2597
2598 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2599 (r.bRequestType & USB_DIR_IN)))
2600 return;
2601
2602 /* This is the first Control Read for this connection: */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002603 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002604 /*
2605 * Connection is NOT SS:
2606 * - Connection must be FS or HS.
2607 * - This FSM state should allow workaround software to
2608 * run after the next USB connection.
2609 */
2610 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002611 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002612 goto restore_data_eps;
2613 }
2614
2615 /* Connection is SS: */
2616 for (ack_wait_timeout = 0;
2617 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2618 ack_wait_timeout++) {
2619
2620 state = readl(&dev->plregs->pl_ep_status_1)
2621 & (0xff << STATE);
2622 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2623 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2624 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002625 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002626 break;
2627 }
2628
2629 /*
2630 * We have not yet received host's Data Phase ACK
2631 * - Wait and try again.
2632 */
2633 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2634
2635 continue;
2636 }
2637
2638
2639 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002640 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002641 "to detect SS host's data phase ACK.");
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002642 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002643 "got 0x%2.2x.\n", state >> STATE);
2644 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002645 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002646 "%duSec for Control Read Data Phase ACK\n",
2647 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2648 }
2649
2650restore_data_eps:
2651 /*
2652 * Restore data EPs to their pre-workaround settings (disabled,
2653 * initialized, and other details).
2654 */
2655 defect7374_disable_data_eps(dev);
2656
2657 set_idx_reg(dev->regs, SCRATCH, scratch);
2658
2659 return;
2660}
2661
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002662static void ep_clear_seqnum(struct net2280_ep *ep)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002663{
2664 struct net2280 *dev = ep->dev;
2665 u32 val;
2666 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2667
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002668 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2669 val |= ep_pl[ep->num];
2670 writel(val, &dev->plregs->pl_ep_ctrl);
2671 val |= BIT(SEQUENCE_NUMBER_RESET);
2672 writel(val, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002673
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002674 return;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002675}
2676
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002677static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2678 struct net2280_ep *ep, struct usb_ctrlrequest r)
2679{
2680 int tmp = 0;
2681
2682#define w_value le16_to_cpu(r.wValue)
2683#define w_index le16_to_cpu(r.wIndex)
2684#define w_length le16_to_cpu(r.wLength)
2685
2686 switch (r.bRequest) {
2687 struct net2280_ep *e;
2688 u16 status;
2689
2690 case USB_REQ_SET_CONFIGURATION:
2691 dev->addressed_state = !w_value;
2692 goto usb3_delegate;
2693
2694 case USB_REQ_GET_STATUS:
2695 switch (r.bRequestType) {
2696 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2697 status = dev->wakeup_enable ? 0x02 : 0x00;
Peter Chenc8678d92015-01-28 16:32:35 +08002698 if (dev->gadget.is_selfpowered)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002699 status |= BIT(0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002700 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2701 dev->ltm_enable << 4);
2702 writel(0, &dev->epregs[0].ep_irqenb);
2703 set_fifo_bytecount(ep, sizeof(status));
2704 writel((__force u32) status, &dev->epregs[0].ep_data);
2705 allow_status_338x(ep);
2706 break;
2707
2708 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2709 e = get_ep_by_addr(dev, w_index);
2710 if (!e)
2711 goto do_stall3;
2712 status = readl(&e->regs->ep_rsp) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002713 BIT(CLEAR_ENDPOINT_HALT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002714 writel(0, &dev->epregs[0].ep_irqenb);
2715 set_fifo_bytecount(ep, sizeof(status));
2716 writel((__force u32) status, &dev->epregs[0].ep_data);
2717 allow_status_338x(ep);
2718 break;
2719
2720 default:
2721 goto usb3_delegate;
2722 }
2723 break;
2724
2725 case USB_REQ_CLEAR_FEATURE:
2726 switch (r.bRequestType) {
2727 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2728 if (!dev->addressed_state) {
2729 switch (w_value) {
2730 case USB_DEVICE_U1_ENABLE:
2731 dev->u1_enable = 0;
2732 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002733 ~BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002734 &dev->usb_ext->usbctl2);
2735 allow_status_338x(ep);
2736 goto next_endpoints3;
2737
2738 case USB_DEVICE_U2_ENABLE:
2739 dev->u2_enable = 0;
2740 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002741 ~BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002742 &dev->usb_ext->usbctl2);
2743 allow_status_338x(ep);
2744 goto next_endpoints3;
2745
2746 case USB_DEVICE_LTM_ENABLE:
2747 dev->ltm_enable = 0;
2748 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002749 ~BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002750 &dev->usb_ext->usbctl2);
2751 allow_status_338x(ep);
2752 goto next_endpoints3;
2753
2754 default:
2755 break;
2756 }
2757 }
2758 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2759 dev->wakeup_enable = 0;
2760 writel(readl(&dev->usb->usbctl) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002761 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002762 &dev->usb->usbctl);
2763 allow_status_338x(ep);
2764 break;
2765 }
2766 goto usb3_delegate;
2767
2768 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2769 e = get_ep_by_addr(dev, w_index);
2770 if (!e)
2771 goto do_stall3;
2772 if (w_value != USB_ENDPOINT_HALT)
2773 goto do_stall3;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002774 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002775 /*
2776 * Workaround for SS SeqNum not cleared via
2777 * Endpoint Halt (Clear) bit. select endpoint
2778 */
2779 ep_clear_seqnum(e);
2780 clear_halt(e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002781 if (!list_empty(&e->queue) && e->td_dma)
2782 restart_dma(e);
2783 allow_status(ep);
2784 ep->stopped = 1;
2785 break;
2786
2787 default:
2788 goto usb3_delegate;
2789 }
2790 break;
2791 case USB_REQ_SET_FEATURE:
2792 switch (r.bRequestType) {
2793 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2794 if (!dev->addressed_state) {
2795 switch (w_value) {
2796 case USB_DEVICE_U1_ENABLE:
2797 dev->u1_enable = 1;
2798 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002799 BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002800 &dev->usb_ext->usbctl2);
2801 allow_status_338x(ep);
2802 goto next_endpoints3;
2803
2804 case USB_DEVICE_U2_ENABLE:
2805 dev->u2_enable = 1;
2806 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002807 BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002808 &dev->usb_ext->usbctl2);
2809 allow_status_338x(ep);
2810 goto next_endpoints3;
2811
2812 case USB_DEVICE_LTM_ENABLE:
2813 dev->ltm_enable = 1;
2814 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002815 BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002816 &dev->usb_ext->usbctl2);
2817 allow_status_338x(ep);
2818 goto next_endpoints3;
2819 default:
2820 break;
2821 }
2822 }
2823
2824 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2825 dev->wakeup_enable = 1;
2826 writel(readl(&dev->usb->usbctl) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002827 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002828 &dev->usb->usbctl);
2829 allow_status_338x(ep);
2830 break;
2831 }
2832 goto usb3_delegate;
2833
2834 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2835 e = get_ep_by_addr(dev, w_index);
2836 if (!e || (w_value != USB_ENDPOINT_HALT))
2837 goto do_stall3;
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002838 ep->stopped = 1;
2839 if (ep->num == 0)
2840 ep->dev->protocol_stall = 1;
2841 else {
2842 if (ep->dma)
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01002843 abort_dma(ep);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002844 set_halt(ep);
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002845 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002846 allow_status_338x(ep);
2847 break;
2848
2849 default:
2850 goto usb3_delegate;
2851 }
2852
2853 break;
2854 default:
2855
2856usb3_delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002857 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002858 r.bRequestType, r.bRequest,
2859 w_value, w_index, w_length,
2860 readl(&ep->cfg->ep_cfg));
2861
2862 ep->responded = 0;
2863 spin_unlock(&dev->lock);
2864 tmp = dev->driver->setup(&dev->gadget, &r);
2865 spin_lock(&dev->lock);
2866 }
2867do_stall3:
2868 if (tmp < 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002869 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002870 r.bRequestType, r.bRequest, tmp);
2871 dev->protocol_stall = 1;
2872 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002873 set_halt(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002874 }
2875
2876next_endpoints3:
2877
2878#undef w_value
2879#undef w_index
2880#undef w_length
2881
2882 return;
2883}
2884
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002885static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 struct net2280_ep *ep;
2888 u32 num, scratch;
2889
2890 /* most of these don't need individual acks */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002891 stat &= ~BIT(INTA_ASSERTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (!stat)
2893 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002894 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 /* starting a control request? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002897 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 union {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002899 u32 raw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 struct usb_ctrlrequest r;
2901 } u;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002902 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 struct net2280_request *req;
2904
2905 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002906 u32 val = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002907 if (val & BIT(SUPER_SPEED)) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002908 dev->gadget.speed = USB_SPEED_SUPER;
2909 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2910 EP0_SS_MAX_PACKET_SIZE);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002911 } else if (val & BIT(HIGH_SPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 dev->gadget.speed = USB_SPEED_HIGH;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002913 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2914 EP0_HS_MAX_PACKET_SIZE);
2915 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 dev->gadget.speed = USB_SPEED_FULL;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002917 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2918 EP0_HS_MAX_PACKET_SIZE);
2919 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002920 net2280_led_speed(dev, dev->gadget.speed);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002921 ep_dbg(dev, "%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002922 usb_speed_string(dev->gadget.speed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002925 ep = &dev->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 ep->irqs++;
2927
2928 /* make sure any leftover request state is cleared */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002929 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002930 while (!list_empty(&ep->queue)) {
2931 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 struct net2280_request, queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002933 done(ep, req, (req->req.actual == req->req.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 ? 0 : -EPROTO);
2935 }
2936 ep->stopped = 0;
2937 dev->protocol_stall = 0;
Ricardo Ribalda Delgado5d1b6842014-11-28 14:50:53 +01002938 if (!(dev->quirks & PLX_SUPERSPEED)) {
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002939 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002940 tmp = BIT(FIFO_OVERFLOW) |
2941 BIT(FIFO_UNDERFLOW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002942 else
2943 tmp = 0;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002944
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002945 writel(tmp | BIT(TIMEOUT) |
2946 BIT(USB_STALL_SENT) |
2947 BIT(USB_IN_NAK_SENT) |
2948 BIT(USB_IN_ACK_RCVD) |
2949 BIT(USB_OUT_PING_NAK_SENT) |
2950 BIT(USB_OUT_ACK_SENT) |
2951 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
2952 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
2953 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2954 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2955 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002956 BIT(DATA_IN_TOKEN_INTERRUPT),
2957 &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002958 }
2959 u.raw[0] = readl(&dev->usb->setup0123);
2960 u.raw[1] = readl(&dev->usb->setup4567);
David Brownell901b3d72006-09-02 03:13:45 -07002961
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002962 cpu_to_le32s(&u.raw[0]);
2963 cpu_to_le32s(&u.raw[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002965 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002966 defect7374_workaround(dev, u.r);
2967
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002968 tmp = 0;
2969
David Brownell01ee7d72007-05-25 20:40:14 -07002970#define w_value le16_to_cpu(u.r.wValue)
2971#define w_index le16_to_cpu(u.r.wIndex)
2972#define w_length le16_to_cpu(u.r.wLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 /* ack the irq */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002975 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
2976 stat ^= BIT(SETUP_PACKET_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 /* watch control traffic at the token level, and force
2979 * synchronization before letting the status stage happen.
2980 * FIXME ignore tokens we'll NAK, until driver responds.
2981 * that'll mean a lot less irqs for some drivers.
2982 */
2983 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2984 if (ep->is_in) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002985 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2986 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2987 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002988 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 } else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002990 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2991 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2992 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002993 writel(scratch, &dev->epregs[0].ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 /* we made the hardware handle most lowlevel requests;
2996 * everything else goes uplevel to the gadget code.
2997 */
Alan Stern1f26e282006-11-16 10:16:00 -05002998 ep->responded = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002999
3000 if (dev->gadget.speed == USB_SPEED_SUPER) {
3001 handle_stat0_irqs_superspeed(dev, ep, u.r);
3002 goto next_endpoints;
3003 }
3004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 switch (u.r.bRequest) {
3006 case USB_REQ_GET_STATUS: {
3007 struct net2280_ep *e;
David Brownell320f3452005-05-07 13:05:18 -07003008 __le32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 /* hw handles device and interface status */
3011 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
3012 goto delegate;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003013 e = get_ep_by_addr(dev, w_index);
3014 if (!e || w_length > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 goto do_stall;
3016
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003017 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003018 status = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003020 status = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 /* don't bother with a request object! */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003023 writel(0, &dev->epregs[0].ep_irqenb);
3024 set_fifo_bytecount(ep, w_length);
3025 writel((__force u32)status, &dev->epregs[0].ep_data);
3026 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003027 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 goto next_endpoints;
3029 }
3030 break;
3031 case USB_REQ_CLEAR_FEATURE: {
3032 struct net2280_ep *e;
3033
3034 /* hw handles device features */
3035 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3036 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003037 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003039 e = get_ep_by_addr(dev, w_index);
3040 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003042 if (e->wedged) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003043 ep_vdbg(dev, "%s wedged, halt not cleared\n",
Alan Stern80661342008-08-14 15:49:11 -04003044 ep->ep.name);
3045 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003046 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Alan Stern80661342008-08-14 15:49:11 -04003047 clear_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003048 if ((ep->dev->quirks & PLX_SUPERSPEED) &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003049 !list_empty(&e->queue) && e->td_dma)
3050 restart_dma(e);
Alan Stern80661342008-08-14 15:49:11 -04003051 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003052 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 goto next_endpoints;
3054 }
3055 break;
3056 case USB_REQ_SET_FEATURE: {
3057 struct net2280_ep *e;
3058
3059 /* hw handles device features */
3060 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3061 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003062 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003064 e = get_ep_by_addr(dev, w_index);
3065 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003067 if (e->ep.name == ep0name)
3068 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003069 set_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003070 if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003071 abort_dma(e);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003072 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003073 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 goto next_endpoints;
3075 }
3076 break;
3077 default:
3078delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003079 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 "ep_cfg %08x\n",
3081 u.r.bRequestType, u.r.bRequest,
David Brownell320f3452005-05-07 13:05:18 -07003082 w_value, w_index, w_length,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003083 readl(&ep->cfg->ep_cfg));
Alan Stern1f26e282006-11-16 10:16:00 -05003084 ep->responded = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003085 spin_unlock(&dev->lock);
3086 tmp = dev->driver->setup(&dev->gadget, &u.r);
3087 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
3089
3090 /* stall ep0 on error */
3091 if (tmp < 0) {
3092do_stall:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003093 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 u.r.bRequestType, u.r.bRequest, tmp);
3095 dev->protocol_stall = 1;
3096 }
3097
3098 /* some in/out token irq should follow; maybe stall then.
3099 * driver must queue a request (even zlp) or halt ep0
3100 * before the host times out.
3101 */
3102 }
3103
David Brownell320f3452005-05-07 13:05:18 -07003104#undef w_value
3105#undef w_index
3106#undef w_length
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108next_endpoints:
3109 /* endpoint data irq ? */
3110 scratch = stat & 0x7f;
3111 stat &= ~0x7f;
3112 for (num = 0; scratch; num++) {
3113 u32 t;
3114
3115 /* do this endpoint's FIFO and queue need tending? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003116 t = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if ((scratch & t) == 0)
3118 continue;
3119 scratch ^= t;
3120
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003121 ep = &dev->ep[num];
3122 handle_ep_small(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 }
3124
3125 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003126 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003129#define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3130 BIT(DMA_C_INTERRUPT) | \
3131 BIT(DMA_B_INTERRUPT) | \
3132 BIT(DMA_A_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133#define PCI_ERROR_INTERRUPTS ( \
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003134 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3135 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3136 BIT(PCI_RETRY_ABORT_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003138static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
Felipe Balbiccf5fb62015-02-27 09:44:51 -06003139__releases(dev->lock)
3140__acquires(dev->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
3142 struct net2280_ep *ep;
3143 u32 tmp, num, mask, scratch;
3144
3145 /* after disconnect there's nothing else to do! */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003146 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3147 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03003150 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
David Brownell901b3d72006-09-02 03:13:45 -07003151 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 * only indicates a change in the reset state).
3153 */
3154 if (stat & tmp) {
Alan Sternb611e422014-11-06 14:27:56 +08003155 bool reset = false;
3156 bool disconnect = false;
3157
3158 /*
3159 * Ignore disconnects and resets if the speed hasn't been set.
3160 * VBUS can bounce and there's always an initial reset.
3161 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003162 writel(tmp, &dev->regs->irqstat1);
Alan Sternb611e422014-11-06 14:27:56 +08003163 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3164 if ((stat & BIT(VBUS_INTERRUPT)) &&
3165 (readl(&dev->usb->usbctl) &
3166 BIT(VBUS_PIN)) == 0) {
3167 disconnect = true;
3168 ep_dbg(dev, "disconnect %s\n",
3169 dev->driver->driver.name);
3170 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3171 (readl(&dev->usb->usbstat) & mask)
3172 == 0) {
3173 reset = true;
3174 ep_dbg(dev, "reset %s\n",
3175 dev->driver->driver.name);
3176 }
3177
3178 if (disconnect || reset) {
3179 stop_activity(dev, dev->driver);
3180 ep0_start(dev);
3181 spin_unlock(&dev->lock);
3182 if (reset)
3183 usb_gadget_udc_reset
3184 (&dev->gadget, dev->driver);
3185 else
3186 (dev->driver->disconnect)
3187 (&dev->gadget);
3188 spin_lock(&dev->lock);
3189 return;
3190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192 stat &= ~tmp;
3193
3194 /* vBUS can bounce ... one of many reasons to ignore the
3195 * notion of hotplug events on bus connect/disconnect!
3196 */
3197 if (!stat)
3198 return;
3199 }
3200
3201 /* NOTE: chip stays in PCI D0 state for now, but it could
3202 * enter D1 to save more power
3203 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003204 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (stat & tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003206 writel(tmp, &dev->regs->irqstat1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003207 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (dev->driver->suspend)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003209 dev->driver->suspend(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 if (!enable_suspend)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003211 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 } else {
3213 if (dev->driver->resume)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003214 dev->driver->resume(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 /* at high speed, note erratum 0133 */
3216 }
3217 stat &= ~tmp;
3218 }
3219
3220 /* clear any other status/irqs */
3221 if (stat)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003222 writel(stat, &dev->regs->irqstat1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 /* some status we can just ignore */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003225 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003226 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3227 BIT(SUSPEND_REQUEST_INTERRUPT) |
3228 BIT(RESUME_INTERRUPT) |
3229 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003230 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003231 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3232 BIT(RESUME_INTERRUPT) |
3233 BIT(SOF_DOWN_INTERRUPT) |
3234 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (!stat)
3237 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003238 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
3240 /* DMA status, for ep-{a,b,c,d} */
3241 scratch = stat & DMA_INTERRUPTS;
3242 stat &= ~DMA_INTERRUPTS;
3243 scratch >>= 9;
3244 for (num = 0; scratch; num++) {
3245 struct net2280_dma_regs __iomem *dma;
3246
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003247 tmp = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if ((tmp & scratch) == 0)
3249 continue;
3250 scratch ^= tmp;
3251
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003252 ep = &dev->ep[num + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 dma = ep->dma;
3254
3255 if (!dma)
3256 continue;
3257
3258 /* clear ep's dma status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003259 tmp = readl(&dma->dmastat);
3260 writel(tmp, &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003262 /* dma sync*/
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003263 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003264 u32 r_dmacount = readl(&dma->dmacount);
3265 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003266 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003267 continue;
3268 }
3269
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003270 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3271 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3272 ep->ep.name, tmp);
3273 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 }
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003275 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 /* OUT transfers terminate when the data from the
3278 * host is in our memory. Process whatever's done.
3279 * On this path, we know transfer's last packet wasn't
3280 * less than req->length. NAK_OUT_PACKETS may be set,
3281 * or the FIFO may already be holding new packets.
3282 *
3283 * IN transfers can linger in the FIFO for a very
3284 * long time ... we ignore that for now, accounting
3285 * precisely (like PIO does) needs per-packet irqs
3286 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003287 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 /* disable dma on inactive queues; else maybe restart */
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003290 if (!list_empty(&ep->queue)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003291 tmp = readl(&dma->dmactl);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003292 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
3294 ep->irqs++;
3295 }
3296
3297 /* NOTE: there are other PCI errors we might usefully notice.
3298 * if they appear very often, here's where to try recovering.
3299 */
3300 if (stat & PCI_ERROR_INTERRUPTS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003301 ep_err(dev, "pci dma error; stat %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 stat &= ~PCI_ERROR_INTERRUPTS;
3303 /* these are fatal errors, but "maybe" they won't
3304 * happen again ...
3305 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003306 stop_activity(dev, dev->driver);
3307 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 stat = 0;
3309 }
3310
3311 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003312 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003315static irqreturn_t net2280_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
3317 struct net2280 *dev = _dev;
3318
Alan Stern658ad5e2006-04-14 16:44:11 -04003319 /* shared interrupt, not ours */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003320 if ((dev->quirks & PLX_LEGACY) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003321 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
Alan Stern658ad5e2006-04-14 16:44:11 -04003322 return IRQ_NONE;
3323
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003324 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 /* handle disconnect, dma, and more */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003327 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329 /* control requests and PIO */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003330 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003332 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003333 /* re-enable interrupt to trigger any possible new interrupt */
3334 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3335 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3336 writel(pciirqenb1, &dev->regs->pciirqenb1);
3337 }
3338
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003339 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 return IRQ_HANDLED;
3342}
3343
3344/*-------------------------------------------------------------------------*/
3345
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003346static void gadget_release(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003348 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003350 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
3353/* tear down the binding between this driver and the pci device */
3354
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003355static void net2280_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003357 struct net2280 *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003359 usb_del_gadget_udc(&dev->gadget);
3360
David Brownell6bea4762006-12-05 03:15:33 -08003361 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 /* then clean up the resources we allocated during probe() */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003364 net2280_led_shutdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 if (dev->requests) {
3366 int i;
3367 for (i = 1; i < 5; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003368 if (!dev->ep[i].dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003370 pci_pool_free(dev->requests, dev->ep[i].dummy,
3371 dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003373 pci_pool_destroy(dev->requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
3375 if (dev->got_irq)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003376 free_irq(pdev->irq, dev);
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003377 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003378 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (dev->regs)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003380 iounmap(dev->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (dev->region)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003382 release_mem_region(pci_resource_start(pdev, 0),
3383 pci_resource_len(pdev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 if (dev->enabled)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003385 pci_disable_device(pdev);
3386 device_remove_file(&pdev->dev, &dev_attr_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003388 ep_info(dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
3391/* wrap this driver around the specified device, but
3392 * don't respond over USB until a gadget driver binds to us.
3393 */
3394
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003395static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
3397 struct net2280 *dev;
3398 unsigned long resource, len;
3399 void __iomem *base = NULL;
3400 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 /* alloc, and start init */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003403 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3404 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 retval = -ENOMEM;
3406 goto done;
3407 }
3408
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003409 pci_set_drvdata(pdev, dev);
3410 spin_lock_init(&dev->lock);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003411 dev->quirks = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 dev->pdev = pdev;
3413 dev->gadget.ops = &net2280_ops;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003414 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003415 USB_SPEED_SUPER : USB_SPEED_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417 /* the "gadget" abstracts/virtualizes the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 dev->gadget.name = driver_name;
3419
3420 /* now all the pci goodies ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003421 if (pci_enable_device(pdev) < 0) {
3422 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 goto done;
3424 }
3425 dev->enabled = 1;
3426
3427 /* BAR 0 holds all the registers
3428 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3429 * BAR 2 is fifo memory; unused here
3430 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003431 resource = pci_resource_start(pdev, 0);
3432 len = pci_resource_len(pdev, 0);
3433 if (!request_mem_region(resource, len, driver_name)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003434 ep_dbg(dev, "controller already in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 retval = -EBUSY;
3436 goto done;
3437 }
3438 dev->region = 1;
3439
David Brownell901b3d72006-09-02 03:13:45 -07003440 /* FIXME provide firmware download interface to put
3441 * 8051 code into the chip, e.g. to turn on PCI PM.
3442 */
3443
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003444 base = ioremap_nocache(resource, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 if (base == NULL) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003446 ep_dbg(dev, "can't map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 retval = -EFAULT;
3448 goto done;
3449 }
3450 dev->regs = (struct net2280_regs __iomem *) base;
3451 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3452 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3453 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3454 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3455 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3456
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003457 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003458 u32 fsmvalue;
3459 u32 usbstat;
3460 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3461 (base + 0x00b4);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003462 dev->llregs = (struct usb338x_ll_regs __iomem *)
3463 (base + 0x0700);
3464 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3465 (base + 0x0748);
3466 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3467 (base + 0x077c);
3468 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3469 (base + 0x079c);
3470 dev->plregs = (struct usb338x_pl_regs __iomem *)
3471 (base + 0x0800);
3472 usbstat = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003473 dev->enhanced_mode = !!(usbstat & BIT(11));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003474 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3475 /* put into initial config, link up all endpoints */
3476 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3477 (0xf << DEFECT7374_FSM_FIELD);
3478 /* See if firmware needs to set up for workaround: */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003479 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3480 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003481 writel(0, &dev->usb->usbctl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003482 } else
3483 dev->bug7734_patched = 0;
3484 } else {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003485 dev->enhanced_mode = 0;
3486 dev->n_ep = 7;
3487 /* put into initial config, link up all endpoints */
3488 writel(0, &dev->usb->usbctl);
3489 }
3490
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003491 usb_reset(dev);
3492 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494 /* irq setup after old hardware is cleaned up */
3495 if (!pdev->irq) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003496 ep_err(dev, "No IRQ. Check PCI setup!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 retval = -ENODEV;
3498 goto done;
3499 }
David S. Millerc6387a42006-06-20 01:21:29 -07003500
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003501 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003502 if (pci_enable_msi(pdev))
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003503 ep_err(dev, "Failed to enable MSI mode\n");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003504
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003505 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3506 driver_name, dev)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003507 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 retval = -EBUSY;
3509 goto done;
3510 }
3511 dev->got_irq = 1;
3512
3513 /* DMA setup */
3514 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003515 dev->requests = pci_pool_create("requests", pdev,
3516 sizeof(struct net2280_dma),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 0 /* no alignment requirements */,
3518 0 /* or page-crossing issues */);
3519 if (!dev->requests) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003520 ep_dbg(dev, "can't get request pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 retval = -ENOMEM;
3522 goto done;
3523 }
3524 for (i = 1; i < 5; i++) {
3525 struct net2280_dma *td;
3526
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003527 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3528 &dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 if (!td) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003530 ep_dbg(dev, "can't get dummy %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 retval = -ENOMEM;
3532 goto done;
3533 }
3534 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 td->dmadesc = td->dmaaddr;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003536 dev->ep[i].dummy = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
3538
3539 /* enable lower-overhead pci memory bursts during DMA */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003540 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003541 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3542 /*
3543 * 256 write retries may not be enough...
3544 BIT(PCI_RETRY_ABORT_ENABLE) |
3545 */
3546 BIT(DMA_READ_MULTIPLE_ENABLE) |
3547 BIT(DMA_READ_LINE_ENABLE),
3548 &dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003550 pci_set_master(pdev);
3551 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 /* ... also flushes any posted pci writes */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003554 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 /* done */
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003557 ep_info(dev, "%s\n", driver_desc);
3558 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
David S. Millerc6387a42006-06-20 01:21:29 -07003559 pdev->irq, base, dev->chiprev);
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01003560 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003561 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003562 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3563 if (retval)
3564 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Felipe Balbi2901df62013-02-26 15:15:27 +02003566 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3567 gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003568 if (retval)
3569 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return 0;
3571
3572done:
3573 if (dev)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003574 net2280_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return retval;
3576}
3577
Alan Stern2d61bde2006-05-05 16:23:42 -04003578/* make sure the board is quiescent; otherwise it will continue
3579 * generating IRQs across the upcoming reboot.
3580 */
3581
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003582static void net2280_shutdown(struct pci_dev *pdev)
Alan Stern2d61bde2006-05-05 16:23:42 -04003583{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003584 struct net2280 *dev = pci_get_drvdata(pdev);
Alan Stern2d61bde2006-05-05 16:23:42 -04003585
3586 /* disable IRQs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003587 writel(0, &dev->regs->pciirqenb0);
3588 writel(0, &dev->regs->pciirqenb1);
Alan Stern2d61bde2006-05-05 16:23:42 -04003589
3590 /* disable the pullup so the host will think we're gone */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003591 writel(0, &dev->usb->usbctl);
Alan Stern2f076072013-01-30 16:40:14 -05003592
Alan Stern2d61bde2006-05-05 16:23:42 -04003593}
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596/*-------------------------------------------------------------------------*/
3597
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003598static const struct pci_device_id pci_ids[] = { {
David Brownell901b3d72006-09-02 03:13:45 -07003599 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3600 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003601 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 .device = 0x2280,
3603 .subvendor = PCI_ANY_ID,
3604 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003605 .driver_data = PLX_LEGACY | PLX_2280,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003606 }, {
David Brownell901b3d72006-09-02 03:13:45 -07003607 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3608 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003609 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003610 .device = 0x2282,
3611 .subvendor = PCI_ANY_ID,
3612 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003613 .driver_data = PLX_LEGACY,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003614 },
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003615 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003616 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3617 .class_mask = ~0,
3618 .vendor = PCI_VENDOR_ID_PLX,
3619 .device = 0x3380,
3620 .subvendor = PCI_ANY_ID,
3621 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003622 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003623 },
3624 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003625 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3626 .class_mask = ~0,
3627 .vendor = PCI_VENDOR_ID_PLX,
3628 .device = 0x3382,
3629 .subvendor = PCI_ANY_ID,
3630 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003631 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003632 },
3633{ /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634};
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003635MODULE_DEVICE_TABLE(pci, pci_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637/* pci driver glue; this is a "new style" PCI driver module */
3638static struct pci_driver net2280_pci_driver = {
3639 .name = (char *) driver_name,
3640 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642 .probe = net2280_probe,
3643 .remove = net2280_remove,
Alan Stern2d61bde2006-05-05 16:23:42 -04003644 .shutdown = net2280_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646 /* FIXME add power management support */
3647};
3648
Ricardo Ribalda Delgado9a028e42014-05-20 18:30:07 +02003649module_pci_driver(net2280_pci_driver);
3650
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003651MODULE_DESCRIPTION(DRIVER_DESC);
3652MODULE_AUTHOR("David Brownell");
3653MODULE_LICENSE("GPL");