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> |
Rob Herring | c11eede | 2013-11-10 23:19:08 -0600 | [diff] [blame] | 18 | #include <linux/of_address.h> |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 19 | #include <linux/of_platform.h> |
| 20 | #include <linux/io.h> |
| 21 | |
| 22 | #define FPGA_NAND_CMD_MASK (0x7 << 28) |
| 23 | #define FPGA_NAND_CMD_COMMAND (0x0 << 28) |
| 24 | #define FPGA_NAND_CMD_ADDR (0x1 << 28) |
| 25 | #define FPGA_NAND_CMD_READ (0x2 << 28) |
| 26 | #define FPGA_NAND_CMD_WRITE (0x3 << 28) |
| 27 | #define FPGA_NAND_BUSY (0x1 << 15) |
| 28 | #define FPGA_NAND_ENABLE (0x1 << 31) |
| 29 | #define FPGA_NAND_DATA_SHIFT 16 |
| 30 | |
| 31 | struct socrates_nand_host { |
| 32 | struct nand_chip nand_chip; |
| 33 | struct mtd_info mtd; |
| 34 | void __iomem *io_base; |
| 35 | struct device *dev; |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * socrates_nand_write_buf - write buffer to chip |
| 40 | * @mtd: MTD device structure |
| 41 | * @buf: data buffer |
| 42 | * @len: number of bytes to write |
| 43 | */ |
| 44 | static void socrates_nand_write_buf(struct mtd_info *mtd, |
| 45 | const uint8_t *buf, int len) |
| 46 | { |
| 47 | int i; |
| 48 | struct nand_chip *this = mtd->priv; |
| 49 | struct socrates_nand_host *host = this->priv; |
| 50 | |
| 51 | for (i = 0; i < len; i++) { |
| 52 | out_be32(host->io_base, FPGA_NAND_ENABLE | |
| 53 | FPGA_NAND_CMD_WRITE | |
| 54 | (buf[i] << FPGA_NAND_DATA_SHIFT)); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * socrates_nand_read_buf - read chip data into buffer |
| 60 | * @mtd: MTD device structure |
| 61 | * @buf: buffer to store date |
| 62 | * @len: number of bytes to read |
| 63 | */ |
| 64 | static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 65 | { |
| 66 | int i; |
| 67 | struct nand_chip *this = mtd->priv; |
| 68 | struct socrates_nand_host *host = this->priv; |
| 69 | uint32_t val; |
| 70 | |
| 71 | val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ; |
| 72 | |
| 73 | out_be32(host->io_base, val); |
| 74 | for (i = 0; i < len; i++) { |
| 75 | buf[i] = (in_be32(host->io_base) >> |
| 76 | FPGA_NAND_DATA_SHIFT) & 0xff; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * socrates_nand_read_byte - read one byte from the chip |
| 82 | * @mtd: MTD device structure |
| 83 | */ |
| 84 | static uint8_t socrates_nand_read_byte(struct mtd_info *mtd) |
| 85 | { |
| 86 | uint8_t byte; |
| 87 | socrates_nand_read_buf(mtd, &byte, sizeof(byte)); |
| 88 | return byte; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * socrates_nand_read_word - read one word from the chip |
| 93 | * @mtd: MTD device structure |
| 94 | */ |
| 95 | static uint16_t socrates_nand_read_word(struct mtd_info *mtd) |
| 96 | { |
| 97 | uint16_t word; |
| 98 | socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word)); |
| 99 | return word; |
| 100 | } |
| 101 | |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 102 | /* |
| 103 | * Hardware specific access to control-lines |
| 104 | */ |
| 105 | static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, |
| 106 | unsigned int ctrl) |
| 107 | { |
| 108 | struct nand_chip *nand_chip = mtd->priv; |
| 109 | struct socrates_nand_host *host = nand_chip->priv; |
| 110 | uint32_t val; |
| 111 | |
| 112 | if (cmd == NAND_CMD_NONE) |
| 113 | return; |
| 114 | |
| 115 | if (ctrl & NAND_CLE) |
| 116 | val = FPGA_NAND_CMD_COMMAND; |
| 117 | else |
| 118 | val = FPGA_NAND_CMD_ADDR; |
| 119 | |
| 120 | if (ctrl & NAND_NCE) |
| 121 | val |= FPGA_NAND_ENABLE; |
| 122 | |
| 123 | val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT; |
| 124 | |
| 125 | out_be32(host->io_base, val); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Read the Device Ready pin. |
| 130 | */ |
| 131 | static int socrates_nand_device_ready(struct mtd_info *mtd) |
| 132 | { |
| 133 | struct nand_chip *nand_chip = mtd->priv; |
| 134 | struct socrates_nand_host *host = nand_chip->priv; |
| 135 | |
| 136 | if (in_be32(host->io_base) & FPGA_NAND_BUSY) |
| 137 | return 0; /* busy */ |
| 138 | return 1; |
| 139 | } |
| 140 | |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 141 | /* |
| 142 | * Probe for the NAND device. |
| 143 | */ |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 144 | static int socrates_nand_probe(struct platform_device *ofdev) |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 145 | { |
| 146 | struct socrates_nand_host *host; |
| 147 | struct mtd_info *mtd; |
| 148 | struct nand_chip *nand_chip; |
| 149 | int res; |
Dmitry Eremin-Solenikov | 2cd9ea5 | 2011-05-30 01:02:26 +0400 | [diff] [blame] | 150 | struct mtd_part_parser_data ppdata; |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 151 | |
| 152 | /* Allocate memory for the device structure (and zero it) */ |
Sachin Kamat | cf3a9b5 | 2013-10-08 15:31:46 +0530 | [diff] [blame] | 153 | host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL); |
| 154 | if (!host) |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 155 | return -ENOMEM; |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 156 | |
Anatolij Gustschin | c8a4d0f | 2010-06-03 02:37:17 +0200 | [diff] [blame] | 157 | host->io_base = of_iomap(ofdev->dev.of_node, 0); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 158 | if (host->io_base == NULL) { |
Sachin Kamat | 5422933 | 2013-10-08 15:38:08 +0530 | [diff] [blame] | 159 | dev_err(&ofdev->dev, "ioremap failed\n"); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 160 | return -EIO; |
| 161 | } |
| 162 | |
| 163 | mtd = &host->mtd; |
| 164 | nand_chip = &host->nand_chip; |
| 165 | host->dev = &ofdev->dev; |
| 166 | |
| 167 | nand_chip->priv = host; /* link the private data structures */ |
| 168 | mtd->priv = nand_chip; |
| 169 | mtd->name = "socrates_nand"; |
| 170 | mtd->owner = THIS_MODULE; |
| 171 | mtd->dev.parent = &ofdev->dev; |
Dmitry Eremin-Solenikov | 2cd9ea5 | 2011-05-30 01:02:26 +0400 | [diff] [blame] | 172 | ppdata.of_node = ofdev->dev.of_node; |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 173 | |
| 174 | /*should never be accessed directly */ |
| 175 | nand_chip->IO_ADDR_R = (void *)0xdeadbeef; |
| 176 | nand_chip->IO_ADDR_W = (void *)0xdeadbeef; |
| 177 | |
| 178 | nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl; |
| 179 | nand_chip->read_byte = socrates_nand_read_byte; |
| 180 | nand_chip->read_word = socrates_nand_read_word; |
| 181 | nand_chip->write_buf = socrates_nand_write_buf; |
| 182 | nand_chip->read_buf = socrates_nand_read_buf; |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 183 | nand_chip->dev_ready = socrates_nand_device_ready; |
| 184 | |
| 185 | nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */ |
| 186 | |
| 187 | /* TODO: I have no idea what real delay is. */ |
| 188 | nand_chip->chip_delay = 20; /* 20us command delay time */ |
| 189 | |
| 190 | dev_set_drvdata(&ofdev->dev, host); |
| 191 | |
| 192 | /* first scan to find the device and get the page size */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 193 | if (nand_scan_ident(mtd, 1, NULL)) { |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 194 | res = -ENXIO; |
| 195 | goto out; |
| 196 | } |
| 197 | |
| 198 | /* second phase scan */ |
| 199 | if (nand_scan_tail(mtd)) { |
| 200 | res = -ENXIO; |
| 201 | goto out; |
| 202 | } |
| 203 | |
Dmitry Eremin-Solenikov | b2a5a48 | 2011-06-02 18:01:07 +0400 | [diff] [blame] | 204 | res = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 205 | if (!res) |
| 206 | return res; |
| 207 | |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 208 | nand_release(mtd); |
| 209 | |
| 210 | out: |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 211 | iounmap(host->io_base); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 212 | return res; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Remove a NAND device. |
| 217 | */ |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 218 | static int socrates_nand_remove(struct platform_device *ofdev) |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 219 | { |
| 220 | struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev); |
| 221 | struct mtd_info *mtd = &host->mtd; |
| 222 | |
| 223 | nand_release(mtd); |
| 224 | |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 225 | iounmap(host->io_base); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Márton Németh | b2d4fba | 2010-01-09 15:10:46 +0100 | [diff] [blame] | 230 | static const struct of_device_id socrates_nand_match[] = |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 231 | { |
| 232 | { |
| 233 | .compatible = "abb,socrates-nand", |
| 234 | }, |
| 235 | {}, |
| 236 | }; |
| 237 | |
| 238 | MODULE_DEVICE_TABLE(of, socrates_nand_match); |
| 239 | |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 240 | static struct platform_driver socrates_nand_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 241 | .driver = { |
| 242 | .name = "socrates_nand", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 243 | .of_match_table = socrates_nand_match, |
| 244 | }, |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 245 | .probe = socrates_nand_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 246 | .remove = socrates_nand_remove, |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 247 | }; |
| 248 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 249 | module_platform_driver(socrates_nand_driver); |
Wolfgang Grandegger | 1b57819 | 2009-03-25 11:48:38 +0100 | [diff] [blame] | 250 | |
| 251 | MODULE_LICENSE("GPL"); |
| 252 | MODULE_AUTHOR("Ilya Yanok"); |
| 253 | MODULE_DESCRIPTION("NAND driver for Socrates board"); |