Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2009 |
| 3 | * Marvell Semiconductor <www.marvell.com> |
| 4 | * Written-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 5 | * |
| 6 | * Derived from drivers/spi/mpc8xxx_spi.c |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 24 | * MA 02110-1301 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <malloc.h> |
| 29 | #include <spi.h> |
Lei Wen | 298ae91 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 30 | #include <asm/io.h> |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 31 | #include <asm/arch/kirkwood.h> |
| 32 | #include <asm/arch/spi.h> |
| 33 | #include <asm/arch/mpp.h> |
| 34 | |
| 35 | static struct kwspi_registers *spireg = (struct kwspi_registers *)KW_SPI_BASE; |
| 36 | |
Valentin Longchamp | 1ad0acd | 2012-06-01 01:31:01 +0000 | [diff] [blame] | 37 | u32 cs_spi_mpp_back[2]; |
| 38 | |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 39 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 40 | unsigned int max_hz, unsigned int mode) |
| 41 | { |
| 42 | struct spi_slave *slave; |
| 43 | u32 data; |
Valentin Longchamp | 1ad0acd | 2012-06-01 01:31:01 +0000 | [diff] [blame] | 44 | u32 kwspi_mpp_config[] = { 0, 0 }; |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 45 | |
| 46 | if (!spi_cs_is_valid(bus, cs)) |
| 47 | return NULL; |
| 48 | |
| 49 | slave = malloc(sizeof(struct spi_slave)); |
| 50 | if (!slave) |
| 51 | return NULL; |
| 52 | |
| 53 | slave->bus = bus; |
| 54 | slave->cs = cs; |
| 55 | |
| 56 | writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl); |
| 57 | |
| 58 | /* calculate spi clock prescaller using max_hz */ |
Valentin Longchamp | baddd9f | 2012-08-15 05:31:49 +0000 | [diff] [blame] | 59 | data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10; |
| 60 | data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data; |
| 61 | data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data; |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 62 | |
| 63 | /* program spi clock prescaller using max_hz */ |
| 64 | writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg); |
| 65 | debug("data = 0x%08x \n", data); |
| 66 | |
| 67 | writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause); |
Ian Campbell | 573b62b | 2012-01-12 06:10:22 +0000 | [diff] [blame] | 68 | writel(KWSPI_IRQMASK, &spireg->irq_mask); |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 69 | |
| 70 | /* program mpp registers to select SPI_CSn */ |
| 71 | if (cs) { |
Valentin Longchamp | 1ad0acd | 2012-06-01 01:31:01 +0000 | [diff] [blame] | 72 | kwspi_mpp_config[0] = MPP7_SPI_SCn; |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 73 | } else { |
| 74 | kwspi_mpp_config[0] = MPP0_SPI_SCn; |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 75 | } |
Valentin Longchamp | 1ad0acd | 2012-06-01 01:31:01 +0000 | [diff] [blame] | 76 | kirkwood_mpp_conf(kwspi_mpp_config, cs_spi_mpp_back); |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 77 | |
| 78 | return slave; |
| 79 | } |
| 80 | |
| 81 | void spi_free_slave(struct spi_slave *slave) |
| 82 | { |
Valentin Longchamp | 1ad0acd | 2012-06-01 01:31:01 +0000 | [diff] [blame] | 83 | kirkwood_mpp_conf(cs_spi_mpp_back, NULL); |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 84 | free(slave); |
| 85 | } |
| 86 | |
Valentin Longchamp | db22ce7 | 2012-06-01 01:31:02 +0000 | [diff] [blame] | 87 | #if defined(CONFIG_SYS_KW_SPI_MPP) |
| 88 | u32 spi_mpp_backup[4]; |
| 89 | #endif |
| 90 | |
Valentin Longchamp | 41f94c4 | 2012-06-01 01:31:03 +0000 | [diff] [blame] | 91 | __attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave) |
| 92 | { |
| 93 | return 0; |
| 94 | } |
| 95 | |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 96 | int spi_claim_bus(struct spi_slave *slave) |
| 97 | { |
Valentin Longchamp | db22ce7 | 2012-06-01 01:31:02 +0000 | [diff] [blame] | 98 | #if defined(CONFIG_SYS_KW_SPI_MPP) |
| 99 | u32 config; |
| 100 | u32 spi_mpp_config[4]; |
| 101 | |
| 102 | config = CONFIG_SYS_KW_SPI_MPP; |
| 103 | |
| 104 | if (config & MOSI_MPP6) |
| 105 | spi_mpp_config[0] = MPP6_SPI_MOSI; |
| 106 | else |
| 107 | spi_mpp_config[0] = MPP1_SPI_MOSI; |
| 108 | |
| 109 | if (config & SCK_MPP10) |
| 110 | spi_mpp_config[1] = MPP10_SPI_SCK; |
| 111 | else |
| 112 | spi_mpp_config[1] = MPP2_SPI_SCK; |
| 113 | |
| 114 | if (config & MISO_MPP11) |
| 115 | spi_mpp_config[2] = MPP11_SPI_MISO; |
| 116 | else |
| 117 | spi_mpp_config[2] = MPP3_SPI_MISO; |
| 118 | |
| 119 | spi_mpp_config[3] = 0; |
| 120 | spi_mpp_backup[3] = 0; |
| 121 | |
| 122 | /* set new spi mpp and save current mpp config */ |
| 123 | kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup); |
| 124 | |
| 125 | #endif |
| 126 | |
Valentin Longchamp | 41f94c4 | 2012-06-01 01:31:03 +0000 | [diff] [blame] | 127 | return board_spi_claim_bus(slave); |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 128 | } |
| 129 | |
Valentin Longchamp | 41f94c4 | 2012-06-01 01:31:03 +0000 | [diff] [blame] | 130 | __attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave) |
| 131 | { |
| 132 | } |
| 133 | |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 134 | void spi_release_bus(struct spi_slave *slave) |
| 135 | { |
Valentin Longchamp | db22ce7 | 2012-06-01 01:31:02 +0000 | [diff] [blame] | 136 | #if defined(CONFIG_SYS_KW_SPI_MPP) |
| 137 | kirkwood_mpp_conf(spi_mpp_backup, NULL); |
| 138 | #endif |
Valentin Longchamp | 41f94c4 | 2012-06-01 01:31:03 +0000 | [diff] [blame] | 139 | |
| 140 | board_spi_release_bus(slave); |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | #ifndef CONFIG_SPI_CS_IS_VALID |
| 144 | /* |
| 145 | * you can define this function board specific |
| 146 | * define above CONFIG in board specific config file and |
| 147 | * provide the function in board specific src file |
| 148 | */ |
| 149 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 150 | { |
| 151 | return (bus == 0 && (cs == 0 || cs == 1)); |
| 152 | } |
| 153 | #endif |
| 154 | |
Michael Walle | 86ea45b | 2011-10-18 20:12:00 +0530 | [diff] [blame] | 155 | void spi_init(void) |
| 156 | { |
| 157 | } |
| 158 | |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 159 | void spi_cs_activate(struct spi_slave *slave) |
| 160 | { |
| 161 | writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl); |
| 162 | } |
| 163 | |
| 164 | void spi_cs_deactivate(struct spi_slave *slave) |
| 165 | { |
| 166 | writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl); |
| 167 | } |
| 168 | |
| 169 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 170 | void *din, unsigned long flags) |
| 171 | { |
| 172 | unsigned int tmpdout, tmpdin; |
| 173 | int tm, isread = 0; |
| 174 | |
Marek Vasut | 922a3a9 | 2011-10-24 23:39:59 +0000 | [diff] [blame] | 175 | debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n", |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 176 | slave->bus, slave->cs, dout, din, bitlen); |
| 177 | |
| 178 | if (flags & SPI_XFER_BEGIN) |
| 179 | spi_cs_activate(slave); |
| 180 | |
| 181 | /* |
| 182 | * handle data in 8-bit chunks |
| 183 | * TBD: 2byte xfer mode to be enabled |
| 184 | */ |
| 185 | writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) | |
| 186 | KWSPI_XFERLEN_1BYTE), &spireg->cfg); |
| 187 | |
| 188 | while (bitlen > 4) { |
| 189 | debug("loopstart bitlen %d\n", bitlen); |
| 190 | tmpdout = 0; |
| 191 | |
| 192 | /* Shift data so it's msb-justified */ |
| 193 | if (dout) |
| 194 | tmpdout = *(u32 *) dout & 0x0ff; |
| 195 | |
| 196 | writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause); |
| 197 | writel(tmpdout, &spireg->dout); /* Write the data out */ |
| 198 | debug("*** spi_xfer: ... %08x written, bitlen %d\n", |
| 199 | tmpdout, bitlen); |
| 200 | |
| 201 | /* |
| 202 | * Wait for SPI transmit to get out |
| 203 | * or time out (1 second = 1000 ms) |
| 204 | * The NE event must be read and cleared first |
| 205 | */ |
| 206 | for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) { |
| 207 | if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) { |
| 208 | isread = 1; |
| 209 | tmpdin = readl(&spireg->din); |
| 210 | debug |
Marek Vasut | 922a3a9 | 2011-10-24 23:39:59 +0000 | [diff] [blame] | 211 | ("spi_xfer: din %p..%08x read\n", |
Prafulla Wadaskar | 1fd6a9b | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 212 | din, tmpdin); |
| 213 | |
| 214 | if (din) { |
| 215 | *((u8 *) din) = (u8) tmpdin; |
| 216 | din += 1; |
| 217 | } |
| 218 | if (dout) |
| 219 | dout += 1; |
| 220 | bitlen -= 8; |
| 221 | } |
| 222 | if (isread) |
| 223 | break; |
| 224 | } |
| 225 | if (tm >= KWSPI_TIMEOUT) |
| 226 | printf("*** spi_xfer: Time out during SPI transfer\n"); |
| 227 | |
| 228 | debug("loopend bitlen %d\n", bitlen); |
| 229 | } |
| 230 | |
| 231 | if (flags & SPI_XFER_END) |
| 232 | spi_cs_deactivate(slave); |
| 233 | |
| 234 | return 0; |
| 235 | } |