timerfd: Allow timers to be cancelled when clock was set
[linux-2.6/linux-mips.git] / drivers / mtd / nand / socrates_nand.c
bloba853548986f0ebdb69d20756b20a04f400f0d8f9
1 /*
2 * drivers/mtd/nand/socrates_nand.c
4 * Copyright © 2008 Ilya Yanok, Emcraft Systems
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.
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>
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
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;
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
43 static void socrates_nand_write_buf(struct mtd_info *mtd,
44 const uint8_t *buf, int len)
46 int i;
47 struct nand_chip *this = mtd->priv;
48 struct socrates_nand_host *host = this->priv;
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));
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
63 static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
65 int i;
66 struct nand_chip *this = mtd->priv;
67 struct socrates_nand_host *host = this->priv;
68 uint32_t val;
70 val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ;
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;
79 /**
80 * socrates_nand_read_byte - read one byte from the chip
81 * @mtd: MTD device structure
83 static uint8_t socrates_nand_read_byte(struct mtd_info *mtd)
85 uint8_t byte;
86 socrates_nand_read_buf(mtd, &byte, sizeof(byte));
87 return byte;
90 /**
91 * socrates_nand_read_word - read one word from the chip
92 * @mtd: MTD device structure
94 static uint16_t socrates_nand_read_word(struct mtd_info *mtd)
96 uint16_t word;
97 socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word));
98 return word;
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
107 static int socrates_nand_verify_buf(struct mtd_info *mtd, const u8 *buf,
108 int len)
110 int i;
112 for (i = 0; i < len; i++) {
113 if (buf[i] != socrates_nand_read_byte(mtd))
114 return -EFAULT;
116 return 0;
120 * Hardware specific access to control-lines
122 static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
123 unsigned int ctrl)
125 struct nand_chip *nand_chip = mtd->priv;
126 struct socrates_nand_host *host = nand_chip->priv;
127 uint32_t val;
129 if (cmd == NAND_CMD_NONE)
130 return;
132 if (ctrl & NAND_CLE)
133 val = FPGA_NAND_CMD_COMMAND;
134 else
135 val = FPGA_NAND_CMD_ADDR;
137 if (ctrl & NAND_NCE)
138 val |= FPGA_NAND_ENABLE;
140 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
142 out_be32(host->io_base, val);
146 * Read the Device Ready pin.
148 static int socrates_nand_device_ready(struct mtd_info *mtd)
150 struct nand_chip *nand_chip = mtd->priv;
151 struct socrates_nand_host *host = nand_chip->priv;
153 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
154 return 0; /* busy */
155 return 1;
158 #ifdef CONFIG_MTD_PARTITIONS
159 static const char *part_probes[] = { "cmdlinepart", NULL };
160 #endif
163 * Probe for the NAND device.
165 static int __devinit socrates_nand_probe(struct platform_device *ofdev)
167 struct socrates_nand_host *host;
168 struct mtd_info *mtd;
169 struct nand_chip *nand_chip;
170 int res;
172 #ifdef CONFIG_MTD_PARTITIONS
173 struct mtd_partition *partitions = NULL;
174 int num_partitions = 0;
175 #endif
177 /* Allocate memory for the device structure (and zero it) */
178 host = kzalloc(sizeof(struct socrates_nand_host), GFP_KERNEL);
179 if (!host) {
180 printk(KERN_ERR
181 "socrates_nand: failed to allocate device structure.\n");
182 return -ENOMEM;
185 host->io_base = of_iomap(ofdev->dev.of_node, 0);
186 if (host->io_base == NULL) {
187 printk(KERN_ERR "socrates_nand: ioremap failed\n");
188 kfree(host);
189 return -EIO;
192 mtd = &host->mtd;
193 nand_chip = &host->nand_chip;
194 host->dev = &ofdev->dev;
196 nand_chip->priv = host; /* link the private data structures */
197 mtd->priv = nand_chip;
198 mtd->name = "socrates_nand";
199 mtd->owner = THIS_MODULE;
200 mtd->dev.parent = &ofdev->dev;
202 /*should never be accessed directly */
203 nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
204 nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
206 nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
207 nand_chip->read_byte = socrates_nand_read_byte;
208 nand_chip->read_word = socrates_nand_read_word;
209 nand_chip->write_buf = socrates_nand_write_buf;
210 nand_chip->read_buf = socrates_nand_read_buf;
211 nand_chip->verify_buf = socrates_nand_verify_buf;
212 nand_chip->dev_ready = socrates_nand_device_ready;
214 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
216 /* TODO: I have no idea what real delay is. */
217 nand_chip->chip_delay = 20; /* 20us command delay time */
219 dev_set_drvdata(&ofdev->dev, host);
221 /* first scan to find the device and get the page size */
222 if (nand_scan_ident(mtd, 1, NULL)) {
223 res = -ENXIO;
224 goto out;
227 /* second phase scan */
228 if (nand_scan_tail(mtd)) {
229 res = -ENXIO;
230 goto out;
233 #ifdef CONFIG_MTD_PARTITIONS
234 #ifdef CONFIG_MTD_CMDLINE_PARTS
235 num_partitions = parse_mtd_partitions(mtd, part_probes,
236 &partitions, 0);
237 if (num_partitions < 0) {
238 res = num_partitions;
239 goto release;
241 #endif
243 #ifdef CONFIG_MTD_OF_PARTS
244 if (num_partitions == 0) {
245 num_partitions = of_mtd_parse_partitions(&ofdev->dev,
246 ofdev->dev.of_node,
247 &partitions);
248 if (num_partitions < 0) {
249 res = num_partitions;
250 goto release;
253 #endif
254 if (partitions && (num_partitions > 0))
255 res = add_mtd_partitions(mtd, partitions, num_partitions);
256 else
257 #endif
258 res = add_mtd_device(mtd);
260 if (!res)
261 return res;
263 #ifdef CONFIG_MTD_PARTITIONS
264 release:
265 #endif
266 nand_release(mtd);
268 out:
269 dev_set_drvdata(&ofdev->dev, NULL);
270 iounmap(host->io_base);
271 kfree(host);
272 return res;
276 * Remove a NAND device.
278 static int __devexit socrates_nand_remove(struct platform_device *ofdev)
280 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
281 struct mtd_info *mtd = &host->mtd;
283 nand_release(mtd);
285 dev_set_drvdata(&ofdev->dev, NULL);
286 iounmap(host->io_base);
287 kfree(host);
289 return 0;
292 static const struct of_device_id socrates_nand_match[] =
295 .compatible = "abb,socrates-nand",
300 MODULE_DEVICE_TABLE(of, socrates_nand_match);
302 static struct platform_driver socrates_nand_driver = {
303 .driver = {
304 .name = "socrates_nand",
305 .owner = THIS_MODULE,
306 .of_match_table = socrates_nand_match,
308 .probe = socrates_nand_probe,
309 .remove = __devexit_p(socrates_nand_remove),
312 static int __init socrates_nand_init(void)
314 return platform_driver_register(&socrates_nand_driver);
317 static void __exit socrates_nand_exit(void)
319 platform_driver_unregister(&socrates_nand_driver);
322 module_init(socrates_nand_init);
323 module_exit(socrates_nand_exit);
325 MODULE_LICENSE("GPL");
326 MODULE_AUTHOR("Ilya Yanok");
327 MODULE_DESCRIPTION("NAND driver for Socrates board");