stm32_fmc2_nand.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <limits.h>
  9. #include <stdint.h>
  10. #include <common/debug.h>
  11. #include <drivers/clk.h>
  12. #include <drivers/delay_timer.h>
  13. #include <drivers/raw_nand.h>
  14. #include <drivers/st/stm32_fmc2_nand.h>
  15. #include <drivers/st/stm32_gpio.h>
  16. #include <drivers/st/stm32mp_reset.h>
  17. #include <lib/mmio.h>
  18. #include <lib/utils_def.h>
  19. #include <libfdt.h>
  20. #include <platform_def.h>
  21. /* Timeout for device interface reset */
  22. #define TIMEOUT_US_1_MS 1000U
  23. /* FMC2 Compatibility */
  24. #define DT_FMC2_EBI_COMPAT "st,stm32mp1-fmc2-ebi"
  25. #define DT_FMC2_NFC_COMPAT "st,stm32mp1-fmc2-nfc"
  26. #define MAX_CS 2U
  27. #define MAX_BANK 5U
  28. /* FMC2 Controller Registers */
  29. #define FMC2_BCR1 0x00U
  30. #define FMC2_PCR 0x80U
  31. #define FMC2_SR 0x84U
  32. #define FMC2_PMEM 0x88U
  33. #define FMC2_PATT 0x8CU
  34. #define FMC2_HECCR 0x94U
  35. #define FMC2_BCHISR 0x254U
  36. #define FMC2_BCHICR 0x258U
  37. #define FMC2_BCHDSR0 0x27CU
  38. #define FMC2_BCHDSR1 0x280U
  39. #define FMC2_BCHDSR2 0x284U
  40. #define FMC2_BCHDSR3 0x288U
  41. #define FMC2_BCHDSR4 0x28CU
  42. /* FMC2_BCR1 register */
  43. #define FMC2_BCR1_FMC2EN BIT(31)
  44. /* FMC2_PCR register */
  45. #define FMC2_PCR_PWAITEN BIT(1)
  46. #define FMC2_PCR_PBKEN BIT(2)
  47. #define FMC2_PCR_PWID_MASK GENMASK_32(5, 4)
  48. #define FMC2_PCR_PWID(x) (((x) << 4) & FMC2_PCR_PWID_MASK)
  49. #define FMC2_PCR_PWID_8 0x0U
  50. #define FMC2_PCR_PWID_16 0x1U
  51. #define FMC2_PCR_ECCEN BIT(6)
  52. #define FMC2_PCR_ECCALG BIT(8)
  53. #define FMC2_PCR_TCLR_MASK GENMASK_32(12, 9)
  54. #define FMC2_PCR_TCLR(x) (((x) << 9) & FMC2_PCR_TCLR_MASK)
  55. #define FMC2_PCR_TCLR_DEFAULT 0xFU
  56. #define FMC2_PCR_TAR_MASK GENMASK_32(16, 13)
  57. #define FMC2_PCR_TAR(x) (((x) << 13) & FMC2_PCR_TAR_MASK)
  58. #define FMC2_PCR_TAR_DEFAULT 0xFU
  59. #define FMC2_PCR_ECCSS_MASK GENMASK_32(19, 17)
  60. #define FMC2_PCR_ECCSS(x) (((x) << 17) & FMC2_PCR_ECCSS_MASK)
  61. #define FMC2_PCR_ECCSS_512 0x1U
  62. #define FMC2_PCR_ECCSS_2048 0x3U
  63. #define FMC2_PCR_BCHECC BIT(24)
  64. #define FMC2_PCR_WEN BIT(25)
  65. /* FMC2_SR register */
  66. #define FMC2_SR_NWRF BIT(6)
  67. /* FMC2_PMEM register*/
  68. #define FMC2_PMEM_MEMSET(x) (((x) & GENMASK_32(7, 0)) << 0)
  69. #define FMC2_PMEM_MEMWAIT(x) (((x) & GENMASK_32(7, 0)) << 8)
  70. #define FMC2_PMEM_MEMHOLD(x) (((x) & GENMASK_32(7, 0)) << 16)
  71. #define FMC2_PMEM_MEMHIZ(x) (((x) & GENMASK_32(7, 0)) << 24)
  72. #define FMC2_PMEM_DEFAULT 0x0A0A0A0AU
  73. /* FMC2_PATT register */
  74. #define FMC2_PATT_ATTSET(x) (((x) & GENMASK_32(7, 0)) << 0)
  75. #define FMC2_PATT_ATTWAIT(x) (((x) & GENMASK_32(7, 0)) << 8)
  76. #define FMC2_PATT_ATTHOLD(x) (((x) & GENMASK_32(7, 0)) << 16)
  77. #define FMC2_PATT_ATTHIZ(x) (((x) & GENMASK_32(7, 0)) << 24)
  78. #define FMC2_PATT_DEFAULT 0x0A0A0A0AU
  79. /* FMC2_BCHISR register */
  80. #define FMC2_BCHISR_DERF BIT(1)
  81. /* FMC2_BCHICR register */
  82. #define FMC2_BCHICR_CLEAR_IRQ GENMASK_32(4, 0)
  83. /* FMC2_BCHDSR0 register */
  84. #define FMC2_BCHDSR0_DUE BIT(0)
  85. #define FMC2_BCHDSR0_DEF BIT(1)
  86. #define FMC2_BCHDSR0_DEN_MASK GENMASK_32(7, 4)
  87. #define FMC2_BCHDSR0_DEN_SHIFT 4U
  88. /* FMC2_BCHDSR1 register */
  89. #define FMC2_BCHDSR1_EBP1_MASK GENMASK_32(12, 0)
  90. #define FMC2_BCHDSR1_EBP2_MASK GENMASK_32(28, 16)
  91. #define FMC2_BCHDSR1_EBP2_SHIFT 16U
  92. /* FMC2_BCHDSR2 register */
  93. #define FMC2_BCHDSR2_EBP3_MASK GENMASK_32(12, 0)
  94. #define FMC2_BCHDSR2_EBP4_MASK GENMASK_32(28, 16)
  95. #define FMC2_BCHDSR2_EBP4_SHIFT 16U
  96. /* FMC2_BCHDSR3 register */
  97. #define FMC2_BCHDSR3_EBP5_MASK GENMASK_32(12, 0)
  98. #define FMC2_BCHDSR3_EBP6_MASK GENMASK_32(28, 16)
  99. #define FMC2_BCHDSR3_EBP6_SHIFT 16U
  100. /* FMC2_BCHDSR4 register */
  101. #define FMC2_BCHDSR4_EBP7_MASK GENMASK_32(12, 0)
  102. #define FMC2_BCHDSR4_EBP8_MASK GENMASK_32(28, 16)
  103. #define FMC2_BCHDSR4_EBP8_SHIFT 16U
  104. /* Timings */
  105. #define FMC2_THIZ 0x01U
  106. #define FMC2_TIO 8000U
  107. #define FMC2_TSYNC 3000U
  108. #define FMC2_PCR_TIMING_MASK GENMASK_32(3, 0)
  109. #define FMC2_PMEM_PATT_TIMING_MASK GENMASK_32(7, 0)
  110. #define FMC2_BBM_LEN 2U
  111. #define FMC2_MAX_ECC_BYTES 14U
  112. #define TIMEOUT_US_10_MS 10000U
  113. #define FMC2_PSEC_PER_MSEC (1000UL * 1000UL * 1000UL)
  114. enum stm32_fmc2_ecc {
  115. FMC2_ECC_HAM = 1U,
  116. FMC2_ECC_BCH4 = 4U,
  117. FMC2_ECC_BCH8 = 8U
  118. };
  119. struct stm32_fmc2_cs_reg {
  120. uintptr_t data_base;
  121. uintptr_t cmd_base;
  122. uintptr_t addr_base;
  123. };
  124. struct stm32_fmc2_nand_timings {
  125. uint8_t tclr;
  126. uint8_t tar;
  127. uint8_t thiz;
  128. uint8_t twait;
  129. uint8_t thold_mem;
  130. uint8_t tset_mem;
  131. uint8_t thold_att;
  132. uint8_t tset_att;
  133. };
  134. struct stm32_fmc2_nfc {
  135. uintptr_t reg_base;
  136. struct stm32_fmc2_cs_reg cs[MAX_CS];
  137. unsigned long clock_id;
  138. unsigned int reset_id;
  139. uint8_t cs_sel;
  140. };
  141. static struct stm32_fmc2_nfc stm32_fmc2;
  142. static uintptr_t fmc2_base(void)
  143. {
  144. return stm32_fmc2.reg_base;
  145. }
  146. static void stm32_fmc2_nand_setup_timing(void)
  147. {
  148. struct stm32_fmc2_nand_timings tims;
  149. unsigned long hclk = clk_get_rate(stm32_fmc2.clock_id);
  150. unsigned long hclkp = FMC2_PSEC_PER_MSEC / (hclk / 1000U);
  151. unsigned long timing, tar, tclr, thiz, twait;
  152. unsigned long tset_mem, tset_att, thold_mem, thold_att;
  153. uint32_t pcr, pmem, patt;
  154. tar = MAX(hclkp, NAND_TAR_MIN);
  155. timing = div_round_up(tar, hclkp) - 1U;
  156. tims.tar = MIN(timing, (unsigned long)FMC2_PCR_TIMING_MASK);
  157. tclr = MAX(hclkp, NAND_TCLR_MIN);
  158. timing = div_round_up(tclr, hclkp) - 1U;
  159. tims.tclr = MIN(timing, (unsigned long)FMC2_PCR_TIMING_MASK);
  160. tims.thiz = FMC2_THIZ;
  161. thiz = (tims.thiz + 1U) * hclkp;
  162. /*
  163. * tWAIT > tRP
  164. * tWAIT > tWP
  165. * tWAIT > tREA + tIO
  166. */
  167. twait = MAX(hclkp, NAND_TRP_MIN);
  168. twait = MAX(twait, NAND_TWP_MIN);
  169. twait = MAX(twait, NAND_TREA_MAX + FMC2_TIO);
  170. timing = div_round_up(twait, hclkp);
  171. tims.twait = CLAMP(timing, 1UL,
  172. (unsigned long)FMC2_PMEM_PATT_TIMING_MASK);
  173. /*
  174. * tSETUP_MEM > tCS - tWAIT
  175. * tSETUP_MEM > tALS - tWAIT
  176. * tSETUP_MEM > tDS - (tWAIT - tHIZ)
  177. */
  178. tset_mem = hclkp;
  179. if ((twait < NAND_TCS_MIN) && (tset_mem < (NAND_TCS_MIN - twait))) {
  180. tset_mem = NAND_TCS_MIN - twait;
  181. }
  182. if ((twait > thiz) && ((twait - thiz) < NAND_TDS_MIN) &&
  183. (tset_mem < (NAND_TDS_MIN - (twait - thiz)))) {
  184. tset_mem = NAND_TDS_MIN - (twait - thiz);
  185. }
  186. timing = div_round_up(tset_mem, hclkp);
  187. tims.tset_mem = CLAMP(timing, 1UL,
  188. (unsigned long)FMC2_PMEM_PATT_TIMING_MASK);
  189. /*
  190. * tHOLD_MEM > tCH
  191. * tHOLD_MEM > tREH - tSETUP_MEM
  192. * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT)
  193. */
  194. thold_mem = MAX(hclkp, NAND_TCH_MIN);
  195. if ((tset_mem < NAND_TREH_MIN) &&
  196. (thold_mem < (NAND_TREH_MIN - tset_mem))) {
  197. thold_mem = NAND_TREH_MIN - tset_mem;
  198. }
  199. if (((tset_mem + twait) < NAND_TRC_MIN) &&
  200. (thold_mem < (NAND_TRC_MIN - (tset_mem + twait)))) {
  201. thold_mem = NAND_TRC_MIN - (tset_mem + twait);
  202. }
  203. if (((tset_mem + twait) < NAND_TWC_MIN) &&
  204. (thold_mem < (NAND_TWC_MIN - (tset_mem + twait)))) {
  205. thold_mem = NAND_TWC_MIN - (tset_mem + twait);
  206. }
  207. timing = div_round_up(thold_mem, hclkp);
  208. tims.thold_mem = CLAMP(timing, 1UL,
  209. (unsigned long)FMC2_PMEM_PATT_TIMING_MASK);
  210. /*
  211. * tSETUP_ATT > tCS - tWAIT
  212. * tSETUP_ATT > tCLS - tWAIT
  213. * tSETUP_ATT > tALS - tWAIT
  214. * tSETUP_ATT > tRHW - tHOLD_MEM
  215. * tSETUP_ATT > tDS - (tWAIT - tHIZ)
  216. */
  217. tset_att = hclkp;
  218. if ((twait < NAND_TCS_MIN) && (tset_att < (NAND_TCS_MIN - twait))) {
  219. tset_att = NAND_TCS_MIN - twait;
  220. }
  221. if ((thold_mem < NAND_TRHW_MIN) &&
  222. (tset_att < (NAND_TRHW_MIN - thold_mem))) {
  223. tset_att = NAND_TRHW_MIN - thold_mem;
  224. }
  225. if ((twait > thiz) && ((twait - thiz) < NAND_TDS_MIN) &&
  226. (tset_att < (NAND_TDS_MIN - (twait - thiz)))) {
  227. tset_att = NAND_TDS_MIN - (twait - thiz);
  228. }
  229. timing = div_round_up(tset_att, hclkp);
  230. tims.tset_att = CLAMP(timing, 1UL,
  231. (unsigned long)FMC2_PMEM_PATT_TIMING_MASK);
  232. /*
  233. * tHOLD_ATT > tALH
  234. * tHOLD_ATT > tCH
  235. * tHOLD_ATT > tCLH
  236. * tHOLD_ATT > tCOH
  237. * tHOLD_ATT > tDH
  238. * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM
  239. * tHOLD_ATT > tADL - tSETUP_MEM
  240. * tHOLD_ATT > tWH - tSETUP_MEM
  241. * tHOLD_ATT > tWHR - tSETUP_MEM
  242. * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT)
  243. * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT)
  244. */
  245. thold_att = MAX(hclkp, NAND_TALH_MIN);
  246. thold_att = MAX(thold_att, NAND_TCH_MIN);
  247. thold_att = MAX(thold_att, NAND_TCLH_MIN);
  248. thold_att = MAX(thold_att, NAND_TCOH_MIN);
  249. thold_att = MAX(thold_att, NAND_TDH_MIN);
  250. if (((NAND_TWB_MAX + FMC2_TIO + FMC2_TSYNC) > tset_mem) &&
  251. (thold_att < (NAND_TWB_MAX + FMC2_TIO + FMC2_TSYNC - tset_mem))) {
  252. thold_att = NAND_TWB_MAX + FMC2_TIO + FMC2_TSYNC - tset_mem;
  253. }
  254. if ((tset_mem < NAND_TADL_MIN) &&
  255. (thold_att < (NAND_TADL_MIN - tset_mem))) {
  256. thold_att = NAND_TADL_MIN - tset_mem;
  257. }
  258. if ((tset_mem < NAND_TWH_MIN) &&
  259. (thold_att < (NAND_TWH_MIN - tset_mem))) {
  260. thold_att = NAND_TWH_MIN - tset_mem;
  261. }
  262. if ((tset_mem < NAND_TWHR_MIN) &&
  263. (thold_att < (NAND_TWHR_MIN - tset_mem))) {
  264. thold_att = NAND_TWHR_MIN - tset_mem;
  265. }
  266. if (((tset_att + twait) < NAND_TRC_MIN) &&
  267. (thold_att < (NAND_TRC_MIN - (tset_att + twait)))) {
  268. thold_att = NAND_TRC_MIN - (tset_att + twait);
  269. }
  270. if (((tset_att + twait) < NAND_TWC_MIN) &&
  271. (thold_att < (NAND_TWC_MIN - (tset_att + twait)))) {
  272. thold_att = NAND_TWC_MIN - (tset_att + twait);
  273. }
  274. timing = div_round_up(thold_att, hclkp);
  275. tims.thold_att = CLAMP(timing, 1UL,
  276. (unsigned long)FMC2_PMEM_PATT_TIMING_MASK);
  277. VERBOSE("NAND timings: %u - %u - %u - %u - %u - %u - %u - %u\n",
  278. tims.tclr, tims.tar, tims.thiz, tims.twait,
  279. tims.thold_mem, tims.tset_mem,
  280. tims.thold_att, tims.tset_att);
  281. /* Set tclr/tar timings */
  282. pcr = mmio_read_32(fmc2_base() + FMC2_PCR);
  283. pcr &= ~FMC2_PCR_TCLR_MASK;
  284. pcr |= FMC2_PCR_TCLR(tims.tclr);
  285. pcr &= ~FMC2_PCR_TAR_MASK;
  286. pcr |= FMC2_PCR_TAR(tims.tar);
  287. /* Set tset/twait/thold/thiz timings in common bank */
  288. pmem = FMC2_PMEM_MEMSET(tims.tset_mem);
  289. pmem |= FMC2_PMEM_MEMWAIT(tims.twait);
  290. pmem |= FMC2_PMEM_MEMHOLD(tims.thold_mem);
  291. pmem |= FMC2_PMEM_MEMHIZ(tims.thiz);
  292. /* Set tset/twait/thold/thiz timings in attribute bank */
  293. patt = FMC2_PATT_ATTSET(tims.tset_att);
  294. patt |= FMC2_PATT_ATTWAIT(tims.twait);
  295. patt |= FMC2_PATT_ATTHOLD(tims.thold_att);
  296. patt |= FMC2_PATT_ATTHIZ(tims.thiz);
  297. mmio_write_32(fmc2_base() + FMC2_PCR, pcr);
  298. mmio_write_32(fmc2_base() + FMC2_PMEM, pmem);
  299. mmio_write_32(fmc2_base() + FMC2_PATT, patt);
  300. }
  301. static void stm32_fmc2_set_buswidth_16(bool set)
  302. {
  303. mmio_clrsetbits_32(fmc2_base() + FMC2_PCR, FMC2_PCR_PWID_MASK,
  304. (set ? FMC2_PCR_PWID(FMC2_PCR_PWID_16) : 0U));
  305. }
  306. static void stm32_fmc2_set_ecc(bool enable)
  307. {
  308. mmio_clrsetbits_32(fmc2_base() + FMC2_PCR, FMC2_PCR_ECCEN,
  309. (enable ? FMC2_PCR_ECCEN : 0U));
  310. }
  311. static int stm32_fmc2_ham_correct(uint8_t *buffer, uint8_t *eccbuffer,
  312. uint8_t *ecc)
  313. {
  314. uint8_t xor_ecc_ones;
  315. uint16_t xor_ecc_1b, xor_ecc_2b, xor_ecc_3b;
  316. union {
  317. uint32_t val;
  318. uint8_t bytes[4];
  319. } xor_ecc;
  320. /* Page size--------ECC_Code Size
  321. * 256---------------22 bits LSB (ECC_CODE & 0x003FFFFF)
  322. * 512---------------24 bits (ECC_CODE & 0x00FFFFFF)
  323. * 1024--------------26 bits (ECC_CODE & 0x03FFFFFF)
  324. * 2048--------------28 bits (ECC_CODE & 0x0FFFFFFF)
  325. * 4096--------------30 bits (ECC_CODE & 0x3FFFFFFF)
  326. * 8192--------------32 bits (ECC_CODE & 0xFFFFFFFF)
  327. */
  328. /* For Page size 512, ECC_Code size 24 bits */
  329. xor_ecc_1b = ecc[0] ^ eccbuffer[0];
  330. xor_ecc_2b = ecc[1] ^ eccbuffer[1];
  331. xor_ecc_3b = ecc[2] ^ eccbuffer[2];
  332. xor_ecc.val = 0U;
  333. xor_ecc.bytes[2] = xor_ecc_3b;
  334. xor_ecc.bytes[1] = xor_ecc_2b;
  335. xor_ecc.bytes[0] = xor_ecc_1b;
  336. if (xor_ecc.val == 0U) {
  337. return 0; /* No Error */
  338. }
  339. xor_ecc_ones = __builtin_popcount(xor_ecc.val);
  340. if (xor_ecc_ones < 23U) {
  341. if (xor_ecc_ones == 12U) {
  342. uint16_t bit_address, byte_address;
  343. /* Correctable ERROR */
  344. bit_address = ((xor_ecc_1b >> 1) & BIT(0)) |
  345. ((xor_ecc_1b >> 2) & BIT(1)) |
  346. ((xor_ecc_1b >> 3) & BIT(2));
  347. byte_address = ((xor_ecc_1b >> 7) & BIT(0)) |
  348. ((xor_ecc_2b) & BIT(1)) |
  349. ((xor_ecc_2b >> 1) & BIT(2)) |
  350. ((xor_ecc_2b >> 2) & BIT(3)) |
  351. ((xor_ecc_2b >> 3) & BIT(4)) |
  352. ((xor_ecc_3b << 4) & BIT(5)) |
  353. ((xor_ecc_3b << 3) & BIT(6)) |
  354. ((xor_ecc_3b << 2) & BIT(7)) |
  355. ((xor_ecc_3b << 1) & BIT(8));
  356. /* Correct bit error in the data */
  357. buffer[byte_address] =
  358. buffer[byte_address] ^ BIT(bit_address);
  359. VERBOSE("Hamming: 1 ECC error corrected\n");
  360. return 0;
  361. }
  362. /* Non Correctable ERROR */
  363. ERROR("%s: Uncorrectable ECC Errors\n", __func__);
  364. return -1;
  365. }
  366. /* ECC ERROR */
  367. ERROR("%s: Hamming correction error\n", __func__);
  368. return -1;
  369. }
  370. static int stm32_fmc2_ham_calculate(uint8_t *buffer, uint8_t *ecc)
  371. {
  372. uint32_t heccr;
  373. uint64_t timeout = timeout_init_us(TIMEOUT_US_10_MS);
  374. while ((mmio_read_32(fmc2_base() + FMC2_SR) & FMC2_SR_NWRF) == 0U) {
  375. if (timeout_elapsed(timeout)) {
  376. return -ETIMEDOUT;
  377. }
  378. }
  379. heccr = mmio_read_32(fmc2_base() + FMC2_HECCR);
  380. ecc[0] = heccr;
  381. ecc[1] = heccr >> 8;
  382. ecc[2] = heccr >> 16;
  383. /* Disable ECC */
  384. stm32_fmc2_set_ecc(false);
  385. return 0;
  386. }
  387. static int stm32_fmc2_bch_correct(uint8_t *buffer, unsigned int eccsize)
  388. {
  389. uint32_t bchdsr0, bchdsr1, bchdsr2, bchdsr3, bchdsr4;
  390. uint16_t pos[8];
  391. int i, den;
  392. uint64_t timeout = timeout_init_us(TIMEOUT_US_10_MS);
  393. while ((mmio_read_32(fmc2_base() + FMC2_BCHISR) &
  394. FMC2_BCHISR_DERF) == 0U) {
  395. if (timeout_elapsed(timeout)) {
  396. return -ETIMEDOUT;
  397. }
  398. }
  399. bchdsr0 = mmio_read_32(fmc2_base() + FMC2_BCHDSR0);
  400. bchdsr1 = mmio_read_32(fmc2_base() + FMC2_BCHDSR1);
  401. bchdsr2 = mmio_read_32(fmc2_base() + FMC2_BCHDSR2);
  402. bchdsr3 = mmio_read_32(fmc2_base() + FMC2_BCHDSR3);
  403. bchdsr4 = mmio_read_32(fmc2_base() + FMC2_BCHDSR4);
  404. /* Disable ECC */
  405. stm32_fmc2_set_ecc(false);
  406. /* No error found */
  407. if ((bchdsr0 & FMC2_BCHDSR0_DEF) == 0U) {
  408. return 0;
  409. }
  410. /* Too many errors detected */
  411. if ((bchdsr0 & FMC2_BCHDSR0_DUE) != 0U) {
  412. return -EBADMSG;
  413. }
  414. pos[0] = bchdsr1 & FMC2_BCHDSR1_EBP1_MASK;
  415. pos[1] = (bchdsr1 & FMC2_BCHDSR1_EBP2_MASK) >> FMC2_BCHDSR1_EBP2_SHIFT;
  416. pos[2] = bchdsr2 & FMC2_BCHDSR2_EBP3_MASK;
  417. pos[3] = (bchdsr2 & FMC2_BCHDSR2_EBP4_MASK) >> FMC2_BCHDSR2_EBP4_SHIFT;
  418. pos[4] = bchdsr3 & FMC2_BCHDSR3_EBP5_MASK;
  419. pos[5] = (bchdsr3 & FMC2_BCHDSR3_EBP6_MASK) >> FMC2_BCHDSR3_EBP6_SHIFT;
  420. pos[6] = bchdsr4 & FMC2_BCHDSR4_EBP7_MASK;
  421. pos[7] = (bchdsr4 & FMC2_BCHDSR4_EBP8_MASK) >> FMC2_BCHDSR4_EBP8_SHIFT;
  422. den = (bchdsr0 & FMC2_BCHDSR0_DEN_MASK) >> FMC2_BCHDSR0_DEN_SHIFT;
  423. for (i = 0; i < den; i++) {
  424. if (pos[i] < (eccsize * 8U)) {
  425. uint8_t bitmask = BIT(pos[i] % 8U);
  426. uint32_t offset = pos[i] / 8U;
  427. *(buffer + offset) ^= bitmask;
  428. }
  429. }
  430. return 0;
  431. }
  432. static void stm32_fmc2_hwctl(struct nand_device *nand)
  433. {
  434. stm32_fmc2_set_ecc(false);
  435. if (nand->ecc.max_bit_corr != FMC2_ECC_HAM) {
  436. mmio_clrbits_32(fmc2_base() + FMC2_PCR, FMC2_PCR_WEN);
  437. mmio_write_32(fmc2_base() + FMC2_BCHICR, FMC2_BCHICR_CLEAR_IRQ);
  438. }
  439. stm32_fmc2_set_ecc(true);
  440. }
  441. static int stm32_fmc2_read_page(struct nand_device *nand,
  442. unsigned int page, uintptr_t buffer)
  443. {
  444. unsigned int eccsize = nand->ecc.size;
  445. unsigned int eccbytes = nand->ecc.bytes;
  446. unsigned int eccsteps = nand->page_size / eccsize;
  447. uint8_t ecc_corr[FMC2_MAX_ECC_BYTES];
  448. uint8_t ecc_cal[FMC2_MAX_ECC_BYTES] = {0U};
  449. uint8_t *p;
  450. unsigned int i;
  451. unsigned int s;
  452. int ret;
  453. VERBOSE(">%s page %u buffer %lx\n", __func__, page, buffer);
  454. ret = nand_read_page_cmd(page, 0U, 0U, 0U);
  455. if (ret != 0) {
  456. return ret;
  457. }
  458. for (s = 0U, i = nand->page_size + FMC2_BBM_LEN, p = (uint8_t *)buffer;
  459. s < eccsteps;
  460. s++, i += eccbytes, p += eccsize) {
  461. stm32_fmc2_hwctl(nand);
  462. /* Read the NAND page sector (512 bytes) */
  463. ret = nand_change_read_column_cmd(s * eccsize, (uintptr_t)p,
  464. eccsize);
  465. if (ret != 0) {
  466. return ret;
  467. }
  468. if (nand->ecc.max_bit_corr == FMC2_ECC_HAM) {
  469. ret = stm32_fmc2_ham_calculate(p, ecc_cal);
  470. if (ret != 0) {
  471. return ret;
  472. }
  473. }
  474. /* Read the corresponding ECC bytes */
  475. ret = nand_change_read_column_cmd(i, (uintptr_t)ecc_corr,
  476. eccbytes);
  477. if (ret != 0) {
  478. return ret;
  479. }
  480. /* Correct the data */
  481. if (nand->ecc.max_bit_corr == FMC2_ECC_HAM) {
  482. ret = stm32_fmc2_ham_correct(p, ecc_corr, ecc_cal);
  483. } else {
  484. ret = stm32_fmc2_bch_correct(p, eccsize);
  485. }
  486. if (ret != 0) {
  487. return ret;
  488. }
  489. }
  490. return 0;
  491. }
  492. static void stm32_fmc2_read_data(struct nand_device *nand,
  493. uint8_t *buff, unsigned int length,
  494. bool use_bus8)
  495. {
  496. uintptr_t data_base = stm32_fmc2.cs[stm32_fmc2.cs_sel].data_base;
  497. if (use_bus8 && (nand->buswidth == NAND_BUS_WIDTH_16)) {
  498. stm32_fmc2_set_buswidth_16(false);
  499. }
  500. if ((((uintptr_t)buff & BIT(0)) != 0U) && (length != 0U)) {
  501. *buff = mmio_read_8(data_base);
  502. buff += sizeof(uint8_t);
  503. length -= sizeof(uint8_t);
  504. }
  505. if ((((uintptr_t)buff & GENMASK_32(1, 0)) != 0U) &&
  506. (length >= sizeof(uint16_t))) {
  507. *(uint16_t *)buff = mmio_read_16(data_base);
  508. buff += sizeof(uint16_t);
  509. length -= sizeof(uint16_t);
  510. }
  511. /* 32bit aligned */
  512. while (length >= sizeof(uint32_t)) {
  513. *(uint32_t *)buff = mmio_read_32(data_base);
  514. buff += sizeof(uint32_t);
  515. length -= sizeof(uint32_t);
  516. }
  517. /* Read remaining bytes */
  518. if (length >= sizeof(uint16_t)) {
  519. *(uint16_t *)buff = mmio_read_16(data_base);
  520. buff += sizeof(uint16_t);
  521. length -= sizeof(uint16_t);
  522. }
  523. if (length != 0U) {
  524. *buff = mmio_read_8(data_base);
  525. }
  526. if (use_bus8 && (nand->buswidth == NAND_BUS_WIDTH_16)) {
  527. /* Reconfigure bus width to 16-bit */
  528. stm32_fmc2_set_buswidth_16(true);
  529. }
  530. }
  531. static void stm32_fmc2_write_data(struct nand_device *nand,
  532. uint8_t *buff, unsigned int length,
  533. bool use_bus8)
  534. {
  535. uintptr_t data_base = stm32_fmc2.cs[stm32_fmc2.cs_sel].data_base;
  536. if (use_bus8 && (nand->buswidth == NAND_BUS_WIDTH_16)) {
  537. /* Reconfigure bus width to 8-bit */
  538. stm32_fmc2_set_buswidth_16(false);
  539. }
  540. if ((((uintptr_t)buff & BIT(0)) != 0U) && (length != 0U)) {
  541. mmio_write_8(data_base, *buff);
  542. buff += sizeof(uint8_t);
  543. length -= sizeof(uint8_t);
  544. }
  545. if ((((uintptr_t)buff & GENMASK_32(1, 0)) != 0U) &&
  546. (length >= sizeof(uint16_t))) {
  547. mmio_write_16(data_base, *(uint16_t *)buff);
  548. buff += sizeof(uint16_t);
  549. length -= sizeof(uint16_t);
  550. }
  551. /* 32bits aligned */
  552. while (length >= sizeof(uint32_t)) {
  553. mmio_write_32(data_base, *(uint32_t *)buff);
  554. buff += sizeof(uint32_t);
  555. length -= sizeof(uint32_t);
  556. }
  557. /* Read remaining bytes */
  558. if (length >= sizeof(uint16_t)) {
  559. mmio_write_16(data_base, *(uint16_t *)buff);
  560. buff += sizeof(uint16_t);
  561. length -= sizeof(uint16_t);
  562. }
  563. if (length != 0U) {
  564. mmio_write_8(data_base, *buff);
  565. }
  566. if (use_bus8 && (nand->buswidth == NAND_BUS_WIDTH_16)) {
  567. /* Reconfigure bus width to 16-bit */
  568. stm32_fmc2_set_buswidth_16(true);
  569. }
  570. }
  571. static void stm32_fmc2_ctrl_init(void)
  572. {
  573. uint32_t pcr = mmio_read_32(fmc2_base() + FMC2_PCR);
  574. uint32_t bcr1 = mmio_read_32(fmc2_base() + FMC2_BCR1);
  575. /* Enable wait feature and NAND flash memory bank */
  576. pcr |= FMC2_PCR_PWAITEN;
  577. pcr |= FMC2_PCR_PBKEN;
  578. /* Set buswidth to 8 bits mode for identification */
  579. pcr &= ~FMC2_PCR_PWID_MASK;
  580. /* ECC logic is disabled */
  581. pcr &= ~FMC2_PCR_ECCEN;
  582. /* Default mode */
  583. pcr &= ~FMC2_PCR_ECCALG;
  584. pcr &= ~FMC2_PCR_BCHECC;
  585. pcr &= ~FMC2_PCR_WEN;
  586. /* Set default ECC sector size */
  587. pcr &= ~FMC2_PCR_ECCSS_MASK;
  588. pcr |= FMC2_PCR_ECCSS(FMC2_PCR_ECCSS_2048);
  589. /* Set default TCLR/TAR timings */
  590. pcr &= ~FMC2_PCR_TCLR_MASK;
  591. pcr |= FMC2_PCR_TCLR(FMC2_PCR_TCLR_DEFAULT);
  592. pcr &= ~FMC2_PCR_TAR_MASK;
  593. pcr |= FMC2_PCR_TAR(FMC2_PCR_TAR_DEFAULT);
  594. /* Enable FMC2 controller */
  595. bcr1 |= FMC2_BCR1_FMC2EN;
  596. mmio_write_32(fmc2_base() + FMC2_BCR1, bcr1);
  597. mmio_write_32(fmc2_base() + FMC2_PCR, pcr);
  598. mmio_write_32(fmc2_base() + FMC2_PMEM, FMC2_PMEM_DEFAULT);
  599. mmio_write_32(fmc2_base() + FMC2_PATT, FMC2_PATT_DEFAULT);
  600. }
  601. static int stm32_fmc2_exec(struct nand_req *req)
  602. {
  603. int ret = 0;
  604. switch (req->type & NAND_REQ_MASK) {
  605. case NAND_REQ_CMD:
  606. VERBOSE("Write CMD %x\n", (uint8_t)req->type);
  607. mmio_write_8(stm32_fmc2.cs[stm32_fmc2.cs_sel].cmd_base,
  608. (uint8_t)req->type);
  609. break;
  610. case NAND_REQ_ADDR:
  611. VERBOSE("Write ADDR %x\n", *(req->addr));
  612. mmio_write_8(stm32_fmc2.cs[stm32_fmc2.cs_sel].addr_base,
  613. *(req->addr));
  614. break;
  615. case NAND_REQ_DATAIN:
  616. VERBOSE("Read data\n");
  617. stm32_fmc2_read_data(req->nand, req->addr, req->length,
  618. ((req->type & NAND_REQ_BUS_WIDTH_8) !=
  619. 0U));
  620. break;
  621. case NAND_REQ_DATAOUT:
  622. VERBOSE("Write data\n");
  623. stm32_fmc2_write_data(req->nand, req->addr, req->length,
  624. ((req->type & NAND_REQ_BUS_WIDTH_8) !=
  625. 0U));
  626. break;
  627. case NAND_REQ_WAIT:
  628. VERBOSE("WAIT Ready\n");
  629. ret = nand_wait_ready(req->delay_ms);
  630. break;
  631. default:
  632. ret = -EINVAL;
  633. break;
  634. };
  635. return ret;
  636. }
  637. static void stm32_fmc2_setup(struct nand_device *nand)
  638. {
  639. uint32_t pcr = mmio_read_32(fmc2_base() + FMC2_PCR);
  640. /* Set buswidth */
  641. pcr &= ~FMC2_PCR_PWID_MASK;
  642. if (nand->buswidth == NAND_BUS_WIDTH_16) {
  643. pcr |= FMC2_PCR_PWID(FMC2_PCR_PWID_16);
  644. }
  645. if (nand->ecc.mode == NAND_ECC_HW) {
  646. nand->mtd_read_page = stm32_fmc2_read_page;
  647. pcr &= ~FMC2_PCR_ECCALG;
  648. pcr &= ~FMC2_PCR_BCHECC;
  649. pcr &= ~FMC2_PCR_ECCSS_MASK;
  650. pcr |= FMC2_PCR_ECCSS(FMC2_PCR_ECCSS_512);
  651. switch (nand->ecc.max_bit_corr) {
  652. case FMC2_ECC_HAM:
  653. nand->ecc.bytes = 3;
  654. break;
  655. case FMC2_ECC_BCH8:
  656. pcr |= FMC2_PCR_ECCALG;
  657. pcr |= FMC2_PCR_BCHECC;
  658. nand->ecc.bytes = 13;
  659. break;
  660. default:
  661. /* Use FMC2 ECC BCH4 */
  662. pcr |= FMC2_PCR_ECCALG;
  663. nand->ecc.bytes = 7;
  664. break;
  665. }
  666. if ((nand->buswidth & NAND_BUS_WIDTH_16) != 0) {
  667. nand->ecc.bytes++;
  668. }
  669. }
  670. mmio_write_32(stm32_fmc2.reg_base + FMC2_PCR, pcr);
  671. }
  672. static const struct nand_ctrl_ops ctrl_ops = {
  673. .setup = stm32_fmc2_setup,
  674. .exec = stm32_fmc2_exec
  675. };
  676. int stm32_fmc2_init(void)
  677. {
  678. int fmc_ebi_node;
  679. int fmc_nfc_node;
  680. int fmc_flash_node = 0;
  681. int nchips = 0;
  682. unsigned int i;
  683. void *fdt = NULL;
  684. const fdt32_t *cuint;
  685. struct dt_node_info info;
  686. uintptr_t bank_address[MAX_BANK] = { 0, 0, 0, 0, 0 };
  687. uint8_t bank_assigned = 0;
  688. uint8_t bank;
  689. int ret;
  690. if (fdt_get_address(&fdt) == 0) {
  691. return -FDT_ERR_NOTFOUND;
  692. }
  693. fmc_ebi_node = dt_get_node(&info, -1, DT_FMC2_EBI_COMPAT);
  694. if (fmc_ebi_node < 0) {
  695. return fmc_ebi_node;
  696. }
  697. if (info.status == DT_DISABLED) {
  698. return -FDT_ERR_NOTFOUND;
  699. }
  700. stm32_fmc2.reg_base = info.base;
  701. if ((info.clock < 0) || (info.reset < 0)) {
  702. return -FDT_ERR_BADVALUE;
  703. }
  704. stm32_fmc2.clock_id = (unsigned long)info.clock;
  705. stm32_fmc2.reset_id = (unsigned int)info.reset;
  706. cuint = fdt_getprop(fdt, fmc_ebi_node, "ranges", NULL);
  707. if (cuint == NULL) {
  708. return -FDT_ERR_BADVALUE;
  709. }
  710. for (i = 0U; i < MAX_BANK; i++) {
  711. bank = fdt32_to_cpu(*cuint);
  712. if ((bank >= MAX_BANK) || ((bank_assigned & BIT(bank)) != 0U)) {
  713. return -FDT_ERR_BADVALUE;
  714. }
  715. bank_assigned |= BIT(bank);
  716. bank_address[bank] = fdt32_to_cpu(*(cuint + 2));
  717. cuint += 4;
  718. }
  719. /* Pinctrl initialization */
  720. if (dt_set_pinctrl_config(fmc_ebi_node) != 0) {
  721. return -FDT_ERR_BADVALUE;
  722. }
  723. /* Parse NFC controller node */
  724. fmc_nfc_node = fdt_node_offset_by_compatible(fdt, fmc_ebi_node,
  725. DT_FMC2_NFC_COMPAT);
  726. if (fmc_nfc_node < 0) {
  727. return fmc_nfc_node;
  728. }
  729. if (fdt_get_status(fmc_nfc_node) == DT_DISABLED) {
  730. return -FDT_ERR_NOTFOUND;
  731. }
  732. cuint = fdt_getprop(fdt, fmc_nfc_node, "reg", NULL);
  733. if (cuint == NULL) {
  734. return -FDT_ERR_BADVALUE;
  735. }
  736. for (i = 0U; i < MAX_CS; i++) {
  737. bank = fdt32_to_cpu(*cuint);
  738. if (bank >= MAX_BANK) {
  739. return -FDT_ERR_BADVALUE;
  740. }
  741. stm32_fmc2.cs[i].data_base = fdt32_to_cpu(*(cuint + 1)) +
  742. bank_address[bank];
  743. bank = fdt32_to_cpu(*(cuint + 3));
  744. if (bank >= MAX_BANK) {
  745. return -FDT_ERR_BADVALUE;
  746. }
  747. stm32_fmc2.cs[i].cmd_base = fdt32_to_cpu(*(cuint + 4)) +
  748. bank_address[bank];
  749. bank = fdt32_to_cpu(*(cuint + 6));
  750. if (bank >= MAX_BANK) {
  751. return -FDT_ERR_BADVALUE;
  752. }
  753. stm32_fmc2.cs[i].addr_base = fdt32_to_cpu(*(cuint + 7)) +
  754. bank_address[bank];
  755. cuint += 9;
  756. }
  757. /* Parse flash nodes */
  758. fdt_for_each_subnode(fmc_flash_node, fdt, fmc_nfc_node) {
  759. nchips++;
  760. }
  761. if (nchips != 1) {
  762. WARN("Only one SLC NAND device supported\n");
  763. return -FDT_ERR_BADVALUE;
  764. }
  765. fdt_for_each_subnode(fmc_flash_node, fdt, fmc_nfc_node) {
  766. /* Get chip select */
  767. cuint = fdt_getprop(fdt, fmc_flash_node, "reg", NULL);
  768. if (cuint == NULL) {
  769. WARN("Chip select not well defined\n");
  770. return -FDT_ERR_BADVALUE;
  771. }
  772. stm32_fmc2.cs_sel = fdt32_to_cpu(*cuint);
  773. if (stm32_fmc2.cs_sel >= MAX_CS) {
  774. return -FDT_ERR_BADVALUE;
  775. }
  776. VERBOSE("NAND CS %i\n", stm32_fmc2.cs_sel);
  777. }
  778. /* Enable Clock */
  779. clk_enable(stm32_fmc2.clock_id);
  780. /* Reset IP */
  781. ret = stm32mp_reset_assert(stm32_fmc2.reset_id, TIMEOUT_US_1_MS);
  782. if (ret != 0) {
  783. panic();
  784. }
  785. ret = stm32mp_reset_deassert(stm32_fmc2.reset_id, TIMEOUT_US_1_MS);
  786. if (ret != 0) {
  787. panic();
  788. }
  789. /* Setup default IP registers */
  790. stm32_fmc2_ctrl_init();
  791. /* Setup default timings */
  792. stm32_fmc2_nand_setup_timing();
  793. /* Init NAND RAW framework */
  794. nand_raw_ctrl_init(&ctrl_ops);
  795. return 0;
  796. }