blob: e7c6be593dc1dfaec86b39d3829b17fcc5a88872 [file] [log] [blame]
Marek Behúnef2b6b12017-06-09 19:28:44 +02001/*
2 * I2C Driver for Atmel ATSHA204 over I2C
3 *
4 * Copyright (C) 2014 Josh Datko, Cryptotronix, jbd@cryptotronix.com
Wolfgang Denk62fb2b42021-09-27 17:42:39 +02005 * 2016 Tomas Hlavacek, CZ.NIC, tmshlvck@gmail.com
Marek Behúnd63726e2022-06-01 17:17:06 +02006 * 2017 Marek Behún, CZ.NIC, kabel@kernel.org
Marek Behúnef2b6b12017-06-09 19:28:44 +02007 *
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 Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Pali Rohár2e269302022-04-12 11:20:44 +020021#include <linux/bitrev.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070022#include <u-boot/crc.h>
Marek Behúnef2b6b12017-06-09 19:28:44 +020023
Paweł Anikiela47bfcb2022-06-17 12:47:21 +020024#define ATSHA204A_TWLO_US 60
25#define ATSHA204A_TWHI_US 2500
Marek Behúnef2b6b12017-06-09 19:28:44 +020026#define ATSHA204A_TRANSACTION_TIMEOUT 100000
27#define ATSHA204A_TRANSACTION_RETRY 5
28#define ATSHA204A_EXECTIME 5000
29
30DECLARE_GLOBAL_DATA_PTR;
31
Pali Rohár2e269302022-04-12 11:20:44 +020032static inline u16 atsha204a_crc16(const u8 *buffer, size_t len)
Marek Behúnef2b6b12017-06-09 19:28:44 +020033{
Pali Rohár2e269302022-04-12 11:20:44 +020034 return bitrev16(crc16(0, buffer, len));
Marek Behúnef2b6b12017-06-09 19:28:44 +020035}
36
37static 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
50static 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
63static 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
95int 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
106 memset(req, 0, 4);
107 res = atsha204a_send(dev, req, 4);
108 if (res) {
109 debug("failed on I2C send, trying again\n");
110 continue;
111 }
112
Paweł Anikiela47bfcb2022-06-17 12:47:21 +0200113 udelay(ATSHA204A_TWLO_US + ATSHA204A_TWHI_US);
Marek Behúnef2b6b12017-06-09 19:28:44 +0200114
115 res = atsha204a_recv_resp(dev, &resp);
116 if (res) {
117 debug("failed on receiving response, ending\n");
118 return res;
119 }
120
121 if (resp.code != ATSHA204A_STATUS_AFTER_WAKE) {
122 debug ("failed (responce code = %02x), ending\n",
123 resp.code);
124 return -EBADMSG;
125 }
126
127 debug("success\n");
Adrian Fiergolski58984392022-01-11 19:05:29 +0100128 return 0;
Marek Behúnef2b6b12017-06-09 19:28:44 +0200129 }
130
Adrian Fiergolski58984392022-01-11 19:05:29 +0100131 return -ETIMEDOUT;
Marek Behúnef2b6b12017-06-09 19:28:44 +0200132}
133
134int atsha204a_idle(struct udevice *dev)
135{
136 int res;
137 u8 req = ATSHA204A_FUNC_IDLE;
138
139 res = atsha204a_send(dev, &req, 1);
140 if (res)
141 debug("Failed putting ATSHA204A idle\n");
142 return res;
143}
144
145int atsha204a_sleep(struct udevice *dev)
146{
147 int res;
148 u8 req = ATSHA204A_FUNC_IDLE;
149
150 res = atsha204a_send(dev, &req, 1);
151 if (res)
152 debug("Failed putting ATSHA204A to sleep\n");
153 return res;
154}
155
156static int atsha204a_transaction(struct udevice *dev, struct atsha204a_req *req,
157 struct atsha204a_resp *resp)
158{
159 int res, timeout = ATSHA204A_TRANSACTION_TIMEOUT;
160
161 res = atsha204a_send(dev, (u8 *) req, req->length + 1);
162 if (res) {
163 debug("ATSHA204A transaction send failed\n");
164 return -EBUSY;
165 }
166
167 do {
Adrian Fiergolski9468ddb2022-01-11 19:05:30 +0100168 udelay(ATSHA204A_EXECTIME);
Marek Behúnef2b6b12017-06-09 19:28:44 +0200169 res = atsha204a_recv_resp(dev, resp);
170 if (!res || res == -EMSGSIZE || res == -EBADMSG)
171 break;
172
173 debug("ATSHA204A transaction polling for response "
174 "(timeout = %d)\n", timeout);
175
Marek Behúnef2b6b12017-06-09 19:28:44 +0200176 timeout -= ATSHA204A_EXECTIME;
177 } while (timeout > 0);
178
179 if (timeout <= 0) {
180 debug("ATSHA204A transaction timed out\n");
181 return -ETIMEDOUT;
182 }
183
184 return res;
185}
186
187static void atsha204a_req_crc32(struct atsha204a_req *req)
188{
189 u8 *p = (u8 *) req;
190 u16 computed_crc;
191 u16 *crc_ptr = (u16 *) &p[req->length - 1];
192
193 /* The buffer to crc16 starts at byte 1, not 0 */
194 computed_crc = atsha204a_crc16(p + 1, req->length - 2);
195
196 *crc_ptr = cpu_to_le16(computed_crc);
197}
198
199int atsha204a_read(struct udevice *dev, enum atsha204a_zone zone, bool read32,
200 u16 addr, u8 *buffer)
201{
202 int res, retry = ATSHA204A_TRANSACTION_RETRY;
203 struct atsha204a_req req;
204 struct atsha204a_resp resp;
205
206 req.function = ATSHA204A_FUNC_COMMAND;
207 req.length = 7;
208 req.command = ATSHA204A_CMD_READ;
209
210 req.param1 = (u8) zone;
211 if (read32)
212 req.param1 |= 0x80;
213
214 req.param2 = cpu_to_le16(addr);
215
216 atsha204a_req_crc32(&req);
217
218 do {
219 res = atsha204a_transaction(dev, &req, &resp);
220 if (!res)
221 break;
222
223 debug("ATSHA204A read retry (%d)\n", retry);
224 retry--;
225 atsha204a_wakeup(dev);
226 } while (retry >= 0);
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200227
Marek Behúnef2b6b12017-06-09 19:28:44 +0200228 if (res) {
229 debug("ATSHA204A read failed\n");
230 return res;
231 }
232
233 if (resp.length != (read32 ? 32 : 4) + 3) {
234 debug("ATSHA204A read bad response length (%d)\n",
235 resp.length);
236 return -EBADMSG;
237 }
238
239 memcpy(buffer, ((u8 *) &resp) + 1, read32 ? 32 : 4);
240
241 return 0;
242}
243
244int atsha204a_get_random(struct udevice *dev, u8 *buffer, size_t max)
245{
246 int res;
247 struct atsha204a_req req;
248 struct atsha204a_resp resp;
249
250 req.function = ATSHA204A_FUNC_COMMAND;
251 req.length = 7;
252 req.command = ATSHA204A_CMD_RANDOM;
253
254 req.param1 = 1;
255 req.param2 = 0;
256
257 /* We do not have to compute the checksum dynamically */
258 req.data[0] = 0x27;
259 req.data[1] = 0x47;
260
261 res = atsha204a_transaction(dev, &req, &resp);
262 if (res) {
263 debug("ATSHA204A random transaction failed\n");
264 return res;
265 }
266
267 memcpy(buffer, ((u8 *) &resp) + 1, max >= 32 ? 32 : max);
268 return 0;
269}
270
Simon Glassaad29ae2020-12-03 16:55:21 -0700271static int atsha204a_of_to_plat(struct udevice *dev)
Marek Behúnef2b6b12017-06-09 19:28:44 +0200272{
273 fdt_addr_t *priv = dev_get_priv(dev);
274 fdt_addr_t addr;
275
Adrian Fiergolskibaa5f6a2022-01-11 19:05:31 +0100276 addr = dev_read_addr(dev);
Marek Behúnef2b6b12017-06-09 19:28:44 +0200277 if (addr == FDT_ADDR_T_NONE) {
278 debug("Can't get ATSHA204A I2C base address\n");
279 return -ENXIO;
280 }
281
282 *priv = addr;
283 return 0;
284}
285
286static const struct udevice_id atsha204a_ids[] = {
Pali Rohárb3372b02022-04-05 14:49:08 +0200287 { .compatible = "atmel,atsha204" },
Marek Behúnef2b6b12017-06-09 19:28:44 +0200288 { .compatible = "atmel,atsha204a" },
289 { }
290};
291
292U_BOOT_DRIVER(atsha204) = {
293 .name = "atsha204",
294 .id = UCLASS_MISC,
295 .of_match = atsha204a_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700296 .of_to_plat = atsha204a_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700297 .priv_auto = sizeof(fdt_addr_t),
Marek Behúnef2b6b12017-06-09 19:28:44 +0200298};