time.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 = &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 = {
  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 = {
  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 = {
  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;
  369. unsigned long timer_tick_compare;
  370. static unsigned long timer_ticks_per_nsec_quotient;
  371. #define TICK_SIZE (tick_nsec / 1000)
  372. static inline void timer_check_rtc(void)
  373. {
  374. /* last time the cmos clock got updated */
  375. static long last_rtc_update;
  376. /* Determine when to update the Mostek clock. */
  377. if ((time_status & STA_UNSYNC) == 0 &&
  378. xtime.tv_sec > last_rtc_update + 660 &&
  379. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  380. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  381. if (set_rtc_mmss(xtime.tv_sec) == 0)
  382. last_rtc_update = xtime.tv_sec;
  383. else
  384. last_rtc_update = xtime.tv_sec - 600;
  385. /* do it again in 60 s */
  386. }
  387. }
  388. static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  389. {
  390. unsigned long ticks, pstate;
  391. write_seqlock(&xtime_lock);
  392. do {
  393. #ifndef CONFIG_SMP
  394. profile_tick(CPU_PROFILING, regs);
  395. update_process_times(user_mode(regs));
  396. #endif
  397. do_timer(regs);
  398. /* Guarantee that the following sequences execute
  399. * uninterrupted.
  400. */
  401. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  402. "wrpr %0, %1, %%pstate"
  403. : "=r" (pstate)
  404. : "i" (PSTATE_IE));
  405. timer_tick_compare = tick_ops->add_compare(timer_tick_offset);
  406. ticks = tick_ops->get_tick();
  407. /* Restore PSTATE_IE. */
  408. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  409. : /* no outputs */
  410. : "r" (pstate));
  411. } while (time_after_eq(ticks, timer_tick_compare));
  412. timer_check_rtc();
  413. write_sequnlock(&xtime_lock);
  414. return IRQ_HANDLED;
  415. }
  416. #ifdef CONFIG_SMP
  417. void timer_tick_interrupt(struct pt_regs *regs)
  418. {
  419. write_seqlock(&xtime_lock);
  420. do_timer(regs);
  421. /*
  422. * Only keep timer_tick_offset uptodate, but don't set TICK_CMPR.
  423. */
  424. timer_tick_compare = tick_ops->get_compare() + timer_tick_offset;
  425. timer_check_rtc();
  426. write_sequnlock(&xtime_lock);
  427. }
  428. #endif
  429. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  430. static void __init kick_start_clock(void)
  431. {
  432. void __iomem *regs = mstk48t02_regs;
  433. u8 sec, tmp;
  434. int i, count;
  435. prom_printf("CLOCK: Clock was stopped. Kick start ");
  436. spin_lock_irq(&mostek_lock);
  437. /* Turn on the kick start bit to start the oscillator. */
  438. tmp = mostek_read(regs + MOSTEK_CREG);
  439. tmp |= MSTK_CREG_WRITE;
  440. mostek_write(regs + MOSTEK_CREG, tmp);
  441. tmp = mostek_read(regs + MOSTEK_SEC);
  442. tmp &= ~MSTK_STOP;
  443. mostek_write(regs + MOSTEK_SEC, tmp);
  444. tmp = mostek_read(regs + MOSTEK_HOUR);
  445. tmp |= MSTK_KICK_START;
  446. mostek_write(regs + MOSTEK_HOUR, tmp);
  447. tmp = mostek_read(regs + MOSTEK_CREG);
  448. tmp &= ~MSTK_CREG_WRITE;
  449. mostek_write(regs + MOSTEK_CREG, tmp);
  450. spin_unlock_irq(&mostek_lock);
  451. /* Delay to allow the clock oscillator to start. */
  452. sec = MSTK_REG_SEC(regs);
  453. for (i = 0; i < 3; i++) {
  454. while (sec == MSTK_REG_SEC(regs))
  455. for (count = 0; count < 100000; count++)
  456. /* nothing */ ;
  457. prom_printf(".");
  458. sec = MSTK_REG_SEC(regs);
  459. }
  460. prom_printf("\n");
  461. spin_lock_irq(&mostek_lock);
  462. /* Turn off kick start and set a "valid" time and date. */
  463. tmp = mostek_read(regs + MOSTEK_CREG);
  464. tmp |= MSTK_CREG_WRITE;
  465. mostek_write(regs + MOSTEK_CREG, tmp);
  466. tmp = mostek_read(regs + MOSTEK_HOUR);
  467. tmp &= ~MSTK_KICK_START;
  468. mostek_write(regs + MOSTEK_HOUR, tmp);
  469. MSTK_SET_REG_SEC(regs,0);
  470. MSTK_SET_REG_MIN(regs,0);
  471. MSTK_SET_REG_HOUR(regs,0);
  472. MSTK_SET_REG_DOW(regs,5);
  473. MSTK_SET_REG_DOM(regs,1);
  474. MSTK_SET_REG_MONTH(regs,8);
  475. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  476. tmp = mostek_read(regs + MOSTEK_CREG);
  477. tmp &= ~MSTK_CREG_WRITE;
  478. mostek_write(regs + MOSTEK_CREG, tmp);
  479. spin_unlock_irq(&mostek_lock);
  480. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  481. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  482. prom_printf("CLOCK: Kick start still on!\n");
  483. spin_lock_irq(&mostek_lock);
  484. tmp = mostek_read(regs + MOSTEK_CREG);
  485. tmp |= MSTK_CREG_WRITE;
  486. mostek_write(regs + MOSTEK_CREG, tmp);
  487. tmp = mostek_read(regs + MOSTEK_HOUR);
  488. tmp &= ~MSTK_KICK_START;
  489. mostek_write(regs + MOSTEK_HOUR, tmp);
  490. tmp = mostek_read(regs + MOSTEK_CREG);
  491. tmp &= ~MSTK_CREG_WRITE;
  492. mostek_write(regs + MOSTEK_CREG, tmp);
  493. spin_unlock_irq(&mostek_lock);
  494. }
  495. prom_printf("CLOCK: Kick start procedure successful.\n");
  496. }
  497. /* Return nonzero if the clock chip battery is low. */
  498. static int __init has_low_battery(void)
  499. {
  500. void __iomem *regs = mstk48t02_regs;
  501. u8 data1, data2;
  502. spin_lock_irq(&mostek_lock);
  503. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  504. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  505. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  506. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  507. spin_unlock_irq(&mostek_lock);
  508. return (data1 == data2); /* Was the write blocked? */
  509. }
  510. /* Probe for the real time clock chip. */
  511. static void __init set_system_time(void)
  512. {
  513. unsigned int year, mon, day, hour, min, sec;
  514. void __iomem *mregs = mstk48t02_regs;
  515. #ifdef CONFIG_PCI
  516. unsigned long dregs = ds1287_regs;
  517. #else
  518. unsigned long dregs = 0UL;
  519. #endif
  520. u8 tmp;
  521. if (!mregs && !dregs) {
  522. prom_printf("Something wrong, clock regs not mapped yet.\n");
  523. prom_halt();
  524. }
  525. if (mregs) {
  526. spin_lock_irq(&mostek_lock);
  527. /* Traditional Mostek chip. */
  528. tmp = mostek_read(mregs + MOSTEK_CREG);
  529. tmp |= MSTK_CREG_READ;
  530. mostek_write(mregs + MOSTEK_CREG, tmp);
  531. sec = MSTK_REG_SEC(mregs);
  532. min = MSTK_REG_MIN(mregs);
  533. hour = MSTK_REG_HOUR(mregs);
  534. day = MSTK_REG_DOM(mregs);
  535. mon = MSTK_REG_MONTH(mregs);
  536. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  537. } else {
  538. int i;
  539. /* Dallas 12887 RTC chip. */
  540. /* Stolen from arch/i386/kernel/time.c, see there for
  541. * credits and descriptive comments.
  542. */
  543. for (i = 0; i < 1000000; i++) {
  544. if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
  545. break;
  546. udelay(10);
  547. }
  548. for (i = 0; i < 1000000; i++) {
  549. if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  550. break;
  551. udelay(10);
  552. }
  553. do {
  554. sec = CMOS_READ(RTC_SECONDS);
  555. min = CMOS_READ(RTC_MINUTES);
  556. hour = CMOS_READ(RTC_HOURS);
  557. day = CMOS_READ(RTC_DAY_OF_MONTH);
  558. mon = CMOS_READ(RTC_MONTH);
  559. year = CMOS_READ(RTC_YEAR);
  560. } while (sec != CMOS_READ(RTC_SECONDS));
  561. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  562. BCD_TO_BIN(sec);
  563. BCD_TO_BIN(min);
  564. BCD_TO_BIN(hour);
  565. BCD_TO_BIN(day);
  566. BCD_TO_BIN(mon);
  567. BCD_TO_BIN(year);
  568. }
  569. if ((year += 1900) < 1970)
  570. year += 100;
  571. }
  572. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  573. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  574. set_normalized_timespec(&wall_to_monotonic,
  575. -xtime.tv_sec, -xtime.tv_nsec);
  576. if (mregs) {
  577. tmp = mostek_read(mregs + MOSTEK_CREG);
  578. tmp &= ~MSTK_CREG_READ;
  579. mostek_write(mregs + MOSTEK_CREG, tmp);
  580. spin_unlock_irq(&mostek_lock);
  581. }
  582. }
  583. void __init clock_probe(void)
  584. {
  585. struct linux_prom_registers clk_reg[2];
  586. char model[128];
  587. int node, busnd = -1, err;
  588. unsigned long flags;
  589. struct linux_central *cbus;
  590. #ifdef CONFIG_PCI
  591. struct linux_ebus *ebus = NULL;
  592. struct sparc_isa_bridge *isa_br = NULL;
  593. #endif
  594. static int invoked;
  595. if (invoked)
  596. return;
  597. invoked = 1;
  598. if (this_is_starfire) {
  599. /* davem suggests we keep this within the 4M locked kernel image */
  600. static char obp_gettod[256];
  601. static u32 unix_tod;
  602. sprintf(obp_gettod, "h# %08x unix-gettod",
  603. (unsigned int) (long) &unix_tod);
  604. prom_feval(obp_gettod);
  605. xtime.tv_sec = unix_tod;
  606. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  607. set_normalized_timespec(&wall_to_monotonic,
  608. -xtime.tv_sec, -xtime.tv_nsec);
  609. return;
  610. }
  611. local_irq_save(flags);
  612. cbus = central_bus;
  613. if (cbus != NULL)
  614. busnd = central_bus->child->prom_node;
  615. /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
  616. * That way we handle the presence of multiple properly.
  617. *
  618. * As a special case, machines with Central must provide the
  619. * timer chip there.
  620. */
  621. #ifdef CONFIG_PCI
  622. if (ebus_chain != NULL) {
  623. ebus = ebus_chain;
  624. if (busnd == -1)
  625. busnd = ebus->prom_node;
  626. }
  627. if (isa_chain != NULL) {
  628. isa_br = isa_chain;
  629. if (busnd == -1)
  630. busnd = isa_br->prom_node;
  631. }
  632. #endif
  633. if (sbus_root != NULL && busnd == -1)
  634. busnd = sbus_root->prom_node;
  635. if (busnd == -1) {
  636. prom_printf("clock_probe: problem, cannot find bus to search.\n");
  637. prom_halt();
  638. }
  639. node = prom_getchild(busnd);
  640. while (1) {
  641. if (!node)
  642. model[0] = 0;
  643. else
  644. prom_getstring(node, "model", model, sizeof(model));
  645. if (strcmp(model, "mk48t02") &&
  646. strcmp(model, "mk48t08") &&
  647. strcmp(model, "mk48t59") &&
  648. strcmp(model, "m5819") &&
  649. strcmp(model, "m5819p") &&
  650. strcmp(model, "m5823") &&
  651. strcmp(model, "ds1287")) {
  652. if (cbus != NULL) {
  653. prom_printf("clock_probe: Central bus lacks timer chip.\n");
  654. prom_halt();
  655. }
  656. if (node != 0)
  657. node = prom_getsibling(node);
  658. #ifdef CONFIG_PCI
  659. while ((node == 0) && ebus != NULL) {
  660. ebus = ebus->next;
  661. if (ebus != NULL) {
  662. busnd = ebus->prom_node;
  663. node = prom_getchild(busnd);
  664. }
  665. }
  666. while ((node == 0) && isa_br != NULL) {
  667. isa_br = isa_br->next;
  668. if (isa_br != NULL) {
  669. busnd = isa_br->prom_node;
  670. node = prom_getchild(busnd);
  671. }
  672. }
  673. #endif
  674. if (node == 0) {
  675. prom_printf("clock_probe: Cannot find timer chip\n");
  676. prom_halt();
  677. }
  678. continue;
  679. }
  680. err = prom_getproperty(node, "reg", (char *)clk_reg,
  681. sizeof(clk_reg));
  682. if(err == -1) {
  683. prom_printf("clock_probe: Cannot get Mostek reg property\n");
  684. prom_halt();
  685. }
  686. if (cbus != NULL) {
  687. apply_fhc_ranges(central_bus->child, clk_reg, 1);
  688. apply_central_ranges(central_bus, clk_reg, 1);
  689. }
  690. #ifdef CONFIG_PCI
  691. else if (ebus != NULL) {
  692. struct linux_ebus_device *edev;
  693. for_each_ebusdev(edev, ebus)
  694. if (edev->prom_node == node)
  695. break;
  696. if (edev == NULL) {
  697. if (isa_chain != NULL)
  698. goto try_isa_clock;
  699. prom_printf("%s: Mostek not probed by EBUS\n",
  700. __FUNCTION__);
  701. prom_halt();
  702. }
  703. if (!strcmp(model, "ds1287") ||
  704. !strcmp(model, "m5819") ||
  705. !strcmp(model, "m5819p") ||
  706. !strcmp(model, "m5823")) {
  707. ds1287_regs = edev->resource[0].start;
  708. } else {
  709. mstk48t59_regs = (void __iomem *)
  710. edev->resource[0].start;
  711. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  712. }
  713. break;
  714. }
  715. else if (isa_br != NULL) {
  716. struct sparc_isa_device *isadev;
  717. try_isa_clock:
  718. for_each_isadev(isadev, isa_br)
  719. if (isadev->prom_node == node)
  720. break;
  721. if (isadev == NULL) {
  722. prom_printf("%s: Mostek not probed by ISA\n");
  723. prom_halt();
  724. }
  725. if (!strcmp(model, "ds1287") ||
  726. !strcmp(model, "m5819") ||
  727. !strcmp(model, "m5819p") ||
  728. !strcmp(model, "m5823")) {
  729. ds1287_regs = isadev->resource.start;
  730. } else {
  731. mstk48t59_regs = (void __iomem *)
  732. isadev->resource.start;
  733. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  734. }
  735. break;
  736. }
  737. #endif
  738. else {
  739. if (sbus_root->num_sbus_ranges) {
  740. int nranges = sbus_root->num_sbus_ranges;
  741. int rngc;
  742. for (rngc = 0; rngc < nranges; rngc++)
  743. if (clk_reg[0].which_io ==
  744. sbus_root->sbus_ranges[rngc].ot_child_space)
  745. break;
  746. if (rngc == nranges) {
  747. prom_printf("clock_probe: Cannot find ranges for "
  748. "clock regs.\n");
  749. prom_halt();
  750. }
  751. clk_reg[0].which_io =
  752. sbus_root->sbus_ranges[rngc].ot_parent_space;
  753. clk_reg[0].phys_addr +=
  754. sbus_root->sbus_ranges[rngc].ot_parent_base;
  755. }
  756. }
  757. if(model[5] == '0' && model[6] == '2') {
  758. mstk48t02_regs = (void __iomem *)
  759. (((u64)clk_reg[0].phys_addr) |
  760. (((u64)clk_reg[0].which_io)<<32UL));
  761. } else if(model[5] == '0' && model[6] == '8') {
  762. mstk48t08_regs = (void __iomem *)
  763. (((u64)clk_reg[0].phys_addr) |
  764. (((u64)clk_reg[0].which_io)<<32UL));
  765. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  766. } else {
  767. mstk48t59_regs = (void __iomem *)
  768. (((u64)clk_reg[0].phys_addr) |
  769. (((u64)clk_reg[0].which_io)<<32UL));
  770. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  771. }
  772. break;
  773. }
  774. if (mstk48t02_regs != NULL) {
  775. /* Report a low battery voltage condition. */
  776. if (has_low_battery())
  777. prom_printf("NVRAM: Low battery voltage!\n");
  778. /* Kick start the clock if it is completely stopped. */
  779. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  780. kick_start_clock();
  781. }
  782. set_system_time();
  783. local_irq_restore(flags);
  784. }
  785. /* This is gets the master TICK_INT timer going. */
  786. static unsigned long sparc64_init_timers(void)
  787. {
  788. unsigned long clock;
  789. int node;
  790. #ifdef CONFIG_SMP
  791. extern void smp_tick_init(void);
  792. #endif
  793. if (tlb_type == spitfire) {
  794. unsigned long ver, manuf, impl;
  795. __asm__ __volatile__ ("rdpr %%ver, %0"
  796. : "=&r" (ver));
  797. manuf = ((ver >> 48) & 0xffff);
  798. impl = ((ver >> 32) & 0xffff);
  799. if (manuf == 0x17 && impl == 0x13) {
  800. /* Hummingbird, aka Ultra-IIe */
  801. tick_ops = &hbtick_operations;
  802. node = prom_root_node;
  803. clock = prom_getint(node, "stick-frequency");
  804. } else {
  805. tick_ops = &tick_operations;
  806. cpu_find_by_instance(0, &node, NULL);
  807. clock = prom_getint(node, "clock-frequency");
  808. }
  809. } else {
  810. tick_ops = &stick_operations;
  811. node = prom_root_node;
  812. clock = prom_getint(node, "stick-frequency");
  813. }
  814. timer_tick_offset = clock / HZ;
  815. #ifdef CONFIG_SMP
  816. smp_tick_init();
  817. #endif
  818. return clock;
  819. }
  820. static void sparc64_start_timers(irqreturn_t (*cfunc)(int, void *, struct pt_regs *))
  821. {
  822. unsigned long pstate;
  823. int err;
  824. /* Register IRQ handler. */
  825. err = request_irq(build_irq(0, 0, 0UL, 0UL), cfunc, SA_STATIC_ALLOC,
  826. "timer", NULL);
  827. if (err) {
  828. prom_printf("Serious problem, cannot register TICK_INT\n");
  829. prom_halt();
  830. }
  831. /* Guarantee that the following sequences execute
  832. * uninterrupted.
  833. */
  834. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  835. "wrpr %0, %1, %%pstate"
  836. : "=r" (pstate)
  837. : "i" (PSTATE_IE));
  838. tick_ops->init_tick(timer_tick_offset);
  839. /* Restore PSTATE_IE. */
  840. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  841. : /* no outputs */
  842. : "r" (pstate));
  843. local_irq_enable();
  844. }
  845. struct freq_table {
  846. unsigned long udelay_val_ref;
  847. unsigned long clock_tick_ref;
  848. unsigned int ref_freq;
  849. };
  850. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0, 0 };
  851. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  852. {
  853. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  854. if (ft->clock_tick_ref)
  855. return ft->clock_tick_ref;
  856. return cpu_data(cpu).clock_tick;
  857. }
  858. #ifdef CONFIG_CPU_FREQ
  859. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  860. void *data)
  861. {
  862. struct cpufreq_freqs *freq = data;
  863. unsigned int cpu = freq->cpu;
  864. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  865. if (!ft->ref_freq) {
  866. ft->ref_freq = freq->old;
  867. ft->udelay_val_ref = cpu_data(cpu).udelay_val;
  868. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  869. }
  870. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  871. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  872. (val == CPUFREQ_RESUMECHANGE)) {
  873. cpu_data(cpu).udelay_val =
  874. cpufreq_scale(ft->udelay_val_ref,
  875. ft->ref_freq,
  876. freq->new);
  877. cpu_data(cpu).clock_tick =
  878. cpufreq_scale(ft->clock_tick_ref,
  879. ft->ref_freq,
  880. freq->new);
  881. }
  882. return 0;
  883. }
  884. static struct notifier_block sparc64_cpufreq_notifier_block = {
  885. .notifier_call = sparc64_cpufreq_notifier
  886. };
  887. #endif /* CONFIG_CPU_FREQ */
  888. static struct time_interpolator sparc64_cpu_interpolator = {
  889. .source = TIME_SOURCE_CPU,
  890. .shift = 16,
  891. .mask = 0xffffffffffffffffLL
  892. };
  893. /* The quotient formula is taken from the IA64 port. */
  894. #define SPARC64_NSEC_PER_CYC_SHIFT 30UL
  895. void __init time_init(void)
  896. {
  897. unsigned long clock = sparc64_init_timers();
  898. sparc64_cpu_interpolator.frequency = clock;
  899. register_time_interpolator(&sparc64_cpu_interpolator);
  900. /* Now that the interpolator is registered, it is
  901. * safe to start the timer ticking.
  902. */
  903. sparc64_start_timers(timer_interrupt);
  904. timer_ticks_per_nsec_quotient =
  905. (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
  906. (clock / 2)) / clock);
  907. #ifdef CONFIG_CPU_FREQ
  908. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  909. CPUFREQ_TRANSITION_NOTIFIER);
  910. #endif
  911. }
  912. unsigned long long sched_clock(void)
  913. {
  914. unsigned long ticks = tick_ops->get_tick();
  915. return (ticks * timer_ticks_per_nsec_quotient)
  916. >> SPARC64_NSEC_PER_CYC_SHIFT;
  917. }
  918. static int set_rtc_mmss(unsigned long nowtime)
  919. {
  920. int real_seconds, real_minutes, chip_minutes;
  921. void __iomem *mregs = mstk48t02_regs;
  922. #ifdef CONFIG_PCI
  923. unsigned long dregs = ds1287_regs;
  924. #else
  925. unsigned long dregs = 0UL;
  926. #endif
  927. unsigned long flags;
  928. u8 tmp;
  929. /*
  930. * Not having a register set can lead to trouble.
  931. * Also starfire doesn't have a tod clock.
  932. */
  933. if (!mregs && !dregs)
  934. return -1;
  935. if (mregs) {
  936. spin_lock_irqsave(&mostek_lock, flags);
  937. /* Read the current RTC minutes. */
  938. tmp = mostek_read(mregs + MOSTEK_CREG);
  939. tmp |= MSTK_CREG_READ;
  940. mostek_write(mregs + MOSTEK_CREG, tmp);
  941. chip_minutes = MSTK_REG_MIN(mregs);
  942. tmp = mostek_read(mregs + MOSTEK_CREG);
  943. tmp &= ~MSTK_CREG_READ;
  944. mostek_write(mregs + MOSTEK_CREG, tmp);
  945. /*
  946. * since we're only adjusting minutes and seconds,
  947. * don't interfere with hour overflow. This avoids
  948. * messing with unknown time zones but requires your
  949. * RTC not to be off by more than 15 minutes
  950. */
  951. real_seconds = nowtime % 60;
  952. real_minutes = nowtime / 60;
  953. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  954. real_minutes += 30; /* correct for half hour time zone */
  955. real_minutes %= 60;
  956. if (abs(real_minutes - chip_minutes) < 30) {
  957. tmp = mostek_read(mregs + MOSTEK_CREG);
  958. tmp |= MSTK_CREG_WRITE;
  959. mostek_write(mregs + MOSTEK_CREG, tmp);
  960. MSTK_SET_REG_SEC(mregs,real_seconds);
  961. MSTK_SET_REG_MIN(mregs,real_minutes);
  962. tmp = mostek_read(mregs + MOSTEK_CREG);
  963. tmp &= ~MSTK_CREG_WRITE;
  964. mostek_write(mregs + MOSTEK_CREG, tmp);
  965. spin_unlock_irqrestore(&mostek_lock, flags);
  966. return 0;
  967. } else {
  968. spin_unlock_irqrestore(&mostek_lock, flags);
  969. return -1;
  970. }
  971. } else {
  972. int retval = 0;
  973. unsigned char save_control, save_freq_select;
  974. /* Stolen from arch/i386/kernel/time.c, see there for
  975. * credits and descriptive comments.
  976. */
  977. spin_lock_irqsave(&rtc_lock, flags);
  978. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  979. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  980. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  981. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  982. chip_minutes = CMOS_READ(RTC_MINUTES);
  983. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  984. BCD_TO_BIN(chip_minutes);
  985. real_seconds = nowtime % 60;
  986. real_minutes = nowtime / 60;
  987. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  988. real_minutes += 30;
  989. real_minutes %= 60;
  990. if (abs(real_minutes - chip_minutes) < 30) {
  991. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  992. BIN_TO_BCD(real_seconds);
  993. BIN_TO_BCD(real_minutes);
  994. }
  995. CMOS_WRITE(real_seconds,RTC_SECONDS);
  996. CMOS_WRITE(real_minutes,RTC_MINUTES);
  997. } else {
  998. printk(KERN_WARNING
  999. "set_rtc_mmss: can't update from %d to %d\n",
  1000. chip_minutes, real_minutes);
  1001. retval = -1;
  1002. }
  1003. CMOS_WRITE(save_control, RTC_CONTROL);
  1004. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1005. spin_unlock_irqrestore(&rtc_lock, flags);
  1006. return retval;
  1007. }
  1008. }