core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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. void *buf = spinand->databuf;
  254. unsigned int nbytes;
  255. u16 column = 0;
  256. int ret;
  257. /*
  258. * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset
  259. * the cache content to 0xFF (depends on vendor implementation), so we
  260. * must fill the page cache entirely even if we only want to program
  261. * the data portion of the page, otherwise we might corrupt the BBM or
  262. * user data previously programmed in OOB area.
  263. */
  264. nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
  265. memset(spinand->databuf, 0xff, nbytes);
  266. adjreq.dataoffs = 0;
  267. adjreq.datalen = nanddev_page_size(nand);
  268. adjreq.databuf.out = spinand->databuf;
  269. adjreq.ooblen = nanddev_per_page_oobsize(nand);
  270. adjreq.ooboffs = 0;
  271. adjreq.oobbuf.out = spinand->oobbuf;
  272. if (req->datalen)
  273. memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
  274. req->datalen);
  275. if (req->ooblen) {
  276. if (req->mode == MTD_OPS_AUTO_OOB)
  277. mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
  278. spinand->oobbuf,
  279. req->ooboffs,
  280. req->ooblen);
  281. else
  282. memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
  283. req->ooblen);
  284. }
  285. spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
  286. op = *spinand->op_templates.write_cache;
  287. op.addr.val = column;
  288. /*
  289. * Some controllers are limited in term of max TX data size. In this
  290. * case, split the operation into one LOAD CACHE and one or more
  291. * LOAD RANDOM CACHE.
  292. */
  293. while (nbytes) {
  294. op.data.buf.out = buf;
  295. op.data.nbytes = nbytes;
  296. ret = spi_mem_adjust_op_size(spinand->spimem, &op);
  297. if (ret)
  298. return ret;
  299. ret = spi_mem_exec_op(spinand->spimem, &op);
  300. if (ret)
  301. return ret;
  302. buf += op.data.nbytes;
  303. nbytes -= op.data.nbytes;
  304. op.addr.val += op.data.nbytes;
  305. /*
  306. * We need to use the RANDOM LOAD CACHE operation if there's
  307. * more than one iteration, because the LOAD operation might
  308. * reset the cache to 0xff.
  309. */
  310. if (nbytes) {
  311. column = op.addr.val;
  312. op = *spinand->op_templates.update_cache;
  313. op.addr.val = column;
  314. }
  315. }
  316. return 0;
  317. }
  318. static int spinand_program_op(struct spinand_device *spinand,
  319. const struct nand_page_io_req *req)
  320. {
  321. struct nand_device *nand = spinand_to_nand(spinand);
  322. unsigned int row = nanddev_pos_to_row(nand, &req->pos);
  323. struct spi_mem_op op = SPINAND_PROG_EXEC_OP(row);
  324. return spi_mem_exec_op(spinand->spimem, &op);
  325. }
  326. static int spinand_erase_op(struct spinand_device *spinand,
  327. const struct nand_pos *pos)
  328. {
  329. struct nand_device *nand = spinand_to_nand(spinand);
  330. unsigned int row = nanddev_pos_to_row(nand, pos);
  331. struct spi_mem_op op = SPINAND_BLK_ERASE_OP(row);
  332. return spi_mem_exec_op(spinand->spimem, &op);
  333. }
  334. static int spinand_wait(struct spinand_device *spinand, u8 *s)
  335. {
  336. unsigned long timeo = jiffies + msecs_to_jiffies(400);
  337. u8 status;
  338. int ret;
  339. do {
  340. ret = spinand_read_status(spinand, &status);
  341. if (ret)
  342. return ret;
  343. if (!(status & STATUS_BUSY))
  344. goto out;
  345. } while (time_before(jiffies, timeo));
  346. /*
  347. * Extra read, just in case the STATUS_READY bit has changed
  348. * since our last check
  349. */
  350. ret = spinand_read_status(spinand, &status);
  351. if (ret)
  352. return ret;
  353. out:
  354. if (s)
  355. *s = status;
  356. return status & STATUS_BUSY ? -ETIMEDOUT : 0;
  357. }
  358. static int spinand_read_id_op(struct spinand_device *spinand, u8 *buf)
  359. {
  360. struct spi_mem_op op = SPINAND_READID_OP(0, spinand->scratchbuf,
  361. SPINAND_MAX_ID_LEN);
  362. int ret;
  363. ret = spi_mem_exec_op(spinand->spimem, &op);
  364. if (!ret)
  365. memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
  366. return ret;
  367. }
  368. static int spinand_reset_op(struct spinand_device *spinand)
  369. {
  370. struct spi_mem_op op = SPINAND_RESET_OP;
  371. int ret;
  372. ret = spi_mem_exec_op(spinand->spimem, &op);
  373. if (ret)
  374. return ret;
  375. return spinand_wait(spinand, NULL);
  376. }
  377. static int spinand_lock_block(struct spinand_device *spinand, u8 lock)
  378. {
  379. return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock);
  380. }
  381. static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
  382. {
  383. struct nand_device *nand = spinand_to_nand(spinand);
  384. if (spinand->eccinfo.get_status)
  385. return spinand->eccinfo.get_status(spinand, status);
  386. switch (status & STATUS_ECC_MASK) {
  387. case STATUS_ECC_NO_BITFLIPS:
  388. return 0;
  389. case STATUS_ECC_HAS_BITFLIPS:
  390. /*
  391. * We have no way to know exactly how many bitflips have been
  392. * fixed, so let's return the maximum possible value so that
  393. * wear-leveling layers move the data immediately.
  394. */
  395. return nand->eccreq.strength;
  396. case STATUS_ECC_UNCOR_ERROR:
  397. return -EBADMSG;
  398. default:
  399. break;
  400. }
  401. return -EINVAL;
  402. }
  403. static int spinand_read_page(struct spinand_device *spinand,
  404. const struct nand_page_io_req *req,
  405. bool ecc_enabled)
  406. {
  407. u8 status;
  408. int ret;
  409. ret = spinand_load_page_op(spinand, req);
  410. if (ret)
  411. return ret;
  412. ret = spinand_wait(spinand, &status);
  413. if (ret < 0)
  414. return ret;
  415. ret = spinand_read_from_cache_op(spinand, req);
  416. if (ret)
  417. return ret;
  418. if (!ecc_enabled)
  419. return 0;
  420. return spinand_check_ecc_status(spinand, status);
  421. }
  422. static int spinand_write_page(struct spinand_device *spinand,
  423. const struct nand_page_io_req *req)
  424. {
  425. u8 status;
  426. int ret;
  427. ret = spinand_write_enable_op(spinand);
  428. if (ret)
  429. return ret;
  430. ret = spinand_write_to_cache_op(spinand, req);
  431. if (ret)
  432. return ret;
  433. ret = spinand_program_op(spinand, req);
  434. if (ret)
  435. return ret;
  436. ret = spinand_wait(spinand, &status);
  437. if (!ret && (status & STATUS_PROG_FAILED))
  438. ret = -EIO;
  439. return ret;
  440. }
  441. static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
  442. struct mtd_oob_ops *ops)
  443. {
  444. struct spinand_device *spinand = mtd_to_spinand(mtd);
  445. struct nand_device *nand = mtd_to_nanddev(mtd);
  446. unsigned int max_bitflips = 0;
  447. struct nand_io_iter iter;
  448. bool enable_ecc = false;
  449. bool ecc_failed = false;
  450. int ret = 0;
  451. if (ops->mode != MTD_OPS_RAW && spinand->eccinfo.ooblayout)
  452. enable_ecc = true;
  453. mutex_lock(&spinand->lock);
  454. nanddev_io_for_each_page(nand, from, ops, &iter) {
  455. ret = spinand_select_target(spinand, iter.req.pos.target);
  456. if (ret)
  457. break;
  458. ret = spinand_ecc_enable(spinand, enable_ecc);
  459. if (ret)
  460. break;
  461. ret = spinand_read_page(spinand, &iter.req, enable_ecc);
  462. if (ret < 0 && ret != -EBADMSG)
  463. break;
  464. if (ret == -EBADMSG) {
  465. ecc_failed = true;
  466. mtd->ecc_stats.failed++;
  467. } else {
  468. mtd->ecc_stats.corrected += ret;
  469. max_bitflips = max_t(unsigned int, max_bitflips, ret);
  470. }
  471. ret = 0;
  472. ops->retlen += iter.req.datalen;
  473. ops->oobretlen += iter.req.ooblen;
  474. }
  475. mutex_unlock(&spinand->lock);
  476. if (ecc_failed && !ret)
  477. ret = -EBADMSG;
  478. return ret ? ret : max_bitflips;
  479. }
  480. static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
  481. struct mtd_oob_ops *ops)
  482. {
  483. struct spinand_device *spinand = mtd_to_spinand(mtd);
  484. struct nand_device *nand = mtd_to_nanddev(mtd);
  485. struct nand_io_iter iter;
  486. bool enable_ecc = false;
  487. int ret = 0;
  488. if (ops->mode != MTD_OPS_RAW && mtd->ooblayout)
  489. enable_ecc = true;
  490. mutex_lock(&spinand->lock);
  491. nanddev_io_for_each_page(nand, to, ops, &iter) {
  492. ret = spinand_select_target(spinand, iter.req.pos.target);
  493. if (ret)
  494. break;
  495. ret = spinand_ecc_enable(spinand, enable_ecc);
  496. if (ret)
  497. break;
  498. ret = spinand_write_page(spinand, &iter.req);
  499. if (ret)
  500. break;
  501. ops->retlen += iter.req.datalen;
  502. ops->oobretlen += iter.req.ooblen;
  503. }
  504. mutex_unlock(&spinand->lock);
  505. return ret;
  506. }
  507. static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
  508. {
  509. struct spinand_device *spinand = nand_to_spinand(nand);
  510. u8 marker[2] = { };
  511. struct nand_page_io_req req = {
  512. .pos = *pos,
  513. .ooblen = sizeof(marker),
  514. .ooboffs = 0,
  515. .oobbuf.in = marker,
  516. .mode = MTD_OPS_RAW,
  517. };
  518. spinand_select_target(spinand, pos->target);
  519. spinand_read_page(spinand, &req, false);
  520. if (marker[0] != 0xff || marker[1] != 0xff)
  521. return true;
  522. return false;
  523. }
  524. static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
  525. {
  526. struct nand_device *nand = mtd_to_nanddev(mtd);
  527. struct spinand_device *spinand = nand_to_spinand(nand);
  528. struct nand_pos pos;
  529. int ret;
  530. nanddev_offs_to_pos(nand, offs, &pos);
  531. mutex_lock(&spinand->lock);
  532. ret = nanddev_isbad(nand, &pos);
  533. mutex_unlock(&spinand->lock);
  534. return ret;
  535. }
  536. static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
  537. {
  538. struct spinand_device *spinand = nand_to_spinand(nand);
  539. u8 marker[2] = { };
  540. struct nand_page_io_req req = {
  541. .pos = *pos,
  542. .ooboffs = 0,
  543. .ooblen = sizeof(marker),
  544. .oobbuf.out = marker,
  545. .mode = MTD_OPS_RAW,
  546. };
  547. int ret;
  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. return spinand_write_page(spinand, &req);
  555. }
  556. static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
  557. {
  558. struct nand_device *nand = mtd_to_nanddev(mtd);
  559. struct spinand_device *spinand = nand_to_spinand(nand);
  560. struct nand_pos pos;
  561. int ret;
  562. nanddev_offs_to_pos(nand, offs, &pos);
  563. mutex_lock(&spinand->lock);
  564. ret = nanddev_markbad(nand, &pos);
  565. mutex_unlock(&spinand->lock);
  566. return ret;
  567. }
  568. static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
  569. {
  570. struct spinand_device *spinand = nand_to_spinand(nand);
  571. u8 status;
  572. int ret;
  573. ret = spinand_select_target(spinand, pos->target);
  574. if (ret)
  575. return ret;
  576. ret = spinand_write_enable_op(spinand);
  577. if (ret)
  578. return ret;
  579. ret = spinand_erase_op(spinand, pos);
  580. if (ret)
  581. return ret;
  582. ret = spinand_wait(spinand, &status);
  583. if (!ret && (status & STATUS_ERASE_FAILED))
  584. ret = -EIO;
  585. return ret;
  586. }
  587. static int spinand_mtd_erase(struct mtd_info *mtd,
  588. struct erase_info *einfo)
  589. {
  590. struct spinand_device *spinand = mtd_to_spinand(mtd);
  591. int ret;
  592. mutex_lock(&spinand->lock);
  593. ret = nanddev_mtd_erase(mtd, einfo);
  594. mutex_unlock(&spinand->lock);
  595. return ret;
  596. }
  597. static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
  598. {
  599. struct spinand_device *spinand = mtd_to_spinand(mtd);
  600. struct nand_device *nand = mtd_to_nanddev(mtd);
  601. struct nand_pos pos;
  602. int ret;
  603. nanddev_offs_to_pos(nand, offs, &pos);
  604. mutex_lock(&spinand->lock);
  605. ret = nanddev_isreserved(nand, &pos);
  606. mutex_unlock(&spinand->lock);
  607. return ret;
  608. }
  609. static const struct nand_ops spinand_ops = {
  610. .erase = spinand_erase,
  611. .markbad = spinand_markbad,
  612. .isbad = spinand_isbad,
  613. };
  614. static const struct spinand_manufacturer *spinand_manufacturers[] = {
  615. &macronix_spinand_manufacturer,
  616. &micron_spinand_manufacturer,
  617. &winbond_spinand_manufacturer,
  618. };
  619. static int spinand_manufacturer_detect(struct spinand_device *spinand)
  620. {
  621. unsigned int i;
  622. int ret;
  623. for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
  624. ret = spinand_manufacturers[i]->ops->detect(spinand);
  625. if (ret > 0) {
  626. spinand->manufacturer = spinand_manufacturers[i];
  627. return 0;
  628. } else if (ret < 0) {
  629. return ret;
  630. }
  631. }
  632. return -ENOTSUPP;
  633. }
  634. static int spinand_manufacturer_init(struct spinand_device *spinand)
  635. {
  636. if (spinand->manufacturer->ops->init)
  637. return spinand->manufacturer->ops->init(spinand);
  638. return 0;
  639. }
  640. static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
  641. {
  642. /* Release manufacturer private data */
  643. if (spinand->manufacturer->ops->cleanup)
  644. return spinand->manufacturer->ops->cleanup(spinand);
  645. }
  646. static const struct spi_mem_op *
  647. spinand_select_op_variant(struct spinand_device *spinand,
  648. const struct spinand_op_variants *variants)
  649. {
  650. struct nand_device *nand = spinand_to_nand(spinand);
  651. unsigned int i;
  652. for (i = 0; i < variants->nops; i++) {
  653. struct spi_mem_op op = variants->ops[i];
  654. unsigned int nbytes;
  655. int ret;
  656. nbytes = nanddev_per_page_oobsize(nand) +
  657. nanddev_page_size(nand);
  658. while (nbytes) {
  659. op.data.nbytes = nbytes;
  660. ret = spi_mem_adjust_op_size(spinand->spimem, &op);
  661. if (ret)
  662. break;
  663. if (!spi_mem_supports_op(spinand->spimem, &op))
  664. break;
  665. nbytes -= op.data.nbytes;
  666. }
  667. if (!nbytes)
  668. return &variants->ops[i];
  669. }
  670. return NULL;
  671. }
  672. /**
  673. * spinand_match_and_init() - Try to find a match between a device ID and an
  674. * entry in a spinand_info table
  675. * @spinand: SPI NAND object
  676. * @table: SPI NAND device description table
  677. * @table_size: size of the device description table
  678. *
  679. * Should be used by SPI NAND manufacturer drivers when they want to find a
  680. * match between a device ID retrieved through the READ_ID command and an
  681. * entry in the SPI NAND description table. If a match is found, the spinand
  682. * object will be initialized with information provided by the matching
  683. * spinand_info entry.
  684. *
  685. * Return: 0 on success, a negative error code otherwise.
  686. */
  687. int spinand_match_and_init(struct spinand_device *spinand,
  688. const struct spinand_info *table,
  689. unsigned int table_size, u8 devid)
  690. {
  691. struct nand_device *nand = spinand_to_nand(spinand);
  692. unsigned int i;
  693. for (i = 0; i < table_size; i++) {
  694. const struct spinand_info *info = &table[i];
  695. const struct spi_mem_op *op;
  696. if (devid != info->devid)
  697. continue;
  698. nand->memorg = table[i].memorg;
  699. nand->eccreq = table[i].eccreq;
  700. spinand->eccinfo = table[i].eccinfo;
  701. spinand->flags = table[i].flags;
  702. spinand->select_target = table[i].select_target;
  703. op = spinand_select_op_variant(spinand,
  704. info->op_variants.read_cache);
  705. if (!op)
  706. return -ENOTSUPP;
  707. spinand->op_templates.read_cache = op;
  708. op = spinand_select_op_variant(spinand,
  709. info->op_variants.write_cache);
  710. if (!op)
  711. return -ENOTSUPP;
  712. spinand->op_templates.write_cache = op;
  713. op = spinand_select_op_variant(spinand,
  714. info->op_variants.update_cache);
  715. spinand->op_templates.update_cache = op;
  716. return 0;
  717. }
  718. return -ENOTSUPP;
  719. }
  720. static int spinand_detect(struct spinand_device *spinand)
  721. {
  722. struct device *dev = &spinand->spimem->spi->dev;
  723. struct nand_device *nand = spinand_to_nand(spinand);
  724. int ret;
  725. ret = spinand_reset_op(spinand);
  726. if (ret)
  727. return ret;
  728. ret = spinand_read_id_op(spinand, spinand->id.data);
  729. if (ret)
  730. return ret;
  731. spinand->id.len = SPINAND_MAX_ID_LEN;
  732. ret = spinand_manufacturer_detect(spinand);
  733. if (ret) {
  734. dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
  735. spinand->id.data);
  736. return ret;
  737. }
  738. if (nand->memorg.ntargets > 1 && !spinand->select_target) {
  739. dev_err(dev,
  740. "SPI NANDs with more than one die must implement ->select_target()\n");
  741. return -EINVAL;
  742. }
  743. dev_info(&spinand->spimem->spi->dev,
  744. "%s SPI NAND was found.\n", spinand->manufacturer->name);
  745. dev_info(&spinand->spimem->spi->dev,
  746. "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
  747. nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
  748. nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
  749. return 0;
  750. }
  751. static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
  752. struct mtd_oob_region *region)
  753. {
  754. return -ERANGE;
  755. }
  756. static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
  757. struct mtd_oob_region *region)
  758. {
  759. if (section)
  760. return -ERANGE;
  761. /* Reserve 2 bytes for the BBM. */
  762. region->offset = 2;
  763. region->length = 62;
  764. return 0;
  765. }
  766. static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
  767. .ecc = spinand_noecc_ooblayout_ecc,
  768. .free = spinand_noecc_ooblayout_free,
  769. };
  770. static int spinand_init(struct spinand_device *spinand)
  771. {
  772. struct device *dev = &spinand->spimem->spi->dev;
  773. struct mtd_info *mtd = spinand_to_mtd(spinand);
  774. struct nand_device *nand = mtd_to_nanddev(mtd);
  775. int ret, i;
  776. /*
  777. * We need a scratch buffer because the spi_mem interface requires that
  778. * buf passed in spi_mem_op->data.buf be DMA-able.
  779. */
  780. spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
  781. if (!spinand->scratchbuf)
  782. return -ENOMEM;
  783. ret = spinand_detect(spinand);
  784. if (ret)
  785. goto err_free_bufs;
  786. /*
  787. * Use kzalloc() instead of devm_kzalloc() here, because some drivers
  788. * may use this buffer for DMA access.
  789. * Memory allocated by devm_ does not guarantee DMA-safe alignment.
  790. */
  791. spinand->databuf = kzalloc(nanddev_page_size(nand) +
  792. nanddev_per_page_oobsize(nand),
  793. GFP_KERNEL);
  794. if (!spinand->databuf) {
  795. ret = -ENOMEM;
  796. goto err_free_bufs;
  797. }
  798. spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
  799. ret = spinand_init_cfg_cache(spinand);
  800. if (ret)
  801. goto err_free_bufs;
  802. ret = spinand_init_quad_enable(spinand);
  803. if (ret)
  804. goto err_free_bufs;
  805. ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
  806. if (ret)
  807. goto err_free_bufs;
  808. ret = spinand_manufacturer_init(spinand);
  809. if (ret) {
  810. dev_err(dev,
  811. "Failed to initialize the SPI NAND chip (err = %d)\n",
  812. ret);
  813. goto err_free_bufs;
  814. }
  815. /* After power up, all blocks are locked, so unlock them here. */
  816. for (i = 0; i < nand->memorg.ntargets; i++) {
  817. ret = spinand_select_target(spinand, i);
  818. if (ret)
  819. goto err_manuf_cleanup;
  820. ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
  821. if (ret)
  822. goto err_manuf_cleanup;
  823. }
  824. ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
  825. if (ret)
  826. goto err_manuf_cleanup;
  827. /*
  828. * Right now, we don't support ECC, so let the whole oob
  829. * area is available for user.
  830. */
  831. mtd->_read_oob = spinand_mtd_read;
  832. mtd->_write_oob = spinand_mtd_write;
  833. mtd->_block_isbad = spinand_mtd_block_isbad;
  834. mtd->_block_markbad = spinand_mtd_block_markbad;
  835. mtd->_block_isreserved = spinand_mtd_block_isreserved;
  836. mtd->_erase = spinand_mtd_erase;
  837. if (spinand->eccinfo.ooblayout)
  838. mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
  839. else
  840. mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
  841. ret = mtd_ooblayout_count_freebytes(mtd);
  842. if (ret < 0)
  843. goto err_cleanup_nanddev;
  844. mtd->oobavail = ret;
  845. /* Propagate ECC information to mtd_info */
  846. mtd->ecc_strength = nand->eccreq.strength;
  847. mtd->ecc_step_size = nand->eccreq.step_size;
  848. return 0;
  849. err_cleanup_nanddev:
  850. nanddev_cleanup(nand);
  851. err_manuf_cleanup:
  852. spinand_manufacturer_cleanup(spinand);
  853. err_free_bufs:
  854. kfree(spinand->databuf);
  855. kfree(spinand->scratchbuf);
  856. return ret;
  857. }
  858. static void spinand_cleanup(struct spinand_device *spinand)
  859. {
  860. struct nand_device *nand = spinand_to_nand(spinand);
  861. nanddev_cleanup(nand);
  862. spinand_manufacturer_cleanup(spinand);
  863. kfree(spinand->databuf);
  864. kfree(spinand->scratchbuf);
  865. }
  866. static int spinand_probe(struct spi_mem *mem)
  867. {
  868. struct spinand_device *spinand;
  869. struct mtd_info *mtd;
  870. int ret;
  871. spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
  872. GFP_KERNEL);
  873. if (!spinand)
  874. return -ENOMEM;
  875. spinand->spimem = mem;
  876. spi_mem_set_drvdata(mem, spinand);
  877. spinand_set_of_node(spinand, mem->spi->dev.of_node);
  878. mutex_init(&spinand->lock);
  879. mtd = spinand_to_mtd(spinand);
  880. mtd->dev.parent = &mem->spi->dev;
  881. ret = spinand_init(spinand);
  882. if (ret)
  883. return ret;
  884. ret = mtd_device_register(mtd, NULL, 0);
  885. if (ret)
  886. goto err_spinand_cleanup;
  887. return 0;
  888. err_spinand_cleanup:
  889. spinand_cleanup(spinand);
  890. return ret;
  891. }
  892. static int spinand_remove(struct spi_mem *mem)
  893. {
  894. struct spinand_device *spinand;
  895. struct mtd_info *mtd;
  896. int ret;
  897. spinand = spi_mem_get_drvdata(mem);
  898. mtd = spinand_to_mtd(spinand);
  899. ret = mtd_device_unregister(mtd);
  900. if (ret)
  901. return ret;
  902. spinand_cleanup(spinand);
  903. return 0;
  904. }
  905. static const struct spi_device_id spinand_ids[] = {
  906. { .name = "spi-nand" },
  907. { /* sentinel */ },
  908. };
  909. MODULE_DEVICE_TABLE(spi, spinand_ids);
  910. #ifdef CONFIG_OF
  911. static const struct of_device_id spinand_of_ids[] = {
  912. { .compatible = "spi-nand" },
  913. { /* sentinel */ },
  914. };
  915. MODULE_DEVICE_TABLE(of, spinand_of_ids);
  916. #endif
  917. static struct spi_mem_driver spinand_drv = {
  918. .spidrv = {
  919. .id_table = spinand_ids,
  920. .driver = {
  921. .name = "spi-nand",
  922. .of_match_table = of_match_ptr(spinand_of_ids),
  923. },
  924. },
  925. .probe = spinand_probe,
  926. .remove = spinand_remove,
  927. };
  928. module_spi_mem_driver(spinand_drv);
  929. MODULE_DESCRIPTION("SPI NAND framework");
  930. MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
  931. MODULE_LICENSE("GPL v2");