blob: f47ecdffae76ccd2966be111f5c8282d6f489324 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glasscd0adb32014-11-14 18:18:38 -07002/*
3 * From Coreboot
4 * Copyright (C) 2008-2009 coresystems GmbH
Simon Glasscd0adb32014-11-14 18:18:38 -07005 */
6
7#include <common.h>
Simon Glassfe54fda2017-06-14 21:28:48 -06008#include <ahci.h>
Simon Glass5cc400b2016-01-17 16:11:35 -07009#include <dm.h>
Simon Glasscd0adb32014-11-14 18:18:38 -070010#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glasscd0adb32014-11-14 18:18:38 -070013#include <asm/io.h>
Simon Glass9c852d72016-03-16 07:44:36 -060014#include <asm/pch_common.h>
Simon Glasscd0adb32014-11-14 18:18:38 -070015#include <asm/pci.h>
16#include <asm/arch/pch.h>
Simon Glasscd0adb32014-11-14 18:18:38 -070017
Simon Glass5cc400b2016-01-17 16:11:35 -070018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass35c50a52016-01-17 16:11:38 -070020static void common_sata_init(struct udevice *dev, unsigned int port_map)
Simon Glasscd0adb32014-11-14 18:18:38 -070021{
22 u32 reg32;
23 u16 reg16;
24
25 /* Set IDE I/O Configuration */
26 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Simon Glass35c50a52016-01-17 16:11:38 -070027 dm_pci_write_config32(dev, IDE_CONFIG, reg32);
Simon Glasscd0adb32014-11-14 18:18:38 -070028
29 /* Port enable */
Simon Glass35c50a52016-01-17 16:11:38 -070030 dm_pci_read_config16(dev, 0x92, &reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -070031 reg16 &= ~0x3f;
32 reg16 |= port_map;
Simon Glass35c50a52016-01-17 16:11:38 -070033 dm_pci_write_config16(dev, 0x92, reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -070034
35 /* SATA Initialization register */
36 port_map &= 0xff;
Simon Glass35c50a52016-01-17 16:11:38 -070037 dm_pci_write_config32(dev, 0x94, ((port_map ^ 0x3f) << 24) | 0x183);
Simon Glasscd0adb32014-11-14 18:18:38 -070038}
39
Simon Glassb3a9e512016-01-17 16:11:52 -070040static void bd82x6x_sata_init(struct udevice *dev, struct udevice *pch)
Simon Glasscd0adb32014-11-14 18:18:38 -070041{
42 unsigned int port_map, speed_support, port_tx;
Simon Glass35c50a52016-01-17 16:11:38 -070043 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070044 int node = dev_of_offset(dev);
Simon Glasscd0adb32014-11-14 18:18:38 -070045 const char *mode;
46 u32 reg32;
47 u16 reg16;
48
49 debug("SATA: Initializing...\n");
50
51 /* SATA configuration */
52 port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0);
53 speed_support = fdtdec_get_int(blob, node,
54 "sata_interface_speed_support", 0);
55
Simon Glasscd0adb32014-11-14 18:18:38 -070056 mode = fdt_getprop(blob, node, "intel,sata-mode", NULL);
57 if (!mode || !strcmp(mode, "ahci")) {
Simon Glass6fbb13d2017-01-16 07:03:39 -070058 ulong abar;
Simon Glasscd0adb32014-11-14 18:18:38 -070059
60 debug("SATA: Controller in AHCI mode\n");
61
Simon Glasscd0adb32014-11-14 18:18:38 -070062 /* Set timings */
Simon Glass35c50a52016-01-17 16:11:38 -070063 dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -070064 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
65 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
Simon Glass35c50a52016-01-17 16:11:38 -070066 dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -070067 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
68
69 /* Sync DMA */
Simon Glass35c50a52016-01-17 16:11:38 -070070 dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
71 dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
Simon Glasscd0adb32014-11-14 18:18:38 -070072
73 common_sata_init(dev, 0x8000 | port_map);
74
75 /* Initialize AHCI memory-mapped space */
Simon Glass35c50a52016-01-17 16:11:38 -070076 abar = dm_pci_read_bar32(dev, 5);
Simon Glass6fbb13d2017-01-16 07:03:39 -070077 debug("ABAR: %08lx\n", abar);
Simon Glasscd0adb32014-11-14 18:18:38 -070078 /* CAP (HBA Capabilities) : enable power management */
79 reg32 = readl(abar + 0x00);
80 reg32 |= 0x0c006000; /* set PSC+SSC+SALP+SSS */
81 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
82 /* Set ISS, if available */
83 if (speed_support) {
84 reg32 &= ~0x00f00000;
85 reg32 |= (speed_support & 0x03) << 20;
86 }
87 writel(reg32, abar + 0x00);
88 /* PI (Ports implemented) */
89 writel(port_map, abar + 0x0c);
90 (void) readl(abar + 0x0c); /* Read back 1 */
91 (void) readl(abar + 0x0c); /* Read back 2 */
92 /* CAP2 (HBA Capabilities Extended)*/
93 reg32 = readl(abar + 0x24);
94 reg32 &= ~0x00000002;
95 writel(reg32, abar + 0x24);
96 /* VSP (Vendor Specific Register */
97 reg32 = readl(abar + 0xa0);
98 reg32 &= ~0x00000005;
99 writel(reg32, abar + 0xa0);
100 } else if (!strcmp(mode, "combined")) {
101 debug("SATA: Controller in combined mode\n");
102
103 /* No AHCI: clear AHCI base */
Simon Glass35c50a52016-01-17 16:11:38 -0700104 dm_pci_write_bar32(dev, 5, 0x00000000);
Simon Glasscd0adb32014-11-14 18:18:38 -0700105 /* And without AHCI BAR no memory decoding */
Simon Glass35c50a52016-01-17 16:11:38 -0700106 dm_pci_read_config16(dev, PCI_COMMAND, &reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -0700107 reg16 &= ~PCI_COMMAND_MEMORY;
Simon Glass35c50a52016-01-17 16:11:38 -0700108 dm_pci_write_config16(dev, PCI_COMMAND, reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -0700109
Simon Glass35c50a52016-01-17 16:11:38 -0700110 dm_pci_write_config8(dev, 0x09, 0x80);
Simon Glasscd0adb32014-11-14 18:18:38 -0700111
112 /* Set timings */
Simon Glass35c50a52016-01-17 16:11:38 -0700113 dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -0700114 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
Simon Glass35c50a52016-01-17 16:11:38 -0700115 dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -0700116 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
117 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
118
119 /* Sync DMA */
Simon Glass35c50a52016-01-17 16:11:38 -0700120 dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
121 dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
Simon Glasscd0adb32014-11-14 18:18:38 -0700122
123 common_sata_init(dev, port_map);
124 } else {
125 debug("SATA: Controller in plain-ide mode\n");
126
127 /* No AHCI: clear AHCI base */
Simon Glass35c50a52016-01-17 16:11:38 -0700128 dm_pci_write_bar32(dev, 5, 0x00000000);
Simon Glasscd0adb32014-11-14 18:18:38 -0700129
130 /* And without AHCI BAR no memory decoding */
Simon Glass35c50a52016-01-17 16:11:38 -0700131 dm_pci_read_config16(dev, PCI_COMMAND, &reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -0700132 reg16 &= ~PCI_COMMAND_MEMORY;
Simon Glass35c50a52016-01-17 16:11:38 -0700133 dm_pci_write_config16(dev, PCI_COMMAND, reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -0700134
135 /*
136 * Native mode capable on both primary and secondary (0xa)
137 * OR'ed with enabled (0x50) = 0xf
138 */
Simon Glass35c50a52016-01-17 16:11:38 -0700139 dm_pci_write_config8(dev, 0x09, 0x8f);
Simon Glasscd0adb32014-11-14 18:18:38 -0700140
141 /* Set timings */
Simon Glass35c50a52016-01-17 16:11:38 -0700142 dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -0700143 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
144 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
Simon Glass35c50a52016-01-17 16:11:38 -0700145 dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -0700146 IDE_SITRE | IDE_ISP_3_CLOCKS |
147 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
148
149 /* Sync DMA */
Simon Glass35c50a52016-01-17 16:11:38 -0700150 dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
151 dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
Simon Glasscd0adb32014-11-14 18:18:38 -0700152
153 common_sata_init(dev, port_map);
154 }
155
156 /* Set Gen3 Transmitter settings if needed */
157 port_tx = fdtdec_get_int(blob, node, "intel,sata-port0-gen3-tx", 0);
158 if (port_tx)
Simon Glassb3a9e512016-01-17 16:11:52 -0700159 pch_iobp_update(pch, SATA_IOBP_SP0G3IR, 0, port_tx);
Simon Glasscd0adb32014-11-14 18:18:38 -0700160
161 port_tx = fdtdec_get_int(blob, node, "intel,sata-port1-gen3-tx", 0);
162 if (port_tx)
Simon Glassb3a9e512016-01-17 16:11:52 -0700163 pch_iobp_update(pch, SATA_IOBP_SP1G3IR, 0, port_tx);
Simon Glasscd0adb32014-11-14 18:18:38 -0700164
165 /* Additional Programming Requirements */
Simon Glass9c852d72016-03-16 07:44:36 -0600166 pch_common_sir_write(dev, 0x04, 0x00001600);
167 pch_common_sir_write(dev, 0x28, 0xa0000033);
168 reg32 = pch_common_sir_read(dev, 0x54);
Simon Glasscd0adb32014-11-14 18:18:38 -0700169 reg32 &= 0xff000000;
170 reg32 |= 0x5555aa;
Simon Glass9c852d72016-03-16 07:44:36 -0600171 pch_common_sir_write(dev, 0x54, reg32);
172 pch_common_sir_write(dev, 0x64, 0xcccc8484);
173 reg32 = pch_common_sir_read(dev, 0x68);
Simon Glasscd0adb32014-11-14 18:18:38 -0700174 reg32 &= 0xffff0000;
175 reg32 |= 0xcccc;
Simon Glass9c852d72016-03-16 07:44:36 -0600176 pch_common_sir_write(dev, 0x68, reg32);
177 reg32 = pch_common_sir_read(dev, 0x78);
Simon Glasscd0adb32014-11-14 18:18:38 -0700178 reg32 &= 0x0000ffff;
179 reg32 |= 0x88880000;
Simon Glass9c852d72016-03-16 07:44:36 -0600180 pch_common_sir_write(dev, 0x78, reg32);
181 pch_common_sir_write(dev, 0x84, 0x001c7000);
182 pch_common_sir_write(dev, 0x88, 0x88338822);
183 pch_common_sir_write(dev, 0xa0, 0x001c7000);
184 pch_common_sir_write(dev, 0xc4, 0x0c0c0c0c);
185 pch_common_sir_write(dev, 0xc8, 0x0c0c0c0c);
186 pch_common_sir_write(dev, 0xd4, 0x10000000);
Simon Glasscd0adb32014-11-14 18:18:38 -0700187
Simon Glassb3a9e512016-01-17 16:11:52 -0700188 pch_iobp_update(pch, 0xea004001, 0x3fffffff, 0xc0000000);
189 pch_iobp_update(pch, 0xea00408a, 0xfffffcff, 0x00000100);
Simon Glasscd0adb32014-11-14 18:18:38 -0700190}
191
Simon Glass35c50a52016-01-17 16:11:38 -0700192static void bd82x6x_sata_enable(struct udevice *dev)
Simon Glasscd0adb32014-11-14 18:18:38 -0700193{
Simon Glass35c50a52016-01-17 16:11:38 -0700194 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700195 int node = dev_of_offset(dev);
Simon Glasscd0adb32014-11-14 18:18:38 -0700196 unsigned port_map;
197 const char *mode;
198 u16 map = 0;
199
200 /*
201 * Set SATA controller mode early so the resource allocator can
202 * properly assign IO/Memory resources for the controller.
203 */
204 mode = fdt_getprop(blob, node, "intel,sata-mode", NULL);
205 if (mode && !strcmp(mode, "ahci"))
206 map = 0x0060;
207 port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0);
208
209 map |= (port_map ^ 0x3f) << 8;
Simon Glass35c50a52016-01-17 16:11:38 -0700210 dm_pci_write_config16(dev, 0x90, map);
Simon Glasscd0adb32014-11-14 18:18:38 -0700211}
Simon Glass5cc400b2016-01-17 16:11:35 -0700212
Simon Glassfe54fda2017-06-14 21:28:48 -0600213static int bd82x6x_sata_bind(struct udevice *dev)
214{
215 struct udevice *scsi_dev;
216 int ret;
217
218 if (gd->flags & GD_FLG_RELOC) {
219 ret = ahci_bind_scsi(dev, &scsi_dev);
220 if (ret)
221 return ret;
222 }
223
224 return 0;
225}
226
Simon Glass5cc400b2016-01-17 16:11:35 -0700227static int bd82x6x_sata_probe(struct udevice *dev)
228{
Simon Glassb3a9e512016-01-17 16:11:52 -0700229 struct udevice *pch;
230 int ret;
231
Simon Glassc7298e72016-02-11 13:23:26 -0700232 ret = uclass_first_device_err(UCLASS_PCH, &pch);
Simon Glassb3a9e512016-01-17 16:11:52 -0700233 if (ret)
234 return ret;
Simon Glassb3a9e512016-01-17 16:11:52 -0700235
Simon Glass5cc400b2016-01-17 16:11:35 -0700236 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass35c50a52016-01-17 16:11:38 -0700237 bd82x6x_sata_enable(dev);
Simon Glassfe54fda2017-06-14 21:28:48 -0600238 else {
Simon Glassb3a9e512016-01-17 16:11:52 -0700239 bd82x6x_sata_init(dev, pch);
Simon Glass89e7d972017-07-04 13:31:18 -0600240 ret = ahci_probe_scsi_pci(dev);
Simon Glassfe54fda2017-06-14 21:28:48 -0600241 if (ret)
242 return ret;
243 }
Simon Glass5cc400b2016-01-17 16:11:35 -0700244
245 return 0;
246}
247
248static const struct udevice_id bd82x6x_ahci_ids[] = {
249 { .compatible = "intel,pantherpoint-ahci" },
250 { }
251};
252
253U_BOOT_DRIVER(ahci_ivybridge_drv) = {
254 .name = "ahci_ivybridge",
Simon Glass85ee1652016-05-01 11:35:52 -0600255 .id = UCLASS_AHCI,
Simon Glass5cc400b2016-01-17 16:11:35 -0700256 .of_match = bd82x6x_ahci_ids,
Simon Glassfe54fda2017-06-14 21:28:48 -0600257 .bind = bd82x6x_sata_bind,
Simon Glass5cc400b2016-01-17 16:11:35 -0700258 .probe = bd82x6x_sata_probe,
259};