Thomas Chou | dea6386 | 2011-04-11 19:48:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Opencore tiny_spi driver |
| 3 | * |
| 4 | * http://opencores.org/project,tiny_spi |
| 5 | * |
| 6 | * based on bfin_spi.c |
| 7 | * Copyright (c) 2005-2008 Analog Devices Inc. |
| 8 | * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw> |
| 9 | * |
| 10 | * Licensed under the GPL-2 or later. |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <malloc.h> |
| 16 | #include <spi.h> |
| 17 | #include <asm/gpio.h> |
| 18 | |
| 19 | #define TINY_SPI_STATUS_TXE 0x1 |
| 20 | #define TINY_SPI_STATUS_TXR 0x2 |
| 21 | |
| 22 | struct tiny_spi_regs { |
| 23 | unsigned rxdata; /* Rx data reg */ |
| 24 | unsigned txdata; /* Tx data reg */ |
| 25 | unsigned status; /* Status reg */ |
| 26 | unsigned control; /* Control reg */ |
| 27 | unsigned baud; /* Baud reg */ |
| 28 | }; |
| 29 | |
| 30 | struct tiny_spi_host { |
| 31 | uint base; |
| 32 | uint freq; |
| 33 | uint baudwidth; |
| 34 | }; |
| 35 | static const struct tiny_spi_host tiny_spi_host_list[] = |
| 36 | CONFIG_SYS_TINY_SPI_LIST; |
| 37 | |
| 38 | struct tiny_spi_slave { |
| 39 | struct spi_slave slave; |
| 40 | const struct tiny_spi_host *host; |
| 41 | uint mode; |
| 42 | uint baud; |
| 43 | uint flg; |
| 44 | }; |
| 45 | #define to_tiny_spi_slave(s) container_of(s, struct tiny_spi_slave, slave) |
| 46 | |
| 47 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 48 | { |
| 49 | return bus < ARRAY_SIZE(tiny_spi_host_list) && gpio_is_valid(cs); |
| 50 | } |
| 51 | |
| 52 | void spi_cs_activate(struct spi_slave *slave) |
| 53 | { |
| 54 | struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave); |
| 55 | unsigned int cs = slave->cs; |
| 56 | |
| 57 | gpio_set_value(cs, tiny_spi->flg); |
| 58 | debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs)); |
| 59 | } |
| 60 | |
| 61 | void spi_cs_deactivate(struct spi_slave *slave) |
| 62 | { |
| 63 | struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave); |
| 64 | unsigned int cs = slave->cs; |
| 65 | |
| 66 | gpio_set_value(cs, !tiny_spi->flg); |
| 67 | debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs)); |
| 68 | } |
| 69 | |
| 70 | void spi_set_speed(struct spi_slave *slave, uint hz) |
| 71 | { |
| 72 | struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave); |
| 73 | const struct tiny_spi_host *host = tiny_spi->host; |
| 74 | |
| 75 | tiny_spi->baud = min(DIV_ROUND_UP(host->freq, hz * 2), |
| 76 | (1 << host->baudwidth)) - 1; |
| 77 | debug("%s: speed %u actual %u\n", __func__, hz, |
| 78 | host->freq / ((tiny_spi->baud + 1) * 2)); |
| 79 | } |
| 80 | |
| 81 | void spi_init(void) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 86 | unsigned int hz, unsigned int mode) |
| 87 | { |
| 88 | struct tiny_spi_slave *tiny_spi; |
| 89 | |
| 90 | if (!spi_cs_is_valid(bus, cs) || gpio_request(cs, "tiny_spi")) |
| 91 | return NULL; |
| 92 | |
Simon Glass | d3504fe | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 93 | tiny_spi = spi_alloc_slave(struct tiny_spi_slave, bus, cs); |
Thomas Chou | dea6386 | 2011-04-11 19:48:47 +0000 | [diff] [blame] | 94 | if (!tiny_spi) |
| 95 | return NULL; |
Thomas Chou | dea6386 | 2011-04-11 19:48:47 +0000 | [diff] [blame] | 96 | |
Thomas Chou | dea6386 | 2011-04-11 19:48:47 +0000 | [diff] [blame] | 97 | tiny_spi->host = &tiny_spi_host_list[bus]; |
| 98 | tiny_spi->mode = mode & (SPI_CPOL | SPI_CPHA); |
| 99 | tiny_spi->flg = mode & SPI_CS_HIGH ? 1 : 0; |
| 100 | spi_set_speed(&tiny_spi->slave, hz); |
| 101 | |
| 102 | debug("%s: bus:%i cs:%i base:%lx\n", __func__, |
| 103 | bus, cs, tiny_spi->host->base); |
| 104 | return &tiny_spi->slave; |
| 105 | } |
| 106 | |
| 107 | void spi_free_slave(struct spi_slave *slave) |
| 108 | { |
| 109 | struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave); |
| 110 | |
| 111 | gpio_free(slave->cs); |
| 112 | free(tiny_spi); |
| 113 | } |
| 114 | |
| 115 | int spi_claim_bus(struct spi_slave *slave) |
| 116 | { |
| 117 | struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave); |
| 118 | struct tiny_spi_regs *regs = (void *)tiny_spi->host->base; |
| 119 | |
| 120 | debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs); |
| 121 | gpio_direction_output(slave->cs, !tiny_spi->flg); |
| 122 | writel(tiny_spi->mode, ®s->control); |
| 123 | writel(tiny_spi->baud, ®s->baud); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | void spi_release_bus(struct spi_slave *slave) |
| 128 | { |
| 129 | debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs); |
| 130 | } |
| 131 | |
| 132 | #ifndef CONFIG_TINY_SPI_IDLE_VAL |
| 133 | # define CONFIG_TINY_SPI_IDLE_VAL 0xff |
| 134 | #endif |
| 135 | |
| 136 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 137 | void *din, unsigned long flags) |
| 138 | { |
| 139 | struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave); |
| 140 | struct tiny_spi_regs *regs = (void *)tiny_spi->host->base; |
| 141 | const u8 *txp = dout; |
| 142 | u8 *rxp = din; |
| 143 | uint bytes = bitlen / 8; |
| 144 | uint i; |
| 145 | |
| 146 | debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__, |
| 147 | slave->bus, slave->cs, bitlen, bytes, flags); |
| 148 | if (bitlen == 0) |
| 149 | goto done; |
| 150 | |
| 151 | /* assume to do 8 bits transfers */ |
| 152 | if (bitlen % 8) { |
| 153 | flags |= SPI_XFER_END; |
| 154 | goto done; |
| 155 | } |
| 156 | |
| 157 | if (flags & SPI_XFER_BEGIN) |
| 158 | spi_cs_activate(slave); |
| 159 | |
| 160 | /* we need to tighten the transfer loop */ |
| 161 | if (txp && rxp) { |
| 162 | writeb(*txp++, ®s->txdata); |
| 163 | if (bytes > 1) { |
| 164 | writeb(*txp++, ®s->txdata); |
| 165 | for (i = 2; i < bytes; i++) { |
| 166 | u8 rx, tx = *txp++; |
| 167 | while (!(readb(®s->status) & |
| 168 | TINY_SPI_STATUS_TXR)) |
| 169 | ; |
| 170 | rx = readb(®s->txdata); |
| 171 | writeb(tx, ®s->txdata); |
| 172 | *rxp++ = rx; |
| 173 | } |
| 174 | while (!(readb(®s->status) & |
| 175 | TINY_SPI_STATUS_TXR)) |
| 176 | ; |
| 177 | *rxp++ = readb(®s->txdata); |
| 178 | } |
| 179 | while (!(readb(®s->status) & |
| 180 | TINY_SPI_STATUS_TXE)) |
| 181 | ; |
| 182 | *rxp++ = readb(®s->rxdata); |
| 183 | } else if (rxp) { |
| 184 | writeb(CONFIG_TINY_SPI_IDLE_VAL, ®s->txdata); |
| 185 | if (bytes > 1) { |
| 186 | writeb(CONFIG_TINY_SPI_IDLE_VAL, |
| 187 | ®s->txdata); |
| 188 | for (i = 2; i < bytes; i++) { |
| 189 | u8 rx; |
| 190 | while (!(readb(®s->status) & |
| 191 | TINY_SPI_STATUS_TXR)) |
| 192 | ; |
| 193 | rx = readb(®s->txdata); |
| 194 | writeb(CONFIG_TINY_SPI_IDLE_VAL, |
| 195 | ®s->txdata); |
| 196 | *rxp++ = rx; |
| 197 | } |
| 198 | while (!(readb(®s->status) & |
| 199 | TINY_SPI_STATUS_TXR)) |
| 200 | ; |
| 201 | *rxp++ = readb(®s->txdata); |
| 202 | } |
| 203 | while (!(readb(®s->status) & |
| 204 | TINY_SPI_STATUS_TXE)) |
| 205 | ; |
| 206 | *rxp++ = readb(®s->rxdata); |
| 207 | } else if (txp) { |
| 208 | writeb(*txp++, ®s->txdata); |
| 209 | if (bytes > 1) { |
| 210 | writeb(*txp++, ®s->txdata); |
| 211 | for (i = 2; i < bytes; i++) { |
| 212 | u8 tx = *txp++; |
| 213 | while (!(readb(®s->status) & |
| 214 | TINY_SPI_STATUS_TXR)) |
| 215 | ; |
| 216 | writeb(tx, ®s->txdata); |
| 217 | } |
| 218 | } |
| 219 | while (!(readb(®s->status) & |
| 220 | TINY_SPI_STATUS_TXE)) |
| 221 | ; |
| 222 | } else { |
| 223 | writeb(CONFIG_TINY_SPI_IDLE_VAL, ®s->txdata); |
| 224 | if (bytes > 1) { |
| 225 | writeb(CONFIG_TINY_SPI_IDLE_VAL, |
| 226 | ®s->txdata); |
| 227 | for (i = 2; i < bytes; i++) { |
| 228 | while (!(readb(®s->status) & |
| 229 | TINY_SPI_STATUS_TXR)) |
| 230 | ; |
| 231 | writeb(CONFIG_TINY_SPI_IDLE_VAL, |
| 232 | ®s->txdata); |
| 233 | } |
| 234 | } |
| 235 | while (!(readb(®s->status) & |
| 236 | TINY_SPI_STATUS_TXE)) |
| 237 | ; |
| 238 | } |
| 239 | |
| 240 | done: |
| 241 | if (flags & SPI_XFER_END) |
| 242 | spi_cs_deactivate(slave); |
| 243 | |
| 244 | return 0; |
| 245 | } |