Home | History | Annotate | Line # | Download | only in altera
cycv_platform.c revision 1.19
      1  1.19    skrll /* $NetBSD: cycv_platform.c,v 1.19 2023/04/07 08:55:29 skrll Exp $ */
      2   1.1  aymeric 
      3   1.1  aymeric /* This file is in the public domain. */
      4   1.1  aymeric 
      5   1.1  aymeric #include "arml2cc.h"
      6   1.8  aymeric #include "opt_console.h"
      7   1.1  aymeric #include "opt_multiprocessor.h"
      8   1.1  aymeric 
      9   1.1  aymeric #include <sys/cdefs.h>
     10  1.19    skrll __KERNEL_RCSID(0, "$NetBSD: cycv_platform.c,v 1.19 2023/04/07 08:55:29 skrll Exp $");
     11   1.1  aymeric 
     12   1.1  aymeric #define	_ARM32_BUS_DMA_PRIVATE
     13   1.1  aymeric #include <sys/param.h>
     14   1.1  aymeric #include <sys/bus.h>
     15   1.1  aymeric #include <sys/cpu.h>
     16   1.1  aymeric #include <sys/device.h>
     17   1.1  aymeric 
     18   1.1  aymeric #include <uvm/uvm_extern.h>
     19   1.1  aymeric 
     20   1.3    skrll #include <arm/arm32/machdep.h>
     21   1.3    skrll 
     22   1.1  aymeric #include <arm/altera/cycv_reg.h>
     23   1.1  aymeric #include <arm/altera/cycv_var.h>
     24   1.1  aymeric #include <arm/cortex/a9tmr_var.h>
     25   1.1  aymeric #include <arm/cortex/pl310_var.h>
     26   1.1  aymeric #include <arm/cortex/scu_reg.h>
     27   1.1  aymeric 
     28   1.1  aymeric #include <arm/bootconfig.h>
     29   1.1  aymeric #include <arm/cpufunc.h>
     30   1.1  aymeric 
     31  1.19    skrll #include <dev/fdt/fdtvar.h>
     32  1.19    skrll 
     33   1.1  aymeric #include <arm/fdt/arm_fdtvar.h>
     34   1.8  aymeric #include <dev/ic/comreg.h>
     35   1.1  aymeric 
     36   1.6    skrll void cycv_platform_early_putchar(char);
     37   1.6    skrll 
     38  1.13    skrll void __noasan
     39   1.6    skrll cycv_platform_early_putchar(char c) {
     40   1.6    skrll #ifdef CONSADDR
     41   1.6    skrll #define CONSADDR_VA (CONSADDR - CYCV_PERIPHERAL_BASE + CYCV_PERIPHERAL_VBASE)
     42   1.8  aymeric 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
     43   1.8  aymeric 	    (volatile uint32_t *) CONSADDR_VA :
     44   1.8  aymeric 	    (volatile uint32_t *) CONSADDR;
     45   1.6    skrll 
     46   1.6    skrll 	while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0)
     47   1.6    skrll 		;
     48   1.6    skrll 
     49   1.6    skrll 	uartaddr[com_data] = htole32(c);
     50   1.6    skrll #endif
     51   1.6    skrll }
     52   1.1  aymeric 
     53   1.1  aymeric static const struct pmap_devmap *
     54   1.1  aymeric cycv_platform_devmap(void) {
     55   1.1  aymeric 	static const struct pmap_devmap devmap[] = {
     56   1.1  aymeric 		DEVMAP_ENTRY(CYCV_PERIPHERAL_VBASE,
     57   1.1  aymeric 				CYCV_PERIPHERAL_BASE,
     58   1.1  aymeric 				CYCV_PERIPHERAL_SIZE),
     59   1.1  aymeric 		DEVMAP_ENTRY_END
     60   1.1  aymeric 	};
     61   1.1  aymeric 
     62   1.1  aymeric 	return devmap;
     63   1.1  aymeric }
     64   1.1  aymeric 
     65   1.3    skrll static void
     66   1.3    skrll cycv_platform_bootstrap(void)
     67   1.3    skrll {
     68   1.3    skrll 	bus_space_tag_t bst = &armv7_generic_bs_tag;
     69   1.3    skrll 	bus_space_handle_t bsh_l2c;
     70   1.3    skrll 
     71   1.3    skrll 	bus_space_map(bst, CYCV_L2CACHE_BASE, CYCV_L2CACHE_SIZE, 0, &bsh_l2c);
     72   1.3    skrll 
     73   1.3    skrll #if NARML2CC > 0
     74   1.3    skrll 	arml2cc_init(bst, bsh_l2c, 0);
     75   1.3    skrll #endif
     76   1.9  aymeric 
     77   1.9  aymeric 	arm_fdt_cpu_bootstrap();
     78   1.3    skrll }
     79   1.1  aymeric 
     80  1.10    skrll static int
     81   1.3    skrll cycv_mpstart(void)
     82   1.3    skrll {
     83  1.16      rin 	int ret = 0;
     84  1.16      rin 
     85  1.16      rin #ifdef MULTIPROCESSOR
     86   1.1  aymeric 	bus_space_tag_t bst = &armv7_generic_bs_tag;
     87   1.1  aymeric 	bus_space_handle_t bsh_rst;
     88   1.1  aymeric 	bus_space_handle_t bsh_scu;
     89   1.1  aymeric 
     90   1.1  aymeric 	bus_space_map(bst, CYCV_RSTMGR_BASE, CYCV_RSTMGR_SIZE, 0, &bsh_rst);
     91   1.1  aymeric 	bus_space_map(bst, CYCV_SCU_BASE, CYCV_SCU_SIZE, 0, &bsh_scu);
     92   1.1  aymeric 
     93   1.1  aymeric 	/* Enable Snoop Control Unit */
     94   1.7  aymeric 	bus_space_write_4(bst, bsh_scu, SCU_INV_ALL_REG, 0xff);
     95   1.7  aymeric 	bus_space_write_4(bst, bsh_scu, SCU_CTL,
     96   1.7  aymeric 		bus_space_read_4(bst, bsh_scu, SCU_CTL) | SCU_CTL_SCU_ENA);
     97   1.1  aymeric 
     98  1.11  aymeric 	const uint32_t startfunc =
     99  1.11  aymeric 		(uint32_t) KERN_VTOPHYS((vaddr_t) cpu_mpstart);
    100   1.1  aymeric 
    101   1.1  aymeric 	/*
    102  1.11  aymeric 	 * We place a "LDR PC, =cpu_mpstart" at address 0 in order to bootstrap
    103   1.1  aymeric 	 * CPU 1. We can't use the similar feature of the Boot ROM because
    104  1.11  aymeric 	 * it was unmapped by u-boot in favor of the SDRAM.
    105   1.1  aymeric 	 */
    106   1.5  aymeric 	pmap_map_chunk(kernel_l1pt.pv_va, CYCV_SDRAM_VBASE, CYCV_SDRAM_BASE,
    107   1.5  aymeric 		L1_S_SIZE, VM_PROT_READ|VM_PROT_WRITE, PMAP_NOCACHE);
    108  1.11  aymeric 
    109  1.11  aymeric 	/* 0: LDR PC, [PC, #0x18] -> loads address at 0x20 into PC */
    110  1.11  aymeric 	*(volatile uint32_t *) CYCV_SDRAM_VBASE = htole32(0xe59ff018);
    111  1.11  aymeric 	*(volatile uint32_t *) (CYCV_SDRAM_VBASE + 0x20) = startfunc;
    112  1.11  aymeric 
    113   1.5  aymeric 	pmap_unmap_chunk(kernel_l1pt.pv_va, CYCV_SDRAM_VBASE, L1_S_SIZE);
    114   1.1  aymeric 
    115   1.1  aymeric 	bus_space_write_4(bst, bsh_rst, CYCV_RSTMGR_MPUMODRST,
    116   1.1  aymeric 		bus_space_read_4(bst, bsh_rst, CYCV_RSTMGR_MPUMODRST) &
    117   1.1  aymeric 			~CYCV_RSTMGR_MPUMODRST_CPU1);
    118   1.9  aymeric 
    119   1.9  aymeric 	/* Wait for secondary processor to start */
    120  1.10    skrll 	int i;
    121  1.10    skrll 	for (i = 0x10000000; i > 0; i--) {
    122  1.12    skrll 		if (cpu_hatched_p(1))
    123   1.9  aymeric 			break;
    124   1.9  aymeric 	}
    125  1.10    skrll 	if (i == 0) {
    126  1.10    skrll 		aprint_error("cpu%d: WARNING: AP failed to start\n", 1);
    127  1.10    skrll 		ret++;
    128  1.10    skrll 	}
    129  1.16      rin #endif
    130  1.10    skrll 
    131  1.10    skrll 	return ret;
    132   1.1  aymeric }
    133   1.1  aymeric 
    134   1.1  aymeric static void
    135   1.1  aymeric cycv_platform_init_attach_args(struct fdt_attach_args *faa) {
    136   1.1  aymeric 	faa->faa_bst = &armv7_generic_bs_tag;
    137   1.1  aymeric 	faa->faa_dmat = &arm_generic_dma_tag;
    138   1.1  aymeric }
    139   1.1  aymeric 
    140   1.1  aymeric static void
    141  1.17  thorpej cycv_platform_device_register(device_t dev, void *aux)
    142  1.17  thorpej {
    143   1.1  aymeric 	prop_dictionary_t dict = device_properties(dev);
    144   1.1  aymeric 
    145   1.1  aymeric 	if (device_is_a(dev, "arma9tmr")) {
    146   1.1  aymeric 		prop_dictionary_set_uint32(dict, "frequency",
    147   1.1  aymeric 			cycv_clkmgr_early_get_mpu_clk() / 4);
    148   1.1  aymeric 	}
    149   1.1  aymeric }
    150   1.1  aymeric 
    151   1.1  aymeric static void
    152   1.1  aymeric cycv_platform_reset(void) {
    153   1.1  aymeric 	bus_space_tag_t bst = &armv7_generic_bs_tag;
    154   1.1  aymeric 	bus_space_handle_t bsh;
    155   1.1  aymeric 	uint32_t val;
    156   1.1  aymeric 
    157   1.1  aymeric 	bus_space_map(bst, CYCV_RSTMGR_BASE, CYCV_RSTMGR_SIZE, 0, &bsh);
    158   1.1  aymeric 	val = bus_space_read_4(bst, bsh, CYCV_RSTMGR_CTRL);
    159   1.1  aymeric 	bus_space_write_4(bst, bsh, CYCV_RSTMGR_CTRL,
    160   1.1  aymeric 		val | CYCV_RSTMGR_CTRL_SWCOLDRSTREQ);
    161   1.1  aymeric }
    162   1.1  aymeric 
    163   1.1  aymeric static u_int
    164   1.1  aymeric cycv_platform_uart_freq(void) {
    165   1.1  aymeric 	return cycv_clkmgr_early_get_l4_sp_clk();
    166   1.1  aymeric }
    167   1.1  aymeric 
    168  1.19    skrll static const struct fdt_platform cycv_platform = {
    169  1.19    skrll 	.fp_devmap = cycv_platform_devmap,
    170  1.19    skrll 	.fp_bootstrap = cycv_platform_bootstrap,
    171  1.19    skrll 	.fp_init_attach_args = cycv_platform_init_attach_args,
    172  1.19    skrll 	.fp_device_register = cycv_platform_device_register,
    173  1.19    skrll 	.fp_reset = cycv_platform_reset,
    174  1.19    skrll 	.fp_delay = a9tmr_delay,
    175  1.19    skrll 	.fp_uart_freq = cycv_platform_uart_freq,
    176  1.19    skrll 	.fp_mpstart = cycv_mpstart,
    177   1.1  aymeric };
    178   1.1  aymeric 
    179  1.19    skrll FDT_PLATFORM(cycv, "altr,socfpga-cyclone5", &cycv_platform);
    180