ARM: Orion: NAND: Add support for clk, if there is one.
[deliverable/linux.git] / drivers / mtd / nand / orion_nand.c
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 <linux/clk.h>
20 #include <linux/err.h>
21 #include <asm/io.h>
22 #include <asm/sizes.h>
23 #include <mach/hardware.h>
24 #include <plat/orion_nand.h>
25
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
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) {
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
68 asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
69 buf64[i++] = x;
70 }
71 i *= 8;
72 while (i < len)
73 buf[i++] = readb(io_base);
74 }
75
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;
81 struct resource *res;
82 struct clk *clk;
83 void __iomem *io_base;
84 int ret = 0;
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 /* 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
137 if (nand_scan(mtd, 1)) {
138 ret = -ENXIO;
139 goto no_dev;
140 }
141
142 mtd->name = "orion_nand";
143 ret = mtd_device_parse_register(mtd, NULL, NULL, board->parts,
144 board->nr_parts);
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;
165 struct clk *clk;
166
167 nand_release(mtd);
168
169 iounmap(nc->IO_ADDR_W);
170
171 kfree(nc);
172
173 clk = clk_get(&pdev->dev, NULL);
174 if (!IS_ERR(clk)) {
175 clk_disable_unprepare(clk);
176 clk_put(clk);
177 }
178
179 return 0;
180 }
181
182 static struct platform_driver orion_nand_driver = {
183 .remove = __devexit_p(orion_nand_remove),
184 .driver = {
185 .name = "orion_nand",
186 .owner = THIS_MODULE,
187 },
188 };
189
190 static int __init orion_nand_init(void)
191 {
192 return platform_driver_probe(&orion_nand_driver, orion_nand_probe);
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");
206 MODULE_ALIAS("platform:orion_nand");
This page took 0.035558 seconds and 5 git commands to generate.