sim_time.c 5.3 KB

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