atmel_nand.c 65 KB

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