m25p80.c 38 KB

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