2 * Copyright (C) 2016 Broadcom
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/delay.h>
16 #include <linux/kernel.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/mdio.h>
19 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/phy/phy.h>
23 #include <linux/regmap.h>
25 #define NSP_USB3_RST_CTRL_OFFSET 0x3f8
28 #define NSP_USB3_PHY_BASE_ADDR_REG 0x1f
30 #define NSP_USB3_PHY_PLL30_BLOCK 0x8000
31 #define NSP_USB3_PLL_CONTROL 0x01
32 #define NSP_USB3_PLLA_CONTROL0 0x0a
33 #define NSP_USB3_PLLA_CONTROL1 0x0b
35 #define NSP_USB3_PHY_TX_PMD_BLOCK 0x8040
36 #define NSP_USB3_TX_PMD_CONTROL1 0x01
38 #define NSP_USB3_PHY_PIPE_BLOCK 0x8060
39 #define NSP_USB3_LFPS_CMP 0x02
40 #define NSP_USB3_LFPS_DEGLITCH 0x03
43 struct regmap *usb3_ctrl;
45 struct mdio_device *mdiodev;
48 static int nsp_usb3_phy_init(struct phy *phy)
50 struct nsp_usb3_phy *iphy = phy_get_drvdata(phy);
51 struct mii_bus *bus = iphy->mdiodev->bus;
52 int addr = iphy->mdiodev->addr;
56 rc = regmap_read(iphy->usb3_ctrl, 0, &data);
60 rc = regmap_write(iphy->usb3_ctrl, 0, data);
64 rc = regmap_write(iphy->usb3_ctrl, NSP_USB3_RST_CTRL_OFFSET, 1);
68 rc = mdiobus_write(bus, addr, NSP_USB3_PHY_BASE_ADDR_REG,
69 NSP_USB3_PHY_PLL30_BLOCK);
73 rc = mdiobus_write(bus, addr, NSP_USB3_PLL_CONTROL, 0x1000);
77 rc = mdiobus_write(bus, addr, NSP_USB3_PLLA_CONTROL0, 0x6400);
81 rc = mdiobus_write(bus, addr, NSP_USB3_PLLA_CONTROL1, 0xc000);
85 rc = mdiobus_write(bus, addr, NSP_USB3_PLLA_CONTROL1, 0x8000);
89 rc = regmap_write(iphy->usb3_ctrl, NSP_USB3_RST_CTRL_OFFSET, 0);
93 rc = mdiobus_write(bus, addr, NSP_USB3_PLL_CONTROL, 0x9000);
97 rc = mdiobus_write(bus, addr, NSP_USB3_PHY_BASE_ADDR_REG,
98 NSP_USB3_PHY_PIPE_BLOCK);
102 rc = mdiobus_write(bus, addr, NSP_USB3_LFPS_CMP, 0xf30d);
106 rc = mdiobus_write(bus, addr, NSP_USB3_LFPS_DEGLITCH, 0x6302);
110 rc = mdiobus_write(bus, addr, NSP_USB3_PHY_BASE_ADDR_REG,
111 NSP_USB3_PHY_TX_PMD_BLOCK);
115 rc = mdiobus_write(bus, addr, NSP_USB3_TX_PMD_CONTROL1, 0x1003);
120 static struct phy_ops nsp_usb3_phy_ops = {
121 .init = nsp_usb3_phy_init,
122 .owner = THIS_MODULE,
125 static int nsp_usb3_phy_probe(struct mdio_device *mdiodev)
127 struct device *dev = &mdiodev->dev;
128 struct phy_provider *provider;
129 struct nsp_usb3_phy *iphy;
131 iphy = devm_kzalloc(dev, sizeof(*iphy), GFP_KERNEL);
134 iphy->mdiodev = mdiodev;
136 iphy->usb3_ctrl = syscon_regmap_lookup_by_phandle(dev->of_node,
138 if (IS_ERR(iphy->usb3_ctrl))
139 return PTR_ERR(iphy->usb3_ctrl);
141 iphy->phy = devm_phy_create(dev, dev->of_node, &nsp_usb3_phy_ops);
142 if (IS_ERR(iphy->phy)) {
143 dev_err(dev, "failed to create PHY\n");
144 return PTR_ERR(iphy->phy);
147 phy_set_drvdata(iphy->phy, iphy);
149 provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
150 if (IS_ERR(provider)) {
151 dev_err(dev, "could not register PHY provider\n");
152 return PTR_ERR(provider);
158 static const struct of_device_id nsp_usb3_phy_of_match[] = {
159 {.compatible = "brcm,nsp-usb3-phy",},
163 static struct mdio_driver nsp_usb3_phy_driver = {
166 .name = "nsp-usb3-phy",
167 .of_match_table = nsp_usb3_phy_of_match,
170 .probe = nsp_usb3_phy_probe,
173 mdio_module_driver(nsp_usb3_phy_driver);
175 MODULE_DESCRIPTION("Broadcom NSP USB3 PHY driver");
176 MODULE_LICENSE("GPL v2");
177 MODULE_AUTHOR("Yendapally Reddy Dhananjaya Reddy <yendapally.reddy@broadcom.com");