4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
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
17 #include <linux/delay.h>
18 #include <linux/slab.h>
25 #define usbhsp_addr_offset(p) ((usbhs_pipe_number(p) - 1) * 2)
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)
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",
43 * usb request functions
45 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
49 val = usbhs_read(priv, USBREQ);
50 req->bRequest = (val >> 8) & 0xFF;
51 req->bRequestType = (val >> 0) & 0xFF;
53 req->wValue = usbhs_read(priv, USBVAL);
54 req->wIndex = usbhs_read(priv, USBINDX);
55 req->wLength = usbhs_read(priv, USBLENG);
58 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
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);
67 * DCPCTR/PIPEnCTR functions
69 static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
71 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
72 int offset = usbhsp_addr_offset(pipe);
74 if (usbhs_pipe_is_dcp(pipe))
75 usbhs_bset(priv, DCPCTR, mask, val);
77 usbhs_bset(priv, PIPEnCTR + offset, mask, val);
80 static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe)
82 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
83 int offset = usbhsp_addr_offset(pipe);
85 if (usbhs_pipe_is_dcp(pipe))
86 return usbhs_read(priv, DCPCTR);
88 return usbhs_read(priv, PIPEnCTR + offset);
94 static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
95 u16 dcp_reg, u16 pipe_reg,
98 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
100 if (usbhs_pipe_is_dcp(pipe))
101 usbhs_bset(priv, dcp_reg, mask, val);
103 usbhs_bset(priv, pipe_reg, mask, val);
107 * DCPCFG/PIPECFG functions
109 static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
111 __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
117 static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
119 if (usbhs_pipe_is_dcp(pipe))
122 __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val);
128 static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
130 __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
134 * pipe control functions
136 static void usbhsp_pipe_select(struct usbhs_pipe *pipe)
138 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
141 * On pipe, this is necessary before
142 * accesses to below registers.
144 * PIPESEL : usbhsp_pipe_select
145 * PIPECFG : usbhsp_pipe_cfg_xxx
146 * PIPEBUF : usbhsp_pipe_buf_xxx
147 * PIPEMAXP : usbhsp_pipe_maxp_xxx
152 * if pipe is dcp, no pipe is selected.
153 * it is no problem, because dcp have its register
155 usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe));
158 static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe)
160 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
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.
180 * - "Pipe Control Registers Switching Procedure"
182 usbhs_write(priv, CFIFOSEL, 0);
183 usbhs_pipe_disable(pipe);
186 val = usbhsp_pipectrl_get(pipe);
187 val &= CSSTS | PID_MASK;
198 int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe)
202 val = usbhsp_pipectrl_get(pipe);
212 static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
214 u16 pid = usbhsp_pipectrl_get(pipe);
220 * "Pipe n Control Register" - "PID"
224 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
227 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
231 void usbhs_pipe_disable(struct usbhs_pipe *pipe)
236 /* see "Pipe n Control Register" - "PID" */
237 __usbhsp_pid_try_nak_if_stall(pipe);
239 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
242 val = usbhsp_pipectrl_get(pipe);
251 void usbhs_pipe_enable(struct usbhs_pipe *pipe)
253 /* see "Pipe n Control Register" - "PID" */
254 __usbhsp_pid_try_nak_if_stall(pipe);
256 usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
259 void usbhs_pipe_stall(struct usbhs_pipe *pipe)
261 u16 pid = usbhsp_pipectrl_get(pipe);
267 * "Pipe n Control Register" - "PID"
271 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
274 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
282 static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
285 * only ISO / BULK pipe can use double buffer
287 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
288 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
294 static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
306 [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
307 [USB_ENDPOINT_XFER_INT] = TYPE_INT,
308 [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
310 int is_double = usbhsp_possible_double_buffer(pipe);
312 if (usbhs_pipe_is_dcp(pipe))
319 * - "Register Descriptions" - "PIPECFG" register
320 * - "Features" - "Pipe configuration"
321 * - "Operation" - "Pipe Control"
325 type = type_array[usbhs_pipe_type(pipe)];
328 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
329 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
330 bfre = 0; /* FIXME */
333 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
334 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
335 dblb = (is_double) ? DBLB : 0;
338 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
339 cntmd = 0; /* FIXME */
343 usbhsp_flags_set(pipe, IS_DIR_HOST);
345 if ((is_host && !dir_in) ||
346 (!is_host && dir_in))
350 usbhsp_flags_set(pipe, IS_DIR_IN);
353 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
358 epnum = 0; /* see usbhs_pipe_config_update() */
369 static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe)
371 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
372 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
373 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);
384 * - "Register Descriptions" - "PIPEBUF" register
385 * - "Features" - "Pipe configuration"
386 * - "Operation" - "FIFO Buffer Memory"
387 * - "Operation" - "Pipe Control"
389 * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
392 * 0: pipe0 (DCP 256byte)
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)
406 * it doesn't have good buffer allocator
413 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
415 else if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
420 /* change buff_size to register value */
421 bufnmb_cnt = (buff_size / 64) - 1;
423 /* BUFNMB has been reserved for INT pipe
425 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
426 bufnmb = pipe_num - 2;
428 bufnmb = info->bufnmb_last;
429 info->bufnmb_last += bufnmb_cnt + 1;
435 info->bufnmb_last += bufnmb_cnt + 1;
438 dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
439 pipe_num, buff_size, bufnmb);
441 return (0x1f & bufnmb_cnt) << 10 |
442 (0xff & bufnmb) << 0;
445 void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 epnum, u16 maxp)
447 usbhsp_pipe_barrier(pipe);
451 usbhsp_pipe_select(pipe);
452 usbhsp_pipe_maxp_set(pipe, 0xFFFF, maxp);
454 if (!usbhs_pipe_is_dcp(pipe))
455 usbhsp_pipe_cfg_set(pipe, 0x000F, epnum);
461 int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
465 * usbhs_pipe_config_update()
471 int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
473 return usbhsp_flags_has(pipe, IS_DIR_IN);
476 int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
478 return usbhsp_flags_has(pipe, IS_DIR_HOST);
481 void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe)
483 usbhsp_pipectrl_set(pipe, SQCLR, SQCLR);
486 void usbhs_pipe_clear(struct usbhs_pipe *pipe)
488 usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
489 usbhsp_pipectrl_set(pipe, ACLRM, 0);
492 static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
494 struct usbhs_pipe *pos, *pipe;
501 usbhs_for_each_pipe_with_dcp(pos, priv, i) {
502 if (!usbhs_pipe_type_is(pos, type))
504 if (usbhsp_flags_has(pos, IS_USED))
515 * initialize pipe flags
517 usbhsp_flags_init(pipe);
518 usbhsp_flags_set(pipe, IS_USED);
523 void usbhs_pipe_init(struct usbhs_priv *priv,
524 void (*done)(struct usbhs_pkt *pkt),
525 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map))
527 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
528 struct device *dev = usbhs_priv_to_dev(priv);
529 struct usbhs_pipe *pipe;
533 dev_err(dev, "no done function\n");
540 * driver needs good allocator.
542 * find first free buffer area (BULK, ISOC)
543 * (DCP, INT area is fixed)
545 * buffer number 0 - 3 have been reserved for DCP
549 info->bufnmb_last = 4;
550 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
551 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
554 usbhsp_flags_init(pipe);
556 pipe->mod_private = NULL;
557 INIT_LIST_HEAD(&pipe->list);
559 /* pipe force init */
560 usbhs_pipe_clear(pipe);
564 info->dma_map_ctrl = dma_map_ctrl;
567 struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
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);
576 u16 pipecfg, pipebuf;
578 pipe = usbhsp_get_pipe(priv, endpoint_type);
580 dev_err(dev, "can't get pipe (%s)\n",
581 usbhsp_pipe_name[endpoint_type]);
585 INIT_LIST_HEAD(&pipe->list);
587 usbhs_pipe_disable(pipe);
589 /* make sure pipe is not busy */
590 ret = usbhsp_pipe_barrier(pipe);
592 dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
596 pipecfg = usbhsp_setup_pipecfg(pipe, is_host, dir_in);
597 pipebuf = usbhsp_setup_pipebuff(pipe);
599 usbhsp_pipe_select(pipe);
600 usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
601 usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
603 usbhs_pipe_clear_sequence(pipe);
605 dev_dbg(dev, "enable pipe %d : %s (%s)\n",
606 usbhs_pipe_number(pipe),
607 usbhsp_pipe_name[endpoint_type],
608 usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
611 * epnum / maxp are still not set to this pipe.
612 * call usbhs_pipe_config_update() after this function !!
618 void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo)
621 pipe->fifo->pipe = NULL;
633 struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
635 struct usbhs_pipe *pipe;
637 pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
641 INIT_LIST_HEAD(&pipe->list);
644 * call usbhs_pipe_config_update() after this function !!
650 void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
652 WARN_ON(!usbhs_pipe_is_dcp(pipe));
654 usbhs_pipe_enable(pipe);
655 usbhsp_pipectrl_set(pipe, CCPL, CCPL);
659 * pipe module function
661 int usbhs_pipe_probe(struct usbhs_priv *priv)
663 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
664 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);
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");
676 info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
678 dev_err(dev, "Could not allocate pipe\n");
682 info->size = pipe_size;
687 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
690 usbhs_pipe_type(pipe) =
691 pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
693 dev_dbg(dev, "pipe %x\t: %s\n",
694 i, usbhsp_pipe_name[pipe_type[i]]);
700 void usbhs_pipe_remove(struct usbhs_priv *priv)
702 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);