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
/* $Id: imx23_olinuxino_machdep.c,v 1.15 2023/04/21 14:58:35 skrll Exp $ */

/*
 * Copyright (c) 2012 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Petri Laakso.
 *
 * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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_arm_debug.h"
#include "opt_imx.h"

#include <sys/bus.h>
#include <sys/cdefs.h>
#include <sys/device.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <sys/systm.h>
#include <sys/termios.h>
#include <sys/types.h>

#include <uvm/uvm_prot.h>

#include <machine/bootconfig.h>
#include <machine/db_machdep.h>
#include <machine/pmap.h>

#include <arm/armreg.h>
#include <arm/cpu.h>
#include <arm/cpufunc.h>
#include <arm/locore.h>

#include <arm/arm32/machdep.h>
#include <arm/arm32/pte.h>

#include <arm/imx/imx23_clkctrlreg.h>
#include <arm/imx/imx23_digctlreg.h>
#include <arm/imx/imx23_rtcreg.h>
#include <arm/imx/imx23_uartdbgreg.h>
#include <arm/imx/imx23var.h>

#include "plcom.h"
#if (NPLCOM > 0)
#include <evbarm/dev/plcomreg.h>
#include <evbarm/dev/plcomvar.h>
#endif

#include "opt_evbarm_boardtype.h"
#include "opt_machdep.h"

#define	KERNEL_VM_BASE	(KERNEL_BASE + 0x8000000)
#define	KERNEL_VM_SIZE	0x20000000

#define L1_PAGE_TABLE (DRAM_BASE + MEMSIZE * 1024 * 1024 - L1_TABLE_SIZE)
#define BOOTIMX23_ARGS (L1_PAGE_TABLE - MAX_BOOT_STRING - 1)

#define PLCONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
#define PLCONSPEED 115200

#define REG_RD(reg) *(volatile uint32_t *)(reg)
#define REG_WR(reg, val)						\
do {									\
	*(volatile uint32_t *)((reg)) = val;				\
} while (0)

#define KERNEL_BASE_PHYS ((paddr_t)&KERNEL_BASE_phys)
#define KERNEL_BASE_VIRT ((vaddr_t)&KERNEL_BASE_virt)

/*
 * Static device map for i.MX23 peripheral address space.
 */
static const struct pmap_devmap devmap[] = {
	DEVMAP_ENTRY(
		APBH_BASE,		/* Virtual address. */
		APBH_BASE,		/* Physical address. */
		APBH_SIZE + APBX_SIZE	/* APBX located after APBH. */
	),
	DEVMAP_ENTRY_END
};

static struct plcom_instance imx23_pi = {
	.pi_type = PLCOM_TYPE_PL011,
	.pi_iot = &imx23_bus_space,
	.pi_size = PL011COM_UART_SIZE,
	.pi_iobase = HW_UARTDBG_BASE
};

extern char KERNEL_BASE_phys;
extern char KERNEL_BASE_virt;
BootConfig bootconfig;
char *boot_args;
static char kernel_boot_args[MAX_BOOT_STRING];

#define SSP_DIV 2
#define IO_FRAC 27

static void power_vddio_from_dcdc(int, int);
static void set_ssp_div(unsigned int);
static void set_io_frac(unsigned int);
static void bypass_ssp(void);

/*
 * Initialize ARM and return new SVC stack pointer.
 */
vaddr_t
initarm(void *arg)
{
        psize_t ram_size;

	if (set_cpufuncs())
		panic("set_cpufuncs failed");

	kern_vtopdiff = KERNEL_BASE - KERNEL_BASE_PHYS;

	pmap_devmap_register(devmap);
	consinit();

#define BDSTR(s)	_BDSTR(s)
#define _BDSTR(s)	#s
	printf("\nNetBSD/evbarm (" BDSTR(EVBARM_BOARDTYPE) ") booting ...\n");
#undef BDSTR
#undef _BDSTR

	/*
	 * SSP_CLK setup was postponed here from bootimx23 because SB wasn't
	 * able to load kernel if clocks were changed.
	 */
	power_vddio_from_dcdc(3300, 2925);
	set_ssp_div(SSP_DIV);
	set_io_frac(IO_FRAC);
	bypass_ssp();

	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);

	/* Copy boot arguments passed from bootimx23. */
	boot_args = (char *)KERN_PHYSTOV(BOOTIMX23_ARGS);
	memcpy(kernel_boot_args, boot_args, MAX_BOOT_STRING);
#ifdef BOOT_ARGS
	strcpy(kernel_boot_args, BOOT_ARGS);
