time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/config.h>
  36. #include <linux/init.h>
  37. #include <linux/kernel_stat.h>
  38. #include <linux/sched.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/hardirq.h>
  41. #include <asm/compiler.h>
  42. #include <asm/mipsregs.h>
  43. #include <asm/ptrace.h>
  44. #include <asm/time.h>
  45. #include <asm/div64.h>
  46. #include <asm/mach-au1x00/au1000.h>
  47. #include <linux/mc146818rtc.h>
  48. #include <linux/timex.h>
  49. extern void do_softirq(void);
  50. extern volatile unsigned long wall_jiffies;
  51. unsigned long missed_heart_beats = 0;
  52. static unsigned long r4k_offset; /* Amount to increment compare reg each time */
  53. static unsigned long r4k_cur; /* What counter should be at next timer irq */
  54. int no_au1xxx_32khz;
  55. extern int allow_au1k_wait; /* default off for CP0 Counter */
  56. /* Cycle counter value at the previous timer interrupt.. */
  57. static unsigned int timerhi = 0, timerlo = 0;
  58. #ifdef CONFIG_PM
  59. #if HZ < 100 || HZ > 1000
  60. #error "unsupported HZ value! Must be in [100,1000]"
  61. #endif
  62. #define MATCH20_INC (328*100/HZ) /* magic number 328 is for HZ=100... */
  63. extern void startup_match20_interrupt(irqreturn_t (*handler)(int, void *, struct pt_regs *));
  64. static unsigned long last_pc0, last_match20;
  65. #endif
  66. static DEFINE_SPINLOCK(time_lock);
  67. static inline void ack_r4ktimer(unsigned long newval)
  68. {
  69. write_c0_compare(newval);
  70. }
  71. /*
  72. * There are a lot of conceptually broken versions of the MIPS timer interrupt
  73. * handler floating around. This one is rather different, but the algorithm
  74. * is provably more robust.
  75. */
  76. unsigned long wtimer;
  77. void mips_timer_interrupt(struct pt_regs *regs)
  78. {
  79. int irq = 63;
  80. unsigned long count;
  81. irq_enter();
  82. kstat_this_cpu.irqs[irq]++;
  83. if (r4k_offset == 0)
  84. goto null;
  85. do {
  86. count = read_c0_count();
  87. timerhi += (count < timerlo); /* Wrap around */
  88. timerlo = count;
  89. kstat_this_cpu.irqs[irq]++;
  90. do_timer(regs);
  91. #ifndef CONFIG_SMP
  92. update_process_times(user_mode(regs));
  93. #endif
  94. r4k_cur += r4k_offset;
  95. ack_r4ktimer(r4k_cur);
  96. } while (((unsigned long)read_c0_count()
  97. - r4k_cur) < 0x7fffffff);
  98. irq_exit();
  99. return;
  100. null:
  101. ack_r4ktimer(0);
  102. }
  103. #ifdef CONFIG_PM
  104. irqreturn_t counter0_irq(int irq, void *dev_id, struct pt_regs *regs)
  105. {
  106. unsigned long pc0;
  107. int time_elapsed;
  108. static int jiffie_drift = 0;
  109. if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
  110. /* should never happen! */
  111. printk(KERN_WARNING "counter 0 w status error\n");
  112. return IRQ_NONE;
  113. }
  114. pc0 = au_readl(SYS_TOYREAD);
  115. if (pc0 < last_match20) {
  116. /* counter overflowed */
  117. time_elapsed = (0xffffffff - last_match20) + pc0;
  118. }
  119. else {
  120. time_elapsed = pc0 - last_match20;
  121. }
  122. while (time_elapsed > 0) {
  123. do_timer(regs);
  124. #ifndef CONFIG_SMP
  125. update_process_times(user_mode(regs));
  126. #endif
  127. time_elapsed -= MATCH20_INC;
  128. last_match20 += MATCH20_INC;
  129. jiffie_drift++;
  130. }
  131. last_pc0 = pc0;
  132. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  133. au_sync();
  134. /* our counter ticks at 10.009765625 ms/tick, we we're running
  135. * almost 10uS too slow per tick.
  136. */
  137. if (jiffie_drift >= 999) {
  138. jiffie_drift -= 999;
  139. do_timer(regs); /* increment jiffies by one */
  140. #ifndef CONFIG_SMP
  141. update_process_times(user_mode(regs));
  142. #endif
  143. }
  144. return IRQ_HANDLED;
  145. }
  146. /* When we wakeup from sleep, we have to "catch up" on all of the
  147. * timer ticks we have missed.
  148. */
  149. void
  150. wakeup_counter0_adjust(void)
  151. {
  152. unsigned long pc0;
  153. int time_elapsed;
  154. pc0 = au_readl(SYS_TOYREAD);
  155. if (pc0 < last_match20) {
  156. /* counter overflowed */
  157. time_elapsed = (0xffffffff - last_match20) + pc0;
  158. }
  159. else {
  160. time_elapsed = pc0 - last_match20;
  161. }
  162. while (time_elapsed > 0) {
  163. time_elapsed -= MATCH20_INC;
  164. last_match20 += MATCH20_INC;
  165. }
  166. last_pc0 = pc0;
  167. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  168. au_sync();
  169. }
  170. /* This is just for debugging to set the timer for a sleep delay.
  171. */
  172. void
  173. wakeup_counter0_set(int ticks)
  174. {
  175. unsigned long pc0;
  176. pc0 = au_readl(SYS_TOYREAD);
  177. last_pc0 = pc0;
  178. au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
  179. au_sync();
  180. }
  181. #endif
  182. /* I haven't found anyone that doesn't use a 12 MHz source clock,
  183. * but just in case.....
  184. */
  185. #ifdef CONFIG_AU1000_SRC_CLK
  186. #define AU1000_SRC_CLK CONFIG_AU1000_SRC_CLK
  187. #else
  188. #define AU1000_SRC_CLK 12000000
  189. #endif
  190. /*
  191. * We read the real processor speed from the PLL. This is important
  192. * because it is more accurate than computing it from the 32KHz
  193. * counter, if it exists. If we don't have an accurate processor
  194. * speed, all of the peripherals that derive their clocks based on
  195. * this advertised speed will introduce error and sometimes not work
  196. * properly. This function is futher convoluted to still allow configurations
  197. * to do that in case they have really, really old silicon with a
  198. * write-only PLL register, that we need the 32KHz when power management
  199. * "wait" is enabled, and we need to detect if the 32KHz isn't present
  200. * but requested......got it? :-) -- Dan
  201. */
  202. unsigned long cal_r4koff(void)
  203. {
  204. unsigned long count;
  205. unsigned long cpu_speed;
  206. unsigned long flags;
  207. unsigned long counter;
  208. spin_lock_irqsave(&time_lock, flags);
  209. /* Power management cares if we don't have a 32KHz counter.
  210. */
  211. no_au1xxx_32khz = 0;
  212. counter = au_readl(SYS_COUNTER_CNTRL);
  213. if (counter & SYS_CNTRL_E0) {
  214. int trim_divide = 16;
  215. au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
  216. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
  217. /* RTC now ticks at 32.768/16 kHz */
  218. au_writel(trim_divide-1, SYS_RTCTRIM);
  219. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
  220. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
  221. au_writel (0, SYS_TOYWRITE);
  222. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
  223. #if defined(CONFIG_AU1000_USE32K)
  224. {
  225. unsigned long start, end;
  226. start = au_readl(SYS_RTCREAD);
  227. start += 2;
  228. /* wait for the beginning of a new tick
  229. */
  230. while (au_readl(SYS_RTCREAD) < start);
  231. /* Start r4k counter.
  232. */
  233. write_c0_count(0);
  234. /* Wait 0.5 seconds.
  235. */
  236. end = start + (32768 / trim_divide)/2;
  237. while (end > au_readl(SYS_RTCREAD));
  238. count = read_c0_count();
  239. cpu_speed = count * 2;
  240. }
  241. #else
  242. cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) *
  243. AU1000_SRC_CLK;
  244. count = cpu_speed / 2;
  245. #endif
  246. }
  247. else {
  248. /* The 32KHz oscillator isn't running, so assume there
  249. * isn't one and grab the processor speed from the PLL.
  250. * NOTE: some old silicon doesn't allow reading the PLL.
  251. */
  252. cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
  253. count = cpu_speed / 2;
  254. no_au1xxx_32khz = 1;
  255. }
  256. mips_hpt_frequency = count;
  257. // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16)
  258. set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
  259. spin_unlock_irqrestore(&time_lock, flags);
  260. return (cpu_speed / HZ);
  261. }
  262. /* This is for machines which generate the exact clock. */
  263. #define USECS_PER_JIFFY (1000000/HZ)
  264. #define USECS_PER_JIFFY_FRAC (0x100000000LL*1000000/HZ&0xffffffff)
  265. static unsigned long
  266. div64_32(unsigned long v1, unsigned long v2, unsigned long v3)
  267. {
  268. unsigned long r0;
  269. do_div64_32(r0, v1, v2, v3);
  270. return r0;
  271. }
  272. static unsigned long do_fast_cp0_gettimeoffset(void)
  273. {
  274. u32 count;
  275. unsigned long res, tmp;
  276. unsigned long r0;
  277. /* Last jiffy when do_fast_gettimeoffset() was called. */
  278. static unsigned long last_jiffies=0;
  279. unsigned long quotient;
  280. /*
  281. * Cached "1/(clocks per usec)*2^32" value.
  282. * It has to be recalculated once each jiffy.
  283. */
  284. static unsigned long cached_quotient=0;
  285. tmp = jiffies;
  286. quotient = cached_quotient;
  287. if (tmp && last_jiffies != tmp) {
  288. last_jiffies = tmp;
  289. if (last_jiffies != 0) {
  290. r0 = div64_32(timerhi, timerlo, tmp);
  291. quotient = div64_32(USECS_PER_JIFFY, USECS_PER_JIFFY_FRAC, r0);
  292. cached_quotient = quotient;
  293. }
  294. }
  295. /* Get last timer tick in absolute kernel time */
  296. count = read_c0_count();
  297. /* .. relative to previous jiffy (32 bits is enough) */
  298. count -= timerlo;
  299. __asm__("multu\t%1,%2\n\t"
  300. "mfhi\t%0"
  301. : "=r" (res)
  302. : "r" (count), "r" (quotient)
  303. : "hi", "lo", GCC_REG_ACCUM);
  304. /*
  305. * Due to possible jiffies inconsistencies, we need to check
  306. * the result so that we'll get a timer that is monotonic.
  307. */
  308. if (res >= USECS_PER_JIFFY)
  309. res = USECS_PER_JIFFY-1;
  310. return res;
  311. }
  312. #ifdef CONFIG_PM
  313. static unsigned long do_fast_pm_gettimeoffset(void)
  314. {
  315. unsigned long pc0;
  316. unsigned long offset;
  317. pc0 = au_readl(SYS_TOYREAD);
  318. au_sync();
  319. offset = pc0 - last_pc0;
  320. if (offset > 2*MATCH20_INC) {
  321. printk("huge offset %x, last_pc0 %x last_match20 %x pc0 %x\n",
  322. (unsigned)offset, (unsigned)last_pc0,
  323. (unsigned)last_match20, (unsigned)pc0);
  324. }
  325. offset = (unsigned long)((offset * 305) / 10);
  326. return offset;
  327. }
  328. #endif
  329. void au1xxx_timer_setup(struct irqaction *irq)
  330. {
  331. unsigned int est_freq;
  332. extern unsigned long (*do_gettimeoffset)(void);
  333. printk("calculating r4koff... ");
  334. r4k_offset = cal_r4koff();
  335. printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset);
  336. //est_freq = 2*r4k_offset*HZ;
  337. est_freq = r4k_offset*HZ;
  338. est_freq += 5000; /* round */
  339. est_freq -= est_freq%10000;
  340. printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
  341. (est_freq%1000000)*100/1000000);
  342. set_au1x00_speed(est_freq);
  343. set_au1x00_lcd_clock(); // program the LCD clock
  344. r4k_cur = (read_c0_count() + r4k_offset);
  345. write_c0_compare(r4k_cur);
  346. #ifdef CONFIG_PM
  347. /*
  348. * setup counter 0, since it keeps ticking after a
  349. * 'wait' instruction has been executed. The CP0 timer and
  350. * counter 1 do NOT continue running after 'wait'
  351. *
  352. * It's too early to call request_irq() here, so we handle
  353. * counter 0 interrupt as a special irq and it doesn't show
  354. * up under /proc/interrupts.
  355. *
  356. * Check to ensure we really have a 32KHz oscillator before
  357. * we do this.
  358. */
  359. if (no_au1xxx_32khz) {
  360. unsigned int c0_status;
  361. printk("WARNING: no 32KHz clock found.\n");
  362. do_gettimeoffset = do_fast_cp0_gettimeoffset;
  363. /* Ensure we get CPO_COUNTER interrupts.
  364. */
  365. c0_status = read_c0_status();
  366. c0_status |= IE_IRQ5;
  367. write_c0_status(c0_status);
  368. }
  369. else {
  370. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
  371. au_writel(0, SYS_TOYWRITE);
  372. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
  373. au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK);
  374. au_writel(~0, SYS_WAKESRC);
  375. au_sync();
  376. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
  377. /* setup match20 to interrupt once every HZ */
  378. last_pc0 = last_match20 = au_readl(SYS_TOYREAD);
  379. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  380. au_sync();
  381. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
  382. startup_match20_interrupt(counter0_irq);
  383. do_gettimeoffset = do_fast_pm_gettimeoffset;
  384. /* We can use the real 'wait' instruction.
  385. */
  386. allow_au1k_wait = 1;
  387. }
  388. #else
  389. /* We have to do this here instead of in timer_init because
  390. * the generic code in arch/mips/kernel/time.c will write
  391. * over our function pointer.
  392. */
  393. do_gettimeoffset = do_fast_cp0_gettimeoffset;
  394. #endif
  395. }
  396. void __init au1xxx_time_init(void)
  397. {
  398. }