marvell_nand.c 82 KB

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