atmel_nand.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284
  1. /*
  2. * Copyright © 2003 Rick Bronson
  3. *
  4. * Derived from drivers/mtd/nand/autcpu12.c
  5. * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
  6. *
  7. * Derived from drivers/mtd/spia.c
  8. * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
  9. *
  10. *
  11. * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
  12. * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
  13. *
  14. * Derived from Das U-Boot source code
  15. * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
  16. * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
  17. *
  18. * Add Programmable Multibit ECC support for various AT91 SoC
  19. * © Copyright 2012 ATMEL, Hong Xu
  20. *
  21. * Add Nand Flash Controller support for SAMA5 SoC
  22. * © Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License version 2 as
  26. * published by the Free Software Foundation.
  27. *
  28. */
  29. #include <linux/dma-mapping.h>
  30. #include <linux/slab.h>
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/of.h>
  35. #include <linux/of_device.h>
  36. #include <linux/of_gpio.h>
  37. #include <linux/of_mtd.h>
  38. #include <linux/mtd/mtd.h>
  39. #include <linux/mtd/nand.h>
  40. #include <linux/mtd/partitions.h>
  41. #include <linux/delay.h>
  42. #include <linux/dmaengine.h>
  43. #include <linux/gpio.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/io.h>
  46. #include <linux/platform_data/atmel.h>
  47. static int use_dma = 1;
  48. module_param(use_dma, int, 0);
  49. static int on_flash_bbt = 0;
  50. module_param(on_flash_bbt, int, 0);
  51. /* Register access macros */
  52. #define ecc_readl(add, reg) \
  53. __raw_readl(add + ATMEL_ECC_##reg)
  54. #define ecc_writel(add, reg, value) \
  55. __raw_writel((value), add + ATMEL_ECC_##reg)
  56. #include "atmel_nand_ecc.h" /* Hardware ECC registers */
  57. #include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
  58. /* oob layout for large page size
  59. * bad block info is on bytes 0 and 1
  60. * the bytes have to be consecutives to avoid
  61. * several NAND_CMD_RNDOUT during read
  62. */
  63. static struct nand_ecclayout atmel_oobinfo_large = {
  64. .eccbytes = 4,
  65. .eccpos = {60, 61, 62, 63},
  66. .oobfree = {
  67. {2, 58}
  68. },
  69. };
  70. /* oob layout for small page size
  71. * bad block info is on bytes 4 and 5
  72. * the bytes have to be consecutives to avoid
  73. * several NAND_CMD_RNDOUT during read
  74. */
  75. static struct nand_ecclayout atmel_oobinfo_small = {
  76. .eccbytes = 4,
  77. .eccpos = {0, 1, 2, 3},
  78. .oobfree = {
  79. {6, 10}
  80. },
  81. };
  82. struct atmel_nfc {
  83. void __iomem *base_cmd_regs;
  84. void __iomem *hsmc_regs;
  85. void __iomem *sram_bank0;
  86. dma_addr_t sram_bank0_phys;
  87. bool use_nfc_sram;
  88. bool write_by_sram;
  89. bool is_initialized;
  90. struct completion comp_nfc;
  91. /* Point to the sram bank which include readed data via NFC */
  92. void __iomem *data_in_sram;
  93. bool will_write_sram;
  94. };
  95. static struct atmel_nfc nand_nfc;
  96. struct atmel_nand_host {
  97. struct nand_chip nand_chip;
  98. struct mtd_info mtd;
  99. void __iomem *io_base;
  100. dma_addr_t io_phys;
  101. struct atmel_nand_data board;
  102. struct device *dev;
  103. void __iomem *ecc;
  104. struct completion comp;
  105. struct dma_chan *dma_chan;
  106. struct atmel_nfc *nfc;
  107. bool has_pmecc;
  108. u8 pmecc_corr_cap;
  109. u16 pmecc_sector_size;
  110. u32 pmecc_lookup_table_offset;
  111. u32 pmecc_lookup_table_offset_512;
  112. u32 pmecc_lookup_table_offset_1024;
  113. int pmecc_bytes_per_sector;
  114. int pmecc_sector_number;
  115. int pmecc_degree; /* Degree of remainders */
  116. int pmecc_cw_len; /* Length of codeword */
  117. void __iomem *pmerrloc_base;
  118. void __iomem *pmecc_rom_base;
  119. /* lookup table for alpha_to and index_of */
  120. void __iomem *pmecc_alpha_to;
  121. void __iomem *pmecc_index_of;
  122. /* data for pmecc computation */
  123. int16_t *pmecc_partial_syn;
  124. int16_t *pmecc_si;
  125. int16_t *pmecc_smu; /* Sigma table */
  126. int16_t *pmecc_lmu; /* polynomal order */
  127. int *pmecc_mu;
  128. int *pmecc_dmu;
  129. int *pmecc_delta;
  130. };
  131. static struct nand_ecclayout atmel_pmecc_oobinfo;
  132. /*
  133. * Enable NAND.
  134. */
  135. static void atmel_nand_enable(struct atmel_nand_host *host)
  136. {
  137. if (gpio_is_valid(host->board.enable_pin))
  138. gpio_set_value(host->board.enable_pin, 0);
  139. }
  140. /*
  141. * Disable NAND.
  142. */
  143. static void atmel_nand_disable(struct atmel_nand_host *host)
  144. {
  145. if (gpio_is_valid(host->board.enable_pin))
  146. gpio_set_value(host->board.enable_pin, 1);
  147. }
  148. /*
  149. * Hardware specific access to control-lines
  150. */
  151. static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  152. {
  153. struct nand_chip *nand_chip = mtd->priv;
  154. struct atmel_nand_host *host = nand_chip->priv;
  155. if (ctrl & NAND_CTRL_CHANGE) {
  156. if (ctrl & NAND_NCE)
  157. atmel_nand_enable(host);
  158. else
  159. atmel_nand_disable(host);
  160. }
  161. if (cmd == NAND_CMD_NONE)
  162. return;
  163. if (ctrl & NAND_CLE)
  164. writeb(cmd, host->io_base + (1 << host->board.cle));
  165. else
  166. writeb(cmd, host->io_base + (1 << host->board.ale));
  167. }
  168. /*
  169. * Read the Device Ready pin.
  170. */
  171. static int atmel_nand_device_ready(struct mtd_info *mtd)
  172. {
  173. struct nand_chip *nand_chip = mtd->priv;
  174. struct atmel_nand_host *host = nand_chip->priv;
  175. return gpio_get_value(host->board.rdy_pin) ^
  176. !!host->board.rdy_pin_active_low;
  177. }
  178. /* Set up for hardware ready pin and enable pin. */
  179. static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
  180. {
  181. struct nand_chip *chip = mtd->priv;
  182. struct atmel_nand_host *host = chip->priv;
  183. int res = 0;
  184. if (gpio_is_valid(host->board.rdy_pin)) {
  185. res = devm_gpio_request(host->dev,
  186. host->board.rdy_pin, "nand_rdy");
  187. if (res < 0) {
  188. dev_err(host->dev,
  189. "can't request rdy gpio %d\n",
  190. host->board.rdy_pin);
  191. return res;
  192. }
  193. res = gpio_direction_input(host->board.rdy_pin);
  194. if (res < 0) {
  195. dev_err(host->dev,
  196. "can't request input direction rdy gpio %d\n",
  197. host->board.rdy_pin);
  198. return res;
  199. }
  200. chip->dev_ready = atmel_nand_device_ready;
  201. }
  202. if (gpio_is_valid(host->board.enable_pin)) {
  203. res = devm_gpio_request(host->dev,
  204. host->board.enable_pin, "nand_enable");
  205. if (res < 0) {
  206. dev_err(host->dev,
  207. "can't request enable gpio %d\n",
  208. host->board.enable_pin);
  209. return res;
  210. }
  211. res = gpio_direction_output(host->board.enable_pin, 1);
  212. if (res < 0) {
  213. dev_err(host->dev,
  214. "can't request output direction enable gpio %d\n",
  215. host->board.enable_pin);
  216. return res;
  217. }
  218. }
  219. return res;
  220. }
  221. static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
  222. {
  223. int i;
  224. u32 *t = trg;
  225. const __iomem u32 *s = src;
  226. for (i = 0; i < (size >> 2); i++)
  227. *t++ = readl_relaxed(s++);
  228. }
  229. static void memcpy32_toio(void __iomem *trg, const void *src, int size)
  230. {
  231. int i;
  232. u32 __iomem *t = trg;
  233. const u32 *s = src;
  234. for (i = 0; i < (size >> 2); i++)
  235. writel_relaxed(*s++, t++);
  236. }
  237. /*
  238. * Minimal-overhead PIO for data access.
  239. */
  240. static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
  241. {
  242. struct nand_chip *nand_chip = mtd->priv;
  243. struct atmel_nand_host *host = nand_chip->priv;
  244. if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
  245. memcpy32_fromio(buf, host->nfc->data_in_sram, len);
  246. host->nfc->data_in_sram += len;
  247. } else {
  248. __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
  249. }
  250. }
  251. static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
  252. {
  253. struct nand_chip *nand_chip = mtd->priv;
  254. struct atmel_nand_host *host = nand_chip->priv;
  255. if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
  256. memcpy32_fromio(buf, host->nfc->data_in_sram, len);
  257. host->nfc->data_in_sram += len;
  258. } else {
  259. __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
  260. }
  261. }
  262. static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
  263. {
  264. struct nand_chip *nand_chip = mtd->priv;
  265. __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
  266. }
  267. static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
  268. {
  269. struct nand_chip *nand_chip = mtd->priv;
  270. __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
  271. }
  272. static void dma_complete_func(void *completion)
  273. {
  274. complete(completion);
  275. }
  276. static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
  277. {
  278. /* NFC only has two banks. Must be 0 or 1 */
  279. if (bank > 1)
  280. return -EINVAL;
  281. if (bank) {
  282. /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
  283. if (host->mtd.writesize > 2048)
  284. return -EINVAL;
  285. nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
  286. } else {
  287. nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
  288. }
  289. return 0;
  290. }
  291. static uint nfc_get_sram_off(struct atmel_nand_host *host)
  292. {
  293. if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
  294. return NFC_SRAM_BANK1_OFFSET;
  295. else
  296. return 0;
  297. }
  298. static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
  299. {
  300. if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
  301. return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
  302. else
  303. return host->nfc->sram_bank0_phys;
  304. }
  305. static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
  306. int is_read)
  307. {
  308. struct dma_device *dma_dev;
  309. enum dma_ctrl_flags flags;
  310. dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
  311. struct dma_async_tx_descriptor *tx = NULL;
  312. dma_cookie_t cookie;
  313. struct nand_chip *chip = mtd->priv;
  314. struct atmel_nand_host *host = chip->priv;
  315. void *p = buf;
  316. int err = -EIO;
  317. enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  318. struct atmel_nfc *nfc = host->nfc;
  319. if (buf >= high_memory)
  320. goto err_buf;
  321. dma_dev = host->dma_chan->device;
  322. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
  323. DMA_COMPL_SKIP_DEST_UNMAP;
  324. phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
  325. if (dma_mapping_error(dma_dev->dev, phys_addr)) {
  326. dev_err(host->dev, "Failed to dma_map_single\n");
  327. goto err_buf;
  328. }
  329. if (is_read) {
  330. if (nfc && nfc->data_in_sram)
  331. dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
  332. - (nfc->sram_bank0 + nfc_get_sram_off(host)));
  333. else
  334. dma_src_addr = host->io_phys;
  335. dma_dst_addr = phys_addr;
  336. } else {
  337. dma_src_addr = phys_addr;
  338. if (nfc && nfc->write_by_sram)
  339. dma_dst_addr = nfc_sram_phys(host);
  340. else
  341. dma_dst_addr = host->io_phys;
  342. }
  343. tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
  344. dma_src_addr, len, flags);
  345. if (!tx) {
  346. dev_err(host->dev, "Failed to prepare DMA memcpy\n");
  347. goto err_dma;
  348. }
  349. init_completion(&host->comp);
  350. tx->callback = dma_complete_func;
  351. tx->callback_param = &host->comp;
  352. cookie = tx->tx_submit(tx);
  353. if (dma_submit_error(cookie)) {
  354. dev_err(host->dev, "Failed to do DMA tx_submit\n");
  355. goto err_dma;
  356. }
  357. dma_async_issue_pending(host->dma_chan);
  358. wait_for_completion(&host->comp);
  359. if (is_read && nfc && nfc->data_in_sram)
  360. /* After read data from SRAM, need to increase the position */
  361. nfc->data_in_sram += len;
  362. err = 0;
  363. err_dma:
  364. dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
  365. err_buf:
  366. if (err != 0)
  367. dev_warn(host->dev, "Fall back to CPU I/O\n");
  368. return err;
  369. }
  370. static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  371. {
  372. struct nand_chip *chip = mtd->priv;
  373. struct atmel_nand_host *host = chip->priv;
  374. if (use_dma && len > mtd->oobsize)
  375. /* only use DMA for bigger than oob size: better performances */
  376. if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
  377. return;
  378. if (host->board.bus_width_16)
  379. atmel_read_buf16(mtd, buf, len);
  380. else
  381. atmel_read_buf8(mtd, buf, len);
  382. }
  383. static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  384. {
  385. struct nand_chip *chip = mtd->priv;
  386. struct atmel_nand_host *host = chip->priv;
  387. if (use_dma && len > mtd->oobsize)
  388. /* only use DMA for bigger than oob size: better performances */
  389. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
  390. return;
  391. if (host->board.bus_width_16)
  392. atmel_write_buf16(mtd, buf, len);
  393. else
  394. atmel_write_buf8(mtd, buf, len);
  395. }
  396. /*
  397. * Return number of ecc bytes per sector according to sector size and
  398. * correction capability
  399. *
  400. * Following table shows what at91 PMECC supported:
  401. * Correction Capability Sector_512_bytes Sector_1024_bytes
  402. * ===================== ================ =================
  403. * 2-bits 4-bytes 4-bytes
  404. * 4-bits 7-bytes 7-bytes
  405. * 8-bits 13-bytes 14-bytes
  406. * 12-bits 20-bytes 21-bytes
  407. * 24-bits 39-bytes 42-bytes
  408. */
  409. static int pmecc_get_ecc_bytes(int cap, int sector_size)
  410. {
  411. int m = 12 + sector_size / 512;
  412. return (m * cap + 7) / 8;
  413. }
  414. static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
  415. int oobsize, int ecc_len)
  416. {
  417. int i;
  418. layout->eccbytes = ecc_len;
  419. /* ECC will occupy the last ecc_len bytes continuously */
  420. for (i = 0; i < ecc_len; i++)
  421. layout->eccpos[i] = oobsize - ecc_len + i;
  422. layout->oobfree[0].offset = 2;
  423. layout->oobfree[0].length =
  424. oobsize - ecc_len - layout->oobfree[0].offset;
  425. }
  426. static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
  427. {
  428. int table_size;
  429. table_size = host->pmecc_sector_size == 512 ?
  430. PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
  431. return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
  432. table_size * sizeof(int16_t);
  433. }
  434. static int pmecc_data_alloc(struct atmel_nand_host *host)
  435. {
  436. const int cap = host->pmecc_corr_cap;
  437. int size;
  438. size = (2 * cap + 1) * sizeof(int16_t);
  439. host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
  440. host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
  441. host->pmecc_lmu = devm_kzalloc(host->dev,
  442. (cap + 1) * sizeof(int16_t), GFP_KERNEL);
  443. host->pmecc_smu = devm_kzalloc(host->dev,
  444. (cap + 2) * size, GFP_KERNEL);
  445. size = (cap + 1) * sizeof(int);
  446. host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
  447. host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
  448. host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
  449. if (!host->pmecc_partial_syn ||
  450. !host->pmecc_si ||
  451. !host->pmecc_lmu ||
  452. !host->pmecc_smu ||
  453. !host->pmecc_mu ||
  454. !host->pmecc_dmu ||
  455. !host->pmecc_delta)
  456. return -ENOMEM;
  457. return 0;
  458. }
  459. static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
  460. {
  461. struct nand_chip *nand_chip = mtd->priv;
  462. struct atmel_nand_host *host = nand_chip->priv;
  463. int i;
  464. uint32_t value;
  465. /* Fill odd syndromes */
  466. for (i = 0; i < host->pmecc_corr_cap; i++) {
  467. value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
  468. if (i & 1)
  469. value >>= 16;
  470. value &= 0xffff;
  471. host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
  472. }
  473. }
  474. static void pmecc_substitute(struct mtd_info *mtd)
  475. {
  476. struct nand_chip *nand_chip = mtd->priv;
  477. struct atmel_nand_host *host = nand_chip->priv;
  478. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  479. int16_t __iomem *index_of = host->pmecc_index_of;
  480. int16_t *partial_syn = host->pmecc_partial_syn;
  481. const int cap = host->pmecc_corr_cap;
  482. int16_t *si;
  483. int i, j;
  484. /* si[] is a table that holds the current syndrome value,
  485. * an element of that table belongs to the field
  486. */
  487. si = host->pmecc_si;
  488. memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
  489. /* Computation 2t syndromes based on S(x) */
  490. /* Odd syndromes */
  491. for (i = 1; i < 2 * cap; i += 2) {
  492. for (j = 0; j < host->pmecc_degree; j++) {
  493. if (partial_syn[i] & ((unsigned short)0x1 << j))
  494. si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
  495. }
  496. }
  497. /* Even syndrome = (Odd syndrome) ** 2 */
  498. for (i = 2, j = 1; j <= cap; i = ++j << 1) {
  499. if (si[j] == 0) {
  500. si[i] = 0;
  501. } else {
  502. int16_t tmp;
  503. tmp = readw_relaxed(index_of + si[j]);
  504. tmp = (tmp * 2) % host->pmecc_cw_len;
  505. si[i] = readw_relaxed(alpha_to + tmp);
  506. }
  507. }
  508. return;
  509. }
  510. static void pmecc_get_sigma(struct mtd_info *mtd)
  511. {
  512. struct nand_chip *nand_chip = mtd->priv;
  513. struct atmel_nand_host *host = nand_chip->priv;
  514. int16_t *lmu = host->pmecc_lmu;
  515. int16_t *si = host->pmecc_si;
  516. int *mu = host->pmecc_mu;
  517. int *dmu = host->pmecc_dmu; /* Discrepancy */
  518. int *delta = host->pmecc_delta; /* Delta order */
  519. int cw_len = host->pmecc_cw_len;
  520. const int16_t cap = host->pmecc_corr_cap;
  521. const int num = 2 * cap + 1;
  522. int16_t __iomem *index_of = host->pmecc_index_of;
  523. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  524. int i, j, k;
  525. uint32_t dmu_0_count, tmp;
  526. int16_t *smu = host->pmecc_smu;
  527. /* index of largest delta */
  528. int ro;
  529. int largest;
  530. int diff;
  531. dmu_0_count = 0;
  532. /* First Row */
  533. /* Mu */
  534. mu[0] = -1;
  535. memset(smu, 0, sizeof(int16_t) * num);
  536. smu[0] = 1;
  537. /* discrepancy set to 1 */
  538. dmu[0] = 1;
  539. /* polynom order set to 0 */
  540. lmu[0] = 0;
  541. delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
  542. /* Second Row */
  543. /* Mu */
  544. mu[1] = 0;
  545. /* Sigma(x) set to 1 */
  546. memset(&smu[num], 0, sizeof(int16_t) * num);
  547. smu[num] = 1;
  548. /* discrepancy set to S1 */
  549. dmu[1] = si[1];
  550. /* polynom order set to 0 */
  551. lmu[1] = 0;
  552. delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
  553. /* Init the Sigma(x) last row */
  554. memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
  555. for (i = 1; i <= cap; i++) {
  556. mu[i + 1] = i << 1;
  557. /* Begin Computing Sigma (Mu+1) and L(mu) */
  558. /* check if discrepancy is set to 0 */
  559. if (dmu[i] == 0) {
  560. dmu_0_count++;
  561. tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
  562. if ((cap - (lmu[i] >> 1) - 1) & 0x1)
  563. tmp += 2;
  564. else
  565. tmp += 1;
  566. if (dmu_0_count == tmp) {
  567. for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
  568. smu[(cap + 1) * num + j] =
  569. smu[i * num + j];
  570. lmu[cap + 1] = lmu[i];
  571. return;
  572. }
  573. /* copy polynom */
  574. for (j = 0; j <= lmu[i] >> 1; j++)
  575. smu[(i + 1) * num + j] = smu[i * num + j];
  576. /* copy previous polynom order to the next */
  577. lmu[i + 1] = lmu[i];
  578. } else {
  579. ro = 0;
  580. largest = -1;
  581. /* find largest delta with dmu != 0 */
  582. for (j = 0; j < i; j++) {
  583. if ((dmu[j]) && (delta[j] > largest)) {
  584. largest = delta[j];
  585. ro = j;
  586. }
  587. }
  588. /* compute difference */
  589. diff = (mu[i] - mu[ro]);
  590. /* Compute degree of the new smu polynomial */
  591. if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
  592. lmu[i + 1] = lmu[i];
  593. else
  594. lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
  595. /* Init smu[i+1] with 0 */
  596. for (k = 0; k < num; k++)
  597. smu[(i + 1) * num + k] = 0;
  598. /* Compute smu[i+1] */
  599. for (k = 0; k <= lmu[ro] >> 1; k++) {
  600. int16_t a, b, c;
  601. if (!(smu[ro * num + k] && dmu[i]))
  602. continue;
  603. a = readw_relaxed(index_of + dmu[i]);
  604. b = readw_relaxed(index_of + dmu[ro]);
  605. c = readw_relaxed(index_of + smu[ro * num + k]);
  606. tmp = a + (cw_len - b) + c;
  607. a = readw_relaxed(alpha_to + tmp % cw_len);
  608. smu[(i + 1) * num + (k + diff)] = a;
  609. }
  610. for (k = 0; k <= lmu[i] >> 1; k++)
  611. smu[(i + 1) * num + k] ^= smu[i * num + k];
  612. }
  613. /* End Computing Sigma (Mu+1) and L(mu) */
  614. /* In either case compute delta */
  615. delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
  616. /* Do not compute discrepancy for the last iteration */
  617. if (i >= cap)
  618. continue;
  619. for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
  620. tmp = 2 * (i - 1);
  621. if (k == 0) {
  622. dmu[i + 1] = si[tmp + 3];
  623. } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
  624. int16_t a, b, c;
  625. a = readw_relaxed(index_of +
  626. smu[(i + 1) * num + k]);
  627. b = si[2 * (i - 1) + 3 - k];
  628. c = readw_relaxed(index_of + b);
  629. tmp = a + c;
  630. tmp %= cw_len;
  631. dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
  632. dmu[i + 1];
  633. }
  634. }
  635. }
  636. return;
  637. }
  638. static int pmecc_err_location(struct mtd_info *mtd)
  639. {
  640. struct nand_chip *nand_chip = mtd->priv;
  641. struct atmel_nand_host *host = nand_chip->priv;
  642. unsigned long end_time;
  643. const int cap = host->pmecc_corr_cap;
  644. const int num = 2 * cap + 1;
  645. int sector_size = host->pmecc_sector_size;
  646. int err_nbr = 0; /* number of error */
  647. int roots_nbr; /* number of roots */
  648. int i;
  649. uint32_t val;
  650. int16_t *smu = host->pmecc_smu;
  651. pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
  652. for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
  653. pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
  654. smu[(cap + 1) * num + i]);
  655. err_nbr++;
  656. }
  657. val = (err_nbr - 1) << 16;
  658. if (sector_size == 1024)
  659. val |= 1;
  660. pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
  661. pmerrloc_writel(host->pmerrloc_base, ELEN,
  662. sector_size * 8 + host->pmecc_degree * cap);
  663. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  664. while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
  665. & PMERRLOC_CALC_DONE)) {
  666. if (unlikely(time_after(jiffies, end_time))) {
  667. dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
  668. return -1;
  669. }
  670. cpu_relax();
  671. }
  672. roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
  673. & PMERRLOC_ERR_NUM_MASK) >> 8;
  674. /* Number of roots == degree of smu hence <= cap */
  675. if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
  676. return err_nbr - 1;
  677. /* Number of roots does not match the degree of smu
  678. * unable to correct error */
  679. return -1;
  680. }
  681. static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
  682. int sector_num, int extra_bytes, int err_nbr)
  683. {
  684. struct nand_chip *nand_chip = mtd->priv;
  685. struct atmel_nand_host *host = nand_chip->priv;
  686. int i = 0;
  687. int byte_pos, bit_pos, sector_size, pos;
  688. uint32_t tmp;
  689. uint8_t err_byte;
  690. sector_size = host->pmecc_sector_size;
  691. while (err_nbr) {
  692. tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
  693. byte_pos = tmp / 8;
  694. bit_pos = tmp % 8;
  695. if (byte_pos >= (sector_size + extra_bytes))
  696. BUG(); /* should never happen */
  697. if (byte_pos < sector_size) {
  698. err_byte = *(buf + byte_pos);
  699. *(buf + byte_pos) ^= (1 << bit_pos);
  700. pos = sector_num * host->pmecc_sector_size + byte_pos;
  701. dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  702. pos, bit_pos, err_byte, *(buf + byte_pos));
  703. } else {
  704. /* Bit flip in OOB area */
  705. tmp = sector_num * host->pmecc_bytes_per_sector
  706. + (byte_pos - sector_size);
  707. err_byte = ecc[tmp];
  708. ecc[tmp] ^= (1 << bit_pos);
  709. pos = tmp + nand_chip->ecc.layout->eccpos[0];
  710. dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  711. pos, bit_pos, err_byte, ecc[tmp]);
  712. }
  713. i++;
  714. err_nbr--;
  715. }
  716. return;
  717. }
  718. static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
  719. u8 *ecc)
  720. {
  721. struct nand_chip *nand_chip = mtd->priv;
  722. struct atmel_nand_host *host = nand_chip->priv;
  723. int i, err_nbr, eccbytes;
  724. uint8_t *buf_pos;
  725. int total_err = 0;
  726. eccbytes = nand_chip->ecc.bytes;
  727. for (i = 0; i < eccbytes; i++)
  728. if (ecc[i] != 0xff)
  729. goto normal_check;
  730. /* Erased page, return OK */
  731. return 0;
  732. normal_check:
  733. for (i = 0; i < host->pmecc_sector_number; i++) {
  734. err_nbr = 0;
  735. if (pmecc_stat & 0x1) {
  736. buf_pos = buf + i * host->pmecc_sector_size;
  737. pmecc_gen_syndrome(mtd, i);
  738. pmecc_substitute(mtd);
  739. pmecc_get_sigma(mtd);
  740. err_nbr = pmecc_err_location(mtd);
  741. if (err_nbr == -1) {
  742. dev_err(host->dev, "PMECC: Too many errors\n");
  743. mtd->ecc_stats.failed++;
  744. return -EIO;
  745. } else {
  746. pmecc_correct_data(mtd, buf_pos, ecc, i,
  747. host->pmecc_bytes_per_sector, err_nbr);
  748. mtd->ecc_stats.corrected += err_nbr;
  749. total_err += err_nbr;
  750. }
  751. }
  752. pmecc_stat >>= 1;
  753. }
  754. return total_err;
  755. }
  756. static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
  757. {
  758. u32 val;
  759. if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
  760. dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
  761. return;
  762. }
  763. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  764. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  765. val = pmecc_readl_relaxed(host->ecc, CFG);
  766. if (ecc_op == NAND_ECC_READ)
  767. pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
  768. | PMECC_CFG_AUTO_ENABLE);
  769. else
  770. pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
  771. & ~PMECC_CFG_AUTO_ENABLE);
  772. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  773. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
  774. }
  775. static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
  776. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  777. {
  778. struct atmel_nand_host *host = chip->priv;
  779. int eccsize = chip->ecc.size;
  780. uint8_t *oob = chip->oob_poi;
  781. uint32_t *eccpos = chip->ecc.layout->eccpos;
  782. uint32_t stat;
  783. unsigned long end_time;
  784. int bitflips = 0;
  785. if (!host->nfc || !host->nfc->use_nfc_sram)
  786. pmecc_enable(host, NAND_ECC_READ);
  787. chip->read_buf(mtd, buf, eccsize);
  788. chip->read_buf(mtd, oob, mtd->oobsize);
  789. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  790. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  791. if (unlikely(time_after(jiffies, end_time))) {
  792. dev_err(host->dev, "PMECC: Timeout to get error status.\n");
  793. return -EIO;
  794. }
  795. cpu_relax();
  796. }
  797. stat = pmecc_readl_relaxed(host->ecc, ISR);
  798. if (stat != 0) {
  799. bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
  800. if (bitflips < 0)
  801. /* uncorrectable errors */
  802. return 0;
  803. }
  804. return bitflips;
  805. }
  806. static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
  807. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  808. {
  809. struct atmel_nand_host *host = chip->priv;
  810. uint32_t *eccpos = chip->ecc.layout->eccpos;
  811. int i, j;
  812. unsigned long end_time;
  813. if (!host->nfc || !host->nfc->write_by_sram) {
  814. pmecc_enable(host, NAND_ECC_WRITE);
  815. chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
  816. }
  817. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  818. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  819. if (unlikely(time_after(jiffies, end_time))) {
  820. dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
  821. return -EIO;
  822. }
  823. cpu_relax();
  824. }
  825. for (i = 0; i < host->pmecc_sector_number; i++) {
  826. for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
  827. int pos;
  828. pos = i * host->pmecc_bytes_per_sector + j;
  829. chip->oob_poi[eccpos[pos]] =
  830. pmecc_readb_ecc_relaxed(host->ecc, i, j);
  831. }
  832. }
  833. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  834. return 0;
  835. }
  836. static void atmel_pmecc_core_init(struct mtd_info *mtd)
  837. {
  838. struct nand_chip *nand_chip = mtd->priv;
  839. struct atmel_nand_host *host = nand_chip->priv;
  840. uint32_t val = 0;
  841. struct nand_ecclayout *ecc_layout;
  842. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  843. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  844. switch (host->pmecc_corr_cap) {
  845. case 2:
  846. val = PMECC_CFG_BCH_ERR2;
  847. break;
  848. case 4:
  849. val = PMECC_CFG_BCH_ERR4;
  850. break;
  851. case 8:
  852. val = PMECC_CFG_BCH_ERR8;
  853. break;
  854. case 12:
  855. val = PMECC_CFG_BCH_ERR12;
  856. break;
  857. case 24:
  858. val = PMECC_CFG_BCH_ERR24;
  859. break;
  860. }
  861. if (host->pmecc_sector_size == 512)
  862. val |= PMECC_CFG_SECTOR512;
  863. else if (host->pmecc_sector_size == 1024)
  864. val |= PMECC_CFG_SECTOR1024;
  865. switch (host->pmecc_sector_number) {
  866. case 1:
  867. val |= PMECC_CFG_PAGE_1SECTOR;
  868. break;
  869. case 2:
  870. val |= PMECC_CFG_PAGE_2SECTORS;
  871. break;
  872. case 4:
  873. val |= PMECC_CFG_PAGE_4SECTORS;
  874. break;
  875. case 8:
  876. val |= PMECC_CFG_PAGE_8SECTORS;
  877. break;
  878. }
  879. val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
  880. | PMECC_CFG_AUTO_DISABLE);
  881. pmecc_writel(host->ecc, CFG, val);
  882. ecc_layout = nand_chip->ecc.layout;
  883. pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
  884. pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
  885. pmecc_writel(host->ecc, EADDR,
  886. ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
  887. /* See datasheet about PMECC Clock Control Register */
  888. pmecc_writel(host->ecc, CLK, 2);
  889. pmecc_writel(host->ecc, IDR, 0xff);
  890. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  891. }
  892. /*
  893. * Get ECC requirement in ONFI parameters, returns -1 if ONFI
  894. * parameters is not supported.
  895. * return 0 if success to get the ECC requirement.
  896. */
  897. static int get_onfi_ecc_param(struct nand_chip *chip,
  898. int *ecc_bits, int *sector_size)
  899. {
  900. *ecc_bits = *sector_size = 0;
  901. if (chip->onfi_params.ecc_bits == 0xff)
  902. /* TODO: the sector_size and ecc_bits need to be find in
  903. * extended ecc parameter, currently we don't support it.
  904. */
  905. return -1;
  906. *ecc_bits = chip->onfi_params.ecc_bits;
  907. /* The default sector size (ecc codeword size) is 512 */
  908. *sector_size = 512;
  909. return 0;
  910. }
  911. /*
  912. * Get ecc requirement from ONFI parameters ecc requirement.
  913. * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
  914. * will set them according to ONFI ecc requirement. Otherwise, use the
  915. * value in DTS file.
  916. * return 0 if success. otherwise return error code.
  917. */
  918. static int pmecc_choose_ecc(struct atmel_nand_host *host,
  919. int *cap, int *sector_size)
  920. {
  921. /* Get ECC requirement from ONFI parameters */
  922. *cap = *sector_size = 0;
  923. if (host->nand_chip.onfi_version) {
  924. if (!get_onfi_ecc_param(&host->nand_chip, cap, sector_size))
  925. dev_info(host->dev, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
  926. *cap, *sector_size);
  927. else
  928. dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
  929. } else {
  930. dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
  931. }
  932. if (*cap == 0 && *sector_size == 0) {
  933. *cap = 2;
  934. *sector_size = 512;
  935. }
  936. /* If dts file doesn't specify then use the one in ONFI parameters */
  937. if (host->pmecc_corr_cap == 0) {
  938. /* use the most fitable ecc bits (the near bigger one ) */
  939. if (*cap <= 2)
  940. host->pmecc_corr_cap = 2;
  941. else if (*cap <= 4)
  942. host->pmecc_corr_cap = 4;
  943. else if (*cap <= 8)
  944. host->pmecc_corr_cap = 8;
  945. else if (*cap <= 12)
  946. host->pmecc_corr_cap = 12;
  947. else if (*cap <= 24)
  948. host->pmecc_corr_cap = 24;
  949. else
  950. return -EINVAL;
  951. }
  952. if (host->pmecc_sector_size == 0) {
  953. /* use the most fitable sector size (the near smaller one ) */
  954. if (*sector_size >= 1024)
  955. host->pmecc_sector_size = 1024;
  956. else if (*sector_size >= 512)
  957. host->pmecc_sector_size = 512;
  958. else
  959. return -EINVAL;
  960. }
  961. return 0;
  962. }
  963. static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
  964. struct atmel_nand_host *host)
  965. {
  966. struct mtd_info *mtd = &host->mtd;
  967. struct nand_chip *nand_chip = &host->nand_chip;
  968. struct resource *regs, *regs_pmerr, *regs_rom;
  969. int cap, sector_size, err_no;
  970. err_no = pmecc_choose_ecc(host, &cap, &sector_size);
  971. if (err_no) {
  972. dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
  973. return err_no;
  974. }
  975. if (cap > host->pmecc_corr_cap ||
  976. sector_size != host->pmecc_sector_size)
  977. dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
  978. cap = host->pmecc_corr_cap;
  979. sector_size = host->pmecc_sector_size;
  980. host->pmecc_lookup_table_offset = (sector_size == 512) ?
  981. host->pmecc_lookup_table_offset_512 :
  982. host->pmecc_lookup_table_offset_1024;
  983. dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
  984. cap, sector_size);
  985. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  986. if (!regs) {
  987. dev_warn(host->dev,
  988. "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
  989. nand_chip->ecc.mode = NAND_ECC_SOFT;
  990. return 0;
  991. }
  992. host->ecc = devm_ioremap_resource(&pdev->dev, regs);
  993. if (IS_ERR(host->ecc)) {
  994. dev_err(host->dev, "ioremap failed\n");
  995. err_no = PTR_ERR(host->ecc);
  996. goto err;
  997. }
  998. regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  999. host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
  1000. if (IS_ERR(host->pmerrloc_base)) {
  1001. dev_err(host->dev,
  1002. "Can not get I/O resource for PMECC ERRLOC controller!\n");
  1003. err_no = PTR_ERR(host->pmerrloc_base);
  1004. goto err;
  1005. }
  1006. regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  1007. host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
  1008. if (IS_ERR(host->pmecc_rom_base)) {
  1009. dev_err(host->dev, "Can not get I/O resource for ROM!\n");
  1010. err_no = PTR_ERR(host->pmecc_rom_base);
  1011. goto err;
  1012. }
  1013. /* ECC is calculated for the whole page (1 step) */
  1014. nand_chip->ecc.size = mtd->writesize;
  1015. /* set ECC page size and oob layout */
  1016. switch (mtd->writesize) {
  1017. case 2048:
  1018. host->pmecc_degree = PMECC_GF_DIMENSION_13;
  1019. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  1020. host->pmecc_sector_number = mtd->writesize / sector_size;
  1021. host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
  1022. cap, sector_size);
  1023. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  1024. host->pmecc_index_of = host->pmecc_rom_base +
  1025. host->pmecc_lookup_table_offset;
  1026. nand_chip->ecc.steps = 1;
  1027. nand_chip->ecc.strength = cap;
  1028. nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
  1029. host->pmecc_sector_number;
  1030. if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
  1031. dev_err(host->dev, "No room for ECC bytes\n");
  1032. err_no = -EINVAL;
  1033. goto err;
  1034. }
  1035. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  1036. mtd->oobsize,
  1037. nand_chip->ecc.bytes);
  1038. nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
  1039. break;
  1040. case 512:
  1041. case 1024:
  1042. case 4096:
  1043. /* TODO */
  1044. dev_warn(host->dev,
  1045. "Unsupported page size for PMECC, use Software ECC\n");
  1046. default:
  1047. /* page size not handled by HW ECC */
  1048. /* switching back to soft ECC */
  1049. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1050. return 0;
  1051. }
  1052. /* Allocate data for PMECC computation */
  1053. err_no = pmecc_data_alloc(host);
  1054. if (err_no) {
  1055. dev_err(host->dev,
  1056. "Cannot allocate memory for PMECC computation!\n");
  1057. goto err;
  1058. }
  1059. nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
  1060. nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
  1061. atmel_pmecc_core_init(mtd);
  1062. return 0;
  1063. err:
  1064. return err_no;
  1065. }
  1066. /*
  1067. * Calculate HW ECC
  1068. *
  1069. * function called after a write
  1070. *
  1071. * mtd: MTD block structure
  1072. * dat: raw data (unused)
  1073. * ecc_code: buffer for ECC
  1074. */
  1075. static int atmel_nand_calculate(struct mtd_info *mtd,
  1076. const u_char *dat, unsigned char *ecc_code)
  1077. {
  1078. struct nand_chip *nand_chip = mtd->priv;
  1079. struct atmel_nand_host *host = nand_chip->priv;
  1080. unsigned int ecc_value;
  1081. /* get the first 2 ECC bytes */
  1082. ecc_value = ecc_readl(host->ecc, PR);
  1083. ecc_code[0] = ecc_value & 0xFF;
  1084. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  1085. /* get the last 2 ECC bytes */
  1086. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  1087. ecc_code[2] = ecc_value & 0xFF;
  1088. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  1089. return 0;
  1090. }
  1091. /*
  1092. * HW ECC read page function
  1093. *
  1094. * mtd: mtd info structure
  1095. * chip: nand chip info structure
  1096. * buf: buffer to store read data
  1097. * oob_required: caller expects OOB data read to chip->oob_poi
  1098. */
  1099. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  1100. uint8_t *buf, int oob_required, int page)
  1101. {
  1102. int eccsize = chip->ecc.size;
  1103. int eccbytes = chip->ecc.bytes;
  1104. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1105. uint8_t *p = buf;
  1106. uint8_t *oob = chip->oob_poi;
  1107. uint8_t *ecc_pos;
  1108. int stat;
  1109. unsigned int max_bitflips = 0;
  1110. /*
  1111. * Errata: ALE is incorrectly wired up to the ECC controller
  1112. * on the AP7000, so it will include the address cycles in the
  1113. * ECC calculation.
  1114. *
  1115. * Workaround: Reset the parity registers before reading the
  1116. * actual data.
  1117. */
  1118. struct atmel_nand_host *host = chip->priv;
  1119. if (host->board.need_reset_workaround)
  1120. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1121. /* read the page */
  1122. chip->read_buf(mtd, p, eccsize);
  1123. /* move to ECC position if needed */
  1124. if (eccpos[0] != 0) {
  1125. /* This only works on large pages
  1126. * because the ECC controller waits for
  1127. * NAND_CMD_RNDOUTSTART after the
  1128. * NAND_CMD_RNDOUT.
  1129. * anyway, for small pages, the eccpos[0] == 0
  1130. */
  1131. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1132. mtd->writesize + eccpos[0], -1);
  1133. }
  1134. /* the ECC controller needs to read the ECC just after the data */
  1135. ecc_pos = oob + eccpos[0];
  1136. chip->read_buf(mtd, ecc_pos, eccbytes);
  1137. /* check if there's an error */
  1138. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1139. if (stat < 0) {
  1140. mtd->ecc_stats.failed++;
  1141. } else {
  1142. mtd->ecc_stats.corrected += stat;
  1143. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1144. }
  1145. /* get back to oob start (end of page) */
  1146. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1147. /* read the oob */
  1148. chip->read_buf(mtd, oob, mtd->oobsize);
  1149. return max_bitflips;
  1150. }
  1151. /*
  1152. * HW ECC Correction
  1153. *
  1154. * function called after a read
  1155. *
  1156. * mtd: MTD block structure
  1157. * dat: raw data read from the chip
  1158. * read_ecc: ECC from the chip (unused)
  1159. * isnull: unused
  1160. *
  1161. * Detect and correct a 1 bit error for a page
  1162. */
  1163. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  1164. u_char *read_ecc, u_char *isnull)
  1165. {
  1166. struct nand_chip *nand_chip = mtd->priv;
  1167. struct atmel_nand_host *host = nand_chip->priv;
  1168. unsigned int ecc_status;
  1169. unsigned int ecc_word, ecc_bit;
  1170. /* get the status from the Status Register */
  1171. ecc_status = ecc_readl(host->ecc, SR);
  1172. /* if there's no error */
  1173. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  1174. return 0;
  1175. /* get error bit offset (4 bits) */
  1176. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  1177. /* get word address (12 bits) */
  1178. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  1179. ecc_word >>= 4;
  1180. /* if there are multiple errors */
  1181. if (ecc_status & ATMEL_ECC_MULERR) {
  1182. /* check if it is a freshly erased block
  1183. * (filled with 0xff) */
  1184. if ((ecc_bit == ATMEL_ECC_BITADDR)
  1185. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  1186. /* the block has just been erased, return OK */
  1187. return 0;
  1188. }
  1189. /* it doesn't seems to be a freshly
  1190. * erased block.
  1191. * We can't correct so many errors */
  1192. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  1193. " Unable to correct.\n");
  1194. return -EIO;
  1195. }
  1196. /* if there's a single bit error : we can correct it */
  1197. if (ecc_status & ATMEL_ECC_ECCERR) {
  1198. /* there's nothing much to do here.
  1199. * the bit error is on the ECC itself.
  1200. */
  1201. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  1202. " Nothing to correct\n");
  1203. return 0;
  1204. }
  1205. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  1206. " (word offset in the page :"
  1207. " 0x%x bit offset : 0x%x)\n",
  1208. ecc_word, ecc_bit);
  1209. /* correct the error */
  1210. if (nand_chip->options & NAND_BUSWIDTH_16) {
  1211. /* 16 bits words */
  1212. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  1213. } else {
  1214. /* 8 bits words */
  1215. dat[ecc_word] ^= (1 << ecc_bit);
  1216. }
  1217. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  1218. return 1;
  1219. }
  1220. /*
  1221. * Enable HW ECC : unused on most chips
  1222. */
  1223. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  1224. {
  1225. struct nand_chip *nand_chip = mtd->priv;
  1226. struct atmel_nand_host *host = nand_chip->priv;
  1227. if (host->board.need_reset_workaround)
  1228. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1229. }
  1230. #if defined(CONFIG_OF)
  1231. static int atmel_of_init_port(struct atmel_nand_host *host,
  1232. struct device_node *np)
  1233. {
  1234. u32 val;
  1235. u32 offset[2];
  1236. int ecc_mode;
  1237. struct atmel_nand_data *board = &host->board;
  1238. enum of_gpio_flags flags;
  1239. if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
  1240. if (val >= 32) {
  1241. dev_err(host->dev, "invalid addr-offset %u\n", val);
  1242. return -EINVAL;
  1243. }
  1244. board->ale = val;
  1245. }
  1246. if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
  1247. if (val >= 32) {
  1248. dev_err(host->dev, "invalid cmd-offset %u\n", val);
  1249. return -EINVAL;
  1250. }
  1251. board->cle = val;
  1252. }
  1253. ecc_mode = of_get_nand_ecc_mode(np);
  1254. board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
  1255. board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
  1256. board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
  1257. if (of_get_nand_bus_width(np) == 16)
  1258. board->bus_width_16 = 1;
  1259. board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
  1260. board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
  1261. board->enable_pin = of_get_gpio(np, 1);
  1262. board->det_pin = of_get_gpio(np, 2);
  1263. host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
  1264. /* load the nfc driver if there is */
  1265. of_platform_populate(np, NULL, NULL, host->dev);
  1266. if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
  1267. return 0; /* Not using PMECC */
  1268. /* use PMECC, get correction capability, sector size and lookup
  1269. * table offset.
  1270. * If correction bits and sector size are not specified, then find
  1271. * them from NAND ONFI parameters.
  1272. */
  1273. if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
  1274. if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
  1275. (val != 24)) {
  1276. dev_err(host->dev,
  1277. "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
  1278. val);
  1279. return -EINVAL;
  1280. }
  1281. host->pmecc_corr_cap = (u8)val;
  1282. }
  1283. if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
  1284. if ((val != 512) && (val != 1024)) {
  1285. dev_err(host->dev,
  1286. "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
  1287. val);
  1288. return -EINVAL;
  1289. }
  1290. host->pmecc_sector_size = (u16)val;
  1291. }
  1292. if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
  1293. offset, 2) != 0) {
  1294. dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
  1295. return -EINVAL;
  1296. }
  1297. if (!offset[0] && !offset[1]) {
  1298. dev_err(host->dev, "Invalid PMECC lookup table offset\n");
  1299. return -EINVAL;
  1300. }
  1301. host->pmecc_lookup_table_offset_512 = offset[0];
  1302. host->pmecc_lookup_table_offset_1024 = offset[1];
  1303. return 0;
  1304. }
  1305. #else
  1306. static int atmel_of_init_port(struct atmel_nand_host *host,
  1307. struct device_node *np)
  1308. {
  1309. return -EINVAL;
  1310. }
  1311. #endif
  1312. static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
  1313. struct atmel_nand_host *host)
  1314. {
  1315. struct mtd_info *mtd = &host->mtd;
  1316. struct nand_chip *nand_chip = &host->nand_chip;
  1317. struct resource *regs;
  1318. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1319. if (!regs) {
  1320. dev_err(host->dev,
  1321. "Can't get I/O resource regs, use software ECC\n");
  1322. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1323. return 0;
  1324. }
  1325. host->ecc = devm_ioremap_resource(&pdev->dev, regs);
  1326. if (IS_ERR(host->ecc)) {
  1327. dev_err(host->dev, "ioremap failed\n");
  1328. return PTR_ERR(host->ecc);
  1329. }
  1330. /* ECC is calculated for the whole page (1 step) */
  1331. nand_chip->ecc.size = mtd->writesize;
  1332. /* set ECC page size and oob layout */
  1333. switch (mtd->writesize) {
  1334. case 512:
  1335. nand_chip->ecc.layout = &atmel_oobinfo_small;
  1336. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  1337. break;
  1338. case 1024:
  1339. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1340. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  1341. break;
  1342. case 2048:
  1343. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1344. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  1345. break;
  1346. case 4096:
  1347. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1348. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  1349. break;
  1350. default:
  1351. /* page size not handled by HW ECC */
  1352. /* switching back to soft ECC */
  1353. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1354. return 0;
  1355. }
  1356. /* set up for HW ECC */
  1357. nand_chip->ecc.calculate = atmel_nand_calculate;
  1358. nand_chip->ecc.correct = atmel_nand_correct;
  1359. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  1360. nand_chip->ecc.read_page = atmel_nand_read_page;
  1361. nand_chip->ecc.bytes = 4;
  1362. nand_chip->ecc.strength = 1;
  1363. return 0;
  1364. }
  1365. /* SMC interrupt service routine */
  1366. static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
  1367. {
  1368. struct atmel_nand_host *host = dev_id;
  1369. u32 status, mask, pending;
  1370. irqreturn_t ret = IRQ_HANDLED;
  1371. status = nfc_readl(host->nfc->hsmc_regs, SR);
  1372. mask = nfc_readl(host->nfc->hsmc_regs, IMR);
  1373. pending = status & mask;
  1374. if (pending & NFC_SR_XFR_DONE) {
  1375. complete(&host->nfc->comp_nfc);
  1376. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
  1377. } else if (pending & NFC_SR_RB_EDGE) {
  1378. complete(&host->nfc->comp_nfc);
  1379. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
  1380. } else if (pending & NFC_SR_CMD_DONE) {
  1381. complete(&host->nfc->comp_nfc);
  1382. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
  1383. } else {
  1384. ret = IRQ_NONE;
  1385. }
  1386. return ret;
  1387. }
  1388. /* NFC(Nand Flash Controller) related functions */
  1389. static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
  1390. {
  1391. unsigned long timeout;
  1392. init_completion(&host->nfc->comp_nfc);
  1393. /* Enable interrupt that need to wait for */
  1394. nfc_writel(host->nfc->hsmc_regs, IER, flag);
  1395. timeout = wait_for_completion_timeout(&host->nfc->comp_nfc,
  1396. msecs_to_jiffies(NFC_TIME_OUT_MS));
  1397. if (timeout)
  1398. return 0;
  1399. /* Time out to wait for the interrupt */
  1400. dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
  1401. return -ETIMEDOUT;
  1402. }
  1403. static int nfc_send_command(struct atmel_nand_host *host,
  1404. unsigned int cmd, unsigned int addr, unsigned char cycle0)
  1405. {
  1406. unsigned long timeout;
  1407. dev_dbg(host->dev,
  1408. "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
  1409. cmd, addr, cycle0);
  1410. timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
  1411. while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
  1412. & NFCADDR_CMD_NFCBUSY) {
  1413. if (time_after(jiffies, timeout)) {
  1414. dev_err(host->dev,
  1415. "Time out to wait CMD_NFCBUSY ready!\n");
  1416. return -ETIMEDOUT;
  1417. }
  1418. }
  1419. nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
  1420. nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
  1421. return nfc_wait_interrupt(host, NFC_SR_CMD_DONE);
  1422. }
  1423. static int nfc_device_ready(struct mtd_info *mtd)
  1424. {
  1425. struct nand_chip *nand_chip = mtd->priv;
  1426. struct atmel_nand_host *host = nand_chip->priv;
  1427. if (!nfc_wait_interrupt(host, NFC_SR_RB_EDGE))
  1428. return 1;
  1429. return 0;
  1430. }
  1431. static void nfc_select_chip(struct mtd_info *mtd, int chip)
  1432. {
  1433. struct nand_chip *nand_chip = mtd->priv;
  1434. struct atmel_nand_host *host = nand_chip->priv;
  1435. if (chip == -1)
  1436. nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
  1437. else
  1438. nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
  1439. }
  1440. static int nfc_make_addr(struct mtd_info *mtd, int column, int page_addr,
  1441. unsigned int *addr1234, unsigned int *cycle0)
  1442. {
  1443. struct nand_chip *chip = mtd->priv;
  1444. int acycle = 0;
  1445. unsigned char addr_bytes[8];
  1446. int index = 0, bit_shift;
  1447. BUG_ON(addr1234 == NULL || cycle0 == NULL);
  1448. *cycle0 = 0;
  1449. *addr1234 = 0;
  1450. if (column != -1) {
  1451. if (chip->options & NAND_BUSWIDTH_16)
  1452. column >>= 1;
  1453. addr_bytes[acycle++] = column & 0xff;
  1454. if (mtd->writesize > 512)
  1455. addr_bytes[acycle++] = (column >> 8) & 0xff;
  1456. }
  1457. if (page_addr != -1) {
  1458. addr_bytes[acycle++] = page_addr & 0xff;
  1459. addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
  1460. if (chip->chipsize > (128 << 20))
  1461. addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
  1462. }
  1463. if (acycle > 4)
  1464. *cycle0 = addr_bytes[index++];
  1465. for (bit_shift = 0; index < acycle; bit_shift += 8)
  1466. *addr1234 += addr_bytes[index++] << bit_shift;
  1467. /* return acycle in cmd register */
  1468. return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
  1469. }
  1470. static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
  1471. int column, int page_addr)
  1472. {
  1473. struct nand_chip *chip = mtd->priv;
  1474. struct atmel_nand_host *host = chip->priv;
  1475. unsigned long timeout;
  1476. unsigned int nfc_addr_cmd = 0;
  1477. unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
  1478. /* Set default settings: no cmd2, no addr cycle. read from nand */
  1479. unsigned int cmd2 = 0;
  1480. unsigned int vcmd2 = 0;
  1481. int acycle = NFCADDR_CMD_ACYCLE_NONE;
  1482. int csid = NFCADDR_CMD_CSID_3;
  1483. int dataen = NFCADDR_CMD_DATADIS;
  1484. int nfcwr = NFCADDR_CMD_NFCRD;
  1485. unsigned int addr1234 = 0;
  1486. unsigned int cycle0 = 0;
  1487. bool do_addr = true;
  1488. host->nfc->data_in_sram = NULL;
  1489. dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
  1490. __func__, command, column, page_addr);
  1491. switch (command) {
  1492. case NAND_CMD_RESET:
  1493. nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
  1494. nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
  1495. udelay(chip->chip_delay);
  1496. nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
  1497. timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
  1498. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
  1499. if (time_after(jiffies, timeout)) {
  1500. dev_err(host->dev,
  1501. "Time out to wait status ready!\n");
  1502. break;
  1503. }
  1504. }
  1505. return;
  1506. case NAND_CMD_STATUS:
  1507. do_addr = false;
  1508. break;
  1509. case NAND_CMD_PARAM:
  1510. case NAND_CMD_READID:
  1511. do_addr = false;
  1512. acycle = NFCADDR_CMD_ACYCLE_1;
  1513. if (column != -1)
  1514. addr1234 = column;
  1515. break;
  1516. case NAND_CMD_RNDOUT:
  1517. cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
  1518. vcmd2 = NFCADDR_CMD_VCMD2;
  1519. break;
  1520. case NAND_CMD_READ0:
  1521. case NAND_CMD_READOOB:
  1522. if (command == NAND_CMD_READOOB) {
  1523. column += mtd->writesize;
  1524. command = NAND_CMD_READ0; /* only READ0 is valid */
  1525. cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
  1526. }
  1527. if (host->nfc->use_nfc_sram) {
  1528. /* Enable Data transfer to sram */
  1529. dataen = NFCADDR_CMD_DATAEN;
  1530. /* Need enable PMECC now, since NFC will transfer
  1531. * data in bus after sending nfc read command.
  1532. */
  1533. if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
  1534. pmecc_enable(host, NAND_ECC_READ);
  1535. }
  1536. cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
  1537. vcmd2 = NFCADDR_CMD_VCMD2;
  1538. break;
  1539. /* For prgramming command, the cmd need set to write enable */
  1540. case NAND_CMD_PAGEPROG:
  1541. case NAND_CMD_SEQIN:
  1542. case NAND_CMD_RNDIN:
  1543. nfcwr = NFCADDR_CMD_NFCWR;
  1544. if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
  1545. dataen = NFCADDR_CMD_DATAEN;
  1546. break;
  1547. default:
  1548. break;
  1549. }
  1550. if (do_addr)
  1551. acycle = nfc_make_addr(mtd, column, page_addr, &addr1234,
  1552. &cycle0);
  1553. nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
  1554. nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
  1555. if (dataen == NFCADDR_CMD_DATAEN)
  1556. if (nfc_wait_interrupt(host, NFC_SR_XFR_DONE))
  1557. dev_err(host->dev, "something wrong, No XFR_DONE interrupt comes.\n");
  1558. /*
  1559. * Program and erase have their own busy handlers status, sequential
  1560. * in, and deplete1 need no delay.
  1561. */
  1562. switch (command) {
  1563. case NAND_CMD_CACHEDPROG:
  1564. case NAND_CMD_PAGEPROG:
  1565. case NAND_CMD_ERASE1:
  1566. case NAND_CMD_ERASE2:
  1567. case NAND_CMD_RNDIN:
  1568. case NAND_CMD_STATUS:
  1569. case NAND_CMD_RNDOUT:
  1570. case NAND_CMD_SEQIN:
  1571. case NAND_CMD_READID:
  1572. return;
  1573. case NAND_CMD_READ0:
  1574. if (dataen == NFCADDR_CMD_DATAEN) {
  1575. host->nfc->data_in_sram = host->nfc->sram_bank0 +
  1576. nfc_get_sram_off(host);
  1577. return;
  1578. }
  1579. /* fall through */
  1580. default:
  1581. nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
  1582. }
  1583. }
  1584. static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1585. uint32_t offset, int data_len, const uint8_t *buf,
  1586. int oob_required, int page, int cached, int raw)
  1587. {
  1588. int cfg, len;
  1589. int status = 0;
  1590. struct atmel_nand_host *host = chip->priv;
  1591. void __iomem *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
  1592. /* Subpage write is not supported */
  1593. if (offset || (data_len < mtd->writesize))
  1594. return -EINVAL;
  1595. cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
  1596. len = mtd->writesize;
  1597. if (unlikely(raw)) {
  1598. len += mtd->oobsize;
  1599. nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
  1600. } else
  1601. nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
  1602. /* Copy page data to sram that will write to nand via NFC */
  1603. if (use_dma) {
  1604. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
  1605. /* Fall back to use cpu copy */
  1606. memcpy32_toio(sram, buf, len);
  1607. } else {
  1608. memcpy32_toio(sram, buf, len);
  1609. }
  1610. if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
  1611. /*
  1612. * When use NFC sram, need set up PMECC before send
  1613. * NAND_CMD_SEQIN command. Since when the nand command
  1614. * is sent, nfc will do transfer from sram and nand.
  1615. */
  1616. pmecc_enable(host, NAND_ECC_WRITE);
  1617. host->nfc->will_write_sram = true;
  1618. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1619. host->nfc->will_write_sram = false;
  1620. if (likely(!raw))
  1621. /* Need to write ecc into oob */
  1622. status = chip->ecc.write_page(mtd, chip, buf, oob_required);
  1623. if (status < 0)
  1624. return status;
  1625. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1626. status = chip->waitfunc(mtd, chip);
  1627. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1628. status = chip->errstat(mtd, chip, FL_WRITING, status, page);
  1629. if (status & NAND_STATUS_FAIL)
  1630. return -EIO;
  1631. return 0;
  1632. }
  1633. static int nfc_sram_init(struct mtd_info *mtd)
  1634. {
  1635. struct nand_chip *chip = mtd->priv;
  1636. struct atmel_nand_host *host = chip->priv;
  1637. int res = 0;
  1638. /* Initialize the NFC CFG register */
  1639. unsigned int cfg_nfc = 0;
  1640. /* set page size and oob layout */
  1641. switch (mtd->writesize) {
  1642. case 512:
  1643. cfg_nfc = NFC_CFG_PAGESIZE_512;
  1644. break;
  1645. case 1024:
  1646. cfg_nfc = NFC_CFG_PAGESIZE_1024;
  1647. break;
  1648. case 2048:
  1649. cfg_nfc = NFC_CFG_PAGESIZE_2048;
  1650. break;
  1651. case 4096:
  1652. cfg_nfc = NFC_CFG_PAGESIZE_4096;
  1653. break;
  1654. case 8192:
  1655. cfg_nfc = NFC_CFG_PAGESIZE_8192;
  1656. break;
  1657. default:
  1658. dev_err(host->dev, "Unsupported page size for NFC.\n");
  1659. res = -ENXIO;
  1660. return res;
  1661. }
  1662. /* oob bytes size = (NFCSPARESIZE + 1) * 4
  1663. * Max support spare size is 512 bytes. */
  1664. cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
  1665. & NFC_CFG_NFC_SPARESIZE);
  1666. /* default set a max timeout */
  1667. cfg_nfc |= NFC_CFG_RSPARE |
  1668. NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
  1669. nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
  1670. host->nfc->will_write_sram = false;
  1671. nfc_set_sram_bank(host, 0);
  1672. /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
  1673. if (host->nfc->write_by_sram) {
  1674. if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
  1675. chip->ecc.mode == NAND_ECC_NONE)
  1676. chip->write_page = nfc_sram_write_page;
  1677. else
  1678. host->nfc->write_by_sram = false;
  1679. }
  1680. dev_info(host->dev, "Using NFC Sram read %s\n",
  1681. host->nfc->write_by_sram ? "and write" : "");
  1682. return 0;
  1683. }
  1684. static struct platform_driver atmel_nand_nfc_driver;
  1685. /*
  1686. * Probe for the NAND device.
  1687. */
  1688. static int __init atmel_nand_probe(struct platform_device *pdev)
  1689. {
  1690. struct atmel_nand_host *host;
  1691. struct mtd_info *mtd;
  1692. struct nand_chip *nand_chip;
  1693. struct resource *mem;
  1694. struct mtd_part_parser_data ppdata = {};
  1695. int res, irq;
  1696. /* Allocate memory for the device structure (and zero it) */
  1697. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  1698. if (!host) {
  1699. printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
  1700. return -ENOMEM;
  1701. }
  1702. res = platform_driver_register(&atmel_nand_nfc_driver);
  1703. if (res)
  1704. dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
  1705. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1706. host->io_base = devm_ioremap_resource(&pdev->dev, mem);
  1707. if (IS_ERR(host->io_base)) {
  1708. dev_err(&pdev->dev, "atmel_nand: ioremap resource failed\n");
  1709. res = PTR_ERR(host->io_base);
  1710. goto err_nand_ioremap;
  1711. }
  1712. host->io_phys = (dma_addr_t)mem->start;
  1713. mtd = &host->mtd;
  1714. nand_chip = &host->nand_chip;
  1715. host->dev = &pdev->dev;
  1716. if (pdev->dev.of_node) {
  1717. res = atmel_of_init_port(host, pdev->dev.of_node);
  1718. if (res)
  1719. goto err_nand_ioremap;
  1720. } else {
  1721. memcpy(&host->board, dev_get_platdata(&pdev->dev),
  1722. sizeof(struct atmel_nand_data));
  1723. }
  1724. nand_chip->priv = host; /* link the private data structures */
  1725. mtd->priv = nand_chip;
  1726. mtd->owner = THIS_MODULE;
  1727. /* Set address of NAND IO lines */
  1728. nand_chip->IO_ADDR_R = host->io_base;
  1729. nand_chip->IO_ADDR_W = host->io_base;
  1730. if (nand_nfc.is_initialized) {
  1731. /* NFC driver is probed and initialized */
  1732. host->nfc = &nand_nfc;
  1733. nand_chip->select_chip = nfc_select_chip;
  1734. nand_chip->dev_ready = nfc_device_ready;
  1735. nand_chip->cmdfunc = nfc_nand_command;
  1736. /* Initialize the interrupt for NFC */
  1737. irq = platform_get_irq(pdev, 0);
  1738. if (irq < 0) {
  1739. dev_err(host->dev, "Cannot get HSMC irq!\n");
  1740. goto err_nand_ioremap;
  1741. }
  1742. res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
  1743. 0, "hsmc", host);
  1744. if (res) {
  1745. dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
  1746. irq);
  1747. goto err_nand_ioremap;
  1748. }
  1749. } else {
  1750. res = atmel_nand_set_enable_ready_pins(mtd);
  1751. if (res)
  1752. goto err_nand_ioremap;
  1753. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  1754. }
  1755. nand_chip->ecc.mode = host->board.ecc_mode;
  1756. nand_chip->chip_delay = 20; /* 20us command delay time */
  1757. if (host->board.bus_width_16) /* 16-bit bus width */
  1758. nand_chip->options |= NAND_BUSWIDTH_16;
  1759. nand_chip->read_buf = atmel_read_buf;
  1760. nand_chip->write_buf = atmel_write_buf;
  1761. platform_set_drvdata(pdev, host);
  1762. atmel_nand_enable(host);
  1763. if (gpio_is_valid(host->board.det_pin)) {
  1764. res = devm_gpio_request(&pdev->dev,
  1765. host->board.det_pin, "nand_det");
  1766. if (res < 0) {
  1767. dev_err(&pdev->dev,
  1768. "can't request det gpio %d\n",
  1769. host->board.det_pin);
  1770. goto err_no_card;
  1771. }
  1772. res = gpio_direction_input(host->board.det_pin);
  1773. if (res < 0) {
  1774. dev_err(&pdev->dev,
  1775. "can't request input direction det gpio %d\n",
  1776. host->board.det_pin);
  1777. goto err_no_card;
  1778. }
  1779. if (gpio_get_value(host->board.det_pin)) {
  1780. printk(KERN_INFO "No SmartMedia card inserted.\n");
  1781. res = -ENXIO;
  1782. goto err_no_card;
  1783. }
  1784. }
  1785. if (host->board.on_flash_bbt || on_flash_bbt) {
  1786. printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
  1787. nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
  1788. }
  1789. if (!host->board.has_dma)
  1790. use_dma = 0;
  1791. if (use_dma) {
  1792. dma_cap_mask_t mask;
  1793. dma_cap_zero(mask);
  1794. dma_cap_set(DMA_MEMCPY, mask);
  1795. host->dma_chan = dma_request_channel(mask, NULL, NULL);
  1796. if (!host->dma_chan) {
  1797. dev_err(host->dev, "Failed to request DMA channel\n");
  1798. use_dma = 0;
  1799. }
  1800. }
  1801. if (use_dma)
  1802. dev_info(host->dev, "Using %s for DMA transfers.\n",
  1803. dma_chan_name(host->dma_chan));
  1804. else
  1805. dev_info(host->dev, "No DMA support for NAND access.\n");
  1806. /* first scan to find the device and get the page size */
  1807. if (nand_scan_ident(mtd, 1, NULL)) {
  1808. res = -ENXIO;
  1809. goto err_scan_ident;
  1810. }
  1811. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  1812. if (host->has_pmecc)
  1813. res = atmel_pmecc_nand_init_params(pdev, host);
  1814. else
  1815. res = atmel_hw_nand_init_params(pdev, host);
  1816. if (res != 0)
  1817. goto err_hw_ecc;
  1818. }
  1819. /* initialize the nfc configuration register */
  1820. if (host->nfc && host->nfc->use_nfc_sram) {
  1821. res = nfc_sram_init(mtd);
  1822. if (res) {
  1823. host->nfc->use_nfc_sram = false;
  1824. dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
  1825. }
  1826. }
  1827. /* second phase scan */
  1828. if (nand_scan_tail(mtd)) {
  1829. res = -ENXIO;
  1830. goto err_scan_tail;
  1831. }
  1832. mtd->name = "atmel_nand";
  1833. ppdata.of_node = pdev->dev.of_node;
  1834. res = mtd_device_parse_register(mtd, NULL, &ppdata,
  1835. host->board.parts, host->board.num_parts);
  1836. if (!res)
  1837. return res;
  1838. err_scan_tail:
  1839. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
  1840. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1841. err_hw_ecc:
  1842. err_scan_ident:
  1843. err_no_card:
  1844. atmel_nand_disable(host);
  1845. if (host->dma_chan)
  1846. dma_release_channel(host->dma_chan);
  1847. err_nand_ioremap:
  1848. platform_driver_unregister(&atmel_nand_nfc_driver);
  1849. return res;
  1850. }
  1851. /*
  1852. * Remove a NAND device.
  1853. */
  1854. static int __exit atmel_nand_remove(struct platform_device *pdev)
  1855. {
  1856. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  1857. struct mtd_info *mtd = &host->mtd;
  1858. nand_release(mtd);
  1859. atmel_nand_disable(host);
  1860. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
  1861. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1862. pmerrloc_writel(host->pmerrloc_base, ELDIS,
  1863. PMERRLOC_DISABLE);
  1864. }
  1865. if (host->dma_chan)
  1866. dma_release_channel(host->dma_chan);
  1867. platform_driver_unregister(&atmel_nand_nfc_driver);
  1868. return 0;
  1869. }
  1870. #if defined(CONFIG_OF)
  1871. static const struct of_device_id atmel_nand_dt_ids[] = {
  1872. { .compatible = "atmel,at91rm9200-nand" },
  1873. { /* sentinel */ }
  1874. };
  1875. MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
  1876. #endif
  1877. static int atmel_nand_nfc_probe(struct platform_device *pdev)
  1878. {
  1879. struct atmel_nfc *nfc = &nand_nfc;
  1880. struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
  1881. nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1882. nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
  1883. if (IS_ERR(nfc->base_cmd_regs))
  1884. return PTR_ERR(nfc->base_cmd_regs);
  1885. nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1886. nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
  1887. if (IS_ERR(nfc->hsmc_regs))
  1888. return PTR_ERR(nfc->hsmc_regs);
  1889. nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1890. if (nfc_sram) {
  1891. nfc->sram_bank0 = devm_ioremap_resource(&pdev->dev, nfc_sram);
  1892. if (IS_ERR(nfc->sram_bank0)) {
  1893. dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
  1894. PTR_ERR(nfc->sram_bank0));
  1895. } else {
  1896. nfc->use_nfc_sram = true;
  1897. nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
  1898. if (pdev->dev.of_node)
  1899. nfc->write_by_sram = of_property_read_bool(
  1900. pdev->dev.of_node,
  1901. "atmel,write-by-sram");
  1902. }
  1903. }
  1904. nfc->is_initialized = true;
  1905. dev_info(&pdev->dev, "NFC is probed.\n");
  1906. return 0;
  1907. }
  1908. #if defined(CONFIG_OF)
  1909. static struct of_device_id atmel_nand_nfc_match[] = {
  1910. { .compatible = "atmel,sama5d3-nfc" },
  1911. { /* sentinel */ }
  1912. };
  1913. #endif
  1914. static struct platform_driver atmel_nand_nfc_driver = {
  1915. .driver = {
  1916. .name = "atmel_nand_nfc",
  1917. .owner = THIS_MODULE,
  1918. .of_match_table = of_match_ptr(atmel_nand_nfc_match),
  1919. },
  1920. .probe = atmel_nand_nfc_probe,
  1921. };
  1922. static struct platform_driver atmel_nand_driver = {
  1923. .remove = __exit_p(atmel_nand_remove),
  1924. .driver = {
  1925. .name = "atmel_nand",
  1926. .owner = THIS_MODULE,
  1927. .of_match_table = of_match_ptr(atmel_nand_dt_ids),
  1928. },
  1929. };
  1930. module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe);
  1931. MODULE_LICENSE("GPL");
  1932. MODULE_AUTHOR("Rick Bronson");
  1933. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  1934. MODULE_ALIAS("platform:atmel_nand");