summaryrefslogtreecommitdiffstats
path: root/drivers/net/designware_tegra186.c
blob: 58484d4095dc8bc2be1c104fd40d1aa79bf86a57 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2016, NVIDIA CORPORATION.
 * Copyright (c) 2019, Ahmad Fatoum, Pengutronix
 *
 * Portions based on U-Boot's rtl8169.c and dwc_eth_qos.
 */

#include <common.h>
#include <init.h>
#include <gpio.h>
#include <of_gpio.h>
#include <linux/clk.h>
#include <net.h>
#include <linux/reset.h>

#include "designware_eqos.h"

/* These registers are Tegra186-specific */
#define EQOS_TEGRA186_REGS_BASE 0x8800
struct eqos_tegra186_regs {
	uint32_t sdmemcomppadctrl;			/* 0x8800 */
	uint32_t auto_cal_config;			/* 0x8804 */
	uint32_t unused_8808;				/* 0x8808 */
	uint32_t auto_cal_status;			/* 0x880c */
};

#define EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD	BIT(31)

#define EQOS_AUTO_CAL_CONFIG_START			BIT(31)
#define EQOS_AUTO_CAL_CONFIG_ENABLE			BIT(29)

#define EQOS_AUTO_CAL_STATUS_ACTIVE			BIT(31)

struct eqos_tegra186 {
	struct clk_bulk_data *clks;
	int num_clks;
	struct reset_control	*rst;
	struct eqos_tegra186_regs __iomem *tegra186_regs;
	int phy_reset_gpio;
};

static inline struct eqos_tegra186 *to_tegra186(struct eqos *eqos)
{
	return eqos->priv;
}

enum { CLK_SLAVE_BUS, CLK_MASTER_BUS, CLK_RX, CLK_PTP_REF, CLK_TX };
static const struct clk_bulk_data tegra186_clks[] = {
	[CLK_SLAVE_BUS]  = { .id = "slave_bus" },
	[CLK_MASTER_BUS] = { .id = "master_bus" },
	[CLK_RX]         = { .id = "rx" },
	[CLK_PTP_REF]    = { .id = "ptp_ref" },
	[CLK_TX]         = { .id = "tx" },
};

static int eqos_clks_set_rate_tegra186(struct eqos_tegra186 *priv)
{
	return clk_set_rate(priv->clks[CLK_PTP_REF].clk, 125 * 1000 * 1000);
}

static int eqos_reset_tegra186(struct eqos_tegra186 *priv, bool reset)
{
	int ret;

	if (reset) {
		reset_control_assert(priv->rst);
		gpio_set_value(priv->phy_reset_gpio, 1);
		return 0;
	}

	gpio_set_value(priv->phy_reset_gpio, 1);

	udelay(2);

	gpio_set_value(priv->phy_reset_gpio, 0);

	ret = reset_control_assert(priv->rst);
	if (ret < 0)
		return ret;

	udelay(2);

	return reset_control_deassert(priv->rst);
}

static int eqos_calibrate_pads_tegra186(struct eqos *eqos)
{
	struct eqos_tegra186 *priv = to_tegra186(eqos);
	u32 active;
	int ret;

	setbits_le32(&priv->tegra186_regs->sdmemcomppadctrl,
		     EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);

	udelay(1);

	setbits_le32(&priv->tegra186_regs->auto_cal_config,
		     EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);

	ret = readl_poll_timeout(&priv->tegra186_regs->auto_cal_status, active,
				 active & EQOS_AUTO_CAL_STATUS_ACTIVE,
				 10000);
	if (ret) {
		eqos_err(eqos, "calibrate didn't start\n");
		goto failed;
	}

	ret = readl_poll_timeout(&priv->tegra186_regs->auto_cal_status, active,
				 !(active & EQOS_AUTO_CAL_STATUS_ACTIVE),
				 10000);
	if (ret) {
		eqos_err(eqos, "calibrate didn't finish\n");
		goto failed;
	}

failed:
	clrbits_le32(&priv->tegra186_regs->sdmemcomppadctrl,
		     EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);

	return ret;
}

static int eqos_calibrate_link_tegra186(struct eqos *eqos, unsigned speed)
{
	struct eqos_tegra186 *priv = to_tegra186(eqos);
	int ret = 0;
	unsigned long rate;
	bool calibrate;

	switch (speed) {
	case SPEED_1000:
		rate = 125 * 1000 * 1000;
		calibrate = true;
		break;
	case SPEED_100:
		rate = 25 * 1000 * 1000;
		calibrate = true;
		break;
	case SPEED_10:
		rate = 2.5 * 1000 * 1000;
		calibrate = false;
		break;
	default:
		return -EINVAL;
	}

	if (calibrate) {
		ret = eqos_calibrate_pads_tegra186(eqos);
		if (ret)
			return ret;
	} else {
		clrbits_le32(&priv->tegra186_regs->auto_cal_config,
			     EQOS_AUTO_CAL_CONFIG_ENABLE);
	}

	ret = clk_set_rate(priv->clks[CLK_TX].clk, rate);
	if (ret < 0) {
		eqos_err(eqos, "clk_set_rate(tx_clk, %lu) failed: %d\n", rate, ret);
		return ret;
	}

	return 0;
}

static unsigned long eqos_get_csr_clk_rate_tegra186(struct eqos *eqos)
{
	return clk_get_rate(to_tegra186(eqos)->clks[CLK_SLAVE_BUS].clk);
}

