sata.c 6.7 KB

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