Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011-12 The Chromium OS Authors. |
| 4 | * |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 5 | * This file is derived from the flashrom project. |
| 6 | */ |
Bin Meng | 316fd94 | 2016-02-01 01:40:36 -0800 | [diff] [blame] | 7 | |
Simon Glass | d500dd8 | 2019-12-06 21:42:41 -0700 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_SPI |
| 9 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | e87e87b | 2019-12-06 21:42:40 -0700 | [diff] [blame] | 11 | #include <div64.h> |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | b7632cb | 2019-12-06 21:42:45 -0700 | [diff] [blame] | 13 | #include <dt-structs.h> |
Simon Glass | a08ca38 | 2015-01-27 22:13:43 -0700 | [diff] [blame] | 14 | #include <errno.h> |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame] | 16 | #include <pch.h> |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 17 | #include <pci.h> |
| 18 | #include <pci_ids.h> |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame] | 19 | #include <spi.h> |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 20 | #include <spi_flash.h> |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 21 | #include <spi-mem.h> |
Simon Glass | bdd2897 | 2019-12-06 21:42:48 -0700 | [diff] [blame] | 22 | #include <spl.h> |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 23 | #include <asm/fast_spi.h> |
Simon Glass | e87e87b | 2019-12-06 21:42:40 -0700 | [diff] [blame] | 24 | #include <asm/io.h> |
Simon Glass | bdd2897 | 2019-12-06 21:42:48 -0700 | [diff] [blame] | 25 | #include <asm/mtrr.h> |
| 26 | #include <linux/sizes.h> |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 27 | |
| 28 | #include "ich.h" |
| 29 | |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 30 | #ifdef DEBUG_TRACE |
| 31 | #define debug_trace(fmt, args...) debug(fmt, ##args) |
| 32 | #else |
| 33 | #define debug_trace(x, args...) |
| 34 | #endif |
| 35 | |
Simon Glass | eb0ae6f | 2019-12-06 21:42:42 -0700 | [diff] [blame] | 36 | struct ich_spi_platdata { |
Simon Glass | b7632cb | 2019-12-06 21:42:45 -0700 | [diff] [blame] | 37 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 38 | struct dtd_intel_fast_spi dtplat; |
| 39 | #endif |
Simon Glass | eb0ae6f | 2019-12-06 21:42:42 -0700 | [diff] [blame] | 40 | enum ich_version ich_version; /* Controller version, 7 or 9 */ |
| 41 | bool lockdown; /* lock down controller settings? */ |
| 42 | ulong mmio_base; /* Base of MMIO registers */ |
Simon Glass | b7632cb | 2019-12-06 21:42:45 -0700 | [diff] [blame] | 43 | pci_dev_t bdf; /* PCI address used by of-platdata */ |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 44 | bool hwseq; /* Use hardware sequencing (not s/w) */ |
Simon Glass | eb0ae6f | 2019-12-06 21:42:42 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 47 | static u8 ich_readb(struct ich_spi_priv *priv, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 48 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 49 | u8 value = readb(priv->base + reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 50 | |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 51 | debug_trace("read %2.2x from %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 52 | |
| 53 | return value; |
| 54 | } |
| 55 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 56 | static u16 ich_readw(struct ich_spi_priv *priv, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 57 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 58 | u16 value = readw(priv->base + reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 59 | |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 60 | debug_trace("read %4.4x from %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 61 | |
| 62 | return value; |
| 63 | } |
| 64 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 65 | static u32 ich_readl(struct ich_spi_priv *priv, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 66 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 67 | u32 value = readl(priv->base + reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 68 | |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 69 | debug_trace("read %8.8x from %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 70 | |
| 71 | return value; |
| 72 | } |
| 73 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 74 | static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 75 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 76 | writeb(value, priv->base + reg); |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 77 | debug_trace("wrote %2.2x to %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 80 | static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 81 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 82 | writew(value, priv->base + reg); |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 83 | debug_trace("wrote %4.4x to %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 86 | static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 87 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 88 | writel(value, priv->base + reg); |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 89 | debug_trace("wrote %8.8x to %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 92 | static void write_reg(struct ich_spi_priv *priv, const void *value, |
| 93 | int dest_reg, uint32_t size) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 94 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 95 | memcpy_toio(priv->base + dest_reg, value, size); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 98 | static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value, |
| 99 | uint32_t size) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 100 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 101 | memcpy_fromio(value, priv->base + src_reg, size); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 104 | static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 105 | { |
| 106 | const uint32_t bbar_mask = 0x00ffff00; |
| 107 | uint32_t ichspi_bbar; |
| 108 | |
Simon Glass | 07b2b99 | 2019-12-06 21:42:49 -0700 | [diff] [blame] | 109 | if (ctlr->bbar) { |
| 110 | minaddr &= bbar_mask; |
| 111 | ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask; |
| 112 | ichspi_bbar |= minaddr; |
| 113 | ich_writel(ctlr, ichspi_bbar, ctlr->bbar); |
| 114 | } |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 117 | /* @return 1 if the SPI flash supports the 33MHz speed */ |
Simon Glass | d500dd8 | 2019-12-06 21:42:41 -0700 | [diff] [blame] | 118 | static bool ich9_can_do_33mhz(struct udevice *dev) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 119 | { |
Simon Glass | 78d520c | 2019-12-06 21:42:38 -0700 | [diff] [blame] | 120 | struct ich_spi_priv *priv = dev_get_priv(dev); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 121 | u32 fdod, speed; |
| 122 | |
Simon Glass | bdd2897 | 2019-12-06 21:42:48 -0700 | [diff] [blame] | 123 | if (!CONFIG_IS_ENABLED(PCI)) |
| 124 | return false; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 125 | /* Observe SPI Descriptor Component Section 0 */ |
Simon Glass | 78d520c | 2019-12-06 21:42:38 -0700 | [diff] [blame] | 126 | dm_pci_write_config32(priv->pch, 0xb0, 0x1000); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 127 | |
| 128 | /* Extract the Write/Erase SPI Frequency from descriptor */ |
Simon Glass | 78d520c | 2019-12-06 21:42:38 -0700 | [diff] [blame] | 129 | dm_pci_read_config32(priv->pch, 0xb4, &fdod); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 130 | |
| 131 | /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */ |
| 132 | speed = (fdod >> 21) & 7; |
| 133 | |
| 134 | return speed == 1; |
| 135 | } |
| 136 | |
Bin Meng | 59de503 | 2017-10-18 18:20:57 -0700 | [diff] [blame] | 137 | static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase) |
| 138 | { |
| 139 | if (plat->ich_version == ICHV_7) { |
| 140 | struct ich7_spi_regs *ich7_spi = sbase; |
| 141 | |
| 142 | setbits_le16(&ich7_spi->spis, SPIS_LOCK); |
| 143 | } else if (plat->ich_version == ICHV_9) { |
| 144 | struct ich9_spi_regs *ich9_spi = sbase; |
| 145 | |
| 146 | setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN); |
| 147 | } |
| 148 | } |
| 149 | |
Bin Meng | 36ce024 | 2017-08-15 22:38:29 -0700 | [diff] [blame] | 150 | static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase) |
| 151 | { |
| 152 | int lock = 0; |
| 153 | |
| 154 | if (plat->ich_version == ICHV_7) { |
| 155 | struct ich7_spi_regs *ich7_spi = sbase; |
| 156 | |
| 157 | lock = readw(&ich7_spi->spis) & SPIS_LOCK; |
| 158 | } else if (plat->ich_version == ICHV_9) { |
| 159 | struct ich9_spi_regs *ich9_spi = sbase; |
| 160 | |
| 161 | lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN; |
| 162 | } |
| 163 | |
| 164 | return lock != 0; |
| 165 | } |
| 166 | |
Bin Meng | 36ce024 | 2017-08-15 22:38:29 -0700 | [diff] [blame] | 167 | static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans, |
| 168 | bool lock) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 169 | { |
| 170 | uint16_t optypes; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 171 | uint8_t opmenu[ctlr->menubytes]; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 172 | |
Bin Meng | 36ce024 | 2017-08-15 22:38:29 -0700 | [diff] [blame] | 173 | if (!lock) { |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 174 | /* The lock is off, so just use index 0. */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 175 | ich_writeb(ctlr, trans->opcode, ctlr->opmenu); |
| 176 | optypes = ich_readw(ctlr, ctlr->optype); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 177 | optypes = (optypes & 0xfffc) | (trans->type & 0x3); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 178 | ich_writew(ctlr, optypes, ctlr->optype); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 179 | return 0; |
| 180 | } else { |
| 181 | /* The lock is on. See if what we need is on the menu. */ |
| 182 | uint8_t optype; |
| 183 | uint16_t opcode_index; |
| 184 | |
| 185 | /* Write Enable is handled as atomic prefix */ |
| 186 | if (trans->opcode == SPI_OPCODE_WREN) |
| 187 | return 0; |
| 188 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 189 | read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu)); |
| 190 | for (opcode_index = 0; opcode_index < ctlr->menubytes; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 191 | opcode_index++) { |
| 192 | if (opmenu[opcode_index] == trans->opcode) |
| 193 | break; |
| 194 | } |
| 195 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 196 | if (opcode_index == ctlr->menubytes) { |
Simon Glass | d500dd8 | 2019-12-06 21:42:41 -0700 | [diff] [blame] | 197 | debug("ICH SPI: Opcode %x not found\n", trans->opcode); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 198 | return -EINVAL; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 201 | optypes = ich_readw(ctlr, ctlr->optype); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 202 | optype = (optypes >> (opcode_index * 2)) & 0x3; |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 203 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 204 | if (optype != trans->type) { |
Simon Glass | d500dd8 | 2019-12-06 21:42:41 -0700 | [diff] [blame] | 205 | debug("ICH SPI: Transaction doesn't fit type %d\n", |
| 206 | optype); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 207 | return -ENOSPC; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 208 | } |
| 209 | return opcode_index; |
| 210 | } |
| 211 | } |
| 212 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 213 | /* |
| 214 | * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 215 | * below is true) or 0. In case the wait was for the bit(s) to set - write |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 216 | * those bits back, which would cause resetting them. |
| 217 | * |
| 218 | * Return the last read status value on success or -1 on failure. |
| 219 | */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 220 | static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask, |
| 221 | int wait_til_set) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 222 | { |
| 223 | int timeout = 600000; /* This will result in 6s */ |
| 224 | u16 status = 0; |
| 225 | |
| 226 | while (timeout--) { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 227 | status = ich_readw(ctlr, ctlr->status); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 228 | if (wait_til_set ^ ((status & bitmask) == 0)) { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 229 | if (wait_til_set) { |
| 230 | ich_writew(ctlr, status & bitmask, |
| 231 | ctlr->status); |
| 232 | } |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 233 | return status; |
| 234 | } |
| 235 | udelay(10); |
| 236 | } |
Simon Glass | d500dd8 | 2019-12-06 21:42:41 -0700 | [diff] [blame] | 237 | debug("ICH SPI: SCIP timeout, read %x, expected %x, wts %x %x\n", |
| 238 | status, bitmask, wait_til_set, status & bitmask); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 239 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 240 | return -ETIMEDOUT; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 243 | static void ich_spi_config_opcode(struct udevice *dev) |
Bin Meng | 552720e | 2017-08-15 22:38:30 -0700 | [diff] [blame] | 244 | { |
| 245 | struct ich_spi_priv *ctlr = dev_get_priv(dev); |
| 246 | |
| 247 | /* |
| 248 | * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down |
| 249 | * to prevent accidental or intentional writes. Before they get |
| 250 | * locked down, these registers should be initialized properly. |
| 251 | */ |
| 252 | ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop); |
| 253 | ich_writew(ctlr, SPI_OPTYPE, ctlr->optype); |
| 254 | ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu); |
| 255 | ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32)); |
| 256 | } |
| 257 | |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 258 | static int ich_spi_exec_op_swseq(struct spi_slave *slave, |
| 259 | const struct spi_mem_op *op) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 260 | { |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 261 | struct udevice *bus = dev_get_parent(slave->dev); |
Simon Glass | 6634f81 | 2015-07-03 18:28:21 -0600 | [diff] [blame] | 262 | struct ich_spi_platdata *plat = dev_get_platdata(bus); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 263 | struct ich_spi_priv *ctlr = dev_get_priv(bus); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 264 | uint16_t control; |
| 265 | int16_t opcode_index; |
| 266 | int with_address; |
| 267 | int status; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 268 | struct spi_trans *trans = &ctlr->trans; |
Bin Meng | 36ce024 | 2017-08-15 22:38:29 -0700 | [diff] [blame] | 269 | bool lock = spi_lock_status(plat, ctlr->base); |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 270 | int ret = 0; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 271 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 272 | trans->in = NULL; |
| 273 | trans->out = NULL; |
| 274 | trans->type = 0xFF; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 275 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 276 | if (op->data.nbytes) { |
| 277 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 278 | trans->in = op->data.buf.in; |
| 279 | trans->bytesin = op->data.nbytes; |
| 280 | } else { |
| 281 | trans->out = op->data.buf.out; |
| 282 | trans->bytesout = op->data.nbytes; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 283 | } |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 286 | if (trans->opcode != op->cmd.opcode) |
| 287 | trans->opcode = op->cmd.opcode; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 288 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 289 | if (lock && trans->opcode == SPI_OPCODE_WRDIS) |
| 290 | return 0; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 291 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 292 | if (trans->opcode == SPI_OPCODE_WREN) { |
| 293 | /* |
| 294 | * Treat Write Enable as Atomic Pre-Op if possible |
| 295 | * in order to prevent the Management Engine from |
| 296 | * issuing a transaction between WREN and DATA. |
| 297 | */ |
| 298 | if (!lock) |
| 299 | ich_writew(ctlr, trans->opcode, ctlr->preop); |
| 300 | return 0; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 303 | ret = ich_status_poll(ctlr, SPIS_SCIP, 0); |
| 304 | if (ret < 0) |
| 305 | return ret; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 306 | |
Bin Meng | 0d3792c | 2016-02-01 01:40:38 -0800 | [diff] [blame] | 307 | if (plat->ich_version == ICHV_7) |
Simon Glass | 6634f81 | 2015-07-03 18:28:21 -0600 | [diff] [blame] | 308 | ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status); |
| 309 | else |
| 310 | ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 311 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 312 | /* Try to guess spi transaction type */ |
| 313 | if (op->data.dir == SPI_MEM_DATA_OUT) { |
| 314 | if (op->addr.nbytes) |
| 315 | trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS; |
| 316 | else |
| 317 | trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS; |
| 318 | } else { |
| 319 | if (op->addr.nbytes) |
| 320 | trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 321 | else |
| 322 | trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS; |
| 323 | } |
| 324 | /* Special erase case handling */ |
| 325 | if (op->addr.nbytes && !op->data.buswidth) |
| 326 | trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS; |
| 327 | |
Bin Meng | 36ce024 | 2017-08-15 22:38:29 -0700 | [diff] [blame] | 328 | opcode_index = spi_setup_opcode(ctlr, trans, lock); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 329 | if (opcode_index < 0) |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 330 | return -EINVAL; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 331 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 332 | if (op->addr.nbytes) { |
| 333 | trans->offset = op->addr.val; |
| 334 | with_address = 1; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 337 | if (ctlr->speed && ctlr->max_speed >= 33000000) { |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 338 | int byte; |
| 339 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 340 | byte = ich_readb(ctlr, ctlr->speed); |
| 341 | if (ctlr->cur_speed >= 33000000) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 342 | byte |= SSFC_SCF_33MHZ; |
| 343 | else |
| 344 | byte &= ~SSFC_SCF_33MHZ; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 345 | ich_writeb(ctlr, byte, ctlr->speed); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 348 | /* Preset control fields */ |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 349 | control = SPIC_SCGO | ((opcode_index & 0x07) << 4); |
| 350 | |
| 351 | /* Issue atomic preop cycle if needed */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 352 | if (ich_readw(ctlr, ctlr->preop)) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 353 | control |= SPIC_ACS; |
| 354 | |
| 355 | if (!trans->bytesout && !trans->bytesin) { |
| 356 | /* SPI addresses are 24 bit only */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 357 | if (with_address) { |
| 358 | ich_writel(ctlr, trans->offset & 0x00FFFFFF, |
| 359 | ctlr->addr); |
| 360 | } |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 361 | /* |
| 362 | * This is a 'no data' command (like Write Enable), its |
| 363 | * bitesout size was 1, decremented to zero while executing |
| 364 | * spi_setup_opcode() above. Tell the chip to send the |
| 365 | * command. |
| 366 | */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 367 | ich_writew(ctlr, control, ctlr->control); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 368 | |
| 369 | /* wait for the result */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 370 | status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1); |
| 371 | if (status < 0) |
| 372 | return status; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 373 | |
| 374 | if (status & SPIS_FCERR) { |
| 375 | debug("ICH SPI: Command transaction error\n"); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 376 | return -EIO; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 382 | while (trans->bytesout || trans->bytesin) { |
| 383 | uint32_t data_length; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 384 | |
| 385 | /* SPI addresses are 24 bit only */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 386 | ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 387 | |
| 388 | if (trans->bytesout) |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 389 | data_length = min(trans->bytesout, ctlr->databytes); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 390 | else |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 391 | data_length = min(trans->bytesin, ctlr->databytes); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 392 | |
| 393 | /* Program data into FDATA0 to N */ |
| 394 | if (trans->bytesout) { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 395 | write_reg(ctlr, trans->out, ctlr->data, data_length); |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 396 | trans->bytesout -= data_length; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Add proper control fields' values */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 400 | control &= ~((ctlr->databytes - 1) << 8); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 401 | control |= SPIC_DS; |
| 402 | control |= (data_length - 1) << 8; |
| 403 | |
| 404 | /* write it */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 405 | ich_writew(ctlr, control, ctlr->control); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 406 | |
Bin Meng | 316fd94 | 2016-02-01 01:40:36 -0800 | [diff] [blame] | 407 | /* Wait for Cycle Done Status or Flash Cycle Error */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 408 | status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1); |
| 409 | if (status < 0) |
| 410 | return status; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 411 | |
| 412 | if (status & SPIS_FCERR) { |
Simon Glass | 7f66bc1 | 2015-06-07 08:50:33 -0600 | [diff] [blame] | 413 | debug("ICH SPI: Data transaction error %x\n", status); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 414 | return -EIO; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (trans->bytesin) { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 418 | read_reg(ctlr, ctlr->data, trans->in, data_length); |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 419 | trans->bytesin -= data_length; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | /* Clear atomic preop now that xfer is done */ |
Bin Meng | 4a75e9b | 2017-08-26 19:22:59 -0700 | [diff] [blame] | 424 | if (!lock) |
| 425 | ich_writew(ctlr, 0, ctlr->preop); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 430 | /* |
| 431 | * Ensure read/write xfer len is not greater than SPIBAR_FDATA_FIFO_SIZE and |
| 432 | * that the operation does not cross page boundary. |
| 433 | */ |
| 434 | static uint get_xfer_len(u32 offset, int len, int page_size) |
| 435 | { |
| 436 | uint xfer_len = min(len, SPIBAR_FDATA_FIFO_SIZE); |
| 437 | uint bytes_left = ALIGN(offset, page_size) - offset; |
| 438 | |
| 439 | if (bytes_left) |
| 440 | xfer_len = min(xfer_len, bytes_left); |
| 441 | |
| 442 | return xfer_len; |
| 443 | } |
| 444 | |
| 445 | /* Fill FDATAn FIFO in preparation for a write transaction */ |
| 446 | static void fill_xfer_fifo(struct fast_spi_regs *regs, const void *data, |
| 447 | uint len) |
| 448 | { |
| 449 | memcpy(regs->fdata, data, len); |
| 450 | } |
| 451 | |
| 452 | /* Drain FDATAn FIFO after a read transaction populates data */ |
| 453 | static void drain_xfer_fifo(struct fast_spi_regs *regs, void *dest, uint len) |
| 454 | { |
| 455 | memcpy(dest, regs->fdata, len); |
| 456 | } |
| 457 | |
| 458 | /* Fire up a transfer using the hardware sequencer */ |
| 459 | static void start_hwseq_xfer(struct fast_spi_regs *regs, uint hsfsts_cycle, |
| 460 | uint offset, uint len) |
| 461 | { |
| 462 | /* Make sure all W1C status bits get cleared */ |
| 463 | u32 hsfsts; |
| 464 | |
| 465 | hsfsts = readl(®s->hsfsts_ctl); |
| 466 | hsfsts &= ~(HSFSTS_FCYCLE_MASK | HSFSTS_FDBC_MASK); |
| 467 | hsfsts |= HSFSTS_AEL | HSFSTS_FCERR | HSFSTS_FDONE; |
| 468 | |
| 469 | /* Set up transaction parameters */ |
| 470 | hsfsts |= hsfsts_cycle << HSFSTS_FCYCLE_SHIFT; |
| 471 | hsfsts |= ((len - 1) << HSFSTS_FDBC_SHIFT) & HSFSTS_FDBC_MASK; |
| 472 | hsfsts |= HSFSTS_FGO; |
| 473 | |
| 474 | writel(offset, ®s->faddr); |
| 475 | writel(hsfsts, ®s->hsfsts_ctl); |
| 476 | } |
| 477 | |
| 478 | static int wait_for_hwseq_xfer(struct fast_spi_regs *regs, uint offset) |
| 479 | { |
| 480 | ulong start; |
| 481 | u32 hsfsts; |
| 482 | |
| 483 | start = get_timer(0); |
| 484 | do { |
| 485 | hsfsts = readl(®s->hsfsts_ctl); |
| 486 | if (hsfsts & HSFSTS_FCERR) { |
| 487 | debug("SPI transaction error at offset %x HSFSTS = %08x\n", |
| 488 | offset, hsfsts); |
| 489 | return -EIO; |
| 490 | } |
| 491 | if (hsfsts & HSFSTS_AEL) |
| 492 | return -EPERM; |
| 493 | |
| 494 | if (hsfsts & HSFSTS_FDONE) |
| 495 | return 0; |
| 496 | } while (get_timer(start) < SPIBAR_HWSEQ_XFER_TIMEOUT_MS); |
| 497 | |
| 498 | debug("SPI transaction timeout at offset %x HSFSTS = %08x, timer %d\n", |
| 499 | offset, hsfsts, (uint)get_timer(start)); |
| 500 | |
| 501 | return -ETIMEDOUT; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * exec_sync_hwseq_xfer() - Execute flash transfer by hardware sequencing |
| 506 | * |
| 507 | * This waits until complete or timeout |
| 508 | * |
| 509 | * @regs: SPI registers |
| 510 | * @hsfsts_cycle: Cycle type (enum hsfsts_cycle_t) |
| 511 | * @offset: Offset to access |
| 512 | * @len: Number of bytes to transfer (can be 0) |
| 513 | * @return 0 if OK, -EIO on flash-cycle error (FCERR), -EPERM on access error |
| 514 | * (AEL), -ETIMEDOUT on timeout |
| 515 | */ |
| 516 | static int exec_sync_hwseq_xfer(struct fast_spi_regs *regs, uint hsfsts_cycle, |
| 517 | uint offset, uint len) |
| 518 | { |
| 519 | start_hwseq_xfer(regs, hsfsts_cycle, offset, len); |
| 520 | |
| 521 | return wait_for_hwseq_xfer(regs, offset); |
| 522 | } |
| 523 | |
| 524 | static int ich_spi_exec_op_hwseq(struct spi_slave *slave, |
| 525 | const struct spi_mem_op *op) |
| 526 | { |
| 527 | struct spi_flash *flash = dev_get_uclass_priv(slave->dev); |
| 528 | struct udevice *bus = dev_get_parent(slave->dev); |
| 529 | struct ich_spi_priv *priv = dev_get_priv(bus); |
| 530 | struct fast_spi_regs *regs = priv->base; |
| 531 | uint page_size; |
| 532 | uint offset; |
| 533 | int cycle; |
| 534 | uint len; |
| 535 | bool out; |
| 536 | int ret; |
| 537 | u8 *buf; |
| 538 | |
| 539 | offset = op->addr.val; |
| 540 | len = op->data.nbytes; |
| 541 | |
| 542 | switch (op->cmd.opcode) { |
| 543 | case SPINOR_OP_RDID: |
| 544 | cycle = HSFSTS_CYCLE_RDID; |
| 545 | break; |
| 546 | case SPINOR_OP_READ_FAST: |
| 547 | cycle = HSFSTS_CYCLE_READ; |
| 548 | break; |
| 549 | case SPINOR_OP_PP: |
| 550 | cycle = HSFSTS_CYCLE_WRITE; |
| 551 | break; |
| 552 | case SPINOR_OP_WREN: |
| 553 | /* Nothing needs to be done */ |
| 554 | return 0; |
| 555 | case SPINOR_OP_WRSR: |
| 556 | cycle = HSFSTS_CYCLE_WR_STATUS; |
| 557 | break; |
| 558 | case SPINOR_OP_RDSR: |
| 559 | cycle = HSFSTS_CYCLE_RD_STATUS; |
| 560 | break; |
| 561 | case SPINOR_OP_WRDI: |
| 562 | return 0; /* ignore */ |
| 563 | case SPINOR_OP_BE_4K: |
| 564 | cycle = HSFSTS_CYCLE_4K_ERASE; |
Wolfgang Wallner | 6157ec1 | 2020-01-14 14:05:48 +0100 | [diff] [blame] | 565 | ret = exec_sync_hwseq_xfer(regs, cycle, offset, 0); |
| 566 | return ret; |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 567 | default: |
| 568 | debug("Unknown cycle %x\n", op->cmd.opcode); |
| 569 | return -EINVAL; |
| 570 | }; |
| 571 | |
| 572 | out = op->data.dir == SPI_MEM_DATA_OUT; |
| 573 | buf = out ? (u8 *)op->data.buf.out : op->data.buf.in; |
| 574 | page_size = flash->page_size ? : 256; |
| 575 | |
| 576 | while (len) { |
| 577 | uint xfer_len = get_xfer_len(offset, len, page_size); |
| 578 | |
| 579 | if (out) |
| 580 | fill_xfer_fifo(regs, buf, xfer_len); |
| 581 | |
| 582 | ret = exec_sync_hwseq_xfer(regs, cycle, offset, xfer_len); |
| 583 | if (ret) |
| 584 | return ret; |
| 585 | |
| 586 | if (!out) |
| 587 | drain_xfer_fifo(regs, buf, xfer_len); |
| 588 | |
| 589 | offset += xfer_len; |
| 590 | buf += xfer_len; |
| 591 | len -= xfer_len; |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static int ich_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op) |
| 598 | { |
| 599 | struct udevice *bus = dev_get_parent(slave->dev); |
| 600 | struct ich_spi_platdata *plat = dev_get_platdata(bus); |
| 601 | int ret; |
| 602 | |
| 603 | bootstage_start(BOOTSTAGE_ID_ACCUM_SPI, "fast_spi"); |
| 604 | if (plat->hwseq) |
| 605 | ret = ich_spi_exec_op_hwseq(slave, op); |
| 606 | else |
| 607 | ret = ich_spi_exec_op_swseq(slave, op); |
| 608 | bootstage_accum(BOOTSTAGE_ID_ACCUM_SPI); |
| 609 | |
| 610 | return ret; |
| 611 | } |
| 612 | |
Simon Glass | 641217d | 2019-12-06 21:42:47 -0700 | [diff] [blame] | 613 | static int ich_get_mmap_bus(struct udevice *bus, ulong *map_basep, |
| 614 | uint *map_sizep, uint *offsetp) |
| 615 | { |
| 616 | pci_dev_t spi_bdf; |
| 617 | |
| 618 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 619 | struct pci_child_platdata *pplat = dev_get_parent_platdata(bus); |
| 620 | |
| 621 | spi_bdf = pplat->devfn; |
| 622 | #else |
| 623 | struct ich_spi_platdata *plat = dev_get_platdata(bus); |
| 624 | |
| 625 | /* |
| 626 | * We cannot rely on plat->bdf being set up yet since this method can |
| 627 | * be called before the device is probed. Use the of-platdata directly |
| 628 | * instead. |
| 629 | */ |
| 630 | spi_bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]); |
| 631 | #endif |
| 632 | |
| 633 | return fast_spi_get_bios_mmap(spi_bdf, map_basep, map_sizep, offsetp); |
| 634 | } |
| 635 | |
| 636 | static int ich_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep, |
| 637 | uint *offsetp) |
| 638 | { |
| 639 | struct udevice *bus = dev_get_parent(dev); |
| 640 | |
| 641 | return ich_get_mmap_bus(bus, map_basep, map_sizep, offsetp); |
| 642 | } |
| 643 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 644 | static int ich_spi_adjust_size(struct spi_slave *slave, struct spi_mem_op *op) |
| 645 | { |
| 646 | unsigned int page_offset; |
| 647 | int addr = op->addr.val; |
| 648 | unsigned int byte_count = op->data.nbytes; |
| 649 | |
| 650 | if (hweight32(ICH_BOUNDARY) == 1) { |
| 651 | page_offset = addr & (ICH_BOUNDARY - 1); |
| 652 | } else { |
| 653 | u64 aux = addr; |
| 654 | |
| 655 | page_offset = do_div(aux, ICH_BOUNDARY); |
| 656 | } |
| 657 | |
Simon Glass | f1c884d | 2019-12-06 21:42:44 -0700 | [diff] [blame] | 658 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 659 | if (slave->max_read_size) { |
| 660 | op->data.nbytes = min(ICH_BOUNDARY - page_offset, |
| 661 | slave->max_read_size); |
| 662 | } |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 663 | } else if (slave->max_write_size) { |
| 664 | op->data.nbytes = min(ICH_BOUNDARY - page_offset, |
| 665 | slave->max_write_size); |
| 666 | } |
| 667 | |
| 668 | op->data.nbytes = min(op->data.nbytes, byte_count); |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
Simon Glass | 78d520c | 2019-12-06 21:42:38 -0700 | [diff] [blame] | 673 | static int ich_protect_lockdown(struct udevice *dev) |
| 674 | { |
| 675 | struct ich_spi_platdata *plat = dev_get_platdata(dev); |
| 676 | struct ich_spi_priv *priv = dev_get_priv(dev); |
| 677 | int ret = -ENOSYS; |
| 678 | |
| 679 | /* Disable the BIOS write protect so write commands are allowed */ |
| 680 | if (priv->pch) |
| 681 | ret = pch_set_spi_protect(priv->pch, false); |
| 682 | if (ret == -ENOSYS) { |
| 683 | u8 bios_cntl; |
| 684 | |
| 685 | bios_cntl = ich_readb(priv, priv->bcr); |
| 686 | bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */ |
| 687 | bios_cntl |= 1; /* Write Protect Disable (WPD) */ |
| 688 | ich_writeb(priv, bios_cntl, priv->bcr); |
| 689 | } else if (ret) { |
| 690 | debug("%s: Failed to disable write-protect: err=%d\n", |
| 691 | __func__, ret); |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | /* Lock down SPI controller settings if required */ |
| 696 | if (plat->lockdown) { |
| 697 | ich_spi_config_opcode(dev); |
| 698 | spi_lock_down(plat, priv->base); |
| 699 | } |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Simon Glass | 23485eb | 2019-12-06 21:42:37 -0700 | [diff] [blame] | 704 | static int ich_init_controller(struct udevice *dev, |
| 705 | struct ich_spi_platdata *plat, |
| 706 | struct ich_spi_priv *ctlr) |
| 707 | { |
Simon Glass | bdd2897 | 2019-12-06 21:42:48 -0700 | [diff] [blame] | 708 | if (spl_phase() == PHASE_TPL) { |
| 709 | struct ich_spi_platdata *plat = dev_get_platdata(dev); |
| 710 | int ret; |
| 711 | |
| 712 | ret = fast_spi_early_init(plat->bdf, plat->mmio_base); |
| 713 | if (ret) |
| 714 | return ret; |
| 715 | } |
| 716 | |
Simon Glass | eb0ae6f | 2019-12-06 21:42:42 -0700 | [diff] [blame] | 717 | ctlr->base = (void *)plat->mmio_base; |
Simon Glass | 23485eb | 2019-12-06 21:42:37 -0700 | [diff] [blame] | 718 | if (plat->ich_version == ICHV_7) { |
Simon Glass | eb0ae6f | 2019-12-06 21:42:42 -0700 | [diff] [blame] | 719 | struct ich7_spi_regs *ich7_spi = ctlr->base; |
Simon Glass | 23485eb | 2019-12-06 21:42:37 -0700 | [diff] [blame] | 720 | |
| 721 | ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu); |
| 722 | ctlr->menubytes = sizeof(ich7_spi->opmenu); |
| 723 | ctlr->optype = offsetof(struct ich7_spi_regs, optype); |
| 724 | ctlr->addr = offsetof(struct ich7_spi_regs, spia); |
| 725 | ctlr->data = offsetof(struct ich7_spi_regs, spid); |
| 726 | ctlr->databytes = sizeof(ich7_spi->spid); |
| 727 | ctlr->status = offsetof(struct ich7_spi_regs, spis); |
| 728 | ctlr->control = offsetof(struct ich7_spi_regs, spic); |
| 729 | ctlr->bbar = offsetof(struct ich7_spi_regs, bbar); |
| 730 | ctlr->preop = offsetof(struct ich7_spi_regs, preop); |
Simon Glass | 23485eb | 2019-12-06 21:42:37 -0700 | [diff] [blame] | 731 | } else if (plat->ich_version == ICHV_9) { |
Simon Glass | eb0ae6f | 2019-12-06 21:42:42 -0700 | [diff] [blame] | 732 | struct ich9_spi_regs *ich9_spi = ctlr->base; |
Simon Glass | 23485eb | 2019-12-06 21:42:37 -0700 | [diff] [blame] | 733 | |
| 734 | ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu); |
| 735 | ctlr->menubytes = sizeof(ich9_spi->opmenu); |
| 736 | ctlr->optype = offsetof(struct ich9_spi_regs, optype); |
| 737 | ctlr->addr = offsetof(struct ich9_spi_regs, faddr); |
| 738 | ctlr->data = offsetof(struct ich9_spi_regs, fdata); |
| 739 | ctlr->databytes = sizeof(ich9_spi->fdata); |
| 740 | ctlr->status = offsetof(struct ich9_spi_regs, ssfs); |
| 741 | ctlr->control = offsetof(struct ich9_spi_regs, ssfc); |
| 742 | ctlr->speed = ctlr->control + 2; |
| 743 | ctlr->bbar = offsetof(struct ich9_spi_regs, bbar); |
| 744 | ctlr->preop = offsetof(struct ich9_spi_regs, preop); |
| 745 | ctlr->bcr = offsetof(struct ich9_spi_regs, bcr); |
| 746 | ctlr->pr = &ich9_spi->pr[0]; |
Simon Glass | 07b2b99 | 2019-12-06 21:42:49 -0700 | [diff] [blame] | 747 | } else if (plat->ich_version == ICHV_APL) { |
Simon Glass | 23485eb | 2019-12-06 21:42:37 -0700 | [diff] [blame] | 748 | } else { |
| 749 | debug("ICH SPI: Unrecognised ICH version %d\n", |
| 750 | plat->ich_version); |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | |
| 754 | /* Work out the maximum speed we can support */ |
| 755 | ctlr->max_speed = 20000000; |
| 756 | if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev)) |
| 757 | ctlr->max_speed = 33000000; |
Simon Glass | eb0ae6f | 2019-12-06 21:42:42 -0700 | [diff] [blame] | 758 | debug("ICH SPI: Version ID %d detected at %lx, speed %ld\n", |
| 759 | plat->ich_version, plat->mmio_base, ctlr->max_speed); |
Simon Glass | 23485eb | 2019-12-06 21:42:37 -0700 | [diff] [blame] | 760 | |
| 761 | ich_set_bbar(ctlr, 0); |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
Simon Glass | bdd2897 | 2019-12-06 21:42:48 -0700 | [diff] [blame] | 766 | static int ich_cache_bios_region(struct udevice *dev) |
| 767 | { |
| 768 | ulong map_base; |
| 769 | uint map_size; |
| 770 | uint offset; |
| 771 | ulong base; |
| 772 | int ret; |
| 773 | |
| 774 | ret = ich_get_mmap_bus(dev, &map_base, &map_size, &offset); |
| 775 | if (ret) |
| 776 | return ret; |
| 777 | |
| 778 | /* Don't use WRBACK since we are not supposed to write to SPI flash */ |
| 779 | base = SZ_4G - map_size; |
| 780 | mtrr_set_next_var(MTRR_TYPE_WRPROT, base, map_size); |
| 781 | log_debug("BIOS cache base=%lx, size=%x\n", base, (uint)map_size); |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame] | 786 | static int ich_spi_probe(struct udevice *dev) |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 787 | { |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame] | 788 | struct ich_spi_platdata *plat = dev_get_platdata(dev); |
| 789 | struct ich_spi_priv *priv = dev_get_priv(dev); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 790 | int ret; |
| 791 | |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame] | 792 | ret = ich_init_controller(dev, plat, priv); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 793 | if (ret) |
| 794 | return ret; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 795 | |
Simon Glass | bdd2897 | 2019-12-06 21:42:48 -0700 | [diff] [blame] | 796 | if (spl_phase() == PHASE_TPL) { |
| 797 | /* Cache the BIOS to speed things up */ |
| 798 | ret = ich_cache_bios_region(dev); |
| 799 | if (ret) |
| 800 | return ret; |
| 801 | } else { |
| 802 | ret = ich_protect_lockdown(dev); |
| 803 | if (ret) |
| 804 | return ret; |
| 805 | } |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 806 | priv->cur_speed = priv->max_speed; |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Stefan Roese | b6647ab | 2017-04-24 09:48:04 +0200 | [diff] [blame] | 811 | static int ich_spi_remove(struct udevice *bus) |
| 812 | { |
Stefan Roese | b6647ab | 2017-04-24 09:48:04 +0200 | [diff] [blame] | 813 | /* |
| 814 | * Configure SPI controller so that the Linux MTD driver can fully |
| 815 | * access the SPI NOR chip |
| 816 | */ |
Bin Meng | 552720e | 2017-08-15 22:38:30 -0700 | [diff] [blame] | 817 | ich_spi_config_opcode(bus); |
Stefan Roese | b6647ab | 2017-04-24 09:48:04 +0200 | [diff] [blame] | 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 822 | static int ich_spi_set_speed(struct udevice *bus, uint speed) |
| 823 | { |
| 824 | struct ich_spi_priv *priv = dev_get_priv(bus); |
| 825 | |
| 826 | priv->cur_speed = speed; |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | static int ich_spi_set_mode(struct udevice *bus, uint mode) |
| 832 | { |
| 833 | debug("%s: mode=%d\n", __func__, mode); |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static int ich_spi_child_pre_probe(struct udevice *dev) |
| 839 | { |
| 840 | struct udevice *bus = dev_get_parent(dev); |
| 841 | struct ich_spi_platdata *plat = dev_get_platdata(bus); |
| 842 | struct ich_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 843 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 844 | |
| 845 | /* |
| 846 | * Yes this controller can only write a small number of bytes at |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 847 | * once! The limit is typically 64 bytes. For hardware sequencing a |
| 848 | * a loop is used to get around this. |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 849 | */ |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 850 | if (!plat->hwseq) |
| 851 | slave->max_write_size = priv->databytes; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 852 | /* |
| 853 | * ICH 7 SPI controller only supports array read command |
| 854 | * and byte program command for SST flash |
| 855 | */ |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 856 | if (plat->ich_version == ICHV_7) |
| 857 | slave->mode = SPI_RX_SLOW | SPI_TX_BYTE; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
Bin Meng | d940667 | 2016-02-01 01:40:37 -0800 | [diff] [blame] | 862 | static int ich_spi_ofdata_to_platdata(struct udevice *dev) |
| 863 | { |
| 864 | struct ich_spi_platdata *plat = dev_get_platdata(dev); |
Simon Glass | b7632cb | 2019-12-06 21:42:45 -0700 | [diff] [blame] | 865 | |
| 866 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 78d520c | 2019-12-06 21:42:38 -0700 | [diff] [blame] | 867 | struct ich_spi_priv *priv = dev_get_priv(dev); |
Bin Meng | d940667 | 2016-02-01 01:40:37 -0800 | [diff] [blame] | 868 | |
Simon Glass | 78d520c | 2019-12-06 21:42:38 -0700 | [diff] [blame] | 869 | /* Find a PCH if there is one */ |
| 870 | uclass_first_device(UCLASS_PCH, &priv->pch); |
| 871 | if (!priv->pch) |
| 872 | priv->pch = dev_get_parent(dev); |
| 873 | |
Simon Glass | 6e37af3 | 2019-12-06 21:42:39 -0700 | [diff] [blame] | 874 | plat->ich_version = dev_get_driver_data(dev); |
| 875 | plat->lockdown = dev_read_bool(dev, "intel,spi-lock-down"); |
Simon Glass | 07b2b99 | 2019-12-06 21:42:49 -0700 | [diff] [blame] | 876 | if (plat->ich_version == ICHV_APL) { |
| 877 | plat->mmio_base = dm_pci_read_bar32(dev, 0); |
| 878 | } else { |
| 879 | /* SBASE is similar */ |
| 880 | pch_get_spi_base(priv->pch, &plat->mmio_base); |
| 881 | } |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 882 | /* |
| 883 | * Use an int so that the property is present in of-platdata even |
| 884 | * when false. |
| 885 | */ |
| 886 | plat->hwseq = dev_read_u32_default(dev, "intel,hardware-seq", 0); |
Simon Glass | b7632cb | 2019-12-06 21:42:45 -0700 | [diff] [blame] | 887 | #else |
| 888 | plat->ich_version = ICHV_APL; |
| 889 | plat->mmio_base = plat->dtplat.early_regs[0]; |
| 890 | plat->bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]); |
Simon Glass | 0a88fd8 | 2019-12-06 21:42:46 -0700 | [diff] [blame] | 891 | plat->hwseq = plat->dtplat.intel_hardware_seq; |
Simon Glass | b7632cb | 2019-12-06 21:42:45 -0700 | [diff] [blame] | 892 | #endif |
| 893 | debug("%s: mmio_base=%lx\n", __func__, plat->mmio_base); |
Simon Glass | eb0ae6f | 2019-12-06 21:42:42 -0700 | [diff] [blame] | 894 | |
Simon Glass | 6e37af3 | 2019-12-06 21:42:39 -0700 | [diff] [blame] | 895 | return 0; |
Bin Meng | d940667 | 2016-02-01 01:40:37 -0800 | [diff] [blame] | 896 | } |
| 897 | |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 898 | static const struct spi_controller_mem_ops ich_controller_mem_ops = { |
| 899 | .adjust_op_size = ich_spi_adjust_size, |
| 900 | .supports_op = NULL, |
| 901 | .exec_op = ich_spi_exec_op, |
| 902 | }; |
| 903 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 904 | static const struct dm_spi_ops ich_spi_ops = { |
Simon Glass | 2d2e860 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 905 | /* xfer is not supported */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 906 | .set_speed = ich_spi_set_speed, |
| 907 | .set_mode = ich_spi_set_mode, |
Bernhard Messerklinger | db3ffe9 | 2019-08-02 08:38:34 +0200 | [diff] [blame] | 908 | .mem_ops = &ich_controller_mem_ops, |
Simon Glass | 641217d | 2019-12-06 21:42:47 -0700 | [diff] [blame] | 909 | .get_mmap = ich_get_mmap, |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 910 | /* |
| 911 | * cs_info is not needed, since we require all chip selects to be |
| 912 | * in the device tree explicitly |
| 913 | */ |
| 914 | }; |
| 915 | |
| 916 | static const struct udevice_id ich_spi_ids[] = { |
Simon Glass | 6e37af3 | 2019-12-06 21:42:39 -0700 | [diff] [blame] | 917 | { .compatible = "intel,ich7-spi", ICHV_7 }, |
| 918 | { .compatible = "intel,ich9-spi", ICHV_9 }, |
Simon Glass | 07b2b99 | 2019-12-06 21:42:49 -0700 | [diff] [blame] | 919 | { .compatible = "intel,fast-spi", ICHV_APL }, |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 920 | { } |
| 921 | }; |
| 922 | |
Simon Glass | b7632cb | 2019-12-06 21:42:45 -0700 | [diff] [blame] | 923 | U_BOOT_DRIVER(intel_fast_spi) = { |
| 924 | .name = "intel_fast_spi", |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 925 | .id = UCLASS_SPI, |
| 926 | .of_match = ich_spi_ids, |
| 927 | .ops = &ich_spi_ops, |
Bin Meng | d940667 | 2016-02-01 01:40:37 -0800 | [diff] [blame] | 928 | .ofdata_to_platdata = ich_spi_ofdata_to_platdata, |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 929 | .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata), |
| 930 | .priv_auto_alloc_size = sizeof(struct ich_spi_priv), |
| 931 | .child_pre_probe = ich_spi_child_pre_probe, |
| 932 | .probe = ich_spi_probe, |
Stefan Roese | b6647ab | 2017-04-24 09:48:04 +0200 | [diff] [blame] | 933 | .remove = ich_spi_remove, |
| 934 | .flags = DM_FLAG_OS_PREPARE, |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 935 | }; |