1 /*
2  * Synopsys DWC Ethernet Quality-of-Service v4.10a linux driver
3  *
4  * Copyright (C) 2016 Joao Pinto <jpinto@synopsys.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * You should have received a copy of the GNU General Public License
11  * along with this program. If not, see <http://www.gnu.org/licenses/>.
12  */
13 
14 #include <linux/clk.h>
15 #include <linux/clk-provider.h>
16 #include <linux/device.h>
17 #include <linux/ethtool.h>
18 #include <linux/io.h>
19 #include <linux/ioport.h>
20 #include <linux/module.h>
21 #include <linux/of_net.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/platform_device.h>
24 #include <linux/stmmac.h>
25 
26 #include "stmmac_platform.h"
27 
28 static int dwc_eth_dwmac_config_dt(struct platform_device *pdev,
29 				   struct plat_stmmacenet_data *plat_dat)
30 {
31 	struct device_node *np = pdev->dev.of_node;
32 	u32 burst_map = 0;
33 	u32 bit_index = 0;
34 	u32 a_index = 0;
35 
36 	if (!plat_dat->axi) {
37 		plat_dat->axi = kzalloc(sizeof(struct stmmac_axi), GFP_KERNEL);
38 
39 		if (!plat_dat->axi)
40 			return -ENOMEM;
41 	}
42 
43 	plat_dat->axi->axi_lpi_en = of_property_read_bool(np, "snps,en-lpi");
44 	if (of_property_read_u32(np, "snps,write-requests",
45 				 &plat_dat->axi->axi_wr_osr_lmt)) {
46 		/**
47 		 * Since the register has a reset value of 1, if property
48 		 * is missing, default to 1.
49 		 */
50 		plat_dat->axi->axi_wr_osr_lmt = 1;
51 	} else {
52 		/**
53 		 * If property exists, to keep the behavior from dwc_eth_qos,
54 		 * subtract one after parsing.
55 		 */
56 		plat_dat->axi->axi_wr_osr_lmt--;
57 	}
58 
59 	if (of_property_read_u32(np, "read,read-requests",
60 				 &plat_dat->axi->axi_rd_osr_lmt)) {
61 		/**
62 		 * Since the register has a reset value of 1, if property
63 		 * is missing, default to 1.
64 		 */
65 		plat_dat->axi->axi_rd_osr_lmt = 1;
66 	} else {
67 		/**
68 		 * If property exists, to keep the behavior from dwc_eth_qos,
69 		 * subtract one after parsing.
70 		 */
71 		plat_dat->axi->axi_rd_osr_lmt--;
72 	}
73 	of_property_read_u32(np, "snps,burst-map", &burst_map);
74 
75 	/* converts burst-map bitmask to burst array */
76 	for (bit_index = 0; bit_index < 7; bit_index++) {
77 		if (burst_map & (1 << bit_index)) {
78 			switch (bit_index) {
79 			case 0:
80 			plat_dat->axi->axi_blen[a_index] = 4; break;
81 			case 1:
82 			plat_dat->axi->axi_blen[a_index] = 8; break;
83 			case 2:
84 			plat_dat->axi->axi_blen[a_index] = 16; break;
85 			case 3:
86 			plat_dat->axi->axi_blen[a_index] = 32; break;
87 			case 4:
88 			plat_dat->axi->axi_blen[a_index] = 64; break;
89 			case 5:
90 			plat_dat->axi->axi_blen[a_index] = 128; break;
91 			case 6:
92 			plat_dat->axi->axi_blen[a_index] = 256; break;
93 			default:
94 			break;
95 			}
96 			a_index++;
97 		}
98 	}
99 
100 	/* dwc-qos needs GMAC4, AAL, TSO and PMT */
101 	plat_dat->has_gmac4 = 1;
102 	plat_dat->dma_cfg->aal = 1;
103 	plat_dat->tso_en = 1;
104 	plat_dat->pmt = 1;
105 
106 	return 0;
107 }
108 
109 static int dwc_eth_dwmac_probe(struct platform_device *pdev)
110 {
111 	struct plat_stmmacenet_data *plat_dat;
112 	struct stmmac_resources stmmac_res;
113 	struct resource *res;
114 	int ret;
115 
116 	memset(&stmmac_res, 0, sizeof(struct stmmac_resources));
117 
118 	/**
119 	 * Since stmmac_platform supports name IRQ only, basic platform
120 	 * resource initialization is done in the glue logic.
121 	 */
122 	stmmac_res.irq = platform_get_irq(pdev, 0);
123 	if (stmmac_res.irq < 0) {
124 		if (stmmac_res.irq != -EPROBE_DEFER)
125 			dev_err(&pdev->dev,
126 				"IRQ configuration information not found\n");
127 
128 		return stmmac_res.irq;
129 	}
130 	stmmac_res.wol_irq = stmmac_res.irq;
131 
132 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 	stmmac_res.addr = devm_ioremap_resource(&pdev->dev, res);
134 	if (IS_ERR(stmmac_res.addr))
135 		return PTR_ERR(stmmac_res.addr);
136 
137 	plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
138 	if (IS_ERR(plat_dat))
139 		return PTR_ERR(plat_dat);
140 
141 	plat_dat->stmmac_clk = devm_clk_get(&pdev->dev, "apb_pclk");
142 	if (IS_ERR(plat_dat->stmmac_clk)) {
143 		dev_err(&pdev->dev, "apb_pclk clock not found.\n");
144 		ret = PTR_ERR(plat_dat->stmmac_clk);
145 		plat_dat->stmmac_clk = NULL;
146 		goto err_remove_config_dt;
147 	}
148 	clk_prepare_enable(plat_dat->stmmac_clk);
149 
150 	plat_dat->pclk = devm_clk_get(&pdev->dev, "phy_ref_clk");
151 	if (IS_ERR(plat_dat->pclk)) {
152 		dev_err(&pdev->dev, "phy_ref_clk clock not found.\n");
153 		ret = PTR_ERR(plat_dat->pclk);
154 		plat_dat->pclk = NULL;
155 		goto err_out_clk_dis_phy;
156 	}
157 	clk_prepare_enable(plat_dat->pclk);
158 
159 	ret = dwc_eth_dwmac_config_dt(pdev, plat_dat);
160 	if (ret)
161 		goto err_out_clk_dis_aper;
162 
163 	ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
164 	if (ret)
165 		goto err_out_clk_dis_aper;
166 
167 	return 0;
168 
169 err_out_clk_dis_aper:
170 	clk_disable_unprepare(plat_dat->pclk);
171 err_out_clk_dis_phy:
172 	clk_disable_unprepare(plat_dat->stmmac_clk);
173 err_remove_config_dt:
174 	stmmac_remove_config_dt(pdev, plat_dat);
175 
176 	return ret;
177 }
178 
179 static int dwc_eth_dwmac_remove(struct platform_device *pdev)
180 {
181 	return stmmac_pltfr_remove(pdev);
182 }
183 
184 static const struct of_device_id dwc_eth_dwmac_match[] = {
185 	{ .compatible = "snps,dwc-qos-ethernet-4.10", },
186 	{ }
187 };
188 MODULE_DEVICE_TABLE(of, dwc_eth_dwmac_match);
189 
190 static struct platform_driver dwc_eth_dwmac_driver = {
191 	.probe  = dwc_eth_dwmac_probe,
192 	.remove = dwc_eth_dwmac_remove,
193 	.driver = {
194 		.name           = "dwc-eth-dwmac",
195 		.of_match_table = dwc_eth_dwmac_match,
196 	},
197 };
198 module_platform_driver(dwc_eth_dwmac_driver);
199 
200 MODULE_AUTHOR("Joao Pinto <jpinto@synopsys.com>");
201 MODULE_DESCRIPTION("Synopsys DWC Ethernet Quality-of-Service v4.10a driver");
202 MODULE_LICENSE("GPL v2");
203