omap2.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  25. #include <linux/bch.h>
  26. #endif
  27. #include <linux/platform_data/mtd-nand-omap2.h>
  28. #define DRIVER_NAME "omap2-nand"
  29. #define OMAP_NAND_TIMEOUT_MS 5000
  30. #define NAND_Ecc_P1e (1 << 0)
  31. #define NAND_Ecc_P2e (1 << 1)
  32. #define NAND_Ecc_P4e (1 << 2)
  33. #define NAND_Ecc_P8e (1 << 3)
  34. #define NAND_Ecc_P16e (1 << 4)
  35. #define NAND_Ecc_P32e (1 << 5)
  36. #define NAND_Ecc_P64e (1 << 6)
  37. #define NAND_Ecc_P128e (1 << 7)
  38. #define NAND_Ecc_P256e (1 << 8)
  39. #define NAND_Ecc_P512e (1 << 9)
  40. #define NAND_Ecc_P1024e (1 << 10)
  41. #define NAND_Ecc_P2048e (1 << 11)
  42. #define NAND_Ecc_P1o (1 << 16)
  43. #define NAND_Ecc_P2o (1 << 17)
  44. #define NAND_Ecc_P4o (1 << 18)
  45. #define NAND_Ecc_P8o (1 << 19)
  46. #define NAND_Ecc_P16o (1 << 20)
  47. #define NAND_Ecc_P32o (1 << 21)
  48. #define NAND_Ecc_P64o (1 << 22)
  49. #define NAND_Ecc_P128o (1 << 23)
  50. #define NAND_Ecc_P256o (1 << 24)
  51. #define NAND_Ecc_P512o (1 << 25)
  52. #define NAND_Ecc_P1024o (1 << 26)
  53. #define NAND_Ecc_P2048o (1 << 27)
  54. #define TF(value) (value ? 1 : 0)
  55. #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
  56. #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
  57. #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
  58. #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
  59. #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
  60. #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
  61. #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
  62. #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
  63. #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
  64. #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
  65. #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
  66. #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
  67. #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
  68. #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
  69. #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
  70. #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
  71. #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
  72. #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
  73. #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
  74. #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
  75. #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
  76. #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
  77. #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
  78. #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
  79. #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
  80. #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
  81. #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
  82. #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
  83. #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
  84. #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
  85. #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
  86. #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
  87. #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
  88. #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
  89. #define PREFETCH_CONFIG1_CS_SHIFT 24
  90. #define ECC_CONFIG_CS_SHIFT 1
  91. #define CS_MASK 0x7
  92. #define ENABLE_PREFETCH (0x1 << 7)
  93. #define DMA_MPU_MODE_SHIFT 2
  94. #define ECCSIZE0_SHIFT 12
  95. #define ECCSIZE1_SHIFT 22
  96. #define ECC1RESULTSIZE 0x1
  97. #define ECCCLEAR 0x100
  98. #define ECC1 0x1
  99. #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
  100. #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
  101. #define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
  102. #define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
  103. #define STATUS_BUFF_EMPTY 0x00000001
  104. #define OMAP24XX_DMA_GPMC 4
  105. /* oob info generated runtime depending on ecc algorithm and layout selected */
  106. static struct nand_ecclayout omap_oobinfo;
  107. /* Define some generic bad / good block scan pattern which are used
  108. * while scanning a device for factory marked good / bad blocks
  109. */
  110. static uint8_t scan_ff_pattern[] = { 0xff };
  111. static struct nand_bbt_descr bb_descrip_flashbased = {
  112. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  113. .offs = 0,
  114. .len = 1,
  115. .pattern = scan_ff_pattern,
  116. };
  117. struct omap_nand_info {
  118. struct nand_hw_control controller;
  119. struct omap_nand_platform_data *pdata;
  120. struct mtd_info mtd;
  121. struct nand_chip nand;
  122. struct platform_device *pdev;
  123. int gpmc_cs;
  124. unsigned long phys_base;
  125. unsigned long mem_size;
  126. struct completion comp;
  127. struct dma_chan *dma;
  128. int gpmc_irq_fifo;
  129. int gpmc_irq_count;
  130. enum {
  131. OMAP_NAND_IO_READ = 0, /* read */
  132. OMAP_NAND_IO_WRITE, /* write */
  133. } iomode;
  134. u_char *buf;
  135. int buf_len;
  136. struct gpmc_nand_regs reg;
  137. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  138. struct bch_control *bch;
  139. struct nand_ecclayout ecclayout;
  140. #endif
  141. };
  142. /**
  143. * omap_prefetch_enable - configures and starts prefetch transfer
  144. * @cs: cs (chip select) number
  145. * @fifo_th: fifo threshold to be used for read/ write
  146. * @dma_mode: dma mode enable (1) or disable (0)
  147. * @u32_count: number of bytes to be transferred
  148. * @is_write: prefetch read(0) or write post(1) mode
  149. */
  150. static int omap_prefetch_enable(int cs, int fifo_th, int dma_mode,
  151. unsigned int u32_count, int is_write, struct omap_nand_info *info)
  152. {
  153. u32 val;
  154. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
  155. return -1;
  156. if (readl(info->reg.gpmc_prefetch_control))
  157. return -EBUSY;
  158. /* Set the amount of bytes to be prefetched */
  159. writel(u32_count, info->reg.gpmc_prefetch_config2);
  160. /* Set dma/mpu mode, the prefetch read / post write and
  161. * enable the engine. Set which cs is has requested for.
  162. */
  163. val = ((cs << PREFETCH_CONFIG1_CS_SHIFT) |
  164. PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH |
  165. (dma_mode << DMA_MPU_MODE_SHIFT) | (0x1 & is_write));
  166. writel(val, info->reg.gpmc_prefetch_config1);
  167. /* Start the prefetch engine */
  168. writel(0x1, info->reg.gpmc_prefetch_control);
  169. return 0;
  170. }
  171. /**
  172. * omap_prefetch_reset - disables and stops the prefetch engine
  173. */
  174. static int omap_prefetch_reset(int cs, struct omap_nand_info *info)
  175. {
  176. u32 config1;
  177. /* check if the same module/cs is trying to reset */
  178. config1 = readl(info->reg.gpmc_prefetch_config1);
  179. if (((config1 >> PREFETCH_CONFIG1_CS_SHIFT) & CS_MASK) != cs)
  180. return -EINVAL;
  181. /* Stop the PFPW engine */
  182. writel(0x0, info->reg.gpmc_prefetch_control);
  183. /* Reset/disable the PFPW engine */
  184. writel(0x0, info->reg.gpmc_prefetch_config1);
  185. return 0;
  186. }
  187. /**
  188. * omap_hwcontrol - hardware specific access to control-lines
  189. * @mtd: MTD device structure
  190. * @cmd: command to device
  191. * @ctrl:
  192. * NAND_NCE: bit 0 -> don't care
  193. * NAND_CLE: bit 1 -> Command Latch
  194. * NAND_ALE: bit 2 -> Address Latch
  195. *
  196. * NOTE: boards may use different bits for these!!
  197. */
  198. static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  199. {
  200. struct omap_nand_info *info = container_of(mtd,
  201. struct omap_nand_info, mtd);
  202. if (cmd != NAND_CMD_NONE) {
  203. if (ctrl & NAND_CLE)
  204. writeb(cmd, info->reg.gpmc_nand_command);
  205. else if (ctrl & NAND_ALE)
  206. writeb(cmd, info->reg.gpmc_nand_address);
  207. else /* NAND_NCE */
  208. writeb(cmd, info->reg.gpmc_nand_data);
  209. }
  210. }
  211. /**
  212. * omap_read_buf8 - read data from NAND controller into buffer
  213. * @mtd: MTD device structure
  214. * @buf: buffer to store date
  215. * @len: number of bytes to read
  216. */
  217. static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
  218. {
  219. struct nand_chip *nand = mtd->priv;
  220. ioread8_rep(nand->IO_ADDR_R, buf, len);
  221. }
  222. /**
  223. * omap_write_buf8 - write buffer to NAND controller
  224. * @mtd: MTD device structure
  225. * @buf: data buffer
  226. * @len: number of bytes to write
  227. */
  228. static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
  229. {
  230. struct omap_nand_info *info = container_of(mtd,
  231. struct omap_nand_info, mtd);
  232. u_char *p = (u_char *)buf;
  233. u32 status = 0;
  234. while (len--) {
  235. iowrite8(*p++, info->nand.IO_ADDR_W);
  236. /* wait until buffer is available for write */
  237. do {
  238. status = readl(info->reg.gpmc_status) &
  239. STATUS_BUFF_EMPTY;
  240. } while (!status);
  241. }
  242. }
  243. /**
  244. * omap_read_buf16 - read data from NAND controller into buffer
  245. * @mtd: MTD device structure
  246. * @buf: buffer to store date
  247. * @len: number of bytes to read
  248. */
  249. static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  250. {
  251. struct nand_chip *nand = mtd->priv;
  252. ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
  253. }
  254. /**
  255. * omap_write_buf16 - write buffer to NAND controller
  256. * @mtd: MTD device structure
  257. * @buf: data buffer
  258. * @len: number of bytes to write
  259. */
  260. static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
  261. {
  262. struct omap_nand_info *info = container_of(mtd,
  263. struct omap_nand_info, mtd);
  264. u16 *p = (u16 *) buf;
  265. u32 status = 0;
  266. /* FIXME try bursts of writesw() or DMA ... */
  267. len >>= 1;
  268. while (len--) {
  269. iowrite16(*p++, info->nand.IO_ADDR_W);
  270. /* wait until buffer is available for write */
  271. do {
  272. status = readl(info->reg.gpmc_status) &
  273. STATUS_BUFF_EMPTY;
  274. } while (!status);
  275. }
  276. }
  277. /**
  278. * omap_read_buf_pref - read data from NAND controller into buffer
  279. * @mtd: MTD device structure
  280. * @buf: buffer to store date
  281. * @len: number of bytes to read
  282. */
  283. static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
  284. {
  285. struct omap_nand_info *info = container_of(mtd,
  286. struct omap_nand_info, mtd);
  287. uint32_t r_count = 0;
  288. int ret = 0;
  289. u32 *p = (u32 *)buf;
  290. /* take care of subpage reads */
  291. if (len % 4) {
  292. if (info->nand.options & NAND_BUSWIDTH_16)
  293. omap_read_buf16(mtd, buf, len % 4);
  294. else
  295. omap_read_buf8(mtd, buf, len % 4);
  296. p = (u32 *) (buf + len % 4);
  297. len -= len % 4;
  298. }
  299. /* configure and start prefetch transfer */
  300. ret = omap_prefetch_enable(info->gpmc_cs,
  301. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0, info);
  302. if (ret) {
  303. /* PFPW engine is busy, use cpu copy method */
  304. if (info->nand.options & NAND_BUSWIDTH_16)
  305. omap_read_buf16(mtd, (u_char *)p, len);
  306. else
  307. omap_read_buf8(mtd, (u_char *)p, len);
  308. } else {
  309. do {
  310. r_count = readl(info->reg.gpmc_prefetch_status);
  311. r_count = PREFETCH_STATUS_FIFO_CNT(r_count);
  312. r_count = r_count >> 2;
  313. ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
  314. p += r_count;
  315. len -= r_count << 2;
  316. } while (len);
  317. /* disable and stop the PFPW engine */
  318. omap_prefetch_reset(info->gpmc_cs, info);
  319. }
  320. }
  321. /**
  322. * omap_write_buf_pref - write buffer to NAND controller
  323. * @mtd: MTD device structure
  324. * @buf: data buffer
  325. * @len: number of bytes to write
  326. */
  327. static void omap_write_buf_pref(struct mtd_info *mtd,
  328. const u_char *buf, int len)
  329. {
  330. struct omap_nand_info *info = container_of(mtd,
  331. struct omap_nand_info, mtd);
  332. uint32_t w_count = 0;
  333. int i = 0, ret = 0;
  334. u16 *p = (u16 *)buf;
  335. unsigned long tim, limit;
  336. u32 val;
  337. /* take care of subpage writes */
  338. if (len % 2 != 0) {
  339. writeb(*buf, info->nand.IO_ADDR_W);
  340. p = (u16 *)(buf + 1);
  341. len--;
  342. }
  343. /* configure and start prefetch transfer */
  344. ret = omap_prefetch_enable(info->gpmc_cs,
  345. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
  346. if (ret) {
  347. /* PFPW engine is busy, use cpu copy method */
  348. if (info->nand.options & NAND_BUSWIDTH_16)
  349. omap_write_buf16(mtd, (u_char *)p, len);
  350. else
  351. omap_write_buf8(mtd, (u_char *)p, len);
  352. } else {
  353. while (len) {
  354. w_count = readl(info->reg.gpmc_prefetch_status);
  355. w_count = PREFETCH_STATUS_FIFO_CNT(w_count);
  356. w_count = w_count >> 1;
  357. for (i = 0; (i < w_count) && len; i++, len -= 2)
  358. iowrite16(*p++, info->nand.IO_ADDR_W);
  359. }
  360. /* wait for data to flushed-out before reset the prefetch */
  361. tim = 0;
  362. limit = (loops_per_jiffy *
  363. msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  364. do {
  365. cpu_relax();
  366. val = readl(info->reg.gpmc_prefetch_status);
  367. val = PREFETCH_STATUS_COUNT(val);
  368. } while (val && (tim++ < limit));
  369. /* disable and stop the PFPW engine */
  370. omap_prefetch_reset(info->gpmc_cs, info);
  371. }
  372. }
  373. /*
  374. * omap_nand_dma_callback: callback on the completion of dma transfer
  375. * @data: pointer to completion data structure
  376. */
  377. static void omap_nand_dma_callback(void *data)
  378. {
  379. complete((struct completion *) data);
  380. }
  381. /*
  382. * omap_nand_dma_transfer: configure and start dma transfer
  383. * @mtd: MTD device structure
  384. * @addr: virtual address in RAM of source/destination
  385. * @len: number of data bytes to be transferred
  386. * @is_write: flag for read/write operation
  387. */
  388. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  389. unsigned int len, int is_write)
  390. {
  391. struct omap_nand_info *info = container_of(mtd,
  392. struct omap_nand_info, mtd);
  393. struct dma_async_tx_descriptor *tx;
  394. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  395. DMA_FROM_DEVICE;
  396. struct scatterlist sg;
  397. unsigned long tim, limit;
  398. unsigned n;
  399. int ret;
  400. u32 val;
  401. if (addr >= high_memory) {
  402. struct page *p1;
  403. if (((size_t)addr & PAGE_MASK) !=
  404. ((size_t)(addr + len - 1) & PAGE_MASK))
  405. goto out_copy;
  406. p1 = vmalloc_to_page(addr);
  407. if (!p1)
  408. goto out_copy;
  409. addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
  410. }
  411. sg_init_one(&sg, addr, len);
  412. n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
  413. if (n == 0) {
  414. dev_err(&info->pdev->dev,
  415. "Couldn't DMA map a %d byte buffer\n", len);
  416. goto out_copy;
  417. }
  418. tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
  419. is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  420. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  421. if (!tx)
  422. goto out_copy_unmap;
  423. tx->callback = omap_nand_dma_callback;
  424. tx->callback_param = &info->comp;
  425. dmaengine_submit(tx);
  426. /* configure and start prefetch transfer */
  427. ret = omap_prefetch_enable(info->gpmc_cs,
  428. PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
  429. if (ret)
  430. /* PFPW engine is busy, use cpu copy method */
  431. goto out_copy_unmap;
  432. init_completion(&info->comp);
  433. dma_async_issue_pending(info->dma);
  434. /* setup and start DMA using dma_addr */
  435. wait_for_completion(&info->comp);
  436. tim = 0;
  437. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  438. do {
  439. cpu_relax();
  440. val = readl(info->reg.gpmc_prefetch_status);
  441. val = PREFETCH_STATUS_COUNT(val);
  442. } while (val && (tim++ < limit));
  443. /* disable and stop the PFPW engine */
  444. omap_prefetch_reset(info->gpmc_cs, info);
  445. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  446. return 0;
  447. out_copy_unmap:
  448. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  449. out_copy:
  450. if (info->nand.options & NAND_BUSWIDTH_16)
  451. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  452. : omap_write_buf16(mtd, (u_char *) addr, len);
  453. else
  454. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  455. : omap_write_buf8(mtd, (u_char *) addr, len);
  456. return 0;
  457. }
  458. /**
  459. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  460. * @mtd: MTD device structure
  461. * @buf: buffer to store date
  462. * @len: number of bytes to read
  463. */
  464. static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
  465. {
  466. if (len <= mtd->oobsize)
  467. omap_read_buf_pref(mtd, buf, len);
  468. else
  469. /* start transfer in DMA mode */
  470. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  471. }
  472. /**
  473. * omap_write_buf_dma_pref - write buffer to NAND controller
  474. * @mtd: MTD device structure
  475. * @buf: data buffer
  476. * @len: number of bytes to write
  477. */
  478. static void omap_write_buf_dma_pref(struct mtd_info *mtd,
  479. const u_char *buf, int len)
  480. {
  481. if (len <= mtd->oobsize)
  482. omap_write_buf_pref(mtd, buf, len);
  483. else
  484. /* start transfer in DMA mode */
  485. omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
  486. }
  487. /*
  488. * omap_nand_irq - GPMC irq handler
  489. * @this_irq: gpmc irq number
  490. * @dev: omap_nand_info structure pointer is passed here
  491. */
  492. static irqreturn_t omap_nand_irq(int this_irq, void *dev)
  493. {
  494. struct omap_nand_info *info = (struct omap_nand_info *) dev;
  495. u32 bytes;
  496. bytes = readl(info->reg.gpmc_prefetch_status);
  497. bytes = PREFETCH_STATUS_FIFO_CNT(bytes);
  498. bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
  499. if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
  500. if (this_irq == info->gpmc_irq_count)
  501. goto done;
  502. if (info->buf_len && (info->buf_len < bytes))
  503. bytes = info->buf_len;
  504. else if (!info->buf_len)
  505. bytes = 0;
  506. iowrite32_rep(info->nand.IO_ADDR_W,
  507. (u32 *)info->buf, bytes >> 2);
  508. info->buf = info->buf + bytes;
  509. info->buf_len -= bytes;
  510. } else {
  511. ioread32_rep(info->nand.IO_ADDR_R,
  512. (u32 *)info->buf, bytes >> 2);
  513. info->buf = info->buf + bytes;
  514. if (this_irq == info->gpmc_irq_count)
  515. goto done;
  516. }
  517. return IRQ_HANDLED;
  518. done:
  519. complete(&info->comp);
  520. disable_irq_nosync(info->gpmc_irq_fifo);
  521. disable_irq_nosync(info->gpmc_irq_count);
  522. return IRQ_HANDLED;
  523. }
  524. /*
  525. * omap_read_buf_irq_pref - read data from NAND controller into buffer
  526. * @mtd: MTD device structure
  527. * @buf: buffer to store date
  528. * @len: number of bytes to read
  529. */
  530. static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
  531. {
  532. struct omap_nand_info *info = container_of(mtd,
  533. struct omap_nand_info, mtd);
  534. int ret = 0;
  535. if (len <= mtd->oobsize) {
  536. omap_read_buf_pref(mtd, buf, len);
  537. return;
  538. }
  539. info->iomode = OMAP_NAND_IO_READ;
  540. info->buf = buf;
  541. init_completion(&info->comp);
  542. /* configure and start prefetch transfer */
  543. ret = omap_prefetch_enable(info->gpmc_cs,
  544. PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
  545. if (ret)
  546. /* PFPW engine is busy, use cpu copy method */
  547. goto out_copy;
  548. info->buf_len = len;
  549. enable_irq(info->gpmc_irq_count);
  550. enable_irq(info->gpmc_irq_fifo);
  551. /* waiting for read to complete */
  552. wait_for_completion(&info->comp);
  553. /* disable and stop the PFPW engine */
  554. omap_prefetch_reset(info->gpmc_cs, info);
  555. return;
  556. out_copy:
  557. if (info->nand.options & NAND_BUSWIDTH_16)
  558. omap_read_buf16(mtd, buf, len);
  559. else
  560. omap_read_buf8(mtd, buf, len);
  561. }
  562. /*
  563. * omap_write_buf_irq_pref - write buffer to NAND controller
  564. * @mtd: MTD device structure
  565. * @buf: data buffer
  566. * @len: number of bytes to write
  567. */
  568. static void omap_write_buf_irq_pref(struct mtd_info *mtd,
  569. const u_char *buf, int len)
  570. {
  571. struct omap_nand_info *info = container_of(mtd,
  572. struct omap_nand_info, mtd);
  573. int ret = 0;
  574. unsigned long tim, limit;
  575. u32 val;
  576. if (len <= mtd->oobsize) {
  577. omap_write_buf_pref(mtd, buf, len);
  578. return;
  579. }
  580. info->iomode = OMAP_NAND_IO_WRITE;
  581. info->buf = (u_char *) buf;
  582. init_completion(&info->comp);
  583. /* configure and start prefetch transfer : size=24 */
  584. ret = omap_prefetch_enable(info->gpmc_cs,
  585. (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
  586. if (ret)
  587. /* PFPW engine is busy, use cpu copy method */
  588. goto out_copy;
  589. info->buf_len = len;
  590. enable_irq(info->gpmc_irq_count);
  591. enable_irq(info->gpmc_irq_fifo);
  592. /* waiting for write to complete */
  593. wait_for_completion(&info->comp);
  594. /* wait for data to flushed-out before reset the prefetch */
  595. tim = 0;
  596. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  597. do {
  598. val = readl(info->reg.gpmc_prefetch_status);
  599. val = PREFETCH_STATUS_COUNT(val);
  600. cpu_relax();
  601. } while (val && (tim++ < limit));
  602. /* disable and stop the PFPW engine */
  603. omap_prefetch_reset(info->gpmc_cs, info);
  604. return;
  605. out_copy:
  606. if (info->nand.options & NAND_BUSWIDTH_16)
  607. omap_write_buf16(mtd, buf, len);
  608. else
  609. omap_write_buf8(mtd, buf, len);
  610. }
  611. /**
  612. * gen_true_ecc - This function will generate true ECC value
  613. * @ecc_buf: buffer to store ecc code
  614. *
  615. * This generated true ECC value can be used when correcting
  616. * data read from NAND flash memory core
  617. */
  618. static void gen_true_ecc(u8 *ecc_buf)
  619. {
  620. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  621. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  622. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  623. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  624. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  625. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  626. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  627. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  628. }
  629. /**
  630. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  631. * @ecc_data1: ecc code from nand spare area
  632. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  633. * @page_data: page data
  634. *
  635. * This function compares two ECC's and indicates if there is an error.
  636. * If the error can be corrected it will be corrected to the buffer.
  637. * If there is no error, %0 is returned. If there is an error but it
  638. * was corrected, %1 is returned. Otherwise, %-1 is returned.
  639. */
  640. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  641. u8 *ecc_data2, /* read from register */
  642. u8 *page_data)
  643. {
  644. uint i;
  645. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  646. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  647. u8 ecc_bit[24];
  648. u8 ecc_sum = 0;
  649. u8 find_bit = 0;
  650. uint find_byte = 0;
  651. int isEccFF;
  652. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  653. gen_true_ecc(ecc_data1);
  654. gen_true_ecc(ecc_data2);
  655. for (i = 0; i <= 2; i++) {
  656. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  657. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  658. }
  659. for (i = 0; i < 8; i++) {
  660. tmp0_bit[i] = *ecc_data1 % 2;
  661. *ecc_data1 = *ecc_data1 / 2;
  662. }
  663. for (i = 0; i < 8; i++) {
  664. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  665. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  666. }
  667. for (i = 0; i < 8; i++) {
  668. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  669. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  670. }
  671. for (i = 0; i < 8; i++) {
  672. comp0_bit[i] = *ecc_data2 % 2;
  673. *ecc_data2 = *ecc_data2 / 2;
  674. }
  675. for (i = 0; i < 8; i++) {
  676. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  677. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  678. }
  679. for (i = 0; i < 8; i++) {
  680. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  681. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  682. }
  683. for (i = 0; i < 6; i++)
  684. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  685. for (i = 0; i < 8; i++)
  686. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  687. for (i = 0; i < 8; i++)
  688. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  689. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  690. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  691. for (i = 0; i < 24; i++)
  692. ecc_sum += ecc_bit[i];
  693. switch (ecc_sum) {
  694. case 0:
  695. /* Not reached because this function is not called if
  696. * ECC values are equal
  697. */
  698. return 0;
  699. case 1:
  700. /* Uncorrectable error */
  701. pr_debug("ECC UNCORRECTED_ERROR 1\n");
  702. return -1;
  703. case 11:
  704. /* UN-Correctable error */
  705. pr_debug("ECC UNCORRECTED_ERROR B\n");
  706. return -1;
  707. case 12:
  708. /* Correctable error */
  709. find_byte = (ecc_bit[23] << 8) +
  710. (ecc_bit[21] << 7) +
  711. (ecc_bit[19] << 6) +
  712. (ecc_bit[17] << 5) +
  713. (ecc_bit[15] << 4) +
  714. (ecc_bit[13] << 3) +
  715. (ecc_bit[11] << 2) +
  716. (ecc_bit[9] << 1) +
  717. ecc_bit[7];
  718. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  719. pr_debug("Correcting single bit ECC error at offset: "
  720. "%d, bit: %d\n", find_byte, find_bit);
  721. page_data[find_byte] ^= (1 << find_bit);
  722. return 1;
  723. default:
  724. if (isEccFF) {
  725. if (ecc_data2[0] == 0 &&
  726. ecc_data2[1] == 0 &&
  727. ecc_data2[2] == 0)
  728. return 0;
  729. }
  730. pr_debug("UNCORRECTED_ERROR default\n");
  731. return -1;
  732. }
  733. }
  734. /**
  735. * omap_correct_data - Compares the ECC read with HW generated ECC
  736. * @mtd: MTD device structure
  737. * @dat: page data
  738. * @read_ecc: ecc read from nand flash
  739. * @calc_ecc: ecc read from HW ECC registers
  740. *
  741. * Compares the ecc read from nand spare area with ECC registers values
  742. * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
  743. * detection and correction. If there are no errors, %0 is returned. If
  744. * there were errors and all of the errors were corrected, the number of
  745. * corrected errors is returned. If uncorrectable errors exist, %-1 is
  746. * returned.
  747. */
  748. static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
  749. u_char *read_ecc, u_char *calc_ecc)
  750. {
  751. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  752. mtd);
  753. int blockCnt = 0, i = 0, ret = 0;
  754. int stat = 0;
  755. /* Ex NAND_ECC_HW12_2048 */
  756. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  757. (info->nand.ecc.size == 2048))
  758. blockCnt = 4;
  759. else
  760. blockCnt = 1;
  761. for (i = 0; i < blockCnt; i++) {
  762. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  763. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  764. if (ret < 0)
  765. return ret;
  766. /* keep track of the number of corrected errors */
  767. stat += ret;
  768. }
  769. read_ecc += 3;
  770. calc_ecc += 3;
  771. dat += 512;
  772. }
  773. return stat;
  774. }
  775. /**
  776. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  777. * @mtd: MTD device structure
  778. * @dat: The pointer to data on which ecc is computed
  779. * @ecc_code: The ecc_code buffer
  780. *
  781. * Using noninverted ECC can be considered ugly since writing a blank
  782. * page ie. padding will clear the ECC bytes. This is no problem as long
  783. * nobody is trying to write data on the seemingly unused page. Reading
  784. * an erased page will produce an ECC mismatch between generated and read
  785. * ECC bytes that has to be dealt with separately.
  786. */
  787. static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  788. u_char *ecc_code)
  789. {
  790. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  791. mtd);
  792. u32 val;
  793. val = readl(info->reg.gpmc_ecc_config);
  794. if (((val >> ECC_CONFIG_CS_SHIFT) & ~CS_MASK) != info->gpmc_cs)
  795. return -EINVAL;
  796. /* read ecc result */
  797. val = readl(info->reg.gpmc_ecc1_result);
  798. *ecc_code++ = val; /* P128e, ..., P1e */
  799. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  800. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  801. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  802. return 0;
  803. }
  804. /**
  805. * omap_enable_hwecc - This function enables the hardware ecc functionality
  806. * @mtd: MTD device structure
  807. * @mode: Read/Write mode
  808. */
  809. static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
  810. {
  811. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  812. mtd);
  813. struct nand_chip *chip = mtd->priv;
  814. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  815. u32 val;
  816. /* clear ecc and enable bits */
  817. val = ECCCLEAR | ECC1;
  818. writel(val, info->reg.gpmc_ecc_control);
  819. /* program ecc and result sizes */
  820. val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
  821. ECC1RESULTSIZE);
  822. writel(val, info->reg.gpmc_ecc_size_config);
  823. switch (mode) {
  824. case NAND_ECC_READ:
  825. case NAND_ECC_WRITE:
  826. writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
  827. break;
  828. case NAND_ECC_READSYN:
  829. writel(ECCCLEAR, info->reg.gpmc_ecc_control);
  830. break;
  831. default:
  832. dev_info(&info->pdev->dev,
  833. "error: unrecognized Mode[%d]!\n", mode);
  834. break;
  835. }
  836. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  837. val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
  838. writel(val, info->reg.gpmc_ecc_config);
  839. }
  840. /**
  841. * omap_wait - wait until the command is done
  842. * @mtd: MTD device structure
  843. * @chip: NAND Chip structure
  844. *
  845. * Wait function is called during Program and erase operations and
  846. * the way it is called from MTD layer, we should wait till the NAND
  847. * chip is ready after the programming/erase operation has completed.
  848. *
  849. * Erase can take up to 400ms and program up to 20ms according to
  850. * general NAND and SmartMedia specs
  851. */
  852. static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
  853. {
  854. struct nand_chip *this = mtd->priv;
  855. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  856. mtd);
  857. unsigned long timeo = jiffies;
  858. int status, state = this->state;
  859. if (state == FL_ERASING)
  860. timeo += (HZ * 400) / 1000;
  861. else
  862. timeo += (HZ * 20) / 1000;
  863. writeb(NAND_CMD_STATUS & 0xFF, info->reg.gpmc_nand_command);
  864. while (time_before(jiffies, timeo)) {
  865. status = readb(info->reg.gpmc_nand_data);
  866. if (status & NAND_STATUS_READY)
  867. break;
  868. cond_resched();
  869. }
  870. status = readb(info->reg.gpmc_nand_data);
  871. return status;
  872. }
  873. /**
  874. * omap_dev_ready - calls the platform specific dev_ready function
  875. * @mtd: MTD device structure
  876. */
  877. static int omap_dev_ready(struct mtd_info *mtd)
  878. {
  879. unsigned int val = 0;
  880. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  881. mtd);
  882. val = readl(info->reg.gpmc_status);
  883. if ((val & 0x100) == 0x100) {
  884. return 1;
  885. } else {
  886. return 0;
  887. }
  888. }
  889. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  890. /**
  891. * omap3_enable_hwecc_bch - Program OMAP3 GPMC to perform BCH ECC correction
  892. * @mtd: MTD device structure
  893. * @mode: Read/Write mode
  894. */
  895. static void omap3_enable_hwecc_bch(struct mtd_info *mtd, int mode)
  896. {
  897. int nerrors;
  898. unsigned int dev_width, nsectors;
  899. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  900. mtd);
  901. struct nand_chip *chip = mtd->priv;
  902. u32 val;
  903. nerrors = (info->nand.ecc.bytes == 13) ? 8 : 4;
  904. dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  905. nsectors = 1;
  906. /*
  907. * Program GPMC to perform correction on one 512-byte sector at a time.
  908. * Using 4 sectors at a time (i.e. ecc.size = 2048) is also possible and
  909. * gives a slight (5%) performance gain (but requires additional code).
  910. */
  911. writel(ECC1, info->reg.gpmc_ecc_control);
  912. /*
  913. * When using BCH, sector size is hardcoded to 512 bytes.
  914. * Here we are using wrapping mode 6 both for reading and writing, with:
  915. * size0 = 0 (no additional protected byte in spare area)
  916. * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
  917. */
  918. val = (32 << ECCSIZE1_SHIFT) | (0 << ECCSIZE0_SHIFT);
  919. writel(val, info->reg.gpmc_ecc_size_config);
  920. /* BCH configuration */
  921. val = ((1 << 16) | /* enable BCH */
  922. (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
  923. (0x06 << 8) | /* wrap mode = 6 */
  924. (dev_width << 7) | /* bus width */
  925. (((nsectors-1) & 0x7) << 4) | /* number of sectors */
  926. (info->gpmc_cs << 1) | /* ECC CS */
  927. (0x1)); /* enable ECC */
  928. writel(val, info->reg.gpmc_ecc_config);
  929. /* clear ecc and enable bits */
  930. writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
  931. }
  932. /**
  933. * omap3_calculate_ecc_bch4 - Generate 7 bytes of ECC bytes
  934. * @mtd: MTD device structure
  935. * @dat: The pointer to data on which ecc is computed
  936. * @ecc_code: The ecc_code buffer
  937. */
  938. static int omap3_calculate_ecc_bch4(struct mtd_info *mtd, const u_char *dat,
  939. u_char *ecc_code)
  940. {
  941. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  942. mtd);
  943. unsigned long nsectors, val1, val2;
  944. int i;
  945. nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
  946. for (i = 0; i < nsectors; i++) {
  947. /* Read hw-computed remainder */
  948. val1 = readl(info->reg.gpmc_bch_result0[i]);
  949. val2 = readl(info->reg.gpmc_bch_result1[i]);
  950. /*
  951. * Add constant polynomial to remainder, in order to get an ecc
  952. * sequence of 0xFFs for a buffer filled with 0xFFs; and
  953. * left-justify the resulting polynomial.
  954. */
  955. *ecc_code++ = 0x28 ^ ((val2 >> 12) & 0xFF);
  956. *ecc_code++ = 0x13 ^ ((val2 >> 4) & 0xFF);
  957. *ecc_code++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
  958. *ecc_code++ = 0x39 ^ ((val1 >> 20) & 0xFF);
  959. *ecc_code++ = 0x96 ^ ((val1 >> 12) & 0xFF);
  960. *ecc_code++ = 0xac ^ ((val1 >> 4) & 0xFF);
  961. *ecc_code++ = 0x7f ^ ((val1 & 0xF) << 4);
  962. }
  963. return 0;
  964. }
  965. /**
  966. * omap3_calculate_ecc_bch8 - Generate 13 bytes of ECC bytes
  967. * @mtd: MTD device structure
  968. * @dat: The pointer to data on which ecc is computed
  969. * @ecc_code: The ecc_code buffer
  970. */
  971. static int omap3_calculate_ecc_bch8(struct mtd_info *mtd, const u_char *dat,
  972. u_char *ecc_code)
  973. {
  974. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  975. mtd);
  976. unsigned long nsectors, val1, val2, val3, val4;
  977. int i;
  978. nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
  979. for (i = 0; i < nsectors; i++) {
  980. /* Read hw-computed remainder */
  981. val1 = readl(info->reg.gpmc_bch_result0[i]);
  982. val2 = readl(info->reg.gpmc_bch_result1[i]);
  983. val3 = readl(info->reg.gpmc_bch_result2[i]);
  984. val4 = readl(info->reg.gpmc_bch_result3[i]);
  985. /*
  986. * Add constant polynomial to remainder, in order to get an ecc
  987. * sequence of 0xFFs for a buffer filled with 0xFFs.
  988. */
  989. *ecc_code++ = 0xef ^ (val4 & 0xFF);
  990. *ecc_code++ = 0x51 ^ ((val3 >> 24) & 0xFF);
  991. *ecc_code++ = 0x2e ^ ((val3 >> 16) & 0xFF);
  992. *ecc_code++ = 0x09 ^ ((val3 >> 8) & 0xFF);
  993. *ecc_code++ = 0xed ^ (val3 & 0xFF);
  994. *ecc_code++ = 0x93 ^ ((val2 >> 24) & 0xFF);
  995. *ecc_code++ = 0x9a ^ ((val2 >> 16) & 0xFF);
  996. *ecc_code++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
  997. *ecc_code++ = 0x97 ^ (val2 & 0xFF);
  998. *ecc_code++ = 0x79 ^ ((val1 >> 24) & 0xFF);
  999. *ecc_code++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
  1000. *ecc_code++ = 0x24 ^ ((val1 >> 8) & 0xFF);
  1001. *ecc_code++ = 0xb5 ^ (val1 & 0xFF);
  1002. }
  1003. return 0;
  1004. }
  1005. /**
  1006. * omap3_correct_data_bch - Decode received data and correct errors
  1007. * @mtd: MTD device structure
  1008. * @data: page data
  1009. * @read_ecc: ecc read from nand flash
  1010. * @calc_ecc: ecc read from HW ECC registers
  1011. */
  1012. static int omap3_correct_data_bch(struct mtd_info *mtd, u_char *data,
  1013. u_char *read_ecc, u_char *calc_ecc)
  1014. {
  1015. int i, count;
  1016. /* cannot correct more than 8 errors */
  1017. unsigned int errloc[8];
  1018. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1019. mtd);
  1020. count = decode_bch(info->bch, NULL, 512, read_ecc, calc_ecc, NULL,
  1021. errloc);
  1022. if (count > 0) {
  1023. /* correct errors */
  1024. for (i = 0; i < count; i++) {
  1025. /* correct data only, not ecc bytes */
  1026. if (errloc[i] < 8*512)
  1027. data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
  1028. pr_debug("corrected bitflip %u\n", errloc[i]);
  1029. }
  1030. } else if (count < 0) {
  1031. pr_err("ecc unrecoverable error\n");
  1032. }
  1033. return count;
  1034. }
  1035. /**
  1036. * omap3_free_bch - Release BCH ecc resources
  1037. * @mtd: MTD device structure
  1038. */
  1039. static void omap3_free_bch(struct mtd_info *mtd)
  1040. {
  1041. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1042. mtd);
  1043. if (info->bch) {
  1044. free_bch(info->bch);
  1045. info->bch = NULL;
  1046. }
  1047. }
  1048. /**
  1049. * omap3_init_bch - Initialize BCH ECC
  1050. * @mtd: MTD device structure
  1051. * @ecc_opt: OMAP ECC mode (OMAP_ECC_BCH4_CODE_HW or OMAP_ECC_BCH8_CODE_HW)
  1052. */
  1053. static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
  1054. {
  1055. int max_errors;
  1056. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1057. mtd);
  1058. #ifdef CONFIG_MTD_NAND_OMAP_BCH8
  1059. const int hw_errors = 8;
  1060. #else
  1061. const int hw_errors = 4;
  1062. #endif
  1063. info->bch = NULL;
  1064. max_errors = (ecc_opt == OMAP_ECC_BCH8_CODE_HW) ? 8 : 4;
  1065. if (max_errors != hw_errors) {
  1066. pr_err("cannot configure %d-bit BCH ecc, only %d-bit supported",
  1067. max_errors, hw_errors);
  1068. goto fail;
  1069. }
  1070. /* software bch library is only used to detect and locate errors */
  1071. info->bch = init_bch(13, max_errors, 0x201b /* hw polynomial */);
  1072. if (!info->bch)
  1073. goto fail;
  1074. info->nand.ecc.size = 512;
  1075. info->nand.ecc.hwctl = omap3_enable_hwecc_bch;
  1076. info->nand.ecc.correct = omap3_correct_data_bch;
  1077. info->nand.ecc.mode = NAND_ECC_HW;
  1078. /*
  1079. * The number of corrected errors in an ecc block that will trigger
  1080. * block scrubbing defaults to the ecc strength (4 or 8).
  1081. * Set mtd->bitflip_threshold here to define a custom threshold.
  1082. */
  1083. if (max_errors == 8) {
  1084. info->nand.ecc.strength = 8;
  1085. info->nand.ecc.bytes = 13;
  1086. info->nand.ecc.calculate = omap3_calculate_ecc_bch8;
  1087. } else {
  1088. info->nand.ecc.strength = 4;
  1089. info->nand.ecc.bytes = 7;
  1090. info->nand.ecc.calculate = omap3_calculate_ecc_bch4;
  1091. }
  1092. pr_info("enabling NAND BCH ecc with %d-bit correction\n", max_errors);
  1093. return 0;
  1094. fail:
  1095. omap3_free_bch(mtd);
  1096. return -1;
  1097. }
  1098. /**
  1099. * omap3_init_bch_tail - Build an oob layout for BCH ECC correction.
  1100. * @mtd: MTD device structure
  1101. */
  1102. static int omap3_init_bch_tail(struct mtd_info *mtd)
  1103. {
  1104. int i, steps;
  1105. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1106. mtd);
  1107. struct nand_ecclayout *layout = &info->ecclayout;
  1108. /* build oob layout */
  1109. steps = mtd->writesize/info->nand.ecc.size;
  1110. layout->eccbytes = steps*info->nand.ecc.bytes;
  1111. /* do not bother creating special oob layouts for small page devices */
  1112. if (mtd->oobsize < 64) {
  1113. pr_err("BCH ecc is not supported on small page devices\n");
  1114. goto fail;
  1115. }
  1116. /* reserve 2 bytes for bad block marker */
  1117. if (layout->eccbytes+2 > mtd->oobsize) {
  1118. pr_err("no oob layout available for oobsize %d eccbytes %u\n",
  1119. mtd->oobsize, layout->eccbytes);
  1120. goto fail;
  1121. }
  1122. /* put ecc bytes at oob tail */
  1123. for (i = 0; i < layout->eccbytes; i++)
  1124. layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  1125. layout->oobfree[0].offset = 2;
  1126. layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  1127. info->nand.ecc.layout = layout;
  1128. if (!(info->nand.options & NAND_BUSWIDTH_16))
  1129. info->nand.badblock_pattern = &bb_descrip_flashbased;
  1130. return 0;
  1131. fail:
  1132. omap3_free_bch(mtd);
  1133. return -1;
  1134. }
  1135. #else
  1136. static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
  1137. {
  1138. pr_err("CONFIG_MTD_NAND_OMAP_BCH is not enabled\n");
  1139. return -1;
  1140. }
  1141. static int omap3_init_bch_tail(struct mtd_info *mtd)
  1142. {
  1143. return -1;
  1144. }
  1145. static void omap3_free_bch(struct mtd_info *mtd)
  1146. {
  1147. }
  1148. #endif /* CONFIG_MTD_NAND_OMAP_BCH */
  1149. static int omap_nand_probe(struct platform_device *pdev)
  1150. {
  1151. struct omap_nand_info *info;
  1152. struct omap_nand_platform_data *pdata;
  1153. int err;
  1154. int i, offset;
  1155. dma_cap_mask_t mask;
  1156. unsigned sig;
  1157. struct resource *res;
  1158. struct mtd_part_parser_data ppdata = {};
  1159. pdata = pdev->dev.platform_data;
  1160. if (pdata == NULL) {
  1161. dev_err(&pdev->dev, "platform data missing\n");
  1162. return -ENODEV;
  1163. }
  1164. info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
  1165. if (!info)
  1166. return -ENOMEM;
  1167. platform_set_drvdata(pdev, info);
  1168. spin_lock_init(&info->controller.lock);
  1169. init_waitqueue_head(&info->controller.wq);
  1170. info->pdev = pdev;
  1171. info->gpmc_cs = pdata->cs;
  1172. info->reg = pdata->reg;
  1173. info->mtd.priv = &info->nand;
  1174. info->mtd.name = dev_name(&pdev->dev);
  1175. info->mtd.owner = THIS_MODULE;
  1176. info->nand.options = pdata->devsize;
  1177. info->nand.options |= NAND_SKIP_BBTSCAN;
  1178. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1179. if (res == NULL) {
  1180. err = -EINVAL;
  1181. dev_err(&pdev->dev, "error getting memory resource\n");
  1182. goto out_free_info;
  1183. }
  1184. info->phys_base = res->start;
  1185. info->mem_size = resource_size(res);
  1186. if (!request_mem_region(info->phys_base, info->mem_size,
  1187. pdev->dev.driver->name)) {
  1188. err = -EBUSY;
  1189. goto out_free_info;
  1190. }
  1191. info->nand.IO_ADDR_R = ioremap(info->phys_base, info->mem_size);
  1192. if (!info->nand.IO_ADDR_R) {
  1193. err = -ENOMEM;
  1194. goto out_release_mem_region;
  1195. }
  1196. info->nand.controller = &info->controller;
  1197. info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
  1198. info->nand.cmd_ctrl = omap_hwcontrol;
  1199. /*
  1200. * If RDY/BSY line is connected to OMAP then use the omap ready
  1201. * function and the generic nand_wait function which reads the status
  1202. * register after monitoring the RDY/BSY line. Otherwise use a standard
  1203. * chip delay which is slightly more than tR (AC Timing) of the NAND
  1204. * device and read status register until you get a failure or success
  1205. */
  1206. if (pdata->dev_ready) {
  1207. info->nand.dev_ready = omap_dev_ready;
  1208. info->nand.chip_delay = 0;
  1209. } else {
  1210. info->nand.waitfunc = omap_wait;
  1211. info->nand.chip_delay = 50;
  1212. }
  1213. switch (pdata->xfer_type) {
  1214. case NAND_OMAP_PREFETCH_POLLED:
  1215. info->nand.read_buf = omap_read_buf_pref;
  1216. info->nand.write_buf = omap_write_buf_pref;
  1217. break;
  1218. case NAND_OMAP_POLLED:
  1219. if (info->nand.options & NAND_BUSWIDTH_16) {
  1220. info->nand.read_buf = omap_read_buf16;
  1221. info->nand.write_buf = omap_write_buf16;
  1222. } else {
  1223. info->nand.read_buf = omap_read_buf8;
  1224. info->nand.write_buf = omap_write_buf8;
  1225. }
  1226. break;
  1227. case NAND_OMAP_PREFETCH_DMA:
  1228. dma_cap_zero(mask);
  1229. dma_cap_set(DMA_SLAVE, mask);
  1230. sig = OMAP24XX_DMA_GPMC;
  1231. info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1232. if (!info->dma) {
  1233. dev_err(&pdev->dev, "DMA engine request failed\n");
  1234. err = -ENXIO;
  1235. goto out_release_mem_region;
  1236. } else {
  1237. struct dma_slave_config cfg;
  1238. memset(&cfg, 0, sizeof(cfg));
  1239. cfg.src_addr = info->phys_base;
  1240. cfg.dst_addr = info->phys_base;
  1241. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1242. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1243. cfg.src_maxburst = 16;
  1244. cfg.dst_maxburst = 16;
  1245. err = dmaengine_slave_config(info->dma, &cfg);
  1246. if (err) {
  1247. dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
  1248. err);
  1249. goto out_release_mem_region;
  1250. }
  1251. info->nand.read_buf = omap_read_buf_dma_pref;
  1252. info->nand.write_buf = omap_write_buf_dma_pref;
  1253. }
  1254. break;
  1255. case NAND_OMAP_PREFETCH_IRQ:
  1256. info->gpmc_irq_fifo = platform_get_irq(pdev, 0);
  1257. if (info->gpmc_irq_fifo <= 0) {
  1258. dev_err(&pdev->dev, "error getting fifo irq\n");
  1259. err = -ENODEV;
  1260. goto out_release_mem_region;
  1261. }
  1262. err = request_irq(info->gpmc_irq_fifo, omap_nand_irq,
  1263. IRQF_SHARED, "gpmc-nand-fifo", info);
  1264. if (err) {
  1265. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  1266. info->gpmc_irq_fifo, err);
  1267. info->gpmc_irq_fifo = 0;
  1268. goto out_release_mem_region;
  1269. }
  1270. info->gpmc_irq_count = platform_get_irq(pdev, 1);
  1271. if (info->gpmc_irq_count <= 0) {
  1272. dev_err(&pdev->dev, "error getting count irq\n");
  1273. err = -ENODEV;
  1274. goto out_release_mem_region;
  1275. }
  1276. err = request_irq(info->gpmc_irq_count, omap_nand_irq,
  1277. IRQF_SHARED, "gpmc-nand-count", info);
  1278. if (err) {
  1279. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  1280. info->gpmc_irq_count, err);
  1281. info->gpmc_irq_count = 0;
  1282. goto out_release_mem_region;
  1283. }
  1284. info->nand.read_buf = omap_read_buf_irq_pref;
  1285. info->nand.write_buf = omap_write_buf_irq_pref;
  1286. break;
  1287. default:
  1288. dev_err(&pdev->dev,
  1289. "xfer_type(%d) not supported!\n", pdata->xfer_type);
  1290. err = -EINVAL;
  1291. goto out_release_mem_region;
  1292. }
  1293. /* select the ecc type */
  1294. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
  1295. info->nand.ecc.mode = NAND_ECC_SOFT;
  1296. else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
  1297. (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
  1298. info->nand.ecc.bytes = 3;
  1299. info->nand.ecc.size = 512;
  1300. info->nand.ecc.strength = 1;
  1301. info->nand.ecc.calculate = omap_calculate_ecc;
  1302. info->nand.ecc.hwctl = omap_enable_hwecc;
  1303. info->nand.ecc.correct = omap_correct_data;
  1304. info->nand.ecc.mode = NAND_ECC_HW;
  1305. } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  1306. (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
  1307. err = omap3_init_bch(&info->mtd, pdata->ecc_opt);
  1308. if (err) {
  1309. err = -EINVAL;
  1310. goto out_release_mem_region;
  1311. }
  1312. }
  1313. /* DIP switches on some boards change between 8 and 16 bit
  1314. * bus widths for flash. Try the other width if the first try fails.
  1315. */
  1316. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  1317. info->nand.options ^= NAND_BUSWIDTH_16;
  1318. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  1319. err = -ENXIO;
  1320. goto out_release_mem_region;
  1321. }
  1322. }
  1323. /* rom code layout */
  1324. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
  1325. if (info->nand.options & NAND_BUSWIDTH_16)
  1326. offset = 2;
  1327. else {
  1328. offset = 1;
  1329. info->nand.badblock_pattern = &bb_descrip_flashbased;
  1330. }
  1331. omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
  1332. for (i = 0; i < omap_oobinfo.eccbytes; i++)
  1333. omap_oobinfo.eccpos[i] = i+offset;
  1334. omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
  1335. omap_oobinfo.oobfree->length = info->mtd.oobsize -
  1336. (offset + omap_oobinfo.eccbytes);
  1337. info->nand.ecc.layout = &omap_oobinfo;
  1338. } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  1339. (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
  1340. /* build OOB layout for BCH ECC correction */
  1341. err = omap3_init_bch_tail(&info->mtd);
  1342. if (err) {
  1343. err = -EINVAL;
  1344. goto out_release_mem_region;
  1345. }
  1346. }
  1347. /* second phase scan */
  1348. if (nand_scan_tail(&info->mtd)) {
  1349. err = -ENXIO;
  1350. goto out_release_mem_region;
  1351. }
  1352. ppdata.of_node = pdata->of_node;
  1353. mtd_device_parse_register(&info->mtd, NULL, &ppdata, pdata->parts,
  1354. pdata->nr_parts);
  1355. platform_set_drvdata(pdev, &info->mtd);
  1356. return 0;
  1357. out_release_mem_region:
  1358. if (info->dma)
  1359. dma_release_channel(info->dma);
  1360. if (info->gpmc_irq_count > 0)
  1361. free_irq(info->gpmc_irq_count, info);
  1362. if (info->gpmc_irq_fifo > 0)
  1363. free_irq(info->gpmc_irq_fifo, info);
  1364. release_mem_region(info->phys_base, info->mem_size);
  1365. out_free_info:
  1366. kfree(info);
  1367. return err;
  1368. }
  1369. static int omap_nand_remove(struct platform_device *pdev)
  1370. {
  1371. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1372. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1373. mtd);
  1374. omap3_free_bch(&info->mtd);
  1375. platform_set_drvdata(pdev, NULL);
  1376. if (info->dma)
  1377. dma_release_channel(info->dma);
  1378. if (info->gpmc_irq_count > 0)
  1379. free_irq(info->gpmc_irq_count, info);
  1380. if (info->gpmc_irq_fifo > 0)
  1381. free_irq(info->gpmc_irq_fifo, info);
  1382. /* Release NAND device, its internal structures and partitions */
  1383. nand_release(&info->mtd);
  1384. iounmap(info->nand.IO_ADDR_R);
  1385. release_mem_region(info->phys_base, info->mem_size);
  1386. kfree(info);
  1387. return 0;
  1388. }
  1389. static struct platform_driver omap_nand_driver = {
  1390. .probe = omap_nand_probe,
  1391. .remove = omap_nand_remove,
  1392. .driver = {
  1393. .name = DRIVER_NAME,
  1394. .owner = THIS_MODULE,
  1395. },
  1396. };
  1397. module_platform_driver(omap_nand_driver);
  1398. MODULE_ALIAS("platform:" DRIVER_NAME);
  1399. MODULE_LICENSE("GPL");
  1400. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");