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