atmel_nand.c 60 KB

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