pxa3xx_nand.c 49 KB

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