mtd.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include <linux/module.h>
  11. #include <linux/mtd/mtd.h>
  12. #include <linux/delay.h>
  13. #define EFX_DRIVER_NAME "sfc_mtd"
  14. #include "net_driver.h"
  15. #include "spi.h"
  16. #include "efx.h"
  17. #define EFX_SPI_VERIFY_BUF_LEN 16
  18. struct efx_mtd {
  19. const struct efx_spi_device *spi;
  20. struct mtd_info mtd;
  21. char name[IFNAMSIZ + 20];
  22. };
  23. /* SPI utilities */
  24. static int efx_spi_slow_wait(struct efx_mtd *efx_mtd, bool uninterruptible)
  25. {
  26. const struct efx_spi_device *spi = efx_mtd->spi;
  27. struct efx_nic *efx = spi->efx;
  28. u8 status;
  29. int rc, i;
  30. /* Wait up to 4s for flash/EEPROM to finish a slow operation. */
  31. for (i = 0; i < 40; i++) {
  32. __set_current_state(uninterruptible ?
  33. TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
  34. schedule_timeout(HZ / 10);
  35. rc = falcon_spi_cmd(spi, SPI_RDSR, -1, NULL,
  36. &status, sizeof(status));
  37. if (rc)
  38. return rc;
  39. if (!(status & SPI_STATUS_NRDY))
  40. return 0;
  41. if (signal_pending(current))
  42. return -EINTR;
  43. }
  44. EFX_ERR(efx, "timed out waiting for %s\n", efx_mtd->name);
  45. return -ETIMEDOUT;
  46. }
  47. static int efx_spi_unlock(const struct efx_spi_device *spi)
  48. {
  49. const u8 unlock_mask = (SPI_STATUS_BP2 | SPI_STATUS_BP1 |
  50. SPI_STATUS_BP0);
  51. u8 status;
  52. int rc;
  53. rc = falcon_spi_cmd(spi, SPI_RDSR, -1, NULL, &status, sizeof(status));
  54. if (rc)
  55. return rc;
  56. if (!(status & unlock_mask))
  57. return 0; /* already unlocked */
  58. rc = falcon_spi_cmd(spi, SPI_WREN, -1, NULL, NULL, 0);
  59. if (rc)
  60. return rc;
  61. rc = falcon_spi_cmd(spi, SPI_SST_EWSR, -1, NULL, NULL, 0);
  62. if (rc)
  63. return rc;
  64. status &= ~unlock_mask;
  65. rc = falcon_spi_cmd(spi, SPI_WRSR, -1, &status, NULL, sizeof(status));
  66. if (rc)
  67. return rc;
  68. rc = falcon_spi_wait_write(spi);
  69. if (rc)
  70. return rc;
  71. return 0;
  72. }
  73. static int efx_spi_erase(struct efx_mtd *efx_mtd, loff_t start, size_t len)
  74. {
  75. const struct efx_spi_device *spi = efx_mtd->spi;
  76. unsigned pos, block_len;
  77. u8 empty[EFX_SPI_VERIFY_BUF_LEN];
  78. u8 buffer[EFX_SPI_VERIFY_BUF_LEN];
  79. int rc;
  80. if (len != spi->erase_size)
  81. return -EINVAL;
  82. if (spi->erase_command == 0)
  83. return -EOPNOTSUPP;
  84. rc = efx_spi_unlock(spi);
  85. if (rc)
  86. return rc;
  87. rc = falcon_spi_cmd(spi, SPI_WREN, -1, NULL, NULL, 0);
  88. if (rc)
  89. return rc;
  90. rc = falcon_spi_cmd(spi, spi->erase_command, start, NULL, NULL, 0);
  91. if (rc)
  92. return rc;
  93. rc = efx_spi_slow_wait(efx_mtd, false);
  94. /* Verify the entire region has been wiped */
  95. memset(empty, 0xff, sizeof(empty));
  96. for (pos = 0; pos < len; pos += block_len) {
  97. block_len = min(len - pos, sizeof(buffer));
  98. rc = falcon_spi_read(spi, start + pos, block_len, NULL, buffer);
  99. if (rc)
  100. return rc;
  101. if (memcmp(empty, buffer, block_len))
  102. return -EIO;
  103. /* Avoid locking up the system */
  104. cond_resched();
  105. if (signal_pending(current))
  106. return -EINTR;
  107. }
  108. return rc;
  109. }
  110. /* MTD interface */
  111. static int efx_mtd_read(struct mtd_info *mtd, loff_t start, size_t len,
  112. size_t *retlen, u8 *buffer)
  113. {
  114. struct efx_mtd *efx_mtd = mtd->priv;
  115. const struct efx_spi_device *spi = efx_mtd->spi;
  116. struct efx_nic *efx = spi->efx;
  117. int rc;
  118. rc = mutex_lock_interruptible(&efx->spi_lock);
  119. if (rc)
  120. return rc;
  121. rc = falcon_spi_read(spi, FALCON_FLASH_BOOTCODE_START + start,
  122. len, retlen, buffer);
  123. mutex_unlock(&efx->spi_lock);
  124. return rc;
  125. }
  126. static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
  127. {
  128. struct efx_mtd *efx_mtd = mtd->priv;
  129. struct efx_nic *efx = efx_mtd->spi->efx;
  130. int rc;
  131. rc = mutex_lock_interruptible(&efx->spi_lock);
  132. if (rc)
  133. return rc;
  134. rc = efx_spi_erase(efx_mtd, FALCON_FLASH_BOOTCODE_START + erase->addr,
  135. erase->len);
  136. mutex_unlock(&efx->spi_lock);
  137. if (rc == 0) {
  138. erase->state = MTD_ERASE_DONE;
  139. } else {
  140. erase->state = MTD_ERASE_FAILED;
  141. erase->fail_addr = 0xffffffff;
  142. }
  143. mtd_erase_callback(erase);
  144. return rc;
  145. }
  146. static int efx_mtd_write(struct mtd_info *mtd, loff_t start,
  147. size_t len, size_t *retlen, const u8 *buffer)
  148. {
  149. struct efx_mtd *efx_mtd = mtd->priv;
  150. const struct efx_spi_device *spi = efx_mtd->spi;
  151. struct efx_nic *efx = spi->efx;
  152. int rc;
  153. rc = mutex_lock_interruptible(&efx->spi_lock);
  154. if (rc)
  155. return rc;
  156. rc = falcon_spi_write(spi, FALCON_FLASH_BOOTCODE_START + start,
  157. len, retlen, buffer);
  158. mutex_unlock(&efx->spi_lock);
  159. return rc;
  160. }
  161. static void efx_mtd_sync(struct mtd_info *mtd)
  162. {
  163. struct efx_mtd *efx_mtd = mtd->priv;
  164. struct efx_nic *efx = efx_mtd->spi->efx;
  165. int rc;
  166. mutex_lock(&efx->spi_lock);
  167. rc = efx_spi_slow_wait(efx_mtd, true);
  168. mutex_unlock(&efx->spi_lock);
  169. if (rc)
  170. EFX_ERR(efx, "%s sync failed (%d)\n", efx_mtd->name, rc);
  171. return;
  172. }
  173. void efx_mtd_remove(struct efx_nic *efx)
  174. {
  175. if (efx->spi_flash && efx->spi_flash->mtd) {
  176. struct efx_mtd *efx_mtd = efx->spi_flash->mtd;
  177. int rc;
  178. for (;;) {
  179. rc = del_mtd_device(&efx_mtd->mtd);
  180. if (rc != -EBUSY)
  181. break;
  182. ssleep(1);
  183. }
  184. WARN_ON(rc);
  185. kfree(efx_mtd);
  186. }
  187. }
  188. void efx_mtd_rename(struct efx_nic *efx)
  189. {
  190. if (efx->spi_flash && efx->spi_flash->mtd) {
  191. struct efx_mtd *efx_mtd = efx->spi_flash->mtd;
  192. snprintf(efx_mtd->name, sizeof(efx_mtd->name),
  193. "%s sfc_flash_bootrom", efx->name);
  194. }
  195. }
  196. int efx_mtd_probe(struct efx_nic *efx)
  197. {
  198. struct efx_spi_device *spi = efx->spi_flash;
  199. struct efx_mtd *efx_mtd;
  200. if (!spi || spi->size <= FALCON_FLASH_BOOTCODE_START)
  201. return -ENODEV;
  202. efx_mtd = kzalloc(sizeof(*efx_mtd), GFP_KERNEL);
  203. if (!efx_mtd)
  204. return -ENOMEM;
  205. efx_mtd->spi = spi;
  206. spi->mtd = efx_mtd;
  207. efx_mtd->mtd.type = MTD_NORFLASH;
  208. efx_mtd->mtd.flags = MTD_CAP_NORFLASH;
  209. efx_mtd->mtd.size = spi->size - FALCON_FLASH_BOOTCODE_START;
  210. efx_mtd->mtd.erasesize = spi->erase_size;
  211. efx_mtd->mtd.writesize = 1;
  212. efx_mtd_rename(efx);
  213. efx_mtd->mtd.owner = THIS_MODULE;
  214. efx_mtd->mtd.priv = efx_mtd;
  215. efx_mtd->mtd.name = efx_mtd->name;
  216. efx_mtd->mtd.erase = efx_mtd_erase;
  217. efx_mtd->mtd.read = efx_mtd_read;
  218. efx_mtd->mtd.write = efx_mtd_write;
  219. efx_mtd->mtd.sync = efx_mtd_sync;
  220. if (add_mtd_device(&efx_mtd->mtd)) {
  221. kfree(efx_mtd);
  222. spi->mtd = NULL;
  223. /* add_mtd_device() returns 1 if the MTD table is full */
  224. return -ENOMEM;
  225. }
  226. return 0;
  227. }