| 1 |
|
| 2 |
|
| 3 |
#include <device/mmio.h> |
| 4 |
#include <assert.h> |
| 5 |
#include <boot_device.h> |
| 6 |
#include <cbfs.h> |
| 7 |
#include <commonlib/region.h> |
| 8 |
#include <console/console.h> |
| 9 |
#include <soc/clk.h> |
| 10 |
#include <soc/gpio.h> |
| 11 |
#include <soc/spi.h> |
| 12 |
#include <stddef.h> |
| 13 |
#include <stdint.h> |
| 14 |
#include <symbols.h> |
| 15 |
|
| 16 |
#if defined(CONFIG_DEBUG_SPI) && CONFIG_DEBUG_SPI |
| 17 |
# define DEBUG_SPI(x,...) printk(BIOS_DEBUG, "EXYNOS_SPI: " x) |
| 18 |
#else |
| 19 |
# define DEBUG_SPI(x,...) |
| 20 |
#endif |
| 21 |
|
| 22 |
static void exynos_spi_rx_tx(struct exynos_spi *regs, int todo, |
| 23 |
void *dinp, void const *doutp, int i) |
| 24 |
{ |
| 25 |
int rx_lvl, tx_lvl; |
| 26 |
unsigned int *rxp = (unsigned int *)(dinp + (i * (32 * 1024))); |
| 27 |
unsigned int out_bytes, in_bytes; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
ASSERT(todo % 4 == 0); |
| 32 |
|
| 33 |
out_bytes = in_bytes = todo; |
| 34 |
setbits32(®s->ch_cfg, SPI_CH_RST); |
| 35 |
clrbits32(®s->ch_cfg, SPI_CH_RST); |
| 36 |
write32(®s->pkt_cnt, ((todo * 8) / 32) | SPI_PACKET_CNT_EN); |
| 37 |
|
| 38 |
while (in_bytes) { |
| 39 |
uint32_t spi_sts; |
| 40 |
int temp; |
| 41 |
|
| 42 |
spi_sts = read32(®s->spi_sts); |
| 43 |
rx_lvl = ((spi_sts >> 15) & 0x7f); |
| 44 |
tx_lvl = ((spi_sts >> 6) & 0x7f); |
| 45 |
while (tx_lvl < 32 && out_bytes) { |
| 46 |
|
| 47 |
|
| 48 |
temp = 0xffffffff; |
| 49 |
write32(®s->tx_data, temp); |
| 50 |
out_bytes -= 4; |
| 51 |
tx_lvl += 4; |
| 52 |
} |
| 53 |
while (rx_lvl >= 4 && in_bytes) { |
| 54 |
temp = read32(®s->rx_data); |
| 55 |
if (rxp) |
| 56 |
*rxp++ = temp; |
| 57 |
in_bytes -= 4; |
| 58 |
rx_lvl -= 4; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
int exynos_spi_open(struct exynos_spi *regs) |
| 65 |
{ |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
write32(®s->pkt_cnt, 4 | SPI_PACKET_CNT_EN); |
| 70 |
|
| 71 |
write32(®s->fb_clk, SPI_FB_DELAY_180); |
| 72 |
|
| 73 |
setbits32(®s->mode_cfg, |
| 74 |
SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD); |
| 75 |
clrbits32(®s->ch_cfg, SPI_CH_CPOL_L); |
| 76 |
|
| 77 |
|
| 78 |
clrbits32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON); |
| 79 |
|
| 80 |
setbits32(®s->swap_cfg, |
| 81 |
SPI_RX_SWAP_EN | SPI_RX_BYTE_SWAP | SPI_RX_HWORD_SWAP); |
| 82 |
|
| 83 |
|
| 84 |
setbits32(®s->ch_cfg, SPI_CH_RST); |
| 85 |
clrbits32(®s->ch_cfg, SPI_CH_RST); |
| 86 |
|
| 87 |
|
| 88 |
setbits32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN); |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
int exynos_spi_read(struct exynos_spi *regs, void *dest, u32 len, u32 off) |
| 93 |
{ |
| 94 |
int upto, todo; |
| 95 |
int i; |
| 96 |
clrbits32(®s->cs_reg, SPI_SLAVE_SIG_INACT); |
| 97 |
|
| 98 |
|
| 99 |
write32(®s->tx_data, (SF_READ_DATA_CMD << 24) | off); |
| 100 |
|
| 101 |
|
| 102 |
while (!(read32(®s->spi_sts) & SPI_ST_TX_DONE)); |
| 103 |
|
| 104 |
for (upto = 0, i = 0; upto < len; upto += todo, i++) { |
| 105 |
todo = MIN(len - upto, (1 << 15)); |
| 106 |
exynos_spi_rx_tx(regs, todo, dest, (void *)(off), i); |
| 107 |
} |
| 108 |
|
| 109 |
setbits32(®s->cs_reg, SPI_SLAVE_SIG_INACT); |
| 110 |
|
| 111 |
return len; |
| 112 |
} |
| 113 |
|
| 114 |
int exynos_spi_close(struct exynos_spi *regs) |
| 115 |
{ |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
clrbits32(®s->mode_cfg, |
| 121 |
SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD); |
| 122 |
write32(®s->swap_cfg, 0); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
clrsetbits32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST); |
| 129 |
clrbits32(®s->ch_cfg, SPI_CH_RST); |
| 130 |
clrbits32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
static struct exynos_spi *boot_slave_regs; |
| 135 |
|
| 136 |
static ssize_t exynos_spi_readat(const struct region_device *rdev, void *dest, |
| 137 |
size_t offset, size_t count) |
| 138 |
{ |
| 139 |
int bytes; |
| 140 |
DEBUG_SPI("exynos_spi_cbfs_read(%u)\n", count); |
| 141 |
exynos_spi_open(boot_slave_regs); |
| 142 |
bytes = exynos_spi_read(boot_slave_regs, dest, count, offset); |
| 143 |
exynos_spi_close(boot_slave_regs); |
| 144 |
return bytes; |
| 145 |
} |
| 146 |
|
| 147 |
static void *exynos_spi_map(const struct region_device *rdev, |
| 148 |
size_t offset, size_t count) |
| 149 |
{ |
| 150 |
DEBUG_SPI("exynos_spi_cbfs_map\n"); |
| 151 |
|
| 152 |
|
| 153 |
if (count % 4) |
| 154 |
count += 4 - (count % 4); |
| 155 |
return mmap_helper_rdev_mmap(rdev, offset, count); |
| 156 |
} |
| 157 |
|
| 158 |
static const struct region_device_ops exynos_spi_ops = { |
| 159 |
.mmap = exynos_spi_map, |
| 160 |
.munmap = mmap_helper_rdev_munmap, |
| 161 |
.readat = exynos_spi_readat, |
| 162 |
}; |
| 163 |
|
| 164 |
static struct mmap_helper_region_device mdev = |
| 165 |
MMAP_HELPER_DEV_INIT(&exynos_spi_ops, 0, CONFIG_ROM_SIZE, &cbfs_cache); |
| 166 |
|
| 167 |
void exynos_init_spi_boot_device(void) |
| 168 |
{ |
| 169 |
boot_slave_regs = (void *)EXYNOS5_SPI1_BASE; |
| 170 |
} |
| 171 |
|
| 172 |
const struct region_device *exynos_spi_boot_device(void) |
| 173 |
{ |
| 174 |
return &mdev.rdev; |
| 175 |
} |