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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* 	$NetBSD: design_gsrd2.c,v 1.8 2021/08/07 16:18:52 thorpej 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 "opt_virtex.h"

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: design_gsrd2.c,v 1.8 2021/08/07 16:18:52 thorpej Exp $");

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

#include <machine/powerpc.h>

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

#include <evbppc/virtex/dev/xcvbusvar.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_TEMAC_BASE 		0x0030
#define	DCR_TFT0_BASE 		0x0082
#define	DCR_TFT1_BASE 		0x0086
#define	DCR_CDMAC_BASE 		0x0140

#define OPB_BASE 		0x80000000 	/* below are offsets in opb */
#define OPB_XLCOM_BASE 		0x010000
#define OPB_GPIO_BASE 		0x020000
#define OPB_PSTWO0_BASE 	0x040000
#define OPB_PSTWO1_BASE 	0x041000
#define CDMAC_NCHAN 		2 	/* cdmac {Tx,Rx} */
#define CDMAC_INTR_LINE 	0

#define	TFT_FB_BASE 		0x3c00000
#define TFT_FB_SIZE 		(2*1024*1024)

/*
 * CDMAC per-channel interrupt handler. CDMAC has one interrupt signal
 * per two channels on mpmc2, 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.
 */
typedef struct cdmac_intrhand {
	void 			(*cih_func)(void *);
	void 			*cih_arg;
} *cdmac_intrhand_t;

/* Two instantiated channels, one logical interrupt per direction. */
static struct cdmac_intrhand 	cdmacintr[CDMAC_NCHAN];
static void 			*cdmac_ih;


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

#ifndef DESIGN_DFC
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_TFT0_BASE, TFT_CTRL);
	WCASE(DCR_TFT0_BASE, TFT_ADDR);
	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_TFT0_BASE, TFT_CTRL);
	RCASE(DCR_TFT0_BASE, TFT_ADDR);
	RDEAD(addr);
	}

	return (val);
}
#endif /* !DESIGN_DFC */

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

	switch (addr) {
	WCASE(DCR_TFT1_BASE, TFT_CTRL);
	WCASE(DCR_TFT0_BASE, TFT_ADDR);
	WDEAD(addr);
	}
}

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

	addr += h;

	switch (addr) {
	RCASE(DCR_TFT1_BASE, TFT_CTRL);
	RCASE(DCR_TFT0_BASE, TFT_ADDR);
	RDEAD(addr);
	}

	return (val);
}

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

static void
cdmac_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_STAT_BASE(0)); 	/* Tx engine */
	WCASE(DCR_CDMAC_BASE, CDMAC_STAT_BASE(1)); 	/* Rx engine */
	WCASE(DCR_CDMAC_BASE, CDMAC_INTR);
	DOCHAN(WCASE, DCR_CDMAC_BASE, CDMAC_CTRL_BASE(0));
	DOCHAN(WCASE, DCR_CDMAC_BASE, CDMAC_CTRL_BASE(1));
	WDEAD(addr);
	}
}

static uint32_t
cdmac_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_STAT_BASE(0)); 	/* Tx engine */
	RCASE(DCR_CDMAC_BASE, CDMAC_STAT_BASE(1)); 	/* Rx engine */
	RCASE(DCR_CDMAC_BASE, CDMAC_INTR);
	DOCHAN(RCASE, DCR_CDMAC_BASE, CDMAC_CTRL_BASE(0));
	DOCHAN(RCASE, DCR_CDMAC_BASE, CDMAC_CTRL_BASE(1));
	RDEAD(addr);
	}

	return (val);
}

#undef DOCHAN

static void
temac_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 uint32_t
temac_read_4(bus_space_tag_t t, bus_space_handle_t h, uint32_t addr)
{
	uint32_t 		val;

	addr += h;

	switch (addr) {
	RCASE(DCR_TEMAC_BASE, TEMAC_RESET);
	RDEAD(addr);
	}

	return (val);
}

