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