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