time.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /* $Id: time.c,v 1.42 2002/01/23 14:33:55 davem Exp $
  2. * time.c: UltraSparc timer and TOD clock support.
  3. *
  4. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  6. *
  7. * Based largely on code which is:
  8. *
  9. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  10. */
  11. #include <linux/config.h>
  12. #include <linux/errno.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/param.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/time.h>
  21. #include <linux/timex.h>
  22. #include <linux/init.h>
  23. #include <linux/ioport.h>
  24. #include <linux/mc146818rtc.h>
  25. #include <linux/delay.h>
  26. #include <linux/profile.h>
  27. #include <linux/bcd.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/cpufreq.h>
  30. #include <linux/percpu.h>
  31. #include <linux/profile.h>
  32. #include <asm/oplib.h>
  33. #include <asm/mostek.h>
  34. #include <asm/timer.h>
  35. #include <asm/irq.h>
  36. #include <asm/io.h>
  37. #include <asm/sbus.h>
  38. #include <asm/fhc.h>
  39. #include <asm/pbm.h>
  40. #include <asm/ebus.h>
  41. #include <asm/isa.h>
  42. #include <asm/starfire.h>
  43. #include <asm/smp.h>
  44. #include <asm/sections.h>
  45. #include <asm/cpudata.h>
  46. DEFINE_SPINLOCK(mostek_lock);
  47. DEFINE_SPINLOCK(rtc_lock);
  48. void __iomem *mstk48t02_regs = NULL;
  49. #ifdef CONFIG_PCI
  50. unsigned long ds1287_regs = 0UL;
  51. #endif
  52. extern unsigned long wall_jiffies;
  53. u64 jiffies_64 = INITIAL_JIFFIES;
  54. EXPORT_SYMBOL(jiffies_64);
  55. static void __iomem *mstk48t08_regs;
  56. static void __iomem *mstk48t59_regs;
  57. static int set_rtc_mmss(unsigned long);
  58. static __init unsigned long dummy_get_tick(void)
  59. {
  60. return 0;
  61. }
  62. static __initdata struct sparc64_tick_ops dummy_tick_ops = {
  63. .get_tick = dummy_get_tick,
  64. };
  65. struct sparc64_tick_ops *tick_ops __read_mostly = &dummy_tick_ops;
  66. #define TICK_PRIV_BIT (1UL << 63)
  67. #ifdef CONFIG_SMP
  68. unsigned long profile_pc(struct pt_regs *regs)
  69. {
  70. unsigned long pc = instruction_pointer(regs);
  71. if (in_lock_functions(pc))
  72. return regs->u_regs[UREG_RETPC];
  73. return pc;
  74. }
  75. EXPORT_SYMBOL(profile_pc);
  76. #endif
  77. static void tick_disable_protection(void)
  78. {
  79. /* Set things up so user can access tick register for profiling
  80. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  81. * read back of %tick after writing it.
  82. */
  83. __asm__ __volatile__(
  84. " ba,pt %%xcc, 1f\n"
  85. " nop\n"
  86. " .align 64\n"
  87. "1: rd %%tick, %%g2\n"
  88. " add %%g2, 6, %%g2\n"
  89. " andn %%g2, %0, %%g2\n"
  90. " wrpr %%g2, 0, %%tick\n"
  91. " rdpr %%tick, %%g0"
  92. : /* no outputs */
  93. : "r" (TICK_PRIV_BIT)
  94. : "g2");
  95. }
  96. static void tick_init_tick(unsigned long offset)
  97. {
  98. tick_disable_protection();
  99. __asm__ __volatile__(
  100. " rd %%tick, %%g1\n"
  101. " andn %%g1, %1, %%g1\n"
  102. " ba,pt %%xcc, 1f\n"
  103. " add %%g1, %0, %%g1\n"
  104. " .align 64\n"
  105. "1: wr %%g1, 0x0, %%tick_cmpr\n"
  106. " rd %%tick_cmpr, %%g0"
  107. : /* no outputs */
  108. : "r" (offset), "r" (TICK_PRIV_BIT)
  109. : "g1");
  110. }
  111. static unsigned long tick_get_tick(void)
  112. {
  113. unsigned long ret;
  114. __asm__ __volatile__("rd %%tick, %0\n\t"
  115. "mov %0, %0"
  116. : "=r" (ret));
  117. return ret & ~TICK_PRIV_BIT;
  118. }
  119. static unsigned long tick_get_compare(void)
  120. {
  121. unsigned long ret;
  122. __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
  123. "mov %0, %0"
  124. : "=r" (ret));
  125. return ret;
  126. }
  127. static unsigned long tick_add_compare(unsigned long adj)
  128. {
  129. unsigned long new_compare;
  130. /* Workaround for Spitfire Errata (#54 I think??), I discovered
  131. * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
  132. * number 103640.
  133. *
  134. * On Blackbird writes to %tick_cmpr can fail, the
  135. * workaround seems to be to execute the wr instruction
  136. * at the start of an I-cache line, and perform a dummy
  137. * read back from %tick_cmpr right after writing to it. -DaveM
  138. */
  139. __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
  140. "ba,pt %%xcc, 1f\n\t"
  141. " add %0, %1, %0\n\t"
  142. ".align 64\n"
  143. "1:\n\t"
  144. "wr %0, 0, %%tick_cmpr\n\t"
  145. "rd %%tick_cmpr, %%g0"
  146. : "=&r" (new_compare)
  147. : "r" (adj));
  148. return new_compare;
  149. }
  150. static unsigned long tick_add_tick(unsigned long adj, unsigned long offset)
  151. {
  152. unsigned long new_tick, tmp;
  153. /* Also need to handle Blackbird bug here too. */
  154. __asm__ __volatile__("rd %%tick, %0\n\t"
  155. "add %0, %2, %0\n\t"
  156. "wrpr %0, 0, %%tick\n\t"
  157. "andn %0, %4, %1\n\t"
  158. "ba,pt %%xcc, 1f\n\t"
  159. " add %1, %3, %1\n\t"
  160. ".align 64\n"
  161. "1:\n\t"
  162. "wr %1, 0, %%tick_cmpr\n\t"
  163. "rd %%tick_cmpr, %%g0"
  164. : "=&r" (new_tick), "=&r" (tmp)
  165. : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
  166. return new_tick;
  167. }
  168. static struct sparc64_tick_ops tick_operations __read_mostly = {
  169. .init_tick = tick_init_tick,
  170. .get_tick = tick_get_tick,
  171. .get_compare = tick_get_compare,
  172. .add_tick = tick_add_tick,
  173. .add_compare = tick_add_compare,
  174. .softint_mask = 1UL << 0,
  175. };
  176. static void stick_init_tick(unsigned long offset)
  177. {
  178. tick_disable_protection();
  179. /* Let the user get at STICK too. */
  180. __asm__ __volatile__(
  181. " rd %%asr24, %%g2\n"
  182. " andn %%g2, %0, %%g2\n"
  183. " wr %%g2, 0, %%asr24"
  184. : /* no outputs */
  185. : "r" (TICK_PRIV_BIT)
  186. : "g1", "g2");
  187. __asm__ __volatile__(
  188. " rd %%asr24, %%g1\n"
  189. " andn %%g1, %1, %%g1\n"
  190. " add %%g1, %0, %%g1\n"
  191. " wr %%g1, 0x0, %%asr25"
  192. : /* no outputs */
  193. : "r" (offset), "r" (TICK_PRIV_BIT)
  194. : "g1");
  195. }
  196. static unsigned long stick_get_tick(void)
  197. {
  198. unsigned long ret;
  199. __asm__ __volatile__("rd %%asr24, %0"
  200. : "=r" (ret));
  201. return ret & ~TICK_PRIV_BIT;
  202. }
  203. static unsigned long stick_get_compare(void)
  204. {
  205. unsigned long ret;
  206. __asm__ __volatile__("rd %%asr25, %0"
  207. : "=r" (ret));
  208. return ret;
  209. }
  210. static unsigned long stick_add_tick(unsigned long adj, unsigned long offset)
  211. {
  212. unsigned long new_tick, tmp;
  213. __asm__ __volatile__("rd %%asr24, %0\n\t"
  214. "add %0, %2, %0\n\t"
  215. "wr %0, 0, %%asr24\n\t"
  216. "andn %0, %4, %1\n\t"
  217. "add %1, %3, %1\n\t"
  218. "wr %1, 0, %%asr25"
  219. : "=&r" (new_tick), "=&r" (tmp)
  220. : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
  221. return new_tick;
  222. }
  223. static unsigned long stick_add_compare(unsigned long adj)
  224. {
  225. unsigned long new_compare;
  226. __asm__ __volatile__("rd %%asr25, %0\n\t"
  227. "add %0, %1, %0\n\t"
  228. "wr %0, 0, %%asr25"
  229. : "=&r" (new_compare)
  230. : "r" (adj));
  231. return new_compare;
  232. }
  233. static struct sparc64_tick_ops stick_operations __read_mostly = {
  234. .init_tick = stick_init_tick,
  235. .get_tick = stick_get_tick,
  236. .get_compare = stick_get_compare,
  237. .add_tick = stick_add_tick,
  238. .add_compare = stick_add_compare,
  239. .softint_mask = 1UL << 16,
  240. };
  241. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  242. * in I/O space. There are two 64-bit registers each, the
  243. * first holds the low 32-bits of the value and the second holds
  244. * the high 32-bits.
  245. *
  246. * Since STICK is constantly updating, we have to access it carefully.
  247. *
  248. * The sequence we use to read is:
  249. * 1) read low
  250. * 2) read high
  251. * 3) read low again, if it rolled over increment high by 1
  252. *
  253. * Writing STICK safely is also tricky:
  254. * 1) write low to zero
  255. * 2) write high
  256. * 3) write low
  257. */
  258. #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
  259. #define HBIRD_STICK_ADDR 0x1fe0000f070UL
  260. static unsigned long __hbird_read_stick(void)
  261. {
  262. unsigned long ret, tmp1, tmp2, tmp3;
  263. unsigned long addr = HBIRD_STICK_ADDR;
  264. __asm__ __volatile__("ldxa [%1] %5, %2\n\t"
  265. "add %1, 0x8, %1\n\t"
  266. "ldxa [%1] %5, %3\n\t"
  267. "sub %1, 0x8, %1\n\t"
  268. "ldxa [%1] %5, %4\n\t"
  269. "cmp %4, %2\n\t"
  270. "blu,a,pn %%xcc, 1f\n\t"
  271. " add %3, 1, %3\n"
  272. "1:\n\t"
  273. "sllx %3, 32, %3\n\t"
  274. "or %3, %4, %0\n\t"
  275. : "=&r" (ret), "=&r" (addr),
  276. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  277. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  278. return ret;
  279. }
  280. static unsigned long __hbird_read_compare(void)
  281. {
  282. unsigned long low, high;
  283. unsigned long addr = HBIRD_STICKCMP_ADDR;
  284. __asm__ __volatile__("ldxa [%2] %3, %0\n\t"
  285. "add %2, 0x8, %2\n\t"
  286. "ldxa [%2] %3, %1"
  287. : "=&r" (low), "=&r" (high), "=&r" (addr)
  288. : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr));
  289. return (high << 32UL) | low;
  290. }
  291. static void __hbird_write_stick(unsigned long val)
  292. {
  293. unsigned long low = (val & 0xffffffffUL);
  294. unsigned long high = (val >> 32UL);
  295. unsigned long addr = HBIRD_STICK_ADDR;
  296. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  297. "add %0, 0x8, %0\n\t"
  298. "stxa %3, [%0] %4\n\t"
  299. "sub %0, 0x8, %0\n\t"
  300. "stxa %2, [%0] %4"
  301. : "=&r" (addr)
  302. : "0" (addr), "r" (low), "r" (high),
  303. "i" (ASI_PHYS_BYPASS_EC_E));
  304. }
  305. static void __hbird_write_compare(unsigned long val)
  306. {
  307. unsigned long low = (val & 0xffffffffUL);
  308. unsigned long high = (val >> 32UL);
  309. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  310. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  311. "sub %0, 0x8, %0\n\t"
  312. "stxa %2, [%0] %4"
  313. : "=&r" (addr)
  314. : "0" (addr), "r" (low), "r" (high),
  315. "i" (ASI_PHYS_BYPASS_EC_E));
  316. }
  317. static void hbtick_init_tick(unsigned long offset)
  318. {
  319. unsigned long val;
  320. tick_disable_protection();
  321. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  322. * XXX into actually sending STICK interrupts. I think because
  323. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  324. * XXX {TICK + STICK} interrupt mux. -DaveM
  325. */
  326. __hbird_write_stick(__hbird_read_stick());
  327. val = __hbird_read_stick() & ~TICK_PRIV_BIT;
  328. __hbird_write_compare(val + offset);
  329. }
  330. static unsigned long hbtick_get_tick(void)
  331. {
  332. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  333. }
  334. static unsigned long hbtick_get_compare(void)
  335. {
  336. return __hbird_read_compare();
  337. }
  338. static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset)
  339. {
  340. unsigned long val;
  341. val = __hbird_read_stick() + adj;
  342. __hbird_write_stick(val);
  343. val &= ~TICK_PRIV_BIT;
  344. __hbird_write_compare(val + offset);
  345. return val;
  346. }
  347. static unsigned long hbtick_add_compare(unsigned long adj)
  348. {
  349. unsigned long val = __hbird_read_compare() + adj;
  350. val &= ~TICK_PRIV_BIT;
  351. __hbird_write_compare(val);
  352. return val;
  353. }
  354. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  355. .init_tick = hbtick_init_tick,
  356. .get_tick = hbtick_get_tick,
  357. .get_compare = hbtick_get_compare,
  358. .add_tick = hbtick_add_tick,
  359. .add_compare = hbtick_add_compare,
  360. .softint_mask = 1UL << 0,
  361. };
  362. /* timer_interrupt() needs to keep up the real-time clock,
  363. * as well as call the "do_timer()" routine every clocktick
  364. *
  365. * NOTE: On SUN5 systems the ticker interrupt comes in using 2
  366. * interrupts, one at level14 and one with softint bit 0.
  367. */
  368. unsigned long timer_tick_offset __read_mostly;
  369. static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
  370. #define TICK_SIZE (tick_nsec / 1000)
  371. static inline void timer_check_rtc(void)
  372. {
  373. /* last time the cmos clock got updated */
  374. static long last_rtc_update;
  375. /* Determine when to update the Mostek clock. */
  376. if (ntp_synced() &&
  377. xtime.tv_sec > last_rtc_update + 660 &&
  378. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  379. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  380. if (set_rtc_mmss(xtime.tv_sec) == 0)
  381. last_rtc_update = xtime.tv_sec;
  382. else
  383. last_rtc_update = xtime.tv_sec - 600;
  384. /* do it again in 60 s */
  385. }
  386. }
  387. static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  388. {
  389. unsigned long ticks, compare, pstate;
  390. write_seqlock(&xtime_lock);
  391. do {
  392. #ifndef CONFIG_SMP
  393. profile_tick(CPU_PROFILING, regs);
  394. update_process_times(user_mode(regs));
  395. #endif
  396. do_timer(regs);
  397. /* Guarantee that the following sequences execute
  398. * uninterrupted.
  399. */
  400. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  401. "wrpr %0, %1, %%pstate"
  402. : "=r" (pstate)
  403. : "i" (PSTATE_IE));
  404. compare = tick_ops->add_compare(timer_tick_offset);
  405. ticks = tick_ops->get_tick();
  406. /* Restore PSTATE_IE. */
  407. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  408. : /* no outputs */
  409. : "r" (pstate));
  410. } while (time_after_eq(ticks, compare));
  411. timer_check_rtc();
  412. write_sequnlock(&xtime_lock);
  413. return IRQ_HANDLED;
  414. }
  415. #ifdef CONFIG_SMP
  416. void timer_tick_interrupt(struct pt_regs *regs)
  417. {
  418. write_seqlock(&xtime_lock);
  419. do_timer(regs);
  420. timer_check_rtc();
  421. write_sequnlock(&xtime_lock);
  422. }
  423. #endif
  424. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  425. static void __init kick_start_clock(void)
  426. {
  427. void __iomem *regs = mstk48t02_regs;
  428. u8 sec, tmp;
  429. int i, count;
  430. prom_printf("CLOCK: Clock was stopped. Kick start ");
  431. spin_lock_irq(&mostek_lock);
  432. /* Turn on the kick start bit to start the oscillator. */
  433. tmp = mostek_read(regs + MOSTEK_CREG);
  434. tmp |= MSTK_CREG_WRITE;
  435. mostek_write(regs + MOSTEK_CREG, tmp);
  436. tmp = mostek_read(regs + MOSTEK_SEC);
  437. tmp &= ~MSTK_STOP;
  438. mostek_write(regs + MOSTEK_SEC, tmp);
  439. tmp = mostek_read(regs + MOSTEK_HOUR);
  440. tmp |= MSTK_KICK_START;
  441. mostek_write(regs + MOSTEK_HOUR, tmp);
  442. tmp = mostek_read(regs + MOSTEK_CREG);
  443. tmp &= ~MSTK_CREG_WRITE;
  444. mostek_write(regs + MOSTEK_CREG, tmp);
  445. spin_unlock_irq(&mostek_lock);
  446. /* Delay to allow the clock oscillator to start. */
  447. sec = MSTK_REG_SEC(regs);
  448. for (i = 0; i < 3; i++) {
  449. while (sec == MSTK_REG_SEC(regs))
  450. for (count = 0; count < 100000; count++)
  451. /* nothing */ ;
  452. prom_printf(".");
  453. sec = MSTK_REG_SEC(regs);
  454. }
  455. prom_printf("\n");
  456. spin_lock_irq(&mostek_lock);
  457. /* Turn off kick start and set a "valid" time and date. */
  458. tmp = mostek_read(regs + MOSTEK_CREG);
  459. tmp |= MSTK_CREG_WRITE;
  460. mostek_write(regs + MOSTEK_CREG, tmp);
  461. tmp = mostek_read(regs + MOSTEK_HOUR);
  462. tmp &= ~MSTK_KICK_START;
  463. mostek_write(regs + MOSTEK_HOUR, tmp);
  464. MSTK_SET_REG_SEC(regs,0);
  465. MSTK_SET_REG_MIN(regs,0);
  466. MSTK_SET_REG_HOUR(regs,0);
  467. MSTK_SET_REG_DOW(regs,5);
  468. MSTK_SET_REG_DOM(regs,1);
  469. MSTK_SET_REG_MONTH(regs,8);
  470. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  471. tmp = mostek_read(regs + MOSTEK_CREG);
  472. tmp &= ~MSTK_CREG_WRITE;
  473. mostek_write(regs + MOSTEK_CREG, tmp);
  474. spin_unlock_irq(&mostek_lock);
  475. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  476. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  477. prom_printf("CLOCK: Kick start still on!\n");
  478. spin_lock_irq(&mostek_lock);
  479. tmp = mostek_read(regs + MOSTEK_CREG);
  480. tmp |= MSTK_CREG_WRITE;
  481. mostek_write(regs + MOSTEK_CREG, tmp);
  482. tmp = mostek_read(regs + MOSTEK_HOUR);
  483. tmp &= ~MSTK_KICK_START;
  484. mostek_write(regs + MOSTEK_HOUR, tmp);
  485. tmp = mostek_read(regs + MOSTEK_CREG);
  486. tmp &= ~MSTK_CREG_WRITE;
  487. mostek_write(regs + MOSTEK_CREG, tmp);
  488. spin_unlock_irq(&mostek_lock);
  489. }
  490. prom_printf("CLOCK: Kick start procedure successful.\n");
  491. }
  492. /* Return nonzero if the clock chip battery is low. */
  493. static int __init has_low_battery(void)
  494. {
  495. void __iomem *regs = mstk48t02_regs;
  496. u8 data1, data2;
  497. spin_lock_irq(&mostek_lock);
  498. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  499. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  500. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  501. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  502. spin_unlock_irq(&mostek_lock);
  503. return (data1 == data2); /* Was the write blocked? */
  504. }
  505. /* Probe for the real time clock chip. */
  506. static void __init set_system_time(void)
  507. {
  508. unsigned int year, mon, day, hour, min, sec;
  509. void __iomem *mregs = mstk48t02_regs;
  510. #ifdef CONFIG_PCI
  511. unsigned long dregs = ds1287_regs;
  512. #else
  513. unsigned long dregs = 0UL;
  514. #endif
  515. u8 tmp;
  516. if (!mregs && !dregs) {
  517. prom_printf("Something wrong, clock regs not mapped yet.\n");
  518. prom_halt();
  519. }
  520. if (mregs) {
  521. spin_lock_irq(&mostek_lock);
  522. /* Traditional Mostek chip. */
  523. tmp = mostek_read(mregs + MOSTEK_CREG);
  524. tmp |= MSTK_CREG_READ;
  525. mostek_write(mregs + MOSTEK_CREG, tmp);
  526. sec = MSTK_REG_SEC(mregs);
  527. min = MSTK_REG_MIN(mregs);
  528. hour = MSTK_REG_HOUR(mregs);
  529. day = MSTK_REG_DOM(mregs);
  530. mon = MSTK_REG_MONTH(mregs);
  531. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  532. } else {
  533. int i;
  534. /* Dallas 12887 RTC chip. */
  535. /* Stolen from arch/i386/kernel/time.c, see there for
  536. * credits and descriptive comments.
  537. */
  538. for (i = 0; i < 1000000; i++) {
  539. if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
  540. break;
  541. udelay(10);
  542. }
  543. for (i = 0; i < 1000000; i++) {
  544. if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  545. break;
  546. udelay(10);
  547. }
  548. do {
  549. sec = CMOS_READ(RTC_SECONDS);
  550. min = CMOS_READ(RTC_MINUTES);
  551. hour = CMOS_READ(RTC_HOURS);
  552. day = CMOS_READ(RTC_DAY_OF_MONTH);
  553. mon = CMOS_READ(RTC_MONTH);
  554. year = CMOS_READ(RTC_YEAR);
  555. } while (sec != CMOS_READ(RTC_SECONDS));
  556. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  557. BCD_TO_BIN(sec);
  558. BCD_TO_BIN(min);
  559. BCD_TO_BIN(hour);
  560. BCD_TO_BIN(day);
  561. BCD_TO_BIN(mon);
  562. BCD_TO_BIN(year);
  563. }
  564. if ((year += 1900) < 1970)
  565. year += 100;
  566. }
  567. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  568. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  569. set_normalized_timespec(&wall_to_monotonic,
  570. -xtime.tv_sec, -xtime.tv_nsec);
  571. if (mregs) {
  572. tmp = mostek_read(mregs + MOSTEK_CREG);
  573. tmp &= ~MSTK_CREG_READ;
  574. mostek_write(mregs + MOSTEK_CREG, tmp);
  575. spin_unlock_irq(&mostek_lock);
  576. }
  577. }
  578. void __init clock_probe(void)
  579. {
  580. struct linux_prom_registers clk_reg[2];
  581. char model[128];
  582. int node, busnd = -1, err;
  583. unsigned long flags;
  584. struct linux_central *cbus;
  585. #ifdef CONFIG_PCI
  586. struct linux_ebus *ebus = NULL;
  587. struct sparc_isa_bridge *isa_br = NULL;
  588. #endif
  589. static int invoked;
  590. if (invoked)
  591. return;
  592. invoked = 1;
  593. if (this_is_starfire) {
  594. /* davem suggests we keep this within the 4M locked kernel image */
  595. static char obp_gettod[256];
  596. static u32 unix_tod;
  597. sprintf(obp_gettod, "h# %08x unix-gettod",
  598. (unsigned int) (long) &unix_tod);
  599. prom_feval(obp_gettod);
  600. xtime.tv_sec = unix_tod;
  601. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  602. set_normalized_timespec(&wall_to_monotonic,
  603. -xtime.tv_sec, -xtime.tv_nsec);
  604. return;
  605. }
  606. local_irq_save(flags);
  607. cbus = central_bus;
  608. if (cbus != NULL)
  609. busnd = central_bus->child->prom_node;
  610. /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
  611. * That way we handle the presence of multiple properly.
  612. *
  613. * As a special case, machines with Central must provide the
  614. * timer chip there.
  615. */
  616. #ifdef CONFIG_PCI
  617. if (ebus_chain != NULL) {
  618. ebus = ebus_chain;
  619. if (busnd == -1)
  620. busnd = ebus->prom_node;
  621. }
  622. if (isa_chain != NULL) {
  623. isa_br = isa_chain;
  624. if (busnd == -1)
  625. busnd = isa_br->prom_node;
  626. }
  627. #endif
  628. if (sbus_root != NULL && busnd == -1)
  629. busnd = sbus_root->prom_node;
  630. if (busnd == -1) {
  631. prom_printf("clock_probe: problem, cannot find bus to search.\n");
  632. prom_halt();
  633. }
  634. node = prom_getchild(busnd);
  635. while (1) {
  636. if (!node)
  637. model[0] = 0;
  638. else
  639. prom_getstring(node, "model", model, sizeof(model));
  640. if (strcmp(model, "mk48t02") &&
  641. strcmp(model, "mk48t08") &&
  642. strcmp(model, "mk48t59") &&
  643. strcmp(model, "m5819") &&
  644. strcmp(model, "m5819p") &&
  645. strcmp(model, "m5823") &&
  646. strcmp(model, "ds1287")) {
  647. if (cbus != NULL) {
  648. prom_printf("clock_probe: Central bus lacks timer chip.\n");
  649. prom_halt();
  650. }
  651. if (node != 0)
  652. node = prom_getsibling(node);
  653. #ifdef CONFIG_PCI
  654. while ((node == 0) && ebus != NULL) {
  655. ebus = ebus->next;
  656. if (ebus != NULL) {
  657. busnd = ebus->prom_node;
  658. node = prom_getchild(busnd);
  659. }
  660. }
  661. while ((node == 0) && isa_br != NULL) {
  662. isa_br = isa_br->next;
  663. if (isa_br != NULL) {
  664. busnd = isa_br->prom_node;
  665. node = prom_getchild(busnd);
  666. }
  667. }
  668. #endif
  669. if (node == 0) {
  670. prom_printf("clock_probe: Cannot find timer chip\n");
  671. prom_halt();
  672. }
  673. continue;
  674. }
  675. err = prom_getproperty(node, "reg", (char *)clk_reg,
  676. sizeof(clk_reg));
  677. if(err == -1) {
  678. prom_printf("clock_probe: Cannot get Mostek reg property\n");
  679. prom_halt();
  680. }
  681. if (cbus != NULL) {
  682. apply_fhc_ranges(central_bus->child, clk_reg, 1);
  683. apply_central_ranges(central_bus, clk_reg, 1);
  684. }
  685. #ifdef CONFIG_PCI
  686. else if (ebus != NULL) {
  687. struct linux_ebus_device *edev;
  688. for_each_ebusdev(edev, ebus)
  689. if (edev->prom_node == node)
  690. break;
  691. if (edev == NULL) {
  692. if (isa_chain != NULL)
  693. goto try_isa_clock;
  694. prom_printf("%s: Mostek not probed by EBUS\n",
  695. __FUNCTION__);
  696. prom_halt();
  697. }
  698. if (!strcmp(model, "ds1287") ||
  699. !strcmp(model, "m5819") ||
  700. !strcmp(model, "m5819p") ||
  701. !strcmp(model, "m5823")) {
  702. ds1287_regs = edev->resource[0].start;
  703. } else {
  704. mstk48t59_regs = (void __iomem *)
  705. edev->resource[0].start;
  706. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  707. }
  708. break;
  709. }
  710. else if (isa_br != NULL) {
  711. struct sparc_isa_device *isadev;
  712. try_isa_clock:
  713. for_each_isadev(isadev, isa_br)
  714. if (isadev->prom_node == node)
  715. break;
  716. if (isadev == NULL) {
  717. prom_printf("%s: Mostek not probed by ISA\n");
  718. prom_halt();
  719. }
  720. if (!strcmp(model, "ds1287") ||
  721. !strcmp(model, "m5819") ||
  722. !strcmp(model, "m5819p") ||
  723. !strcmp(model, "m5823")) {
  724. ds1287_regs = isadev->resource.start;
  725. } else {
  726. mstk48t59_regs = (void __iomem *)
  727. isadev->resource.start;
  728. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  729. }
  730. break;
  731. }
  732. #endif
  733. else {
  734. if (sbus_root->num_sbus_ranges) {
  735. int nranges = sbus_root->num_sbus_ranges;
  736. int rngc;
  737. for (rngc = 0; rngc < nranges; rngc++)
  738. if (clk_reg[0].which_io ==
  739. sbus_root->sbus_ranges[rngc].ot_child_space)
  740. break;
  741. if (rngc == nranges) {
  742. prom_printf("clock_probe: Cannot find ranges for "
  743. "clock regs.\n");
  744. prom_halt();
  745. }
  746. clk_reg[0].which_io =
  747. sbus_root->sbus_ranges[rngc].ot_parent_space;
  748. clk_reg[0].phys_addr +=
  749. sbus_root->sbus_ranges[rngc].ot_parent_base;
  750. }
  751. }
  752. if(model[5] == '0' && model[6] == '2') {
  753. mstk48t02_regs = (void __iomem *)
  754. (((u64)clk_reg[0].phys_addr) |
  755. (((u64)clk_reg[0].which_io)<<32UL));
  756. } else if(model[5] == '0' && model[6] == '8') {
  757. mstk48t08_regs = (void __iomem *)
  758. (((u64)clk_reg[0].phys_addr) |
  759. (((u64)clk_reg[0].which_io)<<32UL));
  760. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  761. } else {
  762. mstk48t59_regs = (void __iomem *)
  763. (((u64)clk_reg[0].phys_addr) |
  764. (((u64)clk_reg[0].which_io)<<32UL));
  765. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  766. }
  767. break;
  768. }
  769. if (mstk48t02_regs != NULL) {
  770. /* Report a low battery voltage condition. */
  771. if (has_low_battery())
  772. prom_printf("NVRAM: Low battery voltage!\n");
  773. /* Kick start the clock if it is completely stopped. */
  774. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  775. kick_start_clock();
  776. }
  777. set_system_time();
  778. local_irq_restore(flags);
  779. }
  780. /* This is gets the master TICK_INT timer going. */
  781. static unsigned long sparc64_init_timers(void)
  782. {
  783. unsigned long clock;
  784. int node;
  785. #ifdef CONFIG_SMP
  786. extern void smp_tick_init(void);
  787. #endif
  788. if (tlb_type == spitfire) {
  789. unsigned long ver, manuf, impl;
  790. __asm__ __volatile__ ("rdpr %%ver, %0"
  791. : "=&r" (ver));
  792. manuf = ((ver >> 48) & 0xffff);
  793. impl = ((ver >> 32) & 0xffff);
  794. if (manuf == 0x17 && impl == 0x13) {
  795. /* Hummingbird, aka Ultra-IIe */
  796. tick_ops = &hbtick_operations;
  797. node = prom_root_node;
  798. clock = prom_getint(node, "stick-frequency");
  799. } else {
  800. tick_ops = &tick_operations;
  801. cpu_find_by_instance(0, &node, NULL);
  802. clock = prom_getint(node, "clock-frequency");
  803. }
  804. } else {
  805. tick_ops = &stick_operations;
  806. node = prom_root_node;
  807. clock = prom_getint(node, "stick-frequency");
  808. }
  809. timer_tick_offset = clock / HZ;
  810. #ifdef CONFIG_SMP
  811. smp_tick_init();
  812. #endif
  813. return clock;
  814. }
  815. static void sparc64_start_timers(irqreturn_t (*cfunc)(int, void *, struct pt_regs *))
  816. {
  817. unsigned long pstate;
  818. int err;
  819. /* Register IRQ handler. */
  820. err = request_irq(build_irq(0, 0, 0UL, 0UL), cfunc, 0,
  821. "timer", NULL);
  822. if (err) {
  823. prom_printf("Serious problem, cannot register TICK_INT\n");
  824. prom_halt();
  825. }
  826. /* Guarantee that the following sequences execute
  827. * uninterrupted.
  828. */
  829. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  830. "wrpr %0, %1, %%pstate"
  831. : "=r" (pstate)
  832. : "i" (PSTATE_IE));
  833. tick_ops->init_tick(timer_tick_offset);
  834. /* Restore PSTATE_IE. */
  835. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  836. : /* no outputs */
  837. : "r" (pstate));
  838. local_irq_enable();
  839. }
  840. struct freq_table {
  841. unsigned long udelay_val_ref;
  842. unsigned long clock_tick_ref;
  843. unsigned int ref_freq;
  844. };
  845. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0, 0 };
  846. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  847. {
  848. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  849. if (ft->clock_tick_ref)
  850. return ft->clock_tick_ref;
  851. return cpu_data(cpu).clock_tick;
  852. }
  853. #ifdef CONFIG_CPU_FREQ
  854. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  855. void *data)
  856. {
  857. struct cpufreq_freqs *freq = data;
  858. unsigned int cpu = freq->cpu;
  859. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  860. if (!ft->ref_freq) {
  861. ft->ref_freq = freq->old;
  862. ft->udelay_val_ref = cpu_data(cpu).udelay_val;
  863. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  864. }
  865. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  866. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  867. (val == CPUFREQ_RESUMECHANGE)) {
  868. cpu_data(cpu).udelay_val =
  869. cpufreq_scale(ft->udelay_val_ref,
  870. ft->ref_freq,
  871. freq->new);
  872. cpu_data(cpu).clock_tick =
  873. cpufreq_scale(ft->clock_tick_ref,
  874. ft->ref_freq,
  875. freq->new);
  876. }
  877. return 0;
  878. }
  879. static struct notifier_block sparc64_cpufreq_notifier_block = {
  880. .notifier_call = sparc64_cpufreq_notifier
  881. };
  882. #endif /* CONFIG_CPU_FREQ */
  883. static struct time_interpolator sparc64_cpu_interpolator = {
  884. .source = TIME_SOURCE_CPU,
  885. .shift = 16,
  886. .mask = 0xffffffffffffffffLL
  887. };
  888. /* The quotient formula is taken from the IA64 port. */
  889. #define SPARC64_NSEC_PER_CYC_SHIFT 30UL
  890. void __init time_init(void)
  891. {
  892. unsigned long clock = sparc64_init_timers();
  893. sparc64_cpu_interpolator.frequency = clock;
  894. register_time_interpolator(&sparc64_cpu_interpolator);
  895. /* Now that the interpolator is registered, it is
  896. * safe to start the timer ticking.
  897. */
  898. sparc64_start_timers(timer_interrupt);
  899. timer_ticks_per_nsec_quotient =
  900. (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
  901. (clock / 2)) / clock);
  902. #ifdef CONFIG_CPU_FREQ
  903. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  904. CPUFREQ_TRANSITION_NOTIFIER);
  905. #endif
  906. }
  907. unsigned long long sched_clock(void)
  908. {
  909. unsigned long ticks = tick_ops->get_tick();
  910. return (ticks * timer_ticks_per_nsec_quotient)
  911. >> SPARC64_NSEC_PER_CYC_SHIFT;
  912. }
  913. static int set_rtc_mmss(unsigned long nowtime)
  914. {
  915. int real_seconds, real_minutes, chip_minutes;
  916. void __iomem *mregs = mstk48t02_regs;
  917. #ifdef CONFIG_PCI
  918. unsigned long dregs = ds1287_regs;
  919. #else
  920. unsigned long dregs = 0UL;
  921. #endif
  922. unsigned long flags;
  923. u8 tmp;
  924. /*
  925. * Not having a register set can lead to trouble.
  926. * Also starfire doesn't have a tod clock.
  927. */
  928. if (!mregs && !dregs)
  929. return -1;
  930. if (mregs) {
  931. spin_lock_irqsave(&mostek_lock, flags);
  932. /* Read the current RTC minutes. */
  933. tmp = mostek_read(mregs + MOSTEK_CREG);
  934. tmp |= MSTK_CREG_READ;
  935. mostek_write(mregs + MOSTEK_CREG, tmp);
  936. chip_minutes = MSTK_REG_MIN(mregs);
  937. tmp = mostek_read(mregs + MOSTEK_CREG);
  938. tmp &= ~MSTK_CREG_READ;
  939. mostek_write(mregs + MOSTEK_CREG, tmp);
  940. /*
  941. * since we're only adjusting minutes and seconds,
  942. * don't interfere with hour overflow. This avoids
  943. * messing with unknown time zones but requires your
  944. * RTC not to be off by more than 15 minutes
  945. */
  946. real_seconds = nowtime % 60;
  947. real_minutes = nowtime / 60;
  948. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  949. real_minutes += 30; /* correct for half hour time zone */
  950. real_minutes %= 60;
  951. if (abs(real_minutes - chip_minutes) < 30) {
  952. tmp = mostek_read(mregs + MOSTEK_CREG);
  953. tmp |= MSTK_CREG_WRITE;
  954. mostek_write(mregs + MOSTEK_CREG, tmp);
  955. MSTK_SET_REG_SEC(mregs,real_seconds);
  956. MSTK_SET_REG_MIN(mregs,real_minutes);
  957. tmp = mostek_read(mregs + MOSTEK_CREG);
  958. tmp &= ~MSTK_CREG_WRITE;
  959. mostek_write(mregs + MOSTEK_CREG, tmp);
  960. spin_unlock_irqrestore(&mostek_lock, flags);
  961. return 0;
  962. } else {
  963. spin_unlock_irqrestore(&mostek_lock, flags);
  964. return -1;
  965. }
  966. } else {
  967. int retval = 0;
  968. unsigned char save_control, save_freq_select;
  969. /* Stolen from arch/i386/kernel/time.c, see there for
  970. * credits and descriptive comments.
  971. */
  972. spin_lock_irqsave(&rtc_lock, flags);
  973. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  974. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  975. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  976. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  977. chip_minutes = CMOS_READ(RTC_MINUTES);
  978. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  979. BCD_TO_BIN(chip_minutes);
  980. real_seconds = nowtime % 60;
  981. real_minutes = nowtime / 60;
  982. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  983. real_minutes += 30;
  984. real_minutes %= 60;
  985. if (abs(real_minutes - chip_minutes) < 30) {
  986. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  987. BIN_TO_BCD(real_seconds);
  988. BIN_TO_BCD(real_minutes);
  989. }
  990. CMOS_WRITE(real_seconds,RTC_SECONDS);
  991. CMOS_WRITE(real_minutes,RTC_MINUTES);
  992. } else {
  993. printk(KERN_WARNING
  994. "set_rtc_mmss: can't update from %d to %d\n",
  995. chip_minutes, real_minutes);
  996. retval = -1;
  997. }
  998. CMOS_WRITE(save_control, RTC_CONTROL);
  999. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1000. spin_unlock_irqrestore(&rtc_lock, flags);
  1001. return retval;
  1002. }
  1003. }