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