time.c 41 KB

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