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