ar5312.c 10 KB

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