Merge branch 'release-2.6.27' of git://git.kernel.org/pub/scm/linux/kernel/git/ak...
[pandora-kernel.git] / drivers / usb / host / ohci-pnx4008.c
1 /*
2  * drivers/usb/host/ohci-pnx4008.c
3  *
4  * driver for Philips PNX4008 USB Host
5  *
6  * Authors: Dmitry Chigirev <source@mvista.com>
7  *          Vitaly Wool <vitalywool@gmail.com>
8  *
9  * register initialization is based on code examples provided by Philips
10  * Copyright (c) 2005 Koninklijke Philips Electronics N.V.
11  *
12  * NOTE: This driver does not have suspend/resume functionality
13  * This driver is intended for engineering development purposes only
14  *
15  * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
16  * the terms of the GNU General Public License version 2. This program
17  * is licensed "as is" without any warranty of any kind, whether express
18  * or implied.
19  */
20 #include <linux/clk.h>
21 #include <linux/platform_device.h>
22 #include <linux/i2c.h>
23
24 #include <asm/hardware.h>
25 #include <asm/io.h>
26 #include <asm/mach-types.h>
27
28 #include <asm/arch/platform.h>
29 #include <asm/arch/irqs.h>
30 #include <asm/arch/gpio.h>
31
32 #define USB_CTRL        IO_ADDRESS(PNX4008_PWRMAN_BASE + 0x64)
33
34 /* USB_CTRL bit defines */
35 #define USB_SLAVE_HCLK_EN       (1 << 24)
36 #define USB_HOST_NEED_CLK_EN    (1 << 21)
37
38 #define USB_OTG_CLK_CTRL        IO_ADDRESS(PNX4008_USB_CONFIG_BASE + 0xFF4)
39 #define USB_OTG_CLK_STAT        IO_ADDRESS(PNX4008_USB_CONFIG_BASE + 0xFF8)
40
41 /* USB_OTG_CLK_CTRL bit defines */
42 #define AHB_M_CLOCK_ON          (1 << 4)
43 #define OTG_CLOCK_ON            (1 << 3)
44 #define I2C_CLOCK_ON            (1 << 2)
45 #define DEV_CLOCK_ON            (1 << 1)
46 #define HOST_CLOCK_ON           (1 << 0)
47
48 #define USB_OTG_STAT_CONTROL    IO_ADDRESS(PNX4008_USB_CONFIG_BASE + 0x110)
49
50 /* USB_OTG_STAT_CONTROL bit defines */
51 #define TRANSPARENT_I2C_EN      (1 << 7)
52 #define HOST_EN                 (1 << 0)
53
54 /* ISP1301 USB transceiver I2C registers */
55 #define ISP1301_MODE_CONTROL_1          0x04    /* u8 read, set, +1 clear */
56
57 #define MC1_SPEED_REG           (1 << 0)
58 #define MC1_SUSPEND_REG         (1 << 1)
59 #define MC1_DAT_SE0             (1 << 2)
60 #define MC1_TRANSPARENT         (1 << 3)
61 #define MC1_BDIS_ACON_EN        (1 << 4)
62 #define MC1_OE_INT_EN           (1 << 5)
63 #define MC1_UART_EN             (1 << 6)
64 #define MC1_MASK                0x7f
65
66 #define ISP1301_MODE_CONTROL_2          0x12    /* u8 read, set, +1 clear */
67
68 #define MC2_GLOBAL_PWR_DN       (1 << 0)
69 #define MC2_SPD_SUSP_CTRL       (1 << 1)
70 #define MC2_BI_DI               (1 << 2)
71 #define MC2_TRANSP_BDIR0        (1 << 3)
72 #define MC2_TRANSP_BDIR1        (1 << 4)
73 #define MC2_AUDIO_EN            (1 << 5)
74 #define MC2_PSW_EN              (1 << 6)
75 #define MC2_EN2V7               (1 << 7)
76
77 #define ISP1301_OTG_CONTROL_1           0x06    /* u8 read, set, +1 clear */
78 #       define  OTG1_DP_PULLUP          (1 << 0)
79 #       define  OTG1_DM_PULLUP          (1 << 1)
80 #       define  OTG1_DP_PULLDOWN        (1 << 2)
81 #       define  OTG1_DM_PULLDOWN        (1 << 3)
82 #       define  OTG1_ID_PULLDOWN        (1 << 4)
83 #       define  OTG1_VBUS_DRV           (1 << 5)
84 #       define  OTG1_VBUS_DISCHRG       (1 << 6)
85 #       define  OTG1_VBUS_CHRG          (1 << 7)
86 #define ISP1301_OTG_STATUS              0x10    /* u8 readonly */
87 #       define  OTG_B_SESS_END          (1 << 6)
88 #       define  OTG_B_SESS_VLD          (1 << 7)
89
90 #define ISP1301_I2C_ADDR 0x2C
91
92 #define ISP1301_I2C_MODE_CONTROL_1 0x4
93 #define ISP1301_I2C_MODE_CONTROL_2 0x12
94 #define ISP1301_I2C_OTG_CONTROL_1 0x6
95 #define ISP1301_I2C_OTG_CONTROL_2 0x10
96 #define ISP1301_I2C_INTERRUPT_SOURCE 0x8
97 #define ISP1301_I2C_INTERRUPT_LATCH 0xA
98 #define ISP1301_I2C_INTERRUPT_FALLING 0xC
99 #define ISP1301_I2C_INTERRUPT_RISING 0xE
100 #define ISP1301_I2C_REG_CLEAR_ADDR 1
101
102 struct i2c_driver isp1301_driver;
103 struct i2c_client *isp1301_i2c_client;
104
105 extern int usb_disabled(void);
106 extern int ocpi_enable(void);
107
108 static struct clk *usb_clk;
109
110 static int isp1301_probe(struct i2c_adapter *adap);
111 static int isp1301_detach(struct i2c_client *client);
112
113 static const unsigned short normal_i2c[] =
114     { ISP1301_I2C_ADDR, ISP1301_I2C_ADDR + 1, I2C_CLIENT_END };
115 static const unsigned short dummy_i2c_addrlist[] = { I2C_CLIENT_END };
116
117 static struct i2c_client_address_data addr_data = {
118         .normal_i2c = normal_i2c,
119         .probe = dummy_i2c_addrlist,
120         .ignore = dummy_i2c_addrlist,
121 };
122
123 struct i2c_driver isp1301_driver = {
124         .driver = {
125                 .name = "isp1301_pnx",
126         },
127         .attach_adapter = isp1301_probe,
128         .detach_client = isp1301_detach,
129 };
130
131 static int isp1301_attach(struct i2c_adapter *adap, int addr, int kind)
132 {
133         struct i2c_client *c;
134         int err;
135
136         c = kzalloc(sizeof(*c), GFP_KERNEL);
137         if (!c)
138                 return -ENOMEM;
139
140         strlcpy(c->name, "isp1301_pnx", I2C_NAME_SIZE);
141         c->flags = 0;
142         c->addr = addr;
143         c->adapter = adap;
144         c->driver = &isp1301_driver;
145
146         err = i2c_attach_client(c);
147         if (err) {
148                 kfree(c);
149                 return err;
150         }
151
152         isp1301_i2c_client = c;
153
154         return 0;
155 }
156
157 static int isp1301_probe(struct i2c_adapter *adap)
158 {
159         return i2c_probe(adap, &addr_data, isp1301_attach);
160 }
161
162 static int isp1301_detach(struct i2c_client *client)
163 {
164         i2c_detach_client(client);
165         kfree(isp1301_i2c_client);
166         return 0;
167 }
168
169 static void i2c_write(u8 buf, u8 subaddr)
170 {
171         char tmpbuf[2];
172
173         tmpbuf[0] = subaddr;    /*register number */
174         tmpbuf[1] = buf;        /*register data */
175         i2c_master_send(isp1301_i2c_client, &tmpbuf[0], 2);
176 }
177
178 static void isp1301_configure(void)
179 {
180         /* PNX4008 only supports DAT_SE0 USB mode */
181         /* PNX4008 R2A requires setting the MAX603 to output 3.6V */
182         /* Power up externel charge-pump */
183
184         i2c_write(MC1_DAT_SE0 | MC1_SPEED_REG, ISP1301_I2C_MODE_CONTROL_1);
185         i2c_write(~(MC1_DAT_SE0 | MC1_SPEED_REG),
186                   ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
187         i2c_write(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL,
188                   ISP1301_I2C_MODE_CONTROL_2);
189         i2c_write(~(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL),
190                   ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR);
191         i2c_write(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN,
192                   ISP1301_I2C_OTG_CONTROL_1);
193         i2c_write(~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN),
194                   ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
195         i2c_write(0xFF,
196                   ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR);
197         i2c_write(0xFF,
198                   ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR);
199         i2c_write(0xFF,
200                   ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR);
201
202 }
203
204 static inline void isp1301_vbus_on(void)
205 {
206         i2c_write(OTG1_VBUS_DRV, ISP1301_I2C_OTG_CONTROL_1);
207 }
208
209 static inline void isp1301_vbus_off(void)
210 {
211         i2c_write(OTG1_VBUS_DRV,
212                   ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
213 }
214
215 static void pnx4008_start_hc(void)
216 {
217         unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
218         __raw_writel(tmp, USB_OTG_STAT_CONTROL);
219         isp1301_vbus_on();
220 }
221
222 static void pnx4008_stop_hc(void)
223 {
224         unsigned long tmp;
225         isp1301_vbus_off();
226         tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN;
227         __raw_writel(tmp, USB_OTG_STAT_CONTROL);
228 }
229
230 static int __devinit ohci_pnx4008_start(struct usb_hcd *hcd)
231 {
232         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
233         int ret;
234
235         if ((ret = ohci_init(ohci)) < 0)
236                 return ret;
237
238         if ((ret = ohci_run(ohci)) < 0) {
239                 dev_err(hcd->self.controller, "can't start\n");
240                 ohci_stop(hcd);
241                 return ret;
242         }
243         return 0;
244 }
245
246 static const struct hc_driver ohci_pnx4008_hc_driver = {
247         .description = hcd_name,
248         .product_desc =         "pnx4008 OHCI",
249
250         /*
251          * generic hardware linkage
252          */
253         .irq = ohci_irq,
254         .flags = HCD_USB11 | HCD_MEMORY,
255
256         .hcd_priv_size =        sizeof(struct ohci_hcd),
257         /*
258          * basic lifecycle operations
259          */
260         .start = ohci_pnx4008_start,
261         .stop = ohci_stop,
262         .shutdown = ohci_shutdown,
263
264         /*
265          * managing i/o requests and associated device resources
266          */
267         .urb_enqueue = ohci_urb_enqueue,
268         .urb_dequeue = ohci_urb_dequeue,
269         .endpoint_disable = ohci_endpoint_disable,
270
271         /*
272          * scheduling support
273          */
274         .get_frame_number = ohci_get_frame,
275
276         /*
277          * root hub support
278          */
279         .hub_status_data = ohci_hub_status_data,
280         .hub_control = ohci_hub_control,
281         .hub_irq_enable = ohci_rhsc_enable,
282 #ifdef  CONFIG_PM
283         .bus_suspend = ohci_bus_suspend,
284         .bus_resume = ohci_bus_resume,
285 #endif
286         .start_port_reset = ohci_start_port_reset,
287 };
288
289 #define USB_CLOCK_MASK (AHB_M_CLOCK_ON| OTG_CLOCK_ON | HOST_CLOCK_ON | I2C_CLOCK_ON)
290
291 static void pnx4008_set_usb_bits(void)
292 {
293         start_int_set_falling_edge(SE_USB_OTG_ATX_INT_N);
294         start_int_ack(SE_USB_OTG_ATX_INT_N);
295         start_int_umask(SE_USB_OTG_ATX_INT_N);
296
297         start_int_set_rising_edge(SE_USB_OTG_TIMER_INT);
298         start_int_ack(SE_USB_OTG_TIMER_INT);
299         start_int_umask(SE_USB_OTG_TIMER_INT);
300
301         start_int_set_rising_edge(SE_USB_I2C_INT);
302         start_int_ack(SE_USB_I2C_INT);
303         start_int_umask(SE_USB_I2C_INT);
304
305         start_int_set_rising_edge(SE_USB_INT);
306         start_int_ack(SE_USB_INT);
307         start_int_umask(SE_USB_INT);
308
309         start_int_set_rising_edge(SE_USB_NEED_CLK_INT);
310         start_int_ack(SE_USB_NEED_CLK_INT);
311         start_int_umask(SE_USB_NEED_CLK_INT);
312
313         start_int_set_rising_edge(SE_USB_AHB_NEED_CLK_INT);
314         start_int_ack(SE_USB_AHB_NEED_CLK_INT);
315         start_int_umask(SE_USB_AHB_NEED_CLK_INT);
316 }
317
318 static void pnx4008_unset_usb_bits(void)
319 {
320         start_int_mask(SE_USB_OTG_ATX_INT_N);
321         start_int_mask(SE_USB_OTG_TIMER_INT);
322         start_int_mask(SE_USB_I2C_INT);
323         start_int_mask(SE_USB_INT);
324         start_int_mask(SE_USB_NEED_CLK_INT);
325         start_int_mask(SE_USB_AHB_NEED_CLK_INT);
326 }
327
328 static int __devinit usb_hcd_pnx4008_probe(struct platform_device *pdev)
329 {
330         struct usb_hcd *hcd = 0;
331         struct ohci_hcd *ohci;
332         const struct hc_driver *driver = &ohci_pnx4008_hc_driver;
333
334         int ret = 0, irq;
335
336         dev_dbg(&pdev->dev, "%s: " DRIVER_INFO " (pnx4008)\n", hcd_name);
337         if (usb_disabled()) {
338                 err("USB is disabled");
339                 ret = -ENODEV;
340                 goto out;
341         }
342
343         if (pdev->num_resources != 2
344             || pdev->resource[0].flags != IORESOURCE_MEM
345             || pdev->resource[1].flags != IORESOURCE_IRQ) {
346                 err("Invalid resource configuration");
347                 ret = -ENODEV;
348                 goto out;
349         }
350
351         /* Enable AHB slave USB clock, needed for further USB clock control */
352         __raw_writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
353
354         ret = i2c_add_driver(&isp1301_driver);
355         if (ret < 0) {
356                 err("failed to connect I2C to ISP1301 USB Transceiver");
357                 goto out;
358         }
359
360         isp1301_configure();
361
362         /* Enable USB PLL */
363         usb_clk = clk_get(&pdev->dev, "ck_pll5");
364         if (IS_ERR(usb_clk)) {
365                 err("failed to acquire USB PLL");
366                 ret = PTR_ERR(usb_clk);
367                 goto out1;
368         }
369
370         ret = clk_enable(usb_clk);
371         if (ret < 0) {
372                 err("failed to start USB PLL");
373                 goto out2;
374         }
375
376         ret = clk_set_rate(usb_clk, 48000);
377         if (ret < 0) {
378                 err("failed to set USB clock rate");
379                 goto out3;
380         }
381
382         __raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
383
384         /* Set to enable all needed USB clocks */
385         __raw_writel(USB_CLOCK_MASK, USB_OTG_CLK_CTRL);
386
387         while ((__raw_readl(USB_OTG_CLK_STAT) & USB_CLOCK_MASK) !=
388                USB_CLOCK_MASK) ;
389
390         hcd = usb_create_hcd (driver, &pdev->dev, dev_name(&pdev->dev));
391         if (!hcd) {
392                 err("Failed to allocate HC buffer");
393                 ret = -ENOMEM;
394                 goto out3;
395         }
396
397         /* Set all USB bits in the Start Enable register */
398         pnx4008_set_usb_bits();
399
400         hcd->rsrc_start = pdev->resource[0].start;
401         hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
402         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
403                 dev_dbg(&pdev->dev, "request_mem_region failed\n");
404                 ret =  -ENOMEM;
405                 goto out4;
406         }
407         hcd->regs = (void __iomem *)pdev->resource[0].start;
408
409         irq = platform_get_irq(pdev, 0);
410         if (irq < 0) {
411                 ret = -ENXIO;
412                 goto out4;
413         }
414
415         pnx4008_start_hc();
416         platform_set_drvdata(pdev, hcd);
417         ohci = hcd_to_ohci(hcd);
418         ohci_hcd_init(ohci);
419
420         dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
421         ret = usb_add_hcd(hcd, irq, IRQF_DISABLED);
422         if (ret == 0)
423                 return ret;
424
425         pnx4008_stop_hc();
426 out4:
427         pnx4008_unset_usb_bits();
428         usb_put_hcd(hcd);
429 out3:
430         clk_disable(usb_clk);
431 out2:
432         clk_put(usb_clk);
433 out1:
434         i2c_del_driver(&isp1301_driver);
435 out:
436         return ret;
437 }
438
439 static int usb_hcd_pnx4008_remove(struct platform_device *pdev)
440 {
441         struct usb_hcd *hcd = platform_get_drvdata(pdev);
442
443         usb_remove_hcd(hcd);
444         pnx4008_stop_hc();
445         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
446         usb_put_hcd(hcd);
447         pnx4008_unset_usb_bits();
448         clk_disable(usb_clk);
449         clk_put(usb_clk);
450         i2c_del_driver(&isp1301_driver);
451
452         platform_set_drvdata(pdev, NULL);
453
454         return 0;
455 }
456
457 /* work with hotplug and coldplug */
458 MODULE_ALIAS("platform:usb-ohci");
459
460 static struct platform_driver usb_hcd_pnx4008_driver = {
461         .driver = {
462                 .name = "usb-ohci",
463                 .owner  = THIS_MODULE,
464         },
465         .probe = usb_hcd_pnx4008_probe,
466         .remove = usb_hcd_pnx4008_remove,
467 };
468