Home | History | Annotate | Line # | Download | only in fdt
arm_fdt.c revision 1.20
      1  1.20  jmcneill /* $NetBSD: arm_fdt.c,v 1.20 2021/10/10 13:03:09 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.5  jmcneill #include "opt_arm_timer.h"
     30  1.12  jmcneill #include "opt_efi.h"
     31  1.11  jmcneill #include "opt_modular.h"
     32   1.5  jmcneill 
     33   1.1  jmcneill #include <sys/cdefs.h>
     34  1.20  jmcneill __KERNEL_RCSID(0, "$NetBSD: arm_fdt.c,v 1.20 2021/10/10 13:03:09 jmcneill Exp $");
     35   1.1  jmcneill 
     36   1.1  jmcneill #include <sys/param.h>
     37   1.1  jmcneill #include <sys/systm.h>
     38   1.7     skrll #include <sys/cpu.h>
     39   1.1  jmcneill #include <sys/device.h>
     40   1.2  jmcneill #include <sys/kmem.h>
     41   1.2  jmcneill #include <sys/bus.h>
     42  1.11  jmcneill #include <sys/module.h>
     43  1.11  jmcneill 
     44  1.11  jmcneill #include <uvm/uvm_extern.h>
     45   1.1  jmcneill 
     46   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     47   1.1  jmcneill #include <dev/ofw/openfirm.h>
     48   1.1  jmcneill 
     49   1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     50   1.1  jmcneill 
     51  1.18  jmcneill #include <arm/locore.h>
     52  1.18  jmcneill 
     53  1.12  jmcneill #ifdef EFI_RUNTIME
     54  1.12  jmcneill #include <arm/arm/efi_runtime.h>
     55  1.12  jmcneill #include <dev/clock_subr.h>
     56  1.12  jmcneill #endif
     57  1.12  jmcneill 
     58   1.1  jmcneill static int	arm_fdt_match(device_t, cfdata_t, void *);
     59   1.1  jmcneill static void	arm_fdt_attach(device_t, device_t, void *);
     60   1.1  jmcneill 
     61  1.15  jmcneill static void	arm_fdt_irq_default_handler(void *);
     62  1.18  jmcneill static void	arm_fdt_fiq_default_handler(void *);
     63  1.15  jmcneill 
     64  1.12  jmcneill #ifdef EFI_RUNTIME
     65  1.12  jmcneill static void	arm_fdt_efi_init(device_t);
     66  1.12  jmcneill static int	arm_fdt_efi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
     67  1.12  jmcneill static int	arm_fdt_efi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
     68  1.12  jmcneill 
     69  1.12  jmcneill static struct todr_chip_handle efi_todr;
     70  1.12  jmcneill #endif
     71  1.12  jmcneill 
     72   1.1  jmcneill CFATTACH_DECL_NEW(arm_fdt, 0,
     73   1.1  jmcneill     arm_fdt_match, arm_fdt_attach, NULL, NULL);
     74   1.1  jmcneill 
     75   1.2  jmcneill struct arm_fdt_cpu_hatch_cb {
     76   1.2  jmcneill 	TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
     77   1.2  jmcneill 	void (*cb)(void *, struct cpu_info *);
     78   1.2  jmcneill 	void *priv;
     79   1.2  jmcneill };
     80   1.2  jmcneill 
     81   1.2  jmcneill static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
     82   1.2  jmcneill     TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
     83   1.2  jmcneill 
     84  1.15  jmcneill static void (*_arm_fdt_irq_handler)(void *) = arm_fdt_irq_default_handler;
     85  1.18  jmcneill static void (*_arm_fdt_fiq_handler)(void *) = arm_fdt_fiq_default_handler;
     86   1.5  jmcneill static void (*_arm_fdt_timer_init)(void) = NULL;
     87   1.3  jmcneill 
     88   1.1  jmcneill int
     89   1.1  jmcneill arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
     90   1.1  jmcneill {
     91   1.1  jmcneill 	return 1;
     92   1.1  jmcneill }
     93   1.1  jmcneill 
     94   1.1  jmcneill void
     95   1.1  jmcneill arm_fdt_attach(device_t parent, device_t self, void *aux)
     96   1.1  jmcneill {
     97   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
     98   1.1  jmcneill 	struct fdt_attach_args faa;
     99   1.1  jmcneill 
    100   1.1  jmcneill 	aprint_naive("\n");
    101   1.1  jmcneill 	aprint_normal("\n");
    102   1.1  jmcneill 
    103  1.18  jmcneill 	DISABLE_INTERRUPT();
    104  1.18  jmcneill 
    105  1.12  jmcneill #ifdef EFI_RUNTIME
    106  1.12  jmcneill 	arm_fdt_efi_init(self);
    107  1.12  jmcneill #endif
    108  1.12  jmcneill 
    109   1.8     skrll 	plat->ap_init_attach_args(&faa);
    110   1.1  jmcneill 	faa.faa_name = "";
    111   1.1  jmcneill 	faa.faa_phandle = OF_peer(0);
    112   1.1  jmcneill 
    113  1.17   thorpej 	config_found(self, &faa, NULL, CFARGS_NONE);
    114   1.1  jmcneill }
    115   1.1  jmcneill 
    116   1.1  jmcneill const struct arm_platform *
    117   1.1  jmcneill arm_fdt_platform(void)
    118   1.1  jmcneill {
    119   1.1  jmcneill 	static const struct arm_platform_info *booted_platform = NULL;
    120  1.10  jmcneill 	__link_set_decl(arm_platforms, struct arm_platform_info);
    121  1.10  jmcneill 	struct arm_platform_info * const *info;
    122   1.1  jmcneill 
    123   1.1  jmcneill 	if (booted_platform == NULL) {
    124   1.1  jmcneill 		const struct arm_platform_info *best_info = NULL;
    125   1.1  jmcneill 		const int phandle = OF_peer(0);
    126   1.1  jmcneill 		int match, best_match = 0;
    127   1.1  jmcneill 
    128   1.1  jmcneill 		__link_set_foreach(info, arm_platforms) {
    129  1.14   thorpej 			const struct device_compatible_entry compat_data[] = {
    130  1.14   thorpej 				{ .compat = (*info)->api_compat },
    131  1.14   thorpej 				DEVICE_COMPAT_EOL
    132  1.14   thorpej 			};
    133  1.14   thorpej 
    134  1.14   thorpej 			match = of_compatible_match(phandle, compat_data);
    135   1.1  jmcneill 			if (match > best_match) {
    136   1.1  jmcneill 				best_match = match;
    137   1.1  jmcneill 				best_info = *info;
    138   1.1  jmcneill 			}
    139   1.1  jmcneill 		}
    140   1.1  jmcneill 
    141   1.1  jmcneill 		booted_platform = best_info;
    142   1.1  jmcneill 	}
    143   1.1  jmcneill 
    144  1.10  jmcneill 	/*
    145  1.10  jmcneill 	 * No SoC specific platform was found. Try to find a generic
    146  1.10  jmcneill 	 * platform definition and use that if available.
    147  1.10  jmcneill 	 */
    148  1.10  jmcneill 	if (booted_platform == NULL) {
    149  1.10  jmcneill 		__link_set_foreach(info, arm_platforms) {
    150  1.10  jmcneill 			if (strcmp((*info)->api_compat, ARM_PLATFORM_DEFAULT) == 0) {
    151  1.10  jmcneill 				booted_platform = *info;
    152  1.10  jmcneill 				break;
    153  1.10  jmcneill 			}
    154  1.10  jmcneill 		}
    155  1.10  jmcneill 	}
    156  1.10  jmcneill 
    157   1.8     skrll 	return booted_platform == NULL ? NULL : booted_platform->api_ops;
    158   1.1  jmcneill }
    159   1.2  jmcneill 
    160   1.2  jmcneill void
    161   1.2  jmcneill arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
    162   1.2  jmcneill {
    163   1.2  jmcneill 	struct arm_fdt_cpu_hatch_cb *c;
    164   1.2  jmcneill 
    165   1.2  jmcneill 	c = kmem_alloc(sizeof(*c), KM_SLEEP);
    166   1.2  jmcneill 	c->priv = priv;
    167   1.2  jmcneill 	c->cb = cb;
    168   1.2  jmcneill 	TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
    169   1.2  jmcneill }
    170   1.2  jmcneill 
    171   1.2  jmcneill void
    172   1.2  jmcneill arm_fdt_cpu_hatch(struct cpu_info *ci)
    173   1.2  jmcneill {
    174   1.2  jmcneill 	struct arm_fdt_cpu_hatch_cb *c;
    175   1.2  jmcneill 
    176   1.2  jmcneill 	TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
    177   1.2  jmcneill 		c->cb(c->priv, ci);
    178   1.2  jmcneill }
    179   1.3  jmcneill 
    180  1.15  jmcneill static void
    181  1.15  jmcneill arm_fdt_irq_default_handler(void *frame)
    182  1.15  jmcneill {
    183  1.18  jmcneill 	panic("No IRQ handler installed");
    184  1.18  jmcneill }
    185  1.18  jmcneill 
    186  1.18  jmcneill static void
    187  1.18  jmcneill arm_fdt_fiq_default_handler(void *frame)
    188  1.18  jmcneill {
    189  1.18  jmcneill 	panic("No FIQ handler installed");
    190  1.15  jmcneill }
    191  1.15  jmcneill 
    192   1.3  jmcneill void
    193   1.3  jmcneill arm_fdt_irq_set_handler(void (*irq_handler)(void *))
    194   1.3  jmcneill {
    195  1.15  jmcneill 	KASSERT(_arm_fdt_irq_handler == arm_fdt_irq_default_handler);
    196   1.3  jmcneill 	_arm_fdt_irq_handler = irq_handler;
    197   1.3  jmcneill }
    198   1.3  jmcneill 
    199   1.3  jmcneill void
    200  1.18  jmcneill arm_fdt_fiq_set_handler(void (*fiq_handler)(void *))
    201  1.18  jmcneill {
    202  1.18  jmcneill 	KASSERT(_arm_fdt_fiq_handler == arm_fdt_fiq_default_handler);
    203  1.18  jmcneill 	_arm_fdt_fiq_handler = fiq_handler;
    204  1.18  jmcneill }
    205  1.18  jmcneill 
    206  1.18  jmcneill void
    207   1.3  jmcneill arm_fdt_irq_handler(void *tf)
    208   1.3  jmcneill {
    209   1.3  jmcneill 	_arm_fdt_irq_handler(tf);
    210   1.3  jmcneill }
    211   1.4  jmcneill 
    212   1.4  jmcneill void
    213  1.18  jmcneill arm_fdt_fiq_handler(void *tf)
    214  1.18  jmcneill {
    215  1.18  jmcneill 	_arm_fdt_fiq_handler(tf);
    216  1.18  jmcneill }
    217  1.18  jmcneill 
    218  1.18  jmcneill void
    219   1.5  jmcneill arm_fdt_timer_register(void (*timerfn)(void))
    220   1.5  jmcneill {
    221   1.5  jmcneill 	if (_arm_fdt_timer_init != NULL) {
    222   1.5  jmcneill #ifdef DIAGNOSTIC
    223   1.5  jmcneill 		aprint_verbose("%s: timer already registered\n", __func__);
    224   1.5  jmcneill #endif
    225   1.5  jmcneill 		return;
    226   1.5  jmcneill 	}
    227   1.5  jmcneill 	_arm_fdt_timer_init = timerfn;
    228   1.5  jmcneill }
    229   1.5  jmcneill 
    230   1.5  jmcneill #ifdef __HAVE_GENERIC_CPU_INITCLOCKS
    231   1.5  jmcneill void
    232   1.5  jmcneill cpu_initclocks(void)
    233   1.5  jmcneill {
    234   1.5  jmcneill 	if (_arm_fdt_timer_init == NULL)
    235   1.5  jmcneill 		panic("cpu_initclocks: no timer registered");
    236   1.5  jmcneill 	_arm_fdt_timer_init();
    237  1.18  jmcneill 	ENABLE_INTERRUPT();
    238   1.5  jmcneill }
    239   1.5  jmcneill #endif
    240  1.11  jmcneill 
    241  1.11  jmcneill void
    242  1.11  jmcneill arm_fdt_module_init(void)
    243  1.11  jmcneill {
    244  1.11  jmcneill #ifdef MODULAR
    245  1.11  jmcneill 	const int chosen = OF_finddevice("/chosen");
    246  1.11  jmcneill 	const char *module_name;
    247  1.11  jmcneill 	const uint64_t *data;
    248  1.11  jmcneill 	u_int index;
    249  1.11  jmcneill 	paddr_t pa;
    250  1.11  jmcneill 	vaddr_t va;
    251  1.11  jmcneill 	int len;
    252  1.11  jmcneill 
    253  1.11  jmcneill 	if (chosen == -1)
    254  1.11  jmcneill 		return;
    255  1.11  jmcneill 
    256  1.11  jmcneill 	data = fdtbus_get_prop(chosen, "netbsd,modules", &len);
    257  1.11  jmcneill 	if (data == NULL)
    258  1.11  jmcneill 		return;
    259  1.11  jmcneill 
    260  1.11  jmcneill 	for (index = 0; index < len / 16; index++, data += 2) {
    261  1.11  jmcneill 		module_name = fdtbus_get_string_index(chosen,
    262  1.11  jmcneill 		    "netbsd,module-names", index);
    263  1.11  jmcneill 		if (module_name == NULL)
    264  1.11  jmcneill 			break;
    265  1.11  jmcneill 
    266  1.11  jmcneill 		const paddr_t startpa = (paddr_t)be64dec(data + 0);
    267  1.11  jmcneill 		const size_t size = (size_t)be64dec(data + 1);
    268  1.11  jmcneill 		const paddr_t endpa = round_page(startpa + size);
    269  1.11  jmcneill 
    270  1.11  jmcneill 		const vaddr_t startva = uvm_km_alloc(kernel_map, endpa - startpa,
    271  1.11  jmcneill 		    0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    272  1.11  jmcneill 		if (startva == 0) {
    273  1.11  jmcneill 			printf("ERROR: Cannot allocate VA for module %s\n",
    274  1.11  jmcneill 			    module_name);
    275  1.11  jmcneill 			continue;
    276  1.11  jmcneill 		}
    277  1.11  jmcneill 
    278  1.11  jmcneill 		for (pa = startpa, va = startva;
    279  1.11  jmcneill 		     pa < endpa;
    280  1.11  jmcneill 		     pa += PAGE_SIZE, va += PAGE_SIZE) {
    281  1.13     skrll 			pmap_kenter_pa(va, pa, VM_PROT_ALL, 0);
    282  1.11  jmcneill 		}
    283  1.11  jmcneill 		pmap_update(pmap_kernel());
    284  1.11  jmcneill 
    285  1.11  jmcneill 		module_prime(module_name, (void *)(uintptr_t)startva, size);
    286  1.11  jmcneill 	}
    287  1.11  jmcneill #endif /* !MODULAR */
    288  1.11  jmcneill }
    289  1.12  jmcneill 
    290  1.12  jmcneill #ifdef EFI_RUNTIME
    291  1.12  jmcneill static void
    292  1.12  jmcneill arm_fdt_efi_init(device_t dev)
    293  1.12  jmcneill {
    294  1.12  jmcneill 	uint64_t efi_system_table;
    295  1.12  jmcneill 	struct efi_tm tm;
    296  1.12  jmcneill 	int error;
    297  1.12  jmcneill 
    298  1.12  jmcneill 	const int chosen = OF_finddevice("/chosen");
    299  1.12  jmcneill 	if (chosen < 0)
    300  1.12  jmcneill 		return;
    301  1.12  jmcneill 
    302  1.12  jmcneill 	if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0)
    303  1.12  jmcneill 		return;
    304  1.12  jmcneill 
    305  1.12  jmcneill 	error = arm_efirt_init(efi_system_table);
    306  1.12  jmcneill 	if (error)
    307  1.12  jmcneill 		return;
    308  1.12  jmcneill 
    309  1.12  jmcneill 	aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table);
    310  1.12  jmcneill 
    311  1.20  jmcneill 	if (arm_efirt_gettime(&tm, NULL) == 0) {
    312  1.12  jmcneill 		aprint_normal_dev(dev, "using EFI runtime services for RTC\n");
    313  1.12  jmcneill 		efi_todr.cookie = NULL;
    314  1.12  jmcneill 		efi_todr.todr_gettime_ymdhms = arm_fdt_efi_rtc_gettime;
    315  1.12  jmcneill 		efi_todr.todr_settime_ymdhms = arm_fdt_efi_rtc_settime;
    316  1.12  jmcneill 		todr_attach(&efi_todr);
    317  1.12  jmcneill 	}
    318  1.12  jmcneill }
    319  1.12  jmcneill 
    320  1.12  jmcneill static int
    321  1.12  jmcneill arm_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    322  1.12  jmcneill {
    323  1.12  jmcneill 	struct efi_tm tm;
    324  1.20  jmcneill 	efi_status status;
    325  1.12  jmcneill 
    326  1.20  jmcneill 	status = arm_efirt_gettime(&tm, NULL);
    327  1.20  jmcneill 	if (status != 0)
    328  1.20  jmcneill 		return EIO;
    329  1.12  jmcneill 
    330  1.12  jmcneill 	dt->dt_year = tm.tm_year;
    331  1.12  jmcneill 	dt->dt_mon = tm.tm_mon;
    332  1.12  jmcneill 	dt->dt_day = tm.tm_mday;
    333  1.12  jmcneill 	dt->dt_wday = 0;
    334  1.12  jmcneill 	dt->dt_hour = tm.tm_hour;
    335  1.12  jmcneill 	dt->dt_min = tm.tm_min;
    336  1.12  jmcneill 	dt->dt_sec = tm.tm_sec;
    337  1.12  jmcneill 
    338  1.12  jmcneill 	return 0;
    339  1.12  jmcneill }
    340  1.12  jmcneill 
    341  1.12  jmcneill static int
    342  1.12  jmcneill arm_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    343  1.12  jmcneill {
    344  1.12  jmcneill 	struct efi_tm tm;
    345  1.20  jmcneill 	efi_status status;
    346  1.12  jmcneill 
    347  1.12  jmcneill 	memset(&tm, 0, sizeof(tm));
    348  1.12  jmcneill 	tm.tm_year = dt->dt_year;
    349  1.12  jmcneill 	tm.tm_mon = dt->dt_mon;
    350  1.12  jmcneill 	tm.tm_mday = dt->dt_day;
    351  1.12  jmcneill 	tm.tm_hour = dt->dt_hour;
    352  1.12  jmcneill 	tm.tm_min = dt->dt_min;
    353  1.12  jmcneill 	tm.tm_sec = dt->dt_sec;
    354  1.12  jmcneill 
    355  1.20  jmcneill 	status = arm_efirt_settime(&tm);
    356  1.20  jmcneill 	if (status != 0)
    357  1.20  jmcneill 		return EIO;
    358  1.20  jmcneill 
    359  1.20  jmcneill 	return 0;
    360  1.12  jmcneill }
    361  1.12  jmcneill #endif
    362