1095-mtd-spi-nor-simplify-write-loop.patch 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. From 93b40e12f7e580a41c4aee5597579cc539fd8544 Mon Sep 17 00:00:00 2001
  2. From: Michal Suchanek <hramrach@gmail.com>
  3. Date: Wed, 2 Dec 2015 10:38:20 +0000
  4. Subject: [PATCH 095/113] mtd: spi-nor: simplify write loop
  5. The spi-nor write loop assumes that what is passed to the hardware
  6. driver write() is what gets written.
  7. When write() writes less than page size at once data is dropped on the
  8. floor. Check the amount of data writen and exit if it does not match
  9. requested amount.
  10. Signed-off-by: Michal Suchanek <hramrach@gmail.com>
  11. Signed-off-by: Hou Zhiqiang <Zhiqiang.Hou@freescale.com>
  12. ---
  13. drivers/mtd/spi-nor/spi-nor.c | 58 ++++++++++++++++++-----------------------
  14. 1 file changed, 25 insertions(+), 33 deletions(-)
  15. --- a/drivers/mtd/spi-nor/spi-nor.c
  16. +++ b/drivers/mtd/spi-nor/spi-nor.c
  17. @@ -1025,8 +1025,8 @@ static int spi_nor_write(struct mtd_info
  18. size_t *retlen, const u_char *buf)
  19. {
  20. struct spi_nor *nor = mtd_to_spi_nor(mtd);
  21. - u32 page_offset, page_size, i;
  22. - int ret;
  23. + size_t page_offset, page_remain, i;
  24. + ssize_t ret;
  25. dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  26. @@ -1034,45 +1034,37 @@ static int spi_nor_write(struct mtd_info
  27. if (ret)
  28. return ret;
  29. - write_enable(nor);
  30. + for (i = 0; i < len; ) {
  31. + ssize_t written;
  32. - page_offset = to & (nor->page_size - 1);
  33. -
  34. - /* do all the bytes fit onto one page? */
  35. - if (page_offset + len <= nor->page_size) {
  36. - ret = nor->write(nor, to, len, buf);
  37. - if (ret < 0)
  38. - goto write_err;
  39. - *retlen += ret;
  40. - } else {
  41. + page_offset = to & (nor->page_size - 1);
  42. + WARN_ONCE(page_offset,
  43. + "Writing at offset %zu into a NOR page. Writing partial pages may decrease reliability and increase wear of NOR flash.",
  44. + page_offset);
  45. /* the size of data remaining on the first page */
  46. - page_size = nor->page_size - page_offset;
  47. - ret = nor->write(nor, to, page_size, buf);
  48. + page_remain = min_t(size_t,
  49. + nor->page_size - page_offset, len - i);
  50. +
  51. + write_enable(nor);
  52. + ret = nor->write(nor, to + i, page_remain, buf + i);
  53. if (ret < 0)
  54. goto write_err;
  55. - *retlen += ret;
  56. + written = ret;
  57. - /* write everything in nor->page_size chunks */
  58. - for (i = ret; i < len; ) {
  59. - page_size = len - i;
  60. - if (page_size > nor->page_size)
  61. - page_size = nor->page_size;
  62. -
  63. - ret = spi_nor_wait_till_ready(nor);
  64. - if (ret)
  65. - goto write_err;
  66. -
  67. - write_enable(nor);
  68. -
  69. - ret = nor->write(nor, to + i, page_size, buf + i);
  70. - if (ret < 0)
  71. - goto write_err;
  72. - *retlen += ret;
  73. - i += ret;
  74. + ret = spi_nor_wait_till_ready(nor);
  75. + if (ret)
  76. + goto write_err;
  77. + *retlen += written;
  78. + i += written;
  79. + if (written != page_remain) {
  80. + dev_err(nor->dev,
  81. + "While writing %zu bytes written %zd bytes\n",
  82. + page_remain, written);
  83. + ret = -EIO;
  84. + goto write_err;
  85. }
  86. }
  87. - ret = spi_nor_wait_till_ready(nor);
  88. write_err:
  89. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  90. return ret;