mpc5121_nfc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. * Copyright 2004-2008 Freescale Semiconductor, Inc.
  3. * Copyright 2009 Semihalf.
  4. *
  5. * Approved as OSADL project by a majority of OSADL members and funded
  6. * by OSADL membership fees in 2009; for details see www.osadl.org.
  7. *
  8. * Based on original driver from Freescale Semiconductor
  9. * written by John Rigby <jrigby@freescale.com> on basis
  10. * of drivers/mtd/nand/mxc_nand.c. Reworked and extended
  11. * Piotr Ziecik <kosmo@semihalf.com>.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. * MA 02110-1301, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/clk.h>
  29. #include <linux/gfp.h>
  30. #include <linux/delay.h>
  31. #include <linux/err.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/io.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/mtd/nand.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <linux/of_device.h>
  39. #include <linux/of_platform.h>
  40. #include <asm/mpc5121.h>
  41. /* Addresses for NFC MAIN RAM BUFFER areas */
  42. #define NFC_MAIN_AREA(n) ((n) * 0x200)
  43. /* Addresses for NFC SPARE BUFFER areas */
  44. #define NFC_SPARE_BUFFERS 8
  45. #define NFC_SPARE_LEN 0x40
  46. #define NFC_SPARE_AREA(n) (0x1000 + ((n) * NFC_SPARE_LEN))
  47. /* MPC5121 NFC registers */
  48. #define NFC_BUF_ADDR 0x1E04
  49. #define NFC_FLASH_ADDR 0x1E06
  50. #define NFC_FLASH_CMD 0x1E08
  51. #define NFC_CONFIG 0x1E0A
  52. #define NFC_ECC_STATUS1 0x1E0C
  53. #define NFC_ECC_STATUS2 0x1E0E
  54. #define NFC_SPAS 0x1E10
  55. #define NFC_WRPROT 0x1E12
  56. #define NFC_NF_WRPRST 0x1E18
  57. #define NFC_CONFIG1 0x1E1A
  58. #define NFC_CONFIG2 0x1E1C
  59. #define NFC_UNLOCKSTART_BLK0 0x1E20
  60. #define NFC_UNLOCKEND_BLK0 0x1E22
  61. #define NFC_UNLOCKSTART_BLK1 0x1E24
  62. #define NFC_UNLOCKEND_BLK1 0x1E26
  63. #define NFC_UNLOCKSTART_BLK2 0x1E28
  64. #define NFC_UNLOCKEND_BLK2 0x1E2A
  65. #define NFC_UNLOCKSTART_BLK3 0x1E2C
  66. #define NFC_UNLOCKEND_BLK3 0x1E2E
  67. /* Bit Definitions: NFC_BUF_ADDR */
  68. #define NFC_RBA_MASK (7 << 0)
  69. #define NFC_ACTIVE_CS_SHIFT 5
  70. #define NFC_ACTIVE_CS_MASK (3 << NFC_ACTIVE_CS_SHIFT)
  71. /* Bit Definitions: NFC_CONFIG */
  72. #define NFC_BLS_UNLOCKED (1 << 1)
  73. /* Bit Definitions: NFC_CONFIG1 */
  74. #define NFC_ECC_4BIT (1 << 0)
  75. #define NFC_FULL_PAGE_DMA (1 << 1)
  76. #define NFC_SPARE_ONLY (1 << 2)
  77. #define NFC_ECC_ENABLE (1 << 3)
  78. #define NFC_INT_MASK (1 << 4)
  79. #define NFC_BIG_ENDIAN (1 << 5)
  80. #define NFC_RESET (1 << 6)
  81. #define NFC_CE (1 << 7)
  82. #define NFC_ONE_CYCLE (1 << 8)
  83. #define NFC_PPB_32 (0 << 9)
  84. #define NFC_PPB_64 (1 << 9)
  85. #define NFC_PPB_128 (2 << 9)
  86. #define NFC_PPB_256 (3 << 9)
  87. #define NFC_PPB_MASK (3 << 9)
  88. #define NFC_FULL_PAGE_INT (1 << 11)
  89. /* Bit Definitions: NFC_CONFIG2 */
  90. #define NFC_COMMAND (1 << 0)
  91. #define NFC_ADDRESS (1 << 1)
  92. #define NFC_INPUT (1 << 2)
  93. #define NFC_OUTPUT (1 << 3)
  94. #define NFC_ID (1 << 4)
  95. #define NFC_STATUS (1 << 5)
  96. #define NFC_CMD_FAIL (1 << 15)
  97. #define NFC_INT (1 << 15)
  98. /* Bit Definitions: NFC_WRPROT */
  99. #define NFC_WPC_LOCK_TIGHT (1 << 0)
  100. #define NFC_WPC_LOCK (1 << 1)
  101. #define NFC_WPC_UNLOCK (1 << 2)
  102. #define DRV_NAME "mpc5121_nfc"
  103. /* Timeouts */
  104. #define NFC_RESET_TIMEOUT 1000 /* 1 ms */
  105. #define NFC_TIMEOUT (HZ / 10) /* 1/10 s */
  106. struct mpc5121_nfc_prv {
  107. struct mtd_info mtd;
  108. struct nand_chip chip;
  109. int irq;
  110. void __iomem *regs;
  111. struct clk *clk;
  112. wait_queue_head_t irq_waitq;
  113. uint column;
  114. int spareonly;
  115. void __iomem *csreg;
  116. struct device *dev;
  117. };
  118. static void mpc5121_nfc_done(struct mtd_info *mtd);
  119. /* Read NFC register */
  120. static inline u16 nfc_read(struct mtd_info *mtd, uint reg)
  121. {
  122. struct nand_chip *chip = mtd->priv;
  123. struct mpc5121_nfc_prv *prv = chip->priv;
  124. return in_be16(prv->regs + reg);
  125. }
  126. /* Write NFC register */
  127. static inline void nfc_write(struct mtd_info *mtd, uint reg, u16 val)
  128. {
  129. struct nand_chip *chip = mtd->priv;
  130. struct mpc5121_nfc_prv *prv = chip->priv;
  131. out_be16(prv->regs + reg, val);
  132. }
  133. /* Set bits in NFC register */
  134. static inline void nfc_set(struct mtd_info *mtd, uint reg, u16 bits)
  135. {
  136. nfc_write(mtd, reg, nfc_read(mtd, reg) | bits);
  137. }
  138. /* Clear bits in NFC register */
  139. static inline void nfc_clear(struct mtd_info *mtd, uint reg, u16 bits)
  140. {
  141. nfc_write(mtd, reg, nfc_read(mtd, reg) & ~bits);
  142. }
  143. /* Invoke address cycle */
  144. static inline void mpc5121_nfc_send_addr(struct mtd_info *mtd, u16 addr)
  145. {
  146. nfc_write(mtd, NFC_FLASH_ADDR, addr);
  147. nfc_write(mtd, NFC_CONFIG2, NFC_ADDRESS);
  148. mpc5121_nfc_done(mtd);
  149. }
  150. /* Invoke command cycle */
  151. static inline void mpc5121_nfc_send_cmd(struct mtd_info *mtd, u16 cmd)
  152. {
  153. nfc_write(mtd, NFC_FLASH_CMD, cmd);
  154. nfc_write(mtd, NFC_CONFIG2, NFC_COMMAND);
  155. mpc5121_nfc_done(mtd);
  156. }
  157. /* Send data from NFC buffers to NAND flash */
  158. static inline void mpc5121_nfc_send_prog_page(struct mtd_info *mtd)
  159. {
  160. nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
  161. nfc_write(mtd, NFC_CONFIG2, NFC_INPUT);
  162. mpc5121_nfc_done(mtd);
  163. }
  164. /* Receive data from NAND flash */
  165. static inline void mpc5121_nfc_send_read_page(struct mtd_info *mtd)
  166. {
  167. nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
  168. nfc_write(mtd, NFC_CONFIG2, NFC_OUTPUT);
  169. mpc5121_nfc_done(mtd);
  170. }
  171. /* Receive ID from NAND flash */
  172. static inline void mpc5121_nfc_send_read_id(struct mtd_info *mtd)
  173. {
  174. nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
  175. nfc_write(mtd, NFC_CONFIG2, NFC_ID);
  176. mpc5121_nfc_done(mtd);
  177. }
  178. /* Receive status from NAND flash */
  179. static inline void mpc5121_nfc_send_read_status(struct mtd_info *mtd)
  180. {
  181. nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
  182. nfc_write(mtd, NFC_CONFIG2, NFC_STATUS);
  183. mpc5121_nfc_done(mtd);
  184. }
  185. /* NFC interrupt handler */
  186. static irqreturn_t mpc5121_nfc_irq(int irq, void *data)
  187. {
  188. struct mtd_info *mtd = data;
  189. struct nand_chip *chip = mtd->priv;
  190. struct mpc5121_nfc_prv *prv = chip->priv;
  191. nfc_set(mtd, NFC_CONFIG1, NFC_INT_MASK);
  192. wake_up(&prv->irq_waitq);
  193. return IRQ_HANDLED;
  194. }
  195. /* Wait for operation complete */
  196. static void mpc5121_nfc_done(struct mtd_info *mtd)
  197. {
  198. struct nand_chip *chip = mtd->priv;
  199. struct mpc5121_nfc_prv *prv = chip->priv;
  200. int rv;
  201. if ((nfc_read(mtd, NFC_CONFIG2) & NFC_INT) == 0) {
  202. nfc_clear(mtd, NFC_CONFIG1, NFC_INT_MASK);
  203. rv = wait_event_timeout(prv->irq_waitq,
  204. (nfc_read(mtd, NFC_CONFIG2) & NFC_INT), NFC_TIMEOUT);
  205. if (!rv)
  206. dev_warn(prv->dev,
  207. "Timeout while waiting for interrupt.\n");
  208. }
  209. nfc_clear(mtd, NFC_CONFIG2, NFC_INT);
  210. }
  211. /* Do address cycle(s) */
  212. static void mpc5121_nfc_addr_cycle(struct mtd_info *mtd, int column, int page)
  213. {
  214. struct nand_chip *chip = mtd->priv;
  215. u32 pagemask = chip->pagemask;
  216. if (column != -1) {
  217. mpc5121_nfc_send_addr(mtd, column);
  218. if (mtd->writesize > 512)
  219. mpc5121_nfc_send_addr(mtd, column >> 8);
  220. }
  221. if (page != -1) {
  222. do {
  223. mpc5121_nfc_send_addr(mtd, page & 0xFF);
  224. page >>= 8;
  225. pagemask >>= 8;
  226. } while (pagemask);
  227. }
  228. }
  229. /* Control chip select signals */
  230. static void mpc5121_nfc_select_chip(struct mtd_info *mtd, int chip)
  231. {
  232. if (chip < 0) {
  233. nfc_clear(mtd, NFC_CONFIG1, NFC_CE);
  234. return;
  235. }
  236. nfc_clear(mtd, NFC_BUF_ADDR, NFC_ACTIVE_CS_MASK);
  237. nfc_set(mtd, NFC_BUF_ADDR, (chip << NFC_ACTIVE_CS_SHIFT) &
  238. NFC_ACTIVE_CS_MASK);
  239. nfc_set(mtd, NFC_CONFIG1, NFC_CE);
  240. }
  241. /* Init external chip select logic on ADS5121 board */
  242. static int ads5121_chipselect_init(struct mtd_info *mtd)
  243. {
  244. struct nand_chip *chip = mtd->priv;
  245. struct mpc5121_nfc_prv *prv = chip->priv;
  246. struct device_node *dn;
  247. dn = of_find_compatible_node(NULL, NULL, "fsl,mpc5121ads-cpld");
  248. if (dn) {
  249. prv->csreg = of_iomap(dn, 0);
  250. of_node_put(dn);
  251. if (!prv->csreg)
  252. return -ENOMEM;
  253. /* CPLD Register 9 controls NAND /CE Lines */
  254. prv->csreg += 9;
  255. return 0;
  256. }
  257. return -EINVAL;
  258. }
  259. /* Control chips select signal on ADS5121 board */
  260. static void ads5121_select_chip(struct mtd_info *mtd, int chip)
  261. {
  262. struct nand_chip *nand = mtd->priv;
  263. struct mpc5121_nfc_prv *prv = nand->priv;
  264. u8 v;
  265. v = in_8(prv->csreg);
  266. v |= 0x0F;
  267. if (chip >= 0) {
  268. mpc5121_nfc_select_chip(mtd, 0);
  269. v &= ~(1 << chip);
  270. } else
  271. mpc5121_nfc_select_chip(mtd, -1);
  272. out_8(prv->csreg, v);
  273. }
  274. /* Read NAND Ready/Busy signal */
  275. static int mpc5121_nfc_dev_ready(struct mtd_info *mtd)
  276. {
  277. /*
  278. * NFC handles ready/busy signal internally. Therefore, this function
  279. * always returns status as ready.
  280. */
  281. return 1;
  282. }
  283. /* Write command to NAND flash */
  284. static void mpc5121_nfc_command(struct mtd_info *mtd, unsigned command,
  285. int column, int page)
  286. {
  287. struct nand_chip *chip = mtd->priv;
  288. struct mpc5121_nfc_prv *prv = chip->priv;
  289. prv->column = (column >= 0) ? column : 0;
  290. prv->spareonly = 0;
  291. switch (command) {
  292. case NAND_CMD_PAGEPROG:
  293. mpc5121_nfc_send_prog_page(mtd);
  294. break;
  295. /*
  296. * NFC does not support sub-page reads and writes,
  297. * so emulate them using full page transfers.
  298. */
  299. case NAND_CMD_READ0:
  300. column = 0;
  301. break;
  302. case NAND_CMD_READ1:
  303. prv->column += 256;
  304. command = NAND_CMD_READ0;
  305. column = 0;
  306. break;
  307. case NAND_CMD_READOOB:
  308. prv->spareonly = 1;
  309. command = NAND_CMD_READ0;
  310. column = 0;
  311. break;
  312. case NAND_CMD_SEQIN:
  313. mpc5121_nfc_command(mtd, NAND_CMD_READ0, column, page);
  314. column = 0;
  315. break;
  316. case NAND_CMD_ERASE1:
  317. case NAND_CMD_ERASE2:
  318. case NAND_CMD_READID:
  319. case NAND_CMD_STATUS:
  320. break;
  321. default:
  322. return;
  323. }
  324. mpc5121_nfc_send_cmd(mtd, command);
  325. mpc5121_nfc_addr_cycle(mtd, column, page);
  326. switch (command) {
  327. case NAND_CMD_READ0:
  328. if (mtd->writesize > 512)
  329. mpc5121_nfc_send_cmd(mtd, NAND_CMD_READSTART);
  330. mpc5121_nfc_send_read_page(mtd);
  331. break;
  332. case NAND_CMD_READID:
  333. mpc5121_nfc_send_read_id(mtd);
  334. break;
  335. case NAND_CMD_STATUS:
  336. mpc5121_nfc_send_read_status(mtd);
  337. if (chip->options & NAND_BUSWIDTH_16)
  338. prv->column = 1;
  339. else
  340. prv->column = 0;
  341. break;
  342. }
  343. }
  344. /* Copy data from/to NFC spare buffers. */
  345. static void mpc5121_nfc_copy_spare(struct mtd_info *mtd, uint offset,
  346. u8 *buffer, uint size, int wr)
  347. {
  348. struct nand_chip *nand = mtd->priv;
  349. struct mpc5121_nfc_prv *prv = nand->priv;
  350. uint o, s, sbsize, blksize;
  351. /*
  352. * NAND spare area is available through NFC spare buffers.
  353. * The NFC divides spare area into (page_size / 512) chunks.
  354. * Each chunk is placed into separate spare memory area, using
  355. * first (spare_size / num_of_chunks) bytes of the buffer.
  356. *
  357. * For NAND device in which the spare area is not divided fully
  358. * by the number of chunks, number of used bytes in each spare
  359. * buffer is rounded down to the nearest even number of bytes,
  360. * and all remaining bytes are added to the last used spare area.
  361. *
  362. * For more information read section 26.6.10 of MPC5121e
  363. * Microcontroller Reference Manual, Rev. 3.
  364. */
  365. /* Calculate number of valid bytes in each spare buffer */
  366. sbsize = (mtd->oobsize / (mtd->writesize / 512)) & ~1;
  367. while (size) {
  368. /* Calculate spare buffer number */
  369. s = offset / sbsize;
  370. if (s > NFC_SPARE_BUFFERS - 1)
  371. s = NFC_SPARE_BUFFERS - 1;
  372. /*
  373. * Calculate offset to requested data block in selected spare
  374. * buffer and its size.
  375. */
  376. o = offset - (s * sbsize);
  377. blksize = min(sbsize - o, size);
  378. if (wr)
  379. memcpy_toio(prv->regs + NFC_SPARE_AREA(s) + o,
  380. buffer, blksize);
  381. else
  382. memcpy_fromio(buffer,
  383. prv->regs + NFC_SPARE_AREA(s) + o, blksize);
  384. buffer += blksize;
  385. offset += blksize;
  386. size -= blksize;
  387. };
  388. }
  389. /* Copy data from/to NFC main and spare buffers */
  390. static void mpc5121_nfc_buf_copy(struct mtd_info *mtd, u_char *buf, int len,
  391. int wr)
  392. {
  393. struct nand_chip *chip = mtd->priv;
  394. struct mpc5121_nfc_prv *prv = chip->priv;
  395. uint c = prv->column;
  396. uint l;
  397. /* Handle spare area access */
  398. if (prv->spareonly || c >= mtd->writesize) {
  399. /* Calculate offset from beginning of spare area */
  400. if (c >= mtd->writesize)
  401. c -= mtd->writesize;
  402. prv->column += len;
  403. mpc5121_nfc_copy_spare(mtd, c, buf, len, wr);
  404. return;
  405. }
  406. /*
  407. * Handle main area access - limit copy length to prevent
  408. * crossing main/spare boundary.
  409. */
  410. l = min((uint)len, mtd->writesize - c);
  411. prv->column += l;
  412. if (wr)
  413. memcpy_toio(prv->regs + NFC_MAIN_AREA(0) + c, buf, l);
  414. else
  415. memcpy_fromio(buf, prv->regs + NFC_MAIN_AREA(0) + c, l);
  416. /* Handle crossing main/spare boundary */
  417. if (l != len) {
  418. buf += l;
  419. len -= l;
  420. mpc5121_nfc_buf_copy(mtd, buf, len, wr);
  421. }
  422. }
  423. /* Read data from NFC buffers */
  424. static void mpc5121_nfc_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  425. {
  426. mpc5121_nfc_buf_copy(mtd, buf, len, 0);
  427. }
  428. /* Write data to NFC buffers */
  429. static void mpc5121_nfc_write_buf(struct mtd_info *mtd,
  430. const u_char *buf, int len)
  431. {
  432. mpc5121_nfc_buf_copy(mtd, (u_char *)buf, len, 1);
  433. }
  434. /* Compare buffer with NAND flash */
  435. static int mpc5121_nfc_verify_buf(struct mtd_info *mtd,
  436. const u_char *buf, int len)
  437. {
  438. u_char tmp[256];
  439. uint bsize;
  440. while (len) {
  441. bsize = min(len, 256);
  442. mpc5121_nfc_read_buf(mtd, tmp, bsize);
  443. if (memcmp(buf, tmp, bsize))
  444. return 1;
  445. buf += bsize;
  446. len -= bsize;
  447. }
  448. return 0;
  449. }
  450. /* Read byte from NFC buffers */
  451. static u8 mpc5121_nfc_read_byte(struct mtd_info *mtd)
  452. {
  453. u8 tmp;
  454. mpc5121_nfc_read_buf(mtd, &tmp, sizeof(tmp));
  455. return tmp;
  456. }
  457. /* Read word from NFC buffers */
  458. static u16 mpc5121_nfc_read_word(struct mtd_info *mtd)
  459. {
  460. u16 tmp;
  461. mpc5121_nfc_read_buf(mtd, (u_char *)&tmp, sizeof(tmp));
  462. return tmp;
  463. }
  464. /*
  465. * Read NFC configuration from Reset Config Word
  466. *
  467. * NFC is configured during reset in basis of information stored
  468. * in Reset Config Word. There is no other way to set NAND block
  469. * size, spare size and bus width.
  470. */
  471. static int mpc5121_nfc_read_hw_config(struct mtd_info *mtd)
  472. {
  473. struct nand_chip *chip = mtd->priv;
  474. struct mpc5121_nfc_prv *prv = chip->priv;
  475. struct mpc512x_reset_module *rm;
  476. struct device_node *rmnode;
  477. uint rcw_pagesize = 0;
  478. uint rcw_sparesize = 0;
  479. uint rcw_width;
  480. uint rcwh;
  481. uint romloc, ps;
  482. int ret = 0;
  483. rmnode = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-reset");
  484. if (!rmnode) {
  485. dev_err(prv->dev, "Missing 'fsl,mpc5121-reset' "
  486. "node in device tree!\n");
  487. return -ENODEV;
  488. }
  489. rm = of_iomap(rmnode, 0);
  490. if (!rm) {
  491. dev_err(prv->dev, "Error mapping reset module node!\n");
  492. ret = -EBUSY;
  493. goto out;
  494. }
  495. rcwh = in_be32(&rm->rcwhr);
  496. /* Bit 6: NFC bus width */
  497. rcw_width = ((rcwh >> 6) & 0x1) ? 2 : 1;
  498. /* Bit 7: NFC Page/Spare size */
  499. ps = (rcwh >> 7) & 0x1;
  500. /* Bits [22:21]: ROM Location */
  501. romloc = (rcwh >> 21) & 0x3;
  502. /* Decode RCW bits */
  503. switch ((ps << 2) | romloc) {
  504. case 0x00:
  505. case 0x01:
  506. rcw_pagesize = 512;
  507. rcw_sparesize = 16;
  508. break;
  509. case 0x02:
  510. case 0x03:
  511. rcw_pagesize = 4096;
  512. rcw_sparesize = 128;
  513. break;
  514. case 0x04:
  515. case 0x05:
  516. rcw_pagesize = 2048;
  517. rcw_sparesize = 64;
  518. break;
  519. case 0x06:
  520. case 0x07:
  521. rcw_pagesize = 4096;
  522. rcw_sparesize = 218;
  523. break;
  524. }
  525. mtd->writesize = rcw_pagesize;
  526. mtd->oobsize = rcw_sparesize;
  527. if (rcw_width == 2)
  528. chip->options |= NAND_BUSWIDTH_16;
  529. dev_notice(prv->dev, "Configured for "
  530. "%u-bit NAND, page size %u "
  531. "with %u spare.\n",
  532. rcw_width * 8, rcw_pagesize,
  533. rcw_sparesize);
  534. iounmap(rm);
  535. out:
  536. of_node_put(rmnode);
  537. return ret;
  538. }
  539. /* Free driver resources */
  540. static void mpc5121_nfc_free(struct device *dev, struct mtd_info *mtd)
  541. {
  542. struct nand_chip *chip = mtd->priv;
  543. struct mpc5121_nfc_prv *prv = chip->priv;
  544. if (prv->clk) {
  545. clk_disable(prv->clk);
  546. clk_put(prv->clk);
  547. }
  548. if (prv->csreg)
  549. iounmap(prv->csreg);
  550. }
  551. static int __devinit mpc5121_nfc_probe(struct platform_device *op)
  552. {
  553. struct device_node *rootnode, *dn = op->dev.of_node;
  554. struct device *dev = &op->dev;
  555. struct mpc5121_nfc_prv *prv;
  556. struct resource res;
  557. struct mtd_info *mtd;
  558. struct nand_chip *chip;
  559. unsigned long regs_paddr, regs_size;
  560. const __be32 *chips_no;
  561. int resettime = 0;
  562. int retval = 0;
  563. int rev, len;
  564. struct mtd_part_parser_data ppdata;
  565. /*
  566. * Check SoC revision. This driver supports only NFC
  567. * in MPC5121 revision 2 and MPC5123 revision 3.
  568. */
  569. rev = (mfspr(SPRN_SVR) >> 4) & 0xF;
  570. if ((rev != 2) && (rev != 3)) {
  571. dev_err(dev, "SoC revision %u is not supported!\n", rev);
  572. return -ENXIO;
  573. }
  574. prv = devm_kzalloc(dev, sizeof(*prv), GFP_KERNEL);
  575. if (!prv) {
  576. dev_err(dev, "Memory exhausted!\n");
  577. return -ENOMEM;
  578. }
  579. mtd = &prv->mtd;
  580. chip = &prv->chip;
  581. mtd->priv = chip;
  582. chip->priv = prv;
  583. prv->dev = dev;
  584. /* Read NFC configuration from Reset Config Word */
  585. retval = mpc5121_nfc_read_hw_config(mtd);
  586. if (retval) {
  587. dev_err(dev, "Unable to read NFC config!\n");
  588. return retval;
  589. }
  590. prv->irq = irq_of_parse_and_map(dn, 0);
  591. if (prv->irq == NO_IRQ) {
  592. dev_err(dev, "Error mapping IRQ!\n");
  593. return -EINVAL;
  594. }
  595. retval = of_address_to_resource(dn, 0, &res);
  596. if (retval) {
  597. dev_err(dev, "Error parsing memory region!\n");
  598. return retval;
  599. }
  600. chips_no = of_get_property(dn, "chips", &len);
  601. if (!chips_no || len != sizeof(*chips_no)) {
  602. dev_err(dev, "Invalid/missing 'chips' property!\n");
  603. return -EINVAL;
  604. }
  605. regs_paddr = res.start;
  606. regs_size = resource_size(&res);
  607. if (!devm_request_mem_region(dev, regs_paddr, regs_size, DRV_NAME)) {
  608. dev_err(dev, "Error requesting memory region!\n");
  609. return -EBUSY;
  610. }
  611. prv->regs = devm_ioremap(dev, regs_paddr, regs_size);
  612. if (!prv->regs) {
  613. dev_err(dev, "Error mapping memory region!\n");
  614. return -ENOMEM;
  615. }
  616. mtd->name = "MPC5121 NAND";
  617. ppdata.of_node = dn;
  618. chip->dev_ready = mpc5121_nfc_dev_ready;
  619. chip->cmdfunc = mpc5121_nfc_command;
  620. chip->read_byte = mpc5121_nfc_read_byte;
  621. chip->read_word = mpc5121_nfc_read_word;
  622. chip->read_buf = mpc5121_nfc_read_buf;
  623. chip->write_buf = mpc5121_nfc_write_buf;
  624. chip->verify_buf = mpc5121_nfc_verify_buf;
  625. chip->select_chip = mpc5121_nfc_select_chip;
  626. chip->options = NAND_NO_AUTOINCR;
  627. chip->bbt_options = NAND_BBT_USE_FLASH;
  628. chip->ecc.mode = NAND_ECC_SOFT;
  629. /* Support external chip-select logic on ADS5121 board */
  630. rootnode = of_find_node_by_path("/");
  631. if (of_device_is_compatible(rootnode, "fsl,mpc5121ads")) {
  632. retval = ads5121_chipselect_init(mtd);
  633. if (retval) {
  634. dev_err(dev, "Chipselect init error!\n");
  635. of_node_put(rootnode);
  636. return retval;
  637. }
  638. chip->select_chip = ads5121_select_chip;
  639. }
  640. of_node_put(rootnode);
  641. /* Enable NFC clock */
  642. prv->clk = clk_get(dev, "nfc_clk");
  643. if (IS_ERR(prv->clk)) {
  644. dev_err(dev, "Unable to acquire NFC clock!\n");
  645. retval = PTR_ERR(prv->clk);
  646. goto error;
  647. }
  648. clk_enable(prv->clk);
  649. /* Reset NAND Flash controller */
  650. nfc_set(mtd, NFC_CONFIG1, NFC_RESET);
  651. while (nfc_read(mtd, NFC_CONFIG1) & NFC_RESET) {
  652. if (resettime++ >= NFC_RESET_TIMEOUT) {
  653. dev_err(dev, "Timeout while resetting NFC!\n");
  654. retval = -EINVAL;
  655. goto error;
  656. }
  657. udelay(1);
  658. }
  659. /* Enable write to NFC memory */
  660. nfc_write(mtd, NFC_CONFIG, NFC_BLS_UNLOCKED);
  661. /* Enable write to all NAND pages */
  662. nfc_write(mtd, NFC_UNLOCKSTART_BLK0, 0x0000);
  663. nfc_write(mtd, NFC_UNLOCKEND_BLK0, 0xFFFF);
  664. nfc_write(mtd, NFC_WRPROT, NFC_WPC_UNLOCK);
  665. /*
  666. * Setup NFC:
  667. * - Big Endian transfers,
  668. * - Interrupt after full page read/write.
  669. */
  670. nfc_write(mtd, NFC_CONFIG1, NFC_BIG_ENDIAN | NFC_INT_MASK |
  671. NFC_FULL_PAGE_INT);
  672. /* Set spare area size */
  673. nfc_write(mtd, NFC_SPAS, mtd->oobsize >> 1);
  674. init_waitqueue_head(&prv->irq_waitq);
  675. retval = devm_request_irq(dev, prv->irq, &mpc5121_nfc_irq, 0, DRV_NAME,
  676. mtd);
  677. if (retval) {
  678. dev_err(dev, "Error requesting IRQ!\n");
  679. goto error;
  680. }
  681. /* Detect NAND chips */
  682. if (nand_scan(mtd, be32_to_cpup(chips_no))) {
  683. dev_err(dev, "NAND Flash not found !\n");
  684. devm_free_irq(dev, prv->irq, mtd);
  685. retval = -ENXIO;
  686. goto error;
  687. }
  688. /* Set erase block size */
  689. switch (mtd->erasesize / mtd->writesize) {
  690. case 32:
  691. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_32);
  692. break;
  693. case 64:
  694. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_64);
  695. break;
  696. case 128:
  697. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_128);
  698. break;
  699. case 256:
  700. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_256);
  701. break;
  702. default:
  703. dev_err(dev, "Unsupported NAND flash!\n");
  704. devm_free_irq(dev, prv->irq, mtd);
  705. retval = -ENXIO;
  706. goto error;
  707. }
  708. dev_set_drvdata(dev, mtd);
  709. /* Register device in MTD */
  710. retval = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  711. if (retval) {
  712. dev_err(dev, "Error adding MTD device!\n");
  713. devm_free_irq(dev, prv->irq, mtd);
  714. goto error;
  715. }
  716. return 0;
  717. error:
  718. mpc5121_nfc_free(dev, mtd);
  719. return retval;
  720. }
  721. static int __devexit mpc5121_nfc_remove(struct platform_device *op)
  722. {
  723. struct device *dev = &op->dev;
  724. struct mtd_info *mtd = dev_get_drvdata(dev);
  725. struct nand_chip *chip = mtd->priv;
  726. struct mpc5121_nfc_prv *prv = chip->priv;
  727. nand_release(mtd);
  728. devm_free_irq(dev, prv->irq, mtd);
  729. mpc5121_nfc_free(dev, mtd);
  730. return 0;
  731. }
  732. static struct of_device_id mpc5121_nfc_match[] __devinitdata = {
  733. { .compatible = "fsl,mpc5121-nfc", },
  734. {},
  735. };
  736. static struct platform_driver mpc5121_nfc_driver = {
  737. .probe = mpc5121_nfc_probe,
  738. .remove = __devexit_p(mpc5121_nfc_remove),
  739. .driver = {
  740. .name = DRV_NAME,
  741. .owner = THIS_MODULE,
  742. .of_match_table = mpc5121_nfc_match,
  743. },
  744. };
  745. module_platform_driver(mpc5121_nfc_driver);
  746. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  747. MODULE_DESCRIPTION("MPC5121 NAND MTD driver");
  748. MODULE_LICENSE("GPL");