mmio.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #include "mt76.h"
  6. #include "trace.h"
  7. static u32 mt76_mmio_rr(struct mt76_dev *dev, u32 offset)
  8. {
  9. u32 val;
  10. val = readl(dev->mmio.regs + offset);
  11. trace_reg_rr(dev, offset, val);
  12. return val;
  13. }
  14. static void mt76_mmio_wr(struct mt76_dev *dev, u32 offset, u32 val)
  15. {
  16. trace_reg_wr(dev, offset, val);
  17. writel(val, dev->mmio.regs + offset);
  18. }
  19. static u32 mt76_mmio_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val)
  20. {
  21. val |= mt76_mmio_rr(dev, offset) & ~mask;
  22. mt76_mmio_wr(dev, offset, val);
  23. return val;
  24. }
  25. static void mt76_mmio_write_copy(struct mt76_dev *dev, u32 offset,
  26. const void *data, int len)
  27. {
  28. __iowrite32_copy(dev->mmio.regs + offset, data, DIV_ROUND_UP(len, 4));
  29. }
  30. static void mt76_mmio_read_copy(struct mt76_dev *dev, u32 offset,
  31. void *data, int len)
  32. {
  33. __ioread32_copy(data, dev->mmio.regs + offset, DIV_ROUND_UP(len, 4));
  34. }
  35. static int mt76_mmio_wr_rp(struct mt76_dev *dev, u32 base,
  36. const struct mt76_reg_pair *data, int len)
  37. {
  38. while (len > 0) {
  39. mt76_mmio_wr(dev, data->reg, data->value);
  40. data++;
  41. len--;
  42. }
  43. return 0;
  44. }
  45. static int mt76_mmio_rd_rp(struct mt76_dev *dev, u32 base,
  46. struct mt76_reg_pair *data, int len)
  47. {
  48. while (len > 0) {
  49. data->value = mt76_mmio_rr(dev, data->reg);
  50. data++;
  51. len--;
  52. }
  53. return 0;
  54. }
  55. void mt76_set_irq_mask(struct mt76_dev *dev, u32 addr,
  56. u32 clear, u32 set)
  57. {
  58. unsigned long flags;
  59. spin_lock_irqsave(&dev->mmio.irq_lock, flags);
  60. dev->mmio.irqmask &= ~clear;
  61. dev->mmio.irqmask |= set;
  62. if (addr)
  63. mt76_mmio_wr(dev, addr, dev->mmio.irqmask);
  64. spin_unlock_irqrestore(&dev->mmio.irq_lock, flags);
  65. }
  66. EXPORT_SYMBOL_GPL(mt76_set_irq_mask);
  67. void mt76_mmio_init(struct mt76_dev *dev, void __iomem *regs)
  68. {
  69. static const struct mt76_bus_ops mt76_mmio_ops = {
  70. .rr = mt76_mmio_rr,
  71. .rmw = mt76_mmio_rmw,
  72. .wr = mt76_mmio_wr,
  73. .write_copy = mt76_mmio_write_copy,
  74. .read_copy = mt76_mmio_read_copy,
  75. .wr_rp = mt76_mmio_wr_rp,
  76. .rd_rp = mt76_mmio_rd_rp,
  77. .type = MT76_BUS_MMIO,
  78. };
  79. dev->bus = &mt76_mmio_ops;
  80. dev->mmio.regs = regs;
  81. spin_lock_init(&dev->mmio.irq_lock);
  82. }
  83. EXPORT_SYMBOL_GPL(mt76_mmio_init);