sata.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * From Coreboot
  3. * Copyright (C) 2008-2009 coresystems GmbH
  4. *
  5. * SPDX-License-Identifier: GPL-2.0
  6. */
  7. #include <common.h>
  8. #include <fdtdec.h>
  9. #include <asm/io.h>
  10. #include <asm/pci.h>
  11. #include <asm/arch/pch.h>
  12. #include <asm/arch/bd82x6x.h>
  13. static inline u32 sir_read(pci_dev_t dev, int idx)
  14. {
  15. x86_pci_write_config32(dev, SATA_SIRI, idx);
  16. return x86_pci_read_config32(dev, SATA_SIRD);
  17. }
  18. static inline void sir_write(pci_dev_t dev, int idx, u32 value)
  19. {
  20. x86_pci_write_config32(dev, SATA_SIRI, idx);
  21. x86_pci_write_config32(dev, SATA_SIRD, value);
  22. }
  23. static void common_sata_init(pci_dev_t dev, unsigned int port_map)
  24. {
  25. u32 reg32;
  26. u16 reg16;
  27. /* Set IDE I/O Configuration */
  28. reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
  29. x86_pci_write_config32(dev, IDE_CONFIG, reg32);
  30. /* Port enable */
  31. reg16 = x86_pci_read_config16(dev, 0x92);
  32. reg16 &= ~0x3f;
  33. reg16 |= port_map;
  34. x86_pci_write_config16(dev, 0x92, reg16);
  35. /* SATA Initialization register */
  36. port_map &= 0xff;
  37. x86_pci_write_config32(dev, 0x94, ((port_map ^ 0x3f) << 24) | 0x183);
  38. }
  39. void bd82x6x_sata_init(pci_dev_t dev, const void *blob, int node)
  40. {
  41. unsigned int port_map, speed_support, port_tx;
  42. struct pci_controller *hose = pci_bus_to_hose(0);
  43. const char *mode;
  44. u32 reg32;
  45. u16 reg16;
  46. debug("SATA: Initializing...\n");
  47. /* SATA configuration */
  48. port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0);
  49. speed_support = fdtdec_get_int(blob, node,
  50. "sata_interface_speed_support", 0);
  51. /* Enable BARs */
  52. x86_pci_write_config16(dev, PCI_COMMAND, 0x0007);
  53. mode = fdt_getprop(blob, node, "intel,sata-mode", NULL);
  54. if (!mode || !strcmp(mode, "ahci")) {
  55. u32 abar;
  56. debug("SATA: Controller in AHCI mode\n");
  57. /* Set Interrupt Line, Interrupt Pin is set by D31IP.PIP */
  58. x86_pci_write_config8(dev, INTR_LN, 0x0a);
  59. /* Set timings */
  60. x86_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
  61. IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
  62. IDE_PPE0 | IDE_IE0 | IDE_TIME0);
  63. x86_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
  64. IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
  65. /* Sync DMA */
  66. x86_pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
  67. x86_pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
  68. common_sata_init(dev, 0x8000 | port_map);
  69. /* Initialize AHCI memory-mapped space */
  70. abar = pci_read_bar32(hose, dev, 5);
  71. debug("ABAR: %08X\n", abar);
  72. /* CAP (HBA Capabilities) : enable power management */
  73. reg32 = readl(abar + 0x00);
  74. reg32 |= 0x0c006000; /* set PSC+SSC+SALP+SSS */
  75. reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
  76. /* Set ISS, if available */
  77. if (speed_support) {
  78. reg32 &= ~0x00f00000;
  79. reg32 |= (speed_support & 0x03) << 20;
  80. }
  81. writel(reg32, abar + 0x00);
  82. /* PI (Ports implemented) */
  83. writel(port_map, abar + 0x0c);
  84. (void) readl(abar + 0x0c); /* Read back 1 */
  85. (void) readl(abar + 0x0c); /* Read back 2 */
  86. /* CAP2 (HBA Capabilities Extended)*/
  87. reg32 = readl(abar + 0x24);
  88. reg32 &= ~0x00000002;
  89. writel(reg32, abar + 0x24);
  90. /* VSP (Vendor Specific Register */
  91. reg32 = readl(abar + 0xa0);
  92. reg32 &= ~0x00000005;
  93. writel(reg32, abar + 0xa0);
  94. } else if (!strcmp(mode, "combined")) {
  95. debug("SATA: Controller in combined mode\n");
  96. /* No AHCI: clear AHCI base */
  97. pci_write_bar32(hose, dev, 5, 0x00000000);
  98. /* And without AHCI BAR no memory decoding */
  99. reg16 = x86_pci_read_config16(dev, PCI_COMMAND);
  100. reg16 &= ~PCI_COMMAND_MEMORY;
  101. x86_pci_write_config16(dev, PCI_COMMAND, reg16);
  102. x86_pci_write_config8(dev, 0x09, 0x80);
  103. /* Set timings */
  104. x86_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
  105. IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
  106. x86_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
  107. IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
  108. IDE_PPE0 | IDE_IE0 | IDE_TIME0);
  109. /* Sync DMA */
  110. x86_pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
  111. x86_pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
  112. common_sata_init(dev, port_map);
  113. } else {
  114. debug("SATA: Controller in plain-ide mode\n");
  115. /* No AHCI: clear AHCI base */
  116. pci_write_bar32(hose, dev, 5, 0x00000000);
  117. /* And without AHCI BAR no memory decoding */
  118. reg16 = x86_pci_read_config16(dev, PCI_COMMAND);
  119. reg16 &= ~PCI_COMMAND_MEMORY;
  120. x86_pci_write_config16(dev, PCI_COMMAND, reg16);
  121. /*
  122. * Native mode capable on both primary and secondary (0xa)
  123. * OR'ed with enabled (0x50) = 0xf
  124. */
  125. x86_pci_write_config8(dev, 0x09, 0x8f);
  126. /* Set Interrupt Line */
  127. /* Interrupt Pin is set by D31IP.PIP */
  128. x86_pci_write_config8(dev, INTR_LN, 0xff);
  129. /* Set timings */
  130. x86_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
  131. IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
  132. IDE_PPE0 | IDE_IE0 | IDE_TIME0);
  133. x86_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
  134. IDE_SITRE | IDE_ISP_3_CLOCKS |
  135. IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
  136. /* Sync DMA */
  137. x86_pci_write_config16(dev, IDE_SDMA_CNT,
  138. IDE_SSDE0 | IDE_PSDE0);
  139. x86_pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
  140. common_sata_init(dev, port_map);
  141. }
  142. /* Set Gen3 Transmitter settings if needed */
  143. port_tx = fdtdec_get_int(blob, node, "intel,sata-port0-gen3-tx", 0);
  144. if (port_tx)
  145. pch_iobp_update(SATA_IOBP_SP0G3IR, 0, port_tx);
  146. port_tx = fdtdec_get_int(blob, node, "intel,sata-port1-gen3-tx", 0);
  147. if (port_tx)
  148. pch_iobp_update(SATA_IOBP_SP1G3IR, 0, port_tx);
  149. /* Additional Programming Requirements */
  150. sir_write(dev, 0x04, 0x00001600);
  151. sir_write(dev, 0x28, 0xa0000033);
  152. reg32 = sir_read(dev, 0x54);
  153. reg32 &= 0xff000000;
  154. reg32 |= 0x5555aa;
  155. sir_write(dev, 0x54, reg32);
  156. sir_write(dev, 0x64, 0xcccc8484);
  157. reg32 = sir_read(dev, 0x68);
  158. reg32 &= 0xffff0000;
  159. reg32 |= 0xcccc;
  160. sir_write(dev, 0x68, reg32);
  161. reg32 = sir_read(dev, 0x78);
  162. reg32 &= 0x0000ffff;
  163. reg32 |= 0x88880000;
  164. sir_write(dev, 0x78, reg32);
  165. sir_write(dev, 0x84, 0x001c7000);
  166. sir_write(dev, 0x88, 0x88338822);
  167. sir_write(dev, 0xa0, 0x001c7000);
  168. sir_write(dev, 0xc4, 0x0c0c0c0c);
  169. sir_write(dev, 0xc8, 0x0c0c0c0c);
  170. sir_write(dev, 0xd4, 0x10000000);
  171. pch_iobp_update(0xea004001, 0x3fffffff, 0xc0000000);
  172. pch_iobp_update(0xea00408a, 0xfffffcff, 0x00000100);
  173. }
  174. void bd82x6x_sata_enable(pci_dev_t dev, const void *blob, int node)
  175. {
  176. unsigned port_map;
  177. const char *mode;
  178. u16 map = 0;
  179. /*
  180. * Set SATA controller mode early so the resource allocator can
  181. * properly assign IO/Memory resources for the controller.
  182. */
  183. mode = fdt_getprop(blob, node, "intel,sata-mode", NULL);
  184. if (mode && !strcmp(mode, "ahci"))
  185. map = 0x0060;
  186. port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0);
  187. map |= (port_map ^ 0x3f) << 8;
  188. x86_pci_write_config16(dev, 0x90, map);
  189. }