denali.c 49 KB

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