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