marvell_nand.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  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_hw_control 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_hw_control *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. /*
  584. * Do not change the timing registers when using the DT property
  585. * marvell,nand-keep-config; in that case ->ndtr0 and ->ndtr1 from the
  586. * marvell_nand structure are supposedly empty.
  587. */
  588. writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
  589. writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
  590. /*
  591. * Reset the NDCR register to a clean state for this particular chip,
  592. * also clear ND_RUN bit.
  593. */
  594. ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
  595. NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
  596. writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
  597. /* Also reset the interrupt status register */
  598. marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
  599. nfc->selected_chip = chip;
  600. marvell_nand->selected_die = die_nr;
  601. }
  602. static irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
  603. {
  604. struct marvell_nfc *nfc = dev_id;
  605. u32 st = readl_relaxed(nfc->regs + NDSR);
  606. u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
  607. /*
  608. * RDY interrupt mask is one bit in NDCR while there are two status
  609. * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
  610. */
  611. if (st & NDSR_RDY(1))
  612. st |= NDSR_RDY(0);
  613. if (!(st & ien))
  614. return IRQ_NONE;
  615. marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
  616. if (!(st & (NDSR_RDDREQ | NDSR_WRDREQ | NDSR_WRCMDREQ)))
  617. complete(&nfc->complete);
  618. return IRQ_HANDLED;
  619. }
  620. /* HW ECC related functions */
  621. static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
  622. {
  623. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  624. u32 ndcr = readl_relaxed(nfc->regs + NDCR);
  625. if (!(ndcr & NDCR_ECC_EN)) {
  626. writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
  627. /*
  628. * When enabling BCH, set threshold to 0 to always know the
  629. * number of corrected bitflips.
  630. */
  631. if (chip->ecc.algo == NAND_ECC_BCH)
  632. writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
  633. }
  634. }
  635. static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
  636. {
  637. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  638. u32 ndcr = readl_relaxed(nfc->regs + NDCR);
  639. if (ndcr & NDCR_ECC_EN) {
  640. writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
  641. if (chip->ecc.algo == NAND_ECC_BCH)
  642. writel_relaxed(0, nfc->regs + NDECCCTRL);
  643. }
  644. }
  645. /* DMA related helpers */
  646. static void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
  647. {
  648. u32 reg;
  649. reg = readl_relaxed(nfc->regs + NDCR);
  650. writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
  651. }
  652. static void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
  653. {
  654. u32 reg;
  655. reg = readl_relaxed(nfc->regs + NDCR);
  656. writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
  657. }
  658. /* Read/write PIO/DMA accessors */
  659. static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
  660. enum dma_data_direction direction,
  661. unsigned int len)
  662. {
  663. unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
  664. struct dma_async_tx_descriptor *tx;
  665. struct scatterlist sg;
  666. dma_cookie_t cookie;
  667. int ret;
  668. marvell_nfc_enable_dma(nfc);
  669. /* Prepare the DMA transfer */
  670. sg_init_one(&sg, nfc->dma_buf, dma_len);
  671. dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
  672. tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
  673. direction == DMA_FROM_DEVICE ?
  674. DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
  675. DMA_PREP_INTERRUPT);
  676. if (!tx) {
  677. dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
  678. return -ENXIO;
  679. }
  680. /* Do the task and wait for it to finish */
  681. cookie = dmaengine_submit(tx);
  682. ret = dma_submit_error(cookie);
  683. if (ret)
  684. return -EIO;
  685. dma_async_issue_pending(nfc->dma_chan);
  686. ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
  687. dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
  688. marvell_nfc_disable_dma(nfc);
  689. if (ret) {
  690. dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
  691. dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
  692. dmaengine_terminate_all(nfc->dma_chan);
  693. return -ETIMEDOUT;
  694. }
  695. return 0;
  696. }
  697. static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
  698. unsigned int len)
  699. {
  700. unsigned int last_len = len % FIFO_DEPTH;
  701. unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
  702. int i;
  703. for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
  704. ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
  705. if (last_len) {
  706. u8 tmp_buf[FIFO_DEPTH];
  707. ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
  708. memcpy(in + last_full_offset, tmp_buf, last_len);
  709. }
  710. return 0;
  711. }
  712. static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
  713. unsigned int len)
  714. {
  715. unsigned int last_len = len % FIFO_DEPTH;
  716. unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
  717. int i;
  718. for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
  719. iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
  720. if (last_len) {
  721. u8 tmp_buf[FIFO_DEPTH];
  722. memcpy(tmp_buf, out + last_full_offset, last_len);
  723. iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
  724. }
  725. return 0;
  726. }
  727. static void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
  728. u8 *data, int data_len,
  729. u8 *spare, int spare_len,
  730. u8 *ecc, int ecc_len,
  731. unsigned int *max_bitflips)
  732. {
  733. struct mtd_info *mtd = nand_to_mtd(chip);
  734. int bf;
  735. /*
  736. * Blank pages (all 0xFF) that have not been written may be recognized
  737. * as bad if bitflips occur, so whenever an uncorrectable error occurs,
  738. * check if the entire page (with ECC bytes) is actually blank or not.
  739. */
  740. if (!data)
  741. data_len = 0;
  742. if (!spare)
  743. spare_len = 0;
  744. if (!ecc)
  745. ecc_len = 0;
  746. bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
  747. spare, spare_len, chip->ecc.strength);
  748. if (bf < 0) {
  749. mtd->ecc_stats.failed++;
  750. return;
  751. }
  752. /* Update the stats and max_bitflips */
  753. mtd->ecc_stats.corrected += bf;
  754. *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
  755. }
  756. /*
  757. * Check a chunk is correct or not according to hardware ECC engine.
  758. * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
  759. * mtd->ecc_stats.failure is not, the function will instead return a non-zero
  760. * value indicating that a check on the emptyness of the subpage must be
  761. * performed before declaring the subpage corrupted.
  762. */
  763. static int marvell_nfc_hw_ecc_correct(struct nand_chip *chip,
  764. unsigned int *max_bitflips)
  765. {
  766. struct mtd_info *mtd = nand_to_mtd(chip);
  767. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  768. int bf = 0;
  769. u32 ndsr;
  770. ndsr = readl_relaxed(nfc->regs + NDSR);
  771. /* Check uncorrectable error flag */
  772. if (ndsr & NDSR_UNCERR) {
  773. writel_relaxed(ndsr, nfc->regs + NDSR);
  774. /*
  775. * Do not increment ->ecc_stats.failed now, instead, return a
  776. * non-zero value to indicate that this chunk was apparently
  777. * bad, and it should be check to see if it empty or not. If
  778. * the chunk (with ECC bytes) is not declared empty, the calling
  779. * function must increment the failure count.
  780. */
  781. return -EBADMSG;
  782. }
  783. /* Check correctable error flag */
  784. if (ndsr & NDSR_CORERR) {
  785. writel_relaxed(ndsr, nfc->regs + NDSR);
  786. if (chip->ecc.algo == NAND_ECC_BCH)
  787. bf = NDSR_ERRCNT(ndsr);
  788. else
  789. bf = 1;
  790. }
  791. /* Update the stats and max_bitflips */
  792. mtd->ecc_stats.corrected += bf;
  793. *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
  794. return 0;
  795. }
  796. /* Hamming read helpers */
  797. static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
  798. u8 *data_buf, u8 *oob_buf,
  799. bool raw, int page)
  800. {
  801. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  802. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  803. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  804. struct marvell_nfc_op nfc_op = {
  805. .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
  806. NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
  807. NDCB0_DBC |
  808. NDCB0_CMD1(NAND_CMD_READ0) |
  809. NDCB0_CMD2(NAND_CMD_READSTART),
  810. .ndcb[1] = NDCB1_ADDRS_PAGE(page),
  811. .ndcb[2] = NDCB2_ADDR5_PAGE(page),
  812. };
  813. unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
  814. int ret;
  815. /* NFCv2 needs more information about the operation being executed */
  816. if (nfc->caps->is_nfcv2)
  817. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
  818. ret = marvell_nfc_prepare_cmd(chip);
  819. if (ret)
  820. return ret;
  821. marvell_nfc_send_cmd(chip, &nfc_op);
  822. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  823. "RDDREQ while draining FIFO (data/oob)");
  824. if (ret)
  825. return ret;
  826. /*
  827. * Read the page then the OOB area. Unlike what is shown in current
  828. * documentation, spare bytes are protected by the ECC engine, and must
  829. * be at the beginning of the OOB area or running this driver on legacy
  830. * systems will prevent the discovery of the BBM/BBT.
  831. */
  832. if (nfc->use_dma) {
  833. marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
  834. lt->data_bytes + oob_bytes);
  835. memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
  836. memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
  837. } else {
  838. marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
  839. marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
  840. }
  841. ret = marvell_nfc_wait_cmdd(chip);
  842. return ret;
  843. }
  844. static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct mtd_info *mtd,
  845. struct nand_chip *chip, u8 *buf,
  846. int oob_required, int page)
  847. {
  848. return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
  849. true, page);
  850. }
  851. static int marvell_nfc_hw_ecc_hmg_read_page(struct mtd_info *mtd,
  852. struct nand_chip *chip,
  853. u8 *buf, int oob_required,
  854. int page)
  855. {
  856. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  857. unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
  858. int max_bitflips = 0, ret;
  859. u8 *raw_buf;
  860. marvell_nfc_enable_hw_ecc(chip);
  861. marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
  862. page);
  863. ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
  864. marvell_nfc_disable_hw_ecc(chip);
  865. if (!ret)
  866. return max_bitflips;
  867. /*
  868. * When ECC failures are detected, check if the full page has been
  869. * written or not. Ignore the failure if it is actually empty.
  870. */
  871. raw_buf = kmalloc(full_sz, GFP_KERNEL);
  872. if (!raw_buf)
  873. return -ENOMEM;
  874. marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
  875. lt->data_bytes, true, page);
  876. marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
  877. &max_bitflips);
  878. kfree(raw_buf);
  879. return max_bitflips;
  880. }
  881. /*
  882. * Spare area in Hamming layouts is not protected by the ECC engine (even if
  883. * it appears before the ECC bytes when reading), the ->read_oob_raw() function
  884. * also stands for ->read_oob().
  885. */
  886. static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct mtd_info *mtd,
  887. struct nand_chip *chip, int page)
  888. {
  889. /* Invalidate page cache */
  890. chip->pagebuf = -1;
  891. return marvell_nfc_hw_ecc_hmg_do_read_page(chip, chip->data_buf,
  892. chip->oob_poi, true, page);
  893. }
  894. /* Hamming write helpers */
  895. static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
  896. const u8 *data_buf,
  897. const u8 *oob_buf, bool raw,
  898. int page)
  899. {
  900. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  901. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  902. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  903. struct marvell_nfc_op nfc_op = {
  904. .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
  905. NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
  906. NDCB0_CMD1(NAND_CMD_SEQIN) |
  907. NDCB0_CMD2(NAND_CMD_PAGEPROG) |
  908. NDCB0_DBC,
  909. .ndcb[1] = NDCB1_ADDRS_PAGE(page),
  910. .ndcb[2] = NDCB2_ADDR5_PAGE(page),
  911. };
  912. unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
  913. int ret;
  914. /* NFCv2 needs more information about the operation being executed */
  915. if (nfc->caps->is_nfcv2)
  916. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
  917. ret = marvell_nfc_prepare_cmd(chip);
  918. if (ret)
  919. return ret;
  920. marvell_nfc_send_cmd(chip, &nfc_op);
  921. ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
  922. "WRDREQ while loading FIFO (data)");
  923. if (ret)
  924. return ret;
  925. /* Write the page then the OOB area */
  926. if (nfc->use_dma) {
  927. memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
  928. memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
  929. marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
  930. lt->ecc_bytes + lt->spare_bytes);
  931. } else {
  932. marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
  933. marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
  934. }
  935. ret = marvell_nfc_wait_cmdd(chip);
  936. if (ret)
  937. return ret;
  938. ret = marvell_nfc_wait_op(chip,
  939. chip->data_interface.timings.sdr.tPROG_max);
  940. return ret;
  941. }
  942. static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct mtd_info *mtd,
  943. struct nand_chip *chip,
  944. const u8 *buf,
  945. int oob_required, int page)
  946. {
  947. return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
  948. true, page);
  949. }
  950. static int marvell_nfc_hw_ecc_hmg_write_page(struct mtd_info *mtd,
  951. struct nand_chip *chip,
  952. const u8 *buf,
  953. int oob_required, int page)
  954. {
  955. int ret;
  956. marvell_nfc_enable_hw_ecc(chip);
  957. ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
  958. false, page);
  959. marvell_nfc_disable_hw_ecc(chip);
  960. return ret;
  961. }
  962. /*
  963. * Spare area in Hamming layouts is not protected by the ECC engine (even if
  964. * it appears before the ECC bytes when reading), the ->write_oob_raw() function
  965. * also stands for ->write_oob().
  966. */
  967. static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct mtd_info *mtd,
  968. struct nand_chip *chip,
  969. int page)
  970. {
  971. /* Invalidate page cache */
  972. chip->pagebuf = -1;
  973. memset(chip->data_buf, 0xFF, mtd->writesize);
  974. return marvell_nfc_hw_ecc_hmg_do_write_page(chip, chip->data_buf,
  975. chip->oob_poi, true, page);
  976. }
  977. /* BCH read helpers */
  978. static int marvell_nfc_hw_ecc_bch_read_page_raw(struct mtd_info *mtd,
  979. struct nand_chip *chip, u8 *buf,
  980. int oob_required, int page)
  981. {
  982. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  983. u8 *oob = chip->oob_poi;
  984. int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
  985. int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
  986. lt->last_spare_bytes;
  987. int data_len = lt->data_bytes;
  988. int spare_len = lt->spare_bytes;
  989. int ecc_len = lt->ecc_bytes;
  990. int chunk;
  991. if (oob_required)
  992. memset(chip->oob_poi, 0xFF, mtd->oobsize);
  993. nand_read_page_op(chip, page, 0, NULL, 0);
  994. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  995. /* Update last chunk length */
  996. if (chunk >= lt->full_chunk_cnt) {
  997. data_len = lt->last_data_bytes;
  998. spare_len = lt->last_spare_bytes;
  999. ecc_len = lt->last_ecc_bytes;
  1000. }
  1001. /* Read data bytes*/
  1002. nand_change_read_column_op(chip, chunk * chunk_size,
  1003. buf + (lt->data_bytes * chunk),
  1004. data_len, false);
  1005. /* Read spare bytes */
  1006. nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
  1007. spare_len, false);
  1008. /* Read ECC bytes */
  1009. nand_read_data_op(chip, oob + ecc_offset +
  1010. (ALIGN(lt->ecc_bytes, 32) * chunk),
  1011. ecc_len, false);
  1012. }
  1013. return 0;
  1014. }
  1015. static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
  1016. u8 *data, unsigned int data_len,
  1017. u8 *spare, unsigned int spare_len,
  1018. int page)
  1019. {
  1020. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  1021. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1022. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1023. int i, ret;
  1024. struct marvell_nfc_op nfc_op = {
  1025. .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
  1026. NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
  1027. NDCB0_LEN_OVRD,
  1028. .ndcb[1] = NDCB1_ADDRS_PAGE(page),
  1029. .ndcb[2] = NDCB2_ADDR5_PAGE(page),
  1030. .ndcb[3] = data_len + spare_len,
  1031. };
  1032. ret = marvell_nfc_prepare_cmd(chip);
  1033. if (ret)
  1034. return;
  1035. if (chunk == 0)
  1036. nfc_op.ndcb[0] |= NDCB0_DBC |
  1037. NDCB0_CMD1(NAND_CMD_READ0) |
  1038. NDCB0_CMD2(NAND_CMD_READSTART);
  1039. /*
  1040. * Trigger the naked read operation only on the last chunk.
  1041. * Otherwise, use monolithic read.
  1042. */
  1043. if (lt->nchunks == 1 || (chunk < lt->nchunks - 1))
  1044. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
  1045. else
  1046. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
  1047. marvell_nfc_send_cmd(chip, &nfc_op);
  1048. /*
  1049. * According to the datasheet, when reading from NDDB
  1050. * with BCH enabled, after each 32 bytes reads, we
  1051. * have to make sure that the NDSR.RDDREQ bit is set.
  1052. *
  1053. * Drain the FIFO, 8 32-bit reads at a time, and skip
  1054. * the polling on the last read.
  1055. *
  1056. * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
  1057. */
  1058. for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
  1059. marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  1060. "RDDREQ while draining FIFO (data)");
  1061. marvell_nfc_xfer_data_in_pio(nfc, data,
  1062. FIFO_DEPTH * BCH_SEQ_READS);
  1063. data += FIFO_DEPTH * BCH_SEQ_READS;
  1064. }
  1065. for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
  1066. marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  1067. "RDDREQ while draining FIFO (OOB)");
  1068. marvell_nfc_xfer_data_in_pio(nfc, spare,
  1069. FIFO_DEPTH * BCH_SEQ_READS);
  1070. spare += FIFO_DEPTH * BCH_SEQ_READS;
  1071. }
  1072. }
  1073. static int marvell_nfc_hw_ecc_bch_read_page(struct mtd_info *mtd,
  1074. struct nand_chip *chip,
  1075. u8 *buf, int oob_required,
  1076. int page)
  1077. {
  1078. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1079. int data_len = lt->data_bytes, spare_len = lt->spare_bytes, ecc_len;
  1080. u8 *data = buf, *spare = chip->oob_poi, *ecc;
  1081. int max_bitflips = 0;
  1082. u32 failure_mask = 0;
  1083. int chunk, ecc_offset_in_page, ret;
  1084. /*
  1085. * With BCH, OOB is not fully used (and thus not read entirely), not
  1086. * expected bytes could show up at the end of the OOB buffer if not
  1087. * explicitly erased.
  1088. */
  1089. if (oob_required)
  1090. memset(chip->oob_poi, 0xFF, mtd->oobsize);
  1091. marvell_nfc_enable_hw_ecc(chip);
  1092. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1093. /* Update length for the last chunk */
  1094. if (chunk >= lt->full_chunk_cnt) {
  1095. data_len = lt->last_data_bytes;
  1096. spare_len = lt->last_spare_bytes;
  1097. }
  1098. /* Read the chunk and detect number of bitflips */
  1099. marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
  1100. spare, spare_len, page);
  1101. ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
  1102. if (ret)
  1103. failure_mask |= BIT(chunk);
  1104. data += data_len;
  1105. spare += spare_len;
  1106. }
  1107. marvell_nfc_disable_hw_ecc(chip);
  1108. if (!failure_mask)
  1109. return max_bitflips;
  1110. /*
  1111. * Please note that dumping the ECC bytes during a normal read with OOB
  1112. * area would add a significant overhead as ECC bytes are "consumed" by
  1113. * the controller in normal mode and must be re-read in raw mode. To
  1114. * avoid dropping the performances, we prefer not to include them. The
  1115. * user should re-read the page in raw mode if ECC bytes are required.
  1116. *
  1117. * However, for any subpage read error reported by ->correct(), the ECC
  1118. * bytes must be read in raw mode and the full subpage must be checked
  1119. * to see if it is entirely empty of if there was an actual error.
  1120. */
  1121. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1122. /* No failure reported for this chunk, move to the next one */
  1123. if (!(failure_mask & BIT(chunk)))
  1124. continue;
  1125. /* Derive ECC bytes positions (in page/buffer) and length */
  1126. ecc = chip->oob_poi +
  1127. (lt->full_chunk_cnt * lt->spare_bytes) +
  1128. lt->last_spare_bytes +
  1129. (chunk * ALIGN(lt->ecc_bytes, 32));
  1130. ecc_offset_in_page =
  1131. (chunk * (lt->data_bytes + lt->spare_bytes +
  1132. lt->ecc_bytes)) +
  1133. (chunk < lt->full_chunk_cnt ?
  1134. lt->data_bytes + lt->spare_bytes :
  1135. lt->last_data_bytes + lt->last_spare_bytes);
  1136. ecc_len = chunk < lt->full_chunk_cnt ?
  1137. lt->ecc_bytes : lt->last_ecc_bytes;
  1138. /* Do the actual raw read of the ECC bytes */
  1139. nand_change_read_column_op(chip, ecc_offset_in_page,
  1140. ecc, ecc_len, false);
  1141. /* Derive data/spare bytes positions (in buffer) and length */
  1142. data = buf + (chunk * lt->data_bytes);
  1143. data_len = chunk < lt->full_chunk_cnt ?
  1144. lt->data_bytes : lt->last_data_bytes;
  1145. spare = chip->oob_poi + (chunk * (lt->spare_bytes +
  1146. lt->ecc_bytes));
  1147. spare_len = chunk < lt->full_chunk_cnt ?
  1148. lt->spare_bytes : lt->last_spare_bytes;
  1149. /* Check the entire chunk (data + spare + ecc) for emptyness */
  1150. marvell_nfc_check_empty_chunk(chip, data, data_len, spare,
  1151. spare_len, ecc, ecc_len,
  1152. &max_bitflips);
  1153. }
  1154. return max_bitflips;
  1155. }
  1156. static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct mtd_info *mtd,
  1157. struct nand_chip *chip, int page)
  1158. {
  1159. /* Invalidate page cache */
  1160. chip->pagebuf = -1;
  1161. return chip->ecc.read_page_raw(mtd, chip, chip->data_buf, true, page);
  1162. }
  1163. static int marvell_nfc_hw_ecc_bch_read_oob(struct mtd_info *mtd,
  1164. struct nand_chip *chip, int page)
  1165. {
  1166. /* Invalidate page cache */
  1167. chip->pagebuf = -1;
  1168. return chip->ecc.read_page(mtd, chip, chip->data_buf, true, page);
  1169. }
  1170. /* BCH write helpers */
  1171. static int marvell_nfc_hw_ecc_bch_write_page_raw(struct mtd_info *mtd,
  1172. struct nand_chip *chip,
  1173. const u8 *buf,
  1174. int oob_required, int page)
  1175. {
  1176. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1177. int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
  1178. int data_len = lt->data_bytes;
  1179. int spare_len = lt->spare_bytes;
  1180. int ecc_len = lt->ecc_bytes;
  1181. int spare_offset = 0;
  1182. int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
  1183. lt->last_spare_bytes;
  1184. int chunk;
  1185. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1186. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1187. if (chunk >= lt->full_chunk_cnt) {
  1188. data_len = lt->last_data_bytes;
  1189. spare_len = lt->last_spare_bytes;
  1190. ecc_len = lt->last_ecc_bytes;
  1191. }
  1192. /* Point to the column of the next chunk */
  1193. nand_change_write_column_op(chip, chunk * full_chunk_size,
  1194. NULL, 0, false);
  1195. /* Write the data */
  1196. nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
  1197. data_len, false);
  1198. if (!oob_required)
  1199. continue;
  1200. /* Write the spare bytes */
  1201. if (spare_len)
  1202. nand_write_data_op(chip, chip->oob_poi + spare_offset,
  1203. spare_len, false);
  1204. /* Write the ECC bytes */
  1205. if (ecc_len)
  1206. nand_write_data_op(chip, chip->oob_poi + ecc_offset,
  1207. ecc_len, false);
  1208. spare_offset += spare_len;
  1209. ecc_offset += ALIGN(ecc_len, 32);
  1210. }
  1211. return nand_prog_page_end_op(chip);
  1212. }
  1213. static int
  1214. marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
  1215. const u8 *data, unsigned int data_len,
  1216. const u8 *spare, unsigned int spare_len,
  1217. int page)
  1218. {
  1219. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  1220. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1221. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1222. int ret;
  1223. struct marvell_nfc_op nfc_op = {
  1224. .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
  1225. .ndcb[3] = data_len + spare_len,
  1226. };
  1227. /*
  1228. * First operation dispatches the CMD_SEQIN command, issue the address
  1229. * cycles and asks for the first chunk of data.
  1230. * All operations in the middle (if any) will issue a naked write and
  1231. * also ask for data.
  1232. * Last operation (if any) asks for the last chunk of data through a
  1233. * last naked write.
  1234. */
  1235. if (chunk == 0) {
  1236. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_WRITE_DISPATCH) |
  1237. NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
  1238. NDCB0_CMD1(NAND_CMD_SEQIN);
  1239. nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
  1240. nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
  1241. } else if (chunk < lt->nchunks - 1) {
  1242. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
  1243. } else {
  1244. nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
  1245. }
  1246. /* Always dispatch the PAGEPROG command on the last chunk */
  1247. if (chunk == lt->nchunks - 1)
  1248. nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
  1249. ret = marvell_nfc_prepare_cmd(chip);
  1250. if (ret)
  1251. return ret;
  1252. marvell_nfc_send_cmd(chip, &nfc_op);
  1253. ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
  1254. "WRDREQ while loading FIFO (data)");
  1255. if (ret)
  1256. return ret;
  1257. /* Transfer the contents */
  1258. iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
  1259. iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
  1260. return 0;
  1261. }
  1262. static int marvell_nfc_hw_ecc_bch_write_page(struct mtd_info *mtd,
  1263. struct nand_chip *chip,
  1264. const u8 *buf,
  1265. int oob_required, int page)
  1266. {
  1267. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1268. const u8 *data = buf;
  1269. const u8 *spare = chip->oob_poi;
  1270. int data_len = lt->data_bytes;
  1271. int spare_len = lt->spare_bytes;
  1272. int chunk, ret;
  1273. /* Spare data will be written anyway, so clear it to avoid garbage */
  1274. if (!oob_required)
  1275. memset(chip->oob_poi, 0xFF, mtd->oobsize);
  1276. marvell_nfc_enable_hw_ecc(chip);
  1277. for (chunk = 0; chunk < lt->nchunks; chunk++) {
  1278. if (chunk >= lt->full_chunk_cnt) {
  1279. data_len = lt->last_data_bytes;
  1280. spare_len = lt->last_spare_bytes;
  1281. }
  1282. marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
  1283. spare, spare_len, page);
  1284. data += data_len;
  1285. spare += spare_len;
  1286. /*
  1287. * Waiting only for CMDD or PAGED is not enough, ECC are
  1288. * partially written. No flag is set once the operation is
  1289. * really finished but the ND_RUN bit is cleared, so wait for it
  1290. * before stepping into the next command.
  1291. */
  1292. marvell_nfc_wait_ndrun(chip);
  1293. }
  1294. ret = marvell_nfc_wait_op(chip,
  1295. chip->data_interface.timings.sdr.tPROG_max);
  1296. marvell_nfc_disable_hw_ecc(chip);
  1297. if (ret)
  1298. return ret;
  1299. return 0;
  1300. }
  1301. static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct mtd_info *mtd,
  1302. struct nand_chip *chip,
  1303. int page)
  1304. {
  1305. /* Invalidate page cache */
  1306. chip->pagebuf = -1;
  1307. memset(chip->data_buf, 0xFF, mtd->writesize);
  1308. return chip->ecc.write_page_raw(mtd, chip, chip->data_buf, true, page);
  1309. }
  1310. static int marvell_nfc_hw_ecc_bch_write_oob(struct mtd_info *mtd,
  1311. struct nand_chip *chip, int page)
  1312. {
  1313. /* Invalidate page cache */
  1314. chip->pagebuf = -1;
  1315. memset(chip->data_buf, 0xFF, mtd->writesize);
  1316. return chip->ecc.write_page(mtd, chip, chip->data_buf, true, page);
  1317. }
  1318. /* NAND framework ->exec_op() hooks and related helpers */
  1319. static void marvell_nfc_parse_instructions(struct nand_chip *chip,
  1320. const struct nand_subop *subop,
  1321. struct marvell_nfc_op *nfc_op)
  1322. {
  1323. const struct nand_op_instr *instr = NULL;
  1324. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1325. bool first_cmd = true;
  1326. unsigned int op_id;
  1327. int i;
  1328. /* Reset the input structure as most of its fields will be OR'ed */
  1329. memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
  1330. for (op_id = 0; op_id < subop->ninstrs; op_id++) {
  1331. unsigned int offset, naddrs;
  1332. const u8 *addrs;
  1333. int len = nand_subop_get_data_len(subop, op_id);
  1334. instr = &subop->instrs[op_id];
  1335. switch (instr->type) {
  1336. case NAND_OP_CMD_INSTR:
  1337. if (first_cmd)
  1338. nfc_op->ndcb[0] |=
  1339. NDCB0_CMD1(instr->ctx.cmd.opcode);
  1340. else
  1341. nfc_op->ndcb[0] |=
  1342. NDCB0_CMD2(instr->ctx.cmd.opcode) |
  1343. NDCB0_DBC;
  1344. nfc_op->cle_ale_delay_ns = instr->delay_ns;
  1345. first_cmd = false;
  1346. break;
  1347. case NAND_OP_ADDR_INSTR:
  1348. offset = nand_subop_get_addr_start_off(subop, op_id);
  1349. naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
  1350. addrs = &instr->ctx.addr.addrs[offset];
  1351. nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
  1352. for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
  1353. nfc_op->ndcb[1] |= addrs[i] << (8 * i);
  1354. if (naddrs >= 5)
  1355. nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
  1356. if (naddrs >= 6)
  1357. nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
  1358. if (naddrs == 7)
  1359. nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
  1360. nfc_op->cle_ale_delay_ns = instr->delay_ns;
  1361. break;
  1362. case NAND_OP_DATA_IN_INSTR:
  1363. nfc_op->data_instr = instr;
  1364. nfc_op->data_instr_idx = op_id;
  1365. nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
  1366. if (nfc->caps->is_nfcv2) {
  1367. nfc_op->ndcb[0] |=
  1368. NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
  1369. NDCB0_LEN_OVRD;
  1370. nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
  1371. }
  1372. nfc_op->data_delay_ns = instr->delay_ns;
  1373. break;
  1374. case NAND_OP_DATA_OUT_INSTR:
  1375. nfc_op->data_instr = instr;
  1376. nfc_op->data_instr_idx = op_id;
  1377. nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
  1378. if (nfc->caps->is_nfcv2) {
  1379. nfc_op->ndcb[0] |=
  1380. NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
  1381. NDCB0_LEN_OVRD;
  1382. nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
  1383. }
  1384. nfc_op->data_delay_ns = instr->delay_ns;
  1385. break;
  1386. case NAND_OP_WAITRDY_INSTR:
  1387. nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
  1388. nfc_op->rdy_delay_ns = instr->delay_ns;
  1389. break;
  1390. }
  1391. }
  1392. }
  1393. static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
  1394. const struct nand_subop *subop,
  1395. struct marvell_nfc_op *nfc_op)
  1396. {
  1397. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1398. const struct nand_op_instr *instr = nfc_op->data_instr;
  1399. unsigned int op_id = nfc_op->data_instr_idx;
  1400. unsigned int len = nand_subop_get_data_len(subop, op_id);
  1401. unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
  1402. bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
  1403. int ret;
  1404. if (instr->ctx.data.force_8bit)
  1405. marvell_nfc_force_byte_access(chip, true);
  1406. if (reading) {
  1407. u8 *in = instr->ctx.data.buf.in + offset;
  1408. ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
  1409. } else {
  1410. const u8 *out = instr->ctx.data.buf.out + offset;
  1411. ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
  1412. }
  1413. if (instr->ctx.data.force_8bit)
  1414. marvell_nfc_force_byte_access(chip, false);
  1415. return ret;
  1416. }
  1417. static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
  1418. const struct nand_subop *subop)
  1419. {
  1420. struct marvell_nfc_op nfc_op;
  1421. bool reading;
  1422. int ret;
  1423. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1424. reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
  1425. ret = marvell_nfc_prepare_cmd(chip);
  1426. if (ret)
  1427. return ret;
  1428. marvell_nfc_send_cmd(chip, &nfc_op);
  1429. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
  1430. "RDDREQ/WRDREQ while draining raw data");
  1431. if (ret)
  1432. return ret;
  1433. cond_delay(nfc_op.cle_ale_delay_ns);
  1434. if (reading) {
  1435. if (nfc_op.rdy_timeout_ms) {
  1436. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1437. if (ret)
  1438. return ret;
  1439. }
  1440. cond_delay(nfc_op.rdy_delay_ns);
  1441. }
  1442. marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
  1443. ret = marvell_nfc_wait_cmdd(chip);
  1444. if (ret)
  1445. return ret;
  1446. cond_delay(nfc_op.data_delay_ns);
  1447. if (!reading) {
  1448. if (nfc_op.rdy_timeout_ms) {
  1449. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1450. if (ret)
  1451. return ret;
  1452. }
  1453. cond_delay(nfc_op.rdy_delay_ns);
  1454. }
  1455. /*
  1456. * NDCR ND_RUN bit should be cleared automatically at the end of each
  1457. * operation but experience shows that the behavior is buggy when it
  1458. * comes to writes (with LEN_OVRD). Clear it by hand in this case.
  1459. */
  1460. if (!reading) {
  1461. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1462. writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
  1463. nfc->regs + NDCR);
  1464. }
  1465. return 0;
  1466. }
  1467. static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
  1468. const struct nand_subop *subop)
  1469. {
  1470. struct marvell_nfc_op nfc_op;
  1471. int ret;
  1472. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1473. /*
  1474. * Naked access are different in that they need to be flagged as naked
  1475. * by the controller. Reset the controller registers fields that inform
  1476. * on the type and refill them according to the ongoing operation.
  1477. */
  1478. nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
  1479. NDCB0_CMD_XTYPE(XTYPE_MASK));
  1480. switch (subop->instrs[0].type) {
  1481. case NAND_OP_CMD_INSTR:
  1482. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
  1483. break;
  1484. case NAND_OP_ADDR_INSTR:
  1485. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
  1486. break;
  1487. case NAND_OP_DATA_IN_INSTR:
  1488. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
  1489. NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
  1490. break;
  1491. case NAND_OP_DATA_OUT_INSTR:
  1492. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
  1493. NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
  1494. break;
  1495. default:
  1496. /* This should never happen */
  1497. break;
  1498. }
  1499. ret = marvell_nfc_prepare_cmd(chip);
  1500. if (ret)
  1501. return ret;
  1502. marvell_nfc_send_cmd(chip, &nfc_op);
  1503. if (!nfc_op.data_instr) {
  1504. ret = marvell_nfc_wait_cmdd(chip);
  1505. cond_delay(nfc_op.cle_ale_delay_ns);
  1506. return ret;
  1507. }
  1508. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
  1509. "RDDREQ/WRDREQ while draining raw data");
  1510. if (ret)
  1511. return ret;
  1512. marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
  1513. ret = marvell_nfc_wait_cmdd(chip);
  1514. if (ret)
  1515. return ret;
  1516. /*
  1517. * NDCR ND_RUN bit should be cleared automatically at the end of each
  1518. * operation but experience shows that the behavior is buggy when it
  1519. * comes to writes (with LEN_OVRD). Clear it by hand in this case.
  1520. */
  1521. if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
  1522. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1523. writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
  1524. nfc->regs + NDCR);
  1525. }
  1526. return 0;
  1527. }
  1528. static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
  1529. const struct nand_subop *subop)
  1530. {
  1531. struct marvell_nfc_op nfc_op;
  1532. int ret;
  1533. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1534. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1535. cond_delay(nfc_op.rdy_delay_ns);
  1536. return ret;
  1537. }
  1538. static int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
  1539. const struct nand_subop *subop)
  1540. {
  1541. struct marvell_nfc_op nfc_op;
  1542. int ret;
  1543. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1544. nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
  1545. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
  1546. ret = marvell_nfc_prepare_cmd(chip);
  1547. if (ret)
  1548. return ret;
  1549. marvell_nfc_send_cmd(chip, &nfc_op);
  1550. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  1551. "RDDREQ while reading ID");
  1552. if (ret)
  1553. return ret;
  1554. cond_delay(nfc_op.cle_ale_delay_ns);
  1555. if (nfc_op.rdy_timeout_ms) {
  1556. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1557. if (ret)
  1558. return ret;
  1559. }
  1560. cond_delay(nfc_op.rdy_delay_ns);
  1561. marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
  1562. ret = marvell_nfc_wait_cmdd(chip);
  1563. if (ret)
  1564. return ret;
  1565. cond_delay(nfc_op.data_delay_ns);
  1566. return 0;
  1567. }
  1568. static int marvell_nfc_read_status_exec(struct nand_chip *chip,
  1569. const struct nand_subop *subop)
  1570. {
  1571. struct marvell_nfc_op nfc_op;
  1572. int ret;
  1573. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1574. nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
  1575. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
  1576. ret = marvell_nfc_prepare_cmd(chip);
  1577. if (ret)
  1578. return ret;
  1579. marvell_nfc_send_cmd(chip, &nfc_op);
  1580. ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
  1581. "RDDREQ while reading status");
  1582. if (ret)
  1583. return ret;
  1584. cond_delay(nfc_op.cle_ale_delay_ns);
  1585. if (nfc_op.rdy_timeout_ms) {
  1586. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1587. if (ret)
  1588. return ret;
  1589. }
  1590. cond_delay(nfc_op.rdy_delay_ns);
  1591. marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
  1592. ret = marvell_nfc_wait_cmdd(chip);
  1593. if (ret)
  1594. return ret;
  1595. cond_delay(nfc_op.data_delay_ns);
  1596. return 0;
  1597. }
  1598. static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
  1599. const struct nand_subop *subop)
  1600. {
  1601. struct marvell_nfc_op nfc_op;
  1602. int ret;
  1603. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1604. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
  1605. ret = marvell_nfc_prepare_cmd(chip);
  1606. if (ret)
  1607. return ret;
  1608. marvell_nfc_send_cmd(chip, &nfc_op);
  1609. ret = marvell_nfc_wait_cmdd(chip);
  1610. if (ret)
  1611. return ret;
  1612. cond_delay(nfc_op.cle_ale_delay_ns);
  1613. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1614. if (ret)
  1615. return ret;
  1616. cond_delay(nfc_op.rdy_delay_ns);
  1617. return 0;
  1618. }
  1619. static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
  1620. const struct nand_subop *subop)
  1621. {
  1622. struct marvell_nfc_op nfc_op;
  1623. int ret;
  1624. marvell_nfc_parse_instructions(chip, subop, &nfc_op);
  1625. nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
  1626. ret = marvell_nfc_prepare_cmd(chip);
  1627. if (ret)
  1628. return ret;
  1629. marvell_nfc_send_cmd(chip, &nfc_op);
  1630. ret = marvell_nfc_wait_cmdd(chip);
  1631. if (ret)
  1632. return ret;
  1633. cond_delay(nfc_op.cle_ale_delay_ns);
  1634. ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
  1635. if (ret)
  1636. return ret;
  1637. cond_delay(nfc_op.rdy_delay_ns);
  1638. return 0;
  1639. }
  1640. static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
  1641. /* Monolithic reads/writes */
  1642. NAND_OP_PARSER_PATTERN(
  1643. marvell_nfc_monolithic_access_exec,
  1644. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1645. NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
  1646. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  1647. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
  1648. NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
  1649. NAND_OP_PARSER_PATTERN(
  1650. marvell_nfc_monolithic_access_exec,
  1651. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1652. NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
  1653. NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
  1654. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  1655. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
  1656. /* Naked commands */
  1657. NAND_OP_PARSER_PATTERN(
  1658. marvell_nfc_naked_access_exec,
  1659. NAND_OP_PARSER_PAT_CMD_ELEM(false)),
  1660. NAND_OP_PARSER_PATTERN(
  1661. marvell_nfc_naked_access_exec,
  1662. NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
  1663. NAND_OP_PARSER_PATTERN(
  1664. marvell_nfc_naked_access_exec,
  1665. NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
  1666. NAND_OP_PARSER_PATTERN(
  1667. marvell_nfc_naked_access_exec,
  1668. NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
  1669. NAND_OP_PARSER_PATTERN(
  1670. marvell_nfc_naked_waitrdy_exec,
  1671. NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
  1672. );
  1673. static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
  1674. /* Naked commands not supported, use a function for each pattern */
  1675. NAND_OP_PARSER_PATTERN(
  1676. marvell_nfc_read_id_type_exec,
  1677. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1678. NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
  1679. NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
  1680. NAND_OP_PARSER_PATTERN(
  1681. marvell_nfc_erase_cmd_type_exec,
  1682. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1683. NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
  1684. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1685. NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
  1686. NAND_OP_PARSER_PATTERN(
  1687. marvell_nfc_read_status_exec,
  1688. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1689. NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
  1690. NAND_OP_PARSER_PATTERN(
  1691. marvell_nfc_reset_cmd_type_exec,
  1692. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1693. NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
  1694. NAND_OP_PARSER_PATTERN(
  1695. marvell_nfc_naked_waitrdy_exec,
  1696. NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
  1697. );
  1698. static int marvell_nfc_exec_op(struct nand_chip *chip,
  1699. const struct nand_operation *op,
  1700. bool check_only)
  1701. {
  1702. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1703. if (nfc->caps->is_nfcv2)
  1704. return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
  1705. op, check_only);
  1706. else
  1707. return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
  1708. op, check_only);
  1709. }
  1710. /*
  1711. * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
  1712. * usable.
  1713. */
  1714. static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
  1715. struct mtd_oob_region *oobregion)
  1716. {
  1717. struct nand_chip *chip = mtd_to_nand(mtd);
  1718. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1719. if (section)
  1720. return -ERANGE;
  1721. oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
  1722. lt->last_ecc_bytes;
  1723. oobregion->offset = mtd->oobsize - oobregion->length;
  1724. return 0;
  1725. }
  1726. static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
  1727. struct mtd_oob_region *oobregion)
  1728. {
  1729. struct nand_chip *chip = mtd_to_nand(mtd);
  1730. const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
  1731. if (section)
  1732. return -ERANGE;
  1733. /*
  1734. * Bootrom looks in bytes 0 & 5 for bad blocks for the
  1735. * 4KB page / 4bit BCH combination.
  1736. */
  1737. if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
  1738. oobregion->offset = 6;
  1739. else
  1740. oobregion->offset = 2;
  1741. oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
  1742. lt->last_spare_bytes - oobregion->offset;
  1743. return 0;
  1744. }
  1745. static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
  1746. .ecc = marvell_nand_ooblayout_ecc,
  1747. .free = marvell_nand_ooblayout_free,
  1748. };
  1749. static int marvell_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
  1750. struct nand_ecc_ctrl *ecc)
  1751. {
  1752. struct nand_chip *chip = mtd_to_nand(mtd);
  1753. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1754. const struct marvell_hw_ecc_layout *l;
  1755. int i;
  1756. if (!nfc->caps->is_nfcv2 &&
  1757. (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
  1758. dev_err(nfc->dev,
  1759. "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
  1760. mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
  1761. return -ENOTSUPP;
  1762. }
  1763. to_marvell_nand(chip)->layout = NULL;
  1764. for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
  1765. l = &marvell_nfc_layouts[i];
  1766. if (mtd->writesize == l->writesize &&
  1767. ecc->size == l->chunk && ecc->strength == l->strength) {
  1768. to_marvell_nand(chip)->layout = l;
  1769. break;
  1770. }
  1771. }
  1772. if (!to_marvell_nand(chip)->layout ||
  1773. (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
  1774. dev_err(nfc->dev,
  1775. "ECC strength %d at page size %d is not supported\n",
  1776. ecc->strength, mtd->writesize);
  1777. return -ENOTSUPP;
  1778. }
  1779. mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
  1780. ecc->steps = l->nchunks;
  1781. ecc->size = l->data_bytes;
  1782. if (ecc->strength == 1) {
  1783. chip->ecc.algo = NAND_ECC_HAMMING;
  1784. ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
  1785. ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
  1786. ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
  1787. ecc->read_oob = ecc->read_oob_raw;
  1788. ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
  1789. ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
  1790. ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
  1791. ecc->write_oob = ecc->write_oob_raw;
  1792. } else {
  1793. chip->ecc.algo = NAND_ECC_BCH;
  1794. ecc->strength = 16;
  1795. ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
  1796. ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
  1797. ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
  1798. ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
  1799. ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
  1800. ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
  1801. ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
  1802. ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
  1803. }
  1804. return 0;
  1805. }
  1806. static int marvell_nand_ecc_init(struct mtd_info *mtd,
  1807. struct nand_ecc_ctrl *ecc)
  1808. {
  1809. struct nand_chip *chip = mtd_to_nand(mtd);
  1810. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1811. int ret;
  1812. if (ecc->mode != NAND_ECC_NONE && (!ecc->size || !ecc->strength)) {
  1813. if (chip->ecc_step_ds && chip->ecc_strength_ds) {
  1814. ecc->size = chip->ecc_step_ds;
  1815. ecc->strength = chip->ecc_strength_ds;
  1816. } else {
  1817. dev_info(nfc->dev,
  1818. "No minimum ECC strength, using 1b/512B\n");
  1819. ecc->size = 512;
  1820. ecc->strength = 1;
  1821. }
  1822. }
  1823. switch (ecc->mode) {
  1824. case NAND_ECC_HW:
  1825. ret = marvell_nand_hw_ecc_ctrl_init(mtd, ecc);
  1826. if (ret)
  1827. return ret;
  1828. break;
  1829. case NAND_ECC_NONE:
  1830. case NAND_ECC_SOFT:
  1831. if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
  1832. mtd->writesize != SZ_2K) {
  1833. dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
  1834. mtd->writesize);
  1835. return -EINVAL;
  1836. }
  1837. break;
  1838. default:
  1839. return -EINVAL;
  1840. }
  1841. return 0;
  1842. }
  1843. static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
  1844. static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
  1845. static struct nand_bbt_descr bbt_main_descr = {
  1846. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  1847. NAND_BBT_2BIT | NAND_BBT_VERSION,
  1848. .offs = 8,
  1849. .len = 6,
  1850. .veroffs = 14,
  1851. .maxblocks = 8, /* Last 8 blocks in each chip */
  1852. .pattern = bbt_pattern
  1853. };
  1854. static struct nand_bbt_descr bbt_mirror_descr = {
  1855. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  1856. NAND_BBT_2BIT | NAND_BBT_VERSION,
  1857. .offs = 8,
  1858. .len = 6,
  1859. .veroffs = 14,
  1860. .maxblocks = 8, /* Last 8 blocks in each chip */
  1861. .pattern = bbt_mirror_pattern
  1862. };
  1863. static int marvell_nfc_setup_data_interface(struct mtd_info *mtd, int chipnr,
  1864. const struct nand_data_interface
  1865. *conf)
  1866. {
  1867. struct nand_chip *chip = mtd_to_nand(mtd);
  1868. struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
  1869. struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
  1870. unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
  1871. const struct nand_sdr_timings *sdr;
  1872. struct marvell_nfc_timings nfc_tmg;
  1873. int read_delay;
  1874. sdr = nand_get_sdr_timings(conf);
  1875. if (IS_ERR(sdr))
  1876. return PTR_ERR(sdr);
  1877. /*
  1878. * SDR timings are given in pico-seconds while NFC timings must be
  1879. * expressed in NAND controller clock cycles, which is half of the
  1880. * frequency of the accessible ECC clock retrieved by clk_get_rate().
  1881. * This is not written anywhere in the datasheet but was observed
  1882. * with an oscilloscope.
  1883. *
  1884. * NFC datasheet gives equations from which thoses calculations
  1885. * are derived, they tend to be slightly more restrictives than the
  1886. * given core timings and may improve the overall speed.
  1887. */
  1888. nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
  1889. nfc_tmg.tRH = nfc_tmg.tRP;
  1890. nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
  1891. nfc_tmg.tWH = nfc_tmg.tWP;
  1892. nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
  1893. nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
  1894. nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
  1895. /*
  1896. * Read delay is the time of propagation from SoC pins to NFC internal
  1897. * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
  1898. * EDO mode, an additional delay of tRH must be taken into account so
  1899. * the data is sampled on the falling edge instead of the rising edge.
  1900. */
  1901. read_delay = sdr->tRC_min >= 30000 ?
  1902. MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
  1903. nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
  1904. /*
  1905. * tWHR and tRHW are supposed to be read to write delays (and vice
  1906. * versa) but in some cases, ie. when doing a change column, they must
  1907. * be greater than that to be sure tCCS delay is respected.
  1908. */
  1909. nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
  1910. period_ns) - 2,
  1911. nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
  1912. period_ns);
  1913. /*
  1914. * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
  1915. * NFCv1: No WAIT_MODE, tR must be maximal.
  1916. */
  1917. if (nfc->caps->is_nfcv2) {
  1918. nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
  1919. } else {
  1920. nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
  1921. period_ns);
  1922. if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
  1923. nfc_tmg.tR = nfc_tmg.tCH - 3;
  1924. else
  1925. nfc_tmg.tR = 0;
  1926. }
  1927. if (chipnr < 0)
  1928. return 0;
  1929. marvell_nand->ndtr0 =
  1930. NDTR0_TRP(nfc_tmg.tRP) |
  1931. NDTR0_TRH(nfc_tmg.tRH) |
  1932. NDTR0_ETRP(nfc_tmg.tRP) |
  1933. NDTR0_TWP(nfc_tmg.tWP) |
  1934. NDTR0_TWH(nfc_tmg.tWH) |
  1935. NDTR0_TCS(nfc_tmg.tCS) |
  1936. NDTR0_TCH(nfc_tmg.tCH);
  1937. marvell_nand->ndtr1 =
  1938. NDTR1_TAR(nfc_tmg.tAR) |
  1939. NDTR1_TWHR(nfc_tmg.tWHR) |
  1940. NDTR1_TR(nfc_tmg.tR);
  1941. if (nfc->caps->is_nfcv2) {
  1942. marvell_nand->ndtr0 |=
  1943. NDTR0_RD_CNT_DEL(read_delay) |
  1944. NDTR0_SELCNTR |
  1945. NDTR0_TADL(nfc_tmg.tADL);
  1946. marvell_nand->ndtr1 |=
  1947. NDTR1_TRHW(nfc_tmg.tRHW) |
  1948. NDTR1_WAIT_MODE;
  1949. }
  1950. return 0;
  1951. }
  1952. static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
  1953. struct device_node *np)
  1954. {
  1955. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
  1956. struct marvell_nand_chip *marvell_nand;
  1957. struct mtd_info *mtd;
  1958. struct nand_chip *chip;
  1959. int nsels, ret, i;
  1960. u32 cs, rb;
  1961. /*
  1962. * The legacy "num-cs" property indicates the number of CS on the only
  1963. * chip connected to the controller (legacy bindings does not support
  1964. * more than one chip). The CS and RB pins are always the #0.
  1965. *
  1966. * When not using legacy bindings, a couple of "reg" and "nand-rb"
  1967. * properties must be filled. For each chip, expressed as a subnode,
  1968. * "reg" points to the CS lines and "nand-rb" to the RB line.
  1969. */
  1970. if (pdata || nfc->caps->legacy_of_bindings) {
  1971. nsels = 1;
  1972. } else {
  1973. nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
  1974. if (nsels <= 0) {
  1975. dev_err(dev, "missing/invalid reg property\n");
  1976. return -EINVAL;
  1977. }
  1978. }
  1979. /* Alloc the nand chip structure */
  1980. marvell_nand = devm_kzalloc(dev, sizeof(*marvell_nand) +
  1981. (nsels *
  1982. sizeof(struct marvell_nand_chip_sel)),
  1983. GFP_KERNEL);
  1984. if (!marvell_nand) {
  1985. dev_err(dev, "could not allocate chip structure\n");
  1986. return -ENOMEM;
  1987. }
  1988. marvell_nand->nsels = nsels;
  1989. marvell_nand->selected_die = -1;
  1990. for (i = 0; i < nsels; i++) {
  1991. if (pdata || nfc->caps->legacy_of_bindings) {
  1992. /*
  1993. * Legacy bindings use the CS lines in natural
  1994. * order (0, 1, ...)
  1995. */
  1996. cs = i;
  1997. } else {
  1998. /* Retrieve CS id */
  1999. ret = of_property_read_u32_index(np, "reg", i, &cs);
  2000. if (ret) {
  2001. dev_err(dev, "could not retrieve reg property: %d\n",
  2002. ret);
  2003. return ret;
  2004. }
  2005. }
  2006. if (cs >= nfc->caps->max_cs_nb) {
  2007. dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
  2008. cs, nfc->caps->max_cs_nb);
  2009. return -EINVAL;
  2010. }
  2011. if (test_and_set_bit(cs, &nfc->assigned_cs)) {
  2012. dev_err(dev, "CS %d already assigned\n", cs);
  2013. return -EINVAL;
  2014. }
  2015. /*
  2016. * The cs variable represents the chip select id, which must be
  2017. * converted in bit fields for NDCB0 and NDCB2 to select the
  2018. * right chip. Unfortunately, due to a lack of information on
  2019. * the subject and incoherent documentation, the user should not
  2020. * use CS1 and CS3 at all as asserting them is not supported in
  2021. * a reliable way (due to multiplexing inside ADDR5 field).
  2022. */
  2023. marvell_nand->sels[i].cs = cs;
  2024. switch (cs) {
  2025. case 0:
  2026. case 2:
  2027. marvell_nand->sels[i].ndcb0_csel = 0;
  2028. break;
  2029. case 1:
  2030. case 3:
  2031. marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
  2032. break;
  2033. default:
  2034. return -EINVAL;
  2035. }
  2036. /* Retrieve RB id */
  2037. if (pdata || nfc->caps->legacy_of_bindings) {
  2038. /* Legacy bindings always use RB #0 */
  2039. rb = 0;
  2040. } else {
  2041. ret = of_property_read_u32_index(np, "nand-rb", i,
  2042. &rb);
  2043. if (ret) {
  2044. dev_err(dev,
  2045. "could not retrieve RB property: %d\n",
  2046. ret);
  2047. return ret;
  2048. }
  2049. }
  2050. if (rb >= nfc->caps->max_rb_nb) {
  2051. dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
  2052. rb, nfc->caps->max_rb_nb);
  2053. return -EINVAL;
  2054. }
  2055. marvell_nand->sels[i].rb = rb;
  2056. }
  2057. chip = &marvell_nand->chip;
  2058. chip->controller = &nfc->controller;
  2059. nand_set_flash_node(chip, np);
  2060. chip->exec_op = marvell_nfc_exec_op;
  2061. chip->select_chip = marvell_nfc_select_chip;
  2062. if (!of_property_read_bool(np, "marvell,nand-keep-config"))
  2063. chip->setup_data_interface = marvell_nfc_setup_data_interface;
  2064. mtd = nand_to_mtd(chip);
  2065. mtd->dev.parent = dev;
  2066. /*
  2067. * Default to HW ECC engine mode. If the nand-ecc-mode property is given
  2068. * in the DT node, this entry will be overwritten in nand_scan_ident().
  2069. */
  2070. chip->ecc.mode = NAND_ECC_HW;
  2071. /*
  2072. * Save a reference value for timing registers before
  2073. * ->setup_data_interface() is called.
  2074. */
  2075. marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
  2076. marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
  2077. chip->options |= NAND_BUSWIDTH_AUTO;
  2078. ret = nand_scan_ident(mtd, marvell_nand->nsels, NULL);
  2079. if (ret) {
  2080. dev_err(dev, "could not identify the nand chip\n");
  2081. return ret;
  2082. }
  2083. if (pdata && pdata->flash_bbt)
  2084. chip->bbt_options |= NAND_BBT_USE_FLASH;
  2085. if (chip->bbt_options & NAND_BBT_USE_FLASH) {
  2086. /*
  2087. * We'll use a bad block table stored in-flash and don't
  2088. * allow writing the bad block marker to the flash.
  2089. */
  2090. chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
  2091. chip->bbt_td = &bbt_main_descr;
  2092. chip->bbt_md = &bbt_mirror_descr;
  2093. }
  2094. /* Save the chip-specific fields of NDCR */
  2095. marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
  2096. if (chip->options & NAND_BUSWIDTH_16)
  2097. marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
  2098. /*
  2099. * On small page NANDs, only one cycle is needed to pass the
  2100. * column address.
  2101. */
  2102. if (mtd->writesize <= 512) {
  2103. marvell_nand->addr_cyc = 1;
  2104. } else {
  2105. marvell_nand->addr_cyc = 2;
  2106. marvell_nand->ndcr |= NDCR_RA_START;
  2107. }
  2108. /*
  2109. * Now add the number of cycles needed to pass the row
  2110. * address.
  2111. *
  2112. * Addressing a chip using CS 2 or 3 should also need the third row
  2113. * cycle but due to inconsistance in the documentation and lack of
  2114. * hardware to test this situation, this case is not supported.
  2115. */
  2116. if (chip->options & NAND_ROW_ADDR_3)
  2117. marvell_nand->addr_cyc += 3;
  2118. else
  2119. marvell_nand->addr_cyc += 2;
  2120. if (pdata) {
  2121. chip->ecc.size = pdata->ecc_step_size;
  2122. chip->ecc.strength = pdata->ecc_strength;
  2123. }
  2124. ret = marvell_nand_ecc_init(mtd, &chip->ecc);
  2125. if (ret) {
  2126. dev_err(dev, "ECC init failed: %d\n", ret);
  2127. return ret;
  2128. }
  2129. if (chip->ecc.mode == NAND_ECC_HW) {
  2130. /*
  2131. * Subpage write not available with hardware ECC, prohibit also
  2132. * subpage read as in userspace subpage access would still be
  2133. * allowed and subpage write, if used, would lead to numerous
  2134. * uncorrectable ECC errors.
  2135. */
  2136. chip->options |= NAND_NO_SUBPAGE_WRITE;
  2137. }
  2138. if (pdata || nfc->caps->legacy_of_bindings) {
  2139. /*
  2140. * We keep the MTD name unchanged to avoid breaking platforms
  2141. * where the MTD cmdline parser is used and the bootloader
  2142. * has not been updated to use the new naming scheme.
  2143. */
  2144. mtd->name = "pxa3xx_nand-0";
  2145. } else if (!mtd->name) {
  2146. /*
  2147. * If the new bindings are used and the bootloader has not been
  2148. * updated to pass a new mtdparts parameter on the cmdline, you
  2149. * should define the following property in your NAND node, ie:
  2150. *
  2151. * label = "main-storage";
  2152. *
  2153. * This way, mtd->name will be set by the core when
  2154. * nand_set_flash_node() is called.
  2155. */
  2156. mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
  2157. "%s:nand.%d", dev_name(nfc->dev),
  2158. marvell_nand->sels[0].cs);
  2159. if (!mtd->name) {
  2160. dev_err(nfc->dev, "Failed to allocate mtd->name\n");
  2161. return -ENOMEM;
  2162. }
  2163. }
  2164. ret = nand_scan_tail(mtd);
  2165. if (ret) {
  2166. dev_err(dev, "nand_scan_tail failed: %d\n", ret);
  2167. return ret;
  2168. }
  2169. if (pdata)
  2170. /* Legacy bindings support only one chip */
  2171. ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
  2172. else
  2173. ret = mtd_device_register(mtd, NULL, 0);
  2174. if (ret) {
  2175. dev_err(dev, "failed to register mtd device: %d\n", ret);
  2176. nand_release(mtd);
  2177. return ret;
  2178. }
  2179. list_add_tail(&marvell_nand->node, &nfc->chips);
  2180. return 0;
  2181. }
  2182. static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
  2183. {
  2184. struct device_node *np = dev->of_node;
  2185. struct device_node *nand_np;
  2186. int max_cs = nfc->caps->max_cs_nb;
  2187. int nchips;
  2188. int ret;
  2189. if (!np)
  2190. nchips = 1;
  2191. else
  2192. nchips = of_get_child_count(np);
  2193. if (nchips > max_cs) {
  2194. dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
  2195. max_cs);
  2196. return -EINVAL;
  2197. }
  2198. /*
  2199. * Legacy bindings do not use child nodes to exhibit NAND chip
  2200. * properties and layout. Instead, NAND properties are mixed with the
  2201. * controller ones, and partitions are defined as direct subnodes of the
  2202. * NAND controller node.
  2203. */
  2204. if (nfc->caps->legacy_of_bindings) {
  2205. ret = marvell_nand_chip_init(dev, nfc, np);
  2206. return ret;
  2207. }
  2208. for_each_child_of_node(np, nand_np) {
  2209. ret = marvell_nand_chip_init(dev, nfc, nand_np);
  2210. if (ret) {
  2211. of_node_put(nand_np);
  2212. return ret;
  2213. }
  2214. }
  2215. return 0;
  2216. }
  2217. static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
  2218. {
  2219. struct marvell_nand_chip *entry, *temp;
  2220. list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
  2221. nand_release(nand_to_mtd(&entry->chip));
  2222. list_del(&entry->node);
  2223. }
  2224. }
  2225. static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
  2226. {
  2227. struct platform_device *pdev = container_of(nfc->dev,
  2228. struct platform_device,
  2229. dev);
  2230. struct dma_slave_config config = {};
  2231. struct resource *r;
  2232. dma_cap_mask_t mask;
  2233. struct pxad_param param;
  2234. int ret;
  2235. if (!IS_ENABLED(CONFIG_PXA_DMA)) {
  2236. dev_warn(nfc->dev,
  2237. "DMA not enabled in configuration\n");
  2238. return -ENOTSUPP;
  2239. }
  2240. ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
  2241. if (ret)
  2242. return ret;
  2243. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  2244. if (!r) {
  2245. dev_err(nfc->dev, "No resource defined for data DMA\n");
  2246. return -ENXIO;
  2247. }
  2248. param.drcmr = r->start;
  2249. param.prio = PXAD_PRIO_LOWEST;
  2250. dma_cap_zero(mask);
  2251. dma_cap_set(DMA_SLAVE, mask);
  2252. nfc->dma_chan =
  2253. dma_request_slave_channel_compat(mask, pxad_filter_fn,
  2254. &param, nfc->dev,
  2255. "data");
  2256. if (!nfc->dma_chan) {
  2257. dev_err(nfc->dev,
  2258. "Unable to request data DMA channel\n");
  2259. return -ENODEV;
  2260. }
  2261. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2262. if (!r)
  2263. return -ENXIO;
  2264. config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  2265. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  2266. config.src_addr = r->start + NDDB;
  2267. config.dst_addr = r->start + NDDB;
  2268. config.src_maxburst = 32;
  2269. config.dst_maxburst = 32;
  2270. ret = dmaengine_slave_config(nfc->dma_chan, &config);
  2271. if (ret < 0) {
  2272. dev_err(nfc->dev, "Failed to configure DMA channel\n");
  2273. return ret;
  2274. }
  2275. /*
  2276. * DMA must act on length multiple of 32 and this length may be
  2277. * bigger than the destination buffer. Use this buffer instead
  2278. * for DMA transfers and then copy the desired amount of data to
  2279. * the provided buffer.
  2280. */
  2281. nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
  2282. if (!nfc->dma_buf)
  2283. return -ENOMEM;
  2284. nfc->use_dma = true;
  2285. return 0;
  2286. }
  2287. static int marvell_nfc_init(struct marvell_nfc *nfc)
  2288. {
  2289. struct device_node *np = nfc->dev->of_node;
  2290. /*
  2291. * Some SoCs like A7k/A8k need to enable manually the NAND
  2292. * controller, gated clocks and reset bits to avoid being bootloader
  2293. * dependent. This is done through the use of the System Functions
  2294. * registers.
  2295. */
  2296. if (nfc->caps->need_system_controller) {
  2297. struct regmap *sysctrl_base =
  2298. syscon_regmap_lookup_by_phandle(np,
  2299. "marvell,system-controller");
  2300. u32 reg;
  2301. if (IS_ERR(sysctrl_base))
  2302. return PTR_ERR(sysctrl_base);
  2303. reg = GENCONF_SOC_DEVICE_MUX_NFC_EN |
  2304. GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
  2305. GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
  2306. GENCONF_SOC_DEVICE_MUX_NFC_INT_EN;
  2307. regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX, reg);
  2308. regmap_read(sysctrl_base, GENCONF_CLK_GATING_CTRL, &reg);
  2309. reg |= GENCONF_CLK_GATING_CTRL_ND_GATE;
  2310. regmap_write(sysctrl_base, GENCONF_CLK_GATING_CTRL, reg);
  2311. regmap_read(sysctrl_base, GENCONF_ND_CLK_CTRL, &reg);
  2312. reg |= GENCONF_ND_CLK_CTRL_EN;
  2313. regmap_write(sysctrl_base, GENCONF_ND_CLK_CTRL, reg);
  2314. }
  2315. /* Configure the DMA if appropriate */
  2316. if (!nfc->caps->is_nfcv2)
  2317. marvell_nfc_init_dma(nfc);
  2318. /*
  2319. * ECC operations and interruptions are only enabled when specifically
  2320. * needed. ECC shall not be activated in the early stages (fails probe).
  2321. * Arbiter flag, even if marked as "reserved", must be set (empirical).
  2322. * SPARE_EN bit must always be set or ECC bytes will not be at the same
  2323. * offset in the read page and this will fail the protection.
  2324. */
  2325. writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
  2326. NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
  2327. writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
  2328. writel_relaxed(0, nfc->regs + NDECCCTRL);
  2329. return 0;
  2330. }
  2331. static int marvell_nfc_probe(struct platform_device *pdev)
  2332. {
  2333. struct device *dev = &pdev->dev;
  2334. struct resource *r;
  2335. struct marvell_nfc *nfc;
  2336. int ret;
  2337. int irq;
  2338. nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
  2339. GFP_KERNEL);
  2340. if (!nfc)
  2341. return -ENOMEM;
  2342. nfc->dev = dev;
  2343. nand_hw_control_init(&nfc->controller);
  2344. INIT_LIST_HEAD(&nfc->chips);
  2345. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2346. nfc->regs = devm_ioremap_resource(dev, r);
  2347. if (IS_ERR(nfc->regs))
  2348. return PTR_ERR(nfc->regs);
  2349. irq = platform_get_irq(pdev, 0);
  2350. if (irq < 0) {
  2351. dev_err(dev, "failed to retrieve irq\n");
  2352. return irq;
  2353. }
  2354. nfc->core_clk = devm_clk_get(&pdev->dev, "core");
  2355. /* Managed the legacy case (when the first clock was not named) */
  2356. if (nfc->core_clk == ERR_PTR(-ENOENT))
  2357. nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
  2358. if (IS_ERR(nfc->core_clk))
  2359. return PTR_ERR(nfc->core_clk);
  2360. ret = clk_prepare_enable(nfc->core_clk);
  2361. if (ret)
  2362. return ret;
  2363. nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
  2364. if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
  2365. if (!IS_ERR(nfc->reg_clk)) {
  2366. ret = clk_prepare_enable(nfc->reg_clk);
  2367. if (ret)
  2368. goto unprepare_core_clk;
  2369. } else {
  2370. ret = PTR_ERR(nfc->reg_clk);
  2371. goto unprepare_core_clk;
  2372. }
  2373. }
  2374. marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
  2375. marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
  2376. ret = devm_request_irq(dev, irq, marvell_nfc_isr,
  2377. 0, "marvell-nfc", nfc);
  2378. if (ret)
  2379. goto unprepare_reg_clk;
  2380. /* Get NAND controller capabilities */
  2381. if (pdev->id_entry)
  2382. nfc->caps = (void *)pdev->id_entry->driver_data;
  2383. else
  2384. nfc->caps = of_device_get_match_data(&pdev->dev);
  2385. if (!nfc->caps) {
  2386. dev_err(dev, "Could not retrieve NFC caps\n");
  2387. ret = -EINVAL;
  2388. goto unprepare_reg_clk;
  2389. }
  2390. /* Init the controller and then probe the chips */
  2391. ret = marvell_nfc_init(nfc);
  2392. if (ret)
  2393. goto unprepare_reg_clk;
  2394. platform_set_drvdata(pdev, nfc);
  2395. ret = marvell_nand_chips_init(dev, nfc);
  2396. if (ret)
  2397. goto unprepare_reg_clk;
  2398. return 0;
  2399. unprepare_reg_clk:
  2400. clk_disable_unprepare(nfc->reg_clk);
  2401. unprepare_core_clk:
  2402. clk_disable_unprepare(nfc->core_clk);
  2403. return ret;
  2404. }
  2405. static int marvell_nfc_remove(struct platform_device *pdev)
  2406. {
  2407. struct marvell_nfc *nfc = platform_get_drvdata(pdev);
  2408. marvell_nand_chips_cleanup(nfc);
  2409. if (nfc->use_dma) {
  2410. dmaengine_terminate_all(nfc->dma_chan);
  2411. dma_release_channel(nfc->dma_chan);
  2412. }
  2413. clk_disable_unprepare(nfc->reg_clk);
  2414. clk_disable_unprepare(nfc->core_clk);
  2415. return 0;
  2416. }
  2417. static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
  2418. .max_cs_nb = 4,
  2419. .max_rb_nb = 2,
  2420. .need_system_controller = true,
  2421. .is_nfcv2 = true,
  2422. };
  2423. static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
  2424. .max_cs_nb = 4,
  2425. .max_rb_nb = 2,
  2426. .is_nfcv2 = true,
  2427. };
  2428. static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
  2429. .max_cs_nb = 2,
  2430. .max_rb_nb = 1,
  2431. .use_dma = true,
  2432. };
  2433. static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
  2434. .max_cs_nb = 4,
  2435. .max_rb_nb = 2,
  2436. .need_system_controller = true,
  2437. .legacy_of_bindings = true,
  2438. .is_nfcv2 = true,
  2439. };
  2440. static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
  2441. .max_cs_nb = 4,
  2442. .max_rb_nb = 2,
  2443. .legacy_of_bindings = true,
  2444. .is_nfcv2 = true,
  2445. };
  2446. static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
  2447. .max_cs_nb = 2,
  2448. .max_rb_nb = 1,
  2449. .legacy_of_bindings = true,
  2450. .use_dma = true,
  2451. };
  2452. static const struct platform_device_id marvell_nfc_platform_ids[] = {
  2453. {
  2454. .name = "pxa3xx-nand",
  2455. .driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
  2456. },
  2457. { /* sentinel */ },
  2458. };
  2459. MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
  2460. static const struct of_device_id marvell_nfc_of_ids[] = {
  2461. {
  2462. .compatible = "marvell,armada-8k-nand-controller",
  2463. .data = &marvell_armada_8k_nfc_caps,
  2464. },
  2465. {
  2466. .compatible = "marvell,armada370-nand-controller",
  2467. .data = &marvell_armada370_nfc_caps,
  2468. },
  2469. {
  2470. .compatible = "marvell,pxa3xx-nand-controller",
  2471. .data = &marvell_pxa3xx_nfc_caps,
  2472. },
  2473. /* Support for old/deprecated bindings: */
  2474. {
  2475. .compatible = "marvell,armada-8k-nand",
  2476. .data = &marvell_armada_8k_nfc_legacy_caps,
  2477. },
  2478. {
  2479. .compatible = "marvell,armada370-nand",
  2480. .data = &marvell_armada370_nfc_legacy_caps,
  2481. },
  2482. {
  2483. .compatible = "marvell,pxa3xx-nand",
  2484. .data = &marvell_pxa3xx_nfc_legacy_caps,
  2485. },
  2486. { /* sentinel */ },
  2487. };
  2488. MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
  2489. static struct platform_driver marvell_nfc_driver = {
  2490. .driver = {
  2491. .name = "marvell-nfc",
  2492. .of_match_table = marvell_nfc_of_ids,
  2493. },
  2494. .id_table = marvell_nfc_platform_ids,
  2495. .probe = marvell_nfc_probe,
  2496. .remove = marvell_nfc_remove,
  2497. };
  2498. module_platform_driver(marvell_nfc_driver);
  2499. MODULE_LICENSE("GPL");
  2500. MODULE_DESCRIPTION("Marvell NAND controller driver");