Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* 	$NetBSD: design_gsrd1.c,v 1.8 2022/02/17 00:54:51 riastradh Exp $ */

/*
 * Copyright (c) 2006 Jachym Holecek
 * All rights reserved.
 *
 * Written for DFC Design, s.r.o.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: design_gsrd1.c,v 1.8 2022/02/17 00:54:51 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/cpu.h>
#include <sys/bus.h>
#include <sys/intr.h>

#include <powerpc/ibm4xx/cpu.h>
#include <powerpc/ibm4xx/dev/plbvar.h>

#include <evbppc/virtex/dev/xcvbusvar.h>

#include <evbppc/virtex/dev/xlcomreg.h>
#include <evbppc/virtex/dev/cdmacreg.h>
#include <evbppc/virtex/dev/temacreg.h>
#include <evbppc/virtex/dev/tftreg.h>

#include <evbppc/virtex/virtex.h>
#include <evbppc/virtex/dcr.h>


#define DCR_CDMAC_BASE 		0x0140
#define DCR_XLCOM_BASE 		0x0000
#define DCR_TEMAC_BASE 		0x0030
#define DCR_LLFB_BASE 		0x0080

#define CDMAC_TX0_STAT 		CDMAC_STAT_BASE(0)
#define CDMAC_RX0_STAT 		CDMAC_STAT_BASE(1)
#define CDMAC_TX1_STAT 		CDMAC_STAT_BASE(2)
#define CDMAC_RX1_STAT 		CDMAC_STAT_BASE(3)

#define CDMAC_TX0_BASE 		CDMAC_CTRL_BASE(0)
#define CDMAC_RX0_BASE 		CDMAC_CTRL_BASE(1)
#define CDMAC_TX1_BASE 		CDMAC_CTRL_BASE(2)
#define CDMAC_RX1_BASE 		CDMAC_CTRL_BASE(3)

#define CDMAC_INTR_LINE 	2
#define CDMAC_NCHAN 		4

#define IPL_CDMAC 		IPL_NET
#define splcdmac() 		splnet()


/*
 * CDMAC per-channel interrupt handler. CDMAC has only one interrupt signal
 * shared by all channels on GSRD, so we have to dispatch channels manually.
 *
 * Note: we hardwire priority to IPL_NET, temac(4) is the only device that
 * needs to service DMA interrupts anyway.
 */
struct cdmac_intr_handle {
	void 			(*cih_func)(void *);
	void 			*cih_arg;
};

static void 			*cdmac_ih = NULL; 	/* real CDMAC intr */
static struct cdmac_intr_handle *cdmac_intrs[CDMAC_NCHAN];


/*
 * DCR bus space leaf access routines.
 */

static void
xlcom0_write_4(bus_space_tag_t t, bus_space_handle_t h, uint32_t addr,
    uint32_t val)
{
	addr += h;

	switch (addr) {
	WCASE(DCR_XLCOM_BASE, XLCOM_TX_FIFO);
	WCASE(DCR_XLCOM_BASE, XLCOM_STAT);
	WCASE(DCR_XLCOM_BASE, XLCOM_CNTL);
	WDEAD(addr);
	}
}

static uint32_t
xlcom0_read_4(bus_space_tag_t t, bus_space_handle_t h, uint32_t addr)
{
	uint32_t 		val;

	addr += h;

	switch (addr) {
	RCASE(DCR_XLCOM_BASE, XLCOM_RX_FIFO);
	RCASE(DCR_XLCOM_BASE, XLCOM_STAT);
	RCASE(DCR_XLCOM_BASE, XLCOM_CNTL);
	RDEAD(addr);
	}

	return (val);
}

static void
tft0_write_4(bus_space_tag_t t, bus_space_handle_t h, uint32_t addr,
    uint32_t val)
{
	addr += h;

	switch (addr) {
	WCASE(DCR_LLFB_BASE, TFT_CTRL);
	WDEAD(addr);
	}
}

static uint32_t
tft0_read_4(bus_space_tag_t t, bus_space_handle_t h, uint32_t addr)
{
	uint32_t 		val;

	addr += h;

	switch (addr) {
	RCASE(DCR_LLFB_BASE, TFT_CTRL);
	RDEAD(addr);
	}

	return (val);
}

#define DOCHAN(op, channel) \
	op(DCR_CDMAC_BASE, channel + CDMAC_NEXT); 	\
	op(DCR_CDMAC_BASE, channel + CDMAC_CURADDR); 	\
	op(DCR_CDMAC_BASE, channel + CDMAC_CURSIZE); 	\
	op(DCR_CDMAC_BASE, channel + CDMAC_CURDESC)

