qcom_nandc.c 75 KB

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