gpmi-nand.c 60 KB

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