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