Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Nuvoton NPCM7xx driver for EHCI HCD
4 *
5 * Copyright (C) 2018 Nuvoton Technologies,
6 * Avi Fishman <avi.fishman@nuvoton.com> <avifishman70@gmail.com>
7 * Tomer Maimon <tomer.maimon@nuvoton.com> <tmaimon77@gmail.com>
8 *
9 * Based on various ehci-spear.c driver
10 */
11
12
13#include <linux/dma-mapping.h>
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
19#include <linux/pm.h>
20#include <linux/usb.h>
21#include <linux/usb/hcd.h>
22
23#include "ehci.h"
24
25#define DRIVER_DESC "EHCI npcm7xx driver"
26
27static struct hc_driver __read_mostly ehci_npcm7xx_hc_driver;
28
29static int __maybe_unused ehci_npcm7xx_drv_suspend(struct device *dev)
30{
31 struct usb_hcd *hcd = dev_get_drvdata(dev);
32 bool do_wakeup = device_may_wakeup(dev);
33
34 return ehci_suspend(hcd, do_wakeup);
35}
36
37static int __maybe_unused ehci_npcm7xx_drv_resume(struct device *dev)
38{
39 struct usb_hcd *hcd = dev_get_drvdata(dev);
40
41 ehci_resume(hcd, false);
42 return 0;
43}
44
45static SIMPLE_DEV_PM_OPS(ehci_npcm7xx_pm_ops, ehci_npcm7xx_drv_suspend,
46 ehci_npcm7xx_drv_resume);
47
48static int npcm7xx_ehci_hcd_drv_probe(struct platform_device *pdev)
49{
50 struct usb_hcd *hcd;
51 struct resource *res;
52 const struct hc_driver *driver = &ehci_npcm7xx_hc_driver;
53 int irq;
54 int retval;
55
56 dev_dbg(&pdev->dev, "initializing npcm7xx ehci USB Controller\n");
57
58 if (usb_disabled())
59 return -ENODEV;
60
61 irq = platform_get_irq(pdev, 0);
62 if (irq < 0) {
63 retval = irq;
64 goto fail;
65 }
66
67 /*
68 * Right now device-tree probed devices don't get dma_mask set.
69 * Since shared usb code relies on it, set it here for now.
70 * Once we have dma capability bindings this can go away.
71 */
72 retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
73 if (retval)
74 goto fail;
75
76 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
77 if (!hcd) {
78 retval = -ENOMEM;
79 goto fail;
80 }
81
82 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
83 if (IS_ERR(hcd->regs)) {
84 retval = PTR_ERR(hcd->regs);
85 goto err_put_hcd;
86 }
87 hcd->rsrc_start = res->start;
88 hcd->rsrc_len = resource_size(res);
89
90 /* registers start at offset 0x0 */
91 hcd_to_ehci(hcd)->caps = hcd->regs;
92
93 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
94 if (retval)
95 goto err_put_hcd;
96
97 device_wakeup_enable(hcd->self.controller);
98 return retval;
99
100err_put_hcd:
101 usb_put_hcd(hcd);
102fail:
103 dev_err(&pdev->dev, "init fail, %d\n", retval);
104
105 return retval;
106}
107
108static void npcm7xx_ehci_hcd_drv_remove(struct platform_device *pdev)
109{
110 struct usb_hcd *hcd = platform_get_drvdata(pdev);
111
112 usb_remove_hcd(hcd);
113
114 usb_put_hcd(hcd);
115}
116
117static const struct of_device_id npcm7xx_ehci_id_table[] = {
118 { .compatible = "nuvoton,npcm750-ehci" },
119 { },
120};
121MODULE_DEVICE_TABLE(of, npcm7xx_ehci_id_table);
122
123static struct platform_driver npcm7xx_ehci_hcd_driver = {
124 .probe = npcm7xx_ehci_hcd_drv_probe,
125 .remove_new = npcm7xx_ehci_hcd_drv_remove,
126 .shutdown = usb_hcd_platform_shutdown,
127 .driver = {
128 .name = "npcm7xx-ehci",
129 .bus = &platform_bus_type,
130 .pm = pm_ptr(&ehci_npcm7xx_pm_ops),
131 .of_match_table = npcm7xx_ehci_id_table,
132 }
133};
134
135static int __init ehci_npcm7xx_init(void)
136{
137 if (usb_disabled())
138 return -ENODEV;
139
140 ehci_init_driver(&ehci_npcm7xx_hc_driver, NULL);
141 return platform_driver_register(&npcm7xx_ehci_hcd_driver);
142}
143module_init(ehci_npcm7xx_init);
144
145static void __exit ehci_npcm7xx_cleanup(void)
146{
147 platform_driver_unregister(&npcm7xx_ehci_hcd_driver);
148}
149module_exit(ehci_npcm7xx_cleanup);
150
151MODULE_DESCRIPTION(DRIVER_DESC);
152MODULE_ALIAS("platform:npcm7xx-ehci");
153MODULE_AUTHOR("Avi Fishman");
154MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Nuvoton NPCM7xx driver for EHCI HCD
4 *
5 * Copyright (C) 2018 Nuvoton Technologies,
6 * Avi Fishman <avi.fishman@nuvoton.com> <avifishman70@gmail.com>
7 * Tomer Maimon <tomer.maimon@nuvoton.com> <tmaimon77@gmail.com>
8 *
9 * Based on various ehci-spear.c driver
10 */
11
12
13#include <linux/dma-mapping.h>
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
19#include <linux/pm.h>
20#include <linux/usb.h>
21#include <linux/usb/hcd.h>
22
23#include "ehci.h"
24
25#include <linux/regmap.h>
26#include <linux/mfd/syscon.h>
27
28#define DRIVER_DESC "EHCI npcm7xx driver"
29
30static const char hcd_name[] = "npcm7xx-ehci";
31
32#define USB2PHYCTL_OFFSET 0x144
33
34#define IPSRST2_OFFSET 0x24
35#define IPSRST3_OFFSET 0x34
36
37
38static struct hc_driver __read_mostly ehci_npcm7xx_hc_driver;
39
40static int __maybe_unused ehci_npcm7xx_drv_suspend(struct device *dev)
41{
42 struct usb_hcd *hcd = dev_get_drvdata(dev);
43 bool do_wakeup = device_may_wakeup(dev);
44
45 return ehci_suspend(hcd, do_wakeup);
46}
47
48static int __maybe_unused ehci_npcm7xx_drv_resume(struct device *dev)
49{
50 struct usb_hcd *hcd = dev_get_drvdata(dev);
51
52 ehci_resume(hcd, false);
53 return 0;
54}
55
56static SIMPLE_DEV_PM_OPS(ehci_npcm7xx_pm_ops, ehci_npcm7xx_drv_suspend,
57 ehci_npcm7xx_drv_resume);
58
59static int npcm7xx_ehci_hcd_drv_probe(struct platform_device *pdev)
60{
61 struct usb_hcd *hcd;
62 struct resource *res;
63 struct regmap *gcr_regmap;
64 struct regmap *rst_regmap;
65 const struct hc_driver *driver = &ehci_npcm7xx_hc_driver;
66 int irq;
67 int retval;
68
69 dev_dbg(&pdev->dev, "initializing npcm7xx ehci USB Controller\n");
70
71 gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
72 if (IS_ERR(gcr_regmap)) {
73 dev_err(&pdev->dev, "%s: failed to find nuvoton,npcm750-gcr\n",
74 __func__);
75 return PTR_ERR(gcr_regmap);
76 }
77
78 rst_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-rst");
79 if (IS_ERR(rst_regmap)) {
80 dev_err(&pdev->dev, "%s: failed to find nuvoton,npcm750-rst\n",
81 __func__);
82 return PTR_ERR(rst_regmap);
83 }
84
85 /********* phy init ******/
86 // reset usb host
87 regmap_update_bits(rst_regmap, IPSRST2_OFFSET,
88 (0x1 << 26), (0x1 << 26));
89 regmap_update_bits(rst_regmap, IPSRST3_OFFSET,
90 (0x1 << 25), (0x1 << 25));
91 regmap_update_bits(gcr_regmap, USB2PHYCTL_OFFSET,
92 (0x1 << 28), 0);
93
94 udelay(1);
95
96 // enable phy
97 regmap_update_bits(rst_regmap, IPSRST3_OFFSET,
98 (0x1 << 25), 0);
99
100 udelay(50); // enable phy
101
102 regmap_update_bits(gcr_regmap, USB2PHYCTL_OFFSET,
103 (0x1 << 28), (0x1 << 28));
104
105 // enable host
106 regmap_update_bits(rst_regmap, IPSRST2_OFFSET,
107 (0x1 << 26), 0);
108
109 if (usb_disabled())
110 return -ENODEV;
111
112 irq = platform_get_irq(pdev, 0);
113 if (irq < 0) {
114 retval = irq;
115 goto fail;
116 }
117
118 /*
119 * Right now device-tree probed devices don't get dma_mask set.
120 * Since shared usb code relies on it, set it here for now.
121 * Once we have dma capability bindings this can go away.
122 */
123 retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
124 if (retval)
125 goto fail;
126
127 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
128 if (!hcd) {
129 retval = -ENOMEM;
130 goto fail;
131 }
132
133 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
135 if (IS_ERR(hcd->regs)) {
136 retval = PTR_ERR(hcd->regs);
137 goto err_put_hcd;
138 }
139 hcd->rsrc_start = res->start;
140 hcd->rsrc_len = resource_size(res);
141
142 /* registers start at offset 0x0 */
143 hcd_to_ehci(hcd)->caps = hcd->regs;
144
145 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
146 if (retval)
147 goto err_put_hcd;
148
149 device_wakeup_enable(hcd->self.controller);
150 return retval;
151
152err_put_hcd:
153 usb_put_hcd(hcd);
154fail:
155 dev_err(&pdev->dev, "init fail, %d\n", retval);
156
157 return retval;
158}
159
160static int npcm7xx_ehci_hcd_drv_remove(struct platform_device *pdev)
161{
162 struct usb_hcd *hcd = platform_get_drvdata(pdev);
163
164 usb_remove_hcd(hcd);
165
166 usb_put_hcd(hcd);
167
168 return 0;
169}
170
171static const struct of_device_id npcm7xx_ehci_id_table[] = {
172 { .compatible = "nuvoton,npcm750-ehci" },
173 { },
174};
175MODULE_DEVICE_TABLE(of, npcm7xx_ehci_id_table);
176
177static struct platform_driver npcm7xx_ehci_hcd_driver = {
178 .probe = npcm7xx_ehci_hcd_drv_probe,
179 .remove = npcm7xx_ehci_hcd_drv_remove,
180 .shutdown = usb_hcd_platform_shutdown,
181 .driver = {
182 .name = "npcm7xx-ehci",
183 .bus = &platform_bus_type,
184 .pm = pm_ptr(&ehci_npcm7xx_pm_ops),
185 .of_match_table = npcm7xx_ehci_id_table,
186 }
187};
188
189static int __init ehci_npcm7xx_init(void)
190{
191 if (usb_disabled())
192 return -ENODEV;
193
194 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
195
196 ehci_init_driver(&ehci_npcm7xx_hc_driver, NULL);
197 return platform_driver_register(&npcm7xx_ehci_hcd_driver);
198}
199module_init(ehci_npcm7xx_init);
200
201static void __exit ehci_npcm7xx_cleanup(void)
202{
203 platform_driver_unregister(&npcm7xx_ehci_hcd_driver);
204}
205module_exit(ehci_npcm7xx_cleanup);
206
207MODULE_DESCRIPTION(DRIVER_DESC);
208MODULE_ALIAS("platform:npcm7xx-ehci");
209MODULE_AUTHOR("Avi Fishman");
210MODULE_LICENSE("GPL v2");