ar5312.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. * Copyright (C) 2012 Alexandros C. Couloumbis <alex@ozo.com>
  11. */
  12. /*
  13. * Platform devices for Atheros AR5312 SoCs
  14. */
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/bitops.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mtd/physmap.h>
  22. #include <linux/reboot.h>
  23. #include <asm/bootinfo.h>
  24. #include <asm/reboot.h>
  25. #include <asm/time.h>
  26. #include <ath25_platform.h>
  27. #include "devices.h"
  28. #include "ar5312.h"
  29. #include "ar5312_regs.h"
  30. static void __iomem *ar5312_rst_base;
  31. static struct irq_domain *ar5312_misc_irq_domain;
  32. static inline u32 ar5312_rst_reg_read(u32 reg)
  33. {
  34. return __raw_readl(ar5312_rst_base + reg);
  35. }
  36. static inline void ar5312_rst_reg_write(u32 reg, u32 val)
  37. {
  38. __raw_writel(val, ar5312_rst_base + reg);
  39. }
  40. static inline void ar5312_rst_reg_mask(u32 reg, u32 mask, u32 val)
  41. {
  42. u32 ret = ar5312_rst_reg_read(reg);
  43. ret &= ~mask;
  44. ret |= val;
  45. ar5312_rst_reg_write(reg, ret);
  46. }
  47. static irqreturn_t ar5312_ahb_err_handler(int cpl, void *dev_id)
  48. {
  49. u32 proc1 = ar5312_rst_reg_read(AR5312_PROC1);
  50. u32 proc_addr = ar5312_rst_reg_read(AR5312_PROCADDR); /* clears error */
  51. u32 dma1 = ar5312_rst_reg_read(AR5312_DMA1);
  52. u32 dma_addr = ar5312_rst_reg_read(AR5312_DMAADDR); /* clears error */
  53. pr_emerg("AHB interrupt: PROCADDR=0x%8.8x PROC1=0x%8.8x DMAADDR=0x%8.8x DMA1=0x%8.8x\n",
  54. proc_addr, proc1, dma_addr, dma1);
  55. machine_restart("AHB error"); /* Catastrophic failure */
  56. return IRQ_HANDLED;
  57. }
  58. static struct irqaction ar5312_ahb_err_interrupt = {
  59. .handler = ar5312_ahb_err_handler,
  60. .name = "ar5312-ahb-error",
  61. };
  62. static void ar5312_misc_irq_handler(struct irq_desc *desc)
  63. {
  64. u32 pending = ar5312_rst_reg_read(AR5312_ISR) &
  65. ar5312_rst_reg_read(AR5312_IMR);
  66. unsigned nr, misc_irq = 0;
  67. if (pending) {
  68. struct irq_domain *domain = irq_desc_get_handler_data(desc);
  69. nr = __ffs(pending);
  70. misc_irq = irq_find_mapping(domain, nr);
  71. }
  72. if (misc_irq) {
  73. generic_handle_irq(misc_irq);
  74. if (nr == AR5312_MISC_IRQ_TIMER)
  75. ar5312_rst_reg_read(AR5312_TIMER);
  76. } else {
  77. spurious_interrupt();
  78. }
  79. }
  80. /* Enable the specified AR5312_MISC_IRQ interrupt */
  81. static void ar5312_misc_irq_unmask(struct irq_data *d)
  82. {
  83. ar5312_rst_reg_mask(AR5312_IMR, 0, BIT(d->hwirq));
  84. }
  85. /* Disable the specified AR5312_MISC_IRQ interrupt */
  86. static void ar5312_misc_irq_mask(struct irq_data *d)
  87. {
  88. ar5312_rst_reg_mask(AR5312_IMR, BIT(d->hwirq), 0);
  89. ar5312_rst_reg_read(AR5312_IMR); /* flush write buffer */
  90. }
  91. static struct irq_chip ar5312_misc_irq_chip = {
  92. .name = "ar5312-misc",
  93. .irq_unmask = ar5312_misc_irq_unmask,
  94. .irq_mask = ar5312_misc_irq_mask,
  95. };
  96. static int ar5312_misc_irq_map(struct irq_domain *d, unsigned irq,
  97. irq_hw_number_t hw)
  98. {
  99. irq_set_chip_and_handler(irq, &ar5312_misc_irq_chip, handle_level_irq);
  100. return 0;
  101. }
  102. static struct irq_domain_ops ar5312_misc_irq_domain_ops = {
  103. .map = ar5312_misc_irq_map,
  104. };
  105. static void ar5312_irq_dispatch(void)
  106. {
  107. u32 pending = read_c0_status() & read_c0_cause();
  108. if (pending & CAUSEF_IP2)
  109. do_IRQ(AR5312_IRQ_WLAN0);
  110. else if (pending & CAUSEF_IP5)
  111. do_IRQ(AR5312_IRQ_WLAN1);
  112. else if (pending & CAUSEF_IP6)
  113. do_IRQ(AR5312_IRQ_MISC);
  114. else if (pending & CAUSEF_IP7)
  115. do_IRQ(ATH25_IRQ_CPU_CLOCK);
  116. else
  117. spurious_interrupt();
  118. }
  119. void __init ar5312_arch_init_irq(void)
  120. {
  121. struct irq_domain *domain;
  122. unsigned irq;
  123. ath25_irq_dispatch = ar5312_irq_dispatch;
  124. domain = irq_domain_add_linear(NULL, AR5312_MISC_IRQ_COUNT,
  125. &ar5312_misc_irq_domain_ops, NULL);
  126. if (!domain)
  127. panic("Failed to add IRQ domain");
  128. irq = irq_create_mapping(domain, AR5312_MISC_IRQ_AHB_PROC);
  129. setup_irq(irq, &ar5312_ahb_err_interrupt);
  130. irq_set_chained_handler_and_data(AR5312_IRQ_MISC,
  131. ar5312_misc_irq_handler, domain);
  132. ar5312_misc_irq_domain = domain;
  133. }
  134. static struct physmap_flash_data ar5312_flash_data = {
  135. .width = 2,
  136. };
  137. static struct resource ar5312_flash_resource = {
  138. .start = AR5312_FLASH_BASE,
  139. .end = AR5312_FLASH_BASE + AR5312_FLASH_SIZE - 1,
  140. .flags = IORESOURCE_MEM,
  141. };
  142. static struct platform_device ar5312_physmap_flash = {
  143. .name = "physmap-flash",
  144. .id = 0,
  145. .dev.platform_data = &ar5312_flash_data,
  146. .resource = &ar5312_flash_resource,
  147. .num_resources = 1,
  148. };
  149. static void __init ar5312_flash_init(void)
  150. {
  151. void __iomem *flashctl_base;
  152. u32 ctl;
  153. flashctl_base = ioremap_nocache(AR5312_FLASHCTL_BASE,
  154. AR5312_FLASHCTL_SIZE);
  155. ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL0);
  156. ctl &= AR5312_FLASHCTL_MW;
  157. /* fixup flash width */
  158. switch (ctl) {
  159. case AR5312_FLASHCTL_MW16:
  160. ar5312_flash_data.width = 2;
  161. break;
  162. case AR5312_FLASHCTL_MW8:
  163. default:
  164. ar5312_flash_data.width = 1;
  165. break;
  166. }
  167. /*
  168. * Configure flash bank 0.
  169. * Assume 8M window size. Flash will be aliased if it's smaller
  170. */
  171. ctl |= AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC_8M | AR5312_FLASHCTL_RBLE;
  172. ctl |= 0x01 << AR5312_FLASHCTL_IDCY_S;
  173. ctl |= 0x07 << AR5312_FLASHCTL_WST1_S;
  174. ctl |= 0x07 << AR5312_FLASHCTL_WST2_S;
  175. __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL0);
  176. /* Disable other flash banks */
  177. ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL1);
  178. ctl &= ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC);
  179. __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL1);
  180. ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL2);
  181. ctl &= ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC);
  182. __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL2);
  183. iounmap(flashctl_base);
  184. }
  185. void __init ar5312_init_devices(void)
  186. {
  187. struct ath25_boarddata *config;
  188. ar5312_flash_init();
  189. /* Locate board/radio config data */
  190. ath25_find_config(AR5312_FLASH_BASE, AR5312_FLASH_SIZE);
  191. config = ath25_board.config;
  192. /* AR2313 has CPU minor rev. 10 */
  193. if ((current_cpu_data.processor_id & 0xff) == 0x0a)
  194. ath25_soc = ATH25_SOC_AR2313;
  195. /* AR2312 shares the same Silicon ID as AR5312 */
  196. else if (config->flags & BD_ISCASPER)
  197. ath25_soc = ATH25_SOC_AR2312;
  198. /* Everything else is probably AR5312 or compatible */
  199. else
  200. ath25_soc = ATH25_SOC_AR5312;
  201. platform_device_register(&ar5312_physmap_flash);
  202. switch (ath25_soc) {
  203. case ATH25_SOC_AR5312:
  204. if (!ath25_board.radio)
  205. return;
  206. if (!(config->flags & BD_WLAN0))
  207. break;
  208. ath25_add_wmac(0, AR5312_WLAN0_BASE, AR5312_IRQ_WLAN0);
  209. break;
  210. case ATH25_SOC_AR2312:
  211. case ATH25_SOC_AR2313:
  212. if (!ath25_board.radio)
  213. return;
  214. break;
  215. default:
  216. break;
  217. }
  218. if (config->flags & BD_WLAN1)
  219. ath25_add_wmac(1, AR5312_WLAN1_BASE, AR5312_IRQ_WLAN1);
  220. }
  221. static void ar5312_restart(char *command)
  222. {
  223. /* reset the system */
  224. local_irq_disable();
  225. while (1)
  226. ar5312_rst_reg_write(AR5312_RESET, AR5312_RESET_SYSTEM);
  227. }
  228. /*
  229. * This table is indexed by bits 5..4 of the CLOCKCTL1 register
  230. * to determine the predevisor value.
  231. */
  232. static unsigned clockctl1_predivide_table[4] __initdata = { 1, 2, 4, 5 };
  233. static unsigned __init ar5312_cpu_frequency(void)
  234. {
  235. u32 scratch, devid, clock_ctl1;
  236. u32 predivide_mask, multiplier_mask, doubler_mask;
  237. unsigned predivide_shift, multiplier_shift;
  238. unsigned predivide_select, predivisor, multiplier;
  239. /* Trust the bootrom's idea of cpu frequency. */
  240. scratch = ar5312_rst_reg_read(AR5312_SCRATCH);
  241. if (scratch)
  242. return scratch;
  243. devid = ar5312_rst_reg_read(AR5312_REV);
  244. devid = (devid & AR5312_REV_MAJ) >> AR5312_REV_MAJ_S;
  245. if (devid == AR5312_REV_MAJ_AR2313) {
  246. predivide_mask = AR2313_CLOCKCTL1_PREDIVIDE_MASK;
  247. predivide_shift = AR2313_CLOCKCTL1_PREDIVIDE_SHIFT;
  248. multiplier_mask = AR2313_CLOCKCTL1_MULTIPLIER_MASK;
  249. multiplier_shift = AR2313_CLOCKCTL1_MULTIPLIER_SHIFT;
  250. doubler_mask = AR2313_CLOCKCTL1_DOUBLER_MASK;
  251. } else { /* AR5312 and AR2312 */
  252. predivide_mask = AR5312_CLOCKCTL1_PREDIVIDE_MASK;
  253. predivide_shift = AR5312_CLOCKCTL1_PREDIVIDE_SHIFT;
  254. multiplier_mask = AR5312_CLOCKCTL1_MULTIPLIER_MASK;
  255. multiplier_shift = AR5312_CLOCKCTL1_MULTIPLIER_SHIFT;
  256. doubler_mask = AR5312_CLOCKCTL1_DOUBLER_MASK;
  257. }
  258. /*
  259. * Clocking is derived from a fixed 40MHz input clock.
  260. *
  261. * cpu_freq = input_clock * MULT (where MULT is PLL multiplier)
  262. * sys_freq = cpu_freq / 4 (used for APB clock, serial,
  263. * flash, Timer, Watchdog Timer)
  264. *
  265. * cnt_freq = cpu_freq / 2 (use for CPU count/compare)
  266. *
  267. * So, for example, with a PLL multiplier of 5, we have
  268. *
  269. * cpu_freq = 200MHz
  270. * sys_freq = 50MHz
  271. * cnt_freq = 100MHz
  272. *
  273. * We compute the CPU frequency, based on PLL settings.
  274. */
  275. clock_ctl1 = ar5312_rst_reg_read(AR5312_CLOCKCTL1);
  276. predivide_select = (clock_ctl1 & predivide_mask) >> predivide_shift;
  277. predivisor = clockctl1_predivide_table[predivide_select];
  278. multiplier = (clock_ctl1 & multiplier_mask) >> multiplier_shift;
  279. if (clock_ctl1 & doubler_mask)
  280. multiplier <<= 1;
  281. return (40000000 / predivisor) * multiplier;
  282. }
  283. static inline unsigned ar5312_sys_frequency(void)
  284. {
  285. return ar5312_cpu_frequency() / 4;
  286. }
  287. void __init ar5312_plat_time_init(void)
  288. {
  289. mips_hpt_frequency = ar5312_cpu_frequency() / 2;
  290. }
  291. void __init ar5312_plat_mem_setup(void)
  292. {
  293. void __iomem *sdram_base;
  294. u32 memsize, memcfg, bank0_ac, bank1_ac;
  295. u32 devid;
  296. /* Detect memory size */
  297. sdram_base = ioremap_nocache(AR5312_SDRAMCTL_BASE,
  298. AR5312_SDRAMCTL_SIZE);
  299. memcfg = __raw_readl(sdram_base + AR5312_MEM_CFG1);
  300. bank0_ac = ATH25_REG_MS(memcfg, AR5312_MEM_CFG1_AC0);
  301. bank1_ac = ATH25_REG_MS(memcfg, AR5312_MEM_CFG1_AC1);
  302. memsize = (bank0_ac ? (1 << (bank0_ac + 1)) : 0) +
  303. (bank1_ac ? (1 << (bank1_ac + 1)) : 0);
  304. memsize <<= 20;
  305. add_memory_region(0, memsize, BOOT_MEM_RAM);
  306. iounmap(sdram_base);
  307. ar5312_rst_base = ioremap_nocache(AR5312_RST_BASE, AR5312_RST_SIZE);
  308. devid = ar5312_rst_reg_read(AR5312_REV);
  309. devid >>= AR5312_REV_WMAC_MIN_S;
  310. devid &= AR5312_REV_CHIP;
  311. ath25_board.devid = (u16)devid;
  312. /* Clear any lingering AHB errors */
  313. ar5312_rst_reg_read(AR5312_PROCADDR);
  314. ar5312_rst_reg_read(AR5312_DMAADDR);
  315. ar5312_rst_reg_write(AR5312_WDT_CTRL, AR5312_WDT_CTRL_IGNORE);
  316. _machine_restart = ar5312_restart;
  317. }
  318. void __init ar5312_arch_init(void)
  319. {
  320. unsigned irq = irq_create_mapping(ar5312_misc_irq_domain,
  321. AR5312_MISC_IRQ_UART0);
  322. ath25_serial_setup(AR5312_UART0_BASE, irq, ar5312_sys_frequency());
  323. }