nicintel_spi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2010 Carl-Daniel Hailfinger
  5. * Copyright (C) 2010 Idwer Vollering
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. * Datasheet:
  22. * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual
  23. * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx
  24. * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf
  25. */
  26. #include <stdlib.h>
  27. #include "flash.h"
  28. #include "programmer.h"
  29. #define PCI_VENDOR_ID_INTEL 0x8086
  30. #define EECD 0x10
  31. #define FLA 0x1c
  32. /*
  33. * Register bits of EECD.
  34. *
  35. * Bit 04, 05: FWE (Flash Write Enable Control)
  36. * 00b = not allowed
  37. * 01b = flash writes disabled
  38. * 10b = flash writes enabled
  39. * 11b = not allowed
  40. */
  41. #define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */
  42. #define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */
  43. /* Flash Access register bits */
  44. /* Table 13-9 */
  45. #define FL_SCK 0
  46. #define FL_CS 1
  47. #define FL_SI 2
  48. #define FL_SO 3
  49. #define FL_REQ 4
  50. #define FL_GNT 5
  51. /* Currently unused */
  52. // #define FL_BUSY 30
  53. // #define FL_ER 31
  54. uint8_t *nicintel_spibar;
  55. const struct pcidev_status nics_intel_spi[] = {
  56. {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"},
  57. {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"},
  58. {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"},
  59. {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"},
  60. {},
  61. };
  62. static void nicintel_request_spibus(void)
  63. {
  64. uint32_t tmp;
  65. tmp = pci_mmio_readl(nicintel_spibar + FLA);
  66. tmp |= 1 << FL_REQ;
  67. pci_mmio_writel(tmp, nicintel_spibar + FLA);
  68. /* Wait until we are allowed to use the SPI bus. */
  69. while (!(pci_mmio_readl(nicintel_spibar + FLA) & (1 << FL_GNT))) ;
  70. }
  71. static void nicintel_release_spibus(void)
  72. {
  73. uint32_t tmp;
  74. tmp = pci_mmio_readl(nicintel_spibar + FLA);
  75. tmp &= ~(1 << FL_REQ);
  76. pci_mmio_writel(tmp, nicintel_spibar + FLA);
  77. }
  78. static void nicintel_bitbang_set_cs(int val)
  79. {
  80. uint32_t tmp;
  81. tmp = pci_mmio_readl(nicintel_spibar + FLA);
  82. tmp &= ~(1 << FL_CS);
  83. tmp |= (val << FL_CS);
  84. pci_mmio_writel(tmp, nicintel_spibar + FLA);
  85. }
  86. static void nicintel_bitbang_set_sck(int val)
  87. {
  88. uint32_t tmp;
  89. tmp = pci_mmio_readl(nicintel_spibar + FLA);
  90. tmp &= ~(1 << FL_SCK);
  91. tmp |= (val << FL_SCK);
  92. pci_mmio_writel(tmp, nicintel_spibar + FLA);
  93. }
  94. static void nicintel_bitbang_set_mosi(int val)
  95. {
  96. uint32_t tmp;
  97. tmp = pci_mmio_readl(nicintel_spibar + FLA);
  98. tmp &= ~(1 << FL_SI);
  99. tmp |= (val << FL_SI);
  100. pci_mmio_writel(tmp, nicintel_spibar + FLA);
  101. }
  102. static int nicintel_bitbang_get_miso(void)
  103. {
  104. uint32_t tmp;
  105. tmp = pci_mmio_readl(nicintel_spibar + FLA);
  106. tmp = (tmp >> FL_SO) & 0x1;
  107. return tmp;
  108. }
  109. static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
  110. .type = BITBANG_SPI_MASTER_NICINTEL,
  111. .set_cs = nicintel_bitbang_set_cs,
  112. .set_sck = nicintel_bitbang_set_sck,
  113. .set_mosi = nicintel_bitbang_set_mosi,
  114. .get_miso = nicintel_bitbang_get_miso,
  115. .request_bus = nicintel_request_spibus,
  116. .release_bus = nicintel_release_spibus,
  117. };
  118. static int nicintel_spi_shutdown(void *data)
  119. {
  120. uint32_t tmp;
  121. /* Disable writes manually. See the comment about EECD in
  122. * nicintel_spi_init() for details.
  123. */
  124. tmp = pci_mmio_readl(nicintel_spibar + EECD);
  125. tmp &= ~FLASH_WRITES_ENABLED;
  126. tmp |= FLASH_WRITES_DISABLED;
  127. pci_mmio_writel(tmp, nicintel_spibar + EECD);
  128. physunmap(nicintel_spibar, 4096);
  129. pci_cleanup(pacc);
  130. release_io_perms();
  131. return 0;
  132. }
  133. int nicintel_spi_init(void)
  134. {
  135. uint32_t tmp;
  136. get_io_perms();
  137. io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, nics_intel_spi);
  138. nicintel_spibar = physmap("Intel Gigabit NIC w/ SPI flash",
  139. io_base_addr, 4096);
  140. /* Automatic restore of EECD on shutdown is not possible because EECD
  141. * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
  142. * but other bits with side effects as well. Those other bits must be
  143. * left untouched.
  144. */
  145. tmp = pci_mmio_readl(nicintel_spibar + EECD);
  146. tmp &= ~FLASH_WRITES_DISABLED;
  147. tmp |= FLASH_WRITES_ENABLED;
  148. pci_mmio_writel(tmp, nicintel_spibar + EECD);
  149. if (register_shutdown(nicintel_spi_shutdown, NULL))
  150. return 1;
  151. /* 1 usec halfperiod delay for now. */
  152. if (bitbang_spi_init(&bitbang_spi_master_nicintel, 1))
  153. return 1;
  154. return 0;
  155. }