denali.c 54 KB

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