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 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 22 | #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo)) |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 23 | #define usbhsf_get_d0fifo(p) (&((p)->fifo_info.d0fifo)) |
| 24 | #define usbhsf_get_d1fifo(p) (&((p)->fifo_info.d1fifo)) |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 25 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 26 | #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */ |
| 27 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 28 | /* |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 29 | * packet initialize |
| 30 | */ |
| 31 | void usbhs_pkt_init(struct usbhs_pkt *pkt) |
| 32 | { |
| 33 | pkt->dma = DMA_ADDR_INVALID; |
| 34 | INIT_LIST_HEAD(&pkt->node); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * packet control function |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 39 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 40 | static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 41 | { |
| 42 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe); |
| 43 | struct device *dev = usbhs_priv_to_dev(priv); |
| 44 | |
| 45 | dev_err(dev, "null handler\n"); |
| 46 | |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | |
| 50 | static struct usbhs_pkt_handle usbhsf_null_handler = { |
| 51 | .prepare = usbhsf_null_handle, |
| 52 | .try_run = usbhsf_null_handle, |
| 53 | }; |
| 54 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 55 | void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt, |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 56 | struct usbhs_pkt_handle *handler, |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 57 | void *buf, int len, int zero) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 58 | { |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 59 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 60 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 61 | unsigned long flags; |
| 62 | |
| 63 | /******************** spin lock ********************/ |
| 64 | usbhs_lock(priv, flags); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 65 | |
| 66 | if (!handler) { |
| 67 | dev_err(dev, "no handler function\n"); |
| 68 | handler = &usbhsf_null_handler; |
| 69 | } |
| 70 | |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 71 | list_del_init(&pkt->node); |
| 72 | list_add_tail(&pkt->node, &pipe->list); |
| 73 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 74 | pkt->pipe = pipe; |
| 75 | pkt->buf = buf; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 76 | pkt->handler = handler; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 77 | pkt->length = len; |
| 78 | pkt->zero = zero; |
| 79 | pkt->actual = 0; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 80 | |
| 81 | usbhs_unlock(priv, flags); |
| 82 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 83 | |
| 84 | usbhs_pkt_start(pipe); |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 87 | static void __usbhsf_pkt_del(struct usbhs_pkt *pkt) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 88 | { |
| 89 | list_del_init(&pkt->node); |
| 90 | } |
| 91 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 92 | static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 93 | { |
| 94 | if (list_empty(&pipe->list)) |
| 95 | return NULL; |
| 96 | |
| 97 | return list_entry(pipe->list.next, struct usbhs_pkt, node); |
| 98 | } |
| 99 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 100 | struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt) |
| 101 | { |
| 102 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 103 | unsigned long flags; |
| 104 | |
| 105 | /******************** spin lock ********************/ |
| 106 | usbhs_lock(priv, flags); |
| 107 | |
| 108 | if (!pkt) |
| 109 | pkt = __usbhsf_pkt_get(pipe); |
| 110 | |
| 111 | if (pkt) |
| 112 | __usbhsf_pkt_del(pkt); |
| 113 | |
| 114 | usbhs_unlock(priv, flags); |
| 115 | /******************** spin unlock ******************/ |
| 116 | |
| 117 | return pkt; |
| 118 | } |
| 119 | |
| 120 | int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type) |
| 121 | { |
| 122 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 123 | struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); |
| 124 | struct usbhs_pkt *pkt; |
| 125 | struct device *dev = usbhs_priv_to_dev(priv); |
| 126 | int (*func)(struct usbhs_pkt *pkt, int *is_done); |
| 127 | unsigned long flags; |
| 128 | int ret = 0; |
| 129 | int is_done = 0; |
| 130 | |
| 131 | /******************** spin lock ********************/ |
| 132 | usbhs_lock(priv, flags); |
| 133 | |
| 134 | pkt = __usbhsf_pkt_get(pipe); |
| 135 | if (!pkt) |
| 136 | goto __usbhs_pkt_handler_end; |
| 137 | |
| 138 | switch (type) { |
| 139 | case USBHSF_PKT_PREPARE: |
| 140 | func = pkt->handler->prepare; |
| 141 | break; |
| 142 | case USBHSF_PKT_TRY_RUN: |
| 143 | func = pkt->handler->try_run; |
| 144 | break; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 145 | case USBHSF_PKT_DMA_DONE: |
| 146 | func = pkt->handler->dma_done; |
| 147 | break; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 148 | default: |
| 149 | dev_err(dev, "unknown pkt hander\n"); |
| 150 | goto __usbhs_pkt_handler_end; |
| 151 | } |
| 152 | |
| 153 | ret = func(pkt, &is_done); |
| 154 | |
| 155 | if (is_done) |
| 156 | __usbhsf_pkt_del(pkt); |
| 157 | |
| 158 | __usbhs_pkt_handler_end: |
| 159 | usbhs_unlock(priv, flags); |
| 160 | /******************** spin unlock ******************/ |
| 161 | |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 162 | if (is_done) { |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 163 | info->done(pkt); |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 164 | usbhs_pkt_start(pipe); |
| 165 | } |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 170 | /* |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 171 | * irq enable/disable function |
| 172 | */ |
| 173 | #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e) |
| 174 | #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e) |
| 175 | #define usbhsf_irq_callback_ctrl(pipe, status, enable) \ |
| 176 | ({ \ |
| 177 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \ |
| 178 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); \ |
| 179 | u16 status = (1 << usbhs_pipe_number(pipe)); \ |
| 180 | if (!mod) \ |
| 181 | return; \ |
| 182 | if (enable) \ |
| 183 | mod->irq_##status |= status; \ |
| 184 | else \ |
| 185 | mod->irq_##status &= ~status; \ |
| 186 | usbhs_irq_callback_update(priv, mod); \ |
| 187 | }) |
| 188 | |
| 189 | static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable) |
| 190 | { |
| 191 | /* |
| 192 | * And DCP pipe can NOT use "ready interrupt" for "send" |
| 193 | * it should use "empty" interrupt. |
| 194 | * see |
| 195 | * "Operation" - "Interrupt Function" - "BRDY Interrupt" |
| 196 | * |
| 197 | * on the other hand, normal pipe can use "ready interrupt" for "send" |
| 198 | * even though it is single/double buffer |
| 199 | */ |
| 200 | if (usbhs_pipe_is_dcp(pipe)) |
| 201 | usbhsf_irq_empty_ctrl(pipe, enable); |
| 202 | else |
| 203 | usbhsf_irq_ready_ctrl(pipe, enable); |
| 204 | } |
| 205 | |
| 206 | static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable) |
| 207 | { |
| 208 | usbhsf_irq_ready_ctrl(pipe, enable); |
| 209 | } |
| 210 | |
| 211 | /* |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 212 | * FIFO ctrl |
| 213 | */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 214 | static void usbhsf_send_terminator(struct usbhs_pipe *pipe, |
| 215 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 216 | { |
| 217 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 218 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 219 | usbhs_bset(priv, fifo->ctr, BVAL, BVAL); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 220 | } |
| 221 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 222 | static int usbhsf_fifo_barrier(struct usbhs_priv *priv, |
| 223 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 224 | { |
| 225 | int timeout = 1024; |
| 226 | |
| 227 | do { |
| 228 | /* The FIFO port is accessible */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 229 | if (usbhs_read(priv, fifo->ctr) & FRDY) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 230 | return 0; |
| 231 | |
| 232 | udelay(10); |
| 233 | } while (timeout--); |
| 234 | |
| 235 | return -EBUSY; |
| 236 | } |
| 237 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 238 | static void usbhsf_fifo_clear(struct usbhs_pipe *pipe, |
| 239 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 240 | { |
| 241 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 242 | |
| 243 | if (!usbhs_pipe_is_dcp(pipe)) |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 244 | usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 245 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 246 | usbhs_write(priv, fifo->ctr, BCLR); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 247 | } |
| 248 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 249 | static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv, |
| 250 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 251 | { |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 252 | return usbhs_read(priv, fifo->ctr) & DTLN_MASK; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 253 | } |
| 254 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 255 | static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe, |
| 256 | struct usbhs_fifo *fifo) |
| 257 | { |
| 258 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 259 | |
| 260 | usbhs_pipe_select_fifo(pipe, NULL); |
| 261 | usbhs_write(priv, fifo->sel, 0); |
| 262 | } |
| 263 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 264 | static int usbhsf_fifo_select(struct usbhs_pipe *pipe, |
| 265 | struct usbhs_fifo *fifo, |
| 266 | int write) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 267 | { |
| 268 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 269 | struct device *dev = usbhs_priv_to_dev(priv); |
| 270 | int timeout = 1024; |
| 271 | u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */ |
| 272 | u16 base = usbhs_pipe_number(pipe); /* CURPIPE */ |
| 273 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 274 | if (usbhs_pipe_is_busy(pipe) || |
| 275 | usbhsf_fifo_is_busy(fifo)) |
| 276 | return -EBUSY; |
| 277 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 278 | if (usbhs_pipe_is_dcp(pipe)) |
| 279 | base |= (1 == write) << 5; /* ISEL */ |
| 280 | |
| 281 | /* "base" will be used below */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 282 | usbhs_write(priv, fifo->sel, base | MBW_32); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 283 | |
| 284 | /* check ISEL and CURPIPE value */ |
| 285 | while (timeout--) { |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 286 | if (base == (mask & usbhs_read(priv, fifo->sel))) { |
| 287 | usbhs_pipe_select_fifo(pipe, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 288 | return 0; |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 289 | } |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 290 | udelay(10); |
| 291 | } |
| 292 | |
| 293 | dev_err(dev, "fifo select error\n"); |
| 294 | |
| 295 | return -EIO; |
| 296 | } |
| 297 | |
| 298 | /* |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 299 | * PIO push handler |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 300 | */ |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 301 | static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 302 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 303 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 304 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 305 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 306 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 307 | void __iomem *addr = priv->base + fifo->port; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 308 | u8 *buf; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 309 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
| 310 | int total_len; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 311 | int i, ret, len; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 312 | int is_short; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 313 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 314 | ret = usbhsf_fifo_select(pipe, fifo, 1); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 315 | if (ret < 0) |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 316 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 317 | |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 318 | ret = usbhs_pipe_is_accessible(pipe); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 319 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 320 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 321 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 322 | ret = usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 323 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 324 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 325 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 326 | buf = pkt->buf + pkt->actual; |
| 327 | len = pkt->length - pkt->actual; |
| 328 | len = min(len, maxp); |
| 329 | total_len = len; |
| 330 | is_short = total_len < maxp; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * FIXME |
| 334 | * |
| 335 | * 32-bit access only |
| 336 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 337 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 338 | iowrite32_rep(addr, buf, len / 4); |
| 339 | len %= 4; |
| 340 | buf += total_len - len; |
| 341 | } |
| 342 | |
| 343 | /* the rest operation */ |
| 344 | for (i = 0; i < len; i++) |
| 345 | iowrite8(buf[i], addr + (0x03 - (i & 0x03))); |
| 346 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 347 | /* |
| 348 | * variable update |
| 349 | */ |
| 350 | pkt->actual += total_len; |
| 351 | |
| 352 | if (pkt->actual < pkt->length) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 353 | *is_done = 0; /* there are remainder data */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 354 | else if (is_short) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 355 | *is_done = 1; /* short packet */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 356 | else |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 357 | *is_done = !pkt->zero; /* send zero packet ? */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 358 | |
| 359 | /* |
| 360 | * pipe/irq handling |
| 361 | */ |
| 362 | if (is_short) |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 363 | usbhsf_send_terminator(pipe, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 364 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 365 | usbhsf_tx_irq_ctrl(pipe, !*is_done); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 366 | usbhs_pipe_enable(pipe); |
| 367 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 368 | dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n", |
| 369 | usbhs_pipe_number(pipe), |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 370 | pkt->length, pkt->actual, *is_done, pkt->zero); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * Transmission end |
| 374 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 375 | if (*is_done) { |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 376 | if (usbhs_pipe_is_dcp(pipe)) |
| 377 | usbhs_dcp_control_transfer_done(pipe); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 378 | } |
| 379 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 380 | usbhsf_fifo_unselect(pipe, fifo); |
| 381 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 382 | return 0; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 383 | |
| 384 | usbhs_fifo_write_busy: |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 385 | usbhsf_fifo_unselect(pipe, fifo); |
| 386 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 387 | /* |
| 388 | * pipe is busy. |
| 389 | * retry in interrupt |
| 390 | */ |
| 391 | usbhsf_tx_irq_ctrl(pipe, 1); |
| 392 | |
| 393 | return ret; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 394 | } |
| 395 | |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 396 | struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = { |
| 397 | .prepare = usbhsf_pio_try_push, |
| 398 | .try_run = usbhsf_pio_try_push, |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 399 | }; |
| 400 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 401 | /* |
| 402 | * PIO pop handler |
| 403 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 404 | static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 405 | { |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 406 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 407 | |
| 408 | if (usbhs_pipe_is_busy(pipe)) |
| 409 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 410 | |
| 411 | /* |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 412 | * pipe enable to prepare packet receive |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 413 | */ |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 414 | |
| 415 | usbhs_pipe_enable(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 416 | usbhsf_rx_irq_ctrl(pipe, 1); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 417 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 418 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 419 | } |
| 420 | |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 421 | static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 422 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 423 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 424 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 425 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 426 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 427 | void __iomem *addr = priv->base + fifo->port; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 428 | u8 *buf; |
| 429 | u32 data = 0; |
| 430 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 431 | int rcv_len, len; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 432 | int i, ret; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 433 | int total_len = 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 434 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 435 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 436 | if (ret < 0) |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 437 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 438 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 439 | ret = usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 440 | if (ret < 0) |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 441 | goto usbhs_fifo_read_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 442 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 443 | rcv_len = usbhsf_fifo_rcv_len(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 444 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 445 | buf = pkt->buf + pkt->actual; |
| 446 | len = pkt->length - pkt->actual; |
| 447 | len = min(len, rcv_len); |
| 448 | total_len = len; |
| 449 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 450 | /* |
| 451 | * Buffer clear if Zero-Length packet |
| 452 | * |
| 453 | * see |
| 454 | * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function" |
| 455 | */ |
| 456 | if (0 == rcv_len) { |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 457 | usbhsf_fifo_clear(pipe, fifo); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 458 | goto usbhs_fifo_read_end; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 459 | } |
| 460 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 461 | /* |
| 462 | * FIXME |
| 463 | * |
| 464 | * 32-bit access only |
| 465 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 466 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 467 | ioread32_rep(addr, buf, len / 4); |
| 468 | len %= 4; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 469 | buf += total_len - len; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* the rest operation */ |
| 473 | for (i = 0; i < len; i++) { |
| 474 | if (!(i & 0x03)) |
| 475 | data = ioread32(addr); |
| 476 | |
| 477 | buf[i] = (data >> ((i & 0x03) * 8)) & 0xff; |
| 478 | } |
| 479 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 480 | pkt->actual += total_len; |
| 481 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 482 | usbhs_fifo_read_end: |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 483 | if ((pkt->actual == pkt->length) || /* receive all data */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 484 | (total_len < maxp)) { /* short packet */ |
| 485 | *is_done = 1; |
| 486 | usbhsf_rx_irq_ctrl(pipe, 0); |
| 487 | usbhs_pipe_disable(pipe); |
| 488 | } |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 489 | |
| 490 | dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n", |
| 491 | usbhs_pipe_number(pipe), |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 492 | pkt->length, pkt->actual, *is_done, pkt->zero); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 493 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 494 | usbhs_fifo_read_busy: |
| 495 | usbhsf_fifo_unselect(pipe, fifo); |
| 496 | |
| 497 | return ret; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 498 | } |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 499 | |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 500 | struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = { |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 501 | .prepare = usbhsf_prepare_pop, |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 502 | .try_run = usbhsf_pio_try_pop, |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | /* |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 506 | * DCP ctrol statge handler |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 507 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 508 | static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 509 | { |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 510 | usbhs_dcp_control_transfer_done(pkt->pipe); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 511 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 512 | *is_done = 1; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = { |
| 518 | .prepare = usbhsf_ctrl_stage_end, |
| 519 | .try_run = usbhsf_ctrl_stage_end, |
| 520 | }; |
| 521 | |
| 522 | /* |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 523 | * DMA fifo functions |
| 524 | */ |
| 525 | static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo, |
| 526 | struct usbhs_pkt *pkt) |
| 527 | { |
| 528 | if (&usbhs_fifo_dma_push_handler == pkt->handler) |
| 529 | return fifo->tx_chan; |
| 530 | |
| 531 | if (&usbhs_fifo_dma_pop_handler == pkt->handler) |
| 532 | return fifo->rx_chan; |
| 533 | |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv, |
| 538 | struct usbhs_pkt *pkt) |
| 539 | { |
| 540 | struct usbhs_fifo *fifo; |
| 541 | |
| 542 | /* DMA :: D0FIFO */ |
| 543 | fifo = usbhsf_get_d0fifo(priv); |
| 544 | if (usbhsf_dma_chan_get(fifo, pkt) && |
| 545 | !usbhsf_fifo_is_busy(fifo)) |
| 546 | return fifo; |
| 547 | |
| 548 | /* DMA :: D1FIFO */ |
| 549 | fifo = usbhsf_get_d1fifo(priv); |
| 550 | if (usbhsf_dma_chan_get(fifo, pkt) && |
| 551 | !usbhsf_fifo_is_busy(fifo)) |
| 552 | return fifo; |
| 553 | |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE) |
| 558 | #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0) |
| 559 | static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe, |
| 560 | struct usbhs_fifo *fifo, |
| 561 | u16 dreqe) |
| 562 | { |
| 563 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 564 | |
| 565 | usbhs_bset(priv, fifo->sel, DREQE, dreqe); |
| 566 | } |
| 567 | |
| 568 | #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1) |
| 569 | #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0) |
| 570 | static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map) |
| 571 | { |
| 572 | struct usbhs_pipe *pipe = pkt->pipe; |
| 573 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 574 | struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); |
| 575 | |
| 576 | return info->dma_map_ctrl(pkt, map); |
| 577 | } |
| 578 | |
| 579 | static void usbhsf_dma_complete(void *arg); |
| 580 | static void usbhsf_dma_prepare_tasklet(unsigned long data) |
| 581 | { |
| 582 | struct usbhs_pkt *pkt = (struct usbhs_pkt *)data; |
| 583 | struct usbhs_pipe *pipe = pkt->pipe; |
| 584 | struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe); |
| 585 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 586 | struct scatterlist sg; |
| 587 | struct dma_async_tx_descriptor *desc; |
| 588 | struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt); |
| 589 | struct device *dev = usbhs_priv_to_dev(priv); |
| 590 | enum dma_data_direction dir; |
| 591 | dma_cookie_t cookie; |
| 592 | |
| 593 | dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 594 | |
| 595 | sg_init_table(&sg, 1); |
| 596 | sg_set_page(&sg, virt_to_page(pkt->dma), |
| 597 | pkt->length, offset_in_page(pkt->dma)); |
| 598 | sg_dma_address(&sg) = pkt->dma + pkt->actual; |
| 599 | sg_dma_len(&sg) = pkt->trans; |
| 600 | |
| 601 | desc = chan->device->device_prep_slave_sg(chan, &sg, 1, dir, |
| 602 | DMA_PREP_INTERRUPT | |
| 603 | DMA_CTRL_ACK); |
| 604 | if (!desc) |
| 605 | return; |
| 606 | |
| 607 | desc->callback = usbhsf_dma_complete; |
| 608 | desc->callback_param = pipe; |
| 609 | |
| 610 | cookie = desc->tx_submit(desc); |
| 611 | if (cookie < 0) { |
| 612 | dev_err(dev, "Failed to submit dma descriptor\n"); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | dev_dbg(dev, " %s %d (%d/ %d)\n", |
| 617 | fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero); |
| 618 | |
| 619 | usbhsf_dma_start(pipe, fifo); |
| 620 | dma_async_issue_pending(chan); |
| 621 | } |
| 622 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 623 | /* |
| 624 | * DMA push handler |
| 625 | */ |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 626 | static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done) |
| 627 | { |
| 628 | struct usbhs_pipe *pipe = pkt->pipe; |
| 629 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 630 | struct usbhs_fifo *fifo; |
| 631 | int len = pkt->length - pkt->actual; |
| 632 | int ret; |
| 633 | |
| 634 | if (usbhs_pipe_is_busy(pipe)) |
| 635 | return 0; |
| 636 | |
| 637 | /* use PIO if packet is less than pio_dma_border or pipe is DCP */ |
| 638 | if ((len < usbhs_get_dparam(priv, pio_dma_border)) || |
| 639 | usbhs_pipe_is_dcp(pipe)) |
| 640 | goto usbhsf_pio_prepare_push; |
| 641 | |
| 642 | if (len % 4) /* 32bit alignment */ |
| 643 | goto usbhsf_pio_prepare_push; |
| 644 | |
Kuninori Morimoto | 9a12d09 | 2011-07-03 17:42:19 -0700 | [diff] [blame^] | 645 | if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */ |
| 646 | goto usbhsf_pio_prepare_push; |
| 647 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 648 | /* get enable DMA fifo */ |
| 649 | fifo = usbhsf_get_dma_fifo(priv, pkt); |
| 650 | if (!fifo) |
| 651 | goto usbhsf_pio_prepare_push; |
| 652 | |
| 653 | if (usbhsf_dma_map(pkt) < 0) |
| 654 | goto usbhsf_pio_prepare_push; |
| 655 | |
| 656 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
| 657 | if (ret < 0) |
| 658 | goto usbhsf_pio_prepare_push_unmap; |
| 659 | |
| 660 | pkt->trans = len; |
| 661 | |
| 662 | tasklet_init(&fifo->tasklet, |
| 663 | usbhsf_dma_prepare_tasklet, |
| 664 | (unsigned long)pkt); |
| 665 | |
| 666 | tasklet_schedule(&fifo->tasklet); |
| 667 | |
| 668 | return 0; |
| 669 | |
| 670 | usbhsf_pio_prepare_push_unmap: |
| 671 | usbhsf_dma_unmap(pkt); |
| 672 | usbhsf_pio_prepare_push: |
| 673 | /* |
| 674 | * change handler to PIO |
| 675 | */ |
| 676 | pkt->handler = &usbhs_fifo_pio_push_handler; |
| 677 | |
| 678 | return pkt->handler->prepare(pkt, is_done); |
| 679 | } |
| 680 | |
| 681 | static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done) |
| 682 | { |
| 683 | struct usbhs_pipe *pipe = pkt->pipe; |
| 684 | |
| 685 | pkt->actual = pkt->trans; |
| 686 | |
| 687 | *is_done = !pkt->zero; /* send zero packet ? */ |
| 688 | |
| 689 | usbhsf_dma_stop(pipe, pipe->fifo); |
| 690 | usbhsf_dma_unmap(pkt); |
| 691 | usbhsf_fifo_unselect(pipe, pipe->fifo); |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = { |
| 697 | .prepare = usbhsf_dma_prepare_push, |
| 698 | .dma_done = usbhsf_dma_push_done, |
| 699 | }; |
| 700 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 701 | /* |
| 702 | * DMA pop handler |
| 703 | */ |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 704 | static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done) |
| 705 | { |
| 706 | struct usbhs_pipe *pipe = pkt->pipe; |
| 707 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 708 | struct usbhs_fifo *fifo; |
| 709 | int len, ret; |
| 710 | |
| 711 | if (usbhs_pipe_is_busy(pipe)) |
| 712 | return 0; |
| 713 | |
| 714 | if (usbhs_pipe_is_dcp(pipe)) |
| 715 | goto usbhsf_pio_prepare_pop; |
| 716 | |
| 717 | /* get enable DMA fifo */ |
| 718 | fifo = usbhsf_get_dma_fifo(priv, pkt); |
| 719 | if (!fifo) |
| 720 | goto usbhsf_pio_prepare_pop; |
| 721 | |
Kuninori Morimoto | 9a12d09 | 2011-07-03 17:42:19 -0700 | [diff] [blame^] | 722 | if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */ |
| 723 | goto usbhsf_pio_prepare_pop; |
| 724 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 725 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
| 726 | if (ret < 0) |
| 727 | goto usbhsf_pio_prepare_pop; |
| 728 | |
| 729 | /* use PIO if packet is less than pio_dma_border */ |
| 730 | len = usbhsf_fifo_rcv_len(priv, fifo); |
| 731 | len = min(pkt->length - pkt->actual, len); |
| 732 | if (len % 4) /* 32bit alignment */ |
| 733 | goto usbhsf_pio_prepare_pop_unselect; |
| 734 | |
| 735 | if (len < usbhs_get_dparam(priv, pio_dma_border)) |
| 736 | goto usbhsf_pio_prepare_pop_unselect; |
| 737 | |
| 738 | ret = usbhsf_fifo_barrier(priv, fifo); |
| 739 | if (ret < 0) |
| 740 | goto usbhsf_pio_prepare_pop_unselect; |
| 741 | |
| 742 | if (usbhsf_dma_map(pkt) < 0) |
| 743 | goto usbhsf_pio_prepare_pop_unselect; |
| 744 | |
| 745 | /* DMA */ |
| 746 | |
| 747 | /* |
| 748 | * usbhs_fifo_dma_pop_handler :: prepare |
| 749 | * enabled irq to come here. |
| 750 | * but it is no longer needed for DMA. disable it. |
| 751 | */ |
| 752 | usbhsf_rx_irq_ctrl(pipe, 0); |
| 753 | |
| 754 | pkt->trans = len; |
| 755 | |
| 756 | tasklet_init(&fifo->tasklet, |
| 757 | usbhsf_dma_prepare_tasklet, |
| 758 | (unsigned long)pkt); |
| 759 | |
| 760 | tasklet_schedule(&fifo->tasklet); |
| 761 | |
| 762 | return 0; |
| 763 | |
| 764 | usbhsf_pio_prepare_pop_unselect: |
| 765 | usbhsf_fifo_unselect(pipe, fifo); |
| 766 | usbhsf_pio_prepare_pop: |
| 767 | |
| 768 | /* |
| 769 | * change handler to PIO |
| 770 | */ |
| 771 | pkt->handler = &usbhs_fifo_pio_pop_handler; |
| 772 | |
| 773 | return pkt->handler->try_run(pkt, is_done); |
| 774 | } |
| 775 | |
| 776 | static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done) |
| 777 | { |
| 778 | struct usbhs_pipe *pipe = pkt->pipe; |
| 779 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
| 780 | |
| 781 | usbhsf_dma_stop(pipe, pipe->fifo); |
| 782 | usbhsf_dma_unmap(pkt); |
| 783 | usbhsf_fifo_unselect(pipe, pipe->fifo); |
| 784 | |
| 785 | pkt->actual += pkt->trans; |
| 786 | |
| 787 | if ((pkt->actual == pkt->length) || /* receive all data */ |
| 788 | (pkt->trans < maxp)) { /* short packet */ |
| 789 | *is_done = 1; |
| 790 | } else { |
| 791 | /* re-enable */ |
| 792 | usbhsf_prepare_pop(pkt, is_done); |
| 793 | } |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = { |
| 799 | .prepare = usbhsf_prepare_pop, |
| 800 | .try_run = usbhsf_dma_try_pop, |
| 801 | .dma_done = usbhsf_dma_pop_done |
| 802 | }; |
| 803 | |
| 804 | /* |
| 805 | * DMA setting |
| 806 | */ |
| 807 | static bool usbhsf_dma_filter(struct dma_chan *chan, void *param) |
| 808 | { |
| 809 | struct sh_dmae_slave *slave = param; |
| 810 | |
| 811 | /* |
| 812 | * FIXME |
| 813 | * |
| 814 | * usbhs doesn't recognize id = 0 as valid DMA |
| 815 | */ |
| 816 | if (0 == slave->slave_id) |
| 817 | return false; |
| 818 | |
| 819 | chan->private = slave; |
| 820 | |
| 821 | return true; |
| 822 | } |
| 823 | |
| 824 | static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo) |
| 825 | { |
| 826 | if (fifo->tx_chan) |
| 827 | dma_release_channel(fifo->tx_chan); |
| 828 | if (fifo->rx_chan) |
| 829 | dma_release_channel(fifo->rx_chan); |
| 830 | |
| 831 | fifo->tx_chan = NULL; |
| 832 | fifo->rx_chan = NULL; |
| 833 | } |
| 834 | |
| 835 | static void usbhsf_dma_init(struct usbhs_priv *priv, |
| 836 | struct usbhs_fifo *fifo) |
| 837 | { |
| 838 | struct device *dev = usbhs_priv_to_dev(priv); |
| 839 | dma_cap_mask_t mask; |
| 840 | |
| 841 | dma_cap_zero(mask); |
| 842 | dma_cap_set(DMA_SLAVE, mask); |
| 843 | fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter, |
| 844 | &fifo->tx_slave); |
| 845 | |
| 846 | dma_cap_zero(mask); |
| 847 | dma_cap_set(DMA_SLAVE, mask); |
| 848 | fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter, |
| 849 | &fifo->rx_slave); |
| 850 | |
| 851 | if (fifo->tx_chan || fifo->rx_chan) |
Kuninori Morimoto | 4ce6880 | 2011-06-21 09:33:43 +0900 | [diff] [blame] | 852 | dev_dbg(dev, "enable DMAEngine (%s%s%s)\n", |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 853 | fifo->name, |
| 854 | fifo->tx_chan ? "[TX]" : " ", |
| 855 | fifo->rx_chan ? "[RX]" : " "); |
| 856 | } |
| 857 | |
| 858 | /* |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 859 | * irq functions |
| 860 | */ |
| 861 | static int usbhsf_irq_empty(struct usbhs_priv *priv, |
| 862 | struct usbhs_irq_state *irq_state) |
| 863 | { |
| 864 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 865 | struct device *dev = usbhs_priv_to_dev(priv); |
| 866 | int i, ret; |
| 867 | |
| 868 | if (!irq_state->bempsts) { |
| 869 | dev_err(dev, "debug %s !!\n", __func__); |
| 870 | return -EIO; |
| 871 | } |
| 872 | |
| 873 | dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts); |
| 874 | |
| 875 | /* |
| 876 | * search interrupted "pipe" |
| 877 | * not "uep". |
| 878 | */ |
| 879 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 880 | if (!(irq_state->bempsts & (1 << i))) |
| 881 | continue; |
| 882 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 883 | ret = usbhs_pkt_run(pipe); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 884 | if (ret < 0) |
| 885 | dev_err(dev, "irq_empty run_error %d : %d\n", i, ret); |
| 886 | } |
| 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | static int usbhsf_irq_ready(struct usbhs_priv *priv, |
| 892 | struct usbhs_irq_state *irq_state) |
| 893 | { |
| 894 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 895 | struct device *dev = usbhs_priv_to_dev(priv); |
| 896 | int i, ret; |
| 897 | |
| 898 | if (!irq_state->brdysts) { |
| 899 | dev_err(dev, "debug %s !!\n", __func__); |
| 900 | return -EIO; |
| 901 | } |
| 902 | |
| 903 | dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts); |
| 904 | |
| 905 | /* |
| 906 | * search interrupted "pipe" |
| 907 | * not "uep". |
| 908 | */ |
| 909 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 910 | if (!(irq_state->brdysts & (1 << i))) |
| 911 | continue; |
| 912 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 913 | ret = usbhs_pkt_run(pipe); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 914 | if (ret < 0) |
| 915 | dev_err(dev, "irq_ready run_error %d : %d\n", i, ret); |
| 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 921 | static void usbhsf_dma_complete(void *arg) |
| 922 | { |
| 923 | struct usbhs_pipe *pipe = arg; |
| 924 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 925 | struct device *dev = usbhs_priv_to_dev(priv); |
| 926 | int ret; |
| 927 | |
| 928 | ret = usbhs_pkt_dmadone(pipe); |
| 929 | if (ret < 0) |
| 930 | dev_err(dev, "dma_complete run_error %d : %d\n", |
| 931 | usbhs_pipe_number(pipe), ret); |
| 932 | } |
| 933 | |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 934 | /* |
| 935 | * fifo init |
| 936 | */ |
| 937 | void usbhs_fifo_init(struct usbhs_priv *priv) |
| 938 | { |
| 939 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 940 | struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 941 | struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv); |
| 942 | struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 943 | |
| 944 | mod->irq_empty = usbhsf_irq_empty; |
| 945 | mod->irq_ready = usbhsf_irq_ready; |
| 946 | mod->irq_bempsts = 0; |
| 947 | mod->irq_brdysts = 0; |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 948 | |
| 949 | cfifo->pipe = NULL; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 950 | cfifo->tx_chan = NULL; |
| 951 | cfifo->rx_chan = NULL; |
| 952 | |
| 953 | d0fifo->pipe = NULL; |
| 954 | d0fifo->tx_chan = NULL; |
| 955 | d0fifo->rx_chan = NULL; |
| 956 | |
| 957 | d1fifo->pipe = NULL; |
| 958 | d1fifo->tx_chan = NULL; |
| 959 | d1fifo->rx_chan = NULL; |
| 960 | |
| 961 | usbhsf_dma_init(priv, usbhsf_get_d0fifo(priv)); |
| 962 | usbhsf_dma_init(priv, usbhsf_get_d1fifo(priv)); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | void usbhs_fifo_quit(struct usbhs_priv *priv) |
| 966 | { |
| 967 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 968 | |
| 969 | mod->irq_empty = NULL; |
| 970 | mod->irq_ready = NULL; |
| 971 | mod->irq_bempsts = 0; |
| 972 | mod->irq_brdysts = 0; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 973 | |
| 974 | usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv)); |
| 975 | usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv)); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 976 | } |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 977 | |
| 978 | int usbhs_fifo_probe(struct usbhs_priv *priv) |
| 979 | { |
| 980 | struct usbhs_fifo *fifo; |
| 981 | |
| 982 | /* CFIFO */ |
| 983 | fifo = usbhsf_get_cfifo(priv); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 984 | fifo->name = "CFIFO"; |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 985 | fifo->port = CFIFO; |
| 986 | fifo->sel = CFIFOSEL; |
| 987 | fifo->ctr = CFIFOCTR; |
| 988 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 989 | /* D0FIFO */ |
| 990 | fifo = usbhsf_get_d0fifo(priv); |
| 991 | fifo->name = "D0FIFO"; |
| 992 | fifo->port = D0FIFO; |
| 993 | fifo->sel = D0FIFOSEL; |
| 994 | fifo->ctr = D0FIFOCTR; |
| 995 | fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id); |
| 996 | fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id); |
| 997 | |
| 998 | /* D1FIFO */ |
| 999 | fifo = usbhsf_get_d1fifo(priv); |
| 1000 | fifo->name = "D1FIFO"; |
| 1001 | fifo->port = D1FIFO; |
| 1002 | fifo->sel = D1FIFOSEL; |
| 1003 | fifo->ctr = D1FIFOCTR; |
| 1004 | fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id); |
| 1005 | fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id); |
| 1006 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | void usbhs_fifo_remove(struct usbhs_priv *priv) |
| 1011 | { |
| 1012 | } |