pxa3xx_nand.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/delay.h>
  17. #include <linux/clk.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/io.h>
  22. #include <linux/irq.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <mach/dma.h>
  27. #include <linux/platform_data/mtd-nand-pxa3xx.h>
  28. #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
  29. #define NAND_STOP_DELAY (2 * HZ/50)
  30. #define PAGE_CHUNK_SIZE (2048)
  31. /* registers and bit definitions */
  32. #define NDCR (0x00) /* Control register */
  33. #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
  34. #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
  35. #define NDSR (0x14) /* Status Register */
  36. #define NDPCR (0x18) /* Page Count Register */
  37. #define NDBDR0 (0x1C) /* Bad Block Register 0 */
  38. #define NDBDR1 (0x20) /* Bad Block Register 1 */
  39. #define NDDB (0x40) /* Data Buffer */
  40. #define NDCB0 (0x48) /* Command Buffer0 */
  41. #define NDCB1 (0x4C) /* Command Buffer1 */
  42. #define NDCB2 (0x50) /* Command Buffer2 */
  43. #define NDCR_SPARE_EN (0x1 << 31)
  44. #define NDCR_ECC_EN (0x1 << 30)
  45. #define NDCR_DMA_EN (0x1 << 29)
  46. #define NDCR_ND_RUN (0x1 << 28)
  47. #define NDCR_DWIDTH_C (0x1 << 27)
  48. #define NDCR_DWIDTH_M (0x1 << 26)
  49. #define NDCR_PAGE_SZ (0x1 << 24)
  50. #define NDCR_NCSX (0x1 << 23)
  51. #define NDCR_ND_MODE (0x3 << 21)
  52. #define NDCR_NAND_MODE (0x0)
  53. #define NDCR_CLR_PG_CNT (0x1 << 20)
  54. #define NDCR_STOP_ON_UNCOR (0x1 << 19)
  55. #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
  56. #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
  57. #define NDCR_RA_START (0x1 << 15)
  58. #define NDCR_PG_PER_BLK (0x1 << 14)
  59. #define NDCR_ND_ARB_EN (0x1 << 12)
  60. #define NDCR_INT_MASK (0xFFF)
  61. #define NDSR_MASK (0xfff)
  62. #define NDSR_RDY (0x1 << 12)
  63. #define NDSR_FLASH_RDY (0x1 << 11)
  64. #define NDSR_CS0_PAGED (0x1 << 10)
  65. #define NDSR_CS1_PAGED (0x1 << 9)
  66. #define NDSR_CS0_CMDD (0x1 << 8)
  67. #define NDSR_CS1_CMDD (0x1 << 7)
  68. #define NDSR_CS0_BBD (0x1 << 6)
  69. #define NDSR_CS1_BBD (0x1 << 5)
  70. #define NDSR_DBERR (0x1 << 4)
  71. #define NDSR_SBERR (0x1 << 3)
  72. #define NDSR_WRDREQ (0x1 << 2)
  73. #define NDSR_RDDREQ (0x1 << 1)
  74. #define NDSR_WRCMDREQ (0x1)
  75. #define NDCB0_LEN_OVRD (0x1 << 28)
  76. #define NDCB0_ST_ROW_EN (0x1 << 26)
  77. #define NDCB0_AUTO_RS (0x1 << 25)
  78. #define NDCB0_CSEL (0x1 << 24)
  79. #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
  80. #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
  81. #define NDCB0_NC (0x1 << 20)
  82. #define NDCB0_DBC (0x1 << 19)
  83. #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
  84. #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
  85. #define NDCB0_CMD2_MASK (0xff << 8)
  86. #define NDCB0_CMD1_MASK (0xff)
  87. #define NDCB0_ADDR_CYC_SHIFT (16)
  88. /* macros for registers read/write */
  89. #define nand_writel(info, off, val) \
  90. __raw_writel((val), (info)->mmio_base + (off))
  91. #define nand_readl(info, off) \
  92. __raw_readl((info)->mmio_base + (off))
  93. /* error code and state */
  94. enum {
  95. ERR_NONE = 0,
  96. ERR_DMABUSERR = -1,
  97. ERR_SENDCMD = -2,
  98. ERR_DBERR = -3,
  99. ERR_BBERR = -4,
  100. ERR_SBERR = -5,
  101. };
  102. enum {
  103. STATE_IDLE = 0,
  104. STATE_PREPARED,
  105. STATE_CMD_HANDLE,
  106. STATE_DMA_READING,
  107. STATE_DMA_WRITING,
  108. STATE_DMA_DONE,
  109. STATE_PIO_READING,
  110. STATE_PIO_WRITING,
  111. STATE_CMD_DONE,
  112. STATE_READY,
  113. };
  114. enum pxa3xx_nand_variant {
  115. PXA3XX_NAND_VARIANT_PXA,
  116. PXA3XX_NAND_VARIANT_ARMADA370,
  117. };
  118. struct pxa3xx_nand_host {
  119. struct nand_chip chip;
  120. struct pxa3xx_nand_cmdset *cmdset;
  121. struct mtd_info *mtd;
  122. void *info_data;
  123. /* page size of attached chip */
  124. unsigned int page_size;
  125. int use_ecc;
  126. int cs;
  127. /* calculated from pxa3xx_nand_flash data */
  128. unsigned int col_addr_cycles;
  129. unsigned int row_addr_cycles;
  130. size_t read_id_bytes;
  131. /* cached register value */
  132. uint32_t reg_ndcr;
  133. uint32_t ndtr0cs0;
  134. uint32_t ndtr1cs0;
  135. };
  136. struct pxa3xx_nand_info {
  137. struct nand_hw_control controller;
  138. struct platform_device *pdev;
  139. struct clk *clk;
  140. void __iomem *mmio_base;
  141. unsigned long mmio_phys;
  142. struct completion cmd_complete;
  143. unsigned int buf_start;
  144. unsigned int buf_count;
  145. /* DMA information */
  146. int drcmr_dat;
  147. int drcmr_cmd;
  148. unsigned char *data_buff;
  149. unsigned char *oob_buff;
  150. dma_addr_t data_buff_phys;
  151. int data_dma_ch;
  152. struct pxa_dma_desc *data_desc;
  153. dma_addr_t data_desc_addr;
  154. struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
  155. unsigned int state;
  156. /*
  157. * This driver supports NFCv1 (as found in PXA SoC)
  158. * and NFCv2 (as found in Armada 370/XP SoC).
  159. */
  160. enum pxa3xx_nand_variant variant;
  161. int cs;
  162. int use_ecc; /* use HW ECC ? */
  163. int use_dma; /* use DMA ? */
  164. int use_spare; /* use spare ? */
  165. int is_ready;
  166. unsigned int page_size; /* page size of attached chip */
  167. unsigned int data_size; /* data size in FIFO */
  168. unsigned int oob_size;
  169. int retcode;
  170. /* generated NDCBx register values */
  171. uint32_t ndcb0;
  172. uint32_t ndcb1;
  173. uint32_t ndcb2;
  174. uint32_t ndcb3;
  175. };
  176. static bool use_dma = 1;
  177. module_param(use_dma, bool, 0444);
  178. MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
  179. /*
  180. * Default NAND flash controller configuration setup by the
  181. * bootloader. This configuration is used only when pdata->keep_config is set
  182. */
  183. static struct pxa3xx_nand_cmdset default_cmdset = {
  184. .read1 = 0x3000,
  185. .read2 = 0x0050,
  186. .program = 0x1080,
  187. .read_status = 0x0070,
  188. .read_id = 0x0090,
  189. .erase = 0xD060,
  190. .reset = 0x00FF,
  191. .lock = 0x002A,
  192. .unlock = 0x2423,
  193. .lock_status = 0x007A,
  194. };
  195. static struct pxa3xx_nand_timing timing[] = {
  196. { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
  197. { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
  198. { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
  199. { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
  200. };
  201. static struct pxa3xx_nand_flash builtin_flash_types[] = {
  202. { "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
  203. { "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
  204. { "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
  205. { "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
  206. { "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
  207. { "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
  208. { "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
  209. { "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
  210. { "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
  211. };
  212. /* Define a default flash type setting serve as flash detecting only */
  213. #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
  214. #define NDTR0_tCH(c) (min((c), 7) << 19)
  215. #define NDTR0_tCS(c) (min((c), 7) << 16)
  216. #define NDTR0_tWH(c) (min((c), 7) << 11)
  217. #define NDTR0_tWP(c) (min((c), 7) << 8)
  218. #define NDTR0_tRH(c) (min((c), 7) << 3)
  219. #define NDTR0_tRP(c) (min((c), 7) << 0)
  220. #define NDTR1_tR(c) (min((c), 65535) << 16)
  221. #define NDTR1_tWHR(c) (min((c), 15) << 4)
  222. #define NDTR1_tAR(c) (min((c), 15) << 0)
  223. /* convert nano-seconds to nand flash controller clock cycles */
  224. #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
  225. static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
  226. const struct pxa3xx_nand_timing *t)
  227. {
  228. struct pxa3xx_nand_info *info = host->info_data;
  229. unsigned long nand_clk = clk_get_rate(info->clk);
  230. uint32_t ndtr0, ndtr1;
  231. ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
  232. NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
  233. NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
  234. NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
  235. NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
  236. NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
  237. ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
  238. NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
  239. NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
  240. host->ndtr0cs0 = ndtr0;
  241. host->ndtr1cs0 = ndtr1;
  242. nand_writel(info, NDTR0CS0, ndtr0);
  243. nand_writel(info, NDTR1CS0, ndtr1);
  244. }
  245. static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
  246. {
  247. struct pxa3xx_nand_host *host = info->host[info->cs];
  248. int oob_enable = host->reg_ndcr & NDCR_SPARE_EN;
  249. info->data_size = host->page_size;
  250. if (!oob_enable) {
  251. info->oob_size = 0;
  252. return;
  253. }
  254. switch (host->page_size) {
  255. case 2048:
  256. info->oob_size = (info->use_ecc) ? 40 : 64;
  257. break;
  258. case 512:
  259. info->oob_size = (info->use_ecc) ? 8 : 16;
  260. break;
  261. }
  262. }
  263. /**
  264. * NOTE: it is a must to set ND_RUN firstly, then write
  265. * command buffer, otherwise, it does not work.
  266. * We enable all the interrupt at the same time, and
  267. * let pxa3xx_nand_irq to handle all logic.
  268. */
  269. static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
  270. {
  271. struct pxa3xx_nand_host *host = info->host[info->cs];
  272. uint32_t ndcr;
  273. ndcr = host->reg_ndcr;
  274. if (info->use_ecc)
  275. ndcr |= NDCR_ECC_EN;
  276. else
  277. ndcr &= ~NDCR_ECC_EN;
  278. if (info->use_dma)
  279. ndcr |= NDCR_DMA_EN;
  280. else
  281. ndcr &= ~NDCR_DMA_EN;
  282. if (info->use_spare)
  283. ndcr |= NDCR_SPARE_EN;
  284. else
  285. ndcr &= ~NDCR_SPARE_EN;
  286. ndcr |= NDCR_ND_RUN;
  287. /* clear status bits and run */
  288. nand_writel(info, NDCR, 0);
  289. nand_writel(info, NDSR, NDSR_MASK);
  290. nand_writel(info, NDCR, ndcr);
  291. }
  292. static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
  293. {
  294. uint32_t ndcr;
  295. int timeout = NAND_STOP_DELAY;
  296. /* wait RUN bit in NDCR become 0 */
  297. ndcr = nand_readl(info, NDCR);
  298. while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
  299. ndcr = nand_readl(info, NDCR);
  300. udelay(1);
  301. }
  302. if (timeout <= 0) {
  303. ndcr &= ~NDCR_ND_RUN;
  304. nand_writel(info, NDCR, ndcr);
  305. }
  306. /* clear status bits */
  307. nand_writel(info, NDSR, NDSR_MASK);
  308. }
  309. static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  310. {
  311. uint32_t ndcr;
  312. ndcr = nand_readl(info, NDCR);
  313. nand_writel(info, NDCR, ndcr & ~int_mask);
  314. }
  315. static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  316. {
  317. uint32_t ndcr;
  318. ndcr = nand_readl(info, NDCR);
  319. nand_writel(info, NDCR, ndcr | int_mask);
  320. }
  321. static void handle_data_pio(struct pxa3xx_nand_info *info)
  322. {
  323. switch (info->state) {
  324. case STATE_PIO_WRITING:
  325. __raw_writesl(info->mmio_base + NDDB, info->data_buff,
  326. DIV_ROUND_UP(info->data_size, 4));
  327. if (info->oob_size > 0)
  328. __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
  329. DIV_ROUND_UP(info->oob_size, 4));
  330. break;
  331. case STATE_PIO_READING:
  332. __raw_readsl(info->mmio_base + NDDB, info->data_buff,
  333. DIV_ROUND_UP(info->data_size, 4));
  334. if (info->oob_size > 0)
  335. __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
  336. DIV_ROUND_UP(info->oob_size, 4));
  337. break;
  338. default:
  339. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  340. info->state);
  341. BUG();
  342. }
  343. }
  344. static void start_data_dma(struct pxa3xx_nand_info *info)
  345. {
  346. struct pxa_dma_desc *desc = info->data_desc;
  347. int dma_len = ALIGN(info->data_size + info->oob_size, 32);
  348. desc->ddadr = DDADR_STOP;
  349. desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
  350. switch (info->state) {
  351. case STATE_DMA_WRITING:
  352. desc->dsadr = info->data_buff_phys;
  353. desc->dtadr = info->mmio_phys + NDDB;
  354. desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
  355. break;
  356. case STATE_DMA_READING:
  357. desc->dtadr = info->data_buff_phys;
  358. desc->dsadr = info->mmio_phys + NDDB;
  359. desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
  360. break;
  361. default:
  362. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  363. info->state);
  364. BUG();
  365. }
  366. DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
  367. DDADR(info->data_dma_ch) = info->data_desc_addr;
  368. DCSR(info->data_dma_ch) |= DCSR_RUN;
  369. }
  370. static void pxa3xx_nand_data_dma_irq(int channel, void *data)
  371. {
  372. struct pxa3xx_nand_info *info = data;
  373. uint32_t dcsr;
  374. dcsr = DCSR(channel);
  375. DCSR(channel) = dcsr;
  376. if (dcsr & DCSR_BUSERR) {
  377. info->retcode = ERR_DMABUSERR;
  378. }
  379. info->state = STATE_DMA_DONE;
  380. enable_int(info, NDCR_INT_MASK);
  381. nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
  382. }
  383. static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
  384. {
  385. struct pxa3xx_nand_info *info = devid;
  386. unsigned int status, is_completed = 0;
  387. unsigned int ready, cmd_done;
  388. if (info->cs == 0) {
  389. ready = NDSR_FLASH_RDY;
  390. cmd_done = NDSR_CS0_CMDD;
  391. } else {
  392. ready = NDSR_RDY;
  393. cmd_done = NDSR_CS1_CMDD;
  394. }
  395. status = nand_readl(info, NDSR);
  396. if (status & NDSR_DBERR)
  397. info->retcode = ERR_DBERR;
  398. if (status & NDSR_SBERR)
  399. info->retcode = ERR_SBERR;
  400. if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
  401. /* whether use dma to transfer data */
  402. if (info->use_dma) {
  403. disable_int(info, NDCR_INT_MASK);
  404. info->state = (status & NDSR_RDDREQ) ?
  405. STATE_DMA_READING : STATE_DMA_WRITING;
  406. start_data_dma(info);
  407. goto NORMAL_IRQ_EXIT;
  408. } else {
  409. info->state = (status & NDSR_RDDREQ) ?
  410. STATE_PIO_READING : STATE_PIO_WRITING;
  411. handle_data_pio(info);
  412. }
  413. }
  414. if (status & cmd_done) {
  415. info->state = STATE_CMD_DONE;
  416. is_completed = 1;
  417. }
  418. if (status & ready) {
  419. info->is_ready = 1;
  420. info->state = STATE_READY;
  421. }
  422. if (status & NDSR_WRCMDREQ) {
  423. nand_writel(info, NDSR, NDSR_WRCMDREQ);
  424. status &= ~NDSR_WRCMDREQ;
  425. info->state = STATE_CMD_HANDLE;
  426. /*
  427. * Command buffer registers NDCB{0-2} (and optionally NDCB3)
  428. * must be loaded by writing directly either 12 or 16
  429. * bytes directly to NDCB0, four bytes at a time.
  430. *
  431. * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
  432. * but each NDCBx register can be read.
  433. */
  434. nand_writel(info, NDCB0, info->ndcb0);
  435. nand_writel(info, NDCB0, info->ndcb1);
  436. nand_writel(info, NDCB0, info->ndcb2);
  437. /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
  438. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  439. nand_writel(info, NDCB0, info->ndcb3);
  440. }
  441. /* clear NDSR to let the controller exit the IRQ */
  442. nand_writel(info, NDSR, status);
  443. if (is_completed)
  444. complete(&info->cmd_complete);
  445. NORMAL_IRQ_EXIT:
  446. return IRQ_HANDLED;
  447. }
  448. static inline int is_buf_blank(uint8_t *buf, size_t len)
  449. {
  450. for (; len > 0; len--)
  451. if (*buf++ != 0xff)
  452. return 0;
  453. return 1;
  454. }
  455. static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
  456. uint16_t column, int page_addr)
  457. {
  458. uint16_t cmd;
  459. int addr_cycle, exec_cmd;
  460. struct pxa3xx_nand_host *host;
  461. struct mtd_info *mtd;
  462. host = info->host[info->cs];
  463. mtd = host->mtd;
  464. addr_cycle = 0;
  465. exec_cmd = 1;
  466. /* reset data and oob column point to handle data */
  467. info->buf_start = 0;
  468. info->buf_count = 0;
  469. info->oob_size = 0;
  470. info->use_ecc = 0;
  471. info->use_spare = 1;
  472. info->use_dma = (use_dma) ? 1 : 0;
  473. info->is_ready = 0;
  474. info->retcode = ERR_NONE;
  475. if (info->cs != 0)
  476. info->ndcb0 = NDCB0_CSEL;
  477. else
  478. info->ndcb0 = 0;
  479. switch (command) {
  480. case NAND_CMD_READ0:
  481. case NAND_CMD_PAGEPROG:
  482. info->use_ecc = 1;
  483. case NAND_CMD_READOOB:
  484. pxa3xx_set_datasize(info);
  485. break;
  486. case NAND_CMD_PARAM:
  487. info->use_spare = 0;
  488. break;
  489. case NAND_CMD_SEQIN:
  490. exec_cmd = 0;
  491. break;
  492. default:
  493. info->ndcb1 = 0;
  494. info->ndcb2 = 0;
  495. info->ndcb3 = 0;
  496. break;
  497. }
  498. addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
  499. + host->col_addr_cycles);
  500. switch (command) {
  501. case NAND_CMD_READOOB:
  502. case NAND_CMD_READ0:
  503. cmd = host->cmdset->read1;
  504. if (command == NAND_CMD_READOOB)
  505. info->buf_start = mtd->writesize + column;
  506. else
  507. info->buf_start = column;
  508. if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
  509. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  510. | addr_cycle
  511. | (cmd & NDCB0_CMD1_MASK);
  512. else
  513. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  514. | NDCB0_DBC
  515. | addr_cycle
  516. | cmd;
  517. case NAND_CMD_SEQIN:
  518. /* small page addr setting */
  519. if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
  520. info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
  521. | (column & 0xFF);
  522. info->ndcb2 = 0;
  523. } else {
  524. info->ndcb1 = ((page_addr & 0xFFFF) << 16)
  525. | (column & 0xFFFF);
  526. if (page_addr & 0xFF0000)
  527. info->ndcb2 = (page_addr & 0xFF0000) >> 16;
  528. else
  529. info->ndcb2 = 0;
  530. }
  531. info->buf_count = mtd->writesize + mtd->oobsize;
  532. memset(info->data_buff, 0xFF, info->buf_count);
  533. break;
  534. case NAND_CMD_PAGEPROG:
  535. if (is_buf_blank(info->data_buff,
  536. (mtd->writesize + mtd->oobsize))) {
  537. exec_cmd = 0;
  538. break;
  539. }
  540. cmd = host->cmdset->program;
  541. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  542. | NDCB0_AUTO_RS
  543. | NDCB0_ST_ROW_EN
  544. | NDCB0_DBC
  545. | cmd
  546. | addr_cycle;
  547. break;
  548. case NAND_CMD_PARAM:
  549. cmd = NAND_CMD_PARAM;
  550. info->buf_count = 256;
  551. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  552. | NDCB0_ADDR_CYC(1)
  553. | NDCB0_LEN_OVRD
  554. | cmd;
  555. info->ndcb1 = (column & 0xFF);
  556. info->ndcb3 = 256;
  557. info->data_size = 256;
  558. break;
  559. case NAND_CMD_READID:
  560. cmd = host->cmdset->read_id;
  561. info->buf_count = host->read_id_bytes;
  562. info->ndcb0 |= NDCB0_CMD_TYPE(3)
  563. | NDCB0_ADDR_CYC(1)
  564. | cmd;
  565. info->ndcb1 = (column & 0xFF);
  566. info->data_size = 8;
  567. break;
  568. case NAND_CMD_STATUS:
  569. cmd = host->cmdset->read_status;
  570. info->buf_count = 1;
  571. info->ndcb0 |= NDCB0_CMD_TYPE(4)
  572. | NDCB0_ADDR_CYC(1)
  573. | cmd;
  574. info->data_size = 8;
  575. break;
  576. case NAND_CMD_ERASE1:
  577. cmd = host->cmdset->erase;
  578. info->ndcb0 |= NDCB0_CMD_TYPE(2)
  579. | NDCB0_AUTO_RS
  580. | NDCB0_ADDR_CYC(3)
  581. | NDCB0_DBC
  582. | cmd;
  583. info->ndcb1 = page_addr;
  584. info->ndcb2 = 0;
  585. break;
  586. case NAND_CMD_RESET:
  587. cmd = host->cmdset->reset;
  588. info->ndcb0 |= NDCB0_CMD_TYPE(5)
  589. | cmd;
  590. break;
  591. case NAND_CMD_ERASE2:
  592. exec_cmd = 0;
  593. break;
  594. default:
  595. exec_cmd = 0;
  596. dev_err(&info->pdev->dev, "non-supported command %x\n",
  597. command);
  598. break;
  599. }
  600. return exec_cmd;
  601. }
  602. static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  603. int column, int page_addr)
  604. {
  605. struct pxa3xx_nand_host *host = mtd->priv;
  606. struct pxa3xx_nand_info *info = host->info_data;
  607. int ret, exec_cmd;
  608. /*
  609. * if this is a x16 device ,then convert the input
  610. * "byte" address into a "word" address appropriate
  611. * for indexing a word-oriented device
  612. */
  613. if (host->reg_ndcr & NDCR_DWIDTH_M)
  614. column /= 2;
  615. /*
  616. * There may be different NAND chip hooked to
  617. * different chip select, so check whether
  618. * chip select has been changed, if yes, reset the timing
  619. */
  620. if (info->cs != host->cs) {
  621. info->cs = host->cs;
  622. nand_writel(info, NDTR0CS0, host->ndtr0cs0);
  623. nand_writel(info, NDTR1CS0, host->ndtr1cs0);
  624. }
  625. info->state = STATE_PREPARED;
  626. exec_cmd = prepare_command_pool(info, command, column, page_addr);
  627. if (exec_cmd) {
  628. init_completion(&info->cmd_complete);
  629. pxa3xx_nand_start(info);
  630. ret = wait_for_completion_timeout(&info->cmd_complete,
  631. CHIP_DELAY_TIMEOUT);
  632. if (!ret) {
  633. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  634. /* Stop State Machine for next command cycle */
  635. pxa3xx_nand_stop(info);
  636. }
  637. }
  638. info->state = STATE_IDLE;
  639. }
  640. static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
  641. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  642. {
  643. chip->write_buf(mtd, buf, mtd->writesize);
  644. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  645. return 0;
  646. }
  647. static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
  648. struct nand_chip *chip, uint8_t *buf, int oob_required,
  649. int page)
  650. {
  651. struct pxa3xx_nand_host *host = mtd->priv;
  652. struct pxa3xx_nand_info *info = host->info_data;
  653. chip->read_buf(mtd, buf, mtd->writesize);
  654. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  655. if (info->retcode == ERR_SBERR) {
  656. switch (info->use_ecc) {
  657. case 1:
  658. mtd->ecc_stats.corrected++;
  659. break;
  660. case 0:
  661. default:
  662. break;
  663. }
  664. } else if (info->retcode == ERR_DBERR) {
  665. /*
  666. * for blank page (all 0xff), HW will calculate its ECC as
  667. * 0, which is different from the ECC information within
  668. * OOB, ignore such double bit errors
  669. */
  670. if (is_buf_blank(buf, mtd->writesize))
  671. info->retcode = ERR_NONE;
  672. else
  673. mtd->ecc_stats.failed++;
  674. }
  675. return 0;
  676. }
  677. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  678. {
  679. struct pxa3xx_nand_host *host = mtd->priv;
  680. struct pxa3xx_nand_info *info = host->info_data;
  681. char retval = 0xFF;
  682. if (info->buf_start < info->buf_count)
  683. /* Has just send a new command? */
  684. retval = info->data_buff[info->buf_start++];
  685. return retval;
  686. }
  687. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  688. {
  689. struct pxa3xx_nand_host *host = mtd->priv;
  690. struct pxa3xx_nand_info *info = host->info_data;
  691. u16 retval = 0xFFFF;
  692. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  693. retval = *((u16 *)(info->data_buff+info->buf_start));
  694. info->buf_start += 2;
  695. }
  696. return retval;
  697. }
  698. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  699. {
  700. struct pxa3xx_nand_host *host = mtd->priv;
  701. struct pxa3xx_nand_info *info = host->info_data;
  702. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  703. memcpy(buf, info->data_buff + info->buf_start, real_len);
  704. info->buf_start += real_len;
  705. }
  706. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  707. const uint8_t *buf, int len)
  708. {
  709. struct pxa3xx_nand_host *host = mtd->priv;
  710. struct pxa3xx_nand_info *info = host->info_data;
  711. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  712. memcpy(info->data_buff + info->buf_start, buf, real_len);
  713. info->buf_start += real_len;
  714. }
  715. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  716. {
  717. return;
  718. }
  719. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  720. {
  721. struct pxa3xx_nand_host *host = mtd->priv;
  722. struct pxa3xx_nand_info *info = host->info_data;
  723. /* pxa3xx_nand_send_command has waited for command complete */
  724. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  725. if (info->retcode == ERR_NONE)
  726. return 0;
  727. else {
  728. /*
  729. * any error make it return 0x01 which will tell
  730. * the caller the erase and write fail
  731. */
  732. return 0x01;
  733. }
  734. }
  735. return 0;
  736. }
  737. static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
  738. const struct pxa3xx_nand_flash *f)
  739. {
  740. struct platform_device *pdev = info->pdev;
  741. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  742. struct pxa3xx_nand_host *host = info->host[info->cs];
  743. uint32_t ndcr = 0x0; /* enable all interrupts */
  744. if (f->page_size != 2048 && f->page_size != 512) {
  745. dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
  746. return -EINVAL;
  747. }
  748. if (f->flash_width != 16 && f->flash_width != 8) {
  749. dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
  750. return -EINVAL;
  751. }
  752. /* calculate flash information */
  753. host->cmdset = &default_cmdset;
  754. host->page_size = f->page_size;
  755. host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
  756. /* calculate addressing information */
  757. host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
  758. if (f->num_blocks * f->page_per_block > 65536)
  759. host->row_addr_cycles = 3;
  760. else
  761. host->row_addr_cycles = 2;
  762. ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  763. ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  764. ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
  765. ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
  766. ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
  767. ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
  768. ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
  769. ndcr |= NDCR_SPARE_EN; /* enable spare by default */
  770. host->reg_ndcr = ndcr;
  771. pxa3xx_nand_set_timing(host, f->timing);
  772. return 0;
  773. }
  774. static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  775. {
  776. /*
  777. * We set 0 by hard coding here, for we don't support keep_config
  778. * when there is more than one chip attached to the controller
  779. */
  780. struct pxa3xx_nand_host *host = info->host[0];
  781. uint32_t ndcr = nand_readl(info, NDCR);
  782. if (ndcr & NDCR_PAGE_SZ) {
  783. host->page_size = 2048;
  784. host->read_id_bytes = 4;
  785. } else {
  786. host->page_size = 512;
  787. host->read_id_bytes = 2;
  788. }
  789. host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
  790. host->cmdset = &default_cmdset;
  791. host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
  792. host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
  793. return 0;
  794. }
  795. /* the maximum possible buffer size for large page with OOB data
  796. * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
  797. * data buffer and the DMA descriptor
  798. */
  799. #define MAX_BUFF_SIZE PAGE_SIZE
  800. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  801. {
  802. struct platform_device *pdev = info->pdev;
  803. int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
  804. if (use_dma == 0) {
  805. info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
  806. if (info->data_buff == NULL)
  807. return -ENOMEM;
  808. return 0;
  809. }
  810. info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
  811. &info->data_buff_phys, GFP_KERNEL);
  812. if (info->data_buff == NULL) {
  813. dev_err(&pdev->dev, "failed to allocate dma buffer\n");
  814. return -ENOMEM;
  815. }
  816. info->data_desc = (void *)info->data_buff + data_desc_offset;
  817. info->data_desc_addr = info->data_buff_phys + data_desc_offset;
  818. info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
  819. pxa3xx_nand_data_dma_irq, info);
  820. if (info->data_dma_ch < 0) {
  821. dev_err(&pdev->dev, "failed to request data dma\n");
  822. dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
  823. info->data_buff, info->data_buff_phys);
  824. return info->data_dma_ch;
  825. }
  826. return 0;
  827. }
  828. static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
  829. {
  830. struct platform_device *pdev = info->pdev;
  831. if (use_dma) {
  832. pxa_free_dma(info->data_dma_ch);
  833. dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
  834. info->data_buff, info->data_buff_phys);
  835. } else {
  836. kfree(info->data_buff);
  837. }
  838. }
  839. static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
  840. {
  841. struct mtd_info *mtd;
  842. int ret;
  843. mtd = info->host[info->cs]->mtd;
  844. /* use the common timing to make a try */
  845. ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
  846. if (ret)
  847. return ret;
  848. pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
  849. if (info->is_ready)
  850. return 0;
  851. return -ENODEV;
  852. }
  853. static int pxa3xx_nand_scan(struct mtd_info *mtd)
  854. {
  855. struct pxa3xx_nand_host *host = mtd->priv;
  856. struct pxa3xx_nand_info *info = host->info_data;
  857. struct platform_device *pdev = info->pdev;
  858. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  859. struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
  860. const struct pxa3xx_nand_flash *f = NULL;
  861. struct nand_chip *chip = mtd->priv;
  862. uint32_t id = -1;
  863. uint64_t chipsize;
  864. int i, ret, num;
  865. if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
  866. goto KEEP_CONFIG;
  867. ret = pxa3xx_nand_sensing(info);
  868. if (ret) {
  869. dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
  870. info->cs);
  871. return ret;
  872. }
  873. chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
  874. id = *((uint16_t *)(info->data_buff));
  875. if (id != 0)
  876. dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
  877. else {
  878. dev_warn(&info->pdev->dev,
  879. "Read out ID 0, potential timing set wrong!!\n");
  880. return -EINVAL;
  881. }
  882. num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
  883. for (i = 0; i < num; i++) {
  884. if (i < pdata->num_flash)
  885. f = pdata->flash + i;
  886. else
  887. f = &builtin_flash_types[i - pdata->num_flash + 1];
  888. /* find the chip in default list */
  889. if (f->chip_id == id)
  890. break;
  891. }
  892. if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
  893. dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
  894. return -EINVAL;
  895. }
  896. ret = pxa3xx_nand_config_flash(info, f);
  897. if (ret) {
  898. dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
  899. return ret;
  900. }
  901. pxa3xx_flash_ids[0].name = f->name;
  902. pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
  903. pxa3xx_flash_ids[0].pagesize = f->page_size;
  904. chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
  905. pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
  906. pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
  907. if (f->flash_width == 16)
  908. pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
  909. pxa3xx_flash_ids[1].name = NULL;
  910. def = pxa3xx_flash_ids;
  911. KEEP_CONFIG:
  912. chip->ecc.mode = NAND_ECC_HW;
  913. chip->ecc.size = host->page_size;
  914. chip->ecc.strength = 1;
  915. if (host->reg_ndcr & NDCR_DWIDTH_M)
  916. chip->options |= NAND_BUSWIDTH_16;
  917. if (nand_scan_ident(mtd, 1, def))
  918. return -ENODEV;
  919. /* calculate addressing information */
  920. if (mtd->writesize >= 2048)
  921. host->col_addr_cycles = 2;
  922. else
  923. host->col_addr_cycles = 1;
  924. info->oob_buff = info->data_buff + mtd->writesize;
  925. if ((mtd->size >> chip->page_shift) > 65536)
  926. host->row_addr_cycles = 3;
  927. else
  928. host->row_addr_cycles = 2;
  929. return nand_scan_tail(mtd);
  930. }
  931. static int alloc_nand_resource(struct platform_device *pdev)
  932. {
  933. struct pxa3xx_nand_platform_data *pdata;
  934. struct pxa3xx_nand_info *info;
  935. struct pxa3xx_nand_host *host;
  936. struct nand_chip *chip = NULL;
  937. struct mtd_info *mtd;
  938. struct resource *r;
  939. int ret, irq, cs;
  940. pdata = dev_get_platdata(&pdev->dev);
  941. info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
  942. sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
  943. if (!info)
  944. return -ENOMEM;
  945. info->pdev = pdev;
  946. for (cs = 0; cs < pdata->num_cs; cs++) {
  947. mtd = (struct mtd_info *)((unsigned int)&info[1] +
  948. (sizeof(*mtd) + sizeof(*host)) * cs);
  949. chip = (struct nand_chip *)(&mtd[1]);
  950. host = (struct pxa3xx_nand_host *)chip;
  951. info->host[cs] = host;
  952. host->mtd = mtd;
  953. host->cs = cs;
  954. host->info_data = info;
  955. mtd->priv = host;
  956. mtd->owner = THIS_MODULE;
  957. chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
  958. chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
  959. chip->controller = &info->controller;
  960. chip->waitfunc = pxa3xx_nand_waitfunc;
  961. chip->select_chip = pxa3xx_nand_select_chip;
  962. chip->cmdfunc = pxa3xx_nand_cmdfunc;
  963. chip->read_word = pxa3xx_nand_read_word;
  964. chip->read_byte = pxa3xx_nand_read_byte;
  965. chip->read_buf = pxa3xx_nand_read_buf;
  966. chip->write_buf = pxa3xx_nand_write_buf;
  967. }
  968. spin_lock_init(&chip->controller->lock);
  969. init_waitqueue_head(&chip->controller->wq);
  970. info->clk = devm_clk_get(&pdev->dev, NULL);
  971. if (IS_ERR(info->clk)) {
  972. dev_err(&pdev->dev, "failed to get nand clock\n");
  973. return PTR_ERR(info->clk);
  974. }
  975. ret = clk_prepare_enable(info->clk);
  976. if (ret < 0)
  977. return ret;
  978. /*
  979. * This is a dirty hack to make this driver work from devicetree
  980. * bindings. It can be removed once we have a prober DMA controller
  981. * framework for DT.
  982. */
  983. if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
  984. info->drcmr_dat = 97;
  985. info->drcmr_cmd = 99;
  986. } else {
  987. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  988. if (r == NULL) {
  989. dev_err(&pdev->dev, "no resource defined for data DMA\n");
  990. ret = -ENXIO;
  991. goto fail_disable_clk;
  992. }
  993. info->drcmr_dat = r->start;
  994. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  995. if (r == NULL) {
  996. dev_err(&pdev->dev, "no resource defined for command DMA\n");
  997. ret = -ENXIO;
  998. goto fail_disable_clk;
  999. }
  1000. info->drcmr_cmd = r->start;
  1001. }
  1002. irq = platform_get_irq(pdev, 0);
  1003. if (irq < 0) {
  1004. dev_err(&pdev->dev, "no IRQ resource defined\n");
  1005. ret = -ENXIO;
  1006. goto fail_disable_clk;
  1007. }
  1008. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1009. info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  1010. if (IS_ERR(info->mmio_base)) {
  1011. ret = PTR_ERR(info->mmio_base);
  1012. goto fail_disable_clk;
  1013. }
  1014. info->mmio_phys = r->start;
  1015. ret = pxa3xx_nand_init_buff(info);
  1016. if (ret)
  1017. goto fail_disable_clk;
  1018. /* initialize all interrupts to be disabled */
  1019. disable_int(info, NDSR_MASK);
  1020. ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
  1021. pdev->name, info);
  1022. if (ret < 0) {
  1023. dev_err(&pdev->dev, "failed to request IRQ\n");
  1024. goto fail_free_buf;
  1025. }
  1026. platform_set_drvdata(pdev, info);
  1027. return 0;
  1028. fail_free_buf:
  1029. free_irq(irq, info);
  1030. pxa3xx_nand_free_buff(info);
  1031. fail_disable_clk:
  1032. clk_disable_unprepare(info->clk);
  1033. return ret;
  1034. }
  1035. static int pxa3xx_nand_remove(struct platform_device *pdev)
  1036. {
  1037. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1038. struct pxa3xx_nand_platform_data *pdata;
  1039. int irq, cs;
  1040. if (!info)
  1041. return 0;
  1042. pdata = dev_get_platdata(&pdev->dev);
  1043. irq = platform_get_irq(pdev, 0);
  1044. if (irq >= 0)
  1045. free_irq(irq, info);
  1046. pxa3xx_nand_free_buff(info);
  1047. clk_disable_unprepare(info->clk);
  1048. for (cs = 0; cs < pdata->num_cs; cs++)
  1049. nand_release(info->host[cs]->mtd);
  1050. return 0;
  1051. }
  1052. #ifdef CONFIG_OF
  1053. static struct of_device_id pxa3xx_nand_dt_ids[] = {
  1054. {
  1055. .compatible = "marvell,pxa3xx-nand",
  1056. .data = (void *)PXA3XX_NAND_VARIANT_PXA,
  1057. },
  1058. {
  1059. .compatible = "marvell,armada370-nand",
  1060. .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
  1061. },
  1062. {}
  1063. };
  1064. MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
  1065. static enum pxa3xx_nand_variant
  1066. pxa3xx_nand_get_variant(struct platform_device *pdev)
  1067. {
  1068. const struct of_device_id *of_id =
  1069. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  1070. if (!of_id)
  1071. return PXA3XX_NAND_VARIANT_PXA;
  1072. return (enum pxa3xx_nand_variant)of_id->data;
  1073. }
  1074. static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
  1075. {
  1076. struct pxa3xx_nand_platform_data *pdata;
  1077. struct device_node *np = pdev->dev.of_node;
  1078. const struct of_device_id *of_id =
  1079. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  1080. if (!of_id)
  1081. return 0;
  1082. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1083. if (!pdata)
  1084. return -ENOMEM;
  1085. if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
  1086. pdata->enable_arbiter = 1;
  1087. if (of_get_property(np, "marvell,nand-keep-config", NULL))
  1088. pdata->keep_config = 1;
  1089. of_property_read_u32(np, "num-cs", &pdata->num_cs);
  1090. pdev->dev.platform_data = pdata;
  1091. return 0;
  1092. }
  1093. #else
  1094. static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
  1095. {
  1096. return 0;
  1097. }
  1098. #endif
  1099. static int pxa3xx_nand_probe(struct platform_device *pdev)
  1100. {
  1101. struct pxa3xx_nand_platform_data *pdata;
  1102. struct mtd_part_parser_data ppdata = {};
  1103. struct pxa3xx_nand_info *info;
  1104. int ret, cs, probe_success;
  1105. ret = pxa3xx_nand_probe_dt(pdev);
  1106. if (ret)
  1107. return ret;
  1108. pdata = dev_get_platdata(&pdev->dev);
  1109. if (!pdata) {
  1110. dev_err(&pdev->dev, "no platform data defined\n");
  1111. return -ENODEV;
  1112. }
  1113. ret = alloc_nand_resource(pdev);
  1114. if (ret) {
  1115. dev_err(&pdev->dev, "alloc nand resource failed\n");
  1116. return ret;
  1117. }
  1118. info = platform_get_drvdata(pdev);
  1119. info->variant = pxa3xx_nand_get_variant(pdev);
  1120. probe_success = 0;
  1121. for (cs = 0; cs < pdata->num_cs; cs++) {
  1122. struct mtd_info *mtd = info->host[cs]->mtd;
  1123. mtd->name = pdev->name;
  1124. info->cs = cs;
  1125. ret = pxa3xx_nand_scan(mtd);
  1126. if (ret) {
  1127. dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
  1128. cs);
  1129. continue;
  1130. }
  1131. ppdata.of_node = pdev->dev.of_node;
  1132. ret = mtd_device_parse_register(mtd, NULL,
  1133. &ppdata, pdata->parts[cs],
  1134. pdata->nr_parts[cs]);
  1135. if (!ret)
  1136. probe_success = 1;
  1137. }
  1138. if (!probe_success) {
  1139. pxa3xx_nand_remove(pdev);
  1140. return -ENODEV;
  1141. }
  1142. return 0;
  1143. }
  1144. #ifdef CONFIG_PM
  1145. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1146. {
  1147. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1148. struct pxa3xx_nand_platform_data *pdata;
  1149. struct mtd_info *mtd;
  1150. int cs;
  1151. pdata = dev_get_platdata(&pdev->dev);
  1152. if (info->state) {
  1153. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1154. return -EAGAIN;
  1155. }
  1156. for (cs = 0; cs < pdata->num_cs; cs++) {
  1157. mtd = info->host[cs]->mtd;
  1158. mtd_suspend(mtd);
  1159. }
  1160. return 0;
  1161. }
  1162. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1163. {
  1164. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1165. struct pxa3xx_nand_platform_data *pdata;
  1166. struct mtd_info *mtd;
  1167. int cs;
  1168. pdata = dev_get_platdata(&pdev->dev);
  1169. /* We don't want to handle interrupt without calling mtd routine */
  1170. disable_int(info, NDCR_INT_MASK);
  1171. /*
  1172. * Directly set the chip select to a invalid value,
  1173. * then the driver would reset the timing according
  1174. * to current chip select at the beginning of cmdfunc
  1175. */
  1176. info->cs = 0xff;
  1177. /*
  1178. * As the spec says, the NDSR would be updated to 0x1800 when
  1179. * doing the nand_clk disable/enable.
  1180. * To prevent it damaging state machine of the driver, clear
  1181. * all status before resume
  1182. */
  1183. nand_writel(info, NDSR, NDSR_MASK);
  1184. for (cs = 0; cs < pdata->num_cs; cs++) {
  1185. mtd = info->host[cs]->mtd;
  1186. mtd_resume(mtd);
  1187. }
  1188. return 0;
  1189. }
  1190. #else
  1191. #define pxa3xx_nand_suspend NULL
  1192. #define pxa3xx_nand_resume NULL
  1193. #endif
  1194. static struct platform_driver pxa3xx_nand_driver = {
  1195. .driver = {
  1196. .name = "pxa3xx-nand",
  1197. .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
  1198. },
  1199. .probe = pxa3xx_nand_probe,
  1200. .remove = pxa3xx_nand_remove,
  1201. .suspend = pxa3xx_nand_suspend,
  1202. .resume = pxa3xx_nand_resume,
  1203. };
  1204. module_platform_driver(pxa3xx_nand_driver);
  1205. MODULE_LICENSE("GPL");
  1206. MODULE_DESCRIPTION("PXA3xx NAND controller driver");