Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 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> |
Andrew Lunn | 9c2bd50 | 2012-02-19 11:01:22 +0100 | [diff] [blame^] | 19 | #include <linux/clk.h> |
| 20 | #include <linux/err.h> |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | #include <asm/sizes.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 23 | #include <mach/hardware.h> |
Lennert Buytenhek | 6f088f1 | 2008-08-09 13:44:58 +0200 | [diff] [blame] | 24 | #include <plat/orion_nand.h> |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 25 | |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 26 | static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
| 27 | { |
| 28 | struct nand_chip *nc = mtd->priv; |
| 29 | struct orion_nand_data *board = nc->priv; |
| 30 | u32 offs; |
| 31 | |
| 32 | if (cmd == NAND_CMD_NONE) |
| 33 | return; |
| 34 | |
| 35 | if (ctrl & NAND_CLE) |
| 36 | offs = (1 << board->cle); |
| 37 | else if (ctrl & NAND_ALE) |
| 38 | offs = (1 << board->ale); |
| 39 | else |
| 40 | return; |
| 41 | |
| 42 | if (nc->options & NAND_BUSWIDTH_16) |
| 43 | offs <<= 1; |
| 44 | |
| 45 | writeb(cmd, nc->IO_ADDR_W + offs); |
| 46 | } |
| 47 | |
Nicolas Pitre | bfee1a4 | 2009-05-31 22:25:40 -0400 | [diff] [blame] | 48 | static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 49 | { |
| 50 | struct nand_chip *chip = mtd->priv; |
| 51 | void __iomem *io_base = chip->IO_ADDR_R; |
| 52 | uint64_t *buf64; |
| 53 | int i = 0; |
| 54 | |
| 55 | while (len && (unsigned long)buf & 7) { |
| 56 | *buf++ = readb(io_base); |
| 57 | len--; |
| 58 | } |
| 59 | buf64 = (uint64_t *)buf; |
| 60 | while (i < len/8) { |
Paulius Zaleckas | a88a2b8 | 2010-04-23 13:17:47 -0400 | [diff] [blame] | 61 | /* |
| 62 | * Since GCC has no proper constraint (PR 43518) |
| 63 | * force x variable to r2/r3 registers as ldrd instruction |
| 64 | * requires first register to be even. |
| 65 | */ |
| 66 | register uint64_t x asm ("r2"); |
| 67 | |
Simon Kagstrom | 94da210 | 2009-08-20 09:19:53 +0200 | [diff] [blame] | 68 | asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base)); |
Nicolas Pitre | bfee1a4 | 2009-05-31 22:25:40 -0400 | [diff] [blame] | 69 | buf64[i++] = x; |
| 70 | } |
| 71 | i *= 8; |
| 72 | while (i < len) |
| 73 | buf[i++] = readb(io_base); |
| 74 | } |
| 75 | |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 76 | static int __init orion_nand_probe(struct platform_device *pdev) |
| 77 | { |
| 78 | struct mtd_info *mtd; |
| 79 | struct nand_chip *nc; |
| 80 | struct orion_nand_data *board; |
H Hartley Sweeten | e990306 | 2009-12-14 16:48:34 -0500 | [diff] [blame] | 81 | struct resource *res; |
Andrew Lunn | 9c2bd50 | 2012-02-19 11:01:22 +0100 | [diff] [blame^] | 82 | struct clk *clk; |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 83 | void __iomem *io_base; |
| 84 | int ret = 0; |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 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 | |
H Hartley Sweeten | e990306 | 2009-12-14 16:48:34 -0500 | [diff] [blame] | 94 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 95 | if (!res) { |
Peter Huewe | 2d6bfc2 | 2010-01-07 02:51:13 +0100 | [diff] [blame] | 96 | ret = -ENODEV; |
H Hartley Sweeten | e990306 | 2009-12-14 16:48:34 -0500 | [diff] [blame] | 97 | goto no_res; |
| 98 | } |
| 99 | |
| 100 | io_base = ioremap(res->start, resource_size(res)); |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 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; |
Nicolas Pitre | bfee1a4 | 2009-05-31 22:25:40 -0400 | [diff] [blame] | 115 | nc->read_buf = orion_nand_read_buf; |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 116 | nc->ecc.mode = NAND_ECC_SOFT; |
| 117 | |
Saeed Bishara | f4db56f | 2008-05-04 19:25:52 -1100 | [diff] [blame] | 118 | if (board->chip_delay) |
| 119 | nc->chip_delay = board->chip_delay; |
| 120 | |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 121 | if (board->width == 16) |
| 122 | nc->options |= NAND_BUSWIDTH_16; |
| 123 | |
Ben Dooks | eedfea2 | 2010-04-20 10:26:18 +0100 | [diff] [blame] | 124 | if (board->dev_ready) |
| 125 | nc->dev_ready = board->dev_ready; |
| 126 | |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 127 | platform_set_drvdata(pdev, mtd); |
| 128 | |
Andrew Lunn | 9c2bd50 | 2012-02-19 11:01:22 +0100 | [diff] [blame^] | 129 | /* Not all platforms can gate the clock, so it is not |
| 130 | an error if the clock does not exists. */ |
| 131 | clk = clk_get(&pdev->dev, NULL); |
| 132 | if (!IS_ERR(clk)) { |
| 133 | clk_prepare_enable(clk); |
| 134 | clk_put(clk); |
| 135 | } |
| 136 | |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 137 | if (nand_scan(mtd, 1)) { |
| 138 | ret = -ENXIO; |
| 139 | goto no_dev; |
| 140 | } |
| 141 | |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 142 | mtd->name = "orion_nand"; |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 143 | ret = mtd_device_parse_register(mtd, NULL, NULL, board->parts, |
| 144 | board->nr_parts); |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 145 | if (ret) { |
| 146 | nand_release(mtd); |
| 147 | goto no_dev; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | |
| 152 | no_dev: |
| 153 | platform_set_drvdata(pdev, NULL); |
| 154 | iounmap(io_base); |
| 155 | no_res: |
| 156 | kfree(nc); |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static int __devexit orion_nand_remove(struct platform_device *pdev) |
| 162 | { |
| 163 | struct mtd_info *mtd = platform_get_drvdata(pdev); |
| 164 | struct nand_chip *nc = mtd->priv; |
Andrew Lunn | 9c2bd50 | 2012-02-19 11:01:22 +0100 | [diff] [blame^] | 165 | struct clk *clk; |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 166 | |
| 167 | nand_release(mtd); |
| 168 | |
| 169 | iounmap(nc->IO_ADDR_W); |
| 170 | |
| 171 | kfree(nc); |
| 172 | |
Andrew Lunn | 9c2bd50 | 2012-02-19 11:01:22 +0100 | [diff] [blame^] | 173 | clk = clk_get(&pdev->dev, NULL); |
| 174 | if (!IS_ERR(clk)) { |
| 175 | clk_disable_unprepare(clk); |
| 176 | clk_put(clk); |
| 177 | } |
| 178 | |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static struct platform_driver orion_nand_driver = { |
Russell King | bdf602b | 2009-03-03 13:43:47 +0000 | [diff] [blame] | 183 | .remove = __devexit_p(orion_nand_remove), |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 184 | .driver = { |
| 185 | .name = "orion_nand", |
| 186 | .owner = THIS_MODULE, |
| 187 | }, |
| 188 | }; |
| 189 | |
| 190 | static int __init orion_nand_init(void) |
| 191 | { |
Uwe Kleine-König | f33dabb | 2009-09-18 12:51:43 -0700 | [diff] [blame] | 192 | return platform_driver_probe(&orion_nand_driver, orion_nand_probe); |
Tzachi Perelstein | 2a1dba2 | 2007-10-17 01:10:40 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void __exit orion_nand_exit(void) |
| 196 | { |
| 197 | platform_driver_unregister(&orion_nand_driver); |
| 198 | } |
| 199 | |
| 200 | module_init(orion_nand_init); |
| 201 | module_exit(orion_nand_exit); |
| 202 | |
| 203 | MODULE_LICENSE("GPL"); |
| 204 | MODULE_AUTHOR("Tzachi Perelstein"); |
| 205 | MODULE_DESCRIPTION("NAND glue for Orion platforms"); |
Kay Sievers | 1ff1842 | 2008-04-18 13:44:27 -0700 | [diff] [blame] | 206 | MODULE_ALIAS("platform:orion_nand"); |