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>
14 #include <linux/rtnetlink.h>
16 #define EFX_DRIVER_NAME "sfc_mtd"
17 #include "net_driver.h"
22 #define EFX_SPI_VERIFY_BUF_LEN 16
24 struct efx_mtd_partition {
27 const char *type_name;
28 char name[IFNAMSIZ + 20];
32 int (*read)(struct mtd_info *mtd, loff_t start, size_t len,
33 size_t *retlen, u8 *buffer);
34 int (*erase)(struct mtd_info *mtd, loff_t start, size_t len);
35 int (*write)(struct mtd_info *mtd, loff_t start, size_t len,
36 size_t *retlen, const u8 *buffer);
37 int (*sync)(struct mtd_info *mtd);
41 struct list_head node;
43 const struct efx_spi_device *spi;
45 const struct efx_mtd_ops *ops;
47 struct efx_mtd_partition part[0];
50 #define efx_for_each_partition(part, efx_mtd) \
51 for ((part) = &(efx_mtd)->part[0]; \
52 (part) != &(efx_mtd)->part[(efx_mtd)->n_parts]; \
55 #define to_efx_mtd_partition(mtd) \
56 container_of(mtd, struct efx_mtd_partition, mtd)
58 static int falcon_mtd_probe(struct efx_nic *efx);
62 static int efx_spi_slow_wait(struct efx_mtd *efx_mtd, bool uninterruptible)
64 const struct efx_spi_device *spi = efx_mtd->spi;
65 struct efx_nic *efx = efx_mtd->efx;
69 /* Wait up to 4s for flash/EEPROM to finish a slow operation. */
70 for (i = 0; i < 40; i++) {
71 __set_current_state(uninterruptible ?
72 TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
73 schedule_timeout(HZ / 10);
74 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
75 &status, sizeof(status));
78 if (!(status & SPI_STATUS_NRDY))
80 if (signal_pending(current))
83 EFX_ERR(efx, "timed out waiting for %s\n", efx_mtd->name);
88 efx_spi_unlock(struct efx_nic *efx, const struct efx_spi_device *spi)
90 const u8 unlock_mask = (SPI_STATUS_BP2 | SPI_STATUS_BP1 |
95 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
96 &status, sizeof(status));
100 if (!(status & unlock_mask))
101 return 0; /* already unlocked */
103 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
106 rc = falcon_spi_cmd(efx, spi, SPI_SST_EWSR, -1, NULL, NULL, 0);
110 status &= ~unlock_mask;
111 rc = falcon_spi_cmd(efx, spi, SPI_WRSR, -1, &status,
112 NULL, sizeof(status));
115 rc = falcon_spi_wait_write(efx, spi);
122 static int efx_spi_erase(struct efx_mtd *efx_mtd, loff_t start, size_t len)
124 const struct efx_spi_device *spi = efx_mtd->spi;
125 struct efx_nic *efx = efx_mtd->efx;
126 unsigned pos, block_len;
127 u8 empty[EFX_SPI_VERIFY_BUF_LEN];
128 u8 buffer[EFX_SPI_VERIFY_BUF_LEN];
131 if (len != spi->erase_size)
134 if (spi->erase_command == 0)
137 rc = efx_spi_unlock(efx, spi);
140 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
143 rc = falcon_spi_cmd(efx, spi, spi->erase_command, start, NULL,
147 rc = efx_spi_slow_wait(efx_mtd, false);
149 /* Verify the entire region has been wiped */
150 memset(empty, 0xff, sizeof(empty));
151 for (pos = 0; pos < len; pos += block_len) {
152 block_len = min(len - pos, sizeof(buffer));
153 rc = falcon_spi_read(efx, spi, start + pos, block_len,
157 if (memcmp(empty, buffer, block_len))
160 /* Avoid locking up the system */
162 if (signal_pending(current))
171 static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
173 struct efx_mtd *efx_mtd = mtd->priv;
176 rc = efx_mtd->ops->erase(mtd, erase->addr, erase->len);
178 erase->state = MTD_ERASE_DONE;
180 erase->state = MTD_ERASE_FAILED;
181 erase->fail_addr = 0xffffffff;
183 mtd_erase_callback(erase);
187 static void efx_mtd_sync(struct mtd_info *mtd)
189 struct efx_mtd *efx_mtd = mtd->priv;
190 struct efx_nic *efx = efx_mtd->efx;
193 rc = efx_mtd->ops->sync(mtd);
195 EFX_ERR(efx, "%s sync failed (%d)\n", efx_mtd->name, rc);
198 static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
203 rc = del_mtd_device(&part->mtd);
211 static void efx_mtd_remove_device(struct efx_mtd *efx_mtd)
213 struct efx_mtd_partition *part;
215 efx_for_each_partition(part, efx_mtd)
216 efx_mtd_remove_partition(part);
217 list_del(&efx_mtd->node);
221 static void efx_mtd_rename_device(struct efx_mtd *efx_mtd)
223 struct efx_mtd_partition *part;
225 efx_for_each_partition(part, efx_mtd)
226 snprintf(part->name, sizeof(part->name),
227 "%s %s", efx_mtd->efx->name,
231 static int efx_mtd_probe_device(struct efx_nic *efx, struct efx_mtd *efx_mtd)
233 struct efx_mtd_partition *part;
237 efx_mtd_rename_device(efx_mtd);
239 efx_for_each_partition(part, efx_mtd) {
240 part->mtd.writesize = 1;
242 part->mtd.owner = THIS_MODULE;
243 part->mtd.priv = efx_mtd;
244 part->mtd.name = part->name;
245 part->mtd.erase = efx_mtd_erase;
246 part->mtd.read = efx_mtd->ops->read;
247 part->mtd.write = efx_mtd->ops->write;
248 part->mtd.sync = efx_mtd_sync;
250 if (add_mtd_device(&part->mtd))
254 list_add(&efx_mtd->node, &efx->mtd_list);
258 while (part != &efx_mtd->part[0]) {
260 efx_mtd_remove_partition(part);
262 /* add_mtd_device() returns 1 if the MTD table is full */
266 void efx_mtd_remove(struct efx_nic *efx)
268 struct efx_mtd *efx_mtd, *next;
270 WARN_ON(efx_dev_registered(efx));
272 list_for_each_entry_safe(efx_mtd, next, &efx->mtd_list, node)
273 efx_mtd_remove_device(efx_mtd);
276 void efx_mtd_rename(struct efx_nic *efx)
278 struct efx_mtd *efx_mtd;
282 list_for_each_entry(efx_mtd, &efx->mtd_list, node)
283 efx_mtd_rename_device(efx_mtd);
286 int efx_mtd_probe(struct efx_nic *efx)
288 return falcon_mtd_probe(efx);
291 /* Implementation of MTD operations for Falcon */
293 static int falcon_mtd_read(struct mtd_info *mtd, loff_t start,
294 size_t len, size_t *retlen, u8 *buffer)
296 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
297 struct efx_mtd *efx_mtd = mtd->priv;
298 const struct efx_spi_device *spi = efx_mtd->spi;
299 struct efx_nic *efx = efx_mtd->efx;
302 rc = mutex_lock_interruptible(&efx->spi_lock);
305 rc = falcon_spi_read(efx, spi, part->offset + start, len,
307 mutex_unlock(&efx->spi_lock);
311 static int falcon_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
313 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
314 struct efx_mtd *efx_mtd = mtd->priv;
315 struct efx_nic *efx = efx_mtd->efx;
318 rc = mutex_lock_interruptible(&efx->spi_lock);
321 rc = efx_spi_erase(efx_mtd, part->offset + start, len);
322 mutex_unlock(&efx->spi_lock);
326 static int falcon_mtd_write(struct mtd_info *mtd, loff_t start,
327 size_t len, size_t *retlen, const u8 *buffer)
329 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
330 struct efx_mtd *efx_mtd = mtd->priv;
331 const struct efx_spi_device *spi = efx_mtd->spi;
332 struct efx_nic *efx = efx_mtd->efx;
335 rc = mutex_lock_interruptible(&efx->spi_lock);
338 rc = falcon_spi_write(efx, spi, part->offset + start, len,
340 mutex_unlock(&efx->spi_lock);
344 static int falcon_mtd_sync(struct mtd_info *mtd)
346 struct efx_mtd *efx_mtd = mtd->priv;
347 struct efx_nic *efx = efx_mtd->efx;
350 mutex_lock(&efx->spi_lock);
351 rc = efx_spi_slow_wait(efx_mtd, true);
352 mutex_unlock(&efx->spi_lock);
356 static struct efx_mtd_ops falcon_mtd_ops = {
357 .read = falcon_mtd_read,
358 .erase = falcon_mtd_erase,
359 .write = falcon_mtd_write,
360 .sync = falcon_mtd_sync,
363 static int falcon_mtd_probe(struct efx_nic *efx)
365 struct efx_spi_device *spi = efx->spi_flash;
366 struct efx_mtd *efx_mtd;
371 if (!spi || spi->size <= FALCON_FLASH_BOOTCODE_START)
374 efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]),
380 efx_mtd->name = "flash";
381 efx_mtd->ops = &falcon_mtd_ops;
383 efx_mtd->n_parts = 1;
384 efx_mtd->part[0].mtd.type = MTD_NORFLASH;
385 efx_mtd->part[0].mtd.flags = MTD_CAP_NORFLASH;
386 efx_mtd->part[0].mtd.size = spi->size - FALCON_FLASH_BOOTCODE_START;
387 efx_mtd->part[0].mtd.erasesize = spi->erase_size;
388 efx_mtd->part[0].offset = FALCON_FLASH_BOOTCODE_START;
389 efx_mtd->part[0].type_name = "sfc_flash_bootrom";
391 rc = efx_mtd_probe_device(efx, efx_mtd);