spi-mtk-snfi.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Driver for the SPI-NAND mode of Mediatek NAND Flash Interface
  4. //
  5. // Copyright (c) 2022 Chuanhong Guo <gch981213@gmail.com>
  6. //
  7. // This driver is based on the SPI-NAND mtd driver from Mediatek SDK:
  8. //
  9. // Copyright (C) 2020 MediaTek Inc.
  10. // Author: Weijie Gao <weijie.gao@mediatek.com>
  11. //
  12. // This controller organize the page data as several interleaved sectors
  13. // like the following: (sizeof(FDM + ECC) = snf->nfi_cfg.spare_size)
  14. // +---------+------+------+---------+------+------+-----+
  15. // | Sector1 | FDM1 | ECC1 | Sector2 | FDM2 | ECC2 | ... |
  16. // +---------+------+------+---------+------+------+-----+
  17. // With auto-format turned on, DMA only returns this part:
  18. // +---------+---------+-----+
  19. // | Sector1 | Sector2 | ... |
  20. // +---------+---------+-----+
  21. // The FDM data will be filled to the registers, and ECC parity data isn't
  22. // accessible.
  23. // With auto-format off, all ((Sector+FDM+ECC)*nsectors) will be read over DMA
  24. // in it's original order shown in the first table. ECC can't be turned on when
  25. // auto-format is off.
  26. //
  27. // However, Linux SPI-NAND driver expects the data returned as:
  28. // +------+-----+
  29. // | Page | OOB |
  30. // +------+-----+
  31. // where the page data is continuously stored instead of interleaved.
  32. // So we assume all instructions matching the page_op template between ECC
  33. // prepare_io_req and finish_io_req are for page cache r/w.
  34. // Here's how this spi-mem driver operates when reading:
  35. // 1. Always set snf->autofmt = true in prepare_io_req (even when ECC is off).
  36. // 2. Perform page ops and let the controller fill the DMA bounce buffer with
  37. // de-interleaved sector data and set FDM registers.
  38. // 3. Return the data as:
  39. // +---------+---------+-----+------+------+-----+
  40. // | Sector1 | Sector2 | ... | FDM1 | FDM2 | ... |
  41. // +---------+---------+-----+------+------+-----+
  42. // 4. For other matching spi_mem ops outside a prepare/finish_io_req pair,
  43. // read the data with auto-format off into the bounce buffer and copy
  44. // needed data to the buffer specified in the request.
  45. //
  46. // Write requests operates in a similar manner.
  47. // As a limitation of this strategy, we won't be able to access any ECC parity
  48. // data at all in Linux.
  49. //
  50. // Here's the bad block mark situation on MTK chips:
  51. // In older chips like mt7622, MTK uses the first FDM byte in the first sector
  52. // as the bad block mark. After de-interleaving, this byte appears at [pagesize]
  53. // in the returned data, which is the BBM position expected by kernel. However,
  54. // the conventional bad block mark is the first byte of the OOB, which is part
  55. // of the last sector data in the interleaved layout. Instead of fixing their
  56. // hardware, MTK decided to address this inconsistency in software. On these
  57. // later chips, the BootROM expects the following:
  58. // 1. The [pagesize] byte on a nand page is used as BBM, which will appear at
  59. // (page_size - (nsectors - 1) * spare_size) in the DMA buffer.
  60. // 2. The original byte stored at that position in the DMA buffer will be stored
  61. // as the first byte of the FDM section in the last sector.
  62. // We can't disagree with the BootROM, so after de-interleaving, we need to
  63. // perform the following swaps in read:
  64. // 1. Store the BBM at [page_size - (nsectors - 1) * spare_size] to [page_size],
  65. // which is the expected BBM position by kernel.
  66. // 2. Store the page data byte at [pagesize + (nsectors-1) * fdm] back to
  67. // [page_size - (nsectors - 1) * spare_size]
  68. // Similarly, when writing, we need to perform swaps in the other direction.
  69. #include <linux/kernel.h>
  70. #include <linux/module.h>
  71. #include <linux/init.h>
  72. #include <linux/device.h>
  73. #include <linux/mutex.h>
  74. #include <linux/clk.h>
  75. #include <linux/interrupt.h>
  76. #include <linux/dma-mapping.h>
  77. #include <linux/iopoll.h>
  78. #include <linux/of.h>
  79. #include <linux/platform_device.h>
  80. #include <linux/mtd/nand-ecc-mtk.h>
  81. #include <linux/spi/spi.h>
  82. #include <linux/spi/spi-mem.h>
  83. #include <linux/mtd/nand.h>
  84. // NFI registers
  85. #define NFI_CNFG 0x000
  86. #define CNFG_OP_MODE_S 12
  87. #define CNFG_OP_MODE_CUST 6
  88. #define CNFG_OP_MODE_PROGRAM 3
  89. #define CNFG_AUTO_FMT_EN BIT(9)
  90. #define CNFG_HW_ECC_EN BIT(8)
  91. #define CNFG_DMA_BURST_EN BIT(2)
  92. #define CNFG_READ_MODE BIT(1)
  93. #define CNFG_DMA_MODE BIT(0)
  94. #define NFI_PAGEFMT 0x0004
  95. #define NFI_SPARE_SIZE_LS_S 16
  96. #define NFI_FDM_ECC_NUM_S 12
  97. #define NFI_FDM_NUM_S 8
  98. #define NFI_SPARE_SIZE_S 4
  99. #define NFI_SEC_SEL_512 BIT(2)
  100. #define NFI_PAGE_SIZE_S 0
  101. #define NFI_PAGE_SIZE_512_2K 0
  102. #define NFI_PAGE_SIZE_2K_4K 1
  103. #define NFI_PAGE_SIZE_4K_8K 2
  104. #define NFI_PAGE_SIZE_8K_16K 3
  105. #define NFI_CON 0x008
  106. #define CON_SEC_NUM_S 12
  107. #define CON_BWR BIT(9)
  108. #define CON_BRD BIT(8)
  109. #define CON_NFI_RST BIT(1)
  110. #define CON_FIFO_FLUSH BIT(0)
  111. #define NFI_INTR_EN 0x010
  112. #define NFI_INTR_STA 0x014
  113. #define NFI_IRQ_INTR_EN BIT(31)
  114. #define NFI_IRQ_CUS_READ BIT(8)
  115. #define NFI_IRQ_CUS_PG BIT(7)
  116. #define NFI_CMD 0x020
  117. #define NFI_CMD_DUMMY_READ 0x00
  118. #define NFI_CMD_DUMMY_WRITE 0x80
  119. #define NFI_STRDATA 0x040
  120. #define STR_DATA BIT(0)
  121. #define NFI_STA 0x060
  122. #define NFI_NAND_FSM_7622 GENMASK(28, 24)
  123. #define NFI_NAND_FSM_7986 GENMASK(29, 23)
  124. #define NFI_FSM GENMASK(19, 16)
  125. #define READ_EMPTY BIT(12)
  126. #define NFI_FIFOSTA 0x064
  127. #define FIFO_WR_REMAIN_S 8
  128. #define FIFO_RD_REMAIN_S 0
  129. #define NFI_ADDRCNTR 0x070
  130. #define SEC_CNTR GENMASK(16, 12)
  131. #define SEC_CNTR_S 12
  132. #define NFI_SEC_CNTR(val) (((val)&SEC_CNTR) >> SEC_CNTR_S)
  133. #define NFI_STRADDR 0x080
  134. #define NFI_BYTELEN 0x084
  135. #define BUS_SEC_CNTR(val) (((val)&SEC_CNTR) >> SEC_CNTR_S)
  136. #define NFI_FDM0L 0x0a0
  137. #define NFI_FDM0M 0x0a4
  138. #define NFI_FDML(n) (NFI_FDM0L + (n)*8)
  139. #define NFI_FDMM(n) (NFI_FDM0M + (n)*8)
  140. #define NFI_DEBUG_CON1 0x220
  141. #define WBUF_EN BIT(2)
  142. #define NFI_MASTERSTA 0x224
  143. #define MAS_ADDR GENMASK(11, 9)
  144. #define MAS_RD GENMASK(8, 6)
  145. #define MAS_WR GENMASK(5, 3)
  146. #define MAS_RDDLY GENMASK(2, 0)
  147. #define NFI_MASTERSTA_MASK_7622 (MAS_ADDR | MAS_RD | MAS_WR | MAS_RDDLY)
  148. #define NFI_MASTERSTA_MASK_7986 3
  149. // SNFI registers
  150. #define SNF_MAC_CTL 0x500
  151. #define MAC_XIO_SEL BIT(4)
  152. #define SF_MAC_EN BIT(3)
  153. #define SF_TRIG BIT(2)
  154. #define WIP_READY BIT(1)
  155. #define WIP BIT(0)
  156. #define SNF_MAC_OUTL 0x504
  157. #define SNF_MAC_INL 0x508
  158. #define SNF_RD_CTL2 0x510
  159. #define DATA_READ_DUMMY_S 8
  160. #define DATA_READ_MAX_DUMMY 0xf
  161. #define DATA_READ_CMD_S 0
  162. #define SNF_RD_CTL3 0x514
  163. #define SNF_PG_CTL1 0x524
  164. #define PG_LOAD_CMD_S 8
  165. #define SNF_PG_CTL2 0x528
  166. #define SNF_MISC_CTL 0x538
  167. #define SW_RST BIT(28)
  168. #define FIFO_RD_LTC_S 25
  169. #define PG_LOAD_X4_EN BIT(20)
  170. #define DATA_READ_MODE_S 16
  171. #define DATA_READ_MODE GENMASK(18, 16)
  172. #define DATA_READ_MODE_X1 0
  173. #define DATA_READ_MODE_X2 1
  174. #define DATA_READ_MODE_X4 2
  175. #define DATA_READ_MODE_DUAL 5
  176. #define DATA_READ_MODE_QUAD 6
  177. #define DATA_READ_LATCH_LAT GENMASK(9, 8)
  178. #define DATA_READ_LATCH_LAT_S 8
  179. #define PG_LOAD_CUSTOM_EN BIT(7)
  180. #define DATARD_CUSTOM_EN BIT(6)
  181. #define CS_DESELECT_CYC_S 0
  182. #define SNF_MISC_CTL2 0x53c
  183. #define PROGRAM_LOAD_BYTE_NUM_S 16
  184. #define READ_DATA_BYTE_NUM_S 11
  185. #define SNF_DLY_CTL3 0x548
  186. #define SFCK_SAM_DLY_S 0
  187. #define SFCK_SAM_DLY GENMASK(5, 0)
  188. #define SFCK_SAM_DLY_TOTAL 9
  189. #define SFCK_SAM_DLY_RANGE 47
  190. #define SNF_STA_CTL1 0x550
  191. #define CUS_PG_DONE BIT(28)
  192. #define CUS_READ_DONE BIT(27)
  193. #define SPI_STATE_S 0
  194. #define SPI_STATE GENMASK(3, 0)
  195. #define SNF_CFG 0x55c
  196. #define SPI_MODE BIT(0)
  197. #define SNF_GPRAM 0x800
  198. #define SNF_GPRAM_SIZE 0xa0
  199. #define SNFI_POLL_INTERVAL 1000000
  200. static const u8 mt7622_spare_sizes[] = { 16, 26, 27, 28 };
  201. static const u8 mt7986_spare_sizes[] = {
  202. 16, 26, 27, 28, 32, 36, 40, 44, 48, 49, 50, 51, 52, 62, 61, 63, 64, 67,
  203. 74
  204. };
  205. struct mtk_snand_caps {
  206. u16 sector_size;
  207. u16 max_sectors;
  208. u16 fdm_size;
  209. u16 fdm_ecc_size;
  210. u16 fifo_size;
  211. bool bbm_swap;
  212. bool empty_page_check;
  213. u32 mastersta_mask;
  214. u32 nandfsm_mask;
  215. const u8 *spare_sizes;
  216. u32 num_spare_size;
  217. };
  218. static const struct mtk_snand_caps mt7622_snand_caps = {
  219. .sector_size = 512,
  220. .max_sectors = 8,
  221. .fdm_size = 8,
  222. .fdm_ecc_size = 1,
  223. .fifo_size = 32,
  224. .bbm_swap = false,
  225. .empty_page_check = false,
  226. .mastersta_mask = NFI_MASTERSTA_MASK_7622,
  227. .nandfsm_mask = NFI_NAND_FSM_7622,
  228. .spare_sizes = mt7622_spare_sizes,
  229. .num_spare_size = ARRAY_SIZE(mt7622_spare_sizes)
  230. };
  231. static const struct mtk_snand_caps mt7629_snand_caps = {
  232. .sector_size = 512,
  233. .max_sectors = 8,
  234. .fdm_size = 8,
  235. .fdm_ecc_size = 1,
  236. .fifo_size = 32,
  237. .bbm_swap = true,
  238. .empty_page_check = false,
  239. .mastersta_mask = NFI_MASTERSTA_MASK_7622,
  240. .nandfsm_mask = NFI_NAND_FSM_7622,
  241. .spare_sizes = mt7622_spare_sizes,
  242. .num_spare_size = ARRAY_SIZE(mt7622_spare_sizes)
  243. };
  244. static const struct mtk_snand_caps mt7986_snand_caps = {
  245. .sector_size = 1024,
  246. .max_sectors = 8,
  247. .fdm_size = 8,
  248. .fdm_ecc_size = 1,
  249. .fifo_size = 64,
  250. .bbm_swap = true,
  251. .empty_page_check = true,
  252. .mastersta_mask = NFI_MASTERSTA_MASK_7986,
  253. .nandfsm_mask = NFI_NAND_FSM_7986,
  254. .spare_sizes = mt7986_spare_sizes,
  255. .num_spare_size = ARRAY_SIZE(mt7986_spare_sizes)
  256. };
  257. struct mtk_snand_conf {
  258. size_t page_size;
  259. size_t oob_size;
  260. u8 nsectors;
  261. u8 spare_size;
  262. };
  263. struct mtk_snand {
  264. struct spi_controller *ctlr;
  265. struct device *dev;
  266. struct clk *nfi_clk;
  267. struct clk *pad_clk;
  268. struct clk *nfi_hclk;
  269. void __iomem *nfi_base;
  270. int irq;
  271. struct completion op_done;
  272. const struct mtk_snand_caps *caps;
  273. struct mtk_ecc_config *ecc_cfg;
  274. struct mtk_ecc *ecc;
  275. struct mtk_snand_conf nfi_cfg;
  276. struct mtk_ecc_stats ecc_stats;
  277. struct nand_ecc_engine ecc_eng;
  278. bool autofmt;
  279. u8 *buf;
  280. size_t buf_len;
  281. };
  282. static struct mtk_snand *nand_to_mtk_snand(struct nand_device *nand)
  283. {
  284. struct nand_ecc_engine *eng = nand->ecc.engine;
  285. return container_of(eng, struct mtk_snand, ecc_eng);
  286. }
  287. static inline int snand_prepare_bouncebuf(struct mtk_snand *snf, size_t size)
  288. {
  289. if (snf->buf_len >= size)
  290. return 0;
  291. kfree(snf->buf);
  292. snf->buf = kmalloc(size, GFP_KERNEL);
  293. if (!snf->buf)
  294. return -ENOMEM;
  295. snf->buf_len = size;
  296. memset(snf->buf, 0xff, snf->buf_len);
  297. return 0;
  298. }
  299. static inline u32 nfi_read32(struct mtk_snand *snf, u32 reg)
  300. {
  301. return readl(snf->nfi_base + reg);
  302. }
  303. static inline void nfi_write32(struct mtk_snand *snf, u32 reg, u32 val)
  304. {
  305. writel(val, snf->nfi_base + reg);
  306. }
  307. static inline void nfi_write16(struct mtk_snand *snf, u32 reg, u16 val)
  308. {
  309. writew(val, snf->nfi_base + reg);
  310. }
  311. static inline void nfi_rmw32(struct mtk_snand *snf, u32 reg, u32 clr, u32 set)
  312. {
  313. u32 val;
  314. val = readl(snf->nfi_base + reg);
  315. val &= ~clr;
  316. val |= set;
  317. writel(val, snf->nfi_base + reg);
  318. }
  319. static void nfi_read_data(struct mtk_snand *snf, u32 reg, u8 *data, u32 len)
  320. {
  321. u32 i, val = 0, es = sizeof(u32);
  322. for (i = reg; i < reg + len; i++) {
  323. if (i == reg || i % es == 0)
  324. val = nfi_read32(snf, i & ~(es - 1));
  325. *data++ = (u8)(val >> (8 * (i % es)));
  326. }
  327. }
  328. static int mtk_nfi_reset(struct mtk_snand *snf)
  329. {
  330. u32 val, fifo_mask;
  331. int ret;
  332. nfi_write32(snf, NFI_CON, CON_FIFO_FLUSH | CON_NFI_RST);
  333. ret = readw_poll_timeout(snf->nfi_base + NFI_MASTERSTA, val,
  334. !(val & snf->caps->mastersta_mask), 0,
  335. SNFI_POLL_INTERVAL);
  336. if (ret) {
  337. dev_err(snf->dev, "NFI master is still busy after reset\n");
  338. return ret;
  339. }
  340. ret = readl_poll_timeout(snf->nfi_base + NFI_STA, val,
  341. !(val & (NFI_FSM | snf->caps->nandfsm_mask)), 0,
  342. SNFI_POLL_INTERVAL);
  343. if (ret) {
  344. dev_err(snf->dev, "Failed to reset NFI\n");
  345. return ret;
  346. }
  347. fifo_mask = ((snf->caps->fifo_size - 1) << FIFO_RD_REMAIN_S) |
  348. ((snf->caps->fifo_size - 1) << FIFO_WR_REMAIN_S);
  349. ret = readw_poll_timeout(snf->nfi_base + NFI_FIFOSTA, val,
  350. !(val & fifo_mask), 0, SNFI_POLL_INTERVAL);
  351. if (ret) {
  352. dev_err(snf->dev, "NFI FIFOs are not empty\n");
  353. return ret;
  354. }
  355. return 0;
  356. }
  357. static int mtk_snand_mac_reset(struct mtk_snand *snf)
  358. {
  359. int ret;
  360. u32 val;
  361. nfi_rmw32(snf, SNF_MISC_CTL, 0, SW_RST);
  362. ret = readl_poll_timeout(snf->nfi_base + SNF_STA_CTL1, val,
  363. !(val & SPI_STATE), 0, SNFI_POLL_INTERVAL);
  364. if (ret)
  365. dev_err(snf->dev, "Failed to reset SNFI MAC\n");
  366. nfi_write32(snf, SNF_MISC_CTL,
  367. (2 << FIFO_RD_LTC_S) | (10 << CS_DESELECT_CYC_S));
  368. return ret;
  369. }
  370. static int mtk_snand_mac_trigger(struct mtk_snand *snf, u32 outlen, u32 inlen)
  371. {
  372. int ret;
  373. u32 val;
  374. nfi_write32(snf, SNF_MAC_CTL, SF_MAC_EN);
  375. nfi_write32(snf, SNF_MAC_OUTL, outlen);
  376. nfi_write32(snf, SNF_MAC_INL, inlen);
  377. nfi_write32(snf, SNF_MAC_CTL, SF_MAC_EN | SF_TRIG);
  378. ret = readl_poll_timeout(snf->nfi_base + SNF_MAC_CTL, val,
  379. val & WIP_READY, 0, SNFI_POLL_INTERVAL);
  380. if (ret) {
  381. dev_err(snf->dev, "Timed out waiting for WIP_READY\n");
  382. goto cleanup;
  383. }
  384. ret = readl_poll_timeout(snf->nfi_base + SNF_MAC_CTL, val, !(val & WIP),
  385. 0, SNFI_POLL_INTERVAL);
  386. if (ret)
  387. dev_err(snf->dev, "Timed out waiting for WIP cleared\n");
  388. cleanup:
  389. nfi_write32(snf, SNF_MAC_CTL, 0);
  390. return ret;
  391. }
  392. static int mtk_snand_mac_io(struct mtk_snand *snf, const struct spi_mem_op *op)
  393. {
  394. u32 rx_len = 0;
  395. u32 reg_offs = 0;
  396. u32 val = 0;
  397. const u8 *tx_buf = NULL;
  398. u8 *rx_buf = NULL;
  399. int i, ret;
  400. u8 b;
  401. if (op->data.dir == SPI_MEM_DATA_IN) {
  402. rx_len = op->data.nbytes;
  403. rx_buf = op->data.buf.in;
  404. } else {
  405. tx_buf = op->data.buf.out;
  406. }
  407. mtk_snand_mac_reset(snf);
  408. for (i = 0; i < op->cmd.nbytes; i++, reg_offs++) {
  409. b = (op->cmd.opcode >> ((op->cmd.nbytes - i - 1) * 8)) & 0xff;
  410. val |= b << (8 * (reg_offs % 4));
  411. if (reg_offs % 4 == 3) {
  412. nfi_write32(snf, SNF_GPRAM + reg_offs - 3, val);
  413. val = 0;
  414. }
  415. }
  416. for (i = 0; i < op->addr.nbytes; i++, reg_offs++) {
  417. b = (op->addr.val >> ((op->addr.nbytes - i - 1) * 8)) & 0xff;
  418. val |= b << (8 * (reg_offs % 4));
  419. if (reg_offs % 4 == 3) {
  420. nfi_write32(snf, SNF_GPRAM + reg_offs - 3, val);
  421. val = 0;
  422. }
  423. }
  424. for (i = 0; i < op->dummy.nbytes; i++, reg_offs++) {
  425. if (reg_offs % 4 == 3) {
  426. nfi_write32(snf, SNF_GPRAM + reg_offs - 3, val);
  427. val = 0;
  428. }
  429. }
  430. if (op->data.dir == SPI_MEM_DATA_OUT) {
  431. for (i = 0; i < op->data.nbytes; i++, reg_offs++) {
  432. val |= tx_buf[i] << (8 * (reg_offs % 4));
  433. if (reg_offs % 4 == 3) {
  434. nfi_write32(snf, SNF_GPRAM + reg_offs - 3, val);
  435. val = 0;
  436. }
  437. }
  438. }
  439. if (reg_offs % 4)
  440. nfi_write32(snf, SNF_GPRAM + (reg_offs & ~3), val);
  441. for (i = 0; i < reg_offs; i += 4)
  442. dev_dbg(snf->dev, "%d: %08X", i,
  443. nfi_read32(snf, SNF_GPRAM + i));
  444. dev_dbg(snf->dev, "SNF TX: %u RX: %u", reg_offs, rx_len);
  445. ret = mtk_snand_mac_trigger(snf, reg_offs, rx_len);
  446. if (ret)
  447. return ret;
  448. if (!rx_len)
  449. return 0;
  450. nfi_read_data(snf, SNF_GPRAM + reg_offs, rx_buf, rx_len);
  451. return 0;
  452. }
  453. static int mtk_snand_setup_pagefmt(struct mtk_snand *snf, u32 page_size,
  454. u32 oob_size)
  455. {
  456. int spare_idx = -1;
  457. u32 spare_size, spare_size_shift, pagesize_idx;
  458. u32 sector_size_512;
  459. u8 nsectors;
  460. int i;
  461. // skip if it's already configured as required.
  462. if (snf->nfi_cfg.page_size == page_size &&
  463. snf->nfi_cfg.oob_size == oob_size)
  464. return 0;
  465. nsectors = page_size / snf->caps->sector_size;
  466. if (nsectors > snf->caps->max_sectors) {
  467. dev_err(snf->dev, "too many sectors required.\n");
  468. goto err;
  469. }
  470. if (snf->caps->sector_size == 512) {
  471. sector_size_512 = NFI_SEC_SEL_512;
  472. spare_size_shift = NFI_SPARE_SIZE_S;
  473. } else {
  474. sector_size_512 = 0;
  475. spare_size_shift = NFI_SPARE_SIZE_LS_S;
  476. }
  477. switch (page_size) {
  478. case SZ_512:
  479. pagesize_idx = NFI_PAGE_SIZE_512_2K;
  480. break;
  481. case SZ_2K:
  482. if (snf->caps->sector_size == 512)
  483. pagesize_idx = NFI_PAGE_SIZE_2K_4K;
  484. else
  485. pagesize_idx = NFI_PAGE_SIZE_512_2K;
  486. break;
  487. case SZ_4K:
  488. if (snf->caps->sector_size == 512)
  489. pagesize_idx = NFI_PAGE_SIZE_4K_8K;
  490. else
  491. pagesize_idx = NFI_PAGE_SIZE_2K_4K;
  492. break;
  493. case SZ_8K:
  494. if (snf->caps->sector_size == 512)
  495. pagesize_idx = NFI_PAGE_SIZE_8K_16K;
  496. else
  497. pagesize_idx = NFI_PAGE_SIZE_4K_8K;
  498. break;
  499. case SZ_16K:
  500. pagesize_idx = NFI_PAGE_SIZE_8K_16K;
  501. break;
  502. default:
  503. dev_err(snf->dev, "unsupported page size.\n");
  504. goto err;
  505. }
  506. spare_size = oob_size / nsectors;
  507. // If we're using the 1KB sector size, HW will automatically double the
  508. // spare size. We should only use half of the value in this case.
  509. if (snf->caps->sector_size == 1024)
  510. spare_size /= 2;
  511. for (i = snf->caps->num_spare_size - 1; i >= 0; i--) {
  512. if (snf->caps->spare_sizes[i] <= spare_size) {
  513. spare_size = snf->caps->spare_sizes[i];
  514. if (snf->caps->sector_size == 1024)
  515. spare_size *= 2;
  516. spare_idx = i;
  517. break;
  518. }
  519. }
  520. if (spare_idx < 0) {
  521. dev_err(snf->dev, "unsupported spare size: %u\n", spare_size);
  522. goto err;
  523. }
  524. nfi_write32(snf, NFI_PAGEFMT,
  525. (snf->caps->fdm_ecc_size << NFI_FDM_ECC_NUM_S) |
  526. (snf->caps->fdm_size << NFI_FDM_NUM_S) |
  527. (spare_idx << spare_size_shift) |
  528. (pagesize_idx << NFI_PAGE_SIZE_S) |
  529. sector_size_512);
  530. snf->nfi_cfg.page_size = page_size;
  531. snf->nfi_cfg.oob_size = oob_size;
  532. snf->nfi_cfg.nsectors = nsectors;
  533. snf->nfi_cfg.spare_size = spare_size;
  534. dev_dbg(snf->dev, "page format: (%u + %u) * %u\n",
  535. snf->caps->sector_size, spare_size, nsectors);
  536. return snand_prepare_bouncebuf(snf, page_size + oob_size);
  537. err:
  538. dev_err(snf->dev, "page size %u + %u is not supported\n", page_size,
  539. oob_size);
  540. return -EOPNOTSUPP;
  541. }
  542. static int mtk_snand_ooblayout_ecc(struct mtd_info *mtd, int section,
  543. struct mtd_oob_region *oobecc)
  544. {
  545. // ECC area is not accessible
  546. return -ERANGE;
  547. }
  548. static int mtk_snand_ooblayout_free(struct mtd_info *mtd, int section,
  549. struct mtd_oob_region *oobfree)
  550. {
  551. struct nand_device *nand = mtd_to_nanddev(mtd);
  552. struct mtk_snand *ms = nand_to_mtk_snand(nand);
  553. if (section >= ms->nfi_cfg.nsectors)
  554. return -ERANGE;
  555. oobfree->length = ms->caps->fdm_size - 1;
  556. oobfree->offset = section * ms->caps->fdm_size + 1;
  557. return 0;
  558. }
  559. static const struct mtd_ooblayout_ops mtk_snand_ooblayout = {
  560. .ecc = mtk_snand_ooblayout_ecc,
  561. .free = mtk_snand_ooblayout_free,
  562. };
  563. static int mtk_snand_ecc_init_ctx(struct nand_device *nand)
  564. {
  565. struct mtk_snand *snf = nand_to_mtk_snand(nand);
  566. struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
  567. struct nand_ecc_props *reqs = &nand->ecc.requirements;
  568. struct nand_ecc_props *user = &nand->ecc.user_conf;
  569. struct mtd_info *mtd = nanddev_to_mtd(nand);
  570. int step_size = 0, strength = 0, desired_correction = 0, steps;
  571. bool ecc_user = false;
  572. int ret;
  573. u32 parity_bits, max_ecc_bytes;
  574. struct mtk_ecc_config *ecc_cfg;
  575. ret = mtk_snand_setup_pagefmt(snf, nand->memorg.pagesize,
  576. nand->memorg.oobsize);
  577. if (ret)
  578. return ret;
  579. ecc_cfg = kzalloc(sizeof(*ecc_cfg), GFP_KERNEL);
  580. if (!ecc_cfg)
  581. return -ENOMEM;
  582. nand->ecc.ctx.priv = ecc_cfg;
  583. if (user->step_size && user->strength) {
  584. step_size = user->step_size;
  585. strength = user->strength;
  586. ecc_user = true;
  587. } else if (reqs->step_size && reqs->strength) {
  588. step_size = reqs->step_size;
  589. strength = reqs->strength;
  590. }
  591. if (step_size && strength) {
  592. steps = mtd->writesize / step_size;
  593. desired_correction = steps * strength;
  594. strength = desired_correction / snf->nfi_cfg.nsectors;
  595. }
  596. ecc_cfg->mode = ECC_NFI_MODE;
  597. ecc_cfg->sectors = snf->nfi_cfg.nsectors;
  598. ecc_cfg->len = snf->caps->sector_size + snf->caps->fdm_ecc_size;
  599. // calculate the max possible strength under current page format
  600. parity_bits = mtk_ecc_get_parity_bits(snf->ecc);
  601. max_ecc_bytes = snf->nfi_cfg.spare_size - snf->caps->fdm_size;
  602. ecc_cfg->strength = max_ecc_bytes * 8 / parity_bits;
  603. mtk_ecc_adjust_strength(snf->ecc, &ecc_cfg->strength);
  604. // if there's a user requested strength, find the minimum strength that
  605. // meets the requirement. Otherwise use the maximum strength which is
  606. // expected by BootROM.
  607. if (ecc_user && strength) {
  608. u32 s_next = ecc_cfg->strength - 1;
  609. while (1) {
  610. mtk_ecc_adjust_strength(snf->ecc, &s_next);
  611. if (s_next >= ecc_cfg->strength)
  612. break;
  613. if (s_next < strength)
  614. break;
  615. s_next = ecc_cfg->strength - 1;
  616. }
  617. }
  618. mtd_set_ooblayout(mtd, &mtk_snand_ooblayout);
  619. conf->step_size = snf->caps->sector_size;
  620. conf->strength = ecc_cfg->strength;
  621. if (ecc_cfg->strength < strength)
  622. dev_warn(snf->dev, "unable to fulfill ECC of %u bits.\n",
  623. strength);
  624. dev_info(snf->dev, "ECC strength: %u bits per %u bytes\n",
  625. ecc_cfg->strength, snf->caps->sector_size);
  626. return 0;
  627. }
  628. static void mtk_snand_ecc_cleanup_ctx(struct nand_device *nand)
  629. {
  630. struct mtk_ecc_config *ecc_cfg = nand_to_ecc_ctx(nand);
  631. kfree(ecc_cfg);
  632. }
  633. static int mtk_snand_ecc_prepare_io_req(struct nand_device *nand,
  634. struct nand_page_io_req *req)
  635. {
  636. struct mtk_snand *snf = nand_to_mtk_snand(nand);
  637. struct mtk_ecc_config *ecc_cfg = nand_to_ecc_ctx(nand);
  638. int ret;
  639. ret = mtk_snand_setup_pagefmt(snf, nand->memorg.pagesize,
  640. nand->memorg.oobsize);
  641. if (ret)
  642. return ret;
  643. snf->autofmt = true;
  644. snf->ecc_cfg = ecc_cfg;
  645. return 0;
  646. }
  647. static int mtk_snand_ecc_finish_io_req(struct nand_device *nand,
  648. struct nand_page_io_req *req)
  649. {
  650. struct mtk_snand *snf = nand_to_mtk_snand(nand);
  651. struct mtd_info *mtd = nanddev_to_mtd(nand);
  652. snf->ecc_cfg = NULL;
  653. snf->autofmt = false;
  654. if ((req->mode == MTD_OPS_RAW) || (req->type != NAND_PAGE_READ))
  655. return 0;
  656. if (snf->ecc_stats.failed)
  657. mtd->ecc_stats.failed += snf->ecc_stats.failed;
  658. mtd->ecc_stats.corrected += snf->ecc_stats.corrected;
  659. return snf->ecc_stats.failed ? -EBADMSG : snf->ecc_stats.bitflips;
  660. }
  661. static struct nand_ecc_engine_ops mtk_snfi_ecc_engine_ops = {
  662. .init_ctx = mtk_snand_ecc_init_ctx,
  663. .cleanup_ctx = mtk_snand_ecc_cleanup_ctx,
  664. .prepare_io_req = mtk_snand_ecc_prepare_io_req,
  665. .finish_io_req = mtk_snand_ecc_finish_io_req,
  666. };
  667. static void mtk_snand_read_fdm(struct mtk_snand *snf, u8 *buf)
  668. {
  669. u32 vall, valm;
  670. u8 *oobptr = buf;
  671. int i, j;
  672. for (i = 0; i < snf->nfi_cfg.nsectors; i++) {
  673. vall = nfi_read32(snf, NFI_FDML(i));
  674. valm = nfi_read32(snf, NFI_FDMM(i));
  675. for (j = 0; j < snf->caps->fdm_size; j++)
  676. oobptr[j] = (j >= 4 ? valm : vall) >> ((j % 4) * 8);
  677. oobptr += snf->caps->fdm_size;
  678. }
  679. }
  680. static void mtk_snand_write_fdm(struct mtk_snand *snf, const u8 *buf)
  681. {
  682. u32 fdm_size = snf->caps->fdm_size;
  683. const u8 *oobptr = buf;
  684. u32 vall, valm;
  685. int i, j;
  686. for (i = 0; i < snf->nfi_cfg.nsectors; i++) {
  687. vall = 0;
  688. valm = 0;
  689. for (j = 0; j < 8; j++) {
  690. if (j < 4)
  691. vall |= (j < fdm_size ? oobptr[j] : 0xff)
  692. << (j * 8);
  693. else
  694. valm |= (j < fdm_size ? oobptr[j] : 0xff)
  695. << ((j - 4) * 8);
  696. }
  697. nfi_write32(snf, NFI_FDML(i), vall);
  698. nfi_write32(snf, NFI_FDMM(i), valm);
  699. oobptr += fdm_size;
  700. }
  701. }
  702. static void mtk_snand_bm_swap(struct mtk_snand *snf, u8 *buf)
  703. {
  704. u32 buf_bbm_pos, fdm_bbm_pos;
  705. if (!snf->caps->bbm_swap || snf->nfi_cfg.nsectors == 1)
  706. return;
  707. // swap [pagesize] byte on nand with the first fdm byte
  708. // in the last sector.
  709. buf_bbm_pos = snf->nfi_cfg.page_size -
  710. (snf->nfi_cfg.nsectors - 1) * snf->nfi_cfg.spare_size;
  711. fdm_bbm_pos = snf->nfi_cfg.page_size +
  712. (snf->nfi_cfg.nsectors - 1) * snf->caps->fdm_size;
  713. swap(snf->buf[fdm_bbm_pos], buf[buf_bbm_pos]);
  714. }
  715. static void mtk_snand_fdm_bm_swap(struct mtk_snand *snf)
  716. {
  717. u32 fdm_bbm_pos1, fdm_bbm_pos2;
  718. if (!snf->caps->bbm_swap || snf->nfi_cfg.nsectors == 1)
  719. return;
  720. // swap the first fdm byte in the first and the last sector.
  721. fdm_bbm_pos1 = snf->nfi_cfg.page_size;
  722. fdm_bbm_pos2 = snf->nfi_cfg.page_size +
  723. (snf->nfi_cfg.nsectors - 1) * snf->caps->fdm_size;
  724. swap(snf->buf[fdm_bbm_pos1], snf->buf[fdm_bbm_pos2]);
  725. }
  726. static int mtk_snand_read_page_cache(struct mtk_snand *snf,
  727. const struct spi_mem_op *op)
  728. {
  729. u8 *buf = snf->buf;
  730. u8 *buf_fdm = buf + snf->nfi_cfg.page_size;
  731. // the address part to be sent by the controller
  732. u32 op_addr = op->addr.val;
  733. // where to start copying data from bounce buffer
  734. u32 rd_offset = 0;
  735. u32 dummy_clk = (op->dummy.nbytes * BITS_PER_BYTE / op->dummy.buswidth);
  736. u32 op_mode = 0;
  737. u32 dma_len = snf->buf_len;
  738. int ret = 0;
  739. u32 rd_mode, rd_bytes, val;
  740. dma_addr_t buf_dma;
  741. if (snf->autofmt) {
  742. u32 last_bit;
  743. u32 mask;
  744. dma_len = snf->nfi_cfg.page_size;
  745. op_mode = CNFG_AUTO_FMT_EN;
  746. if (op->data.ecc)
  747. op_mode |= CNFG_HW_ECC_EN;
  748. // extract the plane bit:
  749. // Find the highest bit set in (pagesize+oobsize).
  750. // Bits higher than that in op->addr are kept and sent over SPI
  751. // Lower bits are used as an offset for copying data from DMA
  752. // bounce buffer.
  753. last_bit = fls(snf->nfi_cfg.page_size + snf->nfi_cfg.oob_size);
  754. mask = (1 << last_bit) - 1;
  755. rd_offset = op_addr & mask;
  756. op_addr &= ~mask;
  757. // check if we can dma to the caller memory
  758. if (rd_offset == 0 && op->data.nbytes >= snf->nfi_cfg.page_size)
  759. buf = op->data.buf.in;
  760. }
  761. mtk_snand_mac_reset(snf);
  762. mtk_nfi_reset(snf);
  763. // command and dummy cycles
  764. nfi_write32(snf, SNF_RD_CTL2,
  765. (dummy_clk << DATA_READ_DUMMY_S) |
  766. (op->cmd.opcode << DATA_READ_CMD_S));
  767. // read address
  768. nfi_write32(snf, SNF_RD_CTL3, op_addr);
  769. // Set read op_mode
  770. if (op->data.buswidth == 4)
  771. rd_mode = op->addr.buswidth == 4 ? DATA_READ_MODE_QUAD :
  772. DATA_READ_MODE_X4;
  773. else if (op->data.buswidth == 2)
  774. rd_mode = op->addr.buswidth == 2 ? DATA_READ_MODE_DUAL :
  775. DATA_READ_MODE_X2;
  776. else
  777. rd_mode = DATA_READ_MODE_X1;
  778. rd_mode <<= DATA_READ_MODE_S;
  779. nfi_rmw32(snf, SNF_MISC_CTL, DATA_READ_MODE,
  780. rd_mode | DATARD_CUSTOM_EN);
  781. // Set bytes to read
  782. rd_bytes = (snf->nfi_cfg.spare_size + snf->caps->sector_size) *
  783. snf->nfi_cfg.nsectors;
  784. nfi_write32(snf, SNF_MISC_CTL2,
  785. (rd_bytes << PROGRAM_LOAD_BYTE_NUM_S) | rd_bytes);
  786. // NFI read prepare
  787. nfi_write16(snf, NFI_CNFG,
  788. (CNFG_OP_MODE_CUST << CNFG_OP_MODE_S) | CNFG_DMA_BURST_EN |
  789. CNFG_READ_MODE | CNFG_DMA_MODE | op_mode);
  790. nfi_write32(snf, NFI_CON, (snf->nfi_cfg.nsectors << CON_SEC_NUM_S));
  791. buf_dma = dma_map_single(snf->dev, buf, dma_len, DMA_FROM_DEVICE);
  792. ret = dma_mapping_error(snf->dev, buf_dma);
  793. if (ret) {
  794. dev_err(snf->dev, "DMA mapping failed.\n");
  795. goto cleanup;
  796. }
  797. nfi_write32(snf, NFI_STRADDR, buf_dma);
  798. if (op->data.ecc) {
  799. snf->ecc_cfg->op = ECC_DECODE;
  800. ret = mtk_ecc_enable(snf->ecc, snf->ecc_cfg);
  801. if (ret)
  802. goto cleanup_dma;
  803. }
  804. // Prepare for custom read interrupt
  805. nfi_write32(snf, NFI_INTR_EN, NFI_IRQ_INTR_EN | NFI_IRQ_CUS_READ);
  806. reinit_completion(&snf->op_done);
  807. // Trigger NFI into custom mode
  808. nfi_write16(snf, NFI_CMD, NFI_CMD_DUMMY_READ);
  809. // Start DMA read
  810. nfi_rmw32(snf, NFI_CON, 0, CON_BRD);
  811. nfi_write16(snf, NFI_STRDATA, STR_DATA);
  812. if (!wait_for_completion_timeout(
  813. &snf->op_done, usecs_to_jiffies(SNFI_POLL_INTERVAL))) {
  814. dev_err(snf->dev, "DMA timed out for reading from cache.\n");
  815. ret = -ETIMEDOUT;
  816. goto cleanup;
  817. }
  818. // Wait for BUS_SEC_CNTR returning expected value
  819. ret = readl_poll_timeout(snf->nfi_base + NFI_BYTELEN, val,
  820. BUS_SEC_CNTR(val) >= snf->nfi_cfg.nsectors, 0,
  821. SNFI_POLL_INTERVAL);
  822. if (ret) {
  823. dev_err(snf->dev, "Timed out waiting for BUS_SEC_CNTR\n");
  824. goto cleanup2;
  825. }
  826. // Wait for bus becoming idle
  827. ret = readl_poll_timeout(snf->nfi_base + NFI_MASTERSTA, val,
  828. !(val & snf->caps->mastersta_mask), 0,
  829. SNFI_POLL_INTERVAL);
  830. if (ret) {
  831. dev_err(snf->dev, "Timed out waiting for bus becoming idle\n");
  832. goto cleanup2;
  833. }
  834. if (op->data.ecc) {
  835. ret = mtk_ecc_wait_done(snf->ecc, ECC_DECODE);
  836. if (ret) {
  837. dev_err(snf->dev, "wait ecc done timeout\n");
  838. goto cleanup2;
  839. }
  840. // save status before disabling ecc
  841. mtk_ecc_get_stats(snf->ecc, &snf->ecc_stats,
  842. snf->nfi_cfg.nsectors);
  843. }
  844. dma_unmap_single(snf->dev, buf_dma, dma_len, DMA_FROM_DEVICE);
  845. if (snf->autofmt) {
  846. mtk_snand_read_fdm(snf, buf_fdm);
  847. if (snf->caps->bbm_swap) {
  848. mtk_snand_bm_swap(snf, buf);
  849. mtk_snand_fdm_bm_swap(snf);
  850. }
  851. }
  852. // copy data back
  853. if (nfi_read32(snf, NFI_STA) & READ_EMPTY) {
  854. memset(op->data.buf.in, 0xff, op->data.nbytes);
  855. snf->ecc_stats.bitflips = 0;
  856. snf->ecc_stats.failed = 0;
  857. snf->ecc_stats.corrected = 0;
  858. } else {
  859. if (buf == op->data.buf.in) {
  860. u32 cap_len = snf->buf_len - snf->nfi_cfg.page_size;
  861. u32 req_left = op->data.nbytes - snf->nfi_cfg.page_size;
  862. if (req_left)
  863. memcpy(op->data.buf.in + snf->nfi_cfg.page_size,
  864. buf_fdm,
  865. cap_len < req_left ? cap_len : req_left);
  866. } else if (rd_offset < snf->buf_len) {
  867. u32 cap_len = snf->buf_len - rd_offset;
  868. if (op->data.nbytes < cap_len)
  869. cap_len = op->data.nbytes;
  870. memcpy(op->data.buf.in, snf->buf + rd_offset, cap_len);
  871. }
  872. }
  873. cleanup2:
  874. if (op->data.ecc)
  875. mtk_ecc_disable(snf->ecc);
  876. cleanup_dma:
  877. // unmap dma only if any error happens. (otherwise it's done before
  878. // data copying)
  879. if (ret)
  880. dma_unmap_single(snf->dev, buf_dma, dma_len, DMA_FROM_DEVICE);
  881. cleanup:
  882. // Stop read
  883. nfi_write32(snf, NFI_CON, 0);
  884. nfi_write16(snf, NFI_CNFG, 0);
  885. // Clear SNF done flag
  886. nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_READ_DONE);
  887. nfi_write32(snf, SNF_STA_CTL1, 0);
  888. // Disable interrupt
  889. nfi_read32(snf, NFI_INTR_STA);
  890. nfi_write32(snf, NFI_INTR_EN, 0);
  891. nfi_rmw32(snf, SNF_MISC_CTL, DATARD_CUSTOM_EN, 0);
  892. return ret;
  893. }
  894. static int mtk_snand_write_page_cache(struct mtk_snand *snf,
  895. const struct spi_mem_op *op)
  896. {
  897. // the address part to be sent by the controller
  898. u32 op_addr = op->addr.val;
  899. // where to start copying data from bounce buffer
  900. u32 wr_offset = 0;
  901. u32 op_mode = 0;
  902. int ret = 0;
  903. u32 wr_mode = 0;
  904. u32 dma_len = snf->buf_len;
  905. u32 wr_bytes, val;
  906. size_t cap_len;
  907. dma_addr_t buf_dma;
  908. if (snf->autofmt) {
  909. u32 last_bit;
  910. u32 mask;
  911. dma_len = snf->nfi_cfg.page_size;
  912. op_mode = CNFG_AUTO_FMT_EN;
  913. if (op->data.ecc)
  914. op_mode |= CNFG_HW_ECC_EN;
  915. last_bit = fls(snf->nfi_cfg.page_size + snf->nfi_cfg.oob_size);
  916. mask = (1 << last_bit) - 1;
  917. wr_offset = op_addr & mask;
  918. op_addr &= ~mask;
  919. }
  920. mtk_snand_mac_reset(snf);
  921. mtk_nfi_reset(snf);
  922. if (wr_offset)
  923. memset(snf->buf, 0xff, wr_offset);
  924. cap_len = snf->buf_len - wr_offset;
  925. if (op->data.nbytes < cap_len)
  926. cap_len = op->data.nbytes;
  927. memcpy(snf->buf + wr_offset, op->data.buf.out, cap_len);
  928. if (snf->autofmt) {
  929. if (snf->caps->bbm_swap) {
  930. mtk_snand_fdm_bm_swap(snf);
  931. mtk_snand_bm_swap(snf, snf->buf);
  932. }
  933. mtk_snand_write_fdm(snf, snf->buf + snf->nfi_cfg.page_size);
  934. }
  935. // Command
  936. nfi_write32(snf, SNF_PG_CTL1, (op->cmd.opcode << PG_LOAD_CMD_S));
  937. // write address
  938. nfi_write32(snf, SNF_PG_CTL2, op_addr);
  939. // Set read op_mode
  940. if (op->data.buswidth == 4)
  941. wr_mode = PG_LOAD_X4_EN;
  942. nfi_rmw32(snf, SNF_MISC_CTL, PG_LOAD_X4_EN,
  943. wr_mode | PG_LOAD_CUSTOM_EN);
  944. // Set bytes to write
  945. wr_bytes = (snf->nfi_cfg.spare_size + snf->caps->sector_size) *
  946. snf->nfi_cfg.nsectors;
  947. nfi_write32(snf, SNF_MISC_CTL2,
  948. (wr_bytes << PROGRAM_LOAD_BYTE_NUM_S) | wr_bytes);
  949. // NFI write prepare
  950. nfi_write16(snf, NFI_CNFG,
  951. (CNFG_OP_MODE_PROGRAM << CNFG_OP_MODE_S) |
  952. CNFG_DMA_BURST_EN | CNFG_DMA_MODE | op_mode);
  953. nfi_write32(snf, NFI_CON, (snf->nfi_cfg.nsectors << CON_SEC_NUM_S));
  954. buf_dma = dma_map_single(snf->dev, snf->buf, dma_len, DMA_TO_DEVICE);
  955. ret = dma_mapping_error(snf->dev, buf_dma);
  956. if (ret) {
  957. dev_err(snf->dev, "DMA mapping failed.\n");
  958. goto cleanup;
  959. }
  960. nfi_write32(snf, NFI_STRADDR, buf_dma);
  961. if (op->data.ecc) {
  962. snf->ecc_cfg->op = ECC_ENCODE;
  963. ret = mtk_ecc_enable(snf->ecc, snf->ecc_cfg);
  964. if (ret)
  965. goto cleanup_dma;
  966. }
  967. // Prepare for custom write interrupt
  968. nfi_write32(snf, NFI_INTR_EN, NFI_IRQ_INTR_EN | NFI_IRQ_CUS_PG);
  969. reinit_completion(&snf->op_done);
  970. ;
  971. // Trigger NFI into custom mode
  972. nfi_write16(snf, NFI_CMD, NFI_CMD_DUMMY_WRITE);
  973. // Start DMA write
  974. nfi_rmw32(snf, NFI_CON, 0, CON_BWR);
  975. nfi_write16(snf, NFI_STRDATA, STR_DATA);
  976. if (!wait_for_completion_timeout(
  977. &snf->op_done, usecs_to_jiffies(SNFI_POLL_INTERVAL))) {
  978. dev_err(snf->dev, "DMA timed out for program load.\n");
  979. ret = -ETIMEDOUT;
  980. goto cleanup_ecc;
  981. }
  982. // Wait for NFI_SEC_CNTR returning expected value
  983. ret = readl_poll_timeout(snf->nfi_base + NFI_ADDRCNTR, val,
  984. NFI_SEC_CNTR(val) >= snf->nfi_cfg.nsectors, 0,
  985. SNFI_POLL_INTERVAL);
  986. if (ret)
  987. dev_err(snf->dev, "Timed out waiting for NFI_SEC_CNTR\n");
  988. cleanup_ecc:
  989. if (op->data.ecc)
  990. mtk_ecc_disable(snf->ecc);
  991. cleanup_dma:
  992. dma_unmap_single(snf->dev, buf_dma, dma_len, DMA_TO_DEVICE);
  993. cleanup:
  994. // Stop write
  995. nfi_write32(snf, NFI_CON, 0);
  996. nfi_write16(snf, NFI_CNFG, 0);
  997. // Clear SNF done flag
  998. nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_PG_DONE);
  999. nfi_write32(snf, SNF_STA_CTL1, 0);
  1000. // Disable interrupt
  1001. nfi_read32(snf, NFI_INTR_STA);
  1002. nfi_write32(snf, NFI_INTR_EN, 0);
  1003. nfi_rmw32(snf, SNF_MISC_CTL, PG_LOAD_CUSTOM_EN, 0);
  1004. return ret;
  1005. }
  1006. /**
  1007. * mtk_snand_is_page_ops() - check if the op is a controller supported page op.
  1008. * @op: spi-mem op to check
  1009. *
  1010. * Check whether op can be executed with read_from_cache or program_load
  1011. * mode in the controller.
  1012. * This controller can execute typical Read From Cache and Program Load
  1013. * instructions found on SPI-NAND with 2-byte address.
  1014. * DTR and cmd buswidth & nbytes should be checked before calling this.
  1015. *
  1016. * Return: true if the op matches the instruction template
  1017. */
  1018. static bool mtk_snand_is_page_ops(const struct spi_mem_op *op)
  1019. {
  1020. if (op->addr.nbytes != 2)
  1021. return false;
  1022. if (op->addr.buswidth != 1 && op->addr.buswidth != 2 &&
  1023. op->addr.buswidth != 4)
  1024. return false;
  1025. // match read from page instructions
  1026. if (op->data.dir == SPI_MEM_DATA_IN) {
  1027. // check dummy cycle first
  1028. if (op->dummy.nbytes * BITS_PER_BYTE / op->dummy.buswidth >
  1029. DATA_READ_MAX_DUMMY)
  1030. return false;
  1031. // quad io / quad out
  1032. if ((op->addr.buswidth == 4 || op->addr.buswidth == 1) &&
  1033. op->data.buswidth == 4)
  1034. return true;
  1035. // dual io / dual out
  1036. if ((op->addr.buswidth == 2 || op->addr.buswidth == 1) &&
  1037. op->data.buswidth == 2)
  1038. return true;
  1039. // standard spi
  1040. if (op->addr.buswidth == 1 && op->data.buswidth == 1)
  1041. return true;
  1042. } else if (op->data.dir == SPI_MEM_DATA_OUT) {
  1043. // check dummy cycle first
  1044. if (op->dummy.nbytes)
  1045. return false;
  1046. // program load quad out
  1047. if (op->addr.buswidth == 1 && op->data.buswidth == 4)
  1048. return true;
  1049. // standard spi
  1050. if (op->addr.buswidth == 1 && op->data.buswidth == 1)
  1051. return true;
  1052. }
  1053. return false;
  1054. }
  1055. static bool mtk_snand_supports_op(struct spi_mem *mem,
  1056. const struct spi_mem_op *op)
  1057. {
  1058. if (!spi_mem_default_supports_op(mem, op))
  1059. return false;
  1060. if (op->cmd.nbytes != 1 || op->cmd.buswidth != 1)
  1061. return false;
  1062. if (mtk_snand_is_page_ops(op))
  1063. return true;
  1064. return ((op->addr.nbytes == 0 || op->addr.buswidth == 1) &&
  1065. (op->dummy.nbytes == 0 || op->dummy.buswidth == 1) &&
  1066. (op->data.nbytes == 0 || op->data.buswidth == 1));
  1067. }
  1068. static int mtk_snand_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
  1069. {
  1070. struct mtk_snand *ms = spi_controller_get_devdata(mem->spi->controller);
  1071. // page ops transfer size must be exactly ((sector_size + spare_size) *
  1072. // nsectors). Limit the op size if the caller requests more than that.
  1073. // exec_op will read more than needed and discard the leftover if the
  1074. // caller requests less data.
  1075. if (mtk_snand_is_page_ops(op)) {
  1076. size_t l;
  1077. // skip adjust_op_size for page ops
  1078. if (ms->autofmt)
  1079. return 0;
  1080. l = ms->caps->sector_size + ms->nfi_cfg.spare_size;
  1081. l *= ms->nfi_cfg.nsectors;
  1082. if (op->data.nbytes > l)
  1083. op->data.nbytes = l;
  1084. } else {
  1085. size_t hl = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
  1086. if (hl >= SNF_GPRAM_SIZE)
  1087. return -EOPNOTSUPP;
  1088. if (op->data.nbytes > SNF_GPRAM_SIZE - hl)
  1089. op->data.nbytes = SNF_GPRAM_SIZE - hl;
  1090. }
  1091. return 0;
  1092. }
  1093. static int mtk_snand_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
  1094. {
  1095. struct mtk_snand *ms = spi_controller_get_devdata(mem->spi->controller);
  1096. dev_dbg(ms->dev, "OP %02x ADDR %08llX@%d:%u DATA %d:%u", op->cmd.opcode,
  1097. op->addr.val, op->addr.buswidth, op->addr.nbytes,
  1098. op->data.buswidth, op->data.nbytes);
  1099. if (mtk_snand_is_page_ops(op)) {
  1100. if (op->data.dir == SPI_MEM_DATA_IN)
  1101. return mtk_snand_read_page_cache(ms, op);
  1102. else
  1103. return mtk_snand_write_page_cache(ms, op);
  1104. } else {
  1105. return mtk_snand_mac_io(ms, op);
  1106. }
  1107. }
  1108. static const struct spi_controller_mem_ops mtk_snand_mem_ops = {
  1109. .adjust_op_size = mtk_snand_adjust_op_size,
  1110. .supports_op = mtk_snand_supports_op,
  1111. .exec_op = mtk_snand_exec_op,
  1112. };
  1113. static const struct spi_controller_mem_caps mtk_snand_mem_caps = {
  1114. .ecc = true,
  1115. };
  1116. static irqreturn_t mtk_snand_irq(int irq, void *id)
  1117. {
  1118. struct mtk_snand *snf = id;
  1119. u32 sta, ien;
  1120. sta = nfi_read32(snf, NFI_INTR_STA);
  1121. ien = nfi_read32(snf, NFI_INTR_EN);
  1122. if (!(sta & ien))
  1123. return IRQ_NONE;
  1124. nfi_write32(snf, NFI_INTR_EN, 0);
  1125. complete(&snf->op_done);
  1126. return IRQ_HANDLED;
  1127. }
  1128. static const struct of_device_id mtk_snand_ids[] = {
  1129. { .compatible = "mediatek,mt7622-snand", .data = &mt7622_snand_caps },
  1130. { .compatible = "mediatek,mt7629-snand", .data = &mt7629_snand_caps },
  1131. { .compatible = "mediatek,mt7986-snand", .data = &mt7986_snand_caps },
  1132. {},
  1133. };
  1134. MODULE_DEVICE_TABLE(of, mtk_snand_ids);
  1135. static int mtk_snand_probe(struct platform_device *pdev)
  1136. {
  1137. struct device_node *np = pdev->dev.of_node;
  1138. const struct of_device_id *dev_id;
  1139. struct spi_controller *ctlr;
  1140. struct mtk_snand *ms;
  1141. unsigned long spi_freq;
  1142. u32 val = 0;
  1143. int ret;
  1144. dev_id = of_match_node(mtk_snand_ids, np);
  1145. if (!dev_id)
  1146. return -EINVAL;
  1147. ctlr = devm_spi_alloc_host(&pdev->dev, sizeof(*ms));
  1148. if (!ctlr)
  1149. return -ENOMEM;
  1150. platform_set_drvdata(pdev, ctlr);
  1151. ms = spi_controller_get_devdata(ctlr);
  1152. ms->ctlr = ctlr;
  1153. ms->caps = dev_id->data;
  1154. ms->ecc = of_mtk_ecc_get(np);
  1155. if (IS_ERR(ms->ecc))
  1156. return PTR_ERR(ms->ecc);
  1157. else if (!ms->ecc)
  1158. return -ENODEV;
  1159. ms->nfi_base = devm_platform_ioremap_resource(pdev, 0);
  1160. if (IS_ERR(ms->nfi_base)) {
  1161. ret = PTR_ERR(ms->nfi_base);
  1162. goto release_ecc;
  1163. }
  1164. ms->dev = &pdev->dev;
  1165. ms->nfi_clk = devm_clk_get_enabled(&pdev->dev, "nfi_clk");
  1166. if (IS_ERR(ms->nfi_clk)) {
  1167. ret = PTR_ERR(ms->nfi_clk);
  1168. dev_err(&pdev->dev, "unable to get nfi_clk, err = %d\n", ret);
  1169. goto release_ecc;
  1170. }
  1171. ms->pad_clk = devm_clk_get_enabled(&pdev->dev, "pad_clk");
  1172. if (IS_ERR(ms->pad_clk)) {
  1173. ret = PTR_ERR(ms->pad_clk);
  1174. dev_err(&pdev->dev, "unable to get pad_clk, err = %d\n", ret);
  1175. goto release_ecc;
  1176. }
  1177. ms->nfi_hclk = devm_clk_get_optional_enabled(&pdev->dev, "nfi_hclk");
  1178. if (IS_ERR(ms->nfi_hclk)) {
  1179. ret = PTR_ERR(ms->nfi_hclk);
  1180. dev_err(&pdev->dev, "unable to get nfi_hclk, err = %d\n", ret);
  1181. goto release_ecc;
  1182. }
  1183. init_completion(&ms->op_done);
  1184. ms->irq = platform_get_irq(pdev, 0);
  1185. if (ms->irq < 0) {
  1186. ret = ms->irq;
  1187. goto release_ecc;
  1188. }
  1189. ret = devm_request_irq(ms->dev, ms->irq, mtk_snand_irq, 0x0,
  1190. "mtk-snand", ms);
  1191. if (ret) {
  1192. dev_err(ms->dev, "failed to request snfi irq\n");
  1193. goto release_ecc;
  1194. }
  1195. ret = dma_set_mask(ms->dev, DMA_BIT_MASK(32));
  1196. if (ret) {
  1197. dev_err(ms->dev, "failed to set dma mask\n");
  1198. goto release_ecc;
  1199. }
  1200. // switch to SNFI mode
  1201. nfi_write32(ms, SNF_CFG, SPI_MODE);
  1202. ret = of_property_read_u32(np, "rx-sample-delay-ns", &val);
  1203. if (!ret)
  1204. nfi_rmw32(ms, SNF_DLY_CTL3, SFCK_SAM_DLY,
  1205. val * SFCK_SAM_DLY_RANGE / SFCK_SAM_DLY_TOTAL);
  1206. ret = of_property_read_u32(np, "mediatek,rx-latch-latency-ns", &val);
  1207. if (!ret) {
  1208. spi_freq = clk_get_rate(ms->pad_clk);
  1209. val = DIV_ROUND_CLOSEST(val, NSEC_PER_SEC / spi_freq);
  1210. nfi_rmw32(ms, SNF_MISC_CTL, DATA_READ_LATCH_LAT,
  1211. val << DATA_READ_LATCH_LAT_S);
  1212. }
  1213. // setup an initial page format for ops matching page_cache_op template
  1214. // before ECC is called.
  1215. ret = mtk_snand_setup_pagefmt(ms, SZ_2K, SZ_64);
  1216. if (ret) {
  1217. dev_err(ms->dev, "failed to set initial page format\n");
  1218. goto release_ecc;
  1219. }
  1220. // setup ECC engine
  1221. ms->ecc_eng.dev = &pdev->dev;
  1222. ms->ecc_eng.integration = NAND_ECC_ENGINE_INTEGRATION_PIPELINED;
  1223. ms->ecc_eng.ops = &mtk_snfi_ecc_engine_ops;
  1224. ms->ecc_eng.priv = ms;
  1225. ret = nand_ecc_register_on_host_hw_engine(&ms->ecc_eng);
  1226. if (ret) {
  1227. dev_err(&pdev->dev, "failed to register ecc engine.\n");
  1228. goto release_ecc;
  1229. }
  1230. ctlr->num_chipselect = 1;
  1231. ctlr->mem_ops = &mtk_snand_mem_ops;
  1232. ctlr->mem_caps = &mtk_snand_mem_caps;
  1233. ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
  1234. ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD;
  1235. ctlr->dev.of_node = pdev->dev.of_node;
  1236. ret = spi_register_controller(ctlr);
  1237. if (ret) {
  1238. dev_err(&pdev->dev, "spi_register_controller failed.\n");
  1239. goto release_ecc;
  1240. }
  1241. return 0;
  1242. release_ecc:
  1243. mtk_ecc_release(ms->ecc);
  1244. return ret;
  1245. }
  1246. static void mtk_snand_remove(struct platform_device *pdev)
  1247. {
  1248. struct spi_controller *ctlr = platform_get_drvdata(pdev);
  1249. struct mtk_snand *ms = spi_controller_get_devdata(ctlr);
  1250. spi_unregister_controller(ctlr);
  1251. mtk_ecc_release(ms->ecc);
  1252. kfree(ms->buf);
  1253. }
  1254. static struct platform_driver mtk_snand_driver = {
  1255. .probe = mtk_snand_probe,
  1256. .remove_new = mtk_snand_remove,
  1257. .driver = {
  1258. .name = "mtk-snand",
  1259. .of_match_table = mtk_snand_ids,
  1260. },
  1261. };
  1262. module_platform_driver(mtk_snand_driver);
  1263. MODULE_LICENSE("GPL");
  1264. MODULE_AUTHOR("Chuanhong Guo <gch981213@gmail.com>");
  1265. MODULE_DESCRIPTION("MeidaTek SPI-NAND Flash Controller Driver");