blob: cd876bff51067505d4b462b826e842f6a4f8dc25 [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";
Robert Baldygac23c3c32015-07-31 16:00:36 +020077
78#define EP_INFO(_name, _caps) \
79 { \
80 .name = _name, \
81 .caps = _caps, \
82 }
83
84static const struct {
85 const char *name;
86 const struct usb_ep_caps caps;
87} ep_info_dft[] = { /* Default endpoint configuration */
88 EP_INFO(ep0name,
89 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
90 EP_INFO("ep-a",
91 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
92 EP_INFO("ep-b",
93 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
94 EP_INFO("ep-c",
95 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
96 EP_INFO("ep-d",
97 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
98 EP_INFO("ep-e",
99 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
100 EP_INFO("ep-f",
101 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
102 EP_INFO("ep-g",
103 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
104 EP_INFO("ep-h",
105 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
106}, ep_info_adv[] = { /* Endpoints for usb3380 advance mode */
107 EP_INFO(ep0name,
108 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
109 EP_INFO("ep1in",
110 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
111 EP_INFO("ep2out",
112 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
113 EP_INFO("ep3in",
114 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
115 EP_INFO("ep4out",
116 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
117 EP_INFO("ep1out",
118 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
119 EP_INFO("ep2in",
120 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
121 EP_INFO("ep3out",
122 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
123 EP_INFO("ep4in",
124 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Robert Baldygac23c3c32015-07-31 16:00:36 +0200127#undef EP_INFO
Mian Yousaf Kaukaba285f402015-02-02 10:55:23 +0100128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* mode 0 == ep-{a,b,c,d} 1K fifo each
130 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
131 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
132 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200133static ushort fifo_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/* "modprobe net2280 fifo_mode=1" etc */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200136module_param(fifo_mode, ushort, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* enable_suspend -- When enabled, the driver will respond to
139 * USB suspend requests by powering down the NET2280. Otherwise,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300140 * USB suspend requests will be ignored. This is acceptable for
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100141 * self-powered devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
Ricardo Ribalda Delgado00d4db02014-05-20 18:30:06 +0200143static bool enable_suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/* "modprobe net2280 enable_suspend=1" etc */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200146module_param(enable_suspend, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
149
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200150static char *type_string(u8 bmAttributes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
153 case USB_ENDPOINT_XFER_BULK: return "bulk";
154 case USB_ENDPOINT_XFER_ISOC: return "iso";
155 case USB_ENDPOINT_XFER_INT: return "intr";
Joe Perches2b84f922013-10-08 16:01:37 -0700156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return "control";
158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160#include "net2280.h"
161
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200162#define valid_bit cpu_to_le32(BIT(VALID_BIT))
163#define dma_done_ie cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Mian Yousaf Kaukabe6ac4bb2015-05-16 22:33:31 +0200165static void ep_clear_seqnum(struct net2280_ep *ep);
Mian Yousaf Kaukab11bece52015-05-16 22:33:39 +0200166static void stop_activity(struct net2280 *dev,
167 struct usb_gadget_driver *driver);
168static void ep0_start(struct net2280 *dev);
Mian Yousaf Kaukabe6ac4bb2015-05-16 22:33:31 +0200169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*-------------------------------------------------------------------------*/
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200171static inline void enable_pciirqenb(struct net2280_ep *ep)
172{
173 u32 tmp = readl(&ep->dev->regs->pciirqenb0);
174
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200175 if (ep->dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200176 tmp |= BIT(ep->num);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200177 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200178 tmp |= BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200179 writel(tmp, &ep->dev->regs->pciirqenb0);
180
181 return;
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200185net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct net2280 *dev;
188 struct net2280_ep *ep;
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200189 u32 max;
190 u32 tmp = 0;
191 u32 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned long flags;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200193 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100194 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200196 ep = container_of(_ep, struct net2280_ep, ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200197 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100198 desc->bDescriptorType != USB_DT_ENDPOINT) {
199 pr_err("%s: failed at line=%d\n", __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dev = ep->dev;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100203 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
204 ret = -ESHUTDOWN;
205 goto print_err;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* erratum 0119 workaround ties up an endpoint number */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100209 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) {
210 ret = -EDOM;
211 goto print_err;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Tim Harvey5185c9132016-05-23 06:58:41 -0700214 if (dev->quirks & PLX_PCIE) {
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100215 if ((desc->bEndpointAddress & 0x0f) >= 0x0c) {
216 ret = -EDOM;
217 goto print_err;
218 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200219 ep->is_in = !!usb_endpoint_dir_in(desc);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100220 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) {
221 ret = -EINVAL;
222 goto print_err;
223 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200224 }
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* sanity check ep-e/ep-f since their fifos are small */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200227 max = usb_endpoint_maxp(desc) & 0x1fff;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100228 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) {
229 ret = -ERANGE;
230 goto print_err;
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200233 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 _ep->maxpacket = max & 0x7ff;
235 ep->desc = desc;
236
237 /* ep_reset() has already been called */
238 ep->stopped = 0;
Alan Stern80661342008-08-14 15:49:11 -0400239 ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ep->out_overflow = 0;
241
242 /* set speed-dependent max packet; may kick in high bandwidth */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200243 set_max_speed(ep, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* set type, direction, address; reset fifo counters */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200246 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200247
Tim Harvey5185c9132016-05-23 06:58:41 -0700248 if ((dev->quirks & PLX_PCIE) && dev->enhanced_mode) {
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200249 tmp = readl(&ep->cfg->ep_cfg);
250 /* If USB ep number doesn't match hardware ep number */
251 if ((tmp & 0xf) != usb_endpoint_num(desc)) {
252 ret = -EINVAL;
253 spin_unlock_irqrestore(&dev->lock, flags);
254 goto print_err;
255 }
256 if (ep->is_in)
257 tmp &= ~USB3380_EP_CFG_MASK_IN;
258 else
259 tmp &= ~USB3380_EP_CFG_MASK_OUT;
260 }
261 type = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
262 if (type == USB_ENDPOINT_XFER_INT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* erratum 0105 workaround prevents hs NYET */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200264 if (dev->chiprev == 0100 &&
265 dev->gadget.speed == USB_SPEED_HIGH &&
266 !(desc->bEndpointAddress & USB_DIR_IN))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200267 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 &ep->regs->ep_rsp);
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200269 } else if (type == USB_ENDPOINT_XFER_BULK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* catch some particularly blatant driver bugs */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200271 if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
272 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
273 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
274 spin_unlock_irqrestore(&dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100275 ret = -ERANGE;
276 goto print_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 }
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200279 ep->is_iso = (type == USB_ENDPOINT_XFER_ISOC);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200280 /* Enable this endpoint */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200281 if (dev->quirks & PLX_LEGACY) {
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200282 tmp |= type << ENDPOINT_TYPE;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200283 tmp |= desc->bEndpointAddress;
284 /* default full fifo lines */
285 tmp |= (4 << ENDPOINT_BYTE_COUNT);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200286 tmp |= BIT(ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200287 ep->is_in = (tmp & USB_DIR_IN) != 0;
288 } else {
289 /* In Legacy mode, only OUT endpoints are used */
290 if (dev->enhanced_mode && ep->is_in) {
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200291 tmp |= type << IN_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200292 tmp |= BIT(IN_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200293 } else {
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200294 tmp |= type << OUT_ENDPOINT_TYPE;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200295 tmp |= BIT(OUT_ENDPOINT_ENABLE);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200296 tmp |= (ep->is_in << ENDPOINT_DIRECTION);
297 }
298
Mian Yousaf Kaukab463e1042015-05-16 22:33:34 +0200299 tmp |= (4 << ENDPOINT_BYTE_COUNT);
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +0200300 if (!dev->enhanced_mode)
301 tmp |= usb_endpoint_num(desc);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200302 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
303 }
304
305 /* Make sure all the registers are written before ep_rsp*/
306 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* for OUT transfers, block the rx fifo until a read is posted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200310 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200311 else if (!(dev->quirks & PLX_2280)) {
David Brownell901b3d72006-09-02 03:13:45 -0700312 /* Added for 2282, Don't use nak packets on an in endpoint,
313 * this was ignored on 2280
314 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200315 writel(BIT(CLEAR_NAK_OUT_PACKETS) |
316 BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Tim Harvey5185c9132016-05-23 06:58:41 -0700319 if (dev->quirks & PLX_PCIE)
Mian Yousaf Kaukabe6ac4bb2015-05-16 22:33:31 +0200320 ep_clear_seqnum(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200321 writel(tmp, &ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* enable irqs */
324 if (!ep->dma) { /* pio, per-packet */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200325 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200327 tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
328 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200329 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200330 tmp |= readl(&ep->regs->ep_irqenb);
331 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 } else { /* dma, per-request */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200333 tmp = BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200334 tmp |= readl(&dev->regs->pciirqenb1);
335 writel(tmp, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* for short OUT transfers, dma completions can't
338 * advance the queue; do it pio-style, by hand.
339 * NOTE erratum 0112 workaround #2
340 */
341 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200342 tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200343 writel(tmp, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200345 enable_pciirqenb(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 }
348
349 tmp = desc->bEndpointAddress;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200350 ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200351 _ep->name, tmp & 0x0f, DIR_STRING(tmp),
352 type_string(desc->bmAttributes),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ep->dma ? "dma" : "pio", max);
354
355 /* pci writes may still be posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200356 spin_unlock_irqrestore(&dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100357 return ret;
358
359print_err:
360 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200364static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 u32 result;
367
368 do {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200369 result = readl(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (result == ~(u32)0) /* "device unplugged" */
371 return -ENODEV;
372 result &= mask;
373 if (result == done)
374 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200375 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 usec--;
377 } while (usec > 0);
378 return -ETIMEDOUT;
379}
380
David Brownell901b3d72006-09-02 03:13:45 -0700381static const struct usb_ep_ops net2280_ep_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200383static void ep_reset_228x(struct net2280_regs __iomem *regs,
384 struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 u32 tmp;
387
388 ep->desc = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200389 INIT_LIST_HEAD(&ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Robert Baldygae117e742013-12-13 12:23:38 +0100391 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ep->ep.ops = &net2280_ep_ops;
393
394 /* disable the dma, irqs, endpoint... */
395 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200396 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200397 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
398 BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
399 BIT(DMA_ABORT),
400 &ep->dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200402 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200403 tmp &= ~BIT(ep->num);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200404 writel(tmp, &regs->pciirqenb0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200406 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200407 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200408 writel(tmp, &regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200410 writel(0, &ep->regs->ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* init to our chosen defaults, notably so that we NAK OUT
413 * packets until the driver queues a read (+note erratum 0112)
414 */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200415 if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200416 tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
417 BIT(SET_NAK_OUT_PACKETS) |
418 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
419 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100420 } else {
421 /* added for 2282 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200422 tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
423 BIT(CLEAR_NAK_OUT_PACKETS) |
424 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
425 BIT(CLEAR_INTERRUPT_MODE);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (ep->num != 0) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200429 tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
430 BIT(CLEAR_ENDPOINT_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200432 writel(tmp, &ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* scrub most status bits, and flush any fifo state */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200435 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200436 tmp = BIT(FIFO_OVERFLOW) |
437 BIT(FIFO_UNDERFLOW);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100438 else
439 tmp = 0;
440
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200441 writel(tmp | BIT(TIMEOUT) |
442 BIT(USB_STALL_SENT) |
443 BIT(USB_IN_NAK_SENT) |
444 BIT(USB_IN_ACK_RCVD) |
445 BIT(USB_OUT_PING_NAK_SENT) |
446 BIT(USB_OUT_ACK_SENT) |
447 BIT(FIFO_FLUSH) |
448 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
449 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
450 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
451 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
452 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200453 BIT(DATA_IN_TOKEN_INTERRUPT),
454 &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* fifo size is handled separately */
457}
458
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200459static void ep_reset_338x(struct net2280_regs __iomem *regs,
460 struct net2280_ep *ep)
461{
462 u32 tmp, dmastat;
463
464 ep->desc = NULL;
465 INIT_LIST_HEAD(&ep->queue);
466
467 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
468 ep->ep.ops = &net2280_ep_ops;
469
470 /* disable the dma, irqs, endpoint... */
471 if (ep->dma) {
472 writel(0, &ep->dma->dmactl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200473 writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
474 BIT(DMA_PAUSE_DONE_INTERRUPT) |
475 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200476 BIT(DMA_TRANSACTION_DONE_INTERRUPT),
477 /* | BIT(DMA_ABORT), */
478 &ep->dma->dmastat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200479
480 dmastat = readl(&ep->dma->dmastat);
481 if (dmastat == 0x5002) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200482 ep_warn(ep->dev, "The dmastat return = %x!!\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200483 dmastat);
484 writel(0x5a, &ep->dma->dmastat);
485 }
486
487 tmp = readl(&regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200488 tmp &= ~BIT(ep_bit[ep->num]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200489 writel(tmp, &regs->pciirqenb0);
490 } else {
491 if (ep->num < 5) {
492 tmp = readl(&regs->pciirqenb1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200493 tmp &= ~BIT((8 + ep->num)); /* completion */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200494 writel(tmp, &regs->pciirqenb1);
495 }
496 }
497 writel(0, &ep->regs->ep_irqenb);
498
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200499 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
500 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
501 BIT(FIFO_OVERFLOW) |
502 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
503 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
504 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
505 BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
Mian Yousaf Kaukab971fe652015-05-16 22:33:38 +0200506
507 tmp = readl(&ep->cfg->ep_cfg);
508 if (ep->is_in)
509 tmp &= ~USB3380_EP_CFG_MASK_IN;
510 else
511 tmp &= ~USB3380_EP_CFG_MASK_OUT;
512 writel(tmp, &ep->cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200513}
514
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200515static void nuke(struct net2280_ep *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200517static int net2280_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct net2280_ep *ep;
520 unsigned long flags;
521
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200522 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100523 if (!_ep || !ep->desc || _ep->name == ep0name) {
524 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100526 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200527 spin_lock_irqsave(&ep->dev->lock, flags);
528 nuke(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200529
Tim Harvey5185c9132016-05-23 06:58:41 -0700530 if (ep->dev->quirks & PLX_PCIE)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200531 ep_reset_338x(ep->dev->regs, ep);
532 else
533 ep_reset_228x(ep->dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200535 ep_vdbg(ep->dev, "disabled %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ep->dma ? "dma" : "pio", _ep->name);
537
538 /* synch memory views with the device */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200539 (void)readl(&ep->cfg->ep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +0100541 if (!ep->dma && ep->num >= 1 && ep->num <= 4)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200542 ep->dma = &ep->dev->dma[ep->num - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200544 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
546}
547
548/*-------------------------------------------------------------------------*/
549
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200550static struct usb_request
551*net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct net2280_ep *ep;
554 struct net2280_request *req;
555
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100556 if (!_ep) {
557 pr_err("%s: Invalid ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return NULL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100559 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200560 ep = container_of(_ep, struct net2280_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800562 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!req)
564 return NULL;
565
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200566 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* this dma descriptor may be swapped with the previous dummy */
569 if (ep->dma) {
570 struct net2280_dma *td;
571
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200572 td = pci_pool_alloc(ep->dev->requests, gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 &req->td_dma);
574 if (!td) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200575 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NULL;
577 }
578 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 td->dmadesc = td->dmaaddr;
580 req->td = td;
581 }
582 return &req->req;
583}
584
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200585static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct net2280_ep *ep;
588 struct net2280_request *req;
589
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200590 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100591 if (!_ep || !_req) {
592 dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n",
593 __func__, _ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200597 req = container_of(_req, struct net2280_request, req);
598 WARN_ON(!list_empty(&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (req->td)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200600 pci_pool_free(ep->dev->requests, req->td, req->td_dma);
601 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604/*-------------------------------------------------------------------------*/
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/* load a packet into the fifo we use for usb IN transfers.
607 * works for all endpoints.
608 *
609 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
610 * at a time, but this code is simpler because it knows it only writes
611 * one packet. ep-a..ep-d should use dma instead.
612 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200613static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct net2280_ep_regs __iomem *regs = ep->regs;
616 u8 *buf;
617 u32 tmp;
618 unsigned count, total;
619
620 /* INVARIANT: fifo is currently empty. (testable) */
621
622 if (req) {
623 buf = req->buf + req->actual;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200624 prefetch(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 total = req->length - req->actual;
626 } else {
627 total = 0;
628 buf = NULL;
629 }
630
631 /* write just one packet at a time */
632 count = ep->ep.maxpacket;
633 if (count > total) /* min() cannot be used on a bitfield */
634 count = total;
635
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200636 ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ep->ep.name, count,
638 (count != ep->ep.maxpacket) ? " (short)" : "",
639 req);
640 while (count >= 4) {
641 /* NOTE be careful if you try to align these. fifo lines
642 * should normally be full (4 bytes) and successive partial
643 * lines are ok only in certain cases.
644 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200645 tmp = get_unaligned((u32 *)buf);
646 cpu_to_le32s(&tmp);
647 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 buf += 4;
649 count -= 4;
650 }
651
652 /* last fifo entry is "short" unless we wrote a full packet.
653 * also explicitly validate last word in (periodic) transfers
654 * when maxpacket is not a multiple of 4 bytes.
655 */
656 if (count || total < ep->ep.maxpacket) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200657 tmp = count ? get_unaligned((u32 *)buf) : count;
658 cpu_to_le32s(&tmp);
659 set_fifo_bytecount(ep, count & 0x03);
660 writel(tmp, &regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 /* pci writes may still be posted */
664}
665
666/* work around erratum 0106: PCI and USB race over the OUT fifo.
667 * caller guarantees chiprev 0100, out endpoint is NAKing, and
668 * there's no real data in the fifo.
669 *
670 * NOTE: also used in cases where that erratum doesn't apply:
671 * where the host wrote "too much" data to us.
672 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200673static void out_flush(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 u32 __iomem *statp;
676 u32 tmp;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 statp = &ep->regs->ep_stat;
Ricardo Ribalda Delgadod82f3db2014-11-28 14:51:01 +0100679
680 tmp = readl(statp);
681 if (tmp & BIT(NAK_OUT_PACKETS)) {
682 ep_dbg(ep->dev, "%s %s %08x !NAK\n",
683 ep->ep.name, __func__, tmp);
684 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
685 }
686
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200687 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200688 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
689 statp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200690 writel(BIT(FIFO_FLUSH), statp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200691 /* Make sure that stap is written */
692 mb();
693 tmp = readl(statp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200694 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* high speed did bulk NYET; fifo isn't filling */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200696 ep->dev->gadget.speed == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 unsigned usec;
698
699 usec = 50; /* 64 byte bulk/interrupt */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200700 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
701 BIT(USB_OUT_PING_NAK_SENT), usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
703 }
704}
705
706/* unload packet(s) from the fifo we use for usb OUT transfers.
707 * returns true iff the request completed, because of short packet
708 * or the request buffer having filled with full packets.
709 *
710 * for ep-a..ep-d this will read multiple packets out when they
711 * have been accepted.
712 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200713static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct net2280_ep_regs __iomem *regs = ep->regs;
716 u8 *buf = req->req.buf + req->req.actual;
717 unsigned count, tmp, is_short;
718 unsigned cleanup = 0, prevent = 0;
719
720 /* erratum 0106 ... packets coming in during fifo reads might
721 * be incompletely rejected. not all cases have workarounds.
722 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200723 if (ep->dev->chiprev == 0x0100 &&
724 ep->dev->gadget.speed == USB_SPEED_FULL) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200725 udelay(1);
726 tmp = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200727 if ((tmp & BIT(NAK_OUT_PACKETS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 cleanup = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200729 else if ((tmp & BIT(FIFO_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200730 start_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 prevent = 1;
732 }
733 /* else: hope we don't see the problem */
734 }
735
736 /* never overflow the rx buffer. the fifo reads packets until
737 * it sees a short one; we might not be ready for them all.
738 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200739 prefetchw(buf);
740 count = readl(&regs->ep_avail);
741 if (unlikely(count == 0)) {
742 udelay(1);
743 tmp = readl(&ep->regs->ep_stat);
744 count = readl(&regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* handled that data already? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200746 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 0;
748 }
749
750 tmp = req->req.length - req->req.actual;
751 if (count > tmp) {
752 /* as with DMA, data overflow gets flushed */
753 if ((tmp % ep->ep.maxpacket) != 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200754 ep_err(ep->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 "%s out fifo %d bytes, expected %d\n",
756 ep->ep.name, count, tmp);
757 req->req.status = -EOVERFLOW;
758 cleanup = 1;
759 /* NAK_OUT_PACKETS will be set, so flushing is safe;
760 * the next read will start with the next packet
761 */
762 } /* else it's a ZLP, no worries */
763 count = tmp;
764 }
765 req->req.actual += count;
766
767 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
768
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200769 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 -0700770 ep->ep.name, count, is_short ? " (short)" : "",
771 cleanup ? " flush" : "", prevent ? " nak" : "",
772 req, req->req.actual, req->req.length);
773
774 while (count >= 4) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200775 tmp = readl(&regs->ep_data);
776 cpu_to_le32s(&tmp);
777 put_unaligned(tmp, (u32 *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 buf += 4;
779 count -= 4;
780 }
781 if (count) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200782 tmp = readl(&regs->ep_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* LE conversion is implicit here: */
784 do {
785 *buf++ = (u8) tmp;
786 tmp >>= 8;
787 } while (--count);
788 }
789 if (cleanup)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200790 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (prevent) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200792 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200793 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200796 return is_short || ((req->req.actual == req->req.length) &&
797 !req->req.zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800/* fill out dma descriptor to match a given request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200801static void fill_dma_desc(struct net2280_ep *ep,
802 struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct net2280_dma *td = req->td;
805 u32 dmacount = req->req.length;
806
807 /* don't let DMA continue after a short OUT packet,
808 * so overruns can't affect the next transfer.
809 * in case of overruns on max-size packets, we can't
810 * stop the fifo from filling but we can flush it.
811 */
812 if (ep->is_in)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200813 dmacount |= BIT(DMA_DIRECTION);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200814 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200815 !(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200816 dmacount |= BIT(END_OF_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 req->valid = valid;
819 if (valid)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200820 dmacount |= BIT(VALID_BIT);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100821 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* td->dmadesc = previously set by caller */
824 td->dmaaddr = cpu_to_le32 (req->req.dma);
825
826 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200827 wmb();
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700828 td->dmacount = cpu_to_le32(dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831static const u32 dmactl_default =
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200832 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
833 BIT(DMA_CLEAR_COUNT_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* erratum 0116 workaround part 1 (use POLLING) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200835 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
836 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
837 BIT(DMA_VALID_BIT_ENABLE) |
838 BIT(DMA_SCATTER_GATHER_ENABLE) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* erratum 0116 workaround part 2 (no AUTOSTART) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200840 BIT(DMA_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200842static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200844 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200847static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200849 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200850 spin_stop_dma(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200853static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct net2280_dma_regs __iomem *dma = ep->dma;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200856 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +0200858 if (!(ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200859 tmp |= BIT(END_OF_CHAIN);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +0100860
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200861 writel(tmp, &dma->dmacount);
862 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200864 writel(td_dma, &dma->dmadesc);
Tim Harvey5185c9132016-05-23 06:58:41 -0700865 if (ep->dev->quirks & PLX_PCIE)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200866 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200867 writel(dmactl, &dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200870 (void) readl(&ep->dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200872 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (!ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200875 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200878static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 u32 tmp;
881 struct net2280_dma_regs __iomem *dma = ep->dma;
882
883 /* FIXME can't use DMA for ZLPs */
884
885 /* on this path we "know" there's no dma active (yet) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200886 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200887 writel(0, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /* previous OUT packet might have been short */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200890 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
891 BIT(NAK_OUT_PACKETS))) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200892 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 &ep->regs->ep_stat);
894
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200895 tmp = readl(&ep->regs->ep_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200897 writel(readl(&dma->dmastat), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* transfer all/some fifo data */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200900 writel(req->req.dma, &dma->dmaaddr);
901 tmp = min(tmp, req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* dma irq, faking scatterlist status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200904 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +0200905 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
906 &dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 req->td->dmadesc = 0;
908 req->valid = 1;
909
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200910 writel(BIT(DMA_ENABLE), &dma->dmactl);
911 writel(BIT(DMA_START), &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return;
913 }
914 }
915
916 tmp = dmactl_default;
917
918 /* force packet boundaries between dma requests, but prevent the
919 * controller from automagically writing a last "short" packet
920 * (zero length) unless the driver explicitly said to do that.
921 */
922 if (ep->is_in) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200923 if (likely((req->req.length % ep->ep.maxpacket) ||
924 req->req.zero)){
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +0200925 tmp |= BIT(DMA_FIFO_VALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ep->in_fifo_validate = 1;
927 } else
928 ep->in_fifo_validate = 0;
929 }
930
931 /* init req->td, pointing to the current dummy */
932 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200933 fill_dma_desc(ep, req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +0100935 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200937 start_queue(ep, tmp, req->td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200941queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct net2280_dma *end;
944 dma_addr_t tmp;
945
946 /* swap new dummy for old, link; fill and maybe activate */
947 end = ep->dummy;
948 ep->dummy = req->td;
949 req->td = end;
950
951 tmp = ep->td_dma;
952 ep->td_dma = req->td_dma;
953 req->td_dma = tmp;
954
955 end->dmadesc = cpu_to_le32 (ep->td_dma);
956
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200957 fill_dma_desc(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200961done(struct net2280_ep *ep, struct net2280_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 struct net2280 *dev;
964 unsigned stopped = ep->stopped;
965
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200966 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (req->req.status == -EINPROGRESS)
969 req->req.status = status;
970 else
971 status = req->req.status;
972
973 dev = ep->dev;
Felipe Balbiae4d7932011-12-19 12:09:56 +0200974 if (ep->dma)
975 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (status && status != -ESHUTDOWN)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +0200978 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 ep->ep.name, &req->req, status,
980 req->req.actual, req->req.length);
981
982 /* don't modify queue heads during completion callback */
983 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200984 spin_unlock(&dev->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200985 usb_gadget_giveback_request(&ep->ep, &req->req);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200986 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ep->stopped = stopped;
988}
989
990/*-------------------------------------------------------------------------*/
991
992static int
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +0200993net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct net2280_request *req;
996 struct net2280_ep *ep;
997 struct net2280 *dev;
998 unsigned long flags;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +0100999 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* we always require a cpu-view buffer, so that we can
1002 * always use pio (as fallback or whatever).
1003 */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001004 ep = container_of(_ep, struct net2280_ep, ep);
1005 if (!_ep || (!ep->desc && ep->num != 0)) {
1006 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1007 return -EINVAL;
1008 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001009 req = container_of(_req, struct net2280_request, req);
1010 if (!_req || !_req->complete || !_req->buf ||
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001011 !list_empty(&req->queue)) {
1012 ret = -EINVAL;
1013 goto print_err;
1014 }
1015 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
1016 ret = -EDOM;
1017 goto print_err;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 dev = ep->dev;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001020 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1021 ret = -ESHUTDOWN;
1022 goto print_err;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /* FIXME implement PIO fallback for ZLPs with DMA */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001026 if (ep->dma && _req->length == 0) {
1027 ret = -EOPNOTSUPP;
1028 goto print_err;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /* set up dma mapping in case the caller didn't */
Felipe Balbiae4d7932011-12-19 12:09:56 +02001032 if (ep->dma) {
Felipe Balbiae4d7932011-12-19 12:09:56 +02001033 ret = usb_gadget_map_request(&dev->gadget, _req,
1034 ep->is_in);
1035 if (ret)
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001036 goto print_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001039 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001042 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 _req->status = -EINPROGRESS;
1045 _req->actual = 0;
1046
1047 /* kickstart this i/o queue? */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +01001048 if (list_empty(&ep->queue) && !ep->stopped &&
Tim Harvey5185c9132016-05-23 06:58:41 -07001049 !((dev->quirks & PLX_PCIE) && ep->dma &&
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +01001050 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /* use DMA if the endpoint supports it, else pio */
Ricardo Ribalda Delgado485f44d2014-11-28 14:50:56 +01001053 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001054 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 else {
1056 /* maybe there's no control data, just status ack */
1057 if (ep->num == 0 && _req->length == 0) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001058 allow_status(ep);
1059 done(ep, req, 0);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001060 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto done;
1062 }
1063
1064 /* PIO ... stuff the fifo, or unblock it. */
1065 if (ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001066 write_fifo(ep, _req);
1067 else if (list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 u32 s;
1069
1070 /* OUT FIFO might have packet(s) buffered */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001071 s = readl(&ep->regs->ep_stat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001072 if ((s & BIT(FIFO_EMPTY)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* note: _req->short_not_ok is
1074 * ignored here since PIO _always_
1075 * stops queue advance here, and
1076 * _req->status doesn't change for
1077 * short reads (only _req->actual)
1078 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001079 if (read_fifo(ep, req) &&
1080 ep->num == 0) {
1081 done(ep, req, 0);
1082 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* don't queue it */
1084 req = NULL;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001085 } else if (read_fifo(ep, req) &&
1086 ep->num != 0) {
1087 done(ep, req, 0);
1088 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001090 s = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
1093 /* don't NAK, let the fifo fill */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001094 if (req && (s & BIT(NAK_OUT_PACKETS)))
1095 writel(BIT(CLEAR_NAK_OUT_PACKETS),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 &ep->regs->ep_rsp);
1097 }
1098 }
1099
1100 } else if (ep->dma) {
1101 int valid = 1;
1102
1103 if (ep->is_in) {
1104 int expect;
1105
1106 /* preventing magic zlps is per-engine state, not
1107 * per-transfer; irq logic must recover hiccups.
1108 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001109 expect = likely(req->req.zero ||
1110 (req->req.length % ep->ep.maxpacket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (expect != ep->in_fifo_validate)
1112 valid = 0;
1113 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001114 queue_dma(ep, req, valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 } /* else the irq handler advances the queue. */
1117
Alan Stern1f26e282006-11-16 10:16:00 -05001118 ep->responded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001120 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001122 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /* pci writes may still be posted */
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001125 return ret;
1126
1127print_err:
1128 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1129 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132static inline void
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001133dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1134 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001137 done(ep, req, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03001140static int scan_dma_completions(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03001142 int num_completed = 0;
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* only look at descriptors that were "naturally" retired,
1145 * so fifo and list head state won't matter
1146 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001147 while (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct net2280_request *req;
1149 u32 tmp;
1150
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001151 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct net2280_request, queue);
1153 if (!req->valid)
1154 break;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001155 rmb();
1156 tmp = le32_to_cpup(&req->td->dmacount);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001157 if ((tmp & BIT(VALID_BIT)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159
1160 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1161 * cases where DMA must be aborted; this code handles
1162 * all non-abort DMA completions.
1163 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001164 if (unlikely(req->td->dmadesc == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* paranoia */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001166 tmp = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (tmp & DMA_BYTE_COUNT_MASK)
1168 break;
1169 /* single transfer mode */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001170 dma_done(ep, req, tmp, 0);
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03001171 num_completed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02001173 } else if (!ep->is_in &&
Ricardo Ribalda Delgado43780aa2014-11-28 14:51:00 +01001174 (req->req.length % ep->ep.maxpacket) &&
Tim Harvey5185c9132016-05-23 06:58:41 -07001175 !(ep->dev->quirks & PLX_PCIE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Mario Schuknecht18a4e652014-11-16 21:21:45 +01001177 tmp = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /* AVOID TROUBLE HERE by not issuing short reads from
1179 * your gadget driver. That helps avoids errata 0121,
1180 * 0122, and 0124; not all cases trigger the warning.
1181 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001182 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001183 ep_warn(ep->dev, "%s lost packet sync!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ep->ep.name);
1185 req->req.status = -EOVERFLOW;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001186 } else {
1187 tmp = readl(&ep->regs->ep_avail);
1188 if (tmp) {
1189 /* fifo gets flushed later */
1190 ep->out_overflow = 1;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001191 ep_dbg(ep->dev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001192 "%s dma, discard %d len %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ep->ep.name, tmp,
1194 req->req.length);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001195 req->req.status = -EOVERFLOW;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001199 dma_done(ep, req, tmp, 0);
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03001200 num_completed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03001202
1203 return num_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001206static void restart_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 struct net2280_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (ep->stopped)
1211 return;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001212 req = list_entry(ep->queue.next, struct net2280_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01001214 start_dma(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01001217static void abort_dma(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 /* abort the current transfer */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001220 if (likely(!list_empty(&ep->queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* FIXME work around errata 0121, 0122, 0124 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001222 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001223 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 } else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001225 stop_dma(ep->dma);
1226 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229/* dequeue ALL requests */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001230static void nuke(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct net2280_request *req;
1233
1234 /* called with spinlock held */
1235 ep->stopped = 1;
1236 if (ep->dma)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001237 abort_dma(ep);
1238 while (!list_empty(&ep->queue)) {
1239 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 struct net2280_request,
1241 queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001242 done(ep, req, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244}
1245
1246/* dequeue JUST ONE request */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001247static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct net2280_ep *ep;
1250 struct net2280_request *req;
1251 unsigned long flags;
1252 u32 dmactl;
1253 int stopped;
1254
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001255 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001256 if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1257 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1258 __func__, _ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001262 spin_lock_irqsave(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 stopped = ep->stopped;
1264
1265 /* quiesce dma while we patch the queue */
1266 dmactl = 0;
1267 ep->stopped = 1;
1268 if (ep->dma) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001269 dmactl = readl(&ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* WARNING erratum 0127 may kick in ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001271 stop_dma(ep->dma);
1272 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
1275 /* make sure it's still queued on this endpoint */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001276 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (&req->req == _req)
1278 break;
1279 }
1280 if (&req->req != _req) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001281 spin_unlock_irqrestore(&ep->dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001282 dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
1283 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EINVAL;
1285 }
1286
1287 /* queue head may be partially complete. */
1288 if (ep->queue.next == &req->queue) {
1289 if (ep->dma) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001290 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 _req->status = -ECONNRESET;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001292 abort_dma(ep);
1293 if (likely(ep->queue.next == &req->queue)) {
1294 /* NOTE: misreports single-transfer mode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 req->td->dmacount = 0; /* invalidate */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001296 dma_done(ep, req,
1297 readl(&ep->dma->dmacount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 -ECONNRESET);
1299 }
1300 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001301 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001302 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
1307 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001308 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 ep->stopped = stopped;
1310
1311 if (ep->dma) {
1312 /* turn off dma on inactive queues */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001313 if (list_empty(&ep->queue))
1314 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 else if (!ep->stopped) {
1316 /* resume current request, or start new one */
1317 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001318 writel(dmactl, &ep->dma->dmactl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001320 start_dma(ep, list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct net2280_request, queue));
1322 }
1323 }
1324
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001325 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return 0;
1327}
1328
1329/*-------------------------------------------------------------------------*/
1330
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001331static int net2280_fifo_status(struct usb_ep *_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333static int
Alan Stern80661342008-08-14 15:49:11 -04001334net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 struct net2280_ep *ep;
1337 unsigned long flags;
1338 int retval = 0;
1339
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001340 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001341 if (!_ep || (!ep->desc && ep->num != 0)) {
1342 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001344 }
1345 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1346 retval = -ESHUTDOWN;
1347 goto print_err;
1348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001350 == USB_ENDPOINT_XFER_ISOC) {
1351 retval = -EINVAL;
1352 goto print_err;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001355 spin_lock_irqsave(&ep->dev->lock, flags);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001356 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 retval = -EAGAIN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001358 goto print_unlock;
1359 } else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 retval = -EAGAIN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001361 goto print_unlock;
1362 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02001363 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
Alan Stern80661342008-08-14 15:49:11 -04001364 value ? "set" : "clear",
1365 wedged ? "wedge" : "halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /* set/clear, then synch memory views with the device */
1367 if (value) {
1368 if (ep->num == 0)
1369 ep->dev->protocol_stall = 1;
1370 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001371 set_halt(ep);
Alan Stern80661342008-08-14 15:49:11 -04001372 if (wedged)
1373 ep->wedged = 1;
1374 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001375 clear_halt(ep);
Tim Harvey5185c9132016-05-23 06:58:41 -07001376 if (ep->dev->quirks & PLX_PCIE &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001377 !list_empty(&ep->queue) && ep->td_dma)
1378 restart_dma(ep);
Alan Stern80661342008-08-14 15:49:11 -04001379 ep->wedged = 0;
1380 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001381 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001383 spin_unlock_irqrestore(&ep->dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 return retval;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001386
1387print_unlock:
1388 spin_unlock_irqrestore(&ep->dev->lock, flags);
1389print_err:
1390 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1391 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001394static int net2280_set_halt(struct usb_ep *_ep, int value)
Alan Stern80661342008-08-14 15:49:11 -04001395{
1396 return net2280_set_halt_and_wedge(_ep, value, 0);
1397}
1398
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001399static int net2280_set_wedge(struct usb_ep *_ep)
Alan Stern80661342008-08-14 15:49:11 -04001400{
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001401 if (!_ep || _ep->name == ep0name) {
1402 pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
Alan Stern80661342008-08-14 15:49:11 -04001403 return -EINVAL;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001404 }
Alan Stern80661342008-08-14 15:49:11 -04001405 return net2280_set_halt_and_wedge(_ep, 1, 1);
1406}
1407
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001408static int net2280_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct net2280_ep *ep;
1411 u32 avail;
1412
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001413 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001414 if (!_ep || (!ep->desc && ep->num != 0)) {
1415 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -ENODEV;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001417 }
1418 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1419 dev_err(&ep->dev->pdev->dev,
1420 "%s: Invalid driver=%p or speed=%d\n",
1421 __func__, ep->dev->driver, ep->dev->gadget.speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return -ESHUTDOWN;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001425 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001426 if (avail > ep->fifo_size) {
1427 dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -EOVERFLOW;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (ep->is_in)
1431 avail = ep->fifo_size - avail;
1432 return avail;
1433}
1434
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001435static void net2280_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct net2280_ep *ep;
1438
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001439 ep = container_of(_ep, struct net2280_ep, ep);
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001440 if (!_ep || (!ep->desc && ep->num != 0)) {
1441 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001443 }
1444 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1445 dev_err(&ep->dev->pdev->dev,
1446 "%s: Invalid driver=%p or speed=%d\n",
1447 __func__, ep->dev->driver, ep->dev->gadget.speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return;
Mian Yousaf Kaukab9ceafcc2015-02-02 10:55:25 +01001449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001451 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001452 (void) readl(&ep->regs->ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
David Brownell901b3d72006-09-02 03:13:45 -07001455static const struct usb_ep_ops net2280_ep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 .enable = net2280_enable,
1457 .disable = net2280_disable,
1458
1459 .alloc_request = net2280_alloc_request,
1460 .free_request = net2280_free_request,
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 .queue = net2280_queue,
1463 .dequeue = net2280_dequeue,
1464
1465 .set_halt = net2280_set_halt,
Alan Stern80661342008-08-14 15:49:11 -04001466 .set_wedge = net2280_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 .fifo_status = net2280_fifo_status,
1468 .fifo_flush = net2280_fifo_flush,
1469};
1470
1471/*-------------------------------------------------------------------------*/
1472
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001473static int net2280_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct net2280 *dev;
1476 unsigned long flags;
1477 u16 retval;
1478
1479 if (!_gadget)
1480 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001481 dev = container_of(_gadget, struct net2280, gadget);
1482 spin_lock_irqsave(&dev->lock, flags);
1483 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1484 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return retval;
1486}
1487
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001488static int net2280_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 struct net2280 *dev;
1491 u32 tmp;
1492 unsigned long flags;
1493
1494 if (!_gadget)
1495 return 0;
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);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001500 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1501 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001502 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 /* pci writes may still be posted */
1505 return 0;
1506}
1507
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001508static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct net2280 *dev;
1511 u32 tmp;
1512 unsigned long flags;
1513
1514 if (!_gadget)
1515 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001516 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001518 spin_lock_irqsave(&dev->lock, flags);
1519 tmp = readl(&dev->usb->usbctl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001520 if (value) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001521 tmp |= BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001522 _gadget->is_selfpowered = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001523 } else {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001524 tmp &= ~BIT(SELF_POWERED_STATUS);
Peter Chenc8678d92015-01-28 16:32:35 +08001525 _gadget->is_selfpowered = 0;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001526 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001527 writel(tmp, &dev->usb->usbctl);
1528 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 return 0;
1531}
1532
1533static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1534{
1535 struct net2280 *dev;
1536 u32 tmp;
1537 unsigned long flags;
1538
1539 if (!_gadget)
1540 return -ENODEV;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001541 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001543 spin_lock_irqsave(&dev->lock, flags);
1544 tmp = readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 dev->softconnect = (is_on != 0);
Mian Yousaf Kaukab11bece52015-05-16 22:33:39 +02001546 if (is_on) {
1547 ep0_start(dev);
1548 writel(tmp | BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1549 } else {
1550 writel(tmp & ~BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1551 stop_activity(dev, dev->driver);
1552 }
1553
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001554 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 return 0;
1557}
1558
Robert Baldyga3e8b2312015-08-06 14:11:13 +02001559static struct usb_ep *net2280_match_ep(struct usb_gadget *_gadget,
1560 struct usb_endpoint_descriptor *desc,
1561 struct usb_ss_ep_comp_descriptor *ep_comp)
1562{
1563 char name[8];
1564 struct usb_ep *ep;
1565
1566 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT) {
1567 /* ep-e, ep-f are PIO with only 64 byte fifos */
1568 ep = gadget_find_ep_by_name(_gadget, "ep-e");
1569 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1570 return ep;
1571 ep = gadget_find_ep_by_name(_gadget, "ep-f");
1572 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1573 return ep;
1574 }
1575
1576 /* USB3380: use same address for usb and hardware endpoints */
1577 snprintf(name, sizeof(name), "ep%d%s", usb_endpoint_num(desc),
1578 usb_endpoint_dir_in(desc) ? "in" : "out");
1579 ep = gadget_find_ep_by_name(_gadget, name);
1580 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1581 return ep;
1582
1583 return NULL;
1584}
1585
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001586static int net2280_start(struct usb_gadget *_gadget,
1587 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001588static int net2280_stop(struct usb_gadget *_gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590static const struct usb_gadget_ops net2280_ops = {
1591 .get_frame = net2280_get_frame,
1592 .wakeup = net2280_wakeup,
1593 .set_selfpowered = net2280_set_selfpowered,
1594 .pullup = net2280_pullup,
Felipe Balbi4cf5e002011-10-10 10:37:17 +03001595 .udc_start = net2280_start,
1596 .udc_stop = net2280_stop,
Robert Baldyga3e8b2312015-08-06 14:11:13 +02001597 .match_ep = net2280_match_ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598};
1599
1600/*-------------------------------------------------------------------------*/
1601
Ricardo Ribalda Delgadob99b4062014-07-04 11:27:03 +02001602#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604/* FIXME move these into procfs, and use seq_file.
1605 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1606 * and also doesn't help products using this with 2.4 kernels.
1607 */
1608
1609/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001610static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1611 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001613 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001615 if (!dev->driver || !dev->driver->function ||
1616 strlen(dev->driver->function) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001618 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001620static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001622static ssize_t registers_show(struct device *_dev,
1623 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 struct net2280 *dev;
1626 char *next;
1627 unsigned size, t;
1628 unsigned long flags;
1629 int i;
1630 u32 t1, t2;
Andrew Morton30e69592005-06-26 17:18:46 -07001631 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001633 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 next = buf;
1635 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001636 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 if (dev->driver)
1639 s = dev->driver->driver.name;
1640 else
1641 s = "(none)";
1642
1643 /* Main Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001644 t = scnprintf(next, size, "%s version " DRIVER_VERSION
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01001645 ", chiprev %04x\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 "devinit %03x fifoctl %08x gadget '%s'\n"
1647 "pci irqenb0 %02x irqenb1 %08x "
1648 "irqstat0 %04x irqstat1 %08x\n",
1649 driver_name, dev->chiprev,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001650 readl(&dev->regs->devinit),
1651 readl(&dev->regs->fifoctl),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 s,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001653 readl(&dev->regs->pciirqenb0),
1654 readl(&dev->regs->pciirqenb1),
1655 readl(&dev->regs->irqstat0),
1656 readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 size -= t;
1658 next += t;
1659
1660 /* USB Control Registers */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001661 t1 = readl(&dev->usb->usbctl);
1662 t2 = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001663 if (t1 & BIT(VBUS_PIN)) {
1664 if (t2 & BIT(HIGH_SPEED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 s = "high speed";
1666 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1667 s = "powered";
1668 else
1669 s = "full speed";
1670 /* full speed bit (6) not working?? */
1671 } else
1672 s = "not attached";
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001673 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 "stdrsp %08x usbctl %08x usbstat %08x "
1675 "addr 0x%02x (%s)\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001676 readl(&dev->usb->stdrsp), t1, t2,
1677 readl(&dev->usb->ouraddr), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 size -= t;
1679 next += t;
1680
1681 /* PCI Master Control Registers */
1682
1683 /* DMA Control Registers */
1684
1685 /* Configurable EP Control Registers */
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001686 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 struct net2280_ep *ep;
1688
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001689 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (i && !ep->desc)
1691 continue;
1692
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001693 t1 = readl(&ep->cfg->ep_cfg);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001694 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1695 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1697 "irqenb %02x\n",
1698 ep->ep.name, t1, t2,
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001699 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 ? "NAK " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001701 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 ? "hide " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001703 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 ? "CRC " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001705 (t2 & BIT(CLEAR_INTERRUPT_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 ? "interrupt " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001707 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 ? "status " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001709 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 ? "NAKmode " : "",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001711 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 ? "DATA1 " : "DATA0 ",
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001713 (t2 & BIT(CLEAR_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 ? "HALT " : "",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001715 readl(&ep->regs->ep_irqenb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 size -= t;
1717 next += t;
1718
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001719 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 "\tstat %08x avail %04x "
1721 "(ep%d%s-%s)%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001722 readl(&ep->regs->ep_stat),
1723 readl(&ep->regs->ep_avail),
1724 t1 & 0x0f, DIR_STRING(t1),
1725 type_string(t1 >> 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 ep->stopped ? "*" : "");
1727 size -= t;
1728 next += t;
1729
1730 if (!ep->dma)
1731 continue;
1732
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001733 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 " dma\tctl %08x stat %08x count %08x\n"
1735 "\taddr %08x desc %08x\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001736 readl(&ep->dma->dmactl),
1737 readl(&ep->dma->dmastat),
1738 readl(&ep->dma->dmacount),
1739 readl(&ep->dma->dmaaddr),
1740 readl(&ep->dma->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 size -= t;
1742 next += t;
1743
1744 }
1745
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001746 /* Indexed Registers (none yet) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* Statistics */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001749 t = scnprintf(next, size, "\nirqs: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 size -= t;
1751 next += t;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001752 for (i = 0; i < dev->n_ep; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 struct net2280_ep *ep;
1754
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001755 ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (i && !ep->irqs)
1757 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001758 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 size -= t;
1760 next += t;
1761
1762 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001763 t = scnprintf(next, size, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 size -= t;
1765 next += t;
1766
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001767 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 return PAGE_SIZE - size;
1770}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001771static DEVICE_ATTR_RO(registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001773static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1774 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 struct net2280 *dev;
1777 char *next;
1778 unsigned size;
1779 unsigned long flags;
1780 int i;
1781
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001782 dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 next = buf;
1784 size = PAGE_SIZE;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001785 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001787 for (i = 0; i < dev->n_ep; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001788 struct net2280_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct net2280_request *req;
1790 int t;
1791
1792 if (i != 0) {
1793 const struct usb_endpoint_descriptor *d;
1794
1795 d = ep->desc;
1796 if (!d)
1797 continue;
1798 t = d->bEndpointAddress;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001799 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1801 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1802 (t & USB_DIR_IN) ? "in" : "out",
Ricardo Ribalda Delgadoa27f37a2014-05-20 18:30:08 +02001803 type_string(d->bmAttributes),
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001804 usb_endpoint_maxp(d) & 0x1fff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 ep->dma ? "dma" : "pio", ep->fifo_size
1806 );
1807 } else /* ep0 should only have one transfer queued */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001808 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 ep->is_in ? "in" : "out");
1810 if (t <= 0 || t > size)
1811 goto done;
1812 size -= t;
1813 next += t;
1814
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001815 if (list_empty(&ep->queue)) {
1816 t = scnprintf(next, size, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (t <= 0 || t > size)
1818 goto done;
1819 size -= t;
1820 next += t;
1821 continue;
1822 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001823 list_for_each_entry(req, &ep->queue, queue) {
1824 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1825 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 "\treq %p len %d/%d "
1827 "buf %p (dmacount %08x)\n",
1828 &req->req, req->req.actual,
1829 req->req.length, req->req.buf,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001830 readl(&ep->dma->dmacount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001832 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 "\treq %p len %d/%d buf %p\n",
1834 &req->req, req->req.actual,
1835 req->req.length, req->req.buf);
1836 if (t <= 0 || t > size)
1837 goto done;
1838 size -= t;
1839 next += t;
1840
1841 if (ep->dma) {
1842 struct net2280_dma *td;
1843
1844 td = req->td;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001845 t = scnprintf(next, size, "\t td %08x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 " count %08x buf %08x desc %08x\n",
1847 (u32) req->td_dma,
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001848 le32_to_cpu(td->dmacount),
1849 le32_to_cpu(td->dmaaddr),
1850 le32_to_cpu(td->dmadesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (t <= 0 || t > size)
1852 goto done;
1853 size -= t;
1854 next += t;
1855 }
1856 }
1857 }
1858
1859done:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001860 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return PAGE_SIZE - size;
1862}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001863static DEVICE_ATTR_RO(queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865
1866#else
1867
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001868#define device_create_file(a, b) (0)
1869#define device_remove_file(a, b) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871#endif
1872
1873/*-------------------------------------------------------------------------*/
1874
1875/* another driver-specific mode might be a request type doing dma
1876 * to/from another device fifo instead of to/from memory.
1877 */
1878
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001879static void set_fifo_mode(struct net2280 *dev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 /* keeping high bits preserves BAR2 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001882 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001885 INIT_LIST_HEAD(&dev->gadget.ep_list);
1886 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1887 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 switch (mode) {
1889 case 0:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001890 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1891 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1892 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 break;
1894 case 1:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001895 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 break;
1897 case 2:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001898 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1899 dev->ep[1].fifo_size = 2048;
1900 dev->ep[2].fifo_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
1902 }
1903 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02001904 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1905 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001908static void defect7374_disable_data_eps(struct net2280 *dev)
1909{
1910 /*
1911 * For Defect 7374, disable data EPs (and more):
1912 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1913 * returing ep regs back to normal.
1914 */
1915 struct net2280_ep *ep;
1916 int i;
1917 unsigned char ep_sel;
1918 u32 tmp_reg;
1919
1920 for (i = 1; i < 5; i++) {
1921 ep = &dev->ep[i];
Mian Yousaf Kaukab81e9d142015-10-19 16:25:15 +02001922 writel(i, &ep->cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001923 }
1924
1925 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1926 for (i = 0; i < 6; i++)
1927 writel(0, &dev->dep[i].dep_cfg);
1928
1929 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1930 /* Select an endpoint for subsequent operations: */
1931 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1932 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1933
1934 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1935 ep_sel == 18 || ep_sel == 20)
1936 continue;
1937
1938 /* Change settings on some selected endpoints */
1939 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001940 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001941 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1942 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02001943 tmp_reg |= BIT(EP_INITIALIZED);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001944 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1945 }
1946}
1947
1948static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1949{
1950 u32 tmp = 0, tmp_reg;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001951 u32 scratch;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001952 int i;
1953 unsigned char ep_sel;
1954
1955 scratch = get_idx_reg(dev->regs, SCRATCH);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001956
1957 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1958 == DEFECT7374_FSM_SS_CONTROL_READ);
1959
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001960 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1961
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001962 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1963 ep_warn(dev, "It will operate on cold-reboot and SS connect");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001964
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001965 /*GPEPs:*/
1966 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1967 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1968 ((dev->enhanced_mode) ?
Mian Yousaf Kaukab25d40ee2015-05-16 22:33:30 +02001969 BIT(OUT_ENDPOINT_ENABLE) | BIT(IN_ENDPOINT_ENABLE) :
1970 BIT(ENDPOINT_ENABLE)));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001971
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001972 for (i = 1; i < 5; i++)
1973 writel(tmp, &dev->ep[i].cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001974
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001975 /* CSRIN, PCIIN, STATIN, RCIN*/
1976 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1977 writel(tmp, &dev->dep[1].dep_cfg);
1978 writel(tmp, &dev->dep[3].dep_cfg);
1979 writel(tmp, &dev->dep[4].dep_cfg);
1980 writel(tmp, &dev->dep[5].dep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001981
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001982 /*Implemented for development and debug.
1983 * Can be refined/tuned later.*/
1984 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1985 /* Select an endpoint for subsequent operations: */
1986 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1987 writel(((tmp_reg & ~0x1f) | ep_sel),
1988 &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001989
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001990 if (ep_sel == 1) {
1991 tmp =
1992 (readl(&dev->plregs->pl_ep_ctrl) |
1993 BIT(CLEAR_ACK_ERROR_CODE) | 0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001994 writel(tmp, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001995 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02001996 }
1997
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01001998 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1999 ep_sel == 18 || ep_sel == 20)
2000 continue;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002001
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002002 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
2003 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
2004 writel(tmp, &dev->plregs->pl_ep_cfg_4);
2005
2006 tmp = readl(&dev->plregs->pl_ep_ctrl) &
2007 ~BIT(EP_INITIALIZED);
2008 writel(tmp, &dev->plregs->pl_ep_ctrl);
2009
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002010 }
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002011
2012 /* Set FSM to focus on the first Control Read:
2013 * - Tip: Connection speed is known upon the first
2014 * setup request.*/
2015 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
2016 set_idx_reg(dev->regs, SCRATCH, scratch);
2017
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002018}
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020/* keeping it simple:
2021 * - one bus driver, initted first;
2022 * - one function driver, initted second
2023 *
2024 * most of the work to support multiple net2280 controllers would
2025 * be to associate this gadget driver (yes?) with all of them, or
2026 * perhaps to bind specific drivers to specific devices.
2027 */
2028
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002029static void usb_reset_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 u32 tmp;
2032
2033 dev->gadget.speed = USB_SPEED_UNKNOWN;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002034 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002036 net2280_led_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /* disable automatic responses, and irqs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002039 writel(0, &dev->usb->stdrsp);
2040 writel(0, &dev->regs->pciirqenb0);
2041 writel(0, &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 /* clear old dma and irq state */
2044 for (tmp = 0; tmp < 4; tmp++) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002045 struct net2280_ep *ep = &dev->ep[tmp + 1];
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07002046 if (ep->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002047 abort_dma(ep);
Greg Kroah-Hartman357d596e2014-05-28 11:35:41 -07002048 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002049
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002050 writel(~0, &dev->regs->irqstat0),
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002051 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 /* reset, and enable pci */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002054 tmp = readl(&dev->regs->devinit) |
2055 BIT(PCI_ENABLE) |
2056 BIT(FIFO_SOFT_RESET) |
2057 BIT(USB_SOFT_RESET) |
2058 BIT(M8051_RESET);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002059 writel(tmp, &dev->regs->devinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /* standard fifo and endpoint allocations */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002062 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002065static void usb_reset_338x(struct net2280 *dev)
2066{
2067 u32 tmp;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002068
2069 dev->gadget.speed = USB_SPEED_UNKNOWN;
2070 (void)readl(&dev->usb->usbctl);
2071
2072 net2280_led_init(dev);
2073
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002074 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002075 /* disable automatic responses, and irqs */
2076 writel(0, &dev->usb->stdrsp);
2077 writel(0, &dev->regs->pciirqenb0);
2078 writel(0, &dev->regs->pciirqenb1);
2079 }
2080
2081 /* clear old dma and irq state */
2082 for (tmp = 0; tmp < 4; tmp++) {
2083 struct net2280_ep *ep = &dev->ep[tmp + 1];
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02002084 struct net2280_dma_regs __iomem *dma;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002085
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02002086 if (ep->dma) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002087 abort_dma(ep);
Mian Yousaf Kaukab3fc0a7c2015-05-16 22:33:32 +02002088 } else {
2089 dma = &dev->dma[tmp];
2090 writel(BIT(DMA_ABORT), &dma->dmastat);
2091 writel(0, &dma->dmactl);
2092 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002093 }
2094
2095 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
2096
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002097 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002098 /* reset, and enable pci */
2099 tmp = readl(&dev->regs->devinit) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002100 BIT(PCI_ENABLE) |
2101 BIT(FIFO_SOFT_RESET) |
2102 BIT(USB_SOFT_RESET) |
2103 BIT(M8051_RESET);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002104
2105 writel(tmp, &dev->regs->devinit);
2106 }
2107
2108 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2109 INIT_LIST_HEAD(&dev->gadget.ep_list);
2110
2111 for (tmp = 1; tmp < dev->n_ep; tmp++)
2112 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2113
2114}
2115
2116static void usb_reset(struct net2280 *dev)
2117{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002118 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002119 return usb_reset_228x(dev);
2120 return usb_reset_338x(dev);
2121}
2122
2123static void usb_reinit_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /* basic endpoint init */
2128 for (tmp = 0; tmp < 7; tmp++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002129 struct net2280_ep *ep = &dev->ep[tmp];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Robert Baldygac23c3c32015-07-31 16:00:36 +02002131 ep->ep.name = ep_info_dft[tmp].name;
2132 ep->ep.caps = ep_info_dft[tmp].caps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 ep->dev = dev;
2134 ep->num = tmp;
2135
2136 if (tmp > 0 && tmp <= 4) {
2137 ep->fifo_size = 1024;
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01002138 ep->dma = &dev->dma[tmp - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 } else
2140 ep->fifo_size = 64;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002141 ep->regs = &dev->epregs[tmp];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002142 ep->cfg = &dev->epregs[tmp];
2143 ep_reset_228x(dev->regs, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002145 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
2146 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
2147 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002149 dev->gadget.ep0 = &dev->ep[0].ep;
2150 dev->ep[0].stopped = 0;
2151 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 /* we want to prevent lowlevel/insecure access from the USB host,
2154 * but erratum 0119 means this enable bit is ignored
2155 */
2156 for (tmp = 0; tmp < 5; tmp++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002157 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002160static void usb_reinit_338x(struct net2280 *dev)
2161{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002162 int i;
2163 u32 tmp, val;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002164 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2165 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2166 0x00, 0xC0, 0x00, 0xC0 };
2167
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002168 /* basic endpoint init */
2169 for (i = 0; i < dev->n_ep; i++) {
2170 struct net2280_ep *ep = &dev->ep[i];
2171
Robert Baldygac23c3c32015-07-31 16:00:36 +02002172 ep->ep.name = dev->enhanced_mode ? ep_info_adv[i].name :
2173 ep_info_dft[i].name;
2174 ep->ep.caps = dev->enhanced_mode ? ep_info_adv[i].caps :
2175 ep_info_dft[i].caps;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002176 ep->dev = dev;
2177 ep->num = i;
2178
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01002179 if (i > 0 && i <= 4)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002180 ep->dma = &dev->dma[i - 1];
2181
2182 if (dev->enhanced_mode) {
2183 ep->cfg = &dev->epregs[ne[i]];
Mian Yousaf Kaukabc65c4f02015-05-16 22:33:36 +02002184 /*
2185 * Set USB endpoint number, hardware allows same number
2186 * in both directions.
2187 */
2188 if (i > 0 && i < 5)
2189 writel(ne[i], &ep->cfg->ep_cfg);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002190 ep->regs = (struct net2280_ep_regs __iomem *)
Felipe Balbic43e97b2014-07-16 12:20:25 -05002191 (((void __iomem *)&dev->epregs[ne[i]]) +
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002192 ep_reg_addr[i]);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002193 } else {
2194 ep->cfg = &dev->epregs[i];
2195 ep->regs = &dev->epregs[i];
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002196 }
2197
2198 ep->fifo_size = (i != 0) ? 2048 : 512;
2199
2200 ep_reset_338x(dev->regs, ep);
2201 }
2202 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2203
2204 dev->gadget.ep0 = &dev->ep[0].ep;
2205 dev->ep[0].stopped = 0;
2206
2207 /* Link layer set up */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002208 if (dev->bug7734_patched) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002209 tmp = readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002210 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002211 writel(tmp, &dev->usb_ext->usbctl2);
2212 }
2213
2214 /* Hardware Defect and Workaround */
2215 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2216 val &= ~(0xf << TIMER_LFPS_6US);
2217 val |= 0x5 << TIMER_LFPS_6US;
2218 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2219
2220 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2221 val &= ~(0xffff << TIMER_LFPS_80US);
2222 val |= 0x0100 << TIMER_LFPS_80US;
2223 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2224
2225 /*
2226 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2227 * Hot Reset Exit Handshake may Fail in Specific Case using
2228 * Default Register Settings. Workaround for Enumeration test.
2229 */
2230 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2231 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2232 val |= 0x10 << HOT_TX_NORESET_TS2;
2233 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2234
2235 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2236 val &= ~(0x1f << HOT_RX_RESET_TS2);
2237 val |= 0x3 << HOT_RX_RESET_TS2;
2238 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2239
2240 /*
2241 * Set Recovery Idle to Recover bit:
2242 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2243 * link robustness with various hosts and hubs.
2244 * - It is safe to set for all connection speeds; all chip revisions.
2245 * - R-M-W to leave other bits undisturbed.
2246 * - Reference PLX TT-7372
2247 */
2248 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002249 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002250 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2251
2252 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2253
2254 /* disable dedicated endpoints */
2255 writel(0x0D, &dev->dep[0].dep_cfg);
2256 writel(0x0D, &dev->dep[1].dep_cfg);
2257 writel(0x0E, &dev->dep[2].dep_cfg);
2258 writel(0x0E, &dev->dep[3].dep_cfg);
2259 writel(0x0F, &dev->dep[4].dep_cfg);
2260 writel(0x0C, &dev->dep[5].dep_cfg);
2261}
2262
2263static void usb_reinit(struct net2280 *dev)
2264{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002265 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002266 return usb_reinit_228x(dev);
2267 return usb_reinit_338x(dev);
2268}
2269
2270static void ep0_start_228x(struct net2280 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002272 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2273 BIT(CLEAR_NAK_OUT_PACKETS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002274 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2275 &dev->epregs[0].ep_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 /*
2278 * hardware optionally handles a bunch of standard requests
2279 * that the API hides from drivers anyway. have it do so.
2280 * endpoint status/features are handled in software, to
2281 * help pass tests for some dubious behavior.
2282 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002283 writel(BIT(SET_TEST_MODE) |
2284 BIT(SET_ADDRESS) |
2285 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2286 BIT(GET_DEVICE_STATUS) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002287 BIT(GET_INTERFACE_STATUS),
2288 &dev->usb->stdrsp);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002289 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2290 BIT(SELF_POWERED_USB_DEVICE) |
2291 BIT(REMOTE_WAKEUP_SUPPORT) |
2292 (dev->softconnect << USB_DETECT_ENABLE) |
2293 BIT(SELF_POWERED_STATUS),
2294 &dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002297 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2298 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2299 &dev->regs->pciirqenb0);
2300 writel(BIT(PCI_INTERRUPT_ENABLE) |
2301 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2302 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2303 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2304 BIT(VBUS_INTERRUPT_ENABLE) |
2305 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2306 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2307 &dev->regs->pciirqenb1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 /* don't leave any writes posted */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002310 (void) readl(&dev->usb->usbctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002313static void ep0_start_338x(struct net2280 *dev)
2314{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002315
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002316 if (dev->bug7734_patched)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002317 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2318 BIT(SET_EP_HIDE_STATUS_PHASE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002319 &dev->epregs[0].ep_rsp);
2320
2321 /*
2322 * hardware optionally handles a bunch of standard requests
2323 * that the API hides from drivers anyway. have it do so.
2324 * endpoint status/features are handled in software, to
2325 * help pass tests for some dubious behavior.
2326 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002327 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2328 BIT(SET_SEL) |
2329 BIT(SET_TEST_MODE) |
2330 BIT(SET_ADDRESS) |
2331 BIT(GET_INTERFACE_STATUS) |
2332 BIT(GET_DEVICE_STATUS),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002333 &dev->usb->stdrsp);
2334 dev->wakeup_enable = 1;
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002335 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002336 (dev->softconnect << USB_DETECT_ENABLE) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002337 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002338 &dev->usb->usbctl);
2339
2340 /* enable irqs so we can see ep0 and general operation */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002341 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002342 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2343 &dev->regs->pciirqenb0);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002344 writel(BIT(PCI_INTERRUPT_ENABLE) |
2345 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2346 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2347 BIT(VBUS_INTERRUPT_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002348 &dev->regs->pciirqenb1);
2349
2350 /* don't leave any writes posted */
2351 (void)readl(&dev->usb->usbctl);
2352}
2353
2354static void ep0_start(struct net2280 *dev)
2355{
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002356 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002357 return ep0_start_228x(dev);
2358 return ep0_start_338x(dev);
2359}
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361/* when a driver is successfully registered, it will receive
2362 * control requests including set_configuration(), which enables
2363 * non-control requests. then usb traffic follows until a
2364 * disconnect is reported. then a host may connect again, or
2365 * the driver might get unbound.
2366 */
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002367static int net2280_start(struct usb_gadget *_gadget,
2368 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002370 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 int retval;
2372 unsigned i;
2373
2374 /* insist on high speed support from the driver, since
2375 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2376 * "must not be used in normal operation"
2377 */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002378 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2379 !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return -EINVAL;
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002381
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002382 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002384 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002385 dev->ep[i].irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 /* hook up the driver ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 driver->driver.bus = NULL;
2389 dev->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002391 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2392 if (retval)
2393 goto err_unbind;
2394 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2395 if (retval)
2396 goto err_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Ricardo Ribalda Delgado7a74c482014-11-28 14:50:47 +01002398 /* enable host detection and ep0; and we're ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 * for set_configuration as well as eventual disconnect.
2400 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002401 net2280_led_active(dev, 1);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002402
Tim Harvey5185c9132016-05-23 06:58:41 -07002403 if ((dev->quirks & PLX_PCIE) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002404 defect7374_enable_data_eps_zero(dev);
2405
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002406 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 /* pci writes may still be posted */
2409 return 0;
Jeff Garzikb3899da2006-10-11 21:50:24 -04002410
2411err_func:
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002412 device_remove_file(&dev->pdev->dev, &dev_attr_function);
Jeff Garzikb3899da2006-10-11 21:50:24 -04002413err_unbind:
Jeff Garzikb3899da2006-10-11 21:50:24 -04002414 dev->driver = NULL;
2415 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002418static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
2420 int i;
2421
2422 /* don't disconnect if it's not connected */
2423 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2424 driver = NULL;
2425
2426 /* stop hardware; prevent new request submissions;
2427 * and kill any outstanding requests.
2428 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002429 usb_reset(dev);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002430 for (i = 0; i < dev->n_ep; i++)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002431 nuke(&dev->ep[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Felipe Balbi699412d2013-03-18 10:14:47 +02002433 /* report disconnect; the driver is already quiesced */
2434 if (driver) {
2435 spin_unlock(&dev->lock);
2436 driver->disconnect(&dev->gadget);
2437 spin_lock(&dev->lock);
2438 }
2439
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002440 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Felipe Balbi22835b82014-10-17 12:05:12 -05002443static int net2280_stop(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Felipe Balbi4cf5e002011-10-10 10:37:17 +03002445 struct net2280 *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 unsigned long flags;
2447
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002448 dev = container_of(_gadget, struct net2280, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002450 spin_lock_irqsave(&dev->lock, flags);
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002451 stop_activity(dev, NULL);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002452 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002454 net2280_led_active(dev, 0);
Alan Stern2f076072013-01-30 16:40:14 -05002455
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002456 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2457 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Felipe Balbibfd0ed52014-10-17 11:23:33 -05002459 dev->driver = NULL;
Ricardo Ribalda Delgado84237bf2014-05-15 14:28:45 +02002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0;
2462}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464/*-------------------------------------------------------------------------*/
2465
2466/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2467 * also works for dma-capable endpoints, in pio mode or just
2468 * to manually advance the queue after short OUT transfers.
2469 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002470static void handle_ep_small(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
2472 struct net2280_request *req;
2473 u32 t;
2474 /* 0 error, 1 mid-data, 2 done */
2475 int mode = 1;
2476
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002477 if (!list_empty(&ep->queue))
2478 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct net2280_request, queue);
2480 else
2481 req = NULL;
2482
2483 /* ack all, and handle what we care about */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002484 t = readl(&ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 ep->irqs++;
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002486
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002487 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
Ricardo Ribalda Delgadofc12c682015-01-13 10:54:58 +01002488 ep->ep.name, t, req ? &req->req : NULL);
Ricardo Ribalda Delgadocb442ee2014-11-28 14:51:02 +01002489
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02002490 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002491 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01002492 else
2493 /* Added for 2282 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002494 writel(t, &ep->regs->ep_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 /* for ep0, monitor token irqs to catch data stage length errors
2497 * and to synchronize on status.
2498 *
2499 * also, to defer reporting of protocol stalls ... here's where
2500 * data or status first appears, handling stalls here should never
2501 * cause trouble on the host side..
2502 *
2503 * control requests could be slightly faster without token synch for
2504 * status, but status can jam up that way.
2505 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002506 if (unlikely(ep->num == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (ep->is_in) {
2508 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002509 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (ep->dev->protocol_stall) {
2511 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002512 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514 if (!req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002515 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 mode = 2;
2517 /* reply to extra IN data tokens with a zlp */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002518 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (ep->dev->protocol_stall) {
2520 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002521 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 mode = 2;
Alan Stern1f26e282006-11-16 10:16:00 -05002523 } else if (ep->responded &&
2524 !req && !ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002525 write_fifo(ep, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
2527 } else {
2528 /* status; stop NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002529 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (ep->dev->protocol_stall) {
2531 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002532 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534 mode = 2;
2535 /* an extra OUT token is an error */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002536 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2537 req &&
2538 req->req.actual == req->req.length) ||
2539 (ep->responded && !req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 ep->dev->protocol_stall = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002541 set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 ep->stopped = 1;
2543 if (req)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002544 done(ep, req, -EOVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 req = NULL;
2546 }
2547 }
2548 }
2549
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002550 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 return;
2552
2553 /* manual DMA queue advance after short OUT */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002554 if (likely(ep->dma)) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002555 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03002556 struct net2280_request *stuck_req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 int stopped = ep->stopped;
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03002558 int num_completed;
2559 int stuck = 0;
2560 u32 count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 /* TRANSFERRED works around OUT_DONE erratum 0112.
2563 * we expect (N <= maxpacket) bytes; host wrote M.
2564 * iff (M < N) we won't ever see a DMA interrupt.
2565 */
2566 ep->stopped = 1;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002567 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 /* any preceding dma transfers must finish.
2570 * dma handles (M >= N), may empty the queue
2571 */
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03002572 num_completed = scan_dma_completions(ep);
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002573 if (unlikely(list_empty(&ep->queue) ||
2574 ep->out_overflow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 req = NULL;
2576 break;
2577 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002578 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct net2280_request, queue);
2580
2581 /* here either (M < N), a "real" short rx;
2582 * or (M == N) and the queue didn't empty
2583 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002584 if (likely(t & BIT(FIFO_EMPTY))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002585 count = readl(&ep->dma->dmacount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 count &= DMA_BYTE_COUNT_MASK;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002587 if (readl(&ep->dma->dmadesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 != req->td_dma)
2589 req = NULL;
2590 break;
2591 }
Jussi Kivilinna1de2ebf2016-08-12 17:29:34 +03002592
2593 /* Escape loop if no dma transfers completed
2594 * after few retries.
2595 */
2596 if (num_completed == 0) {
2597 if (stuck_req == req &&
2598 readl(&ep->dma->dmadesc) !=
2599 req->td_dma && stuck++ > 5) {
2600 count = readl(
2601 &ep->dma->dmacount);
2602 count &= DMA_BYTE_COUNT_MASK;
2603 req = NULL;
2604 ep_dbg(ep->dev, "%s escape stuck %d, count %u\n",
2605 ep->ep.name, stuck,
2606 count);
2607 break;
2608 } else if (stuck_req != req) {
2609 stuck_req = req;
2610 stuck = 0;
2611 }
2612 } else {
2613 stuck_req = NULL;
2614 stuck = 0;
2615 }
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 udelay(1);
2618 }
2619
2620 /* stop DMA, leave ep NAKing */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002621 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002622 spin_stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002624 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 req->td->dmacount = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002626 t = readl(&ep->regs->ep_avail);
2627 dma_done(ep, req, count,
David Brownell901b3d72006-09-02 03:13:45 -07002628 (ep->out_overflow || t)
2629 ? -EOVERFLOW : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631
2632 /* also flush to prevent erratum 0106 trouble */
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02002633 if (unlikely(ep->out_overflow ||
2634 (ep->dev->chiprev == 0x0100 &&
2635 ep->dev->gadget.speed
2636 == USB_SPEED_FULL))) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002637 out_flush(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 ep->out_overflow = 0;
2639 }
2640
2641 /* (re)start dma if needed, stop NAKing */
2642 ep->stopped = stopped;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002643 if (!list_empty(&ep->queue))
2644 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 } else
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002646 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 ep->ep.name, t);
2648 return;
2649
2650 /* data packet(s) received (in the fifo, OUT) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002651 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002652 if (read_fifo(ep, req) && ep->num != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 mode = 2;
2654
2655 /* data packet(s) transmitted (IN) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002656 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 unsigned len;
2658
2659 len = req->req.length - req->req.actual;
2660 if (len > ep->ep.maxpacket)
2661 len = ep->ep.maxpacket;
2662 req->req.actual += len;
2663
2664 /* if we wrote it all, we're usually done */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002665 /* send zlps until the status stage */
2666 if ((req->req.actual == req->req.length) &&
2667 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 mode = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 /* there was nothing to do ... */
2671 } else if (mode == 1)
2672 return;
2673
2674 /* done */
2675 if (mode == 2) {
2676 /* stream endpoints often resubmit/unlink in completion */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002677 done(ep, req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 /* maybe advance queue to next request */
2680 if (ep->num == 0) {
2681 /* NOTE: net2280 could let gadget driver start the
2682 * status stage later. since not all controllers let
2683 * them control that, the api doesn't (yet) allow it.
2684 */
2685 if (!ep->stopped)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002686 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 req = NULL;
2688 } else {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002689 if (!list_empty(&ep->queue) && !ep->stopped)
2690 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct net2280_request, queue);
2692 else
2693 req = NULL;
2694 if (req && !ep->is_in)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002695 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 }
2697 }
2698
2699 /* is there a buffer for the next packet?
2700 * for best streaming performance, make sure there is one.
2701 */
2702 if (req && !ep->stopped) {
2703
2704 /* load IN fifo with next packet (may be zlp) */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002705 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002706 write_fifo(ep, &req->req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
2708}
2709
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002710static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
2712 struct net2280_ep *ep;
2713
2714 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02002715 return &dev->ep[0];
2716 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 u8 bEndpointAddress;
2718
2719 if (!ep->desc)
2720 continue;
2721 bEndpointAddress = ep->desc->bEndpointAddress;
2722 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2723 continue;
2724 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2725 return ep;
2726 }
2727 return NULL;
2728}
2729
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002730static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2731{
2732 u32 scratch, fsmvalue;
2733 u32 ack_wait_timeout, state;
2734
2735 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2736 scratch = get_idx_reg(dev->regs, SCRATCH);
2737 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2738 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2739
2740 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2741 (r.bRequestType & USB_DIR_IN)))
2742 return;
2743
2744 /* This is the first Control Read for this connection: */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002745 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002746 /*
2747 * Connection is NOT SS:
2748 * - Connection must be FS or HS.
2749 * - This FSM state should allow workaround software to
2750 * run after the next USB connection.
2751 */
2752 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002753 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002754 goto restore_data_eps;
2755 }
2756
2757 /* Connection is SS: */
2758 for (ack_wait_timeout = 0;
2759 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2760 ack_wait_timeout++) {
2761
2762 state = readl(&dev->plregs->pl_ep_status_1)
2763 & (0xff << STATE);
2764 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2765 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2766 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01002767 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002768 break;
2769 }
2770
2771 /*
2772 * We have not yet received host's Data Phase ACK
2773 * - Wait and try again.
2774 */
2775 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2776
2777 continue;
2778 }
2779
2780
2781 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002782 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002783 "to detect SS host's data phase ACK.");
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002784 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002785 "got 0x%2.2x.\n", state >> STATE);
2786 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002787 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002788 "%duSec for Control Read Data Phase ACK\n",
2789 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2790 }
2791
2792restore_data_eps:
2793 /*
2794 * Restore data EPs to their pre-workaround settings (disabled,
2795 * initialized, and other details).
2796 */
2797 defect7374_disable_data_eps(dev);
2798
2799 set_idx_reg(dev->regs, SCRATCH, scratch);
2800
2801 return;
2802}
2803
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002804static void ep_clear_seqnum(struct net2280_ep *ep)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002805{
2806 struct net2280 *dev = ep->dev;
2807 u32 val;
2808 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2809
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002810 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2811 val |= ep_pl[ep->num];
2812 writel(val, &dev->plregs->pl_ep_ctrl);
2813 val |= BIT(SEQUENCE_NUMBER_RESET);
2814 writel(val, &dev->plregs->pl_ep_ctrl);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002815
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002816 return;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002817}
2818
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002819static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2820 struct net2280_ep *ep, struct usb_ctrlrequest r)
2821{
2822 int tmp = 0;
2823
2824#define w_value le16_to_cpu(r.wValue)
2825#define w_index le16_to_cpu(r.wIndex)
2826#define w_length le16_to_cpu(r.wLength)
2827
2828 switch (r.bRequest) {
2829 struct net2280_ep *e;
2830 u16 status;
2831
2832 case USB_REQ_SET_CONFIGURATION:
2833 dev->addressed_state = !w_value;
2834 goto usb3_delegate;
2835
2836 case USB_REQ_GET_STATUS:
2837 switch (r.bRequestType) {
2838 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2839 status = dev->wakeup_enable ? 0x02 : 0x00;
Peter Chenc8678d92015-01-28 16:32:35 +08002840 if (dev->gadget.is_selfpowered)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002841 status |= BIT(0);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002842 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2843 dev->ltm_enable << 4);
2844 writel(0, &dev->epregs[0].ep_irqenb);
2845 set_fifo_bytecount(ep, sizeof(status));
2846 writel((__force u32) status, &dev->epregs[0].ep_data);
2847 allow_status_338x(ep);
2848 break;
2849
2850 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2851 e = get_ep_by_addr(dev, w_index);
2852 if (!e)
2853 goto do_stall3;
2854 status = readl(&e->regs->ep_rsp) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002855 BIT(CLEAR_ENDPOINT_HALT);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002856 writel(0, &dev->epregs[0].ep_irqenb);
2857 set_fifo_bytecount(ep, sizeof(status));
2858 writel((__force u32) status, &dev->epregs[0].ep_data);
2859 allow_status_338x(ep);
2860 break;
2861
2862 default:
2863 goto usb3_delegate;
2864 }
2865 break;
2866
2867 case USB_REQ_CLEAR_FEATURE:
2868 switch (r.bRequestType) {
2869 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2870 if (!dev->addressed_state) {
2871 switch (w_value) {
2872 case USB_DEVICE_U1_ENABLE:
2873 dev->u1_enable = 0;
2874 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002875 ~BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002876 &dev->usb_ext->usbctl2);
2877 allow_status_338x(ep);
2878 goto next_endpoints3;
2879
2880 case USB_DEVICE_U2_ENABLE:
2881 dev->u2_enable = 0;
2882 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002883 ~BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002884 &dev->usb_ext->usbctl2);
2885 allow_status_338x(ep);
2886 goto next_endpoints3;
2887
2888 case USB_DEVICE_LTM_ENABLE:
2889 dev->ltm_enable = 0;
2890 writel(readl(&dev->usb_ext->usbctl2) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002891 ~BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002892 &dev->usb_ext->usbctl2);
2893 allow_status_338x(ep);
2894 goto next_endpoints3;
2895
2896 default:
2897 break;
2898 }
2899 }
2900 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2901 dev->wakeup_enable = 0;
2902 writel(readl(&dev->usb->usbctl) &
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002903 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002904 &dev->usb->usbctl);
2905 allow_status_338x(ep);
2906 break;
2907 }
2908 goto usb3_delegate;
2909
2910 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2911 e = get_ep_by_addr(dev, w_index);
2912 if (!e)
2913 goto do_stall3;
2914 if (w_value != USB_ENDPOINT_HALT)
2915 goto do_stall3;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002916 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002917 /*
2918 * Workaround for SS SeqNum not cleared via
2919 * Endpoint Halt (Clear) bit. select endpoint
2920 */
2921 ep_clear_seqnum(e);
2922 clear_halt(e);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002923 if (!list_empty(&e->queue) && e->td_dma)
2924 restart_dma(e);
2925 allow_status(ep);
2926 ep->stopped = 1;
2927 break;
2928
2929 default:
2930 goto usb3_delegate;
2931 }
2932 break;
2933 case USB_REQ_SET_FEATURE:
2934 switch (r.bRequestType) {
2935 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2936 if (!dev->addressed_state) {
2937 switch (w_value) {
2938 case USB_DEVICE_U1_ENABLE:
2939 dev->u1_enable = 1;
2940 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002941 BIT(U1_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002942 &dev->usb_ext->usbctl2);
2943 allow_status_338x(ep);
2944 goto next_endpoints3;
2945
2946 case USB_DEVICE_U2_ENABLE:
2947 dev->u2_enable = 1;
2948 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002949 BIT(U2_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002950 &dev->usb_ext->usbctl2);
2951 allow_status_338x(ep);
2952 goto next_endpoints3;
2953
2954 case USB_DEVICE_LTM_ENABLE:
2955 dev->ltm_enable = 1;
2956 writel(readl(&dev->usb_ext->usbctl2) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002957 BIT(LTM_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002958 &dev->usb_ext->usbctl2);
2959 allow_status_338x(ep);
2960 goto next_endpoints3;
2961 default:
2962 break;
2963 }
2964 }
2965
2966 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2967 dev->wakeup_enable = 1;
2968 writel(readl(&dev->usb->usbctl) |
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02002969 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002970 &dev->usb->usbctl);
2971 allow_status_338x(ep);
2972 break;
2973 }
2974 goto usb3_delegate;
2975
2976 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2977 e = get_ep_by_addr(dev, w_index);
2978 if (!e || (w_value != USB_ENDPOINT_HALT))
2979 goto do_stall3;
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002980 ep->stopped = 1;
2981 if (ep->num == 0)
2982 ep->dev->protocol_stall = 1;
2983 else {
2984 if (ep->dma)
Ricardo Ribalda Delgadoe721c452014-11-28 14:50:55 +01002985 abort_dma(ep);
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01002986 set_halt(ep);
Ricardo Ribalda Delgadocf8b1cd2014-11-28 14:50:52 +01002987 }
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02002988 allow_status_338x(ep);
2989 break;
2990
2991 default:
2992 goto usb3_delegate;
2993 }
2994
2995 break;
2996 default:
2997
2998usb3_delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02002999 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003000 r.bRequestType, r.bRequest,
3001 w_value, w_index, w_length,
3002 readl(&ep->cfg->ep_cfg));
3003
3004 ep->responded = 0;
3005 spin_unlock(&dev->lock);
3006 tmp = dev->driver->setup(&dev->gadget, &r);
3007 spin_lock(&dev->lock);
3008 }
3009do_stall3:
3010 if (tmp < 0) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003011 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003012 r.bRequestType, r.bRequest, tmp);
3013 dev->protocol_stall = 1;
3014 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
Ricardo Ribalda Delgadoe0cbb042014-11-28 14:50:54 +01003015 set_halt(ep);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003016 }
3017
3018next_endpoints3:
3019
3020#undef w_value
3021#undef w_index
3022#undef w_length
3023
3024 return;
3025}
3026
Mian Yousaf Kaukaba09e23f2015-05-16 22:33:35 +02003027static void usb338x_handle_ep_intr(struct net2280 *dev, u32 stat0)
3028{
3029 u32 index;
3030 u32 bit;
3031
3032 for (index = 0; index < ARRAY_SIZE(ep_bit); index++) {
3033 bit = BIT(ep_bit[index]);
3034
3035 if (!stat0)
3036 break;
3037
3038 if (!(stat0 & bit))
3039 continue;
3040
3041 stat0 &= ~bit;
3042
3043 handle_ep_small(&dev->ep[index]);
3044 }
3045}
3046
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003047static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
3049 struct net2280_ep *ep;
3050 u32 num, scratch;
3051
3052 /* most of these don't need individual acks */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003053 stat &= ~BIT(INTA_ASSERTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 if (!stat)
3055 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003056 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 /* starting a control request? */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003059 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 union {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003061 u32 raw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct usb_ctrlrequest r;
3063 } u;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003064 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct net2280_request *req;
3066
3067 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003068 u32 val = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003069 if (val & BIT(SUPER_SPEED)) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003070 dev->gadget.speed = USB_SPEED_SUPER;
3071 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3072 EP0_SS_MAX_PACKET_SIZE);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003073 } else if (val & BIT(HIGH_SPEED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 dev->gadget.speed = USB_SPEED_HIGH;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003075 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3076 EP0_HS_MAX_PACKET_SIZE);
3077 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 dev->gadget.speed = USB_SPEED_FULL;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003079 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3080 EP0_HS_MAX_PACKET_SIZE);
3081 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003082 net2280_led_speed(dev, dev->gadget.speed);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003083 ep_dbg(dev, "%s\n",
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003084 usb_speed_string(dev->gadget.speed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003087 ep = &dev->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 ep->irqs++;
3089
3090 /* make sure any leftover request state is cleared */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003091 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003092 while (!list_empty(&ep->queue)) {
3093 req = list_entry(ep->queue.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 struct net2280_request, queue);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003095 done(ep, req, (req->req.actual == req->req.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 ? 0 : -EPROTO);
3097 }
3098 ep->stopped = 0;
3099 dev->protocol_stall = 0;
Tim Harvey5185c9132016-05-23 06:58:41 -07003100 if (!(dev->quirks & PLX_PCIE)) {
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003101 if (ep->dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003102 tmp = BIT(FIFO_OVERFLOW) |
3103 BIT(FIFO_UNDERFLOW);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003104 else
3105 tmp = 0;
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003106
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003107 writel(tmp | BIT(TIMEOUT) |
3108 BIT(USB_STALL_SENT) |
3109 BIT(USB_IN_NAK_SENT) |
3110 BIT(USB_IN_ACK_RCVD) |
3111 BIT(USB_OUT_PING_NAK_SENT) |
3112 BIT(USB_OUT_ACK_SENT) |
3113 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
3114 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
3115 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
3116 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
3117 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003118 BIT(DATA_IN_TOKEN_INTERRUPT),
3119 &ep->regs->ep_stat);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003120 }
3121 u.raw[0] = readl(&dev->usb->setup0123);
3122 u.raw[1] = readl(&dev->usb->setup4567);
David Brownell901b3d72006-09-02 03:13:45 -07003123
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003124 cpu_to_le32s(&u.raw[0]);
3125 cpu_to_le32s(&u.raw[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Tim Harvey5185c9132016-05-23 06:58:41 -07003127 if ((dev->quirks & PLX_PCIE) && !dev->bug7734_patched)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003128 defect7374_workaround(dev, u.r);
3129
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003130 tmp = 0;
3131
David Brownell01ee7d72007-05-25 20:40:14 -07003132#define w_value le16_to_cpu(u.r.wValue)
3133#define w_index le16_to_cpu(u.r.wIndex)
3134#define w_length le16_to_cpu(u.r.wLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 /* ack the irq */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003137 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
3138 stat ^= BIT(SETUP_PACKET_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 /* watch control traffic at the token level, and force
3141 * synchronization before letting the status stage happen.
3142 * FIXME ignore tokens we'll NAK, until driver responds.
3143 * that'll mean a lot less irqs for some drivers.
3144 */
3145 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
3146 if (ep->is_in) {
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003147 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
3148 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3149 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003150 stop_out_naking(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 } else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003152 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
3153 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3154 BIT(DATA_IN_TOKEN_INTERRUPT);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003155 writel(scratch, &dev->epregs[0].ep_irqenb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157 /* we made the hardware handle most lowlevel requests;
3158 * everything else goes uplevel to the gadget code.
3159 */
Alan Stern1f26e282006-11-16 10:16:00 -05003160 ep->responded = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003161
3162 if (dev->gadget.speed == USB_SPEED_SUPER) {
3163 handle_stat0_irqs_superspeed(dev, ep, u.r);
3164 goto next_endpoints;
3165 }
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 switch (u.r.bRequest) {
3168 case USB_REQ_GET_STATUS: {
3169 struct net2280_ep *e;
David Brownell320f3452005-05-07 13:05:18 -07003170 __le32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 /* hw handles device and interface status */
3173 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
3174 goto delegate;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003175 e = get_ep_by_addr(dev, w_index);
3176 if (!e || w_length > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 goto do_stall;
3178
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003179 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003180 status = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 else
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003182 status = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 /* don't bother with a request object! */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003185 writel(0, &dev->epregs[0].ep_irqenb);
3186 set_fifo_bytecount(ep, w_length);
3187 writel((__force u32)status, &dev->epregs[0].ep_data);
3188 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003189 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 goto next_endpoints;
3191 }
3192 break;
3193 case USB_REQ_CLEAR_FEATURE: {
3194 struct net2280_ep *e;
3195
3196 /* hw handles device features */
3197 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3198 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003199 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003201 e = get_ep_by_addr(dev, w_index);
3202 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003204 if (e->wedged) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003205 ep_vdbg(dev, "%s wedged, halt not cleared\n",
Alan Stern80661342008-08-14 15:49:11 -04003206 ep->ep.name);
3207 } else {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003208 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
Alan Stern80661342008-08-14 15:49:11 -04003209 clear_halt(e);
Tim Harvey5185c9132016-05-23 06:58:41 -07003210 if ((ep->dev->quirks & PLX_PCIE) &&
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003211 !list_empty(&e->queue) && e->td_dma)
3212 restart_dma(e);
Alan Stern80661342008-08-14 15:49:11 -04003213 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003214 allow_status(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 goto next_endpoints;
3216 }
3217 break;
3218 case USB_REQ_SET_FEATURE: {
3219 struct net2280_ep *e;
3220
3221 /* hw handles device features */
3222 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3223 goto delegate;
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003224 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003226 e = get_ep_by_addr(dev, w_index);
3227 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 goto do_stall;
Alan Stern80661342008-08-14 15:49:11 -04003229 if (e->ep.name == ep0name)
3230 goto do_stall;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003231 set_halt(e);
Tim Harvey5185c9132016-05-23 06:58:41 -07003232 if ((dev->quirks & PLX_PCIE) && e->dma)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003233 abort_dma(e);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003234 allow_status(ep);
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003235 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 goto next_endpoints;
3237 }
3238 break;
3239 default:
3240delegate:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003241 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 "ep_cfg %08x\n",
3243 u.r.bRequestType, u.r.bRequest,
David Brownell320f3452005-05-07 13:05:18 -07003244 w_value, w_index, w_length,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003245 readl(&ep->cfg->ep_cfg));
Alan Stern1f26e282006-11-16 10:16:00 -05003246 ep->responded = 0;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003247 spin_unlock(&dev->lock);
3248 tmp = dev->driver->setup(&dev->gadget, &u.r);
3249 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 }
3251
3252 /* stall ep0 on error */
3253 if (tmp < 0) {
3254do_stall:
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003255 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 u.r.bRequestType, u.r.bRequest, tmp);
3257 dev->protocol_stall = 1;
3258 }
3259
3260 /* some in/out token irq should follow; maybe stall then.
3261 * driver must queue a request (even zlp) or halt ep0
3262 * before the host times out.
3263 */
3264 }
3265
David Brownell320f3452005-05-07 13:05:18 -07003266#undef w_value
3267#undef w_index
3268#undef w_length
3269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270next_endpoints:
Tim Harvey5185c9132016-05-23 06:58:41 -07003271 if ((dev->quirks & PLX_PCIE) && dev->enhanced_mode) {
Mian Yousaf Kaukaba09e23f2015-05-16 22:33:35 +02003272 u32 mask = (BIT(ENDPOINT_0_INTERRUPT) |
3273 USB3380_IRQSTAT0_EP_INTR_MASK_IN |
3274 USB3380_IRQSTAT0_EP_INTR_MASK_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Mian Yousaf Kaukaba09e23f2015-05-16 22:33:35 +02003276 if (stat & mask) {
3277 usb338x_handle_ep_intr(dev, stat & mask);
3278 stat &= ~mask;
3279 }
3280 } else {
3281 /* endpoint data irq ? */
3282 scratch = stat & 0x7f;
3283 stat &= ~0x7f;
3284 for (num = 0; scratch; num++) {
3285 u32 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Mian Yousaf Kaukaba09e23f2015-05-16 22:33:35 +02003287 /* do this endpoint's FIFO and queue need tending? */
3288 t = BIT(num);
3289 if ((scratch & t) == 0)
3290 continue;
3291 scratch ^= t;
3292
3293 ep = &dev->ep[num];
3294 handle_ep_small(ep);
3295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 }
3297
3298 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003299 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003302#define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3303 BIT(DMA_C_INTERRUPT) | \
3304 BIT(DMA_B_INTERRUPT) | \
3305 BIT(DMA_A_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306#define PCI_ERROR_INTERRUPTS ( \
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003307 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3308 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3309 BIT(PCI_RETRY_ABORT_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003311static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
Felipe Balbiccf5fb62015-02-27 09:44:51 -06003312__releases(dev->lock)
3313__acquires(dev->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
3315 struct net2280_ep *ep;
3316 u32 tmp, num, mask, scratch;
3317
3318 /* after disconnect there's nothing else to do! */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003319 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3320 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03003323 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
David Brownell901b3d72006-09-02 03:13:45 -07003324 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 * only indicates a change in the reset state).
3326 */
3327 if (stat & tmp) {
Alan Sternb611e422014-11-06 14:27:56 +08003328 bool reset = false;
3329 bool disconnect = false;
3330
3331 /*
3332 * Ignore disconnects and resets if the speed hasn't been set.
3333 * VBUS can bounce and there's always an initial reset.
3334 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003335 writel(tmp, &dev->regs->irqstat1);
Alan Sternb611e422014-11-06 14:27:56 +08003336 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3337 if ((stat & BIT(VBUS_INTERRUPT)) &&
3338 (readl(&dev->usb->usbctl) &
3339 BIT(VBUS_PIN)) == 0) {
3340 disconnect = true;
3341 ep_dbg(dev, "disconnect %s\n",
3342 dev->driver->driver.name);
3343 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3344 (readl(&dev->usb->usbstat) & mask)
3345 == 0) {
3346 reset = true;
3347 ep_dbg(dev, "reset %s\n",
3348 dev->driver->driver.name);
3349 }
3350
3351 if (disconnect || reset) {
3352 stop_activity(dev, dev->driver);
3353 ep0_start(dev);
3354 spin_unlock(&dev->lock);
3355 if (reset)
3356 usb_gadget_udc_reset
3357 (&dev->gadget, dev->driver);
3358 else
3359 (dev->driver->disconnect)
3360 (&dev->gadget);
3361 spin_lock(&dev->lock);
3362 return;
3363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
3365 stat &= ~tmp;
3366
3367 /* vBUS can bounce ... one of many reasons to ignore the
3368 * notion of hotplug events on bus connect/disconnect!
3369 */
3370 if (!stat)
3371 return;
3372 }
3373
3374 /* NOTE: chip stays in PCI D0 state for now, but it could
3375 * enter D1 to save more power
3376 */
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003377 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 if (stat & tmp) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003379 writel(tmp, &dev->regs->irqstat1);
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003380 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (dev->driver->suspend)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003382 dev->driver->suspend(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 if (!enable_suspend)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003384 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 } else {
3386 if (dev->driver->resume)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003387 dev->driver->resume(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 /* at high speed, note erratum 0133 */
3389 }
3390 stat &= ~tmp;
3391 }
3392
3393 /* clear any other status/irqs */
3394 if (stat)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003395 writel(stat, &dev->regs->irqstat1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 /* some status we can just ignore */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003398 if (dev->quirks & PLX_2280)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003399 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3400 BIT(SUSPEND_REQUEST_INTERRUPT) |
3401 BIT(RESUME_INTERRUPT) |
3402 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003403 else
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003404 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3405 BIT(RESUME_INTERRUPT) |
3406 BIT(SOF_DOWN_INTERRUPT) |
3407 BIT(SOF_INTERRUPT));
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 if (!stat)
3410 return;
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003411 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
3413 /* DMA status, for ep-{a,b,c,d} */
3414 scratch = stat & DMA_INTERRUPTS;
3415 stat &= ~DMA_INTERRUPTS;
3416 scratch >>= 9;
3417 for (num = 0; scratch; num++) {
3418 struct net2280_dma_regs __iomem *dma;
3419
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003420 tmp = BIT(num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if ((tmp & scratch) == 0)
3422 continue;
3423 scratch ^= tmp;
3424
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003425 ep = &dev->ep[num + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 dma = ep->dma;
3427
3428 if (!dma)
3429 continue;
3430
3431 /* clear ep's dma status */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003432 tmp = readl(&dma->dmastat);
3433 writel(tmp, &dma->dmastat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003435 /* dma sync*/
Tim Harvey5185c9132016-05-23 06:58:41 -07003436 if (dev->quirks & PLX_PCIE) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003437 u32 r_dmacount = readl(&dma->dmacount);
3438 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003439 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003440 continue;
3441 }
3442
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003443 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3444 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3445 ep->ep.name, tmp);
3446 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 }
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003448 stop_dma(ep->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 /* OUT transfers terminate when the data from the
3451 * host is in our memory. Process whatever's done.
3452 * On this path, we know transfer's last packet wasn't
3453 * less than req->length. NAK_OUT_PACKETS may be set,
3454 * or the FIFO may already be holding new packets.
3455 *
3456 * IN transfers can linger in the FIFO for a very
3457 * long time ... we ignore that for now, accounting
3458 * precisely (like PIO does) needs per-packet irqs
3459 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003460 scan_dma_completions(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 /* disable dma on inactive queues; else maybe restart */
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003463 if (!list_empty(&ep->queue)) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003464 tmp = readl(&dma->dmactl);
Ricardo Ribalda Delgado90664192014-11-28 14:50:46 +01003465 restart_dma(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
3467 ep->irqs++;
3468 }
3469
3470 /* NOTE: there are other PCI errors we might usefully notice.
3471 * if they appear very often, here's where to try recovering.
3472 */
3473 if (stat & PCI_ERROR_INTERRUPTS) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003474 ep_err(dev, "pci dma error; stat %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 stat &= ~PCI_ERROR_INTERRUPTS;
3476 /* these are fatal errors, but "maybe" they won't
3477 * happen again ...
3478 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003479 stop_activity(dev, dev->driver);
3480 ep0_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 stat = 0;
3482 }
3483
3484 if (stat)
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003485 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003488static irqreturn_t net2280_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
3490 struct net2280 *dev = _dev;
3491
Alan Stern658ad5e2006-04-14 16:44:11 -04003492 /* shared interrupt, not ours */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003493 if ((dev->quirks & PLX_LEGACY) &&
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003494 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
Alan Stern658ad5e2006-04-14 16:44:11 -04003495 return IRQ_NONE;
3496
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003497 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
3499 /* handle disconnect, dma, and more */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003500 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502 /* control requests and PIO */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003503 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Tim Harvey5185c9132016-05-23 06:58:41 -07003505 if (dev->quirks & PLX_PCIE) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003506 /* re-enable interrupt to trigger any possible new interrupt */
3507 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3508 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3509 writel(pciirqenb1, &dev->regs->pciirqenb1);
3510 }
3511
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003512 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 return IRQ_HANDLED;
3515}
3516
3517/*-------------------------------------------------------------------------*/
3518
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003519static void gadget_release(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003521 struct net2280 *dev = dev_get_drvdata(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003523 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
3526/* tear down the binding between this driver and the pci device */
3527
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003528static void net2280_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003530 struct net2280 *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003532 usb_del_gadget_udc(&dev->gadget);
3533
David Brownell6bea4762006-12-05 03:15:33 -08003534 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
3536 /* then clean up the resources we allocated during probe() */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003537 net2280_led_shutdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (dev->requests) {
3539 int i;
3540 for (i = 1; i < 5; i++) {
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003541 if (!dev->ep[i].dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 continue;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003543 pci_pool_free(dev->requests, dev->ep[i].dummy,
3544 dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 }
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003546 pci_pool_destroy(dev->requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
3548 if (dev->got_irq)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003549 free_irq(pdev->irq, dev);
Tim Harvey5185c9132016-05-23 06:58:41 -07003550 if (dev->quirks & PLX_PCIE)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003551 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (dev->regs)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003553 iounmap(dev->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 if (dev->region)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003555 release_mem_region(pci_resource_start(pdev, 0),
3556 pci_resource_len(pdev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (dev->enabled)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003558 pci_disable_device(pdev);
3559 device_remove_file(&pdev->dev, &dev_attr_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003561 ep_info(dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
3564/* wrap this driver around the specified device, but
3565 * don't respond over USB until a gadget driver binds to us.
3566 */
3567
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003568static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
3570 struct net2280 *dev;
3571 unsigned long resource, len;
3572 void __iomem *base = NULL;
3573 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 /* alloc, and start init */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003576 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3577 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 retval = -ENOMEM;
3579 goto done;
3580 }
3581
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003582 pci_set_drvdata(pdev, dev);
3583 spin_lock_init(&dev->lock);
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003584 dev->quirks = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 dev->pdev = pdev;
3586 dev->gadget.ops = &net2280_ops;
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003587 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003588 USB_SPEED_SUPER : USB_SPEED_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590 /* the "gadget" abstracts/virtualizes the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 dev->gadget.name = driver_name;
3592
3593 /* now all the pci goodies ... */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003594 if (pci_enable_device(pdev) < 0) {
3595 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 goto done;
3597 }
3598 dev->enabled = 1;
3599
3600 /* BAR 0 holds all the registers
3601 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3602 * BAR 2 is fifo memory; unused here
3603 */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003604 resource = pci_resource_start(pdev, 0);
3605 len = pci_resource_len(pdev, 0);
3606 if (!request_mem_region(resource, len, driver_name)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003607 ep_dbg(dev, "controller already in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 retval = -EBUSY;
3609 goto done;
3610 }
3611 dev->region = 1;
3612
David Brownell901b3d72006-09-02 03:13:45 -07003613 /* FIXME provide firmware download interface to put
3614 * 8051 code into the chip, e.g. to turn on PCI PM.
3615 */
3616
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003617 base = ioremap_nocache(resource, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 if (base == NULL) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003619 ep_dbg(dev, "can't map memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 retval = -EFAULT;
3621 goto done;
3622 }
3623 dev->regs = (struct net2280_regs __iomem *) base;
3624 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3625 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3626 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3627 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3628 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3629
Tim Harvey5185c9132016-05-23 06:58:41 -07003630 if (dev->quirks & PLX_PCIE) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003631 u32 fsmvalue;
3632 u32 usbstat;
3633 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3634 (base + 0x00b4);
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003635 dev->llregs = (struct usb338x_ll_regs __iomem *)
3636 (base + 0x0700);
3637 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3638 (base + 0x0748);
3639 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3640 (base + 0x077c);
3641 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3642 (base + 0x079c);
3643 dev->plregs = (struct usb338x_pl_regs __iomem *)
3644 (base + 0x0800);
3645 usbstat = readl(&dev->usb->usbstat);
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003646 dev->enhanced_mode = !!(usbstat & BIT(11));
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003647 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3648 /* put into initial config, link up all endpoints */
3649 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3650 (0xf << DEFECT7374_FSM_FIELD);
3651 /* See if firmware needs to set up for workaround: */
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003652 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3653 dev->bug7734_patched = 1;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003654 writel(0, &dev->usb->usbctl);
Ricardo Ribalda Delgado55175252014-11-28 14:50:57 +01003655 } else
3656 dev->bug7734_patched = 0;
3657 } else {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003658 dev->enhanced_mode = 0;
3659 dev->n_ep = 7;
3660 /* put into initial config, link up all endpoints */
3661 writel(0, &dev->usb->usbctl);
3662 }
3663
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003664 usb_reset(dev);
3665 usb_reinit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667 /* irq setup after old hardware is cleaned up */
3668 if (!pdev->irq) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003669 ep_err(dev, "No IRQ. Check PCI setup!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 retval = -ENODEV;
3671 goto done;
3672 }
David S. Millerc6387a42006-06-20 01:21:29 -07003673
Tim Harvey5185c9132016-05-23 06:58:41 -07003674 if (dev->quirks & PLX_PCIE)
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003675 if (pci_enable_msi(pdev))
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003676 ep_err(dev, "Failed to enable MSI mode\n");
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003677
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003678 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3679 driver_name, dev)) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003680 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 retval = -EBUSY;
3682 goto done;
3683 }
3684 dev->got_irq = 1;
3685
3686 /* DMA setup */
3687 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003688 dev->requests = pci_pool_create("requests", pdev,
3689 sizeof(struct net2280_dma),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 0 /* no alignment requirements */,
3691 0 /* or page-crossing issues */);
3692 if (!dev->requests) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003693 ep_dbg(dev, "can't get request pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 retval = -ENOMEM;
3695 goto done;
3696 }
3697 for (i = 1; i < 5; i++) {
3698 struct net2280_dma *td;
3699
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003700 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3701 &dev->ep[i].td_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 if (!td) {
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003703 ep_dbg(dev, "can't get dummy %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 retval = -ENOMEM;
3705 goto done;
3706 }
3707 td->dmacount = 0; /* not VALID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 td->dmadesc = td->dmaaddr;
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003709 dev->ep[i].dummy = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 }
3711
3712 /* enable lower-overhead pci memory bursts during DMA */
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003713 if (dev->quirks & PLX_LEGACY)
Ricardo Ribalda Delgado3e76fdc2014-05-20 18:30:05 +02003714 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3715 /*
3716 * 256 write retries may not be enough...
3717 BIT(PCI_RETRY_ABORT_ENABLE) |
3718 */
3719 BIT(DMA_READ_MULTIPLE_ENABLE) |
3720 BIT(DMA_READ_LINE_ENABLE),
3721 &dev->pci->pcimstctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003723 pci_set_master(pdev);
3724 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
3726 /* ... also flushes any posted pci writes */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003727 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 /* done */
Ricardo Ribalda Delgadoe56e69c2014-05-20 18:30:11 +02003730 ep_info(dev, "%s\n", driver_desc);
3731 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
David S. Millerc6387a42006-06-20 01:21:29 -07003732 pdev->irq, base, dev->chiprev);
Ricardo Ribalda Delgadod588ff52014-11-28 14:50:49 +01003733 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003734 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003735 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3736 if (retval)
3737 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Felipe Balbi2901df62013-02-26 15:15:27 +02003739 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3740 gadget_release);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003741 if (retval)
3742 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return 0;
3744
3745done:
3746 if (dev)
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003747 net2280_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return retval;
3749}
3750
Alan Stern2d61bde2006-05-05 16:23:42 -04003751/* make sure the board is quiescent; otherwise it will continue
3752 * generating IRQs across the upcoming reboot.
3753 */
3754
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003755static void net2280_shutdown(struct pci_dev *pdev)
Alan Stern2d61bde2006-05-05 16:23:42 -04003756{
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003757 struct net2280 *dev = pci_get_drvdata(pdev);
Alan Stern2d61bde2006-05-05 16:23:42 -04003758
3759 /* disable IRQs */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003760 writel(0, &dev->regs->pciirqenb0);
3761 writel(0, &dev->regs->pciirqenb1);
Alan Stern2d61bde2006-05-05 16:23:42 -04003762
3763 /* disable the pullup so the host will think we're gone */
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003764 writel(0, &dev->usb->usbctl);
Alan Stern2f076072013-01-30 16:40:14 -05003765
Alan Stern2d61bde2006-05-05 16:23:42 -04003766}
3767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769/*-------------------------------------------------------------------------*/
3770
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003771static const struct pci_device_id pci_ids[] = { {
Heikki Krogerus7b78f482016-03-15 14:06:00 +02003772 .class = PCI_CLASS_SERIAL_USB_DEVICE,
David Brownell901b3d72006-09-02 03:13:45 -07003773 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003774 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 .device = 0x2280,
3776 .subvendor = PCI_ANY_ID,
3777 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003778 .driver_data = PLX_LEGACY | PLX_2280,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003779 }, {
Heikki Krogerus7b78f482016-03-15 14:06:00 +02003780 .class = PCI_CLASS_SERIAL_USB_DEVICE,
David Brownell901b3d72006-09-02 03:13:45 -07003781 .class_mask = ~0,
Ricardo Ribalda Delgadoc2db8a82014-05-20 18:30:04 +02003782 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
Guennadi Liakhovetski950ee4c2006-03-19 20:49:14 +01003783 .device = 0x2282,
3784 .subvendor = PCI_ANY_ID,
3785 .subdevice = PCI_ANY_ID,
Ricardo Ribalda Delgado2eeb0012014-05-20 18:30:12 +02003786 .driver_data = PLX_LEGACY,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003787 },
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003788 {
Heikki Krogerus7b78f482016-03-15 14:06:00 +02003789 .class = PCI_CLASS_SERIAL_USB_DEVICE,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003790 .class_mask = ~0,
3791 .vendor = PCI_VENDOR_ID_PLX,
Tim Harvey5185c9132016-05-23 06:58:41 -07003792 .device = 0x2380,
3793 .subvendor = PCI_ANY_ID,
3794 .subdevice = PCI_ANY_ID,
3795 .driver_data = PLX_PCIE,
3796 },
3797 {
3798 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3799 .class_mask = ~0,
3800 .vendor = PCI_VENDOR_ID_PLX,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003801 .device = 0x3380,
3802 .subvendor = PCI_ANY_ID,
3803 .subdevice = PCI_ANY_ID,
Tim Harvey5185c9132016-05-23 06:58:41 -07003804 .driver_data = PLX_PCIE | PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003805 },
3806 {
Heikki Krogerus7b78f482016-03-15 14:06:00 +02003807 .class = PCI_CLASS_SERIAL_USB_DEVICE,
Ricardo Ribalda Delgadoae8e5302014-05-20 18:30:10 +02003808 .class_mask = ~0,
3809 .vendor = PCI_VENDOR_ID_PLX,
3810 .device = 0x3382,
3811 .subvendor = PCI_ANY_ID,
3812 .subdevice = PCI_ANY_ID,
Tim Harvey5185c9132016-05-23 06:58:41 -07003813 .driver_data = PLX_PCIE | PLX_SUPERSPEED,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02003814 },
3815{ /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816};
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003817MODULE_DEVICE_TABLE(pci, pci_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819/* pci driver glue; this is a "new style" PCI driver module */
3820static struct pci_driver net2280_pci_driver = {
3821 .name = (char *) driver_name,
3822 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 .probe = net2280_probe,
3825 .remove = net2280_remove,
Alan Stern2d61bde2006-05-05 16:23:42 -04003826 .shutdown = net2280_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
3828 /* FIXME add power management support */
3829};
3830
Ricardo Ribalda Delgado9a028e42014-05-20 18:30:07 +02003831module_pci_driver(net2280_pci_driver);
3832
Ricardo Ribalda Delgadofae3c152014-05-20 18:30:09 +02003833MODULE_DESCRIPTION(DRIVER_DESC);
3834MODULE_AUTHOR("David Brownell");
3835MODULE_LICENSE("GPL");