bcsr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * bcsr.h -- Db1xxx/Pb1xxx Devboard CPLD registers ("BCSR") abstraction.
  3. *
  4. * All Alchemy development boards (except, of course, the weird PB1000)
  5. * have a few registers in a CPLD with standardised layout; they mostly
  6. * only differ in base address.
  7. * All registers are 16bits wide with 32bit spacing.
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/irqchip/chained_irq.h>
  11. #include <linux/module.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/irq.h>
  14. #include <asm/addrspace.h>
  15. #include <asm/io.h>
  16. #include <asm/mach-db1x00/bcsr.h>
  17. static struct bcsr_reg {
  18. void __iomem *raddr;
  19. spinlock_t lock;
  20. } bcsr_regs[BCSR_CNT];
  21. static void __iomem *bcsr_virt; /* KSEG1 addr of BCSR base */
  22. static int bcsr_csc_base; /* linux-irq of first cascaded irq */
  23. void __init bcsr_init(unsigned long bcsr1_phys, unsigned long bcsr2_phys)
  24. {
  25. int i;
  26. bcsr1_phys = KSEG1ADDR(CPHYSADDR(bcsr1_phys));
  27. bcsr2_phys = KSEG1ADDR(CPHYSADDR(bcsr2_phys));
  28. bcsr_virt = (void __iomem *)bcsr1_phys;
  29. for (i = 0; i < BCSR_CNT; i++) {
  30. if (i >= BCSR_HEXLEDS)
  31. bcsr_regs[i].raddr = (void __iomem *)bcsr2_phys +
  32. (0x04 * (i - BCSR_HEXLEDS));
  33. else
  34. bcsr_regs[i].raddr = (void __iomem *)bcsr1_phys +
  35. (0x04 * i);
  36. spin_lock_init(&bcsr_regs[i].lock);
  37. }
  38. }
  39. unsigned short bcsr_read(enum bcsr_id reg)
  40. {
  41. unsigned short r;
  42. unsigned long flags;
  43. spin_lock_irqsave(&bcsr_regs[reg].lock, flags);
  44. r = __raw_readw(bcsr_regs[reg].raddr);
  45. spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags);
  46. return r;
  47. }
  48. EXPORT_SYMBOL_GPL(bcsr_read);
  49. void bcsr_write(enum bcsr_id reg, unsigned short val)
  50. {
  51. unsigned long flags;
  52. spin_lock_irqsave(&bcsr_regs[reg].lock, flags);
  53. __raw_writew(val, bcsr_regs[reg].raddr);
  54. wmb();
  55. spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags);
  56. }
  57. EXPORT_SYMBOL_GPL(bcsr_write);
  58. void bcsr_mod(enum bcsr_id reg, unsigned short clr, unsigned short set)
  59. {
  60. unsigned short r;
  61. unsigned long flags;
  62. spin_lock_irqsave(&bcsr_regs[reg].lock, flags);
  63. r = __raw_readw(bcsr_regs[reg].raddr);
  64. r &= ~clr;
  65. r |= set;
  66. __raw_writew(r, bcsr_regs[reg].raddr);
  67. wmb();
  68. spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags);
  69. }
  70. EXPORT_SYMBOL_GPL(bcsr_mod);
  71. /*
  72. * DB1200/PB1200 CPLD IRQ muxer
  73. */
  74. static void bcsr_csc_handler(struct irq_desc *d)
  75. {
  76. unsigned short bisr = __raw_readw(bcsr_virt + BCSR_REG_INTSTAT);
  77. struct irq_chip *chip = irq_desc_get_chip(d);
  78. chained_irq_enter(chip, d);
  79. generic_handle_irq(bcsr_csc_base + __ffs(bisr));
  80. chained_irq_exit(chip, d);
  81. }
  82. static void bcsr_irq_mask(struct irq_data *d)
  83. {
  84. unsigned short v = 1 << (d->irq - bcsr_csc_base);
  85. __raw_writew(v, bcsr_virt + BCSR_REG_MASKCLR);
  86. wmb();
  87. }
  88. static void bcsr_irq_maskack(struct irq_data *d)
  89. {
  90. unsigned short v = 1 << (d->irq - bcsr_csc_base);
  91. __raw_writew(v, bcsr_virt + BCSR_REG_MASKCLR);
  92. __raw_writew(v, bcsr_virt + BCSR_REG_INTSTAT); /* ack */
  93. wmb();
  94. }
  95. static void bcsr_irq_unmask(struct irq_data *d)
  96. {
  97. unsigned short v = 1 << (d->irq - bcsr_csc_base);
  98. __raw_writew(v, bcsr_virt + BCSR_REG_MASKSET);
  99. wmb();
  100. }
  101. static struct irq_chip bcsr_irq_type = {
  102. .name = "CPLD",
  103. .irq_mask = bcsr_irq_mask,
  104. .irq_mask_ack = bcsr_irq_maskack,
  105. .irq_unmask = bcsr_irq_unmask,
  106. };
  107. void __init bcsr_init_irq(int csc_start, int csc_end, int hook_irq)
  108. {
  109. unsigned int irq;
  110. /* mask & enable & ack all */
  111. __raw_writew(0xffff, bcsr_virt + BCSR_REG_MASKCLR);
  112. __raw_writew(0xffff, bcsr_virt + BCSR_REG_INTSET);
  113. __raw_writew(0xffff, bcsr_virt + BCSR_REG_INTSTAT);
  114. wmb();
  115. bcsr_csc_base = csc_start;
  116. for (irq = csc_start; irq <= csc_end; irq++)
  117. irq_set_chip_and_handler_name(irq, &bcsr_irq_type,
  118. handle_level_irq, "level");
  119. irq_set_chained_handler(hook_irq, bcsr_csc_handler);
  120. }