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