blob: 8514b6d3bf371f3e758e1123883ff5f7bda5235a [file] [log] [blame]
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer54309d62009-01-20 22:53:10 +00004 * Copyright (C) 2008-2009 coresystems GmbH
Damien Zammit647e3852016-01-15 13:44:53 +11005 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00006 *
Stefan Reinauera8e11682009-03-11 14:54:18 +00007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
10 * the License.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000011 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000016 */
17
18#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020021#include <device/pci_ops.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000022#include <device/pci_ids.h>
23#include "i82801gx.h"
Damien Zammit647e3852016-01-15 13:44:53 +110024#include "sata.h"
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000025
26typedef struct southbridge_intel_i82801gx_config config_t;
27
Damien Zammit647e3852016-01-15 13:44:53 +110028static u8 get_ich7_sata_ports(void)
29{
30 struct device *lpc;
31
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030032 lpc = pcidev_on_root(31, 0);
Damien Zammit647e3852016-01-15 13:44:53 +110033
34 switch (pci_read_config16(lpc, PCI_DEVICE_ID)) {
35 case 0x27b0:
36 case 0x27b8:
37 return 0xf;
38 case 0x27b9:
39 case 0x27bd:
40 return 0x5;
41 case 0x27bc:
42 return 0x3;
43 default:
44 printk(BIOS_ERR,
45 "i82801gx_sata: error: cannot determine port config\n");
46 return 0;
47 }
48}
49
50void sata_enable(struct device *dev)
51{
52 /* Get the chip configuration */
53 config_t *config = dev->chip_info;
54
55 if (config->sata_ahci) {
56 /* Set map to ahci */
57 pci_write_config8(dev, SATA_MAP,
58 (pci_read_config8(dev, SATA_MAP) & ~0xc3) | 0x40);
59 } else {
60 /* Set map to ide */
61 pci_write_config8(dev, SATA_MAP,
62 pci_read_config8(dev, SATA_MAP) & ~0xc3);
63 }
64
65 /* At this point, the new pci id will appear on the bus */
66}
67
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000068static void sata_init(struct device *dev)
69{
70 u32 reg32;
Stefan Reinauera8e11682009-03-11 14:54:18 +000071 u16 reg16;
Sven Schnelleb2f173e2011-10-27 13:05:40 +020072 u32 *ahci_bar;
Damien Zammit647e3852016-01-15 13:44:53 +110073 u8 ports;
Sven Schnelleb2f173e2011-10-27 13:05:40 +020074
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000075 /* Get the chip configuration */
76 config_t *config = dev->chip_info;
77
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000078 printk(BIOS_DEBUG, "i82801gx_sata: initializing...\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +000079
Stefan Reinauer573f7d42009-07-21 21:50:34 +000080 if (config == NULL) {
Uwe Hermann607614d2010-11-18 20:12:13 +000081 printk(BIOS_ERR, "i82801gx_sata: error: device not in devicetree.cb!\n");
Stefan Reinauer573f7d42009-07-21 21:50:34 +000082 return;
83 }
Stefan Reinauera8e11682009-03-11 14:54:18 +000084
Damien Zammit647e3852016-01-15 13:44:53 +110085 /* Get ICH7 SATA port config */
86 ports = get_ich7_sata_ports();
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000087
88 /* Enable BARs */
Stefan Reinauera8e11682009-03-11 14:54:18 +000089 pci_write_config16(dev, PCI_COMMAND, 0x0007);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000090
91 if (config->ide_legacy_combined) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000092 printk(BIOS_DEBUG, "SATA controller in combined mode.\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +000093 /* No AHCI: clear AHCI base */
94 pci_write_config32(dev, 0x24, 0x00000000);
95 /* And without AHCI BAR no memory decoding */
96 reg16 = pci_read_config16(dev, PCI_COMMAND);
97 reg16 &= ~PCI_COMMAND_MEMORY;
98 pci_write_config16(dev, PCI_COMMAND, reg16);
99
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000100 pci_write_config8(dev, 0x09, 0x80);
101
102 /* Set timings */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000103 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);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000108
109 /* Sync DMA */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000110 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
111 pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000112
Stefan Reinauera8e11682009-03-11 14:54:18 +0000113 /* Set IDE I/O Configuration */
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000114 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000115 pci_write_config32(dev, IDE_CONFIG, reg32);
116
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000117 /* Combine IDE - SATA configuration */
Damien Zammit647e3852016-01-15 13:44:53 +1100118 pci_write_config8(dev, SATA_MAP, 0x02);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000119
Damien Zammit1533f132016-01-16 02:52:53 +1100120 /* Restrict ports - 0 and 2 only available */
121 ports &= 0x5;
Elyes HAOUAS70d79a42016-08-21 18:36:06 +0200122 } else if (config->sata_ahci) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000123 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000124 /* Allow both Legacy and Native mode */
125 pci_write_config8(dev, 0x09, 0x8f);
126
127 /* Set Interrupt Line */
128 /* Interrupt Pin is set by D31IP.PIP */
129 pci_write_config8(dev, INTR_LN, 0x0a);
130
Sven Schnelleb2f173e2011-10-27 13:05:40 +0200131 ahci_bar = (u32 *)(pci_read_config32(dev, 0x27) & ~0x3ff);
132 ahci_bar[3] = config->sata_ports_implemented;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000133 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000134 printk(BIOS_DEBUG, "SATA controller in plain mode.\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +0000135 /* Set Sata Controller Mode. No Mapping(?) */
Damien Zammit647e3852016-01-15 13:44:53 +1100136 pci_write_config8(dev, SATA_MAP, 0x00);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000137
138 /* No AHCI: clear AHCI base */
139 pci_write_config32(dev, 0x24, 0x00000000);
140
141 /* And without AHCI BAR no memory decoding */
142 reg16 = pci_read_config16(dev, PCI_COMMAND);
143 reg16 &= ~PCI_COMMAND_MEMORY;
144 pci_write_config16(dev, PCI_COMMAND, reg16);
145
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000146 /* Native mode capable on both primary and secondary (0xa)
147 * or'ed with enabled (0x50) = 0xf
148 */
149 pci_write_config8(dev, 0x09, 0x8f);
150
151 /* Set Interrupt Line */
152 /* Interrupt Pin is set by D31IP.PIP */
153 pci_write_config8(dev, INTR_LN, 0xff);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000154
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000155 /* Set timings */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000156 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
157 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
158 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
159 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Stefan Reinauer109ab312009-08-12 16:08:05 +0000160 IDE_SITRE | IDE_ISP_3_CLOCKS |
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000161 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000162
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000163 /* Sync DMA */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000164 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
165 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000166
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000167 /* Set IDE I/O Configuration */
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000168 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000169 pci_write_config32(dev, IDE_CONFIG, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000170 }
171
Damien Zammit1533f132016-01-16 02:52:53 +1100172 /* Set port control */
173 pci_write_config8(dev, SATA_PCS, ports);
174
Damien Zammit647e3852016-01-15 13:44:53 +1100175 /* Enable clock gating for unused ports and set initialization reg */
176 pci_write_config32(dev, SATA_IR, SIF3(ports) | SIF2 | SIF1 | SCRE);
177
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000178 /* All configurations need this SATA initialization sequence */
179 pci_write_config8(dev, 0xa0, 0x40);
180 pci_write_config8(dev, 0xa6, 0x22);
181 pci_write_config8(dev, 0xa0, 0x78);
182 pci_write_config8(dev, 0xa6, 0x22);
183 pci_write_config8(dev, 0xa0, 0x88);
184 reg32 = pci_read_config32(dev, 0xa4);
185 reg32 &= 0xc0c0c0c0;
186 reg32 |= 0x1b1b1212;
187 pci_write_config32(dev, 0xa4, reg32);
188 pci_write_config8(dev, 0xa0, 0x8c);
189 reg32 = pci_read_config32(dev, 0xa4);
190 reg32 &= 0xc0c0ff00;
191 reg32 |= 0x121200aa;
192 pci_write_config32(dev, 0xa4, reg32);
193 pci_write_config8(dev, 0xa0, 0x00);
Stefan Reinauer54309d62009-01-20 22:53:10 +0000194
195 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000196
197 /* Sata Initialization Register */
Damien Zammit647e3852016-01-15 13:44:53 +1100198 reg32 = pci_read_config32(dev, SATA_IR);
199 reg32 |= SCRD; // due to some bug
200 pci_write_config32(dev, SATA_IR, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000201}
202
Stefan Reinauera8e11682009-03-11 14:54:18 +0000203static struct pci_operations sata_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530204 .set_subsystem = pci_dev_set_subsystem,
Stefan Reinauera8e11682009-03-11 14:54:18 +0000205};
206
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000207static struct device_operations sata_ops = {
208 .read_resources = pci_dev_read_resources,
209 .set_resources = pci_dev_set_resources,
210 .enable_resources = pci_dev_enable_resources,
211 .init = sata_init,
212 .scan_bus = 0,
213 .enable = i82801gx_enable,
Stefan Reinauera8e11682009-03-11 14:54:18 +0000214 .ops_pci = &sata_pci_ops,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000215};
216
Patrick Georgiefff7332012-07-26 19:48:23 +0200217static const unsigned short sata_ids[] = {
218 0x27c0, /* Desktop Non-AHCI and Non-RAID Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
Patrick Georgiefff7332012-07-26 19:48:23 +0200219 0x27c1, /* Desktop AHCI Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
Damien Zammit647e3852016-01-15 13:44:53 +1100220 0x27c4, /* Mobile Non-AHCI and Non-RAID Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */
Patrick Georgiefff7332012-07-26 19:48:23 +0200221 0x27c5, /* Mobile AHCI Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */
Damien Zammit647e3852016-01-15 13:44:53 +1100222 /* NOTE: Any of the below are not properly supported yet. */
223 0x27c3, /* Desktop RAID mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
Patrick Georgiefff7332012-07-26 19:48:23 +0200224 0x27c6, /* ICH7M DH Raid Mode: 82801GHM (ICH7-M DH) */
225 0
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000226};
227
Patrick Georgiefff7332012-07-26 19:48:23 +0200228static const struct pci_driver i82801gx_sata_driver __pci_driver = {
Arthur Heymans3f111b02017-03-09 12:02:52 +0100229 .ops = &sata_ops,
230 .vendor = PCI_VENDOR_ID_INTEL,
231 .devices = sata_ids,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000232};