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
/* $NetBSD: tegra_apbdma.c,v 1.9 2021/01/27 03:10:19 thorpej Exp $ */

/*-
 * Copyright (c) 2017 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * 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: tegra_apbdma.c,v 1.9 2021/01/27 03:10:19 thorpej Exp $");

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

#include <arm/nvidia/tegra_reg.h>
#include <arm/nvidia/tegra_apbdmareg.h>
#include <arm/nvidia/tegra_var.h>

#include <dev/fdt/fdtvar.h>

#define	TEGRA_APBDMA_NCHAN	32

static void *	tegra_apbdma_acquire(device_t, const void *, size_t,
				     void (*)(void *), void *);
static void	tegra_apbdma_release(device_t, void *);
static int	tegra_apbdma_transfer(device_t, void *,
				      struct fdtbus_dma_req *);
static void	tegra_apbdma_halt(device_t, void *);

static const struct fdtbus_dma_controller_func tegra_apbdma_funcs = {
	.acquire = tegra_apbdma_acquire,
	.release = tegra_apbdma_release,
	.transfer = tegra_apbdma_transfer,
	.halt = tegra_apbdma_halt
};

static int	tegra_apbdma_match(device_t, cfdata_t, void *);
static void	tegra_apbdma_attach(device_t, device_t, void *);

static int	tegra_apbdma_intr(void *);

struct tegra_apbdma_softc;

struct tegra_apbdma_chan {
	struct tegra_apbdma_softc *ch_sc;
	u_int			ch_n;
	void			*ch_ih;
	void			(*ch_cb)(void *);
	void			*ch_cbarg;
	u_int			ch_req;
};

struct tegra_apbdma_softc {
	device_t		sc_dev;
	bus_space_tag_t		sc_bst;
	bus_space_handle_t	sc_bsh;
	int			sc_phandle;

	struct tegra_apbdma_chan sc_chan[TEGRA_APBDMA_NCHAN];
};

CFATTACH_DECL_NEW(tegra_apbdma, sizeof(struct tegra_apbdma_softc),
	tegra_apbdma_match, tegra_apbdma_attach, NULL, NULL);

#define	APBDMA_READ(sc, reg)						\
	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
#define	APBDMA_WRITE(sc, reg, val)					\
	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "nvidia,tegra210-apbdma" },
	{ .compat = "nvidia,tegra124-apbdma" },
	DEVICE_COMPAT_EOL
};

static int
tegra_apbdma_match(device_t parent, cfdata_t cf, void *aux)
{
	struct fdt_attach_args * const faa = aux;

	return of_compatible_match(faa->faa_phandle, compat_data);
}

static void
tegra_apbdma_attach(device_t parent, device_t self, void *aux)
{
	struct tegra_apbdma_softc *sc = device_private(self);
	struct fdt_attach_args * const faa = aux;
	const int phandle = faa->faa_phandle;
	struct fdtbus_reset *rst;
	struct clk *clk;
	bus_addr_t addr;
	bus_size_t size;
	int error;
	u_int n;

	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
		aprint_error(": couldn't get registers\n");
		return;
	}

	clk = fdtbus_clock_get_index(phandle, 0);
	if (clk == NULL) {
		aprint_error(": couldn't get clock\n");
		return;
	}
	rst = fdtbus_reset_get(phandle, "dma");
	if (rst == NULL) {
		aprint_error(": couldn't get reset dma\n");
		return;
	}

	fdtbus_reset_assert(rst);
	error = clk_enable(clk);
	if (error) {
		aprint_error(": couldn't enable clock dma: %d\n", error);
		return;
	}
	fdtbus_reset_deassert(rst);

	sc->sc_dev = self;
	sc->sc_bst = faa->faa_bst;
	sc->sc_phandle = phandle;
	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
	if (error) {
		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
		return;
	}
	for (n = 0; n < TEGRA_APBDMA_NCHAN; n++) {
		sc->sc_chan[n].ch_sc = sc;
		sc->sc_chan[n].ch_n = n;
	}

	aprint_naive("\n");
	aprint_normal(": APBDMA\n");

	/* Stop all channels */
	for (n = 0; n < TEGRA_APBDMA_NCHAN; n++)
		APBDMA_WRITE(sc, APBDMACHAN_CSR_REG(n), 0);

	/* Mask interrupts */
	APBDMA_WRITE(sc, APBDMA_IRQ_MASK_REG, 0);

	/* Global enable */
	APBDMA_WRITE(sc, APBDMA_COMMAND_REG, APBDMA_COMMAND_GEN);

	fdtbus_register_dma_controller(self, phandle, &tegra_apbdma_funcs);
}

