denali.c 46 KB

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