| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * drivers/mtd/nand/socrates_nand.c | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Copyright © 2008 Ilya Yanok, Emcraft Systems | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License version 2 as | 
					
						
							|  |  |  |  * published by the Free Software Foundation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/slab.h>
 | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							|  |  |  | #include <linux/mtd/mtd.h>
 | 
					
						
							|  |  |  | #include <linux/mtd/nand.h>
 | 
					
						
							|  |  |  | #include <linux/mtd/partitions.h>
 | 
					
						
							| 
									
										
										
										
											2013-11-10 23:19:08 -06:00
										 |  |  | #include <linux/of_address.h>
 | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | #include <linux/of_platform.h>
 | 
					
						
							|  |  |  | #include <linux/io.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define FPGA_NAND_CMD_MASK		(0x7 << 28)
 | 
					
						
							|  |  |  | #define FPGA_NAND_CMD_COMMAND		(0x0 << 28)
 | 
					
						
							|  |  |  | #define FPGA_NAND_CMD_ADDR		(0x1 << 28)
 | 
					
						
							|  |  |  | #define FPGA_NAND_CMD_READ		(0x2 << 28)
 | 
					
						
							|  |  |  | #define FPGA_NAND_CMD_WRITE		(0x3 << 28)
 | 
					
						
							|  |  |  | #define FPGA_NAND_BUSY			(0x1 << 15)
 | 
					
						
							|  |  |  | #define FPGA_NAND_ENABLE		(0x1 << 31)
 | 
					
						
							|  |  |  | #define FPGA_NAND_DATA_SHIFT		16
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct socrates_nand_host { | 
					
						
							|  |  |  | 	struct nand_chip	nand_chip; | 
					
						
							|  |  |  | 	struct mtd_info		mtd; | 
					
						
							|  |  |  | 	void __iomem		*io_base; | 
					
						
							|  |  |  | 	struct device		*dev; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * socrates_nand_write_buf -  write buffer to chip | 
					
						
							|  |  |  |  * @mtd:	MTD device structure | 
					
						
							|  |  |  |  * @buf:	data buffer | 
					
						
							|  |  |  |  * @len:	number of bytes to write | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void socrates_nand_write_buf(struct mtd_info *mtd, | 
					
						
							|  |  |  | 		const uint8_t *buf, int len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	struct nand_chip *this = mtd->priv; | 
					
						
							|  |  |  | 	struct socrates_nand_host *host = this->priv; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < len; i++) { | 
					
						
							|  |  |  | 		out_be32(host->io_base, FPGA_NAND_ENABLE | | 
					
						
							|  |  |  | 				FPGA_NAND_CMD_WRITE | | 
					
						
							|  |  |  | 				(buf[i] << FPGA_NAND_DATA_SHIFT)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * socrates_nand_read_buf -  read chip data into buffer | 
					
						
							|  |  |  |  * @mtd:	MTD device structure | 
					
						
							|  |  |  |  * @buf:	buffer to store date | 
					
						
							|  |  |  |  * @len:	number of bytes to read | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	struct nand_chip *this = mtd->priv; | 
					
						
							|  |  |  | 	struct socrates_nand_host *host = this->priv; | 
					
						
							|  |  |  | 	uint32_t val; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	out_be32(host->io_base, val); | 
					
						
							|  |  |  | 	for (i = 0; i < len; i++) { | 
					
						
							|  |  |  | 		buf[i] = (in_be32(host->io_base) >> | 
					
						
							|  |  |  | 				FPGA_NAND_DATA_SHIFT) & 0xff; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * socrates_nand_read_byte -  read one byte from the chip | 
					
						
							|  |  |  |  * @mtd:	MTD device structure | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static uint8_t socrates_nand_read_byte(struct mtd_info *mtd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint8_t byte; | 
					
						
							|  |  |  | 	socrates_nand_read_buf(mtd, &byte, sizeof(byte)); | 
					
						
							|  |  |  | 	return byte; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * socrates_nand_read_word -  read one word from the chip | 
					
						
							|  |  |  |  * @mtd:	MTD device structure | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static uint16_t socrates_nand_read_word(struct mtd_info *mtd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint16_t word; | 
					
						
							|  |  |  | 	socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word)); | 
					
						
							|  |  |  | 	return word; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Hardware specific access to control-lines | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, | 
					
						
							|  |  |  | 		unsigned int ctrl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct nand_chip *nand_chip = mtd->priv; | 
					
						
							|  |  |  | 	struct socrates_nand_host *host = nand_chip->priv; | 
					
						
							|  |  |  | 	uint32_t val; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (cmd == NAND_CMD_NONE) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ctrl & NAND_CLE) | 
					
						
							|  |  |  | 		val = FPGA_NAND_CMD_COMMAND; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		val = FPGA_NAND_CMD_ADDR; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ctrl & NAND_NCE) | 
					
						
							|  |  |  | 		val |= FPGA_NAND_ENABLE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	out_be32(host->io_base, val); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Read the Device Ready pin. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int socrates_nand_device_ready(struct mtd_info *mtd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct nand_chip *nand_chip = mtd->priv; | 
					
						
							|  |  |  | 	struct socrates_nand_host *host = nand_chip->priv; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (in_be32(host->io_base) & FPGA_NAND_BUSY) | 
					
						
							|  |  |  | 		return 0; /* busy */ | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Probe for the NAND device. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-11-19 13:23:07 -05:00
										 |  |  | static int socrates_nand_probe(struct platform_device *ofdev) | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct socrates_nand_host *host; | 
					
						
							|  |  |  | 	struct mtd_info *mtd; | 
					
						
							|  |  |  | 	struct nand_chip *nand_chip; | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2011-05-30 01:02:26 +04:00
										 |  |  | 	struct mtd_part_parser_data ppdata; | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Allocate memory for the device structure (and zero it) */ | 
					
						
							| 
									
										
										
										
											2013-10-08 15:31:46 +05:30
										 |  |  | 	host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL); | 
					
						
							|  |  |  | 	if (!host) | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 		return -ENOMEM; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 02:37:17 +02:00
										 |  |  | 	host->io_base = of_iomap(ofdev->dev.of_node, 0); | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 	if (host->io_base == NULL) { | 
					
						
							| 
									
										
										
										
											2013-10-08 15:38:08 +05:30
										 |  |  | 		dev_err(&ofdev->dev, "ioremap failed\n"); | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 		return -EIO; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mtd = &host->mtd; | 
					
						
							|  |  |  | 	nand_chip = &host->nand_chip; | 
					
						
							|  |  |  | 	host->dev = &ofdev->dev; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	nand_chip->priv = host;		/* link the private data structures */ | 
					
						
							|  |  |  | 	mtd->priv = nand_chip; | 
					
						
							|  |  |  | 	mtd->name = "socrates_nand"; | 
					
						
							|  |  |  | 	mtd->owner = THIS_MODULE; | 
					
						
							|  |  |  | 	mtd->dev.parent = &ofdev->dev; | 
					
						
							| 
									
										
										
										
											2011-05-30 01:02:26 +04:00
										 |  |  | 	ppdata.of_node = ofdev->dev.of_node; | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/*should never be accessed directly */ | 
					
						
							|  |  |  | 	nand_chip->IO_ADDR_R = (void *)0xdeadbeef; | 
					
						
							|  |  |  | 	nand_chip->IO_ADDR_W = (void *)0xdeadbeef; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl; | 
					
						
							|  |  |  | 	nand_chip->read_byte = socrates_nand_read_byte; | 
					
						
							|  |  |  | 	nand_chip->read_word = socrates_nand_read_word; | 
					
						
							|  |  |  | 	nand_chip->write_buf = socrates_nand_write_buf; | 
					
						
							|  |  |  | 	nand_chip->read_buf = socrates_nand_read_buf; | 
					
						
							|  |  |  | 	nand_chip->dev_ready = socrates_nand_device_ready; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	nand_chip->ecc.mode = NAND_ECC_SOFT;	/* enable ECC */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* TODO: I have no idea what real delay is. */ | 
					
						
							|  |  |  | 	nand_chip->chip_delay = 20;		/* 20us command delay time */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dev_set_drvdata(&ofdev->dev, host); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* first scan to find the device and get the page size */ | 
					
						
							| 
									
										
										
										
											2010-02-26 18:32:56 +00:00
										 |  |  | 	if (nand_scan_ident(mtd, 1, NULL)) { | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 		res = -ENXIO; | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* second phase scan */ | 
					
						
							|  |  |  | 	if (nand_scan_tail(mtd)) { | 
					
						
							|  |  |  | 		res = -ENXIO; | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-02 18:01:07 +04:00
										 |  |  | 	res = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0); | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 	if (!res) | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	nand_release(mtd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | out: | 
					
						
							|  |  |  | 	iounmap(host->io_base); | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Remove a NAND device. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-11-19 13:26:04 -05:00
										 |  |  | static int socrates_nand_remove(struct platform_device *ofdev) | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev); | 
					
						
							|  |  |  | 	struct mtd_info *mtd = &host->mtd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	nand_release(mtd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	iounmap(host->io_base); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-09 15:10:46 +01:00
										 |  |  | static const struct of_device_id socrates_nand_match[] = | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		.compatible   = "abb,socrates-nand", | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	{}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MODULE_DEVICE_TABLE(of, socrates_nand_match); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-17 02:43:24 -07:00
										 |  |  | static struct platform_driver socrates_nand_driver = { | 
					
						
							| 
									
										
										
										
											2010-04-13 16:13:02 -07:00
										 |  |  | 	.driver = { | 
					
						
							|  |  |  | 		.name = "socrates_nand", | 
					
						
							|  |  |  | 		.owner = THIS_MODULE, | 
					
						
							|  |  |  | 		.of_match_table = socrates_nand_match, | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 	.probe		= socrates_nand_probe, | 
					
						
							| 
									
										
										
										
											2012-11-19 13:21:24 -05:00
										 |  |  | 	.remove		= socrates_nand_remove, | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 20:45:03 +08:00
										 |  |  | module_platform_driver(socrates_nand_driver); | 
					
						
							| 
									
										
										
										
											2009-03-25 11:48:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | MODULE_LICENSE("GPL"); | 
					
						
							|  |  |  | MODULE_AUTHOR("Ilya Yanok"); | 
					
						
							|  |  |  | MODULE_DESCRIPTION("NAND driver for Socrates board"); |