pxa3xx_nand.c 52 KB

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