time.c 7.5 KB

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