blob: 0ca81fa956b9920f54aa718adbebc31e03441dee [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
Boris Brezillonc0739d82018-09-06 14:05:23 +020037 * @this: NAND chip object
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010038 * @buf: data buffer
39 * @len: number of bytes to write
40 */
Boris Brezillonc0739d82018-09-06 14:05:23 +020041static void socrates_nand_write_buf(struct nand_chip *this, const uint8_t *buf,
42 int len)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010043{
44 int i;
Boris BREZILLONd699ed22015-12-10 09:00:41 +010045 struct socrates_nand_host *host = nand_get_controller_data(this);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010046
47 for (i = 0; i < len; i++) {
48 out_be32(host->io_base, FPGA_NAND_ENABLE |
49 FPGA_NAND_CMD_WRITE |
50 (buf[i] << FPGA_NAND_DATA_SHIFT));
51 }
52}
53
54/**
55 * socrates_nand_read_buf - read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +020056 * @this: NAND chip object
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010057 * @buf: buffer to store date
58 * @len: number of bytes to read
59 */
Boris Brezillon7e534322018-09-06 14:05:22 +020060static void socrates_nand_read_buf(struct nand_chip *this, uint8_t *buf,
61 int len)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010062{
63 int i;
Boris BREZILLONd699ed22015-12-10 09:00:41 +010064 struct socrates_nand_host *host = nand_get_controller_data(this);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010065 uint32_t val;
66
67 val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ;
68
69 out_be32(host->io_base, val);
70 for (i = 0; i < len; i++) {
71 buf[i] = (in_be32(host->io_base) >>
72 FPGA_NAND_DATA_SHIFT) & 0xff;
73 }
74}
75
76/**
77 * socrates_nand_read_byte - read one byte from the chip
78 * @mtd: MTD device structure
79 */
Boris Brezillon7e534322018-09-06 14:05:22 +020080static uint8_t socrates_nand_read_byte(struct nand_chip *this)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010081{
82 uint8_t byte;
Boris Brezillon7e534322018-09-06 14:05:22 +020083 socrates_nand_read_buf(this, &byte, sizeof(byte));
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010084 return byte;
85}
86
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010087/*
88 * Hardware specific access to control-lines
89 */
Boris Brezillon0f808c12018-09-06 14:05:26 +020090static void socrates_nand_cmd_ctrl(struct nand_chip *nand_chip, int cmd,
91 unsigned int ctrl)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010092{
Boris BREZILLONd699ed22015-12-10 09:00:41 +010093 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010094 uint32_t val;
95
96 if (cmd == NAND_CMD_NONE)
97 return;
98
99 if (ctrl & NAND_CLE)
100 val = FPGA_NAND_CMD_COMMAND;
101 else
102 val = FPGA_NAND_CMD_ADDR;
103
104 if (ctrl & NAND_NCE)
105 val |= FPGA_NAND_ENABLE;
106
107 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
108
109 out_be32(host->io_base, val);
110}
111
112/*
113 * Read the Device Ready pin.
114 */
Boris Brezillon50a487e2018-09-06 14:05:27 +0200115static int socrates_nand_device_ready(struct nand_chip *nand_chip)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100116{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100117 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100118
119 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
120 return 0; /* busy */
121 return 1;
122}
123
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100124/*
125 * Probe for the NAND device.
126 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500127static int socrates_nand_probe(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100128{
129 struct socrates_nand_host *host;
130 struct mtd_info *mtd;
131 struct nand_chip *nand_chip;
132 int res;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100133
134 /* Allocate memory for the device structure (and zero it) */
Sachin Kamatcf3a9b52013-10-08 15:31:46 +0530135 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
136 if (!host)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100137 return -ENOMEM;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100138
Anatolij Gustschinc8a4d0f2010-06-03 02:37:17 +0200139 host->io_base = of_iomap(ofdev->dev.of_node, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100140 if (host->io_base == NULL) {
Sachin Kamat54229332013-10-08 15:38:08 +0530141 dev_err(&ofdev->dev, "ioremap failed\n");
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100142 return -EIO;
143 }
144
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100145 nand_chip = &host->nand_chip;
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100146 mtd = nand_to_mtd(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100147 host->dev = &ofdev->dev;
148
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100149 /* link the private data structures */
150 nand_set_controller_data(nand_chip, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700151 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100152 mtd->name = "socrates_nand";
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100153 mtd->dev.parent = &ofdev->dev;
154
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200155 nand_chip->legacy.cmd_ctrl = socrates_nand_cmd_ctrl;
Boris Brezillon716bbba2018-09-07 00:38:35 +0200156 nand_chip->legacy.read_byte = socrates_nand_read_byte;
157 nand_chip->legacy.write_buf = socrates_nand_write_buf;
158 nand_chip->legacy.read_buf = socrates_nand_read_buf;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100159 nand_chip->dev_ready = socrates_nand_device_ready;
160
161 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Rafał Miłeckice111af2016-04-08 12:23:51 +0200162 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100163
164 /* TODO: I have no idea what real delay is. */
165 nand_chip->chip_delay = 20; /* 20us command delay time */
166
167 dev_set_drvdata(&ofdev->dev, host);
168
Boris Brezillon00ad3782018-09-06 14:05:14 +0200169 res = nand_scan(nand_chip, 1);
Masahiro Yamada83f48f82016-11-04 19:43:10 +0900170 if (res)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100171 goto out;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100172
Brian Norrisa61ae812015-10-30 20:33:25 -0700173 res = mtd_device_register(mtd, NULL, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100174 if (!res)
175 return res;
176
Boris Brezillon59ac2762018-09-06 14:05:15 +0200177 nand_release(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100178
179out:
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100180 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100181 return res;
182}
183
184/*
185 * Remove a NAND device.
186 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500187static int socrates_nand_remove(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100188{
189 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100190
Boris Brezillon59ac2762018-09-06 14:05:15 +0200191 nand_release(&host->nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100192
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100193 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100194
195 return 0;
196}
197
Márton Némethb2d4fba2010-01-09 15:10:46 +0100198static const struct of_device_id socrates_nand_match[] =
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100199{
200 {
201 .compatible = "abb,socrates-nand",
202 },
203 {},
204};
205
206MODULE_DEVICE_TABLE(of, socrates_nand_match);
207
Grant Likely1c48a5c2011-02-17 02:43:24 -0700208static struct platform_driver socrates_nand_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700209 .driver = {
210 .name = "socrates_nand",
Grant Likely40182942010-04-13 16:13:02 -0700211 .of_match_table = socrates_nand_match,
212 },
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100213 .probe = socrates_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500214 .remove = socrates_nand_remove,
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100215};
216
Axel Linf99640d2011-11-27 20:45:03 +0800217module_platform_driver(socrates_nand_driver);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100218
219MODULE_LICENSE("GPL");
220MODULE_AUTHOR("Ilya Yanok");
221MODULE_DESCRIPTION("NAND driver for Socrates board");