Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 7 | #include <dm.h> |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 8 | #include <malloc.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 9 | #include <spi.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 12 | #include <asm/gpio.h> |
Stefano Babic | 8627111 | 2011-03-14 15:43:56 +0100 | [diff] [blame] | 13 | #include <asm/arch/imx-regs.h> |
| 14 | #include <asm/arch/clock.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 15 | #include <asm/mach-imx/spi.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 16 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 19 | #ifdef CONFIG_MX27 |
| 20 | /* i.MX27 has a completely wrong register layout and register definitions in the |
| 21 | * datasheet, the correct one is in the Freescale's Linux driver */ |
| 22 | |
Helmut Raiger | 61a58a1 | 2011-06-15 01:45:45 +0000 | [diff] [blame] | 23 | #error "i.MX27 CSPI not supported due to drastic differences in register definitions" \ |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 24 | "See linux mxc_spi driver from Freescale for details." |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 25 | #endif |
| 26 | |
Nikita Kiryanov | 155fa9a | 2014-08-20 15:08:50 +0300 | [diff] [blame] | 27 | __weak int board_spi_cs_gpio(unsigned bus, unsigned cs) |
| 28 | { |
| 29 | return -1; |
| 30 | } |
| 31 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 32 | #define OUT MXC_GPIO_DIRECTION_OUT |
| 33 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 34 | #define reg_read readl |
| 35 | #define reg_write(a, v) writel(v, a) |
| 36 | |
Heiko Schocher | f659b57 | 2014-07-14 10:22:11 +0200 | [diff] [blame] | 37 | #if !defined(CONFIG_SYS_SPI_MXC_WAIT) |
| 38 | #define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */ |
| 39 | #endif |
| 40 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 41 | struct mxc_spi_slave { |
| 42 | struct spi_slave slave; |
| 43 | unsigned long base; |
| 44 | u32 ctrl_reg; |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 45 | #if defined(MXC_ECSPI) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 46 | u32 cfg_reg; |
| 47 | #endif |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 48 | int gpio; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 49 | int ss_pol; |
Markus Niebel | 027a9a0 | 2014-10-23 16:09:39 +0200 | [diff] [blame] | 50 | unsigned int max_hz; |
| 51 | unsigned int mode; |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 52 | struct gpio_desc ss; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 53 | }; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 54 | |
| 55 | static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave) |
| 56 | { |
| 57 | return container_of(slave, struct mxc_spi_slave, slave); |
| 58 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 59 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 60 | static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 61 | { |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 62 | if (CONFIG_IS_ENABLED(DM_SPI)) { |
Michael Trimarchi | 34ad749 | 2018-06-20 22:51:18 +0200 | [diff] [blame] | 63 | dm_gpio_set_value(&mxcs->ss, 1); |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 64 | } else { |
| 65 | if (mxcs->gpio > 0) |
| 66 | gpio_set_value(mxcs->gpio, mxcs->ss_pol); |
| 67 | } |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 70 | static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 71 | { |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 72 | if (CONFIG_IS_ENABLED(DM_SPI)) { |
Michael Trimarchi | 34ad749 | 2018-06-20 22:51:18 +0200 | [diff] [blame] | 73 | dm_gpio_set_value(&mxcs->ss, 0); |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 74 | } else { |
| 75 | if (mxcs->gpio > 0) |
| 76 | gpio_set_value(mxcs->gpio, !(mxcs->ss_pol)); |
| 77 | } |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 80 | u32 get_cspi_div(u32 div) |
| 81 | { |
| 82 | int i; |
| 83 | |
| 84 | for (i = 0; i < 8; i++) { |
| 85 | if (div <= (4 << i)) |
| 86 | return i; |
| 87 | } |
| 88 | return i; |
| 89 | } |
| 90 | |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 91 | #ifdef MXC_CSPI |
Markus Niebel | 027a9a0 | 2014-10-23 16:09:39 +0200 | [diff] [blame] | 92 | static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs) |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 93 | { |
| 94 | unsigned int ctrl_reg; |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 95 | u32 clk_src; |
| 96 | u32 div; |
Markus Niebel | 027a9a0 | 2014-10-23 16:09:39 +0200 | [diff] [blame] | 97 | unsigned int max_hz = mxcs->max_hz; |
| 98 | unsigned int mode = mxcs->mode; |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 99 | |
| 100 | clk_src = mxc_get_clock(MXC_CSPI_CLK); |
| 101 | |
Benoît Thébaudeau | cd20040 | 2012-08-10 08:51:50 +0000 | [diff] [blame] | 102 | div = DIV_ROUND_UP(clk_src, max_hz); |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 103 | div = get_cspi_div(div); |
| 104 | |
| 105 | debug("clk %d Hz, div %d, real clk %d Hz\n", |
| 106 | max_hz, div, clk_src / (4 << div)); |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 107 | |
| 108 | ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) | |
| 109 | MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) | |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 110 | MXC_CSPICTRL_DATARATE(div) | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 111 | MXC_CSPICTRL_EN | |
| 112 | #ifdef CONFIG_MX35 |
| 113 | MXC_CSPICTRL_SSCTL | |
| 114 | #endif |
| 115 | MXC_CSPICTRL_MODE; |
| 116 | |
| 117 | if (mode & SPI_CPHA) |
| 118 | ctrl_reg |= MXC_CSPICTRL_PHA; |
| 119 | if (mode & SPI_CPOL) |
| 120 | ctrl_reg |= MXC_CSPICTRL_POL; |
| 121 | if (mode & SPI_CS_HIGH) |
| 122 | ctrl_reg |= MXC_CSPICTRL_SSPOL; |
| 123 | mxcs->ctrl_reg = ctrl_reg; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | #endif |
| 128 | |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 129 | #ifdef MXC_ECSPI |
Markus Niebel | 027a9a0 | 2014-10-23 16:09:39 +0200 | [diff] [blame] | 130 | static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 131 | { |
| 132 | u32 clk_src = mxc_get_clock(MXC_CSPI_CLK); |
Dirk Behme | 9a30903 | 2013-05-11 07:25:54 +0200 | [diff] [blame] | 133 | s32 reg_ctrl, reg_config; |
Markus Niebel | 5d584cc | 2014-02-17 17:33:17 +0100 | [diff] [blame] | 134 | u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0; |
| 135 | u32 pre_div = 0, post_div = 0; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 136 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
Markus Niebel | 027a9a0 | 2014-10-23 16:09:39 +0200 | [diff] [blame] | 137 | unsigned int max_hz = mxcs->max_hz; |
| 138 | unsigned int mode = mxcs->mode; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 139 | |
Fabio Estevam | 0f1411b | 2013-04-09 13:06:25 +0000 | [diff] [blame] | 140 | /* |
| 141 | * Reset SPI and set all CSs to master mode, if toggling |
| 142 | * between slave and master mode we might see a glitch |
| 143 | * on the clock line |
| 144 | */ |
| 145 | reg_ctrl = MXC_CSPICTRL_MODE_MASK; |
| 146 | reg_write(®s->ctrl, reg_ctrl); |
| 147 | reg_ctrl |= MXC_CSPICTRL_EN; |
| 148 | reg_write(®s->ctrl, reg_ctrl); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 149 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 150 | if (clk_src > max_hz) { |
Dirk Behme | 9a30903 | 2013-05-11 07:25:54 +0200 | [diff] [blame] | 151 | pre_div = (clk_src - 1) / max_hz; |
| 152 | /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */ |
| 153 | post_div = fls(pre_div); |
| 154 | if (post_div > 4) { |
| 155 | post_div -= 4; |
| 156 | if (post_div >= 16) { |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 157 | printf("Error: no divider for the freq: %d\n", |
| 158 | max_hz); |
| 159 | return -1; |
| 160 | } |
Dirk Behme | 9a30903 | 2013-05-11 07:25:54 +0200 | [diff] [blame] | 161 | pre_div >>= post_div; |
| 162 | } else { |
| 163 | post_div = 0; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | debug("pre_div = %d, post_div=%d\n", pre_div, post_div); |
| 168 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) | |
| 169 | MXC_CSPICTRL_SELCHAN(cs); |
| 170 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) | |
| 171 | MXC_CSPICTRL_PREDIV(pre_div); |
| 172 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) | |
| 173 | MXC_CSPICTRL_POSTDIV(post_div); |
| 174 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 175 | if (mode & SPI_CS_HIGH) |
| 176 | ss_pol = 1; |
| 177 | |
Markus Niebel | 5d584cc | 2014-02-17 17:33:17 +0100 | [diff] [blame] | 178 | if (mode & SPI_CPOL) { |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 179 | sclkpol = 1; |
Markus Niebel | 5d584cc | 2014-02-17 17:33:17 +0100 | [diff] [blame] | 180 | sclkctl = 1; |
| 181 | } |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 182 | |
| 183 | if (mode & SPI_CPHA) |
| 184 | sclkpha = 1; |
| 185 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 186 | reg_config = reg_read(®s->cfg); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * Configuration register setup |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 190 | * The MX51 supports different setup for each SS |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 191 | */ |
| 192 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) | |
| 193 | (ss_pol << (cs + MXC_CSPICON_SSPOL)); |
| 194 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) | |
| 195 | (sclkpol << (cs + MXC_CSPICON_POL)); |
Markus Niebel | 5d584cc | 2014-02-17 17:33:17 +0100 | [diff] [blame] | 196 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) | |
| 197 | (sclkctl << (cs + MXC_CSPICON_CTL)); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 198 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) | |
| 199 | (sclkpha << (cs + MXC_CSPICON_PHA)); |
| 200 | |
| 201 | debug("reg_ctrl = 0x%x\n", reg_ctrl); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 202 | reg_write(®s->ctrl, reg_ctrl); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 203 | debug("reg_config = 0x%x\n", reg_config); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 204 | reg_write(®s->cfg, reg_config); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 205 | |
| 206 | /* save config register and control register */ |
| 207 | mxcs->ctrl_reg = reg_ctrl; |
| 208 | mxcs->cfg_reg = reg_config; |
| 209 | |
| 210 | /* clear interrupt reg */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 211 | reg_write(®s->intr, 0); |
| 212 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | #endif |
| 217 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 218 | int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen, |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 219 | const u8 *dout, u8 *din, unsigned long flags) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 220 | { |
Axel Lin | 9675fed | 2013-06-14 21:13:32 +0800 | [diff] [blame] | 221 | int nbytes = DIV_ROUND_UP(bitlen, 8); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 222 | u32 data, cnt, i; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 223 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
Heiko Schocher | f659b57 | 2014-07-14 10:22:11 +0200 | [diff] [blame] | 224 | u32 ts; |
| 225 | int status; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 226 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 227 | debug("%s: bitlen %d dout 0x%x din 0x%x\n", |
| 228 | __func__, bitlen, (u32)dout, (u32)din); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 229 | |
| 230 | mxcs->ctrl_reg = (mxcs->ctrl_reg & |
| 231 | ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) | |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 232 | MXC_CSPICTRL_BITCOUNT(bitlen - 1); |
| 233 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 234 | reg_write(®s->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN); |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 235 | #ifdef MXC_ECSPI |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 236 | reg_write(®s->cfg, mxcs->cfg_reg); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 237 | #endif |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 238 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 239 | /* Clear interrupt register */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 240 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 241 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 242 | /* |
| 243 | * The SPI controller works only with words, |
| 244 | * check if less than a word is sent. |
| 245 | * Access to the FIFO is only 32 bit |
| 246 | */ |
| 247 | if (bitlen % 32) { |
| 248 | data = 0; |
| 249 | cnt = (bitlen % 32) / 8; |
| 250 | if (dout) { |
| 251 | for (i = 0; i < cnt; i++) { |
| 252 | data = (data << 8) | (*dout++ & 0xFF); |
| 253 | } |
| 254 | } |
| 255 | debug("Sending SPI 0x%x\n", data); |
| 256 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 257 | reg_write(®s->txdata, data); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 258 | nbytes -= cnt; |
| 259 | } |
| 260 | |
| 261 | data = 0; |
| 262 | |
| 263 | while (nbytes > 0) { |
| 264 | data = 0; |
| 265 | if (dout) { |
| 266 | /* Buffer is not 32-bit aligned */ |
| 267 | if ((unsigned long)dout & 0x03) { |
| 268 | data = 0; |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 269 | for (i = 0; i < 4; i++) |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 270 | data = (data << 8) | (*dout++ & 0xFF); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 271 | } else { |
| 272 | data = *(u32 *)dout; |
| 273 | data = cpu_to_be32(data); |
Timo Herbrecher | 6d5ce1b | 2013-10-16 00:05:09 +0530 | [diff] [blame] | 274 | dout += 4; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 275 | } |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 276 | } |
| 277 | debug("Sending SPI 0x%x\n", data); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 278 | reg_write(®s->txdata, data); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 279 | nbytes -= 4; |
| 280 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 281 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 282 | /* FIFO is written, now starts the transfer setting the XCH bit */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 283 | reg_write(®s->ctrl, mxcs->ctrl_reg | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 284 | MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 285 | |
Heiko Schocher | f659b57 | 2014-07-14 10:22:11 +0200 | [diff] [blame] | 286 | ts = get_timer(0); |
| 287 | status = reg_read(®s->stat); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 288 | /* Wait until the TC (Transfer completed) bit is set */ |
Heiko Schocher | f659b57 | 2014-07-14 10:22:11 +0200 | [diff] [blame] | 289 | while ((status & MXC_CSPICTRL_TC) == 0) { |
| 290 | if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) { |
| 291 | printf("spi_xchg_single: Timeout!\n"); |
| 292 | return -1; |
| 293 | } |
| 294 | status = reg_read(®s->stat); |
| 295 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 296 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 297 | /* Transfer completed, clear any pending request */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 298 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 299 | |
Axel Lin | 9675fed | 2013-06-14 21:13:32 +0800 | [diff] [blame] | 300 | nbytes = DIV_ROUND_UP(bitlen, 8); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 301 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 302 | cnt = nbytes % 32; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 303 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 304 | if (bitlen % 32) { |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 305 | data = reg_read(®s->rxdata); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 306 | cnt = (bitlen % 32) / 8; |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 307 | data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 308 | debug("SPI Rx unaligned: 0x%x\n", data); |
| 309 | if (din) { |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 310 | memcpy(din, &data, cnt); |
| 311 | din += cnt; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 312 | } |
| 313 | nbytes -= cnt; |
| 314 | } |
| 315 | |
| 316 | while (nbytes > 0) { |
| 317 | u32 tmp; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 318 | tmp = reg_read(®s->rxdata); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 319 | data = cpu_to_be32(tmp); |
| 320 | debug("SPI Rx: 0x%x 0x%x\n", tmp, data); |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 321 | cnt = min_t(u32, nbytes, sizeof(data)); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 322 | if (din) { |
| 323 | memcpy(din, &data, cnt); |
| 324 | din += cnt; |
| 325 | } |
| 326 | nbytes -= cnt; |
| 327 | } |
| 328 | |
| 329 | return 0; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 330 | |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 333 | static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs, |
| 334 | unsigned int bitlen, const void *dout, |
| 335 | void *din, unsigned long flags) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 336 | { |
Axel Lin | 9675fed | 2013-06-14 21:13:32 +0800 | [diff] [blame] | 337 | int n_bytes = DIV_ROUND_UP(bitlen, 8); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 338 | int n_bits; |
| 339 | int ret; |
| 340 | u32 blk_size; |
| 341 | u8 *p_outbuf = (u8 *)dout; |
| 342 | u8 *p_inbuf = (u8 *)din; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 343 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 344 | if (!mxcs) |
| 345 | return -EINVAL; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 346 | |
| 347 | if (flags & SPI_XFER_BEGIN) |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 348 | mxc_spi_cs_activate(mxcs); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 349 | |
| 350 | while (n_bytes > 0) { |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 351 | if (n_bytes < MAX_SPI_BYTES) |
| 352 | blk_size = n_bytes; |
| 353 | else |
| 354 | blk_size = MAX_SPI_BYTES; |
| 355 | |
| 356 | n_bits = blk_size * 8; |
| 357 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 358 | ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 359 | |
| 360 | if (ret) |
| 361 | return ret; |
| 362 | if (dout) |
| 363 | p_outbuf += blk_size; |
| 364 | if (din) |
| 365 | p_inbuf += blk_size; |
| 366 | n_bytes -= blk_size; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 369 | if (flags & SPI_XFER_END) { |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 370 | mxc_spi_cs_deactivate(mxcs); |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 371 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 376 | static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs) |
| 377 | { |
| 378 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
| 379 | int ret; |
| 380 | |
| 381 | reg_write(®s->rxdata, 1); |
| 382 | udelay(1); |
| 383 | ret = spi_cfg_mxc(mxcs, cs); |
| 384 | if (ret) { |
| 385 | printf("mxc_spi: cannot setup SPI controller\n"); |
| 386 | return ret; |
| 387 | } |
| 388 | reg_write(®s->period, MXC_CSPIPERIOD_32KHZ); |
| 389 | reg_write(®s->intr, 0); |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | #ifndef CONFIG_DM_SPI |
| 395 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 396 | void *din, unsigned long flags) |
| 397 | { |
| 398 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 399 | |
| 400 | return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags); |
| 401 | } |
| 402 | |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 403 | void spi_init(void) |
| 404 | { |
| 405 | } |
| 406 | |
Nikita Kiryanov | 155fa9a | 2014-08-20 15:08:50 +0300 | [diff] [blame] | 407 | /* |
| 408 | * Some SPI devices require active chip-select over multiple |
| 409 | * transactions, we achieve this using a GPIO. Still, the SPI |
| 410 | * controller has to be configured to use one of its own chipselects. |
| 411 | * To use this feature you have to implement board_spi_cs_gpio() to assign |
| 412 | * a gpio value for each cs (-1 if cs doesn't need to use gpio). |
| 413 | * You must use some unused on this SPI controller cs between 0 and 3. |
| 414 | */ |
| 415 | static int setup_cs_gpio(struct mxc_spi_slave *mxcs, |
| 416 | unsigned int bus, unsigned int cs) |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 417 | { |
| 418 | int ret; |
| 419 | |
Nikita Kiryanov | 155fa9a | 2014-08-20 15:08:50 +0300 | [diff] [blame] | 420 | mxcs->gpio = board_spi_cs_gpio(bus, cs); |
| 421 | if (mxcs->gpio == -1) |
| 422 | return 0; |
| 423 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 424 | gpio_request(mxcs->gpio, "spi-cs"); |
Nikita Kiryanov | 155fa9a | 2014-08-20 15:08:50 +0300 | [diff] [blame] | 425 | ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol)); |
| 426 | if (ret) { |
| 427 | printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio); |
| 428 | return -EINVAL; |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Nikita Kiryanov | 155fa9a | 2014-08-20 15:08:50 +0300 | [diff] [blame] | 431 | return 0; |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 434 | static unsigned long spi_bases[] = { |
| 435 | MXC_SPI_BASE_ADDRESSES |
| 436 | }; |
| 437 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 438 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 439 | unsigned int max_hz, unsigned int mode) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 440 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 441 | struct mxc_spi_slave *mxcs; |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 442 | int ret; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 443 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 444 | if (bus >= ARRAY_SIZE(spi_bases)) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 445 | return NULL; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 446 | |
Markus Niebel | 027a9a0 | 2014-10-23 16:09:39 +0200 | [diff] [blame] | 447 | if (max_hz == 0) { |
| 448 | printf("Error: desired clock is 0\n"); |
| 449 | return NULL; |
| 450 | } |
| 451 | |
Simon Glass | d3504fe | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 452 | mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 453 | if (!mxcs) { |
| 454 | puts("mxc_spi: SPI Slave not allocated !\n"); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 455 | return NULL; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 456 | } |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 457 | |
Fabio Estevam | de5bf02 | 2012-11-15 11:23:23 +0000 | [diff] [blame] | 458 | mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0; |
| 459 | |
Nikita Kiryanov | 155fa9a | 2014-08-20 15:08:50 +0300 | [diff] [blame] | 460 | ret = setup_cs_gpio(mxcs, bus, cs); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 461 | if (ret < 0) { |
| 462 | free(mxcs); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 466 | mxcs->base = spi_bases[bus]; |
Markus Niebel | 027a9a0 | 2014-10-23 16:09:39 +0200 | [diff] [blame] | 467 | mxcs->max_hz = max_hz; |
| 468 | mxcs->mode = mode; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 469 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 470 | return &mxcs->slave; |
| 471 | } |
| 472 | |
| 473 | void spi_free_slave(struct spi_slave *slave) |
| 474 | { |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 475 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 476 | |
| 477 | free(mxcs); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | int spi_claim_bus(struct spi_slave *slave) |
| 481 | { |
| 482 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 483 | |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 484 | return mxc_spi_claim_bus_internal(mxcs, slave->cs); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 485 | } |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 486 | |
| 487 | void spi_release_bus(struct spi_slave *slave) |
| 488 | { |
| 489 | /* TODO: Shut the controller down */ |
| 490 | } |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 491 | #else |
| 492 | |
| 493 | static int mxc_spi_probe(struct udevice *bus) |
| 494 | { |
| 495 | struct mxc_spi_slave *plat = bus->platdata; |
| 496 | struct mxc_spi_slave *mxcs = dev_get_platdata(bus); |
| 497 | int node = dev_of_offset(bus); |
| 498 | const void *blob = gd->fdt_blob; |
| 499 | int ret; |
| 500 | |
| 501 | if (gpio_request_by_name(bus, "cs-gpios", 0, &plat->ss, |
| 502 | GPIOD_IS_OUT)) { |
| 503 | dev_err(bus, "No cs-gpios property\n"); |
| 504 | return -EINVAL; |
| 505 | } |
| 506 | |
Michael Trimarchi | 618e8e2 | 2018-06-20 22:51:17 +0200 | [diff] [blame] | 507 | plat->base = devfdt_get_addr(bus); |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 508 | if (plat->base == FDT_ADDR_T_NONE) |
| 509 | return -ENODEV; |
| 510 | |
Michael Trimarchi | 34ad749 | 2018-06-20 22:51:18 +0200 | [diff] [blame] | 511 | ret = dm_gpio_set_value(&plat->ss, 0); |
Peng Fan | 994266b | 2017-08-09 13:09:33 +0800 | [diff] [blame] | 512 | if (ret) { |
| 513 | dev_err(bus, "Setting cs error\n"); |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 518 | 20000000); |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 524 | const void *dout, void *din, unsigned long flags) |
| 525 | { |
| 526 | struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent); |
| 527 | |
| 528 | |
| 529 | return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags); |
| 530 | } |
| 531 | |
| 532 | static int mxc_spi_claim_bus(struct udevice *dev) |
| 533 | { |
| 534 | struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent); |
| 535 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
| 536 | |
| 537 | return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs); |
| 538 | } |
| 539 | |
| 540 | static int mxc_spi_release_bus(struct udevice *dev) |
| 541 | { |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int mxc_spi_set_speed(struct udevice *bus, uint speed) |
| 546 | { |
| 547 | /* Nothing to do */ |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static int mxc_spi_set_mode(struct udevice *bus, uint mode) |
| 552 | { |
| 553 | struct mxc_spi_slave *mxcs = dev_get_platdata(bus); |
| 554 | |
| 555 | mxcs->mode = mode; |
| 556 | mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0; |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static const struct dm_spi_ops mxc_spi_ops = { |
| 562 | .claim_bus = mxc_spi_claim_bus, |
| 563 | .release_bus = mxc_spi_release_bus, |
| 564 | .xfer = mxc_spi_xfer, |
| 565 | .set_speed = mxc_spi_set_speed, |
| 566 | .set_mode = mxc_spi_set_mode, |
| 567 | }; |
| 568 | |
| 569 | static const struct udevice_id mxc_spi_ids[] = { |
| 570 | { .compatible = "fsl,imx51-ecspi" }, |
| 571 | { } |
| 572 | }; |
| 573 | |
| 574 | U_BOOT_DRIVER(mxc_spi) = { |
| 575 | .name = "mxc_spi", |
| 576 | .id = UCLASS_SPI, |
| 577 | .of_match = mxc_spi_ids, |
| 578 | .ops = &mxc_spi_ops, |
| 579 | .platdata_auto_alloc_size = sizeof(struct mxc_spi_slave), |
| 580 | .probe = mxc_spi_probe, |
| 581 | }; |
| 582 | #endif |