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