1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 Embedded Edge, LLC
6 #include <linux/delay.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/mtd/mtd.h>
11 #include <linux/mtd/rawnand.h>
12 #include <linux/mtd/partitions.h>
13 #include <linux/platform_device.h>
15 #include <asm/mach-au1x00/au1000.h>
16 #include <asm/mach-au1x00/au1550nd.h>
20 struct nand_controller controller
;
21 struct nand_chip chip
;
27 static struct au1550nd_ctx
*chip_to_au_ctx(struct nand_chip
*this)
29 return container_of(this, struct au1550nd_ctx
, chip
);
33 * au_write_buf - write buffer to chip
34 * @this: NAND chip object
36 * @len: number of bytes to write
38 * write function for 8bit buswidth
40 static void au_write_buf(struct nand_chip
*this, const void *buf
,
43 struct au1550nd_ctx
*ctx
= chip_to_au_ctx(this);
47 for (i
= 0; i
< len
; i
++) {
48 writeb(p
[i
], ctx
->base
+ MEM_STNAND_DATA
);
49 wmb(); /* drain writebuffer */
54 * au_read_buf - read chip data into buffer
55 * @this: NAND chip object
56 * @buf: buffer to store date
57 * @len: number of bytes to read
59 * read function for 8bit buswidth
61 static void au_read_buf(struct nand_chip
*this, void *buf
,
64 struct au1550nd_ctx
*ctx
= chip_to_au_ctx(this);
68 for (i
= 0; i
< len
; i
++) {
69 p
[i
] = readb(ctx
->base
+ MEM_STNAND_DATA
);
70 wmb(); /* drain writebuffer */
75 * au_write_buf16 - write buffer to chip
76 * @this: NAND chip object
78 * @len: number of bytes to write
80 * write function for 16bit buswidth
82 static void au_write_buf16(struct nand_chip
*this, const void *buf
,
85 struct au1550nd_ctx
*ctx
= chip_to_au_ctx(this);
90 for (i
= 0; i
< len
; i
++) {
91 writew(p
[i
], ctx
->base
+ MEM_STNAND_DATA
);
92 wmb(); /* drain writebuffer */
97 * au_read_buf16 - read chip data into buffer
98 * @this: NAND chip object
99 * @buf: buffer to store date
100 * @len: number of bytes to read
102 * read function for 16bit buswidth
104 static void au_read_buf16(struct nand_chip
*this, void *buf
, unsigned int len
)
106 struct au1550nd_ctx
*ctx
= chip_to_au_ctx(this);
111 for (i
= 0; i
< len
; i
++) {
112 p
[i
] = readw(ctx
->base
+ MEM_STNAND_DATA
);
113 wmb(); /* drain writebuffer */
117 static int find_nand_cs(unsigned long nand_base
)
120 (void __iomem
*)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR
);
121 unsigned long addr
, staddr
, start
, mask
, end
;
124 for (i
= 0; i
< 4; i
++) {
125 addr
= 0x1000 + (i
* 0x10); /* CSx */
126 staddr
= __raw_readl(base
+ addr
+ 0x08); /* STADDRx */
127 /* figure out the decoded range of this CS */
128 start
= (staddr
<< 4) & 0xfffc0000;
129 mask
= (staddr
<< 18) & 0xfffc0000;
130 end
= (start
| (start
- 1)) & ~(start
^ mask
);
131 if ((nand_base
>= start
) && (nand_base
< end
))
138 static int au1550nd_waitrdy(struct nand_chip
*this, unsigned int timeout_ms
)
140 unsigned long timeout_jiffies
= jiffies
;
142 timeout_jiffies
+= msecs_to_jiffies(timeout_ms
) + 1;
144 if (alchemy_rdsmem(AU1000_MEM_STSTAT
) & 0x1)
147 usleep_range(10, 100);
148 } while (time_before(jiffies
, timeout_jiffies
));
153 static int au1550nd_exec_instr(struct nand_chip
*this,
154 const struct nand_op_instr
*instr
)
156 struct au1550nd_ctx
*ctx
= chip_to_au_ctx(this);
160 switch (instr
->type
) {
161 case NAND_OP_CMD_INSTR
:
162 writeb(instr
->ctx
.cmd
.opcode
,
163 ctx
->base
+ MEM_STNAND_CMD
);
164 /* Drain the writebuffer */
168 case NAND_OP_ADDR_INSTR
:
169 for (i
= 0; i
< instr
->ctx
.addr
.naddrs
; i
++) {
170 writeb(instr
->ctx
.addr
.addrs
[i
],
171 ctx
->base
+ MEM_STNAND_ADDR
);
172 /* Drain the writebuffer */
177 case NAND_OP_DATA_IN_INSTR
:
178 if ((this->options
& NAND_BUSWIDTH_16
) &&
179 !instr
->ctx
.data
.force_8bit
)
180 au_read_buf16(this, instr
->ctx
.data
.buf
.in
,
181 instr
->ctx
.data
.len
);
183 au_read_buf(this, instr
->ctx
.data
.buf
.in
,
184 instr
->ctx
.data
.len
);
187 case NAND_OP_DATA_OUT_INSTR
:
188 if ((this->options
& NAND_BUSWIDTH_16
) &&
189 !instr
->ctx
.data
.force_8bit
)
190 au_write_buf16(this, instr
->ctx
.data
.buf
.out
,
191 instr
->ctx
.data
.len
);
193 au_write_buf(this, instr
->ctx
.data
.buf
.out
,
194 instr
->ctx
.data
.len
);
197 case NAND_OP_WAITRDY_INSTR
:
198 ret
= au1550nd_waitrdy(this, instr
->ctx
.waitrdy
.timeout_ms
);
205 ndelay(instr
->delay_ns
);
210 static int au1550nd_exec_op(struct nand_chip
*this,
211 const struct nand_operation
*op
,
214 struct au1550nd_ctx
*ctx
= chip_to_au_ctx(this);
221 /* assert (force assert) chip enable */
222 alchemy_wrsmem((1 << (4 + ctx
->cs
)), AU1000_MEM_STNDCTL
);
223 /* Drain the writebuffer */
226 for (i
= 0; i
< op
->ninstrs
; i
++) {
227 ret
= au1550nd_exec_instr(this, &op
->instrs
[i
]);
232 /* deassert chip enable */
233 alchemy_wrsmem(0, AU1000_MEM_STNDCTL
);
234 /* Drain the writebuffer */
240 static int au1550nd_attach_chip(struct nand_chip
*chip
)
242 chip
->ecc
.engine_type
= NAND_ECC_ENGINE_TYPE_SOFT
;
244 if (chip
->ecc
.algo
== NAND_ECC_ALGO_UNKNOWN
)
245 chip
->ecc
.algo
= NAND_ECC_ALGO_HAMMING
;
250 static const struct nand_controller_ops au1550nd_ops
= {
251 .exec_op
= au1550nd_exec_op
,
252 .attach_chip
= au1550nd_attach_chip
,
255 static int au1550nd_probe(struct platform_device
*pdev
)
257 struct au1550nd_platdata
*pd
;
258 struct au1550nd_ctx
*ctx
;
259 struct nand_chip
*this;
260 struct mtd_info
*mtd
;
264 pd
= dev_get_platdata(&pdev
->dev
);
266 dev_err(&pdev
->dev
, "missing platform data\n");
270 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
274 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
276 dev_err(&pdev
->dev
, "no NAND memory resource\n");
280 if (request_mem_region(r
->start
, resource_size(r
), "au1550-nand")) {
281 dev_err(&pdev
->dev
, "cannot claim NAND memory area\n");
286 ctx
->base
= ioremap(r
->start
, 0x1000);
288 dev_err(&pdev
->dev
, "cannot remap NAND memory area\n");
294 mtd
= nand_to_mtd(this);
295 mtd
->dev
.parent
= &pdev
->dev
;
297 /* figure out which CS# r->start belongs to */
298 cs
= find_nand_cs(r
->start
);
300 dev_err(&pdev
->dev
, "cannot detect NAND chipselect\n");
306 nand_controller_init(&ctx
->controller
);
307 ctx
->controller
.ops
= &au1550nd_ops
;
308 this->controller
= &ctx
->controller
;
311 this->options
|= NAND_BUSWIDTH_16
;
313 ret
= nand_scan(this, 1);
315 dev_err(&pdev
->dev
, "NAND scan failed with %d\n", ret
);
319 mtd_device_register(mtd
, pd
->parts
, pd
->num_parts
);
321 platform_set_drvdata(pdev
, ctx
);
328 release_mem_region(r
->start
, resource_size(r
));
334 static int au1550nd_remove(struct platform_device
*pdev
)
336 struct au1550nd_ctx
*ctx
= platform_get_drvdata(pdev
);
337 struct resource
*r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
338 struct nand_chip
*chip
= &ctx
->chip
;
341 ret
= mtd_device_unregister(nand_to_mtd(chip
));
345 release_mem_region(r
->start
, 0x1000);
350 static struct platform_driver au1550nd_driver
= {
352 .name
= "au1550-nand",
354 .probe
= au1550nd_probe
,
355 .remove
= au1550nd_remove
,
358 module_platform_driver(au1550nd_driver
);
360 MODULE_LICENSE("GPL");
361 MODULE_AUTHOR("Embedded Edge, LLC");
362 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");