1 /* Copyright (C) 2014 Altera Corporation
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Adopted from dwmac-sti.c
19 #include <linux/clk.h>
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/mfd/syscon.h>
24 #include <linux/of_address.h>
25 #include <linux/of_net.h>
26 #include <linux/of_platform.h>
27 #include <linux/phy.h>
28 #include <linux/platform_device.h>
29 #include <linux/regmap.h>
30 #include <linux/stmmac.h>
32 #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0x0
33 #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII 0x1
34 #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII 0x2
35 #define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
36 #define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK 0x00000003
38 struct socfpga_dwmac {
42 struct regmap *sys_mgr_base_addr;
43 struct device_node *dwmac_np;
46 static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev)
48 struct device_node *np = dev->of_node;
49 struct device_node *stmmac_np;
50 struct regmap *sys_mgr_base_addr;
54 stmmac_np = of_get_next_available_child(np, NULL);
56 dev_info(dev, "No dwmac node found\n");
60 if (!of_device_is_compatible(stmmac_np, "snps,dwmac")) {
61 dev_info(dev, "dwmac node isn't compatible with snps,dwmac\n");
65 dwmac->interface = of_get_phy_mode(stmmac_np);
66 of_node_put(stmmac_np);
68 sys_mgr_base_addr = syscon_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
69 if (IS_ERR(sys_mgr_base_addr)) {
70 dev_info(dev, "No sysmgr-syscon node found\n");
71 return PTR_ERR(sys_mgr_base_addr);
74 ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, ®_offset);
76 dev_info(dev, "Could not reg_offset into sysmgr-syscon!\n");
80 dwmac->reg_offset = reg_offset;
81 dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
82 dwmac->dwmac_np = stmmac_np;
88 static int socfpga_dwmac_setup(struct socfpga_dwmac *dwmac)
90 struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
91 int phymode = dwmac->interface;
92 u32 reg_offset = dwmac->reg_offset;
93 u32 ctrl, val, shift = 0;
95 if (of_machine_is_compatible("altr,socfpga-vt"))
99 case PHY_INTERFACE_MODE_RGMII:
100 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
102 case PHY_INTERFACE_MODE_MII:
103 case PHY_INTERFACE_MODE_GMII:
104 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
107 dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
111 regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
112 ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << shift);
113 ctrl |= val << shift;
115 regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
119 static int socfpga_dwmac_probe(struct platform_device *pdev)
121 struct device *dev = &pdev->dev;
122 struct device_node *node = dev->of_node;
124 struct socfpga_dwmac *dwmac;
126 dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
130 ret = socfpga_dwmac_parse_data(dwmac, dev);
132 dev_err(dev, "Unable to parse OF data\n");
136 ret = socfpga_dwmac_setup(dwmac);
138 dev_err(dev, "couldn't setup SoC glue (%d)\n", ret);
143 ret = of_platform_populate(node, NULL, NULL, dev);
145 dev_err(dev, "failed to add dwmac core\n");
149 dev_err(dev, "no device node, failed to add dwmac core\n");
153 platform_set_drvdata(pdev, dwmac);
158 static int socfpga_dwmac_remove(struct platform_device *pdev)
163 static const struct of_device_id socfpga_dwmac_match[] = {
164 { .compatible = "altr,socfpga-stmmac" },
167 MODULE_DEVICE_TABLE(of, socfpga_dwmac_match);
169 static struct platform_driver socfpga_dwmac_driver = {
170 .probe = socfpga_dwmac_probe,
171 .remove = socfpga_dwmac_remove,
173 .name = "socfpga-dwmac",
174 .of_match_table = of_match_ptr(socfpga_dwmac_match),
178 module_platform_driver(socfpga_dwmac_driver);
180 MODULE_ALIAS("platform:socfpga-dwmac");
181 MODULE_AUTHOR("Dinh Nguyen <dinguyen@altera.com>");
182 MODULE_LICENSE("GPL v2");
183 MODULE_DESCRIPTION("Altera SOCFPGA DWMAC Glue Layer");