time.c 17 KB

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