pxa3xx_nand.c 50 KB

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