mpc5121_nfc.c 21 KB

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