1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2008 Solarflare Communications Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
11 #include <linux/module.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/delay.h>
15 #define EFX_DRIVER_NAME "sfc_mtd"
16 #include "net_driver.h"
20 #define EFX_SPI_VERIFY_BUF_LEN 16
23 const struct efx_spi_device
*spi
;
25 char name
[IFNAMSIZ
+ 20];
30 static int efx_spi_slow_wait(struct efx_mtd
*efx_mtd
, bool uninterruptible
)
32 const struct efx_spi_device
*spi
= efx_mtd
->spi
;
33 struct efx_nic
*efx
= spi
->efx
;
37 /* Wait up to 4s for flash/EEPROM to finish a slow operation. */
38 for (i
= 0; i
< 40; i
++) {
39 __set_current_state(uninterruptible
?
40 TASK_UNINTERRUPTIBLE
: TASK_INTERRUPTIBLE
);
41 schedule_timeout(HZ
/ 10);
42 rc
= falcon_spi_cmd(spi
, SPI_RDSR
, -1, NULL
,
43 &status
, sizeof(status
));
46 if (!(status
& SPI_STATUS_NRDY
))
48 if (signal_pending(current
))
51 EFX_ERR(efx
, "timed out waiting for %s\n", efx_mtd
->name
);
55 static int efx_spi_unlock(const struct efx_spi_device
*spi
)
57 const u8 unlock_mask
= (SPI_STATUS_BP2
| SPI_STATUS_BP1
|
62 rc
= falcon_spi_cmd(spi
, SPI_RDSR
, -1, NULL
, &status
, sizeof(status
));
66 if (!(status
& unlock_mask
))
67 return 0; /* already unlocked */
69 rc
= falcon_spi_cmd(spi
, SPI_WREN
, -1, NULL
, NULL
, 0);
72 rc
= falcon_spi_cmd(spi
, SPI_SST_EWSR
, -1, NULL
, NULL
, 0);
76 status
&= ~unlock_mask
;
77 rc
= falcon_spi_cmd(spi
, SPI_WRSR
, -1, &status
, NULL
, sizeof(status
));
80 rc
= falcon_spi_wait_write(spi
);
87 static int efx_spi_erase(struct efx_mtd
*efx_mtd
, loff_t start
, size_t len
)
89 const struct efx_spi_device
*spi
= efx_mtd
->spi
;
90 unsigned pos
, block_len
;
91 u8 empty
[EFX_SPI_VERIFY_BUF_LEN
];
92 u8 buffer
[EFX_SPI_VERIFY_BUF_LEN
];
95 if (len
!= spi
->erase_size
)
98 if (spi
->erase_command
== 0)
101 rc
= efx_spi_unlock(spi
);
104 rc
= falcon_spi_cmd(spi
, SPI_WREN
, -1, NULL
, NULL
, 0);
107 rc
= falcon_spi_cmd(spi
, spi
->erase_command
, start
, NULL
, NULL
, 0);
110 rc
= efx_spi_slow_wait(efx_mtd
, false);
112 /* Verify the entire region has been wiped */
113 memset(empty
, 0xff, sizeof(empty
));
114 for (pos
= 0; pos
< len
; pos
+= block_len
) {
115 block_len
= min(len
- pos
, sizeof(buffer
));
116 rc
= falcon_spi_read(spi
, start
+ pos
, block_len
, NULL
, buffer
);
119 if (memcmp(empty
, buffer
, block_len
))
122 /* Avoid locking up the system */
124 if (signal_pending(current
))
133 static int efx_mtd_read(struct mtd_info
*mtd
, loff_t start
, size_t len
,
134 size_t *retlen
, u8
*buffer
)
136 struct efx_mtd
*efx_mtd
= mtd
->priv
;
137 const struct efx_spi_device
*spi
= efx_mtd
->spi
;
138 struct efx_nic
*efx
= spi
->efx
;
141 rc
= mutex_lock_interruptible(&efx
->spi_lock
);
144 rc
= falcon_spi_read(spi
, FALCON_FLASH_BOOTCODE_START
+ start
,
145 len
, retlen
, buffer
);
146 mutex_unlock(&efx
->spi_lock
);
150 static int efx_mtd_erase(struct mtd_info
*mtd
, struct erase_info
*erase
)
152 struct efx_mtd
*efx_mtd
= mtd
->priv
;
153 struct efx_nic
*efx
= efx_mtd
->spi
->efx
;
156 rc
= mutex_lock_interruptible(&efx
->spi_lock
);
159 rc
= efx_spi_erase(efx_mtd
, FALCON_FLASH_BOOTCODE_START
+ erase
->addr
,
161 mutex_unlock(&efx
->spi_lock
);
164 erase
->state
= MTD_ERASE_DONE
;
166 erase
->state
= MTD_ERASE_FAILED
;
167 erase
->fail_addr
= 0xffffffff;
169 mtd_erase_callback(erase
);
173 static int efx_mtd_write(struct mtd_info
*mtd
, loff_t start
,
174 size_t len
, size_t *retlen
, const u8
*buffer
)
176 struct efx_mtd
*efx_mtd
= mtd
->priv
;
177 const struct efx_spi_device
*spi
= efx_mtd
->spi
;
178 struct efx_nic
*efx
= spi
->efx
;
181 rc
= mutex_lock_interruptible(&efx
->spi_lock
);
184 rc
= falcon_spi_write(spi
, FALCON_FLASH_BOOTCODE_START
+ start
,
185 len
, retlen
, buffer
);
186 mutex_unlock(&efx
->spi_lock
);
190 static void efx_mtd_sync(struct mtd_info
*mtd
)
192 struct efx_mtd
*efx_mtd
= mtd
->priv
;
193 struct efx_nic
*efx
= efx_mtd
->spi
->efx
;
196 mutex_lock(&efx
->spi_lock
);
197 rc
= efx_spi_slow_wait(efx_mtd
, true);
198 mutex_unlock(&efx
->spi_lock
);
201 EFX_ERR(efx
, "%s sync failed (%d)\n", efx_mtd
->name
, rc
);
205 void efx_mtd_remove(struct efx_nic
*efx
)
207 if (efx
->spi_flash
&& efx
->spi_flash
->mtd
) {
208 struct efx_mtd
*efx_mtd
= efx
->spi_flash
->mtd
;
212 rc
= del_mtd_device(&efx_mtd
->mtd
);
222 void efx_mtd_rename(struct efx_nic
*efx
)
224 if (efx
->spi_flash
&& efx
->spi_flash
->mtd
) {
225 struct efx_mtd
*efx_mtd
= efx
->spi_flash
->mtd
;
226 snprintf(efx_mtd
->name
, sizeof(efx_mtd
->name
),
227 "%s sfc_flash_bootrom", efx
->name
);
231 int efx_mtd_probe(struct efx_nic
*efx
)
233 struct efx_spi_device
*spi
= efx
->spi_flash
;
234 struct efx_mtd
*efx_mtd
;
236 if (!spi
|| spi
->size
<= FALCON_FLASH_BOOTCODE_START
)
239 efx_mtd
= kzalloc(sizeof(*efx_mtd
), GFP_KERNEL
);
246 efx_mtd
->mtd
.type
= MTD_NORFLASH
;
247 efx_mtd
->mtd
.flags
= MTD_CAP_NORFLASH
;
248 efx_mtd
->mtd
.size
= spi
->size
- FALCON_FLASH_BOOTCODE_START
;
249 efx_mtd
->mtd
.erasesize
= spi
->erase_size
;
250 efx_mtd
->mtd
.writesize
= 1;
253 efx_mtd
->mtd
.owner
= THIS_MODULE
;
254 efx_mtd
->mtd
.priv
= efx_mtd
;
255 efx_mtd
->mtd
.name
= efx_mtd
->name
;
256 efx_mtd
->mtd
.erase
= efx_mtd_erase
;
257 efx_mtd
->mtd
.read
= efx_mtd_read
;
258 efx_mtd
->mtd
.write
= efx_mtd_write
;
259 efx_mtd
->mtd
.sync
= efx_mtd_sync
;
261 if (add_mtd_device(&efx_mtd
->mtd
)) {
264 /* add_mtd_device() returns 1 if the MTD table is full */