#endif
	boot_args = kernel_boot_args;
#ifdef VERBOSE_INIT_ARM
	printf("boot_args @ %lx: '%s'\n", KERN_PHYSTOV(BOOTIMX23_ARGS),
	    boot_args);
#endif
	parse_mi_bootargs(boot_args);

	ram_size = MEMSIZE * 1024 * 1024;

	bootconfig.dramblocks = 1;
	bootconfig.dram[0].address = DRAM_BASE;
	bootconfig.dram[0].pages = ram_size / PAGE_SIZE;
	bootconfig.dram[0].flags = BOOT_DRAM_CAN_DMA;

        arm32_bootmem_init(bootconfig.dram[0].address, ram_size,
            ((vsize_t)&KERNEL_BASE_phys));

        arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0, devmap,
	    false);

        return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
}

/*
 * Initialize console.
 */
void
consinit(void)
{
	/* consinit() is called from also from the main(). */
	static int consinit_called = 0;

	if (consinit_called)
		return;

	plcomcnattach(&imx23_pi, PLCONSPEED, IMX23_UART_CLK, PLCONMODE, 0);

	consinit_called = 1;

	return;
}

/*
 * Reboot or halt the system.
 */
void
cpu_reboot(int howto, char *bootstr)
{
	static int cpu_reboot_called = 0;

	boothowto |= howto;

	/*
	 * If this is the first invocation of cpu_reboot() and the RB_NOSYNC
	 * flag is not set in howto; sync and unmount the system disks by
	 * calling vfs_shutdown(9) and set the time of day clock by calling
	 * resettodr(9).
	 */
	if (!cpu_reboot_called && !(boothowto & RB_NOSYNC)) {
		vfs_shutdown();
		resettodr();
	}

	cpu_reboot_called = 1;

	IRQdisable;	/* FIQ's stays on because they are special. */

	/*
	 * If rebooting after a crash (i.e., if RB_DUMP is set in howto, but
	 * RB_HALT is not), save a system crash dump.
	 */
	if ((boothowto & RB_DUMP) && !(boothowto & RB_HALT)) {
		panic("please implement crash dump!"); // XXX
		for(;;);
		/* NOTREACHED */
	}

	/* Run any shutdown hooks by calling pmf_system_shutdown(9). */
	pmf_system_shutdown(boothowto);

	printf("system %s.\n", boothowto & RB_HALT ? "halted" : "rebooted");

	if (boothowto & RB_HALT) {
		/* Enable i.MX233 wait-for-interrupt mode. */
		REG_WR(HW_CLKCTRL_BASE + HW_CLKCTRL_CPU,
		    (REG_RD(HW_CLKCTRL_BASE + HW_CLKCTRL_CPU) |
		    HW_CLKCTRL_CPU_INTERRUPT_WAIT));

		/* Disable FIQ's and wait for interrupt (which never arrives) */
		__asm volatile(				\
		    "mrs r0, cpsr\n\t"			\
		    "orr r0, #0x40\n\t"			\
		    "msr cpsr_c, r0\n\t"		\
		    "mov r0, #0\n\t"			\
		    "mcr p15, 0, r0, c7, c0, 4\n\t"
		);

		for(;;);

		/* NOT REACHED */
	}

	/* Reboot the system. */
	REG_WR(HW_RTC_BASE + HW_RTC_WATCHDOG, 10000);
	REG_WR(HW_RTC_BASE + HW_RTC_CTRL_SET, HW_RTC_CTRL_WATCHDOGEN);
	REG_WR(HW_RTC_BASE + HW_RTC_WATCHDOG, 0);

	for(;;);

	/* NOT REACHED */
}

/*
 * Delay us microseconds.
 */
void
delay(unsigned int us)
{
	uint32_t start;
	uint32_t now;
	uint32_t elapsed;
	uint32_t total;
	uint32_t last;

	total = 0;
	last = 0;
	start = REG_RD(HW_DIGCTL_BASE + HW_DIGCTL_MICROSECONDS);

	do {
		now = REG_RD(HW_DIGCTL_BASE + HW_DIGCTL_MICROSECONDS);

		if (start <= now)
			elapsed = now - start;
		else	/* Take care of overflow. */
			elapsed = (UINT32_MAX - start) + 1 + now;

		total += elapsed - last;
		last = elapsed;

	} while (total < us);

	return;
}
#include <arm/imx/imx23_powerreg.h>
#define PWR_VDDIOCTRL   (HW_POWER_BASE + HW_POWER_VDDIOCTRL)
#define PWR_CTRL        (HW_POWER_BASE + HW_POWER_CTRL)
#define PWR_CTRL_S      (HW_POWER_BASE + HW_POWER_CTRL_SET)
#define PWR_CTRL_C      (HW_POWER_BASE + HW_POWER_CTRL_CLR)

