board.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <asm/irq_cpu.h>
  14. #include <asm/reboot.h>
  15. #include <asm/bootinfo.h>
  16. #include <asm/time.h>
  17. #include <ath25_platform.h>
  18. #include "devices.h"
  19. #include "ar5312.h"
  20. #include "ar2315.h"
  21. void (*ath25_irq_dispatch)(void);
  22. static inline bool check_radio_magic(const void __iomem *addr)
  23. {
  24. addr += 0x7a; /* offset for flash magic */
  25. return (__raw_readb(addr) == 0x5a) && (__raw_readb(addr + 1) == 0xa5);
  26. }
  27. static inline bool check_notempty(const void __iomem *addr)
  28. {
  29. return __raw_readl(addr) != 0xffffffff;
  30. }
  31. static inline bool check_board_data(const void __iomem *addr, bool broken)
  32. {
  33. /* config magic found */
  34. if (__raw_readl(addr) == ATH25_BD_MAGIC)
  35. return true;
  36. if (!broken)
  37. return false;
  38. /* broken board data detected, use radio data to find the
  39. * offset, user will fix this */
  40. if (check_radio_magic(addr + 0x1000))
  41. return true;
  42. if (check_radio_magic(addr + 0xf8))
  43. return true;
  44. return false;
  45. }
  46. static const void __iomem * __init find_board_config(const void __iomem *limit,
  47. const bool broken)
  48. {
  49. const void __iomem *addr;
  50. const void __iomem *begin = limit - 0x1000;
  51. const void __iomem *end = limit - 0x30000;
  52. for (addr = begin; addr >= end; addr -= 0x1000)
  53. if (check_board_data(addr, broken))
  54. return addr;
  55. return NULL;
  56. }
  57. static const void __iomem * __init find_radio_config(const void __iomem *limit,
  58. const void __iomem *bcfg)
  59. {
  60. const void __iomem *rcfg, *begin, *end;
  61. /*
  62. * Now find the start of Radio Configuration data, using heuristics:
  63. * Search forward from Board Configuration data by 0x1000 bytes
  64. * at a time until we find non-0xffffffff.
  65. */
  66. begin = bcfg + 0x1000;
  67. end = limit;
  68. for (rcfg = begin; rcfg < end; rcfg += 0x1000)
  69. if (check_notempty(rcfg) && check_radio_magic(rcfg))
  70. return rcfg;
  71. /* AR2316 relocates radio config to new location */
  72. begin = bcfg + 0xf8;
  73. end = limit - 0x1000 + 0xf8;
  74. for (rcfg = begin; rcfg < end; rcfg += 0x1000)
  75. if (check_notempty(rcfg) && check_radio_magic(rcfg))
  76. return rcfg;
  77. return NULL;
  78. }
  79. /*
  80. * NB: Search region size could be larger than the actual flash size,
  81. * but this shouldn't be a problem here, because the flash
  82. * will simply be mapped multiple times.
  83. */
  84. int __init ath25_find_config(phys_addr_t base, unsigned long size)
  85. {
  86. const void __iomem *flash_base, *flash_limit;
  87. struct ath25_boarddata *config;
  88. unsigned int rcfg_size;
  89. int broken_boarddata = 0;
  90. const void __iomem *bcfg, *rcfg;
  91. u8 *board_data;
  92. u8 *radio_data;
  93. u8 *mac_addr;
  94. u32 offset;
  95. flash_base = ioremap_nocache(base, size);
  96. flash_limit = flash_base + size;
  97. ath25_board.config = NULL;
  98. ath25_board.radio = NULL;
  99. /* Copy the board and radio data to RAM, because accessing the mapped
  100. * memory of the flash directly after booting is not safe */
  101. /* Try to find valid board and radio data */
  102. bcfg = find_board_config(flash_limit, false);
  103. /* If that fails, try to at least find valid radio data */
  104. if (!bcfg) {
  105. bcfg = find_board_config(flash_limit, true);
  106. broken_boarddata = 1;
  107. }
  108. if (!bcfg) {
  109. pr_warn("WARNING: No board configuration data found!\n");
  110. goto error;
  111. }
  112. board_data = kzalloc(BOARD_CONFIG_BUFSZ, GFP_KERNEL);
  113. ath25_board.config = (struct ath25_boarddata *)board_data;
  114. memcpy_fromio(board_data, bcfg, 0x100);
  115. if (broken_boarddata) {
  116. pr_warn("WARNING: broken board data detected\n");
  117. config = ath25_board.config;
  118. if (is_zero_ether_addr(config->enet0_mac)) {
  119. pr_info("Fixing up empty mac addresses\n");
  120. config->reset_config_gpio = 0xffff;
  121. config->sys_led_gpio = 0xffff;
  122. random_ether_addr(config->wlan0_mac);
  123. config->wlan0_mac[0] &= ~0x06;
  124. random_ether_addr(config->enet0_mac);
  125. random_ether_addr(config->enet1_mac);
  126. }
  127. }
  128. /* Radio config starts 0x100 bytes after board config, regardless
  129. * of what the physical layout on the flash chip looks like */
  130. rcfg = find_radio_config(flash_limit, bcfg);
  131. if (!rcfg) {
  132. pr_warn("WARNING: Could not find Radio Configuration data\n");
  133. goto error;
  134. }
  135. radio_data = board_data + 0x100 + ((rcfg - bcfg) & 0xfff);
  136. ath25_board.radio = radio_data;
  137. offset = radio_data - board_data;
  138. pr_info("Radio config found at offset 0x%x (0x%x)\n", rcfg - bcfg,
  139. offset);
  140. rcfg_size = BOARD_CONFIG_BUFSZ - offset;
  141. memcpy_fromio(radio_data, rcfg, rcfg_size);
  142. mac_addr = &radio_data[0x1d * 2];
  143. if (is_broadcast_ether_addr(mac_addr)) {
  144. pr_info("Radio MAC is blank; using board-data\n");
  145. ether_addr_copy(mac_addr, ath25_board.config->wlan0_mac);
  146. }
  147. iounmap(flash_base);
  148. return 0;
  149. error:
  150. iounmap(flash_base);
  151. return -ENODEV;
  152. }
  153. static void ath25_halt(void)
  154. {
  155. local_irq_disable();
  156. unreachable();
  157. }
  158. void __init plat_mem_setup(void)
  159. {
  160. _machine_halt = ath25_halt;
  161. pm_power_off = ath25_halt;
  162. if (is_ar5312())
  163. ar5312_plat_mem_setup();
  164. else
  165. ar2315_plat_mem_setup();
  166. /* Disable data watchpoints */
  167. write_c0_watchlo0(0);
  168. }
  169. asmlinkage void plat_irq_dispatch(void)
  170. {
  171. ath25_irq_dispatch();
  172. }
  173. void __init plat_time_init(void)
  174. {
  175. if (is_ar5312())
  176. ar5312_plat_time_init();
  177. else
  178. ar2315_plat_time_init();
  179. }
  180. unsigned int get_c0_compare_int(void)
  181. {
  182. return CP0_LEGACY_COMPARE_IRQ;
  183. }
  184. void __init arch_init_irq(void)
  185. {
  186. clear_c0_status(ST0_IM);
  187. mips_cpu_irq_init();
  188. /* Initialize interrupt controllers */
  189. if (is_ar5312())
  190. ar5312_arch_init_irq();
  191. else
  192. ar2315_arch_init_irq();
  193. }