832eeae418ac61c04586fb5c160410dd13f38110
[openocd.git] / src / flash / nand / nuc910.c
1 /***************************************************************************
2 * Copyright (C) 2010 by Spencer Oliver *
3 * spen@spen-soft.co.uk *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
20
21 /*
22 * NAND controller interface for Nuvoton NUC910
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "imp.h"
30 #include "nuc910.h"
31 #include "arm_io.h"
32 #include <target/arm.h>
33
34 struct nuc910_nand_controller {
35 struct arm_nand_data io;
36 };
37
38 static int validate_target_state(struct nand_device *nand)
39 {
40 struct target *target = nand->target;
41
42 if (target->state != TARGET_HALTED) {
43 LOG_ERROR("Target not halted");
44 return ERROR_NAND_OPERATION_FAILED;
45 }
46
47 return ERROR_OK;
48 }
49
50 static int nuc910_nand_command(struct nand_device *nand, uint8_t command)
51 {
52 struct target *target = nand->target;
53 int result;
54
55 result = validate_target_state(nand);
56 if (result != ERROR_OK)
57 return result;
58
59 target_write_u8(target, NUC910_SMCMD, command);
60 return ERROR_OK;
61 }
62
63 static int nuc910_nand_address(struct nand_device *nand, uint8_t address)
64 {
65 struct target *target = nand->target;
66 int result;
67
68 result = validate_target_state(nand);
69 if (result != ERROR_OK)
70 return result;
71
72 target_write_u32(target, NUC910_SMADDR, ((address & 0xff) | NUC910_SMADDR_EOA));
73 return ERROR_OK;
74 }
75
76 static int nuc910_nand_read(struct nand_device *nand, void *data)
77 {
78 struct target *target = nand->target;
79 int result;
80
81 result = validate_target_state(nand);
82 if (result != ERROR_OK)
83 return result;
84
85 target_read_u8(target, NUC910_SMDATA, data);
86 return ERROR_OK;
87 }
88
89 static int nuc910_nand_write(struct nand_device *nand, uint16_t data)
90 {
91 struct target *target = nand->target;
92 int result;
93
94 result = validate_target_state(nand);
95 if (result != ERROR_OK)
96 return result;
97
98 target_write_u8(target, NUC910_SMDATA, data);
99 return ERROR_OK;
100 }
101
102 static int nuc910_nand_read_block_data(struct nand_device *nand,
103 uint8_t *data, int data_size)
104 {
105 struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
106 int result;
107
108 result = validate_target_state(nand);
109 if (result != ERROR_OK)
110 return result;
111
112 nuc910_nand->io.chunk_size = nand->page_size;
113
114 /* try the fast way first */
115 result = arm_nandread(&nuc910_nand->io, data, data_size);
116 if (result != ERROR_NAND_NO_BUFFER)
117 return result;
118
119 /* else do it slowly */
120 while (data_size--)
121 nuc910_nand_read(nand, data++);
122
123 return ERROR_OK;
124 }
125
126 static int nuc910_nand_write_block_data(struct nand_device *nand,
127 uint8_t *data, int data_size)
128 {
129 struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
130 int result;
131
132 result = validate_target_state(nand);
133 if (result != ERROR_OK)
134 return result;
135
136 nuc910_nand->io.chunk_size = nand->page_size;
137
138 /* try the fast way first */
139 result = arm_nandwrite(&nuc910_nand->io, data, data_size);
140 if (result != ERROR_NAND_NO_BUFFER)
141 return result;
142
143 /* else do it slowly */
144 while (data_size--)
145 nuc910_nand_write(nand, *data++);
146
147 return ERROR_OK;
148 }
149
150 static int nuc910_nand_reset(struct nand_device *nand)
151 {
152 return nuc910_nand_command(nand, NAND_CMD_RESET);
153 }
154
155 static int nuc910_nand_ready(struct nand_device *nand, int timeout)
156 {
157 struct target *target = nand->target;
158 uint32_t status;
159
160 do {
161 target_read_u32(target, NUC910_SMISR, &status);
162 if (status & NUC910_SMISR_RB_)
163 return 1;
164 alive_sleep(1);
165 } while (timeout-- > 0);
166
167 return 0;
168 }
169
170 NAND_DEVICE_COMMAND_HANDLER(nuc910_nand_device_command)
171 {
172 struct nuc910_nand_controller *nuc910_nand;
173
174 nuc910_nand = calloc(1, sizeof(struct nuc910_nand_controller));
175 if (!nuc910_nand) {
176 LOG_ERROR("no memory for nand controller");
177 return ERROR_NAND_DEVICE_INVALID;
178 }
179
180 nand->controller_priv = nuc910_nand;
181 return ERROR_OK;
182 }
183
184 static int nuc910_nand_init(struct nand_device *nand)
185 {
186 struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
187 struct target *target = nand->target;
188 int bus_width = nand->bus_width ? : 8;
189 int result;
190
191 result = validate_target_state(nand);
192 if (result != ERROR_OK)
193 return result;
194
195 /* nuc910 only supports 8bit */
196 if (bus_width != 8) {
197 LOG_ERROR("nuc910 only supports 8 bit bus width, not %i", bus_width);
198 return ERROR_NAND_OPERATION_NOT_SUPPORTED;
199 }
200
201 /* inform calling code about selected bus width */
202 nand->bus_width = bus_width;
203
204 nuc910_nand->io.target = target;
205 nuc910_nand->io.data = NUC910_SMDATA;
206 nuc910_nand->io.op = ARM_NAND_NONE;
207
208 /* configure nand controller */
209 target_write_u32(target, NUC910_FMICSR, NUC910_FMICSR_SM_EN);
210 target_write_u32(target, NUC910_SMCSR, 0x010000a8); /* 2048 page size */
211 target_write_u32(target, NUC910_SMTCR, 0x00010204);
212 target_write_u32(target, NUC910_SMIER, 0x00000000);
213
214 return ERROR_OK;
215 }
216
217 struct nand_flash_controller nuc910_nand_controller = {
218 .name = "nuc910",
219 .command = nuc910_nand_command,
220 .address = nuc910_nand_address,
221 .read_data = nuc910_nand_read,
222 .write_data = nuc910_nand_write,
223 .write_block_data = nuc910_nand_write_block_data,
224 .read_block_data = nuc910_nand_read_block_data,
225 .nand_ready = nuc910_nand_ready,
226 .reset = nuc910_nand_reset,
227 .nand_device_command = nuc910_nand_device_command,
228 .init = nuc910_nand_init,
229 };