blob: 9824a9923583c453906ae499e18992b8fd096904 [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
57 * @mtd: MTD device structure
58 * @buf: buffer to store date
59 * @len: number of bytes to read
60 */
61static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
62{
63 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010064 struct nand_chip *this = mtd_to_nand(mtd);
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 */
81static uint8_t socrates_nand_read_byte(struct mtd_info *mtd)
82{
83 uint8_t byte;
84 socrates_nand_read_buf(mtd, &byte, sizeof(byte));
85 return byte;
86}
87
88/**
89 * socrates_nand_read_word - read one word from the chip
90 * @mtd: MTD device structure
91 */
92static uint16_t socrates_nand_read_word(struct mtd_info *mtd)
93{
94 uint16_t word;
95 socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word));
96 return word;
97}
98
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010099/*
100 * Hardware specific access to control-lines
101 */
102static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
103 unsigned int ctrl)
104{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100105 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100106 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100107 uint32_t val;
108
109 if (cmd == NAND_CMD_NONE)
110 return;
111
112 if (ctrl & NAND_CLE)
113 val = FPGA_NAND_CMD_COMMAND;
114 else
115 val = FPGA_NAND_CMD_ADDR;
116
117 if (ctrl & NAND_NCE)
118 val |= FPGA_NAND_ENABLE;
119
120 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
121
122 out_be32(host->io_base, val);
123}
124
125/*
126 * Read the Device Ready pin.
127 */
128static int socrates_nand_device_ready(struct mtd_info *mtd)
129{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100130 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100131 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100132
133 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
134 return 0; /* busy */
135 return 1;
136}
137
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100138/*
139 * Probe for the NAND device.
140 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500141static int socrates_nand_probe(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100142{
143 struct socrates_nand_host *host;
144 struct mtd_info *mtd;
145 struct nand_chip *nand_chip;
146 int res;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100147
148 /* Allocate memory for the device structure (and zero it) */
Sachin Kamatcf3a9b52013-10-08 15:31:46 +0530149 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
150 if (!host)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100151 return -ENOMEM;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100152
Anatolij Gustschinc8a4d0f2010-06-03 02:37:17 +0200153 host->io_base = of_iomap(ofdev->dev.of_node, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100154 if (host->io_base == NULL) {
Sachin Kamat54229332013-10-08 15:38:08 +0530155 dev_err(&ofdev->dev, "ioremap failed\n");
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100156 return -EIO;
157 }
158
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100159 nand_chip = &host->nand_chip;
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100160 mtd = nand_to_mtd(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100161 host->dev = &ofdev->dev;
162
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100163 /* link the private data structures */
164 nand_set_controller_data(nand_chip, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700165 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100166 mtd->name = "socrates_nand";
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100167 mtd->dev.parent = &ofdev->dev;
168
169 /*should never be accessed directly */
170 nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
171 nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
172
173 nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
174 nand_chip->read_byte = socrates_nand_read_byte;
175 nand_chip->read_word = socrates_nand_read_word;
176 nand_chip->write_buf = socrates_nand_write_buf;
177 nand_chip->read_buf = socrates_nand_read_buf;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100178 nand_chip->dev_ready = socrates_nand_device_ready;
179
180 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Rafał Miłeckice111af2016-04-08 12:23:51 +0200181 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100182
183 /* TODO: I have no idea what real delay is. */
184 nand_chip->chip_delay = 20; /* 20us command delay time */
185
186 dev_set_drvdata(&ofdev->dev, host);
187
Masahiro Yamada83f48f82016-11-04 19:43:10 +0900188 res = nand_scan(mtd, 1);
189 if (res)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100190 goto out;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100191
Brian Norrisa61ae812015-10-30 20:33:25 -0700192 res = mtd_device_register(mtd, NULL, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100193 if (!res)
194 return res;
195
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100196 nand_release(mtd);
197
198out:
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100199 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100200 return res;
201}
202
203/*
204 * Remove a NAND device.
205 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500206static int socrates_nand_remove(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100207{
208 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100209 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100210
211 nand_release(mtd);
212
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100213 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100214
215 return 0;
216}
217
Márton Némethb2d4fba2010-01-09 15:10:46 +0100218static const struct of_device_id socrates_nand_match[] =
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100219{
220 {
221 .compatible = "abb,socrates-nand",
222 },
223 {},
224};
225
226MODULE_DEVICE_TABLE(of, socrates_nand_match);
227
Grant Likely1c48a5c2011-02-17 02:43:24 -0700228static struct platform_driver socrates_nand_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700229 .driver = {
230 .name = "socrates_nand",
Grant Likely40182942010-04-13 16:13:02 -0700231 .of_match_table = socrates_nand_match,
232 },
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100233 .probe = socrates_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500234 .remove = socrates_nand_remove,
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100235};
236
Axel Linf99640d2011-11-27 20:45:03 +0800237module_platform_driver(socrates_nand_driver);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100238
239MODULE_LICENSE("GPL");
240MODULE_AUTHOR("Ilya Yanok");
241MODULE_DESCRIPTION("NAND driver for Socrates board");