denali.c 47 KB

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