001-mtk-0020-nand-raw-add-support-for-MediaTek-MT7621-SoC.patch 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. From 8d94833f13ccd7e1dfea605cfdf9a8eb53505515 Mon Sep 17 00:00:00 2001
  2. From: Weijie Gao <[email protected]>
  3. Date: Fri, 20 May 2022 11:23:47 +0800
  4. Subject: [PATCH 20/25] nand: raw: add support for MediaTek MT7621 SoC
  5. This patch adds NAND flash controller driver for MediaTek MT7621 SoC.
  6. The NAND flash controller of MT7621 supports only SLC NAND flashes.
  7. It supports 4~12 bits correction with maximum 4KB page size.
  8. Signed-off-by: Weijie Gao <[email protected]>
  9. ---
  10. drivers/mtd/nand/raw/Kconfig | 17 +-
  11. drivers/mtd/nand/raw/Makefile | 2 +
  12. drivers/mtd/nand/raw/mt7621_nand.c | 1205 ++++++++++++++++++++++++
  13. drivers/mtd/nand/raw/mt7621_nand.h | 29 +
  14. drivers/mtd/nand/raw/mt7621_nand_spl.c | 237 +++++
  15. 5 files changed, 1488 insertions(+), 2 deletions(-)
  16. create mode 100644 drivers/mtd/nand/raw/mt7621_nand.c
  17. create mode 100644 drivers/mtd/nand/raw/mt7621_nand.h
  18. create mode 100644 drivers/mtd/nand/raw/mt7621_nand_spl.c
  19. --- a/drivers/mtd/nand/raw/Kconfig
  20. +++ b/drivers/mtd/nand/raw/Kconfig
  21. @@ -521,12 +521,25 @@ config TEGRA_NAND
  22. help
  23. Enables support for NAND Flash chips on Tegra SoCs platforms.
  24. +config NAND_MT7621
  25. + bool "Support for MediaTek MT7621 NAND flash controller"
  26. + depends on SOC_MT7621
  27. + select SYS_NAND_SELF_INIT
  28. + select SPL_SYS_NAND_SELF_INIT
  29. + imply CMD_NAND
  30. + help
  31. + This enables NAND driver for the NAND flash controller on MediaTek
  32. + MT7621 platform.
  33. + The controller supports 4~12 bits correction per 512 bytes with a
  34. + maximum 4KB page size.
  35. +
  36. comment "Generic NAND options"
  37. config SYS_NAND_BLOCK_SIZE
  38. hex "NAND chip eraseblock size"
  39. depends on ARCH_SUNXI || SPL_NAND_SUPPORT || TPL_NAND_SUPPORT
  40. - depends on !NAND_MXS && !NAND_DENALI_DT && !NAND_LPC32XX_MLC && !NAND_FSL_IFC
  41. + depends on !NAND_MXS && !NAND_DENALI_DT && !NAND_LPC32XX_MLC && \
  42. + !NAND_FSL_IFC && !NAND_MT7621
  43. help
  44. Number of data bytes in one eraseblock for the NAND chip on the
  45. board. This is the multiple of NAND_PAGE_SIZE and the number of
  46. @@ -551,7 +564,7 @@ config SYS_NAND_PAGE_SIZE
  47. depends on ARCH_SUNXI || NAND_OMAP_GPMC || NAND_LPC32XX_SLC || \
  48. SPL_NAND_SIMPLE || (NAND_MXC && SPL_NAND_SUPPORT) || \
  49. (NAND_ATMEL && SPL_NAND_SUPPORT) || SPL_GENERATE_ATMEL_PMECC_HEADER
  50. - depends on !NAND_MXS && !NAND_DENALI_DT && !NAND_LPC32XX_MLC
  51. + depends on !NAND_MXS && !NAND_DENALI_DT && !NAND_LPC32XX_MLC && !NAND_MT7621
  52. help
  53. Number of data bytes in one page for the NAND chip on the
  54. board, not including the OOB area.
  55. --- a/drivers/mtd/nand/raw/Makefile
  56. +++ b/drivers/mtd/nand/raw/Makefile
  57. @@ -72,6 +72,7 @@ obj-$(CONFIG_NAND_ZYNQ) += zynq_nand.o
  58. obj-$(CONFIG_NAND_STM32_FMC2) += stm32_fmc2_nand.o
  59. obj-$(CONFIG_CORTINA_NAND) += cortina_nand.o
  60. obj-$(CONFIG_ROCKCHIP_NAND) += rockchip_nfc.o
  61. +obj-$(CONFIG_NAND_MT7621) += mt7621_nand.o
  62. else # minimal SPL drivers
  63. @@ -80,5 +81,6 @@ obj-$(CONFIG_NAND_FSL_IFC) += fsl_ifc_sp
  64. obj-$(CONFIG_NAND_MXC) += mxc_nand_spl.o
  65. obj-$(CONFIG_NAND_MXS) += mxs_nand_spl.o mxs_nand.o
  66. obj-$(CONFIG_NAND_SUNXI) += sunxi_nand_spl.o
  67. +obj-$(CONFIG_NAND_MT7621) += mt7621_nand_spl.o mt7621_nand.o
  68. endif # drivers
  69. --- /dev/null
  70. +++ b/drivers/mtd/nand/raw/mt7621_nand.c
  71. @@ -0,0 +1,1205 @@
  72. +// SPDX-License-Identifier: GPL-2.0
  73. +/*
  74. + * Copyright (C) 2022 MediaTek Inc. All rights reserved.
  75. + *
  76. + * Author: Weijie Gao <[email protected]>
  77. + */
  78. +
  79. +#include <log.h>
  80. +#include <nand.h>
  81. +#include <malloc.h>
  82. +#include <asm/addrspace.h>
  83. +#include <linux/io.h>
  84. +#include <linux/iopoll.h>
  85. +#include <linux/sizes.h>
  86. +#include <linux/bitops.h>
  87. +#include <linux/bitfield.h>
  88. +#include "mt7621_nand.h"
  89. +
  90. +/* NFI core registers */
  91. +#define NFI_CNFG 0x000
  92. +#define CNFG_OP_MODE GENMASK(14, 12)
  93. +#define CNFG_OP_CUSTOM 6
  94. +#define CNFG_AUTO_FMT_EN BIT(9)
  95. +#define CNFG_HW_ECC_EN BIT(8)
  96. +#define CNFG_BYTE_RW BIT(6)
  97. +#define CNFG_READ_MODE BIT(1)
  98. +
  99. +#define NFI_PAGEFMT 0x004
  100. +#define PAGEFMT_FDM_ECC GENMASK(15, 12)
  101. +#define PAGEFMT_FDM GENMASK(11, 8)
  102. +#define PAGEFMT_SPARE GENMASK(5, 4)
  103. +#define PAGEFMT_PAGE GENMASK(1, 0)
  104. +
  105. +#define NFI_CON 0x008
  106. +#define CON_NFI_SEC GENMASK(15, 12)
  107. +#define CON_NFI_BWR BIT(9)
  108. +#define CON_NFI_BRD BIT(8)
  109. +#define CON_NFI_RST BIT(1)
  110. +#define CON_FIFO_FLUSH BIT(0)
  111. +
  112. +#define NFI_ACCCON 0x00c
  113. +#define ACCCON_POECS GENMASK(31, 28)
  114. +#define ACCCON_POECS_DEF 3
  115. +#define ACCCON_PRECS GENMASK(27, 22)
  116. +#define ACCCON_PRECS_DEF 3
  117. +#define ACCCON_C2R GENMASK(21, 16)
  118. +#define ACCCON_C2R_DEF 7
  119. +#define ACCCON_W2R GENMASK(15, 12)
  120. +#define ACCCON_W2R_DEF 7
  121. +#define ACCCON_WH GENMASK(11, 8)
  122. +#define ACCCON_WH_DEF 15
  123. +#define ACCCON_WST GENMASK(7, 4)
  124. +#define ACCCON_WST_DEF 15
  125. +#define ACCCON_WST_MIN 3
  126. +#define ACCCON_RLT GENMASK(3, 0)
  127. +#define ACCCON_RLT_DEF 15
  128. +#define ACCCON_RLT_MIN 3
  129. +
  130. +#define NFI_CMD 0x020
  131. +
  132. +#define NFI_ADDRNOB 0x030
  133. +#define ADDR_ROW_NOB GENMASK(6, 4)
  134. +#define ADDR_COL_NOB GENMASK(2, 0)
  135. +
  136. +#define NFI_COLADDR 0x034
  137. +#define NFI_ROWADDR 0x038
  138. +
  139. +#define NFI_STRDATA 0x040
  140. +#define STR_DATA BIT(0)
  141. +
  142. +#define NFI_CNRNB 0x044
  143. +#define CB2R_TIME GENMASK(7, 4)
  144. +#define STR_CNRNB BIT(0)
  145. +
  146. +#define NFI_DATAW 0x050
  147. +#define NFI_DATAR 0x054
  148. +
  149. +#define NFI_PIO_DIRDY 0x058
  150. +#define PIO_DIRDY BIT(0)
  151. +
  152. +#define NFI_STA 0x060
  153. +#define STA_NFI_FSM GENMASK(19, 16)
  154. +#define STA_FSM_CUSTOM_DATA 14
  155. +#define STA_BUSY BIT(8)
  156. +#define STA_ADDR BIT(1)
  157. +#define STA_CMD BIT(0)
  158. +
  159. +#define NFI_ADDRCNTR 0x070
  160. +#define SEC_CNTR GENMASK(15, 12)
  161. +#define SEC_ADDR GENMASK(9, 0)
  162. +
  163. +#define NFI_CSEL 0x090
  164. +#define CSEL GENMASK(1, 0)
  165. +
  166. +#define NFI_FDM0L 0x0a0
  167. +#define NFI_FDML(n) (0x0a0 + ((n) << 3))
  168. +
  169. +#define NFI_FDM0M 0x0a4
  170. +#define NFI_FDMM(n) (0x0a4 + ((n) << 3))
  171. +
  172. +#define NFI_MASTER_STA 0x210
  173. +#define MAS_ADDR GENMASK(11, 9)
  174. +#define MAS_RD GENMASK(8, 6)
  175. +#define MAS_WR GENMASK(5, 3)
  176. +#define MAS_RDDLY GENMASK(2, 0)
  177. +
  178. +/* ECC engine registers */
  179. +#define ECC_ENCCON 0x000
  180. +#define ENC_EN BIT(0)
  181. +
  182. +#define ECC_ENCCNFG 0x004
  183. +#define ENC_CNFG_MSG GENMASK(28, 16)
  184. +#define ENC_MODE GENMASK(5, 4)
  185. +#define ENC_MODE_NFI 1
  186. +#define ENC_TNUM GENMASK(2, 0)
  187. +
  188. +#define ECC_ENCIDLE 0x00c
  189. +#define ENC_IDLE BIT(0)
  190. +
  191. +#define ECC_DECCON 0x100
  192. +#define DEC_EN BIT(0)
  193. +
  194. +#define ECC_DECCNFG 0x104
  195. +#define DEC_EMPTY_EN BIT(31)
  196. +#define DEC_CS GENMASK(28, 16)
  197. +#define DEC_CON GENMASK(13, 12)
  198. +#define DEC_CON_EL 2
  199. +#define DEC_MODE GENMASK(5, 4)
  200. +#define DEC_MODE_NFI 1
  201. +#define DEC_TNUM GENMASK(2, 0)
  202. +
  203. +#define ECC_DECIDLE 0x10c
  204. +#define DEC_IDLE BIT(1)
  205. +
  206. +#define ECC_DECENUM 0x114
  207. +#define ERRNUM_S 2
  208. +#define ERRNUM_M GENMASK(3, 0)
  209. +
  210. +#define ECC_DECDONE 0x118
  211. +#define DEC_DONE7 BIT(7)
  212. +#define DEC_DONE6 BIT(6)
  213. +#define DEC_DONE5 BIT(5)
  214. +#define DEC_DONE4 BIT(4)
  215. +#define DEC_DONE3 BIT(3)
  216. +#define DEC_DONE2 BIT(2)
  217. +#define DEC_DONE1 BIT(1)
  218. +#define DEC_DONE0 BIT(0)
  219. +
  220. +#define ECC_DECEL(n) (0x11c + (n) * 4)
  221. +#define DEC_EL_ODD_S 16
  222. +#define DEC_EL_M 0x1fff
  223. +#define DEC_EL_BYTE_POS_S 3
  224. +#define DEC_EL_BIT_POS_M GENMASK(2, 0)
  225. +
  226. +#define ECC_FDMADDR 0x13c
  227. +
  228. +/* ENCIDLE and DECIDLE */
  229. +#define ECC_IDLE BIT(0)
  230. +
  231. +#define ACCTIMING(tpoecs, tprecs, tc2r, tw2r, twh, twst, trlt) \
  232. + (FIELD_PREP(ACCCON_POECS, tpoecs) | \
  233. + FIELD_PREP(ACCCON_PRECS, tprecs) | \
  234. + FIELD_PREP(ACCCON_C2R, tc2r) | \
  235. + FIELD_PREP(ACCCON_W2R, tw2r) | \
  236. + FIELD_PREP(ACCCON_WH, twh) | \
  237. + FIELD_PREP(ACCCON_WST, twst) | \
  238. + FIELD_PREP(ACCCON_RLT, trlt))
  239. +
  240. +#define MASTER_STA_MASK (MAS_ADDR | MAS_RD | MAS_WR | \
  241. + MAS_RDDLY)
  242. +#define NFI_RESET_TIMEOUT 1000000
  243. +#define NFI_CORE_TIMEOUT 500000
  244. +#define ECC_ENGINE_TIMEOUT 500000
  245. +
  246. +#define ECC_SECTOR_SIZE 512
  247. +#define ECC_PARITY_BITS 13
  248. +
  249. +#define NFI_FDM_SIZE 8
  250. +
  251. +/* Register base */
  252. +#define NFI_BASE 0x1e003000
  253. +#define NFI_ECC_BASE 0x1e003800
  254. +
  255. +static struct mt7621_nfc nfc_dev;
  256. +
  257. +static const u16 mt7621_nfi_page_size[] = { SZ_512, SZ_2K, SZ_4K };
  258. +static const u8 mt7621_nfi_spare_size[] = { 16, 26, 27, 28 };
  259. +static const u8 mt7621_ecc_strength[] = { 4, 6, 8, 10, 12 };
  260. +
  261. +static inline u32 nfi_read32(struct mt7621_nfc *nfc, u32 reg)
  262. +{
  263. + return readl(nfc->nfi_regs + reg);
  264. +}
  265. +
  266. +static inline void nfi_write32(struct mt7621_nfc *nfc, u32 reg, u32 val)
  267. +{
  268. + writel(val, nfc->nfi_regs + reg);
  269. +}
  270. +
  271. +static inline u16 nfi_read16(struct mt7621_nfc *nfc, u32 reg)
  272. +{
  273. + return readw(nfc->nfi_regs + reg);
  274. +}
  275. +
  276. +static inline void nfi_write16(struct mt7621_nfc *nfc, u32 reg, u16 val)
  277. +{
  278. + writew(val, nfc->nfi_regs + reg);
  279. +}
  280. +
  281. +static inline void ecc_write16(struct mt7621_nfc *nfc, u32 reg, u16 val)
  282. +{
  283. + writew(val, nfc->ecc_regs + reg);
  284. +}
  285. +
  286. +static inline u32 ecc_read32(struct mt7621_nfc *nfc, u32 reg)
  287. +{
  288. + return readl(nfc->ecc_regs + reg);
  289. +}
  290. +
  291. +static inline void ecc_write32(struct mt7621_nfc *nfc, u32 reg, u32 val)
  292. +{
  293. + return writel(val, nfc->ecc_regs + reg);
  294. +}
  295. +
  296. +static inline u8 *oob_fdm_ptr(struct nand_chip *nand, int sect)
  297. +{
  298. + return nand->oob_poi + sect * NFI_FDM_SIZE;
  299. +}
  300. +
  301. +static inline u8 *oob_ecc_ptr(struct mt7621_nfc *nfc, int sect)
  302. +{
  303. + struct nand_chip *nand = &nfc->nand;
  304. +
  305. + return nand->oob_poi + nand->ecc.steps * NFI_FDM_SIZE +
  306. + sect * (nfc->spare_per_sector - NFI_FDM_SIZE);
  307. +}
  308. +
  309. +static inline u8 *page_data_ptr(struct nand_chip *nand, const u8 *buf,
  310. + int sect)
  311. +{
  312. + return (u8 *)buf + sect * nand->ecc.size;
  313. +}
  314. +
  315. +static int mt7621_ecc_wait_idle(struct mt7621_nfc *nfc, u32 reg)
  316. +{
  317. + u32 val;
  318. + int ret;
  319. +
  320. + ret = readw_poll_timeout(nfc->ecc_regs + reg, val, val & ECC_IDLE,
  321. + ECC_ENGINE_TIMEOUT);
  322. + if (ret) {
  323. + pr_warn("ECC engine timed out entering idle mode\n");
  324. + return -EIO;
  325. + }
  326. +
  327. + return 0;
  328. +}
  329. +
  330. +static int mt7621_ecc_decoder_wait_done(struct mt7621_nfc *nfc, u32 sect)
  331. +{
  332. + u32 val;
  333. + int ret;
  334. +
  335. + ret = readw_poll_timeout(nfc->ecc_regs + ECC_DECDONE, val,
  336. + val & (1 << sect), ECC_ENGINE_TIMEOUT);
  337. + if (ret) {
  338. + pr_warn("ECC decoder for sector %d timed out\n", sect);
  339. + return -ETIMEDOUT;
  340. + }
  341. +
  342. + return 0;
  343. +}
  344. +
  345. +static void mt7621_ecc_encoder_op(struct mt7621_nfc *nfc, bool enable)
  346. +{
  347. + mt7621_ecc_wait_idle(nfc, ECC_ENCIDLE);
  348. + ecc_write16(nfc, ECC_ENCCON, enable ? ENC_EN : 0);
  349. +}
  350. +
  351. +static void mt7621_ecc_decoder_op(struct mt7621_nfc *nfc, bool enable)
  352. +{
  353. + mt7621_ecc_wait_idle(nfc, ECC_DECIDLE);
  354. + ecc_write16(nfc, ECC_DECCON, enable ? DEC_EN : 0);
  355. +}
  356. +
  357. +static int mt7621_ecc_correct_check(struct mt7621_nfc *nfc, u8 *sector_buf,
  358. + u8 *fdm_buf, u32 sect)
  359. +{
  360. + struct nand_chip *nand = &nfc->nand;
  361. + u32 decnum, num_error_bits, fdm_end_bits;
  362. + u32 error_locations, error_bit_loc;
  363. + u32 error_byte_pos, error_bit_pos;
  364. + int bitflips = 0;
  365. + u32 i;
  366. +
  367. + decnum = ecc_read32(nfc, ECC_DECENUM);
  368. + num_error_bits = (decnum >> (sect << ERRNUM_S)) & ERRNUM_M;
  369. + fdm_end_bits = (nand->ecc.size + NFI_FDM_SIZE) << 3;
  370. +
  371. + if (!num_error_bits)
  372. + return 0;
  373. +
  374. + if (num_error_bits == ERRNUM_M)
  375. + return -1;
  376. +
  377. + for (i = 0; i < num_error_bits; i++) {
  378. + error_locations = ecc_read32(nfc, ECC_DECEL(i / 2));
  379. + error_bit_loc = (error_locations >> ((i % 2) * DEC_EL_ODD_S)) &
  380. + DEC_EL_M;
  381. + error_byte_pos = error_bit_loc >> DEC_EL_BYTE_POS_S;
  382. + error_bit_pos = error_bit_loc & DEC_EL_BIT_POS_M;
  383. +
  384. + if (error_bit_loc < (nand->ecc.size << 3)) {
  385. + if (sector_buf) {
  386. + sector_buf[error_byte_pos] ^=
  387. + (1 << error_bit_pos);
  388. + }
  389. + } else if (error_bit_loc < fdm_end_bits) {
  390. + if (fdm_buf) {
  391. + fdm_buf[error_byte_pos - nand->ecc.size] ^=
  392. + (1 << error_bit_pos);
  393. + }
  394. + }
  395. +
  396. + bitflips++;
  397. + }
  398. +
  399. + return bitflips;
  400. +}
  401. +
  402. +static int mt7621_nfc_wait_write_completion(struct mt7621_nfc *nfc,
  403. + struct nand_chip *nand)
  404. +{
  405. + u16 val;
  406. + int ret;
  407. +
  408. + ret = readw_poll_timeout(nfc->nfi_regs + NFI_ADDRCNTR, val,
  409. + FIELD_GET(SEC_CNTR, val) >= nand->ecc.steps,
  410. + NFI_CORE_TIMEOUT);
  411. +
  412. + if (ret) {
  413. + pr_warn("NFI core write operation timed out\n");
  414. + return -ETIMEDOUT;
  415. + }
  416. +
  417. + return ret;
  418. +}
  419. +
  420. +static void mt7621_nfc_hw_reset(struct mt7621_nfc *nfc)
  421. +{
  422. + u32 val;
  423. + int ret;
  424. +
  425. + /* reset all registers and force the NFI master to terminate */
  426. + nfi_write16(nfc, NFI_CON, CON_FIFO_FLUSH | CON_NFI_RST);
  427. +
  428. + /* wait for the master to finish the last transaction */
  429. + ret = readw_poll_timeout(nfc->nfi_regs + NFI_MASTER_STA, val,
  430. + !(val & MASTER_STA_MASK), NFI_RESET_TIMEOUT);
  431. + if (ret) {
  432. + pr_warn("Failed to reset NFI master in %dms\n",
  433. + NFI_RESET_TIMEOUT);
  434. + }
  435. +
  436. + /* ensure any status register affected by the NFI master is reset */
  437. + nfi_write16(nfc, NFI_CON, CON_FIFO_FLUSH | CON_NFI_RST);
  438. + nfi_write16(nfc, NFI_STRDATA, 0);
  439. +}
  440. +
  441. +static inline void mt7621_nfc_hw_init(struct mt7621_nfc *nfc)
  442. +{
  443. + u32 acccon;
  444. +
  445. + /*
  446. + * CNRNB: nand ready/busy register
  447. + * -------------------------------
  448. + * 7:4: timeout register for polling the NAND busy/ready signal
  449. + * 0 : poll the status of the busy/ready signal after [7:4]*16 cycles.
  450. + */
  451. + nfi_write16(nfc, NFI_CNRNB, CB2R_TIME | STR_CNRNB);
  452. +
  453. + mt7621_nfc_hw_reset(nfc);
  454. +
  455. + /* Apply default access timing */
  456. + acccon = ACCTIMING(ACCCON_POECS_DEF, ACCCON_PRECS_DEF, ACCCON_C2R_DEF,
  457. + ACCCON_W2R_DEF, ACCCON_WH_DEF, ACCCON_WST_DEF,
  458. + ACCCON_RLT_DEF);
  459. +
  460. + nfi_write32(nfc, NFI_ACCCON, acccon);
  461. +}
  462. +
  463. +static int mt7621_nfc_send_command(struct mt7621_nfc *nfc, u8 command)
  464. +{
  465. + u32 val;
  466. + int ret;
  467. +
  468. + nfi_write32(nfc, NFI_CMD, command);
  469. +
  470. + ret = readl_poll_timeout(nfc->nfi_regs + NFI_STA, val, !(val & STA_CMD),
  471. + NFI_CORE_TIMEOUT);
  472. + if (ret) {
  473. + pr_warn("NFI core timed out entering command mode\n");
  474. + return -EIO;
  475. + }
  476. +
  477. + return 0;
  478. +}
  479. +
  480. +static int mt7621_nfc_send_address_byte(struct mt7621_nfc *nfc, int addr)
  481. +{
  482. + u32 val;
  483. + int ret;
  484. +
  485. + nfi_write32(nfc, NFI_COLADDR, addr);
  486. + nfi_write32(nfc, NFI_ROWADDR, 0);
  487. + nfi_write16(nfc, NFI_ADDRNOB, 1);
  488. +
  489. + ret = readl_poll_timeout(nfc->nfi_regs + NFI_STA, val,
  490. + !(val & STA_ADDR), NFI_CORE_TIMEOUT);
  491. + if (ret) {
  492. + pr_warn("NFI core timed out entering address mode\n");
  493. + return -EIO;
  494. + }
  495. +
  496. + return 0;
  497. +}
  498. +
  499. +static void mt7621_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
  500. + unsigned int ctrl)
  501. +{
  502. + struct mt7621_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
  503. +
  504. + if (ctrl & NAND_ALE) {
  505. + mt7621_nfc_send_address_byte(nfc, dat & 0xff);
  506. + } else if (ctrl & NAND_CLE) {
  507. + mt7621_nfc_hw_reset(nfc);
  508. + nfi_write16(nfc, NFI_CNFG,
  509. + FIELD_PREP(CNFG_OP_MODE, CNFG_OP_CUSTOM));
  510. + mt7621_nfc_send_command(nfc, dat);
  511. + }
  512. +}
  513. +
  514. +static int mt7621_nfc_dev_ready(struct mtd_info *mtd)
  515. +{
  516. + struct mt7621_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
  517. +
  518. + if (nfi_read32(nfc, NFI_STA) & STA_BUSY)
  519. + return 0;
  520. +
  521. + return 1;
  522. +}
  523. +
  524. +static void mt7621_nfc_select_chip(struct mtd_info *mtd, int chipnr)
  525. +{
  526. + struct mt7621_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
  527. +
  528. + nfi_write16(nfc, NFI_CSEL, 0);
  529. +}
  530. +
  531. +static void mt7621_nfc_wait_pio_ready(struct mt7621_nfc *nfc)
  532. +{
  533. + int ret;
  534. + u16 val;
  535. +
  536. + ret = readw_poll_timeout(nfc->nfi_regs + NFI_PIO_DIRDY, val,
  537. + val & PIO_DIRDY, NFI_CORE_TIMEOUT);
  538. + if (ret < 0)
  539. + pr_err("NFI core PIO mode not ready\n");
  540. +}
  541. +
  542. +static u32 mt7621_nfc_pio_read(struct mt7621_nfc *nfc, bool br)
  543. +{
  544. + u32 reg, fsm;
  545. +
  546. + /* after each byte read, the NFI_STA reg is reset by the hardware */
  547. + reg = nfi_read32(nfc, NFI_STA);
  548. + fsm = FIELD_GET(STA_NFI_FSM, reg);
  549. +
  550. + if (fsm != STA_FSM_CUSTOM_DATA) {
  551. + reg = nfi_read16(nfc, NFI_CNFG);
  552. + reg |= CNFG_READ_MODE | CNFG_BYTE_RW;
  553. + if (!br)
  554. + reg &= ~CNFG_BYTE_RW;
  555. + nfi_write16(nfc, NFI_CNFG, reg);
  556. +
  557. + /*
  558. + * set to max sector to allow the HW to continue reading over
  559. + * unaligned accesses
  560. + */
  561. + nfi_write16(nfc, NFI_CON, CON_NFI_SEC | CON_NFI_BRD);
  562. +
  563. + /* trigger to fetch data */
  564. + nfi_write16(nfc, NFI_STRDATA, STR_DATA);
  565. + }
  566. +
  567. + mt7621_nfc_wait_pio_ready(nfc);
  568. +
  569. + return nfi_read32(nfc, NFI_DATAR);
  570. +}
  571. +
  572. +static void mt7621_nfc_read_data(struct mt7621_nfc *nfc, u8 *buf, u32 len)
  573. +{
  574. + while (((uintptr_t)buf & 3) && len) {
  575. + *buf = mt7621_nfc_pio_read(nfc, true);
  576. + buf++;
  577. + len--;
  578. + }
  579. +
  580. + while (len >= 4) {
  581. + *(u32 *)buf = mt7621_nfc_pio_read(nfc, false);
  582. + buf += 4;
  583. + len -= 4;
  584. + }
  585. +
  586. + while (len) {
  587. + *buf = mt7621_nfc_pio_read(nfc, true);
  588. + buf++;
  589. + len--;
  590. + }
  591. +}
  592. +
  593. +static void mt7621_nfc_read_data_discard(struct mt7621_nfc *nfc, u32 len)
  594. +{
  595. + while (len >= 4) {
  596. + mt7621_nfc_pio_read(nfc, false);
  597. + len -= 4;
  598. + }
  599. +
  600. + while (len) {
  601. + mt7621_nfc_pio_read(nfc, true);
  602. + len--;
  603. + }
  604. +}
  605. +
  606. +static void mt7621_nfc_pio_write(struct mt7621_nfc *nfc, u32 val, bool bw)
  607. +{
  608. + u32 reg, fsm;
  609. +
  610. + reg = nfi_read32(nfc, NFI_STA);
  611. + fsm = FIELD_GET(STA_NFI_FSM, reg);
  612. +
  613. + if (fsm != STA_FSM_CUSTOM_DATA) {
  614. + reg = nfi_read16(nfc, NFI_CNFG);
  615. + reg &= ~(CNFG_READ_MODE | CNFG_BYTE_RW);
  616. + if (bw)
  617. + reg |= CNFG_BYTE_RW;
  618. + nfi_write16(nfc, NFI_CNFG, reg);
  619. +
  620. + nfi_write16(nfc, NFI_CON, CON_NFI_SEC | CON_NFI_BWR);
  621. + nfi_write16(nfc, NFI_STRDATA, STR_DATA);
  622. + }
  623. +
  624. + mt7621_nfc_wait_pio_ready(nfc);
  625. + nfi_write32(nfc, NFI_DATAW, val);
  626. +}
  627. +
  628. +static void mt7621_nfc_write_data(struct mt7621_nfc *nfc, const u8 *buf,
  629. + u32 len)
  630. +{
  631. + while (((uintptr_t)buf & 3) && len) {
  632. + mt7621_nfc_pio_write(nfc, *buf, true);
  633. + buf++;
  634. + len--;
  635. + }
  636. +
  637. + while (len >= 4) {
  638. + mt7621_nfc_pio_write(nfc, *(const u32 *)buf, false);
  639. + buf += 4;
  640. + len -= 4;
  641. + }
  642. +
  643. + while (len) {
  644. + mt7621_nfc_pio_write(nfc, *buf, true);
  645. + buf++;
  646. + len--;
  647. + }
  648. +}
  649. +
  650. +static void mt7621_nfc_write_data_empty(struct mt7621_nfc *nfc, u32 len)
  651. +{
  652. + while (len >= 4) {
  653. + mt7621_nfc_pio_write(nfc, 0xffffffff, false);
  654. + len -= 4;
  655. + }
  656. +
  657. + while (len) {
  658. + mt7621_nfc_pio_write(nfc, 0xff, true);
  659. + len--;
  660. + }
  661. +}
  662. +
  663. +static void mt7621_nfc_write_byte(struct mtd_info *mtd, u8 byte)
  664. +{
  665. + struct mt7621_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
  666. +
  667. + mt7621_nfc_pio_write(nfc, byte, true);
  668. +}
  669. +
  670. +static void mt7621_nfc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  671. +{
  672. + struct mt7621_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
  673. +
  674. + return mt7621_nfc_write_data(nfc, buf, len);
  675. +}
  676. +
  677. +static u8 mt7621_nfc_read_byte(struct mtd_info *mtd)
  678. +{
  679. + struct mt7621_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
  680. +
  681. + return mt7621_nfc_pio_read(nfc, true);
  682. +}
  683. +
  684. +static void mt7621_nfc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  685. +{
  686. + struct mt7621_nfc *nfc = nand_get_controller_data(mtd_to_nand(mtd));
  687. +
  688. + mt7621_nfc_read_data(nfc, buf, len);
  689. +}
  690. +
  691. +static int mt7621_nfc_calc_ecc_strength(struct mt7621_nfc *nfc,
  692. + u32 avail_ecc_bytes)
  693. +{
  694. + struct nand_chip *nand = &nfc->nand;
  695. + struct mtd_info *mtd = nand_to_mtd(nand);
  696. + u32 strength;
  697. + int i;
  698. +
  699. + strength = avail_ecc_bytes * 8 / ECC_PARITY_BITS;
  700. +
  701. + /* Find the closest supported ecc strength */
  702. + for (i = ARRAY_SIZE(mt7621_ecc_strength) - 1; i >= 0; i--) {
  703. + if (mt7621_ecc_strength[i] <= strength)
  704. + break;
  705. + }
  706. +
  707. + if (unlikely(i < 0)) {
  708. + pr_err("OOB size (%u) is not supported\n", mtd->oobsize);
  709. + return -EINVAL;
  710. + }
  711. +
  712. + nand->ecc.strength = mt7621_ecc_strength[i];
  713. + nand->ecc.bytes = DIV_ROUND_UP(nand->ecc.strength * ECC_PARITY_BITS, 8);
  714. +
  715. + pr_debug("ECC strength adjusted to %u bits\n", nand->ecc.strength);
  716. +
  717. + return i;
  718. +}
  719. +
  720. +static int mt7621_nfc_set_spare_per_sector(struct mt7621_nfc *nfc)
  721. +{
  722. + struct nand_chip *nand = &nfc->nand;
  723. + struct mtd_info *mtd = nand_to_mtd(nand);
  724. + u32 size;
  725. + int i;
  726. +
  727. + size = nand->ecc.bytes + NFI_FDM_SIZE;
  728. +
  729. + /* Find the closest supported spare size */
  730. + for (i = 0; i < ARRAY_SIZE(mt7621_nfi_spare_size); i++) {
  731. + if (mt7621_nfi_spare_size[i] >= size)
  732. + break;
  733. + }
  734. +
  735. + if (unlikely(i >= ARRAY_SIZE(mt7621_nfi_spare_size))) {
  736. + pr_err("OOB size (%u) is not supported\n", mtd->oobsize);
  737. + return -EINVAL;
  738. + }
  739. +
  740. + nfc->spare_per_sector = mt7621_nfi_spare_size[i];
  741. +
  742. + return i;
  743. +}
  744. +
  745. +static int mt7621_nfc_ecc_init(struct mt7621_nfc *nfc)
  746. +{
  747. + struct nand_chip *nand = &nfc->nand;
  748. + struct mtd_info *mtd = nand_to_mtd(nand);
  749. + u32 avail_ecc_bytes, encode_block_size, decode_block_size;
  750. + u32 ecc_enccfg, ecc_deccfg;
  751. + int ecc_cap;
  752. +
  753. + nand->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS;
  754. +
  755. + nand->ecc.size = ECC_SECTOR_SIZE;
  756. + nand->ecc.steps = mtd->writesize / nand->ecc.size;
  757. +
  758. + avail_ecc_bytes = mtd->oobsize / nand->ecc.steps - NFI_FDM_SIZE;
  759. +
  760. + ecc_cap = mt7621_nfc_calc_ecc_strength(nfc, avail_ecc_bytes);
  761. + if (ecc_cap < 0)
  762. + return ecc_cap;
  763. +
  764. + /* Sector + FDM */
  765. + encode_block_size = (nand->ecc.size + NFI_FDM_SIZE) * 8;
  766. + ecc_enccfg = ecc_cap | FIELD_PREP(ENC_MODE, ENC_MODE_NFI) |
  767. + FIELD_PREP(ENC_CNFG_MSG, encode_block_size);
  768. +
  769. + /* Sector + FDM + ECC parity bits */
  770. + decode_block_size = ((nand->ecc.size + NFI_FDM_SIZE) * 8) +
  771. + nand->ecc.strength * ECC_PARITY_BITS;
  772. + ecc_deccfg = ecc_cap | FIELD_PREP(DEC_MODE, DEC_MODE_NFI) |
  773. + FIELD_PREP(DEC_CS, decode_block_size) |
  774. + FIELD_PREP(DEC_CON, DEC_CON_EL) | DEC_EMPTY_EN;
  775. +
  776. + mt7621_ecc_encoder_op(nfc, false);
  777. + ecc_write32(nfc, ECC_ENCCNFG, ecc_enccfg);
  778. +
  779. + mt7621_ecc_decoder_op(nfc, false);
  780. + ecc_write32(nfc, ECC_DECCNFG, ecc_deccfg);
  781. +
  782. + return 0;
  783. +}
  784. +
  785. +static int mt7621_nfc_set_page_format(struct mt7621_nfc *nfc)
  786. +{
  787. + struct nand_chip *nand = &nfc->nand;
  788. + struct mtd_info *mtd = nand_to_mtd(nand);
  789. + int i, spare_size;
  790. + u32 pagefmt;
  791. +
  792. + spare_size = mt7621_nfc_set_spare_per_sector(nfc);
  793. + if (spare_size < 0)
  794. + return spare_size;
  795. +
  796. + for (i = 0; i < ARRAY_SIZE(mt7621_nfi_page_size); i++) {
  797. + if (mt7621_nfi_page_size[i] == mtd->writesize)
  798. + break;
  799. + }
  800. +
  801. + if (unlikely(i >= ARRAY_SIZE(mt7621_nfi_page_size))) {
  802. + pr_err("Page size (%u) is not supported\n", mtd->writesize);
  803. + return -EINVAL;
  804. + }
  805. +
  806. + pagefmt = FIELD_PREP(PAGEFMT_PAGE, i) |
  807. + FIELD_PREP(PAGEFMT_SPARE, spare_size) |
  808. + FIELD_PREP(PAGEFMT_FDM, NFI_FDM_SIZE) |
  809. + FIELD_PREP(PAGEFMT_FDM_ECC, NFI_FDM_SIZE);
  810. +
  811. + nfi_write16(nfc, NFI_PAGEFMT, pagefmt);
  812. +
  813. + return 0;
  814. +}
  815. +
  816. +static int mt7621_nfc_attach_chip(struct nand_chip *nand)
  817. +{
  818. + struct mt7621_nfc *nfc = nand_get_controller_data(nand);
  819. + int ret;
  820. +
  821. + if (nand->options & NAND_BUSWIDTH_16) {
  822. + pr_err("16-bit buswidth is not supported");
  823. + return -EINVAL;
  824. + }
  825. +
  826. + ret = mt7621_nfc_ecc_init(nfc);
  827. + if (ret)
  828. + return ret;
  829. +
  830. + return mt7621_nfc_set_page_format(nfc);
  831. +}
  832. +
  833. +static void mt7621_nfc_write_fdm(struct mt7621_nfc *nfc)
  834. +{
  835. + struct nand_chip *nand = &nfc->nand;
  836. + u32 vall, valm;
  837. + u8 *oobptr;
  838. + int i, j;
  839. +
  840. + for (i = 0; i < nand->ecc.steps; i++) {
  841. + vall = 0;
  842. + valm = 0;
  843. + oobptr = oob_fdm_ptr(nand, i);
  844. +
  845. + for (j = 0; j < 4; j++)
  846. + vall |= (u32)oobptr[j] << (j * 8);
  847. +
  848. + for (j = 0; j < 4; j++)
  849. + valm |= (u32)oobptr[j + 4] << (j * 8);
  850. +
  851. + nfi_write32(nfc, NFI_FDML(i), vall);
  852. + nfi_write32(nfc, NFI_FDMM(i), valm);
  853. + }
  854. +}
  855. +
  856. +static void mt7621_nfc_read_sector_fdm(struct mt7621_nfc *nfc, u32 sect)
  857. +{
  858. + struct nand_chip *nand = &nfc->nand;
  859. + u32 vall, valm;
  860. + u8 *oobptr;
  861. + int i;
  862. +
  863. + vall = nfi_read32(nfc, NFI_FDML(sect));
  864. + valm = nfi_read32(nfc, NFI_FDMM(sect));
  865. + oobptr = oob_fdm_ptr(nand, sect);
  866. +
  867. + for (i = 0; i < 4; i++)
  868. + oobptr[i] = (vall >> (i * 8)) & 0xff;
  869. +
  870. + for (i = 0; i < 4; i++)
  871. + oobptr[i + 4] = (valm >> (i * 8)) & 0xff;
  872. +}
  873. +
  874. +static int mt7621_nfc_read_page_hwecc(struct mtd_info *mtd,
  875. + struct nand_chip *nand, uint8_t *buf,
  876. + int oob_required, int page)
  877. +{
  878. + struct mt7621_nfc *nfc = nand_get_controller_data(nand);
  879. + int bitflips = 0, ret = 0;
  880. + int rc, i;
  881. +
  882. + nand_read_page_op(nand, page, 0, NULL, 0);
  883. +
  884. + nfi_write16(nfc, NFI_CNFG, FIELD_PREP(CNFG_OP_MODE, CNFG_OP_CUSTOM) |
  885. + CNFG_READ_MODE | CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN);
  886. +
  887. + mt7621_ecc_decoder_op(nfc, true);
  888. +
  889. + nfi_write16(nfc, NFI_CON, FIELD_PREP(CON_NFI_SEC, nand->ecc.steps) |
  890. + CON_NFI_BRD);
  891. +
  892. + for (i = 0; i < nand->ecc.steps; i++) {
  893. + if (buf)
  894. + mt7621_nfc_read_data(nfc, page_data_ptr(nand, buf, i),
  895. + nand->ecc.size);
  896. + else
  897. + mt7621_nfc_read_data_discard(nfc, nand->ecc.size);
  898. +
  899. + rc = mt7621_ecc_decoder_wait_done(nfc, i);
  900. +
  901. + mt7621_nfc_read_sector_fdm(nfc, i);
  902. +
  903. + if (rc < 0) {
  904. + ret = -EIO;
  905. + continue;
  906. + }
  907. +
  908. + rc = mt7621_ecc_correct_check(nfc,
  909. + buf ? page_data_ptr(nand, buf, i) : NULL,
  910. + oob_fdm_ptr(nand, i), i);
  911. +
  912. + if (rc < 0) {
  913. + pr_warn("Uncorrectable ECC error at page %d step %d\n",
  914. + page, i);
  915. + bitflips = nand->ecc.strength + 1;
  916. + mtd->ecc_stats.failed++;
  917. + } else {
  918. + if (rc > bitflips)
  919. + bitflips = rc;
  920. + mtd->ecc_stats.corrected += rc;
  921. + }
  922. + }
  923. +
  924. + mt7621_ecc_decoder_op(nfc, false);
  925. +
  926. + nfi_write16(nfc, NFI_CON, 0);
  927. +
  928. + if (ret < 0)
  929. + return ret;
  930. +
  931. + return bitflips;
  932. +}
  933. +
  934. +static int mt7621_nfc_read_page_raw(struct mtd_info *mtd,
  935. + struct nand_chip *nand, uint8_t *buf,
  936. + int oob_required, int page)
  937. +{
  938. + struct mt7621_nfc *nfc = nand_get_controller_data(nand);
  939. + int i;
  940. +
  941. + nand_read_page_op(nand, page, 0, NULL, 0);
  942. +
  943. + nfi_write16(nfc, NFI_CNFG, FIELD_PREP(CNFG_OP_MODE, CNFG_OP_CUSTOM) |
  944. + CNFG_READ_MODE);
  945. +
  946. + nfi_write16(nfc, NFI_CON, FIELD_PREP(CON_NFI_SEC, nand->ecc.steps) |
  947. + CON_NFI_BRD);
  948. +
  949. + for (i = 0; i < nand->ecc.steps; i++) {
  950. + /* Read data */
  951. + if (buf)
  952. + mt7621_nfc_read_data(nfc, page_data_ptr(nand, buf, i),
  953. + nand->ecc.size);
  954. + else
  955. + mt7621_nfc_read_data_discard(nfc, nand->ecc.size);
  956. +
  957. + /* Read FDM */
  958. + mt7621_nfc_read_data(nfc, oob_fdm_ptr(nand, i), NFI_FDM_SIZE);
  959. +
  960. + /* Read ECC parity data */
  961. + mt7621_nfc_read_data(nfc, oob_ecc_ptr(nfc, i),
  962. + nfc->spare_per_sector - NFI_FDM_SIZE);
  963. + }
  964. +
  965. + nfi_write16(nfc, NFI_CON, 0);
  966. +
  967. + return 0;
  968. +}
  969. +
  970. +static int mt7621_nfc_read_oob_hwecc(struct mtd_info *mtd,
  971. + struct nand_chip *nand, int page)
  972. +{
  973. + return mt7621_nfc_read_page_hwecc(mtd, nand, NULL, 1, page);
  974. +}
  975. +
  976. +static int mt7621_nfc_read_oob_raw(struct mtd_info *mtd,
  977. + struct nand_chip *nand, int page)
  978. +{
  979. + return mt7621_nfc_read_page_raw(mtd, nand, NULL, 1, page);
  980. +}
  981. +
  982. +static int mt7621_nfc_check_empty_page(struct nand_chip *nand, const u8 *buf)
  983. +{
  984. + struct mtd_info *mtd = nand_to_mtd(nand);
  985. + u8 *oobptr;
  986. + u32 i, j;
  987. +
  988. + if (buf) {
  989. + for (i = 0; i < mtd->writesize; i++)
  990. + if (buf[i] != 0xff)
  991. + return 0;
  992. + }
  993. +
  994. + for (i = 0; i < nand->ecc.steps; i++) {
  995. + oobptr = oob_fdm_ptr(nand, i);
  996. + for (j = 0; j < NFI_FDM_SIZE; j++)
  997. + if (oobptr[j] != 0xff)
  998. + return 0;
  999. + }
  1000. +
  1001. + return 1;
  1002. +}
  1003. +
  1004. +static int mt7621_nfc_write_page_hwecc(struct mtd_info *mtd,
  1005. + struct nand_chip *nand,
  1006. + const u8 *buf, int oob_required,
  1007. + int page)
  1008. +{
  1009. + struct mt7621_nfc *nfc = nand_get_controller_data(nand);
  1010. +
  1011. + if (mt7621_nfc_check_empty_page(nand, buf)) {
  1012. + /*
  1013. + * MT7621 ECC engine always generates parity code for input
  1014. + * pages, even for empty pages. Doing so will write back ECC
  1015. + * parity code to the oob region, which means such pages will
  1016. + * no longer be empty pages.
  1017. + *
  1018. + * To avoid this, stop write operation if current page is an
  1019. + * empty page.
  1020. + */
  1021. + return 0;
  1022. + }
  1023. +
  1024. + nand_prog_page_begin_op(nand, page, 0, NULL, 0);
  1025. +
  1026. + nfi_write16(nfc, NFI_CNFG, FIELD_PREP(CNFG_OP_MODE, CNFG_OP_CUSTOM) |
  1027. + CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN);
  1028. +
  1029. + mt7621_ecc_encoder_op(nfc, true);
  1030. +
  1031. + mt7621_nfc_write_fdm(nfc);
  1032. +
  1033. + nfi_write16(nfc, NFI_CON, FIELD_PREP(CON_NFI_SEC, nand->ecc.steps) |
  1034. + CON_NFI_BWR);
  1035. +
  1036. + if (buf)
  1037. + mt7621_nfc_write_data(nfc, buf, mtd->writesize);
  1038. + else
  1039. + mt7621_nfc_write_data_empty(nfc, mtd->writesize);
  1040. +
  1041. + mt7621_nfc_wait_write_completion(nfc, nand);
  1042. +
  1043. + mt7621_ecc_encoder_op(nfc, false);
  1044. +
  1045. + nfi_write16(nfc, NFI_CON, 0);
  1046. +
  1047. + return nand_prog_page_end_op(nand);
  1048. +}
  1049. +
  1050. +static int mt7621_nfc_write_page_raw(struct mtd_info *mtd,
  1051. + struct nand_chip *nand,
  1052. + const u8 *buf, int oob_required,
  1053. + int page)
  1054. +{
  1055. + struct mt7621_nfc *nfc = nand_get_controller_data(nand);
  1056. + int i;
  1057. +
  1058. + nand_prog_page_begin_op(nand, page, 0, NULL, 0);
  1059. +
  1060. + nfi_write16(nfc, NFI_CNFG, FIELD_PREP(CNFG_OP_MODE, CNFG_OP_CUSTOM));
  1061. +
  1062. + nfi_write16(nfc, NFI_CON, FIELD_PREP(CON_NFI_SEC, nand->ecc.steps) |
  1063. + CON_NFI_BWR);
  1064. +
  1065. + for (i = 0; i < nand->ecc.steps; i++) {
  1066. + /* Write data */
  1067. + if (buf)
  1068. + mt7621_nfc_write_data(nfc, page_data_ptr(nand, buf, i),
  1069. + nand->ecc.size);
  1070. + else
  1071. + mt7621_nfc_write_data_empty(nfc, nand->ecc.size);
  1072. +
  1073. + /* Write FDM */
  1074. + mt7621_nfc_write_data(nfc, oob_fdm_ptr(nand, i),
  1075. + NFI_FDM_SIZE);
  1076. +
  1077. + /* Write dummy ECC parity data */
  1078. + mt7621_nfc_write_data_empty(nfc, nfc->spare_per_sector -
  1079. + NFI_FDM_SIZE);
  1080. + }
  1081. +
  1082. + mt7621_nfc_wait_write_completion(nfc, nand);
  1083. +
  1084. + nfi_write16(nfc, NFI_CON, 0);
  1085. +
  1086. + return nand_prog_page_end_op(nand);
  1087. +}
  1088. +
  1089. +static int mt7621_nfc_write_oob_hwecc(struct mtd_info *mtd,
  1090. + struct nand_chip *nand, int page)
  1091. +{
  1092. + return mt7621_nfc_write_page_hwecc(mtd, nand, NULL, 1, page);
  1093. +}
  1094. +
  1095. +static int mt7621_nfc_write_oob_raw(struct mtd_info *mtd,
  1096. + struct nand_chip *nand, int page)
  1097. +{
  1098. + return mt7621_nfc_write_page_raw(mtd, nand, NULL, 1, page);
  1099. +}
  1100. +
  1101. +static int mt7621_nfc_ooblayout_free(struct mtd_info *mtd, int section,
  1102. + struct mtd_oob_region *oob_region)
  1103. +{
  1104. + struct nand_chip *nand = mtd_to_nand(mtd);
  1105. +
  1106. + if (section >= nand->ecc.steps)
  1107. + return -ERANGE;
  1108. +
  1109. + oob_region->length = NFI_FDM_SIZE - 1;
  1110. + oob_region->offset = section * NFI_FDM_SIZE + 1;
  1111. +
  1112. + return 0;
  1113. +}
  1114. +
  1115. +static int mt7621_nfc_ooblayout_ecc(struct mtd_info *mtd, int section,
  1116. + struct mtd_oob_region *oob_region)
  1117. +{
  1118. + struct nand_chip *nand = mtd_to_nand(mtd);
  1119. +
  1120. + if (section)
  1121. + return -ERANGE;
  1122. +
  1123. + oob_region->offset = NFI_FDM_SIZE * nand->ecc.steps;
  1124. + oob_region->length = mtd->oobsize - oob_region->offset;
  1125. +
  1126. + return 0;
  1127. +}
  1128. +
  1129. +static const struct mtd_ooblayout_ops mt7621_nfc_ooblayout_ops = {
  1130. + .rfree = mt7621_nfc_ooblayout_free,
  1131. + .ecc = mt7621_nfc_ooblayout_ecc,
  1132. +};
  1133. +
  1134. +/*
  1135. + * This function will override the default one which is not supposed to be
  1136. + * used for ECC syndrome based pages.
  1137. + */
  1138. +static int mt7621_nfc_block_bad(struct mtd_info *mtd, loff_t ofs)
  1139. +{
  1140. + struct nand_chip *nand = mtd_to_nand(mtd);
  1141. + struct mtd_oob_ops ops;
  1142. + int ret, i = 0;
  1143. + u16 bad;
  1144. +
  1145. + memset(&ops, 0, sizeof(ops));
  1146. + ops.oobbuf = (uint8_t *)&bad;
  1147. + ops.ooboffs = nand->badblockpos;
  1148. + if (nand->options & NAND_BUSWIDTH_16) {
  1149. + ops.ooboffs &= ~0x01;
  1150. + ops.ooblen = 2;
  1151. + } else {
  1152. + ops.ooblen = 1;
  1153. + }
  1154. + ops.mode = MTD_OPS_RAW;
  1155. +
  1156. + /* Read from first/last page(s) if necessary */
  1157. + if (nand->bbt_options & NAND_BBT_SCANLASTPAGE)
  1158. + ofs += mtd->erasesize - mtd->writesize;
  1159. +
  1160. + do {
  1161. + ret = mtd_read_oob(mtd, ofs, &ops);
  1162. + if (ret)
  1163. + return ret;
  1164. +
  1165. + if (likely(nand->badblockbits == 8))
  1166. + ret = bad != 0xFF;
  1167. + else
  1168. + ret = hweight8(bad) < nand->badblockbits;
  1169. +
  1170. + i++;
  1171. + ofs += mtd->writesize;
  1172. + } while (!ret && (nand->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
  1173. +
  1174. + return ret;
  1175. +}
  1176. +
  1177. +static void mt7621_nfc_init_chip(struct mt7621_nfc *nfc)
  1178. +{
  1179. + struct nand_chip *nand = &nfc->nand;
  1180. + struct mtd_info *mtd;
  1181. + int ret;
  1182. +
  1183. + nand_set_controller_data(nand, nfc);
  1184. +
  1185. + nand->options |= NAND_NO_SUBPAGE_WRITE;
  1186. +
  1187. + nand->ecc.mode = NAND_ECC_HW_SYNDROME;
  1188. + nand->ecc.read_page = mt7621_nfc_read_page_hwecc;
  1189. + nand->ecc.read_page_raw = mt7621_nfc_read_page_raw;
  1190. + nand->ecc.write_page = mt7621_nfc_write_page_hwecc;
  1191. + nand->ecc.write_page_raw = mt7621_nfc_write_page_raw;
  1192. + nand->ecc.read_oob = mt7621_nfc_read_oob_hwecc;
  1193. + nand->ecc.read_oob_raw = mt7621_nfc_read_oob_raw;
  1194. + nand->ecc.write_oob = mt7621_nfc_write_oob_hwecc;
  1195. + nand->ecc.write_oob_raw = mt7621_nfc_write_oob_raw;
  1196. +
  1197. + nand->dev_ready = mt7621_nfc_dev_ready;
  1198. + nand->select_chip = mt7621_nfc_select_chip;
  1199. + nand->write_byte = mt7621_nfc_write_byte;
  1200. + nand->write_buf = mt7621_nfc_write_buf;
  1201. + nand->read_byte = mt7621_nfc_read_byte;
  1202. + nand->read_buf = mt7621_nfc_read_buf;
  1203. + nand->cmd_ctrl = mt7621_nfc_cmd_ctrl;
  1204. + nand->block_bad = mt7621_nfc_block_bad;
  1205. +
  1206. + mtd = nand_to_mtd(nand);
  1207. + mtd_set_ooblayout(mtd, &mt7621_nfc_ooblayout_ops);
  1208. +
  1209. + /* Reset NFI master */
  1210. + mt7621_nfc_hw_init(nfc);
  1211. +
  1212. + ret = nand_scan_ident(mtd, 1, NULL);
  1213. + if (ret)
  1214. + return;
  1215. +
  1216. + mt7621_nfc_attach_chip(nand);
  1217. +
  1218. + ret = nand_scan_tail(mtd);
  1219. + if (ret)
  1220. + return;
  1221. +
  1222. + nand_register(0, mtd);
  1223. +}
  1224. +
  1225. +static void mt7621_nfc_set_regs(struct mt7621_nfc *nfc)
  1226. +{
  1227. + nfc->nfi_regs = (void __iomem *)CKSEG1ADDR(NFI_BASE);
  1228. + nfc->ecc_regs = (void __iomem *)CKSEG1ADDR(NFI_ECC_BASE);
  1229. +}
  1230. +
  1231. +void mt7621_nfc_spl_init(struct mt7621_nfc *nfc)
  1232. +{
  1233. + struct nand_chip *nand = &nfc->nand;
  1234. +
  1235. + mt7621_nfc_set_regs(nfc);
  1236. +
  1237. + nand_set_controller_data(nand, nfc);
  1238. +
  1239. + nand->options |= NAND_NO_SUBPAGE_WRITE;
  1240. +
  1241. + nand->ecc.mode = NAND_ECC_HW_SYNDROME;
  1242. + nand->ecc.read_page = mt7621_nfc_read_page_hwecc;
  1243. +
  1244. + nand->dev_ready = mt7621_nfc_dev_ready;
  1245. + nand->select_chip = mt7621_nfc_select_chip;
  1246. + nand->read_byte = mt7621_nfc_read_byte;
  1247. + nand->read_buf = mt7621_nfc_read_buf;
  1248. + nand->cmd_ctrl = mt7621_nfc_cmd_ctrl;
  1249. +
  1250. + /* Reset NFI master */
  1251. + mt7621_nfc_hw_init(nfc);
  1252. +}
  1253. +
  1254. +int mt7621_nfc_spl_post_init(struct mt7621_nfc *nfc)
  1255. +{
  1256. + struct nand_chip *nand = &nfc->nand;
  1257. + int nand_maf_id, nand_dev_id;
  1258. + struct nand_flash_dev *type;
  1259. +
  1260. + type = nand_get_flash_type(&nand->mtd, nand, &nand_maf_id,
  1261. + &nand_dev_id, NULL);
  1262. +
  1263. + if (IS_ERR(type))
  1264. + return PTR_ERR(type);
  1265. +
  1266. + nand->numchips = 1;
  1267. + nand->mtd.size = nand->chipsize;
  1268. +
  1269. + return mt7621_nfc_attach_chip(nand);
  1270. +}
  1271. +
  1272. +void board_nand_init(void)
  1273. +{
  1274. + mt7621_nfc_set_regs(&nfc_dev);
  1275. + mt7621_nfc_init_chip(&nfc_dev);
  1276. +}
  1277. --- /dev/null
  1278. +++ b/drivers/mtd/nand/raw/mt7621_nand.h
  1279. @@ -0,0 +1,29 @@
  1280. +/* SPDX-License-Identifier: GPL-2.0 */
  1281. +/*
  1282. + * Copyright (C) 2022 MediaTek Inc. All rights reserved.
  1283. + *
  1284. + * Author: Weijie Gao <[email protected]>
  1285. + */
  1286. +
  1287. +#ifndef _MT7621_NAND_H_
  1288. +#define _MT7621_NAND_H_
  1289. +
  1290. +#include <linux/types.h>
  1291. +#include <linux/mtd/mtd.h>
  1292. +#include <linux/compiler.h>
  1293. +#include <linux/mtd/rawnand.h>
  1294. +
  1295. +struct mt7621_nfc {
  1296. + struct nand_chip nand;
  1297. +
  1298. + void __iomem *nfi_regs;
  1299. + void __iomem *ecc_regs;
  1300. +
  1301. + u32 spare_per_sector;
  1302. +};
  1303. +
  1304. +/* for SPL */
  1305. +void mt7621_nfc_spl_init(struct mt7621_nfc *nfc);
  1306. +int mt7621_nfc_spl_post_init(struct mt7621_nfc *nfc);
  1307. +
  1308. +#endif /* _MT7621_NAND_H_ */
  1309. --- /dev/null
  1310. +++ b/drivers/mtd/nand/raw/mt7621_nand_spl.c
  1311. @@ -0,0 +1,237 @@
  1312. +// SPDX-License-Identifier: GPL-2.0
  1313. +/*
  1314. + * Copyright (C) 2022 MediaTek Inc. All rights reserved.
  1315. + *
  1316. + * Author: Weijie Gao <[email protected]>
  1317. + */
  1318. +
  1319. +#include <image.h>
  1320. +#include <malloc.h>
  1321. +#include <linux/sizes.h>
  1322. +#include <linux/delay.h>
  1323. +#include <linux/mtd/rawnand.h>
  1324. +#include "mt7621_nand.h"
  1325. +
  1326. +static struct mt7621_nfc nfc_dev;
  1327. +static u8 *buffer;
  1328. +static int nand_valid;
  1329. +
  1330. +static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  1331. + int column, int page_addr)
  1332. +{
  1333. + register struct nand_chip *chip = mtd_to_nand(mtd);
  1334. +
  1335. + /* Command latch cycle */
  1336. + chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  1337. +
  1338. + if (column != -1 || page_addr != -1) {
  1339. + int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  1340. +
  1341. + /* Serially input address */
  1342. + if (column != -1) {
  1343. + chip->cmd_ctrl(mtd, column, ctrl);
  1344. + ctrl &= ~NAND_CTRL_CHANGE;
  1345. + if (command != NAND_CMD_READID)
  1346. + chip->cmd_ctrl(mtd, column >> 8, ctrl);
  1347. + }
  1348. + if (page_addr != -1) {
  1349. + chip->cmd_ctrl(mtd, page_addr, ctrl);
  1350. + chip->cmd_ctrl(mtd, page_addr >> 8,
  1351. + NAND_NCE | NAND_ALE);
  1352. + if (chip->options & NAND_ROW_ADDR_3)
  1353. + chip->cmd_ctrl(mtd, page_addr >> 16,
  1354. + NAND_NCE | NAND_ALE);
  1355. + }
  1356. + }
  1357. + chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  1358. +
  1359. + /*
  1360. + * Program and erase have their own busy handlers status, sequential
  1361. + * in and status need no delay.
  1362. + */
  1363. + switch (command) {
  1364. + case NAND_CMD_STATUS:
  1365. + case NAND_CMD_READID:
  1366. + case NAND_CMD_SET_FEATURES:
  1367. + return;
  1368. +
  1369. + case NAND_CMD_READ0:
  1370. + chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
  1371. + NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  1372. + chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  1373. + NAND_NCE | NAND_CTRL_CHANGE);
  1374. + }
  1375. +
  1376. + /*
  1377. + * Apply this short delay always to ensure that we do wait tWB in
  1378. + * any case on any machine.
  1379. + */
  1380. + ndelay(100);
  1381. +
  1382. + nand_wait_ready(mtd);
  1383. +}
  1384. +
  1385. +static int nfc_read_page_hwecc(struct mtd_info *mtd, void *buf,
  1386. + unsigned int page)
  1387. +{
  1388. + struct nand_chip *chip = mtd_to_nand(mtd);
  1389. + int ret;
  1390. +
  1391. + chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page);
  1392. +
  1393. + ret = chip->ecc.read_page(mtd, chip, buf, 1, page);
  1394. + if (ret < 0 || ret > chip->ecc.strength)
  1395. + return -1;
  1396. +
  1397. + return 0;
  1398. +}
  1399. +
  1400. +static int nfc_read_oob_hwecc(struct mtd_info *mtd, void *buf, u32 len,
  1401. + unsigned int page)
  1402. +{
  1403. + struct nand_chip *chip = mtd_to_nand(mtd);
  1404. + int ret;
  1405. +
  1406. + chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page);
  1407. +
  1408. + ret = chip->ecc.read_page(mtd, chip, NULL, 1, page);
  1409. + if (ret < 0)
  1410. + return -1;
  1411. +
  1412. + if (len > mtd->oobsize)
  1413. + len = mtd->oobsize;
  1414. +
  1415. + memcpy(buf, chip->oob_poi, len);
  1416. +
  1417. + return 0;
  1418. +}
  1419. +
  1420. +static int nfc_check_bad_block(struct mtd_info *mtd, unsigned int page)
  1421. +{
  1422. + struct nand_chip *chip = mtd_to_nand(mtd);
  1423. + u32 pages_per_block, i = 0;
  1424. + int ret;
  1425. + u8 bad;
  1426. +
  1427. + pages_per_block = 1 << (mtd->erasesize_shift - mtd->writesize_shift);
  1428. +
  1429. + /* Read from first/last page(s) if necessary */
  1430. + if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) {
  1431. + page += pages_per_block - 1;
  1432. + if (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)
  1433. + page--;
  1434. + }
  1435. +
  1436. + do {
  1437. + ret = nfc_read_oob_hwecc(mtd, &bad, 1, page);
  1438. + if (ret)
  1439. + return ret;
  1440. +
  1441. + ret = bad != 0xFF;
  1442. +
  1443. + i++;
  1444. + page++;
  1445. + } while (!ret && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
  1446. +
  1447. + return ret;
  1448. +}
  1449. +
  1450. +int nand_spl_load_image(uint32_t offs, unsigned int size, void *dest)
  1451. +{
  1452. + struct mt7621_nfc *nfc = &nfc_dev;
  1453. + struct nand_chip *chip = &nfc->nand;
  1454. + struct mtd_info *mtd = &chip->mtd;
  1455. + u32 addr, col, page, chksz;
  1456. + bool check_bad = true;
  1457. +
  1458. + if (!nand_valid)
  1459. + return -ENODEV;
  1460. +
  1461. + while (size) {
  1462. + if (check_bad || !(offs & mtd->erasesize_mask)) {
  1463. + addr = offs & (~mtd->erasesize_mask);
  1464. + page = addr >> mtd->writesize_shift;
  1465. + if (nfc_check_bad_block(mtd, page)) {
  1466. + /* Skip bad block */
  1467. + if (addr >= mtd->size - mtd->erasesize)
  1468. + return -1;
  1469. +
  1470. + offs += mtd->erasesize;
  1471. + continue;
  1472. + }
  1473. +
  1474. + check_bad = false;
  1475. + }
  1476. +
  1477. + col = offs & mtd->writesize_mask;
  1478. + page = offs >> mtd->writesize_shift;
  1479. + chksz = min(mtd->writesize - col, (uint32_t)size);
  1480. +
  1481. + if (unlikely(chksz < mtd->writesize)) {
  1482. + /* Not reading a full page */
  1483. + if (nfc_read_page_hwecc(mtd, buffer, page))
  1484. + return -1;
  1485. +
  1486. + memcpy(dest, buffer + col, chksz);
  1487. + } else {
  1488. + if (nfc_read_page_hwecc(mtd, dest, page))
  1489. + return -1;
  1490. + }
  1491. +
  1492. + dest += chksz;
  1493. + offs += chksz;
  1494. + size -= chksz;
  1495. + }
  1496. +
  1497. + return 0;
  1498. +}
  1499. +
  1500. +int nand_default_bbt(struct mtd_info *mtd)
  1501. +{
  1502. + return 0;
  1503. +}
  1504. +
  1505. +unsigned long nand_size(void)
  1506. +{
  1507. + if (!nand_valid)
  1508. + return 0;
  1509. +
  1510. + /* Unlikely that NAND size > 2GBytes */
  1511. + if (nfc_dev.nand.chipsize <= SZ_2G)
  1512. + return nfc_dev.nand.chipsize;
  1513. +
  1514. + return SZ_2G;
  1515. +}
  1516. +
  1517. +void nand_deselect(void)
  1518. +{
  1519. +}
  1520. +
  1521. +void nand_init(void)
  1522. +{
  1523. + struct mtd_info *mtd;
  1524. + struct nand_chip *chip;
  1525. +
  1526. + if (nand_valid)
  1527. + return;
  1528. +
  1529. + mt7621_nfc_spl_init(&nfc_dev);
  1530. +
  1531. + chip = &nfc_dev.nand;
  1532. + mtd = &chip->mtd;
  1533. + chip->cmdfunc = nand_command_lp;
  1534. +
  1535. + if (mt7621_nfc_spl_post_init(&nfc_dev))
  1536. + return;
  1537. +
  1538. + mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  1539. + mtd->writesize_shift = ffs(mtd->writesize) - 1;
  1540. + mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  1541. + mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  1542. +
  1543. + buffer = malloc(mtd->writesize);
  1544. + if (!buffer)
  1545. + return;
  1546. +
  1547. + nand_valid = 1;
  1548. +}