Angel Pons | c3f58f6 | 2020-04-05 15:46:41 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 3 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 4 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 5 | #include <device/pci_ops.h> |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 6 | #include <soc/pci_devs.h> |
| 7 | #include <soc/ramstage.h> |
| 8 | #include <soc/sata.h> |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 9 | #include <console/console.h> |
| 10 | #include <delay.h> |
| 11 | #include <device/device.h> |
| 12 | #include <device/pci.h> |
| 13 | #include <device/pci_ids.h> |
| 14 | |
| 15 | #include "chip.h" |
| 16 | |
| 17 | typedef struct soc_intel_baytrail_config config_t; |
| 18 | |
| 19 | static inline void sir_write(struct device *dev, int idx, u32 value) |
| 20 | { |
| 21 | pci_write_config32(dev, SATA_SIRI, idx); |
| 22 | pci_write_config32(dev, SATA_SIRD, value); |
| 23 | } |
| 24 | |
| 25 | static void sata_init(struct device *dev) |
| 26 | { |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 27 | config_t *config = config_of(dev); |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 28 | u32 reg32; |
| 29 | u16 reg16; |
| 30 | u8 reg8; |
| 31 | |
| 32 | printk(BIOS_DEBUG, "SATA: Initializing...\n"); |
| 33 | |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 34 | if (!config->sata_ahci) { |
| 35 | /* Set legacy or native decoding mode */ |
| 36 | if (config->ide_legacy_combined) { |
| 37 | reg8 = pci_read_config8(dev, 0x09); |
| 38 | reg8 &= ~0x5; |
| 39 | pci_write_config8(dev, 0x09, reg8); |
| 40 | } else { |
| 41 | reg8 = pci_read_config8(dev, 0x09); |
| 42 | reg8 |= 0x5; |
| 43 | pci_write_config8(dev, 0x09, reg8); |
| 44 | } |
| 45 | |
| 46 | /* Set capabilities pointer */ |
| 47 | pci_write_config8(dev, 0x34, 0x70); |
| 48 | reg16 = pci_read_config16(dev, 0x70); |
| 49 | reg16 &= ~0xFF00; |
| 50 | pci_write_config16(dev, 0x70, reg16); |
| 51 | } |
| 52 | |
| 53 | /* Primary timing - decode enable */ |
| 54 | reg16 = pci_read_config16(dev, 0x40); |
| 55 | reg16 |= 1 << 15; |
| 56 | pci_write_config16(dev, 0x40, reg16); |
| 57 | |
| 58 | /* Secondary timing - decode enable */ |
| 59 | reg16 = pci_read_config16(dev, 0x42); |
| 60 | reg16 |= 1 << 15; |
| 61 | pci_write_config16(dev, 0x42, reg16); |
| 62 | |
| 63 | /* Port mapping enables */ |
| 64 | reg16 = pci_read_config16(dev, 0x90); |
| 65 | reg16 |= (config->sata_port_map ^ 0x3) << 8; |
| 66 | pci_write_config16(dev, 0x90, reg16); |
| 67 | |
| 68 | /* Port control enables */ |
| 69 | reg16 = pci_read_config16(dev, 0x92); |
| 70 | reg16 &= ~0x003f; |
| 71 | reg16 |= config->sata_port_map; |
| 72 | pci_write_config16(dev, 0x92, reg16); |
| 73 | |
| 74 | if (config->sata_ahci) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 75 | u8 *abar = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5); |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 76 | |
| 77 | /* Enable CR memory space decoding */ |
| 78 | reg16 = pci_read_config16(dev, 0x04); |
| 79 | reg16 |= 0x2; |
| 80 | pci_write_config16(dev, 0x04, reg16); |
| 81 | |
| 82 | /* Set capability register */ |
| 83 | reg32 = read32(abar + 0x00); |
| 84 | reg32 |= 0x0c046000; // set PSC+SSC+SALP+SSS+SAM |
| 85 | reg32 &= ~0x00f20060; // clear SXS+EMS+PMS+gen bits |
| 86 | reg32 |= (0x3 << 20); // Gen3 SATA |
| 87 | write32(abar + 0x00, reg32); |
| 88 | |
| 89 | /* Ports enabled */ |
| 90 | reg32 = read32(abar + 0x0c); |
| 91 | reg32 &= (u32)(~0x3f); |
| 92 | reg32 |= config->sata_port_map; |
| 93 | write32(abar + 0xc, reg32); |
| 94 | /* Two extra reads to latch */ |
| 95 | read32(abar + 0x0c); |
| 96 | read32(abar + 0x0c); |
| 97 | |
| 98 | /* Set cap2 - Support devslp */ |
| 99 | reg32 = (1 << 5) | (1 << 4) | (1 << 3); |
| 100 | write32(abar + 0x24, reg32); |
| 101 | |
| 102 | /* Set PxCMD registers */ |
| 103 | reg32 = read32(abar + 0x118); |
| 104 | reg32 &= ~((1 << 27) | (1 << 26) | (1 << 22) | (1 << 21) | |
| 105 | (1 << 19) | (1 << 18) | (1 << 1)); |
| 106 | reg32 |= 2; |
| 107 | write32(abar + 0x118, reg32); |
| 108 | |
| 109 | reg32 = read32(abar + 0x198); |
| 110 | reg32 &= ~((1 << 27) | (1 << 26) | (1 << 22) | (1 << 21) | |
| 111 | (1 << 19) | (1 << 18) | (1 << 1)); |
| 112 | reg32 |= 2; |
| 113 | write32(abar + 0x198, reg32); |
| 114 | |
| 115 | /* Clear reset features */ |
| 116 | write32(abar + 0xc8, 0); |
| 117 | |
| 118 | /* Enable interrupts */ |
| 119 | reg8 = read8(abar + 0x04); |
| 120 | reg8 |= 0x02; |
| 121 | write8(abar + 0x04, reg8); |
| 122 | |
| 123 | } else { |
| 124 | /* TODO(shawnn): Configure IDE SATA speed regs */ |
| 125 | } |
| 126 | |
| 127 | /* 1.4 us delay after configuring port / enable bits */ |
| 128 | udelay(2); |
| 129 | |
| 130 | /* Enable clock for ports */ |
| 131 | reg32 = pci_read_config32(dev, 0x94); |
| 132 | reg32 |= 0x3f << 24; |
| 133 | pci_write_config32(dev, 0x94, reg32); |
| 134 | reg32 &= (config->sata_port_map ^ 0x3) << 24; |
| 135 | pci_write_config32(dev, 0x94, reg32); |
| 136 | |
| 137 | /* Lock SataGc register */ |
| 138 | reg32 = (0x1 << 31) | (0x7 << 12); |
| 139 | pci_write_config32(dev, 0x98, reg32); |
| 140 | } |
| 141 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 142 | static void sata_enable(struct device *dev) |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 143 | { |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 144 | config_t *config = config_of(dev); |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 145 | u8 reg8; |
| 146 | u16 reg16; |
| 147 | u32 reg32; |
| 148 | |
| 149 | southcluster_enable_dev(dev); |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 150 | |
| 151 | /* Port mapping -- mask off SPD + SMS + SC bits, then re-set */ |
| 152 | reg16 = pci_read_config16(dev, 0x90); |
| 153 | reg16 &= ~0x03e0; |
| 154 | reg16 |= (config->sata_port_map ^ 0x3) << 8; |
Elyes HAOUAS | 4a83f1c | 2016-08-25 21:07:59 +0200 | [diff] [blame] | 155 | if (config->sata_ahci) |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 156 | reg16 |= 0x60; |
| 157 | pci_write_config16(dev, 0x90, reg16); |
| 158 | |
| 159 | /* Set reg 0x94 before starting configuration */ |
| 160 | reg32 = pci_read_config32(dev, 0x94); |
| 161 | reg32 &= (u32)(~0x1ff); |
| 162 | reg32 |= 0x183; |
| 163 | pci_write_config32(dev, 0x94, reg32); |
| 164 | |
| 165 | /* Set ORM bit */ |
| 166 | reg16 = pci_read_config16(dev, 0x92); |
| 167 | reg16 |= (1 << 15); |
| 168 | pci_write_config16(dev, 0x92, reg16); |
| 169 | |
| 170 | /* R_PCH_SATA_TM2 - Undocumented in EDS, set according to ref. code */ |
| 171 | reg32 = pci_read_config32(dev, 0x98); |
| 172 | reg32 &= (u32)~(0x1f80 | (1 << 6) | (1 << 5)); |
| 173 | reg32 |= (1 << 29) | (1 << 25) | (1 << 23) | (1 << 22) | |
| 174 | (1 << 20) | (1 << 19) | (1 << 18) | (1 << 9) | (1 << 5); |
| 175 | pci_write_config32(dev, 0x98, reg32); |
| 176 | |
| 177 | /* CMD reg - set bus master enable (BME) */ |
| 178 | reg8 = pci_read_config8(dev, 0x04); |
| 179 | reg8 |= (1 << 2); |
| 180 | pci_write_config8(dev, 0x04, reg8); |
| 181 | |
| 182 | /* "Test mode registers" */ |
| 183 | sir_write(dev, 0x70, 0x00288301); |
| 184 | sir_write(dev, 0x54, 0x00000300); |
| 185 | sir_write(dev, 0x58, 0x50000000); |
| 186 | /* "OOB Detection Margin */ |
| 187 | sir_write(dev, 0x6c, 0x130C0603); |
| 188 | /* "Gasket Control" */ |
| 189 | sir_write(dev, 0xf4, 0); |
| 190 | |
| 191 | /* PCS - Enable requested SATA ports */ |
| 192 | reg8 = pci_read_config8(dev, 0x92); |
| 193 | reg8 &= ~0x03; |
| 194 | reg8 |= config->sata_port_map; |
| 195 | pci_write_config8(dev, 0x92, reg8); |
| 196 | } |
| 197 | |
| 198 | static struct device_operations sata_ops = { |
| 199 | .read_resources = pci_dev_read_resources, |
| 200 | .set_resources = pci_dev_set_resources, |
| 201 | .enable_resources = pci_dev_enable_resources, |
| 202 | .init = sata_init, |
| 203 | .enable = sata_enable, |
Shawn Nematbakhsh | 1dbd0e2 | 2013-10-28 16:15:02 -0700 | [diff] [blame] | 204 | .ops_pci = &soc_pci_ops, |
| 205 | }; |
| 206 | |
| 207 | static const unsigned short pci_device_ids[] = { |
| 208 | IDE1_DEVID, IDE2_DEVID, /* IDE */ |
| 209 | AHCI1_DEVID, AHCI2_DEVID, /* AHCI */ |
| 210 | 0, |
| 211 | }; |
| 212 | |
| 213 | static const struct pci_driver baytrail_sata __pci_driver = { |
| 214 | .ops = &sata_ops, |
| 215 | .vendor = PCI_VENDOR_ID_INTEL, |
| 216 | .devices = pci_device_ids, |
| 217 | }; |