blob: 18101332eb8adbbdeb7e29b1bbe5ded214f40144 [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
Kuninori Morimotof1407d52011-04-04 13:44:59 +090032/*
33 * for debug
34 */
35static char *usbhsp_pipe_name[] = {
36 [USB_ENDPOINT_XFER_CONTROL] = "DCP",
37 [USB_ENDPOINT_XFER_BULK] = "BULK",
38 [USB_ENDPOINT_XFER_INT] = "INT",
39 [USB_ENDPOINT_XFER_ISOC] = "ISO",
40};
41
42/*
43 * usb request functions
44 */
45void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
46{
47 u16 val;
48
49 val = usbhs_read(priv, USBREQ);
50 req->bRequest = (val >> 8) & 0xFF;
51 req->bRequestType = (val >> 0) & 0xFF;
52
53 req->wValue = usbhs_read(priv, USBVAL);
54 req->wIndex = usbhs_read(priv, USBINDX);
55 req->wLength = usbhs_read(priv, USBLENG);
56}
57
58void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
59{
60 usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
61 usbhs_write(priv, USBVAL, req->wValue);
62 usbhs_write(priv, USBINDX, req->wIndex);
63 usbhs_write(priv, USBLENG, req->wLength);
64}
65
66/*
67 * DCPCTR/PIPEnCTR functions
68 */
69static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
70{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090071 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +090072 int offset = usbhsp_addr_offset(pipe);
73
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090074 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +090075 usbhs_bset(priv, DCPCTR, mask, val);
76 else
77 usbhs_bset(priv, PIPEnCTR + offset, mask, val);
78}
79
80static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe)
81{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090082 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +090083 int offset = usbhsp_addr_offset(pipe);
84
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090085 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +090086 return usbhs_read(priv, DCPCTR);
87 else
88 return usbhs_read(priv, PIPEnCTR + offset);
89}
90
91/*
92 * DCP/PIPE functions
93 */
94static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
95 u16 dcp_reg, u16 pipe_reg,
96 u16 mask, u16 val)
97{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090098 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +090099
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900100 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900101 usbhs_bset(priv, dcp_reg, mask, val);
102 else
103 usbhs_bset(priv, pipe_reg, mask, val);
104}
105
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900106/*
107 * DCPCFG/PIPECFG functions
108 */
109static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
110{
111 __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
112}
113
114/*
115 * PIPEBUF
116 */
117static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
118{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900119 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900120 return;
121
122 __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val);
123}
124
125/*
126 * DCPMAXP/PIPEMAXP
127 */
128static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
129{
130 __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
131}
132
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900133/*
134 * pipe control functions
135 */
136static void usbhsp_pipe_select(struct usbhs_pipe *pipe)
137{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900138 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900139
140 /*
141 * On pipe, this is necessary before
142 * accesses to below registers.
143 *
144 * PIPESEL : usbhsp_pipe_select
145 * PIPECFG : usbhsp_pipe_cfg_xxx
146 * PIPEBUF : usbhsp_pipe_buf_xxx
147 * PIPEMAXP : usbhsp_pipe_maxp_xxx
148 * PIPEPERI
149 */
150
151 /*
152 * if pipe is dcp, no pipe is selected.
153 * it is no problem, because dcp have its register
154 */
155 usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe));
156}
157
158static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe)
159{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900160 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900161 int timeout = 1024;
162 u16 val;
163
164 /*
165 * make sure....
166 *
167 * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
168 * specified by the CURPIPE bits.
169 * When changing the setting of this bit after changing
170 * the PID bits for the selected pipe from BUF to NAK,
171 * check that CSSTS = 0 and PBUSY = 0.
172 */
173
174 /*
175 * CURPIPE bit = 0
176 *
177 * see also
178 * "Operation"
179 * - "Pipe Control"
180 * - "Pipe Control Registers Switching Procedure"
181 */
182 usbhs_write(priv, CFIFOSEL, 0);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900183 usbhs_pipe_disable(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900184
185 do {
186 val = usbhsp_pipectrl_get(pipe);
187 val &= CSSTS | PID_MASK;
188 if (!val)
189 return 0;
190
191 udelay(10);
192
193 } while (timeout--);
194
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900195 return -EBUSY;
196}
197
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900198int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900199{
200 u16 val;
201
202 val = usbhsp_pipectrl_get(pipe);
203 if (val & BSTS)
204 return 0;
205
206 return -EBUSY;
207}
208
209/*
210 * PID ctrl
211 */
212static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
213{
214 u16 pid = usbhsp_pipectrl_get(pipe);
215
216 pid &= PID_MASK;
217
218 /*
219 * see
220 * "Pipe n Control Register" - "PID"
221 */
222 switch (pid) {
223 case PID_STALL11:
224 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
225 /* fall-through */
226 case PID_STALL10:
227 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
228 }
229}
230
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900231void usbhs_pipe_disable(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900232{
Kuninori Morimotoc786e092011-05-11 16:00:09 +0900233 int timeout = 1024;
234 u16 val;
235
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900236 /* see "Pipe n Control Register" - "PID" */
237 __usbhsp_pid_try_nak_if_stall(pipe);
238
239 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
Kuninori Morimotoc786e092011-05-11 16:00:09 +0900240
241 do {
242 val = usbhsp_pipectrl_get(pipe);
243 val &= PBUSY;
244 if (!val)
245 break;
246
247 udelay(10);
248 } while (timeout--);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900249}
250
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900251void usbhs_pipe_enable(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900252{
253 /* see "Pipe n Control Register" - "PID" */
254 __usbhsp_pid_try_nak_if_stall(pipe);
255
256 usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
257}
258
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900259void usbhs_pipe_stall(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900260{
261 u16 pid = usbhsp_pipectrl_get(pipe);
262
263 pid &= PID_MASK;
264
265 /*
266 * see
267 * "Pipe n Control Register" - "PID"
268 */
269 switch (pid) {
270 case PID_NAK:
271 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
272 break;
273 case PID_BUF:
274 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
275 break;
276 }
277}
278
279/*
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900280 * pipe setup
281 */
282static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
283{
284 /*
285 * only ISO / BULK pipe can use double buffer
286 */
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700287 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
288 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900289 return 1;
290
291 return 0;
292}
293
294static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700295 int is_host,
296 int dir_in)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900297{
298 u16 type = 0;
299 u16 bfre = 0;
300 u16 dblb = 0;
301 u16 cntmd = 0;
302 u16 dir = 0;
303 u16 epnum = 0;
304 u16 shtnak = 0;
305 u16 type_array[] = {
306 [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
307 [USB_ENDPOINT_XFER_INT] = TYPE_INT,
308 [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
309 };
310 int is_double = usbhsp_possible_double_buffer(pipe);
311
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900312 if (usbhs_pipe_is_dcp(pipe))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900313 return -EINVAL;
314
315 /*
316 * PIPECFG
317 *
318 * see
319 * - "Register Descriptions" - "PIPECFG" register
320 * - "Features" - "Pipe configuration"
321 * - "Operation" - "Pipe Control"
322 */
323
324 /* TYPE */
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700325 type = type_array[usbhs_pipe_type(pipe)];
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900326
327 /* BFRE */
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700328 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
329 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900330 bfre = 0; /* FIXME */
331
332 /* DBLB */
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700333 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
334 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900335 dblb = (is_double) ? DBLB : 0;
336
337 /* CNTMD */
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700338 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900339 cntmd = 0; /* FIXME */
340
341 /* DIR */
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700342 if (dir_in)
Kuninori Morimotoad6f2a82011-06-06 14:17:56 +0900343 usbhsp_flags_set(pipe, IS_DIR_HOST);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900344
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700345 if ((is_host && !dir_in) ||
346 (!is_host && dir_in))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900347 dir |= DIR_OUT;
348
Kuninori Morimotoad6f2a82011-06-06 14:17:56 +0900349 if (!dir)
350 usbhsp_flags_set(pipe, IS_DIR_IN);
351
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900352 /* SHTNAK */
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700353 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900354 !dir)
355 shtnak = SHTNAK;
356
357 /* EPNUM */
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700358 epnum = 0; /* see usbhs_pipe_config_update() */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900359
360 return type |
361 bfre |
362 dblb |
363 cntmd |
364 dir |
365 shtnak |
366 epnum;
367}
368
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700369static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900370{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900371 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
372 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900373 struct device *dev = usbhs_priv_to_dev(priv);
374 int pipe_num = usbhs_pipe_number(pipe);
375 int is_double = usbhsp_possible_double_buffer(pipe);
376 u16 buff_size;
377 u16 bufnmb;
378 u16 bufnmb_cnt;
379
380 /*
381 * PIPEBUF
382 *
383 * see
384 * - "Register Descriptions" - "PIPEBUF" register
385 * - "Features" - "Pipe configuration"
386 * - "Operation" - "FIFO Buffer Memory"
387 * - "Operation" - "Pipe Control"
388 *
389 * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
390 *
391 * BUFNMB: PIPE
392 * 0: pipe0 (DCP 256byte)
393 * 1: -
394 * 2: -
395 * 3: -
396 * 4: pipe6 (INT 64byte)
397 * 5: pipe7 (INT 64byte)
398 * 6: pipe8 (INT 64byte)
399 * 7: pipe9 (INT 64byte)
400 * 8 - xx: free (for BULK, ISOC)
401 */
402
403 /*
404 * FIXME
405 *
406 * it doesn't have good buffer allocator
407 *
408 * DCP : 256 byte
409 * BULK: 512 byte
410 * INT : 64 byte
411 * ISOC: 512 byte
412 */
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700413 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900414 buff_size = 256;
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700415 else if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900416 buff_size = 64;
417 else
418 buff_size = 512;
419
420 /* change buff_size to register value */
421 bufnmb_cnt = (buff_size / 64) - 1;
422
423 /* BUFNMB has been reserved for INT pipe
424 * see above */
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700425 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900426 bufnmb = pipe_num - 2;
427 } else {
428 bufnmb = info->bufnmb_last;
429 info->bufnmb_last += bufnmb_cnt + 1;
430
431 /*
432 * double buffer
433 */
434 if (is_double)
435 info->bufnmb_last += bufnmb_cnt + 1;
436 }
437
438 dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
439 pipe_num, buff_size, bufnmb);
440
441 return (0x1f & bufnmb_cnt) << 10 |
442 (0xff & bufnmb) << 0;
443}
444
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700445void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 epnum, u16 maxp)
446{
447 usbhsp_pipe_barrier(pipe);
448
Kuninori Morimoto7fd097e2011-10-10 22:00:42 -0700449 pipe->maxp = maxp;
450
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700451 usbhsp_pipe_select(pipe);
452 usbhsp_pipe_maxp_set(pipe, 0xFFFF, maxp);
453
454 if (!usbhs_pipe_is_dcp(pipe))
455 usbhsp_pipe_cfg_set(pipe, 0x000F, epnum);
456}
457
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900458/*
459 * pipe control
460 */
461int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
462{
Kuninori Morimoto7fd097e2011-10-10 22:00:42 -0700463 /*
464 * see
465 * usbhs_pipe_config_update()
466 * usbhs_dcp_malloc()
467 */
468 return pipe->maxp;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900469}
470
471int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
472{
473 return usbhsp_flags_has(pipe, IS_DIR_IN);
474}
475
Kuninori Morimotoad6f2a82011-06-06 14:17:56 +0900476int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
477{
478 return usbhsp_flags_has(pipe, IS_DIR_HOST);
479}
480
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900481void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe)
482{
483 usbhsp_pipectrl_set(pipe, SQCLR, SQCLR);
484}
485
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900486void usbhs_pipe_clear(struct usbhs_pipe *pipe)
487{
488 usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
489 usbhsp_pipectrl_set(pipe, ACLRM, 0);
490}
491
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900492static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
493{
494 struct usbhs_pipe *pos, *pipe;
495 int i;
496
497 /*
498 * find target pipe
499 */
500 pipe = NULL;
501 usbhs_for_each_pipe_with_dcp(pos, priv, i) {
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700502 if (!usbhs_pipe_type_is(pos, type))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900503 continue;
504 if (usbhsp_flags_has(pos, IS_USED))
505 continue;
506
507 pipe = pos;
508 break;
509 }
510
511 if (!pipe)
512 return NULL;
513
514 /*
515 * initialize pipe flags
516 */
517 usbhsp_flags_init(pipe);
518 usbhsp_flags_set(pipe, IS_USED);
519
520 return pipe;
521}
522
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900523void usbhs_pipe_init(struct usbhs_priv *priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900524 void (*done)(struct usbhs_pkt *pkt),
525 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900526{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900527 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900528 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900529 struct usbhs_pipe *pipe;
530 int i;
531
Kuninori Morimotodad67392011-06-06 14:18:28 +0900532 if (!done) {
533 dev_err(dev, "no done function\n");
534 return;
535 }
536
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900537 /*
538 * FIXME
539 *
540 * driver needs good allocator.
541 *
542 * find first free buffer area (BULK, ISOC)
543 * (DCP, INT area is fixed)
544 *
545 * buffer number 0 - 3 have been reserved for DCP
546 * see
547 * usbhsp_to_bufnmb
548 */
549 info->bufnmb_last = 4;
550 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700551 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900552 info->bufnmb_last++;
553
554 usbhsp_flags_init(pipe);
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900555 pipe->fifo = NULL;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900556 pipe->mod_private = NULL;
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900557 INIT_LIST_HEAD(&pipe->list);
Kuninori Morimoto45e13e62011-04-21 14:09:58 +0900558
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900559 /* pipe force init */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900560 usbhs_pipe_clear(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900561 }
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900562
Kuninori Morimotodad67392011-06-06 14:18:28 +0900563 info->done = done;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900564 info->dma_map_ctrl = dma_map_ctrl;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900565}
566
567struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700568 int endpoint_type,
569 int dir_in)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900570{
571 struct device *dev = usbhs_priv_to_dev(priv);
572 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
573 struct usbhs_pipe *pipe;
574 int is_host = usbhs_mod_is_host(priv, mod);
575 int ret;
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700576 u16 pipecfg, pipebuf;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900577
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700578 pipe = usbhsp_get_pipe(priv, endpoint_type);
Kuninori Morimotof429ea32011-04-21 14:10:24 +0900579 if (!pipe) {
580 dev_err(dev, "can't get pipe (%s)\n",
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700581 usbhsp_pipe_name[endpoint_type]);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900582 return NULL;
Kuninori Morimotof429ea32011-04-21 14:10:24 +0900583 }
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900584
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900585 INIT_LIST_HEAD(&pipe->list);
586
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900587 usbhs_pipe_disable(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900588
589 /* make sure pipe is not busy */
590 ret = usbhsp_pipe_barrier(pipe);
591 if (ret < 0) {
592 dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
593 return NULL;
594 }
595
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700596 pipecfg = usbhsp_setup_pipecfg(pipe, is_host, dir_in);
597 pipebuf = usbhsp_setup_pipebuff(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900598
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900599 usbhsp_pipe_select(pipe);
600 usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
601 usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900602
603 usbhs_pipe_clear_sequence(pipe);
604
605 dev_dbg(dev, "enable pipe %d : %s (%s)\n",
606 usbhs_pipe_number(pipe),
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700607 usbhsp_pipe_name[endpoint_type],
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900608 usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
609
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700610 /*
611 * epnum / maxp are still not set to this pipe.
612 * call usbhs_pipe_config_update() after this function !!
613 */
614
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900615 return pipe;
616}
617
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900618void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo)
619{
620 if (pipe->fifo)
621 pipe->fifo->pipe = NULL;
622
623 pipe->fifo = fifo;
624
625 if (fifo)
626 fifo->pipe = pipe;
627}
628
629
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900630/*
631 * dcp control
632 */
633struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
634{
635 struct usbhs_pipe *pipe;
636
637 pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
638 if (!pipe)
639 return NULL;
640
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900641 INIT_LIST_HEAD(&pipe->list);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900642
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700643 /*
644 * call usbhs_pipe_config_update() after this function !!
645 */
646
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900647 return pipe;
648}
649
650void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
651{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900652 WARN_ON(!usbhs_pipe_is_dcp(pipe));
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900653
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900654 usbhs_pipe_enable(pipe);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900655 usbhsp_pipectrl_set(pipe, CCPL, CCPL);
656}
657
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900658/*
659 * pipe module function
660 */
661int usbhs_pipe_probe(struct usbhs_priv *priv)
662{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900663 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900664 struct usbhs_pipe *pipe;
665 struct device *dev = usbhs_priv_to_dev(priv);
666 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
667 int pipe_size = usbhs_get_dparam(priv, pipe_size);
668 int i;
669
670 /* This driver expects 1st pipe is DCP */
671 if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) {
672 dev_err(dev, "1st PIPE is not DCP\n");
673 return -EINVAL;
674 }
675
676 info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
677 if (!info->pipe) {
678 dev_err(dev, "Could not allocate pipe\n");
679 return -ENOMEM;
680 }
681
682 info->size = pipe_size;
683
684 /*
685 * init pipe
686 */
687 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
688 pipe->priv = priv;
Kuninori Morimoto356db7e2011-10-10 22:02:21 -0700689
690 usbhs_pipe_type(pipe) =
691 pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900692
693 dev_dbg(dev, "pipe %x\t: %s\n",
694 i, usbhsp_pipe_name[pipe_type[i]]);
695 }
696
697 return 0;
698}
699
700void usbhs_pipe_remove(struct usbhs_priv *priv)
701{
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900702 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900703
704 kfree(info->pipe);
705}