m25p80.c 20 KB

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