1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2016 Broadcom
3 
4 #include <linux/device.h>
5 #include <linux/module.h>
6 #include <linux/of_mdio.h>
7 #include <linux/mdio.h>
8 #include <linux/phy.h>
9 #include <linux/phy/phy.h>
10 
11 #define BLK_ADDR_REG_OFFSET	0x1f
12 #define PLL_AFE1_100MHZ_BLK	0x2100
13 #define PLL_CLK_AMP_OFFSET	0x03
14 #define PLL_CLK_AMP_2P05V	0x2b18
15 
16 static int ns2_pci_phy_init(struct phy *p)
17 {
18 	struct mdio_device *mdiodev = phy_get_drvdata(p);
19 	int rc;
20 
21 	/* select the AFE 100MHz block page */
22 	rc = mdiodev_write(mdiodev, BLK_ADDR_REG_OFFSET, PLL_AFE1_100MHZ_BLK);
23 	if (rc)
24 		goto err;
25 
26 	/* set the 100 MHz reference clock amplitude to 2.05 v */
27 	rc = mdiodev_write(mdiodev, PLL_CLK_AMP_OFFSET, PLL_CLK_AMP_2P05V);
28 	if (rc)
29 		goto err;
30 
31 	return 0;
32 
33 err:
34 	dev_err(&mdiodev->dev, "Error %d writing to phy\n", rc);
35 	return rc;
36 }
37 
38 static const struct phy_ops ns2_pci_phy_ops = {
39 	.init = ns2_pci_phy_init,
40 	.owner = THIS_MODULE,
41 };
42 
43 static int ns2_pci_phy_probe(struct mdio_device *mdiodev)
44 {
45 	struct device *dev = &mdiodev->dev;
46 	struct phy_provider *provider;
47 	struct phy *phy;
48 
49 	phy = devm_phy_create(dev, dev->of_node, &ns2_pci_phy_ops);
50 	if (IS_ERR(phy)) {
51 		dev_err(dev, "failed to create Phy\n");
52 		return PTR_ERR(phy);
53 	}
54 
55 	phy_set_drvdata(phy, mdiodev);
56 
57 	provider = devm_of_phy_provider_register(&phy->dev,
58 						 of_phy_simple_xlate);
59 	if (IS_ERR(provider)) {
60 		dev_err(dev, "failed to register Phy provider\n");
61 		return PTR_ERR(provider);
62 	}
63 
64 	dev_info(dev, "%s PHY registered\n", dev_name(dev));
65 
66 	return 0;
67 }
68 
69 static const struct of_device_id ns2_pci_phy_of_match[] = {
70 	{ .compatible = "brcm,ns2-pcie-phy", },
71 	{ /* sentinel */ },
72 };
73 MODULE_DEVICE_TABLE(of, ns2_pci_phy_of_match);
74 
75 static struct mdio_driver ns2_pci_phy_driver = {
76 	.mdiodrv = {
77 		.driver = {
78 			.name = "phy-bcm-ns2-pci",
79 			.of_match_table = ns2_pci_phy_of_match,
80 		},
81 	},
82 	.probe = ns2_pci_phy_probe,
83 };
84 mdio_module_driver(ns2_pci_phy_driver);
85 
86 MODULE_AUTHOR("Broadcom");
87 MODULE_DESCRIPTION("Broadcom Northstar2 PCI Phy driver");
88 MODULE_LICENSE("GPL v2");
89 MODULE_ALIAS("platform:phy-bcm-ns2-pci");
90