blob: 22fc83dd725d2154890f048234dc83994f4c1c99 [file] [log] [blame]
Simon Glass18530302013-03-19 04:58:56 +00001/*
2 * Copyright (c) 2011-12 The Chromium OS Authors.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glass18530302013-03-19 04:58:56 +00005 *
6 * This file is derived from the flashrom project.
7 */
Bin Meng9eb43392016-02-01 01:40:36 -08008
Simon Glass18530302013-03-19 04:58:56 +00009#include <common.h>
Simon Glassba457562015-03-26 09:29:26 -060010#include <dm.h>
Simon Glass5093bad2015-01-27 22:13:43 -070011#include <errno.h>
Simon Glass18530302013-03-19 04:58:56 +000012#include <malloc.h>
Simon Glassf2b85ab2016-01-18 20:19:21 -070013#include <pch.h>
Simon Glass18530302013-03-19 04:58:56 +000014#include <pci.h>
15#include <pci_ids.h>
Simon Glassf2b85ab2016-01-18 20:19:21 -070016#include <spi.h>
Simon Glass18530302013-03-19 04:58:56 +000017#include <asm/io.h>
18
19#include "ich.h"
20
Bin Meng1f9eb592016-02-01 01:40:37 -080021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glassfffe25d2016-01-18 20:19:20 -070023#ifdef DEBUG_TRACE
24#define debug_trace(fmt, args...) debug(fmt, ##args)
25#else
26#define debug_trace(x, args...)
27#endif
28
Simon Glassba457562015-03-26 09:29:26 -060029static u8 ich_readb(struct ich_spi_priv *priv, int reg)
Simon Glass18530302013-03-19 04:58:56 +000030{
Simon Glassba457562015-03-26 09:29:26 -060031 u8 value = readb(priv->base + reg);
Simon Glass18530302013-03-19 04:58:56 +000032
Simon Glassfffe25d2016-01-18 20:19:20 -070033 debug_trace("read %2.2x from %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000034
35 return value;
36}
37
Simon Glassba457562015-03-26 09:29:26 -060038static u16 ich_readw(struct ich_spi_priv *priv, int reg)
Simon Glass18530302013-03-19 04:58:56 +000039{
Simon Glassba457562015-03-26 09:29:26 -060040 u16 value = readw(priv->base + reg);
Simon Glass18530302013-03-19 04:58:56 +000041
Simon Glassfffe25d2016-01-18 20:19:20 -070042 debug_trace("read %4.4x from %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000043
44 return value;
45}
46
Simon Glassba457562015-03-26 09:29:26 -060047static u32 ich_readl(struct ich_spi_priv *priv, int reg)
Simon Glass18530302013-03-19 04:58:56 +000048{
Simon Glassba457562015-03-26 09:29:26 -060049 u32 value = readl(priv->base + reg);
Simon Glass18530302013-03-19 04:58:56 +000050
Simon Glassfffe25d2016-01-18 20:19:20 -070051 debug_trace("read %8.8x from %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000052
53 return value;
54}
55
Simon Glassba457562015-03-26 09:29:26 -060056static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
Simon Glass18530302013-03-19 04:58:56 +000057{
Simon Glassba457562015-03-26 09:29:26 -060058 writeb(value, priv->base + reg);
Simon Glassfffe25d2016-01-18 20:19:20 -070059 debug_trace("wrote %2.2x to %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000060}
61
Simon Glassba457562015-03-26 09:29:26 -060062static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
Simon Glass18530302013-03-19 04:58:56 +000063{
Simon Glassba457562015-03-26 09:29:26 -060064 writew(value, priv->base + reg);
Simon Glassfffe25d2016-01-18 20:19:20 -070065 debug_trace("wrote %4.4x to %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000066}
67
Simon Glassba457562015-03-26 09:29:26 -060068static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
Simon Glass18530302013-03-19 04:58:56 +000069{
Simon Glassba457562015-03-26 09:29:26 -060070 writel(value, priv->base + reg);
Simon Glassfffe25d2016-01-18 20:19:20 -070071 debug_trace("wrote %8.8x to %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000072}
73
Simon Glassba457562015-03-26 09:29:26 -060074static void write_reg(struct ich_spi_priv *priv, const void *value,
75 int dest_reg, uint32_t size)
Simon Glass18530302013-03-19 04:58:56 +000076{
Simon Glassba457562015-03-26 09:29:26 -060077 memcpy_toio(priv->base + dest_reg, value, size);
Simon Glass18530302013-03-19 04:58:56 +000078}
79
Simon Glassba457562015-03-26 09:29:26 -060080static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
81 uint32_t size)
Simon Glass18530302013-03-19 04:58:56 +000082{
Simon Glassba457562015-03-26 09:29:26 -060083 memcpy_fromio(value, priv->base + src_reg, size);
Simon Glass18530302013-03-19 04:58:56 +000084}
85
Simon Glassba457562015-03-26 09:29:26 -060086static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
Simon Glass18530302013-03-19 04:58:56 +000087{
88 const uint32_t bbar_mask = 0x00ffff00;
89 uint32_t ichspi_bbar;
90
91 minaddr &= bbar_mask;
Simon Glassba457562015-03-26 09:29:26 -060092 ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
Simon Glass18530302013-03-19 04:58:56 +000093 ichspi_bbar |= minaddr;
Simon Glassba457562015-03-26 09:29:26 -060094 ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
Simon Glass18530302013-03-19 04:58:56 +000095}
96
Simon Glass18530302013-03-19 04:58:56 +000097/* @return 1 if the SPI flash supports the 33MHz speed */
Simon Glassf2b85ab2016-01-18 20:19:21 -070098static int ich9_can_do_33mhz(struct udevice *dev)
Simon Glass18530302013-03-19 04:58:56 +000099{
100 u32 fdod, speed;
101
102 /* Observe SPI Descriptor Component Section 0 */
Simon Glassf2b85ab2016-01-18 20:19:21 -0700103 dm_pci_write_config32(dev->parent, 0xb0, 0x1000);
Simon Glass18530302013-03-19 04:58:56 +0000104
105 /* Extract the Write/Erase SPI Frequency from descriptor */
Simon Glassf2b85ab2016-01-18 20:19:21 -0700106 dm_pci_read_config32(dev->parent, 0xb4, &fdod);
Simon Glass18530302013-03-19 04:58:56 +0000107
108 /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
109 speed = (fdod >> 21) & 7;
110
111 return speed == 1;
112}
113
Simon Glassf2b85ab2016-01-18 20:19:21 -0700114static int ich_init_controller(struct udevice *dev,
115 struct ich_spi_platdata *plat,
Simon Glassba457562015-03-26 09:29:26 -0600116 struct ich_spi_priv *ctlr)
Simon Glass18530302013-03-19 04:58:56 +0000117{
Simon Glassf2b85ab2016-01-18 20:19:21 -0700118 ulong sbase_addr;
119 void *sbase;
Simon Glass5093bad2015-01-27 22:13:43 -0700120
121 /* SBASE is similar */
Bin Meng3e389d82016-02-01 01:40:42 -0800122 pch_get_spi_base(dev->parent, &sbase_addr);
Simon Glassf2b85ab2016-01-18 20:19:21 -0700123 sbase = (void *)sbase_addr;
124 debug("%s: sbase=%p\n", __func__, sbase);
Simon Glass5093bad2015-01-27 22:13:43 -0700125
Bin Meng6e670b52016-02-01 01:40:38 -0800126 if (plat->ich_version == ICHV_7) {
Simon Glassf2b85ab2016-01-18 20:19:21 -0700127 struct ich7_spi_regs *ich7_spi = sbase;
Simon Glass18530302013-03-19 04:58:56 +0000128
Simon Glassba457562015-03-26 09:29:26 -0600129 ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
Simon Glass18530302013-03-19 04:58:56 +0000130 ctlr->menubytes = sizeof(ich7_spi->opmenu);
Simon Glassba457562015-03-26 09:29:26 -0600131 ctlr->optype = offsetof(struct ich7_spi_regs, optype);
132 ctlr->addr = offsetof(struct ich7_spi_regs, spia);
133 ctlr->data = offsetof(struct ich7_spi_regs, spid);
Simon Glass18530302013-03-19 04:58:56 +0000134 ctlr->databytes = sizeof(ich7_spi->spid);
Simon Glassba457562015-03-26 09:29:26 -0600135 ctlr->status = offsetof(struct ich7_spi_regs, spis);
136 ctlr->control = offsetof(struct ich7_spi_regs, spic);
137 ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
138 ctlr->preop = offsetof(struct ich7_spi_regs, preop);
Simon Glass18530302013-03-19 04:58:56 +0000139 ctlr->base = ich7_spi;
Bin Meng6e670b52016-02-01 01:40:38 -0800140 } else if (plat->ich_version == ICHV_9) {
Simon Glassf2b85ab2016-01-18 20:19:21 -0700141 struct ich9_spi_regs *ich9_spi = sbase;
Simon Glass18530302013-03-19 04:58:56 +0000142
Simon Glassba457562015-03-26 09:29:26 -0600143 ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
Simon Glass18530302013-03-19 04:58:56 +0000144 ctlr->menubytes = sizeof(ich9_spi->opmenu);
Simon Glassba457562015-03-26 09:29:26 -0600145 ctlr->optype = offsetof(struct ich9_spi_regs, optype);
146 ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
147 ctlr->data = offsetof(struct ich9_spi_regs, fdata);
Simon Glass18530302013-03-19 04:58:56 +0000148 ctlr->databytes = sizeof(ich9_spi->fdata);
Simon Glassba457562015-03-26 09:29:26 -0600149 ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
150 ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
151 ctlr->speed = ctlr->control + 2;
152 ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
153 ctlr->preop = offsetof(struct ich9_spi_regs, preop);
Simon Glass50787922015-07-03 18:28:22 -0600154 ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
Simon Glass18530302013-03-19 04:58:56 +0000155 ctlr->pr = &ich9_spi->pr[0];
156 ctlr->base = ich9_spi;
157 } else {
Simon Glassba457562015-03-26 09:29:26 -0600158 debug("ICH SPI: Unrecognised ICH version %d\n",
159 plat->ich_version);
160 return -EINVAL;
Simon Glass18530302013-03-19 04:58:56 +0000161 }
Simon Glass18530302013-03-19 04:58:56 +0000162
163 /* Work out the maximum speed we can support */
164 ctlr->max_speed = 20000000;
Bin Meng6e670b52016-02-01 01:40:38 -0800165 if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
Simon Glass18530302013-03-19 04:58:56 +0000166 ctlr->max_speed = 33000000;
Simon Glassf2b85ab2016-01-18 20:19:21 -0700167 debug("ICH SPI: Version ID %d detected at %p, speed %ld\n",
Simon Glassba457562015-03-26 09:29:26 -0600168 plat->ich_version, ctlr->base, ctlr->max_speed);
Simon Glass18530302013-03-19 04:58:56 +0000169
170 ich_set_bbar(ctlr, 0);
171
172 return 0;
173}
174
Simon Glass18530302013-03-19 04:58:56 +0000175static inline void spi_use_out(struct spi_trans *trans, unsigned bytes)
176{
177 trans->out += bytes;
178 trans->bytesout -= bytes;
179}
180
181static inline void spi_use_in(struct spi_trans *trans, unsigned bytes)
182{
183 trans->in += bytes;
184 trans->bytesin -= bytes;
185}
186
Bin Meng3e791412017-08-15 22:38:29 -0700187static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
188{
189 int lock = 0;
190
191 if (plat->ich_version == ICHV_7) {
192 struct ich7_spi_regs *ich7_spi = sbase;
193
194 lock = readw(&ich7_spi->spis) & SPIS_LOCK;
195 } else if (plat->ich_version == ICHV_9) {
196 struct ich9_spi_regs *ich9_spi = sbase;
197
198 lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
199 }
200
201 return lock != 0;
202}
203
Simon Glass18530302013-03-19 04:58:56 +0000204static void spi_setup_type(struct spi_trans *trans, int data_bytes)
205{
206 trans->type = 0xFF;
207
Bin Meng9eb43392016-02-01 01:40:36 -0800208 /* Try to guess spi type from read/write sizes */
Simon Glass18530302013-03-19 04:58:56 +0000209 if (trans->bytesin == 0) {
210 if (trans->bytesout + data_bytes > 4)
211 /*
212 * If bytesin = 0 and bytesout > 4, we presume this is
213 * a write data operation, which is accompanied by an
214 * address.
215 */
216 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
217 else
218 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
219 return;
220 }
221
222 if (trans->bytesout == 1) { /* and bytesin is > 0 */
223 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
224 return;
225 }
226
227 if (trans->bytesout == 4) /* and bytesin is > 0 */
228 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
229
230 /* Fast read command is called with 5 bytes instead of 4 */
231 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
232 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
233 --trans->bytesout;
234 }
235}
236
Bin Meng3e791412017-08-15 22:38:29 -0700237static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
238 bool lock)
Simon Glass18530302013-03-19 04:58:56 +0000239{
240 uint16_t optypes;
Simon Glassba457562015-03-26 09:29:26 -0600241 uint8_t opmenu[ctlr->menubytes];
Simon Glass18530302013-03-19 04:58:56 +0000242
243 trans->opcode = trans->out[0];
244 spi_use_out(trans, 1);
Bin Meng3e791412017-08-15 22:38:29 -0700245 if (!lock) {
Simon Glass18530302013-03-19 04:58:56 +0000246 /* The lock is off, so just use index 0. */
Simon Glassba457562015-03-26 09:29:26 -0600247 ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
248 optypes = ich_readw(ctlr, ctlr->optype);
Simon Glass18530302013-03-19 04:58:56 +0000249 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Simon Glassba457562015-03-26 09:29:26 -0600250 ich_writew(ctlr, optypes, ctlr->optype);
Simon Glass18530302013-03-19 04:58:56 +0000251 return 0;
252 } else {
253 /* The lock is on. See if what we need is on the menu. */
254 uint8_t optype;
255 uint16_t opcode_index;
256
257 /* Write Enable is handled as atomic prefix */
258 if (trans->opcode == SPI_OPCODE_WREN)
259 return 0;
260
Simon Glassba457562015-03-26 09:29:26 -0600261 read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
262 for (opcode_index = 0; opcode_index < ctlr->menubytes;
Simon Glass18530302013-03-19 04:58:56 +0000263 opcode_index++) {
264 if (opmenu[opcode_index] == trans->opcode)
265 break;
266 }
267
Simon Glassba457562015-03-26 09:29:26 -0600268 if (opcode_index == ctlr->menubytes) {
Simon Glass18530302013-03-19 04:58:56 +0000269 printf("ICH SPI: Opcode %x not found\n",
270 trans->opcode);
Simon Glassba457562015-03-26 09:29:26 -0600271 return -EINVAL;
Simon Glass18530302013-03-19 04:58:56 +0000272 }
273
Simon Glassba457562015-03-26 09:29:26 -0600274 optypes = ich_readw(ctlr, ctlr->optype);
Simon Glass18530302013-03-19 04:58:56 +0000275 optype = (optypes >> (opcode_index * 2)) & 0x3;
276 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
277 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
278 trans->bytesout >= 3) {
279 /* We guessed wrong earlier. Fix it up. */
280 trans->type = optype;
281 }
282 if (optype != trans->type) {
283 printf("ICH SPI: Transaction doesn't fit type %d\n",
284 optype);
Simon Glassba457562015-03-26 09:29:26 -0600285 return -ENOSPC;
Simon Glass18530302013-03-19 04:58:56 +0000286 }
287 return opcode_index;
288 }
289}
290
291static int spi_setup_offset(struct spi_trans *trans)
292{
Bin Meng9eb43392016-02-01 01:40:36 -0800293 /* Separate the SPI address and data */
Simon Glass18530302013-03-19 04:58:56 +0000294 switch (trans->type) {
295 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
296 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
297 return 0;
298 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
299 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
300 trans->offset = ((uint32_t)trans->out[0] << 16) |
301 ((uint32_t)trans->out[1] << 8) |
302 ((uint32_t)trans->out[2] << 0);
303 spi_use_out(trans, 3);
304 return 1;
305 default:
306 printf("Unrecognized SPI transaction type %#x\n", trans->type);
Simon Glassba457562015-03-26 09:29:26 -0600307 return -EPROTO;
Simon Glass18530302013-03-19 04:58:56 +0000308 }
309}
310
311/*
312 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
York Sun472d5462013-04-01 11:29:11 -0700313 * below is true) or 0. In case the wait was for the bit(s) to set - write
Simon Glass18530302013-03-19 04:58:56 +0000314 * those bits back, which would cause resetting them.
315 *
316 * Return the last read status value on success or -1 on failure.
317 */
Simon Glassba457562015-03-26 09:29:26 -0600318static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
319 int wait_til_set)
Simon Glass18530302013-03-19 04:58:56 +0000320{
321 int timeout = 600000; /* This will result in 6s */
322 u16 status = 0;
323
324 while (timeout--) {
Simon Glassba457562015-03-26 09:29:26 -0600325 status = ich_readw(ctlr, ctlr->status);
Simon Glass18530302013-03-19 04:58:56 +0000326 if (wait_til_set ^ ((status & bitmask) == 0)) {
Simon Glassba457562015-03-26 09:29:26 -0600327 if (wait_til_set) {
328 ich_writew(ctlr, status & bitmask,
329 ctlr->status);
330 }
Simon Glass18530302013-03-19 04:58:56 +0000331 return status;
332 }
333 udelay(10);
334 }
335
336 printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
337 status, bitmask);
Simon Glassba457562015-03-26 09:29:26 -0600338 return -ETIMEDOUT;
Simon Glass18530302013-03-19 04:58:56 +0000339}
340
Bin Mengb42711f2017-08-15 22:38:30 -0700341void ich_spi_config_opcode(struct udevice *dev)
342{
343 struct ich_spi_priv *ctlr = dev_get_priv(dev);
344
345 /*
346 * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
347 * to prevent accidental or intentional writes. Before they get
348 * locked down, these registers should be initialized properly.
349 */
350 ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
351 ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
352 ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
353 ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
354}
355
Simon Glassba457562015-03-26 09:29:26 -0600356static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
357 const void *dout, void *din, unsigned long flags)
Simon Glass18530302013-03-19 04:58:56 +0000358{
Simon Glassba457562015-03-26 09:29:26 -0600359 struct udevice *bus = dev_get_parent(dev);
Simon Glasse1e332c2015-07-03 18:28:21 -0600360 struct ich_spi_platdata *plat = dev_get_platdata(bus);
Simon Glassba457562015-03-26 09:29:26 -0600361 struct ich_spi_priv *ctlr = dev_get_priv(bus);
Simon Glass18530302013-03-19 04:58:56 +0000362 uint16_t control;
363 int16_t opcode_index;
364 int with_address;
365 int status;
366 int bytes = bitlen / 8;
Simon Glassba457562015-03-26 09:29:26 -0600367 struct spi_trans *trans = &ctlr->trans;
Simon Glass18530302013-03-19 04:58:56 +0000368 unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
369 int using_cmd = 0;
Bin Meng3e791412017-08-15 22:38:29 -0700370 bool lock = spi_lock_status(plat, ctlr->base);
Simon Glassba457562015-03-26 09:29:26 -0600371 int ret;
Simon Glass18530302013-03-19 04:58:56 +0000372
Simon Glass5d4a7572015-06-07 08:50:33 -0600373 /* We don't support writing partial bytes */
Simon Glass18530302013-03-19 04:58:56 +0000374 if (bitlen % 8) {
375 debug("ICH SPI: Accessing partial bytes not supported\n");
Simon Glassba457562015-03-26 09:29:26 -0600376 return -EPROTONOSUPPORT;
Simon Glass18530302013-03-19 04:58:56 +0000377 }
378
379 /* An empty end transaction can be ignored */
380 if (type == SPI_XFER_END && !dout && !din)
381 return 0;
382
383 if (type & SPI_XFER_BEGIN)
384 memset(trans, '\0', sizeof(*trans));
385
386 /* Dp we need to come back later to finish it? */
387 if (dout && type == SPI_XFER_BEGIN) {
388 if (bytes > ICH_MAX_CMD_LEN) {
389 debug("ICH SPI: Command length limit exceeded\n");
Simon Glassba457562015-03-26 09:29:26 -0600390 return -ENOSPC;
Simon Glass18530302013-03-19 04:58:56 +0000391 }
392 memcpy(trans->cmd, dout, bytes);
393 trans->cmd_len = bytes;
Simon Glassfffe25d2016-01-18 20:19:20 -0700394 debug_trace("ICH SPI: Saved %d bytes\n", bytes);
Simon Glass18530302013-03-19 04:58:56 +0000395 return 0;
396 }
397
398 /*
399 * We process a 'middle' spi_xfer() call, which has no
400 * SPI_XFER_BEGIN/END, as an independent transaction as if it had
401 * an end. We therefore repeat the command. This is because ICH
402 * seems to have no support for this, or because interest (in digging
403 * out the details and creating a special case in the code) is low.
404 */
405 if (trans->cmd_len) {
406 trans->out = trans->cmd;
407 trans->bytesout = trans->cmd_len;
408 using_cmd = 1;
Simon Glassfffe25d2016-01-18 20:19:20 -0700409 debug_trace("ICH SPI: Using %d bytes\n", trans->cmd_len);
Simon Glass18530302013-03-19 04:58:56 +0000410 } else {
411 trans->out = dout;
412 trans->bytesout = dout ? bytes : 0;
413 }
414
415 trans->in = din;
416 trans->bytesin = din ? bytes : 0;
417
Bin Meng9eb43392016-02-01 01:40:36 -0800418 /* There has to always at least be an opcode */
Simon Glass18530302013-03-19 04:58:56 +0000419 if (!trans->bytesout) {
420 debug("ICH SPI: No opcode for transfer\n");
Simon Glassba457562015-03-26 09:29:26 -0600421 return -EPROTO;
Simon Glass18530302013-03-19 04:58:56 +0000422 }
423
Simon Glassba457562015-03-26 09:29:26 -0600424 ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
425 if (ret < 0)
426 return ret;
Simon Glass18530302013-03-19 04:58:56 +0000427
Bin Meng6e670b52016-02-01 01:40:38 -0800428 if (plat->ich_version == ICHV_7)
Simon Glasse1e332c2015-07-03 18:28:21 -0600429 ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
430 else
431 ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
Simon Glass18530302013-03-19 04:58:56 +0000432
433 spi_setup_type(trans, using_cmd ? bytes : 0);
Bin Meng3e791412017-08-15 22:38:29 -0700434 opcode_index = spi_setup_opcode(ctlr, trans, lock);
Simon Glass18530302013-03-19 04:58:56 +0000435 if (opcode_index < 0)
Simon Glassba457562015-03-26 09:29:26 -0600436 return -EINVAL;
Simon Glass18530302013-03-19 04:58:56 +0000437 with_address = spi_setup_offset(trans);
438 if (with_address < 0)
Simon Glassba457562015-03-26 09:29:26 -0600439 return -EINVAL;
Simon Glass18530302013-03-19 04:58:56 +0000440
441 if (trans->opcode == SPI_OPCODE_WREN) {
442 /*
443 * Treat Write Enable as Atomic Pre-Op if possible
444 * in order to prevent the Management Engine from
445 * issuing a transaction between WREN and DATA.
446 */
Bin Meng3e791412017-08-15 22:38:29 -0700447 if (!lock)
Simon Glassba457562015-03-26 09:29:26 -0600448 ich_writew(ctlr, trans->opcode, ctlr->preop);
Simon Glass18530302013-03-19 04:58:56 +0000449 return 0;
450 }
451
Simon Glassba457562015-03-26 09:29:26 -0600452 if (ctlr->speed && ctlr->max_speed >= 33000000) {
Simon Glass18530302013-03-19 04:58:56 +0000453 int byte;
454
Simon Glassba457562015-03-26 09:29:26 -0600455 byte = ich_readb(ctlr, ctlr->speed);
456 if (ctlr->cur_speed >= 33000000)
Simon Glass18530302013-03-19 04:58:56 +0000457 byte |= SSFC_SCF_33MHZ;
458 else
459 byte &= ~SSFC_SCF_33MHZ;
Simon Glassba457562015-03-26 09:29:26 -0600460 ich_writeb(ctlr, byte, ctlr->speed);
Simon Glass18530302013-03-19 04:58:56 +0000461 }
462
463 /* See if we have used up the command data */
464 if (using_cmd && dout && bytes) {
465 trans->out = dout;
466 trans->bytesout = bytes;
Simon Glassfffe25d2016-01-18 20:19:20 -0700467 debug_trace("ICH SPI: Moving to data, %d bytes\n", bytes);
Simon Glass18530302013-03-19 04:58:56 +0000468 }
469
470 /* Preset control fields */
Simon Glass18530302013-03-19 04:58:56 +0000471 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
472
473 /* Issue atomic preop cycle if needed */
Simon Glassba457562015-03-26 09:29:26 -0600474 if (ich_readw(ctlr, ctlr->preop))
Simon Glass18530302013-03-19 04:58:56 +0000475 control |= SPIC_ACS;
476
477 if (!trans->bytesout && !trans->bytesin) {
478 /* SPI addresses are 24 bit only */
Simon Glassba457562015-03-26 09:29:26 -0600479 if (with_address) {
480 ich_writel(ctlr, trans->offset & 0x00FFFFFF,
481 ctlr->addr);
482 }
Simon Glass18530302013-03-19 04:58:56 +0000483 /*
484 * This is a 'no data' command (like Write Enable), its
485 * bitesout size was 1, decremented to zero while executing
486 * spi_setup_opcode() above. Tell the chip to send the
487 * command.
488 */
Simon Glassba457562015-03-26 09:29:26 -0600489 ich_writew(ctlr, control, ctlr->control);
Simon Glass18530302013-03-19 04:58:56 +0000490
491 /* wait for the result */
Simon Glassba457562015-03-26 09:29:26 -0600492 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
493 if (status < 0)
494 return status;
Simon Glass18530302013-03-19 04:58:56 +0000495
496 if (status & SPIS_FCERR) {
497 debug("ICH SPI: Command transaction error\n");
Simon Glassba457562015-03-26 09:29:26 -0600498 return -EIO;
Simon Glass18530302013-03-19 04:58:56 +0000499 }
500
501 return 0;
502 }
503
504 /*
505 * Check if this is a write command atempting to transfer more bytes
506 * than the controller can handle. Iterations for writes are not
507 * supported here because each SPI write command needs to be preceded
508 * and followed by other SPI commands, and this sequence is controlled
509 * by the SPI chip driver.
510 */
Simon Glassba457562015-03-26 09:29:26 -0600511 if (trans->bytesout > ctlr->databytes) {
Simon Glass18530302013-03-19 04:58:56 +0000512 debug("ICH SPI: Too much to write. This should be prevented by the driver's max_write_size?\n");
Simon Glassba457562015-03-26 09:29:26 -0600513 return -EPROTO;
Simon Glass18530302013-03-19 04:58:56 +0000514 }
515
516 /*
517 * Read or write up to databytes bytes at a time until everything has
518 * been sent.
519 */
520 while (trans->bytesout || trans->bytesin) {
521 uint32_t data_length;
Simon Glass18530302013-03-19 04:58:56 +0000522
523 /* SPI addresses are 24 bit only */
Simon Glassba457562015-03-26 09:29:26 -0600524 ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
Simon Glass18530302013-03-19 04:58:56 +0000525
526 if (trans->bytesout)
Simon Glassba457562015-03-26 09:29:26 -0600527 data_length = min(trans->bytesout, ctlr->databytes);
Simon Glass18530302013-03-19 04:58:56 +0000528 else
Simon Glassba457562015-03-26 09:29:26 -0600529 data_length = min(trans->bytesin, ctlr->databytes);
Simon Glass18530302013-03-19 04:58:56 +0000530
531 /* Program data into FDATA0 to N */
532 if (trans->bytesout) {
Simon Glassba457562015-03-26 09:29:26 -0600533 write_reg(ctlr, trans->out, ctlr->data, data_length);
Simon Glass18530302013-03-19 04:58:56 +0000534 spi_use_out(trans, data_length);
535 if (with_address)
536 trans->offset += data_length;
537 }
538
539 /* Add proper control fields' values */
Simon Glassba457562015-03-26 09:29:26 -0600540 control &= ~((ctlr->databytes - 1) << 8);
Simon Glass18530302013-03-19 04:58:56 +0000541 control |= SPIC_DS;
542 control |= (data_length - 1) << 8;
543
544 /* write it */
Simon Glassba457562015-03-26 09:29:26 -0600545 ich_writew(ctlr, control, ctlr->control);
Simon Glass18530302013-03-19 04:58:56 +0000546
Bin Meng9eb43392016-02-01 01:40:36 -0800547 /* Wait for Cycle Done Status or Flash Cycle Error */
Simon Glassba457562015-03-26 09:29:26 -0600548 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
549 if (status < 0)
550 return status;
Simon Glass18530302013-03-19 04:58:56 +0000551
552 if (status & SPIS_FCERR) {
Simon Glass5d4a7572015-06-07 08:50:33 -0600553 debug("ICH SPI: Data transaction error %x\n", status);
Simon Glassba457562015-03-26 09:29:26 -0600554 return -EIO;
Simon Glass18530302013-03-19 04:58:56 +0000555 }
556
557 if (trans->bytesin) {
Simon Glassba457562015-03-26 09:29:26 -0600558 read_reg(ctlr, ctlr->data, trans->in, data_length);
Simon Glass18530302013-03-19 04:58:56 +0000559 spi_use_in(trans, data_length);
560 if (with_address)
561 trans->offset += data_length;
562 }
563 }
564
565 /* Clear atomic preop now that xfer is done */
Bin Mengd2ca80c2017-08-26 19:22:59 -0700566 if (!lock)
567 ich_writew(ctlr, 0, ctlr->preop);
Simon Glass18530302013-03-19 04:58:56 +0000568
569 return 0;
570}
571
Simon Glassf2b85ab2016-01-18 20:19:21 -0700572static int ich_spi_probe(struct udevice *dev)
Simon Glassba457562015-03-26 09:29:26 -0600573{
Simon Glassf2b85ab2016-01-18 20:19:21 -0700574 struct ich_spi_platdata *plat = dev_get_platdata(dev);
575 struct ich_spi_priv *priv = dev_get_priv(dev);
Simon Glassba457562015-03-26 09:29:26 -0600576 uint8_t bios_cntl;
577 int ret;
578
Simon Glassf2b85ab2016-01-18 20:19:21 -0700579 ret = ich_init_controller(dev, plat, priv);
Simon Glassba457562015-03-26 09:29:26 -0600580 if (ret)
581 return ret;
Simon Glassf2b85ab2016-01-18 20:19:21 -0700582 /* Disable the BIOS write protect so write commands are allowed */
583 ret = pch_set_spi_protect(dev->parent, false);
584 if (ret == -ENOSYS) {
Simon Glass50787922015-07-03 18:28:22 -0600585 bios_cntl = ich_readb(priv, priv->bcr);
Jagan Teki69fd4c32015-10-23 01:37:56 +0530586 bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
Simon Glassba457562015-03-26 09:29:26 -0600587 bios_cntl |= 1; /* Write Protect Disable (WPD) */
Simon Glass50787922015-07-03 18:28:22 -0600588 ich_writeb(priv, bios_cntl, priv->bcr);
Simon Glassf2b85ab2016-01-18 20:19:21 -0700589 } else if (ret) {
590 debug("%s: Failed to disable write-protect: err=%d\n",
591 __func__, ret);
592 return ret;
Simon Glassba457562015-03-26 09:29:26 -0600593 }
594
595 priv->cur_speed = priv->max_speed;
596
597 return 0;
598}
599
Stefan Roese4759dff2017-04-24 09:48:04 +0200600static int ich_spi_remove(struct udevice *bus)
601{
Stefan Roese4759dff2017-04-24 09:48:04 +0200602 /*
603 * Configure SPI controller so that the Linux MTD driver can fully
604 * access the SPI NOR chip
605 */
Bin Mengb42711f2017-08-15 22:38:30 -0700606 ich_spi_config_opcode(bus);
Stefan Roese4759dff2017-04-24 09:48:04 +0200607
608 return 0;
609}
610
Simon Glassba457562015-03-26 09:29:26 -0600611static int ich_spi_set_speed(struct udevice *bus, uint speed)
612{
613 struct ich_spi_priv *priv = dev_get_priv(bus);
614
615 priv->cur_speed = speed;
616
617 return 0;
618}
619
620static int ich_spi_set_mode(struct udevice *bus, uint mode)
621{
622 debug("%s: mode=%d\n", __func__, mode);
623
624 return 0;
625}
626
627static int ich_spi_child_pre_probe(struct udevice *dev)
628{
629 struct udevice *bus = dev_get_parent(dev);
630 struct ich_spi_platdata *plat = dev_get_platdata(bus);
631 struct ich_spi_priv *priv = dev_get_priv(bus);
Simon Glassbcbe3d12015-09-28 23:32:01 -0600632 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glassba457562015-03-26 09:29:26 -0600633
634 /*
635 * Yes this controller can only write a small number of bytes at
636 * once! The limit is typically 64 bytes.
637 */
638 slave->max_write_size = priv->databytes;
639 /*
640 * ICH 7 SPI controller only supports array read command
641 * and byte program command for SST flash
642 */
Jagan Teki08fe9c22016-08-08 17:12:12 +0530643 if (plat->ich_version == ICHV_7)
644 slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
Simon Glassba457562015-03-26 09:29:26 -0600645
646 return 0;
647}
648
Bin Meng1f9eb592016-02-01 01:40:37 -0800649static int ich_spi_ofdata_to_platdata(struct udevice *dev)
650{
651 struct ich_spi_platdata *plat = dev_get_platdata(dev);
Simon Glasse160f7d2017-01-17 16:52:55 -0700652 int node = dev_of_offset(dev);
Bin Meng1f9eb592016-02-01 01:40:37 -0800653 int ret;
654
Simon Glasse160f7d2017-01-17 16:52:55 -0700655 ret = fdt_node_check_compatible(gd->fdt_blob, node, "intel,ich7-spi");
Bin Meng1f9eb592016-02-01 01:40:37 -0800656 if (ret == 0) {
Bin Meng6e670b52016-02-01 01:40:38 -0800657 plat->ich_version = ICHV_7;
Bin Meng1f9eb592016-02-01 01:40:37 -0800658 } else {
Simon Glasse160f7d2017-01-17 16:52:55 -0700659 ret = fdt_node_check_compatible(gd->fdt_blob, node,
Bin Meng1f9eb592016-02-01 01:40:37 -0800660 "intel,ich9-spi");
661 if (ret == 0)
Bin Meng6e670b52016-02-01 01:40:38 -0800662 plat->ich_version = ICHV_9;
Bin Meng1f9eb592016-02-01 01:40:37 -0800663 }
664
665 return ret;
666}
667
Simon Glassba457562015-03-26 09:29:26 -0600668static const struct dm_spi_ops ich_spi_ops = {
669 .xfer = ich_spi_xfer,
670 .set_speed = ich_spi_set_speed,
671 .set_mode = ich_spi_set_mode,
672 /*
673 * cs_info is not needed, since we require all chip selects to be
674 * in the device tree explicitly
675 */
676};
677
678static const struct udevice_id ich_spi_ids[] = {
Bin Meng1f9eb592016-02-01 01:40:37 -0800679 { .compatible = "intel,ich7-spi" },
680 { .compatible = "intel,ich9-spi" },
Simon Glassba457562015-03-26 09:29:26 -0600681 { }
682};
683
684U_BOOT_DRIVER(ich_spi) = {
685 .name = "ich_spi",
686 .id = UCLASS_SPI,
687 .of_match = ich_spi_ids,
688 .ops = &ich_spi_ops,
Bin Meng1f9eb592016-02-01 01:40:37 -0800689 .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
Simon Glassba457562015-03-26 09:29:26 -0600690 .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
691 .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
692 .child_pre_probe = ich_spi_child_pre_probe,
693 .probe = ich_spi_probe,
Stefan Roese4759dff2017-04-24 09:48:04 +0200694 .remove = ich_spi_remove,
695 .flags = DM_FLAG_OS_PREPARE,
Simon Glassba457562015-03-26 09:29:26 -0600696};