time.c 30 KB

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