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. |
| 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 | 01bd79f | 2012-03-23 11:36:08 +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 | 01bd79f | 2012-03-23 11:36:08 +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 | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 27 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 28 | static u32 spibar; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 29 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 30 | static void reset_internal_fifo_pointer(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 31 | { |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 32 | do { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 33 | write8(spibar + 2, read8(spibar + 2) | 0x10); |
| 34 | } while (read8(spibar + 0xD) & 0x7); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 35 | } |
| 36 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 37 | static void execute_command(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 38 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 39 | write8(spibar + 2, read8(spibar + 2) | 1); |
| 40 | |
| 41 | while ((read8(spibar + 2) & 1) && (read8(spibar+3) & 0x80)); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 42 | } |
| 43 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 44 | void spi_init() |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 45 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 46 | device_t dev; |
| 47 | |
| 48 | dev = dev_find_slot(0, PCI_DEVFN(0x14, 3)); |
| 49 | spibar = pci_read_config32(dev, 0xA0) & ~0x1F; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 50 | } |
| 51 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 52 | int spi_xfer(struct spi_slave *slave, const void *dout, |
| 53 | unsigned int bitsout, void *din, unsigned int bitsin) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 54 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 55 | /* First byte is cmd which can not being sent through FIFO. */ |
| 56 | u8 cmd = *(u8 *)dout++; |
| 57 | u8 readoffby1; |
| 58 | u8 readwrite; |
| 59 | u8 bytesout, bytesin; |
| 60 | u8 count; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 61 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 62 | bitsout -= 8; |
| 63 | bytesout = bitsout / 8; |
| 64 | bytesin = bitsin / 8; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 65 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 66 | readoffby1 = bytesout ? 0 : 1; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 67 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 68 | readwrite = (bytesin + readoffby1) << 4 | bytesout; |
| 69 | write8(spibar + 1, readwrite); |
| 70 | write8(spibar + 0, cmd); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 71 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 72 | reset_internal_fifo_pointer(); |
| 73 | for (count = 0; count < bytesout; count++, dout++) { |
| 74 | write8(spibar + 0x0C, *(u8 *)dout); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 75 | } |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 76 | |
| 77 | reset_internal_fifo_pointer(); |
| 78 | execute_command(); |
| 79 | |
| 80 | reset_internal_fifo_pointer(); |
| 81 | /* Skip the bytes we sent. */ |
| 82 | for (count = 0; count < bytesout; count++) { |
| 83 | cmd = read8(spibar + 0x0C); |
| 84 | } |
| 85 | |
| 86 | reset_internal_fifo_pointer(); |
| 87 | for (count = 0; count < bytesin; count++, din++) { |
| 88 | *(u8 *)din = read8(spibar + 0x0C); |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | int spi_claim_bus(struct spi_slave *slave) |
| 94 | { |
| 95 | return 0; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 96 | } |
| 97 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 98 | void spi_release_bus(struct spi_slave *slave) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 99 | { |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 100 | } |
| 101 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 102 | void spi_cs_activate(struct spi_slave *slave) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 103 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame^] | 104 | } |
| 105 | |
| 106 | void spi_cs_deactivate(struct spi_slave *slave) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 111 | unsigned int max_hz, unsigned int mode) |
| 112 | { |
| 113 | struct spi_slave *slave = malloc(sizeof(*slave)); |
| 114 | |
| 115 | if (!slave) { |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | memset(slave, 0, sizeof(*slave)); |
| 120 | |
| 121 | return slave; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 122 | } |