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> 26#include <linux/module.h> 27#include <linux/mtd/mtd.h> 28#include <linux/mtd/nand.h> 29#include <linux/mtd/nand_ecc.h> 30#include <linux/of_address.h> 31#include <linux/of_irq.h> 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 42static unsigned int lpcctl; 43static struct mtd_info *pasemi_nand_mtd; 44static const char driver_name[] = "pasemi-nand"; 45 46static void pasemi_read_buf(struct mtd_info *mtd, u_char *buf, int len) 47{ 48 struct nand_chip *chip = mtd->priv; 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 58static void pasemi_write_buf(struct mtd_info *mtd, const u_char *buf, int len) 59{ 60 struct nand_chip *chip = mtd->priv; 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 70static void pasemi_hwcontrol(struct mtd_info *mtd, int cmd, 71 unsigned int ctrl) 72{ 73 struct nand_chip *chip = mtd->priv; 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 88int pasemi_device_ready(struct mtd_info *mtd) 89{ 90 return !!(inl(lpcctl) & LBICTRL_LPCCTL_NR); 91} 92 93static int pasemi_nand_probe(struct platform_device *ofdev) 94{ 95 struct pci_dev *pdev; 96 struct device_node *np = ofdev->dev.of_node; 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 110 pr_debug("pasemi_nand at %pR\n", &res); 111 112 /* Allocate memory for MTD device structure and private data */ 113 pasemi_nand_mtd = kzalloc(sizeof(struct mtd_info) + 114 sizeof(struct nand_chip), GFP_KERNEL); 115 if (!pasemi_nand_mtd) { 116 printk(KERN_WARNING 117 "Unable to allocate PASEMI NAND MTD device structure\n"); 118 err = -ENOMEM; 119 goto out; 120 } 121 122 /* Get pointer to private data */ 123 chip = (struct nand_chip *)&pasemi_nand_mtd[1]; 124 125 /* Link the private data with the MTD structure */ 126 pasemi_nand_mtd->priv = chip; 127 pasemi_nand_mtd->owner = THIS_MODULE; 128 129 chip->IO_ADDR_R = of_iomap(np, 0); 130 chip->IO_ADDR_W = chip->IO_ADDR_R; 131 132 if (!chip->IO_ADDR_R) { 133 err = -EIO; 134 goto out_mtd; 135 } 136 137 pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa008, NULL); 138 if (!pdev) { 139 err = -ENODEV; 140 goto out_ior; 141 } 142 143 lpcctl = pci_resource_start(pdev, 0); 144 pci_dev_put(pdev); 145 146 if (!request_region(lpcctl, 4, driver_name)) { 147 err = -EBUSY; 148 goto out_ior; 149 } 150 151 chip->cmd_ctrl = pasemi_hwcontrol; 152 chip->dev_ready = pasemi_device_ready; 153 chip->read_buf = pasemi_read_buf; 154 chip->write_buf = pasemi_write_buf; 155 chip->chip_delay = 0; 156 chip->ecc.mode = NAND_ECC_SOFT; 157 158 /* Enable the following for a flash based bad block table */ 159 chip->bbt_options = NAND_BBT_USE_FLASH; 160 161 /* Scan to find existence of the device */ 162 if (nand_scan(pasemi_nand_mtd, 1)) { 163 err = -ENXIO; 164 goto out_lpc; 165 } 166 167 if (mtd_device_register(pasemi_nand_mtd, NULL, 0)) { 168 printk(KERN_ERR "pasemi_nand: Unable to register MTD device\n"); 169 err = -ENODEV; 170 goto out_lpc; 171 } 172 173 printk(KERN_INFO "PA Semi NAND flash at %08llx, control at I/O %x\n", 174 res.start, lpcctl); 175 176 return 0; 177 178 out_lpc: 179 release_region(lpcctl, 4); 180 out_ior: 181 iounmap(chip->IO_ADDR_R); 182 out_mtd: 183 kfree(pasemi_nand_mtd); 184 out: 185 return err; 186} 187 188static int pasemi_nand_remove(struct platform_device *ofdev) 189{ 190 struct nand_chip *chip; 191 192 if (!pasemi_nand_mtd) 193 return 0; 194 195 chip = pasemi_nand_mtd->priv; 196 197 /* Release resources, unregister device */ 198 nand_release(pasemi_nand_mtd); 199 200 release_region(lpcctl, 4); 201 202 iounmap(chip->IO_ADDR_R); 203 204 /* Free the MTD device structure */ 205 kfree(pasemi_nand_mtd); 206 207 pasemi_nand_mtd = NULL; 208 209 return 0; 210} 211 212static const struct of_device_id pasemi_nand_match[] = 213{ 214 { 215 .compatible = "pasemi,localbus-nand", 216 }, 217 {}, 218}; 219 220MODULE_DEVICE_TABLE(of, pasemi_nand_match); 221 222static struct platform_driver pasemi_nand_driver = 223{ 224 .driver = { 225 .name = driver_name, 226 .of_match_table = pasemi_nand_match, 227 }, 228 .probe = pasemi_nand_probe, 229 .remove = pasemi_nand_remove, 230}; 231 232module_platform_driver(pasemi_nand_driver); 233 234MODULE_LICENSE("GPL"); 235MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>"); 236MODULE_DESCRIPTION("NAND flash interface driver for PA Semi PWRficient"); 237