summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/stmicro/stmmac/dwmac-ingenic.c
blob: 8e4a30c11db06aa44d102542424df0ff874142a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-License-Identifier: GPL-2.0
/*
 * dwmac-ingenic.c - Ingenic SoCs DWMAC specific glue layer
 *
 * Copyright (c) 2021 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
 */

#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/kernel.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_net.h>
#include <linux/phy.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/stmmac.h>

#include "stmmac_platform.h"

#define MACPHYC_TXCLK_SEL_MASK		GENMASK(31, 31)
#define MACPHYC_TXCLK_SEL_OUTPUT	0x1
#define MACPHYC_TXCLK_SEL_INPUT		0x0
#define MACPHYC_MODE_SEL_MASK		GENMASK(31, 31)
#define MACPHYC_MODE_SEL_RMII		0x0
#define MACPHYC_TX_SEL_MASK			GENMASK(19, 19)
#define MACPHYC_TX_SEL_ORIGIN		0x0
#define MACPHYC_TX_SEL_DELAY		0x1
#define MACPHYC_TX_DELAY_MASK		GENMASK(18, 12)
#define MACPHYC_RX_SEL_MASK			GENMASK(11, 11)
#define MACPHYC_RX_SEL_ORIGIN		0x0
#define MACPHYC_RX_SEL_DELAY		0x1
#define MACPHYC_RX_DELAY_MASK		GENMASK(10, 4)
#define MACPHYC_SOFT_RST_MASK		GENMASK(3, 3)
#define MACPHYC_PHY_INFT_MASK		GENMASK(2, 0)

#define MACPHYC_TX_DELAY_PS_MAX		2496
#define MACPHYC_TX_DELAY_PS_MIN		20

#define MACPHYC_RX_DELAY_PS_MAX		2496
#define MACPHYC_RX_DELAY_PS_MIN		20

enum ingenic_mac_version {
	ID_JZ4775,
	ID_X1000,
	ID_X1600,
	ID_X1830,
	ID_X2000,
};

struct ingenic_mac {
	const struct ingenic_soc_info *soc_info;
	struct plat_stmmacenet_data *plat_dat;
	struct device *dev;
	struct regmap *regmap;

	int rx_delay;
	int tx_delay;
};

struct ingenic_soc_info {
	enum ingenic_mac_version version;
	u32 mask;

	int (*set_mode)(struct ingenic_mac *mac, u8 phy_intf_sel);

	u8 valid_phy_intf_sel;
};

static int jz4775_mac_set_mode(struct ingenic_mac *mac, u8 phy_intf_sel)
{
	unsigned int val;

	val = FIELD_PREP(MACPHYC_PHY_INFT_MASK, phy_intf_sel) |
	      FIELD_PREP(MACPHYC_TXCLK_SEL_MASK, MACPHYC_TXCLK_SEL_INPUT);

	/* Update MAC PHY control register */
	return regmap_update_bits(mac->regmap, 0, mac->soc_info->mask, val);
}

static int x1000_mac_set_mode(struct ingenic_mac *mac, u8 phy_intf_sel)
{
	/* Update MAC PHY control register */
	return regmap_update_bits(mac->regmap, 0, mac->soc_info->mask, 0);
}

static int x1600_mac_set_mode(struct ingenic_mac *mac, u8 phy_intf_sel)
{
	unsigned int val;

	val = FIELD_PREP(MACPHYC_PHY_INFT_MASK, phy_intf_sel);

	/* Update MAC PHY control register */
	return regmap_update_bits(mac->regmap, 0, mac->soc_info->mask, val);
}

static int x1830_mac_set_mode(struct ingenic_mac *mac, u8 phy_intf_sel)
{
	unsigned int val;

	val = FIELD_PREP(MACPHYC_MODE_SEL_MASK, MACPHYC_MODE_SEL_RMII) |
	      FIELD_PREP(MACPHYC_PHY_INFT_MASK, phy_intf_sel);

	/* Update MAC PHY control register */
	return regmap_update_bits(mac->regmap, 0, mac->soc_info->mask, val);
}

