1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 Embedded Edge, LLC
6 #include <linux/slab.h>
7 #include <linux/module.h>
8 #include <linux/interrupt.h>
9 #include <linux/mtd/mtd.h>
10 #include <linux/mtd/rawnand.h>
11 #include <linux/mtd/partitions.h>
12 #include <linux/platform_device.h>
14 #include <asm/mach-au1x00/au1000.h>
15 #include <asm/mach-au1x00/au1550nd.h>
19 struct nand_controller controller;
20 struct nand_chip chip;
26 static struct au1550nd_ctx *chip_to_au_ctx(struct nand_chip *this)
28 return container_of(this, struct au1550nd_ctx, chip);
32 * au_write_buf - write buffer to chip
33 * @this: NAND chip object
35 * @len: number of bytes to write
37 * write function for 8bit buswidth
39 static void au_write_buf(struct nand_chip *this, const void *buf,
42 struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
46 for (i = 0; i < len; i++) {
47 writeb(p[i], ctx->base + MEM_STNAND_DATA);
48 wmb(); /* drain writebuffer */
53 * au_read_buf - read chip data into buffer
54 * @this: NAND chip object
55 * @buf: buffer to store date
56 * @len: number of bytes to read
58 * read function for 8bit buswidth
60 static void au_read_buf(struct nand_chip *this, void *buf,
63 struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
67 for (i = 0; i < len; i++) {
68 p[i] = readb(ctx->base + MEM_STNAND_DATA);
69 wmb(); /* drain writebuffer */
74 * au_write_buf16 - write buffer to chip
75 * @this: NAND chip object
77 * @len: number of bytes to write
79 * write function for 16bit buswidth
81 static void au_write_buf16(struct nand_chip *this, const void *buf,
84 struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
89 for (i = 0; i < len; i++) {
90 writew(p[i], ctx->base + MEM_STNAND_DATA);
91 wmb(); /* drain writebuffer */
96 * au_read_buf16 - read chip data into buffer
97 * @this: NAND chip object
98 * @buf: buffer to store date
99 * @len: number of bytes to read
101 * read function for 16bit buswidth
103 static void au_read_buf16(struct nand_chip *this, void *buf, unsigned int len)
105 struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
110 for (i = 0; i < len; i++) {
111 p[i] = readw(ctx->base + MEM_STNAND_DATA);
112 wmb(); /* drain writebuffer */
116 static int find_nand_cs(unsigned long nand_base)
119 (void __iomem *)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR);
120 unsigned long addr, staddr, start, mask, end;
123 for (i = 0; i < 4; i++) {
124 addr = 0x1000 + (i * 0x10); /* CSx */
125 staddr = __raw_readl(base + addr + 0x08); /* STADDRx */
126 /* figure out the decoded range of this CS */
127 start = (staddr << 4) & 0xfffc0000;
128 mask = (staddr << 18) & 0xfffc0000;
129 end = (start | (start - 1)) & ~(start ^ mask);
130 if ((nand_base >= start) && (nand_base < end))
137 static int au1550nd_waitrdy(struct nand_chip *this, unsigned int timeout_ms)
139 unsigned long timeout_jiffies = jiffies;
141 timeout_jiffies += msecs_to_jiffies(timeout_ms) + 1;
143 if (alchemy_rdsmem(AU1000_MEM_STSTAT) & 0x1)
146 usleep_range(10, 100);
147 } while (time_before(jiffies, timeout_jiffies));
152 static int au1550nd_exec_instr(struct nand_chip *this,
153 const struct nand_op_instr *instr)
155 struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
159 switch (instr->type) {
160 case NAND_OP_CMD_INSTR:
161 writeb(instr->ctx.cmd.opcode,
162 ctx->base + MEM_STNAND_CMD);
163 /* Drain the writebuffer */
167 case NAND_OP_ADDR_INSTR:
168 for (i = 0; i < instr->ctx.addr.naddrs; i++) {
169 writeb(instr->ctx.addr.addrs[i],
170 ctx->base + MEM_STNAND_ADDR);
171 /* Drain the writebuffer */
176 case NAND_OP_DATA_IN_INSTR:
177 if ((this->options & NAND_BUSWIDTH_16) &&
178 !instr->ctx.data.force_8bit)
179 au_read_buf16(this, instr->ctx.data.buf.in,
180 instr->ctx.data.len);
182 au_read_buf(this, instr->ctx.data.buf.in,
183 instr->ctx.data.len);
186 case NAND_OP_DATA_OUT_INSTR:
187 if ((this->options & NAND_BUSWIDTH_16) &&
188 !instr->ctx.data.force_8bit)
189 au_write_buf16(this, instr->ctx.data.buf.out,
190 instr->ctx.data.len);
192 au_write_buf(this, instr->ctx.data.buf.out,
193 instr->ctx.data.len);
196 case NAND_OP_WAITRDY_INSTR:
197 ret = au1550nd_waitrdy(this, instr->ctx.waitrdy.timeout_ms);
204 ndelay(instr->delay_ns);
209 static int au1550nd_exec_op(struct nand_chip *this,
210 const struct nand_operation *op,
213 struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
220 /* assert (force assert) chip enable */
221 alchemy_wrsmem((1 << (4 + ctx->cs)), AU1000_MEM_STNDCTL);
222 /* Drain the writebuffer */
225 for (i = 0; i < op->ninstrs; i++) {
226 ret = au1550nd_exec_instr(this, &op->instrs[i]);
231 /* deassert chip enable */
232 alchemy_wrsmem(0, AU1000_MEM_STNDCTL);
233 /* Drain the writebuffer */
239 static int au1550nd_attach_chip(struct nand_chip *chip)
241 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
242 chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
243 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
248 static const struct nand_controller_ops au1550nd_ops = {
249 .exec_op = au1550nd_exec_op,
250 .attach_chip = au1550nd_attach_chip,
253 static int au1550nd_probe(struct platform_device *pdev)
255 struct au1550nd_platdata *pd;
256 struct au1550nd_ctx *ctx;
257 struct nand_chip *this;
258 struct mtd_info *mtd;
262 pd = dev_get_platdata(&pdev->dev);
264 dev_err(&pdev->dev, "missing platform data\n");
268 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
272 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
274 dev_err(&pdev->dev, "no NAND memory resource\n");
278 if (request_mem_region(r->start, resource_size(r), "au1550-nand")) {
279 dev_err(&pdev->dev, "cannot claim NAND memory area\n");
284 ctx->base = ioremap(r->start, 0x1000);
286 dev_err(&pdev->dev, "cannot remap NAND memory area\n");
292 mtd = nand_to_mtd(this);
293 mtd->dev.parent = &pdev->dev;
295 /* figure out which CS# r->start belongs to */
296 cs = find_nand_cs(r->start);
298 dev_err(&pdev->dev, "cannot detect NAND chipselect\n");
304 nand_controller_init(&ctx->controller);
305 ctx->controller.ops = &au1550nd_ops;
306 this->controller = &ctx->controller;
309 this->options |= NAND_BUSWIDTH_16;
312 * This driver assumes that the default ECC engine should be TYPE_SOFT.
313 * Set ->engine_type before registering the NAND devices in order to
314 * provide a driver specific default value.
316 this->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
318 ret = nand_scan(this, 1);
320 dev_err(&pdev->dev, "NAND scan failed with %d\n", ret);
324 mtd_device_register(mtd, pd->parts, pd->num_parts);
326 platform_set_drvdata(pdev, ctx);
333 release_mem_region(r->start, resource_size(r));
339 static int au1550nd_remove(struct platform_device *pdev)
341 struct au1550nd_ctx *ctx = platform_get_drvdata(pdev);
342 struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
343 struct nand_chip *chip = &ctx->chip;
346 ret = mtd_device_unregister(nand_to_mtd(chip));
350 release_mem_region(r->start, 0x1000);
355 static struct platform_driver au1550nd_driver = {
357 .name = "au1550-nand",
359 .probe = au1550nd_probe,
360 .remove = au1550nd_remove,
363 module_platform_driver(au1550nd_driver);
365 MODULE_LICENSE("GPL");
366 MODULE_AUTHOR("Embedded Edge, LLC");
367 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");