aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/dpaa/fman_port_tx.c
blob: 708656a058d856066974b0abf908b1d836c860f1 (plain) (blame)
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
/*
 * Copyright (c) 2026 Justin Hibbits
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * FMan TX port driver.
 */

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>

#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <machine/bus.h>

#include "fman.h"
#include "fman_parser.h"
#include "fman_port.h"
#include "fman_port_var.h"
#include "fman_if.h"
#include "fman_port_if.h"

#define	TX_10G_PORT_BASE	0x30

/* BMI Tx registers. */
#define	FMBM_TCFG		0x000
#define	FMBM_TST		0x004
#define	FMBM_TDA		0x008
#define	FMBM_TFP		0x00c
#define	  BMI_FIFO_PIPELINE_DEPTH_SHIFT	12
#define	FMBM_TFED		0x010
#define	FMBM_TICP		0x014
#define	  TICP_ICEOF_M		  0x001f0000
#define	  TICP_ICEOF_S		  16
#define	  TICP_ICIOF_M		  0x00000f00
#define	  TICP_ICIOF_S		  8
#define	  TICP_ICSZ_S		  0x0000001f
#define	FMBM_TFDNE		0x018
#define	FMBM_TFCA		0x01c
#define	  TFCA_MR_DEF		  0
#define	  TFCA_ATTR_ORDER	  0x80000000
#define	FMBM_TCFQID		0x020
#define	FMBM_TEFQID		0x024
#define	FMBM_TFENE		0x028
#define	FMBM_TFNE		0x070
#define	  TFNE_EBD		  0x80000000

/* QMI dequeue-config (FMQM_PNDC) field layout: TX-only consumer. */
#define	QMI_DEQ_CFG_PRI			0x80000000
#define	QMI_DEQ_CFG_TYPE1		0x10000000
#define	QMI_DEQ_CFG_TYPE2		0x20000000
#define	QMI_DEQ_CFG_TYPE3		0x30000000
#define	QMI_DEQ_CFG_PREFETCH_PARTIAL	0x01000000
#define	QMI_DEQ_CFG_PREFETCH_FULL	0x03000000
#define	QMI_DEQ_CFG_SP_MASK		0xf
#define	QMI_DEQ_CFG_SP_SHIFT		20

struct fman_port_tx_softc {
	struct fman_port_softc sc_base;

	int sc_deq_byte_count;
	int sc_deq_high_priority;
	int sc_tx_deq_pipeline_depth;
	int sc_qman_channel_id;
};

static struct ofw_compat_data tx_compats[] = {
	{ "fsl,fman-v2-port-tx", 0 },
	{ "fsl,fman-v3-port-tx", PORT_V3 },
	{ NULL, 0 }
};

static int
fman_port_tx_probe(device_t dev)
{

	if (ofw_bus_search_compatible(dev, tx_compats)->ocd_str == NULL)
		return (ENXIO);

	device_set_desc(dev, "FMan TX port");
	return (BUS_PROBE_DEFAULT);
}

static int
fman_port_tx_attach(device_t dev)
{
	struct fman_port_tx_softc *sc;
	uintptr_t compat_data;
	int port_speed;
	int err;

	sc = device_get_softc(dev);
	compat_data = ofw_bus_search_compatible(dev, tx_compats)->ocd_data;

	port_speed = 1000;
	if ((compat_data & PORT_V3) == PORT_V3) {
		if (OF_hasprop(ofw_bus_get_node(dev), "fsl,fman-10g-port"))
			port_speed = 10000;
	} else {
		int cell = 0;
		OF_getencprop(ofw_bus_get_node(dev), "cell-index", &cell,
		    sizeof(cell));
		if (cell > TX_10G_PORT_BASE)
			port_speed = 10000;
	}

	err = fman_port_attach_common(dev, &sc->sc_base, port_speed);
	if (err != 0)
		return (err);

	if (port_speed == 10000) {
		sc->sc_deq_high_priority = true;
		sc->sc_deq_byte_count = 0x1400;
	} else {
		sc->sc_deq_high_priority = false;
		sc->sc_deq_byte_count = 0x0400;
	}

	sc->sc_qman_channel_id = FMAN_GET_QMAN_CHANNEL_ID(
	    device_get_parent(dev), sc->sc_base.sc_port_id);

	return (0);
}