static int x2000_mac_set_mode(struct ingenic_mac *mac, u8 phy_intf_sel)
{
	unsigned int val;

	val = FIELD_PREP(MACPHYC_PHY_INFT_MASK, phy_intf_sel);

	if (phy_intf_sel == PHY_INTF_SEL_RMII) {
		val |= FIELD_PREP(MACPHYC_TX_SEL_MASK, MACPHYC_TX_SEL_ORIGIN) |
		       FIELD_PREP(MACPHYC_RX_SEL_MASK, MACPHYC_RX_SEL_ORIGIN);
	} else if (phy_intf_sel == PHY_INTF_SEL_RGMII) {
		if (mac->tx_delay == 0)
			val |= FIELD_PREP(MACPHYC_TX_SEL_MASK, MACPHYC_TX_SEL_ORIGIN);
		else
			val |= FIELD_PREP(MACPHYC_TX_SEL_MASK, MACPHYC_TX_SEL_DELAY) |
			       FIELD_PREP(MACPHYC_TX_DELAY_MASK, (mac->tx_delay + 9750) / 19500 - 1);

		if (mac->rx_delay == 0)
			val |= FIELD_PREP(MACPHYC_RX_SEL_MASK, MACPHYC_RX_SEL_ORIGIN);
		else
			val |= FIELD_PREP(MACPHYC_RX_SEL_MASK, MACPHYC_RX_SEL_DELAY) |
				   FIELD_PREP(MACPHYC_RX_DELAY_MASK, (mac->rx_delay + 9750) / 19500 - 1);
	}

	/* Update MAC PHY control register */
	return regmap_update_bits(mac->regmap, 0, mac->soc_info->mask, val);
}

static int ingenic_set_phy_intf_sel(void *bsp_priv, u8 phy_intf_sel)
{
	struct ingenic_mac *mac = bsp_priv;

	if (!mac->soc_info->set_mode)
		return 0;

	if (phy_intf_sel >= BITS_PER_BYTE ||
	    ~mac->soc_info->valid_phy_intf_sel & BIT(phy_intf_sel))
		return -EINVAL;

	dev_dbg(mac->dev, "MAC PHY control register: interface %s\n",
		phy_modes(mac->plat_dat->phy_interface));

	return mac->soc_info->set_mode(mac, phy_intf_sel);
}

static int ingenic_mac_probe(struct platform_device *pdev)
{
	struct plat_stmmacenet_data *plat_dat;
	struct stmmac_resources stmmac_res;
	struct ingenic_mac *mac;
	const struct ingenic_soc_info *data;
	u32 tx_delay_ps, rx_delay_ps;
	int ret;

	ret = stmmac_get_platform_resources(pdev, &stmmac_res);
	if (ret)
		return ret;

	plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
	if (IS_ERR(plat_dat))
		return PTR_ERR(plat_dat);

	mac = devm_kzalloc(&pdev->dev, sizeof(*mac), GFP_KERNEL);
	if (!mac)
		return -ENOMEM;

	data = of_device_get_match_data(&pdev->dev);
	if (!data) {
		dev_err(&pdev->dev, "No of match data provided\n");
		return -EINVAL;
	}

	/* Get MAC PHY control register */
	mac->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "mode-reg");
	if (IS_ERR(mac->regmap)) {
		dev_err(&pdev->dev, "%s: Failed to get syscon regmap\n", __func__);
		return PTR_ERR(mac->regmap);
	}

	if (!of_property_read_u32(pdev->dev.of_node, "tx-clk-delay-ps", &tx_delay_ps)) {
		if (tx_delay_ps >= MACPHYC_TX_DELAY_PS_MIN &&
			tx_delay_ps <= MACPHYC_TX_DELAY_PS_MAX) {
			mac->tx_delay = tx_delay_ps * 1000;
		} else {
			dev_err(&pdev->dev, "Invalid TX clock delay: %dps\n", tx_delay_ps);
			return -EINVAL;
		}
	}

	if (!of_property_read_u32(pdev->dev.of_node, "rx-clk-delay-ps", &rx_delay_ps)) {
		if (rx_delay_ps >= MACPHYC_RX_DELAY_PS_MIN &&
			rx_delay_ps <= MACPHYC_RX_DELAY_PS_MAX) {
			mac->rx_delay = rx_delay_ps * 1000;
		} else {
			dev_err(&pdev->dev, "Invalid RX clock delay: %dps\n", rx_delay_ps);
			return -EINVAL;
		}
	}

	mac->soc_info = data;
	mac->dev = &pdev->dev;
	mac->plat_dat = plat_dat;

	plat_dat->bsp_priv = mac;
	plat_dat->set_phy_intf_sel = ingenic_set_phy_intf_sel;

	return devm_stmmac_pltfr_probe(pdev, plat_dat, &stmmac_res);
}

