denali.c 45 KB

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