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