blob: da6455b3bd86674a03665e05f2f436838e26bb24 [file] [log] [blame]
Simon Glasscd0adb32014-11-14 18:18:38 -07001/*
2 * From Coreboot
3 * Copyright (C) 2008-2009 coresystems GmbH
4 *
5 * SPDX-License-Identifier: GPL-2.0
6 */
7
8#include <common.h>
Simon Glass5cc400b2016-01-17 16:11:35 -07009#include <dm.h>
Simon Glasscd0adb32014-11-14 18:18:38 -070010#include <fdtdec.h>
11#include <asm/io.h>
12#include <asm/pci.h>
13#include <asm/arch/pch.h>
14#include <asm/arch/bd82x6x.h>
15
Simon Glass5cc400b2016-01-17 16:11:35 -070016DECLARE_GLOBAL_DATA_PTR;
17
Simon Glass35c50a52016-01-17 16:11:38 -070018static inline u32 sir_read(struct udevice *dev, int idx)
Simon Glasscd0adb32014-11-14 18:18:38 -070019{
Simon Glass35c50a52016-01-17 16:11:38 -070020 u32 data;
21
22 dm_pci_write_config32(dev, SATA_SIRI, idx);
23 dm_pci_read_config32(dev, SATA_SIRD, &data);
24
25 return data;
Simon Glasscd0adb32014-11-14 18:18:38 -070026}
27
Simon Glass35c50a52016-01-17 16:11:38 -070028static inline void sir_write(struct udevice *dev, int idx, u32 value)
Simon Glasscd0adb32014-11-14 18:18:38 -070029{
Simon Glass35c50a52016-01-17 16:11:38 -070030 dm_pci_write_config32(dev, SATA_SIRI, idx);
31 dm_pci_write_config32(dev, SATA_SIRD, value);
Simon Glasscd0adb32014-11-14 18:18:38 -070032}
33
Simon Glass35c50a52016-01-17 16:11:38 -070034static void common_sata_init(struct udevice *dev, unsigned int port_map)
Simon Glasscd0adb32014-11-14 18:18:38 -070035{
36 u32 reg32;
37 u16 reg16;
38
39 /* Set IDE I/O Configuration */
40 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Simon Glass35c50a52016-01-17 16:11:38 -070041 dm_pci_write_config32(dev, IDE_CONFIG, reg32);
Simon Glasscd0adb32014-11-14 18:18:38 -070042
43 /* Port enable */
Simon Glass35c50a52016-01-17 16:11:38 -070044 dm_pci_read_config16(dev, 0x92, &reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -070045 reg16 &= ~0x3f;
46 reg16 |= port_map;
Simon Glass35c50a52016-01-17 16:11:38 -070047 dm_pci_write_config16(dev, 0x92, reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -070048
49 /* SATA Initialization register */
50 port_map &= 0xff;
Simon Glass35c50a52016-01-17 16:11:38 -070051 dm_pci_write_config32(dev, 0x94, ((port_map ^ 0x3f) << 24) | 0x183);
Simon Glasscd0adb32014-11-14 18:18:38 -070052}
53
Simon Glassb3a9e512016-01-17 16:11:52 -070054static void bd82x6x_sata_init(struct udevice *dev, struct udevice *pch)
Simon Glasscd0adb32014-11-14 18:18:38 -070055{
56 unsigned int port_map, speed_support, port_tx;
Simon Glass35c50a52016-01-17 16:11:38 -070057 const void *blob = gd->fdt_blob;
58 int node = dev->of_offset;
Simon Glasscd0adb32014-11-14 18:18:38 -070059 const char *mode;
60 u32 reg32;
61 u16 reg16;
62
63 debug("SATA: Initializing...\n");
64
65 /* SATA configuration */
66 port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0);
67 speed_support = fdtdec_get_int(blob, node,
68 "sata_interface_speed_support", 0);
69
Simon Glasscd0adb32014-11-14 18:18:38 -070070 mode = fdt_getprop(blob, node, "intel,sata-mode", NULL);
71 if (!mode || !strcmp(mode, "ahci")) {
72 u32 abar;
73
74 debug("SATA: Controller in AHCI mode\n");
75
Simon Glasscd0adb32014-11-14 18:18:38 -070076 /* Set timings */
Simon Glass35c50a52016-01-17 16:11:38 -070077 dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -070078 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
79 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
Simon Glass35c50a52016-01-17 16:11:38 -070080 dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -070081 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
82
83 /* Sync DMA */
Simon Glass35c50a52016-01-17 16:11:38 -070084 dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
85 dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
Simon Glasscd0adb32014-11-14 18:18:38 -070086
87 common_sata_init(dev, 0x8000 | port_map);
88
89 /* Initialize AHCI memory-mapped space */
Simon Glass35c50a52016-01-17 16:11:38 -070090 abar = dm_pci_read_bar32(dev, 5);
Simon Glasscd0adb32014-11-14 18:18:38 -070091 debug("ABAR: %08X\n", abar);
92 /* CAP (HBA Capabilities) : enable power management */
93 reg32 = readl(abar + 0x00);
94 reg32 |= 0x0c006000; /* set PSC+SSC+SALP+SSS */
95 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
96 /* Set ISS, if available */
97 if (speed_support) {
98 reg32 &= ~0x00f00000;
99 reg32 |= (speed_support & 0x03) << 20;
100 }
101 writel(reg32, abar + 0x00);
102 /* PI (Ports implemented) */
103 writel(port_map, abar + 0x0c);
104 (void) readl(abar + 0x0c); /* Read back 1 */
105 (void) readl(abar + 0x0c); /* Read back 2 */
106 /* CAP2 (HBA Capabilities Extended)*/
107 reg32 = readl(abar + 0x24);
108 reg32 &= ~0x00000002;
109 writel(reg32, abar + 0x24);
110 /* VSP (Vendor Specific Register */
111 reg32 = readl(abar + 0xa0);
112 reg32 &= ~0x00000005;
113 writel(reg32, abar + 0xa0);
114 } else if (!strcmp(mode, "combined")) {
115 debug("SATA: Controller in combined mode\n");
116
117 /* No AHCI: clear AHCI base */
Simon Glass35c50a52016-01-17 16:11:38 -0700118 dm_pci_write_bar32(dev, 5, 0x00000000);
Simon Glasscd0adb32014-11-14 18:18:38 -0700119 /* And without AHCI BAR no memory decoding */
Simon Glass35c50a52016-01-17 16:11:38 -0700120 dm_pci_read_config16(dev, PCI_COMMAND, &reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -0700121 reg16 &= ~PCI_COMMAND_MEMORY;
Simon Glass35c50a52016-01-17 16:11:38 -0700122 dm_pci_write_config16(dev, PCI_COMMAND, reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -0700123
Simon Glass35c50a52016-01-17 16:11:38 -0700124 dm_pci_write_config8(dev, 0x09, 0x80);
Simon Glasscd0adb32014-11-14 18:18:38 -0700125
126 /* Set timings */
Simon Glass35c50a52016-01-17 16:11:38 -0700127 dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -0700128 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
Simon Glass35c50a52016-01-17 16:11:38 -0700129 dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -0700130 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
131 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
132
133 /* Sync DMA */
Simon Glass35c50a52016-01-17 16:11:38 -0700134 dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
135 dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
Simon Glasscd0adb32014-11-14 18:18:38 -0700136
137 common_sata_init(dev, port_map);
138 } else {
139 debug("SATA: Controller in plain-ide mode\n");
140
141 /* No AHCI: clear AHCI base */
Simon Glass35c50a52016-01-17 16:11:38 -0700142 dm_pci_write_bar32(dev, 5, 0x00000000);
Simon Glasscd0adb32014-11-14 18:18:38 -0700143
144 /* And without AHCI BAR no memory decoding */
Simon Glass35c50a52016-01-17 16:11:38 -0700145 dm_pci_read_config16(dev, PCI_COMMAND, &reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -0700146 reg16 &= ~PCI_COMMAND_MEMORY;
Simon Glass35c50a52016-01-17 16:11:38 -0700147 dm_pci_write_config16(dev, PCI_COMMAND, reg16);
Simon Glasscd0adb32014-11-14 18:18:38 -0700148
149 /*
150 * Native mode capable on both primary and secondary (0xa)
151 * OR'ed with enabled (0x50) = 0xf
152 */
Simon Glass35c50a52016-01-17 16:11:38 -0700153 dm_pci_write_config8(dev, 0x09, 0x8f);
Simon Glasscd0adb32014-11-14 18:18:38 -0700154
155 /* Set timings */
Simon Glass35c50a52016-01-17 16:11:38 -0700156 dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -0700157 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
158 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
Simon Glass35c50a52016-01-17 16:11:38 -0700159 dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Simon Glasscd0adb32014-11-14 18:18:38 -0700160 IDE_SITRE | IDE_ISP_3_CLOCKS |
161 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
162
163 /* Sync DMA */
Simon Glass35c50a52016-01-17 16:11:38 -0700164 dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
165 dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
Simon Glasscd0adb32014-11-14 18:18:38 -0700166
167 common_sata_init(dev, port_map);
168 }
169
170 /* Set Gen3 Transmitter settings if needed */
171 port_tx = fdtdec_get_int(blob, node, "intel,sata-port0-gen3-tx", 0);
172 if (port_tx)
Simon Glassb3a9e512016-01-17 16:11:52 -0700173 pch_iobp_update(pch, SATA_IOBP_SP0G3IR, 0, port_tx);
Simon Glasscd0adb32014-11-14 18:18:38 -0700174
175 port_tx = fdtdec_get_int(blob, node, "intel,sata-port1-gen3-tx", 0);
176 if (port_tx)
Simon Glassb3a9e512016-01-17 16:11:52 -0700177 pch_iobp_update(pch, SATA_IOBP_SP1G3IR, 0, port_tx);
Simon Glasscd0adb32014-11-14 18:18:38 -0700178
179 /* Additional Programming Requirements */
180 sir_write(dev, 0x04, 0x00001600);
181 sir_write(dev, 0x28, 0xa0000033);
182 reg32 = sir_read(dev, 0x54);
183 reg32 &= 0xff000000;
184 reg32 |= 0x5555aa;
185 sir_write(dev, 0x54, reg32);
186 sir_write(dev, 0x64, 0xcccc8484);
187 reg32 = sir_read(dev, 0x68);
188 reg32 &= 0xffff0000;
189 reg32 |= 0xcccc;
190 sir_write(dev, 0x68, reg32);
191 reg32 = sir_read(dev, 0x78);
192 reg32 &= 0x0000ffff;
193 reg32 |= 0x88880000;
194 sir_write(dev, 0x78, reg32);
195 sir_write(dev, 0x84, 0x001c7000);
196 sir_write(dev, 0x88, 0x88338822);
197 sir_write(dev, 0xa0, 0x001c7000);
198 sir_write(dev, 0xc4, 0x0c0c0c0c);
199 sir_write(dev, 0xc8, 0x0c0c0c0c);
200 sir_write(dev, 0xd4, 0x10000000);
201
Simon Glassb3a9e512016-01-17 16:11:52 -0700202 pch_iobp_update(pch, 0xea004001, 0x3fffffff, 0xc0000000);
203 pch_iobp_update(pch, 0xea00408a, 0xfffffcff, 0x00000100);
Simon Glasscd0adb32014-11-14 18:18:38 -0700204}
205
Simon Glass35c50a52016-01-17 16:11:38 -0700206static void bd82x6x_sata_enable(struct udevice *dev)
Simon Glasscd0adb32014-11-14 18:18:38 -0700207{
Simon Glass35c50a52016-01-17 16:11:38 -0700208 const void *blob = gd->fdt_blob;
209 int node = dev->of_offset;
Simon Glasscd0adb32014-11-14 18:18:38 -0700210 unsigned port_map;
211 const char *mode;
212 u16 map = 0;
213
214 /*
215 * Set SATA controller mode early so the resource allocator can
216 * properly assign IO/Memory resources for the controller.
217 */
218 mode = fdt_getprop(blob, node, "intel,sata-mode", NULL);
219 if (mode && !strcmp(mode, "ahci"))
220 map = 0x0060;
221 port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0);
222
223 map |= (port_map ^ 0x3f) << 8;
Simon Glass35c50a52016-01-17 16:11:38 -0700224 dm_pci_write_config16(dev, 0x90, map);
Simon Glasscd0adb32014-11-14 18:18:38 -0700225}
Simon Glass5cc400b2016-01-17 16:11:35 -0700226
227static 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 Glass39f3f8c2016-01-17 16:11:37 -0700238 else
Simon Glassb3a9e512016-01-17 16:11:52 -0700239 bd82x6x_sata_init(dev, pch);
Simon Glass5cc400b2016-01-17 16:11:35 -0700240
241 return 0;
242}
243
244static const struct udevice_id bd82x6x_ahci_ids[] = {
245 { .compatible = "intel,pantherpoint-ahci" },
246 { }
247};
248
249U_BOOT_DRIVER(ahci_ivybridge_drv) = {
250 .name = "ahci_ivybridge",
251 .id = UCLASS_DISK,
252 .of_match = bd82x6x_ahci_ids,
253 .probe = bd82x6x_sata_probe,
254};