numachip.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. *
  3. * Copyright (C) 2015 Numascale AS. All rights reserved.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/clockchips.h>
  16. #include <asm/irq.h>
  17. #include <asm/numachip/numachip.h>
  18. #include <asm/numachip/numachip_csr.h>
  19. static DEFINE_PER_CPU(struct clock_event_device, numachip2_ced);
  20. static cycles_t numachip2_timer_read(struct clocksource *cs)
  21. {
  22. return numachip2_read64_lcsr(NUMACHIP2_TIMER_NOW);
  23. }
  24. static struct clocksource numachip2_clocksource = {
  25. .name = "numachip2",
  26. .rating = 295,
  27. .read = numachip2_timer_read,
  28. .mask = CLOCKSOURCE_MASK(64),
  29. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  30. .mult = 1,
  31. .shift = 0,
  32. };
  33. static int numachip2_set_next_event(unsigned long delta, struct clock_event_device *ced)
  34. {
  35. numachip2_write64_lcsr(NUMACHIP2_TIMER_DEADLINE + numachip2_timer(),
  36. delta);
  37. return 0;
  38. }
  39. static struct clock_event_device numachip2_clockevent = {
  40. .name = "numachip2",
  41. .rating = 400,
  42. .set_next_event = numachip2_set_next_event,
  43. .features = CLOCK_EVT_FEAT_ONESHOT,
  44. .mult = 1,
  45. .shift = 0,
  46. .min_delta_ns = 1250,
  47. .max_delta_ns = LONG_MAX,
  48. };
  49. static void numachip_timer_interrupt(void)
  50. {
  51. struct clock_event_device *ced = this_cpu_ptr(&numachip2_ced);
  52. ced->event_handler(ced);
  53. }
  54. static __init void numachip_timer_each(struct work_struct *work)
  55. {
  56. unsigned local_apicid = __this_cpu_read(x86_cpu_to_apicid) & 0xff;
  57. struct clock_event_device *ced = this_cpu_ptr(&numachip2_ced);
  58. /* Setup IPI vector to local core and relative timing mode */
  59. numachip2_write64_lcsr(NUMACHIP2_TIMER_INT + numachip2_timer(),
  60. (3 << 22) | (X86_PLATFORM_IPI_VECTOR << 14) |
  61. (local_apicid << 6));
  62. *ced = numachip2_clockevent;
  63. ced->cpumask = cpumask_of(smp_processor_id());
  64. clockevents_register_device(ced);
  65. }
  66. static int __init numachip_timer_init(void)
  67. {
  68. if (numachip_system != 2)
  69. return -ENODEV;
  70. /* Reset timer */
  71. numachip2_write64_lcsr(NUMACHIP2_TIMER_RESET, 0);
  72. clocksource_register_hz(&numachip2_clocksource, NSEC_PER_SEC);
  73. /* Setup per-cpu clockevents */
  74. x86_platform_ipi_callback = numachip_timer_interrupt;
  75. schedule_on_each_cpu(&numachip_timer_each);
  76. return 0;
  77. }
  78. arch_initcall(numachip_timer_init);