blob: 20f40c0e812c89749babb7daffbd800ce6fe2f83 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Wolfgang Grandegger1b578192009-03-25 11:48:38 +01002/*
Wolfgang Grandegger1b578192009-03-25 11:48:38 +01003 * Copyright © 2008 Ilya Yanok, Emcraft Systems
Wolfgang Grandegger1b578192009-03-25 11:48:38 +01004 */
5
6#include <linux/slab.h>
7#include <linux/module.h>
8#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +02009#include <linux/mtd/rawnand.h>
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010010#include <linux/mtd/partitions.h>
Rob Herringc11eede2013-11-10 23:19:08 -060011#include <linux/of_address.h>
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010012#include <linux/of_platform.h>
13#include <linux/io.h>
14
15#define FPGA_NAND_CMD_MASK (0x7 << 28)
16#define FPGA_NAND_CMD_COMMAND (0x0 << 28)
17#define FPGA_NAND_CMD_ADDR (0x1 << 28)
18#define FPGA_NAND_CMD_READ (0x2 << 28)
19#define FPGA_NAND_CMD_WRITE (0x3 << 28)
20#define FPGA_NAND_BUSY (0x1 << 15)
21#define FPGA_NAND_ENABLE (0x1 << 31)
22#define FPGA_NAND_DATA_SHIFT 16
23
24struct socrates_nand_host {
25 struct nand_chip nand_chip;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010026 void __iomem *io_base;
27 struct device *dev;
28};
29
30/**
31 * socrates_nand_write_buf - write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +020032 * @this: NAND chip object
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010033 * @buf: data buffer
34 * @len: number of bytes to write
35 */
Boris Brezillonc0739d82018-09-06 14:05:23 +020036static void socrates_nand_write_buf(struct nand_chip *this, const uint8_t *buf,
37 int len)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010038{
39 int i;
Boris BREZILLONd699ed22015-12-10 09:00:41 +010040 struct socrates_nand_host *host = nand_get_controller_data(this);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010041
42 for (i = 0; i < len; i++) {
43 out_be32(host->io_base, FPGA_NAND_ENABLE |
44 FPGA_NAND_CMD_WRITE |
45 (buf[i] << FPGA_NAND_DATA_SHIFT));
46 }
47}
48
49/**
50 * socrates_nand_read_buf - read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +020051 * @this: NAND chip object
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010052 * @buf: buffer to store date
53 * @len: number of bytes to read
54 */
Boris Brezillon7e534322018-09-06 14:05:22 +020055static void socrates_nand_read_buf(struct nand_chip *this, uint8_t *buf,
56 int len)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010057{
58 int i;
Boris BREZILLONd699ed22015-12-10 09:00:41 +010059 struct socrates_nand_host *host = nand_get_controller_data(this);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010060 uint32_t val;
61
62 val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ;
63
64 out_be32(host->io_base, val);
65 for (i = 0; i < len; i++) {
66 buf[i] = (in_be32(host->io_base) >>
67 FPGA_NAND_DATA_SHIFT) & 0xff;
68 }
69}
70
71/**
72 * socrates_nand_read_byte - read one byte from the chip
73 * @mtd: MTD device structure
74 */
Boris Brezillon7e534322018-09-06 14:05:22 +020075static uint8_t socrates_nand_read_byte(struct nand_chip *this)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010076{
77 uint8_t byte;
Boris Brezillon7e534322018-09-06 14:05:22 +020078 socrates_nand_read_buf(this, &byte, sizeof(byte));
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010079 return byte;
80}
81
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010082/*
83 * Hardware specific access to control-lines
84 */
Boris Brezillon0f808c12018-09-06 14:05:26 +020085static void socrates_nand_cmd_ctrl(struct nand_chip *nand_chip, int cmd,
86 unsigned int ctrl)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010087{
Boris BREZILLONd699ed22015-12-10 09:00:41 +010088 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +010089 uint32_t val;
90
91 if (cmd == NAND_CMD_NONE)
92 return;
93
94 if (ctrl & NAND_CLE)
95 val = FPGA_NAND_CMD_COMMAND;
96 else
97 val = FPGA_NAND_CMD_ADDR;
98
99 if (ctrl & NAND_NCE)
100 val |= FPGA_NAND_ENABLE;
101
102 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
103
104 out_be32(host->io_base, val);
105}
106
107/*
108 * Read the Device Ready pin.
109 */
Boris Brezillon50a487e2018-09-06 14:05:27 +0200110static int socrates_nand_device_ready(struct nand_chip *nand_chip)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100111{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100112 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100113
114 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
115 return 0; /* busy */
116 return 1;
117}
118
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100119/*
120 * Probe for the NAND device.
121 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500122static int socrates_nand_probe(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100123{
124 struct socrates_nand_host *host;
125 struct mtd_info *mtd;
126 struct nand_chip *nand_chip;
127 int res;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100128
129 /* Allocate memory for the device structure (and zero it) */
Sachin Kamatcf3a9b52013-10-08 15:31:46 +0530130 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
131 if (!host)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100132 return -ENOMEM;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100133
Anatolij Gustschinc8a4d0f2010-06-03 02:37:17 +0200134 host->io_base = of_iomap(ofdev->dev.of_node, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100135 if (host->io_base == NULL) {
Sachin Kamat54229332013-10-08 15:38:08 +0530136 dev_err(&ofdev->dev, "ioremap failed\n");
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100137 return -EIO;
138 }
139
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100140 nand_chip = &host->nand_chip;
Boris BREZILLONa723bf62015-12-11 15:04:06 +0100141 mtd = nand_to_mtd(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100142 host->dev = &ofdev->dev;
143
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100144 /* link the private data structures */
145 nand_set_controller_data(nand_chip, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700146 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100147 mtd->name = "socrates_nand";
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100148 mtd->dev.parent = &ofdev->dev;
149
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200150 nand_chip->legacy.cmd_ctrl = socrates_nand_cmd_ctrl;
Boris Brezillon716bbba2018-09-07 00:38:35 +0200151 nand_chip->legacy.read_byte = socrates_nand_read_byte;
152 nand_chip->legacy.write_buf = socrates_nand_write_buf;
153 nand_chip->legacy.read_buf = socrates_nand_read_buf;
Boris Brezillon8395b752018-09-07 00:38:37 +0200154 nand_chip->legacy.dev_ready = socrates_nand_device_ready;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100155
156 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Rafał Miłeckice111af2016-04-08 12:23:51 +0200157 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100158
159 /* TODO: I have no idea what real delay is. */
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200160 nand_chip->legacy.chip_delay = 20; /* 20us command delay time */
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100161
162 dev_set_drvdata(&ofdev->dev, host);
163
Boris Brezillon00ad3782018-09-06 14:05:14 +0200164 res = nand_scan(nand_chip, 1);
Masahiro Yamada83f48f82016-11-04 19:43:10 +0900165 if (res)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100166 goto out;
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100167
Brian Norrisa61ae812015-10-30 20:33:25 -0700168 res = mtd_device_register(mtd, NULL, 0);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100169 if (!res)
170 return res;
171
Boris Brezillon59ac2762018-09-06 14:05:15 +0200172 nand_release(nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100173
174out:
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100175 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100176 return res;
177}
178
179/*
180 * Remove a NAND device.
181 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500182static int socrates_nand_remove(struct platform_device *ofdev)
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100183{
184 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100185
Boris Brezillon59ac2762018-09-06 14:05:15 +0200186 nand_release(&host->nand_chip);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100187
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100188 iounmap(host->io_base);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100189
190 return 0;
191}
192
Márton Némethb2d4fba2010-01-09 15:10:46 +0100193static const struct of_device_id socrates_nand_match[] =
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100194{
195 {
196 .compatible = "abb,socrates-nand",
197 },
198 {},
199};
200
201MODULE_DEVICE_TABLE(of, socrates_nand_match);
202
Grant Likely1c48a5c2011-02-17 02:43:24 -0700203static struct platform_driver socrates_nand_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700204 .driver = {
205 .name = "socrates_nand",
Grant Likely40182942010-04-13 16:13:02 -0700206 .of_match_table = socrates_nand_match,
207 },
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100208 .probe = socrates_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500209 .remove = socrates_nand_remove,
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100210};
211
Axel Linf99640d2011-11-27 20:45:03 +0800212module_platform_driver(socrates_nand_driver);
Wolfgang Grandegger1b578192009-03-25 11:48:38 +0100213
214MODULE_LICENSE("GPL");
215MODULE_AUTHOR("Ilya Yanok");
216MODULE_DESCRIPTION("NAND driver for Socrates board");