time.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. *
  3. * Copyright (C) 2001 MontaVista Software, ppopov@mvista.com
  4. * Copied and modified Carsten Langgaard's time.c
  5. *
  6. * Carsten Langgaard, carstenl@mips.com
  7. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  8. *
  9. * ########################################################################
  10. *
  11. * This program is free software; you can distribute it and/or modify it
  12. * under the terms of the GNU General Public License (Version 2) as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. * for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  23. *
  24. * ########################################################################
  25. *
  26. * Setting up the clock on the MIPS boards.
  27. *
  28. * Update. Always configure the kernel with CONFIG_NEW_TIME_C. This
  29. * will use the user interface gettimeofday() functions from the
  30. * arch/mips/kernel/time.c, and we provide the clock interrupt processing
  31. * and the timer offset compute functions. If CONFIG_PM is selected,
  32. * we also ensure the 32KHz timer is available. -- Dan
  33. */
  34. #include <linux/types.h>
  35. #include <linux/init.h>
  36. #include <linux/kernel_stat.h>
  37. #include <linux/sched.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/hardirq.h>
  40. #include <asm/compiler.h>
  41. #include <asm/mipsregs.h>
  42. #include <asm/time.h>
  43. #include <asm/div64.h>
  44. #include <asm/mach-au1x00/au1000.h>
  45. #include <linux/mc146818rtc.h>
  46. #include <linux/timex.h>
  47. static unsigned long r4k_offset; /* Amount to increment compare reg each time */
  48. static unsigned long r4k_cur; /* What counter should be at next timer irq */
  49. int no_au1xxx_32khz;
  50. extern int allow_au1k_wait; /* default off for CP0 Counter */
  51. #ifdef CONFIG_PM
  52. #if HZ < 100 || HZ > 1000
  53. #error "unsupported HZ value! Must be in [100,1000]"
  54. #endif
  55. #define MATCH20_INC (328*100/HZ) /* magic number 328 is for HZ=100... */
  56. extern void startup_match20_interrupt(irq_handler_t handler);
  57. static unsigned long last_pc0, last_match20;
  58. #endif
  59. static DEFINE_SPINLOCK(time_lock);
  60. unsigned long wtimer;
  61. #ifdef CONFIG_PM
  62. irqreturn_t counter0_irq(int irq, void *dev_id)
  63. {
  64. unsigned long pc0;
  65. int time_elapsed;
  66. static int jiffie_drift = 0;
  67. if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
  68. /* should never happen! */
  69. printk(KERN_WARNING "counter 0 w status error\n");
  70. return IRQ_NONE;
  71. }
  72. pc0 = au_readl(SYS_TOYREAD);
  73. if (pc0 < last_match20) {
  74. /* counter overflowed */
  75. time_elapsed = (0xffffffff - last_match20) + pc0;
  76. }
  77. else {
  78. time_elapsed = pc0 - last_match20;
  79. }
  80. while (time_elapsed > 0) {
  81. do_timer(1);
  82. #ifndef CONFIG_SMP
  83. update_process_times(user_mode(get_irq_regs()));
  84. #endif
  85. time_elapsed -= MATCH20_INC;
  86. last_match20 += MATCH20_INC;
  87. jiffie_drift++;
  88. }
  89. last_pc0 = pc0;
  90. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  91. au_sync();
  92. /* our counter ticks at 10.009765625 ms/tick, we we're running
  93. * almost 10uS too slow per tick.
  94. */
  95. if (jiffie_drift >= 999) {
  96. jiffie_drift -= 999;
  97. do_timer(1); /* increment jiffies by one */
  98. #ifndef CONFIG_SMP
  99. update_process_times(user_mode(get_irq_regs()));
  100. #endif
  101. }
  102. return IRQ_HANDLED;
  103. }
  104. /* When we wakeup from sleep, we have to "catch up" on all of the
  105. * timer ticks we have missed.
  106. */
  107. void
  108. wakeup_counter0_adjust(void)
  109. {
  110. unsigned long pc0;
  111. int time_elapsed;
  112. pc0 = au_readl(SYS_TOYREAD);
  113. if (pc0 < last_match20) {
  114. /* counter overflowed */
  115. time_elapsed = (0xffffffff - last_match20) + pc0;
  116. }
  117. else {
  118. time_elapsed = pc0 - last_match20;
  119. }
  120. while (time_elapsed > 0) {
  121. time_elapsed -= MATCH20_INC;
  122. last_match20 += MATCH20_INC;
  123. }
  124. last_pc0 = pc0;
  125. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  126. au_sync();
  127. }
  128. /* This is just for debugging to set the timer for a sleep delay.
  129. */
  130. void
  131. wakeup_counter0_set(int ticks)
  132. {
  133. unsigned long pc0;
  134. pc0 = au_readl(SYS_TOYREAD);
  135. last_pc0 = pc0;
  136. au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
  137. au_sync();
  138. }
  139. #endif
  140. /* I haven't found anyone that doesn't use a 12 MHz source clock,
  141. * but just in case.....
  142. */
  143. #define AU1000_SRC_CLK 12000000
  144. /*
  145. * We read the real processor speed from the PLL. This is important
  146. * because it is more accurate than computing it from the 32KHz
  147. * counter, if it exists. If we don't have an accurate processor
  148. * speed, all of the peripherals that derive their clocks based on
  149. * this advertised speed will introduce error and sometimes not work
  150. * properly. This function is futher convoluted to still allow configurations
  151. * to do that in case they have really, really old silicon with a
  152. * write-only PLL register, that we need the 32KHz when power management
  153. * "wait" is enabled, and we need to detect if the 32KHz isn't present
  154. * but requested......got it? :-) -- Dan
  155. */
  156. unsigned long cal_r4koff(void)
  157. {
  158. unsigned long cpu_speed;
  159. unsigned long flags;
  160. unsigned long counter;
  161. spin_lock_irqsave(&time_lock, flags);
  162. /* Power management cares if we don't have a 32KHz counter.
  163. */
  164. no_au1xxx_32khz = 0;
  165. counter = au_readl(SYS_COUNTER_CNTRL);
  166. if (counter & SYS_CNTRL_E0) {
  167. int trim_divide = 16;
  168. au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
  169. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
  170. /* RTC now ticks at 32.768/16 kHz */
  171. au_writel(trim_divide-1, SYS_RTCTRIM);
  172. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
  173. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
  174. au_writel(0, SYS_TOYWRITE);
  175. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
  176. cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) *
  177. AU1000_SRC_CLK;
  178. }
  179. else {
  180. /* The 32KHz oscillator isn't running, so assume there
  181. * isn't one and grab the processor speed from the PLL.
  182. * NOTE: some old silicon doesn't allow reading the PLL.
  183. */
  184. cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
  185. no_au1xxx_32khz = 1;
  186. }
  187. mips_hpt_frequency = cpu_speed;
  188. // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16)
  189. set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
  190. spin_unlock_irqrestore(&time_lock, flags);
  191. return (cpu_speed / HZ);
  192. }
  193. void __init plat_timer_setup(struct irqaction *irq)
  194. {
  195. unsigned int est_freq;
  196. printk("calculating r4koff... ");
  197. r4k_offset = cal_r4koff();
  198. printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset);
  199. //est_freq = 2*r4k_offset*HZ;
  200. est_freq = r4k_offset*HZ;
  201. est_freq += 5000; /* round */
  202. est_freq -= est_freq%10000;
  203. printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
  204. (est_freq%1000000)*100/1000000);
  205. set_au1x00_speed(est_freq);
  206. set_au1x00_lcd_clock(); // program the LCD clock
  207. r4k_cur = (read_c0_count() + r4k_offset);
  208. write_c0_compare(r4k_cur);
  209. #ifdef CONFIG_PM
  210. /*
  211. * setup counter 0, since it keeps ticking after a
  212. * 'wait' instruction has been executed. The CP0 timer and
  213. * counter 1 do NOT continue running after 'wait'
  214. *
  215. * It's too early to call request_irq() here, so we handle
  216. * counter 0 interrupt as a special irq and it doesn't show
  217. * up under /proc/interrupts.
  218. *
  219. * Check to ensure we really have a 32KHz oscillator before
  220. * we do this.
  221. */
  222. if (no_au1xxx_32khz) {
  223. unsigned int c0_status;
  224. printk("WARNING: no 32KHz clock found.\n");
  225. /* Ensure we get CPO_COUNTER interrupts.
  226. */
  227. c0_status = read_c0_status();
  228. c0_status |= IE_IRQ5;
  229. write_c0_status(c0_status);
  230. }
  231. else {
  232. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
  233. au_writel(0, SYS_TOYWRITE);
  234. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
  235. au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK);
  236. au_writel(~0, SYS_WAKESRC);
  237. au_sync();
  238. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
  239. /* setup match20 to interrupt once every HZ */
  240. last_pc0 = last_match20 = au_readl(SYS_TOYREAD);
  241. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  242. au_sync();
  243. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
  244. startup_match20_interrupt(counter0_irq);
  245. /* We can use the real 'wait' instruction.
  246. */
  247. allow_au1k_wait = 1;
  248. }
  249. #endif
  250. }