gpmi-nand.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale GPMI NAND Flash Driver
  4. *
  5. * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
  6. * Copyright (C) 2008 Embedded Alley Solutions, Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <linux/sched/task_stack.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/mtd/partitions.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/dma/mxs-dma.h>
  19. #include "gpmi-nand.h"
  20. #include "gpmi-regs.h"
  21. #include "bch-regs.h"
  22. /* Resource names for the GPMI NAND driver. */
  23. #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
  24. #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
  25. #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
  26. /* Converts time to clock cycles */
  27. #define TO_CYCLES(duration, period) DIV_ROUND_UP_ULL(duration, period)
  28. #define MXS_SET_ADDR 0x4
  29. #define MXS_CLR_ADDR 0x8
  30. /*
  31. * Clear the bit and poll it cleared. This is usually called with
  32. * a reset address and mask being either SFTRST(bit 31) or CLKGATE
  33. * (bit 30).
  34. */
  35. static int clear_poll_bit(void __iomem *addr, u32 mask)
  36. {
  37. int timeout = 0x400;
  38. /* clear the bit */
  39. writel(mask, addr + MXS_CLR_ADDR);
  40. /*
  41. * SFTRST needs 3 GPMI clocks to settle, the reference manual
  42. * recommends to wait 1us.
  43. */
  44. udelay(1);
  45. /* poll the bit becoming clear */
  46. while ((readl(addr) & mask) && --timeout)
  47. /* nothing */;
  48. return !timeout;
  49. }
  50. #define MODULE_CLKGATE (1 << 30)
  51. #define MODULE_SFTRST (1 << 31)
  52. /*
  53. * The current mxs_reset_block() will do two things:
  54. * [1] enable the module.
  55. * [2] reset the module.
  56. *
  57. * In most of the cases, it's ok.
  58. * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
  59. * If you try to soft reset the BCH block, it becomes unusable until
  60. * the next hard reset. This case occurs in the NAND boot mode. When the board
  61. * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
  62. * So If the driver tries to reset the BCH again, the BCH will not work anymore.
  63. * You will see a DMA timeout in this case. The bug has been fixed
  64. * in the following chips, such as MX28.
  65. *
  66. * To avoid this bug, just add a new parameter `just_enable` for
  67. * the mxs_reset_block(), and rewrite it here.
  68. */
  69. static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
  70. {
  71. int ret;
  72. int timeout = 0x400;
  73. /* clear and poll SFTRST */
  74. ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
  75. if (unlikely(ret))
  76. goto error;
  77. /* clear CLKGATE */
  78. writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
  79. if (!just_enable) {
  80. /* set SFTRST to reset the block */
  81. writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
  82. udelay(1);
  83. /* poll CLKGATE becoming set */
  84. while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
  85. /* nothing */;
  86. if (unlikely(!timeout))
  87. goto error;
  88. }
  89. /* clear and poll SFTRST */
  90. ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
  91. if (unlikely(ret))
  92. goto error;
  93. /* clear and poll CLKGATE */
  94. ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
  95. if (unlikely(ret))
  96. goto error;
  97. return 0;
  98. error:
  99. pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
  100. return -ETIMEDOUT;
  101. }
  102. static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
  103. {
  104. struct clk *clk;
  105. int ret;
  106. int i;
  107. for (i = 0; i < GPMI_CLK_MAX; i++) {
  108. clk = this->resources.clock[i];
  109. if (!clk)
  110. break;
  111. if (v) {
  112. ret = clk_prepare_enable(clk);
  113. if (ret)
  114. goto err_clk;
  115. } else {
  116. clk_disable_unprepare(clk);
  117. }
  118. }
  119. return 0;
  120. err_clk:
  121. for (; i > 0; i--)
  122. clk_disable_unprepare(this->resources.clock[i - 1]);
  123. return ret;
  124. }
  125. static int gpmi_init(struct gpmi_nand_data *this)
  126. {
  127. struct resources *r = &this->resources;
  128. int ret;
  129. ret = pm_runtime_get_sync(this->dev);
  130. if (ret < 0) {
  131. pm_runtime_put_noidle(this->dev);
  132. return ret;
  133. }
  134. ret = gpmi_reset_block(r->gpmi_regs, false);
  135. if (ret)
  136. goto err_out;
  137. /*
  138. * Reset BCH here, too. We got failures otherwise :(
  139. * See later BCH reset for explanation of MX23 and MX28 handling
  140. */
  141. ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this));
  142. if (ret)
  143. goto err_out;
  144. /* Choose NAND mode. */
  145. writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
  146. /* Set the IRQ polarity. */
  147. writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
  148. r->gpmi_regs + HW_GPMI_CTRL1_SET);
  149. /* Disable Write-Protection. */
  150. writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  151. /* Select BCH ECC. */
  152. writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  153. /*
  154. * Decouple the chip select from dma channel. We use dma0 for all
  155. * the chips, force all NAND RDY_BUSY inputs to be sourced from
  156. * RDY_BUSY0.
  157. */
  158. writel(BM_GPMI_CTRL1_DECOUPLE_CS | BM_GPMI_CTRL1_GANGED_RDYBUSY,
  159. r->gpmi_regs + HW_GPMI_CTRL1_SET);
  160. err_out:
  161. pm_runtime_mark_last_busy(this->dev);
  162. pm_runtime_put_autosuspend(this->dev);
  163. return ret;
  164. }
  165. /* This function is very useful. It is called only when the bug occur. */
  166. static void gpmi_dump_info(struct gpmi_nand_data *this)
  167. {
  168. struct resources *r = &this->resources;
  169. struct bch_geometry *geo = &this->bch_geometry;
  170. u32 reg;
  171. int i;
  172. dev_err(this->dev, "Show GPMI registers :\n");
  173. for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
  174. reg = readl(r->gpmi_regs + i * 0x10);
  175. dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
  176. }
  177. /* start to print out the BCH info */
  178. dev_err(this->dev, "Show BCH registers :\n");
  179. for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) {
  180. reg = readl(r->bch_regs + i * 0x10);
  181. dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
  182. }
  183. dev_err(this->dev, "BCH Geometry :\n"
  184. "GF length : %u\n"
  185. "ECC Strength : %u\n"
  186. "Page Size in Bytes : %u\n"
  187. "Metadata Size in Bytes : %u\n"
  188. "ECC0 Chunk Size in Bytes: %u\n"
  189. "ECCn Chunk Size in Bytes: %u\n"
  190. "ECC Chunk Count : %u\n"
  191. "Payload Size in Bytes : %u\n"
  192. "Auxiliary Size in Bytes: %u\n"
  193. "Auxiliary Status Offset: %u\n"
  194. "Block Mark Byte Offset : %u\n"
  195. "Block Mark Bit Offset : %u\n",
  196. geo->gf_len,
  197. geo->ecc_strength,
  198. geo->page_size,
  199. geo->metadata_size,
  200. geo->ecc0_chunk_size,
  201. geo->eccn_chunk_size,
  202. geo->ecc_chunk_count,
  203. geo->payload_size,
  204. geo->auxiliary_size,
  205. geo->auxiliary_status_offset,
  206. geo->block_mark_byte_offset,
  207. geo->block_mark_bit_offset);
  208. }
  209. static bool gpmi_check_ecc(struct gpmi_nand_data *this)
  210. {
  211. struct nand_chip *chip = &this->nand;
  212. struct bch_geometry *geo = &this->bch_geometry;
  213. struct nand_device *nand = &chip->base;
  214. struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
  215. conf->step_size = geo->eccn_chunk_size;
  216. conf->strength = geo->ecc_strength;
  217. /* Do the sanity check. */
  218. if (GPMI_IS_MXS(this)) {
  219. /* The mx23/mx28 only support the GF13. */
  220. if (geo->gf_len == 14)
  221. return false;
  222. }
  223. if (geo->ecc_strength > this->devdata->bch_max_ecc_strength)
  224. return false;
  225. if (!nand_ecc_is_strong_enough(nand))
  226. return false;
  227. return true;
  228. }
  229. /* check if bbm locates in data chunk rather than ecc chunk */
  230. static bool bbm_in_data_chunk(struct gpmi_nand_data *this,
  231. unsigned int *chunk_num)
  232. {
  233. struct bch_geometry *geo = &this->bch_geometry;
  234. struct nand_chip *chip = &this->nand;
  235. struct mtd_info *mtd = nand_to_mtd(chip);
  236. unsigned int i, j;
  237. if (geo->ecc0_chunk_size != geo->eccn_chunk_size) {
  238. dev_err(this->dev,
  239. "The size of ecc0_chunk must equal to eccn_chunk\n");
  240. return false;
  241. }
  242. i = (mtd->writesize * 8 - geo->metadata_size * 8) /
  243. (geo->gf_len * geo->ecc_strength +
  244. geo->eccn_chunk_size * 8);
  245. j = (mtd->writesize * 8 - geo->metadata_size * 8) -
  246. (geo->gf_len * geo->ecc_strength +
  247. geo->eccn_chunk_size * 8) * i;
  248. if (j < geo->eccn_chunk_size * 8) {
  249. *chunk_num = i+1;
  250. dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n",
  251. geo->ecc_strength, *chunk_num);
  252. return true;
  253. }
  254. return false;
  255. }
  256. /*
  257. * If we can get the ECC information from the nand chip, we do not
  258. * need to calculate them ourselves.
  259. *
  260. * We may have available oob space in this case.
  261. */
  262. static int set_geometry_by_ecc_info(struct gpmi_nand_data *this,
  263. unsigned int ecc_strength,
  264. unsigned int ecc_step)
  265. {
  266. struct bch_geometry *geo = &this->bch_geometry;
  267. struct nand_chip *chip = &this->nand;
  268. struct mtd_info *mtd = nand_to_mtd(chip);
  269. unsigned int block_mark_bit_offset;
  270. switch (ecc_step) {
  271. case SZ_512:
  272. geo->gf_len = 13;
  273. break;
  274. case SZ_1K:
  275. geo->gf_len = 14;
  276. break;
  277. default:
  278. dev_err(this->dev,
  279. "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
  280. nanddev_get_ecc_requirements(&chip->base)->strength,
  281. nanddev_get_ecc_requirements(&chip->base)->step_size);
  282. return -EINVAL;
  283. }
  284. geo->ecc0_chunk_size = ecc_step;
  285. geo->eccn_chunk_size = ecc_step;
  286. geo->ecc_strength = round_up(ecc_strength, 2);
  287. if (!gpmi_check_ecc(this))
  288. return -EINVAL;
  289. /* Keep the C >= O */
  290. if (geo->eccn_chunk_size < mtd->oobsize) {
  291. dev_err(this->dev,
  292. "unsupported nand chip. ecc size: %d, oob size : %d\n",
  293. ecc_step, mtd->oobsize);
  294. return -EINVAL;
  295. }
  296. /* The default value, see comment in the legacy_set_geometry(). */
  297. geo->metadata_size = 10;
  298. geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
  299. /*
  300. * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
  301. *
  302. * | P |
  303. * |<----------------------------------------------------->|
  304. * | |
  305. * | (Block Mark) |
  306. * | P' | | | |
  307. * |<-------------------------------------------->| D | | O' |
  308. * | |<---->| |<--->|
  309. * V V V V V
  310. * +---+----------+-+----------+-+----------+-+----------+-+-----+
  311. * | M | data |E| data |E| data |E| data |E| |
  312. * +---+----------+-+----------+-+----------+-+----------+-+-----+
  313. * ^ ^
  314. * | O |
  315. * |<------------>|
  316. * | |
  317. *
  318. * P : the page size for BCH module.
  319. * E : The ECC strength.
  320. * G : the length of Galois Field.
  321. * N : The chunk count of per page.
  322. * M : the metasize of per page.
  323. * C : the ecc chunk size, aka the "data" above.
  324. * P': the nand chip's page size.
  325. * O : the nand chip's oob size.
  326. * O': the free oob.
  327. *
  328. * The formula for P is :
  329. *
  330. * E * G * N
  331. * P = ------------ + P' + M
  332. * 8
  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. * Please see the comment in legacy_set_geometry().
  342. * With the condition C >= O , we still can get same result.
  343. * So the bit position of the physical block mark within the ECC-based
  344. * view of the page is :
  345. * (P' - D) * 8
  346. */
  347. geo->page_size = mtd->writesize + geo->metadata_size +
  348. (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
  349. geo->payload_size = mtd->writesize;
  350. geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
  351. geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
  352. + ALIGN(geo->ecc_chunk_count, 4);
  353. if (!this->swap_block_mark)
  354. return 0;
  355. /* For bit swap. */
  356. block_mark_bit_offset = mtd->writesize * 8 -
  357. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  358. + geo->metadata_size * 8);
  359. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  360. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  361. return 0;
  362. }
  363. /*
  364. * Calculate the ECC strength by hand:
  365. * E : The ECC strength.
  366. * G : the length of Galois Field.
  367. * N : The chunk count of per page.
  368. * O : the oobsize of the NAND chip.
  369. * M : the metasize of per page.
  370. *
  371. * The formula is :
  372. * E * G * N
  373. * ------------ <= (O - M)
  374. * 8
  375. *
  376. * So, we get E by:
  377. * (O - M) * 8
  378. * E <= -------------
  379. * G * N
  380. */
  381. static inline int get_ecc_strength(struct gpmi_nand_data *this)
  382. {
  383. struct bch_geometry *geo = &this->bch_geometry;
  384. struct mtd_info *mtd = nand_to_mtd(&this->nand);
  385. int ecc_strength;
  386. ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
  387. / (geo->gf_len * geo->ecc_chunk_count);
  388. /* We need the minor even number. */
  389. return round_down(ecc_strength, 2);
  390. }
  391. static int set_geometry_for_large_oob(struct gpmi_nand_data *this)
  392. {
  393. struct bch_geometry *geo = &this->bch_geometry;
  394. struct nand_chip *chip = &this->nand;
  395. struct mtd_info *mtd = nand_to_mtd(chip);
  396. const struct nand_ecc_props *requirements =
  397. nanddev_get_ecc_requirements(&chip->base);
  398. unsigned int block_mark_bit_offset;
  399. unsigned int max_ecc;
  400. unsigned int bbm_chunk;
  401. unsigned int i;
  402. /* sanity check for the minimum ecc nand required */
  403. if (!(requirements->strength > 0 &&
  404. requirements->step_size > 0))
  405. return -EINVAL;
  406. geo->ecc_strength = requirements->strength;
  407. /* check if platform can support this nand */
  408. if (!gpmi_check_ecc(this)) {
  409. dev_err(this->dev,
  410. "unsupported NAND chip, minimum ecc required %d\n",
  411. geo->ecc_strength);
  412. return -EINVAL;
  413. }
  414. /* calculate the maximum ecc platform can support*/
  415. geo->metadata_size = 10;
  416. geo->gf_len = 14;
  417. geo->ecc0_chunk_size = 1024;
  418. geo->eccn_chunk_size = 1024;
  419. geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
  420. max_ecc = min(get_ecc_strength(this),
  421. this->devdata->bch_max_ecc_strength);
  422. /*
  423. * search a supported ecc strength that makes bbm
  424. * located in data chunk
  425. */
  426. geo->ecc_strength = max_ecc;
  427. while (!(geo->ecc_strength < requirements->strength)) {
  428. if (bbm_in_data_chunk(this, &bbm_chunk))
  429. goto geo_setting;
  430. geo->ecc_strength -= 2;
  431. }
  432. /* if none of them works, keep using the minimum ecc */
  433. /* nand required but changing ecc page layout */
  434. geo->ecc_strength = requirements->strength;
  435. /* add extra ecc for meta data */
  436. geo->ecc0_chunk_size = 0;
  437. geo->ecc_chunk_count = (mtd->writesize / geo->eccn_chunk_size) + 1;
  438. geo->ecc_for_meta = 1;
  439. /* check if oob can afford this extra ecc chunk */
  440. if (mtd->oobsize * 8 < geo->metadata_size * 8 +
  441. geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) {
  442. dev_err(this->dev, "unsupported NAND chip with new layout\n");
  443. return -EINVAL;
  444. }
  445. /* calculate in which chunk bbm located */
  446. bbm_chunk = (mtd->writesize * 8 - geo->metadata_size * 8 -
  447. geo->gf_len * geo->ecc_strength) /
  448. (geo->gf_len * geo->ecc_strength +
  449. geo->eccn_chunk_size * 8) + 1;
  450. geo_setting:
  451. geo->page_size = mtd->writesize + geo->metadata_size +
  452. (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
  453. geo->payload_size = mtd->writesize;
  454. /*
  455. * The auxiliary buffer contains the metadata and the ECC status. The
  456. * metadata is padded to the nearest 32-bit boundary. The ECC status
  457. * contains one byte for every ECC chunk, and is also padded to the
  458. * nearest 32-bit boundary.
  459. */
  460. geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
  461. geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
  462. + ALIGN(geo->ecc_chunk_count, 4);
  463. if (!this->swap_block_mark)
  464. return 0;
  465. /* calculate the number of ecc chunk behind the bbm */
  466. i = (mtd->writesize / geo->eccn_chunk_size) - bbm_chunk + 1;
  467. block_mark_bit_offset = mtd->writesize * 8 -
  468. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
  469. + geo->metadata_size * 8);
  470. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  471. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  472. dev_dbg(this->dev, "BCH Geometry :\n"
  473. "GF length : %u\n"
  474. "ECC Strength : %u\n"
  475. "Page Size in Bytes : %u\n"
  476. "Metadata Size in Bytes : %u\n"
  477. "ECC0 Chunk Size in Bytes: %u\n"
  478. "ECCn Chunk Size in Bytes: %u\n"
  479. "ECC Chunk Count : %u\n"
  480. "Payload Size in Bytes : %u\n"
  481. "Auxiliary Size in Bytes: %u\n"
  482. "Auxiliary Status Offset: %u\n"
  483. "Block Mark Byte Offset : %u\n"
  484. "Block Mark Bit Offset : %u\n"
  485. "Block Mark in chunk : %u\n"
  486. "Ecc for Meta data : %u\n",
  487. geo->gf_len,
  488. geo->ecc_strength,
  489. geo->page_size,
  490. geo->metadata_size,
  491. geo->ecc0_chunk_size,
  492. geo->eccn_chunk_size,
  493. geo->ecc_chunk_count,
  494. geo->payload_size,
  495. geo->auxiliary_size,
  496. geo->auxiliary_status_offset,
  497. geo->block_mark_byte_offset,
  498. geo->block_mark_bit_offset,
  499. bbm_chunk,
  500. geo->ecc_for_meta);
  501. return 0;
  502. }
  503. static int legacy_set_geometry(struct gpmi_nand_data *this)
  504. {
  505. struct bch_geometry *geo = &this->bch_geometry;
  506. struct mtd_info *mtd = nand_to_mtd(&this->nand);
  507. unsigned int metadata_size;
  508. unsigned int status_size;
  509. unsigned int block_mark_bit_offset;
  510. /*
  511. * The size of the metadata can be changed, though we set it to 10
  512. * bytes now. But it can't be too large, because we have to save
  513. * enough space for BCH.
  514. */
  515. geo->metadata_size = 10;
  516. /* The default for the length of Galois Field. */
  517. geo->gf_len = 13;
  518. /* The default for chunk size. */
  519. geo->ecc0_chunk_size = 512;
  520. geo->eccn_chunk_size = 512;
  521. while (geo->eccn_chunk_size < mtd->oobsize) {
  522. geo->ecc0_chunk_size *= 2; /* keep C >= O */
  523. geo->eccn_chunk_size *= 2; /* keep C >= O */
  524. geo->gf_len = 14;
  525. }
  526. geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
  527. /* We use the same ECC strength for all chunks. */
  528. geo->ecc_strength = get_ecc_strength(this);
  529. if (!gpmi_check_ecc(this)) {
  530. dev_err(this->dev,
  531. "ecc strength: %d cannot be supported by the controller (%d)\n"
  532. "try to use minimum ecc strength that NAND chip required\n",
  533. geo->ecc_strength,
  534. this->devdata->bch_max_ecc_strength);
  535. return -EINVAL;
  536. }
  537. geo->page_size = mtd->writesize + geo->metadata_size +
  538. (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
  539. geo->payload_size = mtd->writesize;
  540. /*
  541. * The auxiliary buffer contains the metadata and the ECC status. The
  542. * metadata is padded to the nearest 32-bit boundary. The ECC status
  543. * contains one byte for every ECC chunk, and is also padded to the
  544. * nearest 32-bit boundary.
  545. */
  546. metadata_size = ALIGN(geo->metadata_size, 4);
  547. status_size = ALIGN(geo->ecc_chunk_count, 4);
  548. geo->auxiliary_size = metadata_size + status_size;
  549. geo->auxiliary_status_offset = metadata_size;
  550. if (!this->swap_block_mark)
  551. return 0;
  552. /*
  553. * We need to compute the byte and bit offsets of
  554. * the physical block mark within the ECC-based view of the page.
  555. *
  556. * NAND chip with 2K page shows below:
  557. * (Block Mark)
  558. * | |
  559. * | D |
  560. * |<---->|
  561. * V V
  562. * +---+----------+-+----------+-+----------+-+----------+-+
  563. * | M | data |E| data |E| data |E| data |E|
  564. * +---+----------+-+----------+-+----------+-+----------+-+
  565. *
  566. * The position of block mark moves forward in the ECC-based view
  567. * of page, and the delta is:
  568. *
  569. * E * G * (N - 1)
  570. * D = (---------------- + M)
  571. * 8
  572. *
  573. * With the formula to compute the ECC strength, and the condition
  574. * : C >= O (C is the ecc chunk size)
  575. *
  576. * It's easy to deduce to the following result:
  577. *
  578. * E * G (O - M) C - M C - M
  579. * ----------- <= ------- <= -------- < ---------
  580. * 8 N N (N - 1)
  581. *
  582. * So, we get:
  583. *
  584. * E * G * (N - 1)
  585. * D = (---------------- + M) < C
  586. * 8
  587. *
  588. * The above inequality means the position of block mark
  589. * within the ECC-based view of the page is still in the data chunk,
  590. * and it's NOT in the ECC bits of the chunk.
  591. *
  592. * Use the following to compute the bit position of the
  593. * physical block mark within the ECC-based view of the page:
  594. * (page_size - D) * 8
  595. *
  596. * --Huang Shijie
  597. */
  598. block_mark_bit_offset = mtd->writesize * 8 -
  599. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  600. + geo->metadata_size * 8);
  601. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  602. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  603. return 0;
  604. }
  605. static int common_nfc_set_geometry(struct gpmi_nand_data *this)
  606. {
  607. struct nand_chip *chip = &this->nand;
  608. struct mtd_info *mtd = nand_to_mtd(&this->nand);
  609. const struct nand_ecc_props *requirements =
  610. nanddev_get_ecc_requirements(&chip->base);
  611. bool use_minimun_ecc;
  612. int err;
  613. use_minimun_ecc = of_property_read_bool(this->dev->of_node,
  614. "fsl,use-minimum-ecc");
  615. /* use legacy bch geometry settings by default*/
  616. if ((!use_minimun_ecc && mtd->oobsize < 1024) ||
  617. !(requirements->strength > 0 && requirements->step_size > 0)) {
  618. dev_dbg(this->dev, "use legacy bch geometry\n");
  619. err = legacy_set_geometry(this);
  620. if (!err)
  621. return 0;
  622. }
  623. /* for large oob nand */
  624. if (mtd->oobsize > 1024) {
  625. dev_dbg(this->dev, "use large oob bch geometry\n");
  626. err = set_geometry_for_large_oob(this);
  627. if (!err)
  628. return 0;
  629. }
  630. /* otherwise use the minimum ecc nand chip required */
  631. dev_dbg(this->dev, "use minimum ecc bch geometry\n");
  632. err = set_geometry_by_ecc_info(this, requirements->strength,
  633. requirements->step_size);
  634. if (err)
  635. dev_err(this->dev, "none of the bch geometry setting works\n");
  636. return err;
  637. }
  638. /* Configures the geometry for BCH. */
  639. static int bch_set_geometry(struct gpmi_nand_data *this)
  640. {
  641. struct resources *r = &this->resources;
  642. int ret;
  643. ret = common_nfc_set_geometry(this);
  644. if (ret)
  645. return ret;
  646. ret = pm_runtime_get_sync(this->dev);
  647. if (ret < 0) {
  648. pm_runtime_put_autosuspend(this->dev);
  649. return ret;
  650. }
  651. /*
  652. * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
  653. * chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
  654. * and MX28.
  655. */
  656. ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this));
  657. if (ret)
  658. goto err_out;
  659. /* Set *all* chip selects to use layout 0. */
  660. writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
  661. ret = 0;
  662. err_out:
  663. pm_runtime_mark_last_busy(this->dev);
  664. pm_runtime_put_autosuspend(this->dev);
  665. return ret;
  666. }
  667. /*
  668. * <1> Firstly, we should know what's the GPMI-clock means.
  669. * The GPMI-clock is the internal clock in the gpmi nand controller.
  670. * If you set 100MHz to gpmi nand controller, the GPMI-clock's period
  671. * is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
  672. *
  673. * <2> Secondly, we should know what's the frequency on the nand chip pins.
  674. * The frequency on the nand chip pins is derived from the GPMI-clock.
  675. * We can get it from the following equation:
  676. *
  677. * F = G / (DS + DH)
  678. *
  679. * F : the frequency on the nand chip pins.
  680. * G : the GPMI clock, such as 100MHz.
  681. * DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
  682. * DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
  683. *
  684. * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
  685. * the nand EDO(extended Data Out) timing could be applied.
  686. * The GPMI implements a feedback read strobe to sample the read data.
  687. * The feedback read strobe can be delayed to support the nand EDO timing
  688. * where the read strobe may deasserts before the read data is valid, and
  689. * read data is valid for some time after read strobe.
  690. *
  691. * The following figure illustrates some aspects of a NAND Flash read:
  692. *
  693. * |<---tREA---->|
  694. * | |
  695. * | | |
  696. * |<--tRP-->| |
  697. * | | |
  698. * __ ___|__________________________________
  699. * RDN \________/ |
  700. * |
  701. * /---------\
  702. * Read Data --------------< >---------
  703. * \---------/
  704. * | |
  705. * |<-D->|
  706. * FeedbackRDN ________ ____________
  707. * \___________/
  708. *
  709. * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
  710. *
  711. *
  712. * <4> Now, we begin to describe how to compute the right RDN_DELAY.
  713. *
  714. * 4.1) From the aspect of the nand chip pins:
  715. * Delay = (tREA + C - tRP) {1}
  716. *
  717. * tREA : the maximum read access time.
  718. * C : a constant to adjust the delay. default is 4000ps.
  719. * tRP : the read pulse width, which is exactly:
  720. * tRP = (GPMI-clock-period) * DATA_SETUP
  721. *
  722. * 4.2) From the aspect of the GPMI nand controller:
  723. * Delay = RDN_DELAY * 0.125 * RP {2}
  724. *
  725. * RP : the DLL reference period.
  726. * if (GPMI-clock-period > DLL_THRETHOLD)
  727. * RP = GPMI-clock-period / 2;
  728. * else
  729. * RP = GPMI-clock-period;
  730. *
  731. * Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
  732. * is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
  733. * is 16000ps, but in mx6q, we use 12000ps.
  734. *
  735. * 4.3) since {1} equals {2}, we get:
  736. *
  737. * (tREA + 4000 - tRP) * 8
  738. * RDN_DELAY = ----------------------- {3}
  739. * RP
  740. */
  741. static int gpmi_nfc_compute_timings(struct gpmi_nand_data *this,
  742. const struct nand_sdr_timings *sdr)
  743. {
  744. struct gpmi_nfc_hardware_timing *hw = &this->hw;
  745. struct resources *r = &this->resources;
  746. unsigned int dll_threshold_ps = this->devdata->max_chain_delay;
  747. unsigned int period_ps, reference_period_ps;
  748. unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles;
  749. unsigned int tRP_ps;
  750. bool use_half_period;
  751. int sample_delay_ps, sample_delay_factor;
  752. unsigned int busy_timeout_cycles;
  753. u8 wrn_dly_sel;
  754. unsigned long clk_rate, min_rate;
  755. u64 busy_timeout_ps;
  756. if (sdr->tRC_min >= 30000) {
  757. /* ONFI non-EDO modes [0-3] */
  758. hw->clk_rate = 22000000;
  759. min_rate = 0;
  760. wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
  761. } else if (sdr->tRC_min >= 25000) {
  762. /* ONFI EDO mode 4 */
  763. hw->clk_rate = 80000000;
  764. min_rate = 22000000;
  765. wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
  766. } else {
  767. /* ONFI EDO mode 5 */
  768. hw->clk_rate = 100000000;
  769. min_rate = 80000000;
  770. wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
  771. }
  772. clk_rate = clk_round_rate(r->clock[0], hw->clk_rate);
  773. if (clk_rate <= min_rate) {
  774. dev_err(this->dev, "clock setting: expected %ld, got %ld\n",
  775. hw->clk_rate, clk_rate);
  776. return -ENOTSUPP;
  777. }
  778. hw->clk_rate = clk_rate;
  779. /* SDR core timings are given in picoseconds */
  780. period_ps = div_u64((u64)NSEC_PER_SEC * 1000, hw->clk_rate);
  781. addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps);
  782. data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps);
  783. data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps);
  784. busy_timeout_ps = max(sdr->tBERS_max, sdr->tPROG_max);
  785. busy_timeout_cycles = TO_CYCLES(busy_timeout_ps, period_ps);
  786. hw->timing0 = BF_GPMI_TIMING0_ADDRESS_SETUP(addr_setup_cycles) |
  787. BF_GPMI_TIMING0_DATA_HOLD(data_hold_cycles) |
  788. BF_GPMI_TIMING0_DATA_SETUP(data_setup_cycles);
  789. hw->timing1 = BF_GPMI_TIMING1_BUSY_TIMEOUT(DIV_ROUND_UP(busy_timeout_cycles, 4096));
  790. /*
  791. * Derive NFC ideal delay from {3}:
  792. *
  793. * (tREA + 4000 - tRP) * 8
  794. * RDN_DELAY = -----------------------
  795. * RP
  796. */
  797. if (period_ps > dll_threshold_ps) {
  798. use_half_period = true;
  799. reference_period_ps = period_ps / 2;
  800. } else {
  801. use_half_period = false;
  802. reference_period_ps = period_ps;
  803. }
  804. tRP_ps = data_setup_cycles * period_ps;
  805. sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8;
  806. if (sample_delay_ps > 0)
  807. sample_delay_factor = sample_delay_ps / reference_period_ps;
  808. else
  809. sample_delay_factor = 0;
  810. hw->ctrl1n = BF_GPMI_CTRL1_WRN_DLY_SEL(wrn_dly_sel);
  811. if (sample_delay_factor)
  812. hw->ctrl1n |= BF_GPMI_CTRL1_RDN_DELAY(sample_delay_factor) |
  813. BM_GPMI_CTRL1_DLL_ENABLE |
  814. (use_half_period ? BM_GPMI_CTRL1_HALF_PERIOD : 0);
  815. return 0;
  816. }
  817. static int gpmi_nfc_apply_timings(struct gpmi_nand_data *this)
  818. {
  819. struct gpmi_nfc_hardware_timing *hw = &this->hw;
  820. struct resources *r = &this->resources;
  821. void __iomem *gpmi_regs = r->gpmi_regs;
  822. unsigned int dll_wait_time_us;
  823. int ret;
  824. /* Clock dividers do NOT guarantee a clean clock signal on its output
  825. * during the change of the divide factor on i.MX6Q/UL/SX. On i.MX7/8,
  826. * all clock dividers provide these guarantee.
  827. */
  828. if (GPMI_IS_MX6Q(this) || GPMI_IS_MX6SX(this))
  829. clk_disable_unprepare(r->clock[0]);
  830. ret = clk_set_rate(r->clock[0], hw->clk_rate);
  831. if (ret) {
  832. dev_err(this->dev, "cannot set clock rate to %lu Hz: %d\n", hw->clk_rate, ret);
  833. return ret;
  834. }
  835. if (GPMI_IS_MX6Q(this) || GPMI_IS_MX6SX(this)) {
  836. ret = clk_prepare_enable(r->clock[0]);
  837. if (ret)
  838. return ret;
  839. }
  840. writel(hw->timing0, gpmi_regs + HW_GPMI_TIMING0);
  841. writel(hw->timing1, gpmi_regs + HW_GPMI_TIMING1);
  842. /*
  843. * Clear several CTRL1 fields, DLL must be disabled when setting
  844. * RDN_DELAY or HALF_PERIOD.
  845. */
  846. writel(BM_GPMI_CTRL1_CLEAR_MASK, gpmi_regs + HW_GPMI_CTRL1_CLR);
  847. writel(hw->ctrl1n, gpmi_regs + HW_GPMI_CTRL1_SET);
  848. /* Wait 64 clock cycles before using the GPMI after enabling the DLL */
  849. dll_wait_time_us = USEC_PER_SEC / hw->clk_rate * 64;
  850. if (!dll_wait_time_us)
  851. dll_wait_time_us = 1;
  852. /* Wait for the DLL to settle. */
  853. udelay(dll_wait_time_us);
  854. return 0;
  855. }
  856. static int gpmi_setup_interface(struct nand_chip *chip, int chipnr,
  857. const struct nand_interface_config *conf)
  858. {
  859. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  860. const struct nand_sdr_timings *sdr;
  861. int ret;
  862. /* Retrieve required NAND timings */
  863. sdr = nand_get_sdr_timings(conf);
  864. if (IS_ERR(sdr))
  865. return PTR_ERR(sdr);
  866. /* Only MX28/MX6 GPMI controller can reach EDO timings */
  867. if (sdr->tRC_min <= 25000 && !GPMI_IS_MX28(this) && !GPMI_IS_MX6(this))
  868. return -ENOTSUPP;
  869. /* Stop here if this call was just a check */
  870. if (chipnr < 0)
  871. return 0;
  872. /* Do the actual derivation of the controller timings */
  873. ret = gpmi_nfc_compute_timings(this, sdr);
  874. if (ret)
  875. return ret;
  876. this->hw.must_apply_timings = true;
  877. return 0;
  878. }
  879. /* Clears a BCH interrupt. */
  880. static void gpmi_clear_bch(struct gpmi_nand_data *this)
  881. {
  882. struct resources *r = &this->resources;
  883. writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
  884. }
  885. static struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
  886. {
  887. /* We use the DMA channel 0 to access all the nand chips. */
  888. return this->dma_chans[0];
  889. }
  890. /* This will be called after the DMA operation is finished. */
  891. static void dma_irq_callback(void *param)
  892. {
  893. struct gpmi_nand_data *this = param;
  894. struct completion *dma_c = &this->dma_done;
  895. complete(dma_c);
  896. }
  897. static irqreturn_t bch_irq(int irq, void *cookie)
  898. {
  899. struct gpmi_nand_data *this = cookie;
  900. gpmi_clear_bch(this);
  901. complete(&this->bch_done);
  902. return IRQ_HANDLED;
  903. }
  904. static int gpmi_raw_len_to_len(struct gpmi_nand_data *this, int raw_len)
  905. {
  906. /*
  907. * raw_len is the length to read/write including bch data which
  908. * we are passed in exec_op. Calculate the data length from it.
  909. */
  910. if (this->bch)
  911. return ALIGN_DOWN(raw_len, this->bch_geometry.eccn_chunk_size);
  912. else
  913. return raw_len;
  914. }
  915. /* Can we use the upper's buffer directly for DMA? */
  916. static bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf,
  917. int raw_len, struct scatterlist *sgl,
  918. enum dma_data_direction dr)
  919. {
  920. int ret;
  921. int len = gpmi_raw_len_to_len(this, raw_len);
  922. /* first try to map the upper buffer directly */
  923. if (virt_addr_valid(buf) && !object_is_on_stack(buf)) {
  924. sg_init_one(sgl, buf, len);
  925. ret = dma_map_sg(this->dev, sgl, 1, dr);
  926. if (ret == 0)
  927. goto map_fail;
  928. return true;
  929. }
  930. map_fail:
  931. /* We have to use our own DMA buffer. */
  932. sg_init_one(sgl, this->data_buffer_dma, len);
  933. if (dr == DMA_TO_DEVICE && buf != this->data_buffer_dma)
  934. memcpy(this->data_buffer_dma, buf, len);
  935. dma_map_sg(this->dev, sgl, 1, dr);
  936. return false;
  937. }
  938. /* add our owner bbt descriptor */
  939. static uint8_t scan_ff_pattern[] = { 0xff };
  940. static struct nand_bbt_descr gpmi_bbt_descr = {
  941. .options = 0,
  942. .offs = 0,
  943. .len = 1,
  944. .pattern = scan_ff_pattern
  945. };
  946. /*
  947. * We may change the layout if we can get the ECC info from the datasheet,
  948. * else we will use all the (page + OOB).
  949. */
  950. static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
  951. struct mtd_oob_region *oobregion)
  952. {
  953. struct nand_chip *chip = mtd_to_nand(mtd);
  954. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  955. struct bch_geometry *geo = &this->bch_geometry;
  956. if (section)
  957. return -ERANGE;
  958. oobregion->offset = 0;
  959. oobregion->length = geo->page_size - mtd->writesize;
  960. return 0;
  961. }
  962. static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
  963. struct mtd_oob_region *oobregion)
  964. {
  965. struct nand_chip *chip = mtd_to_nand(mtd);
  966. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  967. struct bch_geometry *geo = &this->bch_geometry;
  968. if (section)
  969. return -ERANGE;
  970. /* The available oob size we have. */
  971. if (geo->page_size < mtd->writesize + mtd->oobsize) {
  972. oobregion->offset = geo->page_size - mtd->writesize;
  973. oobregion->length = mtd->oobsize - oobregion->offset;
  974. }
  975. return 0;
  976. }
  977. static const char * const gpmi_clks_for_mx2x[] = {
  978. "gpmi_io",
  979. };
  980. static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
  981. .ecc = gpmi_ooblayout_ecc,
  982. .free = gpmi_ooblayout_free,
  983. };
  984. static const struct gpmi_devdata gpmi_devdata_imx23 = {
  985. .type = IS_MX23,
  986. .bch_max_ecc_strength = 20,
  987. .max_chain_delay = 16000,
  988. .clks = gpmi_clks_for_mx2x,
  989. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
  990. };
  991. static const struct gpmi_devdata gpmi_devdata_imx28 = {
  992. .type = IS_MX28,
  993. .bch_max_ecc_strength = 20,
  994. .max_chain_delay = 16000,
  995. .clks = gpmi_clks_for_mx2x,
  996. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
  997. };
  998. static const char * const gpmi_clks_for_mx6[] = {
  999. "gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
  1000. };
  1001. static const struct gpmi_devdata gpmi_devdata_imx6q = {
  1002. .type = IS_MX6Q,
  1003. .bch_max_ecc_strength = 40,
  1004. .max_chain_delay = 12000,
  1005. .clks = gpmi_clks_for_mx6,
  1006. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
  1007. };
  1008. static const struct gpmi_devdata gpmi_devdata_imx6sx = {
  1009. .type = IS_MX6SX,
  1010. .bch_max_ecc_strength = 62,
  1011. .max_chain_delay = 12000,
  1012. .clks = gpmi_clks_for_mx6,
  1013. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
  1014. };
  1015. static const char * const gpmi_clks_for_mx7d[] = {
  1016. "gpmi_io", "gpmi_bch_apb",
  1017. };
  1018. static const struct gpmi_devdata gpmi_devdata_imx7d = {
  1019. .type = IS_MX7D,
  1020. .bch_max_ecc_strength = 62,
  1021. .max_chain_delay = 12000,
  1022. .clks = gpmi_clks_for_mx7d,
  1023. .clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d),
  1024. };
  1025. static int acquire_register_block(struct gpmi_nand_data *this,
  1026. const char *res_name)
  1027. {
  1028. struct platform_device *pdev = this->pdev;
  1029. struct resources *res = &this->resources;
  1030. void __iomem *p;
  1031. p = devm_platform_ioremap_resource_byname(pdev, res_name);
  1032. if (IS_ERR(p))
  1033. return PTR_ERR(p);
  1034. if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
  1035. res->gpmi_regs = p;
  1036. else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
  1037. res->bch_regs = p;
  1038. else
  1039. dev_err(this->dev, "unknown resource name : %s\n", res_name);
  1040. return 0;
  1041. }
  1042. static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
  1043. {
  1044. struct platform_device *pdev = this->pdev;
  1045. const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
  1046. int err;
  1047. err = platform_get_irq_byname(pdev, res_name);
  1048. if (err < 0)
  1049. return err;
  1050. err = devm_request_irq(this->dev, err, irq_h, 0, res_name, this);
  1051. if (err)
  1052. dev_err(this->dev, "error requesting BCH IRQ\n");
  1053. return err;
  1054. }
  1055. static void release_dma_channels(struct gpmi_nand_data *this)
  1056. {
  1057. unsigned int i;
  1058. for (i = 0; i < DMA_CHANS; i++)
  1059. if (this->dma_chans[i]) {
  1060. dma_release_channel(this->dma_chans[i]);
  1061. this->dma_chans[i] = NULL;
  1062. }
  1063. }
  1064. static int acquire_dma_channels(struct gpmi_nand_data *this)
  1065. {
  1066. struct platform_device *pdev = this->pdev;
  1067. struct dma_chan *dma_chan;
  1068. int ret = 0;
  1069. /* request dma channel */
  1070. dma_chan = dma_request_chan(&pdev->dev, "rx-tx");
  1071. if (IS_ERR(dma_chan)) {
  1072. ret = dev_err_probe(this->dev, PTR_ERR(dma_chan),
  1073. "DMA channel request failed\n");
  1074. release_dma_channels(this);
  1075. } else {
  1076. this->dma_chans[0] = dma_chan;
  1077. }
  1078. return ret;
  1079. }
  1080. static int gpmi_get_clks(struct gpmi_nand_data *this)
  1081. {
  1082. struct resources *r = &this->resources;
  1083. struct clk *clk;
  1084. int err, i;
  1085. for (i = 0; i < this->devdata->clks_count; i++) {
  1086. clk = devm_clk_get(this->dev, this->devdata->clks[i]);
  1087. if (IS_ERR(clk)) {
  1088. err = PTR_ERR(clk);
  1089. goto err_clock;
  1090. }
  1091. r->clock[i] = clk;
  1092. }
  1093. return 0;
  1094. err_clock:
  1095. dev_dbg(this->dev, "failed in finding the clocks.\n");
  1096. return err;
  1097. }
  1098. static int acquire_resources(struct gpmi_nand_data *this)
  1099. {
  1100. int ret;
  1101. ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
  1102. if (ret)
  1103. goto exit_regs;
  1104. ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
  1105. if (ret)
  1106. goto exit_regs;
  1107. ret = acquire_bch_irq(this, bch_irq);
  1108. if (ret)
  1109. goto exit_regs;
  1110. ret = acquire_dma_channels(this);
  1111. if (ret)
  1112. goto exit_regs;
  1113. ret = gpmi_get_clks(this);
  1114. if (ret)
  1115. goto exit_clock;
  1116. return 0;
  1117. exit_clock:
  1118. release_dma_channels(this);
  1119. exit_regs:
  1120. return ret;
  1121. }
  1122. static void release_resources(struct gpmi_nand_data *this)
  1123. {
  1124. release_dma_channels(this);
  1125. }
  1126. static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
  1127. {
  1128. struct device *dev = this->dev;
  1129. struct bch_geometry *geo = &this->bch_geometry;
  1130. if (this->auxiliary_virt && virt_addr_valid(this->auxiliary_virt))
  1131. dma_free_coherent(dev, geo->auxiliary_size,
  1132. this->auxiliary_virt,
  1133. this->auxiliary_phys);
  1134. kfree(this->data_buffer_dma);
  1135. kfree(this->raw_buffer);
  1136. this->data_buffer_dma = NULL;
  1137. this->raw_buffer = NULL;
  1138. }
  1139. /* Allocate the DMA buffers */
  1140. static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
  1141. {
  1142. struct bch_geometry *geo = &this->bch_geometry;
  1143. struct device *dev = this->dev;
  1144. struct mtd_info *mtd = nand_to_mtd(&this->nand);
  1145. /*
  1146. * [2] Allocate a read/write data buffer.
  1147. * The gpmi_alloc_dma_buffer can be called twice.
  1148. * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
  1149. * is called before the NAND identification; and we allocate a
  1150. * buffer of the real NAND page size when the gpmi_alloc_dma_buffer
  1151. * is called after.
  1152. */
  1153. this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
  1154. GFP_DMA | GFP_KERNEL);
  1155. if (this->data_buffer_dma == NULL)
  1156. goto error_alloc;
  1157. this->auxiliary_virt = dma_alloc_coherent(dev, geo->auxiliary_size,
  1158. &this->auxiliary_phys, GFP_DMA);
  1159. if (!this->auxiliary_virt)
  1160. goto error_alloc;
  1161. this->raw_buffer = kzalloc((mtd->writesize ?: PAGE_SIZE) + mtd->oobsize, GFP_KERNEL);
  1162. if (!this->raw_buffer)
  1163. goto error_alloc;
  1164. return 0;
  1165. error_alloc:
  1166. gpmi_free_dma_buffer(this);
  1167. return -ENOMEM;
  1168. }
  1169. /*
  1170. * Handles block mark swapping.
  1171. * It can be called in swapping the block mark, or swapping it back,
  1172. * because the operations are the same.
  1173. */
  1174. static void block_mark_swapping(struct gpmi_nand_data *this,
  1175. void *payload, void *auxiliary)
  1176. {
  1177. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1178. unsigned char *p;
  1179. unsigned char *a;
  1180. unsigned int bit;
  1181. unsigned char mask;
  1182. unsigned char from_data;
  1183. unsigned char from_oob;
  1184. if (!this->swap_block_mark)
  1185. return;
  1186. /*
  1187. * If control arrives here, we're swapping. Make some convenience
  1188. * variables.
  1189. */
  1190. bit = nfc_geo->block_mark_bit_offset;
  1191. p = payload + nfc_geo->block_mark_byte_offset;
  1192. a = auxiliary;
  1193. /*
  1194. * Get the byte from the data area that overlays the block mark. Since
  1195. * the ECC engine applies its own view to the bits in the page, the
  1196. * physical block mark won't (in general) appear on a byte boundary in
  1197. * the data.
  1198. */
  1199. from_data = (p[0] >> bit) | (p[1] << (8 - bit));
  1200. /* Get the byte from the OOB. */
  1201. from_oob = a[0];
  1202. /* Swap them. */
  1203. a[0] = from_data;
  1204. mask = (0x1 << bit) - 1;
  1205. p[0] = (p[0] & mask) | (from_oob << bit);
  1206. mask = ~0 << bit;
  1207. p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
  1208. }
  1209. static int gpmi_count_bitflips(struct nand_chip *chip, void *buf, int first,
  1210. int last, int meta)
  1211. {
  1212. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1213. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1214. struct mtd_info *mtd = nand_to_mtd(chip);
  1215. int i;
  1216. unsigned char *status;
  1217. unsigned int max_bitflips = 0;
  1218. /* Loop over status bytes, accumulating ECC status. */
  1219. status = this->auxiliary_virt + ALIGN(meta, 4);
  1220. for (i = first; i < last; i++, status++) {
  1221. if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
  1222. continue;
  1223. if (*status == STATUS_UNCORRECTABLE) {
  1224. int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
  1225. u8 *eccbuf = this->raw_buffer;
  1226. int offset, bitoffset;
  1227. int eccbytes;
  1228. int flips;
  1229. /* Read ECC bytes into our internal raw_buffer */
  1230. offset = nfc_geo->metadata_size * 8;
  1231. offset += ((8 * nfc_geo->eccn_chunk_size) + eccbits) * (i + 1);
  1232. offset -= eccbits;
  1233. bitoffset = offset % 8;
  1234. eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
  1235. offset /= 8;
  1236. eccbytes -= offset;
  1237. nand_change_read_column_op(chip, offset, eccbuf,
  1238. eccbytes, false);
  1239. /*
  1240. * ECC data are not byte aligned and we may have
  1241. * in-band data in the first and last byte of
  1242. * eccbuf. Set non-eccbits to one so that
  1243. * nand_check_erased_ecc_chunk() does not count them
  1244. * as bitflips.
  1245. */
  1246. if (bitoffset)
  1247. eccbuf[0] |= GENMASK(bitoffset - 1, 0);
  1248. bitoffset = (bitoffset + eccbits) % 8;
  1249. if (bitoffset)
  1250. eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
  1251. /*
  1252. * The ECC hardware has an uncorrectable ECC status
  1253. * code in case we have bitflips in an erased page. As
  1254. * nothing was written into this subpage the ECC is
  1255. * obviously wrong and we can not trust it. We assume
  1256. * at this point that we are reading an erased page and
  1257. * try to correct the bitflips in buffer up to
  1258. * ecc_strength bitflips. If this is a page with random
  1259. * data, we exceed this number of bitflips and have a
  1260. * ECC failure. Otherwise we use the corrected buffer.
  1261. */
  1262. if (i == 0) {
  1263. /* The first block includes metadata */
  1264. flips = nand_check_erased_ecc_chunk(
  1265. buf + i * nfc_geo->eccn_chunk_size,
  1266. nfc_geo->eccn_chunk_size,
  1267. eccbuf, eccbytes,
  1268. this->auxiliary_virt,
  1269. nfc_geo->metadata_size,
  1270. nfc_geo->ecc_strength);
  1271. } else {
  1272. flips = nand_check_erased_ecc_chunk(
  1273. buf + i * nfc_geo->eccn_chunk_size,
  1274. nfc_geo->eccn_chunk_size,
  1275. eccbuf, eccbytes,
  1276. NULL, 0,
  1277. nfc_geo->ecc_strength);
  1278. }
  1279. if (flips > 0) {
  1280. max_bitflips = max_t(unsigned int, max_bitflips,
  1281. flips);
  1282. mtd->ecc_stats.corrected += flips;
  1283. continue;
  1284. }
  1285. mtd->ecc_stats.failed++;
  1286. continue;
  1287. }
  1288. mtd->ecc_stats.corrected += *status;
  1289. max_bitflips = max_t(unsigned int, max_bitflips, *status);
  1290. }
  1291. return max_bitflips;
  1292. }
  1293. static void gpmi_bch_layout_std(struct gpmi_nand_data *this)
  1294. {
  1295. struct bch_geometry *geo = &this->bch_geometry;
  1296. unsigned int ecc_strength = geo->ecc_strength >> 1;
  1297. unsigned int gf_len = geo->gf_len;
  1298. unsigned int block0_size = geo->ecc0_chunk_size;
  1299. unsigned int blockn_size = geo->eccn_chunk_size;
  1300. this->bch_flashlayout0 =
  1301. BF_BCH_FLASH0LAYOUT0_NBLOCKS(geo->ecc_chunk_count - 1) |
  1302. BF_BCH_FLASH0LAYOUT0_META_SIZE(geo->metadata_size) |
  1303. BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
  1304. BF_BCH_FLASH0LAYOUT0_GF(gf_len, this) |
  1305. BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block0_size, this);
  1306. this->bch_flashlayout1 =
  1307. BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(geo->page_size) |
  1308. BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
  1309. BF_BCH_FLASH0LAYOUT1_GF(gf_len, this) |
  1310. BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(blockn_size, this);
  1311. }
  1312. static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf,
  1313. int oob_required, int page)
  1314. {
  1315. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1316. struct mtd_info *mtd = nand_to_mtd(chip);
  1317. struct bch_geometry *geo = &this->bch_geometry;
  1318. unsigned int max_bitflips;
  1319. int ret;
  1320. gpmi_bch_layout_std(this);
  1321. this->bch = true;
  1322. ret = nand_read_page_op(chip, page, 0, buf, geo->page_size);
  1323. if (ret)
  1324. return ret;
  1325. max_bitflips = gpmi_count_bitflips(chip, buf, 0,
  1326. geo->ecc_chunk_count,
  1327. geo->auxiliary_status_offset);
  1328. /* handle the block mark swapping */
  1329. block_mark_swapping(this, buf, this->auxiliary_virt);
  1330. if (oob_required) {
  1331. /*
  1332. * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
  1333. * for details about our policy for delivering the OOB.
  1334. *
  1335. * We fill the caller's buffer with set bits, and then copy the
  1336. * block mark to th caller's buffer. Note that, if block mark
  1337. * swapping was necessary, it has already been done, so we can
  1338. * rely on the first byte of the auxiliary buffer to contain
  1339. * the block mark.
  1340. */
  1341. memset(chip->oob_poi, ~0, mtd->oobsize);
  1342. chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0];
  1343. }
  1344. return max_bitflips;
  1345. }
  1346. /* Fake a virtual small page for the subpage read */
  1347. static int gpmi_ecc_read_subpage(struct nand_chip *chip, uint32_t offs,
  1348. uint32_t len, uint8_t *buf, int page)
  1349. {
  1350. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1351. struct bch_geometry *geo = &this->bch_geometry;
  1352. int size = chip->ecc.size; /* ECC chunk size */
  1353. int meta, n, page_size;
  1354. unsigned int max_bitflips;
  1355. unsigned int ecc_strength;
  1356. int first, last, marker_pos;
  1357. int ecc_parity_size;
  1358. int col = 0;
  1359. int ret;
  1360. /* The size of ECC parity */
  1361. ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
  1362. /* Align it with the chunk size */
  1363. first = offs / size;
  1364. last = (offs + len - 1) / size;
  1365. if (this->swap_block_mark) {
  1366. /*
  1367. * Find the chunk which contains the Block Marker.
  1368. * If this chunk is in the range of [first, last],
  1369. * we have to read out the whole page.
  1370. * Why? since we had swapped the data at the position of Block
  1371. * Marker to the metadata which is bound with the chunk 0.
  1372. */
  1373. marker_pos = geo->block_mark_byte_offset / size;
  1374. if (last >= marker_pos && first <= marker_pos) {
  1375. dev_dbg(this->dev,
  1376. "page:%d, first:%d, last:%d, marker at:%d\n",
  1377. page, first, last, marker_pos);
  1378. return gpmi_ecc_read_page(chip, buf, 0, page);
  1379. }
  1380. }
  1381. /*
  1382. * if there is an ECC dedicate for meta:
  1383. * - need to add an extra ECC size when calculating col and page_size,
  1384. * if the meta size is NOT zero.
  1385. * - ecc0_chunk size need to set to the same size as other chunks,
  1386. * if the meta size is zero.
  1387. */
  1388. meta = geo->metadata_size;
  1389. if (first) {
  1390. if (geo->ecc_for_meta)
  1391. col = meta + ecc_parity_size
  1392. + (size + ecc_parity_size) * first;
  1393. else
  1394. col = meta + (size + ecc_parity_size) * first;
  1395. meta = 0;
  1396. buf = buf + first * size;
  1397. }
  1398. ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
  1399. n = last - first + 1;
  1400. if (geo->ecc_for_meta && meta)
  1401. page_size = meta + ecc_parity_size
  1402. + (size + ecc_parity_size) * n;
  1403. else
  1404. page_size = meta + (size + ecc_parity_size) * n;
  1405. ecc_strength = geo->ecc_strength >> 1;
  1406. this->bch_flashlayout0 = BF_BCH_FLASH0LAYOUT0_NBLOCKS(
  1407. (geo->ecc_for_meta ? n : n - 1)) |
  1408. BF_BCH_FLASH0LAYOUT0_META_SIZE(meta) |
  1409. BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
  1410. BF_BCH_FLASH0LAYOUT0_GF(geo->gf_len, this) |
  1411. BF_BCH_FLASH0LAYOUT0_DATA0_SIZE((geo->ecc_for_meta ?
  1412. 0 : geo->ecc0_chunk_size), this);
  1413. this->bch_flashlayout1 = BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) |
  1414. BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
  1415. BF_BCH_FLASH0LAYOUT1_GF(geo->gf_len, this) |
  1416. BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(geo->eccn_chunk_size, this);
  1417. this->bch = true;
  1418. ret = nand_read_page_op(chip, page, col, buf, page_size);
  1419. if (ret)
  1420. return ret;
  1421. dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
  1422. page, offs, len, col, first, n, page_size);
  1423. max_bitflips = gpmi_count_bitflips(chip, buf, first, last, meta);
  1424. return max_bitflips;
  1425. }
  1426. static int gpmi_ecc_write_page(struct nand_chip *chip, const uint8_t *buf,
  1427. int oob_required, int page)
  1428. {
  1429. struct mtd_info *mtd = nand_to_mtd(chip);
  1430. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1431. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1432. dev_dbg(this->dev, "ecc write page.\n");
  1433. gpmi_bch_layout_std(this);
  1434. this->bch = true;
  1435. memcpy(this->auxiliary_virt, chip->oob_poi, nfc_geo->auxiliary_size);
  1436. if (this->swap_block_mark) {
  1437. /*
  1438. * When doing bad block marker swapping we must always copy the
  1439. * input buffer as we can't modify the const buffer.
  1440. */
  1441. memcpy(this->data_buffer_dma, buf, mtd->writesize);
  1442. buf = this->data_buffer_dma;
  1443. block_mark_swapping(this, this->data_buffer_dma,
  1444. this->auxiliary_virt);
  1445. }
  1446. return nand_prog_page_op(chip, page, 0, buf, nfc_geo->page_size);
  1447. }
  1448. /*
  1449. * There are several places in this driver where we have to handle the OOB and
  1450. * block marks. This is the function where things are the most complicated, so
  1451. * this is where we try to explain it all. All the other places refer back to
  1452. * here.
  1453. *
  1454. * These are the rules, in order of decreasing importance:
  1455. *
  1456. * 1) Nothing the caller does can be allowed to imperil the block mark.
  1457. *
  1458. * 2) In read operations, the first byte of the OOB we return must reflect the
  1459. * true state of the block mark, no matter where that block mark appears in
  1460. * the physical page.
  1461. *
  1462. * 3) ECC-based read operations return an OOB full of set bits (since we never
  1463. * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
  1464. * return).
  1465. *
  1466. * 4) "Raw" read operations return a direct view of the physical bytes in the
  1467. * page, using the conventional definition of which bytes are data and which
  1468. * are OOB. This gives the caller a way to see the actual, physical bytes
  1469. * in the page, without the distortions applied by our ECC engine.
  1470. *
  1471. *
  1472. * What we do for this specific read operation depends on two questions:
  1473. *
  1474. * 1) Are we doing a "raw" read, or an ECC-based read?
  1475. *
  1476. * 2) Are we using block mark swapping or transcription?
  1477. *
  1478. * There are four cases, illustrated by the following Karnaugh map:
  1479. *
  1480. * | Raw | ECC-based |
  1481. * -------------+-------------------------+-------------------------+
  1482. * | Read the conventional | |
  1483. * | OOB at the end of the | |
  1484. * Swapping | page and return it. It | |
  1485. * | contains exactly what | |
  1486. * | we want. | Read the block mark and |
  1487. * -------------+-------------------------+ return it in a buffer |
  1488. * | Read the conventional | full of set bits. |
  1489. * | OOB at the end of the | |
  1490. * | page and also the block | |
  1491. * Transcribing | mark in the metadata. | |
  1492. * | Copy the block mark | |
  1493. * | into the first byte of | |
  1494. * | the OOB. | |
  1495. * -------------+-------------------------+-------------------------+
  1496. *
  1497. * Note that we break rule #4 in the Transcribing/Raw case because we're not
  1498. * giving an accurate view of the actual, physical bytes in the page (we're
  1499. * overwriting the block mark). That's OK because it's more important to follow
  1500. * rule #2.
  1501. *
  1502. * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
  1503. * easy. When reading a page, for example, the NAND Flash MTD code calls our
  1504. * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
  1505. * ECC-based or raw view of the page is implicit in which function it calls
  1506. * (there is a similar pair of ECC-based/raw functions for writing).
  1507. */
  1508. static int gpmi_ecc_read_oob(struct nand_chip *chip, int page)
  1509. {
  1510. struct mtd_info *mtd = nand_to_mtd(chip);
  1511. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1512. int ret;
  1513. /* clear the OOB buffer */
  1514. memset(chip->oob_poi, ~0, mtd->oobsize);
  1515. /* Read out the conventional OOB. */
  1516. ret = nand_read_page_op(chip, page, mtd->writesize, chip->oob_poi,
  1517. mtd->oobsize);
  1518. if (ret)
  1519. return ret;
  1520. /*
  1521. * Now, we want to make sure the block mark is correct. In the
  1522. * non-transcribing case (!GPMI_IS_MX23()), we already have it.
  1523. * Otherwise, we need to explicitly read it.
  1524. */
  1525. if (GPMI_IS_MX23(this)) {
  1526. /* Read the block mark into the first byte of the OOB buffer. */
  1527. ret = nand_read_page_op(chip, page, 0, chip->oob_poi, 1);
  1528. if (ret)
  1529. return ret;
  1530. }
  1531. return 0;
  1532. }
  1533. static int gpmi_ecc_write_oob(struct nand_chip *chip, int page)
  1534. {
  1535. struct mtd_info *mtd = nand_to_mtd(chip);
  1536. struct mtd_oob_region of = { };
  1537. /* Do we have available oob area? */
  1538. mtd_ooblayout_free(mtd, 0, &of);
  1539. if (!of.length)
  1540. return -EPERM;
  1541. if (!nand_is_slc(chip))
  1542. return -EPERM;
  1543. return nand_prog_page_op(chip, page, mtd->writesize + of.offset,
  1544. chip->oob_poi + of.offset, of.length);
  1545. }
  1546. /*
  1547. * This function reads a NAND page without involving the ECC engine (no HW
  1548. * ECC correction).
  1549. * The tricky part in the GPMI/BCH controller is that it stores ECC bits
  1550. * inline (interleaved with payload DATA), and do not align data chunk on
  1551. * byte boundaries.
  1552. * We thus need to take care moving the payload data and ECC bits stored in the
  1553. * page into the provided buffers, which is why we're using nand_extract_bits().
  1554. *
  1555. * See set_geometry_by_ecc_info inline comments to have a full description
  1556. * of the layout used by the GPMI controller.
  1557. */
  1558. static int gpmi_ecc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
  1559. int oob_required, int page)
  1560. {
  1561. struct mtd_info *mtd = nand_to_mtd(chip);
  1562. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1563. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1564. int eccsize = nfc_geo->eccn_chunk_size;
  1565. int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
  1566. u8 *tmp_buf = this->raw_buffer;
  1567. size_t src_bit_off;
  1568. size_t oob_bit_off;
  1569. size_t oob_byte_off;
  1570. uint8_t *oob = chip->oob_poi;
  1571. int step;
  1572. int ret;
  1573. ret = nand_read_page_op(chip, page, 0, tmp_buf,
  1574. mtd->writesize + mtd->oobsize);
  1575. if (ret)
  1576. return ret;
  1577. /*
  1578. * If required, swap the bad block marker and the data stored in the
  1579. * metadata section, so that we don't wrongly consider a block as bad.
  1580. *
  1581. * See the layout description for a detailed explanation on why this
  1582. * is needed.
  1583. */
  1584. if (this->swap_block_mark)
  1585. swap(tmp_buf[0], tmp_buf[mtd->writesize]);
  1586. /*
  1587. * Copy the metadata section into the oob buffer (this section is
  1588. * guaranteed to be aligned on a byte boundary).
  1589. */
  1590. if (oob_required)
  1591. memcpy(oob, tmp_buf, nfc_geo->metadata_size);
  1592. oob_bit_off = nfc_geo->metadata_size * 8;
  1593. src_bit_off = oob_bit_off;
  1594. /* Extract interleaved payload data and ECC bits */
  1595. for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
  1596. if (buf)
  1597. nand_extract_bits(buf, step * eccsize * 8, tmp_buf,
  1598. src_bit_off, eccsize * 8);
  1599. src_bit_off += eccsize * 8;
  1600. /* Align last ECC block to align a byte boundary */
  1601. if (step == nfc_geo->ecc_chunk_count - 1 &&
  1602. (oob_bit_off + eccbits) % 8)
  1603. eccbits += 8 - ((oob_bit_off + eccbits) % 8);
  1604. if (oob_required)
  1605. nand_extract_bits(oob, oob_bit_off, tmp_buf,
  1606. src_bit_off, eccbits);
  1607. src_bit_off += eccbits;
  1608. oob_bit_off += eccbits;
  1609. }
  1610. if (oob_required) {
  1611. oob_byte_off = oob_bit_off / 8;
  1612. if (oob_byte_off < mtd->oobsize)
  1613. memcpy(oob + oob_byte_off,
  1614. tmp_buf + mtd->writesize + oob_byte_off,
  1615. mtd->oobsize - oob_byte_off);
  1616. }
  1617. return 0;
  1618. }
  1619. /*
  1620. * This function writes a NAND page without involving the ECC engine (no HW
  1621. * ECC generation).
  1622. * The tricky part in the GPMI/BCH controller is that it stores ECC bits
  1623. * inline (interleaved with payload DATA), and do not align data chunk on
  1624. * byte boundaries.
  1625. * We thus need to take care moving the OOB area at the right place in the
  1626. * final page, which is why we're using nand_extract_bits().
  1627. *
  1628. * See set_geometry_by_ecc_info inline comments to have a full description
  1629. * of the layout used by the GPMI controller.
  1630. */
  1631. static int gpmi_ecc_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
  1632. int oob_required, int page)
  1633. {
  1634. struct mtd_info *mtd = nand_to_mtd(chip);
  1635. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1636. struct bch_geometry *nfc_geo = &this->bch_geometry;
  1637. int eccsize = nfc_geo->eccn_chunk_size;
  1638. int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
  1639. u8 *tmp_buf = this->raw_buffer;
  1640. uint8_t *oob = chip->oob_poi;
  1641. size_t dst_bit_off;
  1642. size_t oob_bit_off;
  1643. size_t oob_byte_off;
  1644. int step;
  1645. /*
  1646. * Initialize all bits to 1 in case we don't have a buffer for the
  1647. * payload or oob data in order to leave unspecified bits of data
  1648. * to their initial state.
  1649. */
  1650. if (!buf || !oob_required)
  1651. memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
  1652. /*
  1653. * First copy the metadata section (stored in oob buffer) at the
  1654. * beginning of the page, as imposed by the GPMI layout.
  1655. */
  1656. memcpy(tmp_buf, oob, nfc_geo->metadata_size);
  1657. oob_bit_off = nfc_geo->metadata_size * 8;
  1658. dst_bit_off = oob_bit_off;
  1659. /* Interleave payload data and ECC bits */
  1660. for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
  1661. if (buf)
  1662. nand_extract_bits(tmp_buf, dst_bit_off, buf,
  1663. step * eccsize * 8, eccsize * 8);
  1664. dst_bit_off += eccsize * 8;
  1665. /* Align last ECC block to align a byte boundary */
  1666. if (step == nfc_geo->ecc_chunk_count - 1 &&
  1667. (oob_bit_off + eccbits) % 8)
  1668. eccbits += 8 - ((oob_bit_off + eccbits) % 8);
  1669. if (oob_required)
  1670. nand_extract_bits(tmp_buf, dst_bit_off, oob,
  1671. oob_bit_off, eccbits);
  1672. dst_bit_off += eccbits;
  1673. oob_bit_off += eccbits;
  1674. }
  1675. oob_byte_off = oob_bit_off / 8;
  1676. if (oob_required && oob_byte_off < mtd->oobsize)
  1677. memcpy(tmp_buf + mtd->writesize + oob_byte_off,
  1678. oob + oob_byte_off, mtd->oobsize - oob_byte_off);
  1679. /*
  1680. * If required, swap the bad block marker and the first byte of the
  1681. * metadata section, so that we don't modify the bad block marker.
  1682. *
  1683. * See the layout description for a detailed explanation on why this
  1684. * is needed.
  1685. */
  1686. if (this->swap_block_mark)
  1687. swap(tmp_buf[0], tmp_buf[mtd->writesize]);
  1688. return nand_prog_page_op(chip, page, 0, tmp_buf,
  1689. mtd->writesize + mtd->oobsize);
  1690. }
  1691. static int gpmi_ecc_read_oob_raw(struct nand_chip *chip, int page)
  1692. {
  1693. return gpmi_ecc_read_page_raw(chip, NULL, 1, page);
  1694. }
  1695. static int gpmi_ecc_write_oob_raw(struct nand_chip *chip, int page)
  1696. {
  1697. return gpmi_ecc_write_page_raw(chip, NULL, 1, page);
  1698. }
  1699. static int gpmi_block_markbad(struct nand_chip *chip, loff_t ofs)
  1700. {
  1701. struct mtd_info *mtd = nand_to_mtd(chip);
  1702. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1703. int ret = 0;
  1704. uint8_t *block_mark;
  1705. int column, page, chipnr;
  1706. chipnr = (int)(ofs >> chip->chip_shift);
  1707. nand_select_target(chip, chipnr);
  1708. column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
  1709. /* Write the block mark. */
  1710. block_mark = this->data_buffer_dma;
  1711. block_mark[0] = 0; /* bad block marker */
  1712. /* Shift to get page */
  1713. page = (int)(ofs >> chip->page_shift);
  1714. ret = nand_prog_page_op(chip, page, column, block_mark, 1);
  1715. nand_deselect_target(chip);
  1716. return ret;
  1717. }
  1718. static int nand_boot_set_geometry(struct gpmi_nand_data *this)
  1719. {
  1720. struct boot_rom_geometry *geometry = &this->rom_geometry;
  1721. /*
  1722. * Set the boot block stride size.
  1723. *
  1724. * In principle, we should be reading this from the OTP bits, since
  1725. * that's where the ROM is going to get it. In fact, we don't have any
  1726. * way to read the OTP bits, so we go with the default and hope for the
  1727. * best.
  1728. */
  1729. geometry->stride_size_in_pages = 64;
  1730. /*
  1731. * Set the search area stride exponent.
  1732. *
  1733. * In principle, we should be reading this from the OTP bits, since
  1734. * that's where the ROM is going to get it. In fact, we don't have any
  1735. * way to read the OTP bits, so we go with the default and hope for the
  1736. * best.
  1737. */
  1738. geometry->search_area_stride_exponent = 2;
  1739. return 0;
  1740. }
  1741. static const char *fingerprint = "STMP";
  1742. static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
  1743. {
  1744. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1745. struct device *dev = this->dev;
  1746. struct nand_chip *chip = &this->nand;
  1747. unsigned int search_area_size_in_strides;
  1748. unsigned int stride;
  1749. unsigned int page;
  1750. u8 *buffer = nand_get_data_buf(chip);
  1751. int found_an_ncb_fingerprint = false;
  1752. int ret;
  1753. /* Compute the number of strides in a search area. */
  1754. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1755. nand_select_target(chip, 0);
  1756. /*
  1757. * Loop through the first search area, looking for the NCB fingerprint.
  1758. */
  1759. dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
  1760. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1761. /* Compute the page addresses. */
  1762. page = stride * rom_geo->stride_size_in_pages;
  1763. dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
  1764. /*
  1765. * Read the NCB fingerprint. The fingerprint is four bytes long
  1766. * and starts in the 12th byte of the page.
  1767. */
  1768. ret = nand_read_page_op(chip, page, 12, buffer,
  1769. strlen(fingerprint));
  1770. if (ret)
  1771. continue;
  1772. /* Look for the fingerprint. */
  1773. if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
  1774. found_an_ncb_fingerprint = true;
  1775. break;
  1776. }
  1777. }
  1778. nand_deselect_target(chip);
  1779. if (found_an_ncb_fingerprint)
  1780. dev_dbg(dev, "\tFound a fingerprint\n");
  1781. else
  1782. dev_dbg(dev, "\tNo fingerprint found\n");
  1783. return found_an_ncb_fingerprint;
  1784. }
  1785. /* Writes a transcription stamp. */
  1786. static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
  1787. {
  1788. struct device *dev = this->dev;
  1789. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1790. struct nand_chip *chip = &this->nand;
  1791. struct mtd_info *mtd = nand_to_mtd(chip);
  1792. unsigned int block_size_in_pages;
  1793. unsigned int search_area_size_in_strides;
  1794. unsigned int search_area_size_in_pages;
  1795. unsigned int search_area_size_in_blocks;
  1796. unsigned int block;
  1797. unsigned int stride;
  1798. unsigned int page;
  1799. u8 *buffer = nand_get_data_buf(chip);
  1800. int status;
  1801. /* Compute the search area geometry. */
  1802. block_size_in_pages = mtd->erasesize / mtd->writesize;
  1803. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1804. search_area_size_in_pages = search_area_size_in_strides *
  1805. rom_geo->stride_size_in_pages;
  1806. search_area_size_in_blocks =
  1807. (search_area_size_in_pages + (block_size_in_pages - 1)) /
  1808. block_size_in_pages;
  1809. dev_dbg(dev, "Search Area Geometry :\n");
  1810. dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
  1811. dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
  1812. dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
  1813. nand_select_target(chip, 0);
  1814. /* Loop over blocks in the first search area, erasing them. */
  1815. dev_dbg(dev, "Erasing the search area...\n");
  1816. for (block = 0; block < search_area_size_in_blocks; block++) {
  1817. /* Erase this block. */
  1818. dev_dbg(dev, "\tErasing block 0x%x\n", block);
  1819. status = nand_erase_op(chip, block);
  1820. if (status)
  1821. dev_err(dev, "[%s] Erase failed.\n", __func__);
  1822. }
  1823. /* Write the NCB fingerprint into the page buffer. */
  1824. memset(buffer, ~0, mtd->writesize);
  1825. memcpy(buffer + 12, fingerprint, strlen(fingerprint));
  1826. /* Loop through the first search area, writing NCB fingerprints. */
  1827. dev_dbg(dev, "Writing NCB fingerprints...\n");
  1828. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1829. /* Compute the page addresses. */
  1830. page = stride * rom_geo->stride_size_in_pages;
  1831. /* Write the first page of the current stride. */
  1832. dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
  1833. status = chip->ecc.write_page_raw(chip, buffer, 0, page);
  1834. if (status)
  1835. dev_err(dev, "[%s] Write failed.\n", __func__);
  1836. }
  1837. nand_deselect_target(chip);
  1838. return 0;
  1839. }
  1840. static int mx23_boot_init(struct gpmi_nand_data *this)
  1841. {
  1842. struct device *dev = this->dev;
  1843. struct nand_chip *chip = &this->nand;
  1844. struct mtd_info *mtd = nand_to_mtd(chip);
  1845. unsigned int block_count;
  1846. unsigned int block;
  1847. int chipnr;
  1848. int page;
  1849. loff_t byte;
  1850. uint8_t block_mark;
  1851. int ret = 0;
  1852. /*
  1853. * If control arrives here, we can't use block mark swapping, which
  1854. * means we're forced to use transcription. First, scan for the
  1855. * transcription stamp. If we find it, then we don't have to do
  1856. * anything -- the block marks are already transcribed.
  1857. */
  1858. if (mx23_check_transcription_stamp(this))
  1859. return 0;
  1860. /*
  1861. * If control arrives here, we couldn't find a transcription stamp, so
  1862. * so we presume the block marks are in the conventional location.
  1863. */
  1864. dev_dbg(dev, "Transcribing bad block marks...\n");
  1865. /* Compute the number of blocks in the entire medium. */
  1866. block_count = nanddev_eraseblocks_per_target(&chip->base);
  1867. /*
  1868. * Loop over all the blocks in the medium, transcribing block marks as
  1869. * we go.
  1870. */
  1871. for (block = 0; block < block_count; block++) {
  1872. /*
  1873. * Compute the chip, page and byte addresses for this block's
  1874. * conventional mark.
  1875. */
  1876. chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
  1877. page = block << (chip->phys_erase_shift - chip->page_shift);
  1878. byte = block << chip->phys_erase_shift;
  1879. /* Send the command to read the conventional block mark. */
  1880. nand_select_target(chip, chipnr);
  1881. ret = nand_read_page_op(chip, page, mtd->writesize, &block_mark,
  1882. 1);
  1883. nand_deselect_target(chip);
  1884. if (ret)
  1885. continue;
  1886. /*
  1887. * Check if the block is marked bad. If so, we need to mark it
  1888. * again, but this time the result will be a mark in the
  1889. * location where we transcribe block marks.
  1890. */
  1891. if (block_mark != 0xff) {
  1892. dev_dbg(dev, "Transcribing mark in block %u\n", block);
  1893. ret = chip->legacy.block_markbad(chip, byte);
  1894. if (ret)
  1895. dev_err(dev,
  1896. "Failed to mark block bad with ret %d\n",
  1897. ret);
  1898. }
  1899. }
  1900. /* Write the stamp that indicates we've transcribed the block marks. */
  1901. mx23_write_transcription_stamp(this);
  1902. return 0;
  1903. }
  1904. static int nand_boot_init(struct gpmi_nand_data *this)
  1905. {
  1906. nand_boot_set_geometry(this);
  1907. /* This is ROM arch-specific initilization before the BBT scanning. */
  1908. if (GPMI_IS_MX23(this))
  1909. return mx23_boot_init(this);
  1910. return 0;
  1911. }
  1912. static int gpmi_set_geometry(struct gpmi_nand_data *this)
  1913. {
  1914. int ret;
  1915. /* Free the temporary DMA memory for reading ID. */
  1916. gpmi_free_dma_buffer(this);
  1917. /* Set up the NFC geometry which is used by BCH. */
  1918. ret = bch_set_geometry(this);
  1919. if (ret) {
  1920. dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
  1921. return ret;
  1922. }
  1923. /* Alloc the new DMA buffers according to the pagesize and oobsize */
  1924. return gpmi_alloc_dma_buffer(this);
  1925. }
  1926. static int gpmi_init_last(struct gpmi_nand_data *this)
  1927. {
  1928. struct nand_chip *chip = &this->nand;
  1929. struct mtd_info *mtd = nand_to_mtd(chip);
  1930. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1931. struct bch_geometry *bch_geo = &this->bch_geometry;
  1932. int ret;
  1933. /* Set up the medium geometry */
  1934. ret = gpmi_set_geometry(this);
  1935. if (ret)
  1936. return ret;
  1937. /* Init the nand_ecc_ctrl{} */
  1938. ecc->read_page = gpmi_ecc_read_page;
  1939. ecc->write_page = gpmi_ecc_write_page;
  1940. ecc->read_oob = gpmi_ecc_read_oob;
  1941. ecc->write_oob = gpmi_ecc_write_oob;
  1942. ecc->read_page_raw = gpmi_ecc_read_page_raw;
  1943. ecc->write_page_raw = gpmi_ecc_write_page_raw;
  1944. ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
  1945. ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
  1946. ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
  1947. ecc->size = bch_geo->eccn_chunk_size;
  1948. ecc->strength = bch_geo->ecc_strength;
  1949. mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
  1950. /*
  1951. * We only enable the subpage read when:
  1952. * (1) the chip is imx6, and
  1953. * (2) the size of the ECC parity is byte aligned.
  1954. */
  1955. if (GPMI_IS_MX6(this) &&
  1956. ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
  1957. ecc->read_subpage = gpmi_ecc_read_subpage;
  1958. chip->options |= NAND_SUBPAGE_READ;
  1959. }
  1960. return 0;
  1961. }
  1962. static int gpmi_nand_attach_chip(struct nand_chip *chip)
  1963. {
  1964. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  1965. int ret;
  1966. if (chip->bbt_options & NAND_BBT_USE_FLASH) {
  1967. chip->bbt_options |= NAND_BBT_NO_OOB;
  1968. if (of_property_read_bool(this->dev->of_node,
  1969. "fsl,no-blockmark-swap"))
  1970. this->swap_block_mark = false;
  1971. }
  1972. dev_dbg(this->dev, "Blockmark swapping %sabled\n",
  1973. this->swap_block_mark ? "en" : "dis");
  1974. ret = gpmi_init_last(this);
  1975. if (ret)
  1976. return ret;
  1977. chip->options |= NAND_SKIP_BBTSCAN;
  1978. return 0;
  1979. }
  1980. static struct gpmi_transfer *get_next_transfer(struct gpmi_nand_data *this)
  1981. {
  1982. struct gpmi_transfer *transfer = &this->transfers[this->ntransfers];
  1983. this->ntransfers++;
  1984. if (this->ntransfers == GPMI_MAX_TRANSFERS)
  1985. return NULL;
  1986. return transfer;
  1987. }
  1988. static struct dma_async_tx_descriptor *gpmi_chain_command(
  1989. struct gpmi_nand_data *this, u8 cmd, const u8 *addr, int naddr)
  1990. {
  1991. struct dma_chan *channel = get_dma_chan(this);
  1992. struct dma_async_tx_descriptor *desc;
  1993. struct gpmi_transfer *transfer;
  1994. int chip = this->nand.cur_cs;
  1995. u32 pio[3];
  1996. /* [1] send out the PIO words */
  1997. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
  1998. | BM_GPMI_CTRL0_WORD_LENGTH
  1999. | BF_GPMI_CTRL0_CS(chip, this)
  2000. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  2001. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
  2002. | BM_GPMI_CTRL0_ADDRESS_INCREMENT
  2003. | BF_GPMI_CTRL0_XFER_COUNT(naddr + 1);
  2004. pio[1] = 0;
  2005. pio[2] = 0;
  2006. desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
  2007. DMA_TRANS_NONE, 0);
  2008. if (!desc)
  2009. return NULL;
  2010. transfer = get_next_transfer(this);
  2011. if (!transfer)
  2012. return NULL;
  2013. transfer->cmdbuf[0] = cmd;
  2014. if (naddr)
  2015. memcpy(&transfer->cmdbuf[1], addr, naddr);
  2016. sg_init_one(&transfer->sgl, transfer->cmdbuf, naddr + 1);
  2017. dma_map_sg(this->dev, &transfer->sgl, 1, DMA_TO_DEVICE);
  2018. transfer->direction = DMA_TO_DEVICE;
  2019. desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, DMA_MEM_TO_DEV,
  2020. MXS_DMA_CTRL_WAIT4END);
  2021. return desc;
  2022. }
  2023. static struct dma_async_tx_descriptor *gpmi_chain_wait_ready(
  2024. struct gpmi_nand_data *this)
  2025. {
  2026. struct dma_chan *channel = get_dma_chan(this);
  2027. u32 pio[2];
  2028. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY)
  2029. | BM_GPMI_CTRL0_WORD_LENGTH
  2030. | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
  2031. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  2032. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
  2033. | BF_GPMI_CTRL0_XFER_COUNT(0);
  2034. pio[1] = 0;
  2035. return mxs_dmaengine_prep_pio(channel, pio, 2, DMA_TRANS_NONE,
  2036. MXS_DMA_CTRL_WAIT4END | MXS_DMA_CTRL_WAIT4RDY);
  2037. }
  2038. static struct dma_async_tx_descriptor *gpmi_chain_data_read(
  2039. struct gpmi_nand_data *this, void *buf, int raw_len, bool *direct)
  2040. {
  2041. struct dma_async_tx_descriptor *desc;
  2042. struct dma_chan *channel = get_dma_chan(this);
  2043. struct gpmi_transfer *transfer;
  2044. u32 pio[6] = {};
  2045. transfer = get_next_transfer(this);
  2046. if (!transfer)
  2047. return NULL;
  2048. transfer->direction = DMA_FROM_DEVICE;
  2049. *direct = prepare_data_dma(this, buf, raw_len, &transfer->sgl,
  2050. DMA_FROM_DEVICE);
  2051. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
  2052. | BM_GPMI_CTRL0_WORD_LENGTH
  2053. | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
  2054. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  2055. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
  2056. | BF_GPMI_CTRL0_XFER_COUNT(raw_len);
  2057. if (this->bch) {
  2058. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  2059. | BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE)
  2060. | BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
  2061. | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY);
  2062. pio[3] = raw_len;
  2063. pio[4] = transfer->sgl.dma_address;
  2064. pio[5] = this->auxiliary_phys;
  2065. }
  2066. desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
  2067. DMA_TRANS_NONE, 0);
  2068. if (!desc)
  2069. return NULL;
  2070. if (!this->bch)
  2071. desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1,
  2072. DMA_DEV_TO_MEM,
  2073. MXS_DMA_CTRL_WAIT4END);
  2074. return desc;
  2075. }
  2076. static struct dma_async_tx_descriptor *gpmi_chain_data_write(
  2077. struct gpmi_nand_data *this, const void *buf, int raw_len)
  2078. {
  2079. struct dma_chan *channel = get_dma_chan(this);
  2080. struct dma_async_tx_descriptor *desc;
  2081. struct gpmi_transfer *transfer;
  2082. u32 pio[6] = {};
  2083. transfer = get_next_transfer(this);
  2084. if (!transfer)
  2085. return NULL;
  2086. transfer->direction = DMA_TO_DEVICE;
  2087. prepare_data_dma(this, buf, raw_len, &transfer->sgl, DMA_TO_DEVICE);
  2088. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
  2089. | BM_GPMI_CTRL0_WORD_LENGTH
  2090. | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
  2091. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  2092. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
  2093. | BF_GPMI_CTRL0_XFER_COUNT(raw_len);
  2094. if (this->bch) {
  2095. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  2096. | BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE)
  2097. | BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
  2098. BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY);
  2099. pio[3] = raw_len;
  2100. pio[4] = transfer->sgl.dma_address;
  2101. pio[5] = this->auxiliary_phys;
  2102. }
  2103. desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
  2104. DMA_TRANS_NONE,
  2105. (this->bch ? MXS_DMA_CTRL_WAIT4END : 0));
  2106. if (!desc)
  2107. return NULL;
  2108. if (!this->bch)
  2109. desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1,
  2110. DMA_MEM_TO_DEV,
  2111. MXS_DMA_CTRL_WAIT4END);
  2112. return desc;
  2113. }
  2114. static int gpmi_nfc_exec_op(struct nand_chip *chip,
  2115. const struct nand_operation *op,
  2116. bool check_only)
  2117. {
  2118. const struct nand_op_instr *instr;
  2119. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  2120. struct dma_async_tx_descriptor *desc = NULL;
  2121. int i, ret, buf_len = 0, nbufs = 0;
  2122. u8 cmd = 0;
  2123. void *buf_read = NULL;
  2124. const void *buf_write = NULL;
  2125. bool direct = false;
  2126. struct completion *dma_completion, *bch_completion;
  2127. unsigned long to;
  2128. if (check_only)
  2129. return 0;
  2130. this->ntransfers = 0;
  2131. for (i = 0; i < GPMI_MAX_TRANSFERS; i++)
  2132. this->transfers[i].direction = DMA_NONE;
  2133. ret = pm_runtime_get_sync(this->dev);
  2134. if (ret < 0) {
  2135. pm_runtime_put_noidle(this->dev);
  2136. return ret;
  2137. }
  2138. /*
  2139. * This driver currently supports only one NAND chip. Plus, dies share
  2140. * the same configuration. So once timings have been applied on the
  2141. * controller side, they will not change anymore. When the time will
  2142. * come, the check on must_apply_timings will have to be dropped.
  2143. */
  2144. if (this->hw.must_apply_timings) {
  2145. this->hw.must_apply_timings = false;
  2146. ret = gpmi_nfc_apply_timings(this);
  2147. if (ret)
  2148. goto out_pm;
  2149. }
  2150. dev_dbg(this->dev, "%s: %d instructions\n", __func__, op->ninstrs);
  2151. for (i = 0; i < op->ninstrs; i++) {
  2152. instr = &op->instrs[i];
  2153. nand_op_trace(" ", instr);
  2154. switch (instr->type) {
  2155. case NAND_OP_WAITRDY_INSTR:
  2156. desc = gpmi_chain_wait_ready(this);
  2157. break;
  2158. case NAND_OP_CMD_INSTR:
  2159. cmd = instr->ctx.cmd.opcode;
  2160. /*
  2161. * When this command has an address cycle chain it
  2162. * together with the address cycle
  2163. */
  2164. if (i + 1 != op->ninstrs &&
  2165. op->instrs[i + 1].type == NAND_OP_ADDR_INSTR)
  2166. continue;
  2167. desc = gpmi_chain_command(this, cmd, NULL, 0);
  2168. break;
  2169. case NAND_OP_ADDR_INSTR:
  2170. desc = gpmi_chain_command(this, cmd, instr->ctx.addr.addrs,
  2171. instr->ctx.addr.naddrs);
  2172. break;
  2173. case NAND_OP_DATA_OUT_INSTR:
  2174. buf_write = instr->ctx.data.buf.out;
  2175. buf_len = instr->ctx.data.len;
  2176. nbufs++;
  2177. desc = gpmi_chain_data_write(this, buf_write, buf_len);
  2178. break;
  2179. case NAND_OP_DATA_IN_INSTR:
  2180. if (!instr->ctx.data.len)
  2181. break;
  2182. buf_read = instr->ctx.data.buf.in;
  2183. buf_len = instr->ctx.data.len;
  2184. nbufs++;
  2185. desc = gpmi_chain_data_read(this, buf_read, buf_len,
  2186. &direct);
  2187. break;
  2188. }
  2189. if (!desc) {
  2190. ret = -ENXIO;
  2191. goto unmap;
  2192. }
  2193. }
  2194. dev_dbg(this->dev, "%s setup done\n", __func__);
  2195. if (nbufs > 1) {
  2196. dev_err(this->dev, "Multiple data instructions not supported\n");
  2197. ret = -EINVAL;
  2198. goto unmap;
  2199. }
  2200. if (this->bch) {
  2201. writel(this->bch_flashlayout0,
  2202. this->resources.bch_regs + HW_BCH_FLASH0LAYOUT0);
  2203. writel(this->bch_flashlayout1,
  2204. this->resources.bch_regs + HW_BCH_FLASH0LAYOUT1);
  2205. }
  2206. desc->callback = dma_irq_callback;
  2207. desc->callback_param = this;
  2208. dma_completion = &this->dma_done;
  2209. bch_completion = NULL;
  2210. init_completion(dma_completion);
  2211. if (this->bch && buf_read) {
  2212. writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
  2213. this->resources.bch_regs + HW_BCH_CTRL_SET);
  2214. bch_completion = &this->bch_done;
  2215. init_completion(bch_completion);
  2216. }
  2217. dmaengine_submit(desc);
  2218. dma_async_issue_pending(get_dma_chan(this));
  2219. to = wait_for_completion_timeout(dma_completion, msecs_to_jiffies(1000));
  2220. if (!to) {
  2221. dev_err(this->dev, "DMA timeout, last DMA\n");
  2222. gpmi_dump_info(this);
  2223. ret = -ETIMEDOUT;
  2224. goto unmap;
  2225. }
  2226. if (this->bch && buf_read) {
  2227. to = wait_for_completion_timeout(bch_completion, msecs_to_jiffies(1000));
  2228. if (!to) {
  2229. dev_err(this->dev, "BCH timeout, last DMA\n");
  2230. gpmi_dump_info(this);
  2231. ret = -ETIMEDOUT;
  2232. goto unmap;
  2233. }
  2234. }
  2235. writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
  2236. this->resources.bch_regs + HW_BCH_CTRL_CLR);
  2237. gpmi_clear_bch(this);
  2238. ret = 0;
  2239. unmap:
  2240. for (i = 0; i < this->ntransfers; i++) {
  2241. struct gpmi_transfer *transfer = &this->transfers[i];
  2242. if (transfer->direction != DMA_NONE)
  2243. dma_unmap_sg(this->dev, &transfer->sgl, 1,
  2244. transfer->direction);
  2245. }
  2246. if (!ret && buf_read && !direct)
  2247. memcpy(buf_read, this->data_buffer_dma,
  2248. gpmi_raw_len_to_len(this, buf_len));
  2249. this->bch = false;
  2250. out_pm:
  2251. pm_runtime_mark_last_busy(this->dev);
  2252. pm_runtime_put_autosuspend(this->dev);
  2253. return ret;
  2254. }
  2255. static const struct nand_controller_ops gpmi_nand_controller_ops = {
  2256. .attach_chip = gpmi_nand_attach_chip,
  2257. .setup_interface = gpmi_setup_interface,
  2258. .exec_op = gpmi_nfc_exec_op,
  2259. };
  2260. static int gpmi_nand_init(struct gpmi_nand_data *this)
  2261. {
  2262. struct nand_chip *chip = &this->nand;
  2263. struct mtd_info *mtd = nand_to_mtd(chip);
  2264. int ret;
  2265. /* init the MTD data structures */
  2266. mtd->name = "gpmi-nand";
  2267. mtd->dev.parent = this->dev;
  2268. /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
  2269. nand_set_controller_data(chip, this);
  2270. nand_set_flash_node(chip, this->pdev->dev.of_node);
  2271. chip->legacy.block_markbad = gpmi_block_markbad;
  2272. chip->badblock_pattern = &gpmi_bbt_descr;
  2273. chip->options |= NAND_NO_SUBPAGE_WRITE;
  2274. /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
  2275. this->swap_block_mark = !GPMI_IS_MX23(this);
  2276. /*
  2277. * Allocate a temporary DMA buffer for reading ID in the
  2278. * nand_scan_ident().
  2279. */
  2280. this->bch_geometry.payload_size = 1024;
  2281. this->bch_geometry.auxiliary_size = 128;
  2282. ret = gpmi_alloc_dma_buffer(this);
  2283. if (ret)
  2284. return ret;
  2285. nand_controller_init(&this->base);
  2286. this->base.ops = &gpmi_nand_controller_ops;
  2287. chip->controller = &this->base;
  2288. ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1);
  2289. if (ret)
  2290. goto err_out;
  2291. ret = nand_boot_init(this);
  2292. if (ret)
  2293. goto err_nand_cleanup;
  2294. ret = nand_create_bbt(chip);
  2295. if (ret)
  2296. goto err_nand_cleanup;
  2297. ret = mtd_device_register(mtd, NULL, 0);
  2298. if (ret)
  2299. goto err_nand_cleanup;
  2300. return 0;
  2301. err_nand_cleanup:
  2302. nand_cleanup(chip);
  2303. err_out:
  2304. gpmi_free_dma_buffer(this);
  2305. return ret;
  2306. }
  2307. static const struct of_device_id gpmi_nand_id_table[] = {
  2308. { .compatible = "fsl,imx23-gpmi-nand", .data = &gpmi_devdata_imx23, },
  2309. { .compatible = "fsl,imx28-gpmi-nand", .data = &gpmi_devdata_imx28, },
  2310. { .compatible = "fsl,imx6q-gpmi-nand", .data = &gpmi_devdata_imx6q, },
  2311. { .compatible = "fsl,imx6sx-gpmi-nand", .data = &gpmi_devdata_imx6sx, },
  2312. { .compatible = "fsl,imx7d-gpmi-nand", .data = &gpmi_devdata_imx7d,},
  2313. {}
  2314. };
  2315. MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
  2316. static int gpmi_nand_probe(struct platform_device *pdev)
  2317. {
  2318. struct gpmi_nand_data *this;
  2319. int ret;
  2320. this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
  2321. if (!this)
  2322. return -ENOMEM;
  2323. this->devdata = of_device_get_match_data(&pdev->dev);
  2324. platform_set_drvdata(pdev, this);
  2325. this->pdev = pdev;
  2326. this->dev = &pdev->dev;
  2327. ret = acquire_resources(this);
  2328. if (ret)
  2329. goto exit_acquire_resources;
  2330. ret = __gpmi_enable_clk(this, true);
  2331. if (ret)
  2332. goto exit_acquire_resources;
  2333. pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
  2334. pm_runtime_use_autosuspend(&pdev->dev);
  2335. pm_runtime_set_active(&pdev->dev);
  2336. pm_runtime_enable(&pdev->dev);
  2337. pm_runtime_get_sync(&pdev->dev);
  2338. ret = gpmi_init(this);
  2339. if (ret)
  2340. goto exit_nfc_init;
  2341. ret = gpmi_nand_init(this);
  2342. if (ret)
  2343. goto exit_nfc_init;
  2344. pm_runtime_mark_last_busy(&pdev->dev);
  2345. pm_runtime_put_autosuspend(&pdev->dev);
  2346. dev_info(this->dev, "driver registered.\n");
  2347. return 0;
  2348. exit_nfc_init:
  2349. pm_runtime_put(&pdev->dev);
  2350. pm_runtime_disable(&pdev->dev);
  2351. release_resources(this);
  2352. exit_acquire_resources:
  2353. return ret;
  2354. }
  2355. static int gpmi_nand_remove(struct platform_device *pdev)
  2356. {
  2357. struct gpmi_nand_data *this = platform_get_drvdata(pdev);
  2358. struct nand_chip *chip = &this->nand;
  2359. int ret;
  2360. pm_runtime_put_sync(&pdev->dev);
  2361. pm_runtime_disable(&pdev->dev);
  2362. ret = mtd_device_unregister(nand_to_mtd(chip));
  2363. WARN_ON(ret);
  2364. nand_cleanup(chip);
  2365. gpmi_free_dma_buffer(this);
  2366. release_resources(this);
  2367. return 0;
  2368. }
  2369. #ifdef CONFIG_PM_SLEEP
  2370. static int gpmi_pm_suspend(struct device *dev)
  2371. {
  2372. struct gpmi_nand_data *this = dev_get_drvdata(dev);
  2373. release_dma_channels(this);
  2374. return 0;
  2375. }
  2376. static int gpmi_pm_resume(struct device *dev)
  2377. {
  2378. struct gpmi_nand_data *this = dev_get_drvdata(dev);
  2379. int ret;
  2380. ret = acquire_dma_channels(this);
  2381. if (ret < 0)
  2382. return ret;
  2383. /* re-init the GPMI registers */
  2384. ret = gpmi_init(this);
  2385. if (ret) {
  2386. dev_err(this->dev, "Error setting GPMI : %d\n", ret);
  2387. return ret;
  2388. }
  2389. /* Set flag to get timing setup restored for next exec_op */
  2390. if (this->hw.clk_rate)
  2391. this->hw.must_apply_timings = true;
  2392. /* re-init the BCH registers */
  2393. ret = bch_set_geometry(this);
  2394. if (ret) {
  2395. dev_err(this->dev, "Error setting BCH : %d\n", ret);
  2396. return ret;
  2397. }
  2398. return 0;
  2399. }
  2400. #endif /* CONFIG_PM_SLEEP */
  2401. static int __maybe_unused gpmi_runtime_suspend(struct device *dev)
  2402. {
  2403. struct gpmi_nand_data *this = dev_get_drvdata(dev);
  2404. return __gpmi_enable_clk(this, false);
  2405. }
  2406. static int __maybe_unused gpmi_runtime_resume(struct device *dev)
  2407. {
  2408. struct gpmi_nand_data *this = dev_get_drvdata(dev);
  2409. return __gpmi_enable_clk(this, true);
  2410. }
  2411. static const struct dev_pm_ops gpmi_pm_ops = {
  2412. SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
  2413. SET_RUNTIME_PM_OPS(gpmi_runtime_suspend, gpmi_runtime_resume, NULL)
  2414. };
  2415. static struct platform_driver gpmi_nand_driver = {
  2416. .driver = {
  2417. .name = "gpmi-nand",
  2418. .pm = &gpmi_pm_ops,
  2419. .of_match_table = gpmi_nand_id_table,
  2420. },
  2421. .probe = gpmi_nand_probe,
  2422. .remove = gpmi_nand_remove,
  2423. };
  2424. module_platform_driver(gpmi_nand_driver);
  2425. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  2426. MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
  2427. MODULE_LICENSE("GPL");