1 /* $NetBSD: ar_conf.c,v 1.3 2015/06/09 22:50:50 matt Exp $ */ 2 /*- 3 * Copyright (c) 2011 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Matt Thomas of 3am Software Foundry. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 33 __KERNEL_RCSID(0, "$NetBSD: ar_conf.c,v 1.3 2015/06/09 22:50:50 matt Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/cpu.h> 37 38 #include "opt_wisoc.h" 39 40 #include <mips/cpuregs.h> 41 #include <mips/locore.h> 42 43 #include <mips/atheros/include/platform.h> 44 #include <mips/atheros/include/ar9344reg.h> 45 46 struct atheros_chip { 47 const struct atheros_platformsw *ac_platformsw; 48 const struct atheros_boardsw *ac_boardsw; 49 uint8_t ac_chipid; 50 uint8_t ac_chipmask; 51 uint8_t ac_cid; 52 uint8_t ac_pid; 53 const char ac_name[8]; 54 }; 55 56 static const struct atheros_chip chips[] = { 57 #ifdef WISOC_AR5312 58 { 59 .ac_platformsw = &ar5312_platformsw, 60 .ac_boardsw = &ar5312_boardsw, 61 .ac_chipid = ARCHIP_AR5312, 62 .ac_chipmask = 0xff, 63 .ac_cid = MIPS_PRID_CID_MTI, 64 .ac_pid = MIPS_4Kc, 65 .ac_name = "AR5312" 66 }, 67 #endif 68 #ifdef WISOC_AR5315 69 { 70 .ac_platformsw = &ar5315_platformsw, 71 .ac_boardsw = &ar5315_boardsw, 72 .ac_chipid = ARCHIP_AR5315, 73 .ac_chipmask = 0xf0, 74 .ac_cid = MIPS_PRID_CID_MTI, 75 .ac_pid = MIPS_4Kc, 76 .ac_name = "AR5315" 77 }, 78 #endif 79 #ifdef WISOC_AR7100 80 { 81 .ac_platformsw = &ar7100_platformsw, 82 .ac_chipid = ARCHIP_AR7130, 83 .ac_chipmask = 0xf3, 84 .ac_cid = MIPS_PRID_CID_MTI, 85 .ac_pid = MIPS_24K, 86 .ac_name = "AR7130" 87 }, { 88 .ac_platformsw = &ar7100_platformsw, 89 .ac_chipid = ARCHIP_AR7141, 90 .ac_chipmask = 0xf3, 91 .ac_cid = MIPS_PRID_CID_MTI, 92 .ac_pid = MIPS_24K, 93 .ac_name = "AR7141" 94 }, { 95 .ac_platformsw = &ar7100_platformsw, 96 .ac_chipid = ARCHIP_AR7161, 97 .ac_chipmask = 0xf3, 98 .ac_cid = MIPS_PRID_CID_MTI, 99 .ac_pid = MIPS_24K, 100 .ac_name = "AR7161" 101 }, 102 #endif 103 #ifdef WISOC_AR9344 104 { 105 .ac_platformsw = &ar9344_platformsw, 106 .ac_chipid = 0x20, /* 7240? */ 107 .ac_chipmask = 0xff, 108 .ac_cid = MIPS_PRID_CID_MTI, 109 .ac_pid = MIPS_74K, 110 .ac_name = "AR7240" 111 }, { 112 .ac_platformsw = &ar9344_platformsw, 113 .ac_chipid = ARCHIP_AR9344, 114 .ac_chipmask = 0xff, 115 .ac_cid = MIPS_PRID_CID_MTI, 116 .ac_pid = MIPS_74K, 117 .ac_name = "AR9344" 118 }, 119 #endif 120 }; 121 122 __CTASSERT(__arraycount(chips) > 0); 123 124 const struct atheros_platformsw *platformsw; 125 126 static const struct atheros_chip *my_chip; 127 128 static struct arfreqs chip_freqs; 129 130 const char * 131 atheros_get_cpuname(void) 132 { 133 return my_chip->ac_name; 134 } 135 136 u_int 137 atheros_get_chipid(void) 138 { 139 return my_chip->ac_chipid; 140 } 141 142 uint32_t 143 atheros_get_uart_freq(void) 144 { 145 if (chip_freqs.freq_uart) 146 return chip_freqs.freq_uart; 147 148 return chip_freqs.freq_bus; 149 } 150 151 uint32_t 152 atheros_get_bus_freq(void) 153 { 154 return chip_freqs.freq_bus; 155 } 156 157 uint32_t 158 atheros_get_cpu_freq(void) 159 { 160 return chip_freqs.freq_cpu; 161 } 162 163 uint32_t 164 atheros_get_mem_freq(void) 165 { 166 return chip_freqs.freq_mem; 167 } 168 169 void 170 atheros_set_platformsw(void) 171 { 172 const u_int cid = MIPS_PRID_CID(mips_options.mips_cpu_id); 173 const u_int pid = MIPS_PRID_IMPL(mips_options.mips_cpu_id); 174 175 for (const struct atheros_chip *ac = chips; 176 ac < chips + __arraycount(chips); 177 ac++) { 178 const struct atheros_platformsw * const apsw = ac->ac_platformsw; 179 if (cid != ac->ac_cid || pid != ac->ac_pid) 180 continue; 181 182 const uint32_t revision_id = *(volatile uint32_t *) 183 MIPS_PHYS_TO_KSEG1(apsw->apsw_revision_id_addr); 184 const uint32_t chipid = AR9344_REVISION_CHIPID(revision_id); 185 186 if ((chipid & ac->ac_chipmask) == ac->ac_chipid) { 187 platformsw = apsw; 188 my_chip = ac; 189 atheros_early_consinit(); 190 printf("Early console started!\n"); 191 (*apsw->apsw_get_freqs)(&chip_freqs); 192 printf("freqs: cpu=%u bus=%u mem=%u ref=%u pll=%u\n", 193 chip_freqs.freq_cpu, 194 chip_freqs.freq_bus, 195 chip_freqs.freq_mem, 196 chip_freqs.freq_ref, 197 chip_freqs.freq_pll); 198 return; 199 } 200 } 201 panic("%s: unrecognized platform", __func__); 202 } 203 204 #include "ath_arbus.h" 205 #if NATH_ARBUS > 0 206 #include <ah_soc.h> 207 208 const struct ar531x_boarddata * 209 atheros_get_board_info(void) 210 { 211 const struct atheros_boardsw * const boardsw = my_chip->ac_boardsw; 212 if (boardsw == NULL) 213 return NULL; 214 return (*boardsw->absw_get_board_info)(); 215 } 216 217 /* 218 * Locate board and radio configuration data in flash. 219 */ 220 int 221 atheros_get_board_config(struct ar531x_config *config) 222 { 223 const struct atheros_boardsw * const boardsw = my_chip->ac_boardsw; 224 if (boardsw == NULL) 225 return ENOENT; 226 227 config->board = (*boardsw->absw_get_board_info)(); 228 if (config->board == NULL) 229 return ENOENT; 230 231 config->radio = (*boardsw->absw_get_radio_info)(); 232 if (config->radio == NULL) 233 return ENOENT; /* XXX distinct code */ 234 235 return 0; 236 } 237 #endif /* NATH_ARBUS > 0 */ 238