time_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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/export.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/param.h>
  15. #include <linux/string.h>
  16. #include <linux/mm.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/time.h>
  19. #include <linux/timex.h>
  20. #include <linux/init.h>
  21. #include <linux/ioport.h>
  22. #include <linux/mc146818rtc.h>
  23. #include <linux/delay.h>
  24. #include <linux/profile.h>
  25. #include <linux/bcd.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/percpu.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/rtc/m48t59.h>
  31. #include <linux/kernel_stat.h>
  32. #include <linux/clockchips.h>
  33. #include <linux/clocksource.h>
  34. #include <linux/of_device.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/ftrace.h>
  37. #include <asm/oplib.h>
  38. #include <asm/timer.h>
  39. #include <asm/irq.h>
  40. #include <asm/io.h>
  41. #include <asm/prom.h>
  42. #include <asm/starfire.h>
  43. #include <asm/smp.h>
  44. #include <asm/sections.h>
  45. #include <asm/cpudata.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/irq_regs.h>
  48. #include "entry.h"
  49. DEFINE_SPINLOCK(rtc_lock);
  50. #define TICK_PRIV_BIT (1UL << 63)
  51. #define TICKCMP_IRQ_BIT (1UL << 63)
  52. #ifdef CONFIG_SMP
  53. unsigned long profile_pc(struct pt_regs *regs)
  54. {
  55. unsigned long pc = instruction_pointer(regs);
  56. if (in_lock_functions(pc))
  57. return regs->u_regs[UREG_RETPC];
  58. return pc;
  59. }
  60. EXPORT_SYMBOL(profile_pc);
  61. #endif
  62. static void tick_disable_protection(void)
  63. {
  64. /* Set things up so user can access tick register for profiling
  65. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  66. * read back of %tick after writing it.
  67. */
  68. __asm__ __volatile__(
  69. " ba,pt %%xcc, 1f\n"
  70. " nop\n"
  71. " .align 64\n"
  72. "1: rd %%tick, %%g2\n"
  73. " add %%g2, 6, %%g2\n"
  74. " andn %%g2, %0, %%g2\n"
  75. " wrpr %%g2, 0, %%tick\n"
  76. " rdpr %%tick, %%g0"
  77. : /* no outputs */
  78. : "r" (TICK_PRIV_BIT)
  79. : "g2");
  80. }
  81. static void tick_disable_irq(void)
  82. {
  83. __asm__ __volatile__(
  84. " ba,pt %%xcc, 1f\n"
  85. " nop\n"
  86. " .align 64\n"
  87. "1: wr %0, 0x0, %%tick_cmpr\n"
  88. " rd %%tick_cmpr, %%g0"
  89. : /* no outputs */
  90. : "r" (TICKCMP_IRQ_BIT));
  91. }
  92. static void tick_init_tick(void)
  93. {
  94. tick_disable_protection();
  95. tick_disable_irq();
  96. }
  97. static unsigned long long tick_get_tick(void)
  98. {
  99. unsigned long ret;
  100. __asm__ __volatile__("rd %%tick, %0\n\t"
  101. "mov %0, %0"
  102. : "=r" (ret));
  103. return ret & ~TICK_PRIV_BIT;
  104. }
  105. static int tick_add_compare(unsigned long adj)
  106. {
  107. unsigned long orig_tick, new_tick, new_compare;
  108. __asm__ __volatile__("rd %%tick, %0"
  109. : "=r" (orig_tick));
  110. orig_tick &= ~TICKCMP_IRQ_BIT;
  111. /* Workaround for Spitfire Errata (#54 I think??), I discovered
  112. * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
  113. * number 103640.
  114. *
  115. * On Blackbird writes to %tick_cmpr can fail, the
  116. * workaround seems to be to execute the wr instruction
  117. * at the start of an I-cache line, and perform a dummy
  118. * read back from %tick_cmpr right after writing to it. -DaveM
  119. */
  120. __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
  121. " add %1, %2, %0\n\t"
  122. ".align 64\n"
  123. "1:\n\t"
  124. "wr %0, 0, %%tick_cmpr\n\t"
  125. "rd %%tick_cmpr, %%g0\n\t"
  126. : "=r" (new_compare)
  127. : "r" (orig_tick), "r" (adj));
  128. __asm__ __volatile__("rd %%tick, %0"
  129. : "=r" (new_tick));
  130. new_tick &= ~TICKCMP_IRQ_BIT;
  131. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  132. }
  133. static unsigned long tick_add_tick(unsigned long adj)
  134. {
  135. unsigned long new_tick;
  136. /* Also need to handle Blackbird bug here too. */
  137. __asm__ __volatile__("rd %%tick, %0\n\t"
  138. "add %0, %1, %0\n\t"
  139. "wrpr %0, 0, %%tick\n\t"
  140. : "=&r" (new_tick)
  141. : "r" (adj));
  142. return new_tick;
  143. }
  144. static struct sparc64_tick_ops tick_operations __read_mostly = {
  145. .name = "tick",
  146. .init_tick = tick_init_tick,
  147. .disable_irq = tick_disable_irq,
  148. .get_tick = tick_get_tick,
  149. .add_tick = tick_add_tick,
  150. .add_compare = tick_add_compare,
  151. .softint_mask = 1UL << 0,
  152. };
  153. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  154. EXPORT_SYMBOL(tick_ops);
  155. static void stick_disable_irq(void)
  156. {
  157. __asm__ __volatile__(
  158. "wr %0, 0x0, %%asr25"
  159. : /* no outputs */
  160. : "r" (TICKCMP_IRQ_BIT));
  161. }
  162. static void stick_init_tick(void)
  163. {
  164. /* Writes to the %tick and %stick register are not
  165. * allowed on sun4v. The Hypervisor controls that
  166. * bit, per-strand.
  167. */
  168. if (tlb_type != hypervisor) {
  169. tick_disable_protection();
  170. tick_disable_irq();
  171. /* Let the user get at STICK too. */
  172. __asm__ __volatile__(
  173. " rd %%asr24, %%g2\n"
  174. " andn %%g2, %0, %%g2\n"
  175. " wr %%g2, 0, %%asr24"
  176. : /* no outputs */
  177. : "r" (TICK_PRIV_BIT)
  178. : "g1", "g2");
  179. }
  180. stick_disable_irq();
  181. }
  182. static unsigned long long stick_get_tick(void)
  183. {
  184. unsigned long ret;
  185. __asm__ __volatile__("rd %%asr24, %0"
  186. : "=r" (ret));
  187. return ret & ~TICK_PRIV_BIT;
  188. }
  189. static unsigned long stick_add_tick(unsigned long adj)
  190. {
  191. unsigned long new_tick;
  192. __asm__ __volatile__("rd %%asr24, %0\n\t"
  193. "add %0, %1, %0\n\t"
  194. "wr %0, 0, %%asr24\n\t"
  195. : "=&r" (new_tick)
  196. : "r" (adj));
  197. return new_tick;
  198. }
  199. static int stick_add_compare(unsigned long adj)
  200. {
  201. unsigned long orig_tick, new_tick;
  202. __asm__ __volatile__("rd %%asr24, %0"
  203. : "=r" (orig_tick));
  204. orig_tick &= ~TICKCMP_IRQ_BIT;
  205. __asm__ __volatile__("wr %0, 0, %%asr25"
  206. : /* no outputs */
  207. : "r" (orig_tick + adj));
  208. __asm__ __volatile__("rd %%asr24, %0"
  209. : "=r" (new_tick));
  210. new_tick &= ~TICKCMP_IRQ_BIT;
  211. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  212. }
  213. static struct sparc64_tick_ops stick_operations __read_mostly = {
  214. .name = "stick",
  215. .init_tick = stick_init_tick,
  216. .disable_irq = stick_disable_irq,
  217. .get_tick = stick_get_tick,
  218. .add_tick = stick_add_tick,
  219. .add_compare = stick_add_compare,
  220. .softint_mask = 1UL << 16,
  221. };
  222. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  223. * in I/O space. There are two 64-bit registers each, the
  224. * first holds the low 32-bits of the value and the second holds
  225. * the high 32-bits.
  226. *
  227. * Since STICK is constantly updating, we have to access it carefully.
  228. *
  229. * The sequence we use to read is:
  230. * 1) read high
  231. * 2) read low
  232. * 3) read high again, if it rolled re-read both low and high again.
  233. *
  234. * Writing STICK safely is also tricky:
  235. * 1) write low to zero
  236. * 2) write high
  237. * 3) write low
  238. */
  239. #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
  240. #define HBIRD_STICK_ADDR 0x1fe0000f070UL
  241. static unsigned long __hbird_read_stick(void)
  242. {
  243. unsigned long ret, tmp1, tmp2, tmp3;
  244. unsigned long addr = HBIRD_STICK_ADDR+8;
  245. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  246. "1:\n\t"
  247. "sub %1, 0x8, %1\n\t"
  248. "ldxa [%1] %5, %3\n\t"
  249. "add %1, 0x8, %1\n\t"
  250. "ldxa [%1] %5, %4\n\t"
  251. "cmp %4, %2\n\t"
  252. "bne,a,pn %%xcc, 1b\n\t"
  253. " mov %4, %2\n\t"
  254. "sllx %4, 32, %4\n\t"
  255. "or %3, %4, %0\n\t"
  256. : "=&r" (ret), "=&r" (addr),
  257. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  258. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  259. return ret;
  260. }
  261. static void __hbird_write_stick(unsigned long val)
  262. {
  263. unsigned long low = (val & 0xffffffffUL);
  264. unsigned long high = (val >> 32UL);
  265. unsigned long addr = HBIRD_STICK_ADDR;
  266. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  267. "add %0, 0x8, %0\n\t"
  268. "stxa %3, [%0] %4\n\t"
  269. "sub %0, 0x8, %0\n\t"
  270. "stxa %2, [%0] %4"
  271. : "=&r" (addr)
  272. : "0" (addr), "r" (low), "r" (high),
  273. "i" (ASI_PHYS_BYPASS_EC_E));
  274. }
  275. static void __hbird_write_compare(unsigned long val)
  276. {
  277. unsigned long low = (val & 0xffffffffUL);
  278. unsigned long high = (val >> 32UL);
  279. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  280. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  281. "sub %0, 0x8, %0\n\t"
  282. "stxa %2, [%0] %4"
  283. : "=&r" (addr)
  284. : "0" (addr), "r" (low), "r" (high),
  285. "i" (ASI_PHYS_BYPASS_EC_E));
  286. }
  287. static void hbtick_disable_irq(void)
  288. {
  289. __hbird_write_compare(TICKCMP_IRQ_BIT);
  290. }
  291. static void hbtick_init_tick(void)
  292. {
  293. tick_disable_protection();
  294. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  295. * XXX into actually sending STICK interrupts. I think because
  296. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  297. * XXX {TICK + STICK} interrupt mux. -DaveM
  298. */
  299. __hbird_write_stick(__hbird_read_stick());
  300. hbtick_disable_irq();
  301. }
  302. static unsigned long long hbtick_get_tick(void)
  303. {
  304. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  305. }
  306. static unsigned long hbtick_add_tick(unsigned long adj)
  307. {
  308. unsigned long val;
  309. val = __hbird_read_stick() + adj;
  310. __hbird_write_stick(val);
  311. return val;
  312. }
  313. static int hbtick_add_compare(unsigned long adj)
  314. {
  315. unsigned long val = __hbird_read_stick();
  316. unsigned long val2;
  317. val &= ~TICKCMP_IRQ_BIT;
  318. val += adj;
  319. __hbird_write_compare(val);
  320. val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
  321. return ((long)(val2 - val)) > 0L;
  322. }
  323. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  324. .name = "hbtick",
  325. .init_tick = hbtick_init_tick,
  326. .disable_irq = hbtick_disable_irq,
  327. .get_tick = hbtick_get_tick,
  328. .add_tick = hbtick_add_tick,
  329. .add_compare = hbtick_add_compare,
  330. .softint_mask = 1UL << 0,
  331. };
  332. static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
  333. unsigned long cmos_regs;
  334. EXPORT_SYMBOL(cmos_regs);
  335. static struct resource rtc_cmos_resource;
  336. static struct platform_device rtc_cmos_device = {
  337. .name = "rtc_cmos",
  338. .id = -1,
  339. .resource = &rtc_cmos_resource,
  340. .num_resources = 1,
  341. };
  342. static int rtc_probe(struct platform_device *op)
  343. {
  344. struct resource *r;
  345. printk(KERN_INFO "%s: RTC regs at 0x%llx\n",
  346. op->dev.of_node->full_name, op->resource[0].start);
  347. /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
  348. * up a fake resource so that the probe works for all cases.
  349. * When the RTC is behind an ISA bus it will have IORESOURCE_IO
  350. * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
  351. */
  352. r = &rtc_cmos_resource;
  353. r->flags = IORESOURCE_IO;
  354. r->name = op->resource[0].name;
  355. r->start = op->resource[0].start;
  356. r->end = op->resource[0].end;
  357. cmos_regs = op->resource[0].start;
  358. return platform_device_register(&rtc_cmos_device);
  359. }
  360. static const struct of_device_id rtc_match[] = {
  361. {
  362. .name = "rtc",
  363. .compatible = "m5819",
  364. },
  365. {
  366. .name = "rtc",
  367. .compatible = "isa-m5819p",
  368. },
  369. {
  370. .name = "rtc",
  371. .compatible = "isa-m5823p",
  372. },
  373. {
  374. .name = "rtc",
  375. .compatible = "ds1287",
  376. },
  377. {},
  378. };
  379. static struct platform_driver rtc_driver = {
  380. .probe = rtc_probe,
  381. .driver = {
  382. .name = "rtc",
  383. .of_match_table = rtc_match,
  384. },
  385. };
  386. static struct platform_device rtc_bq4802_device = {
  387. .name = "rtc-bq4802",
  388. .id = -1,
  389. .num_resources = 1,
  390. };
  391. static int bq4802_probe(struct platform_device *op)
  392. {
  393. printk(KERN_INFO "%s: BQ4802 regs at 0x%llx\n",
  394. op->dev.of_node->full_name, op->resource[0].start);
  395. rtc_bq4802_device.resource = &op->resource[0];
  396. return platform_device_register(&rtc_bq4802_device);
  397. }
  398. static const struct of_device_id bq4802_match[] = {
  399. {
  400. .name = "rtc",
  401. .compatible = "bq4802",
  402. },
  403. {},
  404. };
  405. static struct platform_driver bq4802_driver = {
  406. .probe = bq4802_probe,
  407. .driver = {
  408. .name = "bq4802",
  409. .of_match_table = bq4802_match,
  410. },
  411. };
  412. static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
  413. {
  414. struct platform_device *pdev = to_platform_device(dev);
  415. void __iomem *regs = (void __iomem *) pdev->resource[0].start;
  416. return readb(regs + ofs);
  417. }
  418. static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
  419. {
  420. struct platform_device *pdev = to_platform_device(dev);
  421. void __iomem *regs = (void __iomem *) pdev->resource[0].start;
  422. writeb(val, regs + ofs);
  423. }
  424. static struct m48t59_plat_data m48t59_data = {
  425. .read_byte = mostek_read_byte,
  426. .write_byte = mostek_write_byte,
  427. };
  428. static struct platform_device m48t59_rtc = {
  429. .name = "rtc-m48t59",
  430. .id = 0,
  431. .num_resources = 1,
  432. .dev = {
  433. .platform_data = &m48t59_data,
  434. },
  435. };
  436. static int mostek_probe(struct platform_device *op)
  437. {
  438. struct device_node *dp = op->dev.of_node;
  439. /* On an Enterprise system there can be multiple mostek clocks.
  440. * We should only match the one that is on the central FHC bus.
  441. */
  442. if (!strcmp(dp->parent->name, "fhc") &&
  443. strcmp(dp->parent->parent->name, "central") != 0)
  444. return -ENODEV;
  445. printk(KERN_INFO "%s: Mostek regs at 0x%llx\n",
  446. dp->full_name, op->resource[0].start);
  447. m48t59_rtc.resource = &op->resource[0];
  448. return platform_device_register(&m48t59_rtc);
  449. }
  450. static const struct of_device_id mostek_match[] = {
  451. {
  452. .name = "eeprom",
  453. },
  454. {},
  455. };
  456. static struct platform_driver mostek_driver = {
  457. .probe = mostek_probe,
  458. .driver = {
  459. .name = "mostek",
  460. .of_match_table = mostek_match,
  461. },
  462. };
  463. static struct platform_device rtc_sun4v_device = {
  464. .name = "rtc-sun4v",
  465. .id = -1,
  466. };
  467. static struct platform_device rtc_starfire_device = {
  468. .name = "rtc-starfire",
  469. .id = -1,
  470. };
  471. static int __init clock_init(void)
  472. {
  473. if (this_is_starfire)
  474. return platform_device_register(&rtc_starfire_device);
  475. if (tlb_type == hypervisor)
  476. return platform_device_register(&rtc_sun4v_device);
  477. (void) platform_driver_register(&rtc_driver);
  478. (void) platform_driver_register(&mostek_driver);
  479. (void) platform_driver_register(&bq4802_driver);
  480. return 0;
  481. }
  482. /* Must be after subsys_initcall() so that busses are probed. Must
  483. * be before device_initcall() because things like the RTC driver
  484. * need to see the clock registers.
  485. */
  486. fs_initcall(clock_init);
  487. /* This is gets the master TICK_INT timer going. */
  488. static unsigned long sparc64_init_timers(void)
  489. {
  490. struct device_node *dp;
  491. unsigned long freq;
  492. dp = of_find_node_by_path("/");
  493. if (tlb_type == spitfire) {
  494. unsigned long ver, manuf, impl;
  495. __asm__ __volatile__ ("rdpr %%ver, %0"
  496. : "=&r" (ver));
  497. manuf = ((ver >> 48) & 0xffff);
  498. impl = ((ver >> 32) & 0xffff);
  499. if (manuf == 0x17 && impl == 0x13) {
  500. /* Hummingbird, aka Ultra-IIe */
  501. tick_ops = &hbtick_operations;
  502. freq = of_getintprop_default(dp, "stick-frequency", 0);
  503. } else {
  504. tick_ops = &tick_operations;
  505. freq = local_cpu_data().clock_tick;
  506. }
  507. } else {
  508. tick_ops = &stick_operations;
  509. freq = of_getintprop_default(dp, "stick-frequency", 0);
  510. }
  511. return freq;
  512. }
  513. struct freq_table {
  514. unsigned long clock_tick_ref;
  515. unsigned int ref_freq;
  516. };
  517. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  518. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  519. {
  520. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  521. if (ft->clock_tick_ref)
  522. return ft->clock_tick_ref;
  523. return cpu_data(cpu).clock_tick;
  524. }
  525. EXPORT_SYMBOL(sparc64_get_clock_tick);
  526. #ifdef CONFIG_CPU_FREQ
  527. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  528. void *data)
  529. {
  530. struct cpufreq_freqs *freq = data;
  531. unsigned int cpu = freq->cpu;
  532. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  533. if (!ft->ref_freq) {
  534. ft->ref_freq = freq->old;
  535. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  536. }
  537. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  538. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
  539. cpu_data(cpu).clock_tick =
  540. cpufreq_scale(ft->clock_tick_ref,
  541. ft->ref_freq,
  542. freq->new);
  543. }
  544. return 0;
  545. }
  546. static struct notifier_block sparc64_cpufreq_notifier_block = {
  547. .notifier_call = sparc64_cpufreq_notifier
  548. };
  549. static int __init register_sparc64_cpufreq_notifier(void)
  550. {
  551. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  552. CPUFREQ_TRANSITION_NOTIFIER);
  553. return 0;
  554. }
  555. core_initcall(register_sparc64_cpufreq_notifier);
  556. #endif /* CONFIG_CPU_FREQ */
  557. static int sparc64_next_event(unsigned long delta,
  558. struct clock_event_device *evt)
  559. {
  560. return tick_ops->add_compare(delta) ? -ETIME : 0;
  561. }
  562. static int sparc64_timer_shutdown(struct clock_event_device *evt)
  563. {
  564. tick_ops->disable_irq();
  565. return 0;
  566. }
  567. static struct clock_event_device sparc64_clockevent = {
  568. .features = CLOCK_EVT_FEAT_ONESHOT,
  569. .set_state_shutdown = sparc64_timer_shutdown,
  570. .set_next_event = sparc64_next_event,
  571. .rating = 100,
  572. .shift = 30,
  573. .irq = -1,
  574. };
  575. static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
  576. void __irq_entry timer_interrupt(int irq, struct pt_regs *regs)
  577. {
  578. struct pt_regs *old_regs = set_irq_regs(regs);
  579. unsigned long tick_mask = tick_ops->softint_mask;
  580. int cpu = smp_processor_id();
  581. struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
  582. clear_softint(tick_mask);
  583. irq_enter();
  584. local_cpu_data().irq0_irqs++;
  585. kstat_incr_irq_this_cpu(0);
  586. if (unlikely(!evt->event_handler)) {
  587. printk(KERN_WARNING
  588. "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
  589. } else
  590. evt->event_handler(evt);
  591. irq_exit();
  592. set_irq_regs(old_regs);
  593. }
  594. void setup_sparc64_timer(void)
  595. {
  596. struct clock_event_device *sevt;
  597. unsigned long pstate;
  598. /* Guarantee that the following sequences execute
  599. * uninterrupted.
  600. */
  601. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  602. "wrpr %0, %1, %%pstate"
  603. : "=r" (pstate)
  604. : "i" (PSTATE_IE));
  605. tick_ops->init_tick();
  606. /* Restore PSTATE_IE. */
  607. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  608. : /* no outputs */
  609. : "r" (pstate));
  610. sevt = this_cpu_ptr(&sparc64_events);
  611. memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
  612. sevt->cpumask = cpumask_of(smp_processor_id());
  613. clockevents_register_device(sevt);
  614. }
  615. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  616. static struct clocksource clocksource_tick = {
  617. .rating = 100,
  618. .mask = CLOCKSOURCE_MASK(64),
  619. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  620. };
  621. static unsigned long tb_ticks_per_usec __read_mostly;
  622. void __delay(unsigned long loops)
  623. {
  624. unsigned long bclock, now;
  625. bclock = tick_ops->get_tick();
  626. do {
  627. now = tick_ops->get_tick();
  628. } while ((now-bclock) < loops);
  629. }
  630. EXPORT_SYMBOL(__delay);
  631. void udelay(unsigned long usecs)
  632. {
  633. __delay(tb_ticks_per_usec * usecs);
  634. }
  635. EXPORT_SYMBOL(udelay);
  636. static cycle_t clocksource_tick_read(struct clocksource *cs)
  637. {
  638. return tick_ops->get_tick();
  639. }
  640. void __init time_init(void)
  641. {
  642. unsigned long freq = sparc64_init_timers();
  643. tb_ticks_per_usec = freq / USEC_PER_SEC;
  644. timer_ticks_per_nsec_quotient =
  645. clocksource_hz2mult(freq, SPARC64_NSEC_PER_CYC_SHIFT);
  646. clocksource_tick.name = tick_ops->name;
  647. clocksource_tick.read = clocksource_tick_read;
  648. clocksource_register_hz(&clocksource_tick, freq);
  649. printk("clocksource: mult[%x] shift[%d]\n",
  650. clocksource_tick.mult, clocksource_tick.shift);
  651. sparc64_clockevent.name = tick_ops->name;
  652. clockevents_calc_mult_shift(&sparc64_clockevent, freq, 4);
  653. sparc64_clockevent.max_delta_ns =
  654. clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
  655. sparc64_clockevent.min_delta_ns =
  656. clockevent_delta2ns(0xF, &sparc64_clockevent);
  657. printk("clockevent: mult[%x] shift[%d]\n",
  658. sparc64_clockevent.mult, sparc64_clockevent.shift);
  659. setup_sparc64_timer();
  660. }
  661. unsigned long long sched_clock(void)
  662. {
  663. unsigned long ticks = tick_ops->get_tick();
  664. return (ticks * timer_ticks_per_nsec_quotient)
  665. >> SPARC64_NSEC_PER_CYC_SHIFT;
  666. }
  667. int read_current_timer(unsigned long *timer_val)
  668. {
  669. *timer_val = tick_ops->get_tick();
  670. return 0;
  671. }