mxc_nand.c 26 KB

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