sata.c 7.0 KB

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