blob: b3554f70bd279646440c4d584ebba6d5dc38206a [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Deepak Sikric8c38de2010-11-10 14:33:18 +05302/*
3* OHCI HCD (Host Controller Driver) for USB.
4*
5* Copyright (C) 2010 ST Microelectronics.
6* Deepak Sikri<deepak.sikri@st.com>
7*
8* Based on various ohci-*.c drivers
9*
10* This file is licensed under the terms of the GNU General Public
11* License version 2. This program is licensed "as is" without any
12* warranty of any kind, whether express or implied.
13*/
14
Deepak Sikric8c38de2010-11-10 14:33:18 +053015#include <linux/clk.h>
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +053016#include <linux/dma-mapping.h>
17#include <linux/io.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
Stefan Roese56fafb92012-04-16 09:08:08 +053020#include <linux/of.h>
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +053021#include <linux/platform_device.h>
22#include <linux/signal.h>
23#include <linux/usb.h>
24#include <linux/usb/hcd.h>
Deepak Sikric8c38de2010-11-10 14:33:18 +053025
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +053026#include "ohci.h"
27
28#define DRIVER_DESC "OHCI SPEAr driver"
29
30static const char hcd_name[] = "SPEAr-ohci";
Deepak Sikric8c38de2010-11-10 14:33:18 +053031struct spear_ohci {
Deepak Sikric8c38de2010-11-10 14:33:18 +053032 struct clk *clk;
33};
34
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +053035#define to_spear_ohci(hcd) (struct spear_ohci *)(hcd_to_ohci(hcd)->priv)
Deepak Sikric8c38de2010-11-10 14:33:18 +053036
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +053037static struct hc_driver __read_mostly ohci_spear_hc_driver;
Deepak Sikric8c38de2010-11-10 14:33:18 +053038
39static int spear_ohci_hcd_drv_probe(struct platform_device *pdev)
40{
41 const struct hc_driver *driver = &ohci_spear_hc_driver;
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +053042 struct ohci_hcd *ohci;
Deepak Sikric8c38de2010-11-10 14:33:18 +053043 struct usb_hcd *hcd = NULL;
44 struct clk *usbh_clk;
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +053045 struct spear_ohci *sohci_p;
Deepak Sikric8c38de2010-11-10 14:33:18 +053046 struct resource *res;
47 int retval, irq;
Deepak Sikric8c38de2010-11-10 14:33:18 +053048
49 irq = platform_get_irq(pdev, 0);
50 if (irq < 0) {
51 retval = irq;
Viresh Kumar98515e52012-11-08 20:37:59 +053052 goto fail;
Deepak Sikric8c38de2010-11-10 14:33:18 +053053 }
54
Stefan Roese56fafb92012-04-16 09:08:08 +053055 /*
56 * Right now device-tree probed devices don't get dma_mask set.
57 * Since shared usb code relies on it, set it here for now.
58 * Once we have dma capability bindings this can go away.
59 */
Russell Kinge1fd7342013-06-27 12:36:37 +010060 retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +010061 if (retval)
62 goto fail;
Stefan Roese56fafb92012-04-16 09:08:08 +053063
Viresh Kumar98515e52012-11-08 20:37:59 +053064 usbh_clk = devm_clk_get(&pdev->dev, NULL);
Deepak Sikric8c38de2010-11-10 14:33:18 +053065 if (IS_ERR(usbh_clk)) {
66 dev_err(&pdev->dev, "Error getting interface clock\n");
67 retval = PTR_ERR(usbh_clk);
Viresh Kumar98515e52012-11-08 20:37:59 +053068 goto fail;
Deepak Sikric8c38de2010-11-10 14:33:18 +053069 }
70
71 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
72 if (!hcd) {
73 retval = -ENOMEM;
Viresh Kumar98515e52012-11-08 20:37:59 +053074 goto fail;
Deepak Sikric8c38de2010-11-10 14:33:18 +053075 }
76
77 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han09796be2013-12-11 16:29:15 +090078 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
79 if (IS_ERR(hcd->regs)) {
80 retval = PTR_ERR(hcd->regs);
Viresh Kumar98515e52012-11-08 20:37:59 +053081 goto err_put_hcd;
Deepak Sikric8c38de2010-11-10 14:33:18 +053082 }
83
Varka Bhadram0bf80cb2014-11-04 07:51:19 +053084 hcd->rsrc_start = pdev->resource[0].start;
85 hcd->rsrc_len = resource_size(res);
86
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +053087 sohci_p = to_spear_ohci(hcd);
88 sohci_p->clk = usbh_clk;
89
90 clk_prepare_enable(sohci_p->clk);
91
92 ohci = hcd_to_ohci(hcd);
Deepak Sikric8c38de2010-11-10 14:33:18 +053093
Yong Zhangb5dd18d2011-09-07 16:10:52 +080094 retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), 0);
Peter Chen3c9740a2013-11-05 10:46:02 +080095 if (retval == 0) {
96 device_wakeup_enable(hcd->self.controller);
Deepak Sikric8c38de2010-11-10 14:33:18 +053097 return retval;
Peter Chen3c9740a2013-11-05 10:46:02 +080098 }
Deepak Sikric8c38de2010-11-10 14:33:18 +053099
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +0530100 clk_disable_unprepare(sohci_p->clk);
Viresh Kumar98515e52012-11-08 20:37:59 +0530101err_put_hcd:
Deepak Sikric8c38de2010-11-10 14:33:18 +0530102 usb_put_hcd(hcd);
Viresh Kumar98515e52012-11-08 20:37:59 +0530103fail:
Deepak Sikric8c38de2010-11-10 14:33:18 +0530104 dev_err(&pdev->dev, "init fail, %d\n", retval);
105
106 return retval;
107}
108
109static int spear_ohci_hcd_drv_remove(struct platform_device *pdev)
110{
111 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +0530112 struct spear_ohci *sohci_p = to_spear_ohci(hcd);
Deepak Sikric8c38de2010-11-10 14:33:18 +0530113
114 usb_remove_hcd(hcd);
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +0530115 if (sohci_p->clk)
116 clk_disable_unprepare(sohci_p->clk);
Deepak Sikric8c38de2010-11-10 14:33:18 +0530117
Deepak Sikric8c38de2010-11-10 14:33:18 +0530118 usb_put_hcd(hcd);
Deepak Sikric8c38de2010-11-10 14:33:18 +0530119 return 0;
120}
121
122#if defined(CONFIG_PM)
Majunath Goudard2e3d2b2013-11-13 17:40:21 +0530123static int spear_ohci_hcd_drv_suspend(struct platform_device *pdev,
Deepak Sikric8c38de2010-11-10 14:33:18 +0530124 pm_message_t message)
125{
Majunath Goudard2e3d2b2013-11-13 17:40:21 +0530126 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Deepak Sikric8c38de2010-11-10 14:33:18 +0530127 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +0530128 struct spear_ohci *sohci_p = to_spear_ohci(hcd);
Majunath Goudard2e3d2b2013-11-13 17:40:21 +0530129 bool do_wakeup = device_may_wakeup(&pdev->dev);
130 int ret;
Deepak Sikric8c38de2010-11-10 14:33:18 +0530131
132 if (time_before(jiffies, ohci->next_statechange))
133 msleep(5);
134 ohci->next_statechange = jiffies;
135
Majunath Goudard2e3d2b2013-11-13 17:40:21 +0530136 ret = ohci_suspend(hcd, do_wakeup);
137 if (ret)
138 return ret;
139
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +0530140 clk_disable_unprepare(sohci_p->clk);
141
Majunath Goudard2e3d2b2013-11-13 17:40:21 +0530142 return ret;
Deepak Sikric8c38de2010-11-10 14:33:18 +0530143}
144
145static int spear_ohci_hcd_drv_resume(struct platform_device *dev)
146{
147 struct usb_hcd *hcd = platform_get_drvdata(dev);
148 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +0530149 struct spear_ohci *sohci_p = to_spear_ohci(hcd);
Deepak Sikric8c38de2010-11-10 14:33:18 +0530150
151 if (time_before(jiffies, ohci->next_statechange))
152 msleep(5);
153 ohci->next_statechange = jiffies;
154
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +0530155 clk_prepare_enable(sohci_p->clk);
Florian Fainellicfa49b42012-10-08 15:11:29 +0200156 ohci_resume(hcd, false);
Deepak Sikric8c38de2010-11-10 14:33:18 +0530157 return 0;
158}
159#endif
160
Jingoo Han10e15d62014-06-18 13:38:10 +0900161static const struct of_device_id spear_ohci_id_table[] = {
Stefan Roese56fafb92012-04-16 09:08:08 +0530162 { .compatible = "st,spear600-ohci", },
163 { },
164};
Luis de Bethencourt04c0b762015-09-19 14:10:58 +0100165MODULE_DEVICE_TABLE(of, spear_ohci_id_table);
Stefan Roese56fafb92012-04-16 09:08:08 +0530166
Deepak Sikric8c38de2010-11-10 14:33:18 +0530167/* Driver definition to register with the platform bus */
168static struct platform_driver spear_ohci_hcd_driver = {
169 .probe = spear_ohci_hcd_drv_probe,
170 .remove = spear_ohci_hcd_drv_remove,
171#ifdef CONFIG_PM
172 .suspend = spear_ohci_hcd_drv_suspend,
173 .resume = spear_ohci_hcd_drv_resume,
174#endif
175 .driver = {
Deepak Sikric8c38de2010-11-10 14:33:18 +0530176 .name = "spear-ohci",
Sachin Kamatc0d6f0b2013-05-21 17:17:20 +0530177 .of_match_table = spear_ohci_id_table,
Deepak Sikric8c38de2010-11-10 14:33:18 +0530178 },
179};
180
Manjunath Goudar1cc6ac52013-09-21 16:38:41 +0530181static const struct ohci_driver_overrides spear_overrides __initconst = {
182 .extra_priv_size = sizeof(struct spear_ohci),
183};
184static int __init ohci_spear_init(void)
185{
186 if (usb_disabled())
187 return -ENODEV;
188
189 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
190
191 ohci_init_driver(&ohci_spear_hc_driver, &spear_overrides);
192 return platform_driver_register(&spear_ohci_hcd_driver);
193}
194module_init(ohci_spear_init);
195
196static void __exit ohci_spear_cleanup(void)
197{
198 platform_driver_unregister(&spear_ohci_hcd_driver);
199}
200module_exit(ohci_spear_cleanup);
201
202MODULE_DESCRIPTION(DRIVER_DESC);
203MODULE_AUTHOR("Deepak Sikri");
204MODULE_LICENSE("GPL v2");
Deepak Sikric8c38de2010-11-10 14:33:18 +0530205MODULE_ALIAS("platform:spear-ohci");