marvell_nand.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Marvell NAND flash controller driver
  4. *
  5. * Copyright (C) 2017 Marvell
  6. * Author: Miquel RAYNAL <[email protected]>
  7. *
  8. *
  9. * This NAND controller driver handles two versions of the hardware,
  10. * one is called NFCv1 and is available on PXA SoCs and the other is
  11. * called NFCv2 and is available on Armada SoCs.
  12. *
  13. * The main visible difference is that NFCv1 only has Hamming ECC
  14. * capabilities, while NFCv2 also embeds a BCH ECC engine. Also, DMA
  15. * is not used with NFCv2.
  16. *
  17. * The ECC layouts are depicted in details in Marvell AN-379, but here
  18. * is a brief description.
  19. *
  20. * When using Hamming, the data is split in 512B chunks (either 1, 2
  21. * or 4) and each chunk will have its own ECC "digest" of 6B at the
  22. * beginning of the OOB area and eventually the remaining free OOB
  23. * bytes (also called "spare" bytes in the driver). This engine
  24. * corrects up to 1 bit per chunk and detects reliably an error if
  25. * there are at most 2 bitflips. Here is the page layout used by the
  26. * controller when Hamming is chosen:
  27. *
  28. * +-------------------------------------------------------------+
  29. * | Data 1 | ... | Data N | ECC 1 | ... | ECCN | Free OOB bytes |
  30. * +-------------------------------------------------------------+
  31. *
  32. * When using the BCH engine, there are N identical (data + free OOB +
  33. * ECC) sections and potentially an extra one to deal with
  34. * configurations where the chosen (data + free OOB + ECC) sizes do
  35. * not align with the page (data + OOB) size. ECC bytes are always
  36. * 30B per ECC chunk. Here is the page layout used by the controller
  37. * when BCH is chosen:
  38. *
  39. * +-----------------------------------------
  40. * | Data 1 | Free OOB bytes 1 | ECC 1 | ...
  41. * +-----------------------------------------
  42. *
  43. * -------------------------------------------
  44. * ... | Data N | Free OOB bytes N | ECC N |
  45. * -------------------------------------------
  46. *
  47. * --------------------------------------------+
  48. * Last Data | Last Free OOB bytes | Last ECC |
  49. * --------------------------------------------+
  50. *
  51. * In both cases, the layout seen by the user is always: all data
  52. * first, then all free OOB bytes and finally all ECC bytes. With BCH,
  53. * ECC bytes are 30B long and are padded with 0xFF to align on 32
  54. * bytes.
  55. *
  56. * The controller has certain limitations that are handled by the
  57. * driver:
  58. * - It can only read 2k at a time. To overcome this limitation, the
  59. * driver issues data cycles on the bus, without issuing new
  60. * CMD + ADDR cycles. The Marvell term is "naked" operations.
  61. * - The ECC strength in BCH mode cannot be tuned. It is fixed 16
  62. * bits. What can be tuned is the ECC block size as long as it
  63. * stays between 512B and 2kiB. It's usually chosen based on the
  64. * chip ECC requirements. For instance, using 2kiB ECC chunks
  65. * provides 4b/512B correctability.
  66. * - The controller will always treat data bytes, free OOB bytes
  67. * and ECC bytes in that order, no matter what the real layout is
  68. * (which is usually all data then all OOB bytes). The
  69. * marvell_nfc_layouts array below contains the currently
  70. * supported layouts.
  71. * - Because of these weird layouts, the Bad Block Markers can be
  72. * located in data section. In this case, the NAND_BBT_NO_OOB_BBM
  73. * option must be set to prevent scanning/writing bad block
  74. * markers.
  75. */
  76. #include <linux/module.h>
  77. #include <linux/clk.h>
  78. #include <linux/mtd/rawnand.h>
  79. #include <linux/of_platform.h>
  80. #include <linux/iopoll.h>
  81. #include <linux/interrupt.h>
  82. #include <linux/slab.h>
  83. #include <linux/mfd/syscon.h>
  84. #include <linux/regmap.h>
  85. #include <asm/unaligned.h>
  86. #include <linux/dmaengine.h>
  87. #include <linux/dma-mapping.h>
  88. #include <linux/dma/pxa-dma.h>
  89. #include <linux/platform_data/mtd-nand-pxa3xx.h>
  90. /* Data FIFO granularity, FIFO reads/writes must be a multiple of this length */
  91. #define FIFO_DEPTH 8
  92. #define FIFO_REP(x) (x / sizeof(u32))
  93. #define BCH_SEQ_READS (32 / FIFO_DEPTH)
  94. /* NFC does not support transfers of larger chunks at a time */
  95. #define MAX_CHUNK_SIZE 2112
  96. /* NFCv1 cannot read more that 7 bytes of ID */
  97. #define NFCV1_READID_LEN 7
  98. /* Polling is done at a pace of POLL_PERIOD us until POLL_TIMEOUT is reached */
  99. #define POLL_PERIOD 0
  100. #define POLL_TIMEOUT 100000
  101. /* Interrupt maximum wait period in ms */
  102. #define IRQ_TIMEOUT 1000
  103. /* Latency in clock cycles between SoC pins and NFC logic */
  104. #define MIN_RD_DEL_CNT 3
  105. /* Maximum number of contiguous address cycles */
  106. #define MAX_ADDRESS_CYC_NFCV1 5
  107. #define MAX_ADDRESS_CYC_NFCV2 7
  108. /* System control registers/bits to enable the NAND controller on some SoCs */
  109. #define GENCONF_SOC_DEVICE_MUX 0x208
  110. #define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0)
  111. #define GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST BIT(20)
  112. #define GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST BIT(21)
  113. #define GENCONF_SOC_DEVICE_MUX_NFC_INT_EN BIT(25)
  114. #define GENCONF_CLK_GATING_CTRL 0x220
  115. #define GENCONF_CLK_GATING_CTRL_ND_GATE BIT(2)
  116. #define GENCONF_ND_CLK_CTRL 0x700
  117. #define GENCONF_ND_CLK_CTRL_EN BIT(0)
  118. /* NAND controller data flash control register */
  119. #define NDCR 0x00
  120. #define NDCR_ALL_INT GENMASK(11, 0)
  121. #define NDCR_CS1_CMDDM BIT(7)
  122. #define NDCR_CS0_CMDDM BIT(8)
  123. #define NDCR_RDYM BIT(11)
  124. #define NDCR_ND_ARB_EN BIT(12)
  125. #define NDCR_RA_START BIT(15)
  126. #define NDCR_RD_ID_CNT(x) (min_t(unsigned int, x, 0x7) << 16)
  127. #define NDCR_PAGE_SZ(x) (x >= 2048 ? BIT(24) : 0)
  128. #define NDCR_DWIDTH_M BIT(26)
  129. #define NDCR_DWIDTH_C BIT(27)
  130. #define NDCR_ND_RUN BIT(28)
  131. #define NDCR_DMA_EN BIT(29)
  132. #define NDCR_ECC_EN BIT(30)
  133. #define NDCR_SPARE_EN BIT(31)
  134. #define NDCR_GENERIC_FIELDS_MASK (~(NDCR_RA_START | NDCR_PAGE_SZ(2048) | \
  135. NDCR_DWIDTH_M | NDCR_DWIDTH_C))
  136. /* NAND interface timing parameter 0 register */
  137. #define NDTR0 0x04
  138. #define NDTR0_TRP(x) ((min_t(unsigned int, x, 0xF) & 0x7) << 0)
  139. #define NDTR0_TRH(x) (min_t(unsigned int, x, 0x7) << 3)
  140. #define NDTR0_ETRP(x) ((min_t(unsigned int, x, 0xF) & 0x8) << 3)
  141. #define NDTR0_SEL_NRE_EDGE BIT(7)
  142. #define NDTR0_TWP(x) (min_t(unsigned int, x, 0x7) << 8)
  143. #define NDTR0_TWH(x) (min_t(unsigned int, x, 0x7) << 11)
  144. #define NDTR0_TCS(x) (min_t(unsigned int, x, 0x7) << 16)
  145. #define NDTR0_TCH(x) (min_t(unsigned int, x, 0x7) << 19)
  146. #define NDTR0_RD_CNT_DEL(x) (min_t(unsigned int, x, 0xF) << 22)
  147. #define NDTR0_SELCNTR BIT(26)
  148. #define NDTR0_TADL(x) (min_t(unsigned int, x, 0x1F) << 27)
  149. /* NAND interface timing parameter 1 register */
  150. #define NDTR1 0x0C
  151. #define NDTR1_TAR(x) (min_t(unsigned int, x, 0xF) << 0)
  152. #define NDTR1_TWHR(x) (min_t(unsigned int, x, 0xF) << 4)
  153. #define NDTR1_TRHW(x) (min_t(unsigned int, x / 16, 0x3) << 8)
  154. #define NDTR1_PRESCALE BIT(14)
  155. #define NDTR1_WAIT_MODE BIT(15)
  156. #define NDTR1_TR(x) (min_t(unsigned int, x, 0xFFFF) << 16)
  157. /* NAND controller status register */
  158. #define NDSR 0x14
  159. #define NDSR_WRCMDREQ BIT(0)
  160. #define NDSR_RDDREQ BIT(1)
  161. #define NDSR_WRDREQ BIT(2)
  162. #define NDSR_CORERR BIT(3)
  163. #define NDSR_UNCERR BIT(4)
  164. #define NDSR_CMDD(cs) BIT(8 - cs)
  165. #define NDSR_RDY(rb) BIT(11 + rb)
  166. #define NDSR_ERRCNT(x) ((x >> 16) & 0x1F)
  167. /* NAND ECC control register */
  168. #define NDECCCTRL 0x28
  169. #define NDECCCTRL_BCH_EN BIT(0)
  170. /* NAND controller data buffer register */
  171. #define NDDB 0x40
  172. /* NAND controller command buffer 0 register */
  173. #define NDCB0 0x48
  174. #define NDCB0_CMD1(x) ((x & 0xFF) << 0)
  175. #define NDCB0_CMD2(x) ((x & 0xFF) << 8)
  176. #define NDCB0_ADDR_CYC(x) ((x & 0x7) << 16)
  177. #define NDCB0_ADDR_GET_NUM_CYC(x) (((x) >> 16) & 0x7)
  178. #define NDCB0_DBC BIT(19)
  179. #define NDCB0_CMD_TYPE(x) ((x & 0x7) << 21)
  180. #define NDCB0_CSEL BIT(24)
  181. #define NDCB0_RDY_BYP BIT(27)
  182. #define NDCB0_LEN_OVRD BIT(28)
  183. #define NDCB0_CMD_XTYPE(x) ((x & 0x7) << 29)
  184. /* NAND controller command buffer 1 register */
  185. #define NDCB1 0x4C
  186. #define NDCB1_COLS(x) ((x & 0xFFFF) << 0)
  187. #define NDCB1_ADDRS_PAGE(x) (x << 16)
  188. /* NAND controller command buffer 2 register */
  189. #define NDCB2 0x50
  190. #define NDCB2_ADDR5_PAGE(x) (((x >> 16) & 0xFF) << 0)
  191. #define NDCB2_ADDR5_CYC(x) ((x & 0xFF) << 0)
  192. /* NAND controller command buffer 3 register */
  193. #define NDCB3 0x54
  194. #define NDCB3_ADDR6_CYC(x) ((x & 0xFF) << 16)
  195. #define NDCB3_ADDR7_CYC(x) ((x & 0xFF) << 24)
  196. /* NAND controller command buffer 0 register 'type' and 'xtype' fields */
  197. #define TYPE_READ 0
  198. #define TYPE_WRITE 1
  199. #define TYPE_ERASE 2
  200. #define TYPE_READ_ID 3
  201. #define TYPE_STATUS 4
  202. #define TYPE_RESET 5
  203. #define TYPE_NAKED_CMD 6
  204. #define TYPE_NAKED_ADDR 7
  205. #define TYPE_MASK 7
  206. #define XTYPE_MONOLITHIC_RW 0
  207. #define XTYPE_LAST_NAKED_RW 1
  208. #define XTYPE_FINAL_COMMAND 3
  209. #define XTYPE_READ 4
  210. #define XTYPE_WRITE_DISPATCH 4
  211. #define XTYPE_NAKED_RW 5
  212. #define XTYPE_COMMAND_DISPATCH 6
  213. #define XTYPE_MASK 7
  214. /**
  215. * struct marvell_hw_ecc_layout - layout of Marvell ECC
  216. *
  217. * Marvell ECC engine works differently than the others, in order to limit the
  218. * size of the IP, hardware engineers chose to set a fixed strength at 16 bits
  219. * per subpage, and depending on a the desired strength needed by the NAND chip,
  220. * a particular layout mixing data/spare/ecc is defined, with a possible last
  221. * chunk smaller that the others.
  222. *
  223. * @writesize: Full page size on which the layout applies
  224. * @chunk: Desired ECC chunk size on which the layout applies
  225. * @strength: Desired ECC strength (per chunk size bytes) on which the
  226. * layout applies
  227. * @nchunks: Total number of chunks
  228. * @full_chunk_cnt: Number of full-sized chunks, which is the number of
  229. * repetitions of the pattern:
  230. * (data_bytes + spare_bytes + ecc_bytes).
  231. * @data_bytes: Number of data bytes per chunk
  232. * @spare_bytes: Number of spare bytes per chunk
  233. * @ecc_bytes: Number of ecc bytes per chunk
  234. * @last_data_bytes: Number of data bytes in the last chunk
  235. * @last_spare_bytes: Number of spare bytes in the last chunk
  236. * @last_ecc_bytes: Number of ecc bytes in the last chunk
  237. */
  238. struct marvell_hw_ecc_layout {
  239. /* Constraints */
  240. int writesize;
  241. int chunk;
  242. int strength;
  243. /* Corresponding layout */
  244. int nchunks;
  245. int full_chunk_cnt;
  246. int data_bytes;
  247. int spare_bytes;
  248. int ecc_bytes;
  249. int last_data_bytes;
  250. int last_spare_bytes;
  251. int last_ecc_bytes;
  252. };
  253. #define MARVELL_LAYOUT(ws, dc, ds, nc, fcc, db, sb, eb, ldb, lsb, leb) \
  254. { \
  255. .writesize = ws, \
  256. .chunk = dc, \
  257. .strength = ds, \
  258. .nchunks = nc, \
  259. .full_chunk_cnt = fcc, \
  260. .data_bytes = db, \
  261. .spare_bytes = sb, \
  262. .ecc_bytes = eb, \
  263. .last_data_bytes = ldb, \
  264. .last_spare_bytes = lsb, \
  265. .last_ecc_bytes = leb, \
  266. }
  267. /* Layouts explained in AN-379_Marvell_SoC_NFC_ECC */
  268. static const struct marvell_hw_ecc_layout marvell_nfc_layouts[] = {
  269. MARVELL_LAYOUT( 512, 512, 1, 1, 1, 512, 8, 8, 0, 0, 0),
  270. MARVELL_LAYOUT( 2048, 512, 1, 1, 1, 2048, 40, 24, 0, 0, 0),
  271. MARVELL_LAYOUT( 2048, 512, 4, 1, 1, 2048, 32, 30, 0, 0, 0),
  272. MARVELL_LAYOUT( 2048, 512, 8, 2, 1, 1024, 0, 30,1024,32, 30),
  273. MARVELL_LAYOUT( 4096, 512, 4, 2, 2, 2048, 32, 30, 0, 0, 0),
  274. MARVELL_LAYOUT( 4096, 512, 8, 5, 4, 1024, 0, 30, 0, 64, 30),
  275. MARVELL_LAYOUT( 8192, 512, 4, 4, 4, 2048, 0, 30, 0, 0, 0),
  276. MARVELL_LAYOUT( 8192, 512, 8, 9, 8, 1024, 0, 30, 0, 160, 30),
  277. };
  278. /**
  279. * struct marvell_nand_chip_sel - CS line description
  280. *
  281. * The Nand Flash Controller has up to 4 CE and 2 RB pins. The CE selection
  282. * is made by a field in NDCB0 register, and in another field in NDCB2 register.
  283. * The datasheet describes the logic with an error: ADDR5 field is once
  284. * declared at the beginning of NDCB2, and another time at its end. Because the
  285. * ADDR5 field of NDCB2 may be used by other bytes, it would be more logical
  286. * to use the last bit of this field instead of the first ones.
  287. *
  288. * @cs: Wanted CE lane.
  289. * @ndcb0_csel: Value of the NDCB0 register with or without the flag
  290. * selecting the wanted CE lane. This is set once when
  291. * the Device Tree is probed.
  292. * @rb: Ready/Busy pin for the flash chip
  293. */
  294. struct marvell_nand_chip_sel {
  295. unsigned int cs;
  296. u32 ndcb0_csel;
  297. unsigned int rb;
  298. };
  299. /**
  300. * struct marvell_nand_chip - stores NAND chip device related information
  301. *
  302. * @chip: Base NAND chip structure
  303. * @node: Used to store NAND chips into a list
  304. * @layout: NAND layout when using hardware ECC
  305. * @ndcr: Controller register value for this NAND chip
  306. * @ndtr0: Timing registers 0 value for this NAND chip
  307. * @ndtr1: Timing registers 1 value for this NAND chip
  308. * @addr_cyc: Amount of cycles needed to pass column address
  309. * @selected_die: Current active CS
  310. * @nsels: Number of CS lines required by the NAND chip
  311. * @sels: Array of CS lines descriptions
  312. */
  313. struct marvell_nand_chip {
  314. struct nand_chip chip;
  315. struct list_head node;
  316. const struct marvell_hw_ecc_layout *layout;
  317. u32 ndcr;
  318. u32 ndtr0;
  319. u32 ndtr1;
  320. int addr_cyc;
  321. int selected_die;
  322. unsigned int nsels;
  323. struct marvell_nand_chip_sel sels[];
  324. };
  325. static inline struct marvell_nand_chip *to_marvell_nand(struct nand_chip *chip)
  326. {
  327. return container_of(chip, struct marvell_nand_chip, chip);
  328. }
  329. static inline struct marvell_nand_chip_sel *to_nand_sel(struct marvell_nand_chip
  330. *nand)
  331. {
  332. return &nand->sels[nand->selected_die];
  333. }
  334. /**
  335. * struct marvell_nfc_caps - NAND controller capabilities for distinction
  336. * between compatible strings
  337. *
  338. * @max_cs_nb: Number of Chip Select lines available
  339. * @max_rb_nb: Number of Ready/Busy lines available
  340. * @need_system_controller: Indicates if the SoC needs to have access to the
  341. * system controller (ie. to enable the NAND controller)
  342. * @legacy_of_bindings: Indicates if DT parsing must be done using the old
  343. * fashion way
  344. * @is_nfcv2: NFCv2 has numerous enhancements compared to NFCv1, ie.
  345. * BCH error detection and correction algorithm,
  346. * NDCB3 register has been added
  347. * @use_dma: Use dma for data transfers
  348. */
  349. struct marvell_nfc_caps {
  350. unsigned int max_cs_nb;
  351. unsigned int max_rb_nb;
  352. bool need_system_controller;
  353. bool legacy_of_bindings;
  354. bool is_nfcv2;
  355. bool use_dma;
  356. };
  357. /**
  358. * struct marvell_nfc - stores Marvell NAND controller information
  359. *
  360. * @controller: Base controller structure
  361. * @dev: Parent device (used to print error messages)
  362. * @regs: NAND controller registers
  363. * @core_clk: Core clock
  364. * @reg_clk: Registers clock
  365. * @complete: Completion object to wait for NAND controller events
  366. * @assigned_cs: Bitmask describing already assigned CS lines
  367. * @chips: List containing all the NAND chips attached to
  368. * this NAND controller
  369. * @selected_chip: Currently selected target chip
  370. * @caps: NAND controller capabilities for each compatible string
  371. * @use_dma: Whetner DMA is used
  372. * @dma_chan: DMA channel (NFCv1 only)
  373. * @dma_buf: 32-bit aligned buffer for DMA transfers (NFCv1 only)
  374. */
  375. struct marvell_nfc {
  376. struct nand_controller controller;
  377. struct device *dev;
  378. void __iomem *regs;
  379. struct clk *core_clk;
  380. struct clk *reg_clk;
  381. struct completion complete;
  382. unsigned long assigned_cs;
  383. struct list_head chips;
  384. struct nand_chip *selected_chip;
  385. const struct marvell_nfc_caps *caps;
  386. /* DMA (NFCv1 only) */
  387. bool use_dma;
  388. struct dma_chan *dma_chan;
  389. u8 *dma_buf;
  390. };
  391. static inline struct marvell_nfc *to_marvell_nfc(struct nand_controller *ctrl)
  392. {
  393. return container_of(ctrl, struct marvell_nfc, controller);
  394. }
  395. /**
  396. * struct marvell_nfc_timings - NAND controller timings expressed in NAND
  397. * Controller clock cycles
  398. *
  399. * @tRP: ND_nRE pulse width
  400. * @tRH: ND_nRE high duration
  401. * @tWP: ND_nWE pulse time
  402. * @tWH: ND_nWE high duration
  403. * @tCS: Enable signal setup time
  404. * @tCH: Enable signal hold time
  405. * @tADL: Address to write data delay
  406. * @tAR: ND_ALE low to ND_nRE low delay
  407. * @tWHR: ND_nWE high to ND_nRE low for status read
  408. * @tRHW: ND_nRE high duration, read to write delay
  409. * @tR: ND_nWE high to ND_nRE low for read
  410. */
  411. struct marvell_nfc_timings {
  412. /* NDTR0 fields */
  413. unsigned int tRP;
  414. unsigned int tRH;
  415. unsigned int tWP;
  416. unsigned int tWH;
  417. unsigned int tCS;
  418. unsigned int tCH;
  419. unsigned int tADL;
  420. /* NDTR1 fields */
  421. unsigned int tAR;
  422. unsigned int tWHR;
  423. unsigned int tRHW;
  424. unsigned int tR;
  425. };
  426. /**
  427. * TO_CYCLES() - Derives a duration in numbers of clock cycles.
  428. *
  429. * @ps: Duration in pico-seconds
  430. * @period_ns: Clock period in nano-seconds
  431. *
  432. * Convert the duration in nano-seconds, then divide by the period and
  433. * return the number of clock periods.
  434. */
  435. #define TO_CYCLES(ps, period_ns) (DIV_ROUND_UP(ps / 1000, period_ns))
  436. #define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \
  437. period_ns))
  438. /**
  439. * struct marvell_nfc_op - filled during the parsing of the ->exec_op()
  440. * subop subset of instructions.
  441. *
  442. * @ndcb: Array of values written to NDCBx registers
  443. * @cle_ale_delay_ns: Optional delay after the last CMD or ADDR cycle
  444. * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin
  445. * @rdy_delay_ns: Optional delay after waiting for the RB pin
  446. * @data_delay_ns: Optional delay after the data xfer
  447. * @data_instr_idx: Index of the data instruction in the subop
  448. * @data_instr: Pointer to the data instruction in the subop
  449. */
  450. struct marvell_nfc_op {
  451. u32 ndcb[4];
  452. unsigned int cle_ale_delay_ns;
  453. unsigned int rdy_timeout_ms;
  454. unsigned int rdy_delay_ns;
  455. unsigned int data_delay_ns;
  456. unsigned int data_instr_idx;
  457. const struct nand_op_instr *data_instr;
  458. };
  459. /*
  460. * Internal helper to conditionnally apply a delay (from the above structure,
  461. * most of the time).
  462. */
  463. static void cond_delay(unsigned int ns)
  464. {
  465. if (!ns)
  466. return;
  467. if (ns < 10000)
  468. ndelay(ns);
  469. else
  470. udelay(DIV_ROUND_UP(ns, 1000));
  471. }
  472. /*
  473. * The controller has many flags that could generate interrupts, most of them
  474. * are disabled and polling is used. For the very slow signals, using interrupts
  475. * may relax the CPU charge.
  476. */
  477. static void marvell_nfc_disable_int(struct marvell_nfc *nfc, u32 int_mask)
  478. {
  479. u32 reg;
  480. /* Writing 1 disables the interrupt */
  481. reg = readl_relaxed(nfc->regs + NDCR);
  482. writel_relaxed(reg | int_mask, nfc->regs + NDCR);
  483. }
  484. static void marvell_nfc_enable_int(struct marvell_nfc *nfc, u32 int_mask)
  485. {
  486. u32 reg;
  487. /* Writing 0 enables the interrupt */
  488. reg = readl_relaxed(nfc->regs + NDCR);
  489. writel_relaxed(reg & ~int_mask, nfc->regs + NDCR);
  490. }
  491. static u32 marvell_nfc_clear_int(struct marvell_nfc *nfc, u32 int_mask)
  492. {
  493. u32 reg;
  494. reg = readl_relaxed(nfc->regs + NDSR);
  495. writel_relaxed(int_mask, nfc->regs + NDSR);
  496. return reg & int_mask;
  497. }
  498. static void marvell_nfc_force_byte_access(struct nand_chip *chip,
  499. bool force_8bit)
  500. {
  501. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  502. u32 ndcr;
  503. /*
  504. * Callers of this function do not verify if the NAND is using a 16-bit
  505. * an 8-bit bus for normal operations, so we need to take care of that
  506. * here by leaving the configuration unchanged if the NAND does not have
  507. * the NAND_BUSWIDTH_16 flag set.
  508. */
  509. if (!(chip->options & NAND_BUSWIDTH_16))
  510. return;
  511. ndcr = readl_relaxed(nfc->regs + NDCR);
  512. if (force_8bit)
  513. ndcr &= ~(NDCR_DWIDTH_M | NDCR_DWIDTH_C);
  514. else
  515. ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
  516. writel_relaxed(ndcr, nfc->regs + NDCR);
  517. }
  518. static int marvell_nfc_wait_ndrun(struct nand_chip *chip)
  519. {
  520. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  521. u32 val;
  522. int ret;
  523. /*
  524. * The command is being processed, wait for the ND_RUN bit to be
  525. * cleared by the NFC. If not, we must clear it by hand.
  526. */
  527. ret = readl_relaxed_poll_timeout(nfc->regs + NDCR, val,
  528. (val & NDCR_ND_RUN) == 0,
  529. POLL_PERIOD, POLL_TIMEOUT);
  530. if (ret) {
  531. dev_err(nfc->dev, "Timeout on NAND controller run mode\n");
  532. writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
  533. nfc->regs + NDCR);
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. /*
  539. * Any time a command has to be sent to the controller, the following sequence
  540. * has to be followed:
  541. * - call marvell_nfc_prepare_cmd()
  542. * -> activate the ND_RUN bit that will kind of 'start a job'
  543. * -> wait the signal indicating the NFC is waiting for a command
  544. * - send the command (cmd and address cycles)
  545. * - enventually send or receive the data
  546. * - call marvell_nfc_end_cmd() with the corresponding flag
  547. * -> wait the flag to be triggered or cancel the job with a timeout
  548. *
  549. * The following helpers are here to factorize the code a bit so that
  550. * specialized functions responsible for executing the actual NAND
  551. * operations do not have to replicate the same code blocks.
  552. */
  553. static int marvell_nfc_prepare_cmd(struct nand_chip *chip)
  554. {
  555. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  556. u32 ndcr, val;
  557. int ret;
  558. /* Poll ND_RUN and clear NDSR before issuing any command */
  559. ret = marvell_nfc_wait_ndrun(chip);
  560. if (ret) {
  561. dev_err(nfc->dev, "Last operation did not succeed\n");
  562. return ret;
  563. }
  564. ndcr = readl_relaxed(nfc->regs + NDCR);
  565. writel_relaxed(readl(nfc->regs + NDSR), nfc->regs + NDSR);
  566. /* Assert ND_RUN bit and wait the NFC to be ready */
  567. writel_relaxed(ndcr | NDCR_ND_RUN, nfc->regs + NDCR);
  568. ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
  569. val & NDSR_WRCMDREQ,
  570. POLL_PERIOD, POLL_TIMEOUT);
  571. if (ret) {
  572. dev_err(nfc->dev, "Timeout on WRCMDRE\n");
  573. return -ETIMEDOUT;
  574. }
  575. /* Command may be written, clear WRCMDREQ status bit */
  576. writel_relaxed(NDSR_WRCMDREQ, nfc->regs + NDSR);
  577. return 0;
  578. }
  579. static void marvell_nfc_send_cmd(struct nand_chip *chip,
  580. struct marvell_nfc_op *nfc_op)
  581. {
  582. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  583. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  584. dev_dbg(nfc->dev, "\nNDCR: 0x%08x\n"
  585. "NDCB0: 0x%08x\nNDCB1: 0x%08x\nNDCB2: 0x%08x\nNDCB3: 0x%08x\n",
  586. (u32)readl_relaxed(nfc->regs + NDCR), nfc_op->ndcb[0],
  587. nfc_op->ndcb[1], nfc_op->ndcb[2], nfc_op->ndcb[3]);
  588. writel_relaxed(to_nand_sel(marvell_nand)->ndcb0_csel | nfc_op->ndcb[0],
  589. nfc->regs + NDCB0);
  590. writel_relaxed(nfc_op->ndcb[1], nfc->regs + NDCB0);
  591. writel(nfc_op->ndcb[2], nfc->regs + NDCB0);
  592. /*
  593. * Write NDCB0 four times only if LEN_OVRD is set or if ADDR6 or ADDR7
  594. * fields are used (only available on NFCv2).
  595. */
  596. if (nfc_op->ndcb[0] & NDCB0_LEN_OVRD ||
  597. NDCB0_ADDR_GET_NUM_CYC(nfc_op->ndcb[0]) >= 6) {
  598. if (!WARN_ON_ONCE(!nfc->caps->is_nfcv2))
  599. writel(nfc_op->ndcb[3], nfc->regs + NDCB0);
  600. }
  601. }
  602. static int marvell_nfc_end_cmd(struct nand_chip *chip, int flag,
  603. const char *label)
  604. {
  605. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  606. u32 val;
  607. int ret;
  608. ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
  609. val & flag,
  610. POLL_PERIOD, POLL_TIMEOUT);
  611. if (ret) {
  612. dev_err(nfc->dev, "Timeout on %s (NDSR: 0x%08x)\n",
  613. label, val);
  614. if (nfc->dma_chan)
  615. dmaengine_terminate_all(nfc->dma_chan);
  616. return ret;
  617. }
  618. /*
  619. * DMA function uses this helper to poll on CMDD bits without wanting
  620. * them to be cleared.
  621. */
  622. if (nfc->use_dma && (readl_relaxed(nfc->regs + NDCR) & NDCR_DMA_EN))
  623. return 0;
  624. writel_relaxed(flag, nfc->regs + NDSR);
  625. return 0;
  626. }
  627. static int marvell_nfc_wait_cmdd(struct nand_chip *chip)
  628. {
  629. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  630. int cs_flag = NDSR_CMDD(to_nand_sel(marvell_nand)->ndcb0_csel);
  631. return marvell_nfc_end_cmd(chip, cs_flag, "CMDD");
  632. }
  633. static int marvell_nfc_poll_status(struct marvell_nfc *nfc, u32 mask,
  634. u32 expected_val, unsigned long timeout_ms)
  635. {
  636. unsigned long limit;
  637. u32 st;
  638. limit = jiffies + msecs_to_jiffies(timeout_ms);
  639. do {
  640. st = readl_relaxed(nfc->regs + NDSR);
  641. if (st & NDSR_RDY(1))
  642. st |= NDSR_RDY(0);
  643. if ((st & mask) == expected_val)
  644. return 0;
  645. cpu_relax();
  646. } while (time_after(limit, jiffies));
  647. return -ETIMEDOUT;
  648. }
  649. static int marvell_nfc_wait_op(struct nand_chip *chip, unsigned int timeout_ms)
  650. {
  651. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  652. struct mtd_info *mtd = nand_to_mtd(chip);
  653. u32 pending;
  654. int ret;
  655. /* Timeout is expressed in ms */
  656. if (!timeout_ms)
  657. timeout_ms = IRQ_TIMEOUT;
  658. if (mtd->oops_panic_write) {
  659. ret = marvell_nfc_poll_status(nfc, NDSR_RDY(0),
  660. NDSR_RDY(0),
  661. timeout_ms);
  662. } else {
  663. init_completion(&nfc->complete);
  664. marvell_nfc_enable_int(nfc, NDCR_RDYM);
  665. ret = wait_for_completion_timeout(&nfc->complete,
  666. msecs_to_jiffies(timeout_ms));
  667. marvell_nfc_disable_int(nfc, NDCR_RDYM);
  668. }
  669. pending = marvell_nfc_clear_int(nfc, NDSR_RDY(0) | NDSR_RDY(1));
  670. /*
  671. * In case the interrupt was not served in the required time frame,
  672. * check if the ISR was not served or if something went actually wrong.
  673. */
  674. if (!ret && !pending) {
  675. dev_err(nfc->dev, "Timeout waiting for RB signal\n");
  676. return -ETIMEDOUT;
  677. }
  678. return 0;
  679. }
  680. static void marvell_nfc_select_target(struct nand_chip *chip,
  681. unsigned int die_nr)
  682. {
  683. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  684. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  685. u32 ndcr_generic;
  686. /*
  687. * Reset the NDCR register to a clean state for this particular chip,
  688. * also clear ND_RUN bit.
  689. */
  690. ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
  691. NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
  692. writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
  693. /* Also reset the interrupt status register */
  694. marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
  695. if (chip == nfc->selected_chip && die_nr == marvell_nand->selected_die)
  696. return;
  697. writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
  698. writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
  699. nfc->selected_chip = chip;
  700. marvell_nand->selected_die = die_nr;
  701. }
  702. static irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
  703. {
  704. struct marvell_nfc *nfc = dev_id;
  705. u32 st = readl_relaxed(nfc->regs + NDSR);
  706. u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
  707. /*
  708. * RDY interrupt mask is one bit in NDCR while there are two status
  709. * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
  710. */
  711. if (st & NDSR_RDY(1))
  712. st |= NDSR_RDY(0);
  713. if (!(st & ien))
  714. return IRQ_NONE;
  715. marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
  716. if (st & (NDSR_RDY(0) | NDSR_RDY(1)))
  717. complete(&nfc->complete);
  718. return IRQ_HANDLED;
  719. }
  720. /* HW ECC related functions */
  721. static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
  722. {
  723. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  724. u32 ndcr = readl_relaxed(nfc->regs + NDCR);
  725. if (!(ndcr & NDCR_ECC_EN)) {
  726. writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
  727. /*
  728. * When enabling BCH, set threshold to 0 to always know the
  729. * number of corrected bitflips.
  730. */
  731. if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
  732. writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
  733. }
  734. }
  735. static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
  736. {
  737. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  738. u32 ndcr = readl_relaxed(nfc->regs + NDCR);
  739. if (ndcr & NDCR_ECC_EN) {
  740. writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
  741. if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
  742. writel_relaxed(0, nfc->regs + NDECCCTRL);
  743. }
  744. }
  745. /* DMA related helpers */
  746. static void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
  747. {
  748. u32 reg;
  749. reg = readl_relaxed(nfc->regs + NDCR);
  750. writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
  751. }
  752. static void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
  753. {
  754. u32 reg;
  755. reg = readl_relaxed(nfc->regs + NDCR);
  756. writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
  757. }
  758. /* Read/write PIO/DMA accessors */
  759. static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
  760. enum dma_data_direction direction,
  761. unsigned int len)
  762. {
  763. unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
  764. struct dma_async_tx_descriptor *tx;
  765. struct scatterlist sg;
  766. dma_cookie_t cookie;
  767. int ret;
  768. marvell_nfc_enable_dma(nfc);
  769. /* Prepare the DMA transfer */
  770. sg_init_one(&sg, nfc->dma_buf, dma_len);
  771. ret = dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
  772. if (!ret) {
  773. dev_err(nfc->dev, "Could not map DMA S/G list\n");
  774. return -ENXIO;
  775. }
  776. tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
  777. direction == DMA_FROM_DEVICE ?
  778. DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
  779. DMA_PREP_INTERRUPT);
  780. if (!tx) {
  781. dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
  782. dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
  783. return -ENXIO;
  784. }
  785. /* Do the task and wait for it to finish */
  786. cookie = dmaengine_submit(tx);
  787. ret = dma_submit_error(cookie);
  788. if (ret)
  789. return -EIO;
  790. dma_async_issue_pending(nfc->dma_chan);
  791. ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
  792. dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
  793. marvell_nfc_disable_dma(nfc);
  794. if (ret) {
  795. dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
  796. dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
  797. dmaengine_terminate_all(nfc->dma_chan);
  798. return -ETIMEDOUT;
  799. }
  800. return 0;
  801. }
  802. static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
  803. unsigned int len)
  804. {
  805. unsigned int last_len = len % FIFO_DEPTH;
  806. unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
  807. int i;
  808. for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
  809. ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
  810. if (last_len) {
  811. u8 tmp_buf[FIFO_DEPTH];
  812. ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
  813. memcpy(in + last_full_offset, tmp_buf, last_len);
  814. }
  815. return 0;
  816. }
  817. static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
  818. unsigned int len)
  819. {
  820. unsigned int last_len = len % FIFO_DEPTH;
  821. unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
  822. int i;
  823. for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
  824. iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
  825. if (last_len) {
  826. u8 tmp_buf[FIFO_DEPTH];
  827. memcpy(tmp_buf, out + last_full_offset, last_len);
  828. iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
  829. }
  830. return 0;
  831. }
  832. static void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
  833. u8 *data, int data_len,
  834. u8 *spare, int spare_len,
  835. u8 *ecc, int ecc_len,
  836. unsigned int *max_bitflips)
  837. {
  838. struct mtd_info *mtd = nand_to_mtd(chip);
  839. int bf;
  840. /*
  841. * Blank pages (all 0xFF) that have not been written may be recognized
  842. * as bad if bitflips occur, so whenever an uncorrectable error occurs,
  843. * check if the entire page (with ECC bytes) is actually blank or not.
  844. */
  845. if (!data)
  846. data_len = 0;
  847. if (!spare)
  848. spare_len = 0;
  849. if (!ecc)
  850. ecc_len = 0;
  851. bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
  852. spare, spare_len, chip->ecc.strength);
  853. if (bf < 0) {
  854. mtd->ecc_stats.failed++;
  855. return;
  856. }
  857. /* Update the stats and max_bitflips */
  858. mtd->ecc_stats.corrected += bf;
  859. *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
  860. }
  861. /*
  862. * Check if a chunk is correct or not according to the hardware ECC engine.
  863. * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
  864. * mtd->ecc_stats.failure is not, the function will instead return a non-zero
  865. * value indicating that a check on the emptyness of the subpage must be
  866. * performed before actually declaring the subpage as "corrupted".
  867. */
  868. static int marvell_nfc_hw_ecc_check_bitflips(struct nand_chip *chip,
  869. unsigned int *max_bitflips)
  870. {
  871. struct mtd_info *mtd = nand_to_mtd(chip);
  872. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  873. int bf = 0;
  874. u32 ndsr;
  875. ndsr = readl_relaxed(nfc->regs + NDSR);
  876. /* Check uncorrectable error flag */
  877. if (ndsr & NDSR_UNCERR) {
  878. writel_relaxed(ndsr, nfc->regs + NDSR);
  879. /*
  880. * Do not increment ->ecc_stats.failed now, instead, return a
  881. * non-zero value to indicate that this chunk was apparently
  882. * bad, and it should be check to see if it empty or not. If
  883. * the chunk (with ECC bytes) is not declared empty, the calling
  884. * function must increment the failure count.
  885. */
  886. return -EBADMSG;
  887. }
  888. /* Check correctable error flag */
  889. if (ndsr & NDSR_CORERR) {
  890. writel_relaxed(ndsr, nfc->regs + NDSR);
  891. if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
  892. bf = NDSR_ERRCNT(ndsr);
  893. else
  894. bf = 1;
  895. }
  896. /* Update the stats and max_bitflips */
  897. mtd->ecc_stats.corrected += bf;
  898. *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
  899. return 0;
  900. }
  901. /* Hamming read helpers */
  902. static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
  903. u8 *data_buf, u8 *oob_buf,
  904. bool raw, int page)
  905. {
  906. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  907. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  908. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  909. struct marvell_nfc_op nfc_op = {
  910. .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
  911. NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
  912. NDCB0_DBC |
  913. NDCB0_CMD1(NAND_CMD_READ0) |
  914. NDCB0_CMD2(NAND_CMD_READSTART),
  915. .ndcb[1] = NDCB1_ADDRS_PAGE(page),
  916. .ndcb[2] = NDCB2_ADDR5_PAGE(page),
  917. };
  918. unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
  919. int ret;
  920. /* NFCv2 needs more information about the operation being executed */
  921. if (nfc->caps->is_nfcv2)
  922. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
  923. ret = marvell_nfc_prepare_cmd(chip);
  924. if (ret)
  925. return ret;
  926. marvell_nfc_send_cmd(chip, &nfc_op);
  927. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  928. "RDDREQ while draining FIFO (data/oob)");
  929. if (ret)
  930. return ret;
  931. /*
  932. * Read the page then the OOB area. Unlike what is shown in current
  933. * documentation, spare bytes are protected by the ECC engine, and must
  934. * be at the beginning of the OOB area or running this driver on legacy
  935. * systems will prevent the discovery of the BBM/BBT.
  936. */
  937. if (nfc->use_dma) {
  938. marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
  939. lt->data_bytes + oob_bytes);
  940. memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
  941. memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
  942. } else {
  943. marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
  944. marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
  945. }
  946. ret = marvell_nfc_wait_cmdd(chip);
  947. return ret;
  948. }
  949. static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct nand_chip *chip, u8 *buf,
  950. int oob_required, int page)
  951. {
  952. marvell_nfc_select_target(chip, chip->cur_cs);
  953. return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
  954. true, page);
  955. }
  956. static int marvell_nfc_hw_ecc_hmg_read_page(struct nand_chip *chip, u8 *buf,
  957. int oob_required, int page)
  958. {
  959. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  960. unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
  961. int max_bitflips = 0, ret;
  962. u8 *raw_buf;
  963. marvell_nfc_select_target(chip, chip->cur_cs);
  964. marvell_nfc_enable_hw_ecc(chip);
  965. marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
  966. page);
  967. ret = marvell_nfc_hw_ecc_check_bitflips(chip, &max_bitflips);
  968. marvell_nfc_disable_hw_ecc(chip);
  969. if (!ret)
  970. return max_bitflips;
  971. /*
  972. * When ECC failures are detected, check if the full page has been
  973. * written or not. Ignore the failure if it is actually empty.
  974. */
  975. raw_buf = kmalloc(full_sz, GFP_KERNEL);
  976. if (!raw_buf)
  977. return -ENOMEM;
  978. marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
  979. lt->data_bytes, true, page);
  980. marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
  981. &max_bitflips);
  982. kfree(raw_buf);
  983. return max_bitflips;
  984. }
  985. /*
  986. * Spare area in Hamming layouts is not protected by the ECC engine (even if
  987. * it appears before the ECC bytes when reading), the ->read_oob_raw() function
  988. * also stands for ->read_oob().
  989. */
  990. static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct nand_chip *chip, int page)
  991. {
  992. u8 *buf = nand_get_data_buf(chip);
  993. marvell_nfc_select_target(chip, chip->cur_cs);
  994. return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
  995. true, page);
  996. }
  997. /* Hamming write helpers */
  998. static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
  999. const u8 *data_buf,
  1000. const u8 *oob_buf, bool raw,
  1001. int page)
  1002. {
  1003. const struct nand_sdr_timings *sdr =
  1004. nand_get_sdr_timings(nand_get_interface_config(chip));
  1005. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  1006. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1007. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1008. struct marvell_nfc_op nfc_op = {
  1009. .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
  1010. NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
  1011. NDCB0_CMD1(NAND_CMD_SEQIN) |
  1012. NDCB0_CMD2(NAND_CMD_PAGEPROG) |
  1013. NDCB0_DBC,
  1014. .ndcb[1] = NDCB1_ADDRS_PAGE(page),
  1015. .ndcb[2] = NDCB2_ADDR5_PAGE(page),
  1016. };
  1017. unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
  1018. u8 status;
  1019. int ret;
  1020. /* NFCv2 needs more information about the operation being executed */
  1021. if (nfc->caps->is_nfcv2)
  1022. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
  1023. ret = marvell_nfc_prepare_cmd(chip);
  1024. if (ret)
  1025. return ret;
  1026. marvell_nfc_send_cmd(chip, &nfc_op);
  1027. ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
  1028. "WRDREQ while loading FIFO (data)");
  1029. if (ret)
  1030. return ret;
  1031. /* Write the page then the OOB area */
  1032. if (nfc->use_dma) {
  1033. memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
  1034. memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
  1035. marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
  1036. lt->ecc_bytes + lt->spare_bytes);
  1037. } else {
  1038. marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
  1039. marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
  1040. }
  1041. ret = marvell_nfc_wait_cmdd(chip);
  1042. if (ret)
  1043. return ret;
  1044. ret = marvell_nfc_wait_op(chip,
  1045. PSEC_TO_MSEC(sdr->tPROG_max));
  1046. if (ret)
  1047. return ret;
  1048. /* Check write status on the chip side */
  1049. ret = nand_status_op(chip, &status);
  1050. if (ret)
  1051. return ret;
  1052. if (status & NAND_STATUS_FAIL)
  1053. return -EIO;
  1054. return 0;
  1055. }
  1056. static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct nand_chip *chip,
  1057. const u8 *buf,
  1058. int oob_required, int page)
  1059. {
  1060. marvell_nfc_select_target(chip, chip->cur_cs);
  1061. return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
  1062. true, page);
  1063. }
  1064. static int marvell_nfc_hw_ecc_hmg_write_page(struct nand_chip *chip,
  1065. const u8 *buf,
  1066. int oob_required, int page)
  1067. {
  1068. int ret;
  1069. marvell_nfc_select_target(chip, chip->cur_cs);
  1070. marvell_nfc_enable_hw_ecc(chip);
  1071. ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
  1072. false, page);
  1073. marvell_nfc_disable_hw_ecc(chip);
  1074. return ret;
  1075. }
  1076. /*
  1077. * Spare area in Hamming layouts is not protected by the ECC engine (even if
  1078. * it appears before the ECC bytes when reading), the ->write_oob_raw() function
  1079. * also stands for ->write_oob().
  1080. */
  1081. static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct nand_chip *chip,
  1082. int page)
  1083. {
  1084. struct mtd_info *mtd = nand_to_mtd(chip);
  1085. u8 *buf = nand_get_data_buf(chip);
  1086. memset(buf, 0xFF, mtd->writesize);
  1087. marvell_nfc_select_target(chip, chip->cur_cs);
  1088. return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
  1089. true, page);
  1090. }
  1091. /* BCH read helpers */
  1092. static int marvell_nfc_hw_ecc_bch_read_page_raw(struct nand_chip *chip, u8 *buf,
  1093. int oob_required, int page)
  1094. {
  1095. struct mtd_info *mtd = nand_to_mtd(chip);
  1096. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1097. u8 *oob = chip->oob_poi;
  1098. int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
  1099. int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
  1100. lt->last_spare_bytes;
  1101. int data_len = lt->data_bytes;
  1102. int spare_len = lt->spare_bytes;
  1103. int ecc_len = lt->ecc_bytes;
  1104. int chunk;
  1105. marvell_nfc_select_target(chip, chip->cur_cs);
  1106. if (oob_required)
  1107. memset(chip->oob_poi, 0xFF, mtd->oobsize);
  1108. nand_read_page_op(chip, page, 0, NULL, 0);
  1109. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1110. /* Update last chunk length */
  1111. if (chunk >= lt->full_chunk_cnt) {
  1112. data_len = lt->last_data_bytes;
  1113. spare_len = lt->last_spare_bytes;
  1114. ecc_len = lt->last_ecc_bytes;
  1115. }
  1116. /* Read data bytes*/
  1117. nand_change_read_column_op(chip, chunk * chunk_size,
  1118. buf + (lt->data_bytes * chunk),
  1119. data_len, false);
  1120. /* Read spare bytes */
  1121. nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
  1122. spare_len, false, false);
  1123. /* Read ECC bytes */
  1124. nand_read_data_op(chip, oob + ecc_offset +
  1125. (ALIGN(lt->ecc_bytes, 32) * chunk),
  1126. ecc_len, false, false);
  1127. }
  1128. return 0;
  1129. }
  1130. static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
  1131. u8 *data, unsigned int data_len,
  1132. u8 *spare, unsigned int spare_len,
  1133. int page)
  1134. {
  1135. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  1136. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1137. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1138. int i, ret;
  1139. struct marvell_nfc_op nfc_op = {
  1140. .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
  1141. NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
  1142. NDCB0_LEN_OVRD,
  1143. .ndcb[1] = NDCB1_ADDRS_PAGE(page),
  1144. .ndcb[2] = NDCB2_ADDR5_PAGE(page),
  1145. .ndcb[3] = data_len + spare_len,
  1146. };
  1147. ret = marvell_nfc_prepare_cmd(chip);
  1148. if (ret)
  1149. return;
  1150. if (chunk == 0)
  1151. nfc_op.ndcb[0] |= NDCB0_DBC |
  1152. NDCB0_CMD1(NAND_CMD_READ0) |
  1153. NDCB0_CMD2(NAND_CMD_READSTART);
  1154. /*
  1155. * Trigger the monolithic read on the first chunk, then naked read on
  1156. * intermediate chunks and finally a last naked read on the last chunk.
  1157. */
  1158. if (chunk == 0)
  1159. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
  1160. else if (chunk < lt->nchunks - 1)
  1161. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
  1162. else
  1163. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
  1164. marvell_nfc_send_cmd(chip, &nfc_op);
  1165. /*
  1166. * According to the datasheet, when reading from NDDB
  1167. * with BCH enabled, after each 32 bytes reads, we
  1168. * have to make sure that the NDSR.RDDREQ bit is set.
  1169. *
  1170. * Drain the FIFO, 8 32-bit reads at a time, and skip
  1171. * the polling on the last read.
  1172. *
  1173. * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
  1174. */
  1175. for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
  1176. marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  1177. "RDDREQ while draining FIFO (data)");
  1178. marvell_nfc_xfer_data_in_pio(nfc, data,
  1179. FIFO_DEPTH * BCH_SEQ_READS);
  1180. data += FIFO_DEPTH * BCH_SEQ_READS;
  1181. }
  1182. for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
  1183. marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  1184. "RDDREQ while draining FIFO (OOB)");
  1185. marvell_nfc_xfer_data_in_pio(nfc, spare,
  1186. FIFO_DEPTH * BCH_SEQ_READS);
  1187. spare += FIFO_DEPTH * BCH_SEQ_READS;
  1188. }
  1189. }
  1190. static int marvell_nfc_hw_ecc_bch_read_page(struct nand_chip *chip,
  1191. u8 *buf, int oob_required,
  1192. int page)
  1193. {
  1194. struct mtd_info *mtd = nand_to_mtd(chip);
  1195. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1196. int data_len = lt->data_bytes, spare_len = lt->spare_bytes;
  1197. u8 *data = buf, *spare = chip->oob_poi;
  1198. int max_bitflips = 0;
  1199. u32 failure_mask = 0;
  1200. int chunk, ret;
  1201. marvell_nfc_select_target(chip, chip->cur_cs);
  1202. /*
  1203. * With BCH, OOB is not fully used (and thus not read entirely), not
  1204. * expected bytes could show up at the end of the OOB buffer if not
  1205. * explicitly erased.
  1206. */
  1207. if (oob_required)
  1208. memset(chip->oob_poi, 0xFF, mtd->oobsize);
  1209. marvell_nfc_enable_hw_ecc(chip);
  1210. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1211. /* Update length for the last chunk */
  1212. if (chunk >= lt->full_chunk_cnt) {
  1213. data_len = lt->last_data_bytes;
  1214. spare_len = lt->last_spare_bytes;
  1215. }
  1216. /* Read the chunk and detect number of bitflips */
  1217. marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
  1218. spare, spare_len, page);
  1219. ret = marvell_nfc_hw_ecc_check_bitflips(chip, &max_bitflips);
  1220. if (ret)
  1221. failure_mask |= BIT(chunk);
  1222. data += data_len;
  1223. spare += spare_len;
  1224. }
  1225. marvell_nfc_disable_hw_ecc(chip);
  1226. if (!failure_mask)
  1227. return max_bitflips;
  1228. /*
  1229. * Please note that dumping the ECC bytes during a normal read with OOB
  1230. * area would add a significant overhead as ECC bytes are "consumed" by
  1231. * the controller in normal mode and must be re-read in raw mode. To
  1232. * avoid dropping the performances, we prefer not to include them. The
  1233. * user should re-read the page in raw mode if ECC bytes are required.
  1234. */
  1235. /*
  1236. * In case there is any subpage read error, we usually re-read only ECC
  1237. * bytes in raw mode and check if the whole page is empty. In this case,
  1238. * it is normal that the ECC check failed and we just ignore the error.
  1239. *
  1240. * However, it has been empirically observed that for some layouts (e.g
  1241. * 2k page, 8b strength per 512B chunk), the controller tries to correct
  1242. * bits and may create itself bitflips in the erased area. To overcome
  1243. * this strange behavior, the whole page is re-read in raw mode, not
  1244. * only the ECC bytes.
  1245. */
  1246. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1247. int data_off_in_page, spare_off_in_page, ecc_off_in_page;
  1248. int data_off, spare_off, ecc_off;
  1249. int data_len, spare_len, ecc_len;
  1250. /* No failure reported for this chunk, move to the next one */
  1251. if (!(failure_mask & BIT(chunk)))
  1252. continue;
  1253. data_off_in_page = chunk * (lt->data_bytes + lt->spare_bytes +
  1254. lt->ecc_bytes);
  1255. spare_off_in_page = data_off_in_page +
  1256. (chunk < lt->full_chunk_cnt ? lt->data_bytes :
  1257. lt->last_data_bytes);
  1258. ecc_off_in_page = spare_off_in_page +
  1259. (chunk < lt->full_chunk_cnt ? lt->spare_bytes :
  1260. lt->last_spare_bytes);
  1261. data_off = chunk * lt->data_bytes;
  1262. spare_off = chunk * lt->spare_bytes;
  1263. ecc_off = (lt->full_chunk_cnt * lt->spare_bytes) +
  1264. lt->last_spare_bytes +
  1265. (chunk * (lt->ecc_bytes + 2));
  1266. data_len = chunk < lt->full_chunk_cnt ? lt->data_bytes :
  1267. lt->last_data_bytes;
  1268. spare_len = chunk < lt->full_chunk_cnt ? lt->spare_bytes :
  1269. lt->last_spare_bytes;
  1270. ecc_len = chunk < lt->full_chunk_cnt ? lt->ecc_bytes :
  1271. lt->last_ecc_bytes;
  1272. /*
  1273. * Only re-read the ECC bytes, unless we are using the 2k/8b
  1274. * layout which is buggy in the sense that the ECC engine will
  1275. * try to correct data bytes anyway, creating bitflips. In this
  1276. * case, re-read the entire page.
  1277. */
  1278. if (lt->writesize == 2048 && lt->strength == 8) {
  1279. nand_change_read_column_op(chip, data_off_in_page,
  1280. buf + data_off, data_len,
  1281. false);
  1282. nand_change_read_column_op(chip, spare_off_in_page,
  1283. chip->oob_poi + spare_off, spare_len,
  1284. false);
  1285. }
  1286. nand_change_read_column_op(chip, ecc_off_in_page,
  1287. chip->oob_poi + ecc_off, ecc_len,
  1288. false);
  1289. /* Check the entire chunk (data + spare + ecc) for emptyness */
  1290. marvell_nfc_check_empty_chunk(chip, buf + data_off, data_len,
  1291. chip->oob_poi + spare_off, spare_len,
  1292. chip->oob_poi + ecc_off, ecc_len,
  1293. &max_bitflips);
  1294. }
  1295. return max_bitflips;
  1296. }
  1297. static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct nand_chip *chip, int page)
  1298. {
  1299. u8 *buf = nand_get_data_buf(chip);
  1300. return chip->ecc.read_page_raw(chip, buf, true, page);
  1301. }
  1302. static int marvell_nfc_hw_ecc_bch_read_oob(struct nand_chip *chip, int page)
  1303. {
  1304. u8 *buf = nand_get_data_buf(chip);
  1305. return chip->ecc.read_page(chip, buf, true, page);
  1306. }
  1307. /* BCH write helpers */
  1308. static int marvell_nfc_hw_ecc_bch_write_page_raw(struct nand_chip *chip,
  1309. const u8 *buf,
  1310. int oob_required, int page)
  1311. {
  1312. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1313. int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
  1314. int data_len = lt->data_bytes;
  1315. int spare_len = lt->spare_bytes;
  1316. int ecc_len = lt->ecc_bytes;
  1317. int spare_offset = 0;
  1318. int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
  1319. lt->last_spare_bytes;
  1320. int chunk;
  1321. marvell_nfc_select_target(chip, chip->cur_cs);
  1322. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1323. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1324. if (chunk >= lt->full_chunk_cnt) {
  1325. data_len = lt->last_data_bytes;
  1326. spare_len = lt->last_spare_bytes;
  1327. ecc_len = lt->last_ecc_bytes;
  1328. }
  1329. /* Point to the column of the next chunk */
  1330. nand_change_write_column_op(chip, chunk * full_chunk_size,
  1331. NULL, 0, false);
  1332. /* Write the data */
  1333. nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
  1334. data_len, false);
  1335. if (!oob_required)
  1336. continue;
  1337. /* Write the spare bytes */
  1338. if (spare_len)
  1339. nand_write_data_op(chip, chip->oob_poi + spare_offset,
  1340. spare_len, false);
  1341. /* Write the ECC bytes */
  1342. if (ecc_len)
  1343. nand_write_data_op(chip, chip->oob_poi + ecc_offset,
  1344. ecc_len, false);
  1345. spare_offset += spare_len;
  1346. ecc_offset += ALIGN(ecc_len, 32);
  1347. }
  1348. return nand_prog_page_end_op(chip);
  1349. }
  1350. static int
  1351. marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
  1352. const u8 *data, unsigned int data_len,
  1353. const u8 *spare, unsigned int spare_len,
  1354. int page)
  1355. {
  1356. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  1357. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1358. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1359. u32 xtype;
  1360. int ret;
  1361. struct marvell_nfc_op nfc_op = {
  1362. .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
  1363. .ndcb[3] = data_len + spare_len,
  1364. };
  1365. /*
  1366. * First operation dispatches the CMD_SEQIN command, issue the address
  1367. * cycles and asks for the first chunk of data.
  1368. * All operations in the middle (if any) will issue a naked write and
  1369. * also ask for data.
  1370. * Last operation (if any) asks for the last chunk of data through a
  1371. * last naked write.
  1372. */
  1373. if (chunk == 0) {
  1374. if (lt->nchunks == 1)
  1375. xtype = XTYPE_MONOLITHIC_RW;
  1376. else
  1377. xtype = XTYPE_WRITE_DISPATCH;
  1378. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) |
  1379. NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
  1380. NDCB0_CMD1(NAND_CMD_SEQIN);
  1381. nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
  1382. nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
  1383. } else if (chunk < lt->nchunks - 1) {
  1384. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
  1385. } else {
  1386. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
  1387. }
  1388. /* Always dispatch the PAGEPROG command on the last chunk */
  1389. if (chunk == lt->nchunks - 1)
  1390. nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
  1391. ret = marvell_nfc_prepare_cmd(chip);
  1392. if (ret)
  1393. return ret;
  1394. marvell_nfc_send_cmd(chip, &nfc_op);
  1395. ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
  1396. "WRDREQ while loading FIFO (data)");
  1397. if (ret)
  1398. return ret;
  1399. /* Transfer the contents */
  1400. iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
  1401. iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
  1402. return 0;
  1403. }
  1404. static int marvell_nfc_hw_ecc_bch_write_page(struct nand_chip *chip,
  1405. const u8 *buf,
  1406. int oob_required, int page)
  1407. {
  1408. const struct nand_sdr_timings *sdr =
  1409. nand_get_sdr_timings(nand_get_interface_config(chip));
  1410. struct mtd_info *mtd = nand_to_mtd(chip);
  1411. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1412. const u8 *data = buf;
  1413. const u8 *spare = chip->oob_poi;
  1414. int data_len = lt->data_bytes;
  1415. int spare_len = lt->spare_bytes;
  1416. int chunk, ret;
  1417. u8 status;
  1418. marvell_nfc_select_target(chip, chip->cur_cs);
  1419. /* Spare data will be written anyway, so clear it to avoid garbage */
  1420. if (!oob_required)
  1421. memset(chip->oob_poi, 0xFF, mtd->oobsize);
  1422. marvell_nfc_enable_hw_ecc(chip);
  1423. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1424. if (chunk >= lt->full_chunk_cnt) {
  1425. data_len = lt->last_data_bytes;
  1426. spare_len = lt->last_spare_bytes;
  1427. }
  1428. marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
  1429. spare, spare_len, page);
  1430. data += data_len;
  1431. spare += spare_len;
  1432. /*
  1433. * Waiting only for CMDD or PAGED is not enough, ECC are
  1434. * partially written. No flag is set once the operation is
  1435. * really finished but the ND_RUN bit is cleared, so wait for it
  1436. * before stepping into the next command.
  1437. */
  1438. marvell_nfc_wait_ndrun(chip);
  1439. }
  1440. ret = marvell_nfc_wait_op(chip, PSEC_TO_MSEC(sdr->tPROG_max));
  1441. marvell_nfc_disable_hw_ecc(chip);
  1442. if (ret)
  1443. return ret;
  1444. /* Check write status on the chip side */
  1445. ret = nand_status_op(chip, &status);
  1446. if (ret)
  1447. return ret;
  1448. if (status & NAND_STATUS_FAIL)
  1449. return -EIO;
  1450. return 0;
  1451. }
  1452. static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct nand_chip *chip,
  1453. int page)
  1454. {
  1455. struct mtd_info *mtd = nand_to_mtd(chip);
  1456. u8 *buf = nand_get_data_buf(chip);
  1457. memset(buf, 0xFF, mtd->writesize);
  1458. return chip->ecc.write_page_raw(chip, buf, true, page);
  1459. }
  1460. static int marvell_nfc_hw_ecc_bch_write_oob(struct nand_chip *chip, int page)
  1461. {
  1462. struct mtd_info *mtd = nand_to_mtd(chip);
  1463. u8 *buf = nand_get_data_buf(chip);
  1464. memset(buf, 0xFF, mtd->writesize);
  1465. return chip->ecc.write_page(chip, buf, true, page);
  1466. }
  1467. /* NAND framework ->exec_op() hooks and related helpers */
  1468. static void marvell_nfc_parse_instructions(struct nand_chip *chip,
  1469. const struct nand_subop *subop,
  1470. struct marvell_nfc_op *nfc_op)
  1471. {
  1472. const struct nand_op_instr *instr = NULL;
  1473. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1474. bool first_cmd = true;
  1475. unsigned int op_id;
  1476. int i;
  1477. /* Reset the input structure as most of its fields will be OR'ed */
  1478. memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
  1479. for (op_id = 0; op_id < subop->ninstrs; op_id++) {
  1480. unsigned int offset, naddrs;
  1481. const u8 *addrs;
  1482. int len;
  1483. instr = &subop->instrs[op_id];
  1484. switch (instr->type) {
  1485. case NAND_OP_CMD_INSTR:
  1486. if (first_cmd)
  1487. nfc_op->ndcb[0] |=
  1488. NDCB0_CMD1(instr->ctx.cmd.opcode);
  1489. else
  1490. nfc_op->ndcb[0] |=
  1491. NDCB0_CMD2(instr->ctx.cmd.opcode) |
  1492. NDCB0_DBC;
  1493. nfc_op->cle_ale_delay_ns = instr->delay_ns;
  1494. first_cmd = false;
  1495. break;
  1496. case NAND_OP_ADDR_INSTR:
  1497. offset = nand_subop_get_addr_start_off(subop, op_id);
  1498. naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
  1499. addrs = &instr->ctx.addr.addrs[offset];
  1500. nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
  1501. for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
  1502. nfc_op->ndcb[1] |= addrs[i] << (8 * i);
  1503. if (naddrs >= 5)
  1504. nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
  1505. if (naddrs >= 6)
  1506. nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
  1507. if (naddrs == 7)
  1508. nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
  1509. nfc_op->cle_ale_delay_ns = instr->delay_ns;
  1510. break;
  1511. case NAND_OP_DATA_IN_INSTR:
  1512. nfc_op->data_instr = instr;
  1513. nfc_op->data_instr_idx = op_id;
  1514. nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
  1515. if (nfc->caps->is_nfcv2) {
  1516. nfc_op->ndcb[0] |=
  1517. NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
  1518. NDCB0_LEN_OVRD;
  1519. len = nand_subop_get_data_len(subop, op_id);
  1520. nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
  1521. }
  1522. nfc_op->data_delay_ns = instr->delay_ns;
  1523. break;
  1524. case NAND_OP_DATA_OUT_INSTR:
  1525. nfc_op->data_instr = instr;
  1526. nfc_op->data_instr_idx = op_id;
  1527. nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
  1528. if (nfc->caps->is_nfcv2) {
  1529. nfc_op->ndcb[0] |=
  1530. NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
  1531. NDCB0_LEN_OVRD;
  1532. len = nand_subop_get_data_len(subop, op_id);
  1533. nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
  1534. }
  1535. nfc_op->data_delay_ns = instr->delay_ns;
  1536. break;
  1537. case NAND_OP_WAITRDY_INSTR:
  1538. nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
  1539. nfc_op->rdy_delay_ns = instr->delay_ns;
  1540. break;
  1541. }
  1542. }
  1543. }
  1544. static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
  1545. const struct nand_subop *subop,
  1546. struct marvell_nfc_op *nfc_op)
  1547. {
  1548. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1549. const struct nand_op_instr *instr = nfc_op->data_instr;
  1550. unsigned int op_id = nfc_op->data_instr_idx;
  1551. unsigned int len = nand_subop_get_data_len(subop, op_id);
  1552. unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
  1553. bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
  1554. int ret;
  1555. if (instr->ctx.data.force_8bit)
  1556. marvell_nfc_force_byte_access(chip, true);
  1557. if (reading) {
  1558. u8 *in = instr->ctx.data.buf.in + offset;
  1559. ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
  1560. } else {
  1561. const u8 *out = instr->ctx.data.buf.out + offset;
  1562. ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
  1563. }
  1564. if (instr->ctx.data.force_8bit)
  1565. marvell_nfc_force_byte_access(chip, false);
  1566. return ret;
  1567. }
  1568. static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
  1569. const struct nand_subop *subop)
  1570. {
  1571. struct marvell_nfc_op nfc_op;
  1572. bool reading;
  1573. int ret;
  1574. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1575. reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
  1576. ret = marvell_nfc_prepare_cmd(chip);
  1577. if (ret)
  1578. return ret;
  1579. marvell_nfc_send_cmd(chip, &nfc_op);
  1580. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
  1581. "RDDREQ/WRDREQ while draining raw data");
  1582. if (ret)
  1583. return ret;
  1584. cond_delay(nfc_op.cle_ale_delay_ns);
  1585. if (reading) {
  1586. if (nfc_op.rdy_timeout_ms) {
  1587. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1588. if (ret)
  1589. return ret;
  1590. }
  1591. cond_delay(nfc_op.rdy_delay_ns);
  1592. }
  1593. marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
  1594. ret = marvell_nfc_wait_cmdd(chip);
  1595. if (ret)
  1596. return ret;
  1597. cond_delay(nfc_op.data_delay_ns);
  1598. if (!reading) {
  1599. if (nfc_op.rdy_timeout_ms) {
  1600. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1601. if (ret)
  1602. return ret;
  1603. }
  1604. cond_delay(nfc_op.rdy_delay_ns);
  1605. }
  1606. /*
  1607. * NDCR ND_RUN bit should be cleared automatically at the end of each
  1608. * operation but experience shows that the behavior is buggy when it
  1609. * comes to writes (with LEN_OVRD). Clear it by hand in this case.
  1610. */
  1611. if (!reading) {
  1612. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1613. writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
  1614. nfc->regs + NDCR);
  1615. }
  1616. return 0;
  1617. }
  1618. static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
  1619. const struct nand_subop *subop)
  1620. {
  1621. struct marvell_nfc_op nfc_op;
  1622. int ret;
  1623. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1624. /*
  1625. * Naked access are different in that they need to be flagged as naked
  1626. * by the controller. Reset the controller registers fields that inform
  1627. * on the type and refill them according to the ongoing operation.
  1628. */
  1629. nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
  1630. NDCB0_CMD_XTYPE(XTYPE_MASK));
  1631. switch (subop->instrs[0].type) {
  1632. case NAND_OP_CMD_INSTR:
  1633. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
  1634. break;
  1635. case NAND_OP_ADDR_INSTR:
  1636. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
  1637. break;
  1638. case NAND_OP_DATA_IN_INSTR:
  1639. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
  1640. NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
  1641. break;
  1642. case NAND_OP_DATA_OUT_INSTR:
  1643. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
  1644. NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
  1645. break;
  1646. default:
  1647. /* This should never happen */
  1648. break;
  1649. }
  1650. ret = marvell_nfc_prepare_cmd(chip);
  1651. if (ret)
  1652. return ret;
  1653. marvell_nfc_send_cmd(chip, &nfc_op);
  1654. if (!nfc_op.data_instr) {
  1655. ret = marvell_nfc_wait_cmdd(chip);
  1656. cond_delay(nfc_op.cle_ale_delay_ns);
  1657. return ret;
  1658. }
  1659. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
  1660. "RDDREQ/WRDREQ while draining raw data");
  1661. if (ret)
  1662. return ret;
  1663. marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
  1664. ret = marvell_nfc_wait_cmdd(chip);
  1665. if (ret)
  1666. return ret;
  1667. /*
  1668. * NDCR ND_RUN bit should be cleared automatically at the end of each
  1669. * operation but experience shows that the behavior is buggy when it
  1670. * comes to writes (with LEN_OVRD). Clear it by hand in this case.
  1671. */
  1672. if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
  1673. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1674. writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
  1675. nfc->regs + NDCR);
  1676. }
  1677. return 0;
  1678. }
  1679. static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
  1680. const struct nand_subop *subop)
  1681. {
  1682. struct marvell_nfc_op nfc_op;
  1683. int ret;
  1684. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1685. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1686. cond_delay(nfc_op.rdy_delay_ns);
  1687. return ret;
  1688. }
  1689. static int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
  1690. const struct nand_subop *subop)
  1691. {
  1692. struct marvell_nfc_op nfc_op;
  1693. int ret;
  1694. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1695. nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
  1696. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
  1697. ret = marvell_nfc_prepare_cmd(chip);
  1698. if (ret)
  1699. return ret;
  1700. marvell_nfc_send_cmd(chip, &nfc_op);
  1701. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  1702. "RDDREQ while reading ID");
  1703. if (ret)
  1704. return ret;
  1705. cond_delay(nfc_op.cle_ale_delay_ns);
  1706. if (nfc_op.rdy_timeout_ms) {
  1707. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1708. if (ret)
  1709. return ret;
  1710. }
  1711. cond_delay(nfc_op.rdy_delay_ns);
  1712. marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
  1713. ret = marvell_nfc_wait_cmdd(chip);
  1714. if (ret)
  1715. return ret;
  1716. cond_delay(nfc_op.data_delay_ns);
  1717. return 0;
  1718. }
  1719. static int marvell_nfc_read_status_exec(struct nand_chip *chip,
  1720. const struct nand_subop *subop)
  1721. {
  1722. struct marvell_nfc_op nfc_op;
  1723. int ret;
  1724. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1725. nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
  1726. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
  1727. ret = marvell_nfc_prepare_cmd(chip);
  1728. if (ret)
  1729. return ret;
  1730. marvell_nfc_send_cmd(chip, &nfc_op);
  1731. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  1732. "RDDREQ while reading status");
  1733. if (ret)
  1734. return ret;
  1735. cond_delay(nfc_op.cle_ale_delay_ns);
  1736. if (nfc_op.rdy_timeout_ms) {
  1737. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1738. if (ret)
  1739. return ret;
  1740. }
  1741. cond_delay(nfc_op.rdy_delay_ns);
  1742. marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
  1743. ret = marvell_nfc_wait_cmdd(chip);
  1744. if (ret)
  1745. return ret;
  1746. cond_delay(nfc_op.data_delay_ns);
  1747. return 0;
  1748. }
  1749. static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
  1750. const struct nand_subop *subop)
  1751. {
  1752. struct marvell_nfc_op nfc_op;
  1753. int ret;
  1754. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1755. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
  1756. ret = marvell_nfc_prepare_cmd(chip);
  1757. if (ret)
  1758. return ret;
  1759. marvell_nfc_send_cmd(chip, &nfc_op);
  1760. ret = marvell_nfc_wait_cmdd(chip);
  1761. if (ret)
  1762. return ret;
  1763. cond_delay(nfc_op.cle_ale_delay_ns);
  1764. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1765. if (ret)
  1766. return ret;
  1767. cond_delay(nfc_op.rdy_delay_ns);
  1768. return 0;
  1769. }
  1770. static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
  1771. const struct nand_subop *subop)
  1772. {
  1773. struct marvell_nfc_op nfc_op;
  1774. int ret;
  1775. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1776. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
  1777. ret = marvell_nfc_prepare_cmd(chip);
  1778. if (ret)
  1779. return ret;
  1780. marvell_nfc_send_cmd(chip, &nfc_op);
  1781. ret = marvell_nfc_wait_cmdd(chip);
  1782. if (ret)
  1783. return ret;
  1784. cond_delay(nfc_op.cle_ale_delay_ns);
  1785. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1786. if (ret)
  1787. return ret;
  1788. cond_delay(nfc_op.rdy_delay_ns);
  1789. return 0;
  1790. }
  1791. static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
  1792. /* Monolithic reads/writes */
  1793. NAND_OP_PARSER_PATTERN(
  1794. marvell_nfc_monolithic_access_exec,
  1795. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1796. NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
  1797. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  1798. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
  1799. NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
  1800. NAND_OP_PARSER_PATTERN(
  1801. marvell_nfc_monolithic_access_exec,
  1802. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1803. NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
  1804. NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
  1805. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  1806. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
  1807. /* Naked commands */
  1808. NAND_OP_PARSER_PATTERN(
  1809. marvell_nfc_naked_access_exec,
  1810. NAND_OP_PARSER_PAT_CMD_ELEM(false)),
  1811. NAND_OP_PARSER_PATTERN(
  1812. marvell_nfc_naked_access_exec,
  1813. NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
  1814. NAND_OP_PARSER_PATTERN(
  1815. marvell_nfc_naked_access_exec,
  1816. NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
  1817. NAND_OP_PARSER_PATTERN(
  1818. marvell_nfc_naked_access_exec,
  1819. NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
  1820. NAND_OP_PARSER_PATTERN(
  1821. marvell_nfc_naked_waitrdy_exec,
  1822. NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
  1823. );
  1824. static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
  1825. /* Naked commands not supported, use a function for each pattern */
  1826. NAND_OP_PARSER_PATTERN(
  1827. marvell_nfc_read_id_type_exec,
  1828. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1829. NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
  1830. NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
  1831. NAND_OP_PARSER_PATTERN(
  1832. marvell_nfc_erase_cmd_type_exec,
  1833. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1834. NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
  1835. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1836. NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
  1837. NAND_OP_PARSER_PATTERN(
  1838. marvell_nfc_read_status_exec,
  1839. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1840. NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
  1841. NAND_OP_PARSER_PATTERN(
  1842. marvell_nfc_reset_cmd_type_exec,
  1843. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1844. NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
  1845. NAND_OP_PARSER_PATTERN(
  1846. marvell_nfc_naked_waitrdy_exec,
  1847. NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
  1848. );
  1849. static int marvell_nfc_exec_op(struct nand_chip *chip,
  1850. const struct nand_operation *op,
  1851. bool check_only)
  1852. {
  1853. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1854. if (!check_only)
  1855. marvell_nfc_select_target(chip, op->cs);
  1856. if (nfc->caps->is_nfcv2)
  1857. return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
  1858. op, check_only);
  1859. else
  1860. return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
  1861. op, check_only);
  1862. }
  1863. /*
  1864. * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
  1865. * usable.
  1866. */
  1867. static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
  1868. struct mtd_oob_region *oobregion)
  1869. {
  1870. struct nand_chip *chip = mtd_to_nand(mtd);
  1871. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1872. if (section)
  1873. return -ERANGE;
  1874. oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
  1875. lt->last_ecc_bytes;
  1876. oobregion->offset = mtd->oobsize - oobregion->length;
  1877. return 0;
  1878. }
  1879. static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
  1880. struct mtd_oob_region *oobregion)
  1881. {
  1882. struct nand_chip *chip = mtd_to_nand(mtd);
  1883. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1884. if (section)
  1885. return -ERANGE;
  1886. /*
  1887. * Bootrom looks in bytes 0 & 5 for bad blocks for the
  1888. * 4KB page / 4bit BCH combination.
  1889. */
  1890. if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
  1891. oobregion->offset = 6;
  1892. else
  1893. oobregion->offset = 2;
  1894. oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
  1895. lt->last_spare_bytes - oobregion->offset;
  1896. return 0;
  1897. }
  1898. static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
  1899. .ecc = marvell_nand_ooblayout_ecc,
  1900. .free = marvell_nand_ooblayout_free,
  1901. };
  1902. static int marvell_nand_hw_ecc_controller_init(struct mtd_info *mtd,
  1903. struct nand_ecc_ctrl *ecc)
  1904. {
  1905. struct nand_chip *chip = mtd_to_nand(mtd);
  1906. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1907. const struct marvell_hw_ecc_layout *l;
  1908. int i;
  1909. if (!nfc->caps->is_nfcv2 &&
  1910. (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
  1911. dev_err(nfc->dev,
  1912. "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
  1913. mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
  1914. return -ENOTSUPP;
  1915. }
  1916. to_marvell_nand(chip)->layout = NULL;
  1917. for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
  1918. l = &marvell_nfc_layouts[i];
  1919. if (mtd->writesize == l->writesize &&
  1920. ecc->size == l->chunk && ecc->strength == l->strength) {
  1921. to_marvell_nand(chip)->layout = l;
  1922. break;
  1923. }
  1924. }
  1925. if (!to_marvell_nand(chip)->layout ||
  1926. (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
  1927. dev_err(nfc->dev,
  1928. "ECC strength %d at page size %d is not supported\n",
  1929. ecc->strength, mtd->writesize);
  1930. return -ENOTSUPP;
  1931. }
  1932. /* Special care for the layout 2k/8-bit/512B */
  1933. if (l->writesize == 2048 && l->strength == 8) {
  1934. if (mtd->oobsize < 128) {
  1935. dev_err(nfc->dev, "Requested layout needs at least 128 OOB bytes\n");
  1936. return -ENOTSUPP;
  1937. } else {
  1938. chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
  1939. }
  1940. }
  1941. mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
  1942. ecc->steps = l->nchunks;
  1943. ecc->size = l->data_bytes;
  1944. if (ecc->strength == 1) {
  1945. chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  1946. ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
  1947. ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
  1948. ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
  1949. ecc->read_oob = ecc->read_oob_raw;
  1950. ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
  1951. ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
  1952. ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
  1953. ecc->write_oob = ecc->write_oob_raw;
  1954. } else {
  1955. chip->ecc.algo = NAND_ECC_ALGO_BCH;
  1956. ecc->strength = 16;
  1957. ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
  1958. ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
  1959. ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
  1960. ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
  1961. ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
  1962. ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
  1963. ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
  1964. ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
  1965. }
  1966. return 0;
  1967. }
  1968. static int marvell_nand_ecc_init(struct mtd_info *mtd,
  1969. struct nand_ecc_ctrl *ecc)
  1970. {
  1971. struct nand_chip *chip = mtd_to_nand(mtd);
  1972. const struct nand_ecc_props *requirements =
  1973. nanddev_get_ecc_requirements(&chip->base);
  1974. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1975. int ret;
  1976. if (ecc->engine_type != NAND_ECC_ENGINE_TYPE_NONE &&
  1977. (!ecc->size || !ecc->strength)) {
  1978. if (requirements->step_size && requirements->strength) {
  1979. ecc->size = requirements->step_size;
  1980. ecc->strength = requirements->strength;
  1981. } else {
  1982. dev_info(nfc->dev,
  1983. "No minimum ECC strength, using 1b/512B\n");
  1984. ecc->size = 512;
  1985. ecc->strength = 1;
  1986. }
  1987. }
  1988. switch (ecc->engine_type) {
  1989. case NAND_ECC_ENGINE_TYPE_ON_HOST:
  1990. ret = marvell_nand_hw_ecc_controller_init(mtd, ecc);
  1991. if (ret)
  1992. return ret;
  1993. break;
  1994. case NAND_ECC_ENGINE_TYPE_NONE:
  1995. case NAND_ECC_ENGINE_TYPE_SOFT:
  1996. case NAND_ECC_ENGINE_TYPE_ON_DIE:
  1997. if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
  1998. mtd->writesize != SZ_2K) {
  1999. dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
  2000. mtd->writesize);
  2001. return -EINVAL;
  2002. }
  2003. break;
  2004. default:
  2005. return -EINVAL;
  2006. }
  2007. return 0;
  2008. }
  2009. static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
  2010. static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
  2011. static struct nand_bbt_descr bbt_main_descr = {
  2012. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  2013. NAND_BBT_2BIT | NAND_BBT_VERSION,
  2014. .offs = 8,
  2015. .len = 6,
  2016. .veroffs = 14,
  2017. .maxblocks = 8, /* Last 8 blocks in each chip */
  2018. .pattern = bbt_pattern
  2019. };
  2020. static struct nand_bbt_descr bbt_mirror_descr = {
  2021. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  2022. NAND_BBT_2BIT | NAND_BBT_VERSION,
  2023. .offs = 8,
  2024. .len = 6,
  2025. .veroffs = 14,
  2026. .maxblocks = 8, /* Last 8 blocks in each chip */
  2027. .pattern = bbt_mirror_pattern
  2028. };
  2029. static int marvell_nfc_setup_interface(struct nand_chip *chip, int chipnr,
  2030. const struct nand_interface_config *conf)
  2031. {
  2032. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  2033. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  2034. unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
  2035. const struct nand_sdr_timings *sdr;
  2036. struct marvell_nfc_timings nfc_tmg;
  2037. int read_delay;
  2038. sdr = nand_get_sdr_timings(conf);
  2039. if (IS_ERR(sdr))
  2040. return PTR_ERR(sdr);
  2041. /*
  2042. * SDR timings are given in pico-seconds while NFC timings must be
  2043. * expressed in NAND controller clock cycles, which is half of the
  2044. * frequency of the accessible ECC clock retrieved by clk_get_rate().
  2045. * This is not written anywhere in the datasheet but was observed
  2046. * with an oscilloscope.
  2047. *
  2048. * NFC datasheet gives equations from which thoses calculations
  2049. * are derived, they tend to be slightly more restrictives than the
  2050. * given core timings and may improve the overall speed.
  2051. */
  2052. nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
  2053. nfc_tmg.tRH = nfc_tmg.tRP;
  2054. nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
  2055. nfc_tmg.tWH = nfc_tmg.tWP;
  2056. nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
  2057. nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
  2058. nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
  2059. /*
  2060. * Read delay is the time of propagation from SoC pins to NFC internal
  2061. * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
  2062. * EDO mode, an additional delay of tRH must be taken into account so
  2063. * the data is sampled on the falling edge instead of the rising edge.
  2064. */
  2065. read_delay = sdr->tRC_min >= 30000 ?
  2066. MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
  2067. nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
  2068. /*
  2069. * tWHR and tRHW are supposed to be read to write delays (and vice
  2070. * versa) but in some cases, ie. when doing a change column, they must
  2071. * be greater than that to be sure tCCS delay is respected.
  2072. */
  2073. nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
  2074. period_ns) - 2;
  2075. nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
  2076. period_ns);
  2077. /*
  2078. * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
  2079. * NFCv1: No WAIT_MODE, tR must be maximal.
  2080. */
  2081. if (nfc->caps->is_nfcv2) {
  2082. nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
  2083. } else {
  2084. nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
  2085. period_ns);
  2086. if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
  2087. nfc_tmg.tR = nfc_tmg.tCH - 3;
  2088. else
  2089. nfc_tmg.tR = 0;
  2090. }
  2091. if (chipnr < 0)
  2092. return 0;
  2093. marvell_nand->ndtr0 =
  2094. NDTR0_TRP(nfc_tmg.tRP) |
  2095. NDTR0_TRH(nfc_tmg.tRH) |
  2096. NDTR0_ETRP(nfc_tmg.tRP) |
  2097. NDTR0_TWP(nfc_tmg.tWP) |
  2098. NDTR0_TWH(nfc_tmg.tWH) |
  2099. NDTR0_TCS(nfc_tmg.tCS) |
  2100. NDTR0_TCH(nfc_tmg.tCH);
  2101. marvell_nand->ndtr1 =
  2102. NDTR1_TAR(nfc_tmg.tAR) |
  2103. NDTR1_TWHR(nfc_tmg.tWHR) |
  2104. NDTR1_TR(nfc_tmg.tR);
  2105. if (nfc->caps->is_nfcv2) {
  2106. marvell_nand->ndtr0 |=
  2107. NDTR0_RD_CNT_DEL(read_delay) |
  2108. NDTR0_SELCNTR |
  2109. NDTR0_TADL(nfc_tmg.tADL);
  2110. marvell_nand->ndtr1 |=
  2111. NDTR1_TRHW(nfc_tmg.tRHW) |
  2112. NDTR1_WAIT_MODE;
  2113. }
  2114. /*
  2115. * Reset nfc->selected_chip so the next command will cause the timing
  2116. * registers to be updated in marvell_nfc_select_target().
  2117. */
  2118. nfc->selected_chip = NULL;
  2119. return 0;
  2120. }
  2121. static int marvell_nand_attach_chip(struct nand_chip *chip)
  2122. {
  2123. struct mtd_info *mtd = nand_to_mtd(chip);
  2124. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  2125. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  2126. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev);
  2127. int ret;
  2128. if (pdata && pdata->flash_bbt)
  2129. chip->bbt_options |= NAND_BBT_USE_FLASH;
  2130. if (chip->bbt_options & NAND_BBT_USE_FLASH) {
  2131. /*
  2132. * We'll use a bad block table stored in-flash and don't
  2133. * allow writing the bad block marker to the flash.
  2134. */
  2135. chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
  2136. chip->bbt_td = &bbt_main_descr;
  2137. chip->bbt_md = &bbt_mirror_descr;
  2138. }
  2139. /* Save the chip-specific fields of NDCR */
  2140. marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
  2141. if (chip->options & NAND_BUSWIDTH_16)
  2142. marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
  2143. /*
  2144. * On small page NANDs, only one cycle is needed to pass the
  2145. * column address.
  2146. */
  2147. if (mtd->writesize <= 512) {
  2148. marvell_nand->addr_cyc = 1;
  2149. } else {
  2150. marvell_nand->addr_cyc = 2;
  2151. marvell_nand->ndcr |= NDCR_RA_START;
  2152. }
  2153. /*
  2154. * Now add the number of cycles needed to pass the row
  2155. * address.
  2156. *
  2157. * Addressing a chip using CS 2 or 3 should also need the third row
  2158. * cycle but due to inconsistance in the documentation and lack of
  2159. * hardware to test this situation, this case is not supported.
  2160. */
  2161. if (chip->options & NAND_ROW_ADDR_3)
  2162. marvell_nand->addr_cyc += 3;
  2163. else
  2164. marvell_nand->addr_cyc += 2;
  2165. if (pdata) {
  2166. chip->ecc.size = pdata->ecc_step_size;
  2167. chip->ecc.strength = pdata->ecc_strength;
  2168. }
  2169. ret = marvell_nand_ecc_init(mtd, &chip->ecc);
  2170. if (ret) {
  2171. dev_err(nfc->dev, "ECC init failed: %d\n", ret);
  2172. return ret;
  2173. }
  2174. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
  2175. /*
  2176. * Subpage write not available with hardware ECC, prohibit also
  2177. * subpage read as in userspace subpage access would still be
  2178. * allowed and subpage write, if used, would lead to numerous
  2179. * uncorrectable ECC errors.
  2180. */
  2181. chip->options |= NAND_NO_SUBPAGE_WRITE;
  2182. }
  2183. if (pdata || nfc->caps->legacy_of_bindings) {
  2184. /*
  2185. * We keep the MTD name unchanged to avoid breaking platforms
  2186. * where the MTD cmdline parser is used and the bootloader
  2187. * has not been updated to use the new naming scheme.
  2188. */
  2189. mtd->name = "pxa3xx_nand-0";
  2190. } else if (!mtd->name) {
  2191. /*
  2192. * If the new bindings are used and the bootloader has not been
  2193. * updated to pass a new mtdparts parameter on the cmdline, you
  2194. * should define the following property in your NAND node, ie:
  2195. *
  2196. * label = "main-storage";
  2197. *
  2198. * This way, mtd->name will be set by the core when
  2199. * nand_set_flash_node() is called.
  2200. */
  2201. mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
  2202. "%s:nand.%d", dev_name(nfc->dev),
  2203. marvell_nand->sels[0].cs);
  2204. if (!mtd->name) {
  2205. dev_err(nfc->dev, "Failed to allocate mtd->name\n");
  2206. return -ENOMEM;
  2207. }
  2208. }
  2209. return 0;
  2210. }
  2211. static const struct nand_controller_ops marvell_nand_controller_ops = {
  2212. .attach_chip = marvell_nand_attach_chip,
  2213. .exec_op = marvell_nfc_exec_op,
  2214. .setup_interface = marvell_nfc_setup_interface,
  2215. };
  2216. static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
  2217. struct device_node *np)
  2218. {
  2219. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
  2220. struct marvell_nand_chip *marvell_nand;
  2221. struct mtd_info *mtd;
  2222. struct nand_chip *chip;
  2223. int nsels, ret, i;
  2224. u32 cs, rb;
  2225. /*
  2226. * The legacy "num-cs" property indicates the number of CS on the only
  2227. * chip connected to the controller (legacy bindings does not support
  2228. * more than one chip). The CS and RB pins are always the #0.
  2229. *
  2230. * When not using legacy bindings, a couple of "reg" and "nand-rb"
  2231. * properties must be filled. For each chip, expressed as a subnode,
  2232. * "reg" points to the CS lines and "nand-rb" to the RB line.
  2233. */
  2234. if (pdata || nfc->caps->legacy_of_bindings) {
  2235. nsels = 1;
  2236. } else {
  2237. nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
  2238. if (nsels <= 0) {
  2239. dev_err(dev, "missing/invalid reg property\n");
  2240. return -EINVAL;
  2241. }
  2242. }
  2243. /* Alloc the nand chip structure */
  2244. marvell_nand = devm_kzalloc(dev,
  2245. struct_size(marvell_nand, sels, nsels),
  2246. GFP_KERNEL);
  2247. if (!marvell_nand) {
  2248. dev_err(dev, "could not allocate chip structure\n");
  2249. return -ENOMEM;
  2250. }
  2251. marvell_nand->nsels = nsels;
  2252. marvell_nand->selected_die = -1;
  2253. for (i = 0; i < nsels; i++) {
  2254. if (pdata || nfc->caps->legacy_of_bindings) {
  2255. /*
  2256. * Legacy bindings use the CS lines in natural
  2257. * order (0, 1, ...)
  2258. */
  2259. cs = i;
  2260. } else {
  2261. /* Retrieve CS id */
  2262. ret = of_property_read_u32_index(np, "reg", i, &cs);
  2263. if (ret) {
  2264. dev_err(dev, "could not retrieve reg property: %d\n",
  2265. ret);
  2266. return ret;
  2267. }
  2268. }
  2269. if (cs >= nfc->caps->max_cs_nb) {
  2270. dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
  2271. cs, nfc->caps->max_cs_nb);
  2272. return -EINVAL;
  2273. }
  2274. if (test_and_set_bit(cs, &nfc->assigned_cs)) {
  2275. dev_err(dev, "CS %d already assigned\n", cs);
  2276. return -EINVAL;
  2277. }
  2278. /*
  2279. * The cs variable represents the chip select id, which must be
  2280. * converted in bit fields for NDCB0 and NDCB2 to select the
  2281. * right chip. Unfortunately, due to a lack of information on
  2282. * the subject and incoherent documentation, the user should not
  2283. * use CS1 and CS3 at all as asserting them is not supported in
  2284. * a reliable way (due to multiplexing inside ADDR5 field).
  2285. */
  2286. marvell_nand->sels[i].cs = cs;
  2287. switch (cs) {
  2288. case 0:
  2289. case 2:
  2290. marvell_nand->sels[i].ndcb0_csel = 0;
  2291. break;
  2292. case 1:
  2293. case 3:
  2294. marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
  2295. break;
  2296. default:
  2297. return -EINVAL;
  2298. }
  2299. /* Retrieve RB id */
  2300. if (pdata || nfc->caps->legacy_of_bindings) {
  2301. /* Legacy bindings always use RB #0 */
  2302. rb = 0;
  2303. } else {
  2304. ret = of_property_read_u32_index(np, "nand-rb", i,
  2305. &rb);
  2306. if (ret) {
  2307. dev_err(dev,
  2308. "could not retrieve RB property: %d\n",
  2309. ret);
  2310. return ret;
  2311. }
  2312. }
  2313. if (rb >= nfc->caps->max_rb_nb) {
  2314. dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
  2315. rb, nfc->caps->max_rb_nb);
  2316. return -EINVAL;
  2317. }
  2318. marvell_nand->sels[i].rb = rb;
  2319. }
  2320. chip = &marvell_nand->chip;
  2321. chip->controller = &nfc->controller;
  2322. nand_set_flash_node(chip, np);
  2323. if (of_property_read_bool(np, "marvell,nand-keep-config"))
  2324. chip->options |= NAND_KEEP_TIMINGS;
  2325. mtd = nand_to_mtd(chip);
  2326. mtd->dev.parent = dev;
  2327. /*
  2328. * Save a reference value for timing registers before
  2329. * ->setup_interface() is called.
  2330. */
  2331. marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
  2332. marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
  2333. chip->options |= NAND_BUSWIDTH_AUTO;
  2334. ret = nand_scan(chip, marvell_nand->nsels);
  2335. if (ret) {
  2336. dev_err(dev, "could not scan the nand chip\n");
  2337. return ret;
  2338. }
  2339. if (pdata)
  2340. /* Legacy bindings support only one chip */
  2341. ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
  2342. else
  2343. ret = mtd_device_register(mtd, NULL, 0);
  2344. if (ret) {
  2345. dev_err(dev, "failed to register mtd device: %d\n", ret);
  2346. nand_cleanup(chip);
  2347. return ret;
  2348. }
  2349. list_add_tail(&marvell_nand->node, &nfc->chips);
  2350. return 0;
  2351. }
  2352. static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
  2353. {
  2354. struct marvell_nand_chip *entry, *temp;
  2355. struct nand_chip *chip;
  2356. int ret;
  2357. list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
  2358. chip = &entry->chip;
  2359. ret = mtd_device_unregister(nand_to_mtd(chip));
  2360. WARN_ON(ret);
  2361. nand_cleanup(chip);
  2362. list_del(&entry->node);
  2363. }
  2364. }
  2365. static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
  2366. {
  2367. struct device_node *np = dev->of_node;
  2368. struct device_node *nand_np;
  2369. int max_cs = nfc->caps->max_cs_nb;
  2370. int nchips;
  2371. int ret;
  2372. if (!np)
  2373. nchips = 1;
  2374. else
  2375. nchips = of_get_child_count(np);
  2376. if (nchips > max_cs) {
  2377. dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
  2378. max_cs);
  2379. return -EINVAL;
  2380. }
  2381. /*
  2382. * Legacy bindings do not use child nodes to exhibit NAND chip
  2383. * properties and layout. Instead, NAND properties are mixed with the
  2384. * controller ones, and partitions are defined as direct subnodes of the
  2385. * NAND controller node.
  2386. */
  2387. if (nfc->caps->legacy_of_bindings) {
  2388. ret = marvell_nand_chip_init(dev, nfc, np);
  2389. return ret;
  2390. }
  2391. for_each_child_of_node(np, nand_np) {
  2392. ret = marvell_nand_chip_init(dev, nfc, nand_np);
  2393. if (ret) {
  2394. of_node_put(nand_np);
  2395. goto cleanup_chips;
  2396. }
  2397. }
  2398. return 0;
  2399. cleanup_chips:
  2400. marvell_nand_chips_cleanup(nfc);
  2401. return ret;
  2402. }
  2403. static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
  2404. {
  2405. struct platform_device *pdev = container_of(nfc->dev,
  2406. struct platform_device,
  2407. dev);
  2408. struct dma_slave_config config = {};
  2409. struct resource *r;
  2410. int ret;
  2411. if (!IS_ENABLED(CONFIG_PXA_DMA)) {
  2412. dev_warn(nfc->dev,
  2413. "DMA not enabled in configuration\n");
  2414. return -ENOTSUPP;
  2415. }
  2416. ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
  2417. if (ret)
  2418. return ret;
  2419. nfc->dma_chan = dma_request_chan(nfc->dev, "data");
  2420. if (IS_ERR(nfc->dma_chan)) {
  2421. ret = PTR_ERR(nfc->dma_chan);
  2422. nfc->dma_chan = NULL;
  2423. return dev_err_probe(nfc->dev, ret, "DMA channel request failed\n");
  2424. }
  2425. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2426. if (!r) {
  2427. ret = -ENXIO;
  2428. goto release_channel;
  2429. }
  2430. config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  2431. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  2432. config.src_addr = r->start + NDDB;
  2433. config.dst_addr = r->start + NDDB;
  2434. config.src_maxburst = 32;
  2435. config.dst_maxburst = 32;
  2436. ret = dmaengine_slave_config(nfc->dma_chan, &config);
  2437. if (ret < 0) {
  2438. dev_err(nfc->dev, "Failed to configure DMA channel\n");
  2439. goto release_channel;
  2440. }
  2441. /*
  2442. * DMA must act on length multiple of 32 and this length may be
  2443. * bigger than the destination buffer. Use this buffer instead
  2444. * for DMA transfers and then copy the desired amount of data to
  2445. * the provided buffer.
  2446. */
  2447. nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
  2448. if (!nfc->dma_buf) {
  2449. ret = -ENOMEM;
  2450. goto release_channel;
  2451. }
  2452. nfc->use_dma = true;
  2453. return 0;
  2454. release_channel:
  2455. dma_release_channel(nfc->dma_chan);
  2456. nfc->dma_chan = NULL;
  2457. return ret;
  2458. }
  2459. static void marvell_nfc_reset(struct marvell_nfc *nfc)
  2460. {
  2461. /*
  2462. * ECC operations and interruptions are only enabled when specifically
  2463. * needed. ECC shall not be activated in the early stages (fails probe).
  2464. * Arbiter flag, even if marked as "reserved", must be set (empirical).
  2465. * SPARE_EN bit must always be set or ECC bytes will not be at the same
  2466. * offset in the read page and this will fail the protection.
  2467. */
  2468. writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
  2469. NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
  2470. writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
  2471. writel_relaxed(0, nfc->regs + NDECCCTRL);
  2472. }
  2473. static int marvell_nfc_init(struct marvell_nfc *nfc)
  2474. {
  2475. struct device_node *np = nfc->dev->of_node;
  2476. /*
  2477. * Some SoCs like A7k/A8k need to enable manually the NAND
  2478. * controller, gated clocks and reset bits to avoid being bootloader
  2479. * dependent. This is done through the use of the System Functions
  2480. * registers.
  2481. */
  2482. if (nfc->caps->need_system_controller) {
  2483. struct regmap *sysctrl_base =
  2484. syscon_regmap_lookup_by_phandle(np,
  2485. "marvell,system-controller");
  2486. if (IS_ERR(sysctrl_base))
  2487. return PTR_ERR(sysctrl_base);
  2488. regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX,
  2489. GENCONF_SOC_DEVICE_MUX_NFC_EN |
  2490. GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
  2491. GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
  2492. GENCONF_SOC_DEVICE_MUX_NFC_INT_EN);
  2493. regmap_update_bits(sysctrl_base, GENCONF_CLK_GATING_CTRL,
  2494. GENCONF_CLK_GATING_CTRL_ND_GATE,
  2495. GENCONF_CLK_GATING_CTRL_ND_GATE);
  2496. }
  2497. /* Configure the DMA if appropriate */
  2498. if (!nfc->caps->is_nfcv2)
  2499. marvell_nfc_init_dma(nfc);
  2500. marvell_nfc_reset(nfc);
  2501. return 0;
  2502. }
  2503. static int marvell_nfc_probe(struct platform_device *pdev)
  2504. {
  2505. struct device *dev = &pdev->dev;
  2506. struct marvell_nfc *nfc;
  2507. int ret;
  2508. int irq;
  2509. nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
  2510. GFP_KERNEL);
  2511. if (!nfc)
  2512. return -ENOMEM;
  2513. nfc->dev = dev;
  2514. nand_controller_init(&nfc->controller);
  2515. nfc->controller.ops = &marvell_nand_controller_ops;
  2516. INIT_LIST_HEAD(&nfc->chips);
  2517. nfc->regs = devm_platform_ioremap_resource(pdev, 0);
  2518. if (IS_ERR(nfc->regs))
  2519. return PTR_ERR(nfc->regs);
  2520. irq = platform_get_irq(pdev, 0);
  2521. if (irq < 0)
  2522. return irq;
  2523. nfc->core_clk = devm_clk_get(&pdev->dev, "core");
  2524. /* Managed the legacy case (when the first clock was not named) */
  2525. if (nfc->core_clk == ERR_PTR(-ENOENT))
  2526. nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
  2527. if (IS_ERR(nfc->core_clk))
  2528. return PTR_ERR(nfc->core_clk);
  2529. ret = clk_prepare_enable(nfc->core_clk);
  2530. if (ret)
  2531. return ret;
  2532. nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
  2533. if (IS_ERR(nfc->reg_clk)) {
  2534. if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
  2535. ret = PTR_ERR(nfc->reg_clk);
  2536. goto unprepare_core_clk;
  2537. }
  2538. nfc->reg_clk = NULL;
  2539. }
  2540. ret = clk_prepare_enable(nfc->reg_clk);
  2541. if (ret)
  2542. goto unprepare_core_clk;
  2543. marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
  2544. marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
  2545. ret = devm_request_irq(dev, irq, marvell_nfc_isr,
  2546. 0, "marvell-nfc", nfc);
  2547. if (ret)
  2548. goto unprepare_reg_clk;
  2549. /* Get NAND controller capabilities */
  2550. if (pdev->id_entry)
  2551. nfc->caps = (void *)pdev->id_entry->driver_data;
  2552. else
  2553. nfc->caps = of_device_get_match_data(&pdev->dev);
  2554. if (!nfc->caps) {
  2555. dev_err(dev, "Could not retrieve NFC caps\n");
  2556. ret = -EINVAL;
  2557. goto unprepare_reg_clk;
  2558. }
  2559. /* Init the controller and then probe the chips */
  2560. ret = marvell_nfc_init(nfc);
  2561. if (ret)
  2562. goto unprepare_reg_clk;
  2563. platform_set_drvdata(pdev, nfc);
  2564. ret = marvell_nand_chips_init(dev, nfc);
  2565. if (ret)
  2566. goto release_dma;
  2567. return 0;
  2568. release_dma:
  2569. if (nfc->use_dma)
  2570. dma_release_channel(nfc->dma_chan);
  2571. unprepare_reg_clk:
  2572. clk_disable_unprepare(nfc->reg_clk);
  2573. unprepare_core_clk:
  2574. clk_disable_unprepare(nfc->core_clk);
  2575. return ret;
  2576. }
  2577. static int marvell_nfc_remove(struct platform_device *pdev)
  2578. {
  2579. struct marvell_nfc *nfc = platform_get_drvdata(pdev);
  2580. marvell_nand_chips_cleanup(nfc);
  2581. if (nfc->use_dma) {
  2582. dmaengine_terminate_all(nfc->dma_chan);
  2583. dma_release_channel(nfc->dma_chan);
  2584. }
  2585. clk_disable_unprepare(nfc->reg_clk);
  2586. clk_disable_unprepare(nfc->core_clk);
  2587. return 0;
  2588. }
  2589. static int __maybe_unused marvell_nfc_suspend(struct device *dev)
  2590. {
  2591. struct marvell_nfc *nfc = dev_get_drvdata(dev);
  2592. struct marvell_nand_chip *chip;
  2593. list_for_each_entry(chip, &nfc->chips, node)
  2594. marvell_nfc_wait_ndrun(&chip->chip);
  2595. clk_disable_unprepare(nfc->reg_clk);
  2596. clk_disable_unprepare(nfc->core_clk);
  2597. return 0;
  2598. }
  2599. static int __maybe_unused marvell_nfc_resume(struct device *dev)
  2600. {
  2601. struct marvell_nfc *nfc = dev_get_drvdata(dev);
  2602. int ret;
  2603. ret = clk_prepare_enable(nfc->core_clk);
  2604. if (ret < 0)
  2605. return ret;
  2606. ret = clk_prepare_enable(nfc->reg_clk);
  2607. if (ret < 0) {
  2608. clk_disable_unprepare(nfc->core_clk);
  2609. return ret;
  2610. }
  2611. /*
  2612. * Reset nfc->selected_chip so the next command will cause the timing
  2613. * registers to be restored in marvell_nfc_select_target().
  2614. */
  2615. nfc->selected_chip = NULL;
  2616. /* Reset registers that have lost their contents */
  2617. marvell_nfc_reset(nfc);
  2618. return 0;
  2619. }
  2620. static const struct dev_pm_ops marvell_nfc_pm_ops = {
  2621. SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume)
  2622. };
  2623. static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
  2624. .max_cs_nb = 4,
  2625. .max_rb_nb = 2,
  2626. .need_system_controller = true,
  2627. .is_nfcv2 = true,
  2628. };
  2629. static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
  2630. .max_cs_nb = 4,
  2631. .max_rb_nb = 2,
  2632. .is_nfcv2 = true,
  2633. };
  2634. static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
  2635. .max_cs_nb = 2,
  2636. .max_rb_nb = 1,
  2637. .use_dma = true,
  2638. };
  2639. static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
  2640. .max_cs_nb = 4,
  2641. .max_rb_nb = 2,
  2642. .need_system_controller = true,
  2643. .legacy_of_bindings = true,
  2644. .is_nfcv2 = true,
  2645. };
  2646. static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
  2647. .max_cs_nb = 4,
  2648. .max_rb_nb = 2,
  2649. .legacy_of_bindings = true,
  2650. .is_nfcv2 = true,
  2651. };
  2652. static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
  2653. .max_cs_nb = 2,
  2654. .max_rb_nb = 1,
  2655. .legacy_of_bindings = true,
  2656. .use_dma = true,
  2657. };
  2658. static const struct platform_device_id marvell_nfc_platform_ids[] = {
  2659. {
  2660. .name = "pxa3xx-nand",
  2661. .driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
  2662. },
  2663. { /* sentinel */ },
  2664. };
  2665. MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
  2666. static const struct of_device_id marvell_nfc_of_ids[] = {
  2667. {
  2668. .compatible = "marvell,armada-8k-nand-controller",
  2669. .data = &marvell_armada_8k_nfc_caps,
  2670. },
  2671. {
  2672. .compatible = "marvell,armada370-nand-controller",
  2673. .data = &marvell_armada370_nfc_caps,
  2674. },
  2675. {
  2676. .compatible = "marvell,pxa3xx-nand-controller",
  2677. .data = &marvell_pxa3xx_nfc_caps,
  2678. },
  2679. /* Support for old/deprecated bindings: */
  2680. {
  2681. .compatible = "marvell,armada-8k-nand",
  2682. .data = &marvell_armada_8k_nfc_legacy_caps,
  2683. },
  2684. {
  2685. .compatible = "marvell,armada370-nand",
  2686. .data = &marvell_armada370_nfc_legacy_caps,
  2687. },
  2688. {
  2689. .compatible = "marvell,pxa3xx-nand",
  2690. .data = &marvell_pxa3xx_nfc_legacy_caps,
  2691. },
  2692. { /* sentinel */ },
  2693. };
  2694. MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
  2695. static struct platform_driver marvell_nfc_driver = {
  2696. .driver = {
  2697. .name = "marvell-nfc",
  2698. .of_match_table = marvell_nfc_of_ids,
  2699. .pm = &marvell_nfc_pm_ops,
  2700. },
  2701. .id_table = marvell_nfc_platform_ids,
  2702. .probe = marvell_nfc_probe,
  2703. .remove = marvell_nfc_remove,
  2704. };
  2705. module_platform_driver(marvell_nfc_driver);
  2706. MODULE_LICENSE("GPL");
  2707. MODULE_DESCRIPTION("Marvell NAND controller driver");