zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 14 | */ |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 15 | #include <stdint.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 18 | #include <arch/io.h> |
Kyösti Mälkki | 9f0a2be | 2014-06-30 07:34:36 +0300 | [diff] [blame] | 19 | #include <console/console.h> |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame^] | 20 | #include <spi_flash.h> |
Zheng Bao | 600784e | 2013-02-07 17:30:23 +0800 | [diff] [blame] | 21 | #include <spi-generic.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 22 | #include <device/device.h> |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 23 | #include <device/pci.h> |
| 24 | #include <device/pci_ops.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 25 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 26 | #include "SBPLATFORM.h" |
| 27 | #include <vendorcode/amd/cimx/sb800/ECfan.h> |
| 28 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 29 | #define AMD_SB_SPI_TX_LEN 8 |
| 30 | |
Stefan Reinauer | 12bce3f | 2015-06-18 01:17:38 -0700 | [diff] [blame] | 31 | static uintptr_t spibar; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 32 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 33 | static void reset_internal_fifo_pointer(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 34 | { |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 35 | do { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 36 | write8((void *)(spibar + 2), |
| 37 | read8((void *)(spibar + 2)) | 0x10); |
| 38 | } while (read8((void *)(spibar + 0xD)) & 0x7); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 39 | } |
| 40 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 41 | static void execute_command(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 42 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 43 | write8((void *)(spibar + 2), read8((void *)(spibar + 2)) | 1); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 44 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 45 | while ((read8((void *)(spibar + 2)) & 1) && |
| 46 | (read8((void *)(spibar+3)) & 0x80)); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 47 | } |
| 48 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 49 | void spi_init() |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 50 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 51 | device_t dev; |
| 52 | |
| 53 | dev = dev_find_slot(0, PCI_DEVFN(0x14, 3)); |
| 54 | spibar = pci_read_config32(dev, 0xA0) & ~0x1F; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 55 | } |
| 56 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 57 | unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len) |
| 58 | { |
| 59 | return min(AMD_SB_SPI_TX_LEN - cmd_len, buf_len); |
| 60 | } |
| 61 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 62 | int spi_xfer(struct spi_slave *slave, const void *dout, |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 63 | unsigned int bytesout, void *din, unsigned int bytesin) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 64 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 65 | /* First byte is cmd which can not being sent through FIFO. */ |
| 66 | u8 cmd = *(u8 *)dout++; |
| 67 | u8 readoffby1; |
| 68 | u8 readwrite; |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 69 | u8 count; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 70 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 71 | bytesout--; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 72 | |
Kyösti Mälkki | 9f0a2be | 2014-06-30 07:34:36 +0300 | [diff] [blame] | 73 | /* |
| 74 | * Check if this is a write command attempting to transfer more bytes |
| 75 | * than the controller can handle. Iterations for writes are not |
| 76 | * supported here because each SPI write command needs to be preceded |
| 77 | * and followed by other SPI commands, and this sequence is controlled |
| 78 | * by the SPI chip driver. |
| 79 | */ |
| 80 | if (bytesout > AMD_SB_SPI_TX_LEN) { |
| 81 | printk(BIOS_DEBUG, "FCH SPI: Too much to write. Does your SPI chip driver use" |
| 82 | " spi_crop_chunk()?\n"); |
| 83 | return -1; |
| 84 | } |
| 85 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 86 | readoffby1 = bytesout ? 0 : 1; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 87 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 88 | readwrite = (bytesin + readoffby1) << 4 | bytesout; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 89 | write8((void *)(spibar + 1), readwrite); |
| 90 | write8((void *)(spibar + 0), cmd); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 91 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 92 | reset_internal_fifo_pointer(); |
| 93 | for (count = 0; count < bytesout; count++, dout++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 94 | write8((void *)(spibar + 0x0C), *(u8 *)dout); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 95 | } |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 96 | |
| 97 | reset_internal_fifo_pointer(); |
| 98 | execute_command(); |
| 99 | |
| 100 | reset_internal_fifo_pointer(); |
| 101 | /* Skip the bytes we sent. */ |
| 102 | for (count = 0; count < bytesout; count++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 103 | cmd = read8((void *)(spibar + 0x0C)); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | reset_internal_fifo_pointer(); |
| 107 | for (count = 0; count < bytesin; count++, din++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 108 | *(u8 *)din = read8((void *)(spibar + 0x0C)); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 113 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 114 | static void ImcSleep(void) |
| 115 | { |
| 116 | u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */ |
| 117 | u8 reg0_val = 0; /* clear response register */ |
| 118 | u8 reg1_val = 0xB4; /* request ownership flag */ |
| 119 | |
| 120 | WriteECmsg (MSG_REG0, AccWidthUint8, ®0_val); |
| 121 | WriteECmsg (MSG_REG1, AccWidthUint8, ®1_val); |
| 122 | WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val); |
| 123 | |
| 124 | WaitForEcLDN9MailboxCmdAck(); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static void ImcWakeup(void) |
| 129 | { |
| 130 | u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */ |
Idwer Vollering | d26da9c | 2013-12-22 21:38:18 +0000 | [diff] [blame] | 131 | u8 reg0_val = 0; /* clear response register */ |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 132 | u8 reg1_val = 0xB5; /* release ownership flag */ |
| 133 | |
| 134 | WriteECmsg (MSG_REG0, AccWidthUint8, ®0_val); |
| 135 | WriteECmsg (MSG_REG1, AccWidthUint8, ®1_val); |
| 136 | WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val); |
| 137 | |
| 138 | WaitForEcLDN9MailboxCmdAck(); |
| 139 | } |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 140 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 141 | int spi_claim_bus(struct spi_slave *slave) |
| 142 | { |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame^] | 143 | /* Nothing is required. */ |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 144 | return 0; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 145 | } |
| 146 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 147 | void spi_release_bus(struct spi_slave *slave) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 148 | { |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame^] | 149 | /* Nothing is required. */ |
| 150 | return; |
| 151 | } |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 152 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame^] | 153 | int chipset_volatile_group_begin(const struct spi_flash *flash) |
| 154 | { |
| 155 | if (!IS_ENABLED(CONFIG_SB800_IMC_FWM)) |
| 156 | return 0; |
| 157 | |
| 158 | ImcSleep(); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int chipset_volatile_group_end(const struct spi_flash *flash) |
| 163 | { |
| 164 | if (!IS_ENABLED(CONFIG_SB800_IMC_FWM)) |
| 165 | return 0; |
| 166 | |
| 167 | ImcWakeup(); |
| 168 | return 0; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 171 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs) |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 172 | { |
| 173 | struct spi_slave *slave = malloc(sizeof(*slave)); |
| 174 | |
| 175 | if (!slave) { |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | memset(slave, 0, sizeof(*slave)); |
| 180 | |
| 181 | return slave; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 182 | } |