static int
tegra_apbdma_intr(void *priv)
{
	struct tegra_apbdma_chan *ch = priv;
	struct tegra_apbdma_softc *sc = ch->ch_sc;
	const u_int n = ch->ch_n;
	uint32_t sta;

	sta = APBDMA_READ(sc, APBDMACHAN_STA_REG(n));
	APBDMA_WRITE(sc, APBDMACHAN_STA_REG(n), sta);	/* clear EOC */

	ch->ch_cb(ch->ch_cbarg);

	return 1;
}

static void *
tegra_apbdma_acquire(device_t dev, const void *data, size_t len,
    void (*cb)(void *), void *cbarg)
{
	struct tegra_apbdma_softc *sc = device_private(dev);
	struct tegra_apbdma_chan *ch;
	u_int n;
	char intrstr[128];

	if (len != 4)
		return NULL;

	const u_int req = be32dec(data);
	if (req > __SHIFTOUT_MASK(APBDMACHAN_CSR_REQ_SEL))
		return NULL;

	for (n = 0; n < TEGRA_APBDMA_NCHAN; n++) {
		ch = &sc->sc_chan[n];
		if (ch->ch_ih == NULL)
			break;
	}
	if (n >= TEGRA_APBDMA_NCHAN) {
		aprint_error_dev(dev, "no free DMA channel\n");
		return NULL;
	}

	if (!fdtbus_intr_str(sc->sc_phandle, n, intrstr, sizeof(intrstr))) {
		aprint_error_dev(dev, "failed to decode interrupt %u\n", n);
		return NULL;
	}

	ch->ch_ih = fdtbus_intr_establish_xname(sc->sc_phandle, n, IPL_VM,
	    FDT_INTR_MPSAFE, tegra_apbdma_intr, ch, device_xname(dev));
	if (ch->ch_ih == NULL) {
		aprint_error_dev(dev, "failed to establish interrupt on %s\n",
		    intrstr);
		return NULL;
	}
	aprint_normal_dev(dev, "interrupting on %s (channel %u)\n", intrstr, n);

	ch->ch_cb = cb;
	ch->ch_cbarg = cbarg;
	ch->ch_req = req;

	/* Unmask interrupts for this channel */
	APBDMA_WRITE(sc, APBDMA_IRQ_MASK_SET_REG, __BIT(n));

	return ch;
}
static void
tegra_apbdma_release(device_t dev, void *priv)
{
	struct tegra_apbdma_softc *sc = device_private(dev);
	struct tegra_apbdma_chan *ch = priv;
	const u_int n = ch->ch_n;

	KASSERT(ch->ch_ih != NULL);

	/* Halt the channel */
	APBDMA_WRITE(sc, APBDMACHAN_CSR_REG(n), 0);

	/* Mask interrupts for this channel */
	APBDMA_WRITE(sc, APBDMA_IRQ_MASK_CLR_REG, __BIT(n));

	fdtbus_intr_disestablish(sc->sc_phandle, ch->ch_ih);

	ch->ch_cb = NULL;
	ch->ch_cbarg = NULL;
}

static int
tegra_apbdma_transfer(device_t dev, void *priv, struct fdtbus_dma_req *req)
    
