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> 30 #include <asm/io.h> 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 37 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, 38 unsigned int max_hz, unsigned int mode) 39 { 40 struct spi_slave *slave; 41 u32 data; 42 u32 kwspi_mpp_config[] = { 43 MPP0_GPIO, 44 MPP7_SPI_SCn, 45 0 46 }; 47 48 if (!spi_cs_is_valid(bus, cs)) 49 return NULL; 50 51 slave = malloc(sizeof(struct spi_slave)); 52 if (!slave) 53 return NULL; 54 55 slave->bus = bus; 56 slave->cs = cs; 57 58 writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl); 59 60 /* calculate spi clock prescaller using max_hz */ 61 data = ((CONFIG_SYS_TCLK / 2) / max_hz) & KWSPI_CLKPRESCL_MASK; 62 data |= 0x10; 63 64 /* program spi clock prescaller using max_hz */ 65 writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg); 66 debug("data = 0x%08x \n", data); 67 68 writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause); 69 writel(KWSPI_IRQMASK, spireg->irq_mask); 70 71 /* program mpp registers to select SPI_CSn */ 72 if (cs) { 73 kwspi_mpp_config[0] = MPP0_GPIO; 74 kwspi_mpp_config[1] = MPP7_SPI_SCn; 75 } else { 76 kwspi_mpp_config[0] = MPP0_SPI_SCn; 77 kwspi_mpp_config[1] = MPP7_GPO; 78 } 79 kirkwood_mpp_conf(kwspi_mpp_config); 80 81 return slave; 82 } 83 84 void spi_free_slave(struct spi_slave *slave) 85 { 86 free(slave); 87 } 88 89 int spi_claim_bus(struct spi_slave *slave) 90 { 91 return 0; 92 } 93 94 void spi_release_bus(struct spi_slave *slave) 95 { 96 } 97 98 #ifndef CONFIG_SPI_CS_IS_VALID 99 /* 100 * you can define this function board specific 101 * define above CONFIG in board specific config file and 102 * provide the function in board specific src file 103 */ 104 int spi_cs_is_valid(unsigned int bus, unsigned int cs) 105 { 106 return (bus == 0 && (cs == 0 || cs == 1)); 107 } 108 #endif 109 110 void spi_cs_activate(struct spi_slave *slave) 111 { 112 writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl); 113 } 114 115 void spi_cs_deactivate(struct spi_slave *slave) 116 { 117 writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl); 118 } 119 120 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, 121 void *din, unsigned long flags) 122 { 123 unsigned int tmpdout, tmpdin; 124 int tm, isread = 0; 125 126 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n", 127 slave->bus, slave->cs, dout, din, bitlen); 128 129 if (flags & SPI_XFER_BEGIN) 130 spi_cs_activate(slave); 131 132 /* 133 * handle data in 8-bit chunks 134 * TBD: 2byte xfer mode to be enabled 135 */ 136 writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) | 137 KWSPI_XFERLEN_1BYTE), &spireg->cfg); 138 139 while (bitlen > 4) { 140 debug("loopstart bitlen %d\n", bitlen); 141 tmpdout = 0; 142 143 /* Shift data so it's msb-justified */ 144 if (dout) 145 tmpdout = *(u32 *) dout & 0x0ff; 146 147 writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause); 148 writel(tmpdout, &spireg->dout); /* Write the data out */ 149 debug("*** spi_xfer: ... %08x written, bitlen %d\n", 150 tmpdout, bitlen); 151 152 /* 153 * Wait for SPI transmit to get out 154 * or time out (1 second = 1000 ms) 155 * The NE event must be read and cleared first 156 */ 157 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) { 158 if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) { 159 isread = 1; 160 tmpdin = readl(&spireg->din); 161 debug 162 ("spi_xfer: din %08x..%08x read\n", 163 din, tmpdin); 164 165 if (din) { 166 *((u8 *) din) = (u8) tmpdin; 167 din += 1; 168 } 169 if (dout) 170 dout += 1; 171 bitlen -= 8; 172 } 173 if (isread) 174 break; 175 } 176 if (tm >= KWSPI_TIMEOUT) 177 printf("*** spi_xfer: Time out during SPI transfer\n"); 178 179 debug("loopend bitlen %d\n", bitlen); 180 } 181 182 if (flags & SPI_XFER_END) 183 spi_cs_deactivate(slave); 184 185 return 0; 186 } 187