blob: deedc1cd4dee11fe077c75858f2586a1408fd0a0 [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 */
90static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
91 unsigned int ctrl)
92{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010093 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010094 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010095 uint32_t val;
96
97 if (cmd == NAND_CMD_NONE)
98 return;
99
100 if (ctrl & NAND_CLE)
101 val = FPGA_NAND_CMD_COMMAND;
102 else
103 val = FPGA_NAND_CMD_ADDR;
104
105 if (ctrl & NAND_NCE)
106 val |= FPGA_NAND_ENABLE;
107
108 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
109
110 out_be32(host->io_base, val);
111}
112
113/*
114 * Read the Device Ready pin.
115 */
116static int socrates_nand_device_ready(struct mtd_info *mtd)
117{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100118 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100119 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100120
121 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
122 return 0; /* busy */
123 return 1;
124}
125
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100126/*
127 * Probe for the NAND device.
128 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500129static int socrates_nand_probe(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100130{
131 struct socrates_nand_host *host;
132 struct mtd_info *mtd;
133 struct nand_chip *nand_chip;
134 int res;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100135
136 /* Allocate memory for the device structure (and zero it) */
Sachin Kamatcf3a9b52013-10-08 15:31:46 +0530137 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
138 if (!host)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100139 return -ENOMEM;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100140
Anatolij Gustschinc8a4d0f2010-06-03 02:37:17 +0200141 host->io_base = of_iomap(ofdev->dev.of_node, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100142 if (host->io_base == NULL) {
Sachin Kamat54229332013-10-08 15:38:08 +0530143 dev_err(&ofdev->dev, "ioremap failed\n");
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100144 return -EIO;
145 }
146
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100147 nand_chip = &host->nand_chip;
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100148 mtd = nand_to_mtd(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100149 host->dev = &ofdev->dev;
150
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100151 /* link the private data structures */
152 nand_set_controller_data(nand_chip, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700153 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100154 mtd->name = "socrates_nand";
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100155 mtd->dev.parent = &ofdev->dev;
156
157 /*should never be accessed directly */
158 nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
159 nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
160
161 nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
162 nand_chip->read_byte = socrates_nand_read_byte;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100163 nand_chip->write_buf = socrates_nand_write_buf;
164 nand_chip->read_buf = socrates_nand_read_buf;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100165 nand_chip->dev_ready = socrates_nand_device_ready;
166
167 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Rafał Miłeckice111af2016-04-08 12:23:51 +0200168 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100169
170 /* TODO: I have no idea what real delay is. */
171 nand_chip->chip_delay = 20; /* 20us command delay time */
172
173 dev_set_drvdata(&ofdev->dev, host);
174
Boris Brezillon00ad3782018-09-06 14:05:14 +0200175 res = nand_scan(nand_chip, 1);
Masahiro Yamada83f48f82016-11-04 19:43:10 +0900176 if (res)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100177 goto out;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100178
Brian Norrisa61ae812015-10-30 20:33:25 -0700179 res = mtd_device_register(mtd, NULL, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100180 if (!res)
181 return res;
182
Boris Brezillon59ac2762018-09-06 14:05:15 +0200183 nand_release(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100184
185out:
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100186 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100187 return res;
188}
189
190/*
191 * Remove a NAND device.
192 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500193static int socrates_nand_remove(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100194{
195 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100196
Boris Brezillon59ac2762018-09-06 14:05:15 +0200197 nand_release(&host->nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100198
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100199 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100200
201 return 0;
202}
203
Márton Némethb2d4fba2010-01-09 15:10:46 +0100204static const struct of_device_id socrates_nand_match[] =
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100205{
206 {
207 .compatible = "abb,socrates-nand",
208 },
209 {},
210};
211
212MODULE_DEVICE_TABLE(of, socrates_nand_match);
213
Grant Likely1c48a5c2011-02-17 02:43:24 -0700214static struct platform_driver socrates_nand_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700215 .driver = {
216 .name = "socrates_nand",
Grant Likely40182942010-04-13 16:13:02 -0700217 .of_match_table = socrates_nand_match,
218 },
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100219 .probe = socrates_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500220 .remove = socrates_nand_remove,
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100221};
222
Axel Linf99640d2011-11-27 20:45:03 +0800223module_platform_driver(socrates_nand_driver);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100224
225MODULE_LICENSE("GPL");
226MODULE_AUTHOR("Ilya Yanok");
227MODULE_DESCRIPTION("NAND driver for Socrates board");