pxa3xx_nand.c 52 KB

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