Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-12 The Chromium OS Authors. |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is derived from the flashrom project. |
| 7 | */ |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | a08ca38 | 2015-01-27 22:13:43 -0700 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 12 | #include <pch.h> |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 13 | #include <pci.h> |
| 14 | #include <pci_ids.h> |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 15 | #include <spi.h> |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | |
| 18 | #include "ich.h" |
| 19 | |
| 20 | #define SPI_OPCODE_WREN 0x06 |
| 21 | #define SPI_OPCODE_FAST_READ 0x0b |
| 22 | |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 23 | #ifdef DEBUG_TRACE |
| 24 | #define debug_trace(fmt, args...) debug(fmt, ##args) |
| 25 | #else |
| 26 | #define debug_trace(x, args...) |
| 27 | #endif |
| 28 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 29 | struct ich_spi_platdata { |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 30 | enum pch_version ich_version; /* Controller version, 7 or 9 */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct ich_spi_priv { |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 34 | int ichspi_lock; |
| 35 | int locked; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 36 | int opmenu; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 37 | int menubytes; |
| 38 | void *base; /* Base of register set */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 39 | int preop; |
| 40 | int optype; |
| 41 | int addr; |
| 42 | int data; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 43 | unsigned databytes; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 44 | int status; |
| 45 | int control; |
| 46 | int bbar; |
Simon Glass | bf1623b | 2015-07-03 18:28:22 -0600 | [diff] [blame] | 47 | int bcr; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 48 | uint32_t *pr; /* only for ich9 */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 49 | int speed; /* pointer to speed control */ |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 50 | ulong max_speed; /* Maximum bus speed in MHz */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 51 | ulong cur_speed; /* Current bus speed */ |
| 52 | struct spi_trans trans; /* current transaction in progress */ |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 55 | static u8 ich_readb(struct ich_spi_priv *priv, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 56 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 57 | u8 value = readb(priv->base + reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 58 | |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 59 | debug_trace("read %2.2x from %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 60 | |
| 61 | return value; |
| 62 | } |
| 63 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 64 | static u16 ich_readw(struct ich_spi_priv *priv, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 65 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 66 | u16 value = readw(priv->base + reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 67 | |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 68 | debug_trace("read %4.4x from %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 69 | |
| 70 | return value; |
| 71 | } |
| 72 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 73 | static u32 ich_readl(struct ich_spi_priv *priv, int reg) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 74 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 75 | u32 value = readl(priv->base + reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 76 | |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 77 | debug_trace("read %8.8x from %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 78 | |
| 79 | return value; |
| 80 | } |
| 81 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 82 | 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] | 83 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 84 | writeb(value, priv->base + reg); |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 85 | debug_trace("wrote %2.2x to %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 88 | 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] | 89 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 90 | writew(value, priv->base + reg); |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 91 | debug_trace("wrote %4.4x to %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 94 | 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] | 95 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 96 | writel(value, priv->base + reg); |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 97 | debug_trace("wrote %8.8x to %4.4x\n", value, reg); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 100 | static void write_reg(struct ich_spi_priv *priv, const void *value, |
| 101 | int dest_reg, uint32_t size) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 102 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 103 | memcpy_toio(priv->base + dest_reg, value, size); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 106 | static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value, |
| 107 | uint32_t size) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 108 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 109 | memcpy_fromio(value, priv->base + src_reg, size); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 112 | 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] | 113 | { |
| 114 | const uint32_t bbar_mask = 0x00ffff00; |
| 115 | uint32_t ichspi_bbar; |
| 116 | |
| 117 | minaddr &= bbar_mask; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 118 | ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 119 | ichspi_bbar |= minaddr; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 120 | ich_writel(ctlr, ichspi_bbar, ctlr->bbar); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 123 | /* @return 1 if the SPI flash supports the 33MHz speed */ |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 124 | static int ich9_can_do_33mhz(struct udevice *dev) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 125 | { |
| 126 | u32 fdod, speed; |
| 127 | |
| 128 | /* Observe SPI Descriptor Component Section 0 */ |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 129 | dm_pci_write_config32(dev->parent, 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 | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 132 | dm_pci_read_config32(dev->parent, 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 | |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 140 | static int ich_init_controller(struct udevice *dev, |
| 141 | struct ich_spi_platdata *plat, |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 142 | struct ich_spi_priv *ctlr) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 143 | { |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 144 | ulong sbase_addr; |
| 145 | void *sbase; |
Simon Glass | a08ca38 | 2015-01-27 22:13:43 -0700 | [diff] [blame] | 146 | |
| 147 | /* SBASE is similar */ |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 148 | pch_get_sbase(dev->parent, &sbase_addr); |
| 149 | sbase = (void *)sbase_addr; |
| 150 | debug("%s: sbase=%p\n", __func__, sbase); |
Simon Glass | a08ca38 | 2015-01-27 22:13:43 -0700 | [diff] [blame] | 151 | |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 152 | if (plat->ich_version == PCHV_7) { |
| 153 | struct ich7_spi_regs *ich7_spi = sbase; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 154 | |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 155 | ich7_spi = (struct ich7_spi_regs *)sbase; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 156 | ctlr->ichspi_lock = readw(&ich7_spi->spis) & SPIS_LOCK; |
| 157 | ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 158 | ctlr->menubytes = sizeof(ich7_spi->opmenu); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 159 | ctlr->optype = offsetof(struct ich7_spi_regs, optype); |
| 160 | ctlr->addr = offsetof(struct ich7_spi_regs, spia); |
| 161 | ctlr->data = offsetof(struct ich7_spi_regs, spid); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 162 | ctlr->databytes = sizeof(ich7_spi->spid); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 163 | ctlr->status = offsetof(struct ich7_spi_regs, spis); |
| 164 | ctlr->control = offsetof(struct ich7_spi_regs, spic); |
| 165 | ctlr->bbar = offsetof(struct ich7_spi_regs, bbar); |
| 166 | ctlr->preop = offsetof(struct ich7_spi_regs, preop); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 167 | ctlr->base = ich7_spi; |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 168 | } else if (plat->ich_version == PCHV_9) { |
| 169 | struct ich9_spi_regs *ich9_spi = sbase; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 170 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 171 | ctlr->ichspi_lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN; |
| 172 | ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 173 | ctlr->menubytes = sizeof(ich9_spi->opmenu); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 174 | ctlr->optype = offsetof(struct ich9_spi_regs, optype); |
| 175 | ctlr->addr = offsetof(struct ich9_spi_regs, faddr); |
| 176 | ctlr->data = offsetof(struct ich9_spi_regs, fdata); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 177 | ctlr->databytes = sizeof(ich9_spi->fdata); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 178 | ctlr->status = offsetof(struct ich9_spi_regs, ssfs); |
| 179 | ctlr->control = offsetof(struct ich9_spi_regs, ssfc); |
| 180 | ctlr->speed = ctlr->control + 2; |
| 181 | ctlr->bbar = offsetof(struct ich9_spi_regs, bbar); |
| 182 | ctlr->preop = offsetof(struct ich9_spi_regs, preop); |
Simon Glass | bf1623b | 2015-07-03 18:28:22 -0600 | [diff] [blame] | 183 | ctlr->bcr = offsetof(struct ich9_spi_regs, bcr); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 184 | ctlr->pr = &ich9_spi->pr[0]; |
| 185 | ctlr->base = ich9_spi; |
| 186 | } else { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 187 | debug("ICH SPI: Unrecognised ICH version %d\n", |
| 188 | plat->ich_version); |
| 189 | return -EINVAL; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 190 | } |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 191 | |
| 192 | /* Work out the maximum speed we can support */ |
| 193 | ctlr->max_speed = 20000000; |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 194 | if (plat->ich_version == PCHV_9 && ich9_can_do_33mhz(dev)) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 195 | ctlr->max_speed = 33000000; |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 196 | debug("ICH SPI: Version ID %d detected at %p, speed %ld\n", |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 197 | plat->ich_version, ctlr->base, ctlr->max_speed); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 198 | |
| 199 | ich_set_bbar(ctlr, 0); |
| 200 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 204 | static inline void spi_use_out(struct spi_trans *trans, unsigned bytes) |
| 205 | { |
| 206 | trans->out += bytes; |
| 207 | trans->bytesout -= bytes; |
| 208 | } |
| 209 | |
| 210 | static inline void spi_use_in(struct spi_trans *trans, unsigned bytes) |
| 211 | { |
| 212 | trans->in += bytes; |
| 213 | trans->bytesin -= bytes; |
| 214 | } |
| 215 | |
| 216 | static void spi_setup_type(struct spi_trans *trans, int data_bytes) |
| 217 | { |
| 218 | trans->type = 0xFF; |
| 219 | |
| 220 | /* Try to guess spi type from read/write sizes. */ |
| 221 | if (trans->bytesin == 0) { |
| 222 | if (trans->bytesout + data_bytes > 4) |
| 223 | /* |
| 224 | * If bytesin = 0 and bytesout > 4, we presume this is |
| 225 | * a write data operation, which is accompanied by an |
| 226 | * address. |
| 227 | */ |
| 228 | trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS; |
| 229 | else |
| 230 | trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS; |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (trans->bytesout == 1) { /* and bytesin is > 0 */ |
| 235 | trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS; |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | if (trans->bytesout == 4) /* and bytesin is > 0 */ |
| 240 | trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 241 | |
| 242 | /* Fast read command is called with 5 bytes instead of 4 */ |
| 243 | if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) { |
| 244 | trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS; |
| 245 | --trans->bytesout; |
| 246 | } |
| 247 | } |
| 248 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 249 | static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 250 | { |
| 251 | uint16_t optypes; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 252 | uint8_t opmenu[ctlr->menubytes]; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 253 | |
| 254 | trans->opcode = trans->out[0]; |
| 255 | spi_use_out(trans, 1); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 256 | if (!ctlr->ichspi_lock) { |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 257 | /* The lock is off, so just use index 0. */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 258 | ich_writeb(ctlr, trans->opcode, ctlr->opmenu); |
| 259 | optypes = ich_readw(ctlr, ctlr->optype); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 260 | optypes = (optypes & 0xfffc) | (trans->type & 0x3); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 261 | ich_writew(ctlr, optypes, ctlr->optype); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 262 | return 0; |
| 263 | } else { |
| 264 | /* The lock is on. See if what we need is on the menu. */ |
| 265 | uint8_t optype; |
| 266 | uint16_t opcode_index; |
| 267 | |
| 268 | /* Write Enable is handled as atomic prefix */ |
| 269 | if (trans->opcode == SPI_OPCODE_WREN) |
| 270 | return 0; |
| 271 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 272 | read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu)); |
| 273 | for (opcode_index = 0; opcode_index < ctlr->menubytes; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 274 | opcode_index++) { |
| 275 | if (opmenu[opcode_index] == trans->opcode) |
| 276 | break; |
| 277 | } |
| 278 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 279 | if (opcode_index == ctlr->menubytes) { |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 280 | printf("ICH SPI: Opcode %x not found\n", |
| 281 | trans->opcode); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 282 | return -EINVAL; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 285 | optypes = ich_readw(ctlr, ctlr->optype); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 286 | optype = (optypes >> (opcode_index * 2)) & 0x3; |
| 287 | if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS && |
| 288 | optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS && |
| 289 | trans->bytesout >= 3) { |
| 290 | /* We guessed wrong earlier. Fix it up. */ |
| 291 | trans->type = optype; |
| 292 | } |
| 293 | if (optype != trans->type) { |
| 294 | printf("ICH SPI: Transaction doesn't fit type %d\n", |
| 295 | optype); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 296 | return -ENOSPC; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 297 | } |
| 298 | return opcode_index; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static int spi_setup_offset(struct spi_trans *trans) |
| 303 | { |
| 304 | /* Separate the SPI address and data. */ |
| 305 | switch (trans->type) { |
| 306 | case SPI_OPCODE_TYPE_READ_NO_ADDRESS: |
| 307 | case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS: |
| 308 | return 0; |
| 309 | case SPI_OPCODE_TYPE_READ_WITH_ADDRESS: |
| 310 | case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS: |
| 311 | trans->offset = ((uint32_t)trans->out[0] << 16) | |
| 312 | ((uint32_t)trans->out[1] << 8) | |
| 313 | ((uint32_t)trans->out[2] << 0); |
| 314 | spi_use_out(trans, 3); |
| 315 | return 1; |
| 316 | default: |
| 317 | printf("Unrecognized SPI transaction type %#x\n", trans->type); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 318 | return -EPROTO; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * 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] | 324 | * 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] | 325 | * those bits back, which would cause resetting them. |
| 326 | * |
| 327 | * Return the last read status value on success or -1 on failure. |
| 328 | */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 329 | static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask, |
| 330 | int wait_til_set) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 331 | { |
| 332 | int timeout = 600000; /* This will result in 6s */ |
| 333 | u16 status = 0; |
| 334 | |
| 335 | while (timeout--) { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 336 | status = ich_readw(ctlr, ctlr->status); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 337 | if (wait_til_set ^ ((status & bitmask) == 0)) { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 338 | if (wait_til_set) { |
| 339 | ich_writew(ctlr, status & bitmask, |
| 340 | ctlr->status); |
| 341 | } |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 342 | return status; |
| 343 | } |
| 344 | udelay(10); |
| 345 | } |
| 346 | |
| 347 | printf("ICH SPI: SCIP timeout, read %x, expected %x\n", |
| 348 | status, bitmask); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 349 | return -ETIMEDOUT; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 352 | static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 353 | const void *dout, void *din, unsigned long flags) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 354 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 355 | struct udevice *bus = dev_get_parent(dev); |
Simon Glass | 6634f81 | 2015-07-03 18:28:21 -0600 | [diff] [blame] | 356 | struct ich_spi_platdata *plat = dev_get_platdata(bus); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 357 | struct ich_spi_priv *ctlr = dev_get_priv(bus); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 358 | uint16_t control; |
| 359 | int16_t opcode_index; |
| 360 | int with_address; |
| 361 | int status; |
| 362 | int bytes = bitlen / 8; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 363 | struct spi_trans *trans = &ctlr->trans; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 364 | unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END); |
| 365 | int using_cmd = 0; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 366 | int ret; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 367 | |
Simon Glass | 7f66bc1 | 2015-06-07 08:50:33 -0600 | [diff] [blame] | 368 | /* We don't support writing partial bytes */ |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 369 | if (bitlen % 8) { |
| 370 | debug("ICH SPI: Accessing partial bytes not supported\n"); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 371 | return -EPROTONOSUPPORT; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* An empty end transaction can be ignored */ |
| 375 | if (type == SPI_XFER_END && !dout && !din) |
| 376 | return 0; |
| 377 | |
| 378 | if (type & SPI_XFER_BEGIN) |
| 379 | memset(trans, '\0', sizeof(*trans)); |
| 380 | |
| 381 | /* Dp we need to come back later to finish it? */ |
| 382 | if (dout && type == SPI_XFER_BEGIN) { |
| 383 | if (bytes > ICH_MAX_CMD_LEN) { |
| 384 | debug("ICH SPI: Command length limit exceeded\n"); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 385 | return -ENOSPC; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 386 | } |
| 387 | memcpy(trans->cmd, dout, bytes); |
| 388 | trans->cmd_len = bytes; |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 389 | debug_trace("ICH SPI: Saved %d bytes\n", bytes); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * We process a 'middle' spi_xfer() call, which has no |
| 395 | * SPI_XFER_BEGIN/END, as an independent transaction as if it had |
| 396 | * an end. We therefore repeat the command. This is because ICH |
| 397 | * seems to have no support for this, or because interest (in digging |
| 398 | * out the details and creating a special case in the code) is low. |
| 399 | */ |
| 400 | if (trans->cmd_len) { |
| 401 | trans->out = trans->cmd; |
| 402 | trans->bytesout = trans->cmd_len; |
| 403 | using_cmd = 1; |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 404 | debug_trace("ICH SPI: Using %d bytes\n", trans->cmd_len); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 405 | } else { |
| 406 | trans->out = dout; |
| 407 | trans->bytesout = dout ? bytes : 0; |
| 408 | } |
| 409 | |
| 410 | trans->in = din; |
| 411 | trans->bytesin = din ? bytes : 0; |
| 412 | |
| 413 | /* There has to always at least be an opcode. */ |
| 414 | if (!trans->bytesout) { |
| 415 | debug("ICH SPI: No opcode for transfer\n"); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 416 | return -EPROTO; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 419 | ret = ich_status_poll(ctlr, SPIS_SCIP, 0); |
| 420 | if (ret < 0) |
| 421 | return ret; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 422 | |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 423 | if (plat->ich_version == PCHV_7) |
Simon Glass | 6634f81 | 2015-07-03 18:28:21 -0600 | [diff] [blame] | 424 | ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status); |
| 425 | else |
| 426 | ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 427 | |
| 428 | spi_setup_type(trans, using_cmd ? bytes : 0); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 429 | opcode_index = spi_setup_opcode(ctlr, trans); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 430 | if (opcode_index < 0) |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 431 | return -EINVAL; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 432 | with_address = spi_setup_offset(trans); |
| 433 | if (with_address < 0) |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 434 | return -EINVAL; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 435 | |
| 436 | if (trans->opcode == SPI_OPCODE_WREN) { |
| 437 | /* |
| 438 | * Treat Write Enable as Atomic Pre-Op if possible |
| 439 | * in order to prevent the Management Engine from |
| 440 | * issuing a transaction between WREN and DATA. |
| 441 | */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 442 | if (!ctlr->ichspi_lock) |
| 443 | ich_writew(ctlr, trans->opcode, ctlr->preop); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 447 | if (ctlr->speed && ctlr->max_speed >= 33000000) { |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 448 | int byte; |
| 449 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 450 | byte = ich_readb(ctlr, ctlr->speed); |
| 451 | if (ctlr->cur_speed >= 33000000) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 452 | byte |= SSFC_SCF_33MHZ; |
| 453 | else |
| 454 | byte &= ~SSFC_SCF_33MHZ; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 455 | ich_writeb(ctlr, byte, ctlr->speed); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* See if we have used up the command data */ |
| 459 | if (using_cmd && dout && bytes) { |
| 460 | trans->out = dout; |
| 461 | trans->bytesout = bytes; |
Simon Glass | fcac1dd | 2016-01-18 20:19:20 -0700 | [diff] [blame] | 462 | debug_trace("ICH SPI: Moving to data, %d bytes\n", bytes); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Preset control fields */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 466 | control = ich_readw(ctlr, ctlr->control); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 467 | control &= ~SSFC_RESERVED; |
| 468 | control = SPIC_SCGO | ((opcode_index & 0x07) << 4); |
| 469 | |
| 470 | /* Issue atomic preop cycle if needed */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 471 | if (ich_readw(ctlr, ctlr->preop)) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 472 | control |= SPIC_ACS; |
| 473 | |
| 474 | if (!trans->bytesout && !trans->bytesin) { |
| 475 | /* SPI addresses are 24 bit only */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 476 | if (with_address) { |
| 477 | ich_writel(ctlr, trans->offset & 0x00FFFFFF, |
| 478 | ctlr->addr); |
| 479 | } |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 480 | /* |
| 481 | * This is a 'no data' command (like Write Enable), its |
| 482 | * bitesout size was 1, decremented to zero while executing |
| 483 | * spi_setup_opcode() above. Tell the chip to send the |
| 484 | * command. |
| 485 | */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 486 | ich_writew(ctlr, control, ctlr->control); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 487 | |
| 488 | /* wait for the result */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 489 | status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1); |
| 490 | if (status < 0) |
| 491 | return status; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 492 | |
| 493 | if (status & SPIS_FCERR) { |
| 494 | debug("ICH SPI: Command transaction error\n"); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 495 | return -EIO; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Check if this is a write command atempting to transfer more bytes |
| 503 | * than the controller can handle. Iterations for writes are not |
| 504 | * supported here because each SPI write command needs to be preceded |
| 505 | * and followed by other SPI commands, and this sequence is controlled |
| 506 | * by the SPI chip driver. |
| 507 | */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 508 | if (trans->bytesout > ctlr->databytes) { |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 509 | debug("ICH SPI: Too much to write. This should be prevented by the driver's max_write_size?\n"); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 510 | return -EPROTO; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Read or write up to databytes bytes at a time until everything has |
| 515 | * been sent. |
| 516 | */ |
| 517 | while (trans->bytesout || trans->bytesin) { |
| 518 | uint32_t data_length; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 519 | |
| 520 | /* SPI addresses are 24 bit only */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 521 | ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 522 | |
| 523 | if (trans->bytesout) |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 524 | data_length = min(trans->bytesout, ctlr->databytes); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 525 | else |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 526 | data_length = min(trans->bytesin, ctlr->databytes); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 527 | |
| 528 | /* Program data into FDATA0 to N */ |
| 529 | if (trans->bytesout) { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 530 | write_reg(ctlr, trans->out, ctlr->data, data_length); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 531 | spi_use_out(trans, data_length); |
| 532 | if (with_address) |
| 533 | trans->offset += data_length; |
| 534 | } |
| 535 | |
| 536 | /* Add proper control fields' values */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 537 | control &= ~((ctlr->databytes - 1) << 8); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 538 | control |= SPIC_DS; |
| 539 | control |= (data_length - 1) << 8; |
| 540 | |
| 541 | /* write it */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 542 | ich_writew(ctlr, control, ctlr->control); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 543 | |
| 544 | /* Wait for Cycle Done Status or Flash Cycle Error. */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 545 | status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1); |
| 546 | if (status < 0) |
| 547 | return status; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 548 | |
| 549 | if (status & SPIS_FCERR) { |
Simon Glass | 7f66bc1 | 2015-06-07 08:50:33 -0600 | [diff] [blame] | 550 | debug("ICH SPI: Data transaction error %x\n", status); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 551 | return -EIO; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | if (trans->bytesin) { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 555 | read_reg(ctlr, ctlr->data, trans->in, data_length); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 556 | spi_use_in(trans, data_length); |
| 557 | if (with_address) |
| 558 | trans->offset += data_length; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* Clear atomic preop now that xfer is done */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 563 | ich_writew(ctlr, 0, ctlr->preop); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 568 | /* |
| 569 | * This uses the SPI controller from the Intel Cougar Point and Panther Point |
| 570 | * PCH to write-protect portions of the SPI flash until reboot. The changes |
| 571 | * don't actually take effect until the HSFS[FLOCKDN] bit is set, but that's |
| 572 | * done elsewhere. |
| 573 | */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 574 | int spi_write_protect_region(struct udevice *dev, uint32_t lower_limit, |
| 575 | uint32_t length, int hint) |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 576 | { |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 577 | struct udevice *bus = dev->parent; |
| 578 | struct ich_spi_priv *ctlr = dev_get_priv(bus); |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 579 | uint32_t tmplong; |
| 580 | uint32_t upper_limit; |
| 581 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 582 | if (!ctlr->pr) { |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 583 | printf("%s: operation not supported on this chipset\n", |
| 584 | __func__); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 585 | return -ENOSYS; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | if (length == 0 || |
| 589 | lower_limit > (0xFFFFFFFFUL - length) + 1 || |
| 590 | hint < 0 || hint > 4) { |
| 591 | printf("%s(0x%x, 0x%x, %d): invalid args\n", __func__, |
| 592 | lower_limit, length, hint); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 593 | return -EPERM; |
Simon Glass | 4187740 | 2013-03-19 04:58:56 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | upper_limit = lower_limit + length - 1; |
| 597 | |
| 598 | /* |
| 599 | * Determine bits to write, as follows: |
| 600 | * 31 Write-protection enable (includes erase operation) |
| 601 | * 30:29 reserved |
| 602 | * 28:16 Upper Limit (FLA address bits 24:12, with 11:0 == 0xfff) |
| 603 | * 15 Read-protection enable |
| 604 | * 14:13 reserved |
| 605 | * 12:0 Lower Limit (FLA address bits 24:12, with 11:0 == 0x000) |
| 606 | */ |
| 607 | tmplong = 0x80000000 | |
| 608 | ((upper_limit & 0x01fff000) << 4) | |
| 609 | ((lower_limit & 0x01fff000) >> 12); |
| 610 | |
| 611 | printf("%s: writing 0x%08x to %p\n", __func__, tmplong, |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 612 | &ctlr->pr[hint]); |
| 613 | ctlr->pr[hint] = tmplong; |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 618 | static int ich_spi_probe(struct udevice *dev) |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 619 | { |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 620 | struct ich_spi_platdata *plat = dev_get_platdata(dev); |
| 621 | struct ich_spi_priv *priv = dev_get_priv(dev); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 622 | uint8_t bios_cntl; |
| 623 | int ret; |
| 624 | |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 625 | /* Check the ICH version */ |
| 626 | plat->ich_version = pch_get_version(dev->parent); |
| 627 | |
| 628 | ret = ich_init_controller(dev, plat, priv); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 629 | if (ret) |
| 630 | return ret; |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 631 | /* Disable the BIOS write protect so write commands are allowed */ |
| 632 | ret = pch_set_spi_protect(dev->parent, false); |
| 633 | if (ret == -ENOSYS) { |
Simon Glass | bf1623b | 2015-07-03 18:28:22 -0600 | [diff] [blame] | 634 | bios_cntl = ich_readb(priv, priv->bcr); |
Jagan Teki | 827afe5 | 2015-10-23 01:37:56 +0530 | [diff] [blame] | 635 | bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */ |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 636 | bios_cntl |= 1; /* Write Protect Disable (WPD) */ |
Simon Glass | bf1623b | 2015-07-03 18:28:22 -0600 | [diff] [blame] | 637 | ich_writeb(priv, bios_cntl, priv->bcr); |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 638 | } else if (ret) { |
| 639 | debug("%s: Failed to disable write-protect: err=%d\n", |
| 640 | __func__, ret); |
| 641 | return ret; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | priv->cur_speed = priv->max_speed; |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 649 | static int ich_spi_set_speed(struct udevice *bus, uint speed) |
| 650 | { |
| 651 | struct ich_spi_priv *priv = dev_get_priv(bus); |
| 652 | |
| 653 | priv->cur_speed = speed; |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static int ich_spi_set_mode(struct udevice *bus, uint mode) |
| 659 | { |
| 660 | debug("%s: mode=%d\n", __func__, mode); |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static int ich_spi_child_pre_probe(struct udevice *dev) |
| 666 | { |
| 667 | struct udevice *bus = dev_get_parent(dev); |
| 668 | struct ich_spi_platdata *plat = dev_get_platdata(bus); |
| 669 | struct ich_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 670 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 671 | |
| 672 | /* |
| 673 | * Yes this controller can only write a small number of bytes at |
| 674 | * once! The limit is typically 64 bytes. |
| 675 | */ |
| 676 | slave->max_write_size = priv->databytes; |
| 677 | /* |
| 678 | * ICH 7 SPI controller only supports array read command |
| 679 | * and byte program command for SST flash |
| 680 | */ |
Simon Glass | 3276163 | 2016-01-18 20:19:21 -0700 | [diff] [blame^] | 681 | if (plat->ich_version == PCHV_7) { |
Jagan Teki | 155c19f | 2015-12-16 15:24:24 +0530 | [diff] [blame] | 682 | slave->mode_rx = SPI_RX_SLOW; |
Jagan Teki | 71331b3 | 2015-12-13 20:12:45 +0530 | [diff] [blame] | 683 | slave->mode = SPI_TX_BYTE; |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static const struct dm_spi_ops ich_spi_ops = { |
| 690 | .xfer = ich_spi_xfer, |
| 691 | .set_speed = ich_spi_set_speed, |
| 692 | .set_mode = ich_spi_set_mode, |
| 693 | /* |
| 694 | * cs_info is not needed, since we require all chip selects to be |
| 695 | * in the device tree explicitly |
| 696 | */ |
| 697 | }; |
| 698 | |
| 699 | static const struct udevice_id ich_spi_ids[] = { |
| 700 | { .compatible = "intel,ich-spi" }, |
| 701 | { } |
| 702 | }; |
| 703 | |
| 704 | U_BOOT_DRIVER(ich_spi) = { |
| 705 | .name = "ich_spi", |
| 706 | .id = UCLASS_SPI, |
| 707 | .of_match = ich_spi_ids, |
| 708 | .ops = &ich_spi_ops, |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 709 | .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata), |
| 710 | .priv_auto_alloc_size = sizeof(struct ich_spi_priv), |
| 711 | .child_pre_probe = ich_spi_child_pre_probe, |
| 712 | .probe = ich_spi_probe, |
| 713 | }; |