denali.c 50 KB

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