blob: 37d84cf9a408a0600f71dec9936d3f06a0da44d2 [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>
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090018#include <plat/ehci.h>
19#include <plat/usb-phy.h>
20
Jingoo Han88555a62012-03-05 10:40:14 +090021#define EHCI_INSNREG00(base) (base + 0x90)
22#define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
23#define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
24#define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
25#define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
26#define EHCI_INSNREG00_ENABLE_DMA_BURST \
27 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
28 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
29
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090030struct s5p_ehci_hcd {
31 struct device *dev;
32 struct usb_hcd *hcd;
33 struct clk *clk;
34};
35
36static const struct hc_driver s5p_ehci_hc_driver = {
37 .description = hcd_name,
38 .product_desc = "S5P EHCI Host Controller",
39 .hcd_priv_size = sizeof(struct ehci_hcd),
40
41 .irq = ehci_irq,
42 .flags = HCD_MEMORY | HCD_USB2,
43
Alan Stern1a49e2a2012-07-09 15:55:14 -040044 .reset = ehci_setup,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090045 .start = ehci_run,
46 .stop = ehci_stop,
47 .shutdown = ehci_shutdown,
48
49 .get_frame_number = ehci_get_frame,
50
51 .urb_enqueue = ehci_urb_enqueue,
52 .urb_dequeue = ehci_urb_dequeue,
53 .endpoint_disable = ehci_endpoint_disable,
54 .endpoint_reset = ehci_endpoint_reset,
55
56 .hub_status_data = ehci_hub_status_data,
57 .hub_control = ehci_hub_control,
58 .bus_suspend = ehci_bus_suspend,
59 .bus_resume = ehci_bus_resume,
60
61 .relinquish_port = ehci_relinquish_port,
62 .port_handed_over = ehci_port_handed_over,
63
64 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
65};
66
Vivek Gautam2c026e22012-07-16 11:25:37 +053067static u64 ehci_s5p_dma_mask = DMA_BIT_MASK(32);
68
Jingoo Han27362d42011-05-09 15:28:39 +090069static int __devinit s5p_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090070{
71 struct s5p_ehci_platdata *pdata;
72 struct s5p_ehci_hcd *s5p_ehci;
73 struct usb_hcd *hcd;
74 struct ehci_hcd *ehci;
75 struct resource *res;
76 int irq;
77 int err;
78
79 pdata = pdev->dev.platform_data;
80 if (!pdata) {
81 dev_err(&pdev->dev, "No platform data defined\n");
82 return -EINVAL;
83 }
84
Vivek Gautam2c026e22012-07-16 11:25:37 +053085 /*
86 * Right now device-tree probed devices don't get dma_mask set.
87 * Since shared usb code relies on it, set it here for now.
88 * Once we move to full device tree support this will vanish off.
89 */
90 if (!pdev->dev.dma_mask)
91 pdev->dev.dma_mask = &ehci_s5p_dma_mask;
92 if (!pdev->dev.coherent_dma_mask)
93 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
94
Jingoo Han9cb07562012-06-28 16:29:46 +090095 s5p_ehci = devm_kzalloc(&pdev->dev, sizeof(struct s5p_ehci_hcd),
96 GFP_KERNEL);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090097 if (!s5p_ehci)
98 return -ENOMEM;
99
100 s5p_ehci->dev = &pdev->dev;
101
102 hcd = usb_create_hcd(&s5p_ehci_hc_driver, &pdev->dev,
103 dev_name(&pdev->dev));
104 if (!hcd) {
105 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han9cb07562012-06-28 16:29:46 +0900106 return -ENOMEM;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900107 }
108
Yulgon Kime5d3d442011-08-18 14:02:45 +0900109 s5p_ehci->hcd = hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900110 s5p_ehci->clk = clk_get(&pdev->dev, "usbhost");
111
112 if (IS_ERR(s5p_ehci->clk)) {
113 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
114 err = PTR_ERR(s5p_ehci->clk);
115 goto fail_clk;
116 }
117
118 err = clk_enable(s5p_ehci->clk);
119 if (err)
120 goto fail_clken;
121
122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123 if (!res) {
124 dev_err(&pdev->dev, "Failed to get I/O memory\n");
125 err = -ENXIO;
126 goto fail_io;
127 }
128
129 hcd->rsrc_start = res->start;
130 hcd->rsrc_len = resource_size(res);
Jingoo Han9cb07562012-06-28 16:29:46 +0900131 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900132 if (!hcd->regs) {
133 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
134 err = -ENOMEM;
135 goto fail_io;
136 }
137
138 irq = platform_get_irq(pdev, 0);
139 if (!irq) {
140 dev_err(&pdev->dev, "Failed to get IRQ\n");
141 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900142 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900143 }
144
145 if (pdata->phy_init)
146 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
147
148 ehci = hcd_to_ehci(hcd);
149 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900150
Jingoo Han88555a62012-03-05 10:40:14 +0900151 /* DMA burst Enable */
152 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
153
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800154 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900155 if (err) {
156 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Jingoo Han9cb07562012-06-28 16:29:46 +0900157 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900158 }
159
160 platform_set_drvdata(pdev, s5p_ehci);
161
162 return 0;
163
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900164fail_io:
165 clk_disable(s5p_ehci->clk);
166fail_clken:
167 clk_put(s5p_ehci->clk);
168fail_clk:
169 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900170 return err;
171}
172
Jingoo Han27362d42011-05-09 15:28:39 +0900173static int __devexit s5p_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900174{
175 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
176 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
177 struct usb_hcd *hcd = s5p_ehci->hcd;
178
179 usb_remove_hcd(hcd);
180
181 if (pdata && pdata->phy_exit)
182 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
183
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900184 clk_disable(s5p_ehci->clk);
185 clk_put(s5p_ehci->clk);
186
187 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900188
189 return 0;
190}
191
192static void s5p_ehci_shutdown(struct platform_device *pdev)
193{
194 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
195 struct usb_hcd *hcd = s5p_ehci->hcd;
196
197 if (hcd->driver->shutdown)
198 hcd->driver->shutdown(hcd);
199}
200
Jingoo Han1acb30e2011-05-20 20:48:33 +0900201#ifdef CONFIG_PM
202static int s5p_ehci_suspend(struct device *dev)
203{
204 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
205 struct usb_hcd *hcd = s5p_ehci->hcd;
Alan Sternc5cf9212012-06-28 11:19:02 -0400206 bool do_wakeup = device_may_wakeup(dev);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900207 struct platform_device *pdev = to_platform_device(dev);
208 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
Alan Sternc5cf9212012-06-28 11:19:02 -0400209 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900210
Alan Sternc5cf9212012-06-28 11:19:02 -0400211 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900212
213 if (pdata && pdata->phy_exit)
214 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
215
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900216 clk_disable(s5p_ehci->clk);
217
Jingoo Han1acb30e2011-05-20 20:48:33 +0900218 return rc;
219}
220
221static int s5p_ehci_resume(struct device *dev)
222{
223 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
224 struct usb_hcd *hcd = s5p_ehci->hcd;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900225 struct platform_device *pdev = to_platform_device(dev);
226 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
227
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900228 clk_enable(s5p_ehci->clk);
229
Jingoo Han1acb30e2011-05-20 20:48:33 +0900230 if (pdata && pdata->phy_init)
231 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
232
Jingoo Han88555a62012-03-05 10:40:14 +0900233 /* DMA burst Enable */
234 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
235
Alan Sternc5cf9212012-06-28 11:19:02 -0400236 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900237 return 0;
238}
239#else
240#define s5p_ehci_suspend NULL
241#define s5p_ehci_resume NULL
242#endif
243
244static const struct dev_pm_ops s5p_ehci_pm_ops = {
245 .suspend = s5p_ehci_suspend,
246 .resume = s5p_ehci_resume,
247};
248
Vivek Gautam2c026e22012-07-16 11:25:37 +0530249#ifdef CONFIG_OF
250static const struct of_device_id exynos_ehci_match[] = {
251 { .compatible = "samsung,exynos-ehci" },
252 {},
253};
254MODULE_DEVICE_TABLE(of, exynos_ehci_match);
255#endif
256
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900257static struct platform_driver s5p_ehci_driver = {
258 .probe = s5p_ehci_probe,
Jingoo Han27362d42011-05-09 15:28:39 +0900259 .remove = __devexit_p(s5p_ehci_remove),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900260 .shutdown = s5p_ehci_shutdown,
261 .driver = {
262 .name = "s5p-ehci",
263 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900264 .pm = &s5p_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530265 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900266 }
267};
268
269MODULE_ALIAS("platform:s5p-ehci");