Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mtd/nand/socrates_nand.c |
| 3 | * |
| 4 | * Copyright © 2008 Ilya Yanok, Emcraft Systems |
| 5 | * |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mtd/mtd.h> |
| 16 | #include <linux/mtd/nand.h> |
| 17 | #include <linux/mtd/partitions.h> |
| 18 | #include <linux/of_platform.h> |
| 19 | #include <linux/io.h> |
| 20 | |
| 21 | #define FPGA_NAND_CMD_MASK (0x7 << 28) |
| 22 | #define FPGA_NAND_CMD_COMMAND (0x0 << 28) |
| 23 | #define FPGA_NAND_CMD_ADDR (0x1 << 28) |
| 24 | #define FPGA_NAND_CMD_READ (0x2 << 28) |
| 25 | #define FPGA_NAND_CMD_WRITE (0x3 << 28) |
| 26 | #define FPGA_NAND_BUSY (0x1 << 15) |
| 27 | #define FPGA_NAND_ENABLE (0x1 << 31) |
| 28 | #define FPGA_NAND_DATA_SHIFT 16 |
| 29 | |
| 30 | struct socrates_nand_host { |
| 31 | struct nand_chip nand_chip; |
| 32 | struct mtd_info mtd; |
| 33 | void __iomem *io_base; |
| 34 | struct device *dev; |
| 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * socrates_nand_write_buf - write buffer to chip |
| 39 | * @mtd: MTD device structure |
| 40 | * @buf: data buffer |
| 41 | * @len: number of bytes to write |
| 42 | */ |
| 43 | static void socrates_nand_write_buf(struct mtd_info *mtd, |
| 44 | const uint8_t *buf, int len) |
| 45 | { |
| 46 | int i; |
| 47 | struct nand_chip *this = mtd->priv; |
| 48 | struct socrates_nand_host *host = this->priv; |
| 49 | |
| 50 | for (i = 0; i < len; i++) { |
| 51 | out_be32(host->io_base, FPGA_NAND_ENABLE | |
| 52 | FPGA_NAND_CMD_WRITE | |
| 53 | (buf[i] << FPGA_NAND_DATA_SHIFT)); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * socrates_nand_read_buf - read chip data into buffer |
| 59 | * @mtd: MTD device structure |
| 60 | * @buf: buffer to store date |
| 61 | * @len: number of bytes to read |
| 62 | */ |
| 63 | static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 64 | { |
| 65 | int i; |
| 66 | struct nand_chip *this = mtd->priv; |
| 67 | struct socrates_nand_host *host = this->priv; |
| 68 | uint32_t val; |
| 69 | |
| 70 | val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ; |
| 71 | |
| 72 | out_be32(host->io_base, val); |
| 73 | for (i = 0; i < len; i++) { |
| 74 | buf[i] = (in_be32(host->io_base) >> |
| 75 | FPGA_NAND_DATA_SHIFT) & 0xff; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * socrates_nand_read_byte - read one byte from the chip |
| 81 | * @mtd: MTD device structure |
| 82 | */ |
| 83 | static uint8_t socrates_nand_read_byte(struct mtd_info *mtd) |
| 84 | { |
| 85 | uint8_t byte; |
| 86 | socrates_nand_read_buf(mtd, &byte, sizeof(byte)); |
| 87 | return byte; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * socrates_nand_read_word - read one word from the chip |
| 92 | * @mtd: MTD device structure |
| 93 | */ |
| 94 | static uint16_t socrates_nand_read_word(struct mtd_info *mtd) |
| 95 | { |
| 96 | uint16_t word; |
| 97 | socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word)); |
| 98 | return word; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * socrates_nand_verify_buf - Verify chip data against buffer |
| 103 | * @mtd: MTD device structure |
| 104 | * @buf: buffer containing the data to compare |
| 105 | * @len: number of bytes to compare |
| 106 | */ |
| 107 | static int socrates_nand_verify_buf(struct mtd_info *mtd, const u8 *buf, |
| 108 | int len) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | for (i = 0; i < len; i++) { |
| 113 | if (buf[i] != socrates_nand_read_byte(mtd)) |
| 114 | return -EFAULT; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Hardware specific access to control-lines |
| 121 | */ |
| 122 | static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, |
| 123 | unsigned int ctrl) |
| 124 | { |
| 125 | struct nand_chip *nand_chip = mtd->priv; |
| 126 | struct socrates_nand_host *host = nand_chip->priv; |
| 127 | uint32_t val; |
| 128 | |
| 129 | if (cmd == NAND_CMD_NONE) |
| 130 | return; |
| 131 | |
| 132 | if (ctrl & NAND_CLE) |
| 133 | val = FPGA_NAND_CMD_COMMAND; |
| 134 | else |
| 135 | val = FPGA_NAND_CMD_ADDR; |
| 136 | |
| 137 | if (ctrl & NAND_NCE) |
| 138 | val |= FPGA_NAND_ENABLE; |
| 139 | |
| 140 | val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT; |
| 141 | |
| 142 | out_be32(host->io_base, val); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Read the Device Ready pin. |
| 147 | */ |
| 148 | static int socrates_nand_device_ready(struct mtd_info *mtd) |
| 149 | { |
| 150 | struct nand_chip *nand_chip = mtd->priv; |
| 151 | struct socrates_nand_host *host = nand_chip->priv; |
| 152 | |
| 153 | if (in_be32(host->io_base) & FPGA_NAND_BUSY) |
| 154 | return 0; /* busy */ |
| 155 | return 1; |
| 156 | } |
| 157 | |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 158 | static const char *part_probes[] = { "cmdlinepart", NULL }; |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Probe for the NAND device. |
| 162 | */ |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 163 | static int __devinit socrates_nand_probe(struct platform_device *ofdev) |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 164 | { |
| 165 | struct socrates_nand_host *host; |
| 166 | struct mtd_info *mtd; |
| 167 | struct nand_chip *nand_chip; |
| 168 | int res; |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 169 | struct mtd_partition *partitions = NULL; |
| 170 | int num_partitions = 0; |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 171 | |
| 172 | /* Allocate memory for the device structure (and zero it) */ |
| 173 | host = kzalloc(sizeof(struct socrates_nand_host), GFP_KERNEL); |
| 174 | if (!host) { |
| 175 | printk(KERN_ERR |
| 176 | "socrates_nand: failed to allocate device structure.\n"); |
| 177 | return -ENOMEM; |
| 178 | } |
| 179 | |
Anatolij Gustschin | c8a4d0f | 2010-06-03 02:37:17 +0200 | [diff] [blame] | 180 | host->io_base = of_iomap(ofdev->dev.of_node, 0); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 181 | if (host->io_base == NULL) { |
| 182 | printk(KERN_ERR "socrates_nand: ioremap failed\n"); |
| 183 | kfree(host); |
| 184 | return -EIO; |
| 185 | } |
| 186 | |
| 187 | mtd = &host->mtd; |
| 188 | nand_chip = &host->nand_chip; |
| 189 | host->dev = &ofdev->dev; |
| 190 | |
| 191 | nand_chip->priv = host; /* link the private data structures */ |
| 192 | mtd->priv = nand_chip; |
| 193 | mtd->name = "socrates_nand"; |
| 194 | mtd->owner = THIS_MODULE; |
| 195 | mtd->dev.parent = &ofdev->dev; |
| 196 | |
| 197 | /*should never be accessed directly */ |
| 198 | nand_chip->IO_ADDR_R = (void *)0xdeadbeef; |
| 199 | nand_chip->IO_ADDR_W = (void *)0xdeadbeef; |
| 200 | |
| 201 | nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl; |
| 202 | nand_chip->read_byte = socrates_nand_read_byte; |
| 203 | nand_chip->read_word = socrates_nand_read_word; |
| 204 | nand_chip->write_buf = socrates_nand_write_buf; |
| 205 | nand_chip->read_buf = socrates_nand_read_buf; |
| 206 | nand_chip->verify_buf = socrates_nand_verify_buf; |
| 207 | nand_chip->dev_ready = socrates_nand_device_ready; |
| 208 | |
| 209 | nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */ |
| 210 | |
| 211 | /* TODO: I have no idea what real delay is. */ |
| 212 | nand_chip->chip_delay = 20; /* 20us command delay time */ |
| 213 | |
| 214 | dev_set_drvdata(&ofdev->dev, host); |
| 215 | |
| 216 | /* first scan to find the device and get the page size */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 217 | if (nand_scan_ident(mtd, 1, NULL)) { |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 218 | res = -ENXIO; |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | /* second phase scan */ |
| 223 | if (nand_scan_tail(mtd)) { |
| 224 | res = -ENXIO; |
| 225 | goto out; |
| 226 | } |
| 227 | |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 228 | #ifdef CONFIG_MTD_CMDLINE_PARTS |
| 229 | num_partitions = parse_mtd_partitions(mtd, part_probes, |
| 230 | &partitions, 0); |
| 231 | if (num_partitions < 0) { |
| 232 | res = num_partitions; |
| 233 | goto release; |
| 234 | } |
| 235 | #endif |
| 236 | |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 237 | if (num_partitions == 0) { |
| 238 | num_partitions = of_mtd_parse_partitions(&ofdev->dev, |
Anatolij Gustschin | c8a4d0f | 2010-06-03 02:37:17 +0200 | [diff] [blame] | 239 | ofdev->dev.of_node, |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 240 | &partitions); |
| 241 | if (num_partitions < 0) { |
| 242 | res = num_partitions; |
| 243 | goto release; |
| 244 | } |
| 245 | } |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 246 | |
Jamie Iles | 5667bc8 | 2011-05-23 10:23:35 +0100 | [diff] [blame] | 247 | res = mtd_device_register(mtd, partitions, num_partitions); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 248 | if (!res) |
| 249 | return res; |
| 250 | |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 251 | release: |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 252 | nand_release(mtd); |
| 253 | |
| 254 | out: |
| 255 | dev_set_drvdata(&ofdev->dev, NULL); |
| 256 | iounmap(host->io_base); |
| 257 | kfree(host); |
| 258 | return res; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Remove a NAND device. |
| 263 | */ |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 264 | static int __devexit socrates_nand_remove(struct platform_device *ofdev) |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 265 | { |
| 266 | struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev); |
| 267 | struct mtd_info *mtd = &host->mtd; |
| 268 | |
| 269 | nand_release(mtd); |
| 270 | |
| 271 | dev_set_drvdata(&ofdev->dev, NULL); |
| 272 | iounmap(host->io_base); |
| 273 | kfree(host); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Márton Németh | b2d4fba | 2010-01-09 15:10:46 +0100 | [diff] [blame] | 278 | static const struct of_device_id socrates_nand_match[] = |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 279 | { |
| 280 | { |
| 281 | .compatible = "abb,socrates-nand", |
| 282 | }, |
| 283 | {}, |
| 284 | }; |
| 285 | |
| 286 | MODULE_DEVICE_TABLE(of, socrates_nand_match); |
| 287 | |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 288 | static struct platform_driver socrates_nand_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 289 | .driver = { |
| 290 | .name = "socrates_nand", |
| 291 | .owner = THIS_MODULE, |
| 292 | .of_match_table = socrates_nand_match, |
| 293 | }, |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 294 | .probe = socrates_nand_probe, |
| 295 | .remove = __devexit_p(socrates_nand_remove), |
| 296 | }; |
| 297 | |
| 298 | static int __init socrates_nand_init(void) |
| 299 | { |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 300 | return platform_driver_register(&socrates_nand_driver); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static void __exit socrates_nand_exit(void) |
| 304 | { |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 305 | platform_driver_unregister(&socrates_nand_driver); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | module_init(socrates_nand_init); |
| 309 | module_exit(socrates_nand_exit); |
| 310 | |
| 311 | MODULE_LICENSE("GPL"); |
| 312 | MODULE_AUTHOR("Ilya Yanok"); |
| 313 | MODULE_DESCRIPTION("NAND driver for Socrates board"); |