7f446758b9ebadc03debe754cef289503c87d87b
[openwrt.git] / package / boot / uboot-lantiq / patches / 0003-sf-factor-out-malloc-from-SPI-flash-drivers.patch
1 From 73d127565b5a4b19bcaacabc505689ee039f16fd Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Sun, 11 Nov 2012 03:11:38 +0100
4 Subject: sf: factor out malloc from SPI flash drivers
5
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
7
8 --- a/drivers/mtd/spi/atmel.c
9 +++ b/drivers/mtd/spi/atmel.c
10 @@ -40,18 +40,6 @@ struct atmel_spi_flash_params {
11         const char      *name;
12  };
13  
14 -/* spi_flash needs to be first so upper layers can free() it */
15 -struct atmel_spi_flash {
16 -       struct spi_flash flash;
17 -       const struct atmel_spi_flash_params *params;
18 -};
19 -
20 -static inline struct atmel_spi_flash *
21 -to_atmel_spi_flash(struct spi_flash *flash)
22 -{
23 -       return container_of(flash, struct atmel_spi_flash, flash);
24 -}
25 -
26  static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
27         {
28                 .idcode1                = 0x22,
29 @@ -156,7 +144,8 @@ static int at45_wait_ready(struct spi_fl
30   * Assemble the address part of a command for AT45 devices in
31   * non-power-of-two page size mode.
32   */
33 -static void at45_build_address(struct atmel_spi_flash *asf, u8 *cmd, u32 offset)
34 +static void at45_build_address(const struct atmel_spi_flash_params *params,
35 +                               u8 *cmd, u32 offset)
36  {
37         unsigned long page_addr;
38         unsigned long byte_addr;
39 @@ -167,7 +156,7 @@ static void at45_build_address(struct at
40          * The "extra" space per page is the power-of-two page size
41          * divided by 32.
42          */
43 -       page_shift = asf->params->l2_page_size;
44 +       page_shift = params->l2_page_size;
45         page_size = (1 << page_shift) + (1 << (page_shift - 5));
46         page_shift++;
47         page_addr = offset / page_size;
48 @@ -181,11 +170,11 @@ static void at45_build_address(struct at
49  static int dataflash_read_fast_at45(struct spi_flash *flash,
50                 u32 offset, size_t len, void *buf)
51  {
52 -       struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
53 +       const struct atmel_spi_flash_params *params = flash->priv;
54         u8 cmd[5];
55  
56         cmd[0] = CMD_READ_ARRAY_FAST;
57 -       at45_build_address(asf, cmd + 1, offset);
58 +       at45_build_address(params, cmd + 1, offset);
59         cmd[4] = 0x00;
60  
61         return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
62 @@ -197,7 +186,7 @@ static int dataflash_read_fast_at45(stru
63  static int dataflash_write_p2(struct spi_flash *flash,
64                 u32 offset, size_t len, const void *buf)
65  {
66 -       struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
67 +       const struct atmel_spi_flash_params *params = flash->priv;
68         unsigned long page_size;
69         u32 addr = offset;
70         size_t chunk_len;
71 @@ -211,7 +200,7 @@ static int dataflash_write_p2(struct spi
72          * the other is being programmed into main memory.
73          */
74  
75 -       page_size = (1 << asf->params->l2_page_size);
76 +       page_size = (1 << params->l2_page_size);
77  
78         ret = spi_claim_bus(flash->spi);
79         if (ret) {
80 @@ -263,7 +252,7 @@ out:
81  static int dataflash_write_at45(struct spi_flash *flash,
82                 u32 offset, size_t len, const void *buf)
83  {
84 -       struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
85 +       const struct atmel_spi_flash_params *params = flash->priv;
86         unsigned long page_addr;
87         unsigned long byte_addr;
88         unsigned long page_size;
89 @@ -279,7 +268,7 @@ static int dataflash_write_at45(struct s
90          * the other is being programmed into main memory.
91          */
92  
93 -       page_shift = asf->params->l2_page_size;
94 +       page_shift = params->l2_page_size;
95         page_size = (1 << page_shift) + (1 << (page_shift - 5));
96         page_shift++;
97         page_addr = offset / page_size;
98 @@ -338,7 +327,7 @@ out:
99   */
100  static int dataflash_erase_p2(struct spi_flash *flash, u32 offset, size_t len)
101  {
102 -       struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
103 +       const struct atmel_spi_flash_params *params = flash->priv;
104         unsigned long page_size;
105  
106         size_t actual;
107 @@ -351,7 +340,7 @@ static int dataflash_erase_p2(struct spi
108          * when possible.
109          */
110  
111 -       page_size = (1 << asf->params->l2_page_size);
112 +       page_size = (1 << params->l2_page_size);
113  
114         if (offset % page_size || len % page_size) {
115                 debug("SF: Erase offset/length not multiple of page size\n");
116 @@ -397,7 +386,7 @@ out:
117  
118  static int dataflash_erase_at45(struct spi_flash *flash, u32 offset, size_t len)
119  {
120 -       struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
121 +       const struct atmel_spi_flash_params *params = flash->priv;
122         unsigned long page_addr;
123         unsigned long page_size;
124         unsigned int page_shift;
125 @@ -411,7 +400,7 @@ static int dataflash_erase_at45(struct s
126          * when possible.
127          */
128  
129 -       page_shift = asf->params->l2_page_size;
130 +       page_shift = params->l2_page_size;
131         page_size = (1 << page_shift) + (1 << (page_shift - 5));
132         page_shift++;
133         page_addr = offset / page_size;
134 @@ -458,12 +447,12 @@ out:
135         return ret;
136  }
137  
138 -struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode)
139 +int spi_flash_probe_atmel(struct spi_flash *flash, u8 *idcode)
140  {
141         const struct atmel_spi_flash_params *params;
142 +       struct spi_slave *spi = flash->spi;
143         unsigned page_size;
144         unsigned int family;
145 -       struct atmel_spi_flash *asf;
146         unsigned int i;
147         int ret;
148         u8 status;
149 @@ -477,18 +466,11 @@ struct spi_flash *spi_flash_probe_atmel(
150         if (i == ARRAY_SIZE(atmel_spi_flash_table)) {
151                 debug("SF: Unsupported DataFlash ID %02x\n",
152                                 idcode[1]);
153 -               return NULL;
154 -       }
155 -
156 -       asf = malloc(sizeof(struct atmel_spi_flash));
157 -       if (!asf) {
158 -               debug("SF: Failed to allocate memory\n");
159 -               return NULL;
160 +               return 0;
161         }
162  
163 -       asf->params = params;
164 -       asf->flash.spi = spi;
165 -       asf->flash.name = params->name;
166 +       flash->priv = (void *)params;
167 +       flash->name = params->name;
168  
169         /* Assuming power-of-two page size initially. */
170         page_size = 1 << params->l2_page_size;
171 @@ -503,48 +485,44 @@ struct spi_flash *spi_flash_probe_atmel(
172                  */
173                 ret = spi_flash_cmd(spi, CMD_AT45_READ_STATUS, &status, 1);
174                 if (ret)
175 -                       goto err;
176 +                       return -1;
177  
178                 debug("SF: AT45 status register: %02x\n", status);
179  
180                 if (!(status & AT45_STATUS_P2_PAGE_SIZE)) {
181 -                       asf->flash.read = dataflash_read_fast_at45;
182 -                       asf->flash.write = dataflash_write_at45;
183 -                       asf->flash.erase = dataflash_erase_at45;
184 +                       flash->read = dataflash_read_fast_at45;
185 +                       flash->write = dataflash_write_at45;
186 +                       flash->erase = dataflash_erase_at45;
187                         page_size += 1 << (params->l2_page_size - 5);
188                 } else {
189 -                       asf->flash.read = spi_flash_cmd_read_fast;
190 -                       asf->flash.write = dataflash_write_p2;
191 -                       asf->flash.erase = dataflash_erase_p2;
192 +                       flash->read = spi_flash_cmd_read_fast;
193 +                       flash->write = dataflash_write_p2;
194 +                       flash->erase = dataflash_erase_p2;
195                 }
196  
197 -               asf->flash.page_size = page_size;
198 -               asf->flash.sector_size = page_size;
199 +               flash->page_size = page_size;
200 +               flash->sector_size = page_size;
201                 break;
202  
203         case DF_FAMILY_AT26F:
204         case DF_FAMILY_AT26DF:
205 -               asf->flash.read = spi_flash_cmd_read_fast;
206 -               asf->flash.write = spi_flash_cmd_write_multi;
207 -               asf->flash.erase = spi_flash_cmd_erase;
208 -               asf->flash.page_size = page_size;
209 -               asf->flash.sector_size = 4096;
210 +               flash->read = spi_flash_cmd_read_fast;
211 +               flash->write = spi_flash_cmd_write_multi;
212 +               flash->erase = spi_flash_cmd_erase;
213 +               flash->page_size = page_size;
214 +               flash->sector_size = 4096;
215                 /* clear SPRL# bit for locked flash */
216 -               spi_flash_cmd_write_status(&asf->flash, 0);
217 +               spi_flash_cmd_write_status(flash, 0);
218                 break;
219  
220         default:
221                 debug("SF: Unsupported DataFlash family %u\n", family);
222 -               goto err;
223 +               return -1;
224         }
225  
226 -       asf->flash.size = page_size * params->pages_per_block
227 +       flash->size = page_size * params->pages_per_block
228                                 * params->blocks_per_sector
229                                 * params->nr_sectors;
230  
231 -       return &asf->flash;
232 -
233 -err:
234 -       free(asf);
235 -       return NULL;
236 +       return 1;
237  }
238 --- a/drivers/mtd/spi/eon.c
239 +++ b/drivers/mtd/spi/eon.c
240 @@ -29,10 +29,9 @@ static const struct eon_spi_flash_params
241         },
242  };
243  
244 -struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
245 +int spi_flash_probe_eon(struct spi_flash *flash, u8 *idcode)
246  {
247         const struct eon_spi_flash_params *params;
248 -       struct spi_flash *flash;
249         unsigned int i;
250  
251         for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
252 @@ -43,16 +42,10 @@ struct spi_flash *spi_flash_probe_eon(st
253  
254         if (i == ARRAY_SIZE(eon_spi_flash_table)) {
255                 debug("SF: Unsupported EON ID %02x\n", idcode[1]);
256 -               return NULL;
257 +               return 0;
258         }
259  
260 -       flash = malloc(sizeof(*flash));
261 -       if (!flash) {
262 -               debug("SF: Failed to allocate memory\n");
263 -               return NULL;
264 -       }
265 -
266 -       flash->spi = spi;
267 +       flash->priv = (void *)params;
268         flash->name = params->name;
269  
270         flash->write = spi_flash_cmd_write_multi;
271 @@ -63,5 +56,5 @@ struct spi_flash *spi_flash_probe_eon(st
272         flash->size = 256 * 16
273             * params->nr_sectors;
274  
275 -       return flash;
276 +       return 1;
277  }
278 --- a/drivers/mtd/spi/macronix.c
279 +++ b/drivers/mtd/spi/macronix.c
280 @@ -79,10 +79,9 @@ static const struct macronix_spi_flash_p
281         },
282  };
283  
284 -struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
285 +int spi_flash_probe_macronix(struct spi_flash *flash, u8 *idcode)
286  {
287         const struct macronix_spi_flash_params *params;
288 -       struct spi_flash *flash;
289         unsigned int i;
290         u16 id = idcode[2] | idcode[1] << 8;
291  
292 @@ -94,16 +93,10 @@ struct spi_flash *spi_flash_probe_macron
293  
294         if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
295                 debug("SF: Unsupported Macronix ID %04x\n", id);
296 -               return NULL;
297 +               return 0;
298         }
299  
300 -       flash = malloc(sizeof(*flash));
301 -       if (!flash) {
302 -               debug("SF: Failed to allocate memory\n");
303 -               return NULL;
304 -       }
305 -
306 -       flash->spi = spi;
307 +       flash->priv = (void *)params;
308         flash->name = params->name;
309  
310         flash->write = spi_flash_cmd_write_multi;
311 @@ -116,5 +109,5 @@ struct spi_flash *spi_flash_probe_macron
312         /* Clear BP# bits for read-only flash */
313         spi_flash_cmd_write_status(flash, 0);
314  
315 -       return flash;
316 +       return 1;
317  }
318 --- a/drivers/mtd/spi/ramtron.c
319 +++ b/drivers/mtd/spi/ramtron.c
320 @@ -69,17 +69,6 @@ struct ramtron_spi_fram_params {
321         const char *name;       /* name for display and/or matching */
322  };
323  
324 -struct ramtron_spi_fram {
325 -       struct spi_flash flash;
326 -       const struct ramtron_spi_fram_params *params;
327 -};
328 -
329 -static inline struct ramtron_spi_fram *to_ramtron_spi_fram(struct spi_flash
330 -                                                            *flash)
331 -{
332 -       return container_of(flash, struct ramtron_spi_fram, flash);
333 -}
334 -
335  /*
336   * table describing supported FRAM chips:
337   * chips without RDID command must have the values 0xff for id1 and id2
338 @@ -155,18 +144,18 @@ static const struct ramtron_spi_fram_par
339  static int ramtron_common(struct spi_flash *flash,
340                 u32 offset, size_t len, void *buf, u8 command)
341  {
342 -       struct ramtron_spi_fram *sn = to_ramtron_spi_fram(flash);
343 +       const struct ramtron_spi_fram_params *params = flash->priv;
344         u8 cmd[4];
345         int cmd_len;
346         int ret;
347  
348 -       if (sn->params->addr_len == 3 && sn->params->merge_cmd == 0) {
349 +       if (params->addr_len == 3 && params->merge_cmd == 0) {
350                 cmd[0] = command;
351                 cmd[1] = offset >> 16;
352                 cmd[2] = offset >> 8;
353                 cmd[3] = offset;
354                 cmd_len = 4;
355 -       } else if (sn->params->addr_len == 2 && sn->params->merge_cmd == 0) {
356 +       } else if (params->addr_len == 2 && params->merge_cmd == 0) {
357                 cmd[0] = command;
358                 cmd[1] = offset >> 8;
359                 cmd[2] = offset;
360 @@ -230,10 +219,9 @@ static int ramtron_erase(struct spi_flas
361   * nore: we are called here with idcode pointing to the first non-0x7f byte
362   * already!
363   */
364 -struct spi_flash *spi_fram_probe_ramtron(struct spi_slave *spi, u8 *idcode)
365 +int spi_fram_probe_ramtron(struct spi_flash *flash, u8 *idcode)
366  {
367         const struct ramtron_spi_fram_params *params;
368 -       struct ramtron_spi_fram *sn;
369         unsigned int i;
370  #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
371         int ret;
372 @@ -259,11 +247,11 @@ struct spi_flash *spi_fram_probe_ramtron
373                  */
374                 ret = spi_flash_cmd(spi, CMD_READ_STATUS, &sr, 1);
375                 if (ret)
376 -                       return NULL;
377 +                       return 0;
378  
379                 /* Bits 5,4,0 are fixed 0 for all devices */
380                 if ((sr & 0x31) != 0x00)
381 -                       return NULL;
382 +                       return 0;
383                 /* now find the device */
384                 for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
385                         params = &ramtron_spi_fram_table[i];
386 @@ -281,23 +269,16 @@ struct spi_flash *spi_fram_probe_ramtron
387         /* arriving here means no method has found a device we can handle */
388         debug("SF/ramtron: unsupported device id0=%02x id1=%02x id2=%02x\n",
389                 idcode[0], idcode[1], idcode[2]);
390 -       return NULL;
391 +       return 0;
392  
393  found:
394 -       sn = malloc(sizeof(*sn));
395 -       if (!sn) {
396 -               debug("SF: Failed to allocate memory\n");
397 -               return NULL;
398 -       }
399 +       flash->priv = (void *)params;
400 +       flash->name = params->name;
401  
402 -       sn->params = params;
403 -       sn->flash.spi = spi;
404 -       sn->flash.name = params->name;
405 -
406 -       sn->flash.write = ramtron_write;
407 -       sn->flash.read = ramtron_read;
408 -       sn->flash.erase = ramtron_erase;
409 -       sn->flash.size = params->size;
410 +       flash->write = ramtron_write;
411 +       flash->read = ramtron_read;
412 +       flash->erase = ramtron_erase;
413 +       flash->size = params->size;
414  
415 -       return &sn->flash;
416 +       return 1;
417  }
418 --- a/drivers/mtd/spi/spansion.c
419 +++ b/drivers/mtd/spi/spansion.c
420 @@ -105,10 +105,9 @@ static const struct spansion_spi_flash_p
421         },
422  };
423  
424 -struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
425 +int spi_flash_probe_spansion(struct spi_flash *flash, u8 *idcode)
426  {
427         const struct spansion_spi_flash_params *params;
428 -       struct spi_flash *flash;
429         unsigned int i;
430         unsigned short jedec, ext_jedec;
431  
432 @@ -125,16 +124,10 @@ struct spi_flash *spi_flash_probe_spansi
433  
434         if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
435                 debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
436 -               return NULL;
437 +               return 0;
438         }
439  
440 -       flash = malloc(sizeof(*flash));
441 -       if (!flash) {
442 -               debug("SF: Failed to allocate memory\n");
443 -               return NULL;
444 -       }
445 -
446 -       flash->spi = spi;
447 +       flash->priv = (void *)params;
448         flash->name = params->name;
449  
450         flash->write = spi_flash_cmd_write_multi;
451 @@ -144,5 +137,5 @@ struct spi_flash *spi_flash_probe_spansi
452         flash->sector_size = 256 * params->pages_per_sector;
453         flash->size = flash->sector_size * params->nr_sectors;
454  
455 -       return flash;
456 +       return 1;
457  }
458 --- a/drivers/mtd/spi/spi_flash.c
459 +++ b/drivers/mtd/spi/spi_flash.c
460 @@ -296,7 +296,7 @@ int spi_flash_cmd_write_status(struct sp
461  static struct {
462         const u8 shift;
463         const u8 idcode;
464 -       struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
465 +       int (*probe) (struct spi_flash *flash, u8 *idcode);
466  } flashes[] = {
467         /* Keep it sorted by define name */
468  #ifdef CONFIG_SPI_FLASH_ATMEL
469 @@ -343,7 +343,7 @@ struct spi_flash *spi_flash_probe(unsign
470                 unsigned int max_hz, unsigned int spi_mode)
471  {
472         struct spi_slave *spi;
473 -       struct spi_flash *flash = NULL;
474 +       struct spi_flash *flash;
475         int ret, i, shift;
476         u8 idcode[IDCODE_LEN], *idp;
477  #ifdef CONFIG_NEEDS_MANUAL_RELOC
478 @@ -379,6 +379,15 @@ struct spi_flash *spi_flash_probe(unsign
479         print_buffer(0, idcode, 1, sizeof(idcode), 0);
480  #endif
481  
482 +       flash = malloc(sizeof(*flash));
483 +       if (!flash) {
484 +               debug("SF: failed to alloc memory\n");
485 +               goto err_malloc;
486 +       }
487 +
488 +       memset(flash, 0, sizeof(*flash));
489 +       flash->spi = spi;
490 +
491         /* count the number of continuation bytes */
492         for (shift = 0, idp = idcode;
493              shift < IDCODE_CONT_LEN && *idp == 0x7f;
494 @@ -389,12 +398,12 @@ struct spi_flash *spi_flash_probe(unsign
495         for (i = 0; i < ARRAY_SIZE(flashes); ++i)
496                 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
497                         /* we have a match, call probe */
498 -                       flash = flashes[i].probe(spi, idp);
499 -                       if (flash)
500 +                       ret = flashes[i].probe(flash, idp);
501 +                       if (ret)
502                                 break;
503                 }
504  
505 -       if (!flash) {
506 +       if (ret <= 0) {
507                 printf("SF: Unsupported manufacturer %02x\n", *idp);
508                 goto err_manufacturer_probe;
509         }
510 @@ -408,6 +417,8 @@ struct spi_flash *spi_flash_probe(unsign
511         return flash;
512  
513  err_manufacturer_probe:
514 +       free(flash);
515 +err_malloc:
516  err_read_id:
517         spi_release_bus(spi);
518  err_claim_bus:
519 --- a/drivers/mtd/spi/spi_flash_internal.h
520 +++ b/drivers/mtd/spi/spi_flash_internal.h
521 @@ -98,11 +98,11 @@ int spi_flash_cmd_wait_ready(struct spi_
522  int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len);
523  
524  /* Manufacturer-specific probe functions */
525 -struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode);
526 -struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode);
527 -struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode);
528 -struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode);
529 -struct spi_flash *spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode);
530 -struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 *idcode);
531 -struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode);
532 -struct spi_flash *spi_fram_probe_ramtron(struct spi_slave *spi, u8 *idcode);
533 +int spi_flash_probe_spansion(struct spi_flash *flash, u8 *idcode);
534 +int spi_flash_probe_atmel(struct spi_flash *flash, u8 *idcode);
535 +int spi_flash_probe_eon(struct spi_flash *flash, u8 *idcode);
536 +int spi_flash_probe_macronix(struct spi_flash *flash, u8 *idcode);
537 +int spi_flash_probe_sst(struct spi_flash *flash, u8 *idcode);
538 +int spi_flash_probe_stmicro(struct spi_flash *flash, u8 *idcode);
539 +int spi_flash_probe_winbond(struct spi_flash *flash, u8 *idcode);
540 +int spi_fram_probe_ramtron(struct spi_flash *flash, u8 *idcode);
541 --- a/drivers/mtd/spi/sst.c
542 +++ b/drivers/mtd/spi/sst.c
543 @@ -39,11 +39,6 @@ struct sst_spi_flash_params {
544         const char *name;
545  };
546  
547 -struct sst_spi_flash {
548 -       struct spi_flash flash;
549 -       const struct sst_spi_flash_params *params;
550 -};
551 -
552  static const struct sst_spi_flash_params sst_spi_flash_table[] = {
553         {
554                 .idcode1 = 0x8d,
555 @@ -185,11 +180,9 @@ sst_write_wp(struct spi_flash *flash, u3
556         return ret;
557  }
558  
559 -struct spi_flash *
560 -spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
561 +int spi_flash_probe_sst(struct spi_flash *flash, u8 *idcode)
562  {
563         const struct sst_spi_flash_params *params;
564 -       struct sst_spi_flash *stm;
565         size_t i;
566  
567         for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
568 @@ -200,31 +193,24 @@ spi_flash_probe_sst(struct spi_slave *sp
569  
570         if (i == ARRAY_SIZE(sst_spi_flash_table)) {
571                 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
572 -               return NULL;
573 -       }
574 -
575 -       stm = malloc(sizeof(*stm));
576 -       if (!stm) {
577 -               debug("SF: Failed to allocate memory\n");
578 -               return NULL;
579 +               return 0;
580         }
581  
582 -       stm->params = params;
583 -       stm->flash.spi = spi;
584 -       stm->flash.name = params->name;
585 +       flash->priv = (void *)params;
586 +       flash->name = params->name;
587  
588 -       if (stm->params->flags & SST_FEAT_WP)
589 -               stm->flash.write = sst_write_wp;
590 +       if (params->flags & SST_FEAT_WP)
591 +               flash->write = sst_write_wp;
592         else
593 -               stm->flash.write = spi_flash_cmd_write_multi;
594 -       stm->flash.erase = spi_flash_cmd_erase;
595 -       stm->flash.read = spi_flash_cmd_read_fast;
596 -       stm->flash.page_size = 256;
597 -       stm->flash.sector_size = 4096;
598 -       stm->flash.size = stm->flash.sector_size * params->nr_sectors;
599 +               flash->write = spi_flash_cmd_write_multi;
600 +       flash->erase = spi_flash_cmd_erase;
601 +       flash->read = spi_flash_cmd_read_fast;
602 +       flash->page_size = 256;
603 +       flash->sector_size = 4096;
604 +       flash->size = flash->sector_size * params->nr_sectors;
605  
606         /* Flash powers up read-only, so clear BP# bits */
607 -       spi_flash_cmd_write_status(&stm->flash, 0);
608 +       spi_flash_cmd_write_status(flash, 0);
609  
610 -       return &stm->flash;
611 +       return 1;
612  }
613 --- a/drivers/mtd/spi/stmicro.c
614 +++ b/drivers/mtd/spi/stmicro.c
615 @@ -112,10 +112,10 @@ static const struct stmicro_spi_flash_pa
616         },
617  };
618  
619 -struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode)
620 +int spi_flash_probe_stmicro(struct spi_flash *flash, u8 * idcode)
621  {
622         const struct stmicro_spi_flash_params *params;
623 -       struct spi_flash *flash;
624 +       struct spi_slave *spi = flash->spi;
625         unsigned int i;
626         u16 id;
627  
628 @@ -123,13 +123,13 @@ struct spi_flash *spi_flash_probe_stmicr
629                 i = spi_flash_cmd(spi, CMD_M25PXX_RES,
630                                   idcode, 4);
631                 if (i)
632 -                       return NULL;
633 +                       return 0;
634                 if ((idcode[3] & 0xf0) == 0x10) {
635                         idcode[0] = 0x20;
636                         idcode[1] = 0x20;
637                         idcode[2] = idcode[3] + 1;
638                 } else
639 -                       return NULL;
640 +                       return 0;
641         }
642  
643         id = ((idcode[1] << 8) | idcode[2]);
644 @@ -143,16 +143,10 @@ struct spi_flash *spi_flash_probe_stmicr
645  
646         if (i == ARRAY_SIZE(stmicro_spi_flash_table)) {
647                 debug("SF: Unsupported STMicro ID %04x\n", id);
648 -               return NULL;
649 +               return 0;
650         }
651  
652 -       flash = malloc(sizeof(*flash));
653 -       if (!flash) {
654 -               debug("SF: Failed to allocate memory\n");
655 -               return NULL;
656 -       }
657 -
658 -       flash->spi = spi;
659 +       flash->priv = (void *)params;
660         flash->name = params->name;
661  
662         flash->write = spi_flash_cmd_write_multi;
663 @@ -162,5 +156,5 @@ struct spi_flash *spi_flash_probe_stmicr
664         flash->sector_size = 256 * params->pages_per_sector;
665         flash->size = flash->sector_size * params->nr_sectors;
666  
667 -       return flash;
668 +       return 1;
669  }
670 --- a/drivers/mtd/spi/winbond.c
671 +++ b/drivers/mtd/spi/winbond.c
672 @@ -69,10 +69,9 @@ static const struct winbond_spi_flash_pa
673         },
674  };
675  
676 -struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode)
677 +int spi_flash_probe_winbond(struct spi_flash *flash, u8 *idcode)
678  {
679         const struct winbond_spi_flash_params *params;
680 -       struct spi_flash *flash;
681         unsigned int i;
682  
683         for (i = 0; i < ARRAY_SIZE(winbond_spi_flash_table); i++) {
684 @@ -84,16 +83,10 @@ struct spi_flash *spi_flash_probe_winbon
685         if (i == ARRAY_SIZE(winbond_spi_flash_table)) {
686                 debug("SF: Unsupported Winbond ID %02x%02x\n",
687                                 idcode[1], idcode[2]);
688 -               return NULL;
689 +               return 0;
690         }
691  
692 -       flash = malloc(sizeof(*flash));
693 -       if (!flash) {
694 -               debug("SF: Failed to allocate memory\n");
695 -               return NULL;
696 -       }
697 -
698 -       flash->spi = spi;
699 +       flash->priv = (void *)params;
700         flash->name = params->name;
701  
702         flash->write = spi_flash_cmd_write_multi;
703 @@ -103,5 +96,5 @@ struct spi_flash *spi_flash_probe_winbon
704         flash->sector_size = 4096;
705         flash->size = 4096 * 16 * params->nr_blocks;
706  
707 -       return flash;
708 +       return 1;
709  }
710 --- a/include/spi_flash.h
711 +++ b/include/spi_flash.h
712 @@ -31,6 +31,7 @@ struct spi_flash {
713         struct spi_slave *spi;
714  
715         const char      *name;
716 +       void            *priv;
717  
718         /* Total flash size */
719         u32             size;