time.c 33 KB

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