denali.c 64 KB

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