sim_time.c 5.4 KB

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