time_64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 unsigned long tick_get_frequency(void)
  145. {
  146. return local_cpu_data().clock_tick;
  147. }
  148. static struct sparc64_tick_ops tick_operations __cacheline_aligned = {
  149. .name = "tick",
  150. .init_tick = tick_init_tick,
  151. .disable_irq = tick_disable_irq,
  152. .get_tick = tick_get_tick,
  153. .add_tick = tick_add_tick,
  154. .add_compare = tick_add_compare,
  155. .get_frequency = tick_get_frequency,
  156. .softint_mask = 1UL << 0,
  157. };
  158. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  159. EXPORT_SYMBOL(tick_ops);
  160. static void stick_disable_irq(void)
  161. {
  162. __asm__ __volatile__(
  163. "wr %0, 0x0, %%asr25"
  164. : /* no outputs */
  165. : "r" (TICKCMP_IRQ_BIT));
  166. }
  167. static void stick_init_tick(void)
  168. {
  169. /* Writes to the %tick and %stick register are not
  170. * allowed on sun4v. The Hypervisor controls that
  171. * bit, per-strand.
  172. */
  173. if (tlb_type != hypervisor) {
  174. tick_disable_protection();
  175. tick_disable_irq();
  176. /* Let the user get at STICK too. */
  177. __asm__ __volatile__(
  178. " rd %%asr24, %%g2\n"
  179. " andn %%g2, %0, %%g2\n"
  180. " wr %%g2, 0, %%asr24"
  181. : /* no outputs */
  182. : "r" (TICK_PRIV_BIT)
  183. : "g1", "g2");
  184. }
  185. stick_disable_irq();
  186. }
  187. static unsigned long long stick_get_tick(void)
  188. {
  189. unsigned long ret;
  190. __asm__ __volatile__("rd %%asr24, %0"
  191. : "=r" (ret));
  192. return ret & ~TICK_PRIV_BIT;
  193. }
  194. static unsigned long stick_add_tick(unsigned long adj)
  195. {
  196. unsigned long new_tick;
  197. __asm__ __volatile__("rd %%asr24, %0\n\t"
  198. "add %0, %1, %0\n\t"
  199. "wr %0, 0, %%asr24\n\t"
  200. : "=&r" (new_tick)
  201. : "r" (adj));
  202. return new_tick;
  203. }
  204. static int stick_add_compare(unsigned long adj)
  205. {
  206. unsigned long orig_tick, new_tick;
  207. __asm__ __volatile__("rd %%asr24, %0"
  208. : "=r" (orig_tick));
  209. orig_tick &= ~TICKCMP_IRQ_BIT;
  210. __asm__ __volatile__("wr %0, 0, %%asr25"
  211. : /* no outputs */
  212. : "r" (orig_tick + adj));
  213. __asm__ __volatile__("rd %%asr24, %0"
  214. : "=r" (new_tick));
  215. new_tick &= ~TICKCMP_IRQ_BIT;
  216. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  217. }
  218. static unsigned long stick_get_frequency(void)
  219. {
  220. struct device_node *dp = of_find_node_by_path("/");
  221. return of_getintprop_default(dp, "stick-frequency", 0);
  222. }
  223. static struct sparc64_tick_ops stick_operations __read_mostly = {
  224. .name = "stick",
  225. .init_tick = stick_init_tick,
  226. .disable_irq = stick_disable_irq,
  227. .get_tick = stick_get_tick,
  228. .add_tick = stick_add_tick,
  229. .add_compare = stick_add_compare,
  230. .get_frequency = stick_get_frequency,
  231. .softint_mask = 1UL << 16,
  232. };
  233. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  234. * in I/O space. There are two 64-bit registers each, the
  235. * first holds the low 32-bits of the value and the second holds
  236. * the high 32-bits.
  237. *
  238. * Since STICK is constantly updating, we have to access it carefully.
  239. *
  240. * The sequence we use to read is:
  241. * 1) read high
  242. * 2) read low
  243. * 3) read high again, if it rolled re-read both low and high again.
  244. *
  245. * Writing STICK safely is also tricky:
  246. * 1) write low to zero
  247. * 2) write high
  248. * 3) write low
  249. */
  250. #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
  251. #define HBIRD_STICK_ADDR 0x1fe0000f070UL
  252. static unsigned long __hbird_read_stick(void)
  253. {
  254. unsigned long ret, tmp1, tmp2, tmp3;
  255. unsigned long addr = HBIRD_STICK_ADDR+8;
  256. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  257. "1:\n\t"
  258. "sub %1, 0x8, %1\n\t"
  259. "ldxa [%1] %5, %3\n\t"
  260. "add %1, 0x8, %1\n\t"
  261. "ldxa [%1] %5, %4\n\t"
  262. "cmp %4, %2\n\t"
  263. "bne,a,pn %%xcc, 1b\n\t"
  264. " mov %4, %2\n\t"
  265. "sllx %4, 32, %4\n\t"
  266. "or %3, %4, %0\n\t"
  267. : "=&r" (ret), "=&r" (addr),
  268. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  269. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  270. return ret;
  271. }
  272. static void __hbird_write_stick(unsigned long val)
  273. {
  274. unsigned long low = (val & 0xffffffffUL);
  275. unsigned long high = (val >> 32UL);
  276. unsigned long addr = HBIRD_STICK_ADDR;
  277. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  278. "add %0, 0x8, %0\n\t"
  279. "stxa %3, [%0] %4\n\t"
  280. "sub %0, 0x8, %0\n\t"
  281. "stxa %2, [%0] %4"
  282. : "=&r" (addr)
  283. : "0" (addr), "r" (low), "r" (high),
  284. "i" (ASI_PHYS_BYPASS_EC_E));
  285. }
  286. static void __hbird_write_compare(unsigned long val)
  287. {
  288. unsigned long low = (val & 0xffffffffUL);
  289. unsigned long high = (val >> 32UL);
  290. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  291. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  292. "sub %0, 0x8, %0\n\t"
  293. "stxa %2, [%0] %4"
  294. : "=&r" (addr)
  295. : "0" (addr), "r" (low), "r" (high),
  296. "i" (ASI_PHYS_BYPASS_EC_E));
  297. }
  298. static void hbtick_disable_irq(void)
  299. {
  300. __hbird_write_compare(TICKCMP_IRQ_BIT);
  301. }
  302. static void hbtick_init_tick(void)
  303. {
  304. tick_disable_protection();
  305. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  306. * XXX into actually sending STICK interrupts. I think because
  307. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  308. * XXX {TICK + STICK} interrupt mux. -DaveM
  309. */
  310. __hbird_write_stick(__hbird_read_stick());
  311. hbtick_disable_irq();
  312. }
  313. static unsigned long long hbtick_get_tick(void)
  314. {
  315. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  316. }
  317. static unsigned long hbtick_add_tick(unsigned long adj)
  318. {
  319. unsigned long val;
  320. val = __hbird_read_stick() + adj;
  321. __hbird_write_stick(val);
  322. return val;
  323. }
  324. static int hbtick_add_compare(unsigned long adj)
  325. {
  326. unsigned long val = __hbird_read_stick();
  327. unsigned long val2;
  328. val &= ~TICKCMP_IRQ_BIT;
  329. val += adj;
  330. __hbird_write_compare(val);
  331. val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
  332. return ((long)(val2 - val)) > 0L;
  333. }
  334. static unsigned long hbtick_get_frequency(void)
  335. {
  336. struct device_node *dp = of_find_node_by_path("/");
  337. return of_getintprop_default(dp, "stick-frequency", 0);
  338. }
  339. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  340. .name = "hbtick",
  341. .init_tick = hbtick_init_tick,
  342. .disable_irq = hbtick_disable_irq,
  343. .get_tick = hbtick_get_tick,
  344. .add_tick = hbtick_add_tick,
  345. .add_compare = hbtick_add_compare,
  346. .get_frequency = hbtick_get_frequency,
  347. .softint_mask = 1UL << 0,
  348. };
  349. unsigned long cmos_regs;
  350. EXPORT_SYMBOL(cmos_regs);
  351. static struct resource rtc_cmos_resource;
  352. static struct platform_device rtc_cmos_device = {
  353. .name = "rtc_cmos",
  354. .id = -1,
  355. .resource = &rtc_cmos_resource,
  356. .num_resources = 1,
  357. };
  358. static int rtc_probe(struct platform_device *op)
  359. {
  360. struct resource *r;
  361. printk(KERN_INFO "%s: RTC regs at 0x%llx\n",
  362. op->dev.of_node->full_name, op->resource[0].start);
  363. /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
  364. * up a fake resource so that the probe works for all cases.
  365. * When the RTC is behind an ISA bus it will have IORESOURCE_IO
  366. * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
  367. */
  368. r = &rtc_cmos_resource;
  369. r->flags = IORESOURCE_IO;
  370. r->name = op->resource[0].name;
  371. r->start = op->resource[0].start;
  372. r->end = op->resource[0].end;
  373. cmos_regs = op->resource[0].start;
  374. return platform_device_register(&rtc_cmos_device);
  375. }
  376. static const struct of_device_id rtc_match[] = {
  377. {
  378. .name = "rtc",
  379. .compatible = "m5819",
  380. },
  381. {
  382. .name = "rtc",
  383. .compatible = "isa-m5819p",
  384. },
  385. {
  386. .name = "rtc",
  387. .compatible = "isa-m5823p",
  388. },
  389. {
  390. .name = "rtc",
  391. .compatible = "ds1287",
  392. },
  393. {},
  394. };
  395. static struct platform_driver rtc_driver = {
  396. .probe = rtc_probe,
  397. .driver = {
  398. .name = "rtc",
  399. .of_match_table = rtc_match,
  400. },
  401. };
  402. static struct platform_device rtc_bq4802_device = {
  403. .name = "rtc-bq4802",
  404. .id = -1,
  405. .num_resources = 1,
  406. };
  407. static int bq4802_probe(struct platform_device *op)
  408. {
  409. printk(KERN_INFO "%s: BQ4802 regs at 0x%llx\n",
  410. op->dev.of_node->full_name, op->resource[0].start);
  411. rtc_bq4802_device.resource = &op->resource[0];
  412. return platform_device_register(&rtc_bq4802_device);
  413. }
  414. static const struct of_device_id bq4802_match[] = {
  415. {
  416. .name = "rtc",
  417. .compatible = "bq4802",
  418. },
  419. {},
  420. };
  421. static struct platform_driver bq4802_driver = {
  422. .probe = bq4802_probe,
  423. .driver = {
  424. .name = "bq4802",
  425. .of_match_table = bq4802_match,
  426. },
  427. };
  428. static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
  429. {
  430. struct platform_device *pdev = to_platform_device(dev);
  431. void __iomem *regs = (void __iomem *) pdev->resource[0].start;
  432. return readb(regs + ofs);
  433. }
  434. static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
  435. {
  436. struct platform_device *pdev = to_platform_device(dev);
  437. void __iomem *regs = (void __iomem *) pdev->resource[0].start;
  438. writeb(val, regs + ofs);
  439. }
  440. static struct m48t59_plat_data m48t59_data = {
  441. .read_byte = mostek_read_byte,
  442. .write_byte = mostek_write_byte,
  443. };
  444. static struct platform_device m48t59_rtc = {
  445. .name = "rtc-m48t59",
  446. .id = 0,
  447. .num_resources = 1,
  448. .dev = {
  449. .platform_data = &m48t59_data,
  450. },
  451. };
  452. static int mostek_probe(struct platform_device *op)
  453. {
  454. struct device_node *dp = op->dev.of_node;
  455. /* On an Enterprise system there can be multiple mostek clocks.
  456. * We should only match the one that is on the central FHC bus.
  457. */
  458. if (!strcmp(dp->parent->name, "fhc") &&
  459. strcmp(dp->parent->parent->name, "central") != 0)
  460. return -ENODEV;
  461. printk(KERN_INFO "%s: Mostek regs at 0x%llx\n",
  462. dp->full_name, op->resource[0].start);
  463. m48t59_rtc.resource = &op->resource[0];
  464. return platform_device_register(&m48t59_rtc);
  465. }
  466. static const struct of_device_id mostek_match[] = {
  467. {
  468. .name = "eeprom",
  469. },
  470. {},
  471. };
  472. static struct platform_driver mostek_driver = {
  473. .probe = mostek_probe,
  474. .driver = {
  475. .name = "mostek",
  476. .of_match_table = mostek_match,
  477. },
  478. };
  479. static struct platform_device rtc_sun4v_device = {
  480. .name = "rtc-sun4v",
  481. .id = -1,
  482. };
  483. static struct platform_device rtc_starfire_device = {
  484. .name = "rtc-starfire",
  485. .id = -1,
  486. };
  487. static int __init clock_init(void)
  488. {
  489. if (this_is_starfire)
  490. return platform_device_register(&rtc_starfire_device);
  491. if (tlb_type == hypervisor)
  492. return platform_device_register(&rtc_sun4v_device);
  493. (void) platform_driver_register(&rtc_driver);
  494. (void) platform_driver_register(&mostek_driver);
  495. (void) platform_driver_register(&bq4802_driver);
  496. return 0;
  497. }
  498. /* Must be after subsys_initcall() so that busses are probed. Must
  499. * be before device_initcall() because things like the RTC driver
  500. * need to see the clock registers.
  501. */
  502. fs_initcall(clock_init);
  503. /* Return true if this is Hummingbird, aka Ultra-IIe */
  504. static bool is_hummingbird(void)
  505. {
  506. unsigned long ver, manuf, impl;
  507. __asm__ __volatile__ ("rdpr %%ver, %0"
  508. : "=&r" (ver));
  509. manuf = ((ver >> 48) & 0xffff);
  510. impl = ((ver >> 32) & 0xffff);
  511. return (manuf == 0x17 && impl == 0x13);
  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_operations.add_compare(delta) ? -ETIME : 0;
  561. }
  562. static int sparc64_timer_shutdown(struct clock_event_device *evt)
  563. {
  564. tick_operations.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_operations.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_operations.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_operations.get_tick();
  626. do {
  627. now = tick_operations.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 u64 clocksource_tick_read(struct clocksource *cs)
  637. {
  638. return tick_operations.get_tick();
  639. }
  640. static void init_tick_ops(struct sparc64_tick_ops *ops)
  641. {
  642. unsigned long freq, quotient, tick;
  643. freq = ops->get_frequency();
  644. quotient = clocksource_hz2mult(freq, SPARC64_NSEC_PER_CYC_SHIFT);
  645. tick = ops->get_tick();
  646. ops->offset = (tick * quotient) >> SPARC64_NSEC_PER_CYC_SHIFT;
  647. ops->ticks_per_nsec_quotient = quotient;
  648. ops->frequency = freq;
  649. tick_operations = *ops;
  650. }
  651. void __init time_init(void)
  652. {
  653. unsigned long freq;
  654. if (tlb_type == spitfire) {
  655. if (is_hummingbird())
  656. init_tick_ops(&hbtick_operations);
  657. else
  658. init_tick_ops(&tick_operations);
  659. } else {
  660. init_tick_ops(&stick_operations);
  661. }
  662. freq = tick_operations.frequency;
  663. tb_ticks_per_usec = freq / USEC_PER_SEC;
  664. tick_operations.ticks_per_nsec_quotient =
  665. clocksource_hz2mult(freq, SPARC64_NSEC_PER_CYC_SHIFT);
  666. tick_operations.offset = (tick_operations.get_tick()
  667. * tick_operations.ticks_per_nsec_quotient)
  668. >> SPARC64_NSEC_PER_CYC_SHIFT;
  669. clocksource_tick.name = tick_operations.name;
  670. clocksource_tick.read = clocksource_tick_read;
  671. clocksource_register_hz(&clocksource_tick, freq);
  672. printk("clocksource: mult[%x] shift[%d]\n",
  673. clocksource_tick.mult, clocksource_tick.shift);
  674. sparc64_clockevent.name = tick_operations.name;
  675. clockevents_calc_mult_shift(&sparc64_clockevent, freq, 4);
  676. sparc64_clockevent.max_delta_ns =
  677. clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
  678. sparc64_clockevent.max_delta_ticks = 0x7fffffffffffffffUL;
  679. sparc64_clockevent.min_delta_ns =
  680. clockevent_delta2ns(0xF, &sparc64_clockevent);
  681. sparc64_clockevent.min_delta_ticks = 0xF;
  682. printk("clockevent: mult[%x] shift[%d]\n",
  683. sparc64_clockevent.mult, sparc64_clockevent.shift);
  684. setup_sparc64_timer();
  685. }
  686. unsigned long long sched_clock(void)
  687. {
  688. unsigned long quotient = tick_operations.ticks_per_nsec_quotient;
  689. unsigned long offset = tick_operations.offset;
  690. unsigned long ticks = tick_operations.get_tick();
  691. return ((ticks * quotient) >> SPARC64_NSEC_PER_CYC_SHIFT) - offset;
  692. }
  693. int read_current_timer(unsigned long *timer_val)
  694. {
  695. *timer_val = tick_operations.get_tick();
  696. return 0;
  697. }