m25p80.c 33 KB

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