162-mtd-nand-Qualcomm-NAND-controller-driver.patch 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. Content-Type: text/plain; charset="utf-8"
  2. MIME-Version: 1.0
  3. Content-Transfer-Encoding: 7bit
  4. Subject: [v3,2/5] mtd: nand: Qualcomm NAND controller driver
  5. From: Archit Taneja <architt@codeaurora.org>
  6. X-Patchwork-Id: 6927101
  7. Message-Id: <1438578498-32254-3-git-send-email-architt@codeaurora.org>
  8. To: linux-mtd@lists.infradead.org, dehrenberg@google.com,
  9. cernekee@gmail.com, computersforpeace@gmail.com
  10. Cc: linux-arm-msm@vger.kernel.org, agross@codeaurora.org,
  11. sboyd@codeaurora.org, linux-kernel@vger.kernel.org,
  12. Archit Taneja <architt@codeaurora.org>
  13. Date: Mon, 3 Aug 2015 10:38:15 +0530
  14. The Qualcomm NAND controller is found in SoCs like IPQ806x, MSM7xx,
  15. MDM9x15 series.
  16. It exists as a sub block inside the IPs EBI2 (External Bus Interface 2)
  17. and QPIC (Qualcomm Parallel Interface Controller). These IPs provide a
  18. broader interface for external slow peripheral devices such as LCD and
  19. NAND/NOR flash memory or SRAM like interfaces.
  20. We add support for the NAND controller found within EBI2. For the SoCs
  21. of our interest, we only use the NAND controller within EBI2. Therefore,
  22. it's safe for us to assume that the NAND controller is a standalone block
  23. within the SoC.
  24. The controller supports 512B, 2kB, 4kB and 8kB page 8-bit and 16-bit NAND
  25. flash devices. It contains a HW ECC block that supports BCH ECC (4, 8 and
  26. 16 bit correction/step) and RS ECC(4 bit correction/step) that covers main
  27. and spare data. The controller contains an internal 512 byte page buffer
  28. to which we read/write via DMA. The EBI2 type NAND controller uses ADM DMA
  29. for register read/write and data transfers. The controller performs page
  30. reads and writes at a codeword/step level of 512 bytes. It can support up
  31. to 2 external chips of different configurations.
  32. The driver prepares register read and write configuration descriptors for
  33. each codeword, followed by data descriptors to read or write data from the
  34. controller's internal buffer. It uses a single ADM DMA channel that we get
  35. via dmaengine API. The controller requires 2 ADM CRCIs for command and
  36. data flow control. These are passed via DT.
  37. The ecc layout used by the controller is syndrome like, but we can't use
  38. the standard syndrome ecc ops because of several reasons. First, the amount
  39. of data bytes covered by ecc isn't same in each step. Second, writing to
  40. free oob space requires us writing to the entire step in which the oob
  41. lies. This forces us to create our own ecc ops.
  42. One more difference is how the controller accesses the bad block marker.
  43. The controller ignores reading the marker when ECC is enabled. ECC needs
  44. to be explicity disabled to read or write to the bad block marker. For
  45. this reason, we use the newly created flag NAND_BBT_ACCESS_BBM_RAW to
  46. read the factory provided bad block markers.
  47. v3:
  48. - Refactor dma functions for maximum reuse
  49. - Use dma_slave_confing on stack
  50. - optimize and clean upempty_page_fixup using memchr_inv
  51. - ensure portability with dma register reads using le32_* funcs
  52. - use NAND_USE_BOUNCE_BUFFER instead of doing it ourselves
  53. - fix handling of return values of dmaengine funcs
  54. - constify wherever possible
  55. - Remove dependency on ADM DMA in Kconfig
  56. - Misc fixes and clean ups
  57. v2:
  58. - Use new BBT flag that allows us to read BBM in raw mode
  59. - reduce memcpy-s in the driver
  60. - some refactor and clean ups because of above changes
  61. Reviewed-by: Andy Gross <agross@codeaurora.org>
  62. Signed-off-by: Archit Taneja <architt@codeaurora.org>
  63. ---
  64. drivers/mtd/nand/Kconfig | 7 +
  65. drivers/mtd/nand/Makefile | 1 +
  66. drivers/mtd/nand/qcom_nandc.c | 1913 +++++++++++++++++++++++++++++++++++++++++
  67. 3 files changed, 1921 insertions(+)
  68. create mode 100644 drivers/mtd/nand/qcom_nandc.c
  69. --- a/drivers/mtd/nand/Kconfig
  70. +++ b/drivers/mtd/nand/Kconfig
  71. @@ -546,4 +546,11 @@ config MTD_NAND_HISI504
  72. help
  73. Enables support for NAND controller on Hisilicon SoC Hip04.
  74. +config MTD_NAND_QCOM
  75. + tristate "Support for NAND on QCOM SoCs"
  76. + depends on ARCH_QCOM
  77. + help
  78. + Enables support for NAND flash chips on SoCs containing the EBI2 NAND
  79. + controller. This controller is found on IPQ806x SoC.
  80. +
  81. endif # MTD_NAND
  82. --- /dev/null
  83. +++ b/drivers/mtd/nand/qcom_nandc.c
  84. @@ -0,0 +1,1918 @@
  85. +/*
  86. + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  87. + *
  88. + * This software is licensed under the terms of the GNU General Public
  89. + * License version 2, as published by the Free Software Foundation, and
  90. + * may be copied, distributed, and modified under those terms.
  91. + *
  92. + * This program is distributed in the hope that it will be useful,
  93. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  94. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  95. + * GNU General Public License for more details.
  96. + */
  97. +
  98. +#include <linux/clk.h>
  99. +#include <linux/slab.h>
  100. +#include <linux/bitops.h>
  101. +#include <linux/dma-mapping.h>
  102. +#include <linux/dmaengine.h>
  103. +#include <linux/module.h>
  104. +#include <linux/mtd/nand.h>
  105. +#include <linux/mtd/partitions.h>
  106. +#include <linux/of.h>
  107. +#include <linux/of_device.h>
  108. +#include <linux/of_mtd.h>
  109. +#include <linux/delay.h>
  110. +
  111. +/* NANDc reg offsets */
  112. +#define NAND_FLASH_CMD 0x00
  113. +#define NAND_ADDR0 0x04
  114. +#define NAND_ADDR1 0x08
  115. +#define NAND_FLASH_CHIP_SELECT 0x0c
  116. +#define NAND_EXEC_CMD 0x10
  117. +#define NAND_FLASH_STATUS 0x14
  118. +#define NAND_BUFFER_STATUS 0x18
  119. +#define NAND_DEV0_CFG0 0x20
  120. +#define NAND_DEV0_CFG1 0x24
  121. +#define NAND_DEV0_ECC_CFG 0x28
  122. +#define NAND_DEV1_ECC_CFG 0x2c
  123. +#define NAND_DEV1_CFG0 0x30
  124. +#define NAND_DEV1_CFG1 0x34
  125. +#define NAND_READ_ID 0x40
  126. +#define NAND_READ_STATUS 0x44
  127. +#define NAND_DEV_CMD0 0xa0
  128. +#define NAND_DEV_CMD1 0xa4
  129. +#define NAND_DEV_CMD2 0xa8
  130. +#define NAND_DEV_CMD_VLD 0xac
  131. +#define SFLASHC_BURST_CFG 0xe0
  132. +#define NAND_ERASED_CW_DETECT_CFG 0xe8
  133. +#define NAND_ERASED_CW_DETECT_STATUS 0xec
  134. +#define NAND_EBI2_ECC_BUF_CFG 0xf0
  135. +#define FLASH_BUF_ACC 0x100
  136. +
  137. +#define NAND_CTRL 0xf00
  138. +#define NAND_VERSION 0xf08
  139. +#define NAND_READ_LOCATION_0 0xf20
  140. +#define NAND_READ_LOCATION_1 0xf24
  141. +
  142. +/* dummy register offsets, used by write_reg_dma */
  143. +#define NAND_DEV_CMD1_RESTORE 0xdead
  144. +#define NAND_DEV_CMD_VLD_RESTORE 0xbeef
  145. +
  146. +/* NAND_FLASH_CMD bits */
  147. +#define PAGE_ACC BIT(4)
  148. +#define LAST_PAGE BIT(5)
  149. +
  150. +/* NAND_FLASH_CHIP_SELECT bits */
  151. +#define NAND_DEV_SEL 0
  152. +#define DM_EN BIT(2)
  153. +
  154. +/* NAND_FLASH_STATUS bits */
  155. +#define FS_OP_ERR BIT(4)
  156. +#define FS_READY_BSY_N BIT(5)
  157. +#define FS_MPU_ERR BIT(8)
  158. +#define FS_DEVICE_STS_ERR BIT(16)
  159. +#define FS_DEVICE_WP BIT(23)
  160. +
  161. +/* NAND_BUFFER_STATUS bits */
  162. +#define BS_UNCORRECTABLE_BIT BIT(8)
  163. +#define BS_CORRECTABLE_ERR_MSK 0x1f
  164. +
  165. +/* NAND_DEVn_CFG0 bits */
  166. +#define DISABLE_STATUS_AFTER_WRITE 4
  167. +#define CW_PER_PAGE 6
  168. +#define UD_SIZE_BYTES 9
  169. +#define ECC_PARITY_SIZE_BYTES_RS 19
  170. +#define SPARE_SIZE_BYTES 23
  171. +#define NUM_ADDR_CYCLES 27
  172. +#define STATUS_BFR_READ 30
  173. +#define SET_RD_MODE_AFTER_STATUS 31
  174. +
  175. +/* NAND_DEVn_CFG0 bits */
  176. +#define DEV0_CFG1_ECC_DISABLE 0
  177. +#define WIDE_FLASH 1
  178. +#define NAND_RECOVERY_CYCLES 2
  179. +#define CS_ACTIVE_BSY 5
  180. +#define BAD_BLOCK_BYTE_NUM 6
  181. +#define BAD_BLOCK_IN_SPARE_AREA 16
  182. +#define WR_RD_BSY_GAP 17
  183. +#define ENABLE_BCH_ECC 27
  184. +
  185. +/* NAND_DEV0_ECC_CFG bits */
  186. +#define ECC_CFG_ECC_DISABLE 0
  187. +#define ECC_SW_RESET 1
  188. +#define ECC_MODE 4
  189. +#define ECC_PARITY_SIZE_BYTES_BCH 8
  190. +#define ECC_NUM_DATA_BYTES 16
  191. +#define ECC_FORCE_CLK_OPEN 30
  192. +
  193. +/* NAND_DEV_CMD1 bits */
  194. +#define READ_ADDR 0
  195. +
  196. +/* NAND_DEV_CMD_VLD bits */
  197. +#define READ_START_VLD 0
  198. +
  199. +/* NAND_EBI2_ECC_BUF_CFG bits */
  200. +#define NUM_STEPS 0
  201. +
  202. +/* NAND_ERASED_CW_DETECT_CFG bits */
  203. +#define ERASED_CW_ECC_MASK 1
  204. +#define AUTO_DETECT_RES 0
  205. +#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
  206. +#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
  207. +#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
  208. +#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
  209. +#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
  210. +
  211. +/* NAND_ERASED_CW_DETECT_STATUS bits */
  212. +#define PAGE_ALL_ERASED BIT(7)
  213. +#define CODEWORD_ALL_ERASED BIT(6)
  214. +#define PAGE_ERASED BIT(5)
  215. +#define CODEWORD_ERASED BIT(4)
  216. +#define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
  217. +#define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
  218. +
  219. +/* Version Mask */
  220. +#define NAND_VERSION_MAJOR_MASK 0xf0000000
  221. +#define NAND_VERSION_MAJOR_SHIFT 28
  222. +#define NAND_VERSION_MINOR_MASK 0x0fff0000
  223. +#define NAND_VERSION_MINOR_SHIFT 16
  224. +
  225. +/* NAND OP_CMDs */
  226. +#define PAGE_READ 0x2
  227. +#define PAGE_READ_WITH_ECC 0x3
  228. +#define PAGE_READ_WITH_ECC_SPARE 0x4
  229. +#define PROGRAM_PAGE 0x6
  230. +#define PAGE_PROGRAM_WITH_ECC 0x7
  231. +#define PROGRAM_PAGE_SPARE 0x9
  232. +#define BLOCK_ERASE 0xa
  233. +#define FETCH_ID 0xb
  234. +#define RESET_DEVICE 0xd
  235. +
  236. +/*
  237. + * the NAND controller performs reads/writes with ECC in 516 byte chunks.
  238. + * the driver calls the chunks 'step' or 'codeword' interchangeably
  239. + */
  240. +#define NANDC_STEP_SIZE 512
  241. +
  242. +/*
  243. + * the largest page size we support is 8K, this will have 16 steps/codewords
  244. + * of 512 bytes each
  245. + */
  246. +#define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
  247. +
  248. +/* we read at most 3 registers per codeword scan */
  249. +#define MAX_REG_RD (3 * MAX_NUM_STEPS)
  250. +
  251. +/* ECC modes */
  252. +#define ECC_NONE BIT(0)
  253. +#define ECC_RS_4BIT BIT(1)
  254. +#define ECC_BCH_4BIT BIT(2)
  255. +#define ECC_BCH_8BIT BIT(3)
  256. +
  257. +struct desc_info {
  258. + struct list_head list;
  259. +
  260. + enum dma_transfer_direction dir;
  261. + struct scatterlist sgl;
  262. + struct dma_async_tx_descriptor *dma_desc;
  263. +};
  264. +
  265. +/*
  266. + * holds the current register values that we want to write. acts as a contiguous
  267. + * chunk of memory which we use to write the controller registers through DMA.
  268. + */
  269. +struct nandc_regs {
  270. + u32 cmd;
  271. + u32 addr0;
  272. + u32 addr1;
  273. + u32 chip_sel;
  274. + u32 exec;
  275. +
  276. + u32 cfg0;
  277. + u32 cfg1;
  278. + u32 ecc_bch_cfg;
  279. +
  280. + u32 clrflashstatus;
  281. + u32 clrreadstatus;
  282. +
  283. + u32 cmd1;
  284. + u32 vld;
  285. +
  286. + u32 orig_cmd1;
  287. + u32 orig_vld;
  288. +
  289. + u32 ecc_buf_cfg;
  290. +};
  291. +
  292. +/*
  293. + * @cmd_crci: ADM DMA CRCI for command flow control
  294. + * @data_crci: ADM DMA CRCI for data flow control
  295. + * @list: DMA descriptor list (list of desc_infos)
  296. + * @dma_done: completion param to denote end of last
  297. + * descriptor in the list
  298. + * @data_buffer: our local DMA buffer for page read/writes,
  299. + * used when we can't use the buffer provided
  300. + * by upper layers directly
  301. + * @buf_size/count/start: markers for chip->read_buf/write_buf functions
  302. + * @reg_read_buf: buffer for reading register data via DMA
  303. + * @reg_read_pos: marker for data read in reg_read_buf
  304. + * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
  305. + * ecc/non-ecc mode for the current nand flash
  306. + * device
  307. + * @regs: a contiguous chunk of memory for DMA register
  308. + * writes
  309. + * @ecc_strength: 4 bit or 8 bit ecc, received via DT
  310. + * @bus_width: 8 bit or 16 bit NAND bus width, received via DT
  311. + * @ecc_modes: supported ECC modes by the current controller,
  312. + * initialized via DT match data
  313. + * @cw_size: the number of bytes in a single step/codeword
  314. + * of a page, consisting of all data, ecc, spare
  315. + * and reserved bytes
  316. + * @cw_data: the number of bytes within a codeword protected
  317. + * by ECC
  318. + * @bch_enabled: flag to tell whether BCH or RS ECC mode is used
  319. + * @status: value to be returned if NAND_CMD_STATUS command
  320. + * is executed
  321. + */
  322. +struct qcom_nandc_data {
  323. + struct platform_device *pdev;
  324. + struct device *dev;
  325. +
  326. + void __iomem *base;
  327. + struct resource *res;
  328. +
  329. + struct clk *core_clk;
  330. + struct clk *aon_clk;
  331. +
  332. + /* DMA stuff */
  333. + struct dma_chan *chan;
  334. + struct dma_slave_config slave_conf;
  335. + unsigned int cmd_crci;
  336. + unsigned int data_crci;
  337. + struct list_head list;
  338. + struct completion dma_done;
  339. +
  340. + /* MTD stuff */
  341. + struct nand_chip chip;
  342. + struct mtd_info mtd;
  343. +
  344. + /* local data buffer and markers */
  345. + u8 *data_buffer;
  346. + int buf_size;
  347. + int buf_count;
  348. + int buf_start;
  349. +
  350. + /* local buffer to read back registers */
  351. + u32 *reg_read_buf;
  352. + int reg_read_pos;
  353. +
  354. + /* required configs */
  355. + u32 cfg0, cfg1;
  356. + u32 cfg0_raw, cfg1_raw;
  357. + u32 ecc_buf_cfg;
  358. + u32 ecc_bch_cfg;
  359. + u32 clrflashstatus;
  360. + u32 clrreadstatus;
  361. + u32 sflashc_burst_cfg;
  362. + u32 cmd1, vld;
  363. +
  364. + /* register state */
  365. + struct nandc_regs *regs;
  366. +
  367. + /* things we get from DT */
  368. + int ecc_strength;
  369. + int bus_width;
  370. +
  371. + u32 ecc_modes;
  372. +
  373. + /* misc params */
  374. + int cw_size;
  375. + int cw_data;
  376. + bool use_ecc;
  377. + bool bch_enabled;
  378. + u8 status;
  379. + int last_command;
  380. +};
  381. +
  382. +static inline u32 nandc_read(struct qcom_nandc_data *this, int offset)
  383. +{
  384. + return ioread32(this->base + offset);
  385. +}
  386. +
  387. +static inline void nandc_write(struct qcom_nandc_data *this, int offset,
  388. + u32 val)
  389. +{
  390. + iowrite32(val, this->base + offset);
  391. +}
  392. +
  393. +/* helper to configure address register values */
  394. +static void set_address(struct qcom_nandc_data *this, u16 column, int page)
  395. +{
  396. + struct nand_chip *chip = &this->chip;
  397. + struct nandc_regs *regs = this->regs;
  398. +
  399. + if (chip->options & NAND_BUSWIDTH_16)
  400. + column >>= 1;
  401. +
  402. + regs->addr0 = page << 16 | column;
  403. + regs->addr1 = page >> 16 & 0xff;
  404. +}
  405. +
  406. +/*
  407. + * update_rw_regs: set up read/write register values, these will be
  408. + * written to the NAND controller registers via DMA
  409. + *
  410. + * @num_cw: number of steps for the read/write operation
  411. + * @read: read or write operation
  412. + */
  413. +static void update_rw_regs(struct qcom_nandc_data *this, int num_cw, bool read)
  414. +{
  415. + struct nandc_regs *regs = this->regs;
  416. +
  417. + if (read) {
  418. + if (this->use_ecc)
  419. + regs->cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
  420. + else
  421. + regs->cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
  422. + } else {
  423. + regs->cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
  424. + }
  425. +
  426. + if (this->use_ecc) {
  427. + regs->cfg0 = (this->cfg0 & ~(7U << CW_PER_PAGE)) |
  428. + (num_cw - 1) << CW_PER_PAGE;
  429. +
  430. + regs->cfg1 = this->cfg1;
  431. + regs->ecc_bch_cfg = this->ecc_bch_cfg;
  432. + } else {
  433. + regs->cfg0 = (this->cfg0_raw & ~(7U << CW_PER_PAGE)) |
  434. + (num_cw - 1) << CW_PER_PAGE;
  435. +
  436. + regs->cfg1 = this->cfg1_raw;
  437. + regs->ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
  438. + }
  439. +
  440. + regs->ecc_buf_cfg = this->ecc_buf_cfg;
  441. + regs->clrflashstatus = this->clrflashstatus;
  442. + regs->clrreadstatus = this->clrreadstatus;
  443. + regs->exec = 1;
  444. +}
  445. +
  446. +static int prep_dma_desc(struct qcom_nandc_data *this, bool read, int reg_off,
  447. + const void *vaddr, int size, bool flow_control)
  448. +{
  449. + struct desc_info *desc;
  450. + struct dma_async_tx_descriptor *dma_desc;
  451. + struct scatterlist *sgl;
  452. + struct dma_slave_config slave_conf;
  453. + int r;
  454. +
  455. + desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  456. + if (!desc)
  457. + return -ENOMEM;
  458. +
  459. + list_add_tail(&desc->list, &this->list);
  460. +
  461. + sgl = &desc->sgl;
  462. +
  463. + sg_init_one(sgl, vaddr, size);
  464. +
  465. + desc->dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  466. +
  467. + r = dma_map_sg(this->dev, sgl, 1, desc->dir);
  468. + if (r == 0) {
  469. + r = -ENOMEM;
  470. + goto err;
  471. + }
  472. +
  473. + memset(&slave_conf, 0x00, sizeof(slave_conf));
  474. +
  475. + slave_conf.device_fc = flow_control;
  476. + if (read) {
  477. + slave_conf.src_maxburst = 16;
  478. + slave_conf.src_addr = this->res->start + reg_off;
  479. + slave_conf.slave_id = this->data_crci;
  480. + } else {
  481. + slave_conf.dst_maxburst = 16;
  482. + slave_conf.dst_addr = this->res->start + reg_off;
  483. + slave_conf.slave_id = this->cmd_crci;
  484. + }
  485. +
  486. + r = dmaengine_slave_config(this->chan, &slave_conf);
  487. + if (r) {
  488. + dev_err(this->dev, "failed to configure dma channel\n");
  489. + goto err;
  490. + }
  491. +
  492. + dma_desc = dmaengine_prep_slave_sg(this->chan, sgl, 1, desc->dir, 0);
  493. + if (!dma_desc) {
  494. + dev_err(this->dev, "failed to prepare desc\n");
  495. + r = -EINVAL;
  496. + goto err;
  497. + }
  498. +
  499. + desc->dma_desc = dma_desc;
  500. +
  501. + return 0;
  502. +err:
  503. + kfree(desc);
  504. +
  505. + return r;
  506. +}
  507. +
  508. +/*
  509. + * read_reg_dma: prepares a descriptor to read a given number of
  510. + * contiguous registers to the reg_read_buf pointer
  511. + *
  512. + * @first: offset of the first register in the contiguous block
  513. + * @num_regs: number of registers to read
  514. + */
  515. +static int read_reg_dma(struct qcom_nandc_data *this, int first, int num_regs)
  516. +{
  517. + bool flow_control = false;
  518. + void *vaddr;
  519. + int size;
  520. +
  521. + if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
  522. + flow_control = true;
  523. +
  524. + size = num_regs * sizeof(u32);
  525. + vaddr = this->reg_read_buf + this->reg_read_pos;
  526. + this->reg_read_pos += num_regs;
  527. +
  528. + return prep_dma_desc(this, true, first, vaddr, size, flow_control);
  529. +}
  530. +
  531. +/*
  532. + * write_reg_dma: prepares a descriptor to write a given number of
  533. + * contiguous registers
  534. + *
  535. + * @first: offset of the first register in the contiguous block
  536. + * @num_regs: number of registers to write
  537. + */
  538. +static int write_reg_dma(struct qcom_nandc_data *this, int first, int num_regs)
  539. +{
  540. + bool flow_control = false;
  541. + struct nandc_regs *regs = this->regs;
  542. + void *vaddr;
  543. + int size;
  544. +
  545. + switch (first) {
  546. + case NAND_FLASH_CMD:
  547. + vaddr = &regs->cmd;
  548. + flow_control = true;
  549. + break;
  550. + case NAND_EXEC_CMD:
  551. + vaddr = &regs->exec;
  552. + break;
  553. + case NAND_FLASH_STATUS:
  554. + vaddr = &regs->clrflashstatus;
  555. + break;
  556. + case NAND_DEV0_CFG0:
  557. + vaddr = &regs->cfg0;
  558. + break;
  559. + case NAND_READ_STATUS:
  560. + vaddr = &regs->clrreadstatus;
  561. + break;
  562. + case NAND_DEV_CMD1:
  563. + vaddr = &regs->cmd1;
  564. + break;
  565. + case NAND_DEV_CMD1_RESTORE:
  566. + first = NAND_DEV_CMD1;
  567. + vaddr = &regs->orig_cmd1;
  568. + break;
  569. + case NAND_DEV_CMD_VLD:
  570. + vaddr = &regs->vld;
  571. + break;
  572. + case NAND_DEV_CMD_VLD_RESTORE:
  573. + first = NAND_DEV_CMD_VLD;
  574. + vaddr = &regs->orig_vld;
  575. + break;
  576. + case NAND_EBI2_ECC_BUF_CFG:
  577. + vaddr = &regs->ecc_buf_cfg;
  578. + break;
  579. + default:
  580. + dev_err(this->dev, "invalid starting register\n");
  581. + return -EINVAL;
  582. + }
  583. +
  584. + size = num_regs * sizeof(u32);
  585. +
  586. + return prep_dma_desc(this, false, first, vaddr, size, flow_control);
  587. +}
  588. +
  589. +/*
  590. + * read_data_dma: prepares a DMA descriptor to transfer data from the
  591. + * controller's internal buffer to the buffer 'vaddr'
  592. + *
  593. + * @reg_off: offset within the controller's data buffer
  594. + * @vaddr: virtual address of the buffer we want to write to
  595. + * @size: DMA transaction size in bytes
  596. + */
  597. +static int read_data_dma(struct qcom_nandc_data *this, int reg_off,
  598. + const u8 *vaddr, int size)
  599. +{
  600. + return prep_dma_desc(this, true, reg_off, vaddr, size, false);
  601. +}
  602. +
  603. +/*
  604. + * write_data_dma: prepares a DMA descriptor to transfer data from
  605. + * 'vaddr' to the controller's internal buffer
  606. + *
  607. + * @reg_off: offset within the controller's data buffer
  608. + * @vaddr: virtual address of the buffer we want to read from
  609. + * @size: DMA transaction size in bytes
  610. + */
  611. +static int write_data_dma(struct qcom_nandc_data *this, int reg_off,
  612. + const u8 *vaddr, int size)
  613. +{
  614. + return prep_dma_desc(this, false, reg_off, vaddr, size, false);
  615. +}
  616. +
  617. +/*
  618. + * helper to prepare dma descriptors to configure registers needed for reading a
  619. + * codeword/step in a page
  620. + */
  621. +static void config_cw_read(struct qcom_nandc_data *this)
  622. +{
  623. + write_reg_dma(this, NAND_FLASH_CMD, 3);
  624. + write_reg_dma(this, NAND_DEV0_CFG0, 3);
  625. + write_reg_dma(this, NAND_EBI2_ECC_BUF_CFG, 1);
  626. +
  627. + write_reg_dma(this, NAND_EXEC_CMD, 1);
  628. +
  629. + read_reg_dma(this, NAND_FLASH_STATUS, 2);
  630. + read_reg_dma(this, NAND_ERASED_CW_DETECT_STATUS, 1);
  631. +}
  632. +
  633. +/*
  634. + * helpers to prepare dma descriptors used to configure registers needed for
  635. + * writing a codeword/step in a page
  636. + */
  637. +static void config_cw_write_pre(struct qcom_nandc_data *this)
  638. +{
  639. + write_reg_dma(this, NAND_FLASH_CMD, 3);
  640. + write_reg_dma(this, NAND_DEV0_CFG0, 3);
  641. + write_reg_dma(this, NAND_EBI2_ECC_BUF_CFG, 1);
  642. +}
  643. +
  644. +static void config_cw_write_post(struct qcom_nandc_data *this)
  645. +{
  646. + write_reg_dma(this, NAND_EXEC_CMD, 1);
  647. +
  648. + read_reg_dma(this, NAND_FLASH_STATUS, 1);
  649. +
  650. + write_reg_dma(this, NAND_FLASH_STATUS, 1);
  651. + write_reg_dma(this, NAND_READ_STATUS, 1);
  652. +}
  653. +
  654. +/*
  655. + * the following functions are used within chip->cmdfunc() to perform different
  656. + * NAND_CMD_* commands
  657. + */
  658. +
  659. +/* sets up descriptors for NAND_CMD_PARAM */
  660. +static int nandc_param(struct qcom_nandc_data *this)
  661. +{
  662. + struct nandc_regs *regs = this->regs;
  663. +
  664. + /*
  665. + * NAND_CMD_PARAM is called before we know much about the FLASH chip
  666. + * in use. we configure the controller to perform a raw read of 512
  667. + * bytes to read onfi params
  668. + */
  669. + regs->cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
  670. + regs->addr0 = 0;
  671. + regs->addr1 = 0;
  672. + regs->cfg0 = 0 << CW_PER_PAGE
  673. + | 512 << UD_SIZE_BYTES
  674. + | 5 << NUM_ADDR_CYCLES
  675. + | 0 << SPARE_SIZE_BYTES;
  676. +
  677. + regs->cfg1 = 7 << NAND_RECOVERY_CYCLES
  678. + | 0 << CS_ACTIVE_BSY
  679. + | 17 << BAD_BLOCK_BYTE_NUM
  680. + | 1 << BAD_BLOCK_IN_SPARE_AREA
  681. + | 2 << WR_RD_BSY_GAP
  682. + | 0 << WIDE_FLASH
  683. + | 1 << DEV0_CFG1_ECC_DISABLE;
  684. +
  685. + regs->ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
  686. +
  687. + /* configure CMD1 and VLD for ONFI param probing */
  688. + regs->vld = (this->vld & ~(1 << READ_START_VLD))
  689. + | 0 << READ_START_VLD;
  690. +
  691. + regs->cmd1 = (this->cmd1 & ~(0xFF << READ_ADDR))
  692. + | NAND_CMD_PARAM << READ_ADDR;
  693. +
  694. + regs->exec = 1;
  695. +
  696. + regs->orig_cmd1 = this->cmd1;
  697. + regs->orig_vld = this->vld;
  698. +
  699. + write_reg_dma(this, NAND_DEV_CMD_VLD, 1);
  700. + write_reg_dma(this, NAND_DEV_CMD1, 1);
  701. +
  702. + this->buf_count = 512;
  703. + memset(this->data_buffer, 0xff, this->buf_count);
  704. +
  705. + config_cw_read(this);
  706. +
  707. + read_data_dma(this, FLASH_BUF_ACC, this->data_buffer, this->buf_count);
  708. +
  709. + /* restore CMD1 and VLD regs */
  710. + write_reg_dma(this, NAND_DEV_CMD1_RESTORE, 1);
  711. + write_reg_dma(this, NAND_DEV_CMD_VLD_RESTORE, 1);
  712. +
  713. + return 0;
  714. +}
  715. +
  716. +/* sets up descriptors for NAND_CMD_ERASE1 */
  717. +static int erase_block(struct qcom_nandc_data *this, int page_addr)
  718. +{
  719. + struct nandc_regs *regs = this->regs;
  720. +
  721. + regs->cmd = BLOCK_ERASE | PAGE_ACC | LAST_PAGE;
  722. + regs->addr0 = page_addr;
  723. + regs->addr1 = 0;
  724. + regs->cfg0 = this->cfg0_raw & ~(7 << CW_PER_PAGE);
  725. + regs->cfg1 = this->cfg1_raw;
  726. + regs->exec = 1;
  727. + regs->clrflashstatus = this->clrflashstatus;
  728. + regs->clrreadstatus = this->clrreadstatus;
  729. +
  730. + write_reg_dma(this, NAND_FLASH_CMD, 3);
  731. + write_reg_dma(this, NAND_DEV0_CFG0, 2);
  732. + write_reg_dma(this, NAND_EXEC_CMD, 1);
  733. +
  734. + read_reg_dma(this, NAND_FLASH_STATUS, 1);
  735. +
  736. + write_reg_dma(this, NAND_FLASH_STATUS, 1);
  737. + write_reg_dma(this, NAND_READ_STATUS, 1);
  738. +
  739. + return 0;
  740. +}
  741. +
  742. +/* sets up descriptors for NAND_CMD_READID */
  743. +static int read_id(struct qcom_nandc_data *this, int column)
  744. +{
  745. + struct nandc_regs *regs = this->regs;
  746. +
  747. + if (column == -1)
  748. + return 0;
  749. +
  750. + regs->cmd = FETCH_ID;
  751. + regs->addr0 = column;
  752. + regs->addr1 = 0;
  753. + regs->chip_sel = DM_EN;
  754. + regs->exec = 1;
  755. +
  756. + write_reg_dma(this, NAND_FLASH_CMD, 4);
  757. + write_reg_dma(this, NAND_EXEC_CMD, 1);
  758. +
  759. + read_reg_dma(this, NAND_READ_ID, 1);
  760. +
  761. + return 0;
  762. +}
  763. +
  764. +/* sets up descriptors for NAND_CMD_RESET */
  765. +static int reset(struct qcom_nandc_data *this)
  766. +{
  767. + struct nandc_regs *regs = this->regs;
  768. +
  769. + regs->cmd = RESET_DEVICE;
  770. + regs->exec = 1;
  771. +
  772. + write_reg_dma(this, NAND_FLASH_CMD, 1);
  773. + write_reg_dma(this, NAND_EXEC_CMD, 1);
  774. +
  775. + read_reg_dma(this, NAND_FLASH_STATUS, 1);
  776. +
  777. + return 0;
  778. +}
  779. +
  780. +/* helpers to submit/free our list of dma descriptors */
  781. +static void dma_callback(void *param)
  782. +{
  783. + struct qcom_nandc_data *this = param;
  784. + struct completion *c = &this->dma_done;
  785. +
  786. + complete(c);
  787. +}
  788. +
  789. +static int submit_descs(struct qcom_nandc_data *this)
  790. +{
  791. + struct completion *c = &this->dma_done;
  792. + struct desc_info *desc;
  793. + int r;
  794. +
  795. + init_completion(c);
  796. +
  797. + list_for_each_entry(desc, &this->list, list) {
  798. + /*
  799. + * we add a callback to the last descriptor in our list to
  800. + * notify completion of command
  801. + */
  802. + if (list_is_last(&desc->list, &this->list)) {
  803. + desc->dma_desc->callback = dma_callback;
  804. + desc->dma_desc->callback_param = this;
  805. + }
  806. +
  807. + dmaengine_submit(desc->dma_desc);
  808. + }
  809. +
  810. + dma_async_issue_pending(this->chan);
  811. +
  812. + r = wait_for_completion_timeout(c, msecs_to_jiffies(500));
  813. + if (!r)
  814. + return -ETIMEDOUT;
  815. +
  816. + return 0;
  817. +}
  818. +
  819. +static void free_descs(struct qcom_nandc_data *this)
  820. +{
  821. + struct desc_info *desc, *n;
  822. +
  823. + list_for_each_entry_safe(desc, n, &this->list, list) {
  824. + list_del(&desc->list);
  825. + dma_unmap_sg(this->dev, &desc->sgl, 1, desc->dir);
  826. + kfree(desc);
  827. + }
  828. +}
  829. +
  830. +/* reset the register read buffer for next NAND operation */
  831. +static void clear_read_regs(struct qcom_nandc_data *this)
  832. +{
  833. + this->reg_read_pos = 0;
  834. + memset(this->reg_read_buf, 0, MAX_REG_RD * sizeof(*this->reg_read_buf));
  835. +}
  836. +
  837. +static void pre_command(struct qcom_nandc_data *this, int command)
  838. +{
  839. + this->buf_count = 0;
  840. + this->buf_start = 0;
  841. + this->use_ecc = false;
  842. + this->last_command = command;
  843. +
  844. + clear_read_regs(this);
  845. +}
  846. +
  847. +/*
  848. + * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
  849. + * privately maintained status byte, this status byte can be read after
  850. + * NAND_CMD_STATUS is called
  851. + */
  852. +static void parse_erase_write_errors(struct qcom_nandc_data *this, int command)
  853. +{
  854. + struct nand_chip *chip = &this->chip;
  855. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  856. + int num_cw;
  857. + int i;
  858. +
  859. + num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
  860. +
  861. + for (i = 0; i < num_cw; i++) {
  862. + __le32 flash_status = le32_to_cpu(this->reg_read_buf[i]);
  863. +
  864. + if (flash_status & FS_MPU_ERR)
  865. + this->status &= ~NAND_STATUS_WP;
  866. +
  867. + if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
  868. + (flash_status & FS_DEVICE_STS_ERR)))
  869. + this->status |= NAND_STATUS_FAIL;
  870. + }
  871. +}
  872. +
  873. +static void post_command(struct qcom_nandc_data *this, int command)
  874. +{
  875. + switch (command) {
  876. + case NAND_CMD_READID:
  877. + memcpy(this->data_buffer, this->reg_read_buf, this->buf_count);
  878. + break;
  879. + case NAND_CMD_PAGEPROG:
  880. + case NAND_CMD_ERASE1:
  881. + parse_erase_write_errors(this, command);
  882. + break;
  883. + default:
  884. + break;
  885. + }
  886. +}
  887. +
  888. +/*
  889. + * Implements chip->cmdfunc. It's only used for a limited set of commands.
  890. + * The rest of the commands wouldn't be called by upper layers. For example,
  891. + * NAND_CMD_READOOB would never be called because we have our own versions
  892. + * of read_oob ops for nand_ecc_ctrl.
  893. + */
  894. +static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
  895. + int column, int page_addr)
  896. +{
  897. + struct nand_chip *chip = mtd->priv;
  898. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  899. + struct qcom_nandc_data *this = chip->priv;
  900. + bool wait = false;
  901. + int r = 0;
  902. +
  903. + pre_command(this, command);
  904. +
  905. + switch (command) {
  906. + case NAND_CMD_RESET:
  907. + r = reset(this);
  908. + wait = true;
  909. + break;
  910. +
  911. + case NAND_CMD_READID:
  912. + this->buf_count = 4;
  913. + r = read_id(this, column);
  914. + wait = true;
  915. + break;
  916. +
  917. + case NAND_CMD_PARAM:
  918. + r = nandc_param(this);
  919. + wait = true;
  920. + break;
  921. +
  922. + case NAND_CMD_ERASE1:
  923. + r = erase_block(this, page_addr);
  924. + wait = true;
  925. + break;
  926. +
  927. + case NAND_CMD_READ0:
  928. + /* we read the entire page for now */
  929. + WARN_ON(column != 0);
  930. +
  931. + this->use_ecc = true;
  932. + set_address(this, 0, page_addr);
  933. + update_rw_regs(this, ecc->steps, true);
  934. + break;
  935. +
  936. + case NAND_CMD_SEQIN:
  937. + WARN_ON(column != 0);
  938. + set_address(this, 0, page_addr);
  939. + break;
  940. +
  941. + case NAND_CMD_PAGEPROG:
  942. + case NAND_CMD_STATUS:
  943. + case NAND_CMD_NONE:
  944. + default:
  945. + break;
  946. + }
  947. +
  948. + if (r) {
  949. + dev_err(this->dev, "failure executing command %d\n",
  950. + command);
  951. + free_descs(this);
  952. + return;
  953. + }
  954. +
  955. + if (wait) {
  956. + r = submit_descs(this);
  957. + if (r)
  958. + dev_err(this->dev,
  959. + "failure submitting descs for command %d\n",
  960. + command);
  961. + }
  962. +
  963. + free_descs(this);
  964. +
  965. + post_command(this, command);
  966. +}
  967. +
  968. +/*
  969. + * when using RS ECC, the NAND controller flags an error when reading an
  970. + * erased page. however, there are special characters at certain offsets when
  971. + * we read the erased page. we check here if the page is really empty. if so,
  972. + * we replace the magic characters with 0xffs
  973. + */
  974. +static bool empty_page_fixup(struct qcom_nandc_data *this, u8 *data_buf)
  975. +{
  976. + struct mtd_info *mtd = &this->mtd;
  977. + struct nand_chip *chip = &this->chip;
  978. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  979. + int cwperpage = ecc->steps;
  980. + u8 orig1[MAX_NUM_STEPS], orig2[MAX_NUM_STEPS];
  981. + int i, j;
  982. +
  983. + /* if BCH is enabled, HW will take care of detecting erased pages */
  984. + if (this->bch_enabled || !this->use_ecc)
  985. + return false;
  986. +
  987. + for (i = 0; i < cwperpage; i++) {
  988. + u8 *empty1, *empty2;
  989. + __le32 flash_status = le32_to_cpu(this->reg_read_buf[3 * i]);
  990. +
  991. + /*
  992. + * an erased page flags an error in NAND_FLASH_STATUS, check if
  993. + * the page is erased by looking for 0x54s at offsets 3 and 175
  994. + * from the beginning of each codeword
  995. + */
  996. + if (!(flash_status & FS_OP_ERR))
  997. + break;
  998. +
  999. + empty1 = &data_buf[3 + i * this->cw_data];
  1000. + empty2 = &data_buf[175 + i * this->cw_data];
  1001. +
  1002. + /*
  1003. + * if the error wasn't because of an erased page, bail out and
  1004. + * and let someone else do the error checking
  1005. + */
  1006. + if ((*empty1 == 0x54 && *empty2 == 0xff) ||
  1007. + (*empty1 == 0xff && *empty2 == 0x54)) {
  1008. + orig1[i] = *empty1;
  1009. + orig2[i] = *empty2;
  1010. +
  1011. + *empty1 = 0xff;
  1012. + *empty2 = 0xff;
  1013. + } else {
  1014. + break;
  1015. + }
  1016. + }
  1017. +
  1018. + if (i < cwperpage || memchr_inv(data_buf, 0xff, mtd->writesize))
  1019. + goto not_empty;
  1020. +
  1021. + /*
  1022. + * tell the caller that the page was empty and is fixed up, so that
  1023. + * parse_read_errors() doesn't think it's an error
  1024. + */
  1025. + return true;
  1026. +
  1027. +not_empty:
  1028. + /* restore original values if not empty*/
  1029. + for (j = 0; j < i; j++) {
  1030. + data_buf[3 + j * this->cw_data] = orig1[j];
  1031. + data_buf[175 + j * this->cw_data] = orig2[j];
  1032. + }
  1033. +
  1034. + return false;
  1035. +}
  1036. +
  1037. +struct read_stats {
  1038. + __le32 flash;
  1039. + __le32 buffer;
  1040. + __le32 erased_cw;
  1041. +};
  1042. +
  1043. +/*
  1044. + * reads back status registers set by the controller to notify page read
  1045. + * errors. this is equivalent to what 'ecc->correct()' would do.
  1046. + */
  1047. +static int parse_read_errors(struct qcom_nandc_data *this, bool erased_page)
  1048. +{
  1049. + struct mtd_info *mtd = &this->mtd;
  1050. + struct nand_chip *chip = &this->chip;
  1051. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1052. + int cwperpage = ecc->steps;
  1053. + unsigned int max_bitflips = 0;
  1054. + int i;
  1055. +
  1056. + for (i = 0; i < cwperpage; i++) {
  1057. + int stat;
  1058. + struct read_stats *buf;
  1059. +
  1060. + buf = (struct read_stats *) (this->reg_read_buf + 3 * i);
  1061. +
  1062. + buf->flash = le32_to_cpu(buf->flash);
  1063. + buf->buffer = le32_to_cpu(buf->buffer);
  1064. + buf->erased_cw = le32_to_cpu(buf->erased_cw);
  1065. +
  1066. + if (buf->flash & (FS_OP_ERR | FS_MPU_ERR)) {
  1067. +
  1068. + /* ignore erased codeword errors */
  1069. + if (this->bch_enabled) {
  1070. + if ((buf->erased_cw & ERASED_CW) == ERASED_CW)
  1071. + continue;
  1072. + } else if (erased_page) {
  1073. + continue;
  1074. + }
  1075. +
  1076. + if (buf->buffer & BS_UNCORRECTABLE_BIT) {
  1077. + mtd->ecc_stats.failed++;
  1078. + continue;
  1079. + }
  1080. + }
  1081. +
  1082. + stat = buf->buffer & BS_CORRECTABLE_ERR_MSK;
  1083. + mtd->ecc_stats.corrected += stat;
  1084. +
  1085. + max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1086. + }
  1087. +
  1088. + return max_bitflips;
  1089. +}
  1090. +
  1091. +/*
  1092. + * helper to perform the actual page read operation, used by ecc->read_page()
  1093. + * and ecc->read_oob()
  1094. + */
  1095. +static int read_page_low(struct qcom_nandc_data *this, u8 *data_buf,
  1096. + u8 *oob_buf)
  1097. +{
  1098. + struct nand_chip *chip = &this->chip;
  1099. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1100. + int i, r;
  1101. +
  1102. + /* queue cmd descs for each codeword */
  1103. + for (i = 0; i < ecc->steps; i++) {
  1104. + int data_size, oob_size;
  1105. +
  1106. + if (i == (ecc->steps - 1)) {
  1107. + data_size = ecc->size - ((ecc->steps - 1) << 2);
  1108. + oob_size = (ecc->steps << 2) + ecc->bytes;
  1109. + } else {
  1110. + data_size = this->cw_data;
  1111. + oob_size = ecc->bytes;
  1112. + }
  1113. +
  1114. + config_cw_read(this);
  1115. +
  1116. + if (data_buf)
  1117. + read_data_dma(this, FLASH_BUF_ACC, data_buf, data_size);
  1118. +
  1119. + if (oob_buf)
  1120. + read_data_dma(this, FLASH_BUF_ACC + data_size, oob_buf,
  1121. + oob_size);
  1122. +
  1123. + if (data_buf)
  1124. + data_buf += data_size;
  1125. + if (oob_buf)
  1126. + oob_buf += oob_size;
  1127. + }
  1128. +
  1129. + r = submit_descs(this);
  1130. + if (r)
  1131. + dev_err(this->dev, "failure to read page/oob\n");
  1132. +
  1133. + free_descs(this);
  1134. +
  1135. + return r;
  1136. +}
  1137. +
  1138. +/*
  1139. + * a helper that copies the last step/codeword of a page (containing free oob)
  1140. + * into our local buffer
  1141. + */
  1142. +static int copy_last_cw(struct qcom_nandc_data *this, bool use_ecc, int page)
  1143. +{
  1144. + struct nand_chip *chip = &this->chip;
  1145. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1146. + int size;
  1147. + int r;
  1148. +
  1149. + clear_read_regs(this);
  1150. +
  1151. + size = use_ecc ? this->cw_data : this->cw_size;
  1152. +
  1153. + /* prepare a clean read buffer */
  1154. + memset(this->data_buffer, 0xff, size);
  1155. +
  1156. + this->use_ecc = use_ecc;
  1157. + set_address(this, this->cw_size * (ecc->steps - 1), page);
  1158. + update_rw_regs(this, 1, true);
  1159. +
  1160. + config_cw_read(this);
  1161. +
  1162. + read_data_dma(this, FLASH_BUF_ACC, this->data_buffer, size);
  1163. +
  1164. + r = submit_descs(this);
  1165. + if (r)
  1166. + dev_err(this->dev, "failed to copy last codeword\n");
  1167. +
  1168. + free_descs(this);
  1169. +
  1170. + return r;
  1171. +}
  1172. +
  1173. +/* implements ecc->read_page() */
  1174. +static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  1175. + uint8_t *buf, int oob_required, int page)
  1176. +{
  1177. + struct qcom_nandc_data *this = chip->priv;
  1178. + u8 *data_buf, *oob_buf = NULL;
  1179. + bool erased_page;
  1180. + int r;
  1181. +
  1182. + data_buf = buf;
  1183. + oob_buf = oob_required ? chip->oob_poi : NULL;
  1184. +
  1185. + r = read_page_low(this, data_buf, oob_buf);
  1186. + if (r) {
  1187. + dev_err(this->dev, "failure to read page\n");
  1188. + return r;
  1189. + }
  1190. +
  1191. + erased_page = empty_page_fixup(this, data_buf);
  1192. +
  1193. + return parse_read_errors(this, erased_page);
  1194. +}
  1195. +
  1196. +/* implements ecc->read_oob() */
  1197. +static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1198. + int page)
  1199. +{
  1200. + struct qcom_nandc_data *this = chip->priv;
  1201. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1202. + int r;
  1203. +
  1204. + clear_read_regs(this);
  1205. +
  1206. + this->use_ecc = true;
  1207. + set_address(this, 0, page);
  1208. + update_rw_regs(this, ecc->steps, true);
  1209. +
  1210. + r = read_page_low(this, NULL, chip->oob_poi);
  1211. + if (r)
  1212. + dev_err(this->dev, "failure to read oob\n");
  1213. +
  1214. + return r;
  1215. +}
  1216. +
  1217. +/* implements ecc->read_oob_raw(), used to read the bad block marker flag */
  1218. +static int qcom_nandc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1219. + int page)
  1220. +{
  1221. + struct qcom_nandc_data *this = chip->priv;
  1222. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1223. + uint8_t *oob = chip->oob_poi;
  1224. + int start, length;
  1225. + int r;
  1226. +
  1227. + /*
  1228. + * configure registers for a raw page read, the address is set to the
  1229. + * beginning of the last codeword, we don't care about reading ecc
  1230. + * portion of oob, just the free stuff
  1231. + */
  1232. + r = copy_last_cw(this, false, page);
  1233. + if (r)
  1234. + return r;
  1235. +
  1236. + /*
  1237. + * reading raw oob has 2 parts, first the bad block byte, then the
  1238. + * actual free oob region. perform a memcpy in two steps
  1239. + */
  1240. + start = mtd->writesize - (this->cw_size * (ecc->steps - 1));
  1241. + length = chip->options & NAND_BUSWIDTH_16 ? 2 : 1;
  1242. +
  1243. + memcpy(oob, this->data_buffer + start, length);
  1244. +
  1245. + oob += length;
  1246. +
  1247. + start = this->cw_data - (ecc->steps << 2) + 1;
  1248. + length = ecc->steps << 2;
  1249. +
  1250. + memcpy(oob, this->data_buffer + start, length);
  1251. +
  1252. + return 0;
  1253. +}
  1254. +
  1255. +/* implements ecc->write_page() */
  1256. +static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1257. + const uint8_t *buf, int oob_required)
  1258. +{
  1259. + struct qcom_nandc_data *this = chip->priv;
  1260. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1261. + u8 *data_buf, *oob_buf;
  1262. + int i, r = 0;
  1263. +
  1264. + clear_read_regs(this);
  1265. +
  1266. + data_buf = (u8 *) buf;
  1267. + oob_buf = chip->oob_poi;
  1268. +
  1269. + this->use_ecc = true;
  1270. + update_rw_regs(this, ecc->steps, false);
  1271. +
  1272. + for (i = 0; i < ecc->steps; i++) {
  1273. + int data_size, oob_size;
  1274. +
  1275. + if (i == (ecc->steps - 1)) {
  1276. + data_size = ecc->size - ((ecc->steps - 1) << 2);
  1277. + oob_size = (ecc->steps << 2) + ecc->bytes;
  1278. + } else {
  1279. + data_size = this->cw_data;
  1280. + oob_size = ecc->bytes;
  1281. + }
  1282. +
  1283. + config_cw_write_pre(this);
  1284. + write_data_dma(this, FLASH_BUF_ACC, data_buf, data_size);
  1285. +
  1286. + /*
  1287. + * we don't really need to write anything to oob for the
  1288. + * first n - 1 codewords since these oob regions just
  1289. + * contain ecc that's written by the controller itself
  1290. + */
  1291. + if (i == (ecc->steps - 1))
  1292. + write_data_dma(this, FLASH_BUF_ACC + data_size,
  1293. + oob_buf, oob_size);
  1294. + config_cw_write_post(this);
  1295. +
  1296. + data_buf += data_size;
  1297. + oob_buf += oob_size;
  1298. + }
  1299. +
  1300. + r = submit_descs(this);
  1301. + if (r)
  1302. + dev_err(this->dev, "failure to write page\n");
  1303. +
  1304. + free_descs(this);
  1305. +
  1306. + return r;
  1307. +}
  1308. +
  1309. +/*
  1310. + * implements ecc->write_oob()
  1311. + *
  1312. + * the NAND controller cannot write only data or only oob within a codeword,
  1313. + * since ecc is calculated for the combined codeword. we first copy the
  1314. + * entire contents for the last codeword(data + oob), replace the old oob
  1315. + * with the new one in chip->oob_poi, and then write the entire codeword.
  1316. + * this read-copy-write operation results in a slight perormance loss.
  1317. + */
  1318. +static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1319. + int page)
  1320. +{
  1321. + struct qcom_nandc_data *this = chip->priv;
  1322. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1323. + uint8_t *oob = chip->oob_poi;
  1324. + int free_boff;
  1325. + int data_size, oob_size;
  1326. + int r, status = 0;
  1327. +
  1328. + r = copy_last_cw(this, true, page);
  1329. + if (r)
  1330. + return r;
  1331. +
  1332. + clear_read_regs(this);
  1333. +
  1334. + /* calculate the data and oob size for the last codeword/step */
  1335. + data_size = ecc->size - ((ecc->steps - 1) << 2);
  1336. + oob_size = (ecc->steps << 2) + ecc->bytes;
  1337. +
  1338. + /*
  1339. + * the location of spare data in the oob buffer, we could also use
  1340. + * ecc->layout.oobfree here
  1341. + */
  1342. + free_boff = ecc->bytes * (ecc->steps - 1);
  1343. +
  1344. + /* override new oob content to last codeword */
  1345. + memcpy(this->data_buffer + data_size, oob + free_boff, oob_size);
  1346. +
  1347. + this->use_ecc = true;
  1348. + set_address(this, this->cw_size * (ecc->steps - 1), page);
  1349. + update_rw_regs(this, 1, false);
  1350. +
  1351. + config_cw_write_pre(this);
  1352. + write_data_dma(this, FLASH_BUF_ACC, this->data_buffer,
  1353. + data_size + oob_size);
  1354. + config_cw_write_post(this);
  1355. +
  1356. + r = submit_descs(this);
  1357. +
  1358. + free_descs(this);
  1359. +
  1360. + if (r) {
  1361. + dev_err(this->dev, "failure to write oob\n");
  1362. + return -EIO;
  1363. + }
  1364. +
  1365. + chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1366. +
  1367. + status = chip->waitfunc(mtd, chip);
  1368. +
  1369. + return status & NAND_STATUS_FAIL ? -EIO : 0;
  1370. +}
  1371. +
  1372. +/* implements ecc->write_oob_raw(), used to write bad block marker flag */
  1373. +static int qcom_nandc_write_oob_raw(struct mtd_info *mtd,
  1374. + struct nand_chip *chip, int page)
  1375. +{
  1376. + struct qcom_nandc_data *this = chip->priv;
  1377. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1378. + uint8_t *oob = chip->oob_poi;
  1379. + int start, length;
  1380. + int r, status = 0;
  1381. +
  1382. + r = copy_last_cw(this, false, page);
  1383. + if (r)
  1384. + return r;
  1385. +
  1386. + clear_read_regs(this);
  1387. +
  1388. + /*
  1389. + * writing raw oob has 2 parts, first the bad block region, then the
  1390. + * actual free region
  1391. + */
  1392. + start = mtd->writesize - (this->cw_size * (ecc->steps - 1));
  1393. + length = chip->options & NAND_BUSWIDTH_16 ? 2 : 1;
  1394. +
  1395. + memcpy(this->data_buffer + start, oob, length);
  1396. +
  1397. + oob += length;
  1398. +
  1399. + start = this->cw_data - (ecc->steps << 2) + 1;
  1400. + length = ecc->steps << 2;
  1401. +
  1402. + memcpy(this->data_buffer + start, oob, length);
  1403. +
  1404. + /* prepare write */
  1405. + this->use_ecc = false;
  1406. + set_address(this, this->cw_size * (ecc->steps - 1), page);
  1407. + update_rw_regs(this, 1, false);
  1408. +
  1409. + config_cw_write_pre(this);
  1410. + write_data_dma(this, FLASH_BUF_ACC, this->data_buffer, this->cw_size);
  1411. + config_cw_write_post(this);
  1412. +
  1413. + r = submit_descs(this);
  1414. +
  1415. + free_descs(this);
  1416. +
  1417. + if (r) {
  1418. + dev_err(this->dev, "failure to write updated oob\n");
  1419. + return -EIO;
  1420. + }
  1421. +
  1422. + chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1423. +
  1424. + status = chip->waitfunc(mtd, chip);
  1425. +
  1426. + return status & NAND_STATUS_FAIL ? -EIO : 0;
  1427. +}
  1428. +
  1429. +/*
  1430. + * the three functions below implement chip->read_byte(), chip->read_buf()
  1431. + * and chip->write_buf() respectively. these aren't used for
  1432. + * reading/writing page data, they are used for smaller data like reading
  1433. + * id, status etc
  1434. + */
  1435. +static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
  1436. +{
  1437. + struct nand_chip *chip = mtd->priv;
  1438. + struct qcom_nandc_data *this = chip->priv;
  1439. + uint8_t *buf = this->data_buffer;
  1440. + uint8_t ret = 0x0;
  1441. +
  1442. + if (this->last_command == NAND_CMD_STATUS) {
  1443. + ret = this->status;
  1444. +
  1445. + this->status = NAND_STATUS_READY | NAND_STATUS_WP;
  1446. +
  1447. + return ret;
  1448. + }
  1449. +
  1450. + if (this->buf_start < this->buf_count)
  1451. + ret = buf[this->buf_start++];
  1452. +
  1453. + return ret;
  1454. +}
  1455. +
  1456. +static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  1457. +{
  1458. + struct nand_chip *chip = mtd->priv;
  1459. + struct qcom_nandc_data *this = chip->priv;
  1460. + int real_len = min_t(size_t, len, this->buf_count - this->buf_start);
  1461. +
  1462. + memcpy(buf, this->data_buffer + this->buf_start, real_len);
  1463. + this->buf_start += real_len;
  1464. +}
  1465. +
  1466. +static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  1467. + int len)
  1468. +{
  1469. + struct nand_chip *chip = mtd->priv;
  1470. + struct qcom_nandc_data *this = chip->priv;
  1471. + int real_len = min_t(size_t, len, this->buf_count - this->buf_start);
  1472. +
  1473. + memcpy(this->data_buffer + this->buf_start, buf, real_len);
  1474. +
  1475. + this->buf_start += real_len;
  1476. +}
  1477. +
  1478. +/* we support only one external chip for now */
  1479. +static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
  1480. +{
  1481. + struct nand_chip *chip = mtd->priv;
  1482. + struct qcom_nandc_data *this = chip->priv;
  1483. +
  1484. + if (chipnr <= 0)
  1485. + return;
  1486. +
  1487. + dev_warn(this->dev, "invalid chip select\n");
  1488. +}
  1489. +
  1490. +/*
  1491. + * NAND controller page layout info
  1492. + *
  1493. + * |-----------------------| |---------------------------------|
  1494. + * | xx.......xx| | *********xx.......xx|
  1495. + * | DATA xx..ECC..xx| | DATA **SPARE**xx..ECC..xx|
  1496. + * | (516) xx.......xx| | (516-n*4) **(n*4)**xx.......xx|
  1497. + * | xx.......xx| | *********xx.......xx|
  1498. + * |-----------------------| |---------------------------------|
  1499. + * codeword 1,2..n-1 codeword n
  1500. + * <---(528/532 Bytes)----> <-------(528/532 Bytes)---------->
  1501. + *
  1502. + * n = number of codewords in the page
  1503. + * . = ECC bytes
  1504. + * * = spare bytes
  1505. + * x = unused/reserved bytes
  1506. + *
  1507. + * 2K page: n = 4, spare = 16 bytes
  1508. + * 4K page: n = 8, spare = 32 bytes
  1509. + * 8K page: n = 16, spare = 64 bytes
  1510. + *
  1511. + * the qcom nand controller operates at a sub page/codeword level. each
  1512. + * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
  1513. + * the number of ECC bytes vary based on the ECC strength and the bus width.
  1514. + *
  1515. + * the first n - 1 codewords contains 516 bytes of user data, the remaining
  1516. + * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
  1517. + * both user data and spare(oobavail) bytes that sum up to 516 bytes.
  1518. + *
  1519. + * the layout described above is used by the controller when the ECC block is
  1520. + * enabled. When we read a page with ECC enabled, the unused/reserved bytes are
  1521. + * skipped and not copied to our internal buffer. therefore, the nand_ecclayout
  1522. + * layouts defined below doesn't consider the positions occupied by the reserved
  1523. + * bytes
  1524. + *
  1525. + * when the ECC block is disabled, one unused byte (or two for 16 bit bus width)
  1526. + * in the last codeword is the position of bad block marker. the bad block
  1527. + * marker cannot be accessed when ECC is enabled.
  1528. + *
  1529. + */
  1530. +
  1531. +/*
  1532. + * Layouts for different page sizes and ecc modes. We skip the eccpos field
  1533. + * since it isn't needed for this driver
  1534. + */
  1535. +
  1536. +/* 2K page, 4 bit ECC */
  1537. +static struct nand_ecclayout layout_oob_64 = {
  1538. + .eccbytes = 40,
  1539. + .oobfree = {
  1540. + { 30, 16 },
  1541. + },
  1542. +};
  1543. +
  1544. +/* 4K page, 4 bit ECC, 8/16 bit bus width */
  1545. +static struct nand_ecclayout layout_oob_128 = {
  1546. + .eccbytes = 80,
  1547. + .oobfree = {
  1548. + { 70, 32 },
  1549. + },
  1550. +};
  1551. +
  1552. +/* 4K page, 8 bit ECC, 8 bit bus width */
  1553. +static struct nand_ecclayout layout_oob_224_x8 = {
  1554. + .eccbytes = 104,
  1555. + .oobfree = {
  1556. + { 91, 32 },
  1557. + },
  1558. +};
  1559. +
  1560. +/* 4K page, 8 bit ECC, 16 bit bus width */
  1561. +static struct nand_ecclayout layout_oob_224_x16 = {
  1562. + .eccbytes = 112,
  1563. + .oobfree = {
  1564. + { 98, 32 },
  1565. + },
  1566. +};
  1567. +
  1568. +/* 8K page, 4 bit ECC, 8/16 bit bus width */
  1569. +static struct nand_ecclayout layout_oob_256 = {
  1570. + .eccbytes = 160,
  1571. + .oobfree = {
  1572. + { 151, 64 },
  1573. + },
  1574. +};
  1575. +
  1576. +/*
  1577. + * this is called before scan_ident, we do some minimal configurations so
  1578. + * that reading ID and ONFI params work
  1579. + */
  1580. +static void qcom_nandc_pre_init(struct qcom_nandc_data *this)
  1581. +{
  1582. + /* kill onenand */
  1583. + nandc_write(this, SFLASHC_BURST_CFG, 0);
  1584. +
  1585. + /* enable ADM DMA */
  1586. + nandc_write(this, NAND_FLASH_CHIP_SELECT, DM_EN);
  1587. +
  1588. + /* save the original values of these registers */
  1589. + this->cmd1 = nandc_read(this, NAND_DEV_CMD1);
  1590. + this->vld = nandc_read(this, NAND_DEV_CMD_VLD);
  1591. +
  1592. + /* initial status value */
  1593. + this->status = NAND_STATUS_READY | NAND_STATUS_WP;
  1594. +}
  1595. +
  1596. +static int qcom_nandc_ecc_init(struct qcom_nandc_data *this)
  1597. +{
  1598. + struct mtd_info *mtd = &this->mtd;
  1599. + struct nand_chip *chip = &this->chip;
  1600. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1601. + int cwperpage;
  1602. + bool wide_bus;
  1603. +
  1604. + /* the nand controller fetches codewords/chunks of 512 bytes */
  1605. + cwperpage = mtd->writesize >> 9;
  1606. +
  1607. + ecc->strength = this->ecc_strength;
  1608. +
  1609. + wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
  1610. +
  1611. + if (ecc->strength >= 8) {
  1612. + /* 8 bit ECC defaults to BCH ECC on all platforms */
  1613. + ecc->bytes = wide_bus ? 14 : 13;
  1614. + } else {
  1615. + /*
  1616. + * if the controller supports BCH for 4 bit ECC, the controller
  1617. + * uses lesser bytes for ECC. If RS is used, the ECC bytes is
  1618. + * always 10 bytes
  1619. + */
  1620. + if (this->ecc_modes & ECC_BCH_4BIT)
  1621. + ecc->bytes = wide_bus ? 8 : 7;
  1622. + else
  1623. + ecc->bytes = 10;
  1624. + }
  1625. +
  1626. + /* each step consists of 512 bytes of data */
  1627. + ecc->size = NANDC_STEP_SIZE;
  1628. +
  1629. + ecc->read_page = qcom_nandc_read_page;
  1630. + ecc->read_oob = qcom_nandc_read_oob;
  1631. + ecc->write_page = qcom_nandc_write_page;
  1632. + ecc->write_oob = qcom_nandc_write_oob;
  1633. +
  1634. + /*
  1635. + * the bad block marker is readable only when we read the page with ECC
  1636. + * disabled. all the ops above run with ECC enabled. We need raw read
  1637. + * and write function for oob in order to access bad block marker.
  1638. + */
  1639. + ecc->read_oob_raw = qcom_nandc_read_oob_raw;
  1640. + ecc->write_oob_raw = qcom_nandc_write_oob_raw;
  1641. +
  1642. + switch (mtd->oobsize) {
  1643. + case 64:
  1644. + ecc->layout = &layout_oob_64;
  1645. + break;
  1646. + case 128:
  1647. + ecc->layout = &layout_oob_128;
  1648. + break;
  1649. + case 224:
  1650. + if (wide_bus)
  1651. + ecc->layout = &layout_oob_224_x16;
  1652. + else
  1653. + ecc->layout = &layout_oob_224_x8;
  1654. + break;
  1655. + case 256:
  1656. + ecc->layout = &layout_oob_256;
  1657. + break;
  1658. + default:
  1659. + dev_err(this->dev, "unsupported NAND device, oobsize %d\n",
  1660. + mtd->oobsize);
  1661. + return -ENODEV;
  1662. + }
  1663. +
  1664. + ecc->mode = NAND_ECC_HW;
  1665. +
  1666. + /* enable ecc by default */
  1667. + this->use_ecc = true;
  1668. +
  1669. + return 0;
  1670. +}
  1671. +
  1672. +static void qcom_nandc_hw_post_init(struct qcom_nandc_data *this)
  1673. +{
  1674. + struct mtd_info *mtd = &this->mtd;
  1675. + struct nand_chip *chip = &this->chip;
  1676. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1677. + int cwperpage = mtd->writesize / ecc->size;
  1678. + int spare_bytes, bad_block_byte;
  1679. + bool wide_bus;
  1680. + int ecc_mode = 0;
  1681. +
  1682. + wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
  1683. +
  1684. + if (ecc->strength >= 8) {
  1685. + this->cw_size = 532;
  1686. +
  1687. + spare_bytes = wide_bus ? 0 : 2;
  1688. +
  1689. + this->bch_enabled = true;
  1690. + ecc_mode = 1;
  1691. + } else {
  1692. + this->cw_size = 528;
  1693. +
  1694. + if (this->ecc_modes & ECC_BCH_4BIT) {
  1695. + spare_bytes = wide_bus ? 2 : 4;
  1696. +
  1697. + this->bch_enabled = true;
  1698. + ecc_mode = 0;
  1699. + } else {
  1700. + spare_bytes = wide_bus ? 0 : 1;
  1701. + }
  1702. + }
  1703. +
  1704. + /*
  1705. + * DATA_UD_BYTES varies based on whether the read/write command protects
  1706. + * spare data with ECC too. We protect spare data by default, so we set
  1707. + * it to main + spare data, which are 512 and 4 bytes respectively.
  1708. + */
  1709. + this->cw_data = 516;
  1710. +
  1711. + bad_block_byte = mtd->writesize - this->cw_size * (cwperpage - 1) + 1;
  1712. +
  1713. + this->cfg0 = (cwperpage - 1) << CW_PER_PAGE
  1714. + | this->cw_data << UD_SIZE_BYTES
  1715. + | 0 << DISABLE_STATUS_AFTER_WRITE
  1716. + | 5 << NUM_ADDR_CYCLES
  1717. + | ecc->bytes << ECC_PARITY_SIZE_BYTES_RS
  1718. + | 0 << STATUS_BFR_READ
  1719. + | 1 << SET_RD_MODE_AFTER_STATUS
  1720. + | spare_bytes << SPARE_SIZE_BYTES;
  1721. +
  1722. + this->cfg1 = 7 << NAND_RECOVERY_CYCLES
  1723. + | 0 << CS_ACTIVE_BSY
  1724. + | bad_block_byte << BAD_BLOCK_BYTE_NUM
  1725. + | 0 << BAD_BLOCK_IN_SPARE_AREA
  1726. + | 2 << WR_RD_BSY_GAP
  1727. + | wide_bus << WIDE_FLASH
  1728. + | this->bch_enabled << ENABLE_BCH_ECC;
  1729. +
  1730. + this->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
  1731. + | this->cw_size << UD_SIZE_BYTES
  1732. + | 5 << NUM_ADDR_CYCLES
  1733. + | 0 << SPARE_SIZE_BYTES;
  1734. +
  1735. + this->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
  1736. + | 0 << CS_ACTIVE_BSY
  1737. + | 17 << BAD_BLOCK_BYTE_NUM
  1738. + | 1 << BAD_BLOCK_IN_SPARE_AREA
  1739. + | 2 << WR_RD_BSY_GAP
  1740. + | wide_bus << WIDE_FLASH
  1741. + | 1 << DEV0_CFG1_ECC_DISABLE;
  1742. +
  1743. + this->ecc_bch_cfg = this->bch_enabled << ECC_CFG_ECC_DISABLE
  1744. + | 0 << ECC_SW_RESET
  1745. + | this->cw_data << ECC_NUM_DATA_BYTES
  1746. + | 1 << ECC_FORCE_CLK_OPEN
  1747. + | ecc_mode << ECC_MODE
  1748. + | ecc->bytes << ECC_PARITY_SIZE_BYTES_BCH;
  1749. +
  1750. + this->ecc_buf_cfg = 0x203 << NUM_STEPS;
  1751. +
  1752. + this->clrflashstatus = FS_READY_BSY_N;
  1753. + this->clrreadstatus = 0xc0;
  1754. +
  1755. + dev_dbg(this->dev,
  1756. + "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",
  1757. + this->cfg0, this->cfg1, this->ecc_buf_cfg,
  1758. + this->ecc_bch_cfg, this->cw_size, this->cw_data,
  1759. + ecc->strength, ecc->bytes, cwperpage);
  1760. +}
  1761. +
  1762. +static int qcom_nandc_alloc(struct qcom_nandc_data *this)
  1763. +{
  1764. + int r;
  1765. +
  1766. + r = dma_set_coherent_mask(this->dev, DMA_BIT_MASK(32));
  1767. + if (r) {
  1768. + dev_err(this->dev, "failed to set DMA mask\n");
  1769. + return r;
  1770. + }
  1771. +
  1772. + /*
  1773. + * we use the internal buffer for reading ONFI params, reading small
  1774. + * data like ID and status, and preforming read-copy-write operations
  1775. + * when writing to a codeword partially. 532 is the maximum possible
  1776. + * size of a codeword for our nand controller
  1777. + */
  1778. + this->buf_size = 532;
  1779. +
  1780. + this->data_buffer = devm_kzalloc(this->dev, this->buf_size, GFP_KERNEL);
  1781. + if (!this->data_buffer)
  1782. + return -ENOMEM;
  1783. +
  1784. + this->regs = devm_kzalloc(this->dev, sizeof(*this->regs), GFP_KERNEL);
  1785. + if (!this->regs)
  1786. + return -ENOMEM;
  1787. +
  1788. + this->reg_read_buf = devm_kzalloc(this->dev,
  1789. + MAX_REG_RD * sizeof(*this->reg_read_buf),
  1790. + GFP_KERNEL);
  1791. + if (!this->reg_read_buf)
  1792. + return -ENOMEM;
  1793. +
  1794. + INIT_LIST_HEAD(&this->list);
  1795. +
  1796. + this->chan = dma_request_slave_channel(this->dev, "rxtx");
  1797. + if (!this->chan) {
  1798. + dev_err(this->dev, "failed to request slave channel\n");
  1799. + return -ENODEV;
  1800. + }
  1801. +
  1802. + return 0;
  1803. +}
  1804. +
  1805. +static void qcom_nandc_unalloc(struct qcom_nandc_data *this)
  1806. +{
  1807. + dma_release_channel(this->chan);
  1808. +}
  1809. +
  1810. +static int qcom_nandc_init(struct qcom_nandc_data *this)
  1811. +{
  1812. + struct mtd_info *mtd = &this->mtd;
  1813. + struct nand_chip *chip = &this->chip;
  1814. + struct device_node *np = this->dev->of_node;
  1815. + struct mtd_part_parser_data ppdata = { .of_node = np };
  1816. + int r;
  1817. +
  1818. + mtd->priv = chip;
  1819. + mtd->name = "qcom-nandc";
  1820. + mtd->owner = THIS_MODULE;
  1821. +
  1822. + chip->priv = this;
  1823. +
  1824. + chip->cmdfunc = qcom_nandc_command;
  1825. + chip->select_chip = qcom_nandc_select_chip;
  1826. + chip->read_byte = qcom_nandc_read_byte;
  1827. + chip->read_buf = qcom_nandc_read_buf;
  1828. + chip->write_buf = qcom_nandc_write_buf;
  1829. +
  1830. + chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER;
  1831. + if (this->bus_width == 16)
  1832. + chip->options |= NAND_BUSWIDTH_16;
  1833. +
  1834. + chip->bbt_options = NAND_BBT_ACCESS_BBM_RAW;
  1835. + if (of_get_nand_on_flash_bbt(np))
  1836. + chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  1837. +
  1838. + qcom_nandc_pre_init(this);
  1839. +
  1840. + r = nand_scan_ident(mtd, 1, NULL);
  1841. + if (r)
  1842. + return r;
  1843. +
  1844. + r = qcom_nandc_ecc_init(this);
  1845. + if (r)
  1846. + return r;
  1847. +
  1848. + qcom_nandc_hw_post_init(this);
  1849. +
  1850. + r = nand_scan_tail(mtd);
  1851. + if (r)
  1852. + return r;
  1853. +
  1854. + return mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  1855. +}
  1856. +
  1857. +static int qcom_nandc_parse_dt(struct platform_device *pdev)
  1858. +{
  1859. + struct qcom_nandc_data *this = platform_get_drvdata(pdev);
  1860. + struct device_node *np = this->dev->of_node;
  1861. + int r;
  1862. +
  1863. + this->ecc_strength = of_get_nand_ecc_strength(np);
  1864. + if (this->ecc_strength < 0) {
  1865. + dev_warn(this->dev,
  1866. + "incorrect ecc strength, setting to 4 bits/step\n");
  1867. + this->ecc_strength = 4;
  1868. + }
  1869. +
  1870. + this->bus_width = of_get_nand_bus_width(np);
  1871. + if (this->bus_width < 0) {
  1872. + dev_warn(this->dev, "incorrect bus width, setting to 8\n");
  1873. + this->bus_width = 8;
  1874. + }
  1875. +
  1876. + r = of_property_read_u32(np, "qcom,cmd-crci", &this->cmd_crci);
  1877. + if (r) {
  1878. + dev_err(this->dev, "command CRCI unspecified\n");
  1879. + return r;
  1880. + }
  1881. +
  1882. + r = of_property_read_u32(np, "qcom,data-crci", &this->data_crci);
  1883. + if (r) {
  1884. + dev_err(this->dev, "data CRCI unspecified\n");
  1885. + return r;
  1886. + }
  1887. +
  1888. + return 0;
  1889. +}
  1890. +
  1891. +static int qcom_nandc_probe(struct platform_device *pdev)
  1892. +{
  1893. + struct qcom_nandc_data *this;
  1894. + const struct of_device_id *match;
  1895. + int r;
  1896. +
  1897. + this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
  1898. + if (!this)
  1899. + return -ENOMEM;
  1900. +
  1901. + platform_set_drvdata(pdev, this);
  1902. +
  1903. + this->pdev = pdev;
  1904. + this->dev = &pdev->dev;
  1905. +
  1906. + match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
  1907. + if (!match) {
  1908. + dev_err(&pdev->dev, "failed to match device\n");
  1909. + return -ENODEV;
  1910. + }
  1911. +
  1912. + if (!match->data) {
  1913. + dev_err(&pdev->dev, "failed to get device data\n");
  1914. + return -ENODEV;
  1915. + }
  1916. +
  1917. + this->ecc_modes = (u32) match->data;
  1918. +
  1919. + this->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1920. + this->base = devm_ioremap_resource(&pdev->dev, this->res);
  1921. + if (IS_ERR(this->base))
  1922. + return PTR_ERR(this->base);
  1923. +
  1924. + this->core_clk = devm_clk_get(&pdev->dev, "core");
  1925. + if (IS_ERR(this->core_clk))
  1926. + return PTR_ERR(this->core_clk);
  1927. +
  1928. + this->aon_clk = devm_clk_get(&pdev->dev, "aon");
  1929. + if (IS_ERR(this->aon_clk))
  1930. + return PTR_ERR(this->aon_clk);
  1931. +
  1932. + r = qcom_nandc_parse_dt(pdev);
  1933. + if (r)
  1934. + return r;
  1935. +
  1936. + r = qcom_nandc_alloc(this);
  1937. + if (r)
  1938. + return r;
  1939. +
  1940. + r = clk_prepare_enable(this->core_clk);
  1941. + if (r)
  1942. + goto err_core_clk;
  1943. +
  1944. + r = clk_prepare_enable(this->aon_clk);
  1945. + if (r)
  1946. + goto err_aon_clk;
  1947. +
  1948. + r = qcom_nandc_init(this);
  1949. + if (r)
  1950. + goto err_init;
  1951. +
  1952. + return 0;
  1953. +
  1954. +err_init:
  1955. + clk_disable_unprepare(this->aon_clk);
  1956. +err_aon_clk:
  1957. + clk_disable_unprepare(this->core_clk);
  1958. +err_core_clk:
  1959. + qcom_nandc_unalloc(this);
  1960. +
  1961. + return r;
  1962. +}
  1963. +
  1964. +static int qcom_nandc_remove(struct platform_device *pdev)
  1965. +{
  1966. + struct qcom_nandc_data *this = platform_get_drvdata(pdev);
  1967. +
  1968. + qcom_nandc_unalloc(this);
  1969. +
  1970. + clk_disable_unprepare(this->aon_clk);
  1971. + clk_disable_unprepare(this->core_clk);
  1972. +
  1973. + return 0;
  1974. +}
  1975. +
  1976. +#define EBI2_NANDC_ECC_MODES (ECC_RS_4BIT | ECC_BCH_8BIT)
  1977. +
  1978. +/*
  1979. + * data will hold a struct pointer containing more differences once we support
  1980. + * more IPs
  1981. + */
  1982. +static const struct of_device_id qcom_nandc_of_match[] = {
  1983. + { .compatible = "qcom,ebi2-nandc",
  1984. + .data = (void *) EBI2_NANDC_ECC_MODES,
  1985. + },
  1986. + {}
  1987. +};
  1988. +MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
  1989. +
  1990. +static struct platform_driver qcom_nandc_driver = {
  1991. + .driver = {
  1992. + .name = "qcom-nandc",
  1993. + .of_match_table = qcom_nandc_of_match,
  1994. + },
  1995. + .probe = qcom_nandc_probe,
  1996. + .remove = qcom_nandc_remove,
  1997. +};
  1998. +module_platform_driver(qcom_nandc_driver);
  1999. +
  2000. +MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
  2001. +MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
  2002. +MODULE_LICENSE("GPL v2");
  2003. --- a/drivers/mtd/nand/Makefile
  2004. +++ b/drivers/mtd/nand/Makefile
  2005. @@ -55,5 +55,6 @@ obj-$(CONFIG_MTD_NAND_BCM47XXNFLASH) +=
  2006. obj-$(CONFIG_MTD_NAND_SUNXI) += sunxi_nand.o
  2007. obj-$(CONFIG_MTD_NAND_HISI504) += hisi504_nand.o
  2008. obj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmnand/
  2009. +obj-$(CONFIG_MTD_NAND_QCOM) += qcom_nandc.o
  2010. nand-objs := nand_base.o nand_bbt.o nand_timings.o