perf: Fix NULL deref in inheritance code
[firefly-linux-kernel-4.4.55.git] / drivers / usb / musb / blackfin.c
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>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/list.h>
17 #include <linux/gpio.h>
18 #include <linux/io.h>
19
20 #include <asm/cacheflush.h>
21
22 #include "musb_core.h"
23 #include "blackfin.h"
24
25 /*
26  * Load an endpoint's FIFO
27  */
28 void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
29 {
30         void __iomem *fifo = hw_ep->fifo;
31         void __iomem *epio = hw_ep->regs;
32
33         prefetch((u8 *)src);
34
35         musb_writew(epio, MUSB_TXCOUNT, len);
36
37         DBG(4, "TX ep%d fifo %p count %d buf %p, epio %p\n",
38                         hw_ep->epnum, fifo, len, src, epio);
39
40         dump_fifo_data(src, len);
41
42         if (unlikely((unsigned long)src & 0x01))
43                 outsw_8((unsigned long)fifo, src,
44                         len & 0x01 ? (len >> 1) + 1 : len >> 1);
45         else
46                 outsw((unsigned long)fifo, src,
47                         len & 0x01 ? (len >> 1) + 1 : len >> 1);
48 }
49
50 /*
51  * Unload an endpoint's FIFO
52  */
53 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
54 {
55         void __iomem *fifo = hw_ep->fifo;
56
57 #ifdef CONFIG_BF52x
58         u8 epnum = hw_ep->epnum;
59         u16 dma_reg = 0;
60
61         invalidate_dcache_range((unsigned int)dst,
62                 (unsigned int)(dst + len));
63
64         /* Setup DMA address register */
65         dma_reg = (u16) ((u32) dst & 0xFFFF);
66         bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
67         SSYNC();
68
69         dma_reg = (u16) (((u32) dst >> 16) & 0xFFFF);
70         bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
71         SSYNC();
72
73         /* Setup DMA count register */
74         bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
75         bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
76         SSYNC();
77
78         /* Enable the DMA */
79         dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
80         bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
81         SSYNC();
82
83         /* Wait for compelete */
84         while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
85                 cpu_relax();
86
87         /* acknowledge dma interrupt */
88         bfin_write_USB_DMA_INTERRUPT(1 << epnum);
89         SSYNC();
90
91         /* Reset DMA */
92         bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
93         SSYNC();
94 #else
95         if (unlikely((unsigned long)dst & 0x01))
96                 insw_8((unsigned long)fifo, dst,
97                         len & 0x01 ? (len >> 1) + 1 : len >> 1);
98         else
99                 insw((unsigned long)fifo, dst,
100                         len & 0x01 ? (len >> 1) + 1 : len >> 1);
101 #endif
102
103         DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
104                         'R', hw_ep->epnum, fifo, len, dst);
105
106         dump_fifo_data(dst, len);
107 }
108
109 static irqreturn_t blackfin_interrupt(int irq, void *__hci)
110 {
111         unsigned long   flags;
112         irqreturn_t     retval = IRQ_NONE;
113         struct musb     *musb = __hci;
114
115         spin_lock_irqsave(&musb->lock, flags);
116
117         musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
118         musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
119         musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
120
121         if (musb->int_usb || musb->int_tx || musb->int_rx) {
122                 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
123                 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
124                 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
125                 retval = musb_interrupt(musb);
126         }
127
128         spin_unlock_irqrestore(&musb->lock, flags);
129
130         /* REVISIT we sometimes get spurious IRQs on g_ep0
131          * not clear why... fall in BF54x too.
132          */
133         if (retval != IRQ_HANDLED)
134                 DBG(5, "spurious?\n");
135
136         return IRQ_HANDLED;
137 }
138
139 static void musb_conn_timer_handler(unsigned long _musb)
140 {
141         struct musb *musb = (void *)_musb;
142         unsigned long flags;
143         u16 val;
144
145         spin_lock_irqsave(&musb->lock, flags);
146         switch (musb->xceiv->state) {
147         case OTG_STATE_A_IDLE:
148         case OTG_STATE_A_WAIT_BCON:
149                 /* Start a new session */
150                 val = musb_readw(musb->mregs, MUSB_DEVCTL);
151                 val |= MUSB_DEVCTL_SESSION;
152                 musb_writew(musb->mregs, MUSB_DEVCTL, val);
153
154                 val = musb_readw(musb->mregs, MUSB_DEVCTL);
155                 if (!(val & MUSB_DEVCTL_BDEVICE)) {
156                         gpio_set_value(musb->config->gpio_vrsel, 1);
157                         musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
158                 } else {
159                         gpio_set_value(musb->config->gpio_vrsel, 0);
160
161                         /* Ignore VBUSERROR and SUSPEND IRQ */
162                         val = musb_readb(musb->mregs, MUSB_INTRUSBE);
163                         val &= ~MUSB_INTR_VBUSERROR;
164                         musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
165
166                         val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
167                         musb_writeb(musb->mregs, MUSB_INTRUSB, val);
168
169                         val = MUSB_POWER_HSENAB;
170                         musb_writeb(musb->mregs, MUSB_POWER, val);
171                 }
172                 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
173                 break;
174
175         default:
176                 DBG(1, "%s state not handled\n", otg_state_string(musb));
177                 break;
178         }
179         spin_unlock_irqrestore(&musb->lock, flags);
180
181         DBG(4, "state is %s\n", otg_state_string(musb));
182 }
183
184 void musb_platform_enable(struct musb *musb)
185 {
186         if (is_host_enabled(musb)) {
187                 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
188                 musb->a_wait_bcon = TIMER_DELAY;
189         }
190 }
191
192 void musb_platform_disable(struct musb *musb)
193 {
194 }
195
196 static void bfin_vbus_power(struct musb *musb, int is_on, int sleeping)
197 {
198 }
199
200 static void bfin_set_vbus(struct musb *musb, int is_on)
201 {
202         if (is_on)
203                 gpio_set_value(musb->config->gpio_vrsel, 1);
204         else
205                 gpio_set_value(musb->config->gpio_vrsel, 0);
206
207         DBG(1, "VBUS %s, devctl %02x "
208                 /* otg %3x conf %08x prcm %08x */ "\n",
209                 otg_state_string(musb),
210                 musb_readb(musb->mregs, MUSB_DEVCTL));
211 }
212
213 static int bfin_set_power(struct otg_transceiver *x, unsigned mA)
214 {
215         return 0;
216 }
217
218 void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
219 {
220         if (is_host_enabled(musb))
221                 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
222 }
223
224 int musb_platform_get_vbus_status(struct musb *musb)
225 {
226         return 0;
227 }
228
229 int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
230 {
231         return -EIO;
232 }
233
234 int __init musb_platform_init(struct musb *musb)
235 {
236
237         /*
238          * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
239          * and OTG HOST modes, while rev 1.1 and greater require PE7 to
240          * be low for DEVICE mode and high for HOST mode. We set it high
241          * here because we are in host mode
242          */
243
244         if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
245                 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d \n",
246                         musb->config->gpio_vrsel);
247                 return -ENODEV;
248         }
249         gpio_direction_output(musb->config->gpio_vrsel, 0);
250
251         usb_nop_xceiv_register();
252         musb->xceiv = otg_get_transceiver();
253         if (!musb->xceiv)
254                 return -ENODEV;
255
256         if (ANOMALY_05000346) {
257                 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
258                 SSYNC();
259         }
260
261         if (ANOMALY_05000347) {
262                 bfin_write_USB_APHY_CNTRL(0x0);
263                 SSYNC();
264         }
265
266         /* Configure PLL oscillator register */
267         bfin_write_USB_PLLOSC_CTRL(0x30a8);
268         SSYNC();
269
270         bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
271         SSYNC();
272
273         bfin_write_USB_EP_NI0_RXMAXP(64);
274         SSYNC();
275
276         bfin_write_USB_EP_NI0_TXMAXP(64);
277         SSYNC();
278
279         /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
280         bfin_write_USB_GLOBINTR(0x7);
281         SSYNC();
282
283         bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
284                                 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
285                                 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
286                                 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
287                                 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
288         SSYNC();
289
290         if (is_host_enabled(musb)) {
291                 musb->board_set_vbus = bfin_set_vbus;
292                 setup_timer(&musb_conn_timer,
293                         musb_conn_timer_handler, (unsigned long) musb);
294         }
295         if (is_peripheral_enabled(musb))
296                 musb->xceiv->set_power = bfin_set_power;
297
298         musb->isr = blackfin_interrupt;
299
300         return 0;
301 }
302
303 int musb_platform_suspend(struct musb *musb)
304 {
305         return 0;
306 }
307
308 int musb_platform_resume(struct musb *musb)
309 {
310         return 0;
311 }
312
313
314 int musb_platform_exit(struct musb *musb)
315 {
316
317         bfin_vbus_power(musb, 0 /*off*/, 1);
318         gpio_free(musb->config->gpio_vrsel);
319         musb_platform_suspend(musb);
320
321         return 0;
322 }