time.c 33 KB

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