- renamed x7926 driver to aduc702x to match other flash drivers
[fw/openocd] / src / flash / aduc702x.c
1 /***************************************************************************
2  *   Copyright (C) 2008 by Kevin McGuire                                   *
3  *   Copyright (C) 2008 by Marcel Wijlaars                                 *
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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "replacements.h"
26
27 #include "flash.h"
28 #include "target.h"
29 #include "log.h"
30 #include "armv4_5.h"
31 #include "algorithm.h"
32 #include "binarybuffer.h"
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 int aduc702x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
39 int aduc702x_register_commands(struct command_context_s *cmd_ctx);
40 int aduc702x_erase(struct flash_bank_s *bank, int first, int last);
41 int aduc702x_protect(struct flash_bank_s *bank, int set, int first, int last);
42 int aduc702x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
43 int aduc702x_probe(struct flash_bank_s *bank);
44 int aduc702x_info(struct flash_bank_s *bank, char *buf, int buf_size);
45 int aduc702x_protect_check(struct flash_bank_s *bank);
46
47 #define ADUC702x_FLASH                          0xfffff800
48 #define ADUC702x_FLASH_FEESTA           (0*4)
49 #define ADUC702x_FLASH_FEEMOD           (1*4)
50 #define ADUC702x_FLASH_FEECON           (2*4)
51 #define ADUC702x_FLASH_FEEDAT           (3*4)
52 #define ADUC702x_FLASH_FEEADR           (4*4)
53 #define ADUC702x_FLASH_FEESIGN          (5*4)
54 #define ADUC702x_FLASH_FEEPRO           (6*4)
55 #define ADUC702x_FLASH_FEEHIDE          (7*4)
56
57 typedef struct {
58         u32 feesta;
59         u32 feemod;
60         u32 feecon;
61         u32 feedat;
62         u32 feeadr;
63         u32 feesign;
64         u32 feepro;
65         u32 feehide;
66 } ADUC702x_FLASH_MMIO;
67
68 typedef struct
69 {
70         unsigned char tmp;
71 } aduc702x_bank;
72
73 flash_driver_t aduc702x_flash =
74 {
75         .name = "aduc702x",
76         .register_commands = aduc702x_register_commands,
77         .flash_bank_command = aduc702x_flash_bank_command,
78         .erase = aduc702x_erase,
79         .protect = aduc702x_protect,
80         .write = aduc702x_write,
81         .probe = aduc702x_probe,
82         .auto_probe = aduc702x_probe,
83         .erase_check = default_flash_blank_check,
84         .protect_check = aduc702x_protect_check,
85         .info = aduc702x_info
86 };
87
88 int aduc702x_register_commands(struct command_context_s *cmd_ctx)
89 {
90         return ERROR_OK;
91 }
92
93 /* flash bank aduc702x <base> <size> 0 0 <target#>  */
94 int aduc702x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
95 {
96         aduc702x_bank *nbank;
97
98         if (argc < 6)
99         {
100                 LOG_WARNING("incomplete flash_bank aduc702x configuration");
101                 return ERROR_FLASH_BANK_INVALID;
102         }
103
104         nbank = malloc(sizeof(aduc702x_bank));
105         /* just warn that we are used to normally using 0x80000 */
106         if (bank->base != 0x80000)
107         {
108                 LOG_WARNING("Default base address is 0x80000 on the ADuC7026!");
109         }
110         
111         nbank->tmp = 1;
112         bank->driver_priv = nbank;
113         return ERROR_OK;
114 }
115
116 int aduc702x_protect_check(struct flash_bank_s *bank)
117 {
118         printf("aduc702x_protect_check not implemented yet.\n");
119         return ERROR_OK;
120 }
121
122 int aduc702x_erase(struct flash_bank_s *bank, int first, int last)
123 {
124         int x;
125         int count;
126         u32 v;
127         target_t *target = bank->target;
128
129         /* mass erase */
130         if ((first | last) == 0)
131         {
132                 LOG_DEBUG("performing mass erase.\n");
133                 target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEEDAT, 0x3cff);
134                 target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEEADR, 0xffc3);
135                 target_read_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEEMOD, &v);
136                 target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEEMOD, v | 0x8);
137                 target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEECON, 0x06);
138                 for (v = 0x4; v & 0x4;
139                         target_read_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEESTA, &v));
140
141                 if (!(v & 0x1))
142                 {
143                         LOG_ERROR("mass erase failed.\n");
144                         return ERROR_FLASH_SECTOR_NOT_ERASED;
145                 }
146                 LOG_DEBUG("mass erase successful.\n");
147                 return ERROR_OK;
148         }
149
150         count = last - first;
151         for (x = 0; x < count; ++x)
152         {
153                 target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEEADR, bank->base + first * 512 + x * 512);
154                 target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEECON, 0x05);
155                 for (v = 0x4; v & 0x4; target_read_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEESTA, &v));
156                 if (!(v & 0x1))
157                 {
158                         LOG_ERROR("erase failed for page address %x\n", bank->base + first * 512 + x * 512);
159                         return ERROR_FLASH_SECTOR_NOT_ERASED;
160                 }
161                 LOG_DEBUG("erased page address %x\n", bank->base + first * 512 + x * 512);
162         }
163         return ERROR_OK;
164 }
165
166 int aduc702x_protect(struct flash_bank_s *bank, int set, int first, int last)
167 {
168         printf("aduc702x_protect not implemented yet.\n");
169         return ERROR_FLASH_OPERATION_FAILED;
170 }
171
172 int aduc702x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
173 {
174         int x;
175         u32     v;
176         target_t *target = bank->target;
177         
178         for (x = 0; x < count; x += 2)
179         {
180                 target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEEADR, offset + x);
181                 /* if we able to encounter a single byte instead of a word */
182                 if ((x + 1) == count) {
183                         target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEEDAT, buffer[x]);
184                 }
185                 else {
186                         target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEEDAT, buffer[x] | (buffer[x+1] << 8));
187                 }
188                 target_write_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEECON, 0x02);
189                 for (v = 0x4; v & 0x4; target_read_u32(target, ADUC702x_FLASH + ADUC702x_FLASH_FEESTA, &v));
190                 if (!(v & 0x1))
191                 {
192                         LOG_ERROR("single write failed for address %x.\n", offset + x);
193                         return ERROR_FLASH_OPERATION_FAILED;
194                 }
195                 LOG_DEBUG("single write for address %x.\n", offset + x);
196         }
197         return ERROR_OK;
198 }
199
200 int aduc702x_probe(struct flash_bank_s *bank)
201 {
202         return ERROR_OK;
203 }
204
205 int aduc702x_info(struct flash_bank_s *bank, char *buf, int buf_size)
206 {
207         snprintf(buf, buf_size, "aduc702x flash driver info" );
208         return ERROR_OK;
209 }