blob: 857159dd5ae05f3e00c7324fd9b696ee38bc356e [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302/*
3 * Xilinx USB peripheral controller driver
4 *
5 * Copyright (C) 2004 by Thomas Rathbone
6 * Copyright (C) 2005 by HP Labs
7 * Copyright (C) 2005 by David Brownell
8 * Copyright (C) 2010 - 2014 Xilinx, Inc.
9 *
10 * Some parts of this driver code is based on the driver for at91-series
11 * USB peripheral controller (at91_udc.c).
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +053012 */
13
Shubhrajyoti Datta24749222021-09-28 10:57:11 +053014#include <linux/clk.h>
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +053015#include <linux/delay.h>
16#include <linux/device.h>
17#include <linux/dma-mapping.h>
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/of_address.h>
22#include <linux/of_device.h>
23#include <linux/of_platform.h>
24#include <linux/of_irq.h>
25#include <linux/prefetch.h>
26#include <linux/usb/ch9.h>
27#include <linux/usb/gadget.h>
28
29/* Register offsets for the USB device.*/
30#define XUSB_EP0_CONFIG_OFFSET 0x0000 /* EP0 Config Reg Offset */
31#define XUSB_SETUP_PKT_ADDR_OFFSET 0x0080 /* Setup Packet Address */
32#define XUSB_ADDRESS_OFFSET 0x0100 /* Address Register */
33#define XUSB_CONTROL_OFFSET 0x0104 /* Control Register */
34#define XUSB_STATUS_OFFSET 0x0108 /* Status Register */
35#define XUSB_FRAMENUM_OFFSET 0x010C /* Frame Number Register */
36#define XUSB_IER_OFFSET 0x0110 /* Interrupt Enable Register */
37#define XUSB_BUFFREADY_OFFSET 0x0114 /* Buffer Ready Register */
38#define XUSB_TESTMODE_OFFSET 0x0118 /* Test Mode Register */
39#define XUSB_DMA_RESET_OFFSET 0x0200 /* DMA Soft Reset Register */
40#define XUSB_DMA_CONTROL_OFFSET 0x0204 /* DMA Control Register */
41#define XUSB_DMA_DSAR_ADDR_OFFSET 0x0208 /* DMA source Address Reg */
42#define XUSB_DMA_DDAR_ADDR_OFFSET 0x020C /* DMA destination Addr Reg */
43#define XUSB_DMA_LENGTH_OFFSET 0x0210 /* DMA Length Register */
44#define XUSB_DMA_STATUS_OFFSET 0x0214 /* DMA Status Register */
45
46/* Endpoint Configuration Space offsets */
47#define XUSB_EP_CFGSTATUS_OFFSET 0x00 /* Endpoint Config Status */
48#define XUSB_EP_BUF0COUNT_OFFSET 0x08 /* Buffer 0 Count */
49#define XUSB_EP_BUF1COUNT_OFFSET 0x0C /* Buffer 1 Count */
50
51#define XUSB_CONTROL_USB_READY_MASK 0x80000000 /* USB ready Mask */
52#define XUSB_CONTROL_USB_RMTWAKE_MASK 0x40000000 /* Remote wake up mask */
53
54/* Interrupt register related masks.*/
55#define XUSB_STATUS_GLOBAL_INTR_MASK 0x80000000 /* Global Intr Enable */
56#define XUSB_STATUS_DMADONE_MASK 0x04000000 /* DMA done Mask */
57#define XUSB_STATUS_DMAERR_MASK 0x02000000 /* DMA Error Mask */
58#define XUSB_STATUS_DMABUSY_MASK 0x80000000 /* DMA Error Mask */
59#define XUSB_STATUS_RESUME_MASK 0x01000000 /* USB Resume Mask */
60#define XUSB_STATUS_RESET_MASK 0x00800000 /* USB Reset Mask */
61#define XUSB_STATUS_SUSPEND_MASK 0x00400000 /* USB Suspend Mask */
62#define XUSB_STATUS_DISCONNECT_MASK 0x00200000 /* USB Disconnect Mask */
63#define XUSB_STATUS_FIFO_BUFF_RDY_MASK 0x00100000 /* FIFO Buff Ready Mask */
64#define XUSB_STATUS_FIFO_BUFF_FREE_MASK 0x00080000 /* FIFO Buff Free Mask */
65#define XUSB_STATUS_SETUP_PACKET_MASK 0x00040000 /* Setup packet received */
66#define XUSB_STATUS_EP1_BUFF2_COMP_MASK 0x00000200 /* EP 1 Buff 2 Processed */
67#define XUSB_STATUS_EP1_BUFF1_COMP_MASK 0x00000002 /* EP 1 Buff 1 Processed */
68#define XUSB_STATUS_EP0_BUFF2_COMP_MASK 0x00000100 /* EP 0 Buff 2 Processed */
69#define XUSB_STATUS_EP0_BUFF1_COMP_MASK 0x00000001 /* EP 0 Buff 1 Processed */
70#define XUSB_STATUS_HIGH_SPEED_MASK 0x00010000 /* USB Speed Mask */
71/* Suspend,Reset,Suspend and Disconnect Mask */
72#define XUSB_STATUS_INTR_EVENT_MASK 0x01E00000
73/* Buffers completion Mask */
74#define XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK 0x0000FEFF
75/* Mask for buffer 0 and buffer 1 completion for all Endpoints */
76#define XUSB_STATUS_INTR_BUFF_COMP_SHIFT_MASK 0x00000101
77#define XUSB_STATUS_EP_BUFF2_SHIFT 8 /* EP buffer offset */
78
79/* Endpoint Configuration Status Register */
80#define XUSB_EP_CFG_VALID_MASK 0x80000000 /* Endpoint Valid bit */
81#define XUSB_EP_CFG_STALL_MASK 0x40000000 /* Endpoint Stall bit */
82#define XUSB_EP_CFG_DATA_TOGGLE_MASK 0x08000000 /* Endpoint Data toggle */
83
84/* USB device specific global configuration constants.*/
85#define XUSB_MAX_ENDPOINTS 8 /* Maximum End Points */
86#define XUSB_EP_NUMBER_ZERO 0 /* End point Zero */
87/* DPRAM is the source address for DMA transfer */
88#define XUSB_DMA_READ_FROM_DPRAM 0x80000000
89#define XUSB_DMA_DMASR_BUSY 0x80000000 /* DMA busy */
90#define XUSB_DMA_DMASR_ERROR 0x40000000 /* DMA Error */
91/*
92 * When this bit is set, the DMA buffer ready bit is set by hardware upon
93 * DMA transfer completion.
94 */
95#define XUSB_DMA_BRR_CTRL 0x40000000 /* DMA bufready ctrl bit */
96/* Phase States */
97#define SETUP_PHASE 0x0000 /* Setup Phase */
98#define DATA_PHASE 0x0001 /* Data Phase */
99#define STATUS_PHASE 0x0002 /* Status Phase */
100
101#define EP0_MAX_PACKET 64 /* Endpoint 0 maximum packet length */
102#define STATUSBUFF_SIZE 2 /* Buffer size for GET_STATUS command */
103#define EPNAME_SIZE 4 /* Buffer size for endpoint name */
104
105/* container_of helper macros */
106#define to_udc(g) container_of((g), struct xusb_udc, gadget)
107#define to_xusb_ep(ep) container_of((ep), struct xusb_ep, ep_usb)
108#define to_xusb_req(req) container_of((req), struct xusb_req, usb_req)
109
110/**
111 * struct xusb_req - Xilinx USB device request structure
112 * @usb_req: Linux usb request structure
113 * @queue: usb device request queue
114 * @ep: pointer to xusb_endpoint structure
115 */
116struct xusb_req {
117 struct usb_request usb_req;
118 struct list_head queue;
119 struct xusb_ep *ep;
120};
121
122/**
123 * struct xusb_ep - USB end point structure.
124 * @ep_usb: usb endpoint instance
125 * @queue: endpoint message queue
126 * @udc: xilinx usb peripheral driver instance pointer
127 * @desc: pointer to the usb endpoint descriptor
128 * @rambase: the endpoint buffer address
129 * @offset: the endpoint register offset value
130 * @name: name of the endpoint
131 * @epnumber: endpoint number
132 * @maxpacket: maximum packet size the endpoint can store
133 * @buffer0count: the size of the packet recieved in the first buffer
134 * @buffer1count: the size of the packet received in the second buffer
135 * @curbufnum: current buffer of endpoint that will be processed next
136 * @buffer0ready: the busy state of first buffer
137 * @buffer1ready: the busy state of second buffer
138 * @is_in: endpoint direction (IN or OUT)
139 * @is_iso: endpoint type(isochronous or non isochronous)
140 */
141struct xusb_ep {
142 struct usb_ep ep_usb;
143 struct list_head queue;
144 struct xusb_udc *udc;
145 const struct usb_endpoint_descriptor *desc;
146 u32 rambase;
147 u32 offset;
148 char name[4];
149 u16 epnumber;
150 u16 maxpacket;
151 u16 buffer0count;
152 u16 buffer1count;
153 u8 curbufnum;
154 bool buffer0ready;
155 bool buffer1ready;
156 bool is_in;
157 bool is_iso;
158};
159
160/**
161 * struct xusb_udc - USB peripheral driver structure
162 * @gadget: USB gadget driver instance
163 * @ep: an array of endpoint structures
164 * @driver: pointer to the usb gadget driver instance
165 * @setup: usb_ctrlrequest structure for control requests
166 * @req: pointer to dummy request for get status command
167 * @dev: pointer to device structure in gadget
168 * @usb_state: device in suspended state or not
169 * @remote_wkp: remote wakeup enabled by host
170 * @setupseqtx: tx status
171 * @setupseqrx: rx status
172 * @addr: the usb device base address
173 * @lock: instance of spinlock
174 * @dma_enabled: flag indicating whether the dma is included in the system
Shubhrajyoti Datta24749222021-09-28 10:57:11 +0530175 * @clk: pointer to struct clk
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530176 * @read_fn: function pointer to read device registers
177 * @write_fn: function pointer to write to device registers
178 */
179struct xusb_udc {
180 struct usb_gadget gadget;
181 struct xusb_ep ep[8];
182 struct usb_gadget_driver *driver;
183 struct usb_ctrlrequest setup;
184 struct xusb_req *req;
185 struct device *dev;
186 u32 usb_state;
187 u32 remote_wkp;
188 u32 setupseqtx;
189 u32 setupseqrx;
190 void __iomem *addr;
191 spinlock_t lock;
192 bool dma_enabled;
Shubhrajyoti Datta24749222021-09-28 10:57:11 +0530193 struct clk *clk;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530194
195 unsigned int (*read_fn)(void __iomem *);
196 void (*write_fn)(void __iomem *, u32, u32);
197};
198
199/* Endpoint buffer start addresses in the core */
200static u32 rambase[8] = { 0x22, 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500,
201 0x1600 };
202
203static const char driver_name[] = "xilinx-udc";
204static const char ep0name[] = "ep0";
205
206/* Control endpoint configuration.*/
207static const struct usb_endpoint_descriptor config_bulk_out_desc = {
208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
210 .bEndpointAddress = USB_DIR_OUT,
211 .bmAttributes = USB_ENDPOINT_XFER_BULK,
212 .wMaxPacketSize = cpu_to_le16(EP0_MAX_PACKET),
213};
214
215/**
216 * xudc_write32 - little endian write to device registers
217 * @addr: base addr of device registers
218 * @offset: register offset
219 * @val: data to be written
220 */
221static void xudc_write32(void __iomem *addr, u32 offset, u32 val)
222{
223 iowrite32(val, addr + offset);
224}
225
226/**
227 * xudc_read32 - little endian read from device registers
228 * @addr: addr of device register
229 * Return: value at addr
230 */
231static unsigned int xudc_read32(void __iomem *addr)
232{
233 return ioread32(addr);
234}
235
236/**
237 * xudc_write32_be - big endian write to device registers
238 * @addr: base addr of device registers
239 * @offset: register offset
240 * @val: data to be written
241 */
242static void xudc_write32_be(void __iomem *addr, u32 offset, u32 val)
243{
244 iowrite32be(val, addr + offset);
245}
246
247/**
248 * xudc_read32_be - big endian read from device registers
249 * @addr: addr of device register
250 * Return: value at addr
251 */
252static unsigned int xudc_read32_be(void __iomem *addr)
253{
254 return ioread32be(addr);
255}
256
257/**
258 * xudc_wrstatus - Sets up the usb device status stages.
259 * @udc: pointer to the usb device controller structure.
260 */
261static void xudc_wrstatus(struct xusb_udc *udc)
262{
263 struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
264 u32 epcfgreg;
265
266 epcfgreg = udc->read_fn(udc->addr + ep0->offset)|
267 XUSB_EP_CFG_DATA_TOGGLE_MASK;
268 udc->write_fn(udc->addr, ep0->offset, epcfgreg);
269 udc->write_fn(udc->addr, ep0->offset + XUSB_EP_BUF0COUNT_OFFSET, 0);
270 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
271}
272
273/**
274 * xudc_epconfig - Configures the given endpoint.
275 * @ep: pointer to the usb device endpoint structure.
276 * @udc: pointer to the usb peripheral controller structure.
277 *
278 * This function configures a specific endpoint with the given configuration
279 * data.
280 */
281static void xudc_epconfig(struct xusb_ep *ep, struct xusb_udc *udc)
282{
283 u32 epcfgreg;
284
285 /*
286 * Configure the end point direction, type, Max Packet Size and the
287 * EP buffer location.
288 */
289 epcfgreg = ((ep->is_in << 29) | (ep->is_iso << 28) |
290 (ep->ep_usb.maxpacket << 15) | (ep->rambase));
291 udc->write_fn(udc->addr, ep->offset, epcfgreg);
292
293 /* Set the Buffer count and the Buffer ready bits.*/
294 udc->write_fn(udc->addr, ep->offset + XUSB_EP_BUF0COUNT_OFFSET,
295 ep->buffer0count);
296 udc->write_fn(udc->addr, ep->offset + XUSB_EP_BUF1COUNT_OFFSET,
297 ep->buffer1count);
298 if (ep->buffer0ready)
299 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
300 1 << ep->epnumber);
301 if (ep->buffer1ready)
302 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
303 1 << (ep->epnumber + XUSB_STATUS_EP_BUFF2_SHIFT));
304}
305
306/**
307 * xudc_start_dma - Starts DMA transfer.
308 * @ep: pointer to the usb device endpoint structure.
309 * @src: DMA source address.
310 * @dst: DMA destination address.
311 * @length: number of bytes to transfer.
312 *
313 * Return: 0 on success, error code on failure
314 *
315 * This function starts DMA transfer by writing to DMA source,
316 * destination and lenth registers.
317 */
318static int xudc_start_dma(struct xusb_ep *ep, dma_addr_t src,
319 dma_addr_t dst, u32 length)
320{
321 struct xusb_udc *udc = ep->udc;
322 int rc = 0;
323 u32 timeout = 500;
324 u32 reg;
325
326 /*
327 * Set the addresses in the DMA source and
328 * destination registers and then set the length
329 * into the DMA length register.
330 */
331 udc->write_fn(udc->addr, XUSB_DMA_DSAR_ADDR_OFFSET, src);
332 udc->write_fn(udc->addr, XUSB_DMA_DDAR_ADDR_OFFSET, dst);
333 udc->write_fn(udc->addr, XUSB_DMA_LENGTH_OFFSET, length);
334
335 /*
336 * Wait till DMA transaction is complete and
337 * check whether the DMA transaction was
338 * successful.
339 */
340 do {
341 reg = udc->read_fn(udc->addr + XUSB_DMA_STATUS_OFFSET);
342 if (!(reg & XUSB_DMA_DMASR_BUSY))
343 break;
344
345 /*
346 * We can't sleep here, because it's also called from
347 * interrupt context.
348 */
349 timeout--;
350 if (!timeout) {
351 dev_err(udc->dev, "DMA timeout\n");
352 return -ETIMEDOUT;
353 }
354 udelay(1);
355 } while (1);
356
357 if ((udc->read_fn(udc->addr + XUSB_DMA_STATUS_OFFSET) &
358 XUSB_DMA_DMASR_ERROR) == XUSB_DMA_DMASR_ERROR){
359 dev_err(udc->dev, "DMA Error\n");
360 rc = -EINVAL;
361 }
362
363 return rc;
364}
365
366/**
367 * xudc_dma_send - Sends IN data using DMA.
368 * @ep: pointer to the usb device endpoint structure.
369 * @req: pointer to the usb request structure.
370 * @buffer: pointer to data to be sent.
371 * @length: number of bytes to send.
372 *
373 * Return: 0 on success, -EAGAIN if no buffer is free and error
374 * code on failure.
375 *
376 * This function sends data using DMA.
377 */
378static int xudc_dma_send(struct xusb_ep *ep, struct xusb_req *req,
379 u8 *buffer, u32 length)
380{
381 u32 *eprambase;
382 dma_addr_t src;
383 dma_addr_t dst;
384 struct xusb_udc *udc = ep->udc;
385
386 src = req->usb_req.dma + req->usb_req.actual;
387 if (req->usb_req.length)
388 dma_sync_single_for_device(udc->dev, src,
389 length, DMA_TO_DEVICE);
390 if (!ep->curbufnum && !ep->buffer0ready) {
391 /* Get the Buffer address and copy the transmit data.*/
392 eprambase = (u32 __force *)(udc->addr + ep->rambase);
393 dst = virt_to_phys(eprambase);
394 udc->write_fn(udc->addr, ep->offset +
395 XUSB_EP_BUF0COUNT_OFFSET, length);
396 udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
397 XUSB_DMA_BRR_CTRL | (1 << ep->epnumber));
398 ep->buffer0ready = 1;
399 ep->curbufnum = 1;
400 } else if (ep->curbufnum && !ep->buffer1ready) {
401 /* Get the Buffer address and copy the transmit data.*/
402 eprambase = (u32 __force *)(udc->addr + ep->rambase +
403 ep->ep_usb.maxpacket);
404 dst = virt_to_phys(eprambase);
405 udc->write_fn(udc->addr, ep->offset +
406 XUSB_EP_BUF1COUNT_OFFSET, length);
407 udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
408 XUSB_DMA_BRR_CTRL | (1 << (ep->epnumber +
409 XUSB_STATUS_EP_BUFF2_SHIFT)));
410 ep->buffer1ready = 1;
411 ep->curbufnum = 0;
412 } else {
413 /* None of ping pong buffers are ready currently .*/
414 return -EAGAIN;
415 }
416
417 return xudc_start_dma(ep, src, dst, length);
418}
419
420/**
421 * xudc_dma_receive - Receives OUT data using DMA.
422 * @ep: pointer to the usb device endpoint structure.
423 * @req: pointer to the usb request structure.
424 * @buffer: pointer to storage buffer of received data.
425 * @length: number of bytes to receive.
426 *
427 * Return: 0 on success, -EAGAIN if no buffer is free and error
428 * code on failure.
429 *
430 * This function receives data using DMA.
431 */
432static int xudc_dma_receive(struct xusb_ep *ep, struct xusb_req *req,
433 u8 *buffer, u32 length)
434{
435 u32 *eprambase;
436 dma_addr_t src;
437 dma_addr_t dst;
438 struct xusb_udc *udc = ep->udc;
439
440 dst = req->usb_req.dma + req->usb_req.actual;
441 if (!ep->curbufnum && !ep->buffer0ready) {
442 /* Get the Buffer address and copy the transmit data */
443 eprambase = (u32 __force *)(udc->addr + ep->rambase);
444 src = virt_to_phys(eprambase);
445 udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
446 XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
447 (1 << ep->epnumber));
448 ep->buffer0ready = 1;
449 ep->curbufnum = 1;
450 } else if (ep->curbufnum && !ep->buffer1ready) {
451 /* Get the Buffer address and copy the transmit data */
452 eprambase = (u32 __force *)(udc->addr +
453 ep->rambase + ep->ep_usb.maxpacket);
454 src = virt_to_phys(eprambase);
455 udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
456 XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
457 (1 << (ep->epnumber +
458 XUSB_STATUS_EP_BUFF2_SHIFT)));
459 ep->buffer1ready = 1;
460 ep->curbufnum = 0;
461 } else {
462 /* None of the ping-pong buffers are ready currently */
463 return -EAGAIN;
464 }
465
466 return xudc_start_dma(ep, src, dst, length);
467}
468
469/**
470 * xudc_eptxrx - Transmits or receives data to or from an endpoint.
471 * @ep: pointer to the usb endpoint configuration structure.
472 * @req: pointer to the usb request structure.
473 * @bufferptr: pointer to buffer containing the data to be sent.
474 * @bufferlen: The number of data bytes to be sent.
475 *
476 * Return: 0 on success, -EAGAIN if no buffer is free.
477 *
478 * This function copies the transmit/receive data to/from the end point buffer
479 * and enables the buffer for transmission/reception.
480 */
481static int xudc_eptxrx(struct xusb_ep *ep, struct xusb_req *req,
482 u8 *bufferptr, u32 bufferlen)
483{
484 u32 *eprambase;
485 u32 bytestosend;
486 int rc = 0;
487 struct xusb_udc *udc = ep->udc;
488
489 bytestosend = bufferlen;
490 if (udc->dma_enabled) {
491 if (ep->is_in)
492 rc = xudc_dma_send(ep, req, bufferptr, bufferlen);
493 else
494 rc = xudc_dma_receive(ep, req, bufferptr, bufferlen);
495 return rc;
496 }
497 /* Put the transmit buffer into the correct ping-pong buffer.*/
498 if (!ep->curbufnum && !ep->buffer0ready) {
499 /* Get the Buffer address and copy the transmit data.*/
500 eprambase = (u32 __force *)(udc->addr + ep->rambase);
501 if (ep->is_in) {
502 memcpy(eprambase, bufferptr, bytestosend);
503 udc->write_fn(udc->addr, ep->offset +
504 XUSB_EP_BUF0COUNT_OFFSET, bufferlen);
505 } else {
506 memcpy(bufferptr, eprambase, bytestosend);
507 }
508 /*
509 * Enable the buffer for transmission.
510 */
511 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
512 1 << ep->epnumber);
513 ep->buffer0ready = 1;
514 ep->curbufnum = 1;
515 } else if (ep->curbufnum && !ep->buffer1ready) {
516 /* Get the Buffer address and copy the transmit data.*/
517 eprambase = (u32 __force *)(udc->addr + ep->rambase +
518 ep->ep_usb.maxpacket);
519 if (ep->is_in) {
520 memcpy(eprambase, bufferptr, bytestosend);
521 udc->write_fn(udc->addr, ep->offset +
522 XUSB_EP_BUF1COUNT_OFFSET, bufferlen);
523 } else {
524 memcpy(bufferptr, eprambase, bytestosend);
525 }
526 /*
527 * Enable the buffer for transmission.
528 */
529 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
530 1 << (ep->epnumber + XUSB_STATUS_EP_BUFF2_SHIFT));
531 ep->buffer1ready = 1;
532 ep->curbufnum = 0;
533 } else {
534 /* None of the ping-pong buffers are ready currently */
535 return -EAGAIN;
536 }
537 return rc;
538}
539
540/**
541 * xudc_done - Exeutes the endpoint data transfer completion tasks.
542 * @ep: pointer to the usb device endpoint structure.
543 * @req: pointer to the usb request structure.
544 * @status: Status of the data transfer.
545 *
546 * Deletes the message from the queue and updates data transfer completion
547 * status.
548 */
549static void xudc_done(struct xusb_ep *ep, struct xusb_req *req, int status)
550{
551 struct xusb_udc *udc = ep->udc;
552
553 list_del_init(&req->queue);
554
555 if (req->usb_req.status == -EINPROGRESS)
556 req->usb_req.status = status;
557 else
558 status = req->usb_req.status;
559
560 if (status && status != -ESHUTDOWN)
561 dev_dbg(udc->dev, "%s done %p, status %d\n",
562 ep->ep_usb.name, req, status);
563 /* unmap request if DMA is present*/
564 if (udc->dma_enabled && ep->epnumber && req->usb_req.length)
565 usb_gadget_unmap_request(&udc->gadget, &req->usb_req,
566 ep->is_in);
567
568 if (req->usb_req.complete) {
569 spin_unlock(&udc->lock);
570 req->usb_req.complete(&ep->ep_usb, &req->usb_req);
571 spin_lock(&udc->lock);
572 }
573}
574
575/**
576 * xudc_read_fifo - Reads the data from the given endpoint buffer.
577 * @ep: pointer to the usb device endpoint structure.
578 * @req: pointer to the usb request structure.
579 *
580 * Return: 0 if request is completed and -EAGAIN if not completed.
581 *
582 * Pulls OUT packet data from the endpoint buffer.
583 */
584static int xudc_read_fifo(struct xusb_ep *ep, struct xusb_req *req)
585{
586 u8 *buf;
587 u32 is_short, count, bufferspace;
588 u8 bufoffset;
589 u8 two_pkts = 0;
590 int ret;
591 int retval = -EAGAIN;
592 struct xusb_udc *udc = ep->udc;
593
594 if (ep->buffer0ready && ep->buffer1ready) {
595 dev_dbg(udc->dev, "Packet NOT ready!\n");
596 return retval;
597 }
598top:
599 if (ep->curbufnum)
600 bufoffset = XUSB_EP_BUF1COUNT_OFFSET;
601 else
602 bufoffset = XUSB_EP_BUF0COUNT_OFFSET;
603
604 count = udc->read_fn(udc->addr + ep->offset + bufoffset);
605
606 if (!ep->buffer0ready && !ep->buffer1ready)
607 two_pkts = 1;
608
609 buf = req->usb_req.buf + req->usb_req.actual;
610 prefetchw(buf);
611 bufferspace = req->usb_req.length - req->usb_req.actual;
612 is_short = count < ep->ep_usb.maxpacket;
613
614 if (unlikely(!bufferspace)) {
615 /*
616 * This happens when the driver's buffer
617 * is smaller than what the host sent.
618 * discard the extra data.
619 */
620 if (req->usb_req.status != -EOVERFLOW)
621 dev_dbg(udc->dev, "%s overflow %d\n",
622 ep->ep_usb.name, count);
623 req->usb_req.status = -EOVERFLOW;
624 xudc_done(ep, req, -EOVERFLOW);
625 return 0;
626 }
627
628 ret = xudc_eptxrx(ep, req, buf, count);
629 switch (ret) {
630 case 0:
631 req->usb_req.actual += min(count, bufferspace);
632 dev_dbg(udc->dev, "read %s, %d bytes%s req %p %d/%d\n",
633 ep->ep_usb.name, count, is_short ? "/S" : "", req,
634 req->usb_req.actual, req->usb_req.length);
635 bufferspace -= count;
636 /* Completion */
637 if ((req->usb_req.actual == req->usb_req.length) || is_short) {
638 if (udc->dma_enabled && req->usb_req.length)
639 dma_sync_single_for_cpu(udc->dev,
640 req->usb_req.dma,
641 req->usb_req.actual,
642 DMA_FROM_DEVICE);
643 xudc_done(ep, req, 0);
644 return 0;
645 }
646 if (two_pkts) {
647 two_pkts = 0;
648 goto top;
649 }
650 break;
651 case -EAGAIN:
652 dev_dbg(udc->dev, "receive busy\n");
653 break;
654 case -EINVAL:
655 case -ETIMEDOUT:
656 /* DMA error, dequeue the request */
657 xudc_done(ep, req, -ECONNRESET);
658 retval = 0;
659 break;
660 }
661
662 return retval;
663}
664
665/**
666 * xudc_write_fifo - Writes data into the given endpoint buffer.
667 * @ep: pointer to the usb device endpoint structure.
668 * @req: pointer to the usb request structure.
669 *
670 * Return: 0 if request is completed and -EAGAIN if not completed.
671 *
672 * Loads endpoint buffer for an IN packet.
673 */
674static int xudc_write_fifo(struct xusb_ep *ep, struct xusb_req *req)
675{
676 u32 max;
677 u32 length;
678 int ret;
679 int retval = -EAGAIN;
680 struct xusb_udc *udc = ep->udc;
681 int is_last, is_short = 0;
682 u8 *buf;
683
684 max = le16_to_cpu(ep->desc->wMaxPacketSize);
685 buf = req->usb_req.buf + req->usb_req.actual;
686 prefetch(buf);
687 length = req->usb_req.length - req->usb_req.actual;
688 length = min(length, max);
689
690 ret = xudc_eptxrx(ep, req, buf, length);
691 switch (ret) {
692 case 0:
693 req->usb_req.actual += length;
694 if (unlikely(length != max)) {
695 is_last = is_short = 1;
696 } else {
697 if (likely(req->usb_req.length !=
698 req->usb_req.actual) || req->usb_req.zero)
699 is_last = 0;
700 else
701 is_last = 1;
702 }
703 dev_dbg(udc->dev, "%s: wrote %s %d bytes%s%s %d left %p\n",
704 __func__, ep->ep_usb.name, length, is_last ? "/L" : "",
705 is_short ? "/S" : "",
706 req->usb_req.length - req->usb_req.actual, req);
707 /* completion */
708 if (is_last) {
709 xudc_done(ep, req, 0);
710 retval = 0;
711 }
712 break;
713 case -EAGAIN:
714 dev_dbg(udc->dev, "Send busy\n");
715 break;
716 case -EINVAL:
717 case -ETIMEDOUT:
718 /* DMA error, dequeue the request */
719 xudc_done(ep, req, -ECONNRESET);
720 retval = 0;
721 break;
722 }
723
724 return retval;
725}
726
727/**
728 * xudc_nuke - Cleans up the data transfer message list.
729 * @ep: pointer to the usb device endpoint structure.
730 * @status: Status of the data transfer.
731 */
732static void xudc_nuke(struct xusb_ep *ep, int status)
733{
734 struct xusb_req *req;
735
736 while (!list_empty(&ep->queue)) {
737 req = list_first_entry(&ep->queue, struct xusb_req, queue);
738 xudc_done(ep, req, status);
739 }
740}
741
742/**
743 * xudc_ep_set_halt - Stalls/unstalls the given endpoint.
744 * @_ep: pointer to the usb device endpoint structure.
745 * @value: value to indicate stall/unstall.
746 *
747 * Return: 0 for success and error value on failure
748 */
749static int xudc_ep_set_halt(struct usb_ep *_ep, int value)
750{
751 struct xusb_ep *ep = to_xusb_ep(_ep);
752 struct xusb_udc *udc;
753 unsigned long flags;
754 u32 epcfgreg;
755
756 if (!_ep || (!ep->desc && ep->epnumber)) {
757 pr_debug("%s: bad ep or descriptor\n", __func__);
758 return -EINVAL;
759 }
760 udc = ep->udc;
761
762 if (ep->is_in && (!list_empty(&ep->queue)) && value) {
763 dev_dbg(udc->dev, "requests pending can't halt\n");
764 return -EAGAIN;
765 }
766
767 if (ep->buffer0ready || ep->buffer1ready) {
768 dev_dbg(udc->dev, "HW buffers busy can't halt\n");
769 return -EAGAIN;
770 }
771
772 spin_lock_irqsave(&udc->lock, flags);
773
774 if (value) {
775 /* Stall the device.*/
776 epcfgreg = udc->read_fn(udc->addr + ep->offset);
777 epcfgreg |= XUSB_EP_CFG_STALL_MASK;
778 udc->write_fn(udc->addr, ep->offset, epcfgreg);
779 } else {
780 /* Unstall the device.*/
781 epcfgreg = udc->read_fn(udc->addr + ep->offset);
782 epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
783 udc->write_fn(udc->addr, ep->offset, epcfgreg);
784 if (ep->epnumber) {
785 /* Reset the toggle bit.*/
786 epcfgreg = udc->read_fn(ep->udc->addr + ep->offset);
787 epcfgreg &= ~XUSB_EP_CFG_DATA_TOGGLE_MASK;
788 udc->write_fn(udc->addr, ep->offset, epcfgreg);
789 }
790 }
791
792 spin_unlock_irqrestore(&udc->lock, flags);
793 return 0;
794}
795
796/**
Lee Jones61a140f2021-05-26 14:00:32 +0100797 * __xudc_ep_enable - Enables the given endpoint.
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530798 * @ep: pointer to the xusb endpoint structure.
799 * @desc: pointer to usb endpoint descriptor.
800 *
801 * Return: 0 for success and error value on failure
802 */
803static int __xudc_ep_enable(struct xusb_ep *ep,
804 const struct usb_endpoint_descriptor *desc)
805{
806 struct xusb_udc *udc = ep->udc;
807 u32 tmp;
808 u32 epcfg;
809 u32 ier;
810 u16 maxpacket;
811
812 ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
813 /* Bit 3...0:endpoint number */
814 ep->epnumber = (desc->bEndpointAddress & 0x0f);
815 ep->desc = desc;
816 ep->ep_usb.desc = desc;
817 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
818 ep->ep_usb.maxpacket = maxpacket = le16_to_cpu(desc->wMaxPacketSize);
819
820 switch (tmp) {
821 case USB_ENDPOINT_XFER_CONTROL:
822 dev_dbg(udc->dev, "only one control endpoint\n");
823 /* NON- ISO */
824 ep->is_iso = 0;
825 return -EINVAL;
826 case USB_ENDPOINT_XFER_INT:
827 /* NON- ISO */
828 ep->is_iso = 0;
829 if (maxpacket > 64) {
830 dev_dbg(udc->dev, "bogus maxpacket %d\n", maxpacket);
831 return -EINVAL;
832 }
833 break;
834 case USB_ENDPOINT_XFER_BULK:
835 /* NON- ISO */
836 ep->is_iso = 0;
837 if (!(is_power_of_2(maxpacket) && maxpacket >= 8 &&
838 maxpacket <= 512)) {
839 dev_dbg(udc->dev, "bogus maxpacket %d\n", maxpacket);
840 return -EINVAL;
841 }
842 break;
843 case USB_ENDPOINT_XFER_ISOC:
844 /* ISO */
845 ep->is_iso = 1;
846 break;
847 }
848
Jiapeng Chongfe6ad682021-02-07 14:18:16 +0800849 ep->buffer0ready = false;
850 ep->buffer1ready = false;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530851 ep->curbufnum = 0;
852 ep->rambase = rambase[ep->epnumber];
853 xudc_epconfig(ep, udc);
854
855 dev_dbg(udc->dev, "Enable Endpoint %d max pkt is %d\n",
856 ep->epnumber, maxpacket);
857
858 /* Enable the End point.*/
859 epcfg = udc->read_fn(udc->addr + ep->offset);
860 epcfg |= XUSB_EP_CFG_VALID_MASK;
861 udc->write_fn(udc->addr, ep->offset, epcfg);
862 if (ep->epnumber)
863 ep->rambase <<= 2;
864
865 /* Enable buffer completion interrupts for endpoint */
866 ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
867 ier |= (XUSB_STATUS_INTR_BUFF_COMP_SHIFT_MASK << ep->epnumber);
868 udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
869
870 /* for OUT endpoint set buffers ready to receive */
871 if (ep->epnumber && !ep->is_in) {
872 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
873 1 << ep->epnumber);
Jiapeng Chongfe6ad682021-02-07 14:18:16 +0800874 ep->buffer0ready = true;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530875 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
876 (1 << (ep->epnumber +
877 XUSB_STATUS_EP_BUFF2_SHIFT)));
Jiapeng Chongfe6ad682021-02-07 14:18:16 +0800878 ep->buffer1ready = true;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530879 }
880
881 return 0;
882}
883
884/**
885 * xudc_ep_enable - Enables the given endpoint.
886 * @_ep: pointer to the usb endpoint structure.
887 * @desc: pointer to usb endpoint descriptor.
888 *
889 * Return: 0 for success and error value on failure
890 */
891static int xudc_ep_enable(struct usb_ep *_ep,
892 const struct usb_endpoint_descriptor *desc)
893{
894 struct xusb_ep *ep;
895 struct xusb_udc *udc;
896 unsigned long flags;
897 int ret;
898
899 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
900 pr_debug("%s: bad ep or descriptor\n", __func__);
901 return -EINVAL;
902 }
903
904 ep = to_xusb_ep(_ep);
905 udc = ep->udc;
906
907 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
908 dev_dbg(udc->dev, "bogus device state\n");
909 return -ESHUTDOWN;
910 }
911
912 spin_lock_irqsave(&udc->lock, flags);
913 ret = __xudc_ep_enable(ep, desc);
914 spin_unlock_irqrestore(&udc->lock, flags);
915
916 return ret;
917}
918
919/**
920 * xudc_ep_disable - Disables the given endpoint.
921 * @_ep: pointer to the usb endpoint structure.
922 *
923 * Return: 0 for success and error value on failure
924 */
925static int xudc_ep_disable(struct usb_ep *_ep)
926{
927 struct xusb_ep *ep;
928 unsigned long flags;
929 u32 epcfg;
930 struct xusb_udc *udc;
931
932 if (!_ep) {
933 pr_debug("%s: invalid ep\n", __func__);
934 return -EINVAL;
935 }
936
937 ep = to_xusb_ep(_ep);
938 udc = ep->udc;
939
940 spin_lock_irqsave(&udc->lock, flags);
941
942 xudc_nuke(ep, -ESHUTDOWN);
943
944 /* Restore the endpoint's pristine config */
945 ep->desc = NULL;
946 ep->ep_usb.desc = NULL;
947
948 dev_dbg(udc->dev, "USB Ep %d disable\n ", ep->epnumber);
949 /* Disable the endpoint.*/
950 epcfg = udc->read_fn(udc->addr + ep->offset);
951 epcfg &= ~XUSB_EP_CFG_VALID_MASK;
952 udc->write_fn(udc->addr, ep->offset, epcfg);
953
954 spin_unlock_irqrestore(&udc->lock, flags);
955 return 0;
956}
957
958/**
959 * xudc_ep_alloc_request - Initializes the request queue.
960 * @_ep: pointer to the usb endpoint structure.
961 * @gfp_flags: Flags related to the request call.
962 *
963 * Return: pointer to request structure on success and a NULL on failure.
964 */
965static struct usb_request *xudc_ep_alloc_request(struct usb_ep *_ep,
966 gfp_t gfp_flags)
967{
968 struct xusb_ep *ep = to_xusb_ep(_ep);
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530969 struct xusb_req *req;
970
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530971 req = kzalloc(sizeof(*req), gfp_flags);
Wolfram Sangc86af712016-08-25 19:39:05 +0200972 if (!req)
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530973 return NULL;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530974
975 req->ep = ep;
976 INIT_LIST_HEAD(&req->queue);
977 return &req->usb_req;
978}
979
980/**
981 * xudc_free_request - Releases the request from queue.
982 * @_ep: pointer to the usb device endpoint structure.
983 * @_req: pointer to the usb request structure.
984 */
985static void xudc_free_request(struct usb_ep *_ep, struct usb_request *_req)
986{
987 struct xusb_req *req = to_xusb_req(_req);
988
989 kfree(req);
990}
991
992/**
Lee Jones61a140f2021-05-26 14:00:32 +0100993 * __xudc_ep0_queue - Adds the request to endpoint 0 queue.
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +0530994 * @ep0: pointer to the xusb endpoint 0 structure.
995 * @req: pointer to the xusb request structure.
996 *
997 * Return: 0 for success and error value on failure
998 */
999static int __xudc_ep0_queue(struct xusb_ep *ep0, struct xusb_req *req)
1000{
1001 struct xusb_udc *udc = ep0->udc;
1002 u32 length;
1003 u8 *corebuf;
1004
1005 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
1006 dev_dbg(udc->dev, "%s, bogus device state\n", __func__);
1007 return -EINVAL;
1008 }
1009 if (!list_empty(&ep0->queue)) {
1010 dev_dbg(udc->dev, "%s:ep0 busy\n", __func__);
1011 return -EBUSY;
1012 }
1013
1014 req->usb_req.status = -EINPROGRESS;
1015 req->usb_req.actual = 0;
1016
1017 list_add_tail(&req->queue, &ep0->queue);
1018
1019 if (udc->setup.bRequestType & USB_DIR_IN) {
1020 prefetch(req->usb_req.buf);
1021 length = req->usb_req.length;
1022 corebuf = (void __force *) ((ep0->rambase << 2) +
1023 udc->addr);
1024 length = req->usb_req.actual = min_t(u32, length,
1025 EP0_MAX_PACKET);
1026 memcpy(corebuf, req->usb_req.buf, length);
1027 udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, length);
1028 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
1029 } else {
1030 if (udc->setup.wLength) {
1031 /* Enable EP0 buffer to receive data */
1032 udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, 0);
1033 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
1034 } else {
1035 xudc_wrstatus(udc);
1036 }
1037 }
1038
1039 return 0;
1040}
1041
1042/**
1043 * xudc_ep0_queue - Adds the request to endpoint 0 queue.
1044 * @_ep: pointer to the usb endpoint 0 structure.
1045 * @_req: pointer to the usb request structure.
1046 * @gfp_flags: Flags related to the request call.
1047 *
1048 * Return: 0 for success and error value on failure
1049 */
1050static int xudc_ep0_queue(struct usb_ep *_ep, struct usb_request *_req,
1051 gfp_t gfp_flags)
1052{
1053 struct xusb_req *req = to_xusb_req(_req);
1054 struct xusb_ep *ep0 = to_xusb_ep(_ep);
1055 struct xusb_udc *udc = ep0->udc;
1056 unsigned long flags;
1057 int ret;
1058
1059 spin_lock_irqsave(&udc->lock, flags);
1060 ret = __xudc_ep0_queue(ep0, req);
1061 spin_unlock_irqrestore(&udc->lock, flags);
1062
1063 return ret;
1064}
1065
1066/**
1067 * xudc_ep_queue - Adds the request to endpoint queue.
1068 * @_ep: pointer to the usb endpoint structure.
1069 * @_req: pointer to the usb request structure.
1070 * @gfp_flags: Flags related to the request call.
1071 *
1072 * Return: 0 for success and error value on failure
1073 */
1074static int xudc_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1075 gfp_t gfp_flags)
1076{
1077 struct xusb_req *req = to_xusb_req(_req);
1078 struct xusb_ep *ep = to_xusb_ep(_ep);
1079 struct xusb_udc *udc = ep->udc;
1080 int ret;
1081 unsigned long flags;
1082
1083 if (!ep->desc) {
Colin Ian King3c168902018-09-29 12:43:13 +01001084 dev_dbg(udc->dev, "%s: queuing request to disabled %s\n",
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301085 __func__, ep->name);
1086 return -ESHUTDOWN;
1087 }
1088
1089 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
1090 dev_dbg(udc->dev, "%s, bogus device state\n", __func__);
1091 return -EINVAL;
1092 }
1093
1094 spin_lock_irqsave(&udc->lock, flags);
1095
1096 _req->status = -EINPROGRESS;
1097 _req->actual = 0;
1098
1099 if (udc->dma_enabled) {
1100 ret = usb_gadget_map_request(&udc->gadget, &req->usb_req,
1101 ep->is_in);
1102 if (ret) {
1103 dev_dbg(udc->dev, "gadget_map failed ep%d\n",
1104 ep->epnumber);
1105 spin_unlock_irqrestore(&udc->lock, flags);
1106 return -EAGAIN;
1107 }
1108 }
1109
1110 if (list_empty(&ep->queue)) {
1111 if (ep->is_in) {
1112 dev_dbg(udc->dev, "xudc_write_fifo from ep_queue\n");
1113 if (!xudc_write_fifo(ep, req))
1114 req = NULL;
1115 } else {
1116 dev_dbg(udc->dev, "xudc_read_fifo from ep_queue\n");
1117 if (!xudc_read_fifo(ep, req))
1118 req = NULL;
1119 }
1120 }
1121
1122 if (req != NULL)
1123 list_add_tail(&req->queue, &ep->queue);
1124
1125 spin_unlock_irqrestore(&udc->lock, flags);
1126 return 0;
1127}
1128
1129/**
1130 * xudc_ep_dequeue - Removes the request from the queue.
1131 * @_ep: pointer to the usb device endpoint structure.
1132 * @_req: pointer to the usb request structure.
1133 *
1134 * Return: 0 for success and error value on failure
1135 */
1136static int xudc_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1137{
1138 struct xusb_ep *ep = to_xusb_ep(_ep);
1139 struct xusb_req *req = to_xusb_req(_req);
1140 struct xusb_udc *udc = ep->udc;
1141 unsigned long flags;
1142
1143 spin_lock_irqsave(&udc->lock, flags);
1144 /* Make sure it's actually queued on this endpoint */
1145 list_for_each_entry(req, &ep->queue, queue) {
1146 if (&req->usb_req == _req)
1147 break;
1148 }
1149 if (&req->usb_req != _req) {
Dan Carpenter0df6d8d2017-04-27 12:11:18 +03001150 spin_unlock_irqrestore(&udc->lock, flags);
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301151 return -EINVAL;
1152 }
1153 xudc_done(ep, req, -ECONNRESET);
1154 spin_unlock_irqrestore(&udc->lock, flags);
1155
1156 return 0;
1157}
1158
1159/**
1160 * xudc_ep0_enable - Enables the given endpoint.
1161 * @ep: pointer to the usb endpoint structure.
1162 * @desc: pointer to usb endpoint descriptor.
1163 *
1164 * Return: error always.
1165 *
1166 * endpoint 0 enable should not be called by gadget layer.
1167 */
1168static int xudc_ep0_enable(struct usb_ep *ep,
1169 const struct usb_endpoint_descriptor *desc)
1170{
1171 return -EINVAL;
1172}
1173
1174/**
1175 * xudc_ep0_disable - Disables the given endpoint.
1176 * @ep: pointer to the usb endpoint structure.
1177 *
1178 * Return: error always.
1179 *
1180 * endpoint 0 disable should not be called by gadget layer.
1181 */
1182static int xudc_ep0_disable(struct usb_ep *ep)
1183{
1184 return -EINVAL;
1185}
1186
1187static const struct usb_ep_ops xusb_ep0_ops = {
1188 .enable = xudc_ep0_enable,
1189 .disable = xudc_ep0_disable,
1190 .alloc_request = xudc_ep_alloc_request,
1191 .free_request = xudc_free_request,
1192 .queue = xudc_ep0_queue,
1193 .dequeue = xudc_ep_dequeue,
1194 .set_halt = xudc_ep_set_halt,
1195};
1196
1197static const struct usb_ep_ops xusb_ep_ops = {
1198 .enable = xudc_ep_enable,
1199 .disable = xudc_ep_disable,
1200 .alloc_request = xudc_ep_alloc_request,
1201 .free_request = xudc_free_request,
1202 .queue = xudc_ep_queue,
1203 .dequeue = xudc_ep_dequeue,
1204 .set_halt = xudc_ep_set_halt,
1205};
1206
1207/**
1208 * xudc_get_frame - Reads the current usb frame number.
1209 * @gadget: pointer to the usb gadget structure.
1210 *
1211 * Return: current frame number for success and error value on failure.
1212 */
1213static int xudc_get_frame(struct usb_gadget *gadget)
1214{
1215 struct xusb_udc *udc;
1216 int frame;
1217
1218 if (!gadget)
1219 return -ENODEV;
1220
1221 udc = to_udc(gadget);
1222 frame = udc->read_fn(udc->addr + XUSB_FRAMENUM_OFFSET);
1223 return frame;
1224}
1225
1226/**
1227 * xudc_wakeup - Send remote wakeup signal to host
1228 * @gadget: pointer to the usb gadget structure.
1229 *
1230 * Return: 0 on success and error on failure
1231 */
1232static int xudc_wakeup(struct usb_gadget *gadget)
1233{
1234 struct xusb_udc *udc = to_udc(gadget);
1235 u32 crtlreg;
1236 int status = -EINVAL;
1237 unsigned long flags;
1238
1239 spin_lock_irqsave(&udc->lock, flags);
1240
1241 /* Remote wake up not enabled by host */
1242 if (!udc->remote_wkp)
1243 goto done;
1244
1245 crtlreg = udc->read_fn(udc->addr + XUSB_CONTROL_OFFSET);
1246 crtlreg |= XUSB_CONTROL_USB_RMTWAKE_MASK;
1247 /* set remote wake up bit */
1248 udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
1249 /*
1250 * wait for a while and reset remote wake up bit since this bit
1251 * is not cleared by HW after sending remote wakeup to host.
1252 */
1253 mdelay(2);
1254
1255 crtlreg &= ~XUSB_CONTROL_USB_RMTWAKE_MASK;
1256 udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
1257 status = 0;
1258done:
1259 spin_unlock_irqrestore(&udc->lock, flags);
1260 return status;
1261}
1262
1263/**
1264 * xudc_pullup - start/stop USB traffic
1265 * @gadget: pointer to the usb gadget structure.
1266 * @is_on: flag to start or stop
1267 *
1268 * Return: 0 always
1269 *
1270 * This function starts/stops SIE engine of IP based on is_on.
1271 */
1272static int xudc_pullup(struct usb_gadget *gadget, int is_on)
1273{
1274 struct xusb_udc *udc = to_udc(gadget);
1275 unsigned long flags;
1276 u32 crtlreg;
1277
1278 spin_lock_irqsave(&udc->lock, flags);
1279
1280 crtlreg = udc->read_fn(udc->addr + XUSB_CONTROL_OFFSET);
1281 if (is_on)
1282 crtlreg |= XUSB_CONTROL_USB_READY_MASK;
1283 else
1284 crtlreg &= ~XUSB_CONTROL_USB_READY_MASK;
1285
1286 udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
1287
1288 spin_unlock_irqrestore(&udc->lock, flags);
1289
1290 return 0;
1291}
1292
1293/**
1294 * xudc_eps_init - initialize endpoints.
1295 * @udc: pointer to the usb device controller structure.
1296 */
1297static void xudc_eps_init(struct xusb_udc *udc)
1298{
1299 u32 ep_number;
1300
1301 INIT_LIST_HEAD(&udc->gadget.ep_list);
1302
1303 for (ep_number = 0; ep_number < XUSB_MAX_ENDPOINTS; ep_number++) {
1304 struct xusb_ep *ep = &udc->ep[ep_number];
1305
1306 if (ep_number) {
1307 list_add_tail(&ep->ep_usb.ep_list,
1308 &udc->gadget.ep_list);
1309 usb_ep_set_maxpacket_limit(&ep->ep_usb,
1310 (unsigned short) ~0);
1311 snprintf(ep->name, EPNAME_SIZE, "ep%d", ep_number);
1312 ep->ep_usb.name = ep->name;
1313 ep->ep_usb.ops = &xusb_ep_ops;
Robert Baldyga927d9f72015-07-31 16:00:44 +02001314
1315 ep->ep_usb.caps.type_iso = true;
1316 ep->ep_usb.caps.type_bulk = true;
1317 ep->ep_usb.caps.type_int = true;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301318 } else {
1319 ep->ep_usb.name = ep0name;
1320 usb_ep_set_maxpacket_limit(&ep->ep_usb, EP0_MAX_PACKET);
1321 ep->ep_usb.ops = &xusb_ep0_ops;
Robert Baldyga927d9f72015-07-31 16:00:44 +02001322
1323 ep->ep_usb.caps.type_control = true;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301324 }
1325
Robert Baldyga927d9f72015-07-31 16:00:44 +02001326 ep->ep_usb.caps.dir_in = true;
1327 ep->ep_usb.caps.dir_out = true;
1328
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301329 ep->udc = udc;
1330 ep->epnumber = ep_number;
1331 ep->desc = NULL;
1332 /*
1333 * The configuration register address offset between
1334 * each endpoint is 0x10.
1335 */
1336 ep->offset = XUSB_EP0_CONFIG_OFFSET + (ep_number * 0x10);
1337 ep->is_in = 0;
1338 ep->is_iso = 0;
1339 ep->maxpacket = 0;
1340 xudc_epconfig(ep, udc);
1341
1342 /* Initialize one queue per endpoint */
1343 INIT_LIST_HEAD(&ep->queue);
1344 }
1345}
1346
1347/**
1348 * xudc_stop_activity - Stops any further activity on the device.
1349 * @udc: pointer to the usb device controller structure.
1350 */
1351static void xudc_stop_activity(struct xusb_udc *udc)
1352{
1353 int i;
1354 struct xusb_ep *ep;
1355
1356 for (i = 0; i < XUSB_MAX_ENDPOINTS; i++) {
1357 ep = &udc->ep[i];
1358 xudc_nuke(ep, -ESHUTDOWN);
1359 }
1360}
1361
1362/**
1363 * xudc_start - Starts the device.
1364 * @gadget: pointer to the usb gadget structure
1365 * @driver: pointer to gadget driver structure
1366 *
1367 * Return: zero on success and error on failure
1368 */
1369static int xudc_start(struct usb_gadget *gadget,
1370 struct usb_gadget_driver *driver)
1371{
1372 struct xusb_udc *udc = to_udc(gadget);
1373 struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
1374 const struct usb_endpoint_descriptor *desc = &config_bulk_out_desc;
1375 unsigned long flags;
1376 int ret = 0;
1377
1378 spin_lock_irqsave(&udc->lock, flags);
1379
1380 if (udc->driver) {
1381 dev_err(udc->dev, "%s is already bound to %s\n",
1382 udc->gadget.name, udc->driver->driver.name);
1383 ret = -EBUSY;
1384 goto err;
1385 }
1386
1387 /* hook up the driver */
1388 udc->driver = driver;
1389 udc->gadget.speed = driver->max_speed;
1390
1391 /* Enable the control endpoint. */
1392 ret = __xudc_ep_enable(ep0, desc);
1393
1394 /* Set device address and remote wakeup to 0 */
1395 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
1396 udc->remote_wkp = 0;
1397err:
1398 spin_unlock_irqrestore(&udc->lock, flags);
1399 return ret;
1400}
1401
1402/**
1403 * xudc_stop - stops the device.
1404 * @gadget: pointer to the usb gadget structure
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301405 *
1406 * Return: zero always
1407 */
Felipe Balbi22835b82014-10-17 12:05:12 -05001408static int xudc_stop(struct usb_gadget *gadget)
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301409{
1410 struct xusb_udc *udc = to_udc(gadget);
1411 unsigned long flags;
1412
1413 spin_lock_irqsave(&udc->lock, flags);
1414
1415 udc->gadget.speed = USB_SPEED_UNKNOWN;
1416 udc->driver = NULL;
1417
1418 /* Set device address and remote wakeup to 0 */
1419 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
1420 udc->remote_wkp = 0;
1421
1422 xudc_stop_activity(udc);
1423
1424 spin_unlock_irqrestore(&udc->lock, flags);
1425
1426 return 0;
1427}
1428
1429static const struct usb_gadget_ops xusb_udc_ops = {
1430 .get_frame = xudc_get_frame,
1431 .wakeup = xudc_wakeup,
1432 .pullup = xudc_pullup,
1433 .udc_start = xudc_start,
1434 .udc_stop = xudc_stop,
1435};
1436
1437/**
1438 * xudc_clear_stall_all_ep - clears stall of every endpoint.
1439 * @udc: pointer to the udc structure.
1440 */
1441static void xudc_clear_stall_all_ep(struct xusb_udc *udc)
1442{
1443 struct xusb_ep *ep;
1444 u32 epcfgreg;
1445 int i;
1446
1447 for (i = 0; i < XUSB_MAX_ENDPOINTS; i++) {
1448 ep = &udc->ep[i];
1449 epcfgreg = udc->read_fn(udc->addr + ep->offset);
1450 epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
1451 udc->write_fn(udc->addr, ep->offset, epcfgreg);
1452 if (ep->epnumber) {
1453 /* Reset the toggle bit.*/
1454 epcfgreg = udc->read_fn(udc->addr + ep->offset);
1455 epcfgreg &= ~XUSB_EP_CFG_DATA_TOGGLE_MASK;
1456 udc->write_fn(udc->addr, ep->offset, epcfgreg);
1457 }
1458 }
1459}
1460
1461/**
1462 * xudc_startup_handler - The usb device controller interrupt handler.
1463 * @udc: pointer to the udc structure.
1464 * @intrstatus: The mask value containing the interrupt sources.
1465 *
1466 * This function handles the RESET,SUSPEND,RESUME and DISCONNECT interrupts.
1467 */
1468static void xudc_startup_handler(struct xusb_udc *udc, u32 intrstatus)
1469{
1470 u32 intrreg;
1471
1472 if (intrstatus & XUSB_STATUS_RESET_MASK) {
1473
1474 dev_dbg(udc->dev, "Reset\n");
1475
1476 if (intrstatus & XUSB_STATUS_HIGH_SPEED_MASK)
1477 udc->gadget.speed = USB_SPEED_HIGH;
1478 else
1479 udc->gadget.speed = USB_SPEED_FULL;
1480
1481 xudc_stop_activity(udc);
1482 xudc_clear_stall_all_ep(udc);
1483 udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, 0);
1484
1485 /* Set device address and remote wakeup to 0 */
1486 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
1487 udc->remote_wkp = 0;
1488
1489 /* Enable the suspend, resume and disconnect */
1490 intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1491 intrreg |= XUSB_STATUS_SUSPEND_MASK | XUSB_STATUS_RESUME_MASK |
1492 XUSB_STATUS_DISCONNECT_MASK;
1493 udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
1494 }
1495 if (intrstatus & XUSB_STATUS_SUSPEND_MASK) {
1496
1497 dev_dbg(udc->dev, "Suspend\n");
1498
1499 /* Enable the reset, resume and disconnect */
1500 intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1501 intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_RESUME_MASK |
1502 XUSB_STATUS_DISCONNECT_MASK;
1503 udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
1504
1505 udc->usb_state = USB_STATE_SUSPENDED;
1506
1507 if (udc->driver->suspend) {
1508 spin_unlock(&udc->lock);
1509 udc->driver->suspend(&udc->gadget);
1510 spin_lock(&udc->lock);
1511 }
1512 }
1513 if (intrstatus & XUSB_STATUS_RESUME_MASK) {
1514 bool condition = (udc->usb_state != USB_STATE_SUSPENDED);
1515
1516 dev_WARN_ONCE(udc->dev, condition,
1517 "Resume IRQ while not suspended\n");
1518
1519 dev_dbg(udc->dev, "Resume\n");
1520
1521 /* Enable the reset, suspend and disconnect */
1522 intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1523 intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_SUSPEND_MASK |
1524 XUSB_STATUS_DISCONNECT_MASK;
1525 udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
1526
1527 udc->usb_state = 0;
1528
1529 if (udc->driver->resume) {
1530 spin_unlock(&udc->lock);
1531 udc->driver->resume(&udc->gadget);
1532 spin_lock(&udc->lock);
1533 }
1534 }
1535 if (intrstatus & XUSB_STATUS_DISCONNECT_MASK) {
1536
1537 dev_dbg(udc->dev, "Disconnect\n");
1538
1539 /* Enable the reset, resume and suspend */
1540 intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1541 intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_RESUME_MASK |
1542 XUSB_STATUS_SUSPEND_MASK;
1543 udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
1544
1545 if (udc->driver && udc->driver->disconnect) {
1546 spin_unlock(&udc->lock);
1547 udc->driver->disconnect(&udc->gadget);
1548 spin_lock(&udc->lock);
1549 }
1550 }
1551}
1552
1553/**
1554 * xudc_ep0_stall - Stall endpoint zero.
1555 * @udc: pointer to the udc structure.
1556 *
1557 * This function stalls endpoint zero.
1558 */
1559static void xudc_ep0_stall(struct xusb_udc *udc)
1560{
1561 u32 epcfgreg;
1562 struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
1563
1564 epcfgreg = udc->read_fn(udc->addr + ep0->offset);
1565 epcfgreg |= XUSB_EP_CFG_STALL_MASK;
1566 udc->write_fn(udc->addr, ep0->offset, epcfgreg);
1567}
1568
1569/**
1570 * xudc_setaddress - executes SET_ADDRESS command
1571 * @udc: pointer to the udc structure.
1572 *
1573 * This function executes USB SET_ADDRESS command
1574 */
1575static void xudc_setaddress(struct xusb_udc *udc)
1576{
1577 struct xusb_ep *ep0 = &udc->ep[0];
1578 struct xusb_req *req = udc->req;
1579 int ret;
1580
1581 req->usb_req.length = 0;
1582 ret = __xudc_ep0_queue(ep0, req);
1583 if (ret == 0)
1584 return;
1585
1586 dev_err(udc->dev, "Can't respond to SET ADDRESS request\n");
1587 xudc_ep0_stall(udc);
1588}
1589
1590/**
1591 * xudc_getstatus - executes GET_STATUS command
1592 * @udc: pointer to the udc structure.
1593 *
1594 * This function executes USB GET_STATUS command
1595 */
1596static void xudc_getstatus(struct xusb_udc *udc)
1597{
1598 struct xusb_ep *ep0 = &udc->ep[0];
1599 struct xusb_req *req = udc->req;
1600 struct xusb_ep *target_ep;
1601 u16 status = 0;
1602 u32 epcfgreg;
1603 int epnum;
1604 u32 halt;
1605 int ret;
1606
1607 switch (udc->setup.bRequestType & USB_RECIP_MASK) {
1608 case USB_RECIP_DEVICE:
1609 /* Get device status */
1610 status = 1 << USB_DEVICE_SELF_POWERED;
1611 if (udc->remote_wkp)
1612 status |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1613 break;
1614 case USB_RECIP_INTERFACE:
1615 break;
1616 case USB_RECIP_ENDPOINT:
1617 epnum = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
1618 target_ep = &udc->ep[epnum];
1619 epcfgreg = udc->read_fn(udc->addr + target_ep->offset);
1620 halt = epcfgreg & XUSB_EP_CFG_STALL_MASK;
1621 if (udc->setup.wIndex & USB_DIR_IN) {
1622 if (!target_ep->is_in)
1623 goto stall;
1624 } else {
1625 if (target_ep->is_in)
1626 goto stall;
1627 }
1628 if (halt)
1629 status = 1 << USB_ENDPOINT_HALT;
1630 break;
1631 default:
1632 goto stall;
1633 }
1634
1635 req->usb_req.length = 2;
1636 *(u16 *)req->usb_req.buf = cpu_to_le16(status);
1637 ret = __xudc_ep0_queue(ep0, req);
1638 if (ret == 0)
1639 return;
1640stall:
1641 dev_err(udc->dev, "Can't respond to getstatus request\n");
1642 xudc_ep0_stall(udc);
1643}
1644
1645/**
1646 * xudc_set_clear_feature - Executes the set feature and clear feature commands.
1647 * @udc: pointer to the usb device controller structure.
1648 *
1649 * Processes the SET_FEATURE and CLEAR_FEATURE commands.
1650 */
1651static void xudc_set_clear_feature(struct xusb_udc *udc)
1652{
1653 struct xusb_ep *ep0 = &udc->ep[0];
1654 struct xusb_req *req = udc->req;
1655 struct xusb_ep *target_ep;
1656 u8 endpoint;
1657 u8 outinbit;
1658 u32 epcfgreg;
1659 int flag = (udc->setup.bRequest == USB_REQ_SET_FEATURE ? 1 : 0);
1660 int ret;
1661
1662 switch (udc->setup.bRequestType) {
1663 case USB_RECIP_DEVICE:
1664 switch (udc->setup.wValue) {
1665 case USB_DEVICE_TEST_MODE:
1666 /*
1667 * The Test Mode will be executed
1668 * after the status phase.
1669 */
1670 break;
1671 case USB_DEVICE_REMOTE_WAKEUP:
1672 if (flag)
1673 udc->remote_wkp = 1;
1674 else
1675 udc->remote_wkp = 0;
1676 break;
1677 default:
1678 xudc_ep0_stall(udc);
1679 break;
1680 }
1681 break;
1682 case USB_RECIP_ENDPOINT:
1683 if (!udc->setup.wValue) {
1684 endpoint = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
1685 target_ep = &udc->ep[endpoint];
1686 outinbit = udc->setup.wIndex & USB_ENDPOINT_DIR_MASK;
1687 outinbit = outinbit >> 7;
1688
1689 /* Make sure direction matches.*/
1690 if (outinbit != target_ep->is_in) {
1691 xudc_ep0_stall(udc);
1692 return;
1693 }
1694 epcfgreg = udc->read_fn(udc->addr + target_ep->offset);
1695 if (!endpoint) {
1696 /* Clear the stall.*/
1697 epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
1698 udc->write_fn(udc->addr,
1699 target_ep->offset, epcfgreg);
1700 } else {
1701 if (flag) {
1702 epcfgreg |= XUSB_EP_CFG_STALL_MASK;
1703 udc->write_fn(udc->addr,
1704 target_ep->offset,
1705 epcfgreg);
1706 } else {
1707 /* Unstall the endpoint.*/
1708 epcfgreg &= ~(XUSB_EP_CFG_STALL_MASK |
1709 XUSB_EP_CFG_DATA_TOGGLE_MASK);
1710 udc->write_fn(udc->addr,
1711 target_ep->offset,
1712 epcfgreg);
1713 }
1714 }
1715 }
1716 break;
1717 default:
1718 xudc_ep0_stall(udc);
1719 return;
1720 }
1721
1722 req->usb_req.length = 0;
1723 ret = __xudc_ep0_queue(ep0, req);
1724 if (ret == 0)
1725 return;
1726
1727 dev_err(udc->dev, "Can't respond to SET/CLEAR FEATURE\n");
1728 xudc_ep0_stall(udc);
1729}
1730
1731/**
1732 * xudc_handle_setup - Processes the setup packet.
1733 * @udc: pointer to the usb device controller structure.
1734 *
1735 * Process setup packet and delegate to gadget layer.
1736 */
1737static void xudc_handle_setup(struct xusb_udc *udc)
Jules Irenge66bd76e2020-04-29 11:05:23 +01001738 __must_hold(&udc->lock)
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301739{
1740 struct xusb_ep *ep0 = &udc->ep[0];
1741 struct usb_ctrlrequest setup;
1742 u32 *ep0rambase;
1743
1744 /* Load up the chapter 9 command buffer.*/
1745 ep0rambase = (u32 __force *) (udc->addr + XUSB_SETUP_PKT_ADDR_OFFSET);
1746 memcpy(&setup, ep0rambase, 8);
1747
1748 udc->setup = setup;
1749 udc->setup.wValue = cpu_to_le16(setup.wValue);
1750 udc->setup.wIndex = cpu_to_le16(setup.wIndex);
1751 udc->setup.wLength = cpu_to_le16(setup.wLength);
1752
1753 /* Clear previous requests */
1754 xudc_nuke(ep0, -ECONNRESET);
1755
1756 if (udc->setup.bRequestType & USB_DIR_IN) {
1757 /* Execute the get command.*/
1758 udc->setupseqrx = STATUS_PHASE;
1759 udc->setupseqtx = DATA_PHASE;
1760 } else {
1761 /* Execute the put command.*/
1762 udc->setupseqrx = DATA_PHASE;
1763 udc->setupseqtx = STATUS_PHASE;
1764 }
1765
1766 switch (udc->setup.bRequest) {
1767 case USB_REQ_GET_STATUS:
1768 /* Data+Status phase form udc */
1769 if ((udc->setup.bRequestType &
1770 (USB_DIR_IN | USB_TYPE_MASK)) !=
1771 (USB_DIR_IN | USB_TYPE_STANDARD))
1772 break;
1773 xudc_getstatus(udc);
1774 return;
1775 case USB_REQ_SET_ADDRESS:
1776 /* Status phase from udc */
1777 if (udc->setup.bRequestType != (USB_DIR_OUT |
1778 USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1779 break;
1780 xudc_setaddress(udc);
1781 return;
1782 case USB_REQ_CLEAR_FEATURE:
1783 case USB_REQ_SET_FEATURE:
1784 /* Requests with no data phase, status phase from udc */
1785 if ((udc->setup.bRequestType & USB_TYPE_MASK)
1786 != USB_TYPE_STANDARD)
1787 break;
1788 xudc_set_clear_feature(udc);
1789 return;
1790 default:
1791 break;
1792 }
1793
1794 spin_unlock(&udc->lock);
1795 if (udc->driver->setup(&udc->gadget, &setup) < 0)
1796 xudc_ep0_stall(udc);
1797 spin_lock(&udc->lock);
1798}
1799
1800/**
1801 * xudc_ep0_out - Processes the endpoint 0 OUT token.
1802 * @udc: pointer to the usb device controller structure.
1803 */
1804static void xudc_ep0_out(struct xusb_udc *udc)
1805{
1806 struct xusb_ep *ep0 = &udc->ep[0];
1807 struct xusb_req *req;
1808 u8 *ep0rambase;
1809 unsigned int bytes_to_rx;
1810 void *buffer;
1811
1812 req = list_first_entry(&ep0->queue, struct xusb_req, queue);
1813
1814 switch (udc->setupseqrx) {
1815 case STATUS_PHASE:
1816 /*
1817 * This resets both state machines for the next
1818 * Setup packet.
1819 */
1820 udc->setupseqrx = SETUP_PHASE;
1821 udc->setupseqtx = SETUP_PHASE;
1822 req->usb_req.actual = req->usb_req.length;
1823 xudc_done(ep0, req, 0);
1824 break;
1825 case DATA_PHASE:
1826 bytes_to_rx = udc->read_fn(udc->addr +
1827 XUSB_EP_BUF0COUNT_OFFSET);
1828 /* Copy the data to be received from the DPRAM. */
1829 ep0rambase = (u8 __force *) (udc->addr +
1830 (ep0->rambase << 2));
1831 buffer = req->usb_req.buf + req->usb_req.actual;
1832 req->usb_req.actual = req->usb_req.actual + bytes_to_rx;
1833 memcpy(buffer, ep0rambase, bytes_to_rx);
1834
1835 if (req->usb_req.length == req->usb_req.actual) {
1836 /* Data transfer completed get ready for Status stage */
1837 xudc_wrstatus(udc);
1838 } else {
1839 /* Enable EP0 buffer to receive data */
1840 udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, 0);
1841 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
1842 }
1843 break;
1844 default:
1845 break;
1846 }
1847}
1848
1849/**
1850 * xudc_ep0_in - Processes the endpoint 0 IN token.
1851 * @udc: pointer to the usb device controller structure.
1852 */
1853static void xudc_ep0_in(struct xusb_udc *udc)
1854{
1855 struct xusb_ep *ep0 = &udc->ep[0];
1856 struct xusb_req *req;
1857 unsigned int bytes_to_tx;
1858 void *buffer;
1859 u32 epcfgreg;
1860 u16 count = 0;
1861 u16 length;
1862 u8 *ep0rambase;
1863 u8 test_mode = udc->setup.wIndex >> 8;
1864
1865 req = list_first_entry(&ep0->queue, struct xusb_req, queue);
1866 bytes_to_tx = req->usb_req.length - req->usb_req.actual;
1867
1868 switch (udc->setupseqtx) {
1869 case STATUS_PHASE:
1870 switch (udc->setup.bRequest) {
1871 case USB_REQ_SET_ADDRESS:
1872 /* Set the address of the device.*/
1873 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET,
1874 udc->setup.wValue);
1875 break;
1876 case USB_REQ_SET_FEATURE:
1877 if (udc->setup.bRequestType ==
1878 USB_RECIP_DEVICE) {
1879 if (udc->setup.wValue ==
1880 USB_DEVICE_TEST_MODE)
1881 udc->write_fn(udc->addr,
1882 XUSB_TESTMODE_OFFSET,
1883 test_mode);
1884 }
1885 break;
1886 }
1887 req->usb_req.actual = req->usb_req.length;
1888 xudc_done(ep0, req, 0);
1889 break;
1890 case DATA_PHASE:
1891 if (!bytes_to_tx) {
1892 /*
1893 * We're done with data transfer, next
1894 * will be zero length OUT with data toggle of
1895 * 1. Setup data_toggle.
1896 */
1897 epcfgreg = udc->read_fn(udc->addr + ep0->offset);
1898 epcfgreg |= XUSB_EP_CFG_DATA_TOGGLE_MASK;
1899 udc->write_fn(udc->addr, ep0->offset, epcfgreg);
1900 udc->setupseqtx = STATUS_PHASE;
1901 } else {
1902 length = count = min_t(u32, bytes_to_tx,
1903 EP0_MAX_PACKET);
1904 /* Copy the data to be transmitted into the DPRAM. */
1905 ep0rambase = (u8 __force *) (udc->addr +
1906 (ep0->rambase << 2));
1907 buffer = req->usb_req.buf + req->usb_req.actual;
1908 req->usb_req.actual = req->usb_req.actual + length;
1909 memcpy(ep0rambase, buffer, length);
1910 }
1911 udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, count);
1912 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
1913 break;
1914 default:
1915 break;
1916 }
1917}
1918
1919/**
1920 * xudc_ctrl_ep_handler - Endpoint 0 interrupt handler.
1921 * @udc: pointer to the udc structure.
1922 * @intrstatus: It's the mask value for the interrupt sources on endpoint 0.
1923 *
1924 * Processes the commands received during enumeration phase.
1925 */
1926static void xudc_ctrl_ep_handler(struct xusb_udc *udc, u32 intrstatus)
1927{
1928
1929 if (intrstatus & XUSB_STATUS_SETUP_PACKET_MASK) {
1930 xudc_handle_setup(udc);
1931 } else {
1932 if (intrstatus & XUSB_STATUS_FIFO_BUFF_RDY_MASK)
1933 xudc_ep0_out(udc);
1934 else if (intrstatus & XUSB_STATUS_FIFO_BUFF_FREE_MASK)
1935 xudc_ep0_in(udc);
1936 }
1937}
1938
1939/**
1940 * xudc_nonctrl_ep_handler - Non control endpoint interrupt handler.
1941 * @udc: pointer to the udc structure.
1942 * @epnum: End point number for which the interrupt is to be processed
1943 * @intrstatus: mask value for interrupt sources of endpoints other
1944 * than endpoint 0.
1945 *
1946 * Processes the buffer completion interrupts.
1947 */
1948static void xudc_nonctrl_ep_handler(struct xusb_udc *udc, u8 epnum,
1949 u32 intrstatus)
1950{
1951
1952 struct xusb_req *req;
1953 struct xusb_ep *ep;
1954
1955 ep = &udc->ep[epnum];
1956 /* Process the End point interrupts.*/
1957 if (intrstatus & (XUSB_STATUS_EP0_BUFF1_COMP_MASK << epnum))
1958 ep->buffer0ready = 0;
1959 if (intrstatus & (XUSB_STATUS_EP0_BUFF2_COMP_MASK << epnum))
Jiapeng Zhongc86cad02021-01-14 17:03:22 +08001960 ep->buffer1ready = false;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05301961
1962 if (list_empty(&ep->queue))
1963 return;
1964
1965 req = list_first_entry(&ep->queue, struct xusb_req, queue);
1966
1967 if (ep->is_in)
1968 xudc_write_fifo(ep, req);
1969 else
1970 xudc_read_fifo(ep, req);
1971}
1972
1973/**
1974 * xudc_irq - The main interrupt handler.
1975 * @irq: The interrupt number.
1976 * @_udc: pointer to the usb device controller structure.
1977 *
1978 * Return: IRQ_HANDLED after the interrupt is handled.
1979 */
1980static irqreturn_t xudc_irq(int irq, void *_udc)
1981{
1982 struct xusb_udc *udc = _udc;
1983 u32 intrstatus;
1984 u32 ier;
1985 u8 index;
1986 u32 bufintr;
1987 unsigned long flags;
1988
1989 spin_lock_irqsave(&udc->lock, flags);
1990
1991 /*
1992 * Event interrupts are level sensitive hence first disable
1993 * IER, read ISR and figure out active interrupts.
1994 */
1995 ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1996 ier &= ~XUSB_STATUS_INTR_EVENT_MASK;
1997 udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
1998
1999 /* Read the Interrupt Status Register.*/
2000 intrstatus = udc->read_fn(udc->addr + XUSB_STATUS_OFFSET);
2001
2002 /* Call the handler for the event interrupt.*/
2003 if (intrstatus & XUSB_STATUS_INTR_EVENT_MASK) {
2004 /*
2005 * Check if there is any action to be done for :
2006 * - USB Reset received {XUSB_STATUS_RESET_MASK}
2007 * - USB Suspend received {XUSB_STATUS_SUSPEND_MASK}
2008 * - USB Resume received {XUSB_STATUS_RESUME_MASK}
2009 * - USB Disconnect received {XUSB_STATUS_DISCONNECT_MASK}
2010 */
2011 xudc_startup_handler(udc, intrstatus);
2012 }
2013
2014 /* Check the buffer completion interrupts */
2015 if (intrstatus & XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK) {
2016 /* Enable Reset, Suspend, Resume and Disconnect */
2017 ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
2018 ier |= XUSB_STATUS_INTR_EVENT_MASK;
2019 udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
2020
2021 if (intrstatus & XUSB_STATUS_EP0_BUFF1_COMP_MASK)
2022 xudc_ctrl_ep_handler(udc, intrstatus);
2023
2024 for (index = 1; index < 8; index++) {
2025 bufintr = ((intrstatus &
2026 (XUSB_STATUS_EP1_BUFF1_COMP_MASK <<
2027 (index - 1))) || (intrstatus &
2028 (XUSB_STATUS_EP1_BUFF2_COMP_MASK <<
2029 (index - 1))));
2030 if (bufintr) {
2031 xudc_nonctrl_ep_handler(udc, index,
2032 intrstatus);
2033 }
2034 }
2035 }
2036
2037 spin_unlock_irqrestore(&udc->lock, flags);
2038 return IRQ_HANDLED;
2039}
2040
2041/**
2042 * xudc_probe - The device probe function for driver initialization.
2043 * @pdev: pointer to the platform device structure.
2044 *
2045 * Return: 0 for success and error value on failure
2046 */
2047static int xudc_probe(struct platform_device *pdev)
2048{
2049 struct device_node *np = pdev->dev.of_node;
2050 struct resource *res;
2051 struct xusb_udc *udc;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302052 int irq;
2053 int ret;
2054 u32 ier;
2055 u8 *buff;
2056
2057 udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL);
2058 if (!udc)
2059 return -ENOMEM;
2060
2061 /* Create a dummy request for GET_STATUS, SET_ADDRESS */
2062 udc->req = devm_kzalloc(&pdev->dev, sizeof(struct xusb_req),
2063 GFP_KERNEL);
2064 if (!udc->req)
2065 return -ENOMEM;
2066
2067 buff = devm_kzalloc(&pdev->dev, STATUSBUFF_SIZE, GFP_KERNEL);
2068 if (!buff)
2069 return -ENOMEM;
2070
2071 udc->req->usb_req.buf = buff;
2072
2073 /* Map the registers */
2074 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2075 udc->addr = devm_ioremap_resource(&pdev->dev, res);
Vladimir Zapolskiy49bce152015-03-29 05:43:22 +03002076 if (IS_ERR(udc->addr))
2077 return PTR_ERR(udc->addr);
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302078
2079 irq = platform_get_irq(pdev, 0);
Stephen Boydb33f3702019-07-30 11:15:46 -07002080 if (irq < 0)
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302081 return irq;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302082 ret = devm_request_irq(&pdev->dev, irq, xudc_irq, 0,
2083 dev_name(&pdev->dev), udc);
2084 if (ret < 0) {
2085 dev_dbg(&pdev->dev, "unable to request irq %d", irq);
2086 goto fail;
2087 }
2088
2089 udc->dma_enabled = of_property_read_bool(np, "xlnx,has-builtin-dma");
2090
2091 /* Setup gadget structure */
2092 udc->gadget.ops = &xusb_udc_ops;
2093 udc->gadget.max_speed = USB_SPEED_HIGH;
2094 udc->gadget.speed = USB_SPEED_UNKNOWN;
2095 udc->gadget.ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO].ep_usb;
2096 udc->gadget.name = driver_name;
2097
Shubhrajyoti Datta24749222021-09-28 10:57:11 +05302098 udc->clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
2099 if (IS_ERR(udc->clk)) {
2100 if (PTR_ERR(udc->clk) != -ENOENT) {
2101 ret = PTR_ERR(udc->clk);
2102 goto fail;
2103 }
2104
2105 /*
2106 * Clock framework support is optional, continue on,
2107 * anyways if we don't find a matching clock
2108 */
2109 dev_warn(&pdev->dev, "s_axi_aclk clock property is not found\n");
2110 udc->clk = NULL;
2111 }
2112
2113 ret = clk_prepare_enable(udc->clk);
2114 if (ret) {
2115 dev_err(&pdev->dev, "Unable to enable clock.\n");
2116 return ret;
2117 }
2118
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302119 spin_lock_init(&udc->lock);
2120
2121 /* Check for IP endianness */
2122 udc->write_fn = xudc_write32_be;
2123 udc->read_fn = xudc_read32_be;
Greg Kroah-Hartman62fb45d2020-06-18 16:42:06 +02002124 udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, USB_TEST_J);
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302125 if ((udc->read_fn(udc->addr + XUSB_TESTMODE_OFFSET))
Greg Kroah-Hartman62fb45d2020-06-18 16:42:06 +02002126 != USB_TEST_J) {
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302127 udc->write_fn = xudc_write32;
2128 udc->read_fn = xudc_read32;
2129 }
2130 udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, 0);
2131
2132 xudc_eps_init(udc);
2133
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302134 /* Set device address to 0.*/
2135 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
2136
2137 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2138 if (ret)
Christophe JAILLET3b8599a2021-11-07 17:25:48 +01002139 goto err_disable_unprepare_clk;
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302140
2141 udc->dev = &udc->gadget.dev;
2142
2143 /* Enable the interrupts.*/
2144 ier = XUSB_STATUS_GLOBAL_INTR_MASK | XUSB_STATUS_INTR_EVENT_MASK |
2145 XUSB_STATUS_FIFO_BUFF_RDY_MASK | XUSB_STATUS_FIFO_BUFF_FREE_MASK |
2146 XUSB_STATUS_SETUP_PACKET_MASK |
2147 XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK;
2148
2149 udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
2150
2151 platform_set_drvdata(pdev, udc);
2152
Linus Torvalds7796c112015-02-11 10:52:56 -08002153 dev_vdbg(&pdev->dev, "%s at 0x%08X mapped to %p %s\n",
2154 driver_name, (u32)res->start, udc->addr,
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302155 udc->dma_enabled ? "with DMA" : "without DMA");
2156
2157 return 0;
Christophe JAILLET3b8599a2021-11-07 17:25:48 +01002158
2159err_disable_unprepare_clk:
2160 clk_disable_unprepare(udc->clk);
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302161fail:
2162 dev_err(&pdev->dev, "probe failed, %d\n", ret);
2163 return ret;
2164}
2165
2166/**
2167 * xudc_remove - Releases the resources allocated during the initialization.
2168 * @pdev: pointer to the platform device structure.
2169 *
2170 * Return: 0 always
2171 */
2172static int xudc_remove(struct platform_device *pdev)
2173{
2174 struct xusb_udc *udc = platform_get_drvdata(pdev);
2175
2176 usb_del_gadget_udc(&udc->gadget);
Shubhrajyoti Datta24749222021-09-28 10:57:11 +05302177 clk_disable_unprepare(udc->clk);
Subbaraya Sundeep Bhatta1f7c5162014-09-10 19:24:04 +05302178
2179 return 0;
2180}
2181
2182/* Match table for of_platform binding */
2183static const struct of_device_id usb_of_match[] = {
2184 { .compatible = "xlnx,usb2-device-4.00.a", },
2185 { /* end of list */ },
2186};
2187MODULE_DEVICE_TABLE(of, usb_of_match);
2188
2189static struct platform_driver xudc_driver = {
2190 .driver = {
2191 .name = driver_name,
2192 .of_match_table = usb_of_match,
2193 },
2194 .probe = xudc_probe,
2195 .remove = xudc_remove,
2196};
2197
2198module_platform_driver(xudc_driver);
2199
2200MODULE_DESCRIPTION("Xilinx udc driver");
2201MODULE_AUTHOR("Xilinx, Inc");
2202MODULE_LICENSE("GPL");