omap2.c 63 KB

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