1 /***************************************************************************
2 * Copyright (C) 2009 Øyvind Harboe *
3 * oyvind.harboe@zylin.com *
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. *
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. *
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 ***************************************************************************/
25 #include <target/image.h>
29 struct faux_flash_bank
31 struct target
*target
;
33 uint32_t start_address
;
36 static const int sectorSize
= 0x10000;
39 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
41 FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command
)
43 struct faux_flash_bank
*info
;
47 LOG_WARNING("incomplete flash_bank faux configuration");
48 return ERROR_FLASH_BANK_INVALID
;
51 info
= malloc(sizeof(struct faux_flash_bank
));
54 LOG_ERROR("no memory for flash bank info");
58 struct flash_bank
*bank
= flash_bank_from_object(object
);
59 info
->memory
= malloc(bank
->size
);
63 LOG_ERROR("no memory for flash bank info");
66 set_flash_bank_data(bank
, info
);
68 /* Use 0x10000 as a fixed sector size. */
71 bank
->num_sectors
= bank
->size
/sectorSize
;
72 bank
->sectors
= malloc(sizeof(struct flash_sector
) * bank
->num_sectors
);
73 for (i
= 0; i
< bank
->num_sectors
; i
++)
75 bank
->sectors
[i
].offset
= offset
;
76 bank
->sectors
[i
].size
= sectorSize
;
77 offset
+= bank
->sectors
[i
].size
;
78 bank
->sectors
[i
].is_erased
= -1;
79 bank
->sectors
[i
].is_protected
= 0;
82 info
->target
= get_target(CMD_ARGV
[5]);
83 if (info
->target
== NULL
)
85 LOG_ERROR("target '%s' not defined", CMD_ARGV
[5]);
93 static struct faux_flash_bank
*faux_bank_data(struct flash_bank
*bank
)
95 return (struct faux_flash_bank
*)flash_bank_data(bank
);
98 static int faux_erase(struct flash_bank
*bank
, int first
, int last
)
100 struct faux_flash_bank
*info
= faux_bank_data(bank
);
101 memset(info
->memory
+ first
*sectorSize
, 0xff, sectorSize
*(last
-first
+ 1));
105 static int faux_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
107 LOG_USER("set protection sector %d to %d to %s", first
, last
, set
?"on":"off");
111 static int faux_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
113 struct faux_flash_bank
*info
= faux_bank_data(bank
);
114 memcpy(info
->memory
+ offset
, buffer
, count
);
118 static int faux_protect_check(struct flash_bank
*bank
)
123 static int faux_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
125 snprintf(buf
, buf_size
, "faux flash driver");
129 static int faux_probe(struct flash_bank
*bank
)
134 static const struct command_registration faux_command_handlers
[] = {
138 .help
= "faux flash command group",
139 .chain
= hello_command_handlers
,
141 COMMAND_REGISTRATION_DONE
144 FLASH_DRIVER(faux
, &faux_flash_bank_command
, faux_command_handlers
,
145 .erase
= &faux_erase
,
146 .protect
= &faux_protect
,
147 .write
= &faux_write
,
148 .probe
= &faux_probe
,
149 .auto_probe
= &faux_probe
,
150 .erase_check
= &default_flash_blank_check
,
151 .protect_check
= &faux_protect_check
,