omap2.c 59 KB

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