blob: 930c490dd83b5e4d391687e36a739bf074bcaa76 [file] [log] [blame]
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001/**
2 * linux/drivers/usb/gadget/s3c-hsotg.c
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003 *
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09004 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01007 * Copyright 2008 Openmoko, Inc.
8 * Copyright 2008 Simtec Electronics
9 * Ben Dooks <ben@simtec.co.uk>
10 * http://armlinux.simtec.co.uk/
11 *
12 * S3C USB2.0 High-speed / OtG driver
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020017 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010018
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
23#include <linux/platform_device.h>
24#include <linux/dma-mapping.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27#include <linux/delay.h>
28#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Maurus Cuelenaeree50bf382010-07-19 09:40:50 +010030#include <linux/clk.h>
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020031#include <linux/regulator/consumer.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020032#include <linux/of_platform.h>
Matt Porter74084842013-12-19 09:23:06 -050033#include <linux/phy/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010034
35#include <linux/usb/ch9.h>
36#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053037#include <linux/usb/phy.h>
Lukasz Majewski126625e2012-05-09 13:16:53 +020038#include <linux/platform_data/s3c-hsotg.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010039
Lukasz Majewski127d42a2012-05-04 14:16:59 +020040#include "s3c-hsotg.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020042static const char * const s3c_hsotg_supply_names[] = {
43 "vusb_d", /* digital USB supply, 1.2V */
44 "vusb_a", /* analog USB supply, 1.1V */
45};
46
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020047/*
48 * EP0_MPS_LIMIT
Ben Dooks5b7d70c2009-06-02 14:58:06 +010049 *
50 * Unfortunately there seems to be a limit of the amount of data that can
Lucas De Marchi25985ed2011-03-30 22:57:33 -030051 * be transferred by IN transactions on EP0. This is either 127 bytes or 3
52 * packets (which practically means 1 packet and 63 bytes of data) when the
Ben Dooks5b7d70c2009-06-02 14:58:06 +010053 * MPS is set to 64.
54 *
55 * This means if we are wanting to move >127 bytes of data, we need to
56 * split the transactions up, but just doing one packet at a time does
57 * not work (this may be an implicit DATA0 PID on first packet of the
58 * transaction) and doing 2 packets is outside the controller's limits.
59 *
60 * If we try to lower the MPS size for EP0, then no transfers work properly
61 * for EP0, and the system will fail basic enumeration. As no cause for this
62 * has currently been found, we cannot support any large IN transfers for
63 * EP0.
64 */
65#define EP0_MPS_LIMIT 64
66
67struct s3c_hsotg;
68struct s3c_hsotg_req;
69
70/**
71 * struct s3c_hsotg_ep - driver endpoint definition.
72 * @ep: The gadget layer representation of the endpoint.
73 * @name: The driver generated name for the endpoint.
74 * @queue: Queue of requests for this endpoint.
75 * @parent: Reference back to the parent device structure.
76 * @req: The current request that the endpoint is processing. This is
77 * used to indicate an request has been loaded onto the endpoint
78 * and has yet to be completed (maybe due to data move, or simply
79 * awaiting an ack from the core all the data has been completed).
80 * @debugfs: File entry for debugfs file for this endpoint.
81 * @lock: State lock to protect contents of endpoint.
82 * @dir_in: Set to true if this endpoint is of the IN direction, which
83 * means that it is sending data to the Host.
84 * @index: The index for the endpoint registers.
Robert Baldyga4fca54a2013-10-09 09:00:02 +020085 * @mc: Multi Count - number of transactions per microframe
Robert Baldyga1479e842013-10-09 08:41:57 +020086 * @interval - Interval for periodic endpoints
Ben Dooks5b7d70c2009-06-02 14:58:06 +010087 * @name: The name array passed to the USB core.
88 * @halted: Set if the endpoint has been halted.
89 * @periodic: Set if this is a periodic ep, such as Interrupt
Robert Baldyga1479e842013-10-09 08:41:57 +020090 * @isochronous: Set if this is a isochronous ep
Ben Dooks5b7d70c2009-06-02 14:58:06 +010091 * @sent_zlp: Set if we've sent a zero-length packet.
92 * @total_data: The total number of data bytes done.
93 * @fifo_size: The size of the FIFO (for periodic IN endpoints)
94 * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
95 * @last_load: The offset of data for the last start of request.
96 * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
97 *
98 * This is the driver's state for each registered enpoint, allowing it
99 * to keep track of transactions that need doing. Each endpoint has a
100 * lock to protect the state, to try and avoid using an overall lock
101 * for the host controller as much as possible.
102 *
103 * For periodic IN endpoints, we have fifo_size and fifo_load to try
104 * and keep track of the amount of data in the periodic FIFO for each
105 * of these as we don't have a status register that tells us how much
Ben Dookse7a9ff52010-07-19 09:40:42 +0100106 * is in each of them. (note, this may actually be useless information
107 * as in shared-fifo mode periodic in acts like a single-frame packet
108 * buffer than a fifo)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100109 */
110struct s3c_hsotg_ep {
111 struct usb_ep ep;
112 struct list_head queue;
113 struct s3c_hsotg *parent;
114 struct s3c_hsotg_req *req;
115 struct dentry *debugfs;
116
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100117
118 unsigned long total_data;
119 unsigned int size_loaded;
120 unsigned int last_load;
121 unsigned int fifo_load;
122 unsigned short fifo_size;
123
124 unsigned char dir_in;
125 unsigned char index;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200126 unsigned char mc;
Robert Baldyga1479e842013-10-09 08:41:57 +0200127 unsigned char interval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100128
129 unsigned int halted:1;
130 unsigned int periodic:1;
Robert Baldyga1479e842013-10-09 08:41:57 +0200131 unsigned int isochronous:1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100132 unsigned int sent_zlp:1;
133
134 char name[10];
135};
136
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100137/**
138 * struct s3c_hsotg - driver state.
139 * @dev: The parent device supplied to the probe function
140 * @driver: USB gadget driver
Praveen Panerib2e587d2012-11-14 15:57:16 +0530141 * @phy: The otg phy transceiver structure for phy control.
Matt Porter74084842013-12-19 09:23:06 -0500142 * @uphy: The otg phy transceiver structure for old USB phy control.
Praveen Panerib2e587d2012-11-14 15:57:16 +0530143 * @plat: The platform specific configuration data. This can be removed once
144 * all SoCs support usb transceiver.
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100145 * @regs: The memory area mapped for accessing registers.
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100146 * @irq: The IRQ number we are using
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200147 * @supplies: Definition of USB power supplies
Matt Porterf7e504c2013-12-19 09:23:07 -0500148 * @phyif: PHY interface width
Ben Dooks10aebc72010-07-19 09:40:44 +0100149 * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200150 * @num_of_eps: Number of available EPs (excluding EP0)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100151 * @debug_root: root directrory for debugfs.
152 * @debug_file: main status file for debugfs.
153 * @debug_fifo: FIFO status file for debugfs.
154 * @ep0_reply: Request used for ep0 reply.
155 * @ep0_buff: Buffer for EP0 reply data, if needed.
156 * @ctrl_buff: Buffer for EP0 control requests.
157 * @ctrl_req: Request for EP0 control packets.
Lukasz Majewski71225be2012-05-04 14:17:03 +0200158 * @setup: NAK management for EP0 SETUP
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200159 * @last_rst: Time of last reset
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100160 * @eps: The endpoints being supplied to the gadget framework
161 */
162struct s3c_hsotg {
163 struct device *dev;
164 struct usb_gadget_driver *driver;
Matt Porter74084842013-12-19 09:23:06 -0500165 struct phy *phy;
166 struct usb_phy *uphy;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100167 struct s3c_hsotg_plat *plat;
168
Lukasz Majewski22258f42012-06-14 10:02:24 +0200169 spinlock_t lock;
170
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100171 void __iomem *regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100172 int irq;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +0200173 struct clk *clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100174
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200175 struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
176
Matt Porterf7e504c2013-12-19 09:23:07 -0500177 u32 phyif;
Ben Dooks10aebc72010-07-19 09:40:44 +0100178 unsigned int dedicated_fifos:1;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200179 unsigned char num_of_eps;
Ben Dooks10aebc72010-07-19 09:40:44 +0100180
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100181 struct dentry *debug_root;
182 struct dentry *debug_file;
183 struct dentry *debug_fifo;
184
185 struct usb_request *ep0_reply;
186 struct usb_request *ctrl_req;
187 u8 ep0_buff[8];
188 u8 ctrl_buff[8];
189
190 struct usb_gadget gadget;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200191 unsigned int setup;
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200192 unsigned long last_rst;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200193 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100194};
195
196/**
197 * struct s3c_hsotg_req - data transfer request
198 * @req: The USB gadget request
199 * @queue: The list of requests for the endpoint this is queued for.
200 * @in_progress: Has already had size/packets written to core
201 * @mapped: DMA buffer for this request has been mapped via dma_map_single().
202 */
203struct s3c_hsotg_req {
204 struct usb_request req;
205 struct list_head queue;
206 unsigned char in_progress;
207 unsigned char mapped;
208};
209
210/* conversion functions */
211static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
212{
213 return container_of(req, struct s3c_hsotg_req, req);
214}
215
216static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
217{
218 return container_of(ep, struct s3c_hsotg_ep, ep);
219}
220
221static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
222{
223 return container_of(gadget, struct s3c_hsotg, gadget);
224}
225
226static inline void __orr32(void __iomem *ptr, u32 val)
227{
228 writel(readl(ptr) | val, ptr);
229}
230
231static inline void __bic32(void __iomem *ptr, u32 val)
232{
233 writel(readl(ptr) & ~val, ptr);
234}
235
236/* forward decleration of functions */
237static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
238
239/**
240 * using_dma - return the DMA status of the driver.
241 * @hsotg: The driver state.
242 *
243 * Return true if we're using DMA.
244 *
245 * Currently, we have the DMA support code worked into everywhere
246 * that needs it, but the AMBA DMA implementation in the hardware can
247 * only DMA from 32bit aligned addresses. This means that gadgets such
248 * as the CDC Ethernet cannot work as they often pass packets which are
249 * not 32bit aligned.
250 *
251 * Unfortunately the choice to use DMA or not is global to the controller
252 * and seems to be only settable when the controller is being put through
253 * a core reset. This means we either need to fix the gadgets to take
254 * account of DMA alignment, or add bounce buffers (yuerk).
255 *
256 * Until this issue is sorted out, we always return 'false'.
257 */
258static inline bool using_dma(struct s3c_hsotg *hsotg)
259{
260 return false; /* support is not complete */
261}
262
263/**
264 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
265 * @hsotg: The device state
266 * @ints: A bitmask of the interrupts to enable
267 */
268static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
269{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200270 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100271 u32 new_gsintmsk;
272
273 new_gsintmsk = gsintmsk | ints;
274
275 if (new_gsintmsk != gsintmsk) {
276 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200277 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100278 }
279}
280
281/**
282 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
283 * @hsotg: The device state
284 * @ints: A bitmask of the interrupts to enable
285 */
286static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
287{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200288 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100289 u32 new_gsintmsk;
290
291 new_gsintmsk = gsintmsk & ~ints;
292
293 if (new_gsintmsk != gsintmsk)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200294 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100295}
296
297/**
298 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
299 * @hsotg: The device state
300 * @ep: The endpoint index
301 * @dir_in: True if direction is in.
302 * @en: The enable value, true to enable
303 *
304 * Set or clear the mask for an individual endpoint's interrupt
305 * request.
306 */
307static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
308 unsigned int ep, unsigned int dir_in,
309 unsigned int en)
310{
311 unsigned long flags;
312 u32 bit = 1 << ep;
313 u32 daint;
314
315 if (!dir_in)
316 bit <<= 16;
317
318 local_irq_save(flags);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200319 daint = readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100320 if (en)
321 daint |= bit;
322 else
323 daint &= ~bit;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200324 writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100325 local_irq_restore(flags);
326}
327
328/**
329 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
330 * @hsotg: The device instance.
331 */
332static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
333{
Ben Dooks0f002d22010-05-25 05:36:50 +0100334 unsigned int ep;
335 unsigned int addr;
336 unsigned int size;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100337 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100338 u32 val;
339
Ben Dooks6d091ee2010-07-19 09:40:40 +0100340 /* set FIFO sizes to 2048/1024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100341
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200342 writel(2048, hsotg->regs + GRXFSIZ);
343 writel(GNPTXFSIZ_NPTxFStAddr(2048) |
344 GNPTXFSIZ_NPTxFDep(1024),
345 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100346
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200347 /*
348 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100349 * block have overlapping default addresses. This also ensures
350 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200351 * known values.
352 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100353
354 /* start at the end of the GNPTXFSIZ, rounded up */
355 addr = 2048 + 1024;
356 size = 768;
357
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200358 /*
359 * currently we allocate TX FIFOs for all possible endpoints,
360 * and assume that they are all the same size.
361 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100362
Anton Tikhomirovf7a83fe2012-03-06 14:05:49 +0900363 for (ep = 1; ep <= 15; ep++) {
Ben Dooks0f002d22010-05-25 05:36:50 +0100364 val = addr;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200365 val |= size << DPTXFSIZn_DPTxFSize_SHIFT;
Ben Dooks0f002d22010-05-25 05:36:50 +0100366 addr += size;
367
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200368 writel(val, hsotg->regs + DPTXFSIZn(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100369 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100370
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200371 /*
372 * according to p428 of the design guide, we need to ensure that
373 * all fifos are flushed before continuing
374 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100375
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200376 writel(GRSTCTL_TxFNum(0x10) | GRSTCTL_TxFFlsh |
377 GRSTCTL_RxFFlsh, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100378
379 /* wait until the fifos are both flushed */
380 timeout = 100;
381 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200382 val = readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100383
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200384 if ((val & (GRSTCTL_TxFFlsh | GRSTCTL_RxFFlsh)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100385 break;
386
387 if (--timeout == 0) {
388 dev_err(hsotg->dev,
389 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
390 __func__, val);
391 }
392
393 udelay(1);
394 }
395
396 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100397}
398
399/**
400 * @ep: USB endpoint to allocate request for.
401 * @flags: Allocation flags
402 *
403 * Allocate a new USB request structure appropriate for the specified endpoint
404 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000405static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
406 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100407{
408 struct s3c_hsotg_req *req;
409
410 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
411 if (!req)
412 return NULL;
413
414 INIT_LIST_HEAD(&req->queue);
415
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100416 return &req->req;
417}
418
419/**
420 * is_ep_periodic - return true if the endpoint is in periodic mode.
421 * @hs_ep: The endpoint to query.
422 *
423 * Returns true if the endpoint is in periodic mode, meaning it is being
424 * used for an Interrupt or ISO transfer.
425 */
426static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
427{
428 return hs_ep->periodic;
429}
430
431/**
432 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
433 * @hsotg: The device state.
434 * @hs_ep: The endpoint for the request
435 * @hs_req: The request being processed.
436 *
437 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
438 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200439 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100440static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
441 struct s3c_hsotg_ep *hs_ep,
442 struct s3c_hsotg_req *hs_req)
443{
444 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100445
446 /* ignore this if we're not moving any data */
447 if (hs_req->req.length == 0)
448 return;
449
Jingoo Han17d966a2013-05-11 21:14:00 +0900450 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100451}
452
453/**
454 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
455 * @hsotg: The controller state.
456 * @hs_ep: The endpoint we're going to write for.
457 * @hs_req: The request to write data for.
458 *
459 * This is called when the TxFIFO has some space in it to hold a new
460 * transmission and we have something to give it. The actual setup of
461 * the data size is done elsewhere, so all we have to do is to actually
462 * write the data.
463 *
464 * The return value is zero if there is more space (or nothing was done)
465 * otherwise -ENOSPC is returned if the FIFO space was used up.
466 *
467 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200468 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100469static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
470 struct s3c_hsotg_ep *hs_ep,
471 struct s3c_hsotg_req *hs_req)
472{
473 bool periodic = is_ep_periodic(hs_ep);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200474 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100475 int buf_pos = hs_req->req.actual;
476 int to_write = hs_ep->size_loaded;
477 void *data;
478 int can_write;
479 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200480 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100481
482 to_write -= (buf_pos - hs_ep->last_load);
483
484 /* if there's nothing to write, get out early */
485 if (to_write == 0)
486 return 0;
487
Ben Dooks10aebc72010-07-19 09:40:44 +0100488 if (periodic && !hsotg->dedicated_fifos) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200489 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100490 int size_left;
491 int size_done;
492
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200493 /*
494 * work out how much data was loaded so we can calculate
495 * how much data is left in the fifo.
496 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100497
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200498 size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100499
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200500 /*
501 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100502 * previous data has been completely sent.
503 */
504 if (hs_ep->fifo_load != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200505 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100506 return -ENOSPC;
507 }
508
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100509 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
510 __func__, size_left,
511 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
512
513 /* how much of the data has moved */
514 size_done = hs_ep->size_loaded - size_left;
515
516 /* how much data is left in the fifo */
517 can_write = hs_ep->fifo_load - size_done;
518 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
519 __func__, can_write);
520
521 can_write = hs_ep->fifo_size - can_write;
522 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
523 __func__, can_write);
524
525 if (can_write <= 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200526 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100527 return -ENOSPC;
528 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100529 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200530 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100531
532 can_write &= 0xffff;
533 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100534 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200535 if (GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100536 dev_dbg(hsotg->dev,
537 "%s: no queue slots available (0x%08x)\n",
538 __func__, gnptxsts);
539
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200540 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100541 return -ENOSPC;
542 }
543
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200544 can_write = GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100545 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100546 }
547
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200548 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
549
550 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
551 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100552
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200553 /*
554 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100555 * FIFO, requests of >512 cause the endpoint to get stuck with a
556 * fragment of the end of the transfer in it.
557 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200558 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100559 can_write = 512;
560
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200561 /*
562 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100563 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200564 * doing it.
565 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200566 if (to_write > max_transfer) {
567 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100568
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200569 /* it's needed only when we do not use dedicated fifos */
570 if (!hsotg->dedicated_fifos)
571 s3c_hsotg_en_gsint(hsotg,
572 periodic ? GINTSTS_PTxFEmp :
573 GINTSTS_NPTxFEmp);
Ben Dooks03e10e52010-07-19 09:40:45 +0100574 }
575
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100576 /* see if we can write data */
577
578 if (to_write > can_write) {
579 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200580 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100581
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200582 /*
583 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100584 * exact number of packets.
585 *
586 * Note, we do not currently check to see if we can ever
587 * write a full packet or not to the FIFO.
588 */
589
590 if (pkt_round)
591 to_write -= pkt_round;
592
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200593 /*
594 * enable correct FIFO interrupt to alert us when there
595 * is more room left.
596 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100597
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200598 /* it's needed only when we do not use dedicated fifos */
599 if (!hsotg->dedicated_fifos)
600 s3c_hsotg_en_gsint(hsotg,
601 periodic ? GINTSTS_PTxFEmp :
602 GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100603 }
604
605 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
606 to_write, hs_req->req.length, can_write, buf_pos);
607
608 if (to_write <= 0)
609 return -ENOSPC;
610
611 hs_req->req.actual = buf_pos + to_write;
612 hs_ep->total_data += to_write;
613
614 if (periodic)
615 hs_ep->fifo_load += to_write;
616
617 to_write = DIV_ROUND_UP(to_write, 4);
618 data = hs_req->req.buf + buf_pos;
619
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500620 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100621
622 return (to_write >= can_write) ? -ENOSPC : 0;
623}
624
625/**
626 * get_ep_limit - get the maximum data legnth for this endpoint
627 * @hs_ep: The endpoint
628 *
629 * Return the maximum data that can be queued in one go on a given endpoint
630 * so that transfers that are too long can be split.
631 */
632static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
633{
634 int index = hs_ep->index;
635 unsigned maxsize;
636 unsigned maxpkt;
637
638 if (index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200639 maxsize = DxEPTSIZ_XferSize_LIMIT + 1;
640 maxpkt = DxEPTSIZ_PktCnt_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100641 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100642 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900643 if (hs_ep->dir_in)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200644 maxpkt = DIEPTSIZ0_PktCnt_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900645 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100646 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100647 }
648
649 /* we made the constant loading easier above by using +1 */
650 maxpkt--;
651 maxsize--;
652
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200653 /*
654 * constrain by packet count if maxpkts*pktsize is greater
655 * than the length register size.
656 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100657
658 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
659 maxsize = maxpkt * hs_ep->ep.maxpacket;
660
661 return maxsize;
662}
663
664/**
665 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
666 * @hsotg: The controller state.
667 * @hs_ep: The endpoint to process a request for
668 * @hs_req: The request to start.
669 * @continuing: True if we are doing more for the current request.
670 *
671 * Start the given request running by setting the endpoint registers
672 * appropriately, and writing any data to the FIFOs.
673 */
674static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
675 struct s3c_hsotg_ep *hs_ep,
676 struct s3c_hsotg_req *hs_req,
677 bool continuing)
678{
679 struct usb_request *ureq = &hs_req->req;
680 int index = hs_ep->index;
681 int dir_in = hs_ep->dir_in;
682 u32 epctrl_reg;
683 u32 epsize_reg;
684 u32 epsize;
685 u32 ctrl;
686 unsigned length;
687 unsigned packets;
688 unsigned maxreq;
689
690 if (index != 0) {
691 if (hs_ep->req && !continuing) {
692 dev_err(hsotg->dev, "%s: active request\n", __func__);
693 WARN_ON(1);
694 return;
695 } else if (hs_ep->req != hs_req && continuing) {
696 dev_err(hsotg->dev,
697 "%s: continue different req\n", __func__);
698 WARN_ON(1);
699 return;
700 }
701 }
702
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200703 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
704 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100705
706 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
707 __func__, readl(hsotg->regs + epctrl_reg), index,
708 hs_ep->dir_in ? "in" : "out");
709
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900710 /* If endpoint is stalled, we will restart request later */
711 ctrl = readl(hsotg->regs + epctrl_reg);
712
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200713 if (ctrl & DxEPCTL_Stall) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900714 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
715 return;
716 }
717
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100718 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200719 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
720 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100721 if (0)
722 dev_dbg(hsotg->dev,
Jingoo Han8b3bc142014-02-04 14:25:29 +0900723 "REQ buf %p len %d dma 0x%pad noi=%d zp=%d snok=%d\n",
724 ureq->buf, length, &ureq->dma,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100725 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
726
727 maxreq = get_ep_limit(hs_ep);
728 if (length > maxreq) {
729 int round = maxreq % hs_ep->ep.maxpacket;
730
731 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
732 __func__, length, maxreq, round);
733
734 /* round down to multiple of packets */
735 if (round)
736 maxreq -= round;
737
738 length = maxreq;
739 }
740
741 if (length)
742 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
743 else
744 packets = 1; /* send one packet if length is zero. */
745
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200746 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
747 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
748 return;
749 }
750
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100751 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200752 if (hs_ep->isochronous)
753 epsize = DxEPTSIZ_MC(packets);
754 else
755 epsize = DxEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100756 else
757 epsize = 0;
758
759 if (index != 0 && ureq->zero) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200760 /*
761 * test for the packets being exactly right for the
762 * transfer
763 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100764
765 if (length == (packets * hs_ep->ep.maxpacket))
766 packets++;
767 }
768
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200769 epsize |= DxEPTSIZ_PktCnt(packets);
770 epsize |= DxEPTSIZ_XferSize(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100771
772 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
773 __func__, packets, length, ureq->length, epsize, epsize_reg);
774
775 /* store the request as the current one we're doing */
776 hs_ep->req = hs_req;
777
778 /* write size / packets */
779 writel(epsize, hsotg->regs + epsize_reg);
780
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900781 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100782 unsigned int dma_reg;
783
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200784 /*
785 * write DMA address to control register, buffer already
786 * synced by s3c_hsotg_ep_queue().
787 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100788
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200789 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100790 writel(ureq->dma, hsotg->regs + dma_reg);
791
Jingoo Han8b3bc142014-02-04 14:25:29 +0900792 dev_dbg(hsotg->dev, "%s: 0x%pad => 0x%08x\n",
793 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100794 }
795
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200796 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
797 ctrl |= DxEPCTL_USBActEp;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200798
799 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
800
801 /* For Setup request do not clear NAK */
802 if (hsotg->setup && index == 0)
803 hsotg->setup = 0;
804 else
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200805 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200806
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100807
808 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
809 writel(ctrl, hsotg->regs + epctrl_reg);
810
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200811 /*
812 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100813 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200814 * this information.
815 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100816 hs_ep->size_loaded = length;
817 hs_ep->last_load = ureq->actual;
818
819 if (dir_in && !using_dma(hsotg)) {
820 /* set these anyway, we may need them for non-periodic in */
821 hs_ep->fifo_load = 0;
822
823 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
824 }
825
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200826 /*
827 * clear the INTknTXFEmpMsk when we start request, more as a aide
828 * to debugging to see what is going on.
829 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100830 if (dir_in)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200831 writel(DIEPMSK_INTknTXFEmpMsk,
832 hsotg->regs + DIEPINT(index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100833
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200834 /*
835 * Note, trying to clear the NAK here causes problems with transmit
836 * on the S3C6400 ending up with the TXFIFO becoming full.
837 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100838
839 /* check ep is enabled */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200840 if (!(readl(hsotg->regs + epctrl_reg) & DxEPCTL_EPEna))
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100841 dev_warn(hsotg->dev,
842 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
843 index, readl(hsotg->regs + epctrl_reg));
844
845 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
846 __func__, readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200847
848 /* enable ep interrupts */
849 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100850}
851
852/**
853 * s3c_hsotg_map_dma - map the DMA memory being used for the request
854 * @hsotg: The device state.
855 * @hs_ep: The endpoint the request is on.
856 * @req: The request being processed.
857 *
858 * We've been asked to queue a request, so ensure that the memory buffer
859 * is correctly setup for DMA. If we've been passed an extant DMA address
860 * then ensure the buffer has been synced to memory. If our buffer has no
861 * DMA memory, then we map the memory and mark our request to allow us to
862 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200863 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100864static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
865 struct s3c_hsotg_ep *hs_ep,
866 struct usb_request *req)
867{
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100868 struct s3c_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200869 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100870
871 /* if the length is zero, ignore the DMA data */
872 if (hs_req->req.length == 0)
873 return 0;
874
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200875 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
876 if (ret)
877 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100878
879 return 0;
880
881dma_error:
882 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
883 __func__, req->buf, req->length);
884
885 return -EIO;
886}
887
888static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
889 gfp_t gfp_flags)
890{
891 struct s3c_hsotg_req *hs_req = our_req(req);
892 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
893 struct s3c_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100894 bool first;
895
896 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
897 ep->name, req, req->length, req->buf, req->no_interrupt,
898 req->zero, req->short_not_ok);
899
900 /* initialise status of the request */
901 INIT_LIST_HEAD(&hs_req->queue);
902 req->actual = 0;
903 req->status = -EINPROGRESS;
904
905 /* if we're using DMA, sync the buffers as necessary */
906 if (using_dma(hs)) {
907 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
908 if (ret)
909 return ret;
910 }
911
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100912 first = list_empty(&hs_ep->queue);
913 list_add_tail(&hs_req->queue, &hs_ep->queue);
914
915 if (first)
916 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
917
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100918 return 0;
919}
920
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200921static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
922 gfp_t gfp_flags)
923{
924 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
925 struct s3c_hsotg *hs = hs_ep->parent;
926 unsigned long flags = 0;
927 int ret = 0;
928
929 spin_lock_irqsave(&hs->lock, flags);
930 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
931 spin_unlock_irqrestore(&hs->lock, flags);
932
933 return ret;
934}
935
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100936static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
937 struct usb_request *req)
938{
939 struct s3c_hsotg_req *hs_req = our_req(req);
940
941 kfree(hs_req);
942}
943
944/**
945 * s3c_hsotg_complete_oursetup - setup completion callback
946 * @ep: The endpoint the request was on.
947 * @req: The request completed.
948 *
949 * Called on completion of any requests the driver itself
950 * submitted that need cleaning up.
951 */
952static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
953 struct usb_request *req)
954{
955 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
956 struct s3c_hsotg *hsotg = hs_ep->parent;
957
958 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
959
960 s3c_hsotg_ep_free_request(ep, req);
961}
962
963/**
964 * ep_from_windex - convert control wIndex value to endpoint
965 * @hsotg: The driver state.
966 * @windex: The control request wIndex field (in host order).
967 *
968 * Convert the given wIndex into a pointer to an driver endpoint
969 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200970 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100971static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
972 u32 windex)
973{
974 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
975 int dir = (windex & USB_DIR_IN) ? 1 : 0;
976 int idx = windex & 0x7F;
977
978 if (windex >= 0x100)
979 return NULL;
980
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200981 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100982 return NULL;
983
984 if (idx && ep->dir_in != dir)
985 return NULL;
986
987 return ep;
988}
989
990/**
991 * s3c_hsotg_send_reply - send reply to control request
992 * @hsotg: The device state
993 * @ep: Endpoint 0
994 * @buff: Buffer for request
995 * @length: Length of reply.
996 *
997 * Create a request and queue it on the given endpoint. This is useful as
998 * an internal method of sending replies to certain control requests, etc.
999 */
1000static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
1001 struct s3c_hsotg_ep *ep,
1002 void *buff,
1003 int length)
1004{
1005 struct usb_request *req;
1006 int ret;
1007
1008 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1009
1010 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1011 hsotg->ep0_reply = req;
1012 if (!req) {
1013 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1014 return -ENOMEM;
1015 }
1016
1017 req->buf = hsotg->ep0_buff;
1018 req->length = length;
1019 req->zero = 1; /* always do zero-length final transfer */
1020 req->complete = s3c_hsotg_complete_oursetup;
1021
1022 if (length)
1023 memcpy(req->buf, buff, length);
1024 else
1025 ep->sent_zlp = 1;
1026
1027 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1028 if (ret) {
1029 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1030 return ret;
1031 }
1032
1033 return 0;
1034}
1035
1036/**
1037 * s3c_hsotg_process_req_status - process request GET_STATUS
1038 * @hsotg: The device state
1039 * @ctrl: USB control request
1040 */
1041static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1042 struct usb_ctrlrequest *ctrl)
1043{
1044 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1045 struct s3c_hsotg_ep *ep;
1046 __le16 reply;
1047 int ret;
1048
1049 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1050
1051 if (!ep0->dir_in) {
1052 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1053 return -EINVAL;
1054 }
1055
1056 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1057 case USB_RECIP_DEVICE:
1058 reply = cpu_to_le16(0); /* bit 0 => self powered,
1059 * bit 1 => remote wakeup */
1060 break;
1061
1062 case USB_RECIP_INTERFACE:
1063 /* currently, the data result should be zero */
1064 reply = cpu_to_le16(0);
1065 break;
1066
1067 case USB_RECIP_ENDPOINT:
1068 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1069 if (!ep)
1070 return -ENOENT;
1071
1072 reply = cpu_to_le16(ep->halted ? 1 : 0);
1073 break;
1074
1075 default:
1076 return 0;
1077 }
1078
1079 if (le16_to_cpu(ctrl->wLength) != 2)
1080 return -EINVAL;
1081
1082 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1083 if (ret) {
1084 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1085 return ret;
1086 }
1087
1088 return 1;
1089}
1090
1091static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1092
1093/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001094 * get_ep_head - return the first request on the endpoint
1095 * @hs_ep: The controller endpoint to get
1096 *
1097 * Get the first request on the endpoint.
1098 */
1099static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1100{
1101 if (list_empty(&hs_ep->queue))
1102 return NULL;
1103
1104 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1105}
1106
1107/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001108 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1109 * @hsotg: The device state
1110 * @ctrl: USB control request
1111 */
1112static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1113 struct usb_ctrlrequest *ctrl)
1114{
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001115 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001116 struct s3c_hsotg_req *hs_req;
1117 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001118 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1119 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001120 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001121 bool halted;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122
1123 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1124 __func__, set ? "SET" : "CLEAR");
1125
1126 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1127 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1128 if (!ep) {
1129 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1130 __func__, le16_to_cpu(ctrl->wIndex));
1131 return -ENOENT;
1132 }
1133
1134 switch (le16_to_cpu(ctrl->wValue)) {
1135 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001136 halted = ep->halted;
1137
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001139
1140 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1141 if (ret) {
1142 dev_err(hsotg->dev,
1143 "%s: failed to send reply\n", __func__);
1144 return ret;
1145 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001146
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001147 /*
1148 * we have to complete all requests for ep if it was
1149 * halted, and the halt was cleared by CLEAR_FEATURE
1150 */
1151
1152 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001153 /*
1154 * If we have request in progress,
1155 * then complete it
1156 */
1157 if (ep->req) {
1158 hs_req = ep->req;
1159 ep->req = NULL;
1160 list_del_init(&hs_req->queue);
1161 hs_req->req.complete(&ep->ep,
1162 &hs_req->req);
1163 }
1164
1165 /* If we have pending request, then start it */
1166 restart = !list_empty(&ep->queue);
1167 if (restart) {
1168 hs_req = get_ep_head(ep);
1169 s3c_hsotg_start_req(hsotg, ep,
1170 hs_req, false);
1171 }
1172 }
1173
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001174 break;
1175
1176 default:
1177 return -ENOENT;
1178 }
1179 } else
1180 return -ENOENT; /* currently only deal with endpoint */
1181
1182 return 1;
1183}
1184
Robert Baldygaab93e012013-09-19 11:50:17 +02001185static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
Robert Baldygad18f71162013-11-21 13:49:18 +01001186static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001187
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001188/**
1189 * s3c_hsotg_process_control - process a control request
1190 * @hsotg: The device state
1191 * @ctrl: The control request received
1192 *
1193 * The controller has received the SETUP phase of a control request, and
1194 * needs to work out what to do next (and whether to pass it on to the
1195 * gadget driver).
1196 */
1197static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1198 struct usb_ctrlrequest *ctrl)
1199{
1200 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1201 int ret = 0;
1202 u32 dcfg;
1203
1204 ep0->sent_zlp = 0;
1205
1206 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1207 ctrl->bRequest, ctrl->bRequestType,
1208 ctrl->wValue, ctrl->wLength);
1209
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001210 /*
1211 * record the direction of the request, for later use when enquing
1212 * packets onto EP0.
1213 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001214
1215 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1216 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1217
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001218 /*
1219 * if we've no data with this request, then the last part of the
1220 * transaction is going to implicitly be IN.
1221 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001222 if (ctrl->wLength == 0)
1223 ep0->dir_in = 1;
1224
1225 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1226 switch (ctrl->bRequest) {
1227 case USB_REQ_SET_ADDRESS:
Robert Baldygad18f71162013-11-21 13:49:18 +01001228 s3c_hsotg_disconnect(hsotg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001229 dcfg = readl(hsotg->regs + DCFG);
1230 dcfg &= ~DCFG_DevAddr_MASK;
1231 dcfg |= ctrl->wValue << DCFG_DevAddr_SHIFT;
1232 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001233
1234 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1235
1236 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1237 return;
1238
1239 case USB_REQ_GET_STATUS:
1240 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1241 break;
1242
1243 case USB_REQ_CLEAR_FEATURE:
1244 case USB_REQ_SET_FEATURE:
1245 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1246 break;
1247 }
1248 }
1249
1250 /* as a fallback, try delivering it to the driver to deal with */
1251
1252 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001253 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001254 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001255 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001256 if (ret < 0)
1257 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1258 }
1259
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001260 /*
1261 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001262 * so respond with a STALL for the status stage to indicate failure.
1263 */
1264
1265 if (ret < 0) {
1266 u32 reg;
1267 u32 ctrl;
1268
1269 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001270 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001271
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001272 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001273 * DxEPCTL_Stall will be cleared by EP once it has
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001274 * taken effect, so no need to clear later.
1275 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001276
1277 ctrl = readl(hsotg->regs + reg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001278 ctrl |= DxEPCTL_Stall;
1279 ctrl |= DxEPCTL_CNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001280 writel(ctrl, hsotg->regs + reg);
1281
1282 dev_dbg(hsotg->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001283 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001284 ctrl, reg, readl(hsotg->regs + reg));
1285
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001286 /*
1287 * don't believe we need to anything more to get the EP
1288 * to reply with a STALL packet
1289 */
Robert Baldygaab93e012013-09-19 11:50:17 +02001290
1291 /*
1292 * complete won't be called, so we enqueue
1293 * setup request here
1294 */
1295 s3c_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001296 }
1297}
1298
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001299/**
1300 * s3c_hsotg_complete_setup - completion of a setup transfer
1301 * @ep: The endpoint the request was on.
1302 * @req: The request completed.
1303 *
1304 * Called on completion of any requests the driver itself submitted for
1305 * EP0 setup packets
1306 */
1307static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1308 struct usb_request *req)
1309{
1310 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1311 struct s3c_hsotg *hsotg = hs_ep->parent;
1312
1313 if (req->status < 0) {
1314 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1315 return;
1316 }
1317
Robert Baldyga93f599f2013-11-21 13:49:17 +01001318 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001319 if (req->actual == 0)
1320 s3c_hsotg_enqueue_setup(hsotg);
1321 else
1322 s3c_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001323 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001324}
1325
1326/**
1327 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1328 * @hsotg: The device state.
1329 *
1330 * Enqueue a request on EP0 if necessary to received any SETUP packets
1331 * received from the host.
1332 */
1333static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1334{
1335 struct usb_request *req = hsotg->ctrl_req;
1336 struct s3c_hsotg_req *hs_req = our_req(req);
1337 int ret;
1338
1339 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1340
1341 req->zero = 0;
1342 req->length = 8;
1343 req->buf = hsotg->ctrl_buff;
1344 req->complete = s3c_hsotg_complete_setup;
1345
1346 if (!list_empty(&hs_req->queue)) {
1347 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1348 return;
1349 }
1350
1351 hsotg->eps[0].dir_in = 0;
1352
1353 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1354 if (ret < 0) {
1355 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001356 /*
1357 * Don't think there's much we can do other than watch the
1358 * driver fail.
1359 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001360 }
1361}
1362
1363/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001364 * s3c_hsotg_complete_request - complete a request given to us
1365 * @hsotg: The device state.
1366 * @hs_ep: The endpoint the request was on.
1367 * @hs_req: The request to complete.
1368 * @result: The result code (0 => Ok, otherwise errno)
1369 *
1370 * The given request has finished, so call the necessary completion
1371 * if it has one and then look to see if we can start a new request
1372 * on the endpoint.
1373 *
1374 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001375 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001376static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1377 struct s3c_hsotg_ep *hs_ep,
1378 struct s3c_hsotg_req *hs_req,
1379 int result)
1380{
1381 bool restart;
1382
1383 if (!hs_req) {
1384 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1385 return;
1386 }
1387
1388 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1389 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1390
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001391 /*
1392 * only replace the status if we've not already set an error
1393 * from a previous transaction
1394 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001395
1396 if (hs_req->req.status == -EINPROGRESS)
1397 hs_req->req.status = result;
1398
1399 hs_ep->req = NULL;
1400 list_del_init(&hs_req->queue);
1401
1402 if (using_dma(hsotg))
1403 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1404
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001405 /*
1406 * call the complete request with the locks off, just in case the
1407 * request tries to queue more work for this endpoint.
1408 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001409
1410 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001411 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001412 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001413 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001414 }
1415
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001416 /*
1417 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001418 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001419 * so be careful when doing this.
1420 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001421
1422 if (!hs_ep->req && result >= 0) {
1423 restart = !list_empty(&hs_ep->queue);
1424 if (restart) {
1425 hs_req = get_ep_head(hs_ep);
1426 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1427 }
1428 }
1429}
1430
1431/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001432 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1433 * @hsotg: The device state.
1434 * @ep_idx: The endpoint index for the data
1435 * @size: The size of data in the fifo, in bytes
1436 *
1437 * The FIFO status shows there is data to read from the FIFO for a given
1438 * endpoint, so sort out whether we need to read the data into a request
1439 * that has been made for that endpoint.
1440 */
1441static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1442{
1443 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1444 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001445 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001446 int to_read;
1447 int max_req;
1448 int read_ptr;
1449
Lukasz Majewski22258f42012-06-14 10:02:24 +02001450
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001451 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001452 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001453 int ptr;
1454
1455 dev_warn(hsotg->dev,
1456 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1457 __func__, size, ep_idx, epctl);
1458
1459 /* dump the data from the FIFO, we've nothing we can do */
1460 for (ptr = 0; ptr < size; ptr += 4)
1461 (void)readl(fifo);
1462
1463 return;
1464 }
1465
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001466 to_read = size;
1467 read_ptr = hs_req->req.actual;
1468 max_req = hs_req->req.length - read_ptr;
1469
Ben Dooksa33e7132010-07-19 09:40:49 +01001470 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1471 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1472
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001473 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001474 /*
1475 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001476 * to deal with in this request.
1477 */
1478
1479 /* currently we don't deal this */
1480 WARN_ON_ONCE(1);
1481 }
1482
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001483 hs_ep->total_data += to_read;
1484 hs_req->req.actual += to_read;
1485 to_read = DIV_ROUND_UP(to_read, 4);
1486
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001487 /*
1488 * note, we might over-write the buffer end by 3 bytes depending on
1489 * alignment of the data.
1490 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001491 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001492}
1493
1494/**
1495 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1496 * @hsotg: The device instance
1497 * @req: The request currently on this endpoint
1498 *
1499 * Generate a zero-length IN packet request for terminating a SETUP
1500 * transaction.
1501 *
1502 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001503 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001504 * the TxFIFO.
1505 */
1506static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1507 struct s3c_hsotg_req *req)
1508{
1509 u32 ctrl;
1510
1511 if (!req) {
1512 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1513 return;
1514 }
1515
1516 if (req->req.length == 0) {
1517 hsotg->eps[0].sent_zlp = 1;
1518 s3c_hsotg_enqueue_setup(hsotg);
1519 return;
1520 }
1521
1522 hsotg->eps[0].dir_in = 1;
1523 hsotg->eps[0].sent_zlp = 1;
1524
1525 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1526
1527 /* issue a zero-sized packet to terminate this */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001528 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
1529 DxEPTSIZ_XferSize(0), hsotg->regs + DIEPTSIZ(0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001530
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001531 ctrl = readl(hsotg->regs + DIEPCTL0);
1532 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
1533 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
1534 ctrl |= DxEPCTL_USBActEp;
1535 writel(ctrl, hsotg->regs + DIEPCTL0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001536}
1537
1538/**
1539 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1540 * @hsotg: The device instance
1541 * @epnum: The endpoint received from
1542 * @was_setup: Set if processing a SetupDone event.
1543 *
1544 * The RXFIFO has delivered an OutDone event, which means that the data
1545 * transfer for an OUT endpoint has been completed, either by a short
1546 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001547 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001548static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1549 int epnum, bool was_setup)
1550{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001551 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001552 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1553 struct s3c_hsotg_req *hs_req = hs_ep->req;
1554 struct usb_request *req = &hs_req->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001555 unsigned size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001556 int result = 0;
1557
1558 if (!hs_req) {
1559 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1560 return;
1561 }
1562
1563 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001564 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001565
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001566 /*
1567 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001568 * is left in the endpoint size register and then working it
1569 * out from the amount we loaded for the transfer.
1570 *
1571 * We need to do this as DMA pointers are always 32bit aligned
1572 * so may overshoot/undershoot the transfer.
1573 */
1574
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001575 size_done = hs_ep->size_loaded - size_left;
1576 size_done += hs_ep->last_load;
1577
1578 req->actual = size_done;
1579 }
1580
Ben Dooksa33e7132010-07-19 09:40:49 +01001581 /* if there is more request to do, schedule new transfer */
1582 if (req->actual < req->length && size_left == 0) {
1583 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1584 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001585 } else if (epnum == 0) {
1586 /*
1587 * After was_setup = 1 =>
1588 * set CNAK for non Setup requests
1589 */
1590 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001591 }
1592
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001593 if (req->actual < req->length && req->short_not_ok) {
1594 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1595 __func__, req->actual, req->length);
1596
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001597 /*
1598 * todo - what should we return here? there's no one else
1599 * even bothering to check the status.
1600 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001601 }
1602
1603 if (epnum == 0) {
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001604 /*
1605 * Condition req->complete != s3c_hsotg_complete_setup says:
1606 * send ZLP when we have an asynchronous request from gadget
1607 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001608 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1609 s3c_hsotg_send_zlp(hsotg, hs_req);
1610 }
1611
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001612 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001613}
1614
1615/**
1616 * s3c_hsotg_read_frameno - read current frame number
1617 * @hsotg: The device instance
1618 *
1619 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001620 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001621static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1622{
1623 u32 dsts;
1624
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001625 dsts = readl(hsotg->regs + DSTS);
1626 dsts &= DSTS_SOFFN_MASK;
1627 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001628
1629 return dsts;
1630}
1631
1632/**
1633 * s3c_hsotg_handle_rx - RX FIFO has data
1634 * @hsotg: The device instance
1635 *
1636 * The IRQ handler has detected that the RX FIFO has some data in it
1637 * that requires processing, so find out what is in there and do the
1638 * appropriate read.
1639 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001640 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001641 * chunks, so if you have x packets received on an endpoint you'll get x
1642 * FIFO events delivered, each with a packet's worth of data in it.
1643 *
1644 * When using DMA, we should not be processing events from the RXFIFO
1645 * as the actual data should be sent to the memory directly and we turn
1646 * on the completion interrupts to get notifications of transfer completion.
1647 */
Mark Brown0978f8c2010-01-18 13:18:35 +00001648static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001649{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001650 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001651 u32 epnum, status, size;
1652
1653 WARN_ON(using_dma(hsotg));
1654
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001655 epnum = grxstsr & GRXSTS_EPNum_MASK;
1656 status = grxstsr & GRXSTS_PktSts_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001657
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001658 size = grxstsr & GRXSTS_ByteCnt_MASK;
1659 size >>= GRXSTS_ByteCnt_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001660
1661 if (1)
1662 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1663 __func__, grxstsr, size, epnum);
1664
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001665#define __status(x) ((x) >> GRXSTS_PktSts_SHIFT)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001666
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001667 switch (status >> GRXSTS_PktSts_SHIFT) {
1668 case __status(GRXSTS_PktSts_GlobalOutNAK):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001669 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1670 break;
1671
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001672 case __status(GRXSTS_PktSts_OutDone):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001673 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1674 s3c_hsotg_read_frameno(hsotg));
1675
1676 if (!using_dma(hsotg))
1677 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1678 break;
1679
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001680 case __status(GRXSTS_PktSts_SetupDone):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001681 dev_dbg(hsotg->dev,
1682 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1683 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001684 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001685
1686 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1687 break;
1688
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001689 case __status(GRXSTS_PktSts_OutRX):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001690 s3c_hsotg_rx_data(hsotg, epnum, size);
1691 break;
1692
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001693 case __status(GRXSTS_PktSts_SetupRX):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001694 dev_dbg(hsotg->dev,
1695 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1696 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001697 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001698
1699 s3c_hsotg_rx_data(hsotg, epnum, size);
1700 break;
1701
1702 default:
1703 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1704 __func__, grxstsr);
1705
1706 s3c_hsotg_dump(hsotg);
1707 break;
1708 }
1709}
1710
1711/**
1712 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1713 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001714 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001715static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1716{
1717 switch (mps) {
1718 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001719 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001720 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001721 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001722 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001723 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001724 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001725 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001726 }
1727
1728 /* bad max packet size, warn and return invalid result */
1729 WARN_ON(1);
1730 return (u32)-1;
1731}
1732
1733/**
1734 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1735 * @hsotg: The driver state.
1736 * @ep: The index number of the endpoint
1737 * @mps: The maximum packet size in bytes
1738 *
1739 * Configure the maximum packet size for the given endpoint, updating
1740 * the hardware control registers to reflect this.
1741 */
1742static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1743 unsigned int ep, unsigned int mps)
1744{
1745 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1746 void __iomem *regs = hsotg->regs;
1747 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001748 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001749 u32 reg;
1750
1751 if (ep == 0) {
1752 /* EP0 is a special case */
1753 mpsval = s3c_hsotg_ep0_mps(mps);
1754 if (mpsval > 3)
1755 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001756 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001757 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001758 } else {
Robert Baldygae9edd1992013-10-09 08:20:02 +02001759 mpsval = mps & DxEPCTL_MPS_MASK;
1760 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001761 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001762 mcval = ((mps >> 11) & 0x3) + 1;
1763 hs_ep->mc = mcval;
1764 if (mcval > 3)
1765 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001766 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001767 }
1768
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001769 /*
1770 * update both the in and out endpoint controldir_ registers, even
1771 * if one of the directions may not be in use.
1772 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001773
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001774 reg = readl(regs + DIEPCTL(ep));
1775 reg &= ~DxEPCTL_MPS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001776 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001777 writel(reg, regs + DIEPCTL(ep));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001778
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001779 if (ep) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001780 reg = readl(regs + DOEPCTL(ep));
1781 reg &= ~DxEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001782 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001783 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001784 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001785
1786 return;
1787
1788bad_mps:
1789 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1790}
1791
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001792/**
1793 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1794 * @hsotg: The driver state
1795 * @idx: The index for the endpoint (0..15)
1796 */
1797static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1798{
1799 int timeout;
1800 int val;
1801
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001802 writel(GRSTCTL_TxFNum(idx) | GRSTCTL_TxFFlsh,
1803 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001804
1805 /* wait until the fifo is flushed */
1806 timeout = 100;
1807
1808 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001809 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001810
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001811 if ((val & (GRSTCTL_TxFFlsh)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001812 break;
1813
1814 if (--timeout == 0) {
1815 dev_err(hsotg->dev,
1816 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1817 __func__, val);
1818 }
1819
1820 udelay(1);
1821 }
1822}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001823
1824/**
1825 * s3c_hsotg_trytx - check to see if anything needs transmitting
1826 * @hsotg: The driver state
1827 * @hs_ep: The driver endpoint to check.
1828 *
1829 * Check to see if there is a request that has data to send, and if so
1830 * make an attempt to write data into the FIFO.
1831 */
1832static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1833 struct s3c_hsotg_ep *hs_ep)
1834{
1835 struct s3c_hsotg_req *hs_req = hs_ep->req;
1836
Robert Baldygaafcf4162013-09-19 11:50:19 +02001837 if (!hs_ep->dir_in || !hs_req) {
1838 /**
1839 * if request is not enqueued, we disable interrupts
1840 * for endpoints, excepting ep0
1841 */
1842 if (hs_ep->index != 0)
1843 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1844 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001845 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001846 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001847
1848 if (hs_req->req.actual < hs_req->req.length) {
1849 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1850 hs_ep->index);
1851 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1852 }
1853
1854 return 0;
1855}
1856
1857/**
1858 * s3c_hsotg_complete_in - complete IN transfer
1859 * @hsotg: The device state.
1860 * @hs_ep: The endpoint that has just completed.
1861 *
1862 * An IN transfer has been completed, update the transfer's state and then
1863 * call the relevant completion routines.
1864 */
1865static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1866 struct s3c_hsotg_ep *hs_ep)
1867{
1868 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001869 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001870 int size_left, size_done;
1871
1872 if (!hs_req) {
1873 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1874 return;
1875 }
1876
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001877 /* Finish ZLP handling for IN EP0 transactions */
1878 if (hsotg->eps[0].sent_zlp) {
1879 dev_dbg(hsotg->dev, "zlp packet received\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001880 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001881 return;
1882 }
1883
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001884 /*
1885 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001886 * in the endpoint size register and then working it out from
1887 * the amount we loaded for the transfer.
1888 *
1889 * We do this even for DMA, as the transfer may have incremented
1890 * past the end of the buffer (DMA transfers are always 32bit
1891 * aligned).
1892 */
1893
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001894 size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001895
1896 size_done = hs_ep->size_loaded - size_left;
1897 size_done += hs_ep->last_load;
1898
1899 if (hs_req->req.actual != size_done)
1900 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1901 __func__, hs_req->req.actual, size_done);
1902
1903 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001904 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1905 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001906
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001907 /*
1908 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1909 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1910 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1911 * inform the host that no more data is available.
1912 * The state of req.zero member is checked to be sure that the value to
1913 * send is smaller than wValue expected from host.
1914 * Check req.length to NOT send another ZLP when the current one is
1915 * under completion (the one for which this completion has been called).
1916 */
1917 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1918 hs_req->req.length == hs_req->req.actual &&
1919 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1920
1921 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1922 s3c_hsotg_send_zlp(hsotg, hs_req);
1923
1924 return;
1925 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001926
1927 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1928 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1929 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1930 } else
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001931 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001932}
1933
1934/**
1935 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1936 * @hsotg: The driver state
1937 * @idx: The index for the endpoint (0..15)
1938 * @dir_in: Set if this is an IN endpoint
1939 *
1940 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001941 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001942static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1943 int dir_in)
1944{
1945 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001946 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1947 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1948 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001949 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02001950 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001951
1952 ints = readl(hsotg->regs + epint_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02001953 ctrl = readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001954
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001955 /* Clear endpoint interrupts */
1956 writel(ints, hsotg->regs + epint_reg);
1957
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001958 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1959 __func__, idx, dir_in ? "in" : "out", ints);
1960
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001961 if (ints & DxEPINT_XferCompl) {
Robert Baldyga1479e842013-10-09 08:41:57 +02001962 if (hs_ep->isochronous && hs_ep->interval == 1) {
1963 if (ctrl & DxEPCTL_EOFrNum)
1964 ctrl |= DxEPCTL_SetEvenFr;
1965 else
1966 ctrl |= DxEPCTL_SetOddFr;
1967 writel(ctrl, hsotg->regs + epctl_reg);
1968 }
1969
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001970 dev_dbg(hsotg->dev,
1971 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1972 __func__, readl(hsotg->regs + epctl_reg),
1973 readl(hsotg->regs + epsiz_reg));
1974
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001975 /*
1976 * we get OutDone from the FIFO, so we only need to look
1977 * at completing IN requests here
1978 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001979 if (dir_in) {
1980 s3c_hsotg_complete_in(hsotg, hs_ep);
1981
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001982 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001983 s3c_hsotg_enqueue_setup(hsotg);
1984 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001985 /*
1986 * We're using DMA, we need to fire an OutDone here
1987 * as we ignore the RXFIFO.
1988 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001989
1990 s3c_hsotg_handle_outdone(hsotg, idx, false);
1991 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001992 }
1993
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001994 if (ints & DxEPINT_EPDisbld) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001995 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001997 if (dir_in) {
1998 int epctl = readl(hsotg->regs + epctl_reg);
1999
2000 s3c_hsotg_txfifo_flush(hsotg, idx);
2001
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002002 if ((epctl & DxEPCTL_Stall) &&
2003 (epctl & DxEPCTL_EPType_Bulk)) {
2004 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002005
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002006 dctl |= DCTL_CGNPInNAK;
2007 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002008 }
2009 }
2010 }
2011
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002012 if (ints & DxEPINT_AHBErr)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002013 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002014
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002015 if (ints & DxEPINT_Setup) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002016 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2017
2018 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002019 /*
2020 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002021 * setup packet. In non-DMA mode we'd get this
2022 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002023 * the setup here.
2024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002025
2026 if (dir_in)
2027 WARN_ON_ONCE(1);
2028 else
2029 s3c_hsotg_handle_outdone(hsotg, 0, true);
2030 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002031 }
2032
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002033 if (ints & DxEPINT_Back2BackSetup)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002034 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002035
Robert Baldyga1479e842013-10-09 08:41:57 +02002036 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002037 /* not sure if this is important, but we'll clear it anyway */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002038 if (ints & DIEPMSK_INTknTXFEmpMsk) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002039 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2040 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002041 }
2042
2043 /* this probably means something bad is happening */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002044 if (ints & DIEPMSK_INTknEPMisMsk) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002045 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2046 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002047 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002048
2049 /* FIFO has space or is empty (see GAHBCFG) */
2050 if (hsotg->dedicated_fifos &&
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002051 ints & DIEPMSK_TxFIFOEmpty) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002052 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2053 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002054 if (!using_dma(hsotg))
2055 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002056 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002057 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002058}
2059
2060/**
2061 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
2062 * @hsotg: The device state.
2063 *
2064 * Handle updating the device settings after the enumeration phase has
2065 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002066 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002067static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
2068{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002069 u32 dsts = readl(hsotg->regs + DSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002070 int ep0_mps = 0, ep_mps;
2071
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002072 /*
2073 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002074 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002075 * we connected at.
2076 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002077
2078 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2079
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002080 /*
2081 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002082 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002083 * not advertise a 64byte MPS on EP0.
2084 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002085
2086 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002087 switch (dsts & DSTS_EnumSpd_MASK) {
2088 case DSTS_EnumSpd_FS:
2089 case DSTS_EnumSpd_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002090 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002091 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002092 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002093 break;
2094
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002095 case DSTS_EnumSpd_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002096 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002097 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002098 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002099 break;
2100
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002101 case DSTS_EnumSpd_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002102 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002103 /*
2104 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002105 * moment, and the documentation seems to imply that it isn't
2106 * supported by the PHYs on some of the devices.
2107 */
2108 break;
2109 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002110 dev_info(hsotg->dev, "new device is %s\n",
2111 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002112
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002113 /*
2114 * we should now know the maximum packet size for an
2115 * endpoint, so set the endpoints to a default value.
2116 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002117
2118 if (ep0_mps) {
2119 int i;
2120 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002121 for (i = 1; i < hsotg->num_of_eps; i++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002122 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
2123 }
2124
2125 /* ensure after enumeration our EP0 is active */
2126
2127 s3c_hsotg_enqueue_setup(hsotg);
2128
2129 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002130 readl(hsotg->regs + DIEPCTL0),
2131 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002132}
2133
2134/**
2135 * kill_all_requests - remove all requests from the endpoint's queue
2136 * @hsotg: The device state.
2137 * @ep: The endpoint the requests may be on.
2138 * @result: The result code to use.
2139 * @force: Force removal of any current requests
2140 *
2141 * Go through the requests on the given endpoint and mark them
2142 * completed with the given result code.
2143 */
2144static void kill_all_requests(struct s3c_hsotg *hsotg,
2145 struct s3c_hsotg_ep *ep,
2146 int result, bool force)
2147{
2148 struct s3c_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002149
2150 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002151 /*
2152 * currently, we can't do much about an already
2153 * running request on an in endpoint
2154 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002155
2156 if (ep->req == req && ep->dir_in && !force)
2157 continue;
2158
2159 s3c_hsotg_complete_request(hsotg, ep, req,
2160 result);
2161 }
Robert Baldygab963a812013-12-06 13:03:45 +01002162 if(hsotg->dedicated_fifos)
2163 if ((readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4 < 3072)
2164 s3c_hsotg_txfifo_flush(hsotg, ep->index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002165}
2166
2167#define call_gadget(_hs, _entry) \
Pavel Macheka023da32013-09-30 14:56:02 +02002168do { \
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002169 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002170 (_hs)->driver && (_hs)->driver->_entry) { \
2171 spin_unlock(&_hs->lock); \
2172 (_hs)->driver->_entry(&(_hs)->gadget); \
2173 spin_lock(&_hs->lock); \
Pavel Macheka023da32013-09-30 14:56:02 +02002174 } \
2175} while (0)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002176
2177/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002178 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002179 * @hsotg: The device state.
2180 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002181 * The device has been disconnected. Remove all current
2182 * transactions and signal the gadget driver that this
2183 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002184 */
Lukasz Majewski5e891342012-05-04 14:17:07 +02002185static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002186{
2187 unsigned ep;
2188
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002189 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002190 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2191
2192 call_gadget(hsotg, disconnect);
2193}
2194
2195/**
2196 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2197 * @hsotg: The device state:
2198 * @periodic: True if this is a periodic FIFO interrupt
2199 */
2200static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2201{
2202 struct s3c_hsotg_ep *ep;
2203 int epno, ret;
2204
2205 /* look through for any more data to transmit */
2206
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002207 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002208 ep = &hsotg->eps[epno];
2209
2210 if (!ep->dir_in)
2211 continue;
2212
2213 if ((periodic && !ep->periodic) ||
2214 (!periodic && ep->periodic))
2215 continue;
2216
2217 ret = s3c_hsotg_trytx(hsotg, ep);
2218 if (ret < 0)
2219 break;
2220 }
2221}
2222
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002223/* IRQ flags which will trigger a retry around the IRQ loop */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002224#define IRQ_RETRY_MASK (GINTSTS_NPTxFEmp | \
2225 GINTSTS_PTxFEmp | \
2226 GINTSTS_RxFLvl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002227
2228/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002229 * s3c_hsotg_corereset - issue softreset to the core
2230 * @hsotg: The device state
2231 *
2232 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002233 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002234static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2235{
2236 int timeout;
2237 u32 grstctl;
2238
2239 dev_dbg(hsotg->dev, "resetting core\n");
2240
2241 /* issue soft reset */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002242 writel(GRSTCTL_CSftRst, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002243
Du, Changbin2868fea2012-07-24 08:19:25 +08002244 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002245 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002246 grstctl = readl(hsotg->regs + GRSTCTL);
2247 } while ((grstctl & GRSTCTL_CSftRst) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002248
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002249 if (grstctl & GRSTCTL_CSftRst) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002250 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2251 return -EINVAL;
2252 }
2253
Du, Changbin2868fea2012-07-24 08:19:25 +08002254 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002255
2256 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002257 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002258
2259 if (timeout-- < 0) {
2260 dev_info(hsotg->dev,
2261 "%s: reset failed, GRSTCTL=%08x\n",
2262 __func__, grstctl);
2263 return -ETIMEDOUT;
2264 }
2265
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002266 if (!(grstctl & GRSTCTL_AHBIdle))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002267 continue;
2268
2269 break; /* reset done */
2270 }
2271
2272 dev_dbg(hsotg->dev, "reset successful\n");
2273 return 0;
2274}
2275
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002276/**
2277 * s3c_hsotg_core_init - issue softreset to the core
2278 * @hsotg: The device state
2279 *
2280 * Issue a soft reset to the core, and await the core finishing it.
2281 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002282static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2283{
2284 s3c_hsotg_corereset(hsotg);
2285
2286 /*
2287 * we must now enable ep0 ready for host detection and then
2288 * set configuration.
2289 */
2290
2291 /* set the PLL on, remove the HNP/SRP and set the PHY */
Matt Porterf7e504c2013-12-19 09:23:07 -05002292 writel(hsotg->phyif | GUSBCFG_TOutCal(7) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002293 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002294
2295 s3c_hsotg_init_fifo(hsotg);
2296
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002297 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002298
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002299 writel(1 << 18 | DCFG_DevSpd_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002300
2301 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002302 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002303
2304 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002305 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002306
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002307 writel(GINTSTS_ErlySusp | GINTSTS_SessReqInt |
2308 GINTSTS_GOUTNakEff | GINTSTS_GINNakEff |
2309 GINTSTS_ConIDStsChng | GINTSTS_USBRst |
2310 GINTSTS_EnumDone | GINTSTS_OTGInt |
2311 GINTSTS_USBSusp | GINTSTS_WkUpInt,
2312 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002313
2314 if (using_dma(hsotg))
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002315 writel(GAHBCFG_GlblIntrEn | GAHBCFG_DMAEn |
2316 GAHBCFG_HBstLen_Incr4,
2317 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002318 else
Robert Baldyga8acc8292013-09-19 11:50:23 +02002319 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NPTxFEmpLvl |
2320 GAHBCFG_PTxFEmpLvl) : 0) |
2321 GAHBCFG_GlblIntrEn,
2322 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002323
2324 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002325 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2326 * when we have no data to transfer. Otherwise we get being flooded by
2327 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002328 */
2329
Robert Baldyga8acc8292013-09-19 11:50:23 +02002330 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TxFIFOEmpty |
2331 DIEPMSK_INTknTXFEmpMsk : 0) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002332 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk |
2333 DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2334 DIEPMSK_INTknEPMisMsk,
2335 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002336
2337 /*
2338 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2339 * DMA mode we may need this.
2340 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002341 writel((using_dma(hsotg) ? (DIEPMSK_XferComplMsk |
2342 DIEPMSK_TimeOUTMsk) : 0) |
2343 DOEPMSK_EPDisbldMsk | DOEPMSK_AHBErrMsk |
2344 DOEPMSK_SetupMsk,
2345 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002346
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002347 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002348
2349 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002350 readl(hsotg->regs + DIEPCTL0),
2351 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002352
2353 /* enable in and out endpoint interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002354 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPInt | GINTSTS_IEPInt);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002355
2356 /*
2357 * Enable the RXFIFO when in slave mode, as this is how we collect
2358 * the data. In DMA mode, we get events from the FIFO but also
2359 * things we cannot process, so do not use it.
2360 */
2361 if (!using_dma(hsotg))
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002362 s3c_hsotg_en_gsint(hsotg, GINTSTS_RxFLvl);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002363
2364 /* Enable interrupts for EP0 in and out */
2365 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2366 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2367
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002368 __orr32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002369 udelay(10); /* see openiboot */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002370 __bic32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002371
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002372 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002373
2374 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002375 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002376 * writing to the EPCTL register..
2377 */
2378
2379 /* set to read 1 8byte packet */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002380 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
2381 DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002382
2383 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002384 DxEPCTL_CNAK | DxEPCTL_EPEna |
2385 DxEPCTL_USBActEp,
2386 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002387
2388 /* enable, but don't activate EP0in */
2389 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002390 DxEPCTL_USBActEp, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002391
2392 s3c_hsotg_enqueue_setup(hsotg);
2393
2394 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002395 readl(hsotg->regs + DIEPCTL0),
2396 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002397
2398 /* clear global NAKs */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002399 writel(DCTL_CGOUTNak | DCTL_CGNPInNAK,
2400 hsotg->regs + DCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002401
2402 /* must be at-least 3ms to allow bus to see disconnect */
2403 mdelay(3);
2404
2405 /* remove the soft-disconnect and let's go */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002406 __bic32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002407}
2408
2409/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002410 * s3c_hsotg_irq - handle device interrupt
2411 * @irq: The IRQ number triggered
2412 * @pw: The pw value when registered the handler.
2413 */
2414static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2415{
2416 struct s3c_hsotg *hsotg = pw;
2417 int retry_count = 8;
2418 u32 gintsts;
2419 u32 gintmsk;
2420
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002421 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002422irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002423 gintsts = readl(hsotg->regs + GINTSTS);
2424 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002425
2426 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2427 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2428
2429 gintsts &= gintmsk;
2430
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002431 if (gintsts & GINTSTS_OTGInt) {
2432 u32 otgint = readl(hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002433
2434 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2435
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002436 writel(otgint, hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002437 }
2438
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002439 if (gintsts & GINTSTS_SessReqInt) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002440 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002441 writel(GINTSTS_SessReqInt, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002442 }
2443
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002444 if (gintsts & GINTSTS_EnumDone) {
2445 writel(GINTSTS_EnumDone, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002446
2447 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002448 }
2449
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002450 if (gintsts & GINTSTS_ConIDStsChng) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002451 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002452 readl(hsotg->regs + DSTS),
2453 readl(hsotg->regs + GOTGCTL));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002454
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002455 writel(GINTSTS_ConIDStsChng, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002456 }
2457
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002458 if (gintsts & (GINTSTS_OEPInt | GINTSTS_IEPInt)) {
2459 u32 daint = readl(hsotg->regs + DAINT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002460 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2461 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002462 int ep;
2463
Robert Baldyga7e804652013-09-19 11:50:20 +02002464 daint &= daintmsk;
2465 daint_out = daint >> DAINT_OutEP_SHIFT;
2466 daint_in = daint & ~(daint_out << DAINT_OutEP_SHIFT);
2467
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002468 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2469
2470 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2471 if (daint_out & 1)
2472 s3c_hsotg_epint(hsotg, ep, 0);
2473 }
2474
2475 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2476 if (daint_in & 1)
2477 s3c_hsotg_epint(hsotg, ep, 1);
2478 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002479 }
2480
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002481 if (gintsts & GINTSTS_USBRst) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002482
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002483 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002484
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002485 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2486 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002487 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002488
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002489 writel(GINTSTS_USBRst, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002490
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002491 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002492 if (time_after(jiffies, hsotg->last_rst +
2493 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002494
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002495 kill_all_requests(hsotg, &hsotg->eps[0],
2496 -ECONNRESET, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002497
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002498 s3c_hsotg_core_init(hsotg);
2499 hsotg->last_rst = jiffies;
2500 }
2501 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002502 }
2503
2504 /* check both FIFOs */
2505
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002506 if (gintsts & GINTSTS_NPTxFEmp) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002507 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2508
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002509 /*
2510 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002511 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002512 * it needs re-enabling
2513 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002514
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002515 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002517 }
2518
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002519 if (gintsts & GINTSTS_PTxFEmp) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520 dev_dbg(hsotg->dev, "PTxFEmp\n");
2521
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002522 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002523
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002524 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002525 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002526 }
2527
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002528 if (gintsts & GINTSTS_RxFLvl) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002529 /*
2530 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002531 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002532 * set.
2533 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002534
2535 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002536 }
2537
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002538 if (gintsts & GINTSTS_ModeMis) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002539 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002540 writel(GINTSTS_ModeMis, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002541 }
2542
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002543 if (gintsts & GINTSTS_USBSusp) {
2544 dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
2545 writel(GINTSTS_USBSusp, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002546
2547 call_gadget(hsotg, suspend);
2548 }
2549
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002550 if (gintsts & GINTSTS_WkUpInt) {
2551 dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
2552 writel(GINTSTS_WkUpInt, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002553
2554 call_gadget(hsotg, resume);
2555 }
2556
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002557 if (gintsts & GINTSTS_ErlySusp) {
2558 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
2559 writel(GINTSTS_ErlySusp, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002560 }
2561
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002562 /*
2563 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002564 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002565 * the occurrence.
2566 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002567
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002568 if (gintsts & GINTSTS_GOUTNakEff) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002569 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2570
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002571 writel(DCTL_CGOUTNak, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002572
2573 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002574 }
2575
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002576 if (gintsts & GINTSTS_GINNakEff) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002577 dev_info(hsotg->dev, "GINNakEff triggered\n");
2578
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002579 writel(DCTL_CGNPInNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002580
2581 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002582 }
2583
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002584 /*
2585 * if we've had fifo events, we should try and go around the
2586 * loop again to see if there's any point in returning yet.
2587 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002588
2589 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2590 goto irq_retry;
2591
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002592 spin_unlock(&hsotg->lock);
2593
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002594 return IRQ_HANDLED;
2595}
2596
2597/**
2598 * s3c_hsotg_ep_enable - enable the given endpoint
2599 * @ep: The USB endpint to configure
2600 * @desc: The USB endpoint descriptor to configure with.
2601 *
2602 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002603 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002604static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2605 const struct usb_endpoint_descriptor *desc)
2606{
2607 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2608 struct s3c_hsotg *hsotg = hs_ep->parent;
2609 unsigned long flags;
2610 int index = hs_ep->index;
2611 u32 epctrl_reg;
2612 u32 epctrl;
2613 u32 mps;
2614 int dir_in;
Julia Lawall19c190f2010-03-29 17:36:44 +02002615 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002616
2617 dev_dbg(hsotg->dev,
2618 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2619 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2620 desc->wMaxPacketSize, desc->bInterval);
2621
2622 /* not to be called for EP0 */
2623 WARN_ON(index == 0);
2624
2625 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2626 if (dir_in != hs_ep->dir_in) {
2627 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2628 return -EINVAL;
2629 }
2630
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002631 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002632
2633 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2634
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002635 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002636 epctrl = readl(hsotg->regs + epctrl_reg);
2637
2638 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2639 __func__, epctrl, epctrl_reg);
2640
Lukasz Majewski22258f42012-06-14 10:02:24 +02002641 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002642
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002643 epctrl &= ~(DxEPCTL_EPType_MASK | DxEPCTL_MPS_MASK);
2644 epctrl |= DxEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002645
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002646 /*
2647 * mark the endpoint as active, otherwise the core may ignore
2648 * transactions entirely for this endpoint
2649 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002650 epctrl |= DxEPCTL_USBActEp;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002651
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002652 /*
2653 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002654 * do something with data that we've yet got a request to process
2655 * since the RXFIFO will take data for an endpoint even if the
2656 * size register hasn't been set.
2657 */
2658
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002659 epctrl |= DxEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002660
2661 /* update the endpoint state */
Robert Baldygae9edd1992013-10-09 08:20:02 +02002662 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002663
2664 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002665 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002666 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002667 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002668 hs_ep->interval = desc->bInterval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002669
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002670 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2671 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2672
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002673 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2674 case USB_ENDPOINT_XFER_ISOC:
Robert Baldyga1479e842013-10-09 08:41:57 +02002675 epctrl |= DxEPCTL_EPType_Iso;
2676 epctrl |= DxEPCTL_SetEvenFr;
2677 hs_ep->isochronous = 1;
2678 if (dir_in)
2679 hs_ep->periodic = 1;
2680 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002681
2682 case USB_ENDPOINT_XFER_BULK:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002683 epctrl |= DxEPCTL_EPType_Bulk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002684 break;
2685
2686 case USB_ENDPOINT_XFER_INT:
2687 if (dir_in) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002688 /*
2689 * Allocate our TxFNum by simply using the index
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002690 * of the endpoint for the moment. We could do
2691 * something better if the host indicates how
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002692 * many FIFOs we are expecting to use.
2693 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002694
2695 hs_ep->periodic = 1;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002696 epctrl |= DxEPCTL_TxFNum(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002697 }
2698
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002699 epctrl |= DxEPCTL_EPType_Intterupt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002700 break;
2701
2702 case USB_ENDPOINT_XFER_CONTROL:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002703 epctrl |= DxEPCTL_EPType_Control;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002704 break;
2705 }
2706
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002707 /*
2708 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002709 * a unique tx-fifo even if it is non-periodic.
2710 */
2711 if (dir_in && hsotg->dedicated_fifos)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002712 epctrl |= DxEPCTL_TxFNum(index);
Ben Dooks10aebc72010-07-19 09:40:44 +01002713
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002714 /* for non control endpoints, set PID to D0 */
2715 if (index)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002716 epctrl |= DxEPCTL_SetD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002717
2718 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2719 __func__, epctrl);
2720
2721 writel(epctrl, hsotg->regs + epctrl_reg);
2722 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2723 __func__, readl(hsotg->regs + epctrl_reg));
2724
2725 /* enable the endpoint interrupt */
2726 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2727
Lukasz Majewski22258f42012-06-14 10:02:24 +02002728 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002729 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002730}
2731
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002732/**
2733 * s3c_hsotg_ep_disable - disable given endpoint
2734 * @ep: The endpoint to disable.
2735 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002736static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2737{
2738 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2739 struct s3c_hsotg *hsotg = hs_ep->parent;
2740 int dir_in = hs_ep->dir_in;
2741 int index = hs_ep->index;
2742 unsigned long flags;
2743 u32 epctrl_reg;
2744 u32 ctrl;
2745
2746 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2747
2748 if (ep == &hsotg->eps[0].ep) {
2749 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2750 return -EINVAL;
2751 }
2752
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002753 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002754
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002755 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002756 /* terminate all requests with shutdown */
2757 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2758
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002759
2760 ctrl = readl(hsotg->regs + epctrl_reg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002761 ctrl &= ~DxEPCTL_EPEna;
2762 ctrl &= ~DxEPCTL_USBActEp;
2763 ctrl |= DxEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002764
2765 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2766 writel(ctrl, hsotg->regs + epctrl_reg);
2767
2768 /* disable endpoint interrupts */
2769 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2770
Lukasz Majewski22258f42012-06-14 10:02:24 +02002771 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002772 return 0;
2773}
2774
2775/**
2776 * on_list - check request is on the given endpoint
2777 * @ep: The endpoint to check.
2778 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002779 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002780static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2781{
2782 struct s3c_hsotg_req *req, *treq;
2783
2784 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2785 if (req == test)
2786 return true;
2787 }
2788
2789 return false;
2790}
2791
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002792/**
2793 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2794 * @ep: The endpoint to dequeue.
2795 * @req: The request to be removed from a queue.
2796 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002797static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2798{
2799 struct s3c_hsotg_req *hs_req = our_req(req);
2800 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2801 struct s3c_hsotg *hs = hs_ep->parent;
2802 unsigned long flags;
2803
2804 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2805
Lukasz Majewski22258f42012-06-14 10:02:24 +02002806 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002807
2808 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002809 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002810 return -EINVAL;
2811 }
2812
2813 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002814 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002815
2816 return 0;
2817}
2818
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002819/**
2820 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2821 * @ep: The endpoint to set halt.
2822 * @value: Set or unset the halt.
2823 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002824static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2825{
2826 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2827 struct s3c_hsotg *hs = hs_ep->parent;
2828 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002829 u32 epreg;
2830 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002831 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002832
2833 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2834
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002835 /* write both IN and OUT control registers */
2836
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002837 epreg = DIEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002838 epctl = readl(hs->regs + epreg);
2839
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002840 if (value) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002841 epctl |= DxEPCTL_Stall + DxEPCTL_SNAK;
2842 if (epctl & DxEPCTL_EPEna)
2843 epctl |= DxEPCTL_EPDis;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002844 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002845 epctl &= ~DxEPCTL_Stall;
2846 xfertype = epctl & DxEPCTL_EPType_MASK;
2847 if (xfertype == DxEPCTL_EPType_Bulk ||
2848 xfertype == DxEPCTL_EPType_Intterupt)
2849 epctl |= DxEPCTL_SetD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002850 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002851
2852 writel(epctl, hs->regs + epreg);
2853
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002854 epreg = DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002855 epctl = readl(hs->regs + epreg);
2856
2857 if (value)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002858 epctl |= DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002859 else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002860 epctl &= ~DxEPCTL_Stall;
2861 xfertype = epctl & DxEPCTL_EPType_MASK;
2862 if (xfertype == DxEPCTL_EPType_Bulk ||
2863 xfertype == DxEPCTL_EPType_Intterupt)
2864 epctl |= DxEPCTL_SetD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002865 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002866
2867 writel(epctl, hs->regs + epreg);
2868
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002869 hs_ep->halted = value;
2870
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002871 return 0;
2872}
2873
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002874/**
2875 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2876 * @ep: The endpoint to set halt.
2877 * @value: Set or unset the halt.
2878 */
2879static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2880{
2881 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2882 struct s3c_hsotg *hs = hs_ep->parent;
2883 unsigned long flags = 0;
2884 int ret = 0;
2885
2886 spin_lock_irqsave(&hs->lock, flags);
2887 ret = s3c_hsotg_ep_sethalt(ep, value);
2888 spin_unlock_irqrestore(&hs->lock, flags);
2889
2890 return ret;
2891}
2892
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002893static struct usb_ep_ops s3c_hsotg_ep_ops = {
2894 .enable = s3c_hsotg_ep_enable,
2895 .disable = s3c_hsotg_ep_disable,
2896 .alloc_request = s3c_hsotg_ep_alloc_request,
2897 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002898 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002899 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002900 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002901 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002902};
2903
2904/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002905 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002906 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002907 *
2908 * A wrapper for platform code responsible for controlling
2909 * low-level USB code
2910 */
2911static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2912{
2913 struct platform_device *pdev = to_platform_device(hsotg->dev);
2914
2915 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302916
Matt Porter74084842013-12-19 09:23:06 -05002917 if (hsotg->phy) {
2918 phy_init(hsotg->phy);
2919 phy_power_on(hsotg->phy);
2920 } else if (hsotg->uphy)
2921 usb_phy_init(hsotg->uphy);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302922 else if (hsotg->plat->phy_init)
Lukasz Majewski41188782012-05-04 14:17:01 +02002923 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2924}
2925
2926/**
2927 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002928 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002929 *
2930 * A wrapper for platform code responsible for controlling
2931 * low-level USB code
2932 */
2933static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2934{
2935 struct platform_device *pdev = to_platform_device(hsotg->dev);
2936
Matt Porter74084842013-12-19 09:23:06 -05002937 if (hsotg->phy) {
2938 phy_power_off(hsotg->phy);
2939 phy_exit(hsotg->phy);
2940 } else if (hsotg->uphy)
2941 usb_phy_shutdown(hsotg->uphy);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302942 else if (hsotg->plat->phy_exit)
Lukasz Majewski41188782012-05-04 14:17:01 +02002943 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2944}
2945
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002946/**
2947 * s3c_hsotg_init - initalize the usb core
2948 * @hsotg: The driver state
2949 */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002950static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2951{
2952 /* unmask subset of endpoint interrupts */
2953
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002954 writel(DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2955 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk,
2956 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002957
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002958 writel(DOEPMSK_SetupMsk | DOEPMSK_AHBErrMsk |
2959 DOEPMSK_EPDisbldMsk | DOEPMSK_XferComplMsk,
2960 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002961
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002962 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002963
2964 /* Be in disconnected state until gadget is registered */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002965 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002966
2967 if (0) {
2968 /* post global nak until we're ready */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002969 writel(DCTL_SGNPInNAK | DCTL_SGOUTNak,
2970 hsotg->regs + DCTL);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002971 }
2972
2973 /* setup fifos */
2974
2975 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002976 readl(hsotg->regs + GRXFSIZ),
2977 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002978
2979 s3c_hsotg_init_fifo(hsotg);
2980
2981 /* set the PLL on, remove the HNP/SRP and set the PHY */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002982 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | (0x5 << 10),
2983 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002984
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002985 writel(using_dma(hsotg) ? GAHBCFG_DMAEn : 0x0,
2986 hsotg->regs + GAHBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002987}
2988
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002989/**
2990 * s3c_hsotg_udc_start - prepare the udc for work
2991 * @gadget: The usb gadget state
2992 * @driver: The usb gadget driver
2993 *
2994 * Perform initialization to prepare udc device and driver
2995 * to work.
2996 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002997static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2998 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002999{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02003000 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003001 int ret;
3002
3003 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003004 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003005 return -ENODEV;
3006 }
3007
3008 if (!driver) {
3009 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3010 return -EINVAL;
3011 }
3012
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003013 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003014 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003015
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003016 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003017 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3018 return -EINVAL;
3019 }
3020
3021 WARN_ON(hsotg->driver);
3022
3023 driver->driver.bus = NULL;
3024 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003025 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3027
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003028 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3029 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003030 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003031 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003032 goto err;
3033 }
3034
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02003035 hsotg->last_rst = jiffies;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003036 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
3037 return 0;
3038
3039err:
3040 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003041 return ret;
3042}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003043
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003044/**
3045 * s3c_hsotg_udc_stop - stop the udc
3046 * @gadget: The usb gadget state
3047 * @driver: The usb gadget driver
3048 *
3049 * Stop udc hw block and stay tunned for future transmissions
3050 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003051static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
3052 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003053{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02003054 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003055 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003056 int ep;
3057
3058 if (!hsotg)
3059 return -ENODEV;
3060
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003061 /* all endpoints should be shutdown */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003062 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003063 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3064
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003065 spin_lock_irqsave(&hsotg->lock, flags);
3066
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003067 s3c_hsotg_phy_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003068
Marek Szyprowskic8c10252013-09-12 16:18:48 +02003069 if (!driver)
3070 hsotg->driver = NULL;
3071
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003072 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003073
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003074 spin_unlock_irqrestore(&hsotg->lock, flags);
3075
Marek Szyprowskic8c10252013-09-12 16:18:48 +02003076 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003077
3078 return 0;
3079}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003080
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003081/**
3082 * s3c_hsotg_gadget_getframe - read the frame number
3083 * @gadget: The usb gadget state
3084 *
3085 * Read the {micro} frame number
3086 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003087static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
3088{
3089 return s3c_hsotg_read_frameno(to_hsotg(gadget));
3090}
3091
Lukasz Majewskia188b682012-06-22 09:29:56 +02003092/**
3093 * s3c_hsotg_pullup - connect/disconnect the USB PHY
3094 * @gadget: The usb gadget state
3095 * @is_on: Current state of the USB PHY
3096 *
3097 * Connect/Disconnect the USB PHY pullup
3098 */
3099static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
3100{
3101 struct s3c_hsotg *hsotg = to_hsotg(gadget);
3102 unsigned long flags = 0;
3103
3104 dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on);
3105
3106 spin_lock_irqsave(&hsotg->lock, flags);
3107 if (is_on) {
3108 s3c_hsotg_phy_enable(hsotg);
3109 s3c_hsotg_core_init(hsotg);
3110 } else {
3111 s3c_hsotg_disconnect(hsotg);
3112 s3c_hsotg_phy_disable(hsotg);
3113 }
3114
3115 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3116 spin_unlock_irqrestore(&hsotg->lock, flags);
3117
3118 return 0;
3119}
3120
Felipe Balbieeef4582013-01-24 17:58:16 +02003121static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003122 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003123 .udc_start = s3c_hsotg_udc_start,
3124 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02003125 .pullup = s3c_hsotg_pullup,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003126};
3127
3128/**
3129 * s3c_hsotg_initep - initialise a single endpoint
3130 * @hsotg: The device state.
3131 * @hs_ep: The endpoint to be initialised.
3132 * @epnum: The endpoint number
3133 *
3134 * Initialise the given endpoint (as part of the probe and device state
3135 * creation) to give to the gadget driver. Setup the endpoint name, any
3136 * direction information and other state that may be required.
3137 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003138static void s3c_hsotg_initep(struct s3c_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003139 struct s3c_hsotg_ep *hs_ep,
3140 int epnum)
3141{
3142 u32 ptxfifo;
3143 char *dir;
3144
3145 if (epnum == 0)
3146 dir = "";
3147 else if ((epnum % 2) == 0) {
3148 dir = "out";
3149 } else {
3150 dir = "in";
3151 hs_ep->dir_in = 1;
3152 }
3153
3154 hs_ep->index = epnum;
3155
3156 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3157
3158 INIT_LIST_HEAD(&hs_ep->queue);
3159 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3160
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003161 /* add to the list of endpoints known by the gadget driver */
3162 if (epnum)
3163 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3164
3165 hs_ep->parent = hsotg;
3166 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003167 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003168 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3169
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003170 /*
3171 * Read the FIFO size for the Periodic TX FIFO, even if we're
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003172 * an OUT endpoint, we may as well do this if in future the
3173 * code is changed to make each endpoint's direction changeable.
3174 */
3175
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003176 ptxfifo = readl(hsotg->regs + DPTXFSIZn(epnum));
3177 hs_ep->fifo_size = DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003178
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003179 /*
3180 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003181 * to be something valid.
3182 */
3183
3184 if (using_dma(hsotg)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003185 u32 next = DxEPCTL_NextEp((epnum + 1) % 15);
3186 writel(next, hsotg->regs + DIEPCTL(epnum));
3187 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003188 }
3189}
3190
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003191/**
3192 * s3c_hsotg_hw_cfg - read HW configuration registers
3193 * @param: The device state
3194 *
3195 * Read the USB core HW configuration registers
3196 */
3197static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003198{
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003199 u32 cfg2, cfg4;
Ben Dooks10aebc72010-07-19 09:40:44 +01003200 /* check hardware configuration */
3201
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003202 cfg2 = readl(hsotg->regs + 0x48);
3203 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3204
3205 dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
3206
Ben Dooks10aebc72010-07-19 09:40:44 +01003207 cfg4 = readl(hsotg->regs + 0x50);
3208 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3209
3210 dev_info(hsotg->dev, "%s fifos\n",
3211 hsotg->dedicated_fifos ? "dedicated" : "shared");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003212}
3213
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003214/**
3215 * s3c_hsotg_dump - dump state of the udc
3216 * @param: The device state
3217 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003218static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
3219{
Mark Brown83a01802011-06-01 17:16:15 +01003220#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003221 struct device *dev = hsotg->dev;
3222 void __iomem *regs = hsotg->regs;
3223 u32 val;
3224 int idx;
3225
3226 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003227 readl(regs + DCFG), readl(regs + DCTL),
3228 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003229
3230 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003231 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003232
3233 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003234 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003235
3236 /* show periodic fifo settings */
3237
3238 for (idx = 1; idx <= 15; idx++) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003239 val = readl(regs + DPTXFSIZn(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003240 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003241 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3242 val & DPTXFSIZn_DPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003243 }
3244
3245 for (idx = 0; idx < 15; idx++) {
3246 dev_info(dev,
3247 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003248 readl(regs + DIEPCTL(idx)),
3249 readl(regs + DIEPTSIZ(idx)),
3250 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003251
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003252 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003253 dev_info(dev,
3254 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003255 idx, readl(regs + DOEPCTL(idx)),
3256 readl(regs + DOEPTSIZ(idx)),
3257 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003258
3259 }
3260
3261 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003262 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003263#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003264}
3265
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003266/**
3267 * state_show - debugfs: show overall driver and device state.
3268 * @seq: The seq file to write to.
3269 * @v: Unused parameter.
3270 *
3271 * This debugfs entry shows the overall state of the hardware and
3272 * some general information about each of the endpoints available
3273 * to the system.
3274 */
3275static int state_show(struct seq_file *seq, void *v)
3276{
3277 struct s3c_hsotg *hsotg = seq->private;
3278 void __iomem *regs = hsotg->regs;
3279 int idx;
3280
3281 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003282 readl(regs + DCFG),
3283 readl(regs + DCTL),
3284 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003285
3286 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003287 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003288
3289 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003290 readl(regs + GINTMSK),
3291 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003292
3293 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003294 readl(regs + DAINTMSK),
3295 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003296
3297 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003298 readl(regs + GNPTXSTS),
3299 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003300
Pavel Macheka023da32013-09-30 14:56:02 +02003301 seq_puts(seq, "\nEndpoint status:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003302
3303 for (idx = 0; idx < 15; idx++) {
3304 u32 in, out;
3305
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003306 in = readl(regs + DIEPCTL(idx));
3307 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003308
3309 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3310 idx, in, out);
3311
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003312 in = readl(regs + DIEPTSIZ(idx));
3313 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003314
3315 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3316 in, out);
3317
Pavel Macheka023da32013-09-30 14:56:02 +02003318 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003319 }
3320
3321 return 0;
3322}
3323
3324static int state_open(struct inode *inode, struct file *file)
3325{
3326 return single_open(file, state_show, inode->i_private);
3327}
3328
3329static const struct file_operations state_fops = {
3330 .owner = THIS_MODULE,
3331 .open = state_open,
3332 .read = seq_read,
3333 .llseek = seq_lseek,
3334 .release = single_release,
3335};
3336
3337/**
3338 * fifo_show - debugfs: show the fifo information
3339 * @seq: The seq_file to write data to.
3340 * @v: Unused parameter.
3341 *
3342 * Show the FIFO information for the overall fifo and all the
3343 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003344 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003345static int fifo_show(struct seq_file *seq, void *v)
3346{
3347 struct s3c_hsotg *hsotg = seq->private;
3348 void __iomem *regs = hsotg->regs;
3349 u32 val;
3350 int idx;
3351
Pavel Macheka023da32013-09-30 14:56:02 +02003352 seq_puts(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003353 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003354
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003355 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003356 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003357 val >> GNPTXFSIZ_NPTxFDep_SHIFT,
3358 val & GNPTXFSIZ_NPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003359
Pavel Macheka023da32013-09-30 14:56:02 +02003360 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003361
3362 for (idx = 1; idx <= 15; idx++) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003363 val = readl(regs + DPTXFSIZn(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003364
3365 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003366 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3367 val & DPTXFSIZn_DPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003368 }
3369
3370 return 0;
3371}
3372
3373static int fifo_open(struct inode *inode, struct file *file)
3374{
3375 return single_open(file, fifo_show, inode->i_private);
3376}
3377
3378static const struct file_operations fifo_fops = {
3379 .owner = THIS_MODULE,
3380 .open = fifo_open,
3381 .read = seq_read,
3382 .llseek = seq_lseek,
3383 .release = single_release,
3384};
3385
3386
3387static const char *decode_direction(int is_in)
3388{
3389 return is_in ? "in" : "out";
3390}
3391
3392/**
3393 * ep_show - debugfs: show the state of an endpoint.
3394 * @seq: The seq_file to write data to.
3395 * @v: Unused parameter.
3396 *
3397 * This debugfs entry shows the state of the given endpoint (one is
3398 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003399 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003400static int ep_show(struct seq_file *seq, void *v)
3401{
3402 struct s3c_hsotg_ep *ep = seq->private;
3403 struct s3c_hsotg *hsotg = ep->parent;
3404 struct s3c_hsotg_req *req;
3405 void __iomem *regs = hsotg->regs;
3406 int index = ep->index;
3407 int show_limit = 15;
3408 unsigned long flags;
3409
3410 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3411 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3412
3413 /* first show the register state */
3414
3415 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003416 readl(regs + DIEPCTL(index)),
3417 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003418
3419 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003420 readl(regs + DIEPDMA(index)),
3421 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003422
3423 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003424 readl(regs + DIEPINT(index)),
3425 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003426
3427 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003428 readl(regs + DIEPTSIZ(index)),
3429 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003430
Pavel Macheka023da32013-09-30 14:56:02 +02003431 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003432 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3433 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3434
3435 seq_printf(seq, "request list (%p,%p):\n",
3436 ep->queue.next, ep->queue.prev);
3437
Lukasz Majewski22258f42012-06-14 10:02:24 +02003438 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003439
3440 list_for_each_entry(req, &ep->queue, queue) {
3441 if (--show_limit < 0) {
Pavel Macheka023da32013-09-30 14:56:02 +02003442 seq_puts(seq, "not showing more requests...\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003443 break;
3444 }
3445
3446 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3447 req == ep->req ? '*' : ' ',
3448 req, req->req.length, req->req.buf);
3449 seq_printf(seq, "%d done, res %d\n",
3450 req->req.actual, req->req.status);
3451 }
3452
Lukasz Majewski22258f42012-06-14 10:02:24 +02003453 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003454
3455 return 0;
3456}
3457
3458static int ep_open(struct inode *inode, struct file *file)
3459{
3460 return single_open(file, ep_show, inode->i_private);
3461}
3462
3463static const struct file_operations ep_fops = {
3464 .owner = THIS_MODULE,
3465 .open = ep_open,
3466 .read = seq_read,
3467 .llseek = seq_lseek,
3468 .release = single_release,
3469};
3470
3471/**
3472 * s3c_hsotg_create_debug - create debugfs directory and files
3473 * @hsotg: The driver state
3474 *
3475 * Create the debugfs files to allow the user to get information
3476 * about the state of the system. The directory name is created
3477 * with the same name as the device itself, in case we end up
3478 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003479 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003480static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003481{
3482 struct dentry *root;
3483 unsigned epidx;
3484
3485 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3486 hsotg->debug_root = root;
3487 if (IS_ERR(root)) {
3488 dev_err(hsotg->dev, "cannot create debug root\n");
3489 return;
3490 }
3491
3492 /* create general state file */
3493
3494 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3495 hsotg, &state_fops);
3496
3497 if (IS_ERR(hsotg->debug_file))
3498 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3499
3500 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3501 hsotg, &fifo_fops);
3502
3503 if (IS_ERR(hsotg->debug_fifo))
3504 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3505
3506 /* create one file for each endpoint */
3507
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003508 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003509 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3510
3511 ep->debugfs = debugfs_create_file(ep->name, 0444,
3512 root, ep, &ep_fops);
3513
3514 if (IS_ERR(ep->debugfs))
3515 dev_err(hsotg->dev, "failed to create %s debug file\n",
3516 ep->name);
3517 }
3518}
3519
3520/**
3521 * s3c_hsotg_delete_debug - cleanup debugfs entries
3522 * @hsotg: The driver state
3523 *
3524 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003525 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05003526static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003527{
3528 unsigned epidx;
3529
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003530 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003531 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3532 debugfs_remove(ep->debugfs);
3533 }
3534
3535 debugfs_remove(hsotg->debug_file);
3536 debugfs_remove(hsotg->debug_fifo);
3537 debugfs_remove(hsotg->debug_root);
3538}
3539
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003540/**
3541 * s3c_hsotg_probe - probe function for hsotg driver
3542 * @pdev: The platform information for the driver
3543 */
Lukasz Majewskif026a522012-05-04 14:17:13 +02003544
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003545static int s3c_hsotg_probe(struct platform_device *pdev)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003546{
Jingoo Hane01ee9f2013-07-30 17:00:51 +09003547 struct s3c_hsotg_plat *plat = dev_get_platdata(&pdev->dev);
Matt Porter74084842013-12-19 09:23:06 -05003548 struct phy *phy;
3549 struct usb_phy *uphy;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003550 struct device *dev = &pdev->dev;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003551 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003552 struct s3c_hsotg *hsotg;
3553 struct resource *res;
3554 int epnum;
3555 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003556 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003557
Sachin Kamat338edab2012-05-18 14:33:46 +05303558 hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003559 if (!hsotg) {
3560 dev_err(dev, "cannot get memory\n");
3561 return -ENOMEM;
3562 }
3563
Matt Porter74084842013-12-19 09:23:06 -05003564 /*
3565 * Attempt to find a generic PHY, then look for an old style
3566 * USB PHY, finally fall back to pdata
3567 */
3568 phy = devm_phy_get(&pdev->dev, "usb2-phy");
Felipe Balbif4f5ba52013-03-15 10:56:19 +02003569 if (IS_ERR(phy)) {
Matt Porter74084842013-12-19 09:23:06 -05003570 uphy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3571 if (IS_ERR(uphy)) {
3572 /* Fallback for pdata */
3573 plat = dev_get_platdata(&pdev->dev);
3574 if (!plat) {
3575 dev_err(&pdev->dev,
3576 "no platform data or transceiver defined\n");
3577 return -EPROBE_DEFER;
3578 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303579 hsotg->plat = plat;
Matt Porter74084842013-12-19 09:23:06 -05003580 } else
3581 hsotg->uphy = uphy;
3582 } else
Praveen Panerib2e587d2012-11-14 15:57:16 +05303583 hsotg->phy = phy;
Praveen Panerib2e587d2012-11-14 15:57:16 +05303584
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003585 hsotg->dev = dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003586
Sachin Kamat84749c62012-09-03 16:15:18 +05303587 hsotg->clk = devm_clk_get(&pdev->dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003588 if (IS_ERR(hsotg->clk)) {
3589 dev_err(dev, "cannot get otg clock\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303590 return PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003591 }
3592
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003593 platform_set_drvdata(pdev, hsotg);
3594
3595 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003596
Thierry Reding148e1132013-01-21 11:09:22 +01003597 hsotg->regs = devm_ioremap_resource(&pdev->dev, res);
3598 if (IS_ERR(hsotg->regs)) {
3599 ret = PTR_ERR(hsotg->regs);
Sachin Kamat338edab2012-05-18 14:33:46 +05303600 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003601 }
3602
3603 ret = platform_get_irq(pdev, 0);
3604 if (ret < 0) {
3605 dev_err(dev, "cannot find IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303606 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003607 }
3608
Lukasz Majewski22258f42012-06-14 10:02:24 +02003609 spin_lock_init(&hsotg->lock);
3610
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003611 hsotg->irq = ret;
3612
Sachin Kamat338edab2012-05-18 14:33:46 +05303613 ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
3614 dev_name(dev), hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003615 if (ret < 0) {
3616 dev_err(dev, "cannot claim IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303617 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003618 }
3619
3620 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3621
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003622 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003623 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3624 hsotg->gadget.name = dev_name(dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003625
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003626 /* reset the system */
3627
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003628 clk_prepare_enable(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003629
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003630 /* regulators */
3631
3632 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3633 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3634
Sachin Kamatcd762132013-01-08 14:27:00 +05303635 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003636 hsotg->supplies);
3637 if (ret) {
3638 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303639 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003640 }
3641
3642 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3643 hsotg->supplies);
3644
3645 if (ret) {
3646 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3647 goto err_supplies;
3648 }
3649
Matt Porterf7e504c2013-12-19 09:23:07 -05003650 /* Set default UTMI width */
3651 hsotg->phyif = GUSBCFG_PHYIf16;
3652
3653 /*
3654 * If using the generic PHY framework, check if the PHY bus
3655 * width is 8-bit and set the phyif appropriately.
3656 */
3657 if (hsotg->phy && (phy_get_bus_width(phy) == 8))
3658 hsotg->phyif = GUSBCFG_PHYIf8;
3659
Matt Porter74084842013-12-19 09:23:06 -05003660 if (hsotg->phy)
3661 phy_init(hsotg->phy);
3662
Lukasz Majewski41188782012-05-04 14:17:01 +02003663 /* usb phy enable */
3664 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003665
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003666 s3c_hsotg_corereset(hsotg);
3667 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003668 s3c_hsotg_hw_cfg(hsotg);
3669
3670 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3671
3672 if (hsotg->num_of_eps == 0) {
3673 dev_err(dev, "wrong number of EPs (zero)\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003674 ret = -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003675 goto err_supplies;
3676 }
3677
3678 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3679 GFP_KERNEL);
3680 if (!eps) {
3681 dev_err(dev, "cannot get memory\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003682 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003683 goto err_supplies;
3684 }
3685
3686 hsotg->eps = eps;
3687
3688 /* setup endpoint information */
3689
3690 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3691 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3692
3693 /* allocate EP0 request */
3694
3695 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3696 GFP_KERNEL);
3697 if (!hsotg->ctrl_req) {
3698 dev_err(dev, "failed to allocate ctrl req\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003699 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003700 goto err_ep_mem;
3701 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003702
3703 /* initialise the endpoints now the core has been initialised */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003704 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003705 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3706
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003707 /* disable power and clock */
3708
3709 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3710 hsotg->supplies);
3711 if (ret) {
3712 dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
3713 goto err_ep_mem;
3714 }
3715
3716 s3c_hsotg_phy_disable(hsotg);
3717
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003718 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3719 if (ret)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003720 goto err_ep_mem;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003721
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003722 s3c_hsotg_create_debug(hsotg);
3723
3724 s3c_hsotg_dump(hsotg);
3725
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003726 return 0;
3727
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003728err_ep_mem:
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003729 kfree(eps);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003730err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003731 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003732err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003733 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05303734
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003735 return ret;
3736}
3737
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003738/**
3739 * s3c_hsotg_remove - remove function for hsotg driver
3740 * @pdev: The platform information for the driver
3741 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05003742static int s3c_hsotg_remove(struct platform_device *pdev)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003743{
3744 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3745
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003746 usb_del_gadget_udc(&hsotg->gadget);
3747
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003748 s3c_hsotg_delete_debug(hsotg);
3749
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003750 if (hsotg->driver) {
3751 /* should have been done already by driver model core */
3752 usb_gadget_unregister_driver(hsotg->driver);
3753 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003754
Lukasz Majewski41188782012-05-04 14:17:01 +02003755 s3c_hsotg_phy_disable(hsotg);
Matt Porter74084842013-12-19 09:23:06 -05003756 if (hsotg->phy)
3757 phy_exit(hsotg->phy);
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003758 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003759
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003760 return 0;
3761}
3762
3763#if 1
3764#define s3c_hsotg_suspend NULL
3765#define s3c_hsotg_resume NULL
3766#endif
3767
Tomasz Figac50f056c2013-06-25 17:38:23 +02003768#ifdef CONFIG_OF
3769static const struct of_device_id s3c_hsotg_of_ids[] = {
3770 { .compatible = "samsung,s3c6400-hsotg", },
Matt Porter0d33d822013-12-19 09:23:05 -05003771 { .compatible = "snps,dwc2", },
Tomasz Figac50f056c2013-06-25 17:38:23 +02003772 { /* sentinel */ }
3773};
3774MODULE_DEVICE_TABLE(of, s3c_hsotg_of_ids);
3775#endif
3776
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003777static struct platform_driver s3c_hsotg_driver = {
3778 .driver = {
3779 .name = "s3c-hsotg",
3780 .owner = THIS_MODULE,
Tomasz Figac50f056c2013-06-25 17:38:23 +02003781 .of_match_table = of_match_ptr(s3c_hsotg_of_ids),
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003782 },
3783 .probe = s3c_hsotg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05003784 .remove = s3c_hsotg_remove,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003785 .suspend = s3c_hsotg_suspend,
3786 .resume = s3c_hsotg_resume,
3787};
3788
Axel Lincc27c962011-11-27 20:16:27 +08003789module_platform_driver(s3c_hsotg_driver);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003790
3791MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3792MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3793MODULE_LICENSE("GPL");
3794MODULE_ALIAS("platform:s3c-hsotg");