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 | |
Daniel Mack | ce1807b | 2009-11-19 19:01:42 +0000 | [diff] [blame] | 48 | #define MX3_CSPISTAT 0x14 |
| 49 | #define MX3_CSPISTAT_RR (1 << 3) |
| 50 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 51 | /* generic defines to abstract from the different register layouts */ |
| 52 | #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */ |
| 53 | #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */ |
| 54 | |
| 55 | struct spi_imx_config { |
| 56 | unsigned int speed_hz; |
| 57 | unsigned int bpw; |
| 58 | unsigned int mode; |
| 59 | int cs; |
| 60 | }; |
| 61 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 62 | enum spi_imx_devtype { |
| 63 | SPI_IMX_VER_IMX1, |
| 64 | SPI_IMX_VER_0_0, |
| 65 | SPI_IMX_VER_0_4, |
| 66 | SPI_IMX_VER_0_5, |
| 67 | SPI_IMX_VER_0_7, |
| 68 | SPI_IMX_VER_AUTODETECT, |
| 69 | }; |
| 70 | |
| 71 | struct spi_imx_data; |
| 72 | |
| 73 | struct spi_imx_devtype_data { |
| 74 | void (*intctrl)(struct spi_imx_data *, int); |
| 75 | int (*config)(struct spi_imx_data *, struct spi_imx_config *); |
| 76 | void (*trigger)(struct spi_imx_data *); |
| 77 | int (*rx_available)(struct spi_imx_data *); |
| 78 | }; |
| 79 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 80 | struct spi_imx_data { |
| 81 | struct spi_bitbang bitbang; |
| 82 | |
| 83 | struct completion xfer_done; |
| 84 | void *base; |
| 85 | int irq; |
| 86 | struct clk *clk; |
| 87 | unsigned long spi_clk; |
| 88 | int *chipselect; |
| 89 | |
| 90 | unsigned int count; |
| 91 | void (*tx)(struct spi_imx_data *); |
| 92 | void (*rx)(struct spi_imx_data *); |
| 93 | void *rx_buf; |
| 94 | const void *tx_buf; |
| 95 | unsigned int txfifo; /* number of words pushed in tx FIFO */ |
| 96 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 97 | struct spi_imx_devtype_data devtype_data; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | #define MXC_SPI_BUF_RX(type) \ |
| 101 | static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \ |
| 102 | { \ |
| 103 | unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \ |
| 104 | \ |
| 105 | if (spi_imx->rx_buf) { \ |
| 106 | *(type *)spi_imx->rx_buf = val; \ |
| 107 | spi_imx->rx_buf += sizeof(type); \ |
| 108 | } \ |
| 109 | } |
| 110 | |
| 111 | #define MXC_SPI_BUF_TX(type) \ |
| 112 | static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \ |
| 113 | { \ |
| 114 | type val = 0; \ |
| 115 | \ |
| 116 | if (spi_imx->tx_buf) { \ |
| 117 | val = *(type *)spi_imx->tx_buf; \ |
| 118 | spi_imx->tx_buf += sizeof(type); \ |
| 119 | } \ |
| 120 | \ |
| 121 | spi_imx->count -= sizeof(type); \ |
| 122 | \ |
| 123 | writel(val, spi_imx->base + MXC_CSPITXDATA); \ |
| 124 | } |
| 125 | |
| 126 | MXC_SPI_BUF_RX(u8) |
| 127 | MXC_SPI_BUF_TX(u8) |
| 128 | MXC_SPI_BUF_RX(u16) |
| 129 | MXC_SPI_BUF_TX(u16) |
| 130 | MXC_SPI_BUF_RX(u32) |
| 131 | MXC_SPI_BUF_TX(u32) |
| 132 | |
| 133 | /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set |
| 134 | * (which is currently not the case in this driver) |
| 135 | */ |
| 136 | static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, |
| 137 | 256, 384, 512, 768, 1024}; |
| 138 | |
| 139 | /* MX21, MX27 */ |
| 140 | static unsigned int spi_imx_clkdiv_1(unsigned int fin, |
| 141 | unsigned int fspi) |
| 142 | { |
| 143 | int i, max; |
| 144 | |
| 145 | if (cpu_is_mx21()) |
| 146 | max = 18; |
| 147 | else |
| 148 | max = 16; |
| 149 | |
| 150 | for (i = 2; i < max; i++) |
| 151 | if (fspi * mxc_clkdivs[i] >= fin) |
| 152 | return i; |
| 153 | |
| 154 | return max; |
| 155 | } |
| 156 | |
| 157 | /* MX1, MX31, MX35 */ |
| 158 | static unsigned int spi_imx_clkdiv_2(unsigned int fin, |
| 159 | unsigned int fspi) |
| 160 | { |
| 161 | int i, div = 4; |
| 162 | |
| 163 | for (i = 0; i < 7; i++) { |
| 164 | if (fspi * div >= fin) |
| 165 | return i; |
| 166 | div <<= 1; |
| 167 | } |
| 168 | |
| 169 | return 7; |
| 170 | } |
| 171 | |
| 172 | #define MX31_INTREG_TEEN (1 << 0) |
| 173 | #define MX31_INTREG_RREN (1 << 3) |
| 174 | |
| 175 | #define MX31_CSPICTRL_ENABLE (1 << 0) |
| 176 | #define MX31_CSPICTRL_MASTER (1 << 1) |
| 177 | #define MX31_CSPICTRL_XCH (1 << 2) |
| 178 | #define MX31_CSPICTRL_POL (1 << 4) |
| 179 | #define MX31_CSPICTRL_PHA (1 << 5) |
| 180 | #define MX31_CSPICTRL_SSCTL (1 << 6) |
| 181 | #define MX31_CSPICTRL_SSPOL (1 << 7) |
| 182 | #define MX31_CSPICTRL_BC_SHIFT 8 |
| 183 | #define MX35_CSPICTRL_BL_SHIFT 20 |
| 184 | #define MX31_CSPICTRL_CS_SHIFT 24 |
| 185 | #define MX35_CSPICTRL_CS_SHIFT 12 |
| 186 | #define MX31_CSPICTRL_DR_SHIFT 16 |
| 187 | |
| 188 | #define MX31_CSPISTATUS 0x14 |
| 189 | #define MX31_STATUS_RR (1 << 3) |
| 190 | |
| 191 | /* These functions also work for the i.MX35, but be aware that |
| 192 | * the i.MX35 has a slightly different register layout for bits |
| 193 | * we do not use here. |
| 194 | */ |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 195 | 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] | 196 | { |
| 197 | unsigned int val = 0; |
| 198 | |
| 199 | if (enable & MXC_INT_TE) |
| 200 | val |= MX31_INTREG_TEEN; |
| 201 | if (enable & MXC_INT_RR) |
| 202 | val |= MX31_INTREG_RREN; |
| 203 | |
| 204 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 205 | } |
| 206 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 207 | 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] | 208 | { |
| 209 | unsigned int reg; |
| 210 | |
| 211 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
| 212 | reg |= MX31_CSPICTRL_XCH; |
| 213 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 214 | } |
| 215 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 216 | 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] | 217 | struct spi_imx_config *config) |
| 218 | { |
| 219 | unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER; |
| 220 | |
| 221 | reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) << |
| 222 | MX31_CSPICTRL_DR_SHIFT; |
| 223 | |
| 224 | if (cpu_is_mx31()) |
| 225 | reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT; |
Sascha Hauer | 87f673e | 2009-12-13 00:58:41 -0700 | [diff] [blame] | 226 | else if (cpu_is_mx25() || cpu_is_mx35()) { |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 227 | reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT; |
| 228 | reg |= MX31_CSPICTRL_SSCTL; |
| 229 | } |
| 230 | |
| 231 | if (config->mode & SPI_CPHA) |
| 232 | reg |= MX31_CSPICTRL_PHA; |
| 233 | if (config->mode & SPI_CPOL) |
| 234 | reg |= MX31_CSPICTRL_POL; |
| 235 | if (config->mode & SPI_CS_HIGH) |
| 236 | reg |= MX31_CSPICTRL_SSPOL; |
| 237 | if (config->cs < 0) { |
| 238 | if (cpu_is_mx31()) |
| 239 | reg |= (config->cs + 32) << MX31_CSPICTRL_CS_SHIFT; |
Sascha Hauer | 87f673e | 2009-12-13 00:58:41 -0700 | [diff] [blame] | 240 | else if (cpu_is_mx25() || cpu_is_mx35()) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 241 | reg |= (config->cs + 32) << MX35_CSPICTRL_CS_SHIFT; |
| 242 | } |
| 243 | |
| 244 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 249 | 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] | 250 | { |
| 251 | return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR; |
| 252 | } |
| 253 | |
| 254 | #define MX27_INTREG_RR (1 << 4) |
| 255 | #define MX27_INTREG_TEEN (1 << 9) |
| 256 | #define MX27_INTREG_RREN (1 << 13) |
| 257 | |
| 258 | #define MX27_CSPICTRL_POL (1 << 5) |
| 259 | #define MX27_CSPICTRL_PHA (1 << 6) |
| 260 | #define MX27_CSPICTRL_SSPOL (1 << 8) |
| 261 | #define MX27_CSPICTRL_XCH (1 << 9) |
| 262 | #define MX27_CSPICTRL_ENABLE (1 << 10) |
| 263 | #define MX27_CSPICTRL_MASTER (1 << 11) |
| 264 | #define MX27_CSPICTRL_DR_SHIFT 14 |
| 265 | #define MX27_CSPICTRL_CS_SHIFT 19 |
| 266 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 267 | static void __maybe_unused mx27_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 268 | { |
| 269 | unsigned int val = 0; |
| 270 | |
| 271 | if (enable & MXC_INT_TE) |
| 272 | val |= MX27_INTREG_TEEN; |
| 273 | if (enable & MXC_INT_RR) |
| 274 | val |= MX27_INTREG_RREN; |
| 275 | |
| 276 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 277 | } |
| 278 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 279 | static void __maybe_unused mx27_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 280 | { |
| 281 | unsigned int reg; |
| 282 | |
| 283 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
| 284 | reg |= MX27_CSPICTRL_XCH; |
| 285 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 286 | } |
| 287 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 288 | static int __maybe_unused mx27_config(struct spi_imx_data *spi_imx, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 289 | struct spi_imx_config *config) |
| 290 | { |
| 291 | unsigned int reg = MX27_CSPICTRL_ENABLE | MX27_CSPICTRL_MASTER; |
| 292 | |
| 293 | reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz) << |
| 294 | MX27_CSPICTRL_DR_SHIFT; |
| 295 | reg |= config->bpw - 1; |
| 296 | |
| 297 | if (config->mode & SPI_CPHA) |
| 298 | reg |= MX27_CSPICTRL_PHA; |
| 299 | if (config->mode & SPI_CPOL) |
| 300 | reg |= MX27_CSPICTRL_POL; |
| 301 | if (config->mode & SPI_CS_HIGH) |
| 302 | reg |= MX27_CSPICTRL_SSPOL; |
| 303 | if (config->cs < 0) |
| 304 | reg |= (config->cs + 32) << MX27_CSPICTRL_CS_SHIFT; |
| 305 | |
| 306 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 311 | static int __maybe_unused mx27_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 312 | { |
| 313 | return readl(spi_imx->base + MXC_CSPIINT) & MX27_INTREG_RR; |
| 314 | } |
| 315 | |
| 316 | #define MX1_INTREG_RR (1 << 3) |
| 317 | #define MX1_INTREG_TEEN (1 << 8) |
| 318 | #define MX1_INTREG_RREN (1 << 11) |
| 319 | |
| 320 | #define MX1_CSPICTRL_POL (1 << 4) |
| 321 | #define MX1_CSPICTRL_PHA (1 << 5) |
| 322 | #define MX1_CSPICTRL_XCH (1 << 8) |
| 323 | #define MX1_CSPICTRL_ENABLE (1 << 9) |
| 324 | #define MX1_CSPICTRL_MASTER (1 << 10) |
| 325 | #define MX1_CSPICTRL_DR_SHIFT 13 |
| 326 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 327 | 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] | 328 | { |
| 329 | unsigned int val = 0; |
| 330 | |
| 331 | if (enable & MXC_INT_TE) |
| 332 | val |= MX1_INTREG_TEEN; |
| 333 | if (enable & MXC_INT_RR) |
| 334 | val |= MX1_INTREG_RREN; |
| 335 | |
| 336 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 337 | } |
| 338 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 339 | 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] | 340 | { |
| 341 | unsigned int reg; |
| 342 | |
| 343 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
| 344 | reg |= MX1_CSPICTRL_XCH; |
| 345 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 346 | } |
| 347 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 348 | 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] | 349 | struct spi_imx_config *config) |
| 350 | { |
| 351 | unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER; |
| 352 | |
| 353 | reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) << |
| 354 | MX1_CSPICTRL_DR_SHIFT; |
| 355 | reg |= config->bpw - 1; |
| 356 | |
| 357 | if (config->mode & SPI_CPHA) |
| 358 | reg |= MX1_CSPICTRL_PHA; |
| 359 | if (config->mode & SPI_CPOL) |
| 360 | reg |= MX1_CSPICTRL_POL; |
| 361 | |
| 362 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 367 | 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] | 368 | { |
| 369 | return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR; |
| 370 | } |
| 371 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 372 | /* |
| 373 | * These version numbers are taken from the Freescale driver. Unfortunately it |
| 374 | * doesn't support i.MX1, so this entry doesn't match the scheme. :-( |
| 375 | */ |
| 376 | static struct spi_imx_devtype_data spi_imx_devtype_data[] __devinitdata = { |
| 377 | #ifdef CONFIG_SPI_IMX_VER_IMX1 |
| 378 | [SPI_IMX_VER_IMX1] = { |
| 379 | .intctrl = mx1_intctrl, |
| 380 | .config = mx1_config, |
| 381 | .trigger = mx1_trigger, |
| 382 | .rx_available = mx1_rx_available, |
| 383 | }, |
| 384 | #endif |
| 385 | #ifdef CONFIG_SPI_IMX_VER_0_0 |
| 386 | [SPI_IMX_VER_0_0] = { |
| 387 | .intctrl = mx27_intctrl, |
| 388 | .config = mx27_config, |
| 389 | .trigger = mx27_trigger, |
| 390 | .rx_available = mx27_rx_available, |
| 391 | }, |
| 392 | #endif |
| 393 | #ifdef CONFIG_SPI_IMX_VER_0_4 |
| 394 | [SPI_IMX_VER_0_4] = { |
| 395 | .intctrl = mx31_intctrl, |
| 396 | .config = mx31_config, |
| 397 | .trigger = mx31_trigger, |
| 398 | .rx_available = mx31_rx_available, |
| 399 | }, |
| 400 | #endif |
| 401 | #ifdef CONFIG_SPI_IMX_VER_0_7 |
| 402 | [SPI_IMX_VER_0_7] = { |
| 403 | .intctrl = mx31_intctrl, |
| 404 | .config = mx31_config, |
| 405 | .trigger = mx31_trigger, |
| 406 | .rx_available = mx31_rx_available, |
| 407 | }, |
| 408 | #endif |
| 409 | }; |
| 410 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 411 | static void spi_imx_chipselect(struct spi_device *spi, int is_active) |
| 412 | { |
| 413 | 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] | 414 | int gpio = spi_imx->chipselect[spi->chip_select]; |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 415 | int active = is_active != BITBANG_CS_INACTIVE; |
| 416 | int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 417 | |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 418 | if (gpio < 0) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 419 | return; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 420 | |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 421 | gpio_set_value(gpio, dev_is_lowactive ^ active); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static void spi_imx_push(struct spi_imx_data *spi_imx) |
| 425 | { |
| 426 | while (spi_imx->txfifo < 8) { |
| 427 | if (!spi_imx->count) |
| 428 | break; |
| 429 | spi_imx->tx(spi_imx); |
| 430 | spi_imx->txfifo++; |
| 431 | } |
| 432 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 433 | spi_imx->devtype_data.trigger(spi_imx); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static irqreturn_t spi_imx_isr(int irq, void *dev_id) |
| 437 | { |
| 438 | struct spi_imx_data *spi_imx = dev_id; |
| 439 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 440 | while (spi_imx->devtype_data.rx_available(spi_imx)) { |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 441 | spi_imx->rx(spi_imx); |
| 442 | spi_imx->txfifo--; |
| 443 | } |
| 444 | |
| 445 | if (spi_imx->count) { |
| 446 | spi_imx_push(spi_imx); |
| 447 | return IRQ_HANDLED; |
| 448 | } |
| 449 | |
| 450 | if (spi_imx->txfifo) { |
| 451 | /* No data left to push, but still waiting for rx data, |
| 452 | * enable receive data available interrupt. |
| 453 | */ |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 454 | spi_imx->devtype_data.intctrl( |
| 455 | spi_imx, MXC_INT_RR); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 456 | return IRQ_HANDLED; |
| 457 | } |
| 458 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 459 | spi_imx->devtype_data.intctrl(spi_imx, 0); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 460 | complete(&spi_imx->xfer_done); |
| 461 | |
| 462 | return IRQ_HANDLED; |
| 463 | } |
| 464 | |
| 465 | static int spi_imx_setupxfer(struct spi_device *spi, |
| 466 | struct spi_transfer *t) |
| 467 | { |
| 468 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
| 469 | struct spi_imx_config config; |
| 470 | |
| 471 | config.bpw = t ? t->bits_per_word : spi->bits_per_word; |
| 472 | config.speed_hz = t ? t->speed_hz : spi->max_speed_hz; |
| 473 | config.mode = spi->mode; |
Uwe Kleine-König | d1c627b | 2009-10-01 15:44:32 -0700 | [diff] [blame] | 474 | config.cs = spi_imx->chipselect[spi->chip_select]; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 475 | |
Sascha Hauer | 462d26b | 2009-10-01 15:44:29 -0700 | [diff] [blame] | 476 | if (!config.speed_hz) |
| 477 | config.speed_hz = spi->max_speed_hz; |
| 478 | if (!config.bpw) |
| 479 | config.bpw = spi->bits_per_word; |
| 480 | if (!config.speed_hz) |
| 481 | config.speed_hz = spi->max_speed_hz; |
| 482 | |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 483 | /* Initialize the functions for transfer */ |
| 484 | if (config.bpw <= 8) { |
| 485 | spi_imx->rx = spi_imx_buf_rx_u8; |
| 486 | spi_imx->tx = spi_imx_buf_tx_u8; |
| 487 | } else if (config.bpw <= 16) { |
| 488 | spi_imx->rx = spi_imx_buf_rx_u16; |
| 489 | spi_imx->tx = spi_imx_buf_tx_u16; |
| 490 | } else if (config.bpw <= 32) { |
| 491 | spi_imx->rx = spi_imx_buf_rx_u32; |
| 492 | spi_imx->tx = spi_imx_buf_tx_u32; |
| 493 | } else |
| 494 | BUG(); |
| 495 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 496 | spi_imx->devtype_data.config(spi_imx, &config); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int spi_imx_transfer(struct spi_device *spi, |
| 502 | struct spi_transfer *transfer) |
| 503 | { |
| 504 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
| 505 | |
| 506 | spi_imx->tx_buf = transfer->tx_buf; |
| 507 | spi_imx->rx_buf = transfer->rx_buf; |
| 508 | spi_imx->count = transfer->len; |
| 509 | spi_imx->txfifo = 0; |
| 510 | |
| 511 | init_completion(&spi_imx->xfer_done); |
| 512 | |
| 513 | spi_imx_push(spi_imx); |
| 514 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 515 | spi_imx->devtype_data.intctrl(spi_imx, MXC_INT_TE); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 516 | |
| 517 | wait_for_completion(&spi_imx->xfer_done); |
| 518 | |
| 519 | return transfer->len; |
| 520 | } |
| 521 | |
| 522 | static int spi_imx_setup(struct spi_device *spi) |
| 523 | { |
Sascha Hauer | 6c23e5d | 2009-10-01 15:44:29 -0700 | [diff] [blame] | 524 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
| 525 | int gpio = spi_imx->chipselect[spi->chip_select]; |
| 526 | |
Alberto Panizzo | f4d4ecf | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 527 | 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] | 528 | spi->mode, spi->bits_per_word, spi->max_speed_hz); |
| 529 | |
Sascha Hauer | 6c23e5d | 2009-10-01 15:44:29 -0700 | [diff] [blame] | 530 | if (gpio >= 0) |
| 531 | gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1); |
| 532 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 533 | spi_imx_chipselect(spi, BITBANG_CS_INACTIVE); |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static void spi_imx_cleanup(struct spi_device *spi) |
| 539 | { |
| 540 | } |
| 541 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 542 | static struct platform_device_id spi_imx_devtype[] = { |
| 543 | { |
| 544 | .name = DRIVER_NAME, |
| 545 | .driver_data = SPI_IMX_VER_AUTODETECT, |
| 546 | }, { |
| 547 | .name = "imx1-cspi", |
| 548 | .driver_data = SPI_IMX_VER_IMX1, |
| 549 | }, { |
| 550 | .name = "imx21-cspi", |
| 551 | .driver_data = SPI_IMX_VER_0_0, |
| 552 | }, { |
| 553 | .name = "imx25-cspi", |
| 554 | .driver_data = SPI_IMX_VER_0_7, |
| 555 | }, { |
| 556 | .name = "imx27-cspi", |
| 557 | .driver_data = SPI_IMX_VER_0_0, |
| 558 | }, { |
| 559 | .name = "imx31-cspi", |
| 560 | .driver_data = SPI_IMX_VER_0_4, |
| 561 | }, { |
| 562 | .name = "imx35-cspi", |
| 563 | .driver_data = SPI_IMX_VER_0_7, |
| 564 | }, { |
| 565 | /* sentinel */ |
| 566 | } |
| 567 | }; |
| 568 | |
Grant Likely | 965346e | 2009-12-13 01:03:12 -0700 | [diff] [blame] | 569 | static int __devinit spi_imx_probe(struct platform_device *pdev) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 570 | { |
| 571 | struct spi_imx_master *mxc_platform_info; |
| 572 | struct spi_master *master; |
| 573 | struct spi_imx_data *spi_imx; |
| 574 | struct resource *res; |
| 575 | int i, ret; |
| 576 | |
Uwe Kleine-König | 980f3be | 2009-12-13 01:02:09 -0700 | [diff] [blame] | 577 | mxc_platform_info = dev_get_platdata(&pdev->dev); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 578 | if (!mxc_platform_info) { |
| 579 | dev_err(&pdev->dev, "can't get the platform data\n"); |
| 580 | return -EINVAL; |
| 581 | } |
| 582 | |
| 583 | master = spi_alloc_master(&pdev->dev, sizeof(struct spi_imx_data)); |
| 584 | if (!master) |
| 585 | return -ENOMEM; |
| 586 | |
| 587 | platform_set_drvdata(pdev, master); |
| 588 | |
| 589 | master->bus_num = pdev->id; |
| 590 | master->num_chipselect = mxc_platform_info->num_chipselect; |
| 591 | |
| 592 | spi_imx = spi_master_get_devdata(master); |
| 593 | spi_imx->bitbang.master = spi_master_get(master); |
| 594 | spi_imx->chipselect = mxc_platform_info->chipselect; |
| 595 | |
| 596 | for (i = 0; i < master->num_chipselect; i++) { |
| 597 | if (spi_imx->chipselect[i] < 0) |
| 598 | continue; |
| 599 | ret = gpio_request(spi_imx->chipselect[i], DRIVER_NAME); |
| 600 | if (ret) { |
John Ogness | bbd050a | 2009-11-24 16:53:07 +0000 | [diff] [blame] | 601 | while (i > 0) { |
| 602 | i--; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 603 | if (spi_imx->chipselect[i] >= 0) |
John Ogness | bbd050a | 2009-11-24 16:53:07 +0000 | [diff] [blame] | 604 | gpio_free(spi_imx->chipselect[i]); |
| 605 | } |
| 606 | dev_err(&pdev->dev, "can't get cs gpios\n"); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 607 | goto out_master_put; |
| 608 | } |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | spi_imx->bitbang.chipselect = spi_imx_chipselect; |
| 612 | spi_imx->bitbang.setup_transfer = spi_imx_setupxfer; |
| 613 | spi_imx->bitbang.txrx_bufs = spi_imx_transfer; |
| 614 | spi_imx->bitbang.master->setup = spi_imx_setup; |
| 615 | spi_imx->bitbang.master->cleanup = spi_imx_cleanup; |
Sascha Hauer | 3910f2c | 2009-10-01 15:44:30 -0700 | [diff] [blame] | 616 | 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] | 617 | |
| 618 | init_completion(&spi_imx->xfer_done); |
| 619 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 620 | if (pdev->id_entry->driver_data == SPI_IMX_VER_AUTODETECT) { |
| 621 | if (cpu_is_mx25() || cpu_is_mx35()) |
| 622 | spi_imx->devtype_data = |
| 623 | spi_imx_devtype_data[SPI_IMX_VER_0_7]; |
| 624 | else if (cpu_is_mx25() || cpu_is_mx31() || cpu_is_mx35()) |
| 625 | spi_imx->devtype_data = |
| 626 | spi_imx_devtype_data[SPI_IMX_VER_0_4]; |
| 627 | else if (cpu_is_mx27() || cpu_is_mx21()) |
| 628 | spi_imx->devtype_data = |
| 629 | spi_imx_devtype_data[SPI_IMX_VER_0_0]; |
| 630 | else if (cpu_is_mx1()) |
| 631 | spi_imx->devtype_data = |
| 632 | spi_imx_devtype_data[SPI_IMX_VER_IMX1]; |
| 633 | else |
| 634 | BUG(); |
| 635 | } else |
| 636 | spi_imx->devtype_data = |
| 637 | spi_imx_devtype_data[pdev->id_entry->driver_data]; |
| 638 | |
| 639 | if (!spi_imx->devtype_data.intctrl) { |
| 640 | dev_err(&pdev->dev, "no support for this device compiled in\n"); |
| 641 | ret = -ENODEV; |
| 642 | goto out_gpio_free; |
| 643 | } |
| 644 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 645 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 646 | if (!res) { |
| 647 | dev_err(&pdev->dev, "can't get platform resource\n"); |
| 648 | ret = -ENOMEM; |
| 649 | goto out_gpio_free; |
| 650 | } |
| 651 | |
| 652 | if (!request_mem_region(res->start, resource_size(res), pdev->name)) { |
| 653 | dev_err(&pdev->dev, "request_mem_region failed\n"); |
| 654 | ret = -EBUSY; |
| 655 | goto out_gpio_free; |
| 656 | } |
| 657 | |
| 658 | spi_imx->base = ioremap(res->start, resource_size(res)); |
| 659 | if (!spi_imx->base) { |
| 660 | ret = -EINVAL; |
| 661 | goto out_release_mem; |
| 662 | } |
| 663 | |
| 664 | spi_imx->irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | 60f675a | 2009-12-13 00:58:13 -0700 | [diff] [blame] | 665 | if (spi_imx->irq <= 0) { |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 666 | ret = -EINVAL; |
| 667 | goto out_iounmap; |
| 668 | } |
| 669 | |
| 670 | ret = request_irq(spi_imx->irq, spi_imx_isr, 0, DRIVER_NAME, spi_imx); |
| 671 | if (ret) { |
| 672 | dev_err(&pdev->dev, "can't get irq%d: %d\n", spi_imx->irq, ret); |
| 673 | goto out_iounmap; |
| 674 | } |
| 675 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 676 | spi_imx->clk = clk_get(&pdev->dev, NULL); |
| 677 | if (IS_ERR(spi_imx->clk)) { |
| 678 | dev_err(&pdev->dev, "unable to get clock\n"); |
| 679 | ret = PTR_ERR(spi_imx->clk); |
| 680 | goto out_free_irq; |
| 681 | } |
| 682 | |
| 683 | clk_enable(spi_imx->clk); |
| 684 | spi_imx->spi_clk = clk_get_rate(spi_imx->clk); |
| 685 | |
Uwe Kleine-König | f30d59c | 2009-12-13 00:58:29 -0700 | [diff] [blame] | 686 | if (cpu_is_mx1() || cpu_is_mx21() || cpu_is_mx27()) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 687 | writel(1, spi_imx->base + MXC_RESET); |
| 688 | |
Daniel Mack | ce1807b | 2009-11-19 19:01:42 +0000 | [diff] [blame] | 689 | /* drain receive buffer */ |
Sascha Hauer | 87f673e | 2009-12-13 00:58:41 -0700 | [diff] [blame] | 690 | if (cpu_is_mx25() || cpu_is_mx31() || cpu_is_mx35()) |
Daniel Mack | ce1807b | 2009-11-19 19:01:42 +0000 | [diff] [blame] | 691 | while (readl(spi_imx->base + MX3_CSPISTAT) & MX3_CSPISTAT_RR) |
| 692 | readl(spi_imx->base + MXC_CSPIRXDATA); |
| 693 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 694 | spi_imx->devtype_data.intctrl(spi_imx, 0); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 695 | |
| 696 | ret = spi_bitbang_start(&spi_imx->bitbang); |
| 697 | if (ret) { |
| 698 | dev_err(&pdev->dev, "bitbang start failed with %d\n", ret); |
| 699 | goto out_clk_put; |
| 700 | } |
| 701 | |
| 702 | dev_info(&pdev->dev, "probed\n"); |
| 703 | |
| 704 | return ret; |
| 705 | |
| 706 | out_clk_put: |
| 707 | clk_disable(spi_imx->clk); |
| 708 | clk_put(spi_imx->clk); |
| 709 | out_free_irq: |
| 710 | free_irq(spi_imx->irq, spi_imx); |
| 711 | out_iounmap: |
| 712 | iounmap(spi_imx->base); |
| 713 | out_release_mem: |
| 714 | release_mem_region(res->start, resource_size(res)); |
| 715 | out_gpio_free: |
| 716 | for (i = 0; i < master->num_chipselect; i++) |
| 717 | if (spi_imx->chipselect[i] >= 0) |
| 718 | gpio_free(spi_imx->chipselect[i]); |
| 719 | out_master_put: |
| 720 | spi_master_put(master); |
| 721 | kfree(master); |
| 722 | platform_set_drvdata(pdev, NULL); |
| 723 | return ret; |
| 724 | } |
| 725 | |
Grant Likely | 965346e | 2009-12-13 01:03:12 -0700 | [diff] [blame] | 726 | static int __devexit spi_imx_remove(struct platform_device *pdev) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 727 | { |
| 728 | struct spi_master *master = platform_get_drvdata(pdev); |
| 729 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 730 | struct spi_imx_data *spi_imx = spi_master_get_devdata(master); |
| 731 | int i; |
| 732 | |
| 733 | spi_bitbang_stop(&spi_imx->bitbang); |
| 734 | |
| 735 | writel(0, spi_imx->base + MXC_CSPICTRL); |
| 736 | clk_disable(spi_imx->clk); |
| 737 | clk_put(spi_imx->clk); |
| 738 | free_irq(spi_imx->irq, spi_imx); |
| 739 | iounmap(spi_imx->base); |
| 740 | |
| 741 | for (i = 0; i < master->num_chipselect; i++) |
| 742 | if (spi_imx->chipselect[i] >= 0) |
| 743 | gpio_free(spi_imx->chipselect[i]); |
| 744 | |
| 745 | spi_master_put(master); |
| 746 | |
| 747 | release_mem_region(res->start, resource_size(res)); |
| 748 | |
| 749 | platform_set_drvdata(pdev, NULL); |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static struct platform_driver spi_imx_driver = { |
| 755 | .driver = { |
| 756 | .name = DRIVER_NAME, |
| 757 | .owner = THIS_MODULE, |
| 758 | }, |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame^] | 759 | .id_table = spi_imx_devtype, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 760 | .probe = spi_imx_probe, |
Grant Likely | 965346e | 2009-12-13 01:03:12 -0700 | [diff] [blame] | 761 | .remove = __devexit_p(spi_imx_remove), |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 762 | }; |
| 763 | |
| 764 | static int __init spi_imx_init(void) |
| 765 | { |
| 766 | return platform_driver_register(&spi_imx_driver); |
| 767 | } |
| 768 | |
| 769 | static void __exit spi_imx_exit(void) |
| 770 | { |
| 771 | platform_driver_unregister(&spi_imx_driver); |
| 772 | } |
| 773 | |
| 774 | module_init(spi_imx_init); |
| 775 | module_exit(spi_imx_exit); |
| 776 | |
| 777 | MODULE_DESCRIPTION("SPI Master Controller driver"); |
| 778 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); |
| 779 | MODULE_LICENSE("GPL"); |