time.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2001, 2002, 2003 MontaVista Software Inc.
  3. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  4. * Copyright (C) 2007 Ralf Baechle (ralf@linux-mips.org)
  5. *
  6. * Common time service routines for MIPS machines. See
  7. * Documents/MIPS/README.txt.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/param.h>
  19. #include <linux/time.h>
  20. #include <linux/timer.h>
  21. #include <linux/smp.h>
  22. #include <linux/kernel_stat.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <asm/bootinfo.h>
  26. #include <asm/cpu.h>
  27. #include <asm/time.h>
  28. #include <asm/hardirq.h>
  29. #include <asm/div64.h>
  30. #include <asm/debug.h>
  31. #include <int.h>
  32. #include <cm.h>
  33. static unsigned long cpj;
  34. static cycle_t hpt_read(struct clocksource *cs)
  35. {
  36. return read_c0_count2();
  37. }
  38. static struct clocksource pnx_clocksource = {
  39. .name = "pnx8xxx",
  40. .rating = 200,
  41. .read = hpt_read,
  42. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  43. };
  44. static irqreturn_t pnx8xxx_timer_interrupt(int irq, void *dev_id)
  45. {
  46. struct clock_event_device *c = dev_id;
  47. /* clear MATCH, signal the event */
  48. c->event_handler(c);
  49. return IRQ_HANDLED;
  50. }
  51. static struct irqaction pnx8xxx_timer_irq = {
  52. .handler = pnx8xxx_timer_interrupt,
  53. .flags = IRQF_DISABLED | IRQF_PERCPU,
  54. .name = "pnx8xxx_timer",
  55. };
  56. static irqreturn_t monotonic_interrupt(int irq, void *dev_id)
  57. {
  58. /* Timer 2 clear interrupt */
  59. write_c0_compare2(-1);
  60. return IRQ_HANDLED;
  61. }
  62. static struct irqaction monotonic_irqaction = {
  63. .handler = monotonic_interrupt,
  64. .flags = IRQF_DISABLED,
  65. .name = "Monotonic timer",
  66. };
  67. static int pnx8xxx_set_next_event(unsigned long delta,
  68. struct clock_event_device *evt)
  69. {
  70. write_c0_compare(delta);
  71. return 0;
  72. }
  73. static struct clock_event_device pnx8xxx_clockevent = {
  74. .name = "pnx8xxx_clockevent",
  75. .features = CLOCK_EVT_FEAT_ONESHOT,
  76. .set_next_event = pnx8xxx_set_next_event,
  77. };
  78. static inline void timer_ack(void)
  79. {
  80. write_c0_compare(cpj);
  81. }
  82. __init void plat_time_init(void)
  83. {
  84. unsigned int configPR;
  85. unsigned int n;
  86. unsigned int m;
  87. unsigned int p;
  88. unsigned int pow2p;
  89. pnx8xxx_clockevent.cpumask = cpu_none_mask;
  90. clockevents_register_device(&pnx8xxx_clockevent);
  91. clocksource_register(&pnx_clocksource);
  92. /* Timer 1 start */
  93. configPR = read_c0_config7();
  94. configPR &= ~0x00000008;
  95. write_c0_config7(configPR);
  96. /* Timer 2 start */
  97. configPR = read_c0_config7();
  98. configPR &= ~0x00000010;
  99. write_c0_config7(configPR);
  100. /* Timer 3 stop */
  101. configPR = read_c0_config7();
  102. configPR |= 0x00000020;
  103. write_c0_config7(configPR);
  104. /* PLL0 sets MIPS clock (PLL1 <=> TM1, PLL6 <=> TM2, PLL5 <=> mem) */
  105. /* (but only if CLK_MIPS_CTL select value [bits 3:1] is 1: FIXME) */
  106. n = (PNX8550_CM_PLL0_CTL & PNX8550_CM_PLL_N_MASK) >> 16;
  107. m = (PNX8550_CM_PLL0_CTL & PNX8550_CM_PLL_M_MASK) >> 8;
  108. p = (PNX8550_CM_PLL0_CTL & PNX8550_CM_PLL_P_MASK) >> 2;
  109. pow2p = (1 << p);
  110. db_assert(m != 0 && pow2p != 0);
  111. /*
  112. * Compute the frequency as in the PNX8550 User Manual 1.0, p.186
  113. * (a.k.a. 8-10). Divide by HZ for a timer offset that results in
  114. * HZ timer interrupts per second.
  115. */
  116. mips_hpt_frequency = 27UL * ((1000000UL * n)/(m * pow2p));
  117. cpj = (mips_hpt_frequency + HZ / 2) / HZ;
  118. write_c0_count(0);
  119. timer_ack();
  120. /* Setup Timer 2 */
  121. write_c0_count2(0);
  122. write_c0_compare2(0xffffffff);
  123. setup_irq(PNX8550_INT_TIMER1, &pnx8xxx_timer_irq);
  124. setup_irq(PNX8550_INT_TIMER2, &monotonic_irqaction);
  125. }