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