time.c 41 KB

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