Commit | Line | Data |
---|---|---|
1b578193 WG |
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 | ||
1b578193 WG |
101 | /* |
102 | * Hardware specific access to control-lines | |
103 | */ | |
104 | static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, | |
105 | unsigned int ctrl) | |
106 | { | |
107 | struct nand_chip *nand_chip = mtd->priv; | |
108 | struct socrates_nand_host *host = nand_chip->priv; | |
109 | uint32_t val; | |
110 | ||
111 | if (cmd == NAND_CMD_NONE) | |
112 | return; | |
113 | ||
114 | if (ctrl & NAND_CLE) | |
115 | val = FPGA_NAND_CMD_COMMAND; | |
116 | else | |
117 | val = FPGA_NAND_CMD_ADDR; | |
118 | ||
119 | if (ctrl & NAND_NCE) | |
120 | val |= FPGA_NAND_ENABLE; | |
121 | ||
122 | val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT; | |
123 | ||
124 | out_be32(host->io_base, val); | |
125 | } | |
126 | ||
127 | /* | |
128 | * Read the Device Ready pin. | |
129 | */ | |
130 | static int socrates_nand_device_ready(struct mtd_info *mtd) | |
131 | { | |
132 | struct nand_chip *nand_chip = mtd->priv; | |
133 | struct socrates_nand_host *host = nand_chip->priv; | |
134 | ||
135 | if (in_be32(host->io_base) & FPGA_NAND_BUSY) | |
136 | return 0; /* busy */ | |
137 | return 1; | |
138 | } | |
139 | ||
1b578193 WG |
140 | /* |
141 | * Probe for the NAND device. | |
142 | */ | |
06f25510 | 143 | static int socrates_nand_probe(struct platform_device *ofdev) |
1b578193 WG |
144 | { |
145 | struct socrates_nand_host *host; | |
146 | struct mtd_info *mtd; | |
147 | struct nand_chip *nand_chip; | |
148 | int res; | |
2cd9ea52 | 149 | struct mtd_part_parser_data ppdata; |
1b578193 WG |
150 | |
151 | /* Allocate memory for the device structure (and zero it) */ | |
152 | host = kzalloc(sizeof(struct socrates_nand_host), GFP_KERNEL); | |
153 | if (!host) { | |
154 | printk(KERN_ERR | |
155 | "socrates_nand: failed to allocate device structure.\n"); | |
156 | return -ENOMEM; | |
157 | } | |
158 | ||
c8a4d0fd | 159 | host->io_base = of_iomap(ofdev->dev.of_node, 0); |
1b578193 WG |
160 | if (host->io_base == NULL) { |
161 | printk(KERN_ERR "socrates_nand: ioremap failed\n"); | |
162 | kfree(host); | |
163 | return -EIO; | |
164 | } | |
165 | ||
166 | mtd = &host->mtd; | |
167 | nand_chip = &host->nand_chip; | |
168 | host->dev = &ofdev->dev; | |
169 | ||
170 | nand_chip->priv = host; /* link the private data structures */ | |
171 | mtd->priv = nand_chip; | |
172 | mtd->name = "socrates_nand"; | |
173 | mtd->owner = THIS_MODULE; | |
174 | mtd->dev.parent = &ofdev->dev; | |
2cd9ea52 | 175 | ppdata.of_node = ofdev->dev.of_node; |
1b578193 WG |
176 | |
177 | /*should never be accessed directly */ | |
178 | nand_chip->IO_ADDR_R = (void *)0xdeadbeef; | |
179 | nand_chip->IO_ADDR_W = (void *)0xdeadbeef; | |
180 | ||
181 | nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl; | |
182 | nand_chip->read_byte = socrates_nand_read_byte; | |
183 | nand_chip->read_word = socrates_nand_read_word; | |
184 | nand_chip->write_buf = socrates_nand_write_buf; | |
185 | nand_chip->read_buf = socrates_nand_read_buf; | |
1b578193 WG |
186 | nand_chip->dev_ready = socrates_nand_device_ready; |
187 | ||
188 | nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */ | |
189 | ||
190 | /* TODO: I have no idea what real delay is. */ | |
191 | nand_chip->chip_delay = 20; /* 20us command delay time */ | |
192 | ||
193 | dev_set_drvdata(&ofdev->dev, host); | |
194 | ||
195 | /* first scan to find the device and get the page size */ | |
5e81e88a | 196 | if (nand_scan_ident(mtd, 1, NULL)) { |
1b578193 WG |
197 | res = -ENXIO; |
198 | goto out; | |
199 | } | |
200 | ||
201 | /* second phase scan */ | |
202 | if (nand_scan_tail(mtd)) { | |
203 | res = -ENXIO; | |
204 | goto out; | |
205 | } | |
206 | ||
b2a5a487 | 207 | res = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0); |
1b578193 WG |
208 | if (!res) |
209 | return res; | |
210 | ||
1b578193 WG |
211 | nand_release(mtd); |
212 | ||
213 | out: | |
214 | dev_set_drvdata(&ofdev->dev, NULL); | |
215 | iounmap(host->io_base); | |
216 | kfree(host); | |
217 | return res; | |
218 | } | |
219 | ||
220 | /* | |
221 | * Remove a NAND device. | |
222 | */ | |
810b7e06 | 223 | static int socrates_nand_remove(struct platform_device *ofdev) |
1b578193 WG |
224 | { |
225 | struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev); | |
226 | struct mtd_info *mtd = &host->mtd; | |
227 | ||
228 | nand_release(mtd); | |
229 | ||
230 | dev_set_drvdata(&ofdev->dev, NULL); | |
231 | iounmap(host->io_base); | |
232 | kfree(host); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
b2d4fbab | 237 | static const struct of_device_id socrates_nand_match[] = |
1b578193 WG |
238 | { |
239 | { | |
240 | .compatible = "abb,socrates-nand", | |
241 | }, | |
242 | {}, | |
243 | }; | |
244 | ||
245 | MODULE_DEVICE_TABLE(of, socrates_nand_match); | |
246 | ||
1c48a5c9 | 247 | static struct platform_driver socrates_nand_driver = { |
4018294b GL |
248 | .driver = { |
249 | .name = "socrates_nand", | |
250 | .owner = THIS_MODULE, | |
251 | .of_match_table = socrates_nand_match, | |
252 | }, | |
1b578193 | 253 | .probe = socrates_nand_probe, |
5153b88c | 254 | .remove = socrates_nand_remove, |
1b578193 WG |
255 | }; |
256 | ||
f99640de | 257 | module_platform_driver(socrates_nand_driver); |
1b578193 WG |
258 | |
259 | MODULE_LICENSE("GPL"); | |
260 | MODULE_AUTHOR("Ilya Yanok"); | |
261 | MODULE_DESCRIPTION("NAND driver for Socrates board"); |