gpmi-nand.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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 = 16,
  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 = 16,
  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 = 12,
  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 = 12,
  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 = 12,
  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 init_hardware(struct gpmi_nand_data *this)
  596. {
  597. int ret;
  598. /*
  599. * This structure contains the "safe" GPMI timing that should succeed
  600. * with any NAND Flash device
  601. * (although, with less-than-optimal performance).
  602. */
  603. struct nand_timing safe_timing = {
  604. .data_setup_in_ns = 80,
  605. .data_hold_in_ns = 60,
  606. .address_setup_in_ns = 25,
  607. .gpmi_sample_delay_in_ns = 6,
  608. .tREA_in_ns = -1,
  609. .tRLOH_in_ns = -1,
  610. .tRHOH_in_ns = -1,
  611. };
  612. /* Initialize the hardwares. */
  613. ret = gpmi_init(this);
  614. if (ret)
  615. return ret;
  616. this->timing = safe_timing;
  617. return 0;
  618. }
  619. static int read_page_prepare(struct gpmi_nand_data *this,
  620. void *destination, unsigned length,
  621. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  622. void **use_virt, dma_addr_t *use_phys)
  623. {
  624. struct device *dev = this->dev;
  625. if (virt_addr_valid(destination)) {
  626. dma_addr_t dest_phys;
  627. dest_phys = dma_map_single(dev, destination,
  628. length, DMA_FROM_DEVICE);
  629. if (dma_mapping_error(dev, dest_phys)) {
  630. if (alt_size < length) {
  631. dev_err(dev, "Alternate buffer is too small\n");
  632. return -ENOMEM;
  633. }
  634. goto map_failed;
  635. }
  636. *use_virt = destination;
  637. *use_phys = dest_phys;
  638. this->direct_dma_map_ok = true;
  639. return 0;
  640. }
  641. map_failed:
  642. *use_virt = alt_virt;
  643. *use_phys = alt_phys;
  644. this->direct_dma_map_ok = false;
  645. return 0;
  646. }
  647. static inline void read_page_end(struct gpmi_nand_data *this,
  648. void *destination, unsigned length,
  649. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  650. void *used_virt, dma_addr_t used_phys)
  651. {
  652. if (this->direct_dma_map_ok)
  653. dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
  654. }
  655. static inline void read_page_swap_end(struct gpmi_nand_data *this,
  656. void *destination, unsigned length,
  657. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  658. void *used_virt, dma_addr_t used_phys)
  659. {
  660. if (!this->direct_dma_map_ok)
  661. memcpy(destination, alt_virt, length);
  662. }
  663. static int send_page_prepare(struct gpmi_nand_data *this,
  664. const void *source, unsigned length,
  665. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  666. const void **use_virt, dma_addr_t *use_phys)
  667. {
  668. struct device *dev = this->dev;
  669. if (virt_addr_valid(source)) {
  670. dma_addr_t source_phys;
  671. source_phys = dma_map_single(dev, (void *)source, length,
  672. DMA_TO_DEVICE);
  673. if (dma_mapping_error(dev, source_phys)) {
  674. if (alt_size < length) {
  675. dev_err(dev, "Alternate buffer is too small\n");
  676. return -ENOMEM;
  677. }
  678. goto map_failed;
  679. }
  680. *use_virt = source;
  681. *use_phys = source_phys;
  682. return 0;
  683. }
  684. map_failed:
  685. /*
  686. * Copy the content of the source buffer into the alternate
  687. * buffer and set up the return values accordingly.
  688. */
  689. memcpy(alt_virt, source, length);
  690. *use_virt = alt_virt;
  691. *use_phys = alt_phys;
  692. return 0;
  693. }
  694. static void send_page_end(struct gpmi_nand_data *this,
  695. const void *source, unsigned length,
  696. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  697. const void *used_virt, dma_addr_t used_phys)
  698. {
  699. struct device *dev = this->dev;
  700. if (used_virt == source)
  701. dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
  702. }
  703. static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
  704. {
  705. struct device *dev = this->dev;
  706. if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
  707. dma_free_coherent(dev, this->page_buffer_size,
  708. this->page_buffer_virt,
  709. this->page_buffer_phys);
  710. kfree(this->cmd_buffer);
  711. kfree(this->data_buffer_dma);
  712. kfree(this->raw_buffer);
  713. this->cmd_buffer = NULL;
  714. this->data_buffer_dma = NULL;
  715. this->raw_buffer = NULL;
  716. this->page_buffer_virt = NULL;
  717. this->page_buffer_size = 0;
  718. }
  719. /* Allocate the DMA buffers */
  720. static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
  721. {
  722. struct bch_geometry *geo = &this->bch_geometry;
  723. struct device *dev = this->dev;
  724. struct mtd_info *mtd = nand_to_mtd(&this->nand);
  725. /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
  726. this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
  727. if (this->cmd_buffer == NULL)
  728. goto error_alloc;
  729. /*
  730. * [2] Allocate a read/write data buffer.
  731. * The gpmi_alloc_dma_buffer can be called twice.
  732. * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
  733. * is called before the nand_scan_ident; and we allocate a buffer
  734. * of the real NAND page size when the gpmi_alloc_dma_buffer is
  735. * called after the nand_scan_ident.
  736. */
  737. this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
  738. GFP_DMA | GFP_KERNEL);
  739. if (this->data_buffer_dma == NULL)
  740. goto error_alloc;
  741. /*
  742. * [3] Allocate the page buffer.
  743. *
  744. * Both the payload buffer and the auxiliary buffer must appear on
  745. * 32-bit boundaries. We presume the size of the payload buffer is a
  746. * power of two and is much larger than four, which guarantees the
  747. * auxiliary buffer will appear on a 32-bit boundary.
  748. */
  749. this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
  750. this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
  751. &this->page_buffer_phys, GFP_DMA);
  752. if (!this->page_buffer_virt)
  753. goto error_alloc;
  754. this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
  755. if (!this->raw_buffer)
  756. goto error_alloc;
  757. /* Slice up the page buffer. */
  758. this->payload_virt = this->page_buffer_virt;
  759. this->payload_phys = this->page_buffer_phys;
  760. this->auxiliary_virt = this->payload_virt + geo->payload_size;
  761. this->auxiliary_phys = this->payload_phys + geo->payload_size;
  762. return 0;
  763. error_alloc:
  764. gpmi_free_dma_buffer(this);
  765. return -ENOMEM;
  766. }
  767. static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
  768. {
  769. struct nand_chip *chip = mtd_to_nand(mtd);
  770. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  771. int ret;
  772. /*
  773. * Every operation begins with a command byte and a series of zero or
  774. * more address bytes. These are distinguished by either the Address
  775. * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
  776. * asserted. When MTD is ready to execute the command, it will deassert
  777. * both latch enables.
  778. *
  779. * Rather than run a separate DMA operation for every single byte, we
  780. * queue them up and run a single DMA operation for the entire series
  781. * of command and data bytes. NAND_CMD_NONE means the END of the queue.
  782. */
  783. if ((ctrl & (NAND_ALE | NAND_CLE))) {
  784. if (data != NAND_CMD_NONE)
  785. this->cmd_buffer[this->command_length++] = data;
  786. return;
  787. }
  788. if (!this->command_length)
  789. return;
  790. ret = gpmi_send_command(this);
  791. if (ret)
  792. dev_err(this->dev, "Chip: %u, Error %d\n",
  793. this->current_chip, ret);
  794. this->command_length = 0;
  795. }
  796. static int gpmi_dev_ready(struct mtd_info *mtd)
  797. {
  798. struct nand_chip *chip = mtd_to_nand(mtd);
  799. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  800. return gpmi_is_ready(this, this->current_chip);
  801. }
  802. static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
  803. {
  804. struct nand_chip *chip = mtd_to_nand(mtd);
  805. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  806. if ((this->current_chip < 0) && (chipnr >= 0))
  807. gpmi_begin(this);
  808. else if ((this->current_chip >= 0) && (chipnr < 0))
  809. gpmi_end(this);
  810. this->current_chip = chipnr;
  811. }
  812. static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  813. {
  814. struct nand_chip *chip = mtd_to_nand(mtd);
  815. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  816. dev_dbg(this->dev, "len is %d\n", len);
  817. this->upper_buf = buf;
  818. this->upper_len = len;
  819. gpmi_read_data(this);
  820. }
  821. static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  822. {
  823. struct nand_chip *chip = mtd_to_nand(mtd);
  824. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  825. dev_dbg(this->dev, "len is %d\n", len);
  826. this->upper_buf = (uint8_t *)buf;
  827. this->upper_len = len;
  828. gpmi_send_data(this);
  829. }
  830. static uint8_t gpmi_read_byte(struct mtd_info *mtd)
  831. {
  832. struct nand_chip *chip = mtd_to_nand(mtd);
  833. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  834. uint8_t *buf = this->data_buffer_dma;
  835. gpmi_read_buf(mtd, buf, 1);
  836. return buf[0];
  837. }
  838. /*
  839. * Handles block mark swapping.
  840. * It can be called in swapping the block mark, or swapping it back,
  841. * because the the operations are the same.
  842. */
  843. static void block_mark_swapping(struct gpmi_nand_data *this,
  844. void *payload, void *auxiliary)
  845. {
  846. struct bch_geometry *nfc_geo = &this->bch_geometry;
  847. unsigned char *p;
  848. unsigned char *a;
  849. unsigned int bit;
  850. unsigned char mask;
  851. unsigned char from_data;
  852. unsigned char from_oob;
  853. if (!this->swap_block_mark)
  854. return;
  855. /*
  856. * If control arrives here, we're swapping. Make some convenience
  857. * variables.
  858. */
  859. bit = nfc_geo->block_mark_bit_offset;
  860. p = payload + nfc_geo->block_mark_byte_offset;
  861. a = auxiliary;
  862. /*
  863. * Get the byte from the data area that overlays the block mark. Since
  864. * the ECC engine applies its own view to the bits in the page, the
  865. * physical block mark won't (in general) appear on a byte boundary in
  866. * the data.
  867. */
  868. from_data = (p[0] >> bit) | (p[1] << (8 - bit));
  869. /* Get the byte from the OOB. */
  870. from_oob = a[0];
  871. /* Swap them. */
  872. a[0] = from_data;
  873. mask = (0x1 << bit) - 1;
  874. p[0] = (p[0] & mask) | (from_oob << bit);
  875. mask = ~0 << bit;
  876. p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
  877. }
  878. static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  879. uint8_t *buf, int oob_required, int page)
  880. {
  881. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  882. struct bch_geometry *nfc_geo = &this->bch_geometry;
  883. void *payload_virt;
  884. dma_addr_t payload_phys;
  885. void *auxiliary_virt;
  886. dma_addr_t auxiliary_phys;
  887. unsigned int i;
  888. unsigned char *status;
  889. unsigned int max_bitflips = 0;
  890. int ret;
  891. dev_dbg(this->dev, "page number is : %d\n", page);
  892. ret = read_page_prepare(this, buf, nfc_geo->payload_size,
  893. this->payload_virt, this->payload_phys,
  894. nfc_geo->payload_size,
  895. &payload_virt, &payload_phys);
  896. if (ret) {
  897. dev_err(this->dev, "Inadequate DMA buffer\n");
  898. ret = -ENOMEM;
  899. return ret;
  900. }
  901. auxiliary_virt = this->auxiliary_virt;
  902. auxiliary_phys = this->auxiliary_phys;
  903. /* go! */
  904. ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
  905. read_page_end(this, buf, nfc_geo->payload_size,
  906. this->payload_virt, this->payload_phys,
  907. nfc_geo->payload_size,
  908. payload_virt, payload_phys);
  909. if (ret) {
  910. dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
  911. return ret;
  912. }
  913. /* Loop over status bytes, accumulating ECC status. */
  914. status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
  915. read_page_swap_end(this, buf, nfc_geo->payload_size,
  916. this->payload_virt, this->payload_phys,
  917. nfc_geo->payload_size,
  918. payload_virt, payload_phys);
  919. for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
  920. if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
  921. continue;
  922. if (*status == STATUS_UNCORRECTABLE) {
  923. int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
  924. u8 *eccbuf = this->raw_buffer;
  925. int offset, bitoffset;
  926. int eccbytes;
  927. int flips;
  928. /* Read ECC bytes into our internal raw_buffer */
  929. offset = nfc_geo->metadata_size * 8;
  930. offset += ((8 * nfc_geo->ecc_chunk_size) + eccbits) * (i + 1);
  931. offset -= eccbits;
  932. bitoffset = offset % 8;
  933. eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
  934. offset /= 8;
  935. eccbytes -= offset;
  936. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  937. chip->read_buf(mtd, eccbuf, eccbytes);
  938. /*
  939. * ECC data are not byte aligned and we may have
  940. * in-band data in the first and last byte of
  941. * eccbuf. Set non-eccbits to one so that
  942. * nand_check_erased_ecc_chunk() does not count them
  943. * as bitflips.
  944. */
  945. if (bitoffset)
  946. eccbuf[0] |= GENMASK(bitoffset - 1, 0);
  947. bitoffset = (bitoffset + eccbits) % 8;
  948. if (bitoffset)
  949. eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
  950. /*
  951. * The ECC hardware has an uncorrectable ECC status
  952. * code in case we have bitflips in an erased page. As
  953. * nothing was written into this subpage the ECC is
  954. * obviously wrong and we can not trust it. We assume
  955. * at this point that we are reading an erased page and
  956. * try to correct the bitflips in buffer up to
  957. * ecc_strength bitflips. If this is a page with random
  958. * data, we exceed this number of bitflips and have a
  959. * ECC failure. Otherwise we use the corrected buffer.
  960. */
  961. if (i == 0) {
  962. /* The first block includes metadata */
  963. flips = nand_check_erased_ecc_chunk(
  964. buf + i * nfc_geo->ecc_chunk_size,
  965. nfc_geo->ecc_chunk_size,
  966. eccbuf, eccbytes,
  967. auxiliary_virt,
  968. nfc_geo->metadata_size,
  969. nfc_geo->ecc_strength);
  970. } else {
  971. flips = nand_check_erased_ecc_chunk(
  972. buf + i * nfc_geo->ecc_chunk_size,
  973. nfc_geo->ecc_chunk_size,
  974. eccbuf, eccbytes,
  975. NULL, 0,
  976. nfc_geo->ecc_strength);
  977. }
  978. if (flips > 0) {
  979. max_bitflips = max_t(unsigned int, max_bitflips,
  980. flips);
  981. mtd->ecc_stats.corrected += flips;
  982. continue;
  983. }
  984. mtd->ecc_stats.failed++;
  985. continue;
  986. }
  987. mtd->ecc_stats.corrected += *status;
  988. max_bitflips = max_t(unsigned int, max_bitflips, *status);
  989. }
  990. /* handle the block mark swapping */
  991. block_mark_swapping(this, buf, auxiliary_virt);
  992. if (oob_required) {
  993. /*
  994. * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
  995. * for details about our policy for delivering the OOB.
  996. *
  997. * We fill the caller's buffer with set bits, and then copy the
  998. * block mark to th caller's buffer. Note that, if block mark
  999. * swapping was necessary, it has already been done, so we can
  1000. * rely on the first byte of the auxiliary buffer to contain
  1001. * the block mark.
  1002. */
  1003. memset(chip->oob_poi, ~0, mtd->oobsize);
  1004. chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
  1005. }
  1006. return max_bitflips;
  1007. }
  1008. /* Fake a virtual small page for the subpage read */
  1009. static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  1010. uint32_t offs, uint32_t len, uint8_t *buf, int page)
  1011. {
  1012. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1013. void __iomem *bch_regs = this->resources.bch_regs;
  1014. struct bch_geometry old_geo = this->bch_geometry;
  1015. struct bch_geometry *geo = &this->bch_geometry;
  1016. int size = chip->ecc.size; /* ECC chunk size */
  1017. int meta, n, page_size;
  1018. u32 r1_old, r2_old, r1_new, r2_new;
  1019. unsigned int max_bitflips;
  1020. int first, last, marker_pos;
  1021. int ecc_parity_size;
  1022. int col = 0;
  1023. int old_swap_block_mark = this->swap_block_mark;
  1024. /* The size of ECC parity */
  1025. ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
  1026. /* Align it with the chunk size */
  1027. first = offs / size;
  1028. last = (offs + len - 1) / size;
  1029. if (this->swap_block_mark) {
  1030. /*
  1031. * Find the chunk which contains the Block Marker.
  1032. * If this chunk is in the range of [first, last],
  1033. * we have to read out the whole page.
  1034. * Why? since we had swapped the data at the position of Block
  1035. * Marker to the metadata which is bound with the chunk 0.
  1036. */
  1037. marker_pos = geo->block_mark_byte_offset / size;
  1038. if (last >= marker_pos && first <= marker_pos) {
  1039. dev_dbg(this->dev,
  1040. "page:%d, first:%d, last:%d, marker at:%d\n",
  1041. page, first, last, marker_pos);
  1042. return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
  1043. }
  1044. }
  1045. meta = geo->metadata_size;
  1046. if (first) {
  1047. col = meta + (size + ecc_parity_size) * first;
  1048. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, col, -1);
  1049. meta = 0;
  1050. buf = buf + first * size;
  1051. }
  1052. /* Save the old environment */
  1053. r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
  1054. r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
  1055. /* change the BCH registers and bch_geometry{} */
  1056. n = last - first + 1;
  1057. page_size = meta + (size + ecc_parity_size) * n;
  1058. r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
  1059. BM_BCH_FLASH0LAYOUT0_META_SIZE);
  1060. r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
  1061. | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
  1062. writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
  1063. r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
  1064. r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
  1065. writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
  1066. geo->ecc_chunk_count = n;
  1067. geo->payload_size = n * size;
  1068. geo->page_size = page_size;
  1069. geo->auxiliary_status_offset = ALIGN(meta, 4);
  1070. dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
  1071. page, offs, len, col, first, n, page_size);
  1072. /* Read the subpage now */
  1073. this->swap_block_mark = false;
  1074. max_bitflips = gpmi_ecc_read_page(mtd, chip, buf, 0, page);
  1075. /* Restore */
  1076. writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
  1077. writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
  1078. this->bch_geometry = old_geo;
  1079. this->swap_block_mark = old_swap_block_mark;
  1080. return max_bitflips;
  1081. }
  1082. static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1083. const uint8_t *buf, int oob_required, int page)
  1084. {
  1085. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1086. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1087. const void *payload_virt;
  1088. dma_addr_t payload_phys;
  1089. const void *auxiliary_virt;
  1090. dma_addr_t auxiliary_phys;
  1091. int ret;
  1092. dev_dbg(this->dev, "ecc write page.\n");
  1093. if (this->swap_block_mark) {
  1094. /*
  1095. * If control arrives here, we're doing block mark swapping.
  1096. * Since we can't modify the caller's buffers, we must copy them
  1097. * into our own.
  1098. */
  1099. memcpy(this->payload_virt, buf, mtd->writesize);
  1100. payload_virt = this->payload_virt;
  1101. payload_phys = this->payload_phys;
  1102. memcpy(this->auxiliary_virt, chip->oob_poi,
  1103. nfc_geo->auxiliary_size);
  1104. auxiliary_virt = this->auxiliary_virt;
  1105. auxiliary_phys = this->auxiliary_phys;
  1106. /* Handle block mark swapping. */
  1107. block_mark_swapping(this,
  1108. (void *)payload_virt, (void *)auxiliary_virt);
  1109. } else {
  1110. /*
  1111. * If control arrives here, we're not doing block mark swapping,
  1112. * so we can to try and use the caller's buffers.
  1113. */
  1114. ret = send_page_prepare(this,
  1115. buf, mtd->writesize,
  1116. this->payload_virt, this->payload_phys,
  1117. nfc_geo->payload_size,
  1118. &payload_virt, &payload_phys);
  1119. if (ret) {
  1120. dev_err(this->dev, "Inadequate payload DMA buffer\n");
  1121. return 0;
  1122. }
  1123. ret = send_page_prepare(this,
  1124. chip->oob_poi, mtd->oobsize,
  1125. this->auxiliary_virt, this->auxiliary_phys,
  1126. nfc_geo->auxiliary_size,
  1127. &auxiliary_virt, &auxiliary_phys);
  1128. if (ret) {
  1129. dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
  1130. goto exit_auxiliary;
  1131. }
  1132. }
  1133. /* Ask the NFC. */
  1134. ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
  1135. if (ret)
  1136. dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
  1137. if (!this->swap_block_mark) {
  1138. send_page_end(this, chip->oob_poi, mtd->oobsize,
  1139. this->auxiliary_virt, this->auxiliary_phys,
  1140. nfc_geo->auxiliary_size,
  1141. auxiliary_virt, auxiliary_phys);
  1142. exit_auxiliary:
  1143. send_page_end(this, buf, mtd->writesize,
  1144. this->payload_virt, this->payload_phys,
  1145. nfc_geo->payload_size,
  1146. payload_virt, payload_phys);
  1147. }
  1148. return 0;
  1149. }
  1150. /*
  1151. * There are several places in this driver where we have to handle the OOB and
  1152. * block marks. This is the function where things are the most complicated, so
  1153. * this is where we try to explain it all. All the other places refer back to
  1154. * here.
  1155. *
  1156. * These are the rules, in order of decreasing importance:
  1157. *
  1158. * 1) Nothing the caller does can be allowed to imperil the block mark.
  1159. *
  1160. * 2) In read operations, the first byte of the OOB we return must reflect the
  1161. * true state of the block mark, no matter where that block mark appears in
  1162. * the physical page.
  1163. *
  1164. * 3) ECC-based read operations return an OOB full of set bits (since we never
  1165. * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
  1166. * return).
  1167. *
  1168. * 4) "Raw" read operations return a direct view of the physical bytes in the
  1169. * page, using the conventional definition of which bytes are data and which
  1170. * are OOB. This gives the caller a way to see the actual, physical bytes
  1171. * in the page, without the distortions applied by our ECC engine.
  1172. *
  1173. *
  1174. * What we do for this specific read operation depends on two questions:
  1175. *
  1176. * 1) Are we doing a "raw" read, or an ECC-based read?
  1177. *
  1178. * 2) Are we using block mark swapping or transcription?
  1179. *
  1180. * There are four cases, illustrated by the following Karnaugh map:
  1181. *
  1182. * | Raw | ECC-based |
  1183. * -------------+-------------------------+-------------------------+
  1184. * | Read the conventional | |
  1185. * | OOB at the end of the | |
  1186. * Swapping | page and return it. It | |
  1187. * | contains exactly what | |
  1188. * | we want. | Read the block mark and |
  1189. * -------------+-------------------------+ return it in a buffer |
  1190. * | Read the conventional | full of set bits. |
  1191. * | OOB at the end of the | |
  1192. * | page and also the block | |
  1193. * Transcribing | mark in the metadata. | |
  1194. * | Copy the block mark | |
  1195. * | into the first byte of | |
  1196. * | the OOB. | |
  1197. * -------------+-------------------------+-------------------------+
  1198. *
  1199. * Note that we break rule #4 in the Transcribing/Raw case because we're not
  1200. * giving an accurate view of the actual, physical bytes in the page (we're
  1201. * overwriting the block mark). That's OK because it's more important to follow
  1202. * rule #2.
  1203. *
  1204. * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
  1205. * easy. When reading a page, for example, the NAND Flash MTD code calls our
  1206. * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
  1207. * ECC-based or raw view of the page is implicit in which function it calls
  1208. * (there is a similar pair of ECC-based/raw functions for writing).
  1209. */
  1210. static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1211. int page)
  1212. {
  1213. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1214. dev_dbg(this->dev, "page number is %d\n", page);
  1215. /* clear the OOB buffer */
  1216. memset(chip->oob_poi, ~0, mtd->oobsize);
  1217. /* Read out the conventional OOB. */
  1218. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  1219. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1220. /*
  1221. * Now, we want to make sure the block mark is correct. In the
  1222. * non-transcribing case (!GPMI_IS_MX23()), we already have it.
  1223. * Otherwise, we need to explicitly read it.
  1224. */
  1225. if (GPMI_IS_MX23(this)) {
  1226. /* Read the block mark into the first byte of the OOB buffer. */
  1227. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1228. chip->oob_poi[0] = chip->read_byte(mtd);
  1229. }
  1230. return 0;
  1231. }
  1232. static int
  1233. gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  1234. {
  1235. struct mtd_oob_region of = { };
  1236. int status = 0;
  1237. /* Do we have available oob area? */
  1238. mtd_ooblayout_free(mtd, 0, &of);
  1239. if (!of.length)
  1240. return -EPERM;
  1241. if (!nand_is_slc(chip))
  1242. return -EPERM;
  1243. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of.offset, page);
  1244. chip->write_buf(mtd, chip->oob_poi + of.offset, of.length);
  1245. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1246. status = chip->waitfunc(mtd, chip);
  1247. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1248. }
  1249. /*
  1250. * This function reads a NAND page without involving the ECC engine (no HW
  1251. * ECC correction).
  1252. * The tricky part in the GPMI/BCH controller is that it stores ECC bits
  1253. * inline (interleaved with payload DATA), and do not align data chunk on
  1254. * byte boundaries.
  1255. * We thus need to take care moving the payload data and ECC bits stored in the
  1256. * page into the provided buffers, which is why we're using gpmi_copy_bits.
  1257. *
  1258. * See set_geometry_by_ecc_info inline comments to have a full description
  1259. * of the layout used by the GPMI controller.
  1260. */
  1261. static int gpmi_ecc_read_page_raw(struct mtd_info *mtd,
  1262. struct nand_chip *chip, uint8_t *buf,
  1263. int oob_required, int page)
  1264. {
  1265. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1266. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1267. int eccsize = nfc_geo->ecc_chunk_size;
  1268. int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
  1269. u8 *tmp_buf = this->raw_buffer;
  1270. size_t src_bit_off;
  1271. size_t oob_bit_off;
  1272. size_t oob_byte_off;
  1273. uint8_t *oob = chip->oob_poi;
  1274. int step;
  1275. chip->read_buf(mtd, tmp_buf,
  1276. mtd->writesize + mtd->oobsize);
  1277. /*
  1278. * If required, swap the bad block marker and the data stored in the
  1279. * metadata section, so that we don't wrongly consider a block as bad.
  1280. *
  1281. * See the layout description for a detailed explanation on why this
  1282. * is needed.
  1283. */
  1284. if (this->swap_block_mark) {
  1285. u8 swap = tmp_buf[0];
  1286. tmp_buf[0] = tmp_buf[mtd->writesize];
  1287. tmp_buf[mtd->writesize] = swap;
  1288. }
  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. u8 swap = tmp_buf[0];
  1394. tmp_buf[0] = tmp_buf[mtd->writesize];
  1395. tmp_buf[mtd->writesize] = swap;
  1396. }
  1397. chip->write_buf(mtd, tmp_buf, mtd->writesize + mtd->oobsize);
  1398. return 0;
  1399. }
  1400. static int gpmi_ecc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1401. int page)
  1402. {
  1403. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1404. return gpmi_ecc_read_page_raw(mtd, chip, NULL, 1, page);
  1405. }
  1406. static int gpmi_ecc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1407. int page)
  1408. {
  1409. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
  1410. return gpmi_ecc_write_page_raw(mtd, chip, NULL, 1, page);
  1411. }
  1412. static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1413. {
  1414. struct nand_chip *chip = mtd_to_nand(mtd);
  1415. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1416. int ret = 0;
  1417. uint8_t *block_mark;
  1418. int column, page, status, chipnr;
  1419. chipnr = (int)(ofs >> chip->chip_shift);
  1420. chip->select_chip(mtd, chipnr);
  1421. column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
  1422. /* Write the block mark. */
  1423. block_mark = this->data_buffer_dma;
  1424. block_mark[0] = 0; /* bad block marker */
  1425. /* Shift to get page */
  1426. page = (int)(ofs >> chip->page_shift);
  1427. chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
  1428. chip->write_buf(mtd, block_mark, 1);
  1429. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1430. status = chip->waitfunc(mtd, chip);
  1431. if (status & NAND_STATUS_FAIL)
  1432. ret = -EIO;
  1433. chip->select_chip(mtd, -1);
  1434. return ret;
  1435. }
  1436. static int nand_boot_set_geometry(struct gpmi_nand_data *this)
  1437. {
  1438. struct boot_rom_geometry *geometry = &this->rom_geometry;
  1439. /*
  1440. * Set the boot block stride size.
  1441. *
  1442. * In principle, we should be reading this from the OTP bits, since
  1443. * that's where the ROM is going to get it. In fact, we don't have any
  1444. * way to read the OTP bits, so we go with the default and hope for the
  1445. * best.
  1446. */
  1447. geometry->stride_size_in_pages = 64;
  1448. /*
  1449. * Set the search area stride exponent.
  1450. *
  1451. * In principle, we should be reading this from the OTP bits, since
  1452. * that's where the ROM is going to get it. In fact, we don't have any
  1453. * way to read the OTP bits, so we go with the default and hope for the
  1454. * best.
  1455. */
  1456. geometry->search_area_stride_exponent = 2;
  1457. return 0;
  1458. }
  1459. static const char *fingerprint = "STMP";
  1460. static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
  1461. {
  1462. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1463. struct device *dev = this->dev;
  1464. struct nand_chip *chip = &this->nand;
  1465. struct mtd_info *mtd = nand_to_mtd(chip);
  1466. unsigned int search_area_size_in_strides;
  1467. unsigned int stride;
  1468. unsigned int page;
  1469. uint8_t *buffer = chip->buffers->databuf;
  1470. int saved_chip_number;
  1471. int found_an_ncb_fingerprint = false;
  1472. /* Compute the number of strides in a search area. */
  1473. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1474. saved_chip_number = this->current_chip;
  1475. chip->select_chip(mtd, 0);
  1476. /*
  1477. * Loop through the first search area, looking for the NCB fingerprint.
  1478. */
  1479. dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
  1480. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1481. /* Compute the page addresses. */
  1482. page = stride * rom_geo->stride_size_in_pages;
  1483. dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
  1484. /*
  1485. * Read the NCB fingerprint. The fingerprint is four bytes long
  1486. * and starts in the 12th byte of the page.
  1487. */
  1488. chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
  1489. chip->read_buf(mtd, buffer, strlen(fingerprint));
  1490. /* Look for the fingerprint. */
  1491. if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
  1492. found_an_ncb_fingerprint = true;
  1493. break;
  1494. }
  1495. }
  1496. chip->select_chip(mtd, saved_chip_number);
  1497. if (found_an_ncb_fingerprint)
  1498. dev_dbg(dev, "\tFound a fingerprint\n");
  1499. else
  1500. dev_dbg(dev, "\tNo fingerprint found\n");
  1501. return found_an_ncb_fingerprint;
  1502. }
  1503. /* Writes a transcription stamp. */
  1504. static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
  1505. {
  1506. struct device *dev = this->dev;
  1507. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1508. struct nand_chip *chip = &this->nand;
  1509. struct mtd_info *mtd = nand_to_mtd(chip);
  1510. unsigned int block_size_in_pages;
  1511. unsigned int search_area_size_in_strides;
  1512. unsigned int search_area_size_in_pages;
  1513. unsigned int search_area_size_in_blocks;
  1514. unsigned int block;
  1515. unsigned int stride;
  1516. unsigned int page;
  1517. uint8_t *buffer = chip->buffers->databuf;
  1518. int saved_chip_number;
  1519. int status;
  1520. /* Compute the search area geometry. */
  1521. block_size_in_pages = mtd->erasesize / mtd->writesize;
  1522. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1523. search_area_size_in_pages = search_area_size_in_strides *
  1524. rom_geo->stride_size_in_pages;
  1525. search_area_size_in_blocks =
  1526. (search_area_size_in_pages + (block_size_in_pages - 1)) /
  1527. block_size_in_pages;
  1528. dev_dbg(dev, "Search Area Geometry :\n");
  1529. dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
  1530. dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
  1531. dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
  1532. /* Select chip 0. */
  1533. saved_chip_number = this->current_chip;
  1534. chip->select_chip(mtd, 0);
  1535. /* Loop over blocks in the first search area, erasing them. */
  1536. dev_dbg(dev, "Erasing the search area...\n");
  1537. for (block = 0; block < search_area_size_in_blocks; block++) {
  1538. /* Compute the page address. */
  1539. page = block * block_size_in_pages;
  1540. /* Erase this block. */
  1541. dev_dbg(dev, "\tErasing block 0x%x\n", block);
  1542. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1543. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1544. /* Wait for the erase to finish. */
  1545. status = chip->waitfunc(mtd, chip);
  1546. if (status & NAND_STATUS_FAIL)
  1547. dev_err(dev, "[%s] Erase failed.\n", __func__);
  1548. }
  1549. /* Write the NCB fingerprint into the page buffer. */
  1550. memset(buffer, ~0, mtd->writesize);
  1551. memcpy(buffer + 12, fingerprint, strlen(fingerprint));
  1552. /* Loop through the first search area, writing NCB fingerprints. */
  1553. dev_dbg(dev, "Writing NCB fingerprints...\n");
  1554. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1555. /* Compute the page addresses. */
  1556. page = stride * rom_geo->stride_size_in_pages;
  1557. /* Write the first page of the current stride. */
  1558. dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
  1559. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1560. chip->ecc.write_page_raw(mtd, chip, buffer, 0, page);
  1561. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1562. /* Wait for the write to finish. */
  1563. status = chip->waitfunc(mtd, chip);
  1564. if (status & NAND_STATUS_FAIL)
  1565. dev_err(dev, "[%s] Write failed.\n", __func__);
  1566. }
  1567. /* Deselect chip 0. */
  1568. chip->select_chip(mtd, saved_chip_number);
  1569. return 0;
  1570. }
  1571. static int mx23_boot_init(struct gpmi_nand_data *this)
  1572. {
  1573. struct device *dev = this->dev;
  1574. struct nand_chip *chip = &this->nand;
  1575. struct mtd_info *mtd = nand_to_mtd(chip);
  1576. unsigned int block_count;
  1577. unsigned int block;
  1578. int chipnr;
  1579. int page;
  1580. loff_t byte;
  1581. uint8_t block_mark;
  1582. int ret = 0;
  1583. /*
  1584. * If control arrives here, we can't use block mark swapping, which
  1585. * means we're forced to use transcription. First, scan for the
  1586. * transcription stamp. If we find it, then we don't have to do
  1587. * anything -- the block marks are already transcribed.
  1588. */
  1589. if (mx23_check_transcription_stamp(this))
  1590. return 0;
  1591. /*
  1592. * If control arrives here, we couldn't find a transcription stamp, so
  1593. * so we presume the block marks are in the conventional location.
  1594. */
  1595. dev_dbg(dev, "Transcribing bad block marks...\n");
  1596. /* Compute the number of blocks in the entire medium. */
  1597. block_count = chip->chipsize >> chip->phys_erase_shift;
  1598. /*
  1599. * Loop over all the blocks in the medium, transcribing block marks as
  1600. * we go.
  1601. */
  1602. for (block = 0; block < block_count; block++) {
  1603. /*
  1604. * Compute the chip, page and byte addresses for this block's
  1605. * conventional mark.
  1606. */
  1607. chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
  1608. page = block << (chip->phys_erase_shift - chip->page_shift);
  1609. byte = block << chip->phys_erase_shift;
  1610. /* Send the command to read the conventional block mark. */
  1611. chip->select_chip(mtd, chipnr);
  1612. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  1613. block_mark = chip->read_byte(mtd);
  1614. chip->select_chip(mtd, -1);
  1615. /*
  1616. * Check if the block is marked bad. If so, we need to mark it
  1617. * again, but this time the result will be a mark in the
  1618. * location where we transcribe block marks.
  1619. */
  1620. if (block_mark != 0xff) {
  1621. dev_dbg(dev, "Transcribing mark in block %u\n", block);
  1622. ret = chip->block_markbad(mtd, byte);
  1623. if (ret)
  1624. dev_err(dev,
  1625. "Failed to mark block bad with ret %d\n",
  1626. ret);
  1627. }
  1628. }
  1629. /* Write the stamp that indicates we've transcribed the block marks. */
  1630. mx23_write_transcription_stamp(this);
  1631. return 0;
  1632. }
  1633. static int nand_boot_init(struct gpmi_nand_data *this)
  1634. {
  1635. nand_boot_set_geometry(this);
  1636. /* This is ROM arch-specific initilization before the BBT scanning. */
  1637. if (GPMI_IS_MX23(this))
  1638. return mx23_boot_init(this);
  1639. return 0;
  1640. }
  1641. static int gpmi_set_geometry(struct gpmi_nand_data *this)
  1642. {
  1643. int ret;
  1644. /* Free the temporary DMA memory for reading ID. */
  1645. gpmi_free_dma_buffer(this);
  1646. /* Set up the NFC geometry which is used by BCH. */
  1647. ret = bch_set_geometry(this);
  1648. if (ret) {
  1649. dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
  1650. return ret;
  1651. }
  1652. /* Alloc the new DMA buffers according to the pagesize and oobsize */
  1653. return gpmi_alloc_dma_buffer(this);
  1654. }
  1655. static int gpmi_init_last(struct gpmi_nand_data *this)
  1656. {
  1657. struct nand_chip *chip = &this->nand;
  1658. struct mtd_info *mtd = nand_to_mtd(chip);
  1659. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1660. struct bch_geometry *bch_geo = &this->bch_geometry;
  1661. int ret;
  1662. /* Set up the medium geometry */
  1663. ret = gpmi_set_geometry(this);
  1664. if (ret)
  1665. return ret;
  1666. /* Init the nand_ecc_ctrl{} */
  1667. ecc->read_page = gpmi_ecc_read_page;
  1668. ecc->write_page = gpmi_ecc_write_page;
  1669. ecc->read_oob = gpmi_ecc_read_oob;
  1670. ecc->write_oob = gpmi_ecc_write_oob;
  1671. ecc->read_page_raw = gpmi_ecc_read_page_raw;
  1672. ecc->write_page_raw = gpmi_ecc_write_page_raw;
  1673. ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
  1674. ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
  1675. ecc->mode = NAND_ECC_HW;
  1676. ecc->size = bch_geo->ecc_chunk_size;
  1677. ecc->strength = bch_geo->ecc_strength;
  1678. mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
  1679. /*
  1680. * We only enable the subpage read when:
  1681. * (1) the chip is imx6, and
  1682. * (2) the size of the ECC parity is byte aligned.
  1683. */
  1684. if (GPMI_IS_MX6(this) &&
  1685. ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
  1686. ecc->read_subpage = gpmi_ecc_read_subpage;
  1687. chip->options |= NAND_SUBPAGE_READ;
  1688. }
  1689. /*
  1690. * Can we enable the extra features? such as EDO or Sync mode.
  1691. *
  1692. * We do not check the return value now. That's means if we fail in
  1693. * enable the extra features, we still can run in the normal way.
  1694. */
  1695. gpmi_extra_init(this);
  1696. return 0;
  1697. }
  1698. static int gpmi_nand_init(struct gpmi_nand_data *this)
  1699. {
  1700. struct nand_chip *chip = &this->nand;
  1701. struct mtd_info *mtd = nand_to_mtd(chip);
  1702. int ret;
  1703. /* init current chip */
  1704. this->current_chip = -1;
  1705. /* init the MTD data structures */
  1706. mtd->name = "gpmi-nand";
  1707. mtd->dev.parent = this->dev;
  1708. /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
  1709. nand_set_controller_data(chip, this);
  1710. nand_set_flash_node(chip, this->pdev->dev.of_node);
  1711. chip->select_chip = gpmi_select_chip;
  1712. chip->cmd_ctrl = gpmi_cmd_ctrl;
  1713. chip->dev_ready = gpmi_dev_ready;
  1714. chip->read_byte = gpmi_read_byte;
  1715. chip->read_buf = gpmi_read_buf;
  1716. chip->write_buf = gpmi_write_buf;
  1717. chip->badblock_pattern = &gpmi_bbt_descr;
  1718. chip->block_markbad = gpmi_block_markbad;
  1719. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1720. /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
  1721. this->swap_block_mark = !GPMI_IS_MX23(this);
  1722. /*
  1723. * Allocate a temporary DMA buffer for reading ID in the
  1724. * nand_scan_ident().
  1725. */
  1726. this->bch_geometry.payload_size = 1024;
  1727. this->bch_geometry.auxiliary_size = 128;
  1728. ret = gpmi_alloc_dma_buffer(this);
  1729. if (ret)
  1730. goto err_out;
  1731. ret = nand_scan_ident(mtd, GPMI_IS_MX6(this) ? 2 : 1, NULL);
  1732. if (ret)
  1733. goto err_out;
  1734. if (chip->bbt_options & NAND_BBT_USE_FLASH) {
  1735. chip->bbt_options |= NAND_BBT_NO_OOB;
  1736. if (of_property_read_bool(this->dev->of_node,
  1737. "fsl,no-blockmark-swap"))
  1738. this->swap_block_mark = false;
  1739. }
  1740. dev_dbg(this->dev, "Blockmark swapping %sabled\n",
  1741. this->swap_block_mark ? "en" : "dis");
  1742. ret = gpmi_init_last(this);
  1743. if (ret)
  1744. goto err_out;
  1745. chip->options |= NAND_SKIP_BBTSCAN;
  1746. ret = nand_scan_tail(mtd);
  1747. if (ret)
  1748. goto err_out;
  1749. ret = nand_boot_init(this);
  1750. if (ret)
  1751. goto err_nand_cleanup;
  1752. ret = chip->scan_bbt(mtd);
  1753. if (ret)
  1754. goto err_nand_cleanup;
  1755. ret = mtd_device_register(mtd, NULL, 0);
  1756. if (ret)
  1757. goto err_nand_cleanup;
  1758. return 0;
  1759. err_nand_cleanup:
  1760. nand_cleanup(chip);
  1761. err_out:
  1762. gpmi_free_dma_buffer(this);
  1763. return ret;
  1764. }
  1765. static const struct of_device_id gpmi_nand_id_table[] = {
  1766. {
  1767. .compatible = "fsl,imx23-gpmi-nand",
  1768. .data = &gpmi_devdata_imx23,
  1769. }, {
  1770. .compatible = "fsl,imx28-gpmi-nand",
  1771. .data = &gpmi_devdata_imx28,
  1772. }, {
  1773. .compatible = "fsl,imx6q-gpmi-nand",
  1774. .data = &gpmi_devdata_imx6q,
  1775. }, {
  1776. .compatible = "fsl,imx6sx-gpmi-nand",
  1777. .data = &gpmi_devdata_imx6sx,
  1778. }, {
  1779. .compatible = "fsl,imx7d-gpmi-nand",
  1780. .data = &gpmi_devdata_imx7d,
  1781. }, {}
  1782. };
  1783. MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
  1784. static int gpmi_nand_probe(struct platform_device *pdev)
  1785. {
  1786. struct gpmi_nand_data *this;
  1787. const struct of_device_id *of_id;
  1788. int ret;
  1789. this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
  1790. if (!this)
  1791. return -ENOMEM;
  1792. of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
  1793. if (of_id) {
  1794. this->devdata = of_id->data;
  1795. } else {
  1796. dev_err(&pdev->dev, "Failed to find the right device id.\n");
  1797. return -ENODEV;
  1798. }
  1799. platform_set_drvdata(pdev, this);
  1800. this->pdev = pdev;
  1801. this->dev = &pdev->dev;
  1802. ret = acquire_resources(this);
  1803. if (ret)
  1804. goto exit_acquire_resources;
  1805. ret = init_hardware(this);
  1806. if (ret)
  1807. goto exit_nfc_init;
  1808. ret = gpmi_nand_init(this);
  1809. if (ret)
  1810. goto exit_nfc_init;
  1811. dev_info(this->dev, "driver registered.\n");
  1812. return 0;
  1813. exit_nfc_init:
  1814. release_resources(this);
  1815. exit_acquire_resources:
  1816. return ret;
  1817. }
  1818. static int gpmi_nand_remove(struct platform_device *pdev)
  1819. {
  1820. struct gpmi_nand_data *this = platform_get_drvdata(pdev);
  1821. nand_release(nand_to_mtd(&this->nand));
  1822. gpmi_free_dma_buffer(this);
  1823. release_resources(this);
  1824. return 0;
  1825. }
  1826. #ifdef CONFIG_PM_SLEEP
  1827. static int gpmi_pm_suspend(struct device *dev)
  1828. {
  1829. struct gpmi_nand_data *this = dev_get_drvdata(dev);
  1830. release_dma_channels(this);
  1831. return 0;
  1832. }
  1833. static int gpmi_pm_resume(struct device *dev)
  1834. {
  1835. struct gpmi_nand_data *this = dev_get_drvdata(dev);
  1836. int ret;
  1837. ret = acquire_dma_channels(this);
  1838. if (ret < 0)
  1839. return ret;
  1840. /* re-init the GPMI registers */
  1841. this->flags &= ~GPMI_TIMING_INIT_OK;
  1842. ret = gpmi_init(this);
  1843. if (ret) {
  1844. dev_err(this->dev, "Error setting GPMI : %d\n", ret);
  1845. return ret;
  1846. }
  1847. /* re-init the BCH registers */
  1848. ret = bch_set_geometry(this);
  1849. if (ret) {
  1850. dev_err(this->dev, "Error setting BCH : %d\n", ret);
  1851. return ret;
  1852. }
  1853. /* re-init others */
  1854. gpmi_extra_init(this);
  1855. return 0;
  1856. }
  1857. #endif /* CONFIG_PM_SLEEP */
  1858. static const struct dev_pm_ops gpmi_pm_ops = {
  1859. SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
  1860. };
  1861. static struct platform_driver gpmi_nand_driver = {
  1862. .driver = {
  1863. .name = "gpmi-nand",
  1864. .pm = &gpmi_pm_ops,
  1865. .of_match_table = gpmi_nand_id_table,
  1866. },
  1867. .probe = gpmi_nand_probe,
  1868. .remove = gpmi_nand_remove,
  1869. };
  1870. module_platform_driver(gpmi_nand_driver);
  1871. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1872. MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
  1873. MODULE_LICENSE("GPL");