blob: 4c71df93a6f66f72c54c91786ec79f31660b420f [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Kumar Galaccf06992006-05-20 15:00:15 -07002/*
Mingkai Hub36ece82010-10-12 18:18:31 +08003 * Freescale SPI controller driver.
Kumar Galaccf06992006-05-20 15:00:15 -07004 *
5 * Maintainer: Kumar Gala
6 *
7 * Copyright (C) 2006 Polycom, Inc.
Mingkai Hub36ece82010-10-12 18:18:31 +08008 * Copyright 2010 Freescale Semiconductor, Inc.
Kumar Galaccf06992006-05-20 15:00:15 -07009 *
Anton Vorontsov4c1fba442009-10-12 20:49:27 +040010 * 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 Larsson447b0c72013-02-15 16:52:26 +010014 * GRLIB support:
15 * Copyright (c) 2012 Aeroflex Gaisler AB.
16 * Author: Andreas Larsson <andreas@gaisler.com>
Kumar Galaccf06992006-05-20 15:00:15 -070017 */
Kumar Galaccf06992006-05-20 15:00:15 -070018#include <linux/delay.h>
Anton Vorontsov4c1fba442009-10-12 20:49:27 +040019#include <linux/dma-mapping.h>
Xiubo Lia3108362014-09-29 10:57:06 +080020#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 Vorontsov4c1fba442009-10-12 20:49:27 +040025#include <linux/mm.h>
Xiubo Lia3108362014-09-29 10:57:06 +080026#include <linux/module.h>
Anton Vorontsov4c1fba442009-10-12 20:49:27 +040027#include <linux/mutex.h>
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070028#include <linux/of.h>
Andreas Larssone8beacb2013-02-15 16:52:21 +010029#include <linux/of_address.h>
30#include <linux/of_irq.h>
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070031#include <linux/of_gpio.h>
Xiubo Lia3108362014-09-29 10:57:06 +080032#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 Galaccf06992006-05-20 15:00:15 -070037
Rasmus Villemoes69b921a2019-03-06 10:32:05 +000038#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 Likelyca632f52011-06-06 01:16:30 -060046#include "spi-fsl-lib.h"
Andreas Larssone8beacb2013-02-15 16:52:21 +010047#include "spi-fsl-cpm.h"
48#include "spi-fsl-spi.h"
Kumar Galaccf06992006-05-20 15:00:15 -070049
Andreas Larssonc3f3e772013-02-15 16:52:24 +010050#define TYPE_FSL 0
Andreas Larsson447b0c72013-02-15 16:52:26 +010051#define TYPE_GRLIB 1
Andreas Larssonc3f3e772013-02-15 16:52:24 +010052
53struct fsl_spi_match_data {
54 int type;
55};
56
57static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
58 .type = TYPE_FSL,
59};
60
Andreas Larsson447b0c72013-02-15 16:52:26 +010061static struct fsl_spi_match_data of_fsl_spi_grlib_config = {
62 .type = TYPE_GRLIB,
63};
64
Jingoo Han3aea9012014-06-03 21:03:59 +090065static const struct of_device_id of_fsl_spi_match[] = {
Andreas Larssonc3f3e772013-02-15 16:52:24 +010066 {
67 .compatible = "fsl,spi",
68 .data = &of_fsl_spi_fsl_config,
69 },
Andreas Larsson447b0c72013-02-15 16:52:26 +010070 {
71 .compatible = "aeroflexgaisler,spictrl",
72 .data = &of_fsl_spi_grlib_config,
73 },
Andreas Larssonc3f3e772013-02-15 16:52:24 +010074 {}
75};
76MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
77
78static 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 Hub36ece82010-10-12 18:18:31 +080090static void fsl_spi_change_mode(struct spi_device *spi)
Anton Vorontsova35c1712009-10-12 20:49:24 +040091{
92 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
93 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Mingkai Hub36ece82010-10-12 18:18:31 +080094 struct fsl_spi_reg *reg_base = mspi->reg_base;
95 __be32 __iomem *mode = &reg_base->mode;
Anton Vorontsova35c1712009-10-12 20:49:24 +040096 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 Vorontsova35c1712009-10-12 20:49:24 +0400106
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400107 /* When in CPM mode, we need to reinit tx and rx. */
108 if (mspi->flags & SPI_CPM_MODE) {
Andreas Larssone8beacb2013-02-15 16:52:21 +0100109 fsl_spi_cpm_reinit_txrx(mspi);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400110 }
Joakim Tjernlundf9218c22010-05-22 02:18:02 -0600111 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
Anton Vorontsova35c1712009-10-12 20:49:24 +0400112 local_irq_restore(flags);
113}
114
Mingkai Hub36ece82010-10-12 18:18:31 +0800115static void fsl_spi_chipselect(struct spi_device *spi, int value)
Kumar Galaccf06992006-05-20 15:00:15 -0700116{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700117 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kenth Eriksson5039a862012-03-30 17:05:30 +0200118 struct fsl_spi_platform_data *pdata;
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700119 bool pol = spi->mode & SPI_CS_HIGH;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700120 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700121
Kenth Eriksson5039a862012-03-30 17:05:30 +0200122 pdata = spi->dev.parent->parent->platform_data;
123
Kumar Galaccf06992006-05-20 15:00:15 -0700124 if (value == BITBANG_CS_INACTIVE) {
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700125 if (pdata->cs_control)
126 pdata->cs_control(spi, !pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700127 }
128
129 if (value == BITBANG_CS_ACTIVE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700130 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 Galaccf06992006-05-20 15:00:15 -0700134
Mingkai Hub36ece82010-10-12 18:18:31 +0800135 fsl_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700136
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700137 if (pdata->cs_control)
138 pdata->cs_control(spi, pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700139 }
140}
141
Andreas Larssonb48c4e32013-02-15 16:52:23 +0100142static 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 Larsson447b0c72013-02-15 16:52:26 +0100161static 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 Hub36ece82010-10-12 18:18:31 +0800176static 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 Tjernlund0398fb72010-05-14 09:14:26 +0000180{
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 Tjernlund0398fb72010-05-14 09:14:26 +0000186 } 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 Tjernlund0398fb72010-05-14 09:14:26 +0000189 } 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 Larssonb48c4e32013-02-15 16:52:23 +0100195 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 Tjernlund0398fb72010-05-14 09:14:26 +0000200 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 Hub36ece82010-10-12 18:18:31 +0800208static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
209 struct spi_device *spi,
210 int bits_per_word)
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000211{
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 Hub36ece82010-10-12 18:18:31 +0800226static int fsl_spi_setup_transfer(struct spi_device *spi,
227 struct spi_transfer *t)
Kumar Galaccf06992006-05-20 15:00:15 -0700228{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700229 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800230 int bits_per_word = 0;
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000231 u8 pm;
Mingkai Hub36ece82010-10-12 18:18:31 +0800232 u32 hz = 0;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700233 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700234
Anton Vorontsov575c5802009-06-18 16:49:08 -0700235 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kumar Galaccf06992006-05-20 15:00:15 -0700236
237 if (t) {
238 bits_per_word = t->bits_per_word;
239 hz = t->speed_hz;
Kumar Galaccf06992006-05-20 15:00:15 -0700240 }
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 Tjernlundc9bfcb32008-05-12 14:02:30 -0700246 if (!hz)
247 hz = spi->max_speed_hz;
248
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000249 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 Galaccf06992006-05-20 15:00:15 -0700256
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000257 if (bits_per_word < 0)
258 return bits_per_word;
Kumar Galaccf06992006-05-20 15:00:15 -0700259
260 if (bits_per_word == 32)
261 bits_per_word = 0;
262 else
263 bits_per_word = bits_per_word - 1;
264
Anton Vorontsov32421da2007-07-31 00:38:41 -0700265 /* mask out bits we are going to set */
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700266 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
267 | SPMODE_PM(0xF));
Kumar Galaccf06992006-05-20 15:00:15 -0700268
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700269 cs->hw_mode |= SPMODE_LEN(bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700270
Anton Vorontsov575c5802009-06-18 16:49:08 -0700271 if ((mpc8xxx_spi->spibrg / hz) > 64) {
Peter Korsgaard53604db2008-09-13 02:33:14 -0700272 cs->hw_mode |= SPMODE_DIV16;
Ernst Schwab4f4517c2010-02-16 21:02:57 -0700273 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
Markus Elfring31ae7792017-01-13 13:50:21 +0100274 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 Vorontsovfd8a11e2009-06-18 16:49:01 -0700277 if (pm > 16)
Peter Korsgaard53604db2008-09-13 02:33:14 -0700278 pm = 16;
Mingkai Hub36ece82010-10-12 18:18:31 +0800279 } else {
Ernst Schwab4f4517c2010-02-16 21:02:57 -0700280 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
Mingkai Hub36ece82010-10-12 18:18:31 +0800281 }
Chen Gonga61f5342008-07-23 21:29:52 -0700282 if (pm)
283 pm--;
284
285 cs->hw_mode |= SPMODE_PM(pm);
David Brownelldccd5732007-07-17 04:04:02 -0700286
Mingkai Hub36ece82010-10-12 18:18:31 +0800287 fsl_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700288 return 0;
289}
290
Mingkai Hub36ece82010-10-12 18:18:31 +0800291static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400292 struct spi_transfer *t, unsigned int len)
293{
294 u32 word;
Mingkai Hub36ece82010-10-12 18:18:31 +0800295 struct fsl_spi_reg *reg_base = mspi->reg_base;
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400296
297 mspi->count = len;
298
299 /* enable rx ints */
Mingkai Hub36ece82010-10-12 18:18:31 +0800300 mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400301
302 /* transmit word */
303 word = mspi->get_tx(mspi);
Mingkai Hub36ece82010-10-12 18:18:31 +0800304 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400305
306 return 0;
307}
308
Mingkai Hub36ece82010-10-12 18:18:31 +0800309static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400310 bool is_dma_mapped)
311{
312 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Mingkai Hub36ece82010-10-12 18:18:31 +0800313 struct fsl_spi_reg *reg_base;
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400314 unsigned int len = t->len;
315 u8 bits_per_word;
316 int ret;
317
Mingkai Hub36ece82010-10-12 18:18:31 +0800318 reg_base = mpc8xxx_spi->reg_base;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700319 bits_per_word = spi->bits_per_word;
320 if (t->bits_per_word)
321 bits_per_word = t->bits_per_word;
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400322
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700323 if (bits_per_word > 8) {
324 /* invalid length? */
325 if (len & 1)
326 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700327 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700328 }
329 if (bits_per_word > 16) {
330 /* invalid length? */
331 if (len & 1)
332 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700333 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700334 }
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400335
336 mpc8xxx_spi->tx = t->tx_buf;
337 mpc8xxx_spi->rx = t->rx_buf;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700338
Wolfram Sang16735d02013-11-14 14:32:02 -0800339 reinit_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700340
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400341 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
Mingkai Hub36ece82010-10-12 18:18:31 +0800342 ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400343 else
Mingkai Hub36ece82010-10-12 18:18:31 +0800344 ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400345 if (ret)
346 return ret;
Kumar Galaccf06992006-05-20 15:00:15 -0700347
Anton Vorontsov575c5802009-06-18 16:49:08 -0700348 wait_for_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700349
350 /* disable rx ints */
Mingkai Hub36ece82010-10-12 18:18:31 +0800351 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
Kumar Galaccf06992006-05-20 15:00:15 -0700352
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400353 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
Mingkai Hub36ece82010-10-12 18:18:31 +0800354 fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400355
Anton Vorontsov575c5802009-06-18 16:49:08 -0700356 return mpc8xxx_spi->count;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700357}
358
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100359static int fsl_spi_do_one_msg(struct spi_master *master,
360 struct spi_message *m)
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700361{
Rasmus Villemoesaf0e6242019-03-27 14:30:52 +0000362 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700363 struct spi_device *spi = m->spi;
Stefan Roese4302a5962014-01-31 13:44:59 +0100364 struct spi_transfer *t, *first;
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700365 unsigned int cs_change;
366 const int nsecs = 50;
Rasmus Villemoesa798a702019-03-27 14:30:51 +0000367 int status, last_bpw;
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700368
Rasmus Villemoesaf0e6242019-03-27 14:30:52 +0000369 /*
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 Roese4302a5962014-01-31 13:44:59 +0100384 /* Don't allow changes if CS is active */
Rasmus Villemoes17ecffa2019-03-27 14:30:51 +0000385 cs_change = 1;
Stefan Roese4302a5962014-01-31 13:44:59 +0100386 list_for_each_entry(t, &m->transfers, transfer_list) {
Rasmus Villemoes17ecffa2019-03-27 14:30:51 +0000387 if (cs_change)
388 first = t;
389 cs_change = t->cs_change;
Rasmus Villemoesa798a702019-03-27 14:30:51 +0000390 if (first->speed_hz != t->speed_hz) {
Stefan Roese4302a5962014-01-31 13:44:59 +0100391 dev_err(&spi->dev,
Rasmus Villemoesa798a702019-03-27 14:30:51 +0000392 "speed_hz cannot change while CS is active\n");
Fabio Estevam75c41082014-12-04 11:15:47 -0200393 return -EINVAL;
Stefan Roese4302a5962014-01-31 13:44:59 +0100394 }
395 }
396
Rasmus Villemoesa798a702019-03-27 14:30:51 +0000397 last_bpw = -1;
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700398 cs_change = 1;
Stefan Roese4302a5962014-01-31 13:44:59 +0100399 status = -EINVAL;
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700400 list_for_each_entry(t, &m->transfers, transfer_list) {
Rasmus Villemoesa798a702019-03-27 14:30:51 +0000401 if (cs_change || last_bpw != t->bits_per_word)
Rasmus Villemoes24c36362019-03-27 14:30:50 +0000402 status = fsl_spi_setup_transfer(spi, t);
403 if (status < 0)
404 break;
Rasmus Villemoesa798a702019-03-27 14:30:51 +0000405 last_bpw = t->bits_per_word;
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700406
407 if (cs_change) {
Mingkai Hub36ece82010-10-12 18:18:31 +0800408 fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700409 ndelay(nsecs);
410 }
411 cs_change = t->cs_change;
412 if (t->len)
Mingkai Hub36ece82010-10-12 18:18:31 +0800413 status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700414 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 Hub36ece82010-10-12 18:18:31 +0800425 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700426 ndelay(nsecs);
427 }
428 }
429
430 m->status = status;
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100431 spi_finalize_current_message(master);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700432
433 if (status || !cs_change) {
434 ndelay(nsecs);
Mingkai Hub36ece82010-10-12 18:18:31 +0800435 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700436 }
437
Mingkai Hub36ece82010-10-12 18:18:31 +0800438 fsl_spi_setup_transfer(spi, NULL);
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100439 return 0;
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700440}
441
Mingkai Hub36ece82010-10-12 18:18:31 +0800442static int fsl_spi_setup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700443{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700444 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800445 struct fsl_spi_reg *reg_base;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700446 int retval;
447 u32 hw_mode;
Axel Lind9f26742014-08-31 12:44:09 +0800448 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700449
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700450 if (!spi->max_speed_hz)
451 return -EINVAL;
452
453 if (!cs) {
Axel Lind9f26742014-08-31 12:44:09 +0800454 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700455 if (!cs)
456 return -ENOMEM;
Axel Lind9f26742014-08-31 12:44:09 +0800457 spi_set_ctldata(spi, cs);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700458 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700459 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700460
Mingkai Hub36ece82010-10-12 18:18:31 +0800461 reg_base = mpc8xxx_spi->reg_base;
462
Thomas Weber88393162010-03-16 11:47:56 +0100463 hw_mode = cs->hw_mode; /* Save original settings */
Mingkai Hub36ece82010-10-12 18:18:31 +0800464 cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700465 /* 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 Hub36ece82010-10-12 18:18:31 +0800478 retval = fsl_spi_setup_transfer(spi, NULL);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700479 if (retval < 0) {
480 cs->hw_mode = hw_mode; /* Restore settings */
481 return retval;
482 }
Andreas Larssonf482cd02013-02-15 16:52:22 +0100483
Andreas Larsson76a74982013-02-15 16:52:27 +0100484 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 Larssonf482cd02013-02-15 16:52:22 +0100510 /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
511 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
512
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700513 return 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700514}
515
Andreas Larsson76a74982013-02-15 16:52:27 +0100516static void fsl_spi_cleanup(struct spi_device *spi)
517{
518 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Axel Lind9f26742014-08-31 12:44:09 +0800519 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
Andreas Larsson76a74982013-02-15 16:52:27 +0100520
521 if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
522 gpio_free(spi->cs_gpio);
Axel Lind9f26742014-08-31 12:44:09 +0800523
524 kfree(cs);
525 spi_set_ctldata(spi, NULL);
Andreas Larsson76a74982013-02-15 16:52:27 +0100526}
527
Mingkai Hub36ece82010-10-12 18:18:31 +0800528static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400529{
Mingkai Hub36ece82010-10-12 18:18:31 +0800530 struct fsl_spi_reg *reg_base = mspi->reg_base;
531
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400532 /* We need handle RX first */
533 if (events & SPIE_NE) {
Mingkai Hub36ece82010-10-12 18:18:31 +0800534 u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400535
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 Hub36ece82010-10-12 18:18:31 +0800543 mpc8xxx_spi_read_reg(&reg_base->event)) &
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400544 SPIE_NF) == 0)
545 cpu_relax();
546
547 /* Clear the events */
Mingkai Hub36ece82010-10-12 18:18:31 +0800548 mpc8xxx_spi_write_reg(&reg_base->event, events);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400549
550 mspi->count -= 1;
551 if (mspi->count) {
552 u32 word = mspi->get_tx(mspi);
553
Mingkai Hub36ece82010-10-12 18:18:31 +0800554 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400555 } else {
556 complete(&mspi->done);
557 }
558}
559
Mingkai Hub36ece82010-10-12 18:18:31 +0800560static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400561{
562 struct mpc8xxx_spi *mspi = context_data;
563 irqreturn_t ret = IRQ_NONE;
564 u32 events;
Mingkai Hub36ece82010-10-12 18:18:31 +0800565 struct fsl_spi_reg *reg_base = mspi->reg_base;
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400566
567 /* Get interrupt events(tx/rx) */
Mingkai Hub36ece82010-10-12 18:18:31 +0800568 events = mpc8xxx_spi_read_reg(&reg_base->event);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400569 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 Hub36ece82010-10-12 18:18:31 +0800575 fsl_spi_cpm_irq(mspi, events);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400576 else
Mingkai Hub36ece82010-10-12 18:18:31 +0800577 fsl_spi_cpu_irq(mspi, events);
Kumar Galaccf06992006-05-20 15:00:15 -0700578
579 return ret;
580}
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400581
Andreas Larsson447b0c72013-02-15 16:52:26 +0100582static 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 Larsson76a74982013-02-15 16:52:27 +0100589 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(&reg_base->slvsel);
593 slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
594 mpc8xxx_spi_write_reg(&reg_base->slvsel, slvsel);
595 }
Andreas Larsson447b0c72013-02-15 16:52:26 +0100596}
597
598static void fsl_spi_grlib_probe(struct device *dev)
599{
Jingoo Han8074cf02013-07-30 16:58:59 +0900600 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
Andreas Larsson447b0c72013-02-15 16:52:26 +0100601 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(&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 Larsson76a74982013-02-15 16:52:27 +0100614 mpc8xxx_spi->native_chipselects = 0;
Andreas Larsson447b0c72013-02-15 16:52:26 +0100615 if (SPCAP_SSEN(capabilities)) {
Andreas Larsson76a74982013-02-15 16:52:27 +0100616 mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities);
Andreas Larsson447b0c72013-02-15 16:52:26 +0100617 mpc8xxx_spi_write_reg(&reg_base->slvsel, 0xffffffff);
618 }
Andreas Larsson76a74982013-02-15 16:52:27 +0100619 master->num_chipselect = mpc8xxx_spi->native_chipselects;
Andreas Larsson447b0c72013-02-15 16:52:26 +0100620 pdata->cs_control = fsl_spi_grlib_cs_control;
621}
622
Grant Likelyfd4a3192012-12-07 16:57:14 +0000623static struct spi_master * fsl_spi_probe(struct device *dev,
Mingkai Hub36ece82010-10-12 18:18:31 +0800624 struct resource *mem, unsigned int irq)
Kumar Galaccf06992006-05-20 15:00:15 -0700625{
Jingoo Han8074cf02013-07-30 16:58:59 +0900626 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
Kumar Galaccf06992006-05-20 15:00:15 -0700627 struct spi_master *master;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700628 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800629 struct fsl_spi_reg *reg_base;
Kumar Galaccf06992006-05-20 15:00:15 -0700630 u32 regval;
631 int ret = 0;
632
Anton Vorontsov575c5802009-06-18 16:49:08 -0700633 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
Kumar Galaccf06992006-05-20 15:00:15 -0700634 if (master == NULL) {
635 ret = -ENOMEM;
636 goto err;
637 }
638
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700639 dev_set_drvdata(dev, master);
Kumar Galaccf06992006-05-20 15:00:15 -0700640
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100641 mpc8xxx_spi_probe(dev, mem, irq);
David Brownelle7db06b2009-06-17 16:26:04 -0700642
Mingkai Hub36ece82010-10-12 18:18:31 +0800643 master->setup = fsl_spi_setup;
Andreas Larsson76a74982013-02-15 16:52:27 +0100644 master->cleanup = fsl_spi_cleanup;
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100645 master->transfer_one_message = fsl_spi_do_one_msg;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700646
Anton Vorontsov575c5802009-06-18 16:49:08 -0700647 mpc8xxx_spi = spi_master_get_devdata(master);
Andreas Larsson8922a362013-02-15 16:52:25 +0100648 mpc8xxx_spi->max_bits_per_word = 32;
Andreas Larssonc3f3e772013-02-15 16:52:24 +0100649 mpc8xxx_spi->type = fsl_spi_get_type(dev);
Mingkai Hub36ece82010-10-12 18:18:31 +0800650
651 ret = fsl_spi_cpm_init(mpc8xxx_spi);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400652 if (ret)
653 goto err_cpm_init;
654
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200655 mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
Axel Lin37c5db72015-08-30 18:35:51 +0800656 if (IS_ERR(mpc8xxx_spi->reg_base)) {
657 ret = PTR_ERR(mpc8xxx_spi->reg_base);
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200658 goto err_probe;
Andreas Larsson447b0c72013-02-15 16:52:26 +0100659 }
660
661 if (mpc8xxx_spi->type == TYPE_GRLIB)
662 fsl_spi_grlib_probe(dev);
663
Axel Linf7343942014-02-13 19:05:38 +0800664 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 Larssonb48c4e32013-02-15 16:52:23 +0100668 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 Tjernlundf29ba282007-07-17 04:04:12 -0700675
Kumar Galaccf06992006-05-20 15:00:15 -0700676 /* Register for SPI Interrupt */
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200677 ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_spi_irq,
678 0, "fsl_spi", mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700679
680 if (ret != 0)
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200681 goto err_probe;
Kumar Galaccf06992006-05-20 15:00:15 -0700682
Mingkai Hub36ece82010-10-12 18:18:31 +0800683 reg_base = mpc8xxx_spi->reg_base;
Kumar Galaccf06992006-05-20 15:00:15 -0700684
685 /* SPI controller initializations */
Mingkai Hub36ece82010-10-12 18:18:31 +0800686 mpc8xxx_spi_write_reg(&reg_base->mode, 0);
687 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
688 mpc8xxx_spi_write_reg(&reg_base->command, 0);
689 mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
Kumar Galaccf06992006-05-20 15:00:15 -0700690
691 /* Enable SPI interface */
692 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
Andreas Larsson8922a362013-02-15 16:52:25 +0100693 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 Vorontsov87ec0e92009-10-12 20:49:25 +0400697 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700698 regval |= SPMODE_OP;
699
Mingkai Hub36ece82010-10-12 18:18:31 +0800700 mpc8xxx_spi_write_reg(&reg_base->mode, regval);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700701
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200702 ret = devm_spi_register_master(dev, master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700703 if (ret < 0)
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200704 goto err_probe;
Kumar Galaccf06992006-05-20 15:00:15 -0700705
Mingkai Hub36ece82010-10-12 18:18:31 +0800706 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400707 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
Kumar Galaccf06992006-05-20 15:00:15 -0700708
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700709 return master;
Kumar Galaccf06992006-05-20 15:00:15 -0700710
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200711err_probe:
Mingkai Hub36ece82010-10-12 18:18:31 +0800712 fsl_spi_cpm_free(mpc8xxx_spi);
Anton Vorontsov4c1fba442009-10-12 20:49:27 +0400713err_cpm_init:
Kumar Galaccf06992006-05-20 15:00:15 -0700714 spi_master_put(master);
Kumar Galaccf06992006-05-20 15:00:15 -0700715err:
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700716 return ERR_PTR(ret);
Kumar Galaccf06992006-05-20 15:00:15 -0700717}
718
Mingkai Hub36ece82010-10-12 18:18:31 +0800719static void fsl_spi_cs_control(struct spi_device *spi, bool on)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700720{
Herton Ronaldo Krzesinski067aa482012-05-11 15:29:50 -0700721 struct device *dev = spi->dev.parent->parent;
Jingoo Han8074cf02013-07-30 16:58:59 +0900722 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
723 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700724 u16 cs = spi->chip_select;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700725
Rasmus Villemoes69b921a2019-03-06 10:32:05 +0000726 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 Vorontsov35b4b3c2009-03-31 15:24:37 -0700736}
737
Mingkai Hub36ece82010-10-12 18:18:31 +0800738static int of_fsl_spi_get_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700739{
Grant Likely61c7a082010-04-13 16:12:29 -0700740 struct device_node *np = dev->of_node;
Jingoo Han8074cf02013-07-30 16:58:59 +0900741 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700742 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Rasmus Villemoes69b921a2019-03-06 10:32:05 +0000743 bool spisel_boot = IS_ENABLED(CONFIG_FSL_SOC) &&
744 of_property_read_bool(np, "fsl,spisel_boot");
Grant Likelye80beb22013-02-12 17:48:37 +0000745 int ngpios;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700746 int i = 0;
747 int ret;
748
749 ngpios = of_gpio_count(np);
Rasmus Villemoes69b921a2019-03-06 10:32:05 +0000750 ngpios = max(ngpios, 0);
751 if (ngpios == 0 && !spisel_boot) {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700752 /*
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 Villemoes69b921a2019-03-06 10:32:05 +0000760 pinfo->ngpios = ngpios;
Markus Elfringd9bc4a82017-01-13 13:37:25 +0100761 pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),
762 GFP_KERNEL);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700763 if (!pinfo->gpios)
764 return -ENOMEM;
Roel Kluin02141542009-06-16 15:31:15 -0700765 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700766
Markus Elfring5223db02017-01-13 13:33:02 +0100767 pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700768 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 Vorontsov783058f2009-10-12 20:49:22 +0400781 ret = gpio;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700782 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 Elfring31ae7792017-01-13 13:50:21 +0100797 dev_err(dev,
798 "can't set output direction for gpio #%d: %d\n",
799 i, ret);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700800 goto err_loop;
801 }
802 }
803
Rasmus Villemoes69b921a2019-03-06 10:32:05 +0000804#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 Hub36ece82010-10-12 18:18:31 +0800816 pdata->cs_control = fsl_spi_cs_control;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700817
818 return 0;
819
820err_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;
829err_alloc_flags:
830 kfree(pinfo->gpios);
831 pinfo->gpios = NULL;
832 return ret;
833}
834
Mingkai Hub36ece82010-10-12 18:18:31 +0800835static int of_fsl_spi_free_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700836{
Jingoo Han8074cf02013-07-30 16:58:59 +0900837 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700838 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700839 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 Likelyfd4a3192012-12-07 16:57:14 +0000854static int of_fsl_spi_probe(struct platform_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700855{
856 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -0700857 struct device_node *np = ofdev->dev.of_node;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700858 struct spi_master *master;
859 struct resource mem;
Liu Xiang500a32a2017-03-09 22:40:01 +0800860 int irq = 0, type;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700861 int ret = -ENOMEM;
862
Grant Likely18d306d2011-02-22 21:02:43 -0700863 ret = of_mpc8xxx_spi_probe(ofdev);
Mingkai Hub36ece82010-10-12 18:18:31 +0800864 if (ret)
865 return ret;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700866
Andreas Larsson447b0c72013-02-15 16:52:26 +0100867 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 Vorontsov35b4b3c2009-03-31 15:24:37 -0700873
874 ret = of_address_to_resource(np, 0, &mem);
875 if (ret)
876 goto err;
877
Andreas Larssone8beacb2013-02-15 16:52:21 +0100878 irq = irq_of_parse_and_map(np, 0);
879 if (!irq) {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700880 ret = -EINVAL;
881 goto err;
882 }
883
Andreas Larssone8beacb2013-02-15 16:52:21 +0100884 master = fsl_spi_probe(dev, &mem, irq);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700885 if (IS_ERR(master)) {
886 ret = PTR_ERR(master);
887 goto err;
888 }
889
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700890 return 0;
891
892err:
Liu Xiang500a32a2017-03-09 22:40:01 +0800893 irq_dispose_mapping(irq);
Andreas Larsson447b0c72013-02-15 16:52:26 +0100894 if (type == TYPE_FSL)
895 of_fsl_spi_free_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700896 return ret;
897}
898
Grant Likelyfd4a3192012-12-07 16:57:14 +0000899static int of_fsl_spi_remove(struct platform_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700900{
Jingoo Han24b5a822013-05-23 19:20:40 +0900901 struct spi_master *master = platform_get_drvdata(ofdev);
Andreas Larsson447b0c72013-02-15 16:52:26 +0100902 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700903
Heiner Kallweit3c5395b2015-08-26 21:21:53 +0200904 fsl_spi_cpm_free(mpc8xxx_spi);
Andreas Larsson447b0c72013-02-15 16:52:26 +0100905 if (mpc8xxx_spi->type == TYPE_FSL)
906 of_fsl_spi_free_chipselects(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700907 return 0;
908}
909
Grant Likely18d306d2011-02-22 21:02:43 -0700910static struct platform_driver of_fsl_spi_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700911 .driver = {
Mingkai Hub36ece82010-10-12 18:18:31 +0800912 .name = "fsl_spi",
Mingkai Hub36ece82010-10-12 18:18:31 +0800913 .of_match_table = of_fsl_spi_match,
Grant Likely40182942010-04-13 16:13:02 -0700914 },
Mingkai Hub36ece82010-10-12 18:18:31 +0800915 .probe = of_fsl_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000916 .remove = of_fsl_spi_remove,
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700917};
918
919#ifdef CONFIG_MPC832x_RDB
920/*
Mingkai Hub36ece82010-10-12 18:18:31 +0800921 * XXX XXX XXX
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700922 * 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 Likelyfd4a3192012-12-07 16:57:14 +0000927static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700928{
929 struct resource *mem;
Uwe Kleine-Könige9a172f2010-01-20 13:49:44 -0700930 int irq;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700931 struct spi_master *master;
932
Jingoo Han8074cf02013-07-30 16:58:59 +0900933 if (!dev_get_platdata(&pdev->dev))
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700934 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önige9a172f2010-01-20 13:49:44 -0700941 if (irq <= 0)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700942 return -EINVAL;
943
Mingkai Hub36ece82010-10-12 18:18:31 +0800944 master = fsl_spi_probe(&pdev->dev, mem, irq);
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930945 return PTR_ERR_OR_ZERO(master);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700946}
947
Grant Likelyfd4a3192012-12-07 16:57:14 +0000948static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700949{
Heiner Kallweit3c5395b2015-08-26 21:21:53 +0200950 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 Vorontsov35b4b3c2009-03-31 15:24:37 -0700956}
957
Anton Vorontsov575c5802009-06-18 16:49:08 -0700958MODULE_ALIAS("platform:mpc8xxx_spi");
959static struct platform_driver mpc8xxx_spi_driver = {
960 .probe = plat_mpc8xxx_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000961 .remove = plat_mpc8xxx_spi_remove,
Kumar Galaccf06992006-05-20 15:00:15 -0700962 .driver = {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700963 .name = "mpc8xxx_spi",
Kumar Galaccf06992006-05-20 15:00:15 -0700964 },
965};
966
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700967static bool legacy_driver_failed;
968
969static void __init legacy_driver_register(void)
970{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700971 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700972}
973
974static void __exit legacy_driver_unregister(void)
975{
976 if (legacy_driver_failed)
977 return;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700978 platform_driver_unregister(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700979}
980#else
981static void __init legacy_driver_register(void) {}
982static void __exit legacy_driver_unregister(void) {}
983#endif /* CONFIG_MPC832x_RDB */
984
Mingkai Hub36ece82010-10-12 18:18:31 +0800985static int __init fsl_spi_init(void)
Kumar Galaccf06992006-05-20 15:00:15 -0700986{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700987 legacy_driver_register();
Grant Likely18d306d2011-02-22 21:02:43 -0700988 return platform_driver_register(&of_fsl_spi_driver);
Kumar Galaccf06992006-05-20 15:00:15 -0700989}
Mingkai Hub36ece82010-10-12 18:18:31 +0800990module_init(fsl_spi_init);
Kumar Galaccf06992006-05-20 15:00:15 -0700991
Mingkai Hub36ece82010-10-12 18:18:31 +0800992static void __exit fsl_spi_exit(void)
Kumar Galaccf06992006-05-20 15:00:15 -0700993{
Grant Likely18d306d2011-02-22 21:02:43 -0700994 platform_driver_unregister(&of_fsl_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700995 legacy_driver_unregister();
Kumar Galaccf06992006-05-20 15:00:15 -0700996}
Mingkai Hub36ece82010-10-12 18:18:31 +0800997module_exit(fsl_spi_exit);
Kumar Galaccf06992006-05-20 15:00:15 -0700998
999MODULE_AUTHOR("Kumar Gala");
Mingkai Hub36ece82010-10-12 18:18:31 +08001000MODULE_DESCRIPTION("Simple Freescale SPI Driver");
Kumar Galaccf06992006-05-20 15:00:15 -07001001MODULE_LICENSE("GPL");