denali.c 35 KB

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