]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/usb/renesas_usbhs/pipe.c
usb: gadget: renesas_usbhs: add usbhs_dcp_dir_for_host()
[mv-sheeva.git] / drivers / usb / renesas_usbhs / pipe.c
1 /*
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>
18 #include <linux/slab.h>
19 #include "./common.h"
20 #include "./pipe.h"
21
22 /*
23  *              macros
24  */
25 #define usbhsp_addr_offset(p)   ((usbhs_pipe_number(p) - 1) * 2)
26
27 #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 /*
33  * for debug
34  */
35 static 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  *              DCPCTR/PIPEnCTR functions
44  */
45 static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
46 {
47         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
48         int offset = usbhsp_addr_offset(pipe);
49
50         if (usbhs_pipe_is_dcp(pipe))
51                 usbhs_bset(priv, DCPCTR, mask, val);
52         else
53                 usbhs_bset(priv, PIPEnCTR + offset, mask, val);
54 }
55
56 static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe)
57 {
58         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
59         int offset = usbhsp_addr_offset(pipe);
60
61         if (usbhs_pipe_is_dcp(pipe))
62                 return usbhs_read(priv, DCPCTR);
63         else
64                 return usbhs_read(priv, PIPEnCTR + offset);
65 }
66
67 /*
68  *              DCP/PIPE functions
69  */
70 static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
71                                   u16 dcp_reg, u16 pipe_reg,
72                                   u16 mask, u16 val)
73 {
74         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
75
76         if (usbhs_pipe_is_dcp(pipe))
77                 usbhs_bset(priv, dcp_reg, mask, val);
78         else
79                 usbhs_bset(priv, pipe_reg, mask, val);
80 }
81
82 /*
83  *              DCPCFG/PIPECFG functions
84  */
85 static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
86 {
87         __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
88 }
89
90 /*
91  *              PIPEBUF
92  */
93 static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
94 {
95         if (usbhs_pipe_is_dcp(pipe))
96                 return;
97
98         __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val);
99 }
100
101 /*
102  *              DCPMAXP/PIPEMAXP
103  */
104 static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
105 {
106         __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
107 }
108
109 /*
110  *              pipe control functions
111  */
112 static void usbhsp_pipe_select(struct usbhs_pipe *pipe)
113 {
114         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
115
116         /*
117          * On pipe, this is necessary before
118          * accesses to below registers.
119          *
120          * PIPESEL      : usbhsp_pipe_select
121          * PIPECFG      : usbhsp_pipe_cfg_xxx
122          * PIPEBUF      : usbhsp_pipe_buf_xxx
123          * PIPEMAXP     : usbhsp_pipe_maxp_xxx
124          * PIPEPERI
125          */
126
127         /*
128          * if pipe is dcp, no pipe is selected.
129          * it is no problem, because dcp have its register
130          */
131         usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe));
132 }
133
134 static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe)
135 {
136         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
137         int timeout = 1024;
138         u16 val;
139
140         /*
141          * make sure....
142          *
143          * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
144          * specified by the CURPIPE bits.
145          * When changing the setting of this bit after changing
146          * the PID bits for the selected pipe from BUF to NAK,
147          * check that CSSTS = 0 and PBUSY = 0.
148          */
149
150         /*
151          * CURPIPE bit = 0
152          *
153          * see also
154          *  "Operation"
155          *  - "Pipe Control"
156          *   - "Pipe Control Registers Switching Procedure"
157          */
158         usbhs_write(priv, CFIFOSEL, 0);
159         usbhs_pipe_disable(pipe);
160
161         do {
162                 val  = usbhsp_pipectrl_get(pipe);
163                 val &= CSSTS | PID_MASK;
164                 if (!val)
165                         return 0;
166
167                 udelay(10);
168
169         } while (timeout--);
170
171         return -EBUSY;
172 }
173
174 int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe)
175 {
176         u16 val;
177
178         val = usbhsp_pipectrl_get(pipe);
179         if (val & BSTS)
180                 return 0;
181
182         return -EBUSY;
183 }
184
185 /*
186  *              PID ctrl
187  */
188 static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
189 {
190         u16 pid = usbhsp_pipectrl_get(pipe);
191
192         pid &= PID_MASK;
193
194         /*
195          * see
196          * "Pipe n Control Register" - "PID"
197          */
198         switch (pid) {
199         case PID_STALL11:
200                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
201                 /* fall-through */
202         case PID_STALL10:
203                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
204         }
205 }
206
207 void usbhs_pipe_disable(struct usbhs_pipe *pipe)
208 {
209         int timeout = 1024;
210         u16 val;
211
212         /* see "Pipe n Control Register" - "PID" */
213         __usbhsp_pid_try_nak_if_stall(pipe);
214
215         usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
216
217         do {
218                 val  = usbhsp_pipectrl_get(pipe);
219                 val &= PBUSY;
220                 if (!val)
221                         break;
222
223                 udelay(10);
224         } while (timeout--);
225 }
226
227 void usbhs_pipe_enable(struct usbhs_pipe *pipe)
228 {
229         /* see "Pipe n Control Register" - "PID" */
230         __usbhsp_pid_try_nak_if_stall(pipe);
231
232         usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
233 }
234
235 void usbhs_pipe_stall(struct usbhs_pipe *pipe)
236 {
237         u16 pid = usbhsp_pipectrl_get(pipe);
238
239         pid &= PID_MASK;
240
241         /*
242          * see
243          * "Pipe n Control Register" - "PID"
244          */
245         switch (pid) {
246         case PID_NAK:
247                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
248                 break;
249         case PID_BUF:
250                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
251                 break;
252         }
253 }
254
255 /*
256  *              pipe setup
257  */
258 static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
259 {
260         /*
261          * only ISO / BULK pipe can use double buffer
262          */
263         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
264             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
265                 return 1;
266
267         return 0;
268 }
269
270 static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
271                                 int is_host,
272                                 int dir_in)
273 {
274         u16 type = 0;
275         u16 bfre = 0;
276         u16 dblb = 0;
277         u16 cntmd = 0;
278         u16 dir = 0;
279         u16 epnum = 0;
280         u16 shtnak = 0;
281         u16 type_array[] = {
282                 [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
283                 [USB_ENDPOINT_XFER_INT]  = TYPE_INT,
284                 [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
285         };
286         int is_double = usbhsp_possible_double_buffer(pipe);
287
288         if (usbhs_pipe_is_dcp(pipe))
289                 return -EINVAL;
290
291         /*
292          * PIPECFG
293          *
294          * see
295          *  - "Register Descriptions" - "PIPECFG" register
296          *  - "Features"  - "Pipe configuration"
297          *  - "Operation" - "Pipe Control"
298          */
299
300         /* TYPE */
301         type = type_array[usbhs_pipe_type(pipe)];
302
303         /* BFRE */
304         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
305             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
306                 bfre = 0; /* FIXME */
307
308         /* DBLB */
309         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
310             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
311                 dblb = (is_double) ? DBLB : 0;
312
313         /* CNTMD */
314         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
315                 cntmd = 0; /* FIXME */
316
317         /* DIR */
318         if (dir_in)
319                 usbhsp_flags_set(pipe, IS_DIR_HOST);
320
321         if ((is_host  && !dir_in) ||
322             (!is_host && dir_in))
323                 dir |= DIR_OUT;
324
325         if (!dir)
326                 usbhsp_flags_set(pipe, IS_DIR_IN);
327
328         /* SHTNAK */
329         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
330             !dir)
331                 shtnak = SHTNAK;
332
333         /* EPNUM */
334         epnum = 0; /* see usbhs_pipe_config_update() */
335
336         return  type    |
337                 bfre    |
338                 dblb    |
339                 cntmd   |
340                 dir     |
341                 shtnak  |
342                 epnum;
343 }
344
345 static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe)
346 {
347         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
348         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
349         struct device *dev = usbhs_priv_to_dev(priv);
350         int pipe_num = usbhs_pipe_number(pipe);
351         int is_double = usbhsp_possible_double_buffer(pipe);
352         u16 buff_size;
353         u16 bufnmb;
354         u16 bufnmb_cnt;
355
356         /*
357          * PIPEBUF
358          *
359          * see
360          *  - "Register Descriptions" - "PIPEBUF" register
361          *  - "Features"  - "Pipe configuration"
362          *  - "Operation" - "FIFO Buffer Memory"
363          *  - "Operation" - "Pipe Control"
364          *
365          * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
366          *
367          * BUFNMB:      PIPE
368          * 0:           pipe0 (DCP 256byte)
369          * 1:           -
370          * 2:           -
371          * 3:           -
372          * 4:           pipe6 (INT 64byte)
373          * 5:           pipe7 (INT 64byte)
374          * 6:           pipe8 (INT 64byte)
375          * 7:           pipe9 (INT 64byte)
376          * 8 - xx:      free (for BULK, ISOC)
377          */
378
379         /*
380          * FIXME
381          *
382          * it doesn't have good buffer allocator
383          *
384          * DCP : 256 byte
385          * BULK: 512 byte
386          * INT :  64 byte
387          * ISOC: 512 byte
388          */
389         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
390                 buff_size = 256;
391         else if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
392                 buff_size = 64;
393         else
394                 buff_size = 512;
395
396         /* change buff_size to register value */
397         bufnmb_cnt = (buff_size / 64) - 1;
398
399         /* BUFNMB has been reserved for INT pipe
400          * see above */
401         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
402                 bufnmb = pipe_num - 2;
403         } else {
404                 bufnmb = info->bufnmb_last;
405                 info->bufnmb_last += bufnmb_cnt + 1;
406
407                 /*
408                  * double buffer
409                  */
410                 if (is_double)
411                         info->bufnmb_last += bufnmb_cnt + 1;
412         }
413
414         dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
415                 pipe_num, buff_size, bufnmb);
416
417         return  (0x1f & bufnmb_cnt)     << 10 |
418                 (0xff & bufnmb)         <<  0;
419 }
420
421 void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 epnum, u16 maxp)
422 {
423         usbhsp_pipe_barrier(pipe);
424
425         pipe->maxp = maxp;
426
427         usbhsp_pipe_select(pipe);
428         usbhsp_pipe_maxp_set(pipe, 0xFFFF, maxp);
429
430         if (!usbhs_pipe_is_dcp(pipe))
431                 usbhsp_pipe_cfg_set(pipe,  0x000F, epnum);
432 }
433
434 /*
435  *              pipe control
436  */
437 int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
438 {
439         /*
440          * see
441          *      usbhs_pipe_config_update()
442          *      usbhs_dcp_malloc()
443          */
444         return pipe->maxp;
445 }
446
447 int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
448 {
449         return usbhsp_flags_has(pipe, IS_DIR_IN);
450 }
451
452 int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
453 {
454         return usbhsp_flags_has(pipe, IS_DIR_HOST);
455 }
456
457 void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe)
458 {
459         usbhsp_pipectrl_set(pipe, SQCLR, SQCLR);
460 }
461
462 void usbhs_pipe_clear(struct usbhs_pipe *pipe)
463 {
464         usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
465         usbhsp_pipectrl_set(pipe, ACLRM, 0);
466 }
467
468 static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
469 {
470         struct usbhs_pipe *pos, *pipe;
471         int i;
472
473         /*
474          * find target pipe
475          */
476         pipe = NULL;
477         usbhs_for_each_pipe_with_dcp(pos, priv, i) {
478                 if (!usbhs_pipe_type_is(pos, type))
479                         continue;
480                 if (usbhsp_flags_has(pos, IS_USED))
481                         continue;
482
483                 pipe = pos;
484                 break;
485         }
486
487         if (!pipe)
488                 return NULL;
489
490         /*
491          * initialize pipe flags
492          */
493         usbhsp_flags_init(pipe);
494         usbhsp_flags_set(pipe, IS_USED);
495
496         return pipe;
497 }
498
499 void usbhs_pipe_init(struct usbhs_priv *priv,
500                      void (*done)(struct usbhs_pkt *pkt),
501                      int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map))
502 {
503         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
504         struct device *dev = usbhs_priv_to_dev(priv);
505         struct usbhs_pipe *pipe;
506         int i;
507
508         if (!done) {
509                 dev_err(dev, "no done function\n");
510                 return;
511         }
512
513         /*
514          * FIXME
515          *
516          * driver needs good allocator.
517          *
518          * find first free buffer area (BULK, ISOC)
519          * (DCP, INT area is fixed)
520          *
521          * buffer number 0 - 3 have been reserved for DCP
522          * see
523          *      usbhsp_to_bufnmb
524          */
525         info->bufnmb_last = 4;
526         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
527                 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
528                         info->bufnmb_last++;
529
530                 usbhsp_flags_init(pipe);
531                 pipe->fifo = NULL;
532                 pipe->mod_private = NULL;
533                 INIT_LIST_HEAD(&pipe->list);
534
535                 /* pipe force init */
536                 usbhs_pipe_clear(pipe);
537         }
538
539         info->done = done;
540         info->dma_map_ctrl = dma_map_ctrl;
541 }
542
543 struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
544                                      int endpoint_type,
545                                      int dir_in)
546 {
547         struct device *dev = usbhs_priv_to_dev(priv);
548         struct usbhs_pipe *pipe;
549         int is_host = usbhs_mod_is_host(priv);
550         int ret;
551         u16 pipecfg, pipebuf;
552
553         pipe = usbhsp_get_pipe(priv, endpoint_type);
554         if (!pipe) {
555                 dev_err(dev, "can't get pipe (%s)\n",
556                         usbhsp_pipe_name[endpoint_type]);
557                 return NULL;
558         }
559
560         INIT_LIST_HEAD(&pipe->list);
561
562         usbhs_pipe_disable(pipe);
563
564         /* make sure pipe is not busy */
565         ret = usbhsp_pipe_barrier(pipe);
566         if (ret < 0) {
567                 dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
568                 return NULL;
569         }
570
571         pipecfg  = usbhsp_setup_pipecfg(pipe, is_host, dir_in);
572         pipebuf  = usbhsp_setup_pipebuff(pipe);
573
574         usbhsp_pipe_select(pipe);
575         usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
576         usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
577
578         usbhs_pipe_clear_sequence(pipe);
579
580         dev_dbg(dev, "enable pipe %d : %s (%s)\n",
581                 usbhs_pipe_number(pipe),
582                 usbhsp_pipe_name[endpoint_type],
583                 usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
584
585         /*
586          * epnum / maxp are still not set to this pipe.
587          * call usbhs_pipe_config_update() after this function !!
588          */
589
590         return pipe;
591 }
592
593 void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo)
594 {
595         if (pipe->fifo)
596                 pipe->fifo->pipe = NULL;
597
598         pipe->fifo = fifo;
599
600         if (fifo)
601                 fifo->pipe = pipe;
602 }
603
604
605 /*
606  *              dcp control
607  */
608 struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
609 {
610         struct usbhs_pipe *pipe;
611
612         pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
613         if (!pipe)
614                 return NULL;
615
616         INIT_LIST_HEAD(&pipe->list);
617
618         /*
619          * call usbhs_pipe_config_update() after this function !!
620          */
621
622         return pipe;
623 }
624
625 void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
626 {
627         WARN_ON(!usbhs_pipe_is_dcp(pipe));
628
629         usbhs_pipe_enable(pipe);
630         usbhsp_pipectrl_set(pipe, CCPL, CCPL);
631 }
632
633 void usbhs_dcp_dir_for_host(struct usbhs_pipe *pipe, int dir_out)
634 {
635         usbhsp_pipe_cfg_set(pipe, DIR_OUT,
636                             dir_out ? DIR_OUT : 0);
637 }
638
639 /*
640  *              pipe module function
641  */
642 int usbhs_pipe_probe(struct usbhs_priv *priv)
643 {
644         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
645         struct usbhs_pipe *pipe;
646         struct device *dev = usbhs_priv_to_dev(priv);
647         u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
648         int pipe_size = usbhs_get_dparam(priv, pipe_size);
649         int i;
650
651         /* This driver expects 1st pipe is DCP */
652         if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) {
653                 dev_err(dev, "1st PIPE is not DCP\n");
654                 return -EINVAL;
655         }
656
657         info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
658         if (!info->pipe) {
659                 dev_err(dev, "Could not allocate pipe\n");
660                 return -ENOMEM;
661         }
662
663         info->size = pipe_size;
664
665         /*
666          * init pipe
667          */
668         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
669                 pipe->priv = priv;
670
671                 usbhs_pipe_type(pipe) =
672                         pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
673
674                 dev_dbg(dev, "pipe %x\t: %s\n",
675                         i, usbhsp_pipe_name[pipe_type[i]]);
676         }
677
678         return 0;
679 }
680
681 void usbhs_pipe_remove(struct usbhs_priv *priv)
682 {
683         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
684
685         kfree(info->pipe);
686 }