Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Blackfin On-Chip SPI device |
| 3 | * |
Mike Frysinger | b3e82c7 | 2010-05-05 00:56:30 -0400 | [diff] [blame] | 4 | * Copyright (c) 2005-2010 Analog Devices Inc. |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 5 | * |
Jagannadha Sutradharudu Teki | d92799e | 2013-10-14 13:31:24 +0530 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /*#define DEBUG*/ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <malloc.h> |
| 13 | #include <spi.h> |
| 14 | |
| 15 | #include <asm/blackfin.h> |
Sonic Zhang | 983a2a9 | 2014-01-28 13:53:34 +0800 | [diff] [blame] | 16 | #include <asm/clock.h> |
Mike Frysinger | 9c7c615 | 2010-06-02 06:13:50 -0400 | [diff] [blame] | 17 | #include <asm/gpio.h> |
Mike Frysinger | 9fd3607 | 2010-06-02 06:12:47 -0400 | [diff] [blame] | 18 | #include <asm/portmux.h> |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 19 | #include <asm/mach-common/bits/spi.h> |
| 20 | |
| 21 | struct bfin_spi_slave { |
| 22 | struct spi_slave slave; |
| 23 | void *mmr_base; |
| 24 | u16 ctl, baud, flg; |
| 25 | }; |
| 26 | |
| 27 | #define MAKE_SPI_FUNC(mmr, off) \ |
| 28 | static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \ |
| 29 | static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); } |
| 30 | MAKE_SPI_FUNC(SPI_CTL, 0x00) |
| 31 | MAKE_SPI_FUNC(SPI_FLG, 0x04) |
| 32 | MAKE_SPI_FUNC(SPI_STAT, 0x08) |
| 33 | MAKE_SPI_FUNC(SPI_TDBR, 0x0c) |
| 34 | MAKE_SPI_FUNC(SPI_RDBR, 0x10) |
| 35 | MAKE_SPI_FUNC(SPI_BAUD, 0x14) |
| 36 | |
| 37 | #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave) |
| 38 | |
Mike Frysinger | 9c7c615 | 2010-06-02 06:13:50 -0400 | [diff] [blame] | 39 | #define gpio_cs(cs) ((cs) - MAX_CTRL_CS) |
| 40 | #ifdef CONFIG_BFIN_SPI_GPIO_CS |
| 41 | # define is_gpio_cs(cs) ((cs) > MAX_CTRL_CS) |
| 42 | #else |
| 43 | # define is_gpio_cs(cs) 0 |
| 44 | #endif |
| 45 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 46 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 47 | { |
Mike Frysinger | 9c7c615 | 2010-06-02 06:13:50 -0400 | [diff] [blame] | 48 | if (is_gpio_cs(cs)) |
| 49 | return gpio_is_valid(gpio_cs(cs)); |
| 50 | else |
| 51 | return (cs >= 1 && cs <= MAX_CTRL_CS); |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 54 | void spi_cs_activate(struct spi_slave *slave) |
| 55 | { |
| 56 | struct bfin_spi_slave *bss = to_bfin_spi_slave(slave); |
Mike Frysinger | 9c7c615 | 2010-06-02 06:13:50 -0400 | [diff] [blame] | 57 | |
| 58 | if (is_gpio_cs(slave->cs)) { |
| 59 | unsigned int cs = gpio_cs(slave->cs); |
| 60 | gpio_set_value(cs, bss->flg); |
| 61 | debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs)); |
| 62 | } else { |
| 63 | write_SPI_FLG(bss, |
| 64 | (read_SPI_FLG(bss) & |
| 65 | ~((!bss->flg << 8) << slave->cs)) | |
| 66 | (1 << slave->cs)); |
| 67 | debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss)); |
| 68 | } |
| 69 | |
Todor I Mollov | ee9e3c4 | 2009-04-04 06:53:06 -0400 | [diff] [blame] | 70 | SSYNC(); |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 73 | void spi_cs_deactivate(struct spi_slave *slave) |
| 74 | { |
| 75 | struct bfin_spi_slave *bss = to_bfin_spi_slave(slave); |
Todor I Mollov | ee9e3c4 | 2009-04-04 06:53:06 -0400 | [diff] [blame] | 76 | |
Mike Frysinger | 9c7c615 | 2010-06-02 06:13:50 -0400 | [diff] [blame] | 77 | if (is_gpio_cs(slave->cs)) { |
| 78 | unsigned int cs = gpio_cs(slave->cs); |
| 79 | gpio_set_value(cs, !bss->flg); |
| 80 | debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs)); |
| 81 | } else { |
| 82 | u16 flg; |
| 83 | |
| 84 | /* make sure we force the cs to deassert rather than let the |
| 85 | * pin float back up. otherwise, exact timings may not be |
| 86 | * met some of the time leading to random behavior (ugh). |
| 87 | */ |
| 88 | flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs); |
| 89 | write_SPI_FLG(bss, flg); |
| 90 | SSYNC(); |
| 91 | debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss)); |
Todor I Mollov | ee9e3c4 | 2009-04-04 06:53:06 -0400 | [diff] [blame] | 92 | |
Mike Frysinger | 9c7c615 | 2010-06-02 06:13:50 -0400 | [diff] [blame] | 93 | flg &= ~(1 << slave->cs); |
| 94 | write_SPI_FLG(bss, flg); |
| 95 | debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss)); |
| 96 | } |
| 97 | |
Todor I Mollov | ee9e3c4 | 2009-04-04 06:53:06 -0400 | [diff] [blame] | 98 | SSYNC(); |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void spi_init() |
| 102 | { |
| 103 | } |
| 104 | |
Mike Frysinger | 9fd3607 | 2010-06-02 06:12:47 -0400 | [diff] [blame] | 105 | #ifdef SPI_CTL |
| 106 | # define SPI0_CTL SPI_CTL |
| 107 | #endif |
| 108 | |
| 109 | #define SPI_PINS(n) \ |
| 110 | [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 } |
| 111 | static unsigned short pins[][5] = { |
| 112 | #ifdef SPI0_CTL |
| 113 | SPI_PINS(0), |
| 114 | #endif |
| 115 | #ifdef SPI1_CTL |
| 116 | SPI_PINS(1), |
| 117 | #endif |
| 118 | #ifdef SPI2_CTL |
| 119 | SPI_PINS(2), |
| 120 | #endif |
| 121 | }; |
| 122 | |
| 123 | #define SPI_CS_PINS(n) \ |
| 124 | [n] = { \ |
| 125 | P_SPI##n##_SSEL1, P_SPI##n##_SSEL2, P_SPI##n##_SSEL3, \ |
| 126 | P_SPI##n##_SSEL4, P_SPI##n##_SSEL5, P_SPI##n##_SSEL6, \ |
| 127 | P_SPI##n##_SSEL7, \ |
| 128 | } |
| 129 | static const unsigned short cs_pins[][7] = { |
| 130 | #ifdef SPI0_CTL |
| 131 | SPI_CS_PINS(0), |
| 132 | #endif |
| 133 | #ifdef SPI1_CTL |
| 134 | SPI_CS_PINS(1), |
| 135 | #endif |
| 136 | #ifdef SPI2_CTL |
| 137 | SPI_CS_PINS(2), |
| 138 | #endif |
| 139 | }; |
| 140 | |
Thomas Chou | 6594e15 | 2010-12-24 15:16:08 +0800 | [diff] [blame] | 141 | void spi_set_speed(struct spi_slave *slave, uint hz) |
| 142 | { |
| 143 | struct bfin_spi_slave *bss = to_bfin_spi_slave(slave); |
Sonic Zhang | 983a2a9 | 2014-01-28 13:53:34 +0800 | [diff] [blame] | 144 | ulong clk; |
Thomas Chou | 6594e15 | 2010-12-24 15:16:08 +0800 | [diff] [blame] | 145 | u32 baud; |
| 146 | |
Sonic Zhang | 983a2a9 | 2014-01-28 13:53:34 +0800 | [diff] [blame] | 147 | clk = get_spi_clk(); |
Thomas Chou | 6594e15 | 2010-12-24 15:16:08 +0800 | [diff] [blame] | 148 | /* baud should be rounded up */ |
Sonic Zhang | 983a2a9 | 2014-01-28 13:53:34 +0800 | [diff] [blame] | 149 | baud = DIV_ROUND_UP(clk, 2 * hz); |
Thomas Chou | 6594e15 | 2010-12-24 15:16:08 +0800 | [diff] [blame] | 150 | if (baud < 2) |
| 151 | baud = 2; |
| 152 | else if (baud > (u16)-1) |
| 153 | baud = -1; |
| 154 | bss->baud = baud; |
| 155 | } |
| 156 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 157 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 158 | unsigned int max_hz, unsigned int mode) |
| 159 | { |
| 160 | struct bfin_spi_slave *bss; |
| 161 | u32 mmr_base; |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 162 | |
| 163 | if (!spi_cs_is_valid(bus, cs)) |
| 164 | return NULL; |
| 165 | |
| 166 | switch (bus) { |
Mike Frysinger | 9fd3607 | 2010-06-02 06:12:47 -0400 | [diff] [blame] | 167 | #ifdef SPI0_CTL |
Axel Lin | 203e7ba | 2013-12-02 12:57:33 +0800 | [diff] [blame] | 168 | case 0: |
| 169 | mmr_base = SPI0_CTL; break; |
Mike Frysinger | 9fd3607 | 2010-06-02 06:12:47 -0400 | [diff] [blame] | 170 | #endif |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 171 | #ifdef SPI1_CTL |
Axel Lin | 203e7ba | 2013-12-02 12:57:33 +0800 | [diff] [blame] | 172 | case 1: |
| 173 | mmr_base = SPI1_CTL; break; |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 174 | #endif |
| 175 | #ifdef SPI2_CTL |
Axel Lin | 203e7ba | 2013-12-02 12:57:33 +0800 | [diff] [blame] | 176 | case 2: |
| 177 | mmr_base = SPI2_CTL; break; |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 178 | #endif |
Axel Lin | 203e7ba | 2013-12-02 12:57:33 +0800 | [diff] [blame] | 179 | default: |
| 180 | debug("%s: invalid bus %u\n", __func__, bus); |
| 181 | return NULL; |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Simon Glass | d034a95 | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 184 | bss = spi_alloc_slave(struct bfin_spi_slave, bus, cs); |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 185 | if (!bss) |
| 186 | return NULL; |
| 187 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 188 | bss->mmr_base = (void *)mmr_base; |
| 189 | bss->ctl = SPE | MSTR | TDBR_CORE; |
| 190 | if (mode & SPI_CPHA) bss->ctl |= CPHA; |
| 191 | if (mode & SPI_CPOL) bss->ctl |= CPOL; |
| 192 | if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF; |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 193 | bss->flg = mode & SPI_CS_HIGH ? 1 : 0; |
Thomas Chou | 6594e15 | 2010-12-24 15:16:08 +0800 | [diff] [blame] | 194 | spi_set_speed(&bss->slave, max_hz); |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 195 | |
| 196 | debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__, |
Mike Frysinger | 21e470c | 2011-10-03 20:51:13 -0400 | [diff] [blame] | 197 | bus, cs, mmr_base, bss->ctl, bss->baud, bss->flg); |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 198 | |
| 199 | return &bss->slave; |
| 200 | } |
| 201 | |
| 202 | void spi_free_slave(struct spi_slave *slave) |
| 203 | { |
| 204 | struct bfin_spi_slave *bss = to_bfin_spi_slave(slave); |
| 205 | free(bss); |
| 206 | } |
| 207 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 208 | int spi_claim_bus(struct spi_slave *slave) |
| 209 | { |
| 210 | struct bfin_spi_slave *bss = to_bfin_spi_slave(slave); |
| 211 | |
| 212 | debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs); |
| 213 | |
Mike Frysinger | 9c7c615 | 2010-06-02 06:13:50 -0400 | [diff] [blame] | 214 | if (is_gpio_cs(slave->cs)) { |
| 215 | unsigned int cs = gpio_cs(slave->cs); |
| 216 | gpio_request(cs, "bfin-spi"); |
| 217 | gpio_direction_output(cs, !bss->flg); |
| 218 | pins[slave->bus][0] = P_DONTCARE; |
| 219 | } else |
| 220 | pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1]; |
Mike Frysinger | 9fd3607 | 2010-06-02 06:12:47 -0400 | [diff] [blame] | 221 | peripheral_request_list(pins[slave->bus], "bfin-spi"); |
| 222 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 223 | write_SPI_CTL(bss, bss->ctl); |
| 224 | write_SPI_BAUD(bss, bss->baud); |
| 225 | SSYNC(); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | void spi_release_bus(struct spi_slave *slave) |
| 231 | { |
| 232 | struct bfin_spi_slave *bss = to_bfin_spi_slave(slave); |
Mike Frysinger | 9fd3607 | 2010-06-02 06:12:47 -0400 | [diff] [blame] | 233 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 234 | debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs); |
Mike Frysinger | 9fd3607 | 2010-06-02 06:12:47 -0400 | [diff] [blame] | 235 | |
| 236 | peripheral_free_list(pins[slave->bus]); |
Mike Frysinger | 9c7c615 | 2010-06-02 06:13:50 -0400 | [diff] [blame] | 237 | if (is_gpio_cs(slave->cs)) |
| 238 | gpio_free(gpio_cs(slave->cs)); |
Mike Frysinger | 9fd3607 | 2010-06-02 06:12:47 -0400 | [diff] [blame] | 239 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 240 | write_SPI_CTL(bss, 0); |
| 241 | SSYNC(); |
| 242 | } |
| 243 | |
Cliff Cai | e1e54fc | 2009-11-17 09:36:21 +0000 | [diff] [blame] | 244 | #ifndef CONFIG_BFIN_SPI_IDLE_VAL |
| 245 | # define CONFIG_BFIN_SPI_IDLE_VAL 0xff |
| 246 | #endif |
| 247 | |
Mike Frysinger | b3e82c7 | 2010-05-05 00:56:30 -0400 | [diff] [blame] | 248 | static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx, |
| 249 | uint bytes) |
| 250 | { |
Scott Jiang | abcfbf2 | 2011-12-07 14:53:30 -0500 | [diff] [blame] | 251 | /* discard invalid data and clear RXS */ |
| 252 | read_SPI_RDBR(bss); |
Mike Frysinger | b3e82c7 | 2010-05-05 00:56:30 -0400 | [diff] [blame] | 253 | /* todo: take advantage of hardware fifos */ |
| 254 | while (bytes--) { |
| 255 | u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL); |
| 256 | debug("%s: tx:%x ", __func__, value); |
| 257 | write_SPI_TDBR(bss, value); |
| 258 | SSYNC(); |
| 259 | while ((read_SPI_STAT(bss) & TXS)) |
| 260 | if (ctrlc()) |
| 261 | return -1; |
| 262 | while (!(read_SPI_STAT(bss) & SPIF)) |
| 263 | if (ctrlc()) |
| 264 | return -1; |
| 265 | while (!(read_SPI_STAT(bss) & RXS)) |
| 266 | if (ctrlc()) |
| 267 | return -1; |
| 268 | value = read_SPI_RDBR(bss); |
| 269 | if (rx) |
| 270 | *rx++ = value; |
| 271 | debug("rx:%x\n", value); |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 277 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 278 | void *din, unsigned long flags) |
| 279 | { |
| 280 | struct bfin_spi_slave *bss = to_bfin_spi_slave(slave); |
| 281 | const u8 *tx = dout; |
| 282 | u8 *rx = din; |
| 283 | uint bytes = bitlen / 8; |
| 284 | int ret = 0; |
| 285 | |
| 286 | debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__, |
| 287 | slave->bus, slave->cs, bitlen, bytes, flags); |
| 288 | |
| 289 | if (bitlen == 0) |
| 290 | goto done; |
| 291 | |
| 292 | /* we can only do 8 bit transfers */ |
| 293 | if (bitlen % 8) { |
| 294 | flags |= SPI_XFER_END; |
| 295 | goto done; |
| 296 | } |
| 297 | |
| 298 | if (flags & SPI_XFER_BEGIN) |
| 299 | spi_cs_activate(slave); |
| 300 | |
Scott Jiang | 589891e | 2011-12-07 14:19:55 -0500 | [diff] [blame] | 301 | ret = spi_pio_xfer(bss, tx, rx, bytes); |
Mike Frysinger | 431e4bc | 2008-10-14 07:54:09 -0400 | [diff] [blame] | 302 | |
| 303 | done: |
| 304 | if (flags & SPI_XFER_END) |
| 305 | spi_cs_deactivate(slave); |
| 306 | |
| 307 | return ret; |
| 308 | } |