blob: 46ca5efac82bbe21c929da059985bdad1c978503 [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 Gautamb506eeb2013-01-22 18:30:40 +053020#include <linux/usb/samsung_usb_phy.h>
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090021#include <plat/usb-phy.h>
22
Jingoo Han88555a62012-03-05 10:40:14 +090023#define EHCI_INSNREG00(base) (base + 0x90)
24#define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
25#define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
26#define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
27#define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
28#define EHCI_INSNREG00_ENABLE_DMA_BURST \
29 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
30 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
31
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090032struct s5p_ehci_hcd {
33 struct device *dev;
34 struct usb_hcd *hcd;
35 struct clk *clk;
36};
37
38static const struct hc_driver s5p_ehci_hc_driver = {
39 .description = hcd_name,
40 .product_desc = "S5P EHCI Host Controller",
41 .hcd_priv_size = sizeof(struct ehci_hcd),
42
43 .irq = ehci_irq,
44 .flags = HCD_MEMORY | HCD_USB2,
45
Alan Stern1a49e2a2012-07-09 15:55:14 -040046 .reset = ehci_setup,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090047 .start = ehci_run,
48 .stop = ehci_stop,
49 .shutdown = ehci_shutdown,
50
51 .get_frame_number = ehci_get_frame,
52
53 .urb_enqueue = ehci_urb_enqueue,
54 .urb_dequeue = ehci_urb_dequeue,
55 .endpoint_disable = ehci_endpoint_disable,
56 .endpoint_reset = ehci_endpoint_reset,
57
58 .hub_status_data = ehci_hub_status_data,
59 .hub_control = ehci_hub_control,
60 .bus_suspend = ehci_bus_suspend,
61 .bus_resume = ehci_bus_resume,
62
63 .relinquish_port = ehci_relinquish_port,
64 .port_handed_over = ehci_port_handed_over,
65
66 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
67};
68
Vivek Gautamfd81d592012-07-17 10:10:50 +053069static void s5p_setup_vbus_gpio(struct platform_device *pdev)
70{
71 int err;
72 int gpio;
73
74 if (!pdev->dev.of_node)
75 return;
76
77 gpio = of_get_named_gpio(pdev->dev.of_node,
78 "samsung,vbus-gpio", 0);
79 if (!gpio_is_valid(gpio))
80 return;
81
82 err = gpio_request_one(gpio, GPIOF_OUT_INIT_HIGH, "ehci_vbus_gpio");
83 if (err)
84 dev_err(&pdev->dev, "can't request ehci vbus gpio %d", gpio);
85}
86
Vivek Gautam2c026e22012-07-16 11:25:37 +053087static u64 ehci_s5p_dma_mask = DMA_BIT_MASK(32);
88
Bill Pemberton41ac7b32012-11-19 13:21:48 -050089static int s5p_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090090{
91 struct s5p_ehci_platdata *pdata;
92 struct s5p_ehci_hcd *s5p_ehci;
93 struct usb_hcd *hcd;
94 struct ehci_hcd *ehci;
95 struct resource *res;
96 int irq;
97 int err;
98
99 pdata = pdev->dev.platform_data;
100 if (!pdata) {
101 dev_err(&pdev->dev, "No platform data defined\n");
102 return -EINVAL;
103 }
104
Vivek Gautam2c026e22012-07-16 11:25:37 +0530105 /*
106 * Right now device-tree probed devices don't get dma_mask set.
107 * Since shared usb code relies on it, set it here for now.
108 * Once we move to full device tree support this will vanish off.
109 */
110 if (!pdev->dev.dma_mask)
111 pdev->dev.dma_mask = &ehci_s5p_dma_mask;
112 if (!pdev->dev.coherent_dma_mask)
113 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
114
Vivek Gautamfd81d592012-07-17 10:10:50 +0530115 s5p_setup_vbus_gpio(pdev);
116
Jingoo Han9cb07562012-06-28 16:29:46 +0900117 s5p_ehci = devm_kzalloc(&pdev->dev, sizeof(struct s5p_ehci_hcd),
118 GFP_KERNEL);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900119 if (!s5p_ehci)
120 return -ENOMEM;
121
122 s5p_ehci->dev = &pdev->dev;
123
124 hcd = usb_create_hcd(&s5p_ehci_hc_driver, &pdev->dev,
125 dev_name(&pdev->dev));
126 if (!hcd) {
127 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han9cb07562012-06-28 16:29:46 +0900128 return -ENOMEM;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900129 }
130
Yulgon Kime5d3d442011-08-18 14:02:45 +0900131 s5p_ehci->hcd = hcd;
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200132 s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900133
134 if (IS_ERR(s5p_ehci->clk)) {
135 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
136 err = PTR_ERR(s5p_ehci->clk);
137 goto fail_clk;
138 }
139
Thomas Abrahame1deb562012-10-03 08:40:42 +0900140 err = clk_prepare_enable(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900141 if (err)
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200142 goto fail_clk;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900143
144 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145 if (!res) {
146 dev_err(&pdev->dev, "Failed to get I/O memory\n");
147 err = -ENXIO;
148 goto fail_io;
149 }
150
151 hcd->rsrc_start = res->start;
152 hcd->rsrc_len = resource_size(res);
Jingoo Han9cb07562012-06-28 16:29:46 +0900153 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900154 if (!hcd->regs) {
155 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
156 err = -ENOMEM;
157 goto fail_io;
158 }
159
160 irq = platform_get_irq(pdev, 0);
161 if (!irq) {
162 dev_err(&pdev->dev, "Failed to get IRQ\n");
163 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900164 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900165 }
166
167 if (pdata->phy_init)
Vivek Gautamb506eeb2013-01-22 18:30:40 +0530168 pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900169
170 ehci = hcd_to_ehci(hcd);
171 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900172
Jingoo Han88555a62012-03-05 10:40:14 +0900173 /* DMA burst Enable */
174 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
175
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800176 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900177 if (err) {
178 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Jingoo Han9cb07562012-06-28 16:29:46 +0900179 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900180 }
181
182 platform_set_drvdata(pdev, s5p_ehci);
183
184 return 0;
185
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900186fail_io:
Thomas Abrahame1deb562012-10-03 08:40:42 +0900187 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900188fail_clk:
189 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900190 return err;
191}
192
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500193static int s5p_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900194{
195 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
196 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
197 struct usb_hcd *hcd = s5p_ehci->hcd;
198
199 usb_remove_hcd(hcd);
200
201 if (pdata && pdata->phy_exit)
Vivek Gautamb506eeb2013-01-22 18:30:40 +0530202 pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900203
Thomas Abrahame1deb562012-10-03 08:40:42 +0900204 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900205
206 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900207
208 return 0;
209}
210
211static void s5p_ehci_shutdown(struct platform_device *pdev)
212{
213 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
214 struct usb_hcd *hcd = s5p_ehci->hcd;
215
216 if (hcd->driver->shutdown)
217 hcd->driver->shutdown(hcd);
218}
219
Jingoo Han1acb30e2011-05-20 20:48:33 +0900220#ifdef CONFIG_PM
221static int s5p_ehci_suspend(struct device *dev)
222{
223 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
224 struct usb_hcd *hcd = s5p_ehci->hcd;
Alan Sternc5cf9212012-06-28 11:19:02 -0400225 bool do_wakeup = device_may_wakeup(dev);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900226 struct platform_device *pdev = to_platform_device(dev);
227 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
Alan Sternc5cf9212012-06-28 11:19:02 -0400228 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900229
Alan Sternc5cf9212012-06-28 11:19:02 -0400230 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900231
232 if (pdata && pdata->phy_exit)
Vivek Gautamb506eeb2013-01-22 18:30:40 +0530233 pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900234
Thomas Abrahame1deb562012-10-03 08:40:42 +0900235 clk_disable_unprepare(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900236
Jingoo Han1acb30e2011-05-20 20:48:33 +0900237 return rc;
238}
239
240static int s5p_ehci_resume(struct device *dev)
241{
242 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
243 struct usb_hcd *hcd = s5p_ehci->hcd;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900244 struct platform_device *pdev = to_platform_device(dev);
245 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
246
Thomas Abrahame1deb562012-10-03 08:40:42 +0900247 clk_prepare_enable(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900248
Jingoo Han1acb30e2011-05-20 20:48:33 +0900249 if (pdata && pdata->phy_init)
Vivek Gautamb506eeb2013-01-22 18:30:40 +0530250 pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900251
Jingoo Han88555a62012-03-05 10:40:14 +0900252 /* DMA burst Enable */
253 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
254
Alan Sternc5cf9212012-06-28 11:19:02 -0400255 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900256 return 0;
257}
258#else
259#define s5p_ehci_suspend NULL
260#define s5p_ehci_resume NULL
261#endif
262
263static const struct dev_pm_ops s5p_ehci_pm_ops = {
264 .suspend = s5p_ehci_suspend,
265 .resume = s5p_ehci_resume,
266};
267
Vivek Gautam2c026e22012-07-16 11:25:37 +0530268#ifdef CONFIG_OF
269static const struct of_device_id exynos_ehci_match[] = {
270 { .compatible = "samsung,exynos-ehci" },
271 {},
272};
273MODULE_DEVICE_TABLE(of, exynos_ehci_match);
274#endif
275
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900276static struct platform_driver s5p_ehci_driver = {
277 .probe = s5p_ehci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500278 .remove = s5p_ehci_remove,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900279 .shutdown = s5p_ehci_shutdown,
280 .driver = {
281 .name = "s5p-ehci",
282 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900283 .pm = &s5p_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530284 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900285 }
286};
287
288MODULE_ALIAS("platform:s5p-ehci");