{
	struct tegra_apbdma_softc *sc = device_private(dev);
	struct tegra_apbdma_chan *ch = priv;
	const u_int n = ch->ch_n;
	uint32_t csr = 0;
	uint32_t csre = 0;
	uint32_t ahb_seq = 0;
	uint32_t apb_seq = 0;

	/* Scatter-gather not supported */
	if (req->dreq_nsegs != 1)
		return EINVAL;

	/* Addresses must be aligned to 32-bits */
	if ((req->dreq_segs[0].ds_addr & 3) != 0 ||
	    (req->dreq_dev_phys & 3) != 0)
		return EINVAL;

	/* Length must be a multiple of 32-bits */
	if ((req->dreq_segs[0].ds_len & 3) != 0)
		return EINVAL;

	csr |= __SHIFTIN(ch->ch_req, APBDMACHAN_CSR_REQ_SEL);

	/*
	 * Set DMA transfer direction.
	 * APBDMACHAN_CSR_DIR=0 means "APB read to AHB write", and
	 * APBDMACHAN_CSR_DIR=1 means "AHB read to APB write".
	 */
	if (req->dreq_dir == FDT_DMA_WRITE)
		csr |= APBDMACHAN_CSR_DIR; 

	/*
	 * Generate interrupt when DMA block transfer completes.
	 */
	if (req->dreq_block_irq)
		csr |= APBDMACHAN_CSR_IE_EOC;

	/*
	 * Single or multiple block transfer
	 */
	if (!req->dreq_block_multi)
		csr |= APBDMACHAN_CSR_ONCE;

	/*
	 * Flow control enable
	 */
	if (req->dreq_flow)
		csr |= APBDMACHAN_CSR_FLOW;

	/*
	 * Route interrupt to CPU. 1 = CPU, 0 = COP
	 */
	ahb_seq |= APBDMACHAN_AHB_SEQ_INTR_ENB;

	/*
	 * AHB is a 32-bit bus.
	 */
	if (req->dreq_mem_opt.opt_bus_width != 32)
		return EINVAL;
	ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_BUS_WIDTH_32,
			     APBDMACHAN_AHB_SEQ_BUS_WIDTH);

	/*
	 * AHB data swap.
	 */
	if (req->dreq_mem_opt.opt_swap)
		ahb_seq |= APBDMACHAN_AHB_SEQ_DATA_SWAP;

	/*
	 * AHB burst size.
	 */
	switch (req->dreq_mem_opt.opt_burst_len) {
	case 32:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_BURST_1,
				     APBDMACHAN_AHB_SEQ_BURST);
		break;
	case 128:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_BURST_4,
				     APBDMACHAN_AHB_SEQ_BURST);
		break;
	case 256:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_BURST_8,
				     APBDMACHAN_AHB_SEQ_BURST);
		break;
	default:
		return EINVAL;
	}

	/*
	 * 2X double buffering mode. Only supported in run-multiple mode
	 * with no-wrap operations.
	 */
	if (req->dreq_mem_opt.opt_dblbuf) {
		if (req->dreq_mem_opt.opt_wrap_len != 0)
			return EINVAL;
		if (!req->dreq_block_multi)
			return EINVAL;
		ahb_seq |= APBDMACHAN_AHB_SEQ_DBL_BUF;
	}

	/*
	 * AHB address wrap.
	 */
	switch (req->dreq_mem_opt.opt_wrap_len) {
	case 0:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_WRAP_NO_WRAP,
				     APBDMACHAN_AHB_SEQ_WRAP);
		break;
	case 128:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_WRAP_32,
				     APBDMACHAN_AHB_SEQ_WRAP);
		break;
	case 256:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_WRAP_64,
				     APBDMACHAN_AHB_SEQ_WRAP);
		break;
	case 512:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_WRAP_128,
				     APBDMACHAN_AHB_SEQ_WRAP);
		break;
	case 1024:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_WRAP_256,
				     APBDMACHAN_AHB_SEQ_WRAP);
		break;
	case 2048:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_WRAP_512,
				     APBDMACHAN_AHB_SEQ_WRAP);
		break;
	case 4096:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_WRAP_1024,
				     APBDMACHAN_AHB_SEQ_WRAP);
		break;
	case 8192:
		ahb_seq |= __SHIFTIN(APBDMACHAN_AHB_SEQ_WRAP_2048,
				     APBDMACHAN_AHB_SEQ_WRAP);
		break;
	default:
		return EINVAL;
	}

	/*
	 * APB bus width.
	 */
	switch (req->dreq_dev_opt.opt_bus_width) {
	case 8:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_BUS_WIDTH_8,
				     APBDMACHAN_APB_SEQ_BUS_WIDTH);
		break;
	case 16:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_BUS_WIDTH_16,
				     APBDMACHAN_APB_SEQ_BUS_WIDTH);
		break;
	case 32:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_BUS_WIDTH_32,
				     APBDMACHAN_APB_SEQ_BUS_WIDTH);
		break;
	default:
		return EINVAL;
	}

	/*
	 * APB data swap.
	 */
	if (req->dreq_dev_opt.opt_swap)
		apb_seq |= APBDMACHAN_APB_SEQ_DATA_SWAP;

	/*
	 * APB address wrap-around window.
	 */
	switch (req->dreq_dev_opt.opt_wrap_len) {
	case 0:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_WRAP_NO_WRAP,
				     APBDMACHAN_APB_SEQ_WRAP);
		break;
	case 4:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_WRAP_1,
				     APBDMACHAN_APB_SEQ_WRAP);
		break;
	case 8:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_WRAP_2,
				     APBDMACHAN_APB_SEQ_WRAP);
		break;
	case 16:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_WRAP_4,
				     APBDMACHAN_APB_SEQ_WRAP);
		break;
	case 32:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_WRAP_8,
				     APBDMACHAN_APB_SEQ_WRAP);
		break;
	case 64:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_WRAP_16,
				     APBDMACHAN_APB_SEQ_WRAP);
		break;
	case 128:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_WRAP_32,
				     APBDMACHAN_APB_SEQ_WRAP);
		break;
	case 256:
		apb_seq |= __SHIFTIN(APBDMACHAN_APB_SEQ_WRAP_64,
				     APBDMACHAN_APB_SEQ_WRAP);
		break;
	default:
		return EINVAL;
	}

	/*
	 * Program all channel registers before setting the channel enable bit.
	 */
	APBDMA_WRITE(sc, APBDMACHAN_AHB_PTR_REG(n), req->dreq_segs[0].ds_addr);
	APBDMA_WRITE(sc, APBDMACHAN_APB_PTR_REG(n), req->dreq_dev_phys);
	APBDMA_WRITE(sc, APBDMACHAN_AHB_SEQ_REG(n), ahb_seq);
	APBDMA_WRITE(sc, APBDMACHAN_APB_SEQ_REG(n), apb_seq);
	APBDMA_WRITE(sc, APBDMACHAN_WCOUNT_REG(n), req->dreq_segs[0].ds_len);
	APBDMA_WRITE(sc, APBDMACHAN_CSRE_REG(n), csre);
	APBDMA_WRITE(sc, APBDMACHAN_CSR_REG(n), csr | APBDMACHAN_CSR_ENB);

	return 0;
}

static void
tegra_apbdma_halt(device_t dev, void *priv)
{
	struct tegra_apbdma_softc *sc = device_private(dev);
	struct tegra_apbdma_chan *ch = priv;
	const u_int n = ch->ch_n;
	uint32_t v;

	v = APBDMA_READ(sc, APBDMACHAN_CSR_REG(n));
	v &= ~APBDMACHAN_CSR_ENB;
	APBDMA_WRITE(sc, APBDMACHAN_CSR_REG(n), v);
}