core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016-2017 Micron Technology, Inc.
  4. *
  5. * Authors:
  6. * Peter Pan <peterpandong@micron.com>
  7. * Boris Brezillon <boris.brezillon@bootlin.com>
  8. */
  9. #define pr_fmt(fmt) "spi-nand: " fmt
  10. #include <linux/device.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/mtd/spinand.h>
  15. #include <linux/of.h>
  16. #include <linux/slab.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/spi/spi-mem.h>
  19. static void spinand_cache_op_adjust_colum(struct spinand_device *spinand,
  20. const struct nand_page_io_req *req,
  21. u16 *column)
  22. {
  23. struct nand_device *nand = spinand_to_nand(spinand);
  24. unsigned int shift;
  25. if (nand->memorg.planes_per_lun < 2)
  26. return;
  27. /* The plane number is passed in MSB just above the column address */
  28. shift = fls(nand->memorg.pagesize);
  29. *column |= req->pos.plane << shift;
  30. }
  31. static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)
  32. {
  33. struct spi_mem_op op = SPINAND_GET_FEATURE_OP(reg,
  34. spinand->scratchbuf);
  35. int ret;
  36. ret = spi_mem_exec_op(spinand->spimem, &op);
  37. if (ret)
  38. return ret;
  39. *val = *spinand->scratchbuf;
  40. return 0;
  41. }
  42. static int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val)
  43. {
  44. struct spi_mem_op op = SPINAND_SET_FEATURE_OP(reg,
  45. spinand->scratchbuf);
  46. *spinand->scratchbuf = val;
  47. return spi_mem_exec_op(spinand->spimem, &op);
  48. }
  49. static int spinand_read_status(struct spinand_device *spinand, u8 *status)
  50. {
  51. return spinand_read_reg_op(spinand, REG_STATUS, status);
  52. }
  53. static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg)
  54. {
  55. struct nand_device *nand = spinand_to_nand(spinand);
  56. if (WARN_ON(spinand->cur_target < 0 ||
  57. spinand->cur_target >= nand->memorg.ntargets))
  58. return -EINVAL;
  59. *cfg = spinand->cfg_cache[spinand->cur_target];
  60. return 0;
  61. }
  62. static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg)
  63. {
  64. struct nand_device *nand = spinand_to_nand(spinand);
  65. int ret;
  66. if (WARN_ON(spinand->cur_target < 0 ||
  67. spinand->cur_target >= nand->memorg.ntargets))
  68. return -EINVAL;
  69. if (spinand->cfg_cache[spinand->cur_target] == cfg)
  70. return 0;
  71. ret = spinand_write_reg_op(spinand, REG_CFG, cfg);
  72. if (ret)
  73. return ret;
  74. spinand->cfg_cache[spinand->cur_target] = cfg;
  75. return 0;
  76. }
  77. /**
  78. * spinand_upd_cfg() - Update the configuration register
  79. * @spinand: the spinand device
  80. * @mask: the mask encoding the bits to update in the config reg
  81. * @val: the new value to apply
  82. *
  83. * Update the configuration register.
  84. *
  85. * Return: 0 on success, a negative error code otherwise.
  86. */
  87. int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val)
  88. {
  89. int ret;
  90. u8 cfg;
  91. ret = spinand_get_cfg(spinand, &cfg);
  92. if (ret)
  93. return ret;
  94. cfg &= ~mask;
  95. cfg |= val;
  96. return spinand_set_cfg(spinand, cfg);
  97. }
  98. /**
  99. * spinand_select_target() - Select a specific NAND target/die
  100. * @spinand: the spinand device
  101. * @target: the target/die to select
  102. *
  103. * Select a new target/die. If chip only has one die, this function is a NOOP.
  104. *
  105. * Return: 0 on success, a negative error code otherwise.
  106. */
  107. int spinand_select_target(struct spinand_device *spinand, unsigned int target)
  108. {
  109. struct nand_device *nand = spinand_to_nand(spinand);
  110. int ret;
  111. if (WARN_ON(target >= nand->memorg.ntargets))
  112. return -EINVAL;
  113. if (spinand->cur_target == target)
  114. return 0;
  115. if (nand->memorg.ntargets == 1) {
  116. spinand->cur_target = target;
  117. return 0;
  118. }
  119. ret = spinand->select_target(spinand, target);
  120. if (ret)
  121. return ret;
  122. spinand->cur_target = target;
  123. return 0;
  124. }
  125. static int spinand_init_cfg_cache(struct spinand_device *spinand)
  126. {
  127. struct nand_device *nand = spinand_to_nand(spinand);
  128. struct device *dev = &spinand->spimem->spi->dev;
  129. unsigned int target;
  130. int ret;
  131. spinand->cfg_cache = devm_kcalloc(dev,
  132. nand->memorg.ntargets,
  133. sizeof(*spinand->cfg_cache),
  134. GFP_KERNEL);
  135. if (!spinand->cfg_cache)
  136. return -ENOMEM;
  137. for (target = 0; target < nand->memorg.ntargets; target++) {
  138. ret = spinand_select_target(spinand, target);
  139. if (ret)
  140. return ret;
  141. /*
  142. * We use spinand_read_reg_op() instead of spinand_get_cfg()
  143. * here to bypass the config cache.
  144. */
  145. ret = spinand_read_reg_op(spinand, REG_CFG,
  146. &spinand->cfg_cache[target]);
  147. if (ret)
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. static int spinand_init_quad_enable(struct spinand_device *spinand)
  153. {
  154. bool enable = false;
  155. if (!(spinand->flags & SPINAND_HAS_QE_BIT))
  156. return 0;
  157. if (spinand->op_templates.read_cache->data.buswidth == 4 ||
  158. spinand->op_templates.write_cache->data.buswidth == 4 ||
  159. spinand->op_templates.update_cache->data.buswidth == 4)
  160. enable = true;
  161. return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE,
  162. enable ? CFG_QUAD_ENABLE : 0);
  163. }
  164. static int spinand_ecc_enable(struct spinand_device *spinand,
  165. bool enable)
  166. {
  167. return spinand_upd_cfg(spinand, CFG_ECC_ENABLE,
  168. enable ? CFG_ECC_ENABLE : 0);
  169. }
  170. static int spinand_write_enable_op(struct spinand_device *spinand)
  171. {
  172. struct spi_mem_op op = SPINAND_WR_EN_DIS_OP(true);
  173. return spi_mem_exec_op(spinand->spimem, &op);
  174. }
  175. static int spinand_load_page_op(struct spinand_device *spinand,
  176. const struct nand_page_io_req *req)
  177. {
  178. struct nand_device *nand = spinand_to_nand(spinand);
  179. unsigned int row = nanddev_pos_to_row(nand, &req->pos);
  180. struct spi_mem_op op = SPINAND_PAGE_READ_OP(row);
  181. return spi_mem_exec_op(spinand->spimem, &op);
  182. }
  183. static int spinand_read_from_cache_op(struct spinand_device *spinand,
  184. const struct nand_page_io_req *req)
  185. {
  186. struct spi_mem_op op = *spinand->op_templates.read_cache;
  187. struct nand_device *nand = spinand_to_nand(spinand);
  188. struct mtd_info *mtd = nanddev_to_mtd(nand);
  189. struct nand_page_io_req adjreq = *req;
  190. unsigned int nbytes = 0;
  191. void *buf = NULL;
  192. u16 column = 0;
  193. int ret;
  194. if (req->datalen) {
  195. adjreq.datalen = nanddev_page_size(nand);
  196. adjreq.dataoffs = 0;
  197. adjreq.databuf.in = spinand->databuf;
  198. buf = spinand->databuf;
  199. nbytes = adjreq.datalen;
  200. }
  201. if (req->ooblen) {
  202. adjreq.ooblen = nanddev_per_page_oobsize(nand);
  203. adjreq.ooboffs = 0;
  204. adjreq.oobbuf.in = spinand->oobbuf;
  205. nbytes += nanddev_per_page_oobsize(nand);
  206. if (!buf) {
  207. buf = spinand->oobbuf;
  208. column = nanddev_page_size(nand);
  209. }
  210. }
  211. spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
  212. op.addr.val = column;
  213. /*
  214. * Some controllers are limited in term of max RX data size. In this
  215. * case, just repeat the READ_CACHE operation after updating the
  216. * column.
  217. */
  218. while (nbytes) {
  219. op.data.buf.in = buf;
  220. op.data.nbytes = nbytes;
  221. ret = spi_mem_adjust_op_size(spinand->spimem, &op);
  222. if (ret)
  223. return ret;
  224. ret = spi_mem_exec_op(spinand->spimem, &op);
  225. if (ret)
  226. return ret;
  227. buf += op.data.nbytes;
  228. nbytes -= op.data.nbytes;
  229. op.addr.val += op.data.nbytes;
  230. }
  231. if (req->datalen)
  232. memcpy(req->databuf.in, spinand->databuf + req->dataoffs,
  233. req->datalen);
  234. if (req->ooblen) {
  235. if (req->mode == MTD_OPS_AUTO_OOB)
  236. mtd_ooblayout_get_databytes(mtd, req->oobbuf.in,
  237. spinand->oobbuf,
  238. req->ooboffs,
  239. req->ooblen);
  240. else
  241. memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
  242. req->ooblen);
  243. }
  244. return 0;
  245. }
  246. static int spinand_write_to_cache_op(struct spinand_device *spinand,
  247. const struct nand_page_io_req *req)
  248. {
  249. struct spi_mem_op op = *spinand->op_templates.write_cache;
  250. struct nand_device *nand = spinand_to_nand(spinand);
  251. struct mtd_info *mtd = nanddev_to_mtd(nand);
  252. struct nand_page_io_req adjreq = *req;
  253. unsigned int nbytes = 0;
  254. void *buf = NULL;
  255. u16 column = 0;
  256. int ret;
  257. memset(spinand->databuf, 0xff,
  258. nanddev_page_size(nand) +
  259. nanddev_per_page_oobsize(nand));
  260. if (req->datalen) {
  261. memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
  262. req->datalen);
  263. adjreq.dataoffs = 0;
  264. adjreq.datalen = nanddev_page_size(nand);
  265. adjreq.databuf.out = spinand->databuf;
  266. nbytes = adjreq.datalen;
  267. buf = spinand->databuf;
  268. }
  269. if (req->ooblen) {
  270. if (req->mode == MTD_OPS_AUTO_OOB)
  271. mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
  272. spinand->oobbuf,
  273. req->ooboffs,
  274. req->ooblen);
  275. else
  276. memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
  277. req->ooblen);
  278. adjreq.ooblen = nanddev_per_page_oobsize(nand);
  279. adjreq.ooboffs = 0;
  280. nbytes += nanddev_per_page_oobsize(nand);
  281. if (!buf) {
  282. buf = spinand->oobbuf;
  283. column = nanddev_page_size(nand);
  284. }
  285. }
  286. spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
  287. op = *spinand->op_templates.write_cache;
  288. op.addr.val = column;
  289. /*
  290. * Some controllers are limited in term of max TX data size. In this
  291. * case, split the operation into one LOAD CACHE and one or more
  292. * LOAD RANDOM CACHE.
  293. */
  294. while (nbytes) {
  295. op.data.buf.out = buf;
  296. op.data.nbytes = nbytes;
  297. ret = spi_mem_adjust_op_size(spinand->spimem, &op);
  298. if (ret)
  299. return ret;
  300. ret = spi_mem_exec_op(spinand->spimem, &op);
  301. if (ret)
  302. return ret;
  303. buf += op.data.nbytes;
  304. nbytes -= op.data.nbytes;
  305. op.addr.val += op.data.nbytes;
  306. /*
  307. * We need to use the RANDOM LOAD CACHE operation if there's
  308. * more than one iteration, because the LOAD operation resets
  309. * the cache to 0xff.
  310. */
  311. if (nbytes) {
  312. column = op.addr.val;
  313. op = *spinand->op_templates.update_cache;
  314. op.addr.val = column;
  315. }
  316. }
  317. return 0;
  318. }
  319. static int spinand_program_op(struct spinand_device *spinand,
  320. const struct nand_page_io_req *req)
  321. {
  322. struct nand_device *nand = spinand_to_nand(spinand);
  323. unsigned int row = nanddev_pos_to_row(nand, &req->pos);
  324. struct spi_mem_op op = SPINAND_PROG_EXEC_OP(row);
  325. return spi_mem_exec_op(spinand->spimem, &op);
  326. }
  327. static int spinand_erase_op(struct spinand_device *spinand,
  328. const struct nand_pos *pos)
  329. {
  330. struct nand_device *nand = spinand_to_nand(spinand);
  331. unsigned int row = nanddev_pos_to_row(nand, pos);
  332. struct spi_mem_op op = SPINAND_BLK_ERASE_OP(row);
  333. return spi_mem_exec_op(spinand->spimem, &op);
  334. }
  335. static int spinand_wait(struct spinand_device *spinand, u8 *s)
  336. {
  337. unsigned long timeo = jiffies + msecs_to_jiffies(400);
  338. u8 status;
  339. int ret;
  340. do {
  341. ret = spinand_read_status(spinand, &status);
  342. if (ret)
  343. return ret;
  344. if (!(status & STATUS_BUSY))
  345. goto out;
  346. } while (time_before(jiffies, timeo));
  347. /*
  348. * Extra read, just in case the STATUS_READY bit has changed
  349. * since our last check
  350. */
  351. ret = spinand_read_status(spinand, &status);
  352. if (ret)
  353. return ret;
  354. out:
  355. if (s)
  356. *s = status;
  357. return status & STATUS_BUSY ? -ETIMEDOUT : 0;
  358. }
  359. static int spinand_read_id_op(struct spinand_device *spinand, u8 *buf)
  360. {
  361. struct spi_mem_op op = SPINAND_READID_OP(0, spinand->scratchbuf,
  362. SPINAND_MAX_ID_LEN);
  363. int ret;
  364. ret = spi_mem_exec_op(spinand->spimem, &op);
  365. if (!ret)
  366. memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
  367. return ret;
  368. }
  369. static int spinand_reset_op(struct spinand_device *spinand)
  370. {
  371. struct spi_mem_op op = SPINAND_RESET_OP;
  372. int ret;
  373. ret = spi_mem_exec_op(spinand->spimem, &op);
  374. if (ret)
  375. return ret;
  376. return spinand_wait(spinand, NULL);
  377. }
  378. static int spinand_lock_block(struct spinand_device *spinand, u8 lock)
  379. {
  380. return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock);
  381. }
  382. static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
  383. {
  384. struct nand_device *nand = spinand_to_nand(spinand);
  385. if (spinand->eccinfo.get_status)
  386. return spinand->eccinfo.get_status(spinand, status);
  387. switch (status & STATUS_ECC_MASK) {
  388. case STATUS_ECC_NO_BITFLIPS:
  389. return 0;
  390. case STATUS_ECC_HAS_BITFLIPS:
  391. /*
  392. * We have no way to know exactly how many bitflips have been
  393. * fixed, so let's return the maximum possible value so that
  394. * wear-leveling layers move the data immediately.
  395. */
  396. return nand->eccreq.strength;
  397. case STATUS_ECC_UNCOR_ERROR:
  398. return -EBADMSG;
  399. default:
  400. break;
  401. }
  402. return -EINVAL;
  403. }
  404. static int spinand_read_page(struct spinand_device *spinand,
  405. const struct nand_page_io_req *req,
  406. bool ecc_enabled)
  407. {
  408. u8 status;
  409. int ret;
  410. ret = spinand_load_page_op(spinand, req);
  411. if (ret)
  412. return ret;
  413. ret = spinand_wait(spinand, &status);
  414. if (ret < 0)
  415. return ret;
  416. ret = spinand_read_from_cache_op(spinand, req);
  417. if (ret)
  418. return ret;
  419. if (!ecc_enabled)
  420. return 0;
  421. return spinand_check_ecc_status(spinand, status);
  422. }
  423. static int spinand_write_page(struct spinand_device *spinand,
  424. const struct nand_page_io_req *req)
  425. {
  426. u8 status;
  427. int ret;
  428. ret = spinand_write_enable_op(spinand);
  429. if (ret)
  430. return ret;
  431. ret = spinand_write_to_cache_op(spinand, req);
  432. if (ret)
  433. return ret;
  434. ret = spinand_program_op(spinand, req);
  435. if (ret)
  436. return ret;
  437. ret = spinand_wait(spinand, &status);
  438. if (!ret && (status & STATUS_PROG_FAILED))
  439. ret = -EIO;
  440. return ret;
  441. }
  442. static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
  443. struct mtd_oob_ops *ops)
  444. {
  445. struct spinand_device *spinand = mtd_to_spinand(mtd);
  446. struct nand_device *nand = mtd_to_nanddev(mtd);
  447. unsigned int max_bitflips = 0;
  448. struct nand_io_iter iter;
  449. bool enable_ecc = false;
  450. bool ecc_failed = false;
  451. int ret = 0;
  452. if (ops->mode != MTD_OPS_RAW && spinand->eccinfo.ooblayout)
  453. enable_ecc = true;
  454. mutex_lock(&spinand->lock);
  455. nanddev_io_for_each_page(nand, from, ops, &iter) {
  456. ret = spinand_select_target(spinand, iter.req.pos.target);
  457. if (ret)
  458. break;
  459. ret = spinand_ecc_enable(spinand, enable_ecc);
  460. if (ret)
  461. break;
  462. ret = spinand_read_page(spinand, &iter.req, enable_ecc);
  463. if (ret < 0 && ret != -EBADMSG)
  464. break;
  465. if (ret == -EBADMSG) {
  466. ecc_failed = true;
  467. mtd->ecc_stats.failed++;
  468. ret = 0;
  469. } else {
  470. mtd->ecc_stats.corrected += ret;
  471. max_bitflips = max_t(unsigned int, max_bitflips, ret);
  472. }
  473. ops->retlen += iter.req.datalen;
  474. ops->oobretlen += iter.req.ooblen;
  475. }
  476. mutex_unlock(&spinand->lock);
  477. if (ecc_failed && !ret)
  478. ret = -EBADMSG;
  479. return ret ? ret : max_bitflips;
  480. }
  481. static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
  482. struct mtd_oob_ops *ops)
  483. {
  484. struct spinand_device *spinand = mtd_to_spinand(mtd);
  485. struct nand_device *nand = mtd_to_nanddev(mtd);
  486. struct nand_io_iter iter;
  487. bool enable_ecc = false;
  488. int ret = 0;
  489. if (ops->mode != MTD_OPS_RAW && mtd->ooblayout)
  490. enable_ecc = true;
  491. mutex_lock(&spinand->lock);
  492. nanddev_io_for_each_page(nand, to, ops, &iter) {
  493. ret = spinand_select_target(spinand, iter.req.pos.target);
  494. if (ret)
  495. break;
  496. ret = spinand_ecc_enable(spinand, enable_ecc);
  497. if (ret)
  498. break;
  499. ret = spinand_write_page(spinand, &iter.req);
  500. if (ret)
  501. break;
  502. ops->retlen += iter.req.datalen;
  503. ops->oobretlen += iter.req.ooblen;
  504. }
  505. mutex_unlock(&spinand->lock);
  506. return ret;
  507. }
  508. static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
  509. {
  510. struct spinand_device *spinand = nand_to_spinand(nand);
  511. struct nand_page_io_req req = {
  512. .pos = *pos,
  513. .ooblen = 2,
  514. .ooboffs = 0,
  515. .oobbuf.in = spinand->oobbuf,
  516. .mode = MTD_OPS_RAW,
  517. };
  518. memset(spinand->oobbuf, 0, 2);
  519. spinand_select_target(spinand, pos->target);
  520. spinand_read_page(spinand, &req, false);
  521. if (spinand->oobbuf[0] != 0xff || spinand->oobbuf[1] != 0xff)
  522. return true;
  523. return false;
  524. }
  525. static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
  526. {
  527. struct nand_device *nand = mtd_to_nanddev(mtd);
  528. struct spinand_device *spinand = nand_to_spinand(nand);
  529. struct nand_pos pos;
  530. int ret;
  531. nanddev_offs_to_pos(nand, offs, &pos);
  532. mutex_lock(&spinand->lock);
  533. ret = nanddev_isbad(nand, &pos);
  534. mutex_unlock(&spinand->lock);
  535. return ret;
  536. }
  537. static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
  538. {
  539. struct spinand_device *spinand = nand_to_spinand(nand);
  540. struct nand_page_io_req req = {
  541. .pos = *pos,
  542. .ooboffs = 0,
  543. .ooblen = 2,
  544. .oobbuf.out = spinand->oobbuf,
  545. };
  546. int ret;
  547. /* Erase block before marking it bad. */
  548. ret = spinand_select_target(spinand, pos->target);
  549. if (ret)
  550. return ret;
  551. ret = spinand_write_enable_op(spinand);
  552. if (ret)
  553. return ret;
  554. spinand_erase_op(spinand, pos);
  555. memset(spinand->oobbuf, 0, 2);
  556. return spinand_write_page(spinand, &req);
  557. }
  558. static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
  559. {
  560. struct nand_device *nand = mtd_to_nanddev(mtd);
  561. struct spinand_device *spinand = nand_to_spinand(nand);
  562. struct nand_pos pos;
  563. int ret;
  564. nanddev_offs_to_pos(nand, offs, &pos);
  565. mutex_lock(&spinand->lock);
  566. ret = nanddev_markbad(nand, &pos);
  567. mutex_unlock(&spinand->lock);
  568. return ret;
  569. }
  570. static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
  571. {
  572. struct spinand_device *spinand = nand_to_spinand(nand);
  573. u8 status;
  574. int ret;
  575. ret = spinand_select_target(spinand, pos->target);
  576. if (ret)
  577. return ret;
  578. ret = spinand_write_enable_op(spinand);
  579. if (ret)
  580. return ret;
  581. ret = spinand_erase_op(spinand, pos);
  582. if (ret)
  583. return ret;
  584. ret = spinand_wait(spinand, &status);
  585. if (!ret && (status & STATUS_ERASE_FAILED))
  586. ret = -EIO;
  587. return ret;
  588. }
  589. static int spinand_mtd_erase(struct mtd_info *mtd,
  590. struct erase_info *einfo)
  591. {
  592. struct spinand_device *spinand = mtd_to_spinand(mtd);
  593. int ret;
  594. mutex_lock(&spinand->lock);
  595. ret = nanddev_mtd_erase(mtd, einfo);
  596. mutex_unlock(&spinand->lock);
  597. return ret;
  598. }
  599. static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
  600. {
  601. struct spinand_device *spinand = mtd_to_spinand(mtd);
  602. struct nand_device *nand = mtd_to_nanddev(mtd);
  603. struct nand_pos pos;
  604. int ret;
  605. nanddev_offs_to_pos(nand, offs, &pos);
  606. mutex_lock(&spinand->lock);
  607. ret = nanddev_isreserved(nand, &pos);
  608. mutex_unlock(&spinand->lock);
  609. return ret;
  610. }
  611. static const struct nand_ops spinand_ops = {
  612. .erase = spinand_erase,
  613. .markbad = spinand_markbad,
  614. .isbad = spinand_isbad,
  615. };
  616. static const struct spinand_manufacturer *spinand_manufacturers[] = {
  617. &macronix_spinand_manufacturer,
  618. &micron_spinand_manufacturer,
  619. &winbond_spinand_manufacturer,
  620. };
  621. static int spinand_manufacturer_detect(struct spinand_device *spinand)
  622. {
  623. unsigned int i;
  624. int ret;
  625. for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
  626. ret = spinand_manufacturers[i]->ops->detect(spinand);
  627. if (ret > 0) {
  628. spinand->manufacturer = spinand_manufacturers[i];
  629. return 0;
  630. } else if (ret < 0) {
  631. return ret;
  632. }
  633. }
  634. return -ENOTSUPP;
  635. }
  636. static int spinand_manufacturer_init(struct spinand_device *spinand)
  637. {
  638. if (spinand->manufacturer->ops->init)
  639. return spinand->manufacturer->ops->init(spinand);
  640. return 0;
  641. }
  642. static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
  643. {
  644. /* Release manufacturer private data */
  645. if (spinand->manufacturer->ops->cleanup)
  646. return spinand->manufacturer->ops->cleanup(spinand);
  647. }
  648. static const struct spi_mem_op *
  649. spinand_select_op_variant(struct spinand_device *spinand,
  650. const struct spinand_op_variants *variants)
  651. {
  652. struct nand_device *nand = spinand_to_nand(spinand);
  653. unsigned int i;
  654. for (i = 0; i < variants->nops; i++) {
  655. struct spi_mem_op op = variants->ops[i];
  656. unsigned int nbytes;
  657. int ret;
  658. nbytes = nanddev_per_page_oobsize(nand) +
  659. nanddev_page_size(nand);
  660. while (nbytes) {
  661. op.data.nbytes = nbytes;
  662. ret = spi_mem_adjust_op_size(spinand->spimem, &op);
  663. if (ret)
  664. break;
  665. if (!spi_mem_supports_op(spinand->spimem, &op))
  666. break;
  667. nbytes -= op.data.nbytes;
  668. }
  669. if (!nbytes)
  670. return &variants->ops[i];
  671. }
  672. return NULL;
  673. }
  674. /**
  675. * spinand_match_and_init() - Try to find a match between a device ID and an
  676. * entry in a spinand_info table
  677. * @spinand: SPI NAND object
  678. * @table: SPI NAND device description table
  679. * @table_size: size of the device description table
  680. *
  681. * Should be used by SPI NAND manufacturer drivers when they want to find a
  682. * match between a device ID retrieved through the READ_ID command and an
  683. * entry in the SPI NAND description table. If a match is found, the spinand
  684. * object will be initialized with information provided by the matching
  685. * spinand_info entry.
  686. *
  687. * Return: 0 on success, a negative error code otherwise.
  688. */
  689. int spinand_match_and_init(struct spinand_device *spinand,
  690. const struct spinand_info *table,
  691. unsigned int table_size, u8 devid)
  692. {
  693. struct nand_device *nand = spinand_to_nand(spinand);
  694. unsigned int i;
  695. for (i = 0; i < table_size; i++) {
  696. const struct spinand_info *info = &table[i];
  697. const struct spi_mem_op *op;
  698. if (devid != info->devid)
  699. continue;
  700. nand->memorg = table[i].memorg;
  701. nand->eccreq = table[i].eccreq;
  702. spinand->eccinfo = table[i].eccinfo;
  703. spinand->flags = table[i].flags;
  704. spinand->select_target = table[i].select_target;
  705. op = spinand_select_op_variant(spinand,
  706. info->op_variants.read_cache);
  707. if (!op)
  708. return -ENOTSUPP;
  709. spinand->op_templates.read_cache = op;
  710. op = spinand_select_op_variant(spinand,
  711. info->op_variants.write_cache);
  712. if (!op)
  713. return -ENOTSUPP;
  714. spinand->op_templates.write_cache = op;
  715. op = spinand_select_op_variant(spinand,
  716. info->op_variants.update_cache);
  717. spinand->op_templates.update_cache = op;
  718. return 0;
  719. }
  720. return -ENOTSUPP;
  721. }
  722. static int spinand_detect(struct spinand_device *spinand)
  723. {
  724. struct device *dev = &spinand->spimem->spi->dev;
  725. struct nand_device *nand = spinand_to_nand(spinand);
  726. int ret;
  727. ret = spinand_reset_op(spinand);
  728. if (ret)
  729. return ret;
  730. ret = spinand_read_id_op(spinand, spinand->id.data);
  731. if (ret)
  732. return ret;
  733. spinand->id.len = SPINAND_MAX_ID_LEN;
  734. ret = spinand_manufacturer_detect(spinand);
  735. if (ret) {
  736. dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
  737. spinand->id.data);
  738. return ret;
  739. }
  740. if (nand->memorg.ntargets > 1 && !spinand->select_target) {
  741. dev_err(dev,
  742. "SPI NANDs with more than one die must implement ->select_target()\n");
  743. return -EINVAL;
  744. }
  745. dev_info(&spinand->spimem->spi->dev,
  746. "%s SPI NAND was found.\n", spinand->manufacturer->name);
  747. dev_info(&spinand->spimem->spi->dev,
  748. "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
  749. nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
  750. nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
  751. return 0;
  752. }
  753. static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
  754. struct mtd_oob_region *region)
  755. {
  756. return -ERANGE;
  757. }
  758. static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
  759. struct mtd_oob_region *region)
  760. {
  761. if (section)
  762. return -ERANGE;
  763. /* Reserve 2 bytes for the BBM. */
  764. region->offset = 2;
  765. region->length = 62;
  766. return 0;
  767. }
  768. static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
  769. .ecc = spinand_noecc_ooblayout_ecc,
  770. .free = spinand_noecc_ooblayout_free,
  771. };
  772. static int spinand_init(struct spinand_device *spinand)
  773. {
  774. struct device *dev = &spinand->spimem->spi->dev;
  775. struct mtd_info *mtd = spinand_to_mtd(spinand);
  776. struct nand_device *nand = mtd_to_nanddev(mtd);
  777. int ret, i;
  778. /*
  779. * We need a scratch buffer because the spi_mem interface requires that
  780. * buf passed in spi_mem_op->data.buf be DMA-able.
  781. */
  782. spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
  783. if (!spinand->scratchbuf)
  784. return -ENOMEM;
  785. ret = spinand_detect(spinand);
  786. if (ret)
  787. goto err_free_bufs;
  788. /*
  789. * Use kzalloc() instead of devm_kzalloc() here, because some drivers
  790. * may use this buffer for DMA access.
  791. * Memory allocated by devm_ does not guarantee DMA-safe alignment.
  792. */
  793. spinand->databuf = kzalloc(nanddev_page_size(nand) +
  794. nanddev_per_page_oobsize(nand),
  795. GFP_KERNEL);
  796. if (!spinand->databuf) {
  797. ret = -ENOMEM;
  798. goto err_free_bufs;
  799. }
  800. spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
  801. ret = spinand_init_cfg_cache(spinand);
  802. if (ret)
  803. goto err_free_bufs;
  804. ret = spinand_init_quad_enable(spinand);
  805. if (ret)
  806. goto err_free_bufs;
  807. ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
  808. if (ret)
  809. goto err_free_bufs;
  810. ret = spinand_manufacturer_init(spinand);
  811. if (ret) {
  812. dev_err(dev,
  813. "Failed to initialize the SPI NAND chip (err = %d)\n",
  814. ret);
  815. goto err_free_bufs;
  816. }
  817. /* After power up, all blocks are locked, so unlock them here. */
  818. for (i = 0; i < nand->memorg.ntargets; i++) {
  819. ret = spinand_select_target(spinand, i);
  820. if (ret)
  821. goto err_free_bufs;
  822. ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
  823. if (ret)
  824. goto err_free_bufs;
  825. }
  826. ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
  827. if (ret)
  828. goto err_manuf_cleanup;
  829. /*
  830. * Right now, we don't support ECC, so let the whole oob
  831. * area is available for user.
  832. */
  833. mtd->_read_oob = spinand_mtd_read;
  834. mtd->_write_oob = spinand_mtd_write;
  835. mtd->_block_isbad = spinand_mtd_block_isbad;
  836. mtd->_block_markbad = spinand_mtd_block_markbad;
  837. mtd->_block_isreserved = spinand_mtd_block_isreserved;
  838. mtd->_erase = spinand_mtd_erase;
  839. if (spinand->eccinfo.ooblayout)
  840. mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
  841. else
  842. mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
  843. ret = mtd_ooblayout_count_freebytes(mtd);
  844. if (ret < 0)
  845. goto err_cleanup_nanddev;
  846. mtd->oobavail = ret;
  847. return 0;
  848. err_cleanup_nanddev:
  849. nanddev_cleanup(nand);
  850. err_manuf_cleanup:
  851. spinand_manufacturer_cleanup(spinand);
  852. err_free_bufs:
  853. kfree(spinand->databuf);
  854. kfree(spinand->scratchbuf);
  855. return ret;
  856. }
  857. static void spinand_cleanup(struct spinand_device *spinand)
  858. {
  859. struct nand_device *nand = spinand_to_nand(spinand);
  860. nanddev_cleanup(nand);
  861. spinand_manufacturer_cleanup(spinand);
  862. kfree(spinand->databuf);
  863. kfree(spinand->scratchbuf);
  864. }
  865. static int spinand_probe(struct spi_mem *mem)
  866. {
  867. struct spinand_device *spinand;
  868. struct mtd_info *mtd;
  869. int ret;
  870. spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
  871. GFP_KERNEL);
  872. if (!spinand)
  873. return -ENOMEM;
  874. spinand->spimem = mem;
  875. spi_mem_set_drvdata(mem, spinand);
  876. spinand_set_of_node(spinand, mem->spi->dev.of_node);
  877. mutex_init(&spinand->lock);
  878. mtd = spinand_to_mtd(spinand);
  879. mtd->dev.parent = &mem->spi->dev;
  880. ret = spinand_init(spinand);
  881. if (ret)
  882. return ret;
  883. ret = mtd_device_register(mtd, NULL, 0);
  884. if (ret)
  885. goto err_spinand_cleanup;
  886. return 0;
  887. err_spinand_cleanup:
  888. spinand_cleanup(spinand);
  889. return ret;
  890. }
  891. static int spinand_remove(struct spi_mem *mem)
  892. {
  893. struct spinand_device *spinand;
  894. struct mtd_info *mtd;
  895. int ret;
  896. spinand = spi_mem_get_drvdata(mem);
  897. mtd = spinand_to_mtd(spinand);
  898. ret = mtd_device_unregister(mtd);
  899. if (ret)
  900. return ret;
  901. spinand_cleanup(spinand);
  902. return 0;
  903. }
  904. static const struct spi_device_id spinand_ids[] = {
  905. { .name = "spi-nand" },
  906. { /* sentinel */ },
  907. };
  908. #ifdef CONFIG_OF
  909. static const struct of_device_id spinand_of_ids[] = {
  910. { .compatible = "spi-nand" },
  911. { /* sentinel */ },
  912. };
  913. #endif
  914. static struct spi_mem_driver spinand_drv = {
  915. .spidrv = {
  916. .id_table = spinand_ids,
  917. .driver = {
  918. .name = "spi-nand",
  919. .of_match_table = of_match_ptr(spinand_of_ids),
  920. },
  921. },
  922. .probe = spinand_probe,
  923. .remove = spinand_remove,
  924. };
  925. module_spi_mem_driver(spinand_drv);
  926. MODULE_DESCRIPTION("SPI NAND framework");
  927. MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
  928. MODULE_LICENSE("GPL v2");