static void
cdmac0_write_4(bus_space_tag_t t, bus_space_handle_t h, uint32_t addr,
    uint32_t val)
{
	addr += h;

	switch (addr) {
	WCASE(DCR_CDMAC_BASE, CDMAC_INTR);
	WCASE(DCR_CDMAC_BASE, CDMAC_TX0_STAT);
	WCASE(DCR_CDMAC_BASE, CDMAC_RX0_STAT);
	WCASE(DCR_CDMAC_BASE, CDMAC_TX1_STAT);
	WCASE(DCR_CDMAC_BASE, CDMAC_RX1_STAT);
	DOCHAN(WCASE, CDMAC_TX0_BASE);
	DOCHAN(WCASE, CDMAC_RX0_BASE);
	DOCHAN(WCASE, CDMAC_TX1_BASE);
	DOCHAN(WCASE, CDMAC_RX1_BASE);
	WDEAD(addr);
	}
}

static uint32_t
cdmac0_read_4(bus_space_tag_t t, bus_space_handle_t h, uint32_t addr)
{
	uint32_t 		val;

	addr += h;

	switch (addr) {
	RCASE(DCR_CDMAC_BASE, CDMAC_INTR);
	RCASE(DCR_CDMAC_BASE, CDMAC_TX0_STAT);
	RCASE(DCR_CDMAC_BASE, CDMAC_RX0_STAT);
	RCASE(DCR_CDMAC_BASE, CDMAC_TX1_STAT);
	RCASE(DCR_CDMAC_BASE, CDMAC_RX1_STAT);
	DOCHAN(RCASE, CDMAC_TX0_BASE);
	DOCHAN(RCASE, CDMAC_RX0_BASE);
	DOCHAN(RCASE, CDMAC_TX1_BASE);
	DOCHAN(RCASE, CDMAC_RX1_BASE);
	RDEAD(addr);
	}

	return (val);
}

#undef DOCHAN

static void
temac0_write_4(bus_space_tag_t t, bus_space_handle_t h, uint32_t addr,
    uint32_t val)
{
	addr += h;

	switch (addr) {
	WCASE(DCR_TEMAC_BASE, TEMAC_RESET);
	WDEAD(addr);
	}
}

static const struct powerpc_bus_space xlcom_bst = {
	DCR_BST_BODY(DCR_XLCOM_BASE, xlcom0_read_4, xlcom0_write_4)
};

static const struct powerpc_bus_space cdmac_bst = {
	DCR_BST_BODY(DCR_CDMAC_BASE, cdmac0_read_4, cdmac0_write_4)
};

static const struct powerpc_bus_space temac_bst = {
	DCR_BST_BODY(DCR_TEMAC_BASE, NULL, temac0_write_4)
};

static const struct powerpc_bus_space tft_bst = {
	DCR_BST_BODY(DCR_LLFB_BASE, tft0_read_4, tft0_write_4)
};

/*
 * Master device configuration table for GSRD design.
 */
static const struct gsrddev {
	const char 		*gdv_name;
	const char 		*gdv_attr;
	bus_space_tag_t 	gdv_bst;
	bus_addr_t 		gdv_addr;
	int 			gdv_intr;
	int 			gdv_rx_dma;
	int 			gdv_tx_dma;
} gsrd_devices[] = {
	{			/* gsrd_devices[0] */
		.gdv_name 	= "xlcom",
		.gdv_attr 	= "xcvbus",
		.gdv_bst 	= &xlcom_bst,
		.gdv_addr 	= 0,
		.gdv_intr 	= 0,
		.gdv_rx_dma 	= -1,
		.gdv_tx_dma 	= -1,
	},
	{			/* gsrd_devices[1] */
		.gdv_name 	= "temac",
		.gdv_attr 	= "xcvbus",
		.gdv_bst 	= &temac_bst,
		.gdv_addr 	= 0,
		.gdv_intr 	= 1,
		.gdv_rx_dma 	= 3,
		.gdv_tx_dma 	= 2,
	},
	{			/* gsrd_devices[2] */
		.gdv_name 	= "tft",
		.gdv_attr 	= "llbus",
		.gdv_bst 	= &tft_bst,
		.gdv_addr 	= 0,
		.gdv_intr 	= -1,
		.gdv_rx_dma 	= -1,
		.gdv_tx_dma 	= 0,
	}
};

static struct ll_dmac *
virtex_mpmc_mapdma(int n, struct ll_dmac *chan)
{
	if (n == -1)
		return (NULL);

	chan->dmac_iot = &cdmac_bst;
	chan->dmac_ctrl_addr = CDMAC_CTRL_BASE(n);
	chan->dmac_stat_addr = CDMAC_STAT_BASE(n);
	chan->dmac_chan = n;

	return (chan);
}

