mxc_nand.c 26 KB

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