omap2.c 57 KB

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