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>
25 static void usbhsf_send_terminator(struct usbhs_pipe *pipe)
27 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
29 usbhs_bset(priv, CFIFOCTR, BVAL, BVAL);
32 static int usbhsf_fifo_barrier(struct usbhs_priv *priv)
37 /* The FIFO port is accessible */
38 if (usbhs_read(priv, CFIFOCTR) & FRDY)
47 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe)
49 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
51 if (!usbhs_pipe_is_dcp(pipe))
52 usbhsf_fifo_barrier(priv);
54 usbhs_write(priv, CFIFOCTR, BCLR);
57 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv)
59 return usbhs_read(priv, CFIFOCTR) & DTLN_MASK;
62 static int usbhsf_fifo_select(struct usbhs_pipe *pipe, int write)
64 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
65 struct device *dev = usbhs_priv_to_dev(priv);
67 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
68 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
70 if (usbhs_pipe_is_dcp(pipe))
71 base |= (1 == write) << 5; /* ISEL */
73 /* "base" will be used below */
74 usbhs_write(priv, CFIFOSEL, base | MBW_32);
76 /* check ISEL and CURPIPE value */
78 if (base == (mask & usbhs_read(priv, CFIFOSEL)))
83 dev_err(dev, "fifo select error\n");
91 int usbhs_fifo_prepare_write(struct usbhs_pipe *pipe)
93 return usbhsf_fifo_select(pipe, 1);
96 int usbhs_fifo_write(struct usbhs_pipe *pipe, u8 *buf, int len)
98 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
99 void __iomem *addr = priv->base + CFIFO;
100 int maxp = usbhs_pipe_get_maxpacket(pipe);
104 ret = usbhs_pipe_is_accessible(pipe);
108 ret = usbhsf_fifo_select(pipe, 1);
112 ret = usbhsf_fifo_barrier(priv);
116 len = min(len, maxp);
125 !((unsigned long)buf & 0x03)) {
126 iowrite32_rep(addr, buf, len / 4);
128 buf += total_len - len;
131 /* the rest operation */
132 for (i = 0; i < len; i++)
133 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
135 if (total_len < maxp)
136 usbhsf_send_terminator(pipe);
141 int usbhs_fifo_prepare_read(struct usbhs_pipe *pipe)
146 * select pipe and enable it to prepare packet receive
148 ret = usbhsf_fifo_select(pipe, 0);
152 usbhs_pipe_enable(pipe);
157 int usbhs_fifo_read(struct usbhs_pipe *pipe, u8 *buf, int len)
159 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
160 void __iomem *addr = priv->base + CFIFO;
166 ret = usbhsf_fifo_select(pipe, 0);
170 ret = usbhsf_fifo_barrier(priv);
174 rcv_len = usbhsf_fifo_rcv_len(priv);
177 * Buffer clear if Zero-Length packet
180 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
183 usbhsf_fifo_clear(pipe);
187 len = min(rcv_len, len);
196 !((unsigned long)buf & 0x03)) {
197 ioread32_rep(addr, buf, len / 4);
199 buf += rcv_len - len;
202 /* the rest operation */
203 for (i = 0; i < len; i++) {
205 data = ioread32(addr);
207 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;