sata.c 6.7 KB

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