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