summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/ti/cpsw_sl.c
blob: 0c7531cb0f3982a851153fb6eacb8806310c3c07 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Texas Instruments Ethernet Switch media-access-controller (MAC) submodule/
 * Ethernet MAC Sliver (CPGMAC_SL)
 *
 * Copyright (C) 2019 Texas Instruments
 *
 */

#include <linux/delay.h>
#include <linux/io.h>
#include <linux/kernel.h>

#include "cpsw_sl.h"

#define CPSW_SL_REG_NOTUSED U16_MAX

static const u16 cpsw_sl_reg_map_cpsw[] = {
	[CPSW_SL_IDVER] = 0x00,
	[CPSW_SL_MACCONTROL] = 0x04,
	[CPSW_SL_MACSTATUS] = 0x08,
	[CPSW_SL_SOFT_RESET] = 0x0c,
	[CPSW_SL_RX_MAXLEN] = 0x10,
	[CPSW_SL_BOFFTEST] = 0x14,
	[CPSW_SL_RX_PAUSE] = 0x18,
	[CPSW_SL_TX_PAUSE] = 0x1c,
	[CPSW_SL_EMCONTROL] = 0x20,
	[CPSW_SL_RX_PRI_MAP] = 0x24,
	[CPSW_SL_TX_GAP] = 0x28,
};

static const u16 cpsw_sl_reg_map_66ak2hk[] = {
	[CPSW_SL_IDVER] = 0x00,
	[CPSW_SL_MACCONTROL] = 0x04,
	[CPSW_SL_MACSTATUS] = 0x08,
	[CPSW_SL_SOFT_RESET] = 0x0c,
	[CPSW_SL_RX_MAXLEN] = 0x10,
	[CPSW_SL_BOFFTEST] = CPSW_SL_REG_NOTUSED,
	[CPSW_SL_RX_PAUSE] = 0x18,
	[CPSW_SL_TX_PAUSE] = 0x1c,
	[CPSW_SL_EMCONTROL] = 0x20,
	[CPSW_SL_RX_PRI_MAP] = 0x24,
	[CPSW_SL_TX_GAP] = CPSW_SL_REG_NOTUSED,
};

static const u16 cpsw_sl_reg_map_66ak2x_xgbe[] = {
	[CPSW_SL_IDVER] = 0x00,
	[CPSW_SL_MACCONTROL] = 0x04,
	[CPSW_SL_MACSTATUS] = 0x08,
	[CPSW_SL_SOFT_RESET] = 0x0c,
	[CPSW_SL_RX_MAXLEN] = 0x10,
	[CPSW_SL_BOFFTEST] = CPSW_SL_REG_NOTUSED,
	[CPSW_SL_RX_PAUSE] = 0x18,
	[CPSW_SL_TX_PAUSE] = 0x1c,
	[CPSW_SL_EMCONTROL] = 0x20,
	[CPSW_SL_RX_PRI_MAP] = CPSW_SL_REG_NOTUSED,
	[CPSW_SL_TX_GAP] = 0x28,
};

static const u16 cpsw_sl_reg_map_66ak2elg_am65[] = {
	[CPSW_SL_IDVER] = CPSW_SL_REG_NOTUSED,
	[CPSW_SL_MACCONTROL] = 0x00,
	[CPSW_SL_MACSTATUS] = 0x04,
	[CPSW_SL_SOFT_RESET] = 0x08,
	[CPSW_SL_RX_MAXLEN] = CPSW_SL_REG_NOTUSED,
	[CPSW_SL_BOFFTEST] = 0x0c,
	[CPSW_SL_RX_PAUSE] = 0x10,
	[CPSW_SL_TX_PAUSE] = 0x40,
	[CPSW_SL_EMCONTROL] = 0x70,
	[CPSW_SL_RX_PRI_MAP] = CPSW_SL_REG_NOTUSED,
	[CPSW_SL_TX_GAP] = 0x74,
};

#define CPSW_SL_SOFT_RESET_BIT		BIT(0)

#define CPSW_SL_STATUS_PN_IDLE		BIT(31)
#define CPSW_SL_AM65_STATUS_PN_E_IDLE	BIT(30)
#define CPSW_SL_AM65_STATUS_PN_P_IDLE	BIT(29)
#define CPSW_SL_AM65_STATUS_PN_TX_IDLE	BIT(28)

#define CPSW_SL_STATUS_IDLE_MASK_BASE (CPSW_SL_STATUS_PN_IDLE)

#define CPSW_SL_STATUS_IDLE_MASK_K3 \
	(CPSW_SL_STATUS_IDLE_MASK_BASE | CPSW_SL_AM65_STATUS_PN_E_IDLE | \
	 CPSW_SL_AM65_STATUS_PN_P_IDLE | CPSW_SL_AM65_STATUS_PN_TX_IDLE)

