blob: f39daf7a7dce8997a31d34574e08b8e9b27e7957 [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>
34#include <linux/usb/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010035
36#include <linux/usb/ch9.h>
37#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053038#include <linux/usb/phy.h>
Lukasz Majewski126625e2012-05-09 13:16:53 +020039#include <linux/platform_data/s3c-hsotg.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010040
Lukasz Majewski127d42a2012-05-04 14:16:59 +020041#include "s3c-hsotg.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010042
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020043static const char * const s3c_hsotg_supply_names[] = {
44 "vusb_d", /* digital USB supply, 1.2V */
45 "vusb_a", /* analog USB supply, 1.1V */
46};
47
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020048/*
49 * EP0_MPS_LIMIT
Ben Dooks5b7d70c2009-06-02 14:58:06 +010050 *
51 * Unfortunately there seems to be a limit of the amount of data that can
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052 * be transferred by IN transactions on EP0. This is either 127 bytes or 3
53 * packets (which practically means 1 packet and 63 bytes of data) when the
Ben Dooks5b7d70c2009-06-02 14:58:06 +010054 * MPS is set to 64.
55 *
56 * This means if we are wanting to move >127 bytes of data, we need to
57 * split the transactions up, but just doing one packet at a time does
58 * not work (this may be an implicit DATA0 PID on first packet of the
59 * transaction) and doing 2 packets is outside the controller's limits.
60 *
61 * If we try to lower the MPS size for EP0, then no transfers work properly
62 * for EP0, and the system will fail basic enumeration. As no cause for this
63 * has currently been found, we cannot support any large IN transfers for
64 * EP0.
65 */
66#define EP0_MPS_LIMIT 64
67
68struct s3c_hsotg;
69struct s3c_hsotg_req;
70
71/**
72 * struct s3c_hsotg_ep - driver endpoint definition.
73 * @ep: The gadget layer representation of the endpoint.
74 * @name: The driver generated name for the endpoint.
75 * @queue: Queue of requests for this endpoint.
76 * @parent: Reference back to the parent device structure.
77 * @req: The current request that the endpoint is processing. This is
78 * used to indicate an request has been loaded onto the endpoint
79 * and has yet to be completed (maybe due to data move, or simply
80 * awaiting an ack from the core all the data has been completed).
81 * @debugfs: File entry for debugfs file for this endpoint.
82 * @lock: State lock to protect contents of endpoint.
83 * @dir_in: Set to true if this endpoint is of the IN direction, which
84 * means that it is sending data to the Host.
85 * @index: The index for the endpoint registers.
Robert Baldyga4fca54a2013-10-09 09:00:02 +020086 * @mc: Multi Count - number of transactions per microframe
Robert Baldyga1479e842013-10-09 08:41:57 +020087 * @interval - Interval for periodic endpoints
Ben Dooks5b7d70c2009-06-02 14:58:06 +010088 * @name: The name array passed to the USB core.
89 * @halted: Set if the endpoint has been halted.
90 * @periodic: Set if this is a periodic ep, such as Interrupt
Robert Baldyga1479e842013-10-09 08:41:57 +020091 * @isochronous: Set if this is a isochronous ep
Ben Dooks5b7d70c2009-06-02 14:58:06 +010092 * @sent_zlp: Set if we've sent a zero-length packet.
93 * @total_data: The total number of data bytes done.
94 * @fifo_size: The size of the FIFO (for periodic IN endpoints)
95 * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
96 * @last_load: The offset of data for the last start of request.
97 * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
98 *
99 * This is the driver's state for each registered enpoint, allowing it
100 * to keep track of transactions that need doing. Each endpoint has a
101 * lock to protect the state, to try and avoid using an overall lock
102 * for the host controller as much as possible.
103 *
104 * For periodic IN endpoints, we have fifo_size and fifo_load to try
105 * and keep track of the amount of data in the periodic FIFO for each
106 * of these as we don't have a status register that tells us how much
Ben Dookse7a9ff52010-07-19 09:40:42 +0100107 * is in each of them. (note, this may actually be useless information
108 * as in shared-fifo mode periodic in acts like a single-frame packet
109 * buffer than a fifo)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100110 */
111struct s3c_hsotg_ep {
112 struct usb_ep ep;
113 struct list_head queue;
114 struct s3c_hsotg *parent;
115 struct s3c_hsotg_req *req;
116 struct dentry *debugfs;
117
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100118
119 unsigned long total_data;
120 unsigned int size_loaded;
121 unsigned int last_load;
122 unsigned int fifo_load;
123 unsigned short fifo_size;
124
125 unsigned char dir_in;
126 unsigned char index;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200127 unsigned char mc;
Robert Baldyga1479e842013-10-09 08:41:57 +0200128 unsigned char interval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100129
130 unsigned int halted:1;
131 unsigned int periodic:1;
Robert Baldyga1479e842013-10-09 08:41:57 +0200132 unsigned int isochronous:1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100133 unsigned int sent_zlp:1;
134
135 char name[10];
136};
137
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100138/**
139 * struct s3c_hsotg - driver state.
140 * @dev: The parent device supplied to the probe function
141 * @driver: USB gadget driver
Praveen Panerib2e587d2012-11-14 15:57:16 +0530142 * @phy: The otg phy transceiver structure for phy control.
Matt Porter74084842013-12-19 09:23:06 -0500143 * @uphy: The otg phy transceiver structure for old USB phy control.
Praveen Panerib2e587d2012-11-14 15:57:16 +0530144 * @plat: The platform specific configuration data. This can be removed once
145 * all SoCs support usb transceiver.
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100146 * @regs: The memory area mapped for accessing registers.
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100147 * @irq: The IRQ number we are using
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200148 * @supplies: Definition of USB power supplies
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
Ben Dooks10aebc72010-07-19 09:40:44 +0100177 unsigned int dedicated_fifos:1;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200178 unsigned char num_of_eps;
Ben Dooks10aebc72010-07-19 09:40:44 +0100179
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100180 struct dentry *debug_root;
181 struct dentry *debug_file;
182 struct dentry *debug_fifo;
183
184 struct usb_request *ep0_reply;
185 struct usb_request *ctrl_req;
186 u8 ep0_buff[8];
187 u8 ctrl_buff[8];
188
189 struct usb_gadget gadget;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200190 unsigned int setup;
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200191 unsigned long last_rst;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200192 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100193};
194
195/**
196 * struct s3c_hsotg_req - data transfer request
197 * @req: The USB gadget request
198 * @queue: The list of requests for the endpoint this is queued for.
199 * @in_progress: Has already had size/packets written to core
200 * @mapped: DMA buffer for this request has been mapped via dma_map_single().
201 */
202struct s3c_hsotg_req {
203 struct usb_request req;
204 struct list_head queue;
205 unsigned char in_progress;
206 unsigned char mapped;
207};
208
209/* conversion functions */
210static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
211{
212 return container_of(req, struct s3c_hsotg_req, req);
213}
214
215static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
216{
217 return container_of(ep, struct s3c_hsotg_ep, ep);
218}
219
220static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
221{
222 return container_of(gadget, struct s3c_hsotg, gadget);
223}
224
225static inline void __orr32(void __iomem *ptr, u32 val)
226{
227 writel(readl(ptr) | val, ptr);
228}
229
230static inline void __bic32(void __iomem *ptr, u32 val)
231{
232 writel(readl(ptr) & ~val, ptr);
233}
234
235/* forward decleration of functions */
236static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
237
238/**
239 * using_dma - return the DMA status of the driver.
240 * @hsotg: The driver state.
241 *
242 * Return true if we're using DMA.
243 *
244 * Currently, we have the DMA support code worked into everywhere
245 * that needs it, but the AMBA DMA implementation in the hardware can
246 * only DMA from 32bit aligned addresses. This means that gadgets such
247 * as the CDC Ethernet cannot work as they often pass packets which are
248 * not 32bit aligned.
249 *
250 * Unfortunately the choice to use DMA or not is global to the controller
251 * and seems to be only settable when the controller is being put through
252 * a core reset. This means we either need to fix the gadgets to take
253 * account of DMA alignment, or add bounce buffers (yuerk).
254 *
255 * Until this issue is sorted out, we always return 'false'.
256 */
257static inline bool using_dma(struct s3c_hsotg *hsotg)
258{
259 return false; /* support is not complete */
260}
261
262/**
263 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
264 * @hsotg: The device state
265 * @ints: A bitmask of the interrupts to enable
266 */
267static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
268{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200269 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100270 u32 new_gsintmsk;
271
272 new_gsintmsk = gsintmsk | ints;
273
274 if (new_gsintmsk != gsintmsk) {
275 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200276 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100277 }
278}
279
280/**
281 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
282 * @hsotg: The device state
283 * @ints: A bitmask of the interrupts to enable
284 */
285static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
286{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200287 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100288 u32 new_gsintmsk;
289
290 new_gsintmsk = gsintmsk & ~ints;
291
292 if (new_gsintmsk != gsintmsk)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200293 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100294}
295
296/**
297 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
298 * @hsotg: The device state
299 * @ep: The endpoint index
300 * @dir_in: True if direction is in.
301 * @en: The enable value, true to enable
302 *
303 * Set or clear the mask for an individual endpoint's interrupt
304 * request.
305 */
306static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
307 unsigned int ep, unsigned int dir_in,
308 unsigned int en)
309{
310 unsigned long flags;
311 u32 bit = 1 << ep;
312 u32 daint;
313
314 if (!dir_in)
315 bit <<= 16;
316
317 local_irq_save(flags);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200318 daint = readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100319 if (en)
320 daint |= bit;
321 else
322 daint &= ~bit;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200323 writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100324 local_irq_restore(flags);
325}
326
327/**
328 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
329 * @hsotg: The device instance.
330 */
331static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
332{
Ben Dooks0f002d22010-05-25 05:36:50 +0100333 unsigned int ep;
334 unsigned int addr;
335 unsigned int size;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100336 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100337 u32 val;
338
Ben Dooks6d091ee2010-07-19 09:40:40 +0100339 /* set FIFO sizes to 2048/1024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100340
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200341 writel(2048, hsotg->regs + GRXFSIZ);
342 writel(GNPTXFSIZ_NPTxFStAddr(2048) |
343 GNPTXFSIZ_NPTxFDep(1024),
344 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100345
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200346 /*
347 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100348 * block have overlapping default addresses. This also ensures
349 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200350 * known values.
351 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100352
353 /* start at the end of the GNPTXFSIZ, rounded up */
354 addr = 2048 + 1024;
355 size = 768;
356
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200357 /*
358 * currently we allocate TX FIFOs for all possible endpoints,
359 * and assume that they are all the same size.
360 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100361
Anton Tikhomirovf7a83fe2012-03-06 14:05:49 +0900362 for (ep = 1; ep <= 15; ep++) {
Ben Dooks0f002d22010-05-25 05:36:50 +0100363 val = addr;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200364 val |= size << DPTXFSIZn_DPTxFSize_SHIFT;
Ben Dooks0f002d22010-05-25 05:36:50 +0100365 addr += size;
366
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200367 writel(val, hsotg->regs + DPTXFSIZn(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100368 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100369
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200370 /*
371 * according to p428 of the design guide, we need to ensure that
372 * all fifos are flushed before continuing
373 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100374
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200375 writel(GRSTCTL_TxFNum(0x10) | GRSTCTL_TxFFlsh |
376 GRSTCTL_RxFFlsh, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100377
378 /* wait until the fifos are both flushed */
379 timeout = 100;
380 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200381 val = readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100382
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200383 if ((val & (GRSTCTL_TxFFlsh | GRSTCTL_RxFFlsh)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100384 break;
385
386 if (--timeout == 0) {
387 dev_err(hsotg->dev,
388 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
389 __func__, val);
390 }
391
392 udelay(1);
393 }
394
395 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100396}
397
398/**
399 * @ep: USB endpoint to allocate request for.
400 * @flags: Allocation flags
401 *
402 * Allocate a new USB request structure appropriate for the specified endpoint
403 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000404static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
405 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100406{
407 struct s3c_hsotg_req *req;
408
409 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
410 if (!req)
411 return NULL;
412
413 INIT_LIST_HEAD(&req->queue);
414
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100415 return &req->req;
416}
417
418/**
419 * is_ep_periodic - return true if the endpoint is in periodic mode.
420 * @hs_ep: The endpoint to query.
421 *
422 * Returns true if the endpoint is in periodic mode, meaning it is being
423 * used for an Interrupt or ISO transfer.
424 */
425static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
426{
427 return hs_ep->periodic;
428}
429
430/**
431 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
432 * @hsotg: The device state.
433 * @hs_ep: The endpoint for the request
434 * @hs_req: The request being processed.
435 *
436 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
437 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200438 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100439static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
440 struct s3c_hsotg_ep *hs_ep,
441 struct s3c_hsotg_req *hs_req)
442{
443 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100444
445 /* ignore this if we're not moving any data */
446 if (hs_req->req.length == 0)
447 return;
448
Jingoo Han17d966a2013-05-11 21:14:00 +0900449 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100450}
451
452/**
453 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
454 * @hsotg: The controller state.
455 * @hs_ep: The endpoint we're going to write for.
456 * @hs_req: The request to write data for.
457 *
458 * This is called when the TxFIFO has some space in it to hold a new
459 * transmission and we have something to give it. The actual setup of
460 * the data size is done elsewhere, so all we have to do is to actually
461 * write the data.
462 *
463 * The return value is zero if there is more space (or nothing was done)
464 * otherwise -ENOSPC is returned if the FIFO space was used up.
465 *
466 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200467 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100468static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
469 struct s3c_hsotg_ep *hs_ep,
470 struct s3c_hsotg_req *hs_req)
471{
472 bool periodic = is_ep_periodic(hs_ep);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200473 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100474 int buf_pos = hs_req->req.actual;
475 int to_write = hs_ep->size_loaded;
476 void *data;
477 int can_write;
478 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200479 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100480
481 to_write -= (buf_pos - hs_ep->last_load);
482
483 /* if there's nothing to write, get out early */
484 if (to_write == 0)
485 return 0;
486
Ben Dooks10aebc72010-07-19 09:40:44 +0100487 if (periodic && !hsotg->dedicated_fifos) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200488 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100489 int size_left;
490 int size_done;
491
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200492 /*
493 * work out how much data was loaded so we can calculate
494 * how much data is left in the fifo.
495 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100496
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200497 size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100498
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200499 /*
500 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100501 * previous data has been completely sent.
502 */
503 if (hs_ep->fifo_load != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200504 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100505 return -ENOSPC;
506 }
507
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100508 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
509 __func__, size_left,
510 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
511
512 /* how much of the data has moved */
513 size_done = hs_ep->size_loaded - size_left;
514
515 /* how much data is left in the fifo */
516 can_write = hs_ep->fifo_load - size_done;
517 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
518 __func__, can_write);
519
520 can_write = hs_ep->fifo_size - can_write;
521 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
522 __func__, can_write);
523
524 if (can_write <= 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200525 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100526 return -ENOSPC;
527 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100528 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200529 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100530
531 can_write &= 0xffff;
532 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100533 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200534 if (GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100535 dev_dbg(hsotg->dev,
536 "%s: no queue slots available (0x%08x)\n",
537 __func__, gnptxsts);
538
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200539 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100540 return -ENOSPC;
541 }
542
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200543 can_write = GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100544 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100545 }
546
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200547 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
548
549 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
550 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100551
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200552 /*
553 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100554 * FIFO, requests of >512 cause the endpoint to get stuck with a
555 * fragment of the end of the transfer in it.
556 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200557 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100558 can_write = 512;
559
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200560 /*
561 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100562 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200563 * doing it.
564 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200565 if (to_write > max_transfer) {
566 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100567
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200568 /* it's needed only when we do not use dedicated fifos */
569 if (!hsotg->dedicated_fifos)
570 s3c_hsotg_en_gsint(hsotg,
571 periodic ? GINTSTS_PTxFEmp :
572 GINTSTS_NPTxFEmp);
Ben Dooks03e10e52010-07-19 09:40:45 +0100573 }
574
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100575 /* see if we can write data */
576
577 if (to_write > can_write) {
578 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200579 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100580
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200581 /*
582 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100583 * exact number of packets.
584 *
585 * Note, we do not currently check to see if we can ever
586 * write a full packet or not to the FIFO.
587 */
588
589 if (pkt_round)
590 to_write -= pkt_round;
591
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200592 /*
593 * enable correct FIFO interrupt to alert us when there
594 * is more room left.
595 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100596
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200597 /* it's needed only when we do not use dedicated fifos */
598 if (!hsotg->dedicated_fifos)
599 s3c_hsotg_en_gsint(hsotg,
600 periodic ? GINTSTS_PTxFEmp :
601 GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100602 }
603
604 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
605 to_write, hs_req->req.length, can_write, buf_pos);
606
607 if (to_write <= 0)
608 return -ENOSPC;
609
610 hs_req->req.actual = buf_pos + to_write;
611 hs_ep->total_data += to_write;
612
613 if (periodic)
614 hs_ep->fifo_load += to_write;
615
616 to_write = DIV_ROUND_UP(to_write, 4);
617 data = hs_req->req.buf + buf_pos;
618
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200619 writesl(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100620
621 return (to_write >= can_write) ? -ENOSPC : 0;
622}
623
624/**
625 * get_ep_limit - get the maximum data legnth for this endpoint
626 * @hs_ep: The endpoint
627 *
628 * Return the maximum data that can be queued in one go on a given endpoint
629 * so that transfers that are too long can be split.
630 */
631static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
632{
633 int index = hs_ep->index;
634 unsigned maxsize;
635 unsigned maxpkt;
636
637 if (index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200638 maxsize = DxEPTSIZ_XferSize_LIMIT + 1;
639 maxpkt = DxEPTSIZ_PktCnt_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100640 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100641 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900642 if (hs_ep->dir_in)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200643 maxpkt = DIEPTSIZ0_PktCnt_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900644 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100645 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100646 }
647
648 /* we made the constant loading easier above by using +1 */
649 maxpkt--;
650 maxsize--;
651
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200652 /*
653 * constrain by packet count if maxpkts*pktsize is greater
654 * than the length register size.
655 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100656
657 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
658 maxsize = maxpkt * hs_ep->ep.maxpacket;
659
660 return maxsize;
661}
662
663/**
664 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
665 * @hsotg: The controller state.
666 * @hs_ep: The endpoint to process a request for
667 * @hs_req: The request to start.
668 * @continuing: True if we are doing more for the current request.
669 *
670 * Start the given request running by setting the endpoint registers
671 * appropriately, and writing any data to the FIFOs.
672 */
673static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
674 struct s3c_hsotg_ep *hs_ep,
675 struct s3c_hsotg_req *hs_req,
676 bool continuing)
677{
678 struct usb_request *ureq = &hs_req->req;
679 int index = hs_ep->index;
680 int dir_in = hs_ep->dir_in;
681 u32 epctrl_reg;
682 u32 epsize_reg;
683 u32 epsize;
684 u32 ctrl;
685 unsigned length;
686 unsigned packets;
687 unsigned maxreq;
688
689 if (index != 0) {
690 if (hs_ep->req && !continuing) {
691 dev_err(hsotg->dev, "%s: active request\n", __func__);
692 WARN_ON(1);
693 return;
694 } else if (hs_ep->req != hs_req && continuing) {
695 dev_err(hsotg->dev,
696 "%s: continue different req\n", __func__);
697 WARN_ON(1);
698 return;
699 }
700 }
701
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200702 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
703 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100704
705 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
706 __func__, readl(hsotg->regs + epctrl_reg), index,
707 hs_ep->dir_in ? "in" : "out");
708
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900709 /* If endpoint is stalled, we will restart request later */
710 ctrl = readl(hsotg->regs + epctrl_reg);
711
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200712 if (ctrl & DxEPCTL_Stall) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900713 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
714 return;
715 }
716
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100717 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200718 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
719 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100720 if (0)
721 dev_dbg(hsotg->dev,
722 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
723 ureq->buf, length, ureq->dma,
724 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
725
726 maxreq = get_ep_limit(hs_ep);
727 if (length > maxreq) {
728 int round = maxreq % hs_ep->ep.maxpacket;
729
730 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
731 __func__, length, maxreq, round);
732
733 /* round down to multiple of packets */
734 if (round)
735 maxreq -= round;
736
737 length = maxreq;
738 }
739
740 if (length)
741 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
742 else
743 packets = 1; /* send one packet if length is zero. */
744
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200745 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
746 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
747 return;
748 }
749
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100750 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200751 if (hs_ep->isochronous)
752 epsize = DxEPTSIZ_MC(packets);
753 else
754 epsize = DxEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100755 else
756 epsize = 0;
757
758 if (index != 0 && ureq->zero) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200759 /*
760 * test for the packets being exactly right for the
761 * transfer
762 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100763
764 if (length == (packets * hs_ep->ep.maxpacket))
765 packets++;
766 }
767
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200768 epsize |= DxEPTSIZ_PktCnt(packets);
769 epsize |= DxEPTSIZ_XferSize(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100770
771 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
772 __func__, packets, length, ureq->length, epsize, epsize_reg);
773
774 /* store the request as the current one we're doing */
775 hs_ep->req = hs_req;
776
777 /* write size / packets */
778 writel(epsize, hsotg->regs + epsize_reg);
779
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900780 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100781 unsigned int dma_reg;
782
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200783 /*
784 * write DMA address to control register, buffer already
785 * synced by s3c_hsotg_ep_queue().
786 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100787
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200788 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100789 writel(ureq->dma, hsotg->regs + dma_reg);
790
791 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
792 __func__, ureq->dma, dma_reg);
793 }
794
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200795 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
796 ctrl |= DxEPCTL_USBActEp;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200797
798 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
799
800 /* For Setup request do not clear NAK */
801 if (hsotg->setup && index == 0)
802 hsotg->setup = 0;
803 else
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200804 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200805
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100806
807 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
808 writel(ctrl, hsotg->regs + epctrl_reg);
809
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200810 /*
811 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100812 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200813 * this information.
814 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100815 hs_ep->size_loaded = length;
816 hs_ep->last_load = ureq->actual;
817
818 if (dir_in && !using_dma(hsotg)) {
819 /* set these anyway, we may need them for non-periodic in */
820 hs_ep->fifo_load = 0;
821
822 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
823 }
824
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200825 /*
826 * clear the INTknTXFEmpMsk when we start request, more as a aide
827 * to debugging to see what is going on.
828 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100829 if (dir_in)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200830 writel(DIEPMSK_INTknTXFEmpMsk,
831 hsotg->regs + DIEPINT(index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100832
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200833 /*
834 * Note, trying to clear the NAK here causes problems with transmit
835 * on the S3C6400 ending up with the TXFIFO becoming full.
836 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100837
838 /* check ep is enabled */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200839 if (!(readl(hsotg->regs + epctrl_reg) & DxEPCTL_EPEna))
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100840 dev_warn(hsotg->dev,
841 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
842 index, readl(hsotg->regs + epctrl_reg));
843
844 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
845 __func__, readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200846
847 /* enable ep interrupts */
848 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100849}
850
851/**
852 * s3c_hsotg_map_dma - map the DMA memory being used for the request
853 * @hsotg: The device state.
854 * @hs_ep: The endpoint the request is on.
855 * @req: The request being processed.
856 *
857 * We've been asked to queue a request, so ensure that the memory buffer
858 * is correctly setup for DMA. If we've been passed an extant DMA address
859 * then ensure the buffer has been synced to memory. If our buffer has no
860 * DMA memory, then we map the memory and mark our request to allow us to
861 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200862 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100863static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
864 struct s3c_hsotg_ep *hs_ep,
865 struct usb_request *req)
866{
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100867 struct s3c_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200868 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100869
870 /* if the length is zero, ignore the DMA data */
871 if (hs_req->req.length == 0)
872 return 0;
873
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200874 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
875 if (ret)
876 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100877
878 return 0;
879
880dma_error:
881 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
882 __func__, req->buf, req->length);
883
884 return -EIO;
885}
886
887static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
888 gfp_t gfp_flags)
889{
890 struct s3c_hsotg_req *hs_req = our_req(req);
891 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
892 struct s3c_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100893 bool first;
894
895 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
896 ep->name, req, req->length, req->buf, req->no_interrupt,
897 req->zero, req->short_not_ok);
898
899 /* initialise status of the request */
900 INIT_LIST_HEAD(&hs_req->queue);
901 req->actual = 0;
902 req->status = -EINPROGRESS;
903
904 /* if we're using DMA, sync the buffers as necessary */
905 if (using_dma(hs)) {
906 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
907 if (ret)
908 return ret;
909 }
910
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100911 first = list_empty(&hs_ep->queue);
912 list_add_tail(&hs_req->queue, &hs_ep->queue);
913
914 if (first)
915 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
916
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100917 return 0;
918}
919
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200920static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
921 gfp_t gfp_flags)
922{
923 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
924 struct s3c_hsotg *hs = hs_ep->parent;
925 unsigned long flags = 0;
926 int ret = 0;
927
928 spin_lock_irqsave(&hs->lock, flags);
929 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
930 spin_unlock_irqrestore(&hs->lock, flags);
931
932 return ret;
933}
934
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100935static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
936 struct usb_request *req)
937{
938 struct s3c_hsotg_req *hs_req = our_req(req);
939
940 kfree(hs_req);
941}
942
943/**
944 * s3c_hsotg_complete_oursetup - setup completion callback
945 * @ep: The endpoint the request was on.
946 * @req: The request completed.
947 *
948 * Called on completion of any requests the driver itself
949 * submitted that need cleaning up.
950 */
951static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
952 struct usb_request *req)
953{
954 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
955 struct s3c_hsotg *hsotg = hs_ep->parent;
956
957 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
958
959 s3c_hsotg_ep_free_request(ep, req);
960}
961
962/**
963 * ep_from_windex - convert control wIndex value to endpoint
964 * @hsotg: The driver state.
965 * @windex: The control request wIndex field (in host order).
966 *
967 * Convert the given wIndex into a pointer to an driver endpoint
968 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200969 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100970static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
971 u32 windex)
972{
973 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
974 int dir = (windex & USB_DIR_IN) ? 1 : 0;
975 int idx = windex & 0x7F;
976
977 if (windex >= 0x100)
978 return NULL;
979
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200980 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100981 return NULL;
982
983 if (idx && ep->dir_in != dir)
984 return NULL;
985
986 return ep;
987}
988
989/**
990 * s3c_hsotg_send_reply - send reply to control request
991 * @hsotg: The device state
992 * @ep: Endpoint 0
993 * @buff: Buffer for request
994 * @length: Length of reply.
995 *
996 * Create a request and queue it on the given endpoint. This is useful as
997 * an internal method of sending replies to certain control requests, etc.
998 */
999static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
1000 struct s3c_hsotg_ep *ep,
1001 void *buff,
1002 int length)
1003{
1004 struct usb_request *req;
1005 int ret;
1006
1007 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1008
1009 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1010 hsotg->ep0_reply = req;
1011 if (!req) {
1012 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1013 return -ENOMEM;
1014 }
1015
1016 req->buf = hsotg->ep0_buff;
1017 req->length = length;
1018 req->zero = 1; /* always do zero-length final transfer */
1019 req->complete = s3c_hsotg_complete_oursetup;
1020
1021 if (length)
1022 memcpy(req->buf, buff, length);
1023 else
1024 ep->sent_zlp = 1;
1025
1026 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1027 if (ret) {
1028 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1029 return ret;
1030 }
1031
1032 return 0;
1033}
1034
1035/**
1036 * s3c_hsotg_process_req_status - process request GET_STATUS
1037 * @hsotg: The device state
1038 * @ctrl: USB control request
1039 */
1040static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1041 struct usb_ctrlrequest *ctrl)
1042{
1043 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1044 struct s3c_hsotg_ep *ep;
1045 __le16 reply;
1046 int ret;
1047
1048 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1049
1050 if (!ep0->dir_in) {
1051 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1052 return -EINVAL;
1053 }
1054
1055 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1056 case USB_RECIP_DEVICE:
1057 reply = cpu_to_le16(0); /* bit 0 => self powered,
1058 * bit 1 => remote wakeup */
1059 break;
1060
1061 case USB_RECIP_INTERFACE:
1062 /* currently, the data result should be zero */
1063 reply = cpu_to_le16(0);
1064 break;
1065
1066 case USB_RECIP_ENDPOINT:
1067 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1068 if (!ep)
1069 return -ENOENT;
1070
1071 reply = cpu_to_le16(ep->halted ? 1 : 0);
1072 break;
1073
1074 default:
1075 return 0;
1076 }
1077
1078 if (le16_to_cpu(ctrl->wLength) != 2)
1079 return -EINVAL;
1080
1081 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1082 if (ret) {
1083 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1084 return ret;
1085 }
1086
1087 return 1;
1088}
1089
1090static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1091
1092/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001093 * get_ep_head - return the first request on the endpoint
1094 * @hs_ep: The controller endpoint to get
1095 *
1096 * Get the first request on the endpoint.
1097 */
1098static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1099{
1100 if (list_empty(&hs_ep->queue))
1101 return NULL;
1102
1103 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1104}
1105
1106/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001107 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1108 * @hsotg: The device state
1109 * @ctrl: USB control request
1110 */
1111static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1112 struct usb_ctrlrequest *ctrl)
1113{
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001114 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001115 struct s3c_hsotg_req *hs_req;
1116 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001117 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1118 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001119 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001120 bool halted;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001121
1122 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1123 __func__, set ? "SET" : "CLEAR");
1124
1125 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1126 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1127 if (!ep) {
1128 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1129 __func__, le16_to_cpu(ctrl->wIndex));
1130 return -ENOENT;
1131 }
1132
1133 switch (le16_to_cpu(ctrl->wValue)) {
1134 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001135 halted = ep->halted;
1136
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001137 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001138
1139 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1140 if (ret) {
1141 dev_err(hsotg->dev,
1142 "%s: failed to send reply\n", __func__);
1143 return ret;
1144 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001145
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001146 /*
1147 * we have to complete all requests for ep if it was
1148 * halted, and the halt was cleared by CLEAR_FEATURE
1149 */
1150
1151 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001152 /*
1153 * If we have request in progress,
1154 * then complete it
1155 */
1156 if (ep->req) {
1157 hs_req = ep->req;
1158 ep->req = NULL;
1159 list_del_init(&hs_req->queue);
1160 hs_req->req.complete(&ep->ep,
1161 &hs_req->req);
1162 }
1163
1164 /* If we have pending request, then start it */
1165 restart = !list_empty(&ep->queue);
1166 if (restart) {
1167 hs_req = get_ep_head(ep);
1168 s3c_hsotg_start_req(hsotg, ep,
1169 hs_req, false);
1170 }
1171 }
1172
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001173 break;
1174
1175 default:
1176 return -ENOENT;
1177 }
1178 } else
1179 return -ENOENT; /* currently only deal with endpoint */
1180
1181 return 1;
1182}
1183
Robert Baldygaab93e012013-09-19 11:50:17 +02001184static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
Robert Baldygad18f71162013-11-21 13:49:18 +01001185static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001186
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001187/**
1188 * s3c_hsotg_process_control - process a control request
1189 * @hsotg: The device state
1190 * @ctrl: The control request received
1191 *
1192 * The controller has received the SETUP phase of a control request, and
1193 * needs to work out what to do next (and whether to pass it on to the
1194 * gadget driver).
1195 */
1196static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1197 struct usb_ctrlrequest *ctrl)
1198{
1199 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1200 int ret = 0;
1201 u32 dcfg;
1202
1203 ep0->sent_zlp = 0;
1204
1205 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1206 ctrl->bRequest, ctrl->bRequestType,
1207 ctrl->wValue, ctrl->wLength);
1208
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001209 /*
1210 * record the direction of the request, for later use when enquing
1211 * packets onto EP0.
1212 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001213
1214 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1215 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1216
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001217 /*
1218 * if we've no data with this request, then the last part of the
1219 * transaction is going to implicitly be IN.
1220 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001221 if (ctrl->wLength == 0)
1222 ep0->dir_in = 1;
1223
1224 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1225 switch (ctrl->bRequest) {
1226 case USB_REQ_SET_ADDRESS:
Robert Baldygad18f71162013-11-21 13:49:18 +01001227 s3c_hsotg_disconnect(hsotg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001228 dcfg = readl(hsotg->regs + DCFG);
1229 dcfg &= ~DCFG_DevAddr_MASK;
1230 dcfg |= ctrl->wValue << DCFG_DevAddr_SHIFT;
1231 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001232
1233 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1234
1235 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1236 return;
1237
1238 case USB_REQ_GET_STATUS:
1239 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1240 break;
1241
1242 case USB_REQ_CLEAR_FEATURE:
1243 case USB_REQ_SET_FEATURE:
1244 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1245 break;
1246 }
1247 }
1248
1249 /* as a fallback, try delivering it to the driver to deal with */
1250
1251 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001252 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001253 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001254 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001255 if (ret < 0)
1256 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1257 }
1258
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001259 /*
1260 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001261 * so respond with a STALL for the status stage to indicate failure.
1262 */
1263
1264 if (ret < 0) {
1265 u32 reg;
1266 u32 ctrl;
1267
1268 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001269 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001270
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001271 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001272 * DxEPCTL_Stall will be cleared by EP once it has
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001273 * taken effect, so no need to clear later.
1274 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001275
1276 ctrl = readl(hsotg->regs + reg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001277 ctrl |= DxEPCTL_Stall;
1278 ctrl |= DxEPCTL_CNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001279 writel(ctrl, hsotg->regs + reg);
1280
1281 dev_dbg(hsotg->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001282 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001283 ctrl, reg, readl(hsotg->regs + reg));
1284
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001285 /*
1286 * don't believe we need to anything more to get the EP
1287 * to reply with a STALL packet
1288 */
Robert Baldygaab93e012013-09-19 11:50:17 +02001289
1290 /*
1291 * complete won't be called, so we enqueue
1292 * setup request here
1293 */
1294 s3c_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001295 }
1296}
1297
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001298/**
1299 * s3c_hsotg_complete_setup - completion of a setup transfer
1300 * @ep: The endpoint the request was on.
1301 * @req: The request completed.
1302 *
1303 * Called on completion of any requests the driver itself submitted for
1304 * EP0 setup packets
1305 */
1306static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1307 struct usb_request *req)
1308{
1309 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1310 struct s3c_hsotg *hsotg = hs_ep->parent;
1311
1312 if (req->status < 0) {
1313 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1314 return;
1315 }
1316
Robert Baldyga93f599f2013-11-21 13:49:17 +01001317 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001318 if (req->actual == 0)
1319 s3c_hsotg_enqueue_setup(hsotg);
1320 else
1321 s3c_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001322 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001323}
1324
1325/**
1326 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1327 * @hsotg: The device state.
1328 *
1329 * Enqueue a request on EP0 if necessary to received any SETUP packets
1330 * received from the host.
1331 */
1332static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1333{
1334 struct usb_request *req = hsotg->ctrl_req;
1335 struct s3c_hsotg_req *hs_req = our_req(req);
1336 int ret;
1337
1338 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1339
1340 req->zero = 0;
1341 req->length = 8;
1342 req->buf = hsotg->ctrl_buff;
1343 req->complete = s3c_hsotg_complete_setup;
1344
1345 if (!list_empty(&hs_req->queue)) {
1346 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1347 return;
1348 }
1349
1350 hsotg->eps[0].dir_in = 0;
1351
1352 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1353 if (ret < 0) {
1354 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001355 /*
1356 * Don't think there's much we can do other than watch the
1357 * driver fail.
1358 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001359 }
1360}
1361
1362/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001363 * s3c_hsotg_complete_request - complete a request given to us
1364 * @hsotg: The device state.
1365 * @hs_ep: The endpoint the request was on.
1366 * @hs_req: The request to complete.
1367 * @result: The result code (0 => Ok, otherwise errno)
1368 *
1369 * The given request has finished, so call the necessary completion
1370 * if it has one and then look to see if we can start a new request
1371 * on the endpoint.
1372 *
1373 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001374 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001375static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1376 struct s3c_hsotg_ep *hs_ep,
1377 struct s3c_hsotg_req *hs_req,
1378 int result)
1379{
1380 bool restart;
1381
1382 if (!hs_req) {
1383 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1384 return;
1385 }
1386
1387 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1388 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1389
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001390 /*
1391 * only replace the status if we've not already set an error
1392 * from a previous transaction
1393 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001394
1395 if (hs_req->req.status == -EINPROGRESS)
1396 hs_req->req.status = result;
1397
1398 hs_ep->req = NULL;
1399 list_del_init(&hs_req->queue);
1400
1401 if (using_dma(hsotg))
1402 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1403
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001404 /*
1405 * call the complete request with the locks off, just in case the
1406 * request tries to queue more work for this endpoint.
1407 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001408
1409 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001410 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001411 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001412 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001413 }
1414
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001415 /*
1416 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001417 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001418 * so be careful when doing this.
1419 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001420
1421 if (!hs_ep->req && result >= 0) {
1422 restart = !list_empty(&hs_ep->queue);
1423 if (restart) {
1424 hs_req = get_ep_head(hs_ep);
1425 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1426 }
1427 }
1428}
1429
1430/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001431 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1432 * @hsotg: The device state.
1433 * @ep_idx: The endpoint index for the data
1434 * @size: The size of data in the fifo, in bytes
1435 *
1436 * The FIFO status shows there is data to read from the FIFO for a given
1437 * endpoint, so sort out whether we need to read the data into a request
1438 * that has been made for that endpoint.
1439 */
1440static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1441{
1442 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1443 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001444 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001445 int to_read;
1446 int max_req;
1447 int read_ptr;
1448
Lukasz Majewski22258f42012-06-14 10:02:24 +02001449
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001450 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001451 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001452 int ptr;
1453
1454 dev_warn(hsotg->dev,
1455 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1456 __func__, size, ep_idx, epctl);
1457
1458 /* dump the data from the FIFO, we've nothing we can do */
1459 for (ptr = 0; ptr < size; ptr += 4)
1460 (void)readl(fifo);
1461
1462 return;
1463 }
1464
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001465 to_read = size;
1466 read_ptr = hs_req->req.actual;
1467 max_req = hs_req->req.length - read_ptr;
1468
Ben Dooksa33e7132010-07-19 09:40:49 +01001469 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1470 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1471
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001472 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001473 /*
1474 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001475 * to deal with in this request.
1476 */
1477
1478 /* currently we don't deal this */
1479 WARN_ON_ONCE(1);
1480 }
1481
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001482 hs_ep->total_data += to_read;
1483 hs_req->req.actual += to_read;
1484 to_read = DIV_ROUND_UP(to_read, 4);
1485
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001486 /*
1487 * note, we might over-write the buffer end by 3 bytes depending on
1488 * alignment of the data.
1489 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001490 readsl(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001491}
1492
1493/**
1494 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1495 * @hsotg: The device instance
1496 * @req: The request currently on this endpoint
1497 *
1498 * Generate a zero-length IN packet request for terminating a SETUP
1499 * transaction.
1500 *
1501 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001502 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001503 * the TxFIFO.
1504 */
1505static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1506 struct s3c_hsotg_req *req)
1507{
1508 u32 ctrl;
1509
1510 if (!req) {
1511 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1512 return;
1513 }
1514
1515 if (req->req.length == 0) {
1516 hsotg->eps[0].sent_zlp = 1;
1517 s3c_hsotg_enqueue_setup(hsotg);
1518 return;
1519 }
1520
1521 hsotg->eps[0].dir_in = 1;
1522 hsotg->eps[0].sent_zlp = 1;
1523
1524 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1525
1526 /* issue a zero-sized packet to terminate this */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001527 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
1528 DxEPTSIZ_XferSize(0), hsotg->regs + DIEPTSIZ(0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001529
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001530 ctrl = readl(hsotg->regs + DIEPCTL0);
1531 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
1532 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
1533 ctrl |= DxEPCTL_USBActEp;
1534 writel(ctrl, hsotg->regs + DIEPCTL0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001535}
1536
1537/**
1538 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1539 * @hsotg: The device instance
1540 * @epnum: The endpoint received from
1541 * @was_setup: Set if processing a SetupDone event.
1542 *
1543 * The RXFIFO has delivered an OutDone event, which means that the data
1544 * transfer for an OUT endpoint has been completed, either by a short
1545 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001546 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001547static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1548 int epnum, bool was_setup)
1549{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001550 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001551 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1552 struct s3c_hsotg_req *hs_req = hs_ep->req;
1553 struct usb_request *req = &hs_req->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001554 unsigned size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001555 int result = 0;
1556
1557 if (!hs_req) {
1558 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1559 return;
1560 }
1561
1562 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001563 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001564
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001565 /*
1566 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001567 * is left in the endpoint size register and then working it
1568 * out from the amount we loaded for the transfer.
1569 *
1570 * We need to do this as DMA pointers are always 32bit aligned
1571 * so may overshoot/undershoot the transfer.
1572 */
1573
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001574 size_done = hs_ep->size_loaded - size_left;
1575 size_done += hs_ep->last_load;
1576
1577 req->actual = size_done;
1578 }
1579
Ben Dooksa33e7132010-07-19 09:40:49 +01001580 /* if there is more request to do, schedule new transfer */
1581 if (req->actual < req->length && size_left == 0) {
1582 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1583 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001584 } else if (epnum == 0) {
1585 /*
1586 * After was_setup = 1 =>
1587 * set CNAK for non Setup requests
1588 */
1589 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001590 }
1591
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001592 if (req->actual < req->length && req->short_not_ok) {
1593 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1594 __func__, req->actual, req->length);
1595
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001596 /*
1597 * todo - what should we return here? there's no one else
1598 * even bothering to check the status.
1599 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001600 }
1601
1602 if (epnum == 0) {
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001603 /*
1604 * Condition req->complete != s3c_hsotg_complete_setup says:
1605 * send ZLP when we have an asynchronous request from gadget
1606 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001607 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1608 s3c_hsotg_send_zlp(hsotg, hs_req);
1609 }
1610
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001611 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001612}
1613
1614/**
1615 * s3c_hsotg_read_frameno - read current frame number
1616 * @hsotg: The device instance
1617 *
1618 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001619 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001620static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1621{
1622 u32 dsts;
1623
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001624 dsts = readl(hsotg->regs + DSTS);
1625 dsts &= DSTS_SOFFN_MASK;
1626 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001627
1628 return dsts;
1629}
1630
1631/**
1632 * s3c_hsotg_handle_rx - RX FIFO has data
1633 * @hsotg: The device instance
1634 *
1635 * The IRQ handler has detected that the RX FIFO has some data in it
1636 * that requires processing, so find out what is in there and do the
1637 * appropriate read.
1638 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001639 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001640 * chunks, so if you have x packets received on an endpoint you'll get x
1641 * FIFO events delivered, each with a packet's worth of data in it.
1642 *
1643 * When using DMA, we should not be processing events from the RXFIFO
1644 * as the actual data should be sent to the memory directly and we turn
1645 * on the completion interrupts to get notifications of transfer completion.
1646 */
Mark Brown0978f8c2010-01-18 13:18:35 +00001647static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001648{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001649 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001650 u32 epnum, status, size;
1651
1652 WARN_ON(using_dma(hsotg));
1653
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001654 epnum = grxstsr & GRXSTS_EPNum_MASK;
1655 status = grxstsr & GRXSTS_PktSts_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001656
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001657 size = grxstsr & GRXSTS_ByteCnt_MASK;
1658 size >>= GRXSTS_ByteCnt_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001659
1660 if (1)
1661 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1662 __func__, grxstsr, size, epnum);
1663
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001664#define __status(x) ((x) >> GRXSTS_PktSts_SHIFT)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001665
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001666 switch (status >> GRXSTS_PktSts_SHIFT) {
1667 case __status(GRXSTS_PktSts_GlobalOutNAK):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001668 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1669 break;
1670
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001671 case __status(GRXSTS_PktSts_OutDone):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001672 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1673 s3c_hsotg_read_frameno(hsotg));
1674
1675 if (!using_dma(hsotg))
1676 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1677 break;
1678
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001679 case __status(GRXSTS_PktSts_SetupDone):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001680 dev_dbg(hsotg->dev,
1681 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1682 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001683 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001684
1685 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1686 break;
1687
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001688 case __status(GRXSTS_PktSts_OutRX):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001689 s3c_hsotg_rx_data(hsotg, epnum, size);
1690 break;
1691
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001692 case __status(GRXSTS_PktSts_SetupRX):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001693 dev_dbg(hsotg->dev,
1694 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1695 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001696 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001697
1698 s3c_hsotg_rx_data(hsotg, epnum, size);
1699 break;
1700
1701 default:
1702 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1703 __func__, grxstsr);
1704
1705 s3c_hsotg_dump(hsotg);
1706 break;
1707 }
1708}
1709
1710/**
1711 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1712 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001713 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001714static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1715{
1716 switch (mps) {
1717 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001718 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001719 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001720 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001721 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001722 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001723 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001724 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001725 }
1726
1727 /* bad max packet size, warn and return invalid result */
1728 WARN_ON(1);
1729 return (u32)-1;
1730}
1731
1732/**
1733 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1734 * @hsotg: The driver state.
1735 * @ep: The index number of the endpoint
1736 * @mps: The maximum packet size in bytes
1737 *
1738 * Configure the maximum packet size for the given endpoint, updating
1739 * the hardware control registers to reflect this.
1740 */
1741static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1742 unsigned int ep, unsigned int mps)
1743{
1744 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1745 void __iomem *regs = hsotg->regs;
1746 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001747 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001748 u32 reg;
1749
1750 if (ep == 0) {
1751 /* EP0 is a special case */
1752 mpsval = s3c_hsotg_ep0_mps(mps);
1753 if (mpsval > 3)
1754 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001755 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001756 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001757 } else {
Robert Baldygae9edd1992013-10-09 08:20:02 +02001758 mpsval = mps & DxEPCTL_MPS_MASK;
1759 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001760 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001761 mcval = ((mps >> 11) & 0x3) + 1;
1762 hs_ep->mc = mcval;
1763 if (mcval > 3)
1764 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001765 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001766 }
1767
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001768 /*
1769 * update both the in and out endpoint controldir_ registers, even
1770 * if one of the directions may not be in use.
1771 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001772
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001773 reg = readl(regs + DIEPCTL(ep));
1774 reg &= ~DxEPCTL_MPS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001775 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001776 writel(reg, regs + DIEPCTL(ep));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001777
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001778 if (ep) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001779 reg = readl(regs + DOEPCTL(ep));
1780 reg &= ~DxEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001781 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001782 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001783 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001784
1785 return;
1786
1787bad_mps:
1788 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1789}
1790
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001791/**
1792 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1793 * @hsotg: The driver state
1794 * @idx: The index for the endpoint (0..15)
1795 */
1796static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1797{
1798 int timeout;
1799 int val;
1800
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001801 writel(GRSTCTL_TxFNum(idx) | GRSTCTL_TxFFlsh,
1802 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001803
1804 /* wait until the fifo is flushed */
1805 timeout = 100;
1806
1807 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001808 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001809
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001810 if ((val & (GRSTCTL_TxFFlsh)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001811 break;
1812
1813 if (--timeout == 0) {
1814 dev_err(hsotg->dev,
1815 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1816 __func__, val);
1817 }
1818
1819 udelay(1);
1820 }
1821}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001822
1823/**
1824 * s3c_hsotg_trytx - check to see if anything needs transmitting
1825 * @hsotg: The driver state
1826 * @hs_ep: The driver endpoint to check.
1827 *
1828 * Check to see if there is a request that has data to send, and if so
1829 * make an attempt to write data into the FIFO.
1830 */
1831static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1832 struct s3c_hsotg_ep *hs_ep)
1833{
1834 struct s3c_hsotg_req *hs_req = hs_ep->req;
1835
Robert Baldygaafcf4162013-09-19 11:50:19 +02001836 if (!hs_ep->dir_in || !hs_req) {
1837 /**
1838 * if request is not enqueued, we disable interrupts
1839 * for endpoints, excepting ep0
1840 */
1841 if (hs_ep->index != 0)
1842 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1843 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001844 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001845 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001846
1847 if (hs_req->req.actual < hs_req->req.length) {
1848 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1849 hs_ep->index);
1850 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1851 }
1852
1853 return 0;
1854}
1855
1856/**
1857 * s3c_hsotg_complete_in - complete IN transfer
1858 * @hsotg: The device state.
1859 * @hs_ep: The endpoint that has just completed.
1860 *
1861 * An IN transfer has been completed, update the transfer's state and then
1862 * call the relevant completion routines.
1863 */
1864static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1865 struct s3c_hsotg_ep *hs_ep)
1866{
1867 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001868 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001869 int size_left, size_done;
1870
1871 if (!hs_req) {
1872 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1873 return;
1874 }
1875
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001876 /* Finish ZLP handling for IN EP0 transactions */
1877 if (hsotg->eps[0].sent_zlp) {
1878 dev_dbg(hsotg->dev, "zlp packet received\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001879 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001880 return;
1881 }
1882
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001883 /*
1884 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001885 * in the endpoint size register and then working it out from
1886 * the amount we loaded for the transfer.
1887 *
1888 * We do this even for DMA, as the transfer may have incremented
1889 * past the end of the buffer (DMA transfers are always 32bit
1890 * aligned).
1891 */
1892
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001893 size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001894
1895 size_done = hs_ep->size_loaded - size_left;
1896 size_done += hs_ep->last_load;
1897
1898 if (hs_req->req.actual != size_done)
1899 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1900 __func__, hs_req->req.actual, size_done);
1901
1902 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001903 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1904 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001905
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001906 /*
1907 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1908 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1909 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1910 * inform the host that no more data is available.
1911 * The state of req.zero member is checked to be sure that the value to
1912 * send is smaller than wValue expected from host.
1913 * Check req.length to NOT send another ZLP when the current one is
1914 * under completion (the one for which this completion has been called).
1915 */
1916 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1917 hs_req->req.length == hs_req->req.actual &&
1918 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1919
1920 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1921 s3c_hsotg_send_zlp(hsotg, hs_req);
1922
1923 return;
1924 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001925
1926 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1927 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1928 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1929 } else
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001930 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001931}
1932
1933/**
1934 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1935 * @hsotg: The driver state
1936 * @idx: The index for the endpoint (0..15)
1937 * @dir_in: Set if this is an IN endpoint
1938 *
1939 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001940 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001941static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1942 int dir_in)
1943{
1944 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001945 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1946 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1947 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001948 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02001949 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950
1951 ints = readl(hsotg->regs + epint_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02001952 ctrl = readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001953
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001954 /* Clear endpoint interrupts */
1955 writel(ints, hsotg->regs + epint_reg);
1956
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001957 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1958 __func__, idx, dir_in ? "in" : "out", ints);
1959
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001960 if (ints & DxEPINT_XferCompl) {
Robert Baldyga1479e842013-10-09 08:41:57 +02001961 if (hs_ep->isochronous && hs_ep->interval == 1) {
1962 if (ctrl & DxEPCTL_EOFrNum)
1963 ctrl |= DxEPCTL_SetEvenFr;
1964 else
1965 ctrl |= DxEPCTL_SetOddFr;
1966 writel(ctrl, hsotg->regs + epctl_reg);
1967 }
1968
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001969 dev_dbg(hsotg->dev,
1970 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1971 __func__, readl(hsotg->regs + epctl_reg),
1972 readl(hsotg->regs + epsiz_reg));
1973
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001974 /*
1975 * we get OutDone from the FIFO, so we only need to look
1976 * at completing IN requests here
1977 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001978 if (dir_in) {
1979 s3c_hsotg_complete_in(hsotg, hs_ep);
1980
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001981 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001982 s3c_hsotg_enqueue_setup(hsotg);
1983 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001984 /*
1985 * We're using DMA, we need to fire an OutDone here
1986 * as we ignore the RXFIFO.
1987 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001988
1989 s3c_hsotg_handle_outdone(hsotg, idx, false);
1990 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001991 }
1992
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001993 if (ints & DxEPINT_EPDisbld) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001994 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001995
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001996 if (dir_in) {
1997 int epctl = readl(hsotg->regs + epctl_reg);
1998
1999 s3c_hsotg_txfifo_flush(hsotg, idx);
2000
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002001 if ((epctl & DxEPCTL_Stall) &&
2002 (epctl & DxEPCTL_EPType_Bulk)) {
2003 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002004
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002005 dctl |= DCTL_CGNPInNAK;
2006 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002007 }
2008 }
2009 }
2010
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002011 if (ints & DxEPINT_AHBErr)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002012 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002013
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002014 if (ints & DxEPINT_Setup) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002015 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2016
2017 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002018 /*
2019 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002020 * setup packet. In non-DMA mode we'd get this
2021 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002022 * the setup here.
2023 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002024
2025 if (dir_in)
2026 WARN_ON_ONCE(1);
2027 else
2028 s3c_hsotg_handle_outdone(hsotg, 0, true);
2029 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002030 }
2031
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002032 if (ints & DxEPINT_Back2BackSetup)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002033 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002034
Robert Baldyga1479e842013-10-09 08:41:57 +02002035 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002036 /* not sure if this is important, but we'll clear it anyway */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002037 if (ints & DIEPMSK_INTknTXFEmpMsk) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002038 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2039 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002040 }
2041
2042 /* this probably means something bad is happening */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002043 if (ints & DIEPMSK_INTknEPMisMsk) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002044 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2045 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002046 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002047
2048 /* FIFO has space or is empty (see GAHBCFG) */
2049 if (hsotg->dedicated_fifos &&
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002050 ints & DIEPMSK_TxFIFOEmpty) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002051 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2052 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002053 if (!using_dma(hsotg))
2054 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002055 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002056 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002057}
2058
2059/**
2060 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
2061 * @hsotg: The device state.
2062 *
2063 * Handle updating the device settings after the enumeration phase has
2064 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002065 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002066static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
2067{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002068 u32 dsts = readl(hsotg->regs + DSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002069 int ep0_mps = 0, ep_mps;
2070
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002071 /*
2072 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002073 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002074 * we connected at.
2075 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002076
2077 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2078
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002079 /*
2080 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002081 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002082 * not advertise a 64byte MPS on EP0.
2083 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002084
2085 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002086 switch (dsts & DSTS_EnumSpd_MASK) {
2087 case DSTS_EnumSpd_FS:
2088 case DSTS_EnumSpd_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002089 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002090 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002091 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002092 break;
2093
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002094 case DSTS_EnumSpd_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002095 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002096 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002097 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002098 break;
2099
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002100 case DSTS_EnumSpd_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002101 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002102 /*
2103 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002104 * moment, and the documentation seems to imply that it isn't
2105 * supported by the PHYs on some of the devices.
2106 */
2107 break;
2108 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002109 dev_info(hsotg->dev, "new device is %s\n",
2110 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002111
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002112 /*
2113 * we should now know the maximum packet size for an
2114 * endpoint, so set the endpoints to a default value.
2115 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002116
2117 if (ep0_mps) {
2118 int i;
2119 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002120 for (i = 1; i < hsotg->num_of_eps; i++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002121 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
2122 }
2123
2124 /* ensure after enumeration our EP0 is active */
2125
2126 s3c_hsotg_enqueue_setup(hsotg);
2127
2128 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002129 readl(hsotg->regs + DIEPCTL0),
2130 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002131}
2132
2133/**
2134 * kill_all_requests - remove all requests from the endpoint's queue
2135 * @hsotg: The device state.
2136 * @ep: The endpoint the requests may be on.
2137 * @result: The result code to use.
2138 * @force: Force removal of any current requests
2139 *
2140 * Go through the requests on the given endpoint and mark them
2141 * completed with the given result code.
2142 */
2143static void kill_all_requests(struct s3c_hsotg *hsotg,
2144 struct s3c_hsotg_ep *ep,
2145 int result, bool force)
2146{
2147 struct s3c_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002148
2149 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002150 /*
2151 * currently, we can't do much about an already
2152 * running request on an in endpoint
2153 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002154
2155 if (ep->req == req && ep->dir_in && !force)
2156 continue;
2157
2158 s3c_hsotg_complete_request(hsotg, ep, req,
2159 result);
2160 }
Robert Baldygab963a812013-12-06 13:03:45 +01002161 if(hsotg->dedicated_fifos)
2162 if ((readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4 < 3072)
2163 s3c_hsotg_txfifo_flush(hsotg, ep->index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002164}
2165
2166#define call_gadget(_hs, _entry) \
Pavel Macheka023da32013-09-30 14:56:02 +02002167do { \
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002168 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002169 (_hs)->driver && (_hs)->driver->_entry) { \
2170 spin_unlock(&_hs->lock); \
2171 (_hs)->driver->_entry(&(_hs)->gadget); \
2172 spin_lock(&_hs->lock); \
Pavel Macheka023da32013-09-30 14:56:02 +02002173 } \
2174} while (0)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002175
2176/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002177 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002178 * @hsotg: The device state.
2179 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002180 * The device has been disconnected. Remove all current
2181 * transactions and signal the gadget driver that this
2182 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002183 */
Lukasz Majewski5e891342012-05-04 14:17:07 +02002184static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002185{
2186 unsigned ep;
2187
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002188 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002189 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2190
2191 call_gadget(hsotg, disconnect);
2192}
2193
2194/**
2195 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2196 * @hsotg: The device state:
2197 * @periodic: True if this is a periodic FIFO interrupt
2198 */
2199static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2200{
2201 struct s3c_hsotg_ep *ep;
2202 int epno, ret;
2203
2204 /* look through for any more data to transmit */
2205
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002206 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002207 ep = &hsotg->eps[epno];
2208
2209 if (!ep->dir_in)
2210 continue;
2211
2212 if ((periodic && !ep->periodic) ||
2213 (!periodic && ep->periodic))
2214 continue;
2215
2216 ret = s3c_hsotg_trytx(hsotg, ep);
2217 if (ret < 0)
2218 break;
2219 }
2220}
2221
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002222/* IRQ flags which will trigger a retry around the IRQ loop */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002223#define IRQ_RETRY_MASK (GINTSTS_NPTxFEmp | \
2224 GINTSTS_PTxFEmp | \
2225 GINTSTS_RxFLvl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002226
2227/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002228 * s3c_hsotg_corereset - issue softreset to the core
2229 * @hsotg: The device state
2230 *
2231 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002232 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002233static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2234{
2235 int timeout;
2236 u32 grstctl;
2237
2238 dev_dbg(hsotg->dev, "resetting core\n");
2239
2240 /* issue soft reset */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002241 writel(GRSTCTL_CSftRst, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002242
Du, Changbin2868fea2012-07-24 08:19:25 +08002243 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002244 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002245 grstctl = readl(hsotg->regs + GRSTCTL);
2246 } while ((grstctl & GRSTCTL_CSftRst) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002247
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002248 if (grstctl & GRSTCTL_CSftRst) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002249 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2250 return -EINVAL;
2251 }
2252
Du, Changbin2868fea2012-07-24 08:19:25 +08002253 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002254
2255 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002256 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002257
2258 if (timeout-- < 0) {
2259 dev_info(hsotg->dev,
2260 "%s: reset failed, GRSTCTL=%08x\n",
2261 __func__, grstctl);
2262 return -ETIMEDOUT;
2263 }
2264
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002265 if (!(grstctl & GRSTCTL_AHBIdle))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002266 continue;
2267
2268 break; /* reset done */
2269 }
2270
2271 dev_dbg(hsotg->dev, "reset successful\n");
2272 return 0;
2273}
2274
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002275/**
2276 * s3c_hsotg_core_init - issue softreset to the core
2277 * @hsotg: The device state
2278 *
2279 * Issue a soft reset to the core, and await the core finishing it.
2280 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002281static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2282{
2283 s3c_hsotg_corereset(hsotg);
2284
2285 /*
2286 * we must now enable ep0 ready for host detection and then
2287 * set configuration.
2288 */
2289
2290 /* set the PLL on, remove the HNP/SRP and set the PHY */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002291 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) |
2292 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002293
2294 s3c_hsotg_init_fifo(hsotg);
2295
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002296 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002297
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002298 writel(1 << 18 | DCFG_DevSpd_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002299
2300 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002301 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002302
2303 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002304 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002305
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002306 writel(GINTSTS_ErlySusp | GINTSTS_SessReqInt |
2307 GINTSTS_GOUTNakEff | GINTSTS_GINNakEff |
2308 GINTSTS_ConIDStsChng | GINTSTS_USBRst |
2309 GINTSTS_EnumDone | GINTSTS_OTGInt |
2310 GINTSTS_USBSusp | GINTSTS_WkUpInt,
2311 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002312
2313 if (using_dma(hsotg))
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002314 writel(GAHBCFG_GlblIntrEn | GAHBCFG_DMAEn |
2315 GAHBCFG_HBstLen_Incr4,
2316 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002317 else
Robert Baldyga8acc8292013-09-19 11:50:23 +02002318 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NPTxFEmpLvl |
2319 GAHBCFG_PTxFEmpLvl) : 0) |
2320 GAHBCFG_GlblIntrEn,
2321 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002322
2323 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002324 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2325 * when we have no data to transfer. Otherwise we get being flooded by
2326 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002327 */
2328
Robert Baldyga8acc8292013-09-19 11:50:23 +02002329 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TxFIFOEmpty |
2330 DIEPMSK_INTknTXFEmpMsk : 0) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002331 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk |
2332 DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2333 DIEPMSK_INTknEPMisMsk,
2334 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002335
2336 /*
2337 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2338 * DMA mode we may need this.
2339 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002340 writel((using_dma(hsotg) ? (DIEPMSK_XferComplMsk |
2341 DIEPMSK_TimeOUTMsk) : 0) |
2342 DOEPMSK_EPDisbldMsk | DOEPMSK_AHBErrMsk |
2343 DOEPMSK_SetupMsk,
2344 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002345
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002346 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002347
2348 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002349 readl(hsotg->regs + DIEPCTL0),
2350 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002351
2352 /* enable in and out endpoint interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002353 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPInt | GINTSTS_IEPInt);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002354
2355 /*
2356 * Enable the RXFIFO when in slave mode, as this is how we collect
2357 * the data. In DMA mode, we get events from the FIFO but also
2358 * things we cannot process, so do not use it.
2359 */
2360 if (!using_dma(hsotg))
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002361 s3c_hsotg_en_gsint(hsotg, GINTSTS_RxFLvl);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002362
2363 /* Enable interrupts for EP0 in and out */
2364 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2365 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2366
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002367 __orr32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002368 udelay(10); /* see openiboot */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002369 __bic32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002370
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002371 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002372
2373 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002374 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002375 * writing to the EPCTL register..
2376 */
2377
2378 /* set to read 1 8byte packet */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002379 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
2380 DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002381
2382 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002383 DxEPCTL_CNAK | DxEPCTL_EPEna |
2384 DxEPCTL_USBActEp,
2385 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002386
2387 /* enable, but don't activate EP0in */
2388 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002389 DxEPCTL_USBActEp, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002390
2391 s3c_hsotg_enqueue_setup(hsotg);
2392
2393 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002394 readl(hsotg->regs + DIEPCTL0),
2395 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002396
2397 /* clear global NAKs */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002398 writel(DCTL_CGOUTNak | DCTL_CGNPInNAK,
2399 hsotg->regs + DCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002400
2401 /* must be at-least 3ms to allow bus to see disconnect */
2402 mdelay(3);
2403
2404 /* remove the soft-disconnect and let's go */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002405 __bic32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002406}
2407
2408/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002409 * s3c_hsotg_irq - handle device interrupt
2410 * @irq: The IRQ number triggered
2411 * @pw: The pw value when registered the handler.
2412 */
2413static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2414{
2415 struct s3c_hsotg *hsotg = pw;
2416 int retry_count = 8;
2417 u32 gintsts;
2418 u32 gintmsk;
2419
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002420 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002421irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002422 gintsts = readl(hsotg->regs + GINTSTS);
2423 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002424
2425 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2426 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2427
2428 gintsts &= gintmsk;
2429
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002430 if (gintsts & GINTSTS_OTGInt) {
2431 u32 otgint = readl(hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002432
2433 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2434
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002435 writel(otgint, hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002436 }
2437
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002438 if (gintsts & GINTSTS_SessReqInt) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002439 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002440 writel(GINTSTS_SessReqInt, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002441 }
2442
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002443 if (gintsts & GINTSTS_EnumDone) {
2444 writel(GINTSTS_EnumDone, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002445
2446 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002447 }
2448
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002449 if (gintsts & GINTSTS_ConIDStsChng) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002450 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002451 readl(hsotg->regs + DSTS),
2452 readl(hsotg->regs + GOTGCTL));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002453
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002454 writel(GINTSTS_ConIDStsChng, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002455 }
2456
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002457 if (gintsts & (GINTSTS_OEPInt | GINTSTS_IEPInt)) {
2458 u32 daint = readl(hsotg->regs + DAINT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002459 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2460 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002461 int ep;
2462
Robert Baldyga7e804652013-09-19 11:50:20 +02002463 daint &= daintmsk;
2464 daint_out = daint >> DAINT_OutEP_SHIFT;
2465 daint_in = daint & ~(daint_out << DAINT_OutEP_SHIFT);
2466
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002467 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2468
2469 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2470 if (daint_out & 1)
2471 s3c_hsotg_epint(hsotg, ep, 0);
2472 }
2473
2474 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2475 if (daint_in & 1)
2476 s3c_hsotg_epint(hsotg, ep, 1);
2477 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002478 }
2479
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002480 if (gintsts & GINTSTS_USBRst) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002481
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002482 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002483
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002484 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2485 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002486 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002488 writel(GINTSTS_USBRst, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002489
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002490 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002491 if (time_after(jiffies, hsotg->last_rst +
2492 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002493
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002494 kill_all_requests(hsotg, &hsotg->eps[0],
2495 -ECONNRESET, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002496
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002497 s3c_hsotg_core_init(hsotg);
2498 hsotg->last_rst = jiffies;
2499 }
2500 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002501 }
2502
2503 /* check both FIFOs */
2504
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002505 if (gintsts & GINTSTS_NPTxFEmp) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002506 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2507
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002508 /*
2509 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002510 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002511 * it needs re-enabling
2512 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002513
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002514 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002515 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 }
2517
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002518 if (gintsts & GINTSTS_PTxFEmp) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002519 dev_dbg(hsotg->dev, "PTxFEmp\n");
2520
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002521 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002522
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002523 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002524 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002525 }
2526
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002527 if (gintsts & GINTSTS_RxFLvl) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002528 /*
2529 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002530 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002531 * set.
2532 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002533
2534 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002535 }
2536
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002537 if (gintsts & GINTSTS_ModeMis) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002538 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002539 writel(GINTSTS_ModeMis, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002540 }
2541
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002542 if (gintsts & GINTSTS_USBSusp) {
2543 dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
2544 writel(GINTSTS_USBSusp, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002545
2546 call_gadget(hsotg, suspend);
2547 }
2548
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002549 if (gintsts & GINTSTS_WkUpInt) {
2550 dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
2551 writel(GINTSTS_WkUpInt, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002552
2553 call_gadget(hsotg, resume);
2554 }
2555
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002556 if (gintsts & GINTSTS_ErlySusp) {
2557 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
2558 writel(GINTSTS_ErlySusp, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002559 }
2560
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002561 /*
2562 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002563 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002564 * the occurrence.
2565 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002566
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002567 if (gintsts & GINTSTS_GOUTNakEff) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002568 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2569
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002570 writel(DCTL_CGOUTNak, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002571
2572 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002573 }
2574
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002575 if (gintsts & GINTSTS_GINNakEff) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002576 dev_info(hsotg->dev, "GINNakEff triggered\n");
2577
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002578 writel(DCTL_CGNPInNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002579
2580 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002581 }
2582
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002583 /*
2584 * if we've had fifo events, we should try and go around the
2585 * loop again to see if there's any point in returning yet.
2586 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002587
2588 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2589 goto irq_retry;
2590
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002591 spin_unlock(&hsotg->lock);
2592
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002593 return IRQ_HANDLED;
2594}
2595
2596/**
2597 * s3c_hsotg_ep_enable - enable the given endpoint
2598 * @ep: The USB endpint to configure
2599 * @desc: The USB endpoint descriptor to configure with.
2600 *
2601 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002602 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002603static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2604 const struct usb_endpoint_descriptor *desc)
2605{
2606 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2607 struct s3c_hsotg *hsotg = hs_ep->parent;
2608 unsigned long flags;
2609 int index = hs_ep->index;
2610 u32 epctrl_reg;
2611 u32 epctrl;
2612 u32 mps;
2613 int dir_in;
Julia Lawall19c190f2010-03-29 17:36:44 +02002614 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002615
2616 dev_dbg(hsotg->dev,
2617 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2618 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2619 desc->wMaxPacketSize, desc->bInterval);
2620
2621 /* not to be called for EP0 */
2622 WARN_ON(index == 0);
2623
2624 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2625 if (dir_in != hs_ep->dir_in) {
2626 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2627 return -EINVAL;
2628 }
2629
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002630 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002631
2632 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2633
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002634 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002635 epctrl = readl(hsotg->regs + epctrl_reg);
2636
2637 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2638 __func__, epctrl, epctrl_reg);
2639
Lukasz Majewski22258f42012-06-14 10:02:24 +02002640 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002641
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002642 epctrl &= ~(DxEPCTL_EPType_MASK | DxEPCTL_MPS_MASK);
2643 epctrl |= DxEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002644
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002645 /*
2646 * mark the endpoint as active, otherwise the core may ignore
2647 * transactions entirely for this endpoint
2648 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002649 epctrl |= DxEPCTL_USBActEp;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002650
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002651 /*
2652 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002653 * do something with data that we've yet got a request to process
2654 * since the RXFIFO will take data for an endpoint even if the
2655 * size register hasn't been set.
2656 */
2657
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002658 epctrl |= DxEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002659
2660 /* update the endpoint state */
Robert Baldygae9edd1992013-10-09 08:20:02 +02002661 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002662
2663 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002664 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002665 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002666 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002667 hs_ep->interval = desc->bInterval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002668
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002669 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2670 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2671
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002672 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2673 case USB_ENDPOINT_XFER_ISOC:
Robert Baldyga1479e842013-10-09 08:41:57 +02002674 epctrl |= DxEPCTL_EPType_Iso;
2675 epctrl |= DxEPCTL_SetEvenFr;
2676 hs_ep->isochronous = 1;
2677 if (dir_in)
2678 hs_ep->periodic = 1;
2679 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002680
2681 case USB_ENDPOINT_XFER_BULK:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002682 epctrl |= DxEPCTL_EPType_Bulk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002683 break;
2684
2685 case USB_ENDPOINT_XFER_INT:
2686 if (dir_in) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002687 /*
2688 * Allocate our TxFNum by simply using the index
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002689 * of the endpoint for the moment. We could do
2690 * something better if the host indicates how
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002691 * many FIFOs we are expecting to use.
2692 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002693
2694 hs_ep->periodic = 1;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002695 epctrl |= DxEPCTL_TxFNum(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002696 }
2697
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002698 epctrl |= DxEPCTL_EPType_Intterupt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002699 break;
2700
2701 case USB_ENDPOINT_XFER_CONTROL:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002702 epctrl |= DxEPCTL_EPType_Control;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002703 break;
2704 }
2705
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002706 /*
2707 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002708 * a unique tx-fifo even if it is non-periodic.
2709 */
2710 if (dir_in && hsotg->dedicated_fifos)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002711 epctrl |= DxEPCTL_TxFNum(index);
Ben Dooks10aebc72010-07-19 09:40:44 +01002712
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002713 /* for non control endpoints, set PID to D0 */
2714 if (index)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002715 epctrl |= DxEPCTL_SetD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002716
2717 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2718 __func__, epctrl);
2719
2720 writel(epctrl, hsotg->regs + epctrl_reg);
2721 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2722 __func__, readl(hsotg->regs + epctrl_reg));
2723
2724 /* enable the endpoint interrupt */
2725 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2726
Lukasz Majewski22258f42012-06-14 10:02:24 +02002727 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002728 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002729}
2730
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002731/**
2732 * s3c_hsotg_ep_disable - disable given endpoint
2733 * @ep: The endpoint to disable.
2734 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002735static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2736{
2737 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2738 struct s3c_hsotg *hsotg = hs_ep->parent;
2739 int dir_in = hs_ep->dir_in;
2740 int index = hs_ep->index;
2741 unsigned long flags;
2742 u32 epctrl_reg;
2743 u32 ctrl;
2744
2745 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2746
2747 if (ep == &hsotg->eps[0].ep) {
2748 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2749 return -EINVAL;
2750 }
2751
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002752 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002753
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002754 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002755 /* terminate all requests with shutdown */
2756 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2757
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002758
2759 ctrl = readl(hsotg->regs + epctrl_reg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002760 ctrl &= ~DxEPCTL_EPEna;
2761 ctrl &= ~DxEPCTL_USBActEp;
2762 ctrl |= DxEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002763
2764 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2765 writel(ctrl, hsotg->regs + epctrl_reg);
2766
2767 /* disable endpoint interrupts */
2768 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2769
Lukasz Majewski22258f42012-06-14 10:02:24 +02002770 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002771 return 0;
2772}
2773
2774/**
2775 * on_list - check request is on the given endpoint
2776 * @ep: The endpoint to check.
2777 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002778 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002779static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2780{
2781 struct s3c_hsotg_req *req, *treq;
2782
2783 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2784 if (req == test)
2785 return true;
2786 }
2787
2788 return false;
2789}
2790
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002791/**
2792 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2793 * @ep: The endpoint to dequeue.
2794 * @req: The request to be removed from a queue.
2795 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002796static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2797{
2798 struct s3c_hsotg_req *hs_req = our_req(req);
2799 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2800 struct s3c_hsotg *hs = hs_ep->parent;
2801 unsigned long flags;
2802
2803 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2804
Lukasz Majewski22258f42012-06-14 10:02:24 +02002805 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002806
2807 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002808 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002809 return -EINVAL;
2810 }
2811
2812 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002813 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002814
2815 return 0;
2816}
2817
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002818/**
2819 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2820 * @ep: The endpoint to set halt.
2821 * @value: Set or unset the halt.
2822 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002823static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2824{
2825 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2826 struct s3c_hsotg *hs = hs_ep->parent;
2827 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002828 u32 epreg;
2829 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002830 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002831
2832 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2833
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002834 /* write both IN and OUT control registers */
2835
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002836 epreg = DIEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002837 epctl = readl(hs->regs + epreg);
2838
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002839 if (value) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002840 epctl |= DxEPCTL_Stall + DxEPCTL_SNAK;
2841 if (epctl & DxEPCTL_EPEna)
2842 epctl |= DxEPCTL_EPDis;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002843 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002844 epctl &= ~DxEPCTL_Stall;
2845 xfertype = epctl & DxEPCTL_EPType_MASK;
2846 if (xfertype == DxEPCTL_EPType_Bulk ||
2847 xfertype == DxEPCTL_EPType_Intterupt)
2848 epctl |= DxEPCTL_SetD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002849 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002850
2851 writel(epctl, hs->regs + epreg);
2852
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002853 epreg = DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002854 epctl = readl(hs->regs + epreg);
2855
2856 if (value)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002857 epctl |= DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002858 else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002859 epctl &= ~DxEPCTL_Stall;
2860 xfertype = epctl & DxEPCTL_EPType_MASK;
2861 if (xfertype == DxEPCTL_EPType_Bulk ||
2862 xfertype == DxEPCTL_EPType_Intterupt)
2863 epctl |= DxEPCTL_SetD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002864 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002865
2866 writel(epctl, hs->regs + epreg);
2867
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002868 hs_ep->halted = value;
2869
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002870 return 0;
2871}
2872
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002873/**
2874 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2875 * @ep: The endpoint to set halt.
2876 * @value: Set or unset the halt.
2877 */
2878static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2879{
2880 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2881 struct s3c_hsotg *hs = hs_ep->parent;
2882 unsigned long flags = 0;
2883 int ret = 0;
2884
2885 spin_lock_irqsave(&hs->lock, flags);
2886 ret = s3c_hsotg_ep_sethalt(ep, value);
2887 spin_unlock_irqrestore(&hs->lock, flags);
2888
2889 return ret;
2890}
2891
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002892static struct usb_ep_ops s3c_hsotg_ep_ops = {
2893 .enable = s3c_hsotg_ep_enable,
2894 .disable = s3c_hsotg_ep_disable,
2895 .alloc_request = s3c_hsotg_ep_alloc_request,
2896 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002897 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002898 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002899 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002900 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002901};
2902
2903/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002904 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002905 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002906 *
2907 * A wrapper for platform code responsible for controlling
2908 * low-level USB code
2909 */
2910static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2911{
2912 struct platform_device *pdev = to_platform_device(hsotg->dev);
2913
2914 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302915
Matt Porter74084842013-12-19 09:23:06 -05002916 if (hsotg->phy) {
2917 phy_init(hsotg->phy);
2918 phy_power_on(hsotg->phy);
2919 } else if (hsotg->uphy)
2920 usb_phy_init(hsotg->uphy);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302921 else if (hsotg->plat->phy_init)
Lukasz Majewski41188782012-05-04 14:17:01 +02002922 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2923}
2924
2925/**
2926 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002927 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002928 *
2929 * A wrapper for platform code responsible for controlling
2930 * low-level USB code
2931 */
2932static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2933{
2934 struct platform_device *pdev = to_platform_device(hsotg->dev);
2935
Matt Porter74084842013-12-19 09:23:06 -05002936 if (hsotg->phy) {
2937 phy_power_off(hsotg->phy);
2938 phy_exit(hsotg->phy);
2939 } else if (hsotg->uphy)
2940 usb_phy_shutdown(hsotg->uphy);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302941 else if (hsotg->plat->phy_exit)
Lukasz Majewski41188782012-05-04 14:17:01 +02002942 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2943}
2944
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002945/**
2946 * s3c_hsotg_init - initalize the usb core
2947 * @hsotg: The driver state
2948 */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002949static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2950{
2951 /* unmask subset of endpoint interrupts */
2952
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002953 writel(DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2954 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk,
2955 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002956
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002957 writel(DOEPMSK_SetupMsk | DOEPMSK_AHBErrMsk |
2958 DOEPMSK_EPDisbldMsk | DOEPMSK_XferComplMsk,
2959 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002960
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002961 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002962
2963 /* Be in disconnected state until gadget is registered */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002964 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002965
2966 if (0) {
2967 /* post global nak until we're ready */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002968 writel(DCTL_SGNPInNAK | DCTL_SGOUTNak,
2969 hsotg->regs + DCTL);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002970 }
2971
2972 /* setup fifos */
2973
2974 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002975 readl(hsotg->regs + GRXFSIZ),
2976 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002977
2978 s3c_hsotg_init_fifo(hsotg);
2979
2980 /* set the PLL on, remove the HNP/SRP and set the PHY */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002981 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | (0x5 << 10),
2982 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002983
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002984 writel(using_dma(hsotg) ? GAHBCFG_DMAEn : 0x0,
2985 hsotg->regs + GAHBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002986}
2987
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002988/**
2989 * s3c_hsotg_udc_start - prepare the udc for work
2990 * @gadget: The usb gadget state
2991 * @driver: The usb gadget driver
2992 *
2993 * Perform initialization to prepare udc device and driver
2994 * to work.
2995 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002996static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2997 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002998{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02002999 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003000 int ret;
3001
3002 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003003 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003004 return -ENODEV;
3005 }
3006
3007 if (!driver) {
3008 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3009 return -EINVAL;
3010 }
3011
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003012 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003013 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003014
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003015 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003016 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3017 return -EINVAL;
3018 }
3019
3020 WARN_ON(hsotg->driver);
3021
3022 driver->driver.bus = NULL;
3023 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003024 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003025 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3026
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003027 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3028 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003029 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003030 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003031 goto err;
3032 }
3033
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02003034 hsotg->last_rst = jiffies;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003035 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
3036 return 0;
3037
3038err:
3039 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003040 return ret;
3041}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003042
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003043/**
3044 * s3c_hsotg_udc_stop - stop the udc
3045 * @gadget: The usb gadget state
3046 * @driver: The usb gadget driver
3047 *
3048 * Stop udc hw block and stay tunned for future transmissions
3049 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003050static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
3051 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003052{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02003053 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003054 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003055 int ep;
3056
3057 if (!hsotg)
3058 return -ENODEV;
3059
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003060 /* all endpoints should be shutdown */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003061 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003062 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3063
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003064 spin_lock_irqsave(&hsotg->lock, flags);
3065
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003066 s3c_hsotg_phy_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003067
Marek Szyprowskic8c10252013-09-12 16:18:48 +02003068 if (!driver)
3069 hsotg->driver = NULL;
3070
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003071 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003072
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003073 spin_unlock_irqrestore(&hsotg->lock, flags);
3074
Marek Szyprowskic8c10252013-09-12 16:18:48 +02003075 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003076
3077 return 0;
3078}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003079
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003080/**
3081 * s3c_hsotg_gadget_getframe - read the frame number
3082 * @gadget: The usb gadget state
3083 *
3084 * Read the {micro} frame number
3085 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003086static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
3087{
3088 return s3c_hsotg_read_frameno(to_hsotg(gadget));
3089}
3090
Lukasz Majewskia188b682012-06-22 09:29:56 +02003091/**
3092 * s3c_hsotg_pullup - connect/disconnect the USB PHY
3093 * @gadget: The usb gadget state
3094 * @is_on: Current state of the USB PHY
3095 *
3096 * Connect/Disconnect the USB PHY pullup
3097 */
3098static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
3099{
3100 struct s3c_hsotg *hsotg = to_hsotg(gadget);
3101 unsigned long flags = 0;
3102
3103 dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on);
3104
3105 spin_lock_irqsave(&hsotg->lock, flags);
3106 if (is_on) {
3107 s3c_hsotg_phy_enable(hsotg);
3108 s3c_hsotg_core_init(hsotg);
3109 } else {
3110 s3c_hsotg_disconnect(hsotg);
3111 s3c_hsotg_phy_disable(hsotg);
3112 }
3113
3114 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3115 spin_unlock_irqrestore(&hsotg->lock, flags);
3116
3117 return 0;
3118}
3119
Felipe Balbieeef4582013-01-24 17:58:16 +02003120static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003121 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003122 .udc_start = s3c_hsotg_udc_start,
3123 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02003124 .pullup = s3c_hsotg_pullup,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003125};
3126
3127/**
3128 * s3c_hsotg_initep - initialise a single endpoint
3129 * @hsotg: The device state.
3130 * @hs_ep: The endpoint to be initialised.
3131 * @epnum: The endpoint number
3132 *
3133 * Initialise the given endpoint (as part of the probe and device state
3134 * creation) to give to the gadget driver. Setup the endpoint name, any
3135 * direction information and other state that may be required.
3136 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003137static void s3c_hsotg_initep(struct s3c_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003138 struct s3c_hsotg_ep *hs_ep,
3139 int epnum)
3140{
3141 u32 ptxfifo;
3142 char *dir;
3143
3144 if (epnum == 0)
3145 dir = "";
3146 else if ((epnum % 2) == 0) {
3147 dir = "out";
3148 } else {
3149 dir = "in";
3150 hs_ep->dir_in = 1;
3151 }
3152
3153 hs_ep->index = epnum;
3154
3155 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3156
3157 INIT_LIST_HEAD(&hs_ep->queue);
3158 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3159
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003160 /* add to the list of endpoints known by the gadget driver */
3161 if (epnum)
3162 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3163
3164 hs_ep->parent = hsotg;
3165 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003166 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003167 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3168
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003169 /*
3170 * Read the FIFO size for the Periodic TX FIFO, even if we're
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003171 * an OUT endpoint, we may as well do this if in future the
3172 * code is changed to make each endpoint's direction changeable.
3173 */
3174
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003175 ptxfifo = readl(hsotg->regs + DPTXFSIZn(epnum));
3176 hs_ep->fifo_size = DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003177
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003178 /*
3179 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003180 * to be something valid.
3181 */
3182
3183 if (using_dma(hsotg)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003184 u32 next = DxEPCTL_NextEp((epnum + 1) % 15);
3185 writel(next, hsotg->regs + DIEPCTL(epnum));
3186 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003187 }
3188}
3189
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003190/**
3191 * s3c_hsotg_hw_cfg - read HW configuration registers
3192 * @param: The device state
3193 *
3194 * Read the USB core HW configuration registers
3195 */
3196static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003197{
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003198 u32 cfg2, cfg4;
Ben Dooks10aebc72010-07-19 09:40:44 +01003199 /* check hardware configuration */
3200
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003201 cfg2 = readl(hsotg->regs + 0x48);
3202 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3203
3204 dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
3205
Ben Dooks10aebc72010-07-19 09:40:44 +01003206 cfg4 = readl(hsotg->regs + 0x50);
3207 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3208
3209 dev_info(hsotg->dev, "%s fifos\n",
3210 hsotg->dedicated_fifos ? "dedicated" : "shared");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003211}
3212
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003213/**
3214 * s3c_hsotg_dump - dump state of the udc
3215 * @param: The device state
3216 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003217static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
3218{
Mark Brown83a01802011-06-01 17:16:15 +01003219#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003220 struct device *dev = hsotg->dev;
3221 void __iomem *regs = hsotg->regs;
3222 u32 val;
3223 int idx;
3224
3225 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003226 readl(regs + DCFG), readl(regs + DCTL),
3227 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003228
3229 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003230 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003231
3232 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003233 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003234
3235 /* show periodic fifo settings */
3236
3237 for (idx = 1; idx <= 15; idx++) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003238 val = readl(regs + DPTXFSIZn(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003239 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003240 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3241 val & DPTXFSIZn_DPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003242 }
3243
3244 for (idx = 0; idx < 15; idx++) {
3245 dev_info(dev,
3246 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003247 readl(regs + DIEPCTL(idx)),
3248 readl(regs + DIEPTSIZ(idx)),
3249 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003250
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003251 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003252 dev_info(dev,
3253 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003254 idx, readl(regs + DOEPCTL(idx)),
3255 readl(regs + DOEPTSIZ(idx)),
3256 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003257
3258 }
3259
3260 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003261 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003262#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003263}
3264
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003265/**
3266 * state_show - debugfs: show overall driver and device state.
3267 * @seq: The seq file to write to.
3268 * @v: Unused parameter.
3269 *
3270 * This debugfs entry shows the overall state of the hardware and
3271 * some general information about each of the endpoints available
3272 * to the system.
3273 */
3274static int state_show(struct seq_file *seq, void *v)
3275{
3276 struct s3c_hsotg *hsotg = seq->private;
3277 void __iomem *regs = hsotg->regs;
3278 int idx;
3279
3280 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003281 readl(regs + DCFG),
3282 readl(regs + DCTL),
3283 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003284
3285 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003286 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003287
3288 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003289 readl(regs + GINTMSK),
3290 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003291
3292 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003293 readl(regs + DAINTMSK),
3294 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003295
3296 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003297 readl(regs + GNPTXSTS),
3298 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003299
Pavel Macheka023da32013-09-30 14:56:02 +02003300 seq_puts(seq, "\nEndpoint status:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003301
3302 for (idx = 0; idx < 15; idx++) {
3303 u32 in, out;
3304
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003305 in = readl(regs + DIEPCTL(idx));
3306 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003307
3308 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3309 idx, in, out);
3310
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003311 in = readl(regs + DIEPTSIZ(idx));
3312 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003313
3314 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3315 in, out);
3316
Pavel Macheka023da32013-09-30 14:56:02 +02003317 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003318 }
3319
3320 return 0;
3321}
3322
3323static int state_open(struct inode *inode, struct file *file)
3324{
3325 return single_open(file, state_show, inode->i_private);
3326}
3327
3328static const struct file_operations state_fops = {
3329 .owner = THIS_MODULE,
3330 .open = state_open,
3331 .read = seq_read,
3332 .llseek = seq_lseek,
3333 .release = single_release,
3334};
3335
3336/**
3337 * fifo_show - debugfs: show the fifo information
3338 * @seq: The seq_file to write data to.
3339 * @v: Unused parameter.
3340 *
3341 * Show the FIFO information for the overall fifo and all the
3342 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003343 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003344static int fifo_show(struct seq_file *seq, void *v)
3345{
3346 struct s3c_hsotg *hsotg = seq->private;
3347 void __iomem *regs = hsotg->regs;
3348 u32 val;
3349 int idx;
3350
Pavel Macheka023da32013-09-30 14:56:02 +02003351 seq_puts(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003352 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003353
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003354 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003355 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003356 val >> GNPTXFSIZ_NPTxFDep_SHIFT,
3357 val & GNPTXFSIZ_NPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003358
Pavel Macheka023da32013-09-30 14:56:02 +02003359 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003360
3361 for (idx = 1; idx <= 15; idx++) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003362 val = readl(regs + DPTXFSIZn(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003363
3364 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003365 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3366 val & DPTXFSIZn_DPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003367 }
3368
3369 return 0;
3370}
3371
3372static int fifo_open(struct inode *inode, struct file *file)
3373{
3374 return single_open(file, fifo_show, inode->i_private);
3375}
3376
3377static const struct file_operations fifo_fops = {
3378 .owner = THIS_MODULE,
3379 .open = fifo_open,
3380 .read = seq_read,
3381 .llseek = seq_lseek,
3382 .release = single_release,
3383};
3384
3385
3386static const char *decode_direction(int is_in)
3387{
3388 return is_in ? "in" : "out";
3389}
3390
3391/**
3392 * ep_show - debugfs: show the state of an endpoint.
3393 * @seq: The seq_file to write data to.
3394 * @v: Unused parameter.
3395 *
3396 * This debugfs entry shows the state of the given endpoint (one is
3397 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003398 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003399static int ep_show(struct seq_file *seq, void *v)
3400{
3401 struct s3c_hsotg_ep *ep = seq->private;
3402 struct s3c_hsotg *hsotg = ep->parent;
3403 struct s3c_hsotg_req *req;
3404 void __iomem *regs = hsotg->regs;
3405 int index = ep->index;
3406 int show_limit = 15;
3407 unsigned long flags;
3408
3409 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3410 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3411
3412 /* first show the register state */
3413
3414 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003415 readl(regs + DIEPCTL(index)),
3416 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003417
3418 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003419 readl(regs + DIEPDMA(index)),
3420 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003421
3422 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003423 readl(regs + DIEPINT(index)),
3424 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003425
3426 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003427 readl(regs + DIEPTSIZ(index)),
3428 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003429
Pavel Macheka023da32013-09-30 14:56:02 +02003430 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003431 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3432 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3433
3434 seq_printf(seq, "request list (%p,%p):\n",
3435 ep->queue.next, ep->queue.prev);
3436
Lukasz Majewski22258f42012-06-14 10:02:24 +02003437 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003438
3439 list_for_each_entry(req, &ep->queue, queue) {
3440 if (--show_limit < 0) {
Pavel Macheka023da32013-09-30 14:56:02 +02003441 seq_puts(seq, "not showing more requests...\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003442 break;
3443 }
3444
3445 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3446 req == ep->req ? '*' : ' ',
3447 req, req->req.length, req->req.buf);
3448 seq_printf(seq, "%d done, res %d\n",
3449 req->req.actual, req->req.status);
3450 }
3451
Lukasz Majewski22258f42012-06-14 10:02:24 +02003452 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003453
3454 return 0;
3455}
3456
3457static int ep_open(struct inode *inode, struct file *file)
3458{
3459 return single_open(file, ep_show, inode->i_private);
3460}
3461
3462static const struct file_operations ep_fops = {
3463 .owner = THIS_MODULE,
3464 .open = ep_open,
3465 .read = seq_read,
3466 .llseek = seq_lseek,
3467 .release = single_release,
3468};
3469
3470/**
3471 * s3c_hsotg_create_debug - create debugfs directory and files
3472 * @hsotg: The driver state
3473 *
3474 * Create the debugfs files to allow the user to get information
3475 * about the state of the system. The directory name is created
3476 * with the same name as the device itself, in case we end up
3477 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003478 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003479static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003480{
3481 struct dentry *root;
3482 unsigned epidx;
3483
3484 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3485 hsotg->debug_root = root;
3486 if (IS_ERR(root)) {
3487 dev_err(hsotg->dev, "cannot create debug root\n");
3488 return;
3489 }
3490
3491 /* create general state file */
3492
3493 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3494 hsotg, &state_fops);
3495
3496 if (IS_ERR(hsotg->debug_file))
3497 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3498
3499 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3500 hsotg, &fifo_fops);
3501
3502 if (IS_ERR(hsotg->debug_fifo))
3503 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3504
3505 /* create one file for each endpoint */
3506
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003507 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003508 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3509
3510 ep->debugfs = debugfs_create_file(ep->name, 0444,
3511 root, ep, &ep_fops);
3512
3513 if (IS_ERR(ep->debugfs))
3514 dev_err(hsotg->dev, "failed to create %s debug file\n",
3515 ep->name);
3516 }
3517}
3518
3519/**
3520 * s3c_hsotg_delete_debug - cleanup debugfs entries
3521 * @hsotg: The driver state
3522 *
3523 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003524 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05003525static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003526{
3527 unsigned epidx;
3528
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003529 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003530 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3531 debugfs_remove(ep->debugfs);
3532 }
3533
3534 debugfs_remove(hsotg->debug_file);
3535 debugfs_remove(hsotg->debug_fifo);
3536 debugfs_remove(hsotg->debug_root);
3537}
3538
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003539/**
3540 * s3c_hsotg_probe - probe function for hsotg driver
3541 * @pdev: The platform information for the driver
3542 */
Lukasz Majewskif026a522012-05-04 14:17:13 +02003543
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003544static int s3c_hsotg_probe(struct platform_device *pdev)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003545{
Jingoo Hane01ee9f2013-07-30 17:00:51 +09003546 struct s3c_hsotg_plat *plat = dev_get_platdata(&pdev->dev);
Matt Porter74084842013-12-19 09:23:06 -05003547 struct phy *phy;
3548 struct usb_phy *uphy;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003549 struct device *dev = &pdev->dev;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003550 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003551 struct s3c_hsotg *hsotg;
3552 struct resource *res;
3553 int epnum;
3554 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003555 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003556
Sachin Kamat338edab2012-05-18 14:33:46 +05303557 hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003558 if (!hsotg) {
3559 dev_err(dev, "cannot get memory\n");
3560 return -ENOMEM;
3561 }
3562
Matt Porter74084842013-12-19 09:23:06 -05003563 /*
3564 * Attempt to find a generic PHY, then look for an old style
3565 * USB PHY, finally fall back to pdata
3566 */
3567 phy = devm_phy_get(&pdev->dev, "usb2-phy");
Felipe Balbif4f5ba52013-03-15 10:56:19 +02003568 if (IS_ERR(phy)) {
Matt Porter74084842013-12-19 09:23:06 -05003569 uphy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3570 if (IS_ERR(uphy)) {
3571 /* Fallback for pdata */
3572 plat = dev_get_platdata(&pdev->dev);
3573 if (!plat) {
3574 dev_err(&pdev->dev,
3575 "no platform data or transceiver defined\n");
3576 return -EPROBE_DEFER;
3577 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303578 hsotg->plat = plat;
Matt Porter74084842013-12-19 09:23:06 -05003579 } else
3580 hsotg->uphy = uphy;
3581 } else
Praveen Panerib2e587d2012-11-14 15:57:16 +05303582 hsotg->phy = phy;
Praveen Panerib2e587d2012-11-14 15:57:16 +05303583
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003584 hsotg->dev = dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003585
Sachin Kamat84749c62012-09-03 16:15:18 +05303586 hsotg->clk = devm_clk_get(&pdev->dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003587 if (IS_ERR(hsotg->clk)) {
3588 dev_err(dev, "cannot get otg clock\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303589 return PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003590 }
3591
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003592 platform_set_drvdata(pdev, hsotg);
3593
3594 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003595
Thierry Reding148e1132013-01-21 11:09:22 +01003596 hsotg->regs = devm_ioremap_resource(&pdev->dev, res);
3597 if (IS_ERR(hsotg->regs)) {
3598 ret = PTR_ERR(hsotg->regs);
Sachin Kamat338edab2012-05-18 14:33:46 +05303599 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003600 }
3601
3602 ret = platform_get_irq(pdev, 0);
3603 if (ret < 0) {
3604 dev_err(dev, "cannot find IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303605 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003606 }
3607
Lukasz Majewski22258f42012-06-14 10:02:24 +02003608 spin_lock_init(&hsotg->lock);
3609
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003610 hsotg->irq = ret;
3611
Sachin Kamat338edab2012-05-18 14:33:46 +05303612 ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
3613 dev_name(dev), hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003614 if (ret < 0) {
3615 dev_err(dev, "cannot claim IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303616 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003617 }
3618
3619 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3620
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003621 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003622 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3623 hsotg->gadget.name = dev_name(dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003624
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003625 /* reset the system */
3626
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003627 clk_prepare_enable(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003628
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003629 /* regulators */
3630
3631 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3632 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3633
Sachin Kamatcd762132013-01-08 14:27:00 +05303634 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003635 hsotg->supplies);
3636 if (ret) {
3637 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303638 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003639 }
3640
3641 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3642 hsotg->supplies);
3643
3644 if (ret) {
3645 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3646 goto err_supplies;
3647 }
3648
Matt Porter74084842013-12-19 09:23:06 -05003649 if (hsotg->phy)
3650 phy_init(hsotg->phy);
3651
Lukasz Majewski41188782012-05-04 14:17:01 +02003652 /* usb phy enable */
3653 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003654
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003655 s3c_hsotg_corereset(hsotg);
3656 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003657 s3c_hsotg_hw_cfg(hsotg);
3658
3659 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3660
3661 if (hsotg->num_of_eps == 0) {
3662 dev_err(dev, "wrong number of EPs (zero)\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003663 ret = -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003664 goto err_supplies;
3665 }
3666
3667 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3668 GFP_KERNEL);
3669 if (!eps) {
3670 dev_err(dev, "cannot get memory\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003671 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003672 goto err_supplies;
3673 }
3674
3675 hsotg->eps = eps;
3676
3677 /* setup endpoint information */
3678
3679 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3680 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3681
3682 /* allocate EP0 request */
3683
3684 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3685 GFP_KERNEL);
3686 if (!hsotg->ctrl_req) {
3687 dev_err(dev, "failed to allocate ctrl req\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003688 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003689 goto err_ep_mem;
3690 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003691
3692 /* initialise the endpoints now the core has been initialised */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003693 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003694 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3695
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003696 /* disable power and clock */
3697
3698 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3699 hsotg->supplies);
3700 if (ret) {
3701 dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
3702 goto err_ep_mem;
3703 }
3704
3705 s3c_hsotg_phy_disable(hsotg);
3706
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003707 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3708 if (ret)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003709 goto err_ep_mem;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003710
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003711 s3c_hsotg_create_debug(hsotg);
3712
3713 s3c_hsotg_dump(hsotg);
3714
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003715 return 0;
3716
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003717err_ep_mem:
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003718 kfree(eps);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003719err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003720 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003721err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003722 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05303723
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003724 return ret;
3725}
3726
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003727/**
3728 * s3c_hsotg_remove - remove function for hsotg driver
3729 * @pdev: The platform information for the driver
3730 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05003731static int s3c_hsotg_remove(struct platform_device *pdev)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003732{
3733 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3734
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003735 usb_del_gadget_udc(&hsotg->gadget);
3736
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003737 s3c_hsotg_delete_debug(hsotg);
3738
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003739 if (hsotg->driver) {
3740 /* should have been done already by driver model core */
3741 usb_gadget_unregister_driver(hsotg->driver);
3742 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003743
Lukasz Majewski41188782012-05-04 14:17:01 +02003744 s3c_hsotg_phy_disable(hsotg);
Matt Porter74084842013-12-19 09:23:06 -05003745 if (hsotg->phy)
3746 phy_exit(hsotg->phy);
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003747 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003748
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003749 return 0;
3750}
3751
3752#if 1
3753#define s3c_hsotg_suspend NULL
3754#define s3c_hsotg_resume NULL
3755#endif
3756
Tomasz Figac50f056c2013-06-25 17:38:23 +02003757#ifdef CONFIG_OF
3758static const struct of_device_id s3c_hsotg_of_ids[] = {
3759 { .compatible = "samsung,s3c6400-hsotg", },
Matt Porter0d33d822013-12-19 09:23:05 -05003760 { .compatible = "snps,dwc2", },
Tomasz Figac50f056c2013-06-25 17:38:23 +02003761 { /* sentinel */ }
3762};
3763MODULE_DEVICE_TABLE(of, s3c_hsotg_of_ids);
3764#endif
3765
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003766static struct platform_driver s3c_hsotg_driver = {
3767 .driver = {
3768 .name = "s3c-hsotg",
3769 .owner = THIS_MODULE,
Tomasz Figac50f056c2013-06-25 17:38:23 +02003770 .of_match_table = of_match_ptr(s3c_hsotg_of_ids),
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003771 },
3772 .probe = s3c_hsotg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05003773 .remove = s3c_hsotg_remove,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003774 .suspend = s3c_hsotg_suspend,
3775 .resume = s3c_hsotg_resume,
3776};
3777
Axel Lincc27c962011-11-27 20:16:27 +08003778module_platform_driver(s3c_hsotg_driver);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003779
3780MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3781MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3782MODULE_LICENSE("GPL");
3783MODULE_ALIAS("platform:s3c-hsotg");