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)) |
| 23 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 24 | /* |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 25 | * packet info function |
| 26 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 27 | static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 28 | { |
| 29 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe); |
| 30 | struct device *dev = usbhs_priv_to_dev(priv); |
| 31 | |
| 32 | dev_err(dev, "null handler\n"); |
| 33 | |
| 34 | return -EINVAL; |
| 35 | } |
| 36 | |
| 37 | static struct usbhs_pkt_handle usbhsf_null_handler = { |
| 38 | .prepare = usbhsf_null_handle, |
| 39 | .try_run = usbhsf_null_handle, |
| 40 | }; |
| 41 | |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 42 | void usbhs_pkt_init(struct usbhs_pkt *pkt) |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 43 | { |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 44 | INIT_LIST_HEAD(&pkt->node); |
| 45 | } |
| 46 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 47 | void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt, |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 48 | struct usbhs_pkt_handle *handler, |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 49 | void *buf, int len, int zero) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 50 | { |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 51 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 52 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 53 | unsigned long flags; |
| 54 | |
| 55 | /******************** spin lock ********************/ |
| 56 | usbhs_lock(priv, flags); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 57 | |
| 58 | if (!handler) { |
| 59 | dev_err(dev, "no handler function\n"); |
| 60 | handler = &usbhsf_null_handler; |
| 61 | } |
| 62 | |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 63 | list_del_init(&pkt->node); |
| 64 | list_add_tail(&pkt->node, &pipe->list); |
| 65 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 66 | pkt->pipe = pipe; |
| 67 | pkt->buf = buf; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 68 | pkt->handler = handler; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 69 | pkt->length = len; |
| 70 | pkt->zero = zero; |
| 71 | pkt->actual = 0; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 72 | |
| 73 | usbhs_unlock(priv, flags); |
| 74 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 77 | static void __usbhsf_pkt_del(struct usbhs_pkt *pkt) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 78 | { |
| 79 | list_del_init(&pkt->node); |
| 80 | } |
| 81 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 82 | static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe) |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 83 | { |
| 84 | if (list_empty(&pipe->list)) |
| 85 | return NULL; |
| 86 | |
| 87 | return list_entry(pipe->list.next, struct usbhs_pkt, node); |
| 88 | } |
| 89 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 90 | struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt) |
| 91 | { |
| 92 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 93 | unsigned long flags; |
| 94 | |
| 95 | /******************** spin lock ********************/ |
| 96 | usbhs_lock(priv, flags); |
| 97 | |
| 98 | if (!pkt) |
| 99 | pkt = __usbhsf_pkt_get(pipe); |
| 100 | |
| 101 | if (pkt) |
| 102 | __usbhsf_pkt_del(pkt); |
| 103 | |
| 104 | usbhs_unlock(priv, flags); |
| 105 | /******************** spin unlock ******************/ |
| 106 | |
| 107 | return pkt; |
| 108 | } |
| 109 | |
| 110 | int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type) |
| 111 | { |
| 112 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 113 | struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); |
| 114 | struct usbhs_pkt *pkt; |
| 115 | struct device *dev = usbhs_priv_to_dev(priv); |
| 116 | int (*func)(struct usbhs_pkt *pkt, int *is_done); |
| 117 | unsigned long flags; |
| 118 | int ret = 0; |
| 119 | int is_done = 0; |
| 120 | |
| 121 | /******************** spin lock ********************/ |
| 122 | usbhs_lock(priv, flags); |
| 123 | |
| 124 | pkt = __usbhsf_pkt_get(pipe); |
| 125 | if (!pkt) |
| 126 | goto __usbhs_pkt_handler_end; |
| 127 | |
| 128 | switch (type) { |
| 129 | case USBHSF_PKT_PREPARE: |
| 130 | func = pkt->handler->prepare; |
| 131 | break; |
| 132 | case USBHSF_PKT_TRY_RUN: |
| 133 | func = pkt->handler->try_run; |
| 134 | break; |
| 135 | default: |
| 136 | dev_err(dev, "unknown pkt hander\n"); |
| 137 | goto __usbhs_pkt_handler_end; |
| 138 | } |
| 139 | |
| 140 | ret = func(pkt, &is_done); |
| 141 | |
| 142 | if (is_done) |
| 143 | __usbhsf_pkt_del(pkt); |
| 144 | |
| 145 | __usbhs_pkt_handler_end: |
| 146 | usbhs_unlock(priv, flags); |
| 147 | /******************** spin unlock ******************/ |
| 148 | |
| 149 | if (is_done) |
| 150 | info->done(pkt); |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 155 | /* |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 156 | * irq enable/disable function |
| 157 | */ |
| 158 | #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e) |
| 159 | #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e) |
| 160 | #define usbhsf_irq_callback_ctrl(pipe, status, enable) \ |
| 161 | ({ \ |
| 162 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \ |
| 163 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); \ |
| 164 | u16 status = (1 << usbhs_pipe_number(pipe)); \ |
| 165 | if (!mod) \ |
| 166 | return; \ |
| 167 | if (enable) \ |
| 168 | mod->irq_##status |= status; \ |
| 169 | else \ |
| 170 | mod->irq_##status &= ~status; \ |
| 171 | usbhs_irq_callback_update(priv, mod); \ |
| 172 | }) |
| 173 | |
| 174 | static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable) |
| 175 | { |
| 176 | /* |
| 177 | * And DCP pipe can NOT use "ready interrupt" for "send" |
| 178 | * it should use "empty" interrupt. |
| 179 | * see |
| 180 | * "Operation" - "Interrupt Function" - "BRDY Interrupt" |
| 181 | * |
| 182 | * on the other hand, normal pipe can use "ready interrupt" for "send" |
| 183 | * even though it is single/double buffer |
| 184 | */ |
| 185 | if (usbhs_pipe_is_dcp(pipe)) |
| 186 | usbhsf_irq_empty_ctrl(pipe, enable); |
| 187 | else |
| 188 | usbhsf_irq_ready_ctrl(pipe, enable); |
| 189 | } |
| 190 | |
| 191 | static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable) |
| 192 | { |
| 193 | usbhsf_irq_ready_ctrl(pipe, enable); |
| 194 | } |
| 195 | |
| 196 | /* |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 197 | * FIFO ctrl |
| 198 | */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 199 | static void usbhsf_send_terminator(struct usbhs_pipe *pipe, |
| 200 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 201 | { |
| 202 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 203 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 204 | usbhs_bset(priv, fifo->ctr, BVAL, BVAL); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 205 | } |
| 206 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 207 | static int usbhsf_fifo_barrier(struct usbhs_priv *priv, |
| 208 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 209 | { |
| 210 | int timeout = 1024; |
| 211 | |
| 212 | do { |
| 213 | /* The FIFO port is accessible */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 214 | if (usbhs_read(priv, fifo->ctr) & FRDY) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 215 | return 0; |
| 216 | |
| 217 | udelay(10); |
| 218 | } while (timeout--); |
| 219 | |
| 220 | return -EBUSY; |
| 221 | } |
| 222 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 223 | static void usbhsf_fifo_clear(struct usbhs_pipe *pipe, |
| 224 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 225 | { |
| 226 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 227 | |
| 228 | if (!usbhs_pipe_is_dcp(pipe)) |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 229 | usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 230 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 231 | usbhs_write(priv, fifo->ctr, BCLR); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 232 | } |
| 233 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 234 | static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv, |
| 235 | struct usbhs_fifo *fifo) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 236 | { |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 237 | return usbhs_read(priv, fifo->ctr) & DTLN_MASK; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 238 | } |
| 239 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 240 | static int usbhsf_fifo_select(struct usbhs_pipe *pipe, |
| 241 | struct usbhs_fifo *fifo, |
| 242 | int write) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 243 | { |
| 244 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 245 | struct device *dev = usbhs_priv_to_dev(priv); |
| 246 | int timeout = 1024; |
| 247 | u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */ |
| 248 | u16 base = usbhs_pipe_number(pipe); /* CURPIPE */ |
| 249 | |
| 250 | if (usbhs_pipe_is_dcp(pipe)) |
| 251 | base |= (1 == write) << 5; /* ISEL */ |
| 252 | |
| 253 | /* "base" will be used below */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 254 | usbhs_write(priv, fifo->sel, base | MBW_32); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 255 | |
| 256 | /* check ISEL and CURPIPE value */ |
| 257 | while (timeout--) { |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 258 | if (base == (mask & usbhs_read(priv, fifo->sel))) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 259 | return 0; |
| 260 | udelay(10); |
| 261 | } |
| 262 | |
| 263 | dev_err(dev, "fifo select error\n"); |
| 264 | |
| 265 | return -EIO; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * PIO fifo functions |
| 270 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 271 | static int usbhsf_try_push(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 272 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 273 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 274 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 275 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 276 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 277 | void __iomem *addr = priv->base + fifo->port; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 278 | u8 *buf; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 279 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
| 280 | int total_len; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 281 | int i, ret, len; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 282 | int is_short; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 283 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 284 | ret = usbhsf_fifo_select(pipe, fifo, 1); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 285 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 286 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 287 | |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 288 | ret = usbhs_pipe_is_accessible(pipe); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 289 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 290 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 291 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 292 | ret = usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 293 | if (ret < 0) |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 294 | goto usbhs_fifo_write_busy; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 295 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 296 | buf = pkt->buf + pkt->actual; |
| 297 | len = pkt->length - pkt->actual; |
| 298 | len = min(len, maxp); |
| 299 | total_len = len; |
| 300 | is_short = total_len < maxp; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * FIXME |
| 304 | * |
| 305 | * 32-bit access only |
| 306 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 307 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 308 | iowrite32_rep(addr, buf, len / 4); |
| 309 | len %= 4; |
| 310 | buf += total_len - len; |
| 311 | } |
| 312 | |
| 313 | /* the rest operation */ |
| 314 | for (i = 0; i < len; i++) |
| 315 | iowrite8(buf[i], addr + (0x03 - (i & 0x03))); |
| 316 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 317 | /* |
| 318 | * variable update |
| 319 | */ |
| 320 | pkt->actual += total_len; |
| 321 | |
| 322 | if (pkt->actual < pkt->length) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 323 | *is_done = 0; /* there are remainder data */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 324 | else if (is_short) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 325 | *is_done = 1; /* short packet */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 326 | else |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 327 | *is_done = !pkt->zero; /* send zero packet ? */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * pipe/irq handling |
| 331 | */ |
| 332 | if (is_short) |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 333 | usbhsf_send_terminator(pipe, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 334 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 335 | usbhsf_tx_irq_ctrl(pipe, !*is_done); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 336 | usbhs_pipe_enable(pipe); |
| 337 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 338 | dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n", |
| 339 | usbhs_pipe_number(pipe), |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 340 | pkt->length, pkt->actual, *is_done, pkt->zero); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * Transmission end |
| 344 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 345 | if (*is_done) { |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 346 | if (usbhs_pipe_is_dcp(pipe)) |
| 347 | usbhs_dcp_control_transfer_done(pipe); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return 0; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 351 | |
| 352 | usbhs_fifo_write_busy: |
| 353 | /* |
| 354 | * pipe is busy. |
| 355 | * retry in interrupt |
| 356 | */ |
| 357 | usbhsf_tx_irq_ctrl(pipe, 1); |
| 358 | |
| 359 | return ret; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 360 | } |
| 361 | |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 362 | struct usbhs_pkt_handle usbhs_fifo_push_handler = { |
| 363 | .prepare = usbhsf_try_push, |
| 364 | .try_run = usbhsf_try_push, |
| 365 | }; |
| 366 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 367 | static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 368 | { |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 369 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 370 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
| 371 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 372 | int ret; |
| 373 | |
| 374 | /* |
| 375 | * select pipe and enable it to prepare packet receive |
| 376 | */ |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 377 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 378 | if (ret < 0) |
| 379 | return ret; |
| 380 | |
| 381 | usbhs_pipe_enable(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 382 | usbhsf_rx_irq_ctrl(pipe, 1); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 383 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 384 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 385 | } |
| 386 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 387 | static int usbhsf_try_pop(struct usbhs_pkt *pkt, int *is_done) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 388 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 389 | struct usbhs_pipe *pipe = pkt->pipe; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 390 | struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 391 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 392 | struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */ |
| 393 | void __iomem *addr = priv->base + fifo->port; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 394 | u8 *buf; |
| 395 | u32 data = 0; |
| 396 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 397 | int rcv_len, len; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 398 | int i, ret; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 399 | int total_len = 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 400 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 401 | ret = usbhsf_fifo_select(pipe, fifo, 0); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 402 | if (ret < 0) |
| 403 | return ret; |
| 404 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 405 | ret = usbhsf_fifo_barrier(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 406 | if (ret < 0) |
| 407 | return ret; |
| 408 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 409 | rcv_len = usbhsf_fifo_rcv_len(priv, fifo); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 410 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 411 | buf = pkt->buf + pkt->actual; |
| 412 | len = pkt->length - pkt->actual; |
| 413 | len = min(len, rcv_len); |
| 414 | total_len = len; |
| 415 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 416 | /* |
| 417 | * Buffer clear if Zero-Length packet |
| 418 | * |
| 419 | * see |
| 420 | * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function" |
| 421 | */ |
| 422 | if (0 == rcv_len) { |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 423 | usbhsf_fifo_clear(pipe, fifo); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 424 | goto usbhs_fifo_read_end; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 425 | } |
| 426 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 427 | /* |
| 428 | * FIXME |
| 429 | * |
| 430 | * 32-bit access only |
| 431 | */ |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 432 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 433 | ioread32_rep(addr, buf, len / 4); |
| 434 | len %= 4; |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 435 | buf += total_len - len; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* the rest operation */ |
| 439 | for (i = 0; i < len; i++) { |
| 440 | if (!(i & 0x03)) |
| 441 | data = ioread32(addr); |
| 442 | |
| 443 | buf[i] = (data >> ((i & 0x03) * 8)) & 0xff; |
| 444 | } |
| 445 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 446 | pkt->actual += total_len; |
| 447 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 448 | usbhs_fifo_read_end: |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 449 | if ((pkt->actual == pkt->length) || /* receive all data */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 450 | (total_len < maxp)) { /* short packet */ |
| 451 | *is_done = 1; |
| 452 | usbhsf_rx_irq_ctrl(pipe, 0); |
| 453 | usbhs_pipe_disable(pipe); |
| 454 | } |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 455 | |
| 456 | dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n", |
| 457 | usbhs_pipe_number(pipe), |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 458 | pkt->length, pkt->actual, *is_done, pkt->zero); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 459 | |
| 460 | return 0; |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 461 | } |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 462 | |
| 463 | struct usbhs_pkt_handle usbhs_fifo_pop_handler = { |
| 464 | .prepare = usbhsf_prepare_pop, |
| 465 | .try_run = usbhsf_try_pop, |
| 466 | }; |
| 467 | |
| 468 | /* |
| 469 | * handler function |
| 470 | */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 471 | 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] | 472 | { |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 473 | usbhs_dcp_control_transfer_done(pkt->pipe); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 474 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 475 | *is_done = 1; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = { |
| 481 | .prepare = usbhsf_ctrl_stage_end, |
| 482 | .try_run = usbhsf_ctrl_stage_end, |
| 483 | }; |
| 484 | |
| 485 | /* |
| 486 | * irq functions |
| 487 | */ |
| 488 | static int usbhsf_irq_empty(struct usbhs_priv *priv, |
| 489 | struct usbhs_irq_state *irq_state) |
| 490 | { |
| 491 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 492 | struct device *dev = usbhs_priv_to_dev(priv); |
| 493 | int i, ret; |
| 494 | |
| 495 | if (!irq_state->bempsts) { |
| 496 | dev_err(dev, "debug %s !!\n", __func__); |
| 497 | return -EIO; |
| 498 | } |
| 499 | |
| 500 | dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts); |
| 501 | |
| 502 | /* |
| 503 | * search interrupted "pipe" |
| 504 | * not "uep". |
| 505 | */ |
| 506 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 507 | if (!(irq_state->bempsts & (1 << i))) |
| 508 | continue; |
| 509 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 510 | ret = usbhs_pkt_run(pipe); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 511 | if (ret < 0) |
| 512 | dev_err(dev, "irq_empty run_error %d : %d\n", i, ret); |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int usbhsf_irq_ready(struct usbhs_priv *priv, |
| 519 | struct usbhs_irq_state *irq_state) |
| 520 | { |
| 521 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 522 | struct device *dev = usbhs_priv_to_dev(priv); |
| 523 | int i, ret; |
| 524 | |
| 525 | if (!irq_state->brdysts) { |
| 526 | dev_err(dev, "debug %s !!\n", __func__); |
| 527 | return -EIO; |
| 528 | } |
| 529 | |
| 530 | dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts); |
| 531 | |
| 532 | /* |
| 533 | * search interrupted "pipe" |
| 534 | * not "uep". |
| 535 | */ |
| 536 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 537 | if (!(irq_state->brdysts & (1 << i))) |
| 538 | continue; |
| 539 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 540 | ret = usbhs_pkt_run(pipe); |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 541 | if (ret < 0) |
| 542 | dev_err(dev, "irq_ready run_error %d : %d\n", i, ret); |
| 543 | } |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * fifo init |
| 550 | */ |
| 551 | void usbhs_fifo_init(struct usbhs_priv *priv) |
| 552 | { |
| 553 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 554 | |
| 555 | mod->irq_empty = usbhsf_irq_empty; |
| 556 | mod->irq_ready = usbhsf_irq_ready; |
| 557 | mod->irq_bempsts = 0; |
| 558 | mod->irq_brdysts = 0; |
| 559 | } |
| 560 | |
| 561 | void usbhs_fifo_quit(struct usbhs_priv *priv) |
| 562 | { |
| 563 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 564 | |
| 565 | mod->irq_empty = NULL; |
| 566 | mod->irq_ready = NULL; |
| 567 | mod->irq_bempsts = 0; |
| 568 | mod->irq_brdysts = 0; |
| 569 | } |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame^] | 570 | |
| 571 | int usbhs_fifo_probe(struct usbhs_priv *priv) |
| 572 | { |
| 573 | struct usbhs_fifo *fifo; |
| 574 | |
| 575 | /* CFIFO */ |
| 576 | fifo = usbhsf_get_cfifo(priv); |
| 577 | fifo->port = CFIFO; |
| 578 | fifo->sel = CFIFOSEL; |
| 579 | fifo->ctr = CFIFOCTR; |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | void usbhs_fifo_remove(struct usbhs_priv *priv) |
| 585 | { |
| 586 | } |