Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * driver/usb/gadget/imx_udc.c |
| 3 | * |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 4 | * Copyright (C) 2005 Mike Lee <eemike@gmail.com> |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 5 | * Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/io.h> |
| 26 | #include <linux/irq.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <linux/dma-mapping.h> |
| 29 | #include <linux/clk.h> |
| 30 | #include <linux/delay.h> |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 31 | #include <linux/timer.h> |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 32 | |
| 33 | #include <linux/usb/ch9.h> |
| 34 | #include <linux/usb/gadget.h> |
| 35 | |
| 36 | #include <mach/usb.h> |
| 37 | #include <mach/hardware.h> |
| 38 | |
| 39 | #include "imx_udc.h" |
| 40 | |
| 41 | static const char driver_name[] = "imx_udc"; |
| 42 | static const char ep0name[] = "ep0"; |
| 43 | |
| 44 | void ep0_chg_stat(const char *label, struct imx_udc_struct *imx_usb, |
| 45 | enum ep0_state stat); |
| 46 | |
| 47 | /******************************************************************************* |
| 48 | * IMX UDC hardware related functions |
| 49 | ******************************************************************************* |
| 50 | */ |
| 51 | |
| 52 | void imx_udc_enable(struct imx_udc_struct *imx_usb) |
| 53 | { |
| 54 | int temp = __raw_readl(imx_usb->base + USB_CTRL); |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 55 | __raw_writel(temp | CTRL_FE_ENA | CTRL_AFE_ENA, |
| 56 | imx_usb->base + USB_CTRL); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 57 | imx_usb->gadget.speed = USB_SPEED_FULL; |
| 58 | } |
| 59 | |
| 60 | void imx_udc_disable(struct imx_udc_struct *imx_usb) |
| 61 | { |
| 62 | int temp = __raw_readl(imx_usb->base + USB_CTRL); |
| 63 | |
| 64 | __raw_writel(temp & ~(CTRL_FE_ENA | CTRL_AFE_ENA), |
| 65 | imx_usb->base + USB_CTRL); |
| 66 | |
| 67 | ep0_chg_stat(__func__, imx_usb, EP0_IDLE); |
| 68 | imx_usb->gadget.speed = USB_SPEED_UNKNOWN; |
| 69 | } |
| 70 | |
| 71 | void imx_udc_reset(struct imx_udc_struct *imx_usb) |
| 72 | { |
| 73 | int temp = __raw_readl(imx_usb->base + USB_ENAB); |
| 74 | |
| 75 | /* set RST bit */ |
| 76 | __raw_writel(temp | ENAB_RST, imx_usb->base + USB_ENAB); |
| 77 | |
| 78 | /* wait RST bit to clear */ |
| 79 | do {} while (__raw_readl(imx_usb->base + USB_ENAB) & ENAB_RST); |
| 80 | |
| 81 | /* wait CFG bit to assert */ |
| 82 | do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG)); |
| 83 | |
| 84 | /* udc module is now ready */ |
| 85 | } |
| 86 | |
| 87 | void imx_udc_config(struct imx_udc_struct *imx_usb) |
| 88 | { |
| 89 | u8 ep_conf[5]; |
| 90 | u8 i, j, cfg; |
| 91 | struct imx_ep_struct *imx_ep; |
| 92 | |
| 93 | /* wait CFG bit to assert */ |
| 94 | do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG)); |
| 95 | |
| 96 | /* Download the endpoint buffer for endpoint 0. */ |
| 97 | for (j = 0; j < 5; j++) { |
| 98 | i = (j == 2 ? imx_usb->imx_ep[0].fifosize : 0x00); |
| 99 | __raw_writeb(i, imx_usb->base + USB_DDAT); |
| 100 | do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_BSY); |
| 101 | } |
| 102 | |
| 103 | /* Download the endpoint buffers for endpoints 1-5. |
| 104 | * We specify two configurations, one interface |
| 105 | */ |
| 106 | for (cfg = 1; cfg < 3; cfg++) { |
| 107 | for (i = 1; i < IMX_USB_NB_EP; i++) { |
| 108 | imx_ep = &imx_usb->imx_ep[i]; |
| 109 | /* EP no | Config no */ |
| 110 | ep_conf[0] = (i << 4) | (cfg << 2); |
| 111 | /* Type | Direction */ |
| 112 | ep_conf[1] = (imx_ep->bmAttributes << 3) | |
| 113 | (EP_DIR(imx_ep) << 2); |
| 114 | /* Max packet size */ |
| 115 | ep_conf[2] = imx_ep->fifosize; |
| 116 | /* TRXTYP */ |
| 117 | ep_conf[3] = 0xC0; |
| 118 | /* FIFO no */ |
| 119 | ep_conf[4] = i; |
| 120 | |
| 121 | D_INI(imx_usb->dev, |
| 122 | "<%s> ep%d_conf[%d]:" |
| 123 | "[%02x-%02x-%02x-%02x-%02x]\n", |
| 124 | __func__, i, cfg, |
| 125 | ep_conf[0], ep_conf[1], ep_conf[2], |
| 126 | ep_conf[3], ep_conf[4]); |
| 127 | |
| 128 | for (j = 0; j < 5; j++) { |
| 129 | __raw_writeb(ep_conf[j], |
| 130 | imx_usb->base + USB_DDAT); |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 131 | do {} while (__raw_readl(imx_usb->base |
| 132 | + USB_DADR) |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 133 | & DADR_BSY); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* wait CFG bit to clear */ |
| 139 | do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG); |
| 140 | } |
| 141 | |
| 142 | void imx_udc_init_irq(struct imx_udc_struct *imx_usb) |
| 143 | { |
| 144 | int i; |
| 145 | |
| 146 | /* Mask and clear all irqs */ |
| 147 | __raw_writel(0xFFFFFFFF, imx_usb->base + USB_MASK); |
| 148 | __raw_writel(0xFFFFFFFF, imx_usb->base + USB_INTR); |
| 149 | for (i = 0; i < IMX_USB_NB_EP; i++) { |
| 150 | __raw_writel(0x1FF, imx_usb->base + USB_EP_MASK(i)); |
| 151 | __raw_writel(0x1FF, imx_usb->base + USB_EP_INTR(i)); |
| 152 | } |
| 153 | |
| 154 | /* Enable USB irqs */ |
| 155 | __raw_writel(INTR_MSOF | INTR_FRAME_MATCH, imx_usb->base + USB_MASK); |
| 156 | |
| 157 | /* Enable EP0 irqs */ |
| 158 | __raw_writel(0x1FF & ~(EPINTR_DEVREQ | EPINTR_MDEVREQ | EPINTR_EOT |
| 159 | | EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL), |
| 160 | imx_usb->base + USB_EP_MASK(0)); |
| 161 | } |
| 162 | |
| 163 | void imx_udc_init_ep(struct imx_udc_struct *imx_usb) |
| 164 | { |
| 165 | int i, max, temp; |
| 166 | struct imx_ep_struct *imx_ep; |
| 167 | for (i = 0; i < IMX_USB_NB_EP; i++) { |
| 168 | imx_ep = &imx_usb->imx_ep[i]; |
| 169 | switch (imx_ep->fifosize) { |
| 170 | case 8: |
| 171 | max = 0; |
| 172 | break; |
| 173 | case 16: |
| 174 | max = 1; |
| 175 | break; |
| 176 | case 32: |
| 177 | max = 2; |
| 178 | break; |
| 179 | case 64: |
| 180 | max = 3; |
| 181 | break; |
| 182 | default: |
| 183 | max = 1; |
| 184 | break; |
| 185 | } |
| 186 | temp = (EP_DIR(imx_ep) << 7) | (max << 5) |
| 187 | | (imx_ep->bmAttributes << 3); |
| 188 | __raw_writel(temp, imx_usb->base + USB_EP_STAT(i)); |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 189 | __raw_writel(temp | EPSTAT_FLUSH, |
| 190 | imx_usb->base + USB_EP_STAT(i)); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 191 | D_INI(imx_usb->dev, "<%s> ep%d_stat %08x\n", __func__, i, |
| 192 | __raw_readl(imx_usb->base + USB_EP_STAT(i))); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void imx_udc_init_fifo(struct imx_udc_struct *imx_usb) |
| 197 | { |
| 198 | int i, temp; |
| 199 | struct imx_ep_struct *imx_ep; |
| 200 | for (i = 0; i < IMX_USB_NB_EP; i++) { |
| 201 | imx_ep = &imx_usb->imx_ep[i]; |
| 202 | |
| 203 | /* Fifo control */ |
| 204 | temp = EP_DIR(imx_ep) ? 0x0B000000 : 0x0F000000; |
| 205 | __raw_writel(temp, imx_usb->base + USB_EP_FCTRL(i)); |
| 206 | D_INI(imx_usb->dev, "<%s> ep%d_fctrl %08x\n", __func__, i, |
| 207 | __raw_readl(imx_usb->base + USB_EP_FCTRL(i))); |
| 208 | |
| 209 | /* Fifo alarm */ |
| 210 | temp = (i ? imx_ep->fifosize / 2 : 0); |
| 211 | __raw_writel(temp, imx_usb->base + USB_EP_FALRM(i)); |
| 212 | D_INI(imx_usb->dev, "<%s> ep%d_falrm %08x\n", __func__, i, |
| 213 | __raw_readl(imx_usb->base + USB_EP_FALRM(i))); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void imx_udc_init(struct imx_udc_struct *imx_usb) |
| 218 | { |
| 219 | /* Reset UDC */ |
| 220 | imx_udc_reset(imx_usb); |
| 221 | |
| 222 | /* Download config to enpoint buffer */ |
| 223 | imx_udc_config(imx_usb); |
| 224 | |
| 225 | /* Setup interrups */ |
| 226 | imx_udc_init_irq(imx_usb); |
| 227 | |
| 228 | /* Setup endpoints */ |
| 229 | imx_udc_init_ep(imx_usb); |
| 230 | |
| 231 | /* Setup fifos */ |
| 232 | imx_udc_init_fifo(imx_usb); |
| 233 | } |
| 234 | |
| 235 | void imx_ep_irq_enable(struct imx_ep_struct *imx_ep) |
| 236 | { |
| 237 | |
| 238 | int i = EP_NO(imx_ep); |
| 239 | |
| 240 | __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i)); |
| 241 | __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i)); |
| 242 | __raw_writel(0x1FF & ~(EPINTR_EOT | EPINTR_EOF), |
| 243 | imx_ep->imx_usb->base + USB_EP_MASK(i)); |
| 244 | } |
| 245 | |
| 246 | void imx_ep_irq_disable(struct imx_ep_struct *imx_ep) |
| 247 | { |
| 248 | |
| 249 | int i = EP_NO(imx_ep); |
| 250 | |
| 251 | __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i)); |
| 252 | __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i)); |
| 253 | } |
| 254 | |
| 255 | int imx_ep_empty(struct imx_ep_struct *imx_ep) |
| 256 | { |
| 257 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; |
| 258 | |
| 259 | return __raw_readl(imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep))) |
| 260 | & FSTAT_EMPTY; |
| 261 | } |
| 262 | |
| 263 | unsigned imx_fifo_bcount(struct imx_ep_struct *imx_ep) |
| 264 | { |
| 265 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; |
| 266 | |
| 267 | return (__raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))) |
| 268 | & EPSTAT_BCOUNT) >> 16; |
| 269 | } |
| 270 | |
| 271 | void imx_flush(struct imx_ep_struct *imx_ep) |
| 272 | { |
| 273 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; |
| 274 | |
| 275 | int temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))); |
| 276 | __raw_writel(temp | EPSTAT_FLUSH, |
| 277 | imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))); |
| 278 | } |
| 279 | |
| 280 | void imx_ep_stall(struct imx_ep_struct *imx_ep) |
| 281 | { |
| 282 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; |
| 283 | int temp, i; |
| 284 | |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 285 | D_ERR(imx_usb->dev, |
| 286 | "<%s> Forced stall on %s\n", __func__, imx_ep->ep.name); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 287 | |
| 288 | imx_flush(imx_ep); |
| 289 | |
| 290 | /* Special care for ep0 */ |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 291 | if (!EP_NO(imx_ep)) { |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 292 | temp = __raw_readl(imx_usb->base + USB_CTRL); |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 293 | __raw_writel(temp | CTRL_CMDOVER | CTRL_CMDERROR, |
| 294 | imx_usb->base + USB_CTRL); |
| 295 | do { } while (__raw_readl(imx_usb->base + USB_CTRL) |
| 296 | & CTRL_CMDOVER); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 297 | temp = __raw_readl(imx_usb->base + USB_CTRL); |
| 298 | __raw_writel(temp & ~CTRL_CMDERROR, imx_usb->base + USB_CTRL); |
| 299 | } |
| 300 | else { |
| 301 | temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))); |
| 302 | __raw_writel(temp | EPSTAT_STALL, |
| 303 | imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))); |
| 304 | |
| 305 | for (i = 0; i < 100; i ++) { |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 306 | temp = __raw_readl(imx_usb->base |
| 307 | + USB_EP_STAT(EP_NO(imx_ep))); |
Roel Kluin | 0df2479 | 2009-01-17 16:52:17 +0100 | [diff] [blame] | 308 | if (!(temp & EPSTAT_STALL)) |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 309 | break; |
| 310 | udelay(20); |
| 311 | } |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 312 | if (i == 100) |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 313 | D_ERR(imx_usb->dev, "<%s> Non finished stall on %s\n", |
| 314 | __func__, imx_ep->ep.name); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | static int imx_udc_get_frame(struct usb_gadget *_gadget) |
| 319 | { |
| 320 | struct imx_udc_struct *imx_usb = container_of(_gadget, |
| 321 | struct imx_udc_struct, gadget); |
| 322 | |
| 323 | return __raw_readl(imx_usb->base + USB_FRAME) & 0x7FF; |
| 324 | } |
| 325 | |
| 326 | static int imx_udc_wakeup(struct usb_gadget *_gadget) |
| 327 | { |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /******************************************************************************* |
| 332 | * USB request control functions |
| 333 | ******************************************************************************* |
| 334 | */ |
| 335 | |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 336 | static void ep_add_request(struct imx_ep_struct *imx_ep, |
| 337 | struct imx_request *req) |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 338 | { |
| 339 | if (unlikely(!req)) |
| 340 | return; |
| 341 | |
| 342 | req->in_use = 1; |
| 343 | list_add_tail(&req->queue, &imx_ep->queue); |
| 344 | } |
| 345 | |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 346 | static void ep_del_request(struct imx_ep_struct *imx_ep, |
| 347 | struct imx_request *req) |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 348 | { |
| 349 | if (unlikely(!req)) |
| 350 | return; |
| 351 | |
| 352 | list_del_init(&req->queue); |
| 353 | req->in_use = 0; |
| 354 | } |
| 355 | |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 356 | static void done(struct imx_ep_struct *imx_ep, |
| 357 | struct imx_request *req, int status) |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 358 | { |
| 359 | ep_del_request(imx_ep, req); |
| 360 | |
| 361 | if (likely(req->req.status == -EINPROGRESS)) |
| 362 | req->req.status = status; |
| 363 | else |
| 364 | status = req->req.status; |
| 365 | |
| 366 | if (status && status != -ESHUTDOWN) |
| 367 | D_ERR(imx_ep->imx_usb->dev, |
| 368 | "<%s> complete %s req %p stat %d len %u/%u\n", __func__, |
| 369 | imx_ep->ep.name, &req->req, status, |
| 370 | req->req.actual, req->req.length); |
| 371 | |
| 372 | req->req.complete(&imx_ep->ep, &req->req); |
| 373 | } |
| 374 | |
| 375 | static void nuke(struct imx_ep_struct *imx_ep, int status) |
| 376 | { |
| 377 | struct imx_request *req; |
| 378 | |
| 379 | while (!list_empty(&imx_ep->queue)) { |
| 380 | req = list_entry(imx_ep->queue.next, struct imx_request, queue); |
| 381 | done(imx_ep, req, status); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /******************************************************************************* |
| 386 | * Data tansfer over USB functions |
| 387 | ******************************************************************************* |
| 388 | */ |
| 389 | static int read_packet(struct imx_ep_struct *imx_ep, struct imx_request *req) |
| 390 | { |
| 391 | u8 *buf; |
| 392 | int bytes_ep, bufferspace, count, i; |
| 393 | |
| 394 | bytes_ep = imx_fifo_bcount(imx_ep); |
| 395 | bufferspace = req->req.length - req->req.actual; |
| 396 | |
| 397 | buf = req->req.buf + req->req.actual; |
| 398 | prefetchw(buf); |
| 399 | |
| 400 | if (unlikely(imx_ep_empty(imx_ep))) |
| 401 | count = 0; /* zlp */ |
| 402 | else |
| 403 | count = min(bytes_ep, bufferspace); |
| 404 | |
| 405 | for (i = count; i > 0; i--) |
| 406 | *buf++ = __raw_readb(imx_ep->imx_usb->base |
| 407 | + USB_EP_FDAT0(EP_NO(imx_ep))); |
| 408 | req->req.actual += count; |
| 409 | |
| 410 | return count; |
| 411 | } |
| 412 | |
| 413 | static int write_packet(struct imx_ep_struct *imx_ep, struct imx_request *req) |
| 414 | { |
| 415 | u8 *buf; |
| 416 | int length, count, temp; |
| 417 | |
| 418 | buf = req->req.buf + req->req.actual; |
| 419 | prefetch(buf); |
| 420 | |
| 421 | length = min(req->req.length - req->req.actual, (u32)imx_ep->fifosize); |
| 422 | |
| 423 | if (imx_fifo_bcount(imx_ep) + length > imx_ep->fifosize) { |
| 424 | D_TRX(imx_ep->imx_usb->dev, "<%s> packet overfill %s fifo\n", |
| 425 | __func__, imx_ep->ep.name); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | req->req.actual += length; |
| 430 | count = length; |
| 431 | |
| 432 | if (!count && req->req.zero) { /* zlp */ |
| 433 | temp = __raw_readl(imx_ep->imx_usb->base |
| 434 | + USB_EP_STAT(EP_NO(imx_ep))); |
| 435 | __raw_writel(temp | EPSTAT_ZLPS, imx_ep->imx_usb->base |
| 436 | + USB_EP_STAT(EP_NO(imx_ep))); |
| 437 | D_TRX(imx_ep->imx_usb->dev, "<%s> zero packet\n", __func__); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | while (count--) { |
| 442 | if (count == 0) { /* last byte */ |
| 443 | temp = __raw_readl(imx_ep->imx_usb->base |
| 444 | + USB_EP_FCTRL(EP_NO(imx_ep))); |
| 445 | __raw_writel(temp | FCTRL_WFR, imx_ep->imx_usb->base |
| 446 | + USB_EP_FCTRL(EP_NO(imx_ep))); |
| 447 | } |
| 448 | __raw_writeb(*buf++, |
| 449 | imx_ep->imx_usb->base + USB_EP_FDAT0(EP_NO(imx_ep))); |
| 450 | } |
| 451 | |
| 452 | return length; |
| 453 | } |
| 454 | |
| 455 | static int read_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req) |
| 456 | { |
| 457 | int bytes = 0, |
| 458 | count, |
| 459 | completed = 0; |
| 460 | |
| 461 | while (__raw_readl(imx_ep->imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep))) |
| 462 | & FSTAT_FR) { |
| 463 | count = read_packet(imx_ep, req); |
| 464 | bytes += count; |
| 465 | |
| 466 | completed = (count != imx_ep->fifosize); |
| 467 | if (completed || req->req.actual == req->req.length) { |
| 468 | completed = 1; |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (completed || !req->req.length) { |
| 474 | done(imx_ep, req, 0); |
| 475 | D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n", |
| 476 | __func__, imx_ep->ep.name, req, |
| 477 | completed ? "completed" : "not completed"); |
| 478 | if (!EP_NO(imx_ep)) |
| 479 | ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE); |
| 480 | } |
| 481 | |
| 482 | D_TRX(imx_ep->imx_usb->dev, "<%s> bytes read: %d\n", __func__, bytes); |
| 483 | |
| 484 | return completed; |
| 485 | } |
| 486 | |
| 487 | static int write_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req) |
| 488 | { |
| 489 | int bytes = 0, |
| 490 | count, |
| 491 | completed = 0; |
| 492 | |
| 493 | while (!completed) { |
| 494 | count = write_packet(imx_ep, req); |
| 495 | if (count < 0) |
| 496 | break; /* busy */ |
| 497 | bytes += count; |
| 498 | |
| 499 | /* last packet "must be" short (or a zlp) */ |
| 500 | completed = (count != imx_ep->fifosize); |
| 501 | |
| 502 | if (unlikely(completed)) { |
| 503 | done(imx_ep, req, 0); |
| 504 | D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n", |
| 505 | __func__, imx_ep->ep.name, req, |
| 506 | completed ? "completed" : "not completed"); |
| 507 | if (!EP_NO(imx_ep)) |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 508 | ep0_chg_stat(__func__, |
| 509 | imx_ep->imx_usb, EP0_IDLE); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | D_TRX(imx_ep->imx_usb->dev, "<%s> bytes sent: %d\n", __func__, bytes); |
| 514 | |
| 515 | return completed; |
| 516 | } |
| 517 | |
| 518 | /******************************************************************************* |
| 519 | * Endpoint handlers |
| 520 | ******************************************************************************* |
| 521 | */ |
| 522 | static int handle_ep(struct imx_ep_struct *imx_ep) |
| 523 | { |
| 524 | struct imx_request *req; |
| 525 | int completed = 0; |
| 526 | |
| 527 | do { |
| 528 | if (!list_empty(&imx_ep->queue)) |
| 529 | req = list_entry(imx_ep->queue.next, |
| 530 | struct imx_request, queue); |
| 531 | else { |
| 532 | D_REQ(imx_ep->imx_usb->dev, "<%s> no request on %s\n", |
| 533 | __func__, imx_ep->ep.name); |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | if (EP_DIR(imx_ep)) /* to host */ |
| 538 | completed = write_fifo(imx_ep, req); |
| 539 | else /* to device */ |
| 540 | completed = read_fifo(imx_ep, req); |
| 541 | |
| 542 | dump_ep_stat(__func__, imx_ep); |
| 543 | |
| 544 | } while (completed); |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static int handle_ep0(struct imx_ep_struct *imx_ep) |
| 550 | { |
| 551 | struct imx_request *req = NULL; |
| 552 | int ret = 0; |
| 553 | |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 554 | if (!list_empty(&imx_ep->queue)) { |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 555 | req = list_entry(imx_ep->queue.next, struct imx_request, queue); |
| 556 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 557 | switch (imx_ep->imx_usb->ep0state) { |
| 558 | |
| 559 | case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */ |
| 560 | write_fifo(imx_ep, req); |
| 561 | break; |
| 562 | case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */ |
| 563 | read_fifo(imx_ep, req); |
| 564 | break; |
| 565 | default: |
| 566 | D_EP0(imx_ep->imx_usb->dev, |
| 567 | "<%s> ep0 i/o, odd state %d\n", |
| 568 | __func__, imx_ep->imx_usb->ep0state); |
| 569 | ep_del_request(imx_ep, req); |
| 570 | ret = -EL2HLT; |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 575 | else |
| 576 | D_ERR(imx_ep->imx_usb->dev, "<%s> no request on %s\n", |
| 577 | __func__, imx_ep->ep.name); |
| 578 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | static void handle_ep0_devreq(struct imx_udc_struct *imx_usb) |
| 583 | { |
| 584 | struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0]; |
| 585 | union { |
| 586 | struct usb_ctrlrequest r; |
| 587 | u8 raw[8]; |
| 588 | u32 word[2]; |
| 589 | } u; |
| 590 | int temp, i; |
| 591 | |
| 592 | nuke(imx_ep, -EPROTO); |
| 593 | |
| 594 | /* read SETUP packet */ |
| 595 | for (i = 0; i < 2; i++) { |
| 596 | if (imx_ep_empty(imx_ep)) { |
| 597 | D_ERR(imx_usb->dev, |
| 598 | "<%s> no setup packet received\n", __func__); |
| 599 | goto stall; |
| 600 | } |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 601 | u.word[i] = __raw_readl(imx_usb->base |
| 602 | + USB_EP_FDAT(EP_NO(imx_ep))); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | temp = imx_ep_empty(imx_ep); |
| 606 | while (!imx_ep_empty(imx_ep)) { |
| 607 | i = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep))); |
| 608 | D_ERR(imx_usb->dev, |
| 609 | "<%s> wrong to have extra bytes for setup : 0x%08x\n", |
| 610 | __func__, i); |
| 611 | } |
| 612 | if (!temp) |
| 613 | goto stall; |
| 614 | |
| 615 | le16_to_cpus(&u.r.wValue); |
| 616 | le16_to_cpus(&u.r.wIndex); |
| 617 | le16_to_cpus(&u.r.wLength); |
| 618 | |
| 619 | D_REQ(imx_usb->dev, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n", |
| 620 | __func__, u.r.bRequestType, u.r.bRequest, |
| 621 | u.r.wValue, u.r.wIndex, u.r.wLength); |
| 622 | |
| 623 | if (imx_usb->set_config) { |
| 624 | /* NACK the host by using CMDOVER */ |
| 625 | temp = __raw_readl(imx_usb->base + USB_CTRL); |
| 626 | __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL); |
| 627 | |
| 628 | D_ERR(imx_usb->dev, |
| 629 | "<%s> set config req is pending, NACK the host\n", |
| 630 | __func__); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (u.r.bRequestType & USB_DIR_IN) |
| 635 | ep0_chg_stat(__func__, imx_usb, EP0_IN_DATA_PHASE); |
| 636 | else |
| 637 | ep0_chg_stat(__func__, imx_usb, EP0_OUT_DATA_PHASE); |
| 638 | |
| 639 | i = imx_usb->driver->setup(&imx_usb->gadget, &u.r); |
| 640 | if (i < 0) { |
| 641 | D_ERR(imx_usb->dev, "<%s> device setup error %d\n", |
| 642 | __func__, i); |
| 643 | goto stall; |
| 644 | } |
| 645 | |
| 646 | return; |
| 647 | stall: |
| 648 | D_ERR(imx_usb->dev, "<%s> protocol STALL\n", __func__); |
| 649 | imx_ep_stall(imx_ep); |
| 650 | ep0_chg_stat(__func__, imx_usb, EP0_STALL); |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | /******************************************************************************* |
| 655 | * USB gadget callback functions |
| 656 | ******************************************************************************* |
| 657 | */ |
| 658 | |
| 659 | static int imx_ep_enable(struct usb_ep *usb_ep, |
| 660 | const struct usb_endpoint_descriptor *desc) |
| 661 | { |
| 662 | struct imx_ep_struct *imx_ep = container_of(usb_ep, |
| 663 | struct imx_ep_struct, ep); |
| 664 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; |
| 665 | unsigned long flags; |
| 666 | |
| 667 | if (!usb_ep |
| 668 | || !desc |
| 669 | || !EP_NO(imx_ep) |
| 670 | || desc->bDescriptorType != USB_DT_ENDPOINT |
| 671 | || imx_ep->bEndpointAddress != desc->bEndpointAddress) { |
| 672 | D_ERR(imx_usb->dev, |
| 673 | "<%s> bad ep or descriptor\n", __func__); |
| 674 | return -EINVAL; |
| 675 | } |
| 676 | |
| 677 | if (imx_ep->bmAttributes != desc->bmAttributes) { |
| 678 | D_ERR(imx_usb->dev, |
| 679 | "<%s> %s type mismatch\n", __func__, usb_ep->name); |
| 680 | return -EINVAL; |
| 681 | } |
| 682 | |
| 683 | if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) { |
| 684 | D_ERR(imx_usb->dev, |
| 685 | "<%s> bad %s maxpacket\n", __func__, usb_ep->name); |
| 686 | return -ERANGE; |
| 687 | } |
| 688 | |
| 689 | if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) { |
| 690 | D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__); |
| 691 | return -ESHUTDOWN; |
| 692 | } |
| 693 | |
| 694 | local_irq_save(flags); |
| 695 | |
| 696 | imx_ep->stopped = 0; |
| 697 | imx_flush(imx_ep); |
| 698 | imx_ep_irq_enable(imx_ep); |
| 699 | |
| 700 | local_irq_restore(flags); |
| 701 | |
| 702 | D_EPX(imx_usb->dev, "<%s> ENABLED %s\n", __func__, usb_ep->name); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static int imx_ep_disable(struct usb_ep *usb_ep) |
| 707 | { |
| 708 | struct imx_ep_struct *imx_ep = container_of(usb_ep, |
| 709 | struct imx_ep_struct, ep); |
| 710 | unsigned long flags; |
| 711 | |
| 712 | if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) { |
| 713 | D_ERR(imx_ep->imx_usb->dev, "<%s> %s can not be disabled\n", |
| 714 | __func__, usb_ep ? imx_ep->ep.name : NULL); |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | |
| 718 | local_irq_save(flags); |
| 719 | |
| 720 | imx_ep->stopped = 1; |
| 721 | nuke(imx_ep, -ESHUTDOWN); |
| 722 | imx_flush(imx_ep); |
| 723 | imx_ep_irq_disable(imx_ep); |
| 724 | |
| 725 | local_irq_restore(flags); |
| 726 | |
| 727 | D_EPX(imx_ep->imx_usb->dev, |
| 728 | "<%s> DISABLED %s\n", __func__, usb_ep->name); |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static struct usb_request *imx_ep_alloc_request |
| 733 | (struct usb_ep *usb_ep, gfp_t gfp_flags) |
| 734 | { |
| 735 | struct imx_request *req; |
| 736 | |
Daniel Mack | 1e0abb7e | 2009-05-12 13:50:34 -0700 | [diff] [blame^] | 737 | if (!usb_ep) |
| 738 | return NULL; |
| 739 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 740 | req = kzalloc(sizeof *req, gfp_flags); |
Daniel Mack | 1e0abb7e | 2009-05-12 13:50:34 -0700 | [diff] [blame^] | 741 | if (!req) |
| 742 | return NULL; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 743 | |
| 744 | INIT_LIST_HEAD(&req->queue); |
| 745 | req->in_use = 0; |
| 746 | |
| 747 | return &req->req; |
| 748 | } |
| 749 | |
| 750 | static void imx_ep_free_request |
| 751 | (struct usb_ep *usb_ep, struct usb_request *usb_req) |
| 752 | { |
| 753 | struct imx_request *req; |
| 754 | |
| 755 | req = container_of(usb_req, struct imx_request, req); |
| 756 | WARN_ON(!list_empty(&req->queue)); |
| 757 | kfree(req); |
| 758 | } |
| 759 | |
| 760 | static int imx_ep_queue |
| 761 | (struct usb_ep *usb_ep, struct usb_request *usb_req, gfp_t gfp_flags) |
| 762 | { |
| 763 | struct imx_ep_struct *imx_ep; |
| 764 | struct imx_udc_struct *imx_usb; |
| 765 | struct imx_request *req; |
| 766 | unsigned long flags; |
| 767 | int ret = 0; |
| 768 | |
| 769 | imx_ep = container_of(usb_ep, struct imx_ep_struct, ep); |
| 770 | imx_usb = imx_ep->imx_usb; |
| 771 | req = container_of(usb_req, struct imx_request, req); |
| 772 | |
| 773 | /* |
| 774 | Special care on IMX udc. |
| 775 | Ignore enqueue when after set configuration from the |
| 776 | host. This assume all gadget drivers reply set |
| 777 | configuration with the next ep0 req enqueue. |
| 778 | */ |
| 779 | if (imx_usb->set_config && !EP_NO(imx_ep)) { |
| 780 | imx_usb->set_config = 0; |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 781 | D_ERR(imx_usb->dev, |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 782 | "<%s> gadget reply set config\n", __func__); |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | if (unlikely(!usb_req || !req || !usb_req->complete || !usb_req->buf)) { |
| 787 | D_ERR(imx_usb->dev, "<%s> bad params\n", __func__); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | |
| 791 | if (unlikely(!usb_ep || !imx_ep)) { |
| 792 | D_ERR(imx_usb->dev, "<%s> bad ep\n", __func__); |
| 793 | return -EINVAL; |
| 794 | } |
| 795 | |
| 796 | if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) { |
| 797 | D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__); |
| 798 | return -ESHUTDOWN; |
| 799 | } |
| 800 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 801 | /* Debug */ |
| 802 | D_REQ(imx_usb->dev, "<%s> ep%d %s request for [%d] bytes\n", |
| 803 | __func__, EP_NO(imx_ep), |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 804 | ((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state |
| 805 | == EP0_IN_DATA_PHASE) |
| 806 | || (EP_NO(imx_ep) && EP_DIR(imx_ep))) |
| 807 | ? "IN" : "OUT", usb_req->length); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 808 | dump_req(__func__, imx_ep, usb_req); |
| 809 | |
| 810 | if (imx_ep->stopped) { |
| 811 | usb_req->status = -ESHUTDOWN; |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 812 | return -ESHUTDOWN; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | if (req->in_use) { |
| 816 | D_ERR(imx_usb->dev, |
| 817 | "<%s> refusing to queue req %p (already queued)\n", |
| 818 | __func__, req); |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 819 | return 0; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 822 | local_irq_save(flags); |
| 823 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 824 | usb_req->status = -EINPROGRESS; |
| 825 | usb_req->actual = 0; |
| 826 | |
| 827 | ep_add_request(imx_ep, req); |
| 828 | |
| 829 | if (!EP_NO(imx_ep)) |
| 830 | ret = handle_ep0(imx_ep); |
| 831 | else |
| 832 | ret = handle_ep(imx_ep); |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 833 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 834 | local_irq_restore(flags); |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | static int imx_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req) |
| 839 | { |
| 840 | |
| 841 | struct imx_ep_struct *imx_ep = container_of |
| 842 | (usb_ep, struct imx_ep_struct, ep); |
| 843 | struct imx_request *req; |
| 844 | unsigned long flags; |
| 845 | |
| 846 | if (unlikely(!usb_ep || !EP_NO(imx_ep))) { |
| 847 | D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | |
| 851 | local_irq_save(flags); |
| 852 | |
| 853 | /* make sure it's actually queued on this endpoint */ |
| 854 | list_for_each_entry(req, &imx_ep->queue, queue) { |
| 855 | if (&req->req == usb_req) |
| 856 | break; |
| 857 | } |
| 858 | if (&req->req != usb_req) { |
| 859 | local_irq_restore(flags); |
| 860 | return -EINVAL; |
| 861 | } |
| 862 | |
| 863 | done(imx_ep, req, -ECONNRESET); |
| 864 | |
| 865 | local_irq_restore(flags); |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static int imx_ep_set_halt(struct usb_ep *usb_ep, int value) |
| 870 | { |
| 871 | struct imx_ep_struct *imx_ep = container_of |
| 872 | (usb_ep, struct imx_ep_struct, ep); |
| 873 | unsigned long flags; |
| 874 | |
| 875 | if (unlikely(!usb_ep || !EP_NO(imx_ep))) { |
| 876 | D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__); |
| 877 | return -EINVAL; |
| 878 | } |
| 879 | |
| 880 | local_irq_save(flags); |
| 881 | |
| 882 | if ((imx_ep->bEndpointAddress & USB_DIR_IN) |
| 883 | && !list_empty(&imx_ep->queue)) { |
| 884 | local_irq_restore(flags); |
| 885 | return -EAGAIN; |
| 886 | } |
| 887 | |
| 888 | imx_ep_stall(imx_ep); |
| 889 | |
| 890 | local_irq_restore(flags); |
| 891 | |
| 892 | D_EPX(imx_ep->imx_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name); |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static int imx_ep_fifo_status(struct usb_ep *usb_ep) |
| 897 | { |
| 898 | struct imx_ep_struct *imx_ep = container_of |
| 899 | (usb_ep, struct imx_ep_struct, ep); |
| 900 | |
| 901 | if (!usb_ep) { |
| 902 | D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__); |
| 903 | return -ENODEV; |
| 904 | } |
| 905 | |
| 906 | if (imx_ep->imx_usb->gadget.speed == USB_SPEED_UNKNOWN) |
| 907 | return 0; |
| 908 | else |
| 909 | return imx_fifo_bcount(imx_ep); |
| 910 | } |
| 911 | |
| 912 | static void imx_ep_fifo_flush(struct usb_ep *usb_ep) |
| 913 | { |
| 914 | struct imx_ep_struct *imx_ep = container_of |
| 915 | (usb_ep, struct imx_ep_struct, ep); |
| 916 | unsigned long flags; |
| 917 | |
| 918 | local_irq_save(flags); |
| 919 | |
| 920 | if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) { |
| 921 | D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__); |
| 922 | local_irq_restore(flags); |
| 923 | return; |
| 924 | } |
| 925 | |
| 926 | /* toggle and halt bits stay unchanged */ |
| 927 | imx_flush(imx_ep); |
| 928 | |
| 929 | local_irq_restore(flags); |
| 930 | } |
| 931 | |
| 932 | static struct usb_ep_ops imx_ep_ops = { |
| 933 | .enable = imx_ep_enable, |
| 934 | .disable = imx_ep_disable, |
| 935 | |
| 936 | .alloc_request = imx_ep_alloc_request, |
| 937 | .free_request = imx_ep_free_request, |
| 938 | |
| 939 | .queue = imx_ep_queue, |
| 940 | .dequeue = imx_ep_dequeue, |
| 941 | |
| 942 | .set_halt = imx_ep_set_halt, |
| 943 | .fifo_status = imx_ep_fifo_status, |
| 944 | .fifo_flush = imx_ep_fifo_flush, |
| 945 | }; |
| 946 | |
| 947 | /******************************************************************************* |
| 948 | * USB endpoint control functions |
| 949 | ******************************************************************************* |
| 950 | */ |
| 951 | |
| 952 | void ep0_chg_stat(const char *label, |
| 953 | struct imx_udc_struct *imx_usb, enum ep0_state stat) |
| 954 | { |
| 955 | D_EP0(imx_usb->dev, "<%s> from %15s to %15s\n", |
| 956 | label, state_name[imx_usb->ep0state], state_name[stat]); |
| 957 | |
| 958 | if (imx_usb->ep0state == stat) |
| 959 | return; |
| 960 | |
| 961 | imx_usb->ep0state = stat; |
| 962 | } |
| 963 | |
| 964 | static void usb_init_data(struct imx_udc_struct *imx_usb) |
| 965 | { |
| 966 | struct imx_ep_struct *imx_ep; |
| 967 | u8 i; |
| 968 | |
| 969 | /* device/ep0 records init */ |
| 970 | INIT_LIST_HEAD(&imx_usb->gadget.ep_list); |
| 971 | INIT_LIST_HEAD(&imx_usb->gadget.ep0->ep_list); |
| 972 | ep0_chg_stat(__func__, imx_usb, EP0_IDLE); |
| 973 | |
| 974 | /* basic endpoint records init */ |
| 975 | for (i = 0; i < IMX_USB_NB_EP; i++) { |
| 976 | imx_ep = &imx_usb->imx_ep[i]; |
| 977 | |
| 978 | if (i) { |
| 979 | list_add_tail(&imx_ep->ep.ep_list, |
| 980 | &imx_usb->gadget.ep_list); |
| 981 | imx_ep->stopped = 1; |
| 982 | } else |
| 983 | imx_ep->stopped = 0; |
| 984 | |
| 985 | INIT_LIST_HEAD(&imx_ep->queue); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | static void udc_stop_activity(struct imx_udc_struct *imx_usb, |
| 990 | struct usb_gadget_driver *driver) |
| 991 | { |
| 992 | struct imx_ep_struct *imx_ep; |
| 993 | int i; |
| 994 | |
| 995 | if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN) |
| 996 | driver = NULL; |
| 997 | |
| 998 | /* prevent new request submissions, kill any outstanding requests */ |
| 999 | for (i = 1; i < IMX_USB_NB_EP; i++) { |
| 1000 | imx_ep = &imx_usb->imx_ep[i]; |
| 1001 | imx_flush(imx_ep); |
| 1002 | imx_ep->stopped = 1; |
| 1003 | imx_ep_irq_disable(imx_ep); |
| 1004 | nuke(imx_ep, -ESHUTDOWN); |
| 1005 | } |
| 1006 | |
| 1007 | imx_usb->cfg = 0; |
| 1008 | imx_usb->intf = 0; |
| 1009 | imx_usb->alt = 0; |
| 1010 | |
| 1011 | if (driver) |
| 1012 | driver->disconnect(&imx_usb->gadget); |
| 1013 | } |
| 1014 | |
| 1015 | /******************************************************************************* |
| 1016 | * Interrupt handlers |
| 1017 | ******************************************************************************* |
| 1018 | */ |
| 1019 | |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1020 | /* |
| 1021 | * Called when timer expires. |
| 1022 | * Timer is started when CFG_CHG is received. |
| 1023 | */ |
| 1024 | static void handle_config(unsigned long data) |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1025 | { |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1026 | struct imx_udc_struct *imx_usb = (void *)data; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1027 | struct usb_ctrlrequest u; |
| 1028 | int temp, cfg, intf, alt; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1029 | |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1030 | local_irq_disable(); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1031 | |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1032 | temp = __raw_readl(imx_usb->base + USB_STAT); |
| 1033 | cfg = (temp & STAT_CFG) >> 5; |
| 1034 | intf = (temp & STAT_INTF) >> 3; |
| 1035 | alt = temp & STAT_ALTSET; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1036 | |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1037 | D_REQ(imx_usb->dev, |
| 1038 | "<%s> orig config C=%d, I=%d, A=%d / " |
| 1039 | "req config C=%d, I=%d, A=%d\n", |
| 1040 | __func__, imx_usb->cfg, imx_usb->intf, imx_usb->alt, |
| 1041 | cfg, intf, alt); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1042 | |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1043 | if (cfg == 1 || cfg == 2) { |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1044 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1045 | if (imx_usb->cfg != cfg) { |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1046 | u.bRequest = USB_REQ_SET_CONFIGURATION; |
| 1047 | u.bRequestType = USB_DIR_OUT | |
| 1048 | USB_TYPE_STANDARD | |
| 1049 | USB_RECIP_DEVICE; |
| 1050 | u.wValue = cfg; |
| 1051 | u.wIndex = 0; |
| 1052 | u.wLength = 0; |
| 1053 | imx_usb->cfg = cfg; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1054 | imx_usb->driver->setup(&imx_usb->gadget, &u); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1055 | |
| 1056 | } |
| 1057 | if (imx_usb->intf != intf || imx_usb->alt != alt) { |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1058 | u.bRequest = USB_REQ_SET_INTERFACE; |
| 1059 | u.bRequestType = USB_DIR_OUT | |
| 1060 | USB_TYPE_STANDARD | |
| 1061 | USB_RECIP_INTERFACE; |
| 1062 | u.wValue = alt; |
| 1063 | u.wIndex = intf; |
| 1064 | u.wLength = 0; |
| 1065 | imx_usb->intf = intf; |
| 1066 | imx_usb->alt = alt; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1067 | imx_usb->driver->setup(&imx_usb->gadget, &u); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1071 | imx_usb->set_config = 0; |
| 1072 | |
| 1073 | local_irq_enable(); |
| 1074 | } |
| 1075 | |
| 1076 | static irqreturn_t imx_udc_irq(int irq, void *dev) |
| 1077 | { |
| 1078 | struct imx_udc_struct *imx_usb = dev; |
| 1079 | int intr = __raw_readl(imx_usb->base + USB_INTR); |
| 1080 | int temp; |
| 1081 | |
| 1082 | if (intr & (INTR_WAKEUP | INTR_SUSPEND | INTR_RESUME | INTR_RESET_START |
| 1083 | | INTR_RESET_STOP | INTR_CFG_CHG)) { |
| 1084 | dump_intr(__func__, intr, imx_usb->dev); |
| 1085 | dump_usb_stat(__func__, imx_usb); |
| 1086 | } |
| 1087 | |
| 1088 | if (!imx_usb->driver) |
| 1089 | goto end_irq; |
| 1090 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1091 | if (intr & INTR_SOF) { |
Darius Augulis | 8f182e5 | 2009-01-21 15:17:25 +0200 | [diff] [blame] | 1092 | /* Copy from Freescale BSP. |
| 1093 | We must enable SOF intr and set CMDOVER. |
| 1094 | Datasheet don't specifiy this action, but it |
| 1095 | is done in Freescale BSP, so just copy it. |
| 1096 | */ |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1097 | if (imx_usb->ep0state == EP0_IDLE) { |
| 1098 | temp = __raw_readl(imx_usb->base + USB_CTRL); |
Darius Augulis | 593bef6 | 2009-01-21 15:17:55 +0200 | [diff] [blame] | 1099 | __raw_writel(temp | CTRL_CMDOVER, |
| 1100 | imx_usb->base + USB_CTRL); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1104 | if (intr & INTR_CFG_CHG) { |
| 1105 | /* A workaround of serious IMX UDC bug. |
| 1106 | Handling of CFG_CHG should be delayed for some time, because |
| 1107 | IMX does not NACK the host when CFG_CHG interrupt is pending. |
| 1108 | There is no time to handle current CFG_CHG |
| 1109 | if next CFG_CHG or SETUP packed is send immediately. |
| 1110 | We have to clear CFG_CHG, start the timer and |
| 1111 | NACK the host by setting CTRL_CMDOVER |
| 1112 | if it sends any SETUP packet. |
| 1113 | When timer expires, handler is called to handle configuration |
| 1114 | changes. While CFG_CHG is not handled (set_config=1), |
| 1115 | we must NACK the host to every SETUP packed. |
| 1116 | This delay prevents from going out of sync with host. |
| 1117 | */ |
| 1118 | __raw_writel(INTR_CFG_CHG, imx_usb->base + USB_INTR); |
| 1119 | imx_usb->set_config = 1; |
| 1120 | mod_timer(&imx_usb->timer, jiffies + 5); |
| 1121 | goto end_irq; |
| 1122 | } |
| 1123 | |
| 1124 | if (intr & INTR_WAKEUP) { |
| 1125 | if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN |
| 1126 | && imx_usb->driver && imx_usb->driver->resume) |
| 1127 | imx_usb->driver->resume(&imx_usb->gadget); |
| 1128 | imx_usb->set_config = 0; |
| 1129 | del_timer(&imx_usb->timer); |
| 1130 | imx_usb->gadget.speed = USB_SPEED_FULL; |
| 1131 | } |
| 1132 | |
| 1133 | if (intr & INTR_SUSPEND) { |
| 1134 | if (imx_usb->gadget.speed != USB_SPEED_UNKNOWN |
| 1135 | && imx_usb->driver && imx_usb->driver->suspend) |
| 1136 | imx_usb->driver->suspend(&imx_usb->gadget); |
| 1137 | imx_usb->set_config = 0; |
| 1138 | del_timer(&imx_usb->timer); |
| 1139 | imx_usb->gadget.speed = USB_SPEED_UNKNOWN; |
| 1140 | } |
| 1141 | |
| 1142 | if (intr & INTR_RESET_START) { |
| 1143 | __raw_writel(intr, imx_usb->base + USB_INTR); |
| 1144 | udc_stop_activity(imx_usb, imx_usb->driver); |
| 1145 | imx_usb->set_config = 0; |
| 1146 | del_timer(&imx_usb->timer); |
| 1147 | imx_usb->gadget.speed = USB_SPEED_UNKNOWN; |
| 1148 | } |
| 1149 | |
| 1150 | if (intr & INTR_RESET_STOP) |
| 1151 | imx_usb->gadget.speed = USB_SPEED_FULL; |
| 1152 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1153 | end_irq: |
| 1154 | __raw_writel(intr, imx_usb->base + USB_INTR); |
| 1155 | return IRQ_HANDLED; |
| 1156 | } |
| 1157 | |
| 1158 | static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev) |
| 1159 | { |
| 1160 | struct imx_udc_struct *imx_usb = dev; |
Darius Augulis | d24921a | 2009-01-21 15:18:33 +0200 | [diff] [blame] | 1161 | struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0]; |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1162 | int intr = __raw_readl(imx_usb->base + USB_EP_INTR(0)); |
| 1163 | |
| 1164 | dump_ep_intr(__func__, 0, intr, imx_usb->dev); |
| 1165 | |
| 1166 | if (!imx_usb->driver) { |
| 1167 | __raw_writel(intr, imx_usb->base + USB_EP_INTR(0)); |
| 1168 | return IRQ_HANDLED; |
| 1169 | } |
| 1170 | |
Darius Augulis | d24921a | 2009-01-21 15:18:33 +0200 | [diff] [blame] | 1171 | /* DEVREQ has highest priority */ |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1172 | if (intr & (EPINTR_DEVREQ | EPINTR_MDEVREQ)) |
| 1173 | handle_ep0_devreq(imx_usb); |
| 1174 | /* Seem i.MX is missing EOF interrupt sometimes. |
Darius Augulis | d24921a | 2009-01-21 15:18:33 +0200 | [diff] [blame] | 1175 | * Therefore we don't monitor EOF. |
| 1176 | * We call handle_ep0() only if a request is queued for ep0. |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1177 | */ |
Darius Augulis | d24921a | 2009-01-21 15:18:33 +0200 | [diff] [blame] | 1178 | else if (!list_empty(&imx_ep->queue)) |
| 1179 | handle_ep0(imx_ep); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1180 | |
| 1181 | __raw_writel(intr, imx_usb->base + USB_EP_INTR(0)); |
| 1182 | |
| 1183 | return IRQ_HANDLED; |
| 1184 | } |
| 1185 | |
| 1186 | static irqreturn_t imx_udc_bulk_irq(int irq, void *dev) |
| 1187 | { |
| 1188 | struct imx_udc_struct *imx_usb = dev; |
| 1189 | struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0]; |
| 1190 | int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); |
| 1191 | |
| 1192 | dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev); |
| 1193 | |
| 1194 | if (!imx_usb->driver) { |
| 1195 | __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); |
| 1196 | return IRQ_HANDLED; |
| 1197 | } |
| 1198 | |
| 1199 | handle_ep(imx_ep); |
| 1200 | |
| 1201 | __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); |
| 1202 | |
| 1203 | return IRQ_HANDLED; |
| 1204 | } |
| 1205 | |
| 1206 | irq_handler_t intr_handler(int i) |
| 1207 | { |
| 1208 | switch (i) { |
| 1209 | case 0: |
| 1210 | return imx_udc_ctrl_irq; |
| 1211 | case 1: |
| 1212 | case 2: |
| 1213 | case 3: |
| 1214 | case 4: |
| 1215 | case 5: |
| 1216 | return imx_udc_bulk_irq; |
| 1217 | default: |
| 1218 | return imx_udc_irq; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /******************************************************************************* |
| 1223 | * Static defined IMX UDC structure |
| 1224 | ******************************************************************************* |
| 1225 | */ |
| 1226 | |
| 1227 | static const struct usb_gadget_ops imx_udc_ops = { |
| 1228 | .get_frame = imx_udc_get_frame, |
| 1229 | .wakeup = imx_udc_wakeup, |
| 1230 | }; |
| 1231 | |
| 1232 | static struct imx_udc_struct controller = { |
| 1233 | .gadget = { |
| 1234 | .ops = &imx_udc_ops, |
| 1235 | .ep0 = &controller.imx_ep[0].ep, |
| 1236 | .name = driver_name, |
| 1237 | .dev = { |
Kay Sievers | 5df58524 | 2009-03-24 16:38:23 -0700 | [diff] [blame] | 1238 | .init_name = "gadget", |
| 1239 | }, |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1240 | }, |
| 1241 | |
| 1242 | .imx_ep[0] = { |
| 1243 | .ep = { |
| 1244 | .name = ep0name, |
| 1245 | .ops = &imx_ep_ops, |
| 1246 | .maxpacket = 32, |
| 1247 | }, |
| 1248 | .imx_usb = &controller, |
| 1249 | .fifosize = 32, |
| 1250 | .bEndpointAddress = 0, |
| 1251 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 1252 | }, |
| 1253 | .imx_ep[1] = { |
| 1254 | .ep = { |
| 1255 | .name = "ep1in-bulk", |
| 1256 | .ops = &imx_ep_ops, |
| 1257 | .maxpacket = 64, |
| 1258 | }, |
| 1259 | .imx_usb = &controller, |
| 1260 | .fifosize = 64, |
| 1261 | .bEndpointAddress = USB_DIR_IN | 1, |
| 1262 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 1263 | }, |
| 1264 | .imx_ep[2] = { |
| 1265 | .ep = { |
| 1266 | .name = "ep2out-bulk", |
| 1267 | .ops = &imx_ep_ops, |
| 1268 | .maxpacket = 64, |
| 1269 | }, |
| 1270 | .imx_usb = &controller, |
| 1271 | .fifosize = 64, |
| 1272 | .bEndpointAddress = USB_DIR_OUT | 2, |
| 1273 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 1274 | }, |
| 1275 | .imx_ep[3] = { |
| 1276 | .ep = { |
| 1277 | .name = "ep3out-bulk", |
| 1278 | .ops = &imx_ep_ops, |
| 1279 | .maxpacket = 32, |
| 1280 | }, |
| 1281 | .imx_usb = &controller, |
| 1282 | .fifosize = 32, |
| 1283 | .bEndpointAddress = USB_DIR_OUT | 3, |
| 1284 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 1285 | }, |
| 1286 | .imx_ep[4] = { |
| 1287 | .ep = { |
| 1288 | .name = "ep4in-int", |
| 1289 | .ops = &imx_ep_ops, |
| 1290 | .maxpacket = 32, |
| 1291 | }, |
| 1292 | .imx_usb = &controller, |
| 1293 | .fifosize = 32, |
| 1294 | .bEndpointAddress = USB_DIR_IN | 4, |
| 1295 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 1296 | }, |
| 1297 | .imx_ep[5] = { |
| 1298 | .ep = { |
| 1299 | .name = "ep5out-int", |
| 1300 | .ops = &imx_ep_ops, |
| 1301 | .maxpacket = 32, |
| 1302 | }, |
| 1303 | .imx_usb = &controller, |
| 1304 | .fifosize = 32, |
| 1305 | .bEndpointAddress = USB_DIR_OUT | 5, |
| 1306 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 1307 | }, |
| 1308 | }; |
| 1309 | |
| 1310 | /******************************************************************************* |
| 1311 | * USB gadged driver functions |
| 1312 | ******************************************************************************* |
| 1313 | */ |
| 1314 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 1315 | { |
| 1316 | struct imx_udc_struct *imx_usb = &controller; |
| 1317 | int retval; |
| 1318 | |
| 1319 | if (!driver |
| 1320 | || driver->speed < USB_SPEED_FULL |
| 1321 | || !driver->bind |
| 1322 | || !driver->disconnect |
| 1323 | || !driver->setup) |
| 1324 | return -EINVAL; |
| 1325 | if (!imx_usb) |
| 1326 | return -ENODEV; |
| 1327 | if (imx_usb->driver) |
| 1328 | return -EBUSY; |
| 1329 | |
| 1330 | /* first hook up the driver ... */ |
| 1331 | imx_usb->driver = driver; |
| 1332 | imx_usb->gadget.dev.driver = &driver->driver; |
| 1333 | |
| 1334 | retval = device_add(&imx_usb->gadget.dev); |
| 1335 | if (retval) |
| 1336 | goto fail; |
| 1337 | retval = driver->bind(&imx_usb->gadget); |
| 1338 | if (retval) { |
| 1339 | D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n", |
| 1340 | __func__, driver->driver.name, retval); |
| 1341 | device_del(&imx_usb->gadget.dev); |
| 1342 | |
| 1343 | goto fail; |
| 1344 | } |
| 1345 | |
| 1346 | D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n", |
| 1347 | __func__, driver->driver.name); |
| 1348 | |
| 1349 | imx_udc_enable(imx_usb); |
| 1350 | |
| 1351 | return 0; |
| 1352 | fail: |
| 1353 | imx_usb->driver = NULL; |
| 1354 | imx_usb->gadget.dev.driver = NULL; |
| 1355 | return retval; |
| 1356 | } |
| 1357 | EXPORT_SYMBOL(usb_gadget_register_driver); |
| 1358 | |
| 1359 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 1360 | { |
| 1361 | struct imx_udc_struct *imx_usb = &controller; |
| 1362 | |
| 1363 | if (!imx_usb) |
| 1364 | return -ENODEV; |
| 1365 | if (!driver || driver != imx_usb->driver || !driver->unbind) |
| 1366 | return -EINVAL; |
| 1367 | |
| 1368 | udc_stop_activity(imx_usb, driver); |
| 1369 | imx_udc_disable(imx_usb); |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1370 | del_timer(&imx_usb->timer); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1371 | |
| 1372 | driver->unbind(&imx_usb->gadget); |
| 1373 | imx_usb->gadget.dev.driver = NULL; |
| 1374 | imx_usb->driver = NULL; |
| 1375 | |
| 1376 | device_del(&imx_usb->gadget.dev); |
| 1377 | |
| 1378 | D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n", |
| 1379 | __func__, driver->driver.name); |
| 1380 | |
| 1381 | return 0; |
| 1382 | } |
| 1383 | EXPORT_SYMBOL(usb_gadget_unregister_driver); |
| 1384 | |
| 1385 | /******************************************************************************* |
| 1386 | * Module functions |
| 1387 | ******************************************************************************* |
| 1388 | */ |
| 1389 | |
| 1390 | static int __init imx_udc_probe(struct platform_device *pdev) |
| 1391 | { |
| 1392 | struct imx_udc_struct *imx_usb = &controller; |
| 1393 | struct resource *res; |
| 1394 | struct imxusb_platform_data *pdata; |
| 1395 | struct clk *clk; |
| 1396 | void __iomem *base; |
| 1397 | int ret = 0; |
| 1398 | int i, res_size; |
| 1399 | |
| 1400 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1401 | if (!res) { |
| 1402 | dev_err(&pdev->dev, "can't get device resources\n"); |
| 1403 | return -ENODEV; |
| 1404 | } |
| 1405 | |
| 1406 | pdata = pdev->dev.platform_data; |
| 1407 | if (!pdata) { |
| 1408 | dev_err(&pdev->dev, "driver needs platform data\n"); |
| 1409 | return -ENODEV; |
| 1410 | } |
| 1411 | |
| 1412 | res_size = res->end - res->start + 1; |
| 1413 | if (!request_mem_region(res->start, res_size, res->name)) { |
| 1414 | dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n", |
| 1415 | res_size, res->start); |
| 1416 | return -ENOMEM; |
| 1417 | } |
| 1418 | |
| 1419 | if (pdata->init) { |
| 1420 | ret = pdata->init(&pdev->dev); |
| 1421 | if (ret) |
| 1422 | goto fail0; |
| 1423 | } |
| 1424 | |
| 1425 | base = ioremap(res->start, res_size); |
| 1426 | if (!base) { |
| 1427 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 1428 | ret = -EIO; |
| 1429 | goto fail1; |
| 1430 | } |
| 1431 | |
| 1432 | clk = clk_get(NULL, "usbd_clk"); |
| 1433 | if (IS_ERR(clk)) { |
| 1434 | ret = PTR_ERR(clk); |
| 1435 | dev_err(&pdev->dev, "can't get USB clock\n"); |
| 1436 | goto fail2; |
| 1437 | } |
| 1438 | clk_enable(clk); |
| 1439 | |
| 1440 | if (clk_get_rate(clk) != 48000000) { |
| 1441 | D_INI(&pdev->dev, |
| 1442 | "Bad USB clock (%d Hz), changing to 48000000 Hz\n", |
| 1443 | (int)clk_get_rate(clk)); |
| 1444 | if (clk_set_rate(clk, 48000000)) { |
| 1445 | dev_err(&pdev->dev, |
| 1446 | "Unable to set correct USB clock (48MHz)\n"); |
| 1447 | ret = -EIO; |
| 1448 | goto fail3; |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | for (i = 0; i < IMX_USB_NB_EP + 1; i++) { |
| 1453 | imx_usb->usbd_int[i] = platform_get_irq(pdev, i); |
| 1454 | if (imx_usb->usbd_int[i] < 0) { |
| 1455 | dev_err(&pdev->dev, "can't get irq number\n"); |
| 1456 | ret = -ENODEV; |
| 1457 | goto fail3; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | for (i = 0; i < IMX_USB_NB_EP + 1; i++) { |
| 1462 | ret = request_irq(imx_usb->usbd_int[i], intr_handler(i), |
| 1463 | IRQF_DISABLED, driver_name, imx_usb); |
| 1464 | if (ret) { |
| 1465 | dev_err(&pdev->dev, "can't get irq %i, err %d\n", |
| 1466 | imx_usb->usbd_int[i], ret); |
| 1467 | for (--i; i >= 0; i--) |
| 1468 | free_irq(imx_usb->usbd_int[i], imx_usb); |
| 1469 | goto fail3; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | imx_usb->res = res; |
| 1474 | imx_usb->base = base; |
| 1475 | imx_usb->clk = clk; |
| 1476 | imx_usb->dev = &pdev->dev; |
| 1477 | |
| 1478 | device_initialize(&imx_usb->gadget.dev); |
| 1479 | |
| 1480 | imx_usb->gadget.dev.parent = &pdev->dev; |
| 1481 | imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask; |
| 1482 | |
| 1483 | platform_set_drvdata(pdev, imx_usb); |
| 1484 | |
| 1485 | usb_init_data(imx_usb); |
| 1486 | imx_udc_init(imx_usb); |
| 1487 | |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1488 | init_timer(&imx_usb->timer); |
| 1489 | imx_usb->timer.function = handle_config; |
| 1490 | imx_usb->timer.data = (unsigned long)imx_usb; |
| 1491 | |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1492 | return 0; |
| 1493 | |
| 1494 | fail3: |
| 1495 | clk_put(clk); |
| 1496 | clk_disable(clk); |
| 1497 | fail2: |
| 1498 | iounmap(base); |
| 1499 | fail1: |
| 1500 | if (pdata->exit) |
| 1501 | pdata->exit(&pdev->dev); |
| 1502 | fail0: |
| 1503 | release_mem_region(res->start, res_size); |
| 1504 | return ret; |
| 1505 | } |
| 1506 | |
| 1507 | static int __exit imx_udc_remove(struct platform_device *pdev) |
| 1508 | { |
| 1509 | struct imx_udc_struct *imx_usb = platform_get_drvdata(pdev); |
| 1510 | struct imxusb_platform_data *pdata = pdev->dev.platform_data; |
| 1511 | int i; |
| 1512 | |
| 1513 | imx_udc_disable(imx_usb); |
Darius Augulis | b633d28 | 2009-01-21 15:19:19 +0200 | [diff] [blame] | 1514 | del_timer(&imx_usb->timer); |
Darius Augulis | 2a4f136 | 2008-11-12 13:38:31 -0800 | [diff] [blame] | 1515 | |
| 1516 | for (i = 0; i < IMX_USB_NB_EP + 1; i++) |
| 1517 | free_irq(imx_usb->usbd_int[i], imx_usb); |
| 1518 | |
| 1519 | clk_put(imx_usb->clk); |
| 1520 | clk_disable(imx_usb->clk); |
| 1521 | iounmap(imx_usb->base); |
| 1522 | |
| 1523 | release_mem_region(imx_usb->res->start, |
| 1524 | imx_usb->res->end - imx_usb->res->start + 1); |
| 1525 | |
| 1526 | if (pdata->exit) |
| 1527 | pdata->exit(&pdev->dev); |
| 1528 | |
| 1529 | platform_set_drvdata(pdev, NULL); |
| 1530 | |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | /*----------------------------------------------------------------------------*/ |
| 1535 | |
| 1536 | #ifdef CONFIG_PM |
| 1537 | #define imx_udc_suspend NULL |
| 1538 | #define imx_udc_resume NULL |
| 1539 | #else |
| 1540 | #define imx_udc_suspend NULL |
| 1541 | #define imx_udc_resume NULL |
| 1542 | #endif |
| 1543 | |
| 1544 | /*----------------------------------------------------------------------------*/ |
| 1545 | |
| 1546 | static struct platform_driver udc_driver = { |
| 1547 | .driver = { |
| 1548 | .name = driver_name, |
| 1549 | .owner = THIS_MODULE, |
| 1550 | }, |
| 1551 | .remove = __exit_p(imx_udc_remove), |
| 1552 | .suspend = imx_udc_suspend, |
| 1553 | .resume = imx_udc_resume, |
| 1554 | }; |
| 1555 | |
| 1556 | static int __init udc_init(void) |
| 1557 | { |
| 1558 | return platform_driver_probe(&udc_driver, imx_udc_probe); |
| 1559 | } |
| 1560 | module_init(udc_init); |
| 1561 | |
| 1562 | static void __exit udc_exit(void) |
| 1563 | { |
| 1564 | platform_driver_unregister(&udc_driver); |
| 1565 | } |
| 1566 | module_exit(udc_exit); |
| 1567 | |
| 1568 | MODULE_DESCRIPTION("IMX USB Device Controller driver"); |
| 1569 | MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>"); |
| 1570 | MODULE_LICENSE("GPL"); |
| 1571 | MODULE_ALIAS("platform:imx_udc"); |