time.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /* time.c: UltraSparc timer and TOD clock support.
  2. *
  3. * Copyright (C) 1997, 2008 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  5. *
  6. * Based largely on code which is:
  7. *
  8. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/kernel.h>
  15. #include <linux/param.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/time.h>
  20. #include <linux/timex.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/mc146818rtc.h>
  24. #include <linux/delay.h>
  25. #include <linux/profile.h>
  26. #include <linux/bcd.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/percpu.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/rtc.h>
  32. #include <linux/rtc/m48t59.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/clockchips.h>
  35. #include <linux/clocksource.h>
  36. #include <linux/of_device.h>
  37. #include <linux/platform_device.h>
  38. #include <asm/oplib.h>
  39. #include <asm/timer.h>
  40. #include <asm/irq.h>
  41. #include <asm/io.h>
  42. #include <asm/prom.h>
  43. #include <asm/starfire.h>
  44. #include <asm/smp.h>
  45. #include <asm/sections.h>
  46. #include <asm/cpudata.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/irq_regs.h>
  49. #include "entry.h"
  50. DEFINE_SPINLOCK(rtc_lock);
  51. static void __iomem *bq4802_regs;
  52. static int set_rtc_mmss(unsigned long);
  53. #define TICK_PRIV_BIT (1UL << 63)
  54. #define TICKCMP_IRQ_BIT (1UL << 63)
  55. #ifdef CONFIG_SMP
  56. unsigned long profile_pc(struct pt_regs *regs)
  57. {
  58. unsigned long pc = instruction_pointer(regs);
  59. if (in_lock_functions(pc))
  60. return regs->u_regs[UREG_RETPC];
  61. return pc;
  62. }
  63. EXPORT_SYMBOL(profile_pc);
  64. #endif
  65. static void tick_disable_protection(void)
  66. {
  67. /* Set things up so user can access tick register for profiling
  68. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  69. * read back of %tick after writing it.
  70. */
  71. __asm__ __volatile__(
  72. " ba,pt %%xcc, 1f\n"
  73. " nop\n"
  74. " .align 64\n"
  75. "1: rd %%tick, %%g2\n"
  76. " add %%g2, 6, %%g2\n"
  77. " andn %%g2, %0, %%g2\n"
  78. " wrpr %%g2, 0, %%tick\n"
  79. " rdpr %%tick, %%g0"
  80. : /* no outputs */
  81. : "r" (TICK_PRIV_BIT)
  82. : "g2");
  83. }
  84. static void tick_disable_irq(void)
  85. {
  86. __asm__ __volatile__(
  87. " ba,pt %%xcc, 1f\n"
  88. " nop\n"
  89. " .align 64\n"
  90. "1: wr %0, 0x0, %%tick_cmpr\n"
  91. " rd %%tick_cmpr, %%g0"
  92. : /* no outputs */
  93. : "r" (TICKCMP_IRQ_BIT));
  94. }
  95. static void tick_init_tick(void)
  96. {
  97. tick_disable_protection();
  98. tick_disable_irq();
  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 int tick_add_compare(unsigned long adj)
  109. {
  110. unsigned long orig_tick, new_tick, new_compare;
  111. __asm__ __volatile__("rd %%tick, %0"
  112. : "=r" (orig_tick));
  113. orig_tick &= ~TICKCMP_IRQ_BIT;
  114. /* Workaround for Spitfire Errata (#54 I think??), I discovered
  115. * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
  116. * number 103640.
  117. *
  118. * On Blackbird writes to %tick_cmpr can fail, the
  119. * workaround seems to be to execute the wr instruction
  120. * at the start of an I-cache line, and perform a dummy
  121. * read back from %tick_cmpr right after writing to it. -DaveM
  122. */
  123. __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
  124. " add %1, %2, %0\n\t"
  125. ".align 64\n"
  126. "1:\n\t"
  127. "wr %0, 0, %%tick_cmpr\n\t"
  128. "rd %%tick_cmpr, %%g0\n\t"
  129. : "=r" (new_compare)
  130. : "r" (orig_tick), "r" (adj));
  131. __asm__ __volatile__("rd %%tick, %0"
  132. : "=r" (new_tick));
  133. new_tick &= ~TICKCMP_IRQ_BIT;
  134. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  135. }
  136. static unsigned long tick_add_tick(unsigned long adj)
  137. {
  138. unsigned long new_tick;
  139. /* Also need to handle Blackbird bug here too. */
  140. __asm__ __volatile__("rd %%tick, %0\n\t"
  141. "add %0, %1, %0\n\t"
  142. "wrpr %0, 0, %%tick\n\t"
  143. : "=&r" (new_tick)
  144. : "r" (adj));
  145. return new_tick;
  146. }
  147. static struct sparc64_tick_ops tick_operations __read_mostly = {
  148. .name = "tick",
  149. .init_tick = tick_init_tick,
  150. .disable_irq = tick_disable_irq,
  151. .get_tick = tick_get_tick,
  152. .add_tick = tick_add_tick,
  153. .add_compare = tick_add_compare,
  154. .softint_mask = 1UL << 0,
  155. };
  156. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  157. static void stick_disable_irq(void)
  158. {
  159. __asm__ __volatile__(
  160. "wr %0, 0x0, %%asr25"
  161. : /* no outputs */
  162. : "r" (TICKCMP_IRQ_BIT));
  163. }
  164. static void stick_init_tick(void)
  165. {
  166. /* Writes to the %tick and %stick register are not
  167. * allowed on sun4v. The Hypervisor controls that
  168. * bit, per-strand.
  169. */
  170. if (tlb_type != hypervisor) {
  171. tick_disable_protection();
  172. tick_disable_irq();
  173. /* Let the user get at STICK too. */
  174. __asm__ __volatile__(
  175. " rd %%asr24, %%g2\n"
  176. " andn %%g2, %0, %%g2\n"
  177. " wr %%g2, 0, %%asr24"
  178. : /* no outputs */
  179. : "r" (TICK_PRIV_BIT)
  180. : "g1", "g2");
  181. }
  182. stick_disable_irq();
  183. }
  184. static unsigned long stick_get_tick(void)
  185. {
  186. unsigned long ret;
  187. __asm__ __volatile__("rd %%asr24, %0"
  188. : "=r" (ret));
  189. return ret & ~TICK_PRIV_BIT;
  190. }
  191. static unsigned long stick_add_tick(unsigned long adj)
  192. {
  193. unsigned long new_tick;
  194. __asm__ __volatile__("rd %%asr24, %0\n\t"
  195. "add %0, %1, %0\n\t"
  196. "wr %0, 0, %%asr24\n\t"
  197. : "=&r" (new_tick)
  198. : "r" (adj));
  199. return new_tick;
  200. }
  201. static int stick_add_compare(unsigned long adj)
  202. {
  203. unsigned long orig_tick, new_tick;
  204. __asm__ __volatile__("rd %%asr24, %0"
  205. : "=r" (orig_tick));
  206. orig_tick &= ~TICKCMP_IRQ_BIT;
  207. __asm__ __volatile__("wr %0, 0, %%asr25"
  208. : /* no outputs */
  209. : "r" (orig_tick + adj));
  210. __asm__ __volatile__("rd %%asr24, %0"
  211. : "=r" (new_tick));
  212. new_tick &= ~TICKCMP_IRQ_BIT;
  213. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  214. }
  215. static struct sparc64_tick_ops stick_operations __read_mostly = {
  216. .name = "stick",
  217. .init_tick = stick_init_tick,
  218. .disable_irq = stick_disable_irq,
  219. .get_tick = stick_get_tick,
  220. .add_tick = stick_add_tick,
  221. .add_compare = stick_add_compare,
  222. .softint_mask = 1UL << 16,
  223. };
  224. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  225. * in I/O space. There are two 64-bit registers each, the
  226. * first holds the low 32-bits of the value and the second holds
  227. * the high 32-bits.
  228. *
  229. * Since STICK is constantly updating, we have to access it carefully.
  230. *
  231. * The sequence we use to read is:
  232. * 1) read high
  233. * 2) read low
  234. * 3) read high again, if it rolled re-read both low and high again.
  235. *
  236. * Writing STICK safely is also tricky:
  237. * 1) write low to zero
  238. * 2) write high
  239. * 3) write low
  240. */
  241. #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
  242. #define HBIRD_STICK_ADDR 0x1fe0000f070UL
  243. static unsigned long __hbird_read_stick(void)
  244. {
  245. unsigned long ret, tmp1, tmp2, tmp3;
  246. unsigned long addr = HBIRD_STICK_ADDR+8;
  247. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  248. "1:\n\t"
  249. "sub %1, 0x8, %1\n\t"
  250. "ldxa [%1] %5, %3\n\t"
  251. "add %1, 0x8, %1\n\t"
  252. "ldxa [%1] %5, %4\n\t"
  253. "cmp %4, %2\n\t"
  254. "bne,a,pn %%xcc, 1b\n\t"
  255. " mov %4, %2\n\t"
  256. "sllx %4, 32, %4\n\t"
  257. "or %3, %4, %0\n\t"
  258. : "=&r" (ret), "=&r" (addr),
  259. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  260. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  261. return ret;
  262. }
  263. static void __hbird_write_stick(unsigned long val)
  264. {
  265. unsigned long low = (val & 0xffffffffUL);
  266. unsigned long high = (val >> 32UL);
  267. unsigned long addr = HBIRD_STICK_ADDR;
  268. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  269. "add %0, 0x8, %0\n\t"
  270. "stxa %3, [%0] %4\n\t"
  271. "sub %0, 0x8, %0\n\t"
  272. "stxa %2, [%0] %4"
  273. : "=&r" (addr)
  274. : "0" (addr), "r" (low), "r" (high),
  275. "i" (ASI_PHYS_BYPASS_EC_E));
  276. }
  277. static void __hbird_write_compare(unsigned long val)
  278. {
  279. unsigned long low = (val & 0xffffffffUL);
  280. unsigned long high = (val >> 32UL);
  281. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  282. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  283. "sub %0, 0x8, %0\n\t"
  284. "stxa %2, [%0] %4"
  285. : "=&r" (addr)
  286. : "0" (addr), "r" (low), "r" (high),
  287. "i" (ASI_PHYS_BYPASS_EC_E));
  288. }
  289. static void hbtick_disable_irq(void)
  290. {
  291. __hbird_write_compare(TICKCMP_IRQ_BIT);
  292. }
  293. static void hbtick_init_tick(void)
  294. {
  295. tick_disable_protection();
  296. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  297. * XXX into actually sending STICK interrupts. I think because
  298. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  299. * XXX {TICK + STICK} interrupt mux. -DaveM
  300. */
  301. __hbird_write_stick(__hbird_read_stick());
  302. hbtick_disable_irq();
  303. }
  304. static unsigned long hbtick_get_tick(void)
  305. {
  306. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  307. }
  308. static unsigned long hbtick_add_tick(unsigned long adj)
  309. {
  310. unsigned long val;
  311. val = __hbird_read_stick() + adj;
  312. __hbird_write_stick(val);
  313. return val;
  314. }
  315. static int hbtick_add_compare(unsigned long adj)
  316. {
  317. unsigned long val = __hbird_read_stick();
  318. unsigned long val2;
  319. val &= ~TICKCMP_IRQ_BIT;
  320. val += adj;
  321. __hbird_write_compare(val);
  322. val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
  323. return ((long)(val2 - val)) > 0L;
  324. }
  325. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  326. .name = "hbtick",
  327. .init_tick = hbtick_init_tick,
  328. .disable_irq = hbtick_disable_irq,
  329. .get_tick = hbtick_get_tick,
  330. .add_tick = hbtick_add_tick,
  331. .add_compare = hbtick_add_compare,
  332. .softint_mask = 1UL << 0,
  333. };
  334. static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
  335. int update_persistent_clock(struct timespec now)
  336. {
  337. struct rtc_device *rtc = rtc_class_open("rtc0");
  338. if (rtc)
  339. return rtc_set_mmss(rtc, now.tv_sec);
  340. return set_rtc_mmss(now.tv_sec);
  341. }
  342. /* Probe for the real time clock chip. */
  343. static void __init set_system_time(void)
  344. {
  345. unsigned int year, mon, day, hour, min, sec;
  346. void __iomem *bregs = bq4802_regs;
  347. unsigned char val = readb(bregs + 0x0e);
  348. unsigned int century;
  349. if (!bregs) {
  350. prom_printf("Something wrong, clock regs not mapped yet.\n");
  351. prom_halt();
  352. }
  353. /* BQ4802 RTC chip. */
  354. writeb(val | 0x08, bregs + 0x0e);
  355. sec = readb(bregs + 0x00);
  356. min = readb(bregs + 0x02);
  357. hour = readb(bregs + 0x04);
  358. day = readb(bregs + 0x06);
  359. mon = readb(bregs + 0x09);
  360. year = readb(bregs + 0x0a);
  361. century = readb(bregs + 0x0f);
  362. writeb(val, bregs + 0x0e);
  363. BCD_TO_BIN(sec);
  364. BCD_TO_BIN(min);
  365. BCD_TO_BIN(hour);
  366. BCD_TO_BIN(day);
  367. BCD_TO_BIN(mon);
  368. BCD_TO_BIN(year);
  369. BCD_TO_BIN(century);
  370. year += (century * 100);
  371. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  372. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  373. set_normalized_timespec(&wall_to_monotonic,
  374. -xtime.tv_sec, -xtime.tv_nsec);
  375. }
  376. /* davem suggests we keep this within the 4M locked kernel image */
  377. static u32 starfire_get_time(void)
  378. {
  379. static char obp_gettod[32];
  380. static u32 unix_tod;
  381. sprintf(obp_gettod, "h# %08x unix-gettod",
  382. (unsigned int) (long) &unix_tod);
  383. prom_feval(obp_gettod);
  384. return unix_tod;
  385. }
  386. static int starfire_set_time(u32 val)
  387. {
  388. /* Do nothing, time is set using the service processor
  389. * console on this platform.
  390. */
  391. return 0;
  392. }
  393. static u32 hypervisor_get_time(void)
  394. {
  395. unsigned long ret, time;
  396. int retries = 10000;
  397. retry:
  398. ret = sun4v_tod_get(&time);
  399. if (ret == HV_EOK)
  400. return time;
  401. if (ret == HV_EWOULDBLOCK) {
  402. if (--retries > 0) {
  403. udelay(100);
  404. goto retry;
  405. }
  406. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  407. return 0;
  408. }
  409. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  410. return 0;
  411. }
  412. static int hypervisor_set_time(u32 secs)
  413. {
  414. unsigned long ret;
  415. int retries = 10000;
  416. retry:
  417. ret = sun4v_tod_set(secs);
  418. if (ret == HV_EOK)
  419. return 0;
  420. if (ret == HV_EWOULDBLOCK) {
  421. if (--retries > 0) {
  422. udelay(100);
  423. goto retry;
  424. }
  425. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  426. return -EAGAIN;
  427. }
  428. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  429. return -EOPNOTSUPP;
  430. }
  431. unsigned long cmos_regs;
  432. EXPORT_SYMBOL(cmos_regs);
  433. struct resource rtc_cmos_resource;
  434. static struct platform_device rtc_cmos_device = {
  435. .name = "rtc_cmos",
  436. .id = -1,
  437. .resource = &rtc_cmos_resource,
  438. .num_resources = 1,
  439. };
  440. static int __devinit rtc_probe(struct of_device *op, const struct of_device_id *match)
  441. {
  442. struct resource *r;
  443. printk(KERN_INFO "%s: RTC regs at 0x%lx\n",
  444. op->node->full_name, op->resource[0].start);
  445. /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
  446. * up a fake resource so that the probe works for all cases.
  447. * When the RTC is behind an ISA bus it will have IORESOURCE_IO
  448. * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
  449. */
  450. r = &rtc_cmos_resource;
  451. r->flags = IORESOURCE_IO;
  452. r->name = op->resource[0].name;
  453. r->start = op->resource[0].start;
  454. r->end = op->resource[0].end;
  455. cmos_regs = op->resource[0].start;
  456. return platform_device_register(&rtc_cmos_device);
  457. }
  458. static struct of_device_id rtc_match[] = {
  459. {
  460. .name = "rtc",
  461. .compatible = "m5819",
  462. },
  463. {
  464. .name = "rtc",
  465. .compatible = "isa-m5819p",
  466. },
  467. {
  468. .name = "rtc",
  469. .compatible = "isa-m5823p",
  470. },
  471. {
  472. .name = "rtc",
  473. .compatible = "ds1287",
  474. },
  475. {},
  476. };
  477. static struct of_platform_driver rtc_driver = {
  478. .match_table = rtc_match,
  479. .probe = rtc_probe,
  480. .driver = {
  481. .name = "rtc",
  482. },
  483. };
  484. static int __devinit bq4802_probe(struct of_device *op, const struct of_device_id *match)
  485. {
  486. struct device_node *dp = op->node;
  487. unsigned long flags;
  488. bq4802_regs = of_ioremap(&op->resource[0], 0, resource_size(&op->resource[0]), "bq4802");
  489. if (!bq4802_regs)
  490. return -ENOMEM;
  491. printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, bq4802_regs);
  492. local_irq_save(flags);
  493. set_system_time();
  494. local_irq_restore(flags);
  495. return 0;
  496. }
  497. static struct of_device_id bq4802_match[] = {
  498. {
  499. .name = "rtc",
  500. .compatible = "bq4802",
  501. },
  502. };
  503. static struct of_platform_driver bq4802_driver = {
  504. .match_table = bq4802_match,
  505. .probe = bq4802_probe,
  506. .driver = {
  507. .name = "bq4802",
  508. },
  509. };
  510. static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
  511. {
  512. struct platform_device *pdev = to_platform_device(dev);
  513. void __iomem *regs;
  514. unsigned char val;
  515. regs = (void __iomem *) pdev->resource[0].start;
  516. val = readb(regs + ofs);
  517. /* the year 0 is 1968 */
  518. if (ofs == M48T59_YEAR) {
  519. val += 0x68;
  520. if ((val & 0xf) > 9)
  521. val += 6;
  522. }
  523. return val;
  524. }
  525. static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
  526. {
  527. struct platform_device *pdev = to_platform_device(dev);
  528. void __iomem *regs;
  529. regs = (void __iomem *) pdev->resource[0].start;
  530. if (ofs == M48T59_YEAR) {
  531. if (val < 0x68)
  532. val += 0x32;
  533. else
  534. val -= 0x68;
  535. if ((val & 0xf) > 9)
  536. val += 6;
  537. if ((val & 0xf0) > 0x9A)
  538. val += 0x60;
  539. }
  540. writeb(val, regs + ofs);
  541. }
  542. static struct m48t59_plat_data m48t59_data = {
  543. .read_byte = mostek_read_byte,
  544. .write_byte = mostek_write_byte,
  545. };
  546. static struct platform_device m48t59_rtc = {
  547. .name = "rtc-m48t59",
  548. .id = 0,
  549. .num_resources = 1,
  550. .dev = {
  551. .platform_data = &m48t59_data,
  552. },
  553. };
  554. static int __devinit mostek_probe(struct of_device *op, const struct of_device_id *match)
  555. {
  556. struct device_node *dp = op->node;
  557. /* On an Enterprise system there can be multiple mostek clocks.
  558. * We should only match the one that is on the central FHC bus.
  559. */
  560. if (!strcmp(dp->parent->name, "fhc") &&
  561. strcmp(dp->parent->parent->name, "central") != 0)
  562. return -ENODEV;
  563. printk(KERN_INFO "%s: Mostek regs at 0x%lx\n",
  564. dp->full_name, op->resource[0].start);
  565. m48t59_rtc.resource = &op->resource[0];
  566. return platform_device_register(&m48t59_rtc);
  567. }
  568. static struct of_device_id mostek_match[] = {
  569. {
  570. .name = "eeprom",
  571. },
  572. {},
  573. };
  574. static struct of_platform_driver mostek_driver = {
  575. .match_table = mostek_match,
  576. .probe = mostek_probe,
  577. .driver = {
  578. .name = "mostek",
  579. },
  580. };
  581. static int __init clock_init(void)
  582. {
  583. if (this_is_starfire) {
  584. xtime.tv_sec = starfire_get_time();
  585. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  586. set_normalized_timespec(&wall_to_monotonic,
  587. -xtime.tv_sec, -xtime.tv_nsec);
  588. return 0;
  589. }
  590. if (tlb_type == hypervisor) {
  591. xtime.tv_sec = hypervisor_get_time();
  592. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  593. set_normalized_timespec(&wall_to_monotonic,
  594. -xtime.tv_sec, -xtime.tv_nsec);
  595. return 0;
  596. }
  597. (void) of_register_driver(&rtc_driver, &of_platform_bus_type);
  598. (void) of_register_driver(&mostek_driver, &of_platform_bus_type);
  599. (void) of_register_driver(&bq4802_driver, &of_platform_bus_type);
  600. return 0;
  601. }
  602. /* Must be after subsys_initcall() so that busses are probed. Must
  603. * be before device_initcall() because things like the RTC driver
  604. * need to see the clock registers.
  605. */
  606. fs_initcall(clock_init);
  607. /* This is gets the master TICK_INT timer going. */
  608. static unsigned long sparc64_init_timers(void)
  609. {
  610. struct device_node *dp;
  611. unsigned long clock;
  612. dp = of_find_node_by_path("/");
  613. if (tlb_type == spitfire) {
  614. unsigned long ver, manuf, impl;
  615. __asm__ __volatile__ ("rdpr %%ver, %0"
  616. : "=&r" (ver));
  617. manuf = ((ver >> 48) & 0xffff);
  618. impl = ((ver >> 32) & 0xffff);
  619. if (manuf == 0x17 && impl == 0x13) {
  620. /* Hummingbird, aka Ultra-IIe */
  621. tick_ops = &hbtick_operations;
  622. clock = of_getintprop_default(dp, "stick-frequency", 0);
  623. } else {
  624. tick_ops = &tick_operations;
  625. clock = local_cpu_data().clock_tick;
  626. }
  627. } else {
  628. tick_ops = &stick_operations;
  629. clock = of_getintprop_default(dp, "stick-frequency", 0);
  630. }
  631. return clock;
  632. }
  633. struct freq_table {
  634. unsigned long clock_tick_ref;
  635. unsigned int ref_freq;
  636. };
  637. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  638. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  639. {
  640. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  641. if (ft->clock_tick_ref)
  642. return ft->clock_tick_ref;
  643. return cpu_data(cpu).clock_tick;
  644. }
  645. #ifdef CONFIG_CPU_FREQ
  646. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  647. void *data)
  648. {
  649. struct cpufreq_freqs *freq = data;
  650. unsigned int cpu = freq->cpu;
  651. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  652. if (!ft->ref_freq) {
  653. ft->ref_freq = freq->old;
  654. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  655. }
  656. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  657. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  658. (val == CPUFREQ_RESUMECHANGE)) {
  659. cpu_data(cpu).clock_tick =
  660. cpufreq_scale(ft->clock_tick_ref,
  661. ft->ref_freq,
  662. freq->new);
  663. }
  664. return 0;
  665. }
  666. static struct notifier_block sparc64_cpufreq_notifier_block = {
  667. .notifier_call = sparc64_cpufreq_notifier
  668. };
  669. static int __init register_sparc64_cpufreq_notifier(void)
  670. {
  671. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  672. CPUFREQ_TRANSITION_NOTIFIER);
  673. return 0;
  674. }
  675. core_initcall(register_sparc64_cpufreq_notifier);
  676. #endif /* CONFIG_CPU_FREQ */
  677. static int sparc64_next_event(unsigned long delta,
  678. struct clock_event_device *evt)
  679. {
  680. return tick_ops->add_compare(delta) ? -ETIME : 0;
  681. }
  682. static void sparc64_timer_setup(enum clock_event_mode mode,
  683. struct clock_event_device *evt)
  684. {
  685. switch (mode) {
  686. case CLOCK_EVT_MODE_ONESHOT:
  687. case CLOCK_EVT_MODE_RESUME:
  688. break;
  689. case CLOCK_EVT_MODE_SHUTDOWN:
  690. tick_ops->disable_irq();
  691. break;
  692. case CLOCK_EVT_MODE_PERIODIC:
  693. case CLOCK_EVT_MODE_UNUSED:
  694. WARN_ON(1);
  695. break;
  696. };
  697. }
  698. static struct clock_event_device sparc64_clockevent = {
  699. .features = CLOCK_EVT_FEAT_ONESHOT,
  700. .set_mode = sparc64_timer_setup,
  701. .set_next_event = sparc64_next_event,
  702. .rating = 100,
  703. .shift = 30,
  704. .irq = -1,
  705. };
  706. static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
  707. void timer_interrupt(int irq, struct pt_regs *regs)
  708. {
  709. struct pt_regs *old_regs = set_irq_regs(regs);
  710. unsigned long tick_mask = tick_ops->softint_mask;
  711. int cpu = smp_processor_id();
  712. struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
  713. clear_softint(tick_mask);
  714. irq_enter();
  715. kstat_this_cpu.irqs[0]++;
  716. if (unlikely(!evt->event_handler)) {
  717. printk(KERN_WARNING
  718. "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
  719. } else
  720. evt->event_handler(evt);
  721. irq_exit();
  722. set_irq_regs(old_regs);
  723. }
  724. void __devinit setup_sparc64_timer(void)
  725. {
  726. struct clock_event_device *sevt;
  727. unsigned long pstate;
  728. /* Guarantee that the following sequences execute
  729. * uninterrupted.
  730. */
  731. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  732. "wrpr %0, %1, %%pstate"
  733. : "=r" (pstate)
  734. : "i" (PSTATE_IE));
  735. tick_ops->init_tick();
  736. /* Restore PSTATE_IE. */
  737. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  738. : /* no outputs */
  739. : "r" (pstate));
  740. sevt = &__get_cpu_var(sparc64_events);
  741. memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
  742. sevt->cpumask = cpumask_of_cpu(smp_processor_id());
  743. clockevents_register_device(sevt);
  744. }
  745. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  746. static struct clocksource clocksource_tick = {
  747. .rating = 100,
  748. .mask = CLOCKSOURCE_MASK(64),
  749. .shift = 16,
  750. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  751. };
  752. static void __init setup_clockevent_multiplier(unsigned long hz)
  753. {
  754. unsigned long mult, shift = 32;
  755. while (1) {
  756. mult = div_sc(hz, NSEC_PER_SEC, shift);
  757. if (mult && (mult >> 32UL) == 0UL)
  758. break;
  759. shift--;
  760. }
  761. sparc64_clockevent.shift = shift;
  762. sparc64_clockevent.mult = mult;
  763. }
  764. static unsigned long tb_ticks_per_usec __read_mostly;
  765. void __delay(unsigned long loops)
  766. {
  767. unsigned long bclock, now;
  768. bclock = tick_ops->get_tick();
  769. do {
  770. now = tick_ops->get_tick();
  771. } while ((now-bclock) < loops);
  772. }
  773. EXPORT_SYMBOL(__delay);
  774. void udelay(unsigned long usecs)
  775. {
  776. __delay(tb_ticks_per_usec * usecs);
  777. }
  778. EXPORT_SYMBOL(udelay);
  779. void __init time_init(void)
  780. {
  781. unsigned long clock = sparc64_init_timers();
  782. tb_ticks_per_usec = clock / USEC_PER_SEC;
  783. timer_ticks_per_nsec_quotient =
  784. clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
  785. clocksource_tick.name = tick_ops->name;
  786. clocksource_tick.mult =
  787. clocksource_hz2mult(clock,
  788. clocksource_tick.shift);
  789. clocksource_tick.read = tick_ops->get_tick;
  790. printk("clocksource: mult[%x] shift[%d]\n",
  791. clocksource_tick.mult, clocksource_tick.shift);
  792. clocksource_register(&clocksource_tick);
  793. sparc64_clockevent.name = tick_ops->name;
  794. setup_clockevent_multiplier(clock);
  795. sparc64_clockevent.max_delta_ns =
  796. clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
  797. sparc64_clockevent.min_delta_ns =
  798. clockevent_delta2ns(0xF, &sparc64_clockevent);
  799. printk("clockevent: mult[%lx] shift[%d]\n",
  800. sparc64_clockevent.mult, sparc64_clockevent.shift);
  801. setup_sparc64_timer();
  802. }
  803. unsigned long long sched_clock(void)
  804. {
  805. unsigned long ticks = tick_ops->get_tick();
  806. return (ticks * timer_ticks_per_nsec_quotient)
  807. >> SPARC64_NSEC_PER_CYC_SHIFT;
  808. }
  809. static int set_rtc_mmss(unsigned long nowtime)
  810. {
  811. int real_seconds, real_minutes, chip_minutes;
  812. void __iomem *bregs = bq4802_regs;
  813. unsigned long flags;
  814. unsigned char val;
  815. int retval = 0;
  816. /*
  817. * Not having a register set can lead to trouble.
  818. * Also starfire doesn't have a tod clock.
  819. */
  820. if (!bregs)
  821. return -1;
  822. spin_lock_irqsave(&rtc_lock, flags);
  823. val = readb(bregs + 0x0e);
  824. /* BQ4802 RTC chip. */
  825. writeb(val | 0x08, bregs + 0x0e);
  826. chip_minutes = readb(bregs + 0x02);
  827. BCD_TO_BIN(chip_minutes);
  828. real_seconds = nowtime % 60;
  829. real_minutes = nowtime / 60;
  830. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  831. real_minutes += 30;
  832. real_minutes %= 60;
  833. if (abs(real_minutes - chip_minutes) < 30) {
  834. BIN_TO_BCD(real_seconds);
  835. BIN_TO_BCD(real_minutes);
  836. writeb(real_seconds, bregs + 0x00);
  837. writeb(real_minutes, bregs + 0x02);
  838. } else {
  839. printk(KERN_WARNING
  840. "set_rtc_mmss: can't update from %d to %d\n",
  841. chip_minutes, real_minutes);
  842. retval = -1;
  843. }
  844. writeb(val, bregs + 0x0e);
  845. spin_unlock_irqrestore(&rtc_lock, flags);
  846. return retval;
  847. }
  848. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  849. static unsigned char mini_rtc_status; /* bitmapped status byte. */
  850. #define FEBRUARY 2
  851. #define STARTOFTIME 1970
  852. #define SECDAY 86400L
  853. #define SECYR (SECDAY * 365)
  854. #define leapyear(year) ((year) % 4 == 0 && \
  855. ((year) % 100 != 0 || (year) % 400 == 0))
  856. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  857. #define days_in_month(a) (month_days[(a) - 1])
  858. static int month_days[12] = {
  859. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  860. };
  861. /*
  862. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  863. */
  864. static void GregorianDay(struct rtc_time * tm)
  865. {
  866. int leapsToDate;
  867. int lastYear;
  868. int day;
  869. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  870. lastYear = tm->tm_year - 1;
  871. /*
  872. * Number of leap corrections to apply up to end of last year
  873. */
  874. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  875. /*
  876. * This year is a leap year if it is divisible by 4 except when it is
  877. * divisible by 100 unless it is divisible by 400
  878. *
  879. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  880. */
  881. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  882. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  883. tm->tm_mday;
  884. tm->tm_wday = day % 7;
  885. }
  886. static void to_tm(int tim, struct rtc_time *tm)
  887. {
  888. register int i;
  889. register long hms, day;
  890. day = tim / SECDAY;
  891. hms = tim % SECDAY;
  892. /* Hours, minutes, seconds are easy */
  893. tm->tm_hour = hms / 3600;
  894. tm->tm_min = (hms % 3600) / 60;
  895. tm->tm_sec = (hms % 3600) % 60;
  896. /* Number of years in days */
  897. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  898. day -= days_in_year(i);
  899. tm->tm_year = i;
  900. /* Number of months in days left */
  901. if (leapyear(tm->tm_year))
  902. days_in_month(FEBRUARY) = 29;
  903. for (i = 1; day >= days_in_month(i); i++)
  904. day -= days_in_month(i);
  905. days_in_month(FEBRUARY) = 28;
  906. tm->tm_mon = i;
  907. /* Days are what is left over (+1) from all that. */
  908. tm->tm_mday = day + 1;
  909. /*
  910. * Determine the day of week
  911. */
  912. GregorianDay(tm);
  913. }
  914. /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
  915. * aka Unix time. So we have to convert to/from rtc_time.
  916. */
  917. static void starfire_get_rtc_time(struct rtc_time *time)
  918. {
  919. u32 seconds = starfire_get_time();
  920. to_tm(seconds, time);
  921. time->tm_year -= 1900;
  922. time->tm_mon -= 1;
  923. }
  924. static int starfire_set_rtc_time(struct rtc_time *time)
  925. {
  926. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  927. time->tm_mday, time->tm_hour,
  928. time->tm_min, time->tm_sec);
  929. return starfire_set_time(seconds);
  930. }
  931. static void hypervisor_get_rtc_time(struct rtc_time *time)
  932. {
  933. u32 seconds = hypervisor_get_time();
  934. to_tm(seconds, time);
  935. time->tm_year -= 1900;
  936. time->tm_mon -= 1;
  937. }
  938. static int hypervisor_set_rtc_time(struct rtc_time *time)
  939. {
  940. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  941. time->tm_mday, time->tm_hour,
  942. time->tm_min, time->tm_sec);
  943. return hypervisor_set_time(seconds);
  944. }
  945. static void bq4802_get_rtc_time(struct rtc_time *time)
  946. {
  947. unsigned char val = readb(bq4802_regs + 0x0e);
  948. unsigned int century;
  949. writeb(val | 0x08, bq4802_regs + 0x0e);
  950. time->tm_sec = readb(bq4802_regs + 0x00);
  951. time->tm_min = readb(bq4802_regs + 0x02);
  952. time->tm_hour = readb(bq4802_regs + 0x04);
  953. time->tm_mday = readb(bq4802_regs + 0x06);
  954. time->tm_mon = readb(bq4802_regs + 0x09);
  955. time->tm_year = readb(bq4802_regs + 0x0a);
  956. time->tm_wday = readb(bq4802_regs + 0x08);
  957. century = readb(bq4802_regs + 0x0f);
  958. writeb(val, bq4802_regs + 0x0e);
  959. BCD_TO_BIN(time->tm_sec);
  960. BCD_TO_BIN(time->tm_min);
  961. BCD_TO_BIN(time->tm_hour);
  962. BCD_TO_BIN(time->tm_mday);
  963. BCD_TO_BIN(time->tm_mon);
  964. BCD_TO_BIN(time->tm_year);
  965. BCD_TO_BIN(time->tm_wday);
  966. BCD_TO_BIN(century);
  967. time->tm_year += (century * 100);
  968. time->tm_year -= 1900;
  969. time->tm_mon--;
  970. }
  971. static int bq4802_set_rtc_time(struct rtc_time *time)
  972. {
  973. unsigned char val = readb(bq4802_regs + 0x0e);
  974. unsigned char sec, min, hrs, day, mon, yrs, century;
  975. unsigned int year;
  976. year = time->tm_year + 1900;
  977. century = year / 100;
  978. yrs = year % 100;
  979. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  980. day = time->tm_mday;
  981. hrs = time->tm_hour;
  982. min = time->tm_min;
  983. sec = time->tm_sec;
  984. BIN_TO_BCD(sec);
  985. BIN_TO_BCD(min);
  986. BIN_TO_BCD(hrs);
  987. BIN_TO_BCD(day);
  988. BIN_TO_BCD(mon);
  989. BIN_TO_BCD(yrs);
  990. BIN_TO_BCD(century);
  991. writeb(val | 0x08, bq4802_regs + 0x0e);
  992. writeb(sec, bq4802_regs + 0x00);
  993. writeb(min, bq4802_regs + 0x02);
  994. writeb(hrs, bq4802_regs + 0x04);
  995. writeb(day, bq4802_regs + 0x06);
  996. writeb(mon, bq4802_regs + 0x09);
  997. writeb(yrs, bq4802_regs + 0x0a);
  998. writeb(century, bq4802_regs + 0x0f);
  999. writeb(val, bq4802_regs + 0x0e);
  1000. return 0;
  1001. }
  1002. struct mini_rtc_ops {
  1003. void (*get_rtc_time)(struct rtc_time *);
  1004. int (*set_rtc_time)(struct rtc_time *);
  1005. };
  1006. static struct mini_rtc_ops starfire_rtc_ops = {
  1007. .get_rtc_time = starfire_get_rtc_time,
  1008. .set_rtc_time = starfire_set_rtc_time,
  1009. };
  1010. static struct mini_rtc_ops hypervisor_rtc_ops = {
  1011. .get_rtc_time = hypervisor_get_rtc_time,
  1012. .set_rtc_time = hypervisor_set_rtc_time,
  1013. };
  1014. static struct mini_rtc_ops bq4802_rtc_ops = {
  1015. .get_rtc_time = bq4802_get_rtc_time,
  1016. .set_rtc_time = bq4802_set_rtc_time,
  1017. };
  1018. static struct mini_rtc_ops *mini_rtc_ops;
  1019. static inline void mini_get_rtc_time(struct rtc_time *time)
  1020. {
  1021. unsigned long flags;
  1022. spin_lock_irqsave(&rtc_lock, flags);
  1023. mini_rtc_ops->get_rtc_time(time);
  1024. spin_unlock_irqrestore(&rtc_lock, flags);
  1025. }
  1026. static inline int mini_set_rtc_time(struct rtc_time *time)
  1027. {
  1028. unsigned long flags;
  1029. int err;
  1030. spin_lock_irqsave(&rtc_lock, flags);
  1031. err = mini_rtc_ops->set_rtc_time(time);
  1032. spin_unlock_irqrestore(&rtc_lock, flags);
  1033. return err;
  1034. }
  1035. static int mini_rtc_ioctl(struct inode *inode, struct file *file,
  1036. unsigned int cmd, unsigned long arg)
  1037. {
  1038. struct rtc_time wtime;
  1039. void __user *argp = (void __user *)arg;
  1040. switch (cmd) {
  1041. case RTC_PLL_GET:
  1042. return -EINVAL;
  1043. case RTC_PLL_SET:
  1044. return -EINVAL;
  1045. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  1046. return 0;
  1047. case RTC_UIE_ON: /* enable ints for RTC updates. */
  1048. return -EINVAL;
  1049. case RTC_RD_TIME: /* Read the time/date from RTC */
  1050. /* this doesn't get week-day, who cares */
  1051. memset(&wtime, 0, sizeof(wtime));
  1052. mini_get_rtc_time(&wtime);
  1053. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  1054. case RTC_SET_TIME: /* Set the RTC */
  1055. {
  1056. int year, days;
  1057. if (!capable(CAP_SYS_TIME))
  1058. return -EACCES;
  1059. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  1060. return -EFAULT;
  1061. year = wtime.tm_year + 1900;
  1062. days = month_days[wtime.tm_mon] +
  1063. ((wtime.tm_mon == 1) && leapyear(year));
  1064. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
  1065. (wtime.tm_mday < 1))
  1066. return -EINVAL;
  1067. if (wtime.tm_mday < 0 || wtime.tm_mday > days)
  1068. return -EINVAL;
  1069. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  1070. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  1071. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  1072. return -EINVAL;
  1073. return mini_set_rtc_time(&wtime);
  1074. }
  1075. }
  1076. return -EINVAL;
  1077. }
  1078. static int mini_rtc_open(struct inode *inode, struct file *file)
  1079. {
  1080. lock_kernel();
  1081. if (mini_rtc_status & RTC_IS_OPEN) {
  1082. unlock_kernel();
  1083. return -EBUSY;
  1084. }
  1085. mini_rtc_status |= RTC_IS_OPEN;
  1086. unlock_kernel();
  1087. return 0;
  1088. }
  1089. static int mini_rtc_release(struct inode *inode, struct file *file)
  1090. {
  1091. mini_rtc_status &= ~RTC_IS_OPEN;
  1092. return 0;
  1093. }
  1094. static const struct file_operations mini_rtc_fops = {
  1095. .owner = THIS_MODULE,
  1096. .ioctl = mini_rtc_ioctl,
  1097. .open = mini_rtc_open,
  1098. .release = mini_rtc_release,
  1099. };
  1100. static struct miscdevice rtc_mini_dev =
  1101. {
  1102. .minor = RTC_MINOR,
  1103. .name = "rtc",
  1104. .fops = &mini_rtc_fops,
  1105. };
  1106. static int __init rtc_mini_init(void)
  1107. {
  1108. int retval;
  1109. if (tlb_type == hypervisor)
  1110. mini_rtc_ops = &hypervisor_rtc_ops;
  1111. else if (this_is_starfire)
  1112. mini_rtc_ops = &starfire_rtc_ops;
  1113. else if (bq4802_regs)
  1114. mini_rtc_ops = &bq4802_rtc_ops;
  1115. else
  1116. return -ENODEV;
  1117. printk(KERN_INFO "Mini RTC Driver\n");
  1118. retval = misc_register(&rtc_mini_dev);
  1119. if (retval < 0)
  1120. return retval;
  1121. return 0;
  1122. }
  1123. static void __exit rtc_mini_exit(void)
  1124. {
  1125. misc_deregister(&rtc_mini_dev);
  1126. }
  1127. int __devinit read_current_timer(unsigned long *timer_val)
  1128. {
  1129. *timer_val = tick_ops->get_tick();
  1130. return 0;
  1131. }
  1132. module_init(rtc_mini_init);
  1133. module_exit(rtc_mini_exit);