1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ops.h>
7 #include <device/pci_ids.h>
12 static u8
get_ich7_sata_ports(void)
16 lpc
= pcidev_on_root(31, 0);
18 switch (pci_read_config16(lpc
, PCI_DEVICE_ID
)) {
28 printk(BIOS_ERR
, "i82801gx_sata: error: cannot determine port config\n");
33 void sata_enable(struct device
*dev
)
35 /* Get the chip configuration */
36 struct southbridge_intel_i82801gx_config
*config
= dev
->chip_info
;
38 if (config
->sata_mode
== SATA_MODE_AHCI
) {
39 /* Check if the southbridge supports AHCI */
40 struct device
*lpc_dev
= pcidev_on_root(31, 0);
42 /* According to the PCI spec function 0 on a bus:device
43 needs to be active for other functions to be enabled.
44 Since SATA is on the same bus:device as the LPC
45 bridge, it makes little sense to continue. */
46 die("Couldn't find the LPC device!\n");
49 const bool ahci_supported
= !(pci_read_config32(lpc_dev
, FDVCT
)
52 if (!ahci_supported
) {
53 /* Fallback to IDE PLAIN for sata for the rest of the initialization */
54 config
->sata_mode
= SATA_MODE_IDE_PLAIN
;
55 printk(BIOS_DEBUG
, "AHCI not supported, falling back to plain mode.\n");
59 if (config
->sata_mode
== SATA_MODE_AHCI
) {
61 pci_update_config8(dev
, SATA_MAP
, (u8
)~0xc3, 0x40);
64 pci_and_config8(dev
, SATA_MAP
, (u8
)~0xc3);
66 /* At this point, the new pci id will appear on the bus */
69 static void sata_init(struct device
*dev
)
74 /* Get the chip configuration */
75 const struct southbridge_intel_i82801gx_config
*config
= dev
->chip_info
;
77 printk(BIOS_DEBUG
, "i82801gx_sata: initializing...\n");
79 if (config
== nullptr) {
80 printk(BIOS_ERR
, "i82801gx_sata: error: device not in devicetree.cb!\n");
84 /* Get ICH7 SATA port config */
85 ports
= get_ich7_sata_ports();
88 pci_write_config16(dev
, PCI_COMMAND
,
89 PCI_COMMAND_MASTER
| PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
);
91 switch (config
->sata_mode
) {
92 case SATA_MODE_IDE_LEGACY_COMBINED
:
93 printk(BIOS_DEBUG
, "SATA controller in combined mode.\n");
94 /* No AHCI: clear AHCI base */
95 pci_write_config32(dev
, PCI_BASE_ADDRESS_5
, 0);
97 /* And without AHCI BAR no memory decoding */
98 pci_and_config16(dev
, PCI_COMMAND
, ~PCI_COMMAND_MEMORY
);
100 pci_write_config8(dev
, 0x09, 0x80);
103 pci_write_config16(dev
, IDE_TIM_PRI
, IDE_DECODE_ENABLE
|
104 IDE_ISP_5_CLOCKS
| IDE_RCT_4_CLOCKS
);
105 pci_write_config16(dev
, IDE_TIM_SEC
, IDE_DECODE_ENABLE
|
106 IDE_ISP_3_CLOCKS
| IDE_RCT_1_CLOCKS
|
107 IDE_PPE0
| IDE_IE0
| IDE_TIME0
);
110 pci_write_config16(dev
, IDE_SDMA_CNT
, IDE_SSDE0
);
111 pci_write_config16(dev
, IDE_SDMA_TIM
, 0x0200);
113 /* Set IDE I/O Configuration */
114 reg32
= SIG_MODE_PRI_NORMAL
| FAST_PCB1
| FAST_PCB0
| PCB1
| PCB0
;
115 pci_write_config32(dev
, IDE_CONFIG
, reg32
);
117 /* Combine IDE - SATA configuration */
118 pci_write_config8(dev
, SATA_MAP
, 0x02);
120 /* Restrict ports - 0 and 2 only available */
124 printk(BIOS_DEBUG
, "SATA controller in AHCI mode.\n");
125 /* Allow both Legacy and Native mode */
126 pci_write_config8(dev
, 0x09, 0x8f);
128 /* Set Interrupt Line */
129 /* Interrupt Pin is set by D31IP.PIP */
130 pci_write_config8(dev
, INTR_LN
, 0x0a);
132 struct resource
*ahci_res
= probe_resource(dev
, PCI_BASE_ADDRESS_5
);
133 if (ahci_res
!= nullptr)
134 /* write AHCI GHC_PI register */
135 write32(res2mmio(ahci_res
, 0xc, 0), config
->sata_ports_implemented
);
138 case SATA_MODE_IDE_PLAIN
:
139 printk(BIOS_DEBUG
, "SATA controller in plain mode.\n");
140 /* Set Sata Controller Mode. No Mapping(?) */
141 pci_write_config8(dev
, SATA_MAP
, 0x00);
143 /* No AHCI: clear AHCI base */
144 pci_write_config32(dev
, PCI_BASE_ADDRESS_5
, 0x00000000);
146 /* And without AHCI BAR no memory decoding */
147 pci_and_config16(dev
, PCI_COMMAND
, ~PCI_COMMAND_MEMORY
);
149 /* Native mode capable on both primary and secondary (0xa)
150 * or'ed with enabled (0x50) = 0xf
152 pci_write_config8(dev
, 0x09, 0x8f);
154 /* Set Interrupt Line */
155 /* Interrupt Pin is set by D31IP.PIP */
156 pci_write_config8(dev
, INTR_LN
, 0xff);
159 pci_write_config16(dev
, IDE_TIM_PRI
, IDE_DECODE_ENABLE
|
160 IDE_ISP_3_CLOCKS
| IDE_RCT_1_CLOCKS
|
161 IDE_PPE0
| IDE_IE0
| IDE_TIME0
);
162 pci_write_config16(dev
, IDE_TIM_SEC
, IDE_DECODE_ENABLE
|
163 IDE_SITRE
| IDE_ISP_3_CLOCKS
|
164 IDE_RCT_1_CLOCKS
| IDE_IE0
| IDE_TIME0
);
167 pci_write_config16(dev
, IDE_SDMA_CNT
, IDE_SSDE0
| IDE_PSDE0
);
168 pci_write_config16(dev
, IDE_SDMA_TIM
, 0x0201);
170 /* Set IDE I/O Configuration */
171 reg32
= SIG_MODE_PRI_NORMAL
| FAST_PCB1
| FAST_PCB0
| PCB1
| PCB0
;
172 pci_write_config32(dev
, IDE_CONFIG
, reg32
);
176 /* Set port control */
177 pci_write_config8(dev
, SATA_PCS
, ports
);
179 /* Enable clock gating for unused ports and set initialization reg */
180 pci_write_config32(dev
, SATA_IR
, SIF3(ports
) | SIF2
| SIF1
| SCRE
);
182 /* All configurations need this SATA initialization sequence */
183 pci_write_config8(dev
, 0xa0, 0x40);
184 pci_write_config8(dev
, 0xa6, 0x22);
185 pci_write_config8(dev
, 0xa0, 0x78);
186 pci_write_config8(dev
, 0xa6, 0x22);
187 pci_write_config8(dev
, 0xa0, 0x88);
188 pci_update_config32(dev
, 0xa4, 0xc0c0c0c0, 0x1b1b1212);
189 pci_write_config8(dev
, 0xa0, 0x8c);
190 pci_update_config32(dev
, 0xa4, 0xc0c0ff00, 0x121200aa);
191 pci_write_config8(dev
, 0xa0, 0x00);
193 pci_write_config8(dev
, PCI_INTERRUPT_LINE
, 0);
195 /* Sata Initialization Register */
196 pci_or_config32(dev
, SATA_IR
, SCRD
); // due to some bug
199 static struct device_operations sata_ops
= {
200 .read_resources
= pci_dev_read_resources
,
201 .set_resources
= pci_dev_set_resources
,
202 .enable_resources
= pci_dev_enable_resources
,
204 .enable
= i82801gx_enable
,
205 .ops_pci
= &pci_dev_ops_pci
,
208 static const unsigned short sata_ids
[] = {
209 0x27c0, /* Desktop Non-AHCI and Non-RAID Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
210 0x27c1, /* Desktop AHCI Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
211 0x27c4, /* Mobile Non-AHCI and Non-RAID Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */
212 0x27c5, /* Mobile AHCI Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */
213 /* NOTE: Any of the below are not properly supported yet. */
214 0x27c3, /* Desktop RAID mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
215 0x27c6, /* ICH7M DH Raid Mode: 82801GHM (ICH7-M DH) */
219 static const struct pci_driver i82801gx_sata_driver __pci_driver
= {
221 .vendor
= PCI_VID_INTEL
,