bcsr.c 3.5 KB

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