core.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 int spinand_manufacturer_detect(struct spinand_device *spinand)
  617. {
  618. return -ENOTSUPP;
  619. }
  620. static int spinand_manufacturer_init(struct spinand_device *spinand)
  621. {
  622. if (spinand->manufacturer->ops->init)
  623. return spinand->manufacturer->ops->init(spinand);
  624. return 0;
  625. }
  626. static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
  627. {
  628. /* Release manufacturer private data */
  629. if (spinand->manufacturer->ops->cleanup)
  630. return spinand->manufacturer->ops->cleanup(spinand);
  631. }
  632. static const struct spi_mem_op *
  633. spinand_select_op_variant(struct spinand_device *spinand,
  634. const struct spinand_op_variants *variants)
  635. {
  636. struct nand_device *nand = spinand_to_nand(spinand);
  637. unsigned int i;
  638. for (i = 0; i < variants->nops; i++) {
  639. struct spi_mem_op op = variants->ops[i];
  640. unsigned int nbytes;
  641. int ret;
  642. nbytes = nanddev_per_page_oobsize(nand) +
  643. nanddev_page_size(nand);
  644. while (nbytes) {
  645. op.data.nbytes = nbytes;
  646. ret = spi_mem_adjust_op_size(spinand->spimem, &op);
  647. if (ret)
  648. break;
  649. if (!spi_mem_supports_op(spinand->spimem, &op))
  650. break;
  651. nbytes -= op.data.nbytes;
  652. }
  653. if (!nbytes)
  654. return &variants->ops[i];
  655. }
  656. return NULL;
  657. }
  658. /**
  659. * spinand_match_and_init() - Try to find a match between a device ID and an
  660. * entry in a spinand_info table
  661. * @spinand: SPI NAND object
  662. * @table: SPI NAND device description table
  663. * @table_size: size of the device description table
  664. *
  665. * Should be used by SPI NAND manufacturer drivers when they want to find a
  666. * match between a device ID retrieved through the READ_ID command and an
  667. * entry in the SPI NAND description table. If a match is found, the spinand
  668. * object will be initialized with information provided by the matching
  669. * spinand_info entry.
  670. *
  671. * Return: 0 on success, a negative error code otherwise.
  672. */
  673. int spinand_match_and_init(struct spinand_device *spinand,
  674. const struct spinand_info *table,
  675. unsigned int table_size, u8 devid)
  676. {
  677. struct nand_device *nand = spinand_to_nand(spinand);
  678. unsigned int i;
  679. for (i = 0; i < table_size; i++) {
  680. const struct spinand_info *info = &table[i];
  681. const struct spi_mem_op *op;
  682. if (devid != info->devid)
  683. continue;
  684. nand->memorg = table[i].memorg;
  685. nand->eccreq = table[i].eccreq;
  686. spinand->eccinfo = table[i].eccinfo;
  687. spinand->flags = table[i].flags;
  688. spinand->select_target = table[i].select_target;
  689. op = spinand_select_op_variant(spinand,
  690. info->op_variants.read_cache);
  691. if (!op)
  692. return -ENOTSUPP;
  693. spinand->op_templates.read_cache = op;
  694. op = spinand_select_op_variant(spinand,
  695. info->op_variants.write_cache);
  696. if (!op)
  697. return -ENOTSUPP;
  698. spinand->op_templates.write_cache = op;
  699. op = spinand_select_op_variant(spinand,
  700. info->op_variants.update_cache);
  701. spinand->op_templates.update_cache = op;
  702. return 0;
  703. }
  704. return -ENOTSUPP;
  705. }
  706. static int spinand_detect(struct spinand_device *spinand)
  707. {
  708. struct device *dev = &spinand->spimem->spi->dev;
  709. struct nand_device *nand = spinand_to_nand(spinand);
  710. int ret;
  711. ret = spinand_reset_op(spinand);
  712. if (ret)
  713. return ret;
  714. ret = spinand_read_id_op(spinand, spinand->id.data);
  715. if (ret)
  716. return ret;
  717. spinand->id.len = SPINAND_MAX_ID_LEN;
  718. ret = spinand_manufacturer_detect(spinand);
  719. if (ret) {
  720. dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
  721. spinand->id.data);
  722. return ret;
  723. }
  724. if (nand->memorg.ntargets > 1 && !spinand->select_target) {
  725. dev_err(dev,
  726. "SPI NANDs with more than one die must implement ->select_target()\n");
  727. return -EINVAL;
  728. }
  729. dev_info(&spinand->spimem->spi->dev,
  730. "%s SPI NAND was found.\n", spinand->manufacturer->name);
  731. dev_info(&spinand->spimem->spi->dev,
  732. "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
  733. nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
  734. nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
  735. return 0;
  736. }
  737. static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
  738. struct mtd_oob_region *region)
  739. {
  740. return -ERANGE;
  741. }
  742. static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
  743. struct mtd_oob_region *region)
  744. {
  745. if (section)
  746. return -ERANGE;
  747. /* Reserve 2 bytes for the BBM. */
  748. region->offset = 2;
  749. region->length = 62;
  750. return 0;
  751. }
  752. static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
  753. .ecc = spinand_noecc_ooblayout_ecc,
  754. .free = spinand_noecc_ooblayout_free,
  755. };
  756. static int spinand_init(struct spinand_device *spinand)
  757. {
  758. struct device *dev = &spinand->spimem->spi->dev;
  759. struct mtd_info *mtd = spinand_to_mtd(spinand);
  760. struct nand_device *nand = mtd_to_nanddev(mtd);
  761. int ret, i;
  762. /*
  763. * We need a scratch buffer because the spi_mem interface requires that
  764. * buf passed in spi_mem_op->data.buf be DMA-able.
  765. */
  766. spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
  767. if (!spinand->scratchbuf)
  768. return -ENOMEM;
  769. ret = spinand_detect(spinand);
  770. if (ret)
  771. goto err_free_bufs;
  772. /*
  773. * Use kzalloc() instead of devm_kzalloc() here, because some drivers
  774. * may use this buffer for DMA access.
  775. * Memory allocated by devm_ does not guarantee DMA-safe alignment.
  776. */
  777. spinand->databuf = kzalloc(nanddev_page_size(nand) +
  778. nanddev_per_page_oobsize(nand),
  779. GFP_KERNEL);
  780. if (!spinand->databuf) {
  781. ret = -ENOMEM;
  782. goto err_free_bufs;
  783. }
  784. spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
  785. ret = spinand_init_cfg_cache(spinand);
  786. if (ret)
  787. goto err_free_bufs;
  788. ret = spinand_init_quad_enable(spinand);
  789. if (ret)
  790. goto err_free_bufs;
  791. ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
  792. if (ret)
  793. goto err_free_bufs;
  794. ret = spinand_manufacturer_init(spinand);
  795. if (ret) {
  796. dev_err(dev,
  797. "Failed to initialize the SPI NAND chip (err = %d)\n",
  798. ret);
  799. goto err_free_bufs;
  800. }
  801. /* After power up, all blocks are locked, so unlock them here. */
  802. for (i = 0; i < nand->memorg.ntargets; i++) {
  803. ret = spinand_select_target(spinand, i);
  804. if (ret)
  805. goto err_free_bufs;
  806. ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
  807. if (ret)
  808. goto err_free_bufs;
  809. }
  810. ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
  811. if (ret)
  812. goto err_manuf_cleanup;
  813. /*
  814. * Right now, we don't support ECC, so let the whole oob
  815. * area is available for user.
  816. */
  817. mtd->_read_oob = spinand_mtd_read;
  818. mtd->_write_oob = spinand_mtd_write;
  819. mtd->_block_isbad = spinand_mtd_block_isbad;
  820. mtd->_block_markbad = spinand_mtd_block_markbad;
  821. mtd->_block_isreserved = spinand_mtd_block_isreserved;
  822. mtd->_erase = spinand_mtd_erase;
  823. if (spinand->eccinfo.ooblayout)
  824. mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
  825. else
  826. mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
  827. ret = mtd_ooblayout_count_freebytes(mtd);
  828. if (ret < 0)
  829. goto err_cleanup_nanddev;
  830. mtd->oobavail = ret;
  831. return 0;
  832. err_cleanup_nanddev:
  833. nanddev_cleanup(nand);
  834. err_manuf_cleanup:
  835. spinand_manufacturer_cleanup(spinand);
  836. err_free_bufs:
  837. kfree(spinand->databuf);
  838. kfree(spinand->scratchbuf);
  839. return ret;
  840. }
  841. static void spinand_cleanup(struct spinand_device *spinand)
  842. {
  843. struct nand_device *nand = spinand_to_nand(spinand);
  844. nanddev_cleanup(nand);
  845. spinand_manufacturer_cleanup(spinand);
  846. kfree(spinand->databuf);
  847. kfree(spinand->scratchbuf);
  848. }
  849. static int spinand_probe(struct spi_mem *mem)
  850. {
  851. struct spinand_device *spinand;
  852. struct mtd_info *mtd;
  853. int ret;
  854. spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
  855. GFP_KERNEL);
  856. if (!spinand)
  857. return -ENOMEM;
  858. spinand->spimem = mem;
  859. spi_mem_set_drvdata(mem, spinand);
  860. spinand_set_of_node(spinand, mem->spi->dev.of_node);
  861. mutex_init(&spinand->lock);
  862. mtd = spinand_to_mtd(spinand);
  863. mtd->dev.parent = &mem->spi->dev;
  864. ret = spinand_init(spinand);
  865. if (ret)
  866. return ret;
  867. ret = mtd_device_register(mtd, NULL, 0);
  868. if (ret)
  869. goto err_spinand_cleanup;
  870. return 0;
  871. err_spinand_cleanup:
  872. spinand_cleanup(spinand);
  873. return ret;
  874. }
  875. static int spinand_remove(struct spi_mem *mem)
  876. {
  877. struct spinand_device *spinand;
  878. struct mtd_info *mtd;
  879. int ret;
  880. spinand = spi_mem_get_drvdata(mem);
  881. mtd = spinand_to_mtd(spinand);
  882. ret = mtd_device_unregister(mtd);
  883. if (ret)
  884. return ret;
  885. spinand_cleanup(spinand);
  886. return 0;
  887. }
  888. static const struct spi_device_id spinand_ids[] = {
  889. { .name = "spi-nand" },
  890. { /* sentinel */ },
  891. };
  892. #ifdef CONFIG_OF
  893. static const struct of_device_id spinand_of_ids[] = {
  894. { .compatible = "spi-nand" },
  895. { /* sentinel */ },
  896. };
  897. #endif
  898. static struct spi_mem_driver spinand_drv = {
  899. .spidrv = {
  900. .id_table = spinand_ids,
  901. .driver = {
  902. .name = "spi-nand",
  903. .of_match_table = of_match_ptr(spinand_of_ids),
  904. },
  905. },
  906. .probe = spinand_probe,
  907. .remove = spinand_remove,
  908. };
  909. module_spi_mem_driver(spinand_drv);
  910. MODULE_DESCRIPTION("SPI NAND framework");
  911. MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
  912. MODULE_LICENSE("GPL v2");