blob: 8ffd0f14f8098ea364f7594afd87d9a897ad073e [file] [log] [blame]
Al Cooper49859e52017-09-22 15:34:01 -04001/*
2 * phy-brcm-usb.c - Broadcom USB Phy Driver
3 *
4 * Copyright (C) 2015-2017 Broadcom
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/phy/phy.h>
23#include <linux/platform_device.h>
24#include <linux/interrupt.h>
25#include <linux/soc/brcmstb/brcmstb.h>
26#include <dt-bindings/phy/phy.h>
27
28#include "phy-brcm-usb-init.h"
29
30enum brcm_usb_phy_id {
31 BRCM_USB_PHY_2_0 = 0,
32 BRCM_USB_PHY_3_0,
33 BRCM_USB_PHY_ID_MAX
34};
35
36struct value_to_name_map {
37 int value;
38 const char *name;
39};
40
41static struct value_to_name_map brcm_dr_mode_to_name[] = {
42 { USB_CTLR_MODE_HOST, "host" },
43 { USB_CTLR_MODE_DEVICE, "peripheral" },
44 { USB_CTLR_MODE_DRD, "drd" },
45 { USB_CTLR_MODE_TYPEC_PD, "typec-pd" }
46};
47
48struct brcm_usb_phy {
49 struct phy *phy;
50 unsigned int id;
51 bool inited;
52};
53
54struct brcm_usb_phy_data {
55 struct brcm_usb_init_params ini;
56 bool has_eohci;
57 bool has_xhci;
58 struct clk *usb_20_clk;
59 struct clk *usb_30_clk;
60 struct mutex mutex; /* serialize phy init */
61 int init_count;
62 struct brcm_usb_phy phys[BRCM_USB_PHY_ID_MAX];
63};
64
65static int brcm_usb_phy_init(struct phy *gphy)
66{
67 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
68 struct brcm_usb_phy_data *priv =
69 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
70
71 /*
72 * Use a lock to make sure a second caller waits until
73 * the base phy is inited before using it.
74 */
75 mutex_lock(&priv->mutex);
76 if (priv->init_count++ == 0) {
77 clk_enable(priv->usb_20_clk);
78 clk_enable(priv->usb_30_clk);
79 brcm_usb_init_common(&priv->ini);
80 }
81 mutex_unlock(&priv->mutex);
82 if (phy->id == BRCM_USB_PHY_2_0)
83 brcm_usb_init_eohci(&priv->ini);
84 else if (phy->id == BRCM_USB_PHY_3_0)
85 brcm_usb_init_xhci(&priv->ini);
86 phy->inited = true;
87 dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id,
88 priv->init_count);
89
90 return 0;
91}
92
93static int brcm_usb_phy_exit(struct phy *gphy)
94{
95 struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
96 struct brcm_usb_phy_data *priv =
97 container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
98
99 dev_dbg(&gphy->dev, "EXIT\n");
100 if (phy->id == BRCM_USB_PHY_2_0)
101 brcm_usb_uninit_eohci(&priv->ini);
102 if (phy->id == BRCM_USB_PHY_3_0)
103 brcm_usb_uninit_xhci(&priv->ini);
104
105 /* If both xhci and eohci are gone, reset everything else */
106 mutex_lock(&priv->mutex);
107 if (--priv->init_count == 0) {
108 brcm_usb_uninit_common(&priv->ini);
109 clk_disable(priv->usb_20_clk);
110 clk_disable(priv->usb_30_clk);
111 }
112 mutex_unlock(&priv->mutex);
113 phy->inited = false;
114 return 0;
115}
116
117static struct phy_ops brcm_usb_phy_ops = {
118 .init = brcm_usb_phy_init,
119 .exit = brcm_usb_phy_exit,
120 .owner = THIS_MODULE,
121};
122
123static struct phy *brcm_usb_phy_xlate(struct device *dev,
124 struct of_phandle_args *args)
125{
126 struct brcm_usb_phy_data *data = dev_get_drvdata(dev);
127
128 /*
129 * values 0 and 1 are for backward compatibility with
130 * device tree nodes from older bootloaders.
131 */
132 switch (args->args[0]) {
133 case 0:
134 case PHY_TYPE_USB2:
135 if (data->phys[BRCM_USB_PHY_2_0].phy)
136 return data->phys[BRCM_USB_PHY_2_0].phy;
137 dev_warn(dev, "Error, 2.0 Phy not found\n");
138 break;
139 case 1:
140 case PHY_TYPE_USB3:
141 if (data->phys[BRCM_USB_PHY_3_0].phy)
142 return data->phys[BRCM_USB_PHY_3_0].phy;
143 dev_warn(dev, "Error, 3.0 Phy not found\n");
144 break;
145 }
146 return ERR_PTR(-ENODEV);
147}
148
149static int name_to_value(struct value_to_name_map *table, int count,
150 const char *name, int *value)
151{
152 int x;
153
154 *value = 0;
155 for (x = 0; x < count; x++) {
156 if (sysfs_streq(name, table[x].name)) {
157 *value = x;
158 return 0;
159 }
160 }
161 return -EINVAL;
162}
163
164static int brcm_usb_phy_dvr_init(struct device *dev,
165 struct brcm_usb_phy_data *priv,
166 struct device_node *dn)
167{
168 struct phy *gphy;
169 int err;
170
171 priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb");
172 if (IS_ERR(priv->usb_20_clk)) {
173 dev_info(dev, "Clock not found in Device Tree\n");
174 priv->usb_20_clk = NULL;
175 }
176 err = clk_prepare_enable(priv->usb_20_clk);
177 if (err)
178 return err;
179
180 if (priv->has_eohci) {
181 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
182 if (IS_ERR(gphy)) {
183 dev_err(dev, "failed to create EHCI/OHCI PHY\n");
184 return PTR_ERR(gphy);
185 }
186 priv->phys[BRCM_USB_PHY_2_0].phy = gphy;
187 priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0;
188 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]);
189 }
190
191 if (priv->has_xhci) {
192 gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
193 if (IS_ERR(gphy)) {
194 dev_err(dev, "failed to create XHCI PHY\n");
195 return PTR_ERR(gphy);
196 }
197 priv->phys[BRCM_USB_PHY_3_0].phy = gphy;
198 priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0;
199 phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]);
200
201 priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3");
202 if (IS_ERR(priv->usb_30_clk)) {
203 dev_info(dev,
204 "USB3.0 clock not found in Device Tree\n");
205 priv->usb_30_clk = NULL;
206 }
207 err = clk_prepare_enable(priv->usb_30_clk);
208 if (err)
209 return err;
210 }
211 return 0;
212}
213
214static int brcm_usb_phy_probe(struct platform_device *pdev)
215{
216 struct resource *res;
217 struct device *dev = &pdev->dev;
218 struct brcm_usb_phy_data *priv;
219 struct phy_provider *phy_provider;
220 struct device_node *dn = pdev->dev.of_node;
221 int err;
222 const char *mode;
223
224 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
225 if (!priv)
226 return -ENOMEM;
227 platform_set_drvdata(pdev, priv);
228
229 priv->ini.family_id = brcmstb_get_family_id();
230 priv->ini.product_id = brcmstb_get_product_id();
231 brcm_usb_set_family_map(&priv->ini);
232 dev_dbg(dev, "Best mapping table is for %s\n",
233 priv->ini.family_name);
234 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
235 if (!res) {
236 dev_err(dev, "can't get USB_CTRL base address\n");
237 return -EINVAL;
238 }
239 priv->ini.ctrl_regs = devm_ioremap_resource(dev, res);
240 if (IS_ERR(priv->ini.ctrl_regs)) {
241 dev_err(dev, "can't map CTRL register space\n");
242 return -EINVAL;
243 }
244
245 /* The XHCI EC registers are optional */
246 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
247 if (res) {
248 priv->ini.xhci_ec_regs =
249 devm_ioremap_resource(dev, res);
250 if (IS_ERR(priv->ini.xhci_ec_regs)) {
251 dev_err(dev, "can't map XHCI EC register space\n");
252 return -EINVAL;
253 }
254 }
255
256 of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp);
257 of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc);
258
259 priv->ini.mode = USB_CTLR_MODE_HOST;
260 err = of_property_read_string(dn, "dr_mode", &mode);
261 if (err == 0) {
262 name_to_value(&brcm_dr_mode_to_name[0],
263 ARRAY_SIZE(brcm_dr_mode_to_name),
264 mode, &priv->ini.mode);
265 }
266 if (of_property_read_bool(dn, "brcm,has_xhci"))
267 priv->has_xhci = true;
268 if (of_property_read_bool(dn, "brcm,has_eohci"))
269 priv->has_eohci = true;
270
271 err = brcm_usb_phy_dvr_init(dev, priv, dn);
272 if (err)
273 return err;
274
275 mutex_init(&priv->mutex);
276
277 /* make sure invert settings are correct */
278 brcm_usb_init_ipp(&priv->ini);
279
280 /* start with everything off */
281 if (priv->has_xhci)
282 brcm_usb_uninit_xhci(&priv->ini);
283 if (priv->has_eohci)
284 brcm_usb_uninit_eohci(&priv->ini);
285 brcm_usb_uninit_common(&priv->ini);
286 clk_disable(priv->usb_20_clk);
287 clk_disable(priv->usb_30_clk);
288
289 phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate);
290 if (IS_ERR(phy_provider))
291 return PTR_ERR(phy_provider);
292
293 return 0;
294}
295
296#ifdef CONFIG_PM_SLEEP
297static int brcm_usb_phy_suspend(struct device *dev)
298{
299 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
300
301 if (priv->init_count) {
302 clk_disable(priv->usb_20_clk);
303 clk_disable(priv->usb_30_clk);
304 }
305 return 0;
306}
307
308static int brcm_usb_phy_resume(struct device *dev)
309{
310 struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
311
312 clk_enable(priv->usb_20_clk);
313 clk_enable(priv->usb_30_clk);
314 brcm_usb_init_ipp(&priv->ini);
315
316 /*
317 * Initialize anything that was previously initialized.
318 * Uninitialize anything that wasn't previously initialized.
319 */
320 if (priv->init_count) {
321 brcm_usb_init_common(&priv->ini);
322 if (priv->phys[BRCM_USB_PHY_2_0].inited) {
323 brcm_usb_init_eohci(&priv->ini);
324 } else if (priv->has_eohci) {
325 brcm_usb_uninit_eohci(&priv->ini);
326 clk_disable(priv->usb_20_clk);
327 }
328 if (priv->phys[BRCM_USB_PHY_3_0].inited) {
329 brcm_usb_init_xhci(&priv->ini);
330 } else if (priv->has_xhci) {
331 brcm_usb_uninit_xhci(&priv->ini);
332 clk_disable(priv->usb_30_clk);
333 }
334 } else {
335 if (priv->has_xhci)
336 brcm_usb_uninit_xhci(&priv->ini);
337 if (priv->has_eohci)
338 brcm_usb_uninit_eohci(&priv->ini);
339 brcm_usb_uninit_common(&priv->ini);
340 clk_disable(priv->usb_20_clk);
341 clk_disable(priv->usb_30_clk);
342 }
343
344 return 0;
345}
346#endif /* CONFIG_PM_SLEEP */
347
348static const struct dev_pm_ops brcm_usb_phy_pm_ops = {
349 SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume)
350};
351
352static const struct of_device_id brcm_usb_dt_ids[] = {
353 { .compatible = "brcm,brcmstb-usb-phy" },
354 { /* sentinel */ }
355};
356
357MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids);
358
359static struct platform_driver brcm_usb_driver = {
360 .probe = brcm_usb_phy_probe,
361 .driver = {
362 .name = "brcmstb-usb-phy",
363 .owner = THIS_MODULE,
364 .pm = &brcm_usb_phy_pm_ops,
365 .of_match_table = brcm_usb_dt_ids,
366 },
367};
368
369module_platform_driver(brcm_usb_driver);
370
371MODULE_ALIAS("platform:brcmstb-usb-phy");
372MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
373MODULE_DESCRIPTION("BRCM USB PHY driver");
374MODULE_LICENSE("GPL v2");