m25p80.c 32 KB

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