1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/iopoll.h> 4 #include <linux/module.h> 5 #include <linux/nvmem-provider.h> 6 #include <linux/of.h> 7 #include <linux/platform_device.h> 8 9 #define OTP_OTP_PWR_DN(t) (t + 0x00) 10 #define OTP_OTP_PWR_DN_OTP_PWRDN_N BIT(0) 11 #define OTP_OTP_ADDR_HI(t) (t + 0x04) 12 #define OTP_OTP_ADDR_LO(t) (t + 0x08) 13 #define OTP_OTP_PRGM_DATA(t) (t + 0x10) 14 #define OTP_OTP_PRGM_MODE(t) (t + 0x14) 15 #define OTP_OTP_PRGM_MODE_OTP_PGM_MODE_BYTE BIT(0) 16 #define OTP_OTP_RD_DATA(t) (t + 0x18) 17 #define OTP_OTP_FUNC_CMD(t) (t + 0x20) 18 #define OTP_OTP_FUNC_CMD_OTP_PROGRAM BIT(1) 19 #define OTP_OTP_FUNC_CMD_OTP_READ BIT(0) 20 #define OTP_OTP_CMD_GO(t) (t + 0x28) 21 #define OTP_OTP_CMD_GO_OTP_GO BIT(0) 22 #define OTP_OTP_PASS_FAIL(t) (t + 0x2c) 23 #define OTP_OTP_PASS_FAIL_OTP_READ_PROHIBITED BIT(3) 24 #define OTP_OTP_PASS_FAIL_OTP_WRITE_PROHIBITED BIT(2) 25 #define OTP_OTP_PASS_FAIL_OTP_FAIL BIT(0) 26 #define OTP_OTP_STATUS(t) (t + 0x30) 27 #define OTP_OTP_STATUS_OTP_CPUMPEN BIT(1) 28 #define OTP_OTP_STATUS_OTP_BUSY BIT(0) 29 30 #define OTP_SLEEP_US 10 31 #define OTP_TIMEOUT_US 500000 32 33 struct lan9662_otp { 34 struct device *dev; 35 void __iomem *base; 36 }; 37 38 static int lan9662_otp_wait_flag_clear(void __iomem *reg, u32 flag) 39 { 40 u32 val; 41 42 return readl_poll_timeout(reg, val, !(val & flag), 43 OTP_SLEEP_US, OTP_TIMEOUT_US); 44 } 45 46 static int lan9662_otp_power(struct lan9662_otp *otp, bool up) 47 { 48 void __iomem *pwrdn = OTP_OTP_PWR_DN(otp->base); 49 50 if (up) { 51 writel(readl(pwrdn) & ~OTP_OTP_PWR_DN_OTP_PWRDN_N, pwrdn); 52 if (lan9662_otp_wait_flag_clear(OTP_OTP_STATUS(otp->base), 53 OTP_OTP_STATUS_OTP_CPUMPEN)) 54 return -ETIMEDOUT; 55 } else { 56 writel(readl(pwrdn) | OTP_OTP_PWR_DN_OTP_PWRDN_N, pwrdn); 57 } 58 59 return 0; 60 } 61 62 static int lan9662_otp_execute(struct lan9662_otp *otp) 63 { 64 if (lan9662_otp_wait_flag_clear(OTP_OTP_CMD_GO(otp->base), 65 OTP_OTP_CMD_GO_OTP_GO)) 66 return -ETIMEDOUT; 67 68 if (lan9662_otp_wait_flag_clear(OTP_OTP_STATUS(otp->base), 69 OTP_OTP_STATUS_OTP_BUSY)) 70 return -ETIMEDOUT; 71 72 return 0; 73 } 74 75 static void lan9662_otp_set_address(struct lan9662_otp *otp, u32 offset) 76 { 77 writel(0xff & (offset >> 8), OTP_OTP_ADDR_HI(otp->base)); 78 writel(0xff & offset, OTP_OTP_ADDR_LO(otp->base)); 79 } 80 81 static int lan9662_otp_read_byte(struct lan9662_otp *otp, u32 offset, u8 *dst) 82 { 83 u32 pass; 84 int rc; 85 86 lan9662_otp_set_address(otp, offset); 87 writel(OTP_OTP_FUNC_CMD_OTP_READ, OTP_OTP_FUNC_CMD(otp->base)); 88 writel(OTP_OTP_CMD_GO_OTP_GO, OTP_OTP_CMD_GO(otp->base)); 89 rc = lan9662_otp_execute(otp); 90 if (!rc) { 91 pass = readl(OTP_OTP_PASS_FAIL(otp->base)); 92 if (pass & OTP_OTP_PASS_FAIL_OTP_READ_PROHIBITED) 93 return -EACCES; 94 *dst = (u8) readl(OTP_OTP_RD_DATA(otp->base)); 95 } 96 return rc; 97 } 98 99 static int lan9662_otp_write_byte(struct lan9662_otp *otp, u32 offset, u8 data) 100 { 101 u32 pass; 102 int rc; 103 104 lan9662_otp_set_address(otp, offset); 105 writel(OTP_OTP_PRGM_MODE_OTP_PGM_MODE_BYTE, OTP_OTP_PRGM_MODE(otp->base)); 106 writel(data, OTP_OTP_PRGM_DATA(otp->base)); 107 writel(OTP_OTP_FUNC_CMD_OTP_PROGRAM, OTP_OTP_FUNC_CMD(otp->base)); 108 writel(OTP_OTP_CMD_GO_OTP_GO, OTP_OTP_CMD_GO(otp->base)); 109 110 rc = lan9662_otp_execute(otp); 111 if (!rc) { 112 pass = readl(OTP_OTP_PASS_FAIL(otp->base)); 113 if (pass & OTP_OTP_PASS_FAIL_OTP_WRITE_PROHIBITED) 114 return -EACCES; 115 if (pass & OTP_OTP_PASS_FAIL_OTP_FAIL) 116 return -EIO; 117 } 118 return rc; 119 } 120 121 static int lan9662_otp_read(void *context, unsigned int offset, 122 void *_val, size_t bytes) 123 { 124 struct lan9662_otp *otp = context; 125 u8 *val = _val; 126 uint8_t data; 127 int i, rc = 0; 128 129 lan9662_otp_power(otp, true); 130 for (i = 0; i < bytes; i++) { 131 rc = lan9662_otp_read_byte(otp, offset + i, &data); 132 if (rc < 0) 133 break; 134 *val++ = data; 135 } 136 lan9662_otp_power(otp, false); 137 138 return rc; 139 } 140 141 static int lan9662_otp_write(void *context, unsigned int offset, 142 void *_val, size_t bytes) 143 { 144 struct lan9662_otp *otp = context; 145 u8 *val = _val; 146 u8 data, newdata; 147 int i, rc = 0; 148 149 lan9662_otp_power(otp, true); 150 for (i = 0; i < bytes; i++) { 151 /* Skip zero bytes */ 152 if (val[i]) { 153 rc = lan9662_otp_read_byte(otp, offset + i, &data); 154 if (rc < 0) 155 break; 156 157 newdata = data | val[i]; 158 if (newdata == data) 159 continue; 160 161 rc = lan9662_otp_write_byte(otp, offset + i, 162 newdata); 163 if (rc < 0) 164 break; 165 } 166 } 167 lan9662_otp_power(otp, false); 168 169 return rc; 170 } 171 172 static struct nvmem_config otp_config = { 173 .name = "lan9662-otp", 174 .stride = 1, 175 .word_size = 1, 176 .reg_read = lan9662_otp_read, 177 .reg_write = lan9662_otp_write, 178 }; 179 180 static int lan9662_otp_probe(struct platform_device *pdev) 181 { 182 struct device *dev = &pdev->dev; 183 struct nvmem_device *nvmem; 184 struct lan9662_otp *otp; 185 186 otp = devm_kzalloc(&pdev->dev, sizeof(*otp), GFP_KERNEL); 187 if (!otp) 188 return -ENOMEM; 189 190 otp->dev = dev; 191 otp->base = devm_platform_ioremap_resource(pdev, 0); 192 if (IS_ERR(otp->base)) 193 return PTR_ERR(otp->base); 194 195 otp_config.priv = otp; 196 otp_config.dev = dev; 197 otp_config.size = (uintptr_t) device_get_match_data(dev); 198 199 nvmem = devm_nvmem_register(dev, &otp_config); 200 201 return PTR_ERR_OR_ZERO(nvmem); 202 } 203 204 static const struct of_device_id lan9662_otp_match[] = { 205 { 206 .compatible = "microchip,lan9662-otpc", 207 .data = (const void *) SZ_8K, 208 }, 209 { 210 .compatible = "microchip,lan9691-otpc", 211 .data = (const void *) SZ_16K, 212 }, 213 { }, 214 }; 215 MODULE_DEVICE_TABLE(of, lan9662_otp_match); 216 217 static struct platform_driver lan9662_otp_driver = { 218 .probe = lan9662_otp_probe, 219 .driver = { 220 .name = "lan9662-otp", 221 .of_match_table = lan9662_otp_match, 222 }, 223 }; 224 module_platform_driver(lan9662_otp_driver); 225 226 MODULE_AUTHOR("Horatiu Vultur <horatiu.vultur@microchip.com>"); 227 MODULE_DESCRIPTION("lan9662 OTP driver"); 228 MODULE_LICENSE("GPL"); 229