denali.c 53 KB

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