pxa3xx_nand.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/mtd/nand/pxa3xx_nand.c
  4. *
  5. * Copyright © 2005 Intel Corporation
  6. * Copyright © 2006 Marvell International Ltd.
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <fdtdec.h>
  11. #include <nand.h>
  12. #include <linux/errno.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/cpu.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/rawnand.h>
  17. #include <linux/types.h>
  18. #include "pxa3xx_nand.h"
  19. DECLARE_GLOBAL_DATA_PTR;
  20. #define TIMEOUT_DRAIN_FIFO 5 /* in ms */
  21. #define CHIP_DELAY_TIMEOUT 200
  22. #define NAND_STOP_DELAY 40
  23. #define PAGE_CHUNK_SIZE (2048)
  24. /*
  25. * Define a buffer size for the initial command that detects the flash device:
  26. * STATUS, READID and PARAM. The largest of these is the PARAM command,
  27. * needing 256 bytes.
  28. */
  29. #define INIT_BUFFER_SIZE 256
  30. /* registers and bit definitions */
  31. #define NDCR (0x00) /* Control register */
  32. #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
  33. #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
  34. #define NDSR (0x14) /* Status Register */
  35. #define NDPCR (0x18) /* Page Count Register */
  36. #define NDBDR0 (0x1C) /* Bad Block Register 0 */
  37. #define NDBDR1 (0x20) /* Bad Block Register 1 */
  38. #define NDECCCTRL (0x28) /* ECC control */
  39. #define NDDB (0x40) /* Data Buffer */
  40. #define NDCB0 (0x48) /* Command Buffer0 */
  41. #define NDCB1 (0x4C) /* Command Buffer1 */
  42. #define NDCB2 (0x50) /* Command Buffer2 */
  43. #define NDCR_SPARE_EN (0x1 << 31)
  44. #define NDCR_ECC_EN (0x1 << 30)
  45. #define NDCR_DMA_EN (0x1 << 29)
  46. #define NDCR_ND_RUN (0x1 << 28)
  47. #define NDCR_DWIDTH_C (0x1 << 27)
  48. #define NDCR_DWIDTH_M (0x1 << 26)
  49. #define NDCR_PAGE_SZ (0x1 << 24)
  50. #define NDCR_NCSX (0x1 << 23)
  51. #define NDCR_ND_MODE (0x3 << 21)
  52. #define NDCR_NAND_MODE (0x0)
  53. #define NDCR_CLR_PG_CNT (0x1 << 20)
  54. #define NDCR_STOP_ON_UNCOR (0x1 << 19)
  55. #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
  56. #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
  57. #define NDCR_RA_START (0x1 << 15)
  58. #define NDCR_PG_PER_BLK (0x1 << 14)
  59. #define NDCR_ND_ARB_EN (0x1 << 12)
  60. #define NDCR_INT_MASK (0xFFF)
  61. #define NDSR_MASK (0xfff)
  62. #define NDSR_ERR_CNT_OFF (16)
  63. #define NDSR_ERR_CNT_MASK (0x1f)
  64. #define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
  65. #define NDSR_RDY (0x1 << 12)
  66. #define NDSR_FLASH_RDY (0x1 << 11)
  67. #define NDSR_CS0_PAGED (0x1 << 10)
  68. #define NDSR_CS1_PAGED (0x1 << 9)
  69. #define NDSR_CS0_CMDD (0x1 << 8)
  70. #define NDSR_CS1_CMDD (0x1 << 7)
  71. #define NDSR_CS0_BBD (0x1 << 6)
  72. #define NDSR_CS1_BBD (0x1 << 5)
  73. #define NDSR_UNCORERR (0x1 << 4)
  74. #define NDSR_CORERR (0x1 << 3)
  75. #define NDSR_WRDREQ (0x1 << 2)
  76. #define NDSR_RDDREQ (0x1 << 1)
  77. #define NDSR_WRCMDREQ (0x1)
  78. #define NDCB0_LEN_OVRD (0x1 << 28)
  79. #define NDCB0_ST_ROW_EN (0x1 << 26)
  80. #define NDCB0_AUTO_RS (0x1 << 25)
  81. #define NDCB0_CSEL (0x1 << 24)
  82. #define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
  83. #define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
  84. #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
  85. #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
  86. #define NDCB0_NC (0x1 << 20)
  87. #define NDCB0_DBC (0x1 << 19)
  88. #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
  89. #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
  90. #define NDCB0_CMD2_MASK (0xff << 8)
  91. #define NDCB0_CMD1_MASK (0xff)
  92. #define NDCB0_ADDR_CYC_SHIFT (16)
  93. #define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
  94. #define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
  95. #define EXT_CMD_TYPE_READ 4 /* Read */
  96. #define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
  97. #define EXT_CMD_TYPE_FINAL 3 /* Final command */
  98. #define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
  99. #define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
  100. /* macros for registers read/write */
  101. #define nand_writel(info, off, val) \
  102. writel((val), (info)->mmio_base + (off))
  103. #define nand_readl(info, off) \
  104. readl((info)->mmio_base + (off))
  105. /* error code and state */
  106. enum {
  107. ERR_NONE = 0,
  108. ERR_DMABUSERR = -1,
  109. ERR_SENDCMD = -2,
  110. ERR_UNCORERR = -3,
  111. ERR_BBERR = -4,
  112. ERR_CORERR = -5,
  113. };
  114. enum {
  115. STATE_IDLE = 0,
  116. STATE_PREPARED,
  117. STATE_CMD_HANDLE,
  118. STATE_DMA_READING,
  119. STATE_DMA_WRITING,
  120. STATE_DMA_DONE,
  121. STATE_PIO_READING,
  122. STATE_PIO_WRITING,
  123. STATE_CMD_DONE,
  124. STATE_READY,
  125. };
  126. enum pxa3xx_nand_variant {
  127. PXA3XX_NAND_VARIANT_PXA,
  128. PXA3XX_NAND_VARIANT_ARMADA370,
  129. };
  130. struct pxa3xx_nand_host {
  131. struct nand_chip chip;
  132. struct mtd_info *mtd;
  133. void *info_data;
  134. /* page size of attached chip */
  135. int use_ecc;
  136. int cs;
  137. /* calculated from pxa3xx_nand_flash data */
  138. unsigned int col_addr_cycles;
  139. unsigned int row_addr_cycles;
  140. size_t read_id_bytes;
  141. };
  142. struct pxa3xx_nand_info {
  143. struct nand_hw_control controller;
  144. struct pxa3xx_nand_platform_data *pdata;
  145. struct clk *clk;
  146. void __iomem *mmio_base;
  147. unsigned long mmio_phys;
  148. int cmd_complete, dev_ready;
  149. unsigned int buf_start;
  150. unsigned int buf_count;
  151. unsigned int buf_size;
  152. unsigned int data_buff_pos;
  153. unsigned int oob_buff_pos;
  154. unsigned char *data_buff;
  155. unsigned char *oob_buff;
  156. struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
  157. unsigned int state;
  158. /*
  159. * This driver supports NFCv1 (as found in PXA SoC)
  160. * and NFCv2 (as found in Armada 370/XP SoC).
  161. */
  162. enum pxa3xx_nand_variant variant;
  163. int cs;
  164. int use_ecc; /* use HW ECC ? */
  165. int ecc_bch; /* using BCH ECC? */
  166. int use_spare; /* use spare ? */
  167. int need_wait;
  168. unsigned int data_size; /* data to be read from FIFO */
  169. unsigned int chunk_size; /* split commands chunk size */
  170. unsigned int oob_size;
  171. unsigned int spare_size;
  172. unsigned int ecc_size;
  173. unsigned int ecc_err_cnt;
  174. unsigned int max_bitflips;
  175. int retcode;
  176. /* cached register value */
  177. uint32_t reg_ndcr;
  178. uint32_t ndtr0cs0;
  179. uint32_t ndtr1cs0;
  180. /* generated NDCBx register values */
  181. uint32_t ndcb0;
  182. uint32_t ndcb1;
  183. uint32_t ndcb2;
  184. uint32_t ndcb3;
  185. };
  186. static struct pxa3xx_nand_timing timing[] = {
  187. { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
  188. { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
  189. { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
  190. { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
  191. };
  192. static struct pxa3xx_nand_flash builtin_flash_types[] = {
  193. { 0x46ec, 16, 16, &timing[1] },
  194. { 0xdaec, 8, 8, &timing[1] },
  195. { 0xd7ec, 8, 8, &timing[1] },
  196. { 0xa12c, 8, 8, &timing[2] },
  197. { 0xb12c, 16, 16, &timing[2] },
  198. { 0xdc2c, 8, 8, &timing[2] },
  199. { 0xcc2c, 16, 16, &timing[2] },
  200. { 0xba20, 16, 16, &timing[3] },
  201. };
  202. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  203. static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
  204. static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
  205. static struct nand_bbt_descr bbt_main_descr = {
  206. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  207. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  208. .offs = 8,
  209. .len = 6,
  210. .veroffs = 14,
  211. .maxblocks = 8, /* Last 8 blocks in each chip */
  212. .pattern = bbt_pattern
  213. };
  214. static struct nand_bbt_descr bbt_mirror_descr = {
  215. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  216. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  217. .offs = 8,
  218. .len = 6,
  219. .veroffs = 14,
  220. .maxblocks = 8, /* Last 8 blocks in each chip */
  221. .pattern = bbt_mirror_pattern
  222. };
  223. #endif
  224. static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
  225. .eccbytes = 32,
  226. .eccpos = {
  227. 32, 33, 34, 35, 36, 37, 38, 39,
  228. 40, 41, 42, 43, 44, 45, 46, 47,
  229. 48, 49, 50, 51, 52, 53, 54, 55,
  230. 56, 57, 58, 59, 60, 61, 62, 63},
  231. .oobfree = { {2, 30} }
  232. };
  233. static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
  234. .eccbytes = 64,
  235. .eccpos = {
  236. 32, 33, 34, 35, 36, 37, 38, 39,
  237. 40, 41, 42, 43, 44, 45, 46, 47,
  238. 48, 49, 50, 51, 52, 53, 54, 55,
  239. 56, 57, 58, 59, 60, 61, 62, 63,
  240. 96, 97, 98, 99, 100, 101, 102, 103,
  241. 104, 105, 106, 107, 108, 109, 110, 111,
  242. 112, 113, 114, 115, 116, 117, 118, 119,
  243. 120, 121, 122, 123, 124, 125, 126, 127},
  244. /* Bootrom looks in bytes 0 & 5 for bad blocks */
  245. .oobfree = { {6, 26}, { 64, 32} }
  246. };
  247. static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
  248. .eccbytes = 128,
  249. .eccpos = {
  250. 32, 33, 34, 35, 36, 37, 38, 39,
  251. 40, 41, 42, 43, 44, 45, 46, 47,
  252. 48, 49, 50, 51, 52, 53, 54, 55,
  253. 56, 57, 58, 59, 60, 61, 62, 63},
  254. .oobfree = { }
  255. };
  256. #define NDTR0_tCH(c) (min((c), 7) << 19)
  257. #define NDTR0_tCS(c) (min((c), 7) << 16)
  258. #define NDTR0_tWH(c) (min((c), 7) << 11)
  259. #define NDTR0_tWP(c) (min((c), 7) << 8)
  260. #define NDTR0_tRH(c) (min((c), 7) << 3)
  261. #define NDTR0_tRP(c) (min((c), 7) << 0)
  262. #define NDTR1_tR(c) (min((c), 65535) << 16)
  263. #define NDTR1_tWHR(c) (min((c), 15) << 4)
  264. #define NDTR1_tAR(c) (min((c), 15) << 0)
  265. /* convert nano-seconds to nand flash controller clock cycles */
  266. #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
  267. static enum pxa3xx_nand_variant pxa3xx_nand_get_variant(void)
  268. {
  269. /* We only support the Armada 370/XP/38x for now */
  270. return PXA3XX_NAND_VARIANT_ARMADA370;
  271. }
  272. static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
  273. const struct pxa3xx_nand_timing *t)
  274. {
  275. struct pxa3xx_nand_info *info = host->info_data;
  276. unsigned long nand_clk = mvebu_get_nand_clock();
  277. uint32_t ndtr0, ndtr1;
  278. ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
  279. NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
  280. NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
  281. NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
  282. NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
  283. NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
  284. ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
  285. NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
  286. NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
  287. info->ndtr0cs0 = ndtr0;
  288. info->ndtr1cs0 = ndtr1;
  289. nand_writel(info, NDTR0CS0, ndtr0);
  290. nand_writel(info, NDTR1CS0, ndtr1);
  291. }
  292. static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host,
  293. const struct nand_sdr_timings *t)
  294. {
  295. struct pxa3xx_nand_info *info = host->info_data;
  296. struct nand_chip *chip = &host->chip;
  297. unsigned long nand_clk = mvebu_get_nand_clock();
  298. uint32_t ndtr0, ndtr1;
  299. u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000);
  300. u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000);
  301. u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000);
  302. u32 tWP_min = DIV_ROUND_UP(t->tWC_min - tWH_min, 1000);
  303. u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000);
  304. u32 tRP_min = DIV_ROUND_UP(t->tRC_min - tREH_min, 1000);
  305. u32 tR = chip->chip_delay * 1000;
  306. u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000);
  307. u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000);
  308. /* fallback to a default value if tR = 0 */
  309. if (!tR)
  310. tR = 20000;
  311. ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) |
  312. NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) |
  313. NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) |
  314. NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) |
  315. NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) |
  316. NDTR0_tRP(ns2cycle(tRP_min, nand_clk));
  317. ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) |
  318. NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) |
  319. NDTR1_tAR(ns2cycle(tAR_min, nand_clk));
  320. info->ndtr0cs0 = ndtr0;
  321. info->ndtr1cs0 = ndtr1;
  322. nand_writel(info, NDTR0CS0, ndtr0);
  323. nand_writel(info, NDTR1CS0, ndtr1);
  324. }
  325. static int pxa3xx_nand_init_timings(struct pxa3xx_nand_host *host)
  326. {
  327. const struct nand_sdr_timings *timings;
  328. struct nand_chip *chip = &host->chip;
  329. struct pxa3xx_nand_info *info = host->info_data;
  330. const struct pxa3xx_nand_flash *f = NULL;
  331. int mode, id, ntypes, i;
  332. mode = onfi_get_async_timing_mode(chip);
  333. if (mode == ONFI_TIMING_MODE_UNKNOWN) {
  334. ntypes = ARRAY_SIZE(builtin_flash_types);
  335. chip->cmdfunc(host->mtd, NAND_CMD_READID, 0x00, -1);
  336. id = chip->read_byte(host->mtd);
  337. id |= chip->read_byte(host->mtd) << 0x8;
  338. for (i = 0; i < ntypes; i++) {
  339. f = &builtin_flash_types[i];
  340. if (f->chip_id == id)
  341. break;
  342. }
  343. if (i == ntypes) {
  344. dev_err(&info->pdev->dev, "Error: timings not found\n");
  345. return -EINVAL;
  346. }
  347. pxa3xx_nand_set_timing(host, f->timing);
  348. if (f->flash_width == 16) {
  349. info->reg_ndcr |= NDCR_DWIDTH_M;
  350. chip->options |= NAND_BUSWIDTH_16;
  351. }
  352. info->reg_ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
  353. } else {
  354. mode = fls(mode) - 1;
  355. if (mode < 0)
  356. mode = 0;
  357. timings = onfi_async_timing_mode_to_sdr_timings(mode);
  358. if (IS_ERR(timings))
  359. return PTR_ERR(timings);
  360. pxa3xx_nand_set_sdr_timing(host, timings);
  361. }
  362. return 0;
  363. }
  364. /*
  365. * Set the data and OOB size, depending on the selected
  366. * spare and ECC configuration.
  367. * Only applicable to READ0, READOOB and PAGEPROG commands.
  368. */
  369. static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
  370. struct mtd_info *mtd)
  371. {
  372. int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
  373. info->data_size = mtd->writesize;
  374. if (!oob_enable)
  375. return;
  376. info->oob_size = info->spare_size;
  377. if (!info->use_ecc)
  378. info->oob_size += info->ecc_size;
  379. }
  380. /**
  381. * NOTE: it is a must to set ND_RUN first, then write
  382. * command buffer, otherwise, it does not work.
  383. * We enable all the interrupt at the same time, and
  384. * let pxa3xx_nand_irq to handle all logic.
  385. */
  386. static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
  387. {
  388. uint32_t ndcr;
  389. ndcr = info->reg_ndcr;
  390. if (info->use_ecc) {
  391. ndcr |= NDCR_ECC_EN;
  392. if (info->ecc_bch)
  393. nand_writel(info, NDECCCTRL, 0x1);
  394. } else {
  395. ndcr &= ~NDCR_ECC_EN;
  396. if (info->ecc_bch)
  397. nand_writel(info, NDECCCTRL, 0x0);
  398. }
  399. ndcr &= ~NDCR_DMA_EN;
  400. if (info->use_spare)
  401. ndcr |= NDCR_SPARE_EN;
  402. else
  403. ndcr &= ~NDCR_SPARE_EN;
  404. ndcr |= NDCR_ND_RUN;
  405. /* clear status bits and run */
  406. nand_writel(info, NDCR, 0);
  407. nand_writel(info, NDSR, NDSR_MASK);
  408. nand_writel(info, NDCR, ndcr);
  409. }
  410. static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  411. {
  412. uint32_t ndcr;
  413. ndcr = nand_readl(info, NDCR);
  414. nand_writel(info, NDCR, ndcr | int_mask);
  415. }
  416. static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
  417. {
  418. if (info->ecc_bch) {
  419. u32 ts;
  420. /*
  421. * According to the datasheet, when reading from NDDB
  422. * with BCH enabled, after each 32 bytes reads, we
  423. * have to make sure that the NDSR.RDDREQ bit is set.
  424. *
  425. * Drain the FIFO 8 32 bits reads at a time, and skip
  426. * the polling on the last read.
  427. */
  428. while (len > 8) {
  429. readsl(info->mmio_base + NDDB, data, 8);
  430. ts = get_timer(0);
  431. while (!(nand_readl(info, NDSR) & NDSR_RDDREQ)) {
  432. if (get_timer(ts) > TIMEOUT_DRAIN_FIFO) {
  433. dev_err(&info->pdev->dev,
  434. "Timeout on RDDREQ while draining the FIFO\n");
  435. return;
  436. }
  437. }
  438. data += 32;
  439. len -= 8;
  440. }
  441. }
  442. readsl(info->mmio_base + NDDB, data, len);
  443. }
  444. static void handle_data_pio(struct pxa3xx_nand_info *info)
  445. {
  446. unsigned int do_bytes = min(info->data_size, info->chunk_size);
  447. switch (info->state) {
  448. case STATE_PIO_WRITING:
  449. writesl(info->mmio_base + NDDB,
  450. info->data_buff + info->data_buff_pos,
  451. DIV_ROUND_UP(do_bytes, 4));
  452. if (info->oob_size > 0)
  453. writesl(info->mmio_base + NDDB,
  454. info->oob_buff + info->oob_buff_pos,
  455. DIV_ROUND_UP(info->oob_size, 4));
  456. break;
  457. case STATE_PIO_READING:
  458. drain_fifo(info,
  459. info->data_buff + info->data_buff_pos,
  460. DIV_ROUND_UP(do_bytes, 4));
  461. if (info->oob_size > 0)
  462. drain_fifo(info,
  463. info->oob_buff + info->oob_buff_pos,
  464. DIV_ROUND_UP(info->oob_size, 4));
  465. break;
  466. default:
  467. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  468. info->state);
  469. BUG();
  470. }
  471. /* Update buffer pointers for multi-page read/write */
  472. info->data_buff_pos += do_bytes;
  473. info->oob_buff_pos += info->oob_size;
  474. info->data_size -= do_bytes;
  475. }
  476. static void pxa3xx_nand_irq_thread(struct pxa3xx_nand_info *info)
  477. {
  478. handle_data_pio(info);
  479. info->state = STATE_CMD_DONE;
  480. nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
  481. }
  482. static irqreturn_t pxa3xx_nand_irq(struct pxa3xx_nand_info *info)
  483. {
  484. unsigned int status, is_completed = 0, is_ready = 0;
  485. unsigned int ready, cmd_done;
  486. irqreturn_t ret = IRQ_HANDLED;
  487. if (info->cs == 0) {
  488. ready = NDSR_FLASH_RDY;
  489. cmd_done = NDSR_CS0_CMDD;
  490. } else {
  491. ready = NDSR_RDY;
  492. cmd_done = NDSR_CS1_CMDD;
  493. }
  494. status = nand_readl(info, NDSR);
  495. if (status & NDSR_UNCORERR)
  496. info->retcode = ERR_UNCORERR;
  497. if (status & NDSR_CORERR) {
  498. info->retcode = ERR_CORERR;
  499. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
  500. info->ecc_bch)
  501. info->ecc_err_cnt = NDSR_ERR_CNT(status);
  502. else
  503. info->ecc_err_cnt = 1;
  504. /*
  505. * Each chunk composing a page is corrected independently,
  506. * and we need to store maximum number of corrected bitflips
  507. * to return it to the MTD layer in ecc.read_page().
  508. */
  509. info->max_bitflips = max_t(unsigned int,
  510. info->max_bitflips,
  511. info->ecc_err_cnt);
  512. }
  513. if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
  514. info->state = (status & NDSR_RDDREQ) ?
  515. STATE_PIO_READING : STATE_PIO_WRITING;
  516. /* Call the IRQ thread in U-Boot directly */
  517. pxa3xx_nand_irq_thread(info);
  518. return 0;
  519. }
  520. if (status & cmd_done) {
  521. info->state = STATE_CMD_DONE;
  522. is_completed = 1;
  523. }
  524. if (status & ready) {
  525. info->state = STATE_READY;
  526. is_ready = 1;
  527. }
  528. if (status & NDSR_WRCMDREQ) {
  529. nand_writel(info, NDSR, NDSR_WRCMDREQ);
  530. status &= ~NDSR_WRCMDREQ;
  531. info->state = STATE_CMD_HANDLE;
  532. /*
  533. * Command buffer registers NDCB{0-2} (and optionally NDCB3)
  534. * must be loaded by writing directly either 12 or 16
  535. * bytes directly to NDCB0, four bytes at a time.
  536. *
  537. * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
  538. * but each NDCBx register can be read.
  539. */
  540. nand_writel(info, NDCB0, info->ndcb0);
  541. nand_writel(info, NDCB0, info->ndcb1);
  542. nand_writel(info, NDCB0, info->ndcb2);
  543. /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
  544. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  545. nand_writel(info, NDCB0, info->ndcb3);
  546. }
  547. /* clear NDSR to let the controller exit the IRQ */
  548. nand_writel(info, NDSR, status);
  549. if (is_completed)
  550. info->cmd_complete = 1;
  551. if (is_ready)
  552. info->dev_ready = 1;
  553. return ret;
  554. }
  555. static inline int is_buf_blank(uint8_t *buf, size_t len)
  556. {
  557. for (; len > 0; len--)
  558. if (*buf++ != 0xff)
  559. return 0;
  560. return 1;
  561. }
  562. static void set_command_address(struct pxa3xx_nand_info *info,
  563. unsigned int page_size, uint16_t column, int page_addr)
  564. {
  565. /* small page addr setting */
  566. if (page_size < PAGE_CHUNK_SIZE) {
  567. info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
  568. | (column & 0xFF);
  569. info->ndcb2 = 0;
  570. } else {
  571. info->ndcb1 = ((page_addr & 0xFFFF) << 16)
  572. | (column & 0xFFFF);
  573. if (page_addr & 0xFF0000)
  574. info->ndcb2 = (page_addr & 0xFF0000) >> 16;
  575. else
  576. info->ndcb2 = 0;
  577. }
  578. }
  579. static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
  580. {
  581. struct pxa3xx_nand_host *host = info->host[info->cs];
  582. struct mtd_info *mtd = host->mtd;
  583. /* reset data and oob column point to handle data */
  584. info->buf_start = 0;
  585. info->buf_count = 0;
  586. info->oob_size = 0;
  587. info->data_buff_pos = 0;
  588. info->oob_buff_pos = 0;
  589. info->use_ecc = 0;
  590. info->use_spare = 1;
  591. info->retcode = ERR_NONE;
  592. info->ecc_err_cnt = 0;
  593. info->ndcb3 = 0;
  594. info->need_wait = 0;
  595. switch (command) {
  596. case NAND_CMD_READ0:
  597. case NAND_CMD_PAGEPROG:
  598. info->use_ecc = 1;
  599. case NAND_CMD_READOOB:
  600. pxa3xx_set_datasize(info, mtd);
  601. break;
  602. case NAND_CMD_PARAM:
  603. info->use_spare = 0;
  604. break;
  605. default:
  606. info->ndcb1 = 0;
  607. info->ndcb2 = 0;
  608. break;
  609. }
  610. /*
  611. * If we are about to issue a read command, or about to set
  612. * the write address, then clean the data buffer.
  613. */
  614. if (command == NAND_CMD_READ0 ||
  615. command == NAND_CMD_READOOB ||
  616. command == NAND_CMD_SEQIN) {
  617. info->buf_count = mtd->writesize + mtd->oobsize;
  618. memset(info->data_buff, 0xFF, info->buf_count);
  619. }
  620. }
  621. static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
  622. int ext_cmd_type, uint16_t column, int page_addr)
  623. {
  624. int addr_cycle, exec_cmd;
  625. struct pxa3xx_nand_host *host;
  626. struct mtd_info *mtd;
  627. host = info->host[info->cs];
  628. mtd = host->mtd;
  629. addr_cycle = 0;
  630. exec_cmd = 1;
  631. if (info->cs != 0)
  632. info->ndcb0 = NDCB0_CSEL;
  633. else
  634. info->ndcb0 = 0;
  635. if (command == NAND_CMD_SEQIN)
  636. exec_cmd = 0;
  637. addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
  638. + host->col_addr_cycles);
  639. switch (command) {
  640. case NAND_CMD_READOOB:
  641. case NAND_CMD_READ0:
  642. info->buf_start = column;
  643. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  644. | addr_cycle
  645. | NAND_CMD_READ0;
  646. if (command == NAND_CMD_READOOB)
  647. info->buf_start += mtd->writesize;
  648. /*
  649. * Multiple page read needs an 'extended command type' field,
  650. * which is either naked-read or last-read according to the
  651. * state.
  652. */
  653. if (mtd->writesize == PAGE_CHUNK_SIZE) {
  654. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
  655. } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
  656. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
  657. | NDCB0_LEN_OVRD
  658. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  659. info->ndcb3 = info->chunk_size +
  660. info->oob_size;
  661. }
  662. set_command_address(info, mtd->writesize, column, page_addr);
  663. break;
  664. case NAND_CMD_SEQIN:
  665. info->buf_start = column;
  666. set_command_address(info, mtd->writesize, 0, page_addr);
  667. /*
  668. * Multiple page programming needs to execute the initial
  669. * SEQIN command that sets the page address.
  670. */
  671. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  672. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  673. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  674. | addr_cycle
  675. | command;
  676. /* No data transfer in this case */
  677. info->data_size = 0;
  678. exec_cmd = 1;
  679. }
  680. break;
  681. case NAND_CMD_PAGEPROG:
  682. if (is_buf_blank(info->data_buff,
  683. (mtd->writesize + mtd->oobsize))) {
  684. exec_cmd = 0;
  685. break;
  686. }
  687. /* Second command setting for large pages */
  688. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  689. /*
  690. * Multiple page write uses the 'extended command'
  691. * field. This can be used to issue a command dispatch
  692. * or a naked-write depending on the current stage.
  693. */
  694. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  695. | NDCB0_LEN_OVRD
  696. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  697. info->ndcb3 = info->chunk_size +
  698. info->oob_size;
  699. /*
  700. * This is the command dispatch that completes a chunked
  701. * page program operation.
  702. */
  703. if (info->data_size == 0) {
  704. info->ndcb0 = NDCB0_CMD_TYPE(0x1)
  705. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  706. | command;
  707. info->ndcb1 = 0;
  708. info->ndcb2 = 0;
  709. info->ndcb3 = 0;
  710. }
  711. } else {
  712. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  713. | NDCB0_AUTO_RS
  714. | NDCB0_ST_ROW_EN
  715. | NDCB0_DBC
  716. | (NAND_CMD_PAGEPROG << 8)
  717. | NAND_CMD_SEQIN
  718. | addr_cycle;
  719. }
  720. break;
  721. case NAND_CMD_PARAM:
  722. info->buf_count = 256;
  723. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  724. | NDCB0_ADDR_CYC(1)
  725. | NDCB0_LEN_OVRD
  726. | command;
  727. info->ndcb1 = (column & 0xFF);
  728. info->ndcb3 = 256;
  729. info->data_size = 256;
  730. break;
  731. case NAND_CMD_READID:
  732. info->buf_count = host->read_id_bytes;
  733. info->ndcb0 |= NDCB0_CMD_TYPE(3)
  734. | NDCB0_ADDR_CYC(1)
  735. | command;
  736. info->ndcb1 = (column & 0xFF);
  737. info->data_size = 8;
  738. break;
  739. case NAND_CMD_STATUS:
  740. info->buf_count = 1;
  741. info->ndcb0 |= NDCB0_CMD_TYPE(4)
  742. | NDCB0_ADDR_CYC(1)
  743. | command;
  744. info->data_size = 8;
  745. break;
  746. case NAND_CMD_ERASE1:
  747. info->ndcb0 |= NDCB0_CMD_TYPE(2)
  748. | NDCB0_AUTO_RS
  749. | NDCB0_ADDR_CYC(3)
  750. | NDCB0_DBC
  751. | (NAND_CMD_ERASE2 << 8)
  752. | NAND_CMD_ERASE1;
  753. info->ndcb1 = page_addr;
  754. info->ndcb2 = 0;
  755. break;
  756. case NAND_CMD_RESET:
  757. info->ndcb0 |= NDCB0_CMD_TYPE(5)
  758. | command;
  759. break;
  760. case NAND_CMD_ERASE2:
  761. exec_cmd = 0;
  762. break;
  763. default:
  764. exec_cmd = 0;
  765. dev_err(&info->pdev->dev, "non-supported command %x\n",
  766. command);
  767. break;
  768. }
  769. return exec_cmd;
  770. }
  771. static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  772. int column, int page_addr)
  773. {
  774. struct nand_chip *chip = mtd_to_nand(mtd);
  775. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  776. struct pxa3xx_nand_info *info = host->info_data;
  777. int exec_cmd;
  778. /*
  779. * if this is a x16 device ,then convert the input
  780. * "byte" address into a "word" address appropriate
  781. * for indexing a word-oriented device
  782. */
  783. if (info->reg_ndcr & NDCR_DWIDTH_M)
  784. column /= 2;
  785. /*
  786. * There may be different NAND chip hooked to
  787. * different chip select, so check whether
  788. * chip select has been changed, if yes, reset the timing
  789. */
  790. if (info->cs != host->cs) {
  791. info->cs = host->cs;
  792. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  793. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  794. }
  795. prepare_start_command(info, command);
  796. info->state = STATE_PREPARED;
  797. exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
  798. if (exec_cmd) {
  799. u32 ts;
  800. info->cmd_complete = 0;
  801. info->dev_ready = 0;
  802. info->need_wait = 1;
  803. pxa3xx_nand_start(info);
  804. ts = get_timer(0);
  805. while (1) {
  806. u32 status;
  807. status = nand_readl(info, NDSR);
  808. if (status)
  809. pxa3xx_nand_irq(info);
  810. if (info->cmd_complete)
  811. break;
  812. if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
  813. dev_err(&info->pdev->dev, "Wait timeout!!!\n");
  814. return;
  815. }
  816. }
  817. }
  818. info->state = STATE_IDLE;
  819. }
  820. static void nand_cmdfunc_extended(struct mtd_info *mtd,
  821. const unsigned command,
  822. int column, int page_addr)
  823. {
  824. struct nand_chip *chip = mtd_to_nand(mtd);
  825. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  826. struct pxa3xx_nand_info *info = host->info_data;
  827. int exec_cmd, ext_cmd_type;
  828. /*
  829. * if this is a x16 device then convert the input
  830. * "byte" address into a "word" address appropriate
  831. * for indexing a word-oriented device
  832. */
  833. if (info->reg_ndcr & NDCR_DWIDTH_M)
  834. column /= 2;
  835. /*
  836. * There may be different NAND chip hooked to
  837. * different chip select, so check whether
  838. * chip select has been changed, if yes, reset the timing
  839. */
  840. if (info->cs != host->cs) {
  841. info->cs = host->cs;
  842. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  843. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  844. }
  845. /* Select the extended command for the first command */
  846. switch (command) {
  847. case NAND_CMD_READ0:
  848. case NAND_CMD_READOOB:
  849. ext_cmd_type = EXT_CMD_TYPE_MONO;
  850. break;
  851. case NAND_CMD_SEQIN:
  852. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  853. break;
  854. case NAND_CMD_PAGEPROG:
  855. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  856. break;
  857. default:
  858. ext_cmd_type = 0;
  859. break;
  860. }
  861. prepare_start_command(info, command);
  862. /*
  863. * Prepare the "is ready" completion before starting a command
  864. * transaction sequence. If the command is not executed the
  865. * completion will be completed, see below.
  866. *
  867. * We can do that inside the loop because the command variable
  868. * is invariant and thus so is the exec_cmd.
  869. */
  870. info->need_wait = 1;
  871. info->dev_ready = 0;
  872. do {
  873. u32 ts;
  874. info->state = STATE_PREPARED;
  875. exec_cmd = prepare_set_command(info, command, ext_cmd_type,
  876. column, page_addr);
  877. if (!exec_cmd) {
  878. info->need_wait = 0;
  879. info->dev_ready = 1;
  880. break;
  881. }
  882. info->cmd_complete = 0;
  883. pxa3xx_nand_start(info);
  884. ts = get_timer(0);
  885. while (1) {
  886. u32 status;
  887. status = nand_readl(info, NDSR);
  888. if (status)
  889. pxa3xx_nand_irq(info);
  890. if (info->cmd_complete)
  891. break;
  892. if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
  893. dev_err(&info->pdev->dev, "Wait timeout!!!\n");
  894. return;
  895. }
  896. }
  897. /* Check if the sequence is complete */
  898. if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
  899. break;
  900. /*
  901. * After a splitted program command sequence has issued
  902. * the command dispatch, the command sequence is complete.
  903. */
  904. if (info->data_size == 0 &&
  905. command == NAND_CMD_PAGEPROG &&
  906. ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
  907. break;
  908. if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
  909. /* Last read: issue a 'last naked read' */
  910. if (info->data_size == info->chunk_size)
  911. ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
  912. else
  913. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  914. /*
  915. * If a splitted program command has no more data to transfer,
  916. * the command dispatch must be issued to complete.
  917. */
  918. } else if (command == NAND_CMD_PAGEPROG &&
  919. info->data_size == 0) {
  920. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  921. }
  922. } while (1);
  923. info->state = STATE_IDLE;
  924. }
  925. static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
  926. struct nand_chip *chip, const uint8_t *buf, int oob_required,
  927. int page)
  928. {
  929. chip->write_buf(mtd, buf, mtd->writesize);
  930. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  931. return 0;
  932. }
  933. static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
  934. struct nand_chip *chip, uint8_t *buf, int oob_required,
  935. int page)
  936. {
  937. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  938. struct pxa3xx_nand_info *info = host->info_data;
  939. chip->read_buf(mtd, buf, mtd->writesize);
  940. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  941. if (info->retcode == ERR_CORERR && info->use_ecc) {
  942. mtd->ecc_stats.corrected += info->ecc_err_cnt;
  943. } else if (info->retcode == ERR_UNCORERR) {
  944. /*
  945. * for blank page (all 0xff), HW will calculate its ECC as
  946. * 0, which is different from the ECC information within
  947. * OOB, ignore such uncorrectable errors
  948. */
  949. if (is_buf_blank(buf, mtd->writesize))
  950. info->retcode = ERR_NONE;
  951. else
  952. mtd->ecc_stats.failed++;
  953. }
  954. return info->max_bitflips;
  955. }
  956. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  957. {
  958. struct nand_chip *chip = mtd_to_nand(mtd);
  959. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  960. struct pxa3xx_nand_info *info = host->info_data;
  961. char retval = 0xFF;
  962. if (info->buf_start < info->buf_count)
  963. /* Has just send a new command? */
  964. retval = info->data_buff[info->buf_start++];
  965. return retval;
  966. }
  967. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  968. {
  969. struct nand_chip *chip = mtd_to_nand(mtd);
  970. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  971. struct pxa3xx_nand_info *info = host->info_data;
  972. u16 retval = 0xFFFF;
  973. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  974. retval = *((u16 *)(info->data_buff+info->buf_start));
  975. info->buf_start += 2;
  976. }
  977. return retval;
  978. }
  979. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  980. {
  981. struct nand_chip *chip = mtd_to_nand(mtd);
  982. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  983. struct pxa3xx_nand_info *info = host->info_data;
  984. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  985. memcpy(buf, info->data_buff + info->buf_start, real_len);
  986. info->buf_start += real_len;
  987. }
  988. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  989. const uint8_t *buf, int len)
  990. {
  991. struct nand_chip *chip = mtd_to_nand(mtd);
  992. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  993. struct pxa3xx_nand_info *info = host->info_data;
  994. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  995. memcpy(info->data_buff + info->buf_start, buf, real_len);
  996. info->buf_start += real_len;
  997. }
  998. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  999. {
  1000. return;
  1001. }
  1002. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  1003. {
  1004. struct nand_chip *chip = mtd_to_nand(mtd);
  1005. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1006. struct pxa3xx_nand_info *info = host->info_data;
  1007. if (info->need_wait) {
  1008. u32 ts;
  1009. info->need_wait = 0;
  1010. ts = get_timer(0);
  1011. while (1) {
  1012. u32 status;
  1013. status = nand_readl(info, NDSR);
  1014. if (status)
  1015. pxa3xx_nand_irq(info);
  1016. if (info->dev_ready)
  1017. break;
  1018. if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
  1019. dev_err(&info->pdev->dev, "Ready timeout!!!\n");
  1020. return NAND_STATUS_FAIL;
  1021. }
  1022. }
  1023. }
  1024. /* pxa3xx_nand_send_command has waited for command complete */
  1025. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  1026. if (info->retcode == ERR_NONE)
  1027. return 0;
  1028. else
  1029. return NAND_STATUS_FAIL;
  1030. }
  1031. return NAND_STATUS_READY;
  1032. }
  1033. static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info)
  1034. {
  1035. struct pxa3xx_nand_host *host = info->host[info->cs];
  1036. struct mtd_info *mtd = host->mtd;
  1037. struct nand_chip *chip = mtd_to_nand(mtd);
  1038. info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  1039. info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0;
  1040. info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0;
  1041. return 0;
  1042. }
  1043. static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  1044. {
  1045. /*
  1046. * We set 0 by hard coding here, for we don't support keep_config
  1047. * when there is more than one chip attached to the controller
  1048. */
  1049. struct pxa3xx_nand_host *host = info->host[0];
  1050. uint32_t ndcr = nand_readl(info, NDCR);
  1051. if (ndcr & NDCR_PAGE_SZ) {
  1052. /* Controller's FIFO size */
  1053. info->chunk_size = 2048;
  1054. host->read_id_bytes = 4;
  1055. } else {
  1056. info->chunk_size = 512;
  1057. host->read_id_bytes = 2;
  1058. }
  1059. /* Set an initial chunk size */
  1060. info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
  1061. info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
  1062. info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
  1063. return 0;
  1064. }
  1065. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  1066. {
  1067. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1068. if (info->data_buff == NULL)
  1069. return -ENOMEM;
  1070. return 0;
  1071. }
  1072. static int pxa3xx_nand_sensing(struct pxa3xx_nand_host *host)
  1073. {
  1074. struct pxa3xx_nand_info *info = host->info_data;
  1075. struct pxa3xx_nand_platform_data *pdata = info->pdata;
  1076. struct mtd_info *mtd;
  1077. struct nand_chip *chip;
  1078. const struct nand_sdr_timings *timings;
  1079. int ret;
  1080. mtd = info->host[info->cs]->mtd;
  1081. chip = mtd_to_nand(mtd);
  1082. /* configure default flash values */
  1083. info->reg_ndcr = 0x0; /* enable all interrupts */
  1084. info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  1085. info->reg_ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
  1086. info->reg_ndcr |= NDCR_SPARE_EN; /* enable spare by default */
  1087. /* use the common timing to make a try */
  1088. timings = onfi_async_timing_mode_to_sdr_timings(0);
  1089. if (IS_ERR(timings))
  1090. return PTR_ERR(timings);
  1091. pxa3xx_nand_set_sdr_timing(host, timings);
  1092. chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
  1093. ret = chip->waitfunc(mtd, chip);
  1094. if (ret & NAND_STATUS_FAIL)
  1095. return -ENODEV;
  1096. return 0;
  1097. }
  1098. static int pxa_ecc_init(struct pxa3xx_nand_info *info,
  1099. struct nand_ecc_ctrl *ecc,
  1100. int strength, int ecc_stepsize, int page_size)
  1101. {
  1102. if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
  1103. info->chunk_size = 2048;
  1104. info->spare_size = 40;
  1105. info->ecc_size = 24;
  1106. ecc->mode = NAND_ECC_HW;
  1107. ecc->size = 512;
  1108. ecc->strength = 1;
  1109. } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
  1110. info->chunk_size = 512;
  1111. info->spare_size = 8;
  1112. info->ecc_size = 8;
  1113. ecc->mode = NAND_ECC_HW;
  1114. ecc->size = 512;
  1115. ecc->strength = 1;
  1116. /*
  1117. * Required ECC: 4-bit correction per 512 bytes
  1118. * Select: 16-bit correction per 2048 bytes
  1119. */
  1120. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
  1121. info->ecc_bch = 1;
  1122. info->chunk_size = 2048;
  1123. info->spare_size = 32;
  1124. info->ecc_size = 32;
  1125. ecc->mode = NAND_ECC_HW;
  1126. ecc->size = info->chunk_size;
  1127. ecc->layout = &ecc_layout_2KB_bch4bit;
  1128. ecc->strength = 16;
  1129. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
  1130. info->ecc_bch = 1;
  1131. info->chunk_size = 2048;
  1132. info->spare_size = 32;
  1133. info->ecc_size = 32;
  1134. ecc->mode = NAND_ECC_HW;
  1135. ecc->size = info->chunk_size;
  1136. ecc->layout = &ecc_layout_4KB_bch4bit;
  1137. ecc->strength = 16;
  1138. /*
  1139. * Required ECC: 8-bit correction per 512 bytes
  1140. * Select: 16-bit correction per 1024 bytes
  1141. */
  1142. } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
  1143. info->ecc_bch = 1;
  1144. info->chunk_size = 1024;
  1145. info->spare_size = 0;
  1146. info->ecc_size = 32;
  1147. ecc->mode = NAND_ECC_HW;
  1148. ecc->size = info->chunk_size;
  1149. ecc->layout = &ecc_layout_4KB_bch8bit;
  1150. ecc->strength = 16;
  1151. } else {
  1152. dev_err(&info->pdev->dev,
  1153. "ECC strength %d at page size %d is not supported\n",
  1154. strength, page_size);
  1155. return -ENODEV;
  1156. }
  1157. return 0;
  1158. }
  1159. static int pxa3xx_nand_scan(struct mtd_info *mtd)
  1160. {
  1161. struct nand_chip *chip = mtd_to_nand(mtd);
  1162. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1163. struct pxa3xx_nand_info *info = host->info_data;
  1164. struct pxa3xx_nand_platform_data *pdata = info->pdata;
  1165. int ret;
  1166. uint16_t ecc_strength, ecc_step;
  1167. if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
  1168. goto KEEP_CONFIG;
  1169. /* Set a default chunk size */
  1170. info->chunk_size = 512;
  1171. ret = pxa3xx_nand_sensing(host);
  1172. if (ret) {
  1173. dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
  1174. info->cs);
  1175. return ret;
  1176. }
  1177. KEEP_CONFIG:
  1178. /* Device detection must be done with ECC disabled */
  1179. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  1180. nand_writel(info, NDECCCTRL, 0x0);
  1181. if (nand_scan_ident(mtd, 1, NULL))
  1182. return -ENODEV;
  1183. if (!pdata->keep_config) {
  1184. ret = pxa3xx_nand_init_timings(host);
  1185. if (ret) {
  1186. dev_err(&info->pdev->dev,
  1187. "Failed to set timings: %d\n", ret);
  1188. return ret;
  1189. }
  1190. }
  1191. ret = pxa3xx_nand_config_flash(info);
  1192. if (ret)
  1193. return ret;
  1194. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  1195. /*
  1196. * We'll use a bad block table stored in-flash and don't
  1197. * allow writing the bad block marker to the flash.
  1198. */
  1199. chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB_BBM;
  1200. chip->bbt_td = &bbt_main_descr;
  1201. chip->bbt_md = &bbt_mirror_descr;
  1202. #endif
  1203. /*
  1204. * If the page size is bigger than the FIFO size, let's check
  1205. * we are given the right variant and then switch to the extended
  1206. * (aka splitted) command handling,
  1207. */
  1208. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  1209. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
  1210. chip->cmdfunc = nand_cmdfunc_extended;
  1211. } else {
  1212. dev_err(&info->pdev->dev,
  1213. "unsupported page size on this variant\n");
  1214. return -ENODEV;
  1215. }
  1216. }
  1217. if (pdata->ecc_strength && pdata->ecc_step_size) {
  1218. ecc_strength = pdata->ecc_strength;
  1219. ecc_step = pdata->ecc_step_size;
  1220. } else {
  1221. ecc_strength = chip->ecc_strength_ds;
  1222. ecc_step = chip->ecc_step_ds;
  1223. }
  1224. /* Set default ECC strength requirements on non-ONFI devices */
  1225. if (ecc_strength < 1 && ecc_step < 1) {
  1226. ecc_strength = 1;
  1227. ecc_step = 512;
  1228. }
  1229. ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
  1230. ecc_step, mtd->writesize);
  1231. if (ret)
  1232. return ret;
  1233. /* calculate addressing information */
  1234. if (mtd->writesize >= 2048)
  1235. host->col_addr_cycles = 2;
  1236. else
  1237. host->col_addr_cycles = 1;
  1238. /* release the initial buffer */
  1239. kfree(info->data_buff);
  1240. /* allocate the real data + oob buffer */
  1241. info->buf_size = mtd->writesize + mtd->oobsize;
  1242. ret = pxa3xx_nand_init_buff(info);
  1243. if (ret)
  1244. return ret;
  1245. info->oob_buff = info->data_buff + mtd->writesize;
  1246. if ((mtd->size >> chip->page_shift) > 65536)
  1247. host->row_addr_cycles = 3;
  1248. else
  1249. host->row_addr_cycles = 2;
  1250. return nand_scan_tail(mtd);
  1251. }
  1252. static int alloc_nand_resource(struct pxa3xx_nand_info *info)
  1253. {
  1254. struct pxa3xx_nand_platform_data *pdata;
  1255. struct pxa3xx_nand_host *host;
  1256. struct nand_chip *chip = NULL;
  1257. struct mtd_info *mtd;
  1258. int ret, cs;
  1259. pdata = info->pdata;
  1260. if (pdata->num_cs <= 0)
  1261. return -ENODEV;
  1262. info->variant = pxa3xx_nand_get_variant();
  1263. for (cs = 0; cs < pdata->num_cs; cs++) {
  1264. chip = (struct nand_chip *)
  1265. ((u8 *)&info[1] + sizeof(*host) * cs);
  1266. mtd = nand_to_mtd(chip);
  1267. host = (struct pxa3xx_nand_host *)chip;
  1268. info->host[cs] = host;
  1269. host->mtd = mtd;
  1270. host->cs = cs;
  1271. host->info_data = info;
  1272. host->read_id_bytes = 4;
  1273. mtd->owner = THIS_MODULE;
  1274. nand_set_controller_data(chip, host);
  1275. chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
  1276. chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
  1277. chip->controller = &info->controller;
  1278. chip->waitfunc = pxa3xx_nand_waitfunc;
  1279. chip->select_chip = pxa3xx_nand_select_chip;
  1280. chip->read_word = pxa3xx_nand_read_word;
  1281. chip->read_byte = pxa3xx_nand_read_byte;
  1282. chip->read_buf = pxa3xx_nand_read_buf;
  1283. chip->write_buf = pxa3xx_nand_write_buf;
  1284. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1285. chip->cmdfunc = nand_cmdfunc;
  1286. }
  1287. /* Allocate a buffer to allow flash detection */
  1288. info->buf_size = INIT_BUFFER_SIZE;
  1289. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1290. if (info->data_buff == NULL) {
  1291. ret = -ENOMEM;
  1292. goto fail_disable_clk;
  1293. }
  1294. /* initialize all interrupts to be disabled */
  1295. disable_int(info, NDSR_MASK);
  1296. return 0;
  1297. kfree(info->data_buff);
  1298. fail_disable_clk:
  1299. return ret;
  1300. }
  1301. static int pxa3xx_nand_probe_dt(struct pxa3xx_nand_info *info)
  1302. {
  1303. struct pxa3xx_nand_platform_data *pdata;
  1304. const void *blob = gd->fdt_blob;
  1305. int node = -1;
  1306. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  1307. if (!pdata)
  1308. return -ENOMEM;
  1309. /* Get address decoding nodes from the FDT blob */
  1310. do {
  1311. node = fdt_node_offset_by_compatible(blob, node,
  1312. "marvell,mvebu-pxa3xx-nand");
  1313. if (node < 0)
  1314. break;
  1315. /* Bypass disabeld nodes */
  1316. if (!fdtdec_get_is_enabled(blob, node))
  1317. continue;
  1318. /* Get the first enabled NAND controler base address */
  1319. info->mmio_base =
  1320. (void __iomem *)fdtdec_get_addr_size_auto_noparent(
  1321. blob, node, "reg", 0, NULL, true);
  1322. pdata->num_cs = fdtdec_get_int(blob, node, "num-cs", 1);
  1323. if (pdata->num_cs != 1) {
  1324. pr_err("pxa3xx driver supports single CS only\n");
  1325. break;
  1326. }
  1327. if (fdtdec_get_bool(blob, node, "nand-enable-arbiter"))
  1328. pdata->enable_arbiter = 1;
  1329. if (fdtdec_get_bool(blob, node, "nand-keep-config"))
  1330. pdata->keep_config = 1;
  1331. /*
  1332. * ECC parameters.
  1333. * If these are not set, they will be selected according
  1334. * to the detected flash type.
  1335. */
  1336. /* ECC strength */
  1337. pdata->ecc_strength = fdtdec_get_int(blob, node,
  1338. "nand-ecc-strength", 0);
  1339. /* ECC step size */
  1340. pdata->ecc_step_size = fdtdec_get_int(blob, node,
  1341. "nand-ecc-step-size", 0);
  1342. info->pdata = pdata;
  1343. /* Currently support only a single NAND controller */
  1344. return 0;
  1345. } while (node >= 0);
  1346. return -EINVAL;
  1347. }
  1348. static int pxa3xx_nand_probe(struct pxa3xx_nand_info *info)
  1349. {
  1350. struct pxa3xx_nand_platform_data *pdata;
  1351. int ret, cs, probe_success;
  1352. ret = pxa3xx_nand_probe_dt(info);
  1353. if (ret)
  1354. return ret;
  1355. pdata = info->pdata;
  1356. ret = alloc_nand_resource(info);
  1357. if (ret) {
  1358. dev_err(&pdev->dev, "alloc nand resource failed\n");
  1359. return ret;
  1360. }
  1361. probe_success = 0;
  1362. for (cs = 0; cs < pdata->num_cs; cs++) {
  1363. struct mtd_info *mtd = info->host[cs]->mtd;
  1364. /*
  1365. * The mtd name matches the one used in 'mtdparts' kernel
  1366. * parameter. This name cannot be changed or otherwise
  1367. * user's mtd partitions configuration would get broken.
  1368. */
  1369. mtd->name = "pxa3xx_nand-0";
  1370. info->cs = cs;
  1371. ret = pxa3xx_nand_scan(mtd);
  1372. if (ret) {
  1373. dev_info(&pdev->dev, "failed to scan nand at cs %d\n",
  1374. cs);
  1375. continue;
  1376. }
  1377. if (nand_register(cs, mtd))
  1378. continue;
  1379. probe_success = 1;
  1380. }
  1381. if (!probe_success)
  1382. return -ENODEV;
  1383. return 0;
  1384. }
  1385. /*
  1386. * Main initialization routine
  1387. */
  1388. void board_nand_init(void)
  1389. {
  1390. struct pxa3xx_nand_info *info;
  1391. struct pxa3xx_nand_host *host;
  1392. int ret;
  1393. info = kzalloc(sizeof(*info) +
  1394. sizeof(*host) * CONFIG_SYS_MAX_NAND_DEVICE,
  1395. GFP_KERNEL);
  1396. if (!info)
  1397. return;
  1398. ret = pxa3xx_nand_probe(info);
  1399. if (ret)
  1400. return;
  1401. }