time.c 33 KB

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