Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Altera SPI driver |
| 3 | * |
| 4 | * based on bfin_spi.c |
| 5 | * Copyright (c) 2005-2008 Analog Devices Inc. |
| 6 | * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw> |
| 7 | * |
| 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | #include <common.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <malloc.h> |
| 13 | #include <spi.h> |
| 14 | |
| 15 | #define ALTERA_SPI_RXDATA 0 |
| 16 | #define ALTERA_SPI_TXDATA 4 |
| 17 | #define ALTERA_SPI_STATUS 8 |
| 18 | #define ALTERA_SPI_CONTROL 12 |
| 19 | #define ALTERA_SPI_SLAVE_SEL 20 |
| 20 | |
| 21 | #define ALTERA_SPI_STATUS_ROE_MSK (0x8) |
| 22 | #define ALTERA_SPI_STATUS_TOE_MSK (0x10) |
| 23 | #define ALTERA_SPI_STATUS_TMT_MSK (0x20) |
| 24 | #define ALTERA_SPI_STATUS_TRDY_MSK (0x40) |
| 25 | #define ALTERA_SPI_STATUS_RRDY_MSK (0x80) |
| 26 | #define ALTERA_SPI_STATUS_E_MSK (0x100) |
| 27 | |
| 28 | #define ALTERA_SPI_CONTROL_IROE_MSK (0x8) |
| 29 | #define ALTERA_SPI_CONTROL_ITOE_MSK (0x10) |
| 30 | #define ALTERA_SPI_CONTROL_ITRDY_MSK (0x40) |
| 31 | #define ALTERA_SPI_CONTROL_IRRDY_MSK (0x80) |
| 32 | #define ALTERA_SPI_CONTROL_IE_MSK (0x100) |
| 33 | #define ALTERA_SPI_CONTROL_SSO_MSK (0x400) |
| 34 | |
| 35 | #ifndef CONFIG_SYS_ALTERA_SPI_LIST |
| 36 | #define CONFIG_SYS_ALTERA_SPI_LIST { CONFIG_SYS_SPI_BASE } |
| 37 | #endif |
| 38 | |
| 39 | static ulong altera_spi_base_list[] = CONFIG_SYS_ALTERA_SPI_LIST; |
| 40 | |
| 41 | struct altera_spi_slave { |
| 42 | struct spi_slave slave; |
| 43 | ulong base; |
| 44 | }; |
| 45 | #define to_altera_spi_slave(s) container_of(s, struct altera_spi_slave, slave) |
| 46 | |
| 47 | __attribute__((weak)) |
| 48 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 49 | { |
| 50 | return bus < ARRAY_SIZE(altera_spi_base_list) && cs < 32; |
| 51 | } |
| 52 | |
| 53 | __attribute__((weak)) |
| 54 | void spi_cs_activate(struct spi_slave *slave) |
| 55 | { |
| 56 | struct altera_spi_slave *altspi = to_altera_spi_slave(slave); |
| 57 | writel(1 << slave->cs, altspi->base + ALTERA_SPI_SLAVE_SEL); |
| 58 | writel(ALTERA_SPI_CONTROL_SSO_MSK, altspi->base + ALTERA_SPI_CONTROL); |
| 59 | } |
| 60 | |
| 61 | __attribute__((weak)) |
| 62 | void spi_cs_deactivate(struct spi_slave *slave) |
| 63 | { |
| 64 | struct altera_spi_slave *altspi = to_altera_spi_slave(slave); |
| 65 | writel(0, altspi->base + ALTERA_SPI_CONTROL); |
| 66 | writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL); |
| 67 | } |
| 68 | |
| 69 | void spi_init(void) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 74 | unsigned int max_hz, unsigned int mode) |
| 75 | { |
| 76 | struct altera_spi_slave *altspi; |
| 77 | |
| 78 | if (!spi_cs_is_valid(bus, cs)) |
| 79 | return NULL; |
| 80 | |
| 81 | altspi = malloc(sizeof(*altspi)); |
| 82 | if (!altspi) |
| 83 | return NULL; |
| 84 | |
| 85 | altspi->slave.bus = bus; |
| 86 | altspi->slave.cs = cs; |
| 87 | altspi->base = altera_spi_base_list[bus]; |
| 88 | debug("%s: bus:%i cs:%i base:%lx\n", __func__, |
| 89 | bus, cs, altspi->base); |
| 90 | |
| 91 | return &altspi->slave; |
| 92 | } |
| 93 | |
| 94 | void spi_free_slave(struct spi_slave *slave) |
| 95 | { |
| 96 | struct altera_spi_slave *altspi = to_altera_spi_slave(slave); |
| 97 | free(altspi); |
| 98 | } |
| 99 | |
| 100 | int spi_claim_bus(struct spi_slave *slave) |
| 101 | { |
| 102 | struct altera_spi_slave *altspi = to_altera_spi_slave(slave); |
| 103 | |
| 104 | debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs); |
| 105 | writel(0, altspi->base + ALTERA_SPI_CONTROL); |
| 106 | writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | void spi_release_bus(struct spi_slave *slave) |
| 111 | { |
| 112 | struct altera_spi_slave *altspi = to_altera_spi_slave(slave); |
| 113 | |
| 114 | debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs); |
| 115 | writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL); |
| 116 | } |
| 117 | |
| 118 | #ifndef CONFIG_ALTERA_SPI_IDLE_VAL |
| 119 | # define CONFIG_ALTERA_SPI_IDLE_VAL 0xff |
| 120 | #endif |
| 121 | |
| 122 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 123 | void *din, unsigned long flags) |
| 124 | { |
| 125 | struct altera_spi_slave *altspi = to_altera_spi_slave(slave); |
| 126 | /* assume spi core configured to do 8 bit transfers */ |
| 127 | uint bytes = bitlen / 8; |
| 128 | const uchar *txp = dout; |
| 129 | uchar *rxp = din; |
| 130 | |
| 131 | debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__, |
| 132 | slave->bus, slave->cs, bitlen, bytes, flags); |
| 133 | if (bitlen == 0) |
| 134 | goto done; |
| 135 | |
| 136 | if (bitlen % 8) { |
| 137 | flags |= SPI_XFER_END; |
| 138 | goto done; |
| 139 | } |
| 140 | |
| 141 | /* empty read buffer */ |
| 142 | if (readl(altspi->base + ALTERA_SPI_STATUS) & |
| 143 | ALTERA_SPI_STATUS_RRDY_MSK) |
| 144 | readl(altspi->base + ALTERA_SPI_RXDATA); |
| 145 | if (flags & SPI_XFER_BEGIN) |
| 146 | spi_cs_activate(slave); |
| 147 | |
| 148 | while (bytes--) { |
| 149 | uchar d = txp ? *txp++ : CONFIG_ALTERA_SPI_IDLE_VAL; |
| 150 | debug("%s: tx:%x ", __func__, d); |
| 151 | writel(d, altspi->base + ALTERA_SPI_TXDATA); |
| 152 | while (!(readl(altspi->base + ALTERA_SPI_STATUS) & |
| 153 | ALTERA_SPI_STATUS_RRDY_MSK)) |
| 154 | ; |
| 155 | d = readl(altspi->base + ALTERA_SPI_RXDATA); |
| 156 | if (rxp) |
| 157 | *rxp++ = d; |
| 158 | debug("rx:%x\n", d); |
| 159 | } |
| 160 | done: |
| 161 | if (flags & SPI_XFER_END) |
| 162 | spi_cs_deactivate(slave); |
| 163 | |
| 164 | return 0; |
| 165 | } |