m25p80.c 18 KB

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