pxa3xx_nand.c 34 KB

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