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