pxa3xx_nand.c 34 KB

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