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