]>
Commit | Line | Data |
---|---|---|
54d33c4c | 1 | /* |
54d33c4c MR |
2 | * Copyright (C) 2006 Compulab, Ltd. |
3 | * Mike Rapoport <mike@compulab.co.il> | |
4 | * | |
187c5448 | 5 | * Derived from drivers/mtd/nand/h1910.c (removed in v3.10) |
54d33c4c MR |
6 | * Copyright (C) 2002 Marius Gröger (mag@sysgo.de) |
7 | * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de) | |
8 | * | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | * Overview: | |
15 | * This is a device driver for the NAND flash device found on the | |
16 | * CM-X270 board. | |
17 | */ | |
18 | ||
d4092d76 | 19 | #include <linux/mtd/rawnand.h> |
54d33c4c | 20 | #include <linux/mtd/partitions.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
70eb33d6 | 22 | #include <linux/gpio.h> |
a0e5cc58 | 23 | #include <linux/module.h> |
54d33c4c MR |
24 | |
25 | #include <asm/io.h> | |
26 | #include <asm/irq.h> | |
70eb33d6 | 27 | #include <asm/mach-types.h> |
54d33c4c | 28 | |
b74d1969 | 29 | #include <mach/pxa2xx-regs.h> |
54d33c4c MR |
30 | |
31 | #define GPIO_NAND_CS (11) | |
32 | #define GPIO_NAND_RB (89) | |
33 | ||
54d33c4c MR |
34 | /* MTD structure for CM-X270 board */ |
35 | static struct mtd_info *cmx270_nand_mtd; | |
36 | ||
37 | /* remaped IO address of the device */ | |
38 | static void __iomem *cmx270_nand_io; | |
39 | ||
40 | /* | |
41 | * Define static partitions for flash device | |
42 | */ | |
d4906688 | 43 | static const struct mtd_partition partition_info[] = { |
54d33c4c MR |
44 | [0] = { |
45 | .name = "cmx270-0", | |
46 | .offset = 0, | |
47 | .size = MTDPART_SIZ_FULL | |
48 | } | |
49 | }; | |
50 | #define NUM_PARTITIONS (ARRAY_SIZE(partition_info)) | |
51 | ||
7e534323 | 52 | static u_char cmx270_read_byte(struct nand_chip *this) |
54d33c4c | 53 | { |
82fc5099 | 54 | return (readl(this->legacy.IO_ADDR_R) >> 16); |
54d33c4c MR |
55 | } |
56 | ||
c0739d85 BB |
57 | static void cmx270_write_buf(struct nand_chip *this, const u_char *buf, |
58 | int len) | |
54d33c4c MR |
59 | { |
60 | int i; | |
54d33c4c MR |
61 | |
62 | for (i=0; i<len; i++) | |
82fc5099 | 63 | writel((*buf++ << 16), this->legacy.IO_ADDR_W); |
54d33c4c MR |
64 | } |
65 | ||
7e534323 | 66 | static void cmx270_read_buf(struct nand_chip *this, u_char *buf, int len) |
54d33c4c MR |
67 | { |
68 | int i; | |
54d33c4c MR |
69 | |
70 | for (i=0; i<len; i++) | |
82fc5099 | 71 | *buf++ = readl(this->legacy.IO_ADDR_R) >> 16; |
54d33c4c MR |
72 | } |
73 | ||
54d33c4c MR |
74 | static inline void nand_cs_on(void) |
75 | { | |
70eb33d6 | 76 | gpio_set_value(GPIO_NAND_CS, 0); |
54d33c4c MR |
77 | } |
78 | ||
79 | static void nand_cs_off(void) | |
80 | { | |
70eb33d6 | 81 | dsb(); |
54d33c4c | 82 | |
70eb33d6 | 83 | gpio_set_value(GPIO_NAND_CS, 1); |
54d33c4c MR |
84 | } |
85 | ||
86 | /* | |
87 | * hardware specific access to control-lines | |
88 | */ | |
0f808c16 | 89 | static void cmx270_hwcontrol(struct nand_chip *this, int dat, |
54d33c4c MR |
90 | unsigned int ctrl) |
91 | { | |
82fc5099 | 92 | unsigned int nandaddr = (unsigned int)this->legacy.IO_ADDR_W; |
54d33c4c | 93 | |
70eb33d6 | 94 | dsb(); |
54d33c4c MR |
95 | |
96 | if (ctrl & NAND_CTRL_CHANGE) { | |
97 | if ( ctrl & NAND_ALE ) | |
98 | nandaddr |= (1 << 3); | |
99 | else | |
100 | nandaddr &= ~(1 << 3); | |
101 | if ( ctrl & NAND_CLE ) | |
102 | nandaddr |= (1 << 2); | |
103 | else | |
104 | nandaddr &= ~(1 << 2); | |
105 | if ( ctrl & NAND_NCE ) | |
106 | nand_cs_on(); | |
107 | else | |
108 | nand_cs_off(); | |
109 | } | |
110 | ||
70eb33d6 | 111 | dsb(); |
82fc5099 | 112 | this->legacy.IO_ADDR_W = (void __iomem*)nandaddr; |
54d33c4c | 113 | if (dat != NAND_CMD_NONE) |
82fc5099 | 114 | writel((dat << 16), this->legacy.IO_ADDR_W); |
54d33c4c | 115 | |
70eb33d6 | 116 | dsb(); |
54d33c4c MR |
117 | } |
118 | ||
119 | /* | |
120 | * read device ready pin | |
121 | */ | |
50a487e7 | 122 | static int cmx270_device_ready(struct nand_chip *this) |
54d33c4c | 123 | { |
70eb33d6 | 124 | dsb(); |
54d33c4c | 125 | |
70eb33d6 | 126 | return (gpio_get_value(GPIO_NAND_RB)); |
54d33c4c MR |
127 | } |
128 | ||
129 | /* | |
130 | * Main initialization routine | |
131 | */ | |
627df23c | 132 | static int __init cmx270_init(void) |
54d33c4c MR |
133 | { |
134 | struct nand_chip *this; | |
54d33c4c MR |
135 | int ret; |
136 | ||
a7f3f030 | 137 | if (!(machine_is_armcore() && cpu_is_pxa27x())) |
70eb33d6 MR |
138 | return -ENODEV; |
139 | ||
140 | ret = gpio_request(GPIO_NAND_CS, "NAND CS"); | |
141 | if (ret) { | |
e8348dc5 | 142 | pr_warn("CM-X270: failed to request NAND CS gpio\n"); |
70eb33d6 MR |
143 | return ret; |
144 | } | |
145 | ||
146 | gpio_direction_output(GPIO_NAND_CS, 1); | |
147 | ||
148 | ret = gpio_request(GPIO_NAND_RB, "NAND R/B"); | |
149 | if (ret) { | |
e8348dc5 | 150 | pr_warn("CM-X270: failed to request NAND R/B gpio\n"); |
70eb33d6 MR |
151 | goto err_gpio_request; |
152 | } | |
153 | ||
154 | gpio_direction_input(GPIO_NAND_RB); | |
155 | ||
54d33c4c | 156 | /* Allocate memory for MTD device structure and private data */ |
2afd14f9 BB |
157 | this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL); |
158 | if (!this) { | |
70eb33d6 MR |
159 | ret = -ENOMEM; |
160 | goto err_kzalloc; | |
54d33c4c MR |
161 | } |
162 | ||
163 | cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12); | |
164 | if (!cmx270_nand_io) { | |
70eb33d6 | 165 | pr_debug("Unable to ioremap NAND device\n"); |
54d33c4c | 166 | ret = -EINVAL; |
70eb33d6 | 167 | goto err_ioremap; |
54d33c4c MR |
168 | } |
169 | ||
2afd14f9 | 170 | cmx270_nand_mtd = nand_to_mtd(this); |
54d33c4c MR |
171 | |
172 | /* Link the private data with the MTD structure */ | |
173 | cmx270_nand_mtd->owner = THIS_MODULE; | |
54d33c4c MR |
174 | |
175 | /* insert callbacks */ | |
82fc5099 BB |
176 | this->legacy.IO_ADDR_R = cmx270_nand_io; |
177 | this->legacy.IO_ADDR_W = cmx270_nand_io; | |
bf6065c6 | 178 | this->legacy.cmd_ctrl = cmx270_hwcontrol; |
54d33c4c MR |
179 | this->dev_ready = cmx270_device_ready; |
180 | ||
181 | /* 15 us command delay time */ | |
182 | this->chip_delay = 20; | |
183 | this->ecc.mode = NAND_ECC_SOFT; | |
d9944e1f | 184 | this->ecc.algo = NAND_ECC_HAMMING; |
54d33c4c MR |
185 | |
186 | /* read/write functions */ | |
716bbbab BB |
187 | this->legacy.read_byte = cmx270_read_byte; |
188 | this->legacy.read_buf = cmx270_read_buf; | |
189 | this->legacy.write_buf = cmx270_write_buf; | |
54d33c4c MR |
190 | |
191 | /* Scan to find existence of the device */ | |
00ad378f | 192 | ret = nand_scan(this, 1); |
546fe03f | 193 | if (ret) { |
70eb33d6 | 194 | pr_notice("No NAND device\n"); |
70eb33d6 | 195 | goto err_scan; |
54d33c4c MR |
196 | } |
197 | ||
54d33c4c | 198 | /* Register the partitions */ |
29597ca1 RM |
199 | ret = mtd_device_register(cmx270_nand_mtd, partition_info, |
200 | NUM_PARTITIONS); | |
54d33c4c | 201 | if (ret) |
70eb33d6 | 202 | goto err_scan; |
54d33c4c MR |
203 | |
204 | /* Return happy */ | |
205 | return 0; | |
206 | ||
70eb33d6 | 207 | err_scan: |
54d33c4c | 208 | iounmap(cmx270_nand_io); |
70eb33d6 | 209 | err_ioremap: |
2afd14f9 | 210 | kfree(this); |
70eb33d6 MR |
211 | err_kzalloc: |
212 | gpio_free(GPIO_NAND_RB); | |
213 | err_gpio_request: | |
214 | gpio_free(GPIO_NAND_CS); | |
54d33c4c MR |
215 | |
216 | return ret; | |
217 | ||
218 | } | |
219 | module_init(cmx270_init); | |
220 | ||
221 | /* | |
222 | * Clean up routine | |
223 | */ | |
627df23c | 224 | static void __exit cmx270_cleanup(void) |
54d33c4c MR |
225 | { |
226 | /* Release resources, unregister device */ | |
59ac276f | 227 | nand_release(mtd_to_nand(cmx270_nand_mtd)); |
54d33c4c | 228 | |
70eb33d6 MR |
229 | gpio_free(GPIO_NAND_RB); |
230 | gpio_free(GPIO_NAND_CS); | |
231 | ||
54d33c4c MR |
232 | iounmap(cmx270_nand_io); |
233 | ||
2afd14f9 | 234 | kfree(mtd_to_nand(cmx270_nand_mtd)); |
54d33c4c MR |
235 | } |
236 | module_exit(cmx270_cleanup); | |
237 | ||
238 | MODULE_LICENSE("GPL"); | |
239 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); | |
240 | MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module"); |