]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/spi/ich.c
x86: spi: Support ValleyView in ICH SPI driver
[karo-tx-uboot.git] / drivers / spi / ich.c
1 /*
2  * Copyright (c) 2011-12 The Chromium OS Authors.
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  *
6  * This file is derived from the flashrom project.
7  */
8
9 #include <common.h>
10 #include <errno.h>
11 #include <malloc.h>
12 #include <spi.h>
13 #include <pci.h>
14 #include <pci_ids.h>
15 #include <asm/io.h>
16
17 #include "ich.h"
18
19 #define SPI_OPCODE_WREN      0x06
20 #define SPI_OPCODE_FAST_READ 0x0b
21
22 struct ich_ctlr {
23         pci_dev_t dev;          /* PCI device number */
24         int ich_version;        /* Controller version, 7 or 9 */
25         bool use_sbase;         /* Use SBASE instead of RCB */
26         int ichspi_lock;
27         int locked;
28         uint8_t *opmenu;
29         int menubytes;
30         void *base;             /* Base of register set */
31         uint16_t *preop;
32         uint16_t *optype;
33         uint32_t *addr;
34         uint8_t *data;
35         unsigned databytes;
36         uint8_t *status;
37         uint16_t *control;
38         uint32_t *bbar;
39         uint32_t *pr;           /* only for ich9 */
40         uint8_t *speed;         /* pointer to speed control */
41         ulong max_speed;        /* Maximum bus speed in MHz */
42 };
43
44 struct ich_ctlr ctlr;
45
46 static inline struct ich_spi_slave *to_ich_spi(struct spi_slave *slave)
47 {
48         return container_of(slave, struct ich_spi_slave, slave);
49 }
50
51 static unsigned int ich_reg(const void *addr)
52 {
53         return (unsigned)(addr - ctlr.base) & 0xffff;
54 }
55
56 static u8 ich_readb(const void *addr)
57 {
58         u8 value = readb(addr);
59
60         debug("read %2.2x from %4.4x\n", value, ich_reg(addr));
61
62         return value;
63 }
64
65 static u16 ich_readw(const void *addr)
66 {
67         u16 value = readw(addr);
68
69         debug("read %4.4x from %4.4x\n", value, ich_reg(addr));
70
71         return value;
72 }
73
74 static u32 ich_readl(const void *addr)
75 {
76         u32 value = readl(addr);
77
78         debug("read %8.8x from %4.4x\n", value, ich_reg(addr));
79
80         return value;
81 }
82
83 static void ich_writeb(u8 value, void *addr)
84 {
85         writeb(value, addr);
86         debug("wrote %2.2x to %4.4x\n", value, ich_reg(addr));
87 }
88
89 static void ich_writew(u16 value, void *addr)
90 {
91         writew(value, addr);
92         debug("wrote %4.4x to %4.4x\n", value, ich_reg(addr));
93 }
94
95 static void ich_writel(u32 value, void *addr)
96 {
97         writel(value, addr);
98         debug("wrote %8.8x to %4.4x\n", value, ich_reg(addr));
99 }
100
101 static void write_reg(const void *value, void *dest, uint32_t size)
102 {
103         memcpy_toio(dest, value, size);
104 }
105
106 static void read_reg(const void *src, void *value, uint32_t size)
107 {
108         memcpy_fromio(value, src, size);
109 }
110
111 static void ich_set_bbar(struct ich_ctlr *ctlr, uint32_t minaddr)
112 {
113         const uint32_t bbar_mask = 0x00ffff00;
114         uint32_t ichspi_bbar;
115
116         minaddr &= bbar_mask;
117         ichspi_bbar = ich_readl(ctlr->bbar) & ~bbar_mask;
118         ichspi_bbar |= minaddr;
119         ich_writel(ichspi_bbar, ctlr->bbar);
120 }
121
122 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
123 {
124         puts("spi_cs_is_valid used but not implemented\n");
125         return 0;
126 }
127
128 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
129                 unsigned int max_hz, unsigned int mode)
130 {
131         struct ich_spi_slave *ich;
132
133         ich = spi_alloc_slave(struct ich_spi_slave, bus, cs);
134         if (!ich) {
135                 puts("ICH SPI: Out of memory\n");
136                 return NULL;
137         }
138
139         /*
140          * Yes this controller can only write a small number of bytes at
141          * once! The limit is typically 64 bytes.
142          */
143         ich->slave.max_write_size = ctlr.databytes;
144         ich->speed = max_hz;
145
146         /*
147          * ICH 7 SPI controller only supports array read command
148          * and byte program command for SST flash
149          */
150         if (ctlr.ich_version == 7 || ctlr.use_sbase) {
151                 ich->slave.op_mode_rx = SPI_OPM_RX_AS;
152                 ich->slave.op_mode_tx = SPI_OPM_TX_BP;
153         }
154
155         return &ich->slave;
156 }
157
158 struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
159                                       int spi_node)
160 {
161         /* We only support a single SPI at present */
162         return spi_setup_slave(0, 0, 20000000, 0);
163 }
164
165 void spi_free_slave(struct spi_slave *slave)
166 {
167         struct ich_spi_slave *ich = to_ich_spi(slave);
168
169         free(ich);
170 }
171
172 /*
173  * Check if this device ID matches one of supported Intel PCH devices.
174  *
175  * Return the ICH version if there is a match, or zero otherwise.
176  */
177 static int get_ich_version(uint16_t device_id)
178 {
179         if (device_id == PCI_DEVICE_ID_INTEL_TGP_LPC ||
180             device_id == PCI_DEVICE_ID_INTEL_ITC_LPC)
181                 return 7;
182
183         if ((device_id >= PCI_DEVICE_ID_INTEL_COUGARPOINT_LPC_MIN &&
184              device_id <= PCI_DEVICE_ID_INTEL_COUGARPOINT_LPC_MAX) ||
185             (device_id >= PCI_DEVICE_ID_INTEL_PANTHERPOINT_LPC_MIN &&
186              device_id <= PCI_DEVICE_ID_INTEL_PANTHERPOINT_LPC_MAX) ||
187             device_id == PCI_DEVICE_ID_INTEL_VALLEYVIEW_LPC)
188                 return 9;
189
190         return 0;
191 }
192
193 /* @return 1 if the SPI flash supports the 33MHz speed */
194 static int ich9_can_do_33mhz(pci_dev_t dev)
195 {
196         u32 fdod, speed;
197
198         /* Observe SPI Descriptor Component Section 0 */
199         pci_write_config_dword(dev, 0xb0, 0x1000);
200
201         /* Extract the Write/Erase SPI Frequency from descriptor */
202         pci_read_config_dword(dev, 0xb4, &fdod);
203
204         /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
205         speed = (fdod >> 21) & 7;
206
207         return speed == 1;
208 }
209
210 static int ich_find_spi_controller(struct ich_ctlr *ich)
211 {
212         int last_bus = pci_last_busno();
213         int bus;
214
215         if (last_bus == -1) {
216                 debug("No PCI busses?\n");
217                 return -ENODEV;
218         }
219
220         for (bus = 0; bus <= last_bus; bus++) {
221                 uint16_t vendor_id, device_id;
222                 uint32_t ids;
223                 pci_dev_t dev;
224
225                 dev = PCI_BDF(bus, 31, 0);
226                 pci_read_config_dword(dev, 0, &ids);
227                 vendor_id = ids;
228                 device_id = ids >> 16;
229
230                 if (vendor_id == PCI_VENDOR_ID_INTEL) {
231                         ich->dev = dev;
232                         ich->ich_version = get_ich_version(device_id);
233                         if (device_id == PCI_DEVICE_ID_INTEL_VALLEYVIEW_LPC)
234                                 ich->use_sbase = true;
235                         return ich->ich_version == 0 ? -ENODEV : 0;
236                 }
237         }
238
239         debug("ICH SPI: No ICH found.\n");
240         return -ENODEV;
241 }
242
243 static int ich_init_controller(struct ich_ctlr *ctlr)
244 {
245         uint8_t *rcrb; /* Root Complex Register Block */
246         uint32_t rcba; /* Root Complex Base Address */
247         uint32_t sbase_addr;
248         uint8_t *sbase;
249
250         pci_read_config_dword(ctlr->dev, 0xf0, &rcba);
251         /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
252         rcrb = (uint8_t *)(rcba & 0xffffc000);
253
254         /* SBASE is similar */
255         pci_read_config_dword(ctlr->dev, 0x54, &sbase_addr);
256         sbase = (uint8_t *)(sbase_addr & 0xfffffe00);
257
258         if (ctlr->ich_version == 7) {
259                 struct ich7_spi_regs *ich7_spi;
260
261                 ich7_spi = (struct ich7_spi_regs *)(rcrb + 0x3020);
262                 ctlr->ichspi_lock = ich_readw(&ich7_spi->spis) & SPIS_LOCK;
263                 ctlr->opmenu = ich7_spi->opmenu;
264                 ctlr->menubytes = sizeof(ich7_spi->opmenu);
265                 ctlr->optype = &ich7_spi->optype;
266                 ctlr->addr = &ich7_spi->spia;
267                 ctlr->data = (uint8_t *)ich7_spi->spid;
268                 ctlr->databytes = sizeof(ich7_spi->spid);
269                 ctlr->status = (uint8_t *)&ich7_spi->spis;
270                 ctlr->control = &ich7_spi->spic;
271                 ctlr->bbar = &ich7_spi->bbar;
272                 ctlr->preop = &ich7_spi->preop;
273                 ctlr->base = ich7_spi;
274         } else if (ctlr->ich_version == 9) {
275                 struct ich9_spi_regs *ich9_spi;
276
277                 if (ctlr->use_sbase)
278                         ich9_spi = (struct ich9_spi_regs *)sbase;
279                 else
280                         ich9_spi = (struct ich9_spi_regs *)(rcrb + 0x3800);
281                 ctlr->ichspi_lock = ich_readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
282                 ctlr->opmenu = ich9_spi->opmenu;
283                 ctlr->menubytes = sizeof(ich9_spi->opmenu);
284                 ctlr->optype = &ich9_spi->optype;
285                 ctlr->addr = &ich9_spi->faddr;
286                 ctlr->data = (uint8_t *)ich9_spi->fdata;
287                 ctlr->databytes = sizeof(ich9_spi->fdata);
288                 ctlr->status = &ich9_spi->ssfs;
289                 ctlr->control = (uint16_t *)ich9_spi->ssfc;
290                 ctlr->speed = ich9_spi->ssfc + 2;
291                 ctlr->bbar = &ich9_spi->bbar;
292                 ctlr->preop = &ich9_spi->preop;
293                 ctlr->pr = &ich9_spi->pr[0];
294                 ctlr->base = ich9_spi;
295         } else {
296                 debug("ICH SPI: Unrecognized ICH version %d.\n",
297                       ctlr->ich_version);
298                 return -1;
299         }
300
301         /* Work out the maximum speed we can support */
302         ctlr->max_speed = 20000000;
303         if (ctlr->ich_version == 9 && ich9_can_do_33mhz(ctlr->dev))
304                 ctlr->max_speed = 33000000;
305         debug("ICH SPI: Version %d detected at %p, speed %ld\n",
306               ctlr->ich_version, ctlr->base, ctlr->max_speed);
307
308         ich_set_bbar(ctlr, 0);
309
310         return 0;
311 }
312
313 void spi_init(void)
314 {
315         uint8_t bios_cntl;
316
317         if (ich_find_spi_controller(&ctlr)) {
318                 printf("ICH SPI: Cannot find device\n");
319                 return;
320         }
321
322         if (ich_init_controller(&ctlr)) {
323                 printf("ICH SPI: Cannot setup controller\n");
324                 return;
325         }
326
327         /*
328          * Disable the BIOS write protect so write commands are allowed.  On
329          * v9, deassert SMM BIOS Write Protect Disable.
330          */
331         if (ctlr.use_sbase) {
332                 struct ich9_spi_regs *ich9_spi;
333
334                 ich9_spi = (struct ich9_spi_regs *)ctlr.base;
335                 bios_cntl = ich_readb(&ich9_spi->bcr);
336                 bios_cntl &= ~(1 << 5); /* clear Enable InSMM_STS (EISS) */
337                 bios_cntl |= 1;         /* Write Protect Disable (WPD) */
338                 ich_writeb(bios_cntl, &ich9_spi->bcr);
339         } else {
340                 pci_read_config_byte(ctlr.dev, 0xdc, &bios_cntl);
341                 if (ctlr.ich_version == 9)
342                         bios_cntl &= ~(1 << 5);
343                 pci_write_config_byte(ctlr.dev, 0xdc, bios_cntl | 0x1);
344         }
345 }
346
347 int spi_claim_bus(struct spi_slave *slave)
348 {
349         /* Handled by ICH automatically. */
350         return 0;
351 }
352
353 void spi_release_bus(struct spi_slave *slave)
354 {
355         /* Handled by ICH automatically. */
356 }
357
358 void spi_cs_activate(struct spi_slave *slave)
359 {
360         /* Handled by ICH automatically. */
361 }
362
363 void spi_cs_deactivate(struct spi_slave *slave)
364 {
365         /* Handled by ICH automatically. */
366 }
367
368 static inline void spi_use_out(struct spi_trans *trans, unsigned bytes)
369 {
370         trans->out += bytes;
371         trans->bytesout -= bytes;
372 }
373
374 static inline void spi_use_in(struct spi_trans *trans, unsigned bytes)
375 {
376         trans->in += bytes;
377         trans->bytesin -= bytes;
378 }
379
380 static void spi_setup_type(struct spi_trans *trans, int data_bytes)
381 {
382         trans->type = 0xFF;
383
384         /* Try to guess spi type from read/write sizes. */
385         if (trans->bytesin == 0) {
386                 if (trans->bytesout + data_bytes > 4)
387                         /*
388                          * If bytesin = 0 and bytesout > 4, we presume this is
389                          * a write data operation, which is accompanied by an
390                          * address.
391                          */
392                         trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
393                 else
394                         trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
395                 return;
396         }
397
398         if (trans->bytesout == 1) {     /* and bytesin is > 0 */
399                 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
400                 return;
401         }
402
403         if (trans->bytesout == 4)       /* and bytesin is > 0 */
404                 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
405
406         /* Fast read command is called with 5 bytes instead of 4 */
407         if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
408                 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
409                 --trans->bytesout;
410         }
411 }
412
413 static int spi_setup_opcode(struct spi_trans *trans)
414 {
415         uint16_t optypes;
416         uint8_t opmenu[ctlr.menubytes];
417
418         trans->opcode = trans->out[0];
419         spi_use_out(trans, 1);
420         if (!ctlr.ichspi_lock) {
421                 /* The lock is off, so just use index 0. */
422                 ich_writeb(trans->opcode, ctlr.opmenu);
423                 optypes = ich_readw(ctlr.optype);
424                 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
425                 ich_writew(optypes, ctlr.optype);
426                 return 0;
427         } else {
428                 /* The lock is on. See if what we need is on the menu. */
429                 uint8_t optype;
430                 uint16_t opcode_index;
431
432                 /* Write Enable is handled as atomic prefix */
433                 if (trans->opcode == SPI_OPCODE_WREN)
434                         return 0;
435
436                 read_reg(ctlr.opmenu, opmenu, sizeof(opmenu));
437                 for (opcode_index = 0; opcode_index < ctlr.menubytes;
438                                 opcode_index++) {
439                         if (opmenu[opcode_index] == trans->opcode)
440                                 break;
441                 }
442
443                 if (opcode_index == ctlr.menubytes) {
444                         printf("ICH SPI: Opcode %x not found\n",
445                                trans->opcode);
446                         return -1;
447                 }
448
449                 optypes = ich_readw(ctlr.optype);
450                 optype = (optypes >> (opcode_index * 2)) & 0x3;
451                 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
452                     optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
453                     trans->bytesout >= 3) {
454                         /* We guessed wrong earlier. Fix it up. */
455                         trans->type = optype;
456                 }
457                 if (optype != trans->type) {
458                         printf("ICH SPI: Transaction doesn't fit type %d\n",
459                                optype);
460                         return -1;
461                 }
462                 return opcode_index;
463         }
464 }
465
466 static int spi_setup_offset(struct spi_trans *trans)
467 {
468         /* Separate the SPI address and data. */
469         switch (trans->type) {
470         case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
471         case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
472                 return 0;
473         case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
474         case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
475                 trans->offset = ((uint32_t)trans->out[0] << 16) |
476                                 ((uint32_t)trans->out[1] << 8) |
477                                 ((uint32_t)trans->out[2] << 0);
478                 spi_use_out(trans, 3);
479                 return 1;
480         default:
481                 printf("Unrecognized SPI transaction type %#x\n", trans->type);
482                 return -1;
483         }
484 }
485
486 /*
487  * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
488  * below is true) or 0. In case the wait was for the bit(s) to set - write
489  * those bits back, which would cause resetting them.
490  *
491  * Return the last read status value on success or -1 on failure.
492  */
493 static int ich_status_poll(u16 bitmask, int wait_til_set)
494 {
495         int timeout = 600000; /* This will result in 6s */
496         u16 status = 0;
497
498         while (timeout--) {
499                 status = ich_readw(ctlr.status);
500                 if (wait_til_set ^ ((status & bitmask) == 0)) {
501                         if (wait_til_set)
502                                 ich_writew((status & bitmask), ctlr.status);
503                         return status;
504                 }
505                 udelay(10);
506         }
507
508         printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
509                status, bitmask);
510         return -1;
511 }
512
513 /*
514 int spi_xfer(struct spi_slave *slave, const void *dout,
515                 unsigned int bitsout, void *din, unsigned int bitsin)
516 */
517 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
518                 void *din, unsigned long flags)
519 {
520         struct ich_spi_slave *ich = to_ich_spi(slave);
521         uint16_t control;
522         int16_t opcode_index;
523         int with_address;
524         int status;
525         int bytes = bitlen / 8;
526         struct spi_trans *trans = &ich->trans;
527         unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
528         int using_cmd = 0;
529
530         /* Ee don't support writing partial bytes. */
531         if (bitlen % 8) {
532                 debug("ICH SPI: Accessing partial bytes not supported\n");
533                 return -1;
534         }
535
536         /* An empty end transaction can be ignored */
537         if (type == SPI_XFER_END && !dout && !din)
538                 return 0;
539
540         if (type & SPI_XFER_BEGIN)
541                 memset(trans, '\0', sizeof(*trans));
542
543         /* Dp we need to come back later to finish it? */
544         if (dout && type == SPI_XFER_BEGIN) {
545                 if (bytes > ICH_MAX_CMD_LEN) {
546                         debug("ICH SPI: Command length limit exceeded\n");
547                         return -1;
548                 }
549                 memcpy(trans->cmd, dout, bytes);
550                 trans->cmd_len = bytes;
551                 debug("ICH SPI: Saved %d bytes\n", bytes);
552                 return 0;
553         }
554
555         /*
556          * We process a 'middle' spi_xfer() call, which has no
557          * SPI_XFER_BEGIN/END, as an independent transaction as if it had
558          * an end. We therefore repeat the command. This is because ICH
559          * seems to have no support for this, or because interest (in digging
560          * out the details and creating a special case in the code) is low.
561          */
562         if (trans->cmd_len) {
563                 trans->out = trans->cmd;
564                 trans->bytesout = trans->cmd_len;
565                 using_cmd = 1;
566                 debug("ICH SPI: Using %d bytes\n", trans->cmd_len);
567         } else {
568                 trans->out = dout;
569                 trans->bytesout = dout ? bytes : 0;
570         }
571
572         trans->in = din;
573         trans->bytesin = din ? bytes : 0;
574
575         /* There has to always at least be an opcode. */
576         if (!trans->bytesout) {
577                 debug("ICH SPI: No opcode for transfer\n");
578                 return -1;
579         }
580
581         if (ich_status_poll(SPIS_SCIP, 0) == -1)
582                 return -1;
583
584         ich_writew(SPIS_CDS | SPIS_FCERR, ctlr.status);
585
586         spi_setup_type(trans, using_cmd ? bytes : 0);
587         opcode_index = spi_setup_opcode(trans);
588         if (opcode_index < 0)
589                 return -1;
590         with_address = spi_setup_offset(trans);
591         if (with_address < 0)
592                 return -1;
593
594         if (trans->opcode == SPI_OPCODE_WREN) {
595                 /*
596                  * Treat Write Enable as Atomic Pre-Op if possible
597                  * in order to prevent the Management Engine from
598                  * issuing a transaction between WREN and DATA.
599                  */
600                 if (!ctlr.ichspi_lock)
601                         ich_writew(trans->opcode, ctlr.preop);
602                 return 0;
603         }
604
605         if (ctlr.speed && ctlr.max_speed >= 33000000) {
606                 int byte;
607
608                 byte = ich_readb(ctlr.speed);
609                 if (ich->speed >= 33000000)
610                         byte |= SSFC_SCF_33MHZ;
611                 else
612                         byte &= ~SSFC_SCF_33MHZ;
613                 ich_writeb(byte, ctlr.speed);
614         }
615
616         /* See if we have used up the command data */
617         if (using_cmd && dout && bytes) {
618                 trans->out = dout;
619                 trans->bytesout = bytes;
620                 debug("ICH SPI: Moving to data, %d bytes\n", bytes);
621         }
622
623         /* Preset control fields */
624         control = ich_readw(ctlr.control);
625         control &= ~SSFC_RESERVED;
626         control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
627
628         /* Issue atomic preop cycle if needed */
629         if (ich_readw(ctlr.preop))
630                 control |= SPIC_ACS;
631
632         if (!trans->bytesout && !trans->bytesin) {
633                 /* SPI addresses are 24 bit only */
634                 if (with_address)
635                         ich_writel(trans->offset & 0x00FFFFFF, ctlr.addr);
636
637                 /*
638                  * This is a 'no data' command (like Write Enable), its
639                  * bitesout size was 1, decremented to zero while executing
640                  * spi_setup_opcode() above. Tell the chip to send the
641                  * command.
642                  */
643                 ich_writew(control, ctlr.control);
644
645                 /* wait for the result */
646                 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
647                 if (status == -1)
648                         return -1;
649
650                 if (status & SPIS_FCERR) {
651                         debug("ICH SPI: Command transaction error\n");
652                         return -1;
653                 }
654
655                 return 0;
656         }
657
658         /*
659          * Check if this is a write command atempting to transfer more bytes
660          * than the controller can handle. Iterations for writes are not
661          * supported here because each SPI write command needs to be preceded
662          * and followed by other SPI commands, and this sequence is controlled
663          * by the SPI chip driver.
664          */
665         if (trans->bytesout > ctlr.databytes) {
666                 debug("ICH SPI: Too much to write. This should be prevented by the driver's max_write_size?\n");
667                 return -1;
668         }
669
670         /*
671          * Read or write up to databytes bytes at a time until everything has
672          * been sent.
673          */
674         while (trans->bytesout || trans->bytesin) {
675                 uint32_t data_length;
676
677                 /* SPI addresses are 24 bit only */
678                 ich_writel(trans->offset & 0x00FFFFFF, ctlr.addr);
679
680                 if (trans->bytesout)
681                         data_length = min(trans->bytesout, ctlr.databytes);
682                 else
683                         data_length = min(trans->bytesin, ctlr.databytes);
684
685                 /* Program data into FDATA0 to N */
686                 if (trans->bytesout) {
687                         write_reg(trans->out, ctlr.data, data_length);
688                         spi_use_out(trans, data_length);
689                         if (with_address)
690                                 trans->offset += data_length;
691                 }
692
693                 /* Add proper control fields' values */
694                 control &= ~((ctlr.databytes - 1) << 8);
695                 control |= SPIC_DS;
696                 control |= (data_length - 1) << 8;
697
698                 /* write it */
699                 ich_writew(control, ctlr.control);
700
701                 /* Wait for Cycle Done Status or Flash Cycle Error. */
702                 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
703                 if (status == -1)
704                         return -1;
705
706                 if (status & SPIS_FCERR) {
707                         debug("ICH SPI: Data transaction error\n");
708                         return -1;
709                 }
710
711                 if (trans->bytesin) {
712                         read_reg(ctlr.data, trans->in, data_length);
713                         spi_use_in(trans, data_length);
714                         if (with_address)
715                                 trans->offset += data_length;
716                 }
717         }
718
719         /* Clear atomic preop now that xfer is done */
720         ich_writew(0, ctlr.preop);
721
722         return 0;
723 }
724
725
726 /*
727  * This uses the SPI controller from the Intel Cougar Point and Panther Point
728  * PCH to write-protect portions of the SPI flash until reboot. The changes
729  * don't actually take effect until the HSFS[FLOCKDN] bit is set, but that's
730  * done elsewhere.
731  */
732 int spi_write_protect_region(uint32_t lower_limit, uint32_t length, int hint)
733 {
734         uint32_t tmplong;
735         uint32_t upper_limit;
736
737         if (!ctlr.pr) {
738                 printf("%s: operation not supported on this chipset\n",
739                        __func__);
740                 return -1;
741         }
742
743         if (length == 0 ||
744             lower_limit > (0xFFFFFFFFUL - length) + 1 ||
745             hint < 0 || hint > 4) {
746                 printf("%s(0x%x, 0x%x, %d): invalid args\n", __func__,
747                        lower_limit, length, hint);
748                 return -1;
749         }
750
751         upper_limit = lower_limit + length - 1;
752
753         /*
754          * Determine bits to write, as follows:
755          *  31     Write-protection enable (includes erase operation)
756          *  30:29  reserved
757          *  28:16  Upper Limit (FLA address bits 24:12, with 11:0 == 0xfff)
758          *  15     Read-protection enable
759          *  14:13  reserved
760          *  12:0   Lower Limit (FLA address bits 24:12, with 11:0 == 0x000)
761          */
762         tmplong = 0x80000000 |
763                 ((upper_limit & 0x01fff000) << 4) |
764                 ((lower_limit & 0x01fff000) >> 12);
765
766         printf("%s: writing 0x%08x to %p\n", __func__, tmplong,
767                &ctlr.pr[hint]);
768         ctlr.pr[hint] = tmplong;
769
770         return 0;
771 }