time.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. static inline void ack_r4ktimer(unsigned long newval)
  61. {
  62. write_c0_compare(newval);
  63. }
  64. /*
  65. * There are a lot of conceptually broken versions of the MIPS timer interrupt
  66. * handler floating around. This one is rather different, but the algorithm
  67. * is provably more robust.
  68. */
  69. unsigned long wtimer;
  70. void mips_timer_interrupt(void)
  71. {
  72. int irq = 63;
  73. irq_enter();
  74. kstat_this_cpu.irqs[irq]++;
  75. if (r4k_offset == 0)
  76. goto null;
  77. do {
  78. kstat_this_cpu.irqs[irq]++;
  79. do_timer(1);
  80. #ifndef CONFIG_SMP
  81. update_process_times(user_mode(get_irq_regs()));
  82. #endif
  83. r4k_cur += r4k_offset;
  84. ack_r4ktimer(r4k_cur);
  85. } while (((unsigned long)read_c0_count()
  86. - r4k_cur) < 0x7fffffff);
  87. irq_exit();
  88. return;
  89. null:
  90. ack_r4ktimer(0);
  91. irq_exit();
  92. }
  93. #ifdef CONFIG_PM
  94. irqreturn_t counter0_irq(int irq, void *dev_id)
  95. {
  96. unsigned long pc0;
  97. int time_elapsed;
  98. static int jiffie_drift = 0;
  99. if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
  100. /* should never happen! */
  101. printk(KERN_WARNING "counter 0 w status error\n");
  102. return IRQ_NONE;
  103. }
  104. pc0 = au_readl(SYS_TOYREAD);
  105. if (pc0 < last_match20) {
  106. /* counter overflowed */
  107. time_elapsed = (0xffffffff - last_match20) + pc0;
  108. }
  109. else {
  110. time_elapsed = pc0 - last_match20;
  111. }
  112. while (time_elapsed > 0) {
  113. do_timer(1);
  114. #ifndef CONFIG_SMP
  115. update_process_times(user_mode(get_irq_regs()));
  116. #endif
  117. time_elapsed -= MATCH20_INC;
  118. last_match20 += MATCH20_INC;
  119. jiffie_drift++;
  120. }
  121. last_pc0 = pc0;
  122. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  123. au_sync();
  124. /* our counter ticks at 10.009765625 ms/tick, we we're running
  125. * almost 10uS too slow per tick.
  126. */
  127. if (jiffie_drift >= 999) {
  128. jiffie_drift -= 999;
  129. do_timer(1); /* increment jiffies by one */
  130. #ifndef CONFIG_SMP
  131. update_process_times(user_mode(get_irq_regs()));
  132. #endif
  133. }
  134. return IRQ_HANDLED;
  135. }
  136. /* When we wakeup from sleep, we have to "catch up" on all of the
  137. * timer ticks we have missed.
  138. */
  139. void
  140. wakeup_counter0_adjust(void)
  141. {
  142. unsigned long pc0;
  143. int time_elapsed;
  144. pc0 = au_readl(SYS_TOYREAD);
  145. if (pc0 < last_match20) {
  146. /* counter overflowed */
  147. time_elapsed = (0xffffffff - last_match20) + pc0;
  148. }
  149. else {
  150. time_elapsed = pc0 - last_match20;
  151. }
  152. while (time_elapsed > 0) {
  153. time_elapsed -= MATCH20_INC;
  154. last_match20 += MATCH20_INC;
  155. }
  156. last_pc0 = pc0;
  157. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  158. au_sync();
  159. }
  160. /* This is just for debugging to set the timer for a sleep delay.
  161. */
  162. void
  163. wakeup_counter0_set(int ticks)
  164. {
  165. unsigned long pc0;
  166. pc0 = au_readl(SYS_TOYREAD);
  167. last_pc0 = pc0;
  168. au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
  169. au_sync();
  170. }
  171. #endif
  172. /* I haven't found anyone that doesn't use a 12 MHz source clock,
  173. * but just in case.....
  174. */
  175. #ifdef CONFIG_AU1000_SRC_CLK
  176. #define AU1000_SRC_CLK CONFIG_AU1000_SRC_CLK
  177. #else
  178. #define AU1000_SRC_CLK 12000000
  179. #endif
  180. /*
  181. * We read the real processor speed from the PLL. This is important
  182. * because it is more accurate than computing it from the 32KHz
  183. * counter, if it exists. If we don't have an accurate processor
  184. * speed, all of the peripherals that derive their clocks based on
  185. * this advertised speed will introduce error and sometimes not work
  186. * properly. This function is futher convoluted to still allow configurations
  187. * to do that in case they have really, really old silicon with a
  188. * write-only PLL register, that we need the 32KHz when power management
  189. * "wait" is enabled, and we need to detect if the 32KHz isn't present
  190. * but requested......got it? :-) -- Dan
  191. */
  192. unsigned long cal_r4koff(void)
  193. {
  194. unsigned long cpu_speed;
  195. unsigned long flags;
  196. unsigned long counter;
  197. spin_lock_irqsave(&time_lock, flags);
  198. /* Power management cares if we don't have a 32KHz counter.
  199. */
  200. no_au1xxx_32khz = 0;
  201. counter = au_readl(SYS_COUNTER_CNTRL);
  202. if (counter & SYS_CNTRL_E0) {
  203. int trim_divide = 16;
  204. au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
  205. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
  206. /* RTC now ticks at 32.768/16 kHz */
  207. au_writel(trim_divide-1, SYS_RTCTRIM);
  208. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
  209. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
  210. au_writel (0, SYS_TOYWRITE);
  211. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
  212. #if defined(CONFIG_AU1000_USE32K)
  213. {
  214. unsigned long start, end, count;
  215. start = au_readl(SYS_RTCREAD);
  216. start += 2;
  217. /* wait for the beginning of a new tick
  218. */
  219. while (au_readl(SYS_RTCREAD) < start);
  220. /* Start r4k counter.
  221. */
  222. write_c0_count(0);
  223. /* Wait 0.5 seconds.
  224. */
  225. end = start + (32768 / trim_divide)/2;
  226. while (end > au_readl(SYS_RTCREAD));
  227. count = read_c0_count();
  228. cpu_speed = count * 2;
  229. }
  230. #else
  231. cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) *
  232. AU1000_SRC_CLK;
  233. #endif
  234. }
  235. else {
  236. /* The 32KHz oscillator isn't running, so assume there
  237. * isn't one and grab the processor speed from the PLL.
  238. * NOTE: some old silicon doesn't allow reading the PLL.
  239. */
  240. cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
  241. no_au1xxx_32khz = 1;
  242. }
  243. mips_hpt_frequency = cpu_speed;
  244. // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16)
  245. set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
  246. spin_unlock_irqrestore(&time_lock, flags);
  247. return (cpu_speed / HZ);
  248. }
  249. void __init plat_timer_setup(struct irqaction *irq)
  250. {
  251. unsigned int est_freq;
  252. printk("calculating r4koff... ");
  253. r4k_offset = cal_r4koff();
  254. printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset);
  255. //est_freq = 2*r4k_offset*HZ;
  256. est_freq = r4k_offset*HZ;
  257. est_freq += 5000; /* round */
  258. est_freq -= est_freq%10000;
  259. printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
  260. (est_freq%1000000)*100/1000000);
  261. set_au1x00_speed(est_freq);
  262. set_au1x00_lcd_clock(); // program the LCD clock
  263. r4k_cur = (read_c0_count() + r4k_offset);
  264. write_c0_compare(r4k_cur);
  265. #ifdef CONFIG_PM
  266. /*
  267. * setup counter 0, since it keeps ticking after a
  268. * 'wait' instruction has been executed. The CP0 timer and
  269. * counter 1 do NOT continue running after 'wait'
  270. *
  271. * It's too early to call request_irq() here, so we handle
  272. * counter 0 interrupt as a special irq and it doesn't show
  273. * up under /proc/interrupts.
  274. *
  275. * Check to ensure we really have a 32KHz oscillator before
  276. * we do this.
  277. */
  278. if (no_au1xxx_32khz) {
  279. unsigned int c0_status;
  280. printk("WARNING: no 32KHz clock found.\n");
  281. /* Ensure we get CPO_COUNTER interrupts.
  282. */
  283. c0_status = read_c0_status();
  284. c0_status |= IE_IRQ5;
  285. write_c0_status(c0_status);
  286. }
  287. else {
  288. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
  289. au_writel(0, SYS_TOYWRITE);
  290. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
  291. au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK);
  292. au_writel(~0, SYS_WAKESRC);
  293. au_sync();
  294. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
  295. /* setup match20 to interrupt once every HZ */
  296. last_pc0 = last_match20 = au_readl(SYS_TOYREAD);
  297. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  298. au_sync();
  299. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
  300. startup_match20_interrupt(counter0_irq);
  301. /* We can use the real 'wait' instruction.
  302. */
  303. allow_au1k_wait = 1;
  304. }
  305. #endif
  306. }
  307. void __init au1xxx_time_init(void)
  308. {
  309. }