mtd.c 6.3 KB

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