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