summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/nand/spi/otp.c
blob: ce41bca86ea919aea21083159850e145a1ee04ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2025, SaluteDevices. All Rights Reserved.
 *
 * Author: Martin Kurbanov <mmkurbanov@salutedevices.com>
 */

#include <linux/mtd/mtd.h>
#include <linux/mtd/spinand.h>

/**
 * spinand_otp_page_size() - Get SPI-NAND OTP page size
 * @spinand: the spinand device
 *
 * Return: the OTP page size.
 */
size_t spinand_otp_page_size(struct spinand_device *spinand)
{
	struct nand_device *nand = spinand_to_nand(spinand);

	return nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
}

static size_t spinand_otp_size(struct spinand_device *spinand,
			       const struct spinand_otp_layout *layout)
{
	return layout->npages * spinand_otp_page_size(spinand);
}

/**
 * spinand_fact_otp_size() - Get SPI-NAND factory OTP area size
 * @spinand: the spinand device
 *
 * Return: the OTP size.
 */
size_t spinand_fact_otp_size(struct spinand_device *spinand)
{
	return spinand_otp_size(spinand, &spinand->fact_otp->layout);
}

/**
 * spinand_user_otp_size() - Get SPI-NAND user OTP area size
 * @spinand: the spinand device
 *
 * Return: the OTP size.
 */
size_t spinand_user_otp_size(struct spinand_device *spinand)
{
	return spinand_otp_size(spinand, &spinand->user_otp->layout);
}

static int spinand_otp_check_bounds(struct spinand_device *spinand, loff_t ofs,
				    size_t len,
				    const struct spinand_otp_layout *layout)
{
	if (ofs < 0 || ofs + len > spinand_otp_size(spinand, layout))
		return -EINVAL;

	return 0;
}

static int spinand_user_otp_check_bounds(struct spinand_device *spinand,
					 loff_t ofs, size_t len)
{
	return spinand_otp_check_bounds(spinand, ofs, len,
					&spinand->user_otp->layout);
}

static int spinand_otp_rw(struct spinand_device *spinand, loff_t ofs,
			  size_t len, size_t *retlen, u8 *buf, bool is_write,
			  const struct spinand_otp_layout *layout)
{
	struct nand_page_io_req req = {};
	unsigned long long page;
	size_t copied = 0;
	size_t otp_pagesize = spinand_otp_page_size(spinand);
	int ret;

	if (!len)
		return 0;

	ret = spinand_otp_check_bounds(spinand, ofs, len, layout);
	if (ret)
		return ret;

	ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, CFG_OTP_ENABLE);
	if (ret)
		return ret;

	page = ofs;
	req.dataoffs = do_div(page, otp_pagesize);
	req.pos.page = page + layout->start_page;
	req.type = is_write ? NAND_PAGE_WRITE : NAND_PAGE_READ;
	req.mode = MTD_OPS_RAW;
	req.databuf.in = buf;

	while (copied < len) {
		req.datalen = min_t(unsigned int,
				    otp_pagesize - req.dataoffs,
				    len - copied);

		if (is_write)
			ret = spinand_write_page(spinand, &req);
		else
			ret = spinand_read_page(spinand, &req);

		if (ret < 0)
			break;

		req.databuf.in += req.datalen;
		req.pos.page++;
		req.dataoffs = 0;
		copied += req.datalen;
	}

	*retlen = copied;

	if (spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0)) {
		dev_warn(&spinand_to_mtd(spinand)->dev,
			 "Can not disable OTP mode\n");
		ret = -EIO;
	}

	return ret;
}

/**
 * spinand_fact_otp_read() - Read from OTP area
 * @spinand: the spinand device
 * @ofs: the offset to read
 * @len: the number of data bytes to read
 * @retlen: the pointer to variable to store the number of read bytes
 * @buf: the buffer to store the read data
 *
 * Return: 0 on success, an error code otherwise.
 */
int spinand_fact_otp_read(struct spinand_device *spinand, loff_t ofs,
			  size_t len, size_t *retlen, u8 *buf)
{
	return spinand_otp_rw(spinand, ofs, len, retlen, buf, false,
			      &spinand->fact_otp->layout);
}

/**
 * spinand_user_otp_read() - Read from OTP area
 * @spinand: the spinand device
 * @ofs: the offset to read
 * @len: the number of data bytes to read
 * @retlen: the pointer to variable to store the number of read bytes
 * @buf: the buffer to store the read data
 *
 * Return: 0 on success, an error code otherwise.
 */
int spinand_user_otp_read(struct spinand_device *spinand, loff_t ofs,
			  size_t len, size_t *retlen, u8 *buf)
{
	return spinand_otp_rw(spinand, ofs, len, retlen, buf, false,
			      &spinand->user_otp->layout);
}

/**
 * spinand_user_otp_write() - Write to OTP area
 * @spinand:  the spinand device
 * @ofs: the offset to write to
 * @len: the number of bytes to write
 * @retlen: the pointer to variable to store the number of written bytes
 * @buf: the buffer with data to write
 *
 * Return: 0 on success, an error code otherwise.
 */
int spinand_user_otp_write(struct spinand_device *spinand, loff_t ofs,
			   size_t len, size_t *retlen, const u8 *buf)
{
	return spinand_otp_rw(spinand, ofs, len, retlen, (u8 *)buf, true,
			      &spinand->user_otp->layout);
}

