time.c 38 KB

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