board.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (!board_data)
  114. goto error;
  115. ath25_board.config = (struct ath25_boarddata *)board_data;
  116. memcpy_fromio(board_data, bcfg, 0x100);
  117. if (broken_boarddata) {
  118. pr_warn("WARNING: broken board data detected\n");
  119. config = ath25_board.config;
  120. if (is_zero_ether_addr(config->enet0_mac)) {
  121. pr_info("Fixing up empty mac addresses\n");
  122. config->reset_config_gpio = 0xffff;
  123. config->sys_led_gpio = 0xffff;
  124. eth_random_addr(config->wlan0_mac);
  125. config->wlan0_mac[0] &= ~0x06;
  126. eth_random_addr(config->enet0_mac);
  127. eth_random_addr(config->enet1_mac);
  128. }
  129. }
  130. /* Radio config starts 0x100 bytes after board config, regardless
  131. * of what the physical layout on the flash chip looks like */
  132. rcfg = find_radio_config(flash_limit, bcfg);
  133. if (!rcfg) {
  134. pr_warn("WARNING: Could not find Radio Configuration data\n");
  135. goto error;
  136. }
  137. radio_data = board_data + 0x100 + ((rcfg - bcfg) & 0xfff);
  138. ath25_board.radio = radio_data;
  139. offset = radio_data - board_data;
  140. pr_info("Radio config found at offset 0x%x (0x%x)\n", rcfg - bcfg,
  141. offset);
  142. rcfg_size = BOARD_CONFIG_BUFSZ - offset;
  143. memcpy_fromio(radio_data, rcfg, rcfg_size);
  144. mac_addr = &radio_data[0x1d * 2];
  145. if (is_broadcast_ether_addr(mac_addr)) {
  146. pr_info("Radio MAC is blank; using board-data\n");
  147. ether_addr_copy(mac_addr, ath25_board.config->wlan0_mac);
  148. }
  149. iounmap(flash_base);
  150. return 0;
  151. error:
  152. iounmap(flash_base);
  153. return -ENODEV;
  154. }
  155. static void ath25_halt(void)
  156. {
  157. local_irq_disable();
  158. unreachable();
  159. }
  160. void __init plat_mem_setup(void)
  161. {
  162. _machine_halt = ath25_halt;
  163. pm_power_off = ath25_halt;
  164. if (is_ar5312())
  165. ar5312_plat_mem_setup();
  166. else
  167. ar2315_plat_mem_setup();
  168. /* Disable data watchpoints */
  169. write_c0_watchlo0(0);
  170. }
  171. asmlinkage void plat_irq_dispatch(void)
  172. {
  173. ath25_irq_dispatch();
  174. }
  175. void __init plat_time_init(void)
  176. {
  177. if (is_ar5312())
  178. ar5312_plat_time_init();
  179. else
  180. ar2315_plat_time_init();
  181. }
  182. unsigned int get_c0_compare_int(void)
  183. {
  184. return CP0_LEGACY_COMPARE_IRQ;
  185. }
  186. void __init arch_init_irq(void)
  187. {
  188. clear_c0_status(ST0_IM);
  189. mips_cpu_irq_init();
  190. /* Initialize interrupt controllers */
  191. if (is_ar5312())
  192. ar5312_arch_init_irq();
  193. else
  194. ar2315_arch_init_irq();
  195. }