Commit 1a37889b0ad084a740b4f785031d7ae9955d947b

Authored by Marek Vasut
Committed by Tom Rini
1 parent 39b6f98bd5

eeprom: Pull out the RW loop

Unify the code for doing read/write into single function, since the
code for both the read and write is almost identical. This again
trims down the code duplication.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Simon Glass <sjg@chromium.org>
Cc: Tom Rini <trini@konsulko.com>
Cc: Heiko Schocher <hs@denx.de>
Reviewed-by: Heiko Schocher <hs@denx.de>

Showing 1 changed file with 21 additions and 30 deletions Side-by-side Diff

... ... @@ -146,39 +146,45 @@
146 146 return ret;
147 147 }
148 148  
149   -int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  149 +static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
  150 + unsigned cnt, bool read)
150 151 {
151 152 unsigned end = offset + cnt;
  153 + unsigned alen, len;
152 154 int rcode = 0;
153 155 uchar addr[3];
154 156  
155   - /*
156   - * Read data until done or would cross a page boundary.
157   - * We must write the address again when changing pages
158   - * because the next page may be in a different device.
159   - */
160 157 while (offset < end) {
161   - unsigned alen, len;
162   -
163 158 alen = eeprom_addr(dev_addr, offset, addr);
164 159  
165 160 len = eeprom_len(offset, end);
166 161  
167   - rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
  162 + rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
168 163  
169 164 buffer += len;
170 165 offset += len;
  166 +
  167 + if (!read)
  168 + udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
171 169 }
172 170  
173 171 return rcode;
174 172 }
175 173  
176   -int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  174 +int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
177 175 {
178   - unsigned end = offset + cnt;
179   - int rcode = 0;
180   - uchar addr[3];
  176 + /*
  177 + * Read data until done or would cross a page boundary.
  178 + * We must write the address again when changing pages
  179 + * because the next page may be in a different device.
  180 + */
  181 + return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
  182 +}
181 183  
  184 +int eeprom_write(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  185 +{
  186 + int ret;
  187 +
182 188 eeprom_write_enable(dev_addr, 1);
183 189  
184 190 /*
185 191  
186 192  
... ... @@ -186,25 +192,10 @@
186 192 * We must write the address again when changing pages
187 193 * because the address counter only increments within a page.
188 194 */
  195 + ret = eeprom_rw(dev_addr, offset, buffer, cnt, 1);
189 196  
190   - while (offset < end) {
191   - unsigned alen, len;
192   -
193   - alen = eeprom_addr(dev_addr, offset, addr);
194   -
195   - len = eeprom_len(offset, end);
196   -
197   - rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);
198   -
199   - buffer += len;
200   - offset += len;
201   -
202   - udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
203   - }
204   -
205 197 eeprom_write_enable(dev_addr, 0);
206   -
207   - return rcode;
  198 + return ret;
208 199 }
209 200  
210 201 static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])