time.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * linux/arch/cris/arch-v32/kernel/time.c
  3. *
  4. * Copyright (C) 2003-2010 Axis Communications AB
  5. *
  6. */
  7. #include <linux/timex.h>
  8. #include <linux/time.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/swap.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/threads.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/mm.h>
  17. #include <asm/types.h>
  18. #include <asm/signal.h>
  19. #include <asm/io.h>
  20. #include <asm/delay.h>
  21. #include <asm/irq.h>
  22. #include <asm/irq_regs.h>
  23. #include <hwregs/reg_map.h>
  24. #include <hwregs/reg_rdwr.h>
  25. #include <hwregs/timer_defs.h>
  26. #include <hwregs/intr_vect_defs.h>
  27. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  28. #include <hwregs/clkgen_defs.h>
  29. #endif
  30. /* Watchdog defines */
  31. #define ETRAX_WD_KEY_MASK 0x7F /* key is 7 bit */
  32. #define ETRAX_WD_HZ 763 /* watchdog counts at 763 Hz */
  33. /* Number of 763 counts before watchdog bites */
  34. #define ETRAX_WD_CNT ((2*ETRAX_WD_HZ)/HZ + 1)
  35. /* Register the continuos readonly timer available in FS and ARTPEC-3. */
  36. static cycle_t read_cont_rotime(struct clocksource *cs)
  37. {
  38. return (u32)REG_RD(timer, regi_timer0, r_time);
  39. }
  40. static struct clocksource cont_rotime = {
  41. .name = "crisv32_rotime",
  42. .rating = 300,
  43. .read = read_cont_rotime,
  44. .mask = CLOCKSOURCE_MASK(32),
  45. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  46. };
  47. static int __init etrax_init_cont_rotime(void)
  48. {
  49. clocksource_register_khz(&cont_rotime, 100000);
  50. return 0;
  51. }
  52. arch_initcall(etrax_init_cont_rotime);
  53. unsigned long timer_regs[NR_CPUS] =
  54. {
  55. regi_timer0,
  56. #ifdef CONFIG_SMP
  57. regi_timer2
  58. #endif
  59. };
  60. extern int set_rtc_mmss(unsigned long nowtime);
  61. #ifdef CONFIG_CPU_FREQ
  62. static int
  63. cris_time_freq_notifier(struct notifier_block *nb, unsigned long val,
  64. void *data);
  65. static struct notifier_block cris_time_freq_notifier_block = {
  66. .notifier_call = cris_time_freq_notifier,
  67. };
  68. #endif
  69. unsigned long get_ns_in_jiffie(void)
  70. {
  71. reg_timer_r_tmr0_data data;
  72. unsigned long ns;
  73. data = REG_RD(timer, regi_timer0, r_tmr0_data);
  74. ns = (TIMER0_DIV - data) * 10;
  75. return ns;
  76. }
  77. /* From timer MDS describing the hardware watchdog:
  78. * 4.3.1 Watchdog Operation
  79. * The watchdog timer is an 8-bit timer with a configurable start value.
  80. * Once started the watchdog counts downwards with a frequency of 763 Hz
  81. * (100/131072 MHz). When the watchdog counts down to 1, it generates an
  82. * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
  83. * chip.
  84. */
  85. /* This gives us 1.3 ms to do something useful when the NMI comes */
  86. /* Right now, starting the watchdog is the same as resetting it */
  87. #define start_watchdog reset_watchdog
  88. #if defined(CONFIG_ETRAX_WATCHDOG)
  89. static short int watchdog_key = 42; /* arbitrary 7 bit number */
  90. #endif
  91. /* Number of pages to consider "out of memory". It is normal that the memory
  92. * is used though, so set this really low. */
  93. #define WATCHDOG_MIN_FREE_PAGES 8
  94. void reset_watchdog(void)
  95. {
  96. #if defined(CONFIG_ETRAX_WATCHDOG)
  97. reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
  98. /* Only keep watchdog happy as long as we have memory left! */
  99. if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
  100. /* Reset the watchdog with the inverse of the old key */
  101. /* Invert key, which is 7 bits */
  102. watchdog_key ^= ETRAX_WD_KEY_MASK;
  103. wd_ctrl.cnt = ETRAX_WD_CNT;
  104. wd_ctrl.cmd = regk_timer_start;
  105. wd_ctrl.key = watchdog_key;
  106. REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
  107. }
  108. #endif
  109. }
  110. /* stop the watchdog - we still need the correct key */
  111. void stop_watchdog(void)
  112. {
  113. #if defined(CONFIG_ETRAX_WATCHDOG)
  114. reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
  115. watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
  116. wd_ctrl.cnt = ETRAX_WD_CNT;
  117. wd_ctrl.cmd = regk_timer_stop;
  118. wd_ctrl.key = watchdog_key;
  119. REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
  120. #endif
  121. }
  122. extern void show_registers(struct pt_regs *regs);
  123. void handle_watchdog_bite(struct pt_regs *regs)
  124. {
  125. #if defined(CONFIG_ETRAX_WATCHDOG)
  126. extern int cause_of_death;
  127. oops_in_progress = 1;
  128. printk(KERN_WARNING "Watchdog bite\n");
  129. /* Check if forced restart or unexpected watchdog */
  130. if (cause_of_death == 0xbedead) {
  131. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  132. /* There is a bug in Artpec-3 (voodoo TR 78) that requires
  133. * us to go to lower frequency for the reset to be reliable
  134. */
  135. reg_clkgen_rw_clk_ctrl ctrl =
  136. REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
  137. ctrl.pll = 0;
  138. REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl);
  139. #endif
  140. while(1);
  141. }
  142. /* Unexpected watchdog, stop the watchdog and dump registers. */
  143. stop_watchdog();
  144. printk(KERN_WARNING "Oops: bitten by watchdog\n");
  145. show_registers(regs);
  146. oops_in_progress = 0;
  147. #ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
  148. reset_watchdog();
  149. #endif
  150. while(1) /* nothing */;
  151. #endif
  152. }
  153. /*
  154. * timer_interrupt() needs to keep up the real-time clock,
  155. * as well as call the "xtime_update()" routine every clocktick.
  156. */
  157. extern void cris_do_profile(struct pt_regs *regs);
  158. static inline irqreturn_t timer_interrupt(int irq, void *dev_id)
  159. {
  160. struct pt_regs *regs = get_irq_regs();
  161. int cpu = smp_processor_id();
  162. reg_timer_r_masked_intr masked_intr;
  163. reg_timer_rw_ack_intr ack_intr = { 0 };
  164. /* Check if the timer interrupt is for us (a tmr0 int) */
  165. masked_intr = REG_RD(timer, timer_regs[cpu], r_masked_intr);
  166. if (!masked_intr.tmr0)
  167. return IRQ_NONE;
  168. /* Acknowledge the timer irq. */
  169. ack_intr.tmr0 = 1;
  170. REG_WR(timer, timer_regs[cpu], rw_ack_intr, ack_intr);
  171. /* Reset watchdog otherwise it resets us! */
  172. reset_watchdog();
  173. /* Update statistics. */
  174. update_process_times(user_mode(regs));
  175. cris_do_profile(regs); /* Save profiling information */
  176. /* The master CPU is responsible for the time keeping. */
  177. if (cpu != 0)
  178. return IRQ_HANDLED;
  179. /* Call the real timer interrupt handler */
  180. xtime_update(1);
  181. return IRQ_HANDLED;
  182. }
  183. /* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain. */
  184. static struct irqaction irq_timer = {
  185. .handler = timer_interrupt,
  186. .flags = IRQF_SHARED,
  187. .name = "timer"
  188. };
  189. void __init cris_timer_init(void)
  190. {
  191. int cpu = smp_processor_id();
  192. reg_timer_rw_tmr0_ctrl tmr0_ctrl = { 0 };
  193. reg_timer_rw_tmr0_div tmr0_div = TIMER0_DIV;
  194. reg_timer_rw_intr_mask timer_intr_mask;
  195. /* Setup the etrax timers.
  196. * Base frequency is 100MHz, divider 1000000 -> 100 HZ
  197. * We use timer0, so timer1 is free.
  198. * The trig timer is used by the fasttimer API if enabled.
  199. */
  200. tmr0_ctrl.op = regk_timer_ld;
  201. tmr0_ctrl.freq = regk_timer_f100;
  202. REG_WR(timer, timer_regs[cpu], rw_tmr0_div, tmr0_div);
  203. REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Load */
  204. tmr0_ctrl.op = regk_timer_run;
  205. REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Start */
  206. /* Enable the timer irq. */
  207. timer_intr_mask = REG_RD(timer, timer_regs[cpu], rw_intr_mask);
  208. timer_intr_mask.tmr0 = 1;
  209. REG_WR(timer, timer_regs[cpu], rw_intr_mask, timer_intr_mask);
  210. }
  211. void __init time_init(void)
  212. {
  213. reg_intr_vect_rw_mask intr_mask;
  214. /* Probe for the RTC and read it if it exists.
  215. * Before the RTC can be probed the loops_per_usec variable needs
  216. * to be initialized to make usleep work. A better value for
  217. * loops_per_usec is calculated by the kernel later once the
  218. * clock has started.
  219. */
  220. loops_per_usec = 50;
  221. /* Start CPU local timer. */
  222. cris_timer_init();
  223. /* Enable the timer irq in global config. */
  224. intr_mask = REG_RD_VECT(intr_vect, regi_irq, rw_mask, 1);
  225. intr_mask.timer0 = 1;
  226. REG_WR_VECT(intr_vect, regi_irq, rw_mask, 1, intr_mask);
  227. /* Now actually register the timer irq handler that calls
  228. * timer_interrupt(). */
  229. setup_irq(TIMER0_INTR_VECT, &irq_timer);
  230. /* Enable watchdog if we should use one. */
  231. #if defined(CONFIG_ETRAX_WATCHDOG)
  232. printk(KERN_INFO "Enabling watchdog...\n");
  233. start_watchdog();
  234. /* If we use the hardware watchdog, we want to trap it as an NMI
  235. * and dump registers before it resets us. For this to happen, we
  236. * must set the "m" NMI enable flag (which once set, is unset only
  237. * when an NMI is taken). */
  238. {
  239. unsigned long flags;
  240. local_save_flags(flags);
  241. flags |= (1<<30); /* NMI M flag is at bit 30 */
  242. local_irq_restore(flags);
  243. }
  244. #endif
  245. #ifdef CONFIG_CPU_FREQ
  246. cpufreq_register_notifier(&cris_time_freq_notifier_block,
  247. CPUFREQ_TRANSITION_NOTIFIER);
  248. #endif
  249. }
  250. #ifdef CONFIG_CPU_FREQ
  251. static int
  252. cris_time_freq_notifier(struct notifier_block *nb, unsigned long val,
  253. void *data)
  254. {
  255. struct cpufreq_freqs *freqs = data;
  256. if (val == CPUFREQ_POSTCHANGE) {
  257. reg_timer_r_tmr0_data data;
  258. reg_timer_rw_tmr0_div div = (freqs->new * 500) / HZ;
  259. do {
  260. data = REG_RD(timer, timer_regs[freqs->cpu],
  261. r_tmr0_data);
  262. } while (data > 20);
  263. REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div);
  264. }
  265. return 0;
  266. }
  267. #endif