pxa3xx_nand.c 30 KB

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