core.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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/string.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/spi-mem.h>
  20. static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)
  21. {
  22. struct spi_mem_op op = SPINAND_GET_FEATURE_OP(reg,
  23. spinand->scratchbuf);
  24. int ret;
  25. ret = spi_mem_exec_op(spinand->spimem, &op);
  26. if (ret)
  27. return ret;
  28. *val = *spinand->scratchbuf;
  29. return 0;
  30. }
  31. int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val)
  32. {
  33. struct spi_mem_op op = SPINAND_SET_FEATURE_OP(reg,
  34. spinand->scratchbuf);
  35. *spinand->scratchbuf = val;
  36. return spi_mem_exec_op(spinand->spimem, &op);
  37. }
  38. static int spinand_read_status(struct spinand_device *spinand, u8 *status)
  39. {
  40. return spinand_read_reg_op(spinand, REG_STATUS, status);
  41. }
  42. static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg)
  43. {
  44. struct nand_device *nand = spinand_to_nand(spinand);
  45. if (WARN_ON(spinand->cur_target < 0 ||
  46. spinand->cur_target >= nand->memorg.ntargets))
  47. return -EINVAL;
  48. *cfg = spinand->cfg_cache[spinand->cur_target];
  49. return 0;
  50. }
  51. static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg)
  52. {
  53. struct nand_device *nand = spinand_to_nand(spinand);
  54. int ret;
  55. if (WARN_ON(spinand->cur_target < 0 ||
  56. spinand->cur_target >= nand->memorg.ntargets))
  57. return -EINVAL;
  58. if (spinand->cfg_cache[spinand->cur_target] == cfg)
  59. return 0;
  60. ret = spinand_write_reg_op(spinand, REG_CFG, cfg);
  61. if (ret)
  62. return ret;
  63. spinand->cfg_cache[spinand->cur_target] = cfg;
  64. return 0;
  65. }
  66. /**
  67. * spinand_upd_cfg() - Update the configuration register
  68. * @spinand: the spinand device
  69. * @mask: the mask encoding the bits to update in the config reg
  70. * @val: the new value to apply
  71. *
  72. * Update the configuration register.
  73. *
  74. * Return: 0 on success, a negative error code otherwise.
  75. */
  76. int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val)
  77. {
  78. int ret;
  79. u8 cfg;
  80. ret = spinand_get_cfg(spinand, &cfg);
  81. if (ret)
  82. return ret;
  83. cfg &= ~mask;
  84. cfg |= val;
  85. return spinand_set_cfg(spinand, cfg);
  86. }
  87. /**
  88. * spinand_select_target() - Select a specific NAND target/die
  89. * @spinand: the spinand device
  90. * @target: the target/die to select
  91. *
  92. * Select a new target/die. If chip only has one die, this function is a NOOP.
  93. *
  94. * Return: 0 on success, a negative error code otherwise.
  95. */
  96. int spinand_select_target(struct spinand_device *spinand, unsigned int target)
  97. {
  98. struct nand_device *nand = spinand_to_nand(spinand);
  99. int ret;
  100. if (WARN_ON(target >= nand->memorg.ntargets))
  101. return -EINVAL;
  102. if (spinand->cur_target == target)
  103. return 0;
  104. if (nand->memorg.ntargets == 1) {
  105. spinand->cur_target = target;
  106. return 0;
  107. }
  108. ret = spinand->select_target(spinand, target);
  109. if (ret)
  110. return ret;
  111. spinand->cur_target = target;
  112. return 0;
  113. }
  114. static int spinand_read_cfg(struct spinand_device *spinand)
  115. {
  116. struct nand_device *nand = spinand_to_nand(spinand);
  117. unsigned int target;
  118. int ret;
  119. for (target = 0; target < nand->memorg.ntargets; target++) {
  120. ret = spinand_select_target(spinand, target);
  121. if (ret)
  122. return ret;
  123. /*
  124. * We use spinand_read_reg_op() instead of spinand_get_cfg()
  125. * here to bypass the config cache.
  126. */
  127. ret = spinand_read_reg_op(spinand, REG_CFG,
  128. &spinand->cfg_cache[target]);
  129. if (ret)
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static int spinand_init_cfg_cache(struct spinand_device *spinand)
  135. {
  136. struct nand_device *nand = spinand_to_nand(spinand);
  137. struct device *dev = &spinand->spimem->spi->dev;
  138. spinand->cfg_cache = devm_kcalloc(dev,
  139. nand->memorg.ntargets,
  140. sizeof(*spinand->cfg_cache),
  141. GFP_KERNEL);
  142. if (!spinand->cfg_cache)
  143. return -ENOMEM;
  144. return 0;
  145. }
  146. static int spinand_init_quad_enable(struct spinand_device *spinand)
  147. {
  148. bool enable = false;
  149. if (!(spinand->flags & SPINAND_HAS_QE_BIT))
  150. return 0;
  151. if (spinand->op_templates.read_cache->data.buswidth == 4 ||
  152. spinand->op_templates.write_cache->data.buswidth == 4 ||
  153. spinand->op_templates.update_cache->data.buswidth == 4)
  154. enable = true;
  155. return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE,
  156. enable ? CFG_QUAD_ENABLE : 0);
  157. }
  158. static int spinand_ecc_enable(struct spinand_device *spinand,
  159. bool enable)
  160. {
  161. return spinand_upd_cfg(spinand, CFG_ECC_ENABLE,
  162. enable ? CFG_ECC_ENABLE : 0);
  163. }
  164. static int spinand_cont_read_enable(struct spinand_device *spinand,
  165. bool enable)
  166. {
  167. return spinand->set_cont_read(spinand, enable);
  168. }
  169. static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
  170. {
  171. struct nand_device *nand = spinand_to_nand(spinand);
  172. if (spinand->eccinfo.get_status)
  173. return spinand->eccinfo.get_status(spinand, status);
  174. switch (status & STATUS_ECC_MASK) {
  175. case STATUS_ECC_NO_BITFLIPS:
  176. return 0;
  177. case STATUS_ECC_HAS_BITFLIPS:
  178. /*
  179. * We have no way to know exactly how many bitflips have been
  180. * fixed, so let's return the maximum possible value so that
  181. * wear-leveling layers move the data immediately.
  182. */
  183. return nanddev_get_ecc_conf(nand)->strength;
  184. case STATUS_ECC_UNCOR_ERROR:
  185. return -EBADMSG;
  186. default:
  187. break;
  188. }
  189. return -EINVAL;
  190. }
  191. static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
  192. struct mtd_oob_region *region)
  193. {
  194. return -ERANGE;
  195. }
  196. static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
  197. struct mtd_oob_region *region)
  198. {
  199. if (section)
  200. return -ERANGE;
  201. /* Reserve 2 bytes for the BBM. */
  202. region->offset = 2;
  203. region->length = 62;
  204. return 0;
  205. }
  206. static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
  207. .ecc = spinand_noecc_ooblayout_ecc,
  208. .free = spinand_noecc_ooblayout_free,
  209. };
  210. static int spinand_ondie_ecc_init_ctx(struct nand_device *nand)
  211. {
  212. struct spinand_device *spinand = nand_to_spinand(nand);
  213. struct mtd_info *mtd = nanddev_to_mtd(nand);
  214. struct spinand_ondie_ecc_conf *engine_conf;
  215. nand->ecc.ctx.conf.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE;
  216. nand->ecc.ctx.conf.step_size = nand->ecc.requirements.step_size;
  217. nand->ecc.ctx.conf.strength = nand->ecc.requirements.strength;
  218. engine_conf = kzalloc(sizeof(*engine_conf), GFP_KERNEL);
  219. if (!engine_conf)
  220. return -ENOMEM;
  221. nand->ecc.ctx.priv = engine_conf;
  222. if (spinand->eccinfo.ooblayout)
  223. mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
  224. else
  225. mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
  226. return 0;
  227. }
  228. static void spinand_ondie_ecc_cleanup_ctx(struct nand_device *nand)
  229. {
  230. kfree(nand->ecc.ctx.priv);
  231. }
  232. static int spinand_ondie_ecc_prepare_io_req(struct nand_device *nand,
  233. struct nand_page_io_req *req)
  234. {
  235. struct spinand_device *spinand = nand_to_spinand(nand);
  236. bool enable = (req->mode != MTD_OPS_RAW);
  237. memset(spinand->oobbuf, 0xff, nanddev_per_page_oobsize(nand));
  238. /* Only enable or disable the engine */
  239. return spinand_ecc_enable(spinand, enable);
  240. }
  241. static int spinand_ondie_ecc_finish_io_req(struct nand_device *nand,
  242. struct nand_page_io_req *req)
  243. {
  244. struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv;
  245. struct spinand_device *spinand = nand_to_spinand(nand);
  246. struct mtd_info *mtd = spinand_to_mtd(spinand);
  247. int ret;
  248. if (req->mode == MTD_OPS_RAW)
  249. return 0;
  250. /* Nothing to do when finishing a page write */
  251. if (req->type == NAND_PAGE_WRITE)
  252. return 0;
  253. /* Finish a page read: check the status, report errors/bitflips */
  254. ret = spinand_check_ecc_status(spinand, engine_conf->status);
  255. if (ret == -EBADMSG) {
  256. mtd->ecc_stats.failed++;
  257. } else if (ret > 0) {
  258. unsigned int pages;
  259. /*
  260. * Continuous reads don't allow us to get the detail,
  261. * so we may exagerate the actual number of corrected bitflips.
  262. */
  263. if (!req->continuous)
  264. pages = 1;
  265. else
  266. pages = req->datalen / nanddev_page_size(nand);
  267. mtd->ecc_stats.corrected += ret * pages;
  268. }
  269. return ret;
  270. }
  271. static struct nand_ecc_engine_ops spinand_ondie_ecc_engine_ops = {
  272. .init_ctx = spinand_ondie_ecc_init_ctx,
  273. .cleanup_ctx = spinand_ondie_ecc_cleanup_ctx,
  274. .prepare_io_req = spinand_ondie_ecc_prepare_io_req,
  275. .finish_io_req = spinand_ondie_ecc_finish_io_req,
  276. };
  277. static struct nand_ecc_engine spinand_ondie_ecc_engine = {
  278. .ops = &spinand_ondie_ecc_engine_ops,
  279. };
  280. static void spinand_ondie_ecc_save_status(struct nand_device *nand, u8 status)
  281. {
  282. struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv;
  283. if (nand->ecc.ctx.conf.engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE &&
  284. engine_conf)
  285. engine_conf->status = status;
  286. }
  287. static int spinand_write_enable_op(struct spinand_device *spinand)
  288. {
  289. struct spi_mem_op op = SPINAND_WR_EN_DIS_OP(true);
  290. return spi_mem_exec_op(spinand->spimem, &op);
  291. }
  292. static int spinand_load_page_op(struct spinand_device *spinand,
  293. const struct nand_page_io_req *req)
  294. {
  295. struct nand_device *nand = spinand_to_nand(spinand);
  296. unsigned int row = nanddev_pos_to_row(nand, &req->pos);
  297. struct spi_mem_op op = SPINAND_PAGE_READ_OP(row);
  298. return spi_mem_exec_op(spinand->spimem, &op);
  299. }
  300. static int spinand_read_from_cache_op(struct spinand_device *spinand,
  301. const struct nand_page_io_req *req)
  302. {
  303. struct nand_device *nand = spinand_to_nand(spinand);
  304. struct mtd_info *mtd = spinand_to_mtd(spinand);
  305. struct spi_mem_dirmap_desc *rdesc;
  306. unsigned int nbytes = 0;
  307. void *buf = NULL;
  308. u16 column = 0;
  309. ssize_t ret;
  310. if (req->datalen) {
  311. buf = spinand->databuf;
  312. if (!req->continuous)
  313. nbytes = nanddev_page_size(nand);
  314. else
  315. nbytes = round_up(req->dataoffs + req->datalen,
  316. nanddev_page_size(nand));
  317. column = 0;
  318. }
  319. if (req->ooblen) {
  320. nbytes += nanddev_per_page_oobsize(nand);
  321. if (!buf) {
  322. buf = spinand->oobbuf;
  323. column = nanddev_page_size(nand);
  324. }
  325. }
  326. if (req->mode == MTD_OPS_RAW)
  327. rdesc = spinand->dirmaps[req->pos.plane].rdesc;
  328. else
  329. rdesc = spinand->dirmaps[req->pos.plane].rdesc_ecc;
  330. if (spinand->flags & SPINAND_HAS_READ_PLANE_SELECT_BIT)
  331. column |= req->pos.plane << fls(nanddev_page_size(nand));
  332. while (nbytes) {
  333. ret = spi_mem_dirmap_read(rdesc, column, nbytes, buf);
  334. if (ret < 0)
  335. return ret;
  336. if (!ret || ret > nbytes)
  337. return -EIO;
  338. nbytes -= ret;
  339. column += ret;
  340. buf += ret;
  341. /*
  342. * Dirmap accesses are allowed to toggle the CS.
  343. * Toggling the CS during a continuous read is forbidden.
  344. */
  345. if (nbytes && req->continuous)
  346. return -EIO;
  347. }
  348. if (req->datalen)
  349. memcpy(req->databuf.in, spinand->databuf + req->dataoffs,
  350. req->datalen);
  351. if (req->ooblen) {
  352. if (req->mode == MTD_OPS_AUTO_OOB)
  353. mtd_ooblayout_get_databytes(mtd, req->oobbuf.in,
  354. spinand->oobbuf,
  355. req->ooboffs,
  356. req->ooblen);
  357. else
  358. memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
  359. req->ooblen);
  360. }
  361. return 0;
  362. }
  363. static int spinand_write_to_cache_op(struct spinand_device *spinand,
  364. const struct nand_page_io_req *req)
  365. {
  366. struct nand_device *nand = spinand_to_nand(spinand);
  367. struct mtd_info *mtd = spinand_to_mtd(spinand);
  368. struct spi_mem_dirmap_desc *wdesc;
  369. unsigned int nbytes, column = 0;
  370. void *buf = spinand->databuf;
  371. ssize_t ret;
  372. /*
  373. * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset
  374. * the cache content to 0xFF (depends on vendor implementation), so we
  375. * must fill the page cache entirely even if we only want to program
  376. * the data portion of the page, otherwise we might corrupt the BBM or
  377. * user data previously programmed in OOB area.
  378. *
  379. * Only reset the data buffer manually, the OOB buffer is prepared by
  380. * ECC engines ->prepare_io_req() callback.
  381. */
  382. nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
  383. memset(spinand->databuf, 0xff, nanddev_page_size(nand));
  384. if (req->datalen)
  385. memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
  386. req->datalen);
  387. if (req->ooblen) {
  388. if (req->mode == MTD_OPS_AUTO_OOB)
  389. mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
  390. spinand->oobbuf,
  391. req->ooboffs,
  392. req->ooblen);
  393. else
  394. memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
  395. req->ooblen);
  396. }
  397. if (req->mode == MTD_OPS_RAW)
  398. wdesc = spinand->dirmaps[req->pos.plane].wdesc;
  399. else
  400. wdesc = spinand->dirmaps[req->pos.plane].wdesc_ecc;
  401. if (spinand->flags & SPINAND_HAS_PROG_PLANE_SELECT_BIT)
  402. column |= req->pos.plane << fls(nanddev_page_size(nand));
  403. while (nbytes) {
  404. ret = spi_mem_dirmap_write(wdesc, column, nbytes, buf);
  405. if (ret < 0)
  406. return ret;
  407. if (!ret || ret > nbytes)
  408. return -EIO;
  409. nbytes -= ret;
  410. column += ret;
  411. buf += ret;
  412. }
  413. return 0;
  414. }
  415. static int spinand_program_op(struct spinand_device *spinand,
  416. const struct nand_page_io_req *req)
  417. {
  418. struct nand_device *nand = spinand_to_nand(spinand);
  419. unsigned int row = nanddev_pos_to_row(nand, &req->pos);
  420. struct spi_mem_op op = SPINAND_PROG_EXEC_OP(row);
  421. return spi_mem_exec_op(spinand->spimem, &op);
  422. }
  423. static int spinand_erase_op(struct spinand_device *spinand,
  424. const struct nand_pos *pos)
  425. {
  426. struct nand_device *nand = spinand_to_nand(spinand);
  427. unsigned int row = nanddev_pos_to_row(nand, pos);
  428. struct spi_mem_op op = SPINAND_BLK_ERASE_OP(row);
  429. return spi_mem_exec_op(spinand->spimem, &op);
  430. }
  431. static int spinand_wait(struct spinand_device *spinand,
  432. unsigned long initial_delay_us,
  433. unsigned long poll_delay_us,
  434. u8 *s)
  435. {
  436. struct spi_mem_op op = SPINAND_GET_FEATURE_OP(REG_STATUS,
  437. spinand->scratchbuf);
  438. u8 status;
  439. int ret;
  440. ret = spi_mem_poll_status(spinand->spimem, &op, STATUS_BUSY, 0,
  441. initial_delay_us,
  442. poll_delay_us,
  443. SPINAND_WAITRDY_TIMEOUT_MS);
  444. if (ret)
  445. return ret;
  446. status = *spinand->scratchbuf;
  447. if (!(status & STATUS_BUSY))
  448. goto out;
  449. /*
  450. * Extra read, just in case the STATUS_READY bit has changed
  451. * since our last check
  452. */
  453. ret = spinand_read_status(spinand, &status);
  454. if (ret)
  455. return ret;
  456. out:
  457. if (s)
  458. *s = status;
  459. return status & STATUS_BUSY ? -ETIMEDOUT : 0;
  460. }
  461. static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr,
  462. u8 ndummy, u8 *buf)
  463. {
  464. struct spi_mem_op op = SPINAND_READID_OP(
  465. naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
  466. int ret;
  467. ret = spi_mem_exec_op(spinand->spimem, &op);
  468. if (!ret)
  469. memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
  470. return ret;
  471. }
  472. static int spinand_reset_op(struct spinand_device *spinand)
  473. {
  474. struct spi_mem_op op = SPINAND_RESET_OP;
  475. int ret;
  476. ret = spi_mem_exec_op(spinand->spimem, &op);
  477. if (ret)
  478. return ret;
  479. return spinand_wait(spinand,
  480. SPINAND_RESET_INITIAL_DELAY_US,
  481. SPINAND_RESET_POLL_DELAY_US,
  482. NULL);
  483. }
  484. static int spinand_lock_block(struct spinand_device *spinand, u8 lock)
  485. {
  486. return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock);
  487. }
  488. static int spinand_read_page(struct spinand_device *spinand,
  489. const struct nand_page_io_req *req)
  490. {
  491. struct nand_device *nand = spinand_to_nand(spinand);
  492. u8 status;
  493. int ret;
  494. ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req);
  495. if (ret)
  496. return ret;
  497. ret = spinand_load_page_op(spinand, req);
  498. if (ret)
  499. return ret;
  500. ret = spinand_wait(spinand,
  501. SPINAND_READ_INITIAL_DELAY_US,
  502. SPINAND_READ_POLL_DELAY_US,
  503. &status);
  504. if (ret < 0)
  505. return ret;
  506. spinand_ondie_ecc_save_status(nand, status);
  507. ret = spinand_read_from_cache_op(spinand, req);
  508. if (ret)
  509. return ret;
  510. return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
  511. }
  512. static int spinand_write_page(struct spinand_device *spinand,
  513. const struct nand_page_io_req *req)
  514. {
  515. struct nand_device *nand = spinand_to_nand(spinand);
  516. u8 status;
  517. int ret;
  518. ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req);
  519. if (ret)
  520. return ret;
  521. ret = spinand_write_enable_op(spinand);
  522. if (ret)
  523. return ret;
  524. ret = spinand_write_to_cache_op(spinand, req);
  525. if (ret)
  526. return ret;
  527. ret = spinand_program_op(spinand, req);
  528. if (ret)
  529. return ret;
  530. ret = spinand_wait(spinand,
  531. SPINAND_WRITE_INITIAL_DELAY_US,
  532. SPINAND_WRITE_POLL_DELAY_US,
  533. &status);
  534. if (!ret && (status & STATUS_PROG_FAILED))
  535. return -EIO;
  536. return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
  537. }
  538. static int spinand_mtd_regular_page_read(struct mtd_info *mtd, loff_t from,
  539. struct mtd_oob_ops *ops,
  540. unsigned int *max_bitflips)
  541. {
  542. struct spinand_device *spinand = mtd_to_spinand(mtd);
  543. struct nand_device *nand = mtd_to_nanddev(mtd);
  544. struct nand_io_iter iter;
  545. bool disable_ecc = false;
  546. bool ecc_failed = false;
  547. int ret;
  548. if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
  549. disable_ecc = true;
  550. nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) {
  551. if (disable_ecc)
  552. iter.req.mode = MTD_OPS_RAW;
  553. ret = spinand_select_target(spinand, iter.req.pos.target);
  554. if (ret)
  555. break;
  556. ret = spinand_read_page(spinand, &iter.req);
  557. if (ret < 0 && ret != -EBADMSG)
  558. break;
  559. if (ret == -EBADMSG)
  560. ecc_failed = true;
  561. else
  562. *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
  563. ret = 0;
  564. ops->retlen += iter.req.datalen;
  565. ops->oobretlen += iter.req.ooblen;
  566. }
  567. if (ecc_failed && !ret)
  568. ret = -EBADMSG;
  569. return ret;
  570. }
  571. static int spinand_mtd_continuous_page_read(struct mtd_info *mtd, loff_t from,
  572. struct mtd_oob_ops *ops,
  573. unsigned int *max_bitflips)
  574. {
  575. struct spinand_device *spinand = mtd_to_spinand(mtd);
  576. struct nand_device *nand = mtd_to_nanddev(mtd);
  577. struct nand_io_iter iter;
  578. u8 status;
  579. int ret;
  580. ret = spinand_cont_read_enable(spinand, true);
  581. if (ret)
  582. return ret;
  583. /*
  584. * The cache is divided into two halves. While one half of the cache has
  585. * the requested data, the other half is loaded with the next chunk of data.
  586. * Therefore, the host can read out the data continuously from page to page.
  587. * Each data read must be a multiple of 4-bytes and full pages should be read;
  588. * otherwise, the data output might get out of sequence from one read command
  589. * to another.
  590. */
  591. nanddev_io_for_each_block(nand, NAND_PAGE_READ, from, ops, &iter) {
  592. ret = spinand_select_target(spinand, iter.req.pos.target);
  593. if (ret)
  594. goto end_cont_read;
  595. ret = nand_ecc_prepare_io_req(nand, &iter.req);
  596. if (ret)
  597. goto end_cont_read;
  598. ret = spinand_load_page_op(spinand, &iter.req);
  599. if (ret)
  600. goto end_cont_read;
  601. ret = spinand_wait(spinand, SPINAND_READ_INITIAL_DELAY_US,
  602. SPINAND_READ_POLL_DELAY_US, NULL);
  603. if (ret < 0)
  604. goto end_cont_read;
  605. ret = spinand_read_from_cache_op(spinand, &iter.req);
  606. if (ret)
  607. goto end_cont_read;
  608. ops->retlen += iter.req.datalen;
  609. ret = spinand_read_status(spinand, &status);
  610. if (ret)
  611. goto end_cont_read;
  612. spinand_ondie_ecc_save_status(nand, status);
  613. ret = nand_ecc_finish_io_req(nand, &iter.req);
  614. if (ret < 0)
  615. goto end_cont_read;
  616. *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
  617. ret = 0;
  618. }
  619. end_cont_read:
  620. /*
  621. * Once all the data has been read out, the host can either pull CS#
  622. * high and wait for tRST or manually clear the bit in the configuration
  623. * register to terminate the continuous read operation. We have no
  624. * guarantee the SPI controller drivers will effectively deassert the CS
  625. * when we expect them to, so take the register based approach.
  626. */
  627. spinand_cont_read_enable(spinand, false);
  628. return ret;
  629. }
  630. static void spinand_cont_read_init(struct spinand_device *spinand)
  631. {
  632. struct nand_device *nand = spinand_to_nand(spinand);
  633. enum nand_ecc_engine_type engine_type = nand->ecc.ctx.conf.engine_type;
  634. /* OOBs cannot be retrieved so external/on-host ECC engine won't work */
  635. if (spinand->set_cont_read &&
  636. (engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE ||
  637. engine_type == NAND_ECC_ENGINE_TYPE_NONE)) {
  638. spinand->cont_read_possible = true;
  639. }
  640. }
  641. static bool spinand_use_cont_read(struct mtd_info *mtd, loff_t from,
  642. struct mtd_oob_ops *ops)
  643. {
  644. struct nand_device *nand = mtd_to_nanddev(mtd);
  645. struct spinand_device *spinand = nand_to_spinand(nand);
  646. struct nand_pos start_pos, end_pos;
  647. if (!spinand->cont_read_possible)
  648. return false;
  649. /* OOBs won't be retrieved */
  650. if (ops->ooblen || ops->oobbuf)
  651. return false;
  652. nanddev_offs_to_pos(nand, from, &start_pos);
  653. nanddev_offs_to_pos(nand, from + ops->len - 1, &end_pos);
  654. /*
  655. * Continuous reads never cross LUN boundaries. Some devices don't
  656. * support crossing planes boundaries. Some devices don't even support
  657. * crossing blocks boundaries. The common case being to read through UBI,
  658. * we will very rarely read two consequent blocks or more, so it is safer
  659. * and easier (can be improved) to only enable continuous reads when
  660. * reading within the same erase block.
  661. */
  662. if (start_pos.target != end_pos.target ||
  663. start_pos.plane != end_pos.plane ||
  664. start_pos.eraseblock != end_pos.eraseblock)
  665. return false;
  666. return start_pos.page < end_pos.page;
  667. }
  668. static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
  669. struct mtd_oob_ops *ops)
  670. {
  671. struct spinand_device *spinand = mtd_to_spinand(mtd);
  672. struct mtd_ecc_stats old_stats;
  673. unsigned int max_bitflips = 0;
  674. int ret;
  675. mutex_lock(&spinand->lock);
  676. old_stats = mtd->ecc_stats;
  677. if (spinand_use_cont_read(mtd, from, ops))
  678. ret = spinand_mtd_continuous_page_read(mtd, from, ops, &max_bitflips);
  679. else
  680. ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips);
  681. if (ops->stats) {
  682. ops->stats->uncorrectable_errors +=
  683. mtd->ecc_stats.failed - old_stats.failed;
  684. ops->stats->corrected_bitflips +=
  685. mtd->ecc_stats.corrected - old_stats.corrected;
  686. }
  687. mutex_unlock(&spinand->lock);
  688. return ret ? ret : max_bitflips;
  689. }
  690. static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
  691. struct mtd_oob_ops *ops)
  692. {
  693. struct spinand_device *spinand = mtd_to_spinand(mtd);
  694. struct nand_device *nand = mtd_to_nanddev(mtd);
  695. struct nand_io_iter iter;
  696. bool disable_ecc = false;
  697. int ret = 0;
  698. if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
  699. disable_ecc = true;
  700. mutex_lock(&spinand->lock);
  701. nanddev_io_for_each_page(nand, NAND_PAGE_WRITE, to, ops, &iter) {
  702. if (disable_ecc)
  703. iter.req.mode = MTD_OPS_RAW;
  704. ret = spinand_select_target(spinand, iter.req.pos.target);
  705. if (ret)
  706. break;
  707. ret = spinand_write_page(spinand, &iter.req);
  708. if (ret)
  709. break;
  710. ops->retlen += iter.req.datalen;
  711. ops->oobretlen += iter.req.ooblen;
  712. }
  713. mutex_unlock(&spinand->lock);
  714. return ret;
  715. }
  716. static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
  717. {
  718. struct spinand_device *spinand = nand_to_spinand(nand);
  719. u8 marker[2] = { };
  720. struct nand_page_io_req req = {
  721. .pos = *pos,
  722. .ooblen = sizeof(marker),
  723. .ooboffs = 0,
  724. .oobbuf.in = marker,
  725. .mode = MTD_OPS_RAW,
  726. };
  727. spinand_select_target(spinand, pos->target);
  728. spinand_read_page(spinand, &req);
  729. if (marker[0] != 0xff || marker[1] != 0xff)
  730. return true;
  731. return false;
  732. }
  733. static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
  734. {
  735. struct nand_device *nand = mtd_to_nanddev(mtd);
  736. struct spinand_device *spinand = nand_to_spinand(nand);
  737. struct nand_pos pos;
  738. int ret;
  739. nanddev_offs_to_pos(nand, offs, &pos);
  740. mutex_lock(&spinand->lock);
  741. ret = nanddev_isbad(nand, &pos);
  742. mutex_unlock(&spinand->lock);
  743. return ret;
  744. }
  745. static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
  746. {
  747. struct spinand_device *spinand = nand_to_spinand(nand);
  748. u8 marker[2] = { };
  749. struct nand_page_io_req req = {
  750. .pos = *pos,
  751. .ooboffs = 0,
  752. .ooblen = sizeof(marker),
  753. .oobbuf.out = marker,
  754. .mode = MTD_OPS_RAW,
  755. };
  756. int ret;
  757. ret = spinand_select_target(spinand, pos->target);
  758. if (ret)
  759. return ret;
  760. ret = spinand_write_enable_op(spinand);
  761. if (ret)
  762. return ret;
  763. return spinand_write_page(spinand, &req);
  764. }
  765. static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
  766. {
  767. struct nand_device *nand = mtd_to_nanddev(mtd);
  768. struct spinand_device *spinand = nand_to_spinand(nand);
  769. struct nand_pos pos;
  770. int ret;
  771. nanddev_offs_to_pos(nand, offs, &pos);
  772. mutex_lock(&spinand->lock);
  773. ret = nanddev_markbad(nand, &pos);
  774. mutex_unlock(&spinand->lock);
  775. return ret;
  776. }
  777. static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
  778. {
  779. struct spinand_device *spinand = nand_to_spinand(nand);
  780. u8 status;
  781. int ret;
  782. ret = spinand_select_target(spinand, pos->target);
  783. if (ret)
  784. return ret;
  785. ret = spinand_write_enable_op(spinand);
  786. if (ret)
  787. return ret;
  788. ret = spinand_erase_op(spinand, pos);
  789. if (ret)
  790. return ret;
  791. ret = spinand_wait(spinand,
  792. SPINAND_ERASE_INITIAL_DELAY_US,
  793. SPINAND_ERASE_POLL_DELAY_US,
  794. &status);
  795. if (!ret && (status & STATUS_ERASE_FAILED))
  796. ret = -EIO;
  797. return ret;
  798. }
  799. static int spinand_mtd_erase(struct mtd_info *mtd,
  800. struct erase_info *einfo)
  801. {
  802. struct spinand_device *spinand = mtd_to_spinand(mtd);
  803. int ret;
  804. mutex_lock(&spinand->lock);
  805. ret = nanddev_mtd_erase(mtd, einfo);
  806. mutex_unlock(&spinand->lock);
  807. return ret;
  808. }
  809. static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
  810. {
  811. struct spinand_device *spinand = mtd_to_spinand(mtd);
  812. struct nand_device *nand = mtd_to_nanddev(mtd);
  813. struct nand_pos pos;
  814. int ret;
  815. nanddev_offs_to_pos(nand, offs, &pos);
  816. mutex_lock(&spinand->lock);
  817. ret = nanddev_isreserved(nand, &pos);
  818. mutex_unlock(&spinand->lock);
  819. return ret;
  820. }
  821. static int spinand_create_dirmap(struct spinand_device *spinand,
  822. unsigned int plane)
  823. {
  824. struct nand_device *nand = spinand_to_nand(spinand);
  825. struct spi_mem_dirmap_info info = {
  826. .length = nanddev_page_size(nand) +
  827. nanddev_per_page_oobsize(nand),
  828. };
  829. struct spi_mem_dirmap_desc *desc;
  830. if (spinand->cont_read_possible)
  831. info.length = nanddev_eraseblock_size(nand);
  832. /* The plane number is passed in MSB just above the column address */
  833. info.offset = plane << fls(nand->memorg.pagesize);
  834. info.op_tmpl = *spinand->op_templates.update_cache;
  835. desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
  836. spinand->spimem, &info);
  837. if (IS_ERR(desc))
  838. return PTR_ERR(desc);
  839. spinand->dirmaps[plane].wdesc = desc;
  840. info.op_tmpl = *spinand->op_templates.read_cache;
  841. desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
  842. spinand->spimem, &info);
  843. if (IS_ERR(desc))
  844. return PTR_ERR(desc);
  845. spinand->dirmaps[plane].rdesc = desc;
  846. if (nand->ecc.engine->integration != NAND_ECC_ENGINE_INTEGRATION_PIPELINED) {
  847. spinand->dirmaps[plane].wdesc_ecc = spinand->dirmaps[plane].wdesc;
  848. spinand->dirmaps[plane].rdesc_ecc = spinand->dirmaps[plane].rdesc;
  849. return 0;
  850. }
  851. info.op_tmpl = *spinand->op_templates.update_cache;
  852. info.op_tmpl.data.ecc = true;
  853. desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
  854. spinand->spimem, &info);
  855. if (IS_ERR(desc))
  856. return PTR_ERR(desc);
  857. spinand->dirmaps[plane].wdesc_ecc = desc;
  858. info.op_tmpl = *spinand->op_templates.read_cache;
  859. info.op_tmpl.data.ecc = true;
  860. desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
  861. spinand->spimem, &info);
  862. if (IS_ERR(desc))
  863. return PTR_ERR(desc);
  864. spinand->dirmaps[plane].rdesc_ecc = desc;
  865. return 0;
  866. }
  867. static int spinand_create_dirmaps(struct spinand_device *spinand)
  868. {
  869. struct nand_device *nand = spinand_to_nand(spinand);
  870. int i, ret;
  871. spinand->dirmaps = devm_kzalloc(&spinand->spimem->spi->dev,
  872. sizeof(*spinand->dirmaps) *
  873. nand->memorg.planes_per_lun,
  874. GFP_KERNEL);
  875. if (!spinand->dirmaps)
  876. return -ENOMEM;
  877. for (i = 0; i < nand->memorg.planes_per_lun; i++) {
  878. ret = spinand_create_dirmap(spinand, i);
  879. if (ret)
  880. return ret;
  881. }
  882. return 0;
  883. }
  884. static const struct nand_ops spinand_ops = {
  885. .erase = spinand_erase,
  886. .markbad = spinand_markbad,
  887. .isbad = spinand_isbad,
  888. };
  889. static const struct spinand_manufacturer *spinand_manufacturers[] = {
  890. &alliancememory_spinand_manufacturer,
  891. &ato_spinand_manufacturer,
  892. &esmt_c8_spinand_manufacturer,
  893. &foresee_spinand_manufacturer,
  894. &gigadevice_spinand_manufacturer,
  895. &macronix_spinand_manufacturer,
  896. &micron_spinand_manufacturer,
  897. &paragon_spinand_manufacturer,
  898. &toshiba_spinand_manufacturer,
  899. &winbond_spinand_manufacturer,
  900. &xtx_spinand_manufacturer,
  901. };
  902. static int spinand_manufacturer_match(struct spinand_device *spinand,
  903. enum spinand_readid_method rdid_method)
  904. {
  905. u8 *id = spinand->id.data;
  906. unsigned int i;
  907. int ret;
  908. for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
  909. const struct spinand_manufacturer *manufacturer =
  910. spinand_manufacturers[i];
  911. if (id[0] != manufacturer->id)
  912. continue;
  913. ret = spinand_match_and_init(spinand,
  914. manufacturer->chips,
  915. manufacturer->nchips,
  916. rdid_method);
  917. if (ret < 0)
  918. continue;
  919. spinand->manufacturer = manufacturer;
  920. return 0;
  921. }
  922. return -EOPNOTSUPP;
  923. }
  924. static int spinand_id_detect(struct spinand_device *spinand)
  925. {
  926. u8 *id = spinand->id.data;
  927. int ret;
  928. ret = spinand_read_id_op(spinand, 0, 0, id);
  929. if (ret)
  930. return ret;
  931. ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE);
  932. if (!ret)
  933. return 0;
  934. ret = spinand_read_id_op(spinand, 1, 0, id);
  935. if (ret)
  936. return ret;
  937. ret = spinand_manufacturer_match(spinand,
  938. SPINAND_READID_METHOD_OPCODE_ADDR);
  939. if (!ret)
  940. return 0;
  941. ret = spinand_read_id_op(spinand, 0, 1, id);
  942. if (ret)
  943. return ret;
  944. ret = spinand_manufacturer_match(spinand,
  945. SPINAND_READID_METHOD_OPCODE_DUMMY);
  946. return ret;
  947. }
  948. static int spinand_manufacturer_init(struct spinand_device *spinand)
  949. {
  950. if (spinand->manufacturer->ops->init)
  951. return spinand->manufacturer->ops->init(spinand);
  952. return 0;
  953. }
  954. static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
  955. {
  956. /* Release manufacturer private data */
  957. if (spinand->manufacturer->ops->cleanup)
  958. return spinand->manufacturer->ops->cleanup(spinand);
  959. }
  960. static const struct spi_mem_op *
  961. spinand_select_op_variant(struct spinand_device *spinand,
  962. const struct spinand_op_variants *variants)
  963. {
  964. struct nand_device *nand = spinand_to_nand(spinand);
  965. unsigned int i;
  966. for (i = 0; i < variants->nops; i++) {
  967. struct spi_mem_op op = variants->ops[i];
  968. unsigned int nbytes;
  969. int ret;
  970. nbytes = nanddev_per_page_oobsize(nand) +
  971. nanddev_page_size(nand);
  972. while (nbytes) {
  973. op.data.nbytes = nbytes;
  974. ret = spi_mem_adjust_op_size(spinand->spimem, &op);
  975. if (ret)
  976. break;
  977. if (!spi_mem_supports_op(spinand->spimem, &op))
  978. break;
  979. nbytes -= op.data.nbytes;
  980. }
  981. if (!nbytes)
  982. return &variants->ops[i];
  983. }
  984. return NULL;
  985. }
  986. /**
  987. * spinand_match_and_init() - Try to find a match between a device ID and an
  988. * entry in a spinand_info table
  989. * @spinand: SPI NAND object
  990. * @table: SPI NAND device description table
  991. * @table_size: size of the device description table
  992. * @rdid_method: read id method to match
  993. *
  994. * Match between a device ID retrieved through the READ_ID command and an
  995. * entry in the SPI NAND description table. If a match is found, the spinand
  996. * object will be initialized with information provided by the matching
  997. * spinand_info entry.
  998. *
  999. * Return: 0 on success, a negative error code otherwise.
  1000. */
  1001. int spinand_match_and_init(struct spinand_device *spinand,
  1002. const struct spinand_info *table,
  1003. unsigned int table_size,
  1004. enum spinand_readid_method rdid_method)
  1005. {
  1006. u8 *id = spinand->id.data;
  1007. struct nand_device *nand = spinand_to_nand(spinand);
  1008. unsigned int i;
  1009. for (i = 0; i < table_size; i++) {
  1010. const struct spinand_info *info = &table[i];
  1011. const struct spi_mem_op *op;
  1012. if (rdid_method != info->devid.method)
  1013. continue;
  1014. if (memcmp(id + 1, info->devid.id, info->devid.len))
  1015. continue;
  1016. nand->memorg = table[i].memorg;
  1017. nanddev_set_ecc_requirements(nand, &table[i].eccreq);
  1018. spinand->eccinfo = table[i].eccinfo;
  1019. spinand->flags = table[i].flags;
  1020. spinand->id.len = 1 + table[i].devid.len;
  1021. spinand->select_target = table[i].select_target;
  1022. spinand->set_cont_read = table[i].set_cont_read;
  1023. op = spinand_select_op_variant(spinand,
  1024. info->op_variants.read_cache);
  1025. if (!op)
  1026. return -ENOTSUPP;
  1027. spinand->op_templates.read_cache = op;
  1028. op = spinand_select_op_variant(spinand,
  1029. info->op_variants.write_cache);
  1030. if (!op)
  1031. return -ENOTSUPP;
  1032. spinand->op_templates.write_cache = op;
  1033. op = spinand_select_op_variant(spinand,
  1034. info->op_variants.update_cache);
  1035. spinand->op_templates.update_cache = op;
  1036. return 0;
  1037. }
  1038. return -ENOTSUPP;
  1039. }
  1040. static int spinand_detect(struct spinand_device *spinand)
  1041. {
  1042. struct device *dev = &spinand->spimem->spi->dev;
  1043. struct nand_device *nand = spinand_to_nand(spinand);
  1044. int ret;
  1045. ret = spinand_reset_op(spinand);
  1046. if (ret)
  1047. return ret;
  1048. ret = spinand_id_detect(spinand);
  1049. if (ret) {
  1050. dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
  1051. spinand->id.data);
  1052. return ret;
  1053. }
  1054. if (nand->memorg.ntargets > 1 && !spinand->select_target) {
  1055. dev_err(dev,
  1056. "SPI NANDs with more than one die must implement ->select_target()\n");
  1057. return -EINVAL;
  1058. }
  1059. dev_info(&spinand->spimem->spi->dev,
  1060. "%s SPI NAND was found.\n", spinand->manufacturer->name);
  1061. dev_info(&spinand->spimem->spi->dev,
  1062. "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
  1063. nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
  1064. nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
  1065. return 0;
  1066. }
  1067. static int spinand_init_flash(struct spinand_device *spinand)
  1068. {
  1069. struct device *dev = &spinand->spimem->spi->dev;
  1070. struct nand_device *nand = spinand_to_nand(spinand);
  1071. int ret, i;
  1072. ret = spinand_read_cfg(spinand);
  1073. if (ret)
  1074. return ret;
  1075. ret = spinand_init_quad_enable(spinand);
  1076. if (ret)
  1077. return ret;
  1078. ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
  1079. if (ret)
  1080. return ret;
  1081. ret = spinand_manufacturer_init(spinand);
  1082. if (ret) {
  1083. dev_err(dev,
  1084. "Failed to initialize the SPI NAND chip (err = %d)\n",
  1085. ret);
  1086. return ret;
  1087. }
  1088. /* After power up, all blocks are locked, so unlock them here. */
  1089. for (i = 0; i < nand->memorg.ntargets; i++) {
  1090. ret = spinand_select_target(spinand, i);
  1091. if (ret)
  1092. break;
  1093. ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
  1094. if (ret)
  1095. break;
  1096. }
  1097. if (ret)
  1098. spinand_manufacturer_cleanup(spinand);
  1099. return ret;
  1100. }
  1101. static void spinand_mtd_resume(struct mtd_info *mtd)
  1102. {
  1103. struct spinand_device *spinand = mtd_to_spinand(mtd);
  1104. int ret;
  1105. ret = spinand_reset_op(spinand);
  1106. if (ret)
  1107. return;
  1108. ret = spinand_init_flash(spinand);
  1109. if (ret)
  1110. return;
  1111. spinand_ecc_enable(spinand, false);
  1112. }
  1113. static int spinand_init(struct spinand_device *spinand)
  1114. {
  1115. struct device *dev = &spinand->spimem->spi->dev;
  1116. struct mtd_info *mtd = spinand_to_mtd(spinand);
  1117. struct nand_device *nand = mtd_to_nanddev(mtd);
  1118. int ret;
  1119. /*
  1120. * We need a scratch buffer because the spi_mem interface requires that
  1121. * buf passed in spi_mem_op->data.buf be DMA-able.
  1122. */
  1123. spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
  1124. if (!spinand->scratchbuf)
  1125. return -ENOMEM;
  1126. ret = spinand_detect(spinand);
  1127. if (ret)
  1128. goto err_free_bufs;
  1129. /*
  1130. * Use kzalloc() instead of devm_kzalloc() here, because some drivers
  1131. * may use this buffer for DMA access.
  1132. * Memory allocated by devm_ does not guarantee DMA-safe alignment.
  1133. */
  1134. spinand->databuf = kzalloc(nanddev_eraseblock_size(nand),
  1135. GFP_KERNEL);
  1136. if (!spinand->databuf) {
  1137. ret = -ENOMEM;
  1138. goto err_free_bufs;
  1139. }
  1140. spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
  1141. ret = spinand_init_cfg_cache(spinand);
  1142. if (ret)
  1143. goto err_free_bufs;
  1144. ret = spinand_init_flash(spinand);
  1145. if (ret)
  1146. goto err_free_bufs;
  1147. ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
  1148. if (ret)
  1149. goto err_manuf_cleanup;
  1150. /* SPI-NAND default ECC engine is on-die */
  1151. nand->ecc.defaults.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE;
  1152. nand->ecc.ondie_engine = &spinand_ondie_ecc_engine;
  1153. spinand_ecc_enable(spinand, false);
  1154. ret = nanddev_ecc_engine_init(nand);
  1155. if (ret)
  1156. goto err_cleanup_nanddev;
  1157. /*
  1158. * Continuous read can only be enabled with an on-die ECC engine, so the
  1159. * ECC initialization must have happened previously.
  1160. */
  1161. spinand_cont_read_init(spinand);
  1162. mtd->_read_oob = spinand_mtd_read;
  1163. mtd->_write_oob = spinand_mtd_write;
  1164. mtd->_block_isbad = spinand_mtd_block_isbad;
  1165. mtd->_block_markbad = spinand_mtd_block_markbad;
  1166. mtd->_block_isreserved = spinand_mtd_block_isreserved;
  1167. mtd->_erase = spinand_mtd_erase;
  1168. mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
  1169. mtd->_resume = spinand_mtd_resume;
  1170. if (nand->ecc.engine) {
  1171. ret = mtd_ooblayout_count_freebytes(mtd);
  1172. if (ret < 0)
  1173. goto err_cleanup_ecc_engine;
  1174. }
  1175. mtd->oobavail = ret;
  1176. /* Propagate ECC information to mtd_info */
  1177. mtd->ecc_strength = nanddev_get_ecc_conf(nand)->strength;
  1178. mtd->ecc_step_size = nanddev_get_ecc_conf(nand)->step_size;
  1179. mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
  1180. ret = spinand_create_dirmaps(spinand);
  1181. if (ret) {
  1182. dev_err(dev,
  1183. "Failed to create direct mappings for read/write operations (err = %d)\n",
  1184. ret);
  1185. goto err_cleanup_ecc_engine;
  1186. }
  1187. return 0;
  1188. err_cleanup_ecc_engine:
  1189. nanddev_ecc_engine_cleanup(nand);
  1190. err_cleanup_nanddev:
  1191. nanddev_cleanup(nand);
  1192. err_manuf_cleanup:
  1193. spinand_manufacturer_cleanup(spinand);
  1194. err_free_bufs:
  1195. kfree(spinand->databuf);
  1196. kfree(spinand->scratchbuf);
  1197. return ret;
  1198. }
  1199. static void spinand_cleanup(struct spinand_device *spinand)
  1200. {
  1201. struct nand_device *nand = spinand_to_nand(spinand);
  1202. nanddev_cleanup(nand);
  1203. spinand_manufacturer_cleanup(spinand);
  1204. kfree(spinand->databuf);
  1205. kfree(spinand->scratchbuf);
  1206. }
  1207. static int spinand_probe(struct spi_mem *mem)
  1208. {
  1209. struct spinand_device *spinand;
  1210. struct mtd_info *mtd;
  1211. int ret;
  1212. spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
  1213. GFP_KERNEL);
  1214. if (!spinand)
  1215. return -ENOMEM;
  1216. spinand->spimem = mem;
  1217. spi_mem_set_drvdata(mem, spinand);
  1218. spinand_set_of_node(spinand, mem->spi->dev.of_node);
  1219. mutex_init(&spinand->lock);
  1220. mtd = spinand_to_mtd(spinand);
  1221. mtd->dev.parent = &mem->spi->dev;
  1222. ret = spinand_init(spinand);
  1223. if (ret)
  1224. return ret;
  1225. ret = mtd_device_register(mtd, NULL, 0);
  1226. if (ret)
  1227. goto err_spinand_cleanup;
  1228. return 0;
  1229. err_spinand_cleanup:
  1230. spinand_cleanup(spinand);
  1231. return ret;
  1232. }
  1233. static int spinand_remove(struct spi_mem *mem)
  1234. {
  1235. struct spinand_device *spinand;
  1236. struct mtd_info *mtd;
  1237. int ret;
  1238. spinand = spi_mem_get_drvdata(mem);
  1239. mtd = spinand_to_mtd(spinand);
  1240. ret = mtd_device_unregister(mtd);
  1241. if (ret)
  1242. return ret;
  1243. spinand_cleanup(spinand);
  1244. return 0;
  1245. }
  1246. static const struct spi_device_id spinand_ids[] = {
  1247. { .name = "spi-nand" },
  1248. { /* sentinel */ },
  1249. };
  1250. MODULE_DEVICE_TABLE(spi, spinand_ids);
  1251. #ifdef CONFIG_OF
  1252. static const struct of_device_id spinand_of_ids[] = {
  1253. { .compatible = "spi-nand" },
  1254. { /* sentinel */ },
  1255. };
  1256. MODULE_DEVICE_TABLE(of, spinand_of_ids);
  1257. #endif
  1258. static struct spi_mem_driver spinand_drv = {
  1259. .spidrv = {
  1260. .id_table = spinand_ids,
  1261. .driver = {
  1262. .name = "spi-nand",
  1263. .of_match_table = of_match_ptr(spinand_of_ids),
  1264. },
  1265. },
  1266. .probe = spinand_probe,
  1267. .remove = spinand_remove,
  1268. };
  1269. module_spi_mem_driver(spinand_drv);
  1270. MODULE_DESCRIPTION("SPI NAND framework");
  1271. MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
  1272. MODULE_LICENSE("GPL v2");