time.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Carsten Langgaard, carstenl@mips.com
  3. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  4. *
  5. * This program is free software; you can distribute it and/or modify it
  6. * under the terms of the GNU General Public License (Version 2) as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  17. *
  18. * Setting up the clock on the MIPS boards.
  19. */
  20. #include <linux/types.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel_stat.h>
  23. #include <linux/sched.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/time.h>
  27. #include <linux/timex.h>
  28. #include <linux/mc146818rtc.h>
  29. #include <asm/mipsregs.h>
  30. #include <asm/mipsmtregs.h>
  31. #include <asm/hardirq.h>
  32. #include <asm/irq.h>
  33. #include <asm/div64.h>
  34. #include <asm/cpu.h>
  35. #include <asm/time.h>
  36. #include <asm/mc146818-time.h>
  37. #include <asm/msc01_ic.h>
  38. #include <asm/mips-boards/generic.h>
  39. #include <asm/mips-boards/prom.h>
  40. #ifdef CONFIG_MIPS_ATLAS
  41. #include <asm/mips-boards/atlasint.h>
  42. #endif
  43. #ifdef CONFIG_MIPS_MALTA
  44. #include <asm/mips-boards/maltaint.h>
  45. #endif
  46. #ifdef CONFIG_MIPS_SEAD
  47. #include <asm/mips-boards/seadint.h>
  48. #endif
  49. unsigned long cpu_khz;
  50. static int mips_cpu_timer_irq;
  51. extern int cp0_perfcount_irq;
  52. extern void smtc_timer_broadcast(void);
  53. static void mips_timer_dispatch(void)
  54. {
  55. do_IRQ(mips_cpu_timer_irq);
  56. }
  57. static void mips_perf_dispatch(void)
  58. {
  59. do_IRQ(cp0_perfcount_irq);
  60. }
  61. /*
  62. * Redeclare until I get around mopping the timer code insanity on MIPS.
  63. */
  64. extern int null_perf_irq(void);
  65. extern int (*perf_irq)(void);
  66. /*
  67. * Possibly handle a performance counter interrupt.
  68. * Return true if the timer interrupt should not be checked
  69. */
  70. static inline int handle_perf_irq (int r2)
  71. {
  72. /*
  73. * The performance counter overflow interrupt may be shared with the
  74. * timer interrupt (cp0_perfcount_irq < 0). If it is and a
  75. * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
  76. * and we can't reliably determine if a counter interrupt has also
  77. * happened (!r2) then don't check for a timer interrupt.
  78. */
  79. return (cp0_perfcount_irq < 0) &&
  80. perf_irq() == IRQ_HANDLED &&
  81. !r2;
  82. }
  83. irqreturn_t mips_timer_interrupt(int irq, void *dev_id)
  84. {
  85. int cpu = smp_processor_id();
  86. #ifdef CONFIG_MIPS_MT_SMTC
  87. /*
  88. * In an SMTC system, one Count/Compare set exists per VPE.
  89. * Which TC within a VPE gets the interrupt is essentially
  90. * random - we only know that it shouldn't be one with
  91. * IXMT set. Whichever TC gets the interrupt needs to
  92. * send special interprocessor interrupts to the other
  93. * TCs to make sure that they schedule, etc.
  94. *
  95. * That code is specific to the SMTC kernel, not to
  96. * the a particular platform, so it's invoked from
  97. * the general MIPS timer_interrupt routine.
  98. */
  99. /*
  100. * We could be here due to timer interrupt,
  101. * perf counter overflow, or both.
  102. */
  103. (void) handle_perf_irq(1);
  104. if (read_c0_cause() & (1 << 30)) {
  105. /*
  106. * There are things we only want to do once per tick
  107. * in an "MP" system. One TC of each VPE will take
  108. * the actual timer interrupt. The others will get
  109. * timer broadcast IPIs. We use whoever it is that takes
  110. * the tick on VPE 0 to run the full timer_interrupt().
  111. */
  112. if (cpu_data[cpu].vpe_id == 0) {
  113. timer_interrupt(irq, NULL);
  114. } else {
  115. write_c0_compare(read_c0_count() +
  116. (mips_hpt_frequency/HZ));
  117. local_timer_interrupt(irq, dev_id);
  118. }
  119. smtc_timer_broadcast();
  120. }
  121. #else /* CONFIG_MIPS_MT_SMTC */
  122. int r2 = cpu_has_mips_r2;
  123. if (handle_perf_irq(r2))
  124. goto out;
  125. if (r2 && ((read_c0_cause() & (1 << 30)) == 0))
  126. goto out;
  127. if (cpu == 0) {
  128. /*
  129. * CPU 0 handles the global timer interrupt job and process
  130. * accounting resets count/compare registers to trigger next
  131. * timer int.
  132. */
  133. timer_interrupt(irq, NULL);
  134. } else {
  135. /* Everyone else needs to reset the timer int here as
  136. ll_local_timer_interrupt doesn't */
  137. /*
  138. * FIXME: need to cope with counter underflow.
  139. * More support needs to be added to kernel/time for
  140. * counter/timer interrupts on multiple CPU's
  141. */
  142. write_c0_compare(read_c0_count() + (mips_hpt_frequency/HZ));
  143. /*
  144. * Other CPUs should do profiling and process accounting
  145. */
  146. local_timer_interrupt(irq, dev_id);
  147. }
  148. out:
  149. #endif /* CONFIG_MIPS_MT_SMTC */
  150. return IRQ_HANDLED;
  151. }
  152. /*
  153. * Estimate CPU frequency. Sets mips_hpt_frequency as a side-effect
  154. */
  155. static unsigned int __init estimate_cpu_frequency(void)
  156. {
  157. unsigned int prid = read_c0_prid() & 0xffff00;
  158. unsigned int count;
  159. #if defined(CONFIG_MIPS_SEAD) || defined(CONFIG_MIPS_SIM)
  160. /*
  161. * The SEAD board doesn't have a real time clock, so we can't
  162. * really calculate the timer frequency
  163. * For now we hardwire the SEAD board frequency to 12MHz.
  164. */
  165. if ((prid == (PRID_COMP_MIPS | PRID_IMP_20KC)) ||
  166. (prid == (PRID_COMP_MIPS | PRID_IMP_25KF)))
  167. count = 12000000;
  168. else
  169. count = 6000000;
  170. #endif
  171. #if defined(CONFIG_MIPS_ATLAS) || defined(CONFIG_MIPS_MALTA)
  172. unsigned long flags;
  173. unsigned int start;
  174. local_irq_save(flags);
  175. /* Start counter exactly on falling edge of update flag */
  176. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  177. while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
  178. /* Start r4k counter. */
  179. start = read_c0_count();
  180. /* Read counter exactly on falling edge of update flag */
  181. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  182. while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
  183. count = read_c0_count() - start;
  184. /* restore interrupts */
  185. local_irq_restore(flags);
  186. #endif
  187. mips_hpt_frequency = count;
  188. if ((prid != (PRID_COMP_MIPS | PRID_IMP_20KC)) &&
  189. (prid != (PRID_COMP_MIPS | PRID_IMP_25KF)))
  190. count *= 2;
  191. count += 5000; /* round */
  192. count -= count%10000;
  193. return count;
  194. }
  195. unsigned long __init mips_rtc_get_time(void)
  196. {
  197. return mc146818_get_cmos_time();
  198. }
  199. void __init mips_time_init(void)
  200. {
  201. unsigned int est_freq;
  202. /* Set Data mode - binary. */
  203. CMOS_WRITE(CMOS_READ(RTC_CONTROL) | RTC_DM_BINARY, RTC_CONTROL);
  204. est_freq = estimate_cpu_frequency ();
  205. printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
  206. (est_freq%1000000)*100/1000000);
  207. cpu_khz = est_freq / 1000;
  208. mips_scroll_message();
  209. }
  210. irqreturn_t mips_perf_interrupt(int irq, void *dev_id)
  211. {
  212. return perf_irq();
  213. }
  214. static struct irqaction perf_irqaction = {
  215. .handler = mips_perf_interrupt,
  216. .flags = IRQF_DISABLED | IRQF_PERCPU,
  217. .name = "performance",
  218. };
  219. void __init plat_perf_setup(struct irqaction *irq)
  220. {
  221. cp0_perfcount_irq = -1;
  222. #ifdef MSC01E_INT_BASE
  223. if (cpu_has_veic) {
  224. set_vi_handler (MSC01E_INT_PERFCTR, mips_perf_dispatch);
  225. cp0_perfcount_irq = MSC01E_INT_BASE + MSC01E_INT_PERFCTR;
  226. } else
  227. #endif
  228. if (cp0_perfcount_irq >= 0) {
  229. if (cpu_has_vint)
  230. set_vi_handler(cp0_perfcount_irq, mips_perf_dispatch);
  231. #ifdef CONFIG_MIPS_MT_SMTC
  232. setup_irq_smtc(cp0_perfcount_irq, irq,
  233. 0x100 << cp0_perfcount_irq);
  234. #else
  235. setup_irq(cp0_perfcount_irq, irq);
  236. #endif /* CONFIG_MIPS_MT_SMTC */
  237. #ifdef CONFIG_SMP
  238. set_irq_handler(cp0_perfcount_irq, handle_percpu_irq);
  239. #endif
  240. }
  241. }
  242. void __init plat_timer_setup(struct irqaction *irq)
  243. {
  244. #ifdef MSC01E_INT_BASE
  245. if (cpu_has_veic) {
  246. set_vi_handler (MSC01E_INT_CPUCTR, mips_timer_dispatch);
  247. mips_cpu_timer_irq = MSC01E_INT_BASE + MSC01E_INT_CPUCTR;
  248. }
  249. else
  250. #endif
  251. {
  252. if (cpu_has_vint)
  253. set_vi_handler(cp0_compare_irq, mips_timer_dispatch);
  254. mips_cpu_timer_irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  255. }
  256. /* we are using the cpu counter for timer interrupts */
  257. irq->handler = mips_timer_interrupt; /* we use our own handler */
  258. #ifdef CONFIG_MIPS_MT_SMTC
  259. setup_irq_smtc(mips_cpu_timer_irq, irq, 0x100 << cp0_compare_irq);
  260. #else
  261. setup_irq(mips_cpu_timer_irq, irq);
  262. #endif /* CONFIG_MIPS_MT_SMTC */
  263. #ifdef CONFIG_SMP
  264. set_irq_handler(mips_cpu_timer_irq, handle_percpu_irq);
  265. #endif
  266. plat_perf_setup(&perf_irqaction);
  267. }