atmel_nand.c 61 KB

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