omap2.c 64 KB

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