m25p80.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * MTD SPI driver for ST M25Pxx 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/interrupt.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. #include <asm/semaphore.h>
  27. /* NOTE: AT 25F and SST 25LF series are very similar,
  28. * but commands for sector erase and chip id differ...
  29. */
  30. #define FLASH_PAGESIZE 256
  31. /* Flash opcodes. */
  32. #define OPCODE_WREN 6 /* Write enable */
  33. #define OPCODE_RDSR 5 /* Read status register */
  34. #define OPCODE_READ 3 /* Read data bytes */
  35. #define OPCODE_PP 2 /* Page program */
  36. #define OPCODE_SE 0xd8 /* Sector erase */
  37. #define OPCODE_RES 0xab /* Read Electronic Signature */
  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. #define SR_BP0 4 /* Block protect 0 */
  43. #define SR_BP1 8 /* Block protect 1 */
  44. #define SR_BP2 0x10 /* Block protect 2 */
  45. #define SR_SRWD 0x80 /* SR write protect */
  46. /* Define max times to check status register before we give up. */
  47. #define MAX_READY_WAIT_COUNT 100000
  48. #ifdef CONFIG_MTD_PARTITIONS
  49. #define mtd_has_partitions() (1)
  50. #else
  51. #define mtd_has_partitions() (0)
  52. #endif
  53. /****************************************************************************/
  54. struct m25p {
  55. struct spi_device *spi;
  56. struct semaphore lock;
  57. struct mtd_info mtd;
  58. unsigned partitioned;
  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 at 0x%08x\n", flash->spi->dev.bus_id,
  125. __FUNCTION__, offset);
  126. /* Wait until finished previous write command. */
  127. if (wait_till_ready(flash))
  128. return 1;
  129. /* Send write enable, then erase commands. */
  130. write_enable(flash);
  131. /* Set up command buffer. */
  132. flash->command[0] = OPCODE_SE;
  133. flash->command[1] = offset >> 16;
  134. flash->command[2] = offset >> 8;
  135. flash->command[3] = offset;
  136. spi_write(flash->spi, flash->command, sizeof(flash->command));
  137. return 0;
  138. }
  139. /****************************************************************************/
  140. /*
  141. * MTD implementation
  142. */
  143. /*
  144. * Erase an address range on the flash chip. The address range may extend
  145. * one or more erase sectors. Return an error is there is a problem erasing.
  146. */
  147. static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
  148. {
  149. struct m25p *flash = mtd_to_m25p(mtd);
  150. u32 addr,len;
  151. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %d\n",
  152. flash->spi->dev.bus_id, __FUNCTION__, "at",
  153. (u32)instr->addr, instr->len);
  154. /* sanity checks */
  155. if (instr->addr + instr->len > flash->mtd.size)
  156. return -EINVAL;
  157. if ((instr->addr % mtd->erasesize) != 0
  158. || (instr->len % mtd->erasesize) != 0) {
  159. return -EINVAL;
  160. }
  161. addr = instr->addr;
  162. len = instr->len;
  163. down(&flash->lock);
  164. /* now erase those sectors */
  165. while (len) {
  166. if (erase_sector(flash, addr)) {
  167. instr->state = MTD_ERASE_FAILED;
  168. up(&flash->lock);
  169. return -EIO;
  170. }
  171. addr += mtd->erasesize;
  172. len -= mtd->erasesize;
  173. }
  174. up(&flash->lock);
  175. instr->state = MTD_ERASE_DONE;
  176. mtd_erase_callback(instr);
  177. return 0;
  178. }
  179. /*
  180. * Read an address range from the flash chip. The address range
  181. * may be any size provided it is within the physical boundaries.
  182. */
  183. static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
  184. size_t *retlen, u_char *buf)
  185. {
  186. struct m25p *flash = mtd_to_m25p(mtd);
  187. struct spi_transfer t[2];
  188. struct spi_message m;
  189. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
  190. flash->spi->dev.bus_id, __FUNCTION__, "from",
  191. (u32)from, len);
  192. /* sanity checks */
  193. if (!len)
  194. return 0;
  195. if (from + len > flash->mtd.size)
  196. return -EINVAL;
  197. spi_message_init(&m);
  198. memset(t, 0, (sizeof t));
  199. t[0].tx_buf = flash->command;
  200. t[0].len = sizeof(flash->command);
  201. spi_message_add_tail(&t[0], &m);
  202. t[1].rx_buf = buf;
  203. t[1].len = len;
  204. spi_message_add_tail(&t[1], &m);
  205. /* Byte count starts at zero. */
  206. if (retlen)
  207. *retlen = 0;
  208. down(&flash->lock);
  209. /* Wait till previous write/erase is done. */
  210. if (wait_till_ready(flash)) {
  211. /* REVISIT status return?? */
  212. up(&flash->lock);
  213. return 1;
  214. }
  215. /* NOTE: OPCODE_FAST_READ (if available) is faster... */
  216. /* Set up the write data buffer. */
  217. flash->command[0] = OPCODE_READ;
  218. flash->command[1] = from >> 16;
  219. flash->command[2] = from >> 8;
  220. flash->command[3] = from;
  221. spi_sync(flash->spi, &m);
  222. *retlen = m.actual_length - sizeof(flash->command);
  223. up(&flash->lock);
  224. return 0;
  225. }
  226. /*
  227. * Write an address range to the flash chip. Data must be written in
  228. * FLASH_PAGESIZE chunks. The address range may be any size provided
  229. * it is within the physical boundaries.
  230. */
  231. static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
  232. size_t *retlen, const u_char *buf)
  233. {
  234. struct m25p *flash = mtd_to_m25p(mtd);
  235. u32 page_offset, page_size;
  236. struct spi_transfer t[2];
  237. struct spi_message m;
  238. DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
  239. flash->spi->dev.bus_id, __FUNCTION__, "to",
  240. (u32)to, len);
  241. if (retlen)
  242. *retlen = 0;
  243. /* sanity checks */
  244. if (!len)
  245. return(0);
  246. if (to + len > flash->mtd.size)
  247. return -EINVAL;
  248. spi_message_init(&m);
  249. memset(t, 0, (sizeof t));
  250. t[0].tx_buf = flash->command;
  251. t[0].len = sizeof(flash->command);
  252. spi_message_add_tail(&t[0], &m);
  253. t[1].tx_buf = buf;
  254. spi_message_add_tail(&t[1], &m);
  255. down(&flash->lock);
  256. /* Wait until finished previous write command. */
  257. if (wait_till_ready(flash))
  258. return 1;
  259. write_enable(flash);
  260. /* Set up the opcode in the write buffer. */
  261. flash->command[0] = OPCODE_PP;
  262. flash->command[1] = to >> 16;
  263. flash->command[2] = to >> 8;
  264. flash->command[3] = to;
  265. /* what page do we start with? */
  266. page_offset = to % FLASH_PAGESIZE;
  267. /* do all the bytes fit onto one page? */
  268. if (page_offset + len <= FLASH_PAGESIZE) {
  269. t[1].len = len;
  270. spi_sync(flash->spi, &m);
  271. *retlen = m.actual_length - sizeof(flash->command);
  272. } else {
  273. u32 i;
  274. /* the size of data remaining on the first page */
  275. page_size = FLASH_PAGESIZE - page_offset;
  276. t[1].len = page_size;
  277. spi_sync(flash->spi, &m);
  278. *retlen = m.actual_length - sizeof(flash->command);
  279. /* write everything in PAGESIZE chunks */
  280. for (i = page_size; i < len; i += page_size) {
  281. page_size = len - i;
  282. if (page_size > FLASH_PAGESIZE)
  283. page_size = FLASH_PAGESIZE;
  284. /* write the next page to flash */
  285. flash->command[1] = (to + i) >> 16;
  286. flash->command[2] = (to + i) >> 8;
  287. flash->command[3] = (to + i);
  288. t[1].tx_buf = buf + i;
  289. t[1].len = page_size;
  290. wait_till_ready(flash);
  291. write_enable(flash);
  292. spi_sync(flash->spi, &m);
  293. if (retlen)
  294. *retlen += m.actual_length
  295. - sizeof(flash->command);
  296. }
  297. }
  298. up(&flash->lock);
  299. return 0;
  300. }
  301. /****************************************************************************/
  302. /*
  303. * SPI device driver setup and teardown
  304. */
  305. struct flash_info {
  306. char *name;
  307. u8 id;
  308. u16 jedec_id;
  309. unsigned sector_size;
  310. unsigned n_sectors;
  311. };
  312. static struct flash_info __devinitdata m25p_data [] = {
  313. /* REVISIT: fill in JEDEC ids, for parts that have them */
  314. { "m25p05", 0x05, 0x0000, 32 * 1024, 2 },
  315. { "m25p10", 0x10, 0x0000, 32 * 1024, 4 },
  316. { "m25p20", 0x11, 0x0000, 64 * 1024, 4 },
  317. { "m25p40", 0x12, 0x0000, 64 * 1024, 8 },
  318. { "m25p80", 0x13, 0x0000, 64 * 1024, 16 },
  319. { "m25p16", 0x14, 0x0000, 64 * 1024, 32 },
  320. { "m25p32", 0x15, 0x0000, 64 * 1024, 64 },
  321. { "m25p64", 0x16, 0x2017, 64 * 1024, 128 },
  322. };
  323. /*
  324. * board specific setup should have ensured the SPI clock used here
  325. * matches what the READ command supports, at least until this driver
  326. * understands FAST_READ (for clocks over 25 MHz).
  327. */
  328. static int __devinit m25p_probe(struct spi_device *spi)
  329. {
  330. struct flash_platform_data *data;
  331. struct m25p *flash;
  332. struct flash_info *info;
  333. unsigned i;
  334. /* Platform data helps sort out which chip type we have, as
  335. * well as how this board partitions it.
  336. */
  337. data = spi->dev.platform_data;
  338. if (!data || !data->type) {
  339. /* FIXME some chips can identify themselves with RES
  340. * or JEDEC get-id commands. Try them ...
  341. */
  342. DEBUG(MTD_DEBUG_LEVEL1, "%s: no chip id\n",
  343. flash->spi->dev.bus_id);
  344. return -ENODEV;
  345. }
  346. for (i = 0, info = m25p_data; i < ARRAY_SIZE(m25p_data); i++, info++) {
  347. if (strcmp(data->type, info->name) == 0)
  348. break;
  349. }
  350. if (i == ARRAY_SIZE(m25p_data)) {
  351. DEBUG(MTD_DEBUG_LEVEL1, "%s: unrecognized id %s\n",
  352. flash->spi->dev.bus_id, data->type);
  353. return -ENODEV;
  354. }
  355. flash = kzalloc(sizeof *flash, SLAB_KERNEL);
  356. if (!flash)
  357. return -ENOMEM;
  358. flash->spi = spi;
  359. init_MUTEX(&flash->lock);
  360. dev_set_drvdata(&spi->dev, flash);
  361. if (data->name)
  362. flash->mtd.name = data->name;
  363. else
  364. flash->mtd.name = spi->dev.bus_id;
  365. flash->mtd.type = MTD_NORFLASH;
  366. flash->mtd.flags = MTD_CAP_NORFLASH;
  367. flash->mtd.size = info->sector_size * info->n_sectors;
  368. flash->mtd.erasesize = info->sector_size;
  369. flash->mtd.erase = m25p80_erase;
  370. flash->mtd.read = m25p80_read;
  371. flash->mtd.write = m25p80_write;
  372. dev_info(&spi->dev, "%s (%d Kbytes)\n", info->name,
  373. flash->mtd.size / 1024);
  374. DEBUG(MTD_DEBUG_LEVEL2,
  375. "mtd .name = %s, .size = 0x%.8x (%uM) "
  376. ".erasesize = 0x%.8x (%uK) .numeraseregions = %d\n",
  377. flash->mtd.name,
  378. flash->mtd.size, flash->mtd.size / (1024*1024),
  379. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  380. flash->mtd.numeraseregions);
  381. if (flash->mtd.numeraseregions)
  382. for (i = 0; i < flash->mtd.numeraseregions; i++)
  383. DEBUG(MTD_DEBUG_LEVEL2,
  384. "mtd.eraseregions[%d] = { .offset = 0x%.8x, "
  385. ".erasesize = 0x%.8x (%uK), "
  386. ".numblocks = %d }\n",
  387. i, flash->mtd.eraseregions[i].offset,
  388. flash->mtd.eraseregions[i].erasesize,
  389. flash->mtd.eraseregions[i].erasesize / 1024,
  390. flash->mtd.eraseregions[i].numblocks);
  391. /* partitions should match sector boundaries; and it may be good to
  392. * use readonly partitions for writeprotected sectors (BP2..BP0).
  393. */
  394. if (mtd_has_partitions()) {
  395. struct mtd_partition *parts = NULL;
  396. int nr_parts = 0;
  397. #ifdef CONFIG_MTD_CMDLINE_PARTS
  398. static const char *part_probes[] = { "cmdlinepart", NULL, };
  399. nr_parts = parse_mtd_partitions(&flash->mtd,
  400. part_probes, &parts, 0);
  401. #endif
  402. if (nr_parts <= 0 && data && data->parts) {
  403. parts = data->parts;
  404. nr_parts = data->nr_parts;
  405. }
  406. if (nr_parts > 0) {
  407. for (i = 0; i < data->nr_parts; i++) {
  408. DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
  409. "{.name = %s, .offset = 0x%.8x, "
  410. ".size = 0x%.8x (%uK) }\n",
  411. i, data->parts[i].name,
  412. data->parts[i].offset,
  413. data->parts[i].size,
  414. data->parts[i].size / 1024);
  415. }
  416. flash->partitioned = 1;
  417. return add_mtd_partitions(&flash->mtd, parts, nr_parts);
  418. }
  419. } else if (data->nr_parts)
  420. dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
  421. data->nr_parts, data->name);
  422. return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
  423. }
  424. static int __devexit m25p_remove(struct spi_device *spi)
  425. {
  426. struct m25p *flash = dev_get_drvdata(&spi->dev);
  427. int status;
  428. /* Clean up MTD stuff. */
  429. if (mtd_has_partitions() && flash->partitioned)
  430. status = del_mtd_partitions(&flash->mtd);
  431. else
  432. status = del_mtd_device(&flash->mtd);
  433. if (status == 0)
  434. kfree(flash);
  435. return 0;
  436. }
  437. static struct spi_driver m25p80_driver = {
  438. .driver = {
  439. .name = "m25p80",
  440. .bus = &spi_bus_type,
  441. .owner = THIS_MODULE,
  442. },
  443. .probe = m25p_probe,
  444. .remove = __devexit_p(m25p_remove),
  445. };
  446. static int m25p80_init(void)
  447. {
  448. return spi_register_driver(&m25p80_driver);
  449. }
  450. static void m25p80_exit(void)
  451. {
  452. spi_unregister_driver(&m25p80_driver);
  453. }
  454. module_init(m25p80_init);
  455. module_exit(m25p80_exit);
  456. MODULE_LICENSE("GPL");
  457. MODULE_AUTHOR("Mike Lavender");
  458. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");