m25p80.c 20 KB

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