static int
fman_port_tx_detach(device_t dev)
{
	struct fman_port_tx_softc *sc = device_get_softc(dev);

	return (fman_port_detach_common(dev, &sc->sc_base));
}

static int
fman_port_tx_config(device_t dev, struct fman_port_params *params)
{
	struct fman_port_tx_softc *sc = device_get_softc(dev);
	struct fman_port_softc *base = &sc->sc_base;
	bool fman_v3 = (base->sc_revision_major >= 6);

	fman_port_config_common(base, params);

	if (base->sc_revision_major >= 6 && base->sc_port_speed == 1000)
		/* Errata A005127 workaround */
		bus_write_4(base->sc_mem, FMBM_TFP, 0x00001013);

	base->sc_tasks.extra = 0;
	base->sc_open_dmas.extra = 0;
	base->sc_fifo_bufs.extra = 0;
	switch (base->sc_port_speed) {
	case 10000:
		base->sc_tasks.num = fman_v3 ? 14 : 16;
		base->sc_open_dmas.num = fman_v3 ? 12 : 8;
		base->sc_fifo_bufs.num = fman_v3 ? 64 : 48;
		break;
	case 1000:
		base->sc_tasks.num = fman_v3 ? 4 : 3;
		base->sc_open_dmas.num = fman_v3 ? 3 : 1;
		base->sc_fifo_bufs.num = fman_v3 ? 50 : 44;
		break;
	default:
		base->sc_tasks.num = 0;
		break;
	}

	base->sc_fifo_bufs.num *= FMAN_BMI_FIFO_UNITS;

	/* TODO: buf_margins?  See fman_sp_build_buffer_struct */
	return (0);
}

static int
fman_port_tx_init_bmi(struct fman_port_tx_softc *sc)
{
	struct fman_port_softc *base = &sc->sc_base;
	uint32_t reg;
	int depth;

	bus_write_4(base->sc_mem, FMBM_TCFG, 0);
	bus_write_4(base->sc_mem, FMBM_TDA, 0);
	bus_write_4(base->sc_mem, FMBM_TFED, 0);

	if (base->sc_port_speed == 10000)
		depth = 4;
	else if (base->sc_revision_major >= 6)
		depth = 2;
	else
		depth = 1;
	sc->sc_tx_deq_pipeline_depth = depth;
	reg = ((depth - 1) << BMI_FIFO_PIPELINE_DEPTH_SHIFT) | 0x13;
	bus_write_4(base->sc_mem, FMBM_TFP, reg);

	/* Default color: green */
	bus_write_4(base->sc_mem, FMBM_TFCA, TFCA_MR_DEF | TFCA_ATTR_ORDER);

	bus_write_4(base->sc_mem, FMBM_TFDNE, NIA_ENG_QMI_DEQ);
	bus_write_4(base->sc_mem, FMBM_TFENE,
	    NIA_ENG_QMI_ENQ | NIA_ORDER_RESTORE);

	/* Insert internal context ahead of the frame */
	reg = howmany(FMAN_PARSE_RESULT_OFF, 0x10) << TICP_ICIOF_S;
	reg |= howmany(sizeof(struct fman_internal_context), 0x10);
	bus_write_4(base->sc_mem, FMBM_TICP, reg);

	if (base->sc_revision_major >= 6)
		bus_write_4(base->sc_mem, FMBM_TFNE,
		    (base->sc_default_fqid == 0 ? TFNE_EBD : 0) |
		    NIA_BMI_AC_FETCH_ALLFRAME);
	bus_write_4(base->sc_mem, FMBM_TCFQID, base->sc_default_fqid);
	bus_write_4(base->sc_mem, FMBM_TEFQID, base->sc_err_fqid);

	return (0);
}

static int
fman_port_tx_init_qmi(struct fman_port_tx_softc *sc)
{
	struct fman_port_softc *base = &sc->sc_base;
	uint32_t reg;

	bus_write_4(base->sc_mem, FMQM_PNDN, NIA_ENG_BMI | NIA_BMI_AC_TX);
	bus_write_4(base->sc_mem, FMQM_PNEN,
	    NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE);

	reg = 0;
	if (sc->sc_deq_high_priority)
		reg |= QMI_DEQ_CFG_PRI;
	reg |= QMI_DEQ_CFG_TYPE1;
	reg |= QMI_DEQ_CFG_PREFETCH_FULL;
	reg |= (sc->sc_qman_channel_id & QMI_DEQ_CFG_SP_MASK) <<
	    QMI_DEQ_CFG_SP_SHIFT;
	reg |= sc->sc_deq_byte_count;
	bus_write_4(base->sc_mem, FMQM_PNDC, reg);

	return (0);
}

