denali.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*
  2. * Copyright (C) 2014 Panasonic Corporation
  3. * Copyright (C) 2013-2014, Altera Corporation <www.altera.com>
  4. * Copyright (C) 2009-2010, Intel Corporation and its suppliers.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <nand.h>
  11. #include <asm/errno.h>
  12. #include <asm/io.h>
  13. #include "denali.h"
  14. #define NAND_DEFAULT_TIMINGS -1
  15. static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
  16. /*
  17. * We define a macro here that combines all interrupts this driver uses into
  18. * a single constant value, for convenience.
  19. */
  20. #define DENALI_IRQ_ALL (INTR_STATUS__DMA_CMD_COMP | \
  21. INTR_STATUS__ECC_TRANSACTION_DONE | \
  22. INTR_STATUS__ECC_ERR | \
  23. INTR_STATUS__PROGRAM_FAIL | \
  24. INTR_STATUS__LOAD_COMP | \
  25. INTR_STATUS__PROGRAM_COMP | \
  26. INTR_STATUS__TIME_OUT | \
  27. INTR_STATUS__ERASE_FAIL | \
  28. INTR_STATUS__RST_COMP | \
  29. INTR_STATUS__ERASE_COMP | \
  30. INTR_STATUS__ECC_UNCOR_ERR | \
  31. INTR_STATUS__INT_ACT | \
  32. INTR_STATUS__LOCKED_BLK)
  33. /*
  34. * indicates whether or not the internal value for the flash bank is
  35. * valid or not
  36. */
  37. #define CHIP_SELECT_INVALID -1
  38. #define SUPPORT_8BITECC 1
  39. /*
  40. * this macro allows us to convert from an MTD structure to our own
  41. * device context (denali) structure.
  42. */
  43. #define mtd_to_denali(m) \
  44. container_of(mtd_to_nand(m), struct denali_nand_info, nand)
  45. /*
  46. * These constants are defined by the driver to enable common driver
  47. * configuration options.
  48. */
  49. #define SPARE_ACCESS 0x41
  50. #define MAIN_ACCESS 0x42
  51. #define MAIN_SPARE_ACCESS 0x43
  52. #define PIPELINE_ACCESS 0x2000
  53. #define DENALI_UNLOCK_START 0x10
  54. #define DENALI_UNLOCK_END 0x11
  55. #define DENALI_LOCK 0x21
  56. #define DENALI_LOCK_TIGHT 0x31
  57. #define DENALI_BUFFER_LOAD 0x60
  58. #define DENALI_BUFFER_WRITE 0x62
  59. #define DENALI_READ 0
  60. #define DENALI_WRITE 0x100
  61. /* types of device accesses. We can issue commands and get status */
  62. #define COMMAND_CYCLE 0
  63. #define ADDR_CYCLE 1
  64. #define STATUS_CYCLE 2
  65. /*
  66. * this is a helper macro that allows us to
  67. * format the bank into the proper bits for the controller
  68. */
  69. #define BANK(x) ((x) << 24)
  70. /* Interrupts are cleared by writing a 1 to the appropriate status bit */
  71. static inline void clear_interrupt(struct denali_nand_info *denali,
  72. uint32_t irq_mask)
  73. {
  74. uint32_t intr_status_reg;
  75. intr_status_reg = INTR_STATUS(denali->flash_bank);
  76. writel(irq_mask, denali->flash_reg + intr_status_reg);
  77. }
  78. static uint32_t read_interrupt_status(struct denali_nand_info *denali)
  79. {
  80. uint32_t intr_status_reg;
  81. intr_status_reg = INTR_STATUS(denali->flash_bank);
  82. return readl(denali->flash_reg + intr_status_reg);
  83. }
  84. static void clear_interrupts(struct denali_nand_info *denali)
  85. {
  86. uint32_t status;
  87. status = read_interrupt_status(denali);
  88. clear_interrupt(denali, status);
  89. denali->irq_status = 0;
  90. }
  91. static void denali_irq_enable(struct denali_nand_info *denali,
  92. uint32_t int_mask)
  93. {
  94. int i;
  95. for (i = 0; i < denali->max_banks; ++i)
  96. writel(int_mask, denali->flash_reg + INTR_EN(i));
  97. }
  98. static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
  99. {
  100. unsigned long timeout = 1000000;
  101. uint32_t intr_status;
  102. do {
  103. intr_status = read_interrupt_status(denali) & DENALI_IRQ_ALL;
  104. if (intr_status & irq_mask) {
  105. denali->irq_status &= ~irq_mask;
  106. /* our interrupt was detected */
  107. break;
  108. }
  109. udelay(1);
  110. timeout--;
  111. } while (timeout != 0);
  112. if (timeout == 0) {
  113. /* timeout */
  114. printf("Denali timeout with interrupt status %08x\n",
  115. read_interrupt_status(denali));
  116. intr_status = 0;
  117. }
  118. return intr_status;
  119. }
  120. /*
  121. * Certain operations for the denali NAND controller use an indexed mode to
  122. * read/write data. The operation is performed by writing the address value
  123. * of the command to the device memory followed by the data. This function
  124. * abstracts this common operation.
  125. */
  126. static void index_addr(struct denali_nand_info *denali,
  127. uint32_t address, uint32_t data)
  128. {
  129. writel(address, denali->flash_mem + INDEX_CTRL_REG);
  130. writel(data, denali->flash_mem + INDEX_DATA_REG);
  131. }
  132. /* Perform an indexed read of the device */
  133. static void index_addr_read_data(struct denali_nand_info *denali,
  134. uint32_t address, uint32_t *pdata)
  135. {
  136. writel(address, denali->flash_mem + INDEX_CTRL_REG);
  137. *pdata = readl(denali->flash_mem + INDEX_DATA_REG);
  138. }
  139. /*
  140. * We need to buffer some data for some of the NAND core routines.
  141. * The operations manage buffering that data.
  142. */
  143. static void reset_buf(struct denali_nand_info *denali)
  144. {
  145. denali->buf.head = 0;
  146. denali->buf.tail = 0;
  147. }
  148. static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
  149. {
  150. denali->buf.buf[denali->buf.tail++] = byte;
  151. }
  152. /* resets a specific device connected to the core */
  153. static void reset_bank(struct denali_nand_info *denali)
  154. {
  155. uint32_t irq_status;
  156. uint32_t irq_mask = INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT;
  157. clear_interrupts(denali);
  158. writel(1 << denali->flash_bank, denali->flash_reg + DEVICE_RESET);
  159. irq_status = wait_for_irq(denali, irq_mask);
  160. if (irq_status & INTR_STATUS__TIME_OUT)
  161. debug("reset bank failed.\n");
  162. }
  163. /* Reset the flash controller */
  164. static uint32_t denali_nand_reset(struct denali_nand_info *denali)
  165. {
  166. int i;
  167. for (i = 0; i < denali->max_banks; i++)
  168. writel(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
  169. denali->flash_reg + INTR_STATUS(i));
  170. for (i = 0; i < denali->max_banks; i++) {
  171. writel(1 << i, denali->flash_reg + DEVICE_RESET);
  172. while (!(readl(denali->flash_reg + INTR_STATUS(i)) &
  173. (INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT)))
  174. if (readl(denali->flash_reg + INTR_STATUS(i)) &
  175. INTR_STATUS__TIME_OUT)
  176. debug("NAND Reset operation timed out on bank"
  177. " %d\n", i);
  178. }
  179. for (i = 0; i < denali->max_banks; i++)
  180. writel(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
  181. denali->flash_reg + INTR_STATUS(i));
  182. return 0;
  183. }
  184. /*
  185. * this routine calculates the ONFI timing values for a given mode and
  186. * programs the clocking register accordingly. The mode is determined by
  187. * the get_onfi_nand_para routine.
  188. */
  189. static void nand_onfi_timing_set(struct denali_nand_info *denali,
  190. uint16_t mode)
  191. {
  192. uint32_t trea[6] = {40, 30, 25, 20, 20, 16};
  193. uint32_t trp[6] = {50, 25, 17, 15, 12, 10};
  194. uint32_t treh[6] = {30, 15, 15, 10, 10, 7};
  195. uint32_t trc[6] = {100, 50, 35, 30, 25, 20};
  196. uint32_t trhoh[6] = {0, 15, 15, 15, 15, 15};
  197. uint32_t trloh[6] = {0, 0, 0, 0, 5, 5};
  198. uint32_t tcea[6] = {100, 45, 30, 25, 25, 25};
  199. uint32_t tadl[6] = {200, 100, 100, 100, 70, 70};
  200. uint32_t trhw[6] = {200, 100, 100, 100, 100, 100};
  201. uint32_t trhz[6] = {200, 100, 100, 100, 100, 100};
  202. uint32_t twhr[6] = {120, 80, 80, 60, 60, 60};
  203. uint32_t tcs[6] = {70, 35, 25, 25, 20, 15};
  204. uint32_t data_invalid_rhoh, data_invalid_rloh, data_invalid;
  205. uint32_t dv_window = 0;
  206. uint32_t en_lo, en_hi;
  207. uint32_t acc_clks;
  208. uint32_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt;
  209. en_lo = DIV_ROUND_UP(trp[mode], CLK_X);
  210. en_hi = DIV_ROUND_UP(treh[mode], CLK_X);
  211. if ((en_hi * CLK_X) < (treh[mode] + 2))
  212. en_hi++;
  213. if ((en_lo + en_hi) * CLK_X < trc[mode])
  214. en_lo += DIV_ROUND_UP((trc[mode] - (en_lo + en_hi) * CLK_X),
  215. CLK_X);
  216. if ((en_lo + en_hi) < CLK_MULTI)
  217. en_lo += CLK_MULTI - en_lo - en_hi;
  218. while (dv_window < 8) {
  219. data_invalid_rhoh = en_lo * CLK_X + trhoh[mode];
  220. data_invalid_rloh = (en_lo + en_hi) * CLK_X + trloh[mode];
  221. data_invalid = data_invalid_rhoh < data_invalid_rloh ?
  222. data_invalid_rhoh : data_invalid_rloh;
  223. dv_window = data_invalid - trea[mode];
  224. if (dv_window < 8)
  225. en_lo++;
  226. }
  227. acc_clks = DIV_ROUND_UP(trea[mode], CLK_X);
  228. while (acc_clks * CLK_X - trea[mode] < 3)
  229. acc_clks++;
  230. if (data_invalid - acc_clks * CLK_X < 2)
  231. debug("%s, Line %d: Warning!\n", __FILE__, __LINE__);
  232. addr_2_data = DIV_ROUND_UP(tadl[mode], CLK_X);
  233. re_2_we = DIV_ROUND_UP(trhw[mode], CLK_X);
  234. re_2_re = DIV_ROUND_UP(trhz[mode], CLK_X);
  235. we_2_re = DIV_ROUND_UP(twhr[mode], CLK_X);
  236. cs_cnt = DIV_ROUND_UP((tcs[mode] - trp[mode]), CLK_X);
  237. if (cs_cnt == 0)
  238. cs_cnt = 1;
  239. if (tcea[mode]) {
  240. while (cs_cnt * CLK_X + trea[mode] < tcea[mode])
  241. cs_cnt++;
  242. }
  243. /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */
  244. if (readl(denali->flash_reg + MANUFACTURER_ID) == 0 &&
  245. readl(denali->flash_reg + DEVICE_ID) == 0x88)
  246. acc_clks = 6;
  247. writel(acc_clks, denali->flash_reg + ACC_CLKS);
  248. writel(re_2_we, denali->flash_reg + RE_2_WE);
  249. writel(re_2_re, denali->flash_reg + RE_2_RE);
  250. writel(we_2_re, denali->flash_reg + WE_2_RE);
  251. writel(addr_2_data, denali->flash_reg + ADDR_2_DATA);
  252. writel(en_lo, denali->flash_reg + RDWR_EN_LO_CNT);
  253. writel(en_hi, denali->flash_reg + RDWR_EN_HI_CNT);
  254. writel(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
  255. }
  256. /* queries the NAND device to see what ONFI modes it supports. */
  257. static uint32_t get_onfi_nand_para(struct denali_nand_info *denali)
  258. {
  259. int i;
  260. /*
  261. * we needn't to do a reset here because driver has already
  262. * reset all the banks before
  263. */
  264. if (!(readl(denali->flash_reg + ONFI_TIMING_MODE) &
  265. ONFI_TIMING_MODE__VALUE))
  266. return -EIO;
  267. for (i = 5; i > 0; i--) {
  268. if (readl(denali->flash_reg + ONFI_TIMING_MODE) &
  269. (0x01 << i))
  270. break;
  271. }
  272. nand_onfi_timing_set(denali, i);
  273. /*
  274. * By now, all the ONFI devices we know support the page cache
  275. * rw feature. So here we enable the pipeline_rw_ahead feature
  276. */
  277. return 0;
  278. }
  279. static void get_samsung_nand_para(struct denali_nand_info *denali,
  280. uint8_t device_id)
  281. {
  282. if (device_id == 0xd3) { /* Samsung K9WAG08U1A */
  283. /* Set timing register values according to datasheet */
  284. writel(5, denali->flash_reg + ACC_CLKS);
  285. writel(20, denali->flash_reg + RE_2_WE);
  286. writel(12, denali->flash_reg + WE_2_RE);
  287. writel(14, denali->flash_reg + ADDR_2_DATA);
  288. writel(3, denali->flash_reg + RDWR_EN_LO_CNT);
  289. writel(2, denali->flash_reg + RDWR_EN_HI_CNT);
  290. writel(2, denali->flash_reg + CS_SETUP_CNT);
  291. }
  292. }
  293. static void get_toshiba_nand_para(struct denali_nand_info *denali)
  294. {
  295. uint32_t tmp;
  296. /*
  297. * Workaround to fix a controller bug which reports a wrong
  298. * spare area size for some kind of Toshiba NAND device
  299. */
  300. if ((readl(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) &&
  301. (readl(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) {
  302. writel(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  303. tmp = readl(denali->flash_reg + DEVICES_CONNECTED) *
  304. readl(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  305. writel(tmp, denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
  306. }
  307. }
  308. static void get_hynix_nand_para(struct denali_nand_info *denali,
  309. uint8_t device_id)
  310. {
  311. uint32_t main_size, spare_size;
  312. switch (device_id) {
  313. case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
  314. case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
  315. writel(128, denali->flash_reg + PAGES_PER_BLOCK);
  316. writel(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
  317. writel(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  318. main_size = 4096 *
  319. readl(denali->flash_reg + DEVICES_CONNECTED);
  320. spare_size = 224 *
  321. readl(denali->flash_reg + DEVICES_CONNECTED);
  322. writel(main_size, denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
  323. writel(spare_size, denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
  324. writel(0, denali->flash_reg + DEVICE_WIDTH);
  325. break;
  326. default:
  327. debug("Spectra: Unknown Hynix NAND (Device ID: 0x%x).\n"
  328. "Will use default parameter values instead.\n",
  329. device_id);
  330. }
  331. }
  332. /*
  333. * determines how many NAND chips are connected to the controller. Note for
  334. * Intel CE4100 devices we don't support more than one device.
  335. */
  336. static void find_valid_banks(struct denali_nand_info *denali)
  337. {
  338. uint32_t id[denali->max_banks];
  339. int i;
  340. denali->total_used_banks = 1;
  341. for (i = 0; i < denali->max_banks; i++) {
  342. index_addr(denali, MODE_11 | (i << 24) | 0, 0x90);
  343. index_addr(denali, MODE_11 | (i << 24) | 1, 0);
  344. index_addr_read_data(denali, MODE_11 | (i << 24) | 2, &id[i]);
  345. if (i == 0) {
  346. if (!(id[i] & 0x0ff))
  347. break;
  348. } else {
  349. if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
  350. denali->total_used_banks++;
  351. else
  352. break;
  353. }
  354. }
  355. }
  356. /*
  357. * Use the configuration feature register to determine the maximum number of
  358. * banks that the hardware supports.
  359. */
  360. static void detect_max_banks(struct denali_nand_info *denali)
  361. {
  362. uint32_t features = readl(denali->flash_reg + FEATURES);
  363. /*
  364. * Read the revision register, so we can calculate the max_banks
  365. * properly: the encoding changed from rev 5.0 to 5.1
  366. */
  367. u32 revision = MAKE_COMPARABLE_REVISION(
  368. readl(denali->flash_reg + REVISION));
  369. if (revision < REVISION_5_1)
  370. denali->max_banks = 2 << (features & FEATURES__N_BANKS);
  371. else
  372. denali->max_banks = 1 << (features & FEATURES__N_BANKS);
  373. }
  374. static void detect_partition_feature(struct denali_nand_info *denali)
  375. {
  376. /*
  377. * For MRST platform, denali->fwblks represent the
  378. * number of blocks firmware is taken,
  379. * FW is in protect partition and MTD driver has no
  380. * permission to access it. So let driver know how many
  381. * blocks it can't touch.
  382. */
  383. if (readl(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
  384. if ((readl(denali->flash_reg + PERM_SRC_ID(1)) &
  385. PERM_SRC_ID__SRCID) == SPECTRA_PARTITION_ID) {
  386. denali->fwblks =
  387. ((readl(denali->flash_reg + MIN_MAX_BANK(1)) &
  388. MIN_MAX_BANK__MIN_VALUE) *
  389. denali->blksperchip)
  390. +
  391. (readl(denali->flash_reg + MIN_BLK_ADDR(1)) &
  392. MIN_BLK_ADDR__VALUE);
  393. } else {
  394. denali->fwblks = SPECTRA_START_BLOCK;
  395. }
  396. } else {
  397. denali->fwblks = SPECTRA_START_BLOCK;
  398. }
  399. }
  400. static uint32_t denali_nand_timing_set(struct denali_nand_info *denali)
  401. {
  402. uint32_t id_bytes[8], addr;
  403. uint8_t maf_id, device_id;
  404. int i;
  405. /*
  406. * Use read id method to get device ID and other params.
  407. * For some NAND chips, controller can't report the correct
  408. * device ID by reading from DEVICE_ID register
  409. */
  410. addr = MODE_11 | BANK(denali->flash_bank);
  411. index_addr(denali, addr | 0, 0x90);
  412. index_addr(denali, addr | 1, 0);
  413. for (i = 0; i < 8; i++)
  414. index_addr_read_data(denali, addr | 2, &id_bytes[i]);
  415. maf_id = id_bytes[0];
  416. device_id = id_bytes[1];
  417. if (readl(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
  418. ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
  419. if (get_onfi_nand_para(denali))
  420. return -EIO;
  421. } else if (maf_id == 0xEC) { /* Samsung NAND */
  422. get_samsung_nand_para(denali, device_id);
  423. } else if (maf_id == 0x98) { /* Toshiba NAND */
  424. get_toshiba_nand_para(denali);
  425. } else if (maf_id == 0xAD) { /* Hynix NAND */
  426. get_hynix_nand_para(denali, device_id);
  427. }
  428. find_valid_banks(denali);
  429. detect_partition_feature(denali);
  430. /*
  431. * If the user specified to override the default timings
  432. * with a specific ONFI mode, we apply those changes here.
  433. */
  434. if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
  435. nand_onfi_timing_set(denali, onfi_timing_mode);
  436. return 0;
  437. }
  438. /*
  439. * validation function to verify that the controlling software is making
  440. * a valid request
  441. */
  442. static inline bool is_flash_bank_valid(int flash_bank)
  443. {
  444. return flash_bank >= 0 && flash_bank < 4;
  445. }
  446. static void denali_irq_init(struct denali_nand_info *denali)
  447. {
  448. uint32_t int_mask;
  449. int i;
  450. /* Disable global interrupts */
  451. writel(0, denali->flash_reg + GLOBAL_INT_ENABLE);
  452. int_mask = DENALI_IRQ_ALL;
  453. /* Clear all status bits */
  454. for (i = 0; i < denali->max_banks; ++i)
  455. writel(0xFFFF, denali->flash_reg + INTR_STATUS(i));
  456. denali_irq_enable(denali, int_mask);
  457. }
  458. /*
  459. * This helper function setups the registers for ECC and whether or not
  460. * the spare area will be transferred.
  461. */
  462. static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
  463. bool transfer_spare)
  464. {
  465. int ecc_en_flag, transfer_spare_flag;
  466. /* set ECC, transfer spare bits if needed */
  467. ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
  468. transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
  469. /* Enable spare area/ECC per user's request. */
  470. writel(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
  471. /* applicable for MAP01 only */
  472. writel(transfer_spare_flag, denali->flash_reg + TRANSFER_SPARE_REG);
  473. }
  474. /*
  475. * sends a pipeline command operation to the controller. See the Denali NAND
  476. * controller's user guide for more information (section 4.2.3.6).
  477. */
  478. static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
  479. bool ecc_en, bool transfer_spare,
  480. int access_type, int op)
  481. {
  482. uint32_t addr, cmd, irq_status;
  483. static uint32_t page_count = 1;
  484. setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
  485. clear_interrupts(denali);
  486. addr = BANK(denali->flash_bank) | denali->page;
  487. /* setup the acccess type */
  488. cmd = MODE_10 | addr;
  489. index_addr(denali, cmd, access_type);
  490. /* setup the pipeline command */
  491. index_addr(denali, cmd, 0x2000 | op | page_count);
  492. cmd = MODE_01 | addr;
  493. writel(cmd, denali->flash_mem + INDEX_CTRL_REG);
  494. if (op == DENALI_READ) {
  495. /* wait for command to be accepted */
  496. irq_status = wait_for_irq(denali, INTR_STATUS__LOAD_COMP);
  497. if (irq_status == 0)
  498. return -EIO;
  499. }
  500. return 0;
  501. }
  502. /* helper function that simply writes a buffer to the flash */
  503. static int write_data_to_flash_mem(struct denali_nand_info *denali,
  504. const uint8_t *buf, int len)
  505. {
  506. uint32_t *buf32;
  507. int i;
  508. /*
  509. * verify that the len is a multiple of 4.
  510. * see comment in read_data_from_flash_mem()
  511. */
  512. BUG_ON((len % 4) != 0);
  513. /* write the data to the flash memory */
  514. buf32 = (uint32_t *)buf;
  515. for (i = 0; i < len / 4; i++)
  516. writel(*buf32++, denali->flash_mem + INDEX_DATA_REG);
  517. return i * 4; /* intent is to return the number of bytes read */
  518. }
  519. /* helper function that simply reads a buffer from the flash */
  520. static int read_data_from_flash_mem(struct denali_nand_info *denali,
  521. uint8_t *buf, int len)
  522. {
  523. uint32_t *buf32;
  524. int i;
  525. /*
  526. * we assume that len will be a multiple of 4, if not it would be nice
  527. * to know about it ASAP rather than have random failures...
  528. * This assumption is based on the fact that this function is designed
  529. * to be used to read flash pages, which are typically multiples of 4.
  530. */
  531. BUG_ON((len % 4) != 0);
  532. /* transfer the data from the flash */
  533. buf32 = (uint32_t *)buf;
  534. for (i = 0; i < len / 4; i++)
  535. *buf32++ = readl(denali->flash_mem + INDEX_DATA_REG);
  536. return i * 4; /* intent is to return the number of bytes read */
  537. }
  538. static void denali_mode_main_access(struct denali_nand_info *denali)
  539. {
  540. uint32_t addr, cmd;
  541. addr = BANK(denali->flash_bank) | denali->page;
  542. cmd = MODE_10 | addr;
  543. index_addr(denali, cmd, MAIN_ACCESS);
  544. }
  545. static void denali_mode_main_spare_access(struct denali_nand_info *denali)
  546. {
  547. uint32_t addr, cmd;
  548. addr = BANK(denali->flash_bank) | denali->page;
  549. cmd = MODE_10 | addr;
  550. index_addr(denali, cmd, MAIN_SPARE_ACCESS);
  551. }
  552. /* writes OOB data to the device */
  553. static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
  554. {
  555. struct denali_nand_info *denali = mtd_to_denali(mtd);
  556. uint32_t irq_status;
  557. uint32_t irq_mask = INTR_STATUS__PROGRAM_COMP |
  558. INTR_STATUS__PROGRAM_FAIL;
  559. int status = 0;
  560. denali->page = page;
  561. if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
  562. DENALI_WRITE) == 0) {
  563. write_data_to_flash_mem(denali, buf, mtd->oobsize);
  564. /* wait for operation to complete */
  565. irq_status = wait_for_irq(denali, irq_mask);
  566. if (irq_status == 0) {
  567. dev_err(denali->dev, "OOB write failed\n");
  568. status = -EIO;
  569. }
  570. } else {
  571. printf("unable to send pipeline command\n");
  572. status = -EIO;
  573. }
  574. return status;
  575. }
  576. /* reads OOB data from the device */
  577. static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
  578. {
  579. struct denali_nand_info *denali = mtd_to_denali(mtd);
  580. uint32_t irq_mask = INTR_STATUS__LOAD_COMP;
  581. uint32_t irq_status, addr, cmd;
  582. denali->page = page;
  583. if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
  584. DENALI_READ) == 0) {
  585. read_data_from_flash_mem(denali, buf, mtd->oobsize);
  586. /*
  587. * wait for command to be accepted
  588. * can always use status0 bit as the
  589. * mask is identical for each bank.
  590. */
  591. irq_status = wait_for_irq(denali, irq_mask);
  592. if (irq_status == 0)
  593. printf("page on OOB timeout %d\n", denali->page);
  594. /*
  595. * We set the device back to MAIN_ACCESS here as I observed
  596. * instability with the controller if you do a block erase
  597. * and the last transaction was a SPARE_ACCESS. Block erase
  598. * is reliable (according to the MTD test infrastructure)
  599. * if you are in MAIN_ACCESS.
  600. */
  601. addr = BANK(denali->flash_bank) | denali->page;
  602. cmd = MODE_10 | addr;
  603. index_addr(denali, cmd, MAIN_ACCESS);
  604. }
  605. }
  606. /*
  607. * this function examines buffers to see if they contain data that
  608. * indicate that the buffer is part of an erased region of flash.
  609. */
  610. static bool is_erased(uint8_t *buf, int len)
  611. {
  612. int i;
  613. for (i = 0; i < len; i++)
  614. if (buf[i] != 0xFF)
  615. return false;
  616. return true;
  617. }
  618. /* programs the controller to either enable/disable DMA transfers */
  619. static void denali_enable_dma(struct denali_nand_info *denali, bool en)
  620. {
  621. writel(en ? DMA_ENABLE__FLAG : 0, denali->flash_reg + DMA_ENABLE);
  622. readl(denali->flash_reg + DMA_ENABLE);
  623. }
  624. /* setups the HW to perform the data DMA */
  625. static void denali_setup_dma(struct denali_nand_info *denali, int op)
  626. {
  627. uint32_t mode;
  628. const int page_count = 1;
  629. uint64_t addr = (unsigned long)denali->buf.dma_buf;
  630. flush_dcache_range(addr, addr + sizeof(denali->buf.dma_buf));
  631. /* For Denali controller that is 64 bit bus IP core */
  632. #ifdef CONFIG_SYS_NAND_DENALI_64BIT
  633. mode = MODE_10 | BANK(denali->flash_bank) | denali->page;
  634. /* DMA is a three step process */
  635. /* 1. setup transfer type, interrupt when complete,
  636. burst len = 64 bytes, the number of pages */
  637. index_addr(denali, mode, 0x01002000 | (64 << 16) | op | page_count);
  638. /* 2. set memory low address bits 31:0 */
  639. index_addr(denali, mode, addr);
  640. /* 3. set memory high address bits 64:32 */
  641. index_addr(denali, mode, addr >> 32);
  642. #else
  643. mode = MODE_10 | BANK(denali->flash_bank);
  644. /* DMA is a four step process */
  645. /* 1. setup transfer type and # of pages */
  646. index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
  647. /* 2. set memory high address bits 23:8 */
  648. index_addr(denali, mode | (((addr >> 16) & 0xffff) << 8), 0x2200);
  649. /* 3. set memory low address bits 23:8 */
  650. index_addr(denali, mode | ((addr & 0xffff) << 8), 0x2300);
  651. /* 4. interrupt when complete, burst len = 64 bytes */
  652. index_addr(denali, mode | 0x14000, 0x2400);
  653. #endif
  654. }
  655. /* Common DMA function */
  656. static uint32_t denali_dma_configuration(struct denali_nand_info *denali,
  657. uint32_t ops, bool raw_xfer,
  658. uint32_t irq_mask, int oob_required)
  659. {
  660. uint32_t irq_status = 0;
  661. /* setup_ecc_for_xfer(bool ecc_en, bool transfer_spare) */
  662. setup_ecc_for_xfer(denali, !raw_xfer, oob_required);
  663. /* clear any previous interrupt flags */
  664. clear_interrupts(denali);
  665. /* enable the DMA */
  666. denali_enable_dma(denali, true);
  667. /* setup the DMA */
  668. denali_setup_dma(denali, ops);
  669. /* wait for operation to complete */
  670. irq_status = wait_for_irq(denali, irq_mask);
  671. /* if ECC fault happen, seems we need delay before turning off DMA.
  672. * If not, the controller will go into non responsive condition */
  673. if (irq_status & INTR_STATUS__ECC_UNCOR_ERR)
  674. udelay(100);
  675. /* disable the DMA */
  676. denali_enable_dma(denali, false);
  677. return irq_status;
  678. }
  679. static int write_page(struct mtd_info *mtd, struct nand_chip *chip,
  680. const uint8_t *buf, bool raw_xfer, int oob_required)
  681. {
  682. struct denali_nand_info *denali = mtd_to_denali(mtd);
  683. uint32_t irq_status = 0;
  684. uint32_t irq_mask = INTR_STATUS__DMA_CMD_COMP;
  685. denali->status = 0;
  686. /* copy buffer into DMA buffer */
  687. memcpy(denali->buf.dma_buf, buf, mtd->writesize);
  688. /* need extra memcpy for raw transfer */
  689. if (raw_xfer)
  690. memcpy(denali->buf.dma_buf + mtd->writesize,
  691. chip->oob_poi, mtd->oobsize);
  692. /* setting up DMA */
  693. irq_status = denali_dma_configuration(denali, DENALI_WRITE, raw_xfer,
  694. irq_mask, oob_required);
  695. /* if timeout happen, error out */
  696. if (!(irq_status & INTR_STATUS__DMA_CMD_COMP)) {
  697. debug("DMA timeout for denali write_page\n");
  698. denali->status = NAND_STATUS_FAIL;
  699. return -EIO;
  700. }
  701. if (irq_status & INTR_STATUS__LOCKED_BLK) {
  702. debug("Failed as write to locked block\n");
  703. denali->status = NAND_STATUS_FAIL;
  704. return -EIO;
  705. }
  706. return 0;
  707. }
  708. /* NAND core entry points */
  709. /*
  710. * this is the callback that the NAND core calls to write a page. Since
  711. * writing a page with ECC or without is similar, all the work is done
  712. * by write_page above.
  713. */
  714. static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  715. const uint8_t *buf, int oob_required,
  716. int page)
  717. {
  718. struct denali_nand_info *denali = mtd_to_denali(mtd);
  719. /*
  720. * for regular page writes, we let HW handle all the ECC
  721. * data written to the device.
  722. */
  723. if (oob_required)
  724. /* switch to main + spare access */
  725. denali_mode_main_spare_access(denali);
  726. else
  727. /* switch to main access only */
  728. denali_mode_main_access(denali);
  729. return write_page(mtd, chip, buf, false, oob_required);
  730. }
  731. /*
  732. * This is the callback that the NAND core calls to write a page without ECC.
  733. * raw access is similar to ECC page writes, so all the work is done in the
  734. * write_page() function above.
  735. */
  736. static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  737. const uint8_t *buf, int oob_required,
  738. int page)
  739. {
  740. struct denali_nand_info *denali = mtd_to_denali(mtd);
  741. /*
  742. * for raw page writes, we want to disable ECC and simply write
  743. * whatever data is in the buffer.
  744. */
  745. if (oob_required)
  746. /* switch to main + spare access */
  747. denali_mode_main_spare_access(denali);
  748. else
  749. /* switch to main access only */
  750. denali_mode_main_access(denali);
  751. return write_page(mtd, chip, buf, true, oob_required);
  752. }
  753. static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  754. int page)
  755. {
  756. return write_oob_data(mtd, chip->oob_poi, page);
  757. }
  758. /* raw include ECC value and all the spare area */
  759. static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  760. uint8_t *buf, int oob_required, int page)
  761. {
  762. struct denali_nand_info *denali = mtd_to_denali(mtd);
  763. uint32_t irq_status, irq_mask = INTR_STATUS__DMA_CMD_COMP;
  764. if (denali->page != page) {
  765. debug("Missing NAND_CMD_READ0 command\n");
  766. return -EIO;
  767. }
  768. if (oob_required)
  769. /* switch to main + spare access */
  770. denali_mode_main_spare_access(denali);
  771. else
  772. /* switch to main access only */
  773. denali_mode_main_access(denali);
  774. /* setting up the DMA where ecc_enable is false */
  775. irq_status = denali_dma_configuration(denali, DENALI_READ, true,
  776. irq_mask, oob_required);
  777. /* if timeout happen, error out */
  778. if (!(irq_status & INTR_STATUS__DMA_CMD_COMP)) {
  779. debug("DMA timeout for denali_read_page_raw\n");
  780. return -EIO;
  781. }
  782. /* splitting the content to destination buffer holder */
  783. memcpy(chip->oob_poi, (denali->buf.dma_buf + mtd->writesize),
  784. mtd->oobsize);
  785. memcpy(buf, denali->buf.dma_buf, mtd->writesize);
  786. return 0;
  787. }
  788. static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  789. uint8_t *buf, int oob_required, int page)
  790. {
  791. struct denali_nand_info *denali = mtd_to_denali(mtd);
  792. uint32_t irq_status, irq_mask = INTR_STATUS__DMA_CMD_COMP;
  793. if (denali->page != page) {
  794. debug("Missing NAND_CMD_READ0 command\n");
  795. return -EIO;
  796. }
  797. if (oob_required)
  798. /* switch to main + spare access */
  799. denali_mode_main_spare_access(denali);
  800. else
  801. /* switch to main access only */
  802. denali_mode_main_access(denali);
  803. /* setting up the DMA where ecc_enable is true */
  804. irq_status = denali_dma_configuration(denali, DENALI_READ, false,
  805. irq_mask, oob_required);
  806. memcpy(buf, denali->buf.dma_buf, mtd->writesize);
  807. /* check whether any ECC error */
  808. if (irq_status & INTR_STATUS__ECC_UNCOR_ERR) {
  809. /* is the ECC cause by erase page, check using read_page_raw */
  810. debug(" Uncorrected ECC detected\n");
  811. denali_read_page_raw(mtd, chip, buf, oob_required,
  812. denali->page);
  813. if (is_erased(buf, mtd->writesize) == true &&
  814. is_erased(chip->oob_poi, mtd->oobsize) == true) {
  815. debug(" ECC error cause by erased block\n");
  816. /* false alarm, return the 0xFF */
  817. } else {
  818. return -EIO;
  819. }
  820. }
  821. memcpy(buf, denali->buf.dma_buf, mtd->writesize);
  822. return 0;
  823. }
  824. static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  825. int page)
  826. {
  827. read_oob_data(mtd, chip->oob_poi, page);
  828. return 0;
  829. }
  830. static uint8_t denali_read_byte(struct mtd_info *mtd)
  831. {
  832. struct denali_nand_info *denali = mtd_to_denali(mtd);
  833. uint32_t addr, result;
  834. addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
  835. index_addr_read_data(denali, addr | 2, &result);
  836. return (uint8_t)result & 0xFF;
  837. }
  838. static void denali_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  839. {
  840. struct denali_nand_info *denali = mtd_to_denali(mtd);
  841. uint32_t i, addr, result;
  842. /* delay for tR (data transfer from Flash array to data register) */
  843. udelay(25);
  844. /* ensure device completed else additional delay and polling */
  845. wait_for_irq(denali, INTR_STATUS__INT_ACT);
  846. addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
  847. for (i = 0; i < len; i++) {
  848. index_addr_read_data(denali, (uint32_t)addr | 2, &result);
  849. write_byte_to_buf(denali, result);
  850. }
  851. memcpy(buf, denali->buf.buf, len);
  852. }
  853. static void denali_select_chip(struct mtd_info *mtd, int chip)
  854. {
  855. struct denali_nand_info *denali = mtd_to_denali(mtd);
  856. denali->flash_bank = chip;
  857. }
  858. static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  859. {
  860. struct denali_nand_info *denali = mtd_to_denali(mtd);
  861. int status = denali->status;
  862. denali->status = 0;
  863. return status;
  864. }
  865. static int denali_erase(struct mtd_info *mtd, int page)
  866. {
  867. struct denali_nand_info *denali = mtd_to_denali(mtd);
  868. uint32_t cmd, irq_status;
  869. clear_interrupts(denali);
  870. /* setup page read request for access type */
  871. cmd = MODE_10 | BANK(denali->flash_bank) | page;
  872. index_addr(denali, cmd, 0x1);
  873. /* wait for erase to complete or failure to occur */
  874. irq_status = wait_for_irq(denali, INTR_STATUS__ERASE_COMP |
  875. INTR_STATUS__ERASE_FAIL);
  876. if (irq_status & INTR_STATUS__ERASE_FAIL ||
  877. irq_status & INTR_STATUS__LOCKED_BLK)
  878. return NAND_STATUS_FAIL;
  879. return 0;
  880. }
  881. static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
  882. int page)
  883. {
  884. struct denali_nand_info *denali = mtd_to_denali(mtd);
  885. uint32_t addr;
  886. switch (cmd) {
  887. case NAND_CMD_PAGEPROG:
  888. break;
  889. case NAND_CMD_STATUS:
  890. addr = MODE_11 | BANK(denali->flash_bank);
  891. index_addr(denali, addr | 0, cmd);
  892. break;
  893. case NAND_CMD_READID:
  894. case NAND_CMD_PARAM:
  895. reset_buf(denali);
  896. /*
  897. * sometimes ManufactureId read from register is not right
  898. * e.g. some of Micron MT29F32G08QAA MLC NAND chips
  899. * So here we send READID cmd to NAND insteand
  900. */
  901. addr = MODE_11 | BANK(denali->flash_bank);
  902. index_addr(denali, addr | 0, cmd);
  903. index_addr(denali, addr | 1, col & 0xFF);
  904. if (cmd == NAND_CMD_PARAM)
  905. udelay(50);
  906. break;
  907. case NAND_CMD_RNDOUT:
  908. addr = MODE_11 | BANK(denali->flash_bank);
  909. index_addr(denali, addr | 0, cmd);
  910. index_addr(denali, addr | 1, col & 0xFF);
  911. index_addr(denali, addr | 1, col >> 8);
  912. index_addr(denali, addr | 0, NAND_CMD_RNDOUTSTART);
  913. break;
  914. case NAND_CMD_READ0:
  915. case NAND_CMD_SEQIN:
  916. denali->page = page;
  917. break;
  918. case NAND_CMD_RESET:
  919. reset_bank(denali);
  920. break;
  921. case NAND_CMD_READOOB:
  922. /* TODO: Read OOB data */
  923. break;
  924. case NAND_CMD_ERASE1:
  925. /*
  926. * supporting block erase only, not multiblock erase as
  927. * it will cross plane and software need complex calculation
  928. * to identify the block count for the cross plane
  929. */
  930. denali_erase(mtd, page);
  931. break;
  932. case NAND_CMD_ERASE2:
  933. /* nothing to do here as it was done during NAND_CMD_ERASE1 */
  934. break;
  935. case NAND_CMD_UNLOCK1:
  936. addr = MODE_10 | BANK(denali->flash_bank) | page;
  937. index_addr(denali, addr | 0, DENALI_UNLOCK_START);
  938. break;
  939. case NAND_CMD_UNLOCK2:
  940. addr = MODE_10 | BANK(denali->flash_bank) | page;
  941. index_addr(denali, addr | 0, DENALI_UNLOCK_END);
  942. break;
  943. case NAND_CMD_LOCK:
  944. addr = MODE_10 | BANK(denali->flash_bank);
  945. index_addr(denali, addr | 0, DENALI_LOCK);
  946. break;
  947. default:
  948. printf(": unsupported command received 0x%x\n", cmd);
  949. break;
  950. }
  951. }
  952. /* end NAND core entry points */
  953. /* Initialization code to bring the device up to a known good state */
  954. static void denali_hw_init(struct denali_nand_info *denali)
  955. {
  956. /*
  957. * tell driver how many bit controller will skip before writing
  958. * ECC code in OOB. This is normally used for bad block marker
  959. */
  960. writel(CONFIG_NAND_DENALI_SPARE_AREA_SKIP_BYTES,
  961. denali->flash_reg + SPARE_AREA_SKIP_BYTES);
  962. detect_max_banks(denali);
  963. denali_nand_reset(denali);
  964. writel(0x0F, denali->flash_reg + RB_PIN_ENABLED);
  965. writel(CHIP_EN_DONT_CARE__FLAG,
  966. denali->flash_reg + CHIP_ENABLE_DONT_CARE);
  967. writel(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
  968. /* Should set value for these registers when init */
  969. writel(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
  970. writel(1, denali->flash_reg + ECC_ENABLE);
  971. denali_nand_timing_set(denali);
  972. denali_irq_init(denali);
  973. }
  974. static struct nand_ecclayout nand_oob;
  975. static int denali_init(struct denali_nand_info *denali)
  976. {
  977. int ret;
  978. denali_hw_init(denali);
  979. denali->mtd->name = "denali-nand";
  980. denali->mtd->owner = THIS_MODULE;
  981. /* register the driver with the NAND core subsystem */
  982. denali->nand.select_chip = denali_select_chip;
  983. denali->nand.cmdfunc = denali_cmdfunc;
  984. denali->nand.read_byte = denali_read_byte;
  985. denali->nand.read_buf = denali_read_buf;
  986. denali->nand.waitfunc = denali_waitfunc;
  987. /*
  988. * scan for NAND devices attached to the controller
  989. * this is the first stage in a two step process to register
  990. * with the nand subsystem
  991. */
  992. if (nand_scan_ident(denali->mtd, denali->max_banks, NULL)) {
  993. ret = -ENXIO;
  994. goto fail;
  995. }
  996. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  997. /* check whether flash got BBT table (located at end of flash). As we
  998. * use NAND_BBT_NO_OOB, the BBT page will start with
  999. * bbt_pattern. We will have mirror pattern too */
  1000. denali->nand.bbt_options |= NAND_BBT_USE_FLASH;
  1001. /*
  1002. * We are using main + spare with ECC support. As BBT need ECC support,
  1003. * we need to ensure BBT code don't write to OOB for the BBT pattern.
  1004. * All BBT info will be stored into data area with ECC support.
  1005. */
  1006. denali->nand.bbt_options |= NAND_BBT_NO_OOB;
  1007. #endif
  1008. denali->nand.ecc.mode = NAND_ECC_HW;
  1009. denali->nand.ecc.size = CONFIG_NAND_DENALI_ECC_SIZE;
  1010. /* no subpage writes on denali */
  1011. denali->nand.options |= NAND_NO_SUBPAGE_WRITE;
  1012. /*
  1013. * Tell driver the ecc strength. This register may be already set
  1014. * correctly. So we read this value out.
  1015. */
  1016. denali->nand.ecc.strength = readl(denali->flash_reg + ECC_CORRECTION);
  1017. switch (denali->nand.ecc.size) {
  1018. case 512:
  1019. denali->nand.ecc.bytes =
  1020. (denali->nand.ecc.strength * 13 + 15) / 16 * 2;
  1021. break;
  1022. case 1024:
  1023. denali->nand.ecc.bytes =
  1024. (denali->nand.ecc.strength * 14 + 15) / 16 * 2;
  1025. break;
  1026. default:
  1027. pr_err("Unsupported ECC size\n");
  1028. ret = -EINVAL;
  1029. goto fail;
  1030. }
  1031. nand_oob.eccbytes = denali->nand.ecc.bytes;
  1032. denali->nand.ecc.layout = &nand_oob;
  1033. writel(denali->mtd->erasesize / denali->mtd->writesize,
  1034. denali->flash_reg + PAGES_PER_BLOCK);
  1035. writel(denali->nand.options & NAND_BUSWIDTH_16 ? 1 : 0,
  1036. denali->flash_reg + DEVICE_WIDTH);
  1037. writel(denali->mtd->writesize,
  1038. denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
  1039. writel(denali->mtd->oobsize,
  1040. denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  1041. if (readl(denali->flash_reg + DEVICES_CONNECTED) == 0)
  1042. writel(1, denali->flash_reg + DEVICES_CONNECTED);
  1043. /* override the default operations */
  1044. denali->nand.ecc.read_page = denali_read_page;
  1045. denali->nand.ecc.read_page_raw = denali_read_page_raw;
  1046. denali->nand.ecc.write_page = denali_write_page;
  1047. denali->nand.ecc.write_page_raw = denali_write_page_raw;
  1048. denali->nand.ecc.read_oob = denali_read_oob;
  1049. denali->nand.ecc.write_oob = denali_write_oob;
  1050. if (nand_scan_tail(denali->mtd)) {
  1051. ret = -ENXIO;
  1052. goto fail;
  1053. }
  1054. ret = nand_register(0, denali->mtd);
  1055. fail:
  1056. return ret;
  1057. }
  1058. static int __board_nand_init(void)
  1059. {
  1060. struct denali_nand_info *denali;
  1061. denali = kzalloc(sizeof(*denali), GFP_KERNEL);
  1062. if (!denali)
  1063. return -ENOMEM;
  1064. /*
  1065. * If CONFIG_SYS_NAND_SELF_INIT is defined, each driver is responsible
  1066. * for instantiating struct nand_chip, while drivers/mtd/nand/nand.c
  1067. * still provides a "struct mtd_info nand_info" instance.
  1068. */
  1069. denali->mtd = &denali->nand.mtd;
  1070. /*
  1071. * In the future, these base addresses should be taken from
  1072. * Device Tree or platform data.
  1073. */
  1074. denali->flash_reg = (void __iomem *)CONFIG_SYS_NAND_REGS_BASE;
  1075. denali->flash_mem = (void __iomem *)CONFIG_SYS_NAND_DATA_BASE;
  1076. return denali_init(denali);
  1077. }
  1078. void board_nand_init(void)
  1079. {
  1080. if (__board_nand_init() < 0)
  1081. pr_warn("Failed to initialize Denali NAND controller.\n");
  1082. }