sunxi_nand.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon.dev@gmail.com>
  3. *
  4. * Derived from:
  5. * https://github.com/yuq/sunxi-nfc-mtd
  6. * Copyright (C) 2013 Qiang Yu <yuq825@gmail.com>
  7. *
  8. * https://github.com/hno/Allwinner-Info
  9. * Copyright (C) 2013 Henrik Nordström <Henrik Nordström>
  10. *
  11. * Copyright (C) 2013 Dmitriy B. <rzk333@gmail.com>
  12. * Copyright (C) 2013 Sergey Lapin <slapin@ossfans.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #include <linux/dma-mapping.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/of_mtd.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/nand.h>
  35. #include <linux/mtd/partitions.h>
  36. #include <linux/clk.h>
  37. #include <linux/delay.h>
  38. #include <linux/dmaengine.h>
  39. #include <linux/gpio.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/io.h>
  42. #define NFC_REG_CTL 0x0000
  43. #define NFC_REG_ST 0x0004
  44. #define NFC_REG_INT 0x0008
  45. #define NFC_REG_TIMING_CTL 0x000C
  46. #define NFC_REG_TIMING_CFG 0x0010
  47. #define NFC_REG_ADDR_LOW 0x0014
  48. #define NFC_REG_ADDR_HIGH 0x0018
  49. #define NFC_REG_SECTOR_NUM 0x001C
  50. #define NFC_REG_CNT 0x0020
  51. #define NFC_REG_CMD 0x0024
  52. #define NFC_REG_RCMD_SET 0x0028
  53. #define NFC_REG_WCMD_SET 0x002C
  54. #define NFC_REG_IO_DATA 0x0030
  55. #define NFC_REG_ECC_CTL 0x0034
  56. #define NFC_REG_ECC_ST 0x0038
  57. #define NFC_REG_DEBUG 0x003C
  58. #define NFC_REG_ECC_CNT0 0x0040
  59. #define NFC_REG_ECC_CNT1 0x0044
  60. #define NFC_REG_ECC_CNT2 0x0048
  61. #define NFC_REG_ECC_CNT3 0x004c
  62. #define NFC_REG_USER_DATA_BASE 0x0050
  63. #define NFC_REG_SPARE_AREA 0x00A0
  64. #define NFC_RAM0_BASE 0x0400
  65. #define NFC_RAM1_BASE 0x0800
  66. /* define bit use in NFC_CTL */
  67. #define NFC_EN BIT(0)
  68. #define NFC_RESET BIT(1)
  69. #define NFC_BUS_WIDYH BIT(2)
  70. #define NFC_RB_SEL BIT(3)
  71. #define NFC_CE_SEL GENMASK(26, 24)
  72. #define NFC_CE_CTL BIT(6)
  73. #define NFC_CE_CTL1 BIT(7)
  74. #define NFC_PAGE_SIZE GENMASK(11, 8)
  75. #define NFC_SAM BIT(12)
  76. #define NFC_RAM_METHOD BIT(14)
  77. #define NFC_DEBUG_CTL BIT(31)
  78. /* define bit use in NFC_ST */
  79. #define NFC_RB_B2R BIT(0)
  80. #define NFC_CMD_INT_FLAG BIT(1)
  81. #define NFC_DMA_INT_FLAG BIT(2)
  82. #define NFC_CMD_FIFO_STATUS BIT(3)
  83. #define NFC_STA BIT(4)
  84. #define NFC_NATCH_INT_FLAG BIT(5)
  85. #define NFC_RB_STATE0 BIT(8)
  86. #define NFC_RB_STATE1 BIT(9)
  87. #define NFC_RB_STATE2 BIT(10)
  88. #define NFC_RB_STATE3 BIT(11)
  89. /* define bit use in NFC_INT */
  90. #define NFC_B2R_INT_ENABLE BIT(0)
  91. #define NFC_CMD_INT_ENABLE BIT(1)
  92. #define NFC_DMA_INT_ENABLE BIT(2)
  93. #define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \
  94. NFC_CMD_INT_ENABLE | \
  95. NFC_DMA_INT_ENABLE)
  96. /* define bit use in NFC_CMD */
  97. #define NFC_CMD_LOW_BYTE GENMASK(7, 0)
  98. #define NFC_CMD_HIGH_BYTE GENMASK(15, 8)
  99. #define NFC_ADR_NUM GENMASK(18, 16)
  100. #define NFC_SEND_ADR BIT(19)
  101. #define NFC_ACCESS_DIR BIT(20)
  102. #define NFC_DATA_TRANS BIT(21)
  103. #define NFC_SEND_CMD1 BIT(22)
  104. #define NFC_WAIT_FLAG BIT(23)
  105. #define NFC_SEND_CMD2 BIT(24)
  106. #define NFC_SEQ BIT(25)
  107. #define NFC_DATA_SWAP_METHOD BIT(26)
  108. #define NFC_ROW_AUTO_INC BIT(27)
  109. #define NFC_SEND_CMD3 BIT(28)
  110. #define NFC_SEND_CMD4 BIT(29)
  111. #define NFC_CMD_TYPE GENMASK(31, 30)
  112. /* define bit use in NFC_RCMD_SET */
  113. #define NFC_READ_CMD GENMASK(7, 0)
  114. #define NFC_RANDOM_READ_CMD0 GENMASK(15, 8)
  115. #define NFC_RANDOM_READ_CMD1 GENMASK(23, 16)
  116. /* define bit use in NFC_WCMD_SET */
  117. #define NFC_PROGRAM_CMD GENMASK(7, 0)
  118. #define NFC_RANDOM_WRITE_CMD GENMASK(15, 8)
  119. #define NFC_READ_CMD0 GENMASK(23, 16)
  120. #define NFC_READ_CMD1 GENMASK(31, 24)
  121. /* define bit use in NFC_ECC_CTL */
  122. #define NFC_ECC_EN BIT(0)
  123. #define NFC_ECC_PIPELINE BIT(3)
  124. #define NFC_ECC_EXCEPTION BIT(4)
  125. #define NFC_ECC_BLOCK_SIZE BIT(5)
  126. #define NFC_RANDOM_EN BIT(9)
  127. #define NFC_RANDOM_DIRECTION BIT(10)
  128. #define NFC_ECC_MODE_SHIFT 12
  129. #define NFC_ECC_MODE GENMASK(15, 12)
  130. #define NFC_RANDOM_SEED GENMASK(30, 16)
  131. #define NFC_DEFAULT_TIMEOUT_MS 1000
  132. #define NFC_SRAM_SIZE 1024
  133. #define NFC_MAX_CS 7
  134. /*
  135. * Ready/Busy detection type: describes the Ready/Busy detection modes
  136. *
  137. * @RB_NONE: no external detection available, rely on STATUS command
  138. * and software timeouts
  139. * @RB_NATIVE: use sunxi NAND controller Ready/Busy support. The Ready/Busy
  140. * pin of the NAND flash chip must be connected to one of the
  141. * native NAND R/B pins (those which can be muxed to the NAND
  142. * Controller)
  143. * @RB_GPIO: use a simple GPIO to handle Ready/Busy status. The Ready/Busy
  144. * pin of the NAND flash chip must be connected to a GPIO capable
  145. * pin.
  146. */
  147. enum sunxi_nand_rb_type {
  148. RB_NONE,
  149. RB_NATIVE,
  150. RB_GPIO,
  151. };
  152. /*
  153. * Ready/Busy structure: stores information related to Ready/Busy detection
  154. *
  155. * @type: the Ready/Busy detection mode
  156. * @info: information related to the R/B detection mode. Either a gpio
  157. * id or a native R/B id (those supported by the NAND controller).
  158. */
  159. struct sunxi_nand_rb {
  160. enum sunxi_nand_rb_type type;
  161. union {
  162. int gpio;
  163. int nativeid;
  164. } info;
  165. };
  166. /*
  167. * Chip Select structure: stores information related to NAND Chip Select
  168. *
  169. * @cs: the NAND CS id used to communicate with a NAND Chip
  170. * @rb: the Ready/Busy description
  171. */
  172. struct sunxi_nand_chip_sel {
  173. u8 cs;
  174. struct sunxi_nand_rb rb;
  175. };
  176. /*
  177. * sunxi HW ECC infos: stores information related to HW ECC support
  178. *
  179. * @mode: the sunxi ECC mode field deduced from ECC requirements
  180. * @layout: the OOB layout depending on the ECC requirements and the
  181. * selected ECC mode
  182. */
  183. struct sunxi_nand_hw_ecc {
  184. int mode;
  185. struct nand_ecclayout layout;
  186. };
  187. /*
  188. * NAND chip structure: stores NAND chip device related information
  189. *
  190. * @node: used to store NAND chips into a list
  191. * @nand: base NAND chip structure
  192. * @mtd: base MTD structure
  193. * @clk_rate: clk_rate required for this NAND chip
  194. * @selected: current active CS
  195. * @nsels: number of CS lines required by the NAND chip
  196. * @sels: array of CS lines descriptions
  197. */
  198. struct sunxi_nand_chip {
  199. struct list_head node;
  200. struct nand_chip nand;
  201. struct mtd_info mtd;
  202. unsigned long clk_rate;
  203. int selected;
  204. int nsels;
  205. struct sunxi_nand_chip_sel sels[0];
  206. };
  207. static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand)
  208. {
  209. return container_of(nand, struct sunxi_nand_chip, nand);
  210. }
  211. /*
  212. * NAND Controller structure: stores sunxi NAND controller information
  213. *
  214. * @controller: base controller structure
  215. * @dev: parent device (used to print error messages)
  216. * @regs: NAND controller registers
  217. * @ahb_clk: NAND Controller AHB clock
  218. * @mod_clk: NAND Controller mod clock
  219. * @assigned_cs: bitmask describing already assigned CS lines
  220. * @clk_rate: NAND controller current clock rate
  221. * @chips: a list containing all the NAND chips attached to
  222. * this NAND controller
  223. * @complete: a completion object used to wait for NAND
  224. * controller events
  225. */
  226. struct sunxi_nfc {
  227. struct nand_hw_control controller;
  228. struct device *dev;
  229. void __iomem *regs;
  230. struct clk *ahb_clk;
  231. struct clk *mod_clk;
  232. unsigned long assigned_cs;
  233. unsigned long clk_rate;
  234. struct list_head chips;
  235. struct completion complete;
  236. };
  237. static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_hw_control *ctrl)
  238. {
  239. return container_of(ctrl, struct sunxi_nfc, controller);
  240. }
  241. static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id)
  242. {
  243. struct sunxi_nfc *nfc = dev_id;
  244. u32 st = readl(nfc->regs + NFC_REG_ST);
  245. u32 ien = readl(nfc->regs + NFC_REG_INT);
  246. if (!(ien & st))
  247. return IRQ_NONE;
  248. if ((ien & st) == ien)
  249. complete(&nfc->complete);
  250. writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
  251. writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT);
  252. return IRQ_HANDLED;
  253. }
  254. static int sunxi_nfc_wait_int(struct sunxi_nfc *nfc, u32 flags,
  255. unsigned int timeout_ms)
  256. {
  257. init_completion(&nfc->complete);
  258. writel(flags, nfc->regs + NFC_REG_INT);
  259. if (!timeout_ms)
  260. timeout_ms = NFC_DEFAULT_TIMEOUT_MS;
  261. if (!wait_for_completion_timeout(&nfc->complete,
  262. msecs_to_jiffies(timeout_ms))) {
  263. dev_err(nfc->dev, "wait interrupt timedout\n");
  264. return -ETIMEDOUT;
  265. }
  266. return 0;
  267. }
  268. static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc)
  269. {
  270. unsigned long timeout = jiffies +
  271. msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS);
  272. do {
  273. if (!(readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
  274. return 0;
  275. } while (time_before(jiffies, timeout));
  276. dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n");
  277. return -ETIMEDOUT;
  278. }
  279. static int sunxi_nfc_rst(struct sunxi_nfc *nfc)
  280. {
  281. unsigned long timeout = jiffies +
  282. msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS);
  283. writel(0, nfc->regs + NFC_REG_ECC_CTL);
  284. writel(NFC_RESET, nfc->regs + NFC_REG_CTL);
  285. do {
  286. if (!(readl(nfc->regs + NFC_REG_CTL) & NFC_RESET))
  287. return 0;
  288. } while (time_before(jiffies, timeout));
  289. dev_err(nfc->dev, "wait for NAND controller reset timedout\n");
  290. return -ETIMEDOUT;
  291. }
  292. static int sunxi_nfc_dev_ready(struct mtd_info *mtd)
  293. {
  294. struct nand_chip *nand = mtd->priv;
  295. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  296. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  297. struct sunxi_nand_rb *rb;
  298. unsigned long timeo = (sunxi_nand->nand.state == FL_ERASING ? 400 : 20);
  299. int ret;
  300. if (sunxi_nand->selected < 0)
  301. return 0;
  302. rb = &sunxi_nand->sels[sunxi_nand->selected].rb;
  303. switch (rb->type) {
  304. case RB_NATIVE:
  305. ret = !!(readl(nfc->regs + NFC_REG_ST) &
  306. (NFC_RB_STATE0 << rb->info.nativeid));
  307. if (ret)
  308. break;
  309. sunxi_nfc_wait_int(nfc, NFC_RB_B2R, timeo);
  310. ret = !!(readl(nfc->regs + NFC_REG_ST) &
  311. (NFC_RB_STATE0 << rb->info.nativeid));
  312. break;
  313. case RB_GPIO:
  314. ret = gpio_get_value(rb->info.gpio);
  315. break;
  316. case RB_NONE:
  317. default:
  318. ret = 0;
  319. dev_err(nfc->dev, "cannot check R/B NAND status!\n");
  320. break;
  321. }
  322. return ret;
  323. }
  324. static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip)
  325. {
  326. struct nand_chip *nand = mtd->priv;
  327. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  328. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  329. struct sunxi_nand_chip_sel *sel;
  330. u32 ctl;
  331. if (chip > 0 && chip >= sunxi_nand->nsels)
  332. return;
  333. if (chip == sunxi_nand->selected)
  334. return;
  335. ctl = readl(nfc->regs + NFC_REG_CTL) &
  336. ~(NFC_CE_SEL | NFC_RB_SEL | NFC_EN);
  337. if (chip >= 0) {
  338. sel = &sunxi_nand->sels[chip];
  339. ctl |= (sel->cs << 24) | NFC_EN |
  340. (((nand->page_shift - 10) & 0xf) << 8);
  341. if (sel->rb.type == RB_NONE) {
  342. nand->dev_ready = NULL;
  343. } else {
  344. nand->dev_ready = sunxi_nfc_dev_ready;
  345. if (sel->rb.type == RB_NATIVE)
  346. ctl |= (sel->rb.info.nativeid << 3);
  347. }
  348. writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA);
  349. if (nfc->clk_rate != sunxi_nand->clk_rate) {
  350. clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate);
  351. nfc->clk_rate = sunxi_nand->clk_rate;
  352. }
  353. }
  354. writel(ctl, nfc->regs + NFC_REG_CTL);
  355. sunxi_nand->selected = chip;
  356. }
  357. static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  358. {
  359. struct nand_chip *nand = mtd->priv;
  360. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  361. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  362. int ret;
  363. int cnt;
  364. int offs = 0;
  365. u32 tmp;
  366. while (len > offs) {
  367. cnt = min(len - offs, NFC_SRAM_SIZE);
  368. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  369. if (ret)
  370. break;
  371. writel(cnt, nfc->regs + NFC_REG_CNT);
  372. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD;
  373. writel(tmp, nfc->regs + NFC_REG_CMD);
  374. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  375. if (ret)
  376. break;
  377. if (buf)
  378. memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE,
  379. cnt);
  380. offs += cnt;
  381. }
  382. }
  383. static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  384. int len)
  385. {
  386. struct nand_chip *nand = mtd->priv;
  387. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  388. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  389. int ret;
  390. int cnt;
  391. int offs = 0;
  392. u32 tmp;
  393. while (len > offs) {
  394. cnt = min(len - offs, NFC_SRAM_SIZE);
  395. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  396. if (ret)
  397. break;
  398. writel(cnt, nfc->regs + NFC_REG_CNT);
  399. memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt);
  400. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  401. NFC_ACCESS_DIR;
  402. writel(tmp, nfc->regs + NFC_REG_CMD);
  403. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  404. if (ret)
  405. break;
  406. offs += cnt;
  407. }
  408. }
  409. static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd)
  410. {
  411. uint8_t ret;
  412. sunxi_nfc_read_buf(mtd, &ret, 1);
  413. return ret;
  414. }
  415. static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
  416. unsigned int ctrl)
  417. {
  418. struct nand_chip *nand = mtd->priv;
  419. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  420. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  421. int ret;
  422. u32 tmp;
  423. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  424. if (ret)
  425. return;
  426. if (ctrl & NAND_CTRL_CHANGE) {
  427. tmp = readl(nfc->regs + NFC_REG_CTL);
  428. if (ctrl & NAND_NCE)
  429. tmp |= NFC_CE_CTL;
  430. else
  431. tmp &= ~NFC_CE_CTL;
  432. writel(tmp, nfc->regs + NFC_REG_CTL);
  433. }
  434. if (dat == NAND_CMD_NONE)
  435. return;
  436. if (ctrl & NAND_CLE) {
  437. writel(NFC_SEND_CMD1 | dat, nfc->regs + NFC_REG_CMD);
  438. } else {
  439. writel(dat, nfc->regs + NFC_REG_ADDR_LOW);
  440. writel(NFC_SEND_ADR, nfc->regs + NFC_REG_CMD);
  441. }
  442. sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  443. }
  444. static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd,
  445. struct nand_chip *chip, uint8_t *buf,
  446. int oob_required, int page)
  447. {
  448. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
  449. struct nand_ecc_ctrl *ecc = &chip->ecc;
  450. struct nand_ecclayout *layout = ecc->layout;
  451. struct sunxi_nand_hw_ecc *data = ecc->priv;
  452. unsigned int max_bitflips = 0;
  453. int offset;
  454. int ret;
  455. u32 tmp;
  456. int i;
  457. int cnt;
  458. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  459. tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
  460. tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
  461. NFC_ECC_EXCEPTION;
  462. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  463. for (i = 0; i < ecc->steps; i++) {
  464. if (i)
  465. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i * ecc->size, -1);
  466. offset = mtd->writesize + layout->eccpos[i * ecc->bytes] - 4;
  467. chip->read_buf(mtd, NULL, ecc->size);
  468. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  469. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  470. if (ret)
  471. return ret;
  472. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | (1 << 30);
  473. writel(tmp, nfc->regs + NFC_REG_CMD);
  474. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  475. if (ret)
  476. return ret;
  477. memcpy_fromio(buf + (i * ecc->size),
  478. nfc->regs + NFC_RAM0_BASE, ecc->size);
  479. if (readl(nfc->regs + NFC_REG_ECC_ST) & 0x1) {
  480. mtd->ecc_stats.failed++;
  481. } else {
  482. tmp = readl(nfc->regs + NFC_REG_ECC_CNT0) & 0xff;
  483. mtd->ecc_stats.corrected += tmp;
  484. max_bitflips = max_t(unsigned int, max_bitflips, tmp);
  485. }
  486. if (oob_required) {
  487. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  488. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  489. if (ret)
  490. return ret;
  491. offset -= mtd->writesize;
  492. chip->read_buf(mtd, chip->oob_poi + offset,
  493. ecc->bytes + 4);
  494. }
  495. }
  496. if (oob_required) {
  497. cnt = ecc->layout->oobfree[ecc->steps].length;
  498. if (cnt > 0) {
  499. offset = mtd->writesize +
  500. ecc->layout->oobfree[ecc->steps].offset;
  501. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  502. offset -= mtd->writesize;
  503. chip->read_buf(mtd, chip->oob_poi + offset, cnt);
  504. }
  505. }
  506. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  507. tmp &= ~NFC_ECC_EN;
  508. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  509. return max_bitflips;
  510. }
  511. static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
  512. struct nand_chip *chip,
  513. const uint8_t *buf, int oob_required)
  514. {
  515. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
  516. struct nand_ecc_ctrl *ecc = &chip->ecc;
  517. struct nand_ecclayout *layout = ecc->layout;
  518. struct sunxi_nand_hw_ecc *data = ecc->priv;
  519. int offset;
  520. int ret;
  521. u32 tmp;
  522. int i;
  523. int cnt;
  524. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  525. tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
  526. tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
  527. NFC_ECC_EXCEPTION;
  528. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  529. for (i = 0; i < ecc->steps; i++) {
  530. if (i)
  531. chip->cmdfunc(mtd, NAND_CMD_RNDIN, i * ecc->size, -1);
  532. chip->write_buf(mtd, buf + (i * ecc->size), ecc->size);
  533. offset = layout->eccpos[i * ecc->bytes] - 4 + mtd->writesize;
  534. /* Fill OOB data in */
  535. if (oob_required) {
  536. tmp = 0xffffffff;
  537. memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
  538. 4);
  539. } else {
  540. memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
  541. chip->oob_poi + offset - mtd->writesize,
  542. 4);
  543. }
  544. chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
  545. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  546. if (ret)
  547. return ret;
  548. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ACCESS_DIR |
  549. (1 << 30);
  550. writel(tmp, nfc->regs + NFC_REG_CMD);
  551. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  552. if (ret)
  553. return ret;
  554. }
  555. if (oob_required) {
  556. cnt = ecc->layout->oobfree[i].length;
  557. if (cnt > 0) {
  558. offset = mtd->writesize +
  559. ecc->layout->oobfree[i].offset;
  560. chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
  561. offset -= mtd->writesize;
  562. chip->write_buf(mtd, chip->oob_poi + offset, cnt);
  563. }
  564. }
  565. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  566. tmp &= ~NFC_ECC_EN;
  567. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  568. return 0;
  569. }
  570. static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info *mtd,
  571. struct nand_chip *chip,
  572. uint8_t *buf, int oob_required,
  573. int page)
  574. {
  575. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
  576. struct nand_ecc_ctrl *ecc = &chip->ecc;
  577. struct sunxi_nand_hw_ecc *data = ecc->priv;
  578. unsigned int max_bitflips = 0;
  579. uint8_t *oob = chip->oob_poi;
  580. int offset = 0;
  581. int ret;
  582. int cnt;
  583. u32 tmp;
  584. int i;
  585. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  586. tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
  587. tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
  588. NFC_ECC_EXCEPTION;
  589. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  590. for (i = 0; i < ecc->steps; i++) {
  591. chip->read_buf(mtd, NULL, ecc->size);
  592. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | (1 << 30);
  593. writel(tmp, nfc->regs + NFC_REG_CMD);
  594. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  595. if (ret)
  596. return ret;
  597. memcpy_fromio(buf, nfc->regs + NFC_RAM0_BASE, ecc->size);
  598. buf += ecc->size;
  599. offset += ecc->size;
  600. if (readl(nfc->regs + NFC_REG_ECC_ST) & 0x1) {
  601. mtd->ecc_stats.failed++;
  602. } else {
  603. tmp = readl(nfc->regs + NFC_REG_ECC_CNT0) & 0xff;
  604. mtd->ecc_stats.corrected += tmp;
  605. max_bitflips = max_t(unsigned int, max_bitflips, tmp);
  606. }
  607. if (oob_required) {
  608. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  609. chip->read_buf(mtd, oob, ecc->bytes + ecc->prepad);
  610. oob += ecc->bytes + ecc->prepad;
  611. }
  612. offset += ecc->bytes + ecc->prepad;
  613. }
  614. if (oob_required) {
  615. cnt = mtd->oobsize - (oob - chip->oob_poi);
  616. if (cnt > 0) {
  617. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  618. chip->read_buf(mtd, oob, cnt);
  619. }
  620. }
  621. writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
  622. nfc->regs + NFC_REG_ECC_CTL);
  623. return max_bitflips;
  624. }
  625. static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd,
  626. struct nand_chip *chip,
  627. const uint8_t *buf,
  628. int oob_required)
  629. {
  630. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
  631. struct nand_ecc_ctrl *ecc = &chip->ecc;
  632. struct sunxi_nand_hw_ecc *data = ecc->priv;
  633. uint8_t *oob = chip->oob_poi;
  634. int offset = 0;
  635. int ret;
  636. int cnt;
  637. u32 tmp;
  638. int i;
  639. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  640. tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
  641. tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
  642. NFC_ECC_EXCEPTION;
  643. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  644. for (i = 0; i < ecc->steps; i++) {
  645. chip->write_buf(mtd, buf + (i * ecc->size), ecc->size);
  646. offset += ecc->size;
  647. /* Fill OOB data in */
  648. if (oob_required) {
  649. tmp = 0xffffffff;
  650. memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
  651. 4);
  652. } else {
  653. memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, oob,
  654. 4);
  655. }
  656. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ACCESS_DIR |
  657. (1 << 30);
  658. writel(tmp, nfc->regs + NFC_REG_CMD);
  659. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  660. if (ret)
  661. return ret;
  662. offset += ecc->bytes + ecc->prepad;
  663. oob += ecc->bytes + ecc->prepad;
  664. }
  665. if (oob_required) {
  666. cnt = mtd->oobsize - (oob - chip->oob_poi);
  667. if (cnt > 0) {
  668. chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
  669. chip->write_buf(mtd, oob, cnt);
  670. }
  671. }
  672. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  673. tmp &= ~NFC_ECC_EN;
  674. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  675. return 0;
  676. }
  677. static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip *chip,
  678. const struct nand_sdr_timings *timings)
  679. {
  680. u32 min_clk_period = 0;
  681. /* T1 <=> tCLS */
  682. if (timings->tCLS_min > min_clk_period)
  683. min_clk_period = timings->tCLS_min;
  684. /* T2 <=> tCLH */
  685. if (timings->tCLH_min > min_clk_period)
  686. min_clk_period = timings->tCLH_min;
  687. /* T3 <=> tCS */
  688. if (timings->tCS_min > min_clk_period)
  689. min_clk_period = timings->tCS_min;
  690. /* T4 <=> tCH */
  691. if (timings->tCH_min > min_clk_period)
  692. min_clk_period = timings->tCH_min;
  693. /* T5 <=> tWP */
  694. if (timings->tWP_min > min_clk_period)
  695. min_clk_period = timings->tWP_min;
  696. /* T6 <=> tWH */
  697. if (timings->tWH_min > min_clk_period)
  698. min_clk_period = timings->tWH_min;
  699. /* T7 <=> tALS */
  700. if (timings->tALS_min > min_clk_period)
  701. min_clk_period = timings->tALS_min;
  702. /* T8 <=> tDS */
  703. if (timings->tDS_min > min_clk_period)
  704. min_clk_period = timings->tDS_min;
  705. /* T9 <=> tDH */
  706. if (timings->tDH_min > min_clk_period)
  707. min_clk_period = timings->tDH_min;
  708. /* T10 <=> tRR */
  709. if (timings->tRR_min > (min_clk_period * 3))
  710. min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3);
  711. /* T11 <=> tALH */
  712. if (timings->tALH_min > min_clk_period)
  713. min_clk_period = timings->tALH_min;
  714. /* T12 <=> tRP */
  715. if (timings->tRP_min > min_clk_period)
  716. min_clk_period = timings->tRP_min;
  717. /* T13 <=> tREH */
  718. if (timings->tREH_min > min_clk_period)
  719. min_clk_period = timings->tREH_min;
  720. /* T14 <=> tRC */
  721. if (timings->tRC_min > (min_clk_period * 2))
  722. min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2);
  723. /* T15 <=> tWC */
  724. if (timings->tWC_min > (min_clk_period * 2))
  725. min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2);
  726. /* Convert min_clk_period from picoseconds to nanoseconds */
  727. min_clk_period = DIV_ROUND_UP(min_clk_period, 1000);
  728. /*
  729. * Convert min_clk_period into a clk frequency, then get the
  730. * appropriate rate for the NAND controller IP given this formula
  731. * (specified in the datasheet):
  732. * nand clk_rate = 2 * min_clk_rate
  733. */
  734. chip->clk_rate = (2 * NSEC_PER_SEC) / min_clk_period;
  735. /* TODO: configure T16-T19 */
  736. return 0;
  737. }
  738. static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip *chip,
  739. struct device_node *np)
  740. {
  741. const struct nand_sdr_timings *timings;
  742. int ret;
  743. int mode;
  744. mode = onfi_get_async_timing_mode(&chip->nand);
  745. if (mode == ONFI_TIMING_MODE_UNKNOWN) {
  746. mode = chip->nand.onfi_timing_mode_default;
  747. } else {
  748. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {};
  749. mode = fls(mode) - 1;
  750. if (mode < 0)
  751. mode = 0;
  752. feature[0] = mode;
  753. ret = chip->nand.onfi_set_features(&chip->mtd, &chip->nand,
  754. ONFI_FEATURE_ADDR_TIMING_MODE,
  755. feature);
  756. if (ret)
  757. return ret;
  758. }
  759. timings = onfi_async_timing_mode_to_sdr_timings(mode);
  760. if (IS_ERR(timings))
  761. return PTR_ERR(timings);
  762. return sunxi_nand_chip_set_timings(chip, timings);
  763. }
  764. static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info *mtd,
  765. struct nand_ecc_ctrl *ecc,
  766. struct device_node *np)
  767. {
  768. static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
  769. struct nand_chip *nand = mtd->priv;
  770. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  771. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  772. struct sunxi_nand_hw_ecc *data;
  773. struct nand_ecclayout *layout;
  774. int nsectors;
  775. int ret;
  776. int i;
  777. data = kzalloc(sizeof(*data), GFP_KERNEL);
  778. if (!data)
  779. return -ENOMEM;
  780. /* Add ECC info retrieval from DT */
  781. for (i = 0; i < ARRAY_SIZE(strengths); i++) {
  782. if (ecc->strength <= strengths[i])
  783. break;
  784. }
  785. if (i >= ARRAY_SIZE(strengths)) {
  786. dev_err(nfc->dev, "unsupported strength\n");
  787. ret = -ENOTSUPP;
  788. goto err;
  789. }
  790. data->mode = i;
  791. /* HW ECC always request ECC bytes for 1024 bytes blocks */
  792. ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8);
  793. /* HW ECC always work with even numbers of ECC bytes */
  794. ecc->bytes = ALIGN(ecc->bytes, 2);
  795. layout = &data->layout;
  796. nsectors = mtd->writesize / ecc->size;
  797. if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) {
  798. ret = -EINVAL;
  799. goto err;
  800. }
  801. layout->eccbytes = (ecc->bytes * nsectors);
  802. ecc->layout = layout;
  803. ecc->priv = data;
  804. return 0;
  805. err:
  806. kfree(data);
  807. return ret;
  808. }
  809. static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc)
  810. {
  811. kfree(ecc->priv);
  812. }
  813. static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
  814. struct nand_ecc_ctrl *ecc,
  815. struct device_node *np)
  816. {
  817. struct nand_ecclayout *layout;
  818. int nsectors;
  819. int i, j;
  820. int ret;
  821. ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np);
  822. if (ret)
  823. return ret;
  824. ecc->read_page = sunxi_nfc_hw_ecc_read_page;
  825. ecc->write_page = sunxi_nfc_hw_ecc_write_page;
  826. layout = ecc->layout;
  827. nsectors = mtd->writesize / ecc->size;
  828. for (i = 0; i < nsectors; i++) {
  829. if (i) {
  830. layout->oobfree[i].offset =
  831. layout->oobfree[i - 1].offset +
  832. layout->oobfree[i - 1].length +
  833. ecc->bytes;
  834. layout->oobfree[i].length = 4;
  835. } else {
  836. /*
  837. * The first 2 bytes are used for BB markers, hence we
  838. * only have 2 bytes available in the first user data
  839. * section.
  840. */
  841. layout->oobfree[i].length = 2;
  842. layout->oobfree[i].offset = 2;
  843. }
  844. for (j = 0; j < ecc->bytes; j++)
  845. layout->eccpos[(ecc->bytes * i) + j] =
  846. layout->oobfree[i].offset +
  847. layout->oobfree[i].length + j;
  848. }
  849. if (mtd->oobsize > (ecc->bytes + 4) * nsectors) {
  850. layout->oobfree[nsectors].offset =
  851. layout->oobfree[nsectors - 1].offset +
  852. layout->oobfree[nsectors - 1].length +
  853. ecc->bytes;
  854. layout->oobfree[nsectors].length = mtd->oobsize -
  855. ((ecc->bytes + 4) * nsectors);
  856. }
  857. return 0;
  858. }
  859. static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info *mtd,
  860. struct nand_ecc_ctrl *ecc,
  861. struct device_node *np)
  862. {
  863. struct nand_ecclayout *layout;
  864. int nsectors;
  865. int i;
  866. int ret;
  867. ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np);
  868. if (ret)
  869. return ret;
  870. ecc->prepad = 4;
  871. ecc->read_page = sunxi_nfc_hw_syndrome_ecc_read_page;
  872. ecc->write_page = sunxi_nfc_hw_syndrome_ecc_write_page;
  873. layout = ecc->layout;
  874. nsectors = mtd->writesize / ecc->size;
  875. for (i = 0; i < (ecc->bytes * nsectors); i++)
  876. layout->eccpos[i] = i;
  877. layout->oobfree[0].length = mtd->oobsize - i;
  878. layout->oobfree[0].offset = i;
  879. return 0;
  880. }
  881. static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc)
  882. {
  883. switch (ecc->mode) {
  884. case NAND_ECC_HW:
  885. case NAND_ECC_HW_SYNDROME:
  886. sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc);
  887. break;
  888. case NAND_ECC_NONE:
  889. kfree(ecc->layout);
  890. default:
  891. break;
  892. }
  893. }
  894. static int sunxi_nand_ecc_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc,
  895. struct device_node *np)
  896. {
  897. struct nand_chip *nand = mtd->priv;
  898. int strength;
  899. int blk_size;
  900. int ret;
  901. blk_size = of_get_nand_ecc_step_size(np);
  902. strength = of_get_nand_ecc_strength(np);
  903. if (blk_size > 0 && strength > 0) {
  904. ecc->size = blk_size;
  905. ecc->strength = strength;
  906. } else {
  907. ecc->size = nand->ecc_step_ds;
  908. ecc->strength = nand->ecc_strength_ds;
  909. }
  910. if (!ecc->size || !ecc->strength)
  911. return -EINVAL;
  912. ecc->mode = NAND_ECC_HW;
  913. ret = of_get_nand_ecc_mode(np);
  914. if (ret >= 0)
  915. ecc->mode = ret;
  916. switch (ecc->mode) {
  917. case NAND_ECC_SOFT_BCH:
  918. break;
  919. case NAND_ECC_HW:
  920. ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc, np);
  921. if (ret)
  922. return ret;
  923. break;
  924. case NAND_ECC_HW_SYNDROME:
  925. ret = sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd, ecc, np);
  926. if (ret)
  927. return ret;
  928. break;
  929. case NAND_ECC_NONE:
  930. ecc->layout = kzalloc(sizeof(*ecc->layout), GFP_KERNEL);
  931. if (!ecc->layout)
  932. return -ENOMEM;
  933. ecc->layout->oobfree[0].length = mtd->oobsize;
  934. case NAND_ECC_SOFT:
  935. break;
  936. default:
  937. return -EINVAL;
  938. }
  939. return 0;
  940. }
  941. static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
  942. struct device_node *np)
  943. {
  944. const struct nand_sdr_timings *timings;
  945. struct sunxi_nand_chip *chip;
  946. struct mtd_part_parser_data ppdata;
  947. struct mtd_info *mtd;
  948. struct nand_chip *nand;
  949. int nsels;
  950. int ret;
  951. int i;
  952. u32 tmp;
  953. if (!of_get_property(np, "reg", &nsels))
  954. return -EINVAL;
  955. nsels /= sizeof(u32);
  956. if (!nsels) {
  957. dev_err(dev, "invalid reg property size\n");
  958. return -EINVAL;
  959. }
  960. chip = devm_kzalloc(dev,
  961. sizeof(*chip) +
  962. (nsels * sizeof(struct sunxi_nand_chip_sel)),
  963. GFP_KERNEL);
  964. if (!chip) {
  965. dev_err(dev, "could not allocate chip\n");
  966. return -ENOMEM;
  967. }
  968. chip->nsels = nsels;
  969. chip->selected = -1;
  970. for (i = 0; i < nsels; i++) {
  971. ret = of_property_read_u32_index(np, "reg", i, &tmp);
  972. if (ret) {
  973. dev_err(dev, "could not retrieve reg property: %d\n",
  974. ret);
  975. return ret;
  976. }
  977. if (tmp > NFC_MAX_CS) {
  978. dev_err(dev,
  979. "invalid reg value: %u (max CS = 7)\n",
  980. tmp);
  981. return -EINVAL;
  982. }
  983. if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
  984. dev_err(dev, "CS %d already assigned\n", tmp);
  985. return -EINVAL;
  986. }
  987. chip->sels[i].cs = tmp;
  988. if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) &&
  989. tmp < 2) {
  990. chip->sels[i].rb.type = RB_NATIVE;
  991. chip->sels[i].rb.info.nativeid = tmp;
  992. } else {
  993. ret = of_get_named_gpio(np, "rb-gpios", i);
  994. if (ret >= 0) {
  995. tmp = ret;
  996. chip->sels[i].rb.type = RB_GPIO;
  997. chip->sels[i].rb.info.gpio = tmp;
  998. ret = devm_gpio_request(dev, tmp, "nand-rb");
  999. if (ret)
  1000. return ret;
  1001. ret = gpio_direction_input(tmp);
  1002. if (ret)
  1003. return ret;
  1004. } else {
  1005. chip->sels[i].rb.type = RB_NONE;
  1006. }
  1007. }
  1008. }
  1009. timings = onfi_async_timing_mode_to_sdr_timings(0);
  1010. if (IS_ERR(timings)) {
  1011. ret = PTR_ERR(timings);
  1012. dev_err(dev,
  1013. "could not retrieve timings for ONFI mode 0: %d\n",
  1014. ret);
  1015. return ret;
  1016. }
  1017. ret = sunxi_nand_chip_set_timings(chip, timings);
  1018. if (ret) {
  1019. dev_err(dev, "could not configure chip timings: %d\n", ret);
  1020. return ret;
  1021. }
  1022. nand = &chip->nand;
  1023. /* Default tR value specified in the ONFI spec (chapter 4.15.1) */
  1024. nand->chip_delay = 200;
  1025. nand->controller = &nfc->controller;
  1026. nand->select_chip = sunxi_nfc_select_chip;
  1027. nand->cmd_ctrl = sunxi_nfc_cmd_ctrl;
  1028. nand->read_buf = sunxi_nfc_read_buf;
  1029. nand->write_buf = sunxi_nfc_write_buf;
  1030. nand->read_byte = sunxi_nfc_read_byte;
  1031. if (of_get_nand_on_flash_bbt(np))
  1032. nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  1033. mtd = &chip->mtd;
  1034. mtd->dev.parent = dev;
  1035. mtd->priv = nand;
  1036. mtd->owner = THIS_MODULE;
  1037. ret = nand_scan_ident(mtd, nsels, NULL);
  1038. if (ret)
  1039. return ret;
  1040. ret = sunxi_nand_chip_init_timings(chip, np);
  1041. if (ret) {
  1042. dev_err(dev, "could not configure chip timings: %d\n", ret);
  1043. return ret;
  1044. }
  1045. ret = sunxi_nand_ecc_init(mtd, &nand->ecc, np);
  1046. if (ret) {
  1047. dev_err(dev, "ECC init failed: %d\n", ret);
  1048. return ret;
  1049. }
  1050. ret = nand_scan_tail(mtd);
  1051. if (ret) {
  1052. dev_err(dev, "nand_scan_tail failed: %d\n", ret);
  1053. return ret;
  1054. }
  1055. ppdata.of_node = np;
  1056. ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  1057. if (ret) {
  1058. dev_err(dev, "failed to register mtd device: %d\n", ret);
  1059. nand_release(mtd);
  1060. return ret;
  1061. }
  1062. list_add_tail(&chip->node, &nfc->chips);
  1063. return 0;
  1064. }
  1065. static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
  1066. {
  1067. struct device_node *np = dev->of_node;
  1068. struct device_node *nand_np;
  1069. int nchips = of_get_child_count(np);
  1070. int ret;
  1071. if (nchips > 8) {
  1072. dev_err(dev, "too many NAND chips: %d (max = 8)\n", nchips);
  1073. return -EINVAL;
  1074. }
  1075. for_each_child_of_node(np, nand_np) {
  1076. ret = sunxi_nand_chip_init(dev, nfc, nand_np);
  1077. if (ret)
  1078. return ret;
  1079. }
  1080. return 0;
  1081. }
  1082. static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
  1083. {
  1084. struct sunxi_nand_chip *chip;
  1085. while (!list_empty(&nfc->chips)) {
  1086. chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip,
  1087. node);
  1088. nand_release(&chip->mtd);
  1089. sunxi_nand_ecc_cleanup(&chip->nand.ecc);
  1090. }
  1091. }
  1092. static int sunxi_nfc_probe(struct platform_device *pdev)
  1093. {
  1094. struct device *dev = &pdev->dev;
  1095. struct resource *r;
  1096. struct sunxi_nfc *nfc;
  1097. int irq;
  1098. int ret;
  1099. nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL);
  1100. if (!nfc)
  1101. return -ENOMEM;
  1102. nfc->dev = dev;
  1103. spin_lock_init(&nfc->controller.lock);
  1104. init_waitqueue_head(&nfc->controller.wq);
  1105. INIT_LIST_HEAD(&nfc->chips);
  1106. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1107. nfc->regs = devm_ioremap_resource(dev, r);
  1108. if (IS_ERR(nfc->regs))
  1109. return PTR_ERR(nfc->regs);
  1110. irq = platform_get_irq(pdev, 0);
  1111. if (irq < 0) {
  1112. dev_err(dev, "failed to retrieve irq\n");
  1113. return irq;
  1114. }
  1115. nfc->ahb_clk = devm_clk_get(dev, "ahb");
  1116. if (IS_ERR(nfc->ahb_clk)) {
  1117. dev_err(dev, "failed to retrieve ahb clk\n");
  1118. return PTR_ERR(nfc->ahb_clk);
  1119. }
  1120. ret = clk_prepare_enable(nfc->ahb_clk);
  1121. if (ret)
  1122. return ret;
  1123. nfc->mod_clk = devm_clk_get(dev, "mod");
  1124. if (IS_ERR(nfc->mod_clk)) {
  1125. dev_err(dev, "failed to retrieve mod clk\n");
  1126. ret = PTR_ERR(nfc->mod_clk);
  1127. goto out_ahb_clk_unprepare;
  1128. }
  1129. ret = clk_prepare_enable(nfc->mod_clk);
  1130. if (ret)
  1131. goto out_ahb_clk_unprepare;
  1132. ret = sunxi_nfc_rst(nfc);
  1133. if (ret)
  1134. goto out_mod_clk_unprepare;
  1135. writel(0, nfc->regs + NFC_REG_INT);
  1136. ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt,
  1137. 0, "sunxi-nand", nfc);
  1138. if (ret)
  1139. goto out_mod_clk_unprepare;
  1140. platform_set_drvdata(pdev, nfc);
  1141. /*
  1142. * TODO: replace these magic values with proper flags as soon as we
  1143. * know what they are encoding.
  1144. */
  1145. writel(0x100, nfc->regs + NFC_REG_TIMING_CTL);
  1146. writel(0x7ff, nfc->regs + NFC_REG_TIMING_CFG);
  1147. ret = sunxi_nand_chips_init(dev, nfc);
  1148. if (ret) {
  1149. dev_err(dev, "failed to init nand chips\n");
  1150. goto out_mod_clk_unprepare;
  1151. }
  1152. return 0;
  1153. out_mod_clk_unprepare:
  1154. clk_disable_unprepare(nfc->mod_clk);
  1155. out_ahb_clk_unprepare:
  1156. clk_disable_unprepare(nfc->ahb_clk);
  1157. return ret;
  1158. }
  1159. static int sunxi_nfc_remove(struct platform_device *pdev)
  1160. {
  1161. struct sunxi_nfc *nfc = platform_get_drvdata(pdev);
  1162. sunxi_nand_chips_cleanup(nfc);
  1163. return 0;
  1164. }
  1165. static const struct of_device_id sunxi_nfc_ids[] = {
  1166. { .compatible = "allwinner,sun4i-a10-nand" },
  1167. { /* sentinel */ }
  1168. };
  1169. MODULE_DEVICE_TABLE(of, sunxi_nfc_ids);
  1170. static struct platform_driver sunxi_nfc_driver = {
  1171. .driver = {
  1172. .name = "sunxi_nand",
  1173. .of_match_table = sunxi_nfc_ids,
  1174. },
  1175. .probe = sunxi_nfc_probe,
  1176. .remove = sunxi_nfc_remove,
  1177. };
  1178. module_platform_driver(sunxi_nfc_driver);
  1179. MODULE_LICENSE("GPL v2");
  1180. MODULE_AUTHOR("Boris BREZILLON");
  1181. MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
  1182. MODULE_ALIAS("platform:sunxi_nand");