mpc5121_nfc.c 21 KB

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