blob: 1af19059dd02ed29b624d77ac854093be3add68c [file] [log] [blame]
Kuninori Morimotof1407d52011-04-04 13:44:59 +09001/*
2 * Renesas USB driver
3 *
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 */
17#include <linux/delay.h>
Kuninori Morimotof1407d52011-04-04 13:44:59 +090018#include <linux/slab.h>
19#include "./common.h"
20#include "./pipe.h"
21
22/*
23 * macros
24 */
Kuninori Morimotof1407d52011-04-04 13:44:59 +090025#define usbhsp_addr_offset(p) ((usbhs_pipe_number(p) - 1) * 2)
26
Kuninori Morimotof1407d52011-04-04 13:44:59 +090027#define usbhsp_flags_set(p, f) ((p)->flags |= USBHS_PIPE_FLAGS_##f)
28#define usbhsp_flags_clr(p, f) ((p)->flags &= ~USBHS_PIPE_FLAGS_##f)
29#define usbhsp_flags_has(p, f) ((p)->flags & USBHS_PIPE_FLAGS_##f)
30#define usbhsp_flags_init(p) do {(p)->flags = 0; } while (0)
31
32#define usbhsp_type(p) ((p)->pipe_type)
33#define usbhsp_type_is(p, t) ((p)->pipe_type == t)
34
35/*
36 * for debug
37 */
38static char *usbhsp_pipe_name[] = {
39 [USB_ENDPOINT_XFER_CONTROL] = "DCP",
40 [USB_ENDPOINT_XFER_BULK] = "BULK",
41 [USB_ENDPOINT_XFER_INT] = "INT",
42 [USB_ENDPOINT_XFER_ISOC] = "ISO",
43};
44
45/*
46 * usb request functions
47 */
48void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
49{
50 u16 val;
51
52 val = usbhs_read(priv, USBREQ);
53 req->bRequest = (val >> 8) & 0xFF;
54 req->bRequestType = (val >> 0) & 0xFF;
55
56 req->wValue = usbhs_read(priv, USBVAL);
57 req->wIndex = usbhs_read(priv, USBINDX);
58 req->wLength = usbhs_read(priv, USBLENG);
59}
60
61void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
62{
63 usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
64 usbhs_write(priv, USBVAL, req->wValue);
65 usbhs_write(priv, USBINDX, req->wIndex);
66 usbhs_write(priv, USBLENG, req->wLength);
67}
68
69/*
70 * DCPCTR/PIPEnCTR functions
71 */
72static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
73{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090074 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +090075 int offset = usbhsp_addr_offset(pipe);
76
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090077 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +090078 usbhs_bset(priv, DCPCTR, mask, val);
79 else
80 usbhs_bset(priv, PIPEnCTR + offset, mask, val);
81}
82
83static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe)
84{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090085 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +090086 int offset = usbhsp_addr_offset(pipe);
87
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090088 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +090089 return usbhs_read(priv, DCPCTR);
90 else
91 return usbhs_read(priv, PIPEnCTR + offset);
92}
93
94/*
95 * DCP/PIPE functions
96 */
97static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
98 u16 dcp_reg, u16 pipe_reg,
99 u16 mask, u16 val)
100{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900101 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900102
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900103 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900104 usbhs_bset(priv, dcp_reg, mask, val);
105 else
106 usbhs_bset(priv, pipe_reg, mask, val);
107}
108
109static u16 __usbhsp_pipe_xxx_get(struct usbhs_pipe *pipe,
110 u16 dcp_reg, u16 pipe_reg)
111{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900112 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900113
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900114 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900115 return usbhs_read(priv, dcp_reg);
116 else
117 return usbhs_read(priv, pipe_reg);
118}
119
120/*
121 * DCPCFG/PIPECFG functions
122 */
123static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
124{
125 __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
126}
127
128/*
129 * PIPEBUF
130 */
131static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
132{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900133 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900134 return;
135
136 __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val);
137}
138
139/*
140 * DCPMAXP/PIPEMAXP
141 */
142static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
143{
144 __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
145}
146
147static u16 usbhsp_pipe_maxp_get(struct usbhs_pipe *pipe)
148{
149 return __usbhsp_pipe_xxx_get(pipe, DCPMAXP, PIPEMAXP);
150}
151
152/*
153 * pipe control functions
154 */
155static void usbhsp_pipe_select(struct usbhs_pipe *pipe)
156{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900157 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900158
159 /*
160 * On pipe, this is necessary before
161 * accesses to below registers.
162 *
163 * PIPESEL : usbhsp_pipe_select
164 * PIPECFG : usbhsp_pipe_cfg_xxx
165 * PIPEBUF : usbhsp_pipe_buf_xxx
166 * PIPEMAXP : usbhsp_pipe_maxp_xxx
167 * PIPEPERI
168 */
169
170 /*
171 * if pipe is dcp, no pipe is selected.
172 * it is no problem, because dcp have its register
173 */
174 usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe));
175}
176
177static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe)
178{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900179 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900180 int timeout = 1024;
181 u16 val;
182
183 /*
184 * make sure....
185 *
186 * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
187 * specified by the CURPIPE bits.
188 * When changing the setting of this bit after changing
189 * the PID bits for the selected pipe from BUF to NAK,
190 * check that CSSTS = 0 and PBUSY = 0.
191 */
192
193 /*
194 * CURPIPE bit = 0
195 *
196 * see also
197 * "Operation"
198 * - "Pipe Control"
199 * - "Pipe Control Registers Switching Procedure"
200 */
201 usbhs_write(priv, CFIFOSEL, 0);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900202 usbhs_pipe_disable(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900203
204 do {
205 val = usbhsp_pipectrl_get(pipe);
206 val &= CSSTS | PID_MASK;
207 if (!val)
208 return 0;
209
210 udelay(10);
211
212 } while (timeout--);
213
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900214 return -EBUSY;
215}
216
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900217int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900218{
219 u16 val;
220
221 val = usbhsp_pipectrl_get(pipe);
222 if (val & BSTS)
223 return 0;
224
225 return -EBUSY;
226}
227
228/*
229 * PID ctrl
230 */
231static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
232{
233 u16 pid = usbhsp_pipectrl_get(pipe);
234
235 pid &= PID_MASK;
236
237 /*
238 * see
239 * "Pipe n Control Register" - "PID"
240 */
241 switch (pid) {
242 case PID_STALL11:
243 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
244 /* fall-through */
245 case PID_STALL10:
246 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
247 }
248}
249
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900250void usbhs_pipe_disable(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900251{
Kuninori Morimotoc786e092011-05-11 16:00:09 +0900252 int timeout = 1024;
253 u16 val;
254
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900255 /* see "Pipe n Control Register" - "PID" */
256 __usbhsp_pid_try_nak_if_stall(pipe);
257
258 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
Kuninori Morimotoc786e092011-05-11 16:00:09 +0900259
260 do {
261 val = usbhsp_pipectrl_get(pipe);
262 val &= PBUSY;
263 if (!val)
264 break;
265
266 udelay(10);
267 } while (timeout--);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900268}
269
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900270void usbhs_pipe_enable(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900271{
272 /* see "Pipe n Control Register" - "PID" */
273 __usbhsp_pid_try_nak_if_stall(pipe);
274
275 usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
276}
277
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900278void usbhs_pipe_stall(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900279{
280 u16 pid = usbhsp_pipectrl_get(pipe);
281
282 pid &= PID_MASK;
283
284 /*
285 * see
286 * "Pipe n Control Register" - "PID"
287 */
288 switch (pid) {
289 case PID_NAK:
290 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
291 break;
292 case PID_BUF:
293 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
294 break;
295 }
296}
297
298/*
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900299 * pipe setup
300 */
301static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
302{
303 /*
304 * only ISO / BULK pipe can use double buffer
305 */
306 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
307 usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
308 return 1;
309
310 return 0;
311}
312
313static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700314 int is_host,
315 int dir_in)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900316{
317 u16 type = 0;
318 u16 bfre = 0;
319 u16 dblb = 0;
320 u16 cntmd = 0;
321 u16 dir = 0;
322 u16 epnum = 0;
323 u16 shtnak = 0;
324 u16 type_array[] = {
325 [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
326 [USB_ENDPOINT_XFER_INT] = TYPE_INT,
327 [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
328 };
329 int is_double = usbhsp_possible_double_buffer(pipe);
330
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900331 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900332 return -EINVAL;
333
334 /*
335 * PIPECFG
336 *
337 * see
338 * - "Register Descriptions" - "PIPECFG" register
339 * - "Features" - "Pipe configuration"
340 * - "Operation" - "Pipe Control"
341 */
342
343 /* TYPE */
344 type = type_array[usbhsp_type(pipe)];
345
346 /* BFRE */
347 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
348 usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
349 bfre = 0; /* FIXME */
350
351 /* DBLB */
352 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
353 usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
354 dblb = (is_double) ? DBLB : 0;
355
356 /* CNTMD */
357 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
358 cntmd = 0; /* FIXME */
359
360 /* DIR */
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700361 if (dir_in)
Kuninori Morimotoad6f2a82011-06-06 14:17:56 +0900362 usbhsp_flags_set(pipe, IS_DIR_HOST);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900363
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700364 if ((is_host && !dir_in) ||
365 (!is_host && dir_in))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900366 dir |= DIR_OUT;
367
Kuninori Morimotoad6f2a82011-06-06 14:17:56 +0900368 if (!dir)
369 usbhsp_flags_set(pipe, IS_DIR_IN);
370
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900371 /* SHTNAK */
372 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
373 !dir)
374 shtnak = SHTNAK;
375
376 /* EPNUM */
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700377 epnum = 0; /* see usbhs_pipe_config_update() */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900378
379 return type |
380 bfre |
381 dblb |
382 cntmd |
383 dir |
384 shtnak |
385 epnum;
386}
387
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700388static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900389{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900390 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
391 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900392 struct device *dev = usbhs_priv_to_dev(priv);
393 int pipe_num = usbhs_pipe_number(pipe);
394 int is_double = usbhsp_possible_double_buffer(pipe);
395 u16 buff_size;
396 u16 bufnmb;
397 u16 bufnmb_cnt;
398
399 /*
400 * PIPEBUF
401 *
402 * see
403 * - "Register Descriptions" - "PIPEBUF" register
404 * - "Features" - "Pipe configuration"
405 * - "Operation" - "FIFO Buffer Memory"
406 * - "Operation" - "Pipe Control"
407 *
408 * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
409 *
410 * BUFNMB: PIPE
411 * 0: pipe0 (DCP 256byte)
412 * 1: -
413 * 2: -
414 * 3: -
415 * 4: pipe6 (INT 64byte)
416 * 5: pipe7 (INT 64byte)
417 * 6: pipe8 (INT 64byte)
418 * 7: pipe9 (INT 64byte)
419 * 8 - xx: free (for BULK, ISOC)
420 */
421
422 /*
423 * FIXME
424 *
425 * it doesn't have good buffer allocator
426 *
427 * DCP : 256 byte
428 * BULK: 512 byte
429 * INT : 64 byte
430 * ISOC: 512 byte
431 */
432 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
433 buff_size = 256;
434 else if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT))
435 buff_size = 64;
436 else
437 buff_size = 512;
438
439 /* change buff_size to register value */
440 bufnmb_cnt = (buff_size / 64) - 1;
441
442 /* BUFNMB has been reserved for INT pipe
443 * see above */
444 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
445 bufnmb = pipe_num - 2;
446 } else {
447 bufnmb = info->bufnmb_last;
448 info->bufnmb_last += bufnmb_cnt + 1;
449
450 /*
451 * double buffer
452 */
453 if (is_double)
454 info->bufnmb_last += bufnmb_cnt + 1;
455 }
456
457 dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
458 pipe_num, buff_size, bufnmb);
459
460 return (0x1f & bufnmb_cnt) << 10 |
461 (0xff & bufnmb) << 0;
462}
463
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700464void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 epnum, u16 maxp)
465{
466 usbhsp_pipe_barrier(pipe);
467
468 usbhsp_pipe_select(pipe);
469 usbhsp_pipe_maxp_set(pipe, 0xFFFF, maxp);
470
471 if (!usbhs_pipe_is_dcp(pipe))
472 usbhsp_pipe_cfg_set(pipe, 0x000F, epnum);
473}
474
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900475/*
476 * pipe control
477 */
478int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
479{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900480 u16 mask = usbhs_pipe_is_dcp(pipe) ? DCP_MAXP_MASK : PIPE_MAXP_MASK;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900481
482 usbhsp_pipe_select(pipe);
483
484 return (int)(usbhsp_pipe_maxp_get(pipe) & mask);
485}
486
487int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
488{
489 return usbhsp_flags_has(pipe, IS_DIR_IN);
490}
491
Kuninori Morimotoad6f2a82011-06-06 14:17:56 +0900492int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
493{
494 return usbhsp_flags_has(pipe, IS_DIR_HOST);
495}
496
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900497void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe)
498{
499 usbhsp_pipectrl_set(pipe, SQCLR, SQCLR);
500}
501
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900502void usbhs_pipe_clear(struct usbhs_pipe *pipe)
503{
504 usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
505 usbhsp_pipectrl_set(pipe, ACLRM, 0);
506}
507
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900508static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
509{
510 struct usbhs_pipe *pos, *pipe;
511 int i;
512
513 /*
514 * find target pipe
515 */
516 pipe = NULL;
517 usbhs_for_each_pipe_with_dcp(pos, priv, i) {
518 if (!usbhsp_type_is(pos, type))
519 continue;
520 if (usbhsp_flags_has(pos, IS_USED))
521 continue;
522
523 pipe = pos;
524 break;
525 }
526
527 if (!pipe)
528 return NULL;
529
530 /*
531 * initialize pipe flags
532 */
533 usbhsp_flags_init(pipe);
534 usbhsp_flags_set(pipe, IS_USED);
535
536 return pipe;
537}
538
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900539void usbhs_pipe_init(struct usbhs_priv *priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900540 void (*done)(struct usbhs_pkt *pkt),
541 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900542{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900543 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900544 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900545 struct usbhs_pipe *pipe;
546 int i;
547
Kuninori Morimotodad67392011-06-06 14:18:28 +0900548 if (!done) {
549 dev_err(dev, "no done function\n");
550 return;
551 }
552
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900553 /*
554 * FIXME
555 *
556 * driver needs good allocator.
557 *
558 * find first free buffer area (BULK, ISOC)
559 * (DCP, INT area is fixed)
560 *
561 * buffer number 0 - 3 have been reserved for DCP
562 * see
563 * usbhsp_to_bufnmb
564 */
565 info->bufnmb_last = 4;
566 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
567 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT))
568 info->bufnmb_last++;
569
570 usbhsp_flags_init(pipe);
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900571 pipe->fifo = NULL;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900572 pipe->mod_private = NULL;
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900573 INIT_LIST_HEAD(&pipe->list);
Kuninori Morimoto45e13e62011-04-21 14:09:58 +0900574
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900575 /* pipe force init */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900576 usbhs_pipe_clear(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900577 }
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900578
Kuninori Morimotodad67392011-06-06 14:18:28 +0900579 info->done = done;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900580 info->dma_map_ctrl = dma_map_ctrl;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900581}
582
583struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700584 int endpoint_type,
585 int dir_in)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900586{
587 struct device *dev = usbhs_priv_to_dev(priv);
588 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
589 struct usbhs_pipe *pipe;
590 int is_host = usbhs_mod_is_host(priv, mod);
591 int ret;
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700592 u16 pipecfg, pipebuf;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900593
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700594 pipe = usbhsp_get_pipe(priv, endpoint_type);
Kuninori Morimotof429ea32011-04-21 14:10:24 +0900595 if (!pipe) {
596 dev_err(dev, "can't get pipe (%s)\n",
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700597 usbhsp_pipe_name[endpoint_type]);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900598 return NULL;
Kuninori Morimotof429ea32011-04-21 14:10:24 +0900599 }
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900600
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900601 INIT_LIST_HEAD(&pipe->list);
602
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900603 usbhs_pipe_disable(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900604
605 /* make sure pipe is not busy */
606 ret = usbhsp_pipe_barrier(pipe);
607 if (ret < 0) {
608 dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
609 return NULL;
610 }
611
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700612 pipecfg = usbhsp_setup_pipecfg(pipe, is_host, dir_in);
613 pipebuf = usbhsp_setup_pipebuff(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900614
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900615 usbhsp_pipe_select(pipe);
616 usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
617 usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900618
619 usbhs_pipe_clear_sequence(pipe);
620
621 dev_dbg(dev, "enable pipe %d : %s (%s)\n",
622 usbhs_pipe_number(pipe),
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700623 usbhsp_pipe_name[endpoint_type],
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900624 usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
625
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700626 /*
627 * epnum / maxp are still not set to this pipe.
628 * call usbhs_pipe_config_update() after this function !!
629 */
630
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900631 return pipe;
632}
633
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900634void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo)
635{
636 if (pipe->fifo)
637 pipe->fifo->pipe = NULL;
638
639 pipe->fifo = fifo;
640
641 if (fifo)
642 fifo->pipe = pipe;
643}
644
645
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900646/*
647 * dcp control
648 */
649struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
650{
651 struct usbhs_pipe *pipe;
652
653 pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
654 if (!pipe)
655 return NULL;
656
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900657 INIT_LIST_HEAD(&pipe->list);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900658
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700659 /*
660 * call usbhs_pipe_config_update() after this function !!
661 */
662
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900663 return pipe;
664}
665
666void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
667{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900668 WARN_ON(!usbhs_pipe_is_dcp(pipe));
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900669
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900670 usbhs_pipe_enable(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900671 usbhsp_pipectrl_set(pipe, CCPL, CCPL);
672}
673
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900674/*
675 * pipe module function
676 */
677int usbhs_pipe_probe(struct usbhs_priv *priv)
678{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900679 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900680 struct usbhs_pipe *pipe;
681 struct device *dev = usbhs_priv_to_dev(priv);
682 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
683 int pipe_size = usbhs_get_dparam(priv, pipe_size);
684 int i;
685
686 /* This driver expects 1st pipe is DCP */
687 if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) {
688 dev_err(dev, "1st PIPE is not DCP\n");
689 return -EINVAL;
690 }
691
692 info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
693 if (!info->pipe) {
694 dev_err(dev, "Could not allocate pipe\n");
695 return -ENOMEM;
696 }
697
698 info->size = pipe_size;
699
700 /*
701 * init pipe
702 */
703 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
704 pipe->priv = priv;
705 usbhsp_type(pipe) = pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
706
707 dev_dbg(dev, "pipe %x\t: %s\n",
708 i, usbhsp_pipe_name[pipe_type[i]]);
709 }
710
711 return 0;
712}
713
714void usbhs_pipe_remove(struct usbhs_priv *priv)
715{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900716 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900717
718 kfree(info->pipe);
719}