Martin Roth | 829c41d | 2014-05-21 14:21:22 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2 of |
| 9 | * the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <arch/io.h> |
| 22 | #include <console/console.h> |
| 23 | #include <device/pci_ids.h> |
| 24 | #include <device/pci_def.h> |
| 25 | #include <delay.h> |
| 26 | #include "soc.h" |
| 27 | |
| 28 | #define SPI_DELAY 10 /* 10us */ |
| 29 | #define SPI_RETRY 200000 /* 2s */ |
| 30 | |
| 31 | static int early_spi_read_block(u32 offset, u8 size, u8 *buffer) |
| 32 | { |
| 33 | u32 *ptr32 = (u32*)buffer; |
| 34 | u32 i; |
| 35 | |
| 36 | /* Clear status bits */ |
| 37 | RCBA16(SPIBAR_HSFS) |= SPIBAR_HSFS_AEL | SPIBAR_HSFS_FCERR | |
| 38 | SPIBAR_HSFS_FDONE; |
| 39 | |
| 40 | if (RCBA16(SPIBAR_HSFS) & SPIBAR_HSFS_SCIP) { |
| 41 | printk(BIOS_ERR, "SPI ERROR: transaction in progress\n"); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | /* Set flash address */ |
| 46 | RCBA32(SPIBAR_FADDR) = offset; |
| 47 | |
| 48 | /* Setup read transaction */ |
| 49 | RCBA16(SPIBAR_HSFC) = SPIBAR_HSFC_BYTE_COUNT(size) | |
| 50 | SPIBAR_HSFC_CYCLE_READ; |
| 51 | |
| 52 | /* Start transactinon */ |
| 53 | RCBA16(SPIBAR_HSFC) |= SPIBAR_HSFC_GO; |
| 54 | |
| 55 | /* Wait for completion */ |
| 56 | for (i = 0; i < SPI_RETRY; i++) { |
| 57 | if (RCBA16(SPIBAR_HSFS) & SPIBAR_HSFS_SCIP) { |
| 58 | /* Cycle in progress, wait 1ms */ |
| 59 | udelay(SPI_DELAY); |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | if (RCBA16(SPIBAR_HSFS) & SPIBAR_HSFS_AEL) { |
| 64 | printk(BIOS_ERR, "SPI ERROR: Access Error\n"); |
| 65 | return -1; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | if (RCBA16(SPIBAR_HSFS) & SPIBAR_HSFS_FCERR) { |
| 70 | printk(BIOS_ERR, "SPI ERROR: Flash Cycle Error\n"); |
| 71 | return -1; |
| 72 | } |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | if (i >= SPI_RETRY) { |
| 77 | printk(BIOS_ERR, "SPI ERROR: Timeout\n"); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | /* Read the data */ |
| 82 | for (i = 0; i < size; i+=sizeof(u32)) { |
| 83 | if (size-i >= 4) { |
| 84 | /* reading >= dword */ |
| 85 | *ptr32++ = RCBA32(SPIBAR_FDATA(i/sizeof(u32))); |
| 86 | } else { |
| 87 | /* reading < dword */ |
| 88 | u8 j, *ptr8 = (u8*)ptr32; |
| 89 | u32 temp = RCBA32(SPIBAR_FDATA(i/sizeof(u32))); |
| 90 | for (j = 0; j < (size-i); j++) { |
| 91 | *ptr8++ = temp & 0xff; |
| 92 | temp >>= 8; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return size; |
| 98 | } |
| 99 | |
| 100 | int early_spi_read(u32 offset, u32 size, u8 *buffer) |
| 101 | { |
| 102 | u32 current = 0; |
| 103 | |
| 104 | while (size > 0) { |
| 105 | u8 count = (size < 64) ? size : 64; |
| 106 | if (early_spi_read_block(offset + current, count, |
| 107 | buffer + current) < 0) |
| 108 | return -1; |
| 109 | size -= count; |
| 110 | current += count; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |