pxa3xx_nand.c 33 KB

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