static int
cdmac_intr(void *arg)
{
	uint32_t 		isr;
	int 			i;
	int 			did = 0;

	isr = bus_space_read_4(&cdmac_bst, 0, CDMAC_INTR);
	bus_space_write_4(&cdmac_bst, 0, CDMAC_INTR, isr); 	/* ack */

	for (i = 0; i < CDMAC_NCHAN; i++)
		if (ISSET(isr, CDMAC_CHAN_INTR(i)) &&
		    cdmac_intrs[i] != NULL) {
			(cdmac_intrs[i]->cih_func)(cdmac_intrs[i]->cih_arg);
			did++;
		}

	/* XXX: This happens all the time under load... bug? */
#if 0
	if (did == 0)
		aprint_normal("WARNING: stray cdmac isr 0x%x\n", isr);
#endif

	return (0);
}

/*
 * Public interface.
 */

void
virtex_autoconf(device_t self, struct plb_attach_args *paa)
{
	struct xcvbus_attach_args 	vaa;
	struct ll_dmac 			rx, tx;
	int 				i;

	/* Reset all CDMAC engines, disable interrupt. */
	bus_space_write_4(&cdmac_bst, 0, CDMAC_STAT_BASE(0), CDMAC_STAT_RESET);
	bus_space_write_4(&cdmac_bst, 0, CDMAC_STAT_BASE(1), CDMAC_STAT_RESET);
	bus_space_write_4(&cdmac_bst, 0, CDMAC_STAT_BASE(2), CDMAC_STAT_RESET);
	bus_space_write_4(&cdmac_bst, 0, CDMAC_STAT_BASE(3), CDMAC_STAT_RESET);
	bus_space_write_4(&cdmac_bst, 0, CDMAC_INTR, 0);

	vaa.vaa_dmat = paa->plb_dmat;
	vaa._vaa_is_dcr = 1; 		/* XXX bst flag */

	/* Attach all we have. */
	for (i = 0; i < __arraycount(gsrd_devices); i++) {
		const struct gsrddev 	*g = &gsrd_devices[i];

		vaa.vaa_name 	= g->gdv_name;
		vaa.vaa_addr 	= g->gdv_addr;
		vaa.vaa_intr 	= g->gdv_intr;
		vaa.vaa_iot 	= g->gdv_bst;

		vaa.vaa_rx_dmac = virtex_mpmc_mapdma(g->gdv_rx_dma, &rx);
		vaa.vaa_tx_dmac = virtex_mpmc_mapdma(g->gdv_tx_dma, &tx);

		config_found(self, &vaa, xcvbus_print,
		    CFARGS(.iattr = g->gdv_attr));
	}

	/* Setup the dispatch handler. */
	cdmac_ih = intr_establish(CDMAC_INTR_LINE, IST_LEVEL, IPL_CDMAC,
	    cdmac_intr, NULL);
	if (cdmac_ih == NULL)
		panic("virtex_autoconf: could not establish cdmac intr");

	/* Enable CDMAC interrupt. */
	bus_space_write_4(&cdmac_bst, 0, CDMAC_INTR, ~CDMAC_INTR_MIE);
	bus_space_write_4(&cdmac_bst, 0, CDMAC_INTR, CDMAC_INTR_MIE);
}

void *
ll_dmac_intr_establish(int chan, void (*func)(void *), void *arg)
{
	struct cdmac_intr_handle *ih;

	KASSERT(chan > 0 && chan < CDMAC_NCHAN);

	/* We only allow one handler per channel, somewhat arbitrarily. */
	if (cdmac_intrs[chan] != NULL)
		return (NULL);

	ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
	ih->cih_func = func;
	ih->cih_arg = arg;

	return (cdmac_intrs[chan] = ih);
}

void
ll_dmac_intr_disestablish(int chan, void *handle)
{
	struct cdmac_intr_handle *ih = handle;
	int 			s;

	KASSERT(chan > 0 && chan < CDMAC_NCHAN);
	KASSERT(cdmac_intrs[chan] == handle);

	s = splcdmac();
	cdmac_intrs[chan] = NULL;
	splx(s);

	kmem_free(ih, sizeof(*ih));
}

int
virtex_bus_space_tag(const char *xname, bus_space_tag_t *bst)
{
	if (strncmp(xname, "xlcom", 5) == 0) {
		*bst = &xlcom_bst;
		return (0);
	}

	return (ENODEV);
}

void
virtex_machdep_init(vaddr_t endva, vsize_t maxsz, struct mem_region *phys,
    struct mem_region *avail)
{
	/* Nothing to do -- no memory-mapped devices. */
}

void
device_register(device_t dev, void *aux)
{
	/* Nothing to do -- no property hacks needed. */
}