atmel_nand.c 62 KB

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