gpmi-nand.c 62 KB

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