m25p80.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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/err.h>
  19. #include <linux/errno.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mutex.h>
  24. #include <linux/math64.h>
  25. #include <linux/slab.h>
  26. #include <linux/sched.h>
  27. #include <linux/mod_devicetable.h>
  28. #include <linux/mtd/cfi.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/spi/flash.h>
  34. /* Flash opcodes. */
  35. #define OPCODE_WREN 0x06 /* Write enable */
  36. #define OPCODE_RDSR 0x05 /* Read status register */
  37. #define OPCODE_WRSR 0x01 /* Write status register 1 byte */
  38. #define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
  39. #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
  40. #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
  41. #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
  42. #define OPCODE_BE_4K_PMC 0xd7 /* Erase 4KiB block on PMC chips */
  43. #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
  44. #define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
  45. #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
  46. #define OPCODE_RDID 0x9f /* Read JEDEC ID */
  47. /* 4-byte address opcodes - used on Spansion and some Macronix flashes. */
  48. #define OPCODE_NORM_READ_4B 0x13 /* Read data bytes (low frequency) */
  49. #define OPCODE_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */
  50. #define OPCODE_PP_4B 0x12 /* Page program (up to 256 bytes) */
  51. #define OPCODE_SE_4B 0xdc /* Sector erase (usually 64KiB) */
  52. /* Used for SST flashes only. */
  53. #define OPCODE_BP 0x02 /* Byte program */
  54. #define OPCODE_WRDI 0x04 /* Write disable */
  55. #define OPCODE_AAI_WP 0xad /* Auto address increment word program */
  56. /* Used for Macronix and Winbond flashes. */
  57. #define OPCODE_EN4B 0xb7 /* Enter 4-byte mode */
  58. #define OPCODE_EX4B 0xe9 /* Exit 4-byte mode */
  59. /* Used for Spansion flashes only. */
  60. #define OPCODE_BRWR 0x17 /* Bank register write */
  61. /* Status Register bits. */
  62. #define SR_WIP 1 /* Write in progress */
  63. #define SR_WEL 2 /* Write enable latch */
  64. /* meaning of other SR_* bits may differ between vendors */
  65. #define SR_BP0 4 /* Block protect 0 */
  66. #define SR_BP1 8 /* Block protect 1 */
  67. #define SR_BP2 0x10 /* Block protect 2 */
  68. #define SR_SRWD 0x80 /* SR write protect */
  69. /* Define max times to check status register before we give up. */
  70. #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
  71. #define MAX_CMD_SIZE 6
  72. #define JEDEC_MFR(_jedec_id) ((_jedec_id) >> 16)
  73. /****************************************************************************/
  74. struct m25p {
  75. struct spi_device *spi;
  76. struct mutex lock;
  77. struct mtd_info mtd;
  78. u16 page_size;
  79. u16 addr_width;
  80. u8 erase_opcode;
  81. u8 read_opcode;
  82. u8 program_opcode;
  83. u8 *command;
  84. bool fast_read;
  85. };
  86. static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
  87. {
  88. return container_of(mtd, struct m25p, mtd);
  89. }
  90. /****************************************************************************/
  91. /*
  92. * Internal helper functions
  93. */
  94. /*
  95. * Read the status register, returning its value in the location
  96. * Return the status register value.
  97. * Returns negative if error occurred.
  98. */
  99. static int read_sr(struct m25p *flash)
  100. {
  101. ssize_t retval;
  102. u8 code = OPCODE_RDSR;
  103. u8 val;
  104. retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
  105. if (retval < 0) {
  106. dev_err(&flash->spi->dev, "error %d reading SR\n",
  107. (int) retval);
  108. return retval;
  109. }
  110. return val;
  111. }
  112. /*
  113. * Write status register 1 byte
  114. * Returns negative if error occurred.
  115. */
  116. static int write_sr(struct m25p *flash, u8 val)
  117. {
  118. flash->command[0] = OPCODE_WRSR;
  119. flash->command[1] = val;
  120. return spi_write(flash->spi, flash->command, 2);
  121. }
  122. /*
  123. * Set write enable latch with Write Enable command.
  124. * Returns negative if error occurred.
  125. */
  126. static inline int write_enable(struct m25p *flash)
  127. {
  128. u8 code = OPCODE_WREN;
  129. return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
  130. }
  131. /*
  132. * Send write disble instruction to the chip.
  133. */
  134. static inline int write_disable(struct m25p *flash)
  135. {
  136. u8 code = OPCODE_WRDI;
  137. return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
  138. }
  139. /*
  140. * Enable/disable 4-byte addressing mode.
  141. */
  142. static inline int set_4byte(struct m25p *flash, u32 jedec_id, int enable)
  143. {
  144. int status;
  145. bool need_wren = false;
  146. switch (JEDEC_MFR(jedec_id)) {
  147. case CFI_MFR_ST: /* Micron, actually */
  148. /* Some Micron need WREN command; all will accept it */
  149. need_wren = true;
  150. case CFI_MFR_MACRONIX:
  151. case 0xEF /* winbond */:
  152. if (need_wren)
  153. write_enable(flash);
  154. flash->command[0] = enable ? OPCODE_EN4B : OPCODE_EX4B;
  155. status = spi_write(flash->spi, flash->command, 1);
  156. if (need_wren)
  157. write_disable(flash);
  158. return status;
  159. default:
  160. /* Spansion style */
  161. flash->command[0] = OPCODE_BRWR;
  162. flash->command[1] = enable << 7;
  163. return spi_write(flash->spi, flash->command, 2);
  164. }
  165. }
  166. /*
  167. * Service routine to read status register until ready, or timeout occurs.
  168. * Returns non-zero if error.
  169. */
  170. static int wait_till_ready(struct m25p *flash)
  171. {
  172. unsigned long deadline;
  173. int sr;
  174. deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  175. do {
  176. if ((sr = read_sr(flash)) < 0)
  177. break;
  178. else if (!(sr & SR_WIP))
  179. return 0;
  180. cond_resched();
  181. } while (!time_after_eq(jiffies, deadline));
  182. return 1;
  183. }
  184. /*
  185. * Erase the whole flash memory
  186. *
  187. * Returns 0 if successful, non-zero otherwise.
  188. */
  189. static int erase_chip(struct m25p *flash)
  190. {
  191. pr_debug("%s: %s %lldKiB\n", dev_name(&flash->spi->dev), __func__,
  192. (long long)(flash->mtd.size >> 10));
  193. /* Wait until finished previous write command. */
  194. if (wait_till_ready(flash))
  195. return 1;
  196. /* Send write enable, then erase commands. */
  197. write_enable(flash);
  198. /* Set up command buffer. */
  199. flash->command[0] = OPCODE_CHIP_ERASE;
  200. spi_write(flash->spi, flash->command, 1);
  201. return 0;
  202. }
  203. static void m25p_addr2cmd(struct m25p *flash, unsigned int addr, u8 *cmd)
  204. {
  205. /* opcode is in cmd[0] */
  206. cmd[1] = addr >> (flash->addr_width * 8 - 8);
  207. cmd[2] = addr >> (flash->addr_width * 8 - 16);
  208. cmd[3] = addr >> (flash->addr_width * 8 - 24);
  209. cmd[4] = addr >> (flash->addr_width * 8 - 32);
  210. }
  211. static int m25p_cmdsz(struct m25p *flash)
  212. {
  213. return 1 + flash->addr_width;
  214. }
  215. /*
  216. * Erase one sector of flash memory at offset ``offset'' which is any
  217. * address within the sector which should be erased.
  218. *
  219. * Returns 0 if successful, non-zero otherwise.
  220. */
  221. static int erase_sector(struct m25p *flash, u32 offset)
  222. {
  223. pr_debug("%s: %s %dKiB at 0x%08x\n", dev_name(&flash->spi->dev),
  224. __func__, flash->mtd.erasesize / 1024, offset);
  225. /* Wait until finished previous write command. */
  226. if (wait_till_ready(flash))
  227. return 1;
  228. /* Send write enable, then erase commands. */
  229. write_enable(flash);
  230. /* Set up command buffer. */
  231. flash->command[0] = flash->erase_opcode;
  232. m25p_addr2cmd(flash, offset, flash->command);
  233. spi_write(flash->spi, flash->command, m25p_cmdsz(flash));
  234. return 0;
  235. }
  236. /****************************************************************************/
  237. /*
  238. * MTD implementation
  239. */
  240. /*
  241. * Erase an address range on the flash chip. The address range may extend
  242. * one or more erase sectors. Return an error is there is a problem erasing.
  243. */
  244. static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
  245. {
  246. struct m25p *flash = mtd_to_m25p(mtd);
  247. u32 addr,len;
  248. uint32_t rem;
  249. pr_debug("%s: %s at 0x%llx, len %lld\n", dev_name(&flash->spi->dev),
  250. __func__, (long long)instr->addr,
  251. (long long)instr->len);
  252. div_u64_rem(instr->len, mtd->erasesize, &rem);
  253. if (rem)
  254. return -EINVAL;
  255. addr = instr->addr;
  256. len = instr->len;
  257. mutex_lock(&flash->lock);
  258. /* whole-chip erase? */
  259. if (len == flash->mtd.size) {
  260. if (erase_chip(flash)) {
  261. instr->state = MTD_ERASE_FAILED;
  262. mutex_unlock(&flash->lock);
  263. return -EIO;
  264. }
  265. /* REVISIT in some cases we could speed up erasing large regions
  266. * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
  267. * to use "small sector erase", but that's not always optimal.
  268. */
  269. /* "sector"-at-a-time erase */
  270. } else {
  271. while (len) {
  272. if (erase_sector(flash, addr)) {
  273. instr->state = MTD_ERASE_FAILED;
  274. mutex_unlock(&flash->lock);
  275. return -EIO;
  276. }
  277. addr += mtd->erasesize;
  278. len -= mtd->erasesize;
  279. }
  280. }
  281. mutex_unlock(&flash->lock);
  282. instr->state = MTD_ERASE_DONE;
  283. mtd_erase_callback(instr);
  284. return 0;
  285. }
  286. /*
  287. * Read an address range from the flash chip. The address range
  288. * may be any size provided it is within the physical boundaries.
  289. */
  290. static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
  291. size_t *retlen, u_char *buf)
  292. {
  293. struct m25p *flash = mtd_to_m25p(mtd);
  294. struct spi_transfer t[2];
  295. struct spi_message m;
  296. uint8_t opcode;
  297. pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  298. __func__, (u32)from, len);
  299. spi_message_init(&m);
  300. memset(t, 0, (sizeof t));
  301. t[0].tx_buf = flash->command;
  302. t[0].len = m25p_cmdsz(flash) + (flash->fast_read ? 1 : 0);
  303. spi_message_add_tail(&t[0], &m);
  304. t[1].rx_buf = buf;
  305. t[1].len = len;
  306. spi_message_add_tail(&t[1], &m);
  307. mutex_lock(&flash->lock);
  308. /* Wait till previous write/erase is done. */
  309. if (wait_till_ready(flash)) {
  310. /* REVISIT status return?? */
  311. mutex_unlock(&flash->lock);
  312. return 1;
  313. }
  314. /* Set up the write data buffer. */
  315. opcode = flash->read_opcode;
  316. flash->command[0] = opcode;
  317. m25p_addr2cmd(flash, from, flash->command);
  318. spi_sync(flash->spi, &m);
  319. *retlen = m.actual_length - m25p_cmdsz(flash) -
  320. (flash->fast_read ? 1 : 0);
  321. mutex_unlock(&flash->lock);
  322. return 0;
  323. }
  324. /*
  325. * Write an address range to the flash chip. Data must be written in
  326. * FLASH_PAGESIZE chunks. The address range may be any size provided
  327. * it is within the physical boundaries.
  328. */
  329. static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
  330. size_t *retlen, const u_char *buf)
  331. {
  332. struct m25p *flash = mtd_to_m25p(mtd);
  333. u32 page_offset, page_size;
  334. struct spi_transfer t[2];
  335. struct spi_message m;
  336. pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  337. __func__, (u32)to, len);
  338. spi_message_init(&m);
  339. memset(t, 0, (sizeof t));
  340. t[0].tx_buf = flash->command;
  341. t[0].len = m25p_cmdsz(flash);
  342. spi_message_add_tail(&t[0], &m);
  343. t[1].tx_buf = buf;
  344. spi_message_add_tail(&t[1], &m);
  345. mutex_lock(&flash->lock);
  346. /* Wait until finished previous write command. */
  347. if (wait_till_ready(flash)) {
  348. mutex_unlock(&flash->lock);
  349. return 1;
  350. }
  351. write_enable(flash);
  352. /* Set up the opcode in the write buffer. */
  353. flash->command[0] = flash->program_opcode;
  354. m25p_addr2cmd(flash, to, flash->command);
  355. page_offset = to & (flash->page_size - 1);
  356. /* do all the bytes fit onto one page? */
  357. if (page_offset + len <= flash->page_size) {
  358. t[1].len = len;
  359. spi_sync(flash->spi, &m);
  360. *retlen = m.actual_length - m25p_cmdsz(flash);
  361. } else {
  362. u32 i;
  363. /* the size of data remaining on the first page */
  364. page_size = flash->page_size - page_offset;
  365. t[1].len = page_size;
  366. spi_sync(flash->spi, &m);
  367. *retlen = m.actual_length - m25p_cmdsz(flash);
  368. /* write everything in flash->page_size chunks */
  369. for (i = page_size; i < len; i += page_size) {
  370. page_size = len - i;
  371. if (page_size > flash->page_size)
  372. page_size = flash->page_size;
  373. /* write the next page to flash */
  374. m25p_addr2cmd(flash, to + i, flash->command);
  375. t[1].tx_buf = buf + i;
  376. t[1].len = page_size;
  377. wait_till_ready(flash);
  378. write_enable(flash);
  379. spi_sync(flash->spi, &m);
  380. *retlen += m.actual_length - m25p_cmdsz(flash);
  381. }
  382. }
  383. mutex_unlock(&flash->lock);
  384. return 0;
  385. }
  386. static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  387. size_t *retlen, const u_char *buf)
  388. {
  389. struct m25p *flash = mtd_to_m25p(mtd);
  390. struct spi_transfer t[2];
  391. struct spi_message m;
  392. size_t actual;
  393. int cmd_sz, ret;
  394. pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
  395. __func__, (u32)to, len);
  396. spi_message_init(&m);
  397. memset(t, 0, (sizeof t));
  398. t[0].tx_buf = flash->command;
  399. t[0].len = m25p_cmdsz(flash);
  400. spi_message_add_tail(&t[0], &m);
  401. t[1].tx_buf = buf;
  402. spi_message_add_tail(&t[1], &m);
  403. mutex_lock(&flash->lock);
  404. /* Wait until finished previous write command. */
  405. ret = wait_till_ready(flash);
  406. if (ret)
  407. goto time_out;
  408. write_enable(flash);
  409. actual = to % 2;
  410. /* Start write from odd address. */
  411. if (actual) {
  412. flash->command[0] = OPCODE_BP;
  413. m25p_addr2cmd(flash, to, flash->command);
  414. /* write one byte. */
  415. t[1].len = 1;
  416. spi_sync(flash->spi, &m);
  417. ret = wait_till_ready(flash);
  418. if (ret)
  419. goto time_out;
  420. *retlen += m.actual_length - m25p_cmdsz(flash);
  421. }
  422. to += actual;
  423. flash->command[0] = OPCODE_AAI_WP;
  424. m25p_addr2cmd(flash, to, flash->command);
  425. /* Write out most of the data here. */
  426. cmd_sz = m25p_cmdsz(flash);
  427. for (; actual < len - 1; actual += 2) {
  428. t[0].len = cmd_sz;
  429. /* write two bytes. */
  430. t[1].len = 2;
  431. t[1].tx_buf = buf + actual;
  432. spi_sync(flash->spi, &m);
  433. ret = wait_till_ready(flash);
  434. if (ret)
  435. goto time_out;
  436. *retlen += m.actual_length - cmd_sz;
  437. cmd_sz = 1;
  438. to += 2;
  439. }
  440. write_disable(flash);
  441. ret = wait_till_ready(flash);
  442. if (ret)
  443. goto time_out;
  444. /* Write out trailing byte if it exists. */
  445. if (actual != len) {
  446. write_enable(flash);
  447. flash->command[0] = OPCODE_BP;
  448. m25p_addr2cmd(flash, to, flash->command);
  449. t[0].len = m25p_cmdsz(flash);
  450. t[1].len = 1;
  451. t[1].tx_buf = buf + actual;
  452. spi_sync(flash->spi, &m);
  453. ret = wait_till_ready(flash);
  454. if (ret)
  455. goto time_out;
  456. *retlen += m.actual_length - m25p_cmdsz(flash);
  457. write_disable(flash);
  458. }
  459. time_out:
  460. mutex_unlock(&flash->lock);
  461. return ret;
  462. }
  463. static int m25p80_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  464. {
  465. struct m25p *flash = mtd_to_m25p(mtd);
  466. uint32_t offset = ofs;
  467. uint8_t status_old, status_new;
  468. int res = 0;
  469. mutex_lock(&flash->lock);
  470. /* Wait until finished previous command */
  471. if (wait_till_ready(flash)) {
  472. res = 1;
  473. goto err;
  474. }
  475. status_old = read_sr(flash);
  476. if (offset < flash->mtd.size-(flash->mtd.size/2))
  477. status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
  478. else if (offset < flash->mtd.size-(flash->mtd.size/4))
  479. status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  480. else if (offset < flash->mtd.size-(flash->mtd.size/8))
  481. status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  482. else if (offset < flash->mtd.size-(flash->mtd.size/16))
  483. status_new = (status_old & ~(SR_BP0|SR_BP1)) | SR_BP2;
  484. else if (offset < flash->mtd.size-(flash->mtd.size/32))
  485. status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  486. else if (offset < flash->mtd.size-(flash->mtd.size/64))
  487. status_new = (status_old & ~(SR_BP2|SR_BP0)) | SR_BP1;
  488. else
  489. status_new = (status_old & ~(SR_BP2|SR_BP1)) | SR_BP0;
  490. /* Only modify protection if it will not unlock other areas */
  491. if ((status_new&(SR_BP2|SR_BP1|SR_BP0)) >
  492. (status_old&(SR_BP2|SR_BP1|SR_BP0))) {
  493. write_enable(flash);
  494. if (write_sr(flash, status_new) < 0) {
  495. res = 1;
  496. goto err;
  497. }
  498. }
  499. err: mutex_unlock(&flash->lock);
  500. return res;
  501. }
  502. static int m25p80_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  503. {
  504. struct m25p *flash = mtd_to_m25p(mtd);
  505. uint32_t offset = ofs;
  506. uint8_t status_old, status_new;
  507. int res = 0;
  508. mutex_lock(&flash->lock);
  509. /* Wait until finished previous command */
  510. if (wait_till_ready(flash)) {
  511. res = 1;
  512. goto err;
  513. }
  514. status_old = read_sr(flash);
  515. if (offset+len > flash->mtd.size-(flash->mtd.size/64))
  516. status_new = status_old & ~(SR_BP2|SR_BP1|SR_BP0);
  517. else if (offset+len > flash->mtd.size-(flash->mtd.size/32))
  518. status_new = (status_old & ~(SR_BP2|SR_BP1)) | SR_BP0;
  519. else if (offset+len > flash->mtd.size-(flash->mtd.size/16))
  520. status_new = (status_old & ~(SR_BP2|SR_BP0)) | SR_BP1;
  521. else if (offset+len > flash->mtd.size-(flash->mtd.size/8))
  522. status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  523. else if (offset+len > flash->mtd.size-(flash->mtd.size/4))
  524. status_new = (status_old & ~(SR_BP0|SR_BP1)) | SR_BP2;
  525. else if (offset+len > flash->mtd.size-(flash->mtd.size/2))
  526. status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  527. else
  528. status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  529. /* Only modify protection if it will not lock other areas */
  530. if ((status_new&(SR_BP2|SR_BP1|SR_BP0)) <
  531. (status_old&(SR_BP2|SR_BP1|SR_BP0))) {
  532. write_enable(flash);
  533. if (write_sr(flash, status_new) < 0) {
  534. res = 1;
  535. goto err;
  536. }
  537. }
  538. err: mutex_unlock(&flash->lock);
  539. return res;
  540. }
  541. /****************************************************************************/
  542. /*
  543. * SPI device driver setup and teardown
  544. */
  545. struct flash_info {
  546. /* JEDEC id zero means "no ID" (most older chips); otherwise it has
  547. * a high byte of zero plus three data bytes: the manufacturer id,
  548. * then a two byte device id.
  549. */
  550. u32 jedec_id;
  551. u16 ext_id;
  552. /* The size listed here is what works with OPCODE_SE, which isn't
  553. * necessarily called a "sector" by the vendor.
  554. */
  555. unsigned sector_size;
  556. u16 n_sectors;
  557. u16 page_size;
  558. u16 addr_width;
  559. u16 flags;
  560. #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
  561. #define M25P_NO_ERASE 0x02 /* No erase command needed */
  562. #define SST_WRITE 0x04 /* use SST byte programming */
  563. #define M25P_NO_FR 0x08 /* Can't do fastread */
  564. #define SECT_4K_PMC 0x10 /* OPCODE_BE_4K_PMC works uniformly */
  565. };
  566. #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  567. ((kernel_ulong_t)&(struct flash_info) { \
  568. .jedec_id = (_jedec_id), \
  569. .ext_id = (_ext_id), \
  570. .sector_size = (_sector_size), \
  571. .n_sectors = (_n_sectors), \
  572. .page_size = 256, \
  573. .flags = (_flags), \
  574. })
  575. #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
  576. ((kernel_ulong_t)&(struct flash_info) { \
  577. .sector_size = (_sector_size), \
  578. .n_sectors = (_n_sectors), \
  579. .page_size = (_page_size), \
  580. .addr_width = (_addr_width), \
  581. .flags = (_flags), \
  582. })
  583. /* NOTE: double check command sets and memory organization when you add
  584. * more flash chips. This current list focusses on newer chips, which
  585. * have been converging on command sets which including JEDEC ID.
  586. */
  587. static const struct spi_device_id m25p_ids[] = {
  588. /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  589. { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  590. { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  591. { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  592. { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  593. { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  594. { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  595. { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  596. { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  597. { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  598. { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  599. /* EON -- en25xxx */
  600. { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  601. { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  602. { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  603. { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  604. { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  605. { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  606. /* ESMT */
  607. { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
  608. /* Everspin */
  609. { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, M25P_NO_ERASE | M25P_NO_FR) },
  610. { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, M25P_NO_ERASE | M25P_NO_FR) },
  611. /* GigaDevice */
  612. { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
  613. { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
  614. /* Intel/Numonyx -- xxxs33b */
  615. { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  616. { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  617. { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  618. /* Macronix */
  619. { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  620. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  621. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  622. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  623. { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
  624. { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
  625. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  626. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  627. { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
  628. { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  629. { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, 0) },
  630. /* Micron */
  631. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, 0) },
  632. { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, 0) },
  633. { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, 0) },
  634. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K) },
  635. { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K) },
  636. /* PMC */
  637. { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
  638. { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
  639. { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
  640. /* Spansion -- single (large) sector size only, at least
  641. * for the chips listed here (without boot sectors).
  642. */
  643. { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, 0) },
  644. { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
  645. { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  646. { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, 0) },
  647. { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, 0) },
  648. { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  649. { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  650. { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  651. { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
  652. { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
  653. { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  654. { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  655. { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  656. { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  657. { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  658. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
  659. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  660. /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  661. { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  662. { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  663. { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
  664. { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
  665. { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
  666. { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  667. { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  668. { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  669. { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  670. /* ST Microelectronics -- newer production may have feature updates */
  671. { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  672. { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  673. { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  674. { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  675. { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  676. { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  677. { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  678. { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  679. { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  680. { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, 0) },
  681. { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  682. { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  683. { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  684. { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  685. { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  686. { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  687. { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  688. { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  689. { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  690. { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  691. { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  692. { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  693. { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  694. { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  695. { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  696. { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  697. { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  698. { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  699. { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  700. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  701. { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  702. { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  703. { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  704. { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  705. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  706. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  707. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  708. { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
  709. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  710. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  711. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  712. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  713. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  714. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  715. { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
  716. /* Catalyst / On Semiconductor -- non-JEDEC */
  717. { "cat25c11", CAT25_INFO( 16, 8, 16, 1, M25P_NO_ERASE | M25P_NO_FR) },
  718. { "cat25c03", CAT25_INFO( 32, 8, 16, 2, M25P_NO_ERASE | M25P_NO_FR) },
  719. { "cat25c09", CAT25_INFO( 128, 8, 32, 2, M25P_NO_ERASE | M25P_NO_FR) },
  720. { "cat25c17", CAT25_INFO( 256, 8, 32, 2, M25P_NO_ERASE | M25P_NO_FR) },
  721. { "cat25128", CAT25_INFO(2048, 8, 64, 2, M25P_NO_ERASE | M25P_NO_FR) },
  722. { },
  723. };
  724. MODULE_DEVICE_TABLE(spi, m25p_ids);
  725. static const struct spi_device_id *jedec_probe(struct spi_device *spi)
  726. {
  727. int tmp;
  728. u8 code = OPCODE_RDID;
  729. u8 id[5];
  730. u32 jedec;
  731. u16 ext_jedec;
  732. struct flash_info *info;
  733. /* JEDEC also defines an optional "extended device information"
  734. * string for after vendor-specific data, after the three bytes
  735. * we use here. Supporting some chips might require using it.
  736. */
  737. tmp = spi_write_then_read(spi, &code, 1, id, 5);
  738. if (tmp < 0) {
  739. pr_debug("%s: error %d reading JEDEC ID\n",
  740. dev_name(&spi->dev), tmp);
  741. return ERR_PTR(tmp);
  742. }
  743. jedec = id[0];
  744. jedec = jedec << 8;
  745. jedec |= id[1];
  746. jedec = jedec << 8;
  747. jedec |= id[2];
  748. ext_jedec = id[3] << 8 | id[4];
  749. for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
  750. info = (void *)m25p_ids[tmp].driver_data;
  751. if (info->jedec_id == jedec) {
  752. if (info->ext_id != 0 && info->ext_id != ext_jedec)
  753. continue;
  754. return &m25p_ids[tmp];
  755. }
  756. }
  757. dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
  758. return ERR_PTR(-ENODEV);
  759. }
  760. /*
  761. * board specific setup should have ensured the SPI clock used here
  762. * matches what the READ command supports, at least until this driver
  763. * understands FAST_READ (for clocks over 25 MHz).
  764. */
  765. static int m25p_probe(struct spi_device *spi)
  766. {
  767. const struct spi_device_id *id = spi_get_device_id(spi);
  768. struct flash_platform_data *data;
  769. struct m25p *flash;
  770. struct flash_info *info;
  771. unsigned i;
  772. struct mtd_part_parser_data ppdata;
  773. struct device_node __maybe_unused *np = spi->dev.of_node;
  774. #ifdef CONFIG_MTD_OF_PARTS
  775. if (!of_device_is_available(np))
  776. return -ENODEV;
  777. #endif
  778. /* Platform data helps sort out which chip type we have, as
  779. * well as how this board partitions it. If we don't have
  780. * a chip ID, try the JEDEC id commands; they'll work for most
  781. * newer chips, even if we don't recognize the particular chip.
  782. */
  783. data = dev_get_platdata(&spi->dev);
  784. if (data && data->type) {
  785. const struct spi_device_id *plat_id;
  786. for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
  787. plat_id = &m25p_ids[i];
  788. if (strcmp(data->type, plat_id->name))
  789. continue;
  790. break;
  791. }
  792. if (i < ARRAY_SIZE(m25p_ids) - 1)
  793. id = plat_id;
  794. else
  795. dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
  796. }
  797. info = (void *)id->driver_data;
  798. if (info->jedec_id) {
  799. const struct spi_device_id *jid;
  800. jid = jedec_probe(spi);
  801. if (IS_ERR(jid)) {
  802. return PTR_ERR(jid);
  803. } else if (jid != id) {
  804. /*
  805. * JEDEC knows better, so overwrite platform ID. We
  806. * can't trust partitions any longer, but we'll let
  807. * mtd apply them anyway, since some partitions may be
  808. * marked read-only, and we don't want to lose that
  809. * information, even if it's not 100% accurate.
  810. */
  811. dev_warn(&spi->dev, "found %s, expected %s\n",
  812. jid->name, id->name);
  813. id = jid;
  814. info = (void *)jid->driver_data;
  815. }
  816. }
  817. flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
  818. if (!flash)
  819. return -ENOMEM;
  820. flash->command = devm_kzalloc(&spi->dev, MAX_CMD_SIZE, GFP_KERNEL);
  821. if (!flash->command)
  822. return -ENOMEM;
  823. flash->spi = spi;
  824. mutex_init(&flash->lock);
  825. spi_set_drvdata(spi, flash);
  826. /*
  827. * Atmel, SST and Intel/Numonyx serial flash tend to power
  828. * up with the software protection bits set
  829. */
  830. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ATMEL ||
  831. JEDEC_MFR(info->jedec_id) == CFI_MFR_INTEL ||
  832. JEDEC_MFR(info->jedec_id) == CFI_MFR_SST) {
  833. write_enable(flash);
  834. write_sr(flash, 0);
  835. }
  836. if (data && data->name)
  837. flash->mtd.name = data->name;
  838. else
  839. flash->mtd.name = dev_name(&spi->dev);
  840. flash->mtd.type = MTD_NORFLASH;
  841. flash->mtd.writesize = 1;
  842. flash->mtd.flags = MTD_CAP_NORFLASH;
  843. flash->mtd.size = info->sector_size * info->n_sectors;
  844. flash->mtd._erase = m25p80_erase;
  845. flash->mtd._read = m25p80_read;
  846. /* flash protection support for STmicro chips */
  847. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ST) {
  848. flash->mtd._lock = m25p80_lock;
  849. flash->mtd._unlock = m25p80_unlock;
  850. }
  851. /* sst flash chips use AAI word program */
  852. if (info->flags & SST_WRITE)
  853. flash->mtd._write = sst_write;
  854. else
  855. flash->mtd._write = m25p80_write;
  856. /* prefer "small sector" erase if possible */
  857. if (info->flags & SECT_4K) {
  858. flash->erase_opcode = OPCODE_BE_4K;
  859. flash->mtd.erasesize = 4096;
  860. } else if (info->flags & SECT_4K_PMC) {
  861. flash->erase_opcode = OPCODE_BE_4K_PMC;
  862. flash->mtd.erasesize = 4096;
  863. } else {
  864. flash->erase_opcode = OPCODE_SE;
  865. flash->mtd.erasesize = info->sector_size;
  866. }
  867. if (info->flags & M25P_NO_ERASE)
  868. flash->mtd.flags |= MTD_NO_ERASE;
  869. ppdata.of_node = spi->dev.of_node;
  870. flash->mtd.dev.parent = &spi->dev;
  871. flash->page_size = info->page_size;
  872. flash->mtd.writebufsize = flash->page_size;
  873. flash->fast_read = false;
  874. if (np && of_property_read_bool(np, "m25p,fast-read"))
  875. flash->fast_read = true;
  876. #ifdef CONFIG_M25PXX_USE_FAST_READ
  877. flash->fast_read = true;
  878. #endif
  879. if (info->flags & M25P_NO_FR)
  880. flash->fast_read = false;
  881. /* Default commands */
  882. if (flash->fast_read)
  883. flash->read_opcode = OPCODE_FAST_READ;
  884. else
  885. flash->read_opcode = OPCODE_NORM_READ;
  886. flash->program_opcode = OPCODE_PP;
  887. if (info->addr_width)
  888. flash->addr_width = info->addr_width;
  889. else if (flash->mtd.size > 0x1000000) {
  890. /* enable 4-byte addressing if the device exceeds 16MiB */
  891. flash->addr_width = 4;
  892. if (JEDEC_MFR(info->jedec_id) == CFI_MFR_AMD) {
  893. /* Dedicated 4-byte command set */
  894. flash->read_opcode = flash->fast_read ?
  895. OPCODE_FAST_READ_4B :
  896. OPCODE_NORM_READ_4B;
  897. flash->program_opcode = OPCODE_PP_4B;
  898. /* No small sector erase for 4-byte command set */
  899. flash->erase_opcode = OPCODE_SE_4B;
  900. flash->mtd.erasesize = info->sector_size;
  901. } else
  902. set_4byte(flash, info->jedec_id, 1);
  903. } else {
  904. flash->addr_width = 3;
  905. }
  906. dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
  907. (long long)flash->mtd.size >> 10);
  908. pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
  909. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  910. flash->mtd.name,
  911. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  912. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  913. flash->mtd.numeraseregions);
  914. if (flash->mtd.numeraseregions)
  915. for (i = 0; i < flash->mtd.numeraseregions; i++)
  916. pr_debug("mtd.eraseregions[%d] = { .offset = 0x%llx, "
  917. ".erasesize = 0x%.8x (%uKiB), "
  918. ".numblocks = %d }\n",
  919. i, (long long)flash->mtd.eraseregions[i].offset,
  920. flash->mtd.eraseregions[i].erasesize,
  921. flash->mtd.eraseregions[i].erasesize / 1024,
  922. flash->mtd.eraseregions[i].numblocks);
  923. /* partitions should match sector boundaries; and it may be good to
  924. * use readonly partitions for writeprotected sectors (BP2..BP0).
  925. */
  926. return mtd_device_parse_register(&flash->mtd, NULL, &ppdata,
  927. data ? data->parts : NULL,
  928. data ? data->nr_parts : 0);
  929. }
  930. static int m25p_remove(struct spi_device *spi)
  931. {
  932. struct m25p *flash = spi_get_drvdata(spi);
  933. /* Clean up MTD stuff. */
  934. mtd_device_unregister(&flash->mtd);
  935. return 0;
  936. }
  937. static struct spi_driver m25p80_driver = {
  938. .driver = {
  939. .name = "m25p80",
  940. .owner = THIS_MODULE,
  941. },
  942. .id_table = m25p_ids,
  943. .probe = m25p_probe,
  944. .remove = m25p_remove,
  945. /* REVISIT: many of these chips have deep power-down modes, which
  946. * should clearly be entered on suspend() to minimize power use.
  947. * And also when they're otherwise idle...
  948. */
  949. };
  950. module_spi_driver(m25p80_driver);
  951. MODULE_LICENSE("GPL");
  952. MODULE_AUTHOR("Mike Lavender");
  953. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");