#define CPSW_SL_CTL_FUNC_BASE \
	(CPSW_SL_CTL_FULLDUPLEX |\
	CPSW_SL_CTL_LOOPBACK |\
	CPSW_SL_CTL_RX_FLOW_EN |\
	CPSW_SL_CTL_TX_FLOW_EN |\
	CPSW_SL_CTL_GMII_EN |\
	CPSW_SL_CTL_TX_PACE |\
	CPSW_SL_CTL_GIG |\
	CPSW_SL_CTL_CMD_IDLE |\
	CPSW_SL_CTL_IFCTL_A |\
	CPSW_SL_CTL_IFCTL_B |\
	CPSW_SL_CTL_GIG_FORCE |\
	CPSW_SL_CTL_EXT_EN |\
	CPSW_SL_CTL_RX_CEF_EN |\
	CPSW_SL_CTL_RX_CSF_EN |\
	CPSW_SL_CTL_RX_CMF_EN)

struct cpsw_sl {
	struct device *dev;
	void __iomem *sl_base;
	const u16 *regs;
	u32 control_features;
	u32 idle_mask;
};

struct cpsw_sl_dev_id {
	const char *device_id;
	const u16 *regs;
	const u32 control_features;
	const u32 regs_offset;
	const u32 idle_mask;
};

static const struct cpsw_sl_dev_id cpsw_sl_id_match[] = {
	{
		.device_id = "cpsw",
		.regs = cpsw_sl_reg_map_cpsw,
		.control_features = CPSW_SL_CTL_FUNC_BASE |
				    CPSW_SL_CTL_MTEST |
				    CPSW_SL_CTL_TX_SHORT_GAP_EN |
				    CPSW_SL_CTL_TX_SG_LIM_EN,
		.idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE,
	},
	{
		.device_id = "66ak2hk",
		.regs = cpsw_sl_reg_map_66ak2hk,
		.control_features = CPSW_SL_CTL_FUNC_BASE |
				    CPSW_SL_CTL_TX_SHORT_GAP_EN,
		.idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE,
	},
	{
		.device_id = "66ak2x_xgbe",
		.regs = cpsw_sl_reg_map_66ak2x_xgbe,
		.control_features = CPSW_SL_CTL_FUNC_BASE |
				    CPSW_SL_CTL_XGIG |
				    CPSW_SL_CTL_TX_SHORT_GAP_EN |
				    CPSW_SL_CTL_CRC_TYPE |
				    CPSW_SL_CTL_XGMII_EN,
		.idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE,
	},
	{
		.device_id = "66ak2el",
		.regs = cpsw_sl_reg_map_66ak2elg_am65,
		.regs_offset = 0x330,
		.control_features = CPSW_SL_CTL_FUNC_BASE |
				    CPSW_SL_CTL_MTEST |
				    CPSW_SL_CTL_TX_SHORT_GAP_EN |
				    CPSW_SL_CTL_CRC_TYPE |
				    CPSW_SL_CTL_EXT_EN_RX_FLO |
				    CPSW_SL_CTL_EXT_EN_TX_FLO |
				    CPSW_SL_CTL_TX_SG_LIM_EN,
		.idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE,
	},
	{
		.device_id = "66ak2g",
		.regs = cpsw_sl_reg_map_66ak2elg_am65,
		.regs_offset = 0x330,
		.control_features = CPSW_SL_CTL_FUNC_BASE |
				    CPSW_SL_CTL_MTEST |
				    CPSW_SL_CTL_CRC_TYPE |
				    CPSW_SL_CTL_EXT_EN_RX_FLO |
				    CPSW_SL_CTL_EXT_EN_TX_FLO,
	},
	{
		.device_id = "am65",
		.regs = cpsw_sl_reg_map_66ak2elg_am65,
		.regs_offset = 0x330,
		.control_features = CPSW_SL_CTL_FUNC_BASE |
				    CPSW_SL_CTL_MTEST |
				    CPSW_SL_CTL_XGIG |
				    CPSW_SL_CTL_TX_SHORT_GAP_EN |
				    CPSW_SL_CTL_CRC_TYPE |
				    CPSW_SL_CTL_XGMII_EN |
				    CPSW_SL_CTL_EXT_EN_RX_FLO |
				    CPSW_SL_CTL_EXT_EN_TX_FLO |
				    CPSW_SL_CTL_TX_SG_LIM_EN |
				    CPSW_SL_CTL_EXT_EN_XGIG,
		.idle_mask = CPSW_SL_STATUS_IDLE_MASK_K3,
	},
	{ },
};

u32 cpsw_sl_reg_read(struct cpsw_sl *sl, enum cpsw_sl_regs reg)
{
	int val;

	if (sl->regs[reg] == CPSW_SL_REG_NOTUSED) {
		dev_err(sl->dev, "cpsw_sl: not sup r reg: %04X\n",
			sl->regs[reg]);
		return 0;
	}

	val = readl(sl->sl_base + sl->regs[reg]);
	dev_dbg(sl->dev, "cpsw_sl: reg: %04X r 0x%08X\n", sl->regs[reg], val);
	return val;
}

