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"
19 #define EFX_SPI_VERIFY_BUF_LEN 16
22 const struct efx_spi_device
*spi
;
24 char name
[IFNAMSIZ
+ 20];
29 static int efx_spi_slow_wait(struct efx_mtd
*efx_mtd
, bool uninterruptible
)
31 const struct efx_spi_device
*spi
= efx_mtd
->spi
;
32 struct efx_nic
*efx
= spi
->efx
;
36 /* Wait up to 4s for flash/EEPROM to finish a slow operation. */
37 for (i
= 0; i
< 40; i
++) {
38 __set_current_state(uninterruptible
?
39 TASK_UNINTERRUPTIBLE
: TASK_INTERRUPTIBLE
);
40 schedule_timeout(HZ
/ 10);
41 rc
= falcon_spi_cmd(spi
, SPI_RDSR
, -1, NULL
,
42 &status
, sizeof(status
));
45 if (!(status
& SPI_STATUS_NRDY
))
47 if (signal_pending(current
))
50 EFX_ERR(efx
, "timed out waiting for %s\n", efx_mtd
->name
);
54 static int efx_spi_unlock(const struct efx_spi_device
*spi
)
56 const u8 unlock_mask
= (SPI_STATUS_BP2
| SPI_STATUS_BP1
|
61 rc
= falcon_spi_cmd(spi
, SPI_RDSR
, -1, NULL
, &status
, sizeof(status
));
65 if (!(status
& unlock_mask
))
66 return 0; /* already unlocked */
68 rc
= falcon_spi_cmd(spi
, SPI_WREN
, -1, NULL
, NULL
, 0);
71 rc
= falcon_spi_cmd(spi
, SPI_SST_EWSR
, -1, NULL
, NULL
, 0);
75 status
&= ~unlock_mask
;
76 rc
= falcon_spi_cmd(spi
, SPI_WRSR
, -1, &status
, NULL
, sizeof(status
));
79 rc
= falcon_spi_wait_write(spi
);
86 static int efx_spi_erase(struct efx_mtd
*efx_mtd
, loff_t start
, size_t len
)
88 const struct efx_spi_device
*spi
= efx_mtd
->spi
;
89 unsigned pos
, block_len
;
90 u8 empty
[EFX_SPI_VERIFY_BUF_LEN
];
91 u8 buffer
[EFX_SPI_VERIFY_BUF_LEN
];
94 if (len
!= spi
->erase_size
)
97 if (spi
->erase_command
== 0)
100 rc
= efx_spi_unlock(spi
);
103 rc
= falcon_spi_cmd(spi
, SPI_WREN
, -1, NULL
, NULL
, 0);
106 rc
= falcon_spi_cmd(spi
, spi
->erase_command
, start
, NULL
, NULL
, 0);
109 rc
= efx_spi_slow_wait(efx_mtd
, false);
111 /* Verify the entire region has been wiped */
112 memset(empty
, 0xff, sizeof(empty
));
113 for (pos
= 0; pos
< len
; pos
+= block_len
) {
114 block_len
= min(len
- pos
, sizeof(buffer
));
115 rc
= falcon_spi_read(spi
, start
+ pos
, block_len
, NULL
, buffer
);
118 if (memcmp(empty
, buffer
, block_len
))
121 /* Avoid locking up the system */
123 if (signal_pending(current
))
132 static int efx_mtd_read(struct mtd_info
*mtd
, loff_t start
, size_t len
,
133 size_t *retlen
, u8
*buffer
)
135 struct efx_mtd
*efx_mtd
= mtd
->priv
;
136 const struct efx_spi_device
*spi
= efx_mtd
->spi
;
137 struct efx_nic
*efx
= spi
->efx
;
140 rc
= mutex_lock_interruptible(&efx
->spi_lock
);
143 rc
= falcon_spi_read(spi
, FALCON_FLASH_BOOTCODE_START
+ start
,
144 len
, retlen
, buffer
);
145 mutex_unlock(&efx
->spi_lock
);
149 static int efx_mtd_erase(struct mtd_info
*mtd
, struct erase_info
*erase
)
151 struct efx_mtd
*efx_mtd
= mtd
->priv
;
152 struct efx_nic
*efx
= efx_mtd
->spi
->efx
;
155 rc
= mutex_lock_interruptible(&efx
->spi_lock
);
158 rc
= efx_spi_erase(efx_mtd
, FALCON_FLASH_BOOTCODE_START
+ erase
->addr
,
160 mutex_unlock(&efx
->spi_lock
);
163 erase
->state
= MTD_ERASE_DONE
;
165 erase
->state
= MTD_ERASE_FAILED
;
166 erase
->fail_addr
= 0xffffffff;
168 mtd_erase_callback(erase
);
172 static int efx_mtd_write(struct mtd_info
*mtd
, loff_t start
,
173 size_t len
, size_t *retlen
, const u8
*buffer
)
175 struct efx_mtd
*efx_mtd
= mtd
->priv
;
176 const struct efx_spi_device
*spi
= efx_mtd
->spi
;
177 struct efx_nic
*efx
= spi
->efx
;
180 rc
= mutex_lock_interruptible(&efx
->spi_lock
);
183 rc
= falcon_spi_write(spi
, FALCON_FLASH_BOOTCODE_START
+ start
,
184 len
, retlen
, buffer
);
185 mutex_unlock(&efx
->spi_lock
);
189 static void efx_mtd_sync(struct mtd_info
*mtd
)
191 struct efx_mtd
*efx_mtd
= mtd
->priv
;
192 struct efx_nic
*efx
= efx_mtd
->spi
->efx
;
195 mutex_lock(&efx
->spi_lock
);
196 rc
= efx_spi_slow_wait(efx_mtd
, true);
197 mutex_unlock(&efx
->spi_lock
);
200 EFX_ERR(efx
, "%s sync failed (%d)\n", efx_mtd
->name
, rc
);
204 void efx_mtd_remove(struct efx_nic
*efx
)
206 if (efx
->spi_flash
&& efx
->spi_flash
->mtd
) {
207 struct efx_mtd
*efx_mtd
= efx
->spi_flash
->mtd
;
211 rc
= del_mtd_device(&efx_mtd
->mtd
);
221 void efx_mtd_rename(struct efx_nic
*efx
)
223 if (efx
->spi_flash
&& efx
->spi_flash
->mtd
) {
224 struct efx_mtd
*efx_mtd
= efx
->spi_flash
->mtd
;
225 snprintf(efx_mtd
->name
, sizeof(efx_mtd
->name
),
226 "%s sfc_flash_bootrom", efx
->name
);
230 int efx_mtd_probe(struct efx_nic
*efx
)
232 struct efx_spi_device
*spi
= efx
->spi_flash
;
233 struct efx_mtd
*efx_mtd
;
235 if (!spi
|| spi
->size
<= FALCON_FLASH_BOOTCODE_START
)
238 efx_mtd
= kzalloc(sizeof(*efx_mtd
), GFP_KERNEL
);
245 efx_mtd
->mtd
.type
= MTD_NORFLASH
;
246 efx_mtd
->mtd
.flags
= MTD_CAP_NORFLASH
;
247 efx_mtd
->mtd
.size
= spi
->size
- FALCON_FLASH_BOOTCODE_START
;
248 efx_mtd
->mtd
.erasesize
= spi
->erase_size
;
249 efx_mtd
->mtd
.writesize
= 1;
252 efx_mtd
->mtd
.owner
= THIS_MODULE
;
253 efx_mtd
->mtd
.priv
= efx_mtd
;
254 efx_mtd
->mtd
.name
= efx_mtd
->name
;
255 efx_mtd
->mtd
.erase
= efx_mtd_erase
;
256 efx_mtd
->mtd
.read
= efx_mtd_read
;
257 efx_mtd
->mtd
.write
= efx_mtd_write
;
258 efx_mtd
->mtd
.sync
= efx_mtd_sync
;
260 if (add_mtd_device(&efx_mtd
->mtd
)) {
263 /* add_mtd_device() returns 1 if the MTD table is full */