pxa3xx_nand.c 30 KB

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