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