time.c 29 KB

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