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> |
Paul Bolle | cc502bb | 2012-06-03 18:39:13 +0200 | [diff] [blame] | 20 | #include "common.h" |
| 21 | #include "pipe.h" |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 22 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 23 | #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo)) |
Shimoda, Yoshihiro | 5ea4399 | 2012-01-05 15:37:22 +0900 | [diff] [blame] | 24 | #define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f) |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 25 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 26 | #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */ |
| 27 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 28 | /* |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 29 | * packet initialize |
| 30 | */ |
| 31 | void usbhs_pkt_init(struct usbhs_pkt *pkt) |
| 32 | { |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 33 | INIT_LIST_HEAD(&pkt->node); |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * packet control function |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 38 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 39 | static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 40 | { |
| 41 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe); |
| 42 | struct device *dev = usbhs_priv_to_dev(priv); |
| 43 | |
| 44 | dev_err(dev, "null handler\n"); |
| 45 | |
| 46 | return -EINVAL; |
| 47 | } |
| 48 | |
| 49 | static struct usbhs_pkt_handle usbhsf_null_handler = { |
| 50 | .prepare = usbhsf_null_handle, |
| 51 | .try_run = usbhsf_null_handle, |
| 52 | }; |
| 53 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 54 | void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt, |
Kuninori Morimoto | b331872 | 2011-10-10 22:04:41 -0700 | [diff] [blame] | 55 | void (*done)(struct usbhs_priv *priv, |
| 56 | struct usbhs_pkt *pkt), |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 57 | void *buf, int len, int zero, int sequence) |
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 | |
Kuninori Morimoto | b331872 | 2011-10-10 22:04:41 -0700 | [diff] [blame] | 63 | if (!done) { |
| 64 | dev_err(dev, "no done function\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
Kuninori Morimoto | a2c76b8 | 2011-10-18 20:05:50 -0700 | [diff] [blame] | 68 | /******************** spin lock ********************/ |
| 69 | usbhs_lock(priv, flags); |
| 70 | |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 71 | if (!pipe->handler) { |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 72 | dev_err(dev, "no handler function\n"); |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 73 | pipe->handler = &usbhsf_null_handler; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 74 | } |
| 75 | |
Guennadi Liakhovetski | d526128 | 2012-02-14 11:37:17 +0100 | [diff] [blame] | 76 | list_move_tail(&pkt->node, &pipe->list); |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 77 | |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 78 | /* |
| 79 | * each pkt must hold own handler. |
| 80 | * because handler might be changed by its situation. |
| 81 | * dma handler -> pio handler. |
| 82 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 83 | pkt->pipe = pipe; |
| 84 | pkt->buf = buf; |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 85 | pkt->handler = pipe->handler; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 86 | pkt->length = len; |
| 87 | pkt->zero = zero; |
| 88 | pkt->actual = 0; |
Kuninori Morimoto | b331872 | 2011-10-10 22:04:41 -0700 | [diff] [blame] | 89 | pkt->done = done; |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 90 | pkt->sequence = sequence; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 91 | |
| 92 | usbhs_unlock(priv, flags); |
| 93 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 94 | } |
| 95 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 96 | static void __usbhsf_pkt_del(struct usbhs_pkt *pkt) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 97 | { |
| 98 | list_del_init(&pkt->node); |
| 99 | } |
| 100 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 101 | static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 102 | { |
| 103 | if (list_empty(&pipe->list)) |
| 104 | return NULL; |
| 105 | |
Guennadi Liakhovetski | d526128 | 2012-02-14 11:37:17 +0100 | [diff] [blame] | 106 | return list_first_entry(&pipe->list, struct usbhs_pkt, node); |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Yoshihiro Shimoda | 2743e7f | 2014-08-22 20:14:28 +0900 | [diff] [blame] | 109 | static void usbhsf_fifo_clear(struct usbhs_pipe *pipe, |
| 110 | struct usbhs_fifo *fifo); |
| 111 | static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe, |
| 112 | struct usbhs_fifo *fifo); |
| 113 | static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo, |
| 114 | struct usbhs_pkt *pkt); |
| 115 | #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1) |
| 116 | #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0) |
| 117 | static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 118 | struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt) |
| 119 | { |
| 120 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Yoshihiro Shimoda | 2743e7f | 2014-08-22 20:14:28 +0900 | [diff] [blame] | 121 | struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 122 | unsigned long flags; |
| 123 | |
| 124 | /******************** spin lock ********************/ |
| 125 | usbhs_lock(priv, flags); |
| 126 | |
Yoshihiro Shimoda | 2743e7f | 2014-08-22 20:14:28 +0900 | [diff] [blame] | 127 | usbhs_pipe_disable(pipe); |
| 128 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 129 | if (!pkt) |
| 130 | pkt = __usbhsf_pkt_get(pipe); |
| 131 | |
Yoshihiro Shimoda | 2743e7f | 2014-08-22 20:14:28 +0900 | [diff] [blame] | 132 | if (pkt) { |
| 133 | struct dma_chan *chan = NULL; |
| 134 | |
| 135 | if (fifo) |
| 136 | chan = usbhsf_dma_chan_get(fifo, pkt); |
| 137 | if (chan) { |
| 138 | dmaengine_terminate_all(chan); |
| 139 | usbhsf_fifo_clear(pipe, fifo); |
| 140 | usbhsf_dma_unmap(pkt); |
| 141 | } |
| 142 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 143 | __usbhsf_pkt_del(pkt); |
Yoshihiro Shimoda | 2743e7f | 2014-08-22 20:14:28 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (fifo) |
| 147 | usbhsf_fifo_unselect(pipe, fifo); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 148 | |
| 149 | usbhs_unlock(priv, flags); |
| 150 | /******************** spin unlock ******************/ |
| 151 | |
| 152 | return pkt; |
| 153 | } |
| 154 | |
Kuninori Morimoto | 51b8a02 | 2011-10-10 21:58:45 -0700 | [diff] [blame] | 155 | enum { |
| 156 | USBHSF_PKT_PREPARE, |
| 157 | USBHSF_PKT_TRY_RUN, |
| 158 | USBHSF_PKT_DMA_DONE, |
| 159 | }; |
| 160 | |
| 161 | static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 162 | { |
| 163 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 164 | struct usbhs_pkt *pkt; |
| 165 | struct device *dev = usbhs_priv_to_dev(priv); |
| 166 | int (*func)(struct usbhs_pkt *pkt, int *is_done); |
| 167 | unsigned long flags; |
| 168 | int ret = 0; |
| 169 | int is_done = 0; |
| 170 | |
| 171 | /******************** spin lock ********************/ |
| 172 | usbhs_lock(priv, flags); |
| 173 | |
| 174 | pkt = __usbhsf_pkt_get(pipe); |
| 175 | if (!pkt) |
| 176 | goto __usbhs_pkt_handler_end; |
| 177 | |
| 178 | switch (type) { |
| 179 | case USBHSF_PKT_PREPARE: |
| 180 | func = pkt->handler->prepare; |
| 181 | break; |
| 182 | case USBHSF_PKT_TRY_RUN: |
| 183 | func = pkt->handler->try_run; |
| 184 | break; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 185 | case USBHSF_PKT_DMA_DONE: |
| 186 | func = pkt->handler->dma_done; |
| 187 | break; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 188 | default: |
Masanari Iida | 984e833 | 2012-11-01 00:03:51 +0900 | [diff] [blame] | 189 | dev_err(dev, "unknown pkt handler\n"); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 190 | goto __usbhs_pkt_handler_end; |
| 191 | } |
| 192 | |
| 193 | ret = func(pkt, &is_done); |
| 194 | |
| 195 | if (is_done) |
| 196 | __usbhsf_pkt_del(pkt); |
| 197 | |
| 198 | __usbhs_pkt_handler_end: |
| 199 | usbhs_unlock(priv, flags); |
| 200 | /******************** spin unlock ******************/ |
| 201 | |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 202 | if (is_done) { |
Kuninori Morimoto | b331872 | 2011-10-10 22:04:41 -0700 | [diff] [blame] | 203 | pkt->done(priv, pkt); |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 204 | usbhs_pkt_start(pipe); |
| 205 | } |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 206 | |
| 207 | return ret; |
| 208 | } |
| 209 | |
Kuninori Morimoto | 51b8a02 | 2011-10-10 21:58:45 -0700 | [diff] [blame] | 210 | void usbhs_pkt_start(struct usbhs_pipe *pipe) |
| 211 | { |
| 212 | usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE); |
| 213 | } |
| 214 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 215 | /* |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 216 | * irq enable/disable function |
| 217 | */ |
Kuninori Morimoto | 3192fcb | 2012-10-29 00:45:07 -0700 | [diff] [blame] | 218 | #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e) |
| 219 | #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 220 | #define usbhsf_irq_callback_ctrl(pipe, status, enable) \ |
| 221 | ({ \ |
| 222 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \ |
| 223 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); \ |
| 224 | u16 status = (1 << usbhs_pipe_number(pipe)); \ |
| 225 | if (!mod) \ |
| 226 | return; \ |
| 227 | if (enable) \ |
Kuninori Morimoto | 3192fcb | 2012-10-29 00:45:07 -0700 | [diff] [blame] | 228 | mod->status |= status; \ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 229 | else \ |
Kuninori Morimoto | 3192fcb | 2012-10-29 00:45:07 -0700 | [diff] [blame] | 230 | mod->status &= ~status; \ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 231 | usbhs_irq_callback_update(priv, mod); \ |
| 232 | }) |
| 233 | |
| 234 | static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable) |
| 235 | { |
| 236 | /* |
| 237 | * And DCP pipe can NOT use "ready interrupt" for "send" |
| 238 | * it should use "empty" interrupt. |
| 239 | * see |
| 240 | * "Operation" - "Interrupt Function" - "BRDY Interrupt" |
| 241 | * |
| 242 | * on the other hand, normal pipe can use "ready interrupt" for "send" |
| 243 | * even though it is single/double buffer |
| 244 | */ |
| 245 | if (usbhs_pipe_is_dcp(pipe)) |
| 246 | usbhsf_irq_empty_ctrl(pipe, enable); |
| 247 | else |
| 248 | usbhsf_irq_ready_ctrl(pipe, enable); |
| 249 | } |
| 250 | |
| 251 | static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable) |
| 252 | { |
| 253 | usbhsf_irq_ready_ctrl(pipe, enable); |
| 254 | } |
| 255 | |
| 256 | /* |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 257 | * FIFO ctrl |
| 258 | */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 259 | static void usbhsf_send_terminator(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 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 264 | usbhs_bset(priv, fifo->ctr, BVAL, BVAL); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 265 | } |
| 266 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 267 | static int usbhsf_fifo_barrier(struct usbhs_priv *priv, |
| 268 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 269 | { |
| 270 | int timeout = 1024; |
| 271 | |
| 272 | do { |
| 273 | /* The FIFO port is accessible */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 274 | if (usbhs_read(priv, fifo->ctr) & FRDY) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 275 | return 0; |
| 276 | |
| 277 | udelay(10); |
| 278 | } while (timeout--); |
| 279 | |
| 280 | return -EBUSY; |
| 281 | } |
| 282 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 283 | static void usbhsf_fifo_clear(struct usbhs_pipe *pipe, |
| 284 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 285 | { |
| 286 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 287 | |
| 288 | if (!usbhs_pipe_is_dcp(pipe)) |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 289 | usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 290 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 291 | usbhs_write(priv, fifo->ctr, BCLR); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 292 | } |
| 293 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 294 | static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv, |
| 295 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 296 | { |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 297 | return usbhs_read(priv, fifo->ctr) & DTLN_MASK; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 298 | } |
| 299 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 300 | static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe, |
| 301 | struct usbhs_fifo *fifo) |
| 302 | { |
| 303 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 304 | |
| 305 | usbhs_pipe_select_fifo(pipe, NULL); |
| 306 | usbhs_write(priv, fifo->sel, 0); |
| 307 | } |
| 308 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 309 | static int usbhsf_fifo_select(struct usbhs_pipe *pipe, |
| 310 | struct usbhs_fifo *fifo, |
| 311 | int write) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 312 | { |
| 313 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 314 | struct device *dev = usbhs_priv_to_dev(priv); |
| 315 | int timeout = 1024; |
| 316 | u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */ |
| 317 | u16 base = usbhs_pipe_number(pipe); /* CURPIPE */ |
| 318 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 319 | if (usbhs_pipe_is_busy(pipe) || |
| 320 | usbhsf_fifo_is_busy(fifo)) |
| 321 | return -EBUSY; |
| 322 | |
Kuninori Morimoto | 9235207 | 2011-10-10 22:02:57 -0700 | [diff] [blame] | 323 | if (usbhs_pipe_is_dcp(pipe)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 324 | base |= (1 == write) << 5; /* ISEL */ |
| 325 | |
Kuninori Morimoto | 9235207 | 2011-10-10 22:02:57 -0700 | [diff] [blame] | 326 | if (usbhs_mod_is_host(priv)) |
| 327 | usbhs_dcp_dir_for_host(pipe, write); |
| 328 | } |
| 329 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 330 | /* "base" will be used below */ |
Shimoda, Yoshihiro | 5ea4399 | 2012-01-05 15:37:22 +0900 | [diff] [blame] | 331 | if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo)) |
| 332 | usbhs_write(priv, fifo->sel, base); |
| 333 | else |
| 334 | usbhs_write(priv, fifo->sel, base | MBW_32); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 335 | |
| 336 | /* check ISEL and CURPIPE value */ |
| 337 | while (timeout--) { |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 338 | if (base == (mask & usbhs_read(priv, fifo->sel))) { |
| 339 | usbhs_pipe_select_fifo(pipe, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 340 | return 0; |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 341 | } |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 342 | udelay(10); |
| 343 | } |
| 344 | |
| 345 | dev_err(dev, "fifo select error\n"); |
| 346 | |
| 347 | return -EIO; |
| 348 | } |
| 349 | |
| 350 | /* |
Kuninori Morimoto | 9e74d60 | 2011-10-10 22:07:08 -0700 | [diff] [blame] | 351 | * DCP status stage |
| 352 | */ |
| 353 | static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done) |
| 354 | { |
| 355 | struct usbhs_pipe *pipe = pkt->pipe; |
| 356 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 357 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 358 | struct device *dev = usbhs_priv_to_dev(priv); |
| 359 | int ret; |
| 360 | |
| 361 | usbhs_pipe_disable(pipe); |
| 362 | |
| 363 | ret = usbhsf_fifo_select(pipe, fifo, 1); |
| 364 | if (ret < 0) { |
| 365 | dev_err(dev, "%s() faile\n", __func__); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | usbhs_pipe_sequence_data1(pipe); /* DATA1 */ |
| 370 | |
| 371 | usbhsf_fifo_clear(pipe, fifo); |
| 372 | usbhsf_send_terminator(pipe, fifo); |
| 373 | |
| 374 | usbhsf_fifo_unselect(pipe, fifo); |
| 375 | |
| 376 | usbhsf_tx_irq_ctrl(pipe, 1); |
| 377 | usbhs_pipe_enable(pipe); |
| 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done) |
| 383 | { |
| 384 | struct usbhs_pipe *pipe = pkt->pipe; |
| 385 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 386 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 387 | struct device *dev = usbhs_priv_to_dev(priv); |
| 388 | int ret; |
| 389 | |
| 390 | usbhs_pipe_disable(pipe); |
| 391 | |
| 392 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
| 393 | if (ret < 0) { |
| 394 | dev_err(dev, "%s() fail\n", __func__); |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | usbhs_pipe_sequence_data1(pipe); /* DATA1 */ |
| 399 | usbhsf_fifo_clear(pipe, fifo); |
| 400 | |
| 401 | usbhsf_fifo_unselect(pipe, fifo); |
| 402 | |
| 403 | usbhsf_rx_irq_ctrl(pipe, 1); |
| 404 | usbhs_pipe_enable(pipe); |
| 405 | |
| 406 | return ret; |
| 407 | |
| 408 | } |
| 409 | |
| 410 | static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done) |
| 411 | { |
| 412 | struct usbhs_pipe *pipe = pkt->pipe; |
| 413 | |
| 414 | if (pkt->handler == &usbhs_dcp_status_stage_in_handler) |
| 415 | usbhsf_tx_irq_ctrl(pipe, 0); |
| 416 | else |
| 417 | usbhsf_rx_irq_ctrl(pipe, 0); |
| 418 | |
| 419 | pkt->actual = pkt->length; |
| 420 | *is_done = 1; |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = { |
| 426 | .prepare = usbhs_dcp_dir_switch_to_write, |
| 427 | .try_run = usbhs_dcp_dir_switch_done, |
| 428 | }; |
| 429 | |
| 430 | struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = { |
| 431 | .prepare = usbhs_dcp_dir_switch_to_read, |
| 432 | .try_run = usbhs_dcp_dir_switch_done, |
| 433 | }; |
| 434 | |
| 435 | /* |
| 436 | * DCP data stage (push) |
| 437 | */ |
| 438 | static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done) |
| 439 | { |
| 440 | struct usbhs_pipe *pipe = pkt->pipe; |
| 441 | |
| 442 | usbhs_pipe_sequence_data1(pipe); /* DATA1 */ |
| 443 | |
| 444 | /* |
| 445 | * change handler to PIO push |
| 446 | */ |
| 447 | pkt->handler = &usbhs_fifo_pio_push_handler; |
| 448 | |
| 449 | return pkt->handler->prepare(pkt, is_done); |
| 450 | } |
| 451 | |
| 452 | struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = { |
| 453 | .prepare = usbhsf_dcp_data_stage_try_push, |
| 454 | }; |
| 455 | |
| 456 | /* |
| 457 | * DCP data stage (pop) |
| 458 | */ |
| 459 | static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt, |
| 460 | int *is_done) |
| 461 | { |
| 462 | struct usbhs_pipe *pipe = pkt->pipe; |
| 463 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 464 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); |
| 465 | |
| 466 | if (usbhs_pipe_is_busy(pipe)) |
| 467 | return 0; |
| 468 | |
| 469 | /* |
| 470 | * prepare pop for DCP should |
| 471 | * - change DCP direction, |
| 472 | * - clear fifo |
| 473 | * - DATA1 |
| 474 | */ |
| 475 | usbhs_pipe_disable(pipe); |
| 476 | |
| 477 | usbhs_pipe_sequence_data1(pipe); /* DATA1 */ |
| 478 | |
| 479 | usbhsf_fifo_select(pipe, fifo, 0); |
| 480 | usbhsf_fifo_clear(pipe, fifo); |
| 481 | usbhsf_fifo_unselect(pipe, fifo); |
| 482 | |
| 483 | /* |
| 484 | * change handler to PIO pop |
| 485 | */ |
| 486 | pkt->handler = &usbhs_fifo_pio_pop_handler; |
| 487 | |
| 488 | return pkt->handler->prepare(pkt, is_done); |
| 489 | } |
| 490 | |
| 491 | struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = { |
| 492 | .prepare = usbhsf_dcp_data_stage_prepare_pop, |
| 493 | }; |
| 494 | |
| 495 | /* |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 496 | * PIO push handler |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 497 | */ |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 498 | 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] | 499 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 500 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 501 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 502 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 503 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 504 | void __iomem *addr = priv->base + fifo->port; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 505 | u8 *buf; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 506 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
| 507 | int total_len; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 508 | int i, ret, len; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 509 | int is_short; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 510 | |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 511 | usbhs_pipe_data_sequence(pipe, pkt->sequence); |
| 512 | pkt->sequence = -1; /* -1 sequence will be ignored */ |
| 513 | |
Kuninori Morimoto | 1c90ee0 | 2012-11-06 16:15:09 -0800 | [diff] [blame] | 514 | usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length); |
| 515 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 516 | ret = usbhsf_fifo_select(pipe, fifo, 1); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 517 | if (ret < 0) |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 518 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 519 | |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 520 | ret = usbhs_pipe_is_accessible(pipe); |
Kuninori Morimoto | 4ef85e0 | 2011-07-03 17:42:47 -0700 | [diff] [blame] | 521 | if (ret < 0) { |
| 522 | /* inaccessible pipe is not an error */ |
| 523 | ret = 0; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 524 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | 4ef85e0 | 2011-07-03 17:42:47 -0700 | [diff] [blame] | 525 | } |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 526 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 527 | ret = usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 528 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 529 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 530 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 531 | buf = pkt->buf + pkt->actual; |
| 532 | len = pkt->length - pkt->actual; |
| 533 | len = min(len, maxp); |
| 534 | total_len = len; |
| 535 | is_short = total_len < maxp; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * FIXME |
| 539 | * |
| 540 | * 32-bit access only |
| 541 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 542 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 543 | iowrite32_rep(addr, buf, len / 4); |
| 544 | len %= 4; |
| 545 | buf += total_len - len; |
| 546 | } |
| 547 | |
| 548 | /* the rest operation */ |
| 549 | for (i = 0; i < len; i++) |
| 550 | iowrite8(buf[i], addr + (0x03 - (i & 0x03))); |
| 551 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 552 | /* |
| 553 | * variable update |
| 554 | */ |
| 555 | pkt->actual += total_len; |
| 556 | |
| 557 | if (pkt->actual < pkt->length) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 558 | *is_done = 0; /* there are remainder data */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 559 | else if (is_short) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 560 | *is_done = 1; /* short packet */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 561 | else |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 562 | *is_done = !pkt->zero; /* send zero packet ? */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 563 | |
| 564 | /* |
| 565 | * pipe/irq handling |
| 566 | */ |
| 567 | if (is_short) |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 568 | usbhsf_send_terminator(pipe, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 569 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 570 | usbhsf_tx_irq_ctrl(pipe, !*is_done); |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 571 | usbhs_pipe_running(pipe, !*is_done); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 572 | usbhs_pipe_enable(pipe); |
| 573 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 574 | dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n", |
| 575 | usbhs_pipe_number(pipe), |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 576 | pkt->length, pkt->actual, *is_done, pkt->zero); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 577 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 578 | usbhsf_fifo_unselect(pipe, fifo); |
| 579 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 580 | return 0; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 581 | |
| 582 | usbhs_fifo_write_busy: |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 583 | usbhsf_fifo_unselect(pipe, fifo); |
| 584 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 585 | /* |
| 586 | * pipe is busy. |
| 587 | * retry in interrupt |
| 588 | */ |
| 589 | usbhsf_tx_irq_ctrl(pipe, 1); |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 590 | usbhs_pipe_running(pipe, 1); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 591 | |
| 592 | return ret; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 593 | } |
| 594 | |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 595 | static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done) |
| 596 | { |
| 597 | if (usbhs_pipe_is_running(pkt->pipe)) |
| 598 | return 0; |
| 599 | |
| 600 | return usbhsf_pio_try_push(pkt, is_done); |
| 601 | } |
| 602 | |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 603 | struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = { |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 604 | .prepare = usbhsf_pio_prepare_push, |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 605 | .try_run = usbhsf_pio_try_push, |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 606 | }; |
| 607 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 608 | /* |
| 609 | * PIO pop handler |
| 610 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 611 | static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 612 | { |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 613 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 614 | |
| 615 | if (usbhs_pipe_is_busy(pipe)) |
| 616 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 617 | |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 618 | if (usbhs_pipe_is_running(pipe)) |
| 619 | return 0; |
| 620 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 621 | /* |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 622 | * pipe enable to prepare packet receive |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 623 | */ |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 624 | usbhs_pipe_data_sequence(pipe, pkt->sequence); |
| 625 | pkt->sequence = -1; /* -1 sequence will be ignored */ |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 626 | |
Kuninori Morimoto | 1c90ee0 | 2012-11-06 16:15:09 -0800 | [diff] [blame] | 627 | usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 628 | usbhs_pipe_enable(pipe); |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 629 | usbhs_pipe_running(pipe, 1); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 630 | usbhsf_rx_irq_ctrl(pipe, 1); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 631 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 632 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 633 | } |
| 634 | |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 635 | 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] | 636 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 637 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 638 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 639 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 640 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 641 | void __iomem *addr = priv->base + fifo->port; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 642 | u8 *buf; |
| 643 | u32 data = 0; |
| 644 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 645 | int rcv_len, len; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 646 | int i, ret; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 647 | int total_len = 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 648 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 649 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 650 | if (ret < 0) |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 651 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 652 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 653 | ret = usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 654 | if (ret < 0) |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 655 | goto usbhs_fifo_read_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 656 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 657 | rcv_len = usbhsf_fifo_rcv_len(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 658 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 659 | buf = pkt->buf + pkt->actual; |
| 660 | len = pkt->length - pkt->actual; |
| 661 | len = min(len, rcv_len); |
| 662 | total_len = len; |
| 663 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 664 | /* |
Kuninori Morimoto | 6ff5d09 | 2011-10-10 22:05:51 -0700 | [diff] [blame] | 665 | * update actual length first here to decide disable pipe. |
| 666 | * if this pipe keeps BUF status and all data were popped, |
| 667 | * then, next interrupt/token will be issued again |
| 668 | */ |
| 669 | pkt->actual += total_len; |
| 670 | |
| 671 | if ((pkt->actual == pkt->length) || /* receive all data */ |
| 672 | (total_len < maxp)) { /* short packet */ |
| 673 | *is_done = 1; |
| 674 | usbhsf_rx_irq_ctrl(pipe, 0); |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 675 | usbhs_pipe_running(pipe, 0); |
Kuninori Morimoto | 6ff5d09 | 2011-10-10 22:05:51 -0700 | [diff] [blame] | 676 | usbhs_pipe_disable(pipe); /* disable pipe first */ |
| 677 | } |
| 678 | |
| 679 | /* |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 680 | * Buffer clear if Zero-Length packet |
| 681 | * |
| 682 | * see |
| 683 | * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function" |
| 684 | */ |
| 685 | if (0 == rcv_len) { |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 686 | pkt->zero = 1; |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 687 | usbhsf_fifo_clear(pipe, fifo); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 688 | goto usbhs_fifo_read_end; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 689 | } |
| 690 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 691 | /* |
| 692 | * FIXME |
| 693 | * |
| 694 | * 32-bit access only |
| 695 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 696 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 697 | ioread32_rep(addr, buf, len / 4); |
| 698 | len %= 4; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 699 | buf += total_len - len; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* the rest operation */ |
| 703 | for (i = 0; i < len; i++) { |
| 704 | if (!(i & 0x03)) |
| 705 | data = ioread32(addr); |
| 706 | |
| 707 | buf[i] = (data >> ((i & 0x03) * 8)) & 0xff; |
| 708 | } |
| 709 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 710 | usbhs_fifo_read_end: |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 711 | dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n", |
| 712 | usbhs_pipe_number(pipe), |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 713 | pkt->length, pkt->actual, *is_done, pkt->zero); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 714 | |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 715 | usbhs_fifo_read_busy: |
| 716 | usbhsf_fifo_unselect(pipe, fifo); |
| 717 | |
| 718 | return ret; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 719 | } |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 720 | |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 721 | struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = { |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 722 | .prepare = usbhsf_prepare_pop, |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame] | 723 | .try_run = usbhsf_pio_try_pop, |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 724 | }; |
| 725 | |
| 726 | /* |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 727 | * DCP ctrol statge handler |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 728 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 729 | 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] | 730 | { |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 731 | usbhs_dcp_control_transfer_done(pkt->pipe); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 732 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 733 | *is_done = 1; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = { |
| 739 | .prepare = usbhsf_ctrl_stage_end, |
| 740 | .try_run = usbhsf_ctrl_stage_end, |
| 741 | }; |
| 742 | |
| 743 | /* |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 744 | * DMA fifo functions |
| 745 | */ |
| 746 | static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo, |
| 747 | struct usbhs_pkt *pkt) |
| 748 | { |
| 749 | if (&usbhs_fifo_dma_push_handler == pkt->handler) |
| 750 | return fifo->tx_chan; |
| 751 | |
| 752 | if (&usbhs_fifo_dma_pop_handler == pkt->handler) |
| 753 | return fifo->rx_chan; |
| 754 | |
| 755 | return NULL; |
| 756 | } |
| 757 | |
| 758 | static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv, |
| 759 | struct usbhs_pkt *pkt) |
| 760 | { |
| 761 | struct usbhs_fifo *fifo; |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 762 | int i; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 763 | |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 764 | usbhs_for_each_dfifo(priv, fifo, i) { |
| 765 | if (usbhsf_dma_chan_get(fifo, pkt) && |
| 766 | !usbhsf_fifo_is_busy(fifo)) |
| 767 | return fifo; |
| 768 | } |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 769 | |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE) |
| 774 | #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0) |
| 775 | static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe, |
| 776 | struct usbhs_fifo *fifo, |
| 777 | u16 dreqe) |
| 778 | { |
| 779 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 780 | |
| 781 | usbhs_bset(priv, fifo->sel, DREQE, dreqe); |
| 782 | } |
| 783 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 784 | static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map) |
| 785 | { |
| 786 | struct usbhs_pipe *pipe = pkt->pipe; |
| 787 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 788 | struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); |
| 789 | |
| 790 | return info->dma_map_ctrl(pkt, map); |
| 791 | } |
| 792 | |
| 793 | static void usbhsf_dma_complete(void *arg); |
Guennadi Liakhovetski | 6e4b74e | 2012-02-14 11:37:21 +0100 | [diff] [blame] | 794 | static void xfer_work(struct work_struct *work) |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 795 | { |
Guennadi Liakhovetski | 6e4b74e | 2012-02-14 11:37:21 +0100 | [diff] [blame] | 796 | struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 797 | struct usbhs_pipe *pipe = pkt->pipe; |
| 798 | struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe); |
| 799 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 800 | struct dma_async_tx_descriptor *desc; |
| 801 | struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt); |
| 802 | struct device *dev = usbhs_priv_to_dev(priv); |
Vinod Koul | 55ba4e5 | 2011-10-14 21:57:58 +0530 | [diff] [blame] | 803 | enum dma_transfer_direction dir; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 804 | |
Vinod Koul | 55ba4e5 | 2011-10-14 21:57:58 +0530 | [diff] [blame] | 805 | dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 806 | |
Kuninori Morimoto | 2f0de9d | 2012-07-08 23:10:28 -0700 | [diff] [blame] | 807 | desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual, |
| 808 | pkt->trans, dir, |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 809 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 810 | if (!desc) |
| 811 | return; |
| 812 | |
| 813 | desc->callback = usbhsf_dma_complete; |
| 814 | desc->callback_param = pipe; |
| 815 | |
Yoshihiro Shimoda | ab330cf | 2015-03-12 15:35:20 +0900 | [diff] [blame] | 816 | pkt->cookie = dmaengine_submit(desc); |
| 817 | if (pkt->cookie < 0) { |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 818 | dev_err(dev, "Failed to submit dma descriptor\n"); |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | dev_dbg(dev, " %s %d (%d/ %d)\n", |
| 823 | fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero); |
| 824 | |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 825 | usbhs_pipe_running(pipe, 1); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 826 | usbhsf_dma_start(pipe, fifo); |
Yoshihiro Shimoda | 9b53d9a | 2015-03-12 15:35:19 +0900 | [diff] [blame] | 827 | usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 828 | dma_async_issue_pending(chan); |
Yoshihiro Shimoda | 9b53d9a | 2015-03-12 15:35:19 +0900 | [diff] [blame] | 829 | usbhs_pipe_enable(pipe); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 830 | } |
| 831 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 832 | /* |
| 833 | * DMA push handler |
| 834 | */ |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 835 | static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done) |
| 836 | { |
| 837 | struct usbhs_pipe *pipe = pkt->pipe; |
| 838 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 839 | struct usbhs_fifo *fifo; |
| 840 | int len = pkt->length - pkt->actual; |
| 841 | int ret; |
Yoshihiro Shimoda | ab330cf | 2015-03-12 15:35:20 +0900 | [diff] [blame] | 842 | uintptr_t align_mask; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 843 | |
| 844 | if (usbhs_pipe_is_busy(pipe)) |
| 845 | return 0; |
| 846 | |
| 847 | /* use PIO if packet is less than pio_dma_border or pipe is DCP */ |
| 848 | if ((len < usbhs_get_dparam(priv, pio_dma_border)) || |
| 849 | usbhs_pipe_is_dcp(pipe)) |
| 850 | goto usbhsf_pio_prepare_push; |
| 851 | |
Yoshihiro Shimoda | ab330cf | 2015-03-12 15:35:20 +0900 | [diff] [blame] | 852 | /* check data length if this driver don't use USB-DMAC */ |
| 853 | if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7) |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 854 | goto usbhsf_pio_prepare_push; |
| 855 | |
Yoshihiro Shimoda | ab330cf | 2015-03-12 15:35:20 +0900 | [diff] [blame] | 856 | /* check buffer alignment */ |
| 857 | align_mask = usbhs_get_dparam(priv, has_usb_dmac) ? |
| 858 | USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7; |
| 859 | if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask) |
Kuninori Morimoto | 9a12d09 | 2011-07-03 17:42:19 -0700 | [diff] [blame] | 860 | goto usbhsf_pio_prepare_push; |
| 861 | |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 862 | /* return at this time if the pipe is running */ |
| 863 | if (usbhs_pipe_is_running(pipe)) |
| 864 | return 0; |
| 865 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 866 | /* get enable DMA fifo */ |
| 867 | fifo = usbhsf_get_dma_fifo(priv, pkt); |
| 868 | if (!fifo) |
| 869 | goto usbhsf_pio_prepare_push; |
| 870 | |
| 871 | if (usbhsf_dma_map(pkt) < 0) |
| 872 | goto usbhsf_pio_prepare_push; |
| 873 | |
| 874 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
| 875 | if (ret < 0) |
| 876 | goto usbhsf_pio_prepare_push_unmap; |
| 877 | |
| 878 | pkt->trans = len; |
| 879 | |
Guennadi Liakhovetski | 6e4b74e | 2012-02-14 11:37:21 +0100 | [diff] [blame] | 880 | INIT_WORK(&pkt->work, xfer_work); |
| 881 | schedule_work(&pkt->work); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 882 | |
| 883 | return 0; |
| 884 | |
| 885 | usbhsf_pio_prepare_push_unmap: |
| 886 | usbhsf_dma_unmap(pkt); |
| 887 | usbhsf_pio_prepare_push: |
| 888 | /* |
| 889 | * change handler to PIO |
| 890 | */ |
| 891 | pkt->handler = &usbhs_fifo_pio_push_handler; |
| 892 | |
| 893 | return pkt->handler->prepare(pkt, is_done); |
| 894 | } |
| 895 | |
| 896 | static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done) |
| 897 | { |
| 898 | struct usbhs_pipe *pipe = pkt->pipe; |
Yoshihiro Shimoda | c0ed8b2 | 2014-08-22 20:14:10 +0900 | [diff] [blame] | 899 | int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 900 | |
Yoshihiro Shimoda | c0ed8b2 | 2014-08-22 20:14:10 +0900 | [diff] [blame] | 901 | pkt->actual += pkt->trans; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 902 | |
Yoshihiro Shimoda | c0ed8b2 | 2014-08-22 20:14:10 +0900 | [diff] [blame] | 903 | if (pkt->actual < pkt->length) |
| 904 | *is_done = 0; /* there are remainder data */ |
| 905 | else if (is_short) |
| 906 | *is_done = 1; /* short packet */ |
| 907 | else |
| 908 | *is_done = !pkt->zero; /* send zero packet? */ |
| 909 | |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 910 | usbhs_pipe_running(pipe, !*is_done); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 911 | |
| 912 | usbhsf_dma_stop(pipe, pipe->fifo); |
| 913 | usbhsf_dma_unmap(pkt); |
| 914 | usbhsf_fifo_unselect(pipe, pipe->fifo); |
| 915 | |
Yoshihiro Shimoda | c0ed8b2 | 2014-08-22 20:14:10 +0900 | [diff] [blame] | 916 | if (!*is_done) { |
| 917 | /* change handler to PIO */ |
| 918 | pkt->handler = &usbhs_fifo_pio_push_handler; |
| 919 | return pkt->handler->try_run(pkt, is_done); |
| 920 | } |
| 921 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = { |
| 926 | .prepare = usbhsf_dma_prepare_push, |
| 927 | .dma_done = usbhsf_dma_push_done, |
| 928 | }; |
| 929 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 930 | /* |
| 931 | * DMA pop handler |
| 932 | */ |
Yoshihiro Shimoda | ab330cf | 2015-03-12 15:35:20 +0900 | [diff] [blame] | 933 | |
| 934 | static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt, |
| 935 | int *is_done) |
| 936 | { |
| 937 | return usbhsf_prepare_pop(pkt, is_done); |
| 938 | } |
| 939 | |
| 940 | static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt, |
| 941 | int *is_done) |
| 942 | { |
| 943 | struct usbhs_pipe *pipe = pkt->pipe; |
| 944 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 945 | struct usbhs_fifo *fifo; |
| 946 | int ret; |
| 947 | |
| 948 | if (usbhs_pipe_is_busy(pipe)) |
| 949 | return 0; |
| 950 | |
| 951 | /* use PIO if packet is less than pio_dma_border or pipe is DCP */ |
| 952 | if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) || |
| 953 | usbhs_pipe_is_dcp(pipe)) |
| 954 | goto usbhsf_pio_prepare_pop; |
| 955 | |
| 956 | fifo = usbhsf_get_dma_fifo(priv, pkt); |
| 957 | if (!fifo) |
| 958 | goto usbhsf_pio_prepare_pop; |
| 959 | |
| 960 | if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1)) |
| 961 | goto usbhsf_pio_prepare_pop; |
| 962 | |
| 963 | usbhs_pipe_config_change_bfre(pipe, 1); |
| 964 | |
| 965 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
| 966 | if (ret < 0) |
| 967 | goto usbhsf_pio_prepare_pop; |
| 968 | |
| 969 | if (usbhsf_dma_map(pkt) < 0) |
| 970 | goto usbhsf_pio_prepare_pop_unselect; |
| 971 | |
| 972 | /* DMA */ |
| 973 | |
| 974 | /* |
| 975 | * usbhs_fifo_dma_pop_handler :: prepare |
| 976 | * enabled irq to come here. |
| 977 | * but it is no longer needed for DMA. disable it. |
| 978 | */ |
| 979 | usbhsf_rx_irq_ctrl(pipe, 0); |
| 980 | |
| 981 | pkt->trans = pkt->length; |
| 982 | |
| 983 | INIT_WORK(&pkt->work, xfer_work); |
| 984 | schedule_work(&pkt->work); |
| 985 | |
| 986 | return 0; |
| 987 | |
| 988 | usbhsf_pio_prepare_pop_unselect: |
| 989 | usbhsf_fifo_unselect(pipe, fifo); |
| 990 | usbhsf_pio_prepare_pop: |
| 991 | |
| 992 | /* |
| 993 | * change handler to PIO |
| 994 | */ |
| 995 | pkt->handler = &usbhs_fifo_pio_pop_handler; |
| 996 | usbhs_pipe_config_change_bfre(pipe, 0); |
| 997 | |
| 998 | return pkt->handler->prepare(pkt, is_done); |
| 999 | } |
| 1000 | |
| 1001 | static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done) |
| 1002 | { |
| 1003 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe); |
| 1004 | |
| 1005 | if (usbhs_get_dparam(priv, has_usb_dmac)) |
| 1006 | return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done); |
| 1007 | else |
| 1008 | return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done); |
| 1009 | } |
| 1010 | |
| 1011 | static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1012 | { |
| 1013 | struct usbhs_pipe *pipe = pkt->pipe; |
| 1014 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 1015 | struct usbhs_fifo *fifo; |
| 1016 | int len, ret; |
| 1017 | |
| 1018 | if (usbhs_pipe_is_busy(pipe)) |
| 1019 | return 0; |
| 1020 | |
| 1021 | if (usbhs_pipe_is_dcp(pipe)) |
| 1022 | goto usbhsf_pio_prepare_pop; |
| 1023 | |
| 1024 | /* get enable DMA fifo */ |
| 1025 | fifo = usbhsf_get_dma_fifo(priv, pkt); |
| 1026 | if (!fifo) |
| 1027 | goto usbhsf_pio_prepare_pop; |
| 1028 | |
Kuninori Morimoto | c9ae0c9 | 2011-10-26 01:21:07 -0700 | [diff] [blame] | 1029 | if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */ |
Kuninori Morimoto | 9a12d09 | 2011-07-03 17:42:19 -0700 | [diff] [blame] | 1030 | goto usbhsf_pio_prepare_pop; |
| 1031 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1032 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
| 1033 | if (ret < 0) |
| 1034 | goto usbhsf_pio_prepare_pop; |
| 1035 | |
| 1036 | /* use PIO if packet is less than pio_dma_border */ |
| 1037 | len = usbhsf_fifo_rcv_len(priv, fifo); |
| 1038 | len = min(pkt->length - pkt->actual, len); |
Kuninori Morimoto | 77975ee | 2012-08-22 18:01:13 -0700 | [diff] [blame] | 1039 | if (len & 0x7) /* 8byte alignment */ |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1040 | goto usbhsf_pio_prepare_pop_unselect; |
| 1041 | |
| 1042 | if (len < usbhs_get_dparam(priv, pio_dma_border)) |
| 1043 | goto usbhsf_pio_prepare_pop_unselect; |
| 1044 | |
| 1045 | ret = usbhsf_fifo_barrier(priv, fifo); |
| 1046 | if (ret < 0) |
| 1047 | goto usbhsf_pio_prepare_pop_unselect; |
| 1048 | |
| 1049 | if (usbhsf_dma_map(pkt) < 0) |
| 1050 | goto usbhsf_pio_prepare_pop_unselect; |
| 1051 | |
| 1052 | /* DMA */ |
| 1053 | |
| 1054 | /* |
| 1055 | * usbhs_fifo_dma_pop_handler :: prepare |
| 1056 | * enabled irq to come here. |
| 1057 | * but it is no longer needed for DMA. disable it. |
| 1058 | */ |
| 1059 | usbhsf_rx_irq_ctrl(pipe, 0); |
| 1060 | |
| 1061 | pkt->trans = len; |
| 1062 | |
Guennadi Liakhovetski | 6e4b74e | 2012-02-14 11:37:21 +0100 | [diff] [blame] | 1063 | INIT_WORK(&pkt->work, xfer_work); |
| 1064 | schedule_work(&pkt->work); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1065 | |
| 1066 | return 0; |
| 1067 | |
| 1068 | usbhsf_pio_prepare_pop_unselect: |
| 1069 | usbhsf_fifo_unselect(pipe, fifo); |
| 1070 | usbhsf_pio_prepare_pop: |
| 1071 | |
| 1072 | /* |
| 1073 | * change handler to PIO |
| 1074 | */ |
| 1075 | pkt->handler = &usbhs_fifo_pio_pop_handler; |
| 1076 | |
| 1077 | return pkt->handler->try_run(pkt, is_done); |
| 1078 | } |
| 1079 | |
Yoshihiro Shimoda | ab330cf | 2015-03-12 15:35:20 +0900 | [diff] [blame] | 1080 | static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done) |
| 1081 | { |
| 1082 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe); |
| 1083 | |
| 1084 | BUG_ON(usbhs_get_dparam(priv, has_usb_dmac)); |
| 1085 | |
| 1086 | return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done); |
| 1087 | } |
| 1088 | |
| 1089 | static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1090 | { |
| 1091 | struct usbhs_pipe *pipe = pkt->pipe; |
| 1092 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
| 1093 | |
| 1094 | usbhsf_dma_stop(pipe, pipe->fifo); |
| 1095 | usbhsf_dma_unmap(pkt); |
| 1096 | usbhsf_fifo_unselect(pipe, pipe->fifo); |
| 1097 | |
| 1098 | pkt->actual += pkt->trans; |
| 1099 | |
| 1100 | if ((pkt->actual == pkt->length) || /* receive all data */ |
| 1101 | (pkt->trans < maxp)) { /* short packet */ |
| 1102 | *is_done = 1; |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 1103 | usbhs_pipe_running(pipe, 0); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1104 | } else { |
| 1105 | /* re-enable */ |
Yoshihiro Shimoda | 8355b2b | 2014-08-22 20:13:50 +0900 | [diff] [blame] | 1106 | usbhs_pipe_running(pipe, 0); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1107 | usbhsf_prepare_pop(pkt, is_done); |
| 1108 | } |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
Yoshihiro Shimoda | ab330cf | 2015-03-12 15:35:20 +0900 | [diff] [blame] | 1113 | static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt, |
| 1114 | struct dma_chan *chan, int dtln) |
| 1115 | { |
| 1116 | struct usbhs_pipe *pipe = pkt->pipe; |
| 1117 | struct dma_tx_state state; |
| 1118 | size_t received_size; |
| 1119 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
| 1120 | |
| 1121 | dmaengine_tx_status(chan, pkt->cookie, &state); |
| 1122 | received_size = pkt->length - state.residue; |
| 1123 | |
| 1124 | if (dtln) { |
| 1125 | received_size -= USBHS_USB_DMAC_XFER_SIZE; |
| 1126 | received_size &= ~(maxp - 1); |
| 1127 | received_size += dtln; |
| 1128 | } |
| 1129 | |
| 1130 | return received_size; |
| 1131 | } |
| 1132 | |
| 1133 | static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt, |
| 1134 | int *is_done) |
| 1135 | { |
| 1136 | struct usbhs_pipe *pipe = pkt->pipe; |
| 1137 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 1138 | struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe); |
| 1139 | struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt); |
| 1140 | int rcv_len; |
| 1141 | |
| 1142 | /* |
| 1143 | * Since the driver disables rx_irq in DMA mode, the interrupt handler |
| 1144 | * cannot the BRDYSTS. So, the function clears it here because the |
| 1145 | * driver may use PIO mode next time. |
| 1146 | */ |
| 1147 | usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe)); |
| 1148 | |
| 1149 | rcv_len = usbhsf_fifo_rcv_len(priv, fifo); |
| 1150 | usbhsf_fifo_clear(pipe, fifo); |
| 1151 | pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len); |
| 1152 | |
| 1153 | usbhsf_dma_stop(pipe, fifo); |
| 1154 | usbhsf_dma_unmap(pkt); |
| 1155 | usbhsf_fifo_unselect(pipe, pipe->fifo); |
| 1156 | |
| 1157 | /* The driver can assume the rx transaction is always "done" */ |
| 1158 | *is_done = 1; |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done) |
| 1164 | { |
| 1165 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe); |
| 1166 | |
| 1167 | if (usbhs_get_dparam(priv, has_usb_dmac)) |
| 1168 | return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done); |
| 1169 | else |
| 1170 | return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done); |
| 1171 | } |
| 1172 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1173 | struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = { |
Yoshihiro Shimoda | ab330cf | 2015-03-12 15:35:20 +0900 | [diff] [blame] | 1174 | .prepare = usbhsf_dma_prepare_pop, |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1175 | .try_run = usbhsf_dma_try_pop, |
| 1176 | .dma_done = usbhsf_dma_pop_done |
| 1177 | }; |
| 1178 | |
| 1179 | /* |
| 1180 | * DMA setting |
| 1181 | */ |
| 1182 | static bool usbhsf_dma_filter(struct dma_chan *chan, void *param) |
| 1183 | { |
| 1184 | struct sh_dmae_slave *slave = param; |
| 1185 | |
| 1186 | /* |
| 1187 | * FIXME |
| 1188 | * |
| 1189 | * usbhs doesn't recognize id = 0 as valid DMA |
| 1190 | */ |
Guennadi Liakhovetski | f19b7e0 | 2012-05-09 17:09:19 +0200 | [diff] [blame] | 1191 | if (0 == slave->shdma_slave.slave_id) |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1192 | return false; |
| 1193 | |
| 1194 | chan->private = slave; |
| 1195 | |
| 1196 | return true; |
| 1197 | } |
| 1198 | |
| 1199 | static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo) |
| 1200 | { |
| 1201 | if (fifo->tx_chan) |
| 1202 | dma_release_channel(fifo->tx_chan); |
| 1203 | if (fifo->rx_chan) |
| 1204 | dma_release_channel(fifo->rx_chan); |
| 1205 | |
| 1206 | fifo->tx_chan = NULL; |
| 1207 | fifo->rx_chan = NULL; |
| 1208 | } |
| 1209 | |
Yoshihiro Shimoda | 6e3f53a | 2015-01-19 12:53:16 +0900 | [diff] [blame] | 1210 | static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo) |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1211 | { |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1212 | dma_cap_mask_t mask; |
| 1213 | |
| 1214 | dma_cap_zero(mask); |
| 1215 | dma_cap_set(DMA_SLAVE, mask); |
| 1216 | fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter, |
| 1217 | &fifo->tx_slave); |
| 1218 | |
| 1219 | dma_cap_zero(mask); |
| 1220 | dma_cap_set(DMA_SLAVE, mask); |
| 1221 | fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter, |
| 1222 | &fifo->rx_slave); |
Yoshihiro Shimoda | 6e3f53a | 2015-01-19 12:53:16 +0900 | [diff] [blame] | 1223 | } |
| 1224 | |
Yoshihiro Shimoda | 7a96b78 | 2015-03-12 15:35:18 +0900 | [diff] [blame] | 1225 | static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo, |
| 1226 | int channel) |
Yoshihiro Shimoda | abd2dbf | 2015-01-19 12:53:17 +0900 | [diff] [blame] | 1227 | { |
Yoshihiro Shimoda | 7a96b78 | 2015-03-12 15:35:18 +0900 | [diff] [blame] | 1228 | char name[16]; |
| 1229 | |
| 1230 | snprintf(name, sizeof(name), "tx%d", channel); |
| 1231 | fifo->tx_chan = dma_request_slave_channel_reason(dev, name); |
Yoshihiro Shimoda | ffb9da6 | 2015-01-30 15:58:23 +0900 | [diff] [blame] | 1232 | if (IS_ERR(fifo->tx_chan)) |
| 1233 | fifo->tx_chan = NULL; |
Yoshihiro Shimoda | 7a96b78 | 2015-03-12 15:35:18 +0900 | [diff] [blame] | 1234 | |
| 1235 | snprintf(name, sizeof(name), "rx%d", channel); |
| 1236 | fifo->rx_chan = dma_request_slave_channel_reason(dev, name); |
Yoshihiro Shimoda | ffb9da6 | 2015-01-30 15:58:23 +0900 | [diff] [blame] | 1237 | if (IS_ERR(fifo->rx_chan)) |
| 1238 | fifo->rx_chan = NULL; |
Yoshihiro Shimoda | abd2dbf | 2015-01-19 12:53:17 +0900 | [diff] [blame] | 1239 | } |
| 1240 | |
Yoshihiro Shimoda | 7a96b78 | 2015-03-12 15:35:18 +0900 | [diff] [blame] | 1241 | static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo, |
| 1242 | int channel) |
Yoshihiro Shimoda | 6e3f53a | 2015-01-19 12:53:16 +0900 | [diff] [blame] | 1243 | { |
| 1244 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1245 | |
Yoshihiro Shimoda | abd2dbf | 2015-01-19 12:53:17 +0900 | [diff] [blame] | 1246 | if (dev->of_node) |
Yoshihiro Shimoda | 7a96b78 | 2015-03-12 15:35:18 +0900 | [diff] [blame] | 1247 | usbhsf_dma_init_dt(dev, fifo, channel); |
Yoshihiro Shimoda | abd2dbf | 2015-01-19 12:53:17 +0900 | [diff] [blame] | 1248 | else |
| 1249 | usbhsf_dma_init_pdev(fifo); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1250 | |
| 1251 | if (fifo->tx_chan || fifo->rx_chan) |
Kuninori Morimoto | 4ce6880 | 2011-06-21 09:33:43 +0900 | [diff] [blame] | 1252 | dev_dbg(dev, "enable DMAEngine (%s%s%s)\n", |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1253 | fifo->name, |
| 1254 | fifo->tx_chan ? "[TX]" : " ", |
| 1255 | fifo->rx_chan ? "[RX]" : " "); |
| 1256 | } |
| 1257 | |
| 1258 | /* |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 1259 | * irq functions |
| 1260 | */ |
| 1261 | static int usbhsf_irq_empty(struct usbhs_priv *priv, |
| 1262 | struct usbhs_irq_state *irq_state) |
| 1263 | { |
| 1264 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 1265 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1266 | int i, ret; |
| 1267 | |
| 1268 | if (!irq_state->bempsts) { |
| 1269 | dev_err(dev, "debug %s !!\n", __func__); |
| 1270 | return -EIO; |
| 1271 | } |
| 1272 | |
| 1273 | dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts); |
| 1274 | |
| 1275 | /* |
| 1276 | * search interrupted "pipe" |
| 1277 | * not "uep". |
| 1278 | */ |
| 1279 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 1280 | if (!(irq_state->bempsts & (1 << i))) |
| 1281 | continue; |
| 1282 | |
Kuninori Morimoto | 51b8a02 | 2011-10-10 21:58:45 -0700 | [diff] [blame] | 1283 | ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 1284 | if (ret < 0) |
| 1285 | dev_err(dev, "irq_empty run_error %d : %d\n", i, ret); |
| 1286 | } |
| 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | static int usbhsf_irq_ready(struct usbhs_priv *priv, |
| 1292 | struct usbhs_irq_state *irq_state) |
| 1293 | { |
| 1294 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 1295 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1296 | int i, ret; |
| 1297 | |
| 1298 | if (!irq_state->brdysts) { |
| 1299 | dev_err(dev, "debug %s !!\n", __func__); |
| 1300 | return -EIO; |
| 1301 | } |
| 1302 | |
| 1303 | dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts); |
| 1304 | |
| 1305 | /* |
| 1306 | * search interrupted "pipe" |
| 1307 | * not "uep". |
| 1308 | */ |
| 1309 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 1310 | if (!(irq_state->brdysts & (1 << i))) |
| 1311 | continue; |
| 1312 | |
Kuninori Morimoto | 51b8a02 | 2011-10-10 21:58:45 -0700 | [diff] [blame] | 1313 | ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 1314 | if (ret < 0) |
| 1315 | dev_err(dev, "irq_ready run_error %d : %d\n", i, ret); |
| 1316 | } |
| 1317 | |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1321 | static void usbhsf_dma_complete(void *arg) |
| 1322 | { |
| 1323 | struct usbhs_pipe *pipe = arg; |
| 1324 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 1325 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1326 | int ret; |
| 1327 | |
Kuninori Morimoto | 51b8a02 | 2011-10-10 21:58:45 -0700 | [diff] [blame] | 1328 | ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1329 | if (ret < 0) |
| 1330 | dev_err(dev, "dma_complete run_error %d : %d\n", |
| 1331 | usbhs_pipe_number(pipe), ret); |
| 1332 | } |
| 1333 | |
Yoshihiro Shimoda | cdeb794 | 2014-11-04 10:05:45 +0900 | [diff] [blame] | 1334 | void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe) |
| 1335 | { |
| 1336 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 1337 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 1338 | |
| 1339 | /* clear DCP FIFO of transmission */ |
| 1340 | if (usbhsf_fifo_select(pipe, fifo, 1) < 0) |
| 1341 | return; |
| 1342 | usbhsf_fifo_clear(pipe, fifo); |
| 1343 | usbhsf_fifo_unselect(pipe, fifo); |
| 1344 | |
| 1345 | /* clear DCP FIFO of reception */ |
| 1346 | if (usbhsf_fifo_select(pipe, fifo, 0) < 0) |
| 1347 | return; |
| 1348 | usbhsf_fifo_clear(pipe, fifo); |
| 1349 | usbhsf_fifo_unselect(pipe, fifo); |
| 1350 | } |
| 1351 | |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 1352 | /* |
| 1353 | * fifo init |
| 1354 | */ |
| 1355 | void usbhs_fifo_init(struct usbhs_priv *priv) |
| 1356 | { |
| 1357 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 1358 | struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv); |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 1359 | struct usbhs_fifo *dfifo; |
| 1360 | int i; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 1361 | |
| 1362 | mod->irq_empty = usbhsf_irq_empty; |
| 1363 | mod->irq_ready = usbhsf_irq_ready; |
| 1364 | mod->irq_bempsts = 0; |
| 1365 | mod->irq_brdysts = 0; |
Kuninori Morimoto | d77e3f4 | 2011-06-06 14:18:50 +0900 | [diff] [blame] | 1366 | |
| 1367 | cfifo->pipe = NULL; |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 1368 | usbhs_for_each_dfifo(priv, dfifo, i) |
| 1369 | dfifo->pipe = NULL; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | void usbhs_fifo_quit(struct usbhs_priv *priv) |
| 1373 | { |
| 1374 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 1375 | |
| 1376 | mod->irq_empty = NULL; |
| 1377 | mod->irq_ready = NULL; |
| 1378 | mod->irq_bempsts = 0; |
| 1379 | mod->irq_brdysts = 0; |
| 1380 | } |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 1381 | |
Yoshihiro Shimoda | 53e734b | 2014-11-10 20:02:46 +0900 | [diff] [blame] | 1382 | #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \ |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 1383 | do { \ |
| 1384 | fifo = usbhsf_get_dnfifo(priv, channel); \ |
| 1385 | fifo->name = "D"#channel"FIFO"; \ |
Yoshihiro Shimoda | 53e734b | 2014-11-10 20:02:46 +0900 | [diff] [blame] | 1386 | fifo->port = fifo_port; \ |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 1387 | fifo->sel = D##channel##FIFOSEL; \ |
| 1388 | fifo->ctr = D##channel##FIFOCTR; \ |
| 1389 | fifo->tx_slave.shdma_slave.slave_id = \ |
| 1390 | usbhs_get_dparam(priv, d##channel##_tx_id); \ |
| 1391 | fifo->rx_slave.shdma_slave.slave_id = \ |
| 1392 | usbhs_get_dparam(priv, d##channel##_rx_id); \ |
Yoshihiro Shimoda | 7a96b78 | 2015-03-12 15:35:18 +0900 | [diff] [blame] | 1393 | usbhsf_dma_init(priv, fifo, channel); \ |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 1394 | } while (0) |
| 1395 | |
Yoshihiro Shimoda | 53e734b | 2014-11-10 20:02:46 +0900 | [diff] [blame] | 1396 | #define USBHS_DFIFO_INIT(priv, fifo, channel) \ |
| 1397 | __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO) |
| 1398 | #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \ |
| 1399 | __USBHS_DFIFO_INIT(priv, fifo, channel, 0) |
| 1400 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 1401 | int usbhs_fifo_probe(struct usbhs_priv *priv) |
| 1402 | { |
| 1403 | struct usbhs_fifo *fifo; |
| 1404 | |
| 1405 | /* CFIFO */ |
| 1406 | fifo = usbhsf_get_cfifo(priv); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1407 | fifo->name = "CFIFO"; |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 1408 | fifo->port = CFIFO; |
| 1409 | fifo->sel = CFIFOSEL; |
| 1410 | fifo->ctr = CFIFOCTR; |
| 1411 | |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 1412 | /* DFIFO */ |
| 1413 | USBHS_DFIFO_INIT(priv, fifo, 0); |
| 1414 | USBHS_DFIFO_INIT(priv, fifo, 1); |
Yoshihiro Shimoda | d3cf6a4 | 2014-11-10 20:02:47 +0900 | [diff] [blame] | 1415 | USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2); |
| 1416 | USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 1417 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 1418 | return 0; |
| 1419 | } |
| 1420 | |
| 1421 | void usbhs_fifo_remove(struct usbhs_priv *priv) |
| 1422 | { |
Yoshihiro Shimoda | 3a2634a | 2014-11-10 20:02:45 +0900 | [diff] [blame] | 1423 | struct usbhs_fifo *fifo; |
| 1424 | int i; |
| 1425 | |
| 1426 | usbhs_for_each_dfifo(priv, fifo, i) |
| 1427 | usbhsf_dma_quit(priv, fifo); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 1428 | } |