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