denali.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  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, unsigned int *max_bitflips)
  784. {
  785. bool check_erased_page = false;
  786. unsigned int bitflips = 0;
  787. if (irq_status & INTR_STATUS__ECC_ERR) {
  788. /* read the ECC errors. we'll ignore them for now */
  789. uint32_t err_address = 0, err_correction_info = 0;
  790. uint32_t err_byte = 0, err_sector = 0, err_device = 0;
  791. uint32_t err_correction_value = 0;
  792. denali_set_intr_modes(denali, false);
  793. do {
  794. err_address = ioread32(denali->flash_reg +
  795. ECC_ERROR_ADDRESS);
  796. err_sector = ECC_SECTOR(err_address);
  797. err_byte = ECC_BYTE(err_address);
  798. err_correction_info = ioread32(denali->flash_reg +
  799. ERR_CORRECTION_INFO);
  800. err_correction_value =
  801. ECC_CORRECTION_VALUE(err_correction_info);
  802. err_device = ECC_ERR_DEVICE(err_correction_info);
  803. if (ECC_ERROR_CORRECTABLE(err_correction_info)) {
  804. /* If err_byte is larger than ECC_SECTOR_SIZE,
  805. * means error happened in OOB, so we ignore
  806. * it. It's no need for us to correct it
  807. * err_device is represented the NAND error
  808. * bits are happened in if there are more
  809. * than one NAND connected.
  810. * */
  811. if (err_byte < ECC_SECTOR_SIZE) {
  812. int offset;
  813. offset = (err_sector *
  814. ECC_SECTOR_SIZE +
  815. err_byte) *
  816. denali->devnum +
  817. err_device;
  818. /* correct the ECC error */
  819. buf[offset] ^= err_correction_value;
  820. denali->mtd.ecc_stats.corrected++;
  821. bitflips++;
  822. }
  823. } else {
  824. /* if the error is not correctable, need to
  825. * look at the page to see if it is an erased
  826. * page. if so, then it's not a real ECC error
  827. * */
  828. check_erased_page = true;
  829. }
  830. } while (!ECC_LAST_ERR(err_correction_info));
  831. /* Once handle all ecc errors, controller will triger
  832. * a ECC_TRANSACTION_DONE interrupt, so here just wait
  833. * for a while for this interrupt
  834. * */
  835. while (!(read_interrupt_status(denali) &
  836. INTR_STATUS__ECC_TRANSACTION_DONE))
  837. cpu_relax();
  838. clear_interrupts(denali);
  839. denali_set_intr_modes(denali, true);
  840. }
  841. *max_bitflips = bitflips;
  842. return check_erased_page;
  843. }
  844. /* programs the controller to either enable/disable DMA transfers */
  845. static void denali_enable_dma(struct denali_nand_info *denali, bool en)
  846. {
  847. uint32_t reg_val = 0x0;
  848. if (en)
  849. reg_val = DMA_ENABLE__FLAG;
  850. iowrite32(reg_val, denali->flash_reg + DMA_ENABLE);
  851. ioread32(denali->flash_reg + DMA_ENABLE);
  852. }
  853. /* setups the HW to perform the data DMA */
  854. static void denali_setup_dma(struct denali_nand_info *denali, int op)
  855. {
  856. uint32_t mode = 0x0;
  857. const int page_count = 1;
  858. dma_addr_t addr = denali->buf.dma_buf;
  859. mode = MODE_10 | BANK(denali->flash_bank);
  860. /* DMA is a four step process */
  861. /* 1. setup transfer type and # of pages */
  862. index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
  863. /* 2. set memory high address bits 23:8 */
  864. index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
  865. /* 3. set memory low address bits 23:8 */
  866. index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
  867. /* 4. interrupt when complete, burst len = 64 bytes*/
  868. index_addr(denali, mode | 0x14000, 0x2400);
  869. }
  870. /* writes a page. user specifies type, and this function handles the
  871. * configuration details. */
  872. static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
  873. const uint8_t *buf, bool raw_xfer)
  874. {
  875. struct denali_nand_info *denali = mtd_to_denali(mtd);
  876. dma_addr_t addr = denali->buf.dma_buf;
  877. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  878. uint32_t irq_status = 0;
  879. uint32_t irq_mask = INTR_STATUS__DMA_CMD_COMP |
  880. INTR_STATUS__PROGRAM_FAIL;
  881. /* if it is a raw xfer, we want to disable ecc, and send
  882. * the spare area.
  883. * !raw_xfer - enable ecc
  884. * raw_xfer - transfer spare
  885. */
  886. setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer);
  887. /* copy buffer into DMA buffer */
  888. memcpy(denali->buf.buf, buf, mtd->writesize);
  889. if (raw_xfer) {
  890. /* transfer the data to the spare area */
  891. memcpy(denali->buf.buf + mtd->writesize,
  892. chip->oob_poi,
  893. mtd->oobsize);
  894. }
  895. dma_sync_single_for_device(denali->dev, addr, size, DMA_TO_DEVICE);
  896. clear_interrupts(denali);
  897. denali_enable_dma(denali, true);
  898. denali_setup_dma(denali, DENALI_WRITE);
  899. /* wait for operation to complete */
  900. irq_status = wait_for_irq(denali, irq_mask);
  901. if (irq_status == 0) {
  902. dev_err(denali->dev,
  903. "timeout on write_page (type = %d)\n",
  904. raw_xfer);
  905. denali->status =
  906. (irq_status & INTR_STATUS__PROGRAM_FAIL) ?
  907. NAND_STATUS_FAIL : PASS;
  908. }
  909. denali_enable_dma(denali, false);
  910. dma_sync_single_for_cpu(denali->dev, addr, size, DMA_TO_DEVICE);
  911. }
  912. /* NAND core entry points */
  913. /* this is the callback that the NAND core calls to write a page. Since
  914. * writing a page with ECC or without is similar, all the work is done
  915. * by write_page above.
  916. * */
  917. static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  918. const uint8_t *buf, int oob_required)
  919. {
  920. /* for regular page writes, we let HW handle all the ECC
  921. * data written to the device. */
  922. write_page(mtd, chip, buf, false);
  923. }
  924. /* This is the callback that the NAND core calls to write a page without ECC.
  925. * raw access is similar to ECC page writes, so all the work is done in the
  926. * write_page() function above.
  927. */
  928. static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  929. const uint8_t *buf, int oob_required)
  930. {
  931. /* for raw page writes, we want to disable ECC and simply write
  932. whatever data is in the buffer. */
  933. write_page(mtd, chip, buf, true);
  934. }
  935. static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  936. int page)
  937. {
  938. return write_oob_data(mtd, chip->oob_poi, page);
  939. }
  940. static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  941. int page)
  942. {
  943. read_oob_data(mtd, chip->oob_poi, page);
  944. return 0;
  945. }
  946. static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  947. uint8_t *buf, int oob_required, int page)
  948. {
  949. unsigned int max_bitflips;
  950. struct denali_nand_info *denali = mtd_to_denali(mtd);
  951. dma_addr_t addr = denali->buf.dma_buf;
  952. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  953. uint32_t irq_status = 0;
  954. uint32_t irq_mask = INTR_STATUS__ECC_TRANSACTION_DONE |
  955. INTR_STATUS__ECC_ERR;
  956. bool check_erased_page = false;
  957. if (page != denali->page) {
  958. dev_err(denali->dev, "IN %s: page %d is not"
  959. " equal to denali->page %d, investigate!!",
  960. __func__, page, denali->page);
  961. BUG();
  962. }
  963. setup_ecc_for_xfer(denali, true, false);
  964. denali_enable_dma(denali, true);
  965. dma_sync_single_for_device(denali->dev, addr, size, DMA_FROM_DEVICE);
  966. clear_interrupts(denali);
  967. denali_setup_dma(denali, DENALI_READ);
  968. /* wait for operation to complete */
  969. irq_status = wait_for_irq(denali, irq_mask);
  970. dma_sync_single_for_cpu(denali->dev, addr, size, DMA_FROM_DEVICE);
  971. memcpy(buf, denali->buf.buf, mtd->writesize);
  972. check_erased_page = handle_ecc(denali, buf, irq_status, &max_bitflips);
  973. denali_enable_dma(denali, false);
  974. if (check_erased_page) {
  975. read_oob_data(&denali->mtd, chip->oob_poi, denali->page);
  976. /* check ECC failures that may have occurred on erased pages */
  977. if (check_erased_page) {
  978. if (!is_erased(buf, denali->mtd.writesize))
  979. denali->mtd.ecc_stats.failed++;
  980. if (!is_erased(buf, denali->mtd.oobsize))
  981. denali->mtd.ecc_stats.failed++;
  982. }
  983. }
  984. return max_bitflips;
  985. }
  986. static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  987. uint8_t *buf, int oob_required, int page)
  988. {
  989. struct denali_nand_info *denali = mtd_to_denali(mtd);
  990. dma_addr_t addr = denali->buf.dma_buf;
  991. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  992. uint32_t irq_status = 0;
  993. uint32_t irq_mask = INTR_STATUS__DMA_CMD_COMP;
  994. if (page != denali->page) {
  995. dev_err(denali->dev, "IN %s: page %d is not"
  996. " equal to denali->page %d, investigate!!",
  997. __func__, page, denali->page);
  998. BUG();
  999. }
  1000. setup_ecc_for_xfer(denali, false, true);
  1001. denali_enable_dma(denali, true);
  1002. dma_sync_single_for_device(denali->dev, addr, size, DMA_FROM_DEVICE);
  1003. clear_interrupts(denali);
  1004. denali_setup_dma(denali, DENALI_READ);
  1005. /* wait for operation to complete */
  1006. irq_status = wait_for_irq(denali, irq_mask);
  1007. dma_sync_single_for_cpu(denali->dev, addr, size, DMA_FROM_DEVICE);
  1008. denali_enable_dma(denali, false);
  1009. memcpy(buf, denali->buf.buf, mtd->writesize);
  1010. memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize);
  1011. return 0;
  1012. }
  1013. static uint8_t denali_read_byte(struct mtd_info *mtd)
  1014. {
  1015. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1016. uint8_t result = 0xff;
  1017. if (denali->buf.head < denali->buf.tail)
  1018. result = denali->buf.buf[denali->buf.head++];
  1019. return result;
  1020. }
  1021. static void denali_select_chip(struct mtd_info *mtd, int chip)
  1022. {
  1023. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1024. spin_lock_irq(&denali->irq_lock);
  1025. denali->flash_bank = chip;
  1026. spin_unlock_irq(&denali->irq_lock);
  1027. }
  1028. static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  1029. {
  1030. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1031. int status = denali->status;
  1032. denali->status = 0;
  1033. return status;
  1034. }
  1035. static void denali_erase(struct mtd_info *mtd, int page)
  1036. {
  1037. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1038. uint32_t cmd = 0x0, irq_status = 0;
  1039. /* clear interrupts */
  1040. clear_interrupts(denali);
  1041. /* setup page read request for access type */
  1042. cmd = MODE_10 | BANK(denali->flash_bank) | page;
  1043. index_addr(denali, (uint32_t)cmd, 0x1);
  1044. /* wait for erase to complete or failure to occur */
  1045. irq_status = wait_for_irq(denali, INTR_STATUS__ERASE_COMP |
  1046. INTR_STATUS__ERASE_FAIL);
  1047. denali->status = (irq_status & INTR_STATUS__ERASE_FAIL) ?
  1048. NAND_STATUS_FAIL : PASS;
  1049. }
  1050. static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
  1051. int page)
  1052. {
  1053. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1054. uint32_t addr, id;
  1055. int i;
  1056. switch (cmd) {
  1057. case NAND_CMD_PAGEPROG:
  1058. break;
  1059. case NAND_CMD_STATUS:
  1060. read_status(denali);
  1061. break;
  1062. case NAND_CMD_READID:
  1063. case NAND_CMD_PARAM:
  1064. reset_buf(denali);
  1065. /*sometimes ManufactureId read from register is not right
  1066. * e.g. some of Micron MT29F32G08QAA MLC NAND chips
  1067. * So here we send READID cmd to NAND insteand
  1068. * */
  1069. addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
  1070. index_addr(denali, (uint32_t)addr | 0, 0x90);
  1071. index_addr(denali, (uint32_t)addr | 1, 0);
  1072. for (i = 0; i < 5; i++) {
  1073. index_addr_read_data(denali,
  1074. (uint32_t)addr | 2,
  1075. &id);
  1076. write_byte_to_buf(denali, id);
  1077. }
  1078. break;
  1079. case NAND_CMD_READ0:
  1080. case NAND_CMD_SEQIN:
  1081. denali->page = page;
  1082. break;
  1083. case NAND_CMD_RESET:
  1084. reset_bank(denali);
  1085. break;
  1086. case NAND_CMD_READOOB:
  1087. /* TODO: Read OOB data */
  1088. break;
  1089. default:
  1090. printk(KERN_ERR ": unsupported command"
  1091. " received 0x%x\n", cmd);
  1092. break;
  1093. }
  1094. }
  1095. /* stubs for ECC functions not used by the NAND core */
  1096. static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
  1097. uint8_t *ecc_code)
  1098. {
  1099. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1100. dev_err(denali->dev,
  1101. "denali_ecc_calculate called unexpectedly\n");
  1102. BUG();
  1103. return -EIO;
  1104. }
  1105. static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
  1106. uint8_t *read_ecc, uint8_t *calc_ecc)
  1107. {
  1108. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1109. dev_err(denali->dev,
  1110. "denali_ecc_correct called unexpectedly\n");
  1111. BUG();
  1112. return -EIO;
  1113. }
  1114. static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
  1115. {
  1116. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1117. dev_err(denali->dev,
  1118. "denali_ecc_hwctl called unexpectedly\n");
  1119. BUG();
  1120. }
  1121. /* end NAND core entry points */
  1122. /* Initialization code to bring the device up to a known good state */
  1123. static void denali_hw_init(struct denali_nand_info *denali)
  1124. {
  1125. /* tell driver how many bit controller will skip before
  1126. * writing ECC code in OOB, this register may be already
  1127. * set by firmware. So we read this value out.
  1128. * if this value is 0, just let it be.
  1129. * */
  1130. denali->bbtskipbytes = ioread32(denali->flash_reg +
  1131. SPARE_AREA_SKIP_BYTES);
  1132. detect_max_banks(denali);
  1133. denali_nand_reset(denali);
  1134. iowrite32(0x0F, denali->flash_reg + RB_PIN_ENABLED);
  1135. iowrite32(CHIP_EN_DONT_CARE__FLAG,
  1136. denali->flash_reg + CHIP_ENABLE_DONT_CARE);
  1137. iowrite32(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
  1138. /* Should set value for these registers when init */
  1139. iowrite32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
  1140. iowrite32(1, denali->flash_reg + ECC_ENABLE);
  1141. denali_nand_timing_set(denali);
  1142. denali_irq_init(denali);
  1143. }
  1144. /* Althogh controller spec said SLC ECC is forceb to be 4bit,
  1145. * but denali controller in MRST only support 15bit and 8bit ECC
  1146. * correction
  1147. * */
  1148. #define ECC_8BITS 14
  1149. static struct nand_ecclayout nand_8bit_oob = {
  1150. .eccbytes = 14,
  1151. };
  1152. #define ECC_15BITS 26
  1153. static struct nand_ecclayout nand_15bit_oob = {
  1154. .eccbytes = 26,
  1155. };
  1156. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1157. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1158. static struct nand_bbt_descr bbt_main_descr = {
  1159. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1160. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1161. .offs = 8,
  1162. .len = 4,
  1163. .veroffs = 12,
  1164. .maxblocks = 4,
  1165. .pattern = bbt_pattern,
  1166. };
  1167. static struct nand_bbt_descr bbt_mirror_descr = {
  1168. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1169. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1170. .offs = 8,
  1171. .len = 4,
  1172. .veroffs = 12,
  1173. .maxblocks = 4,
  1174. .pattern = mirror_pattern,
  1175. };
  1176. /* initialize driver data structures */
  1177. void denali_drv_init(struct denali_nand_info *denali)
  1178. {
  1179. denali->idx = 0;
  1180. /* setup interrupt handler */
  1181. /* the completion object will be used to notify
  1182. * the callee that the interrupt is done */
  1183. init_completion(&denali->complete);
  1184. /* the spinlock will be used to synchronize the ISR
  1185. * with any element that might be access shared
  1186. * data (interrupt status) */
  1187. spin_lock_init(&denali->irq_lock);
  1188. /* indicate that MTD has not selected a valid bank yet */
  1189. denali->flash_bank = CHIP_SELECT_INVALID;
  1190. /* initialize our irq_status variable to indicate no interrupts */
  1191. denali->irq_status = 0;
  1192. }
  1193. /* driver entry point */
  1194. static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1195. {
  1196. int ret = -ENODEV;
  1197. resource_size_t csr_base, mem_base;
  1198. unsigned long csr_len, mem_len;
  1199. struct denali_nand_info *denali;
  1200. denali = kzalloc(sizeof(*denali), GFP_KERNEL);
  1201. if (!denali)
  1202. return -ENOMEM;
  1203. ret = pci_enable_device(dev);
  1204. if (ret) {
  1205. printk(KERN_ERR "Spectra: pci_enable_device failed.\n");
  1206. goto failed_alloc_memery;
  1207. }
  1208. if (id->driver_data == INTEL_CE4100) {
  1209. /* Due to a silicon limitation, we can only support
  1210. * ONFI timing mode 1 and below.
  1211. */
  1212. if (onfi_timing_mode < -1 || onfi_timing_mode > 1) {
  1213. printk(KERN_ERR "Intel CE4100 only supports"
  1214. " ONFI timing mode 1 or below\n");
  1215. ret = -EINVAL;
  1216. goto failed_enable_dev;
  1217. }
  1218. denali->platform = INTEL_CE4100;
  1219. mem_base = pci_resource_start(dev, 0);
  1220. mem_len = pci_resource_len(dev, 1);
  1221. csr_base = pci_resource_start(dev, 1);
  1222. csr_len = pci_resource_len(dev, 1);
  1223. } else {
  1224. denali->platform = INTEL_MRST;
  1225. csr_base = pci_resource_start(dev, 0);
  1226. csr_len = pci_resource_len(dev, 0);
  1227. mem_base = pci_resource_start(dev, 1);
  1228. mem_len = pci_resource_len(dev, 1);
  1229. if (!mem_len) {
  1230. mem_base = csr_base + csr_len;
  1231. mem_len = csr_len;
  1232. }
  1233. }
  1234. /* Is 32-bit DMA supported? */
  1235. ret = dma_set_mask(&dev->dev, DMA_BIT_MASK(32));
  1236. if (ret) {
  1237. printk(KERN_ERR "Spectra: no usable DMA configuration\n");
  1238. goto failed_enable_dev;
  1239. }
  1240. denali->buf.dma_buf = dma_map_single(&dev->dev, denali->buf.buf,
  1241. DENALI_BUF_SIZE,
  1242. DMA_BIDIRECTIONAL);
  1243. if (dma_mapping_error(&dev->dev, denali->buf.dma_buf)) {
  1244. dev_err(&dev->dev, "Spectra: failed to map DMA buffer\n");
  1245. goto failed_enable_dev;
  1246. }
  1247. pci_set_master(dev);
  1248. denali->dev = &dev->dev;
  1249. denali->mtd.dev.parent = &dev->dev;
  1250. ret = pci_request_regions(dev, DENALI_NAND_NAME);
  1251. if (ret) {
  1252. printk(KERN_ERR "Spectra: Unable to request memory regions\n");
  1253. goto failed_dma_map;
  1254. }
  1255. denali->flash_reg = ioremap_nocache(csr_base, csr_len);
  1256. if (!denali->flash_reg) {
  1257. printk(KERN_ERR "Spectra: Unable to remap memory region\n");
  1258. ret = -ENOMEM;
  1259. goto failed_req_regions;
  1260. }
  1261. denali->flash_mem = ioremap_nocache(mem_base, mem_len);
  1262. if (!denali->flash_mem) {
  1263. printk(KERN_ERR "Spectra: ioremap_nocache failed!");
  1264. ret = -ENOMEM;
  1265. goto failed_remap_reg;
  1266. }
  1267. denali_hw_init(denali);
  1268. denali_drv_init(denali);
  1269. /* denali_isr register is done after all the hardware
  1270. * initilization is finished*/
  1271. if (request_irq(dev->irq, denali_isr, IRQF_SHARED,
  1272. DENALI_NAND_NAME, denali)) {
  1273. printk(KERN_ERR "Spectra: Unable to allocate IRQ\n");
  1274. ret = -ENODEV;
  1275. goto failed_remap_mem;
  1276. }
  1277. /* now that our ISR is registered, we can enable interrupts */
  1278. denali_set_intr_modes(denali, true);
  1279. pci_set_drvdata(dev, denali);
  1280. denali->mtd.name = "denali-nand";
  1281. denali->mtd.owner = THIS_MODULE;
  1282. denali->mtd.priv = &denali->nand;
  1283. /* register the driver with the NAND core subsystem */
  1284. denali->nand.select_chip = denali_select_chip;
  1285. denali->nand.cmdfunc = denali_cmdfunc;
  1286. denali->nand.read_byte = denali_read_byte;
  1287. denali->nand.waitfunc = denali_waitfunc;
  1288. /* scan for NAND devices attached to the controller
  1289. * this is the first stage in a two step process to register
  1290. * with the nand subsystem */
  1291. if (nand_scan_ident(&denali->mtd, denali->max_banks, NULL)) {
  1292. ret = -ENXIO;
  1293. goto failed_req_irq;
  1294. }
  1295. /* MTD supported page sizes vary by kernel. We validate our
  1296. * kernel supports the device here.
  1297. */
  1298. if (denali->mtd.writesize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE) {
  1299. ret = -ENODEV;
  1300. printk(KERN_ERR "Spectra: device size not supported by this "
  1301. "version of MTD.");
  1302. goto failed_req_irq;
  1303. }
  1304. /* support for multi nand
  1305. * MTD known nothing about multi nand,
  1306. * so we should tell it the real pagesize
  1307. * and anything necessery
  1308. */
  1309. denali->devnum = ioread32(denali->flash_reg + DEVICES_CONNECTED);
  1310. denali->nand.chipsize <<= (denali->devnum - 1);
  1311. denali->nand.page_shift += (denali->devnum - 1);
  1312. denali->nand.pagemask = (denali->nand.chipsize >>
  1313. denali->nand.page_shift) - 1;
  1314. denali->nand.bbt_erase_shift += (denali->devnum - 1);
  1315. denali->nand.phys_erase_shift = denali->nand.bbt_erase_shift;
  1316. denali->nand.chip_shift += (denali->devnum - 1);
  1317. denali->mtd.writesize <<= (denali->devnum - 1);
  1318. denali->mtd.oobsize <<= (denali->devnum - 1);
  1319. denali->mtd.erasesize <<= (denali->devnum - 1);
  1320. denali->mtd.size = denali->nand.numchips * denali->nand.chipsize;
  1321. denali->bbtskipbytes *= denali->devnum;
  1322. /* second stage of the NAND scan
  1323. * this stage requires information regarding ECC and
  1324. * bad block management. */
  1325. /* Bad block management */
  1326. denali->nand.bbt_td = &bbt_main_descr;
  1327. denali->nand.bbt_md = &bbt_mirror_descr;
  1328. /* skip the scan for now until we have OOB read and write support */
  1329. denali->nand.bbt_options |= NAND_BBT_USE_FLASH;
  1330. denali->nand.options |= NAND_SKIP_BBTSCAN;
  1331. denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  1332. /* Denali Controller only support 15bit and 8bit ECC in MRST,
  1333. * so just let controller do 15bit ECC for MLC and 8bit ECC for
  1334. * SLC if possible.
  1335. * */
  1336. if (denali->nand.cellinfo & 0xc &&
  1337. (denali->mtd.oobsize > (denali->bbtskipbytes +
  1338. ECC_15BITS * (denali->mtd.writesize /
  1339. ECC_SECTOR_SIZE)))) {
  1340. /* if MLC OOB size is large enough, use 15bit ECC*/
  1341. denali->nand.ecc.strength = 15;
  1342. denali->nand.ecc.layout = &nand_15bit_oob;
  1343. denali->nand.ecc.bytes = ECC_15BITS;
  1344. iowrite32(15, denali->flash_reg + ECC_CORRECTION);
  1345. } else if (denali->mtd.oobsize < (denali->bbtskipbytes +
  1346. ECC_8BITS * (denali->mtd.writesize /
  1347. ECC_SECTOR_SIZE))) {
  1348. printk(KERN_ERR "Your NAND chip OOB is not large enough to"
  1349. " contain 8bit ECC correction codes");
  1350. goto failed_req_irq;
  1351. } else {
  1352. denali->nand.ecc.strength = 8;
  1353. denali->nand.ecc.layout = &nand_8bit_oob;
  1354. denali->nand.ecc.bytes = ECC_8BITS;
  1355. iowrite32(8, denali->flash_reg + ECC_CORRECTION);
  1356. }
  1357. denali->nand.ecc.bytes *= denali->devnum;
  1358. denali->nand.ecc.strength *= denali->devnum;
  1359. denali->nand.ecc.layout->eccbytes *=
  1360. denali->mtd.writesize / ECC_SECTOR_SIZE;
  1361. denali->nand.ecc.layout->oobfree[0].offset =
  1362. denali->bbtskipbytes + denali->nand.ecc.layout->eccbytes;
  1363. denali->nand.ecc.layout->oobfree[0].length =
  1364. denali->mtd.oobsize - denali->nand.ecc.layout->eccbytes -
  1365. denali->bbtskipbytes;
  1366. /* Let driver know the total blocks number and
  1367. * how many blocks contained by each nand chip.
  1368. * blksperchip will help driver to know how many
  1369. * blocks is taken by FW.
  1370. * */
  1371. denali->totalblks = denali->mtd.size >>
  1372. denali->nand.phys_erase_shift;
  1373. denali->blksperchip = denali->totalblks / denali->nand.numchips;
  1374. /* These functions are required by the NAND core framework, otherwise,
  1375. * the NAND core will assert. However, we don't need them, so we'll stub
  1376. * them out. */
  1377. denali->nand.ecc.calculate = denali_ecc_calculate;
  1378. denali->nand.ecc.correct = denali_ecc_correct;
  1379. denali->nand.ecc.hwctl = denali_ecc_hwctl;
  1380. /* override the default read operations */
  1381. denali->nand.ecc.size = ECC_SECTOR_SIZE * denali->devnum;
  1382. denali->nand.ecc.read_page = denali_read_page;
  1383. denali->nand.ecc.read_page_raw = denali_read_page_raw;
  1384. denali->nand.ecc.write_page = denali_write_page;
  1385. denali->nand.ecc.write_page_raw = denali_write_page_raw;
  1386. denali->nand.ecc.read_oob = denali_read_oob;
  1387. denali->nand.ecc.write_oob = denali_write_oob;
  1388. denali->nand.erase_cmd = denali_erase;
  1389. if (nand_scan_tail(&denali->mtd)) {
  1390. ret = -ENXIO;
  1391. goto failed_req_irq;
  1392. }
  1393. ret = mtd_device_register(&denali->mtd, NULL, 0);
  1394. if (ret) {
  1395. dev_err(&dev->dev, "Spectra: Failed to register MTD: %d\n",
  1396. ret);
  1397. goto failed_req_irq;
  1398. }
  1399. return 0;
  1400. failed_req_irq:
  1401. denali_irq_cleanup(dev->irq, denali);
  1402. failed_remap_mem:
  1403. iounmap(denali->flash_mem);
  1404. failed_remap_reg:
  1405. iounmap(denali->flash_reg);
  1406. failed_req_regions:
  1407. pci_release_regions(dev);
  1408. failed_dma_map:
  1409. dma_unmap_single(&dev->dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
  1410. DMA_BIDIRECTIONAL);
  1411. failed_enable_dev:
  1412. pci_disable_device(dev);
  1413. failed_alloc_memery:
  1414. kfree(denali);
  1415. return ret;
  1416. }
  1417. /* driver exit point */
  1418. static void denali_pci_remove(struct pci_dev *dev)
  1419. {
  1420. struct denali_nand_info *denali = pci_get_drvdata(dev);
  1421. nand_release(&denali->mtd);
  1422. denali_irq_cleanup(dev->irq, denali);
  1423. iounmap(denali->flash_reg);
  1424. iounmap(denali->flash_mem);
  1425. pci_release_regions(dev);
  1426. pci_disable_device(dev);
  1427. dma_unmap_single(&dev->dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
  1428. DMA_BIDIRECTIONAL);
  1429. pci_set_drvdata(dev, NULL);
  1430. kfree(denali);
  1431. }
  1432. MODULE_DEVICE_TABLE(pci, denali_pci_ids);
  1433. static struct pci_driver denali_pci_driver = {
  1434. .name = DENALI_NAND_NAME,
  1435. .id_table = denali_pci_ids,
  1436. .probe = denali_pci_probe,
  1437. .remove = denali_pci_remove,
  1438. };
  1439. module_pci_driver(denali_pci_driver);