blob: aa6acf0f9a0eefccc22999c16564210b43b67681 [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
6 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
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
24#define ATSHA204A_TWLO 60
25#define ATSHA204A_TRANSACTION_TIMEOUT 100000
26#define ATSHA204A_TRANSACTION_RETRY 5
27#define ATSHA204A_EXECTIME 5000
28
29DECLARE_GLOBAL_DATA_PTR;
30
Pali Rohár2e269302022-04-12 11:20:44 +020031static inline u16 atsha204a_crc16(const u8 *buffer, size_t len)
Marek Behúnef2b6b12017-06-09 19:28:44 +020032{
Pali Rohár2e269302022-04-12 11:20:44 +020033 return bitrev16(crc16(0, buffer, len));
Marek Behúnef2b6b12017-06-09 19:28:44 +020034}
35
36static int atsha204a_send(struct udevice *dev, const u8 *buf, u8 len)
37{
38 fdt_addr_t *priv = dev_get_priv(dev);
39 struct i2c_msg msg;
40
41 msg.addr = *priv;
42 msg.flags = I2C_M_STOP;
43 msg.len = len;
44 msg.buf = (u8 *) buf;
45
46 return dm_i2c_xfer(dev, &msg, 1);
47}
48
49static int atsha204a_recv(struct udevice *dev, u8 *buf, u8 len)
50{
51 fdt_addr_t *priv = dev_get_priv(dev);
52 struct i2c_msg msg;
53
54 msg.addr = *priv;
55 msg.flags = I2C_M_RD | I2C_M_STOP;
56 msg.len = len;
57 msg.buf = (u8 *) buf;
58
59 return dm_i2c_xfer(dev, &msg, 1);
60}
61
62static int atsha204a_recv_resp(struct udevice *dev,
63 struct atsha204a_resp *resp)
64{
65 int res;
66 u16 resp_crc, computed_crc;
67 u8 *p = (u8 *) resp;
68
69 res = atsha204a_recv(dev, p, 4);
70 if (res)
71 return res;
72
73 if (resp->length > 4) {
74 if (resp->length > sizeof(*resp))
75 return -EMSGSIZE;
76
77 res = atsha204a_recv(dev, p + 4, resp->length - 4);
78 if (res)
79 return res;
80 }
81
82 resp_crc = (u16) p[resp->length - 2]
83 | (((u16) p[resp->length - 1]) << 8);
84 computed_crc = atsha204a_crc16(p, resp->length - 2);
85
86 if (resp_crc != computed_crc) {
87 debug("Invalid checksum in ATSHA204A response\n");
88 return -EBADMSG;
89 }
90
91 return 0;
92}
93
94int atsha204a_wakeup(struct udevice *dev)
95{
96 u8 req[4];
97 struct atsha204a_resp resp;
98 int try, res;
99
100 debug("Waking up ATSHA204A\n");
101
102 for (try = 1; try <= 10; ++try) {
103 debug("Try %i... ", try);
104
105 memset(req, 0, 4);
106 res = atsha204a_send(dev, req, 4);
107 if (res) {
108 debug("failed on I2C send, trying again\n");
109 continue;
110 }
111
112 udelay(ATSHA204A_TWLO);
113
114 res = atsha204a_recv_resp(dev, &resp);
115 if (res) {
116 debug("failed on receiving response, ending\n");
117 return res;
118 }
119
120 if (resp.code != ATSHA204A_STATUS_AFTER_WAKE) {
121 debug ("failed (responce code = %02x), ending\n",
122 resp.code);
123 return -EBADMSG;
124 }
125
126 debug("success\n");
Adrian Fiergolski58984392022-01-11 19:05:29 +0100127 return 0;
Marek Behúnef2b6b12017-06-09 19:28:44 +0200128 }
129
Adrian Fiergolski58984392022-01-11 19:05:29 +0100130 return -ETIMEDOUT;
Marek Behúnef2b6b12017-06-09 19:28:44 +0200131}
132
133int atsha204a_idle(struct udevice *dev)
134{
135 int res;
136 u8 req = ATSHA204A_FUNC_IDLE;
137
138 res = atsha204a_send(dev, &req, 1);
139 if (res)
140 debug("Failed putting ATSHA204A idle\n");
141 return res;
142}
143
144int atsha204a_sleep(struct udevice *dev)
145{
146 int res;
147 u8 req = ATSHA204A_FUNC_IDLE;
148
149 res = atsha204a_send(dev, &req, 1);
150 if (res)
151 debug("Failed putting ATSHA204A to sleep\n");
152 return res;
153}
154
155static int atsha204a_transaction(struct udevice *dev, struct atsha204a_req *req,
156 struct atsha204a_resp *resp)
157{
158 int res, timeout = ATSHA204A_TRANSACTION_TIMEOUT;
159
160 res = atsha204a_send(dev, (u8 *) req, req->length + 1);
161 if (res) {
162 debug("ATSHA204A transaction send failed\n");
163 return -EBUSY;
164 }
165
166 do {
Adrian Fiergolski9468ddb2022-01-11 19:05:30 +0100167 udelay(ATSHA204A_EXECTIME);
Marek Behúnef2b6b12017-06-09 19:28:44 +0200168 res = atsha204a_recv_resp(dev, resp);
169 if (!res || res == -EMSGSIZE || res == -EBADMSG)
170 break;
171
172 debug("ATSHA204A transaction polling for response "
173 "(timeout = %d)\n", timeout);
174
Marek Behúnef2b6b12017-06-09 19:28:44 +0200175 timeout -= ATSHA204A_EXECTIME;
176 } while (timeout > 0);
177
178 if (timeout <= 0) {
179 debug("ATSHA204A transaction timed out\n");
180 return -ETIMEDOUT;
181 }
182
183 return res;
184}
185
186static void atsha204a_req_crc32(struct atsha204a_req *req)
187{
188 u8 *p = (u8 *) req;
189 u16 computed_crc;
190 u16 *crc_ptr = (u16 *) &p[req->length - 1];
191
192 /* The buffer to crc16 starts at byte 1, not 0 */
193 computed_crc = atsha204a_crc16(p + 1, req->length - 2);
194
195 *crc_ptr = cpu_to_le16(computed_crc);
196}
197
198int atsha204a_read(struct udevice *dev, enum atsha204a_zone zone, bool read32,
199 u16 addr, u8 *buffer)
200{
201 int res, retry = ATSHA204A_TRANSACTION_RETRY;
202 struct atsha204a_req req;
203 struct atsha204a_resp resp;
204
205 req.function = ATSHA204A_FUNC_COMMAND;
206 req.length = 7;
207 req.command = ATSHA204A_CMD_READ;
208
209 req.param1 = (u8) zone;
210 if (read32)
211 req.param1 |= 0x80;
212
213 req.param2 = cpu_to_le16(addr);
214
215 atsha204a_req_crc32(&req);
216
217 do {
218 res = atsha204a_transaction(dev, &req, &resp);
219 if (!res)
220 break;
221
222 debug("ATSHA204A read retry (%d)\n", retry);
223 retry--;
224 atsha204a_wakeup(dev);
225 } while (retry >= 0);
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200226
Marek Behúnef2b6b12017-06-09 19:28:44 +0200227 if (res) {
228 debug("ATSHA204A read failed\n");
229 return res;
230 }
231
232 if (resp.length != (read32 ? 32 : 4) + 3) {
233 debug("ATSHA204A read bad response length (%d)\n",
234 resp.length);
235 return -EBADMSG;
236 }
237
238 memcpy(buffer, ((u8 *) &resp) + 1, read32 ? 32 : 4);
239
240 return 0;
241}
242
243int atsha204a_get_random(struct udevice *dev, u8 *buffer, size_t max)
244{
245 int res;
246 struct atsha204a_req req;
247 struct atsha204a_resp resp;
248
249 req.function = ATSHA204A_FUNC_COMMAND;
250 req.length = 7;
251 req.command = ATSHA204A_CMD_RANDOM;
252
253 req.param1 = 1;
254 req.param2 = 0;
255
256 /* We do not have to compute the checksum dynamically */
257 req.data[0] = 0x27;
258 req.data[1] = 0x47;
259
260 res = atsha204a_transaction(dev, &req, &resp);
261 if (res) {
262 debug("ATSHA204A random transaction failed\n");
263 return res;
264 }
265
266 memcpy(buffer, ((u8 *) &resp) + 1, max >= 32 ? 32 : max);
267 return 0;
268}
269
Simon Glassaad29ae2020-12-03 16:55:21 -0700270static int atsha204a_of_to_plat(struct udevice *dev)
Marek Behúnef2b6b12017-06-09 19:28:44 +0200271{
272 fdt_addr_t *priv = dev_get_priv(dev);
273 fdt_addr_t addr;
274
Adrian Fiergolskibaa5f6a2022-01-11 19:05:31 +0100275 addr = dev_read_addr(dev);
Marek Behúnef2b6b12017-06-09 19:28:44 +0200276 if (addr == FDT_ADDR_T_NONE) {
277 debug("Can't get ATSHA204A I2C base address\n");
278 return -ENXIO;
279 }
280
281 *priv = addr;
282 return 0;
283}
284
285static const struct udevice_id atsha204a_ids[] = {
Pali Rohárb3372b02022-04-05 14:49:08 +0200286 { .compatible = "atmel,atsha204" },
Marek Behúnef2b6b12017-06-09 19:28:44 +0200287 { .compatible = "atmel,atsha204a" },
288 { }
289};
290
291U_BOOT_DRIVER(atsha204) = {
292 .name = "atsha204",
293 .id = UCLASS_MISC,
294 .of_match = atsha204a_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700295 .of_to_plat = atsha204a_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700296 .priv_auto = sizeof(fdt_addr_t),
Marek Behúnef2b6b12017-06-09 19:28:44 +0200297};