xref: /openbmc/linux/drivers/mtd/nand/raw/au1550nd.c (revision 46a73e9e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 2004 Embedded Edge, LLC
4  */
5 
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>
13 #include <asm/io.h>
14 #include <asm/mach-au1x00/au1000.h>
15 #include <asm/mach-au1x00/au1550nd.h>
16 
17 
18 struct au1550nd_ctx {
19 	struct nand_chip chip;
20 
21 	int cs;
22 	void __iomem *base;
23 	void (*write_byte)(struct nand_chip *, u_char);
24 };
25 
26 /**
27  * au_read_byte -  read one byte from the chip
28  * @this:	NAND chip object
29  *
30  * read function for 8bit buswidth
31  */
32 static u_char au_read_byte(struct nand_chip *this)
33 {
34 	u_char ret = readb(this->legacy.IO_ADDR_R);
35 	wmb(); /* drain writebuffer */
36 	return ret;
37 }
38 
39 /**
40  * au_write_byte -  write one byte to the chip
41  * @this:	NAND chip object
42  * @byte:	pointer to data byte to write
43  *
44  * write function for 8it buswidth
45  */
46 static void au_write_byte(struct nand_chip *this, u_char byte)
47 {
48 	writeb(byte, this->legacy.IO_ADDR_W);
49 	wmb(); /* drain writebuffer */
50 }
51 
52 /**
53  * au_read_byte16 -  read one byte endianness aware from the chip
54  * @this:	NAND chip object
55  *
56  * read function for 16bit buswidth with endianness conversion
57  */
58 static u_char au_read_byte16(struct nand_chip *this)
59 {
60 	u_char ret = (u_char) cpu_to_le16(readw(this->legacy.IO_ADDR_R));
61 	wmb(); /* drain writebuffer */
62 	return ret;
63 }
64 
65 /**
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
69  *
70  * write function for 16bit buswidth with endianness conversion
71  */
72 static void au_write_byte16(struct nand_chip *this, u_char byte)
73 {
74 	writew(le16_to_cpu((u16) byte), this->legacy.IO_ADDR_W);
75 	wmb(); /* drain writebuffer */
76 }
77 
78 /**
79  * au_write_buf -  write buffer to chip
80  * @this:	NAND chip object
81  * @buf:	data buffer
82  * @len:	number of bytes to write
83  *
84  * write function for 8bit buswidth
85  */
86 static void au_write_buf(struct nand_chip *this, const u_char *buf, int len)
87 {
88 	int i;
89 
90 	for (i = 0; i < len; i++) {
91 		writeb(buf[i], this->legacy.IO_ADDR_W);
92 		wmb(); /* drain writebuffer */
93 	}
94 }
95 
96 /**
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
101  *
102  * read function for 8bit buswidth
103  */
104 static void au_read_buf(struct nand_chip *this, u_char *buf, int len)
105 {
106 	int i;
107 
108 	for (i = 0; i < len; i++) {
109 		buf[i] = readb(this->legacy.IO_ADDR_R);
110 		wmb(); /* drain writebuffer */
111 	}
112 }
113 
114 /**
115  * au_write_buf16 -  write buffer to chip
116  * @this:	NAND chip object
117  * @buf:	data buffer
118  * @len:	number of bytes to write
119  *
120  * write function for 16bit buswidth
121  */
122 static void au_write_buf16(struct nand_chip *this, const u_char *buf, int len)
123 {
124 	int i;
125 	u16 *p = (u16 *) buf;
126 	len >>= 1;
127 
128 	for (i = 0; i < len; i++) {
129 		writew(p[i], this->legacy.IO_ADDR_W);
130 		wmb(); /* drain writebuffer */
131 	}
132 
133 }
134 
135 /**
136  * au_read_buf16 -  read chip data into buffer
137  * @mtd:	MTD device structure
138  * @buf:	buffer to store date
139  * @len:	number of bytes to read
140  *
141  * read function for 16bit buswidth
142  */
143 static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
144 {
145 	int i;
146 	struct nand_chip *this = mtd_to_nand(mtd);
147 	u16 *p = (u16 *) buf;
148 	len >>= 1;
149 
150 	for (i = 0; i < len; i++) {
151 		p[i] = readw(this->legacy.IO_ADDR_R);
152 		wmb(); /* drain writebuffer */
153 	}
154 }
155 
156 /* Select the chip by setting nCE to low */
157 #define NAND_CTL_SETNCE		1
158 /* Deselect the chip by setting nCE to high */
159 #define NAND_CTL_CLRNCE		2
160 /* Select the command latch by setting CLE to high */
161 #define NAND_CTL_SETCLE		3
162 /* Deselect the command latch by setting CLE to low */
163 #define NAND_CTL_CLRCLE		4
164 /* Select the address latch by setting ALE to high */
165 #define NAND_CTL_SETALE		5
166 /* Deselect the address latch by setting ALE to low */
167 #define NAND_CTL_CLRALE		6
168 
169 static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
170 {
171 	struct nand_chip *this = mtd_to_nand(mtd);
172 	struct au1550nd_ctx *ctx = container_of(this, struct au1550nd_ctx,
173 						chip);
174 
175 	switch (cmd) {
176 
177 	case NAND_CTL_SETCLE:
178 		this->legacy.IO_ADDR_W = ctx->base + MEM_STNAND_CMD;
179 		break;
180 
181 	case NAND_CTL_CLRCLE:
182 		this->legacy.IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
183 		break;
184 
185 	case NAND_CTL_SETALE:
186 		this->legacy.IO_ADDR_W = ctx->base + MEM_STNAND_ADDR;
187 		break;
188 
189 	case NAND_CTL_CLRALE:
190 		this->legacy.IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
191 		/* FIXME: Nobody knows why this is necessary,
192 		 * but it works only that way */
193 		udelay(1);
194 		break;
195 
196 	case NAND_CTL_SETNCE:
197 		/* assert (force assert) chip enable */
198 		alchemy_wrsmem((1 << (4 + ctx->cs)), AU1000_MEM_STNDCTL);
199 		break;
200 
201 	case NAND_CTL_CLRNCE:
202 		/* deassert chip enable */
203 		alchemy_wrsmem(0, AU1000_MEM_STNDCTL);
204 		break;
205 	}
206 
207 	this->legacy.IO_ADDR_R = this->legacy.IO_ADDR_W;
208 
209 	wmb(); /* Drain the writebuffer */
210 }
211 
212 int au1550_device_ready(struct nand_chip *this)
213 {
214 	return (alchemy_rdsmem(AU1000_MEM_STSTAT) & 0x1) ? 1 : 0;
215 }
216 
217 /**
218  * au1550_select_chip - control -CE line
219  *	Forbid driving -CE manually permitting the NAND controller to do this.
220  *	Keeping -CE asserted during the whole sector reads interferes with the
221  *	NOR flash and PCMCIA drivers as it causes contention on the static bus.
222  *	We only have to hold -CE low for the NAND read commands since the flash
223  *	chip needs it to be asserted during chip not ready time but the NAND
224  *	controller keeps it released.
225  *
226  * @this:	NAND chip object
227  * @chip:	chipnumber to select, -1 for deselect
228  */
229 static void au1550_select_chip(struct nand_chip *this, int chip)
230 {
231 }
232 
233 /**
234  * au1550_command - Send command to NAND device
235  * @this:	NAND chip object
236  * @command:	the command to be sent
237  * @column:	the column address for this command, -1 if none
238  * @page_addr:	the page address for this command, -1 if none
239  */
240 static void au1550_command(struct nand_chip *this, unsigned command,
241 			   int column, int page_addr)
242 {
243 	struct mtd_info *mtd = nand_to_mtd(this);
244 	struct au1550nd_ctx *ctx = container_of(this, struct au1550nd_ctx,
245 						chip);
246 	int ce_override = 0, i;
247 	unsigned long flags = 0;
248 
249 	/* Begin command latch cycle */
250 	au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
251 	/*
252 	 * Write out the command to the device.
253 	 */
254 	if (command == NAND_CMD_SEQIN) {
255 		int readcmd;
256 
257 		if (column >= mtd->writesize) {
258 			/* OOB area */
259 			column -= mtd->writesize;
260 			readcmd = NAND_CMD_READOOB;
261 		} else if (column < 256) {
262 			/* First 256 bytes --> READ0 */
263 			readcmd = NAND_CMD_READ0;
264 		} else {
265 			column -= 256;
266 			readcmd = NAND_CMD_READ1;
267 		}
268 		ctx->write_byte(this, readcmd);
269 	}
270 	ctx->write_byte(this, command);
271 
272 	/* Set ALE and clear CLE to start address cycle */
273 	au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
274 
275 	if (column != -1 || page_addr != -1) {
276 		au1550_hwcontrol(mtd, NAND_CTL_SETALE);
277 
278 		/* Serially input address */
279 		if (column != -1) {
280 			/* Adjust columns for 16 bit buswidth */
281 			if (this->options & NAND_BUSWIDTH_16 &&
282 					!nand_opcode_8bits(command))
283 				column >>= 1;
284 			ctx->write_byte(this, column);
285 		}
286 		if (page_addr != -1) {
287 			ctx->write_byte(this, (u8)(page_addr & 0xff));
288 
289 			if (command == NAND_CMD_READ0 ||
290 			    command == NAND_CMD_READ1 ||
291 			    command == NAND_CMD_READOOB) {
292 				/*
293 				 * NAND controller will release -CE after
294 				 * the last address byte is written, so we'll
295 				 * have to forcibly assert it. No interrupts
296 				 * are allowed while we do this as we don't
297 				 * want the NOR flash or PCMCIA drivers to
298 				 * steal our precious bytes of data...
299 				 */
300 				ce_override = 1;
301 				local_irq_save(flags);
302 				au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
303 			}
304 
305 			ctx->write_byte(this, (u8)(page_addr >> 8));
306 
307 			if (this->options & NAND_ROW_ADDR_3)
308 				ctx->write_byte(this,
309 						((page_addr >> 16) & 0x0f));
310 		}
311 		/* Latch in address */
312 		au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
313 	}
314 
315 	/*
316 	 * Program and erase have their own busy handlers.
317 	 * Status and sequential in need no delay.
318 	 */
319 	switch (command) {
320 
321 	case NAND_CMD_PAGEPROG:
322 	case NAND_CMD_ERASE1:
323 	case NAND_CMD_ERASE2:
324 	case NAND_CMD_SEQIN:
325 	case NAND_CMD_STATUS:
326 		return;
327 
328 	case NAND_CMD_RESET:
329 		break;
330 
331 	case NAND_CMD_READ0:
332 	case NAND_CMD_READ1:
333 	case NAND_CMD_READOOB:
334 		/* Check if we're really driving -CE low (just in case) */
335 		if (unlikely(!ce_override))
336 			break;
337 
338 		/* Apply a short delay always to ensure that we do wait tWB. */
339 		ndelay(100);
340 		/* Wait for a chip to become ready... */
341 		for (i = this->legacy.chip_delay;
342 		     !this->legacy.dev_ready(this) && i > 0; --i)
343 			udelay(1);
344 
345 		/* Release -CE and re-enable interrupts. */
346 		au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
347 		local_irq_restore(flags);
348 		return;
349 	}
350 	/* Apply this short delay always to ensure that we do wait tWB. */
351 	ndelay(100);
352 
353 	while(!this->legacy.dev_ready(this));
354 }
355 
356 static int find_nand_cs(unsigned long nand_base)
357 {
358 	void __iomem *base =
359 			(void __iomem *)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR);
360 	unsigned long addr, staddr, start, mask, end;
361 	int i;
362 
363 	for (i = 0; i < 4; i++) {
364 		addr = 0x1000 + (i * 0x10);			/* CSx */
365 		staddr = __raw_readl(base + addr + 0x08);	/* STADDRx */
366 		/* figure out the decoded range of this CS */
367 		start = (staddr << 4) & 0xfffc0000;
368 		mask = (staddr << 18) & 0xfffc0000;
369 		end = (start | (start - 1)) & ~(start ^ mask);
370 		if ((nand_base >= start) && (nand_base < end))
371 			return i;
372 	}
373 
374 	return -ENODEV;
375 }
376 
377 static int au1550nd_probe(struct platform_device *pdev)
378 {
379 	struct au1550nd_platdata *pd;
380 	struct au1550nd_ctx *ctx;
381 	struct nand_chip *this;
382 	struct mtd_info *mtd;
383 	struct resource *r;
384 	int ret, cs;
385 
386 	pd = dev_get_platdata(&pdev->dev);
387 	if (!pd) {
388 		dev_err(&pdev->dev, "missing platform data\n");
389 		return -ENODEV;
390 	}
391 
392 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
393 	if (!ctx)
394 		return -ENOMEM;
395 
396 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
397 	if (!r) {
398 		dev_err(&pdev->dev, "no NAND memory resource\n");
399 		ret = -ENODEV;
400 		goto out1;
401 	}
402 	if (request_mem_region(r->start, resource_size(r), "au1550-nand")) {
403 		dev_err(&pdev->dev, "cannot claim NAND memory area\n");
404 		ret = -ENOMEM;
405 		goto out1;
406 	}
407 
408 	ctx->base = ioremap_nocache(r->start, 0x1000);
409 	if (!ctx->base) {
410 		dev_err(&pdev->dev, "cannot remap NAND memory area\n");
411 		ret = -ENODEV;
412 		goto out2;
413 	}
414 
415 	this = &ctx->chip;
416 	mtd = nand_to_mtd(this);
417 	mtd->dev.parent = &pdev->dev;
418 
419 	/* figure out which CS# r->start belongs to */
420 	cs = find_nand_cs(r->start);
421 	if (cs < 0) {
422 		dev_err(&pdev->dev, "cannot detect NAND chipselect\n");
423 		ret = -ENODEV;
424 		goto out3;
425 	}
426 	ctx->cs = cs;
427 
428 	this->legacy.dev_ready = au1550_device_ready;
429 	this->legacy.select_chip = au1550_select_chip;
430 	this->legacy.cmdfunc = au1550_command;
431 
432 	/* 30 us command delay time */
433 	this->legacy.chip_delay = 30;
434 	this->ecc.mode = NAND_ECC_SOFT;
435 	this->ecc.algo = NAND_ECC_HAMMING;
436 
437 	if (pd->devwidth)
438 		this->options |= NAND_BUSWIDTH_16;
439 
440 	this->legacy.read_byte = (pd->devwidth) ? au_read_byte16 : au_read_byte;
441 	ctx->write_byte = (pd->devwidth) ? au_write_byte16 : au_write_byte;
442 	this->legacy.write_buf = (pd->devwidth) ? au_write_buf16 : au_write_buf;
443 	this->legacy.read_buf = (pd->devwidth) ? au_read_buf16 : au_read_buf;
444 
445 	ret = nand_scan(this, 1);
446 	if (ret) {
447 		dev_err(&pdev->dev, "NAND scan failed with %d\n", ret);
448 		goto out3;
449 	}
450 
451 	mtd_device_register(mtd, pd->parts, pd->num_parts);
452 
453 	platform_set_drvdata(pdev, ctx);
454 
455 	return 0;
456 
457 out3:
458 	iounmap(ctx->base);
459 out2:
460 	release_mem_region(r->start, resource_size(r));
461 out1:
462 	kfree(ctx);
463 	return ret;
464 }
465 
466 static int au1550nd_remove(struct platform_device *pdev)
467 {
468 	struct au1550nd_ctx *ctx = platform_get_drvdata(pdev);
469 	struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
470 
471 	nand_release(&ctx->chip);
472 	iounmap(ctx->base);
473 	release_mem_region(r->start, 0x1000);
474 	kfree(ctx);
475 	return 0;
476 }
477 
478 static struct platform_driver au1550nd_driver = {
479 	.driver = {
480 		.name	= "au1550-nand",
481 	},
482 	.probe		= au1550nd_probe,
483 	.remove		= au1550nd_remove,
484 };
485 
486 module_platform_driver(au1550nd_driver);
487 
488 MODULE_LICENSE("GPL");
489 MODULE_AUTHOR("Embedded Edge, LLC");
490 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");
491