static const struct powerpc_bus_space cdmac_bst = {
	DCR_BST_BODY(DCR_CDMAC_BASE, cdmac_read_4, cdmac_write_4)
};

static const struct powerpc_bus_space temac_bst = {
	DCR_BST_BODY(DCR_TEMAC_BASE, temac_read_4, temac_write_4)
};

#ifndef DESIGN_DFC
static const struct powerpc_bus_space tft0_bst = {
	DCR_BST_BODY(DCR_TFT0_BASE, tft0_read_4, tft0_write_4)
};
#endif

static const struct powerpc_bus_space tft1_bst = {
	DCR_BST_BODY(DCR_TFT1_BASE, tft1_read_4, tft1_write_4)
};

static struct powerpc_bus_space opb_bst = {
	.pbs_flags 	= _BUS_SPACE_BIG_ENDIAN|_BUS_SPACE_MEM_TYPE,
	.pbs_base 	= 0 /*OPB_BASE*/,
	.pbs_offset 	= OPB_BASE,
};

static char opb_extent_storage[EXTENT_FIXED_STORAGE_SIZE(8)] __aligned(8);

/*
 * Master device configuration table for GSRD2 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;
	int 			gdv_dcr; 		/* XXX bst flag */
} gsrd_devices[] = {
	{			/* gsrd_devices[0] */
		.gdv_name 	= "xlcom",
		.gdv_attr 	= "xcvbus",
		.gdv_bst 	= &opb_bst,
		.gdv_addr 	= OPB_XLCOM_BASE,
		.gdv_intr 	= 2,
		.gdv_rx_dma 	= -1,
		.gdv_tx_dma 	= -1,
		.gdv_dcr 	= 0,
	},
	{			/* gsrd_devices[1] */
		.gdv_name 	= "temac",
		.gdv_attr 	= "xcvbus",
		.gdv_bst 	= &temac_bst,
		.gdv_addr 	= 0,
		.gdv_intr 	= 1, 		/* unused MII intr */
		.gdv_rx_dma 	= 1, 		/* cdmac Rx */
		.gdv_tx_dma 	= 0, 		/* cdmac Tx */
		.gdv_dcr 	= 1,
	},
#ifndef DESIGN_DFC
	{			/* gsrd_devices[2] */
		.gdv_name 	= "tft",
		.gdv_attr 	= "plbus",
		.gdv_bst 	= &tft0_bst,
		.gdv_addr 	= 0,
		.gdv_intr 	= -1,
		.gdv_rx_dma 	= -1,
		.gdv_tx_dma 	= -1,
		.gdv_dcr 	= 1,
	},
#endif
	{			/* gsrd_devices[2] */
		.gdv_name 	= "tft",
		.gdv_attr 	= "plbus",
		.gdv_bst 	= &tft1_bst,
		.gdv_addr 	= 0,
		.gdv_intr 	= -1,
		.gdv_rx_dma 	= -1,
		.gdv_tx_dma 	= -1,
		.gdv_dcr 	= 1,
	},
#ifdef DESIGN_DFC
	{			/* gsrd_devices[3] */
		.gdv_name 	= "pstwo",
		.gdv_attr 	= "xcvbus",
		.gdv_bst 	= &opb_bst,
		.gdv_addr 	= OPB_PSTWO0_BASE,
		.gdv_intr 	= 3,
		.gdv_rx_dma 	= -1,
		.gdv_tx_dma 	= -1,
		.gdv_dcr 	= 0,
	},
	{			/* gsrd_devices[4] */
		.gdv_name 	= "pstwo",
		.gdv_attr 	= "xcvbus",
		.gdv_bst 	= &opb_bst,
		.gdv_addr 	= OPB_PSTWO1_BASE,
		.gdv_intr 	= 4,
		.gdv_rx_dma 	= -1,
		.gdv_tx_dma 	= -1,
		.gdv_dcr 	= 0,
	},
#endif
};

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

	KASSERT(idx >= 0 && idx < CDMAC_NCHAN);

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

	return (chan);
}

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

	isr = bus_space_read_4(&cdmac_bst, 0, CDMAC_INTR);

	if (ISSET(isr, CDMAC_INTR_TX0) && cdmacintr[0].cih_func) {
		(cdmacintr[0].cih_func)(cdmacintr[0].cih_arg);
		did++;
	}
	if (ISSET(isr, CDMAC_INTR_RX0) && cdmacintr[1].cih_func) {
		(cdmacintr[1].cih_func)(cdmacintr[1].cih_arg);
		did++;
	}

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

	/* XXX This still happens all the time under load. */
