blob: eeb4c274b8db3e72b4346d8d10c0adba494d40a7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass41877402013-03-19 04:58:56 +00002/*
3 * Copyright (c) 2011-12 The Chromium OS Authors.
4 *
Simon Glass41877402013-03-19 04:58:56 +00005 * This file is derived from the flashrom project.
6 */
Bin Meng316fd942016-02-01 01:40:36 -08007
Simon Glass41877402013-03-19 04:58:56 +00008#include <common.h>
Simon Glasse87e87b2019-12-06 21:42:40 -07009#include <div64.h>
Simon Glass35f15f62015-03-26 09:29:26 -060010#include <dm.h>
Simon Glassa08ca382015-01-27 22:13:43 -070011#include <errno.h>
Simon Glass41877402013-03-19 04:58:56 +000012#include <malloc.h>
Simon Glass32761632016-01-18 20:19:21 -070013#include <pch.h>
Simon Glass41877402013-03-19 04:58:56 +000014#include <pci.h>
15#include <pci_ids.h>
Simon Glass32761632016-01-18 20:19:21 -070016#include <spi.h>
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +020017#include <spi-mem.h>
Simon Glasse87e87b2019-12-06 21:42:40 -070018#include <asm/io.h>
Simon Glass41877402013-03-19 04:58:56 +000019
20#include "ich.h"
21
Bin Mengd9406672016-02-01 01:40:37 -080022DECLARE_GLOBAL_DATA_PTR;
23
Simon Glassfcac1dd2016-01-18 20:19:20 -070024#ifdef DEBUG_TRACE
25#define debug_trace(fmt, args...) debug(fmt, ##args)
26#else
27#define debug_trace(x, args...)
28#endif
29
Simon Glass35f15f62015-03-26 09:29:26 -060030static u8 ich_readb(struct ich_spi_priv *priv, int reg)
Simon Glass41877402013-03-19 04:58:56 +000031{
Simon Glass35f15f62015-03-26 09:29:26 -060032 u8 value = readb(priv->base + reg);
Simon Glass41877402013-03-19 04:58:56 +000033
Simon Glassfcac1dd2016-01-18 20:19:20 -070034 debug_trace("read %2.2x from %4.4x\n", value, reg);
Simon Glass41877402013-03-19 04:58:56 +000035
36 return value;
37}
38
Simon Glass35f15f62015-03-26 09:29:26 -060039static u16 ich_readw(struct ich_spi_priv *priv, int reg)
Simon Glass41877402013-03-19 04:58:56 +000040{
Simon Glass35f15f62015-03-26 09:29:26 -060041 u16 value = readw(priv->base + reg);
Simon Glass41877402013-03-19 04:58:56 +000042
Simon Glassfcac1dd2016-01-18 20:19:20 -070043 debug_trace("read %4.4x from %4.4x\n", value, reg);
Simon Glass41877402013-03-19 04:58:56 +000044
45 return value;
46}
47
Simon Glass35f15f62015-03-26 09:29:26 -060048static u32 ich_readl(struct ich_spi_priv *priv, int reg)
Simon Glass41877402013-03-19 04:58:56 +000049{
Simon Glass35f15f62015-03-26 09:29:26 -060050 u32 value = readl(priv->base + reg);
Simon Glass41877402013-03-19 04:58:56 +000051
Simon Glassfcac1dd2016-01-18 20:19:20 -070052 debug_trace("read %8.8x from %4.4x\n", value, reg);
Simon Glass41877402013-03-19 04:58:56 +000053
54 return value;
55}
56
Simon Glass35f15f62015-03-26 09:29:26 -060057static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
Simon Glass41877402013-03-19 04:58:56 +000058{
Simon Glass35f15f62015-03-26 09:29:26 -060059 writeb(value, priv->base + reg);
Simon Glassfcac1dd2016-01-18 20:19:20 -070060 debug_trace("wrote %2.2x to %4.4x\n", value, reg);
Simon Glass41877402013-03-19 04:58:56 +000061}
62
Simon Glass35f15f62015-03-26 09:29:26 -060063static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
Simon Glass41877402013-03-19 04:58:56 +000064{
Simon Glass35f15f62015-03-26 09:29:26 -060065 writew(value, priv->base + reg);
Simon Glassfcac1dd2016-01-18 20:19:20 -070066 debug_trace("wrote %4.4x to %4.4x\n", value, reg);
Simon Glass41877402013-03-19 04:58:56 +000067}
68
Simon Glass35f15f62015-03-26 09:29:26 -060069static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
Simon Glass41877402013-03-19 04:58:56 +000070{
Simon Glass35f15f62015-03-26 09:29:26 -060071 writel(value, priv->base + reg);
Simon Glassfcac1dd2016-01-18 20:19:20 -070072 debug_trace("wrote %8.8x to %4.4x\n", value, reg);
Simon Glass41877402013-03-19 04:58:56 +000073}
74
Simon Glass35f15f62015-03-26 09:29:26 -060075static void write_reg(struct ich_spi_priv *priv, const void *value,
76 int dest_reg, uint32_t size)
Simon Glass41877402013-03-19 04:58:56 +000077{
Simon Glass35f15f62015-03-26 09:29:26 -060078 memcpy_toio(priv->base + dest_reg, value, size);
Simon Glass41877402013-03-19 04:58:56 +000079}
80
Simon Glass35f15f62015-03-26 09:29:26 -060081static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
82 uint32_t size)
Simon Glass41877402013-03-19 04:58:56 +000083{
Simon Glass35f15f62015-03-26 09:29:26 -060084 memcpy_fromio(value, priv->base + src_reg, size);
Simon Glass41877402013-03-19 04:58:56 +000085}
86
Simon Glass35f15f62015-03-26 09:29:26 -060087static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
Simon Glass41877402013-03-19 04:58:56 +000088{
89 const uint32_t bbar_mask = 0x00ffff00;
90 uint32_t ichspi_bbar;
91
92 minaddr &= bbar_mask;
Simon Glass35f15f62015-03-26 09:29:26 -060093 ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
Simon Glass41877402013-03-19 04:58:56 +000094 ichspi_bbar |= minaddr;
Simon Glass35f15f62015-03-26 09:29:26 -060095 ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
Simon Glass41877402013-03-19 04:58:56 +000096}
97
Simon Glass41877402013-03-19 04:58:56 +000098/* @return 1 if the SPI flash supports the 33MHz speed */
Simon Glass32761632016-01-18 20:19:21 -070099static int ich9_can_do_33mhz(struct udevice *dev)
Simon Glass41877402013-03-19 04:58:56 +0000100{
Simon Glass78d520c2019-12-06 21:42:38 -0700101 struct ich_spi_priv *priv = dev_get_priv(dev);
Simon Glass41877402013-03-19 04:58:56 +0000102 u32 fdod, speed;
103
104 /* Observe SPI Descriptor Component Section 0 */
Simon Glass78d520c2019-12-06 21:42:38 -0700105 dm_pci_write_config32(priv->pch, 0xb0, 0x1000);
Simon Glass41877402013-03-19 04:58:56 +0000106
107 /* Extract the Write/Erase SPI Frequency from descriptor */
Simon Glass78d520c2019-12-06 21:42:38 -0700108 dm_pci_read_config32(priv->pch, 0xb4, &fdod);
Simon Glass41877402013-03-19 04:58:56 +0000109
110 /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
111 speed = (fdod >> 21) & 7;
112
113 return speed == 1;
114}
115
Bin Meng59de5032017-10-18 18:20:57 -0700116static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
117{
118 if (plat->ich_version == ICHV_7) {
119 struct ich7_spi_regs *ich7_spi = sbase;
120
121 setbits_le16(&ich7_spi->spis, SPIS_LOCK);
122 } else if (plat->ich_version == ICHV_9) {
123 struct ich9_spi_regs *ich9_spi = sbase;
124
125 setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
126 }
127}
128
Bin Meng36ce0242017-08-15 22:38:29 -0700129static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
130{
131 int lock = 0;
132
133 if (plat->ich_version == ICHV_7) {
134 struct ich7_spi_regs *ich7_spi = sbase;
135
136 lock = readw(&ich7_spi->spis) & SPIS_LOCK;
137 } else if (plat->ich_version == ICHV_9) {
138 struct ich9_spi_regs *ich9_spi = sbase;
139
140 lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
141 }
142
143 return lock != 0;
144}
145
Bin Meng36ce0242017-08-15 22:38:29 -0700146static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
147 bool lock)
Simon Glass41877402013-03-19 04:58:56 +0000148{
149 uint16_t optypes;
Simon Glass35f15f62015-03-26 09:29:26 -0600150 uint8_t opmenu[ctlr->menubytes];
Simon Glass41877402013-03-19 04:58:56 +0000151
Bin Meng36ce0242017-08-15 22:38:29 -0700152 if (!lock) {
Simon Glass41877402013-03-19 04:58:56 +0000153 /* The lock is off, so just use index 0. */
Simon Glass35f15f62015-03-26 09:29:26 -0600154 ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
155 optypes = ich_readw(ctlr, ctlr->optype);
Simon Glass41877402013-03-19 04:58:56 +0000156 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Simon Glass35f15f62015-03-26 09:29:26 -0600157 ich_writew(ctlr, optypes, ctlr->optype);
Simon Glass41877402013-03-19 04:58:56 +0000158 return 0;
159 } else {
160 /* The lock is on. See if what we need is on the menu. */
161 uint8_t optype;
162 uint16_t opcode_index;
163
164 /* Write Enable is handled as atomic prefix */
165 if (trans->opcode == SPI_OPCODE_WREN)
166 return 0;
167
Simon Glass35f15f62015-03-26 09:29:26 -0600168 read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
169 for (opcode_index = 0; opcode_index < ctlr->menubytes;
Simon Glass41877402013-03-19 04:58:56 +0000170 opcode_index++) {
171 if (opmenu[opcode_index] == trans->opcode)
172 break;
173 }
174
Simon Glass35f15f62015-03-26 09:29:26 -0600175 if (opcode_index == ctlr->menubytes) {
Simon Glass41877402013-03-19 04:58:56 +0000176 printf("ICH SPI: Opcode %x not found\n",
177 trans->opcode);
Simon Glass35f15f62015-03-26 09:29:26 -0600178 return -EINVAL;
Simon Glass41877402013-03-19 04:58:56 +0000179 }
180
Simon Glass35f15f62015-03-26 09:29:26 -0600181 optypes = ich_readw(ctlr, ctlr->optype);
Simon Glass41877402013-03-19 04:58:56 +0000182 optype = (optypes >> (opcode_index * 2)) & 0x3;
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200183
Simon Glass41877402013-03-19 04:58:56 +0000184 if (optype != trans->type) {
185 printf("ICH SPI: Transaction doesn't fit type %d\n",
186 optype);
Simon Glass35f15f62015-03-26 09:29:26 -0600187 return -ENOSPC;
Simon Glass41877402013-03-19 04:58:56 +0000188 }
189 return opcode_index;
190 }
191}
192
Simon Glass41877402013-03-19 04:58:56 +0000193/*
194 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
York Sun4a598092013-04-01 11:29:11 -0700195 * below is true) or 0. In case the wait was for the bit(s) to set - write
Simon Glass41877402013-03-19 04:58:56 +0000196 * those bits back, which would cause resetting them.
197 *
198 * Return the last read status value on success or -1 on failure.
199 */
Simon Glass35f15f62015-03-26 09:29:26 -0600200static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
201 int wait_til_set)
Simon Glass41877402013-03-19 04:58:56 +0000202{
203 int timeout = 600000; /* This will result in 6s */
204 u16 status = 0;
205
206 while (timeout--) {
Simon Glass35f15f62015-03-26 09:29:26 -0600207 status = ich_readw(ctlr, ctlr->status);
Simon Glass41877402013-03-19 04:58:56 +0000208 if (wait_til_set ^ ((status & bitmask) == 0)) {
Simon Glass35f15f62015-03-26 09:29:26 -0600209 if (wait_til_set) {
210 ich_writew(ctlr, status & bitmask,
211 ctlr->status);
212 }
Simon Glass41877402013-03-19 04:58:56 +0000213 return status;
214 }
215 udelay(10);
216 }
217
218 printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
219 status, bitmask);
Simon Glass35f15f62015-03-26 09:29:26 -0600220 return -ETIMEDOUT;
Simon Glass41877402013-03-19 04:58:56 +0000221}
222
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200223static void ich_spi_config_opcode(struct udevice *dev)
Bin Meng552720e2017-08-15 22:38:30 -0700224{
225 struct ich_spi_priv *ctlr = dev_get_priv(dev);
226
227 /*
228 * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
229 * to prevent accidental or intentional writes. Before they get
230 * locked down, these registers should be initialized properly.
231 */
232 ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
233 ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
234 ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
235 ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
236}
237
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200238static int ich_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
Simon Glass41877402013-03-19 04:58:56 +0000239{
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200240 struct udevice *bus = dev_get_parent(slave->dev);
Simon Glass6634f812015-07-03 18:28:21 -0600241 struct ich_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass35f15f62015-03-26 09:29:26 -0600242 struct ich_spi_priv *ctlr = dev_get_priv(bus);
Simon Glass41877402013-03-19 04:58:56 +0000243 uint16_t control;
244 int16_t opcode_index;
245 int with_address;
246 int status;
Simon Glass35f15f62015-03-26 09:29:26 -0600247 struct spi_trans *trans = &ctlr->trans;
Bin Meng36ce0242017-08-15 22:38:29 -0700248 bool lock = spi_lock_status(plat, ctlr->base);
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200249 int ret = 0;
Simon Glass41877402013-03-19 04:58:56 +0000250
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200251 trans->in = NULL;
252 trans->out = NULL;
253 trans->type = 0xFF;
Simon Glass41877402013-03-19 04:58:56 +0000254
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200255 if (op->data.nbytes) {
256 if (op->data.dir == SPI_MEM_DATA_IN) {
257 trans->in = op->data.buf.in;
258 trans->bytesin = op->data.nbytes;
259 } else {
260 trans->out = op->data.buf.out;
261 trans->bytesout = op->data.nbytes;
Simon Glass41877402013-03-19 04:58:56 +0000262 }
Simon Glass41877402013-03-19 04:58:56 +0000263 }
264
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200265 if (trans->opcode != op->cmd.opcode)
266 trans->opcode = op->cmd.opcode;
Simon Glass41877402013-03-19 04:58:56 +0000267
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200268 if (lock && trans->opcode == SPI_OPCODE_WRDIS)
269 return 0;
Simon Glass41877402013-03-19 04:58:56 +0000270
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200271 if (trans->opcode == SPI_OPCODE_WREN) {
272 /*
273 * Treat Write Enable as Atomic Pre-Op if possible
274 * in order to prevent the Management Engine from
275 * issuing a transaction between WREN and DATA.
276 */
277 if (!lock)
278 ich_writew(ctlr, trans->opcode, ctlr->preop);
279 return 0;
Simon Glass41877402013-03-19 04:58:56 +0000280 }
281
Simon Glass35f15f62015-03-26 09:29:26 -0600282 ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
283 if (ret < 0)
284 return ret;
Simon Glass41877402013-03-19 04:58:56 +0000285
Bin Meng0d3792c2016-02-01 01:40:38 -0800286 if (plat->ich_version == ICHV_7)
Simon Glass6634f812015-07-03 18:28:21 -0600287 ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
288 else
289 ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
Simon Glass41877402013-03-19 04:58:56 +0000290
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200291 /* Try to guess spi transaction type */
292 if (op->data.dir == SPI_MEM_DATA_OUT) {
293 if (op->addr.nbytes)
294 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
295 else
296 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
297 } else {
298 if (op->addr.nbytes)
299 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
300 else
301 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
302 }
303 /* Special erase case handling */
304 if (op->addr.nbytes && !op->data.buswidth)
305 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
306
Bin Meng36ce0242017-08-15 22:38:29 -0700307 opcode_index = spi_setup_opcode(ctlr, trans, lock);
Simon Glass41877402013-03-19 04:58:56 +0000308 if (opcode_index < 0)
Simon Glass35f15f62015-03-26 09:29:26 -0600309 return -EINVAL;
Simon Glass41877402013-03-19 04:58:56 +0000310
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200311 if (op->addr.nbytes) {
312 trans->offset = op->addr.val;
313 with_address = 1;
Simon Glass41877402013-03-19 04:58:56 +0000314 }
315
Simon Glass35f15f62015-03-26 09:29:26 -0600316 if (ctlr->speed && ctlr->max_speed >= 33000000) {
Simon Glass41877402013-03-19 04:58:56 +0000317 int byte;
318
Simon Glass35f15f62015-03-26 09:29:26 -0600319 byte = ich_readb(ctlr, ctlr->speed);
320 if (ctlr->cur_speed >= 33000000)
Simon Glass41877402013-03-19 04:58:56 +0000321 byte |= SSFC_SCF_33MHZ;
322 else
323 byte &= ~SSFC_SCF_33MHZ;
Simon Glass35f15f62015-03-26 09:29:26 -0600324 ich_writeb(ctlr, byte, ctlr->speed);
Simon Glass41877402013-03-19 04:58:56 +0000325 }
326
Simon Glass41877402013-03-19 04:58:56 +0000327 /* Preset control fields */
Simon Glass41877402013-03-19 04:58:56 +0000328 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
329
330 /* Issue atomic preop cycle if needed */
Simon Glass35f15f62015-03-26 09:29:26 -0600331 if (ich_readw(ctlr, ctlr->preop))
Simon Glass41877402013-03-19 04:58:56 +0000332 control |= SPIC_ACS;
333
334 if (!trans->bytesout && !trans->bytesin) {
335 /* SPI addresses are 24 bit only */
Simon Glass35f15f62015-03-26 09:29:26 -0600336 if (with_address) {
337 ich_writel(ctlr, trans->offset & 0x00FFFFFF,
338 ctlr->addr);
339 }
Simon Glass41877402013-03-19 04:58:56 +0000340 /*
341 * This is a 'no data' command (like Write Enable), its
342 * bitesout size was 1, decremented to zero while executing
343 * spi_setup_opcode() above. Tell the chip to send the
344 * command.
345 */
Simon Glass35f15f62015-03-26 09:29:26 -0600346 ich_writew(ctlr, control, ctlr->control);
Simon Glass41877402013-03-19 04:58:56 +0000347
348 /* wait for the result */
Simon Glass35f15f62015-03-26 09:29:26 -0600349 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
350 if (status < 0)
351 return status;
Simon Glass41877402013-03-19 04:58:56 +0000352
353 if (status & SPIS_FCERR) {
354 debug("ICH SPI: Command transaction error\n");
Simon Glass35f15f62015-03-26 09:29:26 -0600355 return -EIO;
Simon Glass41877402013-03-19 04:58:56 +0000356 }
357
358 return 0;
359 }
360
Simon Glass41877402013-03-19 04:58:56 +0000361 while (trans->bytesout || trans->bytesin) {
362 uint32_t data_length;
Simon Glass41877402013-03-19 04:58:56 +0000363
364 /* SPI addresses are 24 bit only */
Simon Glass35f15f62015-03-26 09:29:26 -0600365 ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
Simon Glass41877402013-03-19 04:58:56 +0000366
367 if (trans->bytesout)
Simon Glass35f15f62015-03-26 09:29:26 -0600368 data_length = min(trans->bytesout, ctlr->databytes);
Simon Glass41877402013-03-19 04:58:56 +0000369 else
Simon Glass35f15f62015-03-26 09:29:26 -0600370 data_length = min(trans->bytesin, ctlr->databytes);
Simon Glass41877402013-03-19 04:58:56 +0000371
372 /* Program data into FDATA0 to N */
373 if (trans->bytesout) {
Simon Glass35f15f62015-03-26 09:29:26 -0600374 write_reg(ctlr, trans->out, ctlr->data, data_length);
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200375 trans->bytesout -= data_length;
Simon Glass41877402013-03-19 04:58:56 +0000376 }
377
378 /* Add proper control fields' values */
Simon Glass35f15f62015-03-26 09:29:26 -0600379 control &= ~((ctlr->databytes - 1) << 8);
Simon Glass41877402013-03-19 04:58:56 +0000380 control |= SPIC_DS;
381 control |= (data_length - 1) << 8;
382
383 /* write it */
Simon Glass35f15f62015-03-26 09:29:26 -0600384 ich_writew(ctlr, control, ctlr->control);
Simon Glass41877402013-03-19 04:58:56 +0000385
Bin Meng316fd942016-02-01 01:40:36 -0800386 /* Wait for Cycle Done Status or Flash Cycle Error */
Simon Glass35f15f62015-03-26 09:29:26 -0600387 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
388 if (status < 0)
389 return status;
Simon Glass41877402013-03-19 04:58:56 +0000390
391 if (status & SPIS_FCERR) {
Simon Glass7f66bc12015-06-07 08:50:33 -0600392 debug("ICH SPI: Data transaction error %x\n", status);
Simon Glass35f15f62015-03-26 09:29:26 -0600393 return -EIO;
Simon Glass41877402013-03-19 04:58:56 +0000394 }
395
396 if (trans->bytesin) {
Simon Glass35f15f62015-03-26 09:29:26 -0600397 read_reg(ctlr, ctlr->data, trans->in, data_length);
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200398 trans->bytesin -= data_length;
Simon Glass41877402013-03-19 04:58:56 +0000399 }
400 }
401
402 /* Clear atomic preop now that xfer is done */
Bin Meng4a75e9b2017-08-26 19:22:59 -0700403 if (!lock)
404 ich_writew(ctlr, 0, ctlr->preop);
Simon Glass41877402013-03-19 04:58:56 +0000405
406 return 0;
407}
408
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200409static int ich_spi_adjust_size(struct spi_slave *slave, struct spi_mem_op *op)
410{
411 unsigned int page_offset;
412 int addr = op->addr.val;
413 unsigned int byte_count = op->data.nbytes;
414
415 if (hweight32(ICH_BOUNDARY) == 1) {
416 page_offset = addr & (ICH_BOUNDARY - 1);
417 } else {
418 u64 aux = addr;
419
420 page_offset = do_div(aux, ICH_BOUNDARY);
421 }
422
423 if (op->data.dir == SPI_MEM_DATA_IN && slave->max_read_size) {
424 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
425 slave->max_read_size);
426 } else if (slave->max_write_size) {
427 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
428 slave->max_write_size);
429 }
430
431 op->data.nbytes = min(op->data.nbytes, byte_count);
432
433 return 0;
434}
435
Simon Glass78d520c2019-12-06 21:42:38 -0700436static int ich_protect_lockdown(struct udevice *dev)
437{
438 struct ich_spi_platdata *plat = dev_get_platdata(dev);
439 struct ich_spi_priv *priv = dev_get_priv(dev);
440 int ret = -ENOSYS;
441
442 /* Disable the BIOS write protect so write commands are allowed */
443 if (priv->pch)
444 ret = pch_set_spi_protect(priv->pch, false);
445 if (ret == -ENOSYS) {
446 u8 bios_cntl;
447
448 bios_cntl = ich_readb(priv, priv->bcr);
449 bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
450 bios_cntl |= 1; /* Write Protect Disable (WPD) */
451 ich_writeb(priv, bios_cntl, priv->bcr);
452 } else if (ret) {
453 debug("%s: Failed to disable write-protect: err=%d\n",
454 __func__, ret);
455 return ret;
456 }
457
458 /* Lock down SPI controller settings if required */
459 if (plat->lockdown) {
460 ich_spi_config_opcode(dev);
461 spi_lock_down(plat, priv->base);
462 }
463
464 return 0;
465}
466
Simon Glass23485eb2019-12-06 21:42:37 -0700467static int ich_init_controller(struct udevice *dev,
468 struct ich_spi_platdata *plat,
469 struct ich_spi_priv *ctlr)
470{
471 ulong sbase_addr;
472 void *sbase;
473
474 /* SBASE is similar */
475 pch_get_spi_base(dev->parent, &sbase_addr);
476 sbase = (void *)sbase_addr;
477 debug("%s: sbase=%p\n", __func__, sbase);
478
479 if (plat->ich_version == ICHV_7) {
480 struct ich7_spi_regs *ich7_spi = sbase;
481
482 ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
483 ctlr->menubytes = sizeof(ich7_spi->opmenu);
484 ctlr->optype = offsetof(struct ich7_spi_regs, optype);
485 ctlr->addr = offsetof(struct ich7_spi_regs, spia);
486 ctlr->data = offsetof(struct ich7_spi_regs, spid);
487 ctlr->databytes = sizeof(ich7_spi->spid);
488 ctlr->status = offsetof(struct ich7_spi_regs, spis);
489 ctlr->control = offsetof(struct ich7_spi_regs, spic);
490 ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
491 ctlr->preop = offsetof(struct ich7_spi_regs, preop);
492 ctlr->base = ich7_spi;
493 } else if (plat->ich_version == ICHV_9) {
494 struct ich9_spi_regs *ich9_spi = sbase;
495
496 ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
497 ctlr->menubytes = sizeof(ich9_spi->opmenu);
498 ctlr->optype = offsetof(struct ich9_spi_regs, optype);
499 ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
500 ctlr->data = offsetof(struct ich9_spi_regs, fdata);
501 ctlr->databytes = sizeof(ich9_spi->fdata);
502 ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
503 ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
504 ctlr->speed = ctlr->control + 2;
505 ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
506 ctlr->preop = offsetof(struct ich9_spi_regs, preop);
507 ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
508 ctlr->pr = &ich9_spi->pr[0];
509 ctlr->base = ich9_spi;
510 } else {
511 debug("ICH SPI: Unrecognised ICH version %d\n",
512 plat->ich_version);
513 return -EINVAL;
514 }
515
516 /* Work out the maximum speed we can support */
517 ctlr->max_speed = 20000000;
518 if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
519 ctlr->max_speed = 33000000;
520 debug("ICH SPI: Version ID %d detected at %p, speed %ld\n",
521 plat->ich_version, ctlr->base, ctlr->max_speed);
522
523 ich_set_bbar(ctlr, 0);
524
525 return 0;
526}
527
Simon Glass32761632016-01-18 20:19:21 -0700528static int ich_spi_probe(struct udevice *dev)
Simon Glass35f15f62015-03-26 09:29:26 -0600529{
Simon Glass32761632016-01-18 20:19:21 -0700530 struct ich_spi_platdata *plat = dev_get_platdata(dev);
531 struct ich_spi_priv *priv = dev_get_priv(dev);
Simon Glass35f15f62015-03-26 09:29:26 -0600532 int ret;
533
Simon Glass32761632016-01-18 20:19:21 -0700534 ret = ich_init_controller(dev, plat, priv);
Simon Glass35f15f62015-03-26 09:29:26 -0600535 if (ret)
536 return ret;
Simon Glass35f15f62015-03-26 09:29:26 -0600537
Simon Glass78d520c2019-12-06 21:42:38 -0700538 ret = ich_protect_lockdown(dev);
539 if (ret)
540 return ret;
Bin Meng59de5032017-10-18 18:20:57 -0700541
Simon Glass35f15f62015-03-26 09:29:26 -0600542 priv->cur_speed = priv->max_speed;
543
544 return 0;
545}
546
Stefan Roeseb6647ab2017-04-24 09:48:04 +0200547static int ich_spi_remove(struct udevice *bus)
548{
Stefan Roeseb6647ab2017-04-24 09:48:04 +0200549 /*
550 * Configure SPI controller so that the Linux MTD driver can fully
551 * access the SPI NOR chip
552 */
Bin Meng552720e2017-08-15 22:38:30 -0700553 ich_spi_config_opcode(bus);
Stefan Roeseb6647ab2017-04-24 09:48:04 +0200554
555 return 0;
556}
557
Simon Glass35f15f62015-03-26 09:29:26 -0600558static int ich_spi_set_speed(struct udevice *bus, uint speed)
559{
560 struct ich_spi_priv *priv = dev_get_priv(bus);
561
562 priv->cur_speed = speed;
563
564 return 0;
565}
566
567static int ich_spi_set_mode(struct udevice *bus, uint mode)
568{
569 debug("%s: mode=%d\n", __func__, mode);
570
571 return 0;
572}
573
574static int ich_spi_child_pre_probe(struct udevice *dev)
575{
576 struct udevice *bus = dev_get_parent(dev);
577 struct ich_spi_platdata *plat = dev_get_platdata(bus);
578 struct ich_spi_priv *priv = dev_get_priv(bus);
Simon Glassde44acf2015-09-28 23:32:01 -0600579 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass35f15f62015-03-26 09:29:26 -0600580
581 /*
582 * Yes this controller can only write a small number of bytes at
583 * once! The limit is typically 64 bytes.
584 */
585 slave->max_write_size = priv->databytes;
586 /*
587 * ICH 7 SPI controller only supports array read command
588 * and byte program command for SST flash
589 */
Jagan Teki96536b12016-08-08 17:12:12 +0530590 if (plat->ich_version == ICHV_7)
591 slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
Simon Glass35f15f62015-03-26 09:29:26 -0600592
593 return 0;
594}
595
Bin Mengd9406672016-02-01 01:40:37 -0800596static int ich_spi_ofdata_to_platdata(struct udevice *dev)
597{
598 struct ich_spi_platdata *plat = dev_get_platdata(dev);
Simon Glass78d520c2019-12-06 21:42:38 -0700599 struct ich_spi_priv *priv = dev_get_priv(dev);
Bin Mengd9406672016-02-01 01:40:37 -0800600
Simon Glass78d520c2019-12-06 21:42:38 -0700601 /* Find a PCH if there is one */
602 uclass_first_device(UCLASS_PCH, &priv->pch);
603 if (!priv->pch)
604 priv->pch = dev_get_parent(dev);
605
Simon Glass6e37af32019-12-06 21:42:39 -0700606 plat->ich_version = dev_get_driver_data(dev);
607 plat->lockdown = dev_read_bool(dev, "intel,spi-lock-down");
Bin Mengd9406672016-02-01 01:40:37 -0800608
Simon Glass6e37af32019-12-06 21:42:39 -0700609 return 0;
Bin Mengd9406672016-02-01 01:40:37 -0800610}
611
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200612static const struct spi_controller_mem_ops ich_controller_mem_ops = {
613 .adjust_op_size = ich_spi_adjust_size,
614 .supports_op = NULL,
615 .exec_op = ich_spi_exec_op,
616};
617
Simon Glass35f15f62015-03-26 09:29:26 -0600618static const struct dm_spi_ops ich_spi_ops = {
Simon Glass2d2e8602019-12-06 21:42:35 -0700619 /* xfer is not supported */
Simon Glass35f15f62015-03-26 09:29:26 -0600620 .set_speed = ich_spi_set_speed,
621 .set_mode = ich_spi_set_mode,
Bernhard Messerklingerdb3ffe92019-08-02 08:38:34 +0200622 .mem_ops = &ich_controller_mem_ops,
Simon Glass35f15f62015-03-26 09:29:26 -0600623 /*
624 * cs_info is not needed, since we require all chip selects to be
625 * in the device tree explicitly
626 */
627};
628
629static const struct udevice_id ich_spi_ids[] = {
Simon Glass6e37af32019-12-06 21:42:39 -0700630 { .compatible = "intel,ich7-spi", ICHV_7 },
631 { .compatible = "intel,ich9-spi", ICHV_9 },
Simon Glass35f15f62015-03-26 09:29:26 -0600632 { }
633};
634
635U_BOOT_DRIVER(ich_spi) = {
636 .name = "ich_spi",
637 .id = UCLASS_SPI,
638 .of_match = ich_spi_ids,
639 .ops = &ich_spi_ops,
Bin Mengd9406672016-02-01 01:40:37 -0800640 .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
Simon Glass35f15f62015-03-26 09:29:26 -0600641 .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
642 .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
643 .child_pre_probe = ich_spi_child_pre_probe,
644 .probe = ich_spi_probe,
Stefan Roeseb6647ab2017-04-24 09:48:04 +0200645 .remove = ich_spi_remove,
646 .flags = DM_FLAG_OS_PREPARE,
Simon Glass35f15f62015-03-26 09:29:26 -0600647};