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