Prafulla Wadaskar | 5710de4 | 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 | a7efd71 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 30 | #include <asm/io.h> |
Prafulla Wadaskar | 5710de4 | 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 | ca88067 | 2012-06-01 01:31:01 +0000 | [diff] [blame] | 37 | u32 cs_spi_mpp_back[2]; |
| 38 | |
Prafulla Wadaskar | 5710de4 | 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; |
Albert ARIBAUD | 9d86f0c | 2012-11-26 11:27:36 +0000 | [diff] [blame] | 44 | static const u32 kwspi_mpp_config[2][2] = { |
| 45 | { MPP0_SPI_SCn, 0 }, /* if cs == 0 */ |
| 46 | { MPP7_SPI_SCn, 0 } /* if cs != 0 */ |
| 47 | }; |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 48 | |
| 49 | if (!spi_cs_is_valid(bus, cs)) |
| 50 | return NULL; |
| 51 | |
| 52 | slave = malloc(sizeof(struct spi_slave)); |
| 53 | if (!slave) |
| 54 | return NULL; |
| 55 | |
| 56 | slave->bus = bus; |
| 57 | slave->cs = cs; |
| 58 | |
| 59 | writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl); |
| 60 | |
| 61 | /* calculate spi clock prescaller using max_hz */ |
Valentin Longchamp | 8203b20 | 2012-08-15 05:31:49 +0000 | [diff] [blame] | 62 | data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10; |
| 63 | data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data; |
| 64 | data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data; |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 65 | |
| 66 | /* program spi clock prescaller using max_hz */ |
| 67 | writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg); |
| 68 | debug("data = 0x%08x \n", data); |
| 69 | |
| 70 | writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause); |
Ian Campbell | 3f84355 | 2012-01-12 06:10:22 +0000 | [diff] [blame] | 71 | writel(KWSPI_IRQMASK, &spireg->irq_mask); |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 72 | |
| 73 | /* program mpp registers to select SPI_CSn */ |
Albert ARIBAUD | 9d86f0c | 2012-11-26 11:27:36 +0000 | [diff] [blame] | 74 | kirkwood_mpp_conf(kwspi_mpp_config[cs ? 1 : 0], cs_spi_mpp_back); |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 75 | |
| 76 | return slave; |
| 77 | } |
| 78 | |
| 79 | void spi_free_slave(struct spi_slave *slave) |
| 80 | { |
Valentin Longchamp | ca88067 | 2012-06-01 01:31:01 +0000 | [diff] [blame] | 81 | kirkwood_mpp_conf(cs_spi_mpp_back, NULL); |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 82 | free(slave); |
| 83 | } |
| 84 | |
Valentin Longchamp | ac486e3 | 2012-06-01 01:31:02 +0000 | [diff] [blame] | 85 | #if defined(CONFIG_SYS_KW_SPI_MPP) |
| 86 | u32 spi_mpp_backup[4]; |
| 87 | #endif |
| 88 | |
Valentin Longchamp | 24934fe | 2012-06-01 01:31:03 +0000 | [diff] [blame] | 89 | __attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave) |
| 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 94 | int spi_claim_bus(struct spi_slave *slave) |
| 95 | { |
Valentin Longchamp | ac486e3 | 2012-06-01 01:31:02 +0000 | [diff] [blame] | 96 | #if defined(CONFIG_SYS_KW_SPI_MPP) |
| 97 | u32 config; |
| 98 | u32 spi_mpp_config[4]; |
| 99 | |
| 100 | config = CONFIG_SYS_KW_SPI_MPP; |
| 101 | |
| 102 | if (config & MOSI_MPP6) |
| 103 | spi_mpp_config[0] = MPP6_SPI_MOSI; |
| 104 | else |
| 105 | spi_mpp_config[0] = MPP1_SPI_MOSI; |
| 106 | |
| 107 | if (config & SCK_MPP10) |
| 108 | spi_mpp_config[1] = MPP10_SPI_SCK; |
| 109 | else |
| 110 | spi_mpp_config[1] = MPP2_SPI_SCK; |
| 111 | |
| 112 | if (config & MISO_MPP11) |
| 113 | spi_mpp_config[2] = MPP11_SPI_MISO; |
| 114 | else |
| 115 | spi_mpp_config[2] = MPP3_SPI_MISO; |
| 116 | |
| 117 | spi_mpp_config[3] = 0; |
| 118 | spi_mpp_backup[3] = 0; |
| 119 | |
| 120 | /* set new spi mpp and save current mpp config */ |
| 121 | kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup); |
| 122 | |
| 123 | #endif |
| 124 | |
Valentin Longchamp | 24934fe | 2012-06-01 01:31:03 +0000 | [diff] [blame] | 125 | return board_spi_claim_bus(slave); |
| 126 | } |
| 127 | |
| 128 | __attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave) |
| 129 | { |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void spi_release_bus(struct spi_slave *slave) |
| 133 | { |
Valentin Longchamp | ac486e3 | 2012-06-01 01:31:02 +0000 | [diff] [blame] | 134 | #if defined(CONFIG_SYS_KW_SPI_MPP) |
| 135 | kirkwood_mpp_conf(spi_mpp_backup, NULL); |
| 136 | #endif |
Valentin Longchamp | 24934fe | 2012-06-01 01:31:03 +0000 | [diff] [blame] | 137 | |
| 138 | board_spi_release_bus(slave); |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | #ifndef CONFIG_SPI_CS_IS_VALID |
| 142 | /* |
| 143 | * you can define this function board specific |
| 144 | * define above CONFIG in board specific config file and |
| 145 | * provide the function in board specific src file |
| 146 | */ |
| 147 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 148 | { |
| 149 | return (bus == 0 && (cs == 0 || cs == 1)); |
| 150 | } |
| 151 | #endif |
| 152 | |
Michael Walle | efa4e43 | 2011-10-18 20:12:00 +0530 | [diff] [blame] | 153 | void spi_init(void) |
| 154 | { |
| 155 | } |
| 156 | |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 157 | void spi_cs_activate(struct spi_slave *slave) |
| 158 | { |
| 159 | writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl); |
| 160 | } |
| 161 | |
| 162 | void spi_cs_deactivate(struct spi_slave *slave) |
| 163 | { |
| 164 | writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl); |
| 165 | } |
| 166 | |
| 167 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 168 | void *din, unsigned long flags) |
| 169 | { |
| 170 | unsigned int tmpdout, tmpdin; |
| 171 | int tm, isread = 0; |
| 172 | |
Marek Vasut | 2e8f641 | 2011-10-24 23:39:59 +0000 | [diff] [blame] | 173 | debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n", |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 174 | slave->bus, slave->cs, dout, din, bitlen); |
| 175 | |
| 176 | if (flags & SPI_XFER_BEGIN) |
| 177 | spi_cs_activate(slave); |
| 178 | |
| 179 | /* |
| 180 | * handle data in 8-bit chunks |
| 181 | * TBD: 2byte xfer mode to be enabled |
| 182 | */ |
| 183 | writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) | |
| 184 | KWSPI_XFERLEN_1BYTE), &spireg->cfg); |
| 185 | |
| 186 | while (bitlen > 4) { |
| 187 | debug("loopstart bitlen %d\n", bitlen); |
| 188 | tmpdout = 0; |
| 189 | |
| 190 | /* Shift data so it's msb-justified */ |
| 191 | if (dout) |
| 192 | tmpdout = *(u32 *) dout & 0x0ff; |
| 193 | |
| 194 | writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause); |
| 195 | writel(tmpdout, &spireg->dout); /* Write the data out */ |
| 196 | debug("*** spi_xfer: ... %08x written, bitlen %d\n", |
| 197 | tmpdout, bitlen); |
| 198 | |
| 199 | /* |
| 200 | * Wait for SPI transmit to get out |
| 201 | * or time out (1 second = 1000 ms) |
| 202 | * The NE event must be read and cleared first |
| 203 | */ |
| 204 | for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) { |
| 205 | if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) { |
| 206 | isread = 1; |
| 207 | tmpdin = readl(&spireg->din); |
| 208 | debug |
Marek Vasut | 2e8f641 | 2011-10-24 23:39:59 +0000 | [diff] [blame] | 209 | ("spi_xfer: din %p..%08x read\n", |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 210 | din, tmpdin); |
| 211 | |
| 212 | if (din) { |
| 213 | *((u8 *) din) = (u8) tmpdin; |
| 214 | din += 1; |
| 215 | } |
| 216 | if (dout) |
| 217 | dout += 1; |
| 218 | bitlen -= 8; |
| 219 | } |
| 220 | if (isread) |
| 221 | break; |
| 222 | } |
| 223 | if (tm >= KWSPI_TIMEOUT) |
| 224 | printf("*** spi_xfer: Time out during SPI transfer\n"); |
| 225 | |
| 226 | debug("loopend bitlen %d\n", bitlen); |
| 227 | } |
| 228 | |
| 229 | if (flags & SPI_XFER_END) |
| 230 | spi_cs_deactivate(slave); |
| 231 | |
| 232 | return 0; |
| 233 | } |