time.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. time_adjust = 0; /* stop active adjtime() */
  183. time_status |= STA_UNSYNC;
  184. time_maxerror = NTP_PHASE_LIMIT;
  185. time_esterror = NTP_PHASE_LIMIT;
  186. write_sequnlock_irq(&xtime_lock);
  187. clock_was_set();
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(do_settimeofday);
  191. /* last time the RTC clock got updated */
  192. static long last_rtc_update;
  193. /*
  194. * timer_interrupt() needs to keep up the real-time clock,
  195. * as well as call the "do_timer()" routine every clocktick
  196. */
  197. static inline void do_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  198. {
  199. do_timer(regs);
  200. #ifndef CONFIG_SMP
  201. update_process_times(user_mode(regs));
  202. #endif
  203. profile_tick(CPU_PROFILING, regs);
  204. #ifdef CONFIG_HEARTBEAT
  205. if (sh_mv.mv_heartbeat != NULL)
  206. sh_mv.mv_heartbeat();
  207. #endif
  208. /*
  209. * If we have an externally synchronized Linux clock, then update
  210. * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  211. * called as close as possible to 500 ms before the new second starts.
  212. */
  213. if ((time_status & STA_UNSYNC) == 0 &&
  214. xtime.tv_sec > last_rtc_update + 660 &&
  215. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  216. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  217. if (rtc_set_time(xtime.tv_sec) == 0)
  218. last_rtc_update = xtime.tv_sec;
  219. else
  220. last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
  221. }
  222. }
  223. /*
  224. * This is the same as the above, except we _also_ save the current
  225. * Time Stamp Counter value at the time of the timer interrupt, so that
  226. * we later on can estimate the time of day more exactly.
  227. */
  228. static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  229. {
  230. unsigned long timer_status;
  231. /* Clear UNF bit */
  232. timer_status = ctrl_inw(TMU0_TCR);
  233. timer_status &= ~0x100;
  234. ctrl_outw(timer_status, TMU0_TCR);
  235. /*
  236. * Here we are in the timer irq handler. We just have irqs locally
  237. * disabled but we don't know if the timer_bh is running on the other
  238. * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
  239. * the irq version of write_lock because as just said we have irq
  240. * locally disabled. -arca
  241. */
  242. write_seqlock(&xtime_lock);
  243. do_timer_interrupt(irq, NULL, regs);
  244. write_sequnlock(&xtime_lock);
  245. return IRQ_HANDLED;
  246. }
  247. /*
  248. * Hah! We'll see if this works (switching from usecs to nsecs).
  249. */
  250. static unsigned int __init get_timer_frequency(void)
  251. {
  252. u32 freq;
  253. struct timespec ts1, ts2;
  254. unsigned long diff_nsec;
  255. unsigned long factor;
  256. /* Setup the timer: We don't want to generate interrupts, just
  257. * have it count down at its natural rate.
  258. */
  259. ctrl_outb(0, TMU_TSTR);
  260. #if !defined(CONFIG_CPU_SUBTYPE_SH7300)
  261. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  262. #endif
  263. ctrl_outw(TMU0_TCR_CALIB, TMU0_TCR);
  264. ctrl_outl(0xffffffff, TMU0_TCOR);
  265. ctrl_outl(0xffffffff, TMU0_TCNT);
  266. rtc_get_time(&ts2);
  267. do {
  268. rtc_get_time(&ts1);
  269. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  270. /* actually start the timer */
  271. ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
  272. do {
  273. rtc_get_time(&ts2);
  274. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  275. freq = 0xffffffff - ctrl_inl(TMU0_TCNT);
  276. if (ts2.tv_nsec < ts1.tv_nsec) {
  277. ts2.tv_nsec += 1000000000;
  278. ts2.tv_sec--;
  279. }
  280. diff_nsec = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
  281. /* this should work well if the RTC has a precision of n Hz, where
  282. * n is an integer. I don't think we have to worry about the other
  283. * cases. */
  284. factor = (1000000000 + diff_nsec/2) / diff_nsec;
  285. if (factor * diff_nsec > 1100000000 ||
  286. factor * diff_nsec < 900000000)
  287. panic("weird RTC (diff_nsec %ld)", diff_nsec);
  288. return freq * factor;
  289. }
  290. void (*board_time_init)(void);
  291. void (*board_timer_setup)(struct irqaction *irq);
  292. static unsigned int sh_pclk_freq __initdata = CONFIG_SH_PCLK_FREQ;
  293. static int __init sh_pclk_setup(char *str)
  294. {
  295. unsigned int freq;
  296. if (get_option(&str, &freq))
  297. sh_pclk_freq = freq;
  298. return 1;
  299. }
  300. __setup("sh_pclk=", sh_pclk_setup);
  301. static struct irqaction irq0 = { timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer", NULL, NULL};
  302. void get_current_frequency_divisors(unsigned int *ifc, unsigned int *bfc, unsigned int *pfc)
  303. {
  304. unsigned int frqcr = ctrl_inw(FRQCR);
  305. #if defined(CONFIG_CPU_SH3)
  306. #if defined(CONFIG_CPU_SUBTYPE_SH7300)
  307. *ifc = md_table[((frqcr & 0x0070) >> 4)];
  308. *bfc = md_table[((frqcr & 0x0700) >> 8)];
  309. *pfc = md_table[frqcr & 0x0007];
  310. #elif defined(CONFIG_CPU_SUBTYPE_SH7705)
  311. *bfc = stc_multipliers[(frqcr & 0x0300) >> 8];
  312. *ifc = ifc_divisors[(frqcr & 0x0030) >> 4];
  313. *pfc = pfc_divisors[frqcr & 0x0003];
  314. #else
  315. unsigned int tmp;
  316. tmp = (frqcr & 0x8000) >> 13;
  317. tmp |= (frqcr & 0x0030) >> 4;
  318. *bfc = stc_multipliers[tmp];
  319. tmp = (frqcr & 0x4000) >> 12;
  320. tmp |= (frqcr & 0x000c) >> 2;
  321. *ifc = ifc_divisors[tmp];
  322. tmp = (frqcr & 0x2000) >> 11;
  323. tmp |= frqcr & 0x0003;
  324. *pfc = pfc_divisors[tmp];
  325. #endif
  326. #elif defined(CONFIG_CPU_SH4)
  327. #if defined(CONFIG_CPU_SUBTYPE_SH73180)
  328. *ifc = ifc_divisors[(frqcr>> 20) & 0x0007];
  329. *bfc = bfc_divisors[(frqcr>> 12) & 0x0007];
  330. *pfc = pfc_divisors[frqcr & 0x0007];
  331. #else
  332. *ifc = ifc_divisors[(frqcr >> 6) & 0x0007];
  333. *bfc = bfc_divisors[(frqcr >> 3) & 0x0007];
  334. *pfc = pfc_divisors[frqcr & 0x0007];
  335. #endif
  336. #endif
  337. }
  338. /*
  339. * This bit of ugliness builds up accessor routines to get at both
  340. * the divisors and the physical values.
  341. */
  342. #define _FREQ_TABLE(x) \
  343. unsigned int get_##x##_divisor(unsigned int value) \
  344. { return x##_divisors[value]; } \
  345. \
  346. unsigned int get_##x##_value(unsigned int divisor) \
  347. { return x##_values[(divisor - 1)]; }
  348. _FREQ_TABLE(ifc);
  349. _FREQ_TABLE(bfc);
  350. _FREQ_TABLE(pfc);
  351. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  352. /*
  353. * The ST40 divisors are totally different so we set the cpu data
  354. * clocks using a different algorithm
  355. *
  356. * I've just plugged this from the 2.4 code
  357. * - Alex Bennee <kernel-hacker@bennee.com>
  358. */
  359. #define CCN_PVR_CHIP_SHIFT 24
  360. #define CCN_PVR_CHIP_MASK 0xff
  361. #define CCN_PVR_CHIP_ST40STB1 0x4
  362. struct frqcr_data {
  363. unsigned short frqcr;
  364. struct {
  365. unsigned char multiplier;
  366. unsigned char divisor;
  367. } factor[3];
  368. };
  369. static struct frqcr_data st40_frqcr_table[] = {
  370. { 0x000, {{1,1}, {1,1}, {1,2}}},
  371. { 0x002, {{1,1}, {1,1}, {1,4}}},
  372. { 0x004, {{1,1}, {1,1}, {1,8}}},
  373. { 0x008, {{1,1}, {1,2}, {1,2}}},
  374. { 0x00A, {{1,1}, {1,2}, {1,4}}},
  375. { 0x00C, {{1,1}, {1,2}, {1,8}}},
  376. { 0x011, {{1,1}, {2,3}, {1,6}}},
  377. { 0x013, {{1,1}, {2,3}, {1,3}}},
  378. { 0x01A, {{1,1}, {1,2}, {1,4}}},
  379. { 0x01C, {{1,1}, {1,2}, {1,8}}},
  380. { 0x023, {{1,1}, {2,3}, {1,3}}},
  381. { 0x02C, {{1,1}, {1,2}, {1,8}}},
  382. { 0x048, {{1,2}, {1,2}, {1,4}}},
  383. { 0x04A, {{1,2}, {1,2}, {1,6}}},
  384. { 0x04C, {{1,2}, {1,2}, {1,8}}},
  385. { 0x05A, {{1,2}, {1,3}, {1,6}}},
  386. { 0x05C, {{1,2}, {1,3}, {1,6}}},
  387. { 0x063, {{1,2}, {1,4}, {1,4}}},
  388. { 0x06C, {{1,2}, {1,4}, {1,8}}},
  389. { 0x091, {{1,3}, {1,3}, {1,6}}},
  390. { 0x093, {{1,3}, {1,3}, {1,6}}},
  391. { 0x0A3, {{1,3}, {1,6}, {1,6}}},
  392. { 0x0DA, {{1,4}, {1,4}, {1,8}}},
  393. { 0x0DC, {{1,4}, {1,4}, {1,8}}},
  394. { 0x0EC, {{1,4}, {1,8}, {1,8}}},
  395. { 0x123, {{1,4}, {1,4}, {1,8}}},
  396. { 0x16C, {{1,4}, {1,8}, {1,8}}},
  397. };
  398. struct memclk_data {
  399. unsigned char multiplier;
  400. unsigned char divisor;
  401. };
  402. static struct memclk_data st40_memclk_table[8] = {
  403. {1,1}, // 000
  404. {1,2}, // 001
  405. {1,3}, // 010
  406. {2,3}, // 011
  407. {1,4}, // 100
  408. {1,6}, // 101
  409. {1,8}, // 110
  410. {1,8} // 111
  411. };
  412. static void st40_specific_time_init(unsigned int module_clock, unsigned short frqcr)
  413. {
  414. unsigned int cpu_clock, master_clock, bus_clock, memory_clock;
  415. struct frqcr_data *d;
  416. int a;
  417. unsigned long memclkcr;
  418. struct memclk_data *e;
  419. for (a = 0; a < ARRAY_SIZE(st40_frqcr_table); a++) {
  420. d = &st40_frqcr_table[a];
  421. if (d->frqcr == (frqcr & 0x1ff))
  422. break;
  423. }
  424. if (a == ARRAY_SIZE(st40_frqcr_table)) {
  425. d = st40_frqcr_table;
  426. printk("ERROR: Unrecognised FRQCR value (0x%x), "
  427. "using default multipliers\n", frqcr);
  428. }
  429. memclkcr = ctrl_inl(CLOCKGEN_MEMCLKCR);
  430. e = &st40_memclk_table[memclkcr & MEMCLKCR_RATIO_MASK];
  431. printk(KERN_INFO "Clock multipliers: CPU: %d/%d Bus: %d/%d "
  432. "Mem: %d/%d Periph: %d/%d\n",
  433. d->factor[0].multiplier, d->factor[0].divisor,
  434. d->factor[1].multiplier, d->factor[1].divisor,
  435. e->multiplier, e->divisor,
  436. d->factor[2].multiplier, d->factor[2].divisor);
  437. master_clock = module_clock * d->factor[2].divisor
  438. / d->factor[2].multiplier;
  439. bus_clock = master_clock * d->factor[1].multiplier
  440. / d->factor[1].divisor;
  441. memory_clock = master_clock * e->multiplier
  442. / e->divisor;
  443. cpu_clock = master_clock * d->factor[0].multiplier
  444. / d->factor[0].divisor;
  445. current_cpu_data.cpu_clock = cpu_clock;
  446. current_cpu_data.master_clock = master_clock;
  447. current_cpu_data.bus_clock = bus_clock;
  448. current_cpu_data.memory_clock = memory_clock;
  449. current_cpu_data.module_clock = module_clock;
  450. }
  451. #endif
  452. void __init time_init(void)
  453. {
  454. unsigned int timer_freq = 0;
  455. unsigned int ifc, pfc, bfc;
  456. unsigned long interval;
  457. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  458. unsigned long pvr;
  459. unsigned short frqcr;
  460. #endif
  461. if (board_time_init)
  462. board_time_init();
  463. /*
  464. * If we don't have an RTC (such as with the SH7300), don't attempt to
  465. * probe the timer frequency. Rely on an either hardcoded peripheral
  466. * clock value, or on the sh_pclk command line option. Note that we
  467. * still need to have CONFIG_SH_PCLK_FREQ set in order for things like
  468. * CLOCK_TICK_RATE to be sane.
  469. */
  470. current_cpu_data.module_clock = sh_pclk_freq;
  471. #ifdef CONFIG_SH_PCLK_CALC
  472. /* XXX: Switch this over to a more generic test. */
  473. {
  474. unsigned int freq;
  475. /*
  476. * If we've specified a peripheral clock frequency, and we have
  477. * an RTC, compare it against the autodetected value. Complain
  478. * if there's a mismatch.
  479. */
  480. timer_freq = get_timer_frequency();
  481. freq = timer_freq * 4;
  482. if (sh_pclk_freq && (sh_pclk_freq/100*99 > freq || sh_pclk_freq/100*101 < freq)) {
  483. printk(KERN_NOTICE "Calculated peripheral clock value "
  484. "%d differs from sh_pclk value %d, fixing..\n",
  485. freq, sh_pclk_freq);
  486. current_cpu_data.module_clock = freq;
  487. }
  488. }
  489. #endif
  490. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  491. /* XXX: Update ST40 code to use board_time_init() */
  492. pvr = ctrl_inl(CCN_PVR);
  493. frqcr = ctrl_inw(FRQCR);
  494. printk("time.c ST40 Probe: PVR %08lx, FRQCR %04hx\n", pvr, frqcr);
  495. if (((pvr >> CCN_PVR_CHIP_SHIFT) & CCN_PVR_CHIP_MASK) == CCN_PVR_CHIP_ST40STB1)
  496. st40_specific_time_init(current_cpu_data.module_clock, frqcr);
  497. else
  498. #endif
  499. get_current_frequency_divisors(&ifc, &bfc, &pfc);
  500. if (rtc_get_time) {
  501. rtc_get_time(&xtime);
  502. } else {
  503. xtime.tv_sec = mktime(2000, 1, 1, 0, 0, 0);
  504. xtime.tv_nsec = 0;
  505. }
  506. set_normalized_timespec(&wall_to_monotonic,
  507. -xtime.tv_sec, -xtime.tv_nsec);
  508. if (board_timer_setup) {
  509. board_timer_setup(&irq0);
  510. } else {
  511. setup_irq(TIMER_IRQ, &irq0);
  512. }
  513. /*
  514. * for ST40 chips the current_cpu_data should already be set
  515. * so not having valid pfc/bfc/ifc shouldn't be a problem
  516. */
  517. if (!current_cpu_data.master_clock)
  518. current_cpu_data.master_clock = current_cpu_data.module_clock * pfc;
  519. if (!current_cpu_data.bus_clock)
  520. current_cpu_data.bus_clock = current_cpu_data.master_clock / bfc;
  521. if (!current_cpu_data.cpu_clock)
  522. current_cpu_data.cpu_clock = current_cpu_data.master_clock / ifc;
  523. printk("CPU clock: %d.%02dMHz\n",
  524. (current_cpu_data.cpu_clock / 1000000),
  525. (current_cpu_data.cpu_clock % 1000000)/10000);
  526. printk("Bus clock: %d.%02dMHz\n",
  527. (current_cpu_data.bus_clock / 1000000),
  528. (current_cpu_data.bus_clock % 1000000)/10000);
  529. #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
  530. printk("Memory clock: %d.%02dMHz\n",
  531. (current_cpu_data.memory_clock / 1000000),
  532. (current_cpu_data.memory_clock % 1000000)/10000);
  533. #endif
  534. printk("Module clock: %d.%02dMHz\n",
  535. (current_cpu_data.module_clock / 1000000),
  536. (current_cpu_data.module_clock % 1000000)/10000);
  537. interval = (current_cpu_data.module_clock/4 + HZ/2) / HZ;
  538. printk("Interval = %ld\n", interval);
  539. /* Start TMU0 */
  540. ctrl_outb(0, TMU_TSTR);
  541. #if !defined(CONFIG_CPU_SUBTYPE_SH7300)
  542. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  543. #endif
  544. ctrl_outw(TMU0_TCR_INIT, TMU0_TCR);
  545. ctrl_outl(interval, TMU0_TCOR);
  546. ctrl_outl(interval, TMU0_TCNT);
  547. ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
  548. #if defined(CONFIG_SH_KGDB)
  549. /*
  550. * Set up kgdb as requested. We do it here because the serial
  551. * init uses the timer vars we just set up for figuring baud.
  552. */
  553. kgdb_init();
  554. #endif
  555. }