time.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2001, 2002, 2003 MontaVista Software Inc.
  3. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  4. *
  5. * Common time service routines for MIPS machines. See
  6. * Documents/MIPS/README.txt.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/sched.h>
  17. #include <linux/param.h>
  18. #include <linux/time.h>
  19. #include <linux/timer.h>
  20. #include <linux/smp.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.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(void)
  35. {
  36. return read_c0_count2();
  37. }
  38. static void timer_ack(void)
  39. {
  40. write_c0_compare(cpj);
  41. }
  42. /*
  43. * pnx8550_time_init() - it does the following things:
  44. *
  45. * 1) board_time_init() -
  46. * a) (optional) set up RTC routines,
  47. * b) (optional) calibrate and set the mips_hpt_frequency
  48. * (only needed if you intended to use cpu counter as timer interrupt
  49. * source)
  50. */
  51. void pnx8550_time_init(void)
  52. {
  53. unsigned int n;
  54. unsigned int m;
  55. unsigned int p;
  56. unsigned int pow2p;
  57. /* PLL0 sets MIPS clock (PLL1 <=> TM1, PLL6 <=> TM2, PLL5 <=> mem) */
  58. /* (but only if CLK_MIPS_CTL select value [bits 3:1] is 1: FIXME) */
  59. n = (PNX8550_CM_PLL0_CTL & PNX8550_CM_PLL_N_MASK) >> 16;
  60. m = (PNX8550_CM_PLL0_CTL & PNX8550_CM_PLL_M_MASK) >> 8;
  61. p = (PNX8550_CM_PLL0_CTL & PNX8550_CM_PLL_P_MASK) >> 2;
  62. pow2p = (1 << p);
  63. db_assert(m != 0 && pow2p != 0);
  64. /*
  65. * Compute the frequency as in the PNX8550 User Manual 1.0, p.186
  66. * (a.k.a. 8-10). Divide by HZ for a timer offset that results in
  67. * HZ timer interrupts per second.
  68. */
  69. mips_hpt_frequency = 27UL * ((1000000UL * n)/(m * pow2p));
  70. cpj = (mips_hpt_frequency + HZ / 2) / HZ;
  71. write_c0_count(0);
  72. timer_ack();
  73. /* Setup Timer 2 */
  74. write_c0_count2(0);
  75. write_c0_compare2(0xffffffff);
  76. clocksource_mips.read = hpt_read;
  77. mips_timer_ack = timer_ack;
  78. }
  79. static irqreturn_t monotonic_interrupt(int irq, void *dev_id)
  80. {
  81. /* Timer 2 clear interrupt */
  82. write_c0_compare2(-1);
  83. return IRQ_HANDLED;
  84. }
  85. static struct irqaction monotonic_irqaction = {
  86. .handler = monotonic_interrupt,
  87. .flags = IRQF_DISABLED,
  88. .name = "Monotonic timer",
  89. };
  90. void __init plat_timer_setup(struct irqaction *irq)
  91. {
  92. int configPR;
  93. setup_irq(PNX8550_INT_TIMER1, irq);
  94. setup_irq(PNX8550_INT_TIMER2, &monotonic_irqaction);
  95. /* Timer 1 start */
  96. configPR = read_c0_config7();
  97. configPR &= ~0x00000008;
  98. write_c0_config7(configPR);
  99. /* Timer 2 start */
  100. configPR = read_c0_config7();
  101. configPR &= ~0x00000010;
  102. write_c0_config7(configPR);
  103. /* Timer 3 stop */
  104. configPR = read_c0_config7();
  105. configPR |= 0x00000020;
  106. write_c0_config7(configPR);
  107. }