time.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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. unsigned long ret, time;
  568. int retries = 10000;
  569. retry:
  570. ret = sun4v_tod_get(&time);
  571. if (ret == HV_EOK)
  572. return time;
  573. if (ret == HV_EWOULDBLOCK) {
  574. if (--retries > 0) {
  575. udelay(100);
  576. goto retry;
  577. }
  578. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  579. return 0;
  580. }
  581. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  582. return 0;
  583. }
  584. static int hypervisor_set_time(u32 secs)
  585. {
  586. unsigned long ret;
  587. int retries = 10000;
  588. retry:
  589. ret = sun4v_tod_set(secs);
  590. if (ret == HV_EOK)
  591. return 0;
  592. if (ret == HV_EWOULDBLOCK) {
  593. if (--retries > 0) {
  594. udelay(100);
  595. goto retry;
  596. }
  597. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  598. return -EAGAIN;
  599. }
  600. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  601. return -EOPNOTSUPP;
  602. }
  603. static int __init clock_model_matches(const char *model)
  604. {
  605. if (strcmp(model, "mk48t02") &&
  606. strcmp(model, "mk48t08") &&
  607. strcmp(model, "mk48t59") &&
  608. strcmp(model, "m5819") &&
  609. strcmp(model, "m5819p") &&
  610. strcmp(model, "m5823") &&
  611. strcmp(model, "ds1287") &&
  612. strcmp(model, "bq4802"))
  613. return 0;
  614. return 1;
  615. }
  616. static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
  617. {
  618. struct device_node *dp = op->node;
  619. const char *model = of_get_property(dp, "model", NULL);
  620. const char *compat = of_get_property(dp, "compatible", NULL);
  621. unsigned long size, flags;
  622. void __iomem *regs;
  623. if (!model)
  624. model = compat;
  625. if (!model || !clock_model_matches(model))
  626. return -ENODEV;
  627. /* On an Enterprise system there can be multiple mostek clocks.
  628. * We should only match the one that is on the central FHC bus.
  629. */
  630. if (!strcmp(dp->parent->name, "fhc") &&
  631. strcmp(dp->parent->parent->name, "central") != 0)
  632. return -ENODEV;
  633. size = (op->resource[0].end - op->resource[0].start) + 1;
  634. regs = of_ioremap(&op->resource[0], 0, size, "clock");
  635. if (!regs)
  636. return -ENOMEM;
  637. #ifdef CONFIG_PCI
  638. if (!strcmp(model, "ds1287") ||
  639. !strcmp(model, "m5819") ||
  640. !strcmp(model, "m5819p") ||
  641. !strcmp(model, "m5823")) {
  642. ds1287_regs = (unsigned long) regs;
  643. } else if (!strcmp(model, "bq4802")) {
  644. bq4802_regs = regs;
  645. } else
  646. #endif
  647. if (model[5] == '0' && model[6] == '2') {
  648. mstk48t02_regs = regs;
  649. } else if(model[5] == '0' && model[6] == '8') {
  650. mstk48t08_regs = regs;
  651. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  652. } else {
  653. mstk48t59_regs = regs;
  654. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  655. }
  656. printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
  657. local_irq_save(flags);
  658. if (mstk48t02_regs != NULL) {
  659. /* Report a low battery voltage condition. */
  660. if (has_low_battery())
  661. prom_printf("NVRAM: Low battery voltage!\n");
  662. /* Kick start the clock if it is completely stopped. */
  663. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  664. kick_start_clock();
  665. }
  666. set_system_time();
  667. local_irq_restore(flags);
  668. return 0;
  669. }
  670. static struct of_device_id clock_match[] = {
  671. {
  672. .name = "eeprom",
  673. },
  674. {
  675. .name = "rtc",
  676. },
  677. {},
  678. };
  679. static struct of_platform_driver clock_driver = {
  680. .name = "clock",
  681. .match_table = clock_match,
  682. .probe = clock_probe,
  683. };
  684. static int __init clock_init(void)
  685. {
  686. if (this_is_starfire) {
  687. xtime.tv_sec = starfire_get_time();
  688. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  689. set_normalized_timespec(&wall_to_monotonic,
  690. -xtime.tv_sec, -xtime.tv_nsec);
  691. return 0;
  692. }
  693. if (tlb_type == hypervisor) {
  694. xtime.tv_sec = hypervisor_get_time();
  695. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  696. set_normalized_timespec(&wall_to_monotonic,
  697. -xtime.tv_sec, -xtime.tv_nsec);
  698. return 0;
  699. }
  700. return of_register_driver(&clock_driver, &of_bus_type);
  701. }
  702. /* Must be after subsys_initcall() so that busses are probed. Must
  703. * be before device_initcall() because things like the RTC driver
  704. * need to see the clock registers.
  705. */
  706. fs_initcall(clock_init);
  707. /* This is gets the master TICK_INT timer going. */
  708. static unsigned long sparc64_init_timers(void)
  709. {
  710. struct device_node *dp;
  711. unsigned long clock;
  712. dp = of_find_node_by_path("/");
  713. if (tlb_type == spitfire) {
  714. unsigned long ver, manuf, impl;
  715. __asm__ __volatile__ ("rdpr %%ver, %0"
  716. : "=&r" (ver));
  717. manuf = ((ver >> 48) & 0xffff);
  718. impl = ((ver >> 32) & 0xffff);
  719. if (manuf == 0x17 && impl == 0x13) {
  720. /* Hummingbird, aka Ultra-IIe */
  721. tick_ops = &hbtick_operations;
  722. clock = of_getintprop_default(dp, "stick-frequency", 0);
  723. } else {
  724. tick_ops = &tick_operations;
  725. clock = local_cpu_data().clock_tick;
  726. }
  727. } else {
  728. tick_ops = &stick_operations;
  729. clock = of_getintprop_default(dp, "stick-frequency", 0);
  730. }
  731. return clock;
  732. }
  733. struct freq_table {
  734. unsigned long clock_tick_ref;
  735. unsigned int ref_freq;
  736. };
  737. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  738. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  739. {
  740. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  741. if (ft->clock_tick_ref)
  742. return ft->clock_tick_ref;
  743. return cpu_data(cpu).clock_tick;
  744. }
  745. #ifdef CONFIG_CPU_FREQ
  746. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  747. void *data)
  748. {
  749. struct cpufreq_freqs *freq = data;
  750. unsigned int cpu = freq->cpu;
  751. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  752. if (!ft->ref_freq) {
  753. ft->ref_freq = freq->old;
  754. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  755. }
  756. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  757. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  758. (val == CPUFREQ_RESUMECHANGE)) {
  759. cpu_data(cpu).clock_tick =
  760. cpufreq_scale(ft->clock_tick_ref,
  761. ft->ref_freq,
  762. freq->new);
  763. }
  764. return 0;
  765. }
  766. static struct notifier_block sparc64_cpufreq_notifier_block = {
  767. .notifier_call = sparc64_cpufreq_notifier
  768. };
  769. #endif /* CONFIG_CPU_FREQ */
  770. static int sparc64_next_event(unsigned long delta,
  771. struct clock_event_device *evt)
  772. {
  773. return tick_ops->add_compare(delta) ? -ETIME : 0;
  774. }
  775. static void sparc64_timer_setup(enum clock_event_mode mode,
  776. struct clock_event_device *evt)
  777. {
  778. switch (mode) {
  779. case CLOCK_EVT_MODE_ONESHOT:
  780. break;
  781. case CLOCK_EVT_MODE_SHUTDOWN:
  782. tick_ops->disable_irq();
  783. break;
  784. case CLOCK_EVT_MODE_PERIODIC:
  785. case CLOCK_EVT_MODE_UNUSED:
  786. WARN_ON(1);
  787. break;
  788. };
  789. }
  790. static struct clock_event_device sparc64_clockevent = {
  791. .features = CLOCK_EVT_FEAT_ONESHOT,
  792. .set_mode = sparc64_timer_setup,
  793. .set_next_event = sparc64_next_event,
  794. .rating = 100,
  795. .shift = 30,
  796. .irq = -1,
  797. };
  798. static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
  799. void timer_interrupt(int irq, struct pt_regs *regs)
  800. {
  801. struct pt_regs *old_regs = set_irq_regs(regs);
  802. unsigned long tick_mask = tick_ops->softint_mask;
  803. int cpu = smp_processor_id();
  804. struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
  805. clear_softint(tick_mask);
  806. irq_enter();
  807. kstat_this_cpu.irqs[0]++;
  808. if (unlikely(!evt->event_handler)) {
  809. printk(KERN_WARNING
  810. "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
  811. } else
  812. evt->event_handler(evt);
  813. irq_exit();
  814. set_irq_regs(old_regs);
  815. }
  816. void __devinit setup_sparc64_timer(void)
  817. {
  818. struct clock_event_device *sevt;
  819. unsigned long pstate;
  820. /* Guarantee that the following sequences execute
  821. * uninterrupted.
  822. */
  823. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  824. "wrpr %0, %1, %%pstate"
  825. : "=r" (pstate)
  826. : "i" (PSTATE_IE));
  827. tick_ops->init_tick();
  828. /* Restore PSTATE_IE. */
  829. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  830. : /* no outputs */
  831. : "r" (pstate));
  832. sevt = &__get_cpu_var(sparc64_events);
  833. memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
  834. sevt->cpumask = cpumask_of_cpu(smp_processor_id());
  835. clockevents_register_device(sevt);
  836. }
  837. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  838. static struct clocksource clocksource_tick = {
  839. .rating = 100,
  840. .mask = CLOCKSOURCE_MASK(64),
  841. .shift = 16,
  842. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  843. };
  844. static void __init setup_clockevent_multiplier(unsigned long hz)
  845. {
  846. unsigned long mult, shift = 32;
  847. while (1) {
  848. mult = div_sc(hz, NSEC_PER_SEC, shift);
  849. if (mult && (mult >> 32UL) == 0UL)
  850. break;
  851. shift--;
  852. }
  853. sparc64_clockevent.shift = shift;
  854. sparc64_clockevent.mult = mult;
  855. }
  856. static unsigned long tb_ticks_per_usec __read_mostly;
  857. void __delay(unsigned long loops)
  858. {
  859. unsigned long bclock, now;
  860. bclock = tick_ops->get_tick();
  861. do {
  862. now = tick_ops->get_tick();
  863. } while ((now-bclock) < loops);
  864. }
  865. EXPORT_SYMBOL(__delay);
  866. void udelay(unsigned long usecs)
  867. {
  868. __delay(tb_ticks_per_usec * usecs);
  869. }
  870. EXPORT_SYMBOL(udelay);
  871. void __init time_init(void)
  872. {
  873. unsigned long clock = sparc64_init_timers();
  874. tb_ticks_per_usec = clock / USEC_PER_SEC;
  875. timer_ticks_per_nsec_quotient =
  876. clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
  877. clocksource_tick.name = tick_ops->name;
  878. clocksource_tick.mult =
  879. clocksource_hz2mult(clock,
  880. clocksource_tick.shift);
  881. clocksource_tick.read = tick_ops->get_tick;
  882. printk("clocksource: mult[%x] shift[%d]\n",
  883. clocksource_tick.mult, clocksource_tick.shift);
  884. clocksource_register(&clocksource_tick);
  885. sparc64_clockevent.name = tick_ops->name;
  886. setup_clockevent_multiplier(clock);
  887. sparc64_clockevent.max_delta_ns =
  888. clockevent_delta2ns(0x7fffffffffffffff, &sparc64_clockevent);
  889. sparc64_clockevent.min_delta_ns =
  890. clockevent_delta2ns(0xF, &sparc64_clockevent);
  891. printk("clockevent: mult[%lx] shift[%d]\n",
  892. sparc64_clockevent.mult, sparc64_clockevent.shift);
  893. setup_sparc64_timer();
  894. #ifdef CONFIG_CPU_FREQ
  895. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  896. CPUFREQ_TRANSITION_NOTIFIER);
  897. #endif
  898. }
  899. unsigned long long sched_clock(void)
  900. {
  901. unsigned long ticks = tick_ops->get_tick();
  902. return (ticks * timer_ticks_per_nsec_quotient)
  903. >> SPARC64_NSEC_PER_CYC_SHIFT;
  904. }
  905. static int set_rtc_mmss(unsigned long nowtime)
  906. {
  907. int real_seconds, real_minutes, chip_minutes;
  908. void __iomem *mregs = mstk48t02_regs;
  909. #ifdef CONFIG_PCI
  910. unsigned long dregs = ds1287_regs;
  911. void __iomem *bregs = bq4802_regs;
  912. #else
  913. unsigned long dregs = 0UL;
  914. void __iomem *bregs = 0UL;
  915. #endif
  916. unsigned long flags;
  917. u8 tmp;
  918. /*
  919. * Not having a register set can lead to trouble.
  920. * Also starfire doesn't have a tod clock.
  921. */
  922. if (!mregs && !dregs & !bregs)
  923. return -1;
  924. if (mregs) {
  925. spin_lock_irqsave(&mostek_lock, flags);
  926. /* Read the current RTC minutes. */
  927. tmp = mostek_read(mregs + MOSTEK_CREG);
  928. tmp |= MSTK_CREG_READ;
  929. mostek_write(mregs + MOSTEK_CREG, tmp);
  930. chip_minutes = MSTK_REG_MIN(mregs);
  931. tmp = mostek_read(mregs + MOSTEK_CREG);
  932. tmp &= ~MSTK_CREG_READ;
  933. mostek_write(mregs + MOSTEK_CREG, tmp);
  934. /*
  935. * since we're only adjusting minutes and seconds,
  936. * don't interfere with hour overflow. This avoids
  937. * messing with unknown time zones but requires your
  938. * RTC not to be off by more than 15 minutes
  939. */
  940. real_seconds = nowtime % 60;
  941. real_minutes = nowtime / 60;
  942. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  943. real_minutes += 30; /* correct for half hour time zone */
  944. real_minutes %= 60;
  945. if (abs(real_minutes - chip_minutes) < 30) {
  946. tmp = mostek_read(mregs + MOSTEK_CREG);
  947. tmp |= MSTK_CREG_WRITE;
  948. mostek_write(mregs + MOSTEK_CREG, tmp);
  949. MSTK_SET_REG_SEC(mregs,real_seconds);
  950. MSTK_SET_REG_MIN(mregs,real_minutes);
  951. tmp = mostek_read(mregs + MOSTEK_CREG);
  952. tmp &= ~MSTK_CREG_WRITE;
  953. mostek_write(mregs + MOSTEK_CREG, tmp);
  954. spin_unlock_irqrestore(&mostek_lock, flags);
  955. return 0;
  956. } else {
  957. spin_unlock_irqrestore(&mostek_lock, flags);
  958. return -1;
  959. }
  960. } else if (bregs) {
  961. int retval = 0;
  962. unsigned char val = readb(bregs + 0x0e);
  963. /* BQ4802 RTC chip. */
  964. writeb(val | 0x08, bregs + 0x0e);
  965. chip_minutes = readb(bregs + 0x02);
  966. BCD_TO_BIN(chip_minutes);
  967. real_seconds = nowtime % 60;
  968. real_minutes = nowtime / 60;
  969. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  970. real_minutes += 30;
  971. real_minutes %= 60;
  972. if (abs(real_minutes - chip_minutes) < 30) {
  973. BIN_TO_BCD(real_seconds);
  974. BIN_TO_BCD(real_minutes);
  975. writeb(real_seconds, bregs + 0x00);
  976. writeb(real_minutes, bregs + 0x02);
  977. } else {
  978. printk(KERN_WARNING
  979. "set_rtc_mmss: can't update from %d to %d\n",
  980. chip_minutes, real_minutes);
  981. retval = -1;
  982. }
  983. writeb(val, bregs + 0x0e);
  984. return retval;
  985. } else {
  986. int retval = 0;
  987. unsigned char save_control, save_freq_select;
  988. /* Stolen from arch/i386/kernel/time.c, see there for
  989. * credits and descriptive comments.
  990. */
  991. spin_lock_irqsave(&rtc_lock, flags);
  992. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  993. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  994. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  995. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  996. chip_minutes = CMOS_READ(RTC_MINUTES);
  997. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  998. BCD_TO_BIN(chip_minutes);
  999. real_seconds = nowtime % 60;
  1000. real_minutes = nowtime / 60;
  1001. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  1002. real_minutes += 30;
  1003. real_minutes %= 60;
  1004. if (abs(real_minutes - chip_minutes) < 30) {
  1005. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1006. BIN_TO_BCD(real_seconds);
  1007. BIN_TO_BCD(real_minutes);
  1008. }
  1009. CMOS_WRITE(real_seconds,RTC_SECONDS);
  1010. CMOS_WRITE(real_minutes,RTC_MINUTES);
  1011. } else {
  1012. printk(KERN_WARNING
  1013. "set_rtc_mmss: can't update from %d to %d\n",
  1014. chip_minutes, real_minutes);
  1015. retval = -1;
  1016. }
  1017. CMOS_WRITE(save_control, RTC_CONTROL);
  1018. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1019. spin_unlock_irqrestore(&rtc_lock, flags);
  1020. return retval;
  1021. }
  1022. }
  1023. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  1024. static unsigned char mini_rtc_status; /* bitmapped status byte. */
  1025. #define FEBRUARY 2
  1026. #define STARTOFTIME 1970
  1027. #define SECDAY 86400L
  1028. #define SECYR (SECDAY * 365)
  1029. #define leapyear(year) ((year) % 4 == 0 && \
  1030. ((year) % 100 != 0 || (year) % 400 == 0))
  1031. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  1032. #define days_in_month(a) (month_days[(a) - 1])
  1033. static int month_days[12] = {
  1034. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  1035. };
  1036. /*
  1037. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  1038. */
  1039. static void GregorianDay(struct rtc_time * tm)
  1040. {
  1041. int leapsToDate;
  1042. int lastYear;
  1043. int day;
  1044. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1045. lastYear = tm->tm_year - 1;
  1046. /*
  1047. * Number of leap corrections to apply up to end of last year
  1048. */
  1049. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  1050. /*
  1051. * This year is a leap year if it is divisible by 4 except when it is
  1052. * divisible by 100 unless it is divisible by 400
  1053. *
  1054. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  1055. */
  1056. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  1057. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  1058. tm->tm_mday;
  1059. tm->tm_wday = day % 7;
  1060. }
  1061. static void to_tm(int tim, struct rtc_time *tm)
  1062. {
  1063. register int i;
  1064. register long hms, day;
  1065. day = tim / SECDAY;
  1066. hms = tim % SECDAY;
  1067. /* Hours, minutes, seconds are easy */
  1068. tm->tm_hour = hms / 3600;
  1069. tm->tm_min = (hms % 3600) / 60;
  1070. tm->tm_sec = (hms % 3600) % 60;
  1071. /* Number of years in days */
  1072. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  1073. day -= days_in_year(i);
  1074. tm->tm_year = i;
  1075. /* Number of months in days left */
  1076. if (leapyear(tm->tm_year))
  1077. days_in_month(FEBRUARY) = 29;
  1078. for (i = 1; day >= days_in_month(i); i++)
  1079. day -= days_in_month(i);
  1080. days_in_month(FEBRUARY) = 28;
  1081. tm->tm_mon = i;
  1082. /* Days are what is left over (+1) from all that. */
  1083. tm->tm_mday = day + 1;
  1084. /*
  1085. * Determine the day of week
  1086. */
  1087. GregorianDay(tm);
  1088. }
  1089. /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
  1090. * aka Unix time. So we have to convert to/from rtc_time.
  1091. */
  1092. static void starfire_get_rtc_time(struct rtc_time *time)
  1093. {
  1094. u32 seconds = starfire_get_time();
  1095. to_tm(seconds, time);
  1096. time->tm_year -= 1900;
  1097. time->tm_mon -= 1;
  1098. }
  1099. static int starfire_set_rtc_time(struct rtc_time *time)
  1100. {
  1101. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1102. time->tm_mday, time->tm_hour,
  1103. time->tm_min, time->tm_sec);
  1104. return starfire_set_time(seconds);
  1105. }
  1106. static void hypervisor_get_rtc_time(struct rtc_time *time)
  1107. {
  1108. u32 seconds = hypervisor_get_time();
  1109. to_tm(seconds, time);
  1110. time->tm_year -= 1900;
  1111. time->tm_mon -= 1;
  1112. }
  1113. static int hypervisor_set_rtc_time(struct rtc_time *time)
  1114. {
  1115. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1116. time->tm_mday, time->tm_hour,
  1117. time->tm_min, time->tm_sec);
  1118. return hypervisor_set_time(seconds);
  1119. }
  1120. #ifdef CONFIG_PCI
  1121. static void bq4802_get_rtc_time(struct rtc_time *time)
  1122. {
  1123. unsigned char val = readb(bq4802_regs + 0x0e);
  1124. unsigned int century;
  1125. writeb(val | 0x08, bq4802_regs + 0x0e);
  1126. time->tm_sec = readb(bq4802_regs + 0x00);
  1127. time->tm_min = readb(bq4802_regs + 0x02);
  1128. time->tm_hour = readb(bq4802_regs + 0x04);
  1129. time->tm_mday = readb(bq4802_regs + 0x06);
  1130. time->tm_mon = readb(bq4802_regs + 0x09);
  1131. time->tm_year = readb(bq4802_regs + 0x0a);
  1132. time->tm_wday = readb(bq4802_regs + 0x08);
  1133. century = readb(bq4802_regs + 0x0f);
  1134. writeb(val, bq4802_regs + 0x0e);
  1135. BCD_TO_BIN(time->tm_sec);
  1136. BCD_TO_BIN(time->tm_min);
  1137. BCD_TO_BIN(time->tm_hour);
  1138. BCD_TO_BIN(time->tm_mday);
  1139. BCD_TO_BIN(time->tm_mon);
  1140. BCD_TO_BIN(time->tm_year);
  1141. BCD_TO_BIN(time->tm_wday);
  1142. BCD_TO_BIN(century);
  1143. time->tm_year += (century * 100);
  1144. time->tm_year -= 1900;
  1145. time->tm_mon--;
  1146. }
  1147. static int bq4802_set_rtc_time(struct rtc_time *time)
  1148. {
  1149. unsigned char val = readb(bq4802_regs + 0x0e);
  1150. unsigned char sec, min, hrs, day, mon, yrs, century;
  1151. unsigned int year;
  1152. year = time->tm_year + 1900;
  1153. century = year / 100;
  1154. yrs = year % 100;
  1155. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  1156. day = time->tm_mday;
  1157. hrs = time->tm_hour;
  1158. min = time->tm_min;
  1159. sec = time->tm_sec;
  1160. BIN_TO_BCD(sec);
  1161. BIN_TO_BCD(min);
  1162. BIN_TO_BCD(hrs);
  1163. BIN_TO_BCD(day);
  1164. BIN_TO_BCD(mon);
  1165. BIN_TO_BCD(yrs);
  1166. BIN_TO_BCD(century);
  1167. writeb(val | 0x08, bq4802_regs + 0x0e);
  1168. writeb(sec, bq4802_regs + 0x00);
  1169. writeb(min, bq4802_regs + 0x02);
  1170. writeb(hrs, bq4802_regs + 0x04);
  1171. writeb(day, bq4802_regs + 0x06);
  1172. writeb(mon, bq4802_regs + 0x09);
  1173. writeb(yrs, bq4802_regs + 0x0a);
  1174. writeb(century, bq4802_regs + 0x0f);
  1175. writeb(val, bq4802_regs + 0x0e);
  1176. return 0;
  1177. }
  1178. #endif /* CONFIG_PCI */
  1179. struct mini_rtc_ops {
  1180. void (*get_rtc_time)(struct rtc_time *);
  1181. int (*set_rtc_time)(struct rtc_time *);
  1182. };
  1183. static struct mini_rtc_ops starfire_rtc_ops = {
  1184. .get_rtc_time = starfire_get_rtc_time,
  1185. .set_rtc_time = starfire_set_rtc_time,
  1186. };
  1187. static struct mini_rtc_ops hypervisor_rtc_ops = {
  1188. .get_rtc_time = hypervisor_get_rtc_time,
  1189. .set_rtc_time = hypervisor_set_rtc_time,
  1190. };
  1191. #ifdef CONFIG_PCI
  1192. static struct mini_rtc_ops bq4802_rtc_ops = {
  1193. .get_rtc_time = bq4802_get_rtc_time,
  1194. .set_rtc_time = bq4802_set_rtc_time,
  1195. };
  1196. #endif /* CONFIG_PCI */
  1197. static struct mini_rtc_ops *mini_rtc_ops;
  1198. static inline void mini_get_rtc_time(struct rtc_time *time)
  1199. {
  1200. unsigned long flags;
  1201. spin_lock_irqsave(&rtc_lock, flags);
  1202. mini_rtc_ops->get_rtc_time(time);
  1203. spin_unlock_irqrestore(&rtc_lock, flags);
  1204. }
  1205. static inline int mini_set_rtc_time(struct rtc_time *time)
  1206. {
  1207. unsigned long flags;
  1208. int err;
  1209. spin_lock_irqsave(&rtc_lock, flags);
  1210. err = mini_rtc_ops->set_rtc_time(time);
  1211. spin_unlock_irqrestore(&rtc_lock, flags);
  1212. return err;
  1213. }
  1214. static int mini_rtc_ioctl(struct inode *inode, struct file *file,
  1215. unsigned int cmd, unsigned long arg)
  1216. {
  1217. struct rtc_time wtime;
  1218. void __user *argp = (void __user *)arg;
  1219. switch (cmd) {
  1220. case RTC_PLL_GET:
  1221. return -EINVAL;
  1222. case RTC_PLL_SET:
  1223. return -EINVAL;
  1224. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  1225. return 0;
  1226. case RTC_UIE_ON: /* enable ints for RTC updates. */
  1227. return -EINVAL;
  1228. case RTC_RD_TIME: /* Read the time/date from RTC */
  1229. /* this doesn't get week-day, who cares */
  1230. memset(&wtime, 0, sizeof(wtime));
  1231. mini_get_rtc_time(&wtime);
  1232. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  1233. case RTC_SET_TIME: /* Set the RTC */
  1234. {
  1235. int year, days;
  1236. if (!capable(CAP_SYS_TIME))
  1237. return -EACCES;
  1238. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  1239. return -EFAULT;
  1240. year = wtime.tm_year + 1900;
  1241. days = month_days[wtime.tm_mon] +
  1242. ((wtime.tm_mon == 1) && leapyear(year));
  1243. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
  1244. (wtime.tm_mday < 1))
  1245. return -EINVAL;
  1246. if (wtime.tm_mday < 0 || wtime.tm_mday > days)
  1247. return -EINVAL;
  1248. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  1249. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  1250. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  1251. return -EINVAL;
  1252. return mini_set_rtc_time(&wtime);
  1253. }
  1254. }
  1255. return -EINVAL;
  1256. }
  1257. static int mini_rtc_open(struct inode *inode, struct file *file)
  1258. {
  1259. if (mini_rtc_status & RTC_IS_OPEN)
  1260. return -EBUSY;
  1261. mini_rtc_status |= RTC_IS_OPEN;
  1262. return 0;
  1263. }
  1264. static int mini_rtc_release(struct inode *inode, struct file *file)
  1265. {
  1266. mini_rtc_status &= ~RTC_IS_OPEN;
  1267. return 0;
  1268. }
  1269. static const struct file_operations mini_rtc_fops = {
  1270. .owner = THIS_MODULE,
  1271. .ioctl = mini_rtc_ioctl,
  1272. .open = mini_rtc_open,
  1273. .release = mini_rtc_release,
  1274. };
  1275. static struct miscdevice rtc_mini_dev =
  1276. {
  1277. .minor = RTC_MINOR,
  1278. .name = "rtc",
  1279. .fops = &mini_rtc_fops,
  1280. };
  1281. static int __init rtc_mini_init(void)
  1282. {
  1283. int retval;
  1284. if (tlb_type == hypervisor)
  1285. mini_rtc_ops = &hypervisor_rtc_ops;
  1286. else if (this_is_starfire)
  1287. mini_rtc_ops = &starfire_rtc_ops;
  1288. #ifdef CONFIG_PCI
  1289. else if (bq4802_regs)
  1290. mini_rtc_ops = &bq4802_rtc_ops;
  1291. #endif /* CONFIG_PCI */
  1292. else
  1293. return -ENODEV;
  1294. printk(KERN_INFO "Mini RTC Driver\n");
  1295. retval = misc_register(&rtc_mini_dev);
  1296. if (retval < 0)
  1297. return retval;
  1298. return 0;
  1299. }
  1300. static void __exit rtc_mini_exit(void)
  1301. {
  1302. misc_deregister(&rtc_mini_dev);
  1303. }
  1304. module_init(rtc_mini_init);
  1305. module_exit(rtc_mini_exit);