blob: 03531a8c0c3f403ccf156f8782e743caf5f99990 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass18530302013-03-19 04:58:56 +00002/*
3 * Copyright (c) 2011-12 The Chromium OS Authors.
4 *
Simon Glass18530302013-03-19 04:58:56 +00005 * This file is derived from the flashrom project.
6 */
Bin Meng9eb43392016-02-01 01:40:36 -08007
Simon Glass18530302013-03-19 04:58:56 +00008#include <common.h>
Simon Glassba457562015-03-26 09:29:26 -06009#include <dm.h>
Simon Glass5093bad2015-01-27 22:13:43 -070010#include <errno.h>
Simon Glass18530302013-03-19 04:58:56 +000011#include <malloc.h>
Simon Glassf2b85ab2016-01-18 20:19:21 -070012#include <pch.h>
Simon Glass18530302013-03-19 04:58:56 +000013#include <pci.h>
14#include <pci_ids.h>
Simon Glassf2b85ab2016-01-18 20:19:21 -070015#include <spi.h>
Simon Glass18530302013-03-19 04:58:56 +000016#include <asm/io.h>
17
18#include "ich.h"
19
Bin Meng1f9eb592016-02-01 01:40:37 -080020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glassfffe25d2016-01-18 20:19:20 -070022#ifdef DEBUG_TRACE
23#define debug_trace(fmt, args...) debug(fmt, ##args)
24#else
25#define debug_trace(x, args...)
26#endif
27
Simon Glassba457562015-03-26 09:29:26 -060028static u8 ich_readb(struct ich_spi_priv *priv, int reg)
Simon Glass18530302013-03-19 04:58:56 +000029{
Simon Glassba457562015-03-26 09:29:26 -060030 u8 value = readb(priv->base + reg);
Simon Glass18530302013-03-19 04:58:56 +000031
Simon Glassfffe25d2016-01-18 20:19:20 -070032 debug_trace("read %2.2x from %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000033
34 return value;
35}
36
Simon Glassba457562015-03-26 09:29:26 -060037static u16 ich_readw(struct ich_spi_priv *priv, int reg)
Simon Glass18530302013-03-19 04:58:56 +000038{
Simon Glassba457562015-03-26 09:29:26 -060039 u16 value = readw(priv->base + reg);
Simon Glass18530302013-03-19 04:58:56 +000040
Simon Glassfffe25d2016-01-18 20:19:20 -070041 debug_trace("read %4.4x from %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000042
43 return value;
44}
45
Simon Glassba457562015-03-26 09:29:26 -060046static u32 ich_readl(struct ich_spi_priv *priv, int reg)
Simon Glass18530302013-03-19 04:58:56 +000047{
Simon Glassba457562015-03-26 09:29:26 -060048 u32 value = readl(priv->base + reg);
Simon Glass18530302013-03-19 04:58:56 +000049
Simon Glassfffe25d2016-01-18 20:19:20 -070050 debug_trace("read %8.8x from %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000051
52 return value;
53}
54
Simon Glassba457562015-03-26 09:29:26 -060055static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
Simon Glass18530302013-03-19 04:58:56 +000056{
Simon Glassba457562015-03-26 09:29:26 -060057 writeb(value, priv->base + reg);
Simon Glassfffe25d2016-01-18 20:19:20 -070058 debug_trace("wrote %2.2x to %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000059}
60
Simon Glassba457562015-03-26 09:29:26 -060061static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
Simon Glass18530302013-03-19 04:58:56 +000062{
Simon Glassba457562015-03-26 09:29:26 -060063 writew(value, priv->base + reg);
Simon Glassfffe25d2016-01-18 20:19:20 -070064 debug_trace("wrote %4.4x to %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000065}
66
Simon Glassba457562015-03-26 09:29:26 -060067static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
Simon Glass18530302013-03-19 04:58:56 +000068{
Simon Glassba457562015-03-26 09:29:26 -060069 writel(value, priv->base + reg);
Simon Glassfffe25d2016-01-18 20:19:20 -070070 debug_trace("wrote %8.8x to %4.4x\n", value, reg);
Simon Glass18530302013-03-19 04:58:56 +000071}
72
Simon Glassba457562015-03-26 09:29:26 -060073static void write_reg(struct ich_spi_priv *priv, const void *value,
74 int dest_reg, uint32_t size)
Simon Glass18530302013-03-19 04:58:56 +000075{
Simon Glassba457562015-03-26 09:29:26 -060076 memcpy_toio(priv->base + dest_reg, value, size);
Simon Glass18530302013-03-19 04:58:56 +000077}
78
Simon Glassba457562015-03-26 09:29:26 -060079static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
80 uint32_t size)
Simon Glass18530302013-03-19 04:58:56 +000081{
Simon Glassba457562015-03-26 09:29:26 -060082 memcpy_fromio(value, priv->base + src_reg, size);
Simon Glass18530302013-03-19 04:58:56 +000083}
84
Simon Glassba457562015-03-26 09:29:26 -060085static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
Simon Glass18530302013-03-19 04:58:56 +000086{
87 const uint32_t bbar_mask = 0x00ffff00;
88 uint32_t ichspi_bbar;
89
90 minaddr &= bbar_mask;
Simon Glassba457562015-03-26 09:29:26 -060091 ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
Simon Glass18530302013-03-19 04:58:56 +000092 ichspi_bbar |= minaddr;
Simon Glassba457562015-03-26 09:29:26 -060093 ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
Simon Glass18530302013-03-19 04:58:56 +000094}
95
Simon Glass18530302013-03-19 04:58:56 +000096/* @return 1 if the SPI flash supports the 33MHz speed */
Simon Glassf2b85ab2016-01-18 20:19:21 -070097static int ich9_can_do_33mhz(struct udevice *dev)
Simon Glass18530302013-03-19 04:58:56 +000098{
99 u32 fdod, speed;
100
101 /* Observe SPI Descriptor Component Section 0 */
Simon Glassf2b85ab2016-01-18 20:19:21 -0700102 dm_pci_write_config32(dev->parent, 0xb0, 0x1000);
Simon Glass18530302013-03-19 04:58:56 +0000103
104 /* Extract the Write/Erase SPI Frequency from descriptor */
Simon Glassf2b85ab2016-01-18 20:19:21 -0700105 dm_pci_read_config32(dev->parent, 0xb4, &fdod);
Simon Glass18530302013-03-19 04:58:56 +0000106
107 /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
108 speed = (fdod >> 21) & 7;
109
110 return speed == 1;
111}
112
Simon Glassf2b85ab2016-01-18 20:19:21 -0700113static int ich_init_controller(struct udevice *dev,
114 struct ich_spi_platdata *plat,
Simon Glassba457562015-03-26 09:29:26 -0600115 struct ich_spi_priv *ctlr)
Simon Glass18530302013-03-19 04:58:56 +0000116{
Simon Glassf2b85ab2016-01-18 20:19:21 -0700117 ulong sbase_addr;
118 void *sbase;
Simon Glass5093bad2015-01-27 22:13:43 -0700119
120 /* SBASE is similar */
Bin Meng3e389d82016-02-01 01:40:42 -0800121 pch_get_spi_base(dev->parent, &sbase_addr);
Simon Glassf2b85ab2016-01-18 20:19:21 -0700122 sbase = (void *)sbase_addr;
123 debug("%s: sbase=%p\n", __func__, sbase);
Simon Glass5093bad2015-01-27 22:13:43 -0700124
Bin Meng6e670b52016-02-01 01:40:38 -0800125 if (plat->ich_version == ICHV_7) {
Simon Glassf2b85ab2016-01-18 20:19:21 -0700126 struct ich7_spi_regs *ich7_spi = sbase;
Simon Glass18530302013-03-19 04:58:56 +0000127
Simon Glassba457562015-03-26 09:29:26 -0600128 ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
Simon Glass18530302013-03-19 04:58:56 +0000129 ctlr->menubytes = sizeof(ich7_spi->opmenu);
Simon Glassba457562015-03-26 09:29:26 -0600130 ctlr->optype = offsetof(struct ich7_spi_regs, optype);
131 ctlr->addr = offsetof(struct ich7_spi_regs, spia);
132 ctlr->data = offsetof(struct ich7_spi_regs, spid);
Simon Glass18530302013-03-19 04:58:56 +0000133 ctlr->databytes = sizeof(ich7_spi->spid);
Simon Glassba457562015-03-26 09:29:26 -0600134 ctlr->status = offsetof(struct ich7_spi_regs, spis);
135 ctlr->control = offsetof(struct ich7_spi_regs, spic);
136 ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
137 ctlr->preop = offsetof(struct ich7_spi_regs, preop);
Simon Glass18530302013-03-19 04:58:56 +0000138 ctlr->base = ich7_spi;
Bin Meng6e670b52016-02-01 01:40:38 -0800139 } else if (plat->ich_version == ICHV_9) {
Simon Glassf2b85ab2016-01-18 20:19:21 -0700140 struct ich9_spi_regs *ich9_spi = sbase;
Simon Glass18530302013-03-19 04:58:56 +0000141
Simon Glassba457562015-03-26 09:29:26 -0600142 ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
Simon Glass18530302013-03-19 04:58:56 +0000143 ctlr->menubytes = sizeof(ich9_spi->opmenu);
Simon Glassba457562015-03-26 09:29:26 -0600144 ctlr->optype = offsetof(struct ich9_spi_regs, optype);
145 ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
146 ctlr->data = offsetof(struct ich9_spi_regs, fdata);
Simon Glass18530302013-03-19 04:58:56 +0000147 ctlr->databytes = sizeof(ich9_spi->fdata);
Simon Glassba457562015-03-26 09:29:26 -0600148 ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
149 ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
150 ctlr->speed = ctlr->control + 2;
151 ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
152 ctlr->preop = offsetof(struct ich9_spi_regs, preop);
Simon Glass50787922015-07-03 18:28:22 -0600153 ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
Simon Glass18530302013-03-19 04:58:56 +0000154 ctlr->pr = &ich9_spi->pr[0];
155 ctlr->base = ich9_spi;
156 } else {
Simon Glassba457562015-03-26 09:29:26 -0600157 debug("ICH SPI: Unrecognised ICH version %d\n",
158 plat->ich_version);
159 return -EINVAL;
Simon Glass18530302013-03-19 04:58:56 +0000160 }
Simon Glass18530302013-03-19 04:58:56 +0000161
162 /* Work out the maximum speed we can support */
163 ctlr->max_speed = 20000000;
Bin Meng6e670b52016-02-01 01:40:38 -0800164 if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
Simon Glass18530302013-03-19 04:58:56 +0000165 ctlr->max_speed = 33000000;
Simon Glassf2b85ab2016-01-18 20:19:21 -0700166 debug("ICH SPI: Version ID %d detected at %p, speed %ld\n",
Simon Glassba457562015-03-26 09:29:26 -0600167 plat->ich_version, ctlr->base, ctlr->max_speed);
Simon Glass18530302013-03-19 04:58:56 +0000168
169 ich_set_bbar(ctlr, 0);
170
171 return 0;
172}
173
Simon Glass18530302013-03-19 04:58:56 +0000174static inline void spi_use_out(struct spi_trans *trans, unsigned bytes)
175{
176 trans->out += bytes;
177 trans->bytesout -= bytes;
178}
179
180static inline void spi_use_in(struct spi_trans *trans, unsigned bytes)
181{
182 trans->in += bytes;
183 trans->bytesin -= bytes;
184}
185
Bin Mengab201072017-10-18 18:20:57 -0700186static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
187{
188 if (plat->ich_version == ICHV_7) {
189 struct ich7_spi_regs *ich7_spi = sbase;
190
191 setbits_le16(&ich7_spi->spis, SPIS_LOCK);
192 } else if (plat->ich_version == ICHV_9) {
193 struct ich9_spi_regs *ich9_spi = sbase;
194
195 setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
196 }
197}
198
Bin Meng3e791412017-08-15 22:38:29 -0700199static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
200{
201 int lock = 0;
202
203 if (plat->ich_version == ICHV_7) {
204 struct ich7_spi_regs *ich7_spi = sbase;
205
206 lock = readw(&ich7_spi->spis) & SPIS_LOCK;
207 } else if (plat->ich_version == ICHV_9) {
208 struct ich9_spi_regs *ich9_spi = sbase;
209
210 lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
211 }
212
213 return lock != 0;
214}
215
Simon Glass18530302013-03-19 04:58:56 +0000216static void spi_setup_type(struct spi_trans *trans, int data_bytes)
217{
218 trans->type = 0xFF;
219
Bin Meng9eb43392016-02-01 01:40:36 -0800220 /* Try to guess spi type from read/write sizes */
Simon Glass18530302013-03-19 04:58:56 +0000221 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
Bin Meng3e791412017-08-15 22:38:29 -0700249static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
250 bool lock)
Simon Glass18530302013-03-19 04:58:56 +0000251{
252 uint16_t optypes;
Simon Glassba457562015-03-26 09:29:26 -0600253 uint8_t opmenu[ctlr->menubytes];
Simon Glass18530302013-03-19 04:58:56 +0000254
255 trans->opcode = trans->out[0];
256 spi_use_out(trans, 1);
Bin Meng3e791412017-08-15 22:38:29 -0700257 if (!lock) {
Simon Glass18530302013-03-19 04:58:56 +0000258 /* The lock is off, so just use index 0. */
Simon Glassba457562015-03-26 09:29:26 -0600259 ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
260 optypes = ich_readw(ctlr, ctlr->optype);
Simon Glass18530302013-03-19 04:58:56 +0000261 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
Simon Glassba457562015-03-26 09:29:26 -0600262 ich_writew(ctlr, optypes, ctlr->optype);
Simon Glass18530302013-03-19 04:58:56 +0000263 return 0;
264 } else {
265 /* The lock is on. See if what we need is on the menu. */
266 uint8_t optype;
267 uint16_t opcode_index;
268
269 /* Write Enable is handled as atomic prefix */
270 if (trans->opcode == SPI_OPCODE_WREN)
271 return 0;
272
Simon Glassba457562015-03-26 09:29:26 -0600273 read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
274 for (opcode_index = 0; opcode_index < ctlr->menubytes;
Simon Glass18530302013-03-19 04:58:56 +0000275 opcode_index++) {
276 if (opmenu[opcode_index] == trans->opcode)
277 break;
278 }
279
Simon Glassba457562015-03-26 09:29:26 -0600280 if (opcode_index == ctlr->menubytes) {
Simon Glass18530302013-03-19 04:58:56 +0000281 printf("ICH SPI: Opcode %x not found\n",
282 trans->opcode);
Simon Glassba457562015-03-26 09:29:26 -0600283 return -EINVAL;
Simon Glass18530302013-03-19 04:58:56 +0000284 }
285
Simon Glassba457562015-03-26 09:29:26 -0600286 optypes = ich_readw(ctlr, ctlr->optype);
Simon Glass18530302013-03-19 04:58:56 +0000287 optype = (optypes >> (opcode_index * 2)) & 0x3;
288 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
289 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
290 trans->bytesout >= 3) {
291 /* We guessed wrong earlier. Fix it up. */
292 trans->type = optype;
293 }
294 if (optype != trans->type) {
295 printf("ICH SPI: Transaction doesn't fit type %d\n",
296 optype);
Simon Glassba457562015-03-26 09:29:26 -0600297 return -ENOSPC;
Simon Glass18530302013-03-19 04:58:56 +0000298 }
299 return opcode_index;
300 }
301}
302
303static int spi_setup_offset(struct spi_trans *trans)
304{
Bin Meng9eb43392016-02-01 01:40:36 -0800305 /* Separate the SPI address and data */
Simon Glass18530302013-03-19 04:58:56 +0000306 switch (trans->type) {
307 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
308 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
309 return 0;
310 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
311 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
312 trans->offset = ((uint32_t)trans->out[0] << 16) |
313 ((uint32_t)trans->out[1] << 8) |
314 ((uint32_t)trans->out[2] << 0);
315 spi_use_out(trans, 3);
316 return 1;
317 default:
318 printf("Unrecognized SPI transaction type %#x\n", trans->type);
Simon Glassba457562015-03-26 09:29:26 -0600319 return -EPROTO;
Simon Glass18530302013-03-19 04:58:56 +0000320 }
321}
322
323/*
324 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
York Sun472d5462013-04-01 11:29:11 -0700325 * below is true) or 0. In case the wait was for the bit(s) to set - write
Simon Glass18530302013-03-19 04:58:56 +0000326 * those bits back, which would cause resetting them.
327 *
328 * Return the last read status value on success or -1 on failure.
329 */
Simon Glassba457562015-03-26 09:29:26 -0600330static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
331 int wait_til_set)
Simon Glass18530302013-03-19 04:58:56 +0000332{
333 int timeout = 600000; /* This will result in 6s */
334 u16 status = 0;
335
336 while (timeout--) {
Simon Glassba457562015-03-26 09:29:26 -0600337 status = ich_readw(ctlr, ctlr->status);
Simon Glass18530302013-03-19 04:58:56 +0000338 if (wait_til_set ^ ((status & bitmask) == 0)) {
Simon Glassba457562015-03-26 09:29:26 -0600339 if (wait_til_set) {
340 ich_writew(ctlr, status & bitmask,
341 ctlr->status);
342 }
Simon Glass18530302013-03-19 04:58:56 +0000343 return status;
344 }
345 udelay(10);
346 }
347
348 printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
349 status, bitmask);
Simon Glassba457562015-03-26 09:29:26 -0600350 return -ETIMEDOUT;
Simon Glass18530302013-03-19 04:58:56 +0000351}
352
Bin Mengb42711f2017-08-15 22:38:30 -0700353void ich_spi_config_opcode(struct udevice *dev)
354{
355 struct ich_spi_priv *ctlr = dev_get_priv(dev);
356
357 /*
358 * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
359 * to prevent accidental or intentional writes. Before they get
360 * locked down, these registers should be initialized properly.
361 */
362 ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
363 ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
364 ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
365 ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
366}
367
Simon Glassba457562015-03-26 09:29:26 -0600368static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
369 const void *dout, void *din, unsigned long flags)
Simon Glass18530302013-03-19 04:58:56 +0000370{
Simon Glassba457562015-03-26 09:29:26 -0600371 struct udevice *bus = dev_get_parent(dev);
Simon Glasse1e332c2015-07-03 18:28:21 -0600372 struct ich_spi_platdata *plat = dev_get_platdata(bus);
Simon Glassba457562015-03-26 09:29:26 -0600373 struct ich_spi_priv *ctlr = dev_get_priv(bus);
Simon Glass18530302013-03-19 04:58:56 +0000374 uint16_t control;
375 int16_t opcode_index;
376 int with_address;
377 int status;
378 int bytes = bitlen / 8;
Simon Glassba457562015-03-26 09:29:26 -0600379 struct spi_trans *trans = &ctlr->trans;
Simon Glass18530302013-03-19 04:58:56 +0000380 unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
381 int using_cmd = 0;
Bin Meng3e791412017-08-15 22:38:29 -0700382 bool lock = spi_lock_status(plat, ctlr->base);
Simon Glassba457562015-03-26 09:29:26 -0600383 int ret;
Simon Glass18530302013-03-19 04:58:56 +0000384
Simon Glass5d4a7572015-06-07 08:50:33 -0600385 /* We don't support writing partial bytes */
Simon Glass18530302013-03-19 04:58:56 +0000386 if (bitlen % 8) {
387 debug("ICH SPI: Accessing partial bytes not supported\n");
Simon Glassba457562015-03-26 09:29:26 -0600388 return -EPROTONOSUPPORT;
Simon Glass18530302013-03-19 04:58:56 +0000389 }
390
391 /* An empty end transaction can be ignored */
392 if (type == SPI_XFER_END && !dout && !din)
393 return 0;
394
395 if (type & SPI_XFER_BEGIN)
396 memset(trans, '\0', sizeof(*trans));
397
398 /* Dp we need to come back later to finish it? */
399 if (dout && type == SPI_XFER_BEGIN) {
400 if (bytes > ICH_MAX_CMD_LEN) {
401 debug("ICH SPI: Command length limit exceeded\n");
Simon Glassba457562015-03-26 09:29:26 -0600402 return -ENOSPC;
Simon Glass18530302013-03-19 04:58:56 +0000403 }
404 memcpy(trans->cmd, dout, bytes);
405 trans->cmd_len = bytes;
Simon Glassfffe25d2016-01-18 20:19:20 -0700406 debug_trace("ICH SPI: Saved %d bytes\n", bytes);
Simon Glass18530302013-03-19 04:58:56 +0000407 return 0;
408 }
409
410 /*
411 * We process a 'middle' spi_xfer() call, which has no
412 * SPI_XFER_BEGIN/END, as an independent transaction as if it had
413 * an end. We therefore repeat the command. This is because ICH
414 * seems to have no support for this, or because interest (in digging
415 * out the details and creating a special case in the code) is low.
416 */
417 if (trans->cmd_len) {
418 trans->out = trans->cmd;
419 trans->bytesout = trans->cmd_len;
420 using_cmd = 1;
Simon Glassfffe25d2016-01-18 20:19:20 -0700421 debug_trace("ICH SPI: Using %d bytes\n", trans->cmd_len);
Simon Glass18530302013-03-19 04:58:56 +0000422 } else {
423 trans->out = dout;
424 trans->bytesout = dout ? bytes : 0;
425 }
426
427 trans->in = din;
428 trans->bytesin = din ? bytes : 0;
429
Bin Meng9eb43392016-02-01 01:40:36 -0800430 /* There has to always at least be an opcode */
Simon Glass18530302013-03-19 04:58:56 +0000431 if (!trans->bytesout) {
432 debug("ICH SPI: No opcode for transfer\n");
Simon Glassba457562015-03-26 09:29:26 -0600433 return -EPROTO;
Simon Glass18530302013-03-19 04:58:56 +0000434 }
435
Simon Glassba457562015-03-26 09:29:26 -0600436 ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
437 if (ret < 0)
438 return ret;
Simon Glass18530302013-03-19 04:58:56 +0000439
Bin Meng6e670b52016-02-01 01:40:38 -0800440 if (plat->ich_version == ICHV_7)
Simon Glasse1e332c2015-07-03 18:28:21 -0600441 ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
442 else
443 ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
Simon Glass18530302013-03-19 04:58:56 +0000444
445 spi_setup_type(trans, using_cmd ? bytes : 0);
Bin Meng3e791412017-08-15 22:38:29 -0700446 opcode_index = spi_setup_opcode(ctlr, trans, lock);
Simon Glass18530302013-03-19 04:58:56 +0000447 if (opcode_index < 0)
Simon Glassba457562015-03-26 09:29:26 -0600448 return -EINVAL;
Simon Glass18530302013-03-19 04:58:56 +0000449 with_address = spi_setup_offset(trans);
450 if (with_address < 0)
Simon Glassba457562015-03-26 09:29:26 -0600451 return -EINVAL;
Simon Glass18530302013-03-19 04:58:56 +0000452
453 if (trans->opcode == SPI_OPCODE_WREN) {
454 /*
455 * Treat Write Enable as Atomic Pre-Op if possible
456 * in order to prevent the Management Engine from
457 * issuing a transaction between WREN and DATA.
458 */
Bin Meng3e791412017-08-15 22:38:29 -0700459 if (!lock)
Simon Glassba457562015-03-26 09:29:26 -0600460 ich_writew(ctlr, trans->opcode, ctlr->preop);
Simon Glass18530302013-03-19 04:58:56 +0000461 return 0;
462 }
463
Simon Glassba457562015-03-26 09:29:26 -0600464 if (ctlr->speed && ctlr->max_speed >= 33000000) {
Simon Glass18530302013-03-19 04:58:56 +0000465 int byte;
466
Simon Glassba457562015-03-26 09:29:26 -0600467 byte = ich_readb(ctlr, ctlr->speed);
468 if (ctlr->cur_speed >= 33000000)
Simon Glass18530302013-03-19 04:58:56 +0000469 byte |= SSFC_SCF_33MHZ;
470 else
471 byte &= ~SSFC_SCF_33MHZ;
Simon Glassba457562015-03-26 09:29:26 -0600472 ich_writeb(ctlr, byte, ctlr->speed);
Simon Glass18530302013-03-19 04:58:56 +0000473 }
474
475 /* See if we have used up the command data */
476 if (using_cmd && dout && bytes) {
477 trans->out = dout;
478 trans->bytesout = bytes;
Simon Glassfffe25d2016-01-18 20:19:20 -0700479 debug_trace("ICH SPI: Moving to data, %d bytes\n", bytes);
Simon Glass18530302013-03-19 04:58:56 +0000480 }
481
482 /* Preset control fields */
Simon Glass18530302013-03-19 04:58:56 +0000483 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
484
485 /* Issue atomic preop cycle if needed */
Simon Glassba457562015-03-26 09:29:26 -0600486 if (ich_readw(ctlr, ctlr->preop))
Simon Glass18530302013-03-19 04:58:56 +0000487 control |= SPIC_ACS;
488
489 if (!trans->bytesout && !trans->bytesin) {
490 /* SPI addresses are 24 bit only */
Simon Glassba457562015-03-26 09:29:26 -0600491 if (with_address) {
492 ich_writel(ctlr, trans->offset & 0x00FFFFFF,
493 ctlr->addr);
494 }
Simon Glass18530302013-03-19 04:58:56 +0000495 /*
496 * This is a 'no data' command (like Write Enable), its
497 * bitesout size was 1, decremented to zero while executing
498 * spi_setup_opcode() above. Tell the chip to send the
499 * command.
500 */
Simon Glassba457562015-03-26 09:29:26 -0600501 ich_writew(ctlr, control, ctlr->control);
Simon Glass18530302013-03-19 04:58:56 +0000502
503 /* wait for the result */
Simon Glassba457562015-03-26 09:29:26 -0600504 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
505 if (status < 0)
506 return status;
Simon Glass18530302013-03-19 04:58:56 +0000507
508 if (status & SPIS_FCERR) {
509 debug("ICH SPI: Command transaction error\n");
Simon Glassba457562015-03-26 09:29:26 -0600510 return -EIO;
Simon Glass18530302013-03-19 04:58:56 +0000511 }
512
513 return 0;
514 }
515
516 /*
517 * Check if this is a write command atempting to transfer more bytes
518 * than the controller can handle. Iterations for writes are not
519 * supported here because each SPI write command needs to be preceded
520 * and followed by other SPI commands, and this sequence is controlled
521 * by the SPI chip driver.
522 */
Simon Glassba457562015-03-26 09:29:26 -0600523 if (trans->bytesout > ctlr->databytes) {
Simon Glass18530302013-03-19 04:58:56 +0000524 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 -0600525 return -EPROTO;
Simon Glass18530302013-03-19 04:58:56 +0000526 }
527
528 /*
529 * Read or write up to databytes bytes at a time until everything has
530 * been sent.
531 */
532 while (trans->bytesout || trans->bytesin) {
533 uint32_t data_length;
Simon Glass18530302013-03-19 04:58:56 +0000534
535 /* SPI addresses are 24 bit only */
Simon Glassba457562015-03-26 09:29:26 -0600536 ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
Simon Glass18530302013-03-19 04:58:56 +0000537
538 if (trans->bytesout)
Simon Glassba457562015-03-26 09:29:26 -0600539 data_length = min(trans->bytesout, ctlr->databytes);
Simon Glass18530302013-03-19 04:58:56 +0000540 else
Simon Glassba457562015-03-26 09:29:26 -0600541 data_length = min(trans->bytesin, ctlr->databytes);
Simon Glass18530302013-03-19 04:58:56 +0000542
543 /* Program data into FDATA0 to N */
544 if (trans->bytesout) {
Simon Glassba457562015-03-26 09:29:26 -0600545 write_reg(ctlr, trans->out, ctlr->data, data_length);
Simon Glass18530302013-03-19 04:58:56 +0000546 spi_use_out(trans, data_length);
547 if (with_address)
548 trans->offset += data_length;
549 }
550
551 /* Add proper control fields' values */
Simon Glassba457562015-03-26 09:29:26 -0600552 control &= ~((ctlr->databytes - 1) << 8);
Simon Glass18530302013-03-19 04:58:56 +0000553 control |= SPIC_DS;
554 control |= (data_length - 1) << 8;
555
556 /* write it */
Simon Glassba457562015-03-26 09:29:26 -0600557 ich_writew(ctlr, control, ctlr->control);
Simon Glass18530302013-03-19 04:58:56 +0000558
Bin Meng9eb43392016-02-01 01:40:36 -0800559 /* Wait for Cycle Done Status or Flash Cycle Error */
Simon Glassba457562015-03-26 09:29:26 -0600560 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
561 if (status < 0)
562 return status;
Simon Glass18530302013-03-19 04:58:56 +0000563
564 if (status & SPIS_FCERR) {
Simon Glass5d4a7572015-06-07 08:50:33 -0600565 debug("ICH SPI: Data transaction error %x\n", status);
Simon Glassba457562015-03-26 09:29:26 -0600566 return -EIO;
Simon Glass18530302013-03-19 04:58:56 +0000567 }
568
569 if (trans->bytesin) {
Simon Glassba457562015-03-26 09:29:26 -0600570 read_reg(ctlr, ctlr->data, trans->in, data_length);
Simon Glass18530302013-03-19 04:58:56 +0000571 spi_use_in(trans, data_length);
572 if (with_address)
573 trans->offset += data_length;
574 }
575 }
576
577 /* Clear atomic preop now that xfer is done */
Bin Mengd2ca80c2017-08-26 19:22:59 -0700578 if (!lock)
579 ich_writew(ctlr, 0, ctlr->preop);
Simon Glass18530302013-03-19 04:58:56 +0000580
581 return 0;
582}
583
Simon Glassf2b85ab2016-01-18 20:19:21 -0700584static int ich_spi_probe(struct udevice *dev)
Simon Glassba457562015-03-26 09:29:26 -0600585{
Simon Glassf2b85ab2016-01-18 20:19:21 -0700586 struct ich_spi_platdata *plat = dev_get_platdata(dev);
587 struct ich_spi_priv *priv = dev_get_priv(dev);
Simon Glassba457562015-03-26 09:29:26 -0600588 uint8_t bios_cntl;
589 int ret;
590
Simon Glassf2b85ab2016-01-18 20:19:21 -0700591 ret = ich_init_controller(dev, plat, priv);
Simon Glassba457562015-03-26 09:29:26 -0600592 if (ret)
593 return ret;
Simon Glassf2b85ab2016-01-18 20:19:21 -0700594 /* Disable the BIOS write protect so write commands are allowed */
595 ret = pch_set_spi_protect(dev->parent, false);
596 if (ret == -ENOSYS) {
Simon Glass50787922015-07-03 18:28:22 -0600597 bios_cntl = ich_readb(priv, priv->bcr);
Jagan Teki69fd4c32015-10-23 01:37:56 +0530598 bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
Simon Glassba457562015-03-26 09:29:26 -0600599 bios_cntl |= 1; /* Write Protect Disable (WPD) */
Simon Glass50787922015-07-03 18:28:22 -0600600 ich_writeb(priv, bios_cntl, priv->bcr);
Simon Glassf2b85ab2016-01-18 20:19:21 -0700601 } else if (ret) {
602 debug("%s: Failed to disable write-protect: err=%d\n",
603 __func__, ret);
604 return ret;
Simon Glassba457562015-03-26 09:29:26 -0600605 }
606
Bin Mengab201072017-10-18 18:20:57 -0700607 /* Lock down SPI controller settings if required */
608 if (plat->lockdown) {
609 ich_spi_config_opcode(dev);
610 spi_lock_down(plat, priv->base);
611 }
612
Simon Glassba457562015-03-26 09:29:26 -0600613 priv->cur_speed = priv->max_speed;
614
615 return 0;
616}
617
Stefan Roese4759dff2017-04-24 09:48:04 +0200618static int ich_spi_remove(struct udevice *bus)
619{
Stefan Roese4759dff2017-04-24 09:48:04 +0200620 /*
621 * Configure SPI controller so that the Linux MTD driver can fully
622 * access the SPI NOR chip
623 */
Bin Mengb42711f2017-08-15 22:38:30 -0700624 ich_spi_config_opcode(bus);
Stefan Roese4759dff2017-04-24 09:48:04 +0200625
626 return 0;
627}
628
Simon Glassba457562015-03-26 09:29:26 -0600629static int ich_spi_set_speed(struct udevice *bus, uint speed)
630{
631 struct ich_spi_priv *priv = dev_get_priv(bus);
632
633 priv->cur_speed = speed;
634
635 return 0;
636}
637
638static int ich_spi_set_mode(struct udevice *bus, uint mode)
639{
640 debug("%s: mode=%d\n", __func__, mode);
641
642 return 0;
643}
644
645static int ich_spi_child_pre_probe(struct udevice *dev)
646{
647 struct udevice *bus = dev_get_parent(dev);
648 struct ich_spi_platdata *plat = dev_get_platdata(bus);
649 struct ich_spi_priv *priv = dev_get_priv(bus);
Simon Glassbcbe3d12015-09-28 23:32:01 -0600650 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glassba457562015-03-26 09:29:26 -0600651
652 /*
653 * Yes this controller can only write a small number of bytes at
654 * once! The limit is typically 64 bytes.
655 */
656 slave->max_write_size = priv->databytes;
657 /*
658 * ICH 7 SPI controller only supports array read command
659 * and byte program command for SST flash
660 */
Jagan Teki08fe9c22016-08-08 17:12:12 +0530661 if (plat->ich_version == ICHV_7)
662 slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
Simon Glassba457562015-03-26 09:29:26 -0600663
664 return 0;
665}
666
Bin Meng1f9eb592016-02-01 01:40:37 -0800667static int ich_spi_ofdata_to_platdata(struct udevice *dev)
668{
669 struct ich_spi_platdata *plat = dev_get_platdata(dev);
Simon Glasse160f7d2017-01-17 16:52:55 -0700670 int node = dev_of_offset(dev);
Bin Meng1f9eb592016-02-01 01:40:37 -0800671 int ret;
672
Simon Glasse160f7d2017-01-17 16:52:55 -0700673 ret = fdt_node_check_compatible(gd->fdt_blob, node, "intel,ich7-spi");
Bin Meng1f9eb592016-02-01 01:40:37 -0800674 if (ret == 0) {
Bin Meng6e670b52016-02-01 01:40:38 -0800675 plat->ich_version = ICHV_7;
Bin Meng1f9eb592016-02-01 01:40:37 -0800676 } else {
Simon Glasse160f7d2017-01-17 16:52:55 -0700677 ret = fdt_node_check_compatible(gd->fdt_blob, node,
Bin Meng1f9eb592016-02-01 01:40:37 -0800678 "intel,ich9-spi");
679 if (ret == 0)
Bin Meng6e670b52016-02-01 01:40:38 -0800680 plat->ich_version = ICHV_9;
Bin Meng1f9eb592016-02-01 01:40:37 -0800681 }
682
Bin Mengab201072017-10-18 18:20:57 -0700683 plat->lockdown = fdtdec_get_bool(gd->fdt_blob, node,
684 "intel,spi-lock-down");
685
Bin Meng1f9eb592016-02-01 01:40:37 -0800686 return ret;
687}
688
Simon Glassba457562015-03-26 09:29:26 -0600689static 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
699static const struct udevice_id ich_spi_ids[] = {
Bin Meng1f9eb592016-02-01 01:40:37 -0800700 { .compatible = "intel,ich7-spi" },
701 { .compatible = "intel,ich9-spi" },
Simon Glassba457562015-03-26 09:29:26 -0600702 { }
703};
704
705U_BOOT_DRIVER(ich_spi) = {
706 .name = "ich_spi",
707 .id = UCLASS_SPI,
708 .of_match = ich_spi_ids,
709 .ops = &ich_spi_ops,
Bin Meng1f9eb592016-02-01 01:40:37 -0800710 .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
Simon Glassba457562015-03-26 09:29:26 -0600711 .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
712 .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
713 .child_pre_probe = ich_spi_child_pre_probe,
714 .probe = ich_spi_probe,
Stefan Roese4759dff2017-04-24 09:48:04 +0200715 .remove = ich_spi_remove,
716 .flags = DM_FLAG_OS_PREPARE,
Simon Glassba457562015-03-26 09:29:26 -0600717};