pxa3xx_nand.c 32 KB

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