blob: 888fd314c62a234b7a43e3c922a3d4dbb14e36c9 [file] [log] [blame]
Wolfgang Grandegger1b578192009-03-25 11:48:38 +01001/*
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 Herringc11eede2013-11-10 23:19:08 -060018#include <linux/of_address.h>
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010019#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
31struct socrates_nand_host {
32 struct nand_chip nand_chip;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010033 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 */
43static void socrates_nand_write_buf(struct mtd_info *mtd,
44 const uint8_t *buf, int len)
45{
46 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010047 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010048 struct socrates_nand_host *host = nand_get_controller_data(this);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010049
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 */
63static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
64{
65 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010066 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010067 struct socrates_nand_host *host = nand_get_controller_data(this);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010068 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 */
83static 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 */
94static 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
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100101/*
102 * Hardware specific access to control-lines
103 */
104static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
105 unsigned int ctrl)
106{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100107 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100108 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100109 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 */
130static int socrates_nand_device_ready(struct mtd_info *mtd)
131{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100132 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100133 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100134
135 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
136 return 0; /* busy */
137 return 1;
138}
139
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100140/*
141 * Probe for the NAND device.
142 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500143static int socrates_nand_probe(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100144{
145 struct socrates_nand_host *host;
146 struct mtd_info *mtd;
147 struct nand_chip *nand_chip;
148 int res;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100149
150 /* Allocate memory for the device structure (and zero it) */
Sachin Kamatcf3a9b52013-10-08 15:31:46 +0530151 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
152 if (!host)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100153 return -ENOMEM;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100154
Anatolij Gustschinc8a4d0f2010-06-03 02:37:17 +0200155 host->io_base = of_iomap(ofdev->dev.of_node, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100156 if (host->io_base == NULL) {
Sachin Kamat54229332013-10-08 15:38:08 +0530157 dev_err(&ofdev->dev, "ioremap failed\n");
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100158 return -EIO;
159 }
160
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100161 nand_chip = &host->nand_chip;
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100162 mtd = nand_to_mtd(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100163 host->dev = &ofdev->dev;
164
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100165 /* link the private data structures */
166 nand_set_controller_data(nand_chip, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700167 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100168 mtd->name = "socrates_nand";
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100169 mtd->dev.parent = &ofdev->dev;
170
171 /*should never be accessed directly */
172 nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
173 nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
174
175 nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
176 nand_chip->read_byte = socrates_nand_read_byte;
177 nand_chip->read_word = socrates_nand_read_word;
178 nand_chip->write_buf = socrates_nand_write_buf;
179 nand_chip->read_buf = socrates_nand_read_buf;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100180 nand_chip->dev_ready = socrates_nand_device_ready;
181
182 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Rafał Miłeckice111af2016-04-08 12:23:51 +0200183 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100184
185 /* TODO: I have no idea what real delay is. */
186 nand_chip->chip_delay = 20; /* 20us command delay time */
187
188 dev_set_drvdata(&ofdev->dev, host);
189
190 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +0000191 if (nand_scan_ident(mtd, 1, NULL)) {
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100192 res = -ENXIO;
193 goto out;
194 }
195
196 /* second phase scan */
197 if (nand_scan_tail(mtd)) {
198 res = -ENXIO;
199 goto out;
200 }
201
Brian Norrisa61ae812015-10-30 20:33:25 -0700202 res = mtd_device_register(mtd, NULL, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100203 if (!res)
204 return res;
205
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100206 nand_release(mtd);
207
208out:
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100209 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100210 return res;
211}
212
213/*
214 * Remove a NAND device.
215 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500216static int socrates_nand_remove(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100217{
218 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100219 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100220
221 nand_release(mtd);
222
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100223 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100224
225 return 0;
226}
227
Márton Némethb2d4fba2010-01-09 15:10:46 +0100228static const struct of_device_id socrates_nand_match[] =
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100229{
230 {
231 .compatible = "abb,socrates-nand",
232 },
233 {},
234};
235
236MODULE_DEVICE_TABLE(of, socrates_nand_match);
237
Grant Likely1c48a5c2011-02-17 02:43:24 -0700238static struct platform_driver socrates_nand_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700239 .driver = {
240 .name = "socrates_nand",
Grant Likely40182942010-04-13 16:13:02 -0700241 .of_match_table = socrates_nand_match,
242 },
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100243 .probe = socrates_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500244 .remove = socrates_nand_remove,
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100245};
246
Axel Linf99640d2011-11-27 20:45:03 +0800247module_platform_driver(socrates_nand_driver);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100248
249MODULE_LICENSE("GPL");
250MODULE_AUTHOR("Ilya Yanok");
251MODULE_DESCRIPTION("NAND driver for Socrates board");