gpmi-nand.c 62 KB

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