cevt-bcm1480.c 3.7 KB

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