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