pxa3xx_nand.c 49 KB

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