cevt-sb1250.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2000, 2001 Broadcom Corporation
  4. */
  5. #include <linux/clockchips.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/irq.h>
  8. #include <linux/percpu.h>
  9. #include <linux/smp.h>
  10. #include <asm/addrspace.h>
  11. #include <asm/io.h>
  12. #include <asm/time.h>
  13. #include <asm/sibyte/sb1250.h>
  14. #include <asm/sibyte/sb1250_regs.h>
  15. #include <asm/sibyte/sb1250_int.h>
  16. #include <asm/sibyte/sb1250_scd.h>
  17. #define IMR_IP2_VAL K_INT_MAP_I0
  18. #define IMR_IP3_VAL K_INT_MAP_I1
  19. #define IMR_IP4_VAL K_INT_MAP_I2
  20. /*
  21. * The general purpose timer ticks at 1MHz independent if
  22. * the rest of the system
  23. */
  24. static int sibyte_shutdown(struct clock_event_device *evt)
  25. {
  26. void __iomem *cfg;
  27. cfg = IOADDR(A_SCD_TIMER_REGISTER(smp_processor_id(), R_SCD_TIMER_CFG));
  28. /* Stop the timer until we actually program a shot */
  29. __raw_writeq(0, cfg);
  30. return 0;
  31. }
  32. static int sibyte_set_periodic(struct clock_event_device *evt)
  33. {
  34. unsigned int cpu = smp_processor_id();
  35. void __iomem *cfg, *init;
  36. cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG));
  37. init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT));
  38. __raw_writeq(0, cfg);
  39. __raw_writeq((V_SCD_TIMER_FREQ / HZ) - 1, init);
  40. __raw_writeq(M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS, cfg);
  41. return 0;
  42. }
  43. static int sibyte_next_event(unsigned long delta, struct clock_event_device *cd)
  44. {
  45. unsigned int cpu = smp_processor_id();
  46. void __iomem *cfg, *init;
  47. cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG));
  48. init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT));
  49. __raw_writeq(0, cfg);
  50. __raw_writeq(delta - 1, init);
  51. __raw_writeq(M_SCD_TIMER_ENABLE, cfg);
  52. return 0;
  53. }
  54. static irqreturn_t sibyte_counter_handler(int irq, void *dev_id)
  55. {
  56. unsigned int cpu = smp_processor_id();
  57. struct clock_event_device *cd = dev_id;
  58. void __iomem *cfg;
  59. unsigned long tmode;
  60. if (clockevent_state_periodic(cd))
  61. tmode = M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS;
  62. else
  63. tmode = 0;
  64. /* ACK interrupt */
  65. cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG));
  66. ____raw_writeq(tmode, cfg);
  67. cd->event_handler(cd);
  68. return IRQ_HANDLED;
  69. }
  70. static DEFINE_PER_CPU(struct clock_event_device, sibyte_hpt_clockevent);
  71. static DEFINE_PER_CPU(char [18], sibyte_hpt_name);
  72. void sb1250_clockevent_init(void)
  73. {
  74. unsigned int cpu = smp_processor_id();
  75. unsigned int irq = K_INT_TIMER_0 + cpu;
  76. struct clock_event_device *cd = &per_cpu(sibyte_hpt_clockevent, cpu);
  77. unsigned char *name = per_cpu(sibyte_hpt_name, cpu);
  78. unsigned long flags = IRQF_PERCPU | IRQF_TIMER;
  79. /* Only have 4 general purpose timers, and we use last one as hpt */
  80. BUG_ON(cpu > 2);
  81. sprintf(name, "sb1250-counter-%d", cpu);
  82. cd->name = name;
  83. cd->features = CLOCK_EVT_FEAT_PERIODIC |
  84. CLOCK_EVT_FEAT_ONESHOT;
  85. clockevent_set_clock(cd, V_SCD_TIMER_FREQ);
  86. cd->max_delta_ns = clockevent_delta2ns(0x7fffff, cd);
  87. cd->max_delta_ticks = 0x7fffff;
  88. cd->min_delta_ns = clockevent_delta2ns(2, cd);
  89. cd->min_delta_ticks = 2;
  90. cd->rating = 200;
  91. cd->irq = irq;
  92. cd->cpumask = cpumask_of(cpu);
  93. cd->set_next_event = sibyte_next_event;
  94. cd->set_state_shutdown = sibyte_shutdown;
  95. cd->set_state_periodic = sibyte_set_periodic;
  96. cd->set_state_oneshot = sibyte_shutdown;
  97. clockevents_register_device(cd);
  98. sb1250_mask_irq(cpu, irq);
  99. /*
  100. * Map the timer interrupt to IP[4] of this cpu
  101. */
  102. __raw_writeq(IMR_IP4_VAL,
  103. IOADDR(A_IMR_REGISTER(cpu, R_IMR_INTERRUPT_MAP_BASE) +
  104. (irq << 3)));
  105. sb1250_unmask_irq(cpu, irq);
  106. irq_set_affinity(irq, cpumask_of(cpu));
  107. if (request_irq(irq, sibyte_counter_handler, flags, name, cd))
  108. pr_err("Failed to request irq %d (%s)\n", irq, name);
  109. }