1 /* 2 * NAND support for Marvell Orion SoC platforms 3 * 4 * Tzachi Perelstein <tzachi@marvell.com> 5 * 6 * This file is licensed under the terms of the GNU General Public 7 * License version 2. This program is licensed "as is" without any 8 * warranty of any kind, whether express or implied. 9 */ 10 11 #include <linux/slab.h> 12 #include <linux/module.h> 13 #include <linux/platform_device.h> 14 #include <linux/of.h> 15 #include <linux/mtd/mtd.h> 16 #include <linux/mtd/rawnand.h> 17 #include <linux/mtd/partitions.h> 18 #include <linux/clk.h> 19 #include <linux/err.h> 20 #include <linux/io.h> 21 #include <linux/sizes.h> 22 #include <linux/platform_data/mtd-orion_nand.h> 23 24 struct orion_nand_info { 25 struct nand_controller controller; 26 struct nand_chip chip; 27 struct clk *clk; 28 }; 29 30 static void orion_nand_cmd_ctrl(struct nand_chip *nc, int cmd, 31 unsigned int ctrl) 32 { 33 struct orion_nand_data *board = nand_get_controller_data(nc); 34 u32 offs; 35 36 if (cmd == NAND_CMD_NONE) 37 return; 38 39 if (ctrl & NAND_CLE) 40 offs = (1 << board->cle); 41 else if (ctrl & NAND_ALE) 42 offs = (1 << board->ale); 43 else 44 return; 45 46 if (nc->options & NAND_BUSWIDTH_16) 47 offs <<= 1; 48 49 writeb(cmd, nc->legacy.IO_ADDR_W + offs); 50 } 51 52 static void orion_nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len) 53 { 54 void __iomem *io_base = chip->legacy.IO_ADDR_R; 55 #if defined(__LINUX_ARM_ARCH__) && __LINUX_ARM_ARCH__ >= 5 56 uint64_t *buf64; 57 #endif 58 int i = 0; 59 60 while (len && (unsigned long)buf & 7) { 61 *buf++ = readb(io_base); 62 len--; 63 } 64 #if defined(__LINUX_ARM_ARCH__) && __LINUX_ARM_ARCH__ >= 5 65 buf64 = (uint64_t *)buf; 66 while (i < len/8) { 67 /* 68 * Since GCC has no proper constraint (PR 43518) 69 * force x variable to r2/r3 registers as ldrd instruction 70 * requires first register to be even. 71 */ 72 register uint64_t x asm ("r2"); 73 74 asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base)); 75 buf64[i++] = x; 76 } 77 i *= 8; 78 #else 79 readsl(io_base, buf, len/4); 80 i = len / 4 * 4; 81 #endif 82 while (i < len) 83 buf[i++] = readb(io_base); 84 } 85 86 static int orion_nand_attach_chip(struct nand_chip *chip) 87 { 88 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT; 89 chip->ecc.algo = NAND_ECC_ALGO_HAMMING; 90 91 return 0; 92 } 93 94 static const struct nand_controller_ops orion_nand_ops = { 95 .attach_chip = orion_nand_attach_chip, 96 }; 97 98 static int __init orion_nand_probe(struct platform_device *pdev) 99 { 100 struct orion_nand_info *info; 101 struct mtd_info *mtd; 102 struct nand_chip *nc; 103 struct orion_nand_data *board; 104 struct resource *res; 105 void __iomem *io_base; 106 int ret = 0; 107 u32 val = 0; 108 109 info = devm_kzalloc(&pdev->dev, 110 sizeof(struct orion_nand_info), 111 GFP_KERNEL); 112 if (!info) 113 return -ENOMEM; 114 nc = &info->chip; 115 mtd = nand_to_mtd(nc); 116 117 nand_controller_init(&info->controller); 118 info->controller.ops = &orion_nand_ops; 119 nc->controller = &info->controller; 120 121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 122 io_base = devm_ioremap_resource(&pdev->dev, res); 123 124 if (IS_ERR(io_base)) 125 return PTR_ERR(io_base); 126 127 if (pdev->dev.of_node) { 128 board = devm_kzalloc(&pdev->dev, sizeof(struct orion_nand_data), 129 GFP_KERNEL); 130 if (!board) 131 return -ENOMEM; 132 if (!of_property_read_u32(pdev->dev.of_node, "cle", &val)) 133 board->cle = (u8)val; 134 else 135 board->cle = 0; 136 if (!of_property_read_u32(pdev->dev.of_node, "ale", &val)) 137 board->ale = (u8)val; 138 else 139 board->ale = 1; 140 if (!of_property_read_u32(pdev->dev.of_node, 141 "bank-width", &val)) 142 board->width = (u8)val * 8; 143 else 144 board->width = 8; 145 if (!of_property_read_u32(pdev->dev.of_node, 146 "chip-delay", &val)) 147 board->chip_delay = (u8)val; 148 } else { 149 board = dev_get_platdata(&pdev->dev); 150 } 151 152 mtd->dev.parent = &pdev->dev; 153 154 nand_set_controller_data(nc, board); 155 nand_set_flash_node(nc, pdev->dev.of_node); 156 nc->legacy.IO_ADDR_R = nc->legacy.IO_ADDR_W = io_base; 157 nc->legacy.cmd_ctrl = orion_nand_cmd_ctrl; 158 nc->legacy.read_buf = orion_nand_read_buf; 159 160 if (board->chip_delay) 161 nc->legacy.chip_delay = board->chip_delay; 162 163 WARN(board->width > 16, 164 "%d bit bus width out of range", 165 board->width); 166 167 if (board->width == 16) 168 nc->options |= NAND_BUSWIDTH_16; 169 170 platform_set_drvdata(pdev, info); 171 172 /* Not all platforms can gate the clock, so it is not 173 an error if the clock does not exists. */ 174 info->clk = devm_clk_get(&pdev->dev, NULL); 175 if (IS_ERR(info->clk)) { 176 ret = PTR_ERR(info->clk); 177 if (ret == -ENOENT) { 178 info->clk = NULL; 179 } else { 180 dev_err(&pdev->dev, "failed to get clock!\n"); 181 return ret; 182 } 183 } 184 185 ret = clk_prepare_enable(info->clk); 186 if (ret) { 187 dev_err(&pdev->dev, "failed to prepare clock!\n"); 188 return ret; 189 } 190 191 ret = nand_scan(nc, 1); 192 if (ret) 193 goto no_dev; 194 195 mtd->name = "orion_nand"; 196 ret = mtd_device_register(mtd, board->parts, board->nr_parts); 197 if (ret) { 198 nand_cleanup(nc); 199 goto no_dev; 200 } 201 202 return 0; 203 204 no_dev: 205 clk_disable_unprepare(info->clk); 206 return ret; 207 } 208 209 static int orion_nand_remove(struct platform_device *pdev) 210 { 211 struct orion_nand_info *info = platform_get_drvdata(pdev); 212 struct nand_chip *chip = &info->chip; 213 int ret; 214 215 ret = mtd_device_unregister(nand_to_mtd(chip)); 216 WARN_ON(ret); 217 218 nand_cleanup(chip); 219 220 clk_disable_unprepare(info->clk); 221 222 return 0; 223 } 224 225 #ifdef CONFIG_OF 226 static const struct of_device_id orion_nand_of_match_table[] = { 227 { .compatible = "marvell,orion-nand", }, 228 {}, 229 }; 230 MODULE_DEVICE_TABLE(of, orion_nand_of_match_table); 231 #endif 232 233 static struct platform_driver orion_nand_driver = { 234 .remove = orion_nand_remove, 235 .driver = { 236 .name = "orion_nand", 237 .of_match_table = of_match_ptr(orion_nand_of_match_table), 238 }, 239 }; 240 241 module_platform_driver_probe(orion_nand_driver, orion_nand_probe); 242 243 MODULE_LICENSE("GPL"); 244 MODULE_AUTHOR("Tzachi Perelstein"); 245 MODULE_DESCRIPTION("NAND glue for Orion platforms"); 246 MODULE_ALIAS("platform:orion_nand"); 247