Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * USB cluster support for Armada 375 platform.
0004  *
0005  * Copyright (C) 2014 Marvell
0006  *
0007  * Gregory CLEMENT <gregory.clement@free-electrons.com>
0008  *
0009  * Armada 375 comes with an USB2 host and device controller and an
0010  * USB3 controller. The USB cluster control register allows to manage
0011  * common features of both USB controllers.
0012  */
0013 
0014 #include <dt-bindings/phy/phy.h>
0015 #include <linux/init.h>
0016 #include <linux/io.h>
0017 #include <linux/kernel.h>
0018 #include <linux/of_address.h>
0019 #include <linux/phy/phy.h>
0020 #include <linux/platform_device.h>
0021 
0022 #define USB2_PHY_CONFIG_DISABLE BIT(0)
0023 
0024 struct armada375_cluster_phy {
0025     struct phy *phy;
0026     void __iomem *reg;
0027     bool use_usb3;
0028     int phy_provided;
0029 };
0030 
0031 static int armada375_usb_phy_init(struct phy *phy)
0032 {
0033     struct armada375_cluster_phy *cluster_phy;
0034     u32 reg;
0035 
0036     cluster_phy = phy_get_drvdata(phy);
0037     if (!cluster_phy)
0038         return -ENODEV;
0039 
0040     reg = readl(cluster_phy->reg);
0041     if (cluster_phy->use_usb3)
0042         reg |= USB2_PHY_CONFIG_DISABLE;
0043     else
0044         reg &= ~USB2_PHY_CONFIG_DISABLE;
0045     writel(reg, cluster_phy->reg);
0046 
0047     return 0;
0048 }
0049 
0050 static const struct phy_ops armada375_usb_phy_ops = {
0051     .init = armada375_usb_phy_init,
0052     .owner = THIS_MODULE,
0053 };
0054 
0055 /*
0056  * Only one controller can use this PHY. We shouldn't have the case
0057  * when two controllers want to use this PHY. But if this case occurs
0058  * then we provide a phy to the first one and return an error for the
0059  * next one. This error has also to be an error returned by
0060  * devm_phy_optional_get() so different from ENODEV for USB2. In the
0061  * USB3 case it still optional and we use ENODEV.
0062  */
0063 static struct phy *armada375_usb_phy_xlate(struct device *dev,
0064                     struct of_phandle_args *args)
0065 {
0066     struct armada375_cluster_phy *cluster_phy = dev_get_drvdata(dev);
0067 
0068     if (!cluster_phy)
0069         return  ERR_PTR(-ENODEV);
0070 
0071     /*
0072      * Either the phy had never been requested and then the first
0073      * usb claiming it can get it, or it had already been
0074      * requested in this case, we only allow to use it with the
0075      * same configuration.
0076      */
0077     if (WARN_ON((cluster_phy->phy_provided != PHY_NONE) &&
0078             (cluster_phy->phy_provided != args->args[0]))) {
0079         dev_err(dev, "This PHY has already been provided!\n");
0080         dev_err(dev, "Check your device tree, only one controller can use it\n.");
0081         if (args->args[0] == PHY_TYPE_USB2)
0082             return ERR_PTR(-EBUSY);
0083         else
0084             return ERR_PTR(-ENODEV);
0085     }
0086 
0087     if (args->args[0] == PHY_TYPE_USB2)
0088         cluster_phy->use_usb3 = false;
0089     else if (args->args[0] == PHY_TYPE_USB3)
0090         cluster_phy->use_usb3 = true;
0091     else {
0092         dev_err(dev, "Invalid PHY mode\n");
0093         return ERR_PTR(-ENODEV);
0094     }
0095 
0096     /* Store which phy mode is used for next test */
0097     cluster_phy->phy_provided = args->args[0];
0098 
0099     return cluster_phy->phy;
0100 }
0101 
0102 static int armada375_usb_phy_probe(struct platform_device *pdev)
0103 {
0104     struct device *dev = &pdev->dev;
0105     struct phy *phy;
0106     struct phy_provider *phy_provider;
0107     void __iomem *usb_cluster_base;
0108     struct armada375_cluster_phy *cluster_phy;
0109 
0110     cluster_phy = devm_kzalloc(dev, sizeof(*cluster_phy), GFP_KERNEL);
0111     if (!cluster_phy)
0112         return  -ENOMEM;
0113 
0114     usb_cluster_base = devm_platform_ioremap_resource(pdev, 0);
0115     if (IS_ERR(usb_cluster_base))
0116         return PTR_ERR(usb_cluster_base);
0117 
0118     phy = devm_phy_create(dev, NULL, &armada375_usb_phy_ops);
0119     if (IS_ERR(phy)) {
0120         dev_err(dev, "failed to create PHY\n");
0121         return PTR_ERR(phy);
0122     }
0123 
0124     cluster_phy->phy = phy;
0125     cluster_phy->reg = usb_cluster_base;
0126 
0127     dev_set_drvdata(dev, cluster_phy);
0128     phy_set_drvdata(phy, cluster_phy);
0129 
0130     phy_provider = devm_of_phy_provider_register(&pdev->dev,
0131                              armada375_usb_phy_xlate);
0132     return PTR_ERR_OR_ZERO(phy_provider);
0133 }
0134 
0135 static const struct of_device_id of_usb_cluster_table[] = {
0136     { .compatible = "marvell,armada-375-usb-cluster", },
0137     { /* end of list */ },
0138 };
0139 
0140 static struct platform_driver armada375_usb_phy_driver = {
0141     .probe  = armada375_usb_phy_probe,
0142     .driver = {
0143         .of_match_table = of_usb_cluster_table,
0144         .name  = "armada-375-usb-cluster",
0145     }
0146 };
0147 builtin_platform_driver(armada375_usb_phy_driver);