qcom_nandc.c 96 KB

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