pxa3xx_nand.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  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. * See Documentation/mtd/nand/pxa3xx-nand.txt for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/delay.h>
  19. #include <linux/clk.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/nand.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/io.h>
  24. #include <linux/irq.h>
  25. #include <linux/slab.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_mtd.h>
  29. #if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
  30. #define ARCH_HAS_DMA
  31. #endif
  32. #ifdef ARCH_HAS_DMA
  33. #include <mach/dma.h>
  34. #endif
  35. #include <linux/platform_data/mtd-nand-pxa3xx.h>
  36. #define NAND_DEV_READY_TIMEOUT 50
  37. #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
  38. #define NAND_STOP_DELAY (2 * HZ/50)
  39. #define PAGE_CHUNK_SIZE (2048)
  40. /*
  41. * Define a buffer size for the initial command that detects the flash device:
  42. * STATUS, READID and PARAM. The largest of these is the PARAM command,
  43. * needing 256 bytes.
  44. */
  45. #define INIT_BUFFER_SIZE 256
  46. /* registers and bit definitions */
  47. #define NDCR (0x00) /* Control register */
  48. #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
  49. #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
  50. #define NDSR (0x14) /* Status Register */
  51. #define NDPCR (0x18) /* Page Count Register */
  52. #define NDBDR0 (0x1C) /* Bad Block Register 0 */
  53. #define NDBDR1 (0x20) /* Bad Block Register 1 */
  54. #define NDECCCTRL (0x28) /* ECC control */
  55. #define NDDB (0x40) /* Data Buffer */
  56. #define NDCB0 (0x48) /* Command Buffer0 */
  57. #define NDCB1 (0x4C) /* Command Buffer1 */
  58. #define NDCB2 (0x50) /* Command Buffer2 */
  59. #define NDCR_SPARE_EN (0x1 << 31)
  60. #define NDCR_ECC_EN (0x1 << 30)
  61. #define NDCR_DMA_EN (0x1 << 29)
  62. #define NDCR_ND_RUN (0x1 << 28)
  63. #define NDCR_DWIDTH_C (0x1 << 27)
  64. #define NDCR_DWIDTH_M (0x1 << 26)
  65. #define NDCR_PAGE_SZ (0x1 << 24)
  66. #define NDCR_NCSX (0x1 << 23)
  67. #define NDCR_ND_MODE (0x3 << 21)
  68. #define NDCR_NAND_MODE (0x0)
  69. #define NDCR_CLR_PG_CNT (0x1 << 20)
  70. #define NDCR_STOP_ON_UNCOR (0x1 << 19)
  71. #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
  72. #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
  73. #define NDCR_RA_START (0x1 << 15)
  74. #define NDCR_PG_PER_BLK (0x1 << 14)
  75. #define NDCR_ND_ARB_EN (0x1 << 12)
  76. #define NDCR_INT_MASK (0xFFF)
  77. #define NDSR_MASK (0xfff)
  78. #define NDSR_ERR_CNT_OFF (16)
  79. #define NDSR_ERR_CNT_MASK (0x1f)
  80. #define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
  81. #define NDSR_RDY (0x1 << 12)
  82. #define NDSR_FLASH_RDY (0x1 << 11)
  83. #define NDSR_CS0_PAGED (0x1 << 10)
  84. #define NDSR_CS1_PAGED (0x1 << 9)
  85. #define NDSR_CS0_CMDD (0x1 << 8)
  86. #define NDSR_CS1_CMDD (0x1 << 7)
  87. #define NDSR_CS0_BBD (0x1 << 6)
  88. #define NDSR_CS1_BBD (0x1 << 5)
  89. #define NDSR_UNCORERR (0x1 << 4)
  90. #define NDSR_CORERR (0x1 << 3)
  91. #define NDSR_WRDREQ (0x1 << 2)
  92. #define NDSR_RDDREQ (0x1 << 1)
  93. #define NDSR_WRCMDREQ (0x1)
  94. #define NDCB0_LEN_OVRD (0x1 << 28)
  95. #define NDCB0_ST_ROW_EN (0x1 << 26)
  96. #define NDCB0_AUTO_RS (0x1 << 25)
  97. #define NDCB0_CSEL (0x1 << 24)
  98. #define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
  99. #define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
  100. #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
  101. #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
  102. #define NDCB0_NC (0x1 << 20)
  103. #define NDCB0_DBC (0x1 << 19)
  104. #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
  105. #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
  106. #define NDCB0_CMD2_MASK (0xff << 8)
  107. #define NDCB0_CMD1_MASK (0xff)
  108. #define NDCB0_ADDR_CYC_SHIFT (16)
  109. #define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
  110. #define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
  111. #define EXT_CMD_TYPE_READ 4 /* Read */
  112. #define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
  113. #define EXT_CMD_TYPE_FINAL 3 /* Final command */
  114. #define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
  115. #define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
  116. /* macros for registers read/write */
  117. #define nand_writel(info, off, val) \
  118. __raw_writel((val), (info)->mmio_base + (off))
  119. #define nand_readl(info, off) \
  120. __raw_readl((info)->mmio_base + (off))
  121. /* error code and state */
  122. enum {
  123. ERR_NONE = 0,
  124. ERR_DMABUSERR = -1,
  125. ERR_SENDCMD = -2,
  126. ERR_UNCORERR = -3,
  127. ERR_BBERR = -4,
  128. ERR_CORERR = -5,
  129. };
  130. enum {
  131. STATE_IDLE = 0,
  132. STATE_PREPARED,
  133. STATE_CMD_HANDLE,
  134. STATE_DMA_READING,
  135. STATE_DMA_WRITING,
  136. STATE_DMA_DONE,
  137. STATE_PIO_READING,
  138. STATE_PIO_WRITING,
  139. STATE_CMD_DONE,
  140. STATE_READY,
  141. };
  142. enum pxa3xx_nand_variant {
  143. PXA3XX_NAND_VARIANT_PXA,
  144. PXA3XX_NAND_VARIANT_ARMADA370,
  145. };
  146. struct pxa3xx_nand_host {
  147. struct nand_chip chip;
  148. struct mtd_info *mtd;
  149. void *info_data;
  150. /* page size of attached chip */
  151. int use_ecc;
  152. int cs;
  153. /* calculated from pxa3xx_nand_flash data */
  154. unsigned int col_addr_cycles;
  155. unsigned int row_addr_cycles;
  156. size_t read_id_bytes;
  157. };
  158. struct pxa3xx_nand_info {
  159. struct nand_hw_control controller;
  160. struct platform_device *pdev;
  161. struct clk *clk;
  162. void __iomem *mmio_base;
  163. unsigned long mmio_phys;
  164. struct completion cmd_complete, dev_ready;
  165. unsigned int buf_start;
  166. unsigned int buf_count;
  167. unsigned int buf_size;
  168. unsigned int data_buff_pos;
  169. unsigned int oob_buff_pos;
  170. /* DMA information */
  171. int drcmr_dat;
  172. int drcmr_cmd;
  173. unsigned char *data_buff;
  174. unsigned char *oob_buff;
  175. dma_addr_t data_buff_phys;
  176. int data_dma_ch;
  177. struct pxa_dma_desc *data_desc;
  178. dma_addr_t data_desc_addr;
  179. struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
  180. unsigned int state;
  181. /*
  182. * This driver supports NFCv1 (as found in PXA SoC)
  183. * and NFCv2 (as found in Armada 370/XP SoC).
  184. */
  185. enum pxa3xx_nand_variant variant;
  186. int cs;
  187. int use_ecc; /* use HW ECC ? */
  188. int ecc_bch; /* using BCH ECC? */
  189. int use_dma; /* use DMA ? */
  190. int use_spare; /* use spare ? */
  191. int need_wait;
  192. unsigned int data_size; /* data to be read from FIFO */
  193. unsigned int chunk_size; /* split commands chunk size */
  194. unsigned int oob_size;
  195. unsigned int spare_size;
  196. unsigned int ecc_size;
  197. unsigned int ecc_err_cnt;
  198. unsigned int max_bitflips;
  199. int retcode;
  200. /* cached register value */
  201. uint32_t reg_ndcr;
  202. uint32_t ndtr0cs0;
  203. uint32_t ndtr1cs0;
  204. /* generated NDCBx register values */
  205. uint32_t ndcb0;
  206. uint32_t ndcb1;
  207. uint32_t ndcb2;
  208. uint32_t ndcb3;
  209. };
  210. static bool use_dma = 1;
  211. module_param(use_dma, bool, 0444);
  212. MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
  213. static struct pxa3xx_nand_timing timing[] = {
  214. { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
  215. { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
  216. { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
  217. { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
  218. };
  219. static struct pxa3xx_nand_flash builtin_flash_types[] = {
  220. { "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
  221. { "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
  222. { "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
  223. { "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
  224. { "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
  225. { "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
  226. { "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
  227. { "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
  228. { "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
  229. };
  230. static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
  231. static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
  232. static struct nand_bbt_descr bbt_main_descr = {
  233. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  234. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  235. .offs = 8,
  236. .len = 6,
  237. .veroffs = 14,
  238. .maxblocks = 8, /* Last 8 blocks in each chip */
  239. .pattern = bbt_pattern
  240. };
  241. static struct nand_bbt_descr bbt_mirror_descr = {
  242. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  243. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  244. .offs = 8,
  245. .len = 6,
  246. .veroffs = 14,
  247. .maxblocks = 8, /* Last 8 blocks in each chip */
  248. .pattern = bbt_mirror_pattern
  249. };
  250. static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
  251. .eccbytes = 32,
  252. .eccpos = {
  253. 32, 33, 34, 35, 36, 37, 38, 39,
  254. 40, 41, 42, 43, 44, 45, 46, 47,
  255. 48, 49, 50, 51, 52, 53, 54, 55,
  256. 56, 57, 58, 59, 60, 61, 62, 63},
  257. .oobfree = { {2, 30} }
  258. };
  259. static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
  260. .eccbytes = 64,
  261. .eccpos = {
  262. 32, 33, 34, 35, 36, 37, 38, 39,
  263. 40, 41, 42, 43, 44, 45, 46, 47,
  264. 48, 49, 50, 51, 52, 53, 54, 55,
  265. 56, 57, 58, 59, 60, 61, 62, 63,
  266. 96, 97, 98, 99, 100, 101, 102, 103,
  267. 104, 105, 106, 107, 108, 109, 110, 111,
  268. 112, 113, 114, 115, 116, 117, 118, 119,
  269. 120, 121, 122, 123, 124, 125, 126, 127},
  270. /* Bootrom looks in bytes 0 & 5 for bad blocks */
  271. .oobfree = { {6, 26}, { 64, 32} }
  272. };
  273. static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
  274. .eccbytes = 128,
  275. .eccpos = {
  276. 32, 33, 34, 35, 36, 37, 38, 39,
  277. 40, 41, 42, 43, 44, 45, 46, 47,
  278. 48, 49, 50, 51, 52, 53, 54, 55,
  279. 56, 57, 58, 59, 60, 61, 62, 63},
  280. .oobfree = { }
  281. };
  282. /* Define a default flash type setting serve as flash detecting only */
  283. #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
  284. #define NDTR0_tCH(c) (min((c), 7) << 19)
  285. #define NDTR0_tCS(c) (min((c), 7) << 16)
  286. #define NDTR0_tWH(c) (min((c), 7) << 11)
  287. #define NDTR0_tWP(c) (min((c), 7) << 8)
  288. #define NDTR0_tRH(c) (min((c), 7) << 3)
  289. #define NDTR0_tRP(c) (min((c), 7) << 0)
  290. #define NDTR1_tR(c) (min((c), 65535) << 16)
  291. #define NDTR1_tWHR(c) (min((c), 15) << 4)
  292. #define NDTR1_tAR(c) (min((c), 15) << 0)
  293. /* convert nano-seconds to nand flash controller clock cycles */
  294. #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
  295. static struct of_device_id pxa3xx_nand_dt_ids[] = {
  296. {
  297. .compatible = "marvell,pxa3xx-nand",
  298. .data = (void *)PXA3XX_NAND_VARIANT_PXA,
  299. },
  300. {
  301. .compatible = "marvell,armada370-nand",
  302. .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
  303. },
  304. {}
  305. };
  306. MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
  307. static enum pxa3xx_nand_variant
  308. pxa3xx_nand_get_variant(struct platform_device *pdev)
  309. {
  310. const struct of_device_id *of_id =
  311. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  312. if (!of_id)
  313. return PXA3XX_NAND_VARIANT_PXA;
  314. return (enum pxa3xx_nand_variant)of_id->data;
  315. }
  316. static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
  317. const struct pxa3xx_nand_timing *t)
  318. {
  319. struct pxa3xx_nand_info *info = host->info_data;
  320. unsigned long nand_clk = clk_get_rate(info->clk);
  321. uint32_t ndtr0, ndtr1;
  322. ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
  323. NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
  324. NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
  325. NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
  326. NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
  327. NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
  328. ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
  329. NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
  330. NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
  331. info->ndtr0cs0 = ndtr0;
  332. info->ndtr1cs0 = ndtr1;
  333. nand_writel(info, NDTR0CS0, ndtr0);
  334. nand_writel(info, NDTR1CS0, ndtr1);
  335. }
  336. /*
  337. * Set the data and OOB size, depending on the selected
  338. * spare and ECC configuration.
  339. * Only applicable to READ0, READOOB and PAGEPROG commands.
  340. */
  341. static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
  342. struct mtd_info *mtd)
  343. {
  344. int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
  345. info->data_size = mtd->writesize;
  346. if (!oob_enable)
  347. return;
  348. info->oob_size = info->spare_size;
  349. if (!info->use_ecc)
  350. info->oob_size += info->ecc_size;
  351. }
  352. /**
  353. * NOTE: it is a must to set ND_RUN firstly, then write
  354. * command buffer, otherwise, it does not work.
  355. * We enable all the interrupt at the same time, and
  356. * let pxa3xx_nand_irq to handle all logic.
  357. */
  358. static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
  359. {
  360. uint32_t ndcr;
  361. ndcr = info->reg_ndcr;
  362. if (info->use_ecc) {
  363. ndcr |= NDCR_ECC_EN;
  364. if (info->ecc_bch)
  365. nand_writel(info, NDECCCTRL, 0x1);
  366. } else {
  367. ndcr &= ~NDCR_ECC_EN;
  368. if (info->ecc_bch)
  369. nand_writel(info, NDECCCTRL, 0x0);
  370. }
  371. if (info->use_dma)
  372. ndcr |= NDCR_DMA_EN;
  373. else
  374. ndcr &= ~NDCR_DMA_EN;
  375. if (info->use_spare)
  376. ndcr |= NDCR_SPARE_EN;
  377. else
  378. ndcr &= ~NDCR_SPARE_EN;
  379. ndcr |= NDCR_ND_RUN;
  380. /* clear status bits and run */
  381. nand_writel(info, NDCR, 0);
  382. nand_writel(info, NDSR, NDSR_MASK);
  383. nand_writel(info, NDCR, ndcr);
  384. }
  385. static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
  386. {
  387. uint32_t ndcr;
  388. int timeout = NAND_STOP_DELAY;
  389. /* wait RUN bit in NDCR become 0 */
  390. ndcr = nand_readl(info, NDCR);
  391. while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
  392. ndcr = nand_readl(info, NDCR);
  393. udelay(1);
  394. }
  395. if (timeout <= 0) {
  396. ndcr &= ~NDCR_ND_RUN;
  397. nand_writel(info, NDCR, ndcr);
  398. }
  399. /* clear status bits */
  400. nand_writel(info, NDSR, NDSR_MASK);
  401. }
  402. static void __maybe_unused
  403. enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  404. {
  405. uint32_t ndcr;
  406. ndcr = nand_readl(info, NDCR);
  407. nand_writel(info, NDCR, ndcr & ~int_mask);
  408. }
  409. static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  410. {
  411. uint32_t ndcr;
  412. ndcr = nand_readl(info, NDCR);
  413. nand_writel(info, NDCR, ndcr | int_mask);
  414. }
  415. static void handle_data_pio(struct pxa3xx_nand_info *info)
  416. {
  417. unsigned int do_bytes = min(info->data_size, info->chunk_size);
  418. switch (info->state) {
  419. case STATE_PIO_WRITING:
  420. __raw_writesl(info->mmio_base + NDDB,
  421. info->data_buff + info->data_buff_pos,
  422. DIV_ROUND_UP(do_bytes, 4));
  423. if (info->oob_size > 0)
  424. __raw_writesl(info->mmio_base + NDDB,
  425. info->oob_buff + info->oob_buff_pos,
  426. DIV_ROUND_UP(info->oob_size, 4));
  427. break;
  428. case STATE_PIO_READING:
  429. __raw_readsl(info->mmio_base + NDDB,
  430. info->data_buff + info->data_buff_pos,
  431. DIV_ROUND_UP(do_bytes, 4));
  432. if (info->oob_size > 0)
  433. __raw_readsl(info->mmio_base + NDDB,
  434. info->oob_buff + info->oob_buff_pos,
  435. DIV_ROUND_UP(info->oob_size, 4));
  436. break;
  437. default:
  438. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  439. info->state);
  440. BUG();
  441. }
  442. /* Update buffer pointers for multi-page read/write */
  443. info->data_buff_pos += do_bytes;
  444. info->oob_buff_pos += info->oob_size;
  445. info->data_size -= do_bytes;
  446. }
  447. #ifdef ARCH_HAS_DMA
  448. static void start_data_dma(struct pxa3xx_nand_info *info)
  449. {
  450. struct pxa_dma_desc *desc = info->data_desc;
  451. int dma_len = ALIGN(info->data_size + info->oob_size, 32);
  452. desc->ddadr = DDADR_STOP;
  453. desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
  454. switch (info->state) {
  455. case STATE_DMA_WRITING:
  456. desc->dsadr = info->data_buff_phys;
  457. desc->dtadr = info->mmio_phys + NDDB;
  458. desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
  459. break;
  460. case STATE_DMA_READING:
  461. desc->dtadr = info->data_buff_phys;
  462. desc->dsadr = info->mmio_phys + NDDB;
  463. desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
  464. break;
  465. default:
  466. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  467. info->state);
  468. BUG();
  469. }
  470. DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
  471. DDADR(info->data_dma_ch) = info->data_desc_addr;
  472. DCSR(info->data_dma_ch) |= DCSR_RUN;
  473. }
  474. static void pxa3xx_nand_data_dma_irq(int channel, void *data)
  475. {
  476. struct pxa3xx_nand_info *info = data;
  477. uint32_t dcsr;
  478. dcsr = DCSR(channel);
  479. DCSR(channel) = dcsr;
  480. if (dcsr & DCSR_BUSERR) {
  481. info->retcode = ERR_DMABUSERR;
  482. }
  483. info->state = STATE_DMA_DONE;
  484. enable_int(info, NDCR_INT_MASK);
  485. nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
  486. }
  487. #else
  488. static void start_data_dma(struct pxa3xx_nand_info *info)
  489. {}
  490. #endif
  491. static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
  492. {
  493. struct pxa3xx_nand_info *info = devid;
  494. unsigned int status, is_completed = 0, is_ready = 0;
  495. unsigned int ready, cmd_done;
  496. if (info->cs == 0) {
  497. ready = NDSR_FLASH_RDY;
  498. cmd_done = NDSR_CS0_CMDD;
  499. } else {
  500. ready = NDSR_RDY;
  501. cmd_done = NDSR_CS1_CMDD;
  502. }
  503. status = nand_readl(info, NDSR);
  504. if (status & NDSR_UNCORERR)
  505. info->retcode = ERR_UNCORERR;
  506. if (status & NDSR_CORERR) {
  507. info->retcode = ERR_CORERR;
  508. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
  509. info->ecc_bch)
  510. info->ecc_err_cnt = NDSR_ERR_CNT(status);
  511. else
  512. info->ecc_err_cnt = 1;
  513. /*
  514. * Each chunk composing a page is corrected independently,
  515. * and we need to store maximum number of corrected bitflips
  516. * to return it to the MTD layer in ecc.read_page().
  517. */
  518. info->max_bitflips = max_t(unsigned int,
  519. info->max_bitflips,
  520. info->ecc_err_cnt);
  521. }
  522. if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
  523. /* whether use dma to transfer data */
  524. if (info->use_dma) {
  525. disable_int(info, NDCR_INT_MASK);
  526. info->state = (status & NDSR_RDDREQ) ?
  527. STATE_DMA_READING : STATE_DMA_WRITING;
  528. start_data_dma(info);
  529. goto NORMAL_IRQ_EXIT;
  530. } else {
  531. info->state = (status & NDSR_RDDREQ) ?
  532. STATE_PIO_READING : STATE_PIO_WRITING;
  533. handle_data_pio(info);
  534. }
  535. }
  536. if (status & cmd_done) {
  537. info->state = STATE_CMD_DONE;
  538. is_completed = 1;
  539. }
  540. if (status & ready) {
  541. info->state = STATE_READY;
  542. is_ready = 1;
  543. }
  544. if (status & NDSR_WRCMDREQ) {
  545. nand_writel(info, NDSR, NDSR_WRCMDREQ);
  546. status &= ~NDSR_WRCMDREQ;
  547. info->state = STATE_CMD_HANDLE;
  548. /*
  549. * Command buffer registers NDCB{0-2} (and optionally NDCB3)
  550. * must be loaded by writing directly either 12 or 16
  551. * bytes directly to NDCB0, four bytes at a time.
  552. *
  553. * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
  554. * but each NDCBx register can be read.
  555. */
  556. nand_writel(info, NDCB0, info->ndcb0);
  557. nand_writel(info, NDCB0, info->ndcb1);
  558. nand_writel(info, NDCB0, info->ndcb2);
  559. /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
  560. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  561. nand_writel(info, NDCB0, info->ndcb3);
  562. }
  563. /* clear NDSR to let the controller exit the IRQ */
  564. nand_writel(info, NDSR, status);
  565. if (is_completed)
  566. complete(&info->cmd_complete);
  567. if (is_ready)
  568. complete(&info->dev_ready);
  569. NORMAL_IRQ_EXIT:
  570. return IRQ_HANDLED;
  571. }
  572. static inline int is_buf_blank(uint8_t *buf, size_t len)
  573. {
  574. for (; len > 0; len--)
  575. if (*buf++ != 0xff)
  576. return 0;
  577. return 1;
  578. }
  579. static void set_command_address(struct pxa3xx_nand_info *info,
  580. unsigned int page_size, uint16_t column, int page_addr)
  581. {
  582. /* small page addr setting */
  583. if (page_size < PAGE_CHUNK_SIZE) {
  584. info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
  585. | (column & 0xFF);
  586. info->ndcb2 = 0;
  587. } else {
  588. info->ndcb1 = ((page_addr & 0xFFFF) << 16)
  589. | (column & 0xFFFF);
  590. if (page_addr & 0xFF0000)
  591. info->ndcb2 = (page_addr & 0xFF0000) >> 16;
  592. else
  593. info->ndcb2 = 0;
  594. }
  595. }
  596. static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
  597. {
  598. struct pxa3xx_nand_host *host = info->host[info->cs];
  599. struct mtd_info *mtd = host->mtd;
  600. /* reset data and oob column point to handle data */
  601. info->buf_start = 0;
  602. info->buf_count = 0;
  603. info->oob_size = 0;
  604. info->data_buff_pos = 0;
  605. info->oob_buff_pos = 0;
  606. info->use_ecc = 0;
  607. info->use_spare = 1;
  608. info->retcode = ERR_NONE;
  609. info->ecc_err_cnt = 0;
  610. info->ndcb3 = 0;
  611. info->need_wait = 0;
  612. switch (command) {
  613. case NAND_CMD_READ0:
  614. case NAND_CMD_PAGEPROG:
  615. info->use_ecc = 1;
  616. case NAND_CMD_READOOB:
  617. pxa3xx_set_datasize(info, mtd);
  618. break;
  619. case NAND_CMD_PARAM:
  620. info->use_spare = 0;
  621. break;
  622. default:
  623. info->ndcb1 = 0;
  624. info->ndcb2 = 0;
  625. break;
  626. }
  627. /*
  628. * If we are about to issue a read command, or about to set
  629. * the write address, then clean the data buffer.
  630. */
  631. if (command == NAND_CMD_READ0 ||
  632. command == NAND_CMD_READOOB ||
  633. command == NAND_CMD_SEQIN) {
  634. info->buf_count = mtd->writesize + mtd->oobsize;
  635. memset(info->data_buff, 0xFF, info->buf_count);
  636. }
  637. }
  638. static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
  639. int ext_cmd_type, uint16_t column, int page_addr)
  640. {
  641. int addr_cycle, exec_cmd;
  642. struct pxa3xx_nand_host *host;
  643. struct mtd_info *mtd;
  644. host = info->host[info->cs];
  645. mtd = host->mtd;
  646. addr_cycle = 0;
  647. exec_cmd = 1;
  648. if (info->cs != 0)
  649. info->ndcb0 = NDCB0_CSEL;
  650. else
  651. info->ndcb0 = 0;
  652. if (command == NAND_CMD_SEQIN)
  653. exec_cmd = 0;
  654. addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
  655. + host->col_addr_cycles);
  656. switch (command) {
  657. case NAND_CMD_READOOB:
  658. case NAND_CMD_READ0:
  659. info->buf_start = column;
  660. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  661. | addr_cycle
  662. | NAND_CMD_READ0;
  663. if (command == NAND_CMD_READOOB)
  664. info->buf_start += mtd->writesize;
  665. /*
  666. * Multiple page read needs an 'extended command type' field,
  667. * which is either naked-read or last-read according to the
  668. * state.
  669. */
  670. if (mtd->writesize == PAGE_CHUNK_SIZE) {
  671. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
  672. } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
  673. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
  674. | NDCB0_LEN_OVRD
  675. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  676. info->ndcb3 = info->chunk_size +
  677. info->oob_size;
  678. }
  679. set_command_address(info, mtd->writesize, column, page_addr);
  680. break;
  681. case NAND_CMD_SEQIN:
  682. info->buf_start = column;
  683. set_command_address(info, mtd->writesize, 0, page_addr);
  684. /*
  685. * Multiple page programming needs to execute the initial
  686. * SEQIN command that sets the page address.
  687. */
  688. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  689. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  690. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  691. | addr_cycle
  692. | command;
  693. /* No data transfer in this case */
  694. info->data_size = 0;
  695. exec_cmd = 1;
  696. }
  697. break;
  698. case NAND_CMD_PAGEPROG:
  699. if (is_buf_blank(info->data_buff,
  700. (mtd->writesize + mtd->oobsize))) {
  701. exec_cmd = 0;
  702. break;
  703. }
  704. /* Second command setting for large pages */
  705. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  706. /*
  707. * Multiple page write uses the 'extended command'
  708. * field. This can be used to issue a command dispatch
  709. * or a naked-write depending on the current stage.
  710. */
  711. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  712. | NDCB0_LEN_OVRD
  713. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  714. info->ndcb3 = info->chunk_size +
  715. info->oob_size;
  716. /*
  717. * This is the command dispatch that completes a chunked
  718. * page program operation.
  719. */
  720. if (info->data_size == 0) {
  721. info->ndcb0 = NDCB0_CMD_TYPE(0x1)
  722. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  723. | command;
  724. info->ndcb1 = 0;
  725. info->ndcb2 = 0;
  726. info->ndcb3 = 0;
  727. }
  728. } else {
  729. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  730. | NDCB0_AUTO_RS
  731. | NDCB0_ST_ROW_EN
  732. | NDCB0_DBC
  733. | (NAND_CMD_PAGEPROG << 8)
  734. | NAND_CMD_SEQIN
  735. | addr_cycle;
  736. }
  737. break;
  738. case NAND_CMD_PARAM:
  739. info->buf_count = 256;
  740. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  741. | NDCB0_ADDR_CYC(1)
  742. | NDCB0_LEN_OVRD
  743. | command;
  744. info->ndcb1 = (column & 0xFF);
  745. info->ndcb3 = 256;
  746. info->data_size = 256;
  747. break;
  748. case NAND_CMD_READID:
  749. info->buf_count = host->read_id_bytes;
  750. info->ndcb0 |= NDCB0_CMD_TYPE(3)
  751. | NDCB0_ADDR_CYC(1)
  752. | command;
  753. info->ndcb1 = (column & 0xFF);
  754. info->data_size = 8;
  755. break;
  756. case NAND_CMD_STATUS:
  757. info->buf_count = 1;
  758. info->ndcb0 |= NDCB0_CMD_TYPE(4)
  759. | NDCB0_ADDR_CYC(1)
  760. | command;
  761. info->data_size = 8;
  762. break;
  763. case NAND_CMD_ERASE1:
  764. info->ndcb0 |= NDCB0_CMD_TYPE(2)
  765. | NDCB0_AUTO_RS
  766. | NDCB0_ADDR_CYC(3)
  767. | NDCB0_DBC
  768. | (NAND_CMD_ERASE2 << 8)
  769. | NAND_CMD_ERASE1;
  770. info->ndcb1 = page_addr;
  771. info->ndcb2 = 0;
  772. break;
  773. case NAND_CMD_RESET:
  774. info->ndcb0 |= NDCB0_CMD_TYPE(5)
  775. | command;
  776. break;
  777. case NAND_CMD_ERASE2:
  778. exec_cmd = 0;
  779. break;
  780. default:
  781. exec_cmd = 0;
  782. dev_err(&info->pdev->dev, "non-supported command %x\n",
  783. command);
  784. break;
  785. }
  786. return exec_cmd;
  787. }
  788. static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  789. int column, int page_addr)
  790. {
  791. struct pxa3xx_nand_host *host = mtd->priv;
  792. struct pxa3xx_nand_info *info = host->info_data;
  793. int ret, exec_cmd;
  794. /*
  795. * if this is a x16 device ,then convert the input
  796. * "byte" address into a "word" address appropriate
  797. * for indexing a word-oriented device
  798. */
  799. if (info->reg_ndcr & NDCR_DWIDTH_M)
  800. column /= 2;
  801. /*
  802. * There may be different NAND chip hooked to
  803. * different chip select, so check whether
  804. * chip select has been changed, if yes, reset the timing
  805. */
  806. if (info->cs != host->cs) {
  807. info->cs = host->cs;
  808. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  809. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  810. }
  811. prepare_start_command(info, command);
  812. info->state = STATE_PREPARED;
  813. exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
  814. if (exec_cmd) {
  815. init_completion(&info->cmd_complete);
  816. init_completion(&info->dev_ready);
  817. info->need_wait = 1;
  818. pxa3xx_nand_start(info);
  819. ret = wait_for_completion_timeout(&info->cmd_complete,
  820. CHIP_DELAY_TIMEOUT);
  821. if (!ret) {
  822. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  823. /* Stop State Machine for next command cycle */
  824. pxa3xx_nand_stop(info);
  825. }
  826. }
  827. info->state = STATE_IDLE;
  828. }
  829. static void nand_cmdfunc_extended(struct mtd_info *mtd,
  830. const unsigned command,
  831. int column, int page_addr)
  832. {
  833. struct pxa3xx_nand_host *host = mtd->priv;
  834. struct pxa3xx_nand_info *info = host->info_data;
  835. int ret, exec_cmd, ext_cmd_type;
  836. /*
  837. * if this is a x16 device then convert the input
  838. * "byte" address into a "word" address appropriate
  839. * for indexing a word-oriented device
  840. */
  841. if (info->reg_ndcr & NDCR_DWIDTH_M)
  842. column /= 2;
  843. /*
  844. * There may be different NAND chip hooked to
  845. * different chip select, so check whether
  846. * chip select has been changed, if yes, reset the timing
  847. */
  848. if (info->cs != host->cs) {
  849. info->cs = host->cs;
  850. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  851. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  852. }
  853. /* Select the extended command for the first command */
  854. switch (command) {
  855. case NAND_CMD_READ0:
  856. case NAND_CMD_READOOB:
  857. ext_cmd_type = EXT_CMD_TYPE_MONO;
  858. break;
  859. case NAND_CMD_SEQIN:
  860. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  861. break;
  862. case NAND_CMD_PAGEPROG:
  863. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  864. break;
  865. default:
  866. ext_cmd_type = 0;
  867. break;
  868. }
  869. prepare_start_command(info, command);
  870. /*
  871. * Prepare the "is ready" completion before starting a command
  872. * transaction sequence. If the command is not executed the
  873. * completion will be completed, see below.
  874. *
  875. * We can do that inside the loop because the command variable
  876. * is invariant and thus so is the exec_cmd.
  877. */
  878. info->need_wait = 1;
  879. init_completion(&info->dev_ready);
  880. do {
  881. info->state = STATE_PREPARED;
  882. exec_cmd = prepare_set_command(info, command, ext_cmd_type,
  883. column, page_addr);
  884. if (!exec_cmd) {
  885. info->need_wait = 0;
  886. complete(&info->dev_ready);
  887. break;
  888. }
  889. init_completion(&info->cmd_complete);
  890. pxa3xx_nand_start(info);
  891. ret = wait_for_completion_timeout(&info->cmd_complete,
  892. CHIP_DELAY_TIMEOUT);
  893. if (!ret) {
  894. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  895. /* Stop State Machine for next command cycle */
  896. pxa3xx_nand_stop(info);
  897. break;
  898. }
  899. /* Check if the sequence is complete */
  900. if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
  901. break;
  902. /*
  903. * After a splitted program command sequence has issued
  904. * the command dispatch, the command sequence is complete.
  905. */
  906. if (info->data_size == 0 &&
  907. command == NAND_CMD_PAGEPROG &&
  908. ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
  909. break;
  910. if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
  911. /* Last read: issue a 'last naked read' */
  912. if (info->data_size == info->chunk_size)
  913. ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
  914. else
  915. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  916. /*
  917. * If a splitted program command has no more data to transfer,
  918. * the command dispatch must be issued to complete.
  919. */
  920. } else if (command == NAND_CMD_PAGEPROG &&
  921. info->data_size == 0) {
  922. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  923. }
  924. } while (1);
  925. info->state = STATE_IDLE;
  926. }
  927. static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
  928. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  929. {
  930. chip->write_buf(mtd, buf, mtd->writesize);
  931. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  932. return 0;
  933. }
  934. static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
  935. struct nand_chip *chip, uint8_t *buf, int oob_required,
  936. int page)
  937. {
  938. struct pxa3xx_nand_host *host = mtd->priv;
  939. struct pxa3xx_nand_info *info = host->info_data;
  940. chip->read_buf(mtd, buf, mtd->writesize);
  941. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  942. if (info->retcode == ERR_CORERR && info->use_ecc) {
  943. mtd->ecc_stats.corrected += info->ecc_err_cnt;
  944. } else if (info->retcode == ERR_UNCORERR) {
  945. /*
  946. * for blank page (all 0xff), HW will calculate its ECC as
  947. * 0, which is different from the ECC information within
  948. * OOB, ignore such uncorrectable errors
  949. */
  950. if (is_buf_blank(buf, mtd->writesize))
  951. info->retcode = ERR_NONE;
  952. else
  953. mtd->ecc_stats.failed++;
  954. }
  955. return info->max_bitflips;
  956. }
  957. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  958. {
  959. struct pxa3xx_nand_host *host = mtd->priv;
  960. struct pxa3xx_nand_info *info = host->info_data;
  961. char retval = 0xFF;
  962. if (info->buf_start < info->buf_count)
  963. /* Has just send a new command? */
  964. retval = info->data_buff[info->buf_start++];
  965. return retval;
  966. }
  967. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  968. {
  969. struct pxa3xx_nand_host *host = mtd->priv;
  970. struct pxa3xx_nand_info *info = host->info_data;
  971. u16 retval = 0xFFFF;
  972. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  973. retval = *((u16 *)(info->data_buff+info->buf_start));
  974. info->buf_start += 2;
  975. }
  976. return retval;
  977. }
  978. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  979. {
  980. struct pxa3xx_nand_host *host = mtd->priv;
  981. struct pxa3xx_nand_info *info = host->info_data;
  982. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  983. memcpy(buf, info->data_buff + info->buf_start, real_len);
  984. info->buf_start += real_len;
  985. }
  986. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  987. const uint8_t *buf, int len)
  988. {
  989. struct pxa3xx_nand_host *host = mtd->priv;
  990. struct pxa3xx_nand_info *info = host->info_data;
  991. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  992. memcpy(info->data_buff + info->buf_start, buf, real_len);
  993. info->buf_start += real_len;
  994. }
  995. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  996. {
  997. return;
  998. }
  999. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  1000. {
  1001. struct pxa3xx_nand_host *host = mtd->priv;
  1002. struct pxa3xx_nand_info *info = host->info_data;
  1003. int ret;
  1004. if (info->need_wait) {
  1005. ret = wait_for_completion_timeout(&info->dev_ready,
  1006. CHIP_DELAY_TIMEOUT);
  1007. info->need_wait = 0;
  1008. if (!ret) {
  1009. dev_err(&info->pdev->dev, "Ready time out!!!\n");
  1010. return NAND_STATUS_FAIL;
  1011. }
  1012. }
  1013. /* pxa3xx_nand_send_command has waited for command complete */
  1014. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  1015. if (info->retcode == ERR_NONE)
  1016. return 0;
  1017. else
  1018. return NAND_STATUS_FAIL;
  1019. }
  1020. return NAND_STATUS_READY;
  1021. }
  1022. static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
  1023. const struct pxa3xx_nand_flash *f)
  1024. {
  1025. struct platform_device *pdev = info->pdev;
  1026. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1027. struct pxa3xx_nand_host *host = info->host[info->cs];
  1028. uint32_t ndcr = 0x0; /* enable all interrupts */
  1029. if (f->page_size != 2048 && f->page_size != 512) {
  1030. dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
  1031. return -EINVAL;
  1032. }
  1033. if (f->flash_width != 16 && f->flash_width != 8) {
  1034. dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
  1035. return -EINVAL;
  1036. }
  1037. /* calculate flash information */
  1038. host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
  1039. /* calculate addressing information */
  1040. host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
  1041. if (f->num_blocks * f->page_per_block > 65536)
  1042. host->row_addr_cycles = 3;
  1043. else
  1044. host->row_addr_cycles = 2;
  1045. ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  1046. ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  1047. ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
  1048. ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
  1049. ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
  1050. ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
  1051. ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
  1052. ndcr |= NDCR_SPARE_EN; /* enable spare by default */
  1053. info->reg_ndcr = ndcr;
  1054. pxa3xx_nand_set_timing(host, f->timing);
  1055. return 0;
  1056. }
  1057. static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  1058. {
  1059. /*
  1060. * We set 0 by hard coding here, for we don't support keep_config
  1061. * when there is more than one chip attached to the controller
  1062. */
  1063. struct pxa3xx_nand_host *host = info->host[0];
  1064. uint32_t ndcr = nand_readl(info, NDCR);
  1065. if (ndcr & NDCR_PAGE_SZ) {
  1066. /* Controller's FIFO size */
  1067. info->chunk_size = 2048;
  1068. host->read_id_bytes = 4;
  1069. } else {
  1070. info->chunk_size = 512;
  1071. host->read_id_bytes = 2;
  1072. }
  1073. /* Set an initial chunk size */
  1074. info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
  1075. info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
  1076. info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
  1077. return 0;
  1078. }
  1079. #ifdef ARCH_HAS_DMA
  1080. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  1081. {
  1082. struct platform_device *pdev = info->pdev;
  1083. int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
  1084. if (use_dma == 0) {
  1085. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1086. if (info->data_buff == NULL)
  1087. return -ENOMEM;
  1088. return 0;
  1089. }
  1090. info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
  1091. &info->data_buff_phys, GFP_KERNEL);
  1092. if (info->data_buff == NULL) {
  1093. dev_err(&pdev->dev, "failed to allocate dma buffer\n");
  1094. return -ENOMEM;
  1095. }
  1096. info->data_desc = (void *)info->data_buff + data_desc_offset;
  1097. info->data_desc_addr = info->data_buff_phys + data_desc_offset;
  1098. info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
  1099. pxa3xx_nand_data_dma_irq, info);
  1100. if (info->data_dma_ch < 0) {
  1101. dev_err(&pdev->dev, "failed to request data dma\n");
  1102. dma_free_coherent(&pdev->dev, info->buf_size,
  1103. info->data_buff, info->data_buff_phys);
  1104. return info->data_dma_ch;
  1105. }
  1106. /*
  1107. * Now that DMA buffers are allocated we turn on
  1108. * DMA proper for I/O operations.
  1109. */
  1110. info->use_dma = 1;
  1111. return 0;
  1112. }
  1113. static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
  1114. {
  1115. struct platform_device *pdev = info->pdev;
  1116. if (info->use_dma) {
  1117. pxa_free_dma(info->data_dma_ch);
  1118. dma_free_coherent(&pdev->dev, info->buf_size,
  1119. info->data_buff, info->data_buff_phys);
  1120. } else {
  1121. kfree(info->data_buff);
  1122. }
  1123. }
  1124. #else
  1125. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  1126. {
  1127. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1128. if (info->data_buff == NULL)
  1129. return -ENOMEM;
  1130. return 0;
  1131. }
  1132. static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
  1133. {
  1134. kfree(info->data_buff);
  1135. }
  1136. #endif
  1137. static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
  1138. {
  1139. struct mtd_info *mtd;
  1140. struct nand_chip *chip;
  1141. int ret;
  1142. mtd = info->host[info->cs]->mtd;
  1143. chip = mtd->priv;
  1144. /* use the common timing to make a try */
  1145. ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
  1146. if (ret)
  1147. return ret;
  1148. chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
  1149. ret = chip->waitfunc(mtd, chip);
  1150. if (ret & NAND_STATUS_FAIL)
  1151. return -ENODEV;
  1152. return 0;
  1153. }
  1154. static int pxa_ecc_init(struct pxa3xx_nand_info *info,
  1155. struct nand_ecc_ctrl *ecc,
  1156. int strength, int ecc_stepsize, int page_size)
  1157. {
  1158. if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
  1159. info->chunk_size = 2048;
  1160. info->spare_size = 40;
  1161. info->ecc_size = 24;
  1162. ecc->mode = NAND_ECC_HW;
  1163. ecc->size = 512;
  1164. ecc->strength = 1;
  1165. return 1;
  1166. } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
  1167. info->chunk_size = 512;
  1168. info->spare_size = 8;
  1169. info->ecc_size = 8;
  1170. ecc->mode = NAND_ECC_HW;
  1171. ecc->size = 512;
  1172. ecc->strength = 1;
  1173. return 1;
  1174. /*
  1175. * Required ECC: 4-bit correction per 512 bytes
  1176. * Select: 16-bit correction per 2048 bytes
  1177. */
  1178. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
  1179. info->ecc_bch = 1;
  1180. info->chunk_size = 2048;
  1181. info->spare_size = 32;
  1182. info->ecc_size = 32;
  1183. ecc->mode = NAND_ECC_HW;
  1184. ecc->size = info->chunk_size;
  1185. ecc->layout = &ecc_layout_2KB_bch4bit;
  1186. ecc->strength = 16;
  1187. return 1;
  1188. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
  1189. info->ecc_bch = 1;
  1190. info->chunk_size = 2048;
  1191. info->spare_size = 32;
  1192. info->ecc_size = 32;
  1193. ecc->mode = NAND_ECC_HW;
  1194. ecc->size = info->chunk_size;
  1195. ecc->layout = &ecc_layout_4KB_bch4bit;
  1196. ecc->strength = 16;
  1197. return 1;
  1198. /*
  1199. * Required ECC: 8-bit correction per 512 bytes
  1200. * Select: 16-bit correction per 1024 bytes
  1201. */
  1202. } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
  1203. info->ecc_bch = 1;
  1204. info->chunk_size = 1024;
  1205. info->spare_size = 0;
  1206. info->ecc_size = 32;
  1207. ecc->mode = NAND_ECC_HW;
  1208. ecc->size = info->chunk_size;
  1209. ecc->layout = &ecc_layout_4KB_bch8bit;
  1210. ecc->strength = 16;
  1211. return 1;
  1212. }
  1213. return 0;
  1214. }
  1215. static int pxa3xx_nand_scan(struct mtd_info *mtd)
  1216. {
  1217. struct pxa3xx_nand_host *host = mtd->priv;
  1218. struct pxa3xx_nand_info *info = host->info_data;
  1219. struct platform_device *pdev = info->pdev;
  1220. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1221. struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
  1222. const struct pxa3xx_nand_flash *f = NULL;
  1223. struct nand_chip *chip = mtd->priv;
  1224. uint32_t id = -1;
  1225. uint64_t chipsize;
  1226. int i, ret, num;
  1227. uint16_t ecc_strength, ecc_step;
  1228. if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
  1229. goto KEEP_CONFIG;
  1230. ret = pxa3xx_nand_sensing(info);
  1231. if (ret) {
  1232. dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
  1233. info->cs);
  1234. return ret;
  1235. }
  1236. chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
  1237. id = *((uint16_t *)(info->data_buff));
  1238. if (id != 0)
  1239. dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
  1240. else {
  1241. dev_warn(&info->pdev->dev,
  1242. "Read out ID 0, potential timing set wrong!!\n");
  1243. return -EINVAL;
  1244. }
  1245. num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
  1246. for (i = 0; i < num; i++) {
  1247. if (i < pdata->num_flash)
  1248. f = pdata->flash + i;
  1249. else
  1250. f = &builtin_flash_types[i - pdata->num_flash + 1];
  1251. /* find the chip in default list */
  1252. if (f->chip_id == id)
  1253. break;
  1254. }
  1255. if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
  1256. dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
  1257. return -EINVAL;
  1258. }
  1259. ret = pxa3xx_nand_config_flash(info, f);
  1260. if (ret) {
  1261. dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
  1262. return ret;
  1263. }
  1264. pxa3xx_flash_ids[0].name = f->name;
  1265. pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
  1266. pxa3xx_flash_ids[0].pagesize = f->page_size;
  1267. chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
  1268. pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
  1269. pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
  1270. if (f->flash_width == 16)
  1271. pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
  1272. pxa3xx_flash_ids[1].name = NULL;
  1273. def = pxa3xx_flash_ids;
  1274. KEEP_CONFIG:
  1275. if (info->reg_ndcr & NDCR_DWIDTH_M)
  1276. chip->options |= NAND_BUSWIDTH_16;
  1277. /* Device detection must be done with ECC disabled */
  1278. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  1279. nand_writel(info, NDECCCTRL, 0x0);
  1280. if (nand_scan_ident(mtd, 1, def))
  1281. return -ENODEV;
  1282. if (pdata->flash_bbt) {
  1283. /*
  1284. * We'll use a bad block table stored in-flash and don't
  1285. * allow writing the bad block marker to the flash.
  1286. */
  1287. chip->bbt_options |= NAND_BBT_USE_FLASH |
  1288. NAND_BBT_NO_OOB_BBM;
  1289. chip->bbt_td = &bbt_main_descr;
  1290. chip->bbt_md = &bbt_mirror_descr;
  1291. }
  1292. /*
  1293. * If the page size is bigger than the FIFO size, let's check
  1294. * we are given the right variant and then switch to the extended
  1295. * (aka splitted) command handling,
  1296. */
  1297. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  1298. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
  1299. chip->cmdfunc = nand_cmdfunc_extended;
  1300. } else {
  1301. dev_err(&info->pdev->dev,
  1302. "unsupported page size on this variant\n");
  1303. return -ENODEV;
  1304. }
  1305. }
  1306. ecc_strength = chip->ecc_strength_ds;
  1307. ecc_step = chip->ecc_step_ds;
  1308. /* Set default ECC strength requirements on non-ONFI devices */
  1309. if (ecc_strength < 1 && ecc_step < 1) {
  1310. ecc_strength = 1;
  1311. ecc_step = 512;
  1312. }
  1313. ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
  1314. ecc_step, mtd->writesize);
  1315. if (!ret) {
  1316. dev_err(&info->pdev->dev,
  1317. "ECC strength %d at page size %d is not supported\n",
  1318. chip->ecc_strength_ds, mtd->writesize);
  1319. return -ENODEV;
  1320. }
  1321. /* calculate addressing information */
  1322. if (mtd->writesize >= 2048)
  1323. host->col_addr_cycles = 2;
  1324. else
  1325. host->col_addr_cycles = 1;
  1326. /* release the initial buffer */
  1327. kfree(info->data_buff);
  1328. /* allocate the real data + oob buffer */
  1329. info->buf_size = mtd->writesize + mtd->oobsize;
  1330. ret = pxa3xx_nand_init_buff(info);
  1331. if (ret)
  1332. return ret;
  1333. info->oob_buff = info->data_buff + mtd->writesize;
  1334. if ((mtd->size >> chip->page_shift) > 65536)
  1335. host->row_addr_cycles = 3;
  1336. else
  1337. host->row_addr_cycles = 2;
  1338. return nand_scan_tail(mtd);
  1339. }
  1340. static int alloc_nand_resource(struct platform_device *pdev)
  1341. {
  1342. struct pxa3xx_nand_platform_data *pdata;
  1343. struct pxa3xx_nand_info *info;
  1344. struct pxa3xx_nand_host *host;
  1345. struct nand_chip *chip = NULL;
  1346. struct mtd_info *mtd;
  1347. struct resource *r;
  1348. int ret, irq, cs;
  1349. pdata = dev_get_platdata(&pdev->dev);
  1350. info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
  1351. sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
  1352. if (!info)
  1353. return -ENOMEM;
  1354. info->pdev = pdev;
  1355. info->variant = pxa3xx_nand_get_variant(pdev);
  1356. for (cs = 0; cs < pdata->num_cs; cs++) {
  1357. mtd = (struct mtd_info *)((unsigned int)&info[1] +
  1358. (sizeof(*mtd) + sizeof(*host)) * cs);
  1359. chip = (struct nand_chip *)(&mtd[1]);
  1360. host = (struct pxa3xx_nand_host *)chip;
  1361. info->host[cs] = host;
  1362. host->mtd = mtd;
  1363. host->cs = cs;
  1364. host->info_data = info;
  1365. mtd->priv = host;
  1366. mtd->owner = THIS_MODULE;
  1367. chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
  1368. chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
  1369. chip->controller = &info->controller;
  1370. chip->waitfunc = pxa3xx_nand_waitfunc;
  1371. chip->select_chip = pxa3xx_nand_select_chip;
  1372. chip->read_word = pxa3xx_nand_read_word;
  1373. chip->read_byte = pxa3xx_nand_read_byte;
  1374. chip->read_buf = pxa3xx_nand_read_buf;
  1375. chip->write_buf = pxa3xx_nand_write_buf;
  1376. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1377. chip->cmdfunc = nand_cmdfunc;
  1378. }
  1379. spin_lock_init(&chip->controller->lock);
  1380. init_waitqueue_head(&chip->controller->wq);
  1381. info->clk = devm_clk_get(&pdev->dev, NULL);
  1382. if (IS_ERR(info->clk)) {
  1383. dev_err(&pdev->dev, "failed to get nand clock\n");
  1384. return PTR_ERR(info->clk);
  1385. }
  1386. ret = clk_prepare_enable(info->clk);
  1387. if (ret < 0)
  1388. return ret;
  1389. if (use_dma) {
  1390. /*
  1391. * This is a dirty hack to make this driver work from
  1392. * devicetree bindings. It can be removed once we have
  1393. * a prober DMA controller framework for DT.
  1394. */
  1395. if (pdev->dev.of_node &&
  1396. of_machine_is_compatible("marvell,pxa3xx")) {
  1397. info->drcmr_dat = 97;
  1398. info->drcmr_cmd = 99;
  1399. } else {
  1400. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1401. if (r == NULL) {
  1402. dev_err(&pdev->dev,
  1403. "no resource defined for data DMA\n");
  1404. ret = -ENXIO;
  1405. goto fail_disable_clk;
  1406. }
  1407. info->drcmr_dat = r->start;
  1408. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  1409. if (r == NULL) {
  1410. dev_err(&pdev->dev,
  1411. "no resource defined for cmd DMA\n");
  1412. ret = -ENXIO;
  1413. goto fail_disable_clk;
  1414. }
  1415. info->drcmr_cmd = r->start;
  1416. }
  1417. }
  1418. irq = platform_get_irq(pdev, 0);
  1419. if (irq < 0) {
  1420. dev_err(&pdev->dev, "no IRQ resource defined\n");
  1421. ret = -ENXIO;
  1422. goto fail_disable_clk;
  1423. }
  1424. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1425. info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  1426. if (IS_ERR(info->mmio_base)) {
  1427. ret = PTR_ERR(info->mmio_base);
  1428. goto fail_disable_clk;
  1429. }
  1430. info->mmio_phys = r->start;
  1431. /* Allocate a buffer to allow flash detection */
  1432. info->buf_size = INIT_BUFFER_SIZE;
  1433. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1434. if (info->data_buff == NULL) {
  1435. ret = -ENOMEM;
  1436. goto fail_disable_clk;
  1437. }
  1438. /* initialize all interrupts to be disabled */
  1439. disable_int(info, NDSR_MASK);
  1440. ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
  1441. if (ret < 0) {
  1442. dev_err(&pdev->dev, "failed to request IRQ\n");
  1443. goto fail_free_buf;
  1444. }
  1445. platform_set_drvdata(pdev, info);
  1446. return 0;
  1447. fail_free_buf:
  1448. free_irq(irq, info);
  1449. kfree(info->data_buff);
  1450. fail_disable_clk:
  1451. clk_disable_unprepare(info->clk);
  1452. return ret;
  1453. }
  1454. static int pxa3xx_nand_remove(struct platform_device *pdev)
  1455. {
  1456. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1457. struct pxa3xx_nand_platform_data *pdata;
  1458. int irq, cs;
  1459. if (!info)
  1460. return 0;
  1461. pdata = dev_get_platdata(&pdev->dev);
  1462. irq = platform_get_irq(pdev, 0);
  1463. if (irq >= 0)
  1464. free_irq(irq, info);
  1465. pxa3xx_nand_free_buff(info);
  1466. clk_disable_unprepare(info->clk);
  1467. for (cs = 0; cs < pdata->num_cs; cs++)
  1468. nand_release(info->host[cs]->mtd);
  1469. return 0;
  1470. }
  1471. static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
  1472. {
  1473. struct pxa3xx_nand_platform_data *pdata;
  1474. struct device_node *np = pdev->dev.of_node;
  1475. const struct of_device_id *of_id =
  1476. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  1477. if (!of_id)
  1478. return 0;
  1479. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1480. if (!pdata)
  1481. return -ENOMEM;
  1482. if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
  1483. pdata->enable_arbiter = 1;
  1484. if (of_get_property(np, "marvell,nand-keep-config", NULL))
  1485. pdata->keep_config = 1;
  1486. of_property_read_u32(np, "num-cs", &pdata->num_cs);
  1487. pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
  1488. pdev->dev.platform_data = pdata;
  1489. return 0;
  1490. }
  1491. static int pxa3xx_nand_probe(struct platform_device *pdev)
  1492. {
  1493. struct pxa3xx_nand_platform_data *pdata;
  1494. struct mtd_part_parser_data ppdata = {};
  1495. struct pxa3xx_nand_info *info;
  1496. int ret, cs, probe_success;
  1497. #ifndef ARCH_HAS_DMA
  1498. if (use_dma) {
  1499. use_dma = 0;
  1500. dev_warn(&pdev->dev,
  1501. "This platform can't do DMA on this device\n");
  1502. }
  1503. #endif
  1504. ret = pxa3xx_nand_probe_dt(pdev);
  1505. if (ret)
  1506. return ret;
  1507. pdata = dev_get_platdata(&pdev->dev);
  1508. if (!pdata) {
  1509. dev_err(&pdev->dev, "no platform data defined\n");
  1510. return -ENODEV;
  1511. }
  1512. ret = alloc_nand_resource(pdev);
  1513. if (ret) {
  1514. dev_err(&pdev->dev, "alloc nand resource failed\n");
  1515. return ret;
  1516. }
  1517. info = platform_get_drvdata(pdev);
  1518. probe_success = 0;
  1519. for (cs = 0; cs < pdata->num_cs; cs++) {
  1520. struct mtd_info *mtd = info->host[cs]->mtd;
  1521. /*
  1522. * The mtd name matches the one used in 'mtdparts' kernel
  1523. * parameter. This name cannot be changed or otherwise
  1524. * user's mtd partitions configuration would get broken.
  1525. */
  1526. mtd->name = "pxa3xx_nand-0";
  1527. info->cs = cs;
  1528. ret = pxa3xx_nand_scan(mtd);
  1529. if (ret) {
  1530. dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
  1531. cs);
  1532. continue;
  1533. }
  1534. ppdata.of_node = pdev->dev.of_node;
  1535. ret = mtd_device_parse_register(mtd, NULL,
  1536. &ppdata, pdata->parts[cs],
  1537. pdata->nr_parts[cs]);
  1538. if (!ret)
  1539. probe_success = 1;
  1540. }
  1541. if (!probe_success) {
  1542. pxa3xx_nand_remove(pdev);
  1543. return -ENODEV;
  1544. }
  1545. return 0;
  1546. }
  1547. #ifdef CONFIG_PM
  1548. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1549. {
  1550. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1551. struct pxa3xx_nand_platform_data *pdata;
  1552. struct mtd_info *mtd;
  1553. int cs;
  1554. pdata = dev_get_platdata(&pdev->dev);
  1555. if (info->state) {
  1556. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1557. return -EAGAIN;
  1558. }
  1559. for (cs = 0; cs < pdata->num_cs; cs++) {
  1560. mtd = info->host[cs]->mtd;
  1561. mtd_suspend(mtd);
  1562. }
  1563. return 0;
  1564. }
  1565. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1566. {
  1567. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1568. struct pxa3xx_nand_platform_data *pdata;
  1569. struct mtd_info *mtd;
  1570. int cs;
  1571. pdata = dev_get_platdata(&pdev->dev);
  1572. /* We don't want to handle interrupt without calling mtd routine */
  1573. disable_int(info, NDCR_INT_MASK);
  1574. /*
  1575. * Directly set the chip select to a invalid value,
  1576. * then the driver would reset the timing according
  1577. * to current chip select at the beginning of cmdfunc
  1578. */
  1579. info->cs = 0xff;
  1580. /*
  1581. * As the spec says, the NDSR would be updated to 0x1800 when
  1582. * doing the nand_clk disable/enable.
  1583. * To prevent it damaging state machine of the driver, clear
  1584. * all status before resume
  1585. */
  1586. nand_writel(info, NDSR, NDSR_MASK);
  1587. for (cs = 0; cs < pdata->num_cs; cs++) {
  1588. mtd = info->host[cs]->mtd;
  1589. mtd_resume(mtd);
  1590. }
  1591. return 0;
  1592. }
  1593. #else
  1594. #define pxa3xx_nand_suspend NULL
  1595. #define pxa3xx_nand_resume NULL
  1596. #endif
  1597. static struct platform_driver pxa3xx_nand_driver = {
  1598. .driver = {
  1599. .name = "pxa3xx-nand",
  1600. .of_match_table = pxa3xx_nand_dt_ids,
  1601. },
  1602. .probe = pxa3xx_nand_probe,
  1603. .remove = pxa3xx_nand_remove,
  1604. .suspend = pxa3xx_nand_suspend,
  1605. .resume = pxa3xx_nand_resume,
  1606. };
  1607. module_platform_driver(pxa3xx_nand_driver);
  1608. MODULE_LICENSE("GPL");
  1609. MODULE_DESCRIPTION("PXA3xx NAND controller driver");