static int spinand_mtd_otp_info(struct mtd_info *mtd, size_t len,
				size_t *retlen, struct otp_info *buf,
				bool is_fact)
{
	struct spinand_device *spinand = mtd_to_spinand(mtd);
	int ret;

	*retlen = 0;

	mutex_lock(&spinand->lock);

	if (is_fact)
		ret = spinand->fact_otp->ops->info(spinand, len, buf, retlen);
	else
		ret = spinand->user_otp->ops->info(spinand, len, buf, retlen);

	mutex_unlock(&spinand->lock);

	return ret;
}

static int spinand_mtd_fact_otp_info(struct mtd_info *mtd, size_t len,
				     size_t *retlen, struct otp_info *buf)
{
	return spinand_mtd_otp_info(mtd, len, retlen, buf, true);
}

static int spinand_mtd_user_otp_info(struct mtd_info *mtd, size_t len,
				     size_t *retlen, struct otp_info *buf)
{
	return spinand_mtd_otp_info(mtd, len, retlen, buf, false);
}

static int spinand_mtd_otp_read(struct mtd_info *mtd, loff_t ofs, size_t len,
				size_t *retlen, u8 *buf, bool is_fact)
{
	struct spinand_device *spinand = mtd_to_spinand(mtd);
	int ret;

	*retlen = 0;

	if (!len)
		return 0;

	ret = spinand_otp_check_bounds(spinand, ofs, len,
				       is_fact ? &spinand->fact_otp->layout :
						 &spinand->user_otp->layout);
	if (ret)
		return ret;

	mutex_lock(&spinand->lock);

	if (is_fact)
		ret = spinand->fact_otp->ops->read(spinand, ofs, len, retlen,
						   buf);
	else
		ret = spinand->user_otp->ops->read(spinand, ofs, len, retlen,
						   buf);

	mutex_unlock(&spinand->lock);

	return ret;
}

static int spinand_mtd_fact_otp_read(struct mtd_info *mtd, loff_t ofs,
				     size_t len, size_t *retlen, u8 *buf)
{
	return spinand_mtd_otp_read(mtd, ofs, len, retlen, buf, true);
}

static int spinand_mtd_user_otp_read(struct mtd_info *mtd, loff_t ofs,
				     size_t len, size_t *retlen, u8 *buf)
{
	return spinand_mtd_otp_read(mtd, ofs, len, retlen, buf, false);
}

static int spinand_mtd_user_otp_write(struct mtd_info *mtd, loff_t ofs,
				      size_t len, size_t *retlen, const u8 *buf)
{
	struct spinand_device *spinand = mtd_to_spinand(mtd);
	const struct spinand_user_otp_ops *ops = spinand->user_otp->ops;
	int ret;

	*retlen = 0;

	if (!len)
		return 0;

	ret = spinand_user_otp_check_bounds(spinand, ofs, len);
	if (ret)
		return ret;

	mutex_lock(&spinand->lock);
	ret = ops->write(spinand, ofs, len, retlen, buf);
	mutex_unlock(&spinand->lock);

	return ret;
}

static int spinand_mtd_user_otp_erase(struct mtd_info *mtd, loff_t ofs,
				      size_t len)
{
	struct spinand_device *spinand = mtd_to_spinand(mtd);
	const struct spinand_user_otp_ops *ops = spinand->user_otp->ops;
	int ret;

	if (!len)
		return 0;

	ret = spinand_user_otp_check_bounds(spinand, ofs, len);
	if (ret)
		return ret;

	mutex_lock(&spinand->lock);
	ret = ops->erase(spinand, ofs, len);
	mutex_unlock(&spinand->lock);

	return ret;
}

static int spinand_mtd_user_otp_lock(struct mtd_info *mtd, loff_t ofs,
				     size_t len)
{
	struct spinand_device *spinand = mtd_to_spinand(mtd);
	const struct spinand_user_otp_ops *ops = spinand->user_otp->ops;
	int ret;

	if (!len)
		return 0;

	ret = spinand_user_otp_check_bounds(spinand, ofs, len);
	if (ret)
		return ret;

	mutex_lock(&spinand->lock);
	ret = ops->lock(spinand, ofs, len);
	mutex_unlock(&spinand->lock);

	return ret;
}

/**
 * spinand_set_mtd_otp_ops() - Setup OTP methods
 * @spinand: the spinand device
 *
 * Setup OTP methods.
 *
 * Return: 0 on success, a negative error code otherwise.
 */
int spinand_set_mtd_otp_ops(struct spinand_device *spinand)
{
	struct mtd_info *mtd = spinand_to_mtd(spinand);
	const struct spinand_fact_otp_ops *fact_ops = spinand->fact_otp->ops;
	const struct spinand_user_otp_ops *user_ops = spinand->user_otp->ops;

	if (!user_ops && !fact_ops)
		return -EINVAL;

	if (user_ops) {
		if (user_ops->info)
			mtd->_get_user_prot_info = spinand_mtd_user_otp_info;

		if (user_ops->read)
			mtd->_read_user_prot_reg = spinand_mtd_user_otp_read;

		if (user_ops->write)
			mtd->_write_user_prot_reg = spinand_mtd_user_otp_write;

		if (user_ops->lock)
			mtd->_lock_user_prot_reg = spinand_mtd_user_otp_lock;

		if (user_ops->erase)
			mtd->_erase_user_prot_reg = spinand_mtd_user_otp_erase;
	}

	if (fact_ops) {
		if (fact_ops->info)
			mtd->_get_fact_prot_info = spinand_mtd_fact_otp_info;

		if (fact_ops->read)
			mtd->_read_fact_prot_reg = spinand_mtd_fact_otp_read;
	}

	return 0;
}