Merge branch 'core-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.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 <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
25 static const char *part_probes[] = { "cmdlinepart", NULL };
26 #endif
27
28 static 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
50 static 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                 /*
64                  * Since GCC has no proper constraint (PR 43518)
65                  * force x variable to r2/r3 registers as ldrd instruction
66                  * requires first register to be even.
67                  */
68                 register uint64_t x asm ("r2");
69
70                 asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
71                 buf64[i++] = x;
72         }
73         i *= 8;
74         while (i < len)
75                 buf[i++] = readb(io_base);
76 }
77
78 static int __init orion_nand_probe(struct platform_device *pdev)
79 {
80         struct mtd_info *mtd;
81         struct nand_chip *nc;
82         struct orion_nand_data *board;
83         void __iomem *io_base;
84         int ret = 0;
85 #ifdef CONFIG_MTD_PARTITIONS
86         struct mtd_partition *partitions = NULL;
87         int num_part = 0;
88 #endif
89
90         nc = kzalloc(sizeof(struct nand_chip) + sizeof(struct mtd_info), GFP_KERNEL);
91         if (!nc) {
92                 printk(KERN_ERR "orion_nand: failed to allocate device structure.\n");
93                 ret = -ENOMEM;
94                 goto no_res;
95         }
96         mtd = (struct mtd_info *)(nc + 1);
97
98         io_base = ioremap(pdev->resource[0].start,
99                         pdev->resource[0].end - pdev->resource[0].start + 1);
100         if (!io_base) {
101                 printk(KERN_ERR "orion_nand: ioremap failed\n");
102                 ret = -EIO;
103                 goto no_res;
104         }
105
106         board = pdev->dev.platform_data;
107
108         mtd->priv = nc;
109         mtd->owner = THIS_MODULE;
110
111         nc->priv = board;
112         nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
113         nc->cmd_ctrl = orion_nand_cmd_ctrl;
114         nc->read_buf = orion_nand_read_buf;
115         nc->ecc.mode = NAND_ECC_SOFT;
116
117         if (board->chip_delay)
118                 nc->chip_delay = board->chip_delay;
119
120         if (board->width == 16)
121                 nc->options |= NAND_BUSWIDTH_16;
122
123         platform_set_drvdata(pdev, mtd);
124
125         if (nand_scan(mtd, 1)) {
126                 ret = -ENXIO;
127                 goto no_dev;
128         }
129
130 #ifdef CONFIG_MTD_PARTITIONS
131 #ifdef CONFIG_MTD_CMDLINE_PARTS
132         mtd->name = "orion_nand";
133         num_part = parse_mtd_partitions(mtd, part_probes, &partitions, 0);
134 #endif
135         /* If cmdline partitions have been passed, let them be used */
136         if (num_part <= 0) {
137                 num_part = board->nr_parts;
138                 partitions = board->parts;
139         }
140
141         if (partitions && num_part > 0)
142                 ret = add_mtd_partitions(mtd, partitions, num_part);
143         else
144                 ret = add_mtd_device(mtd);
145 #else
146         ret = add_mtd_device(mtd);
147 #endif
148
149         if (ret) {
150                 nand_release(mtd);
151                 goto no_dev;
152         }
153
154         return 0;
155
156 no_dev:
157         platform_set_drvdata(pdev, NULL);
158         iounmap(io_base);
159 no_res:
160         kfree(nc);
161
162         return ret;
163 }
164
165 static int __devexit orion_nand_remove(struct platform_device *pdev)
166 {
167         struct mtd_info *mtd = platform_get_drvdata(pdev);
168         struct nand_chip *nc = mtd->priv;
169
170         nand_release(mtd);
171
172         iounmap(nc->IO_ADDR_W);
173
174         kfree(nc);
175
176         return 0;
177 }
178
179 static struct platform_driver orion_nand_driver = {
180         .remove         = __devexit_p(orion_nand_remove),
181         .driver         = {
182                 .name   = "orion_nand",
183                 .owner  = THIS_MODULE,
184         },
185 };
186
187 static int __init orion_nand_init(void)
188 {
189         return platform_driver_probe(&orion_nand_driver, orion_nand_probe);
190 }
191
192 static void __exit orion_nand_exit(void)
193 {
194         platform_driver_unregister(&orion_nand_driver);
195 }
196
197 module_init(orion_nand_init);
198 module_exit(orion_nand_exit);
199
200 MODULE_LICENSE("GPL");
201 MODULE_AUTHOR("Tzachi Perelstein");
202 MODULE_DESCRIPTION("NAND glue for Orion platforms");
203 MODULE_ALIAS("platform:orion_nand");