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