mxc_nand.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. /* 50 us command delay time */
  722. this->chip_delay = 5;
  723. this->priv = host;
  724. this->dev_ready = mxc_nand_dev_ready;
  725. this->cmdfunc = mxc_nand_command;
  726. this->select_chip = mxc_nand_select_chip;
  727. this->read_byte = mxc_nand_read_byte;
  728. this->read_word = mxc_nand_read_word;
  729. this->write_buf = mxc_nand_write_buf;
  730. this->read_buf = mxc_nand_read_buf;
  731. this->verify_buf = mxc_nand_verify_buf;
  732. host->clk = clk_get(&pdev->dev, "nfc_clk");
  733. if (IS_ERR(host->clk))
  734. goto eclk;
  735. clk_enable(host->clk);
  736. host->clk_act = 1;
  737. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  738. if (!res) {
  739. err = -ENODEV;
  740. goto eres;
  741. }
  742. host->regs = ioremap(res->start, res->end - res->start + 1);
  743. if (!host->regs) {
  744. err = -EIO;
  745. goto eres;
  746. }
  747. tmp = readw(host->regs + NFC_CONFIG1);
  748. tmp |= NFC_INT_MSK;
  749. writew(tmp, host->regs + NFC_CONFIG1);
  750. init_waitqueue_head(&host->irq_waitq);
  751. host->irq = platform_get_irq(pdev, 0);
  752. err = request_irq(host->irq, mxc_nfc_irq, 0, "mxc_nd", host);
  753. if (err)
  754. goto eirq;
  755. if (pdata->hw_ecc) {
  756. this->ecc.calculate = mxc_nand_calculate_ecc;
  757. this->ecc.hwctl = mxc_nand_enable_hwecc;
  758. this->ecc.correct = mxc_nand_correct_data;
  759. this->ecc.mode = NAND_ECC_HW;
  760. this->ecc.size = 512;
  761. this->ecc.bytes = 3;
  762. this->ecc.layout = &nand_hw_eccoob_8;
  763. tmp = readw(host->regs + NFC_CONFIG1);
  764. tmp |= NFC_ECC_EN;
  765. writew(tmp, host->regs + NFC_CONFIG1);
  766. } else {
  767. this->ecc.size = 512;
  768. this->ecc.bytes = 3;
  769. this->ecc.layout = &nand_hw_eccoob_8;
  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. /* Reset NAND */
  776. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  777. /* preset operation */
  778. /* Unlock the internal RAM Buffer */
  779. writew(0x2, host->regs + NFC_CONFIG);
  780. /* Blocks to be unlocked */
  781. writew(0x0, host->regs + NFC_UNLOCKSTART_BLKADDR);
  782. writew(0x4000, host->regs + NFC_UNLOCKEND_BLKADDR);
  783. /* Unlock Block Command for given address range */
  784. writew(0x4, host->regs + NFC_WRPROT);
  785. /* NAND bus width determines access funtions used by upper layer */
  786. if (pdata->width == 2) {
  787. this->options |= NAND_BUSWIDTH_16;
  788. this->ecc.layout = &nand_hw_eccoob_16;
  789. }
  790. host->pagesize_2k = 0;
  791. /* Scan to find existence of the device */
  792. if (nand_scan(mtd, 1)) {
  793. DEBUG(MTD_DEBUG_LEVEL0,
  794. "MXC_ND: Unable to find any NAND device.\n");
  795. err = -ENXIO;
  796. goto escan;
  797. }
  798. /* Register the partitions */
  799. #ifdef CONFIG_MTD_PARTITIONS
  800. nr_parts =
  801. parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
  802. if (nr_parts > 0)
  803. add_mtd_partitions(mtd, host->parts, nr_parts);
  804. else
  805. #endif
  806. {
  807. pr_info("Registering %s as whole device\n", mtd->name);
  808. add_mtd_device(mtd);
  809. }
  810. platform_set_drvdata(pdev, host);
  811. return 0;
  812. escan:
  813. free_irq(host->irq, NULL);
  814. eirq:
  815. iounmap(host->regs);
  816. eres:
  817. clk_put(host->clk);
  818. eclk:
  819. kfree(host);
  820. return err;
  821. }
  822. static int __devexit mxcnd_remove(struct platform_device *pdev)
  823. {
  824. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  825. clk_put(host->clk);
  826. platform_set_drvdata(pdev, NULL);
  827. nand_release(&host->mtd);
  828. free_irq(host->irq, NULL);
  829. iounmap(host->regs);
  830. kfree(host);
  831. return 0;
  832. }
  833. #ifdef CONFIG_PM
  834. static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
  835. {
  836. struct mtd_info *info = platform_get_drvdata(pdev);
  837. int ret = 0;
  838. DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
  839. if (info)
  840. ret = info->suspend(info);
  841. /* Disable the NFC clock */
  842. clk_disable(nfc_clk); /* FIXME */
  843. return ret;
  844. }
  845. static int mxcnd_resume(struct platform_device *pdev)
  846. {
  847. struct mtd_info *info = platform_get_drvdata(pdev);
  848. int ret = 0;
  849. DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
  850. /* Enable the NFC clock */
  851. clk_enable(nfc_clk); /* FIXME */
  852. if (info)
  853. info->resume(info);
  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. /* Register the device driver structure. */
  871. pr_info("MXC MTD nand Driver\n");
  872. if (platform_driver_probe(&mxcnd_driver, mxcnd_probe) != 0) {
  873. printk(KERN_ERR "Driver register failed for mxcnd_driver\n");
  874. return -ENODEV;
  875. }
  876. return 0;
  877. }
  878. static void __exit mxc_nd_cleanup(void)
  879. {
  880. /* Unregister the device structure */
  881. platform_driver_unregister(&mxcnd_driver);
  882. }
  883. module_init(mxc_nd_init);
  884. module_exit(mxc_nd_cleanup);
  885. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  886. MODULE_DESCRIPTION("MXC NAND MTD driver");
  887. MODULE_LICENSE("GPL");