Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / drivers / mtd / nand / raw / socrates_nand.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1b578193 2/*
1b578193 3 * Copyright © 2008 Ilya Yanok, Emcraft Systems
1b578193
WG
4 */
5
6#include <linux/slab.h>
7#include <linux/module.h>
8#include <linux/mtd/mtd.h>
d4092d76 9#include <linux/mtd/rawnand.h>
1b578193 10#include <linux/mtd/partitions.h>
c2fc6b69 11#include <linux/of.h>
c11eede6 12#include <linux/of_address.h>
c2fc6b69 13#include <linux/platform_device.h>
1b578193
WG
14#include <linux/io.h>
15
16#define FPGA_NAND_CMD_MASK (0x7 << 28)
17#define FPGA_NAND_CMD_COMMAND (0x0 << 28)
18#define FPGA_NAND_CMD_ADDR (0x1 << 28)
19#define FPGA_NAND_CMD_READ (0x2 << 28)
20#define FPGA_NAND_CMD_WRITE (0x3 << 28)
21#define FPGA_NAND_BUSY (0x1 << 15)
22#define FPGA_NAND_ENABLE (0x1 << 31)
23#define FPGA_NAND_DATA_SHIFT 16
24
25struct socrates_nand_host {
b36bf0a0 26 struct nand_controller controller;
1b578193 27 struct nand_chip nand_chip;
1b578193
WG
28 void __iomem *io_base;
29 struct device *dev;
30};
31
32/**
33 * socrates_nand_write_buf - write buffer to chip
c0739d85 34 * @this: NAND chip object
1b578193
WG
35 * @buf: data buffer
36 * @len: number of bytes to write
37 */
c0739d85
BB
38static void socrates_nand_write_buf(struct nand_chip *this, const uint8_t *buf,
39 int len)
1b578193
WG
40{
41 int i;
d699ed25 42 struct socrates_nand_host *host = nand_get_controller_data(this);
1b578193
WG
43
44 for (i = 0; i < len; i++) {
45 out_be32(host->io_base, FPGA_NAND_ENABLE |
46 FPGA_NAND_CMD_WRITE |
47 (buf[i] << FPGA_NAND_DATA_SHIFT));
48 }
49}
50
51/**
52 * socrates_nand_read_buf - read chip data into buffer
7e534323 53 * @this: NAND chip object
1b578193
WG
54 * @buf: buffer to store date
55 * @len: number of bytes to read
56 */
7e534323
BB
57static void socrates_nand_read_buf(struct nand_chip *this, uint8_t *buf,
58 int len)
1b578193
WG
59{
60 int i;
d699ed25 61 struct socrates_nand_host *host = nand_get_controller_data(this);
1b578193
WG
62 uint32_t val;
63
64 val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ;
65
66 out_be32(host->io_base, val);
67 for (i = 0; i < len; i++) {
68 buf[i] = (in_be32(host->io_base) >>
69 FPGA_NAND_DATA_SHIFT) & 0xff;
70 }
71}
72
73/**
74 * socrates_nand_read_byte - read one byte from the chip
75 * @mtd: MTD device structure
76 */
7e534323 77static uint8_t socrates_nand_read_byte(struct nand_chip *this)
1b578193
WG
78{
79 uint8_t byte;
7e534323 80 socrates_nand_read_buf(this, &byte, sizeof(byte));
1b578193
WG
81 return byte;
82}
83
1b578193
WG
84/*
85 * Hardware specific access to control-lines
86 */
0f808c16
BB
87static void socrates_nand_cmd_ctrl(struct nand_chip *nand_chip, int cmd,
88 unsigned int ctrl)
1b578193 89{
d699ed25 90 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
1b578193
WG
91 uint32_t val;
92
93 if (cmd == NAND_CMD_NONE)
94 return;
95
96 if (ctrl & NAND_CLE)
97 val = FPGA_NAND_CMD_COMMAND;
98 else
99 val = FPGA_NAND_CMD_ADDR;
100
101 if (ctrl & NAND_NCE)
102 val |= FPGA_NAND_ENABLE;
103
104 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
105
106 out_be32(host->io_base, val);
107}
108
109/*
110 * Read the Device Ready pin.
111 */
50a487e7 112static int socrates_nand_device_ready(struct nand_chip *nand_chip)
1b578193 113{
d699ed25 114 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
1b578193
WG
115
116 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
117 return 0; /* busy */
118 return 1;
119}
120
b36bf0a0
MR
121static int socrates_attach_chip(struct nand_chip *chip)
122{
b4ebddd6
MR
123 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
124 chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
cf75f00f 125 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
b36bf0a0
MR
126
127 return 0;
128}
129
130static const struct nand_controller_ops socrates_ops = {
131 .attach_chip = socrates_attach_chip,
132};
133
1b578193
WG
134/*
135 * Probe for the NAND device.
136 */
06f25510 137static int socrates_nand_probe(struct platform_device *ofdev)
1b578193
WG
138{
139 struct socrates_nand_host *host;
140 struct mtd_info *mtd;
141 struct nand_chip *nand_chip;
142 int res;
1b578193
WG
143
144 /* Allocate memory for the device structure (and zero it) */
cf3a9b56
SK
145 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
146 if (!host)
1b578193 147 return -ENOMEM;
1b578193 148
c8a4d0fd 149 host->io_base = of_iomap(ofdev->dev.of_node, 0);
1b578193 150 if (host->io_base == NULL) {
5422933d 151 dev_err(&ofdev->dev, "ioremap failed\n");
1b578193
WG
152 return -EIO;
153 }
154
1b578193 155 nand_chip = &host->nand_chip;
a723bf6a 156 mtd = nand_to_mtd(nand_chip);
1b578193
WG
157 host->dev = &ofdev->dev;
158
b36bf0a0
MR
159 nand_controller_init(&host->controller);
160 host->controller.ops = &socrates_ops;
161 nand_chip->controller = &host->controller;
162
d699ed25
BB
163 /* link the private data structures */
164 nand_set_controller_data(nand_chip, host);
a61ae81a 165 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
1b578193 166 mtd->name = "socrates_nand";
1b578193
WG
167 mtd->dev.parent = &ofdev->dev;
168
bf6065c6 169 nand_chip->legacy.cmd_ctrl = socrates_nand_cmd_ctrl;
716bbbab
BB
170 nand_chip->legacy.read_byte = socrates_nand_read_byte;
171 nand_chip->legacy.write_buf = socrates_nand_write_buf;
172 nand_chip->legacy.read_buf = socrates_nand_read_buf;
8395b753 173 nand_chip->legacy.dev_ready = socrates_nand_device_ready;
1b578193 174
1b578193 175 /* TODO: I have no idea what real delay is. */
3cece3ab 176 nand_chip->legacy.chip_delay = 20; /* 20us command delay time */
1b578193 177
b4ebddd6
MR
178 /*
179 * This driver assumes that the default ECC engine should be TYPE_SOFT.
180 * Set ->engine_type before registering the NAND devices in order to
181 * provide a driver specific default value.
182 */
183 nand_chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
184
1b578193
WG
185 dev_set_drvdata(&ofdev->dev, host);
186
00ad378f 187 res = nand_scan(nand_chip, 1);
83f48f80 188 if (res)
1b578193 189 goto out;
1b578193 190
a61ae81a 191 res = mtd_device_register(mtd, NULL, 0);
1b578193
WG
192 if (!res)
193 return res;
194
9c6c2e5c 195 nand_cleanup(nand_chip);
1b578193
WG
196
197out:
1b578193 198 iounmap(host->io_base);
1b578193
WG
199 return res;
200}
201
202/*
203 * Remove a NAND device.
204 */
ec185b18 205static void socrates_nand_remove(struct platform_device *ofdev)
1b578193
WG
206{
207 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
c121cb98
MR
208 struct nand_chip *chip = &host->nand_chip;
209 int ret;
1b578193 210
c121cb98
MR
211 ret = mtd_device_unregister(nand_to_mtd(chip));
212 WARN_ON(ret);
213 nand_cleanup(chip);
1b578193 214
1b578193 215 iounmap(host->io_base);
1b578193
WG
216}
217
b2d4fbab 218static const struct of_device_id socrates_nand_match[] =
1b578193
WG
219{
220 {
221 .compatible = "abb,socrates-nand",
222 },
223 {},
224};
225
226MODULE_DEVICE_TABLE(of, socrates_nand_match);
227
1c48a5c9 228static struct platform_driver socrates_nand_driver = {
4018294b
GL
229 .driver = {
230 .name = "socrates_nand",
4018294b
GL
231 .of_match_table = socrates_nand_match,
232 },
1b578193 233 .probe = socrates_nand_probe,
f8470006 234 .remove = socrates_nand_remove,
1b578193
WG
235};
236
f99640de 237module_platform_driver(socrates_nand_driver);
1b578193
WG
238
239MODULE_LICENSE("GPL");
240MODULE_AUTHOR("Ilya Yanok");
241MODULE_DESCRIPTION("NAND driver for Socrates board");