Home | History | Annotate | Line # | Download | only in fdt
arm_fdt.c revision 1.7
      1 /* $NetBSD: arm_fdt.c,v 1.7 2017/12/10 21:38:26 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "opt_arm_timer.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: arm_fdt.c,v 1.7 2017/12/10 21:38:26 skrll Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/cpu.h>
     37 #include <sys/device.h>
     38 #include <sys/kmem.h>
     39 #include <sys/bus.h>
     40 
     41 #include <dev/fdt/fdtvar.h>
     42 #include <dev/ofw/openfirm.h>
     43 
     44 #include <arm/fdt/arm_fdtvar.h>
     45 
     46 static int	arm_fdt_match(device_t, cfdata_t, void *);
     47 static void	arm_fdt_attach(device_t, device_t, void *);
     48 
     49 CFATTACH_DECL_NEW(arm_fdt, 0,
     50     arm_fdt_match, arm_fdt_attach, NULL, NULL);
     51 
     52 static struct arm_platlist arm_platform_list =
     53     TAILQ_HEAD_INITIALIZER(arm_platform_list);
     54 
     55 struct arm_fdt_cpu_hatch_cb {
     56 	TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
     57 	void (*cb)(void *, struct cpu_info *);
     58 	void *priv;
     59 };
     60 
     61 static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
     62     TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
     63 
     64 static void (*_arm_fdt_irq_handler)(void *) = NULL;
     65 static void (*_arm_fdt_timer_init)(void) = NULL;
     66 
     67 int
     68 arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
     69 {
     70 	return 1;
     71 }
     72 
     73 void
     74 arm_fdt_attach(device_t parent, device_t self, void *aux)
     75 {
     76 	const struct arm_platform *plat = arm_fdt_platform();
     77 	struct fdt_attach_args faa;
     78 
     79 	aprint_naive("\n");
     80 	aprint_normal("\n");
     81 
     82 	plat->init_attach_args(&faa);
     83 	faa.faa_name = "";
     84 	faa.faa_phandle = OF_peer(0);
     85 
     86 	config_found(self, &faa, NULL);
     87 }
     88 
     89 const struct arm_platform *
     90 arm_fdt_platform(void)
     91 {
     92 	static const struct arm_platform_info *booted_platform = NULL;
     93 
     94 	if (booted_platform == NULL) {
     95 		__link_set_decl(arm_platforms, struct arm_platform_info);
     96 		struct arm_platform_info * const *info;
     97 		const struct arm_platform_info *best_info = NULL;
     98 		const int phandle = OF_peer(0);
     99 		int match, best_match = 0;
    100 
    101 		__link_set_foreach(info, arm_platforms) {
    102 			const char * const compat[] = { (*info)->compat, NULL };
    103 			match = of_match_compatible(phandle, compat);
    104 			if (match > best_match) {
    105 				best_match = match;
    106 				best_info = *info;
    107 			}
    108 		}
    109 
    110 		booted_platform = best_info;
    111 	}
    112 
    113 	return booted_platform == NULL ? NULL : booted_platform->ops;
    114 }
    115 
    116 void
    117 arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
    118 {
    119 	struct arm_fdt_cpu_hatch_cb *c;
    120 
    121 	c = kmem_alloc(sizeof(*c), KM_SLEEP);
    122 	c->priv = priv;
    123 	c->cb = cb;
    124 	TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
    125 }
    126 
    127 void
    128 arm_fdt_cpu_hatch(struct cpu_info *ci)
    129 {
    130 	struct arm_fdt_cpu_hatch_cb *c;
    131 
    132 	TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
    133 		c->cb(c->priv, ci);
    134 }
    135 
    136 void
    137 arm_fdt_irq_set_handler(void (*irq_handler)(void *))
    138 {
    139 	KASSERT(_arm_fdt_irq_handler == NULL);
    140 	_arm_fdt_irq_handler = irq_handler;
    141 }
    142 
    143 void
    144 arm_fdt_irq_handler(void *tf)
    145 {
    146 	_arm_fdt_irq_handler(tf);
    147 }
    148 
    149 void
    150 arm_fdt_timer_register(void (*timerfn)(void))
    151 {
    152 	if (_arm_fdt_timer_init != NULL) {
    153 #ifdef DIAGNOSTIC
    154 		aprint_verbose("%s: timer already registered\n", __func__);
    155 #endif
    156 		return;
    157 	}
    158 	_arm_fdt_timer_init = timerfn;
    159 }
    160 
    161 void
    162 arm_fdt_memory_dump(paddr_t pa)
    163 {
    164 	const struct arm_platform *plat = arm_fdt_platform();
    165 	struct fdt_attach_args faa;
    166 	bus_space_tag_t bst;
    167 	bus_space_handle_t bsh;
    168 
    169 	plat->init_attach_args(&faa);
    170 
    171 	bst = faa.faa_bst;
    172 	bus_space_map(bst, pa, 0x100, 0, &bsh);
    173 
    174 	for (int i = 0; i < 0x100; i += 0x10) {
    175 		printf("%" PRIxPTR ": %08x %08x %08x %08x\n",
    176 		    (uintptr_t)(pa + i),
    177 		    bus_space_read_4(bst, bsh, i + 0),
    178 		    bus_space_read_4(bst, bsh, i + 4),
    179 		    bus_space_read_4(bst, bsh, i + 8),
    180 		    bus_space_read_4(bst, bsh, i + 12));
    181 	}
    182 }
    183 
    184 #ifdef __HAVE_GENERIC_CPU_INITCLOCKS
    185 void
    186 cpu_initclocks(void)
    187 {
    188 	if (_arm_fdt_timer_init == NULL)
    189 		panic("cpu_initclocks: no timer registered");
    190 	_arm_fdt_timer_init();
    191 }
    192 #endif
    193