pxa3xx_nand.c 49 KB

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