pxa3xx_nand.c 35 KB

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