xref: /openbmc/linux/drivers/usb/host/ohci-nxp.c (revision 95e9fd10)
1 /*
2  * driver for NXP USB Host devices
3  *
4  * Currently supported OHCI host devices:
5  * - Philips PNX4008
6  * - NXP LPC32xx
7  *
8  * Authors: Dmitry Chigirev <source@mvista.com>
9  *	    Vitaly Wool <vitalywool@gmail.com>
10  *
11  * register initialization is based on code examples provided by Philips
12  * Copyright (c) 2005 Koninklijke Philips Electronics N.V.
13  *
14  * NOTE: This driver does not have suspend/resume functionality
15  * This driver is intended for engineering development purposes only
16  *
17  * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
18  * the terms of the GNU General Public License version 2. This program
19  * is licensed "as is" without any warranty of any kind, whether express
20  * or implied.
21  */
22 #include <linux/clk.h>
23 #include <linux/platform_device.h>
24 #include <linux/i2c.h>
25 #include <linux/of.h>
26 #include <linux/usb/isp1301.h>
27 
28 #include <mach/hardware.h>
29 #include <asm/mach-types.h>
30 #include <asm/io.h>
31 
32 #include <mach/platform.h>
33 #include <mach/irqs.h>
34 
35 #define USB_CONFIG_BASE		0x31020000
36 #define PWRMAN_BASE		0x40004000
37 
38 #define USB_CTRL		IO_ADDRESS(PWRMAN_BASE + 0x64)
39 
40 /* USB_CTRL bit defines */
41 #define USB_SLAVE_HCLK_EN	(1 << 24)
42 #define USB_DEV_NEED_CLK_EN	(1 << 22)
43 #define USB_HOST_NEED_CLK_EN	(1 << 21)
44 #define PAD_CONTROL_LAST_DRIVEN	(1 << 19)
45 
46 #define USB_OTG_STAT_CONTROL	IO_ADDRESS(USB_CONFIG_BASE + 0x110)
47 
48 /* USB_OTG_STAT_CONTROL bit defines */
49 #define TRANSPARENT_I2C_EN	(1 << 7)
50 #define HOST_EN			(1 << 0)
51 
52 /* On LPC32xx, those are undefined */
53 #ifndef start_int_set_falling_edge
54 #define start_int_set_falling_edge(irq)
55 #define start_int_set_rising_edge(irq)
56 #define start_int_ack(irq)
57 #define start_int_mask(irq)
58 #define start_int_umask(irq)
59 #endif
60 
61 static struct i2c_client *isp1301_i2c_client;
62 
63 extern int usb_disabled(void);
64 
65 static struct clk *usb_pll_clk;
66 static struct clk *usb_dev_clk;
67 static struct clk *usb_otg_clk;
68 
69 static void isp1301_configure_pnx4008(void)
70 {
71 	/* PNX4008 only supports DAT_SE0 USB mode */
72 	/* PNX4008 R2A requires setting the MAX603 to output 3.6V */
73 	/* Power up externel charge-pump */
74 
75 	i2c_smbus_write_byte_data(isp1301_i2c_client,
76 		ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0 | MC1_SPEED_REG);
77 	i2c_smbus_write_byte_data(isp1301_i2c_client,
78 		ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
79 		~(MC1_DAT_SE0 | MC1_SPEED_REG));
80 	i2c_smbus_write_byte_data(isp1301_i2c_client,
81 		ISP1301_I2C_MODE_CONTROL_2,
82 		MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL);
83 	i2c_smbus_write_byte_data(isp1301_i2c_client,
84 		ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR,
85 		~(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
86 	i2c_smbus_write_byte_data(isp1301_i2c_client,
87 		ISP1301_I2C_OTG_CONTROL_1, OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
88 	i2c_smbus_write_byte_data(isp1301_i2c_client,
89 		ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
90 		~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
91 	i2c_smbus_write_byte_data(isp1301_i2c_client,
92 		ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, 0xFF);
93 	i2c_smbus_write_byte_data(isp1301_i2c_client,
94 		ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
95 		0xFF);
96 	i2c_smbus_write_byte_data(isp1301_i2c_client,
97 		ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR,
98 		0xFF);
99 }
100 
101 static void isp1301_configure_lpc32xx(void)
102 {
103 	/* LPC32XX only supports DAT_SE0 USB mode */
104 	/* This sequence is important */
105 
106 	/* Disable transparent UART mode first */
107 	i2c_smbus_write_byte_data(isp1301_i2c_client,
108 		(ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
109 		MC1_UART_EN);
110 	i2c_smbus_write_byte_data(isp1301_i2c_client,
111 		(ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
112 		~MC1_SPEED_REG);
113 	i2c_smbus_write_byte_data(isp1301_i2c_client,
114 				  ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
115 	i2c_smbus_write_byte_data(isp1301_i2c_client,
116 		  (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
117 		  ~0);
118 	i2c_smbus_write_byte_data(isp1301_i2c_client,
119 		ISP1301_I2C_MODE_CONTROL_2,
120 		(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
121 	i2c_smbus_write_byte_data(isp1301_i2c_client,
122 		(ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
123 	i2c_smbus_write_byte_data(isp1301_i2c_client,
124 		ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
125 	i2c_smbus_write_byte_data(isp1301_i2c_client,
126 		ISP1301_I2C_OTG_CONTROL_1,
127 		(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
128 	i2c_smbus_write_byte_data(isp1301_i2c_client,
129 		(ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
130 		(OTG1_DM_PULLUP | OTG1_DP_PULLUP));
131 	i2c_smbus_write_byte_data(isp1301_i2c_client,
132 		ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
133 	i2c_smbus_write_byte_data(isp1301_i2c_client,
134 		ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
135 		~0);
136 	i2c_smbus_write_byte_data(isp1301_i2c_client,
137 		ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
138 
139 	/* Enable usb_need_clk clock after transceiver is initialized */
140 	__raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
141 
142 	printk(KERN_INFO "ISP1301 Vendor ID  : 0x%04x\n",
143 	      i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
144 	printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
145 	      i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
146 	printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
147 	      i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
148 }
149 
150 static void isp1301_configure(void)
151 {
152 	if (machine_is_pnx4008())
153 		isp1301_configure_pnx4008();
154 	else
155 		isp1301_configure_lpc32xx();
156 }
157 
158 static inline void isp1301_vbus_on(void)
159 {
160 	i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
161 				  OTG1_VBUS_DRV);
162 }
163 
164 static inline void isp1301_vbus_off(void)
165 {
166 	i2c_smbus_write_byte_data(isp1301_i2c_client,
167 		ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
168 		OTG1_VBUS_DRV);
169 }
170 
171 static void nxp_start_hc(void)
172 {
173 	unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
174 	__raw_writel(tmp, USB_OTG_STAT_CONTROL);
175 	isp1301_vbus_on();
176 }
177 
178 static void nxp_stop_hc(void)
179 {
180 	unsigned long tmp;
181 	isp1301_vbus_off();
182 	tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN;
183 	__raw_writel(tmp, USB_OTG_STAT_CONTROL);
184 }
185 
186 static int __devinit ohci_nxp_start(struct usb_hcd *hcd)
187 {
188 	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
189 	int ret;
190 
191 	if ((ret = ohci_init(ohci)) < 0)
192 		return ret;
193 
194 	if ((ret = ohci_run(ohci)) < 0) {
195 		dev_err(hcd->self.controller, "can't start\n");
196 		ohci_stop(hcd);
197 		return ret;
198 	}
199 	return 0;
200 }
201 
202 static const struct hc_driver ohci_nxp_hc_driver = {
203 	.description = hcd_name,
204 	.product_desc =		"nxp OHCI",
205 
206 	/*
207 	 * generic hardware linkage
208 	 */
209 	.irq = ohci_irq,
210 	.flags = HCD_USB11 | HCD_MEMORY,
211 
212 	.hcd_priv_size =	sizeof(struct ohci_hcd),
213 	/*
214 	 * basic lifecycle operations
215 	 */
216 	.start = ohci_nxp_start,
217 	.stop = ohci_stop,
218 	.shutdown = ohci_shutdown,
219 
220 	/*
221 	 * managing i/o requests and associated device resources
222 	 */
223 	.urb_enqueue = ohci_urb_enqueue,
224 	.urb_dequeue = ohci_urb_dequeue,
225 	.endpoint_disable = ohci_endpoint_disable,
226 
227 	/*
228 	 * scheduling support
229 	 */
230 	.get_frame_number = ohci_get_frame,
231 
232 	/*
233 	 * root hub support
234 	 */
235 	.hub_status_data = ohci_hub_status_data,
236 	.hub_control = ohci_hub_control,
237 #ifdef	CONFIG_PM
238 	.bus_suspend = ohci_bus_suspend,
239 	.bus_resume = ohci_bus_resume,
240 #endif
241 	.start_port_reset = ohci_start_port_reset,
242 };
243 
244 static void nxp_set_usb_bits(void)
245 {
246 	if (machine_is_pnx4008()) {
247 		start_int_set_falling_edge(SE_USB_OTG_ATX_INT_N);
248 		start_int_ack(SE_USB_OTG_ATX_INT_N);
249 		start_int_umask(SE_USB_OTG_ATX_INT_N);
250 
251 		start_int_set_rising_edge(SE_USB_OTG_TIMER_INT);
252 		start_int_ack(SE_USB_OTG_TIMER_INT);
253 		start_int_umask(SE_USB_OTG_TIMER_INT);
254 
255 		start_int_set_rising_edge(SE_USB_I2C_INT);
256 		start_int_ack(SE_USB_I2C_INT);
257 		start_int_umask(SE_USB_I2C_INT);
258 
259 		start_int_set_rising_edge(SE_USB_INT);
260 		start_int_ack(SE_USB_INT);
261 		start_int_umask(SE_USB_INT);
262 
263 		start_int_set_rising_edge(SE_USB_NEED_CLK_INT);
264 		start_int_ack(SE_USB_NEED_CLK_INT);
265 		start_int_umask(SE_USB_NEED_CLK_INT);
266 
267 		start_int_set_rising_edge(SE_USB_AHB_NEED_CLK_INT);
268 		start_int_ack(SE_USB_AHB_NEED_CLK_INT);
269 		start_int_umask(SE_USB_AHB_NEED_CLK_INT);
270 	}
271 }
272 
273 static void nxp_unset_usb_bits(void)
274 {
275 	if (machine_is_pnx4008()) {
276 		start_int_mask(SE_USB_OTG_ATX_INT_N);
277 		start_int_mask(SE_USB_OTG_TIMER_INT);
278 		start_int_mask(SE_USB_I2C_INT);
279 		start_int_mask(SE_USB_INT);
280 		start_int_mask(SE_USB_NEED_CLK_INT);
281 		start_int_mask(SE_USB_AHB_NEED_CLK_INT);
282 	}
283 }
284 
285 static int __devinit usb_hcd_nxp_probe(struct platform_device *pdev)
286 {
287 	struct usb_hcd *hcd = 0;
288 	struct ohci_hcd *ohci;
289 	const struct hc_driver *driver = &ohci_nxp_hc_driver;
290 	struct resource *res;
291 	int ret = 0, irq;
292 	struct device_node *isp1301_node;
293 
294 	if (pdev->dev.of_node) {
295 		isp1301_node = of_parse_phandle(pdev->dev.of_node,
296 						"transceiver", 0);
297 	} else {
298 		isp1301_node = NULL;
299 	}
300 
301 	isp1301_i2c_client = isp1301_get_client(isp1301_node);
302 	if (!isp1301_i2c_client) {
303 		ret = -EPROBE_DEFER;
304 		goto out;
305 	}
306 
307 	pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
308 	pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
309 
310 	dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
311 	if (usb_disabled()) {
312 		dev_err(&pdev->dev, "USB is disabled\n");
313 		ret = -ENODEV;
314 		goto out;
315 	}
316 
317 	/* Enable AHB slave USB clock, needed for further USB clock control */
318 	__raw_writel(USB_SLAVE_HCLK_EN | PAD_CONTROL_LAST_DRIVEN, USB_CTRL);
319 
320 	/* Enable USB PLL */
321 	usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
322 	if (IS_ERR(usb_pll_clk)) {
323 		dev_err(&pdev->dev, "failed to acquire USB PLL\n");
324 		ret = PTR_ERR(usb_pll_clk);
325 		goto out1;
326 	}
327 
328 	ret = clk_enable(usb_pll_clk);
329 	if (ret < 0) {
330 		dev_err(&pdev->dev, "failed to start USB PLL\n");
331 		goto out2;
332 	}
333 
334 	ret = clk_set_rate(usb_pll_clk, 48000);
335 	if (ret < 0) {
336 		dev_err(&pdev->dev, "failed to set USB clock rate\n");
337 		goto out3;
338 	}
339 
340 	/* Enable USB device clock */
341 	usb_dev_clk = clk_get(&pdev->dev, "ck_usbd");
342 	if (IS_ERR(usb_dev_clk)) {
343 		dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n");
344 		ret = PTR_ERR(usb_dev_clk);
345 		goto out4;
346 	}
347 
348 	ret = clk_enable(usb_dev_clk);
349 	if (ret < 0) {
350 		dev_err(&pdev->dev, "failed to start USB DEV Clock\n");
351 		goto out5;
352 	}
353 
354 	/* Enable USB otg clocks */
355 	usb_otg_clk = clk_get(&pdev->dev, "ck_usb_otg");
356 	if (IS_ERR(usb_otg_clk)) {
357 		dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n");
358 		ret = PTR_ERR(usb_dev_clk);
359 		goto out6;
360 	}
361 
362 	__raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
363 
364 	ret = clk_enable(usb_otg_clk);
365 	if (ret < 0) {
366 		dev_err(&pdev->dev, "failed to start USB DEV Clock\n");
367 		goto out7;
368 	}
369 
370 	isp1301_configure();
371 
372 	hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
373 	if (!hcd) {
374 		dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
375 		ret = -ENOMEM;
376 		goto out8;
377 	}
378 
379 	/* Set all USB bits in the Start Enable register */
380 	nxp_set_usb_bits();
381 
382 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
383 	if (!res) {
384 		dev_err(&pdev->dev, "Failed to get MEM resource\n");
385 		ret =  -ENOMEM;
386 		goto out8;
387 	}
388 
389 	hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
390 	if (!hcd->regs) {
391 		dev_err(&pdev->dev, "Failed to devm_request_and_ioremap\n");
392 		ret =  -ENOMEM;
393 		goto out8;
394 	}
395 	hcd->rsrc_start = res->start;
396 	hcd->rsrc_len = resource_size(res);
397 
398 	irq = platform_get_irq(pdev, 0);
399 	if (irq < 0) {
400 		ret = -ENXIO;
401 		goto out8;
402 	}
403 
404 	nxp_start_hc();
405 	platform_set_drvdata(pdev, hcd);
406 	ohci = hcd_to_ohci(hcd);
407 	ohci_hcd_init(ohci);
408 
409 	dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
410 	ret = usb_add_hcd(hcd, irq, 0);
411 	if (ret == 0)
412 		return ret;
413 
414 	nxp_stop_hc();
415 out8:
416 	nxp_unset_usb_bits();
417 	usb_put_hcd(hcd);
418 out7:
419 	clk_disable(usb_otg_clk);
420 out6:
421 	clk_put(usb_otg_clk);
422 out5:
423 	clk_disable(usb_dev_clk);
424 out4:
425 	clk_put(usb_dev_clk);
426 out3:
427 	clk_disable(usb_pll_clk);
428 out2:
429 	clk_put(usb_pll_clk);
430 out1:
431 	isp1301_i2c_client = NULL;
432 out:
433 	return ret;
434 }
435 
436 static int usb_hcd_nxp_remove(struct platform_device *pdev)
437 {
438 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
439 
440 	usb_remove_hcd(hcd);
441 	nxp_stop_hc();
442 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
443 	usb_put_hcd(hcd);
444 	nxp_unset_usb_bits();
445 	clk_disable(usb_pll_clk);
446 	clk_put(usb_pll_clk);
447 	clk_disable(usb_dev_clk);
448 	clk_put(usb_dev_clk);
449 	i2c_unregister_device(isp1301_i2c_client);
450 	isp1301_i2c_client = NULL;
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 #ifdef CONFIG_OF
461 static const struct of_device_id usb_hcd_nxp_match[] = {
462 	{ .compatible = "nxp,ohci-nxp" },
463 	{},
464 };
465 MODULE_DEVICE_TABLE(of, usb_hcd_nxp_match);
466 #endif
467 
468 static struct platform_driver usb_hcd_nxp_driver = {
469 	.driver = {
470 		.name = "usb-ohci",
471 		.owner	= THIS_MODULE,
472 		.of_match_table = of_match_ptr(usb_hcd_nxp_match),
473 	},
474 	.probe = usb_hcd_nxp_probe,
475 	.remove = usb_hcd_nxp_remove,
476 };
477 
478