1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Common LiteX header providing 4 * helper functions for accessing CSRs. 5 * 6 * Implementation of the functions is provided by 7 * the LiteX SoC Controller driver. 8 * 9 * Copyright (C) 2019-2020 Antmicro <www.antmicro.com> 10 */ 11 12 #ifndef _LINUX_LITEX_H 13 #define _LINUX_LITEX_H 14 15 #include <linux/io.h> 16 #include <linux/types.h> 17 #include <linux/compiler_types.h> 18 19 /* 20 * The parameters below are true for LiteX SoCs configured for 8-bit CSR Bus, 21 * 32-bit aligned. 22 * 23 * Supporting other configurations will require extending the logic in this 24 * header and in the LiteX SoC controller driver. 25 */ 26 #define LITEX_REG_SIZE 0x4 27 #define LITEX_SUBREG_SIZE 0x1 28 #define LITEX_SUBREG_SIZE_BIT (LITEX_SUBREG_SIZE * 8) 29 30 #define WRITE_LITEX_SUBREGISTER(val, base_offset, subreg_id) \ 31 writel((u32 __force)cpu_to_le32(val), base_offset + (LITEX_REG_SIZE * subreg_id)) 32 33 #define READ_LITEX_SUBREGISTER(base_offset, subreg_id) \ 34 le32_to_cpu((__le32 __force)readl(base_offset + (LITEX_REG_SIZE * subreg_id))) 35 36 void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val); 37 38 unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz); 39 40 static inline void litex_write8(void __iomem *reg, u8 val) 41 { 42 WRITE_LITEX_SUBREGISTER(val, reg, 0); 43 } 44 45 static inline void litex_write16(void __iomem *reg, u16 val) 46 { 47 WRITE_LITEX_SUBREGISTER(val >> 8, reg, 0); 48 WRITE_LITEX_SUBREGISTER(val, reg, 1); 49 } 50 51 static inline void litex_write32(void __iomem *reg, u32 val) 52 { 53 WRITE_LITEX_SUBREGISTER(val >> 24, reg, 0); 54 WRITE_LITEX_SUBREGISTER(val >> 16, reg, 1); 55 WRITE_LITEX_SUBREGISTER(val >> 8, reg, 2); 56 WRITE_LITEX_SUBREGISTER(val, reg, 3); 57 } 58 59 static inline void litex_write64(void __iomem *reg, u64 val) 60 { 61 WRITE_LITEX_SUBREGISTER(val >> 56, reg, 0); 62 WRITE_LITEX_SUBREGISTER(val >> 48, reg, 1); 63 WRITE_LITEX_SUBREGISTER(val >> 40, reg, 2); 64 WRITE_LITEX_SUBREGISTER(val >> 32, reg, 3); 65 WRITE_LITEX_SUBREGISTER(val >> 24, reg, 4); 66 WRITE_LITEX_SUBREGISTER(val >> 16, reg, 5); 67 WRITE_LITEX_SUBREGISTER(val >> 8, reg, 6); 68 WRITE_LITEX_SUBREGISTER(val, reg, 7); 69 } 70 71 static inline u8 litex_read8(void __iomem *reg) 72 { 73 return READ_LITEX_SUBREGISTER(reg, 0); 74 } 75 76 static inline u16 litex_read16(void __iomem *reg) 77 { 78 return (READ_LITEX_SUBREGISTER(reg, 0) << 8) 79 | (READ_LITEX_SUBREGISTER(reg, 1)); 80 } 81 82 static inline u32 litex_read32(void __iomem *reg) 83 { 84 return (READ_LITEX_SUBREGISTER(reg, 0) << 24) 85 | (READ_LITEX_SUBREGISTER(reg, 1) << 16) 86 | (READ_LITEX_SUBREGISTER(reg, 2) << 8) 87 | (READ_LITEX_SUBREGISTER(reg, 3)); 88 } 89 90 static inline u64 litex_read64(void __iomem *reg) 91 { 92 return ((u64)READ_LITEX_SUBREGISTER(reg, 0) << 56) 93 | ((u64)READ_LITEX_SUBREGISTER(reg, 1) << 48) 94 | ((u64)READ_LITEX_SUBREGISTER(reg, 2) << 40) 95 | ((u64)READ_LITEX_SUBREGISTER(reg, 3) << 32) 96 | ((u64)READ_LITEX_SUBREGISTER(reg, 4) << 24) 97 | ((u64)READ_LITEX_SUBREGISTER(reg, 5) << 16) 98 | ((u64)READ_LITEX_SUBREGISTER(reg, 6) << 8) 99 | ((u64)READ_LITEX_SUBREGISTER(reg, 7)); 100 } 101 102 #endif /* _LINUX_LITEX_H */ 103