mpc5121_nfc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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/rawnand.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 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_to_nand(mtd);
  123. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  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_to_nand(mtd);
  130. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  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_to_nand(mtd);
  190. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  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_to_nand(mtd);
  199. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  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_to_nand(mtd);
  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_to_nand(mtd);
  245. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  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_to_nand(mtd);
  263. struct mpc5121_nfc_prv *prv = nand_get_controller_data(nand);
  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_to_nand(mtd);
  288. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  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_to_nand(mtd);
  349. struct mpc5121_nfc_prv *prv = nand_get_controller_data(nand);
  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_to_nand(mtd);
  394. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  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. /* Read byte from NFC buffers */
  435. static u8 mpc5121_nfc_read_byte(struct mtd_info *mtd)
  436. {
  437. u8 tmp;
  438. mpc5121_nfc_read_buf(mtd, &tmp, sizeof(tmp));
  439. return tmp;
  440. }
  441. /* Read word from NFC buffers */
  442. static u16 mpc5121_nfc_read_word(struct mtd_info *mtd)
  443. {
  444. u16 tmp;
  445. mpc5121_nfc_read_buf(mtd, (u_char *)&tmp, sizeof(tmp));
  446. return tmp;
  447. }
  448. /*
  449. * Read NFC configuration from Reset Config Word
  450. *
  451. * NFC is configured during reset in basis of information stored
  452. * in Reset Config Word. There is no other way to set NAND block
  453. * size, spare size and bus width.
  454. */
  455. static int mpc5121_nfc_read_hw_config(struct mtd_info *mtd)
  456. {
  457. struct nand_chip *chip = mtd_to_nand(mtd);
  458. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  459. struct mpc512x_reset_module *rm;
  460. struct device_node *rmnode;
  461. uint rcw_pagesize = 0;
  462. uint rcw_sparesize = 0;
  463. uint rcw_width;
  464. uint rcwh;
  465. uint romloc, ps;
  466. int ret = 0;
  467. rmnode = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-reset");
  468. if (!rmnode) {
  469. dev_err(prv->dev, "Missing 'fsl,mpc5121-reset' "
  470. "node in device tree!\n");
  471. return -ENODEV;
  472. }
  473. rm = of_iomap(rmnode, 0);
  474. if (!rm) {
  475. dev_err(prv->dev, "Error mapping reset module node!\n");
  476. ret = -EBUSY;
  477. goto out;
  478. }
  479. rcwh = in_be32(&rm->rcwhr);
  480. /* Bit 6: NFC bus width */
  481. rcw_width = ((rcwh >> 6) & 0x1) ? 2 : 1;
  482. /* Bit 7: NFC Page/Spare size */
  483. ps = (rcwh >> 7) & 0x1;
  484. /* Bits [22:21]: ROM Location */
  485. romloc = (rcwh >> 21) & 0x3;
  486. /* Decode RCW bits */
  487. switch ((ps << 2) | romloc) {
  488. case 0x00:
  489. case 0x01:
  490. rcw_pagesize = 512;
  491. rcw_sparesize = 16;
  492. break;
  493. case 0x02:
  494. case 0x03:
  495. rcw_pagesize = 4096;
  496. rcw_sparesize = 128;
  497. break;
  498. case 0x04:
  499. case 0x05:
  500. rcw_pagesize = 2048;
  501. rcw_sparesize = 64;
  502. break;
  503. case 0x06:
  504. case 0x07:
  505. rcw_pagesize = 4096;
  506. rcw_sparesize = 218;
  507. break;
  508. }
  509. mtd->writesize = rcw_pagesize;
  510. mtd->oobsize = rcw_sparesize;
  511. if (rcw_width == 2)
  512. chip->options |= NAND_BUSWIDTH_16;
  513. dev_notice(prv->dev, "Configured for "
  514. "%u-bit NAND, page size %u "
  515. "with %u spare.\n",
  516. rcw_width * 8, rcw_pagesize,
  517. rcw_sparesize);
  518. iounmap(rm);
  519. out:
  520. of_node_put(rmnode);
  521. return ret;
  522. }
  523. /* Free driver resources */
  524. static void mpc5121_nfc_free(struct device *dev, struct mtd_info *mtd)
  525. {
  526. struct nand_chip *chip = mtd_to_nand(mtd);
  527. struct mpc5121_nfc_prv *prv = nand_get_controller_data(chip);
  528. if (prv->clk)
  529. clk_disable_unprepare(prv->clk);
  530. if (prv->csreg)
  531. iounmap(prv->csreg);
  532. }
  533. static int mpc5121_nfc_probe(struct platform_device *op)
  534. {
  535. struct device_node *dn = op->dev.of_node;
  536. struct clk *clk;
  537. struct device *dev = &op->dev;
  538. struct mpc5121_nfc_prv *prv;
  539. struct resource res;
  540. struct mtd_info *mtd;
  541. struct nand_chip *chip;
  542. unsigned long regs_paddr, regs_size;
  543. const __be32 *chips_no;
  544. int resettime = 0;
  545. int retval = 0;
  546. int rev, len;
  547. /*
  548. * Check SoC revision. This driver supports only NFC
  549. * in MPC5121 revision 2 and MPC5123 revision 3.
  550. */
  551. rev = (mfspr(SPRN_SVR) >> 4) & 0xF;
  552. if ((rev != 2) && (rev != 3)) {
  553. dev_err(dev, "SoC revision %u is not supported!\n", rev);
  554. return -ENXIO;
  555. }
  556. prv = devm_kzalloc(dev, sizeof(*prv), GFP_KERNEL);
  557. if (!prv)
  558. return -ENOMEM;
  559. chip = &prv->chip;
  560. mtd = nand_to_mtd(chip);
  561. mtd->dev.parent = dev;
  562. nand_set_controller_data(chip, prv);
  563. nand_set_flash_node(chip, dn);
  564. prv->dev = dev;
  565. /* Read NFC configuration from Reset Config Word */
  566. retval = mpc5121_nfc_read_hw_config(mtd);
  567. if (retval) {
  568. dev_err(dev, "Unable to read NFC config!\n");
  569. return retval;
  570. }
  571. prv->irq = irq_of_parse_and_map(dn, 0);
  572. if (prv->irq == NO_IRQ) {
  573. dev_err(dev, "Error mapping IRQ!\n");
  574. return -EINVAL;
  575. }
  576. retval = of_address_to_resource(dn, 0, &res);
  577. if (retval) {
  578. dev_err(dev, "Error parsing memory region!\n");
  579. return retval;
  580. }
  581. chips_no = of_get_property(dn, "chips", &len);
  582. if (!chips_no || len != sizeof(*chips_no)) {
  583. dev_err(dev, "Invalid/missing 'chips' property!\n");
  584. return -EINVAL;
  585. }
  586. regs_paddr = res.start;
  587. regs_size = resource_size(&res);
  588. if (!devm_request_mem_region(dev, regs_paddr, regs_size, DRV_NAME)) {
  589. dev_err(dev, "Error requesting memory region!\n");
  590. return -EBUSY;
  591. }
  592. prv->regs = devm_ioremap(dev, regs_paddr, regs_size);
  593. if (!prv->regs) {
  594. dev_err(dev, "Error mapping memory region!\n");
  595. return -ENOMEM;
  596. }
  597. mtd->name = "MPC5121 NAND";
  598. chip->dev_ready = mpc5121_nfc_dev_ready;
  599. chip->cmdfunc = mpc5121_nfc_command;
  600. chip->read_byte = mpc5121_nfc_read_byte;
  601. chip->read_word = mpc5121_nfc_read_word;
  602. chip->read_buf = mpc5121_nfc_read_buf;
  603. chip->write_buf = mpc5121_nfc_write_buf;
  604. chip->select_chip = mpc5121_nfc_select_chip;
  605. chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
  606. chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
  607. chip->bbt_options = NAND_BBT_USE_FLASH;
  608. chip->ecc.mode = NAND_ECC_SOFT;
  609. chip->ecc.algo = NAND_ECC_HAMMING;
  610. /* Support external chip-select logic on ADS5121 board */
  611. if (of_machine_is_compatible("fsl,mpc5121ads")) {
  612. retval = ads5121_chipselect_init(mtd);
  613. if (retval) {
  614. dev_err(dev, "Chipselect init error!\n");
  615. return retval;
  616. }
  617. chip->select_chip = ads5121_select_chip;
  618. }
  619. /* Enable NFC clock */
  620. clk = devm_clk_get(dev, "ipg");
  621. if (IS_ERR(clk)) {
  622. dev_err(dev, "Unable to acquire NFC clock!\n");
  623. retval = PTR_ERR(clk);
  624. goto error;
  625. }
  626. retval = clk_prepare_enable(clk);
  627. if (retval) {
  628. dev_err(dev, "Unable to enable NFC clock!\n");
  629. goto error;
  630. }
  631. prv->clk = clk;
  632. /* Reset NAND Flash controller */
  633. nfc_set(mtd, NFC_CONFIG1, NFC_RESET);
  634. while (nfc_read(mtd, NFC_CONFIG1) & NFC_RESET) {
  635. if (resettime++ >= NFC_RESET_TIMEOUT) {
  636. dev_err(dev, "Timeout while resetting NFC!\n");
  637. retval = -EINVAL;
  638. goto error;
  639. }
  640. udelay(1);
  641. }
  642. /* Enable write to NFC memory */
  643. nfc_write(mtd, NFC_CONFIG, NFC_BLS_UNLOCKED);
  644. /* Enable write to all NAND pages */
  645. nfc_write(mtd, NFC_UNLOCKSTART_BLK0, 0x0000);
  646. nfc_write(mtd, NFC_UNLOCKEND_BLK0, 0xFFFF);
  647. nfc_write(mtd, NFC_WRPROT, NFC_WPC_UNLOCK);
  648. /*
  649. * Setup NFC:
  650. * - Big Endian transfers,
  651. * - Interrupt after full page read/write.
  652. */
  653. nfc_write(mtd, NFC_CONFIG1, NFC_BIG_ENDIAN | NFC_INT_MASK |
  654. NFC_FULL_PAGE_INT);
  655. /* Set spare area size */
  656. nfc_write(mtd, NFC_SPAS, mtd->oobsize >> 1);
  657. init_waitqueue_head(&prv->irq_waitq);
  658. retval = devm_request_irq(dev, prv->irq, &mpc5121_nfc_irq, 0, DRV_NAME,
  659. mtd);
  660. if (retval) {
  661. dev_err(dev, "Error requesting IRQ!\n");
  662. goto error;
  663. }
  664. /* Detect NAND chips */
  665. retval = nand_scan(mtd, be32_to_cpup(chips_no));
  666. if (retval) {
  667. dev_err(dev, "NAND Flash not found !\n");
  668. goto error;
  669. }
  670. /* Set erase block size */
  671. switch (mtd->erasesize / mtd->writesize) {
  672. case 32:
  673. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_32);
  674. break;
  675. case 64:
  676. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_64);
  677. break;
  678. case 128:
  679. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_128);
  680. break;
  681. case 256:
  682. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_256);
  683. break;
  684. default:
  685. dev_err(dev, "Unsupported NAND flash!\n");
  686. retval = -ENXIO;
  687. goto error;
  688. }
  689. dev_set_drvdata(dev, mtd);
  690. /* Register device in MTD */
  691. retval = mtd_device_register(mtd, NULL, 0);
  692. if (retval) {
  693. dev_err(dev, "Error adding MTD device!\n");
  694. goto error;
  695. }
  696. return 0;
  697. error:
  698. mpc5121_nfc_free(dev, mtd);
  699. return retval;
  700. }
  701. static int mpc5121_nfc_remove(struct platform_device *op)
  702. {
  703. struct device *dev = &op->dev;
  704. struct mtd_info *mtd = dev_get_drvdata(dev);
  705. nand_release(mtd_to_nand(mtd));
  706. mpc5121_nfc_free(dev, mtd);
  707. return 0;
  708. }
  709. static const struct of_device_id mpc5121_nfc_match[] = {
  710. { .compatible = "fsl,mpc5121-nfc", },
  711. {},
  712. };
  713. MODULE_DEVICE_TABLE(of, mpc5121_nfc_match);
  714. static struct platform_driver mpc5121_nfc_driver = {
  715. .probe = mpc5121_nfc_probe,
  716. .remove = mpc5121_nfc_remove,
  717. .driver = {
  718. .name = DRV_NAME,
  719. .of_match_table = mpc5121_nfc_match,
  720. },
  721. };
  722. module_platform_driver(mpc5121_nfc_driver);
  723. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  724. MODULE_DESCRIPTION("MPC5121 NAND MTD driver");
  725. MODULE_LICENSE("GPL");