time.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  388. {
  389. unsigned long ticks, compare, pstate;
  390. write_seqlock(&xtime_lock);
  391. do {
  392. #ifndef CONFIG_SMP
  393. profile_tick(CPU_PROFILING, regs);
  394. update_process_times(user_mode(regs));
  395. #endif
  396. do_timer(regs);
  397. /* Guarantee that the following sequences execute
  398. * uninterrupted.
  399. */
  400. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  401. "wrpr %0, %1, %%pstate"
  402. : "=r" (pstate)
  403. : "i" (PSTATE_IE));
  404. compare = tick_ops->add_compare(timer_tick_offset);
  405. ticks = tick_ops->get_tick();
  406. /* Restore PSTATE_IE. */
  407. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  408. : /* no outputs */
  409. : "r" (pstate));
  410. } while (time_after_eq(ticks, compare));
  411. timer_check_rtc();
  412. write_sequnlock(&xtime_lock);
  413. return IRQ_HANDLED;
  414. }
  415. #ifdef CONFIG_SMP
  416. void timer_tick_interrupt(struct pt_regs *regs)
  417. {
  418. write_seqlock(&xtime_lock);
  419. do_timer(regs);
  420. timer_check_rtc();
  421. write_sequnlock(&xtime_lock);
  422. }
  423. #endif
  424. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  425. static void __init kick_start_clock(void)
  426. {
  427. void __iomem *regs = mstk48t02_regs;
  428. u8 sec, tmp;
  429. int i, count;
  430. prom_printf("CLOCK: Clock was stopped. Kick start ");
  431. spin_lock_irq(&mostek_lock);
  432. /* Turn on the kick start bit to start the oscillator. */
  433. tmp = mostek_read(regs + MOSTEK_CREG);
  434. tmp |= MSTK_CREG_WRITE;
  435. mostek_write(regs + MOSTEK_CREG, tmp);
  436. tmp = mostek_read(regs + MOSTEK_SEC);
  437. tmp &= ~MSTK_STOP;
  438. mostek_write(regs + MOSTEK_SEC, tmp);
  439. tmp = mostek_read(regs + MOSTEK_HOUR);
  440. tmp |= MSTK_KICK_START;
  441. mostek_write(regs + MOSTEK_HOUR, tmp);
  442. tmp = mostek_read(regs + MOSTEK_CREG);
  443. tmp &= ~MSTK_CREG_WRITE;
  444. mostek_write(regs + MOSTEK_CREG, tmp);
  445. spin_unlock_irq(&mostek_lock);
  446. /* Delay to allow the clock oscillator to start. */
  447. sec = MSTK_REG_SEC(regs);
  448. for (i = 0; i < 3; i++) {
  449. while (sec == MSTK_REG_SEC(regs))
  450. for (count = 0; count < 100000; count++)
  451. /* nothing */ ;
  452. prom_printf(".");
  453. sec = MSTK_REG_SEC(regs);
  454. }
  455. prom_printf("\n");
  456. spin_lock_irq(&mostek_lock);
  457. /* Turn off kick start and set a "valid" time and date. */
  458. tmp = mostek_read(regs + MOSTEK_CREG);
  459. tmp |= MSTK_CREG_WRITE;
  460. mostek_write(regs + MOSTEK_CREG, tmp);
  461. tmp = mostek_read(regs + MOSTEK_HOUR);
  462. tmp &= ~MSTK_KICK_START;
  463. mostek_write(regs + MOSTEK_HOUR, tmp);
  464. MSTK_SET_REG_SEC(regs,0);
  465. MSTK_SET_REG_MIN(regs,0);
  466. MSTK_SET_REG_HOUR(regs,0);
  467. MSTK_SET_REG_DOW(regs,5);
  468. MSTK_SET_REG_DOM(regs,1);
  469. MSTK_SET_REG_MONTH(regs,8);
  470. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  471. tmp = mostek_read(regs + MOSTEK_CREG);
  472. tmp &= ~MSTK_CREG_WRITE;
  473. mostek_write(regs + MOSTEK_CREG, tmp);
  474. spin_unlock_irq(&mostek_lock);
  475. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  476. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  477. prom_printf("CLOCK: Kick start still on!\n");
  478. spin_lock_irq(&mostek_lock);
  479. tmp = mostek_read(regs + MOSTEK_CREG);
  480. tmp |= MSTK_CREG_WRITE;
  481. mostek_write(regs + MOSTEK_CREG, tmp);
  482. tmp = mostek_read(regs + MOSTEK_HOUR);
  483. tmp &= ~MSTK_KICK_START;
  484. mostek_write(regs + MOSTEK_HOUR, tmp);
  485. tmp = mostek_read(regs + MOSTEK_CREG);
  486. tmp &= ~MSTK_CREG_WRITE;
  487. mostek_write(regs + MOSTEK_CREG, tmp);
  488. spin_unlock_irq(&mostek_lock);
  489. }
  490. prom_printf("CLOCK: Kick start procedure successful.\n");
  491. }
  492. /* Return nonzero if the clock chip battery is low. */
  493. static int __init has_low_battery(void)
  494. {
  495. void __iomem *regs = mstk48t02_regs;
  496. u8 data1, data2;
  497. spin_lock_irq(&mostek_lock);
  498. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  499. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  500. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  501. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  502. spin_unlock_irq(&mostek_lock);
  503. return (data1 == data2); /* Was the write blocked? */
  504. }
  505. /* Probe for the real time clock chip. */
  506. static void __init set_system_time(void)
  507. {
  508. unsigned int year, mon, day, hour, min, sec;
  509. void __iomem *mregs = mstk48t02_regs;
  510. #ifdef CONFIG_PCI
  511. unsigned long dregs = ds1287_regs;
  512. #else
  513. unsigned long dregs = 0UL;
  514. #endif
  515. u8 tmp;
  516. if (!mregs && !dregs) {
  517. prom_printf("Something wrong, clock regs not mapped yet.\n");
  518. prom_halt();
  519. }
  520. if (mregs) {
  521. spin_lock_irq(&mostek_lock);
  522. /* Traditional Mostek chip. */
  523. tmp = mostek_read(mregs + MOSTEK_CREG);
  524. tmp |= MSTK_CREG_READ;
  525. mostek_write(mregs + MOSTEK_CREG, tmp);
  526. sec = MSTK_REG_SEC(mregs);
  527. min = MSTK_REG_MIN(mregs);
  528. hour = MSTK_REG_HOUR(mregs);
  529. day = MSTK_REG_DOM(mregs);
  530. mon = MSTK_REG_MONTH(mregs);
  531. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  532. } else {
  533. /* Dallas 12887 RTC chip. */
  534. do {
  535. sec = CMOS_READ(RTC_SECONDS);
  536. min = CMOS_READ(RTC_MINUTES);
  537. hour = CMOS_READ(RTC_HOURS);
  538. day = CMOS_READ(RTC_DAY_OF_MONTH);
  539. mon = CMOS_READ(RTC_MONTH);
  540. year = CMOS_READ(RTC_YEAR);
  541. } while (sec != CMOS_READ(RTC_SECONDS));
  542. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  543. BCD_TO_BIN(sec);
  544. BCD_TO_BIN(min);
  545. BCD_TO_BIN(hour);
  546. BCD_TO_BIN(day);
  547. BCD_TO_BIN(mon);
  548. BCD_TO_BIN(year);
  549. }
  550. if ((year += 1900) < 1970)
  551. year += 100;
  552. }
  553. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  554. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  555. set_normalized_timespec(&wall_to_monotonic,
  556. -xtime.tv_sec, -xtime.tv_nsec);
  557. if (mregs) {
  558. tmp = mostek_read(mregs + MOSTEK_CREG);
  559. tmp &= ~MSTK_CREG_READ;
  560. mostek_write(mregs + MOSTEK_CREG, tmp);
  561. spin_unlock_irq(&mostek_lock);
  562. }
  563. }
  564. /* davem suggests we keep this within the 4M locked kernel image */
  565. static u32 starfire_get_time(void)
  566. {
  567. static char obp_gettod[32];
  568. static u32 unix_tod;
  569. sprintf(obp_gettod, "h# %08x unix-gettod",
  570. (unsigned int) (long) &unix_tod);
  571. prom_feval(obp_gettod);
  572. return unix_tod;
  573. }
  574. static int starfire_set_time(u32 val)
  575. {
  576. /* Do nothing, time is set using the service processor
  577. * console on this platform.
  578. */
  579. return 0;
  580. }
  581. static u32 hypervisor_get_time(void)
  582. {
  583. register unsigned long func asm("%o5");
  584. register unsigned long arg0 asm("%o0");
  585. register unsigned long arg1 asm("%o1");
  586. int retries = 10000;
  587. retry:
  588. func = HV_FAST_TOD_GET;
  589. arg0 = 0;
  590. arg1 = 0;
  591. __asm__ __volatile__("ta %6"
  592. : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
  593. : "0" (func), "1" (arg0), "2" (arg1),
  594. "i" (HV_FAST_TRAP));
  595. if (arg0 == HV_EOK)
  596. return arg1;
  597. if (arg0 == HV_EWOULDBLOCK) {
  598. if (--retries > 0) {
  599. udelay(100);
  600. goto retry;
  601. }
  602. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  603. return 0;
  604. }
  605. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  606. return 0;
  607. }
  608. static int hypervisor_set_time(u32 secs)
  609. {
  610. register unsigned long func asm("%o5");
  611. register unsigned long arg0 asm("%o0");
  612. int retries = 10000;
  613. retry:
  614. func = HV_FAST_TOD_SET;
  615. arg0 = secs;
  616. __asm__ __volatile__("ta %4"
  617. : "=&r" (func), "=&r" (arg0)
  618. : "0" (func), "1" (arg0),
  619. "i" (HV_FAST_TRAP));
  620. if (arg0 == HV_EOK)
  621. return 0;
  622. if (arg0 == HV_EWOULDBLOCK) {
  623. if (--retries > 0) {
  624. udelay(100);
  625. goto retry;
  626. }
  627. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  628. return -EAGAIN;
  629. }
  630. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  631. return -EOPNOTSUPP;
  632. }
  633. void __init clock_probe(void)
  634. {
  635. struct linux_prom_registers clk_reg[2];
  636. char model[128];
  637. int node, busnd = -1, err;
  638. unsigned long flags;
  639. struct linux_central *cbus;
  640. #ifdef CONFIG_PCI
  641. struct linux_ebus *ebus = NULL;
  642. struct sparc_isa_bridge *isa_br = NULL;
  643. #endif
  644. static int invoked;
  645. if (invoked)
  646. return;
  647. invoked = 1;
  648. if (this_is_starfire) {
  649. xtime.tv_sec = starfire_get_time();
  650. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  651. set_normalized_timespec(&wall_to_monotonic,
  652. -xtime.tv_sec, -xtime.tv_nsec);
  653. return;
  654. }
  655. if (tlb_type == hypervisor) {
  656. xtime.tv_sec = hypervisor_get_time();
  657. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  658. set_normalized_timespec(&wall_to_monotonic,
  659. -xtime.tv_sec, -xtime.tv_nsec);
  660. return;
  661. }
  662. local_irq_save(flags);
  663. cbus = central_bus;
  664. if (cbus != NULL)
  665. busnd = central_bus->child->prom_node;
  666. /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
  667. * That way we handle the presence of multiple properly.
  668. *
  669. * As a special case, machines with Central must provide the
  670. * timer chip there.
  671. */
  672. #ifdef CONFIG_PCI
  673. if (ebus_chain != NULL) {
  674. ebus = ebus_chain;
  675. if (busnd == -1)
  676. busnd = ebus->prom_node;
  677. }
  678. if (isa_chain != NULL) {
  679. isa_br = isa_chain;
  680. if (busnd == -1)
  681. busnd = isa_br->prom_node;
  682. }
  683. #endif
  684. if (sbus_root != NULL && busnd == -1)
  685. busnd = sbus_root->prom_node;
  686. if (busnd == -1) {
  687. prom_printf("clock_probe: problem, cannot find bus to search.\n");
  688. prom_halt();
  689. }
  690. node = prom_getchild(busnd);
  691. while (1) {
  692. if (!node)
  693. model[0] = 0;
  694. else
  695. prom_getstring(node, "model", model, sizeof(model));
  696. if (strcmp(model, "mk48t02") &&
  697. strcmp(model, "mk48t08") &&
  698. strcmp(model, "mk48t59") &&
  699. strcmp(model, "m5819") &&
  700. strcmp(model, "m5819p") &&
  701. strcmp(model, "m5823") &&
  702. strcmp(model, "ds1287")) {
  703. if (cbus != NULL) {
  704. prom_printf("clock_probe: Central bus lacks timer chip.\n");
  705. prom_halt();
  706. }
  707. if (node != 0)
  708. node = prom_getsibling(node);
  709. #ifdef CONFIG_PCI
  710. while ((node == 0) && ebus != NULL) {
  711. ebus = ebus->next;
  712. if (ebus != NULL) {
  713. busnd = ebus->prom_node;
  714. node = prom_getchild(busnd);
  715. }
  716. }
  717. while ((node == 0) && isa_br != NULL) {
  718. isa_br = isa_br->next;
  719. if (isa_br != NULL) {
  720. busnd = isa_br->prom_node;
  721. node = prom_getchild(busnd);
  722. }
  723. }
  724. #endif
  725. if (node == 0) {
  726. prom_printf("clock_probe: Cannot find timer chip\n");
  727. prom_halt();
  728. }
  729. continue;
  730. }
  731. err = prom_getproperty(node, "reg", (char *)clk_reg,
  732. sizeof(clk_reg));
  733. if(err == -1) {
  734. prom_printf("clock_probe: Cannot get Mostek reg property\n");
  735. prom_halt();
  736. }
  737. if (cbus != NULL) {
  738. apply_fhc_ranges(central_bus->child, clk_reg, 1);
  739. apply_central_ranges(central_bus, clk_reg, 1);
  740. }
  741. #ifdef CONFIG_PCI
  742. else if (ebus != NULL) {
  743. struct linux_ebus_device *edev;
  744. for_each_ebusdev(edev, ebus)
  745. if (edev->prom_node == node)
  746. break;
  747. if (edev == NULL) {
  748. if (isa_chain != NULL)
  749. goto try_isa_clock;
  750. prom_printf("%s: Mostek not probed by EBUS\n",
  751. __FUNCTION__);
  752. prom_halt();
  753. }
  754. if (!strcmp(model, "ds1287") ||
  755. !strcmp(model, "m5819") ||
  756. !strcmp(model, "m5819p") ||
  757. !strcmp(model, "m5823")) {
  758. ds1287_regs = edev->resource[0].start;
  759. } else {
  760. mstk48t59_regs = (void __iomem *)
  761. edev->resource[0].start;
  762. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  763. }
  764. break;
  765. }
  766. else if (isa_br != NULL) {
  767. struct sparc_isa_device *isadev;
  768. try_isa_clock:
  769. for_each_isadev(isadev, isa_br)
  770. if (isadev->prom_node == node)
  771. break;
  772. if (isadev == NULL) {
  773. prom_printf("%s: Mostek not probed by ISA\n");
  774. prom_halt();
  775. }
  776. if (!strcmp(model, "ds1287") ||
  777. !strcmp(model, "m5819") ||
  778. !strcmp(model, "m5819p") ||
  779. !strcmp(model, "m5823")) {
  780. ds1287_regs = isadev->resource.start;
  781. } else {
  782. mstk48t59_regs = (void __iomem *)
  783. isadev->resource.start;
  784. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  785. }
  786. break;
  787. }
  788. #endif
  789. else {
  790. if (sbus_root->num_sbus_ranges) {
  791. int nranges = sbus_root->num_sbus_ranges;
  792. int rngc;
  793. for (rngc = 0; rngc < nranges; rngc++)
  794. if (clk_reg[0].which_io ==
  795. sbus_root->sbus_ranges[rngc].ot_child_space)
  796. break;
  797. if (rngc == nranges) {
  798. prom_printf("clock_probe: Cannot find ranges for "
  799. "clock regs.\n");
  800. prom_halt();
  801. }
  802. clk_reg[0].which_io =
  803. sbus_root->sbus_ranges[rngc].ot_parent_space;
  804. clk_reg[0].phys_addr +=
  805. sbus_root->sbus_ranges[rngc].ot_parent_base;
  806. }
  807. }
  808. if(model[5] == '0' && model[6] == '2') {
  809. mstk48t02_regs = (void __iomem *)
  810. (((u64)clk_reg[0].phys_addr) |
  811. (((u64)clk_reg[0].which_io)<<32UL));
  812. } else if(model[5] == '0' && model[6] == '8') {
  813. mstk48t08_regs = (void __iomem *)
  814. (((u64)clk_reg[0].phys_addr) |
  815. (((u64)clk_reg[0].which_io)<<32UL));
  816. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  817. } else {
  818. mstk48t59_regs = (void __iomem *)
  819. (((u64)clk_reg[0].phys_addr) |
  820. (((u64)clk_reg[0].which_io)<<32UL));
  821. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  822. }
  823. break;
  824. }
  825. if (mstk48t02_regs != NULL) {
  826. /* Report a low battery voltage condition. */
  827. if (has_low_battery())
  828. prom_printf("NVRAM: Low battery voltage!\n");
  829. /* Kick start the clock if it is completely stopped. */
  830. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  831. kick_start_clock();
  832. }
  833. set_system_time();
  834. local_irq_restore(flags);
  835. }
  836. /* This is gets the master TICK_INT timer going. */
  837. static unsigned long sparc64_init_timers(void)
  838. {
  839. unsigned long clock;
  840. int node;
  841. #ifdef CONFIG_SMP
  842. extern void smp_tick_init(void);
  843. #endif
  844. if (tlb_type == spitfire) {
  845. unsigned long ver, manuf, impl;
  846. __asm__ __volatile__ ("rdpr %%ver, %0"
  847. : "=&r" (ver));
  848. manuf = ((ver >> 48) & 0xffff);
  849. impl = ((ver >> 32) & 0xffff);
  850. if (manuf == 0x17 && impl == 0x13) {
  851. /* Hummingbird, aka Ultra-IIe */
  852. tick_ops = &hbtick_operations;
  853. node = prom_root_node;
  854. clock = prom_getint(node, "stick-frequency");
  855. } else {
  856. tick_ops = &tick_operations;
  857. cpu_find_by_instance(0, &node, NULL);
  858. clock = prom_getint(node, "clock-frequency");
  859. }
  860. } else {
  861. tick_ops = &stick_operations;
  862. node = prom_root_node;
  863. clock = prom_getint(node, "stick-frequency");
  864. }
  865. timer_tick_offset = clock / HZ;
  866. #ifdef CONFIG_SMP
  867. smp_tick_init();
  868. #endif
  869. return clock;
  870. }
  871. static void sparc64_start_timers(void)
  872. {
  873. unsigned long pstate;
  874. /* Guarantee that the following sequences execute
  875. * uninterrupted.
  876. */
  877. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  878. "wrpr %0, %1, %%pstate"
  879. : "=r" (pstate)
  880. : "i" (PSTATE_IE));
  881. tick_ops->init_tick(timer_tick_offset);
  882. /* Restore PSTATE_IE. */
  883. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  884. : /* no outputs */
  885. : "r" (pstate));
  886. local_irq_enable();
  887. }
  888. struct freq_table {
  889. unsigned long clock_tick_ref;
  890. unsigned int ref_freq;
  891. };
  892. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  893. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  894. {
  895. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  896. if (ft->clock_tick_ref)
  897. return ft->clock_tick_ref;
  898. return cpu_data(cpu).clock_tick;
  899. }
  900. #ifdef CONFIG_CPU_FREQ
  901. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  902. void *data)
  903. {
  904. struct cpufreq_freqs *freq = data;
  905. unsigned int cpu = freq->cpu;
  906. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  907. if (!ft->ref_freq) {
  908. ft->ref_freq = freq->old;
  909. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  910. }
  911. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  912. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  913. (val == CPUFREQ_RESUMECHANGE)) {
  914. cpu_data(cpu).clock_tick =
  915. cpufreq_scale(ft->clock_tick_ref,
  916. ft->ref_freq,
  917. freq->new);
  918. }
  919. return 0;
  920. }
  921. static struct notifier_block sparc64_cpufreq_notifier_block = {
  922. .notifier_call = sparc64_cpufreq_notifier
  923. };
  924. #endif /* CONFIG_CPU_FREQ */
  925. static struct time_interpolator sparc64_cpu_interpolator = {
  926. .source = TIME_SOURCE_CPU,
  927. .shift = 16,
  928. .mask = 0xffffffffffffffffLL
  929. };
  930. /* The quotient formula is taken from the IA64 port. */
  931. #define SPARC64_NSEC_PER_CYC_SHIFT 30UL
  932. void __init time_init(void)
  933. {
  934. unsigned long clock = sparc64_init_timers();
  935. sparc64_cpu_interpolator.frequency = clock;
  936. register_time_interpolator(&sparc64_cpu_interpolator);
  937. /* Now that the interpolator is registered, it is
  938. * safe to start the timer ticking.
  939. */
  940. sparc64_start_timers();
  941. timer_ticks_per_nsec_quotient =
  942. (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
  943. (clock / 2)) / clock);
  944. #ifdef CONFIG_CPU_FREQ
  945. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  946. CPUFREQ_TRANSITION_NOTIFIER);
  947. #endif
  948. }
  949. unsigned long long sched_clock(void)
  950. {
  951. unsigned long ticks = tick_ops->get_tick();
  952. return (ticks * timer_ticks_per_nsec_quotient)
  953. >> SPARC64_NSEC_PER_CYC_SHIFT;
  954. }
  955. static int set_rtc_mmss(unsigned long nowtime)
  956. {
  957. int real_seconds, real_minutes, chip_minutes;
  958. void __iomem *mregs = mstk48t02_regs;
  959. #ifdef CONFIG_PCI
  960. unsigned long dregs = ds1287_regs;
  961. #else
  962. unsigned long dregs = 0UL;
  963. #endif
  964. unsigned long flags;
  965. u8 tmp;
  966. /*
  967. * Not having a register set can lead to trouble.
  968. * Also starfire doesn't have a tod clock.
  969. */
  970. if (!mregs && !dregs)
  971. return -1;
  972. if (mregs) {
  973. spin_lock_irqsave(&mostek_lock, flags);
  974. /* Read the current RTC minutes. */
  975. tmp = mostek_read(mregs + MOSTEK_CREG);
  976. tmp |= MSTK_CREG_READ;
  977. mostek_write(mregs + MOSTEK_CREG, tmp);
  978. chip_minutes = MSTK_REG_MIN(mregs);
  979. tmp = mostek_read(mregs + MOSTEK_CREG);
  980. tmp &= ~MSTK_CREG_READ;
  981. mostek_write(mregs + MOSTEK_CREG, tmp);
  982. /*
  983. * since we're only adjusting minutes and seconds,
  984. * don't interfere with hour overflow. This avoids
  985. * messing with unknown time zones but requires your
  986. * RTC not to be off by more than 15 minutes
  987. */
  988. real_seconds = nowtime % 60;
  989. real_minutes = nowtime / 60;
  990. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  991. real_minutes += 30; /* correct for half hour time zone */
  992. real_minutes %= 60;
  993. if (abs(real_minutes - chip_minutes) < 30) {
  994. tmp = mostek_read(mregs + MOSTEK_CREG);
  995. tmp |= MSTK_CREG_WRITE;
  996. mostek_write(mregs + MOSTEK_CREG, tmp);
  997. MSTK_SET_REG_SEC(mregs,real_seconds);
  998. MSTK_SET_REG_MIN(mregs,real_minutes);
  999. tmp = mostek_read(mregs + MOSTEK_CREG);
  1000. tmp &= ~MSTK_CREG_WRITE;
  1001. mostek_write(mregs + MOSTEK_CREG, tmp);
  1002. spin_unlock_irqrestore(&mostek_lock, flags);
  1003. return 0;
  1004. } else {
  1005. spin_unlock_irqrestore(&mostek_lock, flags);
  1006. return -1;
  1007. }
  1008. } else {
  1009. int retval = 0;
  1010. unsigned char save_control, save_freq_select;
  1011. /* Stolen from arch/i386/kernel/time.c, see there for
  1012. * credits and descriptive comments.
  1013. */
  1014. spin_lock_irqsave(&rtc_lock, flags);
  1015. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  1016. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  1017. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  1018. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  1019. chip_minutes = CMOS_READ(RTC_MINUTES);
  1020. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  1021. BCD_TO_BIN(chip_minutes);
  1022. real_seconds = nowtime % 60;
  1023. real_minutes = nowtime / 60;
  1024. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  1025. real_minutes += 30;
  1026. real_minutes %= 60;
  1027. if (abs(real_minutes - chip_minutes) < 30) {
  1028. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1029. BIN_TO_BCD(real_seconds);
  1030. BIN_TO_BCD(real_minutes);
  1031. }
  1032. CMOS_WRITE(real_seconds,RTC_SECONDS);
  1033. CMOS_WRITE(real_minutes,RTC_MINUTES);
  1034. } else {
  1035. printk(KERN_WARNING
  1036. "set_rtc_mmss: can't update from %d to %d\n",
  1037. chip_minutes, real_minutes);
  1038. retval = -1;
  1039. }
  1040. CMOS_WRITE(save_control, RTC_CONTROL);
  1041. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1042. spin_unlock_irqrestore(&rtc_lock, flags);
  1043. return retval;
  1044. }
  1045. }
  1046. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  1047. static unsigned char mini_rtc_status; /* bitmapped status byte. */
  1048. /* months start at 0 now */
  1049. static unsigned char days_in_mo[] =
  1050. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  1051. #define FEBRUARY 2
  1052. #define STARTOFTIME 1970
  1053. #define SECDAY 86400L
  1054. #define SECYR (SECDAY * 365)
  1055. #define leapyear(year) ((year) % 4 == 0 && \
  1056. ((year) % 100 != 0 || (year) % 400 == 0))
  1057. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  1058. #define days_in_month(a) (month_days[(a) - 1])
  1059. static int month_days[12] = {
  1060. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  1061. };
  1062. /*
  1063. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  1064. */
  1065. static void GregorianDay(struct rtc_time * tm)
  1066. {
  1067. int leapsToDate;
  1068. int lastYear;
  1069. int day;
  1070. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1071. lastYear = tm->tm_year - 1;
  1072. /*
  1073. * Number of leap corrections to apply up to end of last year
  1074. */
  1075. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  1076. /*
  1077. * This year is a leap year if it is divisible by 4 except when it is
  1078. * divisible by 100 unless it is divisible by 400
  1079. *
  1080. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  1081. */
  1082. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  1083. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  1084. tm->tm_mday;
  1085. tm->tm_wday = day % 7;
  1086. }
  1087. static void to_tm(int tim, struct rtc_time *tm)
  1088. {
  1089. register int i;
  1090. register long hms, day;
  1091. day = tim / SECDAY;
  1092. hms = tim % SECDAY;
  1093. /* Hours, minutes, seconds are easy */
  1094. tm->tm_hour = hms / 3600;
  1095. tm->tm_min = (hms % 3600) / 60;
  1096. tm->tm_sec = (hms % 3600) % 60;
  1097. /* Number of years in days */
  1098. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  1099. day -= days_in_year(i);
  1100. tm->tm_year = i;
  1101. /* Number of months in days left */
  1102. if (leapyear(tm->tm_year))
  1103. days_in_month(FEBRUARY) = 29;
  1104. for (i = 1; day >= days_in_month(i); i++)
  1105. day -= days_in_month(i);
  1106. days_in_month(FEBRUARY) = 28;
  1107. tm->tm_mon = i;
  1108. /* Days are what is left over (+1) from all that. */
  1109. tm->tm_mday = day + 1;
  1110. /*
  1111. * Determine the day of week
  1112. */
  1113. GregorianDay(tm);
  1114. }
  1115. /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
  1116. * aka Unix time. So we have to convert to/from rtc_time.
  1117. */
  1118. static inline void mini_get_rtc_time(struct rtc_time *time)
  1119. {
  1120. unsigned long flags;
  1121. u32 seconds;
  1122. spin_lock_irqsave(&rtc_lock, flags);
  1123. seconds = 0;
  1124. if (this_is_starfire)
  1125. seconds = starfire_get_time();
  1126. else if (tlb_type == hypervisor)
  1127. seconds = hypervisor_get_time();
  1128. spin_unlock_irqrestore(&rtc_lock, flags);
  1129. to_tm(seconds, time);
  1130. time->tm_year -= 1900;
  1131. time->tm_mon -= 1;
  1132. }
  1133. static inline int mini_set_rtc_time(struct rtc_time *time)
  1134. {
  1135. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1136. time->tm_mday, time->tm_hour,
  1137. time->tm_min, time->tm_sec);
  1138. unsigned long flags;
  1139. int err;
  1140. spin_lock_irqsave(&rtc_lock, flags);
  1141. err = -ENODEV;
  1142. if (this_is_starfire)
  1143. err = starfire_set_time(seconds);
  1144. else if (tlb_type == hypervisor)
  1145. err = hypervisor_set_time(seconds);
  1146. spin_unlock_irqrestore(&rtc_lock, flags);
  1147. return err;
  1148. }
  1149. static int mini_rtc_ioctl(struct inode *inode, struct file *file,
  1150. unsigned int cmd, unsigned long arg)
  1151. {
  1152. struct rtc_time wtime;
  1153. void __user *argp = (void __user *)arg;
  1154. switch (cmd) {
  1155. case RTC_PLL_GET:
  1156. return -EINVAL;
  1157. case RTC_PLL_SET:
  1158. return -EINVAL;
  1159. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  1160. return 0;
  1161. case RTC_UIE_ON: /* enable ints for RTC updates. */
  1162. return -EINVAL;
  1163. case RTC_RD_TIME: /* Read the time/date from RTC */
  1164. /* this doesn't get week-day, who cares */
  1165. memset(&wtime, 0, sizeof(wtime));
  1166. mini_get_rtc_time(&wtime);
  1167. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  1168. case RTC_SET_TIME: /* Set the RTC */
  1169. {
  1170. int year;
  1171. unsigned char leap_yr;
  1172. if (!capable(CAP_SYS_TIME))
  1173. return -EACCES;
  1174. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  1175. return -EFAULT;
  1176. year = wtime.tm_year + 1900;
  1177. leap_yr = ((!(year % 4) && (year % 100)) ||
  1178. !(year % 400));
  1179. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
  1180. return -EINVAL;
  1181. if (wtime.tm_mday < 0 || wtime.tm_mday >
  1182. (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
  1183. return -EINVAL;
  1184. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  1185. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  1186. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  1187. return -EINVAL;
  1188. return mini_set_rtc_time(&wtime);
  1189. }
  1190. }
  1191. return -EINVAL;
  1192. }
  1193. static int mini_rtc_open(struct inode *inode, struct file *file)
  1194. {
  1195. if (mini_rtc_status & RTC_IS_OPEN)
  1196. return -EBUSY;
  1197. mini_rtc_status |= RTC_IS_OPEN;
  1198. return 0;
  1199. }
  1200. static int mini_rtc_release(struct inode *inode, struct file *file)
  1201. {
  1202. mini_rtc_status &= ~RTC_IS_OPEN;
  1203. return 0;
  1204. }
  1205. static struct file_operations mini_rtc_fops = {
  1206. .owner = THIS_MODULE,
  1207. .ioctl = mini_rtc_ioctl,
  1208. .open = mini_rtc_open,
  1209. .release = mini_rtc_release,
  1210. };
  1211. static struct miscdevice rtc_mini_dev =
  1212. {
  1213. .minor = RTC_MINOR,
  1214. .name = "rtc",
  1215. .fops = &mini_rtc_fops,
  1216. };
  1217. static int __init rtc_mini_init(void)
  1218. {
  1219. int retval;
  1220. if (tlb_type != hypervisor && !this_is_starfire)
  1221. return -ENODEV;
  1222. printk(KERN_INFO "Mini RTC Driver\n");
  1223. retval = misc_register(&rtc_mini_dev);
  1224. if (retval < 0)
  1225. return retval;
  1226. return 0;
  1227. }
  1228. static void __exit rtc_mini_exit(void)
  1229. {
  1230. misc_deregister(&rtc_mini_dev);
  1231. }
  1232. module_init(rtc_mini_init);
  1233. module_exit(rtc_mini_exit);