1/* 2 * drivers/mtd/nand/orion_nand.c 3 * 4 * NAND support for Marvell Orion SoC platforms 5 * 6 * Tzachi Perelstein <tzachi@marvell.com> 7 * 8 * This file is licensed under the terms of the GNU General Public 9 * License version 2. This program is licensed "as is" without any 10 * warranty of any kind, whether express or implied. 11 */ 12 13#include <linux/slab.h> 14#include <linux/module.h> 15#include <linux/platform_device.h> 16#include <linux/mtd/mtd.h> 17#include <linux/mtd/nand.h> 18#include <linux/mtd/partitions.h> 19#include <asm/io.h> 20#include <asm/sizes.h> 21#include <mach/hardware.h> 22#include <plat/orion_nand.h> 23 24#ifdef CONFIG_MTD_CMDLINE_PARTS 25static const char *part_probes[] = { "cmdlinepart", NULL }; 26#endif 27 28static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl) 29{ 30 struct nand_chip *nc = mtd->priv; 31 struct orion_nand_data *board = nc->priv; 32 u32 offs; 33 34 if (cmd == NAND_CMD_NONE) 35 return; 36 37 if (ctrl & NAND_CLE) 38 offs = (1 << board->cle); 39 else if (ctrl & NAND_ALE) 40 offs = (1 << board->ale); 41 else 42 return; 43 44 if (nc->options & NAND_BUSWIDTH_16) 45 offs <<= 1; 46 47 writeb(cmd, nc->IO_ADDR_W + offs); 48} 49 50static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) 51{ 52 struct nand_chip *chip = mtd->priv; 53 void __iomem *io_base = chip->IO_ADDR_R; 54 uint64_t *buf64; 55 int i = 0; 56 57 while (len && (unsigned long)buf & 7) { 58 *buf++ = readb(io_base); 59 len--; 60 } 61 buf64 = (uint64_t *)buf; 62 while (i < len/8) { 63 register uint64_t x asm ("r2"); 64 65 asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base)); 66 buf64[i++] = x; 67 } 68 i *= 8; 69 while (i < len) 70 buf[i++] = readb(io_base); 71} 72 73static int __init orion_nand_probe(struct platform_device *pdev) 74{ 75 struct mtd_info *mtd; 76 struct nand_chip *nc; 77 struct orion_nand_data *board; 78 struct resource *res; 79 void __iomem *io_base; 80 int ret = 0; 81#ifdef CONFIG_MTD_PARTITIONS 82 struct mtd_partition *partitions = NULL; 83 int num_part = 0; 84#endif 85 86 nc = kzalloc(sizeof(struct nand_chip) + sizeof(struct mtd_info), GFP_KERNEL); 87 if (!nc) { 88 printk(KERN_ERR "orion_nand: failed to allocate device structure.\n"); 89 ret = -ENOMEM; 90 goto no_res; 91 } 92 mtd = (struct mtd_info *)(nc + 1); 93 94 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 95 if (!res) { 96 ret = -ENODEV; 97 goto no_res; 98 } 99 100 io_base = ioremap(res->start, resource_size(res)); 101 if (!io_base) { 102 printk(KERN_ERR "orion_nand: ioremap failed\n"); 103 ret = -EIO; 104 goto no_res; 105 } 106 107 board = pdev->dev.platform_data; 108 109 mtd->priv = nc; 110 mtd->owner = THIS_MODULE; 111 112 nc->priv = board; 113 nc->IO_ADDR_R = nc->IO_ADDR_W = io_base; 114 nc->cmd_ctrl = orion_nand_cmd_ctrl; 115 nc->read_buf = orion_nand_read_buf; 116 nc->ecc.mode = NAND_ECC_SOFT; 117 118 if (board->chip_delay) 119 nc->chip_delay = board->chip_delay; 120 121 if (board->width == 16) 122 nc->options |= NAND_BUSWIDTH_16; 123 124 if (board->dev_ready) 125 nc->dev_ready = board->dev_ready; 126 127 platform_set_drvdata(pdev, mtd); 128 129 if (nand_scan(mtd, 1)) { 130 ret = -ENXIO; 131 goto no_dev; 132 } 133 134#ifdef CONFIG_MTD_PARTITIONS 135#ifdef CONFIG_MTD_CMDLINE_PARTS 136 mtd->name = "orion_nand"; 137 num_part = parse_mtd_partitions(mtd, part_probes, &partitions, 0); 138#endif 139 /* If cmdline partitions have been passed, let them be used */ 140 if (num_part <= 0) { 141 num_part = board->nr_parts; 142 partitions = board->parts; 143 } 144 145 if (partitions && num_part > 0) 146 ret = add_mtd_partitions(mtd, partitions, num_part); 147 else 148 ret = add_mtd_device(mtd); 149#else 150 ret = add_mtd_device(mtd); 151#endif 152 153 if (ret) { 154 nand_release(mtd); 155 goto no_dev; 156 } 157 158 return 0; 159 160no_dev: 161 platform_set_drvdata(pdev, NULL); 162 iounmap(io_base); 163no_res: 164 kfree(nc); 165 166 return ret; 167} 168 169static int __devexit orion_nand_remove(struct platform_device *pdev) 170{ 171 struct mtd_info *mtd = platform_get_drvdata(pdev); 172 struct nand_chip *nc = mtd->priv; 173 174 nand_release(mtd); 175 176 iounmap(nc->IO_ADDR_W); 177 178 kfree(nc); 179 180 return 0; 181} 182 183static struct platform_driver orion_nand_driver = { 184 .remove = __devexit_p(orion_nand_remove), 185 .driver = { 186 .name = "orion_nand", 187 .owner = THIS_MODULE, 188 }, 189}; 190 191static int __init orion_nand_init(void) 192{ 193 return platform_driver_probe(&orion_nand_driver, orion_nand_probe); 194} 195 196static void __exit orion_nand_exit(void) 197{ 198 platform_driver_unregister(&orion_nand_driver); 199} 200 201module_init(orion_nand_init); 202module_exit(orion_nand_exit); 203 204MODULE_LICENSE("GPL"); 205MODULE_AUTHOR("Tzachi Perelstein"); 206MODULE_DESCRIPTION("NAND glue for Orion platforms"); 207MODULE_ALIAS("platform:orion_nand"); 208