time.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. int i;
  534. /* Dallas 12887 RTC chip. */
  535. /* Stolen from arch/i386/kernel/time.c, see there for
  536. * credits and descriptive comments.
  537. */
  538. for (i = 0; i < 1000000; i++) {
  539. if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
  540. break;
  541. udelay(10);
  542. }
  543. for (i = 0; i < 1000000; i++) {
  544. if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  545. break;
  546. udelay(10);
  547. }
  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. void __init clock_probe(void)
  648. {
  649. struct linux_prom_registers clk_reg[2];
  650. char model[128];
  651. int node, busnd = -1, err;
  652. unsigned long flags;
  653. struct linux_central *cbus;
  654. #ifdef CONFIG_PCI
  655. struct linux_ebus *ebus = NULL;
  656. struct sparc_isa_bridge *isa_br = NULL;
  657. #endif
  658. static int invoked;
  659. if (invoked)
  660. return;
  661. invoked = 1;
  662. if (this_is_starfire) {
  663. xtime.tv_sec = starfire_get_time();
  664. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  665. set_normalized_timespec(&wall_to_monotonic,
  666. -xtime.tv_sec, -xtime.tv_nsec);
  667. return;
  668. }
  669. if (tlb_type == hypervisor) {
  670. xtime.tv_sec = hypervisor_get_time();
  671. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  672. set_normalized_timespec(&wall_to_monotonic,
  673. -xtime.tv_sec, -xtime.tv_nsec);
  674. return;
  675. }
  676. local_irq_save(flags);
  677. cbus = central_bus;
  678. if (cbus != NULL)
  679. busnd = central_bus->child->prom_node;
  680. /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
  681. * That way we handle the presence of multiple properly.
  682. *
  683. * As a special case, machines with Central must provide the
  684. * timer chip there.
  685. */
  686. #ifdef CONFIG_PCI
  687. if (ebus_chain != NULL) {
  688. ebus = ebus_chain;
  689. if (busnd == -1)
  690. busnd = ebus->prom_node;
  691. }
  692. if (isa_chain != NULL) {
  693. isa_br = isa_chain;
  694. if (busnd == -1)
  695. busnd = isa_br->prom_node;
  696. }
  697. #endif
  698. if (sbus_root != NULL && busnd == -1)
  699. busnd = sbus_root->prom_node;
  700. if (busnd == -1) {
  701. prom_printf("clock_probe: problem, cannot find bus to search.\n");
  702. prom_halt();
  703. }
  704. node = prom_getchild(busnd);
  705. while (1) {
  706. if (!node)
  707. model[0] = 0;
  708. else
  709. prom_getstring(node, "model", model, sizeof(model));
  710. if (strcmp(model, "mk48t02") &&
  711. strcmp(model, "mk48t08") &&
  712. strcmp(model, "mk48t59") &&
  713. strcmp(model, "m5819") &&
  714. strcmp(model, "m5819p") &&
  715. strcmp(model, "m5823") &&
  716. strcmp(model, "ds1287")) {
  717. if (cbus != NULL) {
  718. prom_printf("clock_probe: Central bus lacks timer chip.\n");
  719. prom_halt();
  720. }
  721. if (node != 0)
  722. node = prom_getsibling(node);
  723. #ifdef CONFIG_PCI
  724. while ((node == 0) && ebus != NULL) {
  725. ebus = ebus->next;
  726. if (ebus != NULL) {
  727. busnd = ebus->prom_node;
  728. node = prom_getchild(busnd);
  729. }
  730. }
  731. while ((node == 0) && isa_br != NULL) {
  732. isa_br = isa_br->next;
  733. if (isa_br != NULL) {
  734. busnd = isa_br->prom_node;
  735. node = prom_getchild(busnd);
  736. }
  737. }
  738. #endif
  739. if (node == 0) {
  740. prom_printf("clock_probe: Cannot find timer chip\n");
  741. prom_halt();
  742. }
  743. continue;
  744. }
  745. err = prom_getproperty(node, "reg", (char *)clk_reg,
  746. sizeof(clk_reg));
  747. if(err == -1) {
  748. prom_printf("clock_probe: Cannot get Mostek reg property\n");
  749. prom_halt();
  750. }
  751. if (cbus != NULL) {
  752. apply_fhc_ranges(central_bus->child, clk_reg, 1);
  753. apply_central_ranges(central_bus, clk_reg, 1);
  754. }
  755. #ifdef CONFIG_PCI
  756. else if (ebus != NULL) {
  757. struct linux_ebus_device *edev;
  758. for_each_ebusdev(edev, ebus)
  759. if (edev->prom_node == node)
  760. break;
  761. if (edev == NULL) {
  762. if (isa_chain != NULL)
  763. goto try_isa_clock;
  764. prom_printf("%s: Mostek not probed by EBUS\n",
  765. __FUNCTION__);
  766. prom_halt();
  767. }
  768. if (!strcmp(model, "ds1287") ||
  769. !strcmp(model, "m5819") ||
  770. !strcmp(model, "m5819p") ||
  771. !strcmp(model, "m5823")) {
  772. ds1287_regs = edev->resource[0].start;
  773. } else {
  774. mstk48t59_regs = (void __iomem *)
  775. edev->resource[0].start;
  776. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  777. }
  778. break;
  779. }
  780. else if (isa_br != NULL) {
  781. struct sparc_isa_device *isadev;
  782. try_isa_clock:
  783. for_each_isadev(isadev, isa_br)
  784. if (isadev->prom_node == node)
  785. break;
  786. if (isadev == NULL) {
  787. prom_printf("%s: Mostek not probed by ISA\n");
  788. prom_halt();
  789. }
  790. if (!strcmp(model, "ds1287") ||
  791. !strcmp(model, "m5819") ||
  792. !strcmp(model, "m5819p") ||
  793. !strcmp(model, "m5823")) {
  794. ds1287_regs = isadev->resource.start;
  795. } else {
  796. mstk48t59_regs = (void __iomem *)
  797. isadev->resource.start;
  798. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  799. }
  800. break;
  801. }
  802. #endif
  803. else {
  804. if (sbus_root->num_sbus_ranges) {
  805. int nranges = sbus_root->num_sbus_ranges;
  806. int rngc;
  807. for (rngc = 0; rngc < nranges; rngc++)
  808. if (clk_reg[0].which_io ==
  809. sbus_root->sbus_ranges[rngc].ot_child_space)
  810. break;
  811. if (rngc == nranges) {
  812. prom_printf("clock_probe: Cannot find ranges for "
  813. "clock regs.\n");
  814. prom_halt();
  815. }
  816. clk_reg[0].which_io =
  817. sbus_root->sbus_ranges[rngc].ot_parent_space;
  818. clk_reg[0].phys_addr +=
  819. sbus_root->sbus_ranges[rngc].ot_parent_base;
  820. }
  821. }
  822. if(model[5] == '0' && model[6] == '2') {
  823. mstk48t02_regs = (void __iomem *)
  824. (((u64)clk_reg[0].phys_addr) |
  825. (((u64)clk_reg[0].which_io)<<32UL));
  826. } else if(model[5] == '0' && model[6] == '8') {
  827. mstk48t08_regs = (void __iomem *)
  828. (((u64)clk_reg[0].phys_addr) |
  829. (((u64)clk_reg[0].which_io)<<32UL));
  830. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  831. } else {
  832. mstk48t59_regs = (void __iomem *)
  833. (((u64)clk_reg[0].phys_addr) |
  834. (((u64)clk_reg[0].which_io)<<32UL));
  835. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  836. }
  837. break;
  838. }
  839. if (mstk48t02_regs != NULL) {
  840. /* Report a low battery voltage condition. */
  841. if (has_low_battery())
  842. prom_printf("NVRAM: Low battery voltage!\n");
  843. /* Kick start the clock if it is completely stopped. */
  844. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  845. kick_start_clock();
  846. }
  847. set_system_time();
  848. local_irq_restore(flags);
  849. }
  850. /* This is gets the master TICK_INT timer going. */
  851. static unsigned long sparc64_init_timers(void)
  852. {
  853. unsigned long clock;
  854. int node;
  855. #ifdef CONFIG_SMP
  856. extern void smp_tick_init(void);
  857. #endif
  858. if (tlb_type == spitfire) {
  859. unsigned long ver, manuf, impl;
  860. __asm__ __volatile__ ("rdpr %%ver, %0"
  861. : "=&r" (ver));
  862. manuf = ((ver >> 48) & 0xffff);
  863. impl = ((ver >> 32) & 0xffff);
  864. if (manuf == 0x17 && impl == 0x13) {
  865. /* Hummingbird, aka Ultra-IIe */
  866. tick_ops = &hbtick_operations;
  867. node = prom_root_node;
  868. clock = prom_getint(node, "stick-frequency");
  869. } else {
  870. tick_ops = &tick_operations;
  871. cpu_find_by_instance(0, &node, NULL);
  872. clock = prom_getint(node, "clock-frequency");
  873. }
  874. } else {
  875. tick_ops = &stick_operations;
  876. node = prom_root_node;
  877. clock = prom_getint(node, "stick-frequency");
  878. }
  879. timer_tick_offset = clock / HZ;
  880. #ifdef CONFIG_SMP
  881. smp_tick_init();
  882. #endif
  883. return clock;
  884. }
  885. static void sparc64_start_timers(irqreturn_t (*cfunc)(int, void *, struct pt_regs *))
  886. {
  887. unsigned long pstate;
  888. int err;
  889. /* Register IRQ handler. */
  890. err = request_irq(build_irq(0, 0, 0UL, 0UL), cfunc, 0,
  891. "timer", NULL);
  892. if (err) {
  893. prom_printf("Serious problem, cannot register TICK_INT\n");
  894. prom_halt();
  895. }
  896. /* Guarantee that the following sequences execute
  897. * uninterrupted.
  898. */
  899. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  900. "wrpr %0, %1, %%pstate"
  901. : "=r" (pstate)
  902. : "i" (PSTATE_IE));
  903. tick_ops->init_tick(timer_tick_offset);
  904. /* Restore PSTATE_IE. */
  905. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  906. : /* no outputs */
  907. : "r" (pstate));
  908. local_irq_enable();
  909. }
  910. struct freq_table {
  911. unsigned long clock_tick_ref;
  912. unsigned int ref_freq;
  913. };
  914. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  915. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  916. {
  917. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  918. if (ft->clock_tick_ref)
  919. return ft->clock_tick_ref;
  920. return cpu_data(cpu).clock_tick;
  921. }
  922. #ifdef CONFIG_CPU_FREQ
  923. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  924. void *data)
  925. {
  926. struct cpufreq_freqs *freq = data;
  927. unsigned int cpu = freq->cpu;
  928. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  929. if (!ft->ref_freq) {
  930. ft->ref_freq = freq->old;
  931. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  932. }
  933. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  934. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  935. (val == CPUFREQ_RESUMECHANGE)) {
  936. cpu_data(cpu).clock_tick =
  937. cpufreq_scale(ft->clock_tick_ref,
  938. ft->ref_freq,
  939. freq->new);
  940. }
  941. return 0;
  942. }
  943. static struct notifier_block sparc64_cpufreq_notifier_block = {
  944. .notifier_call = sparc64_cpufreq_notifier
  945. };
  946. #endif /* CONFIG_CPU_FREQ */
  947. static struct time_interpolator sparc64_cpu_interpolator = {
  948. .source = TIME_SOURCE_CPU,
  949. .shift = 16,
  950. .mask = 0xffffffffffffffffLL
  951. };
  952. /* The quotient formula is taken from the IA64 port. */
  953. #define SPARC64_NSEC_PER_CYC_SHIFT 30UL
  954. void __init time_init(void)
  955. {
  956. unsigned long clock = sparc64_init_timers();
  957. sparc64_cpu_interpolator.frequency = clock;
  958. register_time_interpolator(&sparc64_cpu_interpolator);
  959. /* Now that the interpolator is registered, it is
  960. * safe to start the timer ticking.
  961. */
  962. sparc64_start_timers(timer_interrupt);
  963. timer_ticks_per_nsec_quotient =
  964. (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
  965. (clock / 2)) / clock);
  966. #ifdef CONFIG_CPU_FREQ
  967. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  968. CPUFREQ_TRANSITION_NOTIFIER);
  969. #endif
  970. }
  971. unsigned long long sched_clock(void)
  972. {
  973. unsigned long ticks = tick_ops->get_tick();
  974. return (ticks * timer_ticks_per_nsec_quotient)
  975. >> SPARC64_NSEC_PER_CYC_SHIFT;
  976. }
  977. static int set_rtc_mmss(unsigned long nowtime)
  978. {
  979. int real_seconds, real_minutes, chip_minutes;
  980. void __iomem *mregs = mstk48t02_regs;
  981. #ifdef CONFIG_PCI
  982. unsigned long dregs = ds1287_regs;
  983. #else
  984. unsigned long dregs = 0UL;
  985. #endif
  986. unsigned long flags;
  987. u8 tmp;
  988. /*
  989. * Not having a register set can lead to trouble.
  990. * Also starfire doesn't have a tod clock.
  991. */
  992. if (!mregs && !dregs)
  993. return -1;
  994. if (mregs) {
  995. spin_lock_irqsave(&mostek_lock, flags);
  996. /* Read the current RTC minutes. */
  997. tmp = mostek_read(mregs + MOSTEK_CREG);
  998. tmp |= MSTK_CREG_READ;
  999. mostek_write(mregs + MOSTEK_CREG, tmp);
  1000. chip_minutes = MSTK_REG_MIN(mregs);
  1001. tmp = mostek_read(mregs + MOSTEK_CREG);
  1002. tmp &= ~MSTK_CREG_READ;
  1003. mostek_write(mregs + MOSTEK_CREG, tmp);
  1004. /*
  1005. * since we're only adjusting minutes and seconds,
  1006. * don't interfere with hour overflow. This avoids
  1007. * messing with unknown time zones but requires your
  1008. * RTC not to be off by more than 15 minutes
  1009. */
  1010. real_seconds = nowtime % 60;
  1011. real_minutes = nowtime / 60;
  1012. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  1013. real_minutes += 30; /* correct for half hour time zone */
  1014. real_minutes %= 60;
  1015. if (abs(real_minutes - chip_minutes) < 30) {
  1016. tmp = mostek_read(mregs + MOSTEK_CREG);
  1017. tmp |= MSTK_CREG_WRITE;
  1018. mostek_write(mregs + MOSTEK_CREG, tmp);
  1019. MSTK_SET_REG_SEC(mregs,real_seconds);
  1020. MSTK_SET_REG_MIN(mregs,real_minutes);
  1021. tmp = mostek_read(mregs + MOSTEK_CREG);
  1022. tmp &= ~MSTK_CREG_WRITE;
  1023. mostek_write(mregs + MOSTEK_CREG, tmp);
  1024. spin_unlock_irqrestore(&mostek_lock, flags);
  1025. return 0;
  1026. } else {
  1027. spin_unlock_irqrestore(&mostek_lock, flags);
  1028. return -1;
  1029. }
  1030. } else {
  1031. int retval = 0;
  1032. unsigned char save_control, save_freq_select;
  1033. /* Stolen from arch/i386/kernel/time.c, see there for
  1034. * credits and descriptive comments.
  1035. */
  1036. spin_lock_irqsave(&rtc_lock, flags);
  1037. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  1038. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  1039. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  1040. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  1041. chip_minutes = CMOS_READ(RTC_MINUTES);
  1042. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  1043. BCD_TO_BIN(chip_minutes);
  1044. real_seconds = nowtime % 60;
  1045. real_minutes = nowtime / 60;
  1046. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  1047. real_minutes += 30;
  1048. real_minutes %= 60;
  1049. if (abs(real_minutes - chip_minutes) < 30) {
  1050. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1051. BIN_TO_BCD(real_seconds);
  1052. BIN_TO_BCD(real_minutes);
  1053. }
  1054. CMOS_WRITE(real_seconds,RTC_SECONDS);
  1055. CMOS_WRITE(real_minutes,RTC_MINUTES);
  1056. } else {
  1057. printk(KERN_WARNING
  1058. "set_rtc_mmss: can't update from %d to %d\n",
  1059. chip_minutes, real_minutes);
  1060. retval = -1;
  1061. }
  1062. CMOS_WRITE(save_control, RTC_CONTROL);
  1063. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1064. spin_unlock_irqrestore(&rtc_lock, flags);
  1065. return retval;
  1066. }
  1067. }
  1068. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  1069. static unsigned char mini_rtc_status; /* bitmapped status byte. */
  1070. /* months start at 0 now */
  1071. static unsigned char days_in_mo[] =
  1072. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  1073. #define FEBRUARY 2
  1074. #define STARTOFTIME 1970
  1075. #define SECDAY 86400L
  1076. #define SECYR (SECDAY * 365)
  1077. #define leapyear(year) ((year) % 4 == 0 && \
  1078. ((year) % 100 != 0 || (year) % 400 == 0))
  1079. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  1080. #define days_in_month(a) (month_days[(a) - 1])
  1081. static int month_days[12] = {
  1082. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  1083. };
  1084. /*
  1085. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  1086. */
  1087. static void GregorianDay(struct rtc_time * tm)
  1088. {
  1089. int leapsToDate;
  1090. int lastYear;
  1091. int day;
  1092. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1093. lastYear = tm->tm_year - 1;
  1094. /*
  1095. * Number of leap corrections to apply up to end of last year
  1096. */
  1097. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  1098. /*
  1099. * This year is a leap year if it is divisible by 4 except when it is
  1100. * divisible by 100 unless it is divisible by 400
  1101. *
  1102. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  1103. */
  1104. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  1105. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  1106. tm->tm_mday;
  1107. tm->tm_wday = day % 7;
  1108. }
  1109. static void to_tm(int tim, struct rtc_time *tm)
  1110. {
  1111. register int i;
  1112. register long hms, day;
  1113. day = tim / SECDAY;
  1114. hms = tim % SECDAY;
  1115. /* Hours, minutes, seconds are easy */
  1116. tm->tm_hour = hms / 3600;
  1117. tm->tm_min = (hms % 3600) / 60;
  1118. tm->tm_sec = (hms % 3600) % 60;
  1119. /* Number of years in days */
  1120. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  1121. day -= days_in_year(i);
  1122. tm->tm_year = i;
  1123. /* Number of months in days left */
  1124. if (leapyear(tm->tm_year))
  1125. days_in_month(FEBRUARY) = 29;
  1126. for (i = 1; day >= days_in_month(i); i++)
  1127. day -= days_in_month(i);
  1128. days_in_month(FEBRUARY) = 28;
  1129. tm->tm_mon = i;
  1130. /* Days are what is left over (+1) from all that. */
  1131. tm->tm_mday = day + 1;
  1132. /*
  1133. * Determine the day of week
  1134. */
  1135. GregorianDay(tm);
  1136. }
  1137. /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
  1138. * aka Unix time. So we have to convert to/from rtc_time.
  1139. */
  1140. static inline void mini_get_rtc_time(struct rtc_time *time)
  1141. {
  1142. unsigned long flags;
  1143. u32 seconds;
  1144. spin_lock_irqsave(&rtc_lock, flags);
  1145. seconds = 0;
  1146. if (this_is_starfire)
  1147. seconds = starfire_get_time();
  1148. else if (tlb_type == hypervisor)
  1149. seconds = hypervisor_get_time();
  1150. spin_unlock_irqrestore(&rtc_lock, flags);
  1151. to_tm(seconds, time);
  1152. time->tm_year -= 1900;
  1153. time->tm_mon -= 1;
  1154. }
  1155. static inline int mini_set_rtc_time(struct rtc_time *time)
  1156. {
  1157. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1158. time->tm_mday, time->tm_hour,
  1159. time->tm_min, time->tm_sec);
  1160. unsigned long flags;
  1161. int err;
  1162. spin_lock_irqsave(&rtc_lock, flags);
  1163. err = -ENODEV;
  1164. if (this_is_starfire)
  1165. err = starfire_set_time(seconds);
  1166. else if (tlb_type == hypervisor)
  1167. err = hypervisor_set_time(seconds);
  1168. spin_unlock_irqrestore(&rtc_lock, flags);
  1169. return err;
  1170. }
  1171. static int mini_rtc_ioctl(struct inode *inode, struct file *file,
  1172. unsigned int cmd, unsigned long arg)
  1173. {
  1174. struct rtc_time wtime;
  1175. void __user *argp = (void __user *)arg;
  1176. switch (cmd) {
  1177. case RTC_PLL_GET:
  1178. return -EINVAL;
  1179. case RTC_PLL_SET:
  1180. return -EINVAL;
  1181. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  1182. return 0;
  1183. case RTC_UIE_ON: /* enable ints for RTC updates. */
  1184. return -EINVAL;
  1185. case RTC_RD_TIME: /* Read the time/date from RTC */
  1186. /* this doesn't get week-day, who cares */
  1187. memset(&wtime, 0, sizeof(wtime));
  1188. mini_get_rtc_time(&wtime);
  1189. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  1190. case RTC_SET_TIME: /* Set the RTC */
  1191. {
  1192. int year;
  1193. unsigned char leap_yr;
  1194. if (!capable(CAP_SYS_TIME))
  1195. return -EACCES;
  1196. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  1197. return -EFAULT;
  1198. year = wtime.tm_year + 1900;
  1199. leap_yr = ((!(year % 4) && (year % 100)) ||
  1200. !(year % 400));
  1201. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
  1202. return -EINVAL;
  1203. if (wtime.tm_mday < 0 || wtime.tm_mday >
  1204. (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
  1205. return -EINVAL;
  1206. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  1207. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  1208. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  1209. return -EINVAL;
  1210. return mini_set_rtc_time(&wtime);
  1211. }
  1212. }
  1213. return -EINVAL;
  1214. }
  1215. static int mini_rtc_open(struct inode *inode, struct file *file)
  1216. {
  1217. if (mini_rtc_status & RTC_IS_OPEN)
  1218. return -EBUSY;
  1219. mini_rtc_status |= RTC_IS_OPEN;
  1220. return 0;
  1221. }
  1222. static int mini_rtc_release(struct inode *inode, struct file *file)
  1223. {
  1224. mini_rtc_status &= ~RTC_IS_OPEN;
  1225. return 0;
  1226. }
  1227. static struct file_operations mini_rtc_fops = {
  1228. .owner = THIS_MODULE,
  1229. .ioctl = mini_rtc_ioctl,
  1230. .open = mini_rtc_open,
  1231. .release = mini_rtc_release,
  1232. };
  1233. static struct miscdevice rtc_mini_dev =
  1234. {
  1235. .minor = RTC_MINOR,
  1236. .name = "rtc",
  1237. .fops = &mini_rtc_fops,
  1238. };
  1239. static int __init rtc_mini_init(void)
  1240. {
  1241. int retval;
  1242. if (tlb_type != hypervisor && !this_is_starfire)
  1243. return -ENODEV;
  1244. printk(KERN_INFO "Mini RTC Driver\n");
  1245. retval = misc_register(&rtc_mini_dev);
  1246. if (retval < 0)
  1247. return retval;
  1248. return 0;
  1249. }
  1250. static void __exit rtc_mini_exit(void)
  1251. {
  1252. misc_deregister(&rtc_mini_dev);
  1253. }
  1254. module_init(rtc_mini_init);
  1255. module_exit(rtc_mini_exit);