time_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 <linux/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 __cacheline_aligned = {
  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. unsigned long cmos_regs;
  333. EXPORT_SYMBOL(cmos_regs);
  334. static struct resource rtc_cmos_resource;
  335. static struct platform_device rtc_cmos_device = {
  336. .name = "rtc_cmos",
  337. .id = -1,
  338. .resource = &rtc_cmos_resource,
  339. .num_resources = 1,
  340. };
  341. static int rtc_probe(struct platform_device *op)
  342. {
  343. struct resource *r;
  344. printk(KERN_INFO "%s: RTC regs at 0x%llx\n",
  345. op->dev.of_node->full_name, op->resource[0].start);
  346. /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
  347. * up a fake resource so that the probe works for all cases.
  348. * When the RTC is behind an ISA bus it will have IORESOURCE_IO
  349. * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
  350. */
  351. r = &rtc_cmos_resource;
  352. r->flags = IORESOURCE_IO;
  353. r->name = op->resource[0].name;
  354. r->start = op->resource[0].start;
  355. r->end = op->resource[0].end;
  356. cmos_regs = op->resource[0].start;
  357. return platform_device_register(&rtc_cmos_device);
  358. }
  359. static const struct of_device_id rtc_match[] = {
  360. {
  361. .name = "rtc",
  362. .compatible = "m5819",
  363. },
  364. {
  365. .name = "rtc",
  366. .compatible = "isa-m5819p",
  367. },
  368. {
  369. .name = "rtc",
  370. .compatible = "isa-m5823p",
  371. },
  372. {
  373. .name = "rtc",
  374. .compatible = "ds1287",
  375. },
  376. {},
  377. };
  378. static struct platform_driver rtc_driver = {
  379. .probe = rtc_probe,
  380. .driver = {
  381. .name = "rtc",
  382. .of_match_table = rtc_match,
  383. },
  384. };
  385. static struct platform_device rtc_bq4802_device = {
  386. .name = "rtc-bq4802",
  387. .id = -1,
  388. .num_resources = 1,
  389. };
  390. static int bq4802_probe(struct platform_device *op)
  391. {
  392. printk(KERN_INFO "%s: BQ4802 regs at 0x%llx\n",
  393. op->dev.of_node->full_name, op->resource[0].start);
  394. rtc_bq4802_device.resource = &op->resource[0];
  395. return platform_device_register(&rtc_bq4802_device);
  396. }
  397. static const struct of_device_id bq4802_match[] = {
  398. {
  399. .name = "rtc",
  400. .compatible = "bq4802",
  401. },
  402. {},
  403. };
  404. static struct platform_driver bq4802_driver = {
  405. .probe = bq4802_probe,
  406. .driver = {
  407. .name = "bq4802",
  408. .of_match_table = bq4802_match,
  409. },
  410. };
  411. static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
  412. {
  413. struct platform_device *pdev = to_platform_device(dev);
  414. void __iomem *regs = (void __iomem *) pdev->resource[0].start;
  415. return readb(regs + ofs);
  416. }
  417. static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
  418. {
  419. struct platform_device *pdev = to_platform_device(dev);
  420. void __iomem *regs = (void __iomem *) pdev->resource[0].start;
  421. writeb(val, regs + ofs);
  422. }
  423. static struct m48t59_plat_data m48t59_data = {
  424. .read_byte = mostek_read_byte,
  425. .write_byte = mostek_write_byte,
  426. };
  427. static struct platform_device m48t59_rtc = {
  428. .name = "rtc-m48t59",
  429. .id = 0,
  430. .num_resources = 1,
  431. .dev = {
  432. .platform_data = &m48t59_data,
  433. },
  434. };
  435. static int mostek_probe(struct platform_device *op)
  436. {
  437. struct device_node *dp = op->dev.of_node;
  438. /* On an Enterprise system there can be multiple mostek clocks.
  439. * We should only match the one that is on the central FHC bus.
  440. */
  441. if (!strcmp(dp->parent->name, "fhc") &&
  442. strcmp(dp->parent->parent->name, "central") != 0)
  443. return -ENODEV;
  444. printk(KERN_INFO "%s: Mostek regs at 0x%llx\n",
  445. dp->full_name, op->resource[0].start);
  446. m48t59_rtc.resource = &op->resource[0];
  447. return platform_device_register(&m48t59_rtc);
  448. }
  449. static const struct of_device_id mostek_match[] = {
  450. {
  451. .name = "eeprom",
  452. },
  453. {},
  454. };
  455. static struct platform_driver mostek_driver = {
  456. .probe = mostek_probe,
  457. .driver = {
  458. .name = "mostek",
  459. .of_match_table = mostek_match,
  460. },
  461. };
  462. static struct platform_device rtc_sun4v_device = {
  463. .name = "rtc-sun4v",
  464. .id = -1,
  465. };
  466. static struct platform_device rtc_starfire_device = {
  467. .name = "rtc-starfire",
  468. .id = -1,
  469. };
  470. static int __init clock_init(void)
  471. {
  472. if (this_is_starfire)
  473. return platform_device_register(&rtc_starfire_device);
  474. if (tlb_type == hypervisor)
  475. return platform_device_register(&rtc_sun4v_device);
  476. (void) platform_driver_register(&rtc_driver);
  477. (void) platform_driver_register(&mostek_driver);
  478. (void) platform_driver_register(&bq4802_driver);
  479. return 0;
  480. }
  481. /* Must be after subsys_initcall() so that busses are probed. Must
  482. * be before device_initcall() because things like the RTC driver
  483. * need to see the clock registers.
  484. */
  485. fs_initcall(clock_init);
  486. /* This is gets the master TICK_INT timer going. */
  487. static unsigned long sparc64_init_timers(void)
  488. {
  489. struct sparc64_tick_ops *ops = NULL;
  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. ops = &hbtick_operations;
  502. freq = of_getintprop_default(dp, "stick-frequency", 0);
  503. } else {
  504. freq = local_cpu_data().clock_tick;
  505. }
  506. } else {
  507. ops = &stick_operations;
  508. freq = of_getintprop_default(dp, "stick-frequency", 0);
  509. }
  510. if (ops)
  511. memcpy(&tick_operations, ops, sizeof(struct sparc64_tick_ops));
  512. return freq;
  513. }
  514. struct freq_table {
  515. unsigned long clock_tick_ref;
  516. unsigned int ref_freq;
  517. };
  518. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  519. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  520. {
  521. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  522. if (ft->clock_tick_ref)
  523. return ft->clock_tick_ref;
  524. return cpu_data(cpu).clock_tick;
  525. }
  526. EXPORT_SYMBOL(sparc64_get_clock_tick);
  527. #ifdef CONFIG_CPU_FREQ
  528. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  529. void *data)
  530. {
  531. struct cpufreq_freqs *freq = data;
  532. unsigned int cpu = freq->cpu;
  533. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  534. if (!ft->ref_freq) {
  535. ft->ref_freq = freq->old;
  536. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  537. }
  538. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  539. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
  540. cpu_data(cpu).clock_tick =
  541. cpufreq_scale(ft->clock_tick_ref,
  542. ft->ref_freq,
  543. freq->new);
  544. }
  545. return 0;
  546. }
  547. static struct notifier_block sparc64_cpufreq_notifier_block = {
  548. .notifier_call = sparc64_cpufreq_notifier
  549. };
  550. static int __init register_sparc64_cpufreq_notifier(void)
  551. {
  552. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  553. CPUFREQ_TRANSITION_NOTIFIER);
  554. return 0;
  555. }
  556. core_initcall(register_sparc64_cpufreq_notifier);
  557. #endif /* CONFIG_CPU_FREQ */
  558. static int sparc64_next_event(unsigned long delta,
  559. struct clock_event_device *evt)
  560. {
  561. return tick_operations.add_compare(delta) ? -ETIME : 0;
  562. }
  563. static int sparc64_timer_shutdown(struct clock_event_device *evt)
  564. {
  565. tick_operations.disable_irq();
  566. return 0;
  567. }
  568. static struct clock_event_device sparc64_clockevent = {
  569. .features = CLOCK_EVT_FEAT_ONESHOT,
  570. .set_state_shutdown = sparc64_timer_shutdown,
  571. .set_next_event = sparc64_next_event,
  572. .rating = 100,
  573. .shift = 30,
  574. .irq = -1,
  575. };
  576. static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
  577. void __irq_entry timer_interrupt(int irq, struct pt_regs *regs)
  578. {
  579. struct pt_regs *old_regs = set_irq_regs(regs);
  580. unsigned long tick_mask = tick_operations.softint_mask;
  581. int cpu = smp_processor_id();
  582. struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
  583. clear_softint(tick_mask);
  584. irq_enter();
  585. local_cpu_data().irq0_irqs++;
  586. kstat_incr_irq_this_cpu(0);
  587. if (unlikely(!evt->event_handler)) {
  588. printk(KERN_WARNING
  589. "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
  590. } else
  591. evt->event_handler(evt);
  592. irq_exit();
  593. set_irq_regs(old_regs);
  594. }
  595. void setup_sparc64_timer(void)
  596. {
  597. struct clock_event_device *sevt;
  598. unsigned long pstate;
  599. /* Guarantee that the following sequences execute
  600. * uninterrupted.
  601. */
  602. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  603. "wrpr %0, %1, %%pstate"
  604. : "=r" (pstate)
  605. : "i" (PSTATE_IE));
  606. tick_operations.init_tick();
  607. /* Restore PSTATE_IE. */
  608. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  609. : /* no outputs */
  610. : "r" (pstate));
  611. sevt = this_cpu_ptr(&sparc64_events);
  612. memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
  613. sevt->cpumask = cpumask_of(smp_processor_id());
  614. clockevents_register_device(sevt);
  615. }
  616. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  617. static struct clocksource clocksource_tick = {
  618. .rating = 100,
  619. .mask = CLOCKSOURCE_MASK(64),
  620. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  621. };
  622. static unsigned long tb_ticks_per_usec __read_mostly;
  623. void __delay(unsigned long loops)
  624. {
  625. unsigned long bclock, now;
  626. bclock = tick_operations.get_tick();
  627. do {
  628. now = tick_operations.get_tick();
  629. } while ((now-bclock) < loops);
  630. }
  631. EXPORT_SYMBOL(__delay);
  632. void udelay(unsigned long usecs)
  633. {
  634. __delay(tb_ticks_per_usec * usecs);
  635. }
  636. EXPORT_SYMBOL(udelay);
  637. static u64 clocksource_tick_read(struct clocksource *cs)
  638. {
  639. return tick_operations.get_tick();
  640. }
  641. void __init time_init(void)
  642. {
  643. unsigned long freq = sparc64_init_timers();
  644. tb_ticks_per_usec = freq / USEC_PER_SEC;
  645. tick_operations.ticks_per_nsec_quotient =
  646. clocksource_hz2mult(freq, SPARC64_NSEC_PER_CYC_SHIFT);
  647. tick_operations.offset = (tick_operations.get_tick()
  648. * tick_operations.ticks_per_nsec_quotient)
  649. >> SPARC64_NSEC_PER_CYC_SHIFT;
  650. clocksource_tick.name = tick_operations.name;
  651. clocksource_tick.read = clocksource_tick_read;
  652. clocksource_register_hz(&clocksource_tick, freq);
  653. printk("clocksource: mult[%x] shift[%d]\n",
  654. clocksource_tick.mult, clocksource_tick.shift);
  655. sparc64_clockevent.name = tick_operations.name;
  656. clockevents_calc_mult_shift(&sparc64_clockevent, freq, 4);
  657. sparc64_clockevent.max_delta_ns =
  658. clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
  659. sparc64_clockevent.max_delta_ticks = 0x7fffffffffffffffUL;
  660. sparc64_clockevent.min_delta_ns =
  661. clockevent_delta2ns(0xF, &sparc64_clockevent);
  662. sparc64_clockevent.min_delta_ticks = 0xF;
  663. printk("clockevent: mult[%x] shift[%d]\n",
  664. sparc64_clockevent.mult, sparc64_clockevent.shift);
  665. setup_sparc64_timer();
  666. }
  667. unsigned long long sched_clock(void)
  668. {
  669. unsigned long quotient = tick_operations.ticks_per_nsec_quotient;
  670. unsigned long offset = tick_operations.offset;
  671. unsigned long ticks = tick_operations.get_tick();
  672. return ((ticks * quotient) >> SPARC64_NSEC_PER_CYC_SHIFT) - offset;
  673. }
  674. int read_current_timer(unsigned long *timer_val)
  675. {
  676. *timer_val = tick_operations.get_tick();
  677. return 0;
  678. }