blob: 643cd22af0093c07e4e6eb6cda70bf1261fddecd [file] [log] [blame]
Egor Martovetsky846fc312007-11-28 18:37:31 -06001/*
2 * Copyright (C) 2006-2007 PA Semi, Inc
3 *
4 * Author: Egor Martovetsky <egor@pasemi.com>
5 * Maintained by: Olof Johansson <olof@lixom.net>
6 *
7 * Driver for the PWRficient onchip NAND flash interface
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#undef DEBUG
24
25#include <linux/slab.h>
Egor Martovetsky846fc312007-11-28 18:37:31 -060026#include <linux/module.h>
27#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020028#include <linux/mtd/rawnand.h>
Egor Martovetsky846fc312007-11-28 18:37:31 -060029#include <linux/mtd/nand_ecc.h>
Rob Herring5af50732013-09-17 14:28:33 -050030#include <linux/of_address.h>
31#include <linux/of_irq.h>
Egor Martovetsky846fc312007-11-28 18:37:31 -060032#include <linux/of_platform.h>
33#include <linux/platform_device.h>
34#include <linux/pci.h>
35
36#include <asm/io.h>
37
38#define LBICTRL_LPCCTL_NR 0x00004000
39#define CLE_PIN_CTL 15
40#define ALE_PIN_CTL 14
41
42static unsigned int lpcctl;
43static struct mtd_info *pasemi_nand_mtd;
44static const char driver_name[] = "pasemi-nand";
45
Boris Brezillon7e534322018-09-06 14:05:22 +020046static void pasemi_read_buf(struct nand_chip *chip, u_char *buf, int len)
Egor Martovetsky846fc312007-11-28 18:37:31 -060047{
Egor Martovetsky846fc312007-11-28 18:37:31 -060048 while (len > 0x800) {
Boris Brezillon82fc5092018-09-07 00:38:34 +020049 memcpy_fromio(buf, chip->legacy.IO_ADDR_R, 0x800);
Egor Martovetsky846fc312007-11-28 18:37:31 -060050 buf += 0x800;
51 len -= 0x800;
52 }
Boris Brezillon82fc5092018-09-07 00:38:34 +020053 memcpy_fromio(buf, chip->legacy.IO_ADDR_R, len);
Egor Martovetsky846fc312007-11-28 18:37:31 -060054}
55
Boris Brezillonc0739d82018-09-06 14:05:23 +020056static void pasemi_write_buf(struct nand_chip *chip, const u_char *buf,
57 int len)
Egor Martovetsky846fc312007-11-28 18:37:31 -060058{
Egor Martovetsky846fc312007-11-28 18:37:31 -060059 while (len > 0x800) {
Boris Brezillon82fc5092018-09-07 00:38:34 +020060 memcpy_toio(chip->legacy.IO_ADDR_R, buf, 0x800);
Egor Martovetsky846fc312007-11-28 18:37:31 -060061 buf += 0x800;
62 len -= 0x800;
63 }
Boris Brezillon82fc5092018-09-07 00:38:34 +020064 memcpy_toio(chip->legacy.IO_ADDR_R, buf, len);
Egor Martovetsky846fc312007-11-28 18:37:31 -060065}
66
Boris Brezillon0f808c12018-09-06 14:05:26 +020067static void pasemi_hwcontrol(struct nand_chip *chip, int cmd,
Egor Martovetsky846fc312007-11-28 18:37:31 -060068 unsigned int ctrl)
69{
Egor Martovetsky846fc312007-11-28 18:37:31 -060070 if (cmd == NAND_CMD_NONE)
71 return;
72
73 if (ctrl & NAND_CLE)
Boris Brezillon82fc5092018-09-07 00:38:34 +020074 out_8(chip->legacy.IO_ADDR_W + (1 << CLE_PIN_CTL), cmd);
Egor Martovetsky846fc312007-11-28 18:37:31 -060075 else
Boris Brezillon82fc5092018-09-07 00:38:34 +020076 out_8(chip->legacy.IO_ADDR_W + (1 << ALE_PIN_CTL), cmd);
Egor Martovetsky846fc312007-11-28 18:37:31 -060077
78 /* Push out posted writes */
79 eieio();
80 inl(lpcctl);
81}
82
Boris Brezillon50a487e2018-09-06 14:05:27 +020083int pasemi_device_ready(struct nand_chip *chip)
Egor Martovetsky846fc312007-11-28 18:37:31 -060084{
85 return !!(inl(lpcctl) & LBICTRL_LPCCTL_NR);
86}
87
Bill Pemberton06f25512012-11-19 13:23:07 -050088static int pasemi_nand_probe(struct platform_device *ofdev)
Egor Martovetsky846fc312007-11-28 18:37:31 -060089{
Rafał Miłeckiff05fdb2016-04-13 11:48:05 +020090 struct device *dev = &ofdev->dev;
Egor Martovetsky846fc312007-11-28 18:37:31 -060091 struct pci_dev *pdev;
Rafał Miłeckiff05fdb2016-04-13 11:48:05 +020092 struct device_node *np = dev->of_node;
Egor Martovetsky846fc312007-11-28 18:37:31 -060093 struct resource res;
94 struct nand_chip *chip;
95 int err = 0;
96
97 err = of_address_to_resource(np, 0, &res);
98
99 if (err)
100 return -EINVAL;
101
102 /* We only support one device at the moment */
103 if (pasemi_nand_mtd)
104 return -ENODEV;
105
Rafał Miłeckiff05fdb2016-04-13 11:48:05 +0200106 dev_dbg(dev, "pasemi_nand at %pR\n", &res);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600107
108 /* Allocate memory for MTD device structure and private data */
Boris BREZILLON4e3b6d12015-12-10 09:00:18 +0100109 chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
110 if (!chip) {
Egor Martovetsky846fc312007-11-28 18:37:31 -0600111 err = -ENOMEM;
112 goto out;
113 }
114
Boris BREZILLON4e3b6d12015-12-10 09:00:18 +0100115 pasemi_nand_mtd = nand_to_mtd(chip);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600116
117 /* Link the private data with the MTD structure */
Rafał Miłeckiff05fdb2016-04-13 11:48:05 +0200118 pasemi_nand_mtd->dev.parent = dev;
Egor Martovetsky846fc312007-11-28 18:37:31 -0600119
Boris Brezillon82fc5092018-09-07 00:38:34 +0200120 chip->legacy.IO_ADDR_R = of_iomap(np, 0);
121 chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
Egor Martovetsky846fc312007-11-28 18:37:31 -0600122
Boris Brezillon82fc5092018-09-07 00:38:34 +0200123 if (!chip->legacy.IO_ADDR_R) {
Egor Martovetsky846fc312007-11-28 18:37:31 -0600124 err = -EIO;
125 goto out_mtd;
126 }
127
128 pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa008, NULL);
129 if (!pdev) {
130 err = -ENODEV;
131 goto out_ior;
132 }
133
134 lpcctl = pci_resource_start(pdev, 0);
Julia Lawalld9476292008-12-01 23:00:55 +0100135 pci_dev_put(pdev);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600136
137 if (!request_region(lpcctl, 4, driver_name)) {
138 err = -EBUSY;
139 goto out_ior;
140 }
141
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200142 chip->legacy.cmd_ctrl = pasemi_hwcontrol;
Boris Brezillon8395b752018-09-07 00:38:37 +0200143 chip->legacy.dev_ready = pasemi_device_ready;
Boris Brezillon716bbba2018-09-07 00:38:35 +0200144 chip->legacy.read_buf = pasemi_read_buf;
145 chip->legacy.write_buf = pasemi_write_buf;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200146 chip->legacy.chip_delay = 0;
Egor Martovetsky846fc312007-11-28 18:37:31 -0600147 chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckia9670a92016-04-08 12:23:49 +0200148 chip->ecc.algo = NAND_ECC_HAMMING;
Egor Martovetsky846fc312007-11-28 18:37:31 -0600149
150 /* Enable the following for a flash based bad block table */
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -0700151 chip->bbt_options = NAND_BBT_USE_FLASH;
Egor Martovetsky846fc312007-11-28 18:37:31 -0600152
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300153 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200154 err = nand_scan(chip, 1);
Masahiro Yamada4e5af27e22016-11-04 19:42:56 +0900155 if (err)
Egor Martovetsky846fc312007-11-28 18:37:31 -0600156 goto out_lpc;
Egor Martovetsky846fc312007-11-28 18:37:31 -0600157
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100158 if (mtd_device_register(pasemi_nand_mtd, NULL, 0)) {
Rafał Miłeckiff05fdb2016-04-13 11:48:05 +0200159 dev_err(dev, "Unable to register MTD device\n");
Egor Martovetsky846fc312007-11-28 18:37:31 -0600160 err = -ENODEV;
161 goto out_lpc;
162 }
163
Rafał Miłeckiff05fdb2016-04-13 11:48:05 +0200164 dev_info(dev, "PA Semi NAND flash at %pR, control at I/O %x\n", &res,
165 lpcctl);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600166
167 return 0;
168
169 out_lpc:
170 release_region(lpcctl, 4);
171 out_ior:
Boris Brezillon82fc5092018-09-07 00:38:34 +0200172 iounmap(chip->legacy.IO_ADDR_R);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600173 out_mtd:
Boris BREZILLON4e3b6d12015-12-10 09:00:18 +0100174 kfree(chip);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600175 out:
176 return err;
177}
178
Bill Pemberton810b7e02012-11-19 13:26:04 -0500179static int pasemi_nand_remove(struct platform_device *ofdev)
Egor Martovetsky846fc312007-11-28 18:37:31 -0600180{
181 struct nand_chip *chip;
182
183 if (!pasemi_nand_mtd)
184 return 0;
185
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100186 chip = mtd_to_nand(pasemi_nand_mtd);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600187
188 /* Release resources, unregister device */
Boris Brezillon59ac2762018-09-06 14:05:15 +0200189 nand_release(chip);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600190
191 release_region(lpcctl, 4);
192
Boris Brezillon82fc5092018-09-07 00:38:34 +0200193 iounmap(chip->legacy.IO_ADDR_R);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600194
195 /* Free the MTD device structure */
Boris BREZILLON4e3b6d12015-12-10 09:00:18 +0100196 kfree(chip);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600197
198 pasemi_nand_mtd = NULL;
199
200 return 0;
201}
202
Márton Némethb2d4fba2010-01-09 15:10:46 +0100203static const struct of_device_id pasemi_nand_match[] =
Egor Martovetsky846fc312007-11-28 18:37:31 -0600204{
205 {
206 .compatible = "pasemi,localbus-nand",
207 },
208 {},
209};
210
211MODULE_DEVICE_TABLE(of, pasemi_nand_match);
212
Grant Likely1c48a5c2011-02-17 02:43:24 -0700213static struct platform_driver pasemi_nand_driver =
Egor Martovetsky846fc312007-11-28 18:37:31 -0600214{
Grant Likely40182942010-04-13 16:13:02 -0700215 .driver = {
Geert Uytterhoeven8ca14e12013-11-12 20:07:16 +0100216 .name = driver_name,
Grant Likely40182942010-04-13 16:13:02 -0700217 .of_match_table = pasemi_nand_match,
218 },
Egor Martovetsky846fc312007-11-28 18:37:31 -0600219 .probe = pasemi_nand_probe,
220 .remove = pasemi_nand_remove,
221};
222
Axel Linf99640d2011-11-27 20:45:03 +0800223module_platform_driver(pasemi_nand_driver);
Egor Martovetsky846fc312007-11-28 18:37:31 -0600224
225MODULE_LICENSE("GPL");
226MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
227MODULE_DESCRIPTION("NAND flash interface driver for PA Semi PWRficient");