gpmi-nand.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale GPMI NAND Flash Driver
  4. *
  5. * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
  6. * Copyright (C) 2008 Embedded Alley Solutions, Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/mtd/partitions.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include "gpmi-nand.h"
  17. #include "bch-regs.h"
  18. /* Resource names for the GPMI NAND driver. */
  19. #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
  20. #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
  21. #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
  22. /* add our owner bbt descriptor */
  23. static uint8_t scan_ff_pattern[] = { 0xff };
  24. static struct nand_bbt_descr gpmi_bbt_descr = {
  25. .options = 0,
  26. .offs = 0,
  27. .len = 1,
  28. .pattern = scan_ff_pattern
  29. };
  30. /*
  31. * We may change the layout if we can get the ECC info from the datasheet,
  32. * else we will use all the (page + OOB).
  33. */
  34. static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
  35. struct mtd_oob_region *oobregion)
  36. {
  37. struct nand_chip *chip = mtd_to_nand(mtd);
  38. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  39. struct bch_geometry *geo = &this->bch_geometry;
  40. if (section)
  41. return -ERANGE;
  42. oobregion->offset = 0;
  43. oobregion->length = geo->page_size - mtd->writesize;
  44. return 0;
  45. }
  46. static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
  47. struct mtd_oob_region *oobregion)
  48. {
  49. struct nand_chip *chip = mtd_to_nand(mtd);
  50. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  51. struct bch_geometry *geo = &this->bch_geometry;
  52. if (section)
  53. return -ERANGE;
  54. /* The available oob size we have. */
  55. if (geo->page_size < mtd->writesize + mtd->oobsize) {
  56. oobregion->offset = geo->page_size - mtd->writesize;
  57. oobregion->length = mtd->oobsize - oobregion->offset;
  58. }
  59. return 0;
  60. }
  61. static const char * const gpmi_clks_for_mx2x[] = {
  62. "gpmi_io",
  63. };
  64. static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
  65. .ecc = gpmi_ooblayout_ecc,
  66. .free = gpmi_ooblayout_free,
  67. };
  68. static const struct gpmi_devdata gpmi_devdata_imx23 = {
  69. .type = IS_MX23,
  70. .bch_max_ecc_strength = 20,
  71. .max_chain_delay = 16000,
  72. .clks = gpmi_clks_for_mx2x,
  73. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
  74. };
  75. static const struct gpmi_devdata gpmi_devdata_imx28 = {
  76. .type = IS_MX28,
  77. .bch_max_ecc_strength = 20,
  78. .max_chain_delay = 16000,
  79. .clks = gpmi_clks_for_mx2x,
  80. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
  81. };
  82. static const char * const gpmi_clks_for_mx6[] = {
  83. "gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
  84. };
  85. static const struct gpmi_devdata gpmi_devdata_imx6q = {
  86. .type = IS_MX6Q,
  87. .bch_max_ecc_strength = 40,
  88. .max_chain_delay = 12000,
  89. .clks = gpmi_clks_for_mx6,
  90. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
  91. };
  92. static const struct gpmi_devdata gpmi_devdata_imx6sx = {
  93. .type = IS_MX6SX,
  94. .bch_max_ecc_strength = 62,
  95. .max_chain_delay = 12000,
  96. .clks = gpmi_clks_for_mx6,
  97. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
  98. };
  99. static const char * const gpmi_clks_for_mx7d[] = {
  100. "gpmi_io", "gpmi_bch_apb",
  101. };
  102. static const struct gpmi_devdata gpmi_devdata_imx7d = {
  103. .type = IS_MX7D,
  104. .bch_max_ecc_strength = 62,
  105. .max_chain_delay = 12000,
  106. .clks = gpmi_clks_for_mx7d,
  107. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d),
  108. };
  109. static irqreturn_t bch_irq(int irq, void *cookie)
  110. {
  111. struct gpmi_nand_data *this = cookie;
  112. gpmi_clear_bch(this);
  113. complete(&this->bch_done);
  114. return IRQ_HANDLED;
  115. }
  116. /*
  117. * Calculate the ECC strength by hand:
  118. * E : The ECC strength.
  119. * G : the length of Galois Field.
  120. * N : The chunk count of per page.
  121. * O : the oobsize of the NAND chip.
  122. * M : the metasize of per page.
  123. *
  124. * The formula is :
  125. * E * G * N
  126. * ------------ <= (O - M)
  127. * 8
  128. *
  129. * So, we get E by:
  130. * (O - M) * 8
  131. * E <= -------------
  132. * G * N
  133. */
  134. static inline int get_ecc_strength(struct gpmi_nand_data *this)
  135. {
  136. struct bch_geometry *geo = &this->bch_geometry;
  137. struct mtd_info *mtd = nand_to_mtd(&this->nand);
  138. int ecc_strength;
  139. ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
  140. / (geo->gf_len * geo->ecc_chunk_count);
  141. /* We need the minor even number. */
  142. return round_down(ecc_strength, 2);
  143. }
  144. static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
  145. {
  146. struct bch_geometry *geo = &this->bch_geometry;
  147. /* Do the sanity check. */
  148. if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
  149. /* The mx23/mx28 only support the GF13. */
  150. if (geo->gf_len == 14)
  151. return false;
  152. }
  153. return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
  154. }
  155. /*
  156. * If we can get the ECC information from the nand chip, we do not
  157. * need to calculate them ourselves.
  158. *
  159. * We may have available oob space in this case.
  160. */
  161. static int set_geometry_by_ecc_info(struct gpmi_nand_data *this,
  162. unsigned int ecc_strength,
  163. unsigned int ecc_step)
  164. {
  165. struct bch_geometry *geo = &this->bch_geometry;
  166. struct nand_chip *chip = &this->nand;
  167. struct mtd_info *mtd = nand_to_mtd(chip);
  168. unsigned int block_mark_bit_offset;
  169. switch (ecc_step) {
  170. case SZ_512:
  171. geo->gf_len = 13;
  172. break;
  173. case SZ_1K:
  174. geo->gf_len = 14;
  175. break;
  176. default:
  177. dev_err(this->dev,
  178. "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
  179. chip->ecc_strength_ds, chip->ecc_step_ds);
  180. return -EINVAL;
  181. }
  182. geo->ecc_chunk_size = ecc_step;
  183. geo->ecc_strength = round_up(ecc_strength, 2);
  184. if (!gpmi_check_ecc(this))
  185. return -EINVAL;
  186. /* Keep the C >= O */
  187. if (geo->ecc_chunk_size < mtd->oobsize) {
  188. dev_err(this->dev,
  189. "unsupported nand chip. ecc size: %d, oob size : %d\n",
  190. ecc_step, mtd->oobsize);
  191. return -EINVAL;
  192. }
  193. /* The default value, see comment in the legacy_set_geometry(). */
  194. geo->metadata_size = 10;
  195. geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
  196. /*
  197. * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
  198. *
  199. * | P |
  200. * |<----------------------------------------------------->|
  201. * | |
  202. * | (Block Mark) |
  203. * | P' | | | |
  204. * |<-------------------------------------------->| D | | O' |
  205. * | |<---->| |<--->|
  206. * V V V V V
  207. * +---+----------+-+----------+-+----------+-+----------+-+-----+
  208. * | M | data |E| data |E| data |E| data |E| |
  209. * +---+----------+-+----------+-+----------+-+----------+-+-----+
  210. * ^ ^
  211. * | O |
  212. * |<------------>|
  213. * | |
  214. *
  215. * P : the page size for BCH module.
  216. * E : The ECC strength.
  217. * G : the length of Galois Field.
  218. * N : The chunk count of per page.
  219. * M : the metasize of per page.
  220. * C : the ecc chunk size, aka the "data" above.
  221. * P': the nand chip's page size.
  222. * O : the nand chip's oob size.
  223. * O': the free oob.
  224. *
  225. * The formula for P is :
  226. *
  227. * E * G * N
  228. * P = ------------ + P' + M
  229. * 8
  230. *
  231. * The position of block mark moves forward in the ECC-based view
  232. * of page, and the delta is:
  233. *
  234. * E * G * (N - 1)
  235. * D = (---------------- + M)
  236. * 8
  237. *
  238. * Please see the comment in legacy_set_geometry().
  239. * With the condition C >= O , we still can get same result.
  240. * So the bit position of the physical block mark within the ECC-based
  241. * view of the page is :
  242. * (P' - D) * 8
  243. */
  244. geo->page_size = mtd->writesize + geo->metadata_size +
  245. (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
  246. geo->payload_size = mtd->writesize;
  247. geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
  248. geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
  249. + ALIGN(geo->ecc_chunk_count, 4);
  250. if (!this->swap_block_mark)
  251. return 0;
  252. /* For bit swap. */
  253. block_mark_bit_offset = mtd->writesize * 8 -
  254. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  255. + geo->metadata_size * 8);
  256. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  257. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  258. return 0;
  259. }
  260. static int legacy_set_geometry(struct gpmi_nand_data *this)
  261. {
  262. struct bch_geometry *geo = &this->bch_geometry;
  263. struct mtd_info *mtd = nand_to_mtd(&this->nand);
  264. unsigned int metadata_size;
  265. unsigned int status_size;
  266. unsigned int block_mark_bit_offset;
  267. /*
  268. * The size of the metadata can be changed, though we set it to 10
  269. * bytes now. But it can't be too large, because we have to save
  270. * enough space for BCH.
  271. */
  272. geo->metadata_size = 10;
  273. /* The default for the length of Galois Field. */
  274. geo->gf_len = 13;
  275. /* The default for chunk size. */
  276. geo->ecc_chunk_size = 512;
  277. while (geo->ecc_chunk_size < mtd->oobsize) {
  278. geo->ecc_chunk_size *= 2; /* keep C >= O */
  279. geo->gf_len = 14;
  280. }
  281. geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
  282. /* We use the same ECC strength for all chunks. */
  283. geo->ecc_strength = get_ecc_strength(this);
  284. if (!gpmi_check_ecc(this)) {
  285. dev_err(this->dev,
  286. "ecc strength: %d cannot be supported by the controller (%d)\n"
  287. "try to use minimum ecc strength that NAND chip required\n",
  288. geo->ecc_strength,
  289. this->devdata->bch_max_ecc_strength);
  290. return -EINVAL;
  291. }
  292. geo->page_size = mtd->writesize + geo->metadata_size +
  293. (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
  294. geo->payload_size = mtd->writesize;
  295. /*
  296. * The auxiliary buffer contains the metadata and the ECC status. The
  297. * metadata is padded to the nearest 32-bit boundary. The ECC status
  298. * contains one byte for every ECC chunk, and is also padded to the
  299. * nearest 32-bit boundary.
  300. */
  301. metadata_size = ALIGN(geo->metadata_size, 4);
  302. status_size = ALIGN(geo->ecc_chunk_count, 4);
  303. geo->auxiliary_size = metadata_size + status_size;
  304. geo->auxiliary_status_offset = metadata_size;
  305. if (!this->swap_block_mark)
  306. return 0;
  307. /*
  308. * We need to compute the byte and bit offsets of
  309. * the physical block mark within the ECC-based view of the page.
  310. *
  311. * NAND chip with 2K page shows below:
  312. * (Block Mark)
  313. * | |
  314. * | D |
  315. * |<---->|
  316. * V V
  317. * +---+----------+-+----------+-+----------+-+----------+-+
  318. * | M | data |E| data |E| data |E| data |E|
  319. * +---+----------+-+----------+-+----------+-+----------+-+
  320. *
  321. * The position of block mark moves forward in the ECC-based view
  322. * of page, and the delta is:
  323. *
  324. * E * G * (N - 1)
  325. * D = (---------------- + M)
  326. * 8
  327. *
  328. * With the formula to compute the ECC strength, and the condition
  329. * : C >= O (C is the ecc chunk size)
  330. *
  331. * It's easy to deduce to the following result:
  332. *
  333. * E * G (O - M) C - M C - M
  334. * ----------- <= ------- <= -------- < ---------
  335. * 8 N N (N - 1)
  336. *
  337. * So, we get:
  338. *
  339. * E * G * (N - 1)
  340. * D = (---------------- + M) < C
  341. * 8
  342. *
  343. * The above inequality means the position of block mark
  344. * within the ECC-based view of the page is still in the data chunk,
  345. * and it's NOT in the ECC bits of the chunk.
  346. *
  347. * Use the following to compute the bit position of the
  348. * physical block mark within the ECC-based view of the page:
  349. * (page_size - D) * 8
  350. *
  351. * --Huang Shijie
  352. */
  353. block_mark_bit_offset = mtd->writesize * 8 -
  354. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  355. + geo->metadata_size * 8);
  356. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  357. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  358. return 0;
  359. }
  360. int common_nfc_set_geometry(struct gpmi_nand_data *this)
  361. {
  362. struct nand_chip *chip = &this->nand;
  363. if (chip->ecc.strength > 0 && chip->ecc.size > 0)
  364. return set_geometry_by_ecc_info(this, chip->ecc.strength,
  365. chip->ecc.size);
  366. if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc"))
  367. || legacy_set_geometry(this)) {
  368. if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
  369. return -EINVAL;
  370. return set_geometry_by_ecc_info(this, chip->ecc_strength_ds,
  371. chip->ecc_step_ds);
  372. }
  373. return 0;
  374. }
  375. struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
  376. {
  377. /* We use the DMA channel 0 to access all the nand chips. */
  378. return this->dma_chans[0];
  379. }
  380. /* Can we use the upper's buffer directly for DMA? */
  381. bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf, int len,
  382. enum dma_data_direction dr)
  383. {
  384. struct scatterlist *sgl = &this->data_sgl;
  385. int ret;
  386. /* first try to map the upper buffer directly */
  387. if (virt_addr_valid(buf) && !object_is_on_stack(buf)) {
  388. sg_init_one(sgl, buf, len);
  389. ret = dma_map_sg(this->dev, sgl, 1, dr);
  390. if (ret == 0)
  391. goto map_fail;
  392. return true;
  393. }
  394. map_fail:
  395. /* We have to use our own DMA buffer. */
  396. sg_init_one(sgl, this->data_buffer_dma, len);
  397. if (dr == DMA_TO_DEVICE)
  398. memcpy(this->data_buffer_dma, buf, len);
  399. dma_map_sg(this->dev, sgl, 1, dr);
  400. return false;
  401. }
  402. /* This will be called after the DMA operation is finished. */
  403. static void dma_irq_callback(void *param)
  404. {
  405. struct gpmi_nand_data *this = param;
  406. struct completion *dma_c = &this->dma_done;
  407. complete(dma_c);
  408. }
  409. int start_dma_without_bch_irq(struct gpmi_nand_data *this,
  410. struct dma_async_tx_descriptor *desc)
  411. {
  412. struct completion *dma_c = &this->dma_done;
  413. unsigned long timeout;
  414. init_completion(dma_c);
  415. desc->callback = dma_irq_callback;
  416. desc->callback_param = this;
  417. dmaengine_submit(desc);
  418. dma_async_issue_pending(get_dma_chan(this));
  419. /* Wait for the interrupt from the DMA block. */
  420. timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
  421. if (!timeout) {
  422. dev_err(this->dev, "DMA timeout, last DMA\n");
  423. gpmi_dump_info(this);
  424. return -ETIMEDOUT;
  425. }
  426. return 0;
  427. }
  428. /*
  429. * This function is used in BCH reading or BCH writing pages.
  430. * It will wait for the BCH interrupt as long as ONE second.
  431. * Actually, we must wait for two interrupts :
  432. * [1] firstly the DMA interrupt and
  433. * [2] secondly the BCH interrupt.
  434. */
  435. int start_dma_with_bch_irq(struct gpmi_nand_data *this,
  436. struct dma_async_tx_descriptor *desc)
  437. {
  438. struct completion *bch_c = &this->bch_done;
  439. unsigned long timeout;
  440. /* Prepare to receive an interrupt from the BCH block. */
  441. init_completion(bch_c);
  442. /* start the DMA */
  443. start_dma_without_bch_irq(this, desc);
  444. /* Wait for the interrupt from the BCH block. */
  445. timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
  446. if (!timeout) {
  447. dev_err(this->dev, "BCH timeout\n");
  448. gpmi_dump_info(this);
  449. return -ETIMEDOUT;
  450. }
  451. return 0;
  452. }
  453. static int acquire_register_block(struct gpmi_nand_data *this,
  454. const char *res_name)
  455. {
  456. struct platform_device *pdev = this->pdev;
  457. struct resources *res = &this->resources;
  458. struct resource *r;
  459. void __iomem *p;
  460. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
  461. p = devm_ioremap_resource(&pdev->dev, r);
  462. if (IS_ERR(p))
  463. return PTR_ERR(p);
  464. if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
  465. res->gpmi_regs = p;
  466. else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
  467. res->bch_regs = p;
  468. else
  469. dev_err(this->dev, "unknown resource name : %s\n", res_name);
  470. return 0;
  471. }
  472. static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
  473. {
  474. struct platform_device *pdev = this->pdev;
  475. const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
  476. struct resource *r;
  477. int err;
  478. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
  479. if (!r) {
  480. dev_err(this->dev, "Can't get resource for %s\n", res_name);
  481. return -ENODEV;
  482. }
  483. err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
  484. if (err)
  485. dev_err(this->dev, "error requesting BCH IRQ\n");
  486. return err;
  487. }
  488. static void release_dma_channels(struct gpmi_nand_data *this)
  489. {
  490. unsigned int i;
  491. for (i = 0; i < DMA_CHANS; i++)
  492. if (this->dma_chans[i]) {
  493. dma_release_channel(this->dma_chans[i]);
  494. this->dma_chans[i] = NULL;
  495. }
  496. }
  497. static int acquire_dma_channels(struct gpmi_nand_data *this)
  498. {
  499. struct platform_device *pdev = this->pdev;
  500. struct dma_chan *dma_chan;
  501. /* request dma channel */
  502. dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
  503. if (!dma_chan) {
  504. dev_err(this->dev, "Failed to request DMA channel.\n");
  505. goto acquire_err;
  506. }
  507. this->dma_chans[0] = dma_chan;
  508. return 0;
  509. acquire_err:
  510. release_dma_channels(this);
  511. return -EINVAL;
  512. }
  513. static int gpmi_get_clks(struct gpmi_nand_data *this)
  514. {
  515. struct resources *r = &this->resources;
  516. struct clk *clk;
  517. int err, i;
  518. for (i = 0; i < this->devdata->clks_count; i++) {
  519. clk = devm_clk_get(this->dev, this->devdata->clks[i]);
  520. if (IS_ERR(clk)) {
  521. err = PTR_ERR(clk);
  522. goto err_clock;
  523. }
  524. r->clock[i] = clk;
  525. }
  526. if (GPMI_IS_MX6(this))
  527. /*
  528. * Set the default value for the gpmi clock.
  529. *
  530. * If you want to use the ONFI nand which is in the
  531. * Synchronous Mode, you should change the clock as you need.
  532. */
  533. clk_set_rate(r->clock[0], 22000000);
  534. return 0;
  535. err_clock:
  536. dev_dbg(this->dev, "failed in finding the clocks.\n");
  537. return err;
  538. }
  539. static int acquire_resources(struct gpmi_nand_data *this)
  540. {
  541. int ret;
  542. ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
  543. if (ret)
  544. goto exit_regs;
  545. ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
  546. if (ret)
  547. goto exit_regs;
  548. ret = acquire_bch_irq(this, bch_irq);
  549. if (ret)
  550. goto exit_regs;
  551. ret = acquire_dma_channels(this);
  552. if (ret)
  553. goto exit_regs;
  554. ret = gpmi_get_clks(this);
  555. if (ret)
  556. goto exit_clock;
  557. return 0;
  558. exit_clock:
  559. release_dma_channels(this);
  560. exit_regs:
  561. return ret;
  562. }
  563. static void release_resources(struct gpmi_nand_data *this)
  564. {
  565. release_dma_channels(this);
  566. }
  567. static int send_page_prepare(struct gpmi_nand_data *this,
  568. const void *source, unsigned length,
  569. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  570. const void **use_virt, dma_addr_t *use_phys)
  571. {
  572. struct device *dev = this->dev;
  573. if (virt_addr_valid(source)) {
  574. dma_addr_t source_phys;
  575. source_phys = dma_map_single(dev, (void *)source, length,
  576. DMA_TO_DEVICE);
  577. if (dma_mapping_error(dev, source_phys)) {
  578. if (alt_size < length) {
  579. dev_err(dev, "Alternate buffer is too small\n");
  580. return -ENOMEM;
  581. }
  582. goto map_failed;
  583. }
  584. *use_virt = source;
  585. *use_phys = source_phys;
  586. return 0;
  587. }
  588. map_failed:
  589. /*
  590. * Copy the content of the source buffer into the alternate
  591. * buffer and set up the return values accordingly.
  592. */
  593. memcpy(alt_virt, source, length);
  594. *use_virt = alt_virt;
  595. *use_phys = alt_phys;
  596. return 0;
  597. }
  598. static void send_page_end(struct gpmi_nand_data *this,
  599. const void *source, unsigned length,
  600. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  601. const void *used_virt, dma_addr_t used_phys)
  602. {
  603. struct device *dev = this->dev;
  604. if (used_virt == source)
  605. dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
  606. }
  607. static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
  608. {
  609. struct device *dev = this->dev;
  610. if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
  611. dma_free_coherent(dev, this->page_buffer_size,
  612. this->page_buffer_virt,
  613. this->page_buffer_phys);
  614. kfree(this->cmd_buffer);
  615. kfree(this->data_buffer_dma);
  616. kfree(this->raw_buffer);
  617. this->cmd_buffer = NULL;
  618. this->data_buffer_dma = NULL;
  619. this->raw_buffer = NULL;
  620. this->page_buffer_virt = NULL;
  621. this->page_buffer_size = 0;
  622. }
  623. /* Allocate the DMA buffers */
  624. static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
  625. {
  626. struct bch_geometry *geo = &this->bch_geometry;
  627. struct device *dev = this->dev;
  628. struct mtd_info *mtd = nand_to_mtd(&this->nand);
  629. /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
  630. this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
  631. if (this->cmd_buffer == NULL)
  632. goto error_alloc;
  633. /*
  634. * [2] Allocate a read/write data buffer.
  635. * The gpmi_alloc_dma_buffer can be called twice.
  636. * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
  637. * is called before the NAND identification; and we allocate a
  638. * buffer of the real NAND page size when the gpmi_alloc_dma_buffer
  639. * is called after.
  640. */
  641. this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
  642. GFP_DMA | GFP_KERNEL);
  643. if (this->data_buffer_dma == NULL)
  644. goto error_alloc;
  645. /*
  646. * [3] Allocate the page buffer.
  647. *
  648. * Both the payload buffer and the auxiliary buffer must appear on
  649. * 32-bit boundaries. We presume the size of the payload buffer is a
  650. * power of two and is much larger than four, which guarantees the
  651. * auxiliary buffer will appear on a 32-bit boundary.
  652. */
  653. this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
  654. this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
  655. &this->page_buffer_phys, GFP_DMA);
  656. if (!this->page_buffer_virt)
  657. goto error_alloc;
  658. this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
  659. if (!this->raw_buffer)
  660. goto error_alloc;
  661. /* Slice up the page buffer. */
  662. this->payload_virt = this->page_buffer_virt;
  663. this->payload_phys = this->page_buffer_phys;
  664. this->auxiliary_virt = this->payload_virt + geo->payload_size;
  665. this->auxiliary_phys = this->payload_phys + geo->payload_size;
  666. return 0;
  667. error_alloc:
  668. gpmi_free_dma_buffer(this);
  669. return -ENOMEM;
  670. }
  671. static void gpmi_cmd_ctrl(struct nand_chip *chip, int data, unsigned int ctrl)
  672. {
  673. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  674. int ret;
  675. /*
  676. * Every operation begins with a command byte and a series of zero or
  677. * more address bytes. These are distinguished by either the Address
  678. * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
  679. * asserted. When MTD is ready to execute the command, it will deassert
  680. * both latch enables.
  681. *
  682. * Rather than run a separate DMA operation for every single byte, we
  683. * queue them up and run a single DMA operation for the entire series
  684. * of command and data bytes. NAND_CMD_NONE means the END of the queue.
  685. */
  686. if ((ctrl & (NAND_ALE | NAND_CLE))) {
  687. if (data != NAND_CMD_NONE)
  688. this->cmd_buffer[this->command_length++] = data;
  689. return;
  690. }
  691. if (!this->command_length)
  692. return;
  693. ret = gpmi_send_command(this);
  694. if (ret)
  695. dev_err(this->dev, "Chip: %u, Error %d\n",
  696. this->current_chip, ret);
  697. this->command_length = 0;
  698. }
  699. static int gpmi_dev_ready(struct nand_chip *chip)
  700. {
  701. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  702. return gpmi_is_ready(this, this->current_chip);
  703. }
  704. static void gpmi_select_chip(struct nand_chip *chip, int chipnr)
  705. {
  706. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  707. int ret;
  708. /*
  709. * For power consumption matters, disable/enable the clock each time a
  710. * die is selected/unselected.
  711. */
  712. if (this->current_chip < 0 && chipnr >= 0) {
  713. ret = gpmi_enable_clk(this);
  714. if (ret)
  715. dev_err(this->dev, "Failed to enable the clock\n");
  716. } else if (this->current_chip >= 0 && chipnr < 0) {
  717. ret = gpmi_disable_clk(this);
  718. if (ret)
  719. dev_err(this->dev, "Failed to disable the clock\n");
  720. }
  721. /*
  722. * This driver currently supports only one NAND chip. Plus, dies share
  723. * the same configuration. So once timings have been applied on the
  724. * controller side, they will not change anymore. When the time will
  725. * come, the check on must_apply_timings will have to be dropped.
  726. */
  727. if (chipnr >= 0 && this->hw.must_apply_timings) {
  728. this->hw.must_apply_timings = false;
  729. gpmi_nfc_apply_timings(this);
  730. }
  731. this->current_chip = chipnr;
  732. }
  733. static void gpmi_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
  734. {
  735. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  736. dev_dbg(this->dev, "len is %d\n", len);
  737. gpmi_read_data(this, buf, len);
  738. }
  739. static void gpmi_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
  740. {
  741. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  742. dev_dbg(this->dev, "len is %d\n", len);
  743. gpmi_send_data(this, buf, len);
  744. }
  745. static uint8_t gpmi_read_byte(struct nand_chip *chip)
  746. {
  747. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  748. uint8_t *buf = this->data_buffer_dma;
  749. gpmi_read_buf(chip, buf, 1);
  750. return buf[0];
  751. }
  752. /*
  753. * Handles block mark swapping.
  754. * It can be called in swapping the block mark, or swapping it back,
  755. * because the the operations are the same.
  756. */
  757. static void block_mark_swapping(struct gpmi_nand_data *this,
  758. void *payload, void *auxiliary)
  759. {
  760. struct bch_geometry *nfc_geo = &this->bch_geometry;
  761. unsigned char *p;
  762. unsigned char *a;
  763. unsigned int bit;
  764. unsigned char mask;
  765. unsigned char from_data;
  766. unsigned char from_oob;
  767. if (!this->swap_block_mark)
  768. return;
  769. /*
  770. * If control arrives here, we're swapping. Make some convenience
  771. * variables.
  772. */
  773. bit = nfc_geo->block_mark_bit_offset;
  774. p = payload + nfc_geo->block_mark_byte_offset;
  775. a = auxiliary;
  776. /*
  777. * Get the byte from the data area that overlays the block mark. Since
  778. * the ECC engine applies its own view to the bits in the page, the
  779. * physical block mark won't (in general) appear on a byte boundary in
  780. * the data.
  781. */
  782. from_data = (p[0] >> bit) | (p[1] << (8 - bit));
  783. /* Get the byte from the OOB. */
  784. from_oob = a[0];
  785. /* Swap them. */
  786. a[0] = from_data;
  787. mask = (0x1 << bit) - 1;
  788. p[0] = (p[0] & mask) | (from_oob << bit);
  789. mask = ~0 << bit;
  790. p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
  791. }
  792. static int gpmi_ecc_read_page_data(struct nand_chip *chip,
  793. uint8_t *buf, int oob_required,
  794. int page)
  795. {
  796. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  797. struct bch_geometry *nfc_geo = &this->bch_geometry;
  798. struct mtd_info *mtd = nand_to_mtd(chip);
  799. dma_addr_t payload_phys;
  800. unsigned int i;
  801. unsigned char *status;
  802. unsigned int max_bitflips = 0;
  803. int ret;
  804. bool direct = false;
  805. dev_dbg(this->dev, "page number is : %d\n", page);
  806. payload_phys = this->payload_phys;
  807. if (virt_addr_valid(buf)) {
  808. dma_addr_t dest_phys;
  809. dest_phys = dma_map_single(this->dev, buf, nfc_geo->payload_size,
  810. DMA_FROM_DEVICE);
  811. if (!dma_mapping_error(this->dev, dest_phys)) {
  812. payload_phys = dest_phys;
  813. direct = true;
  814. }
  815. }
  816. /* go! */
  817. ret = gpmi_read_page(this, payload_phys, this->auxiliary_phys);
  818. if (direct)
  819. dma_unmap_single(this->dev, payload_phys, nfc_geo->payload_size,
  820. DMA_FROM_DEVICE);
  821. if (ret) {
  822. dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
  823. return ret;
  824. }
  825. /* Loop over status bytes, accumulating ECC status. */
  826. status = this->auxiliary_virt + nfc_geo->auxiliary_status_offset;
  827. if (!direct)
  828. memcpy(buf, this->payload_virt, nfc_geo->payload_size);
  829. for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
  830. if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
  831. continue;
  832. if (*status == STATUS_UNCORRECTABLE) {
  833. int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
  834. u8 *eccbuf = this->raw_buffer;
  835. int offset, bitoffset;
  836. int eccbytes;
  837. int flips;
  838. /* Read ECC bytes into our internal raw_buffer */
  839. offset = nfc_geo->metadata_size * 8;
  840. offset += ((8 * nfc_geo->ecc_chunk_size) + eccbits) * (i + 1);
  841. offset -= eccbits;
  842. bitoffset = offset % 8;
  843. eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
  844. offset /= 8;
  845. eccbytes -= offset;
  846. nand_change_read_column_op(chip, offset, eccbuf,
  847. eccbytes, false);
  848. /*
  849. * ECC data are not byte aligned and we may have
  850. * in-band data in the first and last byte of
  851. * eccbuf. Set non-eccbits to one so that
  852. * nand_check_erased_ecc_chunk() does not count them
  853. * as bitflips.
  854. */
  855. if (bitoffset)
  856. eccbuf[0] |= GENMASK(bitoffset - 1, 0);
  857. bitoffset = (bitoffset + eccbits) % 8;
  858. if (bitoffset)
  859. eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
  860. /*
  861. * The ECC hardware has an uncorrectable ECC status
  862. * code in case we have bitflips in an erased page. As
  863. * nothing was written into this subpage the ECC is
  864. * obviously wrong and we can not trust it. We assume
  865. * at this point that we are reading an erased page and
  866. * try to correct the bitflips in buffer up to
  867. * ecc_strength bitflips. If this is a page with random
  868. * data, we exceed this number of bitflips and have a
  869. * ECC failure. Otherwise we use the corrected buffer.
  870. */
  871. if (i == 0) {
  872. /* The first block includes metadata */
  873. flips = nand_check_erased_ecc_chunk(
  874. buf + i * nfc_geo->ecc_chunk_size,
  875. nfc_geo->ecc_chunk_size,
  876. eccbuf, eccbytes,
  877. this->auxiliary_virt,
  878. nfc_geo->metadata_size,
  879. nfc_geo->ecc_strength);
  880. } else {
  881. flips = nand_check_erased_ecc_chunk(
  882. buf + i * nfc_geo->ecc_chunk_size,
  883. nfc_geo->ecc_chunk_size,
  884. eccbuf, eccbytes,
  885. NULL, 0,
  886. nfc_geo->ecc_strength);
  887. }
  888. if (flips > 0) {
  889. max_bitflips = max_t(unsigned int, max_bitflips,
  890. flips);
  891. mtd->ecc_stats.corrected += flips;
  892. continue;
  893. }
  894. mtd->ecc_stats.failed++;
  895. continue;
  896. }
  897. mtd->ecc_stats.corrected += *status;
  898. max_bitflips = max_t(unsigned int, max_bitflips, *status);
  899. }
  900. /* handle the block mark swapping */
  901. block_mark_swapping(this, buf, this->auxiliary_virt);
  902. if (oob_required) {
  903. /*
  904. * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
  905. * for details about our policy for delivering the OOB.
  906. *
  907. * We fill the caller's buffer with set bits, and then copy the
  908. * block mark to th caller's buffer. Note that, if block mark
  909. * swapping was necessary, it has already been done, so we can
  910. * rely on the first byte of the auxiliary buffer to contain
  911. * the block mark.
  912. */
  913. memset(chip->oob_poi, ~0, mtd->oobsize);
  914. chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0];
  915. }
  916. return max_bitflips;
  917. }
  918. static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf,
  919. int oob_required, int page)
  920. {
  921. nand_read_page_op(chip, page, 0, NULL, 0);
  922. return gpmi_ecc_read_page_data(chip, buf, oob_required, page);
  923. }
  924. /* Fake a virtual small page for the subpage read */
  925. static int gpmi_ecc_read_subpage(struct nand_chip *chip, uint32_t offs,
  926. uint32_t len, uint8_t *buf, int page)
  927. {
  928. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  929. void __iomem *bch_regs = this->resources.bch_regs;
  930. struct bch_geometry old_geo = this->bch_geometry;
  931. struct bch_geometry *geo = &this->bch_geometry;
  932. int size = chip->ecc.size; /* ECC chunk size */
  933. int meta, n, page_size;
  934. u32 r1_old, r2_old, r1_new, r2_new;
  935. unsigned int max_bitflips;
  936. int first, last, marker_pos;
  937. int ecc_parity_size;
  938. int col = 0;
  939. int old_swap_block_mark = this->swap_block_mark;
  940. /* The size of ECC parity */
  941. ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
  942. /* Align it with the chunk size */
  943. first = offs / size;
  944. last = (offs + len - 1) / size;
  945. if (this->swap_block_mark) {
  946. /*
  947. * Find the chunk which contains the Block Marker.
  948. * If this chunk is in the range of [first, last],
  949. * we have to read out the whole page.
  950. * Why? since we had swapped the data at the position of Block
  951. * Marker to the metadata which is bound with the chunk 0.
  952. */
  953. marker_pos = geo->block_mark_byte_offset / size;
  954. if (last >= marker_pos && first <= marker_pos) {
  955. dev_dbg(this->dev,
  956. "page:%d, first:%d, last:%d, marker at:%d\n",
  957. page, first, last, marker_pos);
  958. return gpmi_ecc_read_page(chip, buf, 0, page);
  959. }
  960. }
  961. meta = geo->metadata_size;
  962. if (first) {
  963. col = meta + (size + ecc_parity_size) * first;
  964. meta = 0;
  965. buf = buf + first * size;
  966. }
  967. nand_read_page_op(chip, page, col, NULL, 0);
  968. /* Save the old environment */
  969. r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
  970. r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
  971. /* change the BCH registers and bch_geometry{} */
  972. n = last - first + 1;
  973. page_size = meta + (size + ecc_parity_size) * n;
  974. r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
  975. BM_BCH_FLASH0LAYOUT0_META_SIZE);
  976. r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
  977. | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
  978. writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
  979. r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
  980. r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
  981. writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
  982. geo->ecc_chunk_count = n;
  983. geo->payload_size = n * size;
  984. geo->page_size = page_size;
  985. geo->auxiliary_status_offset = ALIGN(meta, 4);
  986. dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
  987. page, offs, len, col, first, n, page_size);
  988. /* Read the subpage now */
  989. this->swap_block_mark = false;
  990. max_bitflips = gpmi_ecc_read_page_data(chip, buf, 0, page);
  991. /* Restore */
  992. writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
  993. writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
  994. this->bch_geometry = old_geo;
  995. this->swap_block_mark = old_swap_block_mark;
  996. return max_bitflips;
  997. }
  998. static int gpmi_ecc_write_page(struct nand_chip *chip, const uint8_t *buf,
  999. int oob_required, int page)
  1000. {
  1001. struct mtd_info *mtd = nand_to_mtd(chip);
  1002. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1003. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1004. const void *payload_virt;
  1005. dma_addr_t payload_phys;
  1006. const void *auxiliary_virt;
  1007. dma_addr_t auxiliary_phys;
  1008. int ret;
  1009. dev_dbg(this->dev, "ecc write page.\n");
  1010. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1011. if (this->swap_block_mark) {
  1012. /*
  1013. * If control arrives here, we're doing block mark swapping.
  1014. * Since we can't modify the caller's buffers, we must copy them
  1015. * into our own.
  1016. */
  1017. memcpy(this->payload_virt, buf, mtd->writesize);
  1018. payload_virt = this->payload_virt;
  1019. payload_phys = this->payload_phys;
  1020. memcpy(this->auxiliary_virt, chip->oob_poi,
  1021. nfc_geo->auxiliary_size);
  1022. auxiliary_virt = this->auxiliary_virt;
  1023. auxiliary_phys = this->auxiliary_phys;
  1024. /* Handle block mark swapping. */
  1025. block_mark_swapping(this,
  1026. (void *)payload_virt, (void *)auxiliary_virt);
  1027. } else {
  1028. /*
  1029. * If control arrives here, we're not doing block mark swapping,
  1030. * so we can to try and use the caller's buffers.
  1031. */
  1032. ret = send_page_prepare(this,
  1033. buf, mtd->writesize,
  1034. this->payload_virt, this->payload_phys,
  1035. nfc_geo->payload_size,
  1036. &payload_virt, &payload_phys);
  1037. if (ret) {
  1038. dev_err(this->dev, "Inadequate payload DMA buffer\n");
  1039. return 0;
  1040. }
  1041. ret = send_page_prepare(this,
  1042. chip->oob_poi, mtd->oobsize,
  1043. this->auxiliary_virt, this->auxiliary_phys,
  1044. nfc_geo->auxiliary_size,
  1045. &auxiliary_virt, &auxiliary_phys);
  1046. if (ret) {
  1047. dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
  1048. goto exit_auxiliary;
  1049. }
  1050. }
  1051. /* Ask the NFC. */
  1052. ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
  1053. if (ret)
  1054. dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
  1055. if (!this->swap_block_mark) {
  1056. send_page_end(this, chip->oob_poi, mtd->oobsize,
  1057. this->auxiliary_virt, this->auxiliary_phys,
  1058. nfc_geo->auxiliary_size,
  1059. auxiliary_virt, auxiliary_phys);
  1060. exit_auxiliary:
  1061. send_page_end(this, buf, mtd->writesize,
  1062. this->payload_virt, this->payload_phys,
  1063. nfc_geo->payload_size,
  1064. payload_virt, payload_phys);
  1065. }
  1066. if (ret)
  1067. return ret;
  1068. return nand_prog_page_end_op(chip);
  1069. }
  1070. /*
  1071. * There are several places in this driver where we have to handle the OOB and
  1072. * block marks. This is the function where things are the most complicated, so
  1073. * this is where we try to explain it all. All the other places refer back to
  1074. * here.
  1075. *
  1076. * These are the rules, in order of decreasing importance:
  1077. *
  1078. * 1) Nothing the caller does can be allowed to imperil the block mark.
  1079. *
  1080. * 2) In read operations, the first byte of the OOB we return must reflect the
  1081. * true state of the block mark, no matter where that block mark appears in
  1082. * the physical page.
  1083. *
  1084. * 3) ECC-based read operations return an OOB full of set bits (since we never
  1085. * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
  1086. * return).
  1087. *
  1088. * 4) "Raw" read operations return a direct view of the physical bytes in the
  1089. * page, using the conventional definition of which bytes are data and which
  1090. * are OOB. This gives the caller a way to see the actual, physical bytes
  1091. * in the page, without the distortions applied by our ECC engine.
  1092. *
  1093. *
  1094. * What we do for this specific read operation depends on two questions:
  1095. *
  1096. * 1) Are we doing a "raw" read, or an ECC-based read?
  1097. *
  1098. * 2) Are we using block mark swapping or transcription?
  1099. *
  1100. * There are four cases, illustrated by the following Karnaugh map:
  1101. *
  1102. * | Raw | ECC-based |
  1103. * -------------+-------------------------+-------------------------+
  1104. * | Read the conventional | |
  1105. * | OOB at the end of the | |
  1106. * Swapping | page and return it. It | |
  1107. * | contains exactly what | |
  1108. * | we want. | Read the block mark and |
  1109. * -------------+-------------------------+ return it in a buffer |
  1110. * | Read the conventional | full of set bits. |
  1111. * | OOB at the end of the | |
  1112. * | page and also the block | |
  1113. * Transcribing | mark in the metadata. | |
  1114. * | Copy the block mark | |
  1115. * | into the first byte of | |
  1116. * | the OOB. | |
  1117. * -------------+-------------------------+-------------------------+
  1118. *
  1119. * Note that we break rule #4 in the Transcribing/Raw case because we're not
  1120. * giving an accurate view of the actual, physical bytes in the page (we're
  1121. * overwriting the block mark). That's OK because it's more important to follow
  1122. * rule #2.
  1123. *
  1124. * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
  1125. * easy. When reading a page, for example, the NAND Flash MTD code calls our
  1126. * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
  1127. * ECC-based or raw view of the page is implicit in which function it calls
  1128. * (there is a similar pair of ECC-based/raw functions for writing).
  1129. */
  1130. static int gpmi_ecc_read_oob(struct nand_chip *chip, int page)
  1131. {
  1132. struct mtd_info *mtd = nand_to_mtd(chip);
  1133. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1134. dev_dbg(this->dev, "page number is %d\n", page);
  1135. /* clear the OOB buffer */
  1136. memset(chip->oob_poi, ~0, mtd->oobsize);
  1137. /* Read out the conventional OOB. */
  1138. nand_read_page_op(chip, page, mtd->writesize, NULL, 0);
  1139. chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize);
  1140. /*
  1141. * Now, we want to make sure the block mark is correct. In the
  1142. * non-transcribing case (!GPMI_IS_MX23()), we already have it.
  1143. * Otherwise, we need to explicitly read it.
  1144. */
  1145. if (GPMI_IS_MX23(this)) {
  1146. /* Read the block mark into the first byte of the OOB buffer. */
  1147. nand_read_page_op(chip, page, 0, NULL, 0);
  1148. chip->oob_poi[0] = chip->legacy.read_byte(chip);
  1149. }
  1150. return 0;
  1151. }
  1152. static int gpmi_ecc_write_oob(struct nand_chip *chip, int page)
  1153. {
  1154. struct mtd_info *mtd = nand_to_mtd(chip);
  1155. struct mtd_oob_region of = { };
  1156. /* Do we have available oob area? */
  1157. mtd_ooblayout_free(mtd, 0, &of);
  1158. if (!of.length)
  1159. return -EPERM;
  1160. if (!nand_is_slc(chip))
  1161. return -EPERM;
  1162. return nand_prog_page_op(chip, page, mtd->writesize + of.offset,
  1163. chip->oob_poi + of.offset, of.length);
  1164. }
  1165. /*
  1166. * This function reads a NAND page without involving the ECC engine (no HW
  1167. * ECC correction).
  1168. * The tricky part in the GPMI/BCH controller is that it stores ECC bits
  1169. * inline (interleaved with payload DATA), and do not align data chunk on
  1170. * byte boundaries.
  1171. * We thus need to take care moving the payload data and ECC bits stored in the
  1172. * page into the provided buffers, which is why we're using gpmi_copy_bits.
  1173. *
  1174. * See set_geometry_by_ecc_info inline comments to have a full description
  1175. * of the layout used by the GPMI controller.
  1176. */
  1177. static int gpmi_ecc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
  1178. int oob_required, int page)
  1179. {
  1180. struct mtd_info *mtd = nand_to_mtd(chip);
  1181. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1182. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1183. int eccsize = nfc_geo->ecc_chunk_size;
  1184. int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
  1185. u8 *tmp_buf = this->raw_buffer;
  1186. size_t src_bit_off;
  1187. size_t oob_bit_off;
  1188. size_t oob_byte_off;
  1189. uint8_t *oob = chip->oob_poi;
  1190. int step;
  1191. nand_read_page_op(chip, page, 0, tmp_buf,
  1192. mtd->writesize + mtd->oobsize);
  1193. /*
  1194. * If required, swap the bad block marker and the data stored in the
  1195. * metadata section, so that we don't wrongly consider a block as bad.
  1196. *
  1197. * See the layout description for a detailed explanation on why this
  1198. * is needed.
  1199. */
  1200. if (this->swap_block_mark)
  1201. swap(tmp_buf[0], tmp_buf[mtd->writesize]);
  1202. /*
  1203. * Copy the metadata section into the oob buffer (this section is
  1204. * guaranteed to be aligned on a byte boundary).
  1205. */
  1206. if (oob_required)
  1207. memcpy(oob, tmp_buf, nfc_geo->metadata_size);
  1208. oob_bit_off = nfc_geo->metadata_size * 8;
  1209. src_bit_off = oob_bit_off;
  1210. /* Extract interleaved payload data and ECC bits */
  1211. for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
  1212. if (buf)
  1213. gpmi_copy_bits(buf, step * eccsize * 8,
  1214. tmp_buf, src_bit_off,
  1215. eccsize * 8);
  1216. src_bit_off += eccsize * 8;
  1217. /* Align last ECC block to align a byte boundary */
  1218. if (step == nfc_geo->ecc_chunk_count - 1 &&
  1219. (oob_bit_off + eccbits) % 8)
  1220. eccbits += 8 - ((oob_bit_off + eccbits) % 8);
  1221. if (oob_required)
  1222. gpmi_copy_bits(oob, oob_bit_off,
  1223. tmp_buf, src_bit_off,
  1224. eccbits);
  1225. src_bit_off += eccbits;
  1226. oob_bit_off += eccbits;
  1227. }
  1228. if (oob_required) {
  1229. oob_byte_off = oob_bit_off / 8;
  1230. if (oob_byte_off < mtd->oobsize)
  1231. memcpy(oob + oob_byte_off,
  1232. tmp_buf + mtd->writesize + oob_byte_off,
  1233. mtd->oobsize - oob_byte_off);
  1234. }
  1235. return 0;
  1236. }
  1237. /*
  1238. * This function writes a NAND page without involving the ECC engine (no HW
  1239. * ECC generation).
  1240. * The tricky part in the GPMI/BCH controller is that it stores ECC bits
  1241. * inline (interleaved with payload DATA), and do not align data chunk on
  1242. * byte boundaries.
  1243. * We thus need to take care moving the OOB area at the right place in the
  1244. * final page, which is why we're using gpmi_copy_bits.
  1245. *
  1246. * See set_geometry_by_ecc_info inline comments to have a full description
  1247. * of the layout used by the GPMI controller.
  1248. */
  1249. static int gpmi_ecc_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
  1250. int oob_required, int page)
  1251. {
  1252. struct mtd_info *mtd = nand_to_mtd(chip);
  1253. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1254. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1255. int eccsize = nfc_geo->ecc_chunk_size;
  1256. int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
  1257. u8 *tmp_buf = this->raw_buffer;
  1258. uint8_t *oob = chip->oob_poi;
  1259. size_t dst_bit_off;
  1260. size_t oob_bit_off;
  1261. size_t oob_byte_off;
  1262. int step;
  1263. /*
  1264. * Initialize all bits to 1 in case we don't have a buffer for the
  1265. * payload or oob data in order to leave unspecified bits of data
  1266. * to their initial state.
  1267. */
  1268. if (!buf || !oob_required)
  1269. memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
  1270. /*
  1271. * First copy the metadata section (stored in oob buffer) at the
  1272. * beginning of the page, as imposed by the GPMI layout.
  1273. */
  1274. memcpy(tmp_buf, oob, nfc_geo->metadata_size);
  1275. oob_bit_off = nfc_geo->metadata_size * 8;
  1276. dst_bit_off = oob_bit_off;
  1277. /* Interleave payload data and ECC bits */
  1278. for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
  1279. if (buf)
  1280. gpmi_copy_bits(tmp_buf, dst_bit_off,
  1281. buf, step * eccsize * 8, eccsize * 8);
  1282. dst_bit_off += eccsize * 8;
  1283. /* Align last ECC block to align a byte boundary */
  1284. if (step == nfc_geo->ecc_chunk_count - 1 &&
  1285. (oob_bit_off + eccbits) % 8)
  1286. eccbits += 8 - ((oob_bit_off + eccbits) % 8);
  1287. if (oob_required)
  1288. gpmi_copy_bits(tmp_buf, dst_bit_off,
  1289. oob, oob_bit_off, eccbits);
  1290. dst_bit_off += eccbits;
  1291. oob_bit_off += eccbits;
  1292. }
  1293. oob_byte_off = oob_bit_off / 8;
  1294. if (oob_required && oob_byte_off < mtd->oobsize)
  1295. memcpy(tmp_buf + mtd->writesize + oob_byte_off,
  1296. oob + oob_byte_off, mtd->oobsize - oob_byte_off);
  1297. /*
  1298. * If required, swap the bad block marker and the first byte of the
  1299. * metadata section, so that we don't modify the bad block marker.
  1300. *
  1301. * See the layout description for a detailed explanation on why this
  1302. * is needed.
  1303. */
  1304. if (this->swap_block_mark)
  1305. swap(tmp_buf[0], tmp_buf[mtd->writesize]);
  1306. return nand_prog_page_op(chip, page, 0, tmp_buf,
  1307. mtd->writesize + mtd->oobsize);
  1308. }
  1309. static int gpmi_ecc_read_oob_raw(struct nand_chip *chip, int page)
  1310. {
  1311. return gpmi_ecc_read_page_raw(chip, NULL, 1, page);
  1312. }
  1313. static int gpmi_ecc_write_oob_raw(struct nand_chip *chip, int page)
  1314. {
  1315. return gpmi_ecc_write_page_raw(chip, NULL, 1, page);
  1316. }
  1317. static int gpmi_block_markbad(struct nand_chip *chip, loff_t ofs)
  1318. {
  1319. struct mtd_info *mtd = nand_to_mtd(chip);
  1320. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1321. int ret = 0;
  1322. uint8_t *block_mark;
  1323. int column, page, chipnr;
  1324. chipnr = (int)(ofs >> chip->chip_shift);
  1325. chip->select_chip(chip, chipnr);
  1326. column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
  1327. /* Write the block mark. */
  1328. block_mark = this->data_buffer_dma;
  1329. block_mark[0] = 0; /* bad block marker */
  1330. /* Shift to get page */
  1331. page = (int)(ofs >> chip->page_shift);
  1332. ret = nand_prog_page_op(chip, page, column, block_mark, 1);
  1333. chip->select_chip(chip, -1);
  1334. return ret;
  1335. }
  1336. static int nand_boot_set_geometry(struct gpmi_nand_data *this)
  1337. {
  1338. struct boot_rom_geometry *geometry = &this->rom_geometry;
  1339. /*
  1340. * Set the boot block stride size.
  1341. *
  1342. * In principle, we should be reading this from the OTP bits, since
  1343. * that's where the ROM is going to get it. In fact, we don't have any
  1344. * way to read the OTP bits, so we go with the default and hope for the
  1345. * best.
  1346. */
  1347. geometry->stride_size_in_pages = 64;
  1348. /*
  1349. * Set the search area stride exponent.
  1350. *
  1351. * In principle, we should be reading this from the OTP bits, since
  1352. * that's where the ROM is going to get it. In fact, we don't have any
  1353. * way to read the OTP bits, so we go with the default and hope for the
  1354. * best.
  1355. */
  1356. geometry->search_area_stride_exponent = 2;
  1357. return 0;
  1358. }
  1359. static const char *fingerprint = "STMP";
  1360. static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
  1361. {
  1362. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1363. struct device *dev = this->dev;
  1364. struct nand_chip *chip = &this->nand;
  1365. unsigned int search_area_size_in_strides;
  1366. unsigned int stride;
  1367. unsigned int page;
  1368. uint8_t *buffer = chip->data_buf;
  1369. int saved_chip_number;
  1370. int found_an_ncb_fingerprint = false;
  1371. /* Compute the number of strides in a search area. */
  1372. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1373. saved_chip_number = this->current_chip;
  1374. chip->select_chip(chip, 0);
  1375. /*
  1376. * Loop through the first search area, looking for the NCB fingerprint.
  1377. */
  1378. dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
  1379. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1380. /* Compute the page addresses. */
  1381. page = stride * rom_geo->stride_size_in_pages;
  1382. dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
  1383. /*
  1384. * Read the NCB fingerprint. The fingerprint is four bytes long
  1385. * and starts in the 12th byte of the page.
  1386. */
  1387. nand_read_page_op(chip, page, 12, NULL, 0);
  1388. chip->legacy.read_buf(chip, buffer, strlen(fingerprint));
  1389. /* Look for the fingerprint. */
  1390. if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
  1391. found_an_ncb_fingerprint = true;
  1392. break;
  1393. }
  1394. }
  1395. chip->select_chip(chip, saved_chip_number);
  1396. if (found_an_ncb_fingerprint)
  1397. dev_dbg(dev, "\tFound a fingerprint\n");
  1398. else
  1399. dev_dbg(dev, "\tNo fingerprint found\n");
  1400. return found_an_ncb_fingerprint;
  1401. }
  1402. /* Writes a transcription stamp. */
  1403. static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
  1404. {
  1405. struct device *dev = this->dev;
  1406. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1407. struct nand_chip *chip = &this->nand;
  1408. struct mtd_info *mtd = nand_to_mtd(chip);
  1409. unsigned int block_size_in_pages;
  1410. unsigned int search_area_size_in_strides;
  1411. unsigned int search_area_size_in_pages;
  1412. unsigned int search_area_size_in_blocks;
  1413. unsigned int block;
  1414. unsigned int stride;
  1415. unsigned int page;
  1416. uint8_t *buffer = chip->data_buf;
  1417. int saved_chip_number;
  1418. int status;
  1419. /* Compute the search area geometry. */
  1420. block_size_in_pages = mtd->erasesize / mtd->writesize;
  1421. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1422. search_area_size_in_pages = search_area_size_in_strides *
  1423. rom_geo->stride_size_in_pages;
  1424. search_area_size_in_blocks =
  1425. (search_area_size_in_pages + (block_size_in_pages - 1)) /
  1426. block_size_in_pages;
  1427. dev_dbg(dev, "Search Area Geometry :\n");
  1428. dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
  1429. dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
  1430. dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
  1431. /* Select chip 0. */
  1432. saved_chip_number = this->current_chip;
  1433. chip->select_chip(chip, 0);
  1434. /* Loop over blocks in the first search area, erasing them. */
  1435. dev_dbg(dev, "Erasing the search area...\n");
  1436. for (block = 0; block < search_area_size_in_blocks; block++) {
  1437. /* Erase this block. */
  1438. dev_dbg(dev, "\tErasing block 0x%x\n", block);
  1439. status = nand_erase_op(chip, block);
  1440. if (status)
  1441. dev_err(dev, "[%s] Erase failed.\n", __func__);
  1442. }
  1443. /* Write the NCB fingerprint into the page buffer. */
  1444. memset(buffer, ~0, mtd->writesize);
  1445. memcpy(buffer + 12, fingerprint, strlen(fingerprint));
  1446. /* Loop through the first search area, writing NCB fingerprints. */
  1447. dev_dbg(dev, "Writing NCB fingerprints...\n");
  1448. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1449. /* Compute the page addresses. */
  1450. page = stride * rom_geo->stride_size_in_pages;
  1451. /* Write the first page of the current stride. */
  1452. dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
  1453. status = chip->ecc.write_page_raw(chip, buffer, 0, page);
  1454. if (status)
  1455. dev_err(dev, "[%s] Write failed.\n", __func__);
  1456. }
  1457. /* Deselect chip 0. */
  1458. chip->select_chip(chip, saved_chip_number);
  1459. return 0;
  1460. }
  1461. static int mx23_boot_init(struct gpmi_nand_data *this)
  1462. {
  1463. struct device *dev = this->dev;
  1464. struct nand_chip *chip = &this->nand;
  1465. struct mtd_info *mtd = nand_to_mtd(chip);
  1466. unsigned int block_count;
  1467. unsigned int block;
  1468. int chipnr;
  1469. int page;
  1470. loff_t byte;
  1471. uint8_t block_mark;
  1472. int ret = 0;
  1473. /*
  1474. * If control arrives here, we can't use block mark swapping, which
  1475. * means we're forced to use transcription. First, scan for the
  1476. * transcription stamp. If we find it, then we don't have to do
  1477. * anything -- the block marks are already transcribed.
  1478. */
  1479. if (mx23_check_transcription_stamp(this))
  1480. return 0;
  1481. /*
  1482. * If control arrives here, we couldn't find a transcription stamp, so
  1483. * so we presume the block marks are in the conventional location.
  1484. */
  1485. dev_dbg(dev, "Transcribing bad block marks...\n");
  1486. /* Compute the number of blocks in the entire medium. */
  1487. block_count = chip->chipsize >> chip->phys_erase_shift;
  1488. /*
  1489. * Loop over all the blocks in the medium, transcribing block marks as
  1490. * we go.
  1491. */
  1492. for (block = 0; block < block_count; block++) {
  1493. /*
  1494. * Compute the chip, page and byte addresses for this block's
  1495. * conventional mark.
  1496. */
  1497. chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
  1498. page = block << (chip->phys_erase_shift - chip->page_shift);
  1499. byte = block << chip->phys_erase_shift;
  1500. /* Send the command to read the conventional block mark. */
  1501. chip->select_chip(chip, chipnr);
  1502. nand_read_page_op(chip, page, mtd->writesize, NULL, 0);
  1503. block_mark = chip->legacy.read_byte(chip);
  1504. chip->select_chip(chip, -1);
  1505. /*
  1506. * Check if the block is marked bad. If so, we need to mark it
  1507. * again, but this time the result will be a mark in the
  1508. * location where we transcribe block marks.
  1509. */
  1510. if (block_mark != 0xff) {
  1511. dev_dbg(dev, "Transcribing mark in block %u\n", block);
  1512. ret = chip->legacy.block_markbad(chip, byte);
  1513. if (ret)
  1514. dev_err(dev,
  1515. "Failed to mark block bad with ret %d\n",
  1516. ret);
  1517. }
  1518. }
  1519. /* Write the stamp that indicates we've transcribed the block marks. */
  1520. mx23_write_transcription_stamp(this);
  1521. return 0;
  1522. }
  1523. static int nand_boot_init(struct gpmi_nand_data *this)
  1524. {
  1525. nand_boot_set_geometry(this);
  1526. /* This is ROM arch-specific initilization before the BBT scanning. */
  1527. if (GPMI_IS_MX23(this))
  1528. return mx23_boot_init(this);
  1529. return 0;
  1530. }
  1531. static int gpmi_set_geometry(struct gpmi_nand_data *this)
  1532. {
  1533. int ret;
  1534. /* Free the temporary DMA memory for reading ID. */
  1535. gpmi_free_dma_buffer(this);
  1536. /* Set up the NFC geometry which is used by BCH. */
  1537. ret = bch_set_geometry(this);
  1538. if (ret) {
  1539. dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
  1540. return ret;
  1541. }
  1542. /* Alloc the new DMA buffers according to the pagesize and oobsize */
  1543. return gpmi_alloc_dma_buffer(this);
  1544. }
  1545. static int gpmi_init_last(struct gpmi_nand_data *this)
  1546. {
  1547. struct nand_chip *chip = &this->nand;
  1548. struct mtd_info *mtd = nand_to_mtd(chip);
  1549. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1550. struct bch_geometry *bch_geo = &this->bch_geometry;
  1551. int ret;
  1552. /* Set up the medium geometry */
  1553. ret = gpmi_set_geometry(this);
  1554. if (ret)
  1555. return ret;
  1556. /* Init the nand_ecc_ctrl{} */
  1557. ecc->read_page = gpmi_ecc_read_page;
  1558. ecc->write_page = gpmi_ecc_write_page;
  1559. ecc->read_oob = gpmi_ecc_read_oob;
  1560. ecc->write_oob = gpmi_ecc_write_oob;
  1561. ecc->read_page_raw = gpmi_ecc_read_page_raw;
  1562. ecc->write_page_raw = gpmi_ecc_write_page_raw;
  1563. ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
  1564. ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
  1565. ecc->mode = NAND_ECC_HW;
  1566. ecc->size = bch_geo->ecc_chunk_size;
  1567. ecc->strength = bch_geo->ecc_strength;
  1568. mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
  1569. /*
  1570. * We only enable the subpage read when:
  1571. * (1) the chip is imx6, and
  1572. * (2) the size of the ECC parity is byte aligned.
  1573. */
  1574. if (GPMI_IS_MX6(this) &&
  1575. ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
  1576. ecc->read_subpage = gpmi_ecc_read_subpage;
  1577. chip->options |= NAND_SUBPAGE_READ;
  1578. }
  1579. return 0;
  1580. }
  1581. static int gpmi_nand_attach_chip(struct nand_chip *chip)
  1582. {
  1583. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1584. int ret;
  1585. if (chip->bbt_options & NAND_BBT_USE_FLASH) {
  1586. chip->bbt_options |= NAND_BBT_NO_OOB;
  1587. if (of_property_read_bool(this->dev->of_node,
  1588. "fsl,no-blockmark-swap"))
  1589. this->swap_block_mark = false;
  1590. }
  1591. dev_dbg(this->dev, "Blockmark swapping %sabled\n",
  1592. this->swap_block_mark ? "en" : "dis");
  1593. ret = gpmi_init_last(this);
  1594. if (ret)
  1595. return ret;
  1596. chip->options |= NAND_SKIP_BBTSCAN;
  1597. return 0;
  1598. }
  1599. static const struct nand_controller_ops gpmi_nand_controller_ops = {
  1600. .attach_chip = gpmi_nand_attach_chip,
  1601. };
  1602. static int gpmi_nand_init(struct gpmi_nand_data *this)
  1603. {
  1604. struct nand_chip *chip = &this->nand;
  1605. struct mtd_info *mtd = nand_to_mtd(chip);
  1606. int ret;
  1607. /* init current chip */
  1608. this->current_chip = -1;
  1609. /* init the MTD data structures */
  1610. mtd->name = "gpmi-nand";
  1611. mtd->dev.parent = this->dev;
  1612. /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
  1613. nand_set_controller_data(chip, this);
  1614. nand_set_flash_node(chip, this->pdev->dev.of_node);
  1615. chip->select_chip = gpmi_select_chip;
  1616. chip->setup_data_interface = gpmi_setup_data_interface;
  1617. chip->legacy.cmd_ctrl = gpmi_cmd_ctrl;
  1618. chip->legacy.dev_ready = gpmi_dev_ready;
  1619. chip->legacy.read_byte = gpmi_read_byte;
  1620. chip->legacy.read_buf = gpmi_read_buf;
  1621. chip->legacy.write_buf = gpmi_write_buf;
  1622. chip->badblock_pattern = &gpmi_bbt_descr;
  1623. chip->legacy.block_markbad = gpmi_block_markbad;
  1624. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1625. /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
  1626. this->swap_block_mark = !GPMI_IS_MX23(this);
  1627. /*
  1628. * Allocate a temporary DMA buffer for reading ID in the
  1629. * nand_scan_ident().
  1630. */
  1631. this->bch_geometry.payload_size = 1024;
  1632. this->bch_geometry.auxiliary_size = 128;
  1633. ret = gpmi_alloc_dma_buffer(this);
  1634. if (ret)
  1635. goto err_out;
  1636. chip->dummy_controller.ops = &gpmi_nand_controller_ops;
  1637. ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1);
  1638. if (ret)
  1639. goto err_out;
  1640. ret = nand_boot_init(this);
  1641. if (ret)
  1642. goto err_nand_cleanup;
  1643. ret = nand_create_bbt(chip);
  1644. if (ret)
  1645. goto err_nand_cleanup;
  1646. ret = mtd_device_register(mtd, NULL, 0);
  1647. if (ret)
  1648. goto err_nand_cleanup;
  1649. return 0;
  1650. err_nand_cleanup:
  1651. nand_cleanup(chip);
  1652. err_out:
  1653. gpmi_free_dma_buffer(this);
  1654. return ret;
  1655. }
  1656. static const struct of_device_id gpmi_nand_id_table[] = {
  1657. {
  1658. .compatible = "fsl,imx23-gpmi-nand",
  1659. .data = &gpmi_devdata_imx23,
  1660. }, {
  1661. .compatible = "fsl,imx28-gpmi-nand",
  1662. .data = &gpmi_devdata_imx28,
  1663. }, {
  1664. .compatible = "fsl,imx6q-gpmi-nand",
  1665. .data = &gpmi_devdata_imx6q,
  1666. }, {
  1667. .compatible = "fsl,imx6sx-gpmi-nand",
  1668. .data = &gpmi_devdata_imx6sx,
  1669. }, {
  1670. .compatible = "fsl,imx7d-gpmi-nand",
  1671. .data = &gpmi_devdata_imx7d,
  1672. }, {}
  1673. };
  1674. MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
  1675. static int gpmi_nand_probe(struct platform_device *pdev)
  1676. {
  1677. struct gpmi_nand_data *this;
  1678. const struct of_device_id *of_id;
  1679. int ret;
  1680. this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
  1681. if (!this)
  1682. return -ENOMEM;
  1683. of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
  1684. if (of_id) {
  1685. this->devdata = of_id->data;
  1686. } else {
  1687. dev_err(&pdev->dev, "Failed to find the right device id.\n");
  1688. return -ENODEV;
  1689. }
  1690. platform_set_drvdata(pdev, this);
  1691. this->pdev = pdev;
  1692. this->dev = &pdev->dev;
  1693. ret = acquire_resources(this);
  1694. if (ret)
  1695. goto exit_acquire_resources;
  1696. ret = gpmi_init(this);
  1697. if (ret)
  1698. goto exit_nfc_init;
  1699. ret = gpmi_nand_init(this);
  1700. if (ret)
  1701. goto exit_nfc_init;
  1702. dev_info(this->dev, "driver registered.\n");
  1703. return 0;
  1704. exit_nfc_init:
  1705. release_resources(this);
  1706. exit_acquire_resources:
  1707. return ret;
  1708. }
  1709. static int gpmi_nand_remove(struct platform_device *pdev)
  1710. {
  1711. struct gpmi_nand_data *this = platform_get_drvdata(pdev);
  1712. nand_release(&this->nand);
  1713. gpmi_free_dma_buffer(this);
  1714. release_resources(this);
  1715. return 0;
  1716. }
  1717. #ifdef CONFIG_PM_SLEEP
  1718. static int gpmi_pm_suspend(struct device *dev)
  1719. {
  1720. struct gpmi_nand_data *this = dev_get_drvdata(dev);
  1721. release_dma_channels(this);
  1722. return 0;
  1723. }
  1724. static int gpmi_pm_resume(struct device *dev)
  1725. {
  1726. struct gpmi_nand_data *this = dev_get_drvdata(dev);
  1727. int ret;
  1728. ret = acquire_dma_channels(this);
  1729. if (ret < 0)
  1730. return ret;
  1731. /* re-init the GPMI registers */
  1732. ret = gpmi_init(this);
  1733. if (ret) {
  1734. dev_err(this->dev, "Error setting GPMI : %d\n", ret);
  1735. return ret;
  1736. }
  1737. /* re-init the BCH registers */
  1738. ret = bch_set_geometry(this);
  1739. if (ret) {
  1740. dev_err(this->dev, "Error setting BCH : %d\n", ret);
  1741. return ret;
  1742. }
  1743. return 0;
  1744. }
  1745. #endif /* CONFIG_PM_SLEEP */
  1746. static const struct dev_pm_ops gpmi_pm_ops = {
  1747. SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
  1748. };
  1749. static struct platform_driver gpmi_nand_driver = {
  1750. .driver = {
  1751. .name = "gpmi-nand",
  1752. .pm = &gpmi_pm_ops,
  1753. .of_match_table = gpmi_nand_id_table,
  1754. },
  1755. .probe = gpmi_nand_probe,
  1756. .remove = gpmi_nand_remove,
  1757. };
  1758. module_platform_driver(gpmi_nand_driver);
  1759. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1760. MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
  1761. MODULE_LICENSE("GPL");