time.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /* time.c: UltraSparc timer and TOD clock support.
  2. *
  3. * Copyright (C) 1997, 2008 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  5. *
  6. * Based largely on code which is:
  7. *
  8. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/kernel.h>
  15. #include <linux/param.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/time.h>
  20. #include <linux/timex.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/mc146818rtc.h>
  24. #include <linux/delay.h>
  25. #include <linux/profile.h>
  26. #include <linux/bcd.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/percpu.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/rtc.h>
  32. #include <linux/rtc/m48t59.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/clockchips.h>
  35. #include <linux/clocksource.h>
  36. #include <linux/of_device.h>
  37. #include <linux/platform_device.h>
  38. #include <asm/oplib.h>
  39. #include <asm/timer.h>
  40. #include <asm/irq.h>
  41. #include <asm/io.h>
  42. #include <asm/prom.h>
  43. #include <asm/starfire.h>
  44. #include <asm/smp.h>
  45. #include <asm/sections.h>
  46. #include <asm/cpudata.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/irq_regs.h>
  49. #include "entry.h"
  50. DEFINE_SPINLOCK(rtc_lock);
  51. #define TICK_PRIV_BIT (1UL << 63)
  52. #define TICKCMP_IRQ_BIT (1UL << 63)
  53. #ifdef CONFIG_SMP
  54. unsigned long profile_pc(struct pt_regs *regs)
  55. {
  56. unsigned long pc = instruction_pointer(regs);
  57. if (in_lock_functions(pc))
  58. return regs->u_regs[UREG_RETPC];
  59. return pc;
  60. }
  61. EXPORT_SYMBOL(profile_pc);
  62. #endif
  63. static void tick_disable_protection(void)
  64. {
  65. /* Set things up so user can access tick register for profiling
  66. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  67. * read back of %tick after writing it.
  68. */
  69. __asm__ __volatile__(
  70. " ba,pt %%xcc, 1f\n"
  71. " nop\n"
  72. " .align 64\n"
  73. "1: rd %%tick, %%g2\n"
  74. " add %%g2, 6, %%g2\n"
  75. " andn %%g2, %0, %%g2\n"
  76. " wrpr %%g2, 0, %%tick\n"
  77. " rdpr %%tick, %%g0"
  78. : /* no outputs */
  79. : "r" (TICK_PRIV_BIT)
  80. : "g2");
  81. }
  82. static void tick_disable_irq(void)
  83. {
  84. __asm__ __volatile__(
  85. " ba,pt %%xcc, 1f\n"
  86. " nop\n"
  87. " .align 64\n"
  88. "1: wr %0, 0x0, %%tick_cmpr\n"
  89. " rd %%tick_cmpr, %%g0"
  90. : /* no outputs */
  91. : "r" (TICKCMP_IRQ_BIT));
  92. }
  93. static void tick_init_tick(void)
  94. {
  95. tick_disable_protection();
  96. tick_disable_irq();
  97. }
  98. static unsigned long tick_get_tick(void)
  99. {
  100. unsigned long ret;
  101. __asm__ __volatile__("rd %%tick, %0\n\t"
  102. "mov %0, %0"
  103. : "=r" (ret));
  104. return ret & ~TICK_PRIV_BIT;
  105. }
  106. static int tick_add_compare(unsigned long adj)
  107. {
  108. unsigned long orig_tick, new_tick, new_compare;
  109. __asm__ __volatile__("rd %%tick, %0"
  110. : "=r" (orig_tick));
  111. orig_tick &= ~TICKCMP_IRQ_BIT;
  112. /* Workaround for Spitfire Errata (#54 I think??), I discovered
  113. * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
  114. * number 103640.
  115. *
  116. * On Blackbird writes to %tick_cmpr can fail, the
  117. * workaround seems to be to execute the wr instruction
  118. * at the start of an I-cache line, and perform a dummy
  119. * read back from %tick_cmpr right after writing to it. -DaveM
  120. */
  121. __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
  122. " add %1, %2, %0\n\t"
  123. ".align 64\n"
  124. "1:\n\t"
  125. "wr %0, 0, %%tick_cmpr\n\t"
  126. "rd %%tick_cmpr, %%g0\n\t"
  127. : "=r" (new_compare)
  128. : "r" (orig_tick), "r" (adj));
  129. __asm__ __volatile__("rd %%tick, %0"
  130. : "=r" (new_tick));
  131. new_tick &= ~TICKCMP_IRQ_BIT;
  132. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  133. }
  134. static unsigned long tick_add_tick(unsigned long adj)
  135. {
  136. unsigned long new_tick;
  137. /* Also need to handle Blackbird bug here too. */
  138. __asm__ __volatile__("rd %%tick, %0\n\t"
  139. "add %0, %1, %0\n\t"
  140. "wrpr %0, 0, %%tick\n\t"
  141. : "=&r" (new_tick)
  142. : "r" (adj));
  143. return new_tick;
  144. }
  145. static struct sparc64_tick_ops tick_operations __read_mostly = {
  146. .name = "tick",
  147. .init_tick = tick_init_tick,
  148. .disable_irq = tick_disable_irq,
  149. .get_tick = tick_get_tick,
  150. .add_tick = tick_add_tick,
  151. .add_compare = tick_add_compare,
  152. .softint_mask = 1UL << 0,
  153. };
  154. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  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 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 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. int update_persistent_clock(struct timespec now)
  334. {
  335. struct rtc_device *rtc = rtc_class_open("rtc0");
  336. if (rtc)
  337. return rtc_set_mmss(rtc, now.tv_sec);
  338. return -1;
  339. }
  340. unsigned long cmos_regs;
  341. EXPORT_SYMBOL(cmos_regs);
  342. struct resource rtc_cmos_resource;
  343. static struct platform_device rtc_cmos_device = {
  344. .name = "rtc_cmos",
  345. .id = -1,
  346. .resource = &rtc_cmos_resource,
  347. .num_resources = 1,
  348. };
  349. static int __devinit rtc_probe(struct of_device *op, const struct of_device_id *match)
  350. {
  351. struct resource *r;
  352. printk(KERN_INFO "%s: RTC regs at 0x%lx\n",
  353. op->node->full_name, op->resource[0].start);
  354. /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
  355. * up a fake resource so that the probe works for all cases.
  356. * When the RTC is behind an ISA bus it will have IORESOURCE_IO
  357. * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
  358. */
  359. r = &rtc_cmos_resource;
  360. r->flags = IORESOURCE_IO;
  361. r->name = op->resource[0].name;
  362. r->start = op->resource[0].start;
  363. r->end = op->resource[0].end;
  364. cmos_regs = op->resource[0].start;
  365. return platform_device_register(&rtc_cmos_device);
  366. }
  367. static struct of_device_id __initdata rtc_match[] = {
  368. {
  369. .name = "rtc",
  370. .compatible = "m5819",
  371. },
  372. {
  373. .name = "rtc",
  374. .compatible = "isa-m5819p",
  375. },
  376. {
  377. .name = "rtc",
  378. .compatible = "isa-m5823p",
  379. },
  380. {
  381. .name = "rtc",
  382. .compatible = "ds1287",
  383. },
  384. {},
  385. };
  386. static struct of_platform_driver rtc_driver = {
  387. .match_table = rtc_match,
  388. .probe = rtc_probe,
  389. .driver = {
  390. .name = "rtc",
  391. },
  392. };
  393. static struct platform_device rtc_bq4802_device = {
  394. .name = "rtc-bq4802",
  395. .id = -1,
  396. .num_resources = 1,
  397. };
  398. static int __devinit bq4802_probe(struct of_device *op, const struct of_device_id *match)
  399. {
  400. printk(KERN_INFO "%s: BQ4802 regs at 0x%lx\n",
  401. op->node->full_name, op->resource[0].start);
  402. rtc_bq4802_device.resource = &op->resource[0];
  403. return platform_device_register(&rtc_bq4802_device);
  404. }
  405. static struct of_device_id __initdata bq4802_match[] = {
  406. {
  407. .name = "rtc",
  408. .compatible = "bq4802",
  409. },
  410. };
  411. static struct of_platform_driver bq4802_driver = {
  412. .match_table = bq4802_match,
  413. .probe = bq4802_probe,
  414. .driver = {
  415. .name = "bq4802",
  416. },
  417. };
  418. static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
  419. {
  420. struct platform_device *pdev = to_platform_device(dev);
  421. void __iomem *regs;
  422. unsigned char val;
  423. regs = (void __iomem *) pdev->resource[0].start;
  424. val = readb(regs + ofs);
  425. /* the year 0 is 1968 */
  426. if (ofs == M48T59_YEAR) {
  427. val += 0x68;
  428. if ((val & 0xf) > 9)
  429. val += 6;
  430. }
  431. return val;
  432. }
  433. static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
  434. {
  435. struct platform_device *pdev = to_platform_device(dev);
  436. void __iomem *regs;
  437. regs = (void __iomem *) pdev->resource[0].start;
  438. if (ofs == M48T59_YEAR) {
  439. if (val < 0x68)
  440. val += 0x32;
  441. else
  442. val -= 0x68;
  443. if ((val & 0xf) > 9)
  444. val += 6;
  445. if ((val & 0xf0) > 0x9A)
  446. val += 0x60;
  447. }
  448. writeb(val, regs + ofs);
  449. }
  450. static struct m48t59_plat_data m48t59_data = {
  451. .read_byte = mostek_read_byte,
  452. .write_byte = mostek_write_byte,
  453. };
  454. static struct platform_device m48t59_rtc = {
  455. .name = "rtc-m48t59",
  456. .id = 0,
  457. .num_resources = 1,
  458. .dev = {
  459. .platform_data = &m48t59_data,
  460. },
  461. };
  462. static int __devinit mostek_probe(struct of_device *op, const struct of_device_id *match)
  463. {
  464. struct device_node *dp = op->node;
  465. /* On an Enterprise system there can be multiple mostek clocks.
  466. * We should only match the one that is on the central FHC bus.
  467. */
  468. if (!strcmp(dp->parent->name, "fhc") &&
  469. strcmp(dp->parent->parent->name, "central") != 0)
  470. return -ENODEV;
  471. printk(KERN_INFO "%s: Mostek regs at 0x%lx\n",
  472. dp->full_name, op->resource[0].start);
  473. m48t59_rtc.resource = &op->resource[0];
  474. return platform_device_register(&m48t59_rtc);
  475. }
  476. static struct of_device_id __initdata mostek_match[] = {
  477. {
  478. .name = "eeprom",
  479. },
  480. {},
  481. };
  482. static struct of_platform_driver mostek_driver = {
  483. .match_table = mostek_match,
  484. .probe = mostek_probe,
  485. .driver = {
  486. .name = "mostek",
  487. },
  488. };
  489. static struct platform_device rtc_sun4v_device = {
  490. .name = "rtc-sun4v",
  491. .id = -1,
  492. };
  493. static struct platform_device rtc_starfire_device = {
  494. .name = "rtc-starfire",
  495. .id = -1,
  496. };
  497. static int __init clock_init(void)
  498. {
  499. if (this_is_starfire)
  500. return platform_device_register(&rtc_starfire_device);
  501. if (tlb_type == hypervisor)
  502. return platform_device_register(&rtc_sun4v_device);
  503. (void) of_register_driver(&rtc_driver, &of_platform_bus_type);
  504. (void) of_register_driver(&mostek_driver, &of_platform_bus_type);
  505. (void) of_register_driver(&bq4802_driver, &of_platform_bus_type);
  506. return 0;
  507. }
  508. /* Must be after subsys_initcall() so that busses are probed. Must
  509. * be before device_initcall() because things like the RTC driver
  510. * need to see the clock registers.
  511. */
  512. fs_initcall(clock_init);
  513. /* This is gets the master TICK_INT timer going. */
  514. static unsigned long sparc64_init_timers(void)
  515. {
  516. struct device_node *dp;
  517. unsigned long clock;
  518. dp = of_find_node_by_path("/");
  519. if (tlb_type == spitfire) {
  520. unsigned long ver, manuf, impl;
  521. __asm__ __volatile__ ("rdpr %%ver, %0"
  522. : "=&r" (ver));
  523. manuf = ((ver >> 48) & 0xffff);
  524. impl = ((ver >> 32) & 0xffff);
  525. if (manuf == 0x17 && impl == 0x13) {
  526. /* Hummingbird, aka Ultra-IIe */
  527. tick_ops = &hbtick_operations;
  528. clock = of_getintprop_default(dp, "stick-frequency", 0);
  529. } else {
  530. tick_ops = &tick_operations;
  531. clock = local_cpu_data().clock_tick;
  532. }
  533. } else {
  534. tick_ops = &stick_operations;
  535. clock = of_getintprop_default(dp, "stick-frequency", 0);
  536. }
  537. return clock;
  538. }
  539. struct freq_table {
  540. unsigned long clock_tick_ref;
  541. unsigned int ref_freq;
  542. };
  543. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  544. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  545. {
  546. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  547. if (ft->clock_tick_ref)
  548. return ft->clock_tick_ref;
  549. return cpu_data(cpu).clock_tick;
  550. }
  551. #ifdef CONFIG_CPU_FREQ
  552. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  553. void *data)
  554. {
  555. struct cpufreq_freqs *freq = data;
  556. unsigned int cpu = freq->cpu;
  557. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  558. if (!ft->ref_freq) {
  559. ft->ref_freq = freq->old;
  560. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  561. }
  562. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  563. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  564. (val == CPUFREQ_RESUMECHANGE)) {
  565. cpu_data(cpu).clock_tick =
  566. cpufreq_scale(ft->clock_tick_ref,
  567. ft->ref_freq,
  568. freq->new);
  569. }
  570. return 0;
  571. }
  572. static struct notifier_block sparc64_cpufreq_notifier_block = {
  573. .notifier_call = sparc64_cpufreq_notifier
  574. };
  575. static int __init register_sparc64_cpufreq_notifier(void)
  576. {
  577. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  578. CPUFREQ_TRANSITION_NOTIFIER);
  579. return 0;
  580. }
  581. core_initcall(register_sparc64_cpufreq_notifier);
  582. #endif /* CONFIG_CPU_FREQ */
  583. static int sparc64_next_event(unsigned long delta,
  584. struct clock_event_device *evt)
  585. {
  586. return tick_ops->add_compare(delta) ? -ETIME : 0;
  587. }
  588. static void sparc64_timer_setup(enum clock_event_mode mode,
  589. struct clock_event_device *evt)
  590. {
  591. switch (mode) {
  592. case CLOCK_EVT_MODE_ONESHOT:
  593. case CLOCK_EVT_MODE_RESUME:
  594. break;
  595. case CLOCK_EVT_MODE_SHUTDOWN:
  596. tick_ops->disable_irq();
  597. break;
  598. case CLOCK_EVT_MODE_PERIODIC:
  599. case CLOCK_EVT_MODE_UNUSED:
  600. WARN_ON(1);
  601. break;
  602. };
  603. }
  604. static struct clock_event_device sparc64_clockevent = {
  605. .features = CLOCK_EVT_FEAT_ONESHOT,
  606. .set_mode = sparc64_timer_setup,
  607. .set_next_event = sparc64_next_event,
  608. .rating = 100,
  609. .shift = 30,
  610. .irq = -1,
  611. };
  612. static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
  613. void timer_interrupt(int irq, struct pt_regs *regs)
  614. {
  615. struct pt_regs *old_regs = set_irq_regs(regs);
  616. unsigned long tick_mask = tick_ops->softint_mask;
  617. int cpu = smp_processor_id();
  618. struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
  619. clear_softint(tick_mask);
  620. irq_enter();
  621. kstat_this_cpu.irqs[0]++;
  622. if (unlikely(!evt->event_handler)) {
  623. printk(KERN_WARNING
  624. "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
  625. } else
  626. evt->event_handler(evt);
  627. irq_exit();
  628. set_irq_regs(old_regs);
  629. }
  630. void __devinit setup_sparc64_timer(void)
  631. {
  632. struct clock_event_device *sevt;
  633. unsigned long pstate;
  634. /* Guarantee that the following sequences execute
  635. * uninterrupted.
  636. */
  637. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  638. "wrpr %0, %1, %%pstate"
  639. : "=r" (pstate)
  640. : "i" (PSTATE_IE));
  641. tick_ops->init_tick();
  642. /* Restore PSTATE_IE. */
  643. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  644. : /* no outputs */
  645. : "r" (pstate));
  646. sevt = &__get_cpu_var(sparc64_events);
  647. memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
  648. sevt->cpumask = cpumask_of_cpu(smp_processor_id());
  649. clockevents_register_device(sevt);
  650. }
  651. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  652. static struct clocksource clocksource_tick = {
  653. .rating = 100,
  654. .mask = CLOCKSOURCE_MASK(64),
  655. .shift = 16,
  656. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  657. };
  658. static void __init setup_clockevent_multiplier(unsigned long hz)
  659. {
  660. unsigned long mult, shift = 32;
  661. while (1) {
  662. mult = div_sc(hz, NSEC_PER_SEC, shift);
  663. if (mult && (mult >> 32UL) == 0UL)
  664. break;
  665. shift--;
  666. }
  667. sparc64_clockevent.shift = shift;
  668. sparc64_clockevent.mult = mult;
  669. }
  670. static unsigned long tb_ticks_per_usec __read_mostly;
  671. void __delay(unsigned long loops)
  672. {
  673. unsigned long bclock, now;
  674. bclock = tick_ops->get_tick();
  675. do {
  676. now = tick_ops->get_tick();
  677. } while ((now-bclock) < loops);
  678. }
  679. EXPORT_SYMBOL(__delay);
  680. void udelay(unsigned long usecs)
  681. {
  682. __delay(tb_ticks_per_usec * usecs);
  683. }
  684. EXPORT_SYMBOL(udelay);
  685. void __init time_init(void)
  686. {
  687. unsigned long clock = sparc64_init_timers();
  688. tb_ticks_per_usec = clock / USEC_PER_SEC;
  689. timer_ticks_per_nsec_quotient =
  690. clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
  691. clocksource_tick.name = tick_ops->name;
  692. clocksource_tick.mult =
  693. clocksource_hz2mult(clock,
  694. clocksource_tick.shift);
  695. clocksource_tick.read = tick_ops->get_tick;
  696. printk("clocksource: mult[%x] shift[%d]\n",
  697. clocksource_tick.mult, clocksource_tick.shift);
  698. clocksource_register(&clocksource_tick);
  699. sparc64_clockevent.name = tick_ops->name;
  700. setup_clockevent_multiplier(clock);
  701. sparc64_clockevent.max_delta_ns =
  702. clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
  703. sparc64_clockevent.min_delta_ns =
  704. clockevent_delta2ns(0xF, &sparc64_clockevent);
  705. printk("clockevent: mult[%lx] shift[%d]\n",
  706. sparc64_clockevent.mult, sparc64_clockevent.shift);
  707. setup_sparc64_timer();
  708. }
  709. unsigned long long sched_clock(void)
  710. {
  711. unsigned long ticks = tick_ops->get_tick();
  712. return (ticks * timer_ticks_per_nsec_quotient)
  713. >> SPARC64_NSEC_PER_CYC_SHIFT;
  714. }
  715. int __devinit read_current_timer(unsigned long *timer_val)
  716. {
  717. *timer_val = tick_ops->get_tick();
  718. return 0;
  719. }