blob: c44b19fc1350e29e88164e7787618f84629d0c7e [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 */
115static int socrates_nand_device_ready(struct mtd_info *mtd)
116{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100117 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100118 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100119
120 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
121 return 0; /* busy */
122 return 1;
123}
124
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100125/*
126 * Probe for the NAND device.
127 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500128static int socrates_nand_probe(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100129{
130 struct socrates_nand_host *host;
131 struct mtd_info *mtd;
132 struct nand_chip *nand_chip;
133 int res;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100134
135 /* Allocate memory for the device structure (and zero it) */
Sachin Kamatcf3a9b52013-10-08 15:31:46 +0530136 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
137 if (!host)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100138 return -ENOMEM;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100139
Anatolij Gustschinc8a4d0f2010-06-03 02:37:17 +0200140 host->io_base = of_iomap(ofdev->dev.of_node, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100141 if (host->io_base == NULL) {
Sachin Kamat54229332013-10-08 15:38:08 +0530142 dev_err(&ofdev->dev, "ioremap failed\n");
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100143 return -EIO;
144 }
145
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100146 nand_chip = &host->nand_chip;
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100147 mtd = nand_to_mtd(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100148 host->dev = &ofdev->dev;
149
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100150 /* link the private data structures */
151 nand_set_controller_data(nand_chip, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700152 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100153 mtd->name = "socrates_nand";
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100154 mtd->dev.parent = &ofdev->dev;
155
156 /*should never be accessed directly */
157 nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
158 nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
159
160 nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
161 nand_chip->read_byte = socrates_nand_read_byte;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100162 nand_chip->write_buf = socrates_nand_write_buf;
163 nand_chip->read_buf = socrates_nand_read_buf;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100164 nand_chip->dev_ready = socrates_nand_device_ready;
165
166 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Rafał Miłeckice111af2016-04-08 12:23:51 +0200167 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100168
169 /* TODO: I have no idea what real delay is. */
170 nand_chip->chip_delay = 20; /* 20us command delay time */
171
172 dev_set_drvdata(&ofdev->dev, host);
173
Boris Brezillon00ad3782018-09-06 14:05:14 +0200174 res = nand_scan(nand_chip, 1);
Masahiro Yamada83f48f82016-11-04 19:43:10 +0900175 if (res)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100176 goto out;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100177
Brian Norrisa61ae812015-10-30 20:33:25 -0700178 res = mtd_device_register(mtd, NULL, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100179 if (!res)
180 return res;
181
Boris Brezillon59ac2762018-09-06 14:05:15 +0200182 nand_release(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100183
184out:
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100185 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100186 return res;
187}
188
189/*
190 * Remove a NAND device.
191 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500192static int socrates_nand_remove(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100193{
194 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100195
Boris Brezillon59ac2762018-09-06 14:05:15 +0200196 nand_release(&host->nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100197
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100198 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100199
200 return 0;
201}
202
Márton Némethb2d4fba2010-01-09 15:10:46 +0100203static const struct of_device_id socrates_nand_match[] =
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100204{
205 {
206 .compatible = "abb,socrates-nand",
207 },
208 {},
209};
210
211MODULE_DEVICE_TABLE(of, socrates_nand_match);
212
Grant Likely1c48a5c2011-02-17 02:43:24 -0700213static struct platform_driver socrates_nand_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700214 .driver = {
215 .name = "socrates_nand",
Grant Likely40182942010-04-13 16:13:02 -0700216 .of_match_table = socrates_nand_match,
217 },
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100218 .probe = socrates_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500219 .remove = socrates_nand_remove,
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100220};
221
Axel Linf99640d2011-11-27 20:45:03 +0800222module_platform_driver(socrates_nand_driver);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100223
224MODULE_LICENSE("GPL");
225MODULE_AUTHOR("Ilya Yanok");
226MODULE_DESCRIPTION("NAND driver for Socrates board");