m25p80.c 18 KB

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