static int eqos_set_ethaddr_tegra186(struct eth_device *edev, const unsigned char *mac)
{
	struct eqos *eqos = edev->priv;

	/*
	 * This function may be called before start() or after stop(). At that
	 * time, on at least some configurations of the EQoS HW, all clocks to
	 * the EQoS HW block will be stopped, and a reset signal applied. If
	 * any register access is attempted in this state, bus timeouts or CPU
	 * hangs may occur. This check prevents that.
	 *
	 * A simple solution to this problem would be to not implement
	 * write_hwaddr(), since start() always writes the MAC address into HW
	 * anyway. However, it is desirable to implement write_hwaddr() to
	 * support the case of SW that runs subsequent to U-Boot which expects
	 * the MAC address to already be programmed into the EQoS registers,
	 * which must happen irrespective of whether the U-Boot user (or
	 * scripts) actually made use of the EQoS device, and hence
	 * irrespective of whether start() was ever called.
	 *
	 * Note that this requirement by subsequent SW is not valid for
	 * Tegra186, and is likely not valid for any non-PCI instantiation of
	 * the EQoS HW block. This function is implemented solely as
	 * future-proofing with the expectation the driver will eventually be
	 * ported to some system where the expectation above is true.
	 */

	if (!eqos->started) {
		memcpy(eqos->macaddr, mac, 6);
		return 0;
	}

	return eqos_set_ethaddr(edev, mac);
}

static int eqos_init_tegra186(struct device_d *dev, struct eqos *eqos)
{
	struct eqos_tegra186 *priv = to_tegra186(eqos);
	int phy_reset;
	int ret;

	priv->tegra186_regs = IOMEM(eqos->regs + EQOS_TEGRA186_REGS_BASE);

	priv->rst = reset_control_get(dev, "eqos");
	if (IS_ERR(priv->rst)) {
		ret = PTR_ERR(priv->rst);
		dev_err(dev, "reset_get_by_name(rst) failed: %s\n", strerror(-ret));
		return ret;
	}

	phy_reset = of_get_named_gpio(dev->device_node, "phy-reset-gpios", 0);
	if (gpio_is_valid(phy_reset)) {
		ret = gpio_request(phy_reset, "phy-reset");
		if (ret)
			goto release_res;

		priv->phy_reset_gpio = phy_reset;
	}

	priv->clks = xmemdup(tegra186_clks, sizeof(tegra186_clks));
	priv->num_clks = ARRAY_SIZE(tegra186_clks);

	return 0;

release_res:
	reset_control_put(priv->rst);
	return ret;
}

static int eqos_start_tegra186(struct eth_device *edev)
{
	struct eqos *eqos = edev->priv;
	struct eqos_tegra186 *priv = to_tegra186(eqos);
	int ret;

	ret = clk_bulk_enable(priv->num_clks, priv->clks);
	if (ret < 0) {
		eqos_err(eqos, "clk_bulk_enable() failed: %s\n", strerror(-ret));
		return ret;
	}

	ret = eqos_clks_set_rate_tegra186(priv);
	if (ret < 0) {
		eqos_err(eqos, "clks_set_rate() failed: %s\n", strerror(-ret));
		goto err;
	}

	eqos_reset_tegra186(priv, false);
	if (ret < 0) {
		eqos_err(eqos, "reset(0) failed: %s\n", strerror(-ret));
		goto err_stop_clks;
	}

	udelay(10);

	ret = eqos_start(edev);
	if (ret)
		goto err_stop_resets;

	return 0;

err_stop_resets:
	eqos_reset_tegra186(priv, true);
err_stop_clks:
	clk_bulk_disable(priv->num_clks, priv->clks);
err:
	return ret;
}


static void eqos_stop_tegra186(struct eth_device *edev)
{
	struct eqos_tegra186 *priv = to_tegra186(edev->priv);

	eqos_reset_tegra186(priv, true);

	clk_bulk_disable(priv->num_clks, priv->clks);
}

static void eqos_adjust_link_tegra186(struct eth_device *edev)
{
	struct eqos *eqos = edev->priv;
	unsigned speed = edev->phydev->speed;
	int ret;

	eqos_adjust_link(edev);

	ret = eqos_calibrate_link_tegra186(eqos, speed);
	if (ret < 0) {
		eqos_err(eqos, "eqos_calibrate_link_tegra186() failed: %d\n", ret);
		return;
	}
}

static const struct eqos_ops tegra186_ops = {
	.init = eqos_init_tegra186,
	.get_ethaddr = eqos_get_ethaddr,
	.set_ethaddr = eqos_set_ethaddr_tegra186,
	.start = eqos_start_tegra186,
	.stop = eqos_stop_tegra186,
	.adjust_link = eqos_adjust_link_tegra186,
	.get_csr_clk_rate = eqos_get_csr_clk_rate_tegra186,

	.mdio_wait_us = 10,
	.clk_csr = EQOS_MDIO_ADDR_CR_20_35,
	.config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
};

static int eqos_probe_tegra186(struct device_d *dev)
{
	return eqos_probe(dev, &tegra186_ops, xzalloc(sizeof(struct eqos_tegra186)));
}

static void eqos_remove_tegra186(struct device_d *dev)
{
	struct eqos_tegra186 *priv = to_tegra186(dev->priv);

	eqos_remove(dev);

	clk_bulk_put(priv->num_clks, priv->clks);

	gpio_free(priv->phy_reset_gpio);
	reset_control_put(priv->rst);
}

static const struct of_device_id eqos_tegra186_ids[] = {
	{ .compatible = "nvidia,tegra186-eqos" },
	{ /* sentinel */ }
};

static struct driver_d eqos_tegra186_driver = {
	.name = "eqos-tegra186",
	.probe = eqos_probe_tegra186,
	.remove	= eqos_remove_tegra186,
	.of_compatible = DRV_OF_COMPAT(eqos_tegra186_ids),
};
device_platform_driver(eqos_tegra186_driver);