pxa3xx_nand.c 36 KB

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