blob: 1de955082abcd8f8f732fe6a3d1553ffaf249e51 [file] [log] [blame]
Thomas Abrahama9df3042011-05-07 22:28:04 +02001/* linux/drivers/usb/gadget/s3c-hsudc.c
2 *
3 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
5 *
6 * S3C24XX USB 2.0 High-speed USB controller gadget driver
7 *
8 * The S3C24XX USB 2.0 high-speed USB controller supports upto 9 endpoints.
9 * Each endpoint can be configured as either in or out endpoint. Endpoints
10 * can be configured for Bulk or Interrupt transfer mode.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15*/
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/spinlock.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/dma-mapping.h>
23#include <linux/delay.h>
24#include <linux/io.h>
25#include <linux/slab.h>
26#include <linux/clk.h>
27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
Heiko Stübner938fbe542011-08-21 14:32:14 +020029#include <linux/usb/otg.h>
Bryan Wub38b03b2011-06-02 12:51:29 +080030#include <linux/prefetch.h>
Heiko Stübner715a3e42011-12-19 19:39:15 +010031#include <linux/platform_data/s3c-hsudc.h>
Thomas Abrahama9df3042011-05-07 22:28:04 +020032
33#include <mach/regs-s3c2443-clock.h>
Thomas Abrahama9df3042011-05-07 22:28:04 +020034
35#define S3C_HSUDC_REG(x) (x)
36
37/* Non-Indexed Registers */
38#define S3C_IR S3C_HSUDC_REG(0x00) /* Index Register */
39#define S3C_EIR S3C_HSUDC_REG(0x04) /* EP Intr Status */
40#define S3C_EIR_EP0 (1<<0)
41#define S3C_EIER S3C_HSUDC_REG(0x08) /* EP Intr Enable */
42#define S3C_FAR S3C_HSUDC_REG(0x0c) /* Gadget Address */
43#define S3C_FNR S3C_HSUDC_REG(0x10) /* Frame Number */
44#define S3C_EDR S3C_HSUDC_REG(0x14) /* EP Direction */
45#define S3C_TR S3C_HSUDC_REG(0x18) /* Test Register */
46#define S3C_SSR S3C_HSUDC_REG(0x1c) /* System Status */
47#define S3C_SSR_DTZIEN_EN (0xff8f)
48#define S3C_SSR_ERR (0xff80)
49#define S3C_SSR_VBUSON (1 << 8)
50#define S3C_SSR_HSP (1 << 4)
51#define S3C_SSR_SDE (1 << 3)
52#define S3C_SSR_RESUME (1 << 2)
53#define S3C_SSR_SUSPEND (1 << 1)
54#define S3C_SSR_RESET (1 << 0)
55#define S3C_SCR S3C_HSUDC_REG(0x20) /* System Control */
56#define S3C_SCR_DTZIEN_EN (1 << 14)
57#define S3C_SCR_RRD_EN (1 << 5)
58#define S3C_SCR_SUS_EN (1 << 1)
59#define S3C_SCR_RST_EN (1 << 0)
60#define S3C_EP0SR S3C_HSUDC_REG(0x24) /* EP0 Status */
61#define S3C_EP0SR_EP0_LWO (1 << 6)
62#define S3C_EP0SR_STALL (1 << 4)
63#define S3C_EP0SR_TX_SUCCESS (1 << 1)
64#define S3C_EP0SR_RX_SUCCESS (1 << 0)
65#define S3C_EP0CR S3C_HSUDC_REG(0x28) /* EP0 Control */
66#define S3C_BR(_x) S3C_HSUDC_REG(0x60 + (_x * 4))
67
68/* Indexed Registers */
69#define S3C_ESR S3C_HSUDC_REG(0x2c) /* EPn Status */
70#define S3C_ESR_FLUSH (1 << 6)
71#define S3C_ESR_STALL (1 << 5)
72#define S3C_ESR_LWO (1 << 4)
73#define S3C_ESR_PSIF_ONE (1 << 2)
74#define S3C_ESR_PSIF_TWO (2 << 2)
75#define S3C_ESR_TX_SUCCESS (1 << 1)
76#define S3C_ESR_RX_SUCCESS (1 << 0)
77#define S3C_ECR S3C_HSUDC_REG(0x30) /* EPn Control */
78#define S3C_ECR_DUEN (1 << 7)
79#define S3C_ECR_FLUSH (1 << 6)
80#define S3C_ECR_STALL (1 << 1)
81#define S3C_ECR_IEMS (1 << 0)
82#define S3C_BRCR S3C_HSUDC_REG(0x34) /* Read Count */
83#define S3C_BWCR S3C_HSUDC_REG(0x38) /* Write Count */
84#define S3C_MPR S3C_HSUDC_REG(0x3c) /* Max Pkt Size */
85
86#define WAIT_FOR_SETUP (0)
87#define DATA_STATE_XMIT (1)
88#define DATA_STATE_RECV (2)
89
90/**
91 * struct s3c_hsudc_ep - Endpoint representation used by driver.
92 * @ep: USB gadget layer representation of device endpoint.
93 * @name: Endpoint name (as required by ep autoconfiguration).
94 * @dev: Reference to the device controller to which this EP belongs.
95 * @desc: Endpoint descriptor obtained from the gadget driver.
96 * @queue: Transfer request queue for the endpoint.
97 * @stopped: Maintains state of endpoint, set if EP is halted.
98 * @bEndpointAddress: EP address (including direction bit).
99 * @fifo: Base address of EP FIFO.
100 */
101struct s3c_hsudc_ep {
102 struct usb_ep ep;
103 char name[20];
104 struct s3c_hsudc *dev;
105 const struct usb_endpoint_descriptor *desc;
106 struct list_head queue;
107 u8 stopped;
108 u8 wedge;
109 u8 bEndpointAddress;
110 void __iomem *fifo;
111};
112
113/**
114 * struct s3c_hsudc_req - Driver encapsulation of USB gadget transfer request.
115 * @req: Reference to USB gadget transfer request.
116 * @queue: Used for inserting this request to the endpoint request queue.
117 */
118struct s3c_hsudc_req {
119 struct usb_request req;
120 struct list_head queue;
121};
122
123/**
124 * struct s3c_hsudc - Driver's abstraction of the device controller.
125 * @gadget: Instance of usb_gadget which is referenced by gadget driver.
126 * @driver: Reference to currenty active gadget driver.
127 * @dev: The device reference used by probe function.
128 * @lock: Lock to synchronize the usage of Endpoints (EP's are indexed).
129 * @regs: Remapped base address of controller's register space.
130 * @mem_rsrc: Device memory resource used for remapping device register space.
131 * irq: IRQ number used by the controller.
132 * uclk: Reference to the controller clock.
133 * ep0state: Current state of EP0.
134 * ep: List of endpoints supported by the controller.
135 */
136struct s3c_hsudc {
137 struct usb_gadget gadget;
138 struct usb_gadget_driver *driver;
139 struct device *dev;
140 struct s3c24xx_hsudc_platdata *pd;
Heiko Stübner938fbe542011-08-21 14:32:14 +0200141 struct otg_transceiver *transceiver;
Thomas Abrahama9df3042011-05-07 22:28:04 +0200142 spinlock_t lock;
143 void __iomem *regs;
144 struct resource *mem_rsrc;
145 int irq;
146 struct clk *uclk;
147 int ep0state;
148 struct s3c_hsudc_ep ep[];
149};
150
151#define ep_maxpacket(_ep) ((_ep)->ep.maxpacket)
152#define ep_is_in(_ep) ((_ep)->bEndpointAddress & USB_DIR_IN)
153#define ep_index(_ep) ((_ep)->bEndpointAddress & \
154 USB_ENDPOINT_NUMBER_MASK)
155
156static struct s3c_hsudc *the_controller;
157static const char driver_name[] = "s3c-udc";
158static const char ep0name[] = "ep0-control";
159
160static inline struct s3c_hsudc_req *our_req(struct usb_request *req)
161{
162 return container_of(req, struct s3c_hsudc_req, req);
163}
164
165static inline struct s3c_hsudc_ep *our_ep(struct usb_ep *ep)
166{
167 return container_of(ep, struct s3c_hsudc_ep, ep);
168}
169
170static inline struct s3c_hsudc *to_hsudc(struct usb_gadget *gadget)
171{
172 return container_of(gadget, struct s3c_hsudc, gadget);
173}
174
175static inline void set_index(struct s3c_hsudc *hsudc, int ep_addr)
176{
177 ep_addr &= USB_ENDPOINT_NUMBER_MASK;
178 writel(ep_addr, hsudc->regs + S3C_IR);
179}
180
181static inline void __orr32(void __iomem *ptr, u32 val)
182{
183 writel(readl(ptr) | val, ptr);
184}
185
186static void s3c_hsudc_init_phy(void)
187{
188 u32 cfg;
189
190 cfg = readl(S3C2443_PWRCFG) | S3C2443_PWRCFG_USBPHY;
191 writel(cfg, S3C2443_PWRCFG);
192
193 cfg = readl(S3C2443_URSTCON);
194 cfg |= (S3C2443_URSTCON_FUNCRST | S3C2443_URSTCON_PHYRST);
195 writel(cfg, S3C2443_URSTCON);
196 mdelay(1);
197
198 cfg = readl(S3C2443_URSTCON);
199 cfg &= ~(S3C2443_URSTCON_FUNCRST | S3C2443_URSTCON_PHYRST);
200 writel(cfg, S3C2443_URSTCON);
201
202 cfg = readl(S3C2443_PHYCTRL);
203 cfg &= ~(S3C2443_PHYCTRL_CLKSEL | S3C2443_PHYCTRL_DSPORT);
204 cfg |= (S3C2443_PHYCTRL_EXTCLK | S3C2443_PHYCTRL_PLLSEL);
205 writel(cfg, S3C2443_PHYCTRL);
206
207 cfg = readl(S3C2443_PHYPWR);
208 cfg &= ~(S3C2443_PHYPWR_FSUSPEND | S3C2443_PHYPWR_PLL_PWRDN |
209 S3C2443_PHYPWR_XO_ON | S3C2443_PHYPWR_PLL_REFCLK |
210 S3C2443_PHYPWR_ANALOG_PD);
211 cfg |= S3C2443_PHYPWR_COMMON_ON;
212 writel(cfg, S3C2443_PHYPWR);
213
214 cfg = readl(S3C2443_UCLKCON);
215 cfg |= (S3C2443_UCLKCON_DETECT_VBUS | S3C2443_UCLKCON_FUNC_CLKEN |
216 S3C2443_UCLKCON_TCLKEN);
217 writel(cfg, S3C2443_UCLKCON);
218}
219
220static void s3c_hsudc_uninit_phy(void)
221{
222 u32 cfg;
223
224 cfg = readl(S3C2443_PWRCFG) & ~S3C2443_PWRCFG_USBPHY;
225 writel(cfg, S3C2443_PWRCFG);
226
227 writel(S3C2443_PHYPWR_FSUSPEND, S3C2443_PHYPWR);
228
229 cfg = readl(S3C2443_UCLKCON) & ~S3C2443_UCLKCON_FUNC_CLKEN;
230 writel(cfg, S3C2443_UCLKCON);
231}
232
233/**
234 * s3c_hsudc_complete_request - Complete a transfer request.
235 * @hsep: Endpoint to which the request belongs.
236 * @hsreq: Transfer request to be completed.
237 * @status: Transfer completion status for the transfer request.
238 */
239static void s3c_hsudc_complete_request(struct s3c_hsudc_ep *hsep,
240 struct s3c_hsudc_req *hsreq, int status)
241{
242 unsigned int stopped = hsep->stopped;
243 struct s3c_hsudc *hsudc = hsep->dev;
244
245 list_del_init(&hsreq->queue);
246 hsreq->req.status = status;
247
248 if (!ep_index(hsep)) {
249 hsudc->ep0state = WAIT_FOR_SETUP;
250 hsep->bEndpointAddress &= ~USB_DIR_IN;
251 }
252
253 hsep->stopped = 1;
254 spin_unlock(&hsudc->lock);
255 if (hsreq->req.complete != NULL)
256 hsreq->req.complete(&hsep->ep, &hsreq->req);
257 spin_lock(&hsudc->lock);
258 hsep->stopped = stopped;
259}
260
261/**
262 * s3c_hsudc_nuke_ep - Terminate all requests queued for a endpoint.
263 * @hsep: Endpoint for which queued requests have to be terminated.
264 * @status: Transfer completion status for the transfer request.
265 */
266static void s3c_hsudc_nuke_ep(struct s3c_hsudc_ep *hsep, int status)
267{
268 struct s3c_hsudc_req *hsreq;
269
270 while (!list_empty(&hsep->queue)) {
271 hsreq = list_entry(hsep->queue.next,
272 struct s3c_hsudc_req, queue);
273 s3c_hsudc_complete_request(hsep, hsreq, status);
274 }
275}
276
277/**
278 * s3c_hsudc_stop_activity - Stop activity on all endpoints.
279 * @hsudc: Device controller for which EP activity is to be stopped.
280 * @driver: Reference to the gadget driver which is currently active.
281 *
282 * All the endpoints are stopped and any pending transfer requests if any on
283 * the endpoint are terminated.
284 */
Heiko Stübnerd93e2602011-12-19 19:41:45 +0100285static void s3c_hsudc_stop_activity(struct s3c_hsudc *hsudc)
Thomas Abrahama9df3042011-05-07 22:28:04 +0200286{
287 struct s3c_hsudc_ep *hsep;
288 int epnum;
289
290 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
291
292 for (epnum = 0; epnum < hsudc->pd->epnum; epnum++) {
293 hsep = &hsudc->ep[epnum];
294 hsep->stopped = 1;
295 s3c_hsudc_nuke_ep(hsep, -ESHUTDOWN);
296 }
Thomas Abrahama9df3042011-05-07 22:28:04 +0200297}
298
299/**
300 * s3c_hsudc_read_setup_pkt - Read the received setup packet from EP0 fifo.
301 * @hsudc: Device controller from which setup packet is to be read.
302 * @buf: The buffer into which the setup packet is read.
303 *
304 * The setup packet received in the EP0 fifo is read and stored into a
305 * given buffer address.
306 */
307
308static void s3c_hsudc_read_setup_pkt(struct s3c_hsudc *hsudc, u16 *buf)
309{
310 int count;
311
312 count = readl(hsudc->regs + S3C_BRCR);
313 while (count--)
314 *buf++ = (u16)readl(hsudc->regs + S3C_BR(0));
315
316 writel(S3C_EP0SR_RX_SUCCESS, hsudc->regs + S3C_EP0SR);
317}
318
319/**
320 * s3c_hsudc_write_fifo - Write next chunk of transfer data to EP fifo.
321 * @hsep: Endpoint to which the data is to be written.
322 * @hsreq: Transfer request from which the next chunk of data is written.
323 *
324 * Write the next chunk of data from a transfer request to the endpoint FIFO.
325 * If the transfer request completes, 1 is returned, otherwise 0 is returned.
326 */
327static int s3c_hsudc_write_fifo(struct s3c_hsudc_ep *hsep,
328 struct s3c_hsudc_req *hsreq)
329{
330 u16 *buf;
331 u32 max = ep_maxpacket(hsep);
332 u32 count, length;
333 bool is_last;
334 void __iomem *fifo = hsep->fifo;
335
336 buf = hsreq->req.buf + hsreq->req.actual;
337 prefetch(buf);
338
339 length = hsreq->req.length - hsreq->req.actual;
340 length = min(length, max);
341 hsreq->req.actual += length;
342
343 writel(length, hsep->dev->regs + S3C_BWCR);
344 for (count = 0; count < length; count += 2)
345 writel(*buf++, fifo);
346
347 if (count != max) {
348 is_last = true;
349 } else {
350 if (hsreq->req.length != hsreq->req.actual || hsreq->req.zero)
351 is_last = false;
352 else
353 is_last = true;
354 }
355
356 if (is_last) {
357 s3c_hsudc_complete_request(hsep, hsreq, 0);
358 return 1;
359 }
360
361 return 0;
362}
363
364/**
365 * s3c_hsudc_read_fifo - Read the next chunk of data from EP fifo.
366 * @hsep: Endpoint from which the data is to be read.
367 * @hsreq: Transfer request to which the next chunk of data read is written.
368 *
369 * Read the next chunk of data from the endpoint FIFO and a write it to the
370 * transfer request buffer. If the transfer request completes, 1 is returned,
371 * otherwise 0 is returned.
372 */
373static int s3c_hsudc_read_fifo(struct s3c_hsudc_ep *hsep,
374 struct s3c_hsudc_req *hsreq)
375{
376 struct s3c_hsudc *hsudc = hsep->dev;
377 u32 csr, offset;
378 u16 *buf, word;
379 u32 buflen, rcnt, rlen;
380 void __iomem *fifo = hsep->fifo;
381 u32 is_short = 0;
382
383 offset = (ep_index(hsep)) ? S3C_ESR : S3C_EP0SR;
384 csr = readl(hsudc->regs + offset);
385 if (!(csr & S3C_ESR_RX_SUCCESS))
386 return -EINVAL;
387
388 buf = hsreq->req.buf + hsreq->req.actual;
389 prefetchw(buf);
390 buflen = hsreq->req.length - hsreq->req.actual;
391
392 rcnt = readl(hsudc->regs + S3C_BRCR);
393 rlen = (csr & S3C_ESR_LWO) ? (rcnt * 2 - 1) : (rcnt * 2);
394
395 hsreq->req.actual += min(rlen, buflen);
396 is_short = (rlen < hsep->ep.maxpacket);
397
398 while (rcnt-- != 0) {
399 word = (u16)readl(fifo);
400 if (buflen) {
401 *buf++ = word;
402 buflen--;
403 } else {
404 hsreq->req.status = -EOVERFLOW;
405 }
406 }
407
408 writel(S3C_ESR_RX_SUCCESS, hsudc->regs + offset);
409
410 if (is_short || hsreq->req.actual == hsreq->req.length) {
411 s3c_hsudc_complete_request(hsep, hsreq, 0);
412 return 1;
413 }
414
415 return 0;
416}
417
418/**
419 * s3c_hsudc_epin_intr - Handle in-endpoint interrupt.
420 * @hsudc - Device controller for which the interrupt is to be handled.
421 * @ep_idx - Endpoint number on which an interrupt is pending.
422 *
423 * Handles interrupt for a in-endpoint. The interrupts that are handled are
424 * stall and data transmit complete interrupt.
425 */
426static void s3c_hsudc_epin_intr(struct s3c_hsudc *hsudc, u32 ep_idx)
427{
428 struct s3c_hsudc_ep *hsep = &hsudc->ep[ep_idx];
429 struct s3c_hsudc_req *hsreq;
430 u32 csr;
431
432 csr = readl((u32)hsudc->regs + S3C_ESR);
433 if (csr & S3C_ESR_STALL) {
434 writel(S3C_ESR_STALL, hsudc->regs + S3C_ESR);
435 return;
436 }
437
438 if (csr & S3C_ESR_TX_SUCCESS) {
439 writel(S3C_ESR_TX_SUCCESS, hsudc->regs + S3C_ESR);
440 if (list_empty(&hsep->queue))
441 return;
442
443 hsreq = list_entry(hsep->queue.next,
444 struct s3c_hsudc_req, queue);
445 if ((s3c_hsudc_write_fifo(hsep, hsreq) == 0) &&
446 (csr & S3C_ESR_PSIF_TWO))
447 s3c_hsudc_write_fifo(hsep, hsreq);
448 }
449}
450
451/**
452 * s3c_hsudc_epout_intr - Handle out-endpoint interrupt.
453 * @hsudc - Device controller for which the interrupt is to be handled.
454 * @ep_idx - Endpoint number on which an interrupt is pending.
455 *
456 * Handles interrupt for a out-endpoint. The interrupts that are handled are
457 * stall, flush and data ready interrupt.
458 */
459static void s3c_hsudc_epout_intr(struct s3c_hsudc *hsudc, u32 ep_idx)
460{
461 struct s3c_hsudc_ep *hsep = &hsudc->ep[ep_idx];
462 struct s3c_hsudc_req *hsreq;
463 u32 csr;
464
465 csr = readl((u32)hsudc->regs + S3C_ESR);
466 if (csr & S3C_ESR_STALL) {
467 writel(S3C_ESR_STALL, hsudc->regs + S3C_ESR);
468 return;
469 }
470
471 if (csr & S3C_ESR_FLUSH) {
472 __orr32(hsudc->regs + S3C_ECR, S3C_ECR_FLUSH);
473 return;
474 }
475
476 if (csr & S3C_ESR_RX_SUCCESS) {
477 if (list_empty(&hsep->queue))
478 return;
479
480 hsreq = list_entry(hsep->queue.next,
481 struct s3c_hsudc_req, queue);
482 if (((s3c_hsudc_read_fifo(hsep, hsreq)) == 0) &&
483 (csr & S3C_ESR_PSIF_TWO))
484 s3c_hsudc_read_fifo(hsep, hsreq);
485 }
486}
487
488/** s3c_hsudc_set_halt - Set or clear a endpoint halt.
489 * @_ep: Endpoint on which halt has to be set or cleared.
490 * @value: 1 for setting halt on endpoint, 0 to clear halt.
491 *
492 * Set or clear endpoint halt. If halt is set, the endpoint is stopped.
493 * If halt is cleared, for in-endpoints, if there are any pending
494 * transfer requests, transfers are started.
495 */
496static int s3c_hsudc_set_halt(struct usb_ep *_ep, int value)
497{
498 struct s3c_hsudc_ep *hsep = our_ep(_ep);
499 struct s3c_hsudc *hsudc = hsep->dev;
500 struct s3c_hsudc_req *hsreq;
501 unsigned long irqflags;
502 u32 ecr;
503 u32 offset;
504
505 if (value && ep_is_in(hsep) && !list_empty(&hsep->queue))
506 return -EAGAIN;
507
508 spin_lock_irqsave(&hsudc->lock, irqflags);
509 set_index(hsudc, ep_index(hsep));
510 offset = (ep_index(hsep)) ? S3C_ECR : S3C_EP0CR;
511 ecr = readl(hsudc->regs + offset);
512
513 if (value) {
514 ecr |= S3C_ECR_STALL;
515 if (ep_index(hsep))
516 ecr |= S3C_ECR_FLUSH;
517 hsep->stopped = 1;
518 } else {
519 ecr &= ~S3C_ECR_STALL;
520 hsep->stopped = hsep->wedge = 0;
521 }
522 writel(ecr, hsudc->regs + offset);
523
524 if (ep_is_in(hsep) && !list_empty(&hsep->queue) && !value) {
525 hsreq = list_entry(hsep->queue.next,
526 struct s3c_hsudc_req, queue);
527 if (hsreq)
528 s3c_hsudc_write_fifo(hsep, hsreq);
529 }
530
531 spin_unlock_irqrestore(&hsudc->lock, irqflags);
532 return 0;
533}
534
535/** s3c_hsudc_set_wedge - Sets the halt feature with the clear requests ignored
536 * @_ep: Endpoint on which wedge has to be set.
537 *
538 * Sets the halt feature with the clear requests ignored.
539 */
540static int s3c_hsudc_set_wedge(struct usb_ep *_ep)
541{
542 struct s3c_hsudc_ep *hsep = our_ep(_ep);
543
544 if (!hsep)
545 return -EINVAL;
546
547 hsep->wedge = 1;
548 return usb_ep_set_halt(_ep);
549}
550
551/** s3c_hsudc_handle_reqfeat - Handle set feature or clear feature requests.
552 * @_ep: Device controller on which the set/clear feature needs to be handled.
553 * @ctrl: Control request as received on the endpoint 0.
554 *
555 * Handle set feature or clear feature control requests on the control endpoint.
556 */
557static int s3c_hsudc_handle_reqfeat(struct s3c_hsudc *hsudc,
558 struct usb_ctrlrequest *ctrl)
559{
560 struct s3c_hsudc_ep *hsep;
561 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
562 u8 ep_num = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
563
564 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
565 hsep = &hsudc->ep[ep_num];
566 switch (le16_to_cpu(ctrl->wValue)) {
567 case USB_ENDPOINT_HALT:
568 if (set || (!set && !hsep->wedge))
569 s3c_hsudc_set_halt(&hsep->ep, set);
570 return 0;
571 }
572 }
573
574 return -ENOENT;
575}
576
577/**
578 * s3c_hsudc_process_req_status - Handle get status control request.
579 * @hsudc: Device controller on which get status request has be handled.
580 * @ctrl: Control request as received on the endpoint 0.
581 *
582 * Handle get status control request received on control endpoint.
583 */
584static void s3c_hsudc_process_req_status(struct s3c_hsudc *hsudc,
585 struct usb_ctrlrequest *ctrl)
586{
587 struct s3c_hsudc_ep *hsep0 = &hsudc->ep[0];
588 struct s3c_hsudc_req hsreq;
589 struct s3c_hsudc_ep *hsep;
590 __le16 reply;
591 u8 epnum;
592
593 switch (ctrl->bRequestType & USB_RECIP_MASK) {
594 case USB_RECIP_DEVICE:
595 reply = cpu_to_le16(0);
596 break;
597
598 case USB_RECIP_INTERFACE:
599 reply = cpu_to_le16(0);
600 break;
601
602 case USB_RECIP_ENDPOINT:
603 epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
604 hsep = &hsudc->ep[epnum];
605 reply = cpu_to_le16(hsep->stopped ? 1 : 0);
606 break;
607 }
608
609 INIT_LIST_HEAD(&hsreq.queue);
610 hsreq.req.length = 2;
611 hsreq.req.buf = &reply;
612 hsreq.req.actual = 0;
613 hsreq.req.complete = NULL;
614 s3c_hsudc_write_fifo(hsep0, &hsreq);
615}
616
617/**
618 * s3c_hsudc_process_setup - Process control request received on endpoint 0.
619 * @hsudc: Device controller on which control request has been received.
620 *
621 * Read the control request received on endpoint 0, decode it and handle
622 * the request.
623 */
624static void s3c_hsudc_process_setup(struct s3c_hsudc *hsudc)
625{
626 struct s3c_hsudc_ep *hsep = &hsudc->ep[0];
627 struct usb_ctrlrequest ctrl = {0};
628 int ret;
629
630 s3c_hsudc_nuke_ep(hsep, -EPROTO);
631 s3c_hsudc_read_setup_pkt(hsudc, (u16 *)&ctrl);
632
633 if (ctrl.bRequestType & USB_DIR_IN) {
634 hsep->bEndpointAddress |= USB_DIR_IN;
635 hsudc->ep0state = DATA_STATE_XMIT;
636 } else {
637 hsep->bEndpointAddress &= ~USB_DIR_IN;
638 hsudc->ep0state = DATA_STATE_RECV;
639 }
640
641 switch (ctrl.bRequest) {
642 case USB_REQ_SET_ADDRESS:
643 if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
644 break;
645 hsudc->ep0state = WAIT_FOR_SETUP;
646 return;
647
648 case USB_REQ_GET_STATUS:
649 if ((ctrl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
650 break;
651 s3c_hsudc_process_req_status(hsudc, &ctrl);
652 return;
653
654 case USB_REQ_SET_FEATURE:
655 case USB_REQ_CLEAR_FEATURE:
656 if ((ctrl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
657 break;
658 s3c_hsudc_handle_reqfeat(hsudc, &ctrl);
659 hsudc->ep0state = WAIT_FOR_SETUP;
660 return;
661 }
662
663 if (hsudc->driver) {
664 spin_unlock(&hsudc->lock);
665 ret = hsudc->driver->setup(&hsudc->gadget, &ctrl);
666 spin_lock(&hsudc->lock);
667
668 if (ctrl.bRequest == USB_REQ_SET_CONFIGURATION) {
669 hsep->bEndpointAddress &= ~USB_DIR_IN;
670 hsudc->ep0state = WAIT_FOR_SETUP;
671 }
672
673 if (ret < 0) {
674 dev_err(hsudc->dev, "setup failed, returned %d\n",
675 ret);
676 s3c_hsudc_set_halt(&hsep->ep, 1);
677 hsudc->ep0state = WAIT_FOR_SETUP;
678 hsep->bEndpointAddress &= ~USB_DIR_IN;
679 }
680 }
681}
682
683/** s3c_hsudc_handle_ep0_intr - Handle endpoint 0 interrupt.
684 * @hsudc: Device controller on which endpoint 0 interrupt has occured.
685 *
686 * Handle endpoint 0 interrupt when it occurs. EP0 interrupt could occur
687 * when a stall handshake is sent to host or data is sent/received on
688 * endpoint 0.
689 */
690static void s3c_hsudc_handle_ep0_intr(struct s3c_hsudc *hsudc)
691{
692 struct s3c_hsudc_ep *hsep = &hsudc->ep[0];
693 struct s3c_hsudc_req *hsreq;
694 u32 csr = readl(hsudc->regs + S3C_EP0SR);
695 u32 ecr;
696
697 if (csr & S3C_EP0SR_STALL) {
698 ecr = readl(hsudc->regs + S3C_EP0CR);
699 ecr &= ~(S3C_ECR_STALL | S3C_ECR_FLUSH);
700 writel(ecr, hsudc->regs + S3C_EP0CR);
701
702 writel(S3C_EP0SR_STALL, hsudc->regs + S3C_EP0SR);
703 hsep->stopped = 0;
704
705 s3c_hsudc_nuke_ep(hsep, -ECONNABORTED);
706 hsudc->ep0state = WAIT_FOR_SETUP;
707 hsep->bEndpointAddress &= ~USB_DIR_IN;
708 return;
709 }
710
711 if (csr & S3C_EP0SR_TX_SUCCESS) {
712 writel(S3C_EP0SR_TX_SUCCESS, hsudc->regs + S3C_EP0SR);
713 if (ep_is_in(hsep)) {
714 if (list_empty(&hsep->queue))
715 return;
716
717 hsreq = list_entry(hsep->queue.next,
718 struct s3c_hsudc_req, queue);
719 s3c_hsudc_write_fifo(hsep, hsreq);
720 }
721 }
722
723 if (csr & S3C_EP0SR_RX_SUCCESS) {
724 if (hsudc->ep0state == WAIT_FOR_SETUP)
725 s3c_hsudc_process_setup(hsudc);
726 else {
727 if (!ep_is_in(hsep)) {
728 if (list_empty(&hsep->queue))
729 return;
730 hsreq = list_entry(hsep->queue.next,
731 struct s3c_hsudc_req, queue);
732 s3c_hsudc_read_fifo(hsep, hsreq);
733 }
734 }
735 }
736}
737
738/**
739 * s3c_hsudc_ep_enable - Enable a endpoint.
740 * @_ep: The endpoint to be enabled.
741 * @desc: Endpoint descriptor.
742 *
743 * Enables a endpoint when called from the gadget driver. Endpoint stall if
744 * any is cleared, transfer type is configured and endpoint interrupt is
745 * enabled.
746 */
747static int s3c_hsudc_ep_enable(struct usb_ep *_ep,
748 const struct usb_endpoint_descriptor *desc)
749{
750 struct s3c_hsudc_ep *hsep;
751 struct s3c_hsudc *hsudc;
752 unsigned long flags;
753 u32 ecr = 0;
754
755 hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
756 if (!_ep || !desc || hsep->desc || _ep->name == ep0name
757 || desc->bDescriptorType != USB_DT_ENDPOINT
758 || hsep->bEndpointAddress != desc->bEndpointAddress
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700759 || ep_maxpacket(hsep) < usb_endpoint_maxp(desc))
Thomas Abrahama9df3042011-05-07 22:28:04 +0200760 return -EINVAL;
761
762 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700763 && usb_endpoint_maxp(desc) != ep_maxpacket(hsep))
Thomas Abrahama9df3042011-05-07 22:28:04 +0200764 || !desc->wMaxPacketSize)
765 return -ERANGE;
766
767 hsudc = hsep->dev;
768 if (!hsudc->driver || hsudc->gadget.speed == USB_SPEED_UNKNOWN)
769 return -ESHUTDOWN;
770
771 spin_lock_irqsave(&hsudc->lock, flags);
772
773 set_index(hsudc, hsep->bEndpointAddress);
774 ecr |= ((usb_endpoint_xfer_int(desc)) ? S3C_ECR_IEMS : S3C_ECR_DUEN);
775 writel(ecr, hsudc->regs + S3C_ECR);
776
777 hsep->stopped = hsep->wedge = 0;
778 hsep->desc = desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700779 hsep->ep.maxpacket = usb_endpoint_maxp(desc);
Thomas Abrahama9df3042011-05-07 22:28:04 +0200780
781 s3c_hsudc_set_halt(_ep, 0);
782 __set_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
783
784 spin_unlock_irqrestore(&hsudc->lock, flags);
785 return 0;
786}
787
788/**
789 * s3c_hsudc_ep_disable - Disable a endpoint.
790 * @_ep: The endpoint to be disabled.
791 * @desc: Endpoint descriptor.
792 *
793 * Disables a endpoint when called from the gadget driver.
794 */
795static int s3c_hsudc_ep_disable(struct usb_ep *_ep)
796{
797 struct s3c_hsudc_ep *hsep = our_ep(_ep);
798 struct s3c_hsudc *hsudc = hsep->dev;
799 unsigned long flags;
800
801 if (!_ep || !hsep->desc)
802 return -EINVAL;
803
804 spin_lock_irqsave(&hsudc->lock, flags);
805
806 set_index(hsudc, hsep->bEndpointAddress);
807 __clear_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
808
809 s3c_hsudc_nuke_ep(hsep, -ESHUTDOWN);
810
811 hsep->desc = 0;
812 hsep->stopped = 1;
813
814 spin_unlock_irqrestore(&hsudc->lock, flags);
815 return 0;
816}
817
818/**
819 * s3c_hsudc_alloc_request - Allocate a new request.
820 * @_ep: Endpoint for which request is allocated (not used).
821 * @gfp_flags: Flags used for the allocation.
822 *
823 * Allocates a single transfer request structure when called from gadget driver.
824 */
825static struct usb_request *s3c_hsudc_alloc_request(struct usb_ep *_ep,
826 gfp_t gfp_flags)
827{
828 struct s3c_hsudc_req *hsreq;
829
830 hsreq = kzalloc(sizeof *hsreq, gfp_flags);
831 if (!hsreq)
832 return 0;
833
834 INIT_LIST_HEAD(&hsreq->queue);
835 return &hsreq->req;
836}
837
838/**
839 * s3c_hsudc_free_request - Deallocate a request.
840 * @ep: Endpoint for which request is deallocated (not used).
841 * @_req: Request to be deallocated.
842 *
843 * Allocates a single transfer request structure when called from gadget driver.
844 */
845static void s3c_hsudc_free_request(struct usb_ep *ep, struct usb_request *_req)
846{
847 struct s3c_hsudc_req *hsreq;
848
849 hsreq = container_of(_req, struct s3c_hsudc_req, req);
850 WARN_ON(!list_empty(&hsreq->queue));
851 kfree(hsreq);
852}
853
854/**
855 * s3c_hsudc_queue - Queue a transfer request for the endpoint.
856 * @_ep: Endpoint for which the request is queued.
857 * @_req: Request to be queued.
858 * @gfp_flags: Not used.
859 *
860 * Start or enqueue a request for a endpoint when called from gadget driver.
861 */
862static int s3c_hsudc_queue(struct usb_ep *_ep, struct usb_request *_req,
863 gfp_t gfp_flags)
864{
865 struct s3c_hsudc_req *hsreq;
866 struct s3c_hsudc_ep *hsep;
867 struct s3c_hsudc *hsudc;
868 unsigned long flags;
869 u32 offset;
870 u32 csr;
871
872 hsreq = container_of(_req, struct s3c_hsudc_req, req);
873 if ((!_req || !_req->complete || !_req->buf ||
874 !list_empty(&hsreq->queue)))
875 return -EINVAL;
876
877 hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
878 hsudc = hsep->dev;
879 if (!hsudc->driver || hsudc->gadget.speed == USB_SPEED_UNKNOWN)
880 return -ESHUTDOWN;
881
882 spin_lock_irqsave(&hsudc->lock, flags);
883 set_index(hsudc, hsep->bEndpointAddress);
884
885 _req->status = -EINPROGRESS;
886 _req->actual = 0;
887
888 if (!ep_index(hsep) && _req->length == 0) {
889 hsudc->ep0state = WAIT_FOR_SETUP;
890 s3c_hsudc_complete_request(hsep, hsreq, 0);
891 spin_unlock_irqrestore(&hsudc->lock, flags);
892 return 0;
893 }
894
895 if (list_empty(&hsep->queue) && !hsep->stopped) {
896 offset = (ep_index(hsep)) ? S3C_ESR : S3C_EP0SR;
897 if (ep_is_in(hsep)) {
898 csr = readl((u32)hsudc->regs + offset);
899 if (!(csr & S3C_ESR_TX_SUCCESS) &&
900 (s3c_hsudc_write_fifo(hsep, hsreq) == 1))
901 hsreq = 0;
902 } else {
903 csr = readl((u32)hsudc->regs + offset);
904 if ((csr & S3C_ESR_RX_SUCCESS)
905 && (s3c_hsudc_read_fifo(hsep, hsreq) == 1))
906 hsreq = 0;
907 }
908 }
909
910 if (hsreq != 0)
911 list_add_tail(&hsreq->queue, &hsep->queue);
912
913 spin_unlock_irqrestore(&hsudc->lock, flags);
914 return 0;
915}
916
917/**
918 * s3c_hsudc_dequeue - Dequeue a transfer request from an endpoint.
919 * @_ep: Endpoint from which the request is dequeued.
920 * @_req: Request to be dequeued.
921 *
922 * Dequeue a request from a endpoint when called from gadget driver.
923 */
924static int s3c_hsudc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
925{
926 struct s3c_hsudc_ep *hsep = our_ep(_ep);
927 struct s3c_hsudc *hsudc = hsep->dev;
928 struct s3c_hsudc_req *hsreq;
929 unsigned long flags;
930
931 hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
932 if (!_ep || hsep->ep.name == ep0name)
933 return -EINVAL;
934
935 spin_lock_irqsave(&hsudc->lock, flags);
936
937 list_for_each_entry(hsreq, &hsep->queue, queue) {
938 if (&hsreq->req == _req)
939 break;
940 }
941 if (&hsreq->req != _req) {
942 spin_unlock_irqrestore(&hsudc->lock, flags);
943 return -EINVAL;
944 }
945
946 set_index(hsudc, hsep->bEndpointAddress);
947 s3c_hsudc_complete_request(hsep, hsreq, -ECONNRESET);
948
949 spin_unlock_irqrestore(&hsudc->lock, flags);
950 return 0;
951}
952
953static struct usb_ep_ops s3c_hsudc_ep_ops = {
954 .enable = s3c_hsudc_ep_enable,
955 .disable = s3c_hsudc_ep_disable,
956 .alloc_request = s3c_hsudc_alloc_request,
957 .free_request = s3c_hsudc_free_request,
958 .queue = s3c_hsudc_queue,
959 .dequeue = s3c_hsudc_dequeue,
960 .set_halt = s3c_hsudc_set_halt,
961 .set_wedge = s3c_hsudc_set_wedge,
962};
963
964/**
965 * s3c_hsudc_initep - Initialize a endpoint to default state.
966 * @hsudc - Reference to the device controller.
967 * @hsep - Endpoint to be initialized.
968 * @epnum - Address to be assigned to the endpoint.
969 *
970 * Initialize a endpoint with default configuration.
971 */
972static void s3c_hsudc_initep(struct s3c_hsudc *hsudc,
973 struct s3c_hsudc_ep *hsep, int epnum)
974{
975 char *dir;
976
977 if ((epnum % 2) == 0) {
978 dir = "out";
979 } else {
980 dir = "in";
981 hsep->bEndpointAddress = USB_DIR_IN;
982 }
983
984 hsep->bEndpointAddress |= epnum;
985 if (epnum)
986 snprintf(hsep->name, sizeof(hsep->name), "ep%d%s", epnum, dir);
987 else
988 snprintf(hsep->name, sizeof(hsep->name), "%s", ep0name);
989
990 INIT_LIST_HEAD(&hsep->queue);
991 INIT_LIST_HEAD(&hsep->ep.ep_list);
992 if (epnum)
993 list_add_tail(&hsep->ep.ep_list, &hsudc->gadget.ep_list);
994
995 hsep->dev = hsudc;
996 hsep->ep.name = hsep->name;
997 hsep->ep.maxpacket = epnum ? 512 : 64;
998 hsep->ep.ops = &s3c_hsudc_ep_ops;
999 hsep->fifo = hsudc->regs + S3C_BR(epnum);
1000 hsep->desc = 0;
1001 hsep->stopped = 0;
1002 hsep->wedge = 0;
1003
1004 set_index(hsudc, epnum);
1005 writel(hsep->ep.maxpacket, hsudc->regs + S3C_MPR);
1006}
1007
1008/**
1009 * s3c_hsudc_setup_ep - Configure all endpoints to default state.
1010 * @hsudc: Reference to device controller.
1011 *
1012 * Configures all endpoints to default state.
1013 */
1014static void s3c_hsudc_setup_ep(struct s3c_hsudc *hsudc)
1015{
1016 int epnum;
1017
1018 hsudc->ep0state = WAIT_FOR_SETUP;
1019 INIT_LIST_HEAD(&hsudc->gadget.ep_list);
1020 for (epnum = 0; epnum < hsudc->pd->epnum; epnum++)
1021 s3c_hsudc_initep(hsudc, &hsudc->ep[epnum], epnum);
1022}
1023
1024/**
1025 * s3c_hsudc_reconfig - Reconfigure the device controller to default state.
1026 * @hsudc: Reference to device controller.
1027 *
1028 * Reconfigures the device controller registers to a default state.
1029 */
1030static void s3c_hsudc_reconfig(struct s3c_hsudc *hsudc)
1031{
1032 writel(0xAA, hsudc->regs + S3C_EDR);
1033 writel(1, hsudc->regs + S3C_EIER);
1034 writel(0, hsudc->regs + S3C_TR);
1035 writel(S3C_SCR_DTZIEN_EN | S3C_SCR_RRD_EN | S3C_SCR_SUS_EN |
1036 S3C_SCR_RST_EN, hsudc->regs + S3C_SCR);
1037 writel(0, hsudc->regs + S3C_EP0CR);
1038
1039 s3c_hsudc_setup_ep(hsudc);
1040}
1041
1042/**
1043 * s3c_hsudc_irq - Interrupt handler for device controller.
1044 * @irq: Not used.
1045 * @_dev: Reference to the device controller.
1046 *
1047 * Interrupt handler for the device controller. This handler handles controller
1048 * interrupts and endpoint interrupts.
1049 */
1050static irqreturn_t s3c_hsudc_irq(int irq, void *_dev)
1051{
1052 struct s3c_hsudc *hsudc = _dev;
1053 struct s3c_hsudc_ep *hsep;
1054 u32 ep_intr;
1055 u32 sys_status;
1056 u32 ep_idx;
1057
1058 spin_lock(&hsudc->lock);
1059
1060 sys_status = readl(hsudc->regs + S3C_SSR);
1061 ep_intr = readl(hsudc->regs + S3C_EIR) & 0x3FF;
1062
1063 if (!ep_intr && !(sys_status & S3C_SSR_DTZIEN_EN)) {
1064 spin_unlock(&hsudc->lock);
1065 return IRQ_HANDLED;
1066 }
1067
1068 if (sys_status) {
1069 if (sys_status & S3C_SSR_VBUSON)
1070 writel(S3C_SSR_VBUSON, hsudc->regs + S3C_SSR);
1071
1072 if (sys_status & S3C_SSR_ERR)
1073 writel(S3C_SSR_ERR, hsudc->regs + S3C_SSR);
1074
1075 if (sys_status & S3C_SSR_SDE) {
1076 writel(S3C_SSR_SDE, hsudc->regs + S3C_SSR);
1077 hsudc->gadget.speed = (sys_status & S3C_SSR_HSP) ?
1078 USB_SPEED_HIGH : USB_SPEED_FULL;
1079 }
1080
1081 if (sys_status & S3C_SSR_SUSPEND) {
1082 writel(S3C_SSR_SUSPEND, hsudc->regs + S3C_SSR);
1083 if (hsudc->gadget.speed != USB_SPEED_UNKNOWN
1084 && hsudc->driver && hsudc->driver->suspend)
1085 hsudc->driver->suspend(&hsudc->gadget);
1086 }
1087
1088 if (sys_status & S3C_SSR_RESUME) {
1089 writel(S3C_SSR_RESUME, hsudc->regs + S3C_SSR);
1090 if (hsudc->gadget.speed != USB_SPEED_UNKNOWN
1091 && hsudc->driver && hsudc->driver->resume)
1092 hsudc->driver->resume(&hsudc->gadget);
1093 }
1094
1095 if (sys_status & S3C_SSR_RESET) {
1096 writel(S3C_SSR_RESET, hsudc->regs + S3C_SSR);
1097 for (ep_idx = 0; ep_idx < hsudc->pd->epnum; ep_idx++) {
1098 hsep = &hsudc->ep[ep_idx];
1099 hsep->stopped = 1;
1100 s3c_hsudc_nuke_ep(hsep, -ECONNRESET);
1101 }
1102 s3c_hsudc_reconfig(hsudc);
1103 hsudc->ep0state = WAIT_FOR_SETUP;
1104 }
1105 }
1106
1107 if (ep_intr & S3C_EIR_EP0) {
1108 writel(S3C_EIR_EP0, hsudc->regs + S3C_EIR);
1109 set_index(hsudc, 0);
1110 s3c_hsudc_handle_ep0_intr(hsudc);
1111 }
1112
1113 ep_intr >>= 1;
1114 ep_idx = 1;
1115 while (ep_intr) {
1116 if (ep_intr & 1) {
1117 hsep = &hsudc->ep[ep_idx];
1118 set_index(hsudc, ep_idx);
1119 writel(1 << ep_idx, hsudc->regs + S3C_EIR);
1120 if (ep_is_in(hsep))
1121 s3c_hsudc_epin_intr(hsudc, ep_idx);
1122 else
1123 s3c_hsudc_epout_intr(hsudc, ep_idx);
1124 }
1125 ep_intr >>= 1;
1126 ep_idx++;
1127 }
1128
1129 spin_unlock(&hsudc->lock);
1130 return IRQ_HANDLED;
1131}
1132
Heiko Stübnerd93e2602011-12-19 19:41:45 +01001133static int s3c_hsudc_start(struct usb_gadget *gadget,
1134 struct usb_gadget_driver *driver)
Thomas Abrahama9df3042011-05-07 22:28:04 +02001135{
1136 struct s3c_hsudc *hsudc = the_controller;
1137 int ret;
1138
1139 if (!driver
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01001140 || driver->max_speed < USB_SPEED_FULL
Heiko Stübnerd93e2602011-12-19 19:41:45 +01001141 || !driver->setup)
Thomas Abrahama9df3042011-05-07 22:28:04 +02001142 return -EINVAL;
1143
1144 if (!hsudc)
1145 return -ENODEV;
1146
1147 if (hsudc->driver)
1148 return -EBUSY;
1149
1150 hsudc->driver = driver;
1151 hsudc->gadget.dev.driver = &driver->driver;
Thomas Abrahama9df3042011-05-07 22:28:04 +02001152
Heiko Stübner938fbe542011-08-21 14:32:14 +02001153 /* connect to bus through transceiver */
1154 if (hsudc->transceiver) {
1155 ret = otg_set_peripheral(hsudc->transceiver, &hsudc->gadget);
1156 if (ret) {
1157 dev_err(hsudc->dev, "%s: can't bind to transceiver\n",
1158 hsudc->gadget.name);
Heiko Stübner938fbe542011-08-21 14:32:14 +02001159 hsudc->driver = NULL;
1160 hsudc->gadget.dev.driver = NULL;
1161 return ret;
1162 }
1163 }
1164
Thomas Abrahama9df3042011-05-07 22:28:04 +02001165 enable_irq(hsudc->irq);
1166 dev_info(hsudc->dev, "bound driver %s\n", driver->driver.name);
1167
1168 s3c_hsudc_reconfig(hsudc);
1169 s3c_hsudc_init_phy();
1170 if (hsudc->pd->gpio_init)
1171 hsudc->pd->gpio_init();
1172
1173 return 0;
1174}
Thomas Abrahama9df3042011-05-07 22:28:04 +02001175
Heiko Stübnerd93e2602011-12-19 19:41:45 +01001176static int s3c_hsudc_stop(struct usb_gadget *gadget,
1177 struct usb_gadget_driver *driver)
Thomas Abrahama9df3042011-05-07 22:28:04 +02001178{
1179 struct s3c_hsudc *hsudc = the_controller;
1180 unsigned long flags;
1181
1182 if (!hsudc)
1183 return -ENODEV;
1184
Heiko Stübnerd93e2602011-12-19 19:41:45 +01001185 if (!driver || driver != hsudc->driver)
Thomas Abrahama9df3042011-05-07 22:28:04 +02001186 return -EINVAL;
1187
1188 spin_lock_irqsave(&hsudc->lock, flags);
Heiko Stübnerd93e2602011-12-19 19:41:45 +01001189 hsudc->driver = NULL;
1190 hsudc->gadget.dev.driver = NULL;
1191 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
Thomas Abrahama9df3042011-05-07 22:28:04 +02001192 s3c_hsudc_uninit_phy();
1193 if (hsudc->pd->gpio_uninit)
1194 hsudc->pd->gpio_uninit();
Heiko Stübnerd93e2602011-12-19 19:41:45 +01001195 s3c_hsudc_stop_activity(hsudc);
Thomas Abrahama9df3042011-05-07 22:28:04 +02001196 spin_unlock_irqrestore(&hsudc->lock, flags);
1197
Heiko Stübner938fbe542011-08-21 14:32:14 +02001198 if (hsudc->transceiver)
1199 (void) otg_set_peripheral(hsudc->transceiver, NULL);
1200
Thomas Abrahama9df3042011-05-07 22:28:04 +02001201 disable_irq(hsudc->irq);
1202
1203 dev_info(hsudc->dev, "unregistered gadget driver '%s'\n",
1204 driver->driver.name);
1205 return 0;
1206}
Thomas Abrahama9df3042011-05-07 22:28:04 +02001207
1208static inline u32 s3c_hsudc_read_frameno(struct s3c_hsudc *hsudc)
1209{
1210 return readl(hsudc->regs + S3C_FNR) & 0x3FF;
1211}
1212
1213static int s3c_hsudc_gadget_getframe(struct usb_gadget *gadget)
1214{
1215 return s3c_hsudc_read_frameno(to_hsudc(gadget));
1216}
1217
Heiko Stübnerfba9e542011-09-04 21:56:02 +02001218static int s3c_hsudc_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1219{
1220 struct s3c_hsudc *hsudc = the_controller;
1221
1222 if (!hsudc)
1223 return -ENODEV;
1224
1225 if (hsudc->transceiver)
1226 return otg_set_power(hsudc->transceiver, mA);
1227
1228 return -EOPNOTSUPP;
1229}
1230
Thomas Abrahama9df3042011-05-07 22:28:04 +02001231static struct usb_gadget_ops s3c_hsudc_gadget_ops = {
1232 .get_frame = s3c_hsudc_gadget_getframe,
Heiko Stübnerd93e2602011-12-19 19:41:45 +01001233 .udc_start = s3c_hsudc_start,
1234 .udc_stop = s3c_hsudc_stop,
Heiko Stübnerfba9e542011-09-04 21:56:02 +02001235 .vbus_draw = s3c_hsudc_vbus_draw,
Thomas Abrahama9df3042011-05-07 22:28:04 +02001236};
1237
Heiko Stübnera1977562011-12-19 19:39:52 +01001238static int __devinit s3c_hsudc_probe(struct platform_device *pdev)
Thomas Abrahama9df3042011-05-07 22:28:04 +02001239{
1240 struct device *dev = &pdev->dev;
1241 struct resource *res;
1242 struct s3c_hsudc *hsudc;
1243 struct s3c24xx_hsudc_platdata *pd = pdev->dev.platform_data;
1244 int ret;
1245
1246 hsudc = kzalloc(sizeof(struct s3c_hsudc) +
1247 sizeof(struct s3c_hsudc_ep) * pd->epnum,
1248 GFP_KERNEL);
1249 if (!hsudc) {
1250 dev_err(dev, "cannot allocate memory\n");
1251 return -ENOMEM;
1252 }
1253
1254 the_controller = hsudc;
1255 platform_set_drvdata(pdev, dev);
1256 hsudc->dev = dev;
1257 hsudc->pd = pdev->dev.platform_data;
1258
Heiko Stübner938fbe542011-08-21 14:32:14 +02001259 hsudc->transceiver = otg_get_transceiver();
1260
Thomas Abrahama9df3042011-05-07 22:28:04 +02001261 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1262 if (!res) {
1263 dev_err(dev, "unable to obtain driver resource data\n");
1264 ret = -ENODEV;
1265 goto err_res;
1266 }
1267
1268 hsudc->mem_rsrc = request_mem_region(res->start, resource_size(res),
1269 dev_name(&pdev->dev));
1270 if (!hsudc->mem_rsrc) {
1271 dev_err(dev, "failed to reserve register area\n");
1272 ret = -ENODEV;
1273 goto err_res;
1274 }
1275
1276 hsudc->regs = ioremap(res->start, resource_size(res));
1277 if (!hsudc->regs) {
1278 dev_err(dev, "error mapping device register area\n");
1279 ret = -EBUSY;
1280 goto err_remap;
1281 }
1282
Thomas Abrahama9df3042011-05-07 22:28:04 +02001283 spin_lock_init(&hsudc->lock);
1284
Thomas Abrahama9df3042011-05-07 22:28:04 +02001285 dev_set_name(&hsudc->gadget.dev, "gadget");
1286
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001287 hsudc->gadget.max_speed = USB_SPEED_HIGH;
Thomas Abrahama9df3042011-05-07 22:28:04 +02001288 hsudc->gadget.ops = &s3c_hsudc_gadget_ops;
1289 hsudc->gadget.name = dev_name(dev);
1290 hsudc->gadget.dev.parent = dev;
1291 hsudc->gadget.dev.dma_mask = dev->dma_mask;
1292 hsudc->gadget.ep0 = &hsudc->ep[0].ep;
1293
1294 hsudc->gadget.is_otg = 0;
1295 hsudc->gadget.is_a_peripheral = 0;
Heiko Stübnerd93e2602011-12-19 19:41:45 +01001296 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
Thomas Abrahama9df3042011-05-07 22:28:04 +02001297
1298 s3c_hsudc_setup_ep(hsudc);
1299
Heiko Stübnerda4fc142011-08-21 14:31:17 +02001300 ret = platform_get_irq(pdev, 0);
1301 if (ret < 0) {
1302 dev_err(dev, "unable to obtain IRQ number\n");
1303 goto err_irq;
1304 }
1305 hsudc->irq = ret;
1306
1307 ret = request_irq(hsudc->irq, s3c_hsudc_irq, 0, driver_name, hsudc);
1308 if (ret < 0) {
1309 dev_err(dev, "irq request failed\n");
1310 goto err_irq;
1311 }
1312
Thomas Abrahama9df3042011-05-07 22:28:04 +02001313 hsudc->uclk = clk_get(&pdev->dev, "usb-device");
Jingoo Han004c1272011-05-13 21:27:24 +09001314 if (IS_ERR(hsudc->uclk)) {
Thomas Abrahama9df3042011-05-07 22:28:04 +02001315 dev_err(dev, "failed to find usb-device clock source\n");
Sebastian Andrzej Siewior6bc12952011-06-03 19:50:47 +02001316 ret = PTR_ERR(hsudc->uclk);
1317 goto err_clk;
Thomas Abrahama9df3042011-05-07 22:28:04 +02001318 }
1319 clk_enable(hsudc->uclk);
1320
1321 local_irq_disable();
1322
1323 disable_irq(hsudc->irq);
1324 local_irq_enable();
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001325
Heiko Stübner103495a2011-12-19 19:41:05 +01001326 ret = device_register(&hsudc->gadget.dev);
1327 if (ret) {
1328 put_device(&hsudc->gadget.dev);
1329 goto err_add_device;
1330 }
1331
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001332 ret = usb_add_gadget_udc(&pdev->dev, &hsudc->gadget);
1333 if (ret)
1334 goto err_add_udc;
1335
Thomas Abrahama9df3042011-05-07 22:28:04 +02001336 return 0;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001337err_add_udc:
Heiko Stübner103495a2011-12-19 19:41:05 +01001338 device_unregister(&hsudc->gadget.dev);
1339err_add_device:
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001340 clk_disable(hsudc->uclk);
1341 clk_put(hsudc->uclk);
Sebastian Andrzej Siewior6bc12952011-06-03 19:50:47 +02001342err_clk:
1343 free_irq(hsudc->irq, hsudc);
Thomas Abrahama9df3042011-05-07 22:28:04 +02001344err_irq:
1345 iounmap(hsudc->regs);
1346
1347err_remap:
1348 release_resource(hsudc->mem_rsrc);
1349 kfree(hsudc->mem_rsrc);
1350
1351err_res:
Heiko Stübnere9bcb9e2011-12-19 19:40:28 +01001352 if (hsudc->transceiver)
1353 otg_put_transceiver(hsudc->transceiver);
1354
Thomas Abrahama9df3042011-05-07 22:28:04 +02001355 kfree(hsudc);
1356 return ret;
1357}
1358
1359static struct platform_driver s3c_hsudc_driver = {
1360 .driver = {
1361 .owner = THIS_MODULE,
1362 .name = "s3c-hsudc",
1363 },
1364 .probe = s3c_hsudc_probe,
1365};
Sebastian Andrzej Siewior86081d72011-06-29 16:41:55 +03001366MODULE_ALIAS("platform:s3c-hsudc");
Thomas Abrahama9df3042011-05-07 22:28:04 +02001367
1368static int __init s3c_hsudc_modinit(void)
1369{
1370 return platform_driver_register(&s3c_hsudc_driver);
1371}
1372
1373static void __exit s3c_hsudc_modexit(void)
1374{
1375 platform_driver_unregister(&s3c_hsudc_driver);
1376}
1377
1378module_init(s3c_hsudc_modinit);
1379module_exit(s3c_hsudc_modexit);
1380
1381MODULE_DESCRIPTION("Samsung S3C24XX USB high-speed controller driver");
1382MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1383MODULE_LICENSE("GPL");