062-v4.11-0001-mtd-spi-nor-Add-support-for-S3AN-spi-nor-devices.patch 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. From 61cba34bd6c1bddfc38f94cc3f80bdfefcc3393b Mon Sep 17 00:00:00 2001
  2. From: Ricardo Ribalda <[email protected]>
  3. Date: Fri, 2 Dec 2016 12:31:44 +0100
  4. Subject: [PATCH] mtd: spi-nor: Add support for S3AN spi-nor devices
  5. Xilinx Spartan-3AN FPGAs contain an In-System Flash where they keep
  6. their configuration data and (optionally) some user data.
  7. The protocol of this flash follows most of the spi-nor standard. With
  8. the following differences:
  9. - Page size might not be a power of two.
  10. - The address calculation (default addressing mode).
  11. - The spi nor commands used.
  12. Protocol is described on Xilinx User Guide UG333
  13. Signed-off-by: Ricardo Ribalda Delgado <[email protected]>
  14. Cc: Boris Brezillon <[email protected]>
  15. Cc: Brian Norris <[email protected]>
  16. Cc: Marek Vasut <[email protected]>
  17. Reviewed-by: Marek Vasut <[email protected]>
  18. Signed-off-by: Cyrille Pitchen <[email protected]>
  19. ---
  20. drivers/mtd/spi-nor/spi-nor.c | 154 ++++++++++++++++++++++++++++++++++++++++--
  21. include/linux/mtd/spi-nor.h | 12 ++++
  22. 2 files changed, 161 insertions(+), 5 deletions(-)
  23. --- a/drivers/mtd/spi-nor/spi-nor.c
  24. +++ b/drivers/mtd/spi-nor/spi-nor.c
  25. @@ -75,6 +75,12 @@ struct flash_info {
  26. * bit. Must be used with
  27. * SPI_NOR_HAS_LOCK.
  28. */
  29. +#define SPI_S3AN BIT(10) /*
  30. + * Xilinx Spartan 3AN In-System Flash
  31. + * (MFR cannot be used for probing
  32. + * because it has the same value as
  33. + * ATMEL flashes)
  34. + */
  35. };
  36. #define JEDEC_MFR(info) ((info)->id[0])
  37. @@ -217,6 +223,21 @@ static inline int set_4byte(struct spi_n
  38. return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
  39. }
  40. }
  41. +
  42. +static int s3an_sr_ready(struct spi_nor *nor)
  43. +{
  44. + int ret;
  45. + u8 val;
  46. +
  47. + ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
  48. + if (ret < 0) {
  49. + dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
  50. + return ret;
  51. + }
  52. +
  53. + return !!(val & XSR_RDY);
  54. +}
  55. +
  56. static inline int spi_nor_sr_ready(struct spi_nor *nor)
  57. {
  58. int sr = read_sr(nor);
  59. @@ -238,7 +259,11 @@ static inline int spi_nor_fsr_ready(stru
  60. static int spi_nor_ready(struct spi_nor *nor)
  61. {
  62. int sr, fsr;
  63. - sr = spi_nor_sr_ready(nor);
  64. +
  65. + if (nor->flags & SNOR_F_READY_XSR_RDY)
  66. + sr = s3an_sr_ready(nor);
  67. + else
  68. + sr = spi_nor_sr_ready(nor);
  69. if (sr < 0)
  70. return sr;
  71. fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
  72. @@ -320,6 +345,24 @@ static void spi_nor_unlock_and_unprep(st
  73. }
  74. /*
  75. + * This code converts an address to the Default Address Mode, that has non
  76. + * power of two page sizes. We must support this mode because it is the default
  77. + * mode supported by Xilinx tools, it can access the whole flash area and
  78. + * changing over to the Power-of-two mode is irreversible and corrupts the
  79. + * original data.
  80. + * Addr can safely be unsigned int, the biggest S3AN device is smaller than
  81. + * 4 MiB.
  82. + */
  83. +static loff_t spi_nor_s3an_addr_convert(struct spi_nor *nor, unsigned int addr)
  84. +{
  85. + unsigned int offset = addr;
  86. +
  87. + offset %= nor->page_size;
  88. +
  89. + return ((addr - offset) << 1) | offset;
  90. +}
  91. +
  92. +/*
  93. * Initiate the erasure of a single sector
  94. */
  95. static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  96. @@ -327,6 +370,9 @@ static int spi_nor_erase_sector(struct s
  97. u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
  98. int i;
  99. + if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  100. + addr = spi_nor_s3an_addr_convert(nor, addr);
  101. +
  102. if (nor->erase)
  103. return nor->erase(nor, addr);
  104. @@ -368,7 +414,7 @@ static int spi_nor_erase(struct mtd_info
  105. return ret;
  106. /* whole-chip erase? */
  107. - if (len == mtd->size) {
  108. + if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
  109. unsigned long timeout;
  110. write_enable(nor);
  111. @@ -782,6 +828,19 @@ static int spi_nor_is_locked(struct mtd_
  112. .addr_width = (_addr_width), \
  113. .flags = (_flags),
  114. +#define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
  115. + .id = { \
  116. + ((_jedec_id) >> 16) & 0xff, \
  117. + ((_jedec_id) >> 8) & 0xff, \
  118. + (_jedec_id) & 0xff \
  119. + }, \
  120. + .id_len = 3, \
  121. + .sector_size = (8*_page_size), \
  122. + .n_sectors = (_n_sectors), \
  123. + .page_size = _page_size, \
  124. + .addr_width = 3, \
  125. + .flags = SPI_NOR_NO_FR | SPI_S3AN,
  126. +
  127. /* NOTE: double check command sets and memory organization when you add
  128. * more nor chips. This current list focusses on newer chips, which
  129. * have been converging on command sets which including JEDEC ID.
  130. @@ -1014,6 +1073,13 @@ static const struct flash_info spi_nor_i
  131. { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  132. { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  133. { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  134. +
  135. + /* Xilinx S3AN Internal Flash */
  136. + { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
  137. + { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
  138. + { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
  139. + { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
  140. + { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
  141. { },
  142. };
  143. @@ -1054,7 +1120,12 @@ static int spi_nor_read(struct mtd_info
  144. return ret;
  145. while (len) {
  146. - ret = nor->read(nor, from, len, buf);
  147. + loff_t addr = from;
  148. +
  149. + if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  150. + addr = spi_nor_s3an_addr_convert(nor, addr);
  151. +
  152. + ret = nor->read(nor, addr, len, buf);
  153. if (ret == 0) {
  154. /* We shouldn't see 0-length reads */
  155. ret = -EIO;
  156. @@ -1175,8 +1246,23 @@ static int spi_nor_write(struct mtd_info
  157. for (i = 0; i < len; ) {
  158. ssize_t written;
  159. + loff_t addr = to + i;
  160. - page_offset = (to + i) & (nor->page_size - 1);
  161. + /*
  162. + * If page_size is a power of two, the offset can be quickly
  163. + * calculated with an AND operation. On the other cases we
  164. + * need to do a modulus operation (more expensive).
  165. + * Power of two numbers have only one bit set and we can use
  166. + * the instruction hweight32 to detect if we need to do a
  167. + * modulus (do_div()) or not.
  168. + */
  169. + if (hweight32(nor->page_size) == 1) {
  170. + page_offset = addr & (nor->page_size - 1);
  171. + } else {
  172. + uint64_t aux = addr;
  173. +
  174. + page_offset = do_div(aux, nor->page_size);
  175. + }
  176. WARN_ONCE(page_offset,
  177. "Writing at offset %zu into a NOR page. Writing partial pages may decrease reliability and increase wear of NOR flash.",
  178. page_offset);
  179. @@ -1184,8 +1270,11 @@ static int spi_nor_write(struct mtd_info
  180. page_remain = min_t(size_t,
  181. nor->page_size - page_offset, len - i);
  182. + if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
  183. + addr = spi_nor_s3an_addr_convert(nor, addr);
  184. +
  185. write_enable(nor);
  186. - ret = nor->write(nor, to + i, page_remain, buf + i);
  187. + ret = nor->write(nor, addr, page_remain, buf + i);
  188. if (ret < 0)
  189. goto write_err;
  190. written = ret;
  191. @@ -1312,6 +1401,47 @@ static int spi_nor_check(struct spi_nor
  192. return 0;
  193. }
  194. +static int s3an_nor_scan(const struct flash_info *info, struct spi_nor *nor)
  195. +{
  196. + int ret;
  197. + u8 val;
  198. +
  199. + ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
  200. + if (ret < 0) {
  201. + dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
  202. + return ret;
  203. + }
  204. +
  205. + nor->erase_opcode = SPINOR_OP_XSE;
  206. + nor->program_opcode = SPINOR_OP_XPP;
  207. + nor->read_opcode = SPINOR_OP_READ;
  208. + nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
  209. +
  210. + /*
  211. + * This flashes have a page size of 264 or 528 bytes (known as
  212. + * Default addressing mode). It can be changed to a more standard
  213. + * Power of two mode where the page size is 256/512. This comes
  214. + * with a price: there is 3% less of space, the data is corrupted
  215. + * and the page size cannot be changed back to default addressing
  216. + * mode.
  217. + *
  218. + * The current addressing mode can be read from the XRDSR register
  219. + * and should not be changed, because is a destructive operation.
  220. + */
  221. + if (val & XSR_PAGESIZE) {
  222. + /* Flash in Power of 2 mode */
  223. + nor->page_size = (nor->page_size == 264) ? 256 : 512;
  224. + nor->mtd.writebufsize = nor->page_size;
  225. + nor->mtd.size = 8 * nor->page_size * info->n_sectors;
  226. + nor->mtd.erasesize = 8 * nor->page_size;
  227. + } else {
  228. + /* Flash in Default addressing mode */
  229. + nor->flags |= SNOR_F_S3AN_ADDR_DEFAULT;
  230. + }
  231. +
  232. + return 0;
  233. +}
  234. +
  235. int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
  236. {
  237. const struct flash_info *info = NULL;
  238. @@ -1360,6 +1490,14 @@ int spi_nor_scan(struct spi_nor *nor, co
  239. mutex_init(&nor->lock);
  240. /*
  241. + * Make sure the XSR_RDY flag is set before calling
  242. + * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
  243. + * with Atmel spi-nor
  244. + */
  245. + if (info->flags & SPI_S3AN)
  246. + nor->flags |= SNOR_F_READY_XSR_RDY;
  247. +
  248. + /*
  249. * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
  250. * with the software protection bits set
  251. */
  252. @@ -1517,6 +1655,12 @@ int spi_nor_scan(struct spi_nor *nor, co
  253. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  254. + if (info->flags & SPI_S3AN) {
  255. + ret = s3an_nor_scan(info, nor);
  256. + if (ret)
  257. + return ret;
  258. + }
  259. +
  260. dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  261. (long long)mtd->size >> 10);
  262. --- a/include/linux/mtd/spi-nor.h
  263. +++ b/include/linux/mtd/spi-nor.h
  264. @@ -68,6 +68,15 @@
  265. #define SPINOR_OP_WRDI 0x04 /* Write disable */
  266. #define SPINOR_OP_AAI_WP 0xad /* Auto address increment word program */
  267. +/* Used for S3AN flashes only */
  268. +#define SPINOR_OP_XSE 0x50 /* Sector erase */
  269. +#define SPINOR_OP_XPP 0x82 /* Page program */
  270. +#define SPINOR_OP_XRDSR 0xd7 /* Read status register */
  271. +
  272. +#define XSR_PAGESIZE BIT(0) /* Page size in Po2 or Linear */
  273. +#define XSR_RDY BIT(7) /* Ready */
  274. +
  275. +
  276. /* Used for Macronix and Winbond flashes. */
  277. #define SPINOR_OP_EN4B 0xb7 /* Enter 4-byte mode */
  278. #define SPINOR_OP_EX4B 0xe9 /* Exit 4-byte mode */
  279. @@ -119,6 +128,9 @@ enum spi_nor_ops {
  280. enum spi_nor_option_flags {
  281. SNOR_F_USE_FSR = BIT(0),
  282. SNOR_F_HAS_SR_TB = BIT(1),
  283. + SNOR_F_NO_OP_CHIP_ERASE = BIT(2),
  284. + SNOR_F_S3AN_ADDR_DEFAULT = BIT(3),
  285. + SNOR_F_READY_XSR_RDY = BIT(4),
  286. };
  287. /**