time.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. *
  3. * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@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/spinlock.h>
  37. #include <asm/mipsregs.h>
  38. #include <asm/time.h>
  39. #include <asm/mach-au1x00/au1000.h>
  40. static int no_au1xxx_32khz;
  41. extern int allow_au1k_wait; /* default off for CP0 Counter */
  42. #ifdef CONFIG_PM
  43. #if HZ < 100 || HZ > 1000
  44. #error "unsupported HZ value! Must be in [100,1000]"
  45. #endif
  46. #define MATCH20_INC (328*100/HZ) /* magic number 328 is for HZ=100... */
  47. extern void startup_match20_interrupt(irq_handler_t handler);
  48. static unsigned long last_pc0, last_match20;
  49. #endif
  50. static DEFINE_SPINLOCK(time_lock);
  51. unsigned long wtimer;
  52. #ifdef CONFIG_PM
  53. static irqreturn_t counter0_irq(int irq, void *dev_id)
  54. {
  55. unsigned long pc0;
  56. int time_elapsed;
  57. static int jiffie_drift = 0;
  58. if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
  59. /* should never happen! */
  60. printk(KERN_WARNING "counter 0 w status error\n");
  61. return IRQ_NONE;
  62. }
  63. pc0 = au_readl(SYS_TOYREAD);
  64. if (pc0 < last_match20) {
  65. /* counter overflowed */
  66. time_elapsed = (0xffffffff - last_match20) + pc0;
  67. }
  68. else {
  69. time_elapsed = pc0 - last_match20;
  70. }
  71. while (time_elapsed > 0) {
  72. do_timer(1);
  73. #ifndef CONFIG_SMP
  74. update_process_times(user_mode(get_irq_regs()));
  75. #endif
  76. time_elapsed -= MATCH20_INC;
  77. last_match20 += MATCH20_INC;
  78. jiffie_drift++;
  79. }
  80. last_pc0 = pc0;
  81. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  82. au_sync();
  83. /* our counter ticks at 10.009765625 ms/tick, we we're running
  84. * almost 10uS too slow per tick.
  85. */
  86. if (jiffie_drift >= 999) {
  87. jiffie_drift -= 999;
  88. do_timer(1); /* increment jiffies by one */
  89. #ifndef CONFIG_SMP
  90. update_process_times(user_mode(get_irq_regs()));
  91. #endif
  92. }
  93. return IRQ_HANDLED;
  94. }
  95. struct irqaction counter0_action = {
  96. .handler = counter0_irq,
  97. .flags = IRQF_DISABLED,
  98. .name = "alchemy-toy",
  99. .dev_id = NULL,
  100. };
  101. /* When we wakeup from sleep, we have to "catch up" on all of the
  102. * timer ticks we have missed.
  103. */
  104. void
  105. wakeup_counter0_adjust(void)
  106. {
  107. unsigned long pc0;
  108. int time_elapsed;
  109. pc0 = au_readl(SYS_TOYREAD);
  110. if (pc0 < last_match20) {
  111. /* counter overflowed */
  112. time_elapsed = (0xffffffff - last_match20) + pc0;
  113. }
  114. else {
  115. time_elapsed = pc0 - last_match20;
  116. }
  117. while (time_elapsed > 0) {
  118. time_elapsed -= MATCH20_INC;
  119. last_match20 += MATCH20_INC;
  120. }
  121. last_pc0 = pc0;
  122. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  123. au_sync();
  124. }
  125. /* This is just for debugging to set the timer for a sleep delay.
  126. */
  127. void
  128. wakeup_counter0_set(int ticks)
  129. {
  130. unsigned long pc0;
  131. pc0 = au_readl(SYS_TOYREAD);
  132. last_pc0 = pc0;
  133. au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
  134. au_sync();
  135. }
  136. #endif
  137. /* I haven't found anyone that doesn't use a 12 MHz source clock,
  138. * but just in case.....
  139. */
  140. #define AU1000_SRC_CLK 12000000
  141. /*
  142. * We read the real processor speed from the PLL. This is important
  143. * because it is more accurate than computing it from the 32KHz
  144. * counter, if it exists. If we don't have an accurate processor
  145. * speed, all of the peripherals that derive their clocks based on
  146. * this advertised speed will introduce error and sometimes not work
  147. * properly. This function is futher convoluted to still allow configurations
  148. * to do that in case they have really, really old silicon with a
  149. * write-only PLL register, that we need the 32KHz when power management
  150. * "wait" is enabled, and we need to detect if the 32KHz isn't present
  151. * but requested......got it? :-) -- Dan
  152. */
  153. unsigned long calc_clock(void)
  154. {
  155. unsigned long cpu_speed;
  156. unsigned long flags;
  157. unsigned long counter;
  158. spin_lock_irqsave(&time_lock, flags);
  159. /* Power management cares if we don't have a 32KHz counter.
  160. */
  161. no_au1xxx_32khz = 0;
  162. counter = au_readl(SYS_COUNTER_CNTRL);
  163. if (counter & SYS_CNTRL_E0) {
  164. int trim_divide = 16;
  165. au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
  166. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
  167. /* RTC now ticks at 32.768/16 kHz */
  168. au_writel(trim_divide-1, SYS_RTCTRIM);
  169. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
  170. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
  171. au_writel(0, SYS_TOYWRITE);
  172. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
  173. } else
  174. no_au1xxx_32khz = 1;
  175. /*
  176. * On early Au1000, sys_cpupll was write-only. Since these
  177. * silicon versions of Au1000 are not sold by AMD, we don't bend
  178. * over backwards trying to determine the frequency.
  179. */
  180. if (cur_cpu_spec[0]->cpu_pll_wo)
  181. #ifdef CONFIG_SOC_AU1000_FREQUENCY
  182. cpu_speed = CONFIG_SOC_AU1000_FREQUENCY;
  183. #else
  184. cpu_speed = 396000000;
  185. #endif
  186. else
  187. cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
  188. mips_hpt_frequency = cpu_speed;
  189. // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16)
  190. set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
  191. spin_unlock_irqrestore(&time_lock, flags);
  192. return cpu_speed;
  193. }
  194. void __init plat_time_init(void)
  195. {
  196. unsigned int est_freq = calc_clock();
  197. est_freq += 5000; /* round */
  198. est_freq -= est_freq%10000;
  199. printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
  200. (est_freq%1000000)*100/1000000);
  201. set_au1x00_speed(est_freq);
  202. set_au1x00_lcd_clock(); // program the LCD clock
  203. #ifdef CONFIG_PM
  204. /*
  205. * setup counter 0, since it keeps ticking after a
  206. * 'wait' instruction has been executed. The CP0 timer and
  207. * counter 1 do NOT continue running after 'wait'
  208. *
  209. * It's too early to call request_irq() here, so we handle
  210. * counter 0 interrupt as a special irq and it doesn't show
  211. * up under /proc/interrupts.
  212. *
  213. * Check to ensure we really have a 32KHz oscillator before
  214. * we do this.
  215. */
  216. if (no_au1xxx_32khz)
  217. printk("WARNING: no 32KHz clock found.\n");
  218. else {
  219. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
  220. au_writel(0, SYS_TOYWRITE);
  221. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
  222. au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK);
  223. au_writel(~0, SYS_WAKESRC);
  224. au_sync();
  225. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
  226. /* setup match20 to interrupt once every HZ */
  227. last_pc0 = last_match20 = au_readl(SYS_TOYREAD);
  228. au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
  229. au_sync();
  230. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
  231. setup_irq(AU1000_TOY_MATCH2_INT, &counter0_action);
  232. /* We can use the real 'wait' instruction.
  233. */
  234. allow_au1k_wait = 1;
  235. }
  236. #endif
  237. }