Ajay Bhargav | 51100cf | 2011-10-03 14:00:57 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 |
| 3 | * eInfochips Ltd. <www.einfochips.com> |
| 4 | * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com> |
| 5 | * |
| 6 | * (C) Copyright 2009 |
| 7 | * Marvell Semiconductor <www.marvell.com> |
| 8 | * Based on SSP driver |
| 9 | * Written-by: Lei Wen <leiwen@marvell.com> |
| 10 | * |
| 11 | * See file CREDITS for list of people who contributed to this |
| 12 | * project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 27 | * MA 02110-1301 USA |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | #include <common.h> |
| 32 | #include <malloc.h> |
| 33 | #include <spi.h> |
| 34 | |
| 35 | #include <asm/io.h> |
| 36 | #include <asm/arch/spi.h> |
| 37 | #include <asm/gpio.h> |
| 38 | |
| 39 | #define to_armd_spi_slave(s) container_of(s, struct armd_spi_slave, slave) |
| 40 | |
| 41 | struct armd_spi_slave { |
| 42 | struct spi_slave slave; |
| 43 | struct ssp_reg *spi_reg; |
| 44 | u32 cr0, cr1; |
| 45 | u32 int_cr1; |
| 46 | u32 clear_sr; |
| 47 | const void *tx; |
| 48 | void *rx; |
| 49 | int gpio_cs_inverted; |
| 50 | }; |
| 51 | |
| 52 | static int spi_armd_write(struct armd_spi_slave *pss) |
| 53 | { |
| 54 | int wait_timeout = SSP_FLUSH_NUM; |
| 55 | while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_TNF)) |
| 56 | ; |
| 57 | if (!wait_timeout) { |
| 58 | debug("%s: timeout error\n", __func__); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | if (pss->tx != NULL) { |
| 63 | writel(*(u8 *)pss->tx, &pss->spi_reg->ssdr); |
| 64 | ++pss->tx; |
| 65 | } else { |
| 66 | writel(0, &pss->spi_reg->ssdr); |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int spi_armd_read(struct armd_spi_slave *pss) |
| 72 | { |
| 73 | int wait_timeout = SSP_FLUSH_NUM; |
| 74 | while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_RNE)) |
| 75 | ; |
| 76 | if (!wait_timeout) { |
| 77 | debug("%s: timeout error\n", __func__); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | if (pss->rx != NULL) { |
| 82 | *(u8 *)pss->rx = readl(&pss->spi_reg->ssdr); |
| 83 | ++pss->rx; |
| 84 | } else { |
| 85 | readl(&pss->spi_reg->ssdr); |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int spi_armd_flush(struct armd_spi_slave *pss) |
| 91 | { |
| 92 | unsigned long limit = SSP_FLUSH_NUM; |
| 93 | |
| 94 | do { |
| 95 | while (readl(&pss->spi_reg->sssr) & SSSR_RNE) |
| 96 | readl(&pss->spi_reg->ssdr); |
| 97 | } while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--); |
| 98 | |
| 99 | writel(SSSR_ROR, &pss->spi_reg->sssr); |
| 100 | |
| 101 | return limit; |
| 102 | } |
| 103 | |
| 104 | void spi_cs_activate(struct spi_slave *slave) |
| 105 | { |
| 106 | struct armd_spi_slave *pss = to_armd_spi_slave(slave); |
| 107 | |
| 108 | gpio_set_value(slave->cs, pss->gpio_cs_inverted); |
| 109 | } |
| 110 | |
| 111 | void spi_cs_deactivate(struct spi_slave *slave) |
| 112 | { |
| 113 | struct armd_spi_slave *pss = to_armd_spi_slave(slave); |
| 114 | |
| 115 | gpio_set_value(slave->cs, !pss->gpio_cs_inverted); |
| 116 | } |
| 117 | |
| 118 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 119 | unsigned int max_hz, unsigned int mode) |
| 120 | { |
| 121 | struct armd_spi_slave *pss; |
| 122 | |
Simon Glass | d3504fe | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 123 | pss = spi_alloc_slave(struct armd_spi_slave, bus, cs); |
Ajay Bhargav | 51100cf | 2011-10-03 14:00:57 +0530 | [diff] [blame] | 124 | if (!pss) |
| 125 | return NULL; |
| 126 | |
Ajay Bhargav | 51100cf | 2011-10-03 14:00:57 +0530 | [diff] [blame] | 127 | pss->spi_reg = (struct ssp_reg *)SSP_REG_BASE(CONFIG_SYS_SSP_PORT); |
| 128 | |
| 129 | pss->cr0 = SSCR0_MOTO | SSCR0_DATASIZE(DEFAULT_WORD_LEN) | SSCR0_SSE; |
| 130 | |
| 131 | pss->cr1 = (SSCR1_RXTRESH(RX_THRESH_DEF) & SSCR1_RFT) | |
| 132 | (SSCR1_TXTRESH(TX_THRESH_DEF) & SSCR1_TFT); |
| 133 | pss->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); |
| 134 | pss->cr1 |= (((mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) |
| 135 | | (((mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); |
| 136 | |
| 137 | pss->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; |
| 138 | pss->clear_sr = SSSR_ROR | SSSR_TINT; |
| 139 | |
| 140 | pss->gpio_cs_inverted = mode & SPI_CS_HIGH; |
| 141 | gpio_set_value(cs, !pss->gpio_cs_inverted); |
| 142 | |
| 143 | return &pss->slave; |
| 144 | } |
| 145 | |
| 146 | void spi_free_slave(struct spi_slave *slave) |
| 147 | { |
| 148 | struct armd_spi_slave *pss = to_armd_spi_slave(slave); |
| 149 | |
| 150 | free(pss); |
| 151 | } |
| 152 | |
| 153 | int spi_claim_bus(struct spi_slave *slave) |
| 154 | { |
| 155 | struct armd_spi_slave *pss = to_armd_spi_slave(slave); |
| 156 | |
| 157 | debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs); |
| 158 | if (spi_armd_flush(pss) == 0) |
| 159 | return -1; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | void spi_release_bus(struct spi_slave *slave) |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 169 | void *din, unsigned long flags) |
| 170 | { |
| 171 | struct armd_spi_slave *pss = to_armd_spi_slave(slave); |
| 172 | uint bytes = bitlen / 8; |
| 173 | unsigned long limit; |
| 174 | int ret = 0; |
| 175 | |
| 176 | if (bitlen == 0) |
| 177 | goto done; |
| 178 | |
| 179 | /* we can only do 8 bit transfers */ |
| 180 | if (bitlen % 8) { |
| 181 | flags |= SPI_XFER_END; |
| 182 | goto done; |
| 183 | } |
| 184 | |
| 185 | if (dout) |
| 186 | pss->tx = dout; |
| 187 | else |
| 188 | pss->tx = NULL; |
| 189 | |
| 190 | if (din) |
| 191 | pss->rx = din; |
| 192 | else |
| 193 | pss->rx = NULL; |
| 194 | |
| 195 | if (flags & SPI_XFER_BEGIN) { |
| 196 | spi_cs_activate(slave); |
| 197 | writel(pss->cr1 | pss->int_cr1, &pss->spi_reg->sscr1); |
| 198 | writel(TIMEOUT_DEF, &pss->spi_reg->ssto); |
| 199 | writel(pss->cr0, &pss->spi_reg->sscr0); |
| 200 | } |
| 201 | |
| 202 | while (bytes--) { |
| 203 | limit = SSP_FLUSH_NUM; |
| 204 | ret = spi_armd_write(pss); |
| 205 | if (ret) |
| 206 | break; |
| 207 | |
| 208 | while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--) |
| 209 | udelay(1); |
| 210 | |
| 211 | ret = spi_armd_read(pss); |
| 212 | if (ret) |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | done: |
| 217 | if (flags & SPI_XFER_END) { |
| 218 | /* Stop SSP */ |
| 219 | writel(pss->clear_sr, &pss->spi_reg->sssr); |
| 220 | clrbits_le32(&pss->spi_reg->sscr1, pss->int_cr1); |
| 221 | writel(0, &pss->spi_reg->ssto); |
| 222 | spi_cs_deactivate(slave); |
| 223 | } |
| 224 | |
| 225 | return ret; |
| 226 | } |