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_chip chip
;
23 void (*write_byte
)(struct nand_chip
*, u_char
);
27 * au_read_byte - read one byte from the chip
28 * @this: NAND chip object
30 * read function for 8bit buswidth
32 static u_char
au_read_byte(struct nand_chip
*this)
34 u_char ret
= readb(this->legacy
.IO_ADDR_R
);
35 wmb(); /* drain writebuffer */
40 * au_write_byte - write one byte to the chip
41 * @this: NAND chip object
42 * @byte: pointer to data byte to write
44 * write function for 8it buswidth
46 static void au_write_byte(struct nand_chip
*this, u_char byte
)
48 writeb(byte
, this->legacy
.IO_ADDR_W
);
49 wmb(); /* drain writebuffer */
53 * au_read_byte16 - read one byte endianness aware from the chip
54 * @this: NAND chip object
56 * read function for 16bit buswidth with endianness conversion
58 static u_char
au_read_byte16(struct nand_chip
*this)
60 u_char ret
= (u_char
) cpu_to_le16(readw(this->legacy
.IO_ADDR_R
));
61 wmb(); /* drain writebuffer */
66 * au_write_byte16 - write one byte endianness aware to the chip
67 * @this: NAND chip object
68 * @byte: pointer to data byte to write
70 * write function for 16bit buswidth with endianness conversion
72 static void au_write_byte16(struct nand_chip
*this, u_char byte
)
74 writew(le16_to_cpu((u16
) byte
), this->legacy
.IO_ADDR_W
);
75 wmb(); /* drain writebuffer */
79 * au_write_buf - write buffer to chip
80 * @this: NAND chip object
82 * @len: number of bytes to write
84 * write function for 8bit buswidth
86 static void au_write_buf(struct nand_chip
*this, const u_char
*buf
, int len
)
90 for (i
= 0; i
< len
; i
++) {
91 writeb(buf
[i
], this->legacy
.IO_ADDR_W
);
92 wmb(); /* drain writebuffer */
97 * au_read_buf - 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 8bit buswidth
104 static void au_read_buf(struct nand_chip
*this, u_char
*buf
, int len
)
108 for (i
= 0; i
< len
; i
++) {
109 buf
[i
] = readb(this->legacy
.IO_ADDR_R
);
110 wmb(); /* drain writebuffer */
115 * au_write_buf16 - write buffer to chip
116 * @this: NAND chip object
118 * @len: number of bytes to write
120 * write function for 16bit buswidth
122 static void au_write_buf16(struct nand_chip
*this, const u_char
*buf
, int len
)
125 u16
*p
= (u16
*) buf
;
128 for (i
= 0; i
< len
; i
++) {
129 writew(p
[i
], this->legacy
.IO_ADDR_W
);
130 wmb(); /* drain writebuffer */
136 * au_read_buf16 - read chip data into buffer
137 * @this: NAND chip object
138 * @buf: buffer to store date
139 * @len: number of bytes to read
141 * read function for 16bit buswidth
143 static void au_read_buf16(struct nand_chip
*this, u_char
*buf
, int len
)
146 u16
*p
= (u16
*) buf
;
149 for (i
= 0; i
< len
; i
++) {
150 p
[i
] = readw(this->legacy
.IO_ADDR_R
);
151 wmb(); /* drain writebuffer */
155 /* Select the chip by setting nCE to low */
156 #define NAND_CTL_SETNCE 1
157 /* Deselect the chip by setting nCE to high */
158 #define NAND_CTL_CLRNCE 2
159 /* Select the command latch by setting CLE to high */
160 #define NAND_CTL_SETCLE 3
161 /* Deselect the command latch by setting CLE to low */
162 #define NAND_CTL_CLRCLE 4
163 /* Select the address latch by setting ALE to high */
164 #define NAND_CTL_SETALE 5
165 /* Deselect the address latch by setting ALE to low */
166 #define NAND_CTL_CLRALE 6
168 static void au1550_hwcontrol(struct mtd_info
*mtd
, int cmd
)
170 struct nand_chip
*this = mtd_to_nand(mtd
);
171 struct au1550nd_ctx
*ctx
= container_of(this, struct au1550nd_ctx
,
176 case NAND_CTL_SETCLE
:
177 this->legacy
.IO_ADDR_W
= ctx
->base
+ MEM_STNAND_CMD
;
180 case NAND_CTL_CLRCLE
:
181 this->legacy
.IO_ADDR_W
= ctx
->base
+ MEM_STNAND_DATA
;
184 case NAND_CTL_SETALE
:
185 this->legacy
.IO_ADDR_W
= ctx
->base
+ MEM_STNAND_ADDR
;
188 case NAND_CTL_CLRALE
:
189 this->legacy
.IO_ADDR_W
= ctx
->base
+ MEM_STNAND_DATA
;
190 /* FIXME: Nobody knows why this is necessary,
191 * but it works only that way */
195 case NAND_CTL_SETNCE
:
196 /* assert (force assert) chip enable */
197 alchemy_wrsmem((1 << (4 + ctx
->cs
)), AU1000_MEM_STNDCTL
);
200 case NAND_CTL_CLRNCE
:
201 /* deassert chip enable */
202 alchemy_wrsmem(0, AU1000_MEM_STNDCTL
);
206 this->legacy
.IO_ADDR_R
= this->legacy
.IO_ADDR_W
;
208 wmb(); /* Drain the writebuffer */
211 int au1550_device_ready(struct nand_chip
*this)
213 return (alchemy_rdsmem(AU1000_MEM_STSTAT
) & 0x1) ? 1 : 0;
217 * au1550_select_chip - control -CE line
218 * Forbid driving -CE manually permitting the NAND controller to do this.
219 * Keeping -CE asserted during the whole sector reads interferes with the
220 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
221 * We only have to hold -CE low for the NAND read commands since the flash
222 * chip needs it to be asserted during chip not ready time but the NAND
223 * controller keeps it released.
225 * @this: NAND chip object
226 * @chip: chipnumber to select, -1 for deselect
228 static void au1550_select_chip(struct nand_chip
*this, int chip
)
233 * au1550_command - Send command to NAND device
234 * @this: NAND chip object
235 * @command: the command to be sent
236 * @column: the column address for this command, -1 if none
237 * @page_addr: the page address for this command, -1 if none
239 static void au1550_command(struct nand_chip
*this, unsigned command
,
240 int column
, int page_addr
)
242 struct mtd_info
*mtd
= nand_to_mtd(this);
243 struct au1550nd_ctx
*ctx
= container_of(this, struct au1550nd_ctx
,
245 int ce_override
= 0, i
;
246 unsigned long flags
= 0;
248 /* Begin command latch cycle */
249 au1550_hwcontrol(mtd
, NAND_CTL_SETCLE
);
251 * Write out the command to the device.
253 if (command
== NAND_CMD_SEQIN
) {
256 if (column
>= mtd
->writesize
) {
258 column
-= mtd
->writesize
;
259 readcmd
= NAND_CMD_READOOB
;
260 } else if (column
< 256) {
261 /* First 256 bytes --> READ0 */
262 readcmd
= NAND_CMD_READ0
;
265 readcmd
= NAND_CMD_READ1
;
267 ctx
->write_byte(this, readcmd
);
269 ctx
->write_byte(this, command
);
271 /* Set ALE and clear CLE to start address cycle */
272 au1550_hwcontrol(mtd
, NAND_CTL_CLRCLE
);
274 if (column
!= -1 || page_addr
!= -1) {
275 au1550_hwcontrol(mtd
, NAND_CTL_SETALE
);
277 /* Serially input address */
279 /* Adjust columns for 16 bit buswidth */
280 if (this->options
& NAND_BUSWIDTH_16
&&
281 !nand_opcode_8bits(command
))
283 ctx
->write_byte(this, column
);
285 if (page_addr
!= -1) {
286 ctx
->write_byte(this, (u8
)(page_addr
& 0xff));
288 if (command
== NAND_CMD_READ0
||
289 command
== NAND_CMD_READ1
||
290 command
== NAND_CMD_READOOB
) {
292 * NAND controller will release -CE after
293 * the last address byte is written, so we'll
294 * have to forcibly assert it. No interrupts
295 * are allowed while we do this as we don't
296 * want the NOR flash or PCMCIA drivers to
297 * steal our precious bytes of data...
300 local_irq_save(flags
);
301 au1550_hwcontrol(mtd
, NAND_CTL_SETNCE
);
304 ctx
->write_byte(this, (u8
)(page_addr
>> 8));
306 if (this->options
& NAND_ROW_ADDR_3
)
307 ctx
->write_byte(this,
308 ((page_addr
>> 16) & 0x0f));
310 /* Latch in address */
311 au1550_hwcontrol(mtd
, NAND_CTL_CLRALE
);
315 * Program and erase have their own busy handlers.
316 * Status and sequential in need no delay.
320 case NAND_CMD_PAGEPROG
:
321 case NAND_CMD_ERASE1
:
322 case NAND_CMD_ERASE2
:
324 case NAND_CMD_STATUS
:
332 case NAND_CMD_READOOB
:
333 /* Check if we're really driving -CE low (just in case) */
334 if (unlikely(!ce_override
))
337 /* Apply a short delay always to ensure that we do wait tWB. */
339 /* Wait for a chip to become ready... */
340 for (i
= this->legacy
.chip_delay
;
341 !this->legacy
.dev_ready(this) && i
> 0; --i
)
344 /* Release -CE and re-enable interrupts. */
345 au1550_hwcontrol(mtd
, NAND_CTL_CLRNCE
);
346 local_irq_restore(flags
);
349 /* Apply this short delay always to ensure that we do wait tWB. */
352 while(!this->legacy
.dev_ready(this));
355 static int find_nand_cs(unsigned long nand_base
)
358 (void __iomem
*)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR
);
359 unsigned long addr
, staddr
, start
, mask
, end
;
362 for (i
= 0; i
< 4; i
++) {
363 addr
= 0x1000 + (i
* 0x10); /* CSx */
364 staddr
= __raw_readl(base
+ addr
+ 0x08); /* STADDRx */
365 /* figure out the decoded range of this CS */
366 start
= (staddr
<< 4) & 0xfffc0000;
367 mask
= (staddr
<< 18) & 0xfffc0000;
368 end
= (start
| (start
- 1)) & ~(start
^ mask
);
369 if ((nand_base
>= start
) && (nand_base
< end
))
376 static int au1550nd_probe(struct platform_device
*pdev
)
378 struct au1550nd_platdata
*pd
;
379 struct au1550nd_ctx
*ctx
;
380 struct nand_chip
*this;
381 struct mtd_info
*mtd
;
385 pd
= dev_get_platdata(&pdev
->dev
);
387 dev_err(&pdev
->dev
, "missing platform data\n");
391 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
395 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
397 dev_err(&pdev
->dev
, "no NAND memory resource\n");
401 if (request_mem_region(r
->start
, resource_size(r
), "au1550-nand")) {
402 dev_err(&pdev
->dev
, "cannot claim NAND memory area\n");
407 ctx
->base
= ioremap(r
->start
, 0x1000);
409 dev_err(&pdev
->dev
, "cannot remap NAND memory area\n");
415 mtd
= nand_to_mtd(this);
416 mtd
->dev
.parent
= &pdev
->dev
;
418 /* figure out which CS# r->start belongs to */
419 cs
= find_nand_cs(r
->start
);
421 dev_err(&pdev
->dev
, "cannot detect NAND chipselect\n");
427 this->legacy
.dev_ready
= au1550_device_ready
;
428 this->legacy
.select_chip
= au1550_select_chip
;
429 this->legacy
.cmdfunc
= au1550_command
;
431 /* 30 us command delay time */
432 this->legacy
.chip_delay
= 30;
433 this->ecc
.mode
= NAND_ECC_SOFT
;
434 this->ecc
.algo
= NAND_ECC_HAMMING
;
437 this->options
|= NAND_BUSWIDTH_16
;
439 this->legacy
.read_byte
= (pd
->devwidth
) ? au_read_byte16
: au_read_byte
;
440 ctx
->write_byte
= (pd
->devwidth
) ? au_write_byte16
: au_write_byte
;
441 this->legacy
.write_buf
= (pd
->devwidth
) ? au_write_buf16
: au_write_buf
;
442 this->legacy
.read_buf
= (pd
->devwidth
) ? au_read_buf16
: au_read_buf
;
444 ret
= nand_scan(this, 1);
446 dev_err(&pdev
->dev
, "NAND scan failed with %d\n", ret
);
450 mtd_device_register(mtd
, pd
->parts
, pd
->num_parts
);
452 platform_set_drvdata(pdev
, ctx
);
459 release_mem_region(r
->start
, resource_size(r
));
465 static int au1550nd_remove(struct platform_device
*pdev
)
467 struct au1550nd_ctx
*ctx
= platform_get_drvdata(pdev
);
468 struct resource
*r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
470 nand_release(&ctx
->chip
);
472 release_mem_region(r
->start
, 0x1000);
477 static struct platform_driver au1550nd_driver
= {
479 .name
= "au1550-nand",
481 .probe
= au1550nd_probe
,
482 .remove
= au1550nd_remove
,
485 module_platform_driver(au1550nd_driver
);
487 MODULE_LICENSE("GPL");
488 MODULE_AUTHOR("Embedded Edge, LLC");
489 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");