Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0 |
b84a8dee YHC |
2 | /* |
3 | * FOTG210 UDC Driver supports Bulk transfer so far | |
4 | * | |
5 | * Copyright (C) 2013 Faraday Technology Corporation | |
6 | * | |
7 | * Author : Yuan-Hsin Chen <yhchen@faraday-tech.com> | |
b84a8dee YHC |
8 | */ |
9 | ||
10 | #include <linux/dma-mapping.h> | |
11 | #include <linux/err.h> | |
12 | #include <linux/interrupt.h> | |
13 | #include <linux/io.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/platform_device.h> | |
16 | #include <linux/usb/ch9.h> | |
17 | #include <linux/usb/gadget.h> | |
718a38d0 | 18 | #include <linux/clk.h> |
5f217ccd LW |
19 | #include <linux/usb/otg.h> |
20 | #include <linux/usb/phy.h> | |
b84a8dee | 21 | |
aeffd2c3 | 22 | #include "fotg210.h" |
1dd33a9f | 23 | #include "fotg210-udc.h" |
b84a8dee YHC |
24 | |
25 | #define DRIVER_DESC "FOTG210 USB Device Controller Driver" | |
26 | #define DRIVER_VERSION "30-April-2013" | |
27 | ||
28 | static const char udc_name[] = "fotg210_udc"; | |
29 | static const char * const fotg210_ep_name[] = { | |
30 | "ep0", "ep1", "ep2", "ep3", "ep4"}; | |
31 | ||
32 | static void fotg210_disable_fifo_int(struct fotg210_ep *ep) | |
33 | { | |
34 | u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1); | |
35 | ||
36 | if (ep->dir_in) | |
37 | value |= DMISGR1_MF_IN_INT(ep->epnum - 1); | |
38 | else | |
39 | value |= DMISGR1_MF_OUTSPK_INT(ep->epnum - 1); | |
40 | iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1); | |
41 | } | |
42 | ||
43 | static void fotg210_enable_fifo_int(struct fotg210_ep *ep) | |
44 | { | |
45 | u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1); | |
46 | ||
47 | if (ep->dir_in) | |
48 | value &= ~DMISGR1_MF_IN_INT(ep->epnum - 1); | |
49 | else | |
50 | value &= ~DMISGR1_MF_OUTSPK_INT(ep->epnum - 1); | |
51 | iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1); | |
52 | } | |
53 | ||
54 | static void fotg210_set_cxdone(struct fotg210_udc *fotg210) | |
55 | { | |
56 | u32 value = ioread32(fotg210->reg + FOTG210_DCFESR); | |
57 | ||
58 | value |= DCFESR_CX_DONE; | |
59 | iowrite32(value, fotg210->reg + FOTG210_DCFESR); | |
60 | } | |
61 | ||
62 | static void fotg210_done(struct fotg210_ep *ep, struct fotg210_request *req, | |
63 | int status) | |
64 | { | |
65 | list_del_init(&req->queue); | |
66 | ||
67 | /* don't modify queue heads during completion callback */ | |
68 | if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN) | |
69 | req->req.status = -ESHUTDOWN; | |
70 | else | |
71 | req->req.status = status; | |
72 | ||
73 | spin_unlock(&ep->fotg210->lock); | |
304f7e5e | 74 | usb_gadget_giveback_request(&ep->ep, &req->req); |
b84a8dee YHC |
75 | spin_lock(&ep->fotg210->lock); |
76 | ||
77 | if (ep->epnum) { | |
78 | if (list_empty(&ep->queue)) | |
79 | fotg210_disable_fifo_int(ep); | |
80 | } else { | |
81 | fotg210_set_cxdone(ep->fotg210); | |
82 | } | |
83 | } | |
84 | ||
85 | static void fotg210_fifo_ep_mapping(struct fotg210_ep *ep, u32 epnum, | |
86 | u32 dir_in) | |
87 | { | |
88 | struct fotg210_udc *fotg210 = ep->fotg210; | |
89 | u32 val; | |
90 | ||
91 | /* Driver should map an ep to a fifo and then map the fifo | |
92 | * to the ep. What a brain-damaged design! | |
93 | */ | |
94 | ||
95 | /* map a fifo to an ep */ | |
96 | val = ioread32(fotg210->reg + FOTG210_EPMAP); | |
97 | val &= ~EPMAP_FIFONOMSK(epnum, dir_in); | |
98 | val |= EPMAP_FIFONO(epnum, dir_in); | |
99 | iowrite32(val, fotg210->reg + FOTG210_EPMAP); | |
100 | ||
101 | /* map the ep to the fifo */ | |
102 | val = ioread32(fotg210->reg + FOTG210_FIFOMAP); | |
103 | val &= ~FIFOMAP_EPNOMSK(epnum); | |
104 | val |= FIFOMAP_EPNO(epnum); | |
105 | iowrite32(val, fotg210->reg + FOTG210_FIFOMAP); | |
106 | ||
107 | /* enable fifo */ | |
108 | val = ioread32(fotg210->reg + FOTG210_FIFOCF); | |
109 | val |= FIFOCF_FIFO_EN(epnum - 1); | |
110 | iowrite32(val, fotg210->reg + FOTG210_FIFOCF); | |
111 | } | |
112 | ||
113 | static void fotg210_set_fifo_dir(struct fotg210_ep *ep, u32 epnum, u32 dir_in) | |
114 | { | |
115 | struct fotg210_udc *fotg210 = ep->fotg210; | |
116 | u32 val; | |
117 | ||
118 | val = ioread32(fotg210->reg + FOTG210_FIFOMAP); | |
119 | val |= (dir_in ? FIFOMAP_DIRIN(epnum - 1) : FIFOMAP_DIROUT(epnum - 1)); | |
120 | iowrite32(val, fotg210->reg + FOTG210_FIFOMAP); | |
121 | } | |
122 | ||
123 | static void fotg210_set_tfrtype(struct fotg210_ep *ep, u32 epnum, u32 type) | |
124 | { | |
125 | struct fotg210_udc *fotg210 = ep->fotg210; | |
126 | u32 val; | |
127 | ||
128 | val = ioread32(fotg210->reg + FOTG210_FIFOCF); | |
129 | val |= FIFOCF_TYPE(type, epnum - 1); | |
130 | iowrite32(val, fotg210->reg + FOTG210_FIFOCF); | |
131 | } | |
132 | ||
133 | static void fotg210_set_mps(struct fotg210_ep *ep, u32 epnum, u32 mps, | |
134 | u32 dir_in) | |
135 | { | |
136 | struct fotg210_udc *fotg210 = ep->fotg210; | |
137 | u32 val; | |
138 | u32 offset = dir_in ? FOTG210_INEPMPSR(epnum) : | |
139 | FOTG210_OUTEPMPSR(epnum); | |
140 | ||
141 | val = ioread32(fotg210->reg + offset); | |
142 | val |= INOUTEPMPSR_MPS(mps); | |
143 | iowrite32(val, fotg210->reg + offset); | |
144 | } | |
145 | ||
146 | static int fotg210_config_ep(struct fotg210_ep *ep, | |
147 | const struct usb_endpoint_descriptor *desc) | |
148 | { | |
149 | struct fotg210_udc *fotg210 = ep->fotg210; | |
150 | ||
151 | fotg210_set_fifo_dir(ep, ep->epnum, ep->dir_in); | |
152 | fotg210_set_tfrtype(ep, ep->epnum, ep->type); | |
153 | fotg210_set_mps(ep, ep->epnum, ep->ep.maxpacket, ep->dir_in); | |
154 | fotg210_fifo_ep_mapping(ep, ep->epnum, ep->dir_in); | |
155 | ||
156 | fotg210->ep[ep->epnum] = ep; | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
161 | static int fotg210_ep_enable(struct usb_ep *_ep, | |
162 | const struct usb_endpoint_descriptor *desc) | |
163 | { | |
164 | struct fotg210_ep *ep; | |
165 | ||
166 | ep = container_of(_ep, struct fotg210_ep, ep); | |
167 | ||
168 | ep->desc = desc; | |
169 | ep->epnum = usb_endpoint_num(desc); | |
170 | ep->type = usb_endpoint_type(desc); | |
171 | ep->dir_in = usb_endpoint_dir_in(desc); | |
172 | ep->ep.maxpacket = usb_endpoint_maxp(desc); | |
173 | ||
174 | return fotg210_config_ep(ep, desc); | |
175 | } | |
176 | ||
177 | static void fotg210_reset_tseq(struct fotg210_udc *fotg210, u8 epnum) | |
178 | { | |
179 | struct fotg210_ep *ep = fotg210->ep[epnum]; | |
180 | u32 value; | |
181 | void __iomem *reg; | |
182 | ||
183 | reg = (ep->dir_in) ? | |
184 | fotg210->reg + FOTG210_INEPMPSR(epnum) : | |
185 | fotg210->reg + FOTG210_OUTEPMPSR(epnum); | |
186 | ||
187 | /* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ | |
188 | * bit. Controller wouldn't clear this bit. WTF!!! | |
189 | */ | |
190 | ||
191 | value = ioread32(reg); | |
192 | value |= INOUTEPMPSR_RESET_TSEQ; | |
193 | iowrite32(value, reg); | |
194 | ||
195 | value = ioread32(reg); | |
196 | value &= ~INOUTEPMPSR_RESET_TSEQ; | |
197 | iowrite32(value, reg); | |
198 | } | |
199 | ||
200 | static int fotg210_ep_release(struct fotg210_ep *ep) | |
201 | { | |
202 | if (!ep->epnum) | |
203 | return 0; | |
204 | ep->epnum = 0; | |
205 | ep->stall = 0; | |
206 | ep->wedged = 0; | |
207 | ||
208 | fotg210_reset_tseq(ep->fotg210, ep->epnum); | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
213 | static int fotg210_ep_disable(struct usb_ep *_ep) | |
214 | { | |
215 | struct fotg210_ep *ep; | |
216 | struct fotg210_request *req; | |
217 | unsigned long flags; | |
218 | ||
219 | BUG_ON(!_ep); | |
220 | ||
221 | ep = container_of(_ep, struct fotg210_ep, ep); | |
222 | ||
223 | while (!list_empty(&ep->queue)) { | |
224 | req = list_entry(ep->queue.next, | |
225 | struct fotg210_request, queue); | |
226 | spin_lock_irqsave(&ep->fotg210->lock, flags); | |
227 | fotg210_done(ep, req, -ECONNRESET); | |
228 | spin_unlock_irqrestore(&ep->fotg210->lock, flags); | |
229 | } | |
230 | ||
231 | return fotg210_ep_release(ep); | |
232 | } | |
233 | ||
234 | static struct usb_request *fotg210_ep_alloc_request(struct usb_ep *_ep, | |
235 | gfp_t gfp_flags) | |
236 | { | |
237 | struct fotg210_request *req; | |
238 | ||
239 | req = kzalloc(sizeof(struct fotg210_request), gfp_flags); | |
240 | if (!req) | |
241 | return NULL; | |
242 | ||
243 | INIT_LIST_HEAD(&req->queue); | |
244 | ||
245 | return &req->req; | |
246 | } | |
247 | ||
248 | static void fotg210_ep_free_request(struct usb_ep *_ep, | |
249 | struct usb_request *_req) | |
250 | { | |
251 | struct fotg210_request *req; | |
252 | ||
253 | req = container_of(_req, struct fotg210_request, req); | |
254 | kfree(req); | |
255 | } | |
256 | ||
257 | static void fotg210_enable_dma(struct fotg210_ep *ep, | |
258 | dma_addr_t d, u32 len) | |
259 | { | |
260 | u32 value; | |
261 | struct fotg210_udc *fotg210 = ep->fotg210; | |
262 | ||
263 | /* set transfer length and direction */ | |
264 | value = ioread32(fotg210->reg + FOTG210_DMACPSR1); | |
265 | value &= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1)); | |
266 | value |= DMACPSR1_DMA_LEN(len) | DMACPSR1_DMA_TYPE(ep->dir_in); | |
267 | iowrite32(value, fotg210->reg + FOTG210_DMACPSR1); | |
268 | ||
269 | /* set device DMA target FIFO number */ | |
270 | value = ioread32(fotg210->reg + FOTG210_DMATFNR); | |
271 | if (ep->epnum) | |
272 | value |= DMATFNR_ACC_FN(ep->epnum - 1); | |
273 | else | |
274 | value |= DMATFNR_ACC_CXF; | |
275 | iowrite32(value, fotg210->reg + FOTG210_DMATFNR); | |
276 | ||
277 | /* set DMA memory address */ | |
278 | iowrite32(d, fotg210->reg + FOTG210_DMACPSR2); | |
279 | ||
280 | /* enable MDMA_EROR and MDMA_CMPLT interrupt */ | |
281 | value = ioread32(fotg210->reg + FOTG210_DMISGR2); | |
282 | value &= ~(DMISGR2_MDMA_CMPLT | DMISGR2_MDMA_ERROR); | |
283 | iowrite32(value, fotg210->reg + FOTG210_DMISGR2); | |
284 | ||
285 | /* start DMA */ | |
286 | value = ioread32(fotg210->reg + FOTG210_DMACPSR1); | |
287 | value |= DMACPSR1_DMA_START; | |
288 | iowrite32(value, fotg210->reg + FOTG210_DMACPSR1); | |
289 | } | |
290 | ||
291 | static void fotg210_disable_dma(struct fotg210_ep *ep) | |
292 | { | |
293 | iowrite32(DMATFNR_DISDMA, ep->fotg210->reg + FOTG210_DMATFNR); | |
294 | } | |
295 | ||
296 | static void fotg210_wait_dma_done(struct fotg210_ep *ep) | |
297 | { | |
298 | u32 value; | |
299 | ||
300 | do { | |
301 | value = ioread32(ep->fotg210->reg + FOTG210_DISGR2); | |
302 | if ((value & DISGR2_USBRST_INT) || | |
303 | (value & DISGR2_DMA_ERROR)) | |
304 | goto dma_reset; | |
305 | } while (!(value & DISGR2_DMA_CMPLT)); | |
306 | ||
307 | value &= ~DISGR2_DMA_CMPLT; | |
308 | iowrite32(value, ep->fotg210->reg + FOTG210_DISGR2); | |
309 | return; | |
310 | ||
311 | dma_reset: | |
312 | value = ioread32(ep->fotg210->reg + FOTG210_DMACPSR1); | |
313 | value |= DMACPSR1_DMA_ABORT; | |
314 | iowrite32(value, ep->fotg210->reg + FOTG210_DMACPSR1); | |
315 | ||
316 | /* reset fifo */ | |
317 | if (ep->epnum) { | |
318 | value = ioread32(ep->fotg210->reg + | |
319 | FOTG210_FIBCR(ep->epnum - 1)); | |
320 | value |= FIBCR_FFRST; | |
321 | iowrite32(value, ep->fotg210->reg + | |
322 | FOTG210_FIBCR(ep->epnum - 1)); | |
323 | } else { | |
324 | value = ioread32(ep->fotg210->reg + FOTG210_DCFESR); | |
325 | value |= DCFESR_CX_CLR; | |
326 | iowrite32(value, ep->fotg210->reg + FOTG210_DCFESR); | |
327 | } | |
328 | } | |
329 | ||
330 | static void fotg210_start_dma(struct fotg210_ep *ep, | |
331 | struct fotg210_request *req) | |
332 | { | |
e26bdb01 | 333 | struct device *dev = &ep->fotg210->gadget.dev; |
b84a8dee YHC |
334 | dma_addr_t d; |
335 | u8 *buffer; | |
336 | u32 length; | |
337 | ||
338 | if (ep->epnum) { | |
339 | if (ep->dir_in) { | |
340 | buffer = req->req.buf; | |
341 | length = req->req.length; | |
342 | } else { | |
343 | buffer = req->req.buf + req->req.actual; | |
344 | length = ioread32(ep->fotg210->reg + | |
3e7c2510 FV |
345 | FOTG210_FIBCR(ep->epnum - 1)) & FIBCR_BCFX; |
346 | if (length > req->req.length - req->req.actual) | |
347 | length = req->req.length - req->req.actual; | |
b84a8dee YHC |
348 | } |
349 | } else { | |
350 | buffer = req->req.buf + req->req.actual; | |
351 | if (req->req.length - req->req.actual > ep->ep.maxpacket) | |
352 | length = ep->ep.maxpacket; | |
353 | else | |
755915fc | 354 | length = req->req.length - req->req.actual; |
b84a8dee YHC |
355 | } |
356 | ||
e26bdb01 | 357 | d = dma_map_single(dev, buffer, length, |
b84a8dee YHC |
358 | ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
359 | ||
e26bdb01 | 360 | if (dma_mapping_error(dev, d)) { |
b84a8dee YHC |
361 | pr_err("dma_mapping_error\n"); |
362 | return; | |
363 | } | |
364 | ||
b84a8dee YHC |
365 | fotg210_enable_dma(ep, d, length); |
366 | ||
367 | /* check if dma is done */ | |
368 | fotg210_wait_dma_done(ep); | |
369 | ||
370 | fotg210_disable_dma(ep); | |
371 | ||
372 | /* update actual transfer length */ | |
373 | req->req.actual += length; | |
374 | ||
e26bdb01 | 375 | dma_unmap_single(dev, d, length, DMA_TO_DEVICE); |
b84a8dee YHC |
376 | } |
377 | ||
378 | static void fotg210_ep0_queue(struct fotg210_ep *ep, | |
379 | struct fotg210_request *req) | |
380 | { | |
381 | if (!req->req.length) { | |
382 | fotg210_done(ep, req, 0); | |
383 | return; | |
384 | } | |
385 | if (ep->dir_in) { /* if IN */ | |
1c99cabf | 386 | fotg210_start_dma(ep, req); |
c7f755b2 | 387 | if (req->req.length == req->req.actual) |
b84a8dee YHC |
388 | fotg210_done(ep, req, 0); |
389 | } else { /* OUT */ | |
1c99cabf | 390 | u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR0); |
b84a8dee | 391 | |
1c99cabf DC |
392 | value &= ~DMISGR0_MCX_OUT_INT; |
393 | iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR0); | |
b84a8dee YHC |
394 | } |
395 | } | |
396 | ||
397 | static int fotg210_ep_queue(struct usb_ep *_ep, struct usb_request *_req, | |
398 | gfp_t gfp_flags) | |
399 | { | |
400 | struct fotg210_ep *ep; | |
401 | struct fotg210_request *req; | |
402 | unsigned long flags; | |
403 | int request = 0; | |
404 | ||
405 | ep = container_of(_ep, struct fotg210_ep, ep); | |
406 | req = container_of(_req, struct fotg210_request, req); | |
407 | ||
408 | if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN) | |
409 | return -ESHUTDOWN; | |
410 | ||
411 | spin_lock_irqsave(&ep->fotg210->lock, flags); | |
412 | ||
413 | if (list_empty(&ep->queue)) | |
414 | request = 1; | |
415 | ||
416 | list_add_tail(&req->queue, &ep->queue); | |
417 | ||
418 | req->req.actual = 0; | |
419 | req->req.status = -EINPROGRESS; | |
420 | ||
421 | if (!ep->epnum) /* ep0 */ | |
422 | fotg210_ep0_queue(ep, req); | |
423 | else if (request && !ep->stall) | |
424 | fotg210_enable_fifo_int(ep); | |
425 | ||
426 | spin_unlock_irqrestore(&ep->fotg210->lock, flags); | |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
431 | static int fotg210_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req) | |
432 | { | |
433 | struct fotg210_ep *ep; | |
434 | struct fotg210_request *req; | |
435 | unsigned long flags; | |
436 | ||
437 | ep = container_of(_ep, struct fotg210_ep, ep); | |
438 | req = container_of(_req, struct fotg210_request, req); | |
439 | ||
440 | spin_lock_irqsave(&ep->fotg210->lock, flags); | |
441 | if (!list_empty(&ep->queue)) | |
442 | fotg210_done(ep, req, -ECONNRESET); | |
443 | spin_unlock_irqrestore(&ep->fotg210->lock, flags); | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
448 | static void fotg210_set_epnstall(struct fotg210_ep *ep) | |
449 | { | |
450 | struct fotg210_udc *fotg210 = ep->fotg210; | |
451 | u32 value; | |
452 | void __iomem *reg; | |
453 | ||
454 | /* check if IN FIFO is empty before stall */ | |
455 | if (ep->dir_in) { | |
456 | do { | |
457 | value = ioread32(fotg210->reg + FOTG210_DCFESR); | |
458 | } while (!(value & DCFESR_FIFO_EMPTY(ep->epnum - 1))); | |
459 | } | |
460 | ||
461 | reg = (ep->dir_in) ? | |
462 | fotg210->reg + FOTG210_INEPMPSR(ep->epnum) : | |
463 | fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum); | |
464 | value = ioread32(reg); | |
465 | value |= INOUTEPMPSR_STL_EP; | |
466 | iowrite32(value, reg); | |
467 | } | |
468 | ||
469 | static void fotg210_clear_epnstall(struct fotg210_ep *ep) | |
470 | { | |
471 | struct fotg210_udc *fotg210 = ep->fotg210; | |
472 | u32 value; | |
473 | void __iomem *reg; | |
474 | ||
475 | reg = (ep->dir_in) ? | |
476 | fotg210->reg + FOTG210_INEPMPSR(ep->epnum) : | |
477 | fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum); | |
478 | value = ioread32(reg); | |
479 | value &= ~INOUTEPMPSR_STL_EP; | |
480 | iowrite32(value, reg); | |
481 | } | |
482 | ||
483 | static int fotg210_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge) | |
484 | { | |
485 | struct fotg210_ep *ep; | |
486 | struct fotg210_udc *fotg210; | |
487 | unsigned long flags; | |
b84a8dee YHC |
488 | |
489 | ep = container_of(_ep, struct fotg210_ep, ep); | |
490 | ||
491 | fotg210 = ep->fotg210; | |
492 | ||
493 | spin_lock_irqsave(&ep->fotg210->lock, flags); | |
494 | ||
495 | if (value) { | |
496 | fotg210_set_epnstall(ep); | |
497 | ep->stall = 1; | |
498 | if (wedge) | |
499 | ep->wedged = 1; | |
500 | } else { | |
501 | fotg210_reset_tseq(fotg210, ep->epnum); | |
502 | fotg210_clear_epnstall(ep); | |
503 | ep->stall = 0; | |
504 | ep->wedged = 0; | |
505 | if (!list_empty(&ep->queue)) | |
506 | fotg210_enable_fifo_int(ep); | |
507 | } | |
508 | ||
509 | spin_unlock_irqrestore(&ep->fotg210->lock, flags); | |
811e5c3d | 510 | return 0; |
b84a8dee YHC |
511 | } |
512 | ||
513 | static int fotg210_ep_set_halt(struct usb_ep *_ep, int value) | |
514 | { | |
515 | return fotg210_set_halt_and_wedge(_ep, value, 0); | |
516 | } | |
517 | ||
518 | static int fotg210_ep_set_wedge(struct usb_ep *_ep) | |
519 | { | |
520 | return fotg210_set_halt_and_wedge(_ep, 1, 1); | |
521 | } | |
522 | ||
523 | static void fotg210_ep_fifo_flush(struct usb_ep *_ep) | |
524 | { | |
525 | } | |
526 | ||
977ac789 | 527 | static const struct usb_ep_ops fotg210_ep_ops = { |
b84a8dee YHC |
528 | .enable = fotg210_ep_enable, |
529 | .disable = fotg210_ep_disable, | |
530 | ||
531 | .alloc_request = fotg210_ep_alloc_request, | |
532 | .free_request = fotg210_ep_free_request, | |
533 | ||
534 | .queue = fotg210_ep_queue, | |
535 | .dequeue = fotg210_ep_dequeue, | |
536 | ||
537 | .set_halt = fotg210_ep_set_halt, | |
538 | .fifo_flush = fotg210_ep_fifo_flush, | |
539 | .set_wedge = fotg210_ep_set_wedge, | |
540 | }; | |
541 | ||
542 | static void fotg210_clear_tx0byte(struct fotg210_udc *fotg210) | |
543 | { | |
544 | u32 value = ioread32(fotg210->reg + FOTG210_TX0BYTE); | |
545 | ||
546 | value &= ~(TX0BYTE_EP1 | TX0BYTE_EP2 | TX0BYTE_EP3 | |
547 | | TX0BYTE_EP4); | |
548 | iowrite32(value, fotg210->reg + FOTG210_TX0BYTE); | |
549 | } | |
550 | ||
551 | static void fotg210_clear_rx0byte(struct fotg210_udc *fotg210) | |
552 | { | |
553 | u32 value = ioread32(fotg210->reg + FOTG210_RX0BYTE); | |
554 | ||
555 | value &= ~(RX0BYTE_EP1 | RX0BYTE_EP2 | RX0BYTE_EP3 | |
556 | | RX0BYTE_EP4); | |
557 | iowrite32(value, fotg210->reg + FOTG210_RX0BYTE); | |
558 | } | |
559 | ||
560 | /* read 8-byte setup packet only */ | |
561 | static void fotg210_rdsetupp(struct fotg210_udc *fotg210, | |
562 | u8 *buffer) | |
563 | { | |
564 | int i = 0; | |
565 | u8 *tmp = buffer; | |
566 | u32 data; | |
567 | u32 length = 8; | |
568 | ||
569 | iowrite32(DMATFNR_ACC_CXF, fotg210->reg + FOTG210_DMATFNR); | |
570 | ||
571 | for (i = (length >> 2); i > 0; i--) { | |
572 | data = ioread32(fotg210->reg + FOTG210_CXPORT); | |
573 | *tmp = data & 0xFF; | |
574 | *(tmp + 1) = (data >> 8) & 0xFF; | |
575 | *(tmp + 2) = (data >> 16) & 0xFF; | |
576 | *(tmp + 3) = (data >> 24) & 0xFF; | |
577 | tmp = tmp + 4; | |
578 | } | |
579 | ||
580 | switch (length % 4) { | |
581 | case 1: | |
582 | data = ioread32(fotg210->reg + FOTG210_CXPORT); | |
583 | *tmp = data & 0xFF; | |
584 | break; | |
585 | case 2: | |
586 | data = ioread32(fotg210->reg + FOTG210_CXPORT); | |
587 | *tmp = data & 0xFF; | |
588 | *(tmp + 1) = (data >> 8) & 0xFF; | |
589 | break; | |
590 | case 3: | |
591 | data = ioread32(fotg210->reg + FOTG210_CXPORT); | |
592 | *tmp = data & 0xFF; | |
593 | *(tmp + 1) = (data >> 8) & 0xFF; | |
594 | *(tmp + 2) = (data >> 16) & 0xFF; | |
595 | break; | |
596 | default: | |
597 | break; | |
598 | } | |
599 | ||
600 | iowrite32(DMATFNR_DISDMA, fotg210->reg + FOTG210_DMATFNR); | |
601 | } | |
602 | ||
603 | static void fotg210_set_configuration(struct fotg210_udc *fotg210) | |
604 | { | |
605 | u32 value = ioread32(fotg210->reg + FOTG210_DAR); | |
606 | ||
607 | value |= DAR_AFT_CONF; | |
608 | iowrite32(value, fotg210->reg + FOTG210_DAR); | |
609 | } | |
610 | ||
611 | static void fotg210_set_dev_addr(struct fotg210_udc *fotg210, u32 addr) | |
612 | { | |
613 | u32 value = ioread32(fotg210->reg + FOTG210_DAR); | |
614 | ||
615 | value |= (addr & 0x7F); | |
616 | iowrite32(value, fotg210->reg + FOTG210_DAR); | |
617 | } | |
618 | ||
619 | static void fotg210_set_cxstall(struct fotg210_udc *fotg210) | |
620 | { | |
621 | u32 value = ioread32(fotg210->reg + FOTG210_DCFESR); | |
622 | ||
623 | value |= DCFESR_CX_STL; | |
624 | iowrite32(value, fotg210->reg + FOTG210_DCFESR); | |
625 | } | |
626 | ||
627 | static void fotg210_request_error(struct fotg210_udc *fotg210) | |
628 | { | |
629 | fotg210_set_cxstall(fotg210); | |
630 | pr_err("request error!!\n"); | |
631 | } | |
632 | ||
633 | static void fotg210_set_address(struct fotg210_udc *fotg210, | |
634 | struct usb_ctrlrequest *ctrl) | |
635 | { | |
46ed6026 | 636 | if (le16_to_cpu(ctrl->wValue) >= 0x0100) { |
b84a8dee YHC |
637 | fotg210_request_error(fotg210); |
638 | } else { | |
46ed6026 | 639 | fotg210_set_dev_addr(fotg210, le16_to_cpu(ctrl->wValue)); |
b84a8dee YHC |
640 | fotg210_set_cxdone(fotg210); |
641 | } | |
642 | } | |
643 | ||
644 | static void fotg210_set_feature(struct fotg210_udc *fotg210, | |
645 | struct usb_ctrlrequest *ctrl) | |
646 | { | |
647 | switch (ctrl->bRequestType & USB_RECIP_MASK) { | |
648 | case USB_RECIP_DEVICE: | |
649 | fotg210_set_cxdone(fotg210); | |
650 | break; | |
651 | case USB_RECIP_INTERFACE: | |
652 | fotg210_set_cxdone(fotg210); | |
653 | break; | |
654 | case USB_RECIP_ENDPOINT: { | |
655 | u8 epnum; | |
656 | epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; | |
657 | if (epnum) | |
658 | fotg210_set_epnstall(fotg210->ep[epnum]); | |
659 | else | |
660 | fotg210_set_cxstall(fotg210); | |
661 | fotg210_set_cxdone(fotg210); | |
662 | } | |
663 | break; | |
664 | default: | |
665 | fotg210_request_error(fotg210); | |
666 | break; | |
667 | } | |
668 | } | |
669 | ||
670 | static void fotg210_clear_feature(struct fotg210_udc *fotg210, | |
671 | struct usb_ctrlrequest *ctrl) | |
672 | { | |
673 | struct fotg210_ep *ep = | |
674 | fotg210->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK]; | |
675 | ||
676 | switch (ctrl->bRequestType & USB_RECIP_MASK) { | |
677 | case USB_RECIP_DEVICE: | |
678 | fotg210_set_cxdone(fotg210); | |
679 | break; | |
680 | case USB_RECIP_INTERFACE: | |
681 | fotg210_set_cxdone(fotg210); | |
682 | break; | |
683 | case USB_RECIP_ENDPOINT: | |
684 | if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) { | |
685 | if (ep->wedged) { | |
686 | fotg210_set_cxdone(fotg210); | |
687 | break; | |
688 | } | |
689 | if (ep->stall) | |
690 | fotg210_set_halt_and_wedge(&ep->ep, 0, 0); | |
691 | } | |
692 | fotg210_set_cxdone(fotg210); | |
693 | break; | |
694 | default: | |
695 | fotg210_request_error(fotg210); | |
696 | break; | |
697 | } | |
698 | } | |
699 | ||
700 | static int fotg210_is_epnstall(struct fotg210_ep *ep) | |
701 | { | |
702 | struct fotg210_udc *fotg210 = ep->fotg210; | |
703 | u32 value; | |
704 | void __iomem *reg; | |
705 | ||
706 | reg = (ep->dir_in) ? | |
707 | fotg210->reg + FOTG210_INEPMPSR(ep->epnum) : | |
708 | fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum); | |
709 | value = ioread32(reg); | |
710 | return value & INOUTEPMPSR_STL_EP ? 1 : 0; | |
711 | } | |
712 | ||
713 | static void fotg210_get_status(struct fotg210_udc *fotg210, | |
714 | struct usb_ctrlrequest *ctrl) | |
715 | { | |
716 | u8 epnum; | |
717 | ||
718 | switch (ctrl->bRequestType & USB_RECIP_MASK) { | |
719 | case USB_RECIP_DEVICE: | |
46ed6026 | 720 | fotg210->ep0_data = cpu_to_le16(1 << USB_DEVICE_SELF_POWERED); |
b84a8dee YHC |
721 | break; |
722 | case USB_RECIP_INTERFACE: | |
46ed6026 | 723 | fotg210->ep0_data = cpu_to_le16(0); |
b84a8dee YHC |
724 | break; |
725 | case USB_RECIP_ENDPOINT: | |
726 | epnum = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK; | |
727 | if (epnum) | |
728 | fotg210->ep0_data = | |
46ed6026 LW |
729 | cpu_to_le16(fotg210_is_epnstall(fotg210->ep[epnum]) |
730 | << USB_ENDPOINT_HALT); | |
b84a8dee YHC |
731 | else |
732 | fotg210_request_error(fotg210); | |
733 | break; | |
734 | ||
735 | default: | |
736 | fotg210_request_error(fotg210); | |
737 | return; /* exit */ | |
738 | } | |
739 | ||
740 | fotg210->ep0_req->buf = &fotg210->ep0_data; | |
741 | fotg210->ep0_req->length = 2; | |
742 | ||
743 | spin_unlock(&fotg210->lock); | |
2337a77c | 744 | fotg210_ep_queue(fotg210->gadget.ep0, fotg210->ep0_req, GFP_ATOMIC); |
b84a8dee YHC |
745 | spin_lock(&fotg210->lock); |
746 | } | |
747 | ||
748 | static int fotg210_setup_packet(struct fotg210_udc *fotg210, | |
749 | struct usb_ctrlrequest *ctrl) | |
750 | { | |
751 | u8 *p = (u8 *)ctrl; | |
752 | u8 ret = 0; | |
753 | ||
754 | fotg210_rdsetupp(fotg210, p); | |
755 | ||
756 | fotg210->ep[0]->dir_in = ctrl->bRequestType & USB_DIR_IN; | |
757 | ||
758 | if (fotg210->gadget.speed == USB_SPEED_UNKNOWN) { | |
759 | u32 value = ioread32(fotg210->reg + FOTG210_DMCR); | |
760 | fotg210->gadget.speed = value & DMCR_HS_EN ? | |
761 | USB_SPEED_HIGH : USB_SPEED_FULL; | |
762 | } | |
763 | ||
764 | /* check request */ | |
765 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { | |
766 | switch (ctrl->bRequest) { | |
767 | case USB_REQ_GET_STATUS: | |
768 | fotg210_get_status(fotg210, ctrl); | |
769 | break; | |
770 | case USB_REQ_CLEAR_FEATURE: | |
771 | fotg210_clear_feature(fotg210, ctrl); | |
772 | break; | |
773 | case USB_REQ_SET_FEATURE: | |
774 | fotg210_set_feature(fotg210, ctrl); | |
775 | break; | |
776 | case USB_REQ_SET_ADDRESS: | |
777 | fotg210_set_address(fotg210, ctrl); | |
778 | break; | |
779 | case USB_REQ_SET_CONFIGURATION: | |
780 | fotg210_set_configuration(fotg210); | |
781 | ret = 1; | |
782 | break; | |
783 | default: | |
784 | ret = 1; | |
785 | break; | |
786 | } | |
787 | } else { | |
788 | ret = 1; | |
789 | } | |
790 | ||
791 | return ret; | |
792 | } | |
793 | ||
794 | static void fotg210_ep0out(struct fotg210_udc *fotg210) | |
795 | { | |
796 | struct fotg210_ep *ep = fotg210->ep[0]; | |
797 | ||
798 | if (!list_empty(&ep->queue) && !ep->dir_in) { | |
799 | struct fotg210_request *req; | |
800 | ||
801 | req = list_first_entry(&ep->queue, | |
802 | struct fotg210_request, queue); | |
803 | ||
804 | if (req->req.length) | |
805 | fotg210_start_dma(ep, req); | |
806 | ||
807 | if ((req->req.length - req->req.actual) < ep->ep.maxpacket) | |
808 | fotg210_done(ep, req, 0); | |
809 | } else { | |
810 | pr_err("%s : empty queue\n", __func__); | |
811 | } | |
812 | } | |
813 | ||
814 | static void fotg210_ep0in(struct fotg210_udc *fotg210) | |
815 | { | |
816 | struct fotg210_ep *ep = fotg210->ep[0]; | |
817 | ||
818 | if ((!list_empty(&ep->queue)) && (ep->dir_in)) { | |
819 | struct fotg210_request *req; | |
820 | ||
821 | req = list_entry(ep->queue.next, | |
822 | struct fotg210_request, queue); | |
823 | ||
824 | if (req->req.length) | |
825 | fotg210_start_dma(ep, req); | |
826 | ||
078ba935 | 827 | if (req->req.actual == req->req.length) |
b84a8dee YHC |
828 | fotg210_done(ep, req, 0); |
829 | } else { | |
830 | fotg210_set_cxdone(fotg210); | |
831 | } | |
832 | } | |
833 | ||
834 | static void fotg210_clear_comabt_int(struct fotg210_udc *fotg210) | |
835 | { | |
836 | u32 value = ioread32(fotg210->reg + FOTG210_DISGR0); | |
837 | ||
838 | value &= ~DISGR0_CX_COMABT_INT; | |
839 | iowrite32(value, fotg210->reg + FOTG210_DISGR0); | |
840 | } | |
841 | ||
842 | static void fotg210_in_fifo_handler(struct fotg210_ep *ep) | |
843 | { | |
844 | struct fotg210_request *req = list_entry(ep->queue.next, | |
845 | struct fotg210_request, queue); | |
846 | ||
847 | if (req->req.length) | |
848 | fotg210_start_dma(ep, req); | |
849 | fotg210_done(ep, req, 0); | |
850 | } | |
851 | ||
852 | static void fotg210_out_fifo_handler(struct fotg210_ep *ep) | |
853 | { | |
854 | struct fotg210_request *req = list_entry(ep->queue.next, | |
855 | struct fotg210_request, queue); | |
75bb93be | 856 | int disgr1 = ioread32(ep->fotg210->reg + FOTG210_DISGR1); |
b84a8dee YHC |
857 | |
858 | fotg210_start_dma(ep, req); | |
859 | ||
75bb93be FV |
860 | /* Complete the request when it's full or a short packet arrived. |
861 | * Like other drivers, short_not_ok isn't handled. | |
862 | */ | |
863 | ||
b84a8dee | 864 | if (req->req.length == req->req.actual || |
75bb93be | 865 | (disgr1 & DISGR1_SPK_INT(ep->epnum - 1))) |
b84a8dee YHC |
866 | fotg210_done(ep, req, 0); |
867 | } | |
868 | ||
869 | static irqreturn_t fotg210_irq(int irq, void *_fotg210) | |
870 | { | |
871 | struct fotg210_udc *fotg210 = _fotg210; | |
872 | u32 int_grp = ioread32(fotg210->reg + FOTG210_DIGR); | |
873 | u32 int_msk = ioread32(fotg210->reg + FOTG210_DMIGR); | |
874 | ||
875 | int_grp &= ~int_msk; | |
876 | ||
877 | spin_lock(&fotg210->lock); | |
878 | ||
879 | if (int_grp & DIGR_INT_G2) { | |
880 | void __iomem *reg = fotg210->reg + FOTG210_DISGR2; | |
881 | u32 int_grp2 = ioread32(reg); | |
882 | u32 int_msk2 = ioread32(fotg210->reg + FOTG210_DMISGR2); | |
883 | u32 value; | |
884 | ||
885 | int_grp2 &= ~int_msk2; | |
886 | ||
887 | if (int_grp2 & DISGR2_USBRST_INT) { | |
fe8f103a FV |
888 | usb_gadget_udc_reset(&fotg210->gadget, |
889 | fotg210->driver); | |
b84a8dee YHC |
890 | value = ioread32(reg); |
891 | value &= ~DISGR2_USBRST_INT; | |
892 | iowrite32(value, reg); | |
893 | pr_info("fotg210 udc reset\n"); | |
894 | } | |
895 | if (int_grp2 & DISGR2_SUSP_INT) { | |
896 | value = ioread32(reg); | |
897 | value &= ~DISGR2_SUSP_INT; | |
898 | iowrite32(value, reg); | |
899 | pr_info("fotg210 udc suspend\n"); | |
900 | } | |
901 | if (int_grp2 & DISGR2_RESM_INT) { | |
902 | value = ioread32(reg); | |
903 | value &= ~DISGR2_RESM_INT; | |
904 | iowrite32(value, reg); | |
905 | pr_info("fotg210 udc resume\n"); | |
906 | } | |
907 | if (int_grp2 & DISGR2_ISO_SEQ_ERR_INT) { | |
908 | value = ioread32(reg); | |
909 | value &= ~DISGR2_ISO_SEQ_ERR_INT; | |
910 | iowrite32(value, reg); | |
911 | pr_info("fotg210 iso sequence error\n"); | |
912 | } | |
913 | if (int_grp2 & DISGR2_ISO_SEQ_ABORT_INT) { | |
914 | value = ioread32(reg); | |
915 | value &= ~DISGR2_ISO_SEQ_ABORT_INT; | |
916 | iowrite32(value, reg); | |
917 | pr_info("fotg210 iso sequence abort\n"); | |
918 | } | |
919 | if (int_grp2 & DISGR2_TX0BYTE_INT) { | |
920 | fotg210_clear_tx0byte(fotg210); | |
921 | value = ioread32(reg); | |
922 | value &= ~DISGR2_TX0BYTE_INT; | |
923 | iowrite32(value, reg); | |
924 | pr_info("fotg210 transferred 0 byte\n"); | |
925 | } | |
926 | if (int_grp2 & DISGR2_RX0BYTE_INT) { | |
927 | fotg210_clear_rx0byte(fotg210); | |
928 | value = ioread32(reg); | |
929 | value &= ~DISGR2_RX0BYTE_INT; | |
930 | iowrite32(value, reg); | |
931 | pr_info("fotg210 received 0 byte\n"); | |
932 | } | |
933 | if (int_grp2 & DISGR2_DMA_ERROR) { | |
934 | value = ioread32(reg); | |
935 | value &= ~DISGR2_DMA_ERROR; | |
936 | iowrite32(value, reg); | |
937 | } | |
938 | } | |
939 | ||
940 | if (int_grp & DIGR_INT_G0) { | |
941 | void __iomem *reg = fotg210->reg + FOTG210_DISGR0; | |
942 | u32 int_grp0 = ioread32(reg); | |
943 | u32 int_msk0 = ioread32(fotg210->reg + FOTG210_DMISGR0); | |
944 | struct usb_ctrlrequest ctrl; | |
945 | ||
946 | int_grp0 &= ~int_msk0; | |
947 | ||
948 | /* the highest priority in this source register */ | |
949 | if (int_grp0 & DISGR0_CX_COMABT_INT) { | |
950 | fotg210_clear_comabt_int(fotg210); | |
951 | pr_info("fotg210 CX command abort\n"); | |
952 | } | |
953 | ||
954 | if (int_grp0 & DISGR0_CX_SETUP_INT) { | |
955 | if (fotg210_setup_packet(fotg210, &ctrl)) { | |
956 | spin_unlock(&fotg210->lock); | |
957 | if (fotg210->driver->setup(&fotg210->gadget, | |
958 | &ctrl) < 0) | |
959 | fotg210_set_cxstall(fotg210); | |
960 | spin_lock(&fotg210->lock); | |
961 | } | |
962 | } | |
963 | if (int_grp0 & DISGR0_CX_COMEND_INT) | |
964 | pr_info("fotg210 cmd end\n"); | |
965 | ||
966 | if (int_grp0 & DISGR0_CX_IN_INT) | |
967 | fotg210_ep0in(fotg210); | |
968 | ||
969 | if (int_grp0 & DISGR0_CX_OUT_INT) | |
970 | fotg210_ep0out(fotg210); | |
971 | ||
972 | if (int_grp0 & DISGR0_CX_COMFAIL_INT) { | |
973 | fotg210_set_cxstall(fotg210); | |
974 | pr_info("fotg210 ep0 fail\n"); | |
975 | } | |
976 | } | |
977 | ||
978 | if (int_grp & DIGR_INT_G1) { | |
979 | void __iomem *reg = fotg210->reg + FOTG210_DISGR1; | |
980 | u32 int_grp1 = ioread32(reg); | |
981 | u32 int_msk1 = ioread32(fotg210->reg + FOTG210_DMISGR1); | |
982 | int fifo; | |
983 | ||
984 | int_grp1 &= ~int_msk1; | |
985 | ||
986 | for (fifo = 0; fifo < FOTG210_MAX_FIFO_NUM; fifo++) { | |
987 | if (int_grp1 & DISGR1_IN_INT(fifo)) | |
988 | fotg210_in_fifo_handler(fotg210->ep[fifo + 1]); | |
989 | ||
990 | if ((int_grp1 & DISGR1_OUT_INT(fifo)) || | |
991 | (int_grp1 & DISGR1_SPK_INT(fifo))) | |
992 | fotg210_out_fifo_handler(fotg210->ep[fifo + 1]); | |
993 | } | |
994 | } | |
995 | ||
996 | spin_unlock(&fotg210->lock); | |
997 | ||
998 | return IRQ_HANDLED; | |
999 | } | |
1000 | ||
1001 | static void fotg210_disable_unplug(struct fotg210_udc *fotg210) | |
1002 | { | |
1003 | u32 reg = ioread32(fotg210->reg + FOTG210_PHYTMSR); | |
1004 | ||
1005 | reg &= ~PHYTMSR_UNPLUG; | |
1006 | iowrite32(reg, fotg210->reg + FOTG210_PHYTMSR); | |
1007 | } | |
1008 | ||
1009 | static int fotg210_udc_start(struct usb_gadget *g, | |
1010 | struct usb_gadget_driver *driver) | |
1011 | { | |
1012 | struct fotg210_udc *fotg210 = gadget_to_fotg210(g); | |
1013 | u32 value; | |
5f217ccd | 1014 | int ret; |
b84a8dee YHC |
1015 | |
1016 | /* hook up the driver */ | |
b84a8dee YHC |
1017 | fotg210->driver = driver; |
1018 | ||
5f217ccd LW |
1019 | if (!IS_ERR_OR_NULL(fotg210->phy)) { |
1020 | ret = otg_set_peripheral(fotg210->phy->otg, | |
1021 | &fotg210->gadget); | |
1022 | if (ret) | |
1023 | dev_err(fotg210->dev, "can't bind to phy\n"); | |
1024 | } | |
1025 | ||
b84a8dee YHC |
1026 | /* enable device global interrupt */ |
1027 | value = ioread32(fotg210->reg + FOTG210_DMCR); | |
1028 | value |= DMCR_GLINT_EN; | |
1029 | iowrite32(value, fotg210->reg + FOTG210_DMCR); | |
1030 | ||
1031 | return 0; | |
1032 | } | |
1033 | ||
1034 | static void fotg210_init(struct fotg210_udc *fotg210) | |
1035 | { | |
1036 | u32 value; | |
1037 | ||
1038 | /* disable global interrupt and set int polarity to active high */ | |
1039 | iowrite32(GMIR_MHC_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY, | |
1040 | fotg210->reg + FOTG210_GMIR); | |
1041 | ||
1042 | /* disable device global interrupt */ | |
1043 | value = ioread32(fotg210->reg + FOTG210_DMCR); | |
1044 | value &= ~DMCR_GLINT_EN; | |
1045 | iowrite32(value, fotg210->reg + FOTG210_DMCR); | |
1046 | ||
9aee3a23 FV |
1047 | /* enable only grp2 irqs we handle */ |
1048 | iowrite32(~(DISGR2_DMA_ERROR | DISGR2_RX0BYTE_INT | DISGR2_TX0BYTE_INT | |
1049 | | DISGR2_ISO_SEQ_ABORT_INT | DISGR2_ISO_SEQ_ERR_INT | |
1050 | | DISGR2_RESM_INT | DISGR2_SUSP_INT | DISGR2_USBRST_INT), | |
1051 | fotg210->reg + FOTG210_DMISGR2); | |
1052 | ||
b84a8dee YHC |
1053 | /* disable all fifo interrupt */ |
1054 | iowrite32(~(u32)0, fotg210->reg + FOTG210_DMISGR1); | |
1055 | ||
1056 | /* disable cmd end */ | |
1057 | value = ioread32(fotg210->reg + FOTG210_DMISGR0); | |
1058 | value |= DMISGR0_MCX_COMEND; | |
1059 | iowrite32(value, fotg210->reg + FOTG210_DMISGR0); | |
1060 | } | |
1061 | ||
22835b80 | 1062 | static int fotg210_udc_stop(struct usb_gadget *g) |
b84a8dee YHC |
1063 | { |
1064 | struct fotg210_udc *fotg210 = gadget_to_fotg210(g); | |
1065 | unsigned long flags; | |
1066 | ||
5f217ccd LW |
1067 | if (!IS_ERR_OR_NULL(fotg210->phy)) |
1068 | return otg_set_peripheral(fotg210->phy->otg, NULL); | |
1069 | ||
b84a8dee YHC |
1070 | spin_lock_irqsave(&fotg210->lock, flags); |
1071 | ||
1072 | fotg210_init(fotg210); | |
1073 | fotg210->driver = NULL; | |
1074 | ||
1075 | spin_unlock_irqrestore(&fotg210->lock, flags); | |
1076 | ||
1077 | return 0; | |
1078 | } | |
1079 | ||
bee56235 | 1080 | static const struct usb_gadget_ops fotg210_gadget_ops = { |
b84a8dee YHC |
1081 | .udc_start = fotg210_udc_start, |
1082 | .udc_stop = fotg210_udc_stop, | |
1083 | }; | |
1084 | ||
5f217ccd LW |
1085 | /** |
1086 | * fotg210_phy_event - Called by phy upon VBus event | |
1087 | * @nb: notifier block | |
1088 | * @action: phy action, is vbus connect or disconnect | |
1089 | * @data: the usb_gadget structure in fotg210 | |
1090 | * | |
1091 | * Called by the USB Phy when a cable connect or disconnect is sensed. | |
1092 | * | |
1093 | * Returns NOTIFY_OK or NOTIFY_DONE | |
1094 | */ | |
1095 | static int fotg210_phy_event(struct notifier_block *nb, unsigned long action, | |
1096 | void *data) | |
1097 | { | |
1098 | struct usb_gadget *gadget = data; | |
1099 | ||
1100 | if (!gadget) | |
1101 | return NOTIFY_DONE; | |
1102 | ||
1103 | switch (action) { | |
1104 | case USB_EVENT_VBUS: | |
1105 | usb_gadget_vbus_connect(gadget); | |
1106 | return NOTIFY_OK; | |
1107 | case USB_EVENT_NONE: | |
1108 | usb_gadget_vbus_disconnect(gadget); | |
1109 | return NOTIFY_OK; | |
1110 | default: | |
1111 | return NOTIFY_DONE; | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | static struct notifier_block fotg210_phy_notifier = { | |
1116 | .notifier_call = fotg210_phy_event, | |
1117 | }; | |
1118 | ||
aeffd2c3 | 1119 | int fotg210_udc_remove(struct platform_device *pdev) |
b84a8dee | 1120 | { |
3cb40a59 | 1121 | struct fotg210_udc *fotg210 = platform_get_drvdata(pdev); |
c37bd528 | 1122 | int i; |
b84a8dee YHC |
1123 | |
1124 | usb_del_gadget_udc(&fotg210->gadget); | |
5f217ccd LW |
1125 | if (!IS_ERR_OR_NULL(fotg210->phy)) { |
1126 | usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier); | |
1127 | usb_put_phy(fotg210->phy); | |
1128 | } | |
b84a8dee YHC |
1129 | iounmap(fotg210->reg); |
1130 | free_irq(platform_get_irq(pdev, 0), fotg210); | |
1131 | ||
1132 | fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req); | |
c37bd528 AV |
1133 | for (i = 0; i < FOTG210_MAX_NUM_EP; i++) |
1134 | kfree(fotg210->ep[i]); | |
718a38d0 LW |
1135 | |
1136 | if (!IS_ERR(fotg210->pclk)) | |
1137 | clk_disable_unprepare(fotg210->pclk); | |
1138 | ||
b84a8dee YHC |
1139 | kfree(fotg210); |
1140 | ||
1141 | return 0; | |
1142 | } | |
1143 | ||
aeffd2c3 | 1144 | int fotg210_udc_probe(struct platform_device *pdev) |
b84a8dee | 1145 | { |
f8b729ce | 1146 | struct resource *res; |
b84a8dee | 1147 | struct fotg210_udc *fotg210 = NULL; |
d40eaada | 1148 | struct device *dev = &pdev->dev; |
f8b729ce | 1149 | int irq; |
b84a8dee YHC |
1150 | int ret = 0; |
1151 | int i; | |
1152 | ||
1153 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1154 | if (!res) { | |
1155 | pr_err("platform_get_resource error.\n"); | |
1156 | return -ENODEV; | |
1157 | } | |
1158 | ||
f8b729ce LW |
1159 | irq = platform_get_irq(pdev, 0); |
1160 | if (irq < 0) { | |
1161 | pr_err("could not get irq\n"); | |
b84a8dee YHC |
1162 | return -ENODEV; |
1163 | } | |
1164 | ||
b84a8dee YHC |
1165 | /* initialize udc */ |
1166 | fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL); | |
0351c329 | 1167 | if (fotg210 == NULL) |
0cd142b4 | 1168 | return -ENOMEM; |
b84a8dee | 1169 | |
5f217ccd LW |
1170 | fotg210->dev = dev; |
1171 | ||
718a38d0 LW |
1172 | /* It's OK not to supply this clock */ |
1173 | fotg210->pclk = devm_clk_get(dev, "PCLK"); | |
1174 | if (!IS_ERR(fotg210->pclk)) { | |
1175 | ret = clk_prepare_enable(fotg210->pclk); | |
1176 | if (ret) { | |
1177 | dev_err(dev, "failed to enable PCLK\n"); | |
0cd142b4 | 1178 | goto err; |
718a38d0 LW |
1179 | } |
1180 | } else if (PTR_ERR(fotg210->pclk) == -EPROBE_DEFER) { | |
1181 | /* | |
1182 | * Percolate deferrals, for anything else, | |
1183 | * just live without the clocking. | |
1184 | */ | |
1185 | ret = -EPROBE_DEFER; | |
1186 | goto err; | |
1187 | } | |
1188 | ||
f8b729ce | 1189 | fotg210->phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0); |
5f217ccd LW |
1190 | if (IS_ERR(fotg210->phy)) { |
1191 | ret = PTR_ERR(fotg210->phy); | |
1192 | if (ret == -EPROBE_DEFER) | |
718a38d0 | 1193 | goto err_pclk; |
5f217ccd LW |
1194 | dev_info(dev, "no PHY found\n"); |
1195 | fotg210->phy = NULL; | |
1196 | } else { | |
1197 | ret = usb_phy_init(fotg210->phy); | |
1198 | if (ret) | |
718a38d0 | 1199 | goto err_pclk; |
5f217ccd LW |
1200 | dev_info(dev, "found and initialized PHY\n"); |
1201 | } | |
1202 | ||
1a5a23b9 YY |
1203 | ret = -ENOMEM; |
1204 | ||
b84a8dee | 1205 | for (i = 0; i < FOTG210_MAX_NUM_EP; i++) { |
202f785b CJ |
1206 | fotg210->ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL); |
1207 | if (!fotg210->ep[i]) | |
b84a8dee | 1208 | goto err_alloc; |
b84a8dee YHC |
1209 | } |
1210 | ||
1211 | fotg210->reg = ioremap(res->start, resource_size(res)); | |
1212 | if (fotg210->reg == NULL) { | |
d40eaada | 1213 | dev_err(dev, "ioremap error\n"); |
c37bd528 | 1214 | goto err_alloc; |
b84a8dee YHC |
1215 | } |
1216 | ||
1217 | spin_lock_init(&fotg210->lock); | |
1218 | ||
3cb40a59 | 1219 | platform_set_drvdata(pdev, fotg210); |
b84a8dee YHC |
1220 | |
1221 | fotg210->gadget.ops = &fotg210_gadget_ops; | |
1222 | ||
1223 | fotg210->gadget.max_speed = USB_SPEED_HIGH; | |
d40eaada LW |
1224 | fotg210->gadget.dev.parent = dev; |
1225 | fotg210->gadget.dev.dma_mask = dev->dma_mask; | |
b84a8dee YHC |
1226 | fotg210->gadget.name = udc_name; |
1227 | ||
1228 | INIT_LIST_HEAD(&fotg210->gadget.ep_list); | |
1229 | ||
1230 | for (i = 0; i < FOTG210_MAX_NUM_EP; i++) { | |
1231 | struct fotg210_ep *ep = fotg210->ep[i]; | |
1232 | ||
1233 | if (i) { | |
1234 | INIT_LIST_HEAD(&fotg210->ep[i]->ep.ep_list); | |
1235 | list_add_tail(&fotg210->ep[i]->ep.ep_list, | |
1236 | &fotg210->gadget.ep_list); | |
1237 | } | |
1238 | ep->fotg210 = fotg210; | |
1239 | INIT_LIST_HEAD(&ep->queue); | |
1240 | ep->ep.name = fotg210_ep_name[i]; | |
1241 | ep->ep.ops = &fotg210_ep_ops; | |
e117e742 | 1242 | usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0); |
8d29237a RB |
1243 | |
1244 | if (i == 0) { | |
1245 | ep->ep.caps.type_control = true; | |
1246 | } else { | |
1247 | ep->ep.caps.type_iso = true; | |
1248 | ep->ep.caps.type_bulk = true; | |
1249 | ep->ep.caps.type_int = true; | |
1250 | } | |
1251 | ||
1252 | ep->ep.caps.dir_in = true; | |
1253 | ep->ep.caps.dir_out = true; | |
b84a8dee | 1254 | } |
e117e742 | 1255 | usb_ep_set_maxpacket_limit(&fotg210->ep[0]->ep, 0x40); |
b84a8dee YHC |
1256 | fotg210->gadget.ep0 = &fotg210->ep[0]->ep; |
1257 | INIT_LIST_HEAD(&fotg210->gadget.ep0->ep_list); | |
1258 | ||
1259 | fotg210->ep0_req = fotg210_ep_alloc_request(&fotg210->ep[0]->ep, | |
1260 | GFP_KERNEL); | |
1261 | if (fotg210->ep0_req == NULL) | |
c37bd528 | 1262 | goto err_map; |
b84a8dee YHC |
1263 | |
1264 | fotg210_init(fotg210); | |
1265 | ||
1266 | fotg210_disable_unplug(fotg210); | |
1267 | ||
f8b729ce | 1268 | ret = request_irq(irq, fotg210_irq, IRQF_SHARED, |
b84a8dee YHC |
1269 | udc_name, fotg210); |
1270 | if (ret < 0) { | |
d40eaada | 1271 | dev_err(dev, "request_irq error (%d)\n", ret); |
2184fe63 | 1272 | goto err_req; |
b84a8dee YHC |
1273 | } |
1274 | ||
5f217ccd LW |
1275 | if (!IS_ERR_OR_NULL(fotg210->phy)) |
1276 | usb_register_notifier(fotg210->phy, &fotg210_phy_notifier); | |
1277 | ||
d40eaada | 1278 | ret = usb_add_gadget_udc(dev, &fotg210->gadget); |
b84a8dee YHC |
1279 | if (ret) |
1280 | goto err_add_udc; | |
1281 | ||
d40eaada | 1282 | dev_info(dev, "version %s\n", DRIVER_VERSION); |
b84a8dee YHC |
1283 | |
1284 | return 0; | |
1285 | ||
1286 | err_add_udc: | |
5f217ccd LW |
1287 | if (!IS_ERR_OR_NULL(fotg210->phy)) |
1288 | usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier); | |
f8b729ce | 1289 | free_irq(irq, fotg210); |
b84a8dee YHC |
1290 | |
1291 | err_req: | |
1292 | fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req); | |
1293 | ||
1294 | err_map: | |
c37bd528 | 1295 | iounmap(fotg210->reg); |
b84a8dee YHC |
1296 | |
1297 | err_alloc: | |
c37bd528 AV |
1298 | for (i = 0; i < FOTG210_MAX_NUM_EP; i++) |
1299 | kfree(fotg210->ep[i]); | |
718a38d0 LW |
1300 | err_pclk: |
1301 | if (!IS_ERR(fotg210->pclk)) | |
1302 | clk_disable_unprepare(fotg210->pclk); | |
1303 | ||
c37bd528 | 1304 | err: |
0cd142b4 | 1305 | kfree(fotg210); |
b84a8dee YHC |
1306 | return ret; |
1307 | } |