time.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  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. int update_persistent_clock(struct timespec now)
  343. {
  344. return set_rtc_mmss(now.tv_sec);
  345. }
  346. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  347. static void __init kick_start_clock(void)
  348. {
  349. void __iomem *regs = mstk48t02_regs;
  350. u8 sec, tmp;
  351. int i, count;
  352. prom_printf("CLOCK: Clock was stopped. Kick start ");
  353. spin_lock_irq(&mostek_lock);
  354. /* Turn on the kick start bit to start the oscillator. */
  355. tmp = mostek_read(regs + MOSTEK_CREG);
  356. tmp |= MSTK_CREG_WRITE;
  357. mostek_write(regs + MOSTEK_CREG, tmp);
  358. tmp = mostek_read(regs + MOSTEK_SEC);
  359. tmp &= ~MSTK_STOP;
  360. mostek_write(regs + MOSTEK_SEC, tmp);
  361. tmp = mostek_read(regs + MOSTEK_HOUR);
  362. tmp |= MSTK_KICK_START;
  363. mostek_write(regs + MOSTEK_HOUR, tmp);
  364. tmp = mostek_read(regs + MOSTEK_CREG);
  365. tmp &= ~MSTK_CREG_WRITE;
  366. mostek_write(regs + MOSTEK_CREG, tmp);
  367. spin_unlock_irq(&mostek_lock);
  368. /* Delay to allow the clock oscillator to start. */
  369. sec = MSTK_REG_SEC(regs);
  370. for (i = 0; i < 3; i++) {
  371. while (sec == MSTK_REG_SEC(regs))
  372. for (count = 0; count < 100000; count++)
  373. /* nothing */ ;
  374. prom_printf(".");
  375. sec = MSTK_REG_SEC(regs);
  376. }
  377. prom_printf("\n");
  378. spin_lock_irq(&mostek_lock);
  379. /* Turn off kick start and set a "valid" time and date. */
  380. tmp = mostek_read(regs + MOSTEK_CREG);
  381. tmp |= MSTK_CREG_WRITE;
  382. mostek_write(regs + MOSTEK_CREG, tmp);
  383. tmp = mostek_read(regs + MOSTEK_HOUR);
  384. tmp &= ~MSTK_KICK_START;
  385. mostek_write(regs + MOSTEK_HOUR, tmp);
  386. MSTK_SET_REG_SEC(regs,0);
  387. MSTK_SET_REG_MIN(regs,0);
  388. MSTK_SET_REG_HOUR(regs,0);
  389. MSTK_SET_REG_DOW(regs,5);
  390. MSTK_SET_REG_DOM(regs,1);
  391. MSTK_SET_REG_MONTH(regs,8);
  392. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  393. tmp = mostek_read(regs + MOSTEK_CREG);
  394. tmp &= ~MSTK_CREG_WRITE;
  395. mostek_write(regs + MOSTEK_CREG, tmp);
  396. spin_unlock_irq(&mostek_lock);
  397. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  398. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  399. prom_printf("CLOCK: Kick start still on!\n");
  400. spin_lock_irq(&mostek_lock);
  401. tmp = mostek_read(regs + MOSTEK_CREG);
  402. tmp |= MSTK_CREG_WRITE;
  403. mostek_write(regs + MOSTEK_CREG, tmp);
  404. tmp = mostek_read(regs + MOSTEK_HOUR);
  405. tmp &= ~MSTK_KICK_START;
  406. mostek_write(regs + MOSTEK_HOUR, tmp);
  407. tmp = mostek_read(regs + MOSTEK_CREG);
  408. tmp &= ~MSTK_CREG_WRITE;
  409. mostek_write(regs + MOSTEK_CREG, tmp);
  410. spin_unlock_irq(&mostek_lock);
  411. }
  412. prom_printf("CLOCK: Kick start procedure successful.\n");
  413. }
  414. /* Return nonzero if the clock chip battery is low. */
  415. static int __init has_low_battery(void)
  416. {
  417. void __iomem *regs = mstk48t02_regs;
  418. u8 data1, data2;
  419. spin_lock_irq(&mostek_lock);
  420. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  421. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  422. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  423. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  424. spin_unlock_irq(&mostek_lock);
  425. return (data1 == data2); /* Was the write blocked? */
  426. }
  427. /* Probe for the real time clock chip. */
  428. static void __init set_system_time(void)
  429. {
  430. unsigned int year, mon, day, hour, min, sec;
  431. void __iomem *mregs = mstk48t02_regs;
  432. #ifdef CONFIG_PCI
  433. unsigned long dregs = ds1287_regs;
  434. void __iomem *bregs = bq4802_regs;
  435. #else
  436. unsigned long dregs = 0UL;
  437. void __iomem *bregs = 0UL;
  438. #endif
  439. u8 tmp;
  440. if (!mregs && !dregs && !bregs) {
  441. prom_printf("Something wrong, clock regs not mapped yet.\n");
  442. prom_halt();
  443. }
  444. if (mregs) {
  445. spin_lock_irq(&mostek_lock);
  446. /* Traditional Mostek chip. */
  447. tmp = mostek_read(mregs + MOSTEK_CREG);
  448. tmp |= MSTK_CREG_READ;
  449. mostek_write(mregs + MOSTEK_CREG, tmp);
  450. sec = MSTK_REG_SEC(mregs);
  451. min = MSTK_REG_MIN(mregs);
  452. hour = MSTK_REG_HOUR(mregs);
  453. day = MSTK_REG_DOM(mregs);
  454. mon = MSTK_REG_MONTH(mregs);
  455. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  456. } else if (bregs) {
  457. unsigned char val = readb(bregs + 0x0e);
  458. unsigned int century;
  459. /* BQ4802 RTC chip. */
  460. writeb(val | 0x08, bregs + 0x0e);
  461. sec = readb(bregs + 0x00);
  462. min = readb(bregs + 0x02);
  463. hour = readb(bregs + 0x04);
  464. day = readb(bregs + 0x06);
  465. mon = readb(bregs + 0x09);
  466. year = readb(bregs + 0x0a);
  467. century = readb(bregs + 0x0f);
  468. writeb(val, bregs + 0x0e);
  469. BCD_TO_BIN(sec);
  470. BCD_TO_BIN(min);
  471. BCD_TO_BIN(hour);
  472. BCD_TO_BIN(day);
  473. BCD_TO_BIN(mon);
  474. BCD_TO_BIN(year);
  475. BCD_TO_BIN(century);
  476. year += (century * 100);
  477. } else {
  478. /* Dallas 12887 RTC chip. */
  479. do {
  480. sec = CMOS_READ(RTC_SECONDS);
  481. min = CMOS_READ(RTC_MINUTES);
  482. hour = CMOS_READ(RTC_HOURS);
  483. day = CMOS_READ(RTC_DAY_OF_MONTH);
  484. mon = CMOS_READ(RTC_MONTH);
  485. year = CMOS_READ(RTC_YEAR);
  486. } while (sec != CMOS_READ(RTC_SECONDS));
  487. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  488. BCD_TO_BIN(sec);
  489. BCD_TO_BIN(min);
  490. BCD_TO_BIN(hour);
  491. BCD_TO_BIN(day);
  492. BCD_TO_BIN(mon);
  493. BCD_TO_BIN(year);
  494. }
  495. if ((year += 1900) < 1970)
  496. year += 100;
  497. }
  498. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  499. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  500. set_normalized_timespec(&wall_to_monotonic,
  501. -xtime.tv_sec, -xtime.tv_nsec);
  502. if (mregs) {
  503. tmp = mostek_read(mregs + MOSTEK_CREG);
  504. tmp &= ~MSTK_CREG_READ;
  505. mostek_write(mregs + MOSTEK_CREG, tmp);
  506. spin_unlock_irq(&mostek_lock);
  507. }
  508. }
  509. /* davem suggests we keep this within the 4M locked kernel image */
  510. static u32 starfire_get_time(void)
  511. {
  512. static char obp_gettod[32];
  513. static u32 unix_tod;
  514. sprintf(obp_gettod, "h# %08x unix-gettod",
  515. (unsigned int) (long) &unix_tod);
  516. prom_feval(obp_gettod);
  517. return unix_tod;
  518. }
  519. static int starfire_set_time(u32 val)
  520. {
  521. /* Do nothing, time is set using the service processor
  522. * console on this platform.
  523. */
  524. return 0;
  525. }
  526. static u32 hypervisor_get_time(void)
  527. {
  528. unsigned long ret, time;
  529. int retries = 10000;
  530. retry:
  531. ret = sun4v_tod_get(&time);
  532. if (ret == HV_EOK)
  533. return time;
  534. if (ret == HV_EWOULDBLOCK) {
  535. if (--retries > 0) {
  536. udelay(100);
  537. goto retry;
  538. }
  539. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  540. return 0;
  541. }
  542. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  543. return 0;
  544. }
  545. static int hypervisor_set_time(u32 secs)
  546. {
  547. unsigned long ret;
  548. int retries = 10000;
  549. retry:
  550. ret = sun4v_tod_set(secs);
  551. if (ret == HV_EOK)
  552. return 0;
  553. if (ret == HV_EWOULDBLOCK) {
  554. if (--retries > 0) {
  555. udelay(100);
  556. goto retry;
  557. }
  558. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  559. return -EAGAIN;
  560. }
  561. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  562. return -EOPNOTSUPP;
  563. }
  564. static int __init clock_model_matches(const char *model)
  565. {
  566. if (strcmp(model, "mk48t02") &&
  567. strcmp(model, "mk48t08") &&
  568. strcmp(model, "mk48t59") &&
  569. strcmp(model, "m5819") &&
  570. strcmp(model, "m5819p") &&
  571. strcmp(model, "m5823") &&
  572. strcmp(model, "ds1287") &&
  573. strcmp(model, "bq4802"))
  574. return 0;
  575. return 1;
  576. }
  577. static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
  578. {
  579. struct device_node *dp = op->node;
  580. const char *model = of_get_property(dp, "model", NULL);
  581. const char *compat = of_get_property(dp, "compatible", NULL);
  582. unsigned long size, flags;
  583. void __iomem *regs;
  584. if (!model)
  585. model = compat;
  586. if (!model || !clock_model_matches(model))
  587. return -ENODEV;
  588. /* On an Enterprise system there can be multiple mostek clocks.
  589. * We should only match the one that is on the central FHC bus.
  590. */
  591. if (!strcmp(dp->parent->name, "fhc") &&
  592. strcmp(dp->parent->parent->name, "central") != 0)
  593. return -ENODEV;
  594. size = (op->resource[0].end - op->resource[0].start) + 1;
  595. regs = of_ioremap(&op->resource[0], 0, size, "clock");
  596. if (!regs)
  597. return -ENOMEM;
  598. #ifdef CONFIG_PCI
  599. if (!strcmp(model, "ds1287") ||
  600. !strcmp(model, "m5819") ||
  601. !strcmp(model, "m5819p") ||
  602. !strcmp(model, "m5823")) {
  603. ds1287_regs = (unsigned long) regs;
  604. } else if (!strcmp(model, "bq4802")) {
  605. bq4802_regs = regs;
  606. } else
  607. #endif
  608. if (model[5] == '0' && model[6] == '2') {
  609. mstk48t02_regs = regs;
  610. } else if(model[5] == '0' && model[6] == '8') {
  611. mstk48t08_regs = regs;
  612. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  613. } else {
  614. mstk48t59_regs = regs;
  615. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  616. }
  617. printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
  618. local_irq_save(flags);
  619. if (mstk48t02_regs != NULL) {
  620. /* Report a low battery voltage condition. */
  621. if (has_low_battery())
  622. prom_printf("NVRAM: Low battery voltage!\n");
  623. /* Kick start the clock if it is completely stopped. */
  624. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  625. kick_start_clock();
  626. }
  627. set_system_time();
  628. local_irq_restore(flags);
  629. return 0;
  630. }
  631. static struct of_device_id clock_match[] = {
  632. {
  633. .name = "eeprom",
  634. },
  635. {
  636. .name = "rtc",
  637. },
  638. {},
  639. };
  640. static struct of_platform_driver clock_driver = {
  641. .match_table = clock_match,
  642. .probe = clock_probe,
  643. .driver = {
  644. .name = "clock",
  645. },
  646. };
  647. static int __init clock_init(void)
  648. {
  649. if (this_is_starfire) {
  650. xtime.tv_sec = starfire_get_time();
  651. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  652. set_normalized_timespec(&wall_to_monotonic,
  653. -xtime.tv_sec, -xtime.tv_nsec);
  654. return 0;
  655. }
  656. if (tlb_type == hypervisor) {
  657. xtime.tv_sec = hypervisor_get_time();
  658. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  659. set_normalized_timespec(&wall_to_monotonic,
  660. -xtime.tv_sec, -xtime.tv_nsec);
  661. return 0;
  662. }
  663. return of_register_driver(&clock_driver, &of_platform_bus_type);
  664. }
  665. /* Must be after subsys_initcall() so that busses are probed. Must
  666. * be before device_initcall() because things like the RTC driver
  667. * need to see the clock registers.
  668. */
  669. fs_initcall(clock_init);
  670. /* This is gets the master TICK_INT timer going. */
  671. static unsigned long sparc64_init_timers(void)
  672. {
  673. struct device_node *dp;
  674. unsigned long clock;
  675. dp = of_find_node_by_path("/");
  676. if (tlb_type == spitfire) {
  677. unsigned long ver, manuf, impl;
  678. __asm__ __volatile__ ("rdpr %%ver, %0"
  679. : "=&r" (ver));
  680. manuf = ((ver >> 48) & 0xffff);
  681. impl = ((ver >> 32) & 0xffff);
  682. if (manuf == 0x17 && impl == 0x13) {
  683. /* Hummingbird, aka Ultra-IIe */
  684. tick_ops = &hbtick_operations;
  685. clock = of_getintprop_default(dp, "stick-frequency", 0);
  686. } else {
  687. tick_ops = &tick_operations;
  688. clock = local_cpu_data().clock_tick;
  689. }
  690. } else {
  691. tick_ops = &stick_operations;
  692. clock = of_getintprop_default(dp, "stick-frequency", 0);
  693. }
  694. return clock;
  695. }
  696. struct freq_table {
  697. unsigned long clock_tick_ref;
  698. unsigned int ref_freq;
  699. };
  700. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  701. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  702. {
  703. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  704. if (ft->clock_tick_ref)
  705. return ft->clock_tick_ref;
  706. return cpu_data(cpu).clock_tick;
  707. }
  708. #ifdef CONFIG_CPU_FREQ
  709. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  710. void *data)
  711. {
  712. struct cpufreq_freqs *freq = data;
  713. unsigned int cpu = freq->cpu;
  714. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  715. if (!ft->ref_freq) {
  716. ft->ref_freq = freq->old;
  717. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  718. }
  719. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  720. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  721. (val == CPUFREQ_RESUMECHANGE)) {
  722. cpu_data(cpu).clock_tick =
  723. cpufreq_scale(ft->clock_tick_ref,
  724. ft->ref_freq,
  725. freq->new);
  726. }
  727. return 0;
  728. }
  729. static struct notifier_block sparc64_cpufreq_notifier_block = {
  730. .notifier_call = sparc64_cpufreq_notifier
  731. };
  732. #endif /* CONFIG_CPU_FREQ */
  733. static int sparc64_next_event(unsigned long delta,
  734. struct clock_event_device *evt)
  735. {
  736. return tick_ops->add_compare(delta) ? -ETIME : 0;
  737. }
  738. static void sparc64_timer_setup(enum clock_event_mode mode,
  739. struct clock_event_device *evt)
  740. {
  741. switch (mode) {
  742. case CLOCK_EVT_MODE_ONESHOT:
  743. case CLOCK_EVT_MODE_RESUME:
  744. break;
  745. case CLOCK_EVT_MODE_SHUTDOWN:
  746. tick_ops->disable_irq();
  747. break;
  748. case CLOCK_EVT_MODE_PERIODIC:
  749. case CLOCK_EVT_MODE_UNUSED:
  750. WARN_ON(1);
  751. break;
  752. };
  753. }
  754. static struct clock_event_device sparc64_clockevent = {
  755. .features = CLOCK_EVT_FEAT_ONESHOT,
  756. .set_mode = sparc64_timer_setup,
  757. .set_next_event = sparc64_next_event,
  758. .rating = 100,
  759. .shift = 30,
  760. .irq = -1,
  761. };
  762. static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
  763. void timer_interrupt(int irq, struct pt_regs *regs)
  764. {
  765. struct pt_regs *old_regs = set_irq_regs(regs);
  766. unsigned long tick_mask = tick_ops->softint_mask;
  767. int cpu = smp_processor_id();
  768. struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
  769. clear_softint(tick_mask);
  770. irq_enter();
  771. kstat_this_cpu.irqs[0]++;
  772. if (unlikely(!evt->event_handler)) {
  773. printk(KERN_WARNING
  774. "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
  775. } else
  776. evt->event_handler(evt);
  777. irq_exit();
  778. set_irq_regs(old_regs);
  779. }
  780. void __devinit setup_sparc64_timer(void)
  781. {
  782. struct clock_event_device *sevt;
  783. unsigned long pstate;
  784. /* Guarantee that the following sequences execute
  785. * uninterrupted.
  786. */
  787. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  788. "wrpr %0, %1, %%pstate"
  789. : "=r" (pstate)
  790. : "i" (PSTATE_IE));
  791. tick_ops->init_tick();
  792. /* Restore PSTATE_IE. */
  793. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  794. : /* no outputs */
  795. : "r" (pstate));
  796. sevt = &__get_cpu_var(sparc64_events);
  797. memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
  798. sevt->cpumask = cpumask_of_cpu(smp_processor_id());
  799. clockevents_register_device(sevt);
  800. }
  801. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  802. static struct clocksource clocksource_tick = {
  803. .rating = 100,
  804. .mask = CLOCKSOURCE_MASK(64),
  805. .shift = 16,
  806. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  807. };
  808. static void __init setup_clockevent_multiplier(unsigned long hz)
  809. {
  810. unsigned long mult, shift = 32;
  811. while (1) {
  812. mult = div_sc(hz, NSEC_PER_SEC, shift);
  813. if (mult && (mult >> 32UL) == 0UL)
  814. break;
  815. shift--;
  816. }
  817. sparc64_clockevent.shift = shift;
  818. sparc64_clockevent.mult = mult;
  819. }
  820. static unsigned long tb_ticks_per_usec __read_mostly;
  821. void __delay(unsigned long loops)
  822. {
  823. unsigned long bclock, now;
  824. bclock = tick_ops->get_tick();
  825. do {
  826. now = tick_ops->get_tick();
  827. } while ((now-bclock) < loops);
  828. }
  829. EXPORT_SYMBOL(__delay);
  830. void udelay(unsigned long usecs)
  831. {
  832. __delay(tb_ticks_per_usec * usecs);
  833. }
  834. EXPORT_SYMBOL(udelay);
  835. void __init time_init(void)
  836. {
  837. unsigned long clock = sparc64_init_timers();
  838. tb_ticks_per_usec = clock / USEC_PER_SEC;
  839. timer_ticks_per_nsec_quotient =
  840. clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
  841. clocksource_tick.name = tick_ops->name;
  842. clocksource_tick.mult =
  843. clocksource_hz2mult(clock,
  844. clocksource_tick.shift);
  845. clocksource_tick.read = tick_ops->get_tick;
  846. printk("clocksource: mult[%x] shift[%d]\n",
  847. clocksource_tick.mult, clocksource_tick.shift);
  848. clocksource_register(&clocksource_tick);
  849. sparc64_clockevent.name = tick_ops->name;
  850. setup_clockevent_multiplier(clock);
  851. sparc64_clockevent.max_delta_ns =
  852. clockevent_delta2ns(0x7fffffffffffffff, &sparc64_clockevent);
  853. sparc64_clockevent.min_delta_ns =
  854. clockevent_delta2ns(0xF, &sparc64_clockevent);
  855. printk("clockevent: mult[%lx] shift[%d]\n",
  856. sparc64_clockevent.mult, sparc64_clockevent.shift);
  857. setup_sparc64_timer();
  858. #ifdef CONFIG_CPU_FREQ
  859. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  860. CPUFREQ_TRANSITION_NOTIFIER);
  861. #endif
  862. }
  863. unsigned long long sched_clock(void)
  864. {
  865. unsigned long ticks = tick_ops->get_tick();
  866. return (ticks * timer_ticks_per_nsec_quotient)
  867. >> SPARC64_NSEC_PER_CYC_SHIFT;
  868. }
  869. static int set_rtc_mmss(unsigned long nowtime)
  870. {
  871. int real_seconds, real_minutes, chip_minutes;
  872. void __iomem *mregs = mstk48t02_regs;
  873. #ifdef CONFIG_PCI
  874. unsigned long dregs = ds1287_regs;
  875. void __iomem *bregs = bq4802_regs;
  876. #else
  877. unsigned long dregs = 0UL;
  878. void __iomem *bregs = 0UL;
  879. #endif
  880. unsigned long flags;
  881. u8 tmp;
  882. /*
  883. * Not having a register set can lead to trouble.
  884. * Also starfire doesn't have a tod clock.
  885. */
  886. if (!mregs && !dregs & !bregs)
  887. return -1;
  888. if (mregs) {
  889. spin_lock_irqsave(&mostek_lock, flags);
  890. /* Read the current RTC minutes. */
  891. tmp = mostek_read(mregs + MOSTEK_CREG);
  892. tmp |= MSTK_CREG_READ;
  893. mostek_write(mregs + MOSTEK_CREG, tmp);
  894. chip_minutes = MSTK_REG_MIN(mregs);
  895. tmp = mostek_read(mregs + MOSTEK_CREG);
  896. tmp &= ~MSTK_CREG_READ;
  897. mostek_write(mregs + MOSTEK_CREG, tmp);
  898. /*
  899. * since we're only adjusting minutes and seconds,
  900. * don't interfere with hour overflow. This avoids
  901. * messing with unknown time zones but requires your
  902. * RTC not to be off by more than 15 minutes
  903. */
  904. real_seconds = nowtime % 60;
  905. real_minutes = nowtime / 60;
  906. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  907. real_minutes += 30; /* correct for half hour time zone */
  908. real_minutes %= 60;
  909. if (abs(real_minutes - chip_minutes) < 30) {
  910. tmp = mostek_read(mregs + MOSTEK_CREG);
  911. tmp |= MSTK_CREG_WRITE;
  912. mostek_write(mregs + MOSTEK_CREG, tmp);
  913. MSTK_SET_REG_SEC(mregs,real_seconds);
  914. MSTK_SET_REG_MIN(mregs,real_minutes);
  915. tmp = mostek_read(mregs + MOSTEK_CREG);
  916. tmp &= ~MSTK_CREG_WRITE;
  917. mostek_write(mregs + MOSTEK_CREG, tmp);
  918. spin_unlock_irqrestore(&mostek_lock, flags);
  919. return 0;
  920. } else {
  921. spin_unlock_irqrestore(&mostek_lock, flags);
  922. return -1;
  923. }
  924. } else if (bregs) {
  925. int retval = 0;
  926. unsigned char val = readb(bregs + 0x0e);
  927. /* BQ4802 RTC chip. */
  928. writeb(val | 0x08, bregs + 0x0e);
  929. chip_minutes = readb(bregs + 0x02);
  930. BCD_TO_BIN(chip_minutes);
  931. real_seconds = nowtime % 60;
  932. real_minutes = nowtime / 60;
  933. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  934. real_minutes += 30;
  935. real_minutes %= 60;
  936. if (abs(real_minutes - chip_minutes) < 30) {
  937. BIN_TO_BCD(real_seconds);
  938. BIN_TO_BCD(real_minutes);
  939. writeb(real_seconds, bregs + 0x00);
  940. writeb(real_minutes, bregs + 0x02);
  941. } else {
  942. printk(KERN_WARNING
  943. "set_rtc_mmss: can't update from %d to %d\n",
  944. chip_minutes, real_minutes);
  945. retval = -1;
  946. }
  947. writeb(val, bregs + 0x0e);
  948. return retval;
  949. } else {
  950. int retval = 0;
  951. unsigned char save_control, save_freq_select;
  952. /* Stolen from arch/i386/kernel/time.c, see there for
  953. * credits and descriptive comments.
  954. */
  955. spin_lock_irqsave(&rtc_lock, flags);
  956. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  957. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  958. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  959. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  960. chip_minutes = CMOS_READ(RTC_MINUTES);
  961. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  962. BCD_TO_BIN(chip_minutes);
  963. real_seconds = nowtime % 60;
  964. real_minutes = nowtime / 60;
  965. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  966. real_minutes += 30;
  967. real_minutes %= 60;
  968. if (abs(real_minutes - chip_minutes) < 30) {
  969. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  970. BIN_TO_BCD(real_seconds);
  971. BIN_TO_BCD(real_minutes);
  972. }
  973. CMOS_WRITE(real_seconds,RTC_SECONDS);
  974. CMOS_WRITE(real_minutes,RTC_MINUTES);
  975. } else {
  976. printk(KERN_WARNING
  977. "set_rtc_mmss: can't update from %d to %d\n",
  978. chip_minutes, real_minutes);
  979. retval = -1;
  980. }
  981. CMOS_WRITE(save_control, RTC_CONTROL);
  982. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  983. spin_unlock_irqrestore(&rtc_lock, flags);
  984. return retval;
  985. }
  986. }
  987. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  988. static unsigned char mini_rtc_status; /* bitmapped status byte. */
  989. #define FEBRUARY 2
  990. #define STARTOFTIME 1970
  991. #define SECDAY 86400L
  992. #define SECYR (SECDAY * 365)
  993. #define leapyear(year) ((year) % 4 == 0 && \
  994. ((year) % 100 != 0 || (year) % 400 == 0))
  995. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  996. #define days_in_month(a) (month_days[(a) - 1])
  997. static int month_days[12] = {
  998. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  999. };
  1000. /*
  1001. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  1002. */
  1003. static void GregorianDay(struct rtc_time * tm)
  1004. {
  1005. int leapsToDate;
  1006. int lastYear;
  1007. int day;
  1008. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1009. lastYear = tm->tm_year - 1;
  1010. /*
  1011. * Number of leap corrections to apply up to end of last year
  1012. */
  1013. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  1014. /*
  1015. * This year is a leap year if it is divisible by 4 except when it is
  1016. * divisible by 100 unless it is divisible by 400
  1017. *
  1018. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  1019. */
  1020. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  1021. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  1022. tm->tm_mday;
  1023. tm->tm_wday = day % 7;
  1024. }
  1025. static void to_tm(int tim, struct rtc_time *tm)
  1026. {
  1027. register int i;
  1028. register long hms, day;
  1029. day = tim / SECDAY;
  1030. hms = tim % SECDAY;
  1031. /* Hours, minutes, seconds are easy */
  1032. tm->tm_hour = hms / 3600;
  1033. tm->tm_min = (hms % 3600) / 60;
  1034. tm->tm_sec = (hms % 3600) % 60;
  1035. /* Number of years in days */
  1036. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  1037. day -= days_in_year(i);
  1038. tm->tm_year = i;
  1039. /* Number of months in days left */
  1040. if (leapyear(tm->tm_year))
  1041. days_in_month(FEBRUARY) = 29;
  1042. for (i = 1; day >= days_in_month(i); i++)
  1043. day -= days_in_month(i);
  1044. days_in_month(FEBRUARY) = 28;
  1045. tm->tm_mon = i;
  1046. /* Days are what is left over (+1) from all that. */
  1047. tm->tm_mday = day + 1;
  1048. /*
  1049. * Determine the day of week
  1050. */
  1051. GregorianDay(tm);
  1052. }
  1053. /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
  1054. * aka Unix time. So we have to convert to/from rtc_time.
  1055. */
  1056. static void starfire_get_rtc_time(struct rtc_time *time)
  1057. {
  1058. u32 seconds = starfire_get_time();
  1059. to_tm(seconds, time);
  1060. time->tm_year -= 1900;
  1061. time->tm_mon -= 1;
  1062. }
  1063. static int starfire_set_rtc_time(struct rtc_time *time)
  1064. {
  1065. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1066. time->tm_mday, time->tm_hour,
  1067. time->tm_min, time->tm_sec);
  1068. return starfire_set_time(seconds);
  1069. }
  1070. static void hypervisor_get_rtc_time(struct rtc_time *time)
  1071. {
  1072. u32 seconds = hypervisor_get_time();
  1073. to_tm(seconds, time);
  1074. time->tm_year -= 1900;
  1075. time->tm_mon -= 1;
  1076. }
  1077. static int hypervisor_set_rtc_time(struct rtc_time *time)
  1078. {
  1079. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1080. time->tm_mday, time->tm_hour,
  1081. time->tm_min, time->tm_sec);
  1082. return hypervisor_set_time(seconds);
  1083. }
  1084. #ifdef CONFIG_PCI
  1085. static void bq4802_get_rtc_time(struct rtc_time *time)
  1086. {
  1087. unsigned char val = readb(bq4802_regs + 0x0e);
  1088. unsigned int century;
  1089. writeb(val | 0x08, bq4802_regs + 0x0e);
  1090. time->tm_sec = readb(bq4802_regs + 0x00);
  1091. time->tm_min = readb(bq4802_regs + 0x02);
  1092. time->tm_hour = readb(bq4802_regs + 0x04);
  1093. time->tm_mday = readb(bq4802_regs + 0x06);
  1094. time->tm_mon = readb(bq4802_regs + 0x09);
  1095. time->tm_year = readb(bq4802_regs + 0x0a);
  1096. time->tm_wday = readb(bq4802_regs + 0x08);
  1097. century = readb(bq4802_regs + 0x0f);
  1098. writeb(val, bq4802_regs + 0x0e);
  1099. BCD_TO_BIN(time->tm_sec);
  1100. BCD_TO_BIN(time->tm_min);
  1101. BCD_TO_BIN(time->tm_hour);
  1102. BCD_TO_BIN(time->tm_mday);
  1103. BCD_TO_BIN(time->tm_mon);
  1104. BCD_TO_BIN(time->tm_year);
  1105. BCD_TO_BIN(time->tm_wday);
  1106. BCD_TO_BIN(century);
  1107. time->tm_year += (century * 100);
  1108. time->tm_year -= 1900;
  1109. time->tm_mon--;
  1110. }
  1111. static int bq4802_set_rtc_time(struct rtc_time *time)
  1112. {
  1113. unsigned char val = readb(bq4802_regs + 0x0e);
  1114. unsigned char sec, min, hrs, day, mon, yrs, century;
  1115. unsigned int year;
  1116. year = time->tm_year + 1900;
  1117. century = year / 100;
  1118. yrs = year % 100;
  1119. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  1120. day = time->tm_mday;
  1121. hrs = time->tm_hour;
  1122. min = time->tm_min;
  1123. sec = time->tm_sec;
  1124. BIN_TO_BCD(sec);
  1125. BIN_TO_BCD(min);
  1126. BIN_TO_BCD(hrs);
  1127. BIN_TO_BCD(day);
  1128. BIN_TO_BCD(mon);
  1129. BIN_TO_BCD(yrs);
  1130. BIN_TO_BCD(century);
  1131. writeb(val | 0x08, bq4802_regs + 0x0e);
  1132. writeb(sec, bq4802_regs + 0x00);
  1133. writeb(min, bq4802_regs + 0x02);
  1134. writeb(hrs, bq4802_regs + 0x04);
  1135. writeb(day, bq4802_regs + 0x06);
  1136. writeb(mon, bq4802_regs + 0x09);
  1137. writeb(yrs, bq4802_regs + 0x0a);
  1138. writeb(century, bq4802_regs + 0x0f);
  1139. writeb(val, bq4802_regs + 0x0e);
  1140. return 0;
  1141. }
  1142. static void cmos_get_rtc_time(struct rtc_time *rtc_tm)
  1143. {
  1144. unsigned char ctrl;
  1145. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  1146. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  1147. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  1148. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  1149. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  1150. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  1151. rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
  1152. ctrl = CMOS_READ(RTC_CONTROL);
  1153. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1154. BCD_TO_BIN(rtc_tm->tm_sec);
  1155. BCD_TO_BIN(rtc_tm->tm_min);
  1156. BCD_TO_BIN(rtc_tm->tm_hour);
  1157. BCD_TO_BIN(rtc_tm->tm_mday);
  1158. BCD_TO_BIN(rtc_tm->tm_mon);
  1159. BCD_TO_BIN(rtc_tm->tm_year);
  1160. BCD_TO_BIN(rtc_tm->tm_wday);
  1161. }
  1162. if (rtc_tm->tm_year <= 69)
  1163. rtc_tm->tm_year += 100;
  1164. rtc_tm->tm_mon--;
  1165. }
  1166. static int cmos_set_rtc_time(struct rtc_time *rtc_tm)
  1167. {
  1168. unsigned char mon, day, hrs, min, sec;
  1169. unsigned char save_control, save_freq_select;
  1170. unsigned int yrs;
  1171. yrs = rtc_tm->tm_year;
  1172. mon = rtc_tm->tm_mon + 1;
  1173. day = rtc_tm->tm_mday;
  1174. hrs = rtc_tm->tm_hour;
  1175. min = rtc_tm->tm_min;
  1176. sec = rtc_tm->tm_sec;
  1177. if (yrs >= 100)
  1178. yrs -= 100;
  1179. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1180. BIN_TO_BCD(sec);
  1181. BIN_TO_BCD(min);
  1182. BIN_TO_BCD(hrs);
  1183. BIN_TO_BCD(day);
  1184. BIN_TO_BCD(mon);
  1185. BIN_TO_BCD(yrs);
  1186. }
  1187. save_control = CMOS_READ(RTC_CONTROL);
  1188. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  1189. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1190. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  1191. CMOS_WRITE(yrs, RTC_YEAR);
  1192. CMOS_WRITE(mon, RTC_MONTH);
  1193. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  1194. CMOS_WRITE(hrs, RTC_HOURS);
  1195. CMOS_WRITE(min, RTC_MINUTES);
  1196. CMOS_WRITE(sec, RTC_SECONDS);
  1197. CMOS_WRITE(save_control, RTC_CONTROL);
  1198. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1199. return 0;
  1200. }
  1201. #endif /* CONFIG_PCI */
  1202. static void mostek_get_rtc_time(struct rtc_time *rtc_tm)
  1203. {
  1204. void __iomem *regs = mstk48t02_regs;
  1205. u8 tmp;
  1206. spin_lock_irq(&mostek_lock);
  1207. tmp = mostek_read(regs + MOSTEK_CREG);
  1208. tmp |= MSTK_CREG_READ;
  1209. mostek_write(regs + MOSTEK_CREG, tmp);
  1210. rtc_tm->tm_sec = MSTK_REG_SEC(regs);
  1211. rtc_tm->tm_min = MSTK_REG_MIN(regs);
  1212. rtc_tm->tm_hour = MSTK_REG_HOUR(regs);
  1213. rtc_tm->tm_mday = MSTK_REG_DOM(regs);
  1214. rtc_tm->tm_mon = MSTK_REG_MONTH(regs);
  1215. rtc_tm->tm_year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
  1216. rtc_tm->tm_wday = MSTK_REG_DOW(regs);
  1217. tmp = mostek_read(regs + MOSTEK_CREG);
  1218. tmp &= ~MSTK_CREG_READ;
  1219. mostek_write(regs + MOSTEK_CREG, tmp);
  1220. spin_unlock_irq(&mostek_lock);
  1221. rtc_tm->tm_mon--;
  1222. rtc_tm->tm_wday--;
  1223. rtc_tm->tm_year -= 1900;
  1224. }
  1225. static int mostek_set_rtc_time(struct rtc_time *rtc_tm)
  1226. {
  1227. unsigned char mon, day, hrs, min, sec, wday;
  1228. void __iomem *regs = mstk48t02_regs;
  1229. unsigned int yrs;
  1230. u8 tmp;
  1231. yrs = rtc_tm->tm_year + 1900;
  1232. mon = rtc_tm->tm_mon + 1;
  1233. day = rtc_tm->tm_mday;
  1234. wday = rtc_tm->tm_wday + 1;
  1235. hrs = rtc_tm->tm_hour;
  1236. min = rtc_tm->tm_min;
  1237. sec = rtc_tm->tm_sec;
  1238. spin_lock_irq(&mostek_lock);
  1239. tmp = mostek_read(regs + MOSTEK_CREG);
  1240. tmp |= MSTK_CREG_WRITE;
  1241. mostek_write(regs + MOSTEK_CREG, tmp);
  1242. MSTK_SET_REG_SEC(regs, sec);
  1243. MSTK_SET_REG_MIN(regs, min);
  1244. MSTK_SET_REG_HOUR(regs, hrs);
  1245. MSTK_SET_REG_DOW(regs, wday);
  1246. MSTK_SET_REG_DOM(regs, day);
  1247. MSTK_SET_REG_MONTH(regs, mon);
  1248. MSTK_SET_REG_YEAR(regs, yrs - MSTK_YEAR_ZERO);
  1249. tmp = mostek_read(regs + MOSTEK_CREG);
  1250. tmp &= ~MSTK_CREG_WRITE;
  1251. mostek_write(regs + MOSTEK_CREG, tmp);
  1252. spin_unlock_irq(&mostek_lock);
  1253. return 0;
  1254. }
  1255. struct mini_rtc_ops {
  1256. void (*get_rtc_time)(struct rtc_time *);
  1257. int (*set_rtc_time)(struct rtc_time *);
  1258. };
  1259. static struct mini_rtc_ops starfire_rtc_ops = {
  1260. .get_rtc_time = starfire_get_rtc_time,
  1261. .set_rtc_time = starfire_set_rtc_time,
  1262. };
  1263. static struct mini_rtc_ops hypervisor_rtc_ops = {
  1264. .get_rtc_time = hypervisor_get_rtc_time,
  1265. .set_rtc_time = hypervisor_set_rtc_time,
  1266. };
  1267. #ifdef CONFIG_PCI
  1268. static struct mini_rtc_ops bq4802_rtc_ops = {
  1269. .get_rtc_time = bq4802_get_rtc_time,
  1270. .set_rtc_time = bq4802_set_rtc_time,
  1271. };
  1272. static struct mini_rtc_ops cmos_rtc_ops = {
  1273. .get_rtc_time = cmos_get_rtc_time,
  1274. .set_rtc_time = cmos_set_rtc_time,
  1275. };
  1276. #endif /* CONFIG_PCI */
  1277. static struct mini_rtc_ops mostek_rtc_ops = {
  1278. .get_rtc_time = mostek_get_rtc_time,
  1279. .set_rtc_time = mostek_set_rtc_time,
  1280. };
  1281. static struct mini_rtc_ops *mini_rtc_ops;
  1282. static inline void mini_get_rtc_time(struct rtc_time *time)
  1283. {
  1284. unsigned long flags;
  1285. spin_lock_irqsave(&rtc_lock, flags);
  1286. mini_rtc_ops->get_rtc_time(time);
  1287. spin_unlock_irqrestore(&rtc_lock, flags);
  1288. }
  1289. static inline int mini_set_rtc_time(struct rtc_time *time)
  1290. {
  1291. unsigned long flags;
  1292. int err;
  1293. spin_lock_irqsave(&rtc_lock, flags);
  1294. err = mini_rtc_ops->set_rtc_time(time);
  1295. spin_unlock_irqrestore(&rtc_lock, flags);
  1296. return err;
  1297. }
  1298. static int mini_rtc_ioctl(struct inode *inode, struct file *file,
  1299. unsigned int cmd, unsigned long arg)
  1300. {
  1301. struct rtc_time wtime;
  1302. void __user *argp = (void __user *)arg;
  1303. switch (cmd) {
  1304. case RTC_PLL_GET:
  1305. return -EINVAL;
  1306. case RTC_PLL_SET:
  1307. return -EINVAL;
  1308. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  1309. return 0;
  1310. case RTC_UIE_ON: /* enable ints for RTC updates. */
  1311. return -EINVAL;
  1312. case RTC_RD_TIME: /* Read the time/date from RTC */
  1313. /* this doesn't get week-day, who cares */
  1314. memset(&wtime, 0, sizeof(wtime));
  1315. mini_get_rtc_time(&wtime);
  1316. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  1317. case RTC_SET_TIME: /* Set the RTC */
  1318. {
  1319. int year, days;
  1320. if (!capable(CAP_SYS_TIME))
  1321. return -EACCES;
  1322. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  1323. return -EFAULT;
  1324. year = wtime.tm_year + 1900;
  1325. days = month_days[wtime.tm_mon] +
  1326. ((wtime.tm_mon == 1) && leapyear(year));
  1327. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
  1328. (wtime.tm_mday < 1))
  1329. return -EINVAL;
  1330. if (wtime.tm_mday < 0 || wtime.tm_mday > days)
  1331. return -EINVAL;
  1332. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  1333. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  1334. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  1335. return -EINVAL;
  1336. return mini_set_rtc_time(&wtime);
  1337. }
  1338. }
  1339. return -EINVAL;
  1340. }
  1341. static int mini_rtc_open(struct inode *inode, struct file *file)
  1342. {
  1343. if (mini_rtc_status & RTC_IS_OPEN)
  1344. return -EBUSY;
  1345. mini_rtc_status |= RTC_IS_OPEN;
  1346. return 0;
  1347. }
  1348. static int mini_rtc_release(struct inode *inode, struct file *file)
  1349. {
  1350. mini_rtc_status &= ~RTC_IS_OPEN;
  1351. return 0;
  1352. }
  1353. static const struct file_operations mini_rtc_fops = {
  1354. .owner = THIS_MODULE,
  1355. .ioctl = mini_rtc_ioctl,
  1356. .open = mini_rtc_open,
  1357. .release = mini_rtc_release,
  1358. };
  1359. static struct miscdevice rtc_mini_dev =
  1360. {
  1361. .minor = RTC_MINOR,
  1362. .name = "rtc",
  1363. .fops = &mini_rtc_fops,
  1364. };
  1365. static int __init rtc_mini_init(void)
  1366. {
  1367. int retval;
  1368. if (tlb_type == hypervisor)
  1369. mini_rtc_ops = &hypervisor_rtc_ops;
  1370. else if (this_is_starfire)
  1371. mini_rtc_ops = &starfire_rtc_ops;
  1372. #ifdef CONFIG_PCI
  1373. else if (bq4802_regs)
  1374. mini_rtc_ops = &bq4802_rtc_ops;
  1375. else if (ds1287_regs)
  1376. mini_rtc_ops = &cmos_rtc_ops;
  1377. #endif /* CONFIG_PCI */
  1378. else if (mstk48t02_regs)
  1379. mini_rtc_ops = &mostek_rtc_ops;
  1380. else
  1381. return -ENODEV;
  1382. printk(KERN_INFO "Mini RTC Driver\n");
  1383. retval = misc_register(&rtc_mini_dev);
  1384. if (retval < 0)
  1385. return retval;
  1386. return 0;
  1387. }
  1388. static void __exit rtc_mini_exit(void)
  1389. {
  1390. misc_deregister(&rtc_mini_dev);
  1391. }
  1392. module_init(rtc_mini_init);
  1393. module_exit(rtc_mini_exit);