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