otp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OTP support for SPI NOR flashes
  4. *
  5. * Copyright (C) 2021 Michael Walle <[email protected]>
  6. */
  7. #include <linux/log2.h>
  8. #include <linux/mtd/mtd.h>
  9. #include <linux/mtd/spi-nor.h>
  10. #include "core.h"
  11. #define spi_nor_otp_region_len(nor) ((nor)->params->otp.org->len)
  12. #define spi_nor_otp_n_regions(nor) ((nor)->params->otp.org->n_regions)
  13. /**
  14. * spi_nor_otp_read_secr() - read security register
  15. * @nor: pointer to 'struct spi_nor'
  16. * @addr: offset to read from
  17. * @len: number of bytes to read
  18. * @buf: pointer to dst buffer
  19. *
  20. * Read a security register by using the SPINOR_OP_RSECR commands.
  21. *
  22. * In Winbond/GigaDevice datasheets the term "security register" stands for
  23. * an one-time-programmable memory area, consisting of multiple bytes (usually
  24. * 256). Thus one "security register" maps to one OTP region.
  25. *
  26. * This method is used on GigaDevice and Winbond flashes.
  27. *
  28. * Please note, the read must not span multiple registers.
  29. *
  30. * Return: number of bytes read successfully, -errno otherwise
  31. */
  32. int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf)
  33. {
  34. u8 addr_nbytes, read_opcode, read_dummy;
  35. struct spi_mem_dirmap_desc *rdesc;
  36. enum spi_nor_protocol read_proto;
  37. int ret;
  38. read_opcode = nor->read_opcode;
  39. addr_nbytes = nor->addr_nbytes;
  40. read_dummy = nor->read_dummy;
  41. read_proto = nor->read_proto;
  42. rdesc = nor->dirmap.rdesc;
  43. nor->read_opcode = SPINOR_OP_RSECR;
  44. nor->read_dummy = 8;
  45. nor->read_proto = SNOR_PROTO_1_1_1;
  46. nor->dirmap.rdesc = NULL;
  47. ret = spi_nor_read_data(nor, addr, len, buf);
  48. nor->read_opcode = read_opcode;
  49. nor->addr_nbytes = addr_nbytes;
  50. nor->read_dummy = read_dummy;
  51. nor->read_proto = read_proto;
  52. nor->dirmap.rdesc = rdesc;
  53. return ret;
  54. }
  55. /**
  56. * spi_nor_otp_write_secr() - write security register
  57. * @nor: pointer to 'struct spi_nor'
  58. * @addr: offset to write to
  59. * @len: number of bytes to write
  60. * @buf: pointer to src buffer
  61. *
  62. * Write a security register by using the SPINOR_OP_PSECR commands.
  63. *
  64. * For more information on the term "security register", see the documentation
  65. * of spi_nor_otp_read_secr().
  66. *
  67. * This method is used on GigaDevice and Winbond flashes.
  68. *
  69. * Please note, the write must not span multiple registers.
  70. *
  71. * Return: number of bytes written successfully, -errno otherwise
  72. */
  73. int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
  74. const u8 *buf)
  75. {
  76. enum spi_nor_protocol write_proto;
  77. struct spi_mem_dirmap_desc *wdesc;
  78. u8 addr_nbytes, program_opcode;
  79. int ret, written;
  80. program_opcode = nor->program_opcode;
  81. addr_nbytes = nor->addr_nbytes;
  82. write_proto = nor->write_proto;
  83. wdesc = nor->dirmap.wdesc;
  84. nor->program_opcode = SPINOR_OP_PSECR;
  85. nor->write_proto = SNOR_PROTO_1_1_1;
  86. nor->dirmap.wdesc = NULL;
  87. /*
  88. * We only support a write to one single page. For now all winbond
  89. * flashes only have one page per security register.
  90. */
  91. ret = spi_nor_write_enable(nor);
  92. if (ret)
  93. goto out;
  94. written = spi_nor_write_data(nor, addr, len, buf);
  95. if (written < 0)
  96. goto out;
  97. ret = spi_nor_wait_till_ready(nor);
  98. out:
  99. nor->program_opcode = program_opcode;
  100. nor->addr_nbytes = addr_nbytes;
  101. nor->write_proto = write_proto;
  102. nor->dirmap.wdesc = wdesc;
  103. return ret ?: written;
  104. }
  105. /**
  106. * spi_nor_otp_erase_secr() - erase a security register
  107. * @nor: pointer to 'struct spi_nor'
  108. * @addr: offset of the security register to be erased
  109. *
  110. * Erase a security register by using the SPINOR_OP_ESECR command.
  111. *
  112. * For more information on the term "security register", see the documentation
  113. * of spi_nor_otp_read_secr().
  114. *
  115. * This method is used on GigaDevice and Winbond flashes.
  116. *
  117. * Return: 0 on success, -errno otherwise
  118. */
  119. int spi_nor_otp_erase_secr(struct spi_nor *nor, loff_t addr)
  120. {
  121. u8 erase_opcode = nor->erase_opcode;
  122. int ret;
  123. ret = spi_nor_write_enable(nor);
  124. if (ret)
  125. return ret;
  126. nor->erase_opcode = SPINOR_OP_ESECR;
  127. ret = spi_nor_erase_sector(nor, addr);
  128. nor->erase_opcode = erase_opcode;
  129. if (ret)
  130. return ret;
  131. return spi_nor_wait_till_ready(nor);
  132. }
  133. static int spi_nor_otp_lock_bit_cr(unsigned int region)
  134. {
  135. static const int lock_bits[] = { SR2_LB1, SR2_LB2, SR2_LB3 };
  136. if (region >= ARRAY_SIZE(lock_bits))
  137. return -EINVAL;
  138. return lock_bits[region];
  139. }
  140. /**
  141. * spi_nor_otp_lock_sr2() - lock the OTP region
  142. * @nor: pointer to 'struct spi_nor'
  143. * @region: OTP region
  144. *
  145. * Lock the OTP region by writing the status register-2. This method is used on
  146. * GigaDevice and Winbond flashes.
  147. *
  148. * Return: 0 on success, -errno otherwise.
  149. */
  150. int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region)
  151. {
  152. u8 *cr = nor->bouncebuf;
  153. int ret, lock_bit;
  154. lock_bit = spi_nor_otp_lock_bit_cr(region);
  155. if (lock_bit < 0)
  156. return lock_bit;
  157. ret = spi_nor_read_cr(nor, cr);
  158. if (ret)
  159. return ret;
  160. /* no need to write the register if region is already locked */
  161. if (cr[0] & lock_bit)
  162. return 0;
  163. cr[0] |= lock_bit;
  164. return spi_nor_write_16bit_cr_and_check(nor, cr[0]);
  165. }
  166. /**
  167. * spi_nor_otp_is_locked_sr2() - get the OTP region lock status
  168. * @nor: pointer to 'struct spi_nor'
  169. * @region: OTP region
  170. *
  171. * Retrieve the OTP region lock bit by reading the status register-2. This
  172. * method is used on GigaDevice and Winbond flashes.
  173. *
  174. * Return: 0 on success, -errno otherwise.
  175. */
  176. int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region)
  177. {
  178. u8 *cr = nor->bouncebuf;
  179. int ret, lock_bit;
  180. lock_bit = spi_nor_otp_lock_bit_cr(region);
  181. if (lock_bit < 0)
  182. return lock_bit;
  183. ret = spi_nor_read_cr(nor, cr);
  184. if (ret)
  185. return ret;
  186. return cr[0] & lock_bit;
  187. }
  188. static loff_t spi_nor_otp_region_start(const struct spi_nor *nor, unsigned int region)
  189. {
  190. const struct spi_nor_otp_organization *org = nor->params->otp.org;
  191. return org->base + region * org->offset;
  192. }
  193. static size_t spi_nor_otp_size(struct spi_nor *nor)
  194. {
  195. return spi_nor_otp_n_regions(nor) * spi_nor_otp_region_len(nor);
  196. }
  197. /* Translate the file offsets from and to OTP regions. */
  198. static loff_t spi_nor_otp_region_to_offset(struct spi_nor *nor, unsigned int region)
  199. {
  200. return region * spi_nor_otp_region_len(nor);
  201. }
  202. static unsigned int spi_nor_otp_offset_to_region(struct spi_nor *nor, loff_t ofs)
  203. {
  204. return div64_u64(ofs, spi_nor_otp_region_len(nor));
  205. }
  206. static int spi_nor_mtd_otp_info(struct mtd_info *mtd, size_t len,
  207. size_t *retlen, struct otp_info *buf)
  208. {
  209. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  210. const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
  211. unsigned int n_regions = spi_nor_otp_n_regions(nor);
  212. unsigned int i;
  213. int ret, locked;
  214. if (len < n_regions * sizeof(*buf))
  215. return -ENOSPC;
  216. ret = spi_nor_lock_and_prep(nor);
  217. if (ret)
  218. return ret;
  219. for (i = 0; i < n_regions; i++) {
  220. buf->start = spi_nor_otp_region_to_offset(nor, i);
  221. buf->length = spi_nor_otp_region_len(nor);
  222. locked = ops->is_locked(nor, i);
  223. if (locked < 0) {
  224. ret = locked;
  225. goto out;
  226. }
  227. buf->locked = !!locked;
  228. buf++;
  229. }
  230. *retlen = n_regions * sizeof(*buf);
  231. out:
  232. spi_nor_unlock_and_unprep(nor);
  233. return ret;
  234. }
  235. static int spi_nor_mtd_otp_range_is_locked(struct spi_nor *nor, loff_t ofs,
  236. size_t len)
  237. {
  238. const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
  239. unsigned int region;
  240. int locked;
  241. /*
  242. * If any of the affected OTP regions are locked the entire range is
  243. * considered locked.
  244. */
  245. for (region = spi_nor_otp_offset_to_region(nor, ofs);
  246. region <= spi_nor_otp_offset_to_region(nor, ofs + len - 1);
  247. region++) {
  248. locked = ops->is_locked(nor, region);
  249. /* take the branch it is locked or in case of an error */
  250. if (locked)
  251. return locked;
  252. }
  253. return 0;
  254. }
  255. static int spi_nor_mtd_otp_read_write(struct mtd_info *mtd, loff_t ofs,
  256. size_t total_len, size_t *retlen,
  257. const u8 *buf, bool is_write)
  258. {
  259. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  260. const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
  261. const size_t rlen = spi_nor_otp_region_len(nor);
  262. loff_t rstart, rofs;
  263. unsigned int region;
  264. size_t len;
  265. int ret;
  266. if (ofs < 0 || ofs >= spi_nor_otp_size(nor))
  267. return 0;
  268. /* don't access beyond the end */
  269. total_len = min_t(size_t, total_len, spi_nor_otp_size(nor) - ofs);
  270. if (!total_len)
  271. return 0;
  272. ret = spi_nor_lock_and_prep(nor);
  273. if (ret)
  274. return ret;
  275. if (is_write) {
  276. ret = spi_nor_mtd_otp_range_is_locked(nor, ofs, total_len);
  277. if (ret < 0) {
  278. goto out;
  279. } else if (ret) {
  280. ret = -EROFS;
  281. goto out;
  282. }
  283. }
  284. while (total_len) {
  285. /*
  286. * The OTP regions are mapped into a contiguous area starting
  287. * at 0 as expected by the MTD layer. This will map the MTD
  288. * file offsets to the address of an OTP region as used in the
  289. * actual SPI commands.
  290. */
  291. region = spi_nor_otp_offset_to_region(nor, ofs);
  292. rstart = spi_nor_otp_region_start(nor, region);
  293. /*
  294. * The size of a OTP region is expected to be a power of two,
  295. * thus we can just mask the lower bits and get the offset into
  296. * a region.
  297. */
  298. rofs = ofs & (rlen - 1);
  299. /* don't access beyond one OTP region */
  300. len = min_t(size_t, total_len, rlen - rofs);
  301. if (is_write)
  302. ret = ops->write(nor, rstart + rofs, len, buf);
  303. else
  304. ret = ops->read(nor, rstart + rofs, len, (u8 *)buf);
  305. if (ret == 0)
  306. ret = -EIO;
  307. if (ret < 0)
  308. goto out;
  309. *retlen += ret;
  310. ofs += ret;
  311. buf += ret;
  312. total_len -= ret;
  313. }
  314. ret = 0;
  315. out:
  316. spi_nor_unlock_and_unprep(nor);
  317. return ret;
  318. }
  319. static int spi_nor_mtd_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  320. size_t *retlen, u8 *buf)
  321. {
  322. return spi_nor_mtd_otp_read_write(mtd, from, len, retlen, buf, false);
  323. }
  324. static int spi_nor_mtd_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
  325. size_t *retlen, const u8 *buf)
  326. {
  327. return spi_nor_mtd_otp_read_write(mtd, to, len, retlen, buf, true);
  328. }
  329. static int spi_nor_mtd_otp_erase(struct mtd_info *mtd, loff_t from, size_t len)
  330. {
  331. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  332. const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
  333. const size_t rlen = spi_nor_otp_region_len(nor);
  334. unsigned int region;
  335. loff_t rstart;
  336. int ret;
  337. /* OTP erase is optional */
  338. if (!ops->erase)
  339. return -EOPNOTSUPP;
  340. if (!len)
  341. return 0;
  342. if (from < 0 || (from + len) > spi_nor_otp_size(nor))
  343. return -EINVAL;
  344. /* the user has to explicitly ask for whole regions */
  345. if (!IS_ALIGNED(len, rlen) || !IS_ALIGNED(from, rlen))
  346. return -EINVAL;
  347. ret = spi_nor_lock_and_prep(nor);
  348. if (ret)
  349. return ret;
  350. ret = spi_nor_mtd_otp_range_is_locked(nor, from, len);
  351. if (ret < 0) {
  352. goto out;
  353. } else if (ret) {
  354. ret = -EROFS;
  355. goto out;
  356. }
  357. while (len) {
  358. region = spi_nor_otp_offset_to_region(nor, from);
  359. rstart = spi_nor_otp_region_start(nor, region);
  360. ret = ops->erase(nor, rstart);
  361. if (ret)
  362. goto out;
  363. len -= rlen;
  364. from += rlen;
  365. }
  366. out:
  367. spi_nor_unlock_and_unprep(nor);
  368. return ret;
  369. }
  370. static int spi_nor_mtd_otp_lock(struct mtd_info *mtd, loff_t from, size_t len)
  371. {
  372. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  373. const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
  374. const size_t rlen = spi_nor_otp_region_len(nor);
  375. unsigned int region;
  376. int ret;
  377. if (from < 0 || (from + len) > spi_nor_otp_size(nor))
  378. return -EINVAL;
  379. /* the user has to explicitly ask for whole regions */
  380. if (!IS_ALIGNED(len, rlen) || !IS_ALIGNED(from, rlen))
  381. return -EINVAL;
  382. ret = spi_nor_lock_and_prep(nor);
  383. if (ret)
  384. return ret;
  385. while (len) {
  386. region = spi_nor_otp_offset_to_region(nor, from);
  387. ret = ops->lock(nor, region);
  388. if (ret)
  389. goto out;
  390. len -= rlen;
  391. from += rlen;
  392. }
  393. out:
  394. spi_nor_unlock_and_unprep(nor);
  395. return ret;
  396. }
  397. void spi_nor_set_mtd_otp_ops(struct spi_nor *nor)
  398. {
  399. struct mtd_info *mtd = &nor->mtd;
  400. if (!nor->params->otp.ops)
  401. return;
  402. if (WARN_ON(!is_power_of_2(spi_nor_otp_region_len(nor))))
  403. return;
  404. /*
  405. * We only support user_prot callbacks (yet).
  406. *
  407. * Some SPI NOR flashes like Macronix ones can be ordered in two
  408. * different variants. One with a factory locked OTP area and one where
  409. * it is left to the user to write to it. The factory locked OTP is
  410. * usually preprogrammed with an "electrical serial number". We don't
  411. * support these for now.
  412. */
  413. mtd->_get_user_prot_info = spi_nor_mtd_otp_info;
  414. mtd->_read_user_prot_reg = spi_nor_mtd_otp_read;
  415. mtd->_write_user_prot_reg = spi_nor_mtd_otp_write;
  416. mtd->_lock_user_prot_reg = spi_nor_mtd_otp_lock;
  417. mtd->_erase_user_prot_reg = spi_nor_mtd_otp_erase;
  418. }