Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved. |
| 3 | * Copyright (C) 2008 Juergen Beisert |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the |
| 16 | * Free Software Foundation |
| 17 | * 51 Franklin Street, Fifth Floor |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/completion.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/gpio.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <linux/irq.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 34 | #include <linux/spi/spi.h> |
| 35 | #include <linux/spi/spi_bitbang.h> |
| 36 | #include <linux/types.h> |
| 37 | |
| 38 | #include <mach/spi.h> |
| 39 | |
| 40 | #define DRIVER_NAME "spi_imx" |
| 41 | |
| 42 | #define MXC_CSPIRXDATA 0x00 |
| 43 | #define MXC_CSPITXDATA 0x04 |
| 44 | #define MXC_CSPICTRL 0x08 |
| 45 | #define MXC_CSPIINT 0x0c |
| 46 | #define MXC_RESET 0x1c |
| 47 | |
| 48 | /* generic defines to abstract from the different register layouts */ |
| 49 | #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */ |
| 50 | #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */ |
| 51 | |
| 52 | struct spi_imx_config { |
| 53 | unsigned int speed_hz; |
| 54 | unsigned int bpw; |
| 55 | unsigned int mode; |
Uwe Kleine-König | 3b2aa89 | 2010-09-10 09:42:29 +0200 | [diff] [blame] | 56 | u8 cs; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 59 | enum spi_imx_devtype { |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 60 | IMX1_CSPI, |
| 61 | IMX21_CSPI, |
| 62 | IMX27_CSPI, |
| 63 | IMX31_CSPI, |
| 64 | IMX35_CSPI, /* CSPI on all i.mx except above */ |
| 65 | IMX51_ECSPI, /* ECSPI on i.mx51 and later */ |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | struct spi_imx_data; |
| 69 | |
| 70 | struct spi_imx_devtype_data { |
| 71 | void (*intctrl)(struct spi_imx_data *, int); |
| 72 | int (*config)(struct spi_imx_data *, struct spi_imx_config *); |
| 73 | void (*trigger)(struct spi_imx_data *); |
| 74 | int (*rx_available)(struct spi_imx_data *); |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 75 | void (*reset)(struct spi_imx_data *); |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 76 | enum spi_imx_devtype devtype; |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 79 | struct spi_imx_data { |
| 80 | struct spi_bitbang bitbang; |
| 81 | |
| 82 | struct completion xfer_done; |
| 83 | void *base; |
| 84 | int irq; |
| 85 | struct clk *clk; |
| 86 | unsigned long spi_clk; |
| 87 | int *chipselect; |
| 88 | |
| 89 | unsigned int count; |
| 90 | void (*tx)(struct spi_imx_data *); |
| 91 | void (*rx)(struct spi_imx_data *); |
| 92 | void *rx_buf; |
| 93 | const void *tx_buf; |
| 94 | unsigned int txfifo; /* number of words pushed in tx FIFO */ |
| 95 | |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 96 | struct spi_imx_devtype_data *devtype_data; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 99 | static inline int is_imx27_cspi(struct spi_imx_data *d) |
| 100 | { |
| 101 | return d->devtype_data->devtype == IMX27_CSPI; |
| 102 | } |
| 103 | |
| 104 | static inline int is_imx35_cspi(struct spi_imx_data *d) |
| 105 | { |
| 106 | return d->devtype_data->devtype == IMX35_CSPI; |
| 107 | } |
| 108 | |
| 109 | static inline unsigned spi_imx_get_fifosize(struct spi_imx_data *d) |
| 110 | { |
| 111 | return (d->devtype_data->devtype == IMX51_ECSPI) ? 64 : 8; |
| 112 | } |
| 113 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 114 | #define MXC_SPI_BUF_RX(type) \ |
| 115 | static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \ |
| 116 | { \ |
| 117 | unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \ |
| 118 | \ |
| 119 | if (spi_imx->rx_buf) { \ |
| 120 | *(type *)spi_imx->rx_buf = val; \ |
| 121 | spi_imx->rx_buf += sizeof(type); \ |
| 122 | } \ |
| 123 | } |
| 124 | |
| 125 | #define MXC_SPI_BUF_TX(type) \ |
| 126 | static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \ |
| 127 | { \ |
| 128 | type val = 0; \ |
| 129 | \ |
| 130 | if (spi_imx->tx_buf) { \ |
| 131 | val = *(type *)spi_imx->tx_buf; \ |
| 132 | spi_imx->tx_buf += sizeof(type); \ |
| 133 | } \ |
| 134 | \ |
| 135 | spi_imx->count -= sizeof(type); \ |
| 136 | \ |
| 137 | writel(val, spi_imx->base + MXC_CSPITXDATA); \ |
| 138 | } |
| 139 | |
| 140 | MXC_SPI_BUF_RX(u8) |
| 141 | MXC_SPI_BUF_TX(u8) |
| 142 | MXC_SPI_BUF_RX(u16) |
| 143 | MXC_SPI_BUF_TX(u16) |
| 144 | MXC_SPI_BUF_RX(u32) |
| 145 | MXC_SPI_BUF_TX(u32) |
| 146 | |
| 147 | /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set |
| 148 | * (which is currently not the case in this driver) |
| 149 | */ |
| 150 | static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, |
| 151 | 256, 384, 512, 768, 1024}; |
| 152 | |
| 153 | /* MX21, MX27 */ |
| 154 | static unsigned int spi_imx_clkdiv_1(unsigned int fin, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 155 | unsigned int fspi, unsigned int max) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 156 | { |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 157 | int i; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 158 | |
| 159 | for (i = 2; i < max; i++) |
| 160 | if (fspi * mxc_clkdivs[i] >= fin) |
| 161 | return i; |
| 162 | |
| 163 | return max; |
| 164 | } |
| 165 | |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 166 | /* MX1, MX31, MX35, MX51 CSPI */ |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 167 | static unsigned int spi_imx_clkdiv_2(unsigned int fin, |
| 168 | unsigned int fspi) |
| 169 | { |
| 170 | int i, div = 4; |
| 171 | |
| 172 | for (i = 0; i < 7; i++) { |
| 173 | if (fspi * div >= fin) |
| 174 | return i; |
| 175 | div <<= 1; |
| 176 | } |
| 177 | |
| 178 | return 7; |
| 179 | } |
| 180 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 181 | #define MX51_ECSPI_CTRL 0x08 |
| 182 | #define MX51_ECSPI_CTRL_ENABLE (1 << 0) |
| 183 | #define MX51_ECSPI_CTRL_XCH (1 << 2) |
| 184 | #define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4) |
| 185 | #define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8 |
| 186 | #define MX51_ECSPI_CTRL_PREDIV_OFFSET 12 |
| 187 | #define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18) |
| 188 | #define MX51_ECSPI_CTRL_BL_OFFSET 20 |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 189 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 190 | #define MX51_ECSPI_CONFIG 0x0c |
| 191 | #define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0)) |
| 192 | #define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4)) |
| 193 | #define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8)) |
| 194 | #define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12)) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 195 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 196 | #define MX51_ECSPI_INT 0x10 |
| 197 | #define MX51_ECSPI_INT_TEEN (1 << 0) |
| 198 | #define MX51_ECSPI_INT_RREN (1 << 3) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 199 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 200 | #define MX51_ECSPI_STAT 0x18 |
| 201 | #define MX51_ECSPI_STAT_RR (1 << 3) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 202 | |
| 203 | /* MX51 eCSPI */ |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 204 | static unsigned int mx51_ecspi_clkdiv(unsigned int fin, unsigned int fspi) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 205 | { |
| 206 | /* |
| 207 | * there are two 4-bit dividers, the pre-divider divides by |
| 208 | * $pre, the post-divider by 2^$post |
| 209 | */ |
| 210 | unsigned int pre, post; |
| 211 | |
| 212 | if (unlikely(fspi > fin)) |
| 213 | return 0; |
| 214 | |
| 215 | post = fls(fin) - fls(fspi); |
| 216 | if (fin > fspi << post) |
| 217 | post++; |
| 218 | |
| 219 | /* now we have: (fin <= fspi << post) with post being minimal */ |
| 220 | |
| 221 | post = max(4U, post) - 4; |
| 222 | if (unlikely(post > 0xf)) { |
| 223 | pr_err("%s: cannot set clock freq: %u (base freq: %u)\n", |
| 224 | __func__, fspi, fin); |
| 225 | return 0xff; |
| 226 | } |
| 227 | |
| 228 | pre = DIV_ROUND_UP(fin, fspi << post) - 1; |
| 229 | |
| 230 | pr_debug("%s: fin: %u, fspi: %u, post: %u, pre: %u\n", |
| 231 | __func__, fin, fspi, post, pre); |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 232 | return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) | |
| 233 | (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 236 | static void __maybe_unused mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 237 | { |
| 238 | unsigned val = 0; |
| 239 | |
| 240 | if (enable & MXC_INT_TE) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 241 | val |= MX51_ECSPI_INT_TEEN; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 242 | |
| 243 | if (enable & MXC_INT_RR) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 244 | val |= MX51_ECSPI_INT_RREN; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 245 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 246 | writel(val, spi_imx->base + MX51_ECSPI_INT); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 249 | static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 250 | { |
| 251 | u32 reg; |
| 252 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 253 | reg = readl(spi_imx->base + MX51_ECSPI_CTRL); |
| 254 | reg |= MX51_ECSPI_CTRL_XCH; |
| 255 | writel(reg, spi_imx->base + MX51_ECSPI_CTRL); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 258 | static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx, |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 259 | struct spi_imx_config *config) |
| 260 | { |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 261 | u32 ctrl = MX51_ECSPI_CTRL_ENABLE, cfg = 0; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 262 | |
Sascha Hauer | f020c39 | 2011-02-08 21:08:59 +0100 | [diff] [blame] | 263 | /* |
| 264 | * The hardware seems to have a race condition when changing modes. The |
| 265 | * current assumption is that the selection of the channel arrives |
| 266 | * earlier in the hardware than the mode bits when they are written at |
| 267 | * the same time. |
| 268 | * So set master mode for all channels as we do not support slave mode. |
| 269 | */ |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 270 | ctrl |= MX51_ECSPI_CTRL_MODE_MASK; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 271 | |
| 272 | /* set clock speed */ |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 273 | ctrl |= mx51_ecspi_clkdiv(spi_imx->spi_clk, config->speed_hz); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 274 | |
| 275 | /* set chip select to use */ |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 276 | ctrl |= MX51_ECSPI_CTRL_CS(config->cs); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 277 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 278 | ctrl |= (config->bpw - 1) << MX51_ECSPI_CTRL_BL_OFFSET; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 279 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 280 | cfg |= MX51_ECSPI_CONFIG_SBBCTRL(config->cs); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 281 | |
| 282 | if (config->mode & SPI_CPHA) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 283 | cfg |= MX51_ECSPI_CONFIG_SCLKPHA(config->cs); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 284 | |
| 285 | if (config->mode & SPI_CPOL) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 286 | cfg |= MX51_ECSPI_CONFIG_SCLKPOL(config->cs); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 287 | |
| 288 | if (config->mode & SPI_CS_HIGH) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 289 | cfg |= MX51_ECSPI_CONFIG_SSBPOL(config->cs); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 290 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 291 | writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL); |
| 292 | writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 297 | static int __maybe_unused mx51_ecspi_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 298 | { |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 299 | return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 302 | static void __maybe_unused mx51_ecspi_reset(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 303 | { |
| 304 | /* drain receive buffer */ |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 305 | while (mx51_ecspi_rx_available(spi_imx)) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 306 | readl(spi_imx->base + MXC_CSPIRXDATA); |
| 307 | } |
| 308 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 309 | #define MX31_INTREG_TEEN (1 << 0) |
| 310 | #define MX31_INTREG_RREN (1 << 3) |
| 311 | |
| 312 | #define MX31_CSPICTRL_ENABLE (1 << 0) |
| 313 | #define MX31_CSPICTRL_MASTER (1 << 1) |
| 314 | #define MX31_CSPICTRL_XCH (1 << 2) |
| 315 | #define MX31_CSPICTRL_POL (1 << 4) |
| 316 | #define MX31_CSPICTRL_PHA (1 << 5) |
| 317 | #define MX31_CSPICTRL_SSCTL (1 << 6) |
| 318 | #define MX31_CSPICTRL_SSPOL (1 << 7) |
| 319 | #define MX31_CSPICTRL_BC_SHIFT 8 |
| 320 | #define MX35_CSPICTRL_BL_SHIFT 20 |
| 321 | #define MX31_CSPICTRL_CS_SHIFT 24 |
| 322 | #define MX35_CSPICTRL_CS_SHIFT 12 |
| 323 | #define MX31_CSPICTRL_DR_SHIFT 16 |
| 324 | |
| 325 | #define MX31_CSPISTATUS 0x14 |
| 326 | #define MX31_STATUS_RR (1 << 3) |
| 327 | |
| 328 | /* These functions also work for the i.MX35, but be aware that |
| 329 | * the i.MX35 has a slightly different register layout for bits |
| 330 | * we do not use here. |
| 331 | */ |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 332 | static void __maybe_unused mx31_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 333 | { |
| 334 | unsigned int val = 0; |
| 335 | |
| 336 | if (enable & MXC_INT_TE) |
| 337 | val |= MX31_INTREG_TEEN; |
| 338 | if (enable & MXC_INT_RR) |
| 339 | val |= MX31_INTREG_RREN; |
| 340 | |
| 341 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 342 | } |
| 343 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 344 | static void __maybe_unused mx31_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 345 | { |
| 346 | unsigned int reg; |
| 347 | |
| 348 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
| 349 | reg |= MX31_CSPICTRL_XCH; |
| 350 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 351 | } |
| 352 | |
Shawn Guo | 2a64a90 | 2011-07-10 01:16:38 +0800 | [diff] [blame] | 353 | static int __maybe_unused mx31_config(struct spi_imx_data *spi_imx, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 354 | struct spi_imx_config *config) |
| 355 | { |
| 356 | unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER; |
Uwe Kleine-König | 3b2aa89 | 2010-09-10 09:42:29 +0200 | [diff] [blame] | 357 | int cs = spi_imx->chipselect[config->cs]; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 358 | |
| 359 | reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) << |
| 360 | MX31_CSPICTRL_DR_SHIFT; |
| 361 | |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 362 | if (is_imx35_cspi(spi_imx)) { |
Shawn Guo | 2a64a90 | 2011-07-10 01:16:38 +0800 | [diff] [blame] | 363 | reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT; |
| 364 | reg |= MX31_CSPICTRL_SSCTL; |
| 365 | } else { |
| 366 | reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT; |
| 367 | } |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 368 | |
| 369 | if (config->mode & SPI_CPHA) |
| 370 | reg |= MX31_CSPICTRL_PHA; |
| 371 | if (config->mode & SPI_CPOL) |
| 372 | reg |= MX31_CSPICTRL_POL; |
| 373 | if (config->mode & SPI_CS_HIGH) |
| 374 | reg |= MX31_CSPICTRL_SSPOL; |
Uwe Kleine-König | 3b2aa89 | 2010-09-10 09:42:29 +0200 | [diff] [blame] | 375 | if (cs < 0) |
Shawn Guo | 2a64a90 | 2011-07-10 01:16:38 +0800 | [diff] [blame] | 376 | reg |= (cs + 32) << |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 377 | (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT : |
| 378 | MX31_CSPICTRL_CS_SHIFT); |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 379 | |
| 380 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 385 | static int __maybe_unused mx31_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 386 | { |
| 387 | return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR; |
| 388 | } |
| 389 | |
Shawn Guo | 2a64a90 | 2011-07-10 01:16:38 +0800 | [diff] [blame] | 390 | static void __maybe_unused mx31_reset(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 391 | { |
| 392 | /* drain receive buffer */ |
Shawn Guo | 2a64a90 | 2011-07-10 01:16:38 +0800 | [diff] [blame] | 393 | while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR) |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 394 | readl(spi_imx->base + MXC_CSPIRXDATA); |
| 395 | } |
| 396 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 397 | #define MX21_INTREG_RR (1 << 4) |
| 398 | #define MX21_INTREG_TEEN (1 << 9) |
| 399 | #define MX21_INTREG_RREN (1 << 13) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 400 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 401 | #define MX21_CSPICTRL_POL (1 << 5) |
| 402 | #define MX21_CSPICTRL_PHA (1 << 6) |
| 403 | #define MX21_CSPICTRL_SSPOL (1 << 8) |
| 404 | #define MX21_CSPICTRL_XCH (1 << 9) |
| 405 | #define MX21_CSPICTRL_ENABLE (1 << 10) |
| 406 | #define MX21_CSPICTRL_MASTER (1 << 11) |
| 407 | #define MX21_CSPICTRL_DR_SHIFT 14 |
| 408 | #define MX21_CSPICTRL_CS_SHIFT 19 |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 409 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 410 | static void __maybe_unused mx21_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 411 | { |
| 412 | unsigned int val = 0; |
| 413 | |
| 414 | if (enable & MXC_INT_TE) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 415 | val |= MX21_INTREG_TEEN; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 416 | if (enable & MXC_INT_RR) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 417 | val |= MX21_INTREG_RREN; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 418 | |
| 419 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 420 | } |
| 421 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 422 | static void __maybe_unused mx21_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 423 | { |
| 424 | unsigned int reg; |
| 425 | |
| 426 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 427 | reg |= MX21_CSPICTRL_XCH; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 428 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 429 | } |
| 430 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 431 | static int __maybe_unused mx21_config(struct spi_imx_data *spi_imx, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 432 | struct spi_imx_config *config) |
| 433 | { |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 434 | unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER; |
Uwe Kleine-König | 3b2aa89 | 2010-09-10 09:42:29 +0200 | [diff] [blame] | 435 | int cs = spi_imx->chipselect[config->cs]; |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 436 | unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 437 | |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 438 | reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz, max) << |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 439 | MX21_CSPICTRL_DR_SHIFT; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 440 | reg |= config->bpw - 1; |
| 441 | |
| 442 | if (config->mode & SPI_CPHA) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 443 | reg |= MX21_CSPICTRL_PHA; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 444 | if (config->mode & SPI_CPOL) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 445 | reg |= MX21_CSPICTRL_POL; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 446 | if (config->mode & SPI_CS_HIGH) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 447 | reg |= MX21_CSPICTRL_SSPOL; |
Uwe Kleine-König | 3b2aa89 | 2010-09-10 09:42:29 +0200 | [diff] [blame] | 448 | if (cs < 0) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 449 | reg |= (cs + 32) << MX21_CSPICTRL_CS_SHIFT; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 450 | |
| 451 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 456 | static int __maybe_unused mx21_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 457 | { |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 458 | return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 461 | static void __maybe_unused mx21_reset(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 462 | { |
| 463 | writel(1, spi_imx->base + MXC_RESET); |
| 464 | } |
| 465 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 466 | #define MX1_INTREG_RR (1 << 3) |
| 467 | #define MX1_INTREG_TEEN (1 << 8) |
| 468 | #define MX1_INTREG_RREN (1 << 11) |
| 469 | |
| 470 | #define MX1_CSPICTRL_POL (1 << 4) |
| 471 | #define MX1_CSPICTRL_PHA (1 << 5) |
| 472 | #define MX1_CSPICTRL_XCH (1 << 8) |
| 473 | #define MX1_CSPICTRL_ENABLE (1 << 9) |
| 474 | #define MX1_CSPICTRL_MASTER (1 << 10) |
| 475 | #define MX1_CSPICTRL_DR_SHIFT 13 |
| 476 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 477 | static void __maybe_unused mx1_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 478 | { |
| 479 | unsigned int val = 0; |
| 480 | |
| 481 | if (enable & MXC_INT_TE) |
| 482 | val |= MX1_INTREG_TEEN; |
| 483 | if (enable & MXC_INT_RR) |
| 484 | val |= MX1_INTREG_RREN; |
| 485 | |
| 486 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 487 | } |
| 488 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 489 | static void __maybe_unused mx1_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 490 | { |
| 491 | unsigned int reg; |
| 492 | |
| 493 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
| 494 | reg |= MX1_CSPICTRL_XCH; |
| 495 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 496 | } |
| 497 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 498 | static int __maybe_unused mx1_config(struct spi_imx_data *spi_imx, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 499 | struct spi_imx_config *config) |
| 500 | { |
| 501 | unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER; |
| 502 | |
| 503 | reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) << |
| 504 | MX1_CSPICTRL_DR_SHIFT; |
| 505 | reg |= config->bpw - 1; |
| 506 | |
| 507 | if (config->mode & SPI_CPHA) |
| 508 | reg |= MX1_CSPICTRL_PHA; |
| 509 | if (config->mode & SPI_CPOL) |
| 510 | reg |= MX1_CSPICTRL_POL; |
| 511 | |
| 512 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 517 | static int __maybe_unused mx1_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 518 | { |
| 519 | return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR; |
| 520 | } |
| 521 | |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 522 | static void __maybe_unused mx1_reset(struct spi_imx_data *spi_imx) |
| 523 | { |
| 524 | writel(1, spi_imx->base + MXC_RESET); |
| 525 | } |
| 526 | |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 527 | static struct spi_imx_devtype_data imx1_cspi_devtype_data = { |
| 528 | .intctrl = mx1_intctrl, |
| 529 | .config = mx1_config, |
| 530 | .trigger = mx1_trigger, |
| 531 | .rx_available = mx1_rx_available, |
| 532 | .reset = mx1_reset, |
| 533 | .devtype = IMX1_CSPI, |
| 534 | }; |
| 535 | |
| 536 | static struct spi_imx_devtype_data imx21_cspi_devtype_data = { |
| 537 | .intctrl = mx21_intctrl, |
| 538 | .config = mx21_config, |
| 539 | .trigger = mx21_trigger, |
| 540 | .rx_available = mx21_rx_available, |
| 541 | .reset = mx21_reset, |
| 542 | .devtype = IMX21_CSPI, |
| 543 | }; |
| 544 | |
| 545 | static struct spi_imx_devtype_data imx27_cspi_devtype_data = { |
| 546 | /* i.mx27 cspi shares the functions with i.mx21 one */ |
| 547 | .intctrl = mx21_intctrl, |
| 548 | .config = mx21_config, |
| 549 | .trigger = mx21_trigger, |
| 550 | .rx_available = mx21_rx_available, |
| 551 | .reset = mx21_reset, |
| 552 | .devtype = IMX27_CSPI, |
| 553 | }; |
| 554 | |
| 555 | static struct spi_imx_devtype_data imx31_cspi_devtype_data = { |
| 556 | .intctrl = mx31_intctrl, |
| 557 | .config = mx31_config, |
| 558 | .trigger = mx31_trigger, |
| 559 | .rx_available = mx31_rx_available, |
| 560 | .reset = mx31_reset, |
| 561 | .devtype = IMX31_CSPI, |
| 562 | }; |
| 563 | |
| 564 | static struct spi_imx_devtype_data imx35_cspi_devtype_data = { |
| 565 | /* i.mx35 and later cspi shares the functions with i.mx31 one */ |
| 566 | .intctrl = mx31_intctrl, |
| 567 | .config = mx31_config, |
| 568 | .trigger = mx31_trigger, |
| 569 | .rx_available = mx31_rx_available, |
| 570 | .reset = mx31_reset, |
| 571 | .devtype = IMX35_CSPI, |
| 572 | }; |
| 573 | |
| 574 | static struct spi_imx_devtype_data imx51_ecspi_devtype_data = { |
| 575 | .intctrl = mx51_ecspi_intctrl, |
| 576 | .config = mx51_ecspi_config, |
| 577 | .trigger = mx51_ecspi_trigger, |
| 578 | .rx_available = mx51_ecspi_rx_available, |
| 579 | .reset = mx51_ecspi_reset, |
| 580 | .devtype = IMX51_ECSPI, |
| 581 | }; |
| 582 | |
| 583 | static struct platform_device_id spi_imx_devtype[] = { |
| 584 | { |
| 585 | .name = "imx1-cspi", |
| 586 | .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data, |
| 587 | }, { |
| 588 | .name = "imx21-cspi", |
| 589 | .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data, |
| 590 | }, { |
| 591 | .name = "imx27-cspi", |
| 592 | .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data, |
| 593 | }, { |
| 594 | .name = "imx31-cspi", |
| 595 | .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data, |
| 596 | }, { |
| 597 | .name = "imx35-cspi", |
| 598 | .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data, |
| 599 | }, { |
| 600 | .name = "imx51-ecspi", |
| 601 | .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data, |
| 602 | }, { |
| 603 | /* sentinel */ |
| 604 | } |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 605 | }; |
| 606 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 607 | static void spi_imx_chipselect(struct spi_device *spi, int is_active) |
| 608 | { |
| 609 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 610 | int gpio = spi_imx->chipselect[spi->chip_select]; |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 611 | int active = is_active != BITBANG_CS_INACTIVE; |
| 612 | int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 613 | |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 614 | if (gpio < 0) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 615 | return; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 616 | |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 617 | gpio_set_value(gpio, dev_is_lowactive ^ active); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static void spi_imx_push(struct spi_imx_data *spi_imx) |
| 621 | { |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 622 | while (spi_imx->txfifo < spi_imx_get_fifosize(spi_imx)) { |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 623 | if (!spi_imx->count) |
| 624 | break; |
| 625 | spi_imx->tx(spi_imx); |
| 626 | spi_imx->txfifo++; |
| 627 | } |
| 628 | |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 629 | spi_imx->devtype_data->trigger(spi_imx); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static irqreturn_t spi_imx_isr(int irq, void *dev_id) |
| 633 | { |
| 634 | struct spi_imx_data *spi_imx = dev_id; |
| 635 | |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 636 | while (spi_imx->devtype_data->rx_available(spi_imx)) { |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 637 | spi_imx->rx(spi_imx); |
| 638 | spi_imx->txfifo--; |
| 639 | } |
| 640 | |
| 641 | if (spi_imx->count) { |
| 642 | spi_imx_push(spi_imx); |
| 643 | return IRQ_HANDLED; |
| 644 | } |
| 645 | |
| 646 | if (spi_imx->txfifo) { |
| 647 | /* No data left to push, but still waiting for rx data, |
| 648 | * enable receive data available interrupt. |
| 649 | */ |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 650 | spi_imx->devtype_data->intctrl( |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 651 | spi_imx, MXC_INT_RR); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 652 | return IRQ_HANDLED; |
| 653 | } |
| 654 | |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 655 | spi_imx->devtype_data->intctrl(spi_imx, 0); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 656 | complete(&spi_imx->xfer_done); |
| 657 | |
| 658 | return IRQ_HANDLED; |
| 659 | } |
| 660 | |
| 661 | static int spi_imx_setupxfer(struct spi_device *spi, |
| 662 | struct spi_transfer *t) |
| 663 | { |
| 664 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
| 665 | struct spi_imx_config config; |
| 666 | |
| 667 | config.bpw = t ? t->bits_per_word : spi->bits_per_word; |
| 668 | config.speed_hz = t ? t->speed_hz : spi->max_speed_hz; |
| 669 | config.mode = spi->mode; |
Uwe Kleine-König | 3b2aa89 | 2010-09-10 09:42:29 +0200 | [diff] [blame] | 670 | config.cs = spi->chip_select; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 671 | |
Sascha Hauer | 462d26b | 2009-10-01 15:44:29 -0700 | [diff] [blame] | 672 | if (!config.speed_hz) |
| 673 | config.speed_hz = spi->max_speed_hz; |
| 674 | if (!config.bpw) |
| 675 | config.bpw = spi->bits_per_word; |
| 676 | if (!config.speed_hz) |
| 677 | config.speed_hz = spi->max_speed_hz; |
| 678 | |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 679 | /* Initialize the functions for transfer */ |
| 680 | if (config.bpw <= 8) { |
| 681 | spi_imx->rx = spi_imx_buf_rx_u8; |
| 682 | spi_imx->tx = spi_imx_buf_tx_u8; |
| 683 | } else if (config.bpw <= 16) { |
| 684 | spi_imx->rx = spi_imx_buf_rx_u16; |
| 685 | spi_imx->tx = spi_imx_buf_tx_u16; |
| 686 | } else if (config.bpw <= 32) { |
| 687 | spi_imx->rx = spi_imx_buf_rx_u32; |
| 688 | spi_imx->tx = spi_imx_buf_tx_u32; |
| 689 | } else |
| 690 | BUG(); |
| 691 | |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 692 | spi_imx->devtype_data->config(spi_imx, &config); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static int spi_imx_transfer(struct spi_device *spi, |
| 698 | struct spi_transfer *transfer) |
| 699 | { |
| 700 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
| 701 | |
| 702 | spi_imx->tx_buf = transfer->tx_buf; |
| 703 | spi_imx->rx_buf = transfer->rx_buf; |
| 704 | spi_imx->count = transfer->len; |
| 705 | spi_imx->txfifo = 0; |
| 706 | |
| 707 | init_completion(&spi_imx->xfer_done); |
| 708 | |
| 709 | spi_imx_push(spi_imx); |
| 710 | |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 711 | spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 712 | |
| 713 | wait_for_completion(&spi_imx->xfer_done); |
| 714 | |
| 715 | return transfer->len; |
| 716 | } |
| 717 | |
| 718 | static int spi_imx_setup(struct spi_device *spi) |
| 719 | { |
Sascha Hauer | 6c23e5d | 2009-10-01 15:44:29 -0700 | [diff] [blame] | 720 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
| 721 | int gpio = spi_imx->chipselect[spi->chip_select]; |
| 722 | |
Alberto Panizzo | f4d4ecf | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 723 | dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 724 | spi->mode, spi->bits_per_word, spi->max_speed_hz); |
| 725 | |
Sascha Hauer | 6c23e5d | 2009-10-01 15:44:29 -0700 | [diff] [blame] | 726 | if (gpio >= 0) |
| 727 | gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1); |
| 728 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 729 | spi_imx_chipselect(spi, BITBANG_CS_INACTIVE); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static void spi_imx_cleanup(struct spi_device *spi) |
| 735 | { |
| 736 | } |
| 737 | |
Grant Likely | 965346e | 2009-12-13 01:03:12 -0700 | [diff] [blame] | 738 | static int __devinit spi_imx_probe(struct platform_device *pdev) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 739 | { |
| 740 | struct spi_imx_master *mxc_platform_info; |
| 741 | struct spi_master *master; |
| 742 | struct spi_imx_data *spi_imx; |
| 743 | struct resource *res; |
| 744 | int i, ret; |
| 745 | |
Uwe Kleine-König | 980f3be | 2009-12-13 01:02:09 -0700 | [diff] [blame] | 746 | mxc_platform_info = dev_get_platdata(&pdev->dev); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 747 | if (!mxc_platform_info) { |
| 748 | dev_err(&pdev->dev, "can't get the platform data\n"); |
| 749 | return -EINVAL; |
| 750 | } |
| 751 | |
| 752 | master = spi_alloc_master(&pdev->dev, sizeof(struct spi_imx_data)); |
| 753 | if (!master) |
| 754 | return -ENOMEM; |
| 755 | |
| 756 | platform_set_drvdata(pdev, master); |
| 757 | |
| 758 | master->bus_num = pdev->id; |
| 759 | master->num_chipselect = mxc_platform_info->num_chipselect; |
| 760 | |
| 761 | spi_imx = spi_master_get_devdata(master); |
| 762 | spi_imx->bitbang.master = spi_master_get(master); |
| 763 | spi_imx->chipselect = mxc_platform_info->chipselect; |
| 764 | |
| 765 | for (i = 0; i < master->num_chipselect; i++) { |
| 766 | if (spi_imx->chipselect[i] < 0) |
| 767 | continue; |
| 768 | ret = gpio_request(spi_imx->chipselect[i], DRIVER_NAME); |
| 769 | if (ret) { |
John Ogness | bbd050a | 2009-11-24 16:53:07 +0000 | [diff] [blame] | 770 | while (i > 0) { |
| 771 | i--; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 772 | if (spi_imx->chipselect[i] >= 0) |
John Ogness | bbd050a | 2009-11-24 16:53:07 +0000 | [diff] [blame] | 773 | gpio_free(spi_imx->chipselect[i]); |
| 774 | } |
| 775 | dev_err(&pdev->dev, "can't get cs gpios\n"); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 776 | goto out_master_put; |
| 777 | } |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | spi_imx->bitbang.chipselect = spi_imx_chipselect; |
| 781 | spi_imx->bitbang.setup_transfer = spi_imx_setupxfer; |
| 782 | spi_imx->bitbang.txrx_bufs = spi_imx_transfer; |
| 783 | spi_imx->bitbang.master->setup = spi_imx_setup; |
| 784 | spi_imx->bitbang.master->cleanup = spi_imx_cleanup; |
Sascha Hauer | 3910f2c | 2009-10-01 15:44:30 -0700 | [diff] [blame] | 785 | spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 786 | |
| 787 | init_completion(&spi_imx->xfer_done); |
| 788 | |
Uwe Kleine-König | 8934217 | 2010-11-24 10:05:46 +0100 | [diff] [blame] | 789 | spi_imx->devtype_data = |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame^] | 790 | (struct spi_imx_devtype_data *) pdev->id_entry->driver_data; |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 791 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 792 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 793 | if (!res) { |
| 794 | dev_err(&pdev->dev, "can't get platform resource\n"); |
| 795 | ret = -ENOMEM; |
| 796 | goto out_gpio_free; |
| 797 | } |
| 798 | |
| 799 | if (!request_mem_region(res->start, resource_size(res), pdev->name)) { |
| 800 | dev_err(&pdev->dev, "request_mem_region failed\n"); |
| 801 | ret = -EBUSY; |
| 802 | goto out_gpio_free; |
| 803 | } |
| 804 | |
| 805 | spi_imx->base = ioremap(res->start, resource_size(res)); |
| 806 | if (!spi_imx->base) { |
| 807 | ret = -EINVAL; |
| 808 | goto out_release_mem; |
| 809 | } |
| 810 | |
| 811 | spi_imx->irq = platform_get_irq(pdev, 0); |
Richard Genoud | 7357593 | 2011-01-07 15:26:01 +0100 | [diff] [blame] | 812 | if (spi_imx->irq < 0) { |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 813 | ret = -EINVAL; |
| 814 | goto out_iounmap; |
| 815 | } |
| 816 | |
| 817 | ret = request_irq(spi_imx->irq, spi_imx_isr, 0, DRIVER_NAME, spi_imx); |
| 818 | if (ret) { |
| 819 | dev_err(&pdev->dev, "can't get irq%d: %d\n", spi_imx->irq, ret); |
| 820 | goto out_iounmap; |
| 821 | } |
| 822 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 823 | spi_imx->clk = clk_get(&pdev->dev, NULL); |
| 824 | if (IS_ERR(spi_imx->clk)) { |
| 825 | dev_err(&pdev->dev, "unable to get clock\n"); |
| 826 | ret = PTR_ERR(spi_imx->clk); |
| 827 | goto out_free_irq; |
| 828 | } |
| 829 | |
| 830 | clk_enable(spi_imx->clk); |
| 831 | spi_imx->spi_clk = clk_get_rate(spi_imx->clk); |
| 832 | |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 833 | spi_imx->devtype_data->reset(spi_imx); |
Daniel Mack | ce1807b | 2009-11-19 19:01:42 +0000 | [diff] [blame] | 834 | |
Shawn Guo | edd501b | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 835 | spi_imx->devtype_data->intctrl(spi_imx, 0); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 836 | |
| 837 | ret = spi_bitbang_start(&spi_imx->bitbang); |
| 838 | if (ret) { |
| 839 | dev_err(&pdev->dev, "bitbang start failed with %d\n", ret); |
| 840 | goto out_clk_put; |
| 841 | } |
| 842 | |
| 843 | dev_info(&pdev->dev, "probed\n"); |
| 844 | |
| 845 | return ret; |
| 846 | |
| 847 | out_clk_put: |
| 848 | clk_disable(spi_imx->clk); |
| 849 | clk_put(spi_imx->clk); |
| 850 | out_free_irq: |
| 851 | free_irq(spi_imx->irq, spi_imx); |
| 852 | out_iounmap: |
| 853 | iounmap(spi_imx->base); |
| 854 | out_release_mem: |
| 855 | release_mem_region(res->start, resource_size(res)); |
| 856 | out_gpio_free: |
| 857 | for (i = 0; i < master->num_chipselect; i++) |
| 858 | if (spi_imx->chipselect[i] >= 0) |
| 859 | gpio_free(spi_imx->chipselect[i]); |
| 860 | out_master_put: |
| 861 | spi_master_put(master); |
| 862 | kfree(master); |
| 863 | platform_set_drvdata(pdev, NULL); |
| 864 | return ret; |
| 865 | } |
| 866 | |
Grant Likely | 965346e | 2009-12-13 01:03:12 -0700 | [diff] [blame] | 867 | static int __devexit spi_imx_remove(struct platform_device *pdev) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 868 | { |
| 869 | struct spi_master *master = platform_get_drvdata(pdev); |
| 870 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 871 | struct spi_imx_data *spi_imx = spi_master_get_devdata(master); |
| 872 | int i; |
| 873 | |
| 874 | spi_bitbang_stop(&spi_imx->bitbang); |
| 875 | |
| 876 | writel(0, spi_imx->base + MXC_CSPICTRL); |
| 877 | clk_disable(spi_imx->clk); |
| 878 | clk_put(spi_imx->clk); |
| 879 | free_irq(spi_imx->irq, spi_imx); |
| 880 | iounmap(spi_imx->base); |
| 881 | |
| 882 | for (i = 0; i < master->num_chipselect; i++) |
| 883 | if (spi_imx->chipselect[i] >= 0) |
| 884 | gpio_free(spi_imx->chipselect[i]); |
| 885 | |
| 886 | spi_master_put(master); |
| 887 | |
| 888 | release_mem_region(res->start, resource_size(res)); |
| 889 | |
| 890 | platform_set_drvdata(pdev, NULL); |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static struct platform_driver spi_imx_driver = { |
| 896 | .driver = { |
| 897 | .name = DRIVER_NAME, |
| 898 | .owner = THIS_MODULE, |
| 899 | }, |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 900 | .id_table = spi_imx_devtype, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 901 | .probe = spi_imx_probe, |
Grant Likely | 965346e | 2009-12-13 01:03:12 -0700 | [diff] [blame] | 902 | .remove = __devexit_p(spi_imx_remove), |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 903 | }; |
| 904 | |
| 905 | static int __init spi_imx_init(void) |
| 906 | { |
| 907 | return platform_driver_register(&spi_imx_driver); |
| 908 | } |
| 909 | |
| 910 | static void __exit spi_imx_exit(void) |
| 911 | { |
| 912 | platform_driver_unregister(&spi_imx_driver); |
| 913 | } |
| 914 | |
| 915 | module_init(spi_imx_init); |
| 916 | module_exit(spi_imx_exit); |
| 917 | |
| 918 | MODULE_DESCRIPTION("SPI Master Controller driver"); |
| 919 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); |
| 920 | MODULE_LICENSE("GPL"); |