swp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SPI NOR Software Write Protection logic.
  4. *
  5. * Copyright (C) 2005, Intec Automation Inc.
  6. * Copyright (C) 2014, Freescale Semiconductor, Inc.
  7. */
  8. #include <linux/mtd/mtd.h>
  9. #include <linux/mtd/spi-nor.h>
  10. #include "core.h"
  11. static u8 spi_nor_get_sr_bp_mask(struct spi_nor *nor)
  12. {
  13. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  14. if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6)
  15. return mask | SR_BP3_BIT6;
  16. if (nor->flags & SNOR_F_HAS_4BIT_BP)
  17. return mask | SR_BP3;
  18. return mask;
  19. }
  20. static u8 spi_nor_get_sr_tb_mask(struct spi_nor *nor)
  21. {
  22. if (nor->flags & SNOR_F_HAS_SR_TB_BIT6)
  23. return SR_TB_BIT6;
  24. else
  25. return SR_TB_BIT5;
  26. }
  27. static u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor)
  28. {
  29. unsigned int bp_slots, bp_slots_needed;
  30. u8 mask = spi_nor_get_sr_bp_mask(nor);
  31. /* Reserved one for "protect none" and one for "protect all". */
  32. bp_slots = (1 << hweight8(mask)) - 2;
  33. bp_slots_needed = ilog2(nor->info->n_sectors);
  34. if (bp_slots_needed > bp_slots)
  35. return nor->info->sector_size <<
  36. (bp_slots_needed - bp_slots);
  37. else
  38. return nor->info->sector_size;
  39. }
  40. static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
  41. uint64_t *len)
  42. {
  43. struct mtd_info *mtd = &nor->mtd;
  44. u64 min_prot_len;
  45. u8 mask = spi_nor_get_sr_bp_mask(nor);
  46. u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
  47. u8 bp, val = sr & mask;
  48. if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6)
  49. val = (val & ~SR_BP3_BIT6) | SR_BP3;
  50. bp = val >> SR_BP_SHIFT;
  51. if (!bp) {
  52. /* No protection */
  53. *ofs = 0;
  54. *len = 0;
  55. return;
  56. }
  57. min_prot_len = spi_nor_get_min_prot_length_sr(nor);
  58. *len = min_prot_len << (bp - 1);
  59. if (*len > mtd->size)
  60. *len = mtd->size;
  61. if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask)
  62. *ofs = 0;
  63. else
  64. *ofs = mtd->size - *len;
  65. }
  66. /*
  67. * Return true if the entire region is locked (if @locked is true) or unlocked
  68. * (if @locked is false); false otherwise.
  69. */
  70. static bool spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs,
  71. uint64_t len, u8 sr, bool locked)
  72. {
  73. loff_t lock_offs, lock_offs_max, offs_max;
  74. uint64_t lock_len;
  75. if (!len)
  76. return true;
  77. spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len);
  78. lock_offs_max = lock_offs + lock_len;
  79. offs_max = ofs + len;
  80. if (locked)
  81. /* Requested range is a sub-range of locked range */
  82. return (offs_max <= lock_offs_max) && (ofs >= lock_offs);
  83. else
  84. /* Requested range does not overlap with locked range */
  85. return (ofs >= lock_offs_max) || (offs_max <= lock_offs);
  86. }
  87. static bool spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  88. u8 sr)
  89. {
  90. return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true);
  91. }
  92. static bool spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs,
  93. uint64_t len, u8 sr)
  94. {
  95. return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false);
  96. }
  97. /*
  98. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  99. * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in the status
  100. * register
  101. * (SR). Does not support these features found in newer SR bitfields:
  102. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  103. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  104. *
  105. * Support for the following is provided conditionally for some flash:
  106. * - TB: top/bottom protect
  107. *
  108. * Sample table portion for 8MB flash (Winbond w25q64fw):
  109. *
  110. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  111. * --------------------------------------------------------------------------
  112. * X | X | 0 | 0 | 0 | NONE | NONE
  113. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  114. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  115. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  116. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  117. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  118. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  119. * X | X | 1 | 1 | 1 | 8 MB | ALL
  120. * ------|-------|-------|-------|-------|---------------|-------------------
  121. * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
  122. * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
  123. * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
  124. * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
  125. * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
  126. * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
  127. *
  128. * Returns negative on errors, 0 on success.
  129. */
  130. static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  131. {
  132. struct mtd_info *mtd = &nor->mtd;
  133. u64 min_prot_len;
  134. int ret, status_old, status_new;
  135. u8 mask = spi_nor_get_sr_bp_mask(nor);
  136. u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
  137. u8 pow, val;
  138. loff_t lock_len;
  139. bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  140. bool use_top;
  141. ret = spi_nor_read_sr(nor, nor->bouncebuf);
  142. if (ret)
  143. return ret;
  144. status_old = nor->bouncebuf[0];
  145. /* If nothing in our range is unlocked, we don't need to do anything */
  146. if (spi_nor_is_locked_sr(nor, ofs, len, status_old))
  147. return 0;
  148. /* If anything below us is unlocked, we can't use 'bottom' protection */
  149. if (!spi_nor_is_locked_sr(nor, 0, ofs, status_old))
  150. can_be_bottom = false;
  151. /* If anything above us is unlocked, we can't use 'top' protection */
  152. if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
  153. status_old))
  154. can_be_top = false;
  155. if (!can_be_bottom && !can_be_top)
  156. return -EINVAL;
  157. /* Prefer top, if both are valid */
  158. use_top = can_be_top;
  159. /* lock_len: length of region that should end up locked */
  160. if (use_top)
  161. lock_len = mtd->size - ofs;
  162. else
  163. lock_len = ofs + len;
  164. if (lock_len == mtd->size) {
  165. val = mask;
  166. } else {
  167. min_prot_len = spi_nor_get_min_prot_length_sr(nor);
  168. pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
  169. val = pow << SR_BP_SHIFT;
  170. if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
  171. val = (val & ~SR_BP3) | SR_BP3_BIT6;
  172. if (val & ~mask)
  173. return -EINVAL;
  174. /* Don't "lock" with no region! */
  175. if (!(val & mask))
  176. return -EINVAL;
  177. }
  178. status_new = (status_old & ~mask & ~tb_mask) | val;
  179. /* Disallow further writes if WP pin is asserted */
  180. status_new |= SR_SRWD;
  181. if (!use_top)
  182. status_new |= tb_mask;
  183. /* Don't bother if they're the same */
  184. if (status_new == status_old)
  185. return 0;
  186. /* Only modify protection if it will not unlock other areas */
  187. if ((status_new & mask) < (status_old & mask))
  188. return -EINVAL;
  189. return spi_nor_write_sr_and_check(nor, status_new);
  190. }
  191. /*
  192. * Unlock a region of the flash. See spi_nor_sr_lock() for more info
  193. *
  194. * Returns negative on errors, 0 on success.
  195. */
  196. static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  197. {
  198. struct mtd_info *mtd = &nor->mtd;
  199. u64 min_prot_len;
  200. int ret, status_old, status_new;
  201. u8 mask = spi_nor_get_sr_bp_mask(nor);
  202. u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
  203. u8 pow, val;
  204. loff_t lock_len;
  205. bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  206. bool use_top;
  207. ret = spi_nor_read_sr(nor, nor->bouncebuf);
  208. if (ret)
  209. return ret;
  210. status_old = nor->bouncebuf[0];
  211. /* If nothing in our range is locked, we don't need to do anything */
  212. if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old))
  213. return 0;
  214. /* If anything below us is locked, we can't use 'top' protection */
  215. if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old))
  216. can_be_top = false;
  217. /* If anything above us is locked, we can't use 'bottom' protection */
  218. if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
  219. status_old))
  220. can_be_bottom = false;
  221. if (!can_be_bottom && !can_be_top)
  222. return -EINVAL;
  223. /* Prefer top, if both are valid */
  224. use_top = can_be_top;
  225. /* lock_len: length of region that should remain locked */
  226. if (use_top)
  227. lock_len = mtd->size - (ofs + len);
  228. else
  229. lock_len = ofs;
  230. if (lock_len == 0) {
  231. val = 0; /* fully unlocked */
  232. } else {
  233. min_prot_len = spi_nor_get_min_prot_length_sr(nor);
  234. pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
  235. val = pow << SR_BP_SHIFT;
  236. if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
  237. val = (val & ~SR_BP3) | SR_BP3_BIT6;
  238. /* Some power-of-two sizes are not supported */
  239. if (val & ~mask)
  240. return -EINVAL;
  241. }
  242. status_new = (status_old & ~mask & ~tb_mask) | val;
  243. /* Don't protect status register if we're fully unlocked */
  244. if (lock_len == 0)
  245. status_new &= ~SR_SRWD;
  246. if (!use_top)
  247. status_new |= tb_mask;
  248. /* Don't bother if they're the same */
  249. if (status_new == status_old)
  250. return 0;
  251. /* Only modify protection if it will not lock other areas */
  252. if ((status_new & mask) > (status_old & mask))
  253. return -EINVAL;
  254. return spi_nor_write_sr_and_check(nor, status_new);
  255. }
  256. /*
  257. * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
  258. * for more info.
  259. *
  260. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  261. * negative on errors.
  262. */
  263. static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
  264. {
  265. int ret;
  266. ret = spi_nor_read_sr(nor, nor->bouncebuf);
  267. if (ret)
  268. return ret;
  269. return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
  270. }
  271. static const struct spi_nor_locking_ops spi_nor_sr_locking_ops = {
  272. .lock = spi_nor_sr_lock,
  273. .unlock = spi_nor_sr_unlock,
  274. .is_locked = spi_nor_sr_is_locked,
  275. };
  276. void spi_nor_init_default_locking_ops(struct spi_nor *nor)
  277. {
  278. nor->params->locking_ops = &spi_nor_sr_locking_ops;
  279. }
  280. static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  281. {
  282. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  283. int ret;
  284. ret = spi_nor_lock_and_prep(nor);
  285. if (ret)
  286. return ret;
  287. ret = nor->params->locking_ops->lock(nor, ofs, len);
  288. spi_nor_unlock_and_unprep(nor);
  289. return ret;
  290. }
  291. static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  292. {
  293. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  294. int ret;
  295. ret = spi_nor_lock_and_prep(nor);
  296. if (ret)
  297. return ret;
  298. ret = nor->params->locking_ops->unlock(nor, ofs, len);
  299. spi_nor_unlock_and_unprep(nor);
  300. return ret;
  301. }
  302. static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  303. {
  304. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  305. int ret;
  306. ret = spi_nor_lock_and_prep(nor);
  307. if (ret)
  308. return ret;
  309. ret = nor->params->locking_ops->is_locked(nor, ofs, len);
  310. spi_nor_unlock_and_unprep(nor);
  311. return ret;
  312. }
  313. /**
  314. * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
  315. * @nor: pointer to a 'struct spi_nor'.
  316. *
  317. * Some SPI NOR flashes are write protected by default after a power-on reset
  318. * cycle, in order to avoid inadvertent writes during power-up. Backward
  319. * compatibility imposes to unlock the entire flash memory array at power-up
  320. * by default.
  321. *
  322. * Unprotecting the entire flash array will fail for boards which are hardware
  323. * write-protected. Thus any errors are ignored.
  324. */
  325. void spi_nor_try_unlock_all(struct spi_nor *nor)
  326. {
  327. int ret;
  328. if (!(nor->flags & SNOR_F_HAS_LOCK))
  329. return;
  330. dev_dbg(nor->dev, "Unprotecting entire flash array\n");
  331. ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size);
  332. if (ret)
  333. dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
  334. }
  335. void spi_nor_set_mtd_locking_ops(struct spi_nor *nor)
  336. {
  337. struct mtd_info *mtd = &nor->mtd;
  338. if (!nor->params->locking_ops)
  339. return;
  340. mtd->_lock = spi_nor_lock;
  341. mtd->_unlock = spi_nor_unlock;
  342. mtd->_is_locked = spi_nor_is_locked;
  343. }