spi-mtk-snfi.c 39 KB

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