blob: 3ba140ceaf52cb6ac35ff0c8709f14d4795fdeb4 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Nicolas Ferre501c9c02009-07-27 14:47:40 -07002/*
3 * Driver for EHCI UHP on Atmel chips
4 *
5 * Copyright (C) 2009 Atmel Corporation,
6 * Nicolas Ferre <nicolas.ferre@atmel.com>
7 *
8 * Based on various ehci-*.c drivers
Nicolas Ferre501c9c02009-07-27 14:47:40 -07009 */
10
11#include <linux/clk.h>
Manjunath Goudar97736962013-04-02 18:24:02 +020012#include <linux/dma-mapping.h>
13#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
Nicolas Ferre94753752012-03-27 18:23:31 +020016#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARD9d843002011-11-22 12:11:13 +080017#include <linux/of_platform.h>
Manjunath Goudar97736962013-04-02 18:24:02 +020018#include <linux/platform_device.h>
19#include <linux/usb.h>
20#include <linux/usb/hcd.h>
21
22#include "ehci.h"
23
24#define DRIVER_DESC "EHCI Atmel driver"
25
26static const char hcd_name[] = "ehci-atmel";
Nicolas Ferre501c9c02009-07-27 14:47:40 -070027
28/* interface and function clocks */
Sylvain Rochet7baddac2015-01-20 14:39:01 +010029#define hcd_to_atmel_ehci_priv(h) \
30 ((struct atmel_ehci_priv *)hcd_to_ehci(h)->priv)
31
32struct atmel_ehci_priv {
33 struct clk *iclk;
Sylvain Rochet7baddac2015-01-20 14:39:01 +010034 struct clk *uclk;
35 bool clocked;
36};
37
38static struct hc_driver __read_mostly ehci_atmel_hc_driver;
39
40static const struct ehci_driver_overrides ehci_atmel_drv_overrides __initconst = {
41 .extra_priv_size = sizeof(struct atmel_ehci_priv),
42};
Nicolas Ferre501c9c02009-07-27 14:47:40 -070043
44/*-------------------------------------------------------------------------*/
45
Sylvain Rochet7baddac2015-01-20 14:39:01 +010046static void atmel_start_clock(struct atmel_ehci_priv *atmel_ehci)
Nicolas Ferre501c9c02009-07-27 14:47:40 -070047{
Sylvain Rochet7baddac2015-01-20 14:39:01 +010048 if (atmel_ehci->clocked)
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +010049 return;
Boris Brezillone32643a2015-03-17 17:15:46 +010050
51 clk_prepare_enable(atmel_ehci->uclk);
Sylvain Rochet7baddac2015-01-20 14:39:01 +010052 clk_prepare_enable(atmel_ehci->iclk);
Sylvain Rochet7baddac2015-01-20 14:39:01 +010053 atmel_ehci->clocked = true;
Nicolas Ferre501c9c02009-07-27 14:47:40 -070054}
55
Sylvain Rochet7baddac2015-01-20 14:39:01 +010056static void atmel_stop_clock(struct atmel_ehci_priv *atmel_ehci)
Nicolas Ferre501c9c02009-07-27 14:47:40 -070057{
Sylvain Rochet7baddac2015-01-20 14:39:01 +010058 if (!atmel_ehci->clocked)
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +010059 return;
Boris Brezillone32643a2015-03-17 17:15:46 +010060
Sylvain Rochet7baddac2015-01-20 14:39:01 +010061 clk_disable_unprepare(atmel_ehci->iclk);
Boris Brezillone32643a2015-03-17 17:15:46 +010062 clk_disable_unprepare(atmel_ehci->uclk);
Sylvain Rochet7baddac2015-01-20 14:39:01 +010063 atmel_ehci->clocked = false;
Nicolas Ferre501c9c02009-07-27 14:47:40 -070064}
65
66static void atmel_start_ehci(struct platform_device *pdev)
67{
Sylvain Rochet7baddac2015-01-20 14:39:01 +010068 struct usb_hcd *hcd = platform_get_drvdata(pdev);
69 struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
70
Nicolas Ferre501c9c02009-07-27 14:47:40 -070071 dev_dbg(&pdev->dev, "start\n");
Sylvain Rochet7baddac2015-01-20 14:39:01 +010072 atmel_start_clock(atmel_ehci);
Nicolas Ferre501c9c02009-07-27 14:47:40 -070073}
74
75static void atmel_stop_ehci(struct platform_device *pdev)
76{
Sylvain Rochet7baddac2015-01-20 14:39:01 +010077 struct usb_hcd *hcd = platform_get_drvdata(pdev);
78 struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
79
Nicolas Ferre501c9c02009-07-27 14:47:40 -070080 dev_dbg(&pdev->dev, "stop\n");
Sylvain Rochet7baddac2015-01-20 14:39:01 +010081 atmel_stop_clock(atmel_ehci);
Nicolas Ferre501c9c02009-07-27 14:47:40 -070082}
83
84/*-------------------------------------------------------------------------*/
85
Bill Pemberton41ac7b32012-11-19 13:21:48 -050086static int ehci_atmel_drv_probe(struct platform_device *pdev)
Nicolas Ferre501c9c02009-07-27 14:47:40 -070087{
88 struct usb_hcd *hcd;
89 const struct hc_driver *driver = &ehci_atmel_hc_driver;
90 struct resource *res;
Manjunath Goudar97736962013-04-02 18:24:02 +020091 struct ehci_hcd *ehci;
Sylvain Rochet7baddac2015-01-20 14:39:01 +010092 struct atmel_ehci_priv *atmel_ehci;
Nicolas Ferre501c9c02009-07-27 14:47:40 -070093 int irq;
94 int retval;
95
96 if (usb_disabled())
97 return -ENODEV;
98
99 pr_debug("Initializing Atmel-SoC USB Host Controller\n");
100
101 irq = platform_get_irq(pdev, 0);
102 if (irq <= 0) {
103 dev_err(&pdev->dev,
104 "Found HC with no IRQ. Check %s setup!\n",
105 dev_name(&pdev->dev));
106 retval = -ENODEV;
107 goto fail_create_hcd;
108 }
109
Jean-Christophe PLAGNIOL-VILLARD9d843002011-11-22 12:11:13 +0800110 /* Right now device-tree probed devices don't get dma_mask set.
111 * Since shared usb code relies on it, set it here for now.
112 * Once we have dma capability bindings this can go away.
113 */
Russell Kinge1fd7342013-06-27 12:36:37 +0100114 retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100115 if (retval)
116 goto fail_create_hcd;
Jean-Christophe PLAGNIOL-VILLARD9d843002011-11-22 12:11:13 +0800117
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700118 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
119 if (!hcd) {
120 retval = -ENOMEM;
121 goto fail_create_hcd;
122 }
Sylvain Rochet7baddac2015-01-20 14:39:01 +0100123 atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700124
125 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100126 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
127 if (IS_ERR(hcd->regs)) {
128 retval = PTR_ERR(hcd->regs);
Julia Lawall97983432012-07-29 21:46:05 +0200129 goto fail_request_resource;
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700130 }
131
Varka Bhadram0fb5f702014-11-04 07:51:08 +0530132 hcd->rsrc_start = res->start;
133 hcd->rsrc_len = resource_size(res);
134
Sylvain Rochet7baddac2015-01-20 14:39:01 +0100135 atmel_ehci->iclk = devm_clk_get(&pdev->dev, "ehci_clk");
136 if (IS_ERR(atmel_ehci->iclk)) {
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700137 dev_err(&pdev->dev, "Error getting interface clock\n");
138 retval = -ENOENT;
Julia Lawall97983432012-07-29 21:46:05 +0200139 goto fail_request_resource;
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700140 }
Boris Brezillone32643a2015-03-17 17:15:46 +0100141
142 atmel_ehci->uclk = devm_clk_get(&pdev->dev, "usb_clk");
143 if (IS_ERR(atmel_ehci->uclk)) {
144 dev_err(&pdev->dev, "failed to get uclk\n");
145 retval = PTR_ERR(atmel_ehci->uclk);
Julia Lawall97983432012-07-29 21:46:05 +0200146 goto fail_request_resource;
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700147 }
148
Manjunath Goudar97736962013-04-02 18:24:02 +0200149 ehci = hcd_to_ehci(hcd);
150 /* registers start at offset 0x0 */
151 ehci->caps = hcd->regs;
152
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700153 atmel_start_ehci(pdev);
154
155 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
156 if (retval)
157 goto fail_add_hcd;
Peter Chen3c9740a2013-11-05 10:46:02 +0800158 device_wakeup_enable(hcd->self.controller);
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700159
160 return retval;
161
162fail_add_hcd:
163 atmel_stop_ehci(pdev);
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700164fail_request_resource:
165 usb_put_hcd(hcd);
166fail_create_hcd:
167 dev_err(&pdev->dev, "init %s fail, %d\n",
168 dev_name(&pdev->dev), retval);
169
170 return retval;
171}
172
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500173static int ehci_atmel_drv_remove(struct platform_device *pdev)
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700174{
175 struct usb_hcd *hcd = platform_get_drvdata(pdev);
176
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700177 usb_remove_hcd(hcd);
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700178 usb_put_hcd(hcd);
179
180 atmel_stop_ehci(pdev);
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700181
182 return 0;
183}
184
Arnd Bergmann9fc7c852016-03-02 16:24:06 +0100185static int __maybe_unused ehci_atmel_drv_suspend(struct device *dev)
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100186{
187 struct usb_hcd *hcd = dev_get_drvdata(dev);
Sylvain Rochet7baddac2015-01-20 14:39:01 +0100188 struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100189 int ret;
190
191 ret = ehci_suspend(hcd, false);
192 if (ret)
193 return ret;
194
Sylvain Rochet7baddac2015-01-20 14:39:01 +0100195 atmel_stop_clock(atmel_ehci);
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100196 return 0;
197}
198
Arnd Bergmann9fc7c852016-03-02 16:24:06 +0100199static int __maybe_unused ehci_atmel_drv_resume(struct device *dev)
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100200{
201 struct usb_hcd *hcd = dev_get_drvdata(dev);
Sylvain Rochet7baddac2015-01-20 14:39:01 +0100202 struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100203
Sylvain Rochet7baddac2015-01-20 14:39:01 +0100204 atmel_start_clock(atmel_ehci);
Romain Izarde8470b52017-09-28 11:46:24 +0200205 ehci_resume(hcd, false);
206 return 0;
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100207}
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100208
Jean-Christophe PLAGNIOL-VILLARD9d843002011-11-22 12:11:13 +0800209#ifdef CONFIG_OF
210static const struct of_device_id atmel_ehci_dt_ids[] = {
211 { .compatible = "atmel,at91sam9g45-ehci" },
212 { /* sentinel */ }
213};
214
215MODULE_DEVICE_TABLE(of, atmel_ehci_dt_ids);
216#endif
217
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100218static SIMPLE_DEV_PM_OPS(ehci_atmel_pm_ops, ehci_atmel_drv_suspend,
219 ehci_atmel_drv_resume);
220
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700221static struct platform_driver ehci_atmel_driver = {
222 .probe = ehci_atmel_drv_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500223 .remove = ehci_atmel_drv_remove,
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700224 .shutdown = usb_hcd_platform_shutdown,
Jean-Christophe PLAGNIOL-VILLARD9d843002011-11-22 12:11:13 +0800225 .driver = {
226 .name = "atmel-ehci",
Sylvain Rochetca2c1dc2015-01-20 14:38:59 +0100227 .pm = &ehci_atmel_pm_ops,
Jean-Christophe PLAGNIOL-VILLARD9d843002011-11-22 12:11:13 +0800228 .of_match_table = of_match_ptr(atmel_ehci_dt_ids),
229 },
Nicolas Ferre501c9c02009-07-27 14:47:40 -0700230};
Manjunath Goudar97736962013-04-02 18:24:02 +0200231
232static int __init ehci_atmel_init(void)
233{
234 if (usb_disabled())
235 return -ENODEV;
236
237 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
Sylvain Rochet7baddac2015-01-20 14:39:01 +0100238 ehci_init_driver(&ehci_atmel_hc_driver, &ehci_atmel_drv_overrides);
Manjunath Goudar97736962013-04-02 18:24:02 +0200239 return platform_driver_register(&ehci_atmel_driver);
240}
241module_init(ehci_atmel_init);
242
243static void __exit ehci_atmel_cleanup(void)
244{
245 platform_driver_unregister(&ehci_atmel_driver);
246}
247module_exit(ehci_atmel_cleanup);
248
249MODULE_DESCRIPTION(DRIVER_DESC);
250MODULE_ALIAS("platform:atmel-ehci");
251MODULE_AUTHOR("Nicolas Ferre");
252MODULE_LICENSE("GPL");