mxc_nand.c 26 KB

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