omap2.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. /*
  2. * Copyright © 2004 Texas Instruments, Jian Zhang <jzhang@ti.com>
  3. * Copyright © 2004 Micron Technology Inc.
  4. * Copyright © 2004 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/platform_device.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/sched.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/omap-dma.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/mtd/nand_bch.h>
  27. #include <linux/platform_data/elm.h>
  28. #include <linux/platform_data/mtd-nand-omap2.h>
  29. #define DRIVER_NAME "omap2-nand"
  30. #define OMAP_NAND_TIMEOUT_MS 5000
  31. #define NAND_Ecc_P1e (1 << 0)
  32. #define NAND_Ecc_P2e (1 << 1)
  33. #define NAND_Ecc_P4e (1 << 2)
  34. #define NAND_Ecc_P8e (1 << 3)
  35. #define NAND_Ecc_P16e (1 << 4)
  36. #define NAND_Ecc_P32e (1 << 5)
  37. #define NAND_Ecc_P64e (1 << 6)
  38. #define NAND_Ecc_P128e (1 << 7)
  39. #define NAND_Ecc_P256e (1 << 8)
  40. #define NAND_Ecc_P512e (1 << 9)
  41. #define NAND_Ecc_P1024e (1 << 10)
  42. #define NAND_Ecc_P2048e (1 << 11)
  43. #define NAND_Ecc_P1o (1 << 16)
  44. #define NAND_Ecc_P2o (1 << 17)
  45. #define NAND_Ecc_P4o (1 << 18)
  46. #define NAND_Ecc_P8o (1 << 19)
  47. #define NAND_Ecc_P16o (1 << 20)
  48. #define NAND_Ecc_P32o (1 << 21)
  49. #define NAND_Ecc_P64o (1 << 22)
  50. #define NAND_Ecc_P128o (1 << 23)
  51. #define NAND_Ecc_P256o (1 << 24)
  52. #define NAND_Ecc_P512o (1 << 25)
  53. #define NAND_Ecc_P1024o (1 << 26)
  54. #define NAND_Ecc_P2048o (1 << 27)
  55. #define TF(value) (value ? 1 : 0)
  56. #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
  57. #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
  58. #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
  59. #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
  60. #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
  61. #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
  62. #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
  63. #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
  64. #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
  65. #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
  66. #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
  67. #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
  68. #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
  69. #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
  70. #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
  71. #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
  72. #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
  73. #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
  74. #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
  75. #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
  76. #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
  77. #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
  78. #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
  79. #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
  80. #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
  81. #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
  82. #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
  83. #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
  84. #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
  85. #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
  86. #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
  87. #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
  88. #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
  89. #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
  90. #define PREFETCH_CONFIG1_CS_SHIFT 24
  91. #define ECC_CONFIG_CS_SHIFT 1
  92. #define CS_MASK 0x7
  93. #define ENABLE_PREFETCH (0x1 << 7)
  94. #define DMA_MPU_MODE_SHIFT 2
  95. #define ECCSIZE0_SHIFT 12
  96. #define ECCSIZE1_SHIFT 22
  97. #define ECC1RESULTSIZE 0x1
  98. #define ECCCLEAR 0x100
  99. #define ECC1 0x1
  100. #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
  101. #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
  102. #define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
  103. #define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
  104. #define STATUS_BUFF_EMPTY 0x00000001
  105. #define OMAP24XX_DMA_GPMC 4
  106. #define SECTOR_BYTES 512
  107. /* 4 bit padding to make byte aligned, 56 = 52 + 4 */
  108. #define BCH4_BIT_PAD 4
  109. /* GPMC ecc engine settings for read */
  110. #define BCH_WRAPMODE_1 1 /* BCH wrap mode 1 */
  111. #define BCH8R_ECC_SIZE0 0x1a /* ecc_size0 = 26 */
  112. #define BCH8R_ECC_SIZE1 0x2 /* ecc_size1 = 2 */
  113. #define BCH4R_ECC_SIZE0 0xd /* ecc_size0 = 13 */
  114. #define BCH4R_ECC_SIZE1 0x3 /* ecc_size1 = 3 */
  115. /* GPMC ecc engine settings for write */
  116. #define BCH_WRAPMODE_6 6 /* BCH wrap mode 6 */
  117. #define BCH_ECC_SIZE0 0x0 /* ecc_size0 = 0, no oob protection */
  118. #define BCH_ECC_SIZE1 0x20 /* ecc_size1 = 32 */
  119. #define BADBLOCK_MARKER_LENGTH 2
  120. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  121. static u_char bch16_vector[] = {0xf5, 0x24, 0x1c, 0xd0, 0x61, 0xb3, 0xf1, 0x55,
  122. 0x2e, 0x2c, 0x86, 0xa3, 0xed, 0x36, 0x1b, 0x78,
  123. 0x48, 0x76, 0xa9, 0x3b, 0x97, 0xd1, 0x7a, 0x93,
  124. 0x07, 0x0e};
  125. static u_char bch8_vector[] = {0xf3, 0xdb, 0x14, 0x16, 0x8b, 0xd2, 0xbe, 0xcc,
  126. 0xac, 0x6b, 0xff, 0x99, 0x7b};
  127. static u_char bch4_vector[] = {0x00, 0x6b, 0x31, 0xdd, 0x41, 0xbc, 0x10};
  128. #endif
  129. /* oob info generated runtime depending on ecc algorithm and layout selected */
  130. static struct nand_ecclayout omap_oobinfo;
  131. struct omap_nand_info {
  132. struct nand_hw_control controller;
  133. struct omap_nand_platform_data *pdata;
  134. struct mtd_info mtd;
  135. struct nand_chip nand;
  136. struct platform_device *pdev;
  137. int gpmc_cs;
  138. unsigned long phys_base;
  139. enum omap_ecc ecc_opt;
  140. struct completion comp;
  141. struct dma_chan *dma;
  142. int gpmc_irq_fifo;
  143. int gpmc_irq_count;
  144. enum {
  145. OMAP_NAND_IO_READ = 0, /* read */
  146. OMAP_NAND_IO_WRITE, /* write */
  147. } iomode;
  148. u_char *buf;
  149. int buf_len;
  150. struct gpmc_nand_regs reg;
  151. /* fields specific for BCHx_HW ECC scheme */
  152. struct device *elm_dev;
  153. struct device_node *of_node;
  154. };
  155. /**
  156. * omap_prefetch_enable - configures and starts prefetch transfer
  157. * @cs: cs (chip select) number
  158. * @fifo_th: fifo threshold to be used for read/ write
  159. * @dma_mode: dma mode enable (1) or disable (0)
  160. * @u32_count: number of bytes to be transferred
  161. * @is_write: prefetch read(0) or write post(1) mode
  162. */
  163. static int omap_prefetch_enable(int cs, int fifo_th, int dma_mode,
  164. unsigned int u32_count, int is_write, struct omap_nand_info *info)
  165. {
  166. u32 val;
  167. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
  168. return -1;
  169. if (readl(info->reg.gpmc_prefetch_control))
  170. return -EBUSY;
  171. /* Set the amount of bytes to be prefetched */
  172. writel(u32_count, info->reg.gpmc_prefetch_config2);
  173. /* Set dma/mpu mode, the prefetch read / post write and
  174. * enable the engine. Set which cs is has requested for.
  175. */
  176. val = ((cs << PREFETCH_CONFIG1_CS_SHIFT) |
  177. PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH |
  178. (dma_mode << DMA_MPU_MODE_SHIFT) | (0x1 & is_write));
  179. writel(val, info->reg.gpmc_prefetch_config1);
  180. /* Start the prefetch engine */
  181. writel(0x1, info->reg.gpmc_prefetch_control);
  182. return 0;
  183. }
  184. /**
  185. * omap_prefetch_reset - disables and stops the prefetch engine
  186. */
  187. static int omap_prefetch_reset(int cs, struct omap_nand_info *info)
  188. {
  189. u32 config1;
  190. /* check if the same module/cs is trying to reset */
  191. config1 = readl(info->reg.gpmc_prefetch_config1);
  192. if (((config1 >> PREFETCH_CONFIG1_CS_SHIFT) & CS_MASK) != cs)
  193. return -EINVAL;
  194. /* Stop the PFPW engine */
  195. writel(0x0, info->reg.gpmc_prefetch_control);
  196. /* Reset/disable the PFPW engine */
  197. writel(0x0, info->reg.gpmc_prefetch_config1);
  198. return 0;
  199. }
  200. /**
  201. * omap_hwcontrol - hardware specific access to control-lines
  202. * @mtd: MTD device structure
  203. * @cmd: command to device
  204. * @ctrl:
  205. * NAND_NCE: bit 0 -> don't care
  206. * NAND_CLE: bit 1 -> Command Latch
  207. * NAND_ALE: bit 2 -> Address Latch
  208. *
  209. * NOTE: boards may use different bits for these!!
  210. */
  211. static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  212. {
  213. struct omap_nand_info *info = container_of(mtd,
  214. struct omap_nand_info, mtd);
  215. if (cmd != NAND_CMD_NONE) {
  216. if (ctrl & NAND_CLE)
  217. writeb(cmd, info->reg.gpmc_nand_command);
  218. else if (ctrl & NAND_ALE)
  219. writeb(cmd, info->reg.gpmc_nand_address);
  220. else /* NAND_NCE */
  221. writeb(cmd, info->reg.gpmc_nand_data);
  222. }
  223. }
  224. /**
  225. * omap_read_buf8 - read data from NAND controller into buffer
  226. * @mtd: MTD device structure
  227. * @buf: buffer to store date
  228. * @len: number of bytes to read
  229. */
  230. static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
  231. {
  232. struct nand_chip *nand = mtd->priv;
  233. ioread8_rep(nand->IO_ADDR_R, buf, len);
  234. }
  235. /**
  236. * omap_write_buf8 - write buffer to NAND controller
  237. * @mtd: MTD device structure
  238. * @buf: data buffer
  239. * @len: number of bytes to write
  240. */
  241. static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
  242. {
  243. struct omap_nand_info *info = container_of(mtd,
  244. struct omap_nand_info, mtd);
  245. u_char *p = (u_char *)buf;
  246. u32 status = 0;
  247. while (len--) {
  248. iowrite8(*p++, info->nand.IO_ADDR_W);
  249. /* wait until buffer is available for write */
  250. do {
  251. status = readl(info->reg.gpmc_status) &
  252. STATUS_BUFF_EMPTY;
  253. } while (!status);
  254. }
  255. }
  256. /**
  257. * omap_read_buf16 - read data from NAND controller into buffer
  258. * @mtd: MTD device structure
  259. * @buf: buffer to store date
  260. * @len: number of bytes to read
  261. */
  262. static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  263. {
  264. struct nand_chip *nand = mtd->priv;
  265. ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
  266. }
  267. /**
  268. * omap_write_buf16 - write buffer to NAND controller
  269. * @mtd: MTD device structure
  270. * @buf: data buffer
  271. * @len: number of bytes to write
  272. */
  273. static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
  274. {
  275. struct omap_nand_info *info = container_of(mtd,
  276. struct omap_nand_info, mtd);
  277. u16 *p = (u16 *) buf;
  278. u32 status = 0;
  279. /* FIXME try bursts of writesw() or DMA ... */
  280. len >>= 1;
  281. while (len--) {
  282. iowrite16(*p++, info->nand.IO_ADDR_W);
  283. /* wait until buffer is available for write */
  284. do {
  285. status = readl(info->reg.gpmc_status) &
  286. STATUS_BUFF_EMPTY;
  287. } while (!status);
  288. }
  289. }
  290. /**
  291. * omap_read_buf_pref - read data from NAND controller into buffer
  292. * @mtd: MTD device structure
  293. * @buf: buffer to store date
  294. * @len: number of bytes to read
  295. */
  296. static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
  297. {
  298. struct omap_nand_info *info = container_of(mtd,
  299. struct omap_nand_info, mtd);
  300. uint32_t r_count = 0;
  301. int ret = 0;
  302. u32 *p = (u32 *)buf;
  303. /* take care of subpage reads */
  304. if (len % 4) {
  305. if (info->nand.options & NAND_BUSWIDTH_16)
  306. omap_read_buf16(mtd, buf, len % 4);
  307. else
  308. omap_read_buf8(mtd, buf, len % 4);
  309. p = (u32 *) (buf + len % 4);
  310. len -= len % 4;
  311. }
  312. /* configure and start prefetch transfer */
  313. ret = omap_prefetch_enable(info->gpmc_cs,
  314. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0, info);
  315. if (ret) {
  316. /* PFPW engine is busy, use cpu copy method */
  317. if (info->nand.options & NAND_BUSWIDTH_16)
  318. omap_read_buf16(mtd, (u_char *)p, len);
  319. else
  320. omap_read_buf8(mtd, (u_char *)p, len);
  321. } else {
  322. do {
  323. r_count = readl(info->reg.gpmc_prefetch_status);
  324. r_count = PREFETCH_STATUS_FIFO_CNT(r_count);
  325. r_count = r_count >> 2;
  326. ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
  327. p += r_count;
  328. len -= r_count << 2;
  329. } while (len);
  330. /* disable and stop the PFPW engine */
  331. omap_prefetch_reset(info->gpmc_cs, info);
  332. }
  333. }
  334. /**
  335. * omap_write_buf_pref - write buffer to NAND controller
  336. * @mtd: MTD device structure
  337. * @buf: data buffer
  338. * @len: number of bytes to write
  339. */
  340. static void omap_write_buf_pref(struct mtd_info *mtd,
  341. const u_char *buf, int len)
  342. {
  343. struct omap_nand_info *info = container_of(mtd,
  344. struct omap_nand_info, mtd);
  345. uint32_t w_count = 0;
  346. int i = 0, ret = 0;
  347. u16 *p = (u16 *)buf;
  348. unsigned long tim, limit;
  349. u32 val;
  350. /* take care of subpage writes */
  351. if (len % 2 != 0) {
  352. writeb(*buf, info->nand.IO_ADDR_W);
  353. p = (u16 *)(buf + 1);
  354. len--;
  355. }
  356. /* configure and start prefetch transfer */
  357. ret = omap_prefetch_enable(info->gpmc_cs,
  358. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
  359. if (ret) {
  360. /* PFPW engine is busy, use cpu copy method */
  361. if (info->nand.options & NAND_BUSWIDTH_16)
  362. omap_write_buf16(mtd, (u_char *)p, len);
  363. else
  364. omap_write_buf8(mtd, (u_char *)p, len);
  365. } else {
  366. while (len) {
  367. w_count = readl(info->reg.gpmc_prefetch_status);
  368. w_count = PREFETCH_STATUS_FIFO_CNT(w_count);
  369. w_count = w_count >> 1;
  370. for (i = 0; (i < w_count) && len; i++, len -= 2)
  371. iowrite16(*p++, info->nand.IO_ADDR_W);
  372. }
  373. /* wait for data to flushed-out before reset the prefetch */
  374. tim = 0;
  375. limit = (loops_per_jiffy *
  376. msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  377. do {
  378. cpu_relax();
  379. val = readl(info->reg.gpmc_prefetch_status);
  380. val = PREFETCH_STATUS_COUNT(val);
  381. } while (val && (tim++ < limit));
  382. /* disable and stop the PFPW engine */
  383. omap_prefetch_reset(info->gpmc_cs, info);
  384. }
  385. }
  386. /*
  387. * omap_nand_dma_callback: callback on the completion of dma transfer
  388. * @data: pointer to completion data structure
  389. */
  390. static void omap_nand_dma_callback(void *data)
  391. {
  392. complete((struct completion *) data);
  393. }
  394. /*
  395. * omap_nand_dma_transfer: configure and start dma transfer
  396. * @mtd: MTD device structure
  397. * @addr: virtual address in RAM of source/destination
  398. * @len: number of data bytes to be transferred
  399. * @is_write: flag for read/write operation
  400. */
  401. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  402. unsigned int len, int is_write)
  403. {
  404. struct omap_nand_info *info = container_of(mtd,
  405. struct omap_nand_info, mtd);
  406. struct dma_async_tx_descriptor *tx;
  407. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  408. DMA_FROM_DEVICE;
  409. struct scatterlist sg;
  410. unsigned long tim, limit;
  411. unsigned n;
  412. int ret;
  413. u32 val;
  414. if (addr >= high_memory) {
  415. struct page *p1;
  416. if (((size_t)addr & PAGE_MASK) !=
  417. ((size_t)(addr + len - 1) & PAGE_MASK))
  418. goto out_copy;
  419. p1 = vmalloc_to_page(addr);
  420. if (!p1)
  421. goto out_copy;
  422. addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
  423. }
  424. sg_init_one(&sg, addr, len);
  425. n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
  426. if (n == 0) {
  427. dev_err(&info->pdev->dev,
  428. "Couldn't DMA map a %d byte buffer\n", len);
  429. goto out_copy;
  430. }
  431. tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
  432. is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  433. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  434. if (!tx)
  435. goto out_copy_unmap;
  436. tx->callback = omap_nand_dma_callback;
  437. tx->callback_param = &info->comp;
  438. dmaengine_submit(tx);
  439. /* configure and start prefetch transfer */
  440. ret = omap_prefetch_enable(info->gpmc_cs,
  441. PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
  442. if (ret)
  443. /* PFPW engine is busy, use cpu copy method */
  444. goto out_copy_unmap;
  445. init_completion(&info->comp);
  446. dma_async_issue_pending(info->dma);
  447. /* setup and start DMA using dma_addr */
  448. wait_for_completion(&info->comp);
  449. tim = 0;
  450. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  451. do {
  452. cpu_relax();
  453. val = readl(info->reg.gpmc_prefetch_status);
  454. val = PREFETCH_STATUS_COUNT(val);
  455. } while (val && (tim++ < limit));
  456. /* disable and stop the PFPW engine */
  457. omap_prefetch_reset(info->gpmc_cs, info);
  458. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  459. return 0;
  460. out_copy_unmap:
  461. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  462. out_copy:
  463. if (info->nand.options & NAND_BUSWIDTH_16)
  464. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  465. : omap_write_buf16(mtd, (u_char *) addr, len);
  466. else
  467. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  468. : omap_write_buf8(mtd, (u_char *) addr, len);
  469. return 0;
  470. }
  471. /**
  472. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  473. * @mtd: MTD device structure
  474. * @buf: buffer to store date
  475. * @len: number of bytes to read
  476. */
  477. static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
  478. {
  479. if (len <= mtd->oobsize)
  480. omap_read_buf_pref(mtd, buf, len);
  481. else
  482. /* start transfer in DMA mode */
  483. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  484. }
  485. /**
  486. * omap_write_buf_dma_pref - write buffer to NAND controller
  487. * @mtd: MTD device structure
  488. * @buf: data buffer
  489. * @len: number of bytes to write
  490. */
  491. static void omap_write_buf_dma_pref(struct mtd_info *mtd,
  492. const u_char *buf, int len)
  493. {
  494. if (len <= mtd->oobsize)
  495. omap_write_buf_pref(mtd, buf, len);
  496. else
  497. /* start transfer in DMA mode */
  498. omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
  499. }
  500. /*
  501. * omap_nand_irq - GPMC irq handler
  502. * @this_irq: gpmc irq number
  503. * @dev: omap_nand_info structure pointer is passed here
  504. */
  505. static irqreturn_t omap_nand_irq(int this_irq, void *dev)
  506. {
  507. struct omap_nand_info *info = (struct omap_nand_info *) dev;
  508. u32 bytes;
  509. bytes = readl(info->reg.gpmc_prefetch_status);
  510. bytes = PREFETCH_STATUS_FIFO_CNT(bytes);
  511. bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
  512. if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
  513. if (this_irq == info->gpmc_irq_count)
  514. goto done;
  515. if (info->buf_len && (info->buf_len < bytes))
  516. bytes = info->buf_len;
  517. else if (!info->buf_len)
  518. bytes = 0;
  519. iowrite32_rep(info->nand.IO_ADDR_W,
  520. (u32 *)info->buf, bytes >> 2);
  521. info->buf = info->buf + bytes;
  522. info->buf_len -= bytes;
  523. } else {
  524. ioread32_rep(info->nand.IO_ADDR_R,
  525. (u32 *)info->buf, bytes >> 2);
  526. info->buf = info->buf + bytes;
  527. if (this_irq == info->gpmc_irq_count)
  528. goto done;
  529. }
  530. return IRQ_HANDLED;
  531. done:
  532. complete(&info->comp);
  533. disable_irq_nosync(info->gpmc_irq_fifo);
  534. disable_irq_nosync(info->gpmc_irq_count);
  535. return IRQ_HANDLED;
  536. }
  537. /*
  538. * omap_read_buf_irq_pref - read data from NAND controller into buffer
  539. * @mtd: MTD device structure
  540. * @buf: buffer to store date
  541. * @len: number of bytes to read
  542. */
  543. static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
  544. {
  545. struct omap_nand_info *info = container_of(mtd,
  546. struct omap_nand_info, mtd);
  547. int ret = 0;
  548. if (len <= mtd->oobsize) {
  549. omap_read_buf_pref(mtd, buf, len);
  550. return;
  551. }
  552. info->iomode = OMAP_NAND_IO_READ;
  553. info->buf = buf;
  554. init_completion(&info->comp);
  555. /* configure and start prefetch transfer */
  556. ret = omap_prefetch_enable(info->gpmc_cs,
  557. PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
  558. if (ret)
  559. /* PFPW engine is busy, use cpu copy method */
  560. goto out_copy;
  561. info->buf_len = len;
  562. enable_irq(info->gpmc_irq_count);
  563. enable_irq(info->gpmc_irq_fifo);
  564. /* waiting for read to complete */
  565. wait_for_completion(&info->comp);
  566. /* disable and stop the PFPW engine */
  567. omap_prefetch_reset(info->gpmc_cs, info);
  568. return;
  569. out_copy:
  570. if (info->nand.options & NAND_BUSWIDTH_16)
  571. omap_read_buf16(mtd, buf, len);
  572. else
  573. omap_read_buf8(mtd, buf, len);
  574. }
  575. /*
  576. * omap_write_buf_irq_pref - write buffer to NAND controller
  577. * @mtd: MTD device structure
  578. * @buf: data buffer
  579. * @len: number of bytes to write
  580. */
  581. static void omap_write_buf_irq_pref(struct mtd_info *mtd,
  582. const u_char *buf, int len)
  583. {
  584. struct omap_nand_info *info = container_of(mtd,
  585. struct omap_nand_info, mtd);
  586. int ret = 0;
  587. unsigned long tim, limit;
  588. u32 val;
  589. if (len <= mtd->oobsize) {
  590. omap_write_buf_pref(mtd, buf, len);
  591. return;
  592. }
  593. info->iomode = OMAP_NAND_IO_WRITE;
  594. info->buf = (u_char *) buf;
  595. init_completion(&info->comp);
  596. /* configure and start prefetch transfer : size=24 */
  597. ret = omap_prefetch_enable(info->gpmc_cs,
  598. (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
  599. if (ret)
  600. /* PFPW engine is busy, use cpu copy method */
  601. goto out_copy;
  602. info->buf_len = len;
  603. enable_irq(info->gpmc_irq_count);
  604. enable_irq(info->gpmc_irq_fifo);
  605. /* waiting for write to complete */
  606. wait_for_completion(&info->comp);
  607. /* wait for data to flushed-out before reset the prefetch */
  608. tim = 0;
  609. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  610. do {
  611. val = readl(info->reg.gpmc_prefetch_status);
  612. val = PREFETCH_STATUS_COUNT(val);
  613. cpu_relax();
  614. } while (val && (tim++ < limit));
  615. /* disable and stop the PFPW engine */
  616. omap_prefetch_reset(info->gpmc_cs, info);
  617. return;
  618. out_copy:
  619. if (info->nand.options & NAND_BUSWIDTH_16)
  620. omap_write_buf16(mtd, buf, len);
  621. else
  622. omap_write_buf8(mtd, buf, len);
  623. }
  624. /**
  625. * gen_true_ecc - This function will generate true ECC value
  626. * @ecc_buf: buffer to store ecc code
  627. *
  628. * This generated true ECC value can be used when correcting
  629. * data read from NAND flash memory core
  630. */
  631. static void gen_true_ecc(u8 *ecc_buf)
  632. {
  633. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  634. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  635. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  636. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  637. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  638. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  639. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  640. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  641. }
  642. /**
  643. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  644. * @ecc_data1: ecc code from nand spare area
  645. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  646. * @page_data: page data
  647. *
  648. * This function compares two ECC's and indicates if there is an error.
  649. * If the error can be corrected it will be corrected to the buffer.
  650. * If there is no error, %0 is returned. If there is an error but it
  651. * was corrected, %1 is returned. Otherwise, %-1 is returned.
  652. */
  653. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  654. u8 *ecc_data2, /* read from register */
  655. u8 *page_data)
  656. {
  657. uint i;
  658. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  659. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  660. u8 ecc_bit[24];
  661. u8 ecc_sum = 0;
  662. u8 find_bit = 0;
  663. uint find_byte = 0;
  664. int isEccFF;
  665. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  666. gen_true_ecc(ecc_data1);
  667. gen_true_ecc(ecc_data2);
  668. for (i = 0; i <= 2; i++) {
  669. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  670. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  671. }
  672. for (i = 0; i < 8; i++) {
  673. tmp0_bit[i] = *ecc_data1 % 2;
  674. *ecc_data1 = *ecc_data1 / 2;
  675. }
  676. for (i = 0; i < 8; i++) {
  677. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  678. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  679. }
  680. for (i = 0; i < 8; i++) {
  681. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  682. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  683. }
  684. for (i = 0; i < 8; i++) {
  685. comp0_bit[i] = *ecc_data2 % 2;
  686. *ecc_data2 = *ecc_data2 / 2;
  687. }
  688. for (i = 0; i < 8; i++) {
  689. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  690. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  691. }
  692. for (i = 0; i < 8; i++) {
  693. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  694. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  695. }
  696. for (i = 0; i < 6; i++)
  697. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  698. for (i = 0; i < 8; i++)
  699. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  700. for (i = 0; i < 8; i++)
  701. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  702. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  703. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  704. for (i = 0; i < 24; i++)
  705. ecc_sum += ecc_bit[i];
  706. switch (ecc_sum) {
  707. case 0:
  708. /* Not reached because this function is not called if
  709. * ECC values are equal
  710. */
  711. return 0;
  712. case 1:
  713. /* Uncorrectable error */
  714. pr_debug("ECC UNCORRECTED_ERROR 1\n");
  715. return -1;
  716. case 11:
  717. /* UN-Correctable error */
  718. pr_debug("ECC UNCORRECTED_ERROR B\n");
  719. return -1;
  720. case 12:
  721. /* Correctable error */
  722. find_byte = (ecc_bit[23] << 8) +
  723. (ecc_bit[21] << 7) +
  724. (ecc_bit[19] << 6) +
  725. (ecc_bit[17] << 5) +
  726. (ecc_bit[15] << 4) +
  727. (ecc_bit[13] << 3) +
  728. (ecc_bit[11] << 2) +
  729. (ecc_bit[9] << 1) +
  730. ecc_bit[7];
  731. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  732. pr_debug("Correcting single bit ECC error at offset: "
  733. "%d, bit: %d\n", find_byte, find_bit);
  734. page_data[find_byte] ^= (1 << find_bit);
  735. return 1;
  736. default:
  737. if (isEccFF) {
  738. if (ecc_data2[0] == 0 &&
  739. ecc_data2[1] == 0 &&
  740. ecc_data2[2] == 0)
  741. return 0;
  742. }
  743. pr_debug("UNCORRECTED_ERROR default\n");
  744. return -1;
  745. }
  746. }
  747. /**
  748. * omap_correct_data - Compares the ECC read with HW generated ECC
  749. * @mtd: MTD device structure
  750. * @dat: page data
  751. * @read_ecc: ecc read from nand flash
  752. * @calc_ecc: ecc read from HW ECC registers
  753. *
  754. * Compares the ecc read from nand spare area with ECC registers values
  755. * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
  756. * detection and correction. If there are no errors, %0 is returned. If
  757. * there were errors and all of the errors were corrected, the number of
  758. * corrected errors is returned. If uncorrectable errors exist, %-1 is
  759. * returned.
  760. */
  761. static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
  762. u_char *read_ecc, u_char *calc_ecc)
  763. {
  764. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  765. mtd);
  766. int blockCnt = 0, i = 0, ret = 0;
  767. int stat = 0;
  768. /* Ex NAND_ECC_HW12_2048 */
  769. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  770. (info->nand.ecc.size == 2048))
  771. blockCnt = 4;
  772. else
  773. blockCnt = 1;
  774. for (i = 0; i < blockCnt; i++) {
  775. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  776. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  777. if (ret < 0)
  778. return ret;
  779. /* keep track of the number of corrected errors */
  780. stat += ret;
  781. }
  782. read_ecc += 3;
  783. calc_ecc += 3;
  784. dat += 512;
  785. }
  786. return stat;
  787. }
  788. /**
  789. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  790. * @mtd: MTD device structure
  791. * @dat: The pointer to data on which ecc is computed
  792. * @ecc_code: The ecc_code buffer
  793. *
  794. * Using noninverted ECC can be considered ugly since writing a blank
  795. * page ie. padding will clear the ECC bytes. This is no problem as long
  796. * nobody is trying to write data on the seemingly unused page. Reading
  797. * an erased page will produce an ECC mismatch between generated and read
  798. * ECC bytes that has to be dealt with separately.
  799. */
  800. static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  801. u_char *ecc_code)
  802. {
  803. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  804. mtd);
  805. u32 val;
  806. val = readl(info->reg.gpmc_ecc_config);
  807. if (((val >> ECC_CONFIG_CS_SHIFT) & ~CS_MASK) != info->gpmc_cs)
  808. return -EINVAL;
  809. /* read ecc result */
  810. val = readl(info->reg.gpmc_ecc1_result);
  811. *ecc_code++ = val; /* P128e, ..., P1e */
  812. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  813. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  814. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  815. return 0;
  816. }
  817. /**
  818. * omap_enable_hwecc - This function enables the hardware ecc functionality
  819. * @mtd: MTD device structure
  820. * @mode: Read/Write mode
  821. */
  822. static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
  823. {
  824. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  825. mtd);
  826. struct nand_chip *chip = mtd->priv;
  827. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  828. u32 val;
  829. /* clear ecc and enable bits */
  830. val = ECCCLEAR | ECC1;
  831. writel(val, info->reg.gpmc_ecc_control);
  832. /* program ecc and result sizes */
  833. val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
  834. ECC1RESULTSIZE);
  835. writel(val, info->reg.gpmc_ecc_size_config);
  836. switch (mode) {
  837. case NAND_ECC_READ:
  838. case NAND_ECC_WRITE:
  839. writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
  840. break;
  841. case NAND_ECC_READSYN:
  842. writel(ECCCLEAR, info->reg.gpmc_ecc_control);
  843. break;
  844. default:
  845. dev_info(&info->pdev->dev,
  846. "error: unrecognized Mode[%d]!\n", mode);
  847. break;
  848. }
  849. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  850. val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
  851. writel(val, info->reg.gpmc_ecc_config);
  852. }
  853. /**
  854. * omap_wait - wait until the command is done
  855. * @mtd: MTD device structure
  856. * @chip: NAND Chip structure
  857. *
  858. * Wait function is called during Program and erase operations and
  859. * the way it is called from MTD layer, we should wait till the NAND
  860. * chip is ready after the programming/erase operation has completed.
  861. *
  862. * Erase can take up to 400ms and program up to 20ms according to
  863. * general NAND and SmartMedia specs
  864. */
  865. static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
  866. {
  867. struct nand_chip *this = mtd->priv;
  868. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  869. mtd);
  870. unsigned long timeo = jiffies;
  871. int status, state = this->state;
  872. if (state == FL_ERASING)
  873. timeo += msecs_to_jiffies(400);
  874. else
  875. timeo += msecs_to_jiffies(20);
  876. writeb(NAND_CMD_STATUS & 0xFF, info->reg.gpmc_nand_command);
  877. while (time_before(jiffies, timeo)) {
  878. status = readb(info->reg.gpmc_nand_data);
  879. if (status & NAND_STATUS_READY)
  880. break;
  881. cond_resched();
  882. }
  883. status = readb(info->reg.gpmc_nand_data);
  884. return status;
  885. }
  886. /**
  887. * omap_dev_ready - calls the platform specific dev_ready function
  888. * @mtd: MTD device structure
  889. */
  890. static int omap_dev_ready(struct mtd_info *mtd)
  891. {
  892. unsigned int val = 0;
  893. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  894. mtd);
  895. val = readl(info->reg.gpmc_status);
  896. if ((val & 0x100) == 0x100) {
  897. return 1;
  898. } else {
  899. return 0;
  900. }
  901. }
  902. /**
  903. * omap_enable_hwecc_bch - Program GPMC to perform BCH ECC calculation
  904. * @mtd: MTD device structure
  905. * @mode: Read/Write mode
  906. *
  907. * When using BCH, sector size is hardcoded to 512 bytes.
  908. * Using wrapping mode 6 both for reading and writing if ELM module not uses
  909. * for error correction.
  910. * On writing,
  911. * eccsize0 = 0 (no additional protected byte in spare area)
  912. * eccsize1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
  913. */
  914. static void __maybe_unused omap_enable_hwecc_bch(struct mtd_info *mtd, int mode)
  915. {
  916. unsigned int bch_type;
  917. unsigned int dev_width, nsectors;
  918. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  919. mtd);
  920. enum omap_ecc ecc_opt = info->ecc_opt;
  921. struct nand_chip *chip = mtd->priv;
  922. u32 val, wr_mode;
  923. unsigned int ecc_size1, ecc_size0;
  924. /* GPMC configurations for calculating ECC */
  925. switch (ecc_opt) {
  926. case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
  927. bch_type = 0;
  928. nsectors = 1;
  929. if (mode == NAND_ECC_READ) {
  930. wr_mode = BCH_WRAPMODE_6;
  931. ecc_size0 = BCH_ECC_SIZE0;
  932. ecc_size1 = BCH_ECC_SIZE1;
  933. } else {
  934. wr_mode = BCH_WRAPMODE_6;
  935. ecc_size0 = BCH_ECC_SIZE0;
  936. ecc_size1 = BCH_ECC_SIZE1;
  937. }
  938. break;
  939. case OMAP_ECC_BCH4_CODE_HW:
  940. bch_type = 0;
  941. nsectors = chip->ecc.steps;
  942. if (mode == NAND_ECC_READ) {
  943. wr_mode = BCH_WRAPMODE_1;
  944. ecc_size0 = BCH4R_ECC_SIZE0;
  945. ecc_size1 = BCH4R_ECC_SIZE1;
  946. } else {
  947. wr_mode = BCH_WRAPMODE_6;
  948. ecc_size0 = BCH_ECC_SIZE0;
  949. ecc_size1 = BCH_ECC_SIZE1;
  950. }
  951. break;
  952. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  953. bch_type = 1;
  954. nsectors = 1;
  955. if (mode == NAND_ECC_READ) {
  956. wr_mode = BCH_WRAPMODE_6;
  957. ecc_size0 = BCH_ECC_SIZE0;
  958. ecc_size1 = BCH_ECC_SIZE1;
  959. } else {
  960. wr_mode = BCH_WRAPMODE_6;
  961. ecc_size0 = BCH_ECC_SIZE0;
  962. ecc_size1 = BCH_ECC_SIZE1;
  963. }
  964. break;
  965. case OMAP_ECC_BCH8_CODE_HW:
  966. bch_type = 1;
  967. nsectors = chip->ecc.steps;
  968. if (mode == NAND_ECC_READ) {
  969. wr_mode = BCH_WRAPMODE_1;
  970. ecc_size0 = BCH8R_ECC_SIZE0;
  971. ecc_size1 = BCH8R_ECC_SIZE1;
  972. } else {
  973. wr_mode = BCH_WRAPMODE_6;
  974. ecc_size0 = BCH_ECC_SIZE0;
  975. ecc_size1 = BCH_ECC_SIZE1;
  976. }
  977. break;
  978. case OMAP_ECC_BCH16_CODE_HW:
  979. bch_type = 0x2;
  980. nsectors = chip->ecc.steps;
  981. if (mode == NAND_ECC_READ) {
  982. wr_mode = 0x01;
  983. ecc_size0 = 52; /* ECC bits in nibbles per sector */
  984. ecc_size1 = 0; /* non-ECC bits in nibbles per sector */
  985. } else {
  986. wr_mode = 0x01;
  987. ecc_size0 = 0; /* extra bits in nibbles per sector */
  988. ecc_size1 = 52; /* OOB bits in nibbles per sector */
  989. }
  990. break;
  991. default:
  992. return;
  993. }
  994. writel(ECC1, info->reg.gpmc_ecc_control);
  995. /* Configure ecc size for BCH */
  996. val = (ecc_size1 << ECCSIZE1_SHIFT) | (ecc_size0 << ECCSIZE0_SHIFT);
  997. writel(val, info->reg.gpmc_ecc_size_config);
  998. dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  999. /* BCH configuration */
  1000. val = ((1 << 16) | /* enable BCH */
  1001. (bch_type << 12) | /* BCH4/BCH8/BCH16 */
  1002. (wr_mode << 8) | /* wrap mode */
  1003. (dev_width << 7) | /* bus width */
  1004. (((nsectors-1) & 0x7) << 4) | /* number of sectors */
  1005. (info->gpmc_cs << 1) | /* ECC CS */
  1006. (0x1)); /* enable ECC */
  1007. writel(val, info->reg.gpmc_ecc_config);
  1008. /* Clear ecc and enable bits */
  1009. writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
  1010. }
  1011. static u8 bch4_polynomial[] = {0x28, 0x13, 0xcc, 0x39, 0x96, 0xac, 0x7f};
  1012. static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
  1013. 0x97, 0x79, 0xe5, 0x24, 0xb5};
  1014. /**
  1015. * omap_calculate_ecc_bch - Generate bytes of ECC bytes
  1016. * @mtd: MTD device structure
  1017. * @dat: The pointer to data on which ecc is computed
  1018. * @ecc_code: The ecc_code buffer
  1019. *
  1020. * Support calculating of BCH4/8 ecc vectors for the page
  1021. */
  1022. static int __maybe_unused omap_calculate_ecc_bch(struct mtd_info *mtd,
  1023. const u_char *dat, u_char *ecc_calc)
  1024. {
  1025. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1026. mtd);
  1027. int eccbytes = info->nand.ecc.bytes;
  1028. struct gpmc_nand_regs *gpmc_regs = &info->reg;
  1029. u8 *ecc_code;
  1030. unsigned long nsectors, bch_val1, bch_val2, bch_val3, bch_val4;
  1031. u32 val;
  1032. int i, j;
  1033. nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
  1034. for (i = 0; i < nsectors; i++) {
  1035. ecc_code = ecc_calc;
  1036. switch (info->ecc_opt) {
  1037. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  1038. case OMAP_ECC_BCH8_CODE_HW:
  1039. bch_val1 = readl(gpmc_regs->gpmc_bch_result0[i]);
  1040. bch_val2 = readl(gpmc_regs->gpmc_bch_result1[i]);
  1041. bch_val3 = readl(gpmc_regs->gpmc_bch_result2[i]);
  1042. bch_val4 = readl(gpmc_regs->gpmc_bch_result3[i]);
  1043. *ecc_code++ = (bch_val4 & 0xFF);
  1044. *ecc_code++ = ((bch_val3 >> 24) & 0xFF);
  1045. *ecc_code++ = ((bch_val3 >> 16) & 0xFF);
  1046. *ecc_code++ = ((bch_val3 >> 8) & 0xFF);
  1047. *ecc_code++ = (bch_val3 & 0xFF);
  1048. *ecc_code++ = ((bch_val2 >> 24) & 0xFF);
  1049. *ecc_code++ = ((bch_val2 >> 16) & 0xFF);
  1050. *ecc_code++ = ((bch_val2 >> 8) & 0xFF);
  1051. *ecc_code++ = (bch_val2 & 0xFF);
  1052. *ecc_code++ = ((bch_val1 >> 24) & 0xFF);
  1053. *ecc_code++ = ((bch_val1 >> 16) & 0xFF);
  1054. *ecc_code++ = ((bch_val1 >> 8) & 0xFF);
  1055. *ecc_code++ = (bch_val1 & 0xFF);
  1056. break;
  1057. case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
  1058. case OMAP_ECC_BCH4_CODE_HW:
  1059. bch_val1 = readl(gpmc_regs->gpmc_bch_result0[i]);
  1060. bch_val2 = readl(gpmc_regs->gpmc_bch_result1[i]);
  1061. *ecc_code++ = ((bch_val2 >> 12) & 0xFF);
  1062. *ecc_code++ = ((bch_val2 >> 4) & 0xFF);
  1063. *ecc_code++ = ((bch_val2 & 0xF) << 4) |
  1064. ((bch_val1 >> 28) & 0xF);
  1065. *ecc_code++ = ((bch_val1 >> 20) & 0xFF);
  1066. *ecc_code++ = ((bch_val1 >> 12) & 0xFF);
  1067. *ecc_code++ = ((bch_val1 >> 4) & 0xFF);
  1068. *ecc_code++ = ((bch_val1 & 0xF) << 4);
  1069. break;
  1070. case OMAP_ECC_BCH16_CODE_HW:
  1071. val = readl(gpmc_regs->gpmc_bch_result6[i]);
  1072. ecc_code[0] = ((val >> 8) & 0xFF);
  1073. ecc_code[1] = ((val >> 0) & 0xFF);
  1074. val = readl(gpmc_regs->gpmc_bch_result5[i]);
  1075. ecc_code[2] = ((val >> 24) & 0xFF);
  1076. ecc_code[3] = ((val >> 16) & 0xFF);
  1077. ecc_code[4] = ((val >> 8) & 0xFF);
  1078. ecc_code[5] = ((val >> 0) & 0xFF);
  1079. val = readl(gpmc_regs->gpmc_bch_result4[i]);
  1080. ecc_code[6] = ((val >> 24) & 0xFF);
  1081. ecc_code[7] = ((val >> 16) & 0xFF);
  1082. ecc_code[8] = ((val >> 8) & 0xFF);
  1083. ecc_code[9] = ((val >> 0) & 0xFF);
  1084. val = readl(gpmc_regs->gpmc_bch_result3[i]);
  1085. ecc_code[10] = ((val >> 24) & 0xFF);
  1086. ecc_code[11] = ((val >> 16) & 0xFF);
  1087. ecc_code[12] = ((val >> 8) & 0xFF);
  1088. ecc_code[13] = ((val >> 0) & 0xFF);
  1089. val = readl(gpmc_regs->gpmc_bch_result2[i]);
  1090. ecc_code[14] = ((val >> 24) & 0xFF);
  1091. ecc_code[15] = ((val >> 16) & 0xFF);
  1092. ecc_code[16] = ((val >> 8) & 0xFF);
  1093. ecc_code[17] = ((val >> 0) & 0xFF);
  1094. val = readl(gpmc_regs->gpmc_bch_result1[i]);
  1095. ecc_code[18] = ((val >> 24) & 0xFF);
  1096. ecc_code[19] = ((val >> 16) & 0xFF);
  1097. ecc_code[20] = ((val >> 8) & 0xFF);
  1098. ecc_code[21] = ((val >> 0) & 0xFF);
  1099. val = readl(gpmc_regs->gpmc_bch_result0[i]);
  1100. ecc_code[22] = ((val >> 24) & 0xFF);
  1101. ecc_code[23] = ((val >> 16) & 0xFF);
  1102. ecc_code[24] = ((val >> 8) & 0xFF);
  1103. ecc_code[25] = ((val >> 0) & 0xFF);
  1104. break;
  1105. default:
  1106. return -EINVAL;
  1107. }
  1108. /* ECC scheme specific syndrome customizations */
  1109. switch (info->ecc_opt) {
  1110. case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
  1111. /* Add constant polynomial to remainder, so that
  1112. * ECC of blank pages results in 0x0 on reading back */
  1113. for (j = 0; j < eccbytes; j++)
  1114. ecc_calc[j] ^= bch4_polynomial[j];
  1115. break;
  1116. case OMAP_ECC_BCH4_CODE_HW:
  1117. /* Set 8th ECC byte as 0x0 for ROM compatibility */
  1118. ecc_calc[eccbytes - 1] = 0x0;
  1119. break;
  1120. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  1121. /* Add constant polynomial to remainder, so that
  1122. * ECC of blank pages results in 0x0 on reading back */
  1123. for (j = 0; j < eccbytes; j++)
  1124. ecc_calc[j] ^= bch8_polynomial[j];
  1125. break;
  1126. case OMAP_ECC_BCH8_CODE_HW:
  1127. /* Set 14th ECC byte as 0x0 for ROM compatibility */
  1128. ecc_calc[eccbytes - 1] = 0x0;
  1129. break;
  1130. case OMAP_ECC_BCH16_CODE_HW:
  1131. break;
  1132. default:
  1133. return -EINVAL;
  1134. }
  1135. ecc_calc += eccbytes;
  1136. }
  1137. return 0;
  1138. }
  1139. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  1140. /**
  1141. * erased_sector_bitflips - count bit flips
  1142. * @data: data sector buffer
  1143. * @oob: oob buffer
  1144. * @info: omap_nand_info
  1145. *
  1146. * Check the bit flips in erased page falls below correctable level.
  1147. * If falls below, report the page as erased with correctable bit
  1148. * flip, else report as uncorrectable page.
  1149. */
  1150. static int erased_sector_bitflips(u_char *data, u_char *oob,
  1151. struct omap_nand_info *info)
  1152. {
  1153. int flip_bits = 0, i;
  1154. for (i = 0; i < info->nand.ecc.size; i++) {
  1155. flip_bits += hweight8(~data[i]);
  1156. if (flip_bits > info->nand.ecc.strength)
  1157. return 0;
  1158. }
  1159. for (i = 0; i < info->nand.ecc.bytes - 1; i++) {
  1160. flip_bits += hweight8(~oob[i]);
  1161. if (flip_bits > info->nand.ecc.strength)
  1162. return 0;
  1163. }
  1164. /*
  1165. * Bit flips falls in correctable level.
  1166. * Fill data area with 0xFF
  1167. */
  1168. if (flip_bits) {
  1169. memset(data, 0xFF, info->nand.ecc.size);
  1170. memset(oob, 0xFF, info->nand.ecc.bytes);
  1171. }
  1172. return flip_bits;
  1173. }
  1174. /**
  1175. * omap_elm_correct_data - corrects page data area in case error reported
  1176. * @mtd: MTD device structure
  1177. * @data: page data
  1178. * @read_ecc: ecc read from nand flash
  1179. * @calc_ecc: ecc read from HW ECC registers
  1180. *
  1181. * Calculated ecc vector reported as zero in case of non-error pages.
  1182. * In case of non-zero ecc vector, first filter out erased-pages, and
  1183. * then process data via ELM to detect bit-flips.
  1184. */
  1185. static int omap_elm_correct_data(struct mtd_info *mtd, u_char *data,
  1186. u_char *read_ecc, u_char *calc_ecc)
  1187. {
  1188. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1189. mtd);
  1190. struct nand_ecc_ctrl *ecc = &info->nand.ecc;
  1191. int eccsteps = info->nand.ecc.steps;
  1192. int i , j, stat = 0;
  1193. int eccflag, actual_eccbytes;
  1194. struct elm_errorvec err_vec[ERROR_VECTOR_MAX];
  1195. u_char *ecc_vec = calc_ecc;
  1196. u_char *spare_ecc = read_ecc;
  1197. u_char *erased_ecc_vec;
  1198. u_char *buf;
  1199. int bitflip_count;
  1200. bool is_error_reported = false;
  1201. u32 bit_pos, byte_pos, error_max, pos;
  1202. int err;
  1203. switch (info->ecc_opt) {
  1204. case OMAP_ECC_BCH4_CODE_HW:
  1205. /* omit 7th ECC byte reserved for ROM code compatibility */
  1206. actual_eccbytes = ecc->bytes - 1;
  1207. erased_ecc_vec = bch4_vector;
  1208. break;
  1209. case OMAP_ECC_BCH8_CODE_HW:
  1210. /* omit 14th ECC byte reserved for ROM code compatibility */
  1211. actual_eccbytes = ecc->bytes - 1;
  1212. erased_ecc_vec = bch8_vector;
  1213. break;
  1214. case OMAP_ECC_BCH16_CODE_HW:
  1215. actual_eccbytes = ecc->bytes;
  1216. erased_ecc_vec = bch16_vector;
  1217. break;
  1218. default:
  1219. pr_err("invalid driver configuration\n");
  1220. return -EINVAL;
  1221. }
  1222. /* Initialize elm error vector to zero */
  1223. memset(err_vec, 0, sizeof(err_vec));
  1224. for (i = 0; i < eccsteps ; i++) {
  1225. eccflag = 0; /* initialize eccflag */
  1226. /*
  1227. * Check any error reported,
  1228. * In case of error, non zero ecc reported.
  1229. */
  1230. for (j = 0; j < actual_eccbytes; j++) {
  1231. if (calc_ecc[j] != 0) {
  1232. eccflag = 1; /* non zero ecc, error present */
  1233. break;
  1234. }
  1235. }
  1236. if (eccflag == 1) {
  1237. if (memcmp(calc_ecc, erased_ecc_vec,
  1238. actual_eccbytes) == 0) {
  1239. /*
  1240. * calc_ecc[] matches pattern for ECC(all 0xff)
  1241. * so this is definitely an erased-page
  1242. */
  1243. } else {
  1244. buf = &data[info->nand.ecc.size * i];
  1245. /*
  1246. * count number of 0-bits in read_buf.
  1247. * This check can be removed once a similar
  1248. * check is introduced in generic NAND driver
  1249. */
  1250. bitflip_count = erased_sector_bitflips(
  1251. buf, read_ecc, info);
  1252. if (bitflip_count) {
  1253. /*
  1254. * number of 0-bits within ECC limits
  1255. * So this may be an erased-page
  1256. */
  1257. stat += bitflip_count;
  1258. } else {
  1259. /*
  1260. * Too many 0-bits. It may be a
  1261. * - programmed-page, OR
  1262. * - erased-page with many bit-flips
  1263. * So this page requires check by ELM
  1264. */
  1265. err_vec[i].error_reported = true;
  1266. is_error_reported = true;
  1267. }
  1268. }
  1269. }
  1270. /* Update the ecc vector */
  1271. calc_ecc += ecc->bytes;
  1272. read_ecc += ecc->bytes;
  1273. }
  1274. /* Check if any error reported */
  1275. if (!is_error_reported)
  1276. return stat;
  1277. /* Decode BCH error using ELM module */
  1278. elm_decode_bch_error_page(info->elm_dev, ecc_vec, err_vec);
  1279. err = 0;
  1280. for (i = 0; i < eccsteps; i++) {
  1281. if (err_vec[i].error_uncorrectable) {
  1282. pr_err("nand: uncorrectable bit-flips found\n");
  1283. err = -EBADMSG;
  1284. } else if (err_vec[i].error_reported) {
  1285. for (j = 0; j < err_vec[i].error_count; j++) {
  1286. switch (info->ecc_opt) {
  1287. case OMAP_ECC_BCH4_CODE_HW:
  1288. /* Add 4 bits to take care of padding */
  1289. pos = err_vec[i].error_loc[j] +
  1290. BCH4_BIT_PAD;
  1291. break;
  1292. case OMAP_ECC_BCH8_CODE_HW:
  1293. case OMAP_ECC_BCH16_CODE_HW:
  1294. pos = err_vec[i].error_loc[j];
  1295. break;
  1296. default:
  1297. return -EINVAL;
  1298. }
  1299. error_max = (ecc->size + actual_eccbytes) * 8;
  1300. /* Calculate bit position of error */
  1301. bit_pos = pos % 8;
  1302. /* Calculate byte position of error */
  1303. byte_pos = (error_max - pos - 1) / 8;
  1304. if (pos < error_max) {
  1305. if (byte_pos < 512) {
  1306. pr_debug("bitflip@dat[%d]=%x\n",
  1307. byte_pos, data[byte_pos]);
  1308. data[byte_pos] ^= 1 << bit_pos;
  1309. } else {
  1310. pr_debug("bitflip@oob[%d]=%x\n",
  1311. (byte_pos - 512),
  1312. spare_ecc[byte_pos - 512]);
  1313. spare_ecc[byte_pos - 512] ^=
  1314. 1 << bit_pos;
  1315. }
  1316. } else {
  1317. pr_err("invalid bit-flip @ %d:%d\n",
  1318. byte_pos, bit_pos);
  1319. err = -EBADMSG;
  1320. }
  1321. }
  1322. }
  1323. /* Update number of correctable errors */
  1324. stat += err_vec[i].error_count;
  1325. /* Update page data with sector size */
  1326. data += ecc->size;
  1327. spare_ecc += ecc->bytes;
  1328. }
  1329. return (err) ? err : stat;
  1330. }
  1331. /**
  1332. * omap_write_page_bch - BCH ecc based write page function for entire page
  1333. * @mtd: mtd info structure
  1334. * @chip: nand chip info structure
  1335. * @buf: data buffer
  1336. * @oob_required: must write chip->oob_poi to OOB
  1337. *
  1338. * Custom write page method evolved to support multi sector writing in one shot
  1339. */
  1340. static int omap_write_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
  1341. const uint8_t *buf, int oob_required)
  1342. {
  1343. int i;
  1344. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1345. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1346. /* Enable GPMC ecc engine */
  1347. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1348. /* Write data */
  1349. chip->write_buf(mtd, buf, mtd->writesize);
  1350. /* Update ecc vector from GPMC result registers */
  1351. chip->ecc.calculate(mtd, buf, &ecc_calc[0]);
  1352. for (i = 0; i < chip->ecc.total; i++)
  1353. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1354. /* Write ecc vector to OOB area */
  1355. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1356. return 0;
  1357. }
  1358. /**
  1359. * omap_read_page_bch - BCH ecc based page read function for entire page
  1360. * @mtd: mtd info structure
  1361. * @chip: nand chip info structure
  1362. * @buf: buffer to store read data
  1363. * @oob_required: caller requires OOB data read to chip->oob_poi
  1364. * @page: page number to read
  1365. *
  1366. * For BCH ecc scheme, GPMC used for syndrome calculation and ELM module
  1367. * used for error correction.
  1368. * Custom method evolved to support ELM error correction & multi sector
  1369. * reading. On reading page data area is read along with OOB data with
  1370. * ecc engine enabled. ecc vector updated after read of OOB data.
  1371. * For non error pages ecc vector reported as zero.
  1372. */
  1373. static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
  1374. uint8_t *buf, int oob_required, int page)
  1375. {
  1376. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1377. uint8_t *ecc_code = chip->buffers->ecccode;
  1378. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1379. uint8_t *oob = &chip->oob_poi[eccpos[0]];
  1380. uint32_t oob_pos = mtd->writesize + chip->ecc.layout->eccpos[0];
  1381. int stat;
  1382. unsigned int max_bitflips = 0;
  1383. /* Enable GPMC ecc engine */
  1384. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1385. /* Read data */
  1386. chip->read_buf(mtd, buf, mtd->writesize);
  1387. /* Read oob bytes */
  1388. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
  1389. chip->read_buf(mtd, oob, chip->ecc.total);
  1390. /* Calculate ecc bytes */
  1391. chip->ecc.calculate(mtd, buf, ecc_calc);
  1392. memcpy(ecc_code, &chip->oob_poi[eccpos[0]], chip->ecc.total);
  1393. stat = chip->ecc.correct(mtd, buf, ecc_code, ecc_calc);
  1394. if (stat < 0) {
  1395. mtd->ecc_stats.failed++;
  1396. } else {
  1397. mtd->ecc_stats.corrected += stat;
  1398. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1399. }
  1400. return max_bitflips;
  1401. }
  1402. /**
  1403. * is_elm_present - checks for presence of ELM module by scanning DT nodes
  1404. * @omap_nand_info: NAND device structure containing platform data
  1405. * @bch_type: 0x0=BCH4, 0x1=BCH8, 0x2=BCH16
  1406. */
  1407. static int is_elm_present(struct omap_nand_info *info,
  1408. struct device_node *elm_node, enum bch_ecc bch_type)
  1409. {
  1410. struct platform_device *pdev;
  1411. struct nand_ecc_ctrl *ecc = &info->nand.ecc;
  1412. int err;
  1413. /* check whether elm-id is passed via DT */
  1414. if (!elm_node) {
  1415. pr_err("nand: error: ELM DT node not found\n");
  1416. return -ENODEV;
  1417. }
  1418. pdev = of_find_device_by_node(elm_node);
  1419. /* check whether ELM device is registered */
  1420. if (!pdev) {
  1421. pr_err("nand: error: ELM device not found\n");
  1422. return -ENODEV;
  1423. }
  1424. /* ELM module available, now configure it */
  1425. info->elm_dev = &pdev->dev;
  1426. err = elm_config(info->elm_dev, bch_type,
  1427. (info->mtd.writesize / ecc->size), ecc->size, ecc->bytes);
  1428. return err;
  1429. }
  1430. #endif /* CONFIG_MTD_NAND_ECC_BCH */
  1431. static int omap_nand_probe(struct platform_device *pdev)
  1432. {
  1433. struct omap_nand_info *info;
  1434. struct omap_nand_platform_data *pdata;
  1435. struct mtd_info *mtd;
  1436. struct nand_chip *nand_chip;
  1437. struct nand_ecclayout *ecclayout;
  1438. int err;
  1439. int i;
  1440. dma_cap_mask_t mask;
  1441. unsigned sig;
  1442. unsigned oob_index;
  1443. struct resource *res;
  1444. struct mtd_part_parser_data ppdata = {};
  1445. pdata = dev_get_platdata(&pdev->dev);
  1446. if (pdata == NULL) {
  1447. dev_err(&pdev->dev, "platform data missing\n");
  1448. return -ENODEV;
  1449. }
  1450. info = devm_kzalloc(&pdev->dev, sizeof(struct omap_nand_info),
  1451. GFP_KERNEL);
  1452. if (!info)
  1453. return -ENOMEM;
  1454. platform_set_drvdata(pdev, info);
  1455. spin_lock_init(&info->controller.lock);
  1456. init_waitqueue_head(&info->controller.wq);
  1457. info->pdev = pdev;
  1458. info->gpmc_cs = pdata->cs;
  1459. info->reg = pdata->reg;
  1460. info->of_node = pdata->of_node;
  1461. info->ecc_opt = pdata->ecc_opt;
  1462. mtd = &info->mtd;
  1463. mtd->priv = &info->nand;
  1464. mtd->name = dev_name(&pdev->dev);
  1465. mtd->owner = THIS_MODULE;
  1466. nand_chip = &info->nand;
  1467. nand_chip->ecc.priv = NULL;
  1468. nand_chip->options |= NAND_SKIP_BBTSCAN;
  1469. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1470. nand_chip->IO_ADDR_R = devm_ioremap_resource(&pdev->dev, res);
  1471. if (IS_ERR(nand_chip->IO_ADDR_R))
  1472. return PTR_ERR(nand_chip->IO_ADDR_R);
  1473. info->phys_base = res->start;
  1474. nand_chip->controller = &info->controller;
  1475. nand_chip->IO_ADDR_W = nand_chip->IO_ADDR_R;
  1476. nand_chip->cmd_ctrl = omap_hwcontrol;
  1477. /*
  1478. * If RDY/BSY line is connected to OMAP then use the omap ready
  1479. * function and the generic nand_wait function which reads the status
  1480. * register after monitoring the RDY/BSY line. Otherwise use a standard
  1481. * chip delay which is slightly more than tR (AC Timing) of the NAND
  1482. * device and read status register until you get a failure or success
  1483. */
  1484. if (pdata->dev_ready) {
  1485. nand_chip->dev_ready = omap_dev_ready;
  1486. nand_chip->chip_delay = 0;
  1487. } else {
  1488. nand_chip->waitfunc = omap_wait;
  1489. nand_chip->chip_delay = 50;
  1490. }
  1491. /* scan NAND device connected to chip controller */
  1492. nand_chip->options |= pdata->devsize & NAND_BUSWIDTH_16;
  1493. if (nand_scan_ident(mtd, 1, NULL)) {
  1494. pr_err("nand device scan failed, may be bus-width mismatch\n");
  1495. err = -ENXIO;
  1496. goto return_error;
  1497. }
  1498. /* check for small page devices */
  1499. if ((mtd->oobsize < 64) && (pdata->ecc_opt != OMAP_ECC_HAM1_CODE_HW)) {
  1500. pr_err("small page devices are not supported\n");
  1501. err = -EINVAL;
  1502. goto return_error;
  1503. }
  1504. /* re-populate low-level callbacks based on xfer modes */
  1505. switch (pdata->xfer_type) {
  1506. case NAND_OMAP_PREFETCH_POLLED:
  1507. nand_chip->read_buf = omap_read_buf_pref;
  1508. nand_chip->write_buf = omap_write_buf_pref;
  1509. break;
  1510. case NAND_OMAP_POLLED:
  1511. /* Use nand_base defaults for {read,write}_buf */
  1512. break;
  1513. case NAND_OMAP_PREFETCH_DMA:
  1514. dma_cap_zero(mask);
  1515. dma_cap_set(DMA_SLAVE, mask);
  1516. sig = OMAP24XX_DMA_GPMC;
  1517. info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1518. if (!info->dma) {
  1519. dev_err(&pdev->dev, "DMA engine request failed\n");
  1520. err = -ENXIO;
  1521. goto return_error;
  1522. } else {
  1523. struct dma_slave_config cfg;
  1524. memset(&cfg, 0, sizeof(cfg));
  1525. cfg.src_addr = info->phys_base;
  1526. cfg.dst_addr = info->phys_base;
  1527. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1528. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1529. cfg.src_maxburst = 16;
  1530. cfg.dst_maxburst = 16;
  1531. err = dmaengine_slave_config(info->dma, &cfg);
  1532. if (err) {
  1533. dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
  1534. err);
  1535. goto return_error;
  1536. }
  1537. nand_chip->read_buf = omap_read_buf_dma_pref;
  1538. nand_chip->write_buf = omap_write_buf_dma_pref;
  1539. }
  1540. break;
  1541. case NAND_OMAP_PREFETCH_IRQ:
  1542. info->gpmc_irq_fifo = platform_get_irq(pdev, 0);
  1543. if (info->gpmc_irq_fifo <= 0) {
  1544. dev_err(&pdev->dev, "error getting fifo irq\n");
  1545. err = -ENODEV;
  1546. goto return_error;
  1547. }
  1548. err = devm_request_irq(&pdev->dev, info->gpmc_irq_fifo,
  1549. omap_nand_irq, IRQF_SHARED,
  1550. "gpmc-nand-fifo", info);
  1551. if (err) {
  1552. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  1553. info->gpmc_irq_fifo, err);
  1554. info->gpmc_irq_fifo = 0;
  1555. goto return_error;
  1556. }
  1557. info->gpmc_irq_count = platform_get_irq(pdev, 1);
  1558. if (info->gpmc_irq_count <= 0) {
  1559. dev_err(&pdev->dev, "error getting count irq\n");
  1560. err = -ENODEV;
  1561. goto return_error;
  1562. }
  1563. err = devm_request_irq(&pdev->dev, info->gpmc_irq_count,
  1564. omap_nand_irq, IRQF_SHARED,
  1565. "gpmc-nand-count", info);
  1566. if (err) {
  1567. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  1568. info->gpmc_irq_count, err);
  1569. info->gpmc_irq_count = 0;
  1570. goto return_error;
  1571. }
  1572. nand_chip->read_buf = omap_read_buf_irq_pref;
  1573. nand_chip->write_buf = omap_write_buf_irq_pref;
  1574. break;
  1575. default:
  1576. dev_err(&pdev->dev,
  1577. "xfer_type(%d) not supported!\n", pdata->xfer_type);
  1578. err = -EINVAL;
  1579. goto return_error;
  1580. }
  1581. /* populate MTD interface based on ECC scheme */
  1582. nand_chip->ecc.layout = &omap_oobinfo;
  1583. ecclayout = &omap_oobinfo;
  1584. switch (info->ecc_opt) {
  1585. case OMAP_ECC_HAM1_CODE_HW:
  1586. pr_info("nand: using OMAP_ECC_HAM1_CODE_HW\n");
  1587. nand_chip->ecc.mode = NAND_ECC_HW;
  1588. nand_chip->ecc.bytes = 3;
  1589. nand_chip->ecc.size = 512;
  1590. nand_chip->ecc.strength = 1;
  1591. nand_chip->ecc.calculate = omap_calculate_ecc;
  1592. nand_chip->ecc.hwctl = omap_enable_hwecc;
  1593. nand_chip->ecc.correct = omap_correct_data;
  1594. /* define ECC layout */
  1595. ecclayout->eccbytes = nand_chip->ecc.bytes *
  1596. (mtd->writesize /
  1597. nand_chip->ecc.size);
  1598. if (nand_chip->options & NAND_BUSWIDTH_16)
  1599. oob_index = BADBLOCK_MARKER_LENGTH;
  1600. else
  1601. oob_index = 1;
  1602. for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
  1603. ecclayout->eccpos[i] = oob_index;
  1604. /* no reserved-marker in ecclayout for this ecc-scheme */
  1605. ecclayout->oobfree->offset =
  1606. ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
  1607. break;
  1608. case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
  1609. #ifdef CONFIG_MTD_NAND_ECC_BCH
  1610. pr_info("nand: using OMAP_ECC_BCH4_CODE_HW_DETECTION_SW\n");
  1611. nand_chip->ecc.mode = NAND_ECC_HW;
  1612. nand_chip->ecc.size = 512;
  1613. nand_chip->ecc.bytes = 7;
  1614. nand_chip->ecc.strength = 4;
  1615. nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
  1616. nand_chip->ecc.correct = nand_bch_correct_data;
  1617. nand_chip->ecc.calculate = omap_calculate_ecc_bch;
  1618. /* define ECC layout */
  1619. ecclayout->eccbytes = nand_chip->ecc.bytes *
  1620. (mtd->writesize /
  1621. nand_chip->ecc.size);
  1622. oob_index = BADBLOCK_MARKER_LENGTH;
  1623. for (i = 0; i < ecclayout->eccbytes; i++, oob_index++) {
  1624. ecclayout->eccpos[i] = oob_index;
  1625. if (((i + 1) % nand_chip->ecc.bytes) == 0)
  1626. oob_index++;
  1627. }
  1628. /* include reserved-marker in ecclayout->oobfree calculation */
  1629. ecclayout->oobfree->offset = 1 +
  1630. ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
  1631. /* software bch library is used for locating errors */
  1632. nand_chip->ecc.priv = nand_bch_init(mtd,
  1633. nand_chip->ecc.size,
  1634. nand_chip->ecc.bytes,
  1635. &nand_chip->ecc.layout);
  1636. if (!nand_chip->ecc.priv) {
  1637. pr_err("nand: error: unable to use s/w BCH library\n");
  1638. err = -EINVAL;
  1639. }
  1640. break;
  1641. #else
  1642. pr_err("nand: error: CONFIG_MTD_NAND_ECC_BCH not enabled\n");
  1643. err = -EINVAL;
  1644. goto return_error;
  1645. #endif
  1646. case OMAP_ECC_BCH4_CODE_HW:
  1647. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  1648. pr_info("nand: using OMAP_ECC_BCH4_CODE_HW ECC scheme\n");
  1649. nand_chip->ecc.mode = NAND_ECC_HW;
  1650. nand_chip->ecc.size = 512;
  1651. /* 14th bit is kept reserved for ROM-code compatibility */
  1652. nand_chip->ecc.bytes = 7 + 1;
  1653. nand_chip->ecc.strength = 4;
  1654. nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
  1655. nand_chip->ecc.correct = omap_elm_correct_data;
  1656. nand_chip->ecc.calculate = omap_calculate_ecc_bch;
  1657. nand_chip->ecc.read_page = omap_read_page_bch;
  1658. nand_chip->ecc.write_page = omap_write_page_bch;
  1659. /* define ECC layout */
  1660. ecclayout->eccbytes = nand_chip->ecc.bytes *
  1661. (mtd->writesize /
  1662. nand_chip->ecc.size);
  1663. oob_index = BADBLOCK_MARKER_LENGTH;
  1664. for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
  1665. ecclayout->eccpos[i] = oob_index;
  1666. /* reserved marker already included in ecclayout->eccbytes */
  1667. ecclayout->oobfree->offset =
  1668. ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
  1669. /* This ECC scheme requires ELM H/W block */
  1670. if (is_elm_present(info, pdata->elm_of_node, BCH4_ECC) < 0) {
  1671. pr_err("nand: error: could not initialize ELM\n");
  1672. err = -ENODEV;
  1673. goto return_error;
  1674. }
  1675. break;
  1676. #else
  1677. pr_err("nand: error: CONFIG_MTD_NAND_OMAP_BCH not enabled\n");
  1678. err = -EINVAL;
  1679. goto return_error;
  1680. #endif
  1681. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  1682. #ifdef CONFIG_MTD_NAND_ECC_BCH
  1683. pr_info("nand: using OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
  1684. nand_chip->ecc.mode = NAND_ECC_HW;
  1685. nand_chip->ecc.size = 512;
  1686. nand_chip->ecc.bytes = 13;
  1687. nand_chip->ecc.strength = 8;
  1688. nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
  1689. nand_chip->ecc.correct = nand_bch_correct_data;
  1690. nand_chip->ecc.calculate = omap_calculate_ecc_bch;
  1691. /* define ECC layout */
  1692. ecclayout->eccbytes = nand_chip->ecc.bytes *
  1693. (mtd->writesize /
  1694. nand_chip->ecc.size);
  1695. oob_index = BADBLOCK_MARKER_LENGTH;
  1696. for (i = 0; i < ecclayout->eccbytes; i++, oob_index++) {
  1697. ecclayout->eccpos[i] = oob_index;
  1698. if (((i + 1) % nand_chip->ecc.bytes) == 0)
  1699. oob_index++;
  1700. }
  1701. /* include reserved-marker in ecclayout->oobfree calculation */
  1702. ecclayout->oobfree->offset = 1 +
  1703. ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
  1704. /* software bch library is used for locating errors */
  1705. nand_chip->ecc.priv = nand_bch_init(mtd,
  1706. nand_chip->ecc.size,
  1707. nand_chip->ecc.bytes,
  1708. &nand_chip->ecc.layout);
  1709. if (!nand_chip->ecc.priv) {
  1710. pr_err("nand: error: unable to use s/w BCH library\n");
  1711. err = -EINVAL;
  1712. goto return_error;
  1713. }
  1714. break;
  1715. #else
  1716. pr_err("nand: error: CONFIG_MTD_NAND_ECC_BCH not enabled\n");
  1717. err = -EINVAL;
  1718. goto return_error;
  1719. #endif
  1720. case OMAP_ECC_BCH8_CODE_HW:
  1721. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  1722. pr_info("nand: using OMAP_ECC_BCH8_CODE_HW ECC scheme\n");
  1723. nand_chip->ecc.mode = NAND_ECC_HW;
  1724. nand_chip->ecc.size = 512;
  1725. /* 14th bit is kept reserved for ROM-code compatibility */
  1726. nand_chip->ecc.bytes = 13 + 1;
  1727. nand_chip->ecc.strength = 8;
  1728. nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
  1729. nand_chip->ecc.correct = omap_elm_correct_data;
  1730. nand_chip->ecc.calculate = omap_calculate_ecc_bch;
  1731. nand_chip->ecc.read_page = omap_read_page_bch;
  1732. nand_chip->ecc.write_page = omap_write_page_bch;
  1733. /* This ECC scheme requires ELM H/W block */
  1734. err = is_elm_present(info, pdata->elm_of_node, BCH8_ECC);
  1735. if (err < 0) {
  1736. pr_err("nand: error: could not initialize ELM\n");
  1737. goto return_error;
  1738. }
  1739. /* define ECC layout */
  1740. ecclayout->eccbytes = nand_chip->ecc.bytes *
  1741. (mtd->writesize /
  1742. nand_chip->ecc.size);
  1743. oob_index = BADBLOCK_MARKER_LENGTH;
  1744. for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
  1745. ecclayout->eccpos[i] = oob_index;
  1746. /* reserved marker already included in ecclayout->eccbytes */
  1747. ecclayout->oobfree->offset =
  1748. ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
  1749. break;
  1750. #else
  1751. pr_err("nand: error: CONFIG_MTD_NAND_OMAP_BCH not enabled\n");
  1752. err = -EINVAL;
  1753. goto return_error;
  1754. #endif
  1755. case OMAP_ECC_BCH16_CODE_HW:
  1756. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  1757. pr_info("using OMAP_ECC_BCH16_CODE_HW ECC scheme\n");
  1758. nand_chip->ecc.mode = NAND_ECC_HW;
  1759. nand_chip->ecc.size = 512;
  1760. nand_chip->ecc.bytes = 26;
  1761. nand_chip->ecc.strength = 16;
  1762. nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
  1763. nand_chip->ecc.correct = omap_elm_correct_data;
  1764. nand_chip->ecc.calculate = omap_calculate_ecc_bch;
  1765. nand_chip->ecc.read_page = omap_read_page_bch;
  1766. nand_chip->ecc.write_page = omap_write_page_bch;
  1767. /* This ECC scheme requires ELM H/W block */
  1768. err = is_elm_present(info, pdata->elm_of_node, BCH16_ECC);
  1769. if (err < 0) {
  1770. pr_err("ELM is required for this ECC scheme\n");
  1771. goto return_error;
  1772. }
  1773. /* define ECC layout */
  1774. ecclayout->eccbytes = nand_chip->ecc.bytes *
  1775. (mtd->writesize /
  1776. nand_chip->ecc.size);
  1777. oob_index = BADBLOCK_MARKER_LENGTH;
  1778. for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
  1779. ecclayout->eccpos[i] = oob_index;
  1780. /* reserved marker already included in ecclayout->eccbytes */
  1781. ecclayout->oobfree->offset =
  1782. ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
  1783. break;
  1784. #else
  1785. pr_err("nand: error: CONFIG_MTD_NAND_OMAP_BCH not enabled\n");
  1786. err = -EINVAL;
  1787. goto return_error;
  1788. #endif
  1789. default:
  1790. pr_err("nand: error: invalid or unsupported ECC scheme\n");
  1791. err = -EINVAL;
  1792. goto return_error;
  1793. }
  1794. /* all OOB bytes from oobfree->offset till end off OOB are free */
  1795. ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
  1796. /* check if NAND device's OOB is enough to store ECC signatures */
  1797. if (mtd->oobsize < (ecclayout->eccbytes + BADBLOCK_MARKER_LENGTH)) {
  1798. pr_err("not enough OOB bytes required = %d, available=%d\n",
  1799. ecclayout->eccbytes, mtd->oobsize);
  1800. err = -EINVAL;
  1801. goto return_error;
  1802. }
  1803. /* second phase scan */
  1804. if (nand_scan_tail(mtd)) {
  1805. err = -ENXIO;
  1806. goto return_error;
  1807. }
  1808. ppdata.of_node = pdata->of_node;
  1809. mtd_device_parse_register(mtd, NULL, &ppdata, pdata->parts,
  1810. pdata->nr_parts);
  1811. platform_set_drvdata(pdev, mtd);
  1812. return 0;
  1813. return_error:
  1814. if (info->dma)
  1815. dma_release_channel(info->dma);
  1816. if (nand_chip->ecc.priv) {
  1817. nand_bch_free(nand_chip->ecc.priv);
  1818. nand_chip->ecc.priv = NULL;
  1819. }
  1820. return err;
  1821. }
  1822. static int omap_nand_remove(struct platform_device *pdev)
  1823. {
  1824. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1825. struct nand_chip *nand_chip = mtd->priv;
  1826. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1827. mtd);
  1828. if (nand_chip->ecc.priv) {
  1829. nand_bch_free(nand_chip->ecc.priv);
  1830. nand_chip->ecc.priv = NULL;
  1831. }
  1832. if (info->dma)
  1833. dma_release_channel(info->dma);
  1834. nand_release(mtd);
  1835. return 0;
  1836. }
  1837. static struct platform_driver omap_nand_driver = {
  1838. .probe = omap_nand_probe,
  1839. .remove = omap_nand_remove,
  1840. .driver = {
  1841. .name = DRIVER_NAME,
  1842. .owner = THIS_MODULE,
  1843. },
  1844. };
  1845. module_platform_driver(omap_nand_driver);
  1846. MODULE_ALIAS("platform:" DRIVER_NAME);
  1847. MODULE_LICENSE("GPL");
  1848. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");