pxa3xx_nand.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. writel_relaxed((val), (info)->mmio_base + (off))
  88. #define nand_readl(info, off) \
  89. readl_relaxed((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 bool 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->ecc.strength = 1;
  855. chip->options = NAND_NO_AUTOINCR;
  856. chip->options |= NAND_NO_READRDY;
  857. if (host->reg_ndcr & NDCR_DWIDTH_M)
  858. chip->options |= NAND_BUSWIDTH_16;
  859. if (nand_scan_ident(mtd, 1, def))
  860. return -ENODEV;
  861. /* calculate addressing information */
  862. if (mtd->writesize >= 2048)
  863. host->col_addr_cycles = 2;
  864. else
  865. host->col_addr_cycles = 1;
  866. info->oob_buff = info->data_buff + mtd->writesize;
  867. if ((mtd->size >> chip->page_shift) > 65536)
  868. host->row_addr_cycles = 3;
  869. else
  870. host->row_addr_cycles = 2;
  871. mtd->name = mtd_names[0];
  872. return nand_scan_tail(mtd);
  873. }
  874. static int alloc_nand_resource(struct platform_device *pdev)
  875. {
  876. struct pxa3xx_nand_platform_data *pdata;
  877. struct pxa3xx_nand_info *info;
  878. struct pxa3xx_nand_host *host;
  879. struct nand_chip *chip;
  880. struct mtd_info *mtd;
  881. struct resource *r;
  882. int ret, irq, cs;
  883. pdata = pdev->dev.platform_data;
  884. info = kzalloc(sizeof(*info) + (sizeof(*mtd) +
  885. sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
  886. if (!info) {
  887. dev_err(&pdev->dev, "failed to allocate memory\n");
  888. return -ENOMEM;
  889. }
  890. info->pdev = pdev;
  891. for (cs = 0; cs < pdata->num_cs; cs++) {
  892. mtd = (struct mtd_info *)((unsigned int)&info[1] +
  893. (sizeof(*mtd) + sizeof(*host)) * cs);
  894. chip = (struct nand_chip *)(&mtd[1]);
  895. host = (struct pxa3xx_nand_host *)chip;
  896. info->host[cs] = host;
  897. host->mtd = mtd;
  898. host->cs = cs;
  899. host->info_data = info;
  900. mtd->priv = host;
  901. mtd->owner = THIS_MODULE;
  902. chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
  903. chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
  904. chip->controller = &info->controller;
  905. chip->waitfunc = pxa3xx_nand_waitfunc;
  906. chip->select_chip = pxa3xx_nand_select_chip;
  907. chip->cmdfunc = pxa3xx_nand_cmdfunc;
  908. chip->read_word = pxa3xx_nand_read_word;
  909. chip->read_byte = pxa3xx_nand_read_byte;
  910. chip->read_buf = pxa3xx_nand_read_buf;
  911. chip->write_buf = pxa3xx_nand_write_buf;
  912. chip->verify_buf = pxa3xx_nand_verify_buf;
  913. }
  914. spin_lock_init(&chip->controller->lock);
  915. init_waitqueue_head(&chip->controller->wq);
  916. info->clk = clk_get(&pdev->dev, NULL);
  917. if (IS_ERR(info->clk)) {
  918. dev_err(&pdev->dev, "failed to get nand clock\n");
  919. ret = PTR_ERR(info->clk);
  920. goto fail_free_mtd;
  921. }
  922. clk_enable(info->clk);
  923. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  924. if (r == NULL) {
  925. dev_err(&pdev->dev, "no resource defined for data DMA\n");
  926. ret = -ENXIO;
  927. goto fail_put_clk;
  928. }
  929. info->drcmr_dat = r->start;
  930. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  931. if (r == NULL) {
  932. dev_err(&pdev->dev, "no resource defined for command DMA\n");
  933. ret = -ENXIO;
  934. goto fail_put_clk;
  935. }
  936. info->drcmr_cmd = r->start;
  937. irq = platform_get_irq(pdev, 0);
  938. if (irq < 0) {
  939. dev_err(&pdev->dev, "no IRQ resource defined\n");
  940. ret = -ENXIO;
  941. goto fail_put_clk;
  942. }
  943. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  944. if (r == NULL) {
  945. dev_err(&pdev->dev, "no IO memory resource defined\n");
  946. ret = -ENODEV;
  947. goto fail_put_clk;
  948. }
  949. r = request_mem_region(r->start, resource_size(r), pdev->name);
  950. if (r == NULL) {
  951. dev_err(&pdev->dev, "failed to request memory resource\n");
  952. ret = -EBUSY;
  953. goto fail_put_clk;
  954. }
  955. info->mmio_base = ioremap(r->start, resource_size(r));
  956. if (info->mmio_base == NULL) {
  957. dev_err(&pdev->dev, "ioremap() failed\n");
  958. ret = -ENODEV;
  959. goto fail_free_res;
  960. }
  961. info->mmio_phys = r->start;
  962. ret = pxa3xx_nand_init_buff(info);
  963. if (ret)
  964. goto fail_free_io;
  965. /* initialize all interrupts to be disabled */
  966. disable_int(info, NDSR_MASK);
  967. ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
  968. pdev->name, info);
  969. if (ret < 0) {
  970. dev_err(&pdev->dev, "failed to request IRQ\n");
  971. goto fail_free_buf;
  972. }
  973. platform_set_drvdata(pdev, info);
  974. return 0;
  975. fail_free_buf:
  976. free_irq(irq, info);
  977. if (use_dma) {
  978. pxa_free_dma(info->data_dma_ch);
  979. dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
  980. info->data_buff, info->data_buff_phys);
  981. } else
  982. kfree(info->data_buff);
  983. fail_free_io:
  984. iounmap(info->mmio_base);
  985. fail_free_res:
  986. release_mem_region(r->start, resource_size(r));
  987. fail_put_clk:
  988. clk_disable(info->clk);
  989. clk_put(info->clk);
  990. fail_free_mtd:
  991. kfree(info);
  992. return ret;
  993. }
  994. static int pxa3xx_nand_remove(struct platform_device *pdev)
  995. {
  996. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  997. struct pxa3xx_nand_platform_data *pdata;
  998. struct resource *r;
  999. int irq, cs;
  1000. if (!info)
  1001. return 0;
  1002. pdata = pdev->dev.platform_data;
  1003. platform_set_drvdata(pdev, NULL);
  1004. irq = platform_get_irq(pdev, 0);
  1005. if (irq >= 0)
  1006. free_irq(irq, info);
  1007. if (use_dma) {
  1008. pxa_free_dma(info->data_dma_ch);
  1009. dma_free_writecombine(&pdev->dev, MAX_BUFF_SIZE,
  1010. info->data_buff, info->data_buff_phys);
  1011. } else
  1012. kfree(info->data_buff);
  1013. iounmap(info->mmio_base);
  1014. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1015. release_mem_region(r->start, resource_size(r));
  1016. clk_disable(info->clk);
  1017. clk_put(info->clk);
  1018. for (cs = 0; cs < pdata->num_cs; cs++)
  1019. nand_release(info->host[cs]->mtd);
  1020. kfree(info);
  1021. return 0;
  1022. }
  1023. static int pxa3xx_nand_probe(struct platform_device *pdev)
  1024. {
  1025. struct pxa3xx_nand_platform_data *pdata;
  1026. struct pxa3xx_nand_info *info;
  1027. int ret, cs, probe_success;
  1028. pdata = pdev->dev.platform_data;
  1029. if (!pdata) {
  1030. dev_err(&pdev->dev, "no platform data defined\n");
  1031. return -ENODEV;
  1032. }
  1033. ret = alloc_nand_resource(pdev);
  1034. if (ret) {
  1035. dev_err(&pdev->dev, "alloc nand resource failed\n");
  1036. return ret;
  1037. }
  1038. info = platform_get_drvdata(pdev);
  1039. probe_success = 0;
  1040. for (cs = 0; cs < pdata->num_cs; cs++) {
  1041. info->cs = cs;
  1042. ret = pxa3xx_nand_scan(info->host[cs]->mtd);
  1043. if (ret) {
  1044. dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
  1045. cs);
  1046. continue;
  1047. }
  1048. ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
  1049. NULL, pdata->parts[cs],
  1050. pdata->nr_parts[cs]);
  1051. if (!ret)
  1052. probe_success = 1;
  1053. }
  1054. if (!probe_success) {
  1055. pxa3xx_nand_remove(pdev);
  1056. return -ENODEV;
  1057. }
  1058. return 0;
  1059. }
  1060. #ifdef CONFIG_PM
  1061. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1062. {
  1063. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1064. struct pxa3xx_nand_platform_data *pdata;
  1065. struct mtd_info *mtd;
  1066. int cs;
  1067. pdata = pdev->dev.platform_data;
  1068. if (info->state) {
  1069. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1070. return -EAGAIN;
  1071. }
  1072. for (cs = 0; cs < pdata->num_cs; cs++) {
  1073. mtd = info->host[cs]->mtd;
  1074. mtd_suspend(mtd);
  1075. }
  1076. return 0;
  1077. }
  1078. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1079. {
  1080. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1081. struct pxa3xx_nand_platform_data *pdata;
  1082. struct mtd_info *mtd;
  1083. int cs;
  1084. pdata = pdev->dev.platform_data;
  1085. /* We don't want to handle interrupt without calling mtd routine */
  1086. disable_int(info, NDCR_INT_MASK);
  1087. /*
  1088. * Directly set the chip select to a invalid value,
  1089. * then the driver would reset the timing according
  1090. * to current chip select at the beginning of cmdfunc
  1091. */
  1092. info->cs = 0xff;
  1093. /*
  1094. * As the spec says, the NDSR would be updated to 0x1800 when
  1095. * doing the nand_clk disable/enable.
  1096. * To prevent it damaging state machine of the driver, clear
  1097. * all status before resume
  1098. */
  1099. nand_writel(info, NDSR, NDSR_MASK);
  1100. for (cs = 0; cs < pdata->num_cs; cs++) {
  1101. mtd = info->host[cs]->mtd;
  1102. mtd_resume(mtd);
  1103. }
  1104. return 0;
  1105. }
  1106. #else
  1107. #define pxa3xx_nand_suspend NULL
  1108. #define pxa3xx_nand_resume NULL
  1109. #endif
  1110. static struct platform_driver pxa3xx_nand_driver = {
  1111. .driver = {
  1112. .name = "pxa3xx-nand",
  1113. },
  1114. .probe = pxa3xx_nand_probe,
  1115. .remove = pxa3xx_nand_remove,
  1116. .suspend = pxa3xx_nand_suspend,
  1117. .resume = pxa3xx_nand_resume,
  1118. };
  1119. module_platform_driver(pxa3xx_nand_driver);
  1120. MODULE_LICENSE("GPL");
  1121. MODULE_DESCRIPTION("PXA3xx NAND controller driver");