xref: /openbmc/linux/arch/mips/ath25/board.c (revision 4bdc0d67)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2003 Atheros Communications, Inc.,  All Rights Reserved.
7  * Copyright (C) 2006 FON Technology, SL.
8  * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
9  * Copyright (C) 2006-2009 Felix Fietkau <nbd@openwrt.org>
10  */
11 
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <asm/irq_cpu.h>
15 #include <asm/reboot.h>
16 #include <asm/bootinfo.h>
17 #include <asm/time.h>
18 
19 #include <ath25_platform.h>
20 #include "devices.h"
21 #include "ar5312.h"
22 #include "ar2315.h"
23 
24 void (*ath25_irq_dispatch)(void);
25 
check_radio_magic(const void __iomem * addr)26 static inline bool check_radio_magic(const void __iomem *addr)
27 {
28 	addr += 0x7a; /* offset for flash magic */
29 	return (__raw_readb(addr) == 0x5a) && (__raw_readb(addr + 1) == 0xa5);
30 }
31 
check_notempty(const void __iomem * addr)32 static inline bool check_notempty(const void __iomem *addr)
33 {
34 	return __raw_readl(addr) != 0xffffffff;
35 }
36 
check_board_data(const void __iomem * addr,bool broken)37 static inline bool check_board_data(const void __iomem *addr, bool broken)
38 {
39 	/* config magic found */
40 	if (__raw_readl(addr) == ATH25_BD_MAGIC)
41 		return true;
42 
43 	if (!broken)
44 		return false;
45 
46 	/* broken board data detected, use radio data to find the
47 	 * offset, user will fix this */
48 
49 	if (check_radio_magic(addr + 0x1000))
50 		return true;
51 	if (check_radio_magic(addr + 0xf8))
52 		return true;
53 
54 	return false;
55 }
56 
find_board_config(const void __iomem * limit,const bool broken)57 static const void __iomem * __init find_board_config(const void __iomem *limit,
58 						     const bool broken)
59 {
60 	const void __iomem *addr;
61 	const void __iomem *begin = limit - 0x1000;
62 	const void __iomem *end = limit - 0x30000;
63 
64 	for (addr = begin; addr >= end; addr -= 0x1000)
65 		if (check_board_data(addr, broken))
66 			return addr;
67 
68 	return NULL;
69 }
70 
find_radio_config(const void __iomem * limit,const void __iomem * bcfg)71 static const void __iomem * __init find_radio_config(const void __iomem *limit,
72 						     const void __iomem *bcfg)
73 {
74 	const void __iomem *rcfg, *begin, *end;
75 
76 	/*
77 	 * Now find the start of Radio Configuration data, using heuristics:
78 	 * Search forward from Board Configuration data by 0x1000 bytes
79 	 * at a time until we find non-0xffffffff.
80 	 */
81 	begin = bcfg + 0x1000;
82 	end = limit;
83 	for (rcfg = begin; rcfg < end; rcfg += 0x1000)
84 		if (check_notempty(rcfg) && check_radio_magic(rcfg))
85 			return rcfg;
86 
87 	/* AR2316 relocates radio config to new location */
88 	begin = bcfg + 0xf8;
89 	end = limit - 0x1000 + 0xf8;
90 	for (rcfg = begin; rcfg < end; rcfg += 0x1000)
91 		if (check_notempty(rcfg) && check_radio_magic(rcfg))
92 			return rcfg;
93 
94 	return NULL;
95 }
96 
97 /*
98  * NB: Search region size could be larger than the actual flash size,
99  * but this shouldn't be a problem here, because the flash
100  * will simply be mapped multiple times.
101  */
ath25_find_config(phys_addr_t base,unsigned long size)102 int __init ath25_find_config(phys_addr_t base, unsigned long size)
103 {
104 	const void __iomem *flash_base, *flash_limit;
105 	struct ath25_boarddata *config;
106 	unsigned int rcfg_size;
107 	int broken_boarddata = 0;
108 	const void __iomem *bcfg, *rcfg;
109 	u8 *board_data;
110 	u8 *radio_data;
111 	u8 *mac_addr;
112 	u32 offset;
113 
114 	flash_base = ioremap(base, size);
115 	flash_limit = flash_base + size;
116 
117 	ath25_board.config = NULL;
118 	ath25_board.radio = NULL;
119 
120 	/* Copy the board and radio data to RAM, because accessing the mapped
121 	 * memory of the flash directly after booting is not safe */
122 
123 	/* Try to find valid board and radio data */
124 	bcfg = find_board_config(flash_limit, false);
125 
126 	/* If that fails, try to at least find valid radio data */
127 	if (!bcfg) {
128 		bcfg = find_board_config(flash_limit, true);
129 		broken_boarddata = 1;
130 	}
131 
132 	if (!bcfg) {
133 		pr_warn("WARNING: No board configuration data found!\n");
134 		goto error;
135 	}
136 
137 	board_data = kzalloc(BOARD_CONFIG_BUFSZ, GFP_KERNEL);
138 	if (!board_data)
139 		goto error;
140 	ath25_board.config = (struct ath25_boarddata *)board_data;
141 	memcpy_fromio(board_data, bcfg, 0x100);
142 	if (broken_boarddata) {
143 		pr_warn("WARNING: broken board data detected\n");
144 		config = ath25_board.config;
145 		if (is_zero_ether_addr(config->enet0_mac)) {
146 			pr_info("Fixing up empty mac addresses\n");
147 			config->reset_config_gpio = 0xffff;
148 			config->sys_led_gpio = 0xffff;
149 			eth_random_addr(config->wlan0_mac);
150 			config->wlan0_mac[0] &= ~0x06;
151 			eth_random_addr(config->enet0_mac);
152 			eth_random_addr(config->enet1_mac);
153 		}
154 	}
155 
156 	/* Radio config starts 0x100 bytes after board config, regardless
157 	 * of what the physical layout on the flash chip looks like */
158 
159 	rcfg = find_radio_config(flash_limit, bcfg);
160 	if (!rcfg) {
161 		pr_warn("WARNING: Could not find Radio Configuration data\n");
162 		goto error;
163 	}
164 
165 	radio_data = board_data + 0x100 + ((rcfg - bcfg) & 0xfff);
166 	ath25_board.radio = radio_data;
167 	offset = radio_data - board_data;
168 	pr_info("Radio config found at offset 0x%x (0x%x)\n", rcfg - bcfg,
169 		offset);
170 	rcfg_size = BOARD_CONFIG_BUFSZ - offset;
171 	memcpy_fromio(radio_data, rcfg, rcfg_size);
172 
173 	mac_addr = &radio_data[0x1d * 2];
174 	if (is_broadcast_ether_addr(mac_addr)) {
175 		pr_info("Radio MAC is blank; using board-data\n");
176 		ether_addr_copy(mac_addr, ath25_board.config->wlan0_mac);
177 	}
178 
179 	iounmap(flash_base);
180 
181 	return 0;
182 
183 error:
184 	iounmap(flash_base);
185 	return -ENODEV;
186 }
187 
ath25_halt(void)188 static void ath25_halt(void)
189 {
190 	local_irq_disable();
191 	unreachable();
192 }
193 
plat_mem_setup(void)194 void __init plat_mem_setup(void)
195 {
196 	_machine_halt = ath25_halt;
197 	pm_power_off = ath25_halt;
198 
199 	if (is_ar5312())
200 		ar5312_plat_mem_setup();
201 	else
202 		ar2315_plat_mem_setup();
203 
204 	/* Disable data watchpoints */
205 	write_c0_watchlo0(0);
206 }
207 
plat_irq_dispatch(void)208 asmlinkage void plat_irq_dispatch(void)
209 {
210 	ath25_irq_dispatch();
211 }
212 
plat_time_init(void)213 void __init plat_time_init(void)
214 {
215 	if (is_ar5312())
216 		ar5312_plat_time_init();
217 	else
218 		ar2315_plat_time_init();
219 }
220 
get_c0_compare_int(void)221 unsigned int get_c0_compare_int(void)
222 {
223 	return CP0_LEGACY_COMPARE_IRQ;
224 }
225 
arch_init_irq(void)226 void __init arch_init_irq(void)
227 {
228 	clear_c0_status(ST0_IM);
229 	mips_cpu_irq_init();
230 
231 	/* Initialize interrupt controllers */
232 	if (is_ar5312())
233 		ar5312_arch_init_irq();
234 	else
235 		ar2315_arch_init_irq();
236 }
237