mxc_nand.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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, 6}, {12, 4}, }
  121. };
  122. #ifdef CONFIG_MTD_PARTITIONS
  123. static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
  124. #endif
  125. static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
  126. {
  127. struct mxc_nand_host *host = dev_id;
  128. uint16_t tmp;
  129. tmp = readw(host->regs + NFC_CONFIG1);
  130. tmp |= NFC_INT_MSK; /* Disable interrupt */
  131. writew(tmp, host->regs + NFC_CONFIG1);
  132. wake_up(&host->irq_waitq);
  133. return IRQ_HANDLED;
  134. }
  135. /* This function polls the NANDFC to wait for the basic operation to
  136. * complete by checking the INT bit of config2 register.
  137. */
  138. static void wait_op_done(struct mxc_nand_host *host, int max_retries,
  139. uint16_t param, int useirq)
  140. {
  141. uint32_t tmp;
  142. if (useirq) {
  143. if ((readw(host->regs + NFC_CONFIG2) & NFC_INT) == 0) {
  144. tmp = readw(host->regs + NFC_CONFIG1);
  145. tmp &= ~NFC_INT_MSK; /* Enable interrupt */
  146. writew(tmp, host->regs + NFC_CONFIG1);
  147. wait_event(host->irq_waitq,
  148. readw(host->regs + NFC_CONFIG2) & NFC_INT);
  149. tmp = readw(host->regs + NFC_CONFIG2);
  150. tmp &= ~NFC_INT;
  151. writew(tmp, host->regs + NFC_CONFIG2);
  152. }
  153. } else {
  154. while (max_retries-- > 0) {
  155. if (readw(host->regs + NFC_CONFIG2) & NFC_INT) {
  156. tmp = readw(host->regs + NFC_CONFIG2);
  157. tmp &= ~NFC_INT;
  158. writew(tmp, host->regs + NFC_CONFIG2);
  159. break;
  160. }
  161. udelay(1);
  162. }
  163. if (max_retries <= 0)
  164. DEBUG(MTD_DEBUG_LEVEL0, "%s(%d): INT not set\n",
  165. __func__, param);
  166. }
  167. }
  168. /* This function issues the specified command to the NAND device and
  169. * waits for completion. */
  170. static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  171. {
  172. DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
  173. writew(cmd, host->regs + NFC_FLASH_CMD);
  174. writew(NFC_CMD, host->regs + NFC_CONFIG2);
  175. /* Wait for operation to complete */
  176. wait_op_done(host, TROP_US_DELAY, cmd, useirq);
  177. }
  178. /* This function sends an address (or partial address) to the
  179. * NAND device. The address is used to select the source/destination for
  180. * a NAND command. */
  181. static void send_addr(struct mxc_nand_host *host, uint16_t addr, int islast)
  182. {
  183. DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
  184. writew(addr, host->regs + NFC_FLASH_ADDR);
  185. writew(NFC_ADDR, host->regs + NFC_CONFIG2);
  186. /* Wait for operation to complete */
  187. wait_op_done(host, TROP_US_DELAY, addr, islast);
  188. }
  189. /* This function requests the NANDFC to initate the transfer
  190. * of data currently in the NANDFC RAM buffer to the NAND device. */
  191. static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
  192. int spare_only)
  193. {
  194. DEBUG(MTD_DEBUG_LEVEL3, "send_prog_page (%d)\n", spare_only);
  195. /* NANDFC buffer 0 is used for page read/write */
  196. writew(buf_id, host->regs + NFC_BUF_ADDR);
  197. /* Configure spare or page+spare access */
  198. if (!host->pagesize_2k) {
  199. uint16_t config1 = readw(host->regs + NFC_CONFIG1);
  200. if (spare_only)
  201. config1 |= NFC_SP_EN;
  202. else
  203. config1 &= ~(NFC_SP_EN);
  204. writew(config1, host->regs + NFC_CONFIG1);
  205. }
  206. writew(NFC_INPUT, host->regs + NFC_CONFIG2);
  207. /* Wait for operation to complete */
  208. wait_op_done(host, TROP_US_DELAY, spare_only, true);
  209. }
  210. /* Requests NANDFC to initated the transfer of data from the
  211. * NAND device into in the NANDFC ram buffer. */
  212. static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
  213. int spare_only)
  214. {
  215. DEBUG(MTD_DEBUG_LEVEL3, "send_read_page (%d)\n", spare_only);
  216. /* NANDFC buffer 0 is used for page read/write */
  217. writew(buf_id, host->regs + NFC_BUF_ADDR);
  218. /* Configure spare or page+spare access */
  219. if (!host->pagesize_2k) {
  220. uint32_t config1 = readw(host->regs + NFC_CONFIG1);
  221. if (spare_only)
  222. config1 |= NFC_SP_EN;
  223. else
  224. config1 &= ~NFC_SP_EN;
  225. writew(config1, host->regs + NFC_CONFIG1);
  226. }
  227. writew(NFC_OUTPUT, host->regs + NFC_CONFIG2);
  228. /* Wait for operation to complete */
  229. wait_op_done(host, TROP_US_DELAY, spare_only, true);
  230. }
  231. /* Request the NANDFC to perform a read of the NAND device ID. */
  232. static void send_read_id(struct mxc_nand_host *host)
  233. {
  234. struct nand_chip *this = &host->nand;
  235. uint16_t tmp;
  236. /* NANDFC buffer 0 is used for device ID output */
  237. writew(0x0, host->regs + NFC_BUF_ADDR);
  238. /* Read ID into main buffer */
  239. tmp = readw(host->regs + NFC_CONFIG1);
  240. tmp &= ~NFC_SP_EN;
  241. writew(tmp, host->regs + NFC_CONFIG1);
  242. writew(NFC_ID, host->regs + NFC_CONFIG2);
  243. /* Wait for operation to complete */
  244. wait_op_done(host, TROP_US_DELAY, 0, true);
  245. if (this->options & NAND_BUSWIDTH_16) {
  246. void __iomem *main_buf = host->regs + MAIN_AREA0;
  247. /* compress the ID info */
  248. writeb(readb(main_buf + 2), main_buf + 1);
  249. writeb(readb(main_buf + 4), main_buf + 2);
  250. writeb(readb(main_buf + 6), main_buf + 3);
  251. writeb(readb(main_buf + 8), main_buf + 4);
  252. writeb(readb(main_buf + 10), main_buf + 5);
  253. }
  254. }
  255. /* This function requests the NANDFC to perform a read of the
  256. * NAND device status and returns the current status. */
  257. static uint16_t get_dev_status(struct mxc_nand_host *host)
  258. {
  259. void __iomem *main_buf = host->regs + MAIN_AREA1;
  260. uint32_t store;
  261. uint16_t ret, tmp;
  262. /* Issue status request to NAND device */
  263. /* store the main area1 first word, later do recovery */
  264. store = readl(main_buf);
  265. /* NANDFC buffer 1 is used for device status to prevent
  266. * corruption of read/write buffer on status requests. */
  267. writew(1, host->regs + NFC_BUF_ADDR);
  268. /* Read status into main buffer */
  269. tmp = readw(host->regs + NFC_CONFIG1);
  270. tmp &= ~NFC_SP_EN;
  271. writew(tmp, host->regs + NFC_CONFIG1);
  272. writew(NFC_STATUS, host->regs + NFC_CONFIG2);
  273. /* Wait for operation to complete */
  274. wait_op_done(host, TROP_US_DELAY, 0, true);
  275. /* Status is placed in first word of main buffer */
  276. /* get status, then recovery area 1 data */
  277. ret = readw(main_buf);
  278. writel(store, main_buf);
  279. return ret;
  280. }
  281. /* This functions is used by upper layer to checks if device is ready */
  282. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  283. {
  284. /*
  285. * NFC handles R/B internally. Therefore, this function
  286. * always returns status as ready.
  287. */
  288. return 1;
  289. }
  290. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  291. {
  292. /*
  293. * If HW ECC is enabled, we turn it on during init. There is
  294. * no need to enable again here.
  295. */
  296. }
  297. static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  298. u_char *read_ecc, u_char *calc_ecc)
  299. {
  300. struct nand_chip *nand_chip = mtd->priv;
  301. struct mxc_nand_host *host = nand_chip->priv;
  302. /*
  303. * 1-Bit errors are automatically corrected in HW. No need for
  304. * additional correction. 2-Bit errors cannot be corrected by
  305. * HW ECC, so we need to return failure
  306. */
  307. uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT);
  308. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  309. DEBUG(MTD_DEBUG_LEVEL0,
  310. "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  316. u_char *ecc_code)
  317. {
  318. return 0;
  319. }
  320. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  321. {
  322. struct nand_chip *nand_chip = mtd->priv;
  323. struct mxc_nand_host *host = nand_chip->priv;
  324. uint8_t ret = 0;
  325. uint16_t col, rd_word;
  326. uint16_t __iomem *main_buf = host->regs + MAIN_AREA0;
  327. uint16_t __iomem *spare_buf = host->regs + SPARE_AREA0;
  328. /* Check for status request */
  329. if (host->status_request)
  330. return get_dev_status(host) & 0xFF;
  331. /* Get column for 16-bit access */
  332. col = host->col_addr >> 1;
  333. /* If we are accessing the spare region */
  334. if (host->spare_only)
  335. rd_word = readw(&spare_buf[col]);
  336. else
  337. rd_word = readw(&main_buf[col]);
  338. /* Pick upper/lower byte of word from RAM buffer */
  339. if (host->col_addr & 0x1)
  340. ret = (rd_word >> 8) & 0xFF;
  341. else
  342. ret = rd_word & 0xFF;
  343. /* Update saved column address */
  344. host->col_addr++;
  345. return ret;
  346. }
  347. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  348. {
  349. struct nand_chip *nand_chip = mtd->priv;
  350. struct mxc_nand_host *host = nand_chip->priv;
  351. uint16_t col, rd_word, ret;
  352. uint16_t __iomem *p;
  353. DEBUG(MTD_DEBUG_LEVEL3,
  354. "mxc_nand_read_word(col = %d)\n", host->col_addr);
  355. col = host->col_addr;
  356. /* Adjust saved column address */
  357. if (col < mtd->writesize && host->spare_only)
  358. col += mtd->writesize;
  359. if (col < mtd->writesize)
  360. p = (host->regs + MAIN_AREA0) + (col >> 1);
  361. else
  362. p = (host->regs + SPARE_AREA0) + ((col - mtd->writesize) >> 1);
  363. if (col & 1) {
  364. rd_word = readw(p);
  365. ret = (rd_word >> 8) & 0xff;
  366. rd_word = readw(&p[1]);
  367. ret |= (rd_word << 8) & 0xff00;
  368. } else
  369. ret = readw(p);
  370. /* Update saved column address */
  371. host->col_addr = col + 2;
  372. return ret;
  373. }
  374. /* Write data of length len to buffer buf. The data to be
  375. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  376. * Operation by the NFC, the data is written to NAND Flash */
  377. static void mxc_nand_write_buf(struct mtd_info *mtd,
  378. const u_char *buf, int len)
  379. {
  380. struct nand_chip *nand_chip = mtd->priv;
  381. struct mxc_nand_host *host = nand_chip->priv;
  382. int n, col, i = 0;
  383. DEBUG(MTD_DEBUG_LEVEL3,
  384. "mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
  385. len);
  386. col = host->col_addr;
  387. /* Adjust saved column address */
  388. if (col < mtd->writesize && host->spare_only)
  389. col += mtd->writesize;
  390. n = mtd->writesize + mtd->oobsize - col;
  391. n = min(len, n);
  392. DEBUG(MTD_DEBUG_LEVEL3,
  393. "%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
  394. while (n) {
  395. void __iomem *p;
  396. if (col < mtd->writesize)
  397. p = host->regs + MAIN_AREA0 + (col & ~3);
  398. else
  399. p = host->regs + SPARE_AREA0 -
  400. mtd->writesize + (col & ~3);
  401. DEBUG(MTD_DEBUG_LEVEL3, "%s:%d: p = %p\n", __func__,
  402. __LINE__, p);
  403. if (((col | (int)&buf[i]) & 3) || n < 16) {
  404. uint32_t data = 0;
  405. if (col & 3 || n < 4)
  406. data = readl(p);
  407. switch (col & 3) {
  408. case 0:
  409. if (n) {
  410. data = (data & 0xffffff00) |
  411. (buf[i++] << 0);
  412. n--;
  413. col++;
  414. }
  415. case 1:
  416. if (n) {
  417. data = (data & 0xffff00ff) |
  418. (buf[i++] << 8);
  419. n--;
  420. col++;
  421. }
  422. case 2:
  423. if (n) {
  424. data = (data & 0xff00ffff) |
  425. (buf[i++] << 16);
  426. n--;
  427. col++;
  428. }
  429. case 3:
  430. if (n) {
  431. data = (data & 0x00ffffff) |
  432. (buf[i++] << 24);
  433. n--;
  434. col++;
  435. }
  436. }
  437. writel(data, p);
  438. } else {
  439. int m = mtd->writesize - col;
  440. if (col >= mtd->writesize)
  441. m += mtd->oobsize;
  442. m = min(n, m) & ~3;
  443. DEBUG(MTD_DEBUG_LEVEL3,
  444. "%s:%d: n = %d, m = %d, i = %d, col = %d\n",
  445. __func__, __LINE__, n, m, i, col);
  446. memcpy(p, &buf[i], m);
  447. col += m;
  448. i += m;
  449. n -= m;
  450. }
  451. }
  452. /* Update saved column address */
  453. host->col_addr = col;
  454. }
  455. /* Read the data buffer from the NAND Flash. To read the data from NAND
  456. * Flash first the data output cycle is initiated by the NFC, which copies
  457. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  458. */
  459. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  460. {
  461. struct nand_chip *nand_chip = mtd->priv;
  462. struct mxc_nand_host *host = nand_chip->priv;
  463. int n, col, i = 0;
  464. DEBUG(MTD_DEBUG_LEVEL3,
  465. "mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr, len);
  466. col = host->col_addr;
  467. /* Adjust saved column address */
  468. if (col < mtd->writesize && host->spare_only)
  469. col += mtd->writesize;
  470. n = mtd->writesize + mtd->oobsize - col;
  471. n = min(len, n);
  472. while (n) {
  473. void __iomem *p;
  474. if (col < mtd->writesize)
  475. p = host->regs + MAIN_AREA0 + (col & ~3);
  476. else
  477. p = host->regs + SPARE_AREA0 -
  478. mtd->writesize + (col & ~3);
  479. if (((col | (int)&buf[i]) & 3) || n < 16) {
  480. uint32_t data;
  481. data = readl(p);
  482. switch (col & 3) {
  483. case 0:
  484. if (n) {
  485. buf[i++] = (uint8_t) (data);
  486. n--;
  487. col++;
  488. }
  489. case 1:
  490. if (n) {
  491. buf[i++] = (uint8_t) (data >> 8);
  492. n--;
  493. col++;
  494. }
  495. case 2:
  496. if (n) {
  497. buf[i++] = (uint8_t) (data >> 16);
  498. n--;
  499. col++;
  500. }
  501. case 3:
  502. if (n) {
  503. buf[i++] = (uint8_t) (data >> 24);
  504. n--;
  505. col++;
  506. }
  507. }
  508. } else {
  509. int m = mtd->writesize - col;
  510. if (col >= mtd->writesize)
  511. m += mtd->oobsize;
  512. m = min(n, m) & ~3;
  513. memcpy(&buf[i], p, m);
  514. col += m;
  515. i += m;
  516. n -= m;
  517. }
  518. }
  519. /* Update saved column address */
  520. host->col_addr = col;
  521. }
  522. /* Used by the upper layer to verify the data in NAND Flash
  523. * with the data in the buf. */
  524. static int mxc_nand_verify_buf(struct mtd_info *mtd,
  525. const u_char *buf, int len)
  526. {
  527. return -EFAULT;
  528. }
  529. /* This function is used by upper layer for select and
  530. * deselect of the NAND chip */
  531. static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
  532. {
  533. struct nand_chip *nand_chip = mtd->priv;
  534. struct mxc_nand_host *host = nand_chip->priv;
  535. #ifdef CONFIG_MTD_NAND_MXC_FORCE_CE
  536. if (chip > 0) {
  537. DEBUG(MTD_DEBUG_LEVEL0,
  538. "ERROR: Illegal chip select (chip = %d)\n", chip);
  539. return;
  540. }
  541. if (chip == -1) {
  542. writew(readw(host->regs + NFC_CONFIG1) & ~NFC_CE,
  543. host->regs + NFC_CONFIG1);
  544. return;
  545. }
  546. writew(readw(host->regs + NFC_CONFIG1) | NFC_CE,
  547. host->regs + NFC_CONFIG1);
  548. #endif
  549. switch (chip) {
  550. case -1:
  551. /* Disable the NFC clock */
  552. if (host->clk_act) {
  553. clk_disable(host->clk);
  554. host->clk_act = 0;
  555. }
  556. break;
  557. case 0:
  558. /* Enable the NFC clock */
  559. if (!host->clk_act) {
  560. clk_enable(host->clk);
  561. host->clk_act = 1;
  562. }
  563. break;
  564. default:
  565. break;
  566. }
  567. }
  568. /* Used by the upper layer to write command to NAND Flash for
  569. * different operations to be carried out on NAND Flash */
  570. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  571. int column, int page_addr)
  572. {
  573. struct nand_chip *nand_chip = mtd->priv;
  574. struct mxc_nand_host *host = nand_chip->priv;
  575. int useirq = true;
  576. DEBUG(MTD_DEBUG_LEVEL3,
  577. "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  578. command, column, page_addr);
  579. /* Reset command state information */
  580. host->status_request = false;
  581. /* Command pre-processing step */
  582. switch (command) {
  583. case NAND_CMD_STATUS:
  584. host->col_addr = 0;
  585. host->status_request = true;
  586. break;
  587. case NAND_CMD_READ0:
  588. host->col_addr = column;
  589. host->spare_only = false;
  590. useirq = false;
  591. break;
  592. case NAND_CMD_READOOB:
  593. host->col_addr = column;
  594. host->spare_only = true;
  595. useirq = false;
  596. if (host->pagesize_2k)
  597. command = NAND_CMD_READ0; /* only READ0 is valid */
  598. break;
  599. case NAND_CMD_SEQIN:
  600. if (column >= mtd->writesize) {
  601. /*
  602. * FIXME: before send SEQIN command for write OOB,
  603. * We must read one page out.
  604. * For K9F1GXX has no READ1 command to set current HW
  605. * pointer to spare area, we must write the whole page
  606. * including OOB together.
  607. */
  608. if (host->pagesize_2k)
  609. /* call ourself to read a page */
  610. mxc_nand_command(mtd, NAND_CMD_READ0, 0,
  611. page_addr);
  612. host->col_addr = column - mtd->writesize;
  613. host->spare_only = true;
  614. /* Set program pointer to spare region */
  615. if (!host->pagesize_2k)
  616. send_cmd(host, NAND_CMD_READOOB, false);
  617. } else {
  618. host->spare_only = false;
  619. host->col_addr = column;
  620. /* Set program pointer to page start */
  621. if (!host->pagesize_2k)
  622. send_cmd(host, NAND_CMD_READ0, false);
  623. }
  624. useirq = false;
  625. break;
  626. case NAND_CMD_PAGEPROG:
  627. send_prog_page(host, 0, host->spare_only);
  628. if (host->pagesize_2k) {
  629. /* data in 4 areas datas */
  630. send_prog_page(host, 1, host->spare_only);
  631. send_prog_page(host, 2, host->spare_only);
  632. send_prog_page(host, 3, host->spare_only);
  633. }
  634. break;
  635. case NAND_CMD_ERASE1:
  636. useirq = false;
  637. break;
  638. }
  639. /* Write out the command to the device. */
  640. send_cmd(host, command, useirq);
  641. /* Write out column address, if necessary */
  642. if (column != -1) {
  643. /*
  644. * MXC NANDFC can only perform full page+spare or
  645. * spare-only read/write. When the upper layers
  646. * layers perform a read/write buf operation,
  647. * we will used the saved column adress to index into
  648. * the full page.
  649. */
  650. send_addr(host, 0, page_addr == -1);
  651. if (host->pagesize_2k)
  652. /* another col addr cycle for 2k page */
  653. send_addr(host, 0, false);
  654. }
  655. /* Write out page address, if necessary */
  656. if (page_addr != -1) {
  657. /* paddr_0 - p_addr_7 */
  658. send_addr(host, (page_addr & 0xff), false);
  659. if (host->pagesize_2k) {
  660. send_addr(host, (page_addr >> 8) & 0xFF, false);
  661. if (mtd->size >= 0x40000000)
  662. send_addr(host, (page_addr >> 16) & 0xff, true);
  663. } else {
  664. /* One more address cycle for higher density devices */
  665. if (mtd->size >= 0x4000000) {
  666. /* paddr_8 - paddr_15 */
  667. send_addr(host, (page_addr >> 8) & 0xff, false);
  668. send_addr(host, (page_addr >> 16) & 0xff, true);
  669. } else
  670. /* paddr_8 - paddr_15 */
  671. send_addr(host, (page_addr >> 8) & 0xff, true);
  672. }
  673. }
  674. /* Command post-processing step */
  675. switch (command) {
  676. case NAND_CMD_RESET:
  677. break;
  678. case NAND_CMD_READOOB:
  679. case NAND_CMD_READ0:
  680. if (host->pagesize_2k) {
  681. /* send read confirm command */
  682. send_cmd(host, NAND_CMD_READSTART, true);
  683. /* read for each AREA */
  684. send_read_page(host, 0, host->spare_only);
  685. send_read_page(host, 1, host->spare_only);
  686. send_read_page(host, 2, host->spare_only);
  687. send_read_page(host, 3, host->spare_only);
  688. } else
  689. send_read_page(host, 0, host->spare_only);
  690. break;
  691. case NAND_CMD_READID:
  692. send_read_id(host);
  693. break;
  694. case NAND_CMD_PAGEPROG:
  695. break;
  696. case NAND_CMD_STATUS:
  697. break;
  698. case NAND_CMD_ERASE2:
  699. break;
  700. }
  701. }
  702. static int __init mxcnd_probe(struct platform_device *pdev)
  703. {
  704. struct nand_chip *this;
  705. struct mtd_info *mtd;
  706. struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
  707. struct mxc_nand_host *host;
  708. struct resource *res;
  709. uint16_t tmp;
  710. int err = 0, nr_parts = 0;
  711. /* Allocate memory for MTD device structure and private data */
  712. host = kzalloc(sizeof(struct mxc_nand_host), GFP_KERNEL);
  713. if (!host)
  714. return -ENOMEM;
  715. host->dev = &pdev->dev;
  716. /* structures must be linked */
  717. this = &host->nand;
  718. mtd = &host->mtd;
  719. mtd->priv = this;
  720. mtd->owner = THIS_MODULE;
  721. mtd->dev.parent = &pdev->dev;
  722. /* 50 us command delay time */
  723. this->chip_delay = 5;
  724. this->priv = host;
  725. this->dev_ready = mxc_nand_dev_ready;
  726. this->cmdfunc = mxc_nand_command;
  727. this->select_chip = mxc_nand_select_chip;
  728. this->read_byte = mxc_nand_read_byte;
  729. this->read_word = mxc_nand_read_word;
  730. this->write_buf = mxc_nand_write_buf;
  731. this->read_buf = mxc_nand_read_buf;
  732. this->verify_buf = mxc_nand_verify_buf;
  733. host->clk = clk_get(&pdev->dev, "nfc");
  734. if (IS_ERR(host->clk))
  735. goto eclk;
  736. clk_enable(host->clk);
  737. host->clk_act = 1;
  738. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  739. if (!res) {
  740. err = -ENODEV;
  741. goto eres;
  742. }
  743. host->regs = ioremap(res->start, res->end - res->start + 1);
  744. if (!host->regs) {
  745. err = -EIO;
  746. goto eres;
  747. }
  748. tmp = readw(host->regs + NFC_CONFIG1);
  749. tmp |= NFC_INT_MSK;
  750. writew(tmp, host->regs + NFC_CONFIG1);
  751. init_waitqueue_head(&host->irq_waitq);
  752. host->irq = platform_get_irq(pdev, 0);
  753. err = request_irq(host->irq, mxc_nfc_irq, 0, "mxc_nd", host);
  754. if (err)
  755. goto eirq;
  756. if (pdata->hw_ecc) {
  757. this->ecc.calculate = mxc_nand_calculate_ecc;
  758. this->ecc.hwctl = mxc_nand_enable_hwecc;
  759. this->ecc.correct = mxc_nand_correct_data;
  760. this->ecc.mode = NAND_ECC_HW;
  761. this->ecc.size = 512;
  762. this->ecc.bytes = 3;
  763. this->ecc.layout = &nand_hw_eccoob_8;
  764. tmp = readw(host->regs + NFC_CONFIG1);
  765. tmp |= NFC_ECC_EN;
  766. writew(tmp, host->regs + NFC_CONFIG1);
  767. } else {
  768. this->ecc.size = 512;
  769. this->ecc.bytes = 3;
  770. this->ecc.layout = &nand_hw_eccoob_8;
  771. this->ecc.mode = NAND_ECC_SOFT;
  772. tmp = readw(host->regs + NFC_CONFIG1);
  773. tmp &= ~NFC_ECC_EN;
  774. writew(tmp, host->regs + NFC_CONFIG1);
  775. }
  776. /* Reset NAND */
  777. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  778. /* preset operation */
  779. /* Unlock the internal RAM Buffer */
  780. writew(0x2, host->regs + NFC_CONFIG);
  781. /* Blocks to be unlocked */
  782. writew(0x0, host->regs + NFC_UNLOCKSTART_BLKADDR);
  783. writew(0x4000, host->regs + NFC_UNLOCKEND_BLKADDR);
  784. /* Unlock Block Command for given address range */
  785. writew(0x4, host->regs + NFC_WRPROT);
  786. /* NAND bus width determines access funtions used by upper layer */
  787. if (pdata->width == 2) {
  788. this->options |= NAND_BUSWIDTH_16;
  789. this->ecc.layout = &nand_hw_eccoob_16;
  790. }
  791. host->pagesize_2k = 0;
  792. /* Scan to find existence of the device */
  793. if (nand_scan(mtd, 1)) {
  794. DEBUG(MTD_DEBUG_LEVEL0,
  795. "MXC_ND: Unable to find any NAND device.\n");
  796. err = -ENXIO;
  797. goto escan;
  798. }
  799. /* Register the partitions */
  800. #ifdef CONFIG_MTD_PARTITIONS
  801. nr_parts =
  802. parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
  803. if (nr_parts > 0)
  804. add_mtd_partitions(mtd, host->parts, nr_parts);
  805. else
  806. #endif
  807. {
  808. pr_info("Registering %s as whole device\n", mtd->name);
  809. add_mtd_device(mtd);
  810. }
  811. platform_set_drvdata(pdev, host);
  812. return 0;
  813. escan:
  814. free_irq(host->irq, NULL);
  815. eirq:
  816. iounmap(host->regs);
  817. eres:
  818. clk_put(host->clk);
  819. eclk:
  820. kfree(host);
  821. return err;
  822. }
  823. static int __devexit mxcnd_remove(struct platform_device *pdev)
  824. {
  825. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  826. clk_put(host->clk);
  827. platform_set_drvdata(pdev, NULL);
  828. nand_release(&host->mtd);
  829. free_irq(host->irq, NULL);
  830. iounmap(host->regs);
  831. kfree(host);
  832. return 0;
  833. }
  834. #ifdef CONFIG_PM
  835. static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
  836. {
  837. struct mtd_info *info = platform_get_drvdata(pdev);
  838. int ret = 0;
  839. DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
  840. if (info)
  841. ret = info->suspend(info);
  842. /* Disable the NFC clock */
  843. clk_disable(nfc_clk); /* FIXME */
  844. return ret;
  845. }
  846. static int mxcnd_resume(struct platform_device *pdev)
  847. {
  848. struct mtd_info *info = platform_get_drvdata(pdev);
  849. int ret = 0;
  850. DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
  851. /* Enable the NFC clock */
  852. clk_enable(nfc_clk); /* FIXME */
  853. if (info)
  854. info->resume(info);
  855. return ret;
  856. }
  857. #else
  858. # define mxcnd_suspend NULL
  859. # define mxcnd_resume NULL
  860. #endif /* CONFIG_PM */
  861. static struct platform_driver mxcnd_driver = {
  862. .driver = {
  863. .name = DRIVER_NAME,
  864. },
  865. .remove = __exit_p(mxcnd_remove),
  866. .suspend = mxcnd_suspend,
  867. .resume = mxcnd_resume,
  868. };
  869. static int __init mxc_nd_init(void)
  870. {
  871. /* Register the device driver structure. */
  872. pr_info("MXC MTD nand Driver\n");
  873. if (platform_driver_probe(&mxcnd_driver, mxcnd_probe) != 0) {
  874. printk(KERN_ERR "Driver register failed for mxcnd_driver\n");
  875. return -ENODEV;
  876. }
  877. return 0;
  878. }
  879. static void __exit mxc_nd_cleanup(void)
  880. {
  881. /* Unregister the device structure */
  882. platform_driver_unregister(&mxcnd_driver);
  883. }
  884. module_init(mxc_nd_init);
  885. module_exit(mxc_nd_cleanup);
  886. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  887. MODULE_DESCRIPTION("MXC NAND MTD driver");
  888. MODULE_LICENSE("GPL");