marvell_nand.c 83 KB

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