Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Elyes HAOUAS | 400f9ca | 2019-06-23 07:01:22 +0200 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Kyösti Mälkki | 9f0a2be | 2014-06-30 07:34:36 +0300 | [diff] [blame] | 4 | #include <console/console.h> |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 5 | #include <spi_flash.h> |
Zheng Bao | 600784e | 2013-02-07 17:30:23 +0800 | [diff] [blame] | 6 | #include <spi-generic.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 7 | #include <device/device.h> |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 8 | #include <device/pci.h> |
| 9 | #include <device/pci_ops.h> |
Elyes HAOUAS | 400f9ca | 2019-06-23 07:01:22 +0200 | [diff] [blame] | 10 | #include <types.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 11 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 12 | #include "SBPLATFORM.h" |
| 13 | #include <vendorcode/amd/cimx/sb800/ECfan.h> |
| 14 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 15 | #define AMD_SB_SPI_TX_LEN 8 |
| 16 | |
Stefan Reinauer | 12bce3f | 2015-06-18 01:17:38 -0700 | [diff] [blame] | 17 | static uintptr_t spibar; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 18 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 19 | static void reset_internal_fifo_pointer(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 20 | { |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 21 | do { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 22 | write8((void *)(spibar + 2), |
| 23 | read8((void *)(spibar + 2)) | 0x10); |
| 24 | } while (read8((void *)(spibar + 0xD)) & 0x7); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 25 | } |
| 26 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 27 | static void execute_command(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 28 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 29 | write8((void *)(spibar + 2), read8((void *)(spibar + 2)) | 1); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 30 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 31 | while ((read8((void *)(spibar + 2)) & 1) && |
| 32 | (read8((void *)(spibar+3)) & 0x80)); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 33 | } |
| 34 | |
Elyes HAOUAS | eb9e63f | 2022-01-25 11:51:43 +0100 | [diff] [blame] | 35 | void spi_init(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 36 | { |
Elyes HAOUAS | 1a4abb7 | 2018-05-19 16:49:20 +0200 | [diff] [blame] | 37 | struct device *dev; |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 38 | |
Kyösti Mälkki | c70eed1 | 2018-05-22 02:18:00 +0300 | [diff] [blame] | 39 | dev = pcidev_on_root(0x14, 3); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 40 | spibar = pci_read_config32(dev, 0xA0) & ~0x1F; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 41 | } |
| 42 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 43 | static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout, |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 44 | size_t bytesout, void *din, size_t bytesin) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 45 | { |
Paul Menzel | dee0f88 | 2018-11-10 11:24:38 +0100 | [diff] [blame] | 46 | /* First byte is cmd which can not be sent through FIFO. */ |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 47 | u8 cmd = *(u8 *)dout++; |
| 48 | u8 readoffby1; |
| 49 | u8 readwrite; |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 50 | size_t count; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 51 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 52 | bytesout--; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 53 | |
Kyösti Mälkki | 9f0a2be | 2014-06-30 07:34:36 +0300 | [diff] [blame] | 54 | /* |
| 55 | * Check if this is a write command attempting to transfer more bytes |
| 56 | * than the controller can handle. Iterations for writes are not |
| 57 | * supported here because each SPI write command needs to be preceded |
| 58 | * and followed by other SPI commands, and this sequence is controlled |
| 59 | * by the SPI chip driver. |
| 60 | */ |
| 61 | if (bytesout > AMD_SB_SPI_TX_LEN) { |
| 62 | printk(BIOS_DEBUG, "FCH SPI: Too much to write. Does your SPI chip driver use" |
| 63 | " spi_crop_chunk()?\n"); |
| 64 | return -1; |
| 65 | } |
| 66 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 67 | readoffby1 = bytesout ? 0 : 1; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 68 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 69 | readwrite = (bytesin + readoffby1) << 4 | bytesout; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 70 | write8((void *)(spibar + 1), readwrite); |
| 71 | write8((void *)(spibar + 0), cmd); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 72 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 73 | reset_internal_fifo_pointer(); |
| 74 | for (count = 0; count < bytesout; count++, dout++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 75 | write8((void *)(spibar + 0x0C), *(u8 *)dout); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 76 | } |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 77 | |
| 78 | reset_internal_fifo_pointer(); |
| 79 | execute_command(); |
| 80 | |
| 81 | reset_internal_fifo_pointer(); |
| 82 | /* Skip the bytes we sent. */ |
| 83 | for (count = 0; count < bytesout; count++) { |
Paul Menzel | 9eb4d0a | 2018-11-10 11:27:02 +0100 | [diff] [blame] | 84 | read8((void *)(spibar + 0x0C)); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | reset_internal_fifo_pointer(); |
| 88 | for (count = 0; count < bytesin; count++, din++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 89 | *(u8 *)din = read8((void *)(spibar + 0x0C)); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 94 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 95 | static void ImcSleep(void) |
| 96 | { |
| 97 | u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */ |
| 98 | u8 reg0_val = 0; /* clear response register */ |
| 99 | u8 reg1_val = 0xB4; /* request ownership flag */ |
| 100 | |
| 101 | WriteECmsg (MSG_REG0, AccWidthUint8, ®0_val); |
| 102 | WriteECmsg (MSG_REG1, AccWidthUint8, ®1_val); |
| 103 | WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val); |
| 104 | |
| 105 | WaitForEcLDN9MailboxCmdAck(); |
| 106 | } |
| 107 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 108 | static void ImcWakeup(void) |
| 109 | { |
| 110 | u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */ |
Idwer Vollering | d26da9c | 2013-12-22 21:38:18 +0000 | [diff] [blame] | 111 | u8 reg0_val = 0; /* clear response register */ |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 112 | u8 reg1_val = 0xB5; /* release ownership flag */ |
| 113 | |
| 114 | WriteECmsg (MSG_REG0, AccWidthUint8, ®0_val); |
| 115 | WriteECmsg (MSG_REG1, AccWidthUint8, ®1_val); |
| 116 | WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val); |
| 117 | |
| 118 | WaitForEcLDN9MailboxCmdAck(); |
| 119 | } |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 120 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 121 | int chipset_volatile_group_begin(const struct spi_flash *flash) |
| 122 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 123 | if (!CONFIG(SB800_IMC_FWM)) |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 124 | return 0; |
| 125 | |
| 126 | ImcSleep(); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int chipset_volatile_group_end(const struct spi_flash *flash) |
| 131 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 132 | if (!CONFIG(SB800_IMC_FWM)) |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 133 | return 0; |
| 134 | |
| 135 | ImcWakeup(); |
| 136 | return 0; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 137 | } |
| 138 | |
Aaron Durbin | 851dde8 | 2018-04-19 21:15:25 -0600 | [diff] [blame] | 139 | static int xfer_vectors(const struct spi_slave *slave, |
| 140 | struct spi_op vectors[], size_t count) |
| 141 | { |
| 142 | return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer); |
| 143 | } |
| 144 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 145 | static const struct spi_ctrlr spi_ctrlr = { |
Aaron Durbin | 851dde8 | 2018-04-19 21:15:25 -0600 | [diff] [blame] | 146 | .xfer_vector = xfer_vectors, |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 147 | .max_xfer_size = AMD_SB_SPI_TX_LEN, |
Aaron Durbin | 1fcc9f3 | 2018-01-29 11:30:17 -0700 | [diff] [blame] | 148 | .flags = SPI_CNTRLR_DEDUCT_CMD_LEN, |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
Furquan Shaikh | 12eca76 | 2017-05-18 14:58:49 -0700 | [diff] [blame] | 151 | const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = { |
| 152 | { |
| 153 | .ctrlr = &spi_ctrlr, |
| 154 | .bus_start = 0, |
| 155 | .bus_end = 0, |
| 156 | }, |
| 157 | }; |
| 158 | |
| 159 | const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map); |