openocd: src: fix incorrect SPDX tags
[fw/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, see <http://www.gnu.org/licenses/>. *
17  ***************************************************************************/
18
19 /*
20  * NAND controller interface for Nuvoton NUC910
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "imp.h"
28 #include "nuc910.h"
29 #include "arm_io.h"
30 #include <target/arm.h>
31
32 struct nuc910_nand_controller {
33         struct arm_nand_data io;
34 };
35
36 static int validate_target_state(struct nand_device *nand)
37 {
38         struct target *target = nand->target;
39
40         if (target->state != TARGET_HALTED) {
41                 LOG_ERROR("Target not halted");
42                 return ERROR_NAND_OPERATION_FAILED;
43         }
44
45         return ERROR_OK;
46 }
47
48 static int nuc910_nand_command(struct nand_device *nand, uint8_t command)
49 {
50         struct target *target = nand->target;
51         int result;
52
53         result = validate_target_state(nand);
54         if (result != ERROR_OK)
55                 return result;
56
57         target_write_u8(target, NUC910_SMCMD, command);
58         return ERROR_OK;
59 }
60
61 static int nuc910_nand_address(struct nand_device *nand, uint8_t address)
62 {
63         struct target *target = nand->target;
64         int result;
65
66         result = validate_target_state(nand);
67         if (result != ERROR_OK)
68                 return result;
69
70         target_write_u32(target, NUC910_SMADDR, ((address & 0xff) | NUC910_SMADDR_EOA));
71         return ERROR_OK;
72 }
73
74 static int nuc910_nand_read(struct nand_device *nand, void *data)
75 {
76         struct target *target = nand->target;
77         int result;
78
79         result = validate_target_state(nand);
80         if (result != 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         result = validate_target_state(nand);
93         if (result != ERROR_OK)
94                 return result;
95
96         target_write_u8(target, NUC910_SMDATA, data);
97         return ERROR_OK;
98 }
99
100 static int nuc910_nand_read_block_data(struct nand_device *nand,
101                 uint8_t *data, int data_size)
102 {
103         struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
104         int result;
105
106         result = validate_target_state(nand);
107         if (result != ERROR_OK)
108                 return result;
109
110         nuc910_nand->io.chunk_size = nand->page_size;
111
112         /* try the fast way first */
113         result = arm_nandread(&nuc910_nand->io, data, data_size);
114         if (result != ERROR_NAND_NO_BUFFER)
115                 return result;
116
117         /* else do it slowly */
118         while (data_size--)
119                 nuc910_nand_read(nand, data++);
120
121         return ERROR_OK;
122 }
123
124 static int nuc910_nand_write_block_data(struct nand_device *nand,
125                 uint8_t *data, int data_size)
126 {
127         struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
128         int result;
129
130         result = validate_target_state(nand);
131         if (result != ERROR_OK)
132                 return result;
133
134         nuc910_nand->io.chunk_size = nand->page_size;
135
136         /* try the fast way first */
137         result = arm_nandwrite(&nuc910_nand->io, data, data_size);
138         if (result != ERROR_NAND_NO_BUFFER)
139                 return result;
140
141         /* else do it slowly */
142         while (data_size--)
143                 nuc910_nand_write(nand, *data++);
144
145         return ERROR_OK;
146 }
147
148 static int nuc910_nand_reset(struct nand_device *nand)
149 {
150         return nuc910_nand_command(nand, NAND_CMD_RESET);
151 }
152
153 static int nuc910_nand_ready(struct nand_device *nand, int timeout)
154 {
155         struct target *target = nand->target;
156         uint32_t status;
157
158         do {
159                 target_read_u32(target, NUC910_SMISR, &status);
160                 if (status & NUC910_SMISR_RB_)
161                         return 1;
162                 alive_sleep(1);
163         } while (timeout-- > 0);
164
165         return 0;
166 }
167
168 NAND_DEVICE_COMMAND_HANDLER(nuc910_nand_device_command)
169 {
170         struct nuc910_nand_controller *nuc910_nand;
171
172         nuc910_nand = calloc(1, sizeof(struct nuc910_nand_controller));
173         if (!nuc910_nand) {
174                 LOG_ERROR("no memory for nand controller");
175                 return ERROR_NAND_DEVICE_INVALID;
176         }
177
178         nand->controller_priv = nuc910_nand;
179         return ERROR_OK;
180 }
181
182 static int nuc910_nand_init(struct nand_device *nand)
183 {
184         struct nuc910_nand_controller *nuc910_nand = nand->controller_priv;
185         struct target *target = nand->target;
186         int bus_width = nand->bus_width ? nand->bus_width : 8;
187         int result;
188
189         result = validate_target_state(nand);
190         if (result != ERROR_OK)
191                 return result;
192
193         /* nuc910 only supports 8bit */
194         if (bus_width != 8) {
195                 LOG_ERROR("nuc910 only supports 8 bit bus width, not %i", bus_width);
196                 return ERROR_NAND_OPERATION_NOT_SUPPORTED;
197         }
198
199         /* inform calling code about selected bus width */
200         nand->bus_width = bus_width;
201
202         nuc910_nand->io.target = target;
203         nuc910_nand->io.data = NUC910_SMDATA;
204         nuc910_nand->io.op = ARM_NAND_NONE;
205
206         /* configure nand controller */
207         target_write_u32(target, NUC910_FMICSR, NUC910_FMICSR_SM_EN);
208         target_write_u32(target, NUC910_SMCSR, 0x010000a8);     /* 2048 page size */
209         target_write_u32(target, NUC910_SMTCR, 0x00010204);
210         target_write_u32(target, NUC910_SMIER, 0x00000000);
211
212         return ERROR_OK;
213 }
214
215 struct nand_flash_controller nuc910_nand_controller = {
216         .name = "nuc910",
217         .command = nuc910_nand_command,
218         .address = nuc910_nand_address,
219         .read_data = nuc910_nand_read,
220         .write_data     = nuc910_nand_write,
221         .write_block_data = nuc910_nand_write_block_data,
222         .read_block_data = nuc910_nand_read_block_data,
223         .nand_ready = nuc910_nand_ready,
224         .reset = nuc910_nand_reset,
225         .nand_device_command = nuc910_nand_device_command,
226         .init = nuc910_nand_init,
227 };