atmel_nand.c 64 KB

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