pxa3xx_nand.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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->is_ready = 0;
  440. info->retcode = ERR_NONE;
  441. if (info->cs != 0)
  442. info->ndcb0 = NDCB0_CSEL;
  443. else
  444. info->ndcb0 = 0;
  445. switch (command) {
  446. case NAND_CMD_READ0:
  447. case NAND_CMD_PAGEPROG:
  448. info->use_ecc = 1;
  449. case NAND_CMD_READOOB:
  450. pxa3xx_set_datasize(info);
  451. break;
  452. case NAND_CMD_SEQIN:
  453. exec_cmd = 0;
  454. break;
  455. default:
  456. info->ndcb1 = 0;
  457. info->ndcb2 = 0;
  458. break;
  459. }
  460. addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
  461. + host->col_addr_cycles);
  462. switch (command) {
  463. case NAND_CMD_READOOB:
  464. case NAND_CMD_READ0:
  465. cmd = host->cmdset->read1;
  466. if (command == NAND_CMD_READOOB)
  467. info->buf_start = mtd->writesize + column;
  468. else
  469. info->buf_start = column;
  470. if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
  471. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  472. | addr_cycle
  473. | (cmd & NDCB0_CMD1_MASK);
  474. else
  475. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  476. | NDCB0_DBC
  477. | addr_cycle
  478. | cmd;
  479. case NAND_CMD_SEQIN:
  480. /* small page addr setting */
  481. if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
  482. info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
  483. | (column & 0xFF);
  484. info->ndcb2 = 0;
  485. } else {
  486. info->ndcb1 = ((page_addr & 0xFFFF) << 16)
  487. | (column & 0xFFFF);
  488. if (page_addr & 0xFF0000)
  489. info->ndcb2 = (page_addr & 0xFF0000) >> 16;
  490. else
  491. info->ndcb2 = 0;
  492. }
  493. info->buf_count = mtd->writesize + mtd->oobsize;
  494. memset(info->data_buff, 0xFF, info->buf_count);
  495. break;
  496. case NAND_CMD_PAGEPROG:
  497. if (is_buf_blank(info->data_buff,
  498. (mtd->writesize + mtd->oobsize))) {
  499. exec_cmd = 0;
  500. break;
  501. }
  502. cmd = host->cmdset->program;
  503. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  504. | NDCB0_AUTO_RS
  505. | NDCB0_ST_ROW_EN
  506. | NDCB0_DBC
  507. | cmd
  508. | addr_cycle;
  509. break;
  510. case NAND_CMD_READID:
  511. cmd = host->cmdset->read_id;
  512. info->buf_count = host->read_id_bytes;
  513. info->ndcb0 |= NDCB0_CMD_TYPE(3)
  514. | NDCB0_ADDR_CYC(1)
  515. | cmd;
  516. info->data_size = 8;
  517. break;
  518. case NAND_CMD_STATUS:
  519. cmd = host->cmdset->read_status;
  520. info->buf_count = 1;
  521. info->ndcb0 |= NDCB0_CMD_TYPE(4)
  522. | NDCB0_ADDR_CYC(1)
  523. | cmd;
  524. info->data_size = 8;
  525. break;
  526. case NAND_CMD_ERASE1:
  527. cmd = host->cmdset->erase;
  528. info->ndcb0 |= NDCB0_CMD_TYPE(2)
  529. | NDCB0_AUTO_RS
  530. | NDCB0_ADDR_CYC(3)
  531. | NDCB0_DBC
  532. | cmd;
  533. info->ndcb1 = page_addr;
  534. info->ndcb2 = 0;
  535. break;
  536. case NAND_CMD_RESET:
  537. cmd = host->cmdset->reset;
  538. info->ndcb0 |= NDCB0_CMD_TYPE(5)
  539. | cmd;
  540. break;
  541. case NAND_CMD_ERASE2:
  542. exec_cmd = 0;
  543. break;
  544. default:
  545. exec_cmd = 0;
  546. dev_err(&info->pdev->dev, "non-supported command %x\n",
  547. command);
  548. break;
  549. }
  550. return exec_cmd;
  551. }
  552. static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  553. int column, int page_addr)
  554. {
  555. struct pxa3xx_nand_host *host = mtd->priv;
  556. struct pxa3xx_nand_info *info = host->info_data;
  557. int ret, exec_cmd;
  558. /*
  559. * if this is a x16 device ,then convert the input
  560. * "byte" address into a "word" address appropriate
  561. * for indexing a word-oriented device
  562. */
  563. if (host->reg_ndcr & NDCR_DWIDTH_M)
  564. column /= 2;
  565. /*
  566. * There may be different NAND chip hooked to
  567. * different chip select, so check whether
  568. * chip select has been changed, if yes, reset the timing
  569. */
  570. if (info->cs != host->cs) {
  571. info->cs = host->cs;
  572. nand_writel(info, NDTR0CS0, host->ndtr0cs0);
  573. nand_writel(info, NDTR1CS0, host->ndtr1cs0);
  574. }
  575. info->state = STATE_PREPARED;
  576. exec_cmd = prepare_command_pool(info, command, column, page_addr);
  577. if (exec_cmd) {
  578. init_completion(&info->cmd_complete);
  579. pxa3xx_nand_start(info);
  580. ret = wait_for_completion_timeout(&info->cmd_complete,
  581. CHIP_DELAY_TIMEOUT);
  582. if (!ret) {
  583. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  584. /* Stop State Machine for next command cycle */
  585. pxa3xx_nand_stop(info);
  586. }
  587. }
  588. info->state = STATE_IDLE;
  589. }
  590. static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
  591. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  592. {
  593. chip->write_buf(mtd, buf, mtd->writesize);
  594. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  595. return 0;
  596. }
  597. static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
  598. struct nand_chip *chip, uint8_t *buf, int oob_required,
  599. int page)
  600. {
  601. struct pxa3xx_nand_host *host = mtd->priv;
  602. struct pxa3xx_nand_info *info = host->info_data;
  603. chip->read_buf(mtd, buf, mtd->writesize);
  604. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  605. if (info->retcode == ERR_SBERR) {
  606. switch (info->use_ecc) {
  607. case 1:
  608. mtd->ecc_stats.corrected++;
  609. break;
  610. case 0:
  611. default:
  612. break;
  613. }
  614. } else if (info->retcode == ERR_DBERR) {
  615. /*
  616. * for blank page (all 0xff), HW will calculate its ECC as
  617. * 0, which is different from the ECC information within
  618. * OOB, ignore such double bit errors
  619. */
  620. if (is_buf_blank(buf, mtd->writesize))
  621. info->retcode = ERR_NONE;
  622. else
  623. mtd->ecc_stats.failed++;
  624. }
  625. return 0;
  626. }
  627. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  628. {
  629. struct pxa3xx_nand_host *host = mtd->priv;
  630. struct pxa3xx_nand_info *info = host->info_data;
  631. char retval = 0xFF;
  632. if (info->buf_start < info->buf_count)
  633. /* Has just send a new command? */
  634. retval = info->data_buff[info->buf_start++];
  635. return retval;
  636. }
  637. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  638. {
  639. struct pxa3xx_nand_host *host = mtd->priv;
  640. struct pxa3xx_nand_info *info = host->info_data;
  641. u16 retval = 0xFFFF;
  642. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  643. retval = *((u16 *)(info->data_buff+info->buf_start));
  644. info->buf_start += 2;
  645. }
  646. return retval;
  647. }
  648. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  649. {
  650. struct pxa3xx_nand_host *host = mtd->priv;
  651. struct pxa3xx_nand_info *info = host->info_data;
  652. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  653. memcpy(buf, info->data_buff + info->buf_start, real_len);
  654. info->buf_start += real_len;
  655. }
  656. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  657. const uint8_t *buf, int len)
  658. {
  659. struct pxa3xx_nand_host *host = mtd->priv;
  660. struct pxa3xx_nand_info *info = host->info_data;
  661. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  662. memcpy(info->data_buff + info->buf_start, buf, real_len);
  663. info->buf_start += real_len;
  664. }
  665. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  666. {
  667. return;
  668. }
  669. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  670. {
  671. struct pxa3xx_nand_host *host = mtd->priv;
  672. struct pxa3xx_nand_info *info = host->info_data;
  673. /* pxa3xx_nand_send_command has waited for command complete */
  674. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  675. if (info->retcode == ERR_NONE)
  676. return 0;
  677. else {
  678. /*
  679. * any error make it return 0x01 which will tell
  680. * the caller the erase and write fail
  681. */
  682. return 0x01;
  683. }
  684. }
  685. return 0;
  686. }
  687. static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
  688. const struct pxa3xx_nand_flash *f)
  689. {
  690. struct platform_device *pdev = info->pdev;
  691. struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
  692. struct pxa3xx_nand_host *host = info->host[info->cs];
  693. uint32_t ndcr = 0x0; /* enable all interrupts */
  694. if (f->page_size != 2048 && f->page_size != 512) {
  695. dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
  696. return -EINVAL;
  697. }
  698. if (f->flash_width != 16 && f->flash_width != 8) {
  699. dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
  700. return -EINVAL;
  701. }
  702. /* calculate flash information */
  703. host->cmdset = &default_cmdset;
  704. host->page_size = f->page_size;
  705. host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
  706. /* calculate addressing information */
  707. host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
  708. if (f->num_blocks * f->page_per_block > 65536)
  709. host->row_addr_cycles = 3;
  710. else
  711. host->row_addr_cycles = 2;
  712. ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  713. ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  714. ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
  715. ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
  716. ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
  717. ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
  718. ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
  719. ndcr |= NDCR_SPARE_EN; /* enable spare by default */
  720. host->reg_ndcr = ndcr;
  721. pxa3xx_nand_set_timing(host, f->timing);
  722. return 0;
  723. }
  724. static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  725. {
  726. /*
  727. * We set 0 by hard coding here, for we don't support keep_config
  728. * when there is more than one chip attached to the controller
  729. */
  730. struct pxa3xx_nand_host *host = info->host[0];
  731. uint32_t ndcr = nand_readl(info, NDCR);
  732. if (ndcr & NDCR_PAGE_SZ) {
  733. host->page_size = 2048;
  734. host->read_id_bytes = 4;
  735. } else {
  736. host->page_size = 512;
  737. host->read_id_bytes = 2;
  738. }
  739. host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
  740. host->cmdset = &default_cmdset;
  741. host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
  742. host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
  743. return 0;
  744. }
  745. /* the maximum possible buffer size for large page with OOB data
  746. * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
  747. * data buffer and the DMA descriptor
  748. */
  749. #define MAX_BUFF_SIZE PAGE_SIZE
  750. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  751. {
  752. struct platform_device *pdev = info->pdev;
  753. int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
  754. if (use_dma == 0) {
  755. info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
  756. if (info->data_buff == NULL)
  757. return -ENOMEM;
  758. return 0;
  759. }
  760. info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
  761. &info->data_buff_phys, GFP_KERNEL);
  762. if (info->data_buff == NULL) {
  763. dev_err(&pdev->dev, "failed to allocate dma buffer\n");
  764. return -ENOMEM;
  765. }
  766. info->data_desc = (void *)info->data_buff + data_desc_offset;
  767. info->data_desc_addr = info->data_buff_phys + data_desc_offset;
  768. info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
  769. pxa3xx_nand_data_dma_irq, info);
  770. if (info->data_dma_ch < 0) {
  771. dev_err(&pdev->dev, "failed to request data dma\n");
  772. dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
  773. info->data_buff, info->data_buff_phys);
  774. return info->data_dma_ch;
  775. }
  776. return 0;
  777. }
  778. static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
  779. {
  780. struct mtd_info *mtd;
  781. int ret;
  782. mtd = info->host[info->cs]->mtd;
  783. /* use the common timing to make a try */
  784. ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
  785. if (ret)
  786. return ret;
  787. pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
  788. if (info->is_ready)
  789. return 0;
  790. return -ENODEV;
  791. }
  792. static int pxa3xx_nand_scan(struct mtd_info *mtd)
  793. {
  794. struct pxa3xx_nand_host *host = mtd->priv;
  795. struct pxa3xx_nand_info *info = host->info_data;
  796. struct platform_device *pdev = info->pdev;
  797. struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
  798. struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
  799. const struct pxa3xx_nand_flash *f = NULL;
  800. struct nand_chip *chip = mtd->priv;
  801. uint32_t id = -1;
  802. uint64_t chipsize;
  803. int i, ret, num;
  804. if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
  805. goto KEEP_CONFIG;
  806. ret = pxa3xx_nand_sensing(info);
  807. if (ret) {
  808. dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
  809. info->cs);
  810. return ret;
  811. }
  812. chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
  813. id = *((uint16_t *)(info->data_buff));
  814. if (id != 0)
  815. dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
  816. else {
  817. dev_warn(&info->pdev->dev,
  818. "Read out ID 0, potential timing set wrong!!\n");
  819. return -EINVAL;
  820. }
  821. num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
  822. for (i = 0; i < num; i++) {
  823. if (i < pdata->num_flash)
  824. f = pdata->flash + i;
  825. else
  826. f = &builtin_flash_types[i - pdata->num_flash + 1];
  827. /* find the chip in default list */
  828. if (f->chip_id == id)
  829. break;
  830. }
  831. if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
  832. dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
  833. return -EINVAL;
  834. }
  835. ret = pxa3xx_nand_config_flash(info, f);
  836. if (ret) {
  837. dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
  838. return ret;
  839. }
  840. pxa3xx_flash_ids[0].name = f->name;
  841. pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
  842. pxa3xx_flash_ids[0].pagesize = f->page_size;
  843. chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
  844. pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
  845. pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
  846. if (f->flash_width == 16)
  847. pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
  848. pxa3xx_flash_ids[1].name = NULL;
  849. def = pxa3xx_flash_ids;
  850. KEEP_CONFIG:
  851. chip->ecc.mode = NAND_ECC_HW;
  852. chip->ecc.size = host->page_size;
  853. chip->ecc.strength = 1;
  854. if (host->reg_ndcr & NDCR_DWIDTH_M)
  855. chip->options |= NAND_BUSWIDTH_16;
  856. if (nand_scan_ident(mtd, 1, def))
  857. return -ENODEV;
  858. /* calculate addressing information */
  859. if (mtd->writesize >= 2048)
  860. host->col_addr_cycles = 2;
  861. else
  862. host->col_addr_cycles = 1;
  863. info->oob_buff = info->data_buff + mtd->writesize;
  864. if ((mtd->size >> chip->page_shift) > 65536)
  865. host->row_addr_cycles = 3;
  866. else
  867. host->row_addr_cycles = 2;
  868. mtd->name = mtd_names[0];
  869. return nand_scan_tail(mtd);
  870. }
  871. static int alloc_nand_resource(struct platform_device *pdev)
  872. {
  873. struct pxa3xx_nand_platform_data *pdata;
  874. struct pxa3xx_nand_info *info;
  875. struct pxa3xx_nand_host *host;
  876. struct nand_chip *chip = NULL;
  877. struct mtd_info *mtd;
  878. struct resource *r;
  879. int ret, irq, cs;
  880. pdata = pdev->dev.platform_data;
  881. info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
  882. sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
  883. if (!info)
  884. return -ENOMEM;
  885. info->pdev = pdev;
  886. for (cs = 0; cs < pdata->num_cs; cs++) {
  887. mtd = (struct mtd_info *)((unsigned int)&info[1] +
  888. (sizeof(*mtd) + sizeof(*host)) * cs);
  889. chip = (struct nand_chip *)(&mtd[1]);
  890. host = (struct pxa3xx_nand_host *)chip;
  891. info->host[cs] = host;
  892. host->mtd = mtd;
  893. host->cs = cs;
  894. host->info_data = info;
  895. mtd->priv = host;
  896. mtd->owner = THIS_MODULE;
  897. chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
  898. chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
  899. chip->controller = &info->controller;
  900. chip->waitfunc = pxa3xx_nand_waitfunc;
  901. chip->select_chip = pxa3xx_nand_select_chip;
  902. chip->cmdfunc = pxa3xx_nand_cmdfunc;
  903. chip->read_word = pxa3xx_nand_read_word;
  904. chip->read_byte = pxa3xx_nand_read_byte;
  905. chip->read_buf = pxa3xx_nand_read_buf;
  906. chip->write_buf = pxa3xx_nand_write_buf;
  907. }
  908. spin_lock_init(&chip->controller->lock);
  909. init_waitqueue_head(&chip->controller->wq);
  910. info->clk = devm_clk_get(&pdev->dev, NULL);
  911. if (IS_ERR(info->clk)) {
  912. dev_err(&pdev->dev, "failed to get nand clock\n");
  913. return PTR_ERR(info->clk);
  914. }
  915. ret = clk_prepare_enable(info->clk);
  916. if (ret < 0)
  917. return ret;
  918. /*
  919. * This is a dirty hack to make this driver work from devicetree
  920. * bindings. It can be removed once we have a prober DMA controller
  921. * framework for DT.
  922. */
  923. if (pdev->dev.of_node && cpu_is_pxa3xx()) {
  924. info->drcmr_dat = 97;
  925. info->drcmr_cmd = 99;
  926. } else {
  927. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  928. if (r == NULL) {
  929. dev_err(&pdev->dev, "no resource defined for data DMA\n");
  930. ret = -ENXIO;
  931. goto fail_disable_clk;
  932. }
  933. info->drcmr_dat = r->start;
  934. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  935. if (r == NULL) {
  936. dev_err(&pdev->dev, "no resource defined for command DMA\n");
  937. ret = -ENXIO;
  938. goto fail_disable_clk;
  939. }
  940. info->drcmr_cmd = r->start;
  941. }
  942. irq = platform_get_irq(pdev, 0);
  943. if (irq < 0) {
  944. dev_err(&pdev->dev, "no IRQ resource defined\n");
  945. ret = -ENXIO;
  946. goto fail_disable_clk;
  947. }
  948. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  949. info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  950. if (IS_ERR(info->mmio_base)) {
  951. ret = PTR_ERR(info->mmio_base);
  952. goto fail_disable_clk;
  953. }
  954. info->mmio_phys = r->start;
  955. ret = pxa3xx_nand_init_buff(info);
  956. if (ret)
  957. goto fail_disable_clk;
  958. /* initialize all interrupts to be disabled */
  959. disable_int(info, NDSR_MASK);
  960. ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
  961. pdev->name, info);
  962. if (ret < 0) {
  963. dev_err(&pdev->dev, "failed to request IRQ\n");
  964. goto fail_free_buf;
  965. }
  966. platform_set_drvdata(pdev, info);
  967. return 0;
  968. fail_free_buf:
  969. free_irq(irq, info);
  970. if (use_dma) {
  971. pxa_free_dma(info->data_dma_ch);
  972. dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
  973. info->data_buff, info->data_buff_phys);
  974. } else
  975. kfree(info->data_buff);
  976. fail_disable_clk:
  977. clk_disable_unprepare(info->clk);
  978. return ret;
  979. }
  980. static int pxa3xx_nand_remove(struct platform_device *pdev)
  981. {
  982. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  983. struct pxa3xx_nand_platform_data *pdata;
  984. int irq, cs;
  985. if (!info)
  986. return 0;
  987. pdata = pdev->dev.platform_data;
  988. platform_set_drvdata(pdev, NULL);
  989. irq = platform_get_irq(pdev, 0);
  990. if (irq >= 0)
  991. free_irq(irq, info);
  992. if (use_dma) {
  993. pxa_free_dma(info->data_dma_ch);
  994. dma_free_writecombine(&pdev->dev, MAX_BUFF_SIZE,
  995. info->data_buff, info->data_buff_phys);
  996. } else
  997. kfree(info->data_buff);
  998. clk_disable_unprepare(info->clk);
  999. for (cs = 0; cs < pdata->num_cs; cs++)
  1000. nand_release(info->host[cs]->mtd);
  1001. return 0;
  1002. }
  1003. #ifdef CONFIG_OF
  1004. static struct of_device_id pxa3xx_nand_dt_ids[] = {
  1005. { .compatible = "marvell,pxa3xx-nand" },
  1006. {}
  1007. };
  1008. MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
  1009. static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
  1010. {
  1011. struct pxa3xx_nand_platform_data *pdata;
  1012. struct device_node *np = pdev->dev.of_node;
  1013. const struct of_device_id *of_id =
  1014. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  1015. if (!of_id)
  1016. return 0;
  1017. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1018. if (!pdata)
  1019. return -ENOMEM;
  1020. if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
  1021. pdata->enable_arbiter = 1;
  1022. if (of_get_property(np, "marvell,nand-keep-config", NULL))
  1023. pdata->keep_config = 1;
  1024. of_property_read_u32(np, "num-cs", &pdata->num_cs);
  1025. pdev->dev.platform_data = pdata;
  1026. return 0;
  1027. }
  1028. #else
  1029. static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
  1030. {
  1031. return 0;
  1032. }
  1033. #endif
  1034. static int pxa3xx_nand_probe(struct platform_device *pdev)
  1035. {
  1036. struct pxa3xx_nand_platform_data *pdata;
  1037. struct mtd_part_parser_data ppdata = {};
  1038. struct pxa3xx_nand_info *info;
  1039. int ret, cs, probe_success;
  1040. ret = pxa3xx_nand_probe_dt(pdev);
  1041. if (ret)
  1042. return ret;
  1043. pdata = pdev->dev.platform_data;
  1044. if (!pdata) {
  1045. dev_err(&pdev->dev, "no platform data defined\n");
  1046. return -ENODEV;
  1047. }
  1048. ret = alloc_nand_resource(pdev);
  1049. if (ret) {
  1050. dev_err(&pdev->dev, "alloc nand resource failed\n");
  1051. return ret;
  1052. }
  1053. info = platform_get_drvdata(pdev);
  1054. probe_success = 0;
  1055. for (cs = 0; cs < pdata->num_cs; cs++) {
  1056. info->cs = cs;
  1057. ret = pxa3xx_nand_scan(info->host[cs]->mtd);
  1058. if (ret) {
  1059. dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
  1060. cs);
  1061. continue;
  1062. }
  1063. ppdata.of_node = pdev->dev.of_node;
  1064. ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
  1065. &ppdata, pdata->parts[cs],
  1066. pdata->nr_parts[cs]);
  1067. if (!ret)
  1068. probe_success = 1;
  1069. }
  1070. if (!probe_success) {
  1071. pxa3xx_nand_remove(pdev);
  1072. return -ENODEV;
  1073. }
  1074. return 0;
  1075. }
  1076. #ifdef CONFIG_PM
  1077. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1078. {
  1079. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1080. struct pxa3xx_nand_platform_data *pdata;
  1081. struct mtd_info *mtd;
  1082. int cs;
  1083. pdata = pdev->dev.platform_data;
  1084. if (info->state) {
  1085. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1086. return -EAGAIN;
  1087. }
  1088. for (cs = 0; cs < pdata->num_cs; cs++) {
  1089. mtd = info->host[cs]->mtd;
  1090. mtd_suspend(mtd);
  1091. }
  1092. return 0;
  1093. }
  1094. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1095. {
  1096. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1097. struct pxa3xx_nand_platform_data *pdata;
  1098. struct mtd_info *mtd;
  1099. int cs;
  1100. pdata = pdev->dev.platform_data;
  1101. /* We don't want to handle interrupt without calling mtd routine */
  1102. disable_int(info, NDCR_INT_MASK);
  1103. /*
  1104. * Directly set the chip select to a invalid value,
  1105. * then the driver would reset the timing according
  1106. * to current chip select at the beginning of cmdfunc
  1107. */
  1108. info->cs = 0xff;
  1109. /*
  1110. * As the spec says, the NDSR would be updated to 0x1800 when
  1111. * doing the nand_clk disable/enable.
  1112. * To prevent it damaging state machine of the driver, clear
  1113. * all status before resume
  1114. */
  1115. nand_writel(info, NDSR, NDSR_MASK);
  1116. for (cs = 0; cs < pdata->num_cs; cs++) {
  1117. mtd = info->host[cs]->mtd;
  1118. mtd_resume(mtd);
  1119. }
  1120. return 0;
  1121. }
  1122. #else
  1123. #define pxa3xx_nand_suspend NULL
  1124. #define pxa3xx_nand_resume NULL
  1125. #endif
  1126. static struct platform_driver pxa3xx_nand_driver = {
  1127. .driver = {
  1128. .name = "pxa3xx-nand",
  1129. .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
  1130. },
  1131. .probe = pxa3xx_nand_probe,
  1132. .remove = pxa3xx_nand_remove,
  1133. .suspend = pxa3xx_nand_suspend,
  1134. .resume = pxa3xx_nand_resume,
  1135. };
  1136. module_platform_driver(pxa3xx_nand_driver);
  1137. MODULE_LICENSE("GPL");
  1138. MODULE_DESCRIPTION("PXA3xx NAND controller driver");