time.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/clockchips.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/swap.h>
  13. #include <linux/sched.h>
  14. #include <linux/init.h>
  15. #include <linux/threads.h>
  16. #include <linux/cpufreq.h>
  17. #include <linux/sched_clock.h>
  18. #include <linux/mm.h>
  19. #include <asm/types.h>
  20. #include <asm/signal.h>
  21. #include <asm/io.h>
  22. #include <asm/delay.h>
  23. #include <asm/irq.h>
  24. #include <asm/irq_regs.h>
  25. #include <hwregs/reg_map.h>
  26. #include <hwregs/reg_rdwr.h>
  27. #include <hwregs/timer_defs.h>
  28. #include <hwregs/intr_vect_defs.h>
  29. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  30. #include <hwregs/clkgen_defs.h>
  31. #endif
  32. /* Watchdog defines */
  33. #define ETRAX_WD_KEY_MASK 0x7F /* key is 7 bit */
  34. #define ETRAX_WD_HZ 763 /* watchdog counts at 763 Hz */
  35. /* Number of 763 counts before watchdog bites */
  36. #define ETRAX_WD_CNT ((2*ETRAX_WD_HZ)/HZ + 1)
  37. #define CRISV32_TIMER_FREQ (100000000lu)
  38. unsigned long timer_regs[NR_CPUS] =
  39. {
  40. regi_timer0,
  41. };
  42. extern int set_rtc_mmss(unsigned long nowtime);
  43. #ifdef CONFIG_CPU_FREQ
  44. static int cris_time_freq_notifier(struct notifier_block *nb,
  45. unsigned long val, void *data);
  46. static struct notifier_block cris_time_freq_notifier_block = {
  47. .notifier_call = cris_time_freq_notifier,
  48. };
  49. #endif
  50. unsigned long get_ns_in_jiffie(void)
  51. {
  52. reg_timer_r_tmr0_data data;
  53. unsigned long ns;
  54. data = REG_RD(timer, regi_timer0, r_tmr0_data);
  55. ns = (TIMER0_DIV - data) * 10;
  56. return ns;
  57. }
  58. /* From timer MDS describing the hardware watchdog:
  59. * 4.3.1 Watchdog Operation
  60. * The watchdog timer is an 8-bit timer with a configurable start value.
  61. * Once started the watchdog counts downwards with a frequency of 763 Hz
  62. * (100/131072 MHz). When the watchdog counts down to 1, it generates an
  63. * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
  64. * chip.
  65. */
  66. /* This gives us 1.3 ms to do something useful when the NMI comes */
  67. /* Right now, starting the watchdog is the same as resetting it */
  68. #define start_watchdog reset_watchdog
  69. #if defined(CONFIG_ETRAX_WATCHDOG)
  70. static short int watchdog_key = 42; /* arbitrary 7 bit number */
  71. #endif
  72. /* Number of pages to consider "out of memory". It is normal that the memory
  73. * is used though, so set this really low. */
  74. #define WATCHDOG_MIN_FREE_PAGES 8
  75. #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
  76. /* for reliable NICE_DOGGY behaviour */
  77. static int bite_in_progress;
  78. #endif
  79. void reset_watchdog(void)
  80. {
  81. #if defined(CONFIG_ETRAX_WATCHDOG)
  82. reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
  83. #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
  84. if (unlikely(bite_in_progress))
  85. return;
  86. #endif
  87. /* Only keep watchdog happy as long as we have memory left! */
  88. if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
  89. /* Reset the watchdog with the inverse of the old key */
  90. /* Invert key, which is 7 bits */
  91. watchdog_key ^= ETRAX_WD_KEY_MASK;
  92. wd_ctrl.cnt = ETRAX_WD_CNT;
  93. wd_ctrl.cmd = regk_timer_start;
  94. wd_ctrl.key = watchdog_key;
  95. REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
  96. }
  97. #endif
  98. }
  99. /* stop the watchdog - we still need the correct key */
  100. void stop_watchdog(void)
  101. {
  102. #if defined(CONFIG_ETRAX_WATCHDOG)
  103. reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
  104. watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
  105. wd_ctrl.cnt = ETRAX_WD_CNT;
  106. wd_ctrl.cmd = regk_timer_stop;
  107. wd_ctrl.key = watchdog_key;
  108. REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
  109. #endif
  110. }
  111. extern void show_registers(struct pt_regs *regs);
  112. void handle_watchdog_bite(struct pt_regs *regs)
  113. {
  114. #if defined(CONFIG_ETRAX_WATCHDOG)
  115. extern int cause_of_death;
  116. nmi_enter();
  117. oops_in_progress = 1;
  118. #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
  119. bite_in_progress = 1;
  120. #endif
  121. printk(KERN_WARNING "Watchdog bite\n");
  122. /* Check if forced restart or unexpected watchdog */
  123. if (cause_of_death == 0xbedead) {
  124. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  125. /* There is a bug in Artpec-3 (voodoo TR 78) that requires
  126. * us to go to lower frequency for the reset to be reliable
  127. */
  128. reg_clkgen_rw_clk_ctrl ctrl =
  129. REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
  130. ctrl.pll = 0;
  131. REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl);
  132. #endif
  133. while(1);
  134. }
  135. /* Unexpected watchdog, stop the watchdog and dump registers. */
  136. stop_watchdog();
  137. printk(KERN_WARNING "Oops: bitten by watchdog\n");
  138. show_registers(regs);
  139. oops_in_progress = 0;
  140. printk("\n"); /* Flush mtdoops. */
  141. #ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
  142. reset_watchdog();
  143. #endif
  144. while(1) /* nothing */;
  145. #endif
  146. }
  147. extern void cris_profile_sample(struct pt_regs *regs);
  148. static void __iomem *timer_base;
  149. static int crisv32_clkevt_switch_state(struct clock_event_device *dev)
  150. {
  151. reg_timer_rw_tmr0_ctrl ctrl = {
  152. .op = regk_timer_hold,
  153. .freq = regk_timer_f100,
  154. };
  155. REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
  156. return 0;
  157. }
  158. static int crisv32_clkevt_next_event(unsigned long evt,
  159. struct clock_event_device *dev)
  160. {
  161. reg_timer_rw_tmr0_ctrl ctrl = {
  162. .op = regk_timer_ld,
  163. .freq = regk_timer_f100,
  164. };
  165. REG_WR(timer, timer_base, rw_tmr0_div, evt);
  166. REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
  167. ctrl.op = regk_timer_run;
  168. REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
  169. return 0;
  170. }
  171. static irqreturn_t crisv32_timer_interrupt(int irq, void *dev_id)
  172. {
  173. struct clock_event_device *evt = dev_id;
  174. reg_timer_rw_tmr0_ctrl ctrl = {
  175. .op = regk_timer_hold,
  176. .freq = regk_timer_f100,
  177. };
  178. reg_timer_rw_ack_intr ack = { .tmr0 = 1 };
  179. reg_timer_r_masked_intr intr;
  180. intr = REG_RD(timer, timer_base, r_masked_intr);
  181. if (!intr.tmr0)
  182. return IRQ_NONE;
  183. REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
  184. REG_WR(timer, timer_base, rw_ack_intr, ack);
  185. reset_watchdog();
  186. #ifdef CONFIG_SYSTEM_PROFILER
  187. cris_profile_sample(get_irq_regs());
  188. #endif
  189. evt->event_handler(evt);
  190. return IRQ_HANDLED;
  191. }
  192. static struct clock_event_device crisv32_clockevent = {
  193. .name = "crisv32-timer",
  194. .rating = 300,
  195. .features = CLOCK_EVT_FEAT_ONESHOT,
  196. .set_state_oneshot = crisv32_clkevt_switch_state,
  197. .set_state_shutdown = crisv32_clkevt_switch_state,
  198. .tick_resume = crisv32_clkevt_switch_state,
  199. .set_next_event = crisv32_clkevt_next_event,
  200. };
  201. /* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain. */
  202. static struct irqaction irq_timer = {
  203. .handler = crisv32_timer_interrupt,
  204. .flags = IRQF_TIMER | IRQF_SHARED,
  205. .name = "crisv32-timer",
  206. .dev_id = &crisv32_clockevent,
  207. };
  208. static u64 notrace crisv32_timer_sched_clock(void)
  209. {
  210. return REG_RD(timer, timer_base, r_time);
  211. }
  212. static void __init crisv32_timer_init(void)
  213. {
  214. reg_timer_rw_intr_mask timer_intr_mask;
  215. reg_timer_rw_tmr0_ctrl ctrl = {
  216. .op = regk_timer_hold,
  217. .freq = regk_timer_f100,
  218. };
  219. REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
  220. timer_intr_mask = REG_RD(timer, timer_base, rw_intr_mask);
  221. timer_intr_mask.tmr0 = 1;
  222. REG_WR(timer, timer_base, rw_intr_mask, timer_intr_mask);
  223. }
  224. void __init time_init(void)
  225. {
  226. int irq;
  227. int ret;
  228. /* Probe for the RTC and read it if it exists.
  229. * Before the RTC can be probed the loops_per_usec variable needs
  230. * to be initialized to make usleep work. A better value for
  231. * loops_per_usec is calculated by the kernel later once the
  232. * clock has started.
  233. */
  234. loops_per_usec = 50;
  235. irq = TIMER0_INTR_VECT;
  236. timer_base = (void __iomem *) regi_timer0;
  237. crisv32_timer_init();
  238. sched_clock_register(crisv32_timer_sched_clock, 32,
  239. CRISV32_TIMER_FREQ);
  240. clocksource_mmio_init(timer_base + REG_RD_ADDR_timer_r_time,
  241. "crisv32-timer", CRISV32_TIMER_FREQ,
  242. 300, 32, clocksource_mmio_readl_up);
  243. crisv32_clockevent.cpumask = cpu_possible_mask;
  244. crisv32_clockevent.irq = irq;
  245. ret = setup_irq(irq, &irq_timer);
  246. if (ret)
  247. pr_warn("failed to setup irq %d\n", irq);
  248. clockevents_config_and_register(&crisv32_clockevent,
  249. CRISV32_TIMER_FREQ,
  250. 2, 0xffffffff);
  251. /* Enable watchdog if we should use one. */
  252. #if defined(CONFIG_ETRAX_WATCHDOG)
  253. printk(KERN_INFO "Enabling watchdog...\n");
  254. start_watchdog();
  255. /* If we use the hardware watchdog, we want to trap it as an NMI
  256. * and dump registers before it resets us. For this to happen, we
  257. * must set the "m" NMI enable flag (which once set, is unset only
  258. * when an NMI is taken). */
  259. {
  260. unsigned long flags;
  261. local_save_flags(flags);
  262. flags |= (1<<30); /* NMI M flag is at bit 30 */
  263. local_irq_restore(flags);
  264. }
  265. #endif
  266. #ifdef CONFIG_CPU_FREQ
  267. cpufreq_register_notifier(&cris_time_freq_notifier_block,
  268. CPUFREQ_TRANSITION_NOTIFIER);
  269. #endif
  270. }
  271. #ifdef CONFIG_CPU_FREQ
  272. static int cris_time_freq_notifier(struct notifier_block *nb,
  273. unsigned long val, void *data)
  274. {
  275. struct cpufreq_freqs *freqs = data;
  276. if (val == CPUFREQ_POSTCHANGE) {
  277. reg_timer_r_tmr0_data data;
  278. reg_timer_rw_tmr0_div div = (freqs->new * 500) / HZ;
  279. do {
  280. data = REG_RD(timer, timer_regs[freqs->cpu],
  281. r_tmr0_data);
  282. } while (data > 20);
  283. REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div);
  284. }
  285. return 0;
  286. }
  287. #endif