1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright © 2008 Ilya Yanok, Emcraft Systems 4 */ 5 6 #include <linux/slab.h> 7 #include <linux/module.h> 8 #include <linux/mtd/mtd.h> 9 #include <linux/mtd/rawnand.h> 10 #include <linux/mtd/partitions.h> 11 #include <linux/of_address.h> 12 #include <linux/of_platform.h> 13 #include <linux/io.h> 14 15 #define FPGA_NAND_CMD_MASK (0x7 << 28) 16 #define FPGA_NAND_CMD_COMMAND (0x0 << 28) 17 #define FPGA_NAND_CMD_ADDR (0x1 << 28) 18 #define FPGA_NAND_CMD_READ (0x2 << 28) 19 #define FPGA_NAND_CMD_WRITE (0x3 << 28) 20 #define FPGA_NAND_BUSY (0x1 << 15) 21 #define FPGA_NAND_ENABLE (0x1 << 31) 22 #define FPGA_NAND_DATA_SHIFT 16 23 24 struct socrates_nand_host { 25 struct nand_controller controller; 26 struct nand_chip nand_chip; 27 void __iomem *io_base; 28 struct device *dev; 29 }; 30 31 /** 32 * socrates_nand_write_buf - write buffer to chip 33 * @this: NAND chip object 34 * @buf: data buffer 35 * @len: number of bytes to write 36 */ 37 static void socrates_nand_write_buf(struct nand_chip *this, const uint8_t *buf, 38 int len) 39 { 40 int i; 41 struct socrates_nand_host *host = nand_get_controller_data(this); 42 43 for (i = 0; i < len; i++) { 44 out_be32(host->io_base, FPGA_NAND_ENABLE | 45 FPGA_NAND_CMD_WRITE | 46 (buf[i] << FPGA_NAND_DATA_SHIFT)); 47 } 48 } 49 50 /** 51 * socrates_nand_read_buf - read chip data into buffer 52 * @this: NAND chip object 53 * @buf: buffer to store date 54 * @len: number of bytes to read 55 */ 56 static void socrates_nand_read_buf(struct nand_chip *this, uint8_t *buf, 57 int len) 58 { 59 int i; 60 struct socrates_nand_host *host = nand_get_controller_data(this); 61 uint32_t val; 62 63 val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ; 64 65 out_be32(host->io_base, val); 66 for (i = 0; i < len; i++) { 67 buf[i] = (in_be32(host->io_base) >> 68 FPGA_NAND_DATA_SHIFT) & 0xff; 69 } 70 } 71 72 /** 73 * socrates_nand_read_byte - read one byte from the chip 74 * @mtd: MTD device structure 75 */ 76 static uint8_t socrates_nand_read_byte(struct nand_chip *this) 77 { 78 uint8_t byte; 79 socrates_nand_read_buf(this, &byte, sizeof(byte)); 80 return byte; 81 } 82 83 /* 84 * Hardware specific access to control-lines 85 */ 86 static void socrates_nand_cmd_ctrl(struct nand_chip *nand_chip, int cmd, 87 unsigned int ctrl) 88 { 89 struct socrates_nand_host *host = nand_get_controller_data(nand_chip); 90 uint32_t val; 91 92 if (cmd == NAND_CMD_NONE) 93 return; 94 95 if (ctrl & NAND_CLE) 96 val = FPGA_NAND_CMD_COMMAND; 97 else 98 val = FPGA_NAND_CMD_ADDR; 99 100 if (ctrl & NAND_NCE) 101 val |= FPGA_NAND_ENABLE; 102 103 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT; 104 105 out_be32(host->io_base, val); 106 } 107 108 /* 109 * Read the Device Ready pin. 110 */ 111 static int socrates_nand_device_ready(struct nand_chip *nand_chip) 112 { 113 struct socrates_nand_host *host = nand_get_controller_data(nand_chip); 114 115 if (in_be32(host->io_base) & FPGA_NAND_BUSY) 116 return 0; /* busy */ 117 return 1; 118 } 119 120 static int socrates_attach_chip(struct nand_chip *chip) 121 { 122 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT; 123 chip->ecc.algo = NAND_ECC_ALGO_HAMMING; 124 125 return 0; 126 } 127 128 static const struct nand_controller_ops socrates_ops = { 129 .attach_chip = socrates_attach_chip, 130 }; 131 132 /* 133 * Probe for the NAND device. 134 */ 135 static int socrates_nand_probe(struct platform_device *ofdev) 136 { 137 struct socrates_nand_host *host; 138 struct mtd_info *mtd; 139 struct nand_chip *nand_chip; 140 int res; 141 142 /* Allocate memory for the device structure (and zero it) */ 143 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL); 144 if (!host) 145 return -ENOMEM; 146 147 host->io_base = of_iomap(ofdev->dev.of_node, 0); 148 if (host->io_base == NULL) { 149 dev_err(&ofdev->dev, "ioremap failed\n"); 150 return -EIO; 151 } 152 153 nand_chip = &host->nand_chip; 154 mtd = nand_to_mtd(nand_chip); 155 host->dev = &ofdev->dev; 156 157 nand_controller_init(&host->controller); 158 host->controller.ops = &socrates_ops; 159 nand_chip->controller = &host->controller; 160 161 /* link the private data structures */ 162 nand_set_controller_data(nand_chip, host); 163 nand_set_flash_node(nand_chip, ofdev->dev.of_node); 164 mtd->name = "socrates_nand"; 165 mtd->dev.parent = &ofdev->dev; 166 167 nand_chip->legacy.cmd_ctrl = socrates_nand_cmd_ctrl; 168 nand_chip->legacy.read_byte = socrates_nand_read_byte; 169 nand_chip->legacy.write_buf = socrates_nand_write_buf; 170 nand_chip->legacy.read_buf = socrates_nand_read_buf; 171 nand_chip->legacy.dev_ready = socrates_nand_device_ready; 172 173 /* TODO: I have no idea what real delay is. */ 174 nand_chip->legacy.chip_delay = 20; /* 20us command delay time */ 175 176 dev_set_drvdata(&ofdev->dev, host); 177 178 res = nand_scan(nand_chip, 1); 179 if (res) 180 goto out; 181 182 res = mtd_device_register(mtd, NULL, 0); 183 if (!res) 184 return res; 185 186 nand_cleanup(nand_chip); 187 188 out: 189 iounmap(host->io_base); 190 return res; 191 } 192 193 /* 194 * Remove a NAND device. 195 */ 196 static int socrates_nand_remove(struct platform_device *ofdev) 197 { 198 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev); 199 struct nand_chip *chip = &host->nand_chip; 200 int ret; 201 202 ret = mtd_device_unregister(nand_to_mtd(chip)); 203 WARN_ON(ret); 204 nand_cleanup(chip); 205 206 iounmap(host->io_base); 207 208 return 0; 209 } 210 211 static const struct of_device_id socrates_nand_match[] = 212 { 213 { 214 .compatible = "abb,socrates-nand", 215 }, 216 {}, 217 }; 218 219 MODULE_DEVICE_TABLE(of, socrates_nand_match); 220 221 static struct platform_driver socrates_nand_driver = { 222 .driver = { 223 .name = "socrates_nand", 224 .of_match_table = socrates_nand_match, 225 }, 226 .probe = socrates_nand_probe, 227 .remove = socrates_nand_remove, 228 }; 229 230 module_platform_driver(socrates_nand_driver); 231 232 MODULE_LICENSE("GPL"); 233 MODULE_AUTHOR("Ilya Yanok"); 234 MODULE_DESCRIPTION("NAND driver for Socrates board"); 235