denali.c 50 KB

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