denali.c 46 KB

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