blob: 738490e6d4298f9046bc4296d231b2f9bfbfd45d [file] [log] [blame]
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +09001/*
2 * SAMSUNG S5P USB HOST EHCI Controller
3 *
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Jingoo Han <jg1.han@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/clk.h>
Vivek Gautam2c026e22012-07-16 11:25:37 +053016#include <linux/of.h>
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090017#include <linux/platform_device.h>
Vivek Gautamfd81d592012-07-17 10:10:50 +053018#include <linux/of_gpio.h>
Arnd Bergmann436d42c2012-08-24 15:22:12 +020019#include <linux/platform_data/usb-ehci-s5p.h>
Vivek Gautamd233c192013-01-22 18:30:42 +053020#include <linux/usb/phy.h>
Vivek Gautamb506eeb2013-01-22 18:30:40 +053021#include <linux/usb/samsung_usb_phy.h>
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090022#include <plat/usb-phy.h>
23
Jingoo Han88555a62012-03-05 10:40:14 +090024#define EHCI_INSNREG00(base) (base + 0x90)
25#define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
26#define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
27#define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
28#define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
29#define EHCI_INSNREG00_ENABLE_DMA_BURST \
30 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
31 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
32
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090033struct s5p_ehci_hcd {
34 struct device *dev;
35 struct usb_hcd *hcd;
36 struct clk *clk;
Vivek Gautamd233c192013-01-22 18:30:42 +053037 struct usb_phy *phy;
38 struct usb_otg *otg;
39 struct s5p_ehci_platdata *pdata;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090040};
41
42static const struct hc_driver s5p_ehci_hc_driver = {
43 .description = hcd_name,
44 .product_desc = "S5P EHCI Host Controller",
45 .hcd_priv_size = sizeof(struct ehci_hcd),
46
47 .irq = ehci_irq,
48 .flags = HCD_MEMORY | HCD_USB2,
49
Alan Stern1a49e2a2012-07-09 15:55:14 -040050 .reset = ehci_setup,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090051 .start = ehci_run,
52 .stop = ehci_stop,
53 .shutdown = ehci_shutdown,
54
55 .get_frame_number = ehci_get_frame,
56
57 .urb_enqueue = ehci_urb_enqueue,
58 .urb_dequeue = ehci_urb_dequeue,
59 .endpoint_disable = ehci_endpoint_disable,
60 .endpoint_reset = ehci_endpoint_reset,
61
62 .hub_status_data = ehci_hub_status_data,
63 .hub_control = ehci_hub_control,
64 .bus_suspend = ehci_bus_suspend,
65 .bus_resume = ehci_bus_resume,
66
67 .relinquish_port = ehci_relinquish_port,
68 .port_handed_over = ehci_port_handed_over,
69
70 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
71};
72
Vivek Gautamd233c192013-01-22 18:30:42 +053073static void s5p_ehci_phy_enable(struct s5p_ehci_hcd *s5p_ehci)
74{
75 struct platform_device *pdev = to_platform_device(s5p_ehci->dev);
76
77 if (s5p_ehci->phy)
78 usb_phy_init(s5p_ehci->phy);
79 else if (s5p_ehci->pdata->phy_init)
80 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
81}
82
83static void s5p_ehci_phy_disable(struct s5p_ehci_hcd *s5p_ehci)
84{
85 struct platform_device *pdev = to_platform_device(s5p_ehci->dev);
86
87 if (s5p_ehci->phy)
88 usb_phy_shutdown(s5p_ehci->phy);
89 else if (s5p_ehci->pdata->phy_exit)
90 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
91}
92
Vivek Gautamfd81d592012-07-17 10:10:50 +053093static void s5p_setup_vbus_gpio(struct platform_device *pdev)
94{
Doug Anderson3f3b55b2013-03-14 20:15:37 -070095 struct device *dev = &pdev->dev;
Vivek Gautamfd81d592012-07-17 10:10:50 +053096 int err;
97 int gpio;
98
Doug Anderson3f3b55b2013-03-14 20:15:37 -070099 if (!dev->of_node)
Vivek Gautamfd81d592012-07-17 10:10:50 +0530100 return;
101
Doug Anderson3f3b55b2013-03-14 20:15:37 -0700102 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
Vivek Gautamfd81d592012-07-17 10:10:50 +0530103 if (!gpio_is_valid(gpio))
104 return;
105
Doug Anderson3f3b55b2013-03-14 20:15:37 -0700106 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
107 "ehci_vbus_gpio");
Vivek Gautamfd81d592012-07-17 10:10:50 +0530108 if (err)
Doug Anderson3f3b55b2013-03-14 20:15:37 -0700109 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
Vivek Gautamfd81d592012-07-17 10:10:50 +0530110}
111
Vivek Gautam2c026e22012-07-16 11:25:37 +0530112static u64 ehci_s5p_dma_mask = DMA_BIT_MASK(32);
113
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500114static int s5p_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900115{
Vivek Gautamd233c192013-01-22 18:30:42 +0530116 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900117 struct s5p_ehci_hcd *s5p_ehci;
118 struct usb_hcd *hcd;
119 struct ehci_hcd *ehci;
120 struct resource *res;
Vivek Gautamd233c192013-01-22 18:30:42 +0530121 struct usb_phy *phy;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900122 int irq;
123 int err;
124
Vivek Gautam2c026e22012-07-16 11:25:37 +0530125 /*
126 * Right now device-tree probed devices don't get dma_mask set.
127 * Since shared usb code relies on it, set it here for now.
128 * Once we move to full device tree support this will vanish off.
129 */
130 if (!pdev->dev.dma_mask)
131 pdev->dev.dma_mask = &ehci_s5p_dma_mask;
132 if (!pdev->dev.coherent_dma_mask)
133 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
134
Vivek Gautamfd81d592012-07-17 10:10:50 +0530135 s5p_setup_vbus_gpio(pdev);
136
Jingoo Han9cb07562012-06-28 16:29:46 +0900137 s5p_ehci = devm_kzalloc(&pdev->dev, sizeof(struct s5p_ehci_hcd),
138 GFP_KERNEL);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900139 if (!s5p_ehci)
140 return -ENOMEM;
141
Vivek Gautamd233c192013-01-22 18:30:42 +0530142 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
143 if (IS_ERR_OR_NULL(phy)) {
144 /* Fallback to pdata */
145 if (!pdata) {
146 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
147 return -EPROBE_DEFER;
148 } else {
149 s5p_ehci->pdata = pdata;
150 }
151 } else {
152 s5p_ehci->phy = phy;
153 s5p_ehci->otg = phy->otg;
154 }
155
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900156 s5p_ehci->dev = &pdev->dev;
157
158 hcd = usb_create_hcd(&s5p_ehci_hc_driver, &pdev->dev,
159 dev_name(&pdev->dev));
160 if (!hcd) {
161 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han9cb07562012-06-28 16:29:46 +0900162 return -ENOMEM;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900163 }
164
Yulgon Kime5d3d442011-08-18 14:02:45 +0900165 s5p_ehci->hcd = hcd;
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200166 s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900167
168 if (IS_ERR(s5p_ehci->clk)) {
169 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
170 err = PTR_ERR(s5p_ehci->clk);
171 goto fail_clk;
172 }
173
Thomas Abrahame1deb562012-10-03 08:40:42 +0900174 err = clk_prepare_enable(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900175 if (err)
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200176 goto fail_clk;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900177
178 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
179 if (!res) {
180 dev_err(&pdev->dev, "Failed to get I/O memory\n");
181 err = -ENXIO;
182 goto fail_io;
183 }
184
185 hcd->rsrc_start = res->start;
186 hcd->rsrc_len = resource_size(res);
Jingoo Han9cb07562012-06-28 16:29:46 +0900187 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900188 if (!hcd->regs) {
189 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
190 err = -ENOMEM;
191 goto fail_io;
192 }
193
194 irq = platform_get_irq(pdev, 0);
195 if (!irq) {
196 dev_err(&pdev->dev, "Failed to get IRQ\n");
197 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900198 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900199 }
200
Vivek Gautamd233c192013-01-22 18:30:42 +0530201 if (s5p_ehci->otg)
202 s5p_ehci->otg->set_host(s5p_ehci->otg, &s5p_ehci->hcd->self);
203
204 s5p_ehci_phy_enable(s5p_ehci);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900205
206 ehci = hcd_to_ehci(hcd);
207 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900208
Jingoo Han88555a62012-03-05 10:40:14 +0900209 /* DMA burst Enable */
210 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
211
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800212 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900213 if (err) {
214 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamd233c192013-01-22 18:30:42 +0530215 goto fail_add_hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900216 }
217
218 platform_set_drvdata(pdev, s5p_ehci);
219
220 return 0;
221
Vivek Gautamd233c192013-01-22 18:30:42 +0530222fail_add_hcd:
223 s5p_ehci_phy_disable(s5p_ehci);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900224fail_io:
Thomas Abrahame1deb562012-10-03 08:40:42 +0900225 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900226fail_clk:
227 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900228 return err;
229}
230
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500231static int s5p_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900232{
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900233 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
234 struct usb_hcd *hcd = s5p_ehci->hcd;
235
236 usb_remove_hcd(hcd);
237
Vivek Gautamd233c192013-01-22 18:30:42 +0530238 if (s5p_ehci->otg)
239 s5p_ehci->otg->set_host(s5p_ehci->otg, &s5p_ehci->hcd->self);
240
241 s5p_ehci_phy_disable(s5p_ehci);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900242
Thomas Abrahame1deb562012-10-03 08:40:42 +0900243 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900244
245 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900246
247 return 0;
248}
249
250static void s5p_ehci_shutdown(struct platform_device *pdev)
251{
252 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
253 struct usb_hcd *hcd = s5p_ehci->hcd;
254
255 if (hcd->driver->shutdown)
256 hcd->driver->shutdown(hcd);
257}
258
Jingoo Han1acb30e2011-05-20 20:48:33 +0900259#ifdef CONFIG_PM
260static int s5p_ehci_suspend(struct device *dev)
261{
262 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
263 struct usb_hcd *hcd = s5p_ehci->hcd;
Alan Sternc5cf9212012-06-28 11:19:02 -0400264 bool do_wakeup = device_may_wakeup(dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400265 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900266
Alan Sternc5cf9212012-06-28 11:19:02 -0400267 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900268
Vivek Gautamd233c192013-01-22 18:30:42 +0530269 if (s5p_ehci->otg)
270 s5p_ehci->otg->set_host(s5p_ehci->otg, &s5p_ehci->hcd->self);
271
272 s5p_ehci_phy_disable(s5p_ehci);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900273
Thomas Abrahame1deb562012-10-03 08:40:42 +0900274 clk_disable_unprepare(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900275
Jingoo Han1acb30e2011-05-20 20:48:33 +0900276 return rc;
277}
278
279static int s5p_ehci_resume(struct device *dev)
280{
281 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
282 struct usb_hcd *hcd = s5p_ehci->hcd;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900283
Thomas Abrahame1deb562012-10-03 08:40:42 +0900284 clk_prepare_enable(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900285
Vivek Gautamd233c192013-01-22 18:30:42 +0530286 if (s5p_ehci->otg)
287 s5p_ehci->otg->set_host(s5p_ehci->otg, &s5p_ehci->hcd->self);
288
289 s5p_ehci_phy_enable(s5p_ehci);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900290
Jingoo Han88555a62012-03-05 10:40:14 +0900291 /* DMA burst Enable */
292 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
293
Alan Sternc5cf9212012-06-28 11:19:02 -0400294 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900295 return 0;
296}
297#else
298#define s5p_ehci_suspend NULL
299#define s5p_ehci_resume NULL
300#endif
301
302static const struct dev_pm_ops s5p_ehci_pm_ops = {
303 .suspend = s5p_ehci_suspend,
304 .resume = s5p_ehci_resume,
305};
306
Vivek Gautam2c026e22012-07-16 11:25:37 +0530307#ifdef CONFIG_OF
308static const struct of_device_id exynos_ehci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530309 { .compatible = "samsung,exynos4210-ehci" },
Vivek Gautam2c026e22012-07-16 11:25:37 +0530310 {},
311};
312MODULE_DEVICE_TABLE(of, exynos_ehci_match);
313#endif
314
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900315static struct platform_driver s5p_ehci_driver = {
316 .probe = s5p_ehci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500317 .remove = s5p_ehci_remove,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900318 .shutdown = s5p_ehci_shutdown,
319 .driver = {
320 .name = "s5p-ehci",
321 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900322 .pm = &s5p_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530323 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900324 }
325};
326
327MODULE_ALIAS("platform:s5p-ehci");