atmel_nand.c 65 KB

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