denali.c 47 KB

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