ar2315.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 Felix Fietkau <nbd@openwrt.org>
  10. * Copyright (C) 2012 Alexandros C. Couloumbis <alex@ozo.com>
  11. */
  12. /*
  13. * Platform devices for Atheros AR2315 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/reboot.h>
  22. #include <asm/bootinfo.h>
  23. #include <asm/reboot.h>
  24. #include <asm/time.h>
  25. #include <ath25_platform.h>
  26. #include "devices.h"
  27. #include "ar2315.h"
  28. #include "ar2315_regs.h"
  29. static void __iomem *ar2315_rst_base;
  30. static struct irq_domain *ar2315_misc_irq_domain;
  31. static inline u32 ar2315_rst_reg_read(u32 reg)
  32. {
  33. return __raw_readl(ar2315_rst_base + reg);
  34. }
  35. static inline void ar2315_rst_reg_write(u32 reg, u32 val)
  36. {
  37. __raw_writel(val, ar2315_rst_base + reg);
  38. }
  39. static inline void ar2315_rst_reg_mask(u32 reg, u32 mask, u32 val)
  40. {
  41. u32 ret = ar2315_rst_reg_read(reg);
  42. ret &= ~mask;
  43. ret |= val;
  44. ar2315_rst_reg_write(reg, ret);
  45. }
  46. static irqreturn_t ar2315_ahb_err_handler(int cpl, void *dev_id)
  47. {
  48. ar2315_rst_reg_write(AR2315_AHB_ERR0, AR2315_AHB_ERROR_DET);
  49. ar2315_rst_reg_read(AR2315_AHB_ERR1);
  50. pr_emerg("AHB fatal error\n");
  51. machine_restart("AHB error"); /* Catastrophic failure */
  52. return IRQ_HANDLED;
  53. }
  54. static struct irqaction ar2315_ahb_err_interrupt = {
  55. .handler = ar2315_ahb_err_handler,
  56. .name = "ar2315-ahb-error",
  57. };
  58. static void ar2315_misc_irq_handler(struct irq_desc *desc)
  59. {
  60. u32 pending = ar2315_rst_reg_read(AR2315_ISR) &
  61. ar2315_rst_reg_read(AR2315_IMR);
  62. unsigned nr, misc_irq = 0;
  63. if (pending) {
  64. struct irq_domain *domain = irq_desc_get_handler_data(desc);
  65. nr = __ffs(pending);
  66. misc_irq = irq_find_mapping(domain, nr);
  67. }
  68. if (misc_irq) {
  69. if (nr == AR2315_MISC_IRQ_GPIO)
  70. ar2315_rst_reg_write(AR2315_ISR, AR2315_ISR_GPIO);
  71. else if (nr == AR2315_MISC_IRQ_WATCHDOG)
  72. ar2315_rst_reg_write(AR2315_ISR, AR2315_ISR_WD);
  73. generic_handle_irq(misc_irq);
  74. } else {
  75. spurious_interrupt();
  76. }
  77. }
  78. static void ar2315_misc_irq_unmask(struct irq_data *d)
  79. {
  80. ar2315_rst_reg_mask(AR2315_IMR, 0, BIT(d->hwirq));
  81. }
  82. static void ar2315_misc_irq_mask(struct irq_data *d)
  83. {
  84. ar2315_rst_reg_mask(AR2315_IMR, BIT(d->hwirq), 0);
  85. }
  86. static struct irq_chip ar2315_misc_irq_chip = {
  87. .name = "ar2315-misc",
  88. .irq_unmask = ar2315_misc_irq_unmask,
  89. .irq_mask = ar2315_misc_irq_mask,
  90. };
  91. static int ar2315_misc_irq_map(struct irq_domain *d, unsigned irq,
  92. irq_hw_number_t hw)
  93. {
  94. irq_set_chip_and_handler(irq, &ar2315_misc_irq_chip, handle_level_irq);
  95. return 0;
  96. }
  97. static struct irq_domain_ops ar2315_misc_irq_domain_ops = {
  98. .map = ar2315_misc_irq_map,
  99. };
  100. /*
  101. * Called when an interrupt is received, this function
  102. * determines exactly which interrupt it was, and it
  103. * invokes the appropriate handler.
  104. *
  105. * Implicitly, we also define interrupt priority by
  106. * choosing which to dispatch first.
  107. */
  108. static void ar2315_irq_dispatch(void)
  109. {
  110. u32 pending = read_c0_status() & read_c0_cause();
  111. if (pending & CAUSEF_IP3)
  112. do_IRQ(AR2315_IRQ_WLAN0);
  113. #ifdef CONFIG_PCI_AR2315
  114. else if (pending & CAUSEF_IP5)
  115. do_IRQ(AR2315_IRQ_LCBUS_PCI);
  116. #endif
  117. else if (pending & CAUSEF_IP2)
  118. do_IRQ(AR2315_IRQ_MISC);
  119. else if (pending & CAUSEF_IP7)
  120. do_IRQ(ATH25_IRQ_CPU_CLOCK);
  121. else
  122. spurious_interrupt();
  123. }
  124. void __init ar2315_arch_init_irq(void)
  125. {
  126. struct irq_domain *domain;
  127. unsigned irq;
  128. ath25_irq_dispatch = ar2315_irq_dispatch;
  129. domain = irq_domain_add_linear(NULL, AR2315_MISC_IRQ_COUNT,
  130. &ar2315_misc_irq_domain_ops, NULL);
  131. if (!domain)
  132. panic("Failed to add IRQ domain");
  133. irq = irq_create_mapping(domain, AR2315_MISC_IRQ_AHB);
  134. setup_irq(irq, &ar2315_ahb_err_interrupt);
  135. irq_set_chained_handler_and_data(AR2315_IRQ_MISC,
  136. ar2315_misc_irq_handler, domain);
  137. ar2315_misc_irq_domain = domain;
  138. }
  139. void __init ar2315_init_devices(void)
  140. {
  141. /* Find board configuration */
  142. ath25_find_config(AR2315_SPI_READ_BASE, AR2315_SPI_READ_SIZE);
  143. ath25_add_wmac(0, AR2315_WLAN0_BASE, AR2315_IRQ_WLAN0);
  144. }
  145. static void ar2315_restart(char *command)
  146. {
  147. void (*mips_reset_vec)(void) = (void *)0xbfc00000;
  148. local_irq_disable();
  149. /* try reset the system via reset control */
  150. ar2315_rst_reg_write(AR2315_COLD_RESET, AR2317_RESET_SYSTEM);
  151. /* Cold reset does not work on the AR2315/6, use the GPIO reset bits
  152. * a workaround. Give it some time to attempt a gpio based hardware
  153. * reset (atheros reference design workaround) */
  154. /* TODO: implement the GPIO reset workaround */
  155. /* Some boards (e.g. Senao EOC-2610) don't implement the reset logic
  156. * workaround. Attempt to jump to the mips reset location -
  157. * the boot loader itself might be able to recover the system */
  158. mips_reset_vec();
  159. }
  160. /*
  161. * This table is indexed by bits 5..4 of the CLOCKCTL1 register
  162. * to determine the predevisor value.
  163. */
  164. static int clockctl1_predivide_table[4] __initdata = { 1, 2, 4, 5 };
  165. static int pllc_divide_table[5] __initdata = { 2, 3, 4, 6, 3 };
  166. static unsigned __init ar2315_sys_clk(u32 clock_ctl)
  167. {
  168. unsigned int pllc_ctrl, cpu_div;
  169. unsigned int pllc_out, refdiv, fdiv, divby2;
  170. unsigned int clk_div;
  171. pllc_ctrl = ar2315_rst_reg_read(AR2315_PLLC_CTL);
  172. refdiv = ATH25_REG_MS(pllc_ctrl, AR2315_PLLC_REF_DIV);
  173. refdiv = clockctl1_predivide_table[refdiv];
  174. fdiv = ATH25_REG_MS(pllc_ctrl, AR2315_PLLC_FDBACK_DIV);
  175. divby2 = ATH25_REG_MS(pllc_ctrl, AR2315_PLLC_ADD_FDBACK_DIV) + 1;
  176. pllc_out = (40000000 / refdiv) * (2 * divby2) * fdiv;
  177. /* clkm input selected */
  178. switch (clock_ctl & AR2315_CPUCLK_CLK_SEL_M) {
  179. case 0:
  180. case 1:
  181. clk_div = ATH25_REG_MS(pllc_ctrl, AR2315_PLLC_CLKM_DIV);
  182. clk_div = pllc_divide_table[clk_div];
  183. break;
  184. case 2:
  185. clk_div = ATH25_REG_MS(pllc_ctrl, AR2315_PLLC_CLKC_DIV);
  186. clk_div = pllc_divide_table[clk_div];
  187. break;
  188. default:
  189. pllc_out = 40000000;
  190. clk_div = 1;
  191. break;
  192. }
  193. cpu_div = ATH25_REG_MS(clock_ctl, AR2315_CPUCLK_CLK_DIV);
  194. cpu_div = cpu_div * 2 ?: 1;
  195. return pllc_out / (clk_div * cpu_div);
  196. }
  197. static inline unsigned ar2315_cpu_frequency(void)
  198. {
  199. return ar2315_sys_clk(ar2315_rst_reg_read(AR2315_CPUCLK));
  200. }
  201. static inline unsigned ar2315_apb_frequency(void)
  202. {
  203. return ar2315_sys_clk(ar2315_rst_reg_read(AR2315_AMBACLK));
  204. }
  205. void __init ar2315_plat_time_init(void)
  206. {
  207. mips_hpt_frequency = ar2315_cpu_frequency() / 2;
  208. }
  209. void __init ar2315_plat_mem_setup(void)
  210. {
  211. void __iomem *sdram_base;
  212. u32 memsize, memcfg;
  213. u32 devid;
  214. u32 config;
  215. /* Detect memory size */
  216. sdram_base = ioremap_nocache(AR2315_SDRAMCTL_BASE,
  217. AR2315_SDRAMCTL_SIZE);
  218. memcfg = __raw_readl(sdram_base + AR2315_MEM_CFG);
  219. memsize = 1 + ATH25_REG_MS(memcfg, AR2315_MEM_CFG_DATA_WIDTH);
  220. memsize <<= 1 + ATH25_REG_MS(memcfg, AR2315_MEM_CFG_COL_WIDTH);
  221. memsize <<= 1 + ATH25_REG_MS(memcfg, AR2315_MEM_CFG_ROW_WIDTH);
  222. memsize <<= 3;
  223. add_memory_region(0, memsize, BOOT_MEM_RAM);
  224. iounmap(sdram_base);
  225. ar2315_rst_base = ioremap_nocache(AR2315_RST_BASE, AR2315_RST_SIZE);
  226. /* Detect the hardware based on the device ID */
  227. devid = ar2315_rst_reg_read(AR2315_SREV) & AR2315_REV_CHIP;
  228. switch (devid) {
  229. case 0x91: /* Need to check */
  230. ath25_soc = ATH25_SOC_AR2318;
  231. break;
  232. case 0x90:
  233. ath25_soc = ATH25_SOC_AR2317;
  234. break;
  235. case 0x87:
  236. ath25_soc = ATH25_SOC_AR2316;
  237. break;
  238. case 0x86:
  239. default:
  240. ath25_soc = ATH25_SOC_AR2315;
  241. break;
  242. }
  243. ath25_board.devid = devid;
  244. /* Clear any lingering AHB errors */
  245. config = read_c0_config();
  246. write_c0_config(config & ~0x3);
  247. ar2315_rst_reg_write(AR2315_AHB_ERR0, AR2315_AHB_ERROR_DET);
  248. ar2315_rst_reg_read(AR2315_AHB_ERR1);
  249. ar2315_rst_reg_write(AR2315_WDT_CTRL, AR2315_WDT_CTRL_IGNORE);
  250. _machine_restart = ar2315_restart;
  251. }
  252. #ifdef CONFIG_PCI_AR2315
  253. static struct resource ar2315_pci_res[] = {
  254. {
  255. .name = "ar2315-pci-ctrl",
  256. .flags = IORESOURCE_MEM,
  257. .start = AR2315_PCI_BASE,
  258. .end = AR2315_PCI_BASE + AR2315_PCI_SIZE - 1,
  259. },
  260. {
  261. .name = "ar2315-pci-ext",
  262. .flags = IORESOURCE_MEM,
  263. .start = AR2315_PCI_EXT_BASE,
  264. .end = AR2315_PCI_EXT_BASE + AR2315_PCI_EXT_SIZE - 1,
  265. },
  266. {
  267. .name = "ar2315-pci",
  268. .flags = IORESOURCE_IRQ,
  269. .start = AR2315_IRQ_LCBUS_PCI,
  270. .end = AR2315_IRQ_LCBUS_PCI,
  271. },
  272. };
  273. #endif
  274. void __init ar2315_arch_init(void)
  275. {
  276. unsigned irq = irq_create_mapping(ar2315_misc_irq_domain,
  277. AR2315_MISC_IRQ_UART0);
  278. ath25_serial_setup(AR2315_UART0_BASE, irq, ar2315_apb_frequency());
  279. #ifdef CONFIG_PCI_AR2315
  280. if (ath25_soc == ATH25_SOC_AR2315) {
  281. /* Reset PCI DMA logic */
  282. ar2315_rst_reg_mask(AR2315_RESET, 0, AR2315_RESET_PCIDMA);
  283. msleep(20);
  284. ar2315_rst_reg_mask(AR2315_RESET, AR2315_RESET_PCIDMA, 0);
  285. msleep(20);
  286. /* Configure endians */
  287. ar2315_rst_reg_mask(AR2315_ENDIAN_CTL, 0, AR2315_CONFIG_PCIAHB |
  288. AR2315_CONFIG_PCIAHB_BRIDGE);
  289. /* Configure as PCI host with DMA */
  290. ar2315_rst_reg_write(AR2315_PCICLK, AR2315_PCICLK_PLLC_CLKM |
  291. (AR2315_PCICLK_IN_FREQ_DIV_6 <<
  292. AR2315_PCICLK_DIV_S));
  293. ar2315_rst_reg_mask(AR2315_AHB_ARB_CTL, 0, AR2315_ARB_PCI);
  294. ar2315_rst_reg_mask(AR2315_IF_CTL, AR2315_IF_PCI_CLK_MASK |
  295. AR2315_IF_MASK, AR2315_IF_PCI |
  296. AR2315_IF_PCI_HOST | AR2315_IF_PCI_INTR |
  297. (AR2315_IF_PCI_CLK_OUTPUT_CLK <<
  298. AR2315_IF_PCI_CLK_SHIFT));
  299. platform_device_register_simple("ar2315-pci", -1,
  300. ar2315_pci_res,
  301. ARRAY_SIZE(ar2315_pci_res));
  302. }
  303. #endif
  304. }