omap2.c 63 KB

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