blob: bd40e597f25663ddc515ce04020835b580aaf194 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Jingoo Han62194242011-12-23 11:20:54 +09002/*
3 * SAMSUNG EXYNOS USB HOST OHCI Controller
4 *
5 * Copyright (C) 2011 Samsung Electronics Co.Ltd
6 * Author: Jingoo Han <jg1.han@samsung.com>
Jingoo Han62194242011-12-23 11:20:54 +09007 */
8
9#include <linux/clk.h>
Manjunath Goudar50a97e02013-09-21 16:38:38 +053010#include <linux/dma-mapping.h>
11#include <linux/io.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
Vivek Gautamd5138932012-07-16 11:25:36 +053014#include <linux/of.h>
Jingoo Han62194242011-12-23 11:20:54 +090015#include <linux/platform_device.h>
Vivek Gautam7d28e542014-05-05 10:32:57 +053016#include <linux/phy/phy.h>
Manjunath Goudar50a97e02013-09-21 16:38:38 +053017#include <linux/usb.h>
18#include <linux/usb/hcd.h>
Manjunath Goudar50a97e02013-09-21 16:38:38 +053019
20#include "ohci.h"
21
Krzysztof Kozlowskidea7b202020-01-04 16:20:55 +010022#define DRIVER_DESC "OHCI Exynos driver"
Manjunath Goudar50a97e02013-09-21 16:38:38 +053023
24static const char hcd_name[] = "ohci-exynos";
25static struct hc_driver __read_mostly exynos_ohci_hc_driver;
26
27#define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
Jingoo Han62194242011-12-23 11:20:54 +090028
Vivek Gautam7d28e542014-05-05 10:32:57 +053029#define PHY_NUMBER 3
30
Jingoo Han62194242011-12-23 11:20:54 +090031struct exynos_ohci_hcd {
Jingoo Han62194242011-12-23 11:20:54 +090032 struct clk *clk;
Marek Szyprowski01d40712019-05-20 11:08:23 +020033 struct device_node *of_node;
Vivek Gautam2db94162014-09-22 11:16:19 +053034 struct phy *phy[PHY_NUMBER];
Marek Szyprowski214b6062019-07-26 10:14:52 +020035 bool legacy_phy;
Jingoo Han62194242011-12-23 11:20:54 +090036};
37
Vivek Gautam7d28e542014-05-05 10:32:57 +053038static int exynos_ohci_get_phy(struct device *dev,
39 struct exynos_ohci_hcd *exynos_ohci)
40{
41 struct device_node *child;
42 struct phy *phy;
Marek Szyprowski214b6062019-07-26 10:14:52 +020043 int phy_number, num_phys;
Vivek Gautam2db94162014-09-22 11:16:19 +053044 int ret;
Vivek Gautam7d28e542014-05-05 10:32:57 +053045
Vivek Gautam2db94162014-09-22 11:16:19 +053046 /* Get PHYs for the controller */
Marek Szyprowski214b6062019-07-26 10:14:52 +020047 num_phys = of_count_phandle_with_args(dev->of_node, "phys",
48 "#phy-cells");
49 for (phy_number = 0; phy_number < num_phys; phy_number++) {
50 phy = devm_of_phy_get_by_index(dev, dev->of_node, phy_number);
51 if (IS_ERR(phy))
52 return PTR_ERR(phy);
53 exynos_ohci->phy[phy_number] = phy;
54 }
55 if (num_phys > 0)
56 return 0;
57
58 /* Get PHYs using legacy bindings */
Vivek Gautam7d28e542014-05-05 10:32:57 +053059 for_each_available_child_of_node(dev->of_node, child) {
60 ret = of_property_read_u32(child, "reg", &phy_number);
61 if (ret) {
62 dev_err(dev, "Failed to parse device tree\n");
63 of_node_put(child);
64 return ret;
65 }
66
67 if (phy_number >= PHY_NUMBER) {
68 dev_err(dev, "Invalid number of PHYs\n");
69 of_node_put(child);
70 return -EINVAL;
71 }
72
Sachin Kamat473e92e2014-06-06 14:13:44 +053073 phy = devm_of_phy_get(dev, child, NULL);
Vivek Gautam2db94162014-09-22 11:16:19 +053074 exynos_ohci->phy[phy_number] = phy;
Vivek Gautam2db94162014-09-22 11:16:19 +053075 if (IS_ERR(phy)) {
76 ret = PTR_ERR(phy);
77 if (ret == -EPROBE_DEFER) {
Krzysztof Kozlowski68bd6fc2017-01-07 10:41:41 +020078 of_node_put(child);
Vivek Gautam2db94162014-09-22 11:16:19 +053079 return ret;
80 } else if (ret != -ENOSYS && ret != -ENODEV) {
81 dev_err(dev,
82 "Error retrieving usb2 phy: %d\n", ret);
Krzysztof Kozlowski68bd6fc2017-01-07 10:41:41 +020083 of_node_put(child);
Vivek Gautam2db94162014-09-22 11:16:19 +053084 return ret;
85 }
86 }
Vivek Gautam2f7f41c2014-08-05 16:09:08 +053087 }
88
Marek Szyprowski214b6062019-07-26 10:14:52 +020089 exynos_ohci->legacy_phy = true;
Vivek Gautam2f7f41c2014-08-05 16:09:08 +053090 return 0;
Vivek Gautam7d28e542014-05-05 10:32:57 +053091}
92
93static int exynos_ohci_phy_enable(struct device *dev)
Vivek Gautamed993bf2013-01-22 18:30:43 +053094{
Vivek Gautam54969ed2014-05-05 10:33:42 +053095 struct usb_hcd *hcd = dev_get_drvdata(dev);
Manjunath Goudar50a97e02013-09-21 16:38:38 +053096 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Vivek Gautam7d28e542014-05-05 10:32:57 +053097 int i;
98 int ret = 0;
Vivek Gautamed993bf2013-01-22 18:30:43 +053099
Vivek Gautam7d28e542014-05-05 10:32:57 +0530100 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
Vivek Gautam2db94162014-09-22 11:16:19 +0530101 if (!IS_ERR(exynos_ohci->phy[i]))
102 ret = phy_power_on(exynos_ohci->phy[i]);
Vivek Gautam7d28e542014-05-05 10:32:57 +0530103 if (ret)
104 for (i--; i >= 0; i--)
Vivek Gautam2db94162014-09-22 11:16:19 +0530105 if (!IS_ERR(exynos_ohci->phy[i]))
106 phy_power_off(exynos_ohci->phy[i]);
Vivek Gautam7d28e542014-05-05 10:32:57 +0530107
108 return ret;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530109}
110
Vivek Gautam54969ed2014-05-05 10:33:42 +0530111static void exynos_ohci_phy_disable(struct device *dev)
Vivek Gautamed993bf2013-01-22 18:30:43 +0530112{
Vivek Gautam54969ed2014-05-05 10:33:42 +0530113 struct usb_hcd *hcd = dev_get_drvdata(dev);
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530114 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Vivek Gautam7d28e542014-05-05 10:32:57 +0530115 int i;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530116
Vivek Gautam7d28e542014-05-05 10:32:57 +0530117 for (i = 0; i < PHY_NUMBER; i++)
Vivek Gautam2db94162014-09-22 11:16:19 +0530118 if (!IS_ERR(exynos_ohci->phy[i]))
119 phy_power_off(exynos_ohci->phy[i]);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530120}
121
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500122static int exynos_ohci_probe(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900123{
Jingoo Han62194242011-12-23 11:20:54 +0900124 struct exynos_ohci_hcd *exynos_ohci;
125 struct usb_hcd *hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900126 struct resource *res;
127 int irq;
128 int err;
129
Vivek Gautamd5138932012-07-16 11:25:36 +0530130 /*
131 * Right now device-tree probed devices don't get dma_mask set.
132 * Since shared usb code relies on it, set it here for now.
133 * Once we move to full device tree support this will vanish off.
134 */
Russell Kinge1fd7342013-06-27 12:36:37 +0100135 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100136 if (err)
137 return err;
Vivek Gautamd5138932012-07-16 11:25:36 +0530138
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530139 hcd = usb_create_hcd(&exynos_ohci_hc_driver,
140 &pdev->dev, dev_name(&pdev->dev));
141 if (!hcd) {
142 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han62194242011-12-23 11:20:54 +0900143 return -ENOMEM;
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530144 }
145
146 exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900147
Vivek Gautam7d28e542014-05-05 10:32:57 +0530148 err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
149 if (err)
150 goto fail_clk;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530151
Jingoo Han60d80ad2012-10-04 16:11:50 +0900152 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
Jingoo Han62194242011-12-23 11:20:54 +0900153
154 if (IS_ERR(exynos_ohci->clk)) {
155 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
156 err = PTR_ERR(exynos_ohci->clk);
157 goto fail_clk;
158 }
159
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900160 err = clk_prepare_enable(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900161 if (err)
Jingoo Han60d80ad2012-10-04 16:11:50 +0900162 goto fail_clk;
Jingoo Han62194242011-12-23 11:20:54 +0900163
164 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vivek Gautambae00c12014-05-10 17:30:10 +0530165 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
166 if (IS_ERR(hcd->regs)) {
167 err = PTR_ERR(hcd->regs);
Jingoo Han62194242011-12-23 11:20:54 +0900168 goto fail_io;
169 }
Varka Bhadramb87a9c52014-11-04 07:51:13 +0530170 hcd->rsrc_start = res->start;
171 hcd->rsrc_len = resource_size(res);
Jingoo Han62194242011-12-23 11:20:54 +0900172
173 irq = platform_get_irq(pdev, 0);
174 if (!irq) {
175 dev_err(&pdev->dev, "Failed to get IRQ\n");
176 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900177 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900178 }
179
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530180 platform_set_drvdata(pdev, hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900181
Vivek Gautam7d28e542014-05-05 10:32:57 +0530182 err = exynos_ohci_phy_enable(&pdev->dev);
183 if (err) {
184 dev_err(&pdev->dev, "Failed to enable USB phy\n");
185 goto fail_io;
186 }
Jingoo Han62194242011-12-23 11:20:54 +0900187
Marek Szyprowski01d40712019-05-20 11:08:23 +0200188 /*
Marek Szyprowski214b6062019-07-26 10:14:52 +0200189 * Workaround: reset of_node pointer to avoid conflict between legacy
190 * Exynos OHCI port subnodes and generic USB device bindings
Marek Szyprowski01d40712019-05-20 11:08:23 +0200191 */
192 exynos_ohci->of_node = pdev->dev.of_node;
Marek Szyprowski214b6062019-07-26 10:14:52 +0200193 if (exynos_ohci->legacy_phy)
194 pdev->dev.of_node = NULL;
Marek Szyprowski01d40712019-05-20 11:08:23 +0200195
Jingoo Han62194242011-12-23 11:20:54 +0900196 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
197 if (err) {
198 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamed993bf2013-01-22 18:30:43 +0530199 goto fail_add_hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900200 }
Peter Chen3c9740a2013-11-05 10:46:02 +0800201 device_wakeup_enable(hcd->self.controller);
Jingoo Han62194242011-12-23 11:20:54 +0900202 return 0;
203
Vivek Gautamed993bf2013-01-22 18:30:43 +0530204fail_add_hcd:
Vivek Gautam54969ed2014-05-05 10:33:42 +0530205 exynos_ohci_phy_disable(&pdev->dev);
Marek Szyprowski01d40712019-05-20 11:08:23 +0200206 pdev->dev.of_node = exynos_ohci->of_node;
Jingoo Han62194242011-12-23 11:20:54 +0900207fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900208 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900209fail_clk:
210 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900211 return err;
212}
213
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500214static int exynos_ohci_remove(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900215{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530216 struct usb_hcd *hcd = platform_get_drvdata(pdev);
217 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900218
Marek Szyprowski01d40712019-05-20 11:08:23 +0200219 pdev->dev.of_node = exynos_ohci->of_node;
220
Jingoo Han62194242011-12-23 11:20:54 +0900221 usb_remove_hcd(hcd);
222
Vivek Gautam54969ed2014-05-05 10:33:42 +0530223 exynos_ohci_phy_disable(&pdev->dev);
Jingoo Han62194242011-12-23 11:20:54 +0900224
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900225 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900226
227 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900228
229 return 0;
230}
231
232static void exynos_ohci_shutdown(struct platform_device *pdev)
233{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530234 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900235
236 if (hcd->driver->shutdown)
237 hcd->driver->shutdown(hcd);
238}
239
240#ifdef CONFIG_PM
241static int exynos_ohci_suspend(struct device *dev)
242{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530243 struct usb_hcd *hcd = dev_get_drvdata(dev);
244 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Majunath Goudar14982e32013-11-13 17:40:20 +0530245 bool do_wakeup = device_may_wakeup(dev);
Majunath Goudar14982e32013-11-13 17:40:20 +0530246 int rc = ohci_suspend(hcd, do_wakeup);
Jingoo Han62194242011-12-23 11:20:54 +0900247
Majunath Goudar14982e32013-11-13 17:40:20 +0530248 if (rc)
249 return rc;
250
Vivek Gautam54969ed2014-05-05 10:33:42 +0530251 exynos_ohci_phy_disable(dev);
Jingoo Hane864abe2012-06-28 16:49:42 +0900252
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900253 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900254
Majunath Goudar14982e32013-11-13 17:40:20 +0530255 return 0;
Jingoo Han62194242011-12-23 11:20:54 +0900256}
257
258static int exynos_ohci_resume(struct device *dev)
259{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530260 struct usb_hcd *hcd = dev_get_drvdata(dev);
261 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Vivek Gautam7d28e542014-05-05 10:32:57 +0530262 int ret;
Jingoo Han62194242011-12-23 11:20:54 +0900263
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900264 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900265
Vivek Gautam7d28e542014-05-05 10:32:57 +0530266 ret = exynos_ohci_phy_enable(dev);
267 if (ret) {
268 dev_err(dev, "Failed to enable USB phy\n");
269 clk_disable_unprepare(exynos_ohci->clk);
270 return ret;
271 }
Jingoo Han62194242011-12-23 11:20:54 +0900272
Florian Fainellicfa49b42012-10-08 15:11:29 +0200273 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900274
275 return 0;
276}
277#else
278#define exynos_ohci_suspend NULL
279#define exynos_ohci_resume NULL
280#endif
281
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530282static const struct ohci_driver_overrides exynos_overrides __initconst = {
283 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
284};
285
Jingoo Han62194242011-12-23 11:20:54 +0900286static const struct dev_pm_ops exynos_ohci_pm_ops = {
287 .suspend = exynos_ohci_suspend,
288 .resume = exynos_ohci_resume,
289};
290
Vivek Gautamd5138932012-07-16 11:25:36 +0530291#ifdef CONFIG_OF
292static const struct of_device_id exynos_ohci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530293 { .compatible = "samsung,exynos4210-ohci" },
Vivek Gautamd5138932012-07-16 11:25:36 +0530294 {},
295};
296MODULE_DEVICE_TABLE(of, exynos_ohci_match);
297#endif
298
Jingoo Han62194242011-12-23 11:20:54 +0900299static struct platform_driver exynos_ohci_driver = {
300 .probe = exynos_ohci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500301 .remove = exynos_ohci_remove,
Jingoo Han62194242011-12-23 11:20:54 +0900302 .shutdown = exynos_ohci_shutdown,
303 .driver = {
304 .name = "exynos-ohci",
Jingoo Han62194242011-12-23 11:20:54 +0900305 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530306 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900307 }
308};
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530309static int __init ohci_exynos_init(void)
310{
311 if (usb_disabled())
312 return -ENODEV;
313
314 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
315 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
316 return platform_driver_register(&exynos_ohci_driver);
317}
318module_init(ohci_exynos_init);
319
320static void __exit ohci_exynos_cleanup(void)
321{
322 platform_driver_unregister(&exynos_ohci_driver);
323}
324module_exit(ohci_exynos_cleanup);
Jingoo Han62194242011-12-23 11:20:54 +0900325
326MODULE_ALIAS("platform:exynos-ohci");
327MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530328MODULE_LICENSE("GPL v2");