mpc5121_nfc.c 20 KB

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