spi: ich: Correct max-size bug in ich_spi_adjust_size()
[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 <div64.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <malloc.h>
15 #include <pch.h>
16 #include <pci.h>
17 #include <pci_ids.h>
18 #include <spi.h>
19 #include <spi-mem.h>
20 #include <asm/io.h>
21
22 #include "ich.h"
23
24 #ifdef DEBUG_TRACE
25 #define debug_trace(fmt, args...) debug(fmt, ##args)
26 #else
27 #define debug_trace(x, args...)
28 #endif
29
30 struct ich_spi_platdata {
31         enum ich_version ich_version;   /* Controller version, 7 or 9 */
32         bool lockdown;                  /* lock down controller settings? */
33         ulong mmio_base;                /* Base of MMIO registers */
34 };
35
36 static u8 ich_readb(struct ich_spi_priv *priv, int reg)
37 {
38         u8 value = readb(priv->base + reg);
39
40         debug_trace("read %2.2x from %4.4x\n", value, reg);
41
42         return value;
43 }
44
45 static u16 ich_readw(struct ich_spi_priv *priv, int reg)
46 {
47         u16 value = readw(priv->base + reg);
48
49         debug_trace("read %4.4x from %4.4x\n", value, reg);
50
51         return value;
52 }
53
54 static u32 ich_readl(struct ich_spi_priv *priv, int reg)
55 {
56         u32 value = readl(priv->base + reg);
57
58         debug_trace("read %8.8x from %4.4x\n", value, reg);
59
60         return value;
61 }
62
63 static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
64 {
65         writeb(value, priv->base + reg);
66         debug_trace("wrote %2.2x to %4.4x\n", value, reg);
67 }
68
69 static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
70 {
71         writew(value, priv->base + reg);
72         debug_trace("wrote %4.4x to %4.4x\n", value, reg);
73 }
74
75 static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
76 {
77         writel(value, priv->base + reg);
78         debug_trace("wrote %8.8x to %4.4x\n", value, reg);
79 }
80
81 static void write_reg(struct ich_spi_priv *priv, const void *value,
82                       int dest_reg, uint32_t size)
83 {
84         memcpy_toio(priv->base + dest_reg, value, size);
85 }
86
87 static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
88                      uint32_t size)
89 {
90         memcpy_fromio(value, priv->base + src_reg, size);
91 }
92
93 static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
94 {
95         const uint32_t bbar_mask = 0x00ffff00;
96         uint32_t ichspi_bbar;
97
98         minaddr &= bbar_mask;
99         ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
100         ichspi_bbar |= minaddr;
101         ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
102 }
103
104 /* @return 1 if the SPI flash supports the 33MHz speed */
105 static bool ich9_can_do_33mhz(struct udevice *dev)
106 {
107         struct ich_spi_priv *priv = dev_get_priv(dev);
108         u32 fdod, speed;
109
110         /* Observe SPI Descriptor Component Section 0 */
111         dm_pci_write_config32(priv->pch, 0xb0, 0x1000);
112
113         /* Extract the Write/Erase SPI Frequency from descriptor */
114         dm_pci_read_config32(priv->pch, 0xb4, &fdod);
115
116         /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
117         speed = (fdod >> 21) & 7;
118
119         return speed == 1;
120 }
121
122 static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
123 {
124         if (plat->ich_version == ICHV_7) {
125                 struct ich7_spi_regs *ich7_spi = sbase;
126
127                 setbits_le16(&ich7_spi->spis, SPIS_LOCK);
128         } else if (plat->ich_version == ICHV_9) {
129                 struct ich9_spi_regs *ich9_spi = sbase;
130
131                 setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
132         }
133 }
134
135 static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
136 {
137         int lock = 0;
138
139         if (plat->ich_version == ICHV_7) {
140                 struct ich7_spi_regs *ich7_spi = sbase;
141
142                 lock = readw(&ich7_spi->spis) & SPIS_LOCK;
143         } else if (plat->ich_version == ICHV_9) {
144                 struct ich9_spi_regs *ich9_spi = sbase;
145
146                 lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
147         }
148
149         return lock != 0;
150 }
151
152 static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
153                             bool lock)
154 {
155         uint16_t optypes;
156         uint8_t opmenu[ctlr->menubytes];
157
158         if (!lock) {
159                 /* The lock is off, so just use index 0. */
160                 ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
161                 optypes = ich_readw(ctlr, ctlr->optype);
162                 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
163                 ich_writew(ctlr, optypes, ctlr->optype);
164                 return 0;
165         } else {
166                 /* The lock is on. See if what we need is on the menu. */
167                 uint8_t optype;
168                 uint16_t opcode_index;
169
170                 /* Write Enable is handled as atomic prefix */
171                 if (trans->opcode == SPI_OPCODE_WREN)
172                         return 0;
173
174                 read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
175                 for (opcode_index = 0; opcode_index < ctlr->menubytes;
176                                 opcode_index++) {
177                         if (opmenu[opcode_index] == trans->opcode)
178                                 break;
179                 }
180
181                 if (opcode_index == ctlr->menubytes) {
182                         debug("ICH SPI: Opcode %x not found\n", trans->opcode);
183                         return -EINVAL;
184                 }
185
186                 optypes = ich_readw(ctlr, ctlr->optype);
187                 optype = (optypes >> (opcode_index * 2)) & 0x3;
188
189                 if (optype != trans->type) {
190                         debug("ICH SPI: Transaction doesn't fit type %d\n",
191                               optype);
192                         return -ENOSPC;
193                 }
194                 return opcode_index;
195         }
196 }
197
198 /*
199  * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
200  * below is true) or 0. In case the wait was for the bit(s) to set - write
201  * those bits back, which would cause resetting them.
202  *
203  * Return the last read status value on success or -1 on failure.
204  */
205 static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
206                            int wait_til_set)
207 {
208         int timeout = 600000; /* This will result in 6s */
209         u16 status = 0;
210
211         while (timeout--) {
212                 status = ich_readw(ctlr, ctlr->status);
213                 if (wait_til_set ^ ((status & bitmask) == 0)) {
214                         if (wait_til_set) {
215                                 ich_writew(ctlr, status & bitmask,
216                                            ctlr->status);
217                         }
218                         return status;
219                 }
220                 udelay(10);
221         }
222         debug("ICH SPI: SCIP timeout, read %x, expected %x, wts %x %x\n",
223               status, bitmask, wait_til_set, status & bitmask);
224
225         return -ETIMEDOUT;
226 }
227
228 static void ich_spi_config_opcode(struct udevice *dev)
229 {
230         struct ich_spi_priv *ctlr = dev_get_priv(dev);
231
232         /*
233          * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
234          * to prevent accidental or intentional writes. Before they get
235          * locked down, these registers should be initialized properly.
236          */
237         ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
238         ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
239         ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
240         ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
241 }
242
243 static int ich_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
244 {
245         struct udevice *bus = dev_get_parent(slave->dev);
246         struct ich_spi_platdata *plat = dev_get_platdata(bus);
247         struct ich_spi_priv *ctlr = dev_get_priv(bus);
248         uint16_t control;
249         int16_t opcode_index;
250         int with_address;
251         int status;
252         struct spi_trans *trans = &ctlr->trans;
253         bool lock = spi_lock_status(plat, ctlr->base);
254         int ret = 0;
255
256         trans->in = NULL;
257         trans->out = NULL;
258         trans->type = 0xFF;
259
260         if (op->data.nbytes) {
261                 if (op->data.dir == SPI_MEM_DATA_IN) {
262                         trans->in = op->data.buf.in;
263                         trans->bytesin = op->data.nbytes;
264                 } else {
265                         trans->out = op->data.buf.out;
266                         trans->bytesout = op->data.nbytes;
267                 }
268         }
269
270         if (trans->opcode != op->cmd.opcode)
271                 trans->opcode = op->cmd.opcode;
272
273         if (lock && trans->opcode == SPI_OPCODE_WRDIS)
274                 return 0;
275
276         if (trans->opcode == SPI_OPCODE_WREN) {
277                 /*
278                  * Treat Write Enable as Atomic Pre-Op if possible
279                  * in order to prevent the Management Engine from
280                  * issuing a transaction between WREN and DATA.
281                  */
282                 if (!lock)
283                         ich_writew(ctlr, trans->opcode, ctlr->preop);
284                 return 0;
285         }
286
287         ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
288         if (ret < 0)
289                 return ret;
290
291         if (plat->ich_version == ICHV_7)
292                 ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
293         else
294                 ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
295
296         /* Try to guess spi transaction type */
297         if (op->data.dir == SPI_MEM_DATA_OUT) {
298                 if (op->addr.nbytes)
299                         trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
300                 else
301                         trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
302         } else {
303                 if (op->addr.nbytes)
304                         trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
305                 else
306                         trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
307         }
308         /* Special erase case handling */
309         if (op->addr.nbytes && !op->data.buswidth)
310                 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
311
312         opcode_index = spi_setup_opcode(ctlr, trans, lock);
313         if (opcode_index < 0)
314                 return -EINVAL;
315
316         if (op->addr.nbytes) {
317                 trans->offset = op->addr.val;
318                 with_address = 1;
319         }
320
321         if (ctlr->speed && ctlr->max_speed >= 33000000) {
322                 int byte;
323
324                 byte = ich_readb(ctlr, ctlr->speed);
325                 if (ctlr->cur_speed >= 33000000)
326                         byte |= SSFC_SCF_33MHZ;
327                 else
328                         byte &= ~SSFC_SCF_33MHZ;
329                 ich_writeb(ctlr, byte, ctlr->speed);
330         }
331
332         /* Preset control fields */
333         control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
334
335         /* Issue atomic preop cycle if needed */
336         if (ich_readw(ctlr, ctlr->preop))
337                 control |= SPIC_ACS;
338
339         if (!trans->bytesout && !trans->bytesin) {
340                 /* SPI addresses are 24 bit only */
341                 if (with_address) {
342                         ich_writel(ctlr, trans->offset & 0x00FFFFFF,
343                                    ctlr->addr);
344                 }
345                 /*
346                  * This is a 'no data' command (like Write Enable), its
347                  * bitesout size was 1, decremented to zero while executing
348                  * spi_setup_opcode() above. Tell the chip to send the
349                  * command.
350                  */
351                 ich_writew(ctlr, control, ctlr->control);
352
353                 /* wait for the result */
354                 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
355                 if (status < 0)
356                         return status;
357
358                 if (status & SPIS_FCERR) {
359                         debug("ICH SPI: Command transaction error\n");
360                         return -EIO;
361                 }
362
363                 return 0;
364         }
365
366         while (trans->bytesout || trans->bytesin) {
367                 uint32_t data_length;
368
369                 /* SPI addresses are 24 bit only */
370                 ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
371
372                 if (trans->bytesout)
373                         data_length = min(trans->bytesout, ctlr->databytes);
374                 else
375                         data_length = min(trans->bytesin, ctlr->databytes);
376
377                 /* Program data into FDATA0 to N */
378                 if (trans->bytesout) {
379                         write_reg(ctlr, trans->out, ctlr->data, data_length);
380                         trans->bytesout -= data_length;
381                 }
382
383                 /* Add proper control fields' values */
384                 control &= ~((ctlr->databytes - 1) << 8);
385                 control |= SPIC_DS;
386                 control |= (data_length - 1) << 8;
387
388                 /* write it */
389                 ich_writew(ctlr, control, ctlr->control);
390
391                 /* Wait for Cycle Done Status or Flash Cycle Error */
392                 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
393                 if (status < 0)
394                         return status;
395
396                 if (status & SPIS_FCERR) {
397                         debug("ICH SPI: Data transaction error %x\n", status);
398                         return -EIO;
399                 }
400
401                 if (trans->bytesin) {
402                         read_reg(ctlr, ctlr->data, trans->in, data_length);
403                         trans->bytesin -= data_length;
404                 }
405         }
406
407         /* Clear atomic preop now that xfer is done */
408         if (!lock)
409                 ich_writew(ctlr, 0, ctlr->preop);
410
411         return 0;
412 }
413
414 static int ich_spi_adjust_size(struct spi_slave *slave, struct spi_mem_op *op)
415 {
416         unsigned int page_offset;
417         int addr = op->addr.val;
418         unsigned int byte_count = op->data.nbytes;
419
420         if (hweight32(ICH_BOUNDARY) == 1) {
421                 page_offset = addr & (ICH_BOUNDARY - 1);
422         } else {
423                 u64 aux = addr;
424
425                 page_offset = do_div(aux, ICH_BOUNDARY);
426         }
427
428         if (op->data.dir == SPI_MEM_DATA_IN) {
429                 if (slave->max_read_size) {
430                         op->data.nbytes = min(ICH_BOUNDARY - page_offset,
431                                               slave->max_read_size);
432                 }
433         } else if (slave->max_write_size) {
434                 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
435                                       slave->max_write_size);
436         }
437
438         op->data.nbytes = min(op->data.nbytes, byte_count);
439
440         return 0;
441 }
442
443 static int ich_protect_lockdown(struct udevice *dev)
444 {
445         struct ich_spi_platdata *plat = dev_get_platdata(dev);
446         struct ich_spi_priv *priv = dev_get_priv(dev);
447         int ret = -ENOSYS;
448
449         /* Disable the BIOS write protect so write commands are allowed */
450         if (priv->pch)
451                 ret = pch_set_spi_protect(priv->pch, false);
452         if (ret == -ENOSYS) {
453                 u8 bios_cntl;
454
455                 bios_cntl = ich_readb(priv, priv->bcr);
456                 bios_cntl &= ~BIT(5);   /* clear Enable InSMM_STS (EISS) */
457                 bios_cntl |= 1;         /* Write Protect Disable (WPD) */
458                 ich_writeb(priv, bios_cntl, priv->bcr);
459         } else if (ret) {
460                 debug("%s: Failed to disable write-protect: err=%d\n",
461                       __func__, ret);
462                 return ret;
463         }
464
465         /* Lock down SPI controller settings if required */
466         if (plat->lockdown) {
467                 ich_spi_config_opcode(dev);
468                 spi_lock_down(plat, priv->base);
469         }
470
471         return 0;
472 }
473
474 static int ich_init_controller(struct udevice *dev,
475                                struct ich_spi_platdata *plat,
476                                struct ich_spi_priv *ctlr)
477 {
478         ctlr->base = (void *)plat->mmio_base;
479         if (plat->ich_version == ICHV_7) {
480                 struct ich7_spi_regs *ich7_spi = ctlr->base;
481
482                 ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
483                 ctlr->menubytes = sizeof(ich7_spi->opmenu);
484                 ctlr->optype = offsetof(struct ich7_spi_regs, optype);
485                 ctlr->addr = offsetof(struct ich7_spi_regs, spia);
486                 ctlr->data = offsetof(struct ich7_spi_regs, spid);
487                 ctlr->databytes = sizeof(ich7_spi->spid);
488                 ctlr->status = offsetof(struct ich7_spi_regs, spis);
489                 ctlr->control = offsetof(struct ich7_spi_regs, spic);
490                 ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
491                 ctlr->preop = offsetof(struct ich7_spi_regs, preop);
492         } else if (plat->ich_version == ICHV_9) {
493                 struct ich9_spi_regs *ich9_spi = ctlr->base;
494
495                 ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
496                 ctlr->menubytes = sizeof(ich9_spi->opmenu);
497                 ctlr->optype = offsetof(struct ich9_spi_regs, optype);
498                 ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
499                 ctlr->data = offsetof(struct ich9_spi_regs, fdata);
500                 ctlr->databytes = sizeof(ich9_spi->fdata);
501                 ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
502                 ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
503                 ctlr->speed = ctlr->control + 2;
504                 ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
505                 ctlr->preop = offsetof(struct ich9_spi_regs, preop);
506                 ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
507                 ctlr->pr = &ich9_spi->pr[0];
508         } else {
509                 debug("ICH SPI: Unrecognised ICH version %d\n",
510                       plat->ich_version);
511                 return -EINVAL;
512         }
513
514         /* Work out the maximum speed we can support */
515         ctlr->max_speed = 20000000;
516         if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
517                 ctlr->max_speed = 33000000;
518         debug("ICH SPI: Version ID %d detected at %lx, speed %ld\n",
519               plat->ich_version, plat->mmio_base, ctlr->max_speed);
520
521         ich_set_bbar(ctlr, 0);
522
523         return 0;
524 }
525
526 static int ich_spi_probe(struct udevice *dev)
527 {
528         struct ich_spi_platdata *plat = dev_get_platdata(dev);
529         struct ich_spi_priv *priv = dev_get_priv(dev);
530         int ret;
531
532         ret = ich_init_controller(dev, plat, priv);
533         if (ret)
534                 return ret;
535
536         ret = ich_protect_lockdown(dev);
537         if (ret)
538                 return ret;
539
540         priv->cur_speed = priv->max_speed;
541
542         return 0;
543 }
544
545 static int ich_spi_remove(struct udevice *bus)
546 {
547         /*
548          * Configure SPI controller so that the Linux MTD driver can fully
549          * access the SPI NOR chip
550          */
551         ich_spi_config_opcode(bus);
552
553         return 0;
554 }
555
556 static int ich_spi_set_speed(struct udevice *bus, uint speed)
557 {
558         struct ich_spi_priv *priv = dev_get_priv(bus);
559
560         priv->cur_speed = speed;
561
562         return 0;
563 }
564
565 static int ich_spi_set_mode(struct udevice *bus, uint mode)
566 {
567         debug("%s: mode=%d\n", __func__, mode);
568
569         return 0;
570 }
571
572 static int ich_spi_child_pre_probe(struct udevice *dev)
573 {
574         struct udevice *bus = dev_get_parent(dev);
575         struct ich_spi_platdata *plat = dev_get_platdata(bus);
576         struct ich_spi_priv *priv = dev_get_priv(bus);
577         struct spi_slave *slave = dev_get_parent_priv(dev);
578
579         /*
580          * Yes this controller can only write a small number of bytes at
581          * once! The limit is typically 64 bytes.
582          */
583         slave->max_write_size = priv->databytes;
584         /*
585          * ICH 7 SPI controller only supports array read command
586          * and byte program command for SST flash
587          */
588         if (plat->ich_version == ICHV_7)
589                 slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
590
591         return 0;
592 }
593
594 static int ich_spi_ofdata_to_platdata(struct udevice *dev)
595 {
596         struct ich_spi_platdata *plat = dev_get_platdata(dev);
597         struct ich_spi_priv *priv = dev_get_priv(dev);
598
599         /* Find a PCH if there is one */
600         uclass_first_device(UCLASS_PCH, &priv->pch);
601         if (!priv->pch)
602                 priv->pch = dev_get_parent(dev);
603
604         plat->ich_version = dev_get_driver_data(dev);
605         plat->lockdown = dev_read_bool(dev, "intel,spi-lock-down");
606
607         pch_get_spi_base(priv->pch, &plat->mmio_base);
608
609         return 0;
610 }
611
612 static const struct spi_controller_mem_ops ich_controller_mem_ops = {
613         .adjust_op_size = ich_spi_adjust_size,
614         .supports_op    = NULL,
615         .exec_op        = ich_spi_exec_op,
616 };
617
618 static const struct dm_spi_ops ich_spi_ops = {
619         /* xfer is not supported */
620         .set_speed      = ich_spi_set_speed,
621         .set_mode       = ich_spi_set_mode,
622         .mem_ops        = &ich_controller_mem_ops,
623         /*
624          * cs_info is not needed, since we require all chip selects to be
625          * in the device tree explicitly
626          */
627 };
628
629 static const struct udevice_id ich_spi_ids[] = {
630         { .compatible = "intel,ich7-spi", ICHV_7 },
631         { .compatible = "intel,ich9-spi", ICHV_9 },
632         { }
633 };
634
635 U_BOOT_DRIVER(ich_spi) = {
636         .name   = "ich_spi",
637         .id     = UCLASS_SPI,
638         .of_match = ich_spi_ids,
639         .ops    = &ich_spi_ops,
640         .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
641         .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
642         .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
643         .child_pre_probe = ich_spi_child_pre_probe,
644         .probe  = ich_spi_probe,
645         .remove = ich_spi_remove,
646         .flags  = DM_FLAG_OS_PREPARE,
647 };