time.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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 <linux/miscdevice.h>
  33. #include <linux/rtc.h>
  34. #include <asm/oplib.h>
  35. #include <asm/mostek.h>
  36. #include <asm/timer.h>
  37. #include <asm/irq.h>
  38. #include <asm/io.h>
  39. #include <asm/sbus.h>
  40. #include <asm/fhc.h>
  41. #include <asm/pbm.h>
  42. #include <asm/ebus.h>
  43. #include <asm/isa.h>
  44. #include <asm/starfire.h>
  45. #include <asm/smp.h>
  46. #include <asm/sections.h>
  47. #include <asm/cpudata.h>
  48. #include <asm/uaccess.h>
  49. DEFINE_SPINLOCK(mostek_lock);
  50. DEFINE_SPINLOCK(rtc_lock);
  51. void __iomem *mstk48t02_regs = NULL;
  52. #ifdef CONFIG_PCI
  53. unsigned long ds1287_regs = 0UL;
  54. #endif
  55. extern unsigned long wall_jiffies;
  56. static void __iomem *mstk48t08_regs;
  57. static void __iomem *mstk48t59_regs;
  58. static int set_rtc_mmss(unsigned long);
  59. #define TICK_PRIV_BIT (1UL << 63)
  60. #ifdef CONFIG_SMP
  61. unsigned long profile_pc(struct pt_regs *regs)
  62. {
  63. unsigned long pc = instruction_pointer(regs);
  64. if (in_lock_functions(pc))
  65. return regs->u_regs[UREG_RETPC];
  66. return pc;
  67. }
  68. EXPORT_SYMBOL(profile_pc);
  69. #endif
  70. static void tick_disable_protection(void)
  71. {
  72. /* Set things up so user can access tick register for profiling
  73. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  74. * read back of %tick after writing it.
  75. */
  76. __asm__ __volatile__(
  77. " ba,pt %%xcc, 1f\n"
  78. " nop\n"
  79. " .align 64\n"
  80. "1: rd %%tick, %%g2\n"
  81. " add %%g2, 6, %%g2\n"
  82. " andn %%g2, %0, %%g2\n"
  83. " wrpr %%g2, 0, %%tick\n"
  84. " rdpr %%tick, %%g0"
  85. : /* no outputs */
  86. : "r" (TICK_PRIV_BIT)
  87. : "g2");
  88. }
  89. static void tick_init_tick(unsigned long offset)
  90. {
  91. tick_disable_protection();
  92. __asm__ __volatile__(
  93. " rd %%tick, %%g1\n"
  94. " andn %%g1, %1, %%g1\n"
  95. " ba,pt %%xcc, 1f\n"
  96. " add %%g1, %0, %%g1\n"
  97. " .align 64\n"
  98. "1: wr %%g1, 0x0, %%tick_cmpr\n"
  99. " rd %%tick_cmpr, %%g0"
  100. : /* no outputs */
  101. : "r" (offset), "r" (TICK_PRIV_BIT)
  102. : "g1");
  103. }
  104. static unsigned long tick_get_tick(void)
  105. {
  106. unsigned long ret;
  107. __asm__ __volatile__("rd %%tick, %0\n\t"
  108. "mov %0, %0"
  109. : "=r" (ret));
  110. return ret & ~TICK_PRIV_BIT;
  111. }
  112. static unsigned long tick_get_compare(void)
  113. {
  114. unsigned long ret;
  115. __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
  116. "mov %0, %0"
  117. : "=r" (ret));
  118. return ret;
  119. }
  120. static unsigned long tick_add_compare(unsigned long adj)
  121. {
  122. unsigned long new_compare;
  123. /* Workaround for Spitfire Errata (#54 I think??), I discovered
  124. * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
  125. * number 103640.
  126. *
  127. * On Blackbird writes to %tick_cmpr can fail, the
  128. * workaround seems to be to execute the wr instruction
  129. * at the start of an I-cache line, and perform a dummy
  130. * read back from %tick_cmpr right after writing to it. -DaveM
  131. */
  132. __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
  133. "ba,pt %%xcc, 1f\n\t"
  134. " add %0, %1, %0\n\t"
  135. ".align 64\n"
  136. "1:\n\t"
  137. "wr %0, 0, %%tick_cmpr\n\t"
  138. "rd %%tick_cmpr, %%g0"
  139. : "=&r" (new_compare)
  140. : "r" (adj));
  141. return new_compare;
  142. }
  143. static unsigned long tick_add_tick(unsigned long adj, unsigned long offset)
  144. {
  145. unsigned long new_tick, tmp;
  146. /* Also need to handle Blackbird bug here too. */
  147. __asm__ __volatile__("rd %%tick, %0\n\t"
  148. "add %0, %2, %0\n\t"
  149. "wrpr %0, 0, %%tick\n\t"
  150. "andn %0, %4, %1\n\t"
  151. "ba,pt %%xcc, 1f\n\t"
  152. " add %1, %3, %1\n\t"
  153. ".align 64\n"
  154. "1:\n\t"
  155. "wr %1, 0, %%tick_cmpr\n\t"
  156. "rd %%tick_cmpr, %%g0"
  157. : "=&r" (new_tick), "=&r" (tmp)
  158. : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
  159. return new_tick;
  160. }
  161. static struct sparc64_tick_ops tick_operations __read_mostly = {
  162. .init_tick = tick_init_tick,
  163. .get_tick = tick_get_tick,
  164. .get_compare = tick_get_compare,
  165. .add_tick = tick_add_tick,
  166. .add_compare = tick_add_compare,
  167. .softint_mask = 1UL << 0,
  168. };
  169. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  170. static void stick_init_tick(unsigned long offset)
  171. {
  172. /* Writes to the %tick and %stick register are not
  173. * allowed on sun4v. The Hypervisor controls that
  174. * bit, per-strand.
  175. */
  176. if (tlb_type != hypervisor) {
  177. tick_disable_protection();
  178. /* Let the user get at STICK too. */
  179. __asm__ __volatile__(
  180. " rd %%asr24, %%g2\n"
  181. " andn %%g2, %0, %%g2\n"
  182. " wr %%g2, 0, %%asr24"
  183. : /* no outputs */
  184. : "r" (TICK_PRIV_BIT)
  185. : "g1", "g2");
  186. }
  187. __asm__ __volatile__(
  188. " rd %%asr24, %%g1\n"
  189. " andn %%g1, %1, %%g1\n"
  190. " add %%g1, %0, %%g1\n"
  191. " wr %%g1, 0x0, %%asr25"
  192. : /* no outputs */
  193. : "r" (offset), "r" (TICK_PRIV_BIT)
  194. : "g1");
  195. }
  196. static unsigned long stick_get_tick(void)
  197. {
  198. unsigned long ret;
  199. __asm__ __volatile__("rd %%asr24, %0"
  200. : "=r" (ret));
  201. return ret & ~TICK_PRIV_BIT;
  202. }
  203. static unsigned long stick_get_compare(void)
  204. {
  205. unsigned long ret;
  206. __asm__ __volatile__("rd %%asr25, %0"
  207. : "=r" (ret));
  208. return ret;
  209. }
  210. static unsigned long stick_add_tick(unsigned long adj, unsigned long offset)
  211. {
  212. unsigned long new_tick, tmp;
  213. __asm__ __volatile__("rd %%asr24, %0\n\t"
  214. "add %0, %2, %0\n\t"
  215. "wr %0, 0, %%asr24\n\t"
  216. "andn %0, %4, %1\n\t"
  217. "add %1, %3, %1\n\t"
  218. "wr %1, 0, %%asr25"
  219. : "=&r" (new_tick), "=&r" (tmp)
  220. : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
  221. return new_tick;
  222. }
  223. static unsigned long stick_add_compare(unsigned long adj)
  224. {
  225. unsigned long new_compare;
  226. __asm__ __volatile__("rd %%asr25, %0\n\t"
  227. "add %0, %1, %0\n\t"
  228. "wr %0, 0, %%asr25"
  229. : "=&r" (new_compare)
  230. : "r" (adj));
  231. return new_compare;
  232. }
  233. static struct sparc64_tick_ops stick_operations __read_mostly = {
  234. .init_tick = stick_init_tick,
  235. .get_tick = stick_get_tick,
  236. .get_compare = stick_get_compare,
  237. .add_tick = stick_add_tick,
  238. .add_compare = stick_add_compare,
  239. .softint_mask = 1UL << 16,
  240. };
  241. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  242. * in I/O space. There are two 64-bit registers each, the
  243. * first holds the low 32-bits of the value and the second holds
  244. * the high 32-bits.
  245. *
  246. * Since STICK is constantly updating, we have to access it carefully.
  247. *
  248. * The sequence we use to read is:
  249. * 1) read high
  250. * 2) read low
  251. * 3) read high again, if it rolled re-read both low and high again.
  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+8;
  264. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  265. "1:\n\t"
  266. "sub %1, 0x8, %1\n\t"
  267. "ldxa [%1] %5, %3\n\t"
  268. "add %1, 0x8, %1\n\t"
  269. "ldxa [%1] %5, %4\n\t"
  270. "cmp %4, %2\n\t"
  271. "bne,a,pn %%xcc, 1b\n\t"
  272. " mov %4, %2\n\t"
  273. "sllx %4, 32, %4\n\t"
  274. "or %3, %4, %0\n\t"
  275. : "=&r" (ret), "=&r" (addr),
  276. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  277. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  278. return ret;
  279. }
  280. static unsigned long __hbird_read_compare(void)
  281. {
  282. unsigned long low, high;
  283. unsigned long addr = HBIRD_STICKCMP_ADDR;
  284. __asm__ __volatile__("ldxa [%2] %3, %0\n\t"
  285. "add %2, 0x8, %2\n\t"
  286. "ldxa [%2] %3, %1"
  287. : "=&r" (low), "=&r" (high), "=&r" (addr)
  288. : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr));
  289. return (high << 32UL) | low;
  290. }
  291. static void __hbird_write_stick(unsigned long val)
  292. {
  293. unsigned long low = (val & 0xffffffffUL);
  294. unsigned long high = (val >> 32UL);
  295. unsigned long addr = HBIRD_STICK_ADDR;
  296. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  297. "add %0, 0x8, %0\n\t"
  298. "stxa %3, [%0] %4\n\t"
  299. "sub %0, 0x8, %0\n\t"
  300. "stxa %2, [%0] %4"
  301. : "=&r" (addr)
  302. : "0" (addr), "r" (low), "r" (high),
  303. "i" (ASI_PHYS_BYPASS_EC_E));
  304. }
  305. static void __hbird_write_compare(unsigned long val)
  306. {
  307. unsigned long low = (val & 0xffffffffUL);
  308. unsigned long high = (val >> 32UL);
  309. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  310. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  311. "sub %0, 0x8, %0\n\t"
  312. "stxa %2, [%0] %4"
  313. : "=&r" (addr)
  314. : "0" (addr), "r" (low), "r" (high),
  315. "i" (ASI_PHYS_BYPASS_EC_E));
  316. }
  317. static void hbtick_init_tick(unsigned long offset)
  318. {
  319. unsigned long val;
  320. tick_disable_protection();
  321. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  322. * XXX into actually sending STICK interrupts. I think because
  323. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  324. * XXX {TICK + STICK} interrupt mux. -DaveM
  325. */
  326. __hbird_write_stick(__hbird_read_stick());
  327. val = __hbird_read_stick() & ~TICK_PRIV_BIT;
  328. __hbird_write_compare(val + offset);
  329. }
  330. static unsigned long hbtick_get_tick(void)
  331. {
  332. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  333. }
  334. static unsigned long hbtick_get_compare(void)
  335. {
  336. return __hbird_read_compare();
  337. }
  338. static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset)
  339. {
  340. unsigned long val;
  341. val = __hbird_read_stick() + adj;
  342. __hbird_write_stick(val);
  343. val &= ~TICK_PRIV_BIT;
  344. __hbird_write_compare(val + offset);
  345. return val;
  346. }
  347. static unsigned long hbtick_add_compare(unsigned long adj)
  348. {
  349. unsigned long val = __hbird_read_compare() + adj;
  350. val &= ~TICK_PRIV_BIT;
  351. __hbird_write_compare(val);
  352. return val;
  353. }
  354. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  355. .init_tick = hbtick_init_tick,
  356. .get_tick = hbtick_get_tick,
  357. .get_compare = hbtick_get_compare,
  358. .add_tick = hbtick_add_tick,
  359. .add_compare = hbtick_add_compare,
  360. .softint_mask = 1UL << 0,
  361. };
  362. /* timer_interrupt() needs to keep up the real-time clock,
  363. * as well as call the "do_timer()" routine every clocktick
  364. *
  365. * NOTE: On SUN5 systems the ticker interrupt comes in using 2
  366. * interrupts, one at level14 and one with softint bit 0.
  367. */
  368. unsigned long timer_tick_offset __read_mostly;
  369. static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
  370. #define TICK_SIZE (tick_nsec / 1000)
  371. static inline void timer_check_rtc(void)
  372. {
  373. /* last time the cmos clock got updated */
  374. static long last_rtc_update;
  375. /* Determine when to update the Mostek clock. */
  376. if (ntp_synced() &&
  377. xtime.tv_sec > last_rtc_update + 660 &&
  378. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  379. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  380. if (set_rtc_mmss(xtime.tv_sec) == 0)
  381. last_rtc_update = xtime.tv_sec;
  382. else
  383. last_rtc_update = xtime.tv_sec - 600;
  384. /* do it again in 60 s */
  385. }
  386. }
  387. static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  388. {
  389. unsigned long ticks, compare, pstate;
  390. write_seqlock(&xtime_lock);
  391. do {
  392. #ifndef CONFIG_SMP
  393. profile_tick(CPU_PROFILING, regs);
  394. update_process_times(user_mode(regs));
  395. #endif
  396. do_timer(regs);
  397. /* Guarantee that the following sequences execute
  398. * uninterrupted.
  399. */
  400. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  401. "wrpr %0, %1, %%pstate"
  402. : "=r" (pstate)
  403. : "i" (PSTATE_IE));
  404. compare = tick_ops->add_compare(timer_tick_offset);
  405. ticks = tick_ops->get_tick();
  406. /* Restore PSTATE_IE. */
  407. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  408. : /* no outputs */
  409. : "r" (pstate));
  410. } while (time_after_eq(ticks, compare));
  411. timer_check_rtc();
  412. write_sequnlock(&xtime_lock);
  413. return IRQ_HANDLED;
  414. }
  415. #ifdef CONFIG_SMP
  416. void timer_tick_interrupt(struct pt_regs *regs)
  417. {
  418. write_seqlock(&xtime_lock);
  419. do_timer(regs);
  420. timer_check_rtc();
  421. write_sequnlock(&xtime_lock);
  422. }
  423. #endif
  424. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  425. static void __init kick_start_clock(void)
  426. {
  427. void __iomem *regs = mstk48t02_regs;
  428. u8 sec, tmp;
  429. int i, count;
  430. prom_printf("CLOCK: Clock was stopped. Kick start ");
  431. spin_lock_irq(&mostek_lock);
  432. /* Turn on the kick start bit to start the oscillator. */
  433. tmp = mostek_read(regs + MOSTEK_CREG);
  434. tmp |= MSTK_CREG_WRITE;
  435. mostek_write(regs + MOSTEK_CREG, tmp);
  436. tmp = mostek_read(regs + MOSTEK_SEC);
  437. tmp &= ~MSTK_STOP;
  438. mostek_write(regs + MOSTEK_SEC, tmp);
  439. tmp = mostek_read(regs + MOSTEK_HOUR);
  440. tmp |= MSTK_KICK_START;
  441. mostek_write(regs + MOSTEK_HOUR, tmp);
  442. tmp = mostek_read(regs + MOSTEK_CREG);
  443. tmp &= ~MSTK_CREG_WRITE;
  444. mostek_write(regs + MOSTEK_CREG, tmp);
  445. spin_unlock_irq(&mostek_lock);
  446. /* Delay to allow the clock oscillator to start. */
  447. sec = MSTK_REG_SEC(regs);
  448. for (i = 0; i < 3; i++) {
  449. while (sec == MSTK_REG_SEC(regs))
  450. for (count = 0; count < 100000; count++)
  451. /* nothing */ ;
  452. prom_printf(".");
  453. sec = MSTK_REG_SEC(regs);
  454. }
  455. prom_printf("\n");
  456. spin_lock_irq(&mostek_lock);
  457. /* Turn off kick start and set a "valid" time and date. */
  458. tmp = mostek_read(regs + MOSTEK_CREG);
  459. tmp |= MSTK_CREG_WRITE;
  460. mostek_write(regs + MOSTEK_CREG, tmp);
  461. tmp = mostek_read(regs + MOSTEK_HOUR);
  462. tmp &= ~MSTK_KICK_START;
  463. mostek_write(regs + MOSTEK_HOUR, tmp);
  464. MSTK_SET_REG_SEC(regs,0);
  465. MSTK_SET_REG_MIN(regs,0);
  466. MSTK_SET_REG_HOUR(regs,0);
  467. MSTK_SET_REG_DOW(regs,5);
  468. MSTK_SET_REG_DOM(regs,1);
  469. MSTK_SET_REG_MONTH(regs,8);
  470. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  471. tmp = mostek_read(regs + MOSTEK_CREG);
  472. tmp &= ~MSTK_CREG_WRITE;
  473. mostek_write(regs + MOSTEK_CREG, tmp);
  474. spin_unlock_irq(&mostek_lock);
  475. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  476. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  477. prom_printf("CLOCK: Kick start still on!\n");
  478. spin_lock_irq(&mostek_lock);
  479. tmp = mostek_read(regs + MOSTEK_CREG);
  480. tmp |= MSTK_CREG_WRITE;
  481. mostek_write(regs + MOSTEK_CREG, tmp);
  482. tmp = mostek_read(regs + MOSTEK_HOUR);
  483. tmp &= ~MSTK_KICK_START;
  484. mostek_write(regs + MOSTEK_HOUR, tmp);
  485. tmp = mostek_read(regs + MOSTEK_CREG);
  486. tmp &= ~MSTK_CREG_WRITE;
  487. mostek_write(regs + MOSTEK_CREG, tmp);
  488. spin_unlock_irq(&mostek_lock);
  489. }
  490. prom_printf("CLOCK: Kick start procedure successful.\n");
  491. }
  492. /* Return nonzero if the clock chip battery is low. */
  493. static int __init has_low_battery(void)
  494. {
  495. void __iomem *regs = mstk48t02_regs;
  496. u8 data1, data2;
  497. spin_lock_irq(&mostek_lock);
  498. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  499. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  500. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  501. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  502. spin_unlock_irq(&mostek_lock);
  503. return (data1 == data2); /* Was the write blocked? */
  504. }
  505. /* Probe for the real time clock chip. */
  506. static void __init set_system_time(void)
  507. {
  508. unsigned int year, mon, day, hour, min, sec;
  509. void __iomem *mregs = mstk48t02_regs;
  510. #ifdef CONFIG_PCI
  511. unsigned long dregs = ds1287_regs;
  512. #else
  513. unsigned long dregs = 0UL;
  514. #endif
  515. u8 tmp;
  516. if (!mregs && !dregs) {
  517. prom_printf("Something wrong, clock regs not mapped yet.\n");
  518. prom_halt();
  519. }
  520. if (mregs) {
  521. spin_lock_irq(&mostek_lock);
  522. /* Traditional Mostek chip. */
  523. tmp = mostek_read(mregs + MOSTEK_CREG);
  524. tmp |= MSTK_CREG_READ;
  525. mostek_write(mregs + MOSTEK_CREG, tmp);
  526. sec = MSTK_REG_SEC(mregs);
  527. min = MSTK_REG_MIN(mregs);
  528. hour = MSTK_REG_HOUR(mregs);
  529. day = MSTK_REG_DOM(mregs);
  530. mon = MSTK_REG_MONTH(mregs);
  531. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  532. } else {
  533. /* Dallas 12887 RTC chip. */
  534. do {
  535. sec = CMOS_READ(RTC_SECONDS);
  536. min = CMOS_READ(RTC_MINUTES);
  537. hour = CMOS_READ(RTC_HOURS);
  538. day = CMOS_READ(RTC_DAY_OF_MONTH);
  539. mon = CMOS_READ(RTC_MONTH);
  540. year = CMOS_READ(RTC_YEAR);
  541. } while (sec != CMOS_READ(RTC_SECONDS));
  542. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  543. BCD_TO_BIN(sec);
  544. BCD_TO_BIN(min);
  545. BCD_TO_BIN(hour);
  546. BCD_TO_BIN(day);
  547. BCD_TO_BIN(mon);
  548. BCD_TO_BIN(year);
  549. }
  550. if ((year += 1900) < 1970)
  551. year += 100;
  552. }
  553. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  554. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  555. set_normalized_timespec(&wall_to_monotonic,
  556. -xtime.tv_sec, -xtime.tv_nsec);
  557. if (mregs) {
  558. tmp = mostek_read(mregs + MOSTEK_CREG);
  559. tmp &= ~MSTK_CREG_READ;
  560. mostek_write(mregs + MOSTEK_CREG, tmp);
  561. spin_unlock_irq(&mostek_lock);
  562. }
  563. }
  564. /* davem suggests we keep this within the 4M locked kernel image */
  565. static u32 starfire_get_time(void)
  566. {
  567. static char obp_gettod[32];
  568. static u32 unix_tod;
  569. sprintf(obp_gettod, "h# %08x unix-gettod",
  570. (unsigned int) (long) &unix_tod);
  571. prom_feval(obp_gettod);
  572. return unix_tod;
  573. }
  574. static int starfire_set_time(u32 val)
  575. {
  576. /* Do nothing, time is set using the service processor
  577. * console on this platform.
  578. */
  579. return 0;
  580. }
  581. static u32 hypervisor_get_time(void)
  582. {
  583. register unsigned long func asm("%o5");
  584. register unsigned long arg0 asm("%o0");
  585. register unsigned long arg1 asm("%o1");
  586. int retries = 10000;
  587. retry:
  588. func = HV_FAST_TOD_GET;
  589. arg0 = 0;
  590. arg1 = 0;
  591. __asm__ __volatile__("ta %6"
  592. : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
  593. : "0" (func), "1" (arg0), "2" (arg1),
  594. "i" (HV_FAST_TRAP));
  595. if (arg0 == HV_EOK)
  596. return arg1;
  597. if (arg0 == HV_EWOULDBLOCK) {
  598. if (--retries > 0) {
  599. udelay(100);
  600. goto retry;
  601. }
  602. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  603. return 0;
  604. }
  605. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  606. return 0;
  607. }
  608. static int hypervisor_set_time(u32 secs)
  609. {
  610. register unsigned long func asm("%o5");
  611. register unsigned long arg0 asm("%o0");
  612. int retries = 10000;
  613. retry:
  614. func = HV_FAST_TOD_SET;
  615. arg0 = secs;
  616. __asm__ __volatile__("ta %4"
  617. : "=&r" (func), "=&r" (arg0)
  618. : "0" (func), "1" (arg0),
  619. "i" (HV_FAST_TRAP));
  620. if (arg0 == HV_EOK)
  621. return 0;
  622. if (arg0 == HV_EWOULDBLOCK) {
  623. if (--retries > 0) {
  624. udelay(100);
  625. goto retry;
  626. }
  627. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  628. return -EAGAIN;
  629. }
  630. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  631. return -EOPNOTSUPP;
  632. }
  633. void __init clock_probe(void)
  634. {
  635. struct linux_prom_registers clk_reg[2];
  636. char model[128];
  637. int node, busnd = -1, err;
  638. unsigned long flags;
  639. struct linux_central *cbus;
  640. #ifdef CONFIG_PCI
  641. struct linux_ebus *ebus = NULL;
  642. struct sparc_isa_bridge *isa_br = NULL;
  643. #endif
  644. static int invoked;
  645. if (invoked)
  646. return;
  647. invoked = 1;
  648. if (this_is_starfire) {
  649. xtime.tv_sec = starfire_get_time();
  650. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  651. set_normalized_timespec(&wall_to_monotonic,
  652. -xtime.tv_sec, -xtime.tv_nsec);
  653. return;
  654. }
  655. if (tlb_type == hypervisor) {
  656. xtime.tv_sec = hypervisor_get_time();
  657. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  658. set_normalized_timespec(&wall_to_monotonic,
  659. -xtime.tv_sec, -xtime.tv_nsec);
  660. return;
  661. }
  662. local_irq_save(flags);
  663. cbus = central_bus;
  664. if (cbus != NULL)
  665. busnd = central_bus->child->prom_node;
  666. /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
  667. * That way we handle the presence of multiple properly.
  668. *
  669. * As a special case, machines with Central must provide the
  670. * timer chip there.
  671. */
  672. #ifdef CONFIG_PCI
  673. if (ebus_chain != NULL) {
  674. ebus = ebus_chain;
  675. if (busnd == -1)
  676. busnd = ebus->prom_node;
  677. }
  678. if (isa_chain != NULL) {
  679. isa_br = isa_chain;
  680. if (busnd == -1)
  681. busnd = isa_br->prom_node;
  682. }
  683. #endif
  684. if (sbus_root != NULL && busnd == -1)
  685. busnd = sbus_root->prom_node;
  686. if (busnd == -1) {
  687. prom_printf("clock_probe: problem, cannot find bus to search.\n");
  688. prom_halt();
  689. }
  690. node = prom_getchild(busnd);
  691. while (1) {
  692. if (!node)
  693. model[0] = 0;
  694. else
  695. prom_getstring(node, "model", model, sizeof(model));
  696. if (strcmp(model, "mk48t02") &&
  697. strcmp(model, "mk48t08") &&
  698. strcmp(model, "mk48t59") &&
  699. strcmp(model, "m5819") &&
  700. strcmp(model, "m5819p") &&
  701. strcmp(model, "m5823") &&
  702. strcmp(model, "ds1287")) {
  703. if (cbus != NULL) {
  704. prom_printf("clock_probe: Central bus lacks timer chip.\n");
  705. prom_halt();
  706. }
  707. if (node != 0)
  708. node = prom_getsibling(node);
  709. #ifdef CONFIG_PCI
  710. while ((node == 0) && ebus != NULL) {
  711. ebus = ebus->next;
  712. if (ebus != NULL) {
  713. busnd = ebus->prom_node;
  714. node = prom_getchild(busnd);
  715. }
  716. }
  717. while ((node == 0) && isa_br != NULL) {
  718. isa_br = isa_br->next;
  719. if (isa_br != NULL) {
  720. busnd = isa_br->prom_node;
  721. node = prom_getchild(busnd);
  722. }
  723. }
  724. #endif
  725. if (node == 0) {
  726. prom_printf("clock_probe: Cannot find timer chip\n");
  727. prom_halt();
  728. }
  729. continue;
  730. }
  731. err = prom_getproperty(node, "reg", (char *)clk_reg,
  732. sizeof(clk_reg));
  733. if(err == -1) {
  734. prom_printf("clock_probe: Cannot get Mostek reg property\n");
  735. prom_halt();
  736. }
  737. if (cbus != NULL) {
  738. apply_fhc_ranges(central_bus->child, clk_reg, 1);
  739. apply_central_ranges(central_bus, clk_reg, 1);
  740. }
  741. #ifdef CONFIG_PCI
  742. else if (ebus != NULL) {
  743. struct linux_ebus_device *edev;
  744. for_each_ebusdev(edev, ebus)
  745. if (edev->prom_node == node)
  746. break;
  747. if (edev == NULL) {
  748. if (isa_chain != NULL)
  749. goto try_isa_clock;
  750. prom_printf("%s: Mostek not probed by EBUS\n",
  751. __FUNCTION__);
  752. prom_halt();
  753. }
  754. if (!strcmp(model, "ds1287") ||
  755. !strcmp(model, "m5819") ||
  756. !strcmp(model, "m5819p") ||
  757. !strcmp(model, "m5823")) {
  758. ds1287_regs = edev->resource[0].start;
  759. } else {
  760. mstk48t59_regs = (void __iomem *)
  761. edev->resource[0].start;
  762. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  763. }
  764. break;
  765. }
  766. else if (isa_br != NULL) {
  767. struct sparc_isa_device *isadev;
  768. try_isa_clock:
  769. for_each_isadev(isadev, isa_br)
  770. if (isadev->prom_node == node)
  771. break;
  772. if (isadev == NULL) {
  773. prom_printf("%s: Mostek not probed by ISA\n");
  774. prom_halt();
  775. }
  776. if (!strcmp(model, "ds1287") ||
  777. !strcmp(model, "m5819") ||
  778. !strcmp(model, "m5819p") ||
  779. !strcmp(model, "m5823")) {
  780. ds1287_regs = isadev->resource.start;
  781. } else {
  782. mstk48t59_regs = (void __iomem *)
  783. isadev->resource.start;
  784. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  785. }
  786. break;
  787. }
  788. #endif
  789. else {
  790. if (sbus_root->num_sbus_ranges) {
  791. int nranges = sbus_root->num_sbus_ranges;
  792. int rngc;
  793. for (rngc = 0; rngc < nranges; rngc++)
  794. if (clk_reg[0].which_io ==
  795. sbus_root->sbus_ranges[rngc].ot_child_space)
  796. break;
  797. if (rngc == nranges) {
  798. prom_printf("clock_probe: Cannot find ranges for "
  799. "clock regs.\n");
  800. prom_halt();
  801. }
  802. clk_reg[0].which_io =
  803. sbus_root->sbus_ranges[rngc].ot_parent_space;
  804. clk_reg[0].phys_addr +=
  805. sbus_root->sbus_ranges[rngc].ot_parent_base;
  806. }
  807. }
  808. if(model[5] == '0' && model[6] == '2') {
  809. mstk48t02_regs = (void __iomem *)
  810. (((u64)clk_reg[0].phys_addr) |
  811. (((u64)clk_reg[0].which_io)<<32UL));
  812. } else if(model[5] == '0' && model[6] == '8') {
  813. mstk48t08_regs = (void __iomem *)
  814. (((u64)clk_reg[0].phys_addr) |
  815. (((u64)clk_reg[0].which_io)<<32UL));
  816. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  817. } else {
  818. mstk48t59_regs = (void __iomem *)
  819. (((u64)clk_reg[0].phys_addr) |
  820. (((u64)clk_reg[0].which_io)<<32UL));
  821. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  822. }
  823. break;
  824. }
  825. if (mstk48t02_regs != NULL) {
  826. /* Report a low battery voltage condition. */
  827. if (has_low_battery())
  828. prom_printf("NVRAM: Low battery voltage!\n");
  829. /* Kick start the clock if it is completely stopped. */
  830. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  831. kick_start_clock();
  832. }
  833. set_system_time();
  834. local_irq_restore(flags);
  835. }
  836. /* This is gets the master TICK_INT timer going. */
  837. static unsigned long sparc64_init_timers(void)
  838. {
  839. unsigned long clock;
  840. int node;
  841. #ifdef CONFIG_SMP
  842. extern void smp_tick_init(void);
  843. #endif
  844. if (tlb_type == spitfire) {
  845. unsigned long ver, manuf, impl;
  846. __asm__ __volatile__ ("rdpr %%ver, %0"
  847. : "=&r" (ver));
  848. manuf = ((ver >> 48) & 0xffff);
  849. impl = ((ver >> 32) & 0xffff);
  850. if (manuf == 0x17 && impl == 0x13) {
  851. /* Hummingbird, aka Ultra-IIe */
  852. tick_ops = &hbtick_operations;
  853. node = prom_root_node;
  854. clock = prom_getint(node, "stick-frequency");
  855. } else {
  856. tick_ops = &tick_operations;
  857. cpu_find_by_instance(0, &node, NULL);
  858. clock = prom_getint(node, "clock-frequency");
  859. }
  860. } else {
  861. tick_ops = &stick_operations;
  862. node = prom_root_node;
  863. clock = prom_getint(node, "stick-frequency");
  864. }
  865. timer_tick_offset = clock / HZ;
  866. #ifdef CONFIG_SMP
  867. smp_tick_init();
  868. #endif
  869. return clock;
  870. }
  871. static void sparc64_start_timers(irqreturn_t (*cfunc)(int, void *, struct pt_regs *))
  872. {
  873. unsigned long pstate;
  874. int err;
  875. /* Register IRQ handler. */
  876. err = request_irq(build_irq(0, 0, 0UL, 0UL), cfunc, 0,
  877. "timer", NULL);
  878. if (err) {
  879. prom_printf("Serious problem, cannot register TICK_INT\n");
  880. prom_halt();
  881. }
  882. /* Guarantee that the following sequences execute
  883. * uninterrupted.
  884. */
  885. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  886. "wrpr %0, %1, %%pstate"
  887. : "=r" (pstate)
  888. : "i" (PSTATE_IE));
  889. tick_ops->init_tick(timer_tick_offset);
  890. /* Restore PSTATE_IE. */
  891. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  892. : /* no outputs */
  893. : "r" (pstate));
  894. local_irq_enable();
  895. }
  896. struct freq_table {
  897. unsigned long clock_tick_ref;
  898. unsigned int ref_freq;
  899. };
  900. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  901. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  902. {
  903. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  904. if (ft->clock_tick_ref)
  905. return ft->clock_tick_ref;
  906. return cpu_data(cpu).clock_tick;
  907. }
  908. #ifdef CONFIG_CPU_FREQ
  909. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  910. void *data)
  911. {
  912. struct cpufreq_freqs *freq = data;
  913. unsigned int cpu = freq->cpu;
  914. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  915. if (!ft->ref_freq) {
  916. ft->ref_freq = freq->old;
  917. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  918. }
  919. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  920. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  921. (val == CPUFREQ_RESUMECHANGE)) {
  922. cpu_data(cpu).clock_tick =
  923. cpufreq_scale(ft->clock_tick_ref,
  924. ft->ref_freq,
  925. freq->new);
  926. }
  927. return 0;
  928. }
  929. static struct notifier_block sparc64_cpufreq_notifier_block = {
  930. .notifier_call = sparc64_cpufreq_notifier
  931. };
  932. #endif /* CONFIG_CPU_FREQ */
  933. static struct time_interpolator sparc64_cpu_interpolator = {
  934. .source = TIME_SOURCE_CPU,
  935. .shift = 16,
  936. .mask = 0xffffffffffffffffLL
  937. };
  938. /* The quotient formula is taken from the IA64 port. */
  939. #define SPARC64_NSEC_PER_CYC_SHIFT 30UL
  940. void __init time_init(void)
  941. {
  942. unsigned long clock = sparc64_init_timers();
  943. sparc64_cpu_interpolator.frequency = clock;
  944. register_time_interpolator(&sparc64_cpu_interpolator);
  945. /* Now that the interpolator is registered, it is
  946. * safe to start the timer ticking.
  947. */
  948. sparc64_start_timers(timer_interrupt);
  949. timer_ticks_per_nsec_quotient =
  950. (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
  951. (clock / 2)) / clock);
  952. #ifdef CONFIG_CPU_FREQ
  953. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  954. CPUFREQ_TRANSITION_NOTIFIER);
  955. #endif
  956. }
  957. unsigned long long sched_clock(void)
  958. {
  959. unsigned long ticks = tick_ops->get_tick();
  960. return (ticks * timer_ticks_per_nsec_quotient)
  961. >> SPARC64_NSEC_PER_CYC_SHIFT;
  962. }
  963. static int set_rtc_mmss(unsigned long nowtime)
  964. {
  965. int real_seconds, real_minutes, chip_minutes;
  966. void __iomem *mregs = mstk48t02_regs;
  967. #ifdef CONFIG_PCI
  968. unsigned long dregs = ds1287_regs;
  969. #else
  970. unsigned long dregs = 0UL;
  971. #endif
  972. unsigned long flags;
  973. u8 tmp;
  974. /*
  975. * Not having a register set can lead to trouble.
  976. * Also starfire doesn't have a tod clock.
  977. */
  978. if (!mregs && !dregs)
  979. return -1;
  980. if (mregs) {
  981. spin_lock_irqsave(&mostek_lock, flags);
  982. /* Read the current RTC minutes. */
  983. tmp = mostek_read(mregs + MOSTEK_CREG);
  984. tmp |= MSTK_CREG_READ;
  985. mostek_write(mregs + MOSTEK_CREG, tmp);
  986. chip_minutes = MSTK_REG_MIN(mregs);
  987. tmp = mostek_read(mregs + MOSTEK_CREG);
  988. tmp &= ~MSTK_CREG_READ;
  989. mostek_write(mregs + MOSTEK_CREG, tmp);
  990. /*
  991. * since we're only adjusting minutes and seconds,
  992. * don't interfere with hour overflow. This avoids
  993. * messing with unknown time zones but requires your
  994. * RTC not to be off by more than 15 minutes
  995. */
  996. real_seconds = nowtime % 60;
  997. real_minutes = nowtime / 60;
  998. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  999. real_minutes += 30; /* correct for half hour time zone */
  1000. real_minutes %= 60;
  1001. if (abs(real_minutes - chip_minutes) < 30) {
  1002. tmp = mostek_read(mregs + MOSTEK_CREG);
  1003. tmp |= MSTK_CREG_WRITE;
  1004. mostek_write(mregs + MOSTEK_CREG, tmp);
  1005. MSTK_SET_REG_SEC(mregs,real_seconds);
  1006. MSTK_SET_REG_MIN(mregs,real_minutes);
  1007. tmp = mostek_read(mregs + MOSTEK_CREG);
  1008. tmp &= ~MSTK_CREG_WRITE;
  1009. mostek_write(mregs + MOSTEK_CREG, tmp);
  1010. spin_unlock_irqrestore(&mostek_lock, flags);
  1011. return 0;
  1012. } else {
  1013. spin_unlock_irqrestore(&mostek_lock, flags);
  1014. return -1;
  1015. }
  1016. } else {
  1017. int retval = 0;
  1018. unsigned char save_control, save_freq_select;
  1019. /* Stolen from arch/i386/kernel/time.c, see there for
  1020. * credits and descriptive comments.
  1021. */
  1022. spin_lock_irqsave(&rtc_lock, flags);
  1023. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  1024. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  1025. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  1026. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  1027. chip_minutes = CMOS_READ(RTC_MINUTES);
  1028. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  1029. BCD_TO_BIN(chip_minutes);
  1030. real_seconds = nowtime % 60;
  1031. real_minutes = nowtime / 60;
  1032. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  1033. real_minutes += 30;
  1034. real_minutes %= 60;
  1035. if (abs(real_minutes - chip_minutes) < 30) {
  1036. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1037. BIN_TO_BCD(real_seconds);
  1038. BIN_TO_BCD(real_minutes);
  1039. }
  1040. CMOS_WRITE(real_seconds,RTC_SECONDS);
  1041. CMOS_WRITE(real_minutes,RTC_MINUTES);
  1042. } else {
  1043. printk(KERN_WARNING
  1044. "set_rtc_mmss: can't update from %d to %d\n",
  1045. chip_minutes, real_minutes);
  1046. retval = -1;
  1047. }
  1048. CMOS_WRITE(save_control, RTC_CONTROL);
  1049. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1050. spin_unlock_irqrestore(&rtc_lock, flags);
  1051. return retval;
  1052. }
  1053. }
  1054. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  1055. static unsigned char mini_rtc_status; /* bitmapped status byte. */
  1056. /* months start at 0 now */
  1057. static unsigned char days_in_mo[] =
  1058. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  1059. #define FEBRUARY 2
  1060. #define STARTOFTIME 1970
  1061. #define SECDAY 86400L
  1062. #define SECYR (SECDAY * 365)
  1063. #define leapyear(year) ((year) % 4 == 0 && \
  1064. ((year) % 100 != 0 || (year) % 400 == 0))
  1065. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  1066. #define days_in_month(a) (month_days[(a) - 1])
  1067. static int month_days[12] = {
  1068. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  1069. };
  1070. /*
  1071. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  1072. */
  1073. static void GregorianDay(struct rtc_time * tm)
  1074. {
  1075. int leapsToDate;
  1076. int lastYear;
  1077. int day;
  1078. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1079. lastYear = tm->tm_year - 1;
  1080. /*
  1081. * Number of leap corrections to apply up to end of last year
  1082. */
  1083. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  1084. /*
  1085. * This year is a leap year if it is divisible by 4 except when it is
  1086. * divisible by 100 unless it is divisible by 400
  1087. *
  1088. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  1089. */
  1090. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  1091. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  1092. tm->tm_mday;
  1093. tm->tm_wday = day % 7;
  1094. }
  1095. static void to_tm(int tim, struct rtc_time *tm)
  1096. {
  1097. register int i;
  1098. register long hms, day;
  1099. day = tim / SECDAY;
  1100. hms = tim % SECDAY;
  1101. /* Hours, minutes, seconds are easy */
  1102. tm->tm_hour = hms / 3600;
  1103. tm->tm_min = (hms % 3600) / 60;
  1104. tm->tm_sec = (hms % 3600) % 60;
  1105. /* Number of years in days */
  1106. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  1107. day -= days_in_year(i);
  1108. tm->tm_year = i;
  1109. /* Number of months in days left */
  1110. if (leapyear(tm->tm_year))
  1111. days_in_month(FEBRUARY) = 29;
  1112. for (i = 1; day >= days_in_month(i); i++)
  1113. day -= days_in_month(i);
  1114. days_in_month(FEBRUARY) = 28;
  1115. tm->tm_mon = i;
  1116. /* Days are what is left over (+1) from all that. */
  1117. tm->tm_mday = day + 1;
  1118. /*
  1119. * Determine the day of week
  1120. */
  1121. GregorianDay(tm);
  1122. }
  1123. /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
  1124. * aka Unix time. So we have to convert to/from rtc_time.
  1125. */
  1126. static inline void mini_get_rtc_time(struct rtc_time *time)
  1127. {
  1128. unsigned long flags;
  1129. u32 seconds;
  1130. spin_lock_irqsave(&rtc_lock, flags);
  1131. seconds = 0;
  1132. if (this_is_starfire)
  1133. seconds = starfire_get_time();
  1134. else if (tlb_type == hypervisor)
  1135. seconds = hypervisor_get_time();
  1136. spin_unlock_irqrestore(&rtc_lock, flags);
  1137. to_tm(seconds, time);
  1138. time->tm_year -= 1900;
  1139. time->tm_mon -= 1;
  1140. }
  1141. static inline int mini_set_rtc_time(struct rtc_time *time)
  1142. {
  1143. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1144. time->tm_mday, time->tm_hour,
  1145. time->tm_min, time->tm_sec);
  1146. unsigned long flags;
  1147. int err;
  1148. spin_lock_irqsave(&rtc_lock, flags);
  1149. err = -ENODEV;
  1150. if (this_is_starfire)
  1151. err = starfire_set_time(seconds);
  1152. else if (tlb_type == hypervisor)
  1153. err = hypervisor_set_time(seconds);
  1154. spin_unlock_irqrestore(&rtc_lock, flags);
  1155. return err;
  1156. }
  1157. static int mini_rtc_ioctl(struct inode *inode, struct file *file,
  1158. unsigned int cmd, unsigned long arg)
  1159. {
  1160. struct rtc_time wtime;
  1161. void __user *argp = (void __user *)arg;
  1162. switch (cmd) {
  1163. case RTC_PLL_GET:
  1164. return -EINVAL;
  1165. case RTC_PLL_SET:
  1166. return -EINVAL;
  1167. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  1168. return 0;
  1169. case RTC_UIE_ON: /* enable ints for RTC updates. */
  1170. return -EINVAL;
  1171. case RTC_RD_TIME: /* Read the time/date from RTC */
  1172. /* this doesn't get week-day, who cares */
  1173. memset(&wtime, 0, sizeof(wtime));
  1174. mini_get_rtc_time(&wtime);
  1175. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  1176. case RTC_SET_TIME: /* Set the RTC */
  1177. {
  1178. int year;
  1179. unsigned char leap_yr;
  1180. if (!capable(CAP_SYS_TIME))
  1181. return -EACCES;
  1182. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  1183. return -EFAULT;
  1184. year = wtime.tm_year + 1900;
  1185. leap_yr = ((!(year % 4) && (year % 100)) ||
  1186. !(year % 400));
  1187. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
  1188. return -EINVAL;
  1189. if (wtime.tm_mday < 0 || wtime.tm_mday >
  1190. (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
  1191. return -EINVAL;
  1192. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  1193. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  1194. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  1195. return -EINVAL;
  1196. return mini_set_rtc_time(&wtime);
  1197. }
  1198. }
  1199. return -EINVAL;
  1200. }
  1201. static int mini_rtc_open(struct inode *inode, struct file *file)
  1202. {
  1203. if (mini_rtc_status & RTC_IS_OPEN)
  1204. return -EBUSY;
  1205. mini_rtc_status |= RTC_IS_OPEN;
  1206. return 0;
  1207. }
  1208. static int mini_rtc_release(struct inode *inode, struct file *file)
  1209. {
  1210. mini_rtc_status &= ~RTC_IS_OPEN;
  1211. return 0;
  1212. }
  1213. static struct file_operations mini_rtc_fops = {
  1214. .owner = THIS_MODULE,
  1215. .ioctl = mini_rtc_ioctl,
  1216. .open = mini_rtc_open,
  1217. .release = mini_rtc_release,
  1218. };
  1219. static struct miscdevice rtc_mini_dev =
  1220. {
  1221. .minor = RTC_MINOR,
  1222. .name = "rtc",
  1223. .fops = &mini_rtc_fops,
  1224. };
  1225. static int __init rtc_mini_init(void)
  1226. {
  1227. int retval;
  1228. if (tlb_type != hypervisor && !this_is_starfire)
  1229. return -ENODEV;
  1230. printk(KERN_INFO "Mini RTC Driver\n");
  1231. retval = misc_register(&rtc_mini_dev);
  1232. if (retval < 0)
  1233. return retval;
  1234. return 0;
  1235. }
  1236. static void __exit rtc_mini_exit(void)
  1237. {
  1238. misc_deregister(&rtc_mini_dev);
  1239. }
  1240. module_init(rtc_mini_init);
  1241. module_exit(rtc_mini_exit);