denali.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  1. /*
  2. * NAND Flash Controller Device Driver
  3. * Copyright © 2009-2010, Intel Corporation and its suppliers.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include <linux/interrupt.h>
  20. #include <linux/delay.h>
  21. #include <linux/wait.h>
  22. #include <linux/mutex.h>
  23. #include <linux/pci.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/module.h>
  26. #include "denali.h"
  27. MODULE_LICENSE("GPL");
  28. /* We define a module parameter that allows the user to override
  29. * the hardware and decide what timing mode should be used.
  30. */
  31. #define NAND_DEFAULT_TIMINGS -1
  32. static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
  33. module_param(onfi_timing_mode, int, S_IRUGO);
  34. MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting."
  35. " -1 indicates use default timings");
  36. #define DENALI_NAND_NAME "denali-nand"
  37. /* We define a macro here that combines all interrupts this driver uses into
  38. * a single constant value, for convenience. */
  39. #define DENALI_IRQ_ALL (INTR_STATUS0__DMA_CMD_COMP | \
  40. INTR_STATUS0__ECC_TRANSACTION_DONE | \
  41. INTR_STATUS0__ECC_ERR | \
  42. INTR_STATUS0__PROGRAM_FAIL | \
  43. INTR_STATUS0__LOAD_COMP | \
  44. INTR_STATUS0__PROGRAM_COMP | \
  45. INTR_STATUS0__TIME_OUT | \
  46. INTR_STATUS0__ERASE_FAIL | \
  47. INTR_STATUS0__RST_COMP | \
  48. INTR_STATUS0__ERASE_COMP)
  49. /* indicates whether or not the internal value for the flash bank is
  50. valid or not */
  51. #define CHIP_SELECT_INVALID -1
  52. #define SUPPORT_8BITECC 1
  53. /* This macro divides two integers and rounds fractional values up
  54. * to the nearest integer value. */
  55. #define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y)))
  56. /* this macro allows us to convert from an MTD structure to our own
  57. * device context (denali) structure.
  58. */
  59. #define mtd_to_denali(m) container_of(m, struct denali_nand_info, mtd)
  60. /* These constants are defined by the driver to enable common driver
  61. configuration options. */
  62. #define SPARE_ACCESS 0x41
  63. #define MAIN_ACCESS 0x42
  64. #define MAIN_SPARE_ACCESS 0x43
  65. #define DENALI_READ 0
  66. #define DENALI_WRITE 0x100
  67. /* types of device accesses. We can issue commands and get status */
  68. #define COMMAND_CYCLE 0
  69. #define ADDR_CYCLE 1
  70. #define STATUS_CYCLE 2
  71. /* this is a helper macro that allows us to
  72. * format the bank into the proper bits for the controller */
  73. #define BANK(x) ((x) << 24)
  74. /* List of platforms this NAND controller has be integrated into */
  75. static const struct pci_device_id denali_pci_ids[] = {
  76. { PCI_VDEVICE(INTEL, 0x0701), INTEL_CE4100 },
  77. { PCI_VDEVICE(INTEL, 0x0809), INTEL_MRST },
  78. { /* end: all zeroes */ }
  79. };
  80. /* these are static lookup tables that give us easy access to
  81. registers in the NAND controller.
  82. */
  83. static const uint32_t intr_status_addresses[4] = {INTR_STATUS0,
  84. INTR_STATUS1,
  85. INTR_STATUS2,
  86. INTR_STATUS3};
  87. static const uint32_t device_reset_banks[4] = {DEVICE_RESET__BANK0,
  88. DEVICE_RESET__BANK1,
  89. DEVICE_RESET__BANK2,
  90. DEVICE_RESET__BANK3};
  91. static const uint32_t operation_timeout[4] = {INTR_STATUS0__TIME_OUT,
  92. INTR_STATUS1__TIME_OUT,
  93. INTR_STATUS2__TIME_OUT,
  94. INTR_STATUS3__TIME_OUT};
  95. static const uint32_t reset_complete[4] = {INTR_STATUS0__RST_COMP,
  96. INTR_STATUS1__RST_COMP,
  97. INTR_STATUS2__RST_COMP,
  98. INTR_STATUS3__RST_COMP};
  99. /* specifies the debug level of the driver */
  100. static int nand_debug_level;
  101. /* forward declarations */
  102. static void clear_interrupts(struct denali_nand_info *denali);
  103. static uint32_t wait_for_irq(struct denali_nand_info *denali,
  104. uint32_t irq_mask);
  105. static void denali_irq_enable(struct denali_nand_info *denali,
  106. uint32_t int_mask);
  107. static uint32_t read_interrupt_status(struct denali_nand_info *denali);
  108. #define DEBUG_DENALI 0
  109. /* This is a wrapper for writing to the denali registers.
  110. * this allows us to create debug information so we can
  111. * observe how the driver is programming the device.
  112. * it uses standard linux convention for (val, addr) */
  113. static void denali_write32(uint32_t value, void *addr)
  114. {
  115. iowrite32(value, addr);
  116. #if DEBUG_DENALI
  117. printk(KERN_INFO "wrote: 0x%x -> 0x%x\n", value,
  118. (uint32_t)((uint32_t)addr & 0x1fff));
  119. #endif
  120. }
  121. /* Certain operations for the denali NAND controller use
  122. * an indexed mode to read/write data. The operation is
  123. * performed by writing the address value of the command
  124. * to the device memory followed by the data. This function
  125. * abstracts this common operation.
  126. */
  127. static void index_addr(struct denali_nand_info *denali,
  128. uint32_t address, uint32_t data)
  129. {
  130. denali_write32(address, denali->flash_mem);
  131. denali_write32(data, denali->flash_mem + 0x10);
  132. }
  133. /* Perform an indexed read of the device */
  134. static void index_addr_read_data(struct denali_nand_info *denali,
  135. uint32_t address, uint32_t *pdata)
  136. {
  137. denali_write32(address, denali->flash_mem);
  138. *pdata = ioread32(denali->flash_mem + 0x10);
  139. }
  140. /* We need to buffer some data for some of the NAND core routines.
  141. * The operations manage buffering that data. */
  142. static void reset_buf(struct denali_nand_info *denali)
  143. {
  144. denali->buf.head = denali->buf.tail = 0;
  145. }
  146. static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
  147. {
  148. BUG_ON(denali->buf.tail >= sizeof(denali->buf.buf));
  149. denali->buf.buf[denali->buf.tail++] = byte;
  150. }
  151. /* reads the status of the device */
  152. static void read_status(struct denali_nand_info *denali)
  153. {
  154. uint32_t cmd = 0x0;
  155. /* initialize the data buffer to store status */
  156. reset_buf(denali);
  157. /* initiate a device status read */
  158. cmd = MODE_11 | BANK(denali->flash_bank);
  159. index_addr(denali, cmd | COMMAND_CYCLE, 0x70);
  160. denali_write32(cmd | STATUS_CYCLE, denali->flash_mem);
  161. /* update buffer with status value */
  162. write_byte_to_buf(denali, ioread32(denali->flash_mem + 0x10));
  163. #if DEBUG_DENALI
  164. printk(KERN_INFO "device reporting status value of 0x%2x\n",
  165. denali->buf.buf[0]);
  166. #endif
  167. }
  168. /* resets a specific device connected to the core */
  169. static void reset_bank(struct denali_nand_info *denali)
  170. {
  171. uint32_t irq_status = 0;
  172. uint32_t irq_mask = reset_complete[denali->flash_bank] |
  173. operation_timeout[denali->flash_bank];
  174. int bank = 0;
  175. clear_interrupts(denali);
  176. bank = device_reset_banks[denali->flash_bank];
  177. denali_write32(bank, denali->flash_reg + DEVICE_RESET);
  178. irq_status = wait_for_irq(denali, irq_mask);
  179. if (irq_status & operation_timeout[denali->flash_bank])
  180. printk(KERN_ERR "reset bank failed.\n");
  181. }
  182. /* Reset the flash controller */
  183. static uint16_t denali_nand_reset(struct denali_nand_info *denali)
  184. {
  185. uint32_t i;
  186. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  187. __FILE__, __LINE__, __func__);
  188. for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++)
  189. denali_write32(reset_complete[i] | operation_timeout[i],
  190. denali->flash_reg + intr_status_addresses[i]);
  191. for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) {
  192. denali_write32(device_reset_banks[i],
  193. denali->flash_reg + DEVICE_RESET);
  194. while (!(ioread32(denali->flash_reg +
  195. intr_status_addresses[i]) &
  196. (reset_complete[i] | operation_timeout[i])))
  197. ;
  198. if (ioread32(denali->flash_reg + intr_status_addresses[i]) &
  199. operation_timeout[i])
  200. nand_dbg_print(NAND_DBG_WARN,
  201. "NAND Reset operation timed out on bank %d\n", i);
  202. }
  203. for (i = 0; i < LLD_MAX_FLASH_BANKS; i++)
  204. denali_write32(reset_complete[i] | operation_timeout[i],
  205. denali->flash_reg + intr_status_addresses[i]);
  206. return PASS;
  207. }
  208. /* this routine calculates the ONFI timing values for a given mode and
  209. * programs the clocking register accordingly. The mode is determined by
  210. * the get_onfi_nand_para routine.
  211. */
  212. static void nand_onfi_timing_set(struct denali_nand_info *denali,
  213. uint16_t mode)
  214. {
  215. uint16_t Trea[6] = {40, 30, 25, 20, 20, 16};
  216. uint16_t Trp[6] = {50, 25, 17, 15, 12, 10};
  217. uint16_t Treh[6] = {30, 15, 15, 10, 10, 7};
  218. uint16_t Trc[6] = {100, 50, 35, 30, 25, 20};
  219. uint16_t Trhoh[6] = {0, 15, 15, 15, 15, 15};
  220. uint16_t Trloh[6] = {0, 0, 0, 0, 5, 5};
  221. uint16_t Tcea[6] = {100, 45, 30, 25, 25, 25};
  222. uint16_t Tadl[6] = {200, 100, 100, 100, 70, 70};
  223. uint16_t Trhw[6] = {200, 100, 100, 100, 100, 100};
  224. uint16_t Trhz[6] = {200, 100, 100, 100, 100, 100};
  225. uint16_t Twhr[6] = {120, 80, 80, 60, 60, 60};
  226. uint16_t Tcs[6] = {70, 35, 25, 25, 20, 15};
  227. uint16_t TclsRising = 1;
  228. uint16_t data_invalid_rhoh, data_invalid_rloh, data_invalid;
  229. uint16_t dv_window = 0;
  230. uint16_t en_lo, en_hi;
  231. uint16_t acc_clks;
  232. uint16_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt;
  233. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  234. __FILE__, __LINE__, __func__);
  235. en_lo = CEIL_DIV(Trp[mode], CLK_X);
  236. en_hi = CEIL_DIV(Treh[mode], CLK_X);
  237. #if ONFI_BLOOM_TIME
  238. if ((en_hi * CLK_X) < (Treh[mode] + 2))
  239. en_hi++;
  240. #endif
  241. if ((en_lo + en_hi) * CLK_X < Trc[mode])
  242. en_lo += CEIL_DIV((Trc[mode] - (en_lo + en_hi) * CLK_X), CLK_X);
  243. if ((en_lo + en_hi) < CLK_MULTI)
  244. en_lo += CLK_MULTI - en_lo - en_hi;
  245. while (dv_window < 8) {
  246. data_invalid_rhoh = en_lo * CLK_X + Trhoh[mode];
  247. data_invalid_rloh = (en_lo + en_hi) * CLK_X + Trloh[mode];
  248. data_invalid =
  249. data_invalid_rhoh <
  250. data_invalid_rloh ? data_invalid_rhoh : data_invalid_rloh;
  251. dv_window = data_invalid - Trea[mode];
  252. if (dv_window < 8)
  253. en_lo++;
  254. }
  255. acc_clks = CEIL_DIV(Trea[mode], CLK_X);
  256. while (((acc_clks * CLK_X) - Trea[mode]) < 3)
  257. acc_clks++;
  258. if ((data_invalid - acc_clks * CLK_X) < 2)
  259. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d: Warning!\n",
  260. __FILE__, __LINE__);
  261. addr_2_data = CEIL_DIV(Tadl[mode], CLK_X);
  262. re_2_we = CEIL_DIV(Trhw[mode], CLK_X);
  263. re_2_re = CEIL_DIV(Trhz[mode], CLK_X);
  264. we_2_re = CEIL_DIV(Twhr[mode], CLK_X);
  265. cs_cnt = CEIL_DIV((Tcs[mode] - Trp[mode]), CLK_X);
  266. if (!TclsRising)
  267. cs_cnt = CEIL_DIV(Tcs[mode], CLK_X);
  268. if (cs_cnt == 0)
  269. cs_cnt = 1;
  270. if (Tcea[mode]) {
  271. while (((cs_cnt * CLK_X) + Trea[mode]) < Tcea[mode])
  272. cs_cnt++;
  273. }
  274. #if MODE5_WORKAROUND
  275. if (mode == 5)
  276. acc_clks = 5;
  277. #endif
  278. /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */
  279. if ((ioread32(denali->flash_reg + MANUFACTURER_ID) == 0) &&
  280. (ioread32(denali->flash_reg + DEVICE_ID) == 0x88))
  281. acc_clks = 6;
  282. denali_write32(acc_clks, denali->flash_reg + ACC_CLKS);
  283. denali_write32(re_2_we, denali->flash_reg + RE_2_WE);
  284. denali_write32(re_2_re, denali->flash_reg + RE_2_RE);
  285. denali_write32(we_2_re, denali->flash_reg + WE_2_RE);
  286. denali_write32(addr_2_data, denali->flash_reg + ADDR_2_DATA);
  287. denali_write32(en_lo, denali->flash_reg + RDWR_EN_LO_CNT);
  288. denali_write32(en_hi, denali->flash_reg + RDWR_EN_HI_CNT);
  289. denali_write32(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
  290. }
  291. /* configures the initial ECC settings for the controller */
  292. static void set_ecc_config(struct denali_nand_info *denali)
  293. {
  294. #if SUPPORT_8BITECC
  295. if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) < 4096) ||
  296. (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) <= 128))
  297. denali_write32(8, denali->flash_reg + ECC_CORRECTION);
  298. #endif
  299. if ((ioread32(denali->flash_reg + ECC_CORRECTION) &
  300. ECC_CORRECTION__VALUE) == 1) {
  301. denali->dev_info.wECCBytesPerSector = 4;
  302. denali->dev_info.wECCBytesPerSector *=
  303. denali->dev_info.wDevicesConnected;
  304. denali->dev_info.wNumPageSpareFlag =
  305. denali->dev_info.wPageSpareSize -
  306. denali->dev_info.wPageDataSize /
  307. (ECC_SECTOR_SIZE * denali->dev_info.wDevicesConnected) *
  308. denali->dev_info.wECCBytesPerSector
  309. - denali->dev_info.wSpareSkipBytes;
  310. } else {
  311. denali->dev_info.wECCBytesPerSector =
  312. (ioread32(denali->flash_reg + ECC_CORRECTION) &
  313. ECC_CORRECTION__VALUE) * 13 / 8;
  314. if ((denali->dev_info.wECCBytesPerSector) % 2 == 0)
  315. denali->dev_info.wECCBytesPerSector += 2;
  316. else
  317. denali->dev_info.wECCBytesPerSector += 1;
  318. denali->dev_info.wECCBytesPerSector *=
  319. denali->dev_info.wDevicesConnected;
  320. denali->dev_info.wNumPageSpareFlag =
  321. denali->dev_info.wPageSpareSize -
  322. denali->dev_info.wPageDataSize /
  323. (ECC_SECTOR_SIZE * denali->dev_info.wDevicesConnected) *
  324. denali->dev_info.wECCBytesPerSector
  325. - denali->dev_info.wSpareSkipBytes;
  326. }
  327. }
  328. /* queries the NAND device to see what ONFI modes it supports. */
  329. static uint16_t get_onfi_nand_para(struct denali_nand_info *denali)
  330. {
  331. int i;
  332. uint16_t blks_lun_l, blks_lun_h, n_of_luns;
  333. uint32_t blockperlun, id;
  334. denali_write32(DEVICE_RESET__BANK0, denali->flash_reg + DEVICE_RESET);
  335. while (!((ioread32(denali->flash_reg + INTR_STATUS0) &
  336. INTR_STATUS0__RST_COMP) |
  337. (ioread32(denali->flash_reg + INTR_STATUS0) &
  338. INTR_STATUS0__TIME_OUT)))
  339. ;
  340. if (ioread32(denali->flash_reg + INTR_STATUS0) &
  341. INTR_STATUS0__RST_COMP) {
  342. denali_write32(DEVICE_RESET__BANK1,
  343. denali->flash_reg + DEVICE_RESET);
  344. while (!((ioread32(denali->flash_reg + INTR_STATUS1) &
  345. INTR_STATUS1__RST_COMP) |
  346. (ioread32(denali->flash_reg + INTR_STATUS1) &
  347. INTR_STATUS1__TIME_OUT)))
  348. ;
  349. if (ioread32(denali->flash_reg + INTR_STATUS1) &
  350. INTR_STATUS1__RST_COMP) {
  351. denali_write32(DEVICE_RESET__BANK2,
  352. denali->flash_reg + DEVICE_RESET);
  353. while (!((ioread32(denali->flash_reg + INTR_STATUS2) &
  354. INTR_STATUS2__RST_COMP) |
  355. (ioread32(denali->flash_reg + INTR_STATUS2) &
  356. INTR_STATUS2__TIME_OUT)))
  357. ;
  358. if (ioread32(denali->flash_reg + INTR_STATUS2) &
  359. INTR_STATUS2__RST_COMP) {
  360. denali_write32(DEVICE_RESET__BANK3,
  361. denali->flash_reg + DEVICE_RESET);
  362. while (!((ioread32(denali->flash_reg +
  363. INTR_STATUS3) &
  364. INTR_STATUS3__RST_COMP) |
  365. (ioread32(denali->flash_reg +
  366. INTR_STATUS3) &
  367. INTR_STATUS3__TIME_OUT)))
  368. ;
  369. } else {
  370. printk(KERN_ERR "Getting a time out for bank 2!\n");
  371. }
  372. } else {
  373. printk(KERN_ERR "Getting a time out for bank 1!\n");
  374. }
  375. }
  376. denali_write32(INTR_STATUS0__TIME_OUT,
  377. denali->flash_reg + INTR_STATUS0);
  378. denali_write32(INTR_STATUS1__TIME_OUT,
  379. denali->flash_reg + INTR_STATUS1);
  380. denali_write32(INTR_STATUS2__TIME_OUT,
  381. denali->flash_reg + INTR_STATUS2);
  382. denali_write32(INTR_STATUS3__TIME_OUT,
  383. denali->flash_reg + INTR_STATUS3);
  384. denali->dev_info.wONFIDevFeatures =
  385. ioread32(denali->flash_reg + ONFI_DEVICE_FEATURES);
  386. denali->dev_info.wONFIOptCommands =
  387. ioread32(denali->flash_reg + ONFI_OPTIONAL_COMMANDS);
  388. denali->dev_info.wONFITimingMode =
  389. ioread32(denali->flash_reg + ONFI_TIMING_MODE);
  390. denali->dev_info.wONFIPgmCacheTimingMode =
  391. ioread32(denali->flash_reg + ONFI_PGM_CACHE_TIMING_MODE);
  392. n_of_luns = ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
  393. ONFI_DEVICE_NO_OF_LUNS__NO_OF_LUNS;
  394. blks_lun_l = ioread32(denali->flash_reg +
  395. ONFI_DEVICE_NO_OF_BLOCKS_PER_LUN_L);
  396. blks_lun_h = ioread32(denali->flash_reg +
  397. ONFI_DEVICE_NO_OF_BLOCKS_PER_LUN_U);
  398. blockperlun = (blks_lun_h << 16) | blks_lun_l;
  399. denali->dev_info.wTotalBlocks = n_of_luns * blockperlun;
  400. if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
  401. ONFI_TIMING_MODE__VALUE))
  402. return FAIL;
  403. for (i = 5; i > 0; i--) {
  404. if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
  405. (0x01 << i))
  406. break;
  407. }
  408. nand_onfi_timing_set(denali, i);
  409. index_addr(denali, MODE_11 | 0, 0x90);
  410. index_addr(denali, MODE_11 | 1, 0);
  411. for (i = 0; i < 3; i++)
  412. index_addr_read_data(denali, MODE_11 | 2, &id);
  413. nand_dbg_print(NAND_DBG_DEBUG, "3rd ID: 0x%x\n", id);
  414. denali->dev_info.MLCDevice = id & 0x0C;
  415. /* By now, all the ONFI devices we know support the page cache */
  416. /* rw feature. So here we enable the pipeline_rw_ahead feature */
  417. /* iowrite32(1, denali->flash_reg + CACHE_WRITE_ENABLE); */
  418. /* iowrite32(1, denali->flash_reg + CACHE_READ_ENABLE); */
  419. return PASS;
  420. }
  421. static void get_samsung_nand_para(struct denali_nand_info *denali)
  422. {
  423. uint8_t no_of_planes;
  424. uint32_t blk_size;
  425. uint64_t plane_size, capacity;
  426. uint32_t id_bytes[5];
  427. int i;
  428. index_addr(denali, (uint32_t)(MODE_11 | 0), 0x90);
  429. index_addr(denali, (uint32_t)(MODE_11 | 1), 0);
  430. for (i = 0; i < 5; i++)
  431. index_addr_read_data(denali, (uint32_t)(MODE_11 | 2),
  432. &id_bytes[i]);
  433. nand_dbg_print(NAND_DBG_DEBUG,
  434. "ID bytes: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
  435. id_bytes[0], id_bytes[1], id_bytes[2],
  436. id_bytes[3], id_bytes[4]);
  437. if ((id_bytes[1] & 0xff) == 0xd3) { /* Samsung K9WAG08U1A */
  438. /* Set timing register values according to datasheet */
  439. denali_write32(5, denali->flash_reg + ACC_CLKS);
  440. denali_write32(20, denali->flash_reg + RE_2_WE);
  441. denali_write32(12, denali->flash_reg + WE_2_RE);
  442. denali_write32(14, denali->flash_reg + ADDR_2_DATA);
  443. denali_write32(3, denali->flash_reg + RDWR_EN_LO_CNT);
  444. denali_write32(2, denali->flash_reg + RDWR_EN_HI_CNT);
  445. denali_write32(2, denali->flash_reg + CS_SETUP_CNT);
  446. }
  447. no_of_planes = 1 << ((id_bytes[4] & 0x0c) >> 2);
  448. plane_size = (uint64_t)64 << ((id_bytes[4] & 0x70) >> 4);
  449. blk_size = 64 << ((ioread32(denali->flash_reg + DEVICE_PARAM_1) &
  450. 0x30) >> 4);
  451. capacity = (uint64_t)128 * plane_size * no_of_planes;
  452. do_div(capacity, blk_size);
  453. denali->dev_info.wTotalBlocks = capacity;
  454. }
  455. static void get_toshiba_nand_para(struct denali_nand_info *denali)
  456. {
  457. uint32_t tmp;
  458. /* Workaround to fix a controller bug which reports a wrong */
  459. /* spare area size for some kind of Toshiba NAND device */
  460. if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) &&
  461. (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) {
  462. denali_write32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  463. tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) *
  464. ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  465. denali_write32(tmp,
  466. denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
  467. #if SUPPORT_15BITECC
  468. denali_write32(15, denali->flash_reg + ECC_CORRECTION);
  469. #elif SUPPORT_8BITECC
  470. denali_write32(8, denali->flash_reg + ECC_CORRECTION);
  471. #endif
  472. }
  473. }
  474. static void get_hynix_nand_para(struct denali_nand_info *denali,
  475. uint8_t device_id)
  476. {
  477. uint32_t main_size, spare_size;
  478. switch (device_id) {
  479. case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
  480. case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
  481. denali_write32(128, denali->flash_reg + PAGES_PER_BLOCK);
  482. denali_write32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
  483. denali_write32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  484. main_size = 4096 *
  485. ioread32(denali->flash_reg + DEVICES_CONNECTED);
  486. spare_size = 224 *
  487. ioread32(denali->flash_reg + DEVICES_CONNECTED);
  488. denali_write32(main_size,
  489. denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
  490. denali_write32(spare_size,
  491. denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
  492. denali_write32(0, denali->flash_reg + DEVICE_WIDTH);
  493. #if SUPPORT_15BITECC
  494. denali_write32(15, denali->flash_reg + ECC_CORRECTION);
  495. #elif SUPPORT_8BITECC
  496. denali_write32(8, denali->flash_reg + ECC_CORRECTION);
  497. #endif
  498. denali->dev_info.MLCDevice = 1;
  499. break;
  500. default:
  501. nand_dbg_print(NAND_DBG_WARN,
  502. "Spectra: Unknown Hynix NAND (Device ID: 0x%x)."
  503. "Will use default parameter values instead.\n",
  504. denali->dev_info.wDeviceID);
  505. }
  506. }
  507. /* determines how many NAND chips are connected to the controller. Note for
  508. Intel CE4100 devices we don't support more than one device.
  509. */
  510. static void find_valid_banks(struct denali_nand_info *denali)
  511. {
  512. uint32_t id[LLD_MAX_FLASH_BANKS];
  513. int i;
  514. denali->total_used_banks = 1;
  515. for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) {
  516. index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90);
  517. index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0);
  518. index_addr_read_data(denali,
  519. (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]);
  520. nand_dbg_print(NAND_DBG_DEBUG,
  521. "Return 1st ID for bank[%d]: %x\n", i, id[i]);
  522. if (i == 0) {
  523. if (!(id[i] & 0x0ff))
  524. break; /* WTF? */
  525. } else {
  526. if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
  527. denali->total_used_banks++;
  528. else
  529. break;
  530. }
  531. }
  532. if (denali->platform == INTEL_CE4100) {
  533. /* Platform limitations of the CE4100 device limit
  534. * users to a single chip solution for NAND.
  535. * Multichip support is not enabled.
  536. */
  537. if (denali->total_used_banks != 1) {
  538. printk(KERN_ERR "Sorry, Intel CE4100 only supports "
  539. "a single NAND device.\n");
  540. BUG();
  541. }
  542. }
  543. nand_dbg_print(NAND_DBG_DEBUG,
  544. "denali->total_used_banks: %d\n", denali->total_used_banks);
  545. }
  546. static void detect_partition_feature(struct denali_nand_info *denali)
  547. {
  548. if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
  549. if ((ioread32(denali->flash_reg + PERM_SRC_ID_1) &
  550. PERM_SRC_ID_1__SRCID) == SPECTRA_PARTITION_ID) {
  551. denali->dev_info.wSpectraStartBlock =
  552. ((ioread32(denali->flash_reg + MIN_MAX_BANK_1) &
  553. MIN_MAX_BANK_1__MIN_VALUE) *
  554. denali->dev_info.wTotalBlocks)
  555. +
  556. (ioread32(denali->flash_reg + MIN_BLK_ADDR_1) &
  557. MIN_BLK_ADDR_1__VALUE);
  558. denali->dev_info.wSpectraEndBlock =
  559. (((ioread32(denali->flash_reg + MIN_MAX_BANK_1) &
  560. MIN_MAX_BANK_1__MAX_VALUE) >> 2) *
  561. denali->dev_info.wTotalBlocks)
  562. +
  563. (ioread32(denali->flash_reg + MAX_BLK_ADDR_1) &
  564. MAX_BLK_ADDR_1__VALUE);
  565. denali->dev_info.wTotalBlocks *=
  566. denali->total_used_banks;
  567. if (denali->dev_info.wSpectraEndBlock >=
  568. denali->dev_info.wTotalBlocks) {
  569. denali->dev_info.wSpectraEndBlock =
  570. denali->dev_info.wTotalBlocks - 1;
  571. }
  572. denali->dev_info.wDataBlockNum =
  573. denali->dev_info.wSpectraEndBlock -
  574. denali->dev_info.wSpectraStartBlock + 1;
  575. } else {
  576. denali->dev_info.wTotalBlocks *=
  577. denali->total_used_banks;
  578. denali->dev_info.wSpectraStartBlock =
  579. SPECTRA_START_BLOCK;
  580. denali->dev_info.wSpectraEndBlock =
  581. denali->dev_info.wTotalBlocks - 1;
  582. denali->dev_info.wDataBlockNum =
  583. denali->dev_info.wSpectraEndBlock -
  584. denali->dev_info.wSpectraStartBlock + 1;
  585. }
  586. } else {
  587. denali->dev_info.wTotalBlocks *= denali->total_used_banks;
  588. denali->dev_info.wSpectraStartBlock = SPECTRA_START_BLOCK;
  589. denali->dev_info.wSpectraEndBlock =
  590. denali->dev_info.wTotalBlocks - 1;
  591. denali->dev_info.wDataBlockNum =
  592. denali->dev_info.wSpectraEndBlock -
  593. denali->dev_info.wSpectraStartBlock + 1;
  594. }
  595. }
  596. static void dump_device_info(struct denali_nand_info *denali)
  597. {
  598. nand_dbg_print(NAND_DBG_DEBUG, "denali->dev_info:\n");
  599. nand_dbg_print(NAND_DBG_DEBUG, "DeviceMaker: 0x%x\n",
  600. denali->dev_info.wDeviceMaker);
  601. nand_dbg_print(NAND_DBG_DEBUG, "DeviceID: 0x%x\n",
  602. denali->dev_info.wDeviceID);
  603. nand_dbg_print(NAND_DBG_DEBUG, "DeviceType: 0x%x\n",
  604. denali->dev_info.wDeviceType);
  605. nand_dbg_print(NAND_DBG_DEBUG, "SpectraStartBlock: %d\n",
  606. denali->dev_info.wSpectraStartBlock);
  607. nand_dbg_print(NAND_DBG_DEBUG, "SpectraEndBlock: %d\n",
  608. denali->dev_info.wSpectraEndBlock);
  609. nand_dbg_print(NAND_DBG_DEBUG, "TotalBlocks: %d\n",
  610. denali->dev_info.wTotalBlocks);
  611. nand_dbg_print(NAND_DBG_DEBUG, "PagesPerBlock: %d\n",
  612. denali->dev_info.wPagesPerBlock);
  613. nand_dbg_print(NAND_DBG_DEBUG, "PageSize: %d\n",
  614. denali->dev_info.wPageSize);
  615. nand_dbg_print(NAND_DBG_DEBUG, "PageDataSize: %d\n",
  616. denali->dev_info.wPageDataSize);
  617. nand_dbg_print(NAND_DBG_DEBUG, "PageSpareSize: %d\n",
  618. denali->dev_info.wPageSpareSize);
  619. nand_dbg_print(NAND_DBG_DEBUG, "NumPageSpareFlag: %d\n",
  620. denali->dev_info.wNumPageSpareFlag);
  621. nand_dbg_print(NAND_DBG_DEBUG, "ECCBytesPerSector: %d\n",
  622. denali->dev_info.wECCBytesPerSector);
  623. nand_dbg_print(NAND_DBG_DEBUG, "BlockSize: %d\n",
  624. denali->dev_info.wBlockSize);
  625. nand_dbg_print(NAND_DBG_DEBUG, "BlockDataSize: %d\n",
  626. denali->dev_info.wBlockDataSize);
  627. nand_dbg_print(NAND_DBG_DEBUG, "DataBlockNum: %d\n",
  628. denali->dev_info.wDataBlockNum);
  629. nand_dbg_print(NAND_DBG_DEBUG, "PlaneNum: %d\n",
  630. denali->dev_info.bPlaneNum);
  631. nand_dbg_print(NAND_DBG_DEBUG, "DeviceMainAreaSize: %d\n",
  632. denali->dev_info.wDeviceMainAreaSize);
  633. nand_dbg_print(NAND_DBG_DEBUG, "DeviceSpareAreaSize: %d\n",
  634. denali->dev_info.wDeviceSpareAreaSize);
  635. nand_dbg_print(NAND_DBG_DEBUG, "DevicesConnected: %d\n",
  636. denali->dev_info.wDevicesConnected);
  637. nand_dbg_print(NAND_DBG_DEBUG, "DeviceWidth: %d\n",
  638. denali->dev_info.wDeviceWidth);
  639. nand_dbg_print(NAND_DBG_DEBUG, "HWRevision: 0x%x\n",
  640. denali->dev_info.wHWRevision);
  641. nand_dbg_print(NAND_DBG_DEBUG, "HWFeatures: 0x%x\n",
  642. denali->dev_info.wHWFeatures);
  643. nand_dbg_print(NAND_DBG_DEBUG, "ONFIDevFeatures: 0x%x\n",
  644. denali->dev_info.wONFIDevFeatures);
  645. nand_dbg_print(NAND_DBG_DEBUG, "ONFIOptCommands: 0x%x\n",
  646. denali->dev_info.wONFIOptCommands);
  647. nand_dbg_print(NAND_DBG_DEBUG, "ONFITimingMode: 0x%x\n",
  648. denali->dev_info.wONFITimingMode);
  649. nand_dbg_print(NAND_DBG_DEBUG, "ONFIPgmCacheTimingMode: 0x%x\n",
  650. denali->dev_info.wONFIPgmCacheTimingMode);
  651. nand_dbg_print(NAND_DBG_DEBUG, "MLCDevice: %s\n",
  652. denali->dev_info.MLCDevice ? "Yes" : "No");
  653. nand_dbg_print(NAND_DBG_DEBUG, "SpareSkipBytes: %d\n",
  654. denali->dev_info.wSpareSkipBytes);
  655. nand_dbg_print(NAND_DBG_DEBUG, "BitsInPageNumber: %d\n",
  656. denali->dev_info.nBitsInPageNumber);
  657. nand_dbg_print(NAND_DBG_DEBUG, "BitsInPageDataSize: %d\n",
  658. denali->dev_info.nBitsInPageDataSize);
  659. nand_dbg_print(NAND_DBG_DEBUG, "BitsInBlockDataSize: %d\n",
  660. denali->dev_info.nBitsInBlockDataSize);
  661. }
  662. static uint16_t denali_nand_timing_set(struct denali_nand_info *denali)
  663. {
  664. uint16_t status = PASS;
  665. uint8_t no_of_planes;
  666. uint32_t id_bytes[5], addr;
  667. uint8_t i, maf_id, device_id;
  668. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  669. __FILE__, __LINE__, __func__);
  670. /* Use read id method to get device ID and other
  671. * params. For some NAND chips, controller can't
  672. * report the correct device ID by reading from
  673. * DEVICE_ID register
  674. * */
  675. addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
  676. index_addr(denali, (uint32_t)addr | 0, 0x90);
  677. index_addr(denali, (uint32_t)addr | 1, 0);
  678. for (i = 0; i < 5; i++)
  679. index_addr_read_data(denali, addr | 2, &id_bytes[i]);
  680. maf_id = id_bytes[0];
  681. device_id = id_bytes[1];
  682. if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
  683. ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
  684. if (FAIL == get_onfi_nand_para(denali))
  685. return FAIL;
  686. } else if (maf_id == 0xEC) { /* Samsung NAND */
  687. get_samsung_nand_para(denali);
  688. } else if (maf_id == 0x98) { /* Toshiba NAND */
  689. get_toshiba_nand_para(denali);
  690. } else if (maf_id == 0xAD) { /* Hynix NAND */
  691. get_hynix_nand_para(denali, device_id);
  692. } else {
  693. denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS;
  694. }
  695. nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:"
  696. "acc_clks: %d, re_2_we: %d, we_2_re: %d,"
  697. "addr_2_data: %d, rdwr_en_lo_cnt: %d, "
  698. "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n",
  699. ioread32(denali->flash_reg + ACC_CLKS),
  700. ioread32(denali->flash_reg + RE_2_WE),
  701. ioread32(denali->flash_reg + WE_2_RE),
  702. ioread32(denali->flash_reg + ADDR_2_DATA),
  703. ioread32(denali->flash_reg + RDWR_EN_LO_CNT),
  704. ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
  705. ioread32(denali->flash_reg + CS_SETUP_CNT));
  706. denali->dev_info.wHWRevision = ioread32(denali->flash_reg + REVISION);
  707. denali->dev_info.wHWFeatures = ioread32(denali->flash_reg + FEATURES);
  708. denali->dev_info.wDeviceMainAreaSize =
  709. ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
  710. denali->dev_info.wDeviceSpareAreaSize =
  711. ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
  712. denali->dev_info.wPageDataSize =
  713. ioread32(denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
  714. /* Note: When using the Micon 4K NAND device, the controller will report
  715. * Page Spare Size as 216 bytes. But Micron's Spec say it's 218 bytes.
  716. * And if force set it to 218 bytes, the controller can not work
  717. * correctly. So just let it be. But keep in mind that this bug may
  718. * cause
  719. * other problems in future. - Yunpeng 2008-10-10
  720. */
  721. denali->dev_info.wPageSpareSize =
  722. ioread32(denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
  723. denali->dev_info.wPagesPerBlock =
  724. ioread32(denali->flash_reg + PAGES_PER_BLOCK);
  725. denali->dev_info.wPageSize =
  726. denali->dev_info.wPageDataSize + denali->dev_info.wPageSpareSize;
  727. denali->dev_info.wBlockSize =
  728. denali->dev_info.wPageSize * denali->dev_info.wPagesPerBlock;
  729. denali->dev_info.wBlockDataSize =
  730. denali->dev_info.wPagesPerBlock * denali->dev_info.wPageDataSize;
  731. denali->dev_info.wDeviceWidth =
  732. ioread32(denali->flash_reg + DEVICE_WIDTH);
  733. denali->dev_info.wDeviceType =
  734. ((ioread32(denali->flash_reg + DEVICE_WIDTH) > 0) ? 16 : 8);
  735. denali->dev_info.wDevicesConnected =
  736. ioread32(denali->flash_reg + DEVICES_CONNECTED);
  737. denali->dev_info.wSpareSkipBytes =
  738. ioread32(denali->flash_reg + SPARE_AREA_SKIP_BYTES) *
  739. denali->dev_info.wDevicesConnected;
  740. denali->dev_info.nBitsInPageNumber =
  741. ilog2(denali->dev_info.wPagesPerBlock);
  742. denali->dev_info.nBitsInPageDataSize =
  743. ilog2(denali->dev_info.wPageDataSize);
  744. denali->dev_info.nBitsInBlockDataSize =
  745. ilog2(denali->dev_info.wBlockDataSize);
  746. set_ecc_config(denali);
  747. no_of_planes = ioread32(denali->flash_reg + NUMBER_OF_PLANES) &
  748. NUMBER_OF_PLANES__VALUE;
  749. switch (no_of_planes) {
  750. case 0:
  751. case 1:
  752. case 3:
  753. case 7:
  754. denali->dev_info.bPlaneNum = no_of_planes + 1;
  755. break;
  756. default:
  757. status = FAIL;
  758. break;
  759. }
  760. find_valid_banks(denali);
  761. detect_partition_feature(denali);
  762. dump_device_info(denali);
  763. /* If the user specified to override the default timings
  764. * with a specific ONFI mode, we apply those changes here.
  765. */
  766. if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
  767. nand_onfi_timing_set(denali, onfi_timing_mode);
  768. return status;
  769. }
  770. static void denali_set_intr_modes(struct denali_nand_info *denali,
  771. uint16_t INT_ENABLE)
  772. {
  773. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  774. __FILE__, __LINE__, __func__);
  775. if (INT_ENABLE)
  776. denali_write32(1, denali->flash_reg + GLOBAL_INT_ENABLE);
  777. else
  778. denali_write32(0, denali->flash_reg + GLOBAL_INT_ENABLE);
  779. }
  780. /* validation function to verify that the controlling software is making
  781. a valid request
  782. */
  783. static inline bool is_flash_bank_valid(int flash_bank)
  784. {
  785. return (flash_bank >= 0 && flash_bank < 4);
  786. }
  787. static void denali_irq_init(struct denali_nand_info *denali)
  788. {
  789. uint32_t int_mask = 0;
  790. /* Disable global interrupts */
  791. denali_set_intr_modes(denali, false);
  792. int_mask = DENALI_IRQ_ALL;
  793. /* Clear all status bits */
  794. denali_write32(0xFFFF, denali->flash_reg + INTR_STATUS0);
  795. denali_write32(0xFFFF, denali->flash_reg + INTR_STATUS1);
  796. denali_write32(0xFFFF, denali->flash_reg + INTR_STATUS2);
  797. denali_write32(0xFFFF, denali->flash_reg + INTR_STATUS3);
  798. denali_irq_enable(denali, int_mask);
  799. }
  800. static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali)
  801. {
  802. denali_set_intr_modes(denali, false);
  803. free_irq(irqnum, denali);
  804. }
  805. static void denali_irq_enable(struct denali_nand_info *denali,
  806. uint32_t int_mask)
  807. {
  808. denali_write32(int_mask, denali->flash_reg + INTR_EN0);
  809. denali_write32(int_mask, denali->flash_reg + INTR_EN1);
  810. denali_write32(int_mask, denali->flash_reg + INTR_EN2);
  811. denali_write32(int_mask, denali->flash_reg + INTR_EN3);
  812. }
  813. /* This function only returns when an interrupt that this driver cares about
  814. * occurs. This is to reduce the overhead of servicing interrupts
  815. */
  816. static inline uint32_t denali_irq_detected(struct denali_nand_info *denali)
  817. {
  818. return read_interrupt_status(denali) & DENALI_IRQ_ALL;
  819. }
  820. /* Interrupts are cleared by writing a 1 to the appropriate status bit */
  821. static inline void clear_interrupt(struct denali_nand_info *denali,
  822. uint32_t irq_mask)
  823. {
  824. uint32_t intr_status_reg = 0;
  825. intr_status_reg = intr_status_addresses[denali->flash_bank];
  826. denali_write32(irq_mask, denali->flash_reg + intr_status_reg);
  827. }
  828. static void clear_interrupts(struct denali_nand_info *denali)
  829. {
  830. uint32_t status = 0x0;
  831. spin_lock_irq(&denali->irq_lock);
  832. status = read_interrupt_status(denali);
  833. #if DEBUG_DENALI
  834. denali->irq_debug_array[denali->idx++] = 0x30000000 | status;
  835. denali->idx %= 32;
  836. #endif
  837. denali->irq_status = 0x0;
  838. spin_unlock_irq(&denali->irq_lock);
  839. }
  840. static uint32_t read_interrupt_status(struct denali_nand_info *denali)
  841. {
  842. uint32_t intr_status_reg = 0;
  843. intr_status_reg = intr_status_addresses[denali->flash_bank];
  844. return ioread32(denali->flash_reg + intr_status_reg);
  845. }
  846. #if DEBUG_DENALI
  847. static void print_irq_log(struct denali_nand_info *denali)
  848. {
  849. int i = 0;
  850. printk(KERN_INFO "ISR debug log index = %X\n", denali->idx);
  851. for (i = 0; i < 32; i++)
  852. printk(KERN_INFO "%08X: %08X\n", i, denali->irq_debug_array[i]);
  853. }
  854. #endif
  855. /* This is the interrupt service routine. It handles all interrupts
  856. * sent to this device. Note that on CE4100, this is a shared
  857. * interrupt.
  858. */
  859. static irqreturn_t denali_isr(int irq, void *dev_id)
  860. {
  861. struct denali_nand_info *denali = dev_id;
  862. uint32_t irq_status = 0x0;
  863. irqreturn_t result = IRQ_NONE;
  864. spin_lock(&denali->irq_lock);
  865. /* check to see if a valid NAND chip has
  866. * been selected.
  867. */
  868. if (is_flash_bank_valid(denali->flash_bank)) {
  869. /* check to see if controller generated
  870. * the interrupt, since this is a shared interrupt */
  871. irq_status = denali_irq_detected(denali);
  872. if (irq_status != 0) {
  873. #if DEBUG_DENALI
  874. denali->irq_debug_array[denali->idx++] =
  875. 0x10000000 | irq_status;
  876. denali->idx %= 32;
  877. printk(KERN_INFO "IRQ status = 0x%04x\n", irq_status);
  878. #endif
  879. /* handle interrupt */
  880. /* first acknowledge it */
  881. clear_interrupt(denali, irq_status);
  882. /* store the status in the device context for someone
  883. to read */
  884. denali->irq_status |= irq_status;
  885. /* notify anyone who cares that it happened */
  886. complete(&denali->complete);
  887. /* tell the OS that we've handled this */
  888. result = IRQ_HANDLED;
  889. }
  890. }
  891. spin_unlock(&denali->irq_lock);
  892. return result;
  893. }
  894. #define BANK(x) ((x) << 24)
  895. static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
  896. {
  897. unsigned long comp_res = 0;
  898. uint32_t intr_status = 0;
  899. bool retry = false;
  900. unsigned long timeout = msecs_to_jiffies(1000);
  901. do {
  902. #if DEBUG_DENALI
  903. printk(KERN_INFO "waiting for 0x%x\n", irq_mask);
  904. #endif
  905. comp_res =
  906. wait_for_completion_timeout(&denali->complete, timeout);
  907. spin_lock_irq(&denali->irq_lock);
  908. intr_status = denali->irq_status;
  909. #if DEBUG_DENALI
  910. denali->irq_debug_array[denali->idx++] =
  911. 0x20000000 | (irq_mask << 16) | intr_status;
  912. denali->idx %= 32;
  913. #endif
  914. if (intr_status & irq_mask) {
  915. denali->irq_status &= ~irq_mask;
  916. spin_unlock_irq(&denali->irq_lock);
  917. #if DEBUG_DENALI
  918. if (retry)
  919. printk(KERN_INFO "status on retry = 0x%x\n",
  920. intr_status);
  921. #endif
  922. /* our interrupt was detected */
  923. break;
  924. } else {
  925. /* these are not the interrupts you are looking for -
  926. * need to wait again */
  927. spin_unlock_irq(&denali->irq_lock);
  928. #if DEBUG_DENALI
  929. print_irq_log(denali);
  930. printk(KERN_INFO "received irq nobody cared:"
  931. " irq_status = 0x%x, irq_mask = 0x%x,"
  932. " timeout = %ld\n", intr_status,
  933. irq_mask, comp_res);
  934. #endif
  935. retry = true;
  936. }
  937. } while (comp_res != 0);
  938. if (comp_res == 0) {
  939. /* timeout */
  940. printk(KERN_ERR "timeout occurred, status = 0x%x, mask = 0x%x\n",
  941. intr_status, irq_mask);
  942. intr_status = 0;
  943. }
  944. return intr_status;
  945. }
  946. /* This helper function setups the registers for ECC and whether or not
  947. the spare area will be transfered. */
  948. static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
  949. bool transfer_spare)
  950. {
  951. int ecc_en_flag = 0, transfer_spare_flag = 0;
  952. /* set ECC, transfer spare bits if needed */
  953. ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
  954. transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
  955. /* Enable spare area/ECC per user's request. */
  956. denali_write32(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
  957. denali_write32(transfer_spare_flag,
  958. denali->flash_reg + TRANSFER_SPARE_REG);
  959. }
  960. /* sends a pipeline command operation to the controller. See the Denali NAND
  961. controller's user guide for more information (section 4.2.3.6).
  962. */
  963. static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
  964. bool ecc_en,
  965. bool transfer_spare,
  966. int access_type,
  967. int op)
  968. {
  969. int status = PASS;
  970. uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0,
  971. irq_mask = 0;
  972. if (op == DENALI_READ)
  973. irq_mask = INTR_STATUS0__LOAD_COMP;
  974. else if (op == DENALI_WRITE)
  975. irq_mask = 0;
  976. else
  977. BUG();
  978. setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
  979. #if DEBUG_DENALI
  980. spin_lock_irq(&denali->irq_lock);
  981. denali->irq_debug_array[denali->idx++] =
  982. 0x40000000 | ioread32(denali->flash_reg + ECC_ENABLE) |
  983. (access_type << 4);
  984. denali->idx %= 32;
  985. spin_unlock_irq(&denali->irq_lock);
  986. #endif
  987. /* clear interrupts */
  988. clear_interrupts(denali);
  989. addr = BANK(denali->flash_bank) | denali->page;
  990. if (op == DENALI_WRITE && access_type != SPARE_ACCESS) {
  991. cmd = MODE_01 | addr;
  992. denali_write32(cmd, denali->flash_mem);
  993. } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) {
  994. /* read spare area */
  995. cmd = MODE_10 | addr;
  996. index_addr(denali, (uint32_t)cmd, access_type);
  997. cmd = MODE_01 | addr;
  998. denali_write32(cmd, denali->flash_mem);
  999. } else if (op == DENALI_READ) {
  1000. /* setup page read request for access type */
  1001. cmd = MODE_10 | addr;
  1002. index_addr(denali, (uint32_t)cmd, access_type);
  1003. /* page 33 of the NAND controller spec indicates we should not
  1004. use the pipeline commands in Spare area only mode. So we
  1005. don't.
  1006. */
  1007. if (access_type == SPARE_ACCESS) {
  1008. cmd = MODE_01 | addr;
  1009. denali_write32(cmd, denali->flash_mem);
  1010. } else {
  1011. index_addr(denali, (uint32_t)cmd,
  1012. 0x2000 | op | page_count);
  1013. /* wait for command to be accepted
  1014. * can always use status0 bit as the
  1015. * mask is identical for each
  1016. * bank. */
  1017. irq_status = wait_for_irq(denali, irq_mask);
  1018. if (irq_status == 0) {
  1019. printk(KERN_ERR "cmd, page, addr on timeout "
  1020. "(0x%x, 0x%x, 0x%x)\n", cmd,
  1021. denali->page, addr);
  1022. status = FAIL;
  1023. } else {
  1024. cmd = MODE_01 | addr;
  1025. denali_write32(cmd, denali->flash_mem);
  1026. }
  1027. }
  1028. }
  1029. return status;
  1030. }
  1031. /* helper function that simply writes a buffer to the flash */
  1032. static int write_data_to_flash_mem(struct denali_nand_info *denali,
  1033. const uint8_t *buf,
  1034. int len)
  1035. {
  1036. uint32_t i = 0, *buf32;
  1037. /* verify that the len is a multiple of 4. see comment in
  1038. * read_data_from_flash_mem() */
  1039. BUG_ON((len % 4) != 0);
  1040. /* write the data to the flash memory */
  1041. buf32 = (uint32_t *)buf;
  1042. for (i = 0; i < len / 4; i++)
  1043. denali_write32(*buf32++, denali->flash_mem + 0x10);
  1044. return i*4; /* intent is to return the number of bytes read */
  1045. }
  1046. /* helper function that simply reads a buffer from the flash */
  1047. static int read_data_from_flash_mem(struct denali_nand_info *denali,
  1048. uint8_t *buf,
  1049. int len)
  1050. {
  1051. uint32_t i = 0, *buf32;
  1052. /* we assume that len will be a multiple of 4, if not
  1053. * it would be nice to know about it ASAP rather than
  1054. * have random failures...
  1055. * This assumption is based on the fact that this
  1056. * function is designed to be used to read flash pages,
  1057. * which are typically multiples of 4...
  1058. */
  1059. BUG_ON((len % 4) != 0);
  1060. /* transfer the data from the flash */
  1061. buf32 = (uint32_t *)buf;
  1062. for (i = 0; i < len / 4; i++)
  1063. *buf32++ = ioread32(denali->flash_mem + 0x10);
  1064. return i*4; /* intent is to return the number of bytes read */
  1065. }
  1066. /* writes OOB data to the device */
  1067. static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
  1068. {
  1069. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1070. uint32_t irq_status = 0;
  1071. uint32_t irq_mask = INTR_STATUS0__PROGRAM_COMP |
  1072. INTR_STATUS0__PROGRAM_FAIL;
  1073. int status = 0;
  1074. denali->page = page;
  1075. if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS,
  1076. DENALI_WRITE) == PASS) {
  1077. write_data_to_flash_mem(denali, buf, mtd->oobsize);
  1078. #if DEBUG_DENALI
  1079. spin_lock_irq(&denali->irq_lock);
  1080. denali->irq_debug_array[denali->idx++] =
  1081. 0x80000000 | mtd->oobsize;
  1082. denali->idx %= 32;
  1083. spin_unlock_irq(&denali->irq_lock);
  1084. #endif
  1085. /* wait for operation to complete */
  1086. irq_status = wait_for_irq(denali, irq_mask);
  1087. if (irq_status == 0) {
  1088. printk(KERN_ERR "OOB write failed\n");
  1089. status = -EIO;
  1090. }
  1091. } else {
  1092. printk(KERN_ERR "unable to send pipeline command\n");
  1093. status = -EIO;
  1094. }
  1095. return status;
  1096. }
  1097. /* reads OOB data from the device */
  1098. static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
  1099. {
  1100. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1101. uint32_t irq_mask = INTR_STATUS0__LOAD_COMP,
  1102. irq_status = 0, addr = 0x0, cmd = 0x0;
  1103. denali->page = page;
  1104. #if DEBUG_DENALI
  1105. printk(KERN_INFO "read_oob %d\n", page);
  1106. #endif
  1107. if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
  1108. DENALI_READ) == PASS) {
  1109. read_data_from_flash_mem(denali, buf, mtd->oobsize);
  1110. /* wait for command to be accepted
  1111. * can always use status0 bit as the mask is identical for each
  1112. * bank. */
  1113. irq_status = wait_for_irq(denali, irq_mask);
  1114. if (irq_status == 0)
  1115. printk(KERN_ERR "page on OOB timeout %d\n",
  1116. denali->page);
  1117. /* We set the device back to MAIN_ACCESS here as I observed
  1118. * instability with the controller if you do a block erase
  1119. * and the last transaction was a SPARE_ACCESS. Block erase
  1120. * is reliable (according to the MTD test infrastructure)
  1121. * if you are in MAIN_ACCESS.
  1122. */
  1123. addr = BANK(denali->flash_bank) | denali->page;
  1124. cmd = MODE_10 | addr;
  1125. index_addr(denali, (uint32_t)cmd, MAIN_ACCESS);
  1126. #if DEBUG_DENALI
  1127. spin_lock_irq(&denali->irq_lock);
  1128. denali->irq_debug_array[denali->idx++] =
  1129. 0x60000000 | mtd->oobsize;
  1130. denali->idx %= 32;
  1131. spin_unlock_irq(&denali->irq_lock);
  1132. #endif
  1133. }
  1134. }
  1135. /* this function examines buffers to see if they contain data that
  1136. * indicate that the buffer is part of an erased region of flash.
  1137. */
  1138. bool is_erased(uint8_t *buf, int len)
  1139. {
  1140. int i = 0;
  1141. for (i = 0; i < len; i++)
  1142. if (buf[i] != 0xFF)
  1143. return false;
  1144. return true;
  1145. }
  1146. #define ECC_SECTOR_SIZE 512
  1147. #define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12)
  1148. #define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET))
  1149. #define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK)
  1150. #define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO))
  1151. #define ECC_ERR_DEVICE(x) ((x) & ERR_CORRECTION_INFO__DEVICE_NR >> 8)
  1152. #define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
  1153. static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
  1154. uint8_t *oobbuf, uint32_t irq_status)
  1155. {
  1156. bool check_erased_page = false;
  1157. if (irq_status & INTR_STATUS0__ECC_ERR) {
  1158. /* read the ECC errors. we'll ignore them for now */
  1159. uint32_t err_address = 0, err_correction_info = 0;
  1160. uint32_t err_byte = 0, err_sector = 0, err_device = 0;
  1161. uint32_t err_correction_value = 0;
  1162. do {
  1163. err_address = ioread32(denali->flash_reg +
  1164. ECC_ERROR_ADDRESS);
  1165. err_sector = ECC_SECTOR(err_address);
  1166. err_byte = ECC_BYTE(err_address);
  1167. err_correction_info = ioread32(denali->flash_reg +
  1168. ERR_CORRECTION_INFO);
  1169. err_correction_value =
  1170. ECC_CORRECTION_VALUE(err_correction_info);
  1171. err_device = ECC_ERR_DEVICE(err_correction_info);
  1172. if (ECC_ERROR_CORRECTABLE(err_correction_info)) {
  1173. /* offset in our buffer is computed as:
  1174. sector number * sector size + offset in
  1175. sector
  1176. */
  1177. int offset = err_sector * ECC_SECTOR_SIZE +
  1178. err_byte;
  1179. if (offset < denali->mtd.writesize) {
  1180. /* correct the ECC error */
  1181. buf[offset] ^= err_correction_value;
  1182. denali->mtd.ecc_stats.corrected++;
  1183. } else {
  1184. /* bummer, couldn't correct the error */
  1185. printk(KERN_ERR "ECC offset invalid\n");
  1186. denali->mtd.ecc_stats.failed++;
  1187. }
  1188. } else {
  1189. /* if the error is not correctable, need to
  1190. * look at the page to see if it is an erased
  1191. * page. if so, then it's not a real ECC error
  1192. * */
  1193. check_erased_page = true;
  1194. }
  1195. #if DEBUG_DENALI
  1196. printk(KERN_INFO "Detected ECC error in page %d:"
  1197. " err_addr = 0x%08x, info to fix is"
  1198. " 0x%08x\n", denali->page, err_address,
  1199. err_correction_info);
  1200. #endif
  1201. } while (!ECC_LAST_ERR(err_correction_info));
  1202. }
  1203. return check_erased_page;
  1204. }
  1205. /* programs the controller to either enable/disable DMA transfers */
  1206. static void denali_enable_dma(struct denali_nand_info *denali, bool en)
  1207. {
  1208. uint32_t reg_val = 0x0;
  1209. if (en)
  1210. reg_val = DMA_ENABLE__FLAG;
  1211. denali_write32(reg_val, denali->flash_reg + DMA_ENABLE);
  1212. ioread32(denali->flash_reg + DMA_ENABLE);
  1213. }
  1214. /* setups the HW to perform the data DMA */
  1215. static void denali_setup_dma(struct denali_nand_info *denali, int op)
  1216. {
  1217. uint32_t mode = 0x0;
  1218. const int page_count = 1;
  1219. dma_addr_t addr = denali->buf.dma_buf;
  1220. mode = MODE_10 | BANK(denali->flash_bank);
  1221. /* DMA is a four step process */
  1222. /* 1. setup transfer type and # of pages */
  1223. index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
  1224. /* 2. set memory high address bits 23:8 */
  1225. index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
  1226. /* 3. set memory low address bits 23:8 */
  1227. index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
  1228. /* 4. interrupt when complete, burst len = 64 bytes*/
  1229. index_addr(denali, mode | 0x14000, 0x2400);
  1230. }
  1231. /* writes a page. user specifies type, and this function handles the
  1232. configuration details. */
  1233. static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1234. const uint8_t *buf, bool raw_xfer)
  1235. {
  1236. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1237. struct pci_dev *pci_dev = denali->dev;
  1238. dma_addr_t addr = denali->buf.dma_buf;
  1239. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  1240. uint32_t irq_status = 0;
  1241. uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP |
  1242. INTR_STATUS0__PROGRAM_FAIL;
  1243. /* if it is a raw xfer, we want to disable ecc, and send
  1244. * the spare area.
  1245. * !raw_xfer - enable ecc
  1246. * raw_xfer - transfer spare
  1247. */
  1248. setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer);
  1249. /* copy buffer into DMA buffer */
  1250. memcpy(denali->buf.buf, buf, mtd->writesize);
  1251. if (raw_xfer) {
  1252. /* transfer the data to the spare area */
  1253. memcpy(denali->buf.buf + mtd->writesize,
  1254. chip->oob_poi,
  1255. mtd->oobsize);
  1256. }
  1257. pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_TODEVICE);
  1258. clear_interrupts(denali);
  1259. denali_enable_dma(denali, true);
  1260. denali_setup_dma(denali, DENALI_WRITE);
  1261. /* wait for operation to complete */
  1262. irq_status = wait_for_irq(denali, irq_mask);
  1263. if (irq_status == 0) {
  1264. printk(KERN_ERR "timeout on write_page"
  1265. " (type = %d)\n", raw_xfer);
  1266. denali->status =
  1267. (irq_status & INTR_STATUS0__PROGRAM_FAIL) ?
  1268. NAND_STATUS_FAIL : PASS;
  1269. }
  1270. denali_enable_dma(denali, false);
  1271. pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_TODEVICE);
  1272. }
  1273. /* NAND core entry points */
  1274. /* this is the callback that the NAND core calls to write a page. Since
  1275. writing a page with ECC or without is similar, all the work is done
  1276. by write_page above. */
  1277. static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1278. const uint8_t *buf)
  1279. {
  1280. /* for regular page writes, we let HW handle all the ECC
  1281. * data written to the device. */
  1282. write_page(mtd, chip, buf, false);
  1283. }
  1284. /* This is the callback that the NAND core calls to write a page without ECC.
  1285. raw access is similiar to ECC page writes, so all the work is done in the
  1286. write_page() function above.
  1287. */
  1288. static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1289. const uint8_t *buf)
  1290. {
  1291. /* for raw page writes, we want to disable ECC and simply write
  1292. whatever data is in the buffer. */
  1293. write_page(mtd, chip, buf, true);
  1294. }
  1295. static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1296. int page)
  1297. {
  1298. return write_oob_data(mtd, chip->oob_poi, page);
  1299. }
  1300. static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1301. int page, int sndcmd)
  1302. {
  1303. read_oob_data(mtd, chip->oob_poi, page);
  1304. return 0; /* notify NAND core to send command to
  1305. NAND device. */
  1306. }
  1307. static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  1308. uint8_t *buf, int page)
  1309. {
  1310. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1311. struct pci_dev *pci_dev = denali->dev;
  1312. dma_addr_t addr = denali->buf.dma_buf;
  1313. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  1314. uint32_t irq_status = 0;
  1315. uint32_t irq_mask = INTR_STATUS0__ECC_TRANSACTION_DONE |
  1316. INTR_STATUS0__ECC_ERR;
  1317. bool check_erased_page = false;
  1318. setup_ecc_for_xfer(denali, true, false);
  1319. denali_enable_dma(denali, true);
  1320. pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
  1321. clear_interrupts(denali);
  1322. denali_setup_dma(denali, DENALI_READ);
  1323. /* wait for operation to complete */
  1324. irq_status = wait_for_irq(denali, irq_mask);
  1325. pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
  1326. memcpy(buf, denali->buf.buf, mtd->writesize);
  1327. check_erased_page = handle_ecc(denali, buf, chip->oob_poi, irq_status);
  1328. denali_enable_dma(denali, false);
  1329. if (check_erased_page) {
  1330. read_oob_data(&denali->mtd, chip->oob_poi, denali->page);
  1331. /* check ECC failures that may have occurred on erased pages */
  1332. if (check_erased_page) {
  1333. if (!is_erased(buf, denali->mtd.writesize))
  1334. denali->mtd.ecc_stats.failed++;
  1335. if (!is_erased(buf, denali->mtd.oobsize))
  1336. denali->mtd.ecc_stats.failed++;
  1337. }
  1338. }
  1339. return 0;
  1340. }
  1341. static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1342. uint8_t *buf, int page)
  1343. {
  1344. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1345. struct pci_dev *pci_dev = denali->dev;
  1346. dma_addr_t addr = denali->buf.dma_buf;
  1347. size_t size = denali->mtd.writesize + denali->mtd.oobsize;
  1348. uint32_t irq_status = 0;
  1349. uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP;
  1350. setup_ecc_for_xfer(denali, false, true);
  1351. denali_enable_dma(denali, true);
  1352. pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
  1353. clear_interrupts(denali);
  1354. denali_setup_dma(denali, DENALI_READ);
  1355. /* wait for operation to complete */
  1356. irq_status = wait_for_irq(denali, irq_mask);
  1357. pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
  1358. denali_enable_dma(denali, false);
  1359. memcpy(buf, denali->buf.buf, mtd->writesize);
  1360. memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize);
  1361. return 0;
  1362. }
  1363. static uint8_t denali_read_byte(struct mtd_info *mtd)
  1364. {
  1365. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1366. uint8_t result = 0xff;
  1367. if (denali->buf.head < denali->buf.tail)
  1368. result = denali->buf.buf[denali->buf.head++];
  1369. #if DEBUG_DENALI
  1370. printk(KERN_INFO "read byte -> 0x%02x\n", result);
  1371. #endif
  1372. return result;
  1373. }
  1374. static void denali_select_chip(struct mtd_info *mtd, int chip)
  1375. {
  1376. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1377. #if DEBUG_DENALI
  1378. printk(KERN_INFO "denali select chip %d\n", chip);
  1379. #endif
  1380. spin_lock_irq(&denali->irq_lock);
  1381. denali->flash_bank = chip;
  1382. spin_unlock_irq(&denali->irq_lock);
  1383. }
  1384. static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  1385. {
  1386. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1387. int status = denali->status;
  1388. denali->status = 0;
  1389. #if DEBUG_DENALI
  1390. printk(KERN_INFO "waitfunc %d\n", status);
  1391. #endif
  1392. return status;
  1393. }
  1394. static void denali_erase(struct mtd_info *mtd, int page)
  1395. {
  1396. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1397. uint32_t cmd = 0x0, irq_status = 0;
  1398. #if DEBUG_DENALI
  1399. printk(KERN_INFO "erase page: %d\n", page);
  1400. #endif
  1401. /* clear interrupts */
  1402. clear_interrupts(denali);
  1403. /* setup page read request for access type */
  1404. cmd = MODE_10 | BANK(denali->flash_bank) | page;
  1405. index_addr(denali, (uint32_t)cmd, 0x1);
  1406. /* wait for erase to complete or failure to occur */
  1407. irq_status = wait_for_irq(denali, INTR_STATUS0__ERASE_COMP |
  1408. INTR_STATUS0__ERASE_FAIL);
  1409. denali->status = (irq_status & INTR_STATUS0__ERASE_FAIL) ?
  1410. NAND_STATUS_FAIL : PASS;
  1411. }
  1412. static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
  1413. int page)
  1414. {
  1415. struct denali_nand_info *denali = mtd_to_denali(mtd);
  1416. uint32_t addr, id;
  1417. int i;
  1418. #if DEBUG_DENALI
  1419. printk(KERN_INFO "cmdfunc: 0x%x %d %d\n", cmd, col, page);
  1420. #endif
  1421. switch (cmd) {
  1422. case NAND_CMD_PAGEPROG:
  1423. break;
  1424. case NAND_CMD_STATUS:
  1425. read_status(denali);
  1426. break;
  1427. case NAND_CMD_READID:
  1428. reset_buf(denali);
  1429. /*sometimes ManufactureId read from register is not right
  1430. * e.g. some of Micron MT29F32G08QAA MLC NAND chips
  1431. * So here we send READID cmd to NAND insteand
  1432. * */
  1433. addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
  1434. index_addr(denali, (uint32_t)addr | 0, 0x90);
  1435. index_addr(denali, (uint32_t)addr | 1, 0);
  1436. for (i = 0; i < 5; i++) {
  1437. index_addr_read_data(denali,
  1438. (uint32_t)addr | 2,
  1439. &id);
  1440. write_byte_to_buf(denali, id);
  1441. }
  1442. break;
  1443. case NAND_CMD_READ0:
  1444. case NAND_CMD_SEQIN:
  1445. denali->page = page;
  1446. break;
  1447. case NAND_CMD_RESET:
  1448. reset_bank(denali);
  1449. break;
  1450. case NAND_CMD_READOOB:
  1451. /* TODO: Read OOB data */
  1452. break;
  1453. default:
  1454. printk(KERN_ERR ": unsupported command"
  1455. " received 0x%x\n", cmd);
  1456. break;
  1457. }
  1458. }
  1459. /* stubs for ECC functions not used by the NAND core */
  1460. static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
  1461. uint8_t *ecc_code)
  1462. {
  1463. printk(KERN_ERR "denali_ecc_calculate called unexpectedly\n");
  1464. BUG();
  1465. return -EIO;
  1466. }
  1467. static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
  1468. uint8_t *read_ecc, uint8_t *calc_ecc)
  1469. {
  1470. printk(KERN_ERR "denali_ecc_correct called unexpectedly\n");
  1471. BUG();
  1472. return -EIO;
  1473. }
  1474. static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
  1475. {
  1476. printk(KERN_ERR "denali_ecc_hwctl called unexpectedly\n");
  1477. BUG();
  1478. }
  1479. /* end NAND core entry points */
  1480. /* Initialization code to bring the device up to a known good state */
  1481. static void denali_hw_init(struct denali_nand_info *denali)
  1482. {
  1483. denali_irq_init(denali);
  1484. denali_nand_reset(denali);
  1485. denali_write32(0x0F, denali->flash_reg + RB_PIN_ENABLED);
  1486. denali_write32(CHIP_EN_DONT_CARE__FLAG,
  1487. denali->flash_reg + CHIP_ENABLE_DONT_CARE);
  1488. denali_write32(0x0, denali->flash_reg + SPARE_AREA_SKIP_BYTES);
  1489. denali_write32(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
  1490. /* Should set value for these registers when init */
  1491. denali_write32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
  1492. denali_write32(1, denali->flash_reg + ECC_ENABLE);
  1493. }
  1494. /* ECC layout for SLC devices. Denali spec indicates SLC fixed at 4 bytes */
  1495. #define ECC_BYTES_SLC (4 * (2048 / ECC_SECTOR_SIZE))
  1496. static struct nand_ecclayout nand_oob_slc = {
  1497. .eccbytes = 4,
  1498. .eccpos = { 0, 1, 2, 3 }, /* not used */
  1499. .oobfree = {
  1500. {
  1501. .offset = ECC_BYTES_SLC,
  1502. .length = 64 - ECC_BYTES_SLC
  1503. }
  1504. }
  1505. };
  1506. #define ECC_BYTES_MLC (14 * (2048 / ECC_SECTOR_SIZE))
  1507. static struct nand_ecclayout nand_oob_mlc_14bit = {
  1508. .eccbytes = 14,
  1509. .eccpos = { 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, /* not used */
  1510. .oobfree = {
  1511. {
  1512. .offset = ECC_BYTES_MLC,
  1513. .length = 64 - ECC_BYTES_MLC
  1514. }
  1515. }
  1516. };
  1517. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1518. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1519. static struct nand_bbt_descr bbt_main_descr = {
  1520. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1521. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1522. .offs = 8,
  1523. .len = 4,
  1524. .veroffs = 12,
  1525. .maxblocks = 4,
  1526. .pattern = bbt_pattern,
  1527. };
  1528. static struct nand_bbt_descr bbt_mirror_descr = {
  1529. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1530. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1531. .offs = 8,
  1532. .len = 4,
  1533. .veroffs = 12,
  1534. .maxblocks = 4,
  1535. .pattern = mirror_pattern,
  1536. };
  1537. /* initalize driver data structures */
  1538. void denali_drv_init(struct denali_nand_info *denali)
  1539. {
  1540. denali->idx = 0;
  1541. /* setup interrupt handler */
  1542. /* the completion object will be used to notify
  1543. * the callee that the interrupt is done */
  1544. init_completion(&denali->complete);
  1545. /* the spinlock will be used to synchronize the ISR
  1546. * with any element that might be access shared
  1547. * data (interrupt status) */
  1548. spin_lock_init(&denali->irq_lock);
  1549. /* indicate that MTD has not selected a valid bank yet */
  1550. denali->flash_bank = CHIP_SELECT_INVALID;
  1551. /* initialize our irq_status variable to indicate no interrupts */
  1552. denali->irq_status = 0;
  1553. }
  1554. /* driver entry point */
  1555. static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1556. {
  1557. int ret = -ENODEV;
  1558. resource_size_t csr_base, mem_base;
  1559. unsigned long csr_len, mem_len;
  1560. struct denali_nand_info *denali;
  1561. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1562. __FILE__, __LINE__, __func__);
  1563. denali = kzalloc(sizeof(*denali), GFP_KERNEL);
  1564. if (!denali)
  1565. return -ENOMEM;
  1566. ret = pci_enable_device(dev);
  1567. if (ret) {
  1568. printk(KERN_ERR "Spectra: pci_enable_device failed.\n");
  1569. goto failed_enable;
  1570. }
  1571. if (id->driver_data == INTEL_CE4100) {
  1572. /* Due to a silicon limitation, we can only support
  1573. * ONFI timing mode 1 and below.
  1574. */
  1575. if (onfi_timing_mode < -1 || onfi_timing_mode > 1) {
  1576. printk(KERN_ERR "Intel CE4100 only supports"
  1577. " ONFI timing mode 1 or below\n");
  1578. ret = -EINVAL;
  1579. goto failed_enable;
  1580. }
  1581. denali->platform = INTEL_CE4100;
  1582. mem_base = pci_resource_start(dev, 0);
  1583. mem_len = pci_resource_len(dev, 1);
  1584. csr_base = pci_resource_start(dev, 1);
  1585. csr_len = pci_resource_len(dev, 1);
  1586. } else {
  1587. denali->platform = INTEL_MRST;
  1588. csr_base = pci_resource_start(dev, 0);
  1589. csr_len = pci_resource_start(dev, 0);
  1590. mem_base = pci_resource_start(dev, 1);
  1591. mem_len = pci_resource_len(dev, 1);
  1592. if (!mem_len) {
  1593. mem_base = csr_base + csr_len;
  1594. mem_len = csr_len;
  1595. nand_dbg_print(NAND_DBG_WARN,
  1596. "Spectra: No second"
  1597. " BAR for PCI device;"
  1598. " assuming %08Lx\n",
  1599. (uint64_t)csr_base);
  1600. }
  1601. }
  1602. /* Is 32-bit DMA supported? */
  1603. ret = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
  1604. if (ret) {
  1605. printk(KERN_ERR "Spectra: no usable DMA configuration\n");
  1606. goto failed_enable;
  1607. }
  1608. denali->buf.dma_buf =
  1609. pci_map_single(dev, denali->buf.buf,
  1610. DENALI_BUF_SIZE,
  1611. PCI_DMA_BIDIRECTIONAL);
  1612. if (pci_dma_mapping_error(dev, denali->buf.dma_buf)) {
  1613. printk(KERN_ERR "Spectra: failed to map DMA buffer\n");
  1614. goto failed_enable;
  1615. }
  1616. pci_set_master(dev);
  1617. denali->dev = dev;
  1618. ret = pci_request_regions(dev, DENALI_NAND_NAME);
  1619. if (ret) {
  1620. printk(KERN_ERR "Spectra: Unable to request memory regions\n");
  1621. goto failed_req_csr;
  1622. }
  1623. denali->flash_reg = ioremap_nocache(csr_base, csr_len);
  1624. if (!denali->flash_reg) {
  1625. printk(KERN_ERR "Spectra: Unable to remap memory region\n");
  1626. ret = -ENOMEM;
  1627. goto failed_remap_csr;
  1628. }
  1629. nand_dbg_print(NAND_DBG_DEBUG, "Spectra: CSR 0x%08Lx -> 0x%p (0x%lx)\n",
  1630. (uint64_t)csr_base, denali->flash_reg, csr_len);
  1631. denali->flash_mem = ioremap_nocache(mem_base, mem_len);
  1632. if (!denali->flash_mem) {
  1633. printk(KERN_ERR "Spectra: ioremap_nocache failed!");
  1634. iounmap(denali->flash_reg);
  1635. ret = -ENOMEM;
  1636. goto failed_remap_csr;
  1637. }
  1638. nand_dbg_print(NAND_DBG_WARN,
  1639. "Spectra: Remapped flash base address: "
  1640. "0x%p, len: %ld\n",
  1641. denali->flash_mem, csr_len);
  1642. denali_hw_init(denali);
  1643. denali_drv_init(denali);
  1644. nand_dbg_print(NAND_DBG_DEBUG, "Spectra: IRQ %d\n", dev->irq);
  1645. if (request_irq(dev->irq, denali_isr, IRQF_SHARED,
  1646. DENALI_NAND_NAME, denali)) {
  1647. printk(KERN_ERR "Spectra: Unable to allocate IRQ\n");
  1648. ret = -ENODEV;
  1649. goto failed_request_irq;
  1650. }
  1651. /* now that our ISR is registered, we can enable interrupts */
  1652. denali_set_intr_modes(denali, true);
  1653. pci_set_drvdata(dev, denali);
  1654. denali_nand_timing_set(denali);
  1655. /* MTD supported page sizes vary by kernel. We validate our
  1656. * kernel supports the device here.
  1657. */
  1658. if (denali->dev_info.wPageSize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE) {
  1659. ret = -ENODEV;
  1660. printk(KERN_ERR "Spectra: device size not supported by this "
  1661. "version of MTD.");
  1662. goto failed_nand;
  1663. }
  1664. nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:"
  1665. "acc_clks: %d, re_2_we: %d, we_2_re: %d,"
  1666. "addr_2_data: %d, rdwr_en_lo_cnt: %d, "
  1667. "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n",
  1668. ioread32(denali->flash_reg + ACC_CLKS),
  1669. ioread32(denali->flash_reg + RE_2_WE),
  1670. ioread32(denali->flash_reg + WE_2_RE),
  1671. ioread32(denali->flash_reg + ADDR_2_DATA),
  1672. ioread32(denali->flash_reg + RDWR_EN_LO_CNT),
  1673. ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
  1674. ioread32(denali->flash_reg + CS_SETUP_CNT));
  1675. denali->mtd.name = "Denali NAND";
  1676. denali->mtd.owner = THIS_MODULE;
  1677. denali->mtd.priv = &denali->nand;
  1678. /* register the driver with the NAND core subsystem */
  1679. denali->nand.select_chip = denali_select_chip;
  1680. denali->nand.cmdfunc = denali_cmdfunc;
  1681. denali->nand.read_byte = denali_read_byte;
  1682. denali->nand.waitfunc = denali_waitfunc;
  1683. /* scan for NAND devices attached to the controller
  1684. * this is the first stage in a two step process to register
  1685. * with the nand subsystem */
  1686. if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL)) {
  1687. ret = -ENXIO;
  1688. goto failed_nand;
  1689. }
  1690. /* second stage of the NAND scan
  1691. * this stage requires information regarding ECC and
  1692. * bad block management. */
  1693. /* Bad block management */
  1694. denali->nand.bbt_td = &bbt_main_descr;
  1695. denali->nand.bbt_md = &bbt_mirror_descr;
  1696. /* skip the scan for now until we have OOB read and write support */
  1697. denali->nand.options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN;
  1698. denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  1699. if (denali->dev_info.MLCDevice) {
  1700. denali->nand.ecc.layout = &nand_oob_mlc_14bit;
  1701. denali->nand.ecc.bytes = ECC_BYTES_MLC;
  1702. } else {/* SLC */
  1703. denali->nand.ecc.layout = &nand_oob_slc;
  1704. denali->nand.ecc.bytes = ECC_BYTES_SLC;
  1705. }
  1706. /* These functions are required by the NAND core framework, otherwise,
  1707. * the NAND core will assert. However, we don't need them, so we'll stub
  1708. * them out. */
  1709. denali->nand.ecc.calculate = denali_ecc_calculate;
  1710. denali->nand.ecc.correct = denali_ecc_correct;
  1711. denali->nand.ecc.hwctl = denali_ecc_hwctl;
  1712. /* override the default read operations */
  1713. denali->nand.ecc.size = denali->mtd.writesize;
  1714. denali->nand.ecc.read_page = denali_read_page;
  1715. denali->nand.ecc.read_page_raw = denali_read_page_raw;
  1716. denali->nand.ecc.write_page = denali_write_page;
  1717. denali->nand.ecc.write_page_raw = denali_write_page_raw;
  1718. denali->nand.ecc.read_oob = denali_read_oob;
  1719. denali->nand.ecc.write_oob = denali_write_oob;
  1720. denali->nand.erase_cmd = denali_erase;
  1721. if (nand_scan_tail(&denali->mtd)) {
  1722. ret = -ENXIO;
  1723. goto failed_nand;
  1724. }
  1725. ret = add_mtd_device(&denali->mtd);
  1726. if (ret) {
  1727. printk(KERN_ERR "Spectra: Failed to register"
  1728. " MTD device: %d\n", ret);
  1729. goto failed_nand;
  1730. }
  1731. return 0;
  1732. failed_nand:
  1733. denali_irq_cleanup(dev->irq, denali);
  1734. failed_request_irq:
  1735. iounmap(denali->flash_reg);
  1736. iounmap(denali->flash_mem);
  1737. failed_remap_csr:
  1738. pci_release_regions(dev);
  1739. failed_req_csr:
  1740. pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
  1741. PCI_DMA_BIDIRECTIONAL);
  1742. failed_enable:
  1743. kfree(denali);
  1744. return ret;
  1745. }
  1746. /* driver exit point */
  1747. static void denali_pci_remove(struct pci_dev *dev)
  1748. {
  1749. struct denali_nand_info *denali = pci_get_drvdata(dev);
  1750. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
  1751. __FILE__, __LINE__, __func__);
  1752. nand_release(&denali->mtd);
  1753. del_mtd_device(&denali->mtd);
  1754. denali_irq_cleanup(dev->irq, denali);
  1755. iounmap(denali->flash_reg);
  1756. iounmap(denali->flash_mem);
  1757. pci_release_regions(dev);
  1758. pci_disable_device(dev);
  1759. pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
  1760. PCI_DMA_BIDIRECTIONAL);
  1761. pci_set_drvdata(dev, NULL);
  1762. kfree(denali);
  1763. }
  1764. MODULE_DEVICE_TABLE(pci, denali_pci_ids);
  1765. static struct pci_driver denali_pci_driver = {
  1766. .name = DENALI_NAND_NAME,
  1767. .id_table = denali_pci_ids,
  1768. .probe = denali_pci_probe,
  1769. .remove = denali_pci_remove,
  1770. };
  1771. static int __devinit denali_init(void)
  1772. {
  1773. printk(KERN_INFO "Spectra MTD driver built on %s @ %s\n",
  1774. __DATE__, __TIME__);
  1775. return pci_register_driver(&denali_pci_driver);
  1776. }
  1777. /* Free memory */
  1778. static void __devexit denali_exit(void)
  1779. {
  1780. pci_unregister_driver(&denali_pci_driver);
  1781. }
  1782. module_init(denali_init);
  1783. module_exit(denali_exit);