090-m25p80_spi-nor_update_to_4.4rc1.patch 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. --- a/drivers/mtd/devices/m25p80.c
  2. +++ b/drivers/mtd/devices/m25p80.c
  3. @@ -31,7 +31,6 @@
  4. struct m25p {
  5. struct spi_device *spi;
  6. struct spi_nor spi_nor;
  7. - struct mtd_info mtd;
  8. u8 command[MAX_CMD_SIZE];
  9. };
  10. @@ -62,8 +61,7 @@ static int m25p_cmdsz(struct spi_nor *no
  11. return 1 + nor->addr_width;
  12. }
  13. -static int m25p80_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len,
  14. - int wr_en)
  15. +static int m25p80_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  16. {
  17. struct m25p *flash = nor->priv;
  18. struct spi_device *spi = flash->spi;
  19. @@ -159,7 +157,7 @@ static int m25p80_erase(struct spi_nor *
  20. struct m25p *flash = nor->priv;
  21. dev_dbg(nor->dev, "%dKiB at 0x%08x\n",
  22. - flash->mtd.erasesize / 1024, (u32)offset);
  23. + flash->spi_nor.mtd.erasesize / 1024, (u32)offset);
  24. /* Set up command buffer. */
  25. flash->command[0] = nor->erase_opcode;
  26. @@ -201,11 +199,10 @@ static int m25p_probe(struct spi_device
  27. nor->read_reg = m25p80_read_reg;
  28. nor->dev = &spi->dev;
  29. - nor->mtd = &flash->mtd;
  30. + nor->flash_node = spi->dev.of_node;
  31. nor->priv = flash;
  32. spi_set_drvdata(spi, flash);
  33. - flash->mtd.priv = nor;
  34. flash->spi = spi;
  35. if (spi->mode & SPI_RX_QUAD)
  36. @@ -214,7 +211,7 @@ static int m25p_probe(struct spi_device
  37. mode = SPI_NOR_DUAL;
  38. if (data && data->name)
  39. - flash->mtd.name = data->name;
  40. + nor->mtd.name = data->name;
  41. /* For some (historical?) reason many platforms provide two different
  42. * names in flash_platform_data: "name" and "type". Quite often name is
  43. @@ -223,8 +220,6 @@ static int m25p_probe(struct spi_device
  44. */
  45. if (data && data->type)
  46. flash_name = data->type;
  47. - else if (!strcmp(spi->modalias, "spi-nor"))
  48. - flash_name = NULL; /* auto-detect */
  49. else
  50. flash_name = spi->modalias;
  51. @@ -234,7 +229,7 @@ static int m25p_probe(struct spi_device
  52. ppdata.of_node = spi->dev.of_node;
  53. - return mtd_device_parse_register(&flash->mtd, NULL, &ppdata,
  54. + return mtd_device_parse_register(&nor->mtd, NULL, &ppdata,
  55. data ? data->parts : NULL,
  56. data ? data->nr_parts : 0);
  57. }
  58. @@ -245,7 +240,7 @@ static int m25p_remove(struct spi_device
  59. struct m25p *flash = spi_get_drvdata(spi);
  60. /* Clean up MTD stuff. */
  61. - return mtd_device_unregister(&flash->mtd);
  62. + return mtd_device_unregister(&flash->spi_nor.mtd);
  63. }
  64. /*
  65. @@ -261,59 +256,52 @@ static int m25p_remove(struct spi_device
  66. * keep them available as module aliases for existing platforms.
  67. */
  68. static const struct spi_device_id m25p_ids[] = {
  69. - {"at25fs010"}, {"at25fs040"}, {"at25df041a"}, {"at25df321a"},
  70. - {"at25df641"}, {"at26f004"}, {"at26df081a"}, {"at26df161a"},
  71. - {"at26df321"}, {"at45db081d"},
  72. - {"en25f32"}, {"en25p32"}, {"en25q32b"}, {"en25p64"},
  73. - {"en25q64"}, {"en25qh128"}, {"en25qh256"},
  74. - {"f25l32pa"},
  75. - {"mr25h256"}, {"mr25h10"},
  76. - {"gd25q32"}, {"gd25q64"},
  77. - {"160s33b"}, {"320s33b"}, {"640s33b"},
  78. - {"mx25l2005a"}, {"mx25l4005a"}, {"mx25l8005"}, {"mx25l1606e"},
  79. - {"mx25l3205d"}, {"mx25l3255e"}, {"mx25l6405d"}, {"mx25l12805d"},
  80. - {"mx25l12855e"},{"mx25l25635e"},{"mx25l25655e"},{"mx66l51235l"},
  81. - {"mx66l1g55g"},
  82. - {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q256a"},
  83. - {"n25q512a"}, {"n25q512ax3"}, {"n25q00"},
  84. - {"pm25lv512"}, {"pm25lv010"}, {"pm25lq032"},
  85. - {"s25sl032p"}, {"s25sl064p"}, {"s25fl256s0"}, {"s25fl256s1"},
  86. - {"s25fl512s"}, {"s70fl01gs"}, {"s25sl12800"}, {"s25sl12801"},
  87. - {"s25fl129p0"}, {"s25fl129p1"}, {"s25sl004a"}, {"s25sl008a"},
  88. - {"s25sl016a"}, {"s25sl032a"}, {"s25sl064a"}, {"s25fl008k"},
  89. - {"s25fl016k"}, {"s25fl064k"}, {"s25fl132k"},
  90. - {"sst25vf040b"},{"sst25vf080b"},{"sst25vf016b"},{"sst25vf032b"},
  91. - {"sst25vf064c"},{"sst25wf512"}, {"sst25wf010"}, {"sst25wf020"},
  92. - {"sst25wf040"},
  93. - {"m25p05"}, {"m25p10"}, {"m25p20"}, {"m25p40"},
  94. - {"m25p80"}, {"m25p16"}, {"m25p32"}, {"m25p64"},
  95. - {"m25p128"}, {"n25q032"},
  96. + /*
  97. + * Entries not used in DTs that should be safe to drop after replacing
  98. + * them with "nor-jedec" in platform data.
  99. + */
  100. + {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
  101. +
  102. + /*
  103. + * Entries that were used in DTs without "nor-jedec" fallback and should
  104. + * be kept for backward compatibility.
  105. + */
  106. + {"at25df321a"}, {"at25df641"}, {"at26df081a"},
  107. + {"mr25h256"},
  108. + {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
  109. + {"mx25l25635e"},{"mx66l51235l"},
  110. + {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
  111. + {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
  112. + {"s25fl064k"},
  113. + {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
  114. + {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
  115. + {"m25p64"}, {"m25p128"},
  116. + {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
  117. + {"w25q80bl"}, {"w25q128"}, {"w25q256"},
  118. +
  119. + /* Flashes that can't be detected using JEDEC */
  120. {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
  121. {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
  122. {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
  123. - {"m45pe10"}, {"m45pe80"}, {"m45pe16"},
  124. - {"m25pe20"}, {"m25pe80"}, {"m25pe16"},
  125. - {"m25px16"}, {"m25px32"}, {"m25px32-s0"}, {"m25px32-s1"},
  126. - {"m25px64"}, {"m25px80"},
  127. - {"w25x10"}, {"w25x20"}, {"w25x40"}, {"w25x80"},
  128. - {"w25x16"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
  129. - {"w25x64"}, {"w25q64"}, {"w25q80"}, {"w25q80bl"},
  130. - {"w25q128"}, {"w25q256"}, {"cat25c11"},
  131. - {"cat25c03"}, {"cat25c09"}, {"cat25c17"}, {"cat25128"},
  132. - /*
  133. - * Generic support for SPI NOR that can be identified by the JEDEC READ
  134. - * ID opcode (0x9F). Use this, if possible.
  135. - */
  136. - {"spi-nor"},
  137. { },
  138. };
  139. MODULE_DEVICE_TABLE(spi, m25p_ids);
  140. +static const struct of_device_id m25p_of_table[] = {
  141. + /*
  142. + * Generic compatibility for SPI NOR that can be identified by the
  143. + * JEDEC READ ID opcode (0x9F). Use this, if possible.
  144. + */
  145. + { .compatible = "jedec,spi-nor" },
  146. + {}
  147. +};
  148. +MODULE_DEVICE_TABLE(of, m25p_of_table);
  149. +
  150. static struct spi_driver m25p80_driver = {
  151. .driver = {
  152. .name = "m25p80",
  153. - .owner = THIS_MODULE,
  154. + .of_match_table = m25p_of_table,
  155. },
  156. .id_table = m25p_ids,
  157. .probe = m25p_probe,
  158. --- a/drivers/mtd/spi-nor/spi-nor.c
  159. +++ b/drivers/mtd/spi-nor/spi-nor.c
  160. @@ -16,19 +16,32 @@
  161. #include <linux/device.h>
  162. #include <linux/mutex.h>
  163. #include <linux/math64.h>
  164. +#include <linux/sizes.h>
  165. -#include <linux/mtd/cfi.h>
  166. #include <linux/mtd/mtd.h>
  167. #include <linux/of_platform.h>
  168. #include <linux/spi/flash.h>
  169. #include <linux/mtd/spi-nor.h>
  170. /* Define max times to check status register before we give up. */
  171. -#define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
  172. +
  173. +/*
  174. + * For everything but full-chip erase; probably could be much smaller, but kept
  175. + * around for safety for now
  176. + */
  177. +#define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
  178. +
  179. +/*
  180. + * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
  181. + * for larger flash
  182. + */
  183. +#define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  184. #define SPI_NOR_MAX_ID_LEN 6
  185. struct flash_info {
  186. + char *name;
  187. +
  188. /*
  189. * This array stores the ID bytes.
  190. * The first three bytes are the JEDIC ID.
  191. @@ -59,7 +72,7 @@ struct flash_info {
  192. #define JEDEC_MFR(info) ((info)->id[0])
  193. -static const struct spi_device_id *spi_nor_match_id(const char *name);
  194. +static const struct flash_info *spi_nor_match_id(const char *name);
  195. /*
  196. * Read the status register, returning its value in the location
  197. @@ -143,7 +156,7 @@ static inline int spi_nor_read_dummy_cyc
  198. static inline int write_sr(struct spi_nor *nor, u8 val)
  199. {
  200. nor->cmd_buf[0] = val;
  201. - return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
  202. + return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
  203. }
  204. /*
  205. @@ -152,7 +165,7 @@ static inline int write_sr(struct spi_no
  206. */
  207. static inline int write_enable(struct spi_nor *nor)
  208. {
  209. - return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
  210. + return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
  211. }
  212. /*
  213. @@ -160,7 +173,7 @@ static inline int write_enable(struct sp
  214. */
  215. static inline int write_disable(struct spi_nor *nor)
  216. {
  217. - return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0, 0);
  218. + return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
  219. }
  220. static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
  221. @@ -169,7 +182,7 @@ static inline struct spi_nor *mtd_to_spi
  222. }
  223. /* Enable/disable 4-byte addressing mode. */
  224. -static inline int set_4byte(struct spi_nor *nor, struct flash_info *info,
  225. +static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
  226. int enable)
  227. {
  228. int status;
  229. @@ -177,16 +190,16 @@ static inline int set_4byte(struct spi_n
  230. u8 cmd;
  231. switch (JEDEC_MFR(info)) {
  232. - case CFI_MFR_ST: /* Micron, actually */
  233. + case SNOR_MFR_MICRON:
  234. /* Some Micron need WREN command; all will accept it */
  235. need_wren = true;
  236. - case CFI_MFR_MACRONIX:
  237. - case 0xEF /* winbond */:
  238. + case SNOR_MFR_MACRONIX:
  239. + case SNOR_MFR_WINBOND:
  240. if (need_wren)
  241. write_enable(nor);
  242. cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
  243. - status = nor->write_reg(nor, cmd, NULL, 0, 0);
  244. + status = nor->write_reg(nor, cmd, NULL, 0);
  245. if (need_wren)
  246. write_disable(nor);
  247. @@ -194,7 +207,7 @@ static inline int set_4byte(struct spi_n
  248. default:
  249. /* Spansion style */
  250. nor->cmd_buf[0] = enable << 7;
  251. - return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1, 0);
  252. + return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
  253. }
  254. }
  255. static inline int spi_nor_sr_ready(struct spi_nor *nor)
  256. @@ -231,12 +244,13 @@ static int spi_nor_ready(struct spi_nor
  257. * Service routine to read status register until ready, or timeout occurs.
  258. * Returns non-zero if error.
  259. */
  260. -static int spi_nor_wait_till_ready(struct spi_nor *nor)
  261. +static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
  262. + unsigned long timeout_jiffies)
  263. {
  264. unsigned long deadline;
  265. int timeout = 0, ret;
  266. - deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  267. + deadline = jiffies + timeout_jiffies;
  268. while (!timeout) {
  269. if (time_after_eq(jiffies, deadline))
  270. @@ -256,6 +270,12 @@ static int spi_nor_wait_till_ready(struc
  271. return -ETIMEDOUT;
  272. }
  273. +static int spi_nor_wait_till_ready(struct spi_nor *nor)
  274. +{
  275. + return spi_nor_wait_till_ready_with_timeout(nor,
  276. + DEFAULT_READY_WAIT_JIFFIES);
  277. +}
  278. +
  279. /*
  280. * Erase the whole flash memory
  281. *
  282. @@ -263,9 +283,9 @@ static int spi_nor_wait_till_ready(struc
  283. */
  284. static int erase_chip(struct spi_nor *nor)
  285. {
  286. - dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd->size >> 10));
  287. + dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
  288. - return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0, 0);
  289. + return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
  290. }
  291. static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  292. @@ -319,6 +339,8 @@ static int spi_nor_erase(struct mtd_info
  293. /* whole-chip erase? */
  294. if (len == mtd->size) {
  295. + unsigned long timeout;
  296. +
  297. write_enable(nor);
  298. if (erase_chip(nor)) {
  299. @@ -326,7 +348,16 @@ static int spi_nor_erase(struct mtd_info
  300. goto erase_err;
  301. }
  302. - ret = spi_nor_wait_till_ready(nor);
  303. + /*
  304. + * Scale the timeout linearly with the size of the flash, with
  305. + * a minimum calibrated to an old 2MB flash. We could try to
  306. + * pull these from CFI/SFDP, but these values should be good
  307. + * enough for now.
  308. + */
  309. + timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
  310. + CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
  311. + (unsigned long)(mtd->size / SZ_2M));
  312. + ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
  313. if (ret)
  314. goto erase_err;
  315. @@ -369,72 +400,171 @@ erase_err:
  316. return ret;
  317. }
  318. +static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
  319. + uint64_t *len)
  320. +{
  321. + struct mtd_info *mtd = &nor->mtd;
  322. + u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  323. + int shift = ffs(mask) - 1;
  324. + int pow;
  325. +
  326. + if (!(sr & mask)) {
  327. + /* No protection */
  328. + *ofs = 0;
  329. + *len = 0;
  330. + } else {
  331. + pow = ((sr & mask) ^ mask) >> shift;
  332. + *len = mtd->size >> pow;
  333. + *ofs = mtd->size - *len;
  334. + }
  335. +}
  336. +
  337. +/*
  338. + * Return 1 if the entire region is locked, 0 otherwise
  339. + */
  340. +static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  341. + u8 sr)
  342. +{
  343. + loff_t lock_offs;
  344. + uint64_t lock_len;
  345. +
  346. + stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
  347. +
  348. + return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  349. +}
  350. +
  351. +/*
  352. + * Lock a region of the flash. Compatible with ST Micro and similar flash.
  353. + * Supports only the block protection bits BP{0,1,2} in the status register
  354. + * (SR). Does not support these features found in newer SR bitfields:
  355. + * - TB: top/bottom protect - only handle TB=0 (top protect)
  356. + * - SEC: sector/block protect - only handle SEC=0 (block protect)
  357. + * - CMP: complement protect - only support CMP=0 (range is not complemented)
  358. + *
  359. + * Sample table portion for 8MB flash (Winbond w25q64fw):
  360. + *
  361. + * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  362. + * --------------------------------------------------------------------------
  363. + * X | X | 0 | 0 | 0 | NONE | NONE
  364. + * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  365. + * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  366. + * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  367. + * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  368. + * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  369. + * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  370. + * X | X | 1 | 1 | 1 | 8 MB | ALL
  371. + *
  372. + * Returns negative on errors, 0 on success.
  373. + */
  374. static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  375. {
  376. - struct mtd_info *mtd = nor->mtd;
  377. - uint32_t offset = ofs;
  378. - uint8_t status_old, status_new;
  379. - int ret = 0;
  380. + struct mtd_info *mtd = &nor->mtd;
  381. + u8 status_old, status_new;
  382. + u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  383. + u8 shift = ffs(mask) - 1, pow, val;
  384. status_old = read_sr(nor);
  385. - if (offset < mtd->size - (mtd->size / 2))
  386. - status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
  387. - else if (offset < mtd->size - (mtd->size / 4))
  388. - status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  389. - else if (offset < mtd->size - (mtd->size / 8))
  390. - status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  391. - else if (offset < mtd->size - (mtd->size / 16))
  392. - status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
  393. - else if (offset < mtd->size - (mtd->size / 32))
  394. - status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  395. - else if (offset < mtd->size - (mtd->size / 64))
  396. - status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
  397. - else
  398. - status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
  399. + /* SPI NOR always locks to the end */
  400. + if (ofs + len != mtd->size) {
  401. + /* Does combined region extend to end? */
  402. + if (!stm_is_locked_sr(nor, ofs + len, mtd->size - ofs - len,
  403. + status_old))
  404. + return -EINVAL;
  405. + len = mtd->size - ofs;
  406. + }
  407. +
  408. + /*
  409. + * Need smallest pow such that:
  410. + *
  411. + * 1 / (2^pow) <= (len / size)
  412. + *
  413. + * so (assuming power-of-2 size) we do:
  414. + *
  415. + * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  416. + */
  417. + pow = ilog2(mtd->size) - ilog2(len);
  418. + val = mask - (pow << shift);
  419. + if (val & ~mask)
  420. + return -EINVAL;
  421. + /* Don't "lock" with no region! */
  422. + if (!(val & mask))
  423. + return -EINVAL;
  424. +
  425. + status_new = (status_old & ~mask) | val;
  426. /* Only modify protection if it will not unlock other areas */
  427. - if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) >
  428. - (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
  429. - write_enable(nor);
  430. - ret = write_sr(nor, status_new);
  431. - }
  432. + if ((status_new & mask) <= (status_old & mask))
  433. + return -EINVAL;
  434. - return ret;
  435. + write_enable(nor);
  436. + return write_sr(nor, status_new);
  437. }
  438. +/*
  439. + * Unlock a region of the flash. See stm_lock() for more info
  440. + *
  441. + * Returns negative on errors, 0 on success.
  442. + */
  443. static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  444. {
  445. - struct mtd_info *mtd = nor->mtd;
  446. - uint32_t offset = ofs;
  447. + struct mtd_info *mtd = &nor->mtd;
  448. uint8_t status_old, status_new;
  449. - int ret = 0;
  450. + u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  451. + u8 shift = ffs(mask) - 1, pow, val;
  452. status_old = read_sr(nor);
  453. - if (offset+len > mtd->size - (mtd->size / 64))
  454. - status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0);
  455. - else if (offset+len > mtd->size - (mtd->size / 32))
  456. - status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
  457. - else if (offset+len > mtd->size - (mtd->size / 16))
  458. - status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
  459. - else if (offset+len > mtd->size - (mtd->size / 8))
  460. - status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  461. - else if (offset+len > mtd->size - (mtd->size / 4))
  462. - status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
  463. - else if (offset+len > mtd->size - (mtd->size / 2))
  464. - status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  465. - else
  466. - status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  467. + /* Cannot unlock; would unlock larger region than requested */
  468. + if (stm_is_locked_sr(nor, status_old, ofs - mtd->erasesize,
  469. + mtd->erasesize))
  470. + return -EINVAL;
  471. - /* Only modify protection if it will not lock other areas */
  472. - if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) <
  473. - (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
  474. - write_enable(nor);
  475. - ret = write_sr(nor, status_new);
  476. + /*
  477. + * Need largest pow such that:
  478. + *
  479. + * 1 / (2^pow) >= (len / size)
  480. + *
  481. + * so (assuming power-of-2 size) we do:
  482. + *
  483. + * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  484. + */
  485. + pow = ilog2(mtd->size) - order_base_2(mtd->size - (ofs + len));
  486. + if (ofs + len == mtd->size) {
  487. + val = 0; /* fully unlocked */
  488. + } else {
  489. + val = mask - (pow << shift);
  490. + /* Some power-of-two sizes are not supported */
  491. + if (val & ~mask)
  492. + return -EINVAL;
  493. }
  494. - return ret;
  495. + status_new = (status_old & ~mask) | val;
  496. +
  497. + /* Only modify protection if it will not lock other areas */
  498. + if ((status_new & mask) >= (status_old & mask))
  499. + return -EINVAL;
  500. +
  501. + write_enable(nor);
  502. + return write_sr(nor, status_new);
  503. +}
  504. +
  505. +/*
  506. + * Check if a region of the flash is (completely) locked. See stm_lock() for
  507. + * more info.
  508. + *
  509. + * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  510. + * negative on errors.
  511. + */
  512. +static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
  513. +{
  514. + int status;
  515. +
  516. + status = read_sr(nor);
  517. + if (status < 0)
  518. + return status;
  519. +
  520. + return stm_is_locked_sr(nor, ofs, len, status);
  521. }
  522. static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  523. @@ -467,9 +597,23 @@ static int spi_nor_unlock(struct mtd_inf
  524. return ret;
  525. }
  526. +static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  527. +{
  528. + struct spi_nor *nor = mtd_to_spi_nor(mtd);
  529. + int ret;
  530. +
  531. + ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  532. + if (ret)
  533. + return ret;
  534. +
  535. + ret = nor->flash_is_locked(nor, ofs, len);
  536. +
  537. + spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  538. + return ret;
  539. +}
  540. +
  541. /* Used when the "_ext_id" is two bytes at most */
  542. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  543. - ((kernel_ulong_t)&(struct flash_info) { \
  544. .id = { \
  545. ((_jedec_id) >> 16) & 0xff, \
  546. ((_jedec_id) >> 8) & 0xff, \
  547. @@ -481,11 +625,9 @@ static int spi_nor_unlock(struct mtd_inf
  548. .sector_size = (_sector_size), \
  549. .n_sectors = (_n_sectors), \
  550. .page_size = 256, \
  551. - .flags = (_flags), \
  552. - })
  553. + .flags = (_flags),
  554. #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  555. - ((kernel_ulong_t)&(struct flash_info) { \
  556. .id = { \
  557. ((_jedec_id) >> 16) & 0xff, \
  558. ((_jedec_id) >> 8) & 0xff, \
  559. @@ -498,23 +640,27 @@ static int spi_nor_unlock(struct mtd_inf
  560. .sector_size = (_sector_size), \
  561. .n_sectors = (_n_sectors), \
  562. .page_size = 256, \
  563. - .flags = (_flags), \
  564. - })
  565. + .flags = (_flags),
  566. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
  567. - ((kernel_ulong_t)&(struct flash_info) { \
  568. .sector_size = (_sector_size), \
  569. .n_sectors = (_n_sectors), \
  570. .page_size = (_page_size), \
  571. .addr_width = (_addr_width), \
  572. - .flags = (_flags), \
  573. - })
  574. + .flags = (_flags),
  575. /* NOTE: double check command sets and memory organization when you add
  576. * more nor chips. This current list focusses on newer chips, which
  577. * have been converging on command sets which including JEDEC ID.
  578. + *
  579. + * All newly added entries should describe *hardware* and should use SECT_4K
  580. + * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
  581. + * scenarios excluding small sectors there is config option that can be
  582. + * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
  583. + * For historical (and compatibility) reasons (before we got above config) some
  584. + * old entries may be missing 4K flag.
  585. */
  586. -static const struct spi_device_id spi_nor_ids[] = {
  587. +static const struct flash_info spi_nor_ids[] = {
  588. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  589. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  590. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  591. @@ -538,7 +684,7 @@ static const struct spi_device_id spi_no
  592. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  593. { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
  594. { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  595. - { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, 0) },
  596. + { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
  597. /* ESMT */
  598. { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
  599. @@ -560,7 +706,11 @@ static const struct spi_device_id spi_no
  600. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  601. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  602. + /* ISSI */
  603. + { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
  604. +
  605. /* Macronix */
  606. + { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
  607. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  608. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  609. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  610. @@ -578,7 +728,9 @@ static const struct spi_device_id spi_no
  611. /* Micron */
  612. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  613. - { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SPI_NOR_QUAD_READ) },
  614. + { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  615. + { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  616. + { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  617. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  618. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  619. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
  620. @@ -595,25 +747,28 @@ static const struct spi_device_id spi_no
  621. * for the chips listed here (without boot sectors).
  622. */
  623. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  624. - { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
  625. + { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  626. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  627. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  628. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  629. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  630. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  631. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  632. - { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  633. - { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
  634. - { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
  635. + { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  636. + { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  637. + { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  638. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  639. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  640. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  641. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  642. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  643. - { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  644. - { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
  645. + { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  646. + { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  647. + { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  648. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  649. - { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, 0) },
  650. + { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
  651. + { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
  652. + { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
  653. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  654. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  655. @@ -624,6 +779,8 @@ static const struct spi_device_id spi_no
  656. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  657. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  658. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  659. + { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
  660. + { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
  661. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  662. { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  663. @@ -672,10 +829,11 @@ static const struct spi_device_id spi_no
  664. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  665. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  666. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  667. - { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
  668. + { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  669. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  670. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  671. - { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
  672. + { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  673. + { "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  674. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  675. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  676. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  677. @@ -690,11 +848,11 @@ static const struct spi_device_id spi_no
  678. { },
  679. };
  680. -static const struct spi_device_id *spi_nor_read_id(struct spi_nor *nor)
  681. +static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
  682. {
  683. int tmp;
  684. u8 id[SPI_NOR_MAX_ID_LEN];
  685. - struct flash_info *info;
  686. + const struct flash_info *info;
  687. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  688. if (tmp < 0) {
  689. @@ -703,7 +861,7 @@ static const struct spi_device_id *spi_n
  690. }
  691. for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
  692. - info = (void *)spi_nor_ids[tmp].driver_data;
  693. + info = &spi_nor_ids[tmp];
  694. if (info->id_len) {
  695. if (!memcmp(info->id, id, info->id_len))
  696. return &spi_nor_ids[tmp];
  697. @@ -857,8 +1015,7 @@ static int macronix_quad_enable(struct s
  698. val = read_sr(nor);
  699. write_enable(nor);
  700. - nor->cmd_buf[0] = val | SR_QUAD_EN_MX;
  701. - nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
  702. + write_sr(nor, val | SR_QUAD_EN_MX);
  703. if (spi_nor_wait_till_ready(nor))
  704. return 1;
  705. @@ -883,7 +1040,7 @@ static int write_sr_cr(struct spi_nor *n
  706. nor->cmd_buf[0] = val & 0xff;
  707. nor->cmd_buf[1] = (val >> 8);
  708. - return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2, 0);
  709. + return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
  710. }
  711. static int spansion_quad_enable(struct spi_nor *nor)
  712. @@ -925,7 +1082,7 @@ static int micron_quad_enable(struct spi
  713. /* set EVCR, enable quad I/O */
  714. nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON;
  715. - ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1, 0);
  716. + ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1);
  717. if (ret < 0) {
  718. dev_err(nor->dev, "error while writing EVCR register\n");
  719. return ret;
  720. @@ -949,19 +1106,19 @@ static int micron_quad_enable(struct spi
  721. return 0;
  722. }
  723. -static int set_quad_mode(struct spi_nor *nor, struct flash_info *info)
  724. +static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
  725. {
  726. int status;
  727. switch (JEDEC_MFR(info)) {
  728. - case CFI_MFR_MACRONIX:
  729. + case SNOR_MFR_MACRONIX:
  730. status = macronix_quad_enable(nor);
  731. if (status) {
  732. dev_err(nor->dev, "Macronix quad-read not enabled\n");
  733. return -EINVAL;
  734. }
  735. return status;
  736. - case CFI_MFR_ST:
  737. + case SNOR_MFR_MICRON:
  738. status = micron_quad_enable(nor);
  739. if (status) {
  740. dev_err(nor->dev, "Micron quad-read not enabled\n");
  741. @@ -991,11 +1148,10 @@ static int spi_nor_check(struct spi_nor
  742. int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
  743. {
  744. - const struct spi_device_id *id = NULL;
  745. - struct flash_info *info;
  746. + const struct flash_info *info = NULL;
  747. struct device *dev = nor->dev;
  748. - struct mtd_info *mtd = nor->mtd;
  749. - struct device_node *np = dev->of_node;
  750. + struct mtd_info *mtd = &nor->mtd;
  751. + struct device_node *np = nor->flash_node;
  752. int ret;
  753. int i;
  754. @@ -1003,27 +1159,25 @@ int spi_nor_scan(struct spi_nor *nor, co
  755. if (ret)
  756. return ret;
  757. - /* Try to auto-detect if chip name wasn't specified */
  758. - if (!name)
  759. - id = spi_nor_read_id(nor);
  760. - else
  761. - id = spi_nor_match_id(name);
  762. - if (IS_ERR_OR_NULL(id))
  763. + if (name)
  764. + info = spi_nor_match_id(name);
  765. + /* Try to auto-detect if chip name wasn't specified or not found */
  766. + if (!info)
  767. + info = spi_nor_read_id(nor);
  768. + if (IS_ERR_OR_NULL(info))
  769. return -ENOENT;
  770. - info = (void *)id->driver_data;
  771. -
  772. /*
  773. * If caller has specified name of flash model that can normally be
  774. * detected using JEDEC, let's verify it.
  775. */
  776. if (name && info->id_len) {
  777. - const struct spi_device_id *jid;
  778. + const struct flash_info *jinfo;
  779. - jid = spi_nor_read_id(nor);
  780. - if (IS_ERR(jid)) {
  781. - return PTR_ERR(jid);
  782. - } else if (jid != id) {
  783. + jinfo = spi_nor_read_id(nor);
  784. + if (IS_ERR(jinfo)) {
  785. + return PTR_ERR(jinfo);
  786. + } else if (jinfo != info) {
  787. /*
  788. * JEDEC knows better, so overwrite platform ID. We
  789. * can't trust partitions any longer, but we'll let
  790. @@ -1032,28 +1186,29 @@ int spi_nor_scan(struct spi_nor *nor, co
  791. * information, even if it's not 100% accurate.
  792. */
  793. dev_warn(dev, "found %s, expected %s\n",
  794. - jid->name, id->name);
  795. - id = jid;
  796. - info = (void *)jid->driver_data;
  797. + jinfo->name, info->name);
  798. + info = jinfo;
  799. }
  800. }
  801. mutex_init(&nor->lock);
  802. /*
  803. - * Atmel, SST and Intel/Numonyx serial nor tend to power
  804. - * up with the software protection bits set
  805. + * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
  806. + * with the software protection bits set
  807. */
  808. - if (JEDEC_MFR(info) == CFI_MFR_ATMEL ||
  809. - JEDEC_MFR(info) == CFI_MFR_INTEL ||
  810. - JEDEC_MFR(info) == CFI_MFR_SST) {
  811. + if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
  812. + JEDEC_MFR(info) == SNOR_MFR_INTEL ||
  813. + JEDEC_MFR(info) == SNOR_MFR_SST ||
  814. + JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
  815. write_enable(nor);
  816. write_sr(nor, 0);
  817. }
  818. if (!mtd->name)
  819. mtd->name = dev_name(dev);
  820. + mtd->priv = nor;
  821. mtd->type = MTD_NORFLASH;
  822. mtd->writesize = 1;
  823. mtd->flags = MTD_CAP_NORFLASH;
  824. @@ -1061,15 +1216,18 @@ int spi_nor_scan(struct spi_nor *nor, co
  825. mtd->_erase = spi_nor_erase;
  826. mtd->_read = spi_nor_read;
  827. - /* nor protection support for STmicro chips */
  828. - if (JEDEC_MFR(info) == CFI_MFR_ST) {
  829. + /* NOR protection support for STmicro/Micron chips and similar */
  830. + if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
  831. + JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
  832. nor->flash_lock = stm_lock;
  833. nor->flash_unlock = stm_unlock;
  834. + nor->flash_is_locked = stm_is_locked;
  835. }
  836. - if (nor->flash_lock && nor->flash_unlock) {
  837. + if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
  838. mtd->_lock = spi_nor_lock;
  839. mtd->_unlock = spi_nor_unlock;
  840. + mtd->_is_locked = spi_nor_is_locked;
  841. }
  842. /* sst nor chips use AAI word program */
  843. @@ -1156,7 +1314,7 @@ int spi_nor_scan(struct spi_nor *nor, co
  844. else if (mtd->size > 0x1000000) {
  845. /* enable 4-byte addressing if the device exceeds 16MiB */
  846. nor->addr_width = 4;
  847. - if (JEDEC_MFR(info) == CFI_MFR_AMD) {
  848. + if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) {
  849. /* Dedicated 4-byte command set */
  850. switch (nor->flash_read) {
  851. case SPI_NOR_QUAD:
  852. @@ -1184,7 +1342,7 @@ int spi_nor_scan(struct spi_nor *nor, co
  853. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  854. - dev_info(dev, "%s (%lld Kbytes)\n", id->name,
  855. + dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  856. (long long)mtd->size >> 10);
  857. dev_dbg(dev,
  858. @@ -1207,11 +1365,11 @@ int spi_nor_scan(struct spi_nor *nor, co
  859. }
  860. EXPORT_SYMBOL_GPL(spi_nor_scan);
  861. -static const struct spi_device_id *spi_nor_match_id(const char *name)
  862. +static const struct flash_info *spi_nor_match_id(const char *name)
  863. {
  864. - const struct spi_device_id *id = spi_nor_ids;
  865. + const struct flash_info *id = spi_nor_ids;
  866. - while (id->name[0]) {
  867. + while (id->name) {
  868. if (!strcmp(name, id->name))
  869. return id;
  870. id++;
  871. --- a/include/linux/mtd/spi-nor.h
  872. +++ b/include/linux/mtd/spi-nor.h
  873. @@ -10,6 +10,23 @@
  874. #ifndef __LINUX_MTD_SPI_NOR_H
  875. #define __LINUX_MTD_SPI_NOR_H
  876. +#include <linux/bitops.h>
  877. +#include <linux/mtd/cfi.h>
  878. +
  879. +/*
  880. + * Manufacturer IDs
  881. + *
  882. + * The first byte returned from the flash after sending opcode SPINOR_OP_RDID.
  883. + * Sometimes these are the same as CFI IDs, but sometimes they aren't.
  884. + */
  885. +#define SNOR_MFR_ATMEL CFI_MFR_ATMEL
  886. +#define SNOR_MFR_INTEL CFI_MFR_INTEL
  887. +#define SNOR_MFR_MICRON CFI_MFR_ST /* ST Micro <--> Micron */
  888. +#define SNOR_MFR_MACRONIX CFI_MFR_MACRONIX
  889. +#define SNOR_MFR_SPANSION CFI_MFR_AMD
  890. +#define SNOR_MFR_SST CFI_MFR_SST
  891. +#define SNOR_MFR_WINBOND 0xef
  892. +
  893. /*
  894. * Note on opcode nomenclature: some opcodes have a format like
  895. * SPINOR_OP_FUNCTION{4,}_x_y_z. The numbers x, y, and z stand for the number
  896. @@ -61,24 +78,24 @@
  897. #define SPINOR_OP_WD_EVCR 0x61 /* Write EVCR register */
  898. /* Status Register bits. */
  899. -#define SR_WIP 1 /* Write in progress */
  900. -#define SR_WEL 2 /* Write enable latch */
  901. +#define SR_WIP BIT(0) /* Write in progress */
  902. +#define SR_WEL BIT(1) /* Write enable latch */
  903. /* meaning of other SR_* bits may differ between vendors */
  904. -#define SR_BP0 4 /* Block protect 0 */
  905. -#define SR_BP1 8 /* Block protect 1 */
  906. -#define SR_BP2 0x10 /* Block protect 2 */
  907. -#define SR_SRWD 0x80 /* SR write protect */
  908. +#define SR_BP0 BIT(2) /* Block protect 0 */
  909. +#define SR_BP1 BIT(3) /* Block protect 1 */
  910. +#define SR_BP2 BIT(4) /* Block protect 2 */
  911. +#define SR_SRWD BIT(7) /* SR write protect */
  912. -#define SR_QUAD_EN_MX 0x40 /* Macronix Quad I/O */
  913. +#define SR_QUAD_EN_MX BIT(6) /* Macronix Quad I/O */
  914. /* Enhanced Volatile Configuration Register bits */
  915. -#define EVCR_QUAD_EN_MICRON 0x80 /* Micron Quad I/O */
  916. +#define EVCR_QUAD_EN_MICRON BIT(7) /* Micron Quad I/O */
  917. /* Flag Status Register bits */
  918. -#define FSR_READY 0x80
  919. +#define FSR_READY BIT(7)
  920. /* Configuration Register bits. */
  921. -#define CR_QUAD_EN_SPAN 0x2 /* Spansion Quad I/O */
  922. +#define CR_QUAD_EN_SPAN BIT(1) /* Spansion Quad I/O */
  923. enum read_mode {
  924. SPI_NOR_NORMAL = 0,
  925. @@ -87,33 +104,6 @@ enum read_mode {
  926. SPI_NOR_QUAD,
  927. };
  928. -/**
  929. - * struct spi_nor_xfer_cfg - Structure for defining a Serial Flash transfer
  930. - * @wren: command for "Write Enable", or 0x00 for not required
  931. - * @cmd: command for operation
  932. - * @cmd_pins: number of pins to send @cmd (1, 2, 4)
  933. - * @addr: address for operation
  934. - * @addr_pins: number of pins to send @addr (1, 2, 4)
  935. - * @addr_width: number of address bytes
  936. - * (3,4, or 0 for address not required)
  937. - * @mode: mode data
  938. - * @mode_pins: number of pins to send @mode (1, 2, 4)
  939. - * @mode_cycles: number of mode cycles (0 for mode not required)
  940. - * @dummy_cycles: number of dummy cycles (0 for dummy not required)
  941. - */
  942. -struct spi_nor_xfer_cfg {
  943. - u8 wren;
  944. - u8 cmd;
  945. - u8 cmd_pins;
  946. - u32 addr;
  947. - u8 addr_pins;
  948. - u8 addr_width;
  949. - u8 mode;
  950. - u8 mode_pins;
  951. - u8 mode_cycles;
  952. - u8 dummy_cycles;
  953. -};
  954. -
  955. #define SPI_NOR_MAX_CMD_SIZE 8
  956. enum spi_nor_ops {
  957. SPI_NOR_OPS_READ = 0,
  958. @@ -127,11 +117,14 @@ enum spi_nor_option_flags {
  959. SNOR_F_USE_FSR = BIT(0),
  960. };
  961. +struct mtd_info;
  962. +
  963. /**
  964. * struct spi_nor - Structure for defining a the SPI NOR layer
  965. * @mtd: point to a mtd_info structure
  966. * @lock: the lock for the read/write/erase/lock/unlock operations
  967. * @dev: point to a spi device, or a spi nor controller device.
  968. + * @flash_node: point to a device node describing this flash instance.
  969. * @page_size: the page size of the SPI NOR
  970. * @addr_width: number of address bytes
  971. * @erase_opcode: the opcode for erasing a sector
  972. @@ -141,28 +134,28 @@ enum spi_nor_option_flags {
  973. * @flash_read: the mode of the read
  974. * @sst_write_second: used by the SST write operation
  975. * @flags: flag options for the current SPI-NOR (SNOR_F_*)
  976. - * @cfg: used by the read_xfer/write_xfer
  977. * @cmd_buf: used by the write_reg
  978. * @prepare: [OPTIONAL] do some preparations for the
  979. * read/write/erase/lock/unlock operations
  980. * @unprepare: [OPTIONAL] do some post work after the
  981. * read/write/erase/lock/unlock operations
  982. - * @read_xfer: [OPTIONAL] the read fundamental primitive
  983. - * @write_xfer: [OPTIONAL] the writefundamental primitive
  984. * @read_reg: [DRIVER-SPECIFIC] read out the register
  985. * @write_reg: [DRIVER-SPECIFIC] write data to the register
  986. * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
  987. * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
  988. * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
  989. * at the offset @offs
  990. - * @lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
  991. - * @unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
  992. + * @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
  993. + * @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
  994. + * @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is
  995. + * completely locked
  996. * @priv: the private data
  997. */
  998. struct spi_nor {
  999. - struct mtd_info *mtd;
  1000. + struct mtd_info mtd;
  1001. struct mutex lock;
  1002. struct device *dev;
  1003. + struct device_node *flash_node;
  1004. u32 page_size;
  1005. u8 addr_width;
  1006. u8 erase_opcode;
  1007. @@ -172,18 +165,12 @@ struct spi_nor {
  1008. enum read_mode flash_read;
  1009. bool sst_write_second;
  1010. u32 flags;
  1011. - struct spi_nor_xfer_cfg cfg;
  1012. u8 cmd_buf[SPI_NOR_MAX_CMD_SIZE];
  1013. int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops);
  1014. void (*unprepare)(struct spi_nor *nor, enum spi_nor_ops ops);
  1015. - int (*read_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg,
  1016. - u8 *buf, size_t len);
  1017. - int (*write_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg,
  1018. - u8 *buf, size_t len);
  1019. int (*read_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
  1020. - int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len,
  1021. - int write_enable);
  1022. + int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
  1023. int (*read)(struct spi_nor *nor, loff_t from,
  1024. size_t len, size_t *retlen, u_char *read_buf);
  1025. @@ -193,6 +180,7 @@ struct spi_nor {
  1026. int (*flash_lock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
  1027. int (*flash_unlock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
  1028. + int (*flash_is_locked)(struct spi_nor *nor, loff_t ofs, uint64_t len);
  1029. void *priv;
  1030. };