marvell_nand.c 87 KB

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