pxa3xx_nand.c 34 KB

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