Home | History | Annotate | Line # | Download | only in altera
cycv_platform.c revision 1.7
      1  1.7  aymeric /* $NetBSD: cycv_platform.c,v 1.7 2018/11/02 18:09:17 aymeric 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.1  aymeric #include "opt_multiprocessor.h"
      7  1.1  aymeric 
      8  1.1  aymeric #include <sys/cdefs.h>
      9  1.7  aymeric __KERNEL_RCSID(0, "$NetBSD: cycv_platform.c,v 1.7 2018/11/02 18:09:17 aymeric Exp $");
     10  1.1  aymeric 
     11  1.1  aymeric #define	_ARM32_BUS_DMA_PRIVATE
     12  1.1  aymeric #include <sys/param.h>
     13  1.1  aymeric #include <sys/bus.h>
     14  1.1  aymeric #include <sys/cpu.h>
     15  1.1  aymeric #include <sys/device.h>
     16  1.1  aymeric 
     17  1.1  aymeric #include <uvm/uvm_extern.h>
     18  1.1  aymeric 
     19  1.3    skrll #include <arm/arm32/machdep.h>
     20  1.3    skrll 
     21  1.1  aymeric #include <arm/altera/cycv_reg.h>
     22  1.1  aymeric #include <arm/altera/cycv_var.h>
     23  1.1  aymeric #include <arm/cortex/a9tmr_var.h>
     24  1.1  aymeric #include <arm/cortex/pl310_var.h>
     25  1.1  aymeric #include <arm/cortex/scu_reg.h>
     26  1.1  aymeric 
     27  1.1  aymeric #include <arm/bootconfig.h>
     28  1.1  aymeric #include <arm/cpufunc.h>
     29  1.1  aymeric 
     30  1.1  aymeric #include <arm/fdt/arm_fdtvar.h>
     31  1.1  aymeric #include <dev/fdt/fdtvar.h>
     32  1.1  aymeric 
     33  1.6    skrll void cycv_platform_early_putchar(char);
     34  1.6    skrll 
     35  1.6    skrll void
     36  1.6    skrll cycv_platform_early_putchar(char c) {
     37  1.6    skrll #ifdef CONSADDR
     38  1.6    skrll #define CONSADDR_VA (CONSADDR - CYCV_PERIPHERAL_BASE + CYCV_PERIPHERAL_VBASE)
     39  1.6    skrll 	volatile uint32_t *uartaddr = (volatile uint32_t *) CONSADDR_VA;
     40  1.6    skrll 
     41  1.6    skrll 	while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0)
     42  1.6    skrll 		;
     43  1.6    skrll 
     44  1.6    skrll 	uartaddr[com_data] = htole32(c);
     45  1.6    skrll #endif
     46  1.6    skrll }
     47  1.1  aymeric 
     48  1.1  aymeric static const struct pmap_devmap *
     49  1.1  aymeric cycv_platform_devmap(void) {
     50  1.1  aymeric 	static const struct pmap_devmap devmap[] = {
     51  1.1  aymeric 		DEVMAP_ENTRY(CYCV_PERIPHERAL_VBASE,
     52  1.1  aymeric 				CYCV_PERIPHERAL_BASE,
     53  1.1  aymeric 				CYCV_PERIPHERAL_SIZE),
     54  1.1  aymeric 		DEVMAP_ENTRY_END
     55  1.1  aymeric 	};
     56  1.1  aymeric 
     57  1.1  aymeric 	return devmap;
     58  1.1  aymeric }
     59  1.1  aymeric 
     60  1.3    skrll static void
     61  1.3    skrll cycv_platform_bootstrap(void)
     62  1.3    skrll {
     63  1.3    skrll 	bus_space_tag_t bst = &armv7_generic_bs_tag;
     64  1.3    skrll 	bus_space_handle_t bsh_l2c;
     65  1.3    skrll 
     66  1.3    skrll 	bus_space_map(bst, CYCV_L2CACHE_BASE, CYCV_L2CACHE_SIZE, 0, &bsh_l2c);
     67  1.3    skrll 
     68  1.3    skrll #if NARML2CC > 0
     69  1.3    skrll 	arml2cc_init(bst, bsh_l2c, 0);
     70  1.3    skrll #endif
     71  1.3    skrll }
     72  1.1  aymeric 
     73  1.1  aymeric static void
     74  1.3    skrll cycv_mpstart(void)
     75  1.3    skrll {
     76  1.1  aymeric 	bus_space_tag_t bst = &armv7_generic_bs_tag;
     77  1.1  aymeric 	bus_space_handle_t bsh_rst;
     78  1.1  aymeric 	bus_space_handle_t bsh_scu;
     79  1.1  aymeric 
     80  1.1  aymeric 	bus_space_map(bst, CYCV_RSTMGR_BASE, CYCV_RSTMGR_SIZE, 0, &bsh_rst);
     81  1.1  aymeric 	bus_space_map(bst, CYCV_SCU_BASE, CYCV_SCU_SIZE, 0, &bsh_scu);
     82  1.1  aymeric 
     83  1.1  aymeric 	/* Enable Snoop Control Unit */
     84  1.7  aymeric 	bus_space_write_4(bst, bsh_scu, SCU_INV_ALL_REG, 0xff);
     85  1.7  aymeric 	bus_space_write_4(bst, bsh_scu, SCU_CTL,
     86  1.7  aymeric 		bus_space_read_4(bst, bsh_scu, SCU_CTL) | SCU_CTL_SCU_ENA);
     87  1.1  aymeric 
     88  1.3    skrll 	const uint32_t startfunc = (uint32_t) KERN_VTOPHYS((vaddr_t)cpu_mpstart);
     89  1.1  aymeric 
     90  1.1  aymeric 	/*
     91  1.1  aymeric 	 * We place a "B cortex_mpstart" at address 0 in order to bootstrap
     92  1.1  aymeric 	 * CPU 1. We can't use the similar feature of the Boot ROM because
     93  1.1  aymeric 	 * it was unmapped by u-boot in favor of the SDRAM. Plus the dtb is
     94  1.1  aymeric 	 * stored very low in RAM so we can't re-map the Boot ROM easily.
     95  1.1  aymeric 	 */
     96  1.5  aymeric 	pmap_map_chunk(kernel_l1pt.pv_va, CYCV_SDRAM_VBASE, CYCV_SDRAM_BASE,
     97  1.5  aymeric 		L1_S_SIZE, VM_PROT_READ|VM_PROT_WRITE, PMAP_NOCACHE);
     98  1.3    skrll 	*(volatile uint32_t *) CYCV_SDRAM_VBASE =
     99  1.3    skrll 	    htole32(0xea000000 | ((startfunc - 8 - 0x0) >> 2));
    100  1.5  aymeric 	pmap_unmap_chunk(kernel_l1pt.pv_va, CYCV_SDRAM_VBASE, L1_S_SIZE);
    101  1.1  aymeric 
    102  1.1  aymeric 	arm_cpu_max = 2;
    103  1.1  aymeric 
    104  1.1  aymeric 	bus_space_write_4(bst, bsh_rst, CYCV_RSTMGR_MPUMODRST,
    105  1.1  aymeric 		bus_space_read_4(bst, bsh_rst, CYCV_RSTMGR_MPUMODRST) &
    106  1.1  aymeric 			~CYCV_RSTMGR_MPUMODRST_CPU1);
    107  1.1  aymeric }
    108  1.1  aymeric 
    109  1.1  aymeric static void
    110  1.1  aymeric cycv_platform_init_attach_args(struct fdt_attach_args *faa) {
    111  1.1  aymeric 	faa->faa_bst = &armv7_generic_bs_tag;
    112  1.1  aymeric 	faa->faa_a4x_bst = &armv7_generic_a4x_bs_tag;
    113  1.1  aymeric 	faa->faa_dmat = &arm_generic_dma_tag;
    114  1.1  aymeric }
    115  1.1  aymeric 
    116  1.1  aymeric static void
    117  1.1  aymeric cycv_platform_device_register(device_t dev, void *aux) {
    118  1.1  aymeric 	prop_dictionary_t dict = device_properties(dev);
    119  1.1  aymeric 
    120  1.1  aymeric 	if (device_is_a(dev, "arma9tmr")) {
    121  1.1  aymeric 		prop_dictionary_set_uint32(dict, "frequency",
    122  1.1  aymeric 			cycv_clkmgr_early_get_mpu_clk() / 4);
    123  1.1  aymeric 	}
    124  1.1  aymeric }
    125  1.1  aymeric 
    126  1.1  aymeric static void
    127  1.1  aymeric cycv_platform_reset(void) {
    128  1.1  aymeric 	bus_space_tag_t bst = &armv7_generic_bs_tag;
    129  1.1  aymeric 	bus_space_handle_t bsh;
    130  1.1  aymeric 	uint32_t val;
    131  1.1  aymeric 
    132  1.1  aymeric 	bus_space_map(bst, CYCV_RSTMGR_BASE, CYCV_RSTMGR_SIZE, 0, &bsh);
    133  1.1  aymeric 	val = bus_space_read_4(bst, bsh, CYCV_RSTMGR_CTRL);
    134  1.1  aymeric 	bus_space_write_4(bst, bsh, CYCV_RSTMGR_CTRL,
    135  1.1  aymeric 		val | CYCV_RSTMGR_CTRL_SWCOLDRSTREQ);
    136  1.1  aymeric }
    137  1.1  aymeric 
    138  1.1  aymeric static u_int
    139  1.1  aymeric cycv_platform_uart_freq(void) {
    140  1.1  aymeric 	return cycv_clkmgr_early_get_l4_sp_clk();
    141  1.1  aymeric }
    142  1.1  aymeric 
    143  1.1  aymeric static const struct arm_platform cycv_platform = {
    144  1.1  aymeric 	.ap_devmap = cycv_platform_devmap,
    145  1.1  aymeric 	.ap_bootstrap = cycv_platform_bootstrap,
    146  1.1  aymeric 	.ap_init_attach_args = cycv_platform_init_attach_args,
    147  1.1  aymeric 	.ap_device_register = cycv_platform_device_register,
    148  1.1  aymeric 	.ap_reset = cycv_platform_reset,
    149  1.1  aymeric 	.ap_delay = a9tmr_delay,
    150  1.1  aymeric 	.ap_uart_freq = cycv_platform_uart_freq,
    151  1.3    skrll 	.ap_mpstart = cycv_mpstart,
    152  1.1  aymeric };
    153  1.1  aymeric 
    154  1.1  aymeric ARM_PLATFORM(cycv, "altr,socfpga-cyclone5", &cycv_platform);
    155