Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * MUSB OTG controller driver for Blackfin Processors |
| 3 | * |
| 4 | * Copyright 2006-2008 Analog Devices Inc. |
| 5 | * |
| 6 | * Enter bugs at http://blackfin.uclinux.org/ |
| 7 | * |
| 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/sched.h> |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/list.h> |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 16 | #include <linux/gpio.h> |
| 17 | #include <linux/io.h> |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/dma-mapping.h> |
Bob Liu | ad50c1b | 2011-08-05 17:33:05 +0800 | [diff] [blame] | 20 | #include <linux/prefetch.h> |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 21 | |
| 22 | #include <asm/cacheflush.h> |
| 23 | |
| 24 | #include "musb_core.h" |
Mike Frysinger | 1325430 | 2011-03-30 22:48:54 -0400 | [diff] [blame] | 25 | #include "musbhsdma.h" |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 26 | #include "blackfin.h" |
| 27 | |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 28 | struct bfin_glue { |
| 29 | struct device *dev; |
| 30 | struct platform_device *musb; |
| 31 | }; |
Felipe Balbi | fcd22e3 | 2010-12-02 13:13:09 +0200 | [diff] [blame] | 32 | #define glue_to_musb(g) platform_get_drvdata(g->musb) |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 33 | |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 34 | /* |
| 35 | * Load an endpoint's FIFO |
| 36 | */ |
| 37 | void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src) |
| 38 | { |
Felipe Balbi | 28e4970 | 2011-05-18 00:25:03 +0300 | [diff] [blame] | 39 | struct musb *musb = hw_ep->musb; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 40 | void __iomem *fifo = hw_ep->fifo; |
| 41 | void __iomem *epio = hw_ep->regs; |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 42 | u8 epnum = hw_ep->epnum; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 43 | |
| 44 | prefetch((u8 *)src); |
| 45 | |
| 46 | musb_writew(epio, MUSB_TXCOUNT, len); |
| 47 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 48 | dev_dbg(musb->controller, "TX ep%d fifo %p count %d buf %p, epio %p\n", |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 49 | hw_ep->epnum, fifo, len, src, epio); |
| 50 | |
| 51 | dump_fifo_data(src, len); |
| 52 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 53 | if (!ANOMALY_05000380 && epnum != 0) { |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 54 | u16 dma_reg; |
| 55 | |
| 56 | flush_dcache_range((unsigned long)src, |
| 57 | (unsigned long)(src + len)); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 58 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 59 | /* Setup DMA address register */ |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 60 | dma_reg = (u32)src; |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 61 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg); |
| 62 | SSYNC(); |
| 63 | |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 64 | dma_reg = (u32)src >> 16; |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 65 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg); |
| 66 | SSYNC(); |
| 67 | |
| 68 | /* Setup DMA count register */ |
| 69 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len); |
| 70 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0); |
| 71 | SSYNC(); |
| 72 | |
| 73 | /* Enable the DMA */ |
| 74 | dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION; |
| 75 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg); |
| 76 | SSYNC(); |
| 77 | |
| 78 | /* Wait for compelete */ |
| 79 | while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum))) |
| 80 | cpu_relax(); |
| 81 | |
| 82 | /* acknowledge dma interrupt */ |
| 83 | bfin_write_USB_DMA_INTERRUPT(1 << epnum); |
| 84 | SSYNC(); |
| 85 | |
| 86 | /* Reset DMA */ |
| 87 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0); |
| 88 | SSYNC(); |
| 89 | } else { |
| 90 | SSYNC(); |
| 91 | |
| 92 | if (unlikely((unsigned long)src & 0x01)) |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 93 | outsw_8((unsigned long)fifo, src, (len + 1) >> 1); |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 94 | else |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 95 | outsw((unsigned long)fifo, src, (len + 1) >> 1); |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 96 | } |
| 97 | } |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 98 | /* |
| 99 | * Unload an endpoint's FIFO |
| 100 | */ |
| 101 | void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst) |
| 102 | { |
Felipe Balbi | 28e4970 | 2011-05-18 00:25:03 +0300 | [diff] [blame] | 103 | struct musb *musb = hw_ep->musb; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 104 | void __iomem *fifo = hw_ep->fifo; |
| 105 | u8 epnum = hw_ep->epnum; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 106 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 107 | if (ANOMALY_05000467 && epnum != 0) { |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 108 | u16 dma_reg; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 109 | |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 110 | invalidate_dcache_range((unsigned long)dst, |
| 111 | (unsigned long)(dst + len)); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 112 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 113 | /* Setup DMA address register */ |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 114 | dma_reg = (u32)dst; |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 115 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg); |
| 116 | SSYNC(); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 117 | |
Bryan Wu | 1ca9e9c | 2009-12-28 13:40:39 +0200 | [diff] [blame] | 118 | dma_reg = (u32)dst >> 16; |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 119 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg); |
| 120 | SSYNC(); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 121 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 122 | /* Setup DMA count register */ |
| 123 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len); |
| 124 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0); |
| 125 | SSYNC(); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 126 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 127 | /* Enable the DMA */ |
| 128 | dma_reg = (epnum << 4) | DMA_ENA | INT_ENA; |
| 129 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg); |
| 130 | SSYNC(); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 131 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 132 | /* Wait for compelete */ |
| 133 | while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum))) |
| 134 | cpu_relax(); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 135 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 136 | /* acknowledge dma interrupt */ |
| 137 | bfin_write_USB_DMA_INTERRUPT(1 << epnum); |
| 138 | SSYNC(); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 139 | |
Bryan Wu | 1c4bdc0 | 2009-12-21 09:49:52 -0500 | [diff] [blame] | 140 | /* Reset DMA */ |
| 141 | bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0); |
| 142 | SSYNC(); |
| 143 | } else { |
| 144 | SSYNC(); |
| 145 | /* Read the last byte of packet with odd size from address fifo + 4 |
| 146 | * to trigger 1 byte access to EP0 FIFO. |
| 147 | */ |
| 148 | if (len == 1) |
| 149 | *dst = (u8)inw((unsigned long)fifo + 4); |
| 150 | else { |
| 151 | if (unlikely((unsigned long)dst & 0x01)) |
| 152 | insw_8((unsigned long)fifo, dst, len >> 1); |
| 153 | else |
| 154 | insw((unsigned long)fifo, dst, len >> 1); |
| 155 | |
| 156 | if (len & 0x01) |
| 157 | *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4); |
| 158 | } |
| 159 | } |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 160 | dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n", |
Mike Frysinger | 04f4086 | 2009-11-16 16:19:19 +0530 | [diff] [blame] | 161 | 'R', hw_ep->epnum, fifo, len, dst); |
| 162 | |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 163 | dump_fifo_data(dst, len); |
| 164 | } |
| 165 | |
| 166 | static irqreturn_t blackfin_interrupt(int irq, void *__hci) |
| 167 | { |
| 168 | unsigned long flags; |
| 169 | irqreturn_t retval = IRQ_NONE; |
| 170 | struct musb *musb = __hci; |
| 171 | |
| 172 | spin_lock_irqsave(&musb->lock, flags); |
| 173 | |
| 174 | musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); |
| 175 | musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); |
| 176 | musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); |
| 177 | |
| 178 | if (musb->int_usb || musb->int_tx || musb->int_rx) { |
| 179 | musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb); |
| 180 | musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx); |
| 181 | musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx); |
| 182 | retval = musb_interrupt(musb); |
| 183 | } |
| 184 | |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 185 | /* Start sampling ID pin, when plug is removed from MUSB */ |
Bob Liu | 68f6471 | 2010-10-23 05:12:00 -0500 | [diff] [blame] | 186 | if ((is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE |
| 187 | || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) || |
| 188 | (musb->int_usb & MUSB_INTR_DISCONNECT && is_host_active(musb))) { |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 189 | mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY); |
| 190 | musb->a_wait_bcon = TIMER_DELAY; |
| 191 | } |
| 192 | |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 193 | spin_unlock_irqrestore(&musb->lock, flags); |
| 194 | |
Sergei Shtylyov | 2f83175 | 2010-03-25 13:14:25 +0200 | [diff] [blame] | 195 | return retval; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void musb_conn_timer_handler(unsigned long _musb) |
| 199 | { |
| 200 | struct musb *musb = (void *)_musb; |
| 201 | unsigned long flags; |
| 202 | u16 val; |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 203 | static u8 toggle; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 204 | |
| 205 | spin_lock_irqsave(&musb->lock, flags); |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 206 | switch (musb->xceiv->state) { |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 207 | case OTG_STATE_A_IDLE: |
| 208 | case OTG_STATE_A_WAIT_BCON: |
| 209 | /* Start a new session */ |
| 210 | val = musb_readw(musb->mregs, MUSB_DEVCTL); |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 211 | val &= ~MUSB_DEVCTL_SESSION; |
| 212 | musb_writew(musb->mregs, MUSB_DEVCTL, val); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 213 | val |= MUSB_DEVCTL_SESSION; |
| 214 | musb_writew(musb->mregs, MUSB_DEVCTL, val); |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 215 | /* Check if musb is host or peripheral. */ |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 216 | val = musb_readw(musb->mregs, MUSB_DEVCTL); |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 217 | |
| 218 | if (!(val & MUSB_DEVCTL_BDEVICE)) { |
| 219 | gpio_set_value(musb->config->gpio_vrsel, 1); |
| 220 | musb->xceiv->state = OTG_STATE_A_WAIT_BCON; |
| 221 | } else { |
| 222 | gpio_set_value(musb->config->gpio_vrsel, 0); |
| 223 | /* Ignore VBUSERROR and SUSPEND IRQ */ |
| 224 | val = musb_readb(musb->mregs, MUSB_INTRUSBE); |
| 225 | val &= ~MUSB_INTR_VBUSERROR; |
| 226 | musb_writeb(musb->mregs, MUSB_INTRUSBE, val); |
| 227 | |
| 228 | val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR; |
| 229 | musb_writeb(musb->mregs, MUSB_INTRUSB, val); |
| 230 | if (is_otg_enabled(musb)) |
| 231 | musb->xceiv->state = OTG_STATE_B_IDLE; |
| 232 | else |
| 233 | musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB); |
| 234 | } |
| 235 | mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY); |
| 236 | break; |
| 237 | case OTG_STATE_B_IDLE: |
| 238 | |
| 239 | if (!is_peripheral_enabled(musb)) |
| 240 | break; |
| 241 | /* Start a new session. It seems that MUSB needs taking |
| 242 | * some time to recognize the type of the plug inserted? |
| 243 | */ |
| 244 | val = musb_readw(musb->mregs, MUSB_DEVCTL); |
| 245 | val |= MUSB_DEVCTL_SESSION; |
| 246 | musb_writew(musb->mregs, MUSB_DEVCTL, val); |
| 247 | val = musb_readw(musb->mregs, MUSB_DEVCTL); |
| 248 | |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 249 | if (!(val & MUSB_DEVCTL_BDEVICE)) { |
| 250 | gpio_set_value(musb->config->gpio_vrsel, 1); |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 251 | musb->xceiv->state = OTG_STATE_A_WAIT_BCON; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 252 | } else { |
| 253 | gpio_set_value(musb->config->gpio_vrsel, 0); |
| 254 | |
| 255 | /* Ignore VBUSERROR and SUSPEND IRQ */ |
| 256 | val = musb_readb(musb->mregs, MUSB_INTRUSBE); |
| 257 | val &= ~MUSB_INTR_VBUSERROR; |
| 258 | musb_writeb(musb->mregs, MUSB_INTRUSBE, val); |
| 259 | |
| 260 | val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR; |
| 261 | musb_writeb(musb->mregs, MUSB_INTRUSB, val); |
| 262 | |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 263 | /* Toggle the Soft Conn bit, so that we can response to |
| 264 | * the inserting of either A-plug or B-plug. |
| 265 | */ |
| 266 | if (toggle) { |
| 267 | val = musb_readb(musb->mregs, MUSB_POWER); |
| 268 | val &= ~MUSB_POWER_SOFTCONN; |
| 269 | musb_writeb(musb->mregs, MUSB_POWER, val); |
| 270 | toggle = 0; |
| 271 | } else { |
| 272 | val = musb_readb(musb->mregs, MUSB_POWER); |
| 273 | val |= MUSB_POWER_SOFTCONN; |
| 274 | musb_writeb(musb->mregs, MUSB_POWER, val); |
| 275 | toggle = 1; |
| 276 | } |
| 277 | /* The delay time is set to 1/4 second by default, |
| 278 | * shortening it, if accelerating A-plug detection |
| 279 | * is needed in OTG mode. |
| 280 | */ |
| 281 | mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 282 | } |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 283 | break; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 284 | default: |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 285 | dev_dbg(musb->controller, "%s state not handled\n", |
Anatolij Gustschin | 3df0045 | 2011-05-05 12:11:21 +0200 | [diff] [blame] | 286 | otg_state_string(musb->xceiv->state)); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 287 | break; |
| 288 | } |
| 289 | spin_unlock_irqrestore(&musb->lock, flags); |
| 290 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 291 | dev_dbg(musb->controller, "state is %s\n", |
| 292 | otg_state_string(musb->xceiv->state)); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 295 | static void bfin_musb_enable(struct musb *musb) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 296 | { |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 297 | if (!is_otg_enabled(musb) && is_host_enabled(musb)) { |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 298 | mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY); |
| 299 | musb->a_wait_bcon = TIMER_DELAY; |
| 300 | } |
| 301 | } |
| 302 | |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 303 | static void bfin_musb_disable(struct musb *musb) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 304 | { |
| 305 | } |
| 306 | |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 307 | static void bfin_musb_set_vbus(struct musb *musb, int is_on) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 308 | { |
Cliff Cai | 6ddc6da | 2010-03-12 10:29:10 +0200 | [diff] [blame] | 309 | int value = musb->config->gpio_vrsel_active; |
| 310 | if (!is_on) |
| 311 | value = !value; |
| 312 | gpio_set_value(musb->config->gpio_vrsel, value); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 313 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 314 | dev_dbg(musb->controller, "VBUS %s, devctl %02x " |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 315 | /* otg %3x conf %08x prcm %08x */ "\n", |
Anatolij Gustschin | 3df0045 | 2011-05-05 12:11:21 +0200 | [diff] [blame] | 316 | otg_state_string(musb->xceiv->state), |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 317 | musb_readb(musb->mregs, MUSB_DEVCTL)); |
| 318 | } |
| 319 | |
Heikki Krogerus | 8675381 | 2012-02-13 13:24:02 +0200 | [diff] [blame] | 320 | static int bfin_musb_set_power(struct usb_phy *x, unsigned mA) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 321 | { |
| 322 | return 0; |
| 323 | } |
| 324 | |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 325 | static void bfin_musb_try_idle(struct musb *musb, unsigned long timeout) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 326 | { |
Cliff Cai | ff927ad | 2010-03-25 13:25:19 +0200 | [diff] [blame] | 327 | if (!is_otg_enabled(musb) && is_host_enabled(musb)) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 328 | mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY); |
| 329 | } |
| 330 | |
Mike Frysinger | 45567c2 | 2011-03-21 14:06:32 -0400 | [diff] [blame] | 331 | static int bfin_musb_vbus_status(struct musb *musb) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 332 | { |
| 333 | return 0; |
| 334 | } |
| 335 | |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 336 | static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 337 | { |
Bryan Wu | 2002e76 | 2009-11-16 16:19:25 +0530 | [diff] [blame] | 338 | return -EIO; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Mike Frysinger | 1325430 | 2011-03-30 22:48:54 -0400 | [diff] [blame] | 341 | static int bfin_musb_adjust_channel_params(struct dma_channel *channel, |
| 342 | u16 packet_sz, u8 *mode, |
| 343 | dma_addr_t *dma_addr, u32 *len) |
| 344 | { |
| 345 | struct musb_dma_channel *musb_channel = channel->private_data; |
| 346 | |
| 347 | /* |
| 348 | * Anomaly 05000450 might cause data corruption when using DMA |
| 349 | * MODE 1 transmits with short packet. So to work around this, |
| 350 | * we truncate all MODE 1 transfers down to a multiple of the |
| 351 | * max packet size, and then do the last short packet transfer |
| 352 | * (if there is any) using MODE 0. |
| 353 | */ |
| 354 | if (ANOMALY_05000450) { |
| 355 | if (musb_channel->transmit && *mode == 1) |
| 356 | *len = *len - (*len % packet_sz); |
| 357 | } |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 362 | static void bfin_musb_reg_init(struct musb *musb) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 363 | { |
Robin Getz | d426e60 | 2008-12-02 21:33:45 +0200 | [diff] [blame] | 364 | if (ANOMALY_05000346) { |
| 365 | bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value); |
| 366 | SSYNC(); |
| 367 | } |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 368 | |
Robin Getz | d426e60 | 2008-12-02 21:33:45 +0200 | [diff] [blame] | 369 | if (ANOMALY_05000347) { |
| 370 | bfin_write_USB_APHY_CNTRL(0x0); |
| 371 | SSYNC(); |
| 372 | } |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 373 | |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 374 | /* Configure PLL oscillator register */ |
Bob Liu | 9c75646 | 2010-10-23 05:12:01 -0500 | [diff] [blame] | 375 | bfin_write_USB_PLLOSC_CTRL(0x3080 | |
| 376 | ((480/musb->config->clkin) << 1)); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 377 | SSYNC(); |
| 378 | |
| 379 | bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1); |
| 380 | SSYNC(); |
| 381 | |
| 382 | bfin_write_USB_EP_NI0_RXMAXP(64); |
| 383 | SSYNC(); |
| 384 | |
| 385 | bfin_write_USB_EP_NI0_TXMAXP(64); |
| 386 | SSYNC(); |
| 387 | |
| 388 | /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/ |
| 389 | bfin_write_USB_GLOBINTR(0x7); |
| 390 | SSYNC(); |
| 391 | |
| 392 | bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA | |
| 393 | EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA | |
| 394 | EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA | |
| 395 | EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA | |
| 396 | EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA); |
| 397 | SSYNC(); |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static int bfin_musb_init(struct musb *musb) |
| 401 | { |
| 402 | |
| 403 | /* |
| 404 | * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE |
| 405 | * and OTG HOST modes, while rev 1.1 and greater require PE7 to |
| 406 | * be low for DEVICE mode and high for HOST mode. We set it high |
| 407 | * here because we are in host mode |
| 408 | */ |
| 409 | |
| 410 | if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) { |
| 411 | printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n", |
| 412 | musb->config->gpio_vrsel); |
| 413 | return -ENODEV; |
| 414 | } |
| 415 | gpio_direction_output(musb->config->gpio_vrsel, 0); |
| 416 | |
| 417 | usb_nop_xceiv_register(); |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 418 | musb->xceiv = usb_get_transceiver(); |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 419 | if (!musb->xceiv) { |
| 420 | gpio_free(musb->config->gpio_vrsel); |
| 421 | return -ENODEV; |
| 422 | } |
| 423 | |
| 424 | bfin_musb_reg_init(musb); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 425 | |
| 426 | if (is_host_enabled(musb)) { |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 427 | setup_timer(&musb_conn_timer, |
| 428 | musb_conn_timer_handler, (unsigned long) musb); |
| 429 | } |
| 430 | if (is_peripheral_enabled(musb)) |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 431 | musb->xceiv->set_power = bfin_musb_set_power; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 432 | |
| 433 | musb->isr = blackfin_interrupt; |
Felipe Balbi | 0662481 | 2011-01-21 13:39:20 +0800 | [diff] [blame] | 434 | musb->double_buffer_not_ok = true; |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 439 | static int bfin_musb_exit(struct musb *musb) |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 440 | { |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 441 | gpio_free(musb->config->gpio_vrsel); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 442 | |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 443 | usb_put_transceiver(musb->xceiv); |
Sergei Shtylyov | 3daad24 | 2010-09-29 09:54:30 +0300 | [diff] [blame] | 444 | usb_nop_xceiv_unregister(); |
Bryan Wu | 0c6a881 | 2008-12-02 21:33:44 +0200 | [diff] [blame] | 445 | return 0; |
| 446 | } |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 447 | |
Felipe Balbi | f7ec943 | 2010-12-02 09:48:58 +0200 | [diff] [blame] | 448 | static const struct musb_platform_ops bfin_ops = { |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 449 | .init = bfin_musb_init, |
| 450 | .exit = bfin_musb_exit, |
| 451 | |
| 452 | .enable = bfin_musb_enable, |
| 453 | .disable = bfin_musb_disable, |
| 454 | |
| 455 | .set_mode = bfin_musb_set_mode, |
| 456 | .try_idle = bfin_musb_try_idle, |
| 457 | |
| 458 | .vbus_status = bfin_musb_vbus_status, |
| 459 | .set_vbus = bfin_musb_set_vbus, |
Mike Frysinger | 1325430 | 2011-03-30 22:48:54 -0400 | [diff] [blame] | 460 | |
| 461 | .adjust_channel_params = bfin_musb_adjust_channel_params, |
Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 462 | }; |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 463 | |
| 464 | static u64 bfin_dmamask = DMA_BIT_MASK(32); |
| 465 | |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 466 | static int __devinit bfin_probe(struct platform_device *pdev) |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 467 | { |
| 468 | struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data; |
| 469 | struct platform_device *musb; |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 470 | struct bfin_glue *glue; |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 471 | |
| 472 | int ret = -ENOMEM; |
| 473 | |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 474 | glue = kzalloc(sizeof(*glue), GFP_KERNEL); |
| 475 | if (!glue) { |
| 476 | dev_err(&pdev->dev, "failed to allocate glue context\n"); |
| 477 | goto err0; |
| 478 | } |
| 479 | |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 480 | musb = platform_device_alloc("musb-hdrc", -1); |
| 481 | if (!musb) { |
| 482 | dev_err(&pdev->dev, "failed to allocate musb device\n"); |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 483 | goto err1; |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | musb->dev.parent = &pdev->dev; |
| 487 | musb->dev.dma_mask = &bfin_dmamask; |
| 488 | musb->dev.coherent_dma_mask = bfin_dmamask; |
| 489 | |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 490 | glue->dev = &pdev->dev; |
| 491 | glue->musb = musb; |
| 492 | |
Felipe Balbi | f7ec943 | 2010-12-02 09:48:58 +0200 | [diff] [blame] | 493 | pdata->platform_ops = &bfin_ops; |
| 494 | |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 495 | platform_set_drvdata(pdev, glue); |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 496 | |
| 497 | ret = platform_device_add_resources(musb, pdev->resource, |
| 498 | pdev->num_resources); |
| 499 | if (ret) { |
| 500 | dev_err(&pdev->dev, "failed to add resources\n"); |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 501 | goto err2; |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | ret = platform_device_add_data(musb, pdata, sizeof(*pdata)); |
| 505 | if (ret) { |
| 506 | dev_err(&pdev->dev, "failed to add platform_data\n"); |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 507 | goto err2; |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | ret = platform_device_add(musb); |
| 511 | if (ret) { |
| 512 | dev_err(&pdev->dev, "failed to register musb device\n"); |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 513 | goto err2; |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 518 | err2: |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 519 | platform_device_put(musb); |
| 520 | |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 521 | err1: |
| 522 | kfree(glue); |
| 523 | |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 524 | err0: |
| 525 | return ret; |
| 526 | } |
| 527 | |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 528 | static int __devexit bfin_remove(struct platform_device *pdev) |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 529 | { |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 530 | struct bfin_glue *glue = platform_get_drvdata(pdev); |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 531 | |
Felipe Balbi | a023c63 | 2010-12-02 09:42:50 +0200 | [diff] [blame] | 532 | platform_device_del(glue->musb); |
| 533 | platform_device_put(glue->musb); |
| 534 | kfree(glue); |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
Felipe Balbi | fcd22e3 | 2010-12-02 13:13:09 +0200 | [diff] [blame] | 539 | #ifdef CONFIG_PM |
| 540 | static int bfin_suspend(struct device *dev) |
| 541 | { |
| 542 | struct bfin_glue *glue = dev_get_drvdata(dev); |
| 543 | struct musb *musb = glue_to_musb(glue); |
| 544 | |
| 545 | if (is_host_active(musb)) |
| 546 | /* |
| 547 | * During hibernate gpio_vrsel will change from high to low |
| 548 | * low which will generate wakeup event resume the system |
| 549 | * immediately. Set it to 0 before hibernate to avoid this |
| 550 | * wakeup event. |
| 551 | */ |
| 552 | gpio_set_value(musb->config->gpio_vrsel, 0); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static int bfin_resume(struct device *dev) |
| 558 | { |
| 559 | struct bfin_glue *glue = dev_get_drvdata(dev); |
| 560 | struct musb *musb = glue_to_musb(glue); |
| 561 | |
| 562 | bfin_musb_reg_init(musb); |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static struct dev_pm_ops bfin_pm_ops = { |
| 568 | .suspend = bfin_suspend, |
| 569 | .resume = bfin_resume, |
| 570 | }; |
| 571 | |
Bob Liu | 8f7e7b8 | 2011-03-21 14:06:31 -0400 | [diff] [blame] | 572 | #define DEV_PM_OPS &bfin_pm_ops |
Felipe Balbi | fcd22e3 | 2010-12-02 13:13:09 +0200 | [diff] [blame] | 573 | #else |
| 574 | #define DEV_PM_OPS NULL |
| 575 | #endif |
| 576 | |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 577 | static struct platform_driver bfin_driver = { |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 578 | .probe = bfin_probe, |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 579 | .remove = __exit_p(bfin_remove), |
| 580 | .driver = { |
Mike Frysinger | 417ddf8 | 2011-03-22 14:43:37 -0400 | [diff] [blame] | 581 | .name = "musb-blackfin", |
Felipe Balbi | fcd22e3 | 2010-12-02 13:13:09 +0200 | [diff] [blame] | 582 | .pm = DEV_PM_OPS, |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 583 | }, |
| 584 | }; |
| 585 | |
| 586 | MODULE_DESCRIPTION("Blackfin MUSB Glue Layer"); |
| 587 | MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>"); |
| 588 | MODULE_LICENSE("GPL v2"); |
| 589 | |
| 590 | static int __init bfin_init(void) |
| 591 | { |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 592 | return platform_driver_register(&bfin_driver); |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 593 | } |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 594 | module_init(bfin_init); |
Felipe Balbi | 9cb0308 | 2010-12-02 09:21:05 +0200 | [diff] [blame] | 595 | |
| 596 | static void __exit bfin_exit(void) |
| 597 | { |
| 598 | platform_driver_unregister(&bfin_driver); |
| 599 | } |
| 600 | module_exit(bfin_exit); |