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