denali.c 46 KB

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