litex.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common LiteX header providing
  4. * helper functions for accessing CSRs.
  5. *
  6. * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
  7. */
  8. #ifndef _LINUX_LITEX_H
  9. #define _LINUX_LITEX_H
  10. #include <linux/io.h>
  11. static inline void _write_litex_subregister(u32 val, void __iomem *addr)
  12. {
  13. writel((u32 __force)cpu_to_le32(val), addr);
  14. }
  15. static inline u32 _read_litex_subregister(void __iomem *addr)
  16. {
  17. return le32_to_cpu((__le32 __force)readl(addr));
  18. }
  19. /*
  20. * LiteX SoC Generator, depending on the configuration, can split a single
  21. * logical CSR (Control&Status Register) into a series of consecutive physical
  22. * registers.
  23. *
  24. * For example, in the configuration with 8-bit CSR Bus, a 32-bit aligned,
  25. * 32-bit wide logical CSR will be laid out as four 32-bit physical
  26. * subregisters, each one containing one byte of meaningful data.
  27. *
  28. * For Linux support, upstream LiteX enforces a 32-bit wide CSR bus, which
  29. * means that only larger-than-32-bit CSRs will be split across multiple
  30. * subregisters (e.g., a 64-bit CSR will be spread across two consecutive
  31. * 32-bit subregisters).
  32. *
  33. * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
  34. */
  35. static inline void litex_write8(void __iomem *reg, u8 val)
  36. {
  37. _write_litex_subregister(val, reg);
  38. }
  39. static inline void litex_write16(void __iomem *reg, u16 val)
  40. {
  41. _write_litex_subregister(val, reg);
  42. }
  43. static inline void litex_write32(void __iomem *reg, u32 val)
  44. {
  45. _write_litex_subregister(val, reg);
  46. }
  47. static inline void litex_write64(void __iomem *reg, u64 val)
  48. {
  49. _write_litex_subregister(val >> 32, reg);
  50. _write_litex_subregister(val, reg + 4);
  51. }
  52. static inline u8 litex_read8(void __iomem *reg)
  53. {
  54. return _read_litex_subregister(reg);
  55. }
  56. static inline u16 litex_read16(void __iomem *reg)
  57. {
  58. return _read_litex_subregister(reg);
  59. }
  60. static inline u32 litex_read32(void __iomem *reg)
  61. {
  62. return _read_litex_subregister(reg);
  63. }
  64. static inline u64 litex_read64(void __iomem *reg)
  65. {
  66. return ((u64)_read_litex_subregister(reg) << 32) |
  67. _read_litex_subregister(reg + 4);
  68. }
  69. #endif /* _LINUX_LITEX_H */