omap2.c 63 KB

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