Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tpm: Remove read16/read32/write32 calls from tpm_tis_phy_ops

Only tpm_tis and tpm_tis_synquacer have a dedicated way to access
multiple bytes at once, every other driver will just fall back to
read_bytes/write_bytes. Therefore, remove the read16/read32/write32
calls and move their logic to read_bytes/write_bytes.

Suggested-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Johannes Holland <johannes.holland@infineon.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>

authored by

Johannes Holland and committed by
Jarkko Sakkinen
6422cbd3 d0dc1a71

+117 -160
+29 -36
drivers/char/tpm/tpm_tis.c
··· 153 153 #endif 154 154 155 155 static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len, 156 - u8 *result) 156 + u8 *result, enum tpm_tis_io_mode io_mode) 157 157 { 158 158 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 159 + __le16 result_le16; 160 + __le32 result_le32; 159 161 160 - while (len--) 161 - *result++ = ioread8(phy->iobase + addr); 162 + switch (io_mode) { 163 + case TPM_TIS_PHYS_8: 164 + while (len--) 165 + *result++ = ioread8(phy->iobase + addr); 166 + break; 167 + case TPM_TIS_PHYS_16: 168 + result_le16 = cpu_to_le16(ioread16(phy->iobase + addr)); 169 + memcpy(result, &result_le16, sizeof(u16)); 170 + break; 171 + case TPM_TIS_PHYS_32: 172 + result_le32 = cpu_to_le32(ioread32(phy->iobase + addr)); 173 + memcpy(result, &result_le32, sizeof(u32)); 174 + break; 175 + } 162 176 163 177 return 0; 164 178 } 165 179 166 180 static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len, 167 - const u8 *value) 181 + const u8 *value, enum tpm_tis_io_mode io_mode) 168 182 { 169 183 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 170 184 171 - while (len--) 172 - iowrite8(*value++, phy->iobase + addr); 173 - 174 - return 0; 175 - } 176 - 177 - static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result) 178 - { 179 - struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 180 - 181 - *result = ioread16(phy->iobase + addr); 182 - 183 - return 0; 184 - } 185 - 186 - static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result) 187 - { 188 - struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 189 - 190 - *result = ioread32(phy->iobase + addr); 191 - 192 - return 0; 193 - } 194 - 195 - static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value) 196 - { 197 - struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 198 - 199 - iowrite32(value, phy->iobase + addr); 185 + switch (io_mode) { 186 + case TPM_TIS_PHYS_8: 187 + while (len--) 188 + iowrite8(*value++, phy->iobase + addr); 189 + break; 190 + case TPM_TIS_PHYS_16: 191 + return -EINVAL; 192 + case TPM_TIS_PHYS_32: 193 + iowrite32(le32_to_cpu(*((__le32 *)value)), phy->iobase + addr); 194 + break; 195 + } 200 196 201 197 return 0; 202 198 } ··· 200 204 static const struct tpm_tis_phy_ops tpm_tcg = { 201 205 .read_bytes = tpm_tcg_read_bytes, 202 206 .write_bytes = tpm_tcg_write_bytes, 203 - .read16 = tpm_tcg_read16, 204 - .read32 = tpm_tcg_read32, 205 - .write32 = tpm_tcg_write32, 206 207 }; 207 208 208 209 static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
+46 -12
drivers/char/tpm/tpm_tis_core.h
··· 104 104 unsigned int timeout_max; /* usecs */ 105 105 }; 106 106 107 + /* 108 + * IO modes to indicate how many bytes should be read/written at once in the 109 + * tpm_tis_phy_ops read_bytes/write_bytes calls. Use TPM_TIS_PHYS_8 to 110 + * receive/transmit byte-wise, TPM_TIS_PHYS_16 for two bytes etc. 111 + */ 112 + enum tpm_tis_io_mode { 113 + TPM_TIS_PHYS_8, 114 + TPM_TIS_PHYS_16, 115 + TPM_TIS_PHYS_32, 116 + }; 117 + 107 118 struct tpm_tis_phy_ops { 119 + /* data is passed in little endian */ 108 120 int (*read_bytes)(struct tpm_tis_data *data, u32 addr, u16 len, 109 - u8 *result); 121 + u8 *result, enum tpm_tis_io_mode mode); 110 122 int (*write_bytes)(struct tpm_tis_data *data, u32 addr, u16 len, 111 - const u8 *value); 112 - int (*read16)(struct tpm_tis_data *data, u32 addr, u16 *result); 113 - int (*read32)(struct tpm_tis_data *data, u32 addr, u32 *result); 114 - int (*write32)(struct tpm_tis_data *data, u32 addr, u32 src); 123 + const u8 *value, enum tpm_tis_io_mode mode); 115 124 }; 116 125 117 126 static inline int tpm_tis_read_bytes(struct tpm_tis_data *data, u32 addr, 118 127 u16 len, u8 *result) 119 128 { 120 - return data->phy_ops->read_bytes(data, addr, len, result); 129 + return data->phy_ops->read_bytes(data, addr, len, result, 130 + TPM_TIS_PHYS_8); 121 131 } 122 132 123 133 static inline int tpm_tis_read8(struct tpm_tis_data *data, u32 addr, u8 *result) 124 134 { 125 - return data->phy_ops->read_bytes(data, addr, 1, result); 135 + return data->phy_ops->read_bytes(data, addr, 1, result, TPM_TIS_PHYS_8); 126 136 } 127 137 128 138 static inline int tpm_tis_read16(struct tpm_tis_data *data, u32 addr, 129 139 u16 *result) 130 140 { 131 - return data->phy_ops->read16(data, addr, result); 141 + __le16 result_le; 142 + int rc; 143 + 144 + rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), 145 + (u8 *)&result_le, TPM_TIS_PHYS_16); 146 + if (!rc) 147 + *result = le16_to_cpu(result_le); 148 + 149 + return rc; 132 150 } 133 151 134 152 static inline int tpm_tis_read32(struct tpm_tis_data *data, u32 addr, 135 153 u32 *result) 136 154 { 137 - return data->phy_ops->read32(data, addr, result); 155 + __le32 result_le; 156 + int rc; 157 + 158 + rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), 159 + (u8 *)&result_le, TPM_TIS_PHYS_32); 160 + if (!rc) 161 + *result = le32_to_cpu(result_le); 162 + 163 + return rc; 138 164 } 139 165 140 166 static inline int tpm_tis_write_bytes(struct tpm_tis_data *data, u32 addr, 141 167 u16 len, const u8 *value) 142 168 { 143 - return data->phy_ops->write_bytes(data, addr, len, value); 169 + return data->phy_ops->write_bytes(data, addr, len, value, 170 + TPM_TIS_PHYS_8); 144 171 } 145 172 146 173 static inline int tpm_tis_write8(struct tpm_tis_data *data, u32 addr, u8 value) 147 174 { 148 - return data->phy_ops->write_bytes(data, addr, 1, &value); 175 + return data->phy_ops->write_bytes(data, addr, 1, &value, 176 + TPM_TIS_PHYS_8); 149 177 } 150 178 151 179 static inline int tpm_tis_write32(struct tpm_tis_data *data, u32 addr, 152 180 u32 value) 153 181 { 154 - return data->phy_ops->write32(data, addr, value); 182 + __le32 value_le; 183 + int rc; 184 + 185 + value_le = cpu_to_le32(value); 186 + rc = data->phy_ops->write_bytes(data, addr, sizeof(u32), 187 + (u8 *)&value_le, TPM_TIS_PHYS_32); 188 + return rc; 155 189 } 156 190 157 191 static inline bool is_bsw(void)
-4
drivers/char/tpm/tpm_tis_spi.h
··· 31 31 extern int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len, 32 32 u8 *in, const u8 *out); 33 33 34 - extern int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result); 35 - extern int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result); 36 - extern int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value); 37 - 38 34 #ifdef CONFIG_TCG_TIS_SPI_CR50 39 35 extern int cr50_spi_probe(struct spi_device *spi); 40 36 #else
+2 -5
drivers/char/tpm/tpm_tis_spi_cr50.c
··· 222 222 } 223 223 224 224 static int tpm_tis_spi_cr50_read_bytes(struct tpm_tis_data *data, u32 addr, 225 - u16 len, u8 *result) 225 + u16 len, u8 *result, enum tpm_tis_io_mode io_mode) 226 226 { 227 227 return tpm_tis_spi_cr50_transfer(data, addr, len, result, NULL); 228 228 } 229 229 230 230 static int tpm_tis_spi_cr50_write_bytes(struct tpm_tis_data *data, u32 addr, 231 - u16 len, const u8 *value) 231 + u16 len, const u8 *value, enum tpm_tis_io_mode io_mode) 232 232 { 233 233 return tpm_tis_spi_cr50_transfer(data, addr, len, NULL, value); 234 234 } ··· 236 236 static const struct tpm_tis_phy_ops tpm_spi_cr50_phy_ops = { 237 237 .read_bytes = tpm_tis_spi_cr50_read_bytes, 238 238 .write_bytes = tpm_tis_spi_cr50_write_bytes, 239 - .read16 = tpm_tis_spi_read16, 240 - .read32 = tpm_tis_spi_read32, 241 - .write32 = tpm_tis_spi_write32, 242 239 }; 243 240 244 241 static void cr50_print_fw_version(struct tpm_tis_data *data)
+2 -43
drivers/char/tpm/tpm_tis_spi_main.c
··· 141 141 } 142 142 143 143 static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr, 144 - u16 len, u8 *result) 144 + u16 len, u8 *result, enum tpm_tis_io_mode io_mode) 145 145 { 146 146 return tpm_tis_spi_transfer(data, addr, len, result, NULL); 147 147 } 148 148 149 149 static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr, 150 - u16 len, const u8 *value) 150 + u16 len, const u8 *value, enum tpm_tis_io_mode io_mode) 151 151 { 152 152 return tpm_tis_spi_transfer(data, addr, len, NULL, value); 153 - } 154 - 155 - int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result) 156 - { 157 - __le16 result_le; 158 - int rc; 159 - 160 - rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), 161 - (u8 *)&result_le); 162 - if (!rc) 163 - *result = le16_to_cpu(result_le); 164 - 165 - return rc; 166 - } 167 - 168 - int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result) 169 - { 170 - __le32 result_le; 171 - int rc; 172 - 173 - rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), 174 - (u8 *)&result_le); 175 - if (!rc) 176 - *result = le32_to_cpu(result_le); 177 - 178 - return rc; 179 - } 180 - 181 - int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value) 182 - { 183 - __le32 value_le; 184 - int rc; 185 - 186 - value_le = cpu_to_le32(value); 187 - rc = data->phy_ops->write_bytes(data, addr, sizeof(u32), 188 - (u8 *)&value_le); 189 - 190 - return rc; 191 153 } 192 154 193 155 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy, ··· 167 205 static const struct tpm_tis_phy_ops tpm_spi_phy_ops = { 168 206 .read_bytes = tpm_tis_spi_read_bytes, 169 207 .write_bytes = tpm_tis_spi_write_bytes, 170 - .read16 = tpm_tis_spi_read16, 171 - .read32 = tpm_tis_spi_read32, 172 - .write32 = tpm_tis_spi_write32, 173 208 }; 174 209 175 210 static int tpm_tis_spi_probe(struct spi_device *dev)
+38 -60
drivers/char/tpm/tpm_tis_synquacer.c
··· 35 35 } 36 36 37 37 static int tpm_tis_synquacer_read_bytes(struct tpm_tis_data *data, u32 addr, 38 - u16 len, u8 *result) 38 + u16 len, u8 *result, 39 + enum tpm_tis_io_mode io_mode) 39 40 { 40 41 struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data); 41 - 42 - while (len--) 43 - *result++ = ioread8(phy->iobase + addr); 42 + switch (io_mode) { 43 + case TPM_TIS_PHYS_8: 44 + while (len--) 45 + *result++ = ioread8(phy->iobase + addr); 46 + break; 47 + case TPM_TIS_PHYS_16: 48 + result[1] = ioread8(phy->iobase + addr + 1); 49 + result[0] = ioread8(phy->iobase + addr); 50 + break; 51 + case TPM_TIS_PHYS_32: 52 + result[3] = ioread8(phy->iobase + addr + 3); 53 + result[2] = ioread8(phy->iobase + addr + 2); 54 + result[1] = ioread8(phy->iobase + addr + 1); 55 + result[0] = ioread8(phy->iobase + addr); 56 + break; 57 + } 44 58 45 59 return 0; 46 60 } 47 61 48 62 static int tpm_tis_synquacer_write_bytes(struct tpm_tis_data *data, u32 addr, 49 - u16 len, const u8 *value) 63 + u16 len, const u8 *value, 64 + enum tpm_tis_io_mode io_mode) 50 65 { 51 66 struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data); 52 - 53 - while (len--) 54 - iowrite8(*value++, phy->iobase + addr); 55 - 56 - return 0; 57 - } 58 - 59 - static int tpm_tis_synquacer_read16_bw(struct tpm_tis_data *data, 60 - u32 addr, u16 *result) 61 - { 62 - struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data); 63 - 64 - /* 65 - * Due to the limitation of SPI controller on SynQuacer, 66 - * 16/32 bits access must be done in byte-wise and descending order. 67 - */ 68 - *result = (ioread8(phy->iobase + addr + 1) << 8) | 69 - (ioread8(phy->iobase + addr)); 70 - 71 - return 0; 72 - } 73 - 74 - static int tpm_tis_synquacer_read32_bw(struct tpm_tis_data *data, 75 - u32 addr, u32 *result) 76 - { 77 - struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data); 78 - 79 - /* 80 - * Due to the limitation of SPI controller on SynQuacer, 81 - * 16/32 bits access must be done in byte-wise and descending order. 82 - */ 83 - *result = (ioread8(phy->iobase + addr + 3) << 24) | 84 - (ioread8(phy->iobase + addr + 2) << 16) | 85 - (ioread8(phy->iobase + addr + 1) << 8) | 86 - (ioread8(phy->iobase + addr)); 87 - 88 - return 0; 89 - } 90 - 91 - static int tpm_tis_synquacer_write32_bw(struct tpm_tis_data *data, 92 - u32 addr, u32 value) 93 - { 94 - struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data); 95 - 96 - /* 97 - * Due to the limitation of SPI controller on SynQuacer, 98 - * 16/32 bits access must be done in byte-wise and descending order. 99 - */ 100 - iowrite8(value >> 24, phy->iobase + addr + 3); 101 - iowrite8(value >> 16, phy->iobase + addr + 2); 102 - iowrite8(value >> 8, phy->iobase + addr + 1); 103 - iowrite8(value, phy->iobase + addr); 67 + switch (io_mode) { 68 + case TPM_TIS_PHYS_8: 69 + while (len--) 70 + iowrite8(*value++, phy->iobase + addr); 71 + break; 72 + case TPM_TIS_PHYS_16: 73 + return -EINVAL; 74 + case TPM_TIS_PHYS_32: 75 + /* 76 + * Due to the limitation of SPI controller on SynQuacer, 77 + * 16/32 bits access must be done in byte-wise and descending order. 78 + */ 79 + iowrite8(value[3], phy->iobase + addr + 3); 80 + iowrite8(value[2], phy->iobase + addr + 2); 81 + iowrite8(value[1], phy->iobase + addr + 1); 82 + iowrite8(value[0], phy->iobase + addr); 83 + break; 84 + } 104 85 105 86 return 0; 106 87 } ··· 89 108 static const struct tpm_tis_phy_ops tpm_tcg_bw = { 90 109 .read_bytes = tpm_tis_synquacer_read_bytes, 91 110 .write_bytes = tpm_tis_synquacer_write_bytes, 92 - .read16 = tpm_tis_synquacer_read16_bw, 93 - .read32 = tpm_tis_synquacer_read32_bw, 94 - .write32 = tpm_tis_synquacer_write32_bw, 95 111 }; 96 112 97 113 static int tpm_tis_synquacer_init(struct device *dev,