omap2.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322
  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. * @chip: NAND chip object
  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 nand_chip *chip, int cmd, unsigned int ctrl)
  218. {
  219. struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
  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->legacy.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.legacy.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->legacy.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.legacy.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. * @chip: NAND chip object
  294. * @buf: buffer to store date
  295. * @len: number of bytes to read
  296. */
  297. static void omap_read_buf_pref(struct nand_chip *chip, u_char *buf, int len)
  298. {
  299. struct mtd_info *mtd = nand_to_mtd(chip);
  300. struct omap_nand_info *info = mtd_to_omap(mtd);
  301. uint32_t r_count = 0;
  302. int ret = 0;
  303. u32 *p = (u32 *)buf;
  304. /* take care of subpage reads */
  305. if (len % 4) {
  306. if (info->nand.options & NAND_BUSWIDTH_16)
  307. omap_read_buf16(mtd, buf, len % 4);
  308. else
  309. omap_read_buf8(mtd, buf, len % 4);
  310. p = (u32 *) (buf + len % 4);
  311. len -= len % 4;
  312. }
  313. /* configure and start prefetch transfer */
  314. ret = omap_prefetch_enable(info->gpmc_cs,
  315. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0, info);
  316. if (ret) {
  317. /* PFPW engine is busy, use cpu copy method */
  318. if (info->nand.options & NAND_BUSWIDTH_16)
  319. omap_read_buf16(mtd, (u_char *)p, len);
  320. else
  321. omap_read_buf8(mtd, (u_char *)p, len);
  322. } else {
  323. do {
  324. r_count = readl(info->reg.gpmc_prefetch_status);
  325. r_count = PREFETCH_STATUS_FIFO_CNT(r_count);
  326. r_count = r_count >> 2;
  327. ioread32_rep(info->nand.legacy.IO_ADDR_R, p, r_count);
  328. p += r_count;
  329. len -= r_count << 2;
  330. } while (len);
  331. /* disable and stop the PFPW engine */
  332. omap_prefetch_reset(info->gpmc_cs, info);
  333. }
  334. }
  335. /**
  336. * omap_write_buf_pref - write buffer to NAND controller
  337. * @chip: NAND chip object
  338. * @buf: data buffer
  339. * @len: number of bytes to write
  340. */
  341. static void omap_write_buf_pref(struct nand_chip *chip, const u_char *buf,
  342. int len)
  343. {
  344. struct mtd_info *mtd = nand_to_mtd(chip);
  345. struct omap_nand_info *info = mtd_to_omap(mtd);
  346. uint32_t w_count = 0;
  347. int i = 0, ret = 0;
  348. u16 *p = (u16 *)buf;
  349. unsigned long tim, limit;
  350. u32 val;
  351. /* take care of subpage writes */
  352. if (len % 2 != 0) {
  353. writeb(*buf, info->nand.legacy.IO_ADDR_W);
  354. p = (u16 *)(buf + 1);
  355. len--;
  356. }
  357. /* configure and start prefetch transfer */
  358. ret = omap_prefetch_enable(info->gpmc_cs,
  359. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
  360. if (ret) {
  361. /* PFPW engine is busy, use cpu copy method */
  362. if (info->nand.options & NAND_BUSWIDTH_16)
  363. omap_write_buf16(mtd, (u_char *)p, len);
  364. else
  365. omap_write_buf8(mtd, (u_char *)p, len);
  366. } else {
  367. while (len) {
  368. w_count = readl(info->reg.gpmc_prefetch_status);
  369. w_count = PREFETCH_STATUS_FIFO_CNT(w_count);
  370. w_count = w_count >> 1;
  371. for (i = 0; (i < w_count) && len; i++, len -= 2)
  372. iowrite16(*p++, info->nand.legacy.IO_ADDR_W);
  373. }
  374. /* wait for data to flushed-out before reset the prefetch */
  375. tim = 0;
  376. limit = (loops_per_jiffy *
  377. msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  378. do {
  379. cpu_relax();
  380. val = readl(info->reg.gpmc_prefetch_status);
  381. val = PREFETCH_STATUS_COUNT(val);
  382. } while (val && (tim++ < limit));
  383. /* disable and stop the PFPW engine */
  384. omap_prefetch_reset(info->gpmc_cs, info);
  385. }
  386. }
  387. /*
  388. * omap_nand_dma_callback: callback on the completion of dma transfer
  389. * @data: pointer to completion data structure
  390. */
  391. static void omap_nand_dma_callback(void *data)
  392. {
  393. complete((struct completion *) data);
  394. }
  395. /*
  396. * omap_nand_dma_transfer: configure and start dma transfer
  397. * @mtd: MTD device structure
  398. * @addr: virtual address in RAM of source/destination
  399. * @len: number of data bytes to be transferred
  400. * @is_write: flag for read/write operation
  401. */
  402. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  403. unsigned int len, int is_write)
  404. {
  405. struct omap_nand_info *info = mtd_to_omap(mtd);
  406. struct dma_async_tx_descriptor *tx;
  407. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  408. DMA_FROM_DEVICE;
  409. struct scatterlist sg;
  410. unsigned long tim, limit;
  411. unsigned n;
  412. int ret;
  413. u32 val;
  414. if (!virt_addr_valid(addr))
  415. goto out_copy;
  416. sg_init_one(&sg, addr, len);
  417. n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
  418. if (n == 0) {
  419. dev_err(&info->pdev->dev,
  420. "Couldn't DMA map a %d byte buffer\n", len);
  421. goto out_copy;
  422. }
  423. tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
  424. is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  425. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  426. if (!tx)
  427. goto out_copy_unmap;
  428. tx->callback = omap_nand_dma_callback;
  429. tx->callback_param = &info->comp;
  430. dmaengine_submit(tx);
  431. init_completion(&info->comp);
  432. /* setup and start DMA using dma_addr */
  433. dma_async_issue_pending(info->dma);
  434. /* configure and start prefetch transfer */
  435. ret = omap_prefetch_enable(info->gpmc_cs,
  436. PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
  437. if (ret)
  438. /* PFPW engine is busy, use cpu copy method */
  439. goto out_copy_unmap;
  440. wait_for_completion(&info->comp);
  441. tim = 0;
  442. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  443. do {
  444. cpu_relax();
  445. val = readl(info->reg.gpmc_prefetch_status);
  446. val = PREFETCH_STATUS_COUNT(val);
  447. } while (val && (tim++ < limit));
  448. /* disable and stop the PFPW engine */
  449. omap_prefetch_reset(info->gpmc_cs, info);
  450. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  451. return 0;
  452. out_copy_unmap:
  453. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  454. out_copy:
  455. if (info->nand.options & NAND_BUSWIDTH_16)
  456. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  457. : omap_write_buf16(mtd, (u_char *) addr, len);
  458. else
  459. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  460. : omap_write_buf8(mtd, (u_char *) addr, len);
  461. return 0;
  462. }
  463. /**
  464. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  465. * @chip: NAND chip object
  466. * @buf: buffer to store date
  467. * @len: number of bytes to read
  468. */
  469. static void omap_read_buf_dma_pref(struct nand_chip *chip, u_char *buf,
  470. int len)
  471. {
  472. struct mtd_info *mtd = nand_to_mtd(chip);
  473. if (len <= mtd->oobsize)
  474. omap_read_buf_pref(chip, buf, len);
  475. else
  476. /* start transfer in DMA mode */
  477. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  478. }
  479. /**
  480. * omap_write_buf_dma_pref - write buffer to NAND controller
  481. * @chip: NAND chip object
  482. * @buf: data buffer
  483. * @len: number of bytes to write
  484. */
  485. static void omap_write_buf_dma_pref(struct nand_chip *chip, const u_char *buf,
  486. int len)
  487. {
  488. struct mtd_info *mtd = nand_to_mtd(chip);
  489. if (len <= mtd->oobsize)
  490. omap_write_buf_pref(chip, 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.legacy.IO_ADDR_W, (u32 *)info->buf,
  515. bytes >> 2);
  516. info->buf = info->buf + bytes;
  517. info->buf_len -= bytes;
  518. } else {
  519. ioread32_rep(info->nand.legacy.IO_ADDR_R, (u32 *)info->buf,
  520. 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. * @chip: NAND chip object
  535. * @buf: buffer to store date
  536. * @len: number of bytes to read
  537. */
  538. static void omap_read_buf_irq_pref(struct nand_chip *chip, u_char *buf,
  539. int len)
  540. {
  541. struct mtd_info *mtd = nand_to_mtd(chip);
  542. struct omap_nand_info *info = mtd_to_omap(mtd);
  543. int ret = 0;
  544. if (len <= mtd->oobsize) {
  545. omap_read_buf_pref(chip, buf, len);
  546. return;
  547. }
  548. info->iomode = OMAP_NAND_IO_READ;
  549. info->buf = buf;
  550. init_completion(&info->comp);
  551. /* configure and start prefetch transfer */
  552. ret = omap_prefetch_enable(info->gpmc_cs,
  553. PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
  554. if (ret)
  555. /* PFPW engine is busy, use cpu copy method */
  556. goto out_copy;
  557. info->buf_len = len;
  558. enable_irq(info->gpmc_irq_count);
  559. enable_irq(info->gpmc_irq_fifo);
  560. /* waiting for read to complete */
  561. wait_for_completion(&info->comp);
  562. /* disable and stop the PFPW engine */
  563. omap_prefetch_reset(info->gpmc_cs, info);
  564. return;
  565. out_copy:
  566. if (info->nand.options & NAND_BUSWIDTH_16)
  567. omap_read_buf16(mtd, buf, len);
  568. else
  569. omap_read_buf8(mtd, buf, len);
  570. }
  571. /*
  572. * omap_write_buf_irq_pref - write buffer to NAND controller
  573. * @chip: NAND chip object
  574. * @buf: data buffer
  575. * @len: number of bytes to write
  576. */
  577. static void omap_write_buf_irq_pref(struct nand_chip *chip, const u_char *buf,
  578. int len)
  579. {
  580. struct mtd_info *mtd = nand_to_mtd(chip);
  581. struct omap_nand_info *info = mtd_to_omap(mtd);
  582. int ret = 0;
  583. unsigned long tim, limit;
  584. u32 val;
  585. if (len <= mtd->oobsize) {
  586. omap_write_buf_pref(chip, buf, len);
  587. return;
  588. }
  589. info->iomode = OMAP_NAND_IO_WRITE;
  590. info->buf = (u_char *) buf;
  591. init_completion(&info->comp);
  592. /* configure and start prefetch transfer : size=24 */
  593. ret = omap_prefetch_enable(info->gpmc_cs,
  594. (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
  595. if (ret)
  596. /* PFPW engine is busy, use cpu copy method */
  597. goto out_copy;
  598. info->buf_len = len;
  599. enable_irq(info->gpmc_irq_count);
  600. enable_irq(info->gpmc_irq_fifo);
  601. /* waiting for write to complete */
  602. wait_for_completion(&info->comp);
  603. /* wait for data to flushed-out before reset the prefetch */
  604. tim = 0;
  605. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  606. do {
  607. val = readl(info->reg.gpmc_prefetch_status);
  608. val = PREFETCH_STATUS_COUNT(val);
  609. cpu_relax();
  610. } while (val && (tim++ < limit));
  611. /* disable and stop the PFPW engine */
  612. omap_prefetch_reset(info->gpmc_cs, info);
  613. return;
  614. out_copy:
  615. if (info->nand.options & NAND_BUSWIDTH_16)
  616. omap_write_buf16(mtd, buf, len);
  617. else
  618. omap_write_buf8(mtd, buf, len);
  619. }
  620. /**
  621. * gen_true_ecc - This function will generate true ECC value
  622. * @ecc_buf: buffer to store ecc code
  623. *
  624. * This generated true ECC value can be used when correcting
  625. * data read from NAND flash memory core
  626. */
  627. static void gen_true_ecc(u8 *ecc_buf)
  628. {
  629. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  630. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  631. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  632. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  633. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  634. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  635. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  636. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  637. }
  638. /**
  639. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  640. * @ecc_data1: ecc code from nand spare area
  641. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  642. * @page_data: page data
  643. *
  644. * This function compares two ECC's and indicates if there is an error.
  645. * If the error can be corrected it will be corrected to the buffer.
  646. * If there is no error, %0 is returned. If there is an error but it
  647. * was corrected, %1 is returned. Otherwise, %-1 is returned.
  648. */
  649. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  650. u8 *ecc_data2, /* read from register */
  651. u8 *page_data)
  652. {
  653. uint i;
  654. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  655. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  656. u8 ecc_bit[24];
  657. u8 ecc_sum = 0;
  658. u8 find_bit = 0;
  659. uint find_byte = 0;
  660. int isEccFF;
  661. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  662. gen_true_ecc(ecc_data1);
  663. gen_true_ecc(ecc_data2);
  664. for (i = 0; i <= 2; i++) {
  665. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  666. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  667. }
  668. for (i = 0; i < 8; i++) {
  669. tmp0_bit[i] = *ecc_data1 % 2;
  670. *ecc_data1 = *ecc_data1 / 2;
  671. }
  672. for (i = 0; i < 8; i++) {
  673. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  674. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  675. }
  676. for (i = 0; i < 8; i++) {
  677. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  678. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  679. }
  680. for (i = 0; i < 8; i++) {
  681. comp0_bit[i] = *ecc_data2 % 2;
  682. *ecc_data2 = *ecc_data2 / 2;
  683. }
  684. for (i = 0; i < 8; i++) {
  685. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  686. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  687. }
  688. for (i = 0; i < 8; i++) {
  689. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  690. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  691. }
  692. for (i = 0; i < 6; i++)
  693. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  694. for (i = 0; i < 8; i++)
  695. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  696. for (i = 0; i < 8; i++)
  697. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  698. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  699. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  700. for (i = 0; i < 24; i++)
  701. ecc_sum += ecc_bit[i];
  702. switch (ecc_sum) {
  703. case 0:
  704. /* Not reached because this function is not called if
  705. * ECC values are equal
  706. */
  707. return 0;
  708. case 1:
  709. /* Uncorrectable error */
  710. pr_debug("ECC UNCORRECTED_ERROR 1\n");
  711. return -EBADMSG;
  712. case 11:
  713. /* UN-Correctable error */
  714. pr_debug("ECC UNCORRECTED_ERROR B\n");
  715. return -EBADMSG;
  716. case 12:
  717. /* Correctable error */
  718. find_byte = (ecc_bit[23] << 8) +
  719. (ecc_bit[21] << 7) +
  720. (ecc_bit[19] << 6) +
  721. (ecc_bit[17] << 5) +
  722. (ecc_bit[15] << 4) +
  723. (ecc_bit[13] << 3) +
  724. (ecc_bit[11] << 2) +
  725. (ecc_bit[9] << 1) +
  726. ecc_bit[7];
  727. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  728. pr_debug("Correcting single bit ECC error at offset: "
  729. "%d, bit: %d\n", find_byte, find_bit);
  730. page_data[find_byte] ^= (1 << find_bit);
  731. return 1;
  732. default:
  733. if (isEccFF) {
  734. if (ecc_data2[0] == 0 &&
  735. ecc_data2[1] == 0 &&
  736. ecc_data2[2] == 0)
  737. return 0;
  738. }
  739. pr_debug("UNCORRECTED_ERROR default\n");
  740. return -EBADMSG;
  741. }
  742. }
  743. /**
  744. * omap_correct_data - Compares the ECC read with HW generated ECC
  745. * @chip: NAND chip object
  746. * @dat: page data
  747. * @read_ecc: ecc read from nand flash
  748. * @calc_ecc: ecc read from HW ECC registers
  749. *
  750. * Compares the ecc read from nand spare area with ECC registers values
  751. * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
  752. * detection and correction. If there are no errors, %0 is returned. If
  753. * there were errors and all of the errors were corrected, the number of
  754. * corrected errors is returned. If uncorrectable errors exist, %-1 is
  755. * returned.
  756. */
  757. static int omap_correct_data(struct nand_chip *chip, u_char *dat,
  758. u_char *read_ecc, u_char *calc_ecc)
  759. {
  760. struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
  761. int blockCnt = 0, i = 0, ret = 0;
  762. int stat = 0;
  763. /* Ex NAND_ECC_HW12_2048 */
  764. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  765. (info->nand.ecc.size == 2048))
  766. blockCnt = 4;
  767. else
  768. blockCnt = 1;
  769. for (i = 0; i < blockCnt; i++) {
  770. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  771. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  772. if (ret < 0)
  773. return ret;
  774. /* keep track of the number of corrected errors */
  775. stat += ret;
  776. }
  777. read_ecc += 3;
  778. calc_ecc += 3;
  779. dat += 512;
  780. }
  781. return stat;
  782. }
  783. /**
  784. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  785. * @chip: NAND chip object
  786. * @dat: The pointer to data on which ecc is computed
  787. * @ecc_code: The ecc_code buffer
  788. *
  789. * Using noninverted ECC can be considered ugly since writing a blank
  790. * page ie. padding will clear the ECC bytes. This is no problem as long
  791. * nobody is trying to write data on the seemingly unused page. Reading
  792. * an erased page will produce an ECC mismatch between generated and read
  793. * ECC bytes that has to be dealt with separately.
  794. */
  795. static int omap_calculate_ecc(struct nand_chip *chip, const u_char *dat,
  796. u_char *ecc_code)
  797. {
  798. struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
  799. u32 val;
  800. val = readl(info->reg.gpmc_ecc_config);
  801. if (((val >> ECC_CONFIG_CS_SHIFT) & CS_MASK) != info->gpmc_cs)
  802. return -EINVAL;
  803. /* read ecc result */
  804. val = readl(info->reg.gpmc_ecc1_result);
  805. *ecc_code++ = val; /* P128e, ..., P1e */
  806. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  807. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  808. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  809. return 0;
  810. }
  811. /**
  812. * omap_enable_hwecc - This function enables the hardware ecc functionality
  813. * @mtd: MTD device structure
  814. * @mode: Read/Write mode
  815. */
  816. static void omap_enable_hwecc(struct nand_chip *chip, int mode)
  817. {
  818. struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
  819. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  820. u32 val;
  821. /* clear ecc and enable bits */
  822. val = ECCCLEAR | ECC1;
  823. writel(val, info->reg.gpmc_ecc_control);
  824. /* program ecc and result sizes */
  825. val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
  826. ECC1RESULTSIZE);
  827. writel(val, info->reg.gpmc_ecc_size_config);
  828. switch (mode) {
  829. case NAND_ECC_READ:
  830. case NAND_ECC_WRITE:
  831. writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
  832. break;
  833. case NAND_ECC_READSYN:
  834. writel(ECCCLEAR, info->reg.gpmc_ecc_control);
  835. break;
  836. default:
  837. dev_info(&info->pdev->dev,
  838. "error: unrecognized Mode[%d]!\n", mode);
  839. break;
  840. }
  841. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  842. val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
  843. writel(val, info->reg.gpmc_ecc_config);
  844. }
  845. /**
  846. * omap_wait - wait until the command is done
  847. * @this: NAND Chip structure
  848. *
  849. * Wait function is called during Program and erase operations and
  850. * the way it is called from MTD layer, we should wait till the NAND
  851. * chip is ready after the programming/erase operation has completed.
  852. *
  853. * Erase can take up to 400ms and program up to 20ms according to
  854. * general NAND and SmartMedia specs
  855. */
  856. static int omap_wait(struct nand_chip *this)
  857. {
  858. struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(this));
  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 nand_chip *chip)
  882. {
  883. struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
  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 nand_chip *chip,
  898. int mode)
  899. {
  900. unsigned int bch_type;
  901. unsigned int dev_width, nsectors;
  902. struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
  903. enum omap_ecc ecc_opt = info->ecc_opt;
  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. * @chip: NAND chip object
  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 nand_chip *chip,
  1119. const u_char *dat, u_char *ecc_calc)
  1120. {
  1121. return _omap_calculate_ecc_bch(nand_to_mtd(chip), 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. * @chip: NAND chip object
  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 nand_chip *chip, u_char *data,
  1193. u_char *read_ecc, u_char *calc_ecc)
  1194. {
  1195. struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
  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. * @chip: nand chip info structure
  1342. * @buf: data buffer
  1343. * @oob_required: must write chip->oob_poi to OOB
  1344. * @page: page
  1345. *
  1346. * Custom write page method evolved to support multi sector writing in one shot
  1347. */
  1348. static int omap_write_page_bch(struct nand_chip *chip, const uint8_t *buf,
  1349. int oob_required, int page)
  1350. {
  1351. struct mtd_info *mtd = nand_to_mtd(chip);
  1352. int ret;
  1353. uint8_t *ecc_calc = chip->ecc.calc_buf;
  1354. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1355. /* Enable GPMC ecc engine */
  1356. chip->ecc.hwctl(chip, NAND_ECC_WRITE);
  1357. /* Write data */
  1358. chip->legacy.write_buf(chip, buf, mtd->writesize);
  1359. /* Update ecc vector from GPMC result registers */
  1360. omap_calculate_ecc_bch_multi(mtd, buf, &ecc_calc[0]);
  1361. ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
  1362. chip->ecc.total);
  1363. if (ret)
  1364. return ret;
  1365. /* Write ecc vector to OOB area */
  1366. chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize);
  1367. return nand_prog_page_end_op(chip);
  1368. }
  1369. /**
  1370. * omap_write_subpage_bch - BCH hardware ECC based subpage write
  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 nand_chip *chip, u32 offset,
  1381. u32 data_len, const u8 *buf,
  1382. int oob_required, int page)
  1383. {
  1384. struct mtd_info *mtd = nand_to_mtd(chip);
  1385. u8 *ecc_calc = chip->ecc.calc_buf;
  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. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1399. /* Enable GPMC ECC engine */
  1400. chip->ecc.hwctl(chip, NAND_ECC_WRITE);
  1401. /* Write data */
  1402. chip->legacy.write_buf(chip, buf, mtd->writesize);
  1403. for (step = 0; step < ecc_steps; step++) {
  1404. /* mask ECC of un-touched subpages by padding 0xFF */
  1405. if (step < start_step || step > end_step)
  1406. memset(ecc_calc, 0xff, ecc_bytes);
  1407. else
  1408. ret = _omap_calculate_ecc_bch(mtd, buf, ecc_calc, step);
  1409. if (ret)
  1410. return ret;
  1411. buf += ecc_size;
  1412. ecc_calc += ecc_bytes;
  1413. }
  1414. /* copy calculated ECC for whole page to chip->buffer->oob */
  1415. /* this include masked-value(0xFF) for unwritten subpages */
  1416. ecc_calc = chip->ecc.calc_buf;
  1417. ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
  1418. chip->ecc.total);
  1419. if (ret)
  1420. return ret;
  1421. /* write OOB buffer to NAND device */
  1422. chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize);
  1423. return nand_prog_page_end_op(chip);
  1424. }
  1425. /**
  1426. * omap_read_page_bch - BCH ecc based page read function for entire page
  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 nand_chip *chip, uint8_t *buf,
  1440. int oob_required, int page)
  1441. {
  1442. struct mtd_info *mtd = nand_to_mtd(chip);
  1443. uint8_t *ecc_calc = chip->ecc.calc_buf;
  1444. uint8_t *ecc_code = chip->ecc.code_buf;
  1445. int stat, ret;
  1446. unsigned int max_bitflips = 0;
  1447. nand_read_page_op(chip, page, 0, NULL, 0);
  1448. /* Enable GPMC ecc engine */
  1449. chip->ecc.hwctl(chip, NAND_ECC_READ);
  1450. /* Read data */
  1451. chip->legacy.read_buf(chip, buf, mtd->writesize);
  1452. /* Read oob bytes */
  1453. nand_change_read_column_op(chip,
  1454. mtd->writesize + BADBLOCK_MARKER_LENGTH,
  1455. chip->oob_poi + BADBLOCK_MARKER_LENGTH,
  1456. chip->ecc.total, false);
  1457. /* Calculate ecc bytes */
  1458. omap_calculate_ecc_bch_multi(mtd, buf, ecc_calc);
  1459. ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
  1460. chip->ecc.total);
  1461. if (ret)
  1462. return ret;
  1463. stat = chip->ecc.correct(chip, buf, ecc_code, ecc_calc);
  1464. if (stat < 0) {
  1465. mtd->ecc_stats.failed++;
  1466. } else {
  1467. mtd->ecc_stats.corrected += stat;
  1468. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1469. }
  1470. return max_bitflips;
  1471. }
  1472. /**
  1473. * is_elm_present - checks for presence of ELM module by scanning DT nodes
  1474. * @omap_nand_info: NAND device structure containing platform data
  1475. */
  1476. static bool is_elm_present(struct omap_nand_info *info,
  1477. struct device_node *elm_node)
  1478. {
  1479. struct platform_device *pdev;
  1480. /* check whether elm-id is passed via DT */
  1481. if (!elm_node) {
  1482. dev_err(&info->pdev->dev, "ELM devicetree node not found\n");
  1483. return false;
  1484. }
  1485. pdev = of_find_device_by_node(elm_node);
  1486. /* check whether ELM device is registered */
  1487. if (!pdev) {
  1488. dev_err(&info->pdev->dev, "ELM device not found\n");
  1489. return false;
  1490. }
  1491. /* ELM module available, now configure it */
  1492. info->elm_dev = &pdev->dev;
  1493. return true;
  1494. }
  1495. static bool omap2_nand_ecc_check(struct omap_nand_info *info)
  1496. {
  1497. bool ecc_needs_bch, ecc_needs_omap_bch, ecc_needs_elm;
  1498. switch (info->ecc_opt) {
  1499. case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
  1500. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  1501. ecc_needs_omap_bch = false;
  1502. ecc_needs_bch = true;
  1503. ecc_needs_elm = false;
  1504. break;
  1505. case OMAP_ECC_BCH4_CODE_HW:
  1506. case OMAP_ECC_BCH8_CODE_HW:
  1507. case OMAP_ECC_BCH16_CODE_HW:
  1508. ecc_needs_omap_bch = true;
  1509. ecc_needs_bch = false;
  1510. ecc_needs_elm = true;
  1511. break;
  1512. default:
  1513. ecc_needs_omap_bch = false;
  1514. ecc_needs_bch = false;
  1515. ecc_needs_elm = false;
  1516. break;
  1517. }
  1518. if (ecc_needs_bch && !IS_ENABLED(CONFIG_MTD_NAND_ECC_BCH)) {
  1519. dev_err(&info->pdev->dev,
  1520. "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
  1521. return false;
  1522. }
  1523. if (ecc_needs_omap_bch && !IS_ENABLED(CONFIG_MTD_NAND_OMAP_BCH)) {
  1524. dev_err(&info->pdev->dev,
  1525. "CONFIG_MTD_NAND_OMAP_BCH not enabled\n");
  1526. return false;
  1527. }
  1528. if (ecc_needs_elm && !is_elm_present(info, info->elm_of_node)) {
  1529. dev_err(&info->pdev->dev, "ELM not available\n");
  1530. return false;
  1531. }
  1532. return true;
  1533. }
  1534. static const char * const nand_xfer_types[] = {
  1535. [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
  1536. [NAND_OMAP_POLLED] = "polled",
  1537. [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
  1538. [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
  1539. };
  1540. static int omap_get_dt_info(struct device *dev, struct omap_nand_info *info)
  1541. {
  1542. struct device_node *child = dev->of_node;
  1543. int i;
  1544. const char *s;
  1545. u32 cs;
  1546. if (of_property_read_u32(child, "reg", &cs) < 0) {
  1547. dev_err(dev, "reg not found in DT\n");
  1548. return -EINVAL;
  1549. }
  1550. info->gpmc_cs = cs;
  1551. /* detect availability of ELM module. Won't be present pre-OMAP4 */
  1552. info->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
  1553. if (!info->elm_of_node) {
  1554. info->elm_of_node = of_parse_phandle(child, "elm_id", 0);
  1555. if (!info->elm_of_node)
  1556. dev_dbg(dev, "ti,elm-id not in DT\n");
  1557. }
  1558. /* select ecc-scheme for NAND */
  1559. if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
  1560. dev_err(dev, "ti,nand-ecc-opt not found\n");
  1561. return -EINVAL;
  1562. }
  1563. if (!strcmp(s, "sw")) {
  1564. info->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
  1565. } else if (!strcmp(s, "ham1") ||
  1566. !strcmp(s, "hw") || !strcmp(s, "hw-romcode")) {
  1567. info->ecc_opt = OMAP_ECC_HAM1_CODE_HW;
  1568. } else if (!strcmp(s, "bch4")) {
  1569. if (info->elm_of_node)
  1570. info->ecc_opt = OMAP_ECC_BCH4_CODE_HW;
  1571. else
  1572. info->ecc_opt = OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
  1573. } else if (!strcmp(s, "bch8")) {
  1574. if (info->elm_of_node)
  1575. info->ecc_opt = OMAP_ECC_BCH8_CODE_HW;
  1576. else
  1577. info->ecc_opt = OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
  1578. } else if (!strcmp(s, "bch16")) {
  1579. info->ecc_opt = OMAP_ECC_BCH16_CODE_HW;
  1580. } else {
  1581. dev_err(dev, "unrecognized value for ti,nand-ecc-opt\n");
  1582. return -EINVAL;
  1583. }
  1584. /* select data transfer mode */
  1585. if (!of_property_read_string(child, "ti,nand-xfer-type", &s)) {
  1586. for (i = 0; i < ARRAY_SIZE(nand_xfer_types); i++) {
  1587. if (!strcasecmp(s, nand_xfer_types[i])) {
  1588. info->xfer_type = i;
  1589. return 0;
  1590. }
  1591. }
  1592. dev_err(dev, "unrecognized value for ti,nand-xfer-type\n");
  1593. return -EINVAL;
  1594. }
  1595. return 0;
  1596. }
  1597. static int omap_ooblayout_ecc(struct mtd_info *mtd, int section,
  1598. struct mtd_oob_region *oobregion)
  1599. {
  1600. struct omap_nand_info *info = mtd_to_omap(mtd);
  1601. struct nand_chip *chip = &info->nand;
  1602. int off = BADBLOCK_MARKER_LENGTH;
  1603. if (info->ecc_opt == OMAP_ECC_HAM1_CODE_HW &&
  1604. !(chip->options & NAND_BUSWIDTH_16))
  1605. off = 1;
  1606. if (section)
  1607. return -ERANGE;
  1608. oobregion->offset = off;
  1609. oobregion->length = chip->ecc.total;
  1610. return 0;
  1611. }
  1612. static int omap_ooblayout_free(struct mtd_info *mtd, int section,
  1613. struct mtd_oob_region *oobregion)
  1614. {
  1615. struct omap_nand_info *info = mtd_to_omap(mtd);
  1616. struct nand_chip *chip = &info->nand;
  1617. int off = BADBLOCK_MARKER_LENGTH;
  1618. if (info->ecc_opt == OMAP_ECC_HAM1_CODE_HW &&
  1619. !(chip->options & NAND_BUSWIDTH_16))
  1620. off = 1;
  1621. if (section)
  1622. return -ERANGE;
  1623. off += chip->ecc.total;
  1624. if (off >= mtd->oobsize)
  1625. return -ERANGE;
  1626. oobregion->offset = off;
  1627. oobregion->length = mtd->oobsize - off;
  1628. return 0;
  1629. }
  1630. static const struct mtd_ooblayout_ops omap_ooblayout_ops = {
  1631. .ecc = omap_ooblayout_ecc,
  1632. .free = omap_ooblayout_free,
  1633. };
  1634. static int omap_sw_ooblayout_ecc(struct mtd_info *mtd, int section,
  1635. struct mtd_oob_region *oobregion)
  1636. {
  1637. struct nand_chip *chip = mtd_to_nand(mtd);
  1638. int off = BADBLOCK_MARKER_LENGTH;
  1639. if (section >= chip->ecc.steps)
  1640. return -ERANGE;
  1641. /*
  1642. * When SW correction is employed, one OMAP specific marker byte is
  1643. * reserved after each ECC step.
  1644. */
  1645. oobregion->offset = off + (section * (chip->ecc.bytes + 1));
  1646. oobregion->length = chip->ecc.bytes;
  1647. return 0;
  1648. }
  1649. static int omap_sw_ooblayout_free(struct mtd_info *mtd, int section,
  1650. struct mtd_oob_region *oobregion)
  1651. {
  1652. struct nand_chip *chip = mtd_to_nand(mtd);
  1653. int off = BADBLOCK_MARKER_LENGTH;
  1654. if (section)
  1655. return -ERANGE;
  1656. /*
  1657. * When SW correction is employed, one OMAP specific marker byte is
  1658. * reserved after each ECC step.
  1659. */
  1660. off += ((chip->ecc.bytes + 1) * chip->ecc.steps);
  1661. if (off >= mtd->oobsize)
  1662. return -ERANGE;
  1663. oobregion->offset = off;
  1664. oobregion->length = mtd->oobsize - off;
  1665. return 0;
  1666. }
  1667. static const struct mtd_ooblayout_ops omap_sw_ooblayout_ops = {
  1668. .ecc = omap_sw_ooblayout_ecc,
  1669. .free = omap_sw_ooblayout_free,
  1670. };
  1671. static int omap_nand_attach_chip(struct nand_chip *chip)
  1672. {
  1673. struct mtd_info *mtd = nand_to_mtd(chip);
  1674. struct omap_nand_info *info = mtd_to_omap(mtd);
  1675. struct device *dev = &info->pdev->dev;
  1676. int min_oobbytes = BADBLOCK_MARKER_LENGTH;
  1677. int oobbytes_per_step;
  1678. dma_cap_mask_t mask;
  1679. int err;
  1680. if (chip->bbt_options & NAND_BBT_USE_FLASH)
  1681. chip->bbt_options |= NAND_BBT_NO_OOB;
  1682. else
  1683. chip->options |= NAND_SKIP_BBTSCAN;
  1684. /* Re-populate low-level callbacks based on xfer modes */
  1685. switch (info->xfer_type) {
  1686. case NAND_OMAP_PREFETCH_POLLED:
  1687. chip->legacy.read_buf = omap_read_buf_pref;
  1688. chip->legacy.write_buf = omap_write_buf_pref;
  1689. break;
  1690. case NAND_OMAP_POLLED:
  1691. /* Use nand_base defaults for {read,write}_buf */
  1692. break;
  1693. case NAND_OMAP_PREFETCH_DMA:
  1694. dma_cap_zero(mask);
  1695. dma_cap_set(DMA_SLAVE, mask);
  1696. info->dma = dma_request_chan(dev, "rxtx");
  1697. if (IS_ERR(info->dma)) {
  1698. dev_err(dev, "DMA engine request failed\n");
  1699. return PTR_ERR(info->dma);
  1700. } else {
  1701. struct dma_slave_config cfg;
  1702. memset(&cfg, 0, sizeof(cfg));
  1703. cfg.src_addr = info->phys_base;
  1704. cfg.dst_addr = info->phys_base;
  1705. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1706. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1707. cfg.src_maxburst = 16;
  1708. cfg.dst_maxburst = 16;
  1709. err = dmaengine_slave_config(info->dma, &cfg);
  1710. if (err) {
  1711. dev_err(dev,
  1712. "DMA engine slave config failed: %d\n",
  1713. err);
  1714. return err;
  1715. }
  1716. chip->legacy.read_buf = omap_read_buf_dma_pref;
  1717. chip->legacy.write_buf = omap_write_buf_dma_pref;
  1718. }
  1719. break;
  1720. case NAND_OMAP_PREFETCH_IRQ:
  1721. info->gpmc_irq_fifo = platform_get_irq(info->pdev, 0);
  1722. if (info->gpmc_irq_fifo <= 0) {
  1723. dev_err(dev, "Error getting fifo IRQ\n");
  1724. return -ENODEV;
  1725. }
  1726. err = devm_request_irq(dev, info->gpmc_irq_fifo,
  1727. omap_nand_irq, IRQF_SHARED,
  1728. "gpmc-nand-fifo", info);
  1729. if (err) {
  1730. dev_err(dev, "Requesting IRQ %d, error %d\n",
  1731. info->gpmc_irq_fifo, err);
  1732. info->gpmc_irq_fifo = 0;
  1733. return err;
  1734. }
  1735. info->gpmc_irq_count = platform_get_irq(info->pdev, 1);
  1736. if (info->gpmc_irq_count <= 0) {
  1737. dev_err(dev, "Error getting IRQ count\n");
  1738. return -ENODEV;
  1739. }
  1740. err = devm_request_irq(dev, info->gpmc_irq_count,
  1741. omap_nand_irq, IRQF_SHARED,
  1742. "gpmc-nand-count", info);
  1743. if (err) {
  1744. dev_err(dev, "Requesting IRQ %d, error %d\n",
  1745. info->gpmc_irq_count, err);
  1746. info->gpmc_irq_count = 0;
  1747. return err;
  1748. }
  1749. chip->legacy.read_buf = omap_read_buf_irq_pref;
  1750. chip->legacy.write_buf = omap_write_buf_irq_pref;
  1751. break;
  1752. default:
  1753. dev_err(dev, "xfer_type %d not supported!\n", info->xfer_type);
  1754. return -EINVAL;
  1755. }
  1756. if (!omap2_nand_ecc_check(info))
  1757. return -EINVAL;
  1758. /*
  1759. * Bail out earlier to let NAND_ECC_SOFT code create its own
  1760. * ooblayout instead of using ours.
  1761. */
  1762. if (info->ecc_opt == OMAP_ECC_HAM1_CODE_SW) {
  1763. chip->ecc.mode = NAND_ECC_SOFT;
  1764. chip->ecc.algo = NAND_ECC_HAMMING;
  1765. return 0;
  1766. }
  1767. /* Populate MTD interface based on ECC scheme */
  1768. switch (info->ecc_opt) {
  1769. case OMAP_ECC_HAM1_CODE_HW:
  1770. dev_info(dev, "nand: using OMAP_ECC_HAM1_CODE_HW\n");
  1771. chip->ecc.mode = NAND_ECC_HW;
  1772. chip->ecc.bytes = 3;
  1773. chip->ecc.size = 512;
  1774. chip->ecc.strength = 1;
  1775. chip->ecc.calculate = omap_calculate_ecc;
  1776. chip->ecc.hwctl = omap_enable_hwecc;
  1777. chip->ecc.correct = omap_correct_data;
  1778. mtd_set_ooblayout(mtd, &omap_ooblayout_ops);
  1779. oobbytes_per_step = chip->ecc.bytes;
  1780. if (!(chip->options & NAND_BUSWIDTH_16))
  1781. min_oobbytes = 1;
  1782. break;
  1783. case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
  1784. pr_info("nand: using OMAP_ECC_BCH4_CODE_HW_DETECTION_SW\n");
  1785. chip->ecc.mode = NAND_ECC_HW;
  1786. chip->ecc.size = 512;
  1787. chip->ecc.bytes = 7;
  1788. chip->ecc.strength = 4;
  1789. chip->ecc.hwctl = omap_enable_hwecc_bch;
  1790. chip->ecc.correct = nand_bch_correct_data;
  1791. chip->ecc.calculate = omap_calculate_ecc_bch_sw;
  1792. mtd_set_ooblayout(mtd, &omap_sw_ooblayout_ops);
  1793. /* Reserve one byte for the OMAP marker */
  1794. oobbytes_per_step = chip->ecc.bytes + 1;
  1795. /* Software BCH library is used for locating errors */
  1796. chip->ecc.priv = nand_bch_init(mtd);
  1797. if (!chip->ecc.priv) {
  1798. dev_err(dev, "Unable to use BCH library\n");
  1799. return -EINVAL;
  1800. }
  1801. break;
  1802. case OMAP_ECC_BCH4_CODE_HW:
  1803. pr_info("nand: using OMAP_ECC_BCH4_CODE_HW ECC scheme\n");
  1804. chip->ecc.mode = NAND_ECC_HW;
  1805. chip->ecc.size = 512;
  1806. /* 14th bit is kept reserved for ROM-code compatibility */
  1807. chip->ecc.bytes = 7 + 1;
  1808. chip->ecc.strength = 4;
  1809. chip->ecc.hwctl = omap_enable_hwecc_bch;
  1810. chip->ecc.correct = omap_elm_correct_data;
  1811. chip->ecc.read_page = omap_read_page_bch;
  1812. chip->ecc.write_page = omap_write_page_bch;
  1813. chip->ecc.write_subpage = omap_write_subpage_bch;
  1814. mtd_set_ooblayout(mtd, &omap_ooblayout_ops);
  1815. oobbytes_per_step = chip->ecc.bytes;
  1816. err = elm_config(info->elm_dev, BCH4_ECC,
  1817. mtd->writesize / chip->ecc.size,
  1818. chip->ecc.size, chip->ecc.bytes);
  1819. if (err < 0)
  1820. return err;
  1821. break;
  1822. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  1823. pr_info("nand: using OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
  1824. chip->ecc.mode = NAND_ECC_HW;
  1825. chip->ecc.size = 512;
  1826. chip->ecc.bytes = 13;
  1827. chip->ecc.strength = 8;
  1828. chip->ecc.hwctl = omap_enable_hwecc_bch;
  1829. chip->ecc.correct = nand_bch_correct_data;
  1830. chip->ecc.calculate = omap_calculate_ecc_bch_sw;
  1831. mtd_set_ooblayout(mtd, &omap_sw_ooblayout_ops);
  1832. /* Reserve one byte for the OMAP marker */
  1833. oobbytes_per_step = chip->ecc.bytes + 1;
  1834. /* Software BCH library is used for locating errors */
  1835. chip->ecc.priv = nand_bch_init(mtd);
  1836. if (!chip->ecc.priv) {
  1837. dev_err(dev, "unable to use BCH library\n");
  1838. return -EINVAL;
  1839. }
  1840. break;
  1841. case OMAP_ECC_BCH8_CODE_HW:
  1842. pr_info("nand: using OMAP_ECC_BCH8_CODE_HW ECC scheme\n");
  1843. chip->ecc.mode = NAND_ECC_HW;
  1844. chip->ecc.size = 512;
  1845. /* 14th bit is kept reserved for ROM-code compatibility */
  1846. chip->ecc.bytes = 13 + 1;
  1847. chip->ecc.strength = 8;
  1848. chip->ecc.hwctl = omap_enable_hwecc_bch;
  1849. chip->ecc.correct = omap_elm_correct_data;
  1850. chip->ecc.read_page = omap_read_page_bch;
  1851. chip->ecc.write_page = omap_write_page_bch;
  1852. chip->ecc.write_subpage = omap_write_subpage_bch;
  1853. mtd_set_ooblayout(mtd, &omap_ooblayout_ops);
  1854. oobbytes_per_step = chip->ecc.bytes;
  1855. err = elm_config(info->elm_dev, BCH8_ECC,
  1856. mtd->writesize / chip->ecc.size,
  1857. chip->ecc.size, chip->ecc.bytes);
  1858. if (err < 0)
  1859. return err;
  1860. break;
  1861. case OMAP_ECC_BCH16_CODE_HW:
  1862. pr_info("Using OMAP_ECC_BCH16_CODE_HW ECC scheme\n");
  1863. chip->ecc.mode = NAND_ECC_HW;
  1864. chip->ecc.size = 512;
  1865. chip->ecc.bytes = 26;
  1866. chip->ecc.strength = 16;
  1867. chip->ecc.hwctl = omap_enable_hwecc_bch;
  1868. chip->ecc.correct = omap_elm_correct_data;
  1869. chip->ecc.read_page = omap_read_page_bch;
  1870. chip->ecc.write_page = omap_write_page_bch;
  1871. chip->ecc.write_subpage = omap_write_subpage_bch;
  1872. mtd_set_ooblayout(mtd, &omap_ooblayout_ops);
  1873. oobbytes_per_step = chip->ecc.bytes;
  1874. err = elm_config(info->elm_dev, BCH16_ECC,
  1875. mtd->writesize / chip->ecc.size,
  1876. chip->ecc.size, chip->ecc.bytes);
  1877. if (err < 0)
  1878. return err;
  1879. break;
  1880. default:
  1881. dev_err(dev, "Invalid or unsupported ECC scheme\n");
  1882. return -EINVAL;
  1883. }
  1884. /* Check if NAND device's OOB is enough to store ECC signatures */
  1885. min_oobbytes += (oobbytes_per_step *
  1886. (mtd->writesize / chip->ecc.size));
  1887. if (mtd->oobsize < min_oobbytes) {
  1888. dev_err(dev,
  1889. "Not enough OOB bytes: required = %d, available=%d\n",
  1890. min_oobbytes, mtd->oobsize);
  1891. return -EINVAL;
  1892. }
  1893. return 0;
  1894. }
  1895. static const struct nand_controller_ops omap_nand_controller_ops = {
  1896. .attach_chip = omap_nand_attach_chip,
  1897. };
  1898. /* Shared among all NAND instances to synchronize access to the ECC Engine */
  1899. static struct nand_controller omap_gpmc_controller = {
  1900. .lock = __SPIN_LOCK_UNLOCKED(omap_gpmc_controller.lock),
  1901. .wq = __WAIT_QUEUE_HEAD_INITIALIZER(omap_gpmc_controller.wq),
  1902. .ops = &omap_nand_controller_ops,
  1903. };
  1904. static int omap_nand_probe(struct platform_device *pdev)
  1905. {
  1906. struct omap_nand_info *info;
  1907. struct mtd_info *mtd;
  1908. struct nand_chip *nand_chip;
  1909. int err;
  1910. struct resource *res;
  1911. struct device *dev = &pdev->dev;
  1912. info = devm_kzalloc(&pdev->dev, sizeof(struct omap_nand_info),
  1913. GFP_KERNEL);
  1914. if (!info)
  1915. return -ENOMEM;
  1916. info->pdev = pdev;
  1917. err = omap_get_dt_info(dev, info);
  1918. if (err)
  1919. return err;
  1920. info->ops = gpmc_omap_get_nand_ops(&info->reg, info->gpmc_cs);
  1921. if (!info->ops) {
  1922. dev_err(&pdev->dev, "Failed to get GPMC->NAND interface\n");
  1923. return -ENODEV;
  1924. }
  1925. nand_chip = &info->nand;
  1926. mtd = nand_to_mtd(nand_chip);
  1927. mtd->dev.parent = &pdev->dev;
  1928. nand_chip->ecc.priv = NULL;
  1929. nand_set_flash_node(nand_chip, dev->of_node);
  1930. if (!mtd->name) {
  1931. mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  1932. "omap2-nand.%d", info->gpmc_cs);
  1933. if (!mtd->name) {
  1934. dev_err(&pdev->dev, "Failed to set MTD name\n");
  1935. return -ENOMEM;
  1936. }
  1937. }
  1938. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1939. nand_chip->legacy.IO_ADDR_R = devm_ioremap_resource(&pdev->dev, res);
  1940. if (IS_ERR(nand_chip->legacy.IO_ADDR_R))
  1941. return PTR_ERR(nand_chip->legacy.IO_ADDR_R);
  1942. info->phys_base = res->start;
  1943. nand_chip->controller = &omap_gpmc_controller;
  1944. nand_chip->legacy.IO_ADDR_W = nand_chip->legacy.IO_ADDR_R;
  1945. nand_chip->legacy.cmd_ctrl = omap_hwcontrol;
  1946. info->ready_gpiod = devm_gpiod_get_optional(&pdev->dev, "rb",
  1947. GPIOD_IN);
  1948. if (IS_ERR(info->ready_gpiod)) {
  1949. dev_err(dev, "failed to get ready gpio\n");
  1950. return PTR_ERR(info->ready_gpiod);
  1951. }
  1952. /*
  1953. * If RDY/BSY line is connected to OMAP then use the omap ready
  1954. * function and the generic nand_wait function which reads the status
  1955. * register after monitoring the RDY/BSY line. Otherwise use a standard
  1956. * chip delay which is slightly more than tR (AC Timing) of the NAND
  1957. * device and read status register until you get a failure or success
  1958. */
  1959. if (info->ready_gpiod) {
  1960. nand_chip->legacy.dev_ready = omap_dev_ready;
  1961. nand_chip->legacy.chip_delay = 0;
  1962. } else {
  1963. nand_chip->legacy.waitfunc = omap_wait;
  1964. nand_chip->legacy.chip_delay = 50;
  1965. }
  1966. if (info->flash_bbt)
  1967. nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
  1968. /* scan NAND device connected to chip controller */
  1969. nand_chip->options |= info->devsize & NAND_BUSWIDTH_16;
  1970. err = nand_scan(nand_chip, 1);
  1971. if (err)
  1972. goto return_error;
  1973. err = mtd_device_register(mtd, NULL, 0);
  1974. if (err)
  1975. goto cleanup_nand;
  1976. platform_set_drvdata(pdev, mtd);
  1977. return 0;
  1978. cleanup_nand:
  1979. nand_cleanup(nand_chip);
  1980. return_error:
  1981. if (!IS_ERR_OR_NULL(info->dma))
  1982. dma_release_channel(info->dma);
  1983. if (nand_chip->ecc.priv) {
  1984. nand_bch_free(nand_chip->ecc.priv);
  1985. nand_chip->ecc.priv = NULL;
  1986. }
  1987. return err;
  1988. }
  1989. static int omap_nand_remove(struct platform_device *pdev)
  1990. {
  1991. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1992. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  1993. struct omap_nand_info *info = mtd_to_omap(mtd);
  1994. if (nand_chip->ecc.priv) {
  1995. nand_bch_free(nand_chip->ecc.priv);
  1996. nand_chip->ecc.priv = NULL;
  1997. }
  1998. if (info->dma)
  1999. dma_release_channel(info->dma);
  2000. nand_release(nand_chip);
  2001. return 0;
  2002. }
  2003. static const struct of_device_id omap_nand_ids[] = {
  2004. { .compatible = "ti,omap2-nand", },
  2005. {},
  2006. };
  2007. MODULE_DEVICE_TABLE(of, omap_nand_ids);
  2008. static struct platform_driver omap_nand_driver = {
  2009. .probe = omap_nand_probe,
  2010. .remove = omap_nand_remove,
  2011. .driver = {
  2012. .name = DRIVER_NAME,
  2013. .of_match_table = of_match_ptr(omap_nand_ids),
  2014. },
  2015. };
  2016. module_platform_driver(omap_nand_driver);
  2017. MODULE_ALIAS("platform:" DRIVER_NAME);
  2018. MODULE_LICENSE("GPL");
  2019. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");