denali.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. /*
  2. * NAND Flash Controller Device Driver
  3. * Copyright © 2009-2010, Intel Corporation and its suppliers.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include <linux/interrupt.h>
  20. #include <linux/delay.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/wait.h>
  23. #include <linux/mutex.h>
  24. #include <linux/slab.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/module.h>
  27. #include "denali.h"
  28. MODULE_LICENSE("GPL");
  29. /*
  30. * We define a module parameter that allows the user to override
  31. * the hardware and decide what timing mode should be used.
  32. */
  33. #define NAND_DEFAULT_TIMINGS -1
  34. static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
  35. module_param(onfi_timing_mode, int, S_IRUGO);
  36. MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting."
  37. " -1 indicates use default timings");
  38. #define DENALI_NAND_NAME "denali-nand"
  39. /*
  40. * We define a macro here that combines all interrupts this driver uses into
  41. * a single constant value, for convenience.
  42. */
  43. #define DENALI_IRQ_ALL (INTR_STATUS__DMA_CMD_COMP | \
  44. INTR_STATUS__ECC_TRANSACTION_DONE | \
  45. INTR_STATUS__ECC_ERR | \
  46. INTR_STATUS__PROGRAM_FAIL | \
  47. INTR_STATUS__LOAD_COMP | \
  48. INTR_STATUS__PROGRAM_COMP | \
  49. INTR_STATUS__TIME_OUT | \
  50. INTR_STATUS__ERASE_FAIL | \
  51. INTR_STATUS__RST_COMP | \
  52. INTR_STATUS__ERASE_COMP)
  53. /*
  54. * indicates whether or not the internal value for the flash bank is
  55. * valid or not
  56. */
  57. #define CHIP_SELECT_INVALID -1
  58. #define SUPPORT_8BITECC 1
  59. /*
  60. * This macro divides two integers and rounds fractional values up
  61. * to the nearest integer value.
  62. */
  63. #define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y)))
  64. /*
  65. * this macro allows us to convert from an MTD structure to our own
  66. * device context (denali) structure.
  67. */
  68. #define mtd_to_denali(m) container_of(m, struct denali_nand_info, mtd)
  69. /*
  70. * These constants are defined by the driver to enable common driver
  71. * configuration options.
  72. */
  73. #define SPARE_ACCESS 0x41
  74. #define MAIN_ACCESS 0x42
  75. #define MAIN_SPARE_ACCESS 0x43
  76. #define PIPELINE_ACCESS 0x2000
  77. #define DENALI_READ 0
  78. #define DENALI_WRITE 0x100
  79. /* types of device accesses. We can issue commands and get status */
  80. #define COMMAND_CYCLE 0
  81. #define ADDR_CYCLE 1
  82. #define STATUS_CYCLE 2
  83. /*
  84. * this is a helper macro that allows us to
  85. * format the bank into the proper bits for the controller
  86. */
  87. #define BANK(x) ((x) << 24)
  88. /* forward declarations */
  89. static void clear_interrupts(struct denali_nand_info *denali);
  90. static uint32_t wait_for_irq(struct denali_nand_info *denali,
  91. uint32_t irq_mask);
  92. static void denali_irq_enable(struct denali_nand_info *denali,
  93. uint32_t int_mask);
  94. static uint32_t read_interrupt_status(struct denali_nand_info *denali);
  95. /*
  96. * Certain operations for the denali NAND controller use an indexed mode to
  97. * read/write data. The operation is performed by writing the address value
  98. * of the command to the device memory followed by the data. This function
  99. * abstracts this common operation.
  100. */
  101. static void index_addr(struct denali_nand_info *denali,
  102. uint32_t address, uint32_t data)
  103. {
  104. iowrite32(address, denali->flash_mem);
  105. iowrite32(data, denali->flash_mem + 0x10);
  106. }
  107. /* Perform an indexed read of the device */
  108. static void index_addr_read_data(struct denali_nand_info *denali,
  109. uint32_t address, uint32_t *pdata)
  110. {
  111. iowrite32(address, denali->flash_mem);
  112. *pdata = ioread32(denali->flash_mem + 0x10);
  113. }
  114. /*
  115. * We need to buffer some data for some of the NAND core routines.
  116. * The operations manage buffering that data.
  117. */
  118. static void reset_buf(struct denali_nand_info *denali)
  119. {
  120. denali->buf.head = denali->buf.tail = 0;
  121. }
  122. static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
  123. {
  124. denali->buf.buf[denali->buf.tail++] = byte;
  125. }
  126. /* reads the status of the device */
  127. static void read_status(struct denali_nand_info *denali)
  128. {
  129. uint32_t cmd;
  130. /* initialize the data buffer to store status */
  131. reset_buf(denali);
  132. cmd = ioread32(denali->flash_reg + WRITE_PROTECT);
  133. if (cmd)
  134. write_byte_to_buf(denali, NAND_STATUS_WP);
  135. else
  136. write_byte_to_buf(denali, 0);
  137. }
  138. /* resets a specific device connected to the core */
  139. static void reset_bank(struct denali_nand_info *denali)
  140. {
  141. uint32_t irq_status;
  142. uint32_t irq_mask = INTR_STATUS__RST_COMP |
  143. INTR_STATUS__TIME_OUT;
  144. clear_interrupts(denali);
  145. iowrite32(1 << denali->flash_bank, denali->flash_reg + DEVICE_RESET);
  146. irq_status = wait_for_irq(denali, irq_mask);
  147. if (irq_status & INTR_STATUS__TIME_OUT)
  148. dev_err(denali->dev, "reset bank failed.\n");
  149. }
  150. /* Reset the flash controller */
  151. static uint16_t denali_nand_reset(struct denali_nand_info *denali)
  152. {
  153. int i;
  154. dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
  155. __FILE__, __LINE__, __func__);
  156. for (i = 0 ; i < denali->max_banks; i++)
  157. iowrite32(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
  158. denali->flash_reg + INTR_STATUS(i));
  159. for (i = 0 ; i < denali->max_banks; i++) {
  160. iowrite32(1 << i, denali->flash_reg + DEVICE_RESET);
  161. while (!(ioread32(denali->flash_reg +
  162. INTR_STATUS(i)) &
  163. (INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT)))
  164. cpu_relax();
  165. if (ioread32(denali->flash_reg + INTR_STATUS(i)) &
  166. INTR_STATUS__TIME_OUT)
  167. dev_dbg(denali->dev,
  168. "NAND Reset operation timed out on bank %d\n", i);
  169. }
  170. for (i = 0; i < denali->max_banks; i++)
  171. iowrite32(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
  172. denali->flash_reg + INTR_STATUS(i));
  173. return PASS;
  174. }
  175. /*
  176. * this routine calculates the ONFI timing values for a given mode and
  177. * programs the clocking register accordingly. The mode is determined by
  178. * the get_onfi_nand_para routine.
  179. */
  180. static void nand_onfi_timing_set(struct denali_nand_info *denali,
  181. uint16_t mode)
  182. {
  183. uint16_t Trea[6] = {40, 30, 25, 20, 20, 16};
  184. uint16_t Trp[6] = {50, 25, 17, 15, 12, 10};
  185. uint16_t Treh[6] = {30, 15, 15, 10, 10, 7};
  186. uint16_t Trc[6] = {100, 50, 35, 30, 25, 20};
  187. uint16_t Trhoh[6] = {0, 15, 15, 15, 15, 15};
  188. uint16_t Trloh[6] = {0, 0, 0, 0, 5, 5};
  189. uint16_t Tcea[6] = {100, 45, 30, 25, 25, 25};
  190. uint16_t Tadl[6] = {200, 100, 100, 100, 70, 70};
  191. uint16_t Trhw[6] = {200, 100, 100, 100, 100, 100};
  192. uint16_t Trhz[6] = {200, 100, 100, 100, 100, 100};
  193. uint16_t Twhr[6] = {120, 80, 80, 60, 60, 60};
  194. uint16_t Tcs[6] = {70, 35, 25, 25, 20, 15};
  195. uint16_t TclsRising = 1;
  196. uint16_t data_invalid_rhoh, data_invalid_rloh, data_invalid;
  197. uint16_t dv_window = 0;
  198. uint16_t en_lo, en_hi;
  199. uint16_t acc_clks;
  200. uint16_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt;
  201. dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
  202. __FILE__, __LINE__, __func__);
  203. en_lo = CEIL_DIV(Trp[mode], CLK_X);
  204. en_hi = CEIL_DIV(Treh[mode], CLK_X);
  205. #if ONFI_BLOOM_TIME
  206. if ((en_hi * CLK_X) < (Treh[mode] + 2))
  207. en_hi++;
  208. #endif
  209. if ((en_lo + en_hi) * CLK_X < Trc[mode])
  210. en_lo += CEIL_DIV((Trc[mode] - (en_lo + en_hi) * CLK_X), CLK_X);
  211. if ((en_lo + en_hi) < CLK_MULTI)
  212. en_lo += CLK_MULTI - en_lo - en_hi;
  213. while (dv_window < 8) {
  214. data_invalid_rhoh = en_lo * CLK_X + Trhoh[mode];
  215. data_invalid_rloh = (en_lo + en_hi) * CLK_X + Trloh[mode];
  216. data_invalid =
  217. data_invalid_rhoh <
  218. data_invalid_rloh ? data_invalid_rhoh : data_invalid_rloh;
  219. dv_window = data_invalid - Trea[mode];
  220. if (dv_window < 8)
  221. en_lo++;
  222. }
  223. acc_clks = CEIL_DIV(Trea[mode], CLK_X);
  224. while (((acc_clks * CLK_X) - Trea[mode]) < 3)
  225. acc_clks++;
  226. if ((data_invalid - acc_clks * CLK_X) < 2)
  227. dev_warn(denali->dev, "%s, Line %d: Warning!\n",
  228. __FILE__, __LINE__);
  229. addr_2_data = CEIL_DIV(Tadl[mode], CLK_X);
  230. re_2_we = CEIL_DIV(Trhw[mode], CLK_X);
  231. re_2_re = CEIL_DIV(Trhz[mode], CLK_X);
  232. we_2_re = CEIL_DIV(Twhr[mode], CLK_X);
  233. cs_cnt = CEIL_DIV((Tcs[mode] - Trp[mode]), CLK_X);
  234. if (!TclsRising)
  235. cs_cnt = CEIL_DIV(Tcs[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. #if MODE5_WORKAROUND
  243. if (mode == 5)
  244. acc_clks = 5;
  245. #endif
  246. /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */
  247. if ((ioread32(denali->flash_reg + MANUFACTURER_ID) == 0) &&
  248. (ioread32(denali->flash_reg + DEVICE_ID) == 0x88))
  249. acc_clks = 6;
  250. iowrite32(acc_clks, denali->flash_reg + ACC_CLKS);
  251. iowrite32(re_2_we, denali->flash_reg + RE_2_WE);
  252. iowrite32(re_2_re, denali->flash_reg + RE_2_RE);
  253. iowrite32(we_2_re, denali->flash_reg + WE_2_RE);
  254. iowrite32(addr_2_data, denali->flash_reg + ADDR_2_DATA);
  255. iowrite32(en_lo, denali->flash_reg + RDWR_EN_LO_CNT);
  256. iowrite32(en_hi, denali->flash_reg + RDWR_EN_HI_CNT);
  257. iowrite32(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
  258. }
  259. /* queries the NAND device to see what ONFI modes it supports. */
  260. static uint16_t get_onfi_nand_para(struct denali_nand_info *denali)
  261. {
  262. int i;
  263. /*
  264. * we needn't to do a reset here because driver has already
  265. * reset all the banks before
  266. */
  267. if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
  268. ONFI_TIMING_MODE__VALUE))
  269. return FAIL;
  270. for (i = 5; i > 0; i--) {
  271. if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
  272. (0x01 << i))
  273. break;
  274. }
  275. nand_onfi_timing_set(denali, i);
  276. /*
  277. * By now, all the ONFI devices we know support the page cache
  278. * rw feature. So here we enable the pipeline_rw_ahead feature
  279. */
  280. /* iowrite32(1, denali->flash_reg + CACHE_WRITE_ENABLE); */
  281. /* iowrite32(1, denali->flash_reg + CACHE_READ_ENABLE); */
  282. return PASS;
  283. }
  284. static void get_samsung_nand_para(struct denali_nand_info *denali,
  285. uint8_t device_id)
  286. {
  287. if (device_id == 0xd3) { /* Samsung K9WAG08U1A */
  288. /* Set timing register values according to datasheet */
  289. iowrite32(5, denali->flash_reg + ACC_CLKS);
  290. iowrite32(20, denali->flash_reg + RE_2_WE);
  291. iowrite32(12, denali->flash_reg + WE_2_RE);
  292. iowrite32(14, denali->flash_reg + ADDR_2_DATA);
  293. iowrite32(3, denali->flash_reg + RDWR_EN_LO_CNT);
  294. iowrite32(2, denali->flash_reg + RDWR_EN_HI_CNT);
  295. iowrite32(2, denali->flash_reg + CS_SETUP_CNT);
  296. }
  297. }
  298. static void get_toshiba_nand_para(struct denali_nand_info *denali)
  299. {
  300. uint32_t tmp;
  301. /*
  302. * Workaround to fix a controller bug which reports a wrong
  303. * spare area size for some kind of Toshiba NAND device
  304. */
  305. if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) &&
  306. (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) {
  307. iowrite32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  308. tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) *
  309. ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  310. iowrite32(tmp,
  311. denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
  312. #if SUPPORT_15BITECC
  313. iowrite32(15, denali->flash_reg + ECC_CORRECTION);
  314. #elif SUPPORT_8BITECC
  315. iowrite32(8, denali->flash_reg + ECC_CORRECTION);
  316. #endif
  317. }
  318. }
  319. static void get_hynix_nand_para(struct denali_nand_info *denali,
  320. uint8_t device_id)
  321. {
  322. uint32_t main_size, spare_size;
  323. switch (device_id) {
  324. case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
  325. case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
  326. iowrite32(128, denali->flash_reg + PAGES_PER_BLOCK);
  327. iowrite32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
  328. iowrite32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  329. main_size = 4096 *
  330. ioread32(denali->flash_reg + DEVICES_CONNECTED);
  331. spare_size = 224 *
  332. ioread32(denali->flash_reg + DEVICES_CONNECTED);
  333. iowrite32(main_size,
  334. denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
  335. iowrite32(spare_size,
  336. denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
  337. iowrite32(0, denali->flash_reg + DEVICE_WIDTH);
  338. #if SUPPORT_15BITECC
  339. iowrite32(15, denali->flash_reg + ECC_CORRECTION);
  340. #elif SUPPORT_8BITECC
  341. iowrite32(8, denali->flash_reg + ECC_CORRECTION);
  342. #endif
  343. break;
  344. default:
  345. dev_warn(denali->dev,
  346. "Spectra: Unknown Hynix NAND (Device ID: 0x%x)."
  347. "Will use default parameter values instead.\n",
  348. device_id);
  349. }
  350. }
  351. /*
  352. * determines how many NAND chips are connected to the controller. Note for
  353. * Intel CE4100 devices we don't support more than one device.
  354. */
  355. static void find_valid_banks(struct denali_nand_info *denali)
  356. {
  357. uint32_t id[denali->max_banks];
  358. int i;
  359. denali->total_used_banks = 1;
  360. for (i = 0; i < denali->max_banks; i++) {
  361. index_addr(denali, MODE_11 | (i << 24) | 0, 0x90);
  362. index_addr(denali, MODE_11 | (i << 24) | 1, 0);
  363. index_addr_read_data(denali,
  364. MODE_11 | (i << 24) | 2, &id[i]);
  365. dev_dbg(denali->dev,
  366. "Return 1st ID for bank[%d]: %x\n", i, id[i]);
  367. if (i == 0) {
  368. if (!(id[i] & 0x0ff))
  369. break; /* WTF? */
  370. } else {
  371. if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
  372. denali->total_used_banks++;
  373. else
  374. break;
  375. }
  376. }
  377. if (denali->platform == INTEL_CE4100) {
  378. /*
  379. * Platform limitations of the CE4100 device limit
  380. * users to a single chip solution for NAND.
  381. * Multichip support is not enabled.
  382. */
  383. if (denali->total_used_banks != 1) {
  384. dev_err(denali->dev,
  385. "Sorry, Intel CE4100 only supports "
  386. "a single NAND device.\n");
  387. BUG();
  388. }
  389. }
  390. dev_dbg(denali->dev,
  391. "denali->total_used_banks: %d\n", denali->total_used_banks);
  392. }
  393. /*
  394. * Use the configuration feature register to determine the maximum number of
  395. * banks that the hardware supports.
  396. */
  397. static void detect_max_banks(struct denali_nand_info *denali)
  398. {
  399. uint32_t features = ioread32(denali->flash_reg + FEATURES);
  400. denali->max_banks = 2 << (features & FEATURES__N_BANKS);
  401. }
  402. static void detect_partition_feature(struct denali_nand_info *denali)
  403. {
  404. /*
  405. * For MRST platform, denali->fwblks represent the
  406. * number of blocks firmware is taken,
  407. * FW is in protect partition and MTD driver has no
  408. * permission to access it. So let driver know how many
  409. * blocks it can't touch.
  410. */
  411. if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
  412. if ((ioread32(denali->flash_reg + PERM_SRC_ID(1)) &
  413. PERM_SRC_ID__SRCID) == SPECTRA_PARTITION_ID) {
  414. denali->fwblks =
  415. ((ioread32(denali->flash_reg + MIN_MAX_BANK(1)) &
  416. MIN_MAX_BANK__MIN_VALUE) *
  417. denali->blksperchip)
  418. +
  419. (ioread32(denali->flash_reg + MIN_BLK_ADDR(1)) &
  420. MIN_BLK_ADDR__VALUE);
  421. } else
  422. denali->fwblks = SPECTRA_START_BLOCK;
  423. } else
  424. denali->fwblks = SPECTRA_START_BLOCK;
  425. }
  426. static uint16_t denali_nand_timing_set(struct denali_nand_info *denali)
  427. {
  428. uint16_t status = PASS;
  429. uint32_t id_bytes[8], addr;
  430. uint8_t maf_id, device_id;
  431. int i;
  432. dev_dbg(denali->dev,
  433. "%s, Line %d, Function: %s\n",
  434. __FILE__, __LINE__, __func__);
  435. /*
  436. * Use read id method to get device ID and other params.
  437. * For some NAND chips, controller can't report the correct
  438. * device ID by reading from DEVICE_ID register
  439. */
  440. addr = MODE_11 | BANK(denali->flash_bank);
  441. index_addr(denali, addr | 0, 0x90);
  442. index_addr(denali, addr | 1, 0);
  443. for (i = 0; i < 8; i++)
  444. index_addr_read_data(denali, addr | 2, &id_bytes[i]);
  445. maf_id = id_bytes[0];
  446. device_id = id_bytes[1];
  447. if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
  448. ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
  449. if (FAIL == get_onfi_nand_para(denali))
  450. return FAIL;
  451. } else if (maf_id == 0xEC) { /* Samsung NAND */
  452. get_samsung_nand_para(denali, device_id);
  453. } else if (maf_id == 0x98) { /* Toshiba NAND */
  454. get_toshiba_nand_para(denali);
  455. } else if (maf_id == 0xAD) { /* Hynix NAND */
  456. get_hynix_nand_para(denali, device_id);
  457. }
  458. dev_info(denali->dev,
  459. "Dump timing register values:"
  460. "acc_clks: %d, re_2_we: %d, re_2_re: %d\n"
  461. "we_2_re: %d, addr_2_data: %d, rdwr_en_lo_cnt: %d\n"
  462. "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n",
  463. ioread32(denali->flash_reg + ACC_CLKS),
  464. ioread32(denali->flash_reg + RE_2_WE),
  465. ioread32(denali->flash_reg + RE_2_RE),
  466. ioread32(denali->flash_reg + WE_2_RE),
  467. ioread32(denali->flash_reg + ADDR_2_DATA),
  468. ioread32(denali->flash_reg + RDWR_EN_LO_CNT),
  469. ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
  470. ioread32(denali->flash_reg + CS_SETUP_CNT));
  471. find_valid_banks(denali);
  472. detect_partition_feature(denali);
  473. /*
  474. * If the user specified to override the default timings
  475. * with a specific ONFI mode, we apply those changes here.
  476. */
  477. if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
  478. nand_onfi_timing_set(denali, onfi_timing_mode);
  479. return status;
  480. }
  481. static void denali_set_intr_modes(struct denali_nand_info *denali,
  482. uint16_t INT_ENABLE)
  483. {
  484. dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
  485. __FILE__, __LINE__, __func__);
  486. if (INT_ENABLE)
  487. iowrite32(1, denali->flash_reg + GLOBAL_INT_ENABLE);
  488. else
  489. iowrite32(0, denali->flash_reg + GLOBAL_INT_ENABLE);
  490. }
  491. /*
  492. * validation function to verify that the controlling software is making
  493. * a valid request
  494. */
  495. static inline bool is_flash_bank_valid(int flash_bank)
  496. {
  497. return (flash_bank >= 0 && flash_bank < 4);
  498. }
  499. static void denali_irq_init(struct denali_nand_info *denali)
  500. {
  501. uint32_t int_mask;
  502. int i;
  503. /* Disable global interrupts */
  504. denali_set_intr_modes(denali, false);
  505. int_mask = DENALI_IRQ_ALL;
  506. /* Clear all status bits */
  507. for (i = 0; i < denali->max_banks; ++i)
  508. iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS(i));
  509. denali_irq_enable(denali, int_mask);
  510. }
  511. static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali)
  512. {
  513. denali_set_intr_modes(denali, false);
  514. free_irq(irqnum, denali);
  515. }
  516. static void denali_irq_enable(struct denali_nand_info *denali,
  517. uint32_t int_mask)
  518. {
  519. int i;
  520. for (i = 0; i < denali->max_banks; ++i)
  521. iowrite32(int_mask, denali->flash_reg + INTR_EN(i));
  522. }
  523. /*
  524. * This function only returns when an interrupt that this driver cares about
  525. * occurs. This is to reduce the overhead of servicing interrupts
  526. */
  527. static inline uint32_t denali_irq_detected(struct denali_nand_info *denali)
  528. {
  529. return read_interrupt_status(denali) & DENALI_IRQ_ALL;
  530. }
  531. /* Interrupts are cleared by writing a 1 to the appropriate status bit */
  532. static inline void clear_interrupt(struct denali_nand_info *denali,
  533. uint32_t irq_mask)
  534. {
  535. uint32_t intr_status_reg;
  536. intr_status_reg = INTR_STATUS(denali->flash_bank);
  537. iowrite32(irq_mask, denali->flash_reg + intr_status_reg);
  538. }
  539. static void clear_interrupts(struct denali_nand_info *denali)
  540. {
  541. uint32_t status;
  542. spin_lock_irq(&denali->irq_lock);
  543. status = read_interrupt_status(denali);
  544. clear_interrupt(denali, status);
  545. denali->irq_status = 0x0;
  546. spin_unlock_irq(&denali->irq_lock);
  547. }
  548. static uint32_t read_interrupt_status(struct denali_nand_info *denali)
  549. {
  550. uint32_t intr_status_reg;
  551. intr_status_reg = INTR_STATUS(denali->flash_bank);
  552. return ioread32(denali->flash_reg + intr_status_reg);
  553. }
  554. /*
  555. * This is the interrupt service routine. It handles all interrupts
  556. * sent to this device. Note that on CE4100, this is a shared interrupt.
  557. */
  558. static irqreturn_t denali_isr(int irq, void *dev_id)
  559. {
  560. struct denali_nand_info *denali = dev_id;
  561. uint32_t irq_status;
  562. irqreturn_t result = IRQ_NONE;
  563. spin_lock(&denali->irq_lock);
  564. /* check to see if a valid NAND chip has been selected. */
  565. if (is_flash_bank_valid(denali->flash_bank)) {
  566. /*
  567. * check to see if controller generated the interrupt,
  568. * since this is a shared interrupt
  569. */
  570. irq_status = denali_irq_detected(denali);
  571. if (irq_status != 0) {
  572. /* handle interrupt */
  573. /* first acknowledge it */
  574. clear_interrupt(denali, irq_status);
  575. /*
  576. * store the status in the device context for someone
  577. * to read
  578. */
  579. denali->irq_status |= irq_status;
  580. /* notify anyone who cares that it happened */
  581. complete(&denali->complete);
  582. /* tell the OS that we've handled this */
  583. result = IRQ_HANDLED;
  584. }
  585. }
  586. spin_unlock(&denali->irq_lock);
  587. return result;
  588. }
  589. #define BANK(x) ((x) << 24)
  590. static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
  591. {
  592. unsigned long comp_res;
  593. uint32_t intr_status;
  594. unsigned long timeout = msecs_to_jiffies(1000);
  595. do {
  596. comp_res =
  597. wait_for_completion_timeout(&denali->complete, timeout);
  598. spin_lock_irq(&denali->irq_lock);
  599. intr_status = denali->irq_status;
  600. if (intr_status & irq_mask) {
  601. denali->irq_status &= ~irq_mask;
  602. spin_unlock_irq(&denali->irq_lock);
  603. /* our interrupt was detected */
  604. break;
  605. } else {
  606. /*
  607. * these are not the interrupts you are looking for -
  608. * need to wait again
  609. */
  610. spin_unlock_irq(&denali->irq_lock);
  611. }
  612. } while (comp_res != 0);
  613. if (comp_res == 0) {
  614. /* timeout */
  615. pr_err("timeout occurred, status = 0x%x, mask = 0x%x\n",
  616. intr_status, irq_mask);
  617. intr_status = 0;
  618. }
  619. return intr_status;
  620. }
  621. /*
  622. * This helper function setups the registers for ECC and whether or not
  623. * the spare area will be transferred.
  624. */
  625. static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
  626. bool transfer_spare)
  627. {
  628. int ecc_en_flag, transfer_spare_flag;
  629. /* set ECC, transfer spare bits if needed */
  630. ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
  631. transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
  632. /* Enable spare area/ECC per user's request. */
  633. iowrite32(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
  634. iowrite32(transfer_spare_flag,
  635. denali->flash_reg + TRANSFER_SPARE_REG);
  636. }
  637. /*
  638. * sends a pipeline command operation to the controller. See the Denali NAND
  639. * controller's user guide for more information (section 4.2.3.6).
  640. */
  641. static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
  642. bool ecc_en,
  643. bool transfer_spare,
  644. int access_type,
  645. int op)
  646. {
  647. int status = PASS;
  648. uint32_t page_count = 1;
  649. uint32_t addr, cmd, irq_status, irq_mask;
  650. if (op == DENALI_READ)
  651. irq_mask = INTR_STATUS__LOAD_COMP;
  652. else if (op == DENALI_WRITE)
  653. irq_mask = 0;
  654. else
  655. BUG();
  656. setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
  657. clear_interrupts(denali);
  658. addr = BANK(denali->flash_bank) | denali->page;
  659. if (op == DENALI_WRITE && access_type != SPARE_ACCESS) {
  660. cmd = MODE_01 | addr;
  661. iowrite32(cmd, denali->flash_mem);
  662. } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) {
  663. /* read spare area */
  664. cmd = MODE_10 | addr;
  665. index_addr(denali, cmd, access_type);
  666. cmd = MODE_01 | addr;
  667. iowrite32(cmd, denali->flash_mem);
  668. } else if (op == DENALI_READ) {
  669. /* setup page read request for access type */
  670. cmd = MODE_10 | addr;
  671. index_addr(denali, cmd, access_type);
  672. /*
  673. * page 33 of the NAND controller spec indicates we should not
  674. * use the pipeline commands in Spare area only mode.
  675. * So we don't.
  676. */
  677. if (access_type == SPARE_ACCESS) {
  678. cmd = MODE_01 | addr;
  679. iowrite32(cmd, denali->flash_mem);
  680. } else {
  681. index_addr(denali, cmd,
  682. PIPELINE_ACCESS | op | page_count);
  683. /*
  684. * wait for command to be accepted
  685. * can always use status0 bit as the
  686. * mask is identical for each bank.
  687. */
  688. irq_status = wait_for_irq(denali, irq_mask);
  689. if (irq_status == 0) {
  690. dev_err(denali->dev,
  691. "cmd, page, addr on timeout "
  692. "(0x%x, 0x%x, 0x%x)\n",
  693. cmd, denali->page, addr);
  694. status = FAIL;
  695. } else {
  696. cmd = MODE_01 | addr;
  697. iowrite32(cmd, denali->flash_mem);
  698. }
  699. }
  700. }
  701. return status;
  702. }
  703. /* helper function that simply writes a buffer to the flash */
  704. static int write_data_to_flash_mem(struct denali_nand_info *denali,
  705. const uint8_t *buf,
  706. int len)
  707. {
  708. uint32_t *buf32;
  709. int i;
  710. /*
  711. * verify that the len is a multiple of 4.
  712. * see comment in read_data_from_flash_mem()
  713. */
  714. BUG_ON((len % 4) != 0);
  715. /* write the data to the flash memory */
  716. buf32 = (uint32_t *)buf;
  717. for (i = 0; i < len / 4; i++)
  718. iowrite32(*buf32++, denali->flash_mem + 0x10);
  719. return i*4; /* intent is to return the number of bytes read */
  720. }
  721. /* helper function that simply reads a buffer from the flash */
  722. static int read_data_from_flash_mem(struct denali_nand_info *denali,
  723. uint8_t *buf,
  724. int len)
  725. {
  726. uint32_t *buf32;
  727. int i;
  728. /*
  729. * we assume that len will be a multiple of 4, if not it would be nice
  730. * to know about it ASAP rather than have random failures...
  731. * This assumption is based on the fact that this function is designed
  732. * to be used to read flash pages, which are typically multiples of 4.
  733. */
  734. BUG_ON((len % 4) != 0);
  735. /* transfer the data from the flash */
  736. buf32 = (uint32_t *)buf;
  737. for (i = 0; i < len / 4; i++)
  738. *buf32++ = ioread32(denali->flash_mem + 0x10);
  739. return i*4; /* intent is to return the number of bytes read */
  740. }
  741. /* writes OOB data to the device */
  742. static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
  743. {
  744. struct denali_nand_info *denali = mtd_to_denali(mtd);
  745. uint32_t irq_status;
  746. uint32_t irq_mask = INTR_STATUS__PROGRAM_COMP |
  747. INTR_STATUS__PROGRAM_FAIL;
  748. int status = 0;
  749. denali->page = page;
  750. if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS,
  751. DENALI_WRITE) == PASS) {
  752. write_data_to_flash_mem(denali, buf, mtd->oobsize);
  753. /* wait for operation to complete */
  754. irq_status = wait_for_irq(denali, irq_mask);
  755. if (irq_status == 0) {
  756. dev_err(denali->dev, "OOB write failed\n");
  757. status = -EIO;
  758. }
  759. } else {
  760. dev_err(denali->dev, "unable to send pipeline command\n");
  761. status = -EIO;
  762. }
  763. return status;
  764. }
  765. /* reads OOB data from the device */
  766. static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
  767. {
  768. struct denali_nand_info *denali = mtd_to_denali(mtd);
  769. uint32_t irq_mask = INTR_STATUS__LOAD_COMP;
  770. uint32_t irq_status, addr, cmd;
  771. denali->page = page;
  772. if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
  773. DENALI_READ) == PASS) {
  774. read_data_from_flash_mem(denali, buf, mtd->oobsize);
  775. /*
  776. * wait for command to be accepted
  777. * can always use status0 bit as the
  778. * mask is identical for each bank.
  779. */
  780. irq_status = wait_for_irq(denali, irq_mask);
  781. if (irq_status == 0)
  782. dev_err(denali->dev, "page on OOB timeout %d\n",
  783. denali->page);
  784. /*
  785. * We set the device back to MAIN_ACCESS here as I observed
  786. * instability with the controller if you do a block erase
  787. * and the last transaction was a SPARE_ACCESS. Block erase
  788. * is reliable (according to the MTD test infrastructure)
  789. * if you are in MAIN_ACCESS.
  790. */
  791. addr = BANK(denali->flash_bank) | denali->page;
  792. cmd = MODE_10 | addr;
  793. index_addr(denali, cmd, MAIN_ACCESS);
  794. }
  795. }
  796. /*
  797. * this function examines buffers to see if they contain data that
  798. * indicate that the buffer is part of an erased region of flash.
  799. */
  800. static bool is_erased(uint8_t *buf, int len)
  801. {
  802. int i;
  803. for (i = 0; i < len; i++)
  804. if (buf[i] != 0xFF)
  805. return false;
  806. return true;
  807. }
  808. #define ECC_SECTOR_SIZE 512
  809. #define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12)
  810. #define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET))
  811. #define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK)
  812. #define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO__ERROR_TYPE))
  813. #define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8)
  814. #define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
  815. static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
  816. uint32_t irq_status, unsigned int *max_bitflips)
  817. {
  818. bool check_erased_page = false;
  819. unsigned int bitflips = 0;
  820. if (irq_status & INTR_STATUS__ECC_ERR) {
  821. /* read the ECC errors. we'll ignore them for now */
  822. uint32_t err_address, err_correction_info, err_byte,
  823. err_sector, err_device, err_correction_value;
  824. denali_set_intr_modes(denali, false);
  825. do {
  826. err_address = ioread32(denali->flash_reg +
  827. ECC_ERROR_ADDRESS);
  828. err_sector = ECC_SECTOR(err_address);
  829. err_byte = ECC_BYTE(err_address);
  830. err_correction_info = ioread32(denali->flash_reg +
  831. ERR_CORRECTION_INFO);
  832. err_correction_value =
  833. ECC_CORRECTION_VALUE(err_correction_info);
  834. err_device = ECC_ERR_DEVICE(err_correction_info);
  835. if (ECC_ERROR_CORRECTABLE(err_correction_info)) {
  836. /*
  837. * If err_byte is larger than ECC_SECTOR_SIZE,
  838. * means error happened in OOB, so we ignore
  839. * it. It's no need for us to correct it
  840. * err_device is represented the NAND error
  841. * bits are happened in if there are more
  842. * than one NAND connected.
  843. */
  844. if (err_byte < ECC_SECTOR_SIZE) {
  845. int offset;
  846. offset = (err_sector *
  847. ECC_SECTOR_SIZE +
  848. err_byte) *
  849. denali->devnum +
  850. err_device;
  851. /* correct the ECC error */
  852. buf[offset] ^= err_correction_value;
  853. denali->mtd.ecc_stats.corrected++;
  854. bitflips++;
  855. }
  856. } else {
  857. /*
  858. * if the error is not correctable, need to
  859. * look at the page to see if it is an erased
  860. * page. if so, then it's not a real ECC error
  861. */
  862. check_erased_page = true;
  863. }
  864. } while (!ECC_LAST_ERR(err_correction_info));
  865. /*
  866. * Once handle all ecc errors, controller will triger
  867. * a ECC_TRANSACTION_DONE interrupt, so here just wait
  868. * for a while for this interrupt
  869. */
  870. while (!(read_interrupt_status(denali) &
  871. INTR_STATUS__ECC_TRANSACTION_DONE))
  872. cpu_relax();
  873. clear_interrupts(denali);
  874. denali_set_intr_modes(denali, true);
  875. }
  876. *max_bitflips = bitflips;
  877. return check_erased_page;
  878. }
  879. /* programs the controller to either enable/disable DMA transfers */
  880. static void denali_enable_dma(struct denali_nand_info *denali, bool en)
  881. {
  882. iowrite32(en ? DMA_ENABLE__FLAG : 0, denali->flash_reg + DMA_ENABLE);
  883. ioread32(denali->flash_reg + DMA_ENABLE);
  884. }
  885. /* setups the HW to perform the data DMA */
  886. static void denali_setup_dma(struct denali_nand_info *denali, int op)
  887. {
  888. uint32_t mode;
  889. const int page_count = 1;
  890. uint32_t addr = denali->buf.dma_buf;
  891. mode = MODE_10 | BANK(denali->flash_bank);
  892. /* DMA is a four step process */
  893. /* 1. setup transfer type and # of pages */
  894. index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
  895. /* 2. set memory high address bits 23:8 */
  896. index_addr(denali, mode | ((addr >> 16) << 8), 0x2200);
  897. /* 3. set memory low address bits 23:8 */
  898. index_addr(denali, mode | ((addr & 0xff) << 8), 0x2300);
  899. /* 4. interrupt when complete, burst len = 64 bytes */
  900. index_addr(denali, mode | 0x14000, 0x2400);
  901. }
  902. /*
  903. * writes a page. user specifies type, and this function handles the
  904. * configuration details.
  905. */
  906. static int write_page(struct mtd_info *mtd, struct nand_chip *chip,
  907. const uint8_t *buf, bool raw_xfer)
  908. {
  909. struct denali_nand_info *denali = mtd_to_denali(mtd);
  910. dma_addr_t addr = denali->buf.dma_buf;
  911. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  912. uint32_t irq_status;
  913. uint32_t irq_mask = INTR_STATUS__DMA_CMD_COMP |
  914. INTR_STATUS__PROGRAM_FAIL;
  915. /*
  916. * if it is a raw xfer, we want to disable ecc and send the spare area.
  917. * !raw_xfer - enable ecc
  918. * raw_xfer - transfer spare
  919. */
  920. setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer);
  921. /* copy buffer into DMA buffer */
  922. memcpy(denali->buf.buf, buf, mtd->writesize);
  923. if (raw_xfer) {
  924. /* transfer the data to the spare area */
  925. memcpy(denali->buf.buf + mtd->writesize,
  926. chip->oob_poi,
  927. mtd->oobsize);
  928. }
  929. dma_sync_single_for_device(denali->dev, addr, size, DMA_TO_DEVICE);
  930. clear_interrupts(denali);
  931. denali_enable_dma(denali, true);
  932. denali_setup_dma(denali, DENALI_WRITE);
  933. /* wait for operation to complete */
  934. irq_status = wait_for_irq(denali, irq_mask);
  935. if (irq_status == 0) {
  936. dev_err(denali->dev,
  937. "timeout on write_page (type = %d)\n",
  938. raw_xfer);
  939. denali->status = NAND_STATUS_FAIL;
  940. }
  941. denali_enable_dma(denali, false);
  942. dma_sync_single_for_cpu(denali->dev, addr, size, DMA_TO_DEVICE);
  943. return 0;
  944. }
  945. /* NAND core entry points */
  946. /*
  947. * this is the callback that the NAND core calls to write a page. Since
  948. * writing a page with ECC or without is similar, all the work is done
  949. * by write_page above.
  950. */
  951. static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  952. const uint8_t *buf, int oob_required)
  953. {
  954. /*
  955. * for regular page writes, we let HW handle all the ECC
  956. * data written to the device.
  957. */
  958. return write_page(mtd, chip, buf, false);
  959. }
  960. /*
  961. * This is the callback that the NAND core calls to write a page without ECC.
  962. * raw access is similar to ECC page writes, so all the work is done in the
  963. * write_page() function above.
  964. */
  965. static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  966. const uint8_t *buf, int oob_required)
  967. {
  968. /*
  969. * for raw page writes, we want to disable ECC and simply write
  970. * whatever data is in the buffer.
  971. */
  972. return write_page(mtd, chip, buf, true);
  973. }
  974. static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  975. int page)
  976. {
  977. return write_oob_data(mtd, chip->oob_poi, page);
  978. }
  979. static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  980. int page)
  981. {
  982. read_oob_data(mtd, chip->oob_poi, page);
  983. return 0;
  984. }
  985. static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  986. uint8_t *buf, int oob_required, int page)
  987. {
  988. unsigned int max_bitflips;
  989. struct denali_nand_info *denali = mtd_to_denali(mtd);
  990. dma_addr_t addr = denali->buf.dma_buf;
  991. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  992. uint32_t irq_status;
  993. uint32_t irq_mask = INTR_STATUS__ECC_TRANSACTION_DONE |
  994. INTR_STATUS__ECC_ERR;
  995. bool check_erased_page = false;
  996. if (page != denali->page) {
  997. dev_err(denali->dev, "IN %s: page %d is not"
  998. " equal to denali->page %d, investigate!!",
  999. __func__, page, denali->page);
  1000. BUG();
  1001. }
  1002. setup_ecc_for_xfer(denali, true, false);
  1003. denali_enable_dma(denali, true);
  1004. dma_sync_single_for_device(denali->dev, addr, size, DMA_FROM_DEVICE);
  1005. clear_interrupts(denali);
  1006. denali_setup_dma(denali, DENALI_READ);
  1007. /* wait for operation to complete */
  1008. irq_status = wait_for_irq(denali, irq_mask);
  1009. dma_sync_single_for_cpu(denali->dev, addr, size, DMA_FROM_DEVICE);
  1010. memcpy(buf, denali->buf.buf, mtd->writesize);
  1011. check_erased_page = handle_ecc(denali, buf, irq_status, &max_bitflips);
  1012. denali_enable_dma(denali, false);
  1013. if (check_erased_page) {
  1014. read_oob_data(&denali->mtd, chip->oob_poi, denali->page);
  1015. /* check ECC failures that may have occurred on erased pages */
  1016. if (check_erased_page) {
  1017. if (!is_erased(buf, denali->mtd.writesize))
  1018. denali->mtd.ecc_stats.failed++;
  1019. if (!is_erased(buf, denali->mtd.oobsize))
  1020. denali->mtd.ecc_stats.failed++;
  1021. }
  1022. }
  1023. return max_bitflips;
  1024. }
  1025. static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1026. uint8_t *buf, int oob_required, int page)
  1027. {
  1028. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1029. dma_addr_t addr = denali->buf.dma_buf;
  1030. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  1031. uint32_t irq_status;
  1032. uint32_t irq_mask = INTR_STATUS__DMA_CMD_COMP;
  1033. if (page != denali->page) {
  1034. dev_err(denali->dev, "IN %s: page %d is not"
  1035. " equal to denali->page %d, investigate!!",
  1036. __func__, page, denali->page);
  1037. BUG();
  1038. }
  1039. setup_ecc_for_xfer(denali, false, true);
  1040. denali_enable_dma(denali, true);
  1041. dma_sync_single_for_device(denali->dev, addr, size, DMA_FROM_DEVICE);
  1042. clear_interrupts(denali);
  1043. denali_setup_dma(denali, DENALI_READ);
  1044. /* wait for operation to complete */
  1045. irq_status = wait_for_irq(denali, irq_mask);
  1046. dma_sync_single_for_cpu(denali->dev, addr, size, DMA_FROM_DEVICE);
  1047. denali_enable_dma(denali, false);
  1048. memcpy(buf, denali->buf.buf, mtd->writesize);
  1049. memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize);
  1050. return 0;
  1051. }
  1052. static uint8_t denali_read_byte(struct mtd_info *mtd)
  1053. {
  1054. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1055. uint8_t result = 0xff;
  1056. if (denali->buf.head < denali->buf.tail)
  1057. result = denali->buf.buf[denali->buf.head++];
  1058. return result;
  1059. }
  1060. static void denali_select_chip(struct mtd_info *mtd, int chip)
  1061. {
  1062. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1063. spin_lock_irq(&denali->irq_lock);
  1064. denali->flash_bank = chip;
  1065. spin_unlock_irq(&denali->irq_lock);
  1066. }
  1067. static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  1068. {
  1069. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1070. int status = denali->status;
  1071. denali->status = 0;
  1072. return status;
  1073. }
  1074. static int denali_erase(struct mtd_info *mtd, int page)
  1075. {
  1076. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1077. uint32_t cmd, irq_status;
  1078. clear_interrupts(denali);
  1079. /* setup page read request for access type */
  1080. cmd = MODE_10 | BANK(denali->flash_bank) | page;
  1081. index_addr(denali, cmd, 0x1);
  1082. /* wait for erase to complete or failure to occur */
  1083. irq_status = wait_for_irq(denali, INTR_STATUS__ERASE_COMP |
  1084. INTR_STATUS__ERASE_FAIL);
  1085. return (irq_status & INTR_STATUS__ERASE_FAIL) ? NAND_STATUS_FAIL : PASS;
  1086. }
  1087. static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
  1088. int page)
  1089. {
  1090. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1091. uint32_t addr, id;
  1092. int i;
  1093. switch (cmd) {
  1094. case NAND_CMD_PAGEPROG:
  1095. break;
  1096. case NAND_CMD_STATUS:
  1097. read_status(denali);
  1098. break;
  1099. case NAND_CMD_READID:
  1100. case NAND_CMD_PARAM:
  1101. reset_buf(denali);
  1102. /*
  1103. * sometimes ManufactureId read from register is not right
  1104. * e.g. some of Micron MT29F32G08QAA MLC NAND chips
  1105. * So here we send READID cmd to NAND insteand
  1106. */
  1107. addr = MODE_11 | BANK(denali->flash_bank);
  1108. index_addr(denali, addr | 0, 0x90);
  1109. index_addr(denali, addr | 1, 0);
  1110. for (i = 0; i < 8; i++) {
  1111. index_addr_read_data(denali,
  1112. addr | 2,
  1113. &id);
  1114. write_byte_to_buf(denali, id);
  1115. }
  1116. break;
  1117. case NAND_CMD_READ0:
  1118. case NAND_CMD_SEQIN:
  1119. denali->page = page;
  1120. break;
  1121. case NAND_CMD_RESET:
  1122. reset_bank(denali);
  1123. break;
  1124. case NAND_CMD_READOOB:
  1125. /* TODO: Read OOB data */
  1126. break;
  1127. default:
  1128. pr_err(": unsupported command received 0x%x\n", cmd);
  1129. break;
  1130. }
  1131. }
  1132. /* stubs for ECC functions not used by the NAND core */
  1133. static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
  1134. uint8_t *ecc_code)
  1135. {
  1136. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1137. dev_err(denali->dev,
  1138. "denali_ecc_calculate called unexpectedly\n");
  1139. BUG();
  1140. return -EIO;
  1141. }
  1142. static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
  1143. uint8_t *read_ecc, uint8_t *calc_ecc)
  1144. {
  1145. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1146. dev_err(denali->dev,
  1147. "denali_ecc_correct called unexpectedly\n");
  1148. BUG();
  1149. return -EIO;
  1150. }
  1151. static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
  1152. {
  1153. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1154. dev_err(denali->dev,
  1155. "denali_ecc_hwctl called unexpectedly\n");
  1156. BUG();
  1157. }
  1158. /* end NAND core entry points */
  1159. /* Initialization code to bring the device up to a known good state */
  1160. static void denali_hw_init(struct denali_nand_info *denali)
  1161. {
  1162. /*
  1163. * tell driver how many bit controller will skip before
  1164. * writing ECC code in OOB, this register may be already
  1165. * set by firmware. So we read this value out.
  1166. * if this value is 0, just let it be.
  1167. */
  1168. denali->bbtskipbytes = ioread32(denali->flash_reg +
  1169. SPARE_AREA_SKIP_BYTES);
  1170. detect_max_banks(denali);
  1171. denali_nand_reset(denali);
  1172. iowrite32(0x0F, denali->flash_reg + RB_PIN_ENABLED);
  1173. iowrite32(CHIP_EN_DONT_CARE__FLAG,
  1174. denali->flash_reg + CHIP_ENABLE_DONT_CARE);
  1175. iowrite32(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
  1176. /* Should set value for these registers when init */
  1177. iowrite32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
  1178. iowrite32(1, denali->flash_reg + ECC_ENABLE);
  1179. denali_nand_timing_set(denali);
  1180. denali_irq_init(denali);
  1181. }
  1182. /*
  1183. * Althogh controller spec said SLC ECC is forceb to be 4bit,
  1184. * but denali controller in MRST only support 15bit and 8bit ECC
  1185. * correction
  1186. */
  1187. #define ECC_8BITS 14
  1188. static struct nand_ecclayout nand_8bit_oob = {
  1189. .eccbytes = 14,
  1190. };
  1191. #define ECC_15BITS 26
  1192. static struct nand_ecclayout nand_15bit_oob = {
  1193. .eccbytes = 26,
  1194. };
  1195. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1196. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1197. static struct nand_bbt_descr bbt_main_descr = {
  1198. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1199. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1200. .offs = 8,
  1201. .len = 4,
  1202. .veroffs = 12,
  1203. .maxblocks = 4,
  1204. .pattern = bbt_pattern,
  1205. };
  1206. static struct nand_bbt_descr bbt_mirror_descr = {
  1207. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1208. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1209. .offs = 8,
  1210. .len = 4,
  1211. .veroffs = 12,
  1212. .maxblocks = 4,
  1213. .pattern = mirror_pattern,
  1214. };
  1215. /* initialize driver data structures */
  1216. static void denali_drv_init(struct denali_nand_info *denali)
  1217. {
  1218. denali->idx = 0;
  1219. /* setup interrupt handler */
  1220. /*
  1221. * the completion object will be used to notify
  1222. * the callee that the interrupt is done
  1223. */
  1224. init_completion(&denali->complete);
  1225. /*
  1226. * the spinlock will be used to synchronize the ISR with any
  1227. * element that might be access shared data (interrupt status)
  1228. */
  1229. spin_lock_init(&denali->irq_lock);
  1230. /* indicate that MTD has not selected a valid bank yet */
  1231. denali->flash_bank = CHIP_SELECT_INVALID;
  1232. /* initialize our irq_status variable to indicate no interrupts */
  1233. denali->irq_status = 0;
  1234. }
  1235. int denali_init(struct denali_nand_info *denali)
  1236. {
  1237. int ret;
  1238. if (denali->platform == INTEL_CE4100) {
  1239. /*
  1240. * Due to a silicon limitation, we can only support
  1241. * ONFI timing mode 1 and below.
  1242. */
  1243. if (onfi_timing_mode < -1 || onfi_timing_mode > 1) {
  1244. pr_err("Intel CE4100 only supports ONFI timing mode 1 or below\n");
  1245. return -EINVAL;
  1246. }
  1247. }
  1248. /* allocate a temporary buffer for nand_scan_ident() */
  1249. denali->buf.buf = devm_kzalloc(denali->dev, PAGE_SIZE,
  1250. GFP_DMA | GFP_KERNEL);
  1251. if (!denali->buf.buf)
  1252. return -ENOMEM;
  1253. denali->mtd.dev.parent = denali->dev;
  1254. denali_hw_init(denali);
  1255. denali_drv_init(denali);
  1256. /*
  1257. * denali_isr register is done after all the hardware
  1258. * initilization is finished
  1259. */
  1260. if (request_irq(denali->irq, denali_isr, IRQF_SHARED,
  1261. DENALI_NAND_NAME, denali)) {
  1262. pr_err("Spectra: Unable to allocate IRQ\n");
  1263. return -ENODEV;
  1264. }
  1265. /* now that our ISR is registered, we can enable interrupts */
  1266. denali_set_intr_modes(denali, true);
  1267. denali->mtd.name = "denali-nand";
  1268. denali->mtd.owner = THIS_MODULE;
  1269. denali->mtd.priv = &denali->nand;
  1270. /* register the driver with the NAND core subsystem */
  1271. denali->nand.select_chip = denali_select_chip;
  1272. denali->nand.cmdfunc = denali_cmdfunc;
  1273. denali->nand.read_byte = denali_read_byte;
  1274. denali->nand.waitfunc = denali_waitfunc;
  1275. /*
  1276. * scan for NAND devices attached to the controller
  1277. * this is the first stage in a two step process to register
  1278. * with the nand subsystem
  1279. */
  1280. if (nand_scan_ident(&denali->mtd, denali->max_banks, NULL)) {
  1281. ret = -ENXIO;
  1282. goto failed_req_irq;
  1283. }
  1284. /* allocate the right size buffer now */
  1285. devm_kfree(denali->dev, denali->buf.buf);
  1286. denali->buf.buf = devm_kzalloc(denali->dev,
  1287. denali->mtd.writesize + denali->mtd.oobsize,
  1288. GFP_KERNEL);
  1289. if (!denali->buf.buf) {
  1290. ret = -ENOMEM;
  1291. goto failed_req_irq;
  1292. }
  1293. /* Is 32-bit DMA supported? */
  1294. ret = dma_set_mask(denali->dev, DMA_BIT_MASK(32));
  1295. if (ret) {
  1296. pr_err("Spectra: no usable DMA configuration\n");
  1297. goto failed_req_irq;
  1298. }
  1299. denali->buf.dma_buf = dma_map_single(denali->dev, denali->buf.buf,
  1300. denali->mtd.writesize + denali->mtd.oobsize,
  1301. DMA_BIDIRECTIONAL);
  1302. if (dma_mapping_error(denali->dev, denali->buf.dma_buf)) {
  1303. dev_err(denali->dev, "Spectra: failed to map DMA buffer\n");
  1304. ret = -EIO;
  1305. goto failed_req_irq;
  1306. }
  1307. /*
  1308. * support for multi nand
  1309. * MTD known nothing about multi nand, so we should tell it
  1310. * the real pagesize and anything necessery
  1311. */
  1312. denali->devnum = ioread32(denali->flash_reg + DEVICES_CONNECTED);
  1313. denali->nand.chipsize <<= (denali->devnum - 1);
  1314. denali->nand.page_shift += (denali->devnum - 1);
  1315. denali->nand.pagemask = (denali->nand.chipsize >>
  1316. denali->nand.page_shift) - 1;
  1317. denali->nand.bbt_erase_shift += (denali->devnum - 1);
  1318. denali->nand.phys_erase_shift = denali->nand.bbt_erase_shift;
  1319. denali->nand.chip_shift += (denali->devnum - 1);
  1320. denali->mtd.writesize <<= (denali->devnum - 1);
  1321. denali->mtd.oobsize <<= (denali->devnum - 1);
  1322. denali->mtd.erasesize <<= (denali->devnum - 1);
  1323. denali->mtd.size = denali->nand.numchips * denali->nand.chipsize;
  1324. denali->bbtskipbytes *= denali->devnum;
  1325. /*
  1326. * second stage of the NAND scan
  1327. * this stage requires information regarding ECC and
  1328. * bad block management.
  1329. */
  1330. /* Bad block management */
  1331. denali->nand.bbt_td = &bbt_main_descr;
  1332. denali->nand.bbt_md = &bbt_mirror_descr;
  1333. /* skip the scan for now until we have OOB read and write support */
  1334. denali->nand.bbt_options |= NAND_BBT_USE_FLASH;
  1335. denali->nand.options |= NAND_SKIP_BBTSCAN;
  1336. denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  1337. /*
  1338. * Denali Controller only support 15bit and 8bit ECC in MRST,
  1339. * so just let controller do 15bit ECC for MLC and 8bit ECC for
  1340. * SLC if possible.
  1341. * */
  1342. if (!nand_is_slc(&denali->nand) &&
  1343. (denali->mtd.oobsize > (denali->bbtskipbytes +
  1344. ECC_15BITS * (denali->mtd.writesize /
  1345. ECC_SECTOR_SIZE)))) {
  1346. /* if MLC OOB size is large enough, use 15bit ECC*/
  1347. denali->nand.ecc.strength = 15;
  1348. denali->nand.ecc.layout = &nand_15bit_oob;
  1349. denali->nand.ecc.bytes = ECC_15BITS;
  1350. iowrite32(15, denali->flash_reg + ECC_CORRECTION);
  1351. } else if (denali->mtd.oobsize < (denali->bbtskipbytes +
  1352. ECC_8BITS * (denali->mtd.writesize /
  1353. ECC_SECTOR_SIZE))) {
  1354. pr_err("Your NAND chip OOB is not large enough to \
  1355. contain 8bit ECC correction codes");
  1356. goto failed_req_irq;
  1357. } else {
  1358. denali->nand.ecc.strength = 8;
  1359. denali->nand.ecc.layout = &nand_8bit_oob;
  1360. denali->nand.ecc.bytes = ECC_8BITS;
  1361. iowrite32(8, denali->flash_reg + ECC_CORRECTION);
  1362. }
  1363. denali->nand.ecc.bytes *= denali->devnum;
  1364. denali->nand.ecc.strength *= denali->devnum;
  1365. denali->nand.ecc.layout->eccbytes *=
  1366. denali->mtd.writesize / ECC_SECTOR_SIZE;
  1367. denali->nand.ecc.layout->oobfree[0].offset =
  1368. denali->bbtskipbytes + denali->nand.ecc.layout->eccbytes;
  1369. denali->nand.ecc.layout->oobfree[0].length =
  1370. denali->mtd.oobsize - denali->nand.ecc.layout->eccbytes -
  1371. denali->bbtskipbytes;
  1372. /*
  1373. * Let driver know the total blocks number and how many blocks
  1374. * contained by each nand chip. blksperchip will help driver to
  1375. * know how many blocks is taken by FW.
  1376. */
  1377. denali->totalblks = denali->mtd.size >>
  1378. denali->nand.phys_erase_shift;
  1379. denali->blksperchip = denali->totalblks / denali->nand.numchips;
  1380. /*
  1381. * These functions are required by the NAND core framework, otherwise,
  1382. * the NAND core will assert. However, we don't need them, so we'll stub
  1383. * them out.
  1384. */
  1385. denali->nand.ecc.calculate = denali_ecc_calculate;
  1386. denali->nand.ecc.correct = denali_ecc_correct;
  1387. denali->nand.ecc.hwctl = denali_ecc_hwctl;
  1388. /* override the default read operations */
  1389. denali->nand.ecc.size = ECC_SECTOR_SIZE * denali->devnum;
  1390. denali->nand.ecc.read_page = denali_read_page;
  1391. denali->nand.ecc.read_page_raw = denali_read_page_raw;
  1392. denali->nand.ecc.write_page = denali_write_page;
  1393. denali->nand.ecc.write_page_raw = denali_write_page_raw;
  1394. denali->nand.ecc.read_oob = denali_read_oob;
  1395. denali->nand.ecc.write_oob = denali_write_oob;
  1396. denali->nand.erase = denali_erase;
  1397. if (nand_scan_tail(&denali->mtd)) {
  1398. ret = -ENXIO;
  1399. goto failed_req_irq;
  1400. }
  1401. ret = mtd_device_register(&denali->mtd, NULL, 0);
  1402. if (ret) {
  1403. dev_err(denali->dev, "Spectra: Failed to register MTD: %d\n",
  1404. ret);
  1405. goto failed_req_irq;
  1406. }
  1407. return 0;
  1408. failed_req_irq:
  1409. denali_irq_cleanup(denali->irq, denali);
  1410. return ret;
  1411. }
  1412. EXPORT_SYMBOL(denali_init);
  1413. /* driver exit point */
  1414. void denali_remove(struct denali_nand_info *denali)
  1415. {
  1416. denali_irq_cleanup(denali->irq, denali);
  1417. dma_unmap_single(denali->dev, denali->buf.dma_buf,
  1418. denali->mtd.writesize + denali->mtd.oobsize,
  1419. DMA_BIDIRECTIONAL);
  1420. }
  1421. EXPORT_SYMBOL(denali_remove);