sfc: Rename falcon.h to nic.h
[deliverable/linux.git] / drivers / net / sfc / mtd.c
CommitLineData
f4150724
BH
1/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2008 Solarflare Communications Inc.
5 *
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.
9 */
10
11#include <linux/module.h>
12#include <linux/mtd/mtd.h>
13#include <linux/delay.h>
76884835 14#include <linux/rtnetlink.h>
f4150724
BH
15
16#define EFX_DRIVER_NAME "sfc_mtd"
17#include "net_driver.h"
18#include "spi.h"
ff2ef902 19#include "efx.h"
744093c9 20#include "nic.h"
f4150724
BH
21
22#define EFX_SPI_VERIFY_BUF_LEN 16
23
76884835 24struct efx_mtd_partition {
f4150724 25 struct mtd_info mtd;
76884835
BH
26 size_t offset;
27 const char *type_name;
f4150724
BH
28 char name[IFNAMSIZ + 20];
29};
30
76884835
BH
31struct efx_mtd_ops {
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);
38};
39
40struct efx_mtd {
41 struct list_head node;
42 struct efx_nic *efx;
43 const struct efx_spi_device *spi;
44 const char *name;
45 const struct efx_mtd_ops *ops;
46 size_t n_parts;
47 struct efx_mtd_partition part[0];
48};
49
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]; \
53 (part)++)
54
55#define to_efx_mtd_partition(mtd) \
56 container_of(mtd, struct efx_mtd_partition, mtd)
57
58static int falcon_mtd_probe(struct efx_nic *efx);
59
f4150724
BH
60/* SPI utilities */
61
62static int efx_spi_slow_wait(struct efx_mtd *efx_mtd, bool uninterruptible)
63{
64 const struct efx_spi_device *spi = efx_mtd->spi;
76884835 65 struct efx_nic *efx = efx_mtd->efx;
f4150724
BH
66 u8 status;
67 int rc, i;
68
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);
76884835 74 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
f4150724
BH
75 &status, sizeof(status));
76 if (rc)
77 return rc;
78 if (!(status & SPI_STATUS_NRDY))
79 return 0;
80 if (signal_pending(current))
81 return -EINTR;
82 }
83 EFX_ERR(efx, "timed out waiting for %s\n", efx_mtd->name);
84 return -ETIMEDOUT;
85}
86
76884835
BH
87static int
88efx_spi_unlock(struct efx_nic *efx, const struct efx_spi_device *spi)
f4150724
BH
89{
90 const u8 unlock_mask = (SPI_STATUS_BP2 | SPI_STATUS_BP1 |
91 SPI_STATUS_BP0);
92 u8 status;
93 int rc;
94
76884835
BH
95 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
96 &status, sizeof(status));
f4150724
BH
97 if (rc)
98 return rc;
99
100 if (!(status & unlock_mask))
101 return 0; /* already unlocked */
102
76884835 103 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
f4150724
BH
104 if (rc)
105 return rc;
76884835 106 rc = falcon_spi_cmd(efx, spi, SPI_SST_EWSR, -1, NULL, NULL, 0);
f4150724
BH
107 if (rc)
108 return rc;
109
110 status &= ~unlock_mask;
76884835
BH
111 rc = falcon_spi_cmd(efx, spi, SPI_WRSR, -1, &status,
112 NULL, sizeof(status));
f4150724
BH
113 if (rc)
114 return rc;
76884835 115 rc = falcon_spi_wait_write(efx, spi);
f4150724
BH
116 if (rc)
117 return rc;
118
119 return 0;
120}
121
122static int efx_spi_erase(struct efx_mtd *efx_mtd, loff_t start, size_t len)
123{
124 const struct efx_spi_device *spi = efx_mtd->spi;
76884835 125 struct efx_nic *efx = efx_mtd->efx;
f4150724
BH
126 unsigned pos, block_len;
127 u8 empty[EFX_SPI_VERIFY_BUF_LEN];
128 u8 buffer[EFX_SPI_VERIFY_BUF_LEN];
129 int rc;
130
131 if (len != spi->erase_size)
132 return -EINVAL;
133
134 if (spi->erase_command == 0)
135 return -EOPNOTSUPP;
136
76884835 137 rc = efx_spi_unlock(efx, spi);
f4150724
BH
138 if (rc)
139 return rc;
76884835 140 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
f4150724
BH
141 if (rc)
142 return rc;
76884835
BH
143 rc = falcon_spi_cmd(efx, spi, spi->erase_command, start, NULL,
144 NULL, 0);
f4150724
BH
145 if (rc)
146 return rc;
147 rc = efx_spi_slow_wait(efx_mtd, false);
148
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));
76884835
BH
153 rc = falcon_spi_read(efx, spi, start + pos, block_len,
154 NULL, buffer);
f4150724
BH
155 if (rc)
156 return rc;
157 if (memcmp(empty, buffer, block_len))
158 return -EIO;
159
160 /* Avoid locking up the system */
161 cond_resched();
162 if (signal_pending(current))
163 return -EINTR;
164 }
165
166 return rc;
167}
168
169/* MTD interface */
170
76884835
BH
171static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
172{
173 struct efx_mtd *efx_mtd = mtd->priv;
174 int rc;
175
176 rc = efx_mtd->ops->erase(mtd, erase->addr, erase->len);
177 if (rc == 0) {
178 erase->state = MTD_ERASE_DONE;
179 } else {
180 erase->state = MTD_ERASE_FAILED;
181 erase->fail_addr = 0xffffffff;
182 }
183 mtd_erase_callback(erase);
184 return rc;
185}
186
187static void efx_mtd_sync(struct mtd_info *mtd)
188{
189 struct efx_mtd *efx_mtd = mtd->priv;
190 struct efx_nic *efx = efx_mtd->efx;
191 int rc;
192
193 rc = efx_mtd->ops->sync(mtd);
194 if (rc)
195 EFX_ERR(efx, "%s sync failed (%d)\n", efx_mtd->name, rc);
196}
197
198static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
199{
200 int rc;
201
202 for (;;) {
203 rc = del_mtd_device(&part->mtd);
204 if (rc != -EBUSY)
205 break;
206 ssleep(1);
207 }
208 WARN_ON(rc);
209}
210
211static void efx_mtd_remove_device(struct efx_mtd *efx_mtd)
212{
213 struct efx_mtd_partition *part;
214
215 efx_for_each_partition(part, efx_mtd)
216 efx_mtd_remove_partition(part);
217 list_del(&efx_mtd->node);
218 kfree(efx_mtd);
219}
220
221static void efx_mtd_rename_device(struct efx_mtd *efx_mtd)
222{
223 struct efx_mtd_partition *part;
224
225 efx_for_each_partition(part, efx_mtd)
226 snprintf(part->name, sizeof(part->name),
227 "%s %s", efx_mtd->efx->name,
228 part->type_name);
229}
230
231static int efx_mtd_probe_device(struct efx_nic *efx, struct efx_mtd *efx_mtd)
232{
233 struct efx_mtd_partition *part;
234
235 efx_mtd->efx = efx;
236
237 efx_mtd_rename_device(efx_mtd);
238
239 efx_for_each_partition(part, efx_mtd) {
240 part->mtd.writesize = 1;
241
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;
249
250 if (add_mtd_device(&part->mtd))
251 goto fail;
252 }
253
254 list_add(&efx_mtd->node, &efx->mtd_list);
255 return 0;
256
257fail:
258 while (part != &efx_mtd->part[0]) {
259 --part;
260 efx_mtd_remove_partition(part);
261 }
262 /* add_mtd_device() returns 1 if the MTD table is full */
263 return -ENOMEM;
264}
265
266void efx_mtd_remove(struct efx_nic *efx)
f4150724 267{
76884835
BH
268 struct efx_mtd *efx_mtd, *next;
269
270 WARN_ON(efx_dev_registered(efx));
271
272 list_for_each_entry_safe(efx_mtd, next, &efx->mtd_list, node)
273 efx_mtd_remove_device(efx_mtd);
274}
275
276void efx_mtd_rename(struct efx_nic *efx)
277{
278 struct efx_mtd *efx_mtd;
279
280 ASSERT_RTNL();
281
282 list_for_each_entry(efx_mtd, &efx->mtd_list, node)
283 efx_mtd_rename_device(efx_mtd);
284}
285
286int efx_mtd_probe(struct efx_nic *efx)
287{
288 return falcon_mtd_probe(efx);
289}
290
291/* Implementation of MTD operations for Falcon */
292
293static int falcon_mtd_read(struct mtd_info *mtd, loff_t start,
294 size_t len, size_t *retlen, u8 *buffer)
295{
296 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
f4150724
BH
297 struct efx_mtd *efx_mtd = mtd->priv;
298 const struct efx_spi_device *spi = efx_mtd->spi;
76884835 299 struct efx_nic *efx = efx_mtd->efx;
f4150724
BH
300 int rc;
301
302 rc = mutex_lock_interruptible(&efx->spi_lock);
303 if (rc)
304 return rc;
76884835
BH
305 rc = falcon_spi_read(efx, spi, part->offset + start, len,
306 retlen, buffer);
f4150724
BH
307 mutex_unlock(&efx->spi_lock);
308 return rc;
309}
310
76884835 311static int falcon_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
f4150724 312{
76884835 313 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
f4150724 314 struct efx_mtd *efx_mtd = mtd->priv;
76884835 315 struct efx_nic *efx = efx_mtd->efx;
f4150724
BH
316 int rc;
317
318 rc = mutex_lock_interruptible(&efx->spi_lock);
319 if (rc)
320 return rc;
76884835 321 rc = efx_spi_erase(efx_mtd, part->offset + start, len);
f4150724 322 mutex_unlock(&efx->spi_lock);
f4150724
BH
323 return rc;
324}
325
76884835
BH
326static int falcon_mtd_write(struct mtd_info *mtd, loff_t start,
327 size_t len, size_t *retlen, const u8 *buffer)
f4150724 328{
76884835 329 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
f4150724
BH
330 struct efx_mtd *efx_mtd = mtd->priv;
331 const struct efx_spi_device *spi = efx_mtd->spi;
76884835 332 struct efx_nic *efx = efx_mtd->efx;
f4150724
BH
333 int rc;
334
335 rc = mutex_lock_interruptible(&efx->spi_lock);
336 if (rc)
337 return rc;
76884835
BH
338 rc = falcon_spi_write(efx, spi, part->offset + start, len,
339 retlen, buffer);
f4150724
BH
340 mutex_unlock(&efx->spi_lock);
341 return rc;
342}
343
76884835 344static int falcon_mtd_sync(struct mtd_info *mtd)
f4150724
BH
345{
346 struct efx_mtd *efx_mtd = mtd->priv;
76884835 347 struct efx_nic *efx = efx_mtd->efx;
f4150724
BH
348 int rc;
349
350 mutex_lock(&efx->spi_lock);
351 rc = efx_spi_slow_wait(efx_mtd, true);
352 mutex_unlock(&efx->spi_lock);
76884835 353 return rc;
f4150724
BH
354}
355
76884835
BH
356static 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,
361};
f4150724 362
76884835 363static int falcon_mtd_probe(struct efx_nic *efx)
f4150724
BH
364{
365 struct efx_spi_device *spi = efx->spi_flash;
366 struct efx_mtd *efx_mtd;
76884835
BH
367 int rc;
368
369 ASSERT_RTNL();
f4150724
BH
370
371 if (!spi || spi->size <= FALCON_FLASH_BOOTCODE_START)
372 return -ENODEV;
373
76884835
BH
374 efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]),
375 GFP_KERNEL);
f4150724
BH
376 if (!efx_mtd)
377 return -ENOMEM;
378
379 efx_mtd->spi = spi;
76884835
BH
380 efx_mtd->name = "flash";
381 efx_mtd->ops = &falcon_mtd_ops;
382
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";
390
391 rc = efx_mtd_probe_device(efx, efx_mtd);
392 if (rc)
f4150724 393 kfree(efx_mtd);
76884835 394 return rc;
f4150724 395}
This page took 0.170152 seconds and 5 git commands to generate.