413-mtd-Introduce-SPI-NAND-framework.patch 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. From 082a89a78e29b15008284df90441747cb742f149 Mon Sep 17 00:00:00 2001
  2. From: Ezequiel Garcia <[email protected]>
  3. Date: Tue, 2 Dec 2014 09:58:52 -0300
  4. Subject: mtd: Introduce SPI NAND framework
  5. Add a new framework, to support SPI NAND devices. The framework registers
  6. a NAND chip and handles the generic SPI NAND protocol, calling device-specific
  7. hooks for each SPI NAND command.
  8. The following is the stack design, from userspace to hardware. This commit
  9. adds the "SPI NAND core" layer.
  10. Userspace
  11. ------------------
  12. MTD
  13. ------------------
  14. NAND core
  15. ------------------
  16. SPI NAND core
  17. ------------------
  18. SPI NAND device
  19. ------------------
  20. SPI core
  21. ------------------
  22. SPI master
  23. ------------------
  24. Hardware
  25. (based on http://lists.infradead.org/pipermail/linux-mtd/2014-December/056763.html)
  26. Signed-off-by: Ionela Voinescu <[email protected]>
  27. Signed-off-by: Ezequiel Garcia <[email protected]>
  28. Signed-off-by: Ian Pozella <[email protected]>
  29. ---
  30. drivers/mtd/Kconfig | 2 +
  31. drivers/mtd/Makefile | 1 +
  32. drivers/mtd/spi-nand/Kconfig | 7 +
  33. drivers/mtd/spi-nand/Makefile | 1 +
  34. drivers/mtd/spi-nand/spi-nand-base.c | 566 +++++++++++++++++++++++++++++++++++
  35. include/linux/mtd/spi-nand.h | 54 ++++
  36. 6 files changed, 631 insertions(+)
  37. create mode 100644 drivers/mtd/spi-nand/Kconfig
  38. create mode 100644 drivers/mtd/spi-nand/Makefile
  39. create mode 100644 drivers/mtd/spi-nand/spi-nand-base.c
  40. create mode 100644 include/linux/mtd/spi-nand.h
  41. --- a/drivers/mtd/Kconfig
  42. +++ b/drivers/mtd/Kconfig
  43. @@ -373,6 +373,8 @@ source "drivers/mtd/onenand/Kconfig"
  44. source "drivers/mtd/lpddr/Kconfig"
  45. +source "drivers/mtd/spi-nand/Kconfig"
  46. +
  47. source "drivers/mtd/spi-nor/Kconfig"
  48. source "drivers/mtd/ubi/Kconfig"
  49. --- a/drivers/mtd/Makefile
  50. +++ b/drivers/mtd/Makefile
  51. @@ -37,5 +37,6 @@ inftl-objs := inftlcore.o inftlmount.o
  52. obj-y += chips/ lpddr/ maps/ devices/ nand/ onenand/ tests/
  53. +obj-$(CONFIG_MTD_SPI_NAND) += spi-nand/
  54. obj-$(CONFIG_MTD_SPI_NOR) += spi-nor/
  55. obj-$(CONFIG_MTD_UBI) += ubi/
  56. --- /dev/null
  57. +++ b/drivers/mtd/spi-nand/Kconfig
  58. @@ -0,0 +1,7 @@
  59. +menuconfig MTD_SPI_NAND
  60. + tristate "SPI NAND device support"
  61. + depends on MTD
  62. + select MTD_NAND
  63. + help
  64. + This is the framework for the SPI NAND.
  65. +
  66. --- /dev/null
  67. +++ b/drivers/mtd/spi-nand/Makefile
  68. @@ -0,0 +1 @@
  69. +obj-$(CONFIG_MTD_SPI_NAND) += spi-nand-base.o
  70. --- /dev/null
  71. +++ b/drivers/mtd/spi-nand/spi-nand-base.c
  72. @@ -0,0 +1,566 @@
  73. +/*
  74. + * Copyright (C) 2014 Imagination Technologies Ltd.
  75. + *
  76. + * This program is free software; you can redistribute it and/or modify
  77. + * it under the terms of the GNU General Public License as published by
  78. + * the Free Software Foundation; version 2 of the License.
  79. + *
  80. + * Notes:
  81. + * 1. Erase and program operations need to call write_enable() first,
  82. + * to clear the enable bit. This bit is cleared automatically after
  83. + * the erase or program operation.
  84. + *
  85. + */
  86. +
  87. +#include <linux/device.h>
  88. +#include <linux/err.h>
  89. +#include <linux/errno.h>
  90. +#include <linux/kernel.h>
  91. +#include <linux/module.h>
  92. +#include <linux/mtd/rawnand.h>
  93. +#include <linux/mtd/mtd.h>
  94. +#include <linux/mtd/partitions.h>
  95. +#include <linux/mtd/spi-nand.h>
  96. +#include <linux/of.h>
  97. +#include <linux/slab.h>
  98. +
  99. +/* Registers common to all devices */
  100. +#define SPI_NAND_LOCK_REG 0xa0
  101. +#define SPI_NAND_PROT_UNLOCK_ALL 0x0
  102. +
  103. +#define SPI_NAND_FEATURE_REG 0xb0
  104. +#define SPI_NAND_ECC_EN BIT(4)
  105. +#define SPI_NAND_QUAD_EN BIT(0)
  106. +
  107. +#define SPI_NAND_STATUS_REG 0xc0
  108. +#define SPI_NAND_STATUS_REG_ECC_MASK 0x3
  109. +#define SPI_NAND_STATUS_REG_ECC_SHIFT 4
  110. +#define SPI_NAND_STATUS_REG_PROG_FAIL BIT(3)
  111. +#define SPI_NAND_STATUS_REG_ERASE_FAIL BIT(2)
  112. +#define SPI_NAND_STATUS_REG_WREN BIT(1)
  113. +#define SPI_NAND_STATUS_REG_BUSY BIT(0)
  114. +
  115. +#define SPI_NAND_CMD_BUF_LEN 8
  116. +
  117. +/* Rewind and fill the buffer with 0xff */
  118. +static void spi_nand_clear_buffer(struct spi_nand *snand)
  119. +{
  120. + snand->buf_start = 0;
  121. + memset(snand->data_buf, 0xff, snand->buf_size);
  122. +}
  123. +
  124. +static int spi_nand_enable_ecc(struct spi_nand *snand)
  125. +{
  126. + int ret;
  127. +
  128. + ret = snand->read_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
  129. + if (ret)
  130. + return ret;
  131. +
  132. + snand->buf[0] |= SPI_NAND_ECC_EN;
  133. + ret = snand->write_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
  134. + if (ret)
  135. + return ret;
  136. + snand->ecc = true;
  137. +
  138. + return 0;
  139. +}
  140. +
  141. +static int spi_nand_disable_ecc(struct spi_nand *snand)
  142. +{
  143. + int ret;
  144. +
  145. + ret = snand->read_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
  146. + if (ret)
  147. + return ret;
  148. +
  149. + snand->buf[0] &= ~SPI_NAND_ECC_EN;
  150. + ret = snand->write_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
  151. + if (ret)
  152. + return ret;
  153. + snand->ecc = false;
  154. +
  155. + return 0;
  156. +}
  157. +
  158. +static int spi_nand_enable_quad(struct spi_nand *snand)
  159. +{
  160. + int ret;
  161. +
  162. + ret = snand->read_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
  163. + if (ret)
  164. + return ret;
  165. +
  166. + snand->buf[0] |= SPI_NAND_QUAD_EN;
  167. + ret = snand->write_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
  168. + if (ret)
  169. + return ret;
  170. +
  171. + return 0;
  172. +}
  173. +/*
  174. + * Wait until the status register busy bit is cleared.
  175. + * Returns a negatie errno on error or time out, and a non-negative status
  176. + * value if the device is ready.
  177. + */
  178. +static int spi_nand_wait_till_ready(struct spi_nand *snand)
  179. +{
  180. + unsigned long deadline = jiffies + msecs_to_jiffies(100);
  181. + bool timeout = false;
  182. + int ret;
  183. +
  184. + /*
  185. + * Perhaps we should set a different timeout for each
  186. + * operation (reset, read, write, erase).
  187. + */
  188. + while (!timeout) {
  189. + if (time_after_eq(jiffies, deadline))
  190. + timeout = true;
  191. +
  192. + ret = snand->read_reg(snand, SPI_NAND_STATUS_REG, snand->buf);
  193. + if (ret < 0) {
  194. + dev_err(snand->dev, "error reading status register\n");
  195. + return ret;
  196. + } else if (!(snand->buf[0] & SPI_NAND_STATUS_REG_BUSY)) {
  197. + return snand->buf[0];
  198. + }
  199. +
  200. + cond_resched();
  201. + }
  202. +
  203. + dev_err(snand->dev, "operation timed out\n");
  204. +
  205. + return -ETIMEDOUT;
  206. +}
  207. +
  208. +static int spi_nand_reset(struct spi_nand *snand)
  209. +{
  210. + int ret;
  211. +
  212. + ret = snand->reset(snand);
  213. + if (ret < 0) {
  214. + dev_err(snand->dev, "reset command failed\n");
  215. + return ret;
  216. + }
  217. +
  218. + /*
  219. + * The NAND core won't wait after a device reset, so we need
  220. + * to do that here.
  221. + */
  222. + ret = spi_nand_wait_till_ready(snand);
  223. + if (ret < 0)
  224. + return ret;
  225. + return 0;
  226. +}
  227. +
  228. +static int spi_nand_status(struct spi_nand *snand)
  229. +{
  230. + int ret, status;
  231. +
  232. + ret = snand->read_reg(snand, SPI_NAND_STATUS_REG, snand->buf);
  233. + if (ret < 0) {
  234. + dev_err(snand->dev, "error reading status register\n");
  235. + return ret;
  236. + }
  237. + status = snand->buf[0];
  238. +
  239. + /* Convert this into standard NAND_STATUS values */
  240. + if (status & SPI_NAND_STATUS_REG_BUSY)
  241. + snand->buf[0] = 0;
  242. + else
  243. + snand->buf[0] = NAND_STATUS_READY;
  244. +
  245. + if (status & SPI_NAND_STATUS_REG_PROG_FAIL ||
  246. + status & SPI_NAND_STATUS_REG_ERASE_FAIL)
  247. + snand->buf[0] |= NAND_STATUS_FAIL;
  248. +
  249. + /*
  250. + * Since we unlock the entire device at initialization, unconditionally
  251. + * set the WP bit to indicate it's not protected.
  252. + */
  253. + snand->buf[0] |= NAND_STATUS_WP;
  254. + return 0;
  255. +}
  256. +
  257. +static int spi_nand_erase(struct spi_nand *snand, int page_addr)
  258. +{
  259. + int ret;
  260. +
  261. + ret = snand->write_enable(snand);
  262. + if (ret < 0) {
  263. + dev_err(snand->dev, "write enable command failed\n");
  264. + return ret;
  265. + }
  266. +
  267. + ret = snand->block_erase(snand, page_addr);
  268. + if (ret < 0) {
  269. + dev_err(snand->dev, "block erase command failed\n");
  270. + return ret;
  271. + }
  272. +
  273. + return 0;
  274. +}
  275. +
  276. +static int spi_nand_write(struct spi_nand *snand)
  277. +{
  278. + int ret;
  279. +
  280. + /* Enable quad mode */
  281. + ret = spi_nand_enable_quad(snand);
  282. + if (ret) {
  283. + dev_err(snand->dev, "error %d enabling quad mode\n", ret);
  284. + return ret;
  285. + }
  286. + /* Store the page to cache */
  287. + ret = snand->store_cache(snand, 0, snand->buf_size, snand->data_buf);
  288. + if (ret < 0) {
  289. + dev_err(snand->dev, "error %d storing page 0x%x to cache\n",
  290. + ret, snand->page_addr);
  291. + return ret;
  292. + }
  293. +
  294. + ret = snand->write_enable(snand);
  295. + if (ret < 0) {
  296. + dev_err(snand->dev, "write enable command failed\n");
  297. + return ret;
  298. + }
  299. +
  300. + /* Get page from the device cache into our internal buffer */
  301. + ret = snand->write_page(snand, snand->page_addr);
  302. + if (ret < 0) {
  303. + dev_err(snand->dev, "error %d reading page 0x%x from cache\n",
  304. + ret, snand->page_addr);
  305. + return ret;
  306. + }
  307. +
  308. + return 0;
  309. +}
  310. +
  311. +static int spi_nand_read_id(struct spi_nand *snand)
  312. +{
  313. + int ret;
  314. +
  315. + ret = snand->read_id(snand, snand->data_buf);
  316. + if (ret < 0) {
  317. + dev_err(snand->dev, "error %d reading ID\n", ret);
  318. + return ret;
  319. + }
  320. + return 0;
  321. +}
  322. +
  323. +static int spi_nand_read_page(struct spi_nand *snand, unsigned int page_addr,
  324. + unsigned int page_offset, size_t length)
  325. +{
  326. + unsigned int corrected = 0, ecc_error = 0;
  327. + int ret;
  328. +
  329. + /* Load a page into the cache register */
  330. + ret = snand->load_page(snand, page_addr);
  331. + if (ret < 0) {
  332. + dev_err(snand->dev, "error %d loading page 0x%x to cache\n",
  333. + ret, page_addr);
  334. + return ret;
  335. + }
  336. +
  337. + ret = spi_nand_wait_till_ready(snand);
  338. + if (ret < 0)
  339. + return ret;
  340. +
  341. + if (snand->ecc) {
  342. + snand->get_ecc_status(ret, &corrected, &ecc_error);
  343. + snand->bitflips = corrected;
  344. +
  345. + /*
  346. + * If there's an ECC error, print a message and notify MTD
  347. + * about it. Then complete the read, to load actual data on
  348. + * the buffer (instead of the status result).
  349. + */
  350. + if (ecc_error) {
  351. + dev_err(snand->dev,
  352. + "internal ECC error reading page 0x%x\n",
  353. + page_addr);
  354. + snand->nand_chip.mtd.ecc_stats.failed++;
  355. + } else {
  356. + snand->nand_chip.mtd.ecc_stats.corrected += corrected;
  357. + }
  358. + }
  359. +
  360. + /* Enable quad mode */
  361. + ret = spi_nand_enable_quad(snand);
  362. + if (ret) {
  363. + dev_err(snand->dev, "error %d enabling quad mode\n", ret);
  364. + return ret;
  365. + }
  366. + /* Get page from the device cache into our internal buffer */
  367. + ret = snand->read_cache(snand, page_offset, length, snand->data_buf);
  368. + if (ret < 0) {
  369. + dev_err(snand->dev, "error %d reading page 0x%x from cache\n",
  370. + ret, page_addr);
  371. + return ret;
  372. + }
  373. + return 0;
  374. +}
  375. +
  376. +static u8 spi_nand_read_byte(struct mtd_info *mtd)
  377. +{
  378. + struct nand_chip *chip = mtd_to_nand(mtd);
  379. + struct spi_nand *snand = nand_get_controller_data(chip);
  380. + char val = 0xff;
  381. +
  382. + if (snand->buf_start < snand->buf_size)
  383. + val = snand->data_buf[snand->buf_start++];
  384. + return val;
  385. +}
  386. +
  387. +static void spi_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  388. +{
  389. + struct nand_chip *chip = mtd_to_nand(mtd);
  390. + struct spi_nand *snand = nand_get_controller_data(chip);
  391. + size_t n = min_t(size_t, len, snand->buf_size - snand->buf_start);
  392. +
  393. + memcpy(snand->data_buf + snand->buf_start, buf, n);
  394. + snand->buf_start += n;
  395. +}
  396. +
  397. +static void spi_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  398. +{
  399. + struct nand_chip *chip = mtd_to_nand(mtd);
  400. + struct spi_nand *snand = nand_get_controller_data(chip);
  401. + size_t n = min_t(size_t, len, snand->buf_size - snand->buf_start);
  402. +
  403. + memcpy(buf, snand->data_buf + snand->buf_start, n);
  404. + snand->buf_start += n;
  405. +}
  406. +
  407. +static int spi_nand_write_page_hwecc(struct mtd_info *mtd,
  408. + struct nand_chip *chip, const uint8_t *buf, int oob_required,
  409. + int page)
  410. +{
  411. + chip->write_buf(mtd, buf, mtd->writesize);
  412. + chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  413. +
  414. + return 0;
  415. +}
  416. +
  417. +static int spi_nand_read_page_hwecc(struct mtd_info *mtd,
  418. + struct nand_chip *chip, uint8_t *buf, int oob_required,
  419. + int page)
  420. +{
  421. + struct spi_nand *snand = nand_get_controller_data(chip);
  422. +
  423. + chip->read_buf(mtd, buf, mtd->writesize);
  424. + chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  425. +
  426. + return snand->bitflips;
  427. +}
  428. +
  429. +static int spi_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  430. +{
  431. + struct spi_nand *snand = nand_get_controller_data(chip);
  432. + int ret;
  433. +
  434. + ret = spi_nand_wait_till_ready(snand);
  435. +
  436. + if (ret < 0) {
  437. + return NAND_STATUS_FAIL;
  438. + } else if (ret & SPI_NAND_STATUS_REG_PROG_FAIL) {
  439. + dev_err(snand->dev, "page program failed\n");
  440. + return NAND_STATUS_FAIL;
  441. + } else if (ret & SPI_NAND_STATUS_REG_ERASE_FAIL) {
  442. + dev_err(snand->dev, "block erase failed\n");
  443. + return NAND_STATUS_FAIL;
  444. + }
  445. +
  446. + return NAND_STATUS_READY;
  447. +}
  448. +
  449. +static void spi_nand_cmdfunc(struct mtd_info *mtd, unsigned int command,
  450. + int column, int page_addr)
  451. +{
  452. + struct nand_chip *chip = mtd_to_nand(mtd);
  453. + struct spi_nand *snand = nand_get_controller_data(chip);
  454. +
  455. + /*
  456. + * In case there's any unsupported command, let's make sure
  457. + * we don't keep garbage around in the buffer.
  458. + */
  459. + if (command != NAND_CMD_PAGEPROG) {
  460. + spi_nand_clear_buffer(snand);
  461. + snand->page_addr = 0;
  462. + }
  463. +
  464. + switch (command) {
  465. + case NAND_CMD_READ0:
  466. + spi_nand_read_page(snand, page_addr, 0, mtd->writesize);
  467. + break;
  468. + case NAND_CMD_READOOB:
  469. + spi_nand_disable_ecc(snand);
  470. + spi_nand_read_page(snand, page_addr, mtd->writesize,
  471. + mtd->oobsize);
  472. + spi_nand_enable_ecc(snand);
  473. + break;
  474. + case NAND_CMD_READID:
  475. + spi_nand_read_id(snand);
  476. + break;
  477. + case NAND_CMD_ERASE1:
  478. + spi_nand_erase(snand, page_addr);
  479. + break;
  480. + case NAND_CMD_ERASE2:
  481. + /* There's nothing to do here, as the erase is one-step */
  482. + break;
  483. + case NAND_CMD_SEQIN:
  484. + snand->buf_start = column;
  485. + snand->page_addr = page_addr;
  486. + break;
  487. + case NAND_CMD_PAGEPROG:
  488. + spi_nand_write(snand);
  489. + break;
  490. + case NAND_CMD_STATUS:
  491. + spi_nand_status(snand);
  492. + break;
  493. + case NAND_CMD_RESET:
  494. + spi_nand_reset(snand);
  495. + break;
  496. + default:
  497. + dev_err(&mtd->dev, "unknown command 0x%x\n", command);
  498. + }
  499. +}
  500. +
  501. +static void spi_nand_select_chip(struct mtd_info *mtd, int chip)
  502. +{
  503. + /* We need this to override the default */
  504. +}
  505. +
  506. +int spi_nand_check(struct spi_nand *snand)
  507. +{
  508. + if (!snand->dev)
  509. + return -ENODEV;
  510. + if (!snand->read_cache)
  511. + return -ENODEV;
  512. + if (!snand->load_page)
  513. + return -ENODEV;
  514. + if (!snand->store_cache)
  515. + return -ENODEV;
  516. + if (!snand->write_page)
  517. + return -ENODEV;
  518. + if (!snand->write_reg)
  519. + return -ENODEV;
  520. + if (!snand->read_reg)
  521. + return -ENODEV;
  522. + if (!snand->block_erase)
  523. + return -ENODEV;
  524. + if (!snand->reset)
  525. + return -ENODEV;
  526. + if (!snand->write_enable)
  527. + return -ENODEV;
  528. + if (!snand->write_disable)
  529. + return -ENODEV;
  530. + if (!snand->get_ecc_status)
  531. + return -ENODEV;
  532. + return 0;
  533. +}
  534. +
  535. +int spi_nand_register(struct spi_nand *snand, struct nand_flash_dev *flash_ids)
  536. +{
  537. + struct nand_chip *chip = &snand->nand_chip;
  538. + struct mtd_info *mtd = nand_to_mtd(chip);
  539. + struct device_node *np = snand->dev->of_node;
  540. + const char __maybe_unused *of_mtd_name = NULL;
  541. + int ret;
  542. +
  543. + /* Let's check all the hooks are in-place so we don't panic later */
  544. + ret = spi_nand_check(snand);
  545. + if (ret)
  546. + return ret;
  547. +
  548. + nand_set_controller_data(chip, snand);
  549. + nand_set_flash_node(chip, np);
  550. + chip->read_buf = spi_nand_read_buf;
  551. + chip->write_buf = spi_nand_write_buf;
  552. + chip->read_byte = spi_nand_read_byte;
  553. + chip->cmdfunc = spi_nand_cmdfunc;
  554. + chip->waitfunc = spi_nand_waitfunc;
  555. + chip->select_chip = spi_nand_select_chip;
  556. + chip->options |= NAND_NO_SUBPAGE_WRITE;
  557. + chip->bits_per_cell = 1;
  558. +
  559. + mtd_set_ooblayout(mtd, snand->ooblayout);
  560. + chip->ecc.read_page = spi_nand_read_page_hwecc;
  561. + chip->ecc.write_page = spi_nand_write_page_hwecc;
  562. + chip->ecc.mode = NAND_ECC_HW;
  563. +
  564. + if (of_property_read_bool(np, "nand-on-flash-bbt"))
  565. + chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  566. +
  567. +#ifdef CONFIG_MTD_OF_PARTS
  568. + of_property_read_string(np, "linux,mtd-name", &of_mtd_name);
  569. +#endif
  570. + if (of_mtd_name)
  571. + mtd->name = of_mtd_name;
  572. + else
  573. + mtd->name = snand->name;
  574. + mtd->owner = THIS_MODULE;
  575. +
  576. + /* Allocate buffer to be used to read/write the internal registers */
  577. + snand->buf = kmalloc(SPI_NAND_CMD_BUF_LEN, GFP_KERNEL);
  578. + if (!snand->buf)
  579. + return -ENOMEM;
  580. +
  581. + /* This is enabled at device power up but we'd better make sure */
  582. + ret = spi_nand_enable_ecc(snand);
  583. + if (ret)
  584. + return ret;
  585. +
  586. + /* Preallocate buffer for flash identification (NAND_CMD_READID) */
  587. + snand->buf_size = SPI_NAND_CMD_BUF_LEN;
  588. + snand->data_buf = kmalloc(snand->buf_size, GFP_KERNEL);
  589. +
  590. + ret = nand_scan_ident(mtd, 1, flash_ids);
  591. + if (ret)
  592. + return ret;
  593. +
  594. + /*
  595. + * SPI NAND has on-die ECC, which means we can correct as much as
  596. + * we are required to. This must be done after identification of
  597. + * the device.
  598. + */
  599. + chip->ecc.strength = chip->ecc_strength_ds;
  600. + chip->ecc.size = chip->ecc_step_ds;
  601. +
  602. + /*
  603. + * Unlock all the device before calling nand_scan_tail. This is needed
  604. + * in case the in-flash bad block table needs to be created.
  605. + * We could override __nand_unlock(), but since it's not currently used
  606. + * by the NAND core we call this explicitly.
  607. + */
  608. + snand->buf[0] = SPI_NAND_PROT_UNLOCK_ALL;
  609. + ret = snand->write_reg(snand, SPI_NAND_LOCK_REG, snand->buf);
  610. + if (ret)
  611. + return ret;
  612. +
  613. + /* Free the buffer and allocate a good one, to fit a page plus OOB */
  614. + kfree(snand->data_buf);
  615. +
  616. + snand->buf_size = mtd->writesize + mtd->oobsize;
  617. + snand->data_buf = kmalloc(snand->buf_size, GFP_KERNEL);
  618. + if (!snand->data_buf)
  619. + return -ENOMEM;
  620. +
  621. + ret = nand_scan_tail(mtd);
  622. + if (ret)
  623. + return ret;
  624. +
  625. + return mtd_device_register(mtd, NULL, 0);
  626. +}
  627. +EXPORT_SYMBOL_GPL(spi_nand_register);
  628. +
  629. +void spi_nand_unregister(struct spi_nand *snand)
  630. +{
  631. + kfree(snand->buf);
  632. + kfree(snand->data_buf);
  633. +}
  634. +EXPORT_SYMBOL_GPL(spi_nand_unregister);
  635. +
  636. +MODULE_AUTHOR("Ezequiel Garcia <[email protected]>");
  637. +MODULE_DESCRIPTION("Framework for SPI NAND");
  638. +MODULE_LICENSE("GPL v2");
  639. --- /dev/null
  640. +++ b/include/linux/mtd/spi-nand.h
  641. @@ -0,0 +1,54 @@
  642. +/*
  643. + * Copyright (C) 2014 Imagination Technologies Ltd.
  644. + *
  645. + * This program is free software; you can redistribute it and/or modify
  646. + * it under the terms of the GNU General Public License as published by
  647. + * the Free Software Foundation; version 2 of the License.
  648. + */
  649. +
  650. +#ifndef __LINUX_MTD_SPI_NAND_H
  651. +#define __LINUX_MTD_SPI_NAND_H
  652. +
  653. +#include <linux/mtd/mtd.h>
  654. +#include <linux/mtd/rawnand.h>
  655. +
  656. +struct spi_nand {
  657. + struct nand_chip nand_chip;
  658. + struct device *dev;
  659. + const char *name;
  660. +
  661. + u8 *buf, *data_buf;
  662. + size_t buf_size;
  663. + off_t buf_start;
  664. + unsigned int page_addr;
  665. + unsigned int bitflips;
  666. + bool ecc;
  667. + struct mtd_ooblayout_ops *ooblayout;
  668. +
  669. + int (*reset)(struct spi_nand *snand);
  670. + int (*read_id)(struct spi_nand *snand, u8 *buf);
  671. +
  672. + int (*write_disable)(struct spi_nand *snand);
  673. + int (*write_enable)(struct spi_nand *snand);
  674. +
  675. + int (*read_reg)(struct spi_nand *snand, u8 opcode, u8 *buf);
  676. + int (*write_reg)(struct spi_nand *snand, u8 opcode, u8 *buf);
  677. + void (*get_ecc_status)(unsigned int status,
  678. + unsigned int *corrected,
  679. + unsigned int *ecc_errors);
  680. +
  681. + int (*store_cache)(struct spi_nand *snand, unsigned int page_offset,
  682. + size_t length, u8 *write_buf);
  683. + int (*write_page)(struct spi_nand *snand, unsigned int page_addr);
  684. + int (*load_page)(struct spi_nand *snand, unsigned int page_addr);
  685. + int (*read_cache)(struct spi_nand *snand, unsigned int page_offset,
  686. + size_t length, u8 *read_buf);
  687. + int (*block_erase)(struct spi_nand *snand, unsigned int page_addr);
  688. +
  689. + void *priv;
  690. +};
  691. +
  692. +int spi_nand_register(struct spi_nand *snand, struct nand_flash_dev *flash_ids);
  693. +void spi_nand_unregister(struct spi_nand *snand);
  694. +
  695. +#endif