Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 |
| 4 | * Sergei Poselenov, Emcraft Systems, sposelenov@emcraft.com. |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 7 | #include <config.h> |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 8 | |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 9 | #if defined(CFG_SYS_NAND_BASE) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 10 | #include <nand.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 11 | #include <linux/errno.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 12 | #include <linux/mtd/rawnand.h> |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | |
| 15 | static int state; |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 16 | static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte); |
| 17 | static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len); |
| 18 | static u_char sc_nand_read_byte(struct mtd_info *mtd); |
| 19 | static u16 sc_nand_read_word(struct mtd_info *mtd); |
| 20 | static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len); |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 21 | static int sc_nand_device_ready(struct mtd_info *mtdinfo); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 22 | |
| 23 | #define FPGA_NAND_CMD_MASK (0x7 << 28) |
Scott Wood | 81cb808 | 2008-08-13 18:24:05 -0500 | [diff] [blame] | 24 | #define FPGA_NAND_CMD_COMMAND (0x0 << 28) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 25 | #define FPGA_NAND_CMD_ADDR (0x1 << 28) |
| 26 | #define FPGA_NAND_CMD_READ (0x2 << 28) |
| 27 | #define FPGA_NAND_CMD_WRITE (0x3 << 28) |
| 28 | #define FPGA_NAND_BUSY (0x1 << 15) |
| 29 | #define FPGA_NAND_ENABLE (0x1 << 31) |
Scott Wood | 81cb808 | 2008-08-13 18:24:05 -0500 | [diff] [blame] | 30 | #define FPGA_NAND_DATA_SHIFT 16 |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 31 | |
| 32 | /** |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 33 | * sc_nand_write_byte - write one byte to the chip |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 34 | * @mtd: MTD device structure |
| 35 | * @byte: pointer to data byte to write |
| 36 | */ |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 37 | static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 38 | { |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 39 | sc_nand_write_buf(mtd, (const uchar *)&byte, sizeof(byte)); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 43 | * sc_nand_write_buf - write buffer to chip |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 44 | * @mtd: MTD device structure |
| 45 | * @buf: data buffer |
| 46 | * @len: number of bytes to write |
| 47 | */ |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 48 | static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 49 | { |
| 50 | int i; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 51 | struct nand_chip *this = mtd_to_nand(mtd); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 52 | |
| 53 | for (i = 0; i < len; i++) { |
Scott Wood | 81cb808 | 2008-08-13 18:24:05 -0500 | [diff] [blame] | 54 | out_be32(this->IO_ADDR_W, |
| 55 | state | (buf[i] << FPGA_NAND_DATA_SHIFT)); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 59 | /** |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 60 | * sc_nand_read_byte - read one byte from the chip |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 61 | * @mtd: MTD device structure |
| 62 | */ |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 63 | static u_char sc_nand_read_byte(struct mtd_info *mtd) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 64 | { |
| 65 | u8 byte; |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 66 | sc_nand_read_buf(mtd, (uchar *)&byte, sizeof(byte)); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 67 | return byte; |
| 68 | } |
| 69 | |
| 70 | /** |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 71 | * sc_nand_read_word - read one word from the chip |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 72 | * @mtd: MTD device structure |
| 73 | */ |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 74 | static u16 sc_nand_read_word(struct mtd_info *mtd) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 75 | { |
| 76 | u16 word; |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 77 | sc_nand_read_buf(mtd, (uchar *)&word, sizeof(word)); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 78 | return word; |
| 79 | } |
| 80 | |
| 81 | /** |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 82 | * sc_nand_read_buf - read chip data into buffer |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 83 | * @mtd: MTD device structure |
| 84 | * @buf: buffer to store date |
| 85 | * @len: number of bytes to read |
| 86 | */ |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 87 | static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 88 | { |
| 89 | int i; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 90 | struct nand_chip *this = mtd_to_nand(mtd); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 91 | int val; |
| 92 | |
| 93 | val = (state & FPGA_NAND_ENABLE) | FPGA_NAND_CMD_READ; |
| 94 | |
| 95 | out_be32(this->IO_ADDR_W, val); |
| 96 | for (i = 0; i < len; i++) { |
| 97 | buf[i] = (in_be32(this->IO_ADDR_R) >> FPGA_NAND_DATA_SHIFT) & 0xff; |
| 98 | } |
| 99 | } |
| 100 | |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 101 | /** |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 102 | * sc_nand_device_ready - Check the NAND device is ready for next command. |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 103 | * @mtd: MTD device structure |
| 104 | */ |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 105 | static int sc_nand_device_ready(struct mtd_info *mtdinfo) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 106 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 107 | struct nand_chip *this = mtd_to_nand(mtdinfo); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 108 | |
| 109 | if (in_be32(this->IO_ADDR_W) & FPGA_NAND_BUSY) |
| 110 | return 0; /* busy */ |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | /** |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 115 | * sc_nand_hwcontrol - NAND control functions wrapper. |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 116 | * @mtd: MTD device structure |
| 117 | * @cmd: Command |
| 118 | */ |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 119 | static void sc_nand_hwcontrol(struct mtd_info *mtdinfo, int cmd, unsigned int ctrl) |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 120 | { |
Scott Wood | 81cb808 | 2008-08-13 18:24:05 -0500 | [diff] [blame] | 121 | if (ctrl & NAND_CTRL_CHANGE) { |
| 122 | state &= ~(FPGA_NAND_CMD_MASK | FPGA_NAND_ENABLE); |
| 123 | |
| 124 | switch (ctrl & (NAND_ALE | NAND_CLE)) { |
| 125 | case 0: |
| 126 | state |= FPGA_NAND_CMD_WRITE; |
| 127 | break; |
| 128 | |
| 129 | case NAND_ALE: |
| 130 | state |= FPGA_NAND_CMD_ADDR; |
| 131 | break; |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 132 | |
Scott Wood | 81cb808 | 2008-08-13 18:24:05 -0500 | [diff] [blame] | 133 | case NAND_CLE: |
| 134 | state |= FPGA_NAND_CMD_COMMAND; |
| 135 | break; |
| 136 | |
| 137 | default: |
| 138 | printf("%s: unknown ctrl %#x\n", __FUNCTION__, ctrl); |
| 139 | } |
| 140 | |
| 141 | if (ctrl & NAND_NCE) |
| 142 | state |= FPGA_NAND_ENABLE; |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 143 | } |
Scott Wood | 81cb808 | 2008-08-13 18:24:05 -0500 | [diff] [blame] | 144 | |
| 145 | if (cmd != NAND_CMD_NONE) |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 146 | sc_nand_write_byte(mtdinfo, cmd); |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int board_nand_init(struct nand_chip *nand) |
| 150 | { |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 151 | nand->cmd_ctrl = sc_nand_hwcontrol; |
Scott Wood | 81cb808 | 2008-08-13 18:24:05 -0500 | [diff] [blame] | 152 | nand->ecc.mode = NAND_ECC_SOFT; |
Marek Vasut | 67450a3 | 2011-10-04 00:56:09 +0200 | [diff] [blame] | 153 | nand->dev_ready = sc_nand_device_ready; |
| 154 | nand->read_byte = sc_nand_read_byte; |
| 155 | nand->read_word = sc_nand_read_word; |
| 156 | nand->write_buf = sc_nand_write_buf; |
| 157 | nand->read_buf = sc_nand_read_buf; |
Sergei Poselenov | 30115a0 | 2008-06-06 15:42:44 +0200 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | #endif |