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