Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 1 | /* |
| 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 Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/mtd/mtd.h> |
| 28 | #include <linux/mtd/nand.h> |
| 29 | #include <linux/mtd/nand_ecc.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 30 | #include <linux/of_address.h> |
| 31 | #include <linux/of_irq.h> |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 32 | #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 | |
| 42 | static unsigned int lpcctl; |
| 43 | static struct mtd_info *pasemi_nand_mtd; |
| 44 | static const char driver_name[] = "pasemi-nand"; |
| 45 | |
| 46 | static void pasemi_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 47 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 48 | struct nand_chip *chip = mtd_to_nand(mtd); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 49 | |
| 50 | while (len > 0x800) { |
| 51 | memcpy_fromio(buf, chip->IO_ADDR_R, 0x800); |
| 52 | buf += 0x800; |
| 53 | len -= 0x800; |
| 54 | } |
| 55 | memcpy_fromio(buf, chip->IO_ADDR_R, len); |
| 56 | } |
| 57 | |
| 58 | static void pasemi_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 59 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 60 | struct nand_chip *chip = mtd_to_nand(mtd); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 61 | |
| 62 | while (len > 0x800) { |
| 63 | memcpy_toio(chip->IO_ADDR_R, buf, 0x800); |
| 64 | buf += 0x800; |
| 65 | len -= 0x800; |
| 66 | } |
| 67 | memcpy_toio(chip->IO_ADDR_R, buf, len); |
| 68 | } |
| 69 | |
| 70 | static void pasemi_hwcontrol(struct mtd_info *mtd, int cmd, |
| 71 | unsigned int ctrl) |
| 72 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 73 | struct nand_chip *chip = mtd_to_nand(mtd); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 74 | |
| 75 | if (cmd == NAND_CMD_NONE) |
| 76 | return; |
| 77 | |
| 78 | if (ctrl & NAND_CLE) |
| 79 | out_8(chip->IO_ADDR_W + (1 << CLE_PIN_CTL), cmd); |
| 80 | else |
| 81 | out_8(chip->IO_ADDR_W + (1 << ALE_PIN_CTL), cmd); |
| 82 | |
| 83 | /* Push out posted writes */ |
| 84 | eieio(); |
| 85 | inl(lpcctl); |
| 86 | } |
| 87 | |
| 88 | int pasemi_device_ready(struct mtd_info *mtd) |
| 89 | { |
| 90 | return !!(inl(lpcctl) & LBICTRL_LPCCTL_NR); |
| 91 | } |
| 92 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 93 | static int pasemi_nand_probe(struct platform_device *ofdev) |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 94 | { |
| 95 | struct pci_dev *pdev; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 96 | struct device_node *np = ofdev->dev.of_node; |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 97 | struct resource res; |
| 98 | struct nand_chip *chip; |
| 99 | int err = 0; |
| 100 | |
| 101 | err = of_address_to_resource(np, 0, &res); |
| 102 | |
| 103 | if (err) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | /* We only support one device at the moment */ |
| 107 | if (pasemi_nand_mtd) |
| 108 | return -ENODEV; |
| 109 | |
Joe Perches | 23079f9 | 2010-11-12 13:37:58 -0800 | [diff] [blame] | 110 | pr_debug("pasemi_nand at %pR\n", &res); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 111 | |
| 112 | /* Allocate memory for MTD device structure and private data */ |
Boris BREZILLON | 4e3b6d1 | 2015-12-10 09:00:18 +0100 | [diff] [blame] | 113 | chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL); |
| 114 | if (!chip) { |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 115 | printk(KERN_WARNING |
| 116 | "Unable to allocate PASEMI NAND MTD device structure\n"); |
| 117 | err = -ENOMEM; |
| 118 | goto out; |
| 119 | } |
| 120 | |
Boris BREZILLON | 4e3b6d1 | 2015-12-10 09:00:18 +0100 | [diff] [blame] | 121 | pasemi_nand_mtd = nand_to_mtd(chip); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 122 | |
| 123 | /* Link the private data with the MTD structure */ |
Frans Klaver | 9e86508 | 2015-06-10 22:38:59 +0200 | [diff] [blame] | 124 | pasemi_nand_mtd->dev.parent = &ofdev->dev; |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 125 | |
| 126 | chip->IO_ADDR_R = of_iomap(np, 0); |
| 127 | chip->IO_ADDR_W = chip->IO_ADDR_R; |
| 128 | |
| 129 | if (!chip->IO_ADDR_R) { |
| 130 | err = -EIO; |
| 131 | goto out_mtd; |
| 132 | } |
| 133 | |
| 134 | pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa008, NULL); |
| 135 | if (!pdev) { |
| 136 | err = -ENODEV; |
| 137 | goto out_ior; |
| 138 | } |
| 139 | |
| 140 | lpcctl = pci_resource_start(pdev, 0); |
Julia Lawall | d947629 | 2008-12-01 23:00:55 +0100 | [diff] [blame] | 141 | pci_dev_put(pdev); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 142 | |
| 143 | if (!request_region(lpcctl, 4, driver_name)) { |
| 144 | err = -EBUSY; |
| 145 | goto out_ior; |
| 146 | } |
| 147 | |
| 148 | chip->cmd_ctrl = pasemi_hwcontrol; |
| 149 | chip->dev_ready = pasemi_device_ready; |
| 150 | chip->read_buf = pasemi_read_buf; |
| 151 | chip->write_buf = pasemi_write_buf; |
| 152 | chip->chip_delay = 0; |
| 153 | chip->ecc.mode = NAND_ECC_SOFT; |
| 154 | |
| 155 | /* Enable the following for a flash based bad block table */ |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 156 | chip->bbt_options = NAND_BBT_USE_FLASH; |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 157 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 158 | /* Scan to find existence of the device */ |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 159 | if (nand_scan(pasemi_nand_mtd, 1)) { |
| 160 | err = -ENXIO; |
| 161 | goto out_lpc; |
| 162 | } |
| 163 | |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 164 | if (mtd_device_register(pasemi_nand_mtd, NULL, 0)) { |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 165 | printk(KERN_ERR "pasemi_nand: Unable to register MTD device\n"); |
| 166 | err = -ENODEV; |
| 167 | goto out_lpc; |
| 168 | } |
| 169 | |
Stephen Rothwell | 4712fff | 2009-01-21 13:16:28 +0000 | [diff] [blame] | 170 | printk(KERN_INFO "PA Semi NAND flash at %08llx, control at I/O %x\n", |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 171 | res.start, lpcctl); |
| 172 | |
| 173 | return 0; |
| 174 | |
| 175 | out_lpc: |
| 176 | release_region(lpcctl, 4); |
| 177 | out_ior: |
| 178 | iounmap(chip->IO_ADDR_R); |
| 179 | out_mtd: |
Boris BREZILLON | 4e3b6d1 | 2015-12-10 09:00:18 +0100 | [diff] [blame] | 180 | kfree(chip); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 181 | out: |
| 182 | return err; |
| 183 | } |
| 184 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 185 | static int pasemi_nand_remove(struct platform_device *ofdev) |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 186 | { |
| 187 | struct nand_chip *chip; |
| 188 | |
| 189 | if (!pasemi_nand_mtd) |
| 190 | return 0; |
| 191 | |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 192 | chip = mtd_to_nand(pasemi_nand_mtd); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 193 | |
| 194 | /* Release resources, unregister device */ |
| 195 | nand_release(pasemi_nand_mtd); |
| 196 | |
| 197 | release_region(lpcctl, 4); |
| 198 | |
| 199 | iounmap(chip->IO_ADDR_R); |
| 200 | |
| 201 | /* Free the MTD device structure */ |
Boris BREZILLON | 4e3b6d1 | 2015-12-10 09:00:18 +0100 | [diff] [blame] | 202 | kfree(chip); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 203 | |
| 204 | pasemi_nand_mtd = NULL; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Márton Németh | b2d4fba | 2010-01-09 15:10:46 +0100 | [diff] [blame] | 209 | static const struct of_device_id pasemi_nand_match[] = |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 210 | { |
| 211 | { |
| 212 | .compatible = "pasemi,localbus-nand", |
| 213 | }, |
| 214 | {}, |
| 215 | }; |
| 216 | |
| 217 | MODULE_DEVICE_TABLE(of, pasemi_nand_match); |
| 218 | |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 219 | static struct platform_driver pasemi_nand_driver = |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 220 | { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 221 | .driver = { |
Geert Uytterhoeven | 8ca14e1 | 2013-11-12 20:07:16 +0100 | [diff] [blame] | 222 | .name = driver_name, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 223 | .of_match_table = pasemi_nand_match, |
| 224 | }, |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 225 | .probe = pasemi_nand_probe, |
| 226 | .remove = pasemi_nand_remove, |
| 227 | }; |
| 228 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 229 | module_platform_driver(pasemi_nand_driver); |
Egor Martovetsky | 846fc31 | 2007-11-28 18:37:31 -0600 | [diff] [blame] | 230 | |
| 231 | MODULE_LICENSE("GPL"); |
| 232 | MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>"); |
| 233 | MODULE_DESCRIPTION("NAND flash interface driver for PA Semi PWRficient"); |