28 #include <linux/module.h>
45 #define NFC_MAIN_AREA(n) ((n) * 0x200)
48 #define NFC_SPARE_BUFFERS 8
49 #define NFC_SPARE_LEN 0x40
50 #define NFC_SPARE_AREA(n) (0x1000 + ((n) * NFC_SPARE_LEN))
53 #define NFC_BUF_ADDR 0x1E04
54 #define NFC_FLASH_ADDR 0x1E06
55 #define NFC_FLASH_CMD 0x1E08
56 #define NFC_CONFIG 0x1E0A
57 #define NFC_ECC_STATUS1 0x1E0C
58 #define NFC_ECC_STATUS2 0x1E0E
59 #define NFC_SPAS 0x1E10
60 #define NFC_WRPROT 0x1E12
61 #define NFC_NF_WRPRST 0x1E18
62 #define NFC_CONFIG1 0x1E1A
63 #define NFC_CONFIG2 0x1E1C
64 #define NFC_UNLOCKSTART_BLK0 0x1E20
65 #define NFC_UNLOCKEND_BLK0 0x1E22
66 #define NFC_UNLOCKSTART_BLK1 0x1E24
67 #define NFC_UNLOCKEND_BLK1 0x1E26
68 #define NFC_UNLOCKSTART_BLK2 0x1E28
69 #define NFC_UNLOCKEND_BLK2 0x1E2A
70 #define NFC_UNLOCKSTART_BLK3 0x1E2C
71 #define NFC_UNLOCKEND_BLK3 0x1E2E
74 #define NFC_RBA_MASK (7 << 0)
75 #define NFC_ACTIVE_CS_SHIFT 5
76 #define NFC_ACTIVE_CS_MASK (3 << NFC_ACTIVE_CS_SHIFT)
79 #define NFC_BLS_UNLOCKED (1 << 1)
82 #define NFC_ECC_4BIT (1 << 0)
83 #define NFC_FULL_PAGE_DMA (1 << 1)
84 #define NFC_SPARE_ONLY (1 << 2)
85 #define NFC_ECC_ENABLE (1 << 3)
86 #define NFC_INT_MASK (1 << 4)
87 #define NFC_BIG_ENDIAN (1 << 5)
88 #define NFC_RESET (1 << 6)
89 #define NFC_CE (1 << 7)
90 #define NFC_ONE_CYCLE (1 << 8)
91 #define NFC_PPB_32 (0 << 9)
92 #define NFC_PPB_64 (1 << 9)
93 #define NFC_PPB_128 (2 << 9)
94 #define NFC_PPB_256 (3 << 9)
95 #define NFC_PPB_MASK (3 << 9)
96 #define NFC_FULL_PAGE_INT (1 << 11)
99 #define NFC_COMMAND (1 << 0)
100 #define NFC_ADDRESS (1 << 1)
101 #define NFC_INPUT (1 << 2)
102 #define NFC_OUTPUT (1 << 3)
103 #define NFC_ID (1 << 4)
104 #define NFC_STATUS (1 << 5)
105 #define NFC_CMD_FAIL (1 << 15)
106 #define NFC_INT (1 << 15)
109 #define NFC_WPC_LOCK_TIGHT (1 << 0)
110 #define NFC_WPC_LOCK (1 << 1)
111 #define NFC_WPC_UNLOCK (1 << 2)
113 #define DRV_NAME "mpc5121_nfc"
116 #define NFC_RESET_TIMEOUT 1000
117 #define NFC_TIMEOUT (HZ / 10)
132 static void mpc5121_nfc_done(
struct mtd_info *mtd);
155 nfc_write(mtd, reg, nfc_read(mtd, reg) | bits);
161 nfc_write(mtd, reg, nfc_read(mtd, reg) & ~bits);
165 static inline void mpc5121_nfc_send_addr(
struct mtd_info *mtd,
u16 addr)
169 mpc5121_nfc_done(mtd);
173 static inline void mpc5121_nfc_send_cmd(
struct mtd_info *mtd,
u16 cmd)
177 mpc5121_nfc_done(mtd);
181 static inline void mpc5121_nfc_send_prog_page(
struct mtd_info *mtd)
185 mpc5121_nfc_done(mtd);
189 static inline void mpc5121_nfc_send_read_page(
struct mtd_info *mtd)
193 mpc5121_nfc_done(mtd);
197 static inline void mpc5121_nfc_send_read_id(
struct mtd_info *mtd)
201 mpc5121_nfc_done(mtd);
205 static inline void mpc5121_nfc_send_read_status(
struct mtd_info *mtd)
209 mpc5121_nfc_done(mtd);
226 static void mpc5121_nfc_done(
struct mtd_info *mtd)
239 "Timeout while waiting for interrupt.\n");
252 mpc5121_nfc_send_addr(mtd, column);
254 mpc5121_nfc_send_addr(mtd, column >> 8);
259 mpc5121_nfc_send_addr(mtd, page & 0xFF);
267 static void mpc5121_nfc_select_chip(
struct mtd_info *mtd,
int chip)
281 static int ads5121_chipselect_init(
struct mtd_info *mtd)
303 static void ads5121_select_chip(
struct mtd_info *mtd,
int chip)
313 mpc5121_nfc_select_chip(mtd, 0);
316 mpc5121_nfc_select_chip(mtd, -1);
322 static int mpc5121_nfc_dev_ready(
struct mtd_info *mtd)
332 static void mpc5121_nfc_command(
struct mtd_info *mtd,
unsigned command,
333 int column,
int page)
338 prv->
column = (column >= 0) ? column : 0;
343 mpc5121_nfc_send_prog_page(mtd);
380 mpc5121_nfc_send_cmd(mtd, command);
381 mpc5121_nfc_addr_cycle(mtd, column, page);
387 mpc5121_nfc_send_read_page(mtd);
391 mpc5121_nfc_send_read_id(mtd);
395 mpc5121_nfc_send_read_status(mtd);
410 uint o,
s, sbsize, blksize;
440 o = offset - (s * sbsize);
441 blksize =
min(sbsize - o, size);
472 mpc5121_nfc_copy_spare(mtd, c, buf, len, wr);
492 mpc5121_nfc_buf_copy(mtd, buf, len, wr);
497 static void mpc5121_nfc_read_buf(
struct mtd_info *mtd,
u_char *buf,
int len)
499 mpc5121_nfc_buf_copy(mtd, buf, len, 0);
503 static void mpc5121_nfc_write_buf(
struct mtd_info *mtd,
504 const u_char *buf,
int len)
506 mpc5121_nfc_buf_copy(mtd, (
u_char *)buf, len, 1);
510 static u8 mpc5121_nfc_read_byte(
struct mtd_info *mtd)
514 mpc5121_nfc_read_buf(mtd, &tmp,
sizeof(tmp));
520 static u16 mpc5121_nfc_read_word(
struct mtd_info *mtd)
524 mpc5121_nfc_read_buf(mtd, (
u_char *)&tmp,
sizeof(tmp));
536 static int mpc5121_nfc_read_hw_config(
struct mtd_info *mtd)
542 uint rcw_pagesize = 0;
543 uint rcw_sparesize = 0;
551 dev_err(prv->
dev,
"Missing 'fsl,mpc5121-reset' "
552 "node in device tree!\n");
558 dev_err(prv->
dev,
"Error mapping reset module node!\n");
566 rcw_width = ((rcwh >> 6) & 0x1) ? 2 : 1;
569 ps = (rcwh >> 7) & 0x1;
572 romloc = (rcwh >> 21) & 0x3;
575 switch ((ps << 2) | romloc) {
603 dev_notice(prv->
dev,
"Configured for "
604 "%u-bit NAND, page size %u "
606 rcw_width * 8, rcw_pagesize,
637 unsigned long regs_paddr, regs_size;
648 rev = (
mfspr(SPRN_SVR) >> 4) & 0xF;
649 if ((rev != 2) && (rev != 3)) {
650 dev_err(dev,
"SoC revision %u is not supported!\n", rev);
656 dev_err(dev,
"Memory exhausted!\n");
668 retval = mpc5121_nfc_read_hw_config(mtd);
670 dev_err(dev,
"Unable to read NFC config!\n");
676 dev_err(dev,
"Error mapping IRQ!\n");
682 dev_err(dev,
"Error parsing memory region!\n");
687 if (!chips_no || len !=
sizeof(*chips_no)) {
688 dev_err(dev,
"Invalid/missing 'chips' property!\n");
692 regs_paddr =
res.start;
693 regs_size = resource_size(&
res);
696 dev_err(dev,
"Error requesting memory region!\n");
702 dev_err(dev,
"Error mapping memory region!\n");
706 mtd->
name =
"MPC5121 NAND";
709 chip->
cmdfunc = mpc5121_nfc_command;
712 chip->
read_buf = mpc5121_nfc_read_buf;
721 retval = ads5121_chipselect_init(mtd);
723 dev_err(dev,
"Chipselect init error!\n");
724 of_node_put(rootnode);
730 of_node_put(rootnode);
734 if (IS_ERR(prv->
clk)) {
735 dev_err(dev,
"Unable to acquire NFC clock!\n");
736 retval = PTR_ERR(prv->
clk);
746 dev_err(dev,
"Timeout while resetting NFC!\n");
774 retval = devm_request_irq(dev, prv->
irq, &mpc5121_nfc_irq, 0,
DRV_NAME,
777 dev_err(dev,
"Error requesting IRQ!\n");
783 dev_err(dev,
"NAND Flash not found !\n");
808 dev_err(dev,
"Unsupported NAND flash!\n");
819 dev_err(dev,
"Error adding MTD device!\n");
826 mpc5121_nfc_free(dev, mtd);
839 mpc5121_nfc_free(dev, mtd);
845 { .compatible =
"fsl,mpc5121-nfc", },
850 .probe = mpc5121_nfc_probe,
855 .of_match_table = mpc5121_nfc_match,