qcom_nandc.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/slab.h>
  7. #include <linux/bitops.h>
  8. #include <linux/dma/qcom_adm.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/module.h>
  12. #include <linux/mtd/rawnand.h>
  13. #include <linux/mtd/partitions.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/delay.h>
  17. #include <linux/dma/qcom_bam_dma.h>
  18. /* NANDc reg offsets */
  19. #define NAND_FLASH_CMD 0x00
  20. #define NAND_ADDR0 0x04
  21. #define NAND_ADDR1 0x08
  22. #define NAND_FLASH_CHIP_SELECT 0x0c
  23. #define NAND_EXEC_CMD 0x10
  24. #define NAND_FLASH_STATUS 0x14
  25. #define NAND_BUFFER_STATUS 0x18
  26. #define NAND_DEV0_CFG0 0x20
  27. #define NAND_DEV0_CFG1 0x24
  28. #define NAND_DEV0_ECC_CFG 0x28
  29. #define NAND_AUTO_STATUS_EN 0x2c
  30. #define NAND_DEV1_CFG0 0x30
  31. #define NAND_DEV1_CFG1 0x34
  32. #define NAND_READ_ID 0x40
  33. #define NAND_READ_STATUS 0x44
  34. #define NAND_DEV_CMD0 0xa0
  35. #define NAND_DEV_CMD1 0xa4
  36. #define NAND_DEV_CMD2 0xa8
  37. #define NAND_DEV_CMD_VLD 0xac
  38. #define SFLASHC_BURST_CFG 0xe0
  39. #define NAND_ERASED_CW_DETECT_CFG 0xe8
  40. #define NAND_ERASED_CW_DETECT_STATUS 0xec
  41. #define NAND_EBI2_ECC_BUF_CFG 0xf0
  42. #define FLASH_BUF_ACC 0x100
  43. #define NAND_CTRL 0xf00
  44. #define NAND_VERSION 0xf08
  45. #define NAND_READ_LOCATION_0 0xf20
  46. #define NAND_READ_LOCATION_1 0xf24
  47. #define NAND_READ_LOCATION_2 0xf28
  48. #define NAND_READ_LOCATION_3 0xf2c
  49. #define NAND_READ_LOCATION_LAST_CW_0 0xf40
  50. #define NAND_READ_LOCATION_LAST_CW_1 0xf44
  51. #define NAND_READ_LOCATION_LAST_CW_2 0xf48
  52. #define NAND_READ_LOCATION_LAST_CW_3 0xf4c
  53. /* dummy register offsets, used by write_reg_dma */
  54. #define NAND_DEV_CMD1_RESTORE 0xdead
  55. #define NAND_DEV_CMD_VLD_RESTORE 0xbeef
  56. /* NAND_FLASH_CMD bits */
  57. #define PAGE_ACC BIT(4)
  58. #define LAST_PAGE BIT(5)
  59. /* NAND_FLASH_CHIP_SELECT bits */
  60. #define NAND_DEV_SEL 0
  61. #define DM_EN BIT(2)
  62. /* NAND_FLASH_STATUS bits */
  63. #define FS_OP_ERR BIT(4)
  64. #define FS_READY_BSY_N BIT(5)
  65. #define FS_MPU_ERR BIT(8)
  66. #define FS_DEVICE_STS_ERR BIT(16)
  67. #define FS_DEVICE_WP BIT(23)
  68. /* NAND_BUFFER_STATUS bits */
  69. #define BS_UNCORRECTABLE_BIT BIT(8)
  70. #define BS_CORRECTABLE_ERR_MSK 0x1f
  71. /* NAND_DEVn_CFG0 bits */
  72. #define DISABLE_STATUS_AFTER_WRITE 4
  73. #define CW_PER_PAGE 6
  74. #define UD_SIZE_BYTES 9
  75. #define UD_SIZE_BYTES_MASK GENMASK(18, 9)
  76. #define ECC_PARITY_SIZE_BYTES_RS 19
  77. #define SPARE_SIZE_BYTES 23
  78. #define SPARE_SIZE_BYTES_MASK GENMASK(26, 23)
  79. #define NUM_ADDR_CYCLES 27
  80. #define STATUS_BFR_READ 30
  81. #define SET_RD_MODE_AFTER_STATUS 31
  82. /* NAND_DEVn_CFG0 bits */
  83. #define DEV0_CFG1_ECC_DISABLE 0
  84. #define WIDE_FLASH 1
  85. #define NAND_RECOVERY_CYCLES 2
  86. #define CS_ACTIVE_BSY 5
  87. #define BAD_BLOCK_BYTE_NUM 6
  88. #define BAD_BLOCK_IN_SPARE_AREA 16
  89. #define WR_RD_BSY_GAP 17
  90. #define ENABLE_BCH_ECC 27
  91. /* NAND_DEV0_ECC_CFG bits */
  92. #define ECC_CFG_ECC_DISABLE 0
  93. #define ECC_SW_RESET 1
  94. #define ECC_MODE 4
  95. #define ECC_PARITY_SIZE_BYTES_BCH 8
  96. #define ECC_NUM_DATA_BYTES 16
  97. #define ECC_NUM_DATA_BYTES_MASK GENMASK(25, 16)
  98. #define ECC_FORCE_CLK_OPEN 30
  99. /* NAND_DEV_CMD1 bits */
  100. #define READ_ADDR 0
  101. /* NAND_DEV_CMD_VLD bits */
  102. #define READ_START_VLD BIT(0)
  103. #define READ_STOP_VLD BIT(1)
  104. #define WRITE_START_VLD BIT(2)
  105. #define ERASE_START_VLD BIT(3)
  106. #define SEQ_READ_START_VLD BIT(4)
  107. /* NAND_EBI2_ECC_BUF_CFG bits */
  108. #define NUM_STEPS 0
  109. /* NAND_ERASED_CW_DETECT_CFG bits */
  110. #define ERASED_CW_ECC_MASK 1
  111. #define AUTO_DETECT_RES 0
  112. #define MASK_ECC (1 << ERASED_CW_ECC_MASK)
  113. #define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
  114. #define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
  115. #define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
  116. #define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
  117. /* NAND_ERASED_CW_DETECT_STATUS bits */
  118. #define PAGE_ALL_ERASED BIT(7)
  119. #define CODEWORD_ALL_ERASED BIT(6)
  120. #define PAGE_ERASED BIT(5)
  121. #define CODEWORD_ERASED BIT(4)
  122. #define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
  123. #define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
  124. /* NAND_READ_LOCATION_n bits */
  125. #define READ_LOCATION_OFFSET 0
  126. #define READ_LOCATION_SIZE 16
  127. #define READ_LOCATION_LAST 31
  128. /* Version Mask */
  129. #define NAND_VERSION_MAJOR_MASK 0xf0000000
  130. #define NAND_VERSION_MAJOR_SHIFT 28
  131. #define NAND_VERSION_MINOR_MASK 0x0fff0000
  132. #define NAND_VERSION_MINOR_SHIFT 16
  133. /* NAND OP_CMDs */
  134. #define OP_PAGE_READ 0x2
  135. #define OP_PAGE_READ_WITH_ECC 0x3
  136. #define OP_PAGE_READ_WITH_ECC_SPARE 0x4
  137. #define OP_PAGE_READ_ONFI_READ 0x5
  138. #define OP_PROGRAM_PAGE 0x6
  139. #define OP_PAGE_PROGRAM_WITH_ECC 0x7
  140. #define OP_PROGRAM_PAGE_SPARE 0x9
  141. #define OP_BLOCK_ERASE 0xa
  142. #define OP_FETCH_ID 0xb
  143. #define OP_RESET_DEVICE 0xd
  144. /* Default Value for NAND_DEV_CMD_VLD */
  145. #define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
  146. ERASE_START_VLD | SEQ_READ_START_VLD)
  147. /* NAND_CTRL bits */
  148. #define BAM_MODE_EN BIT(0)
  149. /*
  150. * the NAND controller performs reads/writes with ECC in 516 byte chunks.
  151. * the driver calls the chunks 'step' or 'codeword' interchangeably
  152. */
  153. #define NANDC_STEP_SIZE 512
  154. /*
  155. * the largest page size we support is 8K, this will have 16 steps/codewords
  156. * of 512 bytes each
  157. */
  158. #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
  159. /* we read at most 3 registers per codeword scan */
  160. #define MAX_REG_RD (3 * MAX_NUM_STEPS)
  161. /* ECC modes supported by the controller */
  162. #define ECC_NONE BIT(0)
  163. #define ECC_RS_4BIT BIT(1)
  164. #define ECC_BCH_4BIT BIT(2)
  165. #define ECC_BCH_8BIT BIT(3)
  166. #define nandc_set_read_loc_first(chip, reg, cw_offset, read_size, is_last_read_loc) \
  167. nandc_set_reg(chip, reg, \
  168. ((cw_offset) << READ_LOCATION_OFFSET) | \
  169. ((read_size) << READ_LOCATION_SIZE) | \
  170. ((is_last_read_loc) << READ_LOCATION_LAST))
  171. #define nandc_set_read_loc_last(chip, reg, cw_offset, read_size, is_last_read_loc) \
  172. nandc_set_reg(chip, reg, \
  173. ((cw_offset) << READ_LOCATION_OFFSET) | \
  174. ((read_size) << READ_LOCATION_SIZE) | \
  175. ((is_last_read_loc) << READ_LOCATION_LAST))
  176. /*
  177. * Returns the actual register address for all NAND_DEV_ registers
  178. * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
  179. */
  180. #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
  181. /* Returns the NAND register physical address */
  182. #define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
  183. /* Returns the dma address for reg read buffer */
  184. #define reg_buf_dma_addr(chip, vaddr) \
  185. ((chip)->reg_read_dma + \
  186. ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
  187. #define QPIC_PER_CW_CMD_ELEMENTS 32
  188. #define QPIC_PER_CW_CMD_SGL 32
  189. #define QPIC_PER_CW_DATA_SGL 8
  190. #define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
  191. /*
  192. * Flags used in DMA descriptor preparation helper functions
  193. * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
  194. */
  195. /* Don't set the EOT in current tx BAM sgl */
  196. #define NAND_BAM_NO_EOT BIT(0)
  197. /* Set the NWD flag in current BAM sgl */
  198. #define NAND_BAM_NWD BIT(1)
  199. /* Finish writing in the current BAM sgl and start writing in another BAM sgl */
  200. #define NAND_BAM_NEXT_SGL BIT(2)
  201. /*
  202. * Erased codeword status is being used two times in single transfer so this
  203. * flag will determine the current value of erased codeword status register
  204. */
  205. #define NAND_ERASED_CW_SET BIT(4)
  206. /*
  207. * This data type corresponds to the BAM transaction which will be used for all
  208. * NAND transfers.
  209. * @bam_ce - the array of BAM command elements
  210. * @cmd_sgl - sgl for NAND BAM command pipe
  211. * @data_sgl - sgl for NAND BAM consumer/producer pipe
  212. * @last_data_desc - last DMA desc in data channel (tx/rx).
  213. * @last_cmd_desc - last DMA desc in command channel.
  214. * @txn_done - completion for NAND transfer.
  215. * @bam_ce_pos - the index in bam_ce which is available for next sgl
  216. * @bam_ce_start - the index in bam_ce which marks the start position ce
  217. * for current sgl. It will be used for size calculation
  218. * for current sgl
  219. * @cmd_sgl_pos - current index in command sgl.
  220. * @cmd_sgl_start - start index in command sgl.
  221. * @tx_sgl_pos - current index in data sgl for tx.
  222. * @tx_sgl_start - start index in data sgl for tx.
  223. * @rx_sgl_pos - current index in data sgl for rx.
  224. * @rx_sgl_start - start index in data sgl for rx.
  225. * @wait_second_completion - wait for second DMA desc completion before making
  226. * the NAND transfer completion.
  227. */
  228. struct bam_transaction {
  229. struct bam_cmd_element *bam_ce;
  230. struct scatterlist *cmd_sgl;
  231. struct scatterlist *data_sgl;
  232. struct dma_async_tx_descriptor *last_data_desc;
  233. struct dma_async_tx_descriptor *last_cmd_desc;
  234. struct completion txn_done;
  235. u32 bam_ce_pos;
  236. u32 bam_ce_start;
  237. u32 cmd_sgl_pos;
  238. u32 cmd_sgl_start;
  239. u32 tx_sgl_pos;
  240. u32 tx_sgl_start;
  241. u32 rx_sgl_pos;
  242. u32 rx_sgl_start;
  243. bool wait_second_completion;
  244. };
  245. /*
  246. * This data type corresponds to the nand dma descriptor
  247. * @dma_desc - low level DMA engine descriptor
  248. * @list - list for desc_info
  249. *
  250. * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
  251. * ADM
  252. * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
  253. * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
  254. * @dir - DMA transfer direction
  255. */
  256. struct desc_info {
  257. struct dma_async_tx_descriptor *dma_desc;
  258. struct list_head node;
  259. union {
  260. struct scatterlist adm_sgl;
  261. struct {
  262. struct scatterlist *bam_sgl;
  263. int sgl_cnt;
  264. };
  265. };
  266. enum dma_data_direction dir;
  267. };
  268. /*
  269. * holds the current register values that we want to write. acts as a contiguous
  270. * chunk of memory which we use to write the controller registers through DMA.
  271. */
  272. struct nandc_regs {
  273. __le32 cmd;
  274. __le32 addr0;
  275. __le32 addr1;
  276. __le32 chip_sel;
  277. __le32 exec;
  278. __le32 cfg0;
  279. __le32 cfg1;
  280. __le32 ecc_bch_cfg;
  281. __le32 clrflashstatus;
  282. __le32 clrreadstatus;
  283. __le32 cmd1;
  284. __le32 vld;
  285. __le32 orig_cmd1;
  286. __le32 orig_vld;
  287. __le32 ecc_buf_cfg;
  288. __le32 read_location0;
  289. __le32 read_location1;
  290. __le32 read_location2;
  291. __le32 read_location3;
  292. __le32 read_location_last0;
  293. __le32 read_location_last1;
  294. __le32 read_location_last2;
  295. __le32 read_location_last3;
  296. __le32 erased_cw_detect_cfg_clr;
  297. __le32 erased_cw_detect_cfg_set;
  298. };
  299. /*
  300. * NAND controller data struct
  301. *
  302. * @dev: parent device
  303. *
  304. * @base: MMIO base
  305. *
  306. * @core_clk: controller clock
  307. * @aon_clk: another controller clock
  308. *
  309. * @regs: a contiguous chunk of memory for DMA register
  310. * writes. contains the register values to be
  311. * written to controller
  312. *
  313. * @props: properties of current NAND controller,
  314. * initialized via DT match data
  315. *
  316. * @controller: base controller structure
  317. * @host_list: list containing all the chips attached to the
  318. * controller
  319. *
  320. * @chan: dma channel
  321. * @cmd_crci: ADM DMA CRCI for command flow control
  322. * @data_crci: ADM DMA CRCI for data flow control
  323. *
  324. * @desc_list: DMA descriptor list (list of desc_infos)
  325. *
  326. * @data_buffer: our local DMA buffer for page read/writes,
  327. * used when we can't use the buffer provided
  328. * by upper layers directly
  329. * @reg_read_buf: local buffer for reading back registers via DMA
  330. *
  331. * @base_phys: physical base address of controller registers
  332. * @base_dma: dma base address of controller registers
  333. * @reg_read_dma: contains dma address for register read buffer
  334. *
  335. * @buf_size/count/start: markers for chip->legacy.read_buf/write_buf
  336. * functions
  337. * @max_cwperpage: maximum QPIC codewords required. calculated
  338. * from all connected NAND devices pagesize
  339. *
  340. * @reg_read_pos: marker for data read in reg_read_buf
  341. *
  342. * @cmd1/vld: some fixed controller register values
  343. */
  344. struct qcom_nand_controller {
  345. struct device *dev;
  346. void __iomem *base;
  347. struct clk *core_clk;
  348. struct clk *aon_clk;
  349. struct nandc_regs *regs;
  350. struct bam_transaction *bam_txn;
  351. const struct qcom_nandc_props *props;
  352. struct nand_controller controller;
  353. struct list_head host_list;
  354. union {
  355. /* will be used only by QPIC for BAM DMA */
  356. struct {
  357. struct dma_chan *tx_chan;
  358. struct dma_chan *rx_chan;
  359. struct dma_chan *cmd_chan;
  360. };
  361. /* will be used only by EBI2 for ADM DMA */
  362. struct {
  363. struct dma_chan *chan;
  364. unsigned int cmd_crci;
  365. unsigned int data_crci;
  366. };
  367. };
  368. struct list_head desc_list;
  369. u8 *data_buffer;
  370. __le32 *reg_read_buf;
  371. phys_addr_t base_phys;
  372. dma_addr_t base_dma;
  373. dma_addr_t reg_read_dma;
  374. int buf_size;
  375. int buf_count;
  376. int buf_start;
  377. unsigned int max_cwperpage;
  378. int reg_read_pos;
  379. u32 cmd1, vld;
  380. };
  381. /*
  382. * NAND special boot partitions
  383. *
  384. * @page_offset: offset of the partition where spare data is not protected
  385. * by ECC (value in pages)
  386. * @page_offset: size of the partition where spare data is not protected
  387. * by ECC (value in pages)
  388. */
  389. struct qcom_nand_boot_partition {
  390. u32 page_offset;
  391. u32 page_size;
  392. };
  393. /*
  394. * NAND chip structure
  395. *
  396. * @boot_partitions: array of boot partitions where offset and size of the
  397. * boot partitions are stored
  398. *
  399. * @chip: base NAND chip structure
  400. * @node: list node to add itself to host_list in
  401. * qcom_nand_controller
  402. *
  403. * @nr_boot_partitions: count of the boot partitions where spare data is not
  404. * protected by ECC
  405. *
  406. * @cs: chip select value for this chip
  407. * @cw_size: the number of bytes in a single step/codeword
  408. * of a page, consisting of all data, ecc, spare
  409. * and reserved bytes
  410. * @cw_data: the number of bytes within a codeword protected
  411. * by ECC
  412. * @ecc_bytes_hw: ECC bytes used by controller hardware for this
  413. * chip
  414. *
  415. * @last_command: keeps track of last command on this chip. used
  416. * for reading correct status
  417. *
  418. * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
  419. * ecc/non-ecc mode for the current nand flash
  420. * device
  421. *
  422. * @status: value to be returned if NAND_CMD_STATUS command
  423. * is executed
  424. * @codeword_fixup: keep track of the current layout used by
  425. * the driver for read/write operation.
  426. * @use_ecc: request the controller to use ECC for the
  427. * upcoming read/write
  428. * @bch_enabled: flag to tell whether BCH ECC mode is used
  429. */
  430. struct qcom_nand_host {
  431. struct qcom_nand_boot_partition *boot_partitions;
  432. struct nand_chip chip;
  433. struct list_head node;
  434. int nr_boot_partitions;
  435. int cs;
  436. int cw_size;
  437. int cw_data;
  438. int ecc_bytes_hw;
  439. int spare_bytes;
  440. int bbm_size;
  441. int last_command;
  442. u32 cfg0, cfg1;
  443. u32 cfg0_raw, cfg1_raw;
  444. u32 ecc_buf_cfg;
  445. u32 ecc_bch_cfg;
  446. u32 clrflashstatus;
  447. u32 clrreadstatus;
  448. u8 status;
  449. bool codeword_fixup;
  450. bool use_ecc;
  451. bool bch_enabled;
  452. };
  453. /*
  454. * This data type corresponds to the NAND controller properties which varies
  455. * among different NAND controllers.
  456. * @ecc_modes - ecc mode for NAND
  457. * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
  458. * @is_bam - whether NAND controller is using BAM
  459. * @is_qpic - whether NAND CTRL is part of qpic IP
  460. * @qpic_v2 - flag to indicate QPIC IP version 2
  461. * @use_codeword_fixup - whether NAND has different layout for boot partitions
  462. */
  463. struct qcom_nandc_props {
  464. u32 ecc_modes;
  465. u32 dev_cmd_reg_start;
  466. bool is_bam;
  467. bool is_qpic;
  468. bool qpic_v2;
  469. bool use_codeword_fixup;
  470. };
  471. /* Frees the BAM transaction memory */
  472. static void free_bam_transaction(struct qcom_nand_controller *nandc)
  473. {
  474. struct bam_transaction *bam_txn = nandc->bam_txn;
  475. devm_kfree(nandc->dev, bam_txn);
  476. }
  477. /* Allocates and Initializes the BAM transaction */
  478. static struct bam_transaction *
  479. alloc_bam_transaction(struct qcom_nand_controller *nandc)
  480. {
  481. struct bam_transaction *bam_txn;
  482. size_t bam_txn_size;
  483. unsigned int num_cw = nandc->max_cwperpage;
  484. void *bam_txn_buf;
  485. bam_txn_size =
  486. sizeof(*bam_txn) + num_cw *
  487. ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
  488. (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
  489. (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
  490. bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
  491. if (!bam_txn_buf)
  492. return NULL;
  493. bam_txn = bam_txn_buf;
  494. bam_txn_buf += sizeof(*bam_txn);
  495. bam_txn->bam_ce = bam_txn_buf;
  496. bam_txn_buf +=
  497. sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
  498. bam_txn->cmd_sgl = bam_txn_buf;
  499. bam_txn_buf +=
  500. sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
  501. bam_txn->data_sgl = bam_txn_buf;
  502. init_completion(&bam_txn->txn_done);
  503. return bam_txn;
  504. }
  505. /* Clears the BAM transaction indexes */
  506. static void clear_bam_transaction(struct qcom_nand_controller *nandc)
  507. {
  508. struct bam_transaction *bam_txn = nandc->bam_txn;
  509. if (!nandc->props->is_bam)
  510. return;
  511. bam_txn->bam_ce_pos = 0;
  512. bam_txn->bam_ce_start = 0;
  513. bam_txn->cmd_sgl_pos = 0;
  514. bam_txn->cmd_sgl_start = 0;
  515. bam_txn->tx_sgl_pos = 0;
  516. bam_txn->tx_sgl_start = 0;
  517. bam_txn->rx_sgl_pos = 0;
  518. bam_txn->rx_sgl_start = 0;
  519. bam_txn->last_data_desc = NULL;
  520. bam_txn->wait_second_completion = false;
  521. sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
  522. QPIC_PER_CW_CMD_SGL);
  523. sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
  524. QPIC_PER_CW_DATA_SGL);
  525. reinit_completion(&bam_txn->txn_done);
  526. }
  527. /* Callback for DMA descriptor completion */
  528. static void qpic_bam_dma_done(void *data)
  529. {
  530. struct bam_transaction *bam_txn = data;
  531. /*
  532. * In case of data transfer with NAND, 2 callbacks will be generated.
  533. * One for command channel and another one for data channel.
  534. * If current transaction has data descriptors
  535. * (i.e. wait_second_completion is true), then set this to false
  536. * and wait for second DMA descriptor completion.
  537. */
  538. if (bam_txn->wait_second_completion)
  539. bam_txn->wait_second_completion = false;
  540. else
  541. complete(&bam_txn->txn_done);
  542. }
  543. static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
  544. {
  545. return container_of(chip, struct qcom_nand_host, chip);
  546. }
  547. static inline struct qcom_nand_controller *
  548. get_qcom_nand_controller(struct nand_chip *chip)
  549. {
  550. return container_of(chip->controller, struct qcom_nand_controller,
  551. controller);
  552. }
  553. static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
  554. {
  555. return ioread32(nandc->base + offset);
  556. }
  557. static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
  558. u32 val)
  559. {
  560. iowrite32(val, nandc->base + offset);
  561. }
  562. static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
  563. bool is_cpu)
  564. {
  565. if (!nandc->props->is_bam)
  566. return;
  567. if (is_cpu)
  568. dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
  569. MAX_REG_RD *
  570. sizeof(*nandc->reg_read_buf),
  571. DMA_FROM_DEVICE);
  572. else
  573. dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
  574. MAX_REG_RD *
  575. sizeof(*nandc->reg_read_buf),
  576. DMA_FROM_DEVICE);
  577. }
  578. static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
  579. {
  580. switch (offset) {
  581. case NAND_FLASH_CMD:
  582. return &regs->cmd;
  583. case NAND_ADDR0:
  584. return &regs->addr0;
  585. case NAND_ADDR1:
  586. return &regs->addr1;
  587. case NAND_FLASH_CHIP_SELECT:
  588. return &regs->chip_sel;
  589. case NAND_EXEC_CMD:
  590. return &regs->exec;
  591. case NAND_FLASH_STATUS:
  592. return &regs->clrflashstatus;
  593. case NAND_DEV0_CFG0:
  594. return &regs->cfg0;
  595. case NAND_DEV0_CFG1:
  596. return &regs->cfg1;
  597. case NAND_DEV0_ECC_CFG:
  598. return &regs->ecc_bch_cfg;
  599. case NAND_READ_STATUS:
  600. return &regs->clrreadstatus;
  601. case NAND_DEV_CMD1:
  602. return &regs->cmd1;
  603. case NAND_DEV_CMD1_RESTORE:
  604. return &regs->orig_cmd1;
  605. case NAND_DEV_CMD_VLD:
  606. return &regs->vld;
  607. case NAND_DEV_CMD_VLD_RESTORE:
  608. return &regs->orig_vld;
  609. case NAND_EBI2_ECC_BUF_CFG:
  610. return &regs->ecc_buf_cfg;
  611. case NAND_READ_LOCATION_0:
  612. return &regs->read_location0;
  613. case NAND_READ_LOCATION_1:
  614. return &regs->read_location1;
  615. case NAND_READ_LOCATION_2:
  616. return &regs->read_location2;
  617. case NAND_READ_LOCATION_3:
  618. return &regs->read_location3;
  619. case NAND_READ_LOCATION_LAST_CW_0:
  620. return &regs->read_location_last0;
  621. case NAND_READ_LOCATION_LAST_CW_1:
  622. return &regs->read_location_last1;
  623. case NAND_READ_LOCATION_LAST_CW_2:
  624. return &regs->read_location_last2;
  625. case NAND_READ_LOCATION_LAST_CW_3:
  626. return &regs->read_location_last3;
  627. default:
  628. return NULL;
  629. }
  630. }
  631. static void nandc_set_reg(struct nand_chip *chip, int offset,
  632. u32 val)
  633. {
  634. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  635. struct nandc_regs *regs = nandc->regs;
  636. __le32 *reg;
  637. reg = offset_to_nandc_reg(regs, offset);
  638. if (reg)
  639. *reg = cpu_to_le32(val);
  640. }
  641. /* Helper to check the code word, whether it is last cw or not */
  642. static bool qcom_nandc_is_last_cw(struct nand_ecc_ctrl *ecc, int cw)
  643. {
  644. return cw == (ecc->steps - 1);
  645. }
  646. /* helper to configure location register values */
  647. static void nandc_set_read_loc(struct nand_chip *chip, int cw, int reg,
  648. int cw_offset, int read_size, int is_last_read_loc)
  649. {
  650. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  651. struct nand_ecc_ctrl *ecc = &chip->ecc;
  652. int reg_base = NAND_READ_LOCATION_0;
  653. if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
  654. reg_base = NAND_READ_LOCATION_LAST_CW_0;
  655. reg_base += reg * 4;
  656. if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
  657. return nandc_set_read_loc_last(chip, reg_base, cw_offset,
  658. read_size, is_last_read_loc);
  659. else
  660. return nandc_set_read_loc_first(chip, reg_base, cw_offset,
  661. read_size, is_last_read_loc);
  662. }
  663. /* helper to configure address register values */
  664. static void set_address(struct qcom_nand_host *host, u16 column, int page)
  665. {
  666. struct nand_chip *chip = &host->chip;
  667. if (chip->options & NAND_BUSWIDTH_16)
  668. column >>= 1;
  669. nandc_set_reg(chip, NAND_ADDR0, page << 16 | column);
  670. nandc_set_reg(chip, NAND_ADDR1, page >> 16 & 0xff);
  671. }
  672. /*
  673. * update_rw_regs: set up read/write register values, these will be
  674. * written to the NAND controller registers via DMA
  675. *
  676. * @num_cw: number of steps for the read/write operation
  677. * @read: read or write operation
  678. * @cw : which code word
  679. */
  680. static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read, int cw)
  681. {
  682. struct nand_chip *chip = &host->chip;
  683. u32 cmd, cfg0, cfg1, ecc_bch_cfg;
  684. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  685. if (read) {
  686. if (host->use_ecc)
  687. cmd = OP_PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
  688. else
  689. cmd = OP_PAGE_READ | PAGE_ACC | LAST_PAGE;
  690. } else {
  691. cmd = OP_PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
  692. }
  693. if (host->use_ecc) {
  694. cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
  695. (num_cw - 1) << CW_PER_PAGE;
  696. cfg1 = host->cfg1;
  697. ecc_bch_cfg = host->ecc_bch_cfg;
  698. } else {
  699. cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
  700. (num_cw - 1) << CW_PER_PAGE;
  701. cfg1 = host->cfg1_raw;
  702. ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
  703. }
  704. nandc_set_reg(chip, NAND_FLASH_CMD, cmd);
  705. nandc_set_reg(chip, NAND_DEV0_CFG0, cfg0);
  706. nandc_set_reg(chip, NAND_DEV0_CFG1, cfg1);
  707. nandc_set_reg(chip, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
  708. if (!nandc->props->qpic_v2)
  709. nandc_set_reg(chip, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
  710. nandc_set_reg(chip, NAND_FLASH_STATUS, host->clrflashstatus);
  711. nandc_set_reg(chip, NAND_READ_STATUS, host->clrreadstatus);
  712. nandc_set_reg(chip, NAND_EXEC_CMD, 1);
  713. if (read)
  714. nandc_set_read_loc(chip, cw, 0, 0, host->use_ecc ?
  715. host->cw_data : host->cw_size, 1);
  716. }
  717. /*
  718. * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
  719. * for BAM. This descriptor will be added in the NAND DMA descriptor queue
  720. * which will be submitted to DMA engine.
  721. */
  722. static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
  723. struct dma_chan *chan,
  724. unsigned long flags)
  725. {
  726. struct desc_info *desc;
  727. struct scatterlist *sgl;
  728. unsigned int sgl_cnt;
  729. int ret;
  730. struct bam_transaction *bam_txn = nandc->bam_txn;
  731. enum dma_transfer_direction dir_eng;
  732. struct dma_async_tx_descriptor *dma_desc;
  733. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  734. if (!desc)
  735. return -ENOMEM;
  736. if (chan == nandc->cmd_chan) {
  737. sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
  738. sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
  739. bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
  740. dir_eng = DMA_MEM_TO_DEV;
  741. desc->dir = DMA_TO_DEVICE;
  742. } else if (chan == nandc->tx_chan) {
  743. sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
  744. sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
  745. bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
  746. dir_eng = DMA_MEM_TO_DEV;
  747. desc->dir = DMA_TO_DEVICE;
  748. } else {
  749. sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
  750. sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
  751. bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
  752. dir_eng = DMA_DEV_TO_MEM;
  753. desc->dir = DMA_FROM_DEVICE;
  754. }
  755. sg_mark_end(sgl + sgl_cnt - 1);
  756. ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
  757. if (ret == 0) {
  758. dev_err(nandc->dev, "failure in mapping desc\n");
  759. kfree(desc);
  760. return -ENOMEM;
  761. }
  762. desc->sgl_cnt = sgl_cnt;
  763. desc->bam_sgl = sgl;
  764. dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
  765. flags);
  766. if (!dma_desc) {
  767. dev_err(nandc->dev, "failure in prep desc\n");
  768. dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
  769. kfree(desc);
  770. return -EINVAL;
  771. }
  772. desc->dma_desc = dma_desc;
  773. /* update last data/command descriptor */
  774. if (chan == nandc->cmd_chan)
  775. bam_txn->last_cmd_desc = dma_desc;
  776. else
  777. bam_txn->last_data_desc = dma_desc;
  778. list_add_tail(&desc->node, &nandc->desc_list);
  779. return 0;
  780. }
  781. /*
  782. * Prepares the command descriptor for BAM DMA which will be used for NAND
  783. * register reads and writes. The command descriptor requires the command
  784. * to be formed in command element type so this function uses the command
  785. * element from bam transaction ce array and fills the same with required
  786. * data. A single SGL can contain multiple command elements so
  787. * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
  788. * after the current command element.
  789. */
  790. static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read,
  791. int reg_off, const void *vaddr,
  792. int size, unsigned int flags)
  793. {
  794. int bam_ce_size;
  795. int i, ret;
  796. struct bam_cmd_element *bam_ce_buffer;
  797. struct bam_transaction *bam_txn = nandc->bam_txn;
  798. bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos];
  799. /* fill the command desc */
  800. for (i = 0; i < size; i++) {
  801. if (read)
  802. bam_prep_ce(&bam_ce_buffer[i],
  803. nandc_reg_phys(nandc, reg_off + 4 * i),
  804. BAM_READ_COMMAND,
  805. reg_buf_dma_addr(nandc,
  806. (__le32 *)vaddr + i));
  807. else
  808. bam_prep_ce_le32(&bam_ce_buffer[i],
  809. nandc_reg_phys(nandc, reg_off + 4 * i),
  810. BAM_WRITE_COMMAND,
  811. *((__le32 *)vaddr + i));
  812. }
  813. bam_txn->bam_ce_pos += size;
  814. /* use the separate sgl after this command */
  815. if (flags & NAND_BAM_NEXT_SGL) {
  816. bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start];
  817. bam_ce_size = (bam_txn->bam_ce_pos -
  818. bam_txn->bam_ce_start) *
  819. sizeof(struct bam_cmd_element);
  820. sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos],
  821. bam_ce_buffer, bam_ce_size);
  822. bam_txn->cmd_sgl_pos++;
  823. bam_txn->bam_ce_start = bam_txn->bam_ce_pos;
  824. if (flags & NAND_BAM_NWD) {
  825. ret = prepare_bam_async_desc(nandc, nandc->cmd_chan,
  826. DMA_PREP_FENCE |
  827. DMA_PREP_CMD);
  828. if (ret)
  829. return ret;
  830. }
  831. }
  832. return 0;
  833. }
  834. /*
  835. * Prepares the data descriptor for BAM DMA which will be used for NAND
  836. * data reads and writes.
  837. */
  838. static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
  839. const void *vaddr,
  840. int size, unsigned int flags)
  841. {
  842. int ret;
  843. struct bam_transaction *bam_txn = nandc->bam_txn;
  844. if (read) {
  845. sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
  846. vaddr, size);
  847. bam_txn->rx_sgl_pos++;
  848. } else {
  849. sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
  850. vaddr, size);
  851. bam_txn->tx_sgl_pos++;
  852. /*
  853. * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
  854. * is not set, form the DMA descriptor
  855. */
  856. if (!(flags & NAND_BAM_NO_EOT)) {
  857. ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
  858. DMA_PREP_INTERRUPT);
  859. if (ret)
  860. return ret;
  861. }
  862. }
  863. return 0;
  864. }
  865. static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
  866. int reg_off, const void *vaddr, int size,
  867. bool flow_control)
  868. {
  869. struct desc_info *desc;
  870. struct dma_async_tx_descriptor *dma_desc;
  871. struct scatterlist *sgl;
  872. struct dma_slave_config slave_conf;
  873. struct qcom_adm_peripheral_config periph_conf = {};
  874. enum dma_transfer_direction dir_eng;
  875. int ret;
  876. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  877. if (!desc)
  878. return -ENOMEM;
  879. sgl = &desc->adm_sgl;
  880. sg_init_one(sgl, vaddr, size);
  881. if (read) {
  882. dir_eng = DMA_DEV_TO_MEM;
  883. desc->dir = DMA_FROM_DEVICE;
  884. } else {
  885. dir_eng = DMA_MEM_TO_DEV;
  886. desc->dir = DMA_TO_DEVICE;
  887. }
  888. ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
  889. if (ret == 0) {
  890. ret = -ENOMEM;
  891. goto err;
  892. }
  893. memset(&slave_conf, 0x00, sizeof(slave_conf));
  894. slave_conf.device_fc = flow_control;
  895. if (read) {
  896. slave_conf.src_maxburst = 16;
  897. slave_conf.src_addr = nandc->base_dma + reg_off;
  898. if (nandc->data_crci) {
  899. periph_conf.crci = nandc->data_crci;
  900. slave_conf.peripheral_config = &periph_conf;
  901. slave_conf.peripheral_size = sizeof(periph_conf);
  902. }
  903. } else {
  904. slave_conf.dst_maxburst = 16;
  905. slave_conf.dst_addr = nandc->base_dma + reg_off;
  906. if (nandc->cmd_crci) {
  907. periph_conf.crci = nandc->cmd_crci;
  908. slave_conf.peripheral_config = &periph_conf;
  909. slave_conf.peripheral_size = sizeof(periph_conf);
  910. }
  911. }
  912. ret = dmaengine_slave_config(nandc->chan, &slave_conf);
  913. if (ret) {
  914. dev_err(nandc->dev, "failed to configure dma channel\n");
  915. goto err;
  916. }
  917. dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
  918. if (!dma_desc) {
  919. dev_err(nandc->dev, "failed to prepare desc\n");
  920. ret = -EINVAL;
  921. goto err;
  922. }
  923. desc->dma_desc = dma_desc;
  924. list_add_tail(&desc->node, &nandc->desc_list);
  925. return 0;
  926. err:
  927. kfree(desc);
  928. return ret;
  929. }
  930. /*
  931. * read_reg_dma: prepares a descriptor to read a given number of
  932. * contiguous registers to the reg_read_buf pointer
  933. *
  934. * @first: offset of the first register in the contiguous block
  935. * @num_regs: number of registers to read
  936. * @flags: flags to control DMA descriptor preparation
  937. */
  938. static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
  939. int num_regs, unsigned int flags)
  940. {
  941. bool flow_control = false;
  942. void *vaddr;
  943. vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
  944. nandc->reg_read_pos += num_regs;
  945. if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
  946. first = dev_cmd_reg_addr(nandc, first);
  947. if (nandc->props->is_bam)
  948. return prep_bam_dma_desc_cmd(nandc, true, first, vaddr,
  949. num_regs, flags);
  950. if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
  951. flow_control = true;
  952. return prep_adm_dma_desc(nandc, true, first, vaddr,
  953. num_regs * sizeof(u32), flow_control);
  954. }
  955. /*
  956. * write_reg_dma: prepares a descriptor to write a given number of
  957. * contiguous registers
  958. *
  959. * @first: offset of the first register in the contiguous block
  960. * @num_regs: number of registers to write
  961. * @flags: flags to control DMA descriptor preparation
  962. */
  963. static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
  964. int num_regs, unsigned int flags)
  965. {
  966. bool flow_control = false;
  967. struct nandc_regs *regs = nandc->regs;
  968. void *vaddr;
  969. vaddr = offset_to_nandc_reg(regs, first);
  970. if (first == NAND_ERASED_CW_DETECT_CFG) {
  971. if (flags & NAND_ERASED_CW_SET)
  972. vaddr = &regs->erased_cw_detect_cfg_set;
  973. else
  974. vaddr = &regs->erased_cw_detect_cfg_clr;
  975. }
  976. if (first == NAND_EXEC_CMD)
  977. flags |= NAND_BAM_NWD;
  978. if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
  979. first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
  980. if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
  981. first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
  982. if (nandc->props->is_bam)
  983. return prep_bam_dma_desc_cmd(nandc, false, first, vaddr,
  984. num_regs, flags);
  985. if (first == NAND_FLASH_CMD)
  986. flow_control = true;
  987. return prep_adm_dma_desc(nandc, false, first, vaddr,
  988. num_regs * sizeof(u32), flow_control);
  989. }
  990. /*
  991. * read_data_dma: prepares a DMA descriptor to transfer data from the
  992. * controller's internal buffer to the buffer 'vaddr'
  993. *
  994. * @reg_off: offset within the controller's data buffer
  995. * @vaddr: virtual address of the buffer we want to write to
  996. * @size: DMA transaction size in bytes
  997. * @flags: flags to control DMA descriptor preparation
  998. */
  999. static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
  1000. const u8 *vaddr, int size, unsigned int flags)
  1001. {
  1002. if (nandc->props->is_bam)
  1003. return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
  1004. return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
  1005. }
  1006. /*
  1007. * write_data_dma: prepares a DMA descriptor to transfer data from
  1008. * 'vaddr' to the controller's internal buffer
  1009. *
  1010. * @reg_off: offset within the controller's data buffer
  1011. * @vaddr: virtual address of the buffer we want to read from
  1012. * @size: DMA transaction size in bytes
  1013. * @flags: flags to control DMA descriptor preparation
  1014. */
  1015. static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
  1016. const u8 *vaddr, int size, unsigned int flags)
  1017. {
  1018. if (nandc->props->is_bam)
  1019. return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
  1020. return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
  1021. }
  1022. /*
  1023. * Helper to prepare DMA descriptors for configuring registers
  1024. * before reading a NAND page.
  1025. */
  1026. static void config_nand_page_read(struct nand_chip *chip)
  1027. {
  1028. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1029. write_reg_dma(nandc, NAND_ADDR0, 2, 0);
  1030. write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
  1031. if (!nandc->props->qpic_v2)
  1032. write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
  1033. write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
  1034. write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
  1035. NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
  1036. }
  1037. /*
  1038. * Helper to prepare DMA descriptors for configuring registers
  1039. * before reading each codeword in NAND page.
  1040. */
  1041. static void
  1042. config_nand_cw_read(struct nand_chip *chip, bool use_ecc, int cw)
  1043. {
  1044. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1045. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1046. int reg = NAND_READ_LOCATION_0;
  1047. if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
  1048. reg = NAND_READ_LOCATION_LAST_CW_0;
  1049. if (nandc->props->is_bam)
  1050. write_reg_dma(nandc, reg, 4, NAND_BAM_NEXT_SGL);
  1051. write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
  1052. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  1053. if (use_ecc) {
  1054. read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
  1055. read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
  1056. NAND_BAM_NEXT_SGL);
  1057. } else {
  1058. read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
  1059. }
  1060. }
  1061. /*
  1062. * Helper to prepare dma descriptors to configure registers needed for reading a
  1063. * single codeword in page
  1064. */
  1065. static void
  1066. config_nand_single_cw_page_read(struct nand_chip *chip,
  1067. bool use_ecc, int cw)
  1068. {
  1069. config_nand_page_read(chip);
  1070. config_nand_cw_read(chip, use_ecc, cw);
  1071. }
  1072. /*
  1073. * Helper to prepare DMA descriptors used to configure registers needed for
  1074. * before writing a NAND page.
  1075. */
  1076. static void config_nand_page_write(struct nand_chip *chip)
  1077. {
  1078. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1079. write_reg_dma(nandc, NAND_ADDR0, 2, 0);
  1080. write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
  1081. if (!nandc->props->qpic_v2)
  1082. write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
  1083. NAND_BAM_NEXT_SGL);
  1084. }
  1085. /*
  1086. * Helper to prepare DMA descriptors for configuring registers
  1087. * before writing each codeword in NAND page.
  1088. */
  1089. static void config_nand_cw_write(struct nand_chip *chip)
  1090. {
  1091. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1092. write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
  1093. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  1094. read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
  1095. write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
  1096. write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
  1097. }
  1098. /*
  1099. * the following functions are used within chip->legacy.cmdfunc() to
  1100. * perform different NAND_CMD_* commands
  1101. */
  1102. /* sets up descriptors for NAND_CMD_PARAM */
  1103. static int nandc_param(struct qcom_nand_host *host)
  1104. {
  1105. struct nand_chip *chip = &host->chip;
  1106. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1107. /*
  1108. * NAND_CMD_PARAM is called before we know much about the FLASH chip
  1109. * in use. we configure the controller to perform a raw read of 512
  1110. * bytes to read onfi params
  1111. */
  1112. if (nandc->props->qpic_v2)
  1113. nandc_set_reg(chip, NAND_FLASH_CMD, OP_PAGE_READ_ONFI_READ |
  1114. PAGE_ACC | LAST_PAGE);
  1115. else
  1116. nandc_set_reg(chip, NAND_FLASH_CMD, OP_PAGE_READ |
  1117. PAGE_ACC | LAST_PAGE);
  1118. nandc_set_reg(chip, NAND_ADDR0, 0);
  1119. nandc_set_reg(chip, NAND_ADDR1, 0);
  1120. nandc_set_reg(chip, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
  1121. | 512 << UD_SIZE_BYTES
  1122. | 5 << NUM_ADDR_CYCLES
  1123. | 0 << SPARE_SIZE_BYTES);
  1124. nandc_set_reg(chip, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
  1125. | 0 << CS_ACTIVE_BSY
  1126. | 17 << BAD_BLOCK_BYTE_NUM
  1127. | 1 << BAD_BLOCK_IN_SPARE_AREA
  1128. | 2 << WR_RD_BSY_GAP
  1129. | 0 << WIDE_FLASH
  1130. | 1 << DEV0_CFG1_ECC_DISABLE);
  1131. if (!nandc->props->qpic_v2)
  1132. nandc_set_reg(chip, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
  1133. /* configure CMD1 and VLD for ONFI param probing in QPIC v1 */
  1134. if (!nandc->props->qpic_v2) {
  1135. nandc_set_reg(chip, NAND_DEV_CMD_VLD,
  1136. (nandc->vld & ~READ_START_VLD));
  1137. nandc_set_reg(chip, NAND_DEV_CMD1,
  1138. (nandc->cmd1 & ~(0xFF << READ_ADDR))
  1139. | NAND_CMD_PARAM << READ_ADDR);
  1140. }
  1141. nandc_set_reg(chip, NAND_EXEC_CMD, 1);
  1142. if (!nandc->props->qpic_v2) {
  1143. nandc_set_reg(chip, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
  1144. nandc_set_reg(chip, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
  1145. }
  1146. nandc_set_read_loc(chip, 0, 0, 0, 512, 1);
  1147. if (!nandc->props->qpic_v2) {
  1148. write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
  1149. write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
  1150. }
  1151. nandc->buf_count = 512;
  1152. memset(nandc->data_buffer, 0xff, nandc->buf_count);
  1153. config_nand_single_cw_page_read(chip, false, 0);
  1154. read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
  1155. nandc->buf_count, 0);
  1156. /* restore CMD1 and VLD regs */
  1157. if (!nandc->props->qpic_v2) {
  1158. write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
  1159. write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
  1160. }
  1161. return 0;
  1162. }
  1163. /* sets up descriptors for NAND_CMD_ERASE1 */
  1164. static int erase_block(struct qcom_nand_host *host, int page_addr)
  1165. {
  1166. struct nand_chip *chip = &host->chip;
  1167. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1168. nandc_set_reg(chip, NAND_FLASH_CMD,
  1169. OP_BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
  1170. nandc_set_reg(chip, NAND_ADDR0, page_addr);
  1171. nandc_set_reg(chip, NAND_ADDR1, 0);
  1172. nandc_set_reg(chip, NAND_DEV0_CFG0,
  1173. host->cfg0_raw & ~(7 << CW_PER_PAGE));
  1174. nandc_set_reg(chip, NAND_DEV0_CFG1, host->cfg1_raw);
  1175. nandc_set_reg(chip, NAND_EXEC_CMD, 1);
  1176. nandc_set_reg(chip, NAND_FLASH_STATUS, host->clrflashstatus);
  1177. nandc_set_reg(chip, NAND_READ_STATUS, host->clrreadstatus);
  1178. write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
  1179. write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
  1180. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  1181. read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
  1182. write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
  1183. write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
  1184. return 0;
  1185. }
  1186. /* sets up descriptors for NAND_CMD_READID */
  1187. static int read_id(struct qcom_nand_host *host, int column)
  1188. {
  1189. struct nand_chip *chip = &host->chip;
  1190. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1191. if (column == -1)
  1192. return 0;
  1193. nandc_set_reg(chip, NAND_FLASH_CMD, OP_FETCH_ID);
  1194. nandc_set_reg(chip, NAND_ADDR0, column);
  1195. nandc_set_reg(chip, NAND_ADDR1, 0);
  1196. nandc_set_reg(chip, NAND_FLASH_CHIP_SELECT,
  1197. nandc->props->is_bam ? 0 : DM_EN);
  1198. nandc_set_reg(chip, NAND_EXEC_CMD, 1);
  1199. write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
  1200. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  1201. read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
  1202. return 0;
  1203. }
  1204. /* sets up descriptors for NAND_CMD_RESET */
  1205. static int reset(struct qcom_nand_host *host)
  1206. {
  1207. struct nand_chip *chip = &host->chip;
  1208. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1209. nandc_set_reg(chip, NAND_FLASH_CMD, OP_RESET_DEVICE);
  1210. nandc_set_reg(chip, NAND_EXEC_CMD, 1);
  1211. write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
  1212. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  1213. read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
  1214. return 0;
  1215. }
  1216. /* helpers to submit/free our list of dma descriptors */
  1217. static int submit_descs(struct qcom_nand_controller *nandc)
  1218. {
  1219. struct desc_info *desc;
  1220. dma_cookie_t cookie = 0;
  1221. struct bam_transaction *bam_txn = nandc->bam_txn;
  1222. int r;
  1223. if (nandc->props->is_bam) {
  1224. if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
  1225. r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
  1226. if (r)
  1227. return r;
  1228. }
  1229. if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
  1230. r = prepare_bam_async_desc(nandc, nandc->tx_chan,
  1231. DMA_PREP_INTERRUPT);
  1232. if (r)
  1233. return r;
  1234. }
  1235. if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
  1236. r = prepare_bam_async_desc(nandc, nandc->cmd_chan,
  1237. DMA_PREP_CMD);
  1238. if (r)
  1239. return r;
  1240. }
  1241. }
  1242. list_for_each_entry(desc, &nandc->desc_list, node)
  1243. cookie = dmaengine_submit(desc->dma_desc);
  1244. if (nandc->props->is_bam) {
  1245. bam_txn->last_cmd_desc->callback = qpic_bam_dma_done;
  1246. bam_txn->last_cmd_desc->callback_param = bam_txn;
  1247. if (bam_txn->last_data_desc) {
  1248. bam_txn->last_data_desc->callback = qpic_bam_dma_done;
  1249. bam_txn->last_data_desc->callback_param = bam_txn;
  1250. bam_txn->wait_second_completion = true;
  1251. }
  1252. dma_async_issue_pending(nandc->tx_chan);
  1253. dma_async_issue_pending(nandc->rx_chan);
  1254. dma_async_issue_pending(nandc->cmd_chan);
  1255. if (!wait_for_completion_timeout(&bam_txn->txn_done,
  1256. QPIC_NAND_COMPLETION_TIMEOUT))
  1257. return -ETIMEDOUT;
  1258. } else {
  1259. if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
  1260. return -ETIMEDOUT;
  1261. }
  1262. return 0;
  1263. }
  1264. static void free_descs(struct qcom_nand_controller *nandc)
  1265. {
  1266. struct desc_info *desc, *n;
  1267. list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
  1268. list_del(&desc->node);
  1269. if (nandc->props->is_bam)
  1270. dma_unmap_sg(nandc->dev, desc->bam_sgl,
  1271. desc->sgl_cnt, desc->dir);
  1272. else
  1273. dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
  1274. desc->dir);
  1275. kfree(desc);
  1276. }
  1277. }
  1278. /* reset the register read buffer for next NAND operation */
  1279. static void clear_read_regs(struct qcom_nand_controller *nandc)
  1280. {
  1281. nandc->reg_read_pos = 0;
  1282. nandc_read_buffer_sync(nandc, false);
  1283. }
  1284. static void pre_command(struct qcom_nand_host *host, int command)
  1285. {
  1286. struct nand_chip *chip = &host->chip;
  1287. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1288. nandc->buf_count = 0;
  1289. nandc->buf_start = 0;
  1290. host->use_ecc = false;
  1291. host->last_command = command;
  1292. clear_read_regs(nandc);
  1293. if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
  1294. command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
  1295. clear_bam_transaction(nandc);
  1296. }
  1297. /*
  1298. * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
  1299. * privately maintained status byte, this status byte can be read after
  1300. * NAND_CMD_STATUS is called
  1301. */
  1302. static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
  1303. {
  1304. struct nand_chip *chip = &host->chip;
  1305. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1306. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1307. int num_cw;
  1308. int i;
  1309. num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
  1310. nandc_read_buffer_sync(nandc, true);
  1311. for (i = 0; i < num_cw; i++) {
  1312. u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
  1313. if (flash_status & FS_MPU_ERR)
  1314. host->status &= ~NAND_STATUS_WP;
  1315. if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
  1316. (flash_status &
  1317. FS_DEVICE_STS_ERR)))
  1318. host->status |= NAND_STATUS_FAIL;
  1319. }
  1320. }
  1321. static void post_command(struct qcom_nand_host *host, int command)
  1322. {
  1323. struct nand_chip *chip = &host->chip;
  1324. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1325. switch (command) {
  1326. case NAND_CMD_READID:
  1327. nandc_read_buffer_sync(nandc, true);
  1328. memcpy(nandc->data_buffer, nandc->reg_read_buf,
  1329. nandc->buf_count);
  1330. break;
  1331. case NAND_CMD_PAGEPROG:
  1332. case NAND_CMD_ERASE1:
  1333. parse_erase_write_errors(host, command);
  1334. break;
  1335. default:
  1336. break;
  1337. }
  1338. }
  1339. /*
  1340. * Implements chip->legacy.cmdfunc. It's only used for a limited set of
  1341. * commands. The rest of the commands wouldn't be called by upper layers.
  1342. * For example, NAND_CMD_READOOB would never be called because we have our own
  1343. * versions of read_oob ops for nand_ecc_ctrl.
  1344. */
  1345. static void qcom_nandc_command(struct nand_chip *chip, unsigned int command,
  1346. int column, int page_addr)
  1347. {
  1348. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1349. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1350. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1351. bool wait = false;
  1352. int ret = 0;
  1353. pre_command(host, command);
  1354. switch (command) {
  1355. case NAND_CMD_RESET:
  1356. ret = reset(host);
  1357. wait = true;
  1358. break;
  1359. case NAND_CMD_READID:
  1360. nandc->buf_count = 4;
  1361. ret = read_id(host, column);
  1362. wait = true;
  1363. break;
  1364. case NAND_CMD_PARAM:
  1365. ret = nandc_param(host);
  1366. wait = true;
  1367. break;
  1368. case NAND_CMD_ERASE1:
  1369. ret = erase_block(host, page_addr);
  1370. wait = true;
  1371. break;
  1372. case NAND_CMD_READ0:
  1373. /* we read the entire page for now */
  1374. WARN_ON(column != 0);
  1375. host->use_ecc = true;
  1376. set_address(host, 0, page_addr);
  1377. update_rw_regs(host, ecc->steps, true, 0);
  1378. break;
  1379. case NAND_CMD_SEQIN:
  1380. WARN_ON(column != 0);
  1381. set_address(host, 0, page_addr);
  1382. break;
  1383. case NAND_CMD_PAGEPROG:
  1384. case NAND_CMD_STATUS:
  1385. case NAND_CMD_NONE:
  1386. default:
  1387. break;
  1388. }
  1389. if (ret) {
  1390. dev_err(nandc->dev, "failure executing command %d\n",
  1391. command);
  1392. free_descs(nandc);
  1393. return;
  1394. }
  1395. if (wait) {
  1396. ret = submit_descs(nandc);
  1397. if (ret)
  1398. dev_err(nandc->dev,
  1399. "failure submitting descs for command %d\n",
  1400. command);
  1401. }
  1402. free_descs(nandc);
  1403. post_command(host, command);
  1404. }
  1405. /*
  1406. * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
  1407. * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
  1408. *
  1409. * when using RS ECC, the HW reports the same erros when reading an erased CW,
  1410. * but it notifies that it is an erased CW by placing special characters at
  1411. * certain offsets in the buffer.
  1412. *
  1413. * verify if the page is erased or not, and fix up the page for RS ECC by
  1414. * replacing the special characters with 0xff.
  1415. */
  1416. static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
  1417. {
  1418. u8 empty1, empty2;
  1419. /*
  1420. * an erased page flags an error in NAND_FLASH_STATUS, check if the page
  1421. * is erased by looking for 0x54s at offsets 3 and 175 from the
  1422. * beginning of each codeword
  1423. */
  1424. empty1 = data_buf[3];
  1425. empty2 = data_buf[175];
  1426. /*
  1427. * if the erased codework markers, if they exist override them with
  1428. * 0xffs
  1429. */
  1430. if ((empty1 == 0x54 && empty2 == 0xff) ||
  1431. (empty1 == 0xff && empty2 == 0x54)) {
  1432. data_buf[3] = 0xff;
  1433. data_buf[175] = 0xff;
  1434. }
  1435. /*
  1436. * check if the entire chunk contains 0xffs or not. if it doesn't, then
  1437. * restore the original values at the special offsets
  1438. */
  1439. if (memchr_inv(data_buf, 0xff, data_len)) {
  1440. data_buf[3] = empty1;
  1441. data_buf[175] = empty2;
  1442. return false;
  1443. }
  1444. return true;
  1445. }
  1446. struct read_stats {
  1447. __le32 flash;
  1448. __le32 buffer;
  1449. __le32 erased_cw;
  1450. };
  1451. /* reads back FLASH_STATUS register set by the controller */
  1452. static int check_flash_errors(struct qcom_nand_host *host, int cw_cnt)
  1453. {
  1454. struct nand_chip *chip = &host->chip;
  1455. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1456. int i;
  1457. nandc_read_buffer_sync(nandc, true);
  1458. for (i = 0; i < cw_cnt; i++) {
  1459. u32 flash = le32_to_cpu(nandc->reg_read_buf[i]);
  1460. if (flash & (FS_OP_ERR | FS_MPU_ERR))
  1461. return -EIO;
  1462. }
  1463. return 0;
  1464. }
  1465. /* performs raw read for one codeword */
  1466. static int
  1467. qcom_nandc_read_cw_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1468. u8 *data_buf, u8 *oob_buf, int page, int cw)
  1469. {
  1470. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1471. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1472. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1473. int data_size1, data_size2, oob_size1, oob_size2;
  1474. int ret, reg_off = FLASH_BUF_ACC, read_loc = 0;
  1475. int raw_cw = cw;
  1476. nand_read_page_op(chip, page, 0, NULL, 0);
  1477. host->use_ecc = false;
  1478. if (nandc->props->qpic_v2)
  1479. raw_cw = ecc->steps - 1;
  1480. clear_bam_transaction(nandc);
  1481. set_address(host, host->cw_size * cw, page);
  1482. update_rw_regs(host, 1, true, raw_cw);
  1483. config_nand_page_read(chip);
  1484. data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
  1485. oob_size1 = host->bbm_size;
  1486. if (qcom_nandc_is_last_cw(ecc, cw) && !host->codeword_fixup) {
  1487. data_size2 = ecc->size - data_size1 -
  1488. ((ecc->steps - 1) * 4);
  1489. oob_size2 = (ecc->steps * 4) + host->ecc_bytes_hw +
  1490. host->spare_bytes;
  1491. } else {
  1492. data_size2 = host->cw_data - data_size1;
  1493. oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
  1494. }
  1495. if (nandc->props->is_bam) {
  1496. nandc_set_read_loc(chip, cw, 0, read_loc, data_size1, 0);
  1497. read_loc += data_size1;
  1498. nandc_set_read_loc(chip, cw, 1, read_loc, oob_size1, 0);
  1499. read_loc += oob_size1;
  1500. nandc_set_read_loc(chip, cw, 2, read_loc, data_size2, 0);
  1501. read_loc += data_size2;
  1502. nandc_set_read_loc(chip, cw, 3, read_loc, oob_size2, 1);
  1503. }
  1504. config_nand_cw_read(chip, false, raw_cw);
  1505. read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
  1506. reg_off += data_size1;
  1507. read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
  1508. reg_off += oob_size1;
  1509. read_data_dma(nandc, reg_off, data_buf + data_size1, data_size2, 0);
  1510. reg_off += data_size2;
  1511. read_data_dma(nandc, reg_off, oob_buf + oob_size1, oob_size2, 0);
  1512. ret = submit_descs(nandc);
  1513. free_descs(nandc);
  1514. if (ret) {
  1515. dev_err(nandc->dev, "failure to read raw cw %d\n", cw);
  1516. return ret;
  1517. }
  1518. return check_flash_errors(host, 1);
  1519. }
  1520. /*
  1521. * Bitflips can happen in erased codewords also so this function counts the
  1522. * number of 0 in each CW for which ECC engine returns the uncorrectable
  1523. * error. The page will be assumed as erased if this count is less than or
  1524. * equal to the ecc->strength for each CW.
  1525. *
  1526. * 1. Both DATA and OOB need to be checked for number of 0. The
  1527. * top-level API can be called with only data buf or OOB buf so use
  1528. * chip->data_buf if data buf is null and chip->oob_poi if oob buf
  1529. * is null for copying the raw bytes.
  1530. * 2. Perform raw read for all the CW which has uncorrectable errors.
  1531. * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes.
  1532. * The BBM and spare bytes bit flip won’t affect the ECC so don’t check
  1533. * the number of bitflips in this area.
  1534. */
  1535. static int
  1536. check_for_erased_page(struct qcom_nand_host *host, u8 *data_buf,
  1537. u8 *oob_buf, unsigned long uncorrectable_cws,
  1538. int page, unsigned int max_bitflips)
  1539. {
  1540. struct nand_chip *chip = &host->chip;
  1541. struct mtd_info *mtd = nand_to_mtd(chip);
  1542. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1543. u8 *cw_data_buf, *cw_oob_buf;
  1544. int cw, data_size, oob_size, ret = 0;
  1545. if (!data_buf)
  1546. data_buf = nand_get_data_buf(chip);
  1547. if (!oob_buf) {
  1548. nand_get_data_buf(chip);
  1549. oob_buf = chip->oob_poi;
  1550. }
  1551. for_each_set_bit(cw, &uncorrectable_cws, ecc->steps) {
  1552. if (qcom_nandc_is_last_cw(ecc, cw) && !host->codeword_fixup) {
  1553. data_size = ecc->size - ((ecc->steps - 1) * 4);
  1554. oob_size = (ecc->steps * 4) + host->ecc_bytes_hw;
  1555. } else {
  1556. data_size = host->cw_data;
  1557. oob_size = host->ecc_bytes_hw;
  1558. }
  1559. /* determine starting buffer address for current CW */
  1560. cw_data_buf = data_buf + (cw * host->cw_data);
  1561. cw_oob_buf = oob_buf + (cw * ecc->bytes);
  1562. ret = qcom_nandc_read_cw_raw(mtd, chip, cw_data_buf,
  1563. cw_oob_buf, page, cw);
  1564. if (ret)
  1565. return ret;
  1566. /*
  1567. * make sure it isn't an erased page reported
  1568. * as not-erased by HW because of a few bitflips
  1569. */
  1570. ret = nand_check_erased_ecc_chunk(cw_data_buf, data_size,
  1571. cw_oob_buf + host->bbm_size,
  1572. oob_size, NULL,
  1573. 0, ecc->strength);
  1574. if (ret < 0) {
  1575. mtd->ecc_stats.failed++;
  1576. } else {
  1577. mtd->ecc_stats.corrected += ret;
  1578. max_bitflips = max_t(unsigned int, max_bitflips, ret);
  1579. }
  1580. }
  1581. return max_bitflips;
  1582. }
  1583. /*
  1584. * reads back status registers set by the controller to notify page read
  1585. * errors. this is equivalent to what 'ecc->correct()' would do.
  1586. */
  1587. static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
  1588. u8 *oob_buf, int page)
  1589. {
  1590. struct nand_chip *chip = &host->chip;
  1591. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1592. struct mtd_info *mtd = nand_to_mtd(chip);
  1593. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1594. unsigned int max_bitflips = 0, uncorrectable_cws = 0;
  1595. struct read_stats *buf;
  1596. bool flash_op_err = false, erased;
  1597. int i;
  1598. u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
  1599. buf = (struct read_stats *)nandc->reg_read_buf;
  1600. nandc_read_buffer_sync(nandc, true);
  1601. for (i = 0; i < ecc->steps; i++, buf++) {
  1602. u32 flash, buffer, erased_cw;
  1603. int data_len, oob_len;
  1604. if (qcom_nandc_is_last_cw(ecc, i)) {
  1605. data_len = ecc->size - ((ecc->steps - 1) << 2);
  1606. oob_len = ecc->steps << 2;
  1607. } else {
  1608. data_len = host->cw_data;
  1609. oob_len = 0;
  1610. }
  1611. flash = le32_to_cpu(buf->flash);
  1612. buffer = le32_to_cpu(buf->buffer);
  1613. erased_cw = le32_to_cpu(buf->erased_cw);
  1614. /*
  1615. * Check ECC failure for each codeword. ECC failure can
  1616. * happen in either of the following conditions
  1617. * 1. If number of bitflips are greater than ECC engine
  1618. * capability.
  1619. * 2. If this codeword contains all 0xff for which erased
  1620. * codeword detection check will be done.
  1621. */
  1622. if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
  1623. /*
  1624. * For BCH ECC, ignore erased codeword errors, if
  1625. * ERASED_CW bits are set.
  1626. */
  1627. if (host->bch_enabled) {
  1628. erased = (erased_cw & ERASED_CW) == ERASED_CW;
  1629. /*
  1630. * For RS ECC, HW reports the erased CW by placing
  1631. * special characters at certain offsets in the buffer.
  1632. * These special characters will be valid only if
  1633. * complete page is read i.e. data_buf is not NULL.
  1634. */
  1635. } else if (data_buf) {
  1636. erased = erased_chunk_check_and_fixup(data_buf,
  1637. data_len);
  1638. } else {
  1639. erased = false;
  1640. }
  1641. if (!erased)
  1642. uncorrectable_cws |= BIT(i);
  1643. /*
  1644. * Check if MPU or any other operational error (timeout,
  1645. * device failure, etc.) happened for this codeword and
  1646. * make flash_op_err true. If flash_op_err is set, then
  1647. * EIO will be returned for page read.
  1648. */
  1649. } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
  1650. flash_op_err = true;
  1651. /*
  1652. * No ECC or operational errors happened. Check the number of
  1653. * bits corrected and update the ecc_stats.corrected.
  1654. */
  1655. } else {
  1656. unsigned int stat;
  1657. stat = buffer & BS_CORRECTABLE_ERR_MSK;
  1658. mtd->ecc_stats.corrected += stat;
  1659. max_bitflips = max(max_bitflips, stat);
  1660. }
  1661. if (data_buf)
  1662. data_buf += data_len;
  1663. if (oob_buf)
  1664. oob_buf += oob_len + ecc->bytes;
  1665. }
  1666. if (flash_op_err)
  1667. return -EIO;
  1668. if (!uncorrectable_cws)
  1669. return max_bitflips;
  1670. return check_for_erased_page(host, data_buf_start, oob_buf_start,
  1671. uncorrectable_cws, page,
  1672. max_bitflips);
  1673. }
  1674. /*
  1675. * helper to perform the actual page read operation, used by ecc->read_page(),
  1676. * ecc->read_oob()
  1677. */
  1678. static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
  1679. u8 *oob_buf, int page)
  1680. {
  1681. struct nand_chip *chip = &host->chip;
  1682. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1683. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1684. u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
  1685. int i, ret;
  1686. config_nand_page_read(chip);
  1687. /* queue cmd descs for each codeword */
  1688. for (i = 0; i < ecc->steps; i++) {
  1689. int data_size, oob_size;
  1690. if (qcom_nandc_is_last_cw(ecc, i) && !host->codeword_fixup) {
  1691. data_size = ecc->size - ((ecc->steps - 1) << 2);
  1692. oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
  1693. host->spare_bytes;
  1694. } else {
  1695. data_size = host->cw_data;
  1696. oob_size = host->ecc_bytes_hw + host->spare_bytes;
  1697. }
  1698. if (nandc->props->is_bam) {
  1699. if (data_buf && oob_buf) {
  1700. nandc_set_read_loc(chip, i, 0, 0, data_size, 0);
  1701. nandc_set_read_loc(chip, i, 1, data_size,
  1702. oob_size, 1);
  1703. } else if (data_buf) {
  1704. nandc_set_read_loc(chip, i, 0, 0, data_size, 1);
  1705. } else {
  1706. nandc_set_read_loc(chip, i, 0, data_size,
  1707. oob_size, 1);
  1708. }
  1709. }
  1710. config_nand_cw_read(chip, true, i);
  1711. if (data_buf)
  1712. read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
  1713. data_size, 0);
  1714. /*
  1715. * when ecc is enabled, the controller doesn't read the real
  1716. * or dummy bad block markers in each chunk. To maintain a
  1717. * consistent layout across RAW and ECC reads, we just
  1718. * leave the real/dummy BBM offsets empty (i.e, filled with
  1719. * 0xffs)
  1720. */
  1721. if (oob_buf) {
  1722. int j;
  1723. for (j = 0; j < host->bbm_size; j++)
  1724. *oob_buf++ = 0xff;
  1725. read_data_dma(nandc, FLASH_BUF_ACC + data_size,
  1726. oob_buf, oob_size, 0);
  1727. }
  1728. if (data_buf)
  1729. data_buf += data_size;
  1730. if (oob_buf)
  1731. oob_buf += oob_size;
  1732. }
  1733. ret = submit_descs(nandc);
  1734. free_descs(nandc);
  1735. if (ret) {
  1736. dev_err(nandc->dev, "failure to read page/oob\n");
  1737. return ret;
  1738. }
  1739. return parse_read_errors(host, data_buf_start, oob_buf_start, page);
  1740. }
  1741. /*
  1742. * a helper that copies the last step/codeword of a page (containing free oob)
  1743. * into our local buffer
  1744. */
  1745. static int copy_last_cw(struct qcom_nand_host *host, int page)
  1746. {
  1747. struct nand_chip *chip = &host->chip;
  1748. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1749. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1750. int size;
  1751. int ret;
  1752. clear_read_regs(nandc);
  1753. size = host->use_ecc ? host->cw_data : host->cw_size;
  1754. /* prepare a clean read buffer */
  1755. memset(nandc->data_buffer, 0xff, size);
  1756. set_address(host, host->cw_size * (ecc->steps - 1), page);
  1757. update_rw_regs(host, 1, true, ecc->steps - 1);
  1758. config_nand_single_cw_page_read(chip, host->use_ecc, ecc->steps - 1);
  1759. read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
  1760. ret = submit_descs(nandc);
  1761. if (ret)
  1762. dev_err(nandc->dev, "failed to copy last codeword\n");
  1763. free_descs(nandc);
  1764. return ret;
  1765. }
  1766. static bool qcom_nandc_is_boot_partition(struct qcom_nand_host *host, int page)
  1767. {
  1768. struct qcom_nand_boot_partition *boot_partition;
  1769. u32 start, end;
  1770. int i;
  1771. /*
  1772. * Since the frequent access will be to the non-boot partitions like rootfs,
  1773. * optimize the page check by:
  1774. *
  1775. * 1. Checking if the page lies after the last boot partition.
  1776. * 2. Checking from the boot partition end.
  1777. */
  1778. /* First check the last boot partition */
  1779. boot_partition = &host->boot_partitions[host->nr_boot_partitions - 1];
  1780. start = boot_partition->page_offset;
  1781. end = start + boot_partition->page_size;
  1782. /* Page is after the last boot partition end. This is NOT a boot partition */
  1783. if (page > end)
  1784. return false;
  1785. /* Actually check if it's a boot partition */
  1786. if (page < end && page >= start)
  1787. return true;
  1788. /* Check the other boot partitions starting from the second-last partition */
  1789. for (i = host->nr_boot_partitions - 2; i >= 0; i--) {
  1790. boot_partition = &host->boot_partitions[i];
  1791. start = boot_partition->page_offset;
  1792. end = start + boot_partition->page_size;
  1793. if (page < end && page >= start)
  1794. return true;
  1795. }
  1796. return false;
  1797. }
  1798. static void qcom_nandc_codeword_fixup(struct qcom_nand_host *host, int page)
  1799. {
  1800. bool codeword_fixup = qcom_nandc_is_boot_partition(host, page);
  1801. /* Skip conf write if we are already in the correct mode */
  1802. if (codeword_fixup == host->codeword_fixup)
  1803. return;
  1804. host->codeword_fixup = codeword_fixup;
  1805. host->cw_data = codeword_fixup ? 512 : 516;
  1806. host->spare_bytes = host->cw_size - host->ecc_bytes_hw -
  1807. host->bbm_size - host->cw_data;
  1808. host->cfg0 &= ~(SPARE_SIZE_BYTES_MASK | UD_SIZE_BYTES_MASK);
  1809. host->cfg0 |= host->spare_bytes << SPARE_SIZE_BYTES |
  1810. host->cw_data << UD_SIZE_BYTES;
  1811. host->ecc_bch_cfg &= ~ECC_NUM_DATA_BYTES_MASK;
  1812. host->ecc_bch_cfg |= host->cw_data << ECC_NUM_DATA_BYTES;
  1813. host->ecc_buf_cfg = (host->cw_data - 1) << NUM_STEPS;
  1814. }
  1815. /* implements ecc->read_page() */
  1816. static int qcom_nandc_read_page(struct nand_chip *chip, uint8_t *buf,
  1817. int oob_required, int page)
  1818. {
  1819. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1820. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1821. u8 *data_buf, *oob_buf = NULL;
  1822. if (host->nr_boot_partitions)
  1823. qcom_nandc_codeword_fixup(host, page);
  1824. nand_read_page_op(chip, page, 0, NULL, 0);
  1825. data_buf = buf;
  1826. oob_buf = oob_required ? chip->oob_poi : NULL;
  1827. clear_bam_transaction(nandc);
  1828. return read_page_ecc(host, data_buf, oob_buf, page);
  1829. }
  1830. /* implements ecc->read_page_raw() */
  1831. static int qcom_nandc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
  1832. int oob_required, int page)
  1833. {
  1834. struct mtd_info *mtd = nand_to_mtd(chip);
  1835. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1836. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1837. int cw, ret;
  1838. u8 *data_buf = buf, *oob_buf = chip->oob_poi;
  1839. if (host->nr_boot_partitions)
  1840. qcom_nandc_codeword_fixup(host, page);
  1841. for (cw = 0; cw < ecc->steps; cw++) {
  1842. ret = qcom_nandc_read_cw_raw(mtd, chip, data_buf, oob_buf,
  1843. page, cw);
  1844. if (ret)
  1845. return ret;
  1846. data_buf += host->cw_data;
  1847. oob_buf += ecc->bytes;
  1848. }
  1849. return 0;
  1850. }
  1851. /* implements ecc->read_oob() */
  1852. static int qcom_nandc_read_oob(struct nand_chip *chip, int page)
  1853. {
  1854. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1855. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1856. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1857. if (host->nr_boot_partitions)
  1858. qcom_nandc_codeword_fixup(host, page);
  1859. clear_read_regs(nandc);
  1860. clear_bam_transaction(nandc);
  1861. host->use_ecc = true;
  1862. set_address(host, 0, page);
  1863. update_rw_regs(host, ecc->steps, true, 0);
  1864. return read_page_ecc(host, NULL, chip->oob_poi, page);
  1865. }
  1866. /* implements ecc->write_page() */
  1867. static int qcom_nandc_write_page(struct nand_chip *chip, const uint8_t *buf,
  1868. int oob_required, int page)
  1869. {
  1870. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1871. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1872. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1873. u8 *data_buf, *oob_buf;
  1874. int i, ret;
  1875. if (host->nr_boot_partitions)
  1876. qcom_nandc_codeword_fixup(host, page);
  1877. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1878. clear_read_regs(nandc);
  1879. clear_bam_transaction(nandc);
  1880. data_buf = (u8 *)buf;
  1881. oob_buf = chip->oob_poi;
  1882. host->use_ecc = true;
  1883. update_rw_regs(host, ecc->steps, false, 0);
  1884. config_nand_page_write(chip);
  1885. for (i = 0; i < ecc->steps; i++) {
  1886. int data_size, oob_size;
  1887. if (qcom_nandc_is_last_cw(ecc, i) && !host->codeword_fixup) {
  1888. data_size = ecc->size - ((ecc->steps - 1) << 2);
  1889. oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
  1890. host->spare_bytes;
  1891. } else {
  1892. data_size = host->cw_data;
  1893. oob_size = ecc->bytes;
  1894. }
  1895. write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
  1896. i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
  1897. /*
  1898. * when ECC is enabled, we don't really need to write anything
  1899. * to oob for the first n - 1 codewords since these oob regions
  1900. * just contain ECC bytes that's written by the controller
  1901. * itself. For the last codeword, we skip the bbm positions and
  1902. * write to the free oob area.
  1903. */
  1904. if (qcom_nandc_is_last_cw(ecc, i)) {
  1905. oob_buf += host->bbm_size;
  1906. write_data_dma(nandc, FLASH_BUF_ACC + data_size,
  1907. oob_buf, oob_size, 0);
  1908. }
  1909. config_nand_cw_write(chip);
  1910. data_buf += data_size;
  1911. oob_buf += oob_size;
  1912. }
  1913. ret = submit_descs(nandc);
  1914. if (ret)
  1915. dev_err(nandc->dev, "failure to write page\n");
  1916. free_descs(nandc);
  1917. if (!ret)
  1918. ret = nand_prog_page_end_op(chip);
  1919. return ret;
  1920. }
  1921. /* implements ecc->write_page_raw() */
  1922. static int qcom_nandc_write_page_raw(struct nand_chip *chip,
  1923. const uint8_t *buf, int oob_required,
  1924. int page)
  1925. {
  1926. struct mtd_info *mtd = nand_to_mtd(chip);
  1927. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1928. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1929. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1930. u8 *data_buf, *oob_buf;
  1931. int i, ret;
  1932. if (host->nr_boot_partitions)
  1933. qcom_nandc_codeword_fixup(host, page);
  1934. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1935. clear_read_regs(nandc);
  1936. clear_bam_transaction(nandc);
  1937. data_buf = (u8 *)buf;
  1938. oob_buf = chip->oob_poi;
  1939. host->use_ecc = false;
  1940. update_rw_regs(host, ecc->steps, false, 0);
  1941. config_nand_page_write(chip);
  1942. for (i = 0; i < ecc->steps; i++) {
  1943. int data_size1, data_size2, oob_size1, oob_size2;
  1944. int reg_off = FLASH_BUF_ACC;
  1945. data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
  1946. oob_size1 = host->bbm_size;
  1947. if (qcom_nandc_is_last_cw(ecc, i) && !host->codeword_fixup) {
  1948. data_size2 = ecc->size - data_size1 -
  1949. ((ecc->steps - 1) << 2);
  1950. oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
  1951. host->spare_bytes;
  1952. } else {
  1953. data_size2 = host->cw_data - data_size1;
  1954. oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
  1955. }
  1956. write_data_dma(nandc, reg_off, data_buf, data_size1,
  1957. NAND_BAM_NO_EOT);
  1958. reg_off += data_size1;
  1959. data_buf += data_size1;
  1960. write_data_dma(nandc, reg_off, oob_buf, oob_size1,
  1961. NAND_BAM_NO_EOT);
  1962. reg_off += oob_size1;
  1963. oob_buf += oob_size1;
  1964. write_data_dma(nandc, reg_off, data_buf, data_size2,
  1965. NAND_BAM_NO_EOT);
  1966. reg_off += data_size2;
  1967. data_buf += data_size2;
  1968. write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
  1969. oob_buf += oob_size2;
  1970. config_nand_cw_write(chip);
  1971. }
  1972. ret = submit_descs(nandc);
  1973. if (ret)
  1974. dev_err(nandc->dev, "failure to write raw page\n");
  1975. free_descs(nandc);
  1976. if (!ret)
  1977. ret = nand_prog_page_end_op(chip);
  1978. return ret;
  1979. }
  1980. /*
  1981. * implements ecc->write_oob()
  1982. *
  1983. * the NAND controller cannot write only data or only OOB within a codeword
  1984. * since ECC is calculated for the combined codeword. So update the OOB from
  1985. * chip->oob_poi, and pad the data area with OxFF before writing.
  1986. */
  1987. static int qcom_nandc_write_oob(struct nand_chip *chip, int page)
  1988. {
  1989. struct mtd_info *mtd = nand_to_mtd(chip);
  1990. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1991. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1992. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1993. u8 *oob = chip->oob_poi;
  1994. int data_size, oob_size;
  1995. int ret;
  1996. if (host->nr_boot_partitions)
  1997. qcom_nandc_codeword_fixup(host, page);
  1998. host->use_ecc = true;
  1999. clear_bam_transaction(nandc);
  2000. /* calculate the data and oob size for the last codeword/step */
  2001. data_size = ecc->size - ((ecc->steps - 1) << 2);
  2002. oob_size = mtd->oobavail;
  2003. memset(nandc->data_buffer, 0xff, host->cw_data);
  2004. /* override new oob content to last codeword */
  2005. mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
  2006. 0, mtd->oobavail);
  2007. set_address(host, host->cw_size * (ecc->steps - 1), page);
  2008. update_rw_regs(host, 1, false, 0);
  2009. config_nand_page_write(chip);
  2010. write_data_dma(nandc, FLASH_BUF_ACC,
  2011. nandc->data_buffer, data_size + oob_size, 0);
  2012. config_nand_cw_write(chip);
  2013. ret = submit_descs(nandc);
  2014. free_descs(nandc);
  2015. if (ret) {
  2016. dev_err(nandc->dev, "failure to write oob\n");
  2017. return -EIO;
  2018. }
  2019. return nand_prog_page_end_op(chip);
  2020. }
  2021. static int qcom_nandc_block_bad(struct nand_chip *chip, loff_t ofs)
  2022. {
  2023. struct mtd_info *mtd = nand_to_mtd(chip);
  2024. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  2025. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  2026. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2027. int page, ret, bbpos, bad = 0;
  2028. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  2029. /*
  2030. * configure registers for a raw sub page read, the address is set to
  2031. * the beginning of the last codeword, we don't care about reading ecc
  2032. * portion of oob. we just want the first few bytes from this codeword
  2033. * that contains the BBM
  2034. */
  2035. host->use_ecc = false;
  2036. clear_bam_transaction(nandc);
  2037. ret = copy_last_cw(host, page);
  2038. if (ret)
  2039. goto err;
  2040. if (check_flash_errors(host, 1)) {
  2041. dev_warn(nandc->dev, "error when trying to read BBM\n");
  2042. goto err;
  2043. }
  2044. bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
  2045. bad = nandc->data_buffer[bbpos] != 0xff;
  2046. if (chip->options & NAND_BUSWIDTH_16)
  2047. bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
  2048. err:
  2049. return bad;
  2050. }
  2051. static int qcom_nandc_block_markbad(struct nand_chip *chip, loff_t ofs)
  2052. {
  2053. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  2054. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  2055. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2056. int page, ret;
  2057. clear_read_regs(nandc);
  2058. clear_bam_transaction(nandc);
  2059. /*
  2060. * to mark the BBM as bad, we flash the entire last codeword with 0s.
  2061. * we don't care about the rest of the content in the codeword since
  2062. * we aren't going to use this block again
  2063. */
  2064. memset(nandc->data_buffer, 0x00, host->cw_size);
  2065. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  2066. /* prepare write */
  2067. host->use_ecc = false;
  2068. set_address(host, host->cw_size * (ecc->steps - 1), page);
  2069. update_rw_regs(host, 1, false, ecc->steps - 1);
  2070. config_nand_page_write(chip);
  2071. write_data_dma(nandc, FLASH_BUF_ACC,
  2072. nandc->data_buffer, host->cw_size, 0);
  2073. config_nand_cw_write(chip);
  2074. ret = submit_descs(nandc);
  2075. free_descs(nandc);
  2076. if (ret) {
  2077. dev_err(nandc->dev, "failure to update BBM\n");
  2078. return -EIO;
  2079. }
  2080. return nand_prog_page_end_op(chip);
  2081. }
  2082. /*
  2083. * the three functions below implement chip->legacy.read_byte(),
  2084. * chip->legacy.read_buf() and chip->legacy.write_buf() respectively. these
  2085. * aren't used for reading/writing page data, they are used for smaller data
  2086. * like reading id, status etc
  2087. */
  2088. static uint8_t qcom_nandc_read_byte(struct nand_chip *chip)
  2089. {
  2090. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  2091. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  2092. u8 *buf = nandc->data_buffer;
  2093. u8 ret = 0x0;
  2094. if (host->last_command == NAND_CMD_STATUS) {
  2095. ret = host->status;
  2096. host->status = NAND_STATUS_READY | NAND_STATUS_WP;
  2097. return ret;
  2098. }
  2099. if (nandc->buf_start < nandc->buf_count)
  2100. ret = buf[nandc->buf_start++];
  2101. return ret;
  2102. }
  2103. static void qcom_nandc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
  2104. {
  2105. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  2106. int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
  2107. memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
  2108. nandc->buf_start += real_len;
  2109. }
  2110. static void qcom_nandc_write_buf(struct nand_chip *chip, const uint8_t *buf,
  2111. int len)
  2112. {
  2113. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  2114. int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
  2115. memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
  2116. nandc->buf_start += real_len;
  2117. }
  2118. /* we support only one external chip for now */
  2119. static void qcom_nandc_select_chip(struct nand_chip *chip, int chipnr)
  2120. {
  2121. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  2122. if (chipnr <= 0)
  2123. return;
  2124. dev_warn(nandc->dev, "invalid chip select\n");
  2125. }
  2126. /*
  2127. * NAND controller page layout info
  2128. *
  2129. * Layout with ECC enabled:
  2130. *
  2131. * |----------------------| |---------------------------------|
  2132. * | xx.......yy| | *********xx.......yy|
  2133. * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
  2134. * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
  2135. * | xx.......yy| | *********xx.......yy|
  2136. * |----------------------| |---------------------------------|
  2137. * codeword 1,2..n-1 codeword n
  2138. * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
  2139. *
  2140. * n = Number of codewords in the page
  2141. * . = ECC bytes
  2142. * * = Spare/free bytes
  2143. * x = Unused byte(s)
  2144. * y = Reserved byte(s)
  2145. *
  2146. * 2K page: n = 4, spare = 16 bytes
  2147. * 4K page: n = 8, spare = 32 bytes
  2148. * 8K page: n = 16, spare = 64 bytes
  2149. *
  2150. * the qcom nand controller operates at a sub page/codeword level. each
  2151. * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
  2152. * the number of ECC bytes vary based on the ECC strength and the bus width.
  2153. *
  2154. * the first n - 1 codewords contains 516 bytes of user data, the remaining
  2155. * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
  2156. * both user data and spare(oobavail) bytes that sum up to 516 bytes.
  2157. *
  2158. * When we access a page with ECC enabled, the reserved bytes(s) are not
  2159. * accessible at all. When reading, we fill up these unreadable positions
  2160. * with 0xffs. When writing, the controller skips writing the inaccessible
  2161. * bytes.
  2162. *
  2163. * Layout with ECC disabled:
  2164. *
  2165. * |------------------------------| |---------------------------------------|
  2166. * | yy xx.......| | bb *********xx.......|
  2167. * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
  2168. * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
  2169. * | yy xx.......| | bb *********xx.......|
  2170. * |------------------------------| |---------------------------------------|
  2171. * codeword 1,2..n-1 codeword n
  2172. * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
  2173. *
  2174. * n = Number of codewords in the page
  2175. * . = ECC bytes
  2176. * * = Spare/free bytes
  2177. * x = Unused byte(s)
  2178. * y = Dummy Bad Bock byte(s)
  2179. * b = Real Bad Block byte(s)
  2180. * size1/size2 = function of codeword size and 'n'
  2181. *
  2182. * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
  2183. * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
  2184. * Block Markers. In the last codeword, this position contains the real BBM
  2185. *
  2186. * In order to have a consistent layout between RAW and ECC modes, we assume
  2187. * the following OOB layout arrangement:
  2188. *
  2189. * |-----------| |--------------------|
  2190. * |yyxx.......| |bb*********xx.......|
  2191. * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
  2192. * |yyxx.......| |bb*********xx.......|
  2193. * |yyxx.......| |bb*********xx.......|
  2194. * |-----------| |--------------------|
  2195. * first n - 1 nth OOB region
  2196. * OOB regions
  2197. *
  2198. * n = Number of codewords in the page
  2199. * . = ECC bytes
  2200. * * = FREE OOB bytes
  2201. * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
  2202. * x = Unused byte(s)
  2203. * b = Real bad block byte(s) (inaccessible when ECC enabled)
  2204. *
  2205. * This layout is read as is when ECC is disabled. When ECC is enabled, the
  2206. * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
  2207. * and assumed as 0xffs when we read a page/oob. The ECC, unused and
  2208. * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
  2209. * the sum of the three).
  2210. */
  2211. static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
  2212. struct mtd_oob_region *oobregion)
  2213. {
  2214. struct nand_chip *chip = mtd_to_nand(mtd);
  2215. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  2216. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2217. if (section > 1)
  2218. return -ERANGE;
  2219. if (!section) {
  2220. oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
  2221. host->bbm_size;
  2222. oobregion->offset = 0;
  2223. } else {
  2224. oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
  2225. oobregion->offset = mtd->oobsize - oobregion->length;
  2226. }
  2227. return 0;
  2228. }
  2229. static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
  2230. struct mtd_oob_region *oobregion)
  2231. {
  2232. struct nand_chip *chip = mtd_to_nand(mtd);
  2233. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  2234. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2235. if (section)
  2236. return -ERANGE;
  2237. oobregion->length = ecc->steps * 4;
  2238. oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
  2239. return 0;
  2240. }
  2241. static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
  2242. .ecc = qcom_nand_ooblayout_ecc,
  2243. .free = qcom_nand_ooblayout_free,
  2244. };
  2245. static int
  2246. qcom_nandc_calc_ecc_bytes(int step_size, int strength)
  2247. {
  2248. return strength == 4 ? 12 : 16;
  2249. }
  2250. NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes,
  2251. NANDC_STEP_SIZE, 4, 8);
  2252. static int qcom_nand_attach_chip(struct nand_chip *chip)
  2253. {
  2254. struct mtd_info *mtd = nand_to_mtd(chip);
  2255. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  2256. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2257. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  2258. int cwperpage, bad_block_byte, ret;
  2259. bool wide_bus;
  2260. int ecc_mode = 1;
  2261. /* controller only supports 512 bytes data steps */
  2262. ecc->size = NANDC_STEP_SIZE;
  2263. wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
  2264. cwperpage = mtd->writesize / NANDC_STEP_SIZE;
  2265. /*
  2266. * Each CW has 4 available OOB bytes which will be protected with ECC
  2267. * so remaining bytes can be used for ECC.
  2268. */
  2269. ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps,
  2270. mtd->oobsize - (cwperpage * 4));
  2271. if (ret) {
  2272. dev_err(nandc->dev, "No valid ECC settings possible\n");
  2273. return ret;
  2274. }
  2275. if (ecc->strength >= 8) {
  2276. /* 8 bit ECC defaults to BCH ECC on all platforms */
  2277. host->bch_enabled = true;
  2278. ecc_mode = 1;
  2279. if (wide_bus) {
  2280. host->ecc_bytes_hw = 14;
  2281. host->spare_bytes = 0;
  2282. host->bbm_size = 2;
  2283. } else {
  2284. host->ecc_bytes_hw = 13;
  2285. host->spare_bytes = 2;
  2286. host->bbm_size = 1;
  2287. }
  2288. } else {
  2289. /*
  2290. * if the controller supports BCH for 4 bit ECC, the controller
  2291. * uses lesser bytes for ECC. If RS is used, the ECC bytes is
  2292. * always 10 bytes
  2293. */
  2294. if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
  2295. /* BCH */
  2296. host->bch_enabled = true;
  2297. ecc_mode = 0;
  2298. if (wide_bus) {
  2299. host->ecc_bytes_hw = 8;
  2300. host->spare_bytes = 2;
  2301. host->bbm_size = 2;
  2302. } else {
  2303. host->ecc_bytes_hw = 7;
  2304. host->spare_bytes = 4;
  2305. host->bbm_size = 1;
  2306. }
  2307. } else {
  2308. /* RS */
  2309. host->ecc_bytes_hw = 10;
  2310. if (wide_bus) {
  2311. host->spare_bytes = 0;
  2312. host->bbm_size = 2;
  2313. } else {
  2314. host->spare_bytes = 1;
  2315. host->bbm_size = 1;
  2316. }
  2317. }
  2318. }
  2319. /*
  2320. * we consider ecc->bytes as the sum of all the non-data content in a
  2321. * step. It gives us a clean representation of the oob area (even if
  2322. * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
  2323. * ECC and 12 bytes for 4 bit ECC
  2324. */
  2325. ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
  2326. ecc->read_page = qcom_nandc_read_page;
  2327. ecc->read_page_raw = qcom_nandc_read_page_raw;
  2328. ecc->read_oob = qcom_nandc_read_oob;
  2329. ecc->write_page = qcom_nandc_write_page;
  2330. ecc->write_page_raw = qcom_nandc_write_page_raw;
  2331. ecc->write_oob = qcom_nandc_write_oob;
  2332. ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
  2333. mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
  2334. /* Free the initially allocated BAM transaction for reading the ONFI params */
  2335. if (nandc->props->is_bam)
  2336. free_bam_transaction(nandc);
  2337. nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
  2338. cwperpage);
  2339. /* Now allocate the BAM transaction based on updated max_cwperpage */
  2340. if (nandc->props->is_bam) {
  2341. nandc->bam_txn = alloc_bam_transaction(nandc);
  2342. if (!nandc->bam_txn) {
  2343. dev_err(nandc->dev,
  2344. "failed to allocate bam transaction\n");
  2345. return -ENOMEM;
  2346. }
  2347. }
  2348. /*
  2349. * DATA_UD_BYTES varies based on whether the read/write command protects
  2350. * spare data with ECC too. We protect spare data by default, so we set
  2351. * it to main + spare data, which are 512 and 4 bytes respectively.
  2352. */
  2353. host->cw_data = 516;
  2354. /*
  2355. * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
  2356. * for 8 bit ECC
  2357. */
  2358. host->cw_size = host->cw_data + ecc->bytes;
  2359. bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
  2360. host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
  2361. | host->cw_data << UD_SIZE_BYTES
  2362. | 0 << DISABLE_STATUS_AFTER_WRITE
  2363. | 5 << NUM_ADDR_CYCLES
  2364. | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
  2365. | 0 << STATUS_BFR_READ
  2366. | 1 << SET_RD_MODE_AFTER_STATUS
  2367. | host->spare_bytes << SPARE_SIZE_BYTES;
  2368. host->cfg1 = 7 << NAND_RECOVERY_CYCLES
  2369. | 0 << CS_ACTIVE_BSY
  2370. | bad_block_byte << BAD_BLOCK_BYTE_NUM
  2371. | 0 << BAD_BLOCK_IN_SPARE_AREA
  2372. | 2 << WR_RD_BSY_GAP
  2373. | wide_bus << WIDE_FLASH
  2374. | host->bch_enabled << ENABLE_BCH_ECC;
  2375. host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
  2376. | host->cw_size << UD_SIZE_BYTES
  2377. | 5 << NUM_ADDR_CYCLES
  2378. | 0 << SPARE_SIZE_BYTES;
  2379. host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
  2380. | 0 << CS_ACTIVE_BSY
  2381. | 17 << BAD_BLOCK_BYTE_NUM
  2382. | 1 << BAD_BLOCK_IN_SPARE_AREA
  2383. | 2 << WR_RD_BSY_GAP
  2384. | wide_bus << WIDE_FLASH
  2385. | 1 << DEV0_CFG1_ECC_DISABLE;
  2386. host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
  2387. | 0 << ECC_SW_RESET
  2388. | host->cw_data << ECC_NUM_DATA_BYTES
  2389. | 1 << ECC_FORCE_CLK_OPEN
  2390. | ecc_mode << ECC_MODE
  2391. | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
  2392. if (!nandc->props->qpic_v2)
  2393. host->ecc_buf_cfg = 0x203 << NUM_STEPS;
  2394. host->clrflashstatus = FS_READY_BSY_N;
  2395. host->clrreadstatus = 0xc0;
  2396. nandc->regs->erased_cw_detect_cfg_clr =
  2397. cpu_to_le32(CLR_ERASED_PAGE_DET);
  2398. nandc->regs->erased_cw_detect_cfg_set =
  2399. cpu_to_le32(SET_ERASED_PAGE_DET);
  2400. dev_dbg(nandc->dev,
  2401. "cfg0 %x cfg1 %x ecc_buf_cfg %x ecc_bch cfg %x cw_size %d cw_data %d strength %d parity_bytes %d steps %d\n",
  2402. host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
  2403. host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
  2404. cwperpage);
  2405. return 0;
  2406. }
  2407. static const struct nand_controller_ops qcom_nandc_ops = {
  2408. .attach_chip = qcom_nand_attach_chip,
  2409. };
  2410. static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
  2411. {
  2412. if (nandc->props->is_bam) {
  2413. if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
  2414. dma_unmap_single(nandc->dev, nandc->reg_read_dma,
  2415. MAX_REG_RD *
  2416. sizeof(*nandc->reg_read_buf),
  2417. DMA_FROM_DEVICE);
  2418. if (nandc->tx_chan)
  2419. dma_release_channel(nandc->tx_chan);
  2420. if (nandc->rx_chan)
  2421. dma_release_channel(nandc->rx_chan);
  2422. if (nandc->cmd_chan)
  2423. dma_release_channel(nandc->cmd_chan);
  2424. } else {
  2425. if (nandc->chan)
  2426. dma_release_channel(nandc->chan);
  2427. }
  2428. }
  2429. static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
  2430. {
  2431. int ret;
  2432. ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
  2433. if (ret) {
  2434. dev_err(nandc->dev, "failed to set DMA mask\n");
  2435. return ret;
  2436. }
  2437. /*
  2438. * we use the internal buffer for reading ONFI params, reading small
  2439. * data like ID and status, and preforming read-copy-write operations
  2440. * when writing to a codeword partially. 532 is the maximum possible
  2441. * size of a codeword for our nand controller
  2442. */
  2443. nandc->buf_size = 532;
  2444. nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
  2445. GFP_KERNEL);
  2446. if (!nandc->data_buffer)
  2447. return -ENOMEM;
  2448. nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
  2449. GFP_KERNEL);
  2450. if (!nandc->regs)
  2451. return -ENOMEM;
  2452. nandc->reg_read_buf = devm_kcalloc(nandc->dev,
  2453. MAX_REG_RD, sizeof(*nandc->reg_read_buf),
  2454. GFP_KERNEL);
  2455. if (!nandc->reg_read_buf)
  2456. return -ENOMEM;
  2457. if (nandc->props->is_bam) {
  2458. nandc->reg_read_dma =
  2459. dma_map_single(nandc->dev, nandc->reg_read_buf,
  2460. MAX_REG_RD *
  2461. sizeof(*nandc->reg_read_buf),
  2462. DMA_FROM_DEVICE);
  2463. if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
  2464. dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
  2465. return -EIO;
  2466. }
  2467. nandc->tx_chan = dma_request_chan(nandc->dev, "tx");
  2468. if (IS_ERR(nandc->tx_chan)) {
  2469. ret = PTR_ERR(nandc->tx_chan);
  2470. nandc->tx_chan = NULL;
  2471. dev_err_probe(nandc->dev, ret,
  2472. "tx DMA channel request failed\n");
  2473. goto unalloc;
  2474. }
  2475. nandc->rx_chan = dma_request_chan(nandc->dev, "rx");
  2476. if (IS_ERR(nandc->rx_chan)) {
  2477. ret = PTR_ERR(nandc->rx_chan);
  2478. nandc->rx_chan = NULL;
  2479. dev_err_probe(nandc->dev, ret,
  2480. "rx DMA channel request failed\n");
  2481. goto unalloc;
  2482. }
  2483. nandc->cmd_chan = dma_request_chan(nandc->dev, "cmd");
  2484. if (IS_ERR(nandc->cmd_chan)) {
  2485. ret = PTR_ERR(nandc->cmd_chan);
  2486. nandc->cmd_chan = NULL;
  2487. dev_err_probe(nandc->dev, ret,
  2488. "cmd DMA channel request failed\n");
  2489. goto unalloc;
  2490. }
  2491. /*
  2492. * Initially allocate BAM transaction to read ONFI param page.
  2493. * After detecting all the devices, this BAM transaction will
  2494. * be freed and the next BAM tranasction will be allocated with
  2495. * maximum codeword size
  2496. */
  2497. nandc->max_cwperpage = 1;
  2498. nandc->bam_txn = alloc_bam_transaction(nandc);
  2499. if (!nandc->bam_txn) {
  2500. dev_err(nandc->dev,
  2501. "failed to allocate bam transaction\n");
  2502. ret = -ENOMEM;
  2503. goto unalloc;
  2504. }
  2505. } else {
  2506. nandc->chan = dma_request_chan(nandc->dev, "rxtx");
  2507. if (IS_ERR(nandc->chan)) {
  2508. ret = PTR_ERR(nandc->chan);
  2509. nandc->chan = NULL;
  2510. dev_err_probe(nandc->dev, ret,
  2511. "rxtx DMA channel request failed\n");
  2512. return ret;
  2513. }
  2514. }
  2515. INIT_LIST_HEAD(&nandc->desc_list);
  2516. INIT_LIST_HEAD(&nandc->host_list);
  2517. nand_controller_init(&nandc->controller);
  2518. nandc->controller.ops = &qcom_nandc_ops;
  2519. return 0;
  2520. unalloc:
  2521. qcom_nandc_unalloc(nandc);
  2522. return ret;
  2523. }
  2524. /* one time setup of a few nand controller registers */
  2525. static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
  2526. {
  2527. u32 nand_ctrl;
  2528. /* kill onenand */
  2529. if (!nandc->props->is_qpic)
  2530. nandc_write(nandc, SFLASHC_BURST_CFG, 0);
  2531. if (!nandc->props->qpic_v2)
  2532. nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
  2533. NAND_DEV_CMD_VLD_VAL);
  2534. /* enable ADM or BAM DMA */
  2535. if (nandc->props->is_bam) {
  2536. nand_ctrl = nandc_read(nandc, NAND_CTRL);
  2537. /*
  2538. *NAND_CTRL is an operational registers, and CPU
  2539. * access to operational registers are read only
  2540. * in BAM mode. So update the NAND_CTRL register
  2541. * only if it is not in BAM mode. In most cases BAM
  2542. * mode will be enabled in bootloader
  2543. */
  2544. if (!(nand_ctrl & BAM_MODE_EN))
  2545. nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
  2546. } else {
  2547. nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
  2548. }
  2549. /* save the original values of these registers */
  2550. if (!nandc->props->qpic_v2) {
  2551. nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
  2552. nandc->vld = NAND_DEV_CMD_VLD_VAL;
  2553. }
  2554. return 0;
  2555. }
  2556. static const char * const probes[] = { "cmdlinepart", "ofpart", "qcomsmem", NULL };
  2557. static int qcom_nand_host_parse_boot_partitions(struct qcom_nand_controller *nandc,
  2558. struct qcom_nand_host *host,
  2559. struct device_node *dn)
  2560. {
  2561. struct nand_chip *chip = &host->chip;
  2562. struct mtd_info *mtd = nand_to_mtd(chip);
  2563. struct qcom_nand_boot_partition *boot_partition;
  2564. struct device *dev = nandc->dev;
  2565. int partitions_count, i, j, ret;
  2566. if (!of_find_property(dn, "qcom,boot-partitions", NULL))
  2567. return 0;
  2568. partitions_count = of_property_count_u32_elems(dn, "qcom,boot-partitions");
  2569. if (partitions_count <= 0) {
  2570. dev_err(dev, "Error parsing boot partition\n");
  2571. return partitions_count ? partitions_count : -EINVAL;
  2572. }
  2573. host->nr_boot_partitions = partitions_count / 2;
  2574. host->boot_partitions = devm_kcalloc(dev, host->nr_boot_partitions,
  2575. sizeof(*host->boot_partitions), GFP_KERNEL);
  2576. if (!host->boot_partitions) {
  2577. host->nr_boot_partitions = 0;
  2578. return -ENOMEM;
  2579. }
  2580. for (i = 0, j = 0; i < host->nr_boot_partitions; i++, j += 2) {
  2581. boot_partition = &host->boot_partitions[i];
  2582. ret = of_property_read_u32_index(dn, "qcom,boot-partitions", j,
  2583. &boot_partition->page_offset);
  2584. if (ret) {
  2585. dev_err(dev, "Error parsing boot partition offset at index %d\n", i);
  2586. host->nr_boot_partitions = 0;
  2587. return ret;
  2588. }
  2589. if (boot_partition->page_offset % mtd->writesize) {
  2590. dev_err(dev, "Boot partition offset not multiple of writesize at index %i\n",
  2591. i);
  2592. host->nr_boot_partitions = 0;
  2593. return -EINVAL;
  2594. }
  2595. /* Convert offset to nand pages */
  2596. boot_partition->page_offset /= mtd->writesize;
  2597. ret = of_property_read_u32_index(dn, "qcom,boot-partitions", j + 1,
  2598. &boot_partition->page_size);
  2599. if (ret) {
  2600. dev_err(dev, "Error parsing boot partition size at index %d\n", i);
  2601. host->nr_boot_partitions = 0;
  2602. return ret;
  2603. }
  2604. if (boot_partition->page_size % mtd->writesize) {
  2605. dev_err(dev, "Boot partition size not multiple of writesize at index %i\n",
  2606. i);
  2607. host->nr_boot_partitions = 0;
  2608. return -EINVAL;
  2609. }
  2610. /* Convert size to nand pages */
  2611. boot_partition->page_size /= mtd->writesize;
  2612. }
  2613. return 0;
  2614. }
  2615. static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc,
  2616. struct qcom_nand_host *host,
  2617. struct device_node *dn)
  2618. {
  2619. struct nand_chip *chip = &host->chip;
  2620. struct mtd_info *mtd = nand_to_mtd(chip);
  2621. struct device *dev = nandc->dev;
  2622. int ret;
  2623. ret = of_property_read_u32(dn, "reg", &host->cs);
  2624. if (ret) {
  2625. dev_err(dev, "can't get chip-select\n");
  2626. return -ENXIO;
  2627. }
  2628. nand_set_flash_node(chip, dn);
  2629. mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
  2630. if (!mtd->name)
  2631. return -ENOMEM;
  2632. mtd->owner = THIS_MODULE;
  2633. mtd->dev.parent = dev;
  2634. chip->legacy.cmdfunc = qcom_nandc_command;
  2635. chip->legacy.select_chip = qcom_nandc_select_chip;
  2636. chip->legacy.read_byte = qcom_nandc_read_byte;
  2637. chip->legacy.read_buf = qcom_nandc_read_buf;
  2638. chip->legacy.write_buf = qcom_nandc_write_buf;
  2639. chip->legacy.set_features = nand_get_set_features_notsupp;
  2640. chip->legacy.get_features = nand_get_set_features_notsupp;
  2641. /*
  2642. * the bad block marker is readable only when we read the last codeword
  2643. * of a page with ECC disabled. currently, the nand_base and nand_bbt
  2644. * helpers don't allow us to read BB from a nand chip with ECC
  2645. * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
  2646. * and block_markbad helpers until we permanently switch to using
  2647. * MTD_OPS_RAW for all drivers (with the help of badblockbits)
  2648. */
  2649. chip->legacy.block_bad = qcom_nandc_block_bad;
  2650. chip->legacy.block_markbad = qcom_nandc_block_markbad;
  2651. chip->controller = &nandc->controller;
  2652. chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA |
  2653. NAND_SKIP_BBTSCAN;
  2654. /* set up initial status value */
  2655. host->status = NAND_STATUS_READY | NAND_STATUS_WP;
  2656. ret = nand_scan(chip, 1);
  2657. if (ret)
  2658. return ret;
  2659. ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0);
  2660. if (ret)
  2661. goto err;
  2662. if (nandc->props->use_codeword_fixup) {
  2663. ret = qcom_nand_host_parse_boot_partitions(nandc, host, dn);
  2664. if (ret)
  2665. goto err;
  2666. }
  2667. return 0;
  2668. err:
  2669. nand_cleanup(chip);
  2670. return ret;
  2671. }
  2672. static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
  2673. {
  2674. struct device *dev = nandc->dev;
  2675. struct device_node *dn = dev->of_node, *child;
  2676. struct qcom_nand_host *host;
  2677. int ret = -ENODEV;
  2678. for_each_available_child_of_node(dn, child) {
  2679. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  2680. if (!host) {
  2681. of_node_put(child);
  2682. return -ENOMEM;
  2683. }
  2684. ret = qcom_nand_host_init_and_register(nandc, host, child);
  2685. if (ret) {
  2686. devm_kfree(dev, host);
  2687. continue;
  2688. }
  2689. list_add_tail(&host->node, &nandc->host_list);
  2690. }
  2691. return ret;
  2692. }
  2693. /* parse custom DT properties here */
  2694. static int qcom_nandc_parse_dt(struct platform_device *pdev)
  2695. {
  2696. struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
  2697. struct device_node *np = nandc->dev->of_node;
  2698. int ret;
  2699. if (!nandc->props->is_bam) {
  2700. ret = of_property_read_u32(np, "qcom,cmd-crci",
  2701. &nandc->cmd_crci);
  2702. if (ret) {
  2703. dev_err(nandc->dev, "command CRCI unspecified\n");
  2704. return ret;
  2705. }
  2706. ret = of_property_read_u32(np, "qcom,data-crci",
  2707. &nandc->data_crci);
  2708. if (ret) {
  2709. dev_err(nandc->dev, "data CRCI unspecified\n");
  2710. return ret;
  2711. }
  2712. }
  2713. return 0;
  2714. }
  2715. static int qcom_nandc_probe(struct platform_device *pdev)
  2716. {
  2717. struct qcom_nand_controller *nandc;
  2718. const void *dev_data;
  2719. struct device *dev = &pdev->dev;
  2720. struct resource *res;
  2721. int ret;
  2722. nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
  2723. if (!nandc)
  2724. return -ENOMEM;
  2725. platform_set_drvdata(pdev, nandc);
  2726. nandc->dev = dev;
  2727. dev_data = of_device_get_match_data(dev);
  2728. if (!dev_data) {
  2729. dev_err(&pdev->dev, "failed to get device data\n");
  2730. return -ENODEV;
  2731. }
  2732. nandc->props = dev_data;
  2733. nandc->core_clk = devm_clk_get(dev, "core");
  2734. if (IS_ERR(nandc->core_clk))
  2735. return PTR_ERR(nandc->core_clk);
  2736. nandc->aon_clk = devm_clk_get(dev, "aon");
  2737. if (IS_ERR(nandc->aon_clk))
  2738. return PTR_ERR(nandc->aon_clk);
  2739. ret = qcom_nandc_parse_dt(pdev);
  2740. if (ret)
  2741. return ret;
  2742. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2743. nandc->base = devm_ioremap_resource(dev, res);
  2744. if (IS_ERR(nandc->base))
  2745. return PTR_ERR(nandc->base);
  2746. nandc->base_phys = res->start;
  2747. nandc->base_dma = dma_map_resource(dev, res->start,
  2748. resource_size(res),
  2749. DMA_BIDIRECTIONAL, 0);
  2750. if (dma_mapping_error(dev, nandc->base_dma))
  2751. return -ENXIO;
  2752. ret = clk_prepare_enable(nandc->core_clk);
  2753. if (ret)
  2754. goto err_core_clk;
  2755. ret = clk_prepare_enable(nandc->aon_clk);
  2756. if (ret)
  2757. goto err_aon_clk;
  2758. ret = qcom_nandc_alloc(nandc);
  2759. if (ret)
  2760. goto err_nandc_alloc;
  2761. ret = qcom_nandc_setup(nandc);
  2762. if (ret)
  2763. goto err_setup;
  2764. ret = qcom_probe_nand_devices(nandc);
  2765. if (ret)
  2766. goto err_setup;
  2767. return 0;
  2768. err_setup:
  2769. qcom_nandc_unalloc(nandc);
  2770. err_nandc_alloc:
  2771. clk_disable_unprepare(nandc->aon_clk);
  2772. err_aon_clk:
  2773. clk_disable_unprepare(nandc->core_clk);
  2774. err_core_clk:
  2775. dma_unmap_resource(dev, nandc->base_dma, resource_size(res),
  2776. DMA_BIDIRECTIONAL, 0);
  2777. return ret;
  2778. }
  2779. static int qcom_nandc_remove(struct platform_device *pdev)
  2780. {
  2781. struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
  2782. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2783. struct qcom_nand_host *host;
  2784. struct nand_chip *chip;
  2785. int ret;
  2786. list_for_each_entry(host, &nandc->host_list, node) {
  2787. chip = &host->chip;
  2788. ret = mtd_device_unregister(nand_to_mtd(chip));
  2789. WARN_ON(ret);
  2790. nand_cleanup(chip);
  2791. }
  2792. qcom_nandc_unalloc(nandc);
  2793. clk_disable_unprepare(nandc->aon_clk);
  2794. clk_disable_unprepare(nandc->core_clk);
  2795. dma_unmap_resource(&pdev->dev, nandc->base_dma, resource_size(res),
  2796. DMA_BIDIRECTIONAL, 0);
  2797. return 0;
  2798. }
  2799. static const struct qcom_nandc_props ipq806x_nandc_props = {
  2800. .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
  2801. .is_bam = false,
  2802. .use_codeword_fixup = true,
  2803. .dev_cmd_reg_start = 0x0,
  2804. };
  2805. static const struct qcom_nandc_props ipq4019_nandc_props = {
  2806. .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
  2807. .is_bam = true,
  2808. .is_qpic = true,
  2809. .dev_cmd_reg_start = 0x0,
  2810. };
  2811. static const struct qcom_nandc_props ipq8074_nandc_props = {
  2812. .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
  2813. .is_bam = true,
  2814. .is_qpic = true,
  2815. .dev_cmd_reg_start = 0x7000,
  2816. };
  2817. static const struct qcom_nandc_props sdx55_nandc_props = {
  2818. .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
  2819. .is_bam = true,
  2820. .is_qpic = true,
  2821. .qpic_v2 = true,
  2822. .dev_cmd_reg_start = 0x7000,
  2823. };
  2824. /*
  2825. * data will hold a struct pointer containing more differences once we support
  2826. * more controller variants
  2827. */
  2828. static const struct of_device_id qcom_nandc_of_match[] = {
  2829. {
  2830. .compatible = "qcom,ipq806x-nand",
  2831. .data = &ipq806x_nandc_props,
  2832. },
  2833. {
  2834. .compatible = "qcom,ipq4019-nand",
  2835. .data = &ipq4019_nandc_props,
  2836. },
  2837. {
  2838. .compatible = "qcom,ipq6018-nand",
  2839. .data = &ipq8074_nandc_props,
  2840. },
  2841. {
  2842. .compatible = "qcom,ipq8074-nand",
  2843. .data = &ipq8074_nandc_props,
  2844. },
  2845. {
  2846. .compatible = "qcom,sdx55-nand",
  2847. .data = &sdx55_nandc_props,
  2848. },
  2849. {}
  2850. };
  2851. MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
  2852. static struct platform_driver qcom_nandc_driver = {
  2853. .driver = {
  2854. .name = "qcom-nandc",
  2855. .of_match_table = qcom_nandc_of_match,
  2856. },
  2857. .probe = qcom_nandc_probe,
  2858. .remove = qcom_nandc_remove,
  2859. };
  2860. module_platform_driver(qcom_nandc_driver);
  2861. MODULE_AUTHOR("Archit Taneja <[email protected]>");
  2862. MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
  2863. MODULE_LICENSE("GPL v2");