pxa3xx_nand.c 49 KB

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