void cpsw_sl_reg_write(struct cpsw_sl *sl, enum cpsw_sl_regs reg, u32 val)
{
	if (sl->regs[reg] == CPSW_SL_REG_NOTUSED) {
		dev_err(sl->dev, "cpsw_sl: not sup w reg: %04X\n",
			sl->regs[reg]);
		return;
	}

	dev_dbg(sl->dev, "cpsw_sl: reg: %04X w 0x%08X\n", sl->regs[reg], val);
	writel(val, sl->sl_base + sl->regs[reg]);
}

static const struct cpsw_sl_dev_id *cpsw_sl_match_id(
		const struct cpsw_sl_dev_id *id,
		const char *device_id)
{
	if (!id || !device_id)
		return NULL;

	while (id->device_id) {
		if (strcmp(device_id, id->device_id) == 0)
			return id;
		id++;
	}
	return NULL;
}

struct cpsw_sl *cpsw_sl_get(const char *device_id, struct device *dev,
			    void __iomem *sl_base)
{
	const struct cpsw_sl_dev_id *sl_dev_id;
	struct cpsw_sl *sl;

	sl = devm_kzalloc(dev, sizeof(struct cpsw_sl), GFP_KERNEL);
	if (!sl)
		return ERR_PTR(-ENOMEM);
	sl->dev = dev;
	sl->sl_base = sl_base;

	sl_dev_id = cpsw_sl_match_id(cpsw_sl_id_match, device_id);
	if (!sl_dev_id) {
		dev_err(sl->dev, "cpsw_sl: dev_id %s not found.\n", device_id);
		return ERR_PTR(-EINVAL);
	}
	sl->regs = sl_dev_id->regs;
	sl->control_features = sl_dev_id->control_features;
	sl->idle_mask = sl_dev_id->idle_mask;
	sl->sl_base += sl_dev_id->regs_offset;

	return sl;
}

void cpsw_sl_reset(struct cpsw_sl *sl, unsigned long tmo)
{
	unsigned long timeout = jiffies + msecs_to_jiffies(tmo);

	/* Set the soft reset bit */
	cpsw_sl_reg_write(sl, CPSW_SL_SOFT_RESET, CPSW_SL_SOFT_RESET_BIT);

	/* Wait for the bit to clear */
	do {
		usleep_range(100, 200);
	} while ((cpsw_sl_reg_read(sl, CPSW_SL_SOFT_RESET) &
		  CPSW_SL_SOFT_RESET_BIT) &&
		  time_after(timeout, jiffies));

	if (cpsw_sl_reg_read(sl, CPSW_SL_SOFT_RESET) & CPSW_SL_SOFT_RESET_BIT)
		dev_err(sl->dev, "cpsw_sl failed to soft-reset.\n");
}

u32 cpsw_sl_ctl_set(struct cpsw_sl *sl, u32 ctl_funcs)
{
	u32 val;

	if (ctl_funcs & ~sl->control_features) {
		dev_err(sl->dev, "cpsw_sl: unsupported func 0x%08X\n",
			ctl_funcs & (~sl->control_features));
		return -EINVAL;
	}

	val = cpsw_sl_reg_read(sl, CPSW_SL_MACCONTROL);
	val |= ctl_funcs;
	cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, val);

	return 0;
}

u32 cpsw_sl_ctl_clr(struct cpsw_sl *sl, u32 ctl_funcs)
{
	u32 val;

	if (ctl_funcs & ~sl->control_features) {
		dev_err(sl->dev, "cpsw_sl: unsupported func 0x%08X\n",
			ctl_funcs & (~sl->control_features));
		return -EINVAL;
	}

	val = cpsw_sl_reg_read(sl, CPSW_SL_MACCONTROL);
	val &= ~ctl_funcs;
	cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, val);

	return 0;
}

void cpsw_sl_ctl_reset(struct cpsw_sl *sl)
{
	cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, 0);
}

int cpsw_sl_wait_for_idle(struct cpsw_sl *sl, unsigned long tmo)
{
	unsigned long timeout = jiffies + msecs_to_jiffies(tmo);

	do {
		usleep_range(100, 200);
	} while (!(cpsw_sl_reg_read(sl, CPSW_SL_MACSTATUS) &
		  sl->idle_mask) && time_after(timeout, jiffies));

	if (!(cpsw_sl_reg_read(sl, CPSW_SL_MACSTATUS) & sl->idle_mask)) {
		dev_err(sl->dev, "cpsw_sl failed to soft-reset.\n");
		return -ETIMEDOUT;
	}

	return 0;
}