Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is subject to the terms and conditions of the GNU General Public
0003  * License.  See the file "COPYING" in the main directory of this archive
0004  * for more details.
0005  *
0006  * Copyright (C) 2003 Atheros Communications, Inc.,  All Rights Reserved.
0007  * Copyright (C) 2006 FON Technology, SL.
0008  * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
0009  * Copyright (C) 2006-2009 Felix Fietkau <nbd@openwrt.org>
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/interrupt.h>
0014 #include <asm/irq_cpu.h>
0015 #include <asm/reboot.h>
0016 #include <asm/bootinfo.h>
0017 #include <asm/time.h>
0018 
0019 #include <ath25_platform.h>
0020 #include "devices.h"
0021 #include "ar5312.h"
0022 #include "ar2315.h"
0023 
0024 void (*ath25_irq_dispatch)(void);
0025 
0026 static inline bool check_radio_magic(const void __iomem *addr)
0027 {
0028     addr += 0x7a; /* offset for flash magic */
0029     return (__raw_readb(addr) == 0x5a) && (__raw_readb(addr + 1) == 0xa5);
0030 }
0031 
0032 static inline bool check_notempty(const void __iomem *addr)
0033 {
0034     return __raw_readl(addr) != 0xffffffff;
0035 }
0036 
0037 static inline bool check_board_data(const void __iomem *addr, bool broken)
0038 {
0039     /* config magic found */
0040     if (__raw_readl(addr) == ATH25_BD_MAGIC)
0041         return true;
0042 
0043     if (!broken)
0044         return false;
0045 
0046     /* broken board data detected, use radio data to find the
0047      * offset, user will fix this */
0048 
0049     if (check_radio_magic(addr + 0x1000))
0050         return true;
0051     if (check_radio_magic(addr + 0xf8))
0052         return true;
0053 
0054     return false;
0055 }
0056 
0057 static const void __iomem * __init find_board_config(const void __iomem *limit,
0058                              const bool broken)
0059 {
0060     const void __iomem *addr;
0061     const void __iomem *begin = limit - 0x1000;
0062     const void __iomem *end = limit - 0x30000;
0063 
0064     for (addr = begin; addr >= end; addr -= 0x1000)
0065         if (check_board_data(addr, broken))
0066             return addr;
0067 
0068     return NULL;
0069 }
0070 
0071 static const void __iomem * __init find_radio_config(const void __iomem *limit,
0072                              const void __iomem *bcfg)
0073 {
0074     const void __iomem *rcfg, *begin, *end;
0075 
0076     /*
0077      * Now find the start of Radio Configuration data, using heuristics:
0078      * Search forward from Board Configuration data by 0x1000 bytes
0079      * at a time until we find non-0xffffffff.
0080      */
0081     begin = bcfg + 0x1000;
0082     end = limit;
0083     for (rcfg = begin; rcfg < end; rcfg += 0x1000)
0084         if (check_notempty(rcfg) && check_radio_magic(rcfg))
0085             return rcfg;
0086 
0087     /* AR2316 relocates radio config to new location */
0088     begin = bcfg + 0xf8;
0089     end = limit - 0x1000 + 0xf8;
0090     for (rcfg = begin; rcfg < end; rcfg += 0x1000)
0091         if (check_notempty(rcfg) && check_radio_magic(rcfg))
0092             return rcfg;
0093 
0094     return NULL;
0095 }
0096 
0097 /*
0098  * NB: Search region size could be larger than the actual flash size,
0099  * but this shouldn't be a problem here, because the flash
0100  * will simply be mapped multiple times.
0101  */
0102 int __init ath25_find_config(phys_addr_t base, unsigned long size)
0103 {
0104     const void __iomem *flash_base, *flash_limit;
0105     struct ath25_boarddata *config;
0106     unsigned int rcfg_size;
0107     int broken_boarddata = 0;
0108     const void __iomem *bcfg, *rcfg;
0109     u8 *board_data;
0110     u8 *radio_data;
0111     u8 *mac_addr;
0112     u32 offset;
0113 
0114     flash_base = ioremap(base, size);
0115     flash_limit = flash_base + size;
0116 
0117     ath25_board.config = NULL;
0118     ath25_board.radio = NULL;
0119 
0120     /* Copy the board and radio data to RAM, because accessing the mapped
0121      * memory of the flash directly after booting is not safe */
0122 
0123     /* Try to find valid board and radio data */
0124     bcfg = find_board_config(flash_limit, false);
0125 
0126     /* If that fails, try to at least find valid radio data */
0127     if (!bcfg) {
0128         bcfg = find_board_config(flash_limit, true);
0129         broken_boarddata = 1;
0130     }
0131 
0132     if (!bcfg) {
0133         pr_warn("WARNING: No board configuration data found!\n");
0134         goto error;
0135     }
0136 
0137     board_data = kzalloc(BOARD_CONFIG_BUFSZ, GFP_KERNEL);
0138     if (!board_data)
0139         goto error;
0140     ath25_board.config = (struct ath25_boarddata *)board_data;
0141     memcpy_fromio(board_data, bcfg, 0x100);
0142     if (broken_boarddata) {
0143         pr_warn("WARNING: broken board data detected\n");
0144         config = ath25_board.config;
0145         if (is_zero_ether_addr(config->enet0_mac)) {
0146             pr_info("Fixing up empty mac addresses\n");
0147             config->reset_config_gpio = 0xffff;
0148             config->sys_led_gpio = 0xffff;
0149             eth_random_addr(config->wlan0_mac);
0150             config->wlan0_mac[0] &= ~0x06;
0151             eth_random_addr(config->enet0_mac);
0152             eth_random_addr(config->enet1_mac);
0153         }
0154     }
0155 
0156     /* Radio config starts 0x100 bytes after board config, regardless
0157      * of what the physical layout on the flash chip looks like */
0158 
0159     rcfg = find_radio_config(flash_limit, bcfg);
0160     if (!rcfg) {
0161         pr_warn("WARNING: Could not find Radio Configuration data\n");
0162         goto error;
0163     }
0164 
0165     radio_data = board_data + 0x100 + ((rcfg - bcfg) & 0xfff);
0166     ath25_board.radio = radio_data;
0167     offset = radio_data - board_data;
0168     pr_info("Radio config found at offset 0x%x (0x%x)\n", rcfg - bcfg,
0169         offset);
0170     rcfg_size = BOARD_CONFIG_BUFSZ - offset;
0171     memcpy_fromio(radio_data, rcfg, rcfg_size);
0172 
0173     mac_addr = &radio_data[0x1d * 2];
0174     if (is_broadcast_ether_addr(mac_addr)) {
0175         pr_info("Radio MAC is blank; using board-data\n");
0176         ether_addr_copy(mac_addr, ath25_board.config->wlan0_mac);
0177     }
0178 
0179     iounmap(flash_base);
0180 
0181     return 0;
0182 
0183 error:
0184     iounmap(flash_base);
0185     return -ENODEV;
0186 }
0187 
0188 static void ath25_halt(void)
0189 {
0190     local_irq_disable();
0191     unreachable();
0192 }
0193 
0194 void __init plat_mem_setup(void)
0195 {
0196     _machine_halt = ath25_halt;
0197     pm_power_off = ath25_halt;
0198 
0199     if (is_ar5312())
0200         ar5312_plat_mem_setup();
0201     else
0202         ar2315_plat_mem_setup();
0203 
0204     /* Disable data watchpoints */
0205     write_c0_watchlo0(0);
0206 }
0207 
0208 asmlinkage void plat_irq_dispatch(void)
0209 {
0210     ath25_irq_dispatch();
0211 }
0212 
0213 void __init plat_time_init(void)
0214 {
0215     if (is_ar5312())
0216         ar5312_plat_time_init();
0217     else
0218         ar2315_plat_time_init();
0219 }
0220 
0221 unsigned int get_c0_compare_int(void)
0222 {
0223     return CP0_LEGACY_COMPARE_IRQ;
0224 }
0225 
0226 void __init arch_init_irq(void)
0227 {
0228     clear_c0_status(ST0_IM);
0229     mips_cpu_irq_init();
0230 
0231     /* Initialize interrupt controllers */
0232     if (is_ar5312())
0233         ar5312_arch_init_irq();
0234     else
0235         ar2315_arch_init_irq();
0236 }