time.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * arch/sh/kernel/time.c
  3. *
  4. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  5. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  6. * Copyright (C) 2002, 2003, 2004 Paul Mundt
  7. * Copyright (C) 2002 M. R. Brown <mrbrown@linux-sh.org>
  8. *
  9. * Some code taken from i386 version.
  10. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  11. */
  12. #include <linux/config.h>
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/param.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/time.h>
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/smp.h>
  25. #include <linux/profile.h>
  26. #include <asm/processor.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/io.h>
  29. #include <asm/irq.h>
  30. #include <asm/delay.h>
  31. #include <asm/machvec.h>
  32. #include <asm/rtc.h>
  33. #include <asm/freq.h>
  34. #include <asm/cpu/timer.h>
  35. #ifdef CONFIG_SH_KGDB
  36. #include <asm/kgdb.h>
  37. #endif
  38. #include <linux/timex.h>
  39. #include <linux/irq.h>
  40. #define TMU_TOCR_INIT 0x00
  41. #define TMU0_TCR_INIT 0x0020
  42. #define TMU_TSTR_INIT 1
  43. #define TMU0_TCR_CALIB 0x0000
  44. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  45. #define CLOCKGEN_MEMCLKCR 0xbb040038
  46. #define MEMCLKCR_RATIO_MASK 0x7
  47. #endif /* CONFIG_CPU_SUBTYPE_ST40STB1 */
  48. extern unsigned long wall_jiffies;
  49. #define TICK_SIZE (tick_nsec / 1000)
  50. DEFINE_SPINLOCK(tmu0_lock);
  51. u64 jiffies_64 = INITIAL_JIFFIES;
  52. EXPORT_SYMBOL(jiffies_64);
  53. /* XXX: Can we initialize this in a routine somewhere? Dreamcast doesn't want
  54. * these routines anywhere... */
  55. #ifdef CONFIG_SH_RTC
  56. void (*rtc_get_time)(struct timespec *) = sh_rtc_gettimeofday;
  57. int (*rtc_set_time)(const time_t) = sh_rtc_settimeofday;
  58. #else
  59. void (*rtc_get_time)(struct timespec *);
  60. int (*rtc_set_time)(const time_t);
  61. #endif
  62. #if defined(CONFIG_CPU_SUBTYPE_SH7300)
  63. static int md_table[] = { 1, 2, 3, 4, 6, 8, 12 };
  64. #endif
  65. #if defined(CONFIG_CPU_SH3)
  66. static int stc_multipliers[] = { 1, 2, 3, 4, 6, 1, 1, 1 };
  67. static int stc_values[] = { 0, 1, 4, 2, 5, 0, 0, 0 };
  68. #define bfc_divisors stc_multipliers
  69. #define bfc_values stc_values
  70. static int ifc_divisors[] = { 1, 2, 3, 4, 1, 1, 1, 1 };
  71. static int ifc_values[] = { 0, 1, 4, 2, 0, 0, 0, 0 };
  72. static int pfc_divisors[] = { 1, 2, 3, 4, 6, 1, 1, 1 };
  73. static int pfc_values[] = { 0, 1, 4, 2, 5, 0, 0, 0 };
  74. #elif defined(CONFIG_CPU_SH4)
  75. #if defined(CONFIG_CPU_SUBTYPE_SH73180)
  76. static int ifc_divisors[] = { 1, 2, 3, 4, 6, 8, 12, 16 };
  77. static int ifc_values[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  78. #define bfc_divisors ifc_divisors /* Same */
  79. #define bfc_values ifc_values
  80. #define pfc_divisors ifc_divisors /* Same */
  81. #define pfc_values ifc_values
  82. #else
  83. static int ifc_divisors[] = { 1, 2, 3, 4, 6, 8, 1, 1 };
  84. static int ifc_values[] = { 0, 1, 2, 3, 0, 4, 0, 5 };
  85. #define bfc_divisors ifc_divisors /* Same */
  86. #define bfc_values ifc_values
  87. static int pfc_divisors[] = { 2, 3, 4, 6, 8, 2, 2, 2 };
  88. static int pfc_values[] = { 0, 0, 1, 2, 0, 3, 0, 4 };
  89. #endif
  90. #else
  91. #error "Unknown ifc/bfc/pfc/stc values for this processor"
  92. #endif
  93. /*
  94. * Scheduler clock - returns current time in nanosec units.
  95. */
  96. unsigned long long sched_clock(void)
  97. {
  98. return (unsigned long long)jiffies * (1000000000 / HZ);
  99. }
  100. static unsigned long do_gettimeoffset(void)
  101. {
  102. int count;
  103. unsigned long flags;
  104. static int count_p = 0x7fffffff; /* for the first call after boot */
  105. static unsigned long jiffies_p = 0;
  106. /*
  107. * cache volatile jiffies temporarily; we have IRQs turned off.
  108. */
  109. unsigned long jiffies_t;
  110. spin_lock_irqsave(&tmu0_lock, flags);
  111. /* timer count may underflow right here */
  112. count = ctrl_inl(TMU0_TCNT); /* read the latched count */
  113. jiffies_t = jiffies;
  114. /*
  115. * avoiding timer inconsistencies (they are rare, but they happen)...
  116. * there is one kind of problem that must be avoided here:
  117. * 1. the timer counter underflows
  118. */
  119. if( jiffies_t == jiffies_p ) {
  120. if( count > count_p ) {
  121. /* the nutcase */
  122. if(ctrl_inw(TMU0_TCR) & 0x100) { /* Check UNF bit */
  123. /*
  124. * We cannot detect lost timer interrupts ...
  125. * well, that's why we call them lost, don't we? :)
  126. * [hmm, on the Pentium and Alpha we can ... sort of]
  127. */
  128. count -= LATCH;
  129. } else {
  130. printk("do_slow_gettimeoffset(): hardware timer problem?\n");
  131. }
  132. }
  133. } else
  134. jiffies_p = jiffies_t;
  135. count_p = count;
  136. spin_unlock_irqrestore(&tmu0_lock, flags);
  137. count = ((LATCH-1) - count) * TICK_SIZE;
  138. count = (count + LATCH/2) / LATCH;
  139. return count;
  140. }
  141. void do_gettimeofday(struct timeval *tv)
  142. {
  143. unsigned long seq;
  144. unsigned long usec, sec;
  145. unsigned long lost;
  146. do {
  147. seq = read_seqbegin(&xtime_lock);
  148. usec = do_gettimeoffset();
  149. lost = jiffies - wall_jiffies;
  150. if (lost)
  151. usec += lost * (1000000 / HZ);
  152. sec = xtime.tv_sec;
  153. usec += xtime.tv_nsec / 1000;
  154. } while (read_seqretry(&xtime_lock, seq));
  155. while (usec >= 1000000) {
  156. usec -= 1000000;
  157. sec++;
  158. }
  159. tv->tv_sec = sec;
  160. tv->tv_usec = usec;
  161. }
  162. EXPORT_SYMBOL(do_gettimeofday);
  163. int do_settimeofday(struct timespec *tv)
  164. {
  165. time_t wtm_sec, sec = tv->tv_sec;
  166. long wtm_nsec, nsec = tv->tv_nsec;
  167. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  168. return -EINVAL;
  169. write_seqlock_irq(&xtime_lock);
  170. /*
  171. * This is revolting. We need to set "xtime" correctly. However, the
  172. * value in this location is the value at the most recent update of
  173. * wall time. Discover what correction gettimeofday() would have
  174. * made, and then undo it!
  175. */
  176. nsec -= 1000 * (do_gettimeoffset() +
  177. (jiffies - wall_jiffies) * (1000000 / HZ));
  178. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  179. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  180. set_normalized_timespec(&xtime, sec, nsec);
  181. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  182. ntp_clear();
  183. write_sequnlock_irq(&xtime_lock);
  184. clock_was_set();
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(do_settimeofday);
  188. /* last time the RTC clock got updated */
  189. static long last_rtc_update;
  190. /*
  191. * timer_interrupt() needs to keep up the real-time clock,
  192. * as well as call the "do_timer()" routine every clocktick
  193. */
  194. static inline void do_timer_interrupt(int irq, struct pt_regs *regs)
  195. {
  196. do_timer(regs);
  197. #ifndef CONFIG_SMP
  198. update_process_times(user_mode(regs));
  199. #endif
  200. profile_tick(CPU_PROFILING, regs);
  201. #ifdef CONFIG_HEARTBEAT
  202. if (sh_mv.mv_heartbeat != NULL)
  203. sh_mv.mv_heartbeat();
  204. #endif
  205. /*
  206. * If we have an externally synchronized Linux clock, then update
  207. * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  208. * called as close as possible to 500 ms before the new second starts.
  209. */
  210. if (ntp_synced() &&
  211. xtime.tv_sec > last_rtc_update + 660 &&
  212. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  213. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  214. if (rtc_set_time(xtime.tv_sec) == 0)
  215. last_rtc_update = xtime.tv_sec;
  216. else
  217. last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
  218. }
  219. }
  220. /*
  221. * This is the same as the above, except we _also_ save the current
  222. * Time Stamp Counter value at the time of the timer interrupt, so that
  223. * we later on can estimate the time of day more exactly.
  224. */
  225. static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  226. {
  227. unsigned long timer_status;
  228. /* Clear UNF bit */
  229. timer_status = ctrl_inw(TMU0_TCR);
  230. timer_status &= ~0x100;
  231. ctrl_outw(timer_status, TMU0_TCR);
  232. /*
  233. * Here we are in the timer irq handler. We just have irqs locally
  234. * disabled but we don't know if the timer_bh is running on the other
  235. * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
  236. * the irq version of write_lock because as just said we have irq
  237. * locally disabled. -arca
  238. */
  239. write_seqlock(&xtime_lock);
  240. do_timer_interrupt(irq, regs);
  241. write_sequnlock(&xtime_lock);
  242. return IRQ_HANDLED;
  243. }
  244. /*
  245. * Hah! We'll see if this works (switching from usecs to nsecs).
  246. */
  247. static unsigned int __init get_timer_frequency(void)
  248. {
  249. u32 freq;
  250. struct timespec ts1, ts2;
  251. unsigned long diff_nsec;
  252. unsigned long factor;
  253. /* Setup the timer: We don't want to generate interrupts, just
  254. * have it count down at its natural rate.
  255. */
  256. ctrl_outb(0, TMU_TSTR);
  257. #if !defined(CONFIG_CPU_SUBTYPE_SH7300)
  258. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  259. #endif
  260. ctrl_outw(TMU0_TCR_CALIB, TMU0_TCR);
  261. ctrl_outl(0xffffffff, TMU0_TCOR);
  262. ctrl_outl(0xffffffff, TMU0_TCNT);
  263. rtc_get_time(&ts2);
  264. do {
  265. rtc_get_time(&ts1);
  266. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  267. /* actually start the timer */
  268. ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
  269. do {
  270. rtc_get_time(&ts2);
  271. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  272. freq = 0xffffffff - ctrl_inl(TMU0_TCNT);
  273. if (ts2.tv_nsec < ts1.tv_nsec) {
  274. ts2.tv_nsec += 1000000000;
  275. ts2.tv_sec--;
  276. }
  277. diff_nsec = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
  278. /* this should work well if the RTC has a precision of n Hz, where
  279. * n is an integer. I don't think we have to worry about the other
  280. * cases. */
  281. factor = (1000000000 + diff_nsec/2) / diff_nsec;
  282. if (factor * diff_nsec > 1100000000 ||
  283. factor * diff_nsec < 900000000)
  284. panic("weird RTC (diff_nsec %ld)", diff_nsec);
  285. return freq * factor;
  286. }
  287. void (*board_time_init)(void);
  288. void (*board_timer_setup)(struct irqaction *irq);
  289. static unsigned int sh_pclk_freq __initdata = CONFIG_SH_PCLK_FREQ;
  290. static int __init sh_pclk_setup(char *str)
  291. {
  292. unsigned int freq;
  293. if (get_option(&str, &freq))
  294. sh_pclk_freq = freq;
  295. return 1;
  296. }
  297. __setup("sh_pclk=", sh_pclk_setup);
  298. static struct irqaction irq0 = { timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer", NULL, NULL};
  299. void get_current_frequency_divisors(unsigned int *ifc, unsigned int *bfc, unsigned int *pfc)
  300. {
  301. unsigned int frqcr = ctrl_inw(FRQCR);
  302. #if defined(CONFIG_CPU_SH3)
  303. #if defined(CONFIG_CPU_SUBTYPE_SH7300)
  304. *ifc = md_table[((frqcr & 0x0070) >> 4)];
  305. *bfc = md_table[((frqcr & 0x0700) >> 8)];
  306. *pfc = md_table[frqcr & 0x0007];
  307. #elif defined(CONFIG_CPU_SUBTYPE_SH7705)
  308. *bfc = stc_multipliers[(frqcr & 0x0300) >> 8];
  309. *ifc = ifc_divisors[(frqcr & 0x0030) >> 4];
  310. *pfc = pfc_divisors[frqcr & 0x0003];
  311. #else
  312. unsigned int tmp;
  313. tmp = (frqcr & 0x8000) >> 13;
  314. tmp |= (frqcr & 0x0030) >> 4;
  315. *bfc = stc_multipliers[tmp];
  316. tmp = (frqcr & 0x4000) >> 12;
  317. tmp |= (frqcr & 0x000c) >> 2;
  318. *ifc = ifc_divisors[tmp];
  319. tmp = (frqcr & 0x2000) >> 11;
  320. tmp |= frqcr & 0x0003;
  321. *pfc = pfc_divisors[tmp];
  322. #endif
  323. #elif defined(CONFIG_CPU_SH4)
  324. #if defined(CONFIG_CPU_SUBTYPE_SH73180)
  325. *ifc = ifc_divisors[(frqcr>> 20) & 0x0007];
  326. *bfc = bfc_divisors[(frqcr>> 12) & 0x0007];
  327. *pfc = pfc_divisors[frqcr & 0x0007];
  328. #else
  329. *ifc = ifc_divisors[(frqcr >> 6) & 0x0007];
  330. *bfc = bfc_divisors[(frqcr >> 3) & 0x0007];
  331. *pfc = pfc_divisors[frqcr & 0x0007];
  332. #endif
  333. #endif
  334. }
  335. /*
  336. * This bit of ugliness builds up accessor routines to get at both
  337. * the divisors and the physical values.
  338. */
  339. #define _FREQ_TABLE(x) \
  340. unsigned int get_##x##_divisor(unsigned int value) \
  341. { return x##_divisors[value]; } \
  342. \
  343. unsigned int get_##x##_value(unsigned int divisor) \
  344. { return x##_values[(divisor - 1)]; }
  345. _FREQ_TABLE(ifc);
  346. _FREQ_TABLE(bfc);
  347. _FREQ_TABLE(pfc);
  348. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  349. /*
  350. * The ST40 divisors are totally different so we set the cpu data
  351. * clocks using a different algorithm
  352. *
  353. * I've just plugged this from the 2.4 code
  354. * - Alex Bennee <kernel-hacker@bennee.com>
  355. */
  356. #define CCN_PVR_CHIP_SHIFT 24
  357. #define CCN_PVR_CHIP_MASK 0xff
  358. #define CCN_PVR_CHIP_ST40STB1 0x4
  359. struct frqcr_data {
  360. unsigned short frqcr;
  361. struct {
  362. unsigned char multiplier;
  363. unsigned char divisor;
  364. } factor[3];
  365. };
  366. static struct frqcr_data st40_frqcr_table[] = {
  367. { 0x000, {{1,1}, {1,1}, {1,2}}},
  368. { 0x002, {{1,1}, {1,1}, {1,4}}},
  369. { 0x004, {{1,1}, {1,1}, {1,8}}},
  370. { 0x008, {{1,1}, {1,2}, {1,2}}},
  371. { 0x00A, {{1,1}, {1,2}, {1,4}}},
  372. { 0x00C, {{1,1}, {1,2}, {1,8}}},
  373. { 0x011, {{1,1}, {2,3}, {1,6}}},
  374. { 0x013, {{1,1}, {2,3}, {1,3}}},
  375. { 0x01A, {{1,1}, {1,2}, {1,4}}},
  376. { 0x01C, {{1,1}, {1,2}, {1,8}}},
  377. { 0x023, {{1,1}, {2,3}, {1,3}}},
  378. { 0x02C, {{1,1}, {1,2}, {1,8}}},
  379. { 0x048, {{1,2}, {1,2}, {1,4}}},
  380. { 0x04A, {{1,2}, {1,2}, {1,6}}},
  381. { 0x04C, {{1,2}, {1,2}, {1,8}}},
  382. { 0x05A, {{1,2}, {1,3}, {1,6}}},
  383. { 0x05C, {{1,2}, {1,3}, {1,6}}},
  384. { 0x063, {{1,2}, {1,4}, {1,4}}},
  385. { 0x06C, {{1,2}, {1,4}, {1,8}}},
  386. { 0x091, {{1,3}, {1,3}, {1,6}}},
  387. { 0x093, {{1,3}, {1,3}, {1,6}}},
  388. { 0x0A3, {{1,3}, {1,6}, {1,6}}},
  389. { 0x0DA, {{1,4}, {1,4}, {1,8}}},
  390. { 0x0DC, {{1,4}, {1,4}, {1,8}}},
  391. { 0x0EC, {{1,4}, {1,8}, {1,8}}},
  392. { 0x123, {{1,4}, {1,4}, {1,8}}},
  393. { 0x16C, {{1,4}, {1,8}, {1,8}}},
  394. };
  395. struct memclk_data {
  396. unsigned char multiplier;
  397. unsigned char divisor;
  398. };
  399. static struct memclk_data st40_memclk_table[8] = {
  400. {1,1}, // 000
  401. {1,2}, // 001
  402. {1,3}, // 010
  403. {2,3}, // 011
  404. {1,4}, // 100
  405. {1,6}, // 101
  406. {1,8}, // 110
  407. {1,8} // 111
  408. };
  409. static void st40_specific_time_init(unsigned int module_clock, unsigned short frqcr)
  410. {
  411. unsigned int cpu_clock, master_clock, bus_clock, memory_clock;
  412. struct frqcr_data *d;
  413. int a;
  414. unsigned long memclkcr;
  415. struct memclk_data *e;
  416. for (a = 0; a < ARRAY_SIZE(st40_frqcr_table); a++) {
  417. d = &st40_frqcr_table[a];
  418. if (d->frqcr == (frqcr & 0x1ff))
  419. break;
  420. }
  421. if (a == ARRAY_SIZE(st40_frqcr_table)) {
  422. d = st40_frqcr_table;
  423. printk("ERROR: Unrecognised FRQCR value (0x%x), "
  424. "using default multipliers\n", frqcr);
  425. }
  426. memclkcr = ctrl_inl(CLOCKGEN_MEMCLKCR);
  427. e = &st40_memclk_table[memclkcr & MEMCLKCR_RATIO_MASK];
  428. printk(KERN_INFO "Clock multipliers: CPU: %d/%d Bus: %d/%d "
  429. "Mem: %d/%d Periph: %d/%d\n",
  430. d->factor[0].multiplier, d->factor[0].divisor,
  431. d->factor[1].multiplier, d->factor[1].divisor,
  432. e->multiplier, e->divisor,
  433. d->factor[2].multiplier, d->factor[2].divisor);
  434. master_clock = module_clock * d->factor[2].divisor
  435. / d->factor[2].multiplier;
  436. bus_clock = master_clock * d->factor[1].multiplier
  437. / d->factor[1].divisor;
  438. memory_clock = master_clock * e->multiplier
  439. / e->divisor;
  440. cpu_clock = master_clock * d->factor[0].multiplier
  441. / d->factor[0].divisor;
  442. current_cpu_data.cpu_clock = cpu_clock;
  443. current_cpu_data.master_clock = master_clock;
  444. current_cpu_data.bus_clock = bus_clock;
  445. current_cpu_data.memory_clock = memory_clock;
  446. current_cpu_data.module_clock = module_clock;
  447. }
  448. #endif
  449. void __init time_init(void)
  450. {
  451. unsigned int timer_freq = 0;
  452. unsigned int ifc, pfc, bfc;
  453. unsigned long interval;
  454. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  455. unsigned long pvr;
  456. unsigned short frqcr;
  457. #endif
  458. if (board_time_init)
  459. board_time_init();
  460. /*
  461. * If we don't have an RTC (such as with the SH7300), don't attempt to
  462. * probe the timer frequency. Rely on an either hardcoded peripheral
  463. * clock value, or on the sh_pclk command line option. Note that we
  464. * still need to have CONFIG_SH_PCLK_FREQ set in order for things like
  465. * CLOCK_TICK_RATE to be sane.
  466. */
  467. current_cpu_data.module_clock = sh_pclk_freq;
  468. #ifdef CONFIG_SH_PCLK_CALC
  469. /* XXX: Switch this over to a more generic test. */
  470. {
  471. unsigned int freq;
  472. /*
  473. * If we've specified a peripheral clock frequency, and we have
  474. * an RTC, compare it against the autodetected value. Complain
  475. * if there's a mismatch.
  476. */
  477. timer_freq = get_timer_frequency();
  478. freq = timer_freq * 4;
  479. if (sh_pclk_freq && (sh_pclk_freq/100*99 > freq || sh_pclk_freq/100*101 < freq)) {
  480. printk(KERN_NOTICE "Calculated peripheral clock value "
  481. "%d differs from sh_pclk value %d, fixing..\n",
  482. freq, sh_pclk_freq);
  483. current_cpu_data.module_clock = freq;
  484. }
  485. }
  486. #endif
  487. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  488. /* XXX: Update ST40 code to use board_time_init() */
  489. pvr = ctrl_inl(CCN_PVR);
  490. frqcr = ctrl_inw(FRQCR);
  491. printk("time.c ST40 Probe: PVR %08lx, FRQCR %04hx\n", pvr, frqcr);
  492. if (((pvr >> CCN_PVR_CHIP_SHIFT) & CCN_PVR_CHIP_MASK) == CCN_PVR_CHIP_ST40STB1)
  493. st40_specific_time_init(current_cpu_data.module_clock, frqcr);
  494. else
  495. #endif
  496. get_current_frequency_divisors(&ifc, &bfc, &pfc);
  497. if (rtc_get_time) {
  498. rtc_get_time(&xtime);
  499. } else {
  500. xtime.tv_sec = mktime(2000, 1, 1, 0, 0, 0);
  501. xtime.tv_nsec = 0;
  502. }
  503. set_normalized_timespec(&wall_to_monotonic,
  504. -xtime.tv_sec, -xtime.tv_nsec);
  505. if (board_timer_setup) {
  506. board_timer_setup(&irq0);
  507. } else {
  508. setup_irq(TIMER_IRQ, &irq0);
  509. }
  510. /*
  511. * for ST40 chips the current_cpu_data should already be set
  512. * so not having valid pfc/bfc/ifc shouldn't be a problem
  513. */
  514. if (!current_cpu_data.master_clock)
  515. current_cpu_data.master_clock = current_cpu_data.module_clock * pfc;
  516. if (!current_cpu_data.bus_clock)
  517. current_cpu_data.bus_clock = current_cpu_data.master_clock / bfc;
  518. if (!current_cpu_data.cpu_clock)
  519. current_cpu_data.cpu_clock = current_cpu_data.master_clock / ifc;
  520. printk("CPU clock: %d.%02dMHz\n",
  521. (current_cpu_data.cpu_clock / 1000000),
  522. (current_cpu_data.cpu_clock % 1000000)/10000);
  523. printk("Bus clock: %d.%02dMHz\n",
  524. (current_cpu_data.bus_clock / 1000000),
  525. (current_cpu_data.bus_clock % 1000000)/10000);
  526. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  527. printk("Memory clock: %d.%02dMHz\n",
  528. (current_cpu_data.memory_clock / 1000000),
  529. (current_cpu_data.memory_clock % 1000000)/10000);
  530. #endif
  531. printk("Module clock: %d.%02dMHz\n",
  532. (current_cpu_data.module_clock / 1000000),
  533. (current_cpu_data.module_clock % 1000000)/10000);
  534. interval = (current_cpu_data.module_clock/4 + HZ/2) / HZ;
  535. printk("Interval = %ld\n", interval);
  536. /* Start TMU0 */
  537. ctrl_outb(0, TMU_TSTR);
  538. #if !defined(CONFIG_CPU_SUBTYPE_SH7300)
  539. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  540. #endif
  541. ctrl_outw(TMU0_TCR_INIT, TMU0_TCR);
  542. ctrl_outl(interval, TMU0_TCOR);
  543. ctrl_outl(interval, TMU0_TCNT);
  544. ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
  545. #if defined(CONFIG_SH_KGDB)
  546. /*
  547. * Set up kgdb as requested. We do it here because the serial
  548. * init uses the timer vars we just set up for figuring baud.
  549. */
  550. kgdb_init();
  551. #endif
  552. }