zbao | 246e84b | 2012-07-13 18:47:03 +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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 22 | #include <arch/io.h> |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 23 | #include <spi.h> |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 24 | #include <device/device.h> |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 25 | #include <device/pci.h> |
| 26 | #include <device/pci_ops.h> |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 27 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame^] | 28 | #if defined (CONFIG_HUDSON_IMC_FWM) |
| 29 | #include "FchPlatform.h" |
| 30 | |
| 31 | static int bus_claimed = 0; |
| 32 | #endif |
| 33 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 34 | static u32 spibar; |
| 35 | |
| 36 | static void reset_internal_fifo_pointer(void) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 37 | { |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 38 | do { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 39 | write8(spibar + 2, read8(spibar + 2) | 0x10); |
| 40 | } while (read8(spibar + 0xD) & 0x7); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 41 | } |
| 42 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 43 | static void execute_command(void) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 44 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 45 | write8(spibar + 2, read8(spibar + 2) | 1); |
| 46 | |
| 47 | while ((read8(spibar + 2) & 1) && (read8(spibar+3) & 0x80)); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 50 | void spi_init() |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 51 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 52 | device_t dev; |
| 53 | |
| 54 | dev = dev_find_slot(0, PCI_DEVFN(0x14, 3)); |
| 55 | spibar = pci_read_config32(dev, 0xA0) & ~0x1F; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 56 | } |
| 57 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 58 | int spi_xfer(struct spi_slave *slave, const void *dout, |
| 59 | unsigned int bitsout, void *din, unsigned int bitsin) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 60 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 61 | /* First byte is cmd which can not being sent through FIFO. */ |
| 62 | u8 cmd = *(u8 *)dout++; |
| 63 | u8 readoffby1; |
| 64 | u8 readwrite; |
| 65 | u8 bytesout, bytesin; |
| 66 | u8 count; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 67 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 68 | bitsout -= 8; |
| 69 | bytesout = bitsout / 8; |
| 70 | bytesin = bitsin / 8; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 71 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 72 | readoffby1 = bytesout ? 0 : 1; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 73 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 74 | readwrite = (bytesin + readoffby1) << 4 | bytesout; |
| 75 | write8(spibar + 1, readwrite); |
| 76 | write8(spibar + 0, cmd); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 77 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 78 | reset_internal_fifo_pointer(); |
| 79 | for (count = 0; count < bytesout; count++, dout++) { |
| 80 | write8(spibar + 0x0C, *(u8 *)dout); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 81 | } |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 82 | |
| 83 | reset_internal_fifo_pointer(); |
| 84 | execute_command(); |
| 85 | |
| 86 | reset_internal_fifo_pointer(); |
| 87 | /* Skip the bytes we sent. */ |
| 88 | for (count = 0; count < bytesout; count++) { |
| 89 | cmd = read8(spibar + 0x0C); |
| 90 | } |
| 91 | |
| 92 | reset_internal_fifo_pointer(); |
| 93 | for (count = 0; count < bytesin; count++, din++) { |
| 94 | *(u8 *)din = read8(spibar + 0x0C); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | int spi_claim_bus(struct spi_slave *slave) |
| 100 | { |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame^] | 101 | #if defined (CONFIG_HUDSON_IMC_FWM) |
| 102 | |
| 103 | if (slave->rw == SPI_WRITE_FLAG) { |
| 104 | bus_claimed++; |
| 105 | if (bus_claimed == 1) |
| 106 | ImcSleep(NULL); |
| 107 | } |
| 108 | #endif |
| 109 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 110 | return 0; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 113 | void spi_release_bus(struct spi_slave *slave) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 114 | { |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame^] | 115 | #if defined (CONFIG_HUDSON_IMC_FWM) |
| 116 | |
| 117 | if (slave->rw == SPI_WRITE_FLAG) { |
| 118 | bus_claimed--; |
| 119 | if (bus_claimed <= 0) { |
| 120 | bus_claimed = 0; |
| 121 | ImcWakeup(NULL); |
| 122 | } |
| 123 | } |
| 124 | #endif |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 125 | } |
| 126 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 127 | void spi_cs_activate(struct spi_slave *slave) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 128 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void spi_cs_deactivate(struct spi_slave *slave) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 136 | unsigned int max_hz, unsigned int mode) |
| 137 | { |
| 138 | struct spi_slave *slave = malloc(sizeof(*slave)); |
| 139 | |
| 140 | if (!slave) { |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | memset(slave, 0, sizeof(*slave)); |
| 145 | |
| 146 | return slave; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 147 | } |