]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/stmicro/stmmac/dwmac-socfpga.c
net: stmmac: Add SOCFPGA glue driver
[karo-tx-linux.git] / drivers / net / ethernet / stmicro / stmmac / dwmac-socfpga.c
1 /*  Copyright (C) 2014 Altera Corporation
2  *
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.
7  *
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.
12  *
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/>.
15  *
16  * Adopted from dwmac-sti.c
17  */
18
19 #include <linux/clk.h>
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/of.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>
31
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
37
38 struct socfpga_dwmac {
39         int     interface;
40         u32     reg_offset;
41         struct  device *dev;
42         struct regmap *sys_mgr_base_addr;
43         struct  device_node *dwmac_np;
44 };
45
46 static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev)
47 {
48         struct device_node *np  = dev->of_node;
49         struct device_node *stmmac_np;
50         struct regmap *sys_mgr_base_addr;
51         u32 reg_offset;
52         int ret;
53
54         stmmac_np = of_get_next_available_child(np, NULL);
55         if (!stmmac_np) {
56                 dev_info(dev, "No dwmac node found\n");
57                 return -EINVAL;
58         }
59
60         if (!of_device_is_compatible(stmmac_np, "snps,dwmac")) {
61                 dev_info(dev, "dwmac node isn't compatible with snps,dwmac\n");
62                 return -EINVAL;
63         }
64
65         dwmac->interface = of_get_phy_mode(stmmac_np);
66         of_node_put(stmmac_np);
67
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);
72         }
73
74         ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
75         if (ret) {
76                 dev_info(dev, "Could not reg_offset into sysmgr-syscon!\n");
77                 return -EINVAL;
78         }
79
80         dwmac->reg_offset = reg_offset;
81         dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
82         dwmac->dwmac_np = stmmac_np;
83         dwmac->dev = dev;
84
85         return 0;
86 }
87
88 static int socfpga_dwmac_setup(struct socfpga_dwmac *dwmac)
89 {
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;
94
95         if (of_machine_is_compatible("altr,socfpga-vt"))
96                 return 0;
97
98         switch (phymode) {
99         case PHY_INTERFACE_MODE_RGMII:
100                 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
101                 break;
102         case PHY_INTERFACE_MODE_MII:
103         case PHY_INTERFACE_MODE_GMII:
104                 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
105                 break;
106         default:
107                 dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
108                 return -EINVAL;
109         }
110
111         regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
112         ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << shift);
113         ctrl |= val << shift;
114
115         regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
116         return 0;
117 }
118
119 static int socfpga_dwmac_probe(struct platform_device *pdev)
120 {
121         struct device           *dev = &pdev->dev;
122         struct device_node      *node = dev->of_node;
123         int                     ret = -ENOMEM;
124         struct socfpga_dwmac    *dwmac;
125
126         dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
127         if (!dwmac)
128                 return -ENOMEM;
129
130         ret = socfpga_dwmac_parse_data(dwmac, dev);
131         if (ret) {
132                 dev_err(dev, "Unable to parse OF data\n");
133                 return ret;
134         }
135
136         ret = socfpga_dwmac_setup(dwmac);
137         if (ret) {
138                 dev_err(dev, "couldn't setup SoC glue (%d)\n", ret);
139                 return ret;
140         }
141
142         if (node) {
143                 ret = of_platform_populate(node, NULL, NULL, dev);
144                 if (ret) {
145                         dev_err(dev, "failed to add dwmac core\n");
146                         return ret;
147                 }
148         } else {
149                 dev_err(dev, "no device node, failed to add dwmac core\n");
150                 return -ENODEV;
151         }
152
153         platform_set_drvdata(pdev, dwmac);
154
155         return 0;
156 }
157
158 static int socfpga_dwmac_remove(struct platform_device *pdev)
159 {
160         return 0;
161 }
162
163 static const struct of_device_id socfpga_dwmac_match[] = {
164         { .compatible = "altr,socfpga-stmmac" },
165         {},
166 };
167 MODULE_DEVICE_TABLE(of, socfpga_dwmac_match);
168
169 static struct platform_driver socfpga_dwmac_driver = {
170         .probe          = socfpga_dwmac_probe,
171         .remove         = socfpga_dwmac_remove,
172         .driver         = {
173                 .name   = "socfpga-dwmac",
174                 .of_match_table = of_match_ptr(socfpga_dwmac_match),
175         },
176 };
177
178 module_platform_driver(socfpga_dwmac_driver);
179
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");