gpmi-nand.c 62 KB

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