static struct ingenic_soc_info jz4775_soc_info = {
	.version = ID_JZ4775,
	.mask = MACPHYC_TXCLK_SEL_MASK | MACPHYC_SOFT_RST_MASK | MACPHYC_PHY_INFT_MASK,

	.set_mode = jz4775_mac_set_mode,
	.valid_phy_intf_sel = BIT(PHY_INTF_SEL_GMII_MII) |
			      BIT(PHY_INTF_SEL_RGMII) |
			      BIT(PHY_INTF_SEL_RMII),
};

static struct ingenic_soc_info x1000_soc_info = {
	.version = ID_X1000,
	.mask = MACPHYC_SOFT_RST_MASK,

	.set_mode = x1000_mac_set_mode,
	.valid_phy_intf_sel = BIT(PHY_INTF_SEL_RMII),
};

static struct ingenic_soc_info x1600_soc_info = {
	.version = ID_X1600,
	.mask = MACPHYC_SOFT_RST_MASK | MACPHYC_PHY_INFT_MASK,

	.set_mode = x1600_mac_set_mode,
	.valid_phy_intf_sel = BIT(PHY_INTF_SEL_RMII),
};

static struct ingenic_soc_info x1830_soc_info = {
	.version = ID_X1830,
	.mask = MACPHYC_MODE_SEL_MASK | MACPHYC_SOFT_RST_MASK | MACPHYC_PHY_INFT_MASK,

	.set_mode = x1830_mac_set_mode,
	.valid_phy_intf_sel = BIT(PHY_INTF_SEL_RMII),
};

static struct ingenic_soc_info x2000_soc_info = {
	.version = ID_X2000,
	.mask = MACPHYC_TX_SEL_MASK | MACPHYC_TX_DELAY_MASK | MACPHYC_RX_SEL_MASK |
			MACPHYC_RX_DELAY_MASK | MACPHYC_SOFT_RST_MASK | MACPHYC_PHY_INFT_MASK,

	.set_mode = x2000_mac_set_mode,
	.valid_phy_intf_sel = BIT(PHY_INTF_SEL_RGMII) |
			      BIT(PHY_INTF_SEL_RMII),
};

static const struct of_device_id ingenic_mac_of_matches[] = {
	{ .compatible = "ingenic,jz4775-mac", .data = &jz4775_soc_info },
	{ .compatible = "ingenic,x1000-mac", .data = &x1000_soc_info },
	{ .compatible = "ingenic,x1600-mac", .data = &x1600_soc_info },
	{ .compatible = "ingenic,x1830-mac", .data = &x1830_soc_info },
	{ .compatible = "ingenic,x2000-mac", .data = &x2000_soc_info },
	{ }
};
MODULE_DEVICE_TABLE(of, ingenic_mac_of_matches);

static struct platform_driver ingenic_mac_driver = {
	.probe		= ingenic_mac_probe,
	.driver		= {
		.name	= "ingenic-mac",
		.pm		= &stmmac_pltfr_pm_ops,
		.of_match_table = ingenic_mac_of_matches,
	},
};
module_platform_driver(ingenic_mac_driver);

MODULE_AUTHOR("周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>");
MODULE_DESCRIPTION("Ingenic SoCs DWMAC specific glue layer");
MODULE_LICENSE("GPL v2");