pxa3xx_nand.c 42 KB

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