blob: 7a7eaf71780a8ec5d389c62a11e34c64b6d6daf5 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * Copyright (C) 2014 Google Inc.
3 *
Duncan Lauriec88c54c2014-04-30 16:36:13 -07004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070012 */
13
14/* This file is derived from the flashrom project. */
15#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
18#include <bootstate.h>
19#include <delay.h>
20#include <arch/io.h>
21#include <console/console.h>
22#include <device/pci_ids.h>
Furquan Shaikh52896c62016-11-22 11:43:58 -080023#include <spi_flash.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070024#include <spi-generic.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070025#include <soc/pci_devs.h>
Duncan Laurief059b242015-01-15 15:42:43 -080026#include <soc/rcba.h>
27#include <soc/spi.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070028
Duncan Lauriec88c54c2014-04-30 16:36:13 -070029#ifdef __SMM__
30#define pci_read_config_byte(dev, reg, targ)\
31 *(targ) = pci_read_config8(dev, reg)
32#define pci_read_config_word(dev, reg, targ)\
33 *(targ) = pci_read_config16(dev, reg)
34#define pci_read_config_dword(dev, reg, targ)\
35 *(targ) = pci_read_config32(dev, reg)
36#define pci_write_config_byte(dev, reg, val)\
37 pci_write_config8(dev, reg, val)
38#define pci_write_config_word(dev, reg, val)\
39 pci_write_config16(dev, reg, val)
40#define pci_write_config_dword(dev, reg, val)\
41 pci_write_config32(dev, reg, val)
42#else /* !__SMM__ */
43#include <device/device.h>
44#include <device/pci.h>
45#define pci_read_config_byte(dev, reg, targ)\
46 *(targ) = pci_read_config8(dev, reg)
47#define pci_read_config_word(dev, reg, targ)\
48 *(targ) = pci_read_config16(dev, reg)
49#define pci_read_config_dword(dev, reg, targ)\
50 *(targ) = pci_read_config32(dev, reg)
51#define pci_write_config_byte(dev, reg, val)\
52 pci_write_config8(dev, reg, val)
53#define pci_write_config_word(dev, reg, val)\
54 pci_write_config16(dev, reg, val)
55#define pci_write_config_dword(dev, reg, val)\
56 pci_write_config32(dev, reg, val)
57#endif /* !__SMM__ */
58
59typedef struct spi_slave ich_spi_slave;
60
61static int ichspi_lock = 0;
62
63typedef struct ich9_spi_regs {
64 uint32_t bfpr;
65 uint16_t hsfs;
66 uint16_t hsfc;
67 uint32_t faddr;
68 uint32_t _reserved0;
69 uint32_t fdata[16];
70 uint32_t frap;
71 uint32_t freg[5];
72 uint32_t _reserved1[3];
73 uint32_t pr[5];
74 uint32_t _reserved2[2];
75 uint8_t ssfs;
76 uint8_t ssfc[3];
77 uint16_t preop;
78 uint16_t optype;
79 uint8_t opmenu[8];
80 uint32_t bbar;
81 uint8_t _reserved3[12];
82 uint32_t fdoc;
83 uint32_t fdod;
84 uint8_t _reserved4[8];
85 uint32_t afc;
86 uint32_t lvscc;
87 uint32_t uvscc;
88 uint8_t _reserved5[4];
89 uint32_t fpb;
90 uint8_t _reserved6[28];
91 uint32_t srdl;
92 uint32_t srdc;
93 uint32_t srd;
94} __attribute__((packed)) ich9_spi_regs;
95
96typedef struct ich_spi_controller {
97 int locked;
98
99 uint8_t *opmenu;
100 int menubytes;
101 uint16_t *preop;
102 uint16_t *optype;
103 uint32_t *addr;
104 uint8_t *data;
105 unsigned databytes;
106 uint8_t *status;
107 uint16_t *control;
108 uint32_t *bbar;
109} ich_spi_controller;
110
111static ich_spi_controller cntlr;
112
113enum {
114 SPIS_SCIP = 0x0001,
115 SPIS_GRANT = 0x0002,
116 SPIS_CDS = 0x0004,
117 SPIS_FCERR = 0x0008,
118 SSFS_AEL = 0x0010,
119 SPIS_LOCK = 0x8000,
120 SPIS_RESERVED_MASK = 0x7ff0,
121 SSFS_RESERVED_MASK = 0x7fe2
122};
123
124enum {
125 SPIC_SCGO = 0x000002,
126 SPIC_ACS = 0x000004,
127 SPIC_SPOP = 0x000008,
128 SPIC_DBC = 0x003f00,
129 SPIC_DS = 0x004000,
130 SPIC_SME = 0x008000,
131 SSFC_SCF_MASK = 0x070000,
132 SSFC_RESERVED = 0xf80000
133};
134
135enum {
136 HSFS_FDONE = 0x0001,
137 HSFS_FCERR = 0x0002,
138 HSFS_AEL = 0x0004,
139 HSFS_BERASE_MASK = 0x0018,
140 HSFS_BERASE_SHIFT = 3,
141 HSFS_SCIP = 0x0020,
142 HSFS_FDOPSS = 0x2000,
143 HSFS_FDV = 0x4000,
144 HSFS_FLOCKDN = 0x8000
145};
146
147enum {
148 HSFC_FGO = 0x0001,
149 HSFC_FCYCLE_MASK = 0x0006,
150 HSFC_FCYCLE_SHIFT = 1,
151 HSFC_FDBC_MASK = 0x3f00,
152 HSFC_FDBC_SHIFT = 8,
153 HSFC_FSMIE = 0x8000
154};
155
156enum {
157 SPI_OPCODE_TYPE_READ_NO_ADDRESS = 0,
158 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS = 1,
159 SPI_OPCODE_TYPE_READ_WITH_ADDRESS = 2,
160 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS = 3
161};
162
163#if CONFIG_DEBUG_SPI_FLASH
164
165static u8 readb_(const void *addr)
166{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800167 u8 v = read8(addr);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700168 printk(BIOS_DEBUG, "read %2.2x from %4.4x\n",
169 v, ((unsigned) addr & 0xffff) - 0xf020);
170 return v;
171}
172
173static u16 readw_(const void *addr)
174{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800175 u16 v = read16(addr);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700176 printk(BIOS_DEBUG, "read %4.4x from %4.4x\n",
177 v, ((unsigned) addr & 0xffff) - 0xf020);
178 return v;
179}
180
181static u32 readl_(const void *addr)
182{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800183 u32 v = read32(addr);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700184 printk(BIOS_DEBUG, "read %8.8x from %4.4x\n",
185 v, ((unsigned) addr & 0xffff) - 0xf020);
186 return v;
187}
188
189static void writeb_(u8 b, const void *addr)
190{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800191 write8(addr, b);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700192 printk(BIOS_DEBUG, "wrote %2.2x to %4.4x\n",
193 b, ((unsigned) addr & 0xffff) - 0xf020);
194}
195
196static void writew_(u16 b, const void *addr)
197{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800198 write16(addr, b);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700199 printk(BIOS_DEBUG, "wrote %4.4x to %4.4x\n",
200 b, ((unsigned) addr & 0xffff) - 0xf020);
201}
202
203static void writel_(u32 b, const void *addr)
204{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800205 write32(addr, b);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700206 printk(BIOS_DEBUG, "wrote %8.8x to %4.4x\n",
207 b, ((unsigned) addr & 0xffff) - 0xf020);
208}
209
210#else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
211
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800212#define readb_(a) read8(a)
213#define readw_(a) read16(a)
214#define readl_(a) read32(a)
215#define writeb_(val, addr) write8(addr, val)
216#define writew_(val, addr) write16(addr, val)
217#define writel_(val, addr) write32(addr, val)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700218
219#endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
220
221static void write_reg(const void *value, void *dest, uint32_t size)
222{
223 const uint8_t *bvalue = value;
224 uint8_t *bdest = dest;
225
226 while (size >= 4) {
227 writel_(*(const uint32_t *)bvalue, bdest);
228 bdest += 4; bvalue += 4; size -= 4;
229 }
230 while (size) {
231 writeb_(*bvalue, bdest);
232 bdest++; bvalue++; size--;
233 }
234}
235
236static void read_reg(const void *src, void *value, uint32_t size)
237{
238 const uint8_t *bsrc = src;
239 uint8_t *bvalue = value;
240
241 while (size >= 4) {
242 *(uint32_t *)bvalue = readl_(bsrc);
243 bsrc += 4; bvalue += 4; size -= 4;
244 }
245 while (size) {
246 *bvalue = readb_(bsrc);
247 bsrc++; bvalue++; size--;
248 }
249}
250
251static void ich_set_bbar(uint32_t minaddr)
252{
253 const uint32_t bbar_mask = 0x00ffff00;
254 uint32_t ichspi_bbar;
255
256 minaddr &= bbar_mask;
257 ichspi_bbar = readl_(cntlr.bbar) & ~bbar_mask;
258 ichspi_bbar |= minaddr;
259 writel_(ichspi_bbar, cntlr.bbar);
260}
261
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700262void spi_init(void)
263{
264 uint8_t *rcrb; /* Root Complex Register Block */
265 uint32_t rcba; /* Root Complex Base Address */
266 uint8_t bios_cntl;
267 device_t dev = PCH_DEV_LPC;
268 ich9_spi_regs *ich9_spi;
269
270 pci_read_config_dword(dev, 0xf0, &rcba);
271 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
272 rcrb = (uint8_t *)(rcba & 0xffffc000);
273 ich9_spi = (ich9_spi_regs *)(rcrb + 0x3800);
274 ichspi_lock = readw_(&ich9_spi->hsfs) & HSFS_FLOCKDN;
275 cntlr.opmenu = ich9_spi->opmenu;
276 cntlr.menubytes = sizeof(ich9_spi->opmenu);
277 cntlr.optype = &ich9_spi->optype;
278 cntlr.addr = &ich9_spi->faddr;
279 cntlr.data = (uint8_t *)ich9_spi->fdata;
280 cntlr.databytes = sizeof(ich9_spi->fdata);
281 cntlr.status = &ich9_spi->ssfs;
282 cntlr.control = (uint16_t *)ich9_spi->ssfc;
283 cntlr.bbar = &ich9_spi->bbar;
284 cntlr.preop = &ich9_spi->preop;
285 ich_set_bbar(0);
286
287 /* Disable the BIOS write protect so write commands are allowed. */
288 pci_read_config_byte(dev, 0xdc, &bios_cntl);
289 bios_cntl &= ~(1 << 5);
290 pci_write_config_byte(dev, 0xdc, bios_cntl | 0x1);
291}
292
293static void spi_init_cb(void *unused)
294{
295 spi_init();
296}
297
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500298BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_ENTRY, spi_init_cb, NULL);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700299
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700300typedef struct spi_transaction {
301 const uint8_t *out;
302 uint32_t bytesout;
303 uint8_t *in;
304 uint32_t bytesin;
305 uint8_t type;
306 uint8_t opcode;
307 uint32_t offset;
308} spi_transaction;
309
310static inline void spi_use_out(spi_transaction *trans, unsigned bytes)
311{
312 trans->out += bytes;
313 trans->bytesout -= bytes;
314}
315
316static inline void spi_use_in(spi_transaction *trans, unsigned bytes)
317{
318 trans->in += bytes;
319 trans->bytesin -= bytes;
320}
321
322static void spi_setup_type(spi_transaction *trans)
323{
324 trans->type = 0xFF;
325
326 /* Try to guess spi type from read/write sizes. */
327 if (trans->bytesin == 0) {
328 if (trans->bytesout > 4)
329 /*
330 * If bytesin = 0 and bytesout > 4, we presume this is
331 * a write data operation, which is accompanied by an
332 * address.
333 */
334 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
335 else
336 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
337 return;
338 }
339
340 if (trans->bytesout == 1) { /* and bytesin is > 0 */
341 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
342 return;
343 }
344
345 if (trans->bytesout == 4) { /* and bytesin is > 0 */
346 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
347 }
348
349 /* Fast read command is called with 5 bytes instead of 4 */
350 if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
351 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
352 --trans->bytesout;
353 }
354}
355
356static int spi_setup_opcode(spi_transaction *trans)
357{
358 uint16_t optypes;
359 uint8_t opmenu[cntlr.menubytes];
360
361 trans->opcode = trans->out[0];
362 spi_use_out(trans, 1);
363 if (!ichspi_lock) {
364 /* The lock is off, so just use index 0. */
365 writeb_(trans->opcode, cntlr.opmenu);
366 optypes = readw_(cntlr.optype);
367 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
368 writew_(optypes, cntlr.optype);
369 return 0;
370 } else {
371 /* The lock is on. See if what we need is on the menu. */
372 uint8_t optype;
373 uint16_t opcode_index;
374
375 /* Write Enable is handled as atomic prefix */
376 if (trans->opcode == SPI_OPCODE_WREN)
377 return 0;
378
379 read_reg(cntlr.opmenu, opmenu, sizeof(opmenu));
380 for (opcode_index = 0; opcode_index < cntlr.menubytes;
381 opcode_index++) {
382 if (opmenu[opcode_index] == trans->opcode)
383 break;
384 }
385
386 if (opcode_index == cntlr.menubytes) {
387 printk(BIOS_DEBUG, "ICH SPI: Opcode %x not found\n",
388 trans->opcode);
389 return -1;
390 }
391
392 optypes = readw_(cntlr.optype);
393 optype = (optypes >> (opcode_index * 2)) & 0x3;
394 if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
395 optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
396 trans->bytesout >= 3) {
397 /* We guessed wrong earlier. Fix it up. */
398 trans->type = optype;
399 }
400 if (optype != trans->type) {
401 printk(BIOS_DEBUG, "ICH SPI: Transaction doesn't fit type %d\n",
402 optype);
403 return -1;
404 }
405 return opcode_index;
406 }
407}
408
409static int spi_setup_offset(spi_transaction *trans)
410{
411 /* Separate the SPI address and data. */
412 switch (trans->type) {
413 case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
414 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
415 return 0;
416 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
417 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
418 trans->offset = ((uint32_t)trans->out[0] << 16) |
419 ((uint32_t)trans->out[1] << 8) |
420 ((uint32_t)trans->out[2] << 0);
421 spi_use_out(trans, 3);
422 return 1;
423 default:
424 printk(BIOS_DEBUG, "Unrecognized SPI transaction type %#x\n", trans->type);
425 return -1;
426 }
427}
428
429/*
430 * Wait for up to 60ms til status register bit(s) turn 1 (in case wait_til_set
431 * below is True) or 0. In case the wait was for the bit(s) to set - write
432 * those bits back, which would cause resetting them.
433 *
434 * Return the last read status value on success or -1 on failure.
435 */
436static int ich_status_poll(u16 bitmask, int wait_til_set)
437{
438 int timeout = 6000; /* This will result in 60 ms */
439 u16 status = 0;
440
441 while (timeout--) {
442 status = readw_(cntlr.status);
443 if (wait_til_set ^ ((status & bitmask) == 0)) {
444 if (wait_til_set)
445 writew_((status & bitmask), cntlr.status);
446 return status;
447 }
448 udelay(10);
449 }
450
451 printk(BIOS_DEBUG, "ICH SPI: SCIP timeout, read %x, expected %x\n",
452 status, bitmask);
453 return -1;
454}
455
Marc Jonesa6354a12014-12-26 22:11:14 -0700456unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
457{
458 return min(cntlr.databytes, buf_len);
459}
460
Furquan Shaikh94f86992016-12-01 07:12:32 -0800461static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
462 size_t bytesout, void *din, size_t bytesin)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700463{
464 uint16_t control;
465 int16_t opcode_index;
466 int with_address;
467 int status;
468
469 spi_transaction trans = {
470 dout, bytesout,
471 din, bytesin,
472 0xff, 0xff, 0
473 };
474
475 /* There has to always at least be an opcode. */
476 if (!bytesout || !dout) {
477 printk(BIOS_DEBUG, "ICH SPI: No opcode for transfer\n");
478 return -1;
479 }
480 /* Make sure if we read something we have a place to put it. */
481 if (bytesin != 0 && !din) {
482 printk(BIOS_DEBUG, "ICH SPI: Read but no target buffer\n");
483 return -1;
484 }
485
486 if (ich_status_poll(SPIS_SCIP, 0) == -1)
487 return -1;
488
489 writew_(SPIS_CDS | SPIS_FCERR, cntlr.status);
490
491 spi_setup_type(&trans);
492 if ((opcode_index = spi_setup_opcode(&trans)) < 0)
493 return -1;
494 if ((with_address = spi_setup_offset(&trans)) < 0)
495 return -1;
496
497 if (trans.opcode == SPI_OPCODE_WREN) {
498 /*
499 * Treat Write Enable as Atomic Pre-Op if possible
500 * in order to prevent the Management Engine from
501 * issuing a transaction between WREN and DATA.
502 */
503 if (!ichspi_lock)
504 writew_(trans.opcode, cntlr.preop);
505 return 0;
506 }
507
508 /* Preset control fields */
509 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
510
511 /* Issue atomic preop cycle if needed */
512 if (readw_(cntlr.preop))
513 control |= SPIC_ACS;
514
515 if (!trans.bytesout && !trans.bytesin) {
516 /* SPI addresses are 24 bit only */
517 if (with_address)
518 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
519
520 /*
521 * This is a 'no data' command (like Write Enable), its
Martin Rothde7ed6f2014-12-07 14:58:18 -0700522 * bytesout size was 1, decremented to zero while executing
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700523 * spi_setup_opcode() above. Tell the chip to send the
524 * command.
525 */
526 writew_(control, cntlr.control);
527
528 /* wait for the result */
529 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
530 if (status == -1)
531 return -1;
532
533 if (status & SPIS_FCERR) {
534 printk(BIOS_DEBUG, "ICH SPI: Command transaction error\n");
535 return -1;
536 }
537
538 return 0;
539 }
540
541 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700542 * Check if this is a write command attempting to transfer more bytes
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700543 * than the controller can handle. Iterations for writes are not
544 * supported here because each SPI write command needs to be preceded
545 * and followed by other SPI commands, and this sequence is controlled
546 * by the SPI chip driver.
547 */
548 if (trans.bytesout > cntlr.databytes) {
549 printk(BIOS_DEBUG, "ICH SPI: Too much to write. Does your SPI chip driver use"
550 " CONTROLLER_PAGE_LIMIT?\n");
551 return -1;
552 }
553
554 /*
555 * Read or write up to databytes bytes at a time until everything has
556 * been sent.
557 */
558 while (trans.bytesout || trans.bytesin) {
559 uint32_t data_length;
560
Marc Jonesa6354a12014-12-26 22:11:14 -0700561 /* SPI addresses are 24 bit only */
562 /* http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/pentium-n3520-j2850-celeron-n2920-n2820-n2815-n2806-j1850-j1750-datasheet.pdf */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700563 writel_(trans.offset & 0x00FFFFFF, cntlr.addr);
564
565 if (trans.bytesout)
566 data_length = min(trans.bytesout, cntlr.databytes);
567 else
568 data_length = min(trans.bytesin, cntlr.databytes);
569
570 /* Program data into FDATA0 to N */
571 if (trans.bytesout) {
572 write_reg(trans.out, cntlr.data, data_length);
573 spi_use_out(&trans, data_length);
574 if (with_address)
575 trans.offset += data_length;
576 }
577
578 /* Add proper control fields' values */
579 control &= ~((cntlr.databytes - 1) << 8);
580 control |= SPIC_DS;
581 control |= (data_length - 1) << 8;
582
583 /* write it */
584 writew_(control, cntlr.control);
585
586 /* Wait for Cycle Done Status or Flash Cycle Error. */
587 status = ich_status_poll(SPIS_CDS | SPIS_FCERR, 1);
588 if (status == -1)
589 return -1;
590
591 if (status & SPIS_FCERR) {
592 printk(BIOS_DEBUG, "ICH SPI: Data transaction error\n");
593 return -1;
594 }
595
596 if (trans.bytesin) {
597 read_reg(cntlr.data, trans.in, data_length);
598 spi_use_in(&trans, data_length);
599 if (with_address)
600 trans.offset += data_length;
601 }
602 }
603
604 /* Clear atomic preop now that xfer is done */
605 writew_(0, cntlr.preop);
606
607 return 0;
608}
Duncan Laurief059b242015-01-15 15:42:43 -0800609
610/* Use first empty Protected Range Register to cover region of flash */
611int spi_flash_protect(u32 start, u32 size)
612{
613 u32 end = start + size - 1;
614 u32 reg;
615 int prr;
616
617 /* Find first empty PRR */
618 for (prr = 0; prr < SPI_PRR_MAX; prr++) {
619 reg = SPIBAR32(SPI_PRR(prr));
620 if (reg == 0)
621 break;
622 }
623 if (prr >= SPI_PRR_MAX) {
624 printk(BIOS_ERR, "ERROR: No SPI PRR free!\n");
625 return -1;
626 }
627
628 /* Set protected range base and limit */
629 reg = ((end >> SPI_PRR_SHIFT) & SPI_PRR_MASK);
630 reg <<= SPI_PRR_LIMIT_SHIFT;
631 reg |= ((start >> SPI_PRR_SHIFT) & SPI_PRR_MASK);
632 reg |= SPI_PRR_WPE;
633
634 /* Set the PRR register and verify it is protected */
635 SPIBAR32(SPI_PRR(prr)) = reg;
636 reg = SPIBAR32(SPI_PRR(prr));
637 if (!(reg & SPI_PRR_WPE)) {
638 printk(BIOS_ERR, "ERROR: Unable to set SPI PRR %d\n", prr);
639 return -1;
640 }
641
642 printk(BIOS_INFO, "%s: PRR %d is enabled for range 0x%08x-0x%08x\n",
643 __func__, prr, start, end);
644 return 0;
645}
Furquan Shaikh94f86992016-12-01 07:12:32 -0800646
647static const struct spi_ctrlr spi_ctrlr = {
648 .xfer = spi_ctrlr_xfer,
Furquan Shaikhc2973d12016-11-29 22:07:42 -0800649 .xfer_vector = spi_xfer_two_vectors,
Furquan Shaikh94f86992016-12-01 07:12:32 -0800650};
651
652int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave)
653{
654 slave->bus = bus;
655 slave->cs = cs;
656 slave->ctrlr = &spi_ctrlr;
657 return 0;
658}