blob: 007e37680b88041a00285f35427e1062b65dfd07 [file] [log] [blame]
Wolfgang Grandegger1b578192009-03-25 11:48:38 +01001/*
Wolfgang Grandegger1b578192009-03-25 11:48:38 +01002 * 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 Brezillond4092d72017-08-04 17:29:10 +020014#include <linux/mtd/rawnand.h>
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010015#include <linux/mtd/partitions.h>
Rob Herringc11eede2013-11-10 23:19:08 -060016#include <linux/of_address.h>
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010017#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
29struct socrates_nand_host {
30 struct nand_chip nand_chip;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010031 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 */
41static void socrates_nand_write_buf(struct mtd_info *mtd,
42 const uint8_t *buf, int len)
43{
44 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010045 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010046 struct socrates_nand_host *host = nand_get_controller_data(this);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010047
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
Boris Brezillon7e534322018-09-06 14:05:22 +020057 * @this: NAND chip object
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010058 * @buf: buffer to store date
59 * @len: number of bytes to read
60 */
Boris Brezillon7e534322018-09-06 14:05:22 +020061static void socrates_nand_read_buf(struct nand_chip *this, uint8_t *buf,
62 int len)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010063{
64 int i;
Boris BREZILLONd699ed22015-12-10 09:00:41 +010065 struct socrates_nand_host *host = nand_get_controller_data(this);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010066 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 */
Boris Brezillon7e534322018-09-06 14:05:22 +020081static uint8_t socrates_nand_read_byte(struct nand_chip *this)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010082{
83 uint8_t byte;
Boris Brezillon7e534322018-09-06 14:05:22 +020084 socrates_nand_read_buf(this, &byte, sizeof(byte));
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010085 return byte;
86}
87
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010088/*
89 * Hardware specific access to control-lines
90 */
91static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
92 unsigned int ctrl)
93{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010094 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010095 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010096 uint32_t val;
97
98 if (cmd == NAND_CMD_NONE)
99 return;
100
101 if (ctrl & NAND_CLE)
102 val = FPGA_NAND_CMD_COMMAND;
103 else
104 val = FPGA_NAND_CMD_ADDR;
105
106 if (ctrl & NAND_NCE)
107 val |= FPGA_NAND_ENABLE;
108
109 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
110
111 out_be32(host->io_base, val);
112}
113
114/*
115 * Read the Device Ready pin.
116 */
117static int socrates_nand_device_ready(struct mtd_info *mtd)
118{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100119 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100120 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100121
122 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
123 return 0; /* busy */
124 return 1;
125}
126
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100127/*
128 * Probe for the NAND device.
129 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500130static int socrates_nand_probe(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100131{
132 struct socrates_nand_host *host;
133 struct mtd_info *mtd;
134 struct nand_chip *nand_chip;
135 int res;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100136
137 /* Allocate memory for the device structure (and zero it) */
Sachin Kamatcf3a9b52013-10-08 15:31:46 +0530138 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
139 if (!host)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100140 return -ENOMEM;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100141
Anatolij Gustschinc8a4d0f2010-06-03 02:37:17 +0200142 host->io_base = of_iomap(ofdev->dev.of_node, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100143 if (host->io_base == NULL) {
Sachin Kamat54229332013-10-08 15:38:08 +0530144 dev_err(&ofdev->dev, "ioremap failed\n");
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100145 return -EIO;
146 }
147
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100148 nand_chip = &host->nand_chip;
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100149 mtd = nand_to_mtd(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100150 host->dev = &ofdev->dev;
151
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100152 /* link the private data structures */
153 nand_set_controller_data(nand_chip, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700154 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100155 mtd->name = "socrates_nand";
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100156 mtd->dev.parent = &ofdev->dev;
157
158 /*should never be accessed directly */
159 nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
160 nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
161
162 nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
163 nand_chip->read_byte = socrates_nand_read_byte;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100164 nand_chip->write_buf = socrates_nand_write_buf;
165 nand_chip->read_buf = socrates_nand_read_buf;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100166 nand_chip->dev_ready = socrates_nand_device_ready;
167
168 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Rafał Miłeckice111af2016-04-08 12:23:51 +0200169 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100170
171 /* TODO: I have no idea what real delay is. */
172 nand_chip->chip_delay = 20; /* 20us command delay time */
173
174 dev_set_drvdata(&ofdev->dev, host);
175
Boris Brezillon00ad3782018-09-06 14:05:14 +0200176 res = nand_scan(nand_chip, 1);
Masahiro Yamada83f48f82016-11-04 19:43:10 +0900177 if (res)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100178 goto out;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100179
Brian Norrisa61ae812015-10-30 20:33:25 -0700180 res = mtd_device_register(mtd, NULL, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100181 if (!res)
182 return res;
183
Boris Brezillon59ac2762018-09-06 14:05:15 +0200184 nand_release(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100185
186out:
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100187 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100188 return res;
189}
190
191/*
192 * Remove a NAND device.
193 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500194static int socrates_nand_remove(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100195{
196 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100197
Boris Brezillon59ac2762018-09-06 14:05:15 +0200198 nand_release(&host->nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100199
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100200 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100201
202 return 0;
203}
204
Márton Némethb2d4fba2010-01-09 15:10:46 +0100205static const struct of_device_id socrates_nand_match[] =
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100206{
207 {
208 .compatible = "abb,socrates-nand",
209 },
210 {},
211};
212
213MODULE_DEVICE_TABLE(of, socrates_nand_match);
214
Grant Likely1c48a5c2011-02-17 02:43:24 -0700215static struct platform_driver socrates_nand_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700216 .driver = {
217 .name = "socrates_nand",
Grant Likely40182942010-04-13 16:13:02 -0700218 .of_match_table = socrates_nand_match,
219 },
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100220 .probe = socrates_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500221 .remove = socrates_nand_remove,
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100222};
223
Axel Linf99640d2011-11-27 20:45:03 +0800224module_platform_driver(socrates_nand_driver);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100225
226MODULE_LICENSE("GPL");
227MODULE_AUTHOR("Ilya Yanok");
228MODULE_DESCRIPTION("NAND driver for Socrates board");