denali.c 50 KB

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