]> git.sur5r.net Git - openocd/blob - src/flash/nand/nuc910.c
NAND/NUC910: remove private "target" copy
[openocd] / 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 {
36         struct arm_nand_data io;
37 };
38
39 static int validate_target_state(struct nand_device *nand)
40 {
41         struct target *target = nand->target;
42
43         if (target->state != TARGET_HALTED) {
44                 LOG_ERROR("Target not halted");
45                 return ERROR_NAND_OPERATION_FAILED;
46         }
47
48         return ERROR_OK;
49 }
50
51 static int nuc910_nand_command(struct nand_device *nand, uint8_t command)
52 {
53         struct target *target = nand->target;
54         int result;
55
56         if ((result = validate_target_state(nand)) != 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         if ((result = validate_target_state(nand)) != ERROR_OK)
69                 return result;
70
71         target_write_u32(target, NUC910_SMADDR, ((address & 0xff) | NUC910_SMADDR_EOA));
72         return ERROR_OK;
73 }
74
75 static int nuc910_nand_read(struct nand_device *nand, void *data)
76 {
77         struct target *target = nand->target;
78         int result;
79
80         if ((result = validate_target_state(nand)) != ERROR_OK)
81                 return result;
82
83         target_read_u8(target, NUC910_SMDATA, data);
84         return ERROR_OK;
85 }
86
87 static int nuc910_nand_write(struct nand_device *nand, uint16_t data)
88 {
89         struct target *target = nand->target;
90         int result;
91
92         if ((result = validate_target_state(nand)) != ERROR_OK)
93                 return result;
94
95         target_write_u8(target, NUC910_SMDATA, data);
96         return ERROR_OK;
97 }
98
99 static int nuc910_nand_read_block_data(struct nand_device *nand,
100                 uint8_t *data, int data_size)
101 {
102         struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
103         int result;
104
105         if ((result = validate_target_state(nand)) != ERROR_OK)
106                 return result;
107
108         nuc910_nand->io.chunk_size = nand->page_size;
109
110         /* try the fast way first */
111         result = arm_nandread(&nuc910_nand->io, data, data_size);
112         if (result != ERROR_NAND_NO_BUFFER)
113                 return result;
114
115         /* else do it slowly */
116         while (data_size--)
117                 nuc910_nand_read(nand, data++);
118
119         return ERROR_OK;
120 }
121
122 static int nuc910_nand_write_block_data(struct nand_device *nand,
123                 uint8_t *data, int data_size)
124 {
125         struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
126         int result;
127
128         if ((result = validate_target_state(nand)) != ERROR_OK)
129                 return result;
130
131         nuc910_nand->io.chunk_size = nand->page_size;
132
133         /* try the fast way first */
134         result = arm_nandwrite(&nuc910_nand->io, data, data_size);
135         if (result != ERROR_NAND_NO_BUFFER)
136                 return result;
137
138         /* else do it slowly */
139         while (data_size--)
140                 nuc910_nand_write(nand, *data++);
141
142         return ERROR_OK;
143 }
144
145 static int nuc910_nand_reset(struct nand_device *nand)
146 {
147         return nuc910_nand_command(nand, NAND_CMD_RESET);
148 }
149
150 static int nuc910_nand_ready(struct nand_device *nand, int timeout)
151 {
152         struct target *target = nand->target;
153         uint32_t status;
154
155         do {
156                 target_read_u32(target, NUC910_SMISR, &status);
157                 if (status & NUC910_SMISR_RB_) {
158                         return 1;
159                 }
160                 alive_sleep(1);
161         } while (timeout-- > 0);
162
163         return 0;
164 }
165
166 NAND_DEVICE_COMMAND_HANDLER(nuc910_nand_device_command)
167 {
168         struct nuc910_nand_controller *nuc910_nand;
169
170         nuc910_nand = calloc(1, sizeof(struct nuc910_nand_controller));
171         if (!nuc910_nand) {
172                 LOG_ERROR("no memory for nand controller\n");
173                 return ERROR_NAND_DEVICE_INVALID;
174         }
175
176         nand->controller_priv = nuc910_nand;
177         return ERROR_OK;
178 }
179
180 static int nuc910_nand_init(struct nand_device *nand)
181 {
182         struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
183         struct target *target = nand->target;
184         int bus_width = nand->bus_width ? : 8;
185         int result;
186
187         if ((result = validate_target_state(nand)) != ERROR_OK)
188                 return result;
189
190         /* nuc910 only supports 8bit */
191         if (bus_width != 8)
192         {
193                 LOG_ERROR("nuc910 only supports 8 bit bus width, not %i", bus_width);
194                 return ERROR_NAND_OPERATION_NOT_SUPPORTED;
195         }
196
197         /* inform calling code about selected bus width */
198         nand->bus_width = bus_width;
199
200         nuc910_nand->io.target = target;
201         nuc910_nand->io.data = NUC910_SMDATA;
202         nuc910_nand->io.op = ARM_NAND_NONE;
203
204         /* configure nand controller */
205         target_write_u32(target, NUC910_FMICSR, NUC910_FMICSR_SM_EN);
206         target_write_u32(target, NUC910_SMCSR, 0x010000a8);     /* 2048 page size */
207         target_write_u32(target, NUC910_SMTCR, 0x00010204);
208         target_write_u32(target, NUC910_SMIER, 0x00000000);
209
210         return ERROR_OK;
211 }
212
213 struct nand_flash_controller nuc910_nand_controller =
214 {
215         .name = "nuc910",
216         .command = nuc910_nand_command,
217         .address = nuc910_nand_address,
218         .read_data = nuc910_nand_read,
219         .write_data     = nuc910_nand_write,
220         .write_block_data = nuc910_nand_write_block_data,
221         .read_block_data = nuc910_nand_read_block_data,
222         .nand_ready = nuc910_nand_ready,
223         .reset = nuc910_nand_reset,
224         .nand_device_command = nuc910_nand_device_command,
225         .init = nuc910_nand_init,
226 };