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