qcom_nandc.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821
  1. /*
  2. * Copyright (c) 2016, The Linux Foundation. All rights reserved.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/slab.h>
  15. #include <linux/bitops.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/module.h>
  19. #include <linux/mtd/rawnand.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/delay.h>
  24. /* NANDc reg offsets */
  25. #define NAND_FLASH_CMD 0x00
  26. #define NAND_ADDR0 0x04
  27. #define NAND_ADDR1 0x08
  28. #define NAND_FLASH_CHIP_SELECT 0x0c
  29. #define NAND_EXEC_CMD 0x10
  30. #define NAND_FLASH_STATUS 0x14
  31. #define NAND_BUFFER_STATUS 0x18
  32. #define NAND_DEV0_CFG0 0x20
  33. #define NAND_DEV0_CFG1 0x24
  34. #define NAND_DEV0_ECC_CFG 0x28
  35. #define NAND_DEV1_ECC_CFG 0x2c
  36. #define NAND_DEV1_CFG0 0x30
  37. #define NAND_DEV1_CFG1 0x34
  38. #define NAND_READ_ID 0x40
  39. #define NAND_READ_STATUS 0x44
  40. #define NAND_DEV_CMD0 0xa0
  41. #define NAND_DEV_CMD1 0xa4
  42. #define NAND_DEV_CMD2 0xa8
  43. #define NAND_DEV_CMD_VLD 0xac
  44. #define SFLASHC_BURST_CFG 0xe0
  45. #define NAND_ERASED_CW_DETECT_CFG 0xe8
  46. #define NAND_ERASED_CW_DETECT_STATUS 0xec
  47. #define NAND_EBI2_ECC_BUF_CFG 0xf0
  48. #define FLASH_BUF_ACC 0x100
  49. #define NAND_CTRL 0xf00
  50. #define NAND_VERSION 0xf08
  51. #define NAND_READ_LOCATION_0 0xf20
  52. #define NAND_READ_LOCATION_1 0xf24
  53. #define NAND_READ_LOCATION_2 0xf28
  54. #define NAND_READ_LOCATION_3 0xf2c
  55. /* dummy register offsets, used by write_reg_dma */
  56. #define NAND_DEV_CMD1_RESTORE 0xdead
  57. #define NAND_DEV_CMD_VLD_RESTORE 0xbeef
  58. /* NAND_FLASH_CMD bits */
  59. #define PAGE_ACC BIT(4)
  60. #define LAST_PAGE BIT(5)
  61. /* NAND_FLASH_CHIP_SELECT bits */
  62. #define NAND_DEV_SEL 0
  63. #define DM_EN BIT(2)
  64. /* NAND_FLASH_STATUS bits */
  65. #define FS_OP_ERR BIT(4)
  66. #define FS_READY_BSY_N BIT(5)
  67. #define FS_MPU_ERR BIT(8)
  68. #define FS_DEVICE_STS_ERR BIT(16)
  69. #define FS_DEVICE_WP BIT(23)
  70. /* NAND_BUFFER_STATUS bits */
  71. #define BS_UNCORRECTABLE_BIT BIT(8)
  72. #define BS_CORRECTABLE_ERR_MSK 0x1f
  73. /* NAND_DEVn_CFG0 bits */
  74. #define DISABLE_STATUS_AFTER_WRITE 4
  75. #define CW_PER_PAGE 6
  76. #define UD_SIZE_BYTES 9
  77. #define ECC_PARITY_SIZE_BYTES_RS 19
  78. #define SPARE_SIZE_BYTES 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_FORCE_CLK_OPEN 30
  98. /* NAND_DEV_CMD1 bits */
  99. #define READ_ADDR 0
  100. /* NAND_DEV_CMD_VLD bits */
  101. #define READ_START_VLD BIT(0)
  102. #define READ_STOP_VLD BIT(1)
  103. #define WRITE_START_VLD BIT(2)
  104. #define ERASE_START_VLD BIT(3)
  105. #define SEQ_READ_START_VLD BIT(4)
  106. /* NAND_EBI2_ECC_BUF_CFG bits */
  107. #define NUM_STEPS 0
  108. /* NAND_ERASED_CW_DETECT_CFG bits */
  109. #define ERASED_CW_ECC_MASK 1
  110. #define AUTO_DETECT_RES 0
  111. #define MASK_ECC (1 << ERASED_CW_ECC_MASK)
  112. #define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
  113. #define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
  114. #define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
  115. #define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
  116. /* NAND_ERASED_CW_DETECT_STATUS bits */
  117. #define PAGE_ALL_ERASED BIT(7)
  118. #define CODEWORD_ALL_ERASED BIT(6)
  119. #define PAGE_ERASED BIT(5)
  120. #define CODEWORD_ERASED BIT(4)
  121. #define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
  122. #define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
  123. /* NAND_READ_LOCATION_n bits */
  124. #define READ_LOCATION_OFFSET 0
  125. #define READ_LOCATION_SIZE 16
  126. #define READ_LOCATION_LAST 31
  127. /* Version Mask */
  128. #define NAND_VERSION_MAJOR_MASK 0xf0000000
  129. #define NAND_VERSION_MAJOR_SHIFT 28
  130. #define NAND_VERSION_MINOR_MASK 0x0fff0000
  131. #define NAND_VERSION_MINOR_SHIFT 16
  132. /* NAND OP_CMDs */
  133. #define PAGE_READ 0x2
  134. #define PAGE_READ_WITH_ECC 0x3
  135. #define PAGE_READ_WITH_ECC_SPARE 0x4
  136. #define PROGRAM_PAGE 0x6
  137. #define PAGE_PROGRAM_WITH_ECC 0x7
  138. #define PROGRAM_PAGE_SPARE 0x9
  139. #define BLOCK_ERASE 0xa
  140. #define FETCH_ID 0xb
  141. #define RESET_DEVICE 0xd
  142. /* Default Value for NAND_DEV_CMD_VLD */
  143. #define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
  144. ERASE_START_VLD | SEQ_READ_START_VLD)
  145. /* NAND_CTRL bits */
  146. #define BAM_MODE_EN BIT(0)
  147. /*
  148. * the NAND controller performs reads/writes with ECC in 516 byte chunks.
  149. * the driver calls the chunks 'step' or 'codeword' interchangeably
  150. */
  151. #define NANDC_STEP_SIZE 512
  152. /*
  153. * the largest page size we support is 8K, this will have 16 steps/codewords
  154. * of 512 bytes each
  155. */
  156. #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
  157. /* we read at most 3 registers per codeword scan */
  158. #define MAX_REG_RD (3 * MAX_NUM_STEPS)
  159. /* ECC modes supported by the controller */
  160. #define ECC_NONE BIT(0)
  161. #define ECC_RS_4BIT BIT(1)
  162. #define ECC_BCH_4BIT BIT(2)
  163. #define ECC_BCH_8BIT BIT(3)
  164. #define nandc_set_read_loc(nandc, reg, offset, size, is_last) \
  165. nandc_set_reg(nandc, NAND_READ_LOCATION_##reg, \
  166. ((offset) << READ_LOCATION_OFFSET) | \
  167. ((size) << READ_LOCATION_SIZE) | \
  168. ((is_last) << READ_LOCATION_LAST))
  169. /*
  170. * Returns the actual register address for all NAND_DEV_ registers
  171. * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
  172. */
  173. #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
  174. #define QPIC_PER_CW_CMD_SGL 32
  175. #define QPIC_PER_CW_DATA_SGL 8
  176. /*
  177. * Flags used in DMA descriptor preparation helper functions
  178. * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
  179. */
  180. /* Don't set the EOT in current tx BAM sgl */
  181. #define NAND_BAM_NO_EOT BIT(0)
  182. /* Set the NWD flag in current BAM sgl */
  183. #define NAND_BAM_NWD BIT(1)
  184. /* Finish writing in the current BAM sgl and start writing in another BAM sgl */
  185. #define NAND_BAM_NEXT_SGL BIT(2)
  186. /*
  187. * Erased codeword status is being used two times in single transfer so this
  188. * flag will determine the current value of erased codeword status register
  189. */
  190. #define NAND_ERASED_CW_SET BIT(4)
  191. /*
  192. * This data type corresponds to the BAM transaction which will be used for all
  193. * NAND transfers.
  194. * @cmd_sgl - sgl for NAND BAM command pipe
  195. * @data_sgl - sgl for NAND BAM consumer/producer pipe
  196. * @cmd_sgl_pos - current index in command sgl.
  197. * @cmd_sgl_start - start index in command sgl.
  198. * @tx_sgl_pos - current index in data sgl for tx.
  199. * @tx_sgl_start - start index in data sgl for tx.
  200. * @rx_sgl_pos - current index in data sgl for rx.
  201. * @rx_sgl_start - start index in data sgl for rx.
  202. */
  203. struct bam_transaction {
  204. struct scatterlist *cmd_sgl;
  205. struct scatterlist *data_sgl;
  206. u32 cmd_sgl_pos;
  207. u32 cmd_sgl_start;
  208. u32 tx_sgl_pos;
  209. u32 tx_sgl_start;
  210. u32 rx_sgl_pos;
  211. u32 rx_sgl_start;
  212. };
  213. /*
  214. * This data type corresponds to the nand dma descriptor
  215. * @list - list for desc_info
  216. * @dir - DMA transfer direction
  217. * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
  218. * ADM
  219. * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
  220. * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
  221. * @dma_desc - low level DMA engine descriptor
  222. */
  223. struct desc_info {
  224. struct list_head node;
  225. enum dma_data_direction dir;
  226. union {
  227. struct scatterlist adm_sgl;
  228. struct {
  229. struct scatterlist *bam_sgl;
  230. int sgl_cnt;
  231. };
  232. };
  233. struct dma_async_tx_descriptor *dma_desc;
  234. };
  235. /*
  236. * holds the current register values that we want to write. acts as a contiguous
  237. * chunk of memory which we use to write the controller registers through DMA.
  238. */
  239. struct nandc_regs {
  240. __le32 cmd;
  241. __le32 addr0;
  242. __le32 addr1;
  243. __le32 chip_sel;
  244. __le32 exec;
  245. __le32 cfg0;
  246. __le32 cfg1;
  247. __le32 ecc_bch_cfg;
  248. __le32 clrflashstatus;
  249. __le32 clrreadstatus;
  250. __le32 cmd1;
  251. __le32 vld;
  252. __le32 orig_cmd1;
  253. __le32 orig_vld;
  254. __le32 ecc_buf_cfg;
  255. __le32 read_location0;
  256. __le32 read_location1;
  257. __le32 read_location2;
  258. __le32 read_location3;
  259. __le32 erased_cw_detect_cfg_clr;
  260. __le32 erased_cw_detect_cfg_set;
  261. };
  262. /*
  263. * NAND controller data struct
  264. *
  265. * @controller: base controller structure
  266. * @host_list: list containing all the chips attached to the
  267. * controller
  268. * @dev: parent device
  269. * @base: MMIO base
  270. * @base_dma: physical base address of controller registers
  271. * @core_clk: controller clock
  272. * @aon_clk: another controller clock
  273. *
  274. * @chan: dma channel
  275. * @cmd_crci: ADM DMA CRCI for command flow control
  276. * @data_crci: ADM DMA CRCI for data flow control
  277. * @desc_list: DMA descriptor list (list of desc_infos)
  278. *
  279. * @data_buffer: our local DMA buffer for page read/writes,
  280. * used when we can't use the buffer provided
  281. * by upper layers directly
  282. * @buf_size/count/start: markers for chip->read_buf/write_buf functions
  283. * @reg_read_buf: local buffer for reading back registers via DMA
  284. * @reg_read_dma: contains dma address for register read buffer
  285. * @reg_read_pos: marker for data read in reg_read_buf
  286. *
  287. * @regs: a contiguous chunk of memory for DMA register
  288. * writes. contains the register values to be
  289. * written to controller
  290. * @cmd1/vld: some fixed controller register values
  291. * @props: properties of current NAND controller,
  292. * initialized via DT match data
  293. * @max_cwperpage: maximum QPIC codewords required. calculated
  294. * from all connected NAND devices pagesize
  295. */
  296. struct qcom_nand_controller {
  297. struct nand_hw_control controller;
  298. struct list_head host_list;
  299. struct device *dev;
  300. void __iomem *base;
  301. dma_addr_t base_dma;
  302. struct clk *core_clk;
  303. struct clk *aon_clk;
  304. union {
  305. /* will be used only by QPIC for BAM DMA */
  306. struct {
  307. struct dma_chan *tx_chan;
  308. struct dma_chan *rx_chan;
  309. struct dma_chan *cmd_chan;
  310. };
  311. /* will be used only by EBI2 for ADM DMA */
  312. struct {
  313. struct dma_chan *chan;
  314. unsigned int cmd_crci;
  315. unsigned int data_crci;
  316. };
  317. };
  318. struct list_head desc_list;
  319. struct bam_transaction *bam_txn;
  320. u8 *data_buffer;
  321. int buf_size;
  322. int buf_count;
  323. int buf_start;
  324. unsigned int max_cwperpage;
  325. __le32 *reg_read_buf;
  326. dma_addr_t reg_read_dma;
  327. int reg_read_pos;
  328. struct nandc_regs *regs;
  329. u32 cmd1, vld;
  330. const struct qcom_nandc_props *props;
  331. };
  332. /*
  333. * NAND chip structure
  334. *
  335. * @chip: base NAND chip structure
  336. * @node: list node to add itself to host_list in
  337. * qcom_nand_controller
  338. *
  339. * @cs: chip select value for this chip
  340. * @cw_size: the number of bytes in a single step/codeword
  341. * of a page, consisting of all data, ecc, spare
  342. * and reserved bytes
  343. * @cw_data: the number of bytes within a codeword protected
  344. * by ECC
  345. * @use_ecc: request the controller to use ECC for the
  346. * upcoming read/write
  347. * @bch_enabled: flag to tell whether BCH ECC mode is used
  348. * @ecc_bytes_hw: ECC bytes used by controller hardware for this
  349. * chip
  350. * @status: value to be returned if NAND_CMD_STATUS command
  351. * is executed
  352. * @last_command: keeps track of last command on this chip. used
  353. * for reading correct status
  354. *
  355. * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
  356. * ecc/non-ecc mode for the current nand flash
  357. * device
  358. */
  359. struct qcom_nand_host {
  360. struct nand_chip chip;
  361. struct list_head node;
  362. int cs;
  363. int cw_size;
  364. int cw_data;
  365. bool use_ecc;
  366. bool bch_enabled;
  367. int ecc_bytes_hw;
  368. int spare_bytes;
  369. int bbm_size;
  370. u8 status;
  371. int last_command;
  372. u32 cfg0, cfg1;
  373. u32 cfg0_raw, cfg1_raw;
  374. u32 ecc_buf_cfg;
  375. u32 ecc_bch_cfg;
  376. u32 clrflashstatus;
  377. u32 clrreadstatus;
  378. };
  379. /*
  380. * This data type corresponds to the NAND controller properties which varies
  381. * among different NAND controllers.
  382. * @ecc_modes - ecc mode for NAND
  383. * @is_bam - whether NAND controller is using BAM
  384. * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
  385. */
  386. struct qcom_nandc_props {
  387. u32 ecc_modes;
  388. bool is_bam;
  389. u32 dev_cmd_reg_start;
  390. };
  391. /* Frees the BAM transaction memory */
  392. static void free_bam_transaction(struct qcom_nand_controller *nandc)
  393. {
  394. struct bam_transaction *bam_txn = nandc->bam_txn;
  395. devm_kfree(nandc->dev, bam_txn);
  396. }
  397. /* Allocates and Initializes the BAM transaction */
  398. static struct bam_transaction *
  399. alloc_bam_transaction(struct qcom_nand_controller *nandc)
  400. {
  401. struct bam_transaction *bam_txn;
  402. size_t bam_txn_size;
  403. unsigned int num_cw = nandc->max_cwperpage;
  404. void *bam_txn_buf;
  405. bam_txn_size =
  406. sizeof(*bam_txn) + num_cw *
  407. ((sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
  408. (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
  409. bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
  410. if (!bam_txn_buf)
  411. return NULL;
  412. bam_txn = bam_txn_buf;
  413. bam_txn_buf += sizeof(*bam_txn);
  414. bam_txn->cmd_sgl = bam_txn_buf;
  415. bam_txn_buf +=
  416. sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
  417. bam_txn->data_sgl = bam_txn_buf;
  418. return bam_txn;
  419. }
  420. /* Clears the BAM transaction indexes */
  421. static void clear_bam_transaction(struct qcom_nand_controller *nandc)
  422. {
  423. struct bam_transaction *bam_txn = nandc->bam_txn;
  424. if (!nandc->props->is_bam)
  425. return;
  426. bam_txn->cmd_sgl_pos = 0;
  427. bam_txn->cmd_sgl_start = 0;
  428. bam_txn->tx_sgl_pos = 0;
  429. bam_txn->tx_sgl_start = 0;
  430. bam_txn->rx_sgl_pos = 0;
  431. bam_txn->rx_sgl_start = 0;
  432. sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
  433. QPIC_PER_CW_CMD_SGL);
  434. sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
  435. QPIC_PER_CW_DATA_SGL);
  436. }
  437. static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
  438. {
  439. return container_of(chip, struct qcom_nand_host, chip);
  440. }
  441. static inline struct qcom_nand_controller *
  442. get_qcom_nand_controller(struct nand_chip *chip)
  443. {
  444. return container_of(chip->controller, struct qcom_nand_controller,
  445. controller);
  446. }
  447. static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
  448. {
  449. return ioread32(nandc->base + offset);
  450. }
  451. static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
  452. u32 val)
  453. {
  454. iowrite32(val, nandc->base + offset);
  455. }
  456. static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
  457. bool is_cpu)
  458. {
  459. if (!nandc->props->is_bam)
  460. return;
  461. if (is_cpu)
  462. dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
  463. MAX_REG_RD *
  464. sizeof(*nandc->reg_read_buf),
  465. DMA_FROM_DEVICE);
  466. else
  467. dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
  468. MAX_REG_RD *
  469. sizeof(*nandc->reg_read_buf),
  470. DMA_FROM_DEVICE);
  471. }
  472. static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
  473. {
  474. switch (offset) {
  475. case NAND_FLASH_CMD:
  476. return &regs->cmd;
  477. case NAND_ADDR0:
  478. return &regs->addr0;
  479. case NAND_ADDR1:
  480. return &regs->addr1;
  481. case NAND_FLASH_CHIP_SELECT:
  482. return &regs->chip_sel;
  483. case NAND_EXEC_CMD:
  484. return &regs->exec;
  485. case NAND_FLASH_STATUS:
  486. return &regs->clrflashstatus;
  487. case NAND_DEV0_CFG0:
  488. return &regs->cfg0;
  489. case NAND_DEV0_CFG1:
  490. return &regs->cfg1;
  491. case NAND_DEV0_ECC_CFG:
  492. return &regs->ecc_bch_cfg;
  493. case NAND_READ_STATUS:
  494. return &regs->clrreadstatus;
  495. case NAND_DEV_CMD1:
  496. return &regs->cmd1;
  497. case NAND_DEV_CMD1_RESTORE:
  498. return &regs->orig_cmd1;
  499. case NAND_DEV_CMD_VLD:
  500. return &regs->vld;
  501. case NAND_DEV_CMD_VLD_RESTORE:
  502. return &regs->orig_vld;
  503. case NAND_EBI2_ECC_BUF_CFG:
  504. return &regs->ecc_buf_cfg;
  505. case NAND_READ_LOCATION_0:
  506. return &regs->read_location0;
  507. case NAND_READ_LOCATION_1:
  508. return &regs->read_location1;
  509. case NAND_READ_LOCATION_2:
  510. return &regs->read_location2;
  511. case NAND_READ_LOCATION_3:
  512. return &regs->read_location3;
  513. default:
  514. return NULL;
  515. }
  516. }
  517. static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
  518. u32 val)
  519. {
  520. struct nandc_regs *regs = nandc->regs;
  521. __le32 *reg;
  522. reg = offset_to_nandc_reg(regs, offset);
  523. if (reg)
  524. *reg = cpu_to_le32(val);
  525. }
  526. /* helper to configure address register values */
  527. static void set_address(struct qcom_nand_host *host, u16 column, int page)
  528. {
  529. struct nand_chip *chip = &host->chip;
  530. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  531. if (chip->options & NAND_BUSWIDTH_16)
  532. column >>= 1;
  533. nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
  534. nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
  535. }
  536. /*
  537. * update_rw_regs: set up read/write register values, these will be
  538. * written to the NAND controller registers via DMA
  539. *
  540. * @num_cw: number of steps for the read/write operation
  541. * @read: read or write operation
  542. */
  543. static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
  544. {
  545. struct nand_chip *chip = &host->chip;
  546. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  547. u32 cmd, cfg0, cfg1, ecc_bch_cfg;
  548. if (read) {
  549. if (host->use_ecc)
  550. cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
  551. else
  552. cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
  553. } else {
  554. cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
  555. }
  556. if (host->use_ecc) {
  557. cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
  558. (num_cw - 1) << CW_PER_PAGE;
  559. cfg1 = host->cfg1;
  560. ecc_bch_cfg = host->ecc_bch_cfg;
  561. } else {
  562. cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
  563. (num_cw - 1) << CW_PER_PAGE;
  564. cfg1 = host->cfg1_raw;
  565. ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
  566. }
  567. nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
  568. nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
  569. nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
  570. nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
  571. nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
  572. nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
  573. nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
  574. nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
  575. if (read)
  576. nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
  577. host->cw_data : host->cw_size, 1);
  578. }
  579. /*
  580. * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
  581. * for BAM. This descriptor will be added in the NAND DMA descriptor queue
  582. * which will be submitted to DMA engine.
  583. */
  584. static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
  585. struct dma_chan *chan,
  586. unsigned long flags)
  587. {
  588. struct desc_info *desc;
  589. struct scatterlist *sgl;
  590. unsigned int sgl_cnt;
  591. int ret;
  592. struct bam_transaction *bam_txn = nandc->bam_txn;
  593. enum dma_transfer_direction dir_eng;
  594. struct dma_async_tx_descriptor *dma_desc;
  595. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  596. if (!desc)
  597. return -ENOMEM;
  598. if (chan == nandc->cmd_chan) {
  599. sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
  600. sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
  601. bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
  602. dir_eng = DMA_MEM_TO_DEV;
  603. desc->dir = DMA_TO_DEVICE;
  604. } else if (chan == nandc->tx_chan) {
  605. sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
  606. sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
  607. bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
  608. dir_eng = DMA_MEM_TO_DEV;
  609. desc->dir = DMA_TO_DEVICE;
  610. } else {
  611. sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
  612. sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
  613. bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
  614. dir_eng = DMA_DEV_TO_MEM;
  615. desc->dir = DMA_FROM_DEVICE;
  616. }
  617. sg_mark_end(sgl + sgl_cnt - 1);
  618. ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
  619. if (ret == 0) {
  620. dev_err(nandc->dev, "failure in mapping desc\n");
  621. kfree(desc);
  622. return -ENOMEM;
  623. }
  624. desc->sgl_cnt = sgl_cnt;
  625. desc->bam_sgl = sgl;
  626. dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
  627. flags);
  628. if (!dma_desc) {
  629. dev_err(nandc->dev, "failure in prep desc\n");
  630. dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
  631. kfree(desc);
  632. return -EINVAL;
  633. }
  634. desc->dma_desc = dma_desc;
  635. list_add_tail(&desc->node, &nandc->desc_list);
  636. return 0;
  637. }
  638. /*
  639. * Prepares the data descriptor for BAM DMA which will be used for NAND
  640. * data reads and writes.
  641. */
  642. static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
  643. const void *vaddr,
  644. int size, unsigned int flags)
  645. {
  646. int ret;
  647. struct bam_transaction *bam_txn = nandc->bam_txn;
  648. if (read) {
  649. sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
  650. vaddr, size);
  651. bam_txn->rx_sgl_pos++;
  652. } else {
  653. sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
  654. vaddr, size);
  655. bam_txn->tx_sgl_pos++;
  656. /*
  657. * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
  658. * is not set, form the DMA descriptor
  659. */
  660. if (!(flags & NAND_BAM_NO_EOT)) {
  661. ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
  662. DMA_PREP_INTERRUPT);
  663. if (ret)
  664. return ret;
  665. }
  666. }
  667. return 0;
  668. }
  669. static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
  670. int reg_off, const void *vaddr, int size,
  671. bool flow_control)
  672. {
  673. struct desc_info *desc;
  674. struct dma_async_tx_descriptor *dma_desc;
  675. struct scatterlist *sgl;
  676. struct dma_slave_config slave_conf;
  677. enum dma_transfer_direction dir_eng;
  678. int ret;
  679. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  680. if (!desc)
  681. return -ENOMEM;
  682. sgl = &desc->adm_sgl;
  683. sg_init_one(sgl, vaddr, size);
  684. if (read) {
  685. dir_eng = DMA_DEV_TO_MEM;
  686. desc->dir = DMA_FROM_DEVICE;
  687. } else {
  688. dir_eng = DMA_MEM_TO_DEV;
  689. desc->dir = DMA_TO_DEVICE;
  690. }
  691. ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
  692. if (ret == 0) {
  693. ret = -ENOMEM;
  694. goto err;
  695. }
  696. memset(&slave_conf, 0x00, sizeof(slave_conf));
  697. slave_conf.device_fc = flow_control;
  698. if (read) {
  699. slave_conf.src_maxburst = 16;
  700. slave_conf.src_addr = nandc->base_dma + reg_off;
  701. slave_conf.slave_id = nandc->data_crci;
  702. } else {
  703. slave_conf.dst_maxburst = 16;
  704. slave_conf.dst_addr = nandc->base_dma + reg_off;
  705. slave_conf.slave_id = nandc->cmd_crci;
  706. }
  707. ret = dmaengine_slave_config(nandc->chan, &slave_conf);
  708. if (ret) {
  709. dev_err(nandc->dev, "failed to configure dma channel\n");
  710. goto err;
  711. }
  712. dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
  713. if (!dma_desc) {
  714. dev_err(nandc->dev, "failed to prepare desc\n");
  715. ret = -EINVAL;
  716. goto err;
  717. }
  718. desc->dma_desc = dma_desc;
  719. list_add_tail(&desc->node, &nandc->desc_list);
  720. return 0;
  721. err:
  722. kfree(desc);
  723. return ret;
  724. }
  725. /*
  726. * read_reg_dma: prepares a descriptor to read a given number of
  727. * contiguous registers to the reg_read_buf pointer
  728. *
  729. * @first: offset of the first register in the contiguous block
  730. * @num_regs: number of registers to read
  731. * @flags: flags to control DMA descriptor preparation
  732. */
  733. static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
  734. int num_regs, unsigned int flags)
  735. {
  736. bool flow_control = false;
  737. void *vaddr;
  738. int size;
  739. if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
  740. flow_control = true;
  741. if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
  742. first = dev_cmd_reg_addr(nandc, first);
  743. size = num_regs * sizeof(u32);
  744. vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
  745. nandc->reg_read_pos += num_regs;
  746. return prep_adm_dma_desc(nandc, true, first, vaddr, size, flow_control);
  747. }
  748. /*
  749. * write_reg_dma: prepares a descriptor to write a given number of
  750. * contiguous registers
  751. *
  752. * @first: offset of the first register in the contiguous block
  753. * @num_regs: number of registers to write
  754. * @flags: flags to control DMA descriptor preparation
  755. */
  756. static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
  757. int num_regs, unsigned int flags)
  758. {
  759. bool flow_control = false;
  760. struct nandc_regs *regs = nandc->regs;
  761. void *vaddr;
  762. int size;
  763. vaddr = offset_to_nandc_reg(regs, first);
  764. if (first == NAND_FLASH_CMD)
  765. flow_control = true;
  766. if (first == NAND_ERASED_CW_DETECT_CFG) {
  767. if (flags & NAND_ERASED_CW_SET)
  768. vaddr = &regs->erased_cw_detect_cfg_set;
  769. else
  770. vaddr = &regs->erased_cw_detect_cfg_clr;
  771. }
  772. if (first == NAND_EXEC_CMD)
  773. flags |= NAND_BAM_NWD;
  774. if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
  775. first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
  776. if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
  777. first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
  778. size = num_regs * sizeof(u32);
  779. return prep_adm_dma_desc(nandc, false, first, vaddr, size,
  780. flow_control);
  781. }
  782. /*
  783. * read_data_dma: prepares a DMA descriptor to transfer data from the
  784. * controller's internal buffer to the buffer 'vaddr'
  785. *
  786. * @reg_off: offset within the controller's data buffer
  787. * @vaddr: virtual address of the buffer we want to write to
  788. * @size: DMA transaction size in bytes
  789. * @flags: flags to control DMA descriptor preparation
  790. */
  791. static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
  792. const u8 *vaddr, int size, unsigned int flags)
  793. {
  794. if (nandc->props->is_bam)
  795. return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
  796. return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
  797. }
  798. /*
  799. * write_data_dma: prepares a DMA descriptor to transfer data from
  800. * 'vaddr' to the controller's internal buffer
  801. *
  802. * @reg_off: offset within the controller's data buffer
  803. * @vaddr: virtual address of the buffer we want to read from
  804. * @size: DMA transaction size in bytes
  805. * @flags: flags to control DMA descriptor preparation
  806. */
  807. static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
  808. const u8 *vaddr, int size, unsigned int flags)
  809. {
  810. if (nandc->props->is_bam)
  811. return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
  812. return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
  813. }
  814. /*
  815. * Helper to prepare DMA descriptors for configuring registers
  816. * before reading a NAND page.
  817. */
  818. static void config_nand_page_read(struct qcom_nand_controller *nandc)
  819. {
  820. write_reg_dma(nandc, NAND_ADDR0, 2, 0);
  821. write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
  822. write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
  823. write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
  824. write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
  825. NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
  826. }
  827. /*
  828. * Helper to prepare DMA descriptors for configuring registers
  829. * before reading each codeword in NAND page.
  830. */
  831. static void config_nand_cw_read(struct qcom_nand_controller *nandc)
  832. {
  833. if (nandc->props->is_bam)
  834. write_reg_dma(nandc, NAND_READ_LOCATION_0, 4,
  835. NAND_BAM_NEXT_SGL);
  836. write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
  837. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  838. read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
  839. read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
  840. NAND_BAM_NEXT_SGL);
  841. }
  842. /*
  843. * Helper to prepare dma descriptors to configure registers needed for reading a
  844. * single codeword in page
  845. */
  846. static void config_nand_single_cw_page_read(struct qcom_nand_controller *nandc)
  847. {
  848. config_nand_page_read(nandc);
  849. config_nand_cw_read(nandc);
  850. }
  851. /*
  852. * Helper to prepare DMA descriptors used to configure registers needed for
  853. * before writing a NAND page.
  854. */
  855. static void config_nand_page_write(struct qcom_nand_controller *nandc)
  856. {
  857. write_reg_dma(nandc, NAND_ADDR0, 2, 0);
  858. write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
  859. write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
  860. NAND_BAM_NEXT_SGL);
  861. }
  862. /*
  863. * Helper to prepare DMA descriptors for configuring registers
  864. * before writing each codeword in NAND page.
  865. */
  866. static void config_nand_cw_write(struct qcom_nand_controller *nandc)
  867. {
  868. write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
  869. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  870. read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
  871. write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
  872. write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
  873. }
  874. /*
  875. * the following functions are used within chip->cmdfunc() to perform different
  876. * NAND_CMD_* commands
  877. */
  878. /* sets up descriptors for NAND_CMD_PARAM */
  879. static int nandc_param(struct qcom_nand_host *host)
  880. {
  881. struct nand_chip *chip = &host->chip;
  882. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  883. /*
  884. * NAND_CMD_PARAM is called before we know much about the FLASH chip
  885. * in use. we configure the controller to perform a raw read of 512
  886. * bytes to read onfi params
  887. */
  888. nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
  889. nandc_set_reg(nandc, NAND_ADDR0, 0);
  890. nandc_set_reg(nandc, NAND_ADDR1, 0);
  891. nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
  892. | 512 << UD_SIZE_BYTES
  893. | 5 << NUM_ADDR_CYCLES
  894. | 0 << SPARE_SIZE_BYTES);
  895. nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
  896. | 0 << CS_ACTIVE_BSY
  897. | 17 << BAD_BLOCK_BYTE_NUM
  898. | 1 << BAD_BLOCK_IN_SPARE_AREA
  899. | 2 << WR_RD_BSY_GAP
  900. | 0 << WIDE_FLASH
  901. | 1 << DEV0_CFG1_ECC_DISABLE);
  902. nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
  903. /* configure CMD1 and VLD for ONFI param probing */
  904. nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
  905. (nandc->vld & ~READ_START_VLD));
  906. nandc_set_reg(nandc, NAND_DEV_CMD1,
  907. (nandc->cmd1 & ~(0xFF << READ_ADDR))
  908. | NAND_CMD_PARAM << READ_ADDR);
  909. nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
  910. nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
  911. nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
  912. nandc_set_read_loc(nandc, 0, 0, 512, 1);
  913. write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
  914. write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
  915. nandc->buf_count = 512;
  916. memset(nandc->data_buffer, 0xff, nandc->buf_count);
  917. config_nand_single_cw_page_read(nandc);
  918. read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
  919. nandc->buf_count, 0);
  920. /* restore CMD1 and VLD regs */
  921. write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
  922. write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
  923. return 0;
  924. }
  925. /* sets up descriptors for NAND_CMD_ERASE1 */
  926. static int erase_block(struct qcom_nand_host *host, int page_addr)
  927. {
  928. struct nand_chip *chip = &host->chip;
  929. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  930. nandc_set_reg(nandc, NAND_FLASH_CMD,
  931. BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
  932. nandc_set_reg(nandc, NAND_ADDR0, page_addr);
  933. nandc_set_reg(nandc, NAND_ADDR1, 0);
  934. nandc_set_reg(nandc, NAND_DEV0_CFG0,
  935. host->cfg0_raw & ~(7 << CW_PER_PAGE));
  936. nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
  937. nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
  938. nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
  939. nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
  940. write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
  941. write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
  942. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  943. read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
  944. write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
  945. write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
  946. return 0;
  947. }
  948. /* sets up descriptors for NAND_CMD_READID */
  949. static int read_id(struct qcom_nand_host *host, int column)
  950. {
  951. struct nand_chip *chip = &host->chip;
  952. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  953. if (column == -1)
  954. return 0;
  955. nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
  956. nandc_set_reg(nandc, NAND_ADDR0, column);
  957. nandc_set_reg(nandc, NAND_ADDR1, 0);
  958. nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
  959. nandc->props->is_bam ? 0 : DM_EN);
  960. nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
  961. write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
  962. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  963. read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
  964. return 0;
  965. }
  966. /* sets up descriptors for NAND_CMD_RESET */
  967. static int reset(struct qcom_nand_host *host)
  968. {
  969. struct nand_chip *chip = &host->chip;
  970. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  971. nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
  972. nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
  973. write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
  974. write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
  975. read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
  976. return 0;
  977. }
  978. /* helpers to submit/free our list of dma descriptors */
  979. static int submit_descs(struct qcom_nand_controller *nandc)
  980. {
  981. struct desc_info *desc;
  982. dma_cookie_t cookie = 0;
  983. struct bam_transaction *bam_txn = nandc->bam_txn;
  984. int r;
  985. if (nandc->props->is_bam) {
  986. if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
  987. r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
  988. if (r)
  989. return r;
  990. }
  991. if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
  992. r = prepare_bam_async_desc(nandc, nandc->tx_chan,
  993. DMA_PREP_INTERRUPT);
  994. if (r)
  995. return r;
  996. }
  997. if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
  998. r = prepare_bam_async_desc(nandc, nandc->cmd_chan, 0);
  999. if (r)
  1000. return r;
  1001. }
  1002. }
  1003. list_for_each_entry(desc, &nandc->desc_list, node)
  1004. cookie = dmaengine_submit(desc->dma_desc);
  1005. if (nandc->props->is_bam) {
  1006. dma_async_issue_pending(nandc->tx_chan);
  1007. dma_async_issue_pending(nandc->rx_chan);
  1008. if (dma_sync_wait(nandc->cmd_chan, cookie) != DMA_COMPLETE)
  1009. return -ETIMEDOUT;
  1010. } else {
  1011. if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
  1012. return -ETIMEDOUT;
  1013. }
  1014. return 0;
  1015. }
  1016. static void free_descs(struct qcom_nand_controller *nandc)
  1017. {
  1018. struct desc_info *desc, *n;
  1019. list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
  1020. list_del(&desc->node);
  1021. if (nandc->props->is_bam)
  1022. dma_unmap_sg(nandc->dev, desc->bam_sgl,
  1023. desc->sgl_cnt, desc->dir);
  1024. else
  1025. dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
  1026. desc->dir);
  1027. kfree(desc);
  1028. }
  1029. }
  1030. /* reset the register read buffer for next NAND operation */
  1031. static void clear_read_regs(struct qcom_nand_controller *nandc)
  1032. {
  1033. nandc->reg_read_pos = 0;
  1034. nandc_read_buffer_sync(nandc, false);
  1035. }
  1036. static void pre_command(struct qcom_nand_host *host, int command)
  1037. {
  1038. struct nand_chip *chip = &host->chip;
  1039. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1040. nandc->buf_count = 0;
  1041. nandc->buf_start = 0;
  1042. host->use_ecc = false;
  1043. host->last_command = command;
  1044. clear_read_regs(nandc);
  1045. if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
  1046. command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
  1047. clear_bam_transaction(nandc);
  1048. }
  1049. /*
  1050. * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
  1051. * privately maintained status byte, this status byte can be read after
  1052. * NAND_CMD_STATUS is called
  1053. */
  1054. static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
  1055. {
  1056. struct nand_chip *chip = &host->chip;
  1057. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1058. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1059. int num_cw;
  1060. int i;
  1061. num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
  1062. nandc_read_buffer_sync(nandc, true);
  1063. for (i = 0; i < num_cw; i++) {
  1064. u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
  1065. if (flash_status & FS_MPU_ERR)
  1066. host->status &= ~NAND_STATUS_WP;
  1067. if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
  1068. (flash_status &
  1069. FS_DEVICE_STS_ERR)))
  1070. host->status |= NAND_STATUS_FAIL;
  1071. }
  1072. }
  1073. static void post_command(struct qcom_nand_host *host, int command)
  1074. {
  1075. struct nand_chip *chip = &host->chip;
  1076. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1077. switch (command) {
  1078. case NAND_CMD_READID:
  1079. nandc_read_buffer_sync(nandc, true);
  1080. memcpy(nandc->data_buffer, nandc->reg_read_buf,
  1081. nandc->buf_count);
  1082. break;
  1083. case NAND_CMD_PAGEPROG:
  1084. case NAND_CMD_ERASE1:
  1085. parse_erase_write_errors(host, command);
  1086. break;
  1087. default:
  1088. break;
  1089. }
  1090. }
  1091. /*
  1092. * Implements chip->cmdfunc. It's only used for a limited set of commands.
  1093. * The rest of the commands wouldn't be called by upper layers. For example,
  1094. * NAND_CMD_READOOB would never be called because we have our own versions
  1095. * of read_oob ops for nand_ecc_ctrl.
  1096. */
  1097. static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
  1098. int column, int page_addr)
  1099. {
  1100. struct nand_chip *chip = mtd_to_nand(mtd);
  1101. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1102. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1103. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1104. bool wait = false;
  1105. int ret = 0;
  1106. pre_command(host, command);
  1107. switch (command) {
  1108. case NAND_CMD_RESET:
  1109. ret = reset(host);
  1110. wait = true;
  1111. break;
  1112. case NAND_CMD_READID:
  1113. nandc->buf_count = 4;
  1114. ret = read_id(host, column);
  1115. wait = true;
  1116. break;
  1117. case NAND_CMD_PARAM:
  1118. ret = nandc_param(host);
  1119. wait = true;
  1120. break;
  1121. case NAND_CMD_ERASE1:
  1122. ret = erase_block(host, page_addr);
  1123. wait = true;
  1124. break;
  1125. case NAND_CMD_READ0:
  1126. /* we read the entire page for now */
  1127. WARN_ON(column != 0);
  1128. host->use_ecc = true;
  1129. set_address(host, 0, page_addr);
  1130. update_rw_regs(host, ecc->steps, true);
  1131. break;
  1132. case NAND_CMD_SEQIN:
  1133. WARN_ON(column != 0);
  1134. set_address(host, 0, page_addr);
  1135. break;
  1136. case NAND_CMD_PAGEPROG:
  1137. case NAND_CMD_STATUS:
  1138. case NAND_CMD_NONE:
  1139. default:
  1140. break;
  1141. }
  1142. if (ret) {
  1143. dev_err(nandc->dev, "failure executing command %d\n",
  1144. command);
  1145. free_descs(nandc);
  1146. return;
  1147. }
  1148. if (wait) {
  1149. ret = submit_descs(nandc);
  1150. if (ret)
  1151. dev_err(nandc->dev,
  1152. "failure submitting descs for command %d\n",
  1153. command);
  1154. }
  1155. free_descs(nandc);
  1156. post_command(host, command);
  1157. }
  1158. /*
  1159. * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
  1160. * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
  1161. *
  1162. * when using RS ECC, the HW reports the same erros when reading an erased CW,
  1163. * but it notifies that it is an erased CW by placing special characters at
  1164. * certain offsets in the buffer.
  1165. *
  1166. * verify if the page is erased or not, and fix up the page for RS ECC by
  1167. * replacing the special characters with 0xff.
  1168. */
  1169. static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
  1170. {
  1171. u8 empty1, empty2;
  1172. /*
  1173. * an erased page flags an error in NAND_FLASH_STATUS, check if the page
  1174. * is erased by looking for 0x54s at offsets 3 and 175 from the
  1175. * beginning of each codeword
  1176. */
  1177. empty1 = data_buf[3];
  1178. empty2 = data_buf[175];
  1179. /*
  1180. * if the erased codework markers, if they exist override them with
  1181. * 0xffs
  1182. */
  1183. if ((empty1 == 0x54 && empty2 == 0xff) ||
  1184. (empty1 == 0xff && empty2 == 0x54)) {
  1185. data_buf[3] = 0xff;
  1186. data_buf[175] = 0xff;
  1187. }
  1188. /*
  1189. * check if the entire chunk contains 0xffs or not. if it doesn't, then
  1190. * restore the original values at the special offsets
  1191. */
  1192. if (memchr_inv(data_buf, 0xff, data_len)) {
  1193. data_buf[3] = empty1;
  1194. data_buf[175] = empty2;
  1195. return false;
  1196. }
  1197. return true;
  1198. }
  1199. struct read_stats {
  1200. __le32 flash;
  1201. __le32 buffer;
  1202. __le32 erased_cw;
  1203. };
  1204. /*
  1205. * reads back status registers set by the controller to notify page read
  1206. * errors. this is equivalent to what 'ecc->correct()' would do.
  1207. */
  1208. static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
  1209. u8 *oob_buf)
  1210. {
  1211. struct nand_chip *chip = &host->chip;
  1212. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1213. struct mtd_info *mtd = nand_to_mtd(chip);
  1214. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1215. unsigned int max_bitflips = 0;
  1216. struct read_stats *buf;
  1217. int i;
  1218. buf = (struct read_stats *)nandc->reg_read_buf;
  1219. nandc_read_buffer_sync(nandc, true);
  1220. for (i = 0; i < ecc->steps; i++, buf++) {
  1221. u32 flash, buffer, erased_cw;
  1222. int data_len, oob_len;
  1223. if (i == (ecc->steps - 1)) {
  1224. data_len = ecc->size - ((ecc->steps - 1) << 2);
  1225. oob_len = ecc->steps << 2;
  1226. } else {
  1227. data_len = host->cw_data;
  1228. oob_len = 0;
  1229. }
  1230. flash = le32_to_cpu(buf->flash);
  1231. buffer = le32_to_cpu(buf->buffer);
  1232. erased_cw = le32_to_cpu(buf->erased_cw);
  1233. if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
  1234. bool erased;
  1235. /* ignore erased codeword errors */
  1236. if (host->bch_enabled) {
  1237. erased = (erased_cw & ERASED_CW) == ERASED_CW ?
  1238. true : false;
  1239. } else {
  1240. erased = erased_chunk_check_and_fixup(data_buf,
  1241. data_len);
  1242. }
  1243. if (erased) {
  1244. data_buf += data_len;
  1245. if (oob_buf)
  1246. oob_buf += oob_len + ecc->bytes;
  1247. continue;
  1248. }
  1249. if (buffer & BS_UNCORRECTABLE_BIT) {
  1250. int ret, ecclen, extraooblen;
  1251. void *eccbuf;
  1252. eccbuf = oob_buf ? oob_buf + oob_len : NULL;
  1253. ecclen = oob_buf ? host->ecc_bytes_hw : 0;
  1254. extraooblen = oob_buf ? oob_len : 0;
  1255. /*
  1256. * make sure it isn't an erased page reported
  1257. * as not-erased by HW because of a few bitflips
  1258. */
  1259. ret = nand_check_erased_ecc_chunk(data_buf,
  1260. data_len, eccbuf, ecclen, oob_buf,
  1261. extraooblen, ecc->strength);
  1262. if (ret < 0) {
  1263. mtd->ecc_stats.failed++;
  1264. } else {
  1265. mtd->ecc_stats.corrected += ret;
  1266. max_bitflips =
  1267. max_t(unsigned int, max_bitflips, ret);
  1268. }
  1269. }
  1270. } else {
  1271. unsigned int stat;
  1272. stat = buffer & BS_CORRECTABLE_ERR_MSK;
  1273. mtd->ecc_stats.corrected += stat;
  1274. max_bitflips = max(max_bitflips, stat);
  1275. }
  1276. data_buf += data_len;
  1277. if (oob_buf)
  1278. oob_buf += oob_len + ecc->bytes;
  1279. }
  1280. return max_bitflips;
  1281. }
  1282. /*
  1283. * helper to perform the actual page read operation, used by ecc->read_page(),
  1284. * ecc->read_oob()
  1285. */
  1286. static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
  1287. u8 *oob_buf)
  1288. {
  1289. struct nand_chip *chip = &host->chip;
  1290. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1291. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1292. int i, ret;
  1293. config_nand_page_read(nandc);
  1294. /* queue cmd descs for each codeword */
  1295. for (i = 0; i < ecc->steps; i++) {
  1296. int data_size, oob_size;
  1297. if (i == (ecc->steps - 1)) {
  1298. data_size = ecc->size - ((ecc->steps - 1) << 2);
  1299. oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
  1300. host->spare_bytes;
  1301. } else {
  1302. data_size = host->cw_data;
  1303. oob_size = host->ecc_bytes_hw + host->spare_bytes;
  1304. }
  1305. if (nandc->props->is_bam) {
  1306. if (data_buf && oob_buf) {
  1307. nandc_set_read_loc(nandc, 0, 0, data_size, 0);
  1308. nandc_set_read_loc(nandc, 1, data_size,
  1309. oob_size, 1);
  1310. } else if (data_buf) {
  1311. nandc_set_read_loc(nandc, 0, 0, data_size, 1);
  1312. } else {
  1313. nandc_set_read_loc(nandc, 0, data_size,
  1314. oob_size, 1);
  1315. }
  1316. }
  1317. config_nand_cw_read(nandc);
  1318. if (data_buf)
  1319. read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
  1320. data_size, 0);
  1321. /*
  1322. * when ecc is enabled, the controller doesn't read the real
  1323. * or dummy bad block markers in each chunk. To maintain a
  1324. * consistent layout across RAW and ECC reads, we just
  1325. * leave the real/dummy BBM offsets empty (i.e, filled with
  1326. * 0xffs)
  1327. */
  1328. if (oob_buf) {
  1329. int j;
  1330. for (j = 0; j < host->bbm_size; j++)
  1331. *oob_buf++ = 0xff;
  1332. read_data_dma(nandc, FLASH_BUF_ACC + data_size,
  1333. oob_buf, oob_size, 0);
  1334. }
  1335. if (data_buf)
  1336. data_buf += data_size;
  1337. if (oob_buf)
  1338. oob_buf += oob_size;
  1339. }
  1340. ret = submit_descs(nandc);
  1341. if (ret)
  1342. dev_err(nandc->dev, "failure to read page/oob\n");
  1343. free_descs(nandc);
  1344. return ret;
  1345. }
  1346. /*
  1347. * a helper that copies the last step/codeword of a page (containing free oob)
  1348. * into our local buffer
  1349. */
  1350. static int copy_last_cw(struct qcom_nand_host *host, int page)
  1351. {
  1352. struct nand_chip *chip = &host->chip;
  1353. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1354. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1355. int size;
  1356. int ret;
  1357. clear_read_regs(nandc);
  1358. size = host->use_ecc ? host->cw_data : host->cw_size;
  1359. /* prepare a clean read buffer */
  1360. memset(nandc->data_buffer, 0xff, size);
  1361. set_address(host, host->cw_size * (ecc->steps - 1), page);
  1362. update_rw_regs(host, 1, true);
  1363. config_nand_single_cw_page_read(nandc);
  1364. read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
  1365. ret = submit_descs(nandc);
  1366. if (ret)
  1367. dev_err(nandc->dev, "failed to copy last codeword\n");
  1368. free_descs(nandc);
  1369. return ret;
  1370. }
  1371. /* implements ecc->read_page() */
  1372. static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  1373. uint8_t *buf, int oob_required, int page)
  1374. {
  1375. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1376. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1377. u8 *data_buf, *oob_buf = NULL;
  1378. int ret;
  1379. data_buf = buf;
  1380. oob_buf = oob_required ? chip->oob_poi : NULL;
  1381. clear_bam_transaction(nandc);
  1382. ret = read_page_ecc(host, data_buf, oob_buf);
  1383. if (ret) {
  1384. dev_err(nandc->dev, "failure to read page\n");
  1385. return ret;
  1386. }
  1387. return parse_read_errors(host, data_buf, oob_buf);
  1388. }
  1389. /* implements ecc->read_page_raw() */
  1390. static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
  1391. struct nand_chip *chip, uint8_t *buf,
  1392. int oob_required, int page)
  1393. {
  1394. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1395. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1396. u8 *data_buf, *oob_buf;
  1397. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1398. int i, ret;
  1399. int read_loc;
  1400. data_buf = buf;
  1401. oob_buf = chip->oob_poi;
  1402. host->use_ecc = false;
  1403. clear_bam_transaction(nandc);
  1404. update_rw_regs(host, ecc->steps, true);
  1405. config_nand_page_read(nandc);
  1406. for (i = 0; i < ecc->steps; i++) {
  1407. int data_size1, data_size2, oob_size1, oob_size2;
  1408. int reg_off = FLASH_BUF_ACC;
  1409. data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
  1410. oob_size1 = host->bbm_size;
  1411. if (i == (ecc->steps - 1)) {
  1412. data_size2 = ecc->size - data_size1 -
  1413. ((ecc->steps - 1) << 2);
  1414. oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
  1415. host->spare_bytes;
  1416. } else {
  1417. data_size2 = host->cw_data - data_size1;
  1418. oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
  1419. }
  1420. if (nandc->props->is_bam) {
  1421. read_loc = 0;
  1422. nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
  1423. read_loc += data_size1;
  1424. nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
  1425. read_loc += oob_size1;
  1426. nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
  1427. read_loc += data_size2;
  1428. nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
  1429. }
  1430. config_nand_cw_read(nandc);
  1431. read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
  1432. reg_off += data_size1;
  1433. data_buf += data_size1;
  1434. read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
  1435. reg_off += oob_size1;
  1436. oob_buf += oob_size1;
  1437. read_data_dma(nandc, reg_off, data_buf, data_size2, 0);
  1438. reg_off += data_size2;
  1439. data_buf += data_size2;
  1440. read_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
  1441. oob_buf += oob_size2;
  1442. }
  1443. ret = submit_descs(nandc);
  1444. if (ret)
  1445. dev_err(nandc->dev, "failure to read raw page\n");
  1446. free_descs(nandc);
  1447. return 0;
  1448. }
  1449. /* implements ecc->read_oob() */
  1450. static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1451. int page)
  1452. {
  1453. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1454. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1455. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1456. int ret;
  1457. clear_read_regs(nandc);
  1458. clear_bam_transaction(nandc);
  1459. host->use_ecc = true;
  1460. set_address(host, 0, page);
  1461. update_rw_regs(host, ecc->steps, true);
  1462. ret = read_page_ecc(host, NULL, chip->oob_poi);
  1463. if (ret)
  1464. dev_err(nandc->dev, "failure to read oob\n");
  1465. return ret;
  1466. }
  1467. /* implements ecc->write_page() */
  1468. static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1469. const uint8_t *buf, int oob_required, int page)
  1470. {
  1471. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1472. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1473. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1474. u8 *data_buf, *oob_buf;
  1475. int i, ret;
  1476. clear_read_regs(nandc);
  1477. clear_bam_transaction(nandc);
  1478. data_buf = (u8 *)buf;
  1479. oob_buf = chip->oob_poi;
  1480. host->use_ecc = true;
  1481. update_rw_regs(host, ecc->steps, false);
  1482. config_nand_page_write(nandc);
  1483. for (i = 0; i < ecc->steps; i++) {
  1484. int data_size, oob_size;
  1485. if (i == (ecc->steps - 1)) {
  1486. data_size = ecc->size - ((ecc->steps - 1) << 2);
  1487. oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
  1488. host->spare_bytes;
  1489. } else {
  1490. data_size = host->cw_data;
  1491. oob_size = ecc->bytes;
  1492. }
  1493. write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
  1494. i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
  1495. /*
  1496. * when ECC is enabled, we don't really need to write anything
  1497. * to oob for the first n - 1 codewords since these oob regions
  1498. * just contain ECC bytes that's written by the controller
  1499. * itself. For the last codeword, we skip the bbm positions and
  1500. * write to the free oob area.
  1501. */
  1502. if (i == (ecc->steps - 1)) {
  1503. oob_buf += host->bbm_size;
  1504. write_data_dma(nandc, FLASH_BUF_ACC + data_size,
  1505. oob_buf, oob_size, 0);
  1506. }
  1507. config_nand_cw_write(nandc);
  1508. data_buf += data_size;
  1509. oob_buf += oob_size;
  1510. }
  1511. ret = submit_descs(nandc);
  1512. if (ret)
  1513. dev_err(nandc->dev, "failure to write page\n");
  1514. free_descs(nandc);
  1515. return ret;
  1516. }
  1517. /* implements ecc->write_page_raw() */
  1518. static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
  1519. struct nand_chip *chip, const uint8_t *buf,
  1520. int oob_required, int page)
  1521. {
  1522. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1523. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1524. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1525. u8 *data_buf, *oob_buf;
  1526. int i, ret;
  1527. clear_read_regs(nandc);
  1528. clear_bam_transaction(nandc);
  1529. data_buf = (u8 *)buf;
  1530. oob_buf = chip->oob_poi;
  1531. host->use_ecc = false;
  1532. update_rw_regs(host, ecc->steps, false);
  1533. config_nand_page_write(nandc);
  1534. for (i = 0; i < ecc->steps; i++) {
  1535. int data_size1, data_size2, oob_size1, oob_size2;
  1536. int reg_off = FLASH_BUF_ACC;
  1537. data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
  1538. oob_size1 = host->bbm_size;
  1539. if (i == (ecc->steps - 1)) {
  1540. data_size2 = ecc->size - data_size1 -
  1541. ((ecc->steps - 1) << 2);
  1542. oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
  1543. host->spare_bytes;
  1544. } else {
  1545. data_size2 = host->cw_data - data_size1;
  1546. oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
  1547. }
  1548. write_data_dma(nandc, reg_off, data_buf, data_size1,
  1549. NAND_BAM_NO_EOT);
  1550. reg_off += data_size1;
  1551. data_buf += data_size1;
  1552. write_data_dma(nandc, reg_off, oob_buf, oob_size1,
  1553. NAND_BAM_NO_EOT);
  1554. reg_off += oob_size1;
  1555. oob_buf += oob_size1;
  1556. write_data_dma(nandc, reg_off, data_buf, data_size2,
  1557. NAND_BAM_NO_EOT);
  1558. reg_off += data_size2;
  1559. data_buf += data_size2;
  1560. write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
  1561. oob_buf += oob_size2;
  1562. config_nand_cw_write(nandc);
  1563. }
  1564. ret = submit_descs(nandc);
  1565. if (ret)
  1566. dev_err(nandc->dev, "failure to write raw page\n");
  1567. free_descs(nandc);
  1568. return ret;
  1569. }
  1570. /*
  1571. * implements ecc->write_oob()
  1572. *
  1573. * the NAND controller cannot write only data or only oob within a codeword,
  1574. * since ecc is calculated for the combined codeword. we first copy the
  1575. * entire contents for the last codeword(data + oob), replace the old oob
  1576. * with the new one in chip->oob_poi, and then write the entire codeword.
  1577. * this read-copy-write operation results in a slight performance loss.
  1578. */
  1579. static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1580. int page)
  1581. {
  1582. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1583. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1584. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1585. u8 *oob = chip->oob_poi;
  1586. int data_size, oob_size;
  1587. int ret, status = 0;
  1588. host->use_ecc = true;
  1589. clear_bam_transaction(nandc);
  1590. ret = copy_last_cw(host, page);
  1591. if (ret)
  1592. return ret;
  1593. clear_read_regs(nandc);
  1594. clear_bam_transaction(nandc);
  1595. /* calculate the data and oob size for the last codeword/step */
  1596. data_size = ecc->size - ((ecc->steps - 1) << 2);
  1597. oob_size = mtd->oobavail;
  1598. /* override new oob content to last codeword */
  1599. mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
  1600. 0, mtd->oobavail);
  1601. set_address(host, host->cw_size * (ecc->steps - 1), page);
  1602. update_rw_regs(host, 1, false);
  1603. config_nand_page_write(nandc);
  1604. write_data_dma(nandc, FLASH_BUF_ACC,
  1605. nandc->data_buffer, data_size + oob_size, 0);
  1606. config_nand_cw_write(nandc);
  1607. ret = submit_descs(nandc);
  1608. free_descs(nandc);
  1609. if (ret) {
  1610. dev_err(nandc->dev, "failure to write oob\n");
  1611. return -EIO;
  1612. }
  1613. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1614. status = chip->waitfunc(mtd, chip);
  1615. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1616. }
  1617. static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
  1618. {
  1619. struct nand_chip *chip = mtd_to_nand(mtd);
  1620. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1621. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1622. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1623. int page, ret, bbpos, bad = 0;
  1624. u32 flash_status;
  1625. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  1626. /*
  1627. * configure registers for a raw sub page read, the address is set to
  1628. * the beginning of the last codeword, we don't care about reading ecc
  1629. * portion of oob. we just want the first few bytes from this codeword
  1630. * that contains the BBM
  1631. */
  1632. host->use_ecc = false;
  1633. clear_bam_transaction(nandc);
  1634. ret = copy_last_cw(host, page);
  1635. if (ret)
  1636. goto err;
  1637. flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
  1638. if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
  1639. dev_warn(nandc->dev, "error when trying to read BBM\n");
  1640. goto err;
  1641. }
  1642. bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
  1643. bad = nandc->data_buffer[bbpos] != 0xff;
  1644. if (chip->options & NAND_BUSWIDTH_16)
  1645. bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
  1646. err:
  1647. return bad;
  1648. }
  1649. static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1650. {
  1651. struct nand_chip *chip = mtd_to_nand(mtd);
  1652. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1653. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1654. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1655. int page, ret, status = 0;
  1656. clear_read_regs(nandc);
  1657. clear_bam_transaction(nandc);
  1658. /*
  1659. * to mark the BBM as bad, we flash the entire last codeword with 0s.
  1660. * we don't care about the rest of the content in the codeword since
  1661. * we aren't going to use this block again
  1662. */
  1663. memset(nandc->data_buffer, 0x00, host->cw_size);
  1664. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  1665. /* prepare write */
  1666. host->use_ecc = false;
  1667. set_address(host, host->cw_size * (ecc->steps - 1), page);
  1668. update_rw_regs(host, 1, false);
  1669. config_nand_page_write(nandc);
  1670. write_data_dma(nandc, FLASH_BUF_ACC,
  1671. nandc->data_buffer, host->cw_size, 0);
  1672. config_nand_cw_write(nandc);
  1673. ret = submit_descs(nandc);
  1674. free_descs(nandc);
  1675. if (ret) {
  1676. dev_err(nandc->dev, "failure to update BBM\n");
  1677. return -EIO;
  1678. }
  1679. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1680. status = chip->waitfunc(mtd, chip);
  1681. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1682. }
  1683. /*
  1684. * the three functions below implement chip->read_byte(), chip->read_buf()
  1685. * and chip->write_buf() respectively. these aren't used for
  1686. * reading/writing page data, they are used for smaller data like reading
  1687. * id, status etc
  1688. */
  1689. static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
  1690. {
  1691. struct nand_chip *chip = mtd_to_nand(mtd);
  1692. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1693. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1694. u8 *buf = nandc->data_buffer;
  1695. u8 ret = 0x0;
  1696. if (host->last_command == NAND_CMD_STATUS) {
  1697. ret = host->status;
  1698. host->status = NAND_STATUS_READY | NAND_STATUS_WP;
  1699. return ret;
  1700. }
  1701. if (nandc->buf_start < nandc->buf_count)
  1702. ret = buf[nandc->buf_start++];
  1703. return ret;
  1704. }
  1705. static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  1706. {
  1707. struct nand_chip *chip = mtd_to_nand(mtd);
  1708. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1709. int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
  1710. memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
  1711. nandc->buf_start += real_len;
  1712. }
  1713. static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  1714. int len)
  1715. {
  1716. struct nand_chip *chip = mtd_to_nand(mtd);
  1717. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1718. int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
  1719. memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
  1720. nandc->buf_start += real_len;
  1721. }
  1722. /* we support only one external chip for now */
  1723. static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
  1724. {
  1725. struct nand_chip *chip = mtd_to_nand(mtd);
  1726. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1727. if (chipnr <= 0)
  1728. return;
  1729. dev_warn(nandc->dev, "invalid chip select\n");
  1730. }
  1731. /*
  1732. * NAND controller page layout info
  1733. *
  1734. * Layout with ECC enabled:
  1735. *
  1736. * |----------------------| |---------------------------------|
  1737. * | xx.......yy| | *********xx.......yy|
  1738. * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
  1739. * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
  1740. * | xx.......yy| | *********xx.......yy|
  1741. * |----------------------| |---------------------------------|
  1742. * codeword 1,2..n-1 codeword n
  1743. * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
  1744. *
  1745. * n = Number of codewords in the page
  1746. * . = ECC bytes
  1747. * * = Spare/free bytes
  1748. * x = Unused byte(s)
  1749. * y = Reserved byte(s)
  1750. *
  1751. * 2K page: n = 4, spare = 16 bytes
  1752. * 4K page: n = 8, spare = 32 bytes
  1753. * 8K page: n = 16, spare = 64 bytes
  1754. *
  1755. * the qcom nand controller operates at a sub page/codeword level. each
  1756. * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
  1757. * the number of ECC bytes vary based on the ECC strength and the bus width.
  1758. *
  1759. * the first n - 1 codewords contains 516 bytes of user data, the remaining
  1760. * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
  1761. * both user data and spare(oobavail) bytes that sum up to 516 bytes.
  1762. *
  1763. * When we access a page with ECC enabled, the reserved bytes(s) are not
  1764. * accessible at all. When reading, we fill up these unreadable positions
  1765. * with 0xffs. When writing, the controller skips writing the inaccessible
  1766. * bytes.
  1767. *
  1768. * Layout with ECC disabled:
  1769. *
  1770. * |------------------------------| |---------------------------------------|
  1771. * | yy xx.......| | bb *********xx.......|
  1772. * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
  1773. * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
  1774. * | yy xx.......| | bb *********xx.......|
  1775. * |------------------------------| |---------------------------------------|
  1776. * codeword 1,2..n-1 codeword n
  1777. * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
  1778. *
  1779. * n = Number of codewords in the page
  1780. * . = ECC bytes
  1781. * * = Spare/free bytes
  1782. * x = Unused byte(s)
  1783. * y = Dummy Bad Bock byte(s)
  1784. * b = Real Bad Block byte(s)
  1785. * size1/size2 = function of codeword size and 'n'
  1786. *
  1787. * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
  1788. * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
  1789. * Block Markers. In the last codeword, this position contains the real BBM
  1790. *
  1791. * In order to have a consistent layout between RAW and ECC modes, we assume
  1792. * the following OOB layout arrangement:
  1793. *
  1794. * |-----------| |--------------------|
  1795. * |yyxx.......| |bb*********xx.......|
  1796. * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
  1797. * |yyxx.......| |bb*********xx.......|
  1798. * |yyxx.......| |bb*********xx.......|
  1799. * |-----------| |--------------------|
  1800. * first n - 1 nth OOB region
  1801. * OOB regions
  1802. *
  1803. * n = Number of codewords in the page
  1804. * . = ECC bytes
  1805. * * = FREE OOB bytes
  1806. * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
  1807. * x = Unused byte(s)
  1808. * b = Real bad block byte(s) (inaccessible when ECC enabled)
  1809. *
  1810. * This layout is read as is when ECC is disabled. When ECC is enabled, the
  1811. * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
  1812. * and assumed as 0xffs when we read a page/oob. The ECC, unused and
  1813. * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
  1814. * the sum of the three).
  1815. */
  1816. static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
  1817. struct mtd_oob_region *oobregion)
  1818. {
  1819. struct nand_chip *chip = mtd_to_nand(mtd);
  1820. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1821. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1822. if (section > 1)
  1823. return -ERANGE;
  1824. if (!section) {
  1825. oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
  1826. host->bbm_size;
  1827. oobregion->offset = 0;
  1828. } else {
  1829. oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
  1830. oobregion->offset = mtd->oobsize - oobregion->length;
  1831. }
  1832. return 0;
  1833. }
  1834. static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
  1835. struct mtd_oob_region *oobregion)
  1836. {
  1837. struct nand_chip *chip = mtd_to_nand(mtd);
  1838. struct qcom_nand_host *host = to_qcom_nand_host(chip);
  1839. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1840. if (section)
  1841. return -ERANGE;
  1842. oobregion->length = ecc->steps * 4;
  1843. oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
  1844. return 0;
  1845. }
  1846. static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
  1847. .ecc = qcom_nand_ooblayout_ecc,
  1848. .free = qcom_nand_ooblayout_free,
  1849. };
  1850. static int qcom_nand_host_setup(struct qcom_nand_host *host)
  1851. {
  1852. struct nand_chip *chip = &host->chip;
  1853. struct mtd_info *mtd = nand_to_mtd(chip);
  1854. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1855. struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
  1856. int cwperpage, bad_block_byte;
  1857. bool wide_bus;
  1858. int ecc_mode = 1;
  1859. /*
  1860. * the controller requires each step consists of 512 bytes of data.
  1861. * bail out if DT has populated a wrong step size.
  1862. */
  1863. if (ecc->size != NANDC_STEP_SIZE) {
  1864. dev_err(nandc->dev, "invalid ecc size\n");
  1865. return -EINVAL;
  1866. }
  1867. wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
  1868. if (ecc->strength >= 8) {
  1869. /* 8 bit ECC defaults to BCH ECC on all platforms */
  1870. host->bch_enabled = true;
  1871. ecc_mode = 1;
  1872. if (wide_bus) {
  1873. host->ecc_bytes_hw = 14;
  1874. host->spare_bytes = 0;
  1875. host->bbm_size = 2;
  1876. } else {
  1877. host->ecc_bytes_hw = 13;
  1878. host->spare_bytes = 2;
  1879. host->bbm_size = 1;
  1880. }
  1881. } else {
  1882. /*
  1883. * if the controller supports BCH for 4 bit ECC, the controller
  1884. * uses lesser bytes for ECC. If RS is used, the ECC bytes is
  1885. * always 10 bytes
  1886. */
  1887. if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
  1888. /* BCH */
  1889. host->bch_enabled = true;
  1890. ecc_mode = 0;
  1891. if (wide_bus) {
  1892. host->ecc_bytes_hw = 8;
  1893. host->spare_bytes = 2;
  1894. host->bbm_size = 2;
  1895. } else {
  1896. host->ecc_bytes_hw = 7;
  1897. host->spare_bytes = 4;
  1898. host->bbm_size = 1;
  1899. }
  1900. } else {
  1901. /* RS */
  1902. host->ecc_bytes_hw = 10;
  1903. if (wide_bus) {
  1904. host->spare_bytes = 0;
  1905. host->bbm_size = 2;
  1906. } else {
  1907. host->spare_bytes = 1;
  1908. host->bbm_size = 1;
  1909. }
  1910. }
  1911. }
  1912. /*
  1913. * we consider ecc->bytes as the sum of all the non-data content in a
  1914. * step. It gives us a clean representation of the oob area (even if
  1915. * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
  1916. * ECC and 12 bytes for 4 bit ECC
  1917. */
  1918. ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
  1919. ecc->read_page = qcom_nandc_read_page;
  1920. ecc->read_page_raw = qcom_nandc_read_page_raw;
  1921. ecc->read_oob = qcom_nandc_read_oob;
  1922. ecc->write_page = qcom_nandc_write_page;
  1923. ecc->write_page_raw = qcom_nandc_write_page_raw;
  1924. ecc->write_oob = qcom_nandc_write_oob;
  1925. ecc->mode = NAND_ECC_HW;
  1926. mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
  1927. cwperpage = mtd->writesize / ecc->size;
  1928. nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
  1929. cwperpage);
  1930. /*
  1931. * DATA_UD_BYTES varies based on whether the read/write command protects
  1932. * spare data with ECC too. We protect spare data by default, so we set
  1933. * it to main + spare data, which are 512 and 4 bytes respectively.
  1934. */
  1935. host->cw_data = 516;
  1936. /*
  1937. * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
  1938. * for 8 bit ECC
  1939. */
  1940. host->cw_size = host->cw_data + ecc->bytes;
  1941. if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
  1942. dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
  1943. return -EINVAL;
  1944. }
  1945. bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
  1946. host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
  1947. | host->cw_data << UD_SIZE_BYTES
  1948. | 0 << DISABLE_STATUS_AFTER_WRITE
  1949. | 5 << NUM_ADDR_CYCLES
  1950. | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
  1951. | 0 << STATUS_BFR_READ
  1952. | 1 << SET_RD_MODE_AFTER_STATUS
  1953. | host->spare_bytes << SPARE_SIZE_BYTES;
  1954. host->cfg1 = 7 << NAND_RECOVERY_CYCLES
  1955. | 0 << CS_ACTIVE_BSY
  1956. | bad_block_byte << BAD_BLOCK_BYTE_NUM
  1957. | 0 << BAD_BLOCK_IN_SPARE_AREA
  1958. | 2 << WR_RD_BSY_GAP
  1959. | wide_bus << WIDE_FLASH
  1960. | host->bch_enabled << ENABLE_BCH_ECC;
  1961. host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
  1962. | host->cw_size << UD_SIZE_BYTES
  1963. | 5 << NUM_ADDR_CYCLES
  1964. | 0 << SPARE_SIZE_BYTES;
  1965. host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
  1966. | 0 << CS_ACTIVE_BSY
  1967. | 17 << BAD_BLOCK_BYTE_NUM
  1968. | 1 << BAD_BLOCK_IN_SPARE_AREA
  1969. | 2 << WR_RD_BSY_GAP
  1970. | wide_bus << WIDE_FLASH
  1971. | 1 << DEV0_CFG1_ECC_DISABLE;
  1972. host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
  1973. | 0 << ECC_SW_RESET
  1974. | host->cw_data << ECC_NUM_DATA_BYTES
  1975. | 1 << ECC_FORCE_CLK_OPEN
  1976. | ecc_mode << ECC_MODE
  1977. | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
  1978. host->ecc_buf_cfg = 0x203 << NUM_STEPS;
  1979. host->clrflashstatus = FS_READY_BSY_N;
  1980. host->clrreadstatus = 0xc0;
  1981. nandc->regs->erased_cw_detect_cfg_clr =
  1982. cpu_to_le32(CLR_ERASED_PAGE_DET);
  1983. nandc->regs->erased_cw_detect_cfg_set =
  1984. cpu_to_le32(SET_ERASED_PAGE_DET);
  1985. dev_dbg(nandc->dev,
  1986. "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",
  1987. host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
  1988. host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
  1989. cwperpage);
  1990. return 0;
  1991. }
  1992. static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
  1993. {
  1994. int ret;
  1995. ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
  1996. if (ret) {
  1997. dev_err(nandc->dev, "failed to set DMA mask\n");
  1998. return ret;
  1999. }
  2000. /*
  2001. * we use the internal buffer for reading ONFI params, reading small
  2002. * data like ID and status, and preforming read-copy-write operations
  2003. * when writing to a codeword partially. 532 is the maximum possible
  2004. * size of a codeword for our nand controller
  2005. */
  2006. nandc->buf_size = 532;
  2007. nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
  2008. GFP_KERNEL);
  2009. if (!nandc->data_buffer)
  2010. return -ENOMEM;
  2011. nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
  2012. GFP_KERNEL);
  2013. if (!nandc->regs)
  2014. return -ENOMEM;
  2015. nandc->reg_read_buf = devm_kzalloc(nandc->dev,
  2016. MAX_REG_RD * sizeof(*nandc->reg_read_buf),
  2017. GFP_KERNEL);
  2018. if (!nandc->reg_read_buf)
  2019. return -ENOMEM;
  2020. if (nandc->props->is_bam) {
  2021. nandc->reg_read_dma =
  2022. dma_map_single(nandc->dev, nandc->reg_read_buf,
  2023. MAX_REG_RD *
  2024. sizeof(*nandc->reg_read_buf),
  2025. DMA_FROM_DEVICE);
  2026. if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
  2027. dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
  2028. return -EIO;
  2029. }
  2030. nandc->tx_chan = dma_request_slave_channel(nandc->dev, "tx");
  2031. if (!nandc->tx_chan) {
  2032. dev_err(nandc->dev, "failed to request tx channel\n");
  2033. return -ENODEV;
  2034. }
  2035. nandc->rx_chan = dma_request_slave_channel(nandc->dev, "rx");
  2036. if (!nandc->rx_chan) {
  2037. dev_err(nandc->dev, "failed to request rx channel\n");
  2038. return -ENODEV;
  2039. }
  2040. nandc->cmd_chan = dma_request_slave_channel(nandc->dev, "cmd");
  2041. if (!nandc->cmd_chan) {
  2042. dev_err(nandc->dev, "failed to request cmd channel\n");
  2043. return -ENODEV;
  2044. }
  2045. /*
  2046. * Initially allocate BAM transaction to read ONFI param page.
  2047. * After detecting all the devices, this BAM transaction will
  2048. * be freed and the next BAM tranasction will be allocated with
  2049. * maximum codeword size
  2050. */
  2051. nandc->max_cwperpage = 1;
  2052. nandc->bam_txn = alloc_bam_transaction(nandc);
  2053. if (!nandc->bam_txn) {
  2054. dev_err(nandc->dev,
  2055. "failed to allocate bam transaction\n");
  2056. return -ENOMEM;
  2057. }
  2058. } else {
  2059. nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
  2060. if (!nandc->chan) {
  2061. dev_err(nandc->dev,
  2062. "failed to request slave channel\n");
  2063. return -ENODEV;
  2064. }
  2065. }
  2066. INIT_LIST_HEAD(&nandc->desc_list);
  2067. INIT_LIST_HEAD(&nandc->host_list);
  2068. nand_hw_control_init(&nandc->controller);
  2069. return 0;
  2070. }
  2071. static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
  2072. {
  2073. if (nandc->props->is_bam) {
  2074. if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
  2075. dma_unmap_single(nandc->dev, nandc->reg_read_dma,
  2076. MAX_REG_RD *
  2077. sizeof(*nandc->reg_read_buf),
  2078. DMA_FROM_DEVICE);
  2079. if (nandc->tx_chan)
  2080. dma_release_channel(nandc->tx_chan);
  2081. if (nandc->rx_chan)
  2082. dma_release_channel(nandc->rx_chan);
  2083. if (nandc->cmd_chan)
  2084. dma_release_channel(nandc->cmd_chan);
  2085. } else {
  2086. if (nandc->chan)
  2087. dma_release_channel(nandc->chan);
  2088. }
  2089. }
  2090. /* one time setup of a few nand controller registers */
  2091. static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
  2092. {
  2093. u32 nand_ctrl;
  2094. /* kill onenand */
  2095. nandc_write(nandc, SFLASHC_BURST_CFG, 0);
  2096. nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
  2097. NAND_DEV_CMD_VLD_VAL);
  2098. /* enable ADM or BAM DMA */
  2099. if (nandc->props->is_bam) {
  2100. nand_ctrl = nandc_read(nandc, NAND_CTRL);
  2101. nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
  2102. } else {
  2103. nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
  2104. }
  2105. /* save the original values of these registers */
  2106. nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
  2107. nandc->vld = NAND_DEV_CMD_VLD_VAL;
  2108. return 0;
  2109. }
  2110. static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
  2111. struct qcom_nand_host *host,
  2112. struct device_node *dn)
  2113. {
  2114. struct nand_chip *chip = &host->chip;
  2115. struct mtd_info *mtd = nand_to_mtd(chip);
  2116. struct device *dev = nandc->dev;
  2117. int ret;
  2118. ret = of_property_read_u32(dn, "reg", &host->cs);
  2119. if (ret) {
  2120. dev_err(dev, "can't get chip-select\n");
  2121. return -ENXIO;
  2122. }
  2123. nand_set_flash_node(chip, dn);
  2124. mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
  2125. mtd->owner = THIS_MODULE;
  2126. mtd->dev.parent = dev;
  2127. chip->cmdfunc = qcom_nandc_command;
  2128. chip->select_chip = qcom_nandc_select_chip;
  2129. chip->read_byte = qcom_nandc_read_byte;
  2130. chip->read_buf = qcom_nandc_read_buf;
  2131. chip->write_buf = qcom_nandc_write_buf;
  2132. chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
  2133. chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
  2134. /*
  2135. * the bad block marker is readable only when we read the last codeword
  2136. * of a page with ECC disabled. currently, the nand_base and nand_bbt
  2137. * helpers don't allow us to read BB from a nand chip with ECC
  2138. * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
  2139. * and block_markbad helpers until we permanently switch to using
  2140. * MTD_OPS_RAW for all drivers (with the help of badblockbits)
  2141. */
  2142. chip->block_bad = qcom_nandc_block_bad;
  2143. chip->block_markbad = qcom_nandc_block_markbad;
  2144. chip->controller = &nandc->controller;
  2145. chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
  2146. NAND_SKIP_BBTSCAN;
  2147. /* set up initial status value */
  2148. host->status = NAND_STATUS_READY | NAND_STATUS_WP;
  2149. ret = nand_scan_ident(mtd, 1, NULL);
  2150. if (ret)
  2151. return ret;
  2152. ret = qcom_nand_host_setup(host);
  2153. return ret;
  2154. }
  2155. static int qcom_nand_mtd_register(struct qcom_nand_controller *nandc,
  2156. struct qcom_nand_host *host,
  2157. struct device_node *dn)
  2158. {
  2159. struct nand_chip *chip = &host->chip;
  2160. struct mtd_info *mtd = nand_to_mtd(chip);
  2161. int ret;
  2162. ret = nand_scan_tail(mtd);
  2163. if (ret)
  2164. return ret;
  2165. ret = mtd_device_register(mtd, NULL, 0);
  2166. if (ret)
  2167. nand_cleanup(mtd_to_nand(mtd));
  2168. return ret;
  2169. }
  2170. static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
  2171. {
  2172. struct device *dev = nandc->dev;
  2173. struct device_node *dn = dev->of_node, *child;
  2174. struct qcom_nand_host *host, *tmp;
  2175. int ret;
  2176. for_each_available_child_of_node(dn, child) {
  2177. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  2178. if (!host) {
  2179. of_node_put(child);
  2180. return -ENOMEM;
  2181. }
  2182. ret = qcom_nand_host_init(nandc, host, child);
  2183. if (ret) {
  2184. devm_kfree(dev, host);
  2185. continue;
  2186. }
  2187. list_add_tail(&host->node, &nandc->host_list);
  2188. }
  2189. if (list_empty(&nandc->host_list))
  2190. return -ENODEV;
  2191. if (nandc->props->is_bam) {
  2192. free_bam_transaction(nandc);
  2193. nandc->bam_txn = alloc_bam_transaction(nandc);
  2194. if (!nandc->bam_txn) {
  2195. dev_err(nandc->dev,
  2196. "failed to allocate bam transaction\n");
  2197. return -ENOMEM;
  2198. }
  2199. }
  2200. list_for_each_entry_safe(host, tmp, &nandc->host_list, node) {
  2201. ret = qcom_nand_mtd_register(nandc, host, child);
  2202. if (ret) {
  2203. list_del(&host->node);
  2204. devm_kfree(dev, host);
  2205. }
  2206. }
  2207. if (list_empty(&nandc->host_list))
  2208. return -ENODEV;
  2209. return 0;
  2210. }
  2211. /* parse custom DT properties here */
  2212. static int qcom_nandc_parse_dt(struct platform_device *pdev)
  2213. {
  2214. struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
  2215. struct device_node *np = nandc->dev->of_node;
  2216. int ret;
  2217. if (!nandc->props->is_bam) {
  2218. ret = of_property_read_u32(np, "qcom,cmd-crci",
  2219. &nandc->cmd_crci);
  2220. if (ret) {
  2221. dev_err(nandc->dev, "command CRCI unspecified\n");
  2222. return ret;
  2223. }
  2224. ret = of_property_read_u32(np, "qcom,data-crci",
  2225. &nandc->data_crci);
  2226. if (ret) {
  2227. dev_err(nandc->dev, "data CRCI unspecified\n");
  2228. return ret;
  2229. }
  2230. }
  2231. return 0;
  2232. }
  2233. static int qcom_nandc_probe(struct platform_device *pdev)
  2234. {
  2235. struct qcom_nand_controller *nandc;
  2236. const void *dev_data;
  2237. struct device *dev = &pdev->dev;
  2238. struct resource *res;
  2239. int ret;
  2240. nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
  2241. if (!nandc)
  2242. return -ENOMEM;
  2243. platform_set_drvdata(pdev, nandc);
  2244. nandc->dev = dev;
  2245. dev_data = of_device_get_match_data(dev);
  2246. if (!dev_data) {
  2247. dev_err(&pdev->dev, "failed to get device data\n");
  2248. return -ENODEV;
  2249. }
  2250. nandc->props = dev_data;
  2251. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2252. nandc->base = devm_ioremap_resource(dev, res);
  2253. if (IS_ERR(nandc->base))
  2254. return PTR_ERR(nandc->base);
  2255. nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
  2256. nandc->core_clk = devm_clk_get(dev, "core");
  2257. if (IS_ERR(nandc->core_clk))
  2258. return PTR_ERR(nandc->core_clk);
  2259. nandc->aon_clk = devm_clk_get(dev, "aon");
  2260. if (IS_ERR(nandc->aon_clk))
  2261. return PTR_ERR(nandc->aon_clk);
  2262. ret = qcom_nandc_parse_dt(pdev);
  2263. if (ret)
  2264. return ret;
  2265. ret = qcom_nandc_alloc(nandc);
  2266. if (ret)
  2267. goto err_core_clk;
  2268. ret = clk_prepare_enable(nandc->core_clk);
  2269. if (ret)
  2270. goto err_core_clk;
  2271. ret = clk_prepare_enable(nandc->aon_clk);
  2272. if (ret)
  2273. goto err_aon_clk;
  2274. ret = qcom_nandc_setup(nandc);
  2275. if (ret)
  2276. goto err_setup;
  2277. ret = qcom_probe_nand_devices(nandc);
  2278. if (ret)
  2279. goto err_setup;
  2280. return 0;
  2281. err_setup:
  2282. clk_disable_unprepare(nandc->aon_clk);
  2283. err_aon_clk:
  2284. clk_disable_unprepare(nandc->core_clk);
  2285. err_core_clk:
  2286. qcom_nandc_unalloc(nandc);
  2287. return ret;
  2288. }
  2289. static int qcom_nandc_remove(struct platform_device *pdev)
  2290. {
  2291. struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
  2292. struct qcom_nand_host *host;
  2293. list_for_each_entry(host, &nandc->host_list, node)
  2294. nand_release(nand_to_mtd(&host->chip));
  2295. qcom_nandc_unalloc(nandc);
  2296. clk_disable_unprepare(nandc->aon_clk);
  2297. clk_disable_unprepare(nandc->core_clk);
  2298. return 0;
  2299. }
  2300. static const struct qcom_nandc_props ipq806x_nandc_props = {
  2301. .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
  2302. .is_bam = false,
  2303. .dev_cmd_reg_start = 0x0,
  2304. };
  2305. static const struct qcom_nandc_props ipq4019_nandc_props = {
  2306. .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
  2307. .is_bam = true,
  2308. .dev_cmd_reg_start = 0x0,
  2309. };
  2310. static const struct qcom_nandc_props ipq8074_nandc_props = {
  2311. .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
  2312. .is_bam = true,
  2313. .dev_cmd_reg_start = 0x7000,
  2314. };
  2315. /*
  2316. * data will hold a struct pointer containing more differences once we support
  2317. * more controller variants
  2318. */
  2319. static const struct of_device_id qcom_nandc_of_match[] = {
  2320. {
  2321. .compatible = "qcom,ipq806x-nand",
  2322. .data = &ipq806x_nandc_props,
  2323. },
  2324. {
  2325. .compatible = "qcom,ipq4019-nand",
  2326. .data = &ipq4019_nandc_props,
  2327. },
  2328. {
  2329. .compatible = "qcom,ipq8074-nand",
  2330. .data = &ipq8074_nandc_props,
  2331. },
  2332. {}
  2333. };
  2334. MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
  2335. static struct platform_driver qcom_nandc_driver = {
  2336. .driver = {
  2337. .name = "qcom-nandc",
  2338. .of_match_table = qcom_nandc_of_match,
  2339. },
  2340. .probe = qcom_nandc_probe,
  2341. .remove = qcom_nandc_remove,
  2342. };
  2343. module_platform_driver(qcom_nandc_driver);
  2344. MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
  2345. MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
  2346. MODULE_LICENSE("GPL v2");