blob: f16050c61c5ca31430bad62eb4c50cb885913233 [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>
19#include <asm/io.h>
20#include <asm/sizes.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010021#include <mach/hardware.h>
Lennert Buytenhek6f088f12008-08-09 13:44:58 +020022#include <plat/orion_nand.h>
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020023
24#ifdef CONFIG_MTD_CMDLINE_PARTS
25static const char *part_probes[] = { "cmdlinepart", NULL };
26#endif
27
28static 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
Nicolas Pitrebfee1a42009-05-31 22:25:40 -040050static 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 uint64_t x;
Simon Kagstrom94da2102009-08-20 09:19:53 +020064 asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
Nicolas Pitrebfee1a42009-05-31 22:25:40 -040065 buf64[i++] = x;
66 }
67 i *= 8;
68 while (i < len)
69 buf[i++] = readb(io_base);
70}
71
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020072static int __init orion_nand_probe(struct platform_device *pdev)
73{
74 struct mtd_info *mtd;
75 struct nand_chip *nc;
76 struct orion_nand_data *board;
H Hartley Sweetene9903062009-12-14 16:48:34 -050077 struct resource *res;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020078 void __iomem *io_base;
79 int ret = 0;
80#ifdef CONFIG_MTD_PARTITIONS
81 struct mtd_partition *partitions = NULL;
82 int num_part = 0;
83#endif
84
85 nc = kzalloc(sizeof(struct nand_chip) + sizeof(struct mtd_info), GFP_KERNEL);
86 if (!nc) {
87 printk(KERN_ERR "orion_nand: failed to allocate device structure.\n");
88 ret = -ENOMEM;
89 goto no_res;
90 }
91 mtd = (struct mtd_info *)(nc + 1);
92
H Hartley Sweetene9903062009-12-14 16:48:34 -050093 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94 if (!res) {
Peter Huewe2d6bfc22010-01-07 02:51:13 +010095 ret = -ENODEV;
H Hartley Sweetene9903062009-12-14 16:48:34 -050096 goto no_res;
97 }
98
99 io_base = ioremap(res->start, resource_size(res));
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200100 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;
Nicolas Pitrebfee1a42009-05-31 22:25:40 -0400114 nc->read_buf = orion_nand_read_buf;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200115 nc->ecc.mode = NAND_ECC_SOFT;
116
Saeed Bisharaf4db56f2008-05-04 19:25:52 -1100117 if (board->chip_delay)
118 nc->chip_delay = board->chip_delay;
119
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200120 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
156no_dev:
157 platform_set_drvdata(pdev, NULL);
158 iounmap(io_base);
159no_res:
160 kfree(nc);
161
162 return ret;
163}
164
165static 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
179static struct platform_driver orion_nand_driver = {
Russell Kingbdf602b2009-03-03 13:43:47 +0000180 .remove = __devexit_p(orion_nand_remove),
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200181 .driver = {
182 .name = "orion_nand",
183 .owner = THIS_MODULE,
184 },
185};
186
187static int __init orion_nand_init(void)
188{
Uwe Kleine-Königf33dabb2009-09-18 12:51:43 -0700189 return platform_driver_probe(&orion_nand_driver, orion_nand_probe);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200190}
191
192static void __exit orion_nand_exit(void)
193{
194 platform_driver_unregister(&orion_nand_driver);
195}
196
197module_init(orion_nand_init);
198module_exit(orion_nand_exit);
199
200MODULE_LICENSE("GPL");
201MODULE_AUTHOR("Tzachi Perelstein");
202MODULE_DESCRIPTION("NAND glue for Orion platforms");
Kay Sievers1ff18422008-04-18 13:44:27 -0700203MODULE_ALIAS("platform:orion_nand");