core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright 1999 - 2003 ARM Limited
  3. * Copyright 2000 Deep Blue Solutions Ltd
  4. * Copyright 2008 Cavium Networks
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, Version 2, as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/clockchips.h>
  13. #include <linux/io.h>
  14. #include <linux/irqchip/arm-gic.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/usb/ehci_pdriver.h>
  18. #include <linux/usb/ohci_pdriver.h>
  19. #include <asm/mach/arch.h>
  20. #include <asm/mach/map.h>
  21. #include <asm/mach/time.h>
  22. #include <asm/mach/irq.h>
  23. #include <asm/hardware/cache-l2x0.h>
  24. #include "cns3xxx.h"
  25. #include "core.h"
  26. #include "pm.h"
  27. static struct map_desc cns3xxx_io_desc[] __initdata = {
  28. {
  29. .virtual = CNS3XXX_TC11MP_SCU_BASE_VIRT,
  30. .pfn = __phys_to_pfn(CNS3XXX_TC11MP_SCU_BASE),
  31. .length = SZ_8K,
  32. .type = MT_DEVICE,
  33. }, {
  34. .virtual = CNS3XXX_TIMER1_2_3_BASE_VIRT,
  35. .pfn = __phys_to_pfn(CNS3XXX_TIMER1_2_3_BASE),
  36. .length = SZ_4K,
  37. .type = MT_DEVICE,
  38. }, {
  39. .virtual = CNS3XXX_MISC_BASE_VIRT,
  40. .pfn = __phys_to_pfn(CNS3XXX_MISC_BASE),
  41. .length = SZ_4K,
  42. .type = MT_DEVICE,
  43. }, {
  44. .virtual = CNS3XXX_PM_BASE_VIRT,
  45. .pfn = __phys_to_pfn(CNS3XXX_PM_BASE),
  46. .length = SZ_4K,
  47. .type = MT_DEVICE,
  48. #ifdef CONFIG_PCI
  49. }, {
  50. .virtual = CNS3XXX_PCIE0_HOST_BASE_VIRT,
  51. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_HOST_BASE),
  52. .length = SZ_4K,
  53. .type = MT_DEVICE,
  54. }, {
  55. .virtual = CNS3XXX_PCIE0_CFG0_BASE_VIRT,
  56. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_CFG0_BASE),
  57. .length = SZ_64K, /* really 4 KiB at offset 32 KiB */
  58. .type = MT_DEVICE,
  59. }, {
  60. .virtual = CNS3XXX_PCIE0_CFG1_BASE_VIRT,
  61. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_CFG1_BASE),
  62. .length = SZ_16M,
  63. .type = MT_DEVICE,
  64. }, {
  65. .virtual = CNS3XXX_PCIE1_HOST_BASE_VIRT,
  66. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_HOST_BASE),
  67. .length = SZ_4K,
  68. .type = MT_DEVICE,
  69. }, {
  70. .virtual = CNS3XXX_PCIE1_CFG0_BASE_VIRT,
  71. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_CFG0_BASE),
  72. .length = SZ_64K, /* really 4 KiB at offset 32 KiB */
  73. .type = MT_DEVICE,
  74. }, {
  75. .virtual = CNS3XXX_PCIE1_CFG1_BASE_VIRT,
  76. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_CFG1_BASE),
  77. .length = SZ_16M,
  78. .type = MT_DEVICE,
  79. #endif
  80. },
  81. };
  82. void __init cns3xxx_map_io(void)
  83. {
  84. iotable_init(cns3xxx_io_desc, ARRAY_SIZE(cns3xxx_io_desc));
  85. }
  86. /* used by entry-macro.S */
  87. void __init cns3xxx_init_irq(void)
  88. {
  89. gic_init(0, 29, IOMEM(CNS3XXX_TC11MP_GIC_DIST_BASE_VIRT),
  90. IOMEM(CNS3XXX_TC11MP_GIC_CPU_BASE_VIRT));
  91. }
  92. void cns3xxx_power_off(void)
  93. {
  94. u32 __iomem *pm_base = IOMEM(CNS3XXX_PM_BASE_VIRT);
  95. u32 clkctrl;
  96. printk(KERN_INFO "powering system down...\n");
  97. clkctrl = readl(pm_base + PM_SYS_CLK_CTRL_OFFSET);
  98. clkctrl &= 0xfffff1ff;
  99. clkctrl |= (0x5 << 9); /* Hibernate */
  100. writel(clkctrl, pm_base + PM_SYS_CLK_CTRL_OFFSET);
  101. }
  102. /*
  103. * Timer
  104. */
  105. static void __iomem *cns3xxx_tmr1;
  106. static int cns3xxx_shutdown(struct clock_event_device *clk)
  107. {
  108. writel(0, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  109. return 0;
  110. }
  111. static int cns3xxx_set_oneshot(struct clock_event_device *clk)
  112. {
  113. unsigned long ctrl = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  114. /* period set, and timer enabled in 'next_event' hook */
  115. ctrl |= (1 << 2) | (1 << 9);
  116. writel(ctrl, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  117. return 0;
  118. }
  119. static int cns3xxx_set_periodic(struct clock_event_device *clk)
  120. {
  121. unsigned long ctrl = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  122. int pclk = cns3xxx_cpu_clock() / 8;
  123. int reload;
  124. reload = pclk * 20 / (3 * HZ) * 0x25000;
  125. writel(reload, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
  126. ctrl |= (1 << 0) | (1 << 2) | (1 << 9);
  127. writel(ctrl, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  128. return 0;
  129. }
  130. static int cns3xxx_timer_set_next_event(unsigned long evt,
  131. struct clock_event_device *unused)
  132. {
  133. unsigned long ctrl = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  134. writel(evt, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
  135. writel(ctrl | (1 << 0), cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  136. return 0;
  137. }
  138. static struct clock_event_device cns3xxx_tmr1_clockevent = {
  139. .name = "cns3xxx timer1",
  140. .features = CLOCK_EVT_FEAT_PERIODIC |
  141. CLOCK_EVT_FEAT_ONESHOT,
  142. .set_state_shutdown = cns3xxx_shutdown,
  143. .set_state_periodic = cns3xxx_set_periodic,
  144. .set_state_oneshot = cns3xxx_set_oneshot,
  145. .tick_resume = cns3xxx_shutdown,
  146. .set_next_event = cns3xxx_timer_set_next_event,
  147. .rating = 350,
  148. .cpumask = cpu_all_mask,
  149. };
  150. static void __init cns3xxx_clockevents_init(unsigned int timer_irq)
  151. {
  152. cns3xxx_tmr1_clockevent.irq = timer_irq;
  153. clockevents_config_and_register(&cns3xxx_tmr1_clockevent,
  154. (cns3xxx_cpu_clock() >> 3) * 1000000,
  155. 0xf, 0xffffffff);
  156. }
  157. /*
  158. * IRQ handler for the timer
  159. */
  160. static irqreturn_t cns3xxx_timer_interrupt(int irq, void *dev_id)
  161. {
  162. struct clock_event_device *evt = &cns3xxx_tmr1_clockevent;
  163. u32 __iomem *stat = cns3xxx_tmr1 + TIMER1_2_INTERRUPT_STATUS_OFFSET;
  164. u32 val;
  165. /* Clear the interrupt */
  166. val = readl(stat);
  167. writel(val & ~(1 << 2), stat);
  168. evt->event_handler(evt);
  169. return IRQ_HANDLED;
  170. }
  171. static struct irqaction cns3xxx_timer_irq = {
  172. .name = "timer",
  173. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  174. .handler = cns3xxx_timer_interrupt,
  175. };
  176. /*
  177. * Set up the clock source and clock events devices
  178. */
  179. static void __init __cns3xxx_timer_init(unsigned int timer_irq)
  180. {
  181. u32 val;
  182. u32 irq_mask;
  183. /*
  184. * Initialise to a known state (all timers off)
  185. */
  186. /* disable timer1 and timer2 */
  187. writel(0, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  188. /* stop free running timer3 */
  189. writel(0, cns3xxx_tmr1 + TIMER_FREERUN_CONTROL_OFFSET);
  190. /* timer1 */
  191. writel(0x5C800, cns3xxx_tmr1 + TIMER1_COUNTER_OFFSET);
  192. writel(0x5C800, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
  193. writel(0, cns3xxx_tmr1 + TIMER1_MATCH_V1_OFFSET);
  194. writel(0, cns3xxx_tmr1 + TIMER1_MATCH_V2_OFFSET);
  195. /* mask irq, non-mask timer1 overflow */
  196. irq_mask = readl(cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
  197. irq_mask &= ~(1 << 2);
  198. irq_mask |= 0x03;
  199. writel(irq_mask, cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
  200. /* down counter */
  201. val = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  202. val |= (1 << 9);
  203. writel(val, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  204. /* timer2 */
  205. writel(0, cns3xxx_tmr1 + TIMER2_MATCH_V1_OFFSET);
  206. writel(0, cns3xxx_tmr1 + TIMER2_MATCH_V2_OFFSET);
  207. /* mask irq */
  208. irq_mask = readl(cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
  209. irq_mask |= ((1 << 3) | (1 << 4) | (1 << 5));
  210. writel(irq_mask, cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
  211. /* down counter */
  212. val = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  213. val |= (1 << 10);
  214. writel(val, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  215. /* Make irqs happen for the system timer */
  216. setup_irq(timer_irq, &cns3xxx_timer_irq);
  217. cns3xxx_clockevents_init(timer_irq);
  218. }
  219. void __init cns3xxx_timer_init(void)
  220. {
  221. cns3xxx_tmr1 = IOMEM(CNS3XXX_TIMER1_2_3_BASE_VIRT);
  222. __cns3xxx_timer_init(IRQ_CNS3XXX_TIMER0);
  223. }
  224. #ifdef CONFIG_CACHE_L2X0
  225. void __init cns3xxx_l2x0_init(void)
  226. {
  227. void __iomem *base = ioremap(CNS3XXX_L2C_BASE, SZ_4K);
  228. u32 val;
  229. if (WARN_ON(!base))
  230. return;
  231. /*
  232. * Tag RAM Control register
  233. *
  234. * bit[10:8] - 1 cycle of write accesses latency
  235. * bit[6:4] - 1 cycle of read accesses latency
  236. * bit[3:0] - 1 cycle of setup latency
  237. *
  238. * 1 cycle of latency for setup, read and write accesses
  239. */
  240. val = readl(base + L310_TAG_LATENCY_CTRL);
  241. val &= 0xfffff888;
  242. writel(val, base + L310_TAG_LATENCY_CTRL);
  243. /*
  244. * Data RAM Control register
  245. *
  246. * bit[10:8] - 1 cycles of write accesses latency
  247. * bit[6:4] - 1 cycles of read accesses latency
  248. * bit[3:0] - 1 cycle of setup latency
  249. *
  250. * 1 cycle of latency for setup, read and write accesses
  251. */
  252. val = readl(base + L310_DATA_LATENCY_CTRL);
  253. val &= 0xfffff888;
  254. writel(val, base + L310_DATA_LATENCY_CTRL);
  255. /* 32 KiB, 8-way, parity disable */
  256. l2x0_init(base, 0x00500000, 0xfe0f0fff);
  257. }
  258. #endif /* CONFIG_CACHE_L2X0 */
  259. static int csn3xxx_usb_power_on(struct platform_device *pdev)
  260. {
  261. /*
  262. * EHCI and OHCI share the same clock and power,
  263. * resetting twice would cause the 1st controller been reset.
  264. * Therefore only do power up at the first up device, and
  265. * power down at the last down device.
  266. *
  267. * Set USB AHB INCR length to 16
  268. */
  269. if (atomic_inc_return(&usb_pwr_ref) == 1) {
  270. cns3xxx_pwr_power_up(1 << PM_PLL_HM_PD_CTRL_REG_OFFSET_PLL_USB);
  271. cns3xxx_pwr_clk_en(1 << PM_CLK_GATE_REG_OFFSET_USB_HOST);
  272. cns3xxx_pwr_soft_rst(1 << PM_SOFT_RST_REG_OFFST_USB_HOST);
  273. __raw_writel((__raw_readl(MISC_CHIP_CONFIG_REG) | (0X2 << 24)),
  274. MISC_CHIP_CONFIG_REG);
  275. }
  276. return 0;
  277. }
  278. static void csn3xxx_usb_power_off(struct platform_device *pdev)
  279. {
  280. /*
  281. * EHCI and OHCI share the same clock and power,
  282. * resetting twice would cause the 1st controller been reset.
  283. * Therefore only do power up at the first up device, and
  284. * power down at the last down device.
  285. */
  286. if (atomic_dec_return(&usb_pwr_ref) == 0)
  287. cns3xxx_pwr_clk_dis(1 << PM_CLK_GATE_REG_OFFSET_USB_HOST);
  288. }
  289. static struct usb_ehci_pdata cns3xxx_usb_ehci_pdata = {
  290. .power_on = csn3xxx_usb_power_on,
  291. .power_off = csn3xxx_usb_power_off,
  292. };
  293. static struct usb_ohci_pdata cns3xxx_usb_ohci_pdata = {
  294. .num_ports = 1,
  295. .power_on = csn3xxx_usb_power_on,
  296. .power_off = csn3xxx_usb_power_off,
  297. };
  298. static const struct of_dev_auxdata cns3xxx_auxdata[] __initconst = {
  299. { "intel,usb-ehci", CNS3XXX_USB_BASE, "ehci-platform", &cns3xxx_usb_ehci_pdata },
  300. { "intel,usb-ohci", CNS3XXX_USB_OHCI_BASE, "ohci-platform", &cns3xxx_usb_ohci_pdata },
  301. { "cavium,cns3420-ahci", CNS3XXX_SATA2_BASE, "ahci", NULL },
  302. { "cavium,cns3420-sdhci", CNS3XXX_SDIO_BASE, "ahci", NULL },
  303. {},
  304. };
  305. static void __init cns3xxx_init(void)
  306. {
  307. struct device_node *dn;
  308. cns3xxx_l2x0_init();
  309. dn = of_find_compatible_node(NULL, NULL, "cavium,cns3420-ahci");
  310. if (of_device_is_available(dn)) {
  311. u32 tmp;
  312. tmp = __raw_readl(MISC_SATA_POWER_MODE);
  313. tmp |= 0x1 << 16; /* Disable SATA PHY 0 from SLUMBER Mode */
  314. tmp |= 0x1 << 17; /* Disable SATA PHY 1 from SLUMBER Mode */
  315. __raw_writel(tmp, MISC_SATA_POWER_MODE);
  316. /* Enable SATA PHY */
  317. cns3xxx_pwr_power_up(0x1 << PM_PLL_HM_PD_CTRL_REG_OFFSET_SATA_PHY0);
  318. cns3xxx_pwr_power_up(0x1 << PM_PLL_HM_PD_CTRL_REG_OFFSET_SATA_PHY1);
  319. /* Enable SATA Clock */
  320. cns3xxx_pwr_clk_en(0x1 << PM_CLK_GATE_REG_OFFSET_SATA);
  321. /* De-Asscer SATA Reset */
  322. cns3xxx_pwr_soft_rst(CNS3XXX_PWR_SOFTWARE_RST(SATA));
  323. }
  324. dn = of_find_compatible_node(NULL, NULL, "cavium,cns3420-sdhci");
  325. if (of_device_is_available(dn)) {
  326. u32 __iomem *gpioa = IOMEM(CNS3XXX_MISC_BASE_VIRT + 0x0014);
  327. u32 gpioa_pins = __raw_readl(gpioa);
  328. /* MMC/SD pins share with GPIOA */
  329. gpioa_pins |= 0x1fff0004;
  330. __raw_writel(gpioa_pins, gpioa);
  331. cns3xxx_pwr_clk_en(CNS3XXX_PWR_CLK_EN(SDIO));
  332. cns3xxx_pwr_soft_rst(CNS3XXX_PWR_SOFTWARE_RST(SDIO));
  333. }
  334. pm_power_off = cns3xxx_power_off;
  335. of_platform_default_populate(NULL, cns3xxx_auxdata, NULL);
  336. }
  337. static const char *const cns3xxx_dt_compat[] __initconst = {
  338. "cavium,cns3410",
  339. "cavium,cns3420",
  340. NULL,
  341. };
  342. DT_MACHINE_START(CNS3XXX_DT, "Cavium Networks CNS3xxx")
  343. .dt_compat = cns3xxx_dt_compat,
  344. .map_io = cns3xxx_map_io,
  345. .init_irq = cns3xxx_init_irq,
  346. .init_time = cns3xxx_timer_init,
  347. .init_machine = cns3xxx_init,
  348. .init_late = cns3xxx_pcie_init_late,
  349. .restart = cns3xxx_restart,
  350. MACHINE_END