time.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /* $Id: time.c,v 1.42 2002/01/23 14:33:55 davem Exp $
  2. * time.c: UltraSparc timer and TOD clock support.
  3. *
  4. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  6. *
  7. * Based largely on code which is:
  8. *
  9. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/param.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/time.h>
  20. #include <linux/timex.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/mc146818rtc.h>
  24. #include <linux/delay.h>
  25. #include <linux/profile.h>
  26. #include <linux/bcd.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/percpu.h>
  30. #include <linux/profile.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/rtc.h>
  33. #include <asm/oplib.h>
  34. #include <asm/mostek.h>
  35. #include <asm/timer.h>
  36. #include <asm/irq.h>
  37. #include <asm/io.h>
  38. #include <asm/prom.h>
  39. #include <asm/of_device.h>
  40. #include <asm/starfire.h>
  41. #include <asm/smp.h>
  42. #include <asm/sections.h>
  43. #include <asm/cpudata.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/prom.h>
  46. DEFINE_SPINLOCK(mostek_lock);
  47. DEFINE_SPINLOCK(rtc_lock);
  48. void __iomem *mstk48t02_regs = NULL;
  49. #ifdef CONFIG_PCI
  50. unsigned long ds1287_regs = 0UL;
  51. #endif
  52. static void __iomem *mstk48t08_regs;
  53. static void __iomem *mstk48t59_regs;
  54. static int set_rtc_mmss(unsigned long);
  55. #define TICK_PRIV_BIT (1UL << 63)
  56. #ifdef CONFIG_SMP
  57. unsigned long profile_pc(struct pt_regs *regs)
  58. {
  59. unsigned long pc = instruction_pointer(regs);
  60. if (in_lock_functions(pc))
  61. return regs->u_regs[UREG_RETPC];
  62. return pc;
  63. }
  64. EXPORT_SYMBOL(profile_pc);
  65. #endif
  66. static void tick_disable_protection(void)
  67. {
  68. /* Set things up so user can access tick register for profiling
  69. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  70. * read back of %tick after writing it.
  71. */
  72. __asm__ __volatile__(
  73. " ba,pt %%xcc, 1f\n"
  74. " nop\n"
  75. " .align 64\n"
  76. "1: rd %%tick, %%g2\n"
  77. " add %%g2, 6, %%g2\n"
  78. " andn %%g2, %0, %%g2\n"
  79. " wrpr %%g2, 0, %%tick\n"
  80. " rdpr %%tick, %%g0"
  81. : /* no outputs */
  82. : "r" (TICK_PRIV_BIT)
  83. : "g2");
  84. }
  85. static void tick_init_tick(unsigned long offset)
  86. {
  87. tick_disable_protection();
  88. __asm__ __volatile__(
  89. " rd %%tick, %%g1\n"
  90. " andn %%g1, %1, %%g1\n"
  91. " ba,pt %%xcc, 1f\n"
  92. " add %%g1, %0, %%g1\n"
  93. " .align 64\n"
  94. "1: wr %%g1, 0x0, %%tick_cmpr\n"
  95. " rd %%tick_cmpr, %%g0"
  96. : /* no outputs */
  97. : "r" (offset), "r" (TICK_PRIV_BIT)
  98. : "g1");
  99. }
  100. static unsigned long tick_get_tick(void)
  101. {
  102. unsigned long ret;
  103. __asm__ __volatile__("rd %%tick, %0\n\t"
  104. "mov %0, %0"
  105. : "=r" (ret));
  106. return ret & ~TICK_PRIV_BIT;
  107. }
  108. static unsigned long tick_get_compare(void)
  109. {
  110. unsigned long ret;
  111. __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
  112. "mov %0, %0"
  113. : "=r" (ret));
  114. return ret;
  115. }
  116. static unsigned long tick_add_compare(unsigned long adj)
  117. {
  118. unsigned long new_compare;
  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__("rd %%tick_cmpr, %0\n\t"
  129. "ba,pt %%xcc, 1f\n\t"
  130. " add %0, %1, %0\n\t"
  131. ".align 64\n"
  132. "1:\n\t"
  133. "wr %0, 0, %%tick_cmpr\n\t"
  134. "rd %%tick_cmpr, %%g0"
  135. : "=&r" (new_compare)
  136. : "r" (adj));
  137. return new_compare;
  138. }
  139. static unsigned long tick_add_tick(unsigned long adj, unsigned long offset)
  140. {
  141. unsigned long new_tick, tmp;
  142. /* Also need to handle Blackbird bug here too. */
  143. __asm__ __volatile__("rd %%tick, %0\n\t"
  144. "add %0, %2, %0\n\t"
  145. "wrpr %0, 0, %%tick\n\t"
  146. "andn %0, %4, %1\n\t"
  147. "ba,pt %%xcc, 1f\n\t"
  148. " add %1, %3, %1\n\t"
  149. ".align 64\n"
  150. "1:\n\t"
  151. "wr %1, 0, %%tick_cmpr\n\t"
  152. "rd %%tick_cmpr, %%g0"
  153. : "=&r" (new_tick), "=&r" (tmp)
  154. : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
  155. return new_tick;
  156. }
  157. static struct sparc64_tick_ops tick_operations __read_mostly = {
  158. .init_tick = tick_init_tick,
  159. .get_tick = tick_get_tick,
  160. .get_compare = tick_get_compare,
  161. .add_tick = tick_add_tick,
  162. .add_compare = tick_add_compare,
  163. .softint_mask = 1UL << 0,
  164. };
  165. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  166. static void stick_init_tick(unsigned long offset)
  167. {
  168. /* Writes to the %tick and %stick register are not
  169. * allowed on sun4v. The Hypervisor controls that
  170. * bit, per-strand.
  171. */
  172. if (tlb_type != hypervisor) {
  173. tick_disable_protection();
  174. /* Let the user get at STICK too. */
  175. __asm__ __volatile__(
  176. " rd %%asr24, %%g2\n"
  177. " andn %%g2, %0, %%g2\n"
  178. " wr %%g2, 0, %%asr24"
  179. : /* no outputs */
  180. : "r" (TICK_PRIV_BIT)
  181. : "g1", "g2");
  182. }
  183. __asm__ __volatile__(
  184. " rd %%asr24, %%g1\n"
  185. " andn %%g1, %1, %%g1\n"
  186. " add %%g1, %0, %%g1\n"
  187. " wr %%g1, 0x0, %%asr25"
  188. : /* no outputs */
  189. : "r" (offset), "r" (TICK_PRIV_BIT)
  190. : "g1");
  191. }
  192. static unsigned long stick_get_tick(void)
  193. {
  194. unsigned long ret;
  195. __asm__ __volatile__("rd %%asr24, %0"
  196. : "=r" (ret));
  197. return ret & ~TICK_PRIV_BIT;
  198. }
  199. static unsigned long stick_get_compare(void)
  200. {
  201. unsigned long ret;
  202. __asm__ __volatile__("rd %%asr25, %0"
  203. : "=r" (ret));
  204. return ret;
  205. }
  206. static unsigned long stick_add_tick(unsigned long adj, unsigned long offset)
  207. {
  208. unsigned long new_tick, tmp;
  209. __asm__ __volatile__("rd %%asr24, %0\n\t"
  210. "add %0, %2, %0\n\t"
  211. "wr %0, 0, %%asr24\n\t"
  212. "andn %0, %4, %1\n\t"
  213. "add %1, %3, %1\n\t"
  214. "wr %1, 0, %%asr25"
  215. : "=&r" (new_tick), "=&r" (tmp)
  216. : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
  217. return new_tick;
  218. }
  219. static unsigned long stick_add_compare(unsigned long adj)
  220. {
  221. unsigned long new_compare;
  222. __asm__ __volatile__("rd %%asr25, %0\n\t"
  223. "add %0, %1, %0\n\t"
  224. "wr %0, 0, %%asr25"
  225. : "=&r" (new_compare)
  226. : "r" (adj));
  227. return new_compare;
  228. }
  229. static struct sparc64_tick_ops stick_operations __read_mostly = {
  230. .init_tick = stick_init_tick,
  231. .get_tick = stick_get_tick,
  232. .get_compare = stick_get_compare,
  233. .add_tick = stick_add_tick,
  234. .add_compare = stick_add_compare,
  235. .softint_mask = 1UL << 16,
  236. };
  237. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  238. * in I/O space. There are two 64-bit registers each, the
  239. * first holds the low 32-bits of the value and the second holds
  240. * the high 32-bits.
  241. *
  242. * Since STICK is constantly updating, we have to access it carefully.
  243. *
  244. * The sequence we use to read is:
  245. * 1) read high
  246. * 2) read low
  247. * 3) read high again, if it rolled re-read both low and high again.
  248. *
  249. * Writing STICK safely is also tricky:
  250. * 1) write low to zero
  251. * 2) write high
  252. * 3) write low
  253. */
  254. #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
  255. #define HBIRD_STICK_ADDR 0x1fe0000f070UL
  256. static unsigned long __hbird_read_stick(void)
  257. {
  258. unsigned long ret, tmp1, tmp2, tmp3;
  259. unsigned long addr = HBIRD_STICK_ADDR+8;
  260. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  261. "1:\n\t"
  262. "sub %1, 0x8, %1\n\t"
  263. "ldxa [%1] %5, %3\n\t"
  264. "add %1, 0x8, %1\n\t"
  265. "ldxa [%1] %5, %4\n\t"
  266. "cmp %4, %2\n\t"
  267. "bne,a,pn %%xcc, 1b\n\t"
  268. " mov %4, %2\n\t"
  269. "sllx %4, 32, %4\n\t"
  270. "or %3, %4, %0\n\t"
  271. : "=&r" (ret), "=&r" (addr),
  272. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  273. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  274. return ret;
  275. }
  276. static unsigned long __hbird_read_compare(void)
  277. {
  278. unsigned long low, high;
  279. unsigned long addr = HBIRD_STICKCMP_ADDR;
  280. __asm__ __volatile__("ldxa [%2] %3, %0\n\t"
  281. "add %2, 0x8, %2\n\t"
  282. "ldxa [%2] %3, %1"
  283. : "=&r" (low), "=&r" (high), "=&r" (addr)
  284. : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr));
  285. return (high << 32UL) | low;
  286. }
  287. static void __hbird_write_stick(unsigned long val)
  288. {
  289. unsigned long low = (val & 0xffffffffUL);
  290. unsigned long high = (val >> 32UL);
  291. unsigned long addr = HBIRD_STICK_ADDR;
  292. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  293. "add %0, 0x8, %0\n\t"
  294. "stxa %3, [%0] %4\n\t"
  295. "sub %0, 0x8, %0\n\t"
  296. "stxa %2, [%0] %4"
  297. : "=&r" (addr)
  298. : "0" (addr), "r" (low), "r" (high),
  299. "i" (ASI_PHYS_BYPASS_EC_E));
  300. }
  301. static void __hbird_write_compare(unsigned long val)
  302. {
  303. unsigned long low = (val & 0xffffffffUL);
  304. unsigned long high = (val >> 32UL);
  305. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  306. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  307. "sub %0, 0x8, %0\n\t"
  308. "stxa %2, [%0] %4"
  309. : "=&r" (addr)
  310. : "0" (addr), "r" (low), "r" (high),
  311. "i" (ASI_PHYS_BYPASS_EC_E));
  312. }
  313. static void hbtick_init_tick(unsigned long offset)
  314. {
  315. unsigned long val;
  316. tick_disable_protection();
  317. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  318. * XXX into actually sending STICK interrupts. I think because
  319. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  320. * XXX {TICK + STICK} interrupt mux. -DaveM
  321. */
  322. __hbird_write_stick(__hbird_read_stick());
  323. val = __hbird_read_stick() & ~TICK_PRIV_BIT;
  324. __hbird_write_compare(val + offset);
  325. }
  326. static unsigned long hbtick_get_tick(void)
  327. {
  328. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  329. }
  330. static unsigned long hbtick_get_compare(void)
  331. {
  332. return __hbird_read_compare();
  333. }
  334. static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset)
  335. {
  336. unsigned long val;
  337. val = __hbird_read_stick() + adj;
  338. __hbird_write_stick(val);
  339. val &= ~TICK_PRIV_BIT;
  340. __hbird_write_compare(val + offset);
  341. return val;
  342. }
  343. static unsigned long hbtick_add_compare(unsigned long adj)
  344. {
  345. unsigned long val = __hbird_read_compare() + adj;
  346. val &= ~TICK_PRIV_BIT;
  347. __hbird_write_compare(val);
  348. return val;
  349. }
  350. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  351. .init_tick = hbtick_init_tick,
  352. .get_tick = hbtick_get_tick,
  353. .get_compare = hbtick_get_compare,
  354. .add_tick = hbtick_add_tick,
  355. .add_compare = hbtick_add_compare,
  356. .softint_mask = 1UL << 0,
  357. };
  358. /* timer_interrupt() needs to keep up the real-time clock,
  359. * as well as call the "do_timer()" routine every clocktick
  360. *
  361. * NOTE: On SUN5 systems the ticker interrupt comes in using 2
  362. * interrupts, one at level14 and one with softint bit 0.
  363. */
  364. unsigned long timer_tick_offset __read_mostly;
  365. static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
  366. #define TICK_SIZE (tick_nsec / 1000)
  367. static inline void timer_check_rtc(void)
  368. {
  369. /* last time the cmos clock got updated */
  370. static long last_rtc_update;
  371. /* Determine when to update the Mostek clock. */
  372. if (ntp_synced() &&
  373. xtime.tv_sec > last_rtc_update + 660 &&
  374. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  375. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  376. if (set_rtc_mmss(xtime.tv_sec) == 0)
  377. last_rtc_update = xtime.tv_sec;
  378. else
  379. last_rtc_update = xtime.tv_sec - 600;
  380. /* do it again in 60 s */
  381. }
  382. }
  383. irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  384. {
  385. unsigned long ticks, compare, pstate;
  386. write_seqlock(&xtime_lock);
  387. do {
  388. #ifndef CONFIG_SMP
  389. profile_tick(CPU_PROFILING, regs);
  390. update_process_times(user_mode(regs));
  391. #endif
  392. do_timer(1);
  393. /* Guarantee that the following sequences execute
  394. * uninterrupted.
  395. */
  396. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  397. "wrpr %0, %1, %%pstate"
  398. : "=r" (pstate)
  399. : "i" (PSTATE_IE));
  400. compare = tick_ops->add_compare(timer_tick_offset);
  401. ticks = tick_ops->get_tick();
  402. /* Restore PSTATE_IE. */
  403. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  404. : /* no outputs */
  405. : "r" (pstate));
  406. } while (time_after_eq(ticks, compare));
  407. timer_check_rtc();
  408. write_sequnlock(&xtime_lock);
  409. return IRQ_HANDLED;
  410. }
  411. #ifdef CONFIG_SMP
  412. void timer_tick_interrupt(struct pt_regs *regs)
  413. {
  414. write_seqlock(&xtime_lock);
  415. do_timer(1);
  416. timer_check_rtc();
  417. write_sequnlock(&xtime_lock);
  418. }
  419. #endif
  420. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  421. static void __init kick_start_clock(void)
  422. {
  423. void __iomem *regs = mstk48t02_regs;
  424. u8 sec, tmp;
  425. int i, count;
  426. prom_printf("CLOCK: Clock was stopped. Kick start ");
  427. spin_lock_irq(&mostek_lock);
  428. /* Turn on the kick start bit to start the oscillator. */
  429. tmp = mostek_read(regs + MOSTEK_CREG);
  430. tmp |= MSTK_CREG_WRITE;
  431. mostek_write(regs + MOSTEK_CREG, tmp);
  432. tmp = mostek_read(regs + MOSTEK_SEC);
  433. tmp &= ~MSTK_STOP;
  434. mostek_write(regs + MOSTEK_SEC, tmp);
  435. tmp = mostek_read(regs + MOSTEK_HOUR);
  436. tmp |= MSTK_KICK_START;
  437. mostek_write(regs + MOSTEK_HOUR, tmp);
  438. tmp = mostek_read(regs + MOSTEK_CREG);
  439. tmp &= ~MSTK_CREG_WRITE;
  440. mostek_write(regs + MOSTEK_CREG, tmp);
  441. spin_unlock_irq(&mostek_lock);
  442. /* Delay to allow the clock oscillator to start. */
  443. sec = MSTK_REG_SEC(regs);
  444. for (i = 0; i < 3; i++) {
  445. while (sec == MSTK_REG_SEC(regs))
  446. for (count = 0; count < 100000; count++)
  447. /* nothing */ ;
  448. prom_printf(".");
  449. sec = MSTK_REG_SEC(regs);
  450. }
  451. prom_printf("\n");
  452. spin_lock_irq(&mostek_lock);
  453. /* Turn off kick start and set a "valid" time and date. */
  454. tmp = mostek_read(regs + MOSTEK_CREG);
  455. tmp |= MSTK_CREG_WRITE;
  456. mostek_write(regs + MOSTEK_CREG, tmp);
  457. tmp = mostek_read(regs + MOSTEK_HOUR);
  458. tmp &= ~MSTK_KICK_START;
  459. mostek_write(regs + MOSTEK_HOUR, tmp);
  460. MSTK_SET_REG_SEC(regs,0);
  461. MSTK_SET_REG_MIN(regs,0);
  462. MSTK_SET_REG_HOUR(regs,0);
  463. MSTK_SET_REG_DOW(regs,5);
  464. MSTK_SET_REG_DOM(regs,1);
  465. MSTK_SET_REG_MONTH(regs,8);
  466. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  467. tmp = mostek_read(regs + MOSTEK_CREG);
  468. tmp &= ~MSTK_CREG_WRITE;
  469. mostek_write(regs + MOSTEK_CREG, tmp);
  470. spin_unlock_irq(&mostek_lock);
  471. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  472. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  473. prom_printf("CLOCK: Kick start still on!\n");
  474. spin_lock_irq(&mostek_lock);
  475. tmp = mostek_read(regs + MOSTEK_CREG);
  476. tmp |= MSTK_CREG_WRITE;
  477. mostek_write(regs + MOSTEK_CREG, tmp);
  478. tmp = mostek_read(regs + MOSTEK_HOUR);
  479. tmp &= ~MSTK_KICK_START;
  480. mostek_write(regs + MOSTEK_HOUR, tmp);
  481. tmp = mostek_read(regs + MOSTEK_CREG);
  482. tmp &= ~MSTK_CREG_WRITE;
  483. mostek_write(regs + MOSTEK_CREG, tmp);
  484. spin_unlock_irq(&mostek_lock);
  485. }
  486. prom_printf("CLOCK: Kick start procedure successful.\n");
  487. }
  488. /* Return nonzero if the clock chip battery is low. */
  489. static int __init has_low_battery(void)
  490. {
  491. void __iomem *regs = mstk48t02_regs;
  492. u8 data1, data2;
  493. spin_lock_irq(&mostek_lock);
  494. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  495. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  496. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  497. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  498. spin_unlock_irq(&mostek_lock);
  499. return (data1 == data2); /* Was the write blocked? */
  500. }
  501. /* Probe for the real time clock chip. */
  502. static void __init set_system_time(void)
  503. {
  504. unsigned int year, mon, day, hour, min, sec;
  505. void __iomem *mregs = mstk48t02_regs;
  506. #ifdef CONFIG_PCI
  507. unsigned long dregs = ds1287_regs;
  508. #else
  509. unsigned long dregs = 0UL;
  510. #endif
  511. u8 tmp;
  512. if (!mregs && !dregs) {
  513. prom_printf("Something wrong, clock regs not mapped yet.\n");
  514. prom_halt();
  515. }
  516. if (mregs) {
  517. spin_lock_irq(&mostek_lock);
  518. /* Traditional Mostek chip. */
  519. tmp = mostek_read(mregs + MOSTEK_CREG);
  520. tmp |= MSTK_CREG_READ;
  521. mostek_write(mregs + MOSTEK_CREG, tmp);
  522. sec = MSTK_REG_SEC(mregs);
  523. min = MSTK_REG_MIN(mregs);
  524. hour = MSTK_REG_HOUR(mregs);
  525. day = MSTK_REG_DOM(mregs);
  526. mon = MSTK_REG_MONTH(mregs);
  527. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  528. } else {
  529. /* Dallas 12887 RTC chip. */
  530. do {
  531. sec = CMOS_READ(RTC_SECONDS);
  532. min = CMOS_READ(RTC_MINUTES);
  533. hour = CMOS_READ(RTC_HOURS);
  534. day = CMOS_READ(RTC_DAY_OF_MONTH);
  535. mon = CMOS_READ(RTC_MONTH);
  536. year = CMOS_READ(RTC_YEAR);
  537. } while (sec != CMOS_READ(RTC_SECONDS));
  538. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  539. BCD_TO_BIN(sec);
  540. BCD_TO_BIN(min);
  541. BCD_TO_BIN(hour);
  542. BCD_TO_BIN(day);
  543. BCD_TO_BIN(mon);
  544. BCD_TO_BIN(year);
  545. }
  546. if ((year += 1900) < 1970)
  547. year += 100;
  548. }
  549. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  550. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  551. set_normalized_timespec(&wall_to_monotonic,
  552. -xtime.tv_sec, -xtime.tv_nsec);
  553. if (mregs) {
  554. tmp = mostek_read(mregs + MOSTEK_CREG);
  555. tmp &= ~MSTK_CREG_READ;
  556. mostek_write(mregs + MOSTEK_CREG, tmp);
  557. spin_unlock_irq(&mostek_lock);
  558. }
  559. }
  560. /* davem suggests we keep this within the 4M locked kernel image */
  561. static u32 starfire_get_time(void)
  562. {
  563. static char obp_gettod[32];
  564. static u32 unix_tod;
  565. sprintf(obp_gettod, "h# %08x unix-gettod",
  566. (unsigned int) (long) &unix_tod);
  567. prom_feval(obp_gettod);
  568. return unix_tod;
  569. }
  570. static int starfire_set_time(u32 val)
  571. {
  572. /* Do nothing, time is set using the service processor
  573. * console on this platform.
  574. */
  575. return 0;
  576. }
  577. static u32 hypervisor_get_time(void)
  578. {
  579. register unsigned long func asm("%o5");
  580. register unsigned long arg0 asm("%o0");
  581. register unsigned long arg1 asm("%o1");
  582. int retries = 10000;
  583. retry:
  584. func = HV_FAST_TOD_GET;
  585. arg0 = 0;
  586. arg1 = 0;
  587. __asm__ __volatile__("ta %6"
  588. : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
  589. : "0" (func), "1" (arg0), "2" (arg1),
  590. "i" (HV_FAST_TRAP));
  591. if (arg0 == HV_EOK)
  592. return arg1;
  593. if (arg0 == HV_EWOULDBLOCK) {
  594. if (--retries > 0) {
  595. udelay(100);
  596. goto retry;
  597. }
  598. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  599. return 0;
  600. }
  601. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  602. return 0;
  603. }
  604. static int hypervisor_set_time(u32 secs)
  605. {
  606. register unsigned long func asm("%o5");
  607. register unsigned long arg0 asm("%o0");
  608. int retries = 10000;
  609. retry:
  610. func = HV_FAST_TOD_SET;
  611. arg0 = secs;
  612. __asm__ __volatile__("ta %4"
  613. : "=&r" (func), "=&r" (arg0)
  614. : "0" (func), "1" (arg0),
  615. "i" (HV_FAST_TRAP));
  616. if (arg0 == HV_EOK)
  617. return 0;
  618. if (arg0 == HV_EWOULDBLOCK) {
  619. if (--retries > 0) {
  620. udelay(100);
  621. goto retry;
  622. }
  623. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  624. return -EAGAIN;
  625. }
  626. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  627. return -EOPNOTSUPP;
  628. }
  629. static int __init clock_model_matches(char *model)
  630. {
  631. if (strcmp(model, "mk48t02") &&
  632. strcmp(model, "mk48t08") &&
  633. strcmp(model, "mk48t59") &&
  634. strcmp(model, "m5819") &&
  635. strcmp(model, "m5819p") &&
  636. strcmp(model, "m5823") &&
  637. strcmp(model, "ds1287"))
  638. return 0;
  639. return 1;
  640. }
  641. static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
  642. {
  643. struct device_node *dp = op->node;
  644. char *model = of_get_property(dp, "model", NULL);
  645. unsigned long size, flags;
  646. void __iomem *regs;
  647. if (!model || !clock_model_matches(model))
  648. return -ENODEV;
  649. /* On an Enterprise system there can be multiple mostek clocks.
  650. * We should only match the one that is on the central FHC bus.
  651. */
  652. if (!strcmp(dp->parent->name, "fhc") &&
  653. strcmp(dp->parent->parent->name, "central") != 0)
  654. return -ENODEV;
  655. size = (op->resource[0].end - op->resource[0].start) + 1;
  656. regs = of_ioremap(&op->resource[0], 0, size, "clock");
  657. if (!regs)
  658. return -ENOMEM;
  659. #ifdef CONFIG_PCI
  660. if (!strcmp(model, "ds1287") ||
  661. !strcmp(model, "m5819") ||
  662. !strcmp(model, "m5819p") ||
  663. !strcmp(model, "m5823")) {
  664. ds1287_regs = (unsigned long) regs;
  665. } else
  666. #endif
  667. if (model[5] == '0' && model[6] == '2') {
  668. mstk48t02_regs = regs;
  669. } else if(model[5] == '0' && model[6] == '8') {
  670. mstk48t08_regs = regs;
  671. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  672. } else {
  673. mstk48t59_regs = regs;
  674. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  675. }
  676. printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
  677. local_irq_save(flags);
  678. if (mstk48t02_regs != NULL) {
  679. /* Report a low battery voltage condition. */
  680. if (has_low_battery())
  681. prom_printf("NVRAM: Low battery voltage!\n");
  682. /* Kick start the clock if it is completely stopped. */
  683. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  684. kick_start_clock();
  685. }
  686. set_system_time();
  687. local_irq_restore(flags);
  688. return 0;
  689. }
  690. static struct of_device_id clock_match[] = {
  691. {
  692. .name = "eeprom",
  693. },
  694. {
  695. .name = "rtc",
  696. },
  697. {},
  698. };
  699. static struct of_platform_driver clock_driver = {
  700. .name = "clock",
  701. .match_table = clock_match,
  702. .probe = clock_probe,
  703. };
  704. static int __init clock_init(void)
  705. {
  706. if (this_is_starfire) {
  707. xtime.tv_sec = starfire_get_time();
  708. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  709. set_normalized_timespec(&wall_to_monotonic,
  710. -xtime.tv_sec, -xtime.tv_nsec);
  711. return 0;
  712. }
  713. if (tlb_type == hypervisor) {
  714. xtime.tv_sec = hypervisor_get_time();
  715. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  716. set_normalized_timespec(&wall_to_monotonic,
  717. -xtime.tv_sec, -xtime.tv_nsec);
  718. return 0;
  719. }
  720. return of_register_driver(&clock_driver, &of_bus_type);
  721. }
  722. /* Must be after subsys_initcall() so that busses are probed. Must
  723. * be before device_initcall() because things like the RTC driver
  724. * need to see the clock registers.
  725. */
  726. fs_initcall(clock_init);
  727. /* This is gets the master TICK_INT timer going. */
  728. static unsigned long sparc64_init_timers(void)
  729. {
  730. struct device_node *dp;
  731. struct property *prop;
  732. unsigned long clock;
  733. #ifdef CONFIG_SMP
  734. extern void smp_tick_init(void);
  735. #endif
  736. dp = of_find_node_by_path("/");
  737. if (tlb_type == spitfire) {
  738. unsigned long ver, manuf, impl;
  739. __asm__ __volatile__ ("rdpr %%ver, %0"
  740. : "=&r" (ver));
  741. manuf = ((ver >> 48) & 0xffff);
  742. impl = ((ver >> 32) & 0xffff);
  743. if (manuf == 0x17 && impl == 0x13) {
  744. /* Hummingbird, aka Ultra-IIe */
  745. tick_ops = &hbtick_operations;
  746. prop = of_find_property(dp, "stick-frequency", NULL);
  747. } else {
  748. tick_ops = &tick_operations;
  749. cpu_find_by_instance(0, &dp, NULL);
  750. prop = of_find_property(dp, "clock-frequency", NULL);
  751. }
  752. } else {
  753. tick_ops = &stick_operations;
  754. prop = of_find_property(dp, "stick-frequency", NULL);
  755. }
  756. clock = *(unsigned int *) prop->value;
  757. timer_tick_offset = clock / HZ;
  758. #ifdef CONFIG_SMP
  759. smp_tick_init();
  760. #endif
  761. return clock;
  762. }
  763. static void sparc64_start_timers(void)
  764. {
  765. unsigned long pstate;
  766. /* Guarantee that the following sequences execute
  767. * uninterrupted.
  768. */
  769. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  770. "wrpr %0, %1, %%pstate"
  771. : "=r" (pstate)
  772. : "i" (PSTATE_IE));
  773. tick_ops->init_tick(timer_tick_offset);
  774. /* Restore PSTATE_IE. */
  775. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  776. : /* no outputs */
  777. : "r" (pstate));
  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 10UL
  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);