blob: b5031e3a156989b253eaf0569a6daf443f733783 [file] [log] [blame]
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +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>
18#include <linux/io.h>
19#include "./common.h"
20#include "./pipe.h"
21
22/*
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090023 * packet info function
24 */
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090025void usbhs_pkt_init(struct usbhs_pkt *pkt)
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090026{
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090027 INIT_LIST_HEAD(&pkt->node);
28}
29
Kuninori Morimoto659d4952011-06-06 14:18:23 +090030void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
31 void *buf, int len, int zero)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090032{
33 list_del_init(&pkt->node);
34 list_add_tail(&pkt->node, &pipe->list);
35
Kuninori Morimoto659d4952011-06-06 14:18:23 +090036 pkt->pipe = pipe;
37 pkt->buf = buf;
38 pkt->length = len;
39 pkt->zero = zero;
40 pkt->actual = 0;
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090041}
42
43void usbhs_pkt_pop(struct usbhs_pkt *pkt)
44{
45 list_del_init(&pkt->node);
46}
47
48struct usbhs_pkt *usbhs_pkt_get(struct usbhs_pipe *pipe)
49{
50 if (list_empty(&pipe->list))
51 return NULL;
52
53 return list_entry(pipe->list.next, struct usbhs_pkt, node);
54}
55
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090056/*
Kuninori Morimoto659d4952011-06-06 14:18:23 +090057 * irq enable/disable function
58 */
59#define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
60#define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
61#define usbhsf_irq_callback_ctrl(pipe, status, enable) \
62 ({ \
63 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
64 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
65 u16 status = (1 << usbhs_pipe_number(pipe)); \
66 if (!mod) \
67 return; \
68 if (enable) \
69 mod->irq_##status |= status; \
70 else \
71 mod->irq_##status &= ~status; \
72 usbhs_irq_callback_update(priv, mod); \
73 })
74
75static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
76{
77 /*
78 * And DCP pipe can NOT use "ready interrupt" for "send"
79 * it should use "empty" interrupt.
80 * see
81 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
82 *
83 * on the other hand, normal pipe can use "ready interrupt" for "send"
84 * even though it is single/double buffer
85 */
86 if (usbhs_pipe_is_dcp(pipe))
87 usbhsf_irq_empty_ctrl(pipe, enable);
88 else
89 usbhsf_irq_ready_ctrl(pipe, enable);
90}
91
92static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
93{
94 usbhsf_irq_ready_ctrl(pipe, enable);
95}
96
97/*
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090098 * FIFO ctrl
99 */
100static void usbhsf_send_terminator(struct usbhs_pipe *pipe)
101{
102 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
103
104 usbhs_bset(priv, CFIFOCTR, BVAL, BVAL);
105}
106
107static int usbhsf_fifo_barrier(struct usbhs_priv *priv)
108{
109 int timeout = 1024;
110
111 do {
112 /* The FIFO port is accessible */
113 if (usbhs_read(priv, CFIFOCTR) & FRDY)
114 return 0;
115
116 udelay(10);
117 } while (timeout--);
118
119 return -EBUSY;
120}
121
122static void usbhsf_fifo_clear(struct usbhs_pipe *pipe)
123{
124 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
125
126 if (!usbhs_pipe_is_dcp(pipe))
127 usbhsf_fifo_barrier(priv);
128
129 usbhs_write(priv, CFIFOCTR, BCLR);
130}
131
132static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv)
133{
134 return usbhs_read(priv, CFIFOCTR) & DTLN_MASK;
135}
136
137static int usbhsf_fifo_select(struct usbhs_pipe *pipe, int write)
138{
139 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
140 struct device *dev = usbhs_priv_to_dev(priv);
141 int timeout = 1024;
142 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
143 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
144
145 if (usbhs_pipe_is_dcp(pipe))
146 base |= (1 == write) << 5; /* ISEL */
147
148 /* "base" will be used below */
149 usbhs_write(priv, CFIFOSEL, base | MBW_32);
150
151 /* check ISEL and CURPIPE value */
152 while (timeout--) {
153 if (base == (mask & usbhs_read(priv, CFIFOSEL)))
154 return 0;
155 udelay(10);
156 }
157
158 dev_err(dev, "fifo select error\n");
159
160 return -EIO;
161}
162
163/*
164 * PIO fifo functions
165 */
166int usbhs_fifo_prepare_write(struct usbhs_pipe *pipe)
167{
168 return usbhsf_fifo_select(pipe, 1);
169}
170
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900171int usbhs_fifo_write(struct usbhs_pkt *pkt)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900172{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900173 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900174 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900175 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900176 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900177 void __iomem *addr = priv->base + CFIFO;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900178 u8 *buf;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900179 int maxp = usbhs_pipe_get_maxpacket(pipe);
180 int total_len;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900181 int i, ret, len;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900182 int is_short, is_done;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900183
184 ret = usbhs_pipe_is_accessible(pipe);
185 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900186 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900187
188 ret = usbhsf_fifo_select(pipe, 1);
189 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900190 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900191
192 ret = usbhsf_fifo_barrier(priv);
193 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900194 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900195
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900196 buf = pkt->buf + pkt->actual;
197 len = pkt->length - pkt->actual;
198 len = min(len, maxp);
199 total_len = len;
200 is_short = total_len < maxp;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900201
202 /*
203 * FIXME
204 *
205 * 32-bit access only
206 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900207 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900208 iowrite32_rep(addr, buf, len / 4);
209 len %= 4;
210 buf += total_len - len;
211 }
212
213 /* the rest operation */
214 for (i = 0; i < len; i++)
215 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
216
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900217 /*
218 * variable update
219 */
220 pkt->actual += total_len;
221
222 if (pkt->actual < pkt->length)
223 is_done = 0; /* there are remainder data */
224 else if (is_short)
225 is_done = 1; /* short packet */
226 else
227 is_done = !pkt->zero; /* send zero packet ? */
228
229 /*
230 * pipe/irq handling
231 */
232 if (is_short)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900233 usbhsf_send_terminator(pipe);
234
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900235 usbhsf_tx_irq_ctrl(pipe, !is_done);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900236 usbhs_pipe_enable(pipe);
237
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900238 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
239 usbhs_pipe_number(pipe),
240 pkt->length, pkt->actual, is_done, pkt->zero);
241
242 /*
243 * Transmission end
244 */
245 if (is_done) {
246 if (usbhs_pipe_is_dcp(pipe))
247 usbhs_dcp_control_transfer_done(pipe);
248
249 if (info->tx_done)
250 info->tx_done(pkt);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900251 }
252
253 return 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900254
255usbhs_fifo_write_busy:
256 /*
257 * pipe is busy.
258 * retry in interrupt
259 */
260 usbhsf_tx_irq_ctrl(pipe, 1);
261
262 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900263}
264
265int usbhs_fifo_prepare_read(struct usbhs_pipe *pipe)
266{
267 int ret;
268
269 /*
270 * select pipe and enable it to prepare packet receive
271 */
272 ret = usbhsf_fifo_select(pipe, 0);
273 if (ret < 0)
274 return ret;
275
276 usbhs_pipe_enable(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900277 usbhsf_rx_irq_ctrl(pipe, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900278
279 return ret;
280}
281
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900282int usbhs_fifo_read(struct usbhs_pkt *pkt)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900283{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900284 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900285 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900286 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900287 void __iomem *addr = priv->base + CFIFO;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900288 u8 *buf;
289 u32 data = 0;
290 int maxp = usbhs_pipe_get_maxpacket(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900291 int rcv_len, len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900292 int i, ret;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900293 int total_len = 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900294 int is_done = 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900295
296 ret = usbhsf_fifo_select(pipe, 0);
297 if (ret < 0)
298 return ret;
299
300 ret = usbhsf_fifo_barrier(priv);
301 if (ret < 0)
302 return ret;
303
304 rcv_len = usbhsf_fifo_rcv_len(priv);
305
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900306 buf = pkt->buf + pkt->actual;
307 len = pkt->length - pkt->actual;
308 len = min(len, rcv_len);
309 total_len = len;
310
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900311 /*
312 * Buffer clear if Zero-Length packet
313 *
314 * see
315 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
316 */
317 if (0 == rcv_len) {
318 usbhsf_fifo_clear(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900319 goto usbhs_fifo_read_end;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900320 }
321
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900322 /*
323 * FIXME
324 *
325 * 32-bit access only
326 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900327 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900328 ioread32_rep(addr, buf, len / 4);
329 len %= 4;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900330 buf += total_len - len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900331 }
332
333 /* the rest operation */
334 for (i = 0; i < len; i++) {
335 if (!(i & 0x03))
336 data = ioread32(addr);
337
338 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
339 }
340
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900341 pkt->actual += total_len;
342
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900343usbhs_fifo_read_end:
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900344 if ((pkt->actual == pkt->length) || /* receive all data */
345 (total_len < maxp)) /* short packet */
346 is_done = 1;
347
348 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
349 usbhs_pipe_number(pipe),
350 pkt->length, pkt->actual, is_done, pkt->zero);
351
352 if (is_done) {
353 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
354
355 usbhsf_rx_irq_ctrl(pipe, 0);
356 usbhs_pipe_disable(pipe);
357
358 if (info->rx_done)
359 info->rx_done(pkt);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900360 }
361
362 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900363}