1 /* $NetBSD: arm_fdt.c,v 1.6 2017/09/21 19:28:37 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 Jared D. McNeill <jmcneill@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.6 2017/09/21 19:28:37 skrll Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/device.h> 37 #include <sys/kmem.h> 38 #include <sys/bus.h> 39 40 #include <machine/cpu.h> 41 42 #include <dev/fdt/fdtvar.h> 43 #include <dev/ofw/openfirm.h> 44 45 #include <arm/fdt/arm_fdtvar.h> 46 47 static int arm_fdt_match(device_t, cfdata_t, void *); 48 static void arm_fdt_attach(device_t, device_t, void *); 49 50 CFATTACH_DECL_NEW(arm_fdt, 0, 51 arm_fdt_match, arm_fdt_attach, NULL, NULL); 52 53 static struct arm_platlist arm_platform_list = 54 TAILQ_HEAD_INITIALIZER(arm_platform_list); 55 56 struct arm_fdt_cpu_hatch_cb { 57 TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next; 58 void (*cb)(void *, struct cpu_info *); 59 void *priv; 60 }; 61 62 static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs = 63 TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs); 64 65 static void (*_arm_fdt_irq_handler)(void *) = NULL; 66 static void (*_arm_fdt_timer_init)(void) = NULL; 67 68 int 69 arm_fdt_match(device_t parent, cfdata_t cf, void *aux) 70 { 71 return 1; 72 } 73 74 void 75 arm_fdt_attach(device_t parent, device_t self, void *aux) 76 { 77 const struct arm_platform *plat = arm_fdt_platform(); 78 struct fdt_attach_args faa; 79 80 aprint_naive("\n"); 81 aprint_normal("\n"); 82 83 plat->init_attach_args(&faa); 84 faa.faa_name = ""; 85 faa.faa_phandle = OF_peer(0); 86 87 config_found(self, &faa, NULL); 88 } 89 90 const struct arm_platform * 91 arm_fdt_platform(void) 92 { 93 static const struct arm_platform_info *booted_platform = NULL; 94 95 if (booted_platform == NULL) { 96 __link_set_decl(arm_platforms, struct arm_platform_info); 97 struct arm_platform_info * const *info; 98 const struct arm_platform_info *best_info = NULL; 99 const int phandle = OF_peer(0); 100 int match, best_match = 0; 101 102 __link_set_foreach(info, arm_platforms) { 103 const char * const compat[] = { (*info)->compat, NULL }; 104 match = of_match_compatible(phandle, compat); 105 if (match > best_match) { 106 best_match = match; 107 best_info = *info; 108 } 109 } 110 111 booted_platform = best_info; 112 } 113 114 return booted_platform == NULL ? NULL : booted_platform->ops; 115 } 116 117 void 118 arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *)) 119 { 120 struct arm_fdt_cpu_hatch_cb *c; 121 122 c = kmem_alloc(sizeof(*c), KM_SLEEP); 123 c->priv = priv; 124 c->cb = cb; 125 TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next); 126 } 127 128 void 129 arm_fdt_cpu_hatch(struct cpu_info *ci) 130 { 131 struct arm_fdt_cpu_hatch_cb *c; 132 133 TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next) 134 c->cb(c->priv, ci); 135 } 136 137 void 138 arm_fdt_irq_set_handler(void (*irq_handler)(void *)) 139 { 140 KASSERT(_arm_fdt_irq_handler == NULL); 141 _arm_fdt_irq_handler = irq_handler; 142 } 143 144 void 145 arm_fdt_irq_handler(void *tf) 146 { 147 _arm_fdt_irq_handler(tf); 148 } 149 150 void 151 arm_fdt_timer_register(void (*timerfn)(void)) 152 { 153 if (_arm_fdt_timer_init != NULL) { 154 #ifdef DIAGNOSTIC 155 aprint_verbose("%s: timer already registered\n", __func__); 156 #endif 157 return; 158 } 159 _arm_fdt_timer_init = timerfn; 160 } 161 162 void 163 arm_fdt_memory_dump(paddr_t pa) 164 { 165 const struct arm_platform *plat = arm_fdt_platform(); 166 struct fdt_attach_args faa; 167 bus_space_tag_t bst; 168 bus_space_handle_t bsh; 169 170 plat->init_attach_args(&faa); 171 172 bst = faa.faa_bst; 173 bus_space_map(bst, pa, 0x100, 0, &bsh); 174 175 for (int i = 0; i < 0x100; i += 0x10) { 176 printf("%" PRIxPTR ": %08x %08x %08x %08x\n", 177 (uintptr_t)(pa + i), 178 bus_space_read_4(bst, bsh, i + 0), 179 bus_space_read_4(bst, bsh, i + 4), 180 bus_space_read_4(bst, bsh, i + 8), 181 bus_space_read_4(bst, bsh, i + 12)); 182 } 183 } 184 185 #ifdef __HAVE_GENERIC_CPU_INITCLOCKS 186 void 187 cpu_initclocks(void) 188 { 189 if (_arm_fdt_timer_init == NULL) 190 panic("cpu_initclocks: no timer registered"); 191 _arm_fdt_timer_init(); 192 } 193 #endif 194