blob: 2bee912ca65b1545ff32cd9933fe266f5103f779 [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);
Mian Yousaf Kaukab11bece52015-05-16 22:33:39 +0200127static void stop_activity(struct net2280 *dev,
128 struct usb_gadget_driver *driver);
129static void ep0_start(struct net2280 *dev);
Mian Yousaf Kaukabe6ac4bb2015-05-16 22:33:31 +0200130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*-------------------------------------------------------------------------*/
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200132static inline void enable_pciirqenb(struct net2280_ep *ep)
133{
134 u32 tmp = readl(&ep->dev->regs->pciirqenb0);
135
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200136 if (ep->dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200137 tmp |= BIT(ep->num);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200138 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200139 tmp |= BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200140 writel(tmp, &ep->dev->regs->pciirqenb0);
141
142 return;
143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200146net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct net2280 *dev;
149 struct net2280_ep *ep;
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200150 u32 max;
151 u32 tmp = 0;
152 u32 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 unsigned long flags;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200154 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100155 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200157 ep = container_of(_ep, struct net2280_ep, ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200158 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100159 desc->bDescriptorType != USB_DT_ENDPOINT) {
160 pr_err("%s: failed at line=%d\n", __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 dev = ep->dev;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100164 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
165 ret = -ESHUTDOWN;
166 goto print_err;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* erratum 0119 workaround ties up an endpoint number */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100170 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) {
171 ret = -EDOM;
172 goto print_err;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200175 if (dev->quirks & PLX_SUPERSPEED) {
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100176 if ((desc->bEndpointAddress & 0x0f) >= 0x0c) {
177 ret = -EDOM;
178 goto print_err;
179 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200180 ep->is_in = !!usb_endpoint_dir_in(desc);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100181 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) {
182 ret = -EINVAL;
183 goto print_err;
184 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200185 }
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* sanity check ep-e/ep-f since their fifos are small */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200188 max = usb_endpoint_maxp(desc) & 0x1fff;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100189 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) {
190 ret = -ERANGE;
191 goto print_err;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200194 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 _ep->maxpacket = max & 0x7ff;
196 ep->desc = desc;
197
198 /* ep_reset() has already been called */
199 ep->stopped = 0;
Alan Stern80661342008-08-14 15:49:11 -0400200 ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 ep->out_overflow = 0;
202
203 /* set speed-dependent max packet; may kick in high bandwidth */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200204 set_max_speed(ep, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* set type, direction, address; reset fifo counters */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200207 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200208
209 if ((dev->quirks & PLX_SUPERSPEED) && dev->enhanced_mode) {
210 tmp = readl(&ep->cfg->ep_cfg);
211 /* If USB ep number doesn't match hardware ep number */
212 if ((tmp & 0xf) != usb_endpoint_num(desc)) {
213 ret = -EINVAL;
214 spin_unlock_irqrestore(&dev->lock, flags);
215 goto print_err;
216 }
217 if (ep->is_in)
218 tmp &= ~USB3380_EP_CFG_MASK_IN;
219 else
220 tmp &= ~USB3380_EP_CFG_MASK_OUT;
221 }
222 type = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
223 if (type == USB_ENDPOINT_XFER_INT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* erratum 0105 workaround prevents hs NYET */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200225 if (dev->chiprev == 0100 &&
226 dev->gadget.speed == USB_SPEED_HIGH &&
227 !(desc->bEndpointAddress & USB_DIR_IN))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200228 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 &ep->regs->ep_rsp);
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200230 } else if (type == USB_ENDPOINT_XFER_BULK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* catch some particularly blatant driver bugs */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200232 if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
233 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
234 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
235 spin_unlock_irqrestore(&dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100236 ret = -ERANGE;
237 goto print_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 }
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200240 ep->is_iso = (type == USB_ENDPOINT_XFER_ISOC);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200241 /* Enable this endpoint */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200242 if (dev->quirks & PLX_LEGACY) {
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200243 tmp |= type << ENDPOINT_TYPE;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200244 tmp |= desc->bEndpointAddress;
245 /* default full fifo lines */
246 tmp |= (4 << ENDPOINT_BYTE_COUNT);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200247 tmp |= BIT(ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200248 ep->is_in = (tmp & USB_DIR_IN) != 0;
249 } else {
250 /* In Legacy mode, only OUT endpoints are used */
251 if (dev->enhanced_mode && ep->is_in) {
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200252 tmp |= type << IN_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200253 tmp |= BIT(IN_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200254 } else {
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200255 tmp |= type << OUT_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200256 tmp |= BIT(OUT_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200257 tmp |= (ep->is_in << ENDPOINT_DIRECTION);
258 }
259
Mian Yousaf Kaukab463e1042015-05-16 22:33:34 +0200260 tmp |= (4 << ENDPOINT_BYTE_COUNT);
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200261 if (!dev->enhanced_mode)
262 tmp |= usb_endpoint_num(desc);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200263 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
264 }
265
266 /* Make sure all the registers are written before ep_rsp*/
267 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* for OUT transfers, block the rx fifo until a read is posted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200271 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200272 else if (!(dev->quirks & PLX_2280)) {
David Brownell901b3d72006-09-02 03:13:45 -0700273 /* Added for 2282, Don't use nak packets on an in endpoint,
274 * this was ignored on 2280
275 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200276 writel(BIT(CLEAR_NAK_OUT_PACKETS) |
277 BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Mian Yousaf Kaukabe6ac4bb2015-05-16 22:33:31 +0200280 if (dev->quirks & PLX_SUPERSPEED)
281 ep_clear_seqnum(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200282 writel(tmp, &ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* enable irqs */
285 if (!ep->dma) { /* pio, per-packet */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200286 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200288 tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
289 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200290 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200291 tmp |= readl(&ep->regs->ep_irqenb);
292 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 } else { /* dma, per-request */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200294 tmp = BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200295 tmp |= readl(&dev->regs->pciirqenb1);
296 writel(tmp, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /* for short OUT transfers, dma completions can't
299 * advance the queue; do it pio-style, by hand.
300 * NOTE erratum 0112 workaround #2
301 */
302 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200303 tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200304 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200306 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 }
309
310 tmp = desc->bEndpointAddress;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200311 ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200312 _ep->name, tmp & 0x0f, DIR_STRING(tmp),
313 type_string(desc->bmAttributes),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ep->dma ? "dma" : "pio", max);
315
316 /* pci writes may still be posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200317 spin_unlock_irqrestore(&dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100318 return ret;
319
320print_err:
321 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
322 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200325static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 u32 result;
328
329 do {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200330 result = readl(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (result == ~(u32)0) /* "device unplugged" */
332 return -ENODEV;
333 result &= mask;
334 if (result == done)
335 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200336 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 usec--;
338 } while (usec > 0);
339 return -ETIMEDOUT;
340}
341
David Brownell901b3d72006-09-02 03:13:45 -0700342static const struct usb_ep_ops net2280_ep_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200344static void ep_reset_228x(struct net2280_regs __iomem *regs,
345 struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 u32 tmp;
348
349 ep->desc = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200350 INIT_LIST_HEAD(&ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Robert Baldygae117e742013-12-13 12:23:38 +0100352 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ep->ep.ops = &net2280_ep_ops;
354
355 /* disable the dma, irqs, endpoint... */
356 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200357 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200358 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
359 BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
360 BIT(DMA_ABORT),
361 &ep->dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200363 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200364 tmp &= ~BIT(ep->num);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200365 writel(tmp, &regs->pciirqenb0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200367 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200368 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200369 writel(tmp, &regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200371 writel(0, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /* init to our chosen defaults, notably so that we NAK OUT
374 * packets until the driver queues a read (+note erratum 0112)
375 */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200376 if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200377 tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
378 BIT(SET_NAK_OUT_PACKETS) |
379 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
380 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100381 } else {
382 /* added for 2282 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200383 tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
384 BIT(CLEAR_NAK_OUT_PACKETS) |
385 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
386 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (ep->num != 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200390 tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
391 BIT(CLEAR_ENDPOINT_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200393 writel(tmp, &ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* scrub most status bits, and flush any fifo state */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200396 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200397 tmp = BIT(FIFO_OVERFLOW) |
398 BIT(FIFO_UNDERFLOW);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100399 else
400 tmp = 0;
401
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200402 writel(tmp | BIT(TIMEOUT) |
403 BIT(USB_STALL_SENT) |
404 BIT(USB_IN_NAK_SENT) |
405 BIT(USB_IN_ACK_RCVD) |
406 BIT(USB_OUT_PING_NAK_SENT) |
407 BIT(USB_OUT_ACK_SENT) |
408 BIT(FIFO_FLUSH) |
409 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
410 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
411 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
412 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
413 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200414 BIT(DATA_IN_TOKEN_INTERRUPT),
415 &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* fifo size is handled separately */
418}
419
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200420static void ep_reset_338x(struct net2280_regs __iomem *regs,
421 struct net2280_ep *ep)
422{
423 u32 tmp, dmastat;
424
425 ep->desc = NULL;
426 INIT_LIST_HEAD(&ep->queue);
427
428 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
429 ep->ep.ops = &net2280_ep_ops;
430
431 /* disable the dma, irqs, endpoint... */
432 if (ep->dma) {
433 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200434 writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
435 BIT(DMA_PAUSE_DONE_INTERRUPT) |
436 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200437 BIT(DMA_TRANSACTION_DONE_INTERRUPT),
438 /* | BIT(DMA_ABORT), */
439 &ep->dma->dmastat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200440
441 dmastat = readl(&ep->dma->dmastat);
442 if (dmastat == 0x5002) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200443 ep_warn(ep->dev, "The dmastat return = %x!!\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200444 dmastat);
445 writel(0x5a, &ep->dma->dmastat);
446 }
447
448 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200449 tmp &= ~BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200450 writel(tmp, &regs->pciirqenb0);
451 } else {
452 if (ep->num < 5) {
453 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200454 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200455 writel(tmp, &regs->pciirqenb1);
456 }
457 }
458 writel(0, &ep->regs->ep_irqenb);
459
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200460 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
461 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
462 BIT(FIFO_OVERFLOW) |
463 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
464 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
465 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
466 BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
Mian Yousaf Kaukab971fe652015-05-16 22:33:38 +0200467
468 tmp = readl(&ep->cfg->ep_cfg);
469 if (ep->is_in)
470 tmp &= ~USB3380_EP_CFG_MASK_IN;
471 else
472 tmp &= ~USB3380_EP_CFG_MASK_OUT;
473 writel(tmp, &ep->cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200474}
475
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200476static void nuke(struct net2280_ep *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200478static int net2280_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct net2280_ep *ep;
481 unsigned long flags;
482
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200483 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100484 if (!_ep || !ep->desc || _ep->name == ep0name) {
485 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100487 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200488 spin_lock_irqsave(&ep->dev->lock, flags);
489 nuke(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200490
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200491 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200492 ep_reset_338x(ep->dev->regs, ep);
493 else
494 ep_reset_228x(ep->dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200496 ep_vdbg(ep->dev, "disabled %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 ep->dma ? "dma" : "pio", _ep->name);
498
499 /* synch memory views with the device */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200500 (void)readl(&ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +0100502 if (!ep->dma && ep->num >= 1 && ep->num <= 4)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200503 ep->dma = &ep->dev->dma[ep->num - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200505 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
507}
508
509/*-------------------------------------------------------------------------*/
510
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200511static struct usb_request
512*net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct net2280_ep *ep;
515 struct net2280_request *req;
516
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100517 if (!_ep) {
518 pr_err("%s: Invalid ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return NULL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100520 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200521 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800523 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!req)
525 return NULL;
526
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200527 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* this dma descriptor may be swapped with the previous dummy */
530 if (ep->dma) {
531 struct net2280_dma *td;
532
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200533 td = pci_pool_alloc(ep->dev->requests, gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 &req->td_dma);
535 if (!td) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200536 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return NULL;
538 }
539 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 td->dmadesc = td->dmaaddr;
541 req->td = td;
542 }
543 return &req->req;
544}
545
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200546static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct net2280_ep *ep;
549 struct net2280_request *req;
550
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200551 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100552 if (!_ep || !_req) {
553 dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n",
554 __func__, _ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200558 req = container_of(_req, struct net2280_request, req);
559 WARN_ON(!list_empty(&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (req->td)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200561 pci_pool_free(ep->dev->requests, req->td, req->td_dma);
562 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*-------------------------------------------------------------------------*/
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/* load a packet into the fifo we use for usb IN transfers.
568 * works for all endpoints.
569 *
570 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
571 * at a time, but this code is simpler because it knows it only writes
572 * one packet. ep-a..ep-d should use dma instead.
573 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200574static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct net2280_ep_regs __iomem *regs = ep->regs;
577 u8 *buf;
578 u32 tmp;
579 unsigned count, total;
580
581 /* INVARIANT: fifo is currently empty. (testable) */
582
583 if (req) {
584 buf = req->buf + req->actual;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200585 prefetch(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 total = req->length - req->actual;
587 } else {
588 total = 0;
589 buf = NULL;
590 }
591
592 /* write just one packet at a time */
593 count = ep->ep.maxpacket;
594 if (count > total) /* min() cannot be used on a bitfield */
595 count = total;
596
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200597 ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ep->ep.name, count,
599 (count != ep->ep.maxpacket) ? " (short)" : "",
600 req);
601 while (count >= 4) {
602 /* NOTE be careful if you try to align these. fifo lines
603 * should normally be full (4 bytes) and successive partial
604 * lines are ok only in certain cases.
605 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200606 tmp = get_unaligned((u32 *)buf);
607 cpu_to_le32s(&tmp);
608 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 buf += 4;
610 count -= 4;
611 }
612
613 /* last fifo entry is "short" unless we wrote a full packet.
614 * also explicitly validate last word in (periodic) transfers
615 * when maxpacket is not a multiple of 4 bytes.
616 */
617 if (count || total < ep->ep.maxpacket) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200618 tmp = count ? get_unaligned((u32 *)buf) : count;
619 cpu_to_le32s(&tmp);
620 set_fifo_bytecount(ep, count & 0x03);
621 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 /* pci writes may still be posted */
625}
626
627/* work around erratum 0106: PCI and USB race over the OUT fifo.
628 * caller guarantees chiprev 0100, out endpoint is NAKing, and
629 * there's no real data in the fifo.
630 *
631 * NOTE: also used in cases where that erratum doesn't apply:
632 * where the host wrote "too much" data to us.
633 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200634static void out_flush(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 u32 __iomem *statp;
637 u32 tmp;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 statp = &ep->regs->ep_stat;
Ricardo Ribalda Delgadod82f3db2014-11-28 14:51:01 +0100640
641 tmp = readl(statp);
642 if (tmp & BIT(NAK_OUT_PACKETS)) {
643 ep_dbg(ep->dev, "%s %s %08x !NAK\n",
644 ep->ep.name, __func__, tmp);
645 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
646 }
647
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200648 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200649 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
650 statp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200651 writel(BIT(FIFO_FLUSH), statp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200652 /* Make sure that stap is written */
653 mb();
654 tmp = readl(statp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200655 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* high speed did bulk NYET; fifo isn't filling */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200657 ep->dev->gadget.speed == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 unsigned usec;
659
660 usec = 50; /* 64 byte bulk/interrupt */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200661 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
662 BIT(USB_OUT_PING_NAK_SENT), usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
664 }
665}
666
667/* unload packet(s) from the fifo we use for usb OUT transfers.
668 * returns true iff the request completed, because of short packet
669 * or the request buffer having filled with full packets.
670 *
671 * for ep-a..ep-d this will read multiple packets out when they
672 * have been accepted.
673 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200674static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct net2280_ep_regs __iomem *regs = ep->regs;
677 u8 *buf = req->req.buf + req->req.actual;
678 unsigned count, tmp, is_short;
679 unsigned cleanup = 0, prevent = 0;
680
681 /* erratum 0106 ... packets coming in during fifo reads might
682 * be incompletely rejected. not all cases have workarounds.
683 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200684 if (ep->dev->chiprev == 0x0100 &&
685 ep->dev->gadget.speed == USB_SPEED_FULL) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200686 udelay(1);
687 tmp = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200688 if ((tmp & BIT(NAK_OUT_PACKETS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 cleanup = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200690 else if ((tmp & BIT(FIFO_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200691 start_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 prevent = 1;
693 }
694 /* else: hope we don't see the problem */
695 }
696
697 /* never overflow the rx buffer. the fifo reads packets until
698 * it sees a short one; we might not be ready for them all.
699 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200700 prefetchw(buf);
701 count = readl(&regs->ep_avail);
702 if (unlikely(count == 0)) {
703 udelay(1);
704 tmp = readl(&ep->regs->ep_stat);
705 count = readl(&regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* handled that data already? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200707 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return 0;
709 }
710
711 tmp = req->req.length - req->req.actual;
712 if (count > tmp) {
713 /* as with DMA, data overflow gets flushed */
714 if ((tmp % ep->ep.maxpacket) != 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200715 ep_err(ep->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 "%s out fifo %d bytes, expected %d\n",
717 ep->ep.name, count, tmp);
718 req->req.status = -EOVERFLOW;
719 cleanup = 1;
720 /* NAK_OUT_PACKETS will be set, so flushing is safe;
721 * the next read will start with the next packet
722 */
723 } /* else it's a ZLP, no worries */
724 count = tmp;
725 }
726 req->req.actual += count;
727
728 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
729
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200730 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 -0700731 ep->ep.name, count, is_short ? " (short)" : "",
732 cleanup ? " flush" : "", prevent ? " nak" : "",
733 req, req->req.actual, req->req.length);
734
735 while (count >= 4) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200736 tmp = readl(&regs->ep_data);
737 cpu_to_le32s(&tmp);
738 put_unaligned(tmp, (u32 *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 buf += 4;
740 count -= 4;
741 }
742 if (count) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200743 tmp = readl(&regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* LE conversion is implicit here: */
745 do {
746 *buf++ = (u8) tmp;
747 tmp >>= 8;
748 } while (--count);
749 }
750 if (cleanup)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200751 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (prevent) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200753 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200754 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200757 return is_short || ((req->req.actual == req->req.length) &&
758 !req->req.zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/* fill out dma descriptor to match a given request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200762static void fill_dma_desc(struct net2280_ep *ep,
763 struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct net2280_dma *td = req->td;
766 u32 dmacount = req->req.length;
767
768 /* don't let DMA continue after a short OUT packet,
769 * so overruns can't affect the next transfer.
770 * in case of overruns on max-size packets, we can't
771 * stop the fifo from filling but we can flush it.
772 */
773 if (ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200774 dmacount |= BIT(DMA_DIRECTION);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200775 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200776 !(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200777 dmacount |= BIT(END_OF_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 req->valid = valid;
780 if (valid)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200781 dmacount |= BIT(VALID_BIT);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100782 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* td->dmadesc = previously set by caller */
785 td->dmaaddr = cpu_to_le32 (req->req.dma);
786
787 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200788 wmb();
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700789 td->dmacount = cpu_to_le32(dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792static const u32 dmactl_default =
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200793 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
794 BIT(DMA_CLEAR_COUNT_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* erratum 0116 workaround part 1 (use POLLING) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200796 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
797 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
798 BIT(DMA_VALID_BIT_ENABLE) |
799 BIT(DMA_SCATTER_GATHER_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* erratum 0116 workaround part 2 (no AUTOSTART) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200801 BIT(DMA_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200803static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200805 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200808static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200810 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200811 spin_stop_dma(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200814static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct net2280_dma_regs __iomem *dma = ep->dma;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200817 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200819 if (!(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200820 tmp |= BIT(END_OF_CHAIN);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100821
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200822 writel(tmp, &dma->dmacount);
823 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200825 writel(td_dma, &dma->dmadesc);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200826 if (ep->dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200827 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200828 writel(dmactl, &dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200831 (void) readl(&ep->dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200833 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (!ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200836 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200839static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 u32 tmp;
842 struct net2280_dma_regs __iomem *dma = ep->dma;
843
844 /* FIXME can't use DMA for ZLPs */
845
846 /* on this path we "know" there's no dma active (yet) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200847 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200848 writel(0, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* previous OUT packet might have been short */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200851 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
852 BIT(NAK_OUT_PACKETS))) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200853 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 &ep->regs->ep_stat);
855
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200856 tmp = readl(&ep->regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200858 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* transfer all/some fifo data */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200861 writel(req->req.dma, &dma->dmaaddr);
862 tmp = min(tmp, req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* dma irq, faking scatterlist status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200865 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200866 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
867 &dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 req->td->dmadesc = 0;
869 req->valid = 1;
870
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200871 writel(BIT(DMA_ENABLE), &dma->dmactl);
872 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return;
874 }
875 }
876
877 tmp = dmactl_default;
878
879 /* force packet boundaries between dma requests, but prevent the
880 * controller from automagically writing a last "short" packet
881 * (zero length) unless the driver explicitly said to do that.
882 */
883 if (ep->is_in) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200884 if (likely((req->req.length % ep->ep.maxpacket) ||
885 req->req.zero)){
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200886 tmp |= BIT(DMA_FIFO_VALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ep->in_fifo_validate = 1;
888 } else
889 ep->in_fifo_validate = 0;
890 }
891
892 /* init req->td, pointing to the current dummy */
893 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200894 fill_dma_desc(ep, req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100896 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200898 start_queue(ep, tmp, req->td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200902queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct net2280_dma *end;
905 dma_addr_t tmp;
906
907 /* swap new dummy for old, link; fill and maybe activate */
908 end = ep->dummy;
909 ep->dummy = req->td;
910 req->td = end;
911
912 tmp = ep->td_dma;
913 ep->td_dma = req->td_dma;
914 req->td_dma = tmp;
915
916 end->dmadesc = cpu_to_le32 (ep->td_dma);
917
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200918 fill_dma_desc(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921static void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200922done(struct net2280_ep *ep, struct net2280_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct net2280 *dev;
925 unsigned stopped = ep->stopped;
926
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200927 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (req->req.status == -EINPROGRESS)
930 req->req.status = status;
931 else
932 status = req->req.status;
933
934 dev = ep->dev;
Felipe Balbiae4d7932011-12-19 12:09:56 +0200935 if (ep->dma)
936 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (status && status != -ESHUTDOWN)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200939 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 ep->ep.name, &req->req, status,
941 req->req.actual, req->req.length);
942
943 /* don't modify queue heads during completion callback */
944 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200945 spin_unlock(&dev->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200946 usb_gadget_giveback_request(&ep->ep, &req->req);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200947 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ep->stopped = stopped;
949}
950
951/*-------------------------------------------------------------------------*/
952
953static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200954net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct net2280_request *req;
957 struct net2280_ep *ep;
958 struct net2280 *dev;
959 unsigned long flags;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100960 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /* we always require a cpu-view buffer, so that we can
963 * always use pio (as fallback or whatever).
964 */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100965 ep = container_of(_ep, struct net2280_ep, ep);
966 if (!_ep || (!ep->desc && ep->num != 0)) {
967 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
968 return -EINVAL;
969 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200970 req = container_of(_req, struct net2280_request, req);
971 if (!_req || !_req->complete || !_req->buf ||
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100972 !list_empty(&req->queue)) {
973 ret = -EINVAL;
974 goto print_err;
975 }
976 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
977 ret = -EDOM;
978 goto print_err;
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 dev = ep->dev;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100981 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
982 ret = -ESHUTDOWN;
983 goto print_err;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /* FIXME implement PIO fallback for ZLPs with DMA */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100987 if (ep->dma && _req->length == 0) {
988 ret = -EOPNOTSUPP;
989 goto print_err;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /* set up dma mapping in case the caller didn't */
Felipe Balbiae4d7932011-12-19 12:09:56 +0200993 if (ep->dma) {
Felipe Balbiae4d7932011-12-19 12:09:56 +0200994 ret = usb_gadget_map_request(&dev->gadget, _req,
995 ep->is_in);
996 if (ret)
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100997 goto print_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001000 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001003 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 _req->status = -EINPROGRESS;
1006 _req->actual = 0;
1007
1008 /* kickstart this i/o queue? */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +01001009 if (list_empty(&ep->queue) && !ep->stopped &&
1010 !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
1011 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* use DMA if the endpoint supports it, else pio */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +01001014 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001015 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 else {
1017 /* maybe there's no control data, just status ack */
1018 if (ep->num == 0 && _req->length == 0) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001019 allow_status(ep);
1020 done(ep, req, 0);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001021 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto done;
1023 }
1024
1025 /* PIO ... stuff the fifo, or unblock it. */
1026 if (ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001027 write_fifo(ep, _req);
1028 else if (list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 u32 s;
1030
1031 /* OUT FIFO might have packet(s) buffered */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001032 s = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001033 if ((s & BIT(FIFO_EMPTY)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* note: _req->short_not_ok is
1035 * ignored here since PIO _always_
1036 * stops queue advance here, and
1037 * _req->status doesn't change for
1038 * short reads (only _req->actual)
1039 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001040 if (read_fifo(ep, req) &&
1041 ep->num == 0) {
1042 done(ep, req, 0);
1043 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* don't queue it */
1045 req = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001046 } else if (read_fifo(ep, req) &&
1047 ep->num != 0) {
1048 done(ep, req, 0);
1049 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001051 s = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
1054 /* don't NAK, let the fifo fill */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001055 if (req && (s & BIT(NAK_OUT_PACKETS)))
1056 writel(BIT(CLEAR_NAK_OUT_PACKETS),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 &ep->regs->ep_rsp);
1058 }
1059 }
1060
1061 } else if (ep->dma) {
1062 int valid = 1;
1063
1064 if (ep->is_in) {
1065 int expect;
1066
1067 /* preventing magic zlps is per-engine state, not
1068 * per-transfer; irq logic must recover hiccups.
1069 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001070 expect = likely(req->req.zero ||
1071 (req->req.length % ep->ep.maxpacket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (expect != ep->in_fifo_validate)
1073 valid = 0;
1074 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001075 queue_dma(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 } /* else the irq handler advances the queue. */
1078
Alan Stern1f26e282006-11-16 10:16:00 -05001079 ep->responded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001081 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001083 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* pci writes may still be posted */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001086 return ret;
1087
1088print_err:
1089 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1090 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001094dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1095 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001098 done(ep, req, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001101static void scan_dma_completions(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 /* only look at descriptors that were "naturally" retired,
1104 * so fifo and list head state won't matter
1105 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001106 while (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct net2280_request *req;
1108 u32 tmp;
1109
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001110 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct net2280_request, queue);
1112 if (!req->valid)
1113 break;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001114 rmb();
1115 tmp = le32_to_cpup(&req->td->dmacount);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001116 if ((tmp & BIT(VALID_BIT)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 break;
1118
1119 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1120 * cases where DMA must be aborted; this code handles
1121 * all non-abort DMA completions.
1122 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001123 if (unlikely(req->td->dmadesc == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /* paranoia */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001125 tmp = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (tmp & DMA_BYTE_COUNT_MASK)
1127 break;
1128 /* single transfer mode */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001129 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 break;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02001131 } else if (!ep->is_in &&
Ricardo Ribalda Delgado43780aa2014-11-28 14:51:00 +01001132 (req->req.length % ep->ep.maxpacket) &&
1133 !(ep->dev->quirks & PLX_SUPERSPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Mario Schuknecht18a4e652014-11-16 21:21:45 +01001135 tmp = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /* AVOID TROUBLE HERE by not issuing short reads from
1137 * your gadget driver. That helps avoids errata 0121,
1138 * 0122, and 0124; not all cases trigger the warning.
1139 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001140 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001141 ep_warn(ep->dev, "%s lost packet sync!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 ep->ep.name);
1143 req->req.status = -EOVERFLOW;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001144 } else {
1145 tmp = readl(&ep->regs->ep_avail);
1146 if (tmp) {
1147 /* fifo gets flushed later */
1148 ep->out_overflow = 1;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001149 ep_dbg(ep->dev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001150 "%s dma, discard %d len %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 ep->ep.name, tmp,
1152 req->req.length);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001153 req->req.status = -EOVERFLOW;
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001157 dma_done(ep, req, tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159}
1160
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001161static void restart_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct net2280_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 if (ep->stopped)
1166 return;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001167 req = list_entry(ep->queue.next, struct net2280_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01001169 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01001172static void abort_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 /* abort the current transfer */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001175 if (likely(!list_empty(&ep->queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* FIXME work around errata 0121, 0122, 0124 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001177 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001178 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001180 stop_dma(ep->dma);
1181 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184/* dequeue ALL requests */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001185static void nuke(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct net2280_request *req;
1188
1189 /* called with spinlock held */
1190 ep->stopped = 1;
1191 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001192 abort_dma(ep);
1193 while (!list_empty(&ep->queue)) {
1194 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 struct net2280_request,
1196 queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001197 done(ep, req, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199}
1200
1201/* dequeue JUST ONE request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001202static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct net2280_ep *ep;
1205 struct net2280_request *req;
1206 unsigned long flags;
1207 u32 dmactl;
1208 int stopped;
1209
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001210 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001211 if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1212 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1213 __func__, _ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001217 spin_lock_irqsave(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 stopped = ep->stopped;
1219
1220 /* quiesce dma while we patch the queue */
1221 dmactl = 0;
1222 ep->stopped = 1;
1223 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001224 dmactl = readl(&ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /* WARNING erratum 0127 may kick in ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001226 stop_dma(ep->dma);
1227 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229
1230 /* make sure it's still queued on this endpoint */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001231 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (&req->req == _req)
1233 break;
1234 }
1235 if (&req->req != _req) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001236 spin_unlock_irqrestore(&ep->dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001237 dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
1238 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EINVAL;
1240 }
1241
1242 /* queue head may be partially complete. */
1243 if (ep->queue.next == &req->queue) {
1244 if (ep->dma) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001245 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 _req->status = -ECONNRESET;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001247 abort_dma(ep);
1248 if (likely(ep->queue.next == &req->queue)) {
1249 /* NOTE: misreports single-transfer mode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 req->td->dmacount = 0; /* invalidate */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001251 dma_done(ep, req,
1252 readl(&ep->dma->dmacount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 -ECONNRESET);
1254 }
1255 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001256 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001257 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261
1262 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001263 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 ep->stopped = stopped;
1265
1266 if (ep->dma) {
1267 /* turn off dma on inactive queues */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001268 if (list_empty(&ep->queue))
1269 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 else if (!ep->stopped) {
1271 /* resume current request, or start new one */
1272 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001273 writel(dmactl, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001275 start_dma(ep, list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 struct net2280_request, queue));
1277 }
1278 }
1279
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001280 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
1282}
1283
1284/*-------------------------------------------------------------------------*/
1285
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001286static int net2280_fifo_status(struct usb_ep *_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288static int
Alan Stern80661342008-08-14 15:49:11 -04001289net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct net2280_ep *ep;
1292 unsigned long flags;
1293 int retval = 0;
1294
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001295 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001296 if (!_ep || (!ep->desc && ep->num != 0)) {
1297 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001299 }
1300 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1301 retval = -ESHUTDOWN;
1302 goto print_err;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001305 == USB_ENDPOINT_XFER_ISOC) {
1306 retval = -EINVAL;
1307 goto print_err;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001310 spin_lock_irqsave(&ep->dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001311 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 retval = -EAGAIN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001313 goto print_unlock;
1314 } else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 retval = -EAGAIN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001316 goto print_unlock;
1317 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001318 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
Alan Stern80661342008-08-14 15:49:11 -04001319 value ? "set" : "clear",
1320 wedged ? "wedge" : "halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* set/clear, then synch memory views with the device */
1322 if (value) {
1323 if (ep->num == 0)
1324 ep->dev->protocol_stall = 1;
1325 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001326 set_halt(ep);
Alan Stern80661342008-08-14 15:49:11 -04001327 if (wedged)
1328 ep->wedged = 1;
1329 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001330 clear_halt(ep);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02001331 if (ep->dev->quirks & PLX_SUPERSPEED &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001332 !list_empty(&ep->queue) && ep->td_dma)
1333 restart_dma(ep);
Alan Stern80661342008-08-14 15:49:11 -04001334 ep->wedged = 0;
1335 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001336 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001338 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 return retval;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001341
1342print_unlock:
1343 spin_unlock_irqrestore(&ep->dev->lock, flags);
1344print_err:
1345 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1346 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001349static int net2280_set_halt(struct usb_ep *_ep, int value)
Alan Stern80661342008-08-14 15:49:11 -04001350{
1351 return net2280_set_halt_and_wedge(_ep, value, 0);
1352}
1353
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001354static int net2280_set_wedge(struct usb_ep *_ep)
Alan Stern80661342008-08-14 15:49:11 -04001355{
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001356 if (!_ep || _ep->name == ep0name) {
1357 pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
Alan Stern80661342008-08-14 15:49:11 -04001358 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001359 }
Alan Stern80661342008-08-14 15:49:11 -04001360 return net2280_set_halt_and_wedge(_ep, 1, 1);
1361}
1362
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001363static int net2280_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct net2280_ep *ep;
1366 u32 avail;
1367
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001368 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001369 if (!_ep || (!ep->desc && ep->num != 0)) {
1370 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -ENODEV;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001372 }
1373 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1374 dev_err(&ep->dev->pdev->dev,
1375 "%s: Invalid driver=%p or speed=%d\n",
1376 __func__, ep->dev->driver, ep->dev->gadget.speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return -ESHUTDOWN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001380 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001381 if (avail > ep->fifo_size) {
1382 dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -EOVERFLOW;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (ep->is_in)
1386 avail = ep->fifo_size - avail;
1387 return avail;
1388}
1389
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001390static void net2280_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct net2280_ep *ep;
1393
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001394 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001395 if (!_ep || (!ep->desc && ep->num != 0)) {
1396 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001398 }
1399 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1400 dev_err(&ep->dev->pdev->dev,
1401 "%s: Invalid driver=%p or speed=%d\n",
1402 __func__, ep->dev->driver, ep->dev->gadget.speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001406 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001407 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
David Brownell901b3d72006-09-02 03:13:45 -07001410static const struct usb_ep_ops net2280_ep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 .enable = net2280_enable,
1412 .disable = net2280_disable,
1413
1414 .alloc_request = net2280_alloc_request,
1415 .free_request = net2280_free_request,
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 .queue = net2280_queue,
1418 .dequeue = net2280_dequeue,
1419
1420 .set_halt = net2280_set_halt,
Alan Stern80661342008-08-14 15:49:11 -04001421 .set_wedge = net2280_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 .fifo_status = net2280_fifo_status,
1423 .fifo_flush = net2280_fifo_flush,
1424};
1425
1426/*-------------------------------------------------------------------------*/
1427
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001428static int net2280_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 struct net2280 *dev;
1431 unsigned long flags;
1432 u16 retval;
1433
1434 if (!_gadget)
1435 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001436 dev = container_of(_gadget, struct net2280, gadget);
1437 spin_lock_irqsave(&dev->lock, flags);
1438 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1439 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return retval;
1441}
1442
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001443static int net2280_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct net2280 *dev;
1446 u32 tmp;
1447 unsigned long flags;
1448
1449 if (!_gadget)
1450 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001451 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001453 spin_lock_irqsave(&dev->lock, flags);
1454 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001455 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1456 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001457 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /* pci writes may still be posted */
1460 return 0;
1461}
1462
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001463static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct net2280 *dev;
1466 u32 tmp;
1467 unsigned long flags;
1468
1469 if (!_gadget)
1470 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001471 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001473 spin_lock_irqsave(&dev->lock, flags);
1474 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001475 if (value) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001476 tmp |= BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001477 _gadget->is_selfpowered = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001478 } else {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001479 tmp &= ~BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001480 _gadget->is_selfpowered = 0;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001481 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001482 writel(tmp, &dev->usb->usbctl);
1483 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 return 0;
1486}
1487
1488static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1489{
1490 struct net2280 *dev;
1491 u32 tmp;
1492 unsigned long flags;
1493
1494 if (!_gadget)
1495 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001496 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001498 spin_lock_irqsave(&dev->lock, flags);
1499 tmp = readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 dev->softconnect = (is_on != 0);
Mian Yousaf Kaukab11bece52015-05-16 22:33:39 +02001501 if (is_on) {
1502 ep0_start(dev);
1503 writel(tmp | BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1504 } else {
1505 writel(tmp & ~BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1506 stop_activity(dev, dev->driver);
1507 }
1508
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001509 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 return 0;
1512}
1513
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001514static int net2280_start(struct usb_gadget *_gadget,
1515 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001516static int net2280_stop(struct usb_gadget *_gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static const struct usb_gadget_ops net2280_ops = {
1519 .get_frame = net2280_get_frame,
1520 .wakeup = net2280_wakeup,
1521 .set_selfpowered = net2280_set_selfpowered,
1522 .pullup = net2280_pullup,
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001523 .udc_start = net2280_start,
1524 .udc_stop = net2280_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525};
1526
1527/*-------------------------------------------------------------------------*/
1528
Ricardo Ribalda Delgadob99b4062014-07-04 11:27:03 +02001529#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531/* FIXME move these into procfs, and use seq_file.
1532 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1533 * and also doesn't help products using this with 2.4 kernels.
1534 */
1535
1536/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001537static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1538 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001540 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001542 if (!dev->driver || !dev->driver->function ||
1543 strlen(dev->driver->function) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001545 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001547static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001549static ssize_t registers_show(struct device *_dev,
1550 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 struct net2280 *dev;
1553 char *next;
1554 unsigned size, t;
1555 unsigned long flags;
1556 int i;
1557 u32 t1, t2;
Andrew Morton30e69592005-06-26 17:18:46 -07001558 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001560 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 next = buf;
1562 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001563 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 if (dev->driver)
1566 s = dev->driver->driver.name;
1567 else
1568 s = "(none)";
1569
1570 /* Main Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001571 t = scnprintf(next, size, "%s version " DRIVER_VERSION
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001572 ", chiprev %04x\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 "devinit %03x fifoctl %08x gadget '%s'\n"
1574 "pci irqenb0 %02x irqenb1 %08x "
1575 "irqstat0 %04x irqstat1 %08x\n",
1576 driver_name, dev->chiprev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001577 readl(&dev->regs->devinit),
1578 readl(&dev->regs->fifoctl),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 s,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001580 readl(&dev->regs->pciirqenb0),
1581 readl(&dev->regs->pciirqenb1),
1582 readl(&dev->regs->irqstat0),
1583 readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 size -= t;
1585 next += t;
1586
1587 /* USB Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001588 t1 = readl(&dev->usb->usbctl);
1589 t2 = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001590 if (t1 & BIT(VBUS_PIN)) {
1591 if (t2 & BIT(HIGH_SPEED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 s = "high speed";
1593 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1594 s = "powered";
1595 else
1596 s = "full speed";
1597 /* full speed bit (6) not working?? */
1598 } else
1599 s = "not attached";
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001600 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 "stdrsp %08x usbctl %08x usbstat %08x "
1602 "addr 0x%02x (%s)\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001603 readl(&dev->usb->stdrsp), t1, t2,
1604 readl(&dev->usb->ouraddr), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 size -= t;
1606 next += t;
1607
1608 /* PCI Master Control Registers */
1609
1610 /* DMA Control Registers */
1611
1612 /* Configurable EP Control Registers */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001613 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct net2280_ep *ep;
1615
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001616 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (i && !ep->desc)
1618 continue;
1619
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001620 t1 = readl(&ep->cfg->ep_cfg);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001621 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1622 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1624 "irqenb %02x\n",
1625 ep->ep.name, t1, t2,
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001626 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ? "NAK " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001628 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 ? "hide " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001630 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 ? "CRC " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001632 (t2 & BIT(CLEAR_INTERRUPT_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 ? "interrupt " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001634 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 ? "status " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001636 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 ? "NAKmode " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001638 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 ? "DATA1 " : "DATA0 ",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001640 (t2 & BIT(CLEAR_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ? "HALT " : "",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001642 readl(&ep->regs->ep_irqenb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 size -= t;
1644 next += t;
1645
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001646 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 "\tstat %08x avail %04x "
1648 "(ep%d%s-%s)%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001649 readl(&ep->regs->ep_stat),
1650 readl(&ep->regs->ep_avail),
1651 t1 & 0x0f, DIR_STRING(t1),
1652 type_string(t1 >> 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 ep->stopped ? "*" : "");
1654 size -= t;
1655 next += t;
1656
1657 if (!ep->dma)
1658 continue;
1659
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001660 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 " dma\tctl %08x stat %08x count %08x\n"
1662 "\taddr %08x desc %08x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001663 readl(&ep->dma->dmactl),
1664 readl(&ep->dma->dmastat),
1665 readl(&ep->dma->dmacount),
1666 readl(&ep->dma->dmaaddr),
1667 readl(&ep->dma->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 size -= t;
1669 next += t;
1670
1671 }
1672
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001673 /* Indexed Registers (none yet) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /* Statistics */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001676 t = scnprintf(next, size, "\nirqs: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 size -= t;
1678 next += t;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001679 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct net2280_ep *ep;
1681
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001682 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (i && !ep->irqs)
1684 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001685 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 size -= t;
1687 next += t;
1688
1689 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001690 t = scnprintf(next, size, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 size -= t;
1692 next += t;
1693
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001694 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 return PAGE_SIZE - size;
1697}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001698static DEVICE_ATTR_RO(registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001700static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1701 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 struct net2280 *dev;
1704 char *next;
1705 unsigned size;
1706 unsigned long flags;
1707 int i;
1708
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001709 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 next = buf;
1711 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001712 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001714 for (i = 0; i < dev->n_ep; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001715 struct net2280_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct net2280_request *req;
1717 int t;
1718
1719 if (i != 0) {
1720 const struct usb_endpoint_descriptor *d;
1721
1722 d = ep->desc;
1723 if (!d)
1724 continue;
1725 t = d->bEndpointAddress;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001726 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1728 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1729 (t & USB_DIR_IN) ? "in" : "out",
Ricardo Ribalda Delgadoa27f37a2014-05-20 18:30:08 +02001730 type_string(d->bmAttributes),
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001731 usb_endpoint_maxp(d) & 0x1fff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 ep->dma ? "dma" : "pio", ep->fifo_size
1733 );
1734 } else /* ep0 should only have one transfer queued */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001735 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 ep->is_in ? "in" : "out");
1737 if (t <= 0 || t > size)
1738 goto done;
1739 size -= t;
1740 next += t;
1741
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001742 if (list_empty(&ep->queue)) {
1743 t = scnprintf(next, size, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (t <= 0 || t > size)
1745 goto done;
1746 size -= t;
1747 next += t;
1748 continue;
1749 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001750 list_for_each_entry(req, &ep->queue, queue) {
1751 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1752 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 "\treq %p len %d/%d "
1754 "buf %p (dmacount %08x)\n",
1755 &req->req, req->req.actual,
1756 req->req.length, req->req.buf,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001757 readl(&ep->dma->dmacount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001759 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 "\treq %p len %d/%d buf %p\n",
1761 &req->req, req->req.actual,
1762 req->req.length, req->req.buf);
1763 if (t <= 0 || t > size)
1764 goto done;
1765 size -= t;
1766 next += t;
1767
1768 if (ep->dma) {
1769 struct net2280_dma *td;
1770
1771 td = req->td;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001772 t = scnprintf(next, size, "\t td %08x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 " count %08x buf %08x desc %08x\n",
1774 (u32) req->td_dma,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001775 le32_to_cpu(td->dmacount),
1776 le32_to_cpu(td->dmaaddr),
1777 le32_to_cpu(td->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (t <= 0 || t > size)
1779 goto done;
1780 size -= t;
1781 next += t;
1782 }
1783 }
1784 }
1785
1786done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001787 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return PAGE_SIZE - size;
1789}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001790static DEVICE_ATTR_RO(queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792
1793#else
1794
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001795#define device_create_file(a, b) (0)
1796#define device_remove_file(a, b) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798#endif
1799
1800/*-------------------------------------------------------------------------*/
1801
1802/* another driver-specific mode might be a request type doing dma
1803 * to/from another device fifo instead of to/from memory.
1804 */
1805
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001806static void set_fifo_mode(struct net2280 *dev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 /* keeping high bits preserves BAR2 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001809 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001812 INIT_LIST_HEAD(&dev->gadget.ep_list);
1813 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1814 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 switch (mode) {
1816 case 0:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001817 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1818 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1819 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821 case 1:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001822 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 break;
1824 case 2:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001825 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1826 dev->ep[1].fifo_size = 2048;
1827 dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 break;
1829 }
1830 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001831 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1832 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001835static void defect7374_disable_data_eps(struct net2280 *dev)
1836{
1837 /*
1838 * For Defect 7374, disable data EPs (and more):
1839 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1840 * returing ep regs back to normal.
1841 */
1842 struct net2280_ep *ep;
1843 int i;
1844 unsigned char ep_sel;
1845 u32 tmp_reg;
1846
1847 for (i = 1; i < 5; i++) {
1848 ep = &dev->ep[i];
1849 writel(0, &ep->cfg->ep_cfg);
1850 }
1851
1852 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1853 for (i = 0; i < 6; i++)
1854 writel(0, &dev->dep[i].dep_cfg);
1855
1856 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1857 /* Select an endpoint for subsequent operations: */
1858 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1859 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1860
1861 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1862 ep_sel == 18 || ep_sel == 20)
1863 continue;
1864
1865 /* Change settings on some selected endpoints */
1866 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001867 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001868 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1869 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001870 tmp_reg |= BIT(EP_INITIALIZED);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001871 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1872 }
1873}
1874
1875static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1876{
1877 u32 tmp = 0, tmp_reg;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001878 u32 scratch;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001879 int i;
1880 unsigned char ep_sel;
1881
1882 scratch = get_idx_reg(dev->regs, SCRATCH);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001883
1884 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1885 == DEFECT7374_FSM_SS_CONTROL_READ);
1886
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001887 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1888
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001889 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1890 ep_warn(dev, "It will operate on cold-reboot and SS connect");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001891
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001892 /*GPEPs:*/
1893 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1894 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1895 ((dev->enhanced_mode) ?
Mian Yousaf Kaukab25d40ee2015-05-16 22:33:30 +02001896 BIT(OUT_ENDPOINT_ENABLE) | BIT(IN_ENDPOINT_ENABLE) :
1897 BIT(ENDPOINT_ENABLE)));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001898
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001899 for (i = 1; i < 5; i++)
1900 writel(tmp, &dev->ep[i].cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001901
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001902 /* CSRIN, PCIIN, STATIN, RCIN*/
1903 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1904 writel(tmp, &dev->dep[1].dep_cfg);
1905 writel(tmp, &dev->dep[3].dep_cfg);
1906 writel(tmp, &dev->dep[4].dep_cfg);
1907 writel(tmp, &dev->dep[5].dep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001908
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001909 /*Implemented for development and debug.
1910 * Can be refined/tuned later.*/
1911 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1912 /* Select an endpoint for subsequent operations: */
1913 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1914 writel(((tmp_reg & ~0x1f) | ep_sel),
1915 &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001916
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001917 if (ep_sel == 1) {
1918 tmp =
1919 (readl(&dev->plregs->pl_ep_ctrl) |
1920 BIT(CLEAR_ACK_ERROR_CODE) | 0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001921 writel(tmp, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001922 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001923 }
1924
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001925 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1926 ep_sel == 18 || ep_sel == 20)
1927 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001928
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001929 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1930 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1931 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1932
1933 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1934 ~BIT(EP_INITIALIZED);
1935 writel(tmp, &dev->plregs->pl_ep_ctrl);
1936
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001937 }
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001938
1939 /* Set FSM to focus on the first Control Read:
1940 * - Tip: Connection speed is known upon the first
1941 * setup request.*/
1942 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1943 set_idx_reg(dev->regs, SCRATCH, scratch);
1944
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001945}
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947/* keeping it simple:
1948 * - one bus driver, initted first;
1949 * - one function driver, initted second
1950 *
1951 * most of the work to support multiple net2280 controllers would
1952 * be to associate this gadget driver (yes?) with all of them, or
1953 * perhaps to bind specific drivers to specific devices.
1954 */
1955
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001956static void usb_reset_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 u32 tmp;
1959
1960 dev->gadget.speed = USB_SPEED_UNKNOWN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001961 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001963 net2280_led_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 /* disable automatic responses, and irqs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001966 writel(0, &dev->usb->stdrsp);
1967 writel(0, &dev->regs->pciirqenb0);
1968 writel(0, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* clear old dma and irq state */
1971 for (tmp = 0; tmp < 4; tmp++) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001972 struct net2280_ep *ep = &dev->ep[tmp + 1];
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001973 if (ep->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001974 abort_dma(ep);
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07001975 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001976
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001977 writel(~0, &dev->regs->irqstat0),
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001978 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /* reset, and enable pci */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001981 tmp = readl(&dev->regs->devinit) |
1982 BIT(PCI_ENABLE) |
1983 BIT(FIFO_SOFT_RESET) |
1984 BIT(USB_SOFT_RESET) |
1985 BIT(M8051_RESET);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001986 writel(tmp, &dev->regs->devinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 /* standard fifo and endpoint allocations */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001989 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001992static void usb_reset_338x(struct net2280 *dev)
1993{
1994 u32 tmp;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001995
1996 dev->gadget.speed = USB_SPEED_UNKNOWN;
1997 (void)readl(&dev->usb->usbctl);
1998
1999 net2280_led_init(dev);
2000
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002001 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002002 /* disable automatic responses, and irqs */
2003 writel(0, &dev->usb->stdrsp);
2004 writel(0, &dev->regs->pciirqenb0);
2005 writel(0, &dev->regs->pciirqenb1);
2006 }
2007
2008 /* clear old dma and irq state */
2009 for (tmp = 0; tmp < 4; tmp++) {
2010 struct net2280_ep *ep = &dev->ep[tmp + 1];
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02002011 struct net2280_dma_regs __iomem *dma;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002012
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02002013 if (ep->dma) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002014 abort_dma(ep);
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02002015 } else {
2016 dma = &dev->dma[tmp];
2017 writel(BIT(DMA_ABORT), &dma->dmastat);
2018 writel(0, &dma->dmactl);
2019 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002020 }
2021
2022 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
2023
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002024 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002025 /* reset, and enable pci */
2026 tmp = readl(&dev->regs->devinit) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002027 BIT(PCI_ENABLE) |
2028 BIT(FIFO_SOFT_RESET) |
2029 BIT(USB_SOFT_RESET) |
2030 BIT(M8051_RESET);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002031
2032 writel(tmp, &dev->regs->devinit);
2033 }
2034
2035 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2036 INIT_LIST_HEAD(&dev->gadget.ep_list);
2037
2038 for (tmp = 1; tmp < dev->n_ep; tmp++)
2039 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2040
2041}
2042
2043static void usb_reset(struct net2280 *dev)
2044{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002045 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002046 return usb_reset_228x(dev);
2047 return usb_reset_338x(dev);
2048}
2049
2050static void usb_reinit_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 /* basic endpoint init */
2055 for (tmp = 0; tmp < 7; tmp++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002056 struct net2280_ep *ep = &dev->ep[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002058 ep->ep.name = ep_name[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 ep->dev = dev;
2060 ep->num = tmp;
2061
2062 if (tmp > 0 && tmp <= 4) {
2063 ep->fifo_size = 1024;
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01002064 ep->dma = &dev->dma[tmp - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 } else
2066 ep->fifo_size = 64;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002067 ep->regs = &dev->epregs[tmp];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002068 ep->cfg = &dev->epregs[tmp];
2069 ep_reset_228x(dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002071 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
2072 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
2073 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002075 dev->gadget.ep0 = &dev->ep[0].ep;
2076 dev->ep[0].stopped = 0;
2077 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 /* we want to prevent lowlevel/insecure access from the USB host,
2080 * but erratum 0119 means this enable bit is ignored
2081 */
2082 for (tmp = 0; tmp < 5; tmp++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002083 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002086static void usb_reinit_338x(struct net2280 *dev)
2087{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002088 int i;
2089 u32 tmp, val;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002090 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2091 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2092 0x00, 0xC0, 0x00, 0xC0 };
2093
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002094 /* basic endpoint init */
2095 for (i = 0; i < dev->n_ep; i++) {
2096 struct net2280_ep *ep = &dev->ep[i];
2097
Mian Yousaf Kaukaba285f402015-02-02 10:55:23 +01002098 ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002099 ep->dev = dev;
2100 ep->num = i;
2101
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01002102 if (i > 0 && i <= 4)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002103 ep->dma = &dev->dma[i - 1];
2104
2105 if (dev->enhanced_mode) {
2106 ep->cfg = &dev->epregs[ne[i]];
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +02002107 /*
2108 * Set USB endpoint number, hardware allows same number
2109 * in both directions.
2110 */
2111 if (i > 0 && i < 5)
2112 writel(ne[i], &ep->cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002113 ep->regs = (struct net2280_ep_regs __iomem *)
Felipe Balbic43e97b2014-07-16 12:20:25 -05002114 (((void __iomem *)&dev->epregs[ne[i]]) +
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002115 ep_reg_addr[i]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002116 } else {
2117 ep->cfg = &dev->epregs[i];
2118 ep->regs = &dev->epregs[i];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002119 }
2120
2121 ep->fifo_size = (i != 0) ? 2048 : 512;
2122
2123 ep_reset_338x(dev->regs, ep);
2124 }
2125 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2126
2127 dev->gadget.ep0 = &dev->ep[0].ep;
2128 dev->ep[0].stopped = 0;
2129
2130 /* Link layer set up */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002131 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002132 tmp = readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002133 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002134 writel(tmp, &dev->usb_ext->usbctl2);
2135 }
2136
2137 /* Hardware Defect and Workaround */
2138 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2139 val &= ~(0xf << TIMER_LFPS_6US);
2140 val |= 0x5 << TIMER_LFPS_6US;
2141 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2142
2143 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2144 val &= ~(0xffff << TIMER_LFPS_80US);
2145 val |= 0x0100 << TIMER_LFPS_80US;
2146 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2147
2148 /*
2149 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2150 * Hot Reset Exit Handshake may Fail in Specific Case using
2151 * Default Register Settings. Workaround for Enumeration test.
2152 */
2153 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2154 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2155 val |= 0x10 << HOT_TX_NORESET_TS2;
2156 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2157
2158 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2159 val &= ~(0x1f << HOT_RX_RESET_TS2);
2160 val |= 0x3 << HOT_RX_RESET_TS2;
2161 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2162
2163 /*
2164 * Set Recovery Idle to Recover bit:
2165 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2166 * link robustness with various hosts and hubs.
2167 * - It is safe to set for all connection speeds; all chip revisions.
2168 * - R-M-W to leave other bits undisturbed.
2169 * - Reference PLX TT-7372
2170 */
2171 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002172 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002173 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2174
2175 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2176
2177 /* disable dedicated endpoints */
2178 writel(0x0D, &dev->dep[0].dep_cfg);
2179 writel(0x0D, &dev->dep[1].dep_cfg);
2180 writel(0x0E, &dev->dep[2].dep_cfg);
2181 writel(0x0E, &dev->dep[3].dep_cfg);
2182 writel(0x0F, &dev->dep[4].dep_cfg);
2183 writel(0x0C, &dev->dep[5].dep_cfg);
2184}
2185
2186static void usb_reinit(struct net2280 *dev)
2187{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002188 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002189 return usb_reinit_228x(dev);
2190 return usb_reinit_338x(dev);
2191}
2192
2193static void ep0_start_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002195 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2196 BIT(CLEAR_NAK_OUT_PACKETS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002197 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2198 &dev->epregs[0].ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 /*
2201 * hardware optionally handles a bunch of standard requests
2202 * that the API hides from drivers anyway. have it do so.
2203 * endpoint status/features are handled in software, to
2204 * help pass tests for some dubious behavior.
2205 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002206 writel(BIT(SET_TEST_MODE) |
2207 BIT(SET_ADDRESS) |
2208 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2209 BIT(GET_DEVICE_STATUS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002210 BIT(GET_INTERFACE_STATUS),
2211 &dev->usb->stdrsp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002212 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2213 BIT(SELF_POWERED_USB_DEVICE) |
2214 BIT(REMOTE_WAKEUP_SUPPORT) |
2215 (dev->softconnect << USB_DETECT_ENABLE) |
2216 BIT(SELF_POWERED_STATUS),
2217 &dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002220 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2221 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2222 &dev->regs->pciirqenb0);
2223 writel(BIT(PCI_INTERRUPT_ENABLE) |
2224 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2225 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2226 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2227 BIT(VBUS_INTERRUPT_ENABLE) |
2228 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2229 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2230 &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 /* don't leave any writes posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002233 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002236static void ep0_start_338x(struct net2280 *dev)
2237{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002238
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002239 if (dev->bug7734_patched)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002240 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2241 BIT(SET_EP_HIDE_STATUS_PHASE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002242 &dev->epregs[0].ep_rsp);
2243
2244 /*
2245 * hardware optionally handles a bunch of standard requests
2246 * that the API hides from drivers anyway. have it do so.
2247 * endpoint status/features are handled in software, to
2248 * help pass tests for some dubious behavior.
2249 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002250 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2251 BIT(SET_SEL) |
2252 BIT(SET_TEST_MODE) |
2253 BIT(SET_ADDRESS) |
2254 BIT(GET_INTERFACE_STATUS) |
2255 BIT(GET_DEVICE_STATUS),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002256 &dev->usb->stdrsp);
2257 dev->wakeup_enable = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002258 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002259 (dev->softconnect << USB_DETECT_ENABLE) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002260 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002261 &dev->usb->usbctl);
2262
2263 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002264 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002265 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2266 &dev->regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002267 writel(BIT(PCI_INTERRUPT_ENABLE) |
2268 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2269 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2270 BIT(VBUS_INTERRUPT_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002271 &dev->regs->pciirqenb1);
2272
2273 /* don't leave any writes posted */
2274 (void)readl(&dev->usb->usbctl);
2275}
2276
2277static void ep0_start(struct net2280 *dev)
2278{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002279 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002280 return ep0_start_228x(dev);
2281 return ep0_start_338x(dev);
2282}
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284/* when a driver is successfully registered, it will receive
2285 * control requests including set_configuration(), which enables
2286 * non-control requests. then usb traffic follows until a
2287 * disconnect is reported. then a host may connect again, or
2288 * the driver might get unbound.
2289 */
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002290static int net2280_start(struct usb_gadget *_gadget,
2291 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002293 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 int retval;
2295 unsigned i;
2296
2297 /* insist on high speed support from the driver, since
2298 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2299 * "must not be used in normal operation"
2300 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002301 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2302 !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return -EINVAL;
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002304
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002305 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002307 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002308 dev->ep[i].irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 /* hook up the driver ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 driver->driver.bus = NULL;
2312 dev->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002314 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2315 if (retval)
2316 goto err_unbind;
2317 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2318 if (retval)
2319 goto err_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Ricardo Ribalda Delgado7a74c482014-11-28 14:50:47 +01002321 /* enable host detection and ep0; and we're ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * for set_configuration as well as eventual disconnect.
2323 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002324 net2280_led_active(dev, 1);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002325
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002326 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002327 defect7374_enable_data_eps_zero(dev);
2328
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002329 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 /* pci writes may still be posted */
2332 return 0;
Jeff Garzikb3899da2006-10-11 21:50:24 -04002333
2334err_func:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002335 device_remove_file(&dev->pdev->dev, &dev_attr_function);
Jeff Garzikb3899da2006-10-11 21:50:24 -04002336err_unbind:
Jeff Garzikb3899da2006-10-11 21:50:24 -04002337 dev->driver = NULL;
2338 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002341static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 int i;
2344
2345 /* don't disconnect if it's not connected */
2346 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2347 driver = NULL;
2348
2349 /* stop hardware; prevent new request submissions;
2350 * and kill any outstanding requests.
2351 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002352 usb_reset(dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002353 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002354 nuke(&dev->ep[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Felipe Balbi699412d2013-03-18 10:14:47 +02002356 /* report disconnect; the driver is already quiesced */
2357 if (driver) {
2358 spin_unlock(&dev->lock);
2359 driver->disconnect(&dev->gadget);
2360 spin_lock(&dev->lock);
2361 }
2362
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002363 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Felipe Balbi22835b82014-10-17 12:05:12 -05002366static int net2280_stop(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002368 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 unsigned long flags;
2370
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002371 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002373 spin_lock_irqsave(&dev->lock, flags);
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002374 stop_activity(dev, NULL);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002375 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002377 net2280_led_active(dev, 0);
Alan Stern2f076072013-01-30 16:40:14 -05002378
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002379 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2380 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002382 dev->driver = NULL;
Ricardo Ribalda Delgado84237bf2014-05-15 14:28:45 +02002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return 0;
2385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387/*-------------------------------------------------------------------------*/
2388
2389/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2390 * also works for dma-capable endpoints, in pio mode or just
2391 * to manually advance the queue after short OUT transfers.
2392 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002393static void handle_ep_small(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 struct net2280_request *req;
2396 u32 t;
2397 /* 0 error, 1 mid-data, 2 done */
2398 int mode = 1;
2399
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002400 if (!list_empty(&ep->queue))
2401 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 struct net2280_request, queue);
2403 else
2404 req = NULL;
2405
2406 /* ack all, and handle what we care about */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002407 t = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 ep->irqs++;
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002409
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002410 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
Ricardo Ribalda Delgadofc12c682015-01-13 10:54:58 +01002411 ep->ep.name, t, req ? &req->req : NULL);
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002412
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002413 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002414 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002415 else
2416 /* Added for 2282 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002417 writel(t, &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 /* for ep0, monitor token irqs to catch data stage length errors
2420 * and to synchronize on status.
2421 *
2422 * also, to defer reporting of protocol stalls ... here's where
2423 * data or status first appears, handling stalls here should never
2424 * cause trouble on the host side..
2425 *
2426 * control requests could be slightly faster without token synch for
2427 * status, but status can jam up that way.
2428 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002429 if (unlikely(ep->num == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (ep->is_in) {
2431 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002432 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (ep->dev->protocol_stall) {
2434 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002435 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437 if (!req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002438 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 mode = 2;
2440 /* reply to extra IN data tokens with a zlp */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002441 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (ep->dev->protocol_stall) {
2443 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002444 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 mode = 2;
Alan Stern1f26e282006-11-16 10:16:00 -05002446 } else if (ep->responded &&
2447 !req && !ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002448 write_fifo(ep, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450 } else {
2451 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002452 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (ep->dev->protocol_stall) {
2454 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002455 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
2457 mode = 2;
2458 /* an extra OUT token is an error */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002459 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2460 req &&
2461 req->req.actual == req->req.length) ||
2462 (ep->responded && !req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 ep->dev->protocol_stall = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002464 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 ep->stopped = 1;
2466 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002467 done(ep, req, -EOVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 req = NULL;
2469 }
2470 }
2471 }
2472
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002473 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return;
2475
2476 /* manual DMA queue advance after short OUT */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002477 if (likely(ep->dma)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002478 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 u32 count;
2480 int stopped = ep->stopped;
2481
2482 /* TRANSFERRED works around OUT_DONE erratum 0112.
2483 * we expect (N <= maxpacket) bytes; host wrote M.
2484 * iff (M < N) we won't ever see a DMA interrupt.
2485 */
2486 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002487 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 /* any preceding dma transfers must finish.
2490 * dma handles (M >= N), may empty the queue
2491 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002492 scan_dma_completions(ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002493 if (unlikely(list_empty(&ep->queue) ||
2494 ep->out_overflow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 req = NULL;
2496 break;
2497 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002498 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct net2280_request, queue);
2500
2501 /* here either (M < N), a "real" short rx;
2502 * or (M == N) and the queue didn't empty
2503 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002504 if (likely(t & BIT(FIFO_EMPTY))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002505 count = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 count &= DMA_BYTE_COUNT_MASK;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002507 if (readl(&ep->dma->dmadesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 != req->td_dma)
2509 req = NULL;
2510 break;
2511 }
2512 udelay(1);
2513 }
2514
2515 /* stop DMA, leave ep NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002516 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002517 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002519 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 req->td->dmacount = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002521 t = readl(&ep->regs->ep_avail);
2522 dma_done(ep, req, count,
David Brownell901b3d72006-09-02 03:13:45 -07002523 (ep->out_overflow || t)
2524 ? -EOVERFLOW : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
2526
2527 /* also flush to prevent erratum 0106 trouble */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002528 if (unlikely(ep->out_overflow ||
2529 (ep->dev->chiprev == 0x0100 &&
2530 ep->dev->gadget.speed
2531 == USB_SPEED_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002532 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 ep->out_overflow = 0;
2534 }
2535
2536 /* (re)start dma if needed, stop NAKing */
2537 ep->stopped = stopped;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002538 if (!list_empty(&ep->queue))
2539 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 } else
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002541 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 ep->ep.name, t);
2543 return;
2544
2545 /* data packet(s) received (in the fifo, OUT) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002546 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002547 if (read_fifo(ep, req) && ep->num != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 mode = 2;
2549
2550 /* data packet(s) transmitted (IN) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002551 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 unsigned len;
2553
2554 len = req->req.length - req->req.actual;
2555 if (len > ep->ep.maxpacket)
2556 len = ep->ep.maxpacket;
2557 req->req.actual += len;
2558
2559 /* if we wrote it all, we're usually done */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002560 /* send zlps until the status stage */
2561 if ((req->req.actual == req->req.length) &&
2562 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 mode = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 /* there was nothing to do ... */
2566 } else if (mode == 1)
2567 return;
2568
2569 /* done */
2570 if (mode == 2) {
2571 /* stream endpoints often resubmit/unlink in completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002572 done(ep, req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 /* maybe advance queue to next request */
2575 if (ep->num == 0) {
2576 /* NOTE: net2280 could let gadget driver start the
2577 * status stage later. since not all controllers let
2578 * them control that, the api doesn't (yet) allow it.
2579 */
2580 if (!ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002581 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 req = NULL;
2583 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002584 if (!list_empty(&ep->queue) && !ep->stopped)
2585 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 struct net2280_request, queue);
2587 else
2588 req = NULL;
2589 if (req && !ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002590 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
2592 }
2593
2594 /* is there a buffer for the next packet?
2595 * for best streaming performance, make sure there is one.
2596 */
2597 if (req && !ep->stopped) {
2598
2599 /* load IN fifo with next packet (may be zlp) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002600 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002601 write_fifo(ep, &req->req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603}
2604
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002605static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 struct net2280_ep *ep;
2608
2609 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002610 return &dev->ep[0];
2611 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 u8 bEndpointAddress;
2613
2614 if (!ep->desc)
2615 continue;
2616 bEndpointAddress = ep->desc->bEndpointAddress;
2617 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2618 continue;
2619 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2620 return ep;
2621 }
2622 return NULL;
2623}
2624
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002625static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2626{
2627 u32 scratch, fsmvalue;
2628 u32 ack_wait_timeout, state;
2629
2630 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2631 scratch = get_idx_reg(dev->regs, SCRATCH);
2632 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2633 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2634
2635 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2636 (r.bRequestType & USB_DIR_IN)))
2637 return;
2638
2639 /* This is the first Control Read for this connection: */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002640 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002641 /*
2642 * Connection is NOT SS:
2643 * - Connection must be FS or HS.
2644 * - This FSM state should allow workaround software to
2645 * run after the next USB connection.
2646 */
2647 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002648 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002649 goto restore_data_eps;
2650 }
2651
2652 /* Connection is SS: */
2653 for (ack_wait_timeout = 0;
2654 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2655 ack_wait_timeout++) {
2656
2657 state = readl(&dev->plregs->pl_ep_status_1)
2658 & (0xff << STATE);
2659 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2660 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2661 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002662 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002663 break;
2664 }
2665
2666 /*
2667 * We have not yet received host's Data Phase ACK
2668 * - Wait and try again.
2669 */
2670 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2671
2672 continue;
2673 }
2674
2675
2676 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002677 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002678 "to detect SS host's data phase ACK.");
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002679 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002680 "got 0x%2.2x.\n", state >> STATE);
2681 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002682 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002683 "%duSec for Control Read Data Phase ACK\n",
2684 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2685 }
2686
2687restore_data_eps:
2688 /*
2689 * Restore data EPs to their pre-workaround settings (disabled,
2690 * initialized, and other details).
2691 */
2692 defect7374_disable_data_eps(dev);
2693
2694 set_idx_reg(dev->regs, SCRATCH, scratch);
2695
2696 return;
2697}
2698
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002699static void ep_clear_seqnum(struct net2280_ep *ep)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002700{
2701 struct net2280 *dev = ep->dev;
2702 u32 val;
2703 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2704
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002705 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2706 val |= ep_pl[ep->num];
2707 writel(val, &dev->plregs->pl_ep_ctrl);
2708 val |= BIT(SEQUENCE_NUMBER_RESET);
2709 writel(val, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002710
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002711 return;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002712}
2713
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002714static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2715 struct net2280_ep *ep, struct usb_ctrlrequest r)
2716{
2717 int tmp = 0;
2718
2719#define w_value le16_to_cpu(r.wValue)
2720#define w_index le16_to_cpu(r.wIndex)
2721#define w_length le16_to_cpu(r.wLength)
2722
2723 switch (r.bRequest) {
2724 struct net2280_ep *e;
2725 u16 status;
2726
2727 case USB_REQ_SET_CONFIGURATION:
2728 dev->addressed_state = !w_value;
2729 goto usb3_delegate;
2730
2731 case USB_REQ_GET_STATUS:
2732 switch (r.bRequestType) {
2733 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2734 status = dev->wakeup_enable ? 0x02 : 0x00;
Peter Chenc8678d92015-01-28 16:32:35 +08002735 if (dev->gadget.is_selfpowered)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002736 status |= BIT(0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002737 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2738 dev->ltm_enable << 4);
2739 writel(0, &dev->epregs[0].ep_irqenb);
2740 set_fifo_bytecount(ep, sizeof(status));
2741 writel((__force u32) status, &dev->epregs[0].ep_data);
2742 allow_status_338x(ep);
2743 break;
2744
2745 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2746 e = get_ep_by_addr(dev, w_index);
2747 if (!e)
2748 goto do_stall3;
2749 status = readl(&e->regs->ep_rsp) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002750 BIT(CLEAR_ENDPOINT_HALT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002751 writel(0, &dev->epregs[0].ep_irqenb);
2752 set_fifo_bytecount(ep, sizeof(status));
2753 writel((__force u32) status, &dev->epregs[0].ep_data);
2754 allow_status_338x(ep);
2755 break;
2756
2757 default:
2758 goto usb3_delegate;
2759 }
2760 break;
2761
2762 case USB_REQ_CLEAR_FEATURE:
2763 switch (r.bRequestType) {
2764 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2765 if (!dev->addressed_state) {
2766 switch (w_value) {
2767 case USB_DEVICE_U1_ENABLE:
2768 dev->u1_enable = 0;
2769 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002770 ~BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002771 &dev->usb_ext->usbctl2);
2772 allow_status_338x(ep);
2773 goto next_endpoints3;
2774
2775 case USB_DEVICE_U2_ENABLE:
2776 dev->u2_enable = 0;
2777 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002778 ~BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002779 &dev->usb_ext->usbctl2);
2780 allow_status_338x(ep);
2781 goto next_endpoints3;
2782
2783 case USB_DEVICE_LTM_ENABLE:
2784 dev->ltm_enable = 0;
2785 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002786 ~BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002787 &dev->usb_ext->usbctl2);
2788 allow_status_338x(ep);
2789 goto next_endpoints3;
2790
2791 default:
2792 break;
2793 }
2794 }
2795 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2796 dev->wakeup_enable = 0;
2797 writel(readl(&dev->usb->usbctl) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002798 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002799 &dev->usb->usbctl);
2800 allow_status_338x(ep);
2801 break;
2802 }
2803 goto usb3_delegate;
2804
2805 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2806 e = get_ep_by_addr(dev, w_index);
2807 if (!e)
2808 goto do_stall3;
2809 if (w_value != USB_ENDPOINT_HALT)
2810 goto do_stall3;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002811 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002812 /*
2813 * Workaround for SS SeqNum not cleared via
2814 * Endpoint Halt (Clear) bit. select endpoint
2815 */
2816 ep_clear_seqnum(e);
2817 clear_halt(e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002818 if (!list_empty(&e->queue) && e->td_dma)
2819 restart_dma(e);
2820 allow_status(ep);
2821 ep->stopped = 1;
2822 break;
2823
2824 default:
2825 goto usb3_delegate;
2826 }
2827 break;
2828 case USB_REQ_SET_FEATURE:
2829 switch (r.bRequestType) {
2830 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2831 if (!dev->addressed_state) {
2832 switch (w_value) {
2833 case USB_DEVICE_U1_ENABLE:
2834 dev->u1_enable = 1;
2835 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002836 BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002837 &dev->usb_ext->usbctl2);
2838 allow_status_338x(ep);
2839 goto next_endpoints3;
2840
2841 case USB_DEVICE_U2_ENABLE:
2842 dev->u2_enable = 1;
2843 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002844 BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002845 &dev->usb_ext->usbctl2);
2846 allow_status_338x(ep);
2847 goto next_endpoints3;
2848
2849 case USB_DEVICE_LTM_ENABLE:
2850 dev->ltm_enable = 1;
2851 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002852 BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002853 &dev->usb_ext->usbctl2);
2854 allow_status_338x(ep);
2855 goto next_endpoints3;
2856 default:
2857 break;
2858 }
2859 }
2860
2861 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2862 dev->wakeup_enable = 1;
2863 writel(readl(&dev->usb->usbctl) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002864 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002865 &dev->usb->usbctl);
2866 allow_status_338x(ep);
2867 break;
2868 }
2869 goto usb3_delegate;
2870
2871 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2872 e = get_ep_by_addr(dev, w_index);
2873 if (!e || (w_value != USB_ENDPOINT_HALT))
2874 goto do_stall3;
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002875 ep->stopped = 1;
2876 if (ep->num == 0)
2877 ep->dev->protocol_stall = 1;
2878 else {
2879 if (ep->dma)
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01002880 abort_dma(ep);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002881 set_halt(ep);
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002882 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002883 allow_status_338x(ep);
2884 break;
2885
2886 default:
2887 goto usb3_delegate;
2888 }
2889
2890 break;
2891 default:
2892
2893usb3_delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002894 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002895 r.bRequestType, r.bRequest,
2896 w_value, w_index, w_length,
2897 readl(&ep->cfg->ep_cfg));
2898
2899 ep->responded = 0;
2900 spin_unlock(&dev->lock);
2901 tmp = dev->driver->setup(&dev->gadget, &r);
2902 spin_lock(&dev->lock);
2903 }
2904do_stall3:
2905 if (tmp < 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002906 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002907 r.bRequestType, r.bRequest, tmp);
2908 dev->protocol_stall = 1;
2909 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002910 set_halt(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002911 }
2912
2913next_endpoints3:
2914
2915#undef w_value
2916#undef w_index
2917#undef w_length
2918
2919 return;
2920}
2921
Mian Yousaf Kaukaba09e23f2015-05-16 22:33:35 +02002922static void usb338x_handle_ep_intr(struct net2280 *dev, u32 stat0)
2923{
2924 u32 index;
2925 u32 bit;
2926
2927 for (index = 0; index < ARRAY_SIZE(ep_bit); index++) {
2928 bit = BIT(ep_bit[index]);
2929
2930 if (!stat0)
2931 break;
2932
2933 if (!(stat0 & bit))
2934 continue;
2935
2936 stat0 &= ~bit;
2937
2938 handle_ep_small(&dev->ep[index]);
2939 }
2940}
2941
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002942static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 struct net2280_ep *ep;
2945 u32 num, scratch;
2946
2947 /* most of these don't need individual acks */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002948 stat &= ~BIT(INTA_ASSERTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (!stat)
2950 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002951 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 /* starting a control request? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002954 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 union {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002956 u32 raw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct usb_ctrlrequest r;
2958 } u;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002959 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 struct net2280_request *req;
2961
2962 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002963 u32 val = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002964 if (val & BIT(SUPER_SPEED)) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002965 dev->gadget.speed = USB_SPEED_SUPER;
2966 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2967 EP0_SS_MAX_PACKET_SIZE);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002968 } else if (val & BIT(HIGH_SPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 dev->gadget.speed = USB_SPEED_HIGH;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002970 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2971 EP0_HS_MAX_PACKET_SIZE);
2972 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 dev->gadget.speed = USB_SPEED_FULL;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002974 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2975 EP0_HS_MAX_PACKET_SIZE);
2976 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002977 net2280_led_speed(dev, dev->gadget.speed);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002978 ep_dbg(dev, "%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002979 usb_speed_string(dev->gadget.speed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002982 ep = &dev->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 ep->irqs++;
2984
2985 /* make sure any leftover request state is cleared */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002986 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002987 while (!list_empty(&ep->queue)) {
2988 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct net2280_request, queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002990 done(ep, req, (req->req.actual == req->req.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 ? 0 : -EPROTO);
2992 }
2993 ep->stopped = 0;
2994 dev->protocol_stall = 0;
Ricardo Ribalda Delgado5d1b6842014-11-28 14:50:53 +01002995 if (!(dev->quirks & PLX_SUPERSPEED)) {
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002996 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002997 tmp = BIT(FIFO_OVERFLOW) |
2998 BIT(FIFO_UNDERFLOW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002999 else
3000 tmp = 0;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003001
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003002 writel(tmp | BIT(TIMEOUT) |
3003 BIT(USB_STALL_SENT) |
3004 BIT(USB_IN_NAK_SENT) |
3005 BIT(USB_IN_ACK_RCVD) |
3006 BIT(USB_OUT_PING_NAK_SENT) |
3007 BIT(USB_OUT_ACK_SENT) |
3008 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
3009 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
3010 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
3011 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
3012 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003013 BIT(DATA_IN_TOKEN_INTERRUPT),
3014 &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003015 }
3016 u.raw[0] = readl(&dev->usb->setup0123);
3017 u.raw[1] = readl(&dev->usb->setup4567);
David Brownell901b3d72006-09-02 03:13:45 -07003018
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003019 cpu_to_le32s(&u.raw[0]);
3020 cpu_to_le32s(&u.raw[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003022 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003023 defect7374_workaround(dev, u.r);
3024
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003025 tmp = 0;
3026
David Brownell01ee7d72007-05-25 20:40:14 -07003027#define w_value le16_to_cpu(u.r.wValue)
3028#define w_index le16_to_cpu(u.r.wIndex)
3029#define w_length le16_to_cpu(u.r.wLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 /* ack the irq */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003032 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
3033 stat ^= BIT(SETUP_PACKET_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 /* watch control traffic at the token level, and force
3036 * synchronization before letting the status stage happen.
3037 * FIXME ignore tokens we'll NAK, until driver responds.
3038 * that'll mean a lot less irqs for some drivers.
3039 */
3040 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
3041 if (ep->is_in) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003042 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
3043 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3044 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003045 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 } else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003047 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
3048 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3049 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003050 writel(scratch, &dev->epregs[0].ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 /* we made the hardware handle most lowlevel requests;
3053 * everything else goes uplevel to the gadget code.
3054 */
Alan Stern1f26e282006-11-16 10:16:00 -05003055 ep->responded = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003056
3057 if (dev->gadget.speed == USB_SPEED_SUPER) {
3058 handle_stat0_irqs_superspeed(dev, ep, u.r);
3059 goto next_endpoints;
3060 }
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 switch (u.r.bRequest) {
3063 case USB_REQ_GET_STATUS: {
3064 struct net2280_ep *e;
David Brownell320f3452005-05-07 13:05:18 -07003065 __le32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067 /* hw handles device and interface status */
3068 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
3069 goto delegate;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003070 e = get_ep_by_addr(dev, w_index);
3071 if (!e || w_length > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 goto do_stall;
3073
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003074 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003075 status = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003077 status = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 /* don't bother with a request object! */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003080 writel(0, &dev->epregs[0].ep_irqenb);
3081 set_fifo_bytecount(ep, w_length);
3082 writel((__force u32)status, &dev->epregs[0].ep_data);
3083 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003084 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 goto next_endpoints;
3086 }
3087 break;
3088 case USB_REQ_CLEAR_FEATURE: {
3089 struct net2280_ep *e;
3090
3091 /* hw handles device features */
3092 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3093 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003094 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003096 e = get_ep_by_addr(dev, w_index);
3097 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003099 if (e->wedged) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003100 ep_vdbg(dev, "%s wedged, halt not cleared\n",
Alan Stern80661342008-08-14 15:49:11 -04003101 ep->ep.name);
3102 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003103 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Alan Stern80661342008-08-14 15:49:11 -04003104 clear_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003105 if ((ep->dev->quirks & PLX_SUPERSPEED) &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003106 !list_empty(&e->queue) && e->td_dma)
3107 restart_dma(e);
Alan Stern80661342008-08-14 15:49:11 -04003108 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003109 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 goto next_endpoints;
3111 }
3112 break;
3113 case USB_REQ_SET_FEATURE: {
3114 struct net2280_ep *e;
3115
3116 /* hw handles device features */
3117 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3118 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003119 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003121 e = get_ep_by_addr(dev, w_index);
3122 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003124 if (e->ep.name == ep0name)
3125 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003126 set_halt(e);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003127 if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003128 abort_dma(e);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003129 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003130 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 goto next_endpoints;
3132 }
3133 break;
3134 default:
3135delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003136 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 "ep_cfg %08x\n",
3138 u.r.bRequestType, u.r.bRequest,
David Brownell320f3452005-05-07 13:05:18 -07003139 w_value, w_index, w_length,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003140 readl(&ep->cfg->ep_cfg));
Alan Stern1f26e282006-11-16 10:16:00 -05003141 ep->responded = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003142 spin_unlock(&dev->lock);
3143 tmp = dev->driver->setup(&dev->gadget, &u.r);
3144 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
3146
3147 /* stall ep0 on error */
3148 if (tmp < 0) {
3149do_stall:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003150 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 u.r.bRequestType, u.r.bRequest, tmp);
3152 dev->protocol_stall = 1;
3153 }
3154
3155 /* some in/out token irq should follow; maybe stall then.
3156 * driver must queue a request (even zlp) or halt ep0
3157 * before the host times out.
3158 */
3159 }
3160
David Brownell320f3452005-05-07 13:05:18 -07003161#undef w_value
3162#undef w_index
3163#undef w_length
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165next_endpoints:
Mian Yousaf Kaukaba09e23f2015-05-16 22:33:35 +02003166 if ((dev->quirks & PLX_SUPERSPEED) && dev->enhanced_mode) {
3167 u32 mask = (BIT(ENDPOINT_0_INTERRUPT) |
3168 USB3380_IRQSTAT0_EP_INTR_MASK_IN |
3169 USB3380_IRQSTAT0_EP_INTR_MASK_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Mian Yousaf Kaukaba09e23f2015-05-16 22:33:35 +02003171 if (stat & mask) {
3172 usb338x_handle_ep_intr(dev, stat & mask);
3173 stat &= ~mask;
3174 }
3175 } else {
3176 /* endpoint data irq ? */
3177 scratch = stat & 0x7f;
3178 stat &= ~0x7f;
3179 for (num = 0; scratch; num++) {
3180 u32 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Mian Yousaf Kaukaba09e23f2015-05-16 22:33:35 +02003182 /* do this endpoint's FIFO and queue need tending? */
3183 t = BIT(num);
3184 if ((scratch & t) == 0)
3185 continue;
3186 scratch ^= t;
3187
3188 ep = &dev->ep[num];
3189 handle_ep_small(ep);
3190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192
3193 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003194 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003197#define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3198 BIT(DMA_C_INTERRUPT) | \
3199 BIT(DMA_B_INTERRUPT) | \
3200 BIT(DMA_A_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201#define PCI_ERROR_INTERRUPTS ( \
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003202 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3203 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3204 BIT(PCI_RETRY_ABORT_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003206static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
Felipe Balbiccf5fb62015-02-27 09:44:51 -06003207__releases(dev->lock)
3208__acquires(dev->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
3210 struct net2280_ep *ep;
3211 u32 tmp, num, mask, scratch;
3212
3213 /* after disconnect there's nothing else to do! */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003214 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3215 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03003218 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
David Brownell901b3d72006-09-02 03:13:45 -07003219 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 * only indicates a change in the reset state).
3221 */
3222 if (stat & tmp) {
Alan Sternb611e422014-11-06 14:27:56 +08003223 bool reset = false;
3224 bool disconnect = false;
3225
3226 /*
3227 * Ignore disconnects and resets if the speed hasn't been set.
3228 * VBUS can bounce and there's always an initial reset.
3229 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003230 writel(tmp, &dev->regs->irqstat1);
Alan Sternb611e422014-11-06 14:27:56 +08003231 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3232 if ((stat & BIT(VBUS_INTERRUPT)) &&
3233 (readl(&dev->usb->usbctl) &
3234 BIT(VBUS_PIN)) == 0) {
3235 disconnect = true;
3236 ep_dbg(dev, "disconnect %s\n",
3237 dev->driver->driver.name);
3238 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3239 (readl(&dev->usb->usbstat) & mask)
3240 == 0) {
3241 reset = true;
3242 ep_dbg(dev, "reset %s\n",
3243 dev->driver->driver.name);
3244 }
3245
3246 if (disconnect || reset) {
3247 stop_activity(dev, dev->driver);
3248 ep0_start(dev);
3249 spin_unlock(&dev->lock);
3250 if (reset)
3251 usb_gadget_udc_reset
3252 (&dev->gadget, dev->driver);
3253 else
3254 (dev->driver->disconnect)
3255 (&dev->gadget);
3256 spin_lock(&dev->lock);
3257 return;
3258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 }
3260 stat &= ~tmp;
3261
3262 /* vBUS can bounce ... one of many reasons to ignore the
3263 * notion of hotplug events on bus connect/disconnect!
3264 */
3265 if (!stat)
3266 return;
3267 }
3268
3269 /* NOTE: chip stays in PCI D0 state for now, but it could
3270 * enter D1 to save more power
3271 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003272 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (stat & tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003274 writel(tmp, &dev->regs->irqstat1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003275 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (dev->driver->suspend)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003277 dev->driver->suspend(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (!enable_suspend)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003279 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 } else {
3281 if (dev->driver->resume)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003282 dev->driver->resume(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /* at high speed, note erratum 0133 */
3284 }
3285 stat &= ~tmp;
3286 }
3287
3288 /* clear any other status/irqs */
3289 if (stat)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003290 writel(stat, &dev->regs->irqstat1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 /* some status we can just ignore */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003293 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003294 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3295 BIT(SUSPEND_REQUEST_INTERRUPT) |
3296 BIT(RESUME_INTERRUPT) |
3297 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003298 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003299 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3300 BIT(RESUME_INTERRUPT) |
3301 BIT(SOF_DOWN_INTERRUPT) |
3302 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (!stat)
3305 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003306 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
3308 /* DMA status, for ep-{a,b,c,d} */
3309 scratch = stat & DMA_INTERRUPTS;
3310 stat &= ~DMA_INTERRUPTS;
3311 scratch >>= 9;
3312 for (num = 0; scratch; num++) {
3313 struct net2280_dma_regs __iomem *dma;
3314
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003315 tmp = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if ((tmp & scratch) == 0)
3317 continue;
3318 scratch ^= tmp;
3319
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003320 ep = &dev->ep[num + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 dma = ep->dma;
3322
3323 if (!dma)
3324 continue;
3325
3326 /* clear ep's dma status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003327 tmp = readl(&dma->dmastat);
3328 writel(tmp, &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003330 /* dma sync*/
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003331 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003332 u32 r_dmacount = readl(&dma->dmacount);
3333 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003334 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003335 continue;
3336 }
3337
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003338 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3339 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3340 ep->ep.name, tmp);
3341 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003343 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
3345 /* OUT transfers terminate when the data from the
3346 * host is in our memory. Process whatever's done.
3347 * On this path, we know transfer's last packet wasn't
3348 * less than req->length. NAK_OUT_PACKETS may be set,
3349 * or the FIFO may already be holding new packets.
3350 *
3351 * IN transfers can linger in the FIFO for a very
3352 * long time ... we ignore that for now, accounting
3353 * precisely (like PIO does) needs per-packet irqs
3354 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003355 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 /* disable dma on inactive queues; else maybe restart */
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003358 if (!list_empty(&ep->queue)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003359 tmp = readl(&dma->dmactl);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003360 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
3362 ep->irqs++;
3363 }
3364
3365 /* NOTE: there are other PCI errors we might usefully notice.
3366 * if they appear very often, here's where to try recovering.
3367 */
3368 if (stat & PCI_ERROR_INTERRUPTS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003369 ep_err(dev, "pci dma error; stat %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 stat &= ~PCI_ERROR_INTERRUPTS;
3371 /* these are fatal errors, but "maybe" they won't
3372 * happen again ...
3373 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003374 stop_activity(dev, dev->driver);
3375 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 stat = 0;
3377 }
3378
3379 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003380 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003383static irqreturn_t net2280_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
3385 struct net2280 *dev = _dev;
3386
Alan Stern658ad5e2006-04-14 16:44:11 -04003387 /* shared interrupt, not ours */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003388 if ((dev->quirks & PLX_LEGACY) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003389 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
Alan Stern658ad5e2006-04-14 16:44:11 -04003390 return IRQ_NONE;
3391
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003392 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
3394 /* handle disconnect, dma, and more */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003395 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 /* control requests and PIO */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003398 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003400 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003401 /* re-enable interrupt to trigger any possible new interrupt */
3402 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3403 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3404 writel(pciirqenb1, &dev->regs->pciirqenb1);
3405 }
3406
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003407 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 return IRQ_HANDLED;
3410}
3411
3412/*-------------------------------------------------------------------------*/
3413
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003414static void gadget_release(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003416 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003418 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
3421/* tear down the binding between this driver and the pci device */
3422
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003423static void net2280_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003425 struct net2280 *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003427 usb_del_gadget_udc(&dev->gadget);
3428
David Brownell6bea4762006-12-05 03:15:33 -08003429 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431 /* then clean up the resources we allocated during probe() */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003432 net2280_led_shutdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (dev->requests) {
3434 int i;
3435 for (i = 1; i < 5; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003436 if (!dev->ep[i].dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003438 pci_pool_free(dev->requests, dev->ep[i].dummy,
3439 dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003441 pci_pool_destroy(dev->requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
3443 if (dev->got_irq)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003444 free_irq(pdev->irq, dev);
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003445 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003446 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 if (dev->regs)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003448 iounmap(dev->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (dev->region)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003450 release_mem_region(pci_resource_start(pdev, 0),
3451 pci_resource_len(pdev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (dev->enabled)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003453 pci_disable_device(pdev);
3454 device_remove_file(&pdev->dev, &dev_attr_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003456 ep_info(dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
3459/* wrap this driver around the specified device, but
3460 * don't respond over USB until a gadget driver binds to us.
3461 */
3462
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003463static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
3465 struct net2280 *dev;
3466 unsigned long resource, len;
3467 void __iomem *base = NULL;
3468 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 /* alloc, and start init */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003471 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3472 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 retval = -ENOMEM;
3474 goto done;
3475 }
3476
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003477 pci_set_drvdata(pdev, dev);
3478 spin_lock_init(&dev->lock);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003479 dev->quirks = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 dev->pdev = pdev;
3481 dev->gadget.ops = &net2280_ops;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003482 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003483 USB_SPEED_SUPER : USB_SPEED_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 /* the "gadget" abstracts/virtualizes the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 dev->gadget.name = driver_name;
3487
3488 /* now all the pci goodies ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003489 if (pci_enable_device(pdev) < 0) {
3490 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 goto done;
3492 }
3493 dev->enabled = 1;
3494
3495 /* BAR 0 holds all the registers
3496 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3497 * BAR 2 is fifo memory; unused here
3498 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003499 resource = pci_resource_start(pdev, 0);
3500 len = pci_resource_len(pdev, 0);
3501 if (!request_mem_region(resource, len, driver_name)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003502 ep_dbg(dev, "controller already in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 retval = -EBUSY;
3504 goto done;
3505 }
3506 dev->region = 1;
3507
David Brownell901b3d72006-09-02 03:13:45 -07003508 /* FIXME provide firmware download interface to put
3509 * 8051 code into the chip, e.g. to turn on PCI PM.
3510 */
3511
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003512 base = ioremap_nocache(resource, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 if (base == NULL) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003514 ep_dbg(dev, "can't map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 retval = -EFAULT;
3516 goto done;
3517 }
3518 dev->regs = (struct net2280_regs __iomem *) base;
3519 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3520 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3521 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3522 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3523 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3524
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003525 if (dev->quirks & PLX_SUPERSPEED) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003526 u32 fsmvalue;
3527 u32 usbstat;
3528 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3529 (base + 0x00b4);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003530 dev->llregs = (struct usb338x_ll_regs __iomem *)
3531 (base + 0x0700);
3532 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3533 (base + 0x0748);
3534 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3535 (base + 0x077c);
3536 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3537 (base + 0x079c);
3538 dev->plregs = (struct usb338x_pl_regs __iomem *)
3539 (base + 0x0800);
3540 usbstat = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003541 dev->enhanced_mode = !!(usbstat & BIT(11));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003542 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3543 /* put into initial config, link up all endpoints */
3544 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3545 (0xf << DEFECT7374_FSM_FIELD);
3546 /* See if firmware needs to set up for workaround: */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003547 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3548 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003549 writel(0, &dev->usb->usbctl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003550 } else
3551 dev->bug7734_patched = 0;
3552 } else {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003553 dev->enhanced_mode = 0;
3554 dev->n_ep = 7;
3555 /* put into initial config, link up all endpoints */
3556 writel(0, &dev->usb->usbctl);
3557 }
3558
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003559 usb_reset(dev);
3560 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562 /* irq setup after old hardware is cleaned up */
3563 if (!pdev->irq) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003564 ep_err(dev, "No IRQ. Check PCI setup!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 retval = -ENODEV;
3566 goto done;
3567 }
David S. Millerc6387a42006-06-20 01:21:29 -07003568
Ricardo Ribalda Delgado9c864c22014-11-28 14:50:48 +01003569 if (dev->quirks & PLX_SUPERSPEED)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003570 if (pci_enable_msi(pdev))
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003571 ep_err(dev, "Failed to enable MSI mode\n");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003572
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003573 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3574 driver_name, dev)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003575 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 retval = -EBUSY;
3577 goto done;
3578 }
3579 dev->got_irq = 1;
3580
3581 /* DMA setup */
3582 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003583 dev->requests = pci_pool_create("requests", pdev,
3584 sizeof(struct net2280_dma),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 0 /* no alignment requirements */,
3586 0 /* or page-crossing issues */);
3587 if (!dev->requests) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003588 ep_dbg(dev, "can't get request pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 retval = -ENOMEM;
3590 goto done;
3591 }
3592 for (i = 1; i < 5; i++) {
3593 struct net2280_dma *td;
3594
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003595 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3596 &dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 if (!td) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003598 ep_dbg(dev, "can't get dummy %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 retval = -ENOMEM;
3600 goto done;
3601 }
3602 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 td->dmadesc = td->dmaaddr;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003604 dev->ep[i].dummy = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
3606
3607 /* enable lower-overhead pci memory bursts during DMA */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003608 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003609 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3610 /*
3611 * 256 write retries may not be enough...
3612 BIT(PCI_RETRY_ABORT_ENABLE) |
3613 */
3614 BIT(DMA_READ_MULTIPLE_ENABLE) |
3615 BIT(DMA_READ_LINE_ENABLE),
3616 &dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003618 pci_set_master(pdev);
3619 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
3621 /* ... also flushes any posted pci writes */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003622 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
3624 /* done */
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003625 ep_info(dev, "%s\n", driver_desc);
3626 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
David S. Millerc6387a42006-06-20 01:21:29 -07003627 pdev->irq, base, dev->chiprev);
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01003628 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003629 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003630 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3631 if (retval)
3632 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Felipe Balbi2901df62013-02-26 15:15:27 +02003634 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3635 gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003636 if (retval)
3637 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return 0;
3639
3640done:
3641 if (dev)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003642 net2280_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return retval;
3644}
3645
Alan Stern2d61bde2006-05-05 16:23:42 -04003646/* make sure the board is quiescent; otherwise it will continue
3647 * generating IRQs across the upcoming reboot.
3648 */
3649
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003650static void net2280_shutdown(struct pci_dev *pdev)
Alan Stern2d61bde2006-05-05 16:23:42 -04003651{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003652 struct net2280 *dev = pci_get_drvdata(pdev);
Alan Stern2d61bde2006-05-05 16:23:42 -04003653
3654 /* disable IRQs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003655 writel(0, &dev->regs->pciirqenb0);
3656 writel(0, &dev->regs->pciirqenb1);
Alan Stern2d61bde2006-05-05 16:23:42 -04003657
3658 /* disable the pullup so the host will think we're gone */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003659 writel(0, &dev->usb->usbctl);
Alan Stern2f076072013-01-30 16:40:14 -05003660
Alan Stern2d61bde2006-05-05 16:23:42 -04003661}
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664/*-------------------------------------------------------------------------*/
3665
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003666static const struct pci_device_id pci_ids[] = { {
David Brownell901b3d72006-09-02 03:13:45 -07003667 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3668 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003669 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 .device = 0x2280,
3671 .subvendor = PCI_ANY_ID,
3672 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003673 .driver_data = PLX_LEGACY | PLX_2280,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003674 }, {
David Brownell901b3d72006-09-02 03:13:45 -07003675 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3676 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003677 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003678 .device = 0x2282,
3679 .subvendor = PCI_ANY_ID,
3680 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003681 .driver_data = PLX_LEGACY,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003682 },
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003683 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003684 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3685 .class_mask = ~0,
3686 .vendor = PCI_VENDOR_ID_PLX,
3687 .device = 0x3380,
3688 .subvendor = PCI_ANY_ID,
3689 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003690 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003691 },
3692 {
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003693 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3694 .class_mask = ~0,
3695 .vendor = PCI_VENDOR_ID_PLX,
3696 .device = 0x3382,
3697 .subvendor = PCI_ANY_ID,
3698 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003699 .driver_data = PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003700 },
3701{ /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702};
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003703MODULE_DEVICE_TABLE(pci, pci_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
3705/* pci driver glue; this is a "new style" PCI driver module */
3706static struct pci_driver net2280_pci_driver = {
3707 .name = (char *) driver_name,
3708 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710 .probe = net2280_probe,
3711 .remove = net2280_remove,
Alan Stern2d61bde2006-05-05 16:23:42 -04003712 .shutdown = net2280_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
3714 /* FIXME add power management support */
3715};
3716
Ricardo Ribalda Delgado9a028e42014-05-20 18:30:07 +02003717module_pci_driver(net2280_pci_driver);
3718
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003719MODULE_DESCRIPTION(DRIVER_DESC);
3720MODULE_AUTHOR("David Brownell");
3721MODULE_LICENSE("GPL");