m25p80.c 33 KB

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