m25p80.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /*
  2. * MTD SPI driver for ST M25Pxx (and similar) serial flash chips
  3. *
  4. * Author: Mike Lavender, mike@steroidmicros.com
  5. *
  6. * Copyright (c) 2005, Intec Automation Inc.
  7. *
  8. * Some parts are based on lart.c by Abraham Van Der Merwe
  9. *
  10. * Cleaned up and generalized based on mtd_dataflash.c
  11. *
  12. * This code is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/errno.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mutex.h>
  24. #include <linux/math64.h>
  25. #include <linux/slab.h>
  26. #include <linux/sched.h>
  27. #include <linux/mod_devicetable.h>
  28. #include <linux/mtd/cfi.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/spi/flash.h>
  34. /* Flash opcodes. */
  35. #define OPCODE_WREN 0x06 /* Write enable */
  36. #define OPCODE_RDSR 0x05 /* Read status register */
  37. #define OPCODE_WRSR 0x01 /* Write status register 1 byte */
  38. #define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
  39. #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
  40. #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
  41. #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
  42. #define OPCODE_BE_4K_PMC 0xd7 /* Erase 4KiB block on PMC chips */
  43. #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
  44. #define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
  45. #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
  46. #define OPCODE_RDID 0x9f /* Read JEDEC ID */
  47. /* 4-byte address opcodes - used on Spansion and some Macronix flashes. */
  48. #define OPCODE_NORM_READ_4B 0x13 /* Read data bytes (low frequency) */
  49. #define OPCODE_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */
  50. #define OPCODE_PP_4B 0x12 /* Page program (up to 256 bytes) */
  51. #define OPCODE_SE_4B 0xdc /* Sector erase (usually 64KiB) */
  52. /* Used for SST flashes only. */
  53. #define OPCODE_BP 0x02 /* Byte program */
  54. #define OPCODE_WRDI 0x04 /* Write disable */
  55. #define OPCODE_AAI_WP 0xad /* Auto address increment word program */
  56. /* Used for Macronix and Winbond flashes. */
  57. #define OPCODE_EN4B 0xb7 /* Enter 4-byte mode */
  58. #define OPCODE_EX4B 0xe9 /* Exit 4-byte mode */
  59. /* Used for Spansion flashes only. */
  60. #define OPCODE_BRWR 0x17 /* Bank register write */
  61. /* Status Register bits. */
  62. #define SR_WIP 1 /* Write in progress */
  63. #define SR_WEL 2 /* Write enable latch */
  64. /* meaning of other SR_* bits may differ between vendors */
  65. #define SR_BP0 4 /* Block protect 0 */
  66. #define SR_BP1 8 /* Block protect 1 */
  67. #define SR_BP2 0x10 /* Block protect 2 */
  68. #define SR_SRWD 0x80 /* SR write protect */
  69. /* Define max times to check status register before we give up. */
  70. #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
  71. #define MAX_CMD_SIZE 5
  72. #define JEDEC_MFR(_jedec_id) ((_jedec_id) >> 16)
  73. /****************************************************************************/
  74. struct m25p {
  75. struct spi_device *spi;
  76. struct mutex lock;
  77. struct mtd_info mtd;
  78. u16 page_size;
  79. u16 addr_width;
  80. u8 erase_opcode;
  81. u8 read_opcode;
  82. u8 program_opcode;
  83. u8 *command;
  84. bool fast_read;
  85. };
  86. static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
  87. {
  88. return container_of(mtd, struct m25p, mtd);
  89. }
  90. /****************************************************************************/
  91. /*
  92. * Internal helper functions
  93. */
  94. /*
  95. * Read the status register, returning its value in the location
  96. * Return the status register value.
  97. * Returns negative if error occurred.
  98. */
  99. static int read_sr(struct m25p *flash)
  100. {
  101. ssize_t retval;
  102. u8 code = OPCODE_RDSR;
  103. u8 val;
  104. retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
  105. if (retval < 0) {
  106. dev_err(&flash->spi->dev, "error %d reading SR\n",
  107. (int) retval);
  108. return retval;
  109. }
  110. return val;
  111. }
  112. /*
  113. * Write status register 1 byte
  114. * Returns negative if error occurred.
  115. */
  116. static int write_sr(struct m25p *flash, u8 val)
  117. {
  118. flash->command[0] = OPCODE_WRSR;
  119. flash->command[1] = val;
  120. return spi_write(flash->spi, flash->command, 2);
  121. }
  122. /*
  123. * Set write enable latch with Write Enable command.
  124. * Returns negative if error occurred.
  125. */
  126. static inline int write_enable(struct m25p *flash)
  127. {
  128. u8 code = OPCODE_WREN;
  129. return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
  130. }
  131. /*
  132. * Send write disble instruction to the chip.
  133. */
  134. static inline int write_disable(struct m25p *flash)
  135. {
  136. u8 code = OPCODE_WRDI;
  137. return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
  138. }
  139. /*
  140. * Enable/disable 4-byte addressing mode.
  141. */
  142. static inline int set_4byte(struct m25p *flash, u32 jedec_id, int enable)
  143. {
  144. int status;
  145. bool need_wren = false;
  146. switch (JEDEC_MFR(jedec_id)) {
  147. case CFI_MFR_ST: /* Micron, actually */
  148. /* Some Micron need WREN command; all will accept it */
  149. need_wren = true;
  150. case CFI_MFR_MACRONIX:
  151. case 0xEF /* winbond */:
  152. if (need_wren)
  153. write_enable(flash);
  154. flash->command[0] = enable ? OPCODE_EN4B : OPCODE_EX4B;
  155. status = spi_write(flash->spi, flash->command, 1);
  156. if (need_wren)
  157. write_disable(flash);
  158. return status;
  159. default:
  160. /* Spansion style */
  161. flash->command[0] = OPCODE_BRWR;
  162. flash->command[1] = enable << 7;
  163. return spi_write(flash->spi, flash->command, 2);
  164. }
  165. }
  166. /*
  167. * Service routine to read status register until ready, or timeout occurs.
  168. * Returns non-zero if error.
  169. */
  170. static int wait_till_ready(struct m25p *flash)
  171. {
  172. unsigned long deadline;
  173. int sr;
  174. deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  175. do {
  176. if ((sr = read_sr(flash)) < 0)
  177. break;
  178. else if (!(sr & SR_WIP))
  179. return 0;
  180. cond_resched();
  181. } while (!time_after_eq(jiffies, deadline));
  182. return 1;
  183. }
  184. /*
  185. * Erase the whole flash memory
  186. *
  187. * Returns 0 if successful, non-zero otherwise.
  188. */
  189. static int erase_chip(struct m25p *flash)
  190. {
  191. pr_debug("%s: %s %lldKiB\n", dev_name(&flash->spi->dev), __func__,
  192. (long long)(flash->mtd.size >> 10));
  193. /* Wait until finished previous write command. */
  194. if (wait_till_ready(flash))
  195. return 1;
  196. /* Send write enable, then erase commands. */
  197. write_enable(flash);
  198. /* Set up command buffer. */
  199. flash->command[0] = OPCODE_CHIP_ERASE;
  200. spi_write(flash->spi, flash->command, 1);
  201. return 0;
  202. }
  203. static void m25p_addr2cmd(struct m25p *flash, unsigned int addr, u8 *cmd)
  204. {
  205. /* opcode is in cmd[0] */
  206. cmd[1] = addr >> (flash->addr_width * 8 - 8);
  207. cmd[2] = addr >> (flash->addr_width * 8 - 16);
  208. cmd[3] = addr >> (flash->addr_width * 8 - 24);
  209. cmd[4] = addr >> (flash->addr_width * 8 - 32);
  210. }
  211. static int m25p_cmdsz(struct m25p *flash)
  212. {
  213. return 1 + flash->addr_width;
  214. }
  215. /*
  216. * Erase one sector of flash memory at offset ``offset'' which is any
  217. * address within the sector which should be erased.
  218. *
  219. * Returns 0 if successful, non-zero otherwise.
  220. */
  221. static int erase_sector(struct m25p *flash, u32 offset)
  222. {
  223. pr_debug("%s: %s %dKiB at 0x%08x\n", dev_name(&flash->spi->dev),
  224. __func__, flash->mtd.erasesize / 1024, offset);
  225. /* Wait until finished previous write command. */
  226. if (wait_till_ready(flash))
  227. return 1;
  228. /* Send write enable, then erase commands. */
  229. write_enable(flash);
  230. /* Set up command buffer. */
  231. flash->command[0] = flash->erase_opcode;
  232. m25p_addr2cmd(flash, offset, flash->command);
  233. spi_write(flash->spi, flash->command, m25p_cmdsz(flash));
  234. return 0;
  235. }
  236. /****************************************************************************/
  237. /*
  238. * MTD implementation
  239. */
  240. /*
  241. * Erase an address range on the flash chip. The address range may extend
  242. * one or more erase sectors. Return an error is there is a problem erasing.
  243. */
  244. static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
  245. {
  246. struct m25p *flash = mtd_to_m25p(mtd);
  247. u32 addr,len;
  248. uint32_t rem;
  249. pr_debug("%s: %s at 0x%llx, len %lld\n", dev_name(&flash->spi->dev),
  250. __func__, (long long)instr->addr,
  251. (long long)instr->len);
  252. div_u64_rem(instr->len, mtd->erasesize, &rem);
  253. if (rem)
  254. return -EINVAL;
  255. addr = instr->addr;
  256. len = instr->len;
  257. mutex_lock(&flash->lock);
  258. /* whole-chip erase? */
  259. if (len == flash->mtd.size) {
  260. if (erase_chip(flash)) {
  261. instr->state = MTD_ERASE_FAILED;
  262. mutex_unlock(&flash->lock);
  263. return -EIO;
  264. }
  265. /* REVISIT in some cases we could speed up erasing large regions
  266. * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
  267. * to use "small sector erase", but that's not always optimal.
  268. */
  269. /* "sector"-at-a-time erase */
  270. } else {
  271. while (len) {
  272. if (erase_sector(flash, addr)) {
  273. instr->state = MTD_ERASE_FAILED;
  274. mutex_unlock(&flash->lock);
  275. return -EIO;
  276. }
  277. addr += mtd->erasesize;
  278. len -= mtd->erasesize;
  279. }
  280. }
  281. mutex_unlock(&flash->lock);
  282. instr->state = MTD_ERASE_DONE;
  283. mtd_erase_callback(instr);
  284. return 0;
  285. }
  286. /*
  287. * Read an address range from the flash chip. The address range
  288. * may be any size provided it is within the physical boundaries.
  289. */
  290. static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
  291. size_t *retlen, u_char *buf)
  292. {
  293. struct m25p *flash = mtd_to_m25p(mtd);
  294. struct spi_transfer t[2];
  295. struct spi_message m;
  296. uint8_t opcode;
  297. pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  298. __func__, (u32)from, len);
  299. spi_message_init(&m);
  300. memset(t, 0, (sizeof t));
  301. /* NOTE:
  302. * OPCODE_FAST_READ (if available) is faster.
  303. * Should add 1 byte DUMMY_BYTE.
  304. */
  305. t[0].tx_buf = flash->command;
  306. t[0].len = m25p_cmdsz(flash) + (flash->fast_read ? 1 : 0);
  307. spi_message_add_tail(&t[0], &m);
  308. t[1].rx_buf = buf;
  309. t[1].len = len;
  310. spi_message_add_tail(&t[1], &m);
  311. mutex_lock(&flash->lock);
  312. /* Wait till previous write/erase is done. */
  313. if (wait_till_ready(flash)) {
  314. /* REVISIT status return?? */
  315. mutex_unlock(&flash->lock);
  316. return 1;
  317. }
  318. /* FIXME switch to OPCODE_FAST_READ. It's required for higher
  319. * clocks; and at this writing, every chip this driver handles
  320. * supports that opcode.
  321. */
  322. /* Set up the write data buffer. */
  323. opcode = flash->read_opcode;
  324. flash->command[0] = opcode;
  325. m25p_addr2cmd(flash, from, flash->command);
  326. spi_sync(flash->spi, &m);
  327. *retlen = m.actual_length - m25p_cmdsz(flash) -
  328. (flash->fast_read ? 1 : 0);
  329. mutex_unlock(&flash->lock);
  330. return 0;
  331. }
  332. /*
  333. * Write an address range to the flash chip. Data must be written in
  334. * FLASH_PAGESIZE chunks. The address range may be any size provided
  335. * it is within the physical boundaries.
  336. */
  337. static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
  338. size_t *retlen, const u_char *buf)
  339. {
  340. struct m25p *flash = mtd_to_m25p(mtd);
  341. u32 page_offset, page_size;
  342. struct spi_transfer t[2];
  343. struct spi_message m;
  344. pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  345. __func__, (u32)to, len);
  346. spi_message_init(&m);
  347. memset(t, 0, (sizeof t));
  348. t[0].tx_buf = flash->command;
  349. t[0].len = m25p_cmdsz(flash);
  350. spi_message_add_tail(&t[0], &m);
  351. t[1].tx_buf = buf;
  352. spi_message_add_tail(&t[1], &m);
  353. mutex_lock(&flash->lock);
  354. /* Wait until finished previous write command. */
  355. if (wait_till_ready(flash)) {
  356. mutex_unlock(&flash->lock);
  357. return 1;
  358. }
  359. write_enable(flash);
  360. /* Set up the opcode in the write buffer. */
  361. flash->command[0] = flash->program_opcode;
  362. m25p_addr2cmd(flash, to, flash->command);
  363. page_offset = to & (flash->page_size - 1);
  364. /* do all the bytes fit onto one page? */
  365. if (page_offset + len <= flash->page_size) {
  366. t[1].len = len;
  367. spi_sync(flash->spi, &m);
  368. *retlen = m.actual_length - m25p_cmdsz(flash);
  369. } else {
  370. u32 i;
  371. /* the size of data remaining on the first page */
  372. page_size = flash->page_size - page_offset;
  373. t[1].len = page_size;
  374. spi_sync(flash->spi, &m);
  375. *retlen = m.actual_length - m25p_cmdsz(flash);
  376. /* write everything in flash->page_size chunks */
  377. for (i = page_size; i < len; i += page_size) {
  378. page_size = len - i;
  379. if (page_size > flash->page_size)
  380. page_size = flash->page_size;
  381. /* write the next page to flash */
  382. m25p_addr2cmd(flash, to + i, flash->command);
  383. t[1].tx_buf = buf + i;
  384. t[1].len = page_size;
  385. wait_till_ready(flash);
  386. write_enable(flash);
  387. spi_sync(flash->spi, &m);
  388. *retlen += m.actual_length - m25p_cmdsz(flash);
  389. }
  390. }
  391. mutex_unlock(&flash->lock);
  392. return 0;
  393. }
  394. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  395. size_t *retlen, const u_char *buf)
  396. {
  397. struct m25p *flash = mtd_to_m25p(mtd);
  398. struct spi_transfer t[2];
  399. struct spi_message m;
  400. size_t actual;
  401. int cmd_sz, ret;
  402. pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  403. __func__, (u32)to, len);
  404. spi_message_init(&m);
  405. memset(t, 0, (sizeof t));
  406. t[0].tx_buf = flash->command;
  407. t[0].len = m25p_cmdsz(flash);
  408. spi_message_add_tail(&t[0], &m);
  409. t[1].tx_buf = buf;
  410. spi_message_add_tail(&t[1], &m);
  411. mutex_lock(&flash->lock);
  412. /* Wait until finished previous write command. */
  413. ret = wait_till_ready(flash);
  414. if (ret)
  415. goto time_out;
  416. write_enable(flash);
  417. actual = to % 2;
  418. /* Start write from odd address. */
  419. if (actual) {
  420. flash->command[0] = OPCODE_BP;
  421. m25p_addr2cmd(flash, to, flash->command);
  422. /* write one byte. */
  423. t[1].len = 1;
  424. spi_sync(flash->spi, &m);
  425. ret = wait_till_ready(flash);
  426. if (ret)
  427. goto time_out;
  428. *retlen += m.actual_length - m25p_cmdsz(flash);
  429. }
  430. to += actual;
  431. flash->command[0] = OPCODE_AAI_WP;
  432. m25p_addr2cmd(flash, to, flash->command);
  433. /* Write out most of the data here. */
  434. cmd_sz = m25p_cmdsz(flash);
  435. for (; actual < len - 1; actual += 2) {
  436. t[0].len = cmd_sz;
  437. /* write two bytes. */
  438. t[1].len = 2;
  439. t[1].tx_buf = buf + actual;
  440. spi_sync(flash->spi, &m);
  441. ret = wait_till_ready(flash);
  442. if (ret)
  443. goto time_out;
  444. *retlen += m.actual_length - cmd_sz;
  445. cmd_sz = 1;
  446. to += 2;
  447. }
  448. write_disable(flash);
  449. ret = wait_till_ready(flash);
  450. if (ret)
  451. goto time_out;
  452. /* Write out trailing byte if it exists. */
  453. if (actual != len) {
  454. write_enable(flash);
  455. flash->command[0] = OPCODE_BP;
  456. m25p_addr2cmd(flash, to, flash->command);
  457. t[0].len = m25p_cmdsz(flash);
  458. t[1].len = 1;
  459. t[1].tx_buf = buf + actual;
  460. spi_sync(flash->spi, &m);
  461. ret = wait_till_ready(flash);
  462. if (ret)
  463. goto time_out;
  464. *retlen += m.actual_length - m25p_cmdsz(flash);
  465. write_disable(flash);
  466. }
  467. time_out:
  468. mutex_unlock(&flash->lock);
  469. return ret;
  470. }
  471. static int m25p80_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  472. {
  473. struct m25p *flash = mtd_to_m25p(mtd);
  474. uint32_t offset = ofs;
  475. uint8_t status_old, status_new;
  476. int res = 0;
  477. mutex_lock(&flash->lock);
  478. /* Wait until finished previous command */
  479. if (wait_till_ready(flash)) {
  480. res = 1;
  481. goto err;
  482. }
  483. status_old = read_sr(flash);
  484. if (offset < flash->mtd.size-(flash->mtd.size/2))
  485. status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
  486. else if (offset < flash->mtd.size-(flash->mtd.size/4))
  487. status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  488. else if (offset < flash->mtd.size-(flash->mtd.size/8))
  489. status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  490. else if (offset < flash->mtd.size-(flash->mtd.size/16))
  491. status_new = (status_old & ~(SR_BP0|SR_BP1)) | SR_BP2;
  492. else if (offset < flash->mtd.size-(flash->mtd.size/32))
  493. status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  494. else if (offset < flash->mtd.size-(flash->mtd.size/64))
  495. status_new = (status_old & ~(SR_BP2|SR_BP0)) | SR_BP1;
  496. else
  497. status_new = (status_old & ~(SR_BP2|SR_BP1)) | SR_BP0;
  498. /* Only modify protection if it will not unlock other areas */
  499. if ((status_new&(SR_BP2|SR_BP1|SR_BP0)) >
  500. (status_old&(SR_BP2|SR_BP1|SR_BP0))) {
  501. write_enable(flash);
  502. if (write_sr(flash, status_new) < 0) {
  503. res = 1;
  504. goto err;
  505. }
  506. }
  507. err: mutex_unlock(&flash->lock);
  508. return res;
  509. }
  510. static int m25p80_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  511. {
  512. struct m25p *flash = mtd_to_m25p(mtd);
  513. uint32_t offset = ofs;
  514. uint8_t status_old, status_new;
  515. int res = 0;
  516. mutex_lock(&flash->lock);
  517. /* Wait until finished previous command */
  518. if (wait_till_ready(flash)) {
  519. res = 1;
  520. goto err;
  521. }
  522. status_old = read_sr(flash);
  523. if (offset+len > flash->mtd.size-(flash->mtd.size/64))
  524. status_new = status_old & ~(SR_BP2|SR_BP1|SR_BP0);
  525. else if (offset+len > flash->mtd.size-(flash->mtd.size/32))
  526. status_new = (status_old & ~(SR_BP2|SR_BP1)) | SR_BP0;
  527. else if (offset+len > flash->mtd.size-(flash->mtd.size/16))
  528. status_new = (status_old & ~(SR_BP2|SR_BP0)) | SR_BP1;
  529. else if (offset+len > flash->mtd.size-(flash->mtd.size/8))
  530. status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  531. else if (offset+len > flash->mtd.size-(flash->mtd.size/4))
  532. status_new = (status_old & ~(SR_BP0|SR_BP1)) | SR_BP2;
  533. else if (offset+len > flash->mtd.size-(flash->mtd.size/2))
  534. status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  535. else
  536. status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  537. /* Only modify protection if it will not lock other areas */
  538. if ((status_new&(SR_BP2|SR_BP1|SR_BP0)) <
  539. (status_old&(SR_BP2|SR_BP1|SR_BP0))) {
  540. write_enable(flash);
  541. if (write_sr(flash, status_new) < 0) {
  542. res = 1;
  543. goto err;
  544. }
  545. }
  546. err: mutex_unlock(&flash->lock);
  547. return res;
  548. }
  549. /****************************************************************************/
  550. /*
  551. * SPI device driver setup and teardown
  552. */
  553. struct flash_info {
  554. /* JEDEC id zero means "no ID" (most older chips); otherwise it has
  555. * a high byte of zero plus three data bytes: the manufacturer id,
  556. * then a two byte device id.
  557. */
  558. u32 jedec_id;
  559. u16 ext_id;
  560. /* The size listed here is what works with OPCODE_SE, which isn't
  561. * necessarily called a "sector" by the vendor.
  562. */
  563. unsigned sector_size;
  564. u16 n_sectors;
  565. u16 page_size;
  566. u16 addr_width;
  567. u16 flags;
  568. #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
  569. #define M25P_NO_ERASE 0x02 /* No erase command needed */
  570. #define SST_WRITE 0x04 /* use SST byte programming */
  571. #define M25P_NO_FR 0x08 /* Can't do fastread */
  572. #define SECT_4K_PMC 0x10 /* OPCODE_BE_4K_PMC works uniformly */
  573. };
  574. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  575. ((kernel_ulong_t)&(struct flash_info) { \
  576. .jedec_id = (_jedec_id), \
  577. .ext_id = (_ext_id), \
  578. .sector_size = (_sector_size), \
  579. .n_sectors = (_n_sectors), \
  580. .page_size = 256, \
  581. .flags = (_flags), \
  582. })
  583. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
  584. ((kernel_ulong_t)&(struct flash_info) { \
  585. .sector_size = (_sector_size), \
  586. .n_sectors = (_n_sectors), \
  587. .page_size = (_page_size), \
  588. .addr_width = (_addr_width), \
  589. .flags = (_flags), \
  590. })
  591. /* NOTE: double check command sets and memory organization when you add
  592. * more flash chips. This current list focusses on newer chips, which
  593. * have been converging on command sets which including JEDEC ID.
  594. */
  595. static const struct spi_device_id m25p_ids[] = {
  596. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  597. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  598. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  599. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  600. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  601. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  602. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  603. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  604. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  605. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  606. { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  607. /* EON -- en25xxx */
  608. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  609. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  610. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  611. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  612. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  613. { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  614. /* Everspin */
  615. { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, M25P_NO_ERASE | M25P_NO_FR) },
  616. { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, M25P_NO_ERASE | M25P_NO_FR) },
  617. /* GigaDevice */
  618. { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
  619. { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
  620. /* Intel/Numonyx -- xxxs33b */
  621. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  622. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  623. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  624. /* Macronix */
  625. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  626. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  627. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  628. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  629. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
  630. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
  631. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  632. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  633. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
  634. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  635. { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, 0) },
  636. /* Micron */
  637. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, 0) },
  638. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, 0) },
  639. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, 0) },
  640. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K) },
  641. /* PMC */
  642. { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
  643. { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
  644. { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
  645. /* Spansion -- single (large) sector size only, at least
  646. * for the chips listed here (without boot sectors).
  647. */
  648. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, 0) },
  649. { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
  650. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  651. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, 0) },
  652. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, 0) },
  653. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  654. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  655. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  656. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
  657. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
  658. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  659. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  660. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  661. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  662. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  663. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
  664. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  665. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  666. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  667. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  668. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
  669. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
  670. { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
  671. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  672. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  673. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  674. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  675. /* ST Microelectronics -- newer production may have feature updates */
  676. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  677. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  678. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  679. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  680. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  681. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  682. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  683. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  684. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  685. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, 0) },
  686. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  687. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  688. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  689. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  690. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  691. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  692. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  693. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  694. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  695. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  696. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  697. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  698. { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  699. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  700. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  701. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  702. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  703. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  704. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  705. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  706. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  707. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  708. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  709. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  710. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  711. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  712. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  713. { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
  714. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  715. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  716. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  717. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  718. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  719. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  720. { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
  721. /* Catalyst / On Semiconductor -- non-JEDEC */
  722. { "cat25c11", CAT25_INFO( 16, 8, 16, 1, M25P_NO_ERASE | M25P_NO_FR) },
  723. { "cat25c03", CAT25_INFO( 32, 8, 16, 2, M25P_NO_ERASE | M25P_NO_FR) },
  724. { "cat25c09", CAT25_INFO( 128, 8, 32, 2, M25P_NO_ERASE | M25P_NO_FR) },
  725. { "cat25c17", CAT25_INFO( 256, 8, 32, 2, M25P_NO_ERASE | M25P_NO_FR) },
  726. { "cat25128", CAT25_INFO(2048, 8, 64, 2, M25P_NO_ERASE | M25P_NO_FR) },
  727. { },
  728. };
  729. MODULE_DEVICE_TABLE(spi, m25p_ids);
  730. static const struct spi_device_id *jedec_probe(struct spi_device *spi)
  731. {
  732. int tmp;
  733. u8 code = OPCODE_RDID;
  734. u8 id[5];
  735. u32 jedec;
  736. u16 ext_jedec;
  737. struct flash_info *info;
  738. /* JEDEC also defines an optional "extended device information"
  739. * string for after vendor-specific data, after the three bytes
  740. * we use here. Supporting some chips might require using it.
  741. */
  742. tmp = spi_write_then_read(spi, &code, 1, id, 5);
  743. if (tmp < 0) {
  744. pr_debug("%s: error %d reading JEDEC ID\n",
  745. dev_name(&spi->dev), tmp);
  746. return ERR_PTR(tmp);
  747. }
  748. jedec = id[0];
  749. jedec = jedec << 8;
  750. jedec |= id[1];
  751. jedec = jedec << 8;
  752. jedec |= id[2];
  753. ext_jedec = id[3] << 8 | id[4];
  754. for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
  755. info = (void *)m25p_ids[tmp].driver_data;
  756. if (info->jedec_id == jedec) {
  757. if (info->ext_id != 0 && info->ext_id != ext_jedec)
  758. continue;
  759. return &m25p_ids[tmp];
  760. }
  761. }
  762. dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
  763. return ERR_PTR(-ENODEV);
  764. }
  765. /*
  766. * board specific setup should have ensured the SPI clock used here
  767. * matches what the READ command supports, at least until this driver
  768. * understands FAST_READ (for clocks over 25 MHz).
  769. */
  770. static int m25p_probe(struct spi_device *spi)
  771. {
  772. const struct spi_device_id *id = spi_get_device_id(spi);
  773. struct flash_platform_data *data;
  774. struct m25p *flash;
  775. struct flash_info *info;
  776. unsigned i;
  777. struct mtd_part_parser_data ppdata;
  778. struct device_node __maybe_unused *np = spi->dev.of_node;
  779. #ifdef CONFIG_MTD_OF_PARTS
  780. if (!of_device_is_available(np))
  781. return -ENODEV;
  782. #endif
  783. /* Platform data helps sort out which chip type we have, as
  784. * well as how this board partitions it. If we don't have
  785. * a chip ID, try the JEDEC id commands; they'll work for most
  786. * newer chips, even if we don't recognize the particular chip.
  787. */
  788. data = dev_get_platdata(&spi->dev);
  789. if (data && data->type) {
  790. const struct spi_device_id *plat_id;
  791. for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
  792. plat_id = &m25p_ids[i];
  793. if (strcmp(data->type, plat_id->name))
  794. continue;
  795. break;
  796. }
  797. if (i < ARRAY_SIZE(m25p_ids) - 1)
  798. id = plat_id;
  799. else
  800. dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
  801. }
  802. info = (void *)id->driver_data;
  803. if (info->jedec_id) {
  804. const struct spi_device_id *jid;
  805. jid = jedec_probe(spi);
  806. if (IS_ERR(jid)) {
  807. return PTR_ERR(jid);
  808. } else if (jid != id) {
  809. /*
  810. * JEDEC knows better, so overwrite platform ID. We
  811. * can't trust partitions any longer, but we'll let
  812. * mtd apply them anyway, since some partitions may be
  813. * marked read-only, and we don't want to lose that
  814. * information, even if it's not 100% accurate.
  815. */
  816. dev_warn(&spi->dev, "found %s, expected %s\n",
  817. jid->name, id->name);
  818. id = jid;
  819. info = (void *)jid->driver_data;
  820. }
  821. }
  822. flash = kzalloc(sizeof *flash, GFP_KERNEL);
  823. if (!flash)
  824. return -ENOMEM;
  825. flash->command = kmalloc(MAX_CMD_SIZE + (flash->fast_read ? 1 : 0),
  826. GFP_KERNEL);
  827. if (!flash->command) {
  828. kfree(flash);
  829. return -ENOMEM;
  830. }
  831. flash->spi = spi;
  832. mutex_init(&flash->lock);
  833. spi_set_drvdata(spi, flash);
  834. /*
  835. * Atmel, SST and Intel/Numonyx serial flash tend to power
  836. * up with the software protection bits set
  837. */
  838. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ATMEL ||
  839. JEDEC_MFR(info->jedec_id) == CFI_MFR_INTEL ||
  840. JEDEC_MFR(info->jedec_id) == CFI_MFR_SST) {
  841. write_enable(flash);
  842. write_sr(flash, 0);
  843. }
  844. if (data && data->name)
  845. flash->mtd.name = data->name;
  846. else
  847. flash->mtd.name = dev_name(&spi->dev);
  848. flash->mtd.type = MTD_NORFLASH;
  849. flash->mtd.writesize = 1;
  850. flash->mtd.flags = MTD_CAP_NORFLASH;
  851. flash->mtd.size = info->sector_size * info->n_sectors;
  852. flash->mtd._erase = m25p80_erase;
  853. flash->mtd._read = m25p80_read;
  854. /* flash protection support for STmicro chips */
  855. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ST) {
  856. flash->mtd._lock = m25p80_lock;
  857. flash->mtd._unlock = m25p80_unlock;
  858. }
  859. /* sst flash chips use AAI word program */
  860. if (info->flags & SST_WRITE)
  861. flash->mtd._write = sst_write;
  862. else
  863. flash->mtd._write = m25p80_write;
  864. /* prefer "small sector" erase if possible */
  865. if (info->flags & SECT_4K) {
  866. flash->erase_opcode = OPCODE_BE_4K;
  867. flash->mtd.erasesize = 4096;
  868. } else if (info->flags & SECT_4K_PMC) {
  869. flash->erase_opcode = OPCODE_BE_4K_PMC;
  870. flash->mtd.erasesize = 4096;
  871. } else {
  872. flash->erase_opcode = OPCODE_SE;
  873. flash->mtd.erasesize = info->sector_size;
  874. }
  875. if (info->flags & M25P_NO_ERASE)
  876. flash->mtd.flags |= MTD_NO_ERASE;
  877. ppdata.of_node = spi->dev.of_node;
  878. flash->mtd.dev.parent = &spi->dev;
  879. flash->page_size = info->page_size;
  880. flash->mtd.writebufsize = flash->page_size;
  881. flash->fast_read = false;
  882. if (np && of_property_read_bool(np, "m25p,fast-read"))
  883. flash->fast_read = true;
  884. #ifdef CONFIG_M25PXX_USE_FAST_READ
  885. flash->fast_read = true;
  886. #endif
  887. if (info->flags & M25P_NO_FR)
  888. flash->fast_read = false;
  889. /* Default commands */
  890. if (flash->fast_read)
  891. flash->read_opcode = OPCODE_FAST_READ;
  892. else
  893. flash->read_opcode = OPCODE_NORM_READ;
  894. flash->program_opcode = OPCODE_PP;
  895. if (info->addr_width)
  896. flash->addr_width = info->addr_width;
  897. else if (flash->mtd.size > 0x1000000) {
  898. /* enable 4-byte addressing if the device exceeds 16MiB */
  899. flash->addr_width = 4;
  900. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_AMD) {
  901. /* Dedicated 4-byte command set */
  902. flash->read_opcode = flash->fast_read ?
  903. OPCODE_FAST_READ_4B :
  904. OPCODE_NORM_READ_4B;
  905. flash->program_opcode = OPCODE_PP_4B;
  906. /* No small sector erase for 4-byte command set */
  907. flash->erase_opcode = OPCODE_SE_4B;
  908. flash->mtd.erasesize = info->sector_size;
  909. } else
  910. set_4byte(flash, info->jedec_id, 1);
  911. } else {
  912. flash->addr_width = 3;
  913. }
  914. dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
  915. (long long)flash->mtd.size >> 10);
  916. pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
  917. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  918. flash->mtd.name,
  919. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  920. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  921. flash->mtd.numeraseregions);
  922. if (flash->mtd.numeraseregions)
  923. for (i = 0; i < flash->mtd.numeraseregions; i++)
  924. pr_debug("mtd.eraseregions[%d] = { .offset = 0x%llx, "
  925. ".erasesize = 0x%.8x (%uKiB), "
  926. ".numblocks = %d }\n",
  927. i, (long long)flash->mtd.eraseregions[i].offset,
  928. flash->mtd.eraseregions[i].erasesize,
  929. flash->mtd.eraseregions[i].erasesize / 1024,
  930. flash->mtd.eraseregions[i].numblocks);
  931. /* partitions should match sector boundaries; and it may be good to
  932. * use readonly partitions for writeprotected sectors (BP2..BP0).
  933. */
  934. return mtd_device_parse_register(&flash->mtd, NULL, &ppdata,
  935. data ? data->parts : NULL,
  936. data ? data->nr_parts : 0);
  937. }
  938. static int m25p_remove(struct spi_device *spi)
  939. {
  940. struct m25p *flash = spi_get_drvdata(spi);
  941. int status;
  942. /* Clean up MTD stuff. */
  943. status = mtd_device_unregister(&flash->mtd);
  944. if (status == 0) {
  945. kfree(flash->command);
  946. kfree(flash);
  947. }
  948. return 0;
  949. }
  950. static struct spi_driver m25p80_driver = {
  951. .driver = {
  952. .name = "m25p80",
  953. .owner = THIS_MODULE,
  954. },
  955. .id_table = m25p_ids,
  956. .probe = m25p_probe,
  957. .remove = m25p_remove,
  958. /* REVISIT: many of these chips have deep power-down modes, which
  959. * should clearly be entered on suspend() to minimize power use.
  960. * And also when they're otherwise idle...
  961. */
  962. };
  963. module_spi_driver(m25p80_driver);
  964. MODULE_LICENSE("GPL");
  965. MODULE_AUTHOR("Mike Lavender");
  966. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");