static void
power_vddio_from_dcdc(int target, int brownout)
{
        uint32_t tmp_r;

        /* BO_OFFSET must be within 2700mV - 3475mV */
        if (brownout > 3475)
                brownout = 3475;
        else if (brownout < 2700)
                brownout = 2700;


        /* Set LINREG_OFFSET one step below TRG. */
        tmp_r = REG_RD(PWR_VDDIOCTRL);
        tmp_r &= ~HW_POWER_VDDIOCTRL_LINREG_OFFSET;
        tmp_r |= __SHIFTIN(2, HW_POWER_VDDIOCTRL_LINREG_OFFSET);
        REG_WR(PWR_VDDIOCTRL, tmp_r);
        delay(10000);

        /* Enable VDDIO switching converter output. */
        tmp_r = REG_RD(PWR_VDDIOCTRL);
        tmp_r &= ~HW_POWER_VDDIOCTRL_DISABLE_FET;
        REG_WR(PWR_VDDIOCTRL, tmp_r);
        delay(10000);

        /* Set target voltage and brownout level. */
        tmp_r = REG_RD(PWR_VDDIOCTRL);
        tmp_r &= ~(HW_POWER_VDDIOCTRL_BO_OFFSET | HW_POWER_VDDIOCTRL_TRG);
        tmp_r |= __SHIFTIN(((target - brownout) / 25),
                HW_POWER_VDDIOCTRL_BO_OFFSET);
        tmp_r |= __SHIFTIN(((target - 2800) / 25), HW_POWER_VDDIOCTRL_TRG);
        REG_WR(PWR_VDDIOCTRL, tmp_r);
        delay(10000);

        /* Enable PWDN_BRNOUT. */
        REG_WR(PWR_CTRL_C, HW_POWER_CTRL_VDDIO_BO_IRQ);

        tmp_r = REG_RD(PWR_VDDIOCTRL);
        tmp_r |= HW_POWER_VDDIOCTRL_PWDN_BRNOUT;
        REG_WR(PWR_VDDIOCTRL, tmp_r);

        return;
}
#include <arm/imx/imx23_clkctrlreg.h>
#define CLKCTRL_SSP     (HW_CLKCTRL_BASE + HW_CLKCTRL_SSP)
#define CLKCTRL_FRAC    (HW_CLKCTRL_BASE + HW_CLKCTRL_FRAC)
#define CLKCTRL_SEQ_C   (HW_CLKCTRL_BASE + HW_CLKCTRL_CLKSEQ_CLR)

static
void set_ssp_div(unsigned int div)
{
        uint32_t tmp_r;

        tmp_r = REG_RD(CLKCTRL_SSP);
        tmp_r &= ~HW_CLKCTRL_SSP_CLKGATE;
        REG_WR(CLKCTRL_SSP, tmp_r);

        while (REG_RD(CLKCTRL_SSP) & HW_CLKCTRL_SSP_BUSY)
                ;

        tmp_r = REG_RD(CLKCTRL_SSP);
        tmp_r &= ~HW_CLKCTRL_SSP_DIV;
        tmp_r |= __SHIFTIN(div, HW_CLKCTRL_SSP_DIV);
        REG_WR(CLKCTRL_SSP, tmp_r);

        while (REG_RD(CLKCTRL_SSP) & HW_CLKCTRL_SSP_BUSY)
                ;

        return;

}
static
void set_io_frac(unsigned int frac)
{
        uint8_t *io_frac;
        uint32_t tmp_r;

        io_frac = (uint8_t *)(CLKCTRL_FRAC);
        io_frac++; /* emi */
        io_frac++; /* pix */
        io_frac++; /* io */
        tmp_r = (*io_frac)<<24;
        tmp_r &= ~(HW_CLKCTRL_FRAC_CLKGATEIO | HW_CLKCTRL_FRAC_IOFRAC);
        tmp_r |= __SHIFTIN(frac, HW_CLKCTRL_FRAC_IOFRAC);

        *io_frac = (uint8_t)(tmp_r>>24);

        return;
}
static
void bypass_ssp(void)
{
        REG_WR(CLKCTRL_SEQ_C, HW_CLKCTRL_CLKSEQ_BYPASS_SSP);

        return;
}