Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 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/io.h> |
| 19 | #include "./common.h" |
| 20 | #include "./pipe.h" |
| 21 | |
| 22 | /* |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 23 | * packet info function |
| 24 | */ |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 25 | void usbhs_pkt_init(struct usbhs_pkt *pkt) |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 26 | { |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 27 | INIT_LIST_HEAD(&pkt->node); |
| 28 | } |
| 29 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 30 | void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt, |
| 31 | void *buf, int len, int zero) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 32 | { |
| 33 | list_del_init(&pkt->node); |
| 34 | list_add_tail(&pkt->node, &pipe->list); |
| 35 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 36 | pkt->pipe = pipe; |
| 37 | pkt->buf = buf; |
| 38 | pkt->length = len; |
| 39 | pkt->zero = zero; |
| 40 | pkt->actual = 0; |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void usbhs_pkt_pop(struct usbhs_pkt *pkt) |
| 44 | { |
| 45 | list_del_init(&pkt->node); |
| 46 | } |
| 47 | |
| 48 | struct 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 Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 56 | /* |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 57 | * 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 | |
| 75 | static 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 | |
| 92 | static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable) |
| 93 | { |
| 94 | usbhsf_irq_ready_ctrl(pipe, enable); |
| 95 | } |
| 96 | |
| 97 | /* |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 98 | * FIFO ctrl |
| 99 | */ |
| 100 | static 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 | |
| 107 | static 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 | |
| 122 | static 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 | |
| 132 | static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv) |
| 133 | { |
| 134 | return usbhs_read(priv, CFIFOCTR) & DTLN_MASK; |
| 135 | } |
| 136 | |
| 137 | static 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 | */ |
| 166 | int usbhs_fifo_prepare_write(struct usbhs_pipe *pipe) |
| 167 | { |
| 168 | return usbhsf_fifo_select(pipe, 1); |
| 169 | } |
| 170 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 171 | int usbhs_fifo_write(struct usbhs_pkt *pkt) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 172 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 173 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 174 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 175 | struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 176 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 177 | void __iomem *addr = priv->base + CFIFO; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 178 | u8 *buf; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 179 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
| 180 | int total_len; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 181 | int i, ret, len; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 182 | int is_short, is_done; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 183 | |
| 184 | ret = usbhs_pipe_is_accessible(pipe); |
| 185 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 186 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 187 | |
| 188 | ret = usbhsf_fifo_select(pipe, 1); |
| 189 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 190 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 191 | |
| 192 | ret = usbhsf_fifo_barrier(priv); |
| 193 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 194 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 195 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 196 | 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 Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * FIXME |
| 204 | * |
| 205 | * 32-bit access only |
| 206 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 207 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 208 | 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 Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 217 | /* |
| 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 Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 233 | usbhsf_send_terminator(pipe); |
| 234 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 235 | usbhsf_tx_irq_ctrl(pipe, !is_done); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 236 | usbhs_pipe_enable(pipe); |
| 237 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 238 | 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 Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return 0; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 254 | |
| 255 | usbhs_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 Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | int 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 Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 277 | usbhsf_rx_irq_ctrl(pipe, 1); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 282 | int usbhs_fifo_read(struct usbhs_pkt *pkt) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 283 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 284 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 285 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 286 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 287 | void __iomem *addr = priv->base + CFIFO; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 288 | u8 *buf; |
| 289 | u32 data = 0; |
| 290 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 291 | int rcv_len, len; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 292 | int i, ret; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 293 | int total_len = 0; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 294 | int is_done = 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 295 | |
| 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 Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 306 | buf = pkt->buf + pkt->actual; |
| 307 | len = pkt->length - pkt->actual; |
| 308 | len = min(len, rcv_len); |
| 309 | total_len = len; |
| 310 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 311 | /* |
| 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 Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 319 | goto usbhs_fifo_read_end; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 320 | } |
| 321 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 322 | /* |
| 323 | * FIXME |
| 324 | * |
| 325 | * 32-bit access only |
| 326 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 327 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 328 | ioread32_rep(addr, buf, len / 4); |
| 329 | len %= 4; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 330 | buf += total_len - len; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 331 | } |
| 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 Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 341 | pkt->actual += total_len; |
| 342 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 343 | usbhs_fifo_read_end: |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame^] | 344 | 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 Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 363 | } |