#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 DMA channels. */
	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_INTR, 0);

	vaa.vaa_dmat = paa->plb_dmat;

	for (i = 0; i < __arraycount(gsrd_devices); i++) {
		const struct gsrddev 	*g = &gsrd_devices[i];

		vaa._vaa_is_dcr = g->gdv_dcr; 	/* XXX bst flag */
		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_NET,
	    cdmac_intr, NULL);
	if (cdmac_ih == NULL)
		panic("virtex_mpmc_done: could not establish cdmac intr");

	/* Clear (XXX?) and enable interrupts. */
	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 (*handler)(void *), void *arg)
{
	KASSERT(chan >= 0 && chan < CDMAC_NCHAN);
	KASSERT(cdmacintr[chan].cih_func == NULL);
	KASSERT(cdmacintr[chan].cih_arg == NULL);

	cdmacintr[chan].cih_func = handler;
	cdmacintr[chan].cih_arg = arg;

	return (&cdmacintr[chan]);
}

void
ll_dmac_intr_disestablish(int chan, void *handle)
{
	int 			s;

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

	s = splnet();
	cdmacintr[chan].cih_func = NULL;
	cdmacintr[chan].cih_arg = NULL;
	splx(s);
}

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

	return (ENODEV);
}

void
virtex_machdep_init(vaddr_t endva, vsize_t maxsz, struct mem_region *phys,
    struct mem_region *avail)
{
	ppc4xx_tlb_reserve(OPB_BASE, endva, maxsz, TLB_I | TLB_G);
	endva += maxsz;

	opb_bst.pbs_limit = maxsz;

	if (bus_space_init(&opb_bst, "opbtag", opb_extent_storage,
	    sizeof(opb_extent_storage)))
		panic("virtex_machdep_init: failed to initialize opb_bst");

	/*
	 * The TFT controller is broken, we can't change FB address.
	 * Hardwire it at predefined base address, create uncached
	 * mapping.
	 */

	avail[0].size = TFT_FB_BASE - avail[0].start;
	ppc4xx_tlb_reserve(TFT_FB_BASE, endva, TFT_FB_SIZE, TLB_I | TLB_G);
}

void
device_register(device_t dev, void *aux)
{
	prop_number_t 		pn;
	void 			*fb;

	if (strncmp(device_xname(dev), "tft0", 4) == 0) {
		fb = ppc4xx_tlb_mapiodev(TFT_FB_BASE, TFT_FB_SIZE);
		if (fb == NULL)
			panic("device_register: framebuffer mapping gone!\n");

		pn = prop_number_create_unsigned_integer(TFT_FB_BASE);
		if (pn == NULL) {
			printf("WARNING: could not allocate virtex-tft-pa\n");
			return ;
		}
		if (prop_dictionary_set(device_properties(dev),
		    "virtex-tft-pa", pn) != true)
			printf("WARNING: could not set virtex-tft-pa\n");
		prop_object_release(pn);

		pn = prop_number_create_unsigned_integer((uintptr_t)fb);
		if (pn == NULL) {
			printf("WARNING: could not allocate virtex-tft-va\n");
			return ;
		}
		if (prop_dictionary_set(device_properties(dev),
		    "virtex-tft-va", pn) != true)
			printf("WARNING: could not set virtex-tft-va\n");
		prop_object_release(pn);
	}
}