sim_time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <linux/types.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel_stat.h>
  4. #include <linux/sched.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/mc146818rtc.h>
  8. #include <linux/timex.h>
  9. #include <asm/mipsregs.h>
  10. #include <asm/ptrace.h>
  11. #include <asm/hardirq.h>
  12. #include <asm/div64.h>
  13. #include <asm/cpu.h>
  14. #include <asm/time.h>
  15. #include <asm/irq.h>
  16. #include <asm/mc146818-time.h>
  17. #include <asm/msc01_ic.h>
  18. #include <asm/smp.h>
  19. #include <asm/mips-boards/generic.h>
  20. #include <asm/mips-boards/prom.h>
  21. #include <asm/mips-boards/simint.h>
  22. unsigned long cpu_khz;
  23. irqreturn_t sim_timer_interrupt(int irq, void *dev_id)
  24. {
  25. #ifdef CONFIG_SMP
  26. int cpu = smp_processor_id();
  27. /*
  28. * CPU 0 handles the global timer interrupt job
  29. * resets count/compare registers to trigger next timer int.
  30. */
  31. #ifndef CONFIG_MIPS_MT_SMTC
  32. if (cpu == 0) {
  33. timer_interrupt(irq, dev_id);
  34. }
  35. else {
  36. /* Everyone else needs to reset the timer int here as
  37. ll_local_timer_interrupt doesn't */
  38. /*
  39. * FIXME: need to cope with counter underflow.
  40. * More support needs to be added to kernel/time for
  41. * counter/timer interrupts on multiple CPU's
  42. */
  43. write_c0_compare (read_c0_count() + ( mips_hpt_frequency/HZ));
  44. }
  45. #else /* SMTC */
  46. /*
  47. * In SMTC system, one Count/Compare set exists per VPE.
  48. * Which TC within a VPE gets the interrupt is essentially
  49. * random - we only know that it shouldn't be one with
  50. * IXMT set. Whichever TC gets the interrupt needs to
  51. * send special interprocessor interrupts to the other
  52. * TCs to make sure that they schedule, etc.
  53. *
  54. * That code is specific to the SMTC kernel, not to
  55. * the simulation platform, so it's invoked from
  56. * the general MIPS timer_interrupt routine.
  57. *
  58. * We have a problem in that the interrupt vector code
  59. * had to turn off the timer IM bit to avoid redundant
  60. * entries, but we may never get to mips_cpu_irq_end
  61. * to turn it back on again if the scheduler gets
  62. * involved. So we clear the pending timer here,
  63. * and re-enable the mask...
  64. */
  65. int vpflags = dvpe();
  66. write_c0_compare (read_c0_count() - 1);
  67. clear_c0_cause(0x100 << MIPSCPU_INT_CPUCTR);
  68. set_c0_status(0x100 << MIPSCPU_INT_CPUCTR);
  69. irq_enable_hazard();
  70. evpe(vpflags);
  71. if(cpu_data[cpu].vpe_id == 0) timer_interrupt(irq, dev_id);
  72. else write_c0_compare (read_c0_count() + ( mips_hpt_frequency/HZ));
  73. smtc_timer_broadcast(cpu_data[cpu].vpe_id);
  74. #endif /* CONFIG_MIPS_MT_SMTC */
  75. /*
  76. * every CPU should do profiling and process accounting
  77. */
  78. local_timer_interrupt (irq, dev_id);
  79. return IRQ_HANDLED;
  80. #else
  81. return timer_interrupt (irq, dev_id);
  82. #endif
  83. }
  84. /*
  85. * Estimate CPU frequency. Sets mips_hpt_frequency as a side-effect
  86. */
  87. static unsigned int __init estimate_cpu_frequency(void)
  88. {
  89. unsigned int prid = read_c0_prid() & 0xffff00;
  90. unsigned int count;
  91. #if 1
  92. /*
  93. * hardwire the board frequency to 12MHz.
  94. */
  95. if ((prid == (PRID_COMP_MIPS | PRID_IMP_20KC)) ||
  96. (prid == (PRID_COMP_MIPS | PRID_IMP_25KF)))
  97. count = 12000000;
  98. else
  99. count = 6000000;
  100. #else
  101. unsigned int flags;
  102. local_irq_save(flags);
  103. /* Start counter exactly on falling edge of update flag */
  104. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  105. while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
  106. /* Start r4k counter. */
  107. write_c0_count(0);
  108. /* Read counter exactly on falling edge of update flag */
  109. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  110. while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
  111. count = read_c0_count();
  112. /* restore interrupts */
  113. local_irq_restore(flags);
  114. #endif
  115. mips_hpt_frequency = count;
  116. if ((prid != (PRID_COMP_MIPS | PRID_IMP_20KC)) &&
  117. (prid != (PRID_COMP_MIPS | PRID_IMP_25KF)))
  118. count *= 2;
  119. count += 5000; /* round */
  120. count -= count%10000;
  121. return count;
  122. }
  123. void __init sim_time_init(void)
  124. {
  125. unsigned int est_freq, flags;
  126. local_irq_save(flags);
  127. /* Set Data mode - binary. */
  128. CMOS_WRITE(CMOS_READ(RTC_CONTROL) | RTC_DM_BINARY, RTC_CONTROL);
  129. est_freq = estimate_cpu_frequency ();
  130. printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
  131. (est_freq%1000000)*100/1000000);
  132. cpu_khz = est_freq / 1000;
  133. local_irq_restore(flags);
  134. }
  135. static int mips_cpu_timer_irq;
  136. static void mips_timer_dispatch(void)
  137. {
  138. do_IRQ(mips_cpu_timer_irq);
  139. }
  140. void __init plat_timer_setup(struct irqaction *irq)
  141. {
  142. if (cpu_has_veic) {
  143. set_vi_handler(MSC01E_INT_CPUCTR, mips_timer_dispatch);
  144. mips_cpu_timer_irq = MSC01E_INT_BASE + MSC01E_INT_CPUCTR;
  145. }
  146. else {
  147. if (cpu_has_vint)
  148. set_vi_handler(MIPSCPU_INT_CPUCTR, mips_timer_dispatch);
  149. mips_cpu_timer_irq = MIPSCPU_INT_BASE + MIPSCPU_INT_CPUCTR;
  150. }
  151. /* we are using the cpu counter for timer interrupts */
  152. irq->handler = sim_timer_interrupt;
  153. setup_irq(mips_cpu_timer_irq, irq);
  154. #ifdef CONFIG_SMP
  155. /* irq_desc(riptor) is a global resource, when the interrupt overlaps
  156. on seperate cpu's the first one tries to handle the second interrupt.
  157. The effect is that the int remains disabled on the second cpu.
  158. Mark the interrupt with IRQ_PER_CPU to avoid any confusion */
  159. irq_desc[mips_cpu_timer_irq].flags |= IRQ_PER_CPU;
  160. set_irq_handler(mips_cpu_timer_irq, handle_percpu_irq);
  161. #endif
  162. }