Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 2 | /* |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 3 | * Freescale SPI controller driver. |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 4 | * |
| 5 | * Maintainer: Kumar Gala |
| 6 | * |
| 7 | * Copyright (C) 2006 Polycom, Inc. |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 8 | * Copyright 2010 Freescale Semiconductor, Inc. |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 9 | * |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 10 | * CPM SPI and QE buffer descriptors mode support: |
| 11 | * Copyright (c) 2009 MontaVista Software, Inc. |
| 12 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
| 13 | * |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 14 | * GRLIB support: |
| 15 | * Copyright (c) 2012 Aeroflex Gaisler AB. |
| 16 | * Author: Andreas Larsson <andreas@gaisler.com> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 17 | */ |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
Xiubo Li | a310836 | 2014-09-29 10:57:06 +0800 | [diff] [blame] | 20 | #include <linux/fsl_devices.h> |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/kernel.h> |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 25 | #include <linux/mm.h> |
Xiubo Li | a310836 | 2014-09-29 10:57:06 +0800 | [diff] [blame] | 26 | #include <linux/module.h> |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 28 | #include <linux/of.h> |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 29 | #include <linux/of_address.h> |
| 30 | #include <linux/of_irq.h> |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 31 | #include <linux/of_gpio.h> |
Xiubo Li | a310836 | 2014-09-29 10:57:06 +0800 | [diff] [blame] | 32 | #include <linux/of_platform.h> |
| 33 | #include <linux/platform_device.h> |
| 34 | #include <linux/spi/spi.h> |
| 35 | #include <linux/spi/spi_bitbang.h> |
| 36 | #include <linux/types.h> |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 37 | |
Rasmus Villemoes | 69b921a | 2019-03-06 10:32:05 +0000 | [diff] [blame] | 38 | #ifdef CONFIG_FSL_SOC |
| 39 | #include <sysdev/fsl_soc.h> |
| 40 | #endif |
| 41 | |
| 42 | /* Specific to the MPC8306/MPC8309 */ |
| 43 | #define IMMR_SPI_CS_OFFSET 0x14c |
| 44 | #define SPI_BOOT_SEL_BIT 0x80000000 |
| 45 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 46 | #include "spi-fsl-lib.h" |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 47 | #include "spi-fsl-cpm.h" |
| 48 | #include "spi-fsl-spi.h" |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 49 | |
Andreas Larsson | c3f3e77 | 2013-02-15 16:52:24 +0100 | [diff] [blame] | 50 | #define TYPE_FSL 0 |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 51 | #define TYPE_GRLIB 1 |
Andreas Larsson | c3f3e77 | 2013-02-15 16:52:24 +0100 | [diff] [blame] | 52 | |
| 53 | struct fsl_spi_match_data { |
| 54 | int type; |
| 55 | }; |
| 56 | |
| 57 | static struct fsl_spi_match_data of_fsl_spi_fsl_config = { |
| 58 | .type = TYPE_FSL, |
| 59 | }; |
| 60 | |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 61 | static struct fsl_spi_match_data of_fsl_spi_grlib_config = { |
| 62 | .type = TYPE_GRLIB, |
| 63 | }; |
| 64 | |
Jingoo Han | 3aea901 | 2014-06-03 21:03:59 +0900 | [diff] [blame] | 65 | static const struct of_device_id of_fsl_spi_match[] = { |
Andreas Larsson | c3f3e77 | 2013-02-15 16:52:24 +0100 | [diff] [blame] | 66 | { |
| 67 | .compatible = "fsl,spi", |
| 68 | .data = &of_fsl_spi_fsl_config, |
| 69 | }, |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 70 | { |
| 71 | .compatible = "aeroflexgaisler,spictrl", |
| 72 | .data = &of_fsl_spi_grlib_config, |
| 73 | }, |
Andreas Larsson | c3f3e77 | 2013-02-15 16:52:24 +0100 | [diff] [blame] | 74 | {} |
| 75 | }; |
| 76 | MODULE_DEVICE_TABLE(of, of_fsl_spi_match); |
| 77 | |
| 78 | static int fsl_spi_get_type(struct device *dev) |
| 79 | { |
| 80 | const struct of_device_id *match; |
| 81 | |
| 82 | if (dev->of_node) { |
| 83 | match = of_match_node(of_fsl_spi_match, dev->of_node); |
| 84 | if (match && match->data) |
| 85 | return ((struct fsl_spi_match_data *)match->data)->type; |
| 86 | } |
| 87 | return TYPE_FSL; |
| 88 | } |
| 89 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 90 | static void fsl_spi_change_mode(struct spi_device *spi) |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 91 | { |
| 92 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master); |
| 93 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 94 | struct fsl_spi_reg *reg_base = mspi->reg_base; |
| 95 | __be32 __iomem *mode = ®_base->mode; |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 96 | unsigned long flags; |
| 97 | |
| 98 | if (cs->hw_mode == mpc8xxx_spi_read_reg(mode)) |
| 99 | return; |
| 100 | |
| 101 | /* Turn off IRQs locally to minimize time that SPI is disabled. */ |
| 102 | local_irq_save(flags); |
| 103 | |
| 104 | /* Turn off SPI unit prior changing mode */ |
| 105 | mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE); |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 106 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 107 | /* When in CPM mode, we need to reinit tx and rx. */ |
| 108 | if (mspi->flags & SPI_CPM_MODE) { |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 109 | fsl_spi_cpm_reinit_txrx(mspi); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 110 | } |
Joakim Tjernlund | f9218c2 | 2010-05-22 02:18:02 -0600 | [diff] [blame] | 111 | mpc8xxx_spi_write_reg(mode, cs->hw_mode); |
Anton Vorontsov | a35c171 | 2009-10-12 20:49:24 +0400 | [diff] [blame] | 112 | local_irq_restore(flags); |
| 113 | } |
| 114 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 115 | static void fsl_spi_chipselect(struct spi_device *spi, int value) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 116 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 117 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Kenth Eriksson | 5039a86 | 2012-03-30 17:05:30 +0200 | [diff] [blame] | 118 | struct fsl_spi_platform_data *pdata; |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 119 | bool pol = spi->mode & SPI_CS_HIGH; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 120 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 121 | |
Kenth Eriksson | 5039a86 | 2012-03-30 17:05:30 +0200 | [diff] [blame] | 122 | pdata = spi->dev.parent->parent->platform_data; |
| 123 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 124 | if (value == BITBANG_CS_INACTIVE) { |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 125 | if (pdata->cs_control) |
| 126 | pdata->cs_control(spi, !pol); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (value == BITBANG_CS_ACTIVE) { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 130 | mpc8xxx_spi->rx_shift = cs->rx_shift; |
| 131 | mpc8xxx_spi->tx_shift = cs->tx_shift; |
| 132 | mpc8xxx_spi->get_rx = cs->get_rx; |
| 133 | mpc8xxx_spi->get_tx = cs->get_tx; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 134 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 135 | fsl_spi_change_mode(spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 136 | |
Anton Vorontsov | 364fdbc | 2009-03-31 15:24:36 -0700 | [diff] [blame] | 137 | if (pdata->cs_control) |
| 138 | pdata->cs_control(spi, pol); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Andreas Larsson | b48c4e3 | 2013-02-15 16:52:23 +0100 | [diff] [blame] | 142 | static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift, |
| 143 | int bits_per_word, int msb_first) |
| 144 | { |
| 145 | *rx_shift = 0; |
| 146 | *tx_shift = 0; |
| 147 | if (msb_first) { |
| 148 | if (bits_per_word <= 8) { |
| 149 | *rx_shift = 16; |
| 150 | *tx_shift = 24; |
| 151 | } else if (bits_per_word <= 16) { |
| 152 | *rx_shift = 16; |
| 153 | *tx_shift = 16; |
| 154 | } |
| 155 | } else { |
| 156 | if (bits_per_word <= 8) |
| 157 | *rx_shift = 8; |
| 158 | } |
| 159 | } |
| 160 | |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 161 | static void fsl_spi_grlib_set_shifts(u32 *rx_shift, u32 *tx_shift, |
| 162 | int bits_per_word, int msb_first) |
| 163 | { |
| 164 | *rx_shift = 0; |
| 165 | *tx_shift = 0; |
| 166 | if (bits_per_word <= 16) { |
| 167 | if (msb_first) { |
| 168 | *rx_shift = 16; /* LSB in bit 16 */ |
| 169 | *tx_shift = 32 - bits_per_word; /* MSB in bit 31 */ |
| 170 | } else { |
| 171 | *rx_shift = 16 - bits_per_word; /* MSB in bit 15 */ |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 176 | static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs, |
| 177 | struct spi_device *spi, |
| 178 | struct mpc8xxx_spi *mpc8xxx_spi, |
| 179 | int bits_per_word) |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 180 | { |
| 181 | cs->rx_shift = 0; |
| 182 | cs->tx_shift = 0; |
| 183 | if (bits_per_word <= 8) { |
| 184 | cs->get_rx = mpc8xxx_spi_rx_buf_u8; |
| 185 | cs->get_tx = mpc8xxx_spi_tx_buf_u8; |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 186 | } else if (bits_per_word <= 16) { |
| 187 | cs->get_rx = mpc8xxx_spi_rx_buf_u16; |
| 188 | cs->get_tx = mpc8xxx_spi_tx_buf_u16; |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 189 | } else if (bits_per_word <= 32) { |
| 190 | cs->get_rx = mpc8xxx_spi_rx_buf_u32; |
| 191 | cs->get_tx = mpc8xxx_spi_tx_buf_u32; |
| 192 | } else |
| 193 | return -EINVAL; |
| 194 | |
Andreas Larsson | b48c4e3 | 2013-02-15 16:52:23 +0100 | [diff] [blame] | 195 | if (mpc8xxx_spi->set_shifts) |
| 196 | mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift, |
| 197 | bits_per_word, |
| 198 | !(spi->mode & SPI_LSB_FIRST)); |
| 199 | |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 200 | mpc8xxx_spi->rx_shift = cs->rx_shift; |
| 201 | mpc8xxx_spi->tx_shift = cs->tx_shift; |
| 202 | mpc8xxx_spi->get_rx = cs->get_rx; |
| 203 | mpc8xxx_spi->get_tx = cs->get_tx; |
| 204 | |
| 205 | return bits_per_word; |
| 206 | } |
| 207 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 208 | static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs, |
| 209 | struct spi_device *spi, |
| 210 | int bits_per_word) |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 211 | { |
| 212 | /* QE uses Little Endian for words > 8 |
| 213 | * so transform all words > 8 into 8 bits |
| 214 | * Unfortnatly that doesn't work for LSB so |
| 215 | * reject these for now */ |
| 216 | /* Note: 32 bits word, LSB works iff |
| 217 | * tfcr/rfcr is set to CPMFCR_GBL */ |
| 218 | if (spi->mode & SPI_LSB_FIRST && |
| 219 | bits_per_word > 8) |
| 220 | return -EINVAL; |
| 221 | if (bits_per_word > 8) |
| 222 | return 8; /* pretend its 8 bits */ |
| 223 | return bits_per_word; |
| 224 | } |
| 225 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 226 | static int fsl_spi_setup_transfer(struct spi_device *spi, |
| 227 | struct spi_transfer *t) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 228 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 229 | struct mpc8xxx_spi *mpc8xxx_spi; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 230 | int bits_per_word = 0; |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 231 | u8 pm; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 232 | u32 hz = 0; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 233 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 234 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 235 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 236 | |
| 237 | if (t) { |
| 238 | bits_per_word = t->bits_per_word; |
| 239 | hz = t->speed_hz; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* spi_transfer level calls that work per-word */ |
| 243 | if (!bits_per_word) |
| 244 | bits_per_word = spi->bits_per_word; |
| 245 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 246 | if (!hz) |
| 247 | hz = spi->max_speed_hz; |
| 248 | |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 249 | if (!(mpc8xxx_spi->flags & SPI_CPM_MODE)) |
| 250 | bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi, |
| 251 | mpc8xxx_spi, |
| 252 | bits_per_word); |
| 253 | else if (mpc8xxx_spi->flags & SPI_QE) |
| 254 | bits_per_word = mspi_apply_qe_mode_quirks(cs, spi, |
| 255 | bits_per_word); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 256 | |
Joakim Tjernlund | 0398fb7 | 2010-05-14 09:14:26 +0000 | [diff] [blame] | 257 | if (bits_per_word < 0) |
| 258 | return bits_per_word; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 259 | |
| 260 | if (bits_per_word == 32) |
| 261 | bits_per_word = 0; |
| 262 | else |
| 263 | bits_per_word = bits_per_word - 1; |
| 264 | |
Anton Vorontsov | 32421da | 2007-07-31 00:38:41 -0700 | [diff] [blame] | 265 | /* mask out bits we are going to set */ |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 266 | cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16 |
| 267 | | SPMODE_PM(0xF)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 268 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 269 | cs->hw_mode |= SPMODE_LEN(bits_per_word); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 270 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 271 | if ((mpc8xxx_spi->spibrg / hz) > 64) { |
Peter Korsgaard | 53604db | 2008-09-13 02:33:14 -0700 | [diff] [blame] | 272 | cs->hw_mode |= SPMODE_DIV16; |
Ernst Schwab | 4f4517c | 2010-02-16 21:02:57 -0700 | [diff] [blame] | 273 | pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1; |
Markus Elfring | 31ae779 | 2017-01-13 13:50:21 +0100 | [diff] [blame] | 274 | WARN_ONCE(pm > 16, |
| 275 | "%s: Requested speed is too low: %d Hz. Will use %d Hz instead.\n", |
| 276 | dev_name(&spi->dev), hz, mpc8xxx_spi->spibrg / 1024); |
Anton Vorontsov | fd8a11e | 2009-06-18 16:49:01 -0700 | [diff] [blame] | 277 | if (pm > 16) |
Peter Korsgaard | 53604db | 2008-09-13 02:33:14 -0700 | [diff] [blame] | 278 | pm = 16; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 279 | } else { |
Ernst Schwab | 4f4517c | 2010-02-16 21:02:57 -0700 | [diff] [blame] | 280 | pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 281 | } |
Chen Gong | a61f534 | 2008-07-23 21:29:52 -0700 | [diff] [blame] | 282 | if (pm) |
| 283 | pm--; |
| 284 | |
| 285 | cs->hw_mode |= SPMODE_PM(pm); |
David Brownell | dccd573 | 2007-07-17 04:04:02 -0700 | [diff] [blame] | 286 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 287 | fsl_spi_change_mode(spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 291 | static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi, |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 292 | struct spi_transfer *t, unsigned int len) |
| 293 | { |
| 294 | u32 word; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 295 | struct fsl_spi_reg *reg_base = mspi->reg_base; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 296 | |
| 297 | mspi->count = len; |
| 298 | |
| 299 | /* enable rx ints */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 300 | mpc8xxx_spi_write_reg(®_base->mask, SPIM_NE); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 301 | |
| 302 | /* transmit word */ |
| 303 | word = mspi->get_tx(mspi); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 304 | mpc8xxx_spi_write_reg(®_base->transmit, word); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 309 | static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t, |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 310 | bool is_dma_mapped) |
| 311 | { |
| 312 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 313 | struct fsl_spi_reg *reg_base; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 314 | unsigned int len = t->len; |
| 315 | u8 bits_per_word; |
| 316 | int ret; |
| 317 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 318 | reg_base = mpc8xxx_spi->reg_base; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 319 | bits_per_word = spi->bits_per_word; |
| 320 | if (t->bits_per_word) |
| 321 | bits_per_word = t->bits_per_word; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 322 | |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 323 | if (bits_per_word > 8) { |
| 324 | /* invalid length? */ |
| 325 | if (len & 1) |
| 326 | return -EINVAL; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 327 | len /= 2; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 328 | } |
| 329 | if (bits_per_word > 16) { |
| 330 | /* invalid length? */ |
| 331 | if (len & 1) |
| 332 | return -EINVAL; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 333 | len /= 2; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 334 | } |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 335 | |
| 336 | mpc8xxx_spi->tx = t->tx_buf; |
| 337 | mpc8xxx_spi->rx = t->rx_buf; |
Peter Korsgaard | aa77d96 | 2008-09-13 02:33:15 -0700 | [diff] [blame] | 338 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 339 | reinit_completion(&mpc8xxx_spi->done); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 340 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 341 | if (mpc8xxx_spi->flags & SPI_CPM_MODE) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 342 | ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 343 | else |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 344 | ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 345 | if (ret) |
| 346 | return ret; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 347 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 348 | wait_for_completion(&mpc8xxx_spi->done); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 349 | |
| 350 | /* disable rx ints */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 351 | mpc8xxx_spi_write_reg(®_base->mask, 0); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 352 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 353 | if (mpc8xxx_spi->flags & SPI_CPM_MODE) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 354 | fsl_spi_cpm_bufs_complete(mpc8xxx_spi); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 355 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 356 | return mpc8xxx_spi->count; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 359 | static int fsl_spi_do_one_msg(struct spi_master *master, |
| 360 | struct spi_message *m) |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 361 | { |
Rasmus Villemoes | af0e624 | 2019-03-27 14:30:52 +0000 | [diff] [blame] | 362 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 363 | struct spi_device *spi = m->spi; |
Stefan Roese | 4302a596 | 2014-01-31 13:44:59 +0100 | [diff] [blame] | 364 | struct spi_transfer *t, *first; |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 365 | unsigned int cs_change; |
| 366 | const int nsecs = 50; |
Rasmus Villemoes | a798a70 | 2019-03-27 14:30:51 +0000 | [diff] [blame] | 367 | int status, last_bpw; |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 368 | |
Rasmus Villemoes | af0e624 | 2019-03-27 14:30:52 +0000 | [diff] [blame] | 369 | /* |
| 370 | * In CPU mode, optimize large byte transfers to use larger |
| 371 | * bits_per_word values to reduce number of interrupts taken. |
| 372 | */ |
| 373 | if (!(mpc8xxx_spi->flags & SPI_CPM_MODE)) { |
| 374 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 375 | if (t->len < 256 || t->bits_per_word != 8) |
| 376 | continue; |
| 377 | if ((t->len & 3) == 0) |
| 378 | t->bits_per_word = 32; |
| 379 | else if ((t->len & 1) == 0) |
| 380 | t->bits_per_word = 16; |
| 381 | } |
| 382 | } |
| 383 | |
Stefan Roese | 4302a596 | 2014-01-31 13:44:59 +0100 | [diff] [blame] | 384 | /* Don't allow changes if CS is active */ |
Rasmus Villemoes | 17ecffa | 2019-03-27 14:30:51 +0000 | [diff] [blame] | 385 | cs_change = 1; |
Stefan Roese | 4302a596 | 2014-01-31 13:44:59 +0100 | [diff] [blame] | 386 | list_for_each_entry(t, &m->transfers, transfer_list) { |
Rasmus Villemoes | 17ecffa | 2019-03-27 14:30:51 +0000 | [diff] [blame] | 387 | if (cs_change) |
| 388 | first = t; |
| 389 | cs_change = t->cs_change; |
Rasmus Villemoes | a798a70 | 2019-03-27 14:30:51 +0000 | [diff] [blame] | 390 | if (first->speed_hz != t->speed_hz) { |
Stefan Roese | 4302a596 | 2014-01-31 13:44:59 +0100 | [diff] [blame] | 391 | dev_err(&spi->dev, |
Rasmus Villemoes | a798a70 | 2019-03-27 14:30:51 +0000 | [diff] [blame] | 392 | "speed_hz cannot change while CS is active\n"); |
Fabio Estevam | 75c4108 | 2014-12-04 11:15:47 -0200 | [diff] [blame] | 393 | return -EINVAL; |
Stefan Roese | 4302a596 | 2014-01-31 13:44:59 +0100 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Rasmus Villemoes | a798a70 | 2019-03-27 14:30:51 +0000 | [diff] [blame] | 397 | last_bpw = -1; |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 398 | cs_change = 1; |
Stefan Roese | 4302a596 | 2014-01-31 13:44:59 +0100 | [diff] [blame] | 399 | status = -EINVAL; |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 400 | list_for_each_entry(t, &m->transfers, transfer_list) { |
Rasmus Villemoes | a798a70 | 2019-03-27 14:30:51 +0000 | [diff] [blame] | 401 | if (cs_change || last_bpw != t->bits_per_word) |
Rasmus Villemoes | 24c3636 | 2019-03-27 14:30:50 +0000 | [diff] [blame] | 402 | status = fsl_spi_setup_transfer(spi, t); |
| 403 | if (status < 0) |
| 404 | break; |
Rasmus Villemoes | a798a70 | 2019-03-27 14:30:51 +0000 | [diff] [blame] | 405 | last_bpw = t->bits_per_word; |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 406 | |
| 407 | if (cs_change) { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 408 | fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 409 | ndelay(nsecs); |
| 410 | } |
| 411 | cs_change = t->cs_change; |
| 412 | if (t->len) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 413 | status = fsl_spi_bufs(spi, t, m->is_dma_mapped); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 414 | if (status) { |
| 415 | status = -EMSGSIZE; |
| 416 | break; |
| 417 | } |
| 418 | m->actual_length += t->len; |
| 419 | |
| 420 | if (t->delay_usecs) |
| 421 | udelay(t->delay_usecs); |
| 422 | |
| 423 | if (cs_change) { |
| 424 | ndelay(nsecs); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 425 | fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 426 | ndelay(nsecs); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | m->status = status; |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 431 | |
| 432 | if (status || !cs_change) { |
| 433 | ndelay(nsecs); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 434 | fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 437 | fsl_spi_setup_transfer(spi, NULL); |
Christophe Leroy | 44a0421 | 2019-05-22 11:00:36 +0000 | [diff] [blame] | 438 | spi_finalize_current_message(master); |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 439 | return 0; |
Anton Vorontsov | b9b9af1 | 2009-06-18 16:49:06 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 442 | static int fsl_spi_setup(struct spi_device *spi) |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 443 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 444 | struct mpc8xxx_spi *mpc8xxx_spi; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 445 | struct fsl_spi_reg *reg_base; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 446 | int retval; |
| 447 | u32 hw_mode; |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 448 | struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 449 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 450 | if (!spi->max_speed_hz) |
| 451 | return -EINVAL; |
| 452 | |
| 453 | if (!cs) { |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 454 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 455 | if (!cs) |
| 456 | return -ENOMEM; |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 457 | spi_set_ctldata(spi, cs); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 458 | } |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 459 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 460 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 461 | reg_base = mpc8xxx_spi->reg_base; |
| 462 | |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 463 | hw_mode = cs->hw_mode; /* Save original settings */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 464 | cs->hw_mode = mpc8xxx_spi_read_reg(®_base->mode); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 465 | /* mask out bits we are going to set */ |
| 466 | cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH |
| 467 | | SPMODE_REV | SPMODE_LOOP); |
| 468 | |
| 469 | if (spi->mode & SPI_CPHA) |
| 470 | cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK; |
| 471 | if (spi->mode & SPI_CPOL) |
| 472 | cs->hw_mode |= SPMODE_CI_INACTIVEHIGH; |
| 473 | if (!(spi->mode & SPI_LSB_FIRST)) |
| 474 | cs->hw_mode |= SPMODE_REV; |
| 475 | if (spi->mode & SPI_LOOP) |
| 476 | cs->hw_mode |= SPMODE_LOOP; |
| 477 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 478 | retval = fsl_spi_setup_transfer(spi, NULL); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 479 | if (retval < 0) { |
| 480 | cs->hw_mode = hw_mode; /* Restore settings */ |
| 481 | return retval; |
| 482 | } |
Andreas Larsson | f482cd0 | 2013-02-15 16:52:22 +0100 | [diff] [blame] | 483 | |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 484 | if (mpc8xxx_spi->type == TYPE_GRLIB) { |
| 485 | if (gpio_is_valid(spi->cs_gpio)) { |
| 486 | int desel; |
| 487 | |
| 488 | retval = gpio_request(spi->cs_gpio, |
| 489 | dev_name(&spi->dev)); |
| 490 | if (retval) |
| 491 | return retval; |
| 492 | |
| 493 | desel = !(spi->mode & SPI_CS_HIGH); |
| 494 | retval = gpio_direction_output(spi->cs_gpio, desel); |
| 495 | if (retval) { |
| 496 | gpio_free(spi->cs_gpio); |
| 497 | return retval; |
| 498 | } |
| 499 | } else if (spi->cs_gpio != -ENOENT) { |
| 500 | if (spi->cs_gpio < 0) |
| 501 | return spi->cs_gpio; |
| 502 | return -EINVAL; |
| 503 | } |
| 504 | /* When spi->cs_gpio == -ENOENT, a hole in the phandle list |
| 505 | * indicates to use native chipselect if present, or allow for |
| 506 | * an always selected chip |
| 507 | */ |
| 508 | } |
| 509 | |
Andreas Larsson | f482cd0 | 2013-02-15 16:52:22 +0100 | [diff] [blame] | 510 | /* Initialize chipselect - might be active for SPI_CS_HIGH mode */ |
| 511 | fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE); |
| 512 | |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 513 | return 0; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 516 | static void fsl_spi_cleanup(struct spi_device *spi) |
| 517 | { |
| 518 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 519 | struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi); |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 520 | |
| 521 | if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio)) |
| 522 | gpio_free(spi->cs_gpio); |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 523 | |
| 524 | kfree(cs); |
| 525 | spi_set_ctldata(spi, NULL); |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 528 | static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 529 | { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 530 | struct fsl_spi_reg *reg_base = mspi->reg_base; |
| 531 | |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 532 | /* We need handle RX first */ |
| 533 | if (events & SPIE_NE) { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 534 | u32 rx_data = mpc8xxx_spi_read_reg(®_base->receive); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 535 | |
| 536 | if (mspi->rx) |
| 537 | mspi->get_rx(rx_data, mspi); |
| 538 | } |
| 539 | |
| 540 | if ((events & SPIE_NF) == 0) |
| 541 | /* spin until TX is done */ |
| 542 | while (((events = |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 543 | mpc8xxx_spi_read_reg(®_base->event)) & |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 544 | SPIE_NF) == 0) |
| 545 | cpu_relax(); |
| 546 | |
| 547 | /* Clear the events */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 548 | mpc8xxx_spi_write_reg(®_base->event, events); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 549 | |
| 550 | mspi->count -= 1; |
| 551 | if (mspi->count) { |
| 552 | u32 word = mspi->get_tx(mspi); |
| 553 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 554 | mpc8xxx_spi_write_reg(®_base->transmit, word); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 555 | } else { |
| 556 | complete(&mspi->done); |
| 557 | } |
| 558 | } |
| 559 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 560 | static irqreturn_t fsl_spi_irq(s32 irq, void *context_data) |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 561 | { |
| 562 | struct mpc8xxx_spi *mspi = context_data; |
| 563 | irqreturn_t ret = IRQ_NONE; |
| 564 | u32 events; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 565 | struct fsl_spi_reg *reg_base = mspi->reg_base; |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 566 | |
| 567 | /* Get interrupt events(tx/rx) */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 568 | events = mpc8xxx_spi_read_reg(®_base->event); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 569 | if (events) |
| 570 | ret = IRQ_HANDLED; |
| 571 | |
| 572 | dev_dbg(mspi->dev, "%s: events %x\n", __func__, events); |
| 573 | |
| 574 | if (mspi->flags & SPI_CPM_MODE) |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 575 | fsl_spi_cpm_irq(mspi, events); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 576 | else |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 577 | fsl_spi_cpu_irq(mspi, events); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 578 | |
| 579 | return ret; |
| 580 | } |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 581 | |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 582 | static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on) |
| 583 | { |
| 584 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
| 585 | struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base; |
| 586 | u32 slvsel; |
| 587 | u16 cs = spi->chip_select; |
| 588 | |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 589 | if (gpio_is_valid(spi->cs_gpio)) { |
| 590 | gpio_set_value(spi->cs_gpio, on); |
| 591 | } else if (cs < mpc8xxx_spi->native_chipselects) { |
| 592 | slvsel = mpc8xxx_spi_read_reg(®_base->slvsel); |
| 593 | slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs)); |
| 594 | mpc8xxx_spi_write_reg(®_base->slvsel, slvsel); |
| 595 | } |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static void fsl_spi_grlib_probe(struct device *dev) |
| 599 | { |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 600 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 601 | struct spi_master *master = dev_get_drvdata(dev); |
| 602 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
| 603 | struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base; |
| 604 | int mbits; |
| 605 | u32 capabilities; |
| 606 | |
| 607 | capabilities = mpc8xxx_spi_read_reg(®_base->cap); |
| 608 | |
| 609 | mpc8xxx_spi->set_shifts = fsl_spi_grlib_set_shifts; |
| 610 | mbits = SPCAP_MAXWLEN(capabilities); |
| 611 | if (mbits) |
| 612 | mpc8xxx_spi->max_bits_per_word = mbits + 1; |
| 613 | |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 614 | mpc8xxx_spi->native_chipselects = 0; |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 615 | if (SPCAP_SSEN(capabilities)) { |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 616 | mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities); |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 617 | mpc8xxx_spi_write_reg(®_base->slvsel, 0xffffffff); |
| 618 | } |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 619 | master->num_chipselect = mpc8xxx_spi->native_chipselects; |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 620 | pdata->cs_control = fsl_spi_grlib_cs_control; |
| 621 | } |
| 622 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 623 | static struct spi_master * fsl_spi_probe(struct device *dev, |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 624 | struct resource *mem, unsigned int irq) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 625 | { |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 626 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 627 | struct spi_master *master; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 628 | struct mpc8xxx_spi *mpc8xxx_spi; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 629 | struct fsl_spi_reg *reg_base; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 630 | u32 regval; |
| 631 | int ret = 0; |
| 632 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 633 | master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 634 | if (master == NULL) { |
| 635 | ret = -ENOMEM; |
| 636 | goto err; |
| 637 | } |
| 638 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 639 | dev_set_drvdata(dev, master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 640 | |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 641 | mpc8xxx_spi_probe(dev, mem, irq); |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 642 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 643 | master->setup = fsl_spi_setup; |
Andreas Larsson | 76a7498 | 2013-02-15 16:52:27 +0100 | [diff] [blame] | 644 | master->cleanup = fsl_spi_cleanup; |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 645 | master->transfer_one_message = fsl_spi_do_one_msg; |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 646 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 647 | mpc8xxx_spi = spi_master_get_devdata(master); |
Andreas Larsson | 8922a36 | 2013-02-15 16:52:25 +0100 | [diff] [blame] | 648 | mpc8xxx_spi->max_bits_per_word = 32; |
Andreas Larsson | c3f3e77 | 2013-02-15 16:52:24 +0100 | [diff] [blame] | 649 | mpc8xxx_spi->type = fsl_spi_get_type(dev); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 650 | |
| 651 | ret = fsl_spi_cpm_init(mpc8xxx_spi); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 652 | if (ret) |
| 653 | goto err_cpm_init; |
| 654 | |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 655 | mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem); |
Axel Lin | 37c5db7 | 2015-08-30 18:35:51 +0800 | [diff] [blame] | 656 | if (IS_ERR(mpc8xxx_spi->reg_base)) { |
| 657 | ret = PTR_ERR(mpc8xxx_spi->reg_base); |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 658 | goto err_probe; |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | if (mpc8xxx_spi->type == TYPE_GRLIB) |
| 662 | fsl_spi_grlib_probe(dev); |
| 663 | |
Axel Lin | f734394 | 2014-02-13 19:05:38 +0800 | [diff] [blame] | 664 | master->bits_per_word_mask = |
| 665 | (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32)) & |
| 666 | SPI_BPW_RANGE_MASK(1, mpc8xxx_spi->max_bits_per_word); |
| 667 | |
Andreas Larsson | b48c4e3 | 2013-02-15 16:52:23 +0100 | [diff] [blame] | 668 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) |
| 669 | mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts; |
| 670 | |
| 671 | if (mpc8xxx_spi->set_shifts) |
| 672 | /* 8 bits per word and MSB first */ |
| 673 | mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift, |
| 674 | &mpc8xxx_spi->tx_shift, 8, 1); |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 675 | |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 676 | /* Register for SPI Interrupt */ |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 677 | ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_spi_irq, |
| 678 | 0, "fsl_spi", mpc8xxx_spi); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 679 | |
| 680 | if (ret != 0) |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 681 | goto err_probe; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 682 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 683 | reg_base = mpc8xxx_spi->reg_base; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 684 | |
| 685 | /* SPI controller initializations */ |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 686 | mpc8xxx_spi_write_reg(®_base->mode, 0); |
| 687 | mpc8xxx_spi_write_reg(®_base->mask, 0); |
| 688 | mpc8xxx_spi_write_reg(®_base->command, 0); |
| 689 | mpc8xxx_spi_write_reg(®_base->event, 0xffffffff); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 690 | |
| 691 | /* Enable SPI interface */ |
| 692 | regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE; |
Andreas Larsson | 8922a36 | 2013-02-15 16:52:25 +0100 | [diff] [blame] | 693 | if (mpc8xxx_spi->max_bits_per_word < 8) { |
| 694 | regval &= ~SPMODE_LEN(0xF); |
| 695 | regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1); |
| 696 | } |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 697 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) |
Joakim Tjernlund | f29ba28 | 2007-07-17 04:04:12 -0700 | [diff] [blame] | 698 | regval |= SPMODE_OP; |
| 699 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 700 | mpc8xxx_spi_write_reg(®_base->mode, regval); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 701 | |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 702 | ret = devm_spi_register_master(dev, master); |
Joakim Tjernlund | c9bfcb3 | 2008-05-12 14:02:30 -0700 | [diff] [blame] | 703 | if (ret < 0) |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 704 | goto err_probe; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 705 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 706 | dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base, |
Anton Vorontsov | 87ec0e9 | 2009-10-12 20:49:25 +0400 | [diff] [blame] | 707 | mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags)); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 708 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 709 | return master; |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 710 | |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 711 | err_probe: |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 712 | fsl_spi_cpm_free(mpc8xxx_spi); |
Anton Vorontsov | 4c1fba44 | 2009-10-12 20:49:27 +0400 | [diff] [blame] | 713 | err_cpm_init: |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 714 | spi_master_put(master); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 715 | err: |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 716 | return ERR_PTR(ret); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 719 | static void fsl_spi_cs_control(struct spi_device *spi, bool on) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 720 | { |
Herton Ronaldo Krzesinski | 067aa48 | 2012-05-11 15:29:50 -0700 | [diff] [blame] | 721 | struct device *dev = spi->dev.parent->parent; |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 722 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
| 723 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 724 | u16 cs = spi->chip_select; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 725 | |
Rasmus Villemoes | 69b921a | 2019-03-06 10:32:05 +0000 | [diff] [blame] | 726 | if (cs < pinfo->ngpios) { |
| 727 | int gpio = pinfo->gpios[cs]; |
| 728 | bool alow = pinfo->alow_flags[cs]; |
| 729 | |
| 730 | gpio_set_value(gpio, on ^ alow); |
| 731 | } else { |
| 732 | if (WARN_ON_ONCE(cs > pinfo->ngpios || !pinfo->immr_spi_cs)) |
| 733 | return; |
| 734 | iowrite32be(on ? SPI_BOOT_SEL_BIT : 0, pinfo->immr_spi_cs); |
| 735 | } |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 738 | static int of_fsl_spi_get_chipselects(struct device *dev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 739 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 740 | struct device_node *np = dev->of_node; |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 741 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 742 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
Rasmus Villemoes | 69b921a | 2019-03-06 10:32:05 +0000 | [diff] [blame] | 743 | bool spisel_boot = IS_ENABLED(CONFIG_FSL_SOC) && |
| 744 | of_property_read_bool(np, "fsl,spisel_boot"); |
Grant Likely | e80beb2 | 2013-02-12 17:48:37 +0000 | [diff] [blame] | 745 | int ngpios; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 746 | int i = 0; |
| 747 | int ret; |
| 748 | |
| 749 | ngpios = of_gpio_count(np); |
Rasmus Villemoes | 69b921a | 2019-03-06 10:32:05 +0000 | [diff] [blame] | 750 | ngpios = max(ngpios, 0); |
| 751 | if (ngpios == 0 && !spisel_boot) { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 752 | /* |
| 753 | * SPI w/o chip-select line. One SPI device is still permitted |
| 754 | * though. |
| 755 | */ |
| 756 | pdata->max_chipselect = 1; |
| 757 | return 0; |
| 758 | } |
| 759 | |
Rasmus Villemoes | 69b921a | 2019-03-06 10:32:05 +0000 | [diff] [blame] | 760 | pinfo->ngpios = ngpios; |
Markus Elfring | d9bc4a8 | 2017-01-13 13:37:25 +0100 | [diff] [blame] | 761 | pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios), |
| 762 | GFP_KERNEL); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 763 | if (!pinfo->gpios) |
| 764 | return -ENOMEM; |
Roel Kluin | 0214154 | 2009-06-16 15:31:15 -0700 | [diff] [blame] | 765 | memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios)); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 766 | |
Markus Elfring | 5223db0 | 2017-01-13 13:33:02 +0100 | [diff] [blame] | 767 | pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags), |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 768 | GFP_KERNEL); |
| 769 | if (!pinfo->alow_flags) { |
| 770 | ret = -ENOMEM; |
| 771 | goto err_alloc_flags; |
| 772 | } |
| 773 | |
| 774 | for (; i < ngpios; i++) { |
| 775 | int gpio; |
| 776 | enum of_gpio_flags flags; |
| 777 | |
| 778 | gpio = of_get_gpio_flags(np, i, &flags); |
| 779 | if (!gpio_is_valid(gpio)) { |
| 780 | dev_err(dev, "invalid gpio #%d: %d\n", i, gpio); |
Anton Vorontsov | 783058f | 2009-10-12 20:49:22 +0400 | [diff] [blame] | 781 | ret = gpio; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 782 | goto err_loop; |
| 783 | } |
| 784 | |
| 785 | ret = gpio_request(gpio, dev_name(dev)); |
| 786 | if (ret) { |
| 787 | dev_err(dev, "can't request gpio #%d: %d\n", i, ret); |
| 788 | goto err_loop; |
| 789 | } |
| 790 | |
| 791 | pinfo->gpios[i] = gpio; |
| 792 | pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW; |
| 793 | |
| 794 | ret = gpio_direction_output(pinfo->gpios[i], |
| 795 | pinfo->alow_flags[i]); |
| 796 | if (ret) { |
Markus Elfring | 31ae779 | 2017-01-13 13:50:21 +0100 | [diff] [blame] | 797 | dev_err(dev, |
| 798 | "can't set output direction for gpio #%d: %d\n", |
| 799 | i, ret); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 800 | goto err_loop; |
| 801 | } |
| 802 | } |
| 803 | |
Rasmus Villemoes | 69b921a | 2019-03-06 10:32:05 +0000 | [diff] [blame] | 804 | #if IS_ENABLED(CONFIG_FSL_SOC) |
| 805 | if (spisel_boot) { |
| 806 | pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4); |
| 807 | if (!pinfo->immr_spi_cs) { |
| 808 | ret = -ENOMEM; |
| 809 | i = ngpios - 1; |
| 810 | goto err_loop; |
| 811 | } |
| 812 | } |
| 813 | #endif |
| 814 | |
| 815 | pdata->max_chipselect = ngpios + spisel_boot; |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 816 | pdata->cs_control = fsl_spi_cs_control; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 817 | |
| 818 | return 0; |
| 819 | |
| 820 | err_loop: |
| 821 | while (i >= 0) { |
| 822 | if (gpio_is_valid(pinfo->gpios[i])) |
| 823 | gpio_free(pinfo->gpios[i]); |
| 824 | i--; |
| 825 | } |
| 826 | |
| 827 | kfree(pinfo->alow_flags); |
| 828 | pinfo->alow_flags = NULL; |
| 829 | err_alloc_flags: |
| 830 | kfree(pinfo->gpios); |
| 831 | pinfo->gpios = NULL; |
| 832 | return ret; |
| 833 | } |
| 834 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 835 | static int of_fsl_spi_free_chipselects(struct device *dev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 836 | { |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 837 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 838 | struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 839 | int i; |
| 840 | |
| 841 | if (!pinfo->gpios) |
| 842 | return 0; |
| 843 | |
| 844 | for (i = 0; i < pdata->max_chipselect; i++) { |
| 845 | if (gpio_is_valid(pinfo->gpios[i])) |
| 846 | gpio_free(pinfo->gpios[i]); |
| 847 | } |
| 848 | |
| 849 | kfree(pinfo->gpios); |
| 850 | kfree(pinfo->alow_flags); |
| 851 | return 0; |
| 852 | } |
| 853 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 854 | static int of_fsl_spi_probe(struct platform_device *ofdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 855 | { |
| 856 | struct device *dev = &ofdev->dev; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 857 | struct device_node *np = ofdev->dev.of_node; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 858 | struct spi_master *master; |
| 859 | struct resource mem; |
Liu Xiang | 500a32a | 2017-03-09 22:40:01 +0800 | [diff] [blame] | 860 | int irq = 0, type; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 861 | int ret = -ENOMEM; |
| 862 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 863 | ret = of_mpc8xxx_spi_probe(ofdev); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 864 | if (ret) |
| 865 | return ret; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 866 | |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 867 | type = fsl_spi_get_type(&ofdev->dev); |
| 868 | if (type == TYPE_FSL) { |
| 869 | ret = of_fsl_spi_get_chipselects(dev); |
| 870 | if (ret) |
| 871 | goto err; |
| 872 | } |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 873 | |
| 874 | ret = of_address_to_resource(np, 0, &mem); |
| 875 | if (ret) |
| 876 | goto err; |
| 877 | |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 878 | irq = irq_of_parse_and_map(np, 0); |
| 879 | if (!irq) { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 880 | ret = -EINVAL; |
| 881 | goto err; |
| 882 | } |
| 883 | |
Andreas Larsson | e8beacb | 2013-02-15 16:52:21 +0100 | [diff] [blame] | 884 | master = fsl_spi_probe(dev, &mem, irq); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 885 | if (IS_ERR(master)) { |
| 886 | ret = PTR_ERR(master); |
| 887 | goto err; |
| 888 | } |
| 889 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 890 | return 0; |
| 891 | |
| 892 | err: |
Liu Xiang | 500a32a | 2017-03-09 22:40:01 +0800 | [diff] [blame] | 893 | irq_dispose_mapping(irq); |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 894 | if (type == TYPE_FSL) |
| 895 | of_fsl_spi_free_chipselects(dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 896 | return ret; |
| 897 | } |
| 898 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 899 | static int of_fsl_spi_remove(struct platform_device *ofdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 900 | { |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 901 | struct spi_master *master = platform_get_drvdata(ofdev); |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 902 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 903 | |
Heiner Kallweit | 3c5395b | 2015-08-26 21:21:53 +0200 | [diff] [blame] | 904 | fsl_spi_cpm_free(mpc8xxx_spi); |
Andreas Larsson | 447b0c7 | 2013-02-15 16:52:26 +0100 | [diff] [blame] | 905 | if (mpc8xxx_spi->type == TYPE_FSL) |
| 906 | of_fsl_spi_free_chipselects(&ofdev->dev); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 907 | return 0; |
| 908 | } |
| 909 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 910 | static struct platform_driver of_fsl_spi_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 911 | .driver = { |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 912 | .name = "fsl_spi", |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 913 | .of_match_table = of_fsl_spi_match, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 914 | }, |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 915 | .probe = of_fsl_spi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 916 | .remove = of_fsl_spi_remove, |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 917 | }; |
| 918 | |
| 919 | #ifdef CONFIG_MPC832x_RDB |
| 920 | /* |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 921 | * XXX XXX XXX |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 922 | * This is "legacy" platform driver, was used by the MPC8323E-RDB boards |
| 923 | * only. The driver should go away soon, since newer MPC8323E-RDB's device |
| 924 | * tree can work with OpenFirmware driver. But for now we support old trees |
| 925 | * as well. |
| 926 | */ |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 927 | static int plat_mpc8xxx_spi_probe(struct platform_device *pdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 928 | { |
| 929 | struct resource *mem; |
Uwe Kleine-König | e9a172f | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 930 | int irq; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 931 | struct spi_master *master; |
| 932 | |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 933 | if (!dev_get_platdata(&pdev->dev)) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 934 | return -EINVAL; |
| 935 | |
| 936 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 937 | if (!mem) |
| 938 | return -EINVAL; |
| 939 | |
| 940 | irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | e9a172f | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 941 | if (irq <= 0) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 942 | return -EINVAL; |
| 943 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 944 | master = fsl_spi_probe(&pdev->dev, mem, irq); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 945 | return PTR_ERR_OR_ZERO(master); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 948 | static int plat_mpc8xxx_spi_remove(struct platform_device *pdev) |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 949 | { |
Heiner Kallweit | 3c5395b | 2015-08-26 21:21:53 +0200 | [diff] [blame] | 950 | struct spi_master *master = platform_get_drvdata(pdev); |
| 951 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
| 952 | |
| 953 | fsl_spi_cpm_free(mpc8xxx_spi); |
| 954 | |
| 955 | return 0; |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 958 | MODULE_ALIAS("platform:mpc8xxx_spi"); |
| 959 | static struct platform_driver mpc8xxx_spi_driver = { |
| 960 | .probe = plat_mpc8xxx_spi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 961 | .remove = plat_mpc8xxx_spi_remove, |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 962 | .driver = { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 963 | .name = "mpc8xxx_spi", |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 964 | }, |
| 965 | }; |
| 966 | |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 967 | static bool legacy_driver_failed; |
| 968 | |
| 969 | static void __init legacy_driver_register(void) |
| 970 | { |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 971 | legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static void __exit legacy_driver_unregister(void) |
| 975 | { |
| 976 | if (legacy_driver_failed) |
| 977 | return; |
Anton Vorontsov | 575c580 | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 978 | platform_driver_unregister(&mpc8xxx_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 979 | } |
| 980 | #else |
| 981 | static void __init legacy_driver_register(void) {} |
| 982 | static void __exit legacy_driver_unregister(void) {} |
| 983 | #endif /* CONFIG_MPC832x_RDB */ |
| 984 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 985 | static int __init fsl_spi_init(void) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 986 | { |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 987 | legacy_driver_register(); |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 988 | return platform_driver_register(&of_fsl_spi_driver); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 989 | } |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 990 | module_init(fsl_spi_init); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 991 | |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 992 | static void __exit fsl_spi_exit(void) |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 993 | { |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 994 | platform_driver_unregister(&of_fsl_spi_driver); |
Anton Vorontsov | 35b4b3c | 2009-03-31 15:24:37 -0700 | [diff] [blame] | 995 | legacy_driver_unregister(); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 996 | } |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 997 | module_exit(fsl_spi_exit); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 998 | |
| 999 | MODULE_AUTHOR("Kumar Gala"); |
Mingkai Hu | b36ece8 | 2010-10-12 18:18:31 +0800 | [diff] [blame] | 1000 | MODULE_DESCRIPTION("Simple Freescale SPI Driver"); |
Kumar Gala | ccf0699 | 2006-05-20 15:00:15 -0700 | [diff] [blame] | 1001 | MODULE_LICENSE("GPL"); |