mxc_nand.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/nand.h>
  25. #include <linux/mtd/partitions.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/clk.h>
  30. #include <linux/err.h>
  31. #include <linux/io.h>
  32. #include <asm/mach/flash.h>
  33. #include <mach/mxc_nand.h>
  34. #define DRIVER_NAME "mxc_nand"
  35. /* Addresses for NFC registers */
  36. #define NFC_BUF_SIZE 0xE00
  37. #define NFC_BUF_ADDR 0xE04
  38. #define NFC_FLASH_ADDR 0xE06
  39. #define NFC_FLASH_CMD 0xE08
  40. #define NFC_CONFIG 0xE0A
  41. #define NFC_ECC_STATUS_RESULT 0xE0C
  42. #define NFC_RSLTMAIN_AREA 0xE0E
  43. #define NFC_RSLTSPARE_AREA 0xE10
  44. #define NFC_WRPROT 0xE12
  45. #define NFC_UNLOCKSTART_BLKADDR 0xE14
  46. #define NFC_UNLOCKEND_BLKADDR 0xE16
  47. #define NFC_NF_WRPRST 0xE18
  48. #define NFC_CONFIG1 0xE1A
  49. #define NFC_CONFIG2 0xE1C
  50. /* Addresses for NFC RAM BUFFER Main area 0 */
  51. #define MAIN_AREA0 0x000
  52. #define MAIN_AREA1 0x200
  53. #define MAIN_AREA2 0x400
  54. #define MAIN_AREA3 0x600
  55. /* Addresses for NFC SPARE BUFFER Spare area 0 */
  56. #define SPARE_AREA0 0x800
  57. #define SPARE_AREA1 0x810
  58. #define SPARE_AREA2 0x820
  59. #define SPARE_AREA3 0x830
  60. /* Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register
  61. * for Command operation */
  62. #define NFC_CMD 0x1
  63. /* Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register
  64. * for Address operation */
  65. #define NFC_ADDR 0x2
  66. /* Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register
  67. * for Input operation */
  68. #define NFC_INPUT 0x4
  69. /* Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register
  70. * for Data Output operation */
  71. #define NFC_OUTPUT 0x8
  72. /* Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register
  73. * for Read ID operation */
  74. #define NFC_ID 0x10
  75. /* Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register
  76. * for Read Status operation */
  77. #define NFC_STATUS 0x20
  78. /* Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read
  79. * Status operation */
  80. #define NFC_INT 0x8000
  81. #define NFC_SP_EN (1 << 2)
  82. #define NFC_ECC_EN (1 << 3)
  83. #define NFC_INT_MSK (1 << 4)
  84. #define NFC_BIG (1 << 5)
  85. #define NFC_RST (1 << 6)
  86. #define NFC_CE (1 << 7)
  87. #define NFC_ONE_CYCLE (1 << 8)
  88. struct mxc_nand_host {
  89. struct mtd_info mtd;
  90. struct nand_chip nand;
  91. struct mtd_partition *parts;
  92. struct device *dev;
  93. void __iomem *regs;
  94. int spare_only;
  95. int status_request;
  96. int pagesize_2k;
  97. uint16_t col_addr;
  98. struct clk *clk;
  99. int clk_act;
  100. int irq;
  101. wait_queue_head_t irq_waitq;
  102. };
  103. /* Define delays in microsec for NAND device operations */
  104. #define TROP_US_DELAY 2000
  105. /* Macros to get byte and bit positions of ECC */
  106. #define COLPOS(x) ((x) >> 3)
  107. #define BITPOS(x) ((x) & 0xf)
  108. /* Define single bit Error positions in Main & Spare area */
  109. #define MAIN_SINGLEBIT_ERROR 0x4
  110. #define SPARE_SINGLEBIT_ERROR 0x1
  111. /* OOB placement block for use with hardware ecc generation */
  112. static struct nand_ecclayout nand_hw_eccoob_8 = {
  113. .eccbytes = 5,
  114. .eccpos = {6, 7, 8, 9, 10},
  115. .oobfree = {{0, 5}, {11, 5}, }
  116. };
  117. static struct nand_ecclayout nand_hw_eccoob_16 = {
  118. .eccbytes = 5,
  119. .eccpos = {6, 7, 8, 9, 10},
  120. .oobfree = {{0, 5}, {11, 5}, }
  121. };
  122. static struct nand_ecclayout nand_hw_eccoob_64 = {
  123. .eccbytes = 20,
  124. .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
  125. 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
  126. .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
  127. };
  128. #ifdef CONFIG_MTD_PARTITIONS
  129. static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
  130. #endif
  131. static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
  132. {
  133. struct mxc_nand_host *host = dev_id;
  134. uint16_t tmp;
  135. tmp = readw(host->regs + NFC_CONFIG1);
  136. tmp |= NFC_INT_MSK; /* Disable interrupt */
  137. writew(tmp, host->regs + NFC_CONFIG1);
  138. wake_up(&host->irq_waitq);
  139. return IRQ_HANDLED;
  140. }
  141. /* This function polls the NANDFC to wait for the basic operation to
  142. * complete by checking the INT bit of config2 register.
  143. */
  144. static void wait_op_done(struct mxc_nand_host *host, int max_retries,
  145. uint16_t param, int useirq)
  146. {
  147. uint32_t tmp;
  148. if (useirq) {
  149. if ((readw(host->regs + NFC_CONFIG2) & NFC_INT) == 0) {
  150. tmp = readw(host->regs + NFC_CONFIG1);
  151. tmp &= ~NFC_INT_MSK; /* Enable interrupt */
  152. writew(tmp, host->regs + NFC_CONFIG1);
  153. wait_event(host->irq_waitq,
  154. readw(host->regs + NFC_CONFIG2) & NFC_INT);
  155. tmp = readw(host->regs + NFC_CONFIG2);
  156. tmp &= ~NFC_INT;
  157. writew(tmp, host->regs + NFC_CONFIG2);
  158. }
  159. } else {
  160. while (max_retries-- > 0) {
  161. if (readw(host->regs + NFC_CONFIG2) & NFC_INT) {
  162. tmp = readw(host->regs + NFC_CONFIG2);
  163. tmp &= ~NFC_INT;
  164. writew(tmp, host->regs + NFC_CONFIG2);
  165. break;
  166. }
  167. udelay(1);
  168. }
  169. if (max_retries < 0)
  170. DEBUG(MTD_DEBUG_LEVEL0, "%s(%d): INT not set\n",
  171. __func__, param);
  172. }
  173. }
  174. /* This function issues the specified command to the NAND device and
  175. * waits for completion. */
  176. static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  177. {
  178. DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
  179. writew(cmd, host->regs + NFC_FLASH_CMD);
  180. writew(NFC_CMD, host->regs + NFC_CONFIG2);
  181. /* Wait for operation to complete */
  182. wait_op_done(host, TROP_US_DELAY, cmd, useirq);
  183. }
  184. /* This function sends an address (or partial address) to the
  185. * NAND device. The address is used to select the source/destination for
  186. * a NAND command. */
  187. static void send_addr(struct mxc_nand_host *host, uint16_t addr, int islast)
  188. {
  189. DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
  190. writew(addr, host->regs + NFC_FLASH_ADDR);
  191. writew(NFC_ADDR, host->regs + NFC_CONFIG2);
  192. /* Wait for operation to complete */
  193. wait_op_done(host, TROP_US_DELAY, addr, islast);
  194. }
  195. /* This function requests the NANDFC to initate the transfer
  196. * of data currently in the NANDFC RAM buffer to the NAND device. */
  197. static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
  198. int spare_only)
  199. {
  200. DEBUG(MTD_DEBUG_LEVEL3, "send_prog_page (%d)\n", spare_only);
  201. /* NANDFC buffer 0 is used for page read/write */
  202. writew(buf_id, host->regs + NFC_BUF_ADDR);
  203. /* Configure spare or page+spare access */
  204. if (!host->pagesize_2k) {
  205. uint16_t config1 = readw(host->regs + NFC_CONFIG1);
  206. if (spare_only)
  207. config1 |= NFC_SP_EN;
  208. else
  209. config1 &= ~(NFC_SP_EN);
  210. writew(config1, host->regs + NFC_CONFIG1);
  211. }
  212. writew(NFC_INPUT, host->regs + NFC_CONFIG2);
  213. /* Wait for operation to complete */
  214. wait_op_done(host, TROP_US_DELAY, spare_only, true);
  215. }
  216. /* Requests NANDFC to initated the transfer of data from the
  217. * NAND device into in the NANDFC ram buffer. */
  218. static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
  219. int spare_only)
  220. {
  221. DEBUG(MTD_DEBUG_LEVEL3, "send_read_page (%d)\n", spare_only);
  222. /* NANDFC buffer 0 is used for page read/write */
  223. writew(buf_id, host->regs + NFC_BUF_ADDR);
  224. /* Configure spare or page+spare access */
  225. if (!host->pagesize_2k) {
  226. uint32_t config1 = readw(host->regs + NFC_CONFIG1);
  227. if (spare_only)
  228. config1 |= NFC_SP_EN;
  229. else
  230. config1 &= ~NFC_SP_EN;
  231. writew(config1, host->regs + NFC_CONFIG1);
  232. }
  233. writew(NFC_OUTPUT, host->regs + NFC_CONFIG2);
  234. /* Wait for operation to complete */
  235. wait_op_done(host, TROP_US_DELAY, spare_only, true);
  236. }
  237. /* Request the NANDFC to perform a read of the NAND device ID. */
  238. static void send_read_id(struct mxc_nand_host *host)
  239. {
  240. struct nand_chip *this = &host->nand;
  241. uint16_t tmp;
  242. /* NANDFC buffer 0 is used for device ID output */
  243. writew(0x0, host->regs + NFC_BUF_ADDR);
  244. /* Read ID into main buffer */
  245. tmp = readw(host->regs + NFC_CONFIG1);
  246. tmp &= ~NFC_SP_EN;
  247. writew(tmp, host->regs + NFC_CONFIG1);
  248. writew(NFC_ID, host->regs + NFC_CONFIG2);
  249. /* Wait for operation to complete */
  250. wait_op_done(host, TROP_US_DELAY, 0, true);
  251. if (this->options & NAND_BUSWIDTH_16) {
  252. void __iomem *main_buf = host->regs + MAIN_AREA0;
  253. /* compress the ID info */
  254. writeb(readb(main_buf + 2), main_buf + 1);
  255. writeb(readb(main_buf + 4), main_buf + 2);
  256. writeb(readb(main_buf + 6), main_buf + 3);
  257. writeb(readb(main_buf + 8), main_buf + 4);
  258. writeb(readb(main_buf + 10), main_buf + 5);
  259. }
  260. }
  261. /* This function requests the NANDFC to perform a read of the
  262. * NAND device status and returns the current status. */
  263. static uint16_t get_dev_status(struct mxc_nand_host *host)
  264. {
  265. void __iomem *main_buf = host->regs + MAIN_AREA1;
  266. uint32_t store;
  267. uint16_t ret, tmp;
  268. /* Issue status request to NAND device */
  269. /* store the main area1 first word, later do recovery */
  270. store = readl(main_buf);
  271. /* NANDFC buffer 1 is used for device status to prevent
  272. * corruption of read/write buffer on status requests. */
  273. writew(1, host->regs + NFC_BUF_ADDR);
  274. /* Read status into main buffer */
  275. tmp = readw(host->regs + NFC_CONFIG1);
  276. tmp &= ~NFC_SP_EN;
  277. writew(tmp, host->regs + NFC_CONFIG1);
  278. writew(NFC_STATUS, host->regs + NFC_CONFIG2);
  279. /* Wait for operation to complete */
  280. wait_op_done(host, TROP_US_DELAY, 0, true);
  281. /* Status is placed in first word of main buffer */
  282. /* get status, then recovery area 1 data */
  283. ret = readw(main_buf);
  284. writel(store, main_buf);
  285. return ret;
  286. }
  287. /* This functions is used by upper layer to checks if device is ready */
  288. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  289. {
  290. /*
  291. * NFC handles R/B internally. Therefore, this function
  292. * always returns status as ready.
  293. */
  294. return 1;
  295. }
  296. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  297. {
  298. /*
  299. * If HW ECC is enabled, we turn it on during init. There is
  300. * no need to enable again here.
  301. */
  302. }
  303. static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  304. u_char *read_ecc, u_char *calc_ecc)
  305. {
  306. struct nand_chip *nand_chip = mtd->priv;
  307. struct mxc_nand_host *host = nand_chip->priv;
  308. /*
  309. * 1-Bit errors are automatically corrected in HW. No need for
  310. * additional correction. 2-Bit errors cannot be corrected by
  311. * HW ECC, so we need to return failure
  312. */
  313. uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT);
  314. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  315. DEBUG(MTD_DEBUG_LEVEL0,
  316. "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  322. u_char *ecc_code)
  323. {
  324. return 0;
  325. }
  326. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  327. {
  328. struct nand_chip *nand_chip = mtd->priv;
  329. struct mxc_nand_host *host = nand_chip->priv;
  330. uint8_t ret = 0;
  331. uint16_t col, rd_word;
  332. uint16_t __iomem *main_buf = host->regs + MAIN_AREA0;
  333. uint16_t __iomem *spare_buf = host->regs + SPARE_AREA0;
  334. /* Check for status request */
  335. if (host->status_request)
  336. return get_dev_status(host) & 0xFF;
  337. /* Get column for 16-bit access */
  338. col = host->col_addr >> 1;
  339. /* If we are accessing the spare region */
  340. if (host->spare_only)
  341. rd_word = readw(&spare_buf[col]);
  342. else
  343. rd_word = readw(&main_buf[col]);
  344. /* Pick upper/lower byte of word from RAM buffer */
  345. if (host->col_addr & 0x1)
  346. ret = (rd_word >> 8) & 0xFF;
  347. else
  348. ret = rd_word & 0xFF;
  349. /* Update saved column address */
  350. host->col_addr++;
  351. return ret;
  352. }
  353. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  354. {
  355. struct nand_chip *nand_chip = mtd->priv;
  356. struct mxc_nand_host *host = nand_chip->priv;
  357. uint16_t col, rd_word, ret;
  358. uint16_t __iomem *p;
  359. DEBUG(MTD_DEBUG_LEVEL3,
  360. "mxc_nand_read_word(col = %d)\n", host->col_addr);
  361. col = host->col_addr;
  362. /* Adjust saved column address */
  363. if (col < mtd->writesize && host->spare_only)
  364. col += mtd->writesize;
  365. if (col < mtd->writesize)
  366. p = (host->regs + MAIN_AREA0) + (col >> 1);
  367. else
  368. p = (host->regs + SPARE_AREA0) + ((col - mtd->writesize) >> 1);
  369. if (col & 1) {
  370. rd_word = readw(p);
  371. ret = (rd_word >> 8) & 0xff;
  372. rd_word = readw(&p[1]);
  373. ret |= (rd_word << 8) & 0xff00;
  374. } else
  375. ret = readw(p);
  376. /* Update saved column address */
  377. host->col_addr = col + 2;
  378. return ret;
  379. }
  380. /* Write data of length len to buffer buf. The data to be
  381. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  382. * Operation by the NFC, the data is written to NAND Flash */
  383. static void mxc_nand_write_buf(struct mtd_info *mtd,
  384. const u_char *buf, int len)
  385. {
  386. struct nand_chip *nand_chip = mtd->priv;
  387. struct mxc_nand_host *host = nand_chip->priv;
  388. int n, col, i = 0;
  389. DEBUG(MTD_DEBUG_LEVEL3,
  390. "mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
  391. len);
  392. col = host->col_addr;
  393. /* Adjust saved column address */
  394. if (col < mtd->writesize && host->spare_only)
  395. col += mtd->writesize;
  396. n = mtd->writesize + mtd->oobsize - col;
  397. n = min(len, n);
  398. DEBUG(MTD_DEBUG_LEVEL3,
  399. "%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
  400. while (n) {
  401. void __iomem *p;
  402. if (col < mtd->writesize)
  403. p = host->regs + MAIN_AREA0 + (col & ~3);
  404. else
  405. p = host->regs + SPARE_AREA0 -
  406. mtd->writesize + (col & ~3);
  407. DEBUG(MTD_DEBUG_LEVEL3, "%s:%d: p = %p\n", __func__,
  408. __LINE__, p);
  409. if (((col | (int)&buf[i]) & 3) || n < 16) {
  410. uint32_t data = 0;
  411. if (col & 3 || n < 4)
  412. data = readl(p);
  413. switch (col & 3) {
  414. case 0:
  415. if (n) {
  416. data = (data & 0xffffff00) |
  417. (buf[i++] << 0);
  418. n--;
  419. col++;
  420. }
  421. case 1:
  422. if (n) {
  423. data = (data & 0xffff00ff) |
  424. (buf[i++] << 8);
  425. n--;
  426. col++;
  427. }
  428. case 2:
  429. if (n) {
  430. data = (data & 0xff00ffff) |
  431. (buf[i++] << 16);
  432. n--;
  433. col++;
  434. }
  435. case 3:
  436. if (n) {
  437. data = (data & 0x00ffffff) |
  438. (buf[i++] << 24);
  439. n--;
  440. col++;
  441. }
  442. }
  443. writel(data, p);
  444. } else {
  445. int m = mtd->writesize - col;
  446. if (col >= mtd->writesize)
  447. m += mtd->oobsize;
  448. m = min(n, m) & ~3;
  449. DEBUG(MTD_DEBUG_LEVEL3,
  450. "%s:%d: n = %d, m = %d, i = %d, col = %d\n",
  451. __func__, __LINE__, n, m, i, col);
  452. memcpy(p, &buf[i], m);
  453. col += m;
  454. i += m;
  455. n -= m;
  456. }
  457. }
  458. /* Update saved column address */
  459. host->col_addr = col;
  460. }
  461. /* Read the data buffer from the NAND Flash. To read the data from NAND
  462. * Flash first the data output cycle is initiated by the NFC, which copies
  463. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  464. */
  465. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  466. {
  467. struct nand_chip *nand_chip = mtd->priv;
  468. struct mxc_nand_host *host = nand_chip->priv;
  469. int n, col, i = 0;
  470. DEBUG(MTD_DEBUG_LEVEL3,
  471. "mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr, len);
  472. col = host->col_addr;
  473. /* Adjust saved column address */
  474. if (col < mtd->writesize && host->spare_only)
  475. col += mtd->writesize;
  476. n = mtd->writesize + mtd->oobsize - col;
  477. n = min(len, n);
  478. while (n) {
  479. void __iomem *p;
  480. if (col < mtd->writesize)
  481. p = host->regs + MAIN_AREA0 + (col & ~3);
  482. else
  483. p = host->regs + SPARE_AREA0 -
  484. mtd->writesize + (col & ~3);
  485. if (((col | (int)&buf[i]) & 3) || n < 16) {
  486. uint32_t data;
  487. data = readl(p);
  488. switch (col & 3) {
  489. case 0:
  490. if (n) {
  491. buf[i++] = (uint8_t) (data);
  492. n--;
  493. col++;
  494. }
  495. case 1:
  496. if (n) {
  497. buf[i++] = (uint8_t) (data >> 8);
  498. n--;
  499. col++;
  500. }
  501. case 2:
  502. if (n) {
  503. buf[i++] = (uint8_t) (data >> 16);
  504. n--;
  505. col++;
  506. }
  507. case 3:
  508. if (n) {
  509. buf[i++] = (uint8_t) (data >> 24);
  510. n--;
  511. col++;
  512. }
  513. }
  514. } else {
  515. int m = mtd->writesize - col;
  516. if (col >= mtd->writesize)
  517. m += mtd->oobsize;
  518. m = min(n, m) & ~3;
  519. memcpy(&buf[i], p, m);
  520. col += m;
  521. i += m;
  522. n -= m;
  523. }
  524. }
  525. /* Update saved column address */
  526. host->col_addr = col;
  527. }
  528. /* Used by the upper layer to verify the data in NAND Flash
  529. * with the data in the buf. */
  530. static int mxc_nand_verify_buf(struct mtd_info *mtd,
  531. const u_char *buf, int len)
  532. {
  533. return -EFAULT;
  534. }
  535. /* This function is used by upper layer for select and
  536. * deselect of the NAND chip */
  537. static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
  538. {
  539. struct nand_chip *nand_chip = mtd->priv;
  540. struct mxc_nand_host *host = nand_chip->priv;
  541. #ifdef CONFIG_MTD_NAND_MXC_FORCE_CE
  542. if (chip > 0) {
  543. DEBUG(MTD_DEBUG_LEVEL0,
  544. "ERROR: Illegal chip select (chip = %d)\n", chip);
  545. return;
  546. }
  547. if (chip == -1) {
  548. writew(readw(host->regs + NFC_CONFIG1) & ~NFC_CE,
  549. host->regs + NFC_CONFIG1);
  550. return;
  551. }
  552. writew(readw(host->regs + NFC_CONFIG1) | NFC_CE,
  553. host->regs + NFC_CONFIG1);
  554. #endif
  555. switch (chip) {
  556. case -1:
  557. /* Disable the NFC clock */
  558. if (host->clk_act) {
  559. clk_disable(host->clk);
  560. host->clk_act = 0;
  561. }
  562. break;
  563. case 0:
  564. /* Enable the NFC clock */
  565. if (!host->clk_act) {
  566. clk_enable(host->clk);
  567. host->clk_act = 1;
  568. }
  569. break;
  570. default:
  571. break;
  572. }
  573. }
  574. /* Used by the upper layer to write command to NAND Flash for
  575. * different operations to be carried out on NAND Flash */
  576. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  577. int column, int page_addr)
  578. {
  579. struct nand_chip *nand_chip = mtd->priv;
  580. struct mxc_nand_host *host = nand_chip->priv;
  581. int useirq = true;
  582. DEBUG(MTD_DEBUG_LEVEL3,
  583. "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  584. command, column, page_addr);
  585. /* Reset command state information */
  586. host->status_request = false;
  587. /* Command pre-processing step */
  588. switch (command) {
  589. case NAND_CMD_STATUS:
  590. host->col_addr = 0;
  591. host->status_request = true;
  592. break;
  593. case NAND_CMD_READ0:
  594. host->col_addr = column;
  595. host->spare_only = false;
  596. useirq = false;
  597. break;
  598. case NAND_CMD_READOOB:
  599. host->col_addr = column;
  600. host->spare_only = true;
  601. useirq = false;
  602. if (host->pagesize_2k)
  603. command = NAND_CMD_READ0; /* only READ0 is valid */
  604. break;
  605. case NAND_CMD_SEQIN:
  606. if (column >= mtd->writesize) {
  607. /*
  608. * FIXME: before send SEQIN command for write OOB,
  609. * We must read one page out.
  610. * For K9F1GXX has no READ1 command to set current HW
  611. * pointer to spare area, we must write the whole page
  612. * including OOB together.
  613. */
  614. if (host->pagesize_2k)
  615. /* call ourself to read a page */
  616. mxc_nand_command(mtd, NAND_CMD_READ0, 0,
  617. page_addr);
  618. host->col_addr = column - mtd->writesize;
  619. host->spare_only = true;
  620. /* Set program pointer to spare region */
  621. if (!host->pagesize_2k)
  622. send_cmd(host, NAND_CMD_READOOB, false);
  623. } else {
  624. host->spare_only = false;
  625. host->col_addr = column;
  626. /* Set program pointer to page start */
  627. if (!host->pagesize_2k)
  628. send_cmd(host, NAND_CMD_READ0, false);
  629. }
  630. useirq = false;
  631. break;
  632. case NAND_CMD_PAGEPROG:
  633. send_prog_page(host, 0, host->spare_only);
  634. if (host->pagesize_2k) {
  635. /* data in 4 areas datas */
  636. send_prog_page(host, 1, host->spare_only);
  637. send_prog_page(host, 2, host->spare_only);
  638. send_prog_page(host, 3, host->spare_only);
  639. }
  640. break;
  641. case NAND_CMD_ERASE1:
  642. useirq = false;
  643. break;
  644. }
  645. /* Write out the command to the device. */
  646. send_cmd(host, command, useirq);
  647. /* Write out column address, if necessary */
  648. if (column != -1) {
  649. /*
  650. * MXC NANDFC can only perform full page+spare or
  651. * spare-only read/write. When the upper layers
  652. * layers perform a read/write buf operation,
  653. * we will used the saved column adress to index into
  654. * the full page.
  655. */
  656. send_addr(host, 0, page_addr == -1);
  657. if (host->pagesize_2k)
  658. /* another col addr cycle for 2k page */
  659. send_addr(host, 0, false);
  660. }
  661. /* Write out page address, if necessary */
  662. if (page_addr != -1) {
  663. /* paddr_0 - p_addr_7 */
  664. send_addr(host, (page_addr & 0xff), false);
  665. if (host->pagesize_2k) {
  666. if (mtd->size >= 0x10000000) {
  667. /* paddr_8 - paddr_15 */
  668. send_addr(host, (page_addr >> 8) & 0xff, false);
  669. send_addr(host, (page_addr >> 16) & 0xff, true);
  670. } else
  671. /* paddr_8 - paddr_15 */
  672. send_addr(host, (page_addr >> 8) & 0xff, true);
  673. } else {
  674. /* One more address cycle for higher density devices */
  675. if (mtd->size >= 0x4000000) {
  676. /* paddr_8 - paddr_15 */
  677. send_addr(host, (page_addr >> 8) & 0xff, false);
  678. send_addr(host, (page_addr >> 16) & 0xff, true);
  679. } else
  680. /* paddr_8 - paddr_15 */
  681. send_addr(host, (page_addr >> 8) & 0xff, true);
  682. }
  683. }
  684. /* Command post-processing step */
  685. switch (command) {
  686. case NAND_CMD_RESET:
  687. break;
  688. case NAND_CMD_READOOB:
  689. case NAND_CMD_READ0:
  690. if (host->pagesize_2k) {
  691. /* send read confirm command */
  692. send_cmd(host, NAND_CMD_READSTART, true);
  693. /* read for each AREA */
  694. send_read_page(host, 0, host->spare_only);
  695. send_read_page(host, 1, host->spare_only);
  696. send_read_page(host, 2, host->spare_only);
  697. send_read_page(host, 3, host->spare_only);
  698. } else
  699. send_read_page(host, 0, host->spare_only);
  700. break;
  701. case NAND_CMD_READID:
  702. host->col_addr = 0;
  703. send_read_id(host);
  704. break;
  705. case NAND_CMD_PAGEPROG:
  706. break;
  707. case NAND_CMD_STATUS:
  708. break;
  709. case NAND_CMD_ERASE2:
  710. break;
  711. }
  712. }
  713. static int __init mxcnd_probe(struct platform_device *pdev)
  714. {
  715. struct nand_chip *this;
  716. struct mtd_info *mtd;
  717. struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
  718. struct mxc_nand_host *host;
  719. struct resource *res;
  720. uint16_t tmp;
  721. int err = 0, nr_parts = 0;
  722. /* Allocate memory for MTD device structure and private data */
  723. host = kzalloc(sizeof(struct mxc_nand_host), GFP_KERNEL);
  724. if (!host)
  725. return -ENOMEM;
  726. host->dev = &pdev->dev;
  727. /* structures must be linked */
  728. this = &host->nand;
  729. mtd = &host->mtd;
  730. mtd->priv = this;
  731. mtd->owner = THIS_MODULE;
  732. mtd->dev.parent = &pdev->dev;
  733. mtd->name = "mxc_nand";
  734. /* 50 us command delay time */
  735. this->chip_delay = 5;
  736. this->priv = host;
  737. this->dev_ready = mxc_nand_dev_ready;
  738. this->cmdfunc = mxc_nand_command;
  739. this->select_chip = mxc_nand_select_chip;
  740. this->read_byte = mxc_nand_read_byte;
  741. this->read_word = mxc_nand_read_word;
  742. this->write_buf = mxc_nand_write_buf;
  743. this->read_buf = mxc_nand_read_buf;
  744. this->verify_buf = mxc_nand_verify_buf;
  745. host->clk = clk_get(&pdev->dev, "nfc");
  746. if (IS_ERR(host->clk)) {
  747. err = PTR_ERR(host->clk);
  748. goto eclk;
  749. }
  750. clk_enable(host->clk);
  751. host->clk_act = 1;
  752. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  753. if (!res) {
  754. err = -ENODEV;
  755. goto eres;
  756. }
  757. host->regs = ioremap(res->start, res->end - res->start + 1);
  758. if (!host->regs) {
  759. err = -ENOMEM;
  760. goto eres;
  761. }
  762. tmp = readw(host->regs + NFC_CONFIG1);
  763. tmp |= NFC_INT_MSK;
  764. writew(tmp, host->regs + NFC_CONFIG1);
  765. init_waitqueue_head(&host->irq_waitq);
  766. host->irq = platform_get_irq(pdev, 0);
  767. err = request_irq(host->irq, mxc_nfc_irq, 0, "mxc_nd", host);
  768. if (err)
  769. goto eirq;
  770. if (pdata->hw_ecc) {
  771. this->ecc.calculate = mxc_nand_calculate_ecc;
  772. this->ecc.hwctl = mxc_nand_enable_hwecc;
  773. this->ecc.correct = mxc_nand_correct_data;
  774. this->ecc.mode = NAND_ECC_HW;
  775. this->ecc.size = 512;
  776. this->ecc.bytes = 3;
  777. tmp = readw(host->regs + NFC_CONFIG1);
  778. tmp |= NFC_ECC_EN;
  779. writew(tmp, host->regs + NFC_CONFIG1);
  780. } else {
  781. this->ecc.size = 512;
  782. this->ecc.bytes = 3;
  783. this->ecc.layout = &nand_hw_eccoob_8;
  784. this->ecc.mode = NAND_ECC_SOFT;
  785. tmp = readw(host->regs + NFC_CONFIG1);
  786. tmp &= ~NFC_ECC_EN;
  787. writew(tmp, host->regs + NFC_CONFIG1);
  788. }
  789. /* Reset NAND */
  790. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  791. /* preset operation */
  792. /* Unlock the internal RAM Buffer */
  793. writew(0x2, host->regs + NFC_CONFIG);
  794. /* Blocks to be unlocked */
  795. writew(0x0, host->regs + NFC_UNLOCKSTART_BLKADDR);
  796. writew(0x4000, host->regs + NFC_UNLOCKEND_BLKADDR);
  797. /* Unlock Block Command for given address range */
  798. writew(0x4, host->regs + NFC_WRPROT);
  799. /* NAND bus width determines access funtions used by upper layer */
  800. if (pdata->width == 2) {
  801. this->options |= NAND_BUSWIDTH_16;
  802. this->ecc.layout = &nand_hw_eccoob_16;
  803. }
  804. /* first scan to find the device and get the page size */
  805. if (nand_scan_ident(mtd, 1)) {
  806. err = -ENXIO;
  807. goto escan;
  808. }
  809. host->pagesize_2k = (mtd->writesize == 2048) ? 1 : 0;
  810. if (this->ecc.mode == NAND_ECC_HW) {
  811. switch (mtd->oobsize) {
  812. case 8:
  813. this->ecc.layout = &nand_hw_eccoob_8;
  814. break;
  815. case 16:
  816. this->ecc.layout = &nand_hw_eccoob_16;
  817. break;
  818. case 64:
  819. this->ecc.layout = &nand_hw_eccoob_64;
  820. break;
  821. default:
  822. /* page size not handled by HW ECC */
  823. /* switching back to soft ECC */
  824. this->ecc.size = 512;
  825. this->ecc.bytes = 3;
  826. this->ecc.layout = &nand_hw_eccoob_8;
  827. this->ecc.mode = NAND_ECC_SOFT;
  828. this->ecc.calculate = NULL;
  829. this->ecc.correct = NULL;
  830. this->ecc.hwctl = NULL;
  831. tmp = readw(host->regs + NFC_CONFIG1);
  832. tmp &= ~NFC_ECC_EN;
  833. writew(tmp, host->regs + NFC_CONFIG1);
  834. break;
  835. }
  836. }
  837. /* second phase scan */
  838. if (nand_scan_tail(mtd)) {
  839. err = -ENXIO;
  840. goto escan;
  841. }
  842. /* Register the partitions */
  843. #ifdef CONFIG_MTD_PARTITIONS
  844. nr_parts =
  845. parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
  846. if (nr_parts > 0)
  847. add_mtd_partitions(mtd, host->parts, nr_parts);
  848. else
  849. #endif
  850. {
  851. pr_info("Registering %s as whole device\n", mtd->name);
  852. add_mtd_device(mtd);
  853. }
  854. platform_set_drvdata(pdev, host);
  855. return 0;
  856. escan:
  857. free_irq(host->irq, host);
  858. eirq:
  859. iounmap(host->regs);
  860. eres:
  861. clk_put(host->clk);
  862. eclk:
  863. kfree(host);
  864. return err;
  865. }
  866. static int __devexit mxcnd_remove(struct platform_device *pdev)
  867. {
  868. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  869. clk_put(host->clk);
  870. platform_set_drvdata(pdev, NULL);
  871. nand_release(&host->mtd);
  872. free_irq(host->irq, host);
  873. iounmap(host->regs);
  874. kfree(host);
  875. return 0;
  876. }
  877. #ifdef CONFIG_PM
  878. static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
  879. {
  880. struct mtd_info *mtd = platform_get_drvdata(pdev);
  881. struct nand_chip *nand_chip = mtd->priv;
  882. struct mxc_nand_host *host = nand_chip->priv;
  883. int ret = 0;
  884. DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
  885. if (mtd) {
  886. ret = mtd->suspend(mtd);
  887. /* Disable the NFC clock */
  888. clk_disable(host->clk);
  889. }
  890. return ret;
  891. }
  892. static int mxcnd_resume(struct platform_device *pdev)
  893. {
  894. struct mtd_info *mtd = platform_get_drvdata(pdev);
  895. struct nand_chip *nand_chip = mtd->priv;
  896. struct mxc_nand_host *host = nand_chip->priv;
  897. int ret = 0;
  898. DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
  899. if (mtd) {
  900. /* Enable the NFC clock */
  901. clk_enable(host->clk);
  902. mtd->resume(mtd);
  903. }
  904. return ret;
  905. }
  906. #else
  907. # define mxcnd_suspend NULL
  908. # define mxcnd_resume NULL
  909. #endif /* CONFIG_PM */
  910. static struct platform_driver mxcnd_driver = {
  911. .driver = {
  912. .name = DRIVER_NAME,
  913. },
  914. .remove = __exit_p(mxcnd_remove),
  915. .suspend = mxcnd_suspend,
  916. .resume = mxcnd_resume,
  917. };
  918. static int __init mxc_nd_init(void)
  919. {
  920. return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
  921. }
  922. static void __exit mxc_nd_cleanup(void)
  923. {
  924. /* Unregister the device structure */
  925. platform_driver_unregister(&mxcnd_driver);
  926. }
  927. module_init(mxc_nd_init);
  928. module_exit(mxc_nd_cleanup);
  929. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  930. MODULE_DESCRIPTION("MXC NAND MTD driver");
  931. MODULE_LICENSE("GPL");