pxa3xx_nand.c 35 KB

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