m25p80.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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/module.h>
  19. #include <linux/device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mutex.h>
  22. #include <linux/math64.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/partitions.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/spi/flash.h>
  27. #define FLASH_PAGESIZE 256
  28. /* Flash opcodes. */
  29. #define OPCODE_WREN 0x06 /* Write enable */
  30. #define OPCODE_RDSR 0x05 /* Read status register */
  31. #define OPCODE_WRSR 0x01 /* Write status register 1 byte */
  32. #define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
  33. #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
  34. #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
  35. #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
  36. #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
  37. #define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
  38. #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
  39. #define OPCODE_RDID 0x9f /* Read JEDEC ID */
  40. /* Used for SST flashes only. */
  41. #define OPCODE_BP 0x02 /* Byte program */
  42. #define OPCODE_WRDI 0x04 /* Write disable */
  43. #define OPCODE_AAI_WP 0xad /* Auto address increment word program */
  44. /* Status Register bits. */
  45. #define SR_WIP 1 /* Write in progress */
  46. #define SR_WEL 2 /* Write enable latch */
  47. /* meaning of other SR_* bits may differ between vendors */
  48. #define SR_BP0 4 /* Block protect 0 */
  49. #define SR_BP1 8 /* Block protect 1 */
  50. #define SR_BP2 0x10 /* Block protect 2 */
  51. #define SR_SRWD 0x80 /* SR write protect */
  52. /* Define max times to check status register before we give up. */
  53. #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
  54. #define CMD_SIZE 4
  55. #ifdef CONFIG_M25PXX_USE_FAST_READ
  56. #define OPCODE_READ OPCODE_FAST_READ
  57. #define FAST_READ_DUMMY_BYTE 1
  58. #else
  59. #define OPCODE_READ OPCODE_NORM_READ
  60. #define FAST_READ_DUMMY_BYTE 0
  61. #endif
  62. /****************************************************************************/
  63. struct m25p {
  64. struct spi_device *spi;
  65. struct mutex lock;
  66. struct mtd_info mtd;
  67. unsigned partitioned:1;
  68. u8 erase_opcode;
  69. u8 command[CMD_SIZE + FAST_READ_DUMMY_BYTE];
  70. };
  71. static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
  72. {
  73. return container_of(mtd, struct m25p, mtd);
  74. }
  75. /****************************************************************************/
  76. /*
  77. * Internal helper functions
  78. */
  79. /*
  80. * Read the status register, returning its value in the location
  81. * Return the status register value.
  82. * Returns negative if error occurred.
  83. */
  84. static int read_sr(struct m25p *flash)
  85. {
  86. ssize_t retval;
  87. u8 code = OPCODE_RDSR;
  88. u8 val;
  89. retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
  90. if (retval < 0) {
  91. dev_err(&flash->spi->dev, "error %d reading SR\n",
  92. (int) retval);
  93. return retval;
  94. }
  95. return val;
  96. }
  97. /*
  98. * Write status register 1 byte
  99. * Returns negative if error occurred.
  100. */
  101. static int write_sr(struct m25p *flash, u8 val)
  102. {
  103. flash->command[0] = OPCODE_WRSR;
  104. flash->command[1] = val;
  105. return spi_write(flash->spi, flash->command, 2);
  106. }
  107. /*
  108. * Set write enable latch with Write Enable command.
  109. * Returns negative if error occurred.
  110. */
  111. static inline int write_enable(struct m25p *flash)
  112. {
  113. u8 code = OPCODE_WREN;
  114. return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
  115. }
  116. /*
  117. * Send write disble instruction to the chip.
  118. */
  119. static inline int write_disable(struct m25p *flash)
  120. {
  121. u8 code = OPCODE_WRDI;
  122. return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
  123. }
  124. /*
  125. * Service routine to read status register until ready, or timeout occurs.
  126. * Returns non-zero if error.
  127. */
  128. static int wait_till_ready(struct m25p *flash)
  129. {
  130. unsigned long deadline;
  131. int sr;
  132. deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  133. do {
  134. if ((sr = read_sr(flash)) < 0)
  135. break;
  136. else if (!(sr & SR_WIP))
  137. return 0;
  138. cond_resched();
  139. } while (!time_after_eq(jiffies, deadline));
  140. return 1;
  141. }
  142. /*
  143. * Erase the whole flash memory
  144. *
  145. * Returns 0 if successful, non-zero otherwise.
  146. */
  147. static int erase_chip(struct m25p *flash)
  148. {
  149. DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
  150. dev_name(&flash->spi->dev), __func__,
  151. (long long)(flash->mtd.size >> 10));
  152. /* Wait until finished previous write command. */
  153. if (wait_till_ready(flash))
  154. return 1;
  155. /* Send write enable, then erase commands. */
  156. write_enable(flash);
  157. /* Set up command buffer. */
  158. flash->command[0] = OPCODE_CHIP_ERASE;
  159. spi_write(flash->spi, flash->command, 1);
  160. return 0;
  161. }
  162. /*
  163. * Erase one sector of flash memory at offset ``offset'' which is any
  164. * address within the sector which should be erased.
  165. *
  166. * Returns 0 if successful, non-zero otherwise.
  167. */
  168. static int erase_sector(struct m25p *flash, u32 offset)
  169. {
  170. DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
  171. dev_name(&flash->spi->dev), __func__,
  172. flash->mtd.erasesize / 1024, offset);
  173. /* Wait until finished previous write command. */
  174. if (wait_till_ready(flash))
  175. return 1;
  176. /* Send write enable, then erase commands. */
  177. write_enable(flash);
  178. /* Set up command buffer. */
  179. flash->command[0] = flash->erase_opcode;
  180. flash->command[1] = offset >> 16;
  181. flash->command[2] = offset >> 8;
  182. flash->command[3] = offset;
  183. spi_write(flash->spi, flash->command, CMD_SIZE);
  184. return 0;
  185. }
  186. /****************************************************************************/
  187. /*
  188. * MTD implementation
  189. */
  190. /*
  191. * Erase an address range on the flash chip. The address range may extend
  192. * one or more erase sectors. Return an error is there is a problem erasing.
  193. */
  194. static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
  195. {
  196. struct m25p *flash = mtd_to_m25p(mtd);
  197. u32 addr,len;
  198. uint32_t rem;
  199. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
  200. dev_name(&flash->spi->dev), __func__, "at",
  201. (long long)instr->addr, (long long)instr->len);
  202. /* sanity checks */
  203. if (instr->addr + instr->len > flash->mtd.size)
  204. return -EINVAL;
  205. div_u64_rem(instr->len, mtd->erasesize, &rem);
  206. if (rem)
  207. return -EINVAL;
  208. addr = instr->addr;
  209. len = instr->len;
  210. mutex_lock(&flash->lock);
  211. /* whole-chip erase? */
  212. if (len == flash->mtd.size) {
  213. if (erase_chip(flash)) {
  214. instr->state = MTD_ERASE_FAILED;
  215. mutex_unlock(&flash->lock);
  216. return -EIO;
  217. }
  218. /* REVISIT in some cases we could speed up erasing large regions
  219. * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
  220. * to use "small sector erase", but that's not always optimal.
  221. */
  222. /* "sector"-at-a-time erase */
  223. } else {
  224. while (len) {
  225. if (erase_sector(flash, addr)) {
  226. instr->state = MTD_ERASE_FAILED;
  227. mutex_unlock(&flash->lock);
  228. return -EIO;
  229. }
  230. addr += mtd->erasesize;
  231. len -= mtd->erasesize;
  232. }
  233. }
  234. mutex_unlock(&flash->lock);
  235. instr->state = MTD_ERASE_DONE;
  236. mtd_erase_callback(instr);
  237. return 0;
  238. }
  239. /*
  240. * Read an address range from the flash chip. The address range
  241. * may be any size provided it is within the physical boundaries.
  242. */
  243. static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
  244. size_t *retlen, u_char *buf)
  245. {
  246. struct m25p *flash = mtd_to_m25p(mtd);
  247. struct spi_transfer t[2];
  248. struct spi_message m;
  249. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
  250. dev_name(&flash->spi->dev), __func__, "from",
  251. (u32)from, len);
  252. /* sanity checks */
  253. if (!len)
  254. return 0;
  255. if (from + len > flash->mtd.size)
  256. return -EINVAL;
  257. spi_message_init(&m);
  258. memset(t, 0, (sizeof t));
  259. /* NOTE:
  260. * OPCODE_FAST_READ (if available) is faster.
  261. * Should add 1 byte DUMMY_BYTE.
  262. */
  263. t[0].tx_buf = flash->command;
  264. t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE;
  265. spi_message_add_tail(&t[0], &m);
  266. t[1].rx_buf = buf;
  267. t[1].len = len;
  268. spi_message_add_tail(&t[1], &m);
  269. /* Byte count starts at zero. */
  270. if (retlen)
  271. *retlen = 0;
  272. mutex_lock(&flash->lock);
  273. /* Wait till previous write/erase is done. */
  274. if (wait_till_ready(flash)) {
  275. /* REVISIT status return?? */
  276. mutex_unlock(&flash->lock);
  277. return 1;
  278. }
  279. /* FIXME switch to OPCODE_FAST_READ. It's required for higher
  280. * clocks; and at this writing, every chip this driver handles
  281. * supports that opcode.
  282. */
  283. /* Set up the write data buffer. */
  284. flash->command[0] = OPCODE_READ;
  285. flash->command[1] = from >> 16;
  286. flash->command[2] = from >> 8;
  287. flash->command[3] = from;
  288. spi_sync(flash->spi, &m);
  289. *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE;
  290. mutex_unlock(&flash->lock);
  291. return 0;
  292. }
  293. /*
  294. * Write an address range to the flash chip. Data must be written in
  295. * FLASH_PAGESIZE chunks. The address range may be any size provided
  296. * it is within the physical boundaries.
  297. */
  298. static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
  299. size_t *retlen, const u_char *buf)
  300. {
  301. struct m25p *flash = mtd_to_m25p(mtd);
  302. u32 page_offset, page_size;
  303. struct spi_transfer t[2];
  304. struct spi_message m;
  305. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
  306. dev_name(&flash->spi->dev), __func__, "to",
  307. (u32)to, len);
  308. if (retlen)
  309. *retlen = 0;
  310. /* sanity checks */
  311. if (!len)
  312. return(0);
  313. if (to + len > flash->mtd.size)
  314. return -EINVAL;
  315. spi_message_init(&m);
  316. memset(t, 0, (sizeof t));
  317. t[0].tx_buf = flash->command;
  318. t[0].len = CMD_SIZE;
  319. spi_message_add_tail(&t[0], &m);
  320. t[1].tx_buf = buf;
  321. spi_message_add_tail(&t[1], &m);
  322. mutex_lock(&flash->lock);
  323. /* Wait until finished previous write command. */
  324. if (wait_till_ready(flash)) {
  325. mutex_unlock(&flash->lock);
  326. return 1;
  327. }
  328. write_enable(flash);
  329. /* Set up the opcode in the write buffer. */
  330. flash->command[0] = OPCODE_PP;
  331. flash->command[1] = to >> 16;
  332. flash->command[2] = to >> 8;
  333. flash->command[3] = to;
  334. /* what page do we start with? */
  335. page_offset = to % FLASH_PAGESIZE;
  336. /* do all the bytes fit onto one page? */
  337. if (page_offset + len <= FLASH_PAGESIZE) {
  338. t[1].len = len;
  339. spi_sync(flash->spi, &m);
  340. *retlen = m.actual_length - CMD_SIZE;
  341. } else {
  342. u32 i;
  343. /* the size of data remaining on the first page */
  344. page_size = FLASH_PAGESIZE - page_offset;
  345. t[1].len = page_size;
  346. spi_sync(flash->spi, &m);
  347. *retlen = m.actual_length - CMD_SIZE;
  348. /* write everything in PAGESIZE chunks */
  349. for (i = page_size; i < len; i += page_size) {
  350. page_size = len - i;
  351. if (page_size > FLASH_PAGESIZE)
  352. page_size = FLASH_PAGESIZE;
  353. /* write the next page to flash */
  354. flash->command[1] = (to + i) >> 16;
  355. flash->command[2] = (to + i) >> 8;
  356. flash->command[3] = (to + i);
  357. t[1].tx_buf = buf + i;
  358. t[1].len = page_size;
  359. wait_till_ready(flash);
  360. write_enable(flash);
  361. spi_sync(flash->spi, &m);
  362. if (retlen)
  363. *retlen += m.actual_length - CMD_SIZE;
  364. }
  365. }
  366. mutex_unlock(&flash->lock);
  367. return 0;
  368. }
  369. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  370. size_t *retlen, const u_char *buf)
  371. {
  372. struct m25p *flash = mtd_to_m25p(mtd);
  373. struct spi_transfer t[2];
  374. struct spi_message m;
  375. size_t actual;
  376. int cmd_sz, ret;
  377. if (retlen)
  378. *retlen = 0;
  379. /* sanity checks */
  380. if (!len)
  381. return 0;
  382. if (to + len > flash->mtd.size)
  383. return -EINVAL;
  384. spi_message_init(&m);
  385. memset(t, 0, (sizeof t));
  386. t[0].tx_buf = flash->command;
  387. t[0].len = CMD_SIZE;
  388. spi_message_add_tail(&t[0], &m);
  389. t[1].tx_buf = buf;
  390. spi_message_add_tail(&t[1], &m);
  391. mutex_lock(&flash->lock);
  392. /* Wait until finished previous write command. */
  393. ret = wait_till_ready(flash);
  394. if (ret)
  395. goto time_out;
  396. write_enable(flash);
  397. actual = to % 2;
  398. /* Start write from odd address. */
  399. if (actual) {
  400. flash->command[0] = OPCODE_BP;
  401. flash->command[1] = to >> 16;
  402. flash->command[2] = to >> 8;
  403. flash->command[3] = to;
  404. /* write one byte. */
  405. t[1].len = 1;
  406. spi_sync(flash->spi, &m);
  407. ret = wait_till_ready(flash);
  408. if (ret)
  409. goto time_out;
  410. *retlen += m.actual_length - CMD_SIZE;
  411. }
  412. to += actual;
  413. flash->command[0] = OPCODE_AAI_WP;
  414. flash->command[1] = to >> 16;
  415. flash->command[2] = to >> 8;
  416. flash->command[3] = to;
  417. /* Write out most of the data here. */
  418. cmd_sz = CMD_SIZE;
  419. for (; actual < len - 1; actual += 2) {
  420. t[0].len = cmd_sz;
  421. /* write two bytes. */
  422. t[1].len = 2;
  423. t[1].tx_buf = buf + actual;
  424. spi_sync(flash->spi, &m);
  425. ret = wait_till_ready(flash);
  426. if (ret)
  427. goto time_out;
  428. *retlen += m.actual_length - cmd_sz;
  429. cmd_sz = 1;
  430. to += 2;
  431. }
  432. write_disable(flash);
  433. ret = wait_till_ready(flash);
  434. if (ret)
  435. goto time_out;
  436. /* Write out trailing byte if it exists. */
  437. if (actual != len) {
  438. write_enable(flash);
  439. flash->command[0] = OPCODE_BP;
  440. flash->command[1] = to >> 16;
  441. flash->command[2] = to >> 8;
  442. flash->command[3] = to;
  443. t[0].len = CMD_SIZE;
  444. t[1].len = 1;
  445. t[1].tx_buf = buf + actual;
  446. spi_sync(flash->spi, &m);
  447. ret = wait_till_ready(flash);
  448. if (ret)
  449. goto time_out;
  450. *retlen += m.actual_length - CMD_SIZE;
  451. write_disable(flash);
  452. }
  453. time_out:
  454. mutex_unlock(&flash->lock);
  455. return ret;
  456. }
  457. /****************************************************************************/
  458. /*
  459. * SPI device driver setup and teardown
  460. */
  461. struct flash_info {
  462. char *name;
  463. /* JEDEC id zero means "no ID" (most older chips); otherwise it has
  464. * a high byte of zero plus three data bytes: the manufacturer id,
  465. * then a two byte device id.
  466. */
  467. u32 jedec_id;
  468. u16 ext_id;
  469. /* The size listed here is what works with OPCODE_SE, which isn't
  470. * necessarily called a "sector" by the vendor.
  471. */
  472. unsigned sector_size;
  473. u16 n_sectors;
  474. u16 flags;
  475. #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
  476. };
  477. /* NOTE: double check command sets and memory organization when you add
  478. * more flash chips. This current list focusses on newer chips, which
  479. * have been converging on command sets which including JEDEC ID.
  480. */
  481. static struct flash_info __devinitdata m25p_data [] = {
  482. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  483. { "at25fs010", 0x1f6601, 0, 32 * 1024, 4, SECT_4K, },
  484. { "at25fs040", 0x1f6604, 0, 64 * 1024, 8, SECT_4K, },
  485. { "at25df041a", 0x1f4401, 0, 64 * 1024, 8, SECT_4K, },
  486. { "at25df641", 0x1f4800, 0, 64 * 1024, 128, SECT_4K, },
  487. { "at26f004", 0x1f0400, 0, 64 * 1024, 8, SECT_4K, },
  488. { "at26df081a", 0x1f4501, 0, 64 * 1024, 16, SECT_4K, },
  489. { "at26df161a", 0x1f4601, 0, 64 * 1024, 32, SECT_4K, },
  490. { "at26df321", 0x1f4701, 0, 64 * 1024, 64, SECT_4K, },
  491. /* Macronix */
  492. { "mx25l3205d", 0xc22016, 0, 64 * 1024, 64, },
  493. { "mx25l6405d", 0xc22017, 0, 64 * 1024, 128, },
  494. { "mx25l12805d", 0xc22018, 0, 64 * 1024, 256, },
  495. { "mx25l12855e", 0xc22618, 0, 64 * 1024, 256, },
  496. /* Spansion -- single (large) sector size only, at least
  497. * for the chips listed here (without boot sectors).
  498. */
  499. { "s25sl004a", 0x010212, 0, 64 * 1024, 8, },
  500. { "s25sl008a", 0x010213, 0, 64 * 1024, 16, },
  501. { "s25sl016a", 0x010214, 0, 64 * 1024, 32, },
  502. { "s25sl032a", 0x010215, 0, 64 * 1024, 64, },
  503. { "s25sl064a", 0x010216, 0, 64 * 1024, 128, },
  504. { "s25sl12800", 0x012018, 0x0300, 256 * 1024, 64, },
  505. { "s25sl12801", 0x012018, 0x0301, 64 * 1024, 256, },
  506. { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, },
  507. { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, },
  508. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  509. { "sst25vf040b", 0xbf258d, 0, 64 * 1024, 8, SECT_4K, },
  510. { "sst25vf080b", 0xbf258e, 0, 64 * 1024, 16, SECT_4K, },
  511. { "sst25vf016b", 0xbf2541, 0, 64 * 1024, 32, SECT_4K, },
  512. { "sst25vf032b", 0xbf254a, 0, 64 * 1024, 64, SECT_4K, },
  513. { "sst25wf512", 0xbf2501, 0, 64 * 1024, 1, SECT_4K, },
  514. { "sst25wf010", 0xbf2502, 0, 64 * 1024, 2, SECT_4K, },
  515. { "sst25wf020", 0xbf2503, 0, 64 * 1024, 4, SECT_4K, },
  516. { "sst25wf040", 0xbf2504, 0, 64 * 1024, 8, SECT_4K, },
  517. /* ST Microelectronics -- newer production may have feature updates */
  518. { "m25p05", 0x202010, 0, 32 * 1024, 2, },
  519. { "m25p10", 0x202011, 0, 32 * 1024, 4, },
  520. { "m25p20", 0x202012, 0, 64 * 1024, 4, },
  521. { "m25p40", 0x202013, 0, 64 * 1024, 8, },
  522. { "m25p80", 0, 0, 64 * 1024, 16, },
  523. { "m25p16", 0x202015, 0, 64 * 1024, 32, },
  524. { "m25p32", 0x202016, 0, 64 * 1024, 64, },
  525. { "m25p64", 0x202017, 0, 64 * 1024, 128, },
  526. { "m25p128", 0x202018, 0, 256 * 1024, 64, },
  527. { "m45pe10", 0x204011, 0, 64 * 1024, 2, },
  528. { "m45pe80", 0x204014, 0, 64 * 1024, 16, },
  529. { "m45pe16", 0x204015, 0, 64 * 1024, 32, },
  530. { "m25pe80", 0x208014, 0, 64 * 1024, 16, },
  531. { "m25pe16", 0x208015, 0, 64 * 1024, 32, SECT_4K, },
  532. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  533. { "w25x10", 0xef3011, 0, 64 * 1024, 2, SECT_4K, },
  534. { "w25x20", 0xef3012, 0, 64 * 1024, 4, SECT_4K, },
  535. { "w25x40", 0xef3013, 0, 64 * 1024, 8, SECT_4K, },
  536. { "w25x80", 0xef3014, 0, 64 * 1024, 16, SECT_4K, },
  537. { "w25x16", 0xef3015, 0, 64 * 1024, 32, SECT_4K, },
  538. { "w25x32", 0xef3016, 0, 64 * 1024, 64, SECT_4K, },
  539. { "w25x64", 0xef3017, 0, 64 * 1024, 128, SECT_4K, },
  540. };
  541. static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
  542. {
  543. int tmp;
  544. u8 code = OPCODE_RDID;
  545. u8 id[5];
  546. u32 jedec;
  547. u16 ext_jedec;
  548. struct flash_info *info;
  549. /* JEDEC also defines an optional "extended device information"
  550. * string for after vendor-specific data, after the three bytes
  551. * we use here. Supporting some chips might require using it.
  552. */
  553. tmp = spi_write_then_read(spi, &code, 1, id, 5);
  554. if (tmp < 0) {
  555. DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
  556. dev_name(&spi->dev), tmp);
  557. return NULL;
  558. }
  559. jedec = id[0];
  560. jedec = jedec << 8;
  561. jedec |= id[1];
  562. jedec = jedec << 8;
  563. jedec |= id[2];
  564. ext_jedec = id[3] << 8 | id[4];
  565. for (tmp = 0, info = m25p_data;
  566. tmp < ARRAY_SIZE(m25p_data);
  567. tmp++, info++) {
  568. if (info->jedec_id == jedec) {
  569. if (info->ext_id != 0 && info->ext_id != ext_jedec)
  570. continue;
  571. return info;
  572. }
  573. }
  574. dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
  575. return NULL;
  576. }
  577. /*
  578. * board specific setup should have ensured the SPI clock used here
  579. * matches what the READ command supports, at least until this driver
  580. * understands FAST_READ (for clocks over 25 MHz).
  581. */
  582. static int __devinit m25p_probe(struct spi_device *spi)
  583. {
  584. struct flash_platform_data *data;
  585. struct m25p *flash;
  586. struct flash_info *info;
  587. unsigned i;
  588. /* Platform data helps sort out which chip type we have, as
  589. * well as how this board partitions it. If we don't have
  590. * a chip ID, try the JEDEC id commands; they'll work for most
  591. * newer chips, even if we don't recognize the particular chip.
  592. */
  593. data = spi->dev.platform_data;
  594. if (data && data->type) {
  595. for (i = 0, info = m25p_data;
  596. i < ARRAY_SIZE(m25p_data);
  597. i++, info++) {
  598. if (strcmp(data->type, info->name) == 0)
  599. break;
  600. }
  601. /* unrecognized chip? */
  602. if (i == ARRAY_SIZE(m25p_data)) {
  603. DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
  604. dev_name(&spi->dev), data->type);
  605. info = NULL;
  606. /* recognized; is that chip really what's there? */
  607. } else if (info->jedec_id) {
  608. struct flash_info *chip = jedec_probe(spi);
  609. if (!chip || chip != info) {
  610. dev_warn(&spi->dev, "found %s, expected %s\n",
  611. chip ? chip->name : "UNKNOWN",
  612. info->name);
  613. info = NULL;
  614. }
  615. }
  616. } else
  617. info = jedec_probe(spi);
  618. if (!info)
  619. return -ENODEV;
  620. flash = kzalloc(sizeof *flash, GFP_KERNEL);
  621. if (!flash)
  622. return -ENOMEM;
  623. flash->spi = spi;
  624. mutex_init(&flash->lock);
  625. dev_set_drvdata(&spi->dev, flash);
  626. /*
  627. * Atmel serial flash tend to power up
  628. * with the software protection bits set
  629. */
  630. if (info->jedec_id >> 16 == 0x1f) {
  631. write_enable(flash);
  632. write_sr(flash, 0);
  633. }
  634. if (data && data->name)
  635. flash->mtd.name = data->name;
  636. else
  637. flash->mtd.name = dev_name(&spi->dev);
  638. flash->mtd.type = MTD_NORFLASH;
  639. flash->mtd.writesize = 1;
  640. flash->mtd.flags = MTD_CAP_NORFLASH;
  641. flash->mtd.size = info->sector_size * info->n_sectors;
  642. flash->mtd.erase = m25p80_erase;
  643. flash->mtd.read = m25p80_read;
  644. /* sst flash chips use AAI word program */
  645. if (info->jedec_id >> 16 == 0xbf)
  646. flash->mtd.write = sst_write;
  647. else
  648. flash->mtd.write = m25p80_write;
  649. /* prefer "small sector" erase if possible */
  650. if (info->flags & SECT_4K) {
  651. flash->erase_opcode = OPCODE_BE_4K;
  652. flash->mtd.erasesize = 4096;
  653. } else {
  654. flash->erase_opcode = OPCODE_SE;
  655. flash->mtd.erasesize = info->sector_size;
  656. }
  657. flash->mtd.dev.parent = &spi->dev;
  658. dev_info(&spi->dev, "%s (%lld Kbytes)\n", info->name,
  659. (long long)flash->mtd.size >> 10);
  660. DEBUG(MTD_DEBUG_LEVEL2,
  661. "mtd .name = %s, .size = 0x%llx (%lldMiB) "
  662. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  663. flash->mtd.name,
  664. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  665. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  666. flash->mtd.numeraseregions);
  667. if (flash->mtd.numeraseregions)
  668. for (i = 0; i < flash->mtd.numeraseregions; i++)
  669. DEBUG(MTD_DEBUG_LEVEL2,
  670. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  671. ".erasesize = 0x%.8x (%uKiB), "
  672. ".numblocks = %d }\n",
  673. i, (long long)flash->mtd.eraseregions[i].offset,
  674. flash->mtd.eraseregions[i].erasesize,
  675. flash->mtd.eraseregions[i].erasesize / 1024,
  676. flash->mtd.eraseregions[i].numblocks);
  677. /* partitions should match sector boundaries; and it may be good to
  678. * use readonly partitions for writeprotected sectors (BP2..BP0).
  679. */
  680. if (mtd_has_partitions()) {
  681. struct mtd_partition *parts = NULL;
  682. int nr_parts = 0;
  683. if (mtd_has_cmdlinepart()) {
  684. static const char *part_probes[]
  685. = { "cmdlinepart", NULL, };
  686. nr_parts = parse_mtd_partitions(&flash->mtd,
  687. part_probes, &parts, 0);
  688. }
  689. if (nr_parts <= 0 && data && data->parts) {
  690. parts = data->parts;
  691. nr_parts = data->nr_parts;
  692. }
  693. if (nr_parts > 0) {
  694. for (i = 0; i < nr_parts; i++) {
  695. DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
  696. "{.name = %s, .offset = 0x%llx, "
  697. ".size = 0x%llx (%lldKiB) }\n",
  698. i, parts[i].name,
  699. (long long)parts[i].offset,
  700. (long long)parts[i].size,
  701. (long long)(parts[i].size >> 10));
  702. }
  703. flash->partitioned = 1;
  704. return add_mtd_partitions(&flash->mtd, parts, nr_parts);
  705. }
  706. } else if (data && data->nr_parts)
  707. dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
  708. data->nr_parts, data->name);
  709. return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
  710. }
  711. static int __devexit m25p_remove(struct spi_device *spi)
  712. {
  713. struct m25p *flash = dev_get_drvdata(&spi->dev);
  714. int status;
  715. /* Clean up MTD stuff. */
  716. if (mtd_has_partitions() && flash->partitioned)
  717. status = del_mtd_partitions(&flash->mtd);
  718. else
  719. status = del_mtd_device(&flash->mtd);
  720. if (status == 0)
  721. kfree(flash);
  722. return 0;
  723. }
  724. static struct spi_driver m25p80_driver = {
  725. .driver = {
  726. .name = "m25p80",
  727. .bus = &spi_bus_type,
  728. .owner = THIS_MODULE,
  729. },
  730. .probe = m25p_probe,
  731. .remove = __devexit_p(m25p_remove),
  732. /* REVISIT: many of these chips have deep power-down modes, which
  733. * should clearly be entered on suspend() to minimize power use.
  734. * And also when they're otherwise idle...
  735. */
  736. };
  737. static int __init m25p80_init(void)
  738. {
  739. return spi_register_driver(&m25p80_driver);
  740. }
  741. static void __exit m25p80_exit(void)
  742. {
  743. spi_unregister_driver(&m25p80_driver);
  744. }
  745. module_init(m25p80_init);
  746. module_exit(m25p80_exit);
  747. MODULE_LICENSE("GPL");
  748. MODULE_AUTHOR("Mike Lavender");
  749. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");