time.c 35 KB

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