static int
fman_port_tx_init(device_t dev)
{
	struct fman_port_tx_softc *sc = device_get_softc(dev);
	struct fman_port_softc *base = &sc->sc_base;
	struct fman_port_init_params params;
	int err;

	params.port_id = base->sc_port_id;
	params.is_rx_port = false;
	params.num_tasks = base->sc_tasks.num;
	params.extra_tasks = base->sc_tasks.extra;
	params.open_dmas = base->sc_open_dmas.num;
	params.extra_dmas = base->sc_open_dmas.extra;
	params.fifo_size = base->sc_fifo_bufs.num;
	params.extra_fifo_size = base->sc_fifo_bufs.extra;
	params.max_frame_length = base->sc_max_frame_length;
	params.deq_pipeline_size = sc->sc_tx_deq_pipeline_depth;

	/* TODO: verify_size_of_fifo() from Linux driver */
	err = FMAN_SET_PORT_PARAMS(device_get_parent(dev), &params);
	if (err != 0)
		return (err);

	err = fman_port_tx_init_bmi(sc);
	if (err != 0)
		return (err);

	return (fman_port_tx_init_qmi(sc));
}

static int
fman_port_tx_disable(device_t dev)
{
	struct fman_port_tx_softc *sc = device_get_softc(dev);
	struct fman_port_softc *base = &sc->sc_base;
	uint32_t reg;
	int count;

	reg = bus_read_4(base->sc_mem, FMQM_PNC);
	bus_write_4(base->sc_mem, FMQM_PNC, reg & ~PNC_EN);
	for (count = 0; count < 100; count++) {
		DELAY(10);
		reg = bus_read_4(base->sc_mem, FMQM_PNS);
		if (!(reg & PNS_DEQ_FD_BSY))
			break;
	}
	if (count == 100)
		device_printf(base->sc_dev, "Timeout stopping QMI\n");

	reg = bus_read_4(base->sc_mem, FMBM_TCFG);
	bus_write_4(base->sc_mem, FMBM_TCFG, reg & ~BMI_PORT_CFG_EN);
	for (count = 0; count < 100; count++) {
		DELAY(10);
		reg = bus_read_4(base->sc_mem, FMBM_TST);
		if (!(reg & PNS_DEQ_FD_BSY))
			break;
	}
	if (count == 100)
		device_printf(base->sc_dev, "Timeout stopping BMI\n");

	return (0);
}

static int
fman_port_tx_enable(device_t dev)
{
	struct fman_port_tx_softc *sc = device_get_softc(dev);
	struct fman_port_softc *base = &sc->sc_base;
	uint32_t reg;

	reg = bus_read_4(base->sc_mem, FMQM_PNC);
	bus_write_4(base->sc_mem, FMQM_PNC, reg | PNC_EN | PNC_STEN);

	reg = bus_read_4(base->sc_mem, FMBM_TCFG);
	bus_write_4(base->sc_mem, FMBM_TCFG, reg | BMI_PORT_CFG_EN);

	return (0);
}

static device_method_t fman_port_tx_methods[] = {
	DEVMETHOD(device_probe,		fman_port_tx_probe),
	DEVMETHOD(device_attach,	fman_port_tx_attach),
	DEVMETHOD(device_detach,	fman_port_tx_detach),

	DEVMETHOD(fman_port_config,	fman_port_tx_config),
	DEVMETHOD(fman_port_init,	fman_port_tx_init),
	DEVMETHOD(fman_port_enable,	fman_port_tx_enable),
	DEVMETHOD(fman_port_disable,	fman_port_tx_disable),

	DEVMETHOD_END
};

DEFINE_CLASS_0(fman_port_tx, fman_port_tx_driver, fman_port_tx_methods,
    sizeof(struct fman_port_tx_softc));
EARLY_DRIVER_MODULE(fman_port_tx, fman, fman_port_tx_driver, 0, 0,
    BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);