core.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <asm/mach/map.h>
  15. #include <asm/mach/time.h>
  16. #include <asm/mach/irq.h>
  17. #include <asm/hardware/gic.h>
  18. #include <mach/cns3xxx.h>
  19. #include "core.h"
  20. static struct map_desc cns3xxx_io_desc[] __initdata = {
  21. {
  22. .virtual = CNS3XXX_TC11MP_TWD_BASE_VIRT,
  23. .pfn = __phys_to_pfn(CNS3XXX_TC11MP_TWD_BASE),
  24. .length = SZ_4K,
  25. .type = MT_DEVICE,
  26. }, {
  27. .virtual = CNS3XXX_TC11MP_GIC_CPU_BASE_VIRT,
  28. .pfn = __phys_to_pfn(CNS3XXX_TC11MP_GIC_CPU_BASE),
  29. .length = SZ_4K,
  30. .type = MT_DEVICE,
  31. }, {
  32. .virtual = CNS3XXX_TC11MP_GIC_DIST_BASE_VIRT,
  33. .pfn = __phys_to_pfn(CNS3XXX_TC11MP_GIC_DIST_BASE),
  34. .length = SZ_4K,
  35. .type = MT_DEVICE,
  36. }, {
  37. .virtual = CNS3XXX_TIMER1_2_3_BASE_VIRT,
  38. .pfn = __phys_to_pfn(CNS3XXX_TIMER1_2_3_BASE),
  39. .length = SZ_4K,
  40. .type = MT_DEVICE,
  41. }, {
  42. .virtual = CNS3XXX_GPIOA_BASE_VIRT,
  43. .pfn = __phys_to_pfn(CNS3XXX_GPIOA_BASE),
  44. .length = SZ_4K,
  45. .type = MT_DEVICE,
  46. }, {
  47. .virtual = CNS3XXX_GPIOB_BASE_VIRT,
  48. .pfn = __phys_to_pfn(CNS3XXX_GPIOB_BASE),
  49. .length = SZ_4K,
  50. .type = MT_DEVICE,
  51. }, {
  52. .virtual = CNS3XXX_MISC_BASE_VIRT,
  53. .pfn = __phys_to_pfn(CNS3XXX_MISC_BASE),
  54. .length = SZ_4K,
  55. .type = MT_DEVICE,
  56. }, {
  57. .virtual = CNS3XXX_PM_BASE_VIRT,
  58. .pfn = __phys_to_pfn(CNS3XXX_PM_BASE),
  59. .length = SZ_4K,
  60. .type = MT_DEVICE,
  61. },
  62. };
  63. void __init cns3xxx_map_io(void)
  64. {
  65. iotable_init(cns3xxx_io_desc, ARRAY_SIZE(cns3xxx_io_desc));
  66. }
  67. /* used by entry-macro.S */
  68. void __iomem *gic_cpu_base_addr;
  69. void __init cns3xxx_init_irq(void)
  70. {
  71. gic_cpu_base_addr = __io(CNS3XXX_TC11MP_GIC_CPU_BASE_VIRT);
  72. gic_dist_init(0, __io(CNS3XXX_TC11MP_GIC_DIST_BASE_VIRT), 29);
  73. gic_cpu_init(0, gic_cpu_base_addr);
  74. }
  75. void cns3xxx_power_off(void)
  76. {
  77. u32 __iomem *pm_base = __io(CNS3XXX_PM_BASE_VIRT);
  78. u32 clkctrl;
  79. printk(KERN_INFO "powering system down...\n");
  80. clkctrl = readl(pm_base + PM_SYS_CLK_CTRL_OFFSET);
  81. clkctrl &= 0xfffff1ff;
  82. clkctrl |= (0x5 << 9); /* Hibernate */
  83. writel(clkctrl, pm_base + PM_SYS_CLK_CTRL_OFFSET);
  84. }
  85. /*
  86. * Timer
  87. */
  88. static void __iomem *cns3xxx_tmr1;
  89. static void cns3xxx_timer_set_mode(enum clock_event_mode mode,
  90. struct clock_event_device *clk)
  91. {
  92. unsigned long ctrl = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  93. int pclk = cns3xxx_cpu_clock() / 8;
  94. int reload;
  95. switch (mode) {
  96. case CLOCK_EVT_MODE_PERIODIC:
  97. reload = pclk * 20 / (3 * HZ) * 0x25000;
  98. writel(reload, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
  99. ctrl |= (1 << 0) | (1 << 2) | (1 << 9);
  100. break;
  101. case CLOCK_EVT_MODE_ONESHOT:
  102. /* period set, and timer enabled in 'next_event' hook */
  103. ctrl |= (1 << 2) | (1 << 9);
  104. break;
  105. case CLOCK_EVT_MODE_UNUSED:
  106. case CLOCK_EVT_MODE_SHUTDOWN:
  107. default:
  108. ctrl = 0;
  109. }
  110. writel(ctrl, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  111. }
  112. static int cns3xxx_timer_set_next_event(unsigned long evt,
  113. struct clock_event_device *unused)
  114. {
  115. unsigned long ctrl = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  116. writel(evt, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
  117. writel(ctrl | (1 << 0), cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  118. return 0;
  119. }
  120. static struct clock_event_device cns3xxx_tmr1_clockevent = {
  121. .name = "cns3xxx timer1",
  122. .shift = 8,
  123. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  124. .set_mode = cns3xxx_timer_set_mode,
  125. .set_next_event = cns3xxx_timer_set_next_event,
  126. .rating = 350,
  127. .cpumask = cpu_all_mask,
  128. };
  129. static void __init cns3xxx_clockevents_init(unsigned int timer_irq)
  130. {
  131. cns3xxx_tmr1_clockevent.irq = timer_irq;
  132. cns3xxx_tmr1_clockevent.mult =
  133. div_sc((cns3xxx_cpu_clock() >> 3) * 1000000, NSEC_PER_SEC,
  134. cns3xxx_tmr1_clockevent.shift);
  135. cns3xxx_tmr1_clockevent.max_delta_ns =
  136. clockevent_delta2ns(0xffffffff, &cns3xxx_tmr1_clockevent);
  137. cns3xxx_tmr1_clockevent.min_delta_ns =
  138. clockevent_delta2ns(0xf, &cns3xxx_tmr1_clockevent);
  139. clockevents_register_device(&cns3xxx_tmr1_clockevent);
  140. }
  141. /*
  142. * IRQ handler for the timer
  143. */
  144. static irqreturn_t cns3xxx_timer_interrupt(int irq, void *dev_id)
  145. {
  146. struct clock_event_device *evt = &cns3xxx_tmr1_clockevent;
  147. u32 __iomem *stat = cns3xxx_tmr1 + TIMER1_2_INTERRUPT_STATUS_OFFSET;
  148. u32 val;
  149. /* Clear the interrupt */
  150. val = readl(stat);
  151. writel(val & ~(1 << 2), stat);
  152. evt->event_handler(evt);
  153. return IRQ_HANDLED;
  154. }
  155. static struct irqaction cns3xxx_timer_irq = {
  156. .name = "timer",
  157. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  158. .handler = cns3xxx_timer_interrupt,
  159. };
  160. /*
  161. * Set up the clock source and clock events devices
  162. */
  163. static void __init __cns3xxx_timer_init(unsigned int timer_irq)
  164. {
  165. u32 val;
  166. u32 irq_mask;
  167. /*
  168. * Initialise to a known state (all timers off)
  169. */
  170. /* disable timer1 and timer2 */
  171. writel(0, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  172. /* stop free running timer3 */
  173. writel(0, cns3xxx_tmr1 + TIMER_FREERUN_CONTROL_OFFSET);
  174. /* timer1 */
  175. writel(0x5C800, cns3xxx_tmr1 + TIMER1_COUNTER_OFFSET);
  176. writel(0x5C800, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
  177. writel(0, cns3xxx_tmr1 + TIMER1_MATCH_V1_OFFSET);
  178. writel(0, cns3xxx_tmr1 + TIMER1_MATCH_V2_OFFSET);
  179. /* mask irq, non-mask timer1 overflow */
  180. irq_mask = readl(cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
  181. irq_mask &= ~(1 << 2);
  182. irq_mask |= 0x03;
  183. writel(irq_mask, cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
  184. /* down counter */
  185. val = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  186. val |= (1 << 9);
  187. writel(val, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  188. /* timer2 */
  189. writel(0, cns3xxx_tmr1 + TIMER2_MATCH_V1_OFFSET);
  190. writel(0, cns3xxx_tmr1 + TIMER2_MATCH_V2_OFFSET);
  191. /* mask irq */
  192. irq_mask = readl(cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
  193. irq_mask |= ((1 << 3) | (1 << 4) | (1 << 5));
  194. writel(irq_mask, cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
  195. /* down counter */
  196. val = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  197. val |= (1 << 10);
  198. writel(val, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
  199. /* Make irqs happen for the system timer */
  200. setup_irq(timer_irq, &cns3xxx_timer_irq);
  201. cns3xxx_clockevents_init(timer_irq);
  202. }
  203. static void __init cns3xxx_timer_init(void)
  204. {
  205. cns3xxx_tmr1 = __io(CNS3XXX_TIMER1_2_3_BASE_VIRT);
  206. __cns3xxx_timer_init(IRQ_CNS3XXX_TIMER0);
  207. }
  208. struct sys_timer cns3xxx_timer = {
  209. .init = cns3xxx_timer_init,
  210. };