Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * I2C Driver for Atmel ATSHA204 over I2C |
| 3 | * |
| 4 | * Copyright (C) 2014 Josh Datko, Cryptotronix, jbd@cryptotronix.com |
Wolfgang Denk | 0cf207e | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 5 | * 2016 Tomas Hlavacek, CZ.NIC, tmshlvck@gmail.com |
Marek Behún | 61143f7 | 2022-06-01 17:17:06 +0200 | [diff] [blame] | 6 | * 2017 Marek Behún, CZ.NIC, kabel@kernel.org |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <dm.h> |
| 15 | #include <i2c.h> |
| 16 | #include <errno.h> |
| 17 | #include <atsha204a-i2c.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 18 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 20 | #include <linux/delay.h> |
Pali Rohár | 467f0c4 | 2022-04-12 11:20:44 +0200 | [diff] [blame] | 21 | #include <linux/bitrev.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 22 | #include <u-boot/crc.h> |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 23 | |
Paweł Anikiel | 73d88cf | 2022-06-17 12:47:21 +0200 | [diff] [blame] | 24 | #define ATSHA204A_TWLO_US 60 |
| 25 | #define ATSHA204A_TWHI_US 2500 |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 26 | #define ATSHA204A_TRANSACTION_TIMEOUT 100000 |
| 27 | #define ATSHA204A_TRANSACTION_RETRY 5 |
| 28 | #define ATSHA204A_EXECTIME 5000 |
| 29 | |
| 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Pali Rohár | 467f0c4 | 2022-04-12 11:20:44 +0200 | [diff] [blame] | 32 | static inline u16 atsha204a_crc16(const u8 *buffer, size_t len) |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 33 | { |
Pali Rohár | 467f0c4 | 2022-04-12 11:20:44 +0200 | [diff] [blame] | 34 | return bitrev16(crc16(0, buffer, len)); |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static int atsha204a_send(struct udevice *dev, const u8 *buf, u8 len) |
| 38 | { |
| 39 | fdt_addr_t *priv = dev_get_priv(dev); |
| 40 | struct i2c_msg msg; |
| 41 | |
| 42 | msg.addr = *priv; |
| 43 | msg.flags = I2C_M_STOP; |
| 44 | msg.len = len; |
| 45 | msg.buf = (u8 *) buf; |
| 46 | |
| 47 | return dm_i2c_xfer(dev, &msg, 1); |
| 48 | } |
| 49 | |
| 50 | static int atsha204a_recv(struct udevice *dev, u8 *buf, u8 len) |
| 51 | { |
| 52 | fdt_addr_t *priv = dev_get_priv(dev); |
| 53 | struct i2c_msg msg; |
| 54 | |
| 55 | msg.addr = *priv; |
| 56 | msg.flags = I2C_M_RD | I2C_M_STOP; |
| 57 | msg.len = len; |
| 58 | msg.buf = (u8 *) buf; |
| 59 | |
| 60 | return dm_i2c_xfer(dev, &msg, 1); |
| 61 | } |
| 62 | |
| 63 | static int atsha204a_recv_resp(struct udevice *dev, |
| 64 | struct atsha204a_resp *resp) |
| 65 | { |
| 66 | int res; |
| 67 | u16 resp_crc, computed_crc; |
| 68 | u8 *p = (u8 *) resp; |
| 69 | |
| 70 | res = atsha204a_recv(dev, p, 4); |
| 71 | if (res) |
| 72 | return res; |
| 73 | |
| 74 | if (resp->length > 4) { |
| 75 | if (resp->length > sizeof(*resp)) |
| 76 | return -EMSGSIZE; |
| 77 | |
| 78 | res = atsha204a_recv(dev, p + 4, resp->length - 4); |
| 79 | if (res) |
| 80 | return res; |
| 81 | } |
| 82 | |
| 83 | resp_crc = (u16) p[resp->length - 2] |
| 84 | | (((u16) p[resp->length - 1]) << 8); |
| 85 | computed_crc = atsha204a_crc16(p, resp->length - 2); |
| 86 | |
| 87 | if (resp_crc != computed_crc) { |
| 88 | debug("Invalid checksum in ATSHA204A response\n"); |
| 89 | return -EBADMSG; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | int atsha204a_wakeup(struct udevice *dev) |
| 96 | { |
| 97 | u8 req[4]; |
| 98 | struct atsha204a_resp resp; |
| 99 | int try, res; |
| 100 | |
| 101 | debug("Waking up ATSHA204A\n"); |
| 102 | |
| 103 | for (try = 1; try <= 10; ++try) { |
| 104 | debug("Try %i... ", try); |
| 105 | |
Pali Rohár | ca514d0 | 2022-08-04 13:03:44 +0200 | [diff] [blame] | 106 | /* |
| 107 | * The device ignores any levels or transitions on the SCL pin |
| 108 | * when the device is idle, asleep or during waking up. |
| 109 | * Don't check for error when waking up the device. |
| 110 | */ |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 111 | memset(req, 0, 4); |
Pali Rohár | ca514d0 | 2022-08-04 13:03:44 +0200 | [diff] [blame] | 112 | atsha204a_send(dev, req, 4); |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 113 | |
Paweł Anikiel | 73d88cf | 2022-06-17 12:47:21 +0200 | [diff] [blame] | 114 | udelay(ATSHA204A_TWLO_US + ATSHA204A_TWHI_US); |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 115 | |
| 116 | res = atsha204a_recv_resp(dev, &resp); |
| 117 | if (res) { |
| 118 | debug("failed on receiving response, ending\n"); |
| 119 | return res; |
| 120 | } |
| 121 | |
| 122 | if (resp.code != ATSHA204A_STATUS_AFTER_WAKE) { |
| 123 | debug ("failed (responce code = %02x), ending\n", |
| 124 | resp.code); |
| 125 | return -EBADMSG; |
| 126 | } |
| 127 | |
| 128 | debug("success\n"); |
Adrian Fiergolski | 75967a2 | 2022-01-11 19:05:29 +0100 | [diff] [blame] | 129 | return 0; |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Adrian Fiergolski | 75967a2 | 2022-01-11 19:05:29 +0100 | [diff] [blame] | 132 | return -ETIMEDOUT; |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int atsha204a_idle(struct udevice *dev) |
| 136 | { |
| 137 | int res; |
| 138 | u8 req = ATSHA204A_FUNC_IDLE; |
| 139 | |
| 140 | res = atsha204a_send(dev, &req, 1); |
| 141 | if (res) |
| 142 | debug("Failed putting ATSHA204A idle\n"); |
| 143 | return res; |
| 144 | } |
| 145 | |
| 146 | int atsha204a_sleep(struct udevice *dev) |
| 147 | { |
| 148 | int res; |
| 149 | u8 req = ATSHA204A_FUNC_IDLE; |
| 150 | |
| 151 | res = atsha204a_send(dev, &req, 1); |
| 152 | if (res) |
| 153 | debug("Failed putting ATSHA204A to sleep\n"); |
| 154 | return res; |
| 155 | } |
| 156 | |
| 157 | static int atsha204a_transaction(struct udevice *dev, struct atsha204a_req *req, |
| 158 | struct atsha204a_resp *resp) |
| 159 | { |
| 160 | int res, timeout = ATSHA204A_TRANSACTION_TIMEOUT; |
| 161 | |
| 162 | res = atsha204a_send(dev, (u8 *) req, req->length + 1); |
| 163 | if (res) { |
| 164 | debug("ATSHA204A transaction send failed\n"); |
| 165 | return -EBUSY; |
| 166 | } |
| 167 | |
| 168 | do { |
Adrian Fiergolski | e466271 | 2022-01-11 19:05:30 +0100 | [diff] [blame] | 169 | udelay(ATSHA204A_EXECTIME); |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 170 | res = atsha204a_recv_resp(dev, resp); |
| 171 | if (!res || res == -EMSGSIZE || res == -EBADMSG) |
| 172 | break; |
| 173 | |
| 174 | debug("ATSHA204A transaction polling for response " |
| 175 | "(timeout = %d)\n", timeout); |
| 176 | |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 177 | timeout -= ATSHA204A_EXECTIME; |
| 178 | } while (timeout > 0); |
| 179 | |
| 180 | if (timeout <= 0) { |
| 181 | debug("ATSHA204A transaction timed out\n"); |
| 182 | return -ETIMEDOUT; |
| 183 | } |
| 184 | |
| 185 | return res; |
| 186 | } |
| 187 | |
| 188 | static void atsha204a_req_crc32(struct atsha204a_req *req) |
| 189 | { |
| 190 | u8 *p = (u8 *) req; |
| 191 | u16 computed_crc; |
| 192 | u16 *crc_ptr = (u16 *) &p[req->length - 1]; |
| 193 | |
| 194 | /* The buffer to crc16 starts at byte 1, not 0 */ |
| 195 | computed_crc = atsha204a_crc16(p + 1, req->length - 2); |
| 196 | |
| 197 | *crc_ptr = cpu_to_le16(computed_crc); |
| 198 | } |
| 199 | |
| 200 | int atsha204a_read(struct udevice *dev, enum atsha204a_zone zone, bool read32, |
| 201 | u16 addr, u8 *buffer) |
| 202 | { |
| 203 | int res, retry = ATSHA204A_TRANSACTION_RETRY; |
| 204 | struct atsha204a_req req; |
| 205 | struct atsha204a_resp resp; |
| 206 | |
| 207 | req.function = ATSHA204A_FUNC_COMMAND; |
| 208 | req.length = 7; |
| 209 | req.command = ATSHA204A_CMD_READ; |
| 210 | |
| 211 | req.param1 = (u8) zone; |
| 212 | if (read32) |
| 213 | req.param1 |= 0x80; |
| 214 | |
| 215 | req.param2 = cpu_to_le16(addr); |
| 216 | |
| 217 | atsha204a_req_crc32(&req); |
| 218 | |
| 219 | do { |
| 220 | res = atsha204a_transaction(dev, &req, &resp); |
| 221 | if (!res) |
| 222 | break; |
| 223 | |
| 224 | debug("ATSHA204A read retry (%d)\n", retry); |
| 225 | retry--; |
| 226 | atsha204a_wakeup(dev); |
| 227 | } while (retry >= 0); |
Wolfgang Denk | 0a50b3c | 2021-09-27 17:42:38 +0200 | [diff] [blame] | 228 | |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 229 | if (res) { |
| 230 | debug("ATSHA204A read failed\n"); |
| 231 | return res; |
| 232 | } |
| 233 | |
| 234 | if (resp.length != (read32 ? 32 : 4) + 3) { |
| 235 | debug("ATSHA204A read bad response length (%d)\n", |
| 236 | resp.length); |
| 237 | return -EBADMSG; |
| 238 | } |
| 239 | |
| 240 | memcpy(buffer, ((u8 *) &resp) + 1, read32 ? 32 : 4); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int atsha204a_get_random(struct udevice *dev, u8 *buffer, size_t max) |
| 246 | { |
| 247 | int res; |
| 248 | struct atsha204a_req req; |
| 249 | struct atsha204a_resp resp; |
| 250 | |
| 251 | req.function = ATSHA204A_FUNC_COMMAND; |
| 252 | req.length = 7; |
| 253 | req.command = ATSHA204A_CMD_RANDOM; |
| 254 | |
| 255 | req.param1 = 1; |
| 256 | req.param2 = 0; |
| 257 | |
| 258 | /* We do not have to compute the checksum dynamically */ |
| 259 | req.data[0] = 0x27; |
| 260 | req.data[1] = 0x47; |
| 261 | |
| 262 | res = atsha204a_transaction(dev, &req, &resp); |
| 263 | if (res) { |
| 264 | debug("ATSHA204A random transaction failed\n"); |
| 265 | return res; |
| 266 | } |
| 267 | |
| 268 | memcpy(buffer, ((u8 *) &resp) + 1, max >= 32 ? 32 : max); |
| 269 | return 0; |
| 270 | } |
| 271 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 272 | static int atsha204a_of_to_plat(struct udevice *dev) |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 273 | { |
| 274 | fdt_addr_t *priv = dev_get_priv(dev); |
| 275 | fdt_addr_t addr; |
| 276 | |
Adrian Fiergolski | 532a5b2 | 2022-01-11 19:05:31 +0100 | [diff] [blame] | 277 | addr = dev_read_addr(dev); |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 278 | if (addr == FDT_ADDR_T_NONE) { |
| 279 | debug("Can't get ATSHA204A I2C base address\n"); |
| 280 | return -ENXIO; |
| 281 | } |
| 282 | |
| 283 | *priv = addr; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static const struct udevice_id atsha204a_ids[] = { |
Pali Rohár | 89eabd2 | 2022-04-05 14:49:08 +0200 | [diff] [blame] | 288 | { .compatible = "atmel,atsha204" }, |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 289 | { .compatible = "atmel,atsha204a" }, |
| 290 | { } |
| 291 | }; |
| 292 | |
| 293 | U_BOOT_DRIVER(atsha204) = { |
| 294 | .name = "atsha204", |
| 295 | .id = UCLASS_MISC, |
| 296 | .of_match = atsha204a_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 297 | .of_to_plat = atsha204a_of_to_plat, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 298 | .priv_auto = sizeof(fdt_addr_t), |
Marek Behún | aa5eb9a | 2017-06-09 19:28:44 +0200 | [diff] [blame] | 299 | }; |