time_64.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* time.c: UltraSparc timer and TOD clock support.
  3. *
  4. * Copyright (C) 1997, 2008 David S. Miller (davem@davemloft.net)
  5. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  6. *
  7. * Based largely on code which is:
  8. *
  9. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/export.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/param.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/time.h>
  20. #include <linux/timex.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/mc146818rtc.h>
  24. #include <linux/delay.h>
  25. #include <linux/profile.h>
  26. #include <linux/bcd.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/percpu.h>
  30. #include <linux/rtc/m48t59.h>
  31. #include <linux/kernel_stat.h>
  32. #include <linux/clockchips.h>
  33. #include <linux/clocksource.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/sched/clock.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 <asm/cacheflush.h>
  49. #include "entry.h"
  50. #include "kernel.h"
  51. DEFINE_SPINLOCK(rtc_lock);
  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. /* Searches for cpu clock frequency with given cpuid in OpenBoot tree */
  145. static unsigned long cpuid_to_freq(phandle node, int cpuid)
  146. {
  147. bool is_cpu_node = false;
  148. unsigned long freq = 0;
  149. char type[128];
  150. if (!node)
  151. return freq;
  152. if (prom_getproperty(node, "device_type", type, sizeof(type)) != -1)
  153. is_cpu_node = (strcmp(type, "cpu") == 0);
  154. /* try upa-portid then cpuid to get cpuid, see prom_64.c */
  155. if (is_cpu_node && (prom_getint(node, "upa-portid") == cpuid ||
  156. prom_getint(node, "cpuid") == cpuid))
  157. freq = prom_getintdefault(node, "clock-frequency", 0);
  158. if (!freq)
  159. freq = cpuid_to_freq(prom_getchild(node), cpuid);
  160. if (!freq)
  161. freq = cpuid_to_freq(prom_getsibling(node), cpuid);
  162. return freq;
  163. }
  164. static unsigned long tick_get_frequency(void)
  165. {
  166. return cpuid_to_freq(prom_root_node, hard_smp_processor_id());
  167. }
  168. static struct sparc64_tick_ops tick_operations __cacheline_aligned = {
  169. .name = "tick",
  170. .init_tick = tick_init_tick,
  171. .disable_irq = tick_disable_irq,
  172. .get_tick = tick_get_tick,
  173. .add_tick = tick_add_tick,
  174. .add_compare = tick_add_compare,
  175. .get_frequency = tick_get_frequency,
  176. .softint_mask = 1UL << 0,
  177. };
  178. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  179. EXPORT_SYMBOL(tick_ops);
  180. static void stick_disable_irq(void)
  181. {
  182. __asm__ __volatile__(
  183. "wr %0, 0x0, %%asr25"
  184. : /* no outputs */
  185. : "r" (TICKCMP_IRQ_BIT));
  186. }
  187. static void stick_init_tick(void)
  188. {
  189. /* Writes to the %tick and %stick register are not
  190. * allowed on sun4v. The Hypervisor controls that
  191. * bit, per-strand.
  192. */
  193. if (tlb_type != hypervisor) {
  194. tick_disable_protection();
  195. tick_disable_irq();
  196. /* Let the user get at STICK too. */
  197. __asm__ __volatile__(
  198. " rd %%asr24, %%g2\n"
  199. " andn %%g2, %0, %%g2\n"
  200. " wr %%g2, 0, %%asr24"
  201. : /* no outputs */
  202. : "r" (TICK_PRIV_BIT)
  203. : "g1", "g2");
  204. }
  205. stick_disable_irq();
  206. }
  207. static unsigned long long stick_get_tick(void)
  208. {
  209. unsigned long ret;
  210. __asm__ __volatile__("rd %%asr24, %0"
  211. : "=r" (ret));
  212. return ret & ~TICK_PRIV_BIT;
  213. }
  214. static unsigned long stick_add_tick(unsigned long adj)
  215. {
  216. unsigned long new_tick;
  217. __asm__ __volatile__("rd %%asr24, %0\n\t"
  218. "add %0, %1, %0\n\t"
  219. "wr %0, 0, %%asr24\n\t"
  220. : "=&r" (new_tick)
  221. : "r" (adj));
  222. return new_tick;
  223. }
  224. static int stick_add_compare(unsigned long adj)
  225. {
  226. unsigned long orig_tick, new_tick;
  227. __asm__ __volatile__("rd %%asr24, %0"
  228. : "=r" (orig_tick));
  229. orig_tick &= ~TICKCMP_IRQ_BIT;
  230. __asm__ __volatile__("wr %0, 0, %%asr25"
  231. : /* no outputs */
  232. : "r" (orig_tick + adj));
  233. __asm__ __volatile__("rd %%asr24, %0"
  234. : "=r" (new_tick));
  235. new_tick &= ~TICKCMP_IRQ_BIT;
  236. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  237. }
  238. static unsigned long stick_get_frequency(void)
  239. {
  240. return prom_getintdefault(prom_root_node, "stick-frequency", 0);
  241. }
  242. static struct sparc64_tick_ops stick_operations __read_mostly = {
  243. .name = "stick",
  244. .init_tick = stick_init_tick,
  245. .disable_irq = stick_disable_irq,
  246. .get_tick = stick_get_tick,
  247. .add_tick = stick_add_tick,
  248. .add_compare = stick_add_compare,
  249. .get_frequency = stick_get_frequency,
  250. .softint_mask = 1UL << 16,
  251. };
  252. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  253. * in I/O space. There are two 64-bit registers each, the
  254. * first holds the low 32-bits of the value and the second holds
  255. * the high 32-bits.
  256. *
  257. * Since STICK is constantly updating, we have to access it carefully.
  258. *
  259. * The sequence we use to read is:
  260. * 1) read high
  261. * 2) read low
  262. * 3) read high again, if it rolled re-read both low and high again.
  263. *
  264. * Writing STICK safely is also tricky:
  265. * 1) write low to zero
  266. * 2) write high
  267. * 3) write low
  268. */
  269. static unsigned long __hbird_read_stick(void)
  270. {
  271. unsigned long ret, tmp1, tmp2, tmp3;
  272. unsigned long addr = HBIRD_STICK_ADDR+8;
  273. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  274. "1:\n\t"
  275. "sub %1, 0x8, %1\n\t"
  276. "ldxa [%1] %5, %3\n\t"
  277. "add %1, 0x8, %1\n\t"
  278. "ldxa [%1] %5, %4\n\t"
  279. "cmp %4, %2\n\t"
  280. "bne,a,pn %%xcc, 1b\n\t"
  281. " mov %4, %2\n\t"
  282. "sllx %4, 32, %4\n\t"
  283. "or %3, %4, %0\n\t"
  284. : "=&r" (ret), "=&r" (addr),
  285. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  286. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  287. return ret;
  288. }
  289. static void __hbird_write_stick(unsigned long val)
  290. {
  291. unsigned long low = (val & 0xffffffffUL);
  292. unsigned long high = (val >> 32UL);
  293. unsigned long addr = HBIRD_STICK_ADDR;
  294. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  295. "add %0, 0x8, %0\n\t"
  296. "stxa %3, [%0] %4\n\t"
  297. "sub %0, 0x8, %0\n\t"
  298. "stxa %2, [%0] %4"
  299. : "=&r" (addr)
  300. : "0" (addr), "r" (low), "r" (high),
  301. "i" (ASI_PHYS_BYPASS_EC_E));
  302. }
  303. static void __hbird_write_compare(unsigned long val)
  304. {
  305. unsigned long low = (val & 0xffffffffUL);
  306. unsigned long high = (val >> 32UL);
  307. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  308. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  309. "sub %0, 0x8, %0\n\t"
  310. "stxa %2, [%0] %4"
  311. : "=&r" (addr)
  312. : "0" (addr), "r" (low), "r" (high),
  313. "i" (ASI_PHYS_BYPASS_EC_E));
  314. }
  315. static void hbtick_disable_irq(void)
  316. {
  317. __hbird_write_compare(TICKCMP_IRQ_BIT);
  318. }
  319. static void hbtick_init_tick(void)
  320. {
  321. tick_disable_protection();
  322. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  323. * XXX into actually sending STICK interrupts. I think because
  324. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  325. * XXX {TICK + STICK} interrupt mux. -DaveM
  326. */
  327. __hbird_write_stick(__hbird_read_stick());
  328. hbtick_disable_irq();
  329. }
  330. static unsigned long long hbtick_get_tick(void)
  331. {
  332. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  333. }
  334. static unsigned long hbtick_add_tick(unsigned long adj)
  335. {
  336. unsigned long val;
  337. val = __hbird_read_stick() + adj;
  338. __hbird_write_stick(val);
  339. return val;
  340. }
  341. static int hbtick_add_compare(unsigned long adj)
  342. {
  343. unsigned long val = __hbird_read_stick();
  344. unsigned long val2;
  345. val &= ~TICKCMP_IRQ_BIT;
  346. val += adj;
  347. __hbird_write_compare(val);
  348. val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
  349. return ((long)(val2 - val)) > 0L;
  350. }
  351. static unsigned long hbtick_get_frequency(void)
  352. {
  353. return prom_getintdefault(prom_root_node, "stick-frequency", 0);
  354. }
  355. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  356. .name = "hbtick",
  357. .init_tick = hbtick_init_tick,
  358. .disable_irq = hbtick_disable_irq,
  359. .get_tick = hbtick_get_tick,
  360. .add_tick = hbtick_add_tick,
  361. .add_compare = hbtick_add_compare,
  362. .get_frequency = hbtick_get_frequency,
  363. .softint_mask = 1UL << 0,
  364. };
  365. unsigned long cmos_regs;
  366. EXPORT_SYMBOL(cmos_regs);
  367. static struct resource rtc_cmos_resource;
  368. static struct platform_device rtc_cmos_device = {
  369. .name = "rtc_cmos",
  370. .id = -1,
  371. .resource = &rtc_cmos_resource,
  372. .num_resources = 1,
  373. };
  374. static int rtc_probe(struct platform_device *op)
  375. {
  376. struct resource *r;
  377. printk(KERN_INFO "%pOF: RTC regs at 0x%llx\n",
  378. op->dev.of_node, op->resource[0].start);
  379. /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
  380. * up a fake resource so that the probe works for all cases.
  381. * When the RTC is behind an ISA bus it will have IORESOURCE_IO
  382. * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
  383. */
  384. r = &rtc_cmos_resource;
  385. r->flags = IORESOURCE_IO;
  386. r->name = op->resource[0].name;
  387. r->start = op->resource[0].start;
  388. r->end = op->resource[0].end;
  389. cmos_regs = op->resource[0].start;
  390. return platform_device_register(&rtc_cmos_device);
  391. }
  392. static const struct of_device_id rtc_match[] = {
  393. {
  394. .name = "rtc",
  395. .compatible = "m5819",
  396. },
  397. {
  398. .name = "rtc",
  399. .compatible = "isa-m5819p",
  400. },
  401. {
  402. .name = "rtc",
  403. .compatible = "isa-m5823p",
  404. },
  405. {
  406. .name = "rtc",
  407. .compatible = "ds1287",
  408. },
  409. {},
  410. };
  411. static struct platform_driver rtc_driver = {
  412. .probe = rtc_probe,
  413. .driver = {
  414. .name = "rtc",
  415. .of_match_table = rtc_match,
  416. },
  417. };
  418. static struct platform_device rtc_bq4802_device = {
  419. .name = "rtc-bq4802",
  420. .id = -1,
  421. .num_resources = 1,
  422. };
  423. static int bq4802_probe(struct platform_device *op)
  424. {
  425. printk(KERN_INFO "%pOF: BQ4802 regs at 0x%llx\n",
  426. op->dev.of_node, op->resource[0].start);
  427. rtc_bq4802_device.resource = &op->resource[0];
  428. return platform_device_register(&rtc_bq4802_device);
  429. }
  430. static const struct of_device_id bq4802_match[] = {
  431. {
  432. .name = "rtc",
  433. .compatible = "bq4802",
  434. },
  435. {},
  436. };
  437. static struct platform_driver bq4802_driver = {
  438. .probe = bq4802_probe,
  439. .driver = {
  440. .name = "bq4802",
  441. .of_match_table = bq4802_match,
  442. },
  443. };
  444. static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
  445. {
  446. struct platform_device *pdev = to_platform_device(dev);
  447. void __iomem *regs = (void __iomem *) pdev->resource[0].start;
  448. return readb(regs + ofs);
  449. }
  450. static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
  451. {
  452. struct platform_device *pdev = to_platform_device(dev);
  453. void __iomem *regs = (void __iomem *) pdev->resource[0].start;
  454. writeb(val, regs + ofs);
  455. }
  456. static struct m48t59_plat_data m48t59_data = {
  457. .read_byte = mostek_read_byte,
  458. .write_byte = mostek_write_byte,
  459. };
  460. static struct platform_device m48t59_rtc = {
  461. .name = "rtc-m48t59",
  462. .id = 0,
  463. .num_resources = 1,
  464. .dev = {
  465. .platform_data = &m48t59_data,
  466. },
  467. };
  468. static int mostek_probe(struct platform_device *op)
  469. {
  470. struct device_node *dp = op->dev.of_node;
  471. /* On an Enterprise system there can be multiple mostek clocks.
  472. * We should only match the one that is on the central FHC bus.
  473. */
  474. if (of_node_name_eq(dp->parent, "fhc") &&
  475. !of_node_name_eq(dp->parent->parent, "central"))
  476. return -ENODEV;
  477. printk(KERN_INFO "%pOF: Mostek regs at 0x%llx\n",
  478. dp, op->resource[0].start);
  479. m48t59_rtc.resource = &op->resource[0];
  480. return platform_device_register(&m48t59_rtc);
  481. }
  482. static const struct of_device_id mostek_match[] = {
  483. {
  484. .name = "eeprom",
  485. },
  486. {},
  487. };
  488. static struct platform_driver mostek_driver = {
  489. .probe = mostek_probe,
  490. .driver = {
  491. .name = "mostek",
  492. .of_match_table = mostek_match,
  493. },
  494. };
  495. static struct platform_device rtc_sun4v_device = {
  496. .name = "rtc-sun4v",
  497. .id = -1,
  498. };
  499. static struct platform_device rtc_starfire_device = {
  500. .name = "rtc-starfire",
  501. .id = -1,
  502. };
  503. static int __init clock_init(void)
  504. {
  505. if (this_is_starfire)
  506. return platform_device_register(&rtc_starfire_device);
  507. if (tlb_type == hypervisor)
  508. return platform_device_register(&rtc_sun4v_device);
  509. (void) platform_driver_register(&rtc_driver);
  510. (void) platform_driver_register(&mostek_driver);
  511. (void) platform_driver_register(&bq4802_driver);
  512. return 0;
  513. }
  514. /* Must be after subsys_initcall() so that busses are probed. Must
  515. * be before device_initcall() because things like the RTC driver
  516. * need to see the clock registers.
  517. */
  518. fs_initcall(clock_init);
  519. /* Return true if this is Hummingbird, aka Ultra-IIe */
  520. static bool is_hummingbird(void)
  521. {
  522. unsigned long ver, manuf, impl;
  523. __asm__ __volatile__ ("rdpr %%ver, %0"
  524. : "=&r" (ver));
  525. manuf = ((ver >> 48) & 0xffff);
  526. impl = ((ver >> 32) & 0xffff);
  527. return (manuf == 0x17 && impl == 0x13);
  528. }
  529. struct freq_table {
  530. unsigned long clock_tick_ref;
  531. unsigned int ref_freq;
  532. };
  533. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  534. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  535. {
  536. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  537. if (ft->clock_tick_ref)
  538. return ft->clock_tick_ref;
  539. return cpu_data(cpu).clock_tick;
  540. }
  541. EXPORT_SYMBOL(sparc64_get_clock_tick);
  542. #ifdef CONFIG_CPU_FREQ
  543. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  544. void *data)
  545. {
  546. struct cpufreq_freqs *freq = data;
  547. unsigned int cpu;
  548. struct freq_table *ft;
  549. for_each_cpu(cpu, freq->policy->cpus) {
  550. ft = &per_cpu(sparc64_freq_table, cpu);
  551. if (!ft->ref_freq) {
  552. ft->ref_freq = freq->old;
  553. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  554. }
  555. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  556. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
  557. cpu_data(cpu).clock_tick =
  558. cpufreq_scale(ft->clock_tick_ref, ft->ref_freq,
  559. freq->new);
  560. }
  561. }
  562. return 0;
  563. }
  564. static struct notifier_block sparc64_cpufreq_notifier_block = {
  565. .notifier_call = sparc64_cpufreq_notifier
  566. };
  567. static int __init register_sparc64_cpufreq_notifier(void)
  568. {
  569. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  570. CPUFREQ_TRANSITION_NOTIFIER);
  571. return 0;
  572. }
  573. core_initcall(register_sparc64_cpufreq_notifier);
  574. #endif /* CONFIG_CPU_FREQ */
  575. static int sparc64_next_event(unsigned long delta,
  576. struct clock_event_device *evt)
  577. {
  578. return tick_operations.add_compare(delta) ? -ETIME : 0;
  579. }
  580. static int sparc64_timer_shutdown(struct clock_event_device *evt)
  581. {
  582. tick_operations.disable_irq();
  583. return 0;
  584. }
  585. static struct clock_event_device sparc64_clockevent = {
  586. .features = CLOCK_EVT_FEAT_ONESHOT,
  587. .set_state_shutdown = sparc64_timer_shutdown,
  588. .set_next_event = sparc64_next_event,
  589. .rating = 100,
  590. .shift = 30,
  591. .irq = -1,
  592. };
  593. static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
  594. void __irq_entry timer_interrupt(int irq, struct pt_regs *regs)
  595. {
  596. struct pt_regs *old_regs = set_irq_regs(regs);
  597. unsigned long tick_mask = tick_operations.softint_mask;
  598. int cpu = smp_processor_id();
  599. struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
  600. clear_softint(tick_mask);
  601. irq_enter();
  602. local_cpu_data().irq0_irqs++;
  603. kstat_incr_irq_this_cpu(0);
  604. if (unlikely(!evt->event_handler)) {
  605. printk(KERN_WARNING
  606. "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
  607. } else
  608. evt->event_handler(evt);
  609. irq_exit();
  610. set_irq_regs(old_regs);
  611. }
  612. void setup_sparc64_timer(void)
  613. {
  614. struct clock_event_device *sevt;
  615. unsigned long pstate;
  616. /* Guarantee that the following sequences execute
  617. * uninterrupted.
  618. */
  619. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  620. "wrpr %0, %1, %%pstate"
  621. : "=r" (pstate)
  622. : "i" (PSTATE_IE));
  623. tick_operations.init_tick();
  624. /* Restore PSTATE_IE. */
  625. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  626. : /* no outputs */
  627. : "r" (pstate));
  628. sevt = this_cpu_ptr(&sparc64_events);
  629. memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
  630. sevt->cpumask = cpumask_of(smp_processor_id());
  631. clockevents_register_device(sevt);
  632. }
  633. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  634. static struct clocksource clocksource_tick = {
  635. .rating = 100,
  636. .mask = CLOCKSOURCE_MASK(64),
  637. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  638. };
  639. static unsigned long tb_ticks_per_usec __read_mostly;
  640. void __delay(unsigned long loops)
  641. {
  642. unsigned long bclock = get_tick();
  643. while ((get_tick() - bclock) < loops)
  644. ;
  645. }
  646. EXPORT_SYMBOL(__delay);
  647. void udelay(unsigned long usecs)
  648. {
  649. __delay(tb_ticks_per_usec * usecs);
  650. }
  651. EXPORT_SYMBOL(udelay);
  652. static u64 clocksource_tick_read(struct clocksource *cs)
  653. {
  654. return get_tick();
  655. }
  656. static void __init get_tick_patch(void)
  657. {
  658. unsigned int *addr, *instr, i;
  659. struct get_tick_patch *p;
  660. if (tlb_type == spitfire && is_hummingbird())
  661. return;
  662. for (p = &__get_tick_patch; p < &__get_tick_patch_end; p++) {
  663. instr = (tlb_type == spitfire) ? p->tick : p->stick;
  664. addr = (unsigned int *)(unsigned long)p->addr;
  665. for (i = 0; i < GET_TICK_NINSTR; i++) {
  666. addr[i] = instr[i];
  667. /* ensure that address is modified before flush */
  668. wmb();
  669. flushi(&addr[i]);
  670. }
  671. }
  672. }
  673. static void __init init_tick_ops(struct sparc64_tick_ops *ops)
  674. {
  675. unsigned long freq, quotient, tick;
  676. freq = ops->get_frequency();
  677. quotient = clocksource_hz2mult(freq, SPARC64_NSEC_PER_CYC_SHIFT);
  678. tick = ops->get_tick();
  679. ops->offset = (tick * quotient) >> SPARC64_NSEC_PER_CYC_SHIFT;
  680. ops->ticks_per_nsec_quotient = quotient;
  681. ops->frequency = freq;
  682. tick_operations = *ops;
  683. get_tick_patch();
  684. }
  685. void __init time_init_early(void)
  686. {
  687. if (tlb_type == spitfire) {
  688. if (is_hummingbird()) {
  689. init_tick_ops(&hbtick_operations);
  690. clocksource_tick.archdata.vclock_mode = VCLOCK_NONE;
  691. } else {
  692. init_tick_ops(&tick_operations);
  693. clocksource_tick.archdata.vclock_mode = VCLOCK_TICK;
  694. }
  695. } else {
  696. init_tick_ops(&stick_operations);
  697. clocksource_tick.archdata.vclock_mode = VCLOCK_STICK;
  698. }
  699. }
  700. void __init time_init(void)
  701. {
  702. unsigned long freq;
  703. freq = tick_operations.frequency;
  704. tb_ticks_per_usec = freq / USEC_PER_SEC;
  705. clocksource_tick.name = tick_operations.name;
  706. clocksource_tick.read = clocksource_tick_read;
  707. clocksource_register_hz(&clocksource_tick, freq);
  708. printk("clocksource: mult[%x] shift[%d]\n",
  709. clocksource_tick.mult, clocksource_tick.shift);
  710. sparc64_clockevent.name = tick_operations.name;
  711. clockevents_calc_mult_shift(&sparc64_clockevent, freq, 4);
  712. sparc64_clockevent.max_delta_ns =
  713. clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
  714. sparc64_clockevent.max_delta_ticks = 0x7fffffffffffffffUL;
  715. sparc64_clockevent.min_delta_ns =
  716. clockevent_delta2ns(0xF, &sparc64_clockevent);
  717. sparc64_clockevent.min_delta_ticks = 0xF;
  718. printk("clockevent: mult[%x] shift[%d]\n",
  719. sparc64_clockevent.mult, sparc64_clockevent.shift);
  720. setup_sparc64_timer();
  721. }
  722. unsigned long long sched_clock(void)
  723. {
  724. unsigned long quotient = tick_operations.ticks_per_nsec_quotient;
  725. unsigned long offset = tick_operations.offset;
  726. /* Use barrier so the compiler emits the loads first and overlaps load
  727. * latency with reading tick, because reading %tick/%stick is a
  728. * post-sync instruction that will flush and restart subsequent
  729. * instructions after it commits.
  730. */
  731. barrier();
  732. return ((get_tick() * quotient) >> SPARC64_NSEC_PER_CYC_SHIFT) - offset;
  733. }
  734. int read_current_timer(unsigned long *timer_val)
  735. {
  736. *timer_val = get_tick();
  737. return 0;
  738. }