mtd: rawnand: Pass a nand_chip object to nand_scan()
[linux-2.6-block.git] / drivers / mtd / nand / raw / socrates_nand.c
CommitLineData
1b578193 1/*
1b578193
WG
2 * 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>
d4092d76 14#include <linux/mtd/rawnand.h>
1b578193 15#include <linux/mtd/partitions.h>
c11eede6 16#include <linux/of_address.h>
1b578193
WG
17#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;
1b578193
WG
31 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;
4bd4ebcc 45 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 46 struct socrates_nand_host *host = nand_get_controller_data(this);
1b578193
WG
47
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;
4bd4ebcc 64 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 65 struct socrates_nand_host *host = nand_get_controller_data(this);
1b578193
WG
66 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
1b578193
WG
88/*
89 * Hardware specific access to control-lines
90 */
91static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
92 unsigned int ctrl)
93{
4bd4ebcc 94 struct nand_chip *nand_chip = mtd_to_nand(mtd);
d699ed25 95 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
1b578193
WG
96 uint32_t val;
97
98 if (cmd == NAND_CMD_NONE)
99 return;
100
101 if (ctrl & NAND_CLE)
102 val = FPGA_NAND_CMD_COMMAND;
103 else
104 val = FPGA_NAND_CMD_ADDR;
105
106 if (ctrl & NAND_NCE)
107 val |= FPGA_NAND_ENABLE;
108
109 val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
110
111 out_be32(host->io_base, val);
112}
113
114/*
115 * Read the Device Ready pin.
116 */
117static int socrates_nand_device_ready(struct mtd_info *mtd)
118{
4bd4ebcc 119 struct nand_chip *nand_chip = mtd_to_nand(mtd);
d699ed25 120 struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
1b578193
WG
121
122 if (in_be32(host->io_base) & FPGA_NAND_BUSY)
123 return 0; /* busy */
124 return 1;
125}
126
1b578193
WG
127/*
128 * Probe for the NAND device.
129 */
06f25510 130static int socrates_nand_probe(struct platform_device *ofdev)
1b578193
WG
131{
132 struct socrates_nand_host *host;
133 struct mtd_info *mtd;
134 struct nand_chip *nand_chip;
135 int res;
1b578193
WG
136
137 /* Allocate memory for the device structure (and zero it) */
cf3a9b56
SK
138 host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
139 if (!host)
1b578193 140 return -ENOMEM;
1b578193 141
c8a4d0fd 142 host->io_base = of_iomap(ofdev->dev.of_node, 0);
1b578193 143 if (host->io_base == NULL) {
5422933d 144 dev_err(&ofdev->dev, "ioremap failed\n");
1b578193
WG
145 return -EIO;
146 }
147
1b578193 148 nand_chip = &host->nand_chip;
a723bf6a 149 mtd = nand_to_mtd(nand_chip);
1b578193
WG
150 host->dev = &ofdev->dev;
151
d699ed25
BB
152 /* link the private data structures */
153 nand_set_controller_data(nand_chip, host);
a61ae81a 154 nand_set_flash_node(nand_chip, ofdev->dev.of_node);
1b578193 155 mtd->name = "socrates_nand";
1b578193
WG
156 mtd->dev.parent = &ofdev->dev;
157
158 /*should never be accessed directly */
159 nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
160 nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
161
162 nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
163 nand_chip->read_byte = socrates_nand_read_byte;
1b578193
WG
164 nand_chip->write_buf = socrates_nand_write_buf;
165 nand_chip->read_buf = socrates_nand_read_buf;
1b578193
WG
166 nand_chip->dev_ready = socrates_nand_device_ready;
167
168 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
ce111afd 169 nand_chip->ecc.algo = NAND_ECC_HAMMING;
1b578193
WG
170
171 /* TODO: I have no idea what real delay is. */
172 nand_chip->chip_delay = 20; /* 20us command delay time */
173
174 dev_set_drvdata(&ofdev->dev, host);
175
00ad378f 176 res = nand_scan(nand_chip, 1);
83f48f80 177 if (res)
1b578193 178 goto out;
1b578193 179
a61ae81a 180 res = mtd_device_register(mtd, NULL, 0);
1b578193
WG
181 if (!res)
182 return res;
183
1b578193
WG
184 nand_release(mtd);
185
186out:
1b578193 187 iounmap(host->io_base);
1b578193
WG
188 return res;
189}
190
191/*
192 * Remove a NAND device.
193 */
810b7e06 194static int socrates_nand_remove(struct platform_device *ofdev)
1b578193
WG
195{
196 struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
a723bf6a 197 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
1b578193
WG
198
199 nand_release(mtd);
200
1b578193 201 iounmap(host->io_base);
1b578193
WG
202
203 return 0;
204}
205
b2d4fbab 206static const struct of_device_id socrates_nand_match[] =
1b578193
WG
207{
208 {
209 .compatible = "abb,socrates-nand",
210 },
211 {},
212};
213
214MODULE_DEVICE_TABLE(of, socrates_nand_match);
215
1c48a5c9 216static struct platform_driver socrates_nand_driver = {
4018294b
GL
217 .driver = {
218 .name = "socrates_nand",
4018294b
GL
219 .of_match_table = socrates_nand_match,
220 },
1b578193 221 .probe = socrates_nand_probe,
5153b88c 222 .remove = socrates_nand_remove,
1b578193
WG
223};
224
f99640de 225module_platform_driver(socrates_nand_driver);
1b578193
WG
226
227MODULE_LICENSE("GPL");
228MODULE_AUTHOR("Ilya Yanok");
229MODULE_DESCRIPTION("NAND driver for Socrates board");