blob: fdc4786ea3e57e8c421c58cd531516c451e65cd9 [file] [log] [blame]
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +02001/*
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 Lunn9c2bd502012-02-19 11:01:22 +010019#include <linux/clk.h>
20#include <linux/err.h>
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020021#include <asm/io.h>
22#include <asm/sizes.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/hardware.h>
Lennert Buytenhek6f088f12008-08-09 13:44:58 +020024#include <plat/orion_nand.h>
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020025
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020026static 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 Pitrebfee1a42009-05-31 22:25:40 -040048static 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 Zaleckasa88a2b82010-04-23 13:17:47 -040061 /*
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 Kagstrom94da2102009-08-20 09:19:53 +020068 asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
Nicolas Pitrebfee1a42009-05-31 22:25:40 -040069 buf64[i++] = x;
70 }
71 i *= 8;
72 while (i < len)
73 buf[i++] = readb(io_base);
74}
75
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020076static 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 Sweetene9903062009-12-14 16:48:34 -050081 struct resource *res;
Andrew Lunn9c2bd502012-02-19 11:01:22 +010082 struct clk *clk;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020083 void __iomem *io_base;
84 int ret = 0;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020085
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 Sweetene9903062009-12-14 16:48:34 -050094 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
95 if (!res) {
Peter Huewe2d6bfc22010-01-07 02:51:13 +010096 ret = -ENODEV;
H Hartley Sweetene9903062009-12-14 16:48:34 -050097 goto no_res;
98 }
99
100 io_base = ioremap(res->start, resource_size(res));
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200101 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 Pitrebfee1a42009-05-31 22:25:40 -0400115 nc->read_buf = orion_nand_read_buf;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200116 nc->ecc.mode = NAND_ECC_SOFT;
117
Saeed Bisharaf4db56f2008-05-04 19:25:52 -1100118 if (board->chip_delay)
119 nc->chip_delay = board->chip_delay;
120
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200121 if (board->width == 16)
122 nc->options |= NAND_BUSWIDTH_16;
123
Ben Dookseedfea22010-04-20 10:26:18 +0100124 if (board->dev_ready)
125 nc->dev_ready = board->dev_ready;
126
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200127 platform_set_drvdata(pdev, mtd);
128
Andrew Lunn9c2bd502012-02-19 11:01:22 +0100129 /* 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 Perelstein2a1dba22007-10-17 01:10:40 +0200137 if (nand_scan(mtd, 1)) {
138 ret = -ENXIO;
139 goto no_dev;
140 }
141
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200142 mtd->name = "orion_nand";
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200143 ret = mtd_device_parse_register(mtd, NULL, NULL, board->parts,
144 board->nr_parts);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200145 if (ret) {
146 nand_release(mtd);
147 goto no_dev;
148 }
149
150 return 0;
151
152no_dev:
153 platform_set_drvdata(pdev, NULL);
154 iounmap(io_base);
155no_res:
156 kfree(nc);
157
158 return ret;
159}
160
161static 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 Lunn9c2bd502012-02-19 11:01:22 +0100165 struct clk *clk;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200166
167 nand_release(mtd);
168
169 iounmap(nc->IO_ADDR_W);
170
171 kfree(nc);
172
Andrew Lunn9c2bd502012-02-19 11:01:22 +0100173 clk = clk_get(&pdev->dev, NULL);
174 if (!IS_ERR(clk)) {
175 clk_disable_unprepare(clk);
176 clk_put(clk);
177 }
178
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200179 return 0;
180}
181
182static struct platform_driver orion_nand_driver = {
Russell Kingbdf602b2009-03-03 13:43:47 +0000183 .remove = __devexit_p(orion_nand_remove),
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200184 .driver = {
185 .name = "orion_nand",
186 .owner = THIS_MODULE,
187 },
188};
189
190static int __init orion_nand_init(void)
191{
Uwe Kleine-Königf33dabb2009-09-18 12:51:43 -0700192 return platform_driver_probe(&orion_nand_driver, orion_nand_probe);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200193}
194
195static void __exit orion_nand_exit(void)
196{
197 platform_driver_unregister(&orion_nand_driver);
198}
199
200module_init(orion_nand_init);
201module_exit(orion_nand_exit);
202
203MODULE_LICENSE("GPL");
204MODULE_AUTHOR("Tzachi Perelstein");
205MODULE_DESCRIPTION("NAND glue for Orion platforms");
Kay Sievers1ff18422008-04-18 13:44:27 -0700206MODULE_ALIAS("platform:orion_nand");