Commit | Line | Data |
---|---|---|
2a1dba29 TP |
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> | |
a09e64fb | 21 | #include <mach/hardware.h> |
6f088f1d | 22 | #include <plat/orion_nand.h> |
2a1dba29 | 23 | |
2a1dba29 TP |
24 | static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
25 | { | |
26 | struct nand_chip *nc = mtd->priv; | |
27 | struct orion_nand_data *board = nc->priv; | |
28 | u32 offs; | |
29 | ||
30 | if (cmd == NAND_CMD_NONE) | |
31 | return; | |
32 | ||
33 | if (ctrl & NAND_CLE) | |
34 | offs = (1 << board->cle); | |
35 | else if (ctrl & NAND_ALE) | |
36 | offs = (1 << board->ale); | |
37 | else | |
38 | return; | |
39 | ||
40 | if (nc->options & NAND_BUSWIDTH_16) | |
41 | offs <<= 1; | |
42 | ||
43 | writeb(cmd, nc->IO_ADDR_W + offs); | |
44 | } | |
45 | ||
bfee1a43 NP |
46 | static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
47 | { | |
48 | struct nand_chip *chip = mtd->priv; | |
49 | void __iomem *io_base = chip->IO_ADDR_R; | |
50 | uint64_t *buf64; | |
51 | int i = 0; | |
52 | ||
53 | while (len && (unsigned long)buf & 7) { | |
54 | *buf++ = readb(io_base); | |
55 | len--; | |
56 | } | |
57 | buf64 = (uint64_t *)buf; | |
58 | while (i < len/8) { | |
a88a2b88 PZ |
59 | /* |
60 | * Since GCC has no proper constraint (PR 43518) | |
61 | * force x variable to r2/r3 registers as ldrd instruction | |
62 | * requires first register to be even. | |
63 | */ | |
64 | register uint64_t x asm ("r2"); | |
65 | ||
94da210a | 66 | asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base)); |
bfee1a43 NP |
67 | buf64[i++] = x; |
68 | } | |
69 | i *= 8; | |
70 | while (i < len) | |
71 | buf[i++] = readb(io_base); | |
72 | } | |
73 | ||
2a1dba29 TP |
74 | static int __init orion_nand_probe(struct platform_device *pdev) |
75 | { | |
76 | struct mtd_info *mtd; | |
77 | struct nand_chip *nc; | |
78 | struct orion_nand_data *board; | |
e9903060 | 79 | struct resource *res; |
2a1dba29 TP |
80 | void __iomem *io_base; |
81 | int ret = 0; | |
2a1dba29 TP |
82 | struct mtd_partition *partitions = NULL; |
83 | int num_part = 0; | |
2a1dba29 TP |
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 | ||
e9903060 HS |
93 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
94 | if (!res) { | |
2d6bfc26 | 95 | ret = -ENODEV; |
e9903060 HS |
96 | goto no_res; |
97 | } | |
98 | ||
99 | io_base = ioremap(res->start, resource_size(res)); | |
2a1dba29 TP |
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; | |
bfee1a43 | 114 | nc->read_buf = orion_nand_read_buf; |
2a1dba29 TP |
115 | nc->ecc.mode = NAND_ECC_SOFT; |
116 | ||
f4db56ff SB |
117 | if (board->chip_delay) |
118 | nc->chip_delay = board->chip_delay; | |
119 | ||
2a1dba29 TP |
120 | if (board->width == 16) |
121 | nc->options |= NAND_BUSWIDTH_16; | |
122 | ||
eedfea25 BD |
123 | if (board->dev_ready) |
124 | nc->dev_ready = board->dev_ready; | |
125 | ||
2a1dba29 TP |
126 | platform_set_drvdata(pdev, mtd); |
127 | ||
128 | if (nand_scan(mtd, 1)) { | |
129 | ret = -ENXIO; | |
130 | goto no_dev; | |
131 | } | |
132 | ||
2a1dba29 | 133 | mtd->name = "orion_nand"; |
4ac6b3ef | 134 | num_part = parse_mtd_partitions(mtd, NULL, &partitions, 0); |
2a1dba29 TP |
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 | ||
55c0689c | 141 | ret = mtd_device_register(mtd, partitions, num_part); |
2a1dba29 TP |
142 | if (ret) { |
143 | nand_release(mtd); | |
144 | goto no_dev; | |
145 | } | |
146 | ||
147 | return 0; | |
148 | ||
149 | no_dev: | |
150 | platform_set_drvdata(pdev, NULL); | |
151 | iounmap(io_base); | |
152 | no_res: | |
153 | kfree(nc); | |
154 | ||
155 | return ret; | |
156 | } | |
157 | ||
158 | static int __devexit orion_nand_remove(struct platform_device *pdev) | |
159 | { | |
160 | struct mtd_info *mtd = platform_get_drvdata(pdev); | |
161 | struct nand_chip *nc = mtd->priv; | |
162 | ||
163 | nand_release(mtd); | |
164 | ||
165 | iounmap(nc->IO_ADDR_W); | |
166 | ||
167 | kfree(nc); | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | static struct platform_driver orion_nand_driver = { | |
bdf602bd | 173 | .remove = __devexit_p(orion_nand_remove), |
2a1dba29 TP |
174 | .driver = { |
175 | .name = "orion_nand", | |
176 | .owner = THIS_MODULE, | |
177 | }, | |
178 | }; | |
179 | ||
180 | static int __init orion_nand_init(void) | |
181 | { | |
f33dabbe | 182 | return platform_driver_probe(&orion_nand_driver, orion_nand_probe); |
2a1dba29 TP |
183 | } |
184 | ||
185 | static void __exit orion_nand_exit(void) | |
186 | { | |
187 | platform_driver_unregister(&orion_nand_driver); | |
188 | } | |
189 | ||
190 | module_init(orion_nand_init); | |
191 | module_exit(orion_nand_exit); | |
192 | ||
193 | MODULE_LICENSE("GPL"); | |
194 | MODULE_AUTHOR("Tzachi Perelstein"); | |
195 | MODULE_DESCRIPTION("NAND glue for Orion platforms"); | |
1ff18422 | 196 | MODULE_ALIAS("platform:orion_nand"); |