pxa3xx_nand.c 41 KB

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