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