atmel_nand.c 65 KB

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