omap2.c 58 KB

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