1 /* linux/drivers/usb/gadget/s3c-hsotg.c
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4  *		http://www.samsung.com
5  *
6  * Copyright 2008 Openmoko, Inc.
7  * Copyright 2008 Simtec Electronics
8  *      Ben Dooks <ben@simtec.co.uk>
9  *      http://armlinux.simtec.co.uk/
10  *
11  * S3C USB2.0 High-speed / OtG driver
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16 */
17 
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/spinlock.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/delay.h>
27 #include <linux/io.h>
28 #include <linux/slab.h>
29 #include <linux/clk.h>
30 
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 
34 #include <mach/map.h>
35 
36 #include <plat/regs-usb-hsotg-phy.h>
37 #include <plat/regs-usb-hsotg.h>
38 #include <mach/regs-sys.h>
39 #include <plat/udc-hs.h>
40 #include <plat/cpu.h>
41 
42 #define DMA_ADDR_INVALID (~((dma_addr_t)0))
43 
44 /* EP0_MPS_LIMIT
45  *
46  * Unfortunately there seems to be a limit of the amount of data that can
47  * be transferred by IN transactions on EP0. This is either 127 bytes or 3
48  * packets (which practically means 1 packet and 63 bytes of data) when the
49  * MPS is set to 64.
50  *
51  * This means if we are wanting to move >127 bytes of data, we need to
52  * split the transactions up, but just doing one packet at a time does
53  * not work (this may be an implicit DATA0 PID on first packet of the
54  * transaction) and doing 2 packets is outside the controller's limits.
55  *
56  * If we try to lower the MPS size for EP0, then no transfers work properly
57  * for EP0, and the system will fail basic enumeration. As no cause for this
58  * has currently been found, we cannot support any large IN transfers for
59  * EP0.
60  */
61 #define EP0_MPS_LIMIT	64
62 
63 struct s3c_hsotg;
64 struct s3c_hsotg_req;
65 
66 /**
67  * struct s3c_hsotg_ep - driver endpoint definition.
68  * @ep: The gadget layer representation of the endpoint.
69  * @name: The driver generated name for the endpoint.
70  * @queue: Queue of requests for this endpoint.
71  * @parent: Reference back to the parent device structure.
72  * @req: The current request that the endpoint is processing. This is
73  *       used to indicate an request has been loaded onto the endpoint
74  *       and has yet to be completed (maybe due to data move, or simply
75  *	 awaiting an ack from the core all the data has been completed).
76  * @debugfs: File entry for debugfs file for this endpoint.
77  * @lock: State lock to protect contents of endpoint.
78  * @dir_in: Set to true if this endpoint is of the IN direction, which
79  *	    means that it is sending data to the Host.
80  * @index: The index for the endpoint registers.
81  * @name: The name array passed to the USB core.
82  * @halted: Set if the endpoint has been halted.
83  * @periodic: Set if this is a periodic ep, such as Interrupt
84  * @sent_zlp: Set if we've sent a zero-length packet.
85  * @total_data: The total number of data bytes done.
86  * @fifo_size: The size of the FIFO (for periodic IN endpoints)
87  * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
88  * @last_load: The offset of data for the last start of request.
89  * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
90  *
91  * This is the driver's state for each registered enpoint, allowing it
92  * to keep track of transactions that need doing. Each endpoint has a
93  * lock to protect the state, to try and avoid using an overall lock
94  * for the host controller as much as possible.
95  *
96  * For periodic IN endpoints, we have fifo_size and fifo_load to try
97  * and keep track of the amount of data in the periodic FIFO for each
98  * of these as we don't have a status register that tells us how much
99  * is in each of them. (note, this may actually be useless information
100  * as in shared-fifo mode periodic in acts like a single-frame packet
101  * buffer than a fifo)
102  */
103 struct s3c_hsotg_ep {
104 	struct usb_ep		ep;
105 	struct list_head	queue;
106 	struct s3c_hsotg	*parent;
107 	struct s3c_hsotg_req	*req;
108 	struct dentry		*debugfs;
109 
110 	spinlock_t		lock;
111 
112 	unsigned long		total_data;
113 	unsigned int		size_loaded;
114 	unsigned int		last_load;
115 	unsigned int		fifo_load;
116 	unsigned short		fifo_size;
117 
118 	unsigned char		dir_in;
119 	unsigned char		index;
120 
121 	unsigned int		halted:1;
122 	unsigned int		periodic:1;
123 	unsigned int		sent_zlp:1;
124 
125 	char			name[10];
126 };
127 
128 #define S3C_HSOTG_EPS	(8+1)	/* limit to 9 for the moment */
129 
130 /**
131  * struct s3c_hsotg - driver state.
132  * @dev: The parent device supplied to the probe function
133  * @driver: USB gadget driver
134  * @plat: The platform specific configuration data.
135  * @regs: The memory area mapped for accessing registers.
136  * @regs_res: The resource that was allocated when claiming register space.
137  * @irq: The IRQ number we are using
138  * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
139  * @debug_root: root directrory for debugfs.
140  * @debug_file: main status file for debugfs.
141  * @debug_fifo: FIFO status file for debugfs.
142  * @ep0_reply: Request used for ep0 reply.
143  * @ep0_buff: Buffer for EP0 reply data, if needed.
144  * @ctrl_buff: Buffer for EP0 control requests.
145  * @ctrl_req: Request for EP0 control packets.
146  * @eps: The endpoints being supplied to the gadget framework
147  */
148 struct s3c_hsotg {
149 	struct device		 *dev;
150 	struct usb_gadget_driver *driver;
151 	struct s3c_hsotg_plat	 *plat;
152 
153 	void __iomem		*regs;
154 	struct resource		*regs_res;
155 	int			irq;
156 	struct clk		*clk;
157 
158 	unsigned int		dedicated_fifos:1;
159 
160 	struct dentry		*debug_root;
161 	struct dentry		*debug_file;
162 	struct dentry		*debug_fifo;
163 
164 	struct usb_request	*ep0_reply;
165 	struct usb_request	*ctrl_req;
166 	u8			ep0_buff[8];
167 	u8			ctrl_buff[8];
168 
169 	struct usb_gadget	gadget;
170 	struct s3c_hsotg_ep	eps[];
171 };
172 
173 /**
174  * struct s3c_hsotg_req - data transfer request
175  * @req: The USB gadget request
176  * @queue: The list of requests for the endpoint this is queued for.
177  * @in_progress: Has already had size/packets written to core
178  * @mapped: DMA buffer for this request has been mapped via dma_map_single().
179  */
180 struct s3c_hsotg_req {
181 	struct usb_request	req;
182 	struct list_head	queue;
183 	unsigned char		in_progress;
184 	unsigned char		mapped;
185 };
186 
187 /* conversion functions */
our_req(struct usb_request * req)188 static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
189 {
190 	return container_of(req, struct s3c_hsotg_req, req);
191 }
192 
our_ep(struct usb_ep * ep)193 static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
194 {
195 	return container_of(ep, struct s3c_hsotg_ep, ep);
196 }
197 
to_hsotg(struct usb_gadget * gadget)198 static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
199 {
200 	return container_of(gadget, struct s3c_hsotg, gadget);
201 }
202 
__orr32(void __iomem * ptr,u32 val)203 static inline void __orr32(void __iomem *ptr, u32 val)
204 {
205 	writel(readl(ptr) | val, ptr);
206 }
207 
__bic32(void __iomem * ptr,u32 val)208 static inline void __bic32(void __iomem *ptr, u32 val)
209 {
210 	writel(readl(ptr) & ~val, ptr);
211 }
212 
213 /* forward decleration of functions */
214 static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
215 
216 /**
217  * using_dma - return the DMA status of the driver.
218  * @hsotg: The driver state.
219  *
220  * Return true if we're using DMA.
221  *
222  * Currently, we have the DMA support code worked into everywhere
223  * that needs it, but the AMBA DMA implementation in the hardware can
224  * only DMA from 32bit aligned addresses. This means that gadgets such
225  * as the CDC Ethernet cannot work as they often pass packets which are
226  * not 32bit aligned.
227  *
228  * Unfortunately the choice to use DMA or not is global to the controller
229  * and seems to be only settable when the controller is being put through
230  * a core reset. This means we either need to fix the gadgets to take
231  * account of DMA alignment, or add bounce buffers (yuerk).
232  *
233  * Until this issue is sorted out, we always return 'false'.
234  */
using_dma(struct s3c_hsotg * hsotg)235 static inline bool using_dma(struct s3c_hsotg *hsotg)
236 {
237 	return false;	/* support is not complete */
238 }
239 
240 /**
241  * s3c_hsotg_en_gsint - enable one or more of the general interrupt
242  * @hsotg: The device state
243  * @ints: A bitmask of the interrupts to enable
244  */
s3c_hsotg_en_gsint(struct s3c_hsotg * hsotg,u32 ints)245 static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
246 {
247 	u32 gsintmsk = readl(hsotg->regs + S3C_GINTMSK);
248 	u32 new_gsintmsk;
249 
250 	new_gsintmsk = gsintmsk | ints;
251 
252 	if (new_gsintmsk != gsintmsk) {
253 		dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
254 		writel(new_gsintmsk, hsotg->regs + S3C_GINTMSK);
255 	}
256 }
257 
258 /**
259  * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
260  * @hsotg: The device state
261  * @ints: A bitmask of the interrupts to enable
262  */
s3c_hsotg_disable_gsint(struct s3c_hsotg * hsotg,u32 ints)263 static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
264 {
265 	u32 gsintmsk = readl(hsotg->regs + S3C_GINTMSK);
266 	u32 new_gsintmsk;
267 
268 	new_gsintmsk = gsintmsk & ~ints;
269 
270 	if (new_gsintmsk != gsintmsk)
271 		writel(new_gsintmsk, hsotg->regs + S3C_GINTMSK);
272 }
273 
274 /**
275  * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
276  * @hsotg: The device state
277  * @ep: The endpoint index
278  * @dir_in: True if direction is in.
279  * @en: The enable value, true to enable
280  *
281  * Set or clear the mask for an individual endpoint's interrupt
282  * request.
283  */
s3c_hsotg_ctrl_epint(struct s3c_hsotg * hsotg,unsigned int ep,unsigned int dir_in,unsigned int en)284 static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
285 				 unsigned int ep, unsigned int dir_in,
286 				 unsigned int en)
287 {
288 	unsigned long flags;
289 	u32 bit = 1 << ep;
290 	u32 daint;
291 
292 	if (!dir_in)
293 		bit <<= 16;
294 
295 	local_irq_save(flags);
296 	daint = readl(hsotg->regs + S3C_DAINTMSK);
297 	if (en)
298 		daint |= bit;
299 	else
300 		daint &= ~bit;
301 	writel(daint, hsotg->regs + S3C_DAINTMSK);
302 	local_irq_restore(flags);
303 }
304 
305 /**
306  * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
307  * @hsotg: The device instance.
308  */
s3c_hsotg_init_fifo(struct s3c_hsotg * hsotg)309 static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
310 {
311 	unsigned int ep;
312 	unsigned int addr;
313 	unsigned int size;
314 	int timeout;
315 	u32 val;
316 
317 	/* the ryu 2.6.24 release ahs
318 	   writel(0x1C0, hsotg->regs + S3C_GRXFSIZ);
319 	   writel(S3C_GNPTXFSIZ_NPTxFStAddr(0x200) |
320 		S3C_GNPTXFSIZ_NPTxFDep(0x1C0),
321 		hsotg->regs + S3C_GNPTXFSIZ);
322 	*/
323 
324 	/* set FIFO sizes to 2048/1024 */
325 
326 	writel(2048, hsotg->regs + S3C_GRXFSIZ);
327 	writel(S3C_GNPTXFSIZ_NPTxFStAddr(2048) |
328 	       S3C_GNPTXFSIZ_NPTxFDep(1024),
329 	       hsotg->regs + S3C_GNPTXFSIZ);
330 
331 	/* arange all the rest of the TX FIFOs, as some versions of this
332 	 * block have overlapping default addresses. This also ensures
333 	 * that if the settings have been changed, then they are set to
334 	 * known values. */
335 
336 	/* start at the end of the GNPTXFSIZ, rounded up */
337 	addr = 2048 + 1024;
338 	size = 768;
339 
340 	/* currently we allocate TX FIFOs for all possible endpoints,
341 	 * and assume that they are all the same size. */
342 
343 	for (ep = 1; ep <= 15; ep++) {
344 		val = addr;
345 		val |= size << S3C_DPTXFSIZn_DPTxFSize_SHIFT;
346 		addr += size;
347 
348 		writel(val, hsotg->regs + S3C_DPTXFSIZn(ep));
349 	}
350 
351 	/* according to p428 of the design guide, we need to ensure that
352 	 * all fifos are flushed before continuing */
353 
354 	writel(S3C_GRSTCTL_TxFNum(0x10) | S3C_GRSTCTL_TxFFlsh |
355 	       S3C_GRSTCTL_RxFFlsh, hsotg->regs + S3C_GRSTCTL);
356 
357 	/* wait until the fifos are both flushed */
358 	timeout = 100;
359 	while (1) {
360 		val = readl(hsotg->regs + S3C_GRSTCTL);
361 
362 		if ((val & (S3C_GRSTCTL_TxFFlsh | S3C_GRSTCTL_RxFFlsh)) == 0)
363 			break;
364 
365 		if (--timeout == 0) {
366 			dev_err(hsotg->dev,
367 				"%s: timeout flushing fifos (GRSTCTL=%08x)\n",
368 				__func__, val);
369 		}
370 
371 		udelay(1);
372 	}
373 
374 	dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
375 }
376 
377 /**
378  * @ep: USB endpoint to allocate request for.
379  * @flags: Allocation flags
380  *
381  * Allocate a new USB request structure appropriate for the specified endpoint
382  */
s3c_hsotg_ep_alloc_request(struct usb_ep * ep,gfp_t flags)383 static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
384 						      gfp_t flags)
385 {
386 	struct s3c_hsotg_req *req;
387 
388 	req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
389 	if (!req)
390 		return NULL;
391 
392 	INIT_LIST_HEAD(&req->queue);
393 
394 	req->req.dma = DMA_ADDR_INVALID;
395 	return &req->req;
396 }
397 
398 /**
399  * is_ep_periodic - return true if the endpoint is in periodic mode.
400  * @hs_ep: The endpoint to query.
401  *
402  * Returns true if the endpoint is in periodic mode, meaning it is being
403  * used for an Interrupt or ISO transfer.
404  */
is_ep_periodic(struct s3c_hsotg_ep * hs_ep)405 static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
406 {
407 	return hs_ep->periodic;
408 }
409 
410 /**
411  * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
412  * @hsotg: The device state.
413  * @hs_ep: The endpoint for the request
414  * @hs_req: The request being processed.
415  *
416  * This is the reverse of s3c_hsotg_map_dma(), called for the completion
417  * of a request to ensure the buffer is ready for access by the caller.
418 */
s3c_hsotg_unmap_dma(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep,struct s3c_hsotg_req * hs_req)419 static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
420 				struct s3c_hsotg_ep *hs_ep,
421 				struct s3c_hsotg_req *hs_req)
422 {
423 	struct usb_request *req = &hs_req->req;
424 	enum dma_data_direction dir;
425 
426 	dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
427 
428 	/* ignore this if we're not moving any data */
429 	if (hs_req->req.length == 0)
430 		return;
431 
432 	if (hs_req->mapped) {
433 		/* we mapped this, so unmap and remove the dma */
434 
435 		dma_unmap_single(hsotg->dev, req->dma, req->length, dir);
436 
437 		req->dma = DMA_ADDR_INVALID;
438 		hs_req->mapped = 0;
439 	} else {
440 		dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
441 	}
442 }
443 
444 /**
445  * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
446  * @hsotg: The controller state.
447  * @hs_ep: The endpoint we're going to write for.
448  * @hs_req: The request to write data for.
449  *
450  * This is called when the TxFIFO has some space in it to hold a new
451  * transmission and we have something to give it. The actual setup of
452  * the data size is done elsewhere, so all we have to do is to actually
453  * write the data.
454  *
455  * The return value is zero if there is more space (or nothing was done)
456  * otherwise -ENOSPC is returned if the FIFO space was used up.
457  *
458  * This routine is only needed for PIO
459 */
s3c_hsotg_write_fifo(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep,struct s3c_hsotg_req * hs_req)460 static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
461 				struct s3c_hsotg_ep *hs_ep,
462 				struct s3c_hsotg_req *hs_req)
463 {
464 	bool periodic = is_ep_periodic(hs_ep);
465 	u32 gnptxsts = readl(hsotg->regs + S3C_GNPTXSTS);
466 	int buf_pos = hs_req->req.actual;
467 	int to_write = hs_ep->size_loaded;
468 	void *data;
469 	int can_write;
470 	int pkt_round;
471 
472 	to_write -= (buf_pos - hs_ep->last_load);
473 
474 	/* if there's nothing to write, get out early */
475 	if (to_write == 0)
476 		return 0;
477 
478 	if (periodic && !hsotg->dedicated_fifos) {
479 		u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
480 		int size_left;
481 		int size_done;
482 
483 		/* work out how much data was loaded so we can calculate
484 		 * how much data is left in the fifo. */
485 
486 		size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
487 
488 		/* if shared fifo, we cannot write anything until the
489 		 * previous data has been completely sent.
490 		 */
491 		if (hs_ep->fifo_load != 0) {
492 			s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
493 			return -ENOSPC;
494 		}
495 
496 		dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
497 			__func__, size_left,
498 			hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
499 
500 		/* how much of the data has moved */
501 		size_done = hs_ep->size_loaded - size_left;
502 
503 		/* how much data is left in the fifo */
504 		can_write = hs_ep->fifo_load - size_done;
505 		dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
506 			__func__, can_write);
507 
508 		can_write = hs_ep->fifo_size - can_write;
509 		dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
510 			__func__, can_write);
511 
512 		if (can_write <= 0) {
513 			s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
514 			return -ENOSPC;
515 		}
516 	} else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
517 		can_write = readl(hsotg->regs + S3C_DTXFSTS(hs_ep->index));
518 
519 		can_write &= 0xffff;
520 		can_write *= 4;
521 	} else {
522 		if (S3C_GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
523 			dev_dbg(hsotg->dev,
524 				"%s: no queue slots available (0x%08x)\n",
525 				__func__, gnptxsts);
526 
527 			s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
528 			return -ENOSPC;
529 		}
530 
531 		can_write = S3C_GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
532 		can_write *= 4;	/* fifo size is in 32bit quantities. */
533 	}
534 
535 	dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n",
536 		 __func__, gnptxsts, can_write, to_write, hs_ep->ep.maxpacket);
537 
538 	/* limit to 512 bytes of data, it seems at least on the non-periodic
539 	 * FIFO, requests of >512 cause the endpoint to get stuck with a
540 	 * fragment of the end of the transfer in it.
541 	 */
542 	if (can_write > 512)
543 		can_write = 512;
544 
545 	/* limit the write to one max-packet size worth of data, but allow
546 	 * the transfer to return that it did not run out of fifo space
547 	 * doing it. */
548 	if (to_write > hs_ep->ep.maxpacket) {
549 		to_write = hs_ep->ep.maxpacket;
550 
551 		s3c_hsotg_en_gsint(hsotg,
552 				   periodic ? S3C_GINTSTS_PTxFEmp :
553 				   S3C_GINTSTS_NPTxFEmp);
554 	}
555 
556 	/* see if we can write data */
557 
558 	if (to_write > can_write) {
559 		to_write = can_write;
560 		pkt_round = to_write % hs_ep->ep.maxpacket;
561 
562 		/* Not sure, but we probably shouldn't be writing partial
563 		 * packets into the FIFO, so round the write down to an
564 		 * exact number of packets.
565 		 *
566 		 * Note, we do not currently check to see if we can ever
567 		 * write a full packet or not to the FIFO.
568 		 */
569 
570 		if (pkt_round)
571 			to_write -= pkt_round;
572 
573 		/* enable correct FIFO interrupt to alert us when there
574 		 * is more room left. */
575 
576 		s3c_hsotg_en_gsint(hsotg,
577 				   periodic ? S3C_GINTSTS_PTxFEmp :
578 				   S3C_GINTSTS_NPTxFEmp);
579 	}
580 
581 	dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
582 		 to_write, hs_req->req.length, can_write, buf_pos);
583 
584 	if (to_write <= 0)
585 		return -ENOSPC;
586 
587 	hs_req->req.actual = buf_pos + to_write;
588 	hs_ep->total_data += to_write;
589 
590 	if (periodic)
591 		hs_ep->fifo_load += to_write;
592 
593 	to_write = DIV_ROUND_UP(to_write, 4);
594 	data = hs_req->req.buf + buf_pos;
595 
596 	writesl(hsotg->regs + S3C_EPFIFO(hs_ep->index), data, to_write);
597 
598 	return (to_write >= can_write) ? -ENOSPC : 0;
599 }
600 
601 /**
602  * get_ep_limit - get the maximum data legnth for this endpoint
603  * @hs_ep: The endpoint
604  *
605  * Return the maximum data that can be queued in one go on a given endpoint
606  * so that transfers that are too long can be split.
607  */
get_ep_limit(struct s3c_hsotg_ep * hs_ep)608 static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
609 {
610 	int index = hs_ep->index;
611 	unsigned maxsize;
612 	unsigned maxpkt;
613 
614 	if (index != 0) {
615 		maxsize = S3C_DxEPTSIZ_XferSize_LIMIT + 1;
616 		maxpkt = S3C_DxEPTSIZ_PktCnt_LIMIT + 1;
617 	} else {
618 		maxsize = 64+64;
619 		if (hs_ep->dir_in)
620 			maxpkt = S3C_DIEPTSIZ0_PktCnt_LIMIT + 1;
621 		else
622 			maxpkt = 2;
623 	}
624 
625 	/* we made the constant loading easier above by using +1 */
626 	maxpkt--;
627 	maxsize--;
628 
629 	/* constrain by packet count if maxpkts*pktsize is greater
630 	 * than the length register size. */
631 
632 	if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
633 		maxsize = maxpkt * hs_ep->ep.maxpacket;
634 
635 	return maxsize;
636 }
637 
638 /**
639  * s3c_hsotg_start_req - start a USB request from an endpoint's queue
640  * @hsotg: The controller state.
641  * @hs_ep: The endpoint to process a request for
642  * @hs_req: The request to start.
643  * @continuing: True if we are doing more for the current request.
644  *
645  * Start the given request running by setting the endpoint registers
646  * appropriately, and writing any data to the FIFOs.
647  */
s3c_hsotg_start_req(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep,struct s3c_hsotg_req * hs_req,bool continuing)648 static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
649 				struct s3c_hsotg_ep *hs_ep,
650 				struct s3c_hsotg_req *hs_req,
651 				bool continuing)
652 {
653 	struct usb_request *ureq = &hs_req->req;
654 	int index = hs_ep->index;
655 	int dir_in = hs_ep->dir_in;
656 	u32 epctrl_reg;
657 	u32 epsize_reg;
658 	u32 epsize;
659 	u32 ctrl;
660 	unsigned length;
661 	unsigned packets;
662 	unsigned maxreq;
663 
664 	if (index != 0) {
665 		if (hs_ep->req && !continuing) {
666 			dev_err(hsotg->dev, "%s: active request\n", __func__);
667 			WARN_ON(1);
668 			return;
669 		} else if (hs_ep->req != hs_req && continuing) {
670 			dev_err(hsotg->dev,
671 				"%s: continue different req\n", __func__);
672 			WARN_ON(1);
673 			return;
674 		}
675 	}
676 
677 	epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
678 	epsize_reg = dir_in ? S3C_DIEPTSIZ(index) : S3C_DOEPTSIZ(index);
679 
680 	dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
681 		__func__, readl(hsotg->regs + epctrl_reg), index,
682 		hs_ep->dir_in ? "in" : "out");
683 
684 	/* If endpoint is stalled, we will restart request later */
685 	ctrl = readl(hsotg->regs + epctrl_reg);
686 
687 	if (ctrl & S3C_DxEPCTL_Stall) {
688 		dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
689 		return;
690 	}
691 
692 	length = ureq->length - ureq->actual;
693 
694 	if (0)
695 		dev_dbg(hsotg->dev,
696 			"REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
697 			ureq->buf, length, ureq->dma,
698 			ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
699 
700 	maxreq = get_ep_limit(hs_ep);
701 	if (length > maxreq) {
702 		int round = maxreq % hs_ep->ep.maxpacket;
703 
704 		dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
705 			__func__, length, maxreq, round);
706 
707 		/* round down to multiple of packets */
708 		if (round)
709 			maxreq -= round;
710 
711 		length = maxreq;
712 	}
713 
714 	if (length)
715 		packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
716 	else
717 		packets = 1;	/* send one packet if length is zero. */
718 
719 	if (dir_in && index != 0)
720 		epsize = S3C_DxEPTSIZ_MC(1);
721 	else
722 		epsize = 0;
723 
724 	if (index != 0 && ureq->zero) {
725 		/* test for the packets being exactly right for the
726 		 * transfer */
727 
728 		if (length == (packets * hs_ep->ep.maxpacket))
729 			packets++;
730 	}
731 
732 	epsize |= S3C_DxEPTSIZ_PktCnt(packets);
733 	epsize |= S3C_DxEPTSIZ_XferSize(length);
734 
735 	dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
736 		__func__, packets, length, ureq->length, epsize, epsize_reg);
737 
738 	/* store the request as the current one we're doing */
739 	hs_ep->req = hs_req;
740 
741 	/* write size / packets */
742 	writel(epsize, hsotg->regs + epsize_reg);
743 
744 	if (using_dma(hsotg) && !continuing) {
745 		unsigned int dma_reg;
746 
747 		/* write DMA address to control register, buffer already
748 		 * synced by s3c_hsotg_ep_queue().  */
749 
750 		dma_reg = dir_in ? S3C_DIEPDMA(index) : S3C_DOEPDMA(index);
751 		writel(ureq->dma, hsotg->regs + dma_reg);
752 
753 		dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
754 			__func__, ureq->dma, dma_reg);
755 	}
756 
757 	ctrl |= S3C_DxEPCTL_EPEna;	/* ensure ep enabled */
758 	ctrl |= S3C_DxEPCTL_USBActEp;
759 	ctrl |= S3C_DxEPCTL_CNAK;	/* clear NAK set by core */
760 
761 	dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
762 	writel(ctrl, hsotg->regs + epctrl_reg);
763 
764 	/* set these, it seems that DMA support increments past the end
765 	 * of the packet buffer so we need to calculate the length from
766 	 * this information. */
767 	hs_ep->size_loaded = length;
768 	hs_ep->last_load = ureq->actual;
769 
770 	if (dir_in && !using_dma(hsotg)) {
771 		/* set these anyway, we may need them for non-periodic in */
772 		hs_ep->fifo_load = 0;
773 
774 		s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
775 	}
776 
777 	/* clear the INTknTXFEmpMsk when we start request, more as a aide
778 	 * to debugging to see what is going on. */
779 	if (dir_in)
780 		writel(S3C_DIEPMSK_INTknTXFEmpMsk,
781 		       hsotg->regs + S3C_DIEPINT(index));
782 
783 	/* Note, trying to clear the NAK here causes problems with transmit
784 	 * on the S3C6400 ending up with the TXFIFO becoming full. */
785 
786 	/* check ep is enabled */
787 	if (!(readl(hsotg->regs + epctrl_reg) & S3C_DxEPCTL_EPEna))
788 		dev_warn(hsotg->dev,
789 			 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
790 			 index, readl(hsotg->regs + epctrl_reg));
791 
792 	dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
793 		__func__, readl(hsotg->regs + epctrl_reg));
794 }
795 
796 /**
797  * s3c_hsotg_map_dma - map the DMA memory being used for the request
798  * @hsotg: The device state.
799  * @hs_ep: The endpoint the request is on.
800  * @req: The request being processed.
801  *
802  * We've been asked to queue a request, so ensure that the memory buffer
803  * is correctly setup for DMA. If we've been passed an extant DMA address
804  * then ensure the buffer has been synced to memory. If our buffer has no
805  * DMA memory, then we map the memory and mark our request to allow us to
806  * cleanup on completion.
807 */
s3c_hsotg_map_dma(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep,struct usb_request * req)808 static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
809 			     struct s3c_hsotg_ep *hs_ep,
810 			     struct usb_request *req)
811 {
812 	enum dma_data_direction dir;
813 	struct s3c_hsotg_req *hs_req = our_req(req);
814 
815 	dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
816 
817 	/* if the length is zero, ignore the DMA data */
818 	if (hs_req->req.length == 0)
819 		return 0;
820 
821 	if (req->dma == DMA_ADDR_INVALID) {
822 		dma_addr_t dma;
823 
824 		dma = dma_map_single(hsotg->dev, req->buf, req->length, dir);
825 
826 		if (unlikely(dma_mapping_error(hsotg->dev, dma)))
827 			goto dma_error;
828 
829 		if (dma & 3) {
830 			dev_err(hsotg->dev, "%s: unaligned dma buffer\n",
831 				__func__);
832 
833 			dma_unmap_single(hsotg->dev, dma, req->length, dir);
834 			return -EINVAL;
835 		}
836 
837 		hs_req->mapped = 1;
838 		req->dma = dma;
839 	} else {
840 		dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
841 		hs_req->mapped = 0;
842 	}
843 
844 	return 0;
845 
846 dma_error:
847 	dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
848 		__func__, req->buf, req->length);
849 
850 	return -EIO;
851 }
852 
s3c_hsotg_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)853 static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
854 			      gfp_t gfp_flags)
855 {
856 	struct s3c_hsotg_req *hs_req = our_req(req);
857 	struct s3c_hsotg_ep *hs_ep = our_ep(ep);
858 	struct s3c_hsotg *hs = hs_ep->parent;
859 	unsigned long irqflags;
860 	bool first;
861 
862 	dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
863 		ep->name, req, req->length, req->buf, req->no_interrupt,
864 		req->zero, req->short_not_ok);
865 
866 	/* initialise status of the request */
867 	INIT_LIST_HEAD(&hs_req->queue);
868 	req->actual = 0;
869 	req->status = -EINPROGRESS;
870 
871 	/* if we're using DMA, sync the buffers as necessary */
872 	if (using_dma(hs)) {
873 		int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
874 		if (ret)
875 			return ret;
876 	}
877 
878 	spin_lock_irqsave(&hs_ep->lock, irqflags);
879 
880 	first = list_empty(&hs_ep->queue);
881 	list_add_tail(&hs_req->queue, &hs_ep->queue);
882 
883 	if (first)
884 		s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
885 
886 	spin_unlock_irqrestore(&hs_ep->lock, irqflags);
887 
888 	return 0;
889 }
890 
s3c_hsotg_ep_free_request(struct usb_ep * ep,struct usb_request * req)891 static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
892 				      struct usb_request *req)
893 {
894 	struct s3c_hsotg_req *hs_req = our_req(req);
895 
896 	kfree(hs_req);
897 }
898 
899 /**
900  * s3c_hsotg_complete_oursetup - setup completion callback
901  * @ep: The endpoint the request was on.
902  * @req: The request completed.
903  *
904  * Called on completion of any requests the driver itself
905  * submitted that need cleaning up.
906  */
s3c_hsotg_complete_oursetup(struct usb_ep * ep,struct usb_request * req)907 static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
908 					struct usb_request *req)
909 {
910 	struct s3c_hsotg_ep *hs_ep = our_ep(ep);
911 	struct s3c_hsotg *hsotg = hs_ep->parent;
912 
913 	dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
914 
915 	s3c_hsotg_ep_free_request(ep, req);
916 }
917 
918 /**
919  * ep_from_windex - convert control wIndex value to endpoint
920  * @hsotg: The driver state.
921  * @windex: The control request wIndex field (in host order).
922  *
923  * Convert the given wIndex into a pointer to an driver endpoint
924  * structure, or return NULL if it is not a valid endpoint.
925 */
ep_from_windex(struct s3c_hsotg * hsotg,u32 windex)926 static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
927 					   u32 windex)
928 {
929 	struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
930 	int dir = (windex & USB_DIR_IN) ? 1 : 0;
931 	int idx = windex & 0x7F;
932 
933 	if (windex >= 0x100)
934 		return NULL;
935 
936 	if (idx > S3C_HSOTG_EPS)
937 		return NULL;
938 
939 	if (idx && ep->dir_in != dir)
940 		return NULL;
941 
942 	return ep;
943 }
944 
945 /**
946  * s3c_hsotg_send_reply - send reply to control request
947  * @hsotg: The device state
948  * @ep: Endpoint 0
949  * @buff: Buffer for request
950  * @length: Length of reply.
951  *
952  * Create a request and queue it on the given endpoint. This is useful as
953  * an internal method of sending replies to certain control requests, etc.
954  */
s3c_hsotg_send_reply(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * ep,void * buff,int length)955 static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
956 				struct s3c_hsotg_ep *ep,
957 				void *buff,
958 				int length)
959 {
960 	struct usb_request *req;
961 	int ret;
962 
963 	dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
964 
965 	req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
966 	hsotg->ep0_reply = req;
967 	if (!req) {
968 		dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
969 		return -ENOMEM;
970 	}
971 
972 	req->buf = hsotg->ep0_buff;
973 	req->length = length;
974 	req->zero = 1; /* always do zero-length final transfer */
975 	req->complete = s3c_hsotg_complete_oursetup;
976 
977 	if (length)
978 		memcpy(req->buf, buff, length);
979 	else
980 		ep->sent_zlp = 1;
981 
982 	ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
983 	if (ret) {
984 		dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
985 		return ret;
986 	}
987 
988 	return 0;
989 }
990 
991 /**
992  * s3c_hsotg_process_req_status - process request GET_STATUS
993  * @hsotg: The device state
994  * @ctrl: USB control request
995  */
s3c_hsotg_process_req_status(struct s3c_hsotg * hsotg,struct usb_ctrlrequest * ctrl)996 static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
997 					struct usb_ctrlrequest *ctrl)
998 {
999 	struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1000 	struct s3c_hsotg_ep *ep;
1001 	__le16 reply;
1002 	int ret;
1003 
1004 	dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1005 
1006 	if (!ep0->dir_in) {
1007 		dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1008 		return -EINVAL;
1009 	}
1010 
1011 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
1012 	case USB_RECIP_DEVICE:
1013 		reply = cpu_to_le16(0); /* bit 0 => self powered,
1014 					 * bit 1 => remote wakeup */
1015 		break;
1016 
1017 	case USB_RECIP_INTERFACE:
1018 		/* currently, the data result should be zero */
1019 		reply = cpu_to_le16(0);
1020 		break;
1021 
1022 	case USB_RECIP_ENDPOINT:
1023 		ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1024 		if (!ep)
1025 			return -ENOENT;
1026 
1027 		reply = cpu_to_le16(ep->halted ? 1 : 0);
1028 		break;
1029 
1030 	default:
1031 		return 0;
1032 	}
1033 
1034 	if (le16_to_cpu(ctrl->wLength) != 2)
1035 		return -EINVAL;
1036 
1037 	ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1038 	if (ret) {
1039 		dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1040 		return ret;
1041 	}
1042 
1043 	return 1;
1044 }
1045 
1046 static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1047 
1048 /**
1049  * get_ep_head - return the first request on the endpoint
1050  * @hs_ep: The controller endpoint to get
1051  *
1052  * Get the first request on the endpoint.
1053  */
get_ep_head(struct s3c_hsotg_ep * hs_ep)1054 static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1055 {
1056 	if (list_empty(&hs_ep->queue))
1057 		return NULL;
1058 
1059 	return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1060 }
1061 
1062 /**
1063  * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1064  * @hsotg: The device state
1065  * @ctrl: USB control request
1066  */
s3c_hsotg_process_req_feature(struct s3c_hsotg * hsotg,struct usb_ctrlrequest * ctrl)1067 static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1068 					 struct usb_ctrlrequest *ctrl)
1069 {
1070 	struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1071 	struct s3c_hsotg_req *hs_req;
1072 	bool restart;
1073 	bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1074 	struct s3c_hsotg_ep *ep;
1075 	int ret;
1076 
1077 	dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1078 		__func__, set ? "SET" : "CLEAR");
1079 
1080 	if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1081 		ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1082 		if (!ep) {
1083 			dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1084 				__func__, le16_to_cpu(ctrl->wIndex));
1085 			return -ENOENT;
1086 		}
1087 
1088 		switch (le16_to_cpu(ctrl->wValue)) {
1089 		case USB_ENDPOINT_HALT:
1090 			s3c_hsotg_ep_sethalt(&ep->ep, set);
1091 
1092 			ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1093 			if (ret) {
1094 				dev_err(hsotg->dev,
1095 					"%s: failed to send reply\n", __func__);
1096 				return ret;
1097 			}
1098 
1099 			if (!set) {
1100 				/*
1101 				 * If we have request in progress,
1102 				 * then complete it
1103 				 */
1104 				if (ep->req) {
1105 					hs_req = ep->req;
1106 					ep->req = NULL;
1107 					list_del_init(&hs_req->queue);
1108 					hs_req->req.complete(&ep->ep,
1109 							     &hs_req->req);
1110 				}
1111 
1112 				/* If we have pending request, then start it */
1113 				restart = !list_empty(&ep->queue);
1114 				if (restart) {
1115 					hs_req = get_ep_head(ep);
1116 					s3c_hsotg_start_req(hsotg, ep,
1117 							    hs_req, false);
1118 				}
1119 			}
1120 
1121 			break;
1122 
1123 		default:
1124 			return -ENOENT;
1125 		}
1126 	} else
1127 		return -ENOENT;  /* currently only deal with endpoint */
1128 
1129 	return 1;
1130 }
1131 
1132 /**
1133  * s3c_hsotg_process_control - process a control request
1134  * @hsotg: The device state
1135  * @ctrl: The control request received
1136  *
1137  * The controller has received the SETUP phase of a control request, and
1138  * needs to work out what to do next (and whether to pass it on to the
1139  * gadget driver).
1140  */
s3c_hsotg_process_control(struct s3c_hsotg * hsotg,struct usb_ctrlrequest * ctrl)1141 static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1142 				      struct usb_ctrlrequest *ctrl)
1143 {
1144 	struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1145 	int ret = 0;
1146 	u32 dcfg;
1147 
1148 	ep0->sent_zlp = 0;
1149 
1150 	dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1151 		 ctrl->bRequest, ctrl->bRequestType,
1152 		 ctrl->wValue, ctrl->wLength);
1153 
1154 	/* record the direction of the request, for later use when enquing
1155 	 * packets onto EP0. */
1156 
1157 	ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1158 	dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1159 
1160 	/* if we've no data with this request, then the last part of the
1161 	 * transaction is going to implicitly be IN. */
1162 	if (ctrl->wLength == 0)
1163 		ep0->dir_in = 1;
1164 
1165 	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1166 		switch (ctrl->bRequest) {
1167 		case USB_REQ_SET_ADDRESS:
1168 			dcfg = readl(hsotg->regs + S3C_DCFG);
1169 			dcfg &= ~S3C_DCFG_DevAddr_MASK;
1170 			dcfg |= ctrl->wValue << S3C_DCFG_DevAddr_SHIFT;
1171 			writel(dcfg, hsotg->regs + S3C_DCFG);
1172 
1173 			dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1174 
1175 			ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1176 			return;
1177 
1178 		case USB_REQ_GET_STATUS:
1179 			ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1180 			break;
1181 
1182 		case USB_REQ_CLEAR_FEATURE:
1183 		case USB_REQ_SET_FEATURE:
1184 			ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1185 			break;
1186 		}
1187 	}
1188 
1189 	/* as a fallback, try delivering it to the driver to deal with */
1190 
1191 	if (ret == 0 && hsotg->driver) {
1192 		ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1193 		if (ret < 0)
1194 			dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1195 	}
1196 
1197 	/* the request is either unhandlable, or is not formatted correctly
1198 	 * so respond with a STALL for the status stage to indicate failure.
1199 	 */
1200 
1201 	if (ret < 0) {
1202 		u32 reg;
1203 		u32 ctrl;
1204 
1205 		dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1206 		reg = (ep0->dir_in) ? S3C_DIEPCTL0 : S3C_DOEPCTL0;
1207 
1208 		/* S3C_DxEPCTL_Stall will be cleared by EP once it has
1209 		 * taken effect, so no need to clear later. */
1210 
1211 		ctrl = readl(hsotg->regs + reg);
1212 		ctrl |= S3C_DxEPCTL_Stall;
1213 		ctrl |= S3C_DxEPCTL_CNAK;
1214 		writel(ctrl, hsotg->regs + reg);
1215 
1216 		dev_dbg(hsotg->dev,
1217 			"written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
1218 			ctrl, reg, readl(hsotg->regs + reg));
1219 
1220 		/* don't believe we need to anything more to get the EP
1221 		 * to reply with a STALL packet */
1222 	}
1223 }
1224 
1225 static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
1226 
1227 /**
1228  * s3c_hsotg_complete_setup - completion of a setup transfer
1229  * @ep: The endpoint the request was on.
1230  * @req: The request completed.
1231  *
1232  * Called on completion of any requests the driver itself submitted for
1233  * EP0 setup packets
1234  */
s3c_hsotg_complete_setup(struct usb_ep * ep,struct usb_request * req)1235 static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1236 				     struct usb_request *req)
1237 {
1238 	struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1239 	struct s3c_hsotg *hsotg = hs_ep->parent;
1240 
1241 	if (req->status < 0) {
1242 		dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1243 		return;
1244 	}
1245 
1246 	if (req->actual == 0)
1247 		s3c_hsotg_enqueue_setup(hsotg);
1248 	else
1249 		s3c_hsotg_process_control(hsotg, req->buf);
1250 }
1251 
1252 /**
1253  * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1254  * @hsotg: The device state.
1255  *
1256  * Enqueue a request on EP0 if necessary to received any SETUP packets
1257  * received from the host.
1258  */
s3c_hsotg_enqueue_setup(struct s3c_hsotg * hsotg)1259 static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1260 {
1261 	struct usb_request *req = hsotg->ctrl_req;
1262 	struct s3c_hsotg_req *hs_req = our_req(req);
1263 	int ret;
1264 
1265 	dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1266 
1267 	req->zero = 0;
1268 	req->length = 8;
1269 	req->buf = hsotg->ctrl_buff;
1270 	req->complete = s3c_hsotg_complete_setup;
1271 
1272 	if (!list_empty(&hs_req->queue)) {
1273 		dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1274 		return;
1275 	}
1276 
1277 	hsotg->eps[0].dir_in = 0;
1278 
1279 	ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1280 	if (ret < 0) {
1281 		dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
1282 		/* Don't think there's much we can do other than watch the
1283 		 * driver fail. */
1284 	}
1285 }
1286 
1287 /**
1288  * s3c_hsotg_complete_request - complete a request given to us
1289  * @hsotg: The device state.
1290  * @hs_ep: The endpoint the request was on.
1291  * @hs_req: The request to complete.
1292  * @result: The result code (0 => Ok, otherwise errno)
1293  *
1294  * The given request has finished, so call the necessary completion
1295  * if it has one and then look to see if we can start a new request
1296  * on the endpoint.
1297  *
1298  * Note, expects the ep to already be locked as appropriate.
1299 */
s3c_hsotg_complete_request(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep,struct s3c_hsotg_req * hs_req,int result)1300 static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1301 				       struct s3c_hsotg_ep *hs_ep,
1302 				       struct s3c_hsotg_req *hs_req,
1303 				       int result)
1304 {
1305 	bool restart;
1306 
1307 	if (!hs_req) {
1308 		dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1309 		return;
1310 	}
1311 
1312 	dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1313 		hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1314 
1315 	/* only replace the status if we've not already set an error
1316 	 * from a previous transaction */
1317 
1318 	if (hs_req->req.status == -EINPROGRESS)
1319 		hs_req->req.status = result;
1320 
1321 	hs_ep->req = NULL;
1322 	list_del_init(&hs_req->queue);
1323 
1324 	if (using_dma(hsotg))
1325 		s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1326 
1327 	/* call the complete request with the locks off, just in case the
1328 	 * request tries to queue more work for this endpoint. */
1329 
1330 	if (hs_req->req.complete) {
1331 		spin_unlock(&hs_ep->lock);
1332 		hs_req->req.complete(&hs_ep->ep, &hs_req->req);
1333 		spin_lock(&hs_ep->lock);
1334 	}
1335 
1336 	/* Look to see if there is anything else to do. Note, the completion
1337 	 * of the previous request may have caused a new request to be started
1338 	 * so be careful when doing this. */
1339 
1340 	if (!hs_ep->req && result >= 0) {
1341 		restart = !list_empty(&hs_ep->queue);
1342 		if (restart) {
1343 			hs_req = get_ep_head(hs_ep);
1344 			s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1345 		}
1346 	}
1347 }
1348 
1349 /**
1350  * s3c_hsotg_complete_request_lock - complete a request given to us (locked)
1351  * @hsotg: The device state.
1352  * @hs_ep: The endpoint the request was on.
1353  * @hs_req: The request to complete.
1354  * @result: The result code (0 => Ok, otherwise errno)
1355  *
1356  * See s3c_hsotg_complete_request(), but called with the endpoint's
1357  * lock held.
1358 */
s3c_hsotg_complete_request_lock(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep,struct s3c_hsotg_req * hs_req,int result)1359 static void s3c_hsotg_complete_request_lock(struct s3c_hsotg *hsotg,
1360 					    struct s3c_hsotg_ep *hs_ep,
1361 					    struct s3c_hsotg_req *hs_req,
1362 					    int result)
1363 {
1364 	unsigned long flags;
1365 
1366 	spin_lock_irqsave(&hs_ep->lock, flags);
1367 	s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
1368 	spin_unlock_irqrestore(&hs_ep->lock, flags);
1369 }
1370 
1371 /**
1372  * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1373  * @hsotg: The device state.
1374  * @ep_idx: The endpoint index for the data
1375  * @size: The size of data in the fifo, in bytes
1376  *
1377  * The FIFO status shows there is data to read from the FIFO for a given
1378  * endpoint, so sort out whether we need to read the data into a request
1379  * that has been made for that endpoint.
1380  */
s3c_hsotg_rx_data(struct s3c_hsotg * hsotg,int ep_idx,int size)1381 static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1382 {
1383 	struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1384 	struct s3c_hsotg_req *hs_req = hs_ep->req;
1385 	void __iomem *fifo = hsotg->regs + S3C_EPFIFO(ep_idx);
1386 	int to_read;
1387 	int max_req;
1388 	int read_ptr;
1389 
1390 	if (!hs_req) {
1391 		u32 epctl = readl(hsotg->regs + S3C_DOEPCTL(ep_idx));
1392 		int ptr;
1393 
1394 		dev_warn(hsotg->dev,
1395 			 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1396 			 __func__, size, ep_idx, epctl);
1397 
1398 		/* dump the data from the FIFO, we've nothing we can do */
1399 		for (ptr = 0; ptr < size; ptr += 4)
1400 			(void)readl(fifo);
1401 
1402 		return;
1403 	}
1404 
1405 	spin_lock(&hs_ep->lock);
1406 
1407 	to_read = size;
1408 	read_ptr = hs_req->req.actual;
1409 	max_req = hs_req->req.length - read_ptr;
1410 
1411 	dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1412 		__func__, to_read, max_req, read_ptr, hs_req->req.length);
1413 
1414 	if (to_read > max_req) {
1415 		/* more data appeared than we where willing
1416 		 * to deal with in this request.
1417 		 */
1418 
1419 		/* currently we don't deal this */
1420 		WARN_ON_ONCE(1);
1421 	}
1422 
1423 	hs_ep->total_data += to_read;
1424 	hs_req->req.actual += to_read;
1425 	to_read = DIV_ROUND_UP(to_read, 4);
1426 
1427 	/* note, we might over-write the buffer end by 3 bytes depending on
1428 	 * alignment of the data. */
1429 	readsl(fifo, hs_req->req.buf + read_ptr, to_read);
1430 
1431 	spin_unlock(&hs_ep->lock);
1432 }
1433 
1434 /**
1435  * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1436  * @hsotg: The device instance
1437  * @req: The request currently on this endpoint
1438  *
1439  * Generate a zero-length IN packet request for terminating a SETUP
1440  * transaction.
1441  *
1442  * Note, since we don't write any data to the TxFIFO, then it is
1443  * currently believed that we do not need to wait for any space in
1444  * the TxFIFO.
1445  */
s3c_hsotg_send_zlp(struct s3c_hsotg * hsotg,struct s3c_hsotg_req * req)1446 static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1447 			       struct s3c_hsotg_req *req)
1448 {
1449 	u32 ctrl;
1450 
1451 	if (!req) {
1452 		dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1453 		return;
1454 	}
1455 
1456 	if (req->req.length == 0) {
1457 		hsotg->eps[0].sent_zlp = 1;
1458 		s3c_hsotg_enqueue_setup(hsotg);
1459 		return;
1460 	}
1461 
1462 	hsotg->eps[0].dir_in = 1;
1463 	hsotg->eps[0].sent_zlp = 1;
1464 
1465 	dev_dbg(hsotg->dev, "sending zero-length packet\n");
1466 
1467 	/* issue a zero-sized packet to terminate this */
1468 	writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
1469 	       S3C_DxEPTSIZ_XferSize(0), hsotg->regs + S3C_DIEPTSIZ(0));
1470 
1471 	ctrl = readl(hsotg->regs + S3C_DIEPCTL0);
1472 	ctrl |= S3C_DxEPCTL_CNAK;  /* clear NAK set by core */
1473 	ctrl |= S3C_DxEPCTL_EPEna; /* ensure ep enabled */
1474 	ctrl |= S3C_DxEPCTL_USBActEp;
1475 	writel(ctrl, hsotg->regs + S3C_DIEPCTL0);
1476 }
1477 
1478 /**
1479  * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1480  * @hsotg: The device instance
1481  * @epnum: The endpoint received from
1482  * @was_setup: Set if processing a SetupDone event.
1483  *
1484  * The RXFIFO has delivered an OutDone event, which means that the data
1485  * transfer for an OUT endpoint has been completed, either by a short
1486  * packet or by the finish of a transfer.
1487 */
s3c_hsotg_handle_outdone(struct s3c_hsotg * hsotg,int epnum,bool was_setup)1488 static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1489 				     int epnum, bool was_setup)
1490 {
1491 	u32 epsize = readl(hsotg->regs + S3C_DOEPTSIZ(epnum));
1492 	struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1493 	struct s3c_hsotg_req *hs_req = hs_ep->req;
1494 	struct usb_request *req = &hs_req->req;
1495 	unsigned size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
1496 	int result = 0;
1497 
1498 	if (!hs_req) {
1499 		dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1500 		return;
1501 	}
1502 
1503 	if (using_dma(hsotg)) {
1504 		unsigned size_done;
1505 
1506 		/* Calculate the size of the transfer by checking how much
1507 		 * is left in the endpoint size register and then working it
1508 		 * out from the amount we loaded for the transfer.
1509 		 *
1510 		 * We need to do this as DMA pointers are always 32bit aligned
1511 		 * so may overshoot/undershoot the transfer.
1512 		 */
1513 
1514 		size_done = hs_ep->size_loaded - size_left;
1515 		size_done += hs_ep->last_load;
1516 
1517 		req->actual = size_done;
1518 	}
1519 
1520 	/* if there is more request to do, schedule new transfer */
1521 	if (req->actual < req->length && size_left == 0) {
1522 		s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1523 		return;
1524 	}
1525 
1526 	if (req->actual < req->length && req->short_not_ok) {
1527 		dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1528 			__func__, req->actual, req->length);
1529 
1530 		/* todo - what should we return here? there's no one else
1531 		 * even bothering to check the status. */
1532 	}
1533 
1534 	if (epnum == 0) {
1535 		if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1536 			s3c_hsotg_send_zlp(hsotg, hs_req);
1537 	}
1538 
1539 	s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, result);
1540 }
1541 
1542 /**
1543  * s3c_hsotg_read_frameno - read current frame number
1544  * @hsotg: The device instance
1545  *
1546  * Return the current frame number
1547 */
s3c_hsotg_read_frameno(struct s3c_hsotg * hsotg)1548 static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1549 {
1550 	u32 dsts;
1551 
1552 	dsts = readl(hsotg->regs + S3C_DSTS);
1553 	dsts &= S3C_DSTS_SOFFN_MASK;
1554 	dsts >>= S3C_DSTS_SOFFN_SHIFT;
1555 
1556 	return dsts;
1557 }
1558 
1559 /**
1560  * s3c_hsotg_handle_rx - RX FIFO has data
1561  * @hsotg: The device instance
1562  *
1563  * The IRQ handler has detected that the RX FIFO has some data in it
1564  * that requires processing, so find out what is in there and do the
1565  * appropriate read.
1566  *
1567  * The RXFIFO is a true FIFO, the packets coming out are still in packet
1568  * chunks, so if you have x packets received on an endpoint you'll get x
1569  * FIFO events delivered, each with a packet's worth of data in it.
1570  *
1571  * When using DMA, we should not be processing events from the RXFIFO
1572  * as the actual data should be sent to the memory directly and we turn
1573  * on the completion interrupts to get notifications of transfer completion.
1574  */
s3c_hsotg_handle_rx(struct s3c_hsotg * hsotg)1575 static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
1576 {
1577 	u32 grxstsr = readl(hsotg->regs + S3C_GRXSTSP);
1578 	u32 epnum, status, size;
1579 
1580 	WARN_ON(using_dma(hsotg));
1581 
1582 	epnum = grxstsr & S3C_GRXSTS_EPNum_MASK;
1583 	status = grxstsr & S3C_GRXSTS_PktSts_MASK;
1584 
1585 	size = grxstsr & S3C_GRXSTS_ByteCnt_MASK;
1586 	size >>= S3C_GRXSTS_ByteCnt_SHIFT;
1587 
1588 	if (1)
1589 		dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1590 			__func__, grxstsr, size, epnum);
1591 
1592 #define __status(x) ((x) >> S3C_GRXSTS_PktSts_SHIFT)
1593 
1594 	switch (status >> S3C_GRXSTS_PktSts_SHIFT) {
1595 	case __status(S3C_GRXSTS_PktSts_GlobalOutNAK):
1596 		dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1597 		break;
1598 
1599 	case __status(S3C_GRXSTS_PktSts_OutDone):
1600 		dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1601 			s3c_hsotg_read_frameno(hsotg));
1602 
1603 		if (!using_dma(hsotg))
1604 			s3c_hsotg_handle_outdone(hsotg, epnum, false);
1605 		break;
1606 
1607 	case __status(S3C_GRXSTS_PktSts_SetupDone):
1608 		dev_dbg(hsotg->dev,
1609 			"SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1610 			s3c_hsotg_read_frameno(hsotg),
1611 			readl(hsotg->regs + S3C_DOEPCTL(0)));
1612 
1613 		s3c_hsotg_handle_outdone(hsotg, epnum, true);
1614 		break;
1615 
1616 	case __status(S3C_GRXSTS_PktSts_OutRX):
1617 		s3c_hsotg_rx_data(hsotg, epnum, size);
1618 		break;
1619 
1620 	case __status(S3C_GRXSTS_PktSts_SetupRX):
1621 		dev_dbg(hsotg->dev,
1622 			"SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1623 			s3c_hsotg_read_frameno(hsotg),
1624 			readl(hsotg->regs + S3C_DOEPCTL(0)));
1625 
1626 		s3c_hsotg_rx_data(hsotg, epnum, size);
1627 		break;
1628 
1629 	default:
1630 		dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1631 			 __func__, grxstsr);
1632 
1633 		s3c_hsotg_dump(hsotg);
1634 		break;
1635 	}
1636 }
1637 
1638 /**
1639  * s3c_hsotg_ep0_mps - turn max packet size into register setting
1640  * @mps: The maximum packet size in bytes.
1641 */
s3c_hsotg_ep0_mps(unsigned int mps)1642 static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1643 {
1644 	switch (mps) {
1645 	case 64:
1646 		return S3C_D0EPCTL_MPS_64;
1647 	case 32:
1648 		return S3C_D0EPCTL_MPS_32;
1649 	case 16:
1650 		return S3C_D0EPCTL_MPS_16;
1651 	case 8:
1652 		return S3C_D0EPCTL_MPS_8;
1653 	}
1654 
1655 	/* bad max packet size, warn and return invalid result */
1656 	WARN_ON(1);
1657 	return (u32)-1;
1658 }
1659 
1660 /**
1661  * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1662  * @hsotg: The driver state.
1663  * @ep: The index number of the endpoint
1664  * @mps: The maximum packet size in bytes
1665  *
1666  * Configure the maximum packet size for the given endpoint, updating
1667  * the hardware control registers to reflect this.
1668  */
s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg * hsotg,unsigned int ep,unsigned int mps)1669 static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1670 				       unsigned int ep, unsigned int mps)
1671 {
1672 	struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1673 	void __iomem *regs = hsotg->regs;
1674 	u32 mpsval;
1675 	u32 reg;
1676 
1677 	if (ep == 0) {
1678 		/* EP0 is a special case */
1679 		mpsval = s3c_hsotg_ep0_mps(mps);
1680 		if (mpsval > 3)
1681 			goto bad_mps;
1682 	} else {
1683 		if (mps >= S3C_DxEPCTL_MPS_LIMIT+1)
1684 			goto bad_mps;
1685 
1686 		mpsval = mps;
1687 	}
1688 
1689 	hs_ep->ep.maxpacket = mps;
1690 
1691 	/* update both the in and out endpoint controldir_ registers, even
1692 	 * if one of the directions may not be in use. */
1693 
1694 	reg = readl(regs + S3C_DIEPCTL(ep));
1695 	reg &= ~S3C_DxEPCTL_MPS_MASK;
1696 	reg |= mpsval;
1697 	writel(reg, regs + S3C_DIEPCTL(ep));
1698 
1699 	if (ep) {
1700 		reg = readl(regs + S3C_DOEPCTL(ep));
1701 		reg &= ~S3C_DxEPCTL_MPS_MASK;
1702 		reg |= mpsval;
1703 		writel(reg, regs + S3C_DOEPCTL(ep));
1704 	}
1705 
1706 	return;
1707 
1708 bad_mps:
1709 	dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1710 }
1711 
1712 /**
1713  * s3c_hsotg_txfifo_flush - flush Tx FIFO
1714  * @hsotg: The driver state
1715  * @idx: The index for the endpoint (0..15)
1716  */
s3c_hsotg_txfifo_flush(struct s3c_hsotg * hsotg,unsigned int idx)1717 static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1718 {
1719 	int timeout;
1720 	int val;
1721 
1722 	writel(S3C_GRSTCTL_TxFNum(idx) | S3C_GRSTCTL_TxFFlsh,
1723 		hsotg->regs + S3C_GRSTCTL);
1724 
1725 	/* wait until the fifo is flushed */
1726 	timeout = 100;
1727 
1728 	while (1) {
1729 		val = readl(hsotg->regs + S3C_GRSTCTL);
1730 
1731 		if ((val & (S3C_GRSTCTL_TxFFlsh)) == 0)
1732 			break;
1733 
1734 		if (--timeout == 0) {
1735 			dev_err(hsotg->dev,
1736 				"%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1737 				__func__, val);
1738 		}
1739 
1740 		udelay(1);
1741 	}
1742 }
1743 
1744 /**
1745  * s3c_hsotg_trytx - check to see if anything needs transmitting
1746  * @hsotg: The driver state
1747  * @hs_ep: The driver endpoint to check.
1748  *
1749  * Check to see if there is a request that has data to send, and if so
1750  * make an attempt to write data into the FIFO.
1751  */
s3c_hsotg_trytx(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep)1752 static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1753 			   struct s3c_hsotg_ep *hs_ep)
1754 {
1755 	struct s3c_hsotg_req *hs_req = hs_ep->req;
1756 
1757 	if (!hs_ep->dir_in || !hs_req)
1758 		return 0;
1759 
1760 	if (hs_req->req.actual < hs_req->req.length) {
1761 		dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1762 			hs_ep->index);
1763 		return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1764 	}
1765 
1766 	return 0;
1767 }
1768 
1769 /**
1770  * s3c_hsotg_complete_in - complete IN transfer
1771  * @hsotg: The device state.
1772  * @hs_ep: The endpoint that has just completed.
1773  *
1774  * An IN transfer has been completed, update the transfer's state and then
1775  * call the relevant completion routines.
1776  */
s3c_hsotg_complete_in(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep)1777 static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1778 				  struct s3c_hsotg_ep *hs_ep)
1779 {
1780 	struct s3c_hsotg_req *hs_req = hs_ep->req;
1781 	u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
1782 	int size_left, size_done;
1783 
1784 	if (!hs_req) {
1785 		dev_dbg(hsotg->dev, "XferCompl but no req\n");
1786 		return;
1787 	}
1788 
1789 	/* Calculate the size of the transfer by checking how much is left
1790 	 * in the endpoint size register and then working it out from
1791 	 * the amount we loaded for the transfer.
1792 	 *
1793 	 * We do this even for DMA, as the transfer may have incremented
1794 	 * past the end of the buffer (DMA transfers are always 32bit
1795 	 * aligned).
1796 	 */
1797 
1798 	size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
1799 
1800 	size_done = hs_ep->size_loaded - size_left;
1801 	size_done += hs_ep->last_load;
1802 
1803 	if (hs_req->req.actual != size_done)
1804 		dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1805 			__func__, hs_req->req.actual, size_done);
1806 
1807 	hs_req->req.actual = size_done;
1808 
1809 	/* if we did all of the transfer, and there is more data left
1810 	 * around, then try restarting the rest of the request */
1811 
1812 	if (!size_left && hs_req->req.actual < hs_req->req.length) {
1813 		dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1814 		s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1815 	} else
1816 		s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, 0);
1817 }
1818 
1819 /**
1820  * s3c_hsotg_epint - handle an in/out endpoint interrupt
1821  * @hsotg: The driver state
1822  * @idx: The index for the endpoint (0..15)
1823  * @dir_in: Set if this is an IN endpoint
1824  *
1825  * Process and clear any interrupt pending for an individual endpoint
1826 */
s3c_hsotg_epint(struct s3c_hsotg * hsotg,unsigned int idx,int dir_in)1827 static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1828 			    int dir_in)
1829 {
1830 	struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
1831 	u32 epint_reg = dir_in ? S3C_DIEPINT(idx) : S3C_DOEPINT(idx);
1832 	u32 epctl_reg = dir_in ? S3C_DIEPCTL(idx) : S3C_DOEPCTL(idx);
1833 	u32 epsiz_reg = dir_in ? S3C_DIEPTSIZ(idx) : S3C_DOEPTSIZ(idx);
1834 	u32 ints;
1835 
1836 	ints = readl(hsotg->regs + epint_reg);
1837 
1838 	/* Clear endpoint interrupts */
1839 	writel(ints, hsotg->regs + epint_reg);
1840 
1841 	dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1842 		__func__, idx, dir_in ? "in" : "out", ints);
1843 
1844 	if (ints & S3C_DxEPINT_XferCompl) {
1845 		dev_dbg(hsotg->dev,
1846 			"%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1847 			__func__, readl(hsotg->regs + epctl_reg),
1848 			readl(hsotg->regs + epsiz_reg));
1849 
1850 		/* we get OutDone from the FIFO, so we only need to look
1851 		 * at completing IN requests here */
1852 		if (dir_in) {
1853 			s3c_hsotg_complete_in(hsotg, hs_ep);
1854 
1855 			if (idx == 0 && !hs_ep->req)
1856 				s3c_hsotg_enqueue_setup(hsotg);
1857 		} else if (using_dma(hsotg)) {
1858 			/* We're using DMA, we need to fire an OutDone here
1859 			 * as we ignore the RXFIFO. */
1860 
1861 			s3c_hsotg_handle_outdone(hsotg, idx, false);
1862 		}
1863 	}
1864 
1865 	if (ints & S3C_DxEPINT_EPDisbld) {
1866 		dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
1867 
1868 		if (dir_in) {
1869 			int epctl = readl(hsotg->regs + epctl_reg);
1870 
1871 			s3c_hsotg_txfifo_flush(hsotg, idx);
1872 
1873 			if ((epctl & S3C_DxEPCTL_Stall) &&
1874 				(epctl & S3C_DxEPCTL_EPType_Bulk)) {
1875 				int dctl = readl(hsotg->regs + S3C_DCTL);
1876 
1877 				dctl |= S3C_DCTL_CGNPInNAK;
1878 				writel(dctl, hsotg->regs + S3C_DCTL);
1879 			}
1880 		}
1881 	}
1882 
1883 	if (ints & S3C_DxEPINT_AHBErr)
1884 		dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
1885 
1886 	if (ints & S3C_DxEPINT_Setup) {  /* Setup or Timeout */
1887 		dev_dbg(hsotg->dev, "%s: Setup/Timeout\n",  __func__);
1888 
1889 		if (using_dma(hsotg) && idx == 0) {
1890 			/* this is the notification we've received a
1891 			 * setup packet. In non-DMA mode we'd get this
1892 			 * from the RXFIFO, instead we need to process
1893 			 * the setup here. */
1894 
1895 			if (dir_in)
1896 				WARN_ON_ONCE(1);
1897 			else
1898 				s3c_hsotg_handle_outdone(hsotg, 0, true);
1899 		}
1900 	}
1901 
1902 	if (ints & S3C_DxEPINT_Back2BackSetup)
1903 		dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
1904 
1905 	if (dir_in) {
1906 		/* not sure if this is important, but we'll clear it anyway
1907 		 */
1908 		if (ints & S3C_DIEPMSK_INTknTXFEmpMsk) {
1909 			dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1910 				__func__, idx);
1911 		}
1912 
1913 		/* this probably means something bad is happening */
1914 		if (ints & S3C_DIEPMSK_INTknEPMisMsk) {
1915 			dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1916 				 __func__, idx);
1917 		}
1918 
1919 		/* FIFO has space or is empty (see GAHBCFG) */
1920 		if (hsotg->dedicated_fifos &&
1921 		    ints & S3C_DIEPMSK_TxFIFOEmpty) {
1922 			dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1923 				__func__, idx);
1924 			if (!using_dma(hsotg))
1925 				s3c_hsotg_trytx(hsotg, hs_ep);
1926 		}
1927 	}
1928 }
1929 
1930 /**
1931  * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
1932  * @hsotg: The device state.
1933  *
1934  * Handle updating the device settings after the enumeration phase has
1935  * been completed.
1936 */
s3c_hsotg_irq_enumdone(struct s3c_hsotg * hsotg)1937 static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
1938 {
1939 	u32 dsts = readl(hsotg->regs + S3C_DSTS);
1940 	int ep0_mps = 0, ep_mps;
1941 
1942 	/* This should signal the finish of the enumeration phase
1943 	 * of the USB handshaking, so we should now know what rate
1944 	 * we connected at. */
1945 
1946 	dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
1947 
1948 	/* note, since we're limited by the size of transfer on EP0, and
1949 	 * it seems IN transfers must be a even number of packets we do
1950 	 * not advertise a 64byte MPS on EP0. */
1951 
1952 	/* catch both EnumSpd_FS and EnumSpd_FS48 */
1953 	switch (dsts & S3C_DSTS_EnumSpd_MASK) {
1954 	case S3C_DSTS_EnumSpd_FS:
1955 	case S3C_DSTS_EnumSpd_FS48:
1956 		hsotg->gadget.speed = USB_SPEED_FULL;
1957 		ep0_mps = EP0_MPS_LIMIT;
1958 		ep_mps = 64;
1959 		break;
1960 
1961 	case S3C_DSTS_EnumSpd_HS:
1962 		hsotg->gadget.speed = USB_SPEED_HIGH;
1963 		ep0_mps = EP0_MPS_LIMIT;
1964 		ep_mps = 512;
1965 		break;
1966 
1967 	case S3C_DSTS_EnumSpd_LS:
1968 		hsotg->gadget.speed = USB_SPEED_LOW;
1969 		/* note, we don't actually support LS in this driver at the
1970 		 * moment, and the documentation seems to imply that it isn't
1971 		 * supported by the PHYs on some of the devices.
1972 		 */
1973 		break;
1974 	}
1975 	dev_info(hsotg->dev, "new device is %s\n",
1976 		 usb_speed_string(hsotg->gadget.speed));
1977 
1978 	/* we should now know the maximum packet size for an
1979 	 * endpoint, so set the endpoints to a default value. */
1980 
1981 	if (ep0_mps) {
1982 		int i;
1983 		s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
1984 		for (i = 1; i < S3C_HSOTG_EPS; i++)
1985 			s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
1986 	}
1987 
1988 	/* ensure after enumeration our EP0 is active */
1989 
1990 	s3c_hsotg_enqueue_setup(hsotg);
1991 
1992 	dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
1993 		readl(hsotg->regs + S3C_DIEPCTL0),
1994 		readl(hsotg->regs + S3C_DOEPCTL0));
1995 }
1996 
1997 /**
1998  * kill_all_requests - remove all requests from the endpoint's queue
1999  * @hsotg: The device state.
2000  * @ep: The endpoint the requests may be on.
2001  * @result: The result code to use.
2002  * @force: Force removal of any current requests
2003  *
2004  * Go through the requests on the given endpoint and mark them
2005  * completed with the given result code.
2006  */
kill_all_requests(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * ep,int result,bool force)2007 static void kill_all_requests(struct s3c_hsotg *hsotg,
2008 			      struct s3c_hsotg_ep *ep,
2009 			      int result, bool force)
2010 {
2011 	struct s3c_hsotg_req *req, *treq;
2012 	unsigned long flags;
2013 
2014 	spin_lock_irqsave(&ep->lock, flags);
2015 
2016 	list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2017 		/* currently, we can't do much about an already
2018 		 * running request on an in endpoint */
2019 
2020 		if (ep->req == req && ep->dir_in && !force)
2021 			continue;
2022 
2023 		s3c_hsotg_complete_request(hsotg, ep, req,
2024 					   result);
2025 	}
2026 
2027 	spin_unlock_irqrestore(&ep->lock, flags);
2028 }
2029 
2030 #define call_gadget(_hs, _entry) \
2031 	if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN &&	\
2032 	    (_hs)->driver && (_hs)->driver->_entry)	\
2033 		(_hs)->driver->_entry(&(_hs)->gadget);
2034 
2035 /**
2036  * s3c_hsotg_disconnect_irq - disconnect irq service
2037  * @hsotg: The device state.
2038  *
2039  * A disconnect IRQ has been received, meaning that the host has
2040  * lost contact with the bus. Remove all current transactions
2041  * and signal the gadget driver that this has happened.
2042 */
s3c_hsotg_disconnect_irq(struct s3c_hsotg * hsotg)2043 static void s3c_hsotg_disconnect_irq(struct s3c_hsotg *hsotg)
2044 {
2045 	unsigned ep;
2046 
2047 	for (ep = 0; ep < S3C_HSOTG_EPS; ep++)
2048 		kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2049 
2050 	call_gadget(hsotg, disconnect);
2051 }
2052 
2053 /**
2054  * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2055  * @hsotg: The device state:
2056  * @periodic: True if this is a periodic FIFO interrupt
2057  */
s3c_hsotg_irq_fifoempty(struct s3c_hsotg * hsotg,bool periodic)2058 static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2059 {
2060 	struct s3c_hsotg_ep *ep;
2061 	int epno, ret;
2062 
2063 	/* look through for any more data to transmit */
2064 
2065 	for (epno = 0; epno < S3C_HSOTG_EPS; epno++) {
2066 		ep = &hsotg->eps[epno];
2067 
2068 		if (!ep->dir_in)
2069 			continue;
2070 
2071 		if ((periodic && !ep->periodic) ||
2072 		    (!periodic && ep->periodic))
2073 			continue;
2074 
2075 		ret = s3c_hsotg_trytx(hsotg, ep);
2076 		if (ret < 0)
2077 			break;
2078 	}
2079 }
2080 
2081 static struct s3c_hsotg *our_hsotg;
2082 
2083 /* IRQ flags which will trigger a retry around the IRQ loop */
2084 #define IRQ_RETRY_MASK (S3C_GINTSTS_NPTxFEmp | \
2085 			S3C_GINTSTS_PTxFEmp |  \
2086 			S3C_GINTSTS_RxFLvl)
2087 
2088 /**
2089  * s3c_hsotg_irq - handle device interrupt
2090  * @irq: The IRQ number triggered
2091  * @pw: The pw value when registered the handler.
2092  */
s3c_hsotg_irq(int irq,void * pw)2093 static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2094 {
2095 	struct s3c_hsotg *hsotg = pw;
2096 	int retry_count = 8;
2097 	u32 gintsts;
2098 	u32 gintmsk;
2099 
2100 irq_retry:
2101 	gintsts = readl(hsotg->regs + S3C_GINTSTS);
2102 	gintmsk = readl(hsotg->regs + S3C_GINTMSK);
2103 
2104 	dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2105 		__func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2106 
2107 	gintsts &= gintmsk;
2108 
2109 	if (gintsts & S3C_GINTSTS_OTGInt) {
2110 		u32 otgint = readl(hsotg->regs + S3C_GOTGINT);
2111 
2112 		dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2113 
2114 		writel(otgint, hsotg->regs + S3C_GOTGINT);
2115 	}
2116 
2117 	if (gintsts & S3C_GINTSTS_DisconnInt) {
2118 		dev_dbg(hsotg->dev, "%s: DisconnInt\n", __func__);
2119 		writel(S3C_GINTSTS_DisconnInt, hsotg->regs + S3C_GINTSTS);
2120 
2121 		s3c_hsotg_disconnect_irq(hsotg);
2122 	}
2123 
2124 	if (gintsts & S3C_GINTSTS_SessReqInt) {
2125 		dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
2126 		writel(S3C_GINTSTS_SessReqInt, hsotg->regs + S3C_GINTSTS);
2127 	}
2128 
2129 	if (gintsts & S3C_GINTSTS_EnumDone) {
2130 		writel(S3C_GINTSTS_EnumDone, hsotg->regs + S3C_GINTSTS);
2131 
2132 		s3c_hsotg_irq_enumdone(hsotg);
2133 	}
2134 
2135 	if (gintsts & S3C_GINTSTS_ConIDStsChng) {
2136 		dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
2137 			readl(hsotg->regs + S3C_DSTS),
2138 			readl(hsotg->regs + S3C_GOTGCTL));
2139 
2140 		writel(S3C_GINTSTS_ConIDStsChng, hsotg->regs + S3C_GINTSTS);
2141 	}
2142 
2143 	if (gintsts & (S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt)) {
2144 		u32 daint = readl(hsotg->regs + S3C_DAINT);
2145 		u32 daint_out = daint >> S3C_DAINT_OutEP_SHIFT;
2146 		u32 daint_in = daint & ~(daint_out << S3C_DAINT_OutEP_SHIFT);
2147 		int ep;
2148 
2149 		dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2150 
2151 		for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2152 			if (daint_out & 1)
2153 				s3c_hsotg_epint(hsotg, ep, 0);
2154 		}
2155 
2156 		for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2157 			if (daint_in & 1)
2158 				s3c_hsotg_epint(hsotg, ep, 1);
2159 		}
2160 	}
2161 
2162 	if (gintsts & S3C_GINTSTS_USBRst) {
2163 		dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2164 		dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2165 			readl(hsotg->regs + S3C_GNPTXSTS));
2166 
2167 		writel(S3C_GINTSTS_USBRst, hsotg->regs + S3C_GINTSTS);
2168 
2169 		kill_all_requests(hsotg, &hsotg->eps[0], -ECONNRESET, true);
2170 
2171 		/* it seems after a reset we can end up with a situation
2172 		 * where the TXFIFO still has data in it... the docs
2173 		 * suggest resetting all the fifos, so use the init_fifo
2174 		 * code to relayout and flush the fifos.
2175 		 */
2176 
2177 		s3c_hsotg_init_fifo(hsotg);
2178 
2179 		s3c_hsotg_enqueue_setup(hsotg);
2180 	}
2181 
2182 	/* check both FIFOs */
2183 
2184 	if (gintsts & S3C_GINTSTS_NPTxFEmp) {
2185 		dev_dbg(hsotg->dev, "NPTxFEmp\n");
2186 
2187 		/* Disable the interrupt to stop it happening again
2188 		 * unless one of these endpoint routines decides that
2189 		 * it needs re-enabling */
2190 
2191 		s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
2192 		s3c_hsotg_irq_fifoempty(hsotg, false);
2193 	}
2194 
2195 	if (gintsts & S3C_GINTSTS_PTxFEmp) {
2196 		dev_dbg(hsotg->dev, "PTxFEmp\n");
2197 
2198 		/* See note in S3C_GINTSTS_NPTxFEmp */
2199 
2200 		s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
2201 		s3c_hsotg_irq_fifoempty(hsotg, true);
2202 	}
2203 
2204 	if (gintsts & S3C_GINTSTS_RxFLvl) {
2205 		/* note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
2206 		 * we need to retry s3c_hsotg_handle_rx if this is still
2207 		 * set. */
2208 
2209 		s3c_hsotg_handle_rx(hsotg);
2210 	}
2211 
2212 	if (gintsts & S3C_GINTSTS_ModeMis) {
2213 		dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
2214 		writel(S3C_GINTSTS_ModeMis, hsotg->regs + S3C_GINTSTS);
2215 	}
2216 
2217 	if (gintsts & S3C_GINTSTS_USBSusp) {
2218 		dev_info(hsotg->dev, "S3C_GINTSTS_USBSusp\n");
2219 		writel(S3C_GINTSTS_USBSusp, hsotg->regs + S3C_GINTSTS);
2220 
2221 		call_gadget(hsotg, suspend);
2222 	}
2223 
2224 	if (gintsts & S3C_GINTSTS_WkUpInt) {
2225 		dev_info(hsotg->dev, "S3C_GINTSTS_WkUpIn\n");
2226 		writel(S3C_GINTSTS_WkUpInt, hsotg->regs + S3C_GINTSTS);
2227 
2228 		call_gadget(hsotg, resume);
2229 	}
2230 
2231 	if (gintsts & S3C_GINTSTS_ErlySusp) {
2232 		dev_dbg(hsotg->dev, "S3C_GINTSTS_ErlySusp\n");
2233 		writel(S3C_GINTSTS_ErlySusp, hsotg->regs + S3C_GINTSTS);
2234 	}
2235 
2236 	/* these next two seem to crop-up occasionally causing the core
2237 	 * to shutdown the USB transfer, so try clearing them and logging
2238 	 * the occurrence. */
2239 
2240 	if (gintsts & S3C_GINTSTS_GOUTNakEff) {
2241 		dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2242 
2243 		writel(S3C_DCTL_CGOUTNak, hsotg->regs + S3C_DCTL);
2244 
2245 		s3c_hsotg_dump(hsotg);
2246 	}
2247 
2248 	if (gintsts & S3C_GINTSTS_GINNakEff) {
2249 		dev_info(hsotg->dev, "GINNakEff triggered\n");
2250 
2251 		writel(S3C_DCTL_CGNPInNAK, hsotg->regs + S3C_DCTL);
2252 
2253 		s3c_hsotg_dump(hsotg);
2254 	}
2255 
2256 	/* if we've had fifo events, we should try and go around the
2257 	 * loop again to see if there's any point in returning yet. */
2258 
2259 	if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2260 			goto irq_retry;
2261 
2262 	return IRQ_HANDLED;
2263 }
2264 
2265 /**
2266  * s3c_hsotg_ep_enable - enable the given endpoint
2267  * @ep: The USB endpint to configure
2268  * @desc: The USB endpoint descriptor to configure with.
2269  *
2270  * This is called from the USB gadget code's usb_ep_enable().
2271 */
s3c_hsotg_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)2272 static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2273 			       const struct usb_endpoint_descriptor *desc)
2274 {
2275 	struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2276 	struct s3c_hsotg *hsotg = hs_ep->parent;
2277 	unsigned long flags;
2278 	int index = hs_ep->index;
2279 	u32 epctrl_reg;
2280 	u32 epctrl;
2281 	u32 mps;
2282 	int dir_in;
2283 	int ret = 0;
2284 
2285 	dev_dbg(hsotg->dev,
2286 		"%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2287 		__func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2288 		desc->wMaxPacketSize, desc->bInterval);
2289 
2290 	/* not to be called for EP0 */
2291 	WARN_ON(index == 0);
2292 
2293 	dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2294 	if (dir_in != hs_ep->dir_in) {
2295 		dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2296 		return -EINVAL;
2297 	}
2298 
2299 	mps = usb_endpoint_maxp(desc);
2300 
2301 	/* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2302 
2303 	epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2304 	epctrl = readl(hsotg->regs + epctrl_reg);
2305 
2306 	dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2307 		__func__, epctrl, epctrl_reg);
2308 
2309 	spin_lock_irqsave(&hs_ep->lock, flags);
2310 
2311 	epctrl &= ~(S3C_DxEPCTL_EPType_MASK | S3C_DxEPCTL_MPS_MASK);
2312 	epctrl |= S3C_DxEPCTL_MPS(mps);
2313 
2314 	/* mark the endpoint as active, otherwise the core may ignore
2315 	 * transactions entirely for this endpoint */
2316 	epctrl |= S3C_DxEPCTL_USBActEp;
2317 
2318 	/* set the NAK status on the endpoint, otherwise we might try and
2319 	 * do something with data that we've yet got a request to process
2320 	 * since the RXFIFO will take data for an endpoint even if the
2321 	 * size register hasn't been set.
2322 	 */
2323 
2324 	epctrl |= S3C_DxEPCTL_SNAK;
2325 
2326 	/* update the endpoint state */
2327 	hs_ep->ep.maxpacket = mps;
2328 
2329 	/* default, set to non-periodic */
2330 	hs_ep->periodic = 0;
2331 
2332 	switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2333 	case USB_ENDPOINT_XFER_ISOC:
2334 		dev_err(hsotg->dev, "no current ISOC support\n");
2335 		ret = -EINVAL;
2336 		goto out;
2337 
2338 	case USB_ENDPOINT_XFER_BULK:
2339 		epctrl |= S3C_DxEPCTL_EPType_Bulk;
2340 		break;
2341 
2342 	case USB_ENDPOINT_XFER_INT:
2343 		if (dir_in) {
2344 			/* Allocate our TxFNum by simply using the index
2345 			 * of the endpoint for the moment. We could do
2346 			 * something better if the host indicates how
2347 			 * many FIFOs we are expecting to use. */
2348 
2349 			hs_ep->periodic = 1;
2350 			epctrl |= S3C_DxEPCTL_TxFNum(index);
2351 		}
2352 
2353 		epctrl |= S3C_DxEPCTL_EPType_Intterupt;
2354 		break;
2355 
2356 	case USB_ENDPOINT_XFER_CONTROL:
2357 		epctrl |= S3C_DxEPCTL_EPType_Control;
2358 		break;
2359 	}
2360 
2361 	/* if the hardware has dedicated fifos, we must give each IN EP
2362 	 * a unique tx-fifo even if it is non-periodic.
2363 	 */
2364 	if (dir_in && hsotg->dedicated_fifos)
2365 		epctrl |= S3C_DxEPCTL_TxFNum(index);
2366 
2367 	/* for non control endpoints, set PID to D0 */
2368 	if (index)
2369 		epctrl |= S3C_DxEPCTL_SetD0PID;
2370 
2371 	dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2372 		__func__, epctrl);
2373 
2374 	writel(epctrl, hsotg->regs + epctrl_reg);
2375 	dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2376 		__func__, readl(hsotg->regs + epctrl_reg));
2377 
2378 	/* enable the endpoint interrupt */
2379 	s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2380 
2381 out:
2382 	spin_unlock_irqrestore(&hs_ep->lock, flags);
2383 	return ret;
2384 }
2385 
s3c_hsotg_ep_disable(struct usb_ep * ep)2386 static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2387 {
2388 	struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2389 	struct s3c_hsotg *hsotg = hs_ep->parent;
2390 	int dir_in = hs_ep->dir_in;
2391 	int index = hs_ep->index;
2392 	unsigned long flags;
2393 	u32 epctrl_reg;
2394 	u32 ctrl;
2395 
2396 	dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2397 
2398 	if (ep == &hsotg->eps[0].ep) {
2399 		dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2400 		return -EINVAL;
2401 	}
2402 
2403 	epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2404 
2405 	/* terminate all requests with shutdown */
2406 	kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2407 
2408 	spin_lock_irqsave(&hs_ep->lock, flags);
2409 
2410 	ctrl = readl(hsotg->regs + epctrl_reg);
2411 	ctrl &= ~S3C_DxEPCTL_EPEna;
2412 	ctrl &= ~S3C_DxEPCTL_USBActEp;
2413 	ctrl |= S3C_DxEPCTL_SNAK;
2414 
2415 	dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2416 	writel(ctrl, hsotg->regs + epctrl_reg);
2417 
2418 	/* disable endpoint interrupts */
2419 	s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2420 
2421 	spin_unlock_irqrestore(&hs_ep->lock, flags);
2422 	return 0;
2423 }
2424 
2425 /**
2426  * on_list - check request is on the given endpoint
2427  * @ep: The endpoint to check.
2428  * @test: The request to test if it is on the endpoint.
2429 */
on_list(struct s3c_hsotg_ep * ep,struct s3c_hsotg_req * test)2430 static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2431 {
2432 	struct s3c_hsotg_req *req, *treq;
2433 
2434 	list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2435 		if (req == test)
2436 			return true;
2437 	}
2438 
2439 	return false;
2440 }
2441 
s3c_hsotg_ep_dequeue(struct usb_ep * ep,struct usb_request * req)2442 static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2443 {
2444 	struct s3c_hsotg_req *hs_req = our_req(req);
2445 	struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2446 	struct s3c_hsotg *hs = hs_ep->parent;
2447 	unsigned long flags;
2448 
2449 	dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2450 
2451 	spin_lock_irqsave(&hs_ep->lock, flags);
2452 
2453 	if (!on_list(hs_ep, hs_req)) {
2454 		spin_unlock_irqrestore(&hs_ep->lock, flags);
2455 		return -EINVAL;
2456 	}
2457 
2458 	s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
2459 	spin_unlock_irqrestore(&hs_ep->lock, flags);
2460 
2461 	return 0;
2462 }
2463 
s3c_hsotg_ep_sethalt(struct usb_ep * ep,int value)2464 static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2465 {
2466 	struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2467 	struct s3c_hsotg *hs = hs_ep->parent;
2468 	int index = hs_ep->index;
2469 	unsigned long irqflags;
2470 	u32 epreg;
2471 	u32 epctl;
2472 	u32 xfertype;
2473 
2474 	dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2475 
2476 	spin_lock_irqsave(&hs_ep->lock, irqflags);
2477 
2478 	/* write both IN and OUT control registers */
2479 
2480 	epreg = S3C_DIEPCTL(index);
2481 	epctl = readl(hs->regs + epreg);
2482 
2483 	if (value) {
2484 		epctl |= S3C_DxEPCTL_Stall + S3C_DxEPCTL_SNAK;
2485 		if (epctl & S3C_DxEPCTL_EPEna)
2486 			epctl |= S3C_DxEPCTL_EPDis;
2487 	} else {
2488 		epctl &= ~S3C_DxEPCTL_Stall;
2489 		xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2490 		if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2491 			xfertype == S3C_DxEPCTL_EPType_Intterupt)
2492 				epctl |= S3C_DxEPCTL_SetD0PID;
2493 	}
2494 
2495 	writel(epctl, hs->regs + epreg);
2496 
2497 	epreg = S3C_DOEPCTL(index);
2498 	epctl = readl(hs->regs + epreg);
2499 
2500 	if (value)
2501 		epctl |= S3C_DxEPCTL_Stall;
2502 	else {
2503 		epctl &= ~S3C_DxEPCTL_Stall;
2504 		xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2505 		if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2506 			xfertype == S3C_DxEPCTL_EPType_Intterupt)
2507 				epctl |= S3C_DxEPCTL_SetD0PID;
2508 	}
2509 
2510 	writel(epctl, hs->regs + epreg);
2511 
2512 	spin_unlock_irqrestore(&hs_ep->lock, irqflags);
2513 
2514 	return 0;
2515 }
2516 
2517 static struct usb_ep_ops s3c_hsotg_ep_ops = {
2518 	.enable		= s3c_hsotg_ep_enable,
2519 	.disable	= s3c_hsotg_ep_disable,
2520 	.alloc_request	= s3c_hsotg_ep_alloc_request,
2521 	.free_request	= s3c_hsotg_ep_free_request,
2522 	.queue		= s3c_hsotg_ep_queue,
2523 	.dequeue	= s3c_hsotg_ep_dequeue,
2524 	.set_halt	= s3c_hsotg_ep_sethalt,
2525 	/* note, don't believe we have any call for the fifo routines */
2526 };
2527 
2528 /**
2529  * s3c_hsotg_corereset - issue softreset to the core
2530  * @hsotg: The device state
2531  *
2532  * Issue a soft reset to the core, and await the core finishing it.
2533 */
s3c_hsotg_corereset(struct s3c_hsotg * hsotg)2534 static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2535 {
2536 	int timeout;
2537 	u32 grstctl;
2538 
2539 	dev_dbg(hsotg->dev, "resetting core\n");
2540 
2541 	/* issue soft reset */
2542 	writel(S3C_GRSTCTL_CSftRst, hsotg->regs + S3C_GRSTCTL);
2543 
2544 	timeout = 1000;
2545 	do {
2546 		grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2547 	} while ((grstctl & S3C_GRSTCTL_CSftRst) && timeout-- > 0);
2548 
2549 	if (grstctl & S3C_GRSTCTL_CSftRst) {
2550 		dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2551 		return -EINVAL;
2552 	}
2553 
2554 	timeout = 1000;
2555 
2556 	while (1) {
2557 		u32 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2558 
2559 		if (timeout-- < 0) {
2560 			dev_info(hsotg->dev,
2561 				 "%s: reset failed, GRSTCTL=%08x\n",
2562 				 __func__, grstctl);
2563 			return -ETIMEDOUT;
2564 		}
2565 
2566 		if (!(grstctl & S3C_GRSTCTL_AHBIdle))
2567 			continue;
2568 
2569 		break;		/* reset done */
2570 	}
2571 
2572 	dev_dbg(hsotg->dev, "reset successful\n");
2573 	return 0;
2574 }
2575 
s3c_hsotg_start(struct usb_gadget_driver * driver,int (* bind)(struct usb_gadget *))2576 static int s3c_hsotg_start(struct usb_gadget_driver *driver,
2577 		int (*bind)(struct usb_gadget *))
2578 {
2579 	struct s3c_hsotg *hsotg = our_hsotg;
2580 	int ret;
2581 
2582 	if (!hsotg) {
2583 		printk(KERN_ERR "%s: called with no device\n", __func__);
2584 		return -ENODEV;
2585 	}
2586 
2587 	if (!driver) {
2588 		dev_err(hsotg->dev, "%s: no driver\n", __func__);
2589 		return -EINVAL;
2590 	}
2591 
2592 	if (driver->max_speed < USB_SPEED_FULL)
2593 		dev_err(hsotg->dev, "%s: bad speed\n", __func__);
2594 
2595 	if (!bind || !driver->setup) {
2596 		dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2597 		return -EINVAL;
2598 	}
2599 
2600 	WARN_ON(hsotg->driver);
2601 
2602 	driver->driver.bus = NULL;
2603 	hsotg->driver = driver;
2604 	hsotg->gadget.dev.driver = &driver->driver;
2605 	hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
2606 	hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2607 
2608 	ret = device_add(&hsotg->gadget.dev);
2609 	if (ret) {
2610 		dev_err(hsotg->dev, "failed to register gadget device\n");
2611 		goto err;
2612 	}
2613 
2614 	ret = bind(&hsotg->gadget);
2615 	if (ret) {
2616 		dev_err(hsotg->dev, "failed bind %s\n", driver->driver.name);
2617 
2618 		hsotg->gadget.dev.driver = NULL;
2619 		hsotg->driver = NULL;
2620 		goto err;
2621 	}
2622 
2623 	/* we must now enable ep0 ready for host detection and then
2624 	 * set configuration. */
2625 
2626 	s3c_hsotg_corereset(hsotg);
2627 
2628 	/* set the PLL on, remove the HNP/SRP and set the PHY */
2629 	writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) |
2630 	       (0x5 << 10), hsotg->regs + S3C_GUSBCFG);
2631 
2632 	/* looks like soft-reset changes state of FIFOs */
2633 	s3c_hsotg_init_fifo(hsotg);
2634 
2635 	__orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2636 
2637 	writel(1 << 18 | S3C_DCFG_DevSpd_HS,  hsotg->regs + S3C_DCFG);
2638 
2639 	/* Clear any pending OTG interrupts */
2640 	writel(0xffffffff, hsotg->regs + S3C_GOTGINT);
2641 
2642 	/* Clear any pending interrupts */
2643 	writel(0xffffffff, hsotg->regs + S3C_GINTSTS);
2644 
2645 	writel(S3C_GINTSTS_DisconnInt | S3C_GINTSTS_SessReqInt |
2646 	       S3C_GINTSTS_ConIDStsChng | S3C_GINTSTS_USBRst |
2647 	       S3C_GINTSTS_EnumDone | S3C_GINTSTS_OTGInt |
2648 	       S3C_GINTSTS_USBSusp | S3C_GINTSTS_WkUpInt |
2649 	       S3C_GINTSTS_GOUTNakEff | S3C_GINTSTS_GINNakEff |
2650 	       S3C_GINTSTS_ErlySusp,
2651 	       hsotg->regs + S3C_GINTMSK);
2652 
2653 	if (using_dma(hsotg))
2654 		writel(S3C_GAHBCFG_GlblIntrEn | S3C_GAHBCFG_DMAEn |
2655 		       S3C_GAHBCFG_HBstLen_Incr4,
2656 		       hsotg->regs + S3C_GAHBCFG);
2657 	else
2658 		writel(S3C_GAHBCFG_GlblIntrEn, hsotg->regs + S3C_GAHBCFG);
2659 
2660 	/* Enabling INTknTXFEmpMsk here seems to be a big mistake, we end
2661 	 * up being flooded with interrupts if the host is polling the
2662 	 * endpoint to try and read data. */
2663 
2664 	writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2665 	       S3C_DIEPMSK_INTknEPMisMsk |
2666 	       S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk |
2667 	       ((hsotg->dedicated_fifos) ? S3C_DIEPMSK_TxFIFOEmpty : 0),
2668 	       hsotg->regs + S3C_DIEPMSK);
2669 
2670 	/* don't need XferCompl, we get that from RXFIFO in slave mode. In
2671 	 * DMA mode we may need this. */
2672 	writel(S3C_DOEPMSK_SetupMsk | S3C_DOEPMSK_AHBErrMsk |
2673 	       S3C_DOEPMSK_EPDisbldMsk |
2674 	       (using_dma(hsotg) ? (S3C_DIEPMSK_XferComplMsk |
2675 				   S3C_DIEPMSK_TimeOUTMsk) : 0),
2676 	       hsotg->regs + S3C_DOEPMSK);
2677 
2678 	writel(0, hsotg->regs + S3C_DAINTMSK);
2679 
2680 	dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2681 		readl(hsotg->regs + S3C_DIEPCTL0),
2682 		readl(hsotg->regs + S3C_DOEPCTL0));
2683 
2684 	/* enable in and out endpoint interrupts */
2685 	s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt);
2686 
2687 	/* Enable the RXFIFO when in slave mode, as this is how we collect
2688 	 * the data. In DMA mode, we get events from the FIFO but also
2689 	 * things we cannot process, so do not use it. */
2690 	if (!using_dma(hsotg))
2691 		s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_RxFLvl);
2692 
2693 	/* Enable interrupts for EP0 in and out */
2694 	s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2695 	s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2696 
2697 	__orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2698 	udelay(10);  /* see openiboot */
2699 	__bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2700 
2701 	dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + S3C_DCTL));
2702 
2703 	/* S3C_DxEPCTL_USBActEp says RO in manual, but seems to be set by
2704 	   writing to the EPCTL register.. */
2705 
2706 	/* set to read 1 8byte packet */
2707 	writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
2708 	       S3C_DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
2709 
2710 	writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2711 	       S3C_DxEPCTL_CNAK | S3C_DxEPCTL_EPEna |
2712 	       S3C_DxEPCTL_USBActEp,
2713 	       hsotg->regs + S3C_DOEPCTL0);
2714 
2715 	/* enable, but don't activate EP0in */
2716 	writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2717 	       S3C_DxEPCTL_USBActEp, hsotg->regs + S3C_DIEPCTL0);
2718 
2719 	s3c_hsotg_enqueue_setup(hsotg);
2720 
2721 	dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2722 		readl(hsotg->regs + S3C_DIEPCTL0),
2723 		readl(hsotg->regs + S3C_DOEPCTL0));
2724 
2725 	/* clear global NAKs */
2726 	writel(S3C_DCTL_CGOUTNak | S3C_DCTL_CGNPInNAK,
2727 	       hsotg->regs + S3C_DCTL);
2728 
2729 	/* must be at-least 3ms to allow bus to see disconnect */
2730 	msleep(3);
2731 
2732 	/* remove the soft-disconnect and let's go */
2733 	__bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2734 
2735 	/* report to the user, and return */
2736 
2737 	dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
2738 	return 0;
2739 
2740 err:
2741 	hsotg->driver = NULL;
2742 	hsotg->gadget.dev.driver = NULL;
2743 	return ret;
2744 }
2745 
s3c_hsotg_stop(struct usb_gadget_driver * driver)2746 static int s3c_hsotg_stop(struct usb_gadget_driver *driver)
2747 {
2748 	struct s3c_hsotg *hsotg = our_hsotg;
2749 	int ep;
2750 
2751 	if (!hsotg)
2752 		return -ENODEV;
2753 
2754 	if (!driver || driver != hsotg->driver || !driver->unbind)
2755 		return -EINVAL;
2756 
2757 	/* all endpoints should be shutdown */
2758 	for (ep = 0; ep < S3C_HSOTG_EPS; ep++)
2759 		s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
2760 
2761 	call_gadget(hsotg, disconnect);
2762 
2763 	driver->unbind(&hsotg->gadget);
2764 	hsotg->driver = NULL;
2765 	hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2766 
2767 	device_del(&hsotg->gadget.dev);
2768 
2769 	dev_info(hsotg->dev, "unregistered gadget driver '%s'\n",
2770 		 driver->driver.name);
2771 
2772 	return 0;
2773 }
2774 
s3c_hsotg_gadget_getframe(struct usb_gadget * gadget)2775 static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2776 {
2777 	return s3c_hsotg_read_frameno(to_hsotg(gadget));
2778 }
2779 
2780 static struct usb_gadget_ops s3c_hsotg_gadget_ops = {
2781 	.get_frame	= s3c_hsotg_gadget_getframe,
2782 	.start		= s3c_hsotg_start,
2783 	.stop		= s3c_hsotg_stop,
2784 };
2785 
2786 /**
2787  * s3c_hsotg_initep - initialise a single endpoint
2788  * @hsotg: The device state.
2789  * @hs_ep: The endpoint to be initialised.
2790  * @epnum: The endpoint number
2791  *
2792  * Initialise the given endpoint (as part of the probe and device state
2793  * creation) to give to the gadget driver. Setup the endpoint name, any
2794  * direction information and other state that may be required.
2795  */
s3c_hsotg_initep(struct s3c_hsotg * hsotg,struct s3c_hsotg_ep * hs_ep,int epnum)2796 static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
2797 				       struct s3c_hsotg_ep *hs_ep,
2798 				       int epnum)
2799 {
2800 	u32 ptxfifo;
2801 	char *dir;
2802 
2803 	if (epnum == 0)
2804 		dir = "";
2805 	else if ((epnum % 2) == 0) {
2806 		dir = "out";
2807 	} else {
2808 		dir = "in";
2809 		hs_ep->dir_in = 1;
2810 	}
2811 
2812 	hs_ep->index = epnum;
2813 
2814 	snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
2815 
2816 	INIT_LIST_HEAD(&hs_ep->queue);
2817 	INIT_LIST_HEAD(&hs_ep->ep.ep_list);
2818 
2819 	spin_lock_init(&hs_ep->lock);
2820 
2821 	/* add to the list of endpoints known by the gadget driver */
2822 	if (epnum)
2823 		list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
2824 
2825 	hs_ep->parent = hsotg;
2826 	hs_ep->ep.name = hs_ep->name;
2827 	hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT;
2828 	hs_ep->ep.ops = &s3c_hsotg_ep_ops;
2829 
2830 	/* Read the FIFO size for the Periodic TX FIFO, even if we're
2831 	 * an OUT endpoint, we may as well do this if in future the
2832 	 * code is changed to make each endpoint's direction changeable.
2833 	 */
2834 
2835 	ptxfifo = readl(hsotg->regs + S3C_DPTXFSIZn(epnum));
2836 	hs_ep->fifo_size = S3C_DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
2837 
2838 	/* if we're using dma, we need to set the next-endpoint pointer
2839 	 * to be something valid.
2840 	 */
2841 
2842 	if (using_dma(hsotg)) {
2843 		u32 next = S3C_DxEPCTL_NextEp((epnum + 1) % 15);
2844 		writel(next, hsotg->regs + S3C_DIEPCTL(epnum));
2845 		writel(next, hsotg->regs + S3C_DOEPCTL(epnum));
2846 	}
2847 }
2848 
2849 /**
2850  * s3c_hsotg_otgreset - reset the OtG phy block
2851  * @hsotg: The host state.
2852  *
2853  * Power up the phy, set the basic configuration and start the PHY.
2854  */
s3c_hsotg_otgreset(struct s3c_hsotg * hsotg)2855 static void s3c_hsotg_otgreset(struct s3c_hsotg *hsotg)
2856 {
2857 	struct clk *xusbxti;
2858 	u32 pwr, osc;
2859 
2860 	pwr = readl(S3C_PHYPWR);
2861 	pwr &= ~0x19;
2862 	writel(pwr, S3C_PHYPWR);
2863 	mdelay(1);
2864 
2865 	osc = hsotg->plat->is_osc ? S3C_PHYCLK_EXT_OSC : 0;
2866 
2867 	xusbxti = clk_get(hsotg->dev, "xusbxti");
2868 	if (xusbxti && !IS_ERR(xusbxti)) {
2869 		switch (clk_get_rate(xusbxti)) {
2870 		case 12*MHZ:
2871 			osc |= S3C_PHYCLK_CLKSEL_12M;
2872 			break;
2873 		case 24*MHZ:
2874 			osc |= S3C_PHYCLK_CLKSEL_24M;
2875 			break;
2876 		default:
2877 		case 48*MHZ:
2878 			/* default reference clock */
2879 			break;
2880 		}
2881 		clk_put(xusbxti);
2882 	}
2883 
2884 	writel(osc | 0x10, S3C_PHYCLK);
2885 
2886 	/* issue a full set of resets to the otg and core */
2887 
2888 	writel(S3C_RSTCON_PHY, S3C_RSTCON);
2889 	udelay(20);	/* at-least 10uS */
2890 	writel(0, S3C_RSTCON);
2891 }
2892 
2893 
s3c_hsotg_init(struct s3c_hsotg * hsotg)2894 static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2895 {
2896 	u32 cfg4;
2897 
2898 	/* unmask subset of endpoint interrupts */
2899 
2900 	writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2901 	       S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk,
2902 	       hsotg->regs + S3C_DIEPMSK);
2903 
2904 	writel(S3C_DOEPMSK_SetupMsk | S3C_DOEPMSK_AHBErrMsk |
2905 	       S3C_DOEPMSK_EPDisbldMsk | S3C_DOEPMSK_XferComplMsk,
2906 	       hsotg->regs + S3C_DOEPMSK);
2907 
2908 	writel(0, hsotg->regs + S3C_DAINTMSK);
2909 
2910 	/* Be in disconnected state until gadget is registered */
2911 	__orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2912 
2913 	if (0) {
2914 		/* post global nak until we're ready */
2915 		writel(S3C_DCTL_SGNPInNAK | S3C_DCTL_SGOUTNak,
2916 		       hsotg->regs + S3C_DCTL);
2917 	}
2918 
2919 	/* setup fifos */
2920 
2921 	dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2922 		readl(hsotg->regs + S3C_GRXFSIZ),
2923 		readl(hsotg->regs + S3C_GNPTXFSIZ));
2924 
2925 	s3c_hsotg_init_fifo(hsotg);
2926 
2927 	/* set the PLL on, remove the HNP/SRP and set the PHY */
2928 	writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) | (0x5 << 10),
2929 	       hsotg->regs + S3C_GUSBCFG);
2930 
2931 	writel(using_dma(hsotg) ? S3C_GAHBCFG_DMAEn : 0x0,
2932 	       hsotg->regs + S3C_GAHBCFG);
2933 
2934 	/* check hardware configuration */
2935 
2936 	cfg4 = readl(hsotg->regs + 0x50);
2937 	hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
2938 
2939 	dev_info(hsotg->dev, "%s fifos\n",
2940 		 hsotg->dedicated_fifos ? "dedicated" : "shared");
2941 }
2942 
s3c_hsotg_dump(struct s3c_hsotg * hsotg)2943 static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
2944 {
2945 #ifdef DEBUG
2946 	struct device *dev = hsotg->dev;
2947 	void __iomem *regs = hsotg->regs;
2948 	u32 val;
2949 	int idx;
2950 
2951 	dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
2952 		 readl(regs + S3C_DCFG), readl(regs + S3C_DCTL),
2953 		 readl(regs + S3C_DIEPMSK));
2954 
2955 	dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
2956 		 readl(regs + S3C_GAHBCFG), readl(regs + 0x44));
2957 
2958 	dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2959 		 readl(regs + S3C_GRXFSIZ), readl(regs + S3C_GNPTXFSIZ));
2960 
2961 	/* show periodic fifo settings */
2962 
2963 	for (idx = 1; idx <= 15; idx++) {
2964 		val = readl(regs + S3C_DPTXFSIZn(idx));
2965 		dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
2966 			 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
2967 			 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
2968 	}
2969 
2970 	for (idx = 0; idx < 15; idx++) {
2971 		dev_info(dev,
2972 			 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
2973 			 readl(regs + S3C_DIEPCTL(idx)),
2974 			 readl(regs + S3C_DIEPTSIZ(idx)),
2975 			 readl(regs + S3C_DIEPDMA(idx)));
2976 
2977 		val = readl(regs + S3C_DOEPCTL(idx));
2978 		dev_info(dev,
2979 			 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
2980 			 idx, readl(regs + S3C_DOEPCTL(idx)),
2981 			 readl(regs + S3C_DOEPTSIZ(idx)),
2982 			 readl(regs + S3C_DOEPDMA(idx)));
2983 
2984 	}
2985 
2986 	dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
2987 		 readl(regs + S3C_DVBUSDIS), readl(regs + S3C_DVBUSPULSE));
2988 #endif
2989 }
2990 
2991 
2992 /**
2993  * state_show - debugfs: show overall driver and device state.
2994  * @seq: The seq file to write to.
2995  * @v: Unused parameter.
2996  *
2997  * This debugfs entry shows the overall state of the hardware and
2998  * some general information about each of the endpoints available
2999  * to the system.
3000  */
state_show(struct seq_file * seq,void * v)3001 static int state_show(struct seq_file *seq, void *v)
3002 {
3003 	struct s3c_hsotg *hsotg = seq->private;
3004 	void __iomem *regs = hsotg->regs;
3005 	int idx;
3006 
3007 	seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
3008 		 readl(regs + S3C_DCFG),
3009 		 readl(regs + S3C_DCTL),
3010 		 readl(regs + S3C_DSTS));
3011 
3012 	seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
3013 		   readl(regs + S3C_DIEPMSK), readl(regs + S3C_DOEPMSK));
3014 
3015 	seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
3016 		   readl(regs + S3C_GINTMSK),
3017 		   readl(regs + S3C_GINTSTS));
3018 
3019 	seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
3020 		   readl(regs + S3C_DAINTMSK),
3021 		   readl(regs + S3C_DAINT));
3022 
3023 	seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
3024 		   readl(regs + S3C_GNPTXSTS),
3025 		   readl(regs + S3C_GRXSTSR));
3026 
3027 	seq_printf(seq, "\nEndpoint status:\n");
3028 
3029 	for (idx = 0; idx < 15; idx++) {
3030 		u32 in, out;
3031 
3032 		in = readl(regs + S3C_DIEPCTL(idx));
3033 		out = readl(regs + S3C_DOEPCTL(idx));
3034 
3035 		seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3036 			   idx, in, out);
3037 
3038 		in = readl(regs + S3C_DIEPTSIZ(idx));
3039 		out = readl(regs + S3C_DOEPTSIZ(idx));
3040 
3041 		seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3042 			   in, out);
3043 
3044 		seq_printf(seq, "\n");
3045 	}
3046 
3047 	return 0;
3048 }
3049 
state_open(struct inode * inode,struct file * file)3050 static int state_open(struct inode *inode, struct file *file)
3051 {
3052 	return single_open(file, state_show, inode->i_private);
3053 }
3054 
3055 static const struct file_operations state_fops = {
3056 	.owner		= THIS_MODULE,
3057 	.open		= state_open,
3058 	.read		= seq_read,
3059 	.llseek		= seq_lseek,
3060 	.release	= single_release,
3061 };
3062 
3063 /**
3064  * fifo_show - debugfs: show the fifo information
3065  * @seq: The seq_file to write data to.
3066  * @v: Unused parameter.
3067  *
3068  * Show the FIFO information for the overall fifo and all the
3069  * periodic transmission FIFOs.
3070 */
fifo_show(struct seq_file * seq,void * v)3071 static int fifo_show(struct seq_file *seq, void *v)
3072 {
3073 	struct s3c_hsotg *hsotg = seq->private;
3074 	void __iomem *regs = hsotg->regs;
3075 	u32 val;
3076 	int idx;
3077 
3078 	seq_printf(seq, "Non-periodic FIFOs:\n");
3079 	seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + S3C_GRXFSIZ));
3080 
3081 	val = readl(regs + S3C_GNPTXFSIZ);
3082 	seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
3083 		   val >> S3C_GNPTXFSIZ_NPTxFDep_SHIFT,
3084 		   val & S3C_GNPTXFSIZ_NPTxFStAddr_MASK);
3085 
3086 	seq_printf(seq, "\nPeriodic TXFIFOs:\n");
3087 
3088 	for (idx = 1; idx <= 15; idx++) {
3089 		val = readl(regs + S3C_DPTXFSIZn(idx));
3090 
3091 		seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
3092 			   val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
3093 			   val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
3094 	}
3095 
3096 	return 0;
3097 }
3098 
fifo_open(struct inode * inode,struct file * file)3099 static int fifo_open(struct inode *inode, struct file *file)
3100 {
3101 	return single_open(file, fifo_show, inode->i_private);
3102 }
3103 
3104 static const struct file_operations fifo_fops = {
3105 	.owner		= THIS_MODULE,
3106 	.open		= fifo_open,
3107 	.read		= seq_read,
3108 	.llseek		= seq_lseek,
3109 	.release	= single_release,
3110 };
3111 
3112 
decode_direction(int is_in)3113 static const char *decode_direction(int is_in)
3114 {
3115 	return is_in ? "in" : "out";
3116 }
3117 
3118 /**
3119  * ep_show - debugfs: show the state of an endpoint.
3120  * @seq: The seq_file to write data to.
3121  * @v: Unused parameter.
3122  *
3123  * This debugfs entry shows the state of the given endpoint (one is
3124  * registered for each available).
3125 */
ep_show(struct seq_file * seq,void * v)3126 static int ep_show(struct seq_file *seq, void *v)
3127 {
3128 	struct s3c_hsotg_ep *ep = seq->private;
3129 	struct s3c_hsotg *hsotg = ep->parent;
3130 	struct s3c_hsotg_req *req;
3131 	void __iomem *regs = hsotg->regs;
3132 	int index = ep->index;
3133 	int show_limit = 15;
3134 	unsigned long flags;
3135 
3136 	seq_printf(seq, "Endpoint index %d, named %s,  dir %s:\n",
3137 		   ep->index, ep->ep.name, decode_direction(ep->dir_in));
3138 
3139 	/* first show the register state */
3140 
3141 	seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
3142 		   readl(regs + S3C_DIEPCTL(index)),
3143 		   readl(regs + S3C_DOEPCTL(index)));
3144 
3145 	seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
3146 		   readl(regs + S3C_DIEPDMA(index)),
3147 		   readl(regs + S3C_DOEPDMA(index)));
3148 
3149 	seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
3150 		   readl(regs + S3C_DIEPINT(index)),
3151 		   readl(regs + S3C_DOEPINT(index)));
3152 
3153 	seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
3154 		   readl(regs + S3C_DIEPTSIZ(index)),
3155 		   readl(regs + S3C_DOEPTSIZ(index)));
3156 
3157 	seq_printf(seq, "\n");
3158 	seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3159 	seq_printf(seq, "total_data=%ld\n", ep->total_data);
3160 
3161 	seq_printf(seq, "request list (%p,%p):\n",
3162 		   ep->queue.next, ep->queue.prev);
3163 
3164 	spin_lock_irqsave(&ep->lock, flags);
3165 
3166 	list_for_each_entry(req, &ep->queue, queue) {
3167 		if (--show_limit < 0) {
3168 			seq_printf(seq, "not showing more requests...\n");
3169 			break;
3170 		}
3171 
3172 		seq_printf(seq, "%c req %p: %d bytes @%p, ",
3173 			   req == ep->req ? '*' : ' ',
3174 			   req, req->req.length, req->req.buf);
3175 		seq_printf(seq, "%d done, res %d\n",
3176 			   req->req.actual, req->req.status);
3177 	}
3178 
3179 	spin_unlock_irqrestore(&ep->lock, flags);
3180 
3181 	return 0;
3182 }
3183 
ep_open(struct inode * inode,struct file * file)3184 static int ep_open(struct inode *inode, struct file *file)
3185 {
3186 	return single_open(file, ep_show, inode->i_private);
3187 }
3188 
3189 static const struct file_operations ep_fops = {
3190 	.owner		= THIS_MODULE,
3191 	.open		= ep_open,
3192 	.read		= seq_read,
3193 	.llseek		= seq_lseek,
3194 	.release	= single_release,
3195 };
3196 
3197 /**
3198  * s3c_hsotg_create_debug - create debugfs directory and files
3199  * @hsotg: The driver state
3200  *
3201  * Create the debugfs files to allow the user to get information
3202  * about the state of the system. The directory name is created
3203  * with the same name as the device itself, in case we end up
3204  * with multiple blocks in future systems.
3205 */
s3c_hsotg_create_debug(struct s3c_hsotg * hsotg)3206 static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3207 {
3208 	struct dentry *root;
3209 	unsigned epidx;
3210 
3211 	root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3212 	hsotg->debug_root = root;
3213 	if (IS_ERR(root)) {
3214 		dev_err(hsotg->dev, "cannot create debug root\n");
3215 		return;
3216 	}
3217 
3218 	/* create general state file */
3219 
3220 	hsotg->debug_file = debugfs_create_file("state", 0444, root,
3221 						hsotg, &state_fops);
3222 
3223 	if (IS_ERR(hsotg->debug_file))
3224 		dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3225 
3226 	hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3227 						hsotg, &fifo_fops);
3228 
3229 	if (IS_ERR(hsotg->debug_fifo))
3230 		dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3231 
3232 	/* create one file for each endpoint */
3233 
3234 	for (epidx = 0; epidx < S3C_HSOTG_EPS; epidx++) {
3235 		struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3236 
3237 		ep->debugfs = debugfs_create_file(ep->name, 0444,
3238 						  root, ep, &ep_fops);
3239 
3240 		if (IS_ERR(ep->debugfs))
3241 			dev_err(hsotg->dev, "failed to create %s debug file\n",
3242 				ep->name);
3243 	}
3244 }
3245 
3246 /**
3247  * s3c_hsotg_delete_debug - cleanup debugfs entries
3248  * @hsotg: The driver state
3249  *
3250  * Cleanup (remove) the debugfs files for use on module exit.
3251 */
s3c_hsotg_delete_debug(struct s3c_hsotg * hsotg)3252 static void __devexit s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3253 {
3254 	unsigned epidx;
3255 
3256 	for (epidx = 0; epidx < S3C_HSOTG_EPS; epidx++) {
3257 		struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3258 		debugfs_remove(ep->debugfs);
3259 	}
3260 
3261 	debugfs_remove(hsotg->debug_file);
3262 	debugfs_remove(hsotg->debug_fifo);
3263 	debugfs_remove(hsotg->debug_root);
3264 }
3265 
3266 /**
3267  * s3c_hsotg_gate - set the hardware gate for the block
3268  * @pdev: The device we bound to
3269  * @on: On or off.
3270  *
3271  * Set the hardware gate setting into the block. If we end up on
3272  * something other than an S3C64XX, then we might need to change this
3273  * to using a platform data callback, or some other mechanism.
3274  */
s3c_hsotg_gate(struct platform_device * pdev,bool on)3275 static void s3c_hsotg_gate(struct platform_device *pdev, bool on)
3276 {
3277 	unsigned long flags;
3278 	u32 others;
3279 
3280 	local_irq_save(flags);
3281 
3282 	others = __raw_readl(S3C64XX_OTHERS);
3283 	if (on)
3284 		others |= S3C64XX_OTHERS_USBMASK;
3285 	else
3286 		others &= ~S3C64XX_OTHERS_USBMASK;
3287 	__raw_writel(others, S3C64XX_OTHERS);
3288 
3289 	local_irq_restore(flags);
3290 }
3291 
3292 static struct s3c_hsotg_plat s3c_hsotg_default_pdata;
3293 
s3c_hsotg_probe(struct platform_device * pdev)3294 static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3295 {
3296 	struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
3297 	struct device *dev = &pdev->dev;
3298 	struct s3c_hsotg *hsotg;
3299 	struct resource *res;
3300 	int epnum;
3301 	int ret;
3302 
3303 	if (!plat)
3304 		plat = &s3c_hsotg_default_pdata;
3305 
3306 	hsotg = kzalloc(sizeof(struct s3c_hsotg) +
3307 			sizeof(struct s3c_hsotg_ep) * S3C_HSOTG_EPS,
3308 			GFP_KERNEL);
3309 	if (!hsotg) {
3310 		dev_err(dev, "cannot get memory\n");
3311 		return -ENOMEM;
3312 	}
3313 
3314 	hsotg->dev = dev;
3315 	hsotg->plat = plat;
3316 
3317 	hsotg->clk = clk_get(&pdev->dev, "otg");
3318 	if (IS_ERR(hsotg->clk)) {
3319 		dev_err(dev, "cannot get otg clock\n");
3320 		ret = PTR_ERR(hsotg->clk);
3321 		goto err_mem;
3322 	}
3323 
3324 	platform_set_drvdata(pdev, hsotg);
3325 
3326 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3327 	if (!res) {
3328 		dev_err(dev, "cannot find register resource 0\n");
3329 		ret = -EINVAL;
3330 		goto err_clk;
3331 	}
3332 
3333 	hsotg->regs_res = request_mem_region(res->start, resource_size(res),
3334 					     dev_name(dev));
3335 	if (!hsotg->regs_res) {
3336 		dev_err(dev, "cannot reserve registers\n");
3337 		ret = -ENOENT;
3338 		goto err_clk;
3339 	}
3340 
3341 	hsotg->regs = ioremap(res->start, resource_size(res));
3342 	if (!hsotg->regs) {
3343 		dev_err(dev, "cannot map registers\n");
3344 		ret = -ENXIO;
3345 		goto err_regs_res;
3346 	}
3347 
3348 	ret = platform_get_irq(pdev, 0);
3349 	if (ret < 0) {
3350 		dev_err(dev, "cannot find IRQ\n");
3351 		goto err_regs;
3352 	}
3353 
3354 	hsotg->irq = ret;
3355 
3356 	ret = request_irq(ret, s3c_hsotg_irq, 0, dev_name(dev), hsotg);
3357 	if (ret < 0) {
3358 		dev_err(dev, "cannot claim IRQ\n");
3359 		goto err_regs;
3360 	}
3361 
3362 	dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3363 
3364 	device_initialize(&hsotg->gadget.dev);
3365 
3366 	dev_set_name(&hsotg->gadget.dev, "gadget");
3367 
3368 	hsotg->gadget.max_speed = USB_SPEED_HIGH;
3369 	hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3370 	hsotg->gadget.name = dev_name(dev);
3371 
3372 	hsotg->gadget.dev.parent = dev;
3373 	hsotg->gadget.dev.dma_mask = dev->dma_mask;
3374 
3375 	/* setup endpoint information */
3376 
3377 	INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3378 	hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3379 
3380 	/* allocate EP0 request */
3381 
3382 	hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3383 						     GFP_KERNEL);
3384 	if (!hsotg->ctrl_req) {
3385 		dev_err(dev, "failed to allocate ctrl req\n");
3386 		goto err_regs;
3387 	}
3388 
3389 	/* reset the system */
3390 
3391 	clk_enable(hsotg->clk);
3392 
3393 	s3c_hsotg_gate(pdev, true);
3394 
3395 	s3c_hsotg_otgreset(hsotg);
3396 	s3c_hsotg_corereset(hsotg);
3397 	s3c_hsotg_init(hsotg);
3398 
3399 	/* initialise the endpoints now the core has been initialised */
3400 	for (epnum = 0; epnum < S3C_HSOTG_EPS; epnum++)
3401 		s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3402 
3403 	ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3404 	if (ret)
3405 		goto err_add_udc;
3406 
3407 	s3c_hsotg_create_debug(hsotg);
3408 
3409 	s3c_hsotg_dump(hsotg);
3410 
3411 	our_hsotg = hsotg;
3412 	return 0;
3413 
3414 err_add_udc:
3415 	s3c_hsotg_gate(pdev, false);
3416 	clk_disable(hsotg->clk);
3417 	clk_put(hsotg->clk);
3418 
3419 err_regs:
3420 	iounmap(hsotg->regs);
3421 
3422 err_regs_res:
3423 	release_resource(hsotg->regs_res);
3424 	kfree(hsotg->regs_res);
3425 err_clk:
3426 	clk_put(hsotg->clk);
3427 err_mem:
3428 	kfree(hsotg);
3429 	return ret;
3430 }
3431 
s3c_hsotg_remove(struct platform_device * pdev)3432 static int __devexit s3c_hsotg_remove(struct platform_device *pdev)
3433 {
3434 	struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3435 
3436 	usb_del_gadget_udc(&hsotg->gadget);
3437 
3438 	s3c_hsotg_delete_debug(hsotg);
3439 
3440 	usb_gadget_unregister_driver(hsotg->driver);
3441 
3442 	free_irq(hsotg->irq, hsotg);
3443 	iounmap(hsotg->regs);
3444 
3445 	release_resource(hsotg->regs_res);
3446 	kfree(hsotg->regs_res);
3447 
3448 	s3c_hsotg_gate(pdev, false);
3449 
3450 	clk_disable(hsotg->clk);
3451 	clk_put(hsotg->clk);
3452 
3453 	kfree(hsotg);
3454 	return 0;
3455 }
3456 
3457 #if 1
3458 #define s3c_hsotg_suspend NULL
3459 #define s3c_hsotg_resume NULL
3460 #endif
3461 
3462 static struct platform_driver s3c_hsotg_driver = {
3463 	.driver		= {
3464 		.name	= "s3c-hsotg",
3465 		.owner	= THIS_MODULE,
3466 	},
3467 	.probe		= s3c_hsotg_probe,
3468 	.remove		= __devexit_p(s3c_hsotg_remove),
3469 	.suspend	= s3c_hsotg_suspend,
3470 	.resume		= s3c_hsotg_resume,
3471 };
3472 
3473 module_platform_driver(s3c_hsotg_driver);
3474 
3475 MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3476 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3477 MODULE_LICENSE("GPL");
3478 MODULE_ALIAS("platform:s3c-hsotg");
3479