18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2003 Atheros Communications, Inc.,  All Rights Reserved.
78c2ecf20Sopenharmony_ci * Copyright (C) 2006 FON Technology, SL.
88c2ecf20Sopenharmony_ci * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
98c2ecf20Sopenharmony_ci * Copyright (C) 2006-2009 Felix Fietkau <nbd@openwrt.org>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <asm/irq_cpu.h>
158c2ecf20Sopenharmony_ci#include <asm/reboot.h>
168c2ecf20Sopenharmony_ci#include <asm/bootinfo.h>
178c2ecf20Sopenharmony_ci#include <asm/time.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <ath25_platform.h>
208c2ecf20Sopenharmony_ci#include "devices.h"
218c2ecf20Sopenharmony_ci#include "ar5312.h"
228c2ecf20Sopenharmony_ci#include "ar2315.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_civoid (*ath25_irq_dispatch)(void);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic inline bool check_radio_magic(const void __iomem *addr)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	addr += 0x7a; /* offset for flash magic */
298c2ecf20Sopenharmony_ci	return (__raw_readb(addr) == 0x5a) && (__raw_readb(addr + 1) == 0xa5);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic inline bool check_notempty(const void __iomem *addr)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	return __raw_readl(addr) != 0xffffffff;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic inline bool check_board_data(const void __iomem *addr, bool broken)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	/* config magic found */
408c2ecf20Sopenharmony_ci	if (__raw_readl(addr) == ATH25_BD_MAGIC)
418c2ecf20Sopenharmony_ci		return true;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	if (!broken)
448c2ecf20Sopenharmony_ci		return false;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/* broken board data detected, use radio data to find the
478c2ecf20Sopenharmony_ci	 * offset, user will fix this */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (check_radio_magic(addr + 0x1000))
508c2ecf20Sopenharmony_ci		return true;
518c2ecf20Sopenharmony_ci	if (check_radio_magic(addr + 0xf8))
528c2ecf20Sopenharmony_ci		return true;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	return false;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic const void __iomem * __init find_board_config(const void __iomem *limit,
588c2ecf20Sopenharmony_ci						     const bool broken)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	const void __iomem *addr;
618c2ecf20Sopenharmony_ci	const void __iomem *begin = limit - 0x1000;
628c2ecf20Sopenharmony_ci	const void __iomem *end = limit - 0x30000;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	for (addr = begin; addr >= end; addr -= 0x1000)
658c2ecf20Sopenharmony_ci		if (check_board_data(addr, broken))
668c2ecf20Sopenharmony_ci			return addr;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	return NULL;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic const void __iomem * __init find_radio_config(const void __iomem *limit,
728c2ecf20Sopenharmony_ci						     const void __iomem *bcfg)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	const void __iomem *rcfg, *begin, *end;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/*
778c2ecf20Sopenharmony_ci	 * Now find the start of Radio Configuration data, using heuristics:
788c2ecf20Sopenharmony_ci	 * Search forward from Board Configuration data by 0x1000 bytes
798c2ecf20Sopenharmony_ci	 * at a time until we find non-0xffffffff.
808c2ecf20Sopenharmony_ci	 */
818c2ecf20Sopenharmony_ci	begin = bcfg + 0x1000;
828c2ecf20Sopenharmony_ci	end = limit;
838c2ecf20Sopenharmony_ci	for (rcfg = begin; rcfg < end; rcfg += 0x1000)
848c2ecf20Sopenharmony_ci		if (check_notempty(rcfg) && check_radio_magic(rcfg))
858c2ecf20Sopenharmony_ci			return rcfg;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* AR2316 relocates radio config to new location */
888c2ecf20Sopenharmony_ci	begin = bcfg + 0xf8;
898c2ecf20Sopenharmony_ci	end = limit - 0x1000 + 0xf8;
908c2ecf20Sopenharmony_ci	for (rcfg = begin; rcfg < end; rcfg += 0x1000)
918c2ecf20Sopenharmony_ci		if (check_notempty(rcfg) && check_radio_magic(rcfg))
928c2ecf20Sopenharmony_ci			return rcfg;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return NULL;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/*
988c2ecf20Sopenharmony_ci * NB: Search region size could be larger than the actual flash size,
998c2ecf20Sopenharmony_ci * but this shouldn't be a problem here, because the flash
1008c2ecf20Sopenharmony_ci * will simply be mapped multiple times.
1018c2ecf20Sopenharmony_ci */
1028c2ecf20Sopenharmony_ciint __init ath25_find_config(phys_addr_t base, unsigned long size)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	const void __iomem *flash_base, *flash_limit;
1058c2ecf20Sopenharmony_ci	struct ath25_boarddata *config;
1068c2ecf20Sopenharmony_ci	unsigned int rcfg_size;
1078c2ecf20Sopenharmony_ci	int broken_boarddata = 0;
1088c2ecf20Sopenharmony_ci	const void __iomem *bcfg, *rcfg;
1098c2ecf20Sopenharmony_ci	u8 *board_data;
1108c2ecf20Sopenharmony_ci	u8 *radio_data;
1118c2ecf20Sopenharmony_ci	u8 *mac_addr;
1128c2ecf20Sopenharmony_ci	u32 offset;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	flash_base = ioremap(base, size);
1158c2ecf20Sopenharmony_ci	flash_limit = flash_base + size;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	ath25_board.config = NULL;
1188c2ecf20Sopenharmony_ci	ath25_board.radio = NULL;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Copy the board and radio data to RAM, because accessing the mapped
1218c2ecf20Sopenharmony_ci	 * memory of the flash directly after booting is not safe */
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* Try to find valid board and radio data */
1248c2ecf20Sopenharmony_ci	bcfg = find_board_config(flash_limit, false);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* If that fails, try to at least find valid radio data */
1278c2ecf20Sopenharmony_ci	if (!bcfg) {
1288c2ecf20Sopenharmony_ci		bcfg = find_board_config(flash_limit, true);
1298c2ecf20Sopenharmony_ci		broken_boarddata = 1;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (!bcfg) {
1338c2ecf20Sopenharmony_ci		pr_warn("WARNING: No board configuration data found!\n");
1348c2ecf20Sopenharmony_ci		goto error;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	board_data = kzalloc(BOARD_CONFIG_BUFSZ, GFP_KERNEL);
1388c2ecf20Sopenharmony_ci	if (!board_data)
1398c2ecf20Sopenharmony_ci		goto error;
1408c2ecf20Sopenharmony_ci	ath25_board.config = (struct ath25_boarddata *)board_data;
1418c2ecf20Sopenharmony_ci	memcpy_fromio(board_data, bcfg, 0x100);
1428c2ecf20Sopenharmony_ci	if (broken_boarddata) {
1438c2ecf20Sopenharmony_ci		pr_warn("WARNING: broken board data detected\n");
1448c2ecf20Sopenharmony_ci		config = ath25_board.config;
1458c2ecf20Sopenharmony_ci		if (is_zero_ether_addr(config->enet0_mac)) {
1468c2ecf20Sopenharmony_ci			pr_info("Fixing up empty mac addresses\n");
1478c2ecf20Sopenharmony_ci			config->reset_config_gpio = 0xffff;
1488c2ecf20Sopenharmony_ci			config->sys_led_gpio = 0xffff;
1498c2ecf20Sopenharmony_ci			eth_random_addr(config->wlan0_mac);
1508c2ecf20Sopenharmony_ci			config->wlan0_mac[0] &= ~0x06;
1518c2ecf20Sopenharmony_ci			eth_random_addr(config->enet0_mac);
1528c2ecf20Sopenharmony_ci			eth_random_addr(config->enet1_mac);
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* Radio config starts 0x100 bytes after board config, regardless
1578c2ecf20Sopenharmony_ci	 * of what the physical layout on the flash chip looks like */
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	rcfg = find_radio_config(flash_limit, bcfg);
1608c2ecf20Sopenharmony_ci	if (!rcfg) {
1618c2ecf20Sopenharmony_ci		pr_warn("WARNING: Could not find Radio Configuration data\n");
1628c2ecf20Sopenharmony_ci		goto error;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	radio_data = board_data + 0x100 + ((rcfg - bcfg) & 0xfff);
1668c2ecf20Sopenharmony_ci	ath25_board.radio = radio_data;
1678c2ecf20Sopenharmony_ci	offset = radio_data - board_data;
1688c2ecf20Sopenharmony_ci	pr_info("Radio config found at offset 0x%x (0x%x)\n", rcfg - bcfg,
1698c2ecf20Sopenharmony_ci		offset);
1708c2ecf20Sopenharmony_ci	rcfg_size = BOARD_CONFIG_BUFSZ - offset;
1718c2ecf20Sopenharmony_ci	memcpy_fromio(radio_data, rcfg, rcfg_size);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	mac_addr = &radio_data[0x1d * 2];
1748c2ecf20Sopenharmony_ci	if (is_broadcast_ether_addr(mac_addr)) {
1758c2ecf20Sopenharmony_ci		pr_info("Radio MAC is blank; using board-data\n");
1768c2ecf20Sopenharmony_ci		ether_addr_copy(mac_addr, ath25_board.config->wlan0_mac);
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	iounmap(flash_base);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return 0;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cierror:
1848c2ecf20Sopenharmony_ci	iounmap(flash_base);
1858c2ecf20Sopenharmony_ci	return -ENODEV;
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic void ath25_halt(void)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	local_irq_disable();
1918c2ecf20Sopenharmony_ci	unreachable();
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_civoid __init plat_mem_setup(void)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	_machine_halt = ath25_halt;
1978c2ecf20Sopenharmony_ci	pm_power_off = ath25_halt;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	if (is_ar5312())
2008c2ecf20Sopenharmony_ci		ar5312_plat_mem_setup();
2018c2ecf20Sopenharmony_ci	else
2028c2ecf20Sopenharmony_ci		ar2315_plat_mem_setup();
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	/* Disable data watchpoints */
2058c2ecf20Sopenharmony_ci	write_c0_watchlo0(0);
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ciasmlinkage void plat_irq_dispatch(void)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	ath25_irq_dispatch();
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_civoid __init plat_time_init(void)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	if (is_ar5312())
2168c2ecf20Sopenharmony_ci		ar5312_plat_time_init();
2178c2ecf20Sopenharmony_ci	else
2188c2ecf20Sopenharmony_ci		ar2315_plat_time_init();
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ciunsigned int get_c0_compare_int(void)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	return CP0_LEGACY_COMPARE_IRQ;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_civoid __init arch_init_irq(void)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	clear_c0_status(ST0_IM);
2298c2ecf20Sopenharmony_ci	mips_cpu_irq_init();
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/* Initialize interrupt controllers */
2328c2ecf20Sopenharmony_ci	if (is_ar5312())
2338c2ecf20Sopenharmony_ci		ar5312_arch_init_irq();
2348c2ecf20Sopenharmony_ci	else
2358c2ecf20Sopenharmony_ci		ar2315_arch_init_irq();
2368c2ecf20Sopenharmony_ci}
237