blob: 8539f0584a8ae1fa69896e3e2297e103ff06a751 [file] [log] [blame]
Mingkai Hu8b60d6c2010-10-12 18:18:32 +08001/*
2 * Freescale eSPI controller driver.
3 *
4 * Copyright 2010 Freescale Semiconductor, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 */
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080011#include <linux/delay.h>
Xiubo Lia3108362014-09-29 10:57:06 +080012#include <linux/err.h>
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080013#include <linux/fsl_devices.h>
Xiubo Lia3108362014-09-29 10:57:06 +080014#include <linux/interrupt.h>
Xiubo Lia3108362014-09-29 10:57:06 +080015#include <linux/module.h>
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080016#include <linux/mm.h>
17#include <linux/of.h>
Rob Herring5af50732013-09-17 14:28:33 -050018#include <linux/of_address.h>
19#include <linux/of_irq.h>
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080020#include <linux/of_platform.h>
Xiubo Lia3108362014-09-29 10:57:06 +080021#include <linux/platform_device.h>
22#include <linux/spi/spi.h>
Heiner Kallweite9abb4d2015-08-26 21:21:55 +020023#include <linux/pm_runtime.h>
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080024#include <sysdev/fsl_soc.h>
25
Grant Likelyca632f52011-06-06 01:16:30 -060026#include "spi-fsl-lib.h"
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080027
28/* eSPI Controller registers */
Heiner Kallweit46afd382016-09-13 23:16:02 +020029#define ESPI_SPMODE 0x00 /* eSPI mode register */
30#define ESPI_SPIE 0x04 /* eSPI event register */
31#define ESPI_SPIM 0x08 /* eSPI mask register */
32#define ESPI_SPCOM 0x0c /* eSPI command register */
33#define ESPI_SPITF 0x10 /* eSPI transmit FIFO access register*/
34#define ESPI_SPIRF 0x14 /* eSPI receive FIFO access register*/
35#define ESPI_SPMODE0 0x20 /* eSPI cs0 mode register */
36
37#define ESPI_SPMODEx(x) (ESPI_SPMODE0 + (x) * 4)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080038
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080039/* eSPI Controller mode register definitions */
Heiner Kallweit81abc2e2016-09-13 23:16:06 +020040#define SPMODE_ENABLE BIT(31)
41#define SPMODE_LOOP BIT(30)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080042#define SPMODE_TXTHR(x) ((x) << 8)
43#define SPMODE_RXTHR(x) ((x) << 0)
44
45/* eSPI Controller CS mode register definitions */
Heiner Kallweit81abc2e2016-09-13 23:16:06 +020046#define CSMODE_CI_INACTIVEHIGH BIT(31)
47#define CSMODE_CP_BEGIN_EDGECLK BIT(30)
48#define CSMODE_REV BIT(29)
49#define CSMODE_DIV16 BIT(28)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080050#define CSMODE_PM(x) ((x) << 24)
Heiner Kallweit81abc2e2016-09-13 23:16:06 +020051#define CSMODE_POL_1 BIT(20)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080052#define CSMODE_LEN(x) ((x) << 16)
53#define CSMODE_BEF(x) ((x) << 12)
54#define CSMODE_AFT(x) ((x) << 8)
55#define CSMODE_CG(x) ((x) << 3)
56
Heiner Kallweit54731262016-10-27 21:25:58 +020057#define FSL_ESPI_FIFO_SIZE 32
Heiner Kallweite508cea2016-10-27 21:27:56 +020058#define FSL_ESPI_RXTHR 15
Heiner Kallweit54731262016-10-27 21:25:58 +020059
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080060/* Default mode/csmode for eSPI controller */
Heiner Kallweite508cea2016-10-27 21:27:56 +020061#define SPMODE_INIT_VAL (SPMODE_TXTHR(4) | SPMODE_RXTHR(FSL_ESPI_RXTHR))
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080062#define CSMODE_INIT_VAL (CSMODE_POL_1 | CSMODE_BEF(0) \
63 | CSMODE_AFT(0) | CSMODE_CG(1))
64
65/* SPIE register values */
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080066#define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F)
67#define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F)
Heiner Kallweit81abc2e2016-09-13 23:16:06 +020068#define SPIE_TXE BIT(15) /* TX FIFO empty */
69#define SPIE_DON BIT(14) /* TX done */
70#define SPIE_RXT BIT(13) /* RX FIFO threshold */
71#define SPIE_RXF BIT(12) /* RX FIFO full */
72#define SPIE_TXT BIT(11) /* TX FIFO threshold*/
73#define SPIE_RNE BIT(9) /* RX FIFO not empty */
74#define SPIE_TNF BIT(8) /* TX FIFO not full */
75
76/* SPIM register values */
77#define SPIM_TXE BIT(15) /* TX FIFO empty */
78#define SPIM_DON BIT(14) /* TX done */
79#define SPIM_RXT BIT(13) /* RX FIFO threshold */
80#define SPIM_RXF BIT(12) /* RX FIFO full */
81#define SPIM_TXT BIT(11) /* TX FIFO threshold*/
82#define SPIM_RNE BIT(9) /* RX FIFO not empty */
83#define SPIM_TNF BIT(8) /* TX FIFO not full */
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080084
85/* SPCOM register values */
86#define SPCOM_CS(x) ((x) << 30)
Heiner Kallweit81abc2e2016-09-13 23:16:06 +020087#define SPCOM_DO BIT(28) /* Dual output */
88#define SPCOM_TO BIT(27) /* TX only */
89#define SPCOM_RXSKIP(x) ((x) << 16)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080090#define SPCOM_TRANLEN(x) ((x) << 0)
Heiner Kallweit81abc2e2016-09-13 23:16:06 +020091
Hou Zhiqiang5cfa1e42016-01-22 18:58:26 +080092#define SPCOM_TRANLEN_MAX 0x10000 /* Max transaction length */
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080093
Heiner Kallweite9abb4d2015-08-26 21:21:55 +020094#define AUTOSUSPEND_TIMEOUT 2000
95
Heiner Kallweit35ab0462016-11-13 14:40:51 +010096struct fsl_espi {
97 struct device *dev;
98 void __iomem *reg_base;
99
100 const void *tx;
101 void *rx;
102
103 unsigned int rx_len;
104 unsigned int tx_len;
105 unsigned int rxskip;
106
107 u8 *local_buf;
108 spinlock_t lock;
109
110 u32 spibrg; /* SPIBRG input clock */
111
112 struct completion done;
113};
114
Heiner Kallweit219b5e32016-11-13 14:38:05 +0100115struct fsl_espi_cs {
116 u32 hw_mode;
117};
118
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100119static inline u32 fsl_espi_read_reg(struct fsl_espi *espi, int offset)
Heiner Kallweit46afd382016-09-13 23:16:02 +0200120{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100121 return ioread32be(espi->reg_base + offset);
Heiner Kallweit46afd382016-09-13 23:16:02 +0200122}
123
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100124static inline u8 fsl_espi_read_reg8(struct fsl_espi *espi, int offset)
Heiner Kallweit46afd382016-09-13 23:16:02 +0200125{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100126 return ioread8(espi->reg_base + offset);
Heiner Kallweit46afd382016-09-13 23:16:02 +0200127}
128
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100129static inline void fsl_espi_write_reg(struct fsl_espi *espi, int offset,
Heiner Kallweit46afd382016-09-13 23:16:02 +0200130 u32 val)
131{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100132 iowrite32be(val, espi->reg_base + offset);
Heiner Kallweit46afd382016-09-13 23:16:02 +0200133}
134
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100135static inline void fsl_espi_write_reg8(struct fsl_espi *espi, int offset,
Heiner Kallweit46afd382016-09-13 23:16:02 +0200136 u8 val)
137{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100138 iowrite8(val, espi->reg_base + offset);
Heiner Kallweit46afd382016-09-13 23:16:02 +0200139}
140
Heiner Kallweit923ab152016-10-02 14:22:57 +0200141static void fsl_espi_memcpy_swab(void *to, const void *from,
142 struct spi_message *m,
143 struct spi_transfer *t)
144{
145 unsigned int len = t->len;
146
147 if (!(m->spi->mode & SPI_LSB_FIRST) || t->bits_per_word <= 8) {
148 memcpy(to, from, len);
149 return;
150 }
151
152 /* In case of LSB-first and bits_per_word > 8 byte-swap all words */
153 while (len)
154 if (len >= 4) {
155 *(u32 *)to = swahb32p(from);
156 to += 4;
157 from += 4;
158 len -= 4;
159 } else {
160 *(u16 *)to = swab16p(from);
161 to += 2;
162 from += 2;
163 len -= 2;
164 }
165}
166
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200167static void fsl_espi_copy_to_buf(struct spi_message *m,
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100168 struct fsl_espi *espi)
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200169{
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200170 struct spi_transfer *t;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100171 u8 *buf = espi->local_buf;
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200172
173 list_for_each_entry(t, &m->transfers, transfer_list) {
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200174 if (t->tx_buf)
Heiner Kallweit923ab152016-10-02 14:22:57 +0200175 fsl_espi_memcpy_swab(buf, t->tx_buf, m, t);
Heiner Kallweitaca75152016-11-09 22:58:01 +0100176 /* In RXSKIP mode controller shifts out zeros internally */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100177 else if (!espi->rxskip)
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200178 memset(buf, 0, t->len);
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200179 buf += t->len;
180 }
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200181}
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200182
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200183static void fsl_espi_copy_from_buf(struct spi_message *m,
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100184 struct fsl_espi *espi)
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200185{
186 struct spi_transfer *t;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100187 u8 *buf = espi->local_buf;
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200188
189 list_for_each_entry(t, &m->transfers, transfer_list) {
190 if (t->rx_buf)
Heiner Kallweit923ab152016-10-02 14:22:57 +0200191 fsl_espi_memcpy_swab(t->rx_buf, buf, m, t);
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200192 buf += t->len;
193 }
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200194}
195
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200196static int fsl_espi_check_message(struct spi_message *m)
197{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100198 struct fsl_espi *espi = spi_master_get_devdata(m->spi->master);
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200199 struct spi_transfer *t, *first;
200
201 if (m->frame_length > SPCOM_TRANLEN_MAX) {
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100202 dev_err(espi->dev, "message too long, size is %u bytes\n",
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200203 m->frame_length);
204 return -EMSGSIZE;
205 }
206
207 first = list_first_entry(&m->transfers, struct spi_transfer,
208 transfer_list);
Heiner Kallweite4be7052016-10-02 14:22:35 +0200209
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200210 list_for_each_entry(t, &m->transfers, transfer_list) {
211 if (first->bits_per_word != t->bits_per_word ||
212 first->speed_hz != t->speed_hz) {
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100213 dev_err(espi->dev, "bits_per_word/speed_hz should be the same for all transfers\n");
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200214 return -EINVAL;
215 }
216 }
217
Heiner Kallweite4be7052016-10-02 14:22:35 +0200218 /* ESPI supports MSB-first transfers for word size 8 / 16 only */
219 if (!(m->spi->mode & SPI_LSB_FIRST) && first->bits_per_word != 8 &&
220 first->bits_per_word != 16) {
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100221 dev_err(espi->dev,
Heiner Kallweite4be7052016-10-02 14:22:35 +0200222 "MSB-first transfer not supported for wordsize %u\n",
223 first->bits_per_word);
224 return -EINVAL;
225 }
226
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200227 return 0;
228}
229
Heiner Kallweitaca75152016-11-09 22:58:01 +0100230static unsigned int fsl_espi_check_rxskip_mode(struct spi_message *m)
231{
232 struct spi_transfer *t;
233 unsigned int i = 0, rxskip = 0;
234
235 /*
236 * prerequisites for ESPI rxskip mode:
237 * - message has two transfers
238 * - first transfer is a write and second is a read
239 *
240 * In addition the current low-level transfer mechanism requires
241 * that the rxskip bytes fit into the TX FIFO. Else the transfer
242 * would hang because after the first FSL_ESPI_FIFO_SIZE bytes
243 * the TX FIFO isn't re-filled.
244 */
245 list_for_each_entry(t, &m->transfers, transfer_list) {
246 if (i == 0) {
247 if (!t->tx_buf || t->rx_buf ||
248 t->len > FSL_ESPI_FIFO_SIZE)
249 return 0;
250 rxskip = t->len;
251 } else if (i == 1) {
252 if (t->tx_buf || !t->rx_buf)
253 return 0;
254 }
255 i++;
256 }
257
258 return i == 2 ? rxskip : 0;
259}
260
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100261static void fsl_espi_fill_tx_fifo(struct fsl_espi *espi, u32 events)
Heiner Kallweit54731262016-10-27 21:25:58 +0200262{
263 u32 tx_fifo_avail;
264
265 /* if events is zero transfer has not started and tx fifo is empty */
266 tx_fifo_avail = events ? SPIE_TXCNT(events) : FSL_ESPI_FIFO_SIZE;
267
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100268 while (tx_fifo_avail >= min(4U, espi->tx_len) && espi->tx_len)
269 if (espi->tx_len >= 4) {
270 fsl_espi_write_reg(espi, ESPI_SPITF, *(u32 *)espi->tx);
271 espi->tx += 4;
272 espi->tx_len -= 4;
Heiner Kallweit54731262016-10-27 21:25:58 +0200273 tx_fifo_avail -= 4;
274 } else {
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100275 fsl_espi_write_reg8(espi, ESPI_SPITF, *(u8 *)espi->tx);
276 espi->tx += 1;
277 espi->tx_len -= 1;
Heiner Kallweit54731262016-10-27 21:25:58 +0200278 tx_fifo_avail -= 1;
279 }
280}
281
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100282static void fsl_espi_read_rx_fifo(struct fsl_espi *espi, u32 events)
Heiner Kallweitf05689a2016-10-27 21:27:35 +0200283{
284 u32 rx_fifo_avail = SPIE_RXCNT(events);
285
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100286 while (rx_fifo_avail >= min(4U, espi->rx_len) && espi->rx_len)
287 if (espi->rx_len >= 4) {
288 *(u32 *)espi->rx = fsl_espi_read_reg(espi, ESPI_SPIRF);
289 espi->rx += 4;
290 espi->rx_len -= 4;
Heiner Kallweitf05689a2016-10-27 21:27:35 +0200291 rx_fifo_avail -= 4;
292 } else {
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100293 *(u8 *)espi->rx = fsl_espi_read_reg8(espi, ESPI_SPIRF);
294 espi->rx += 1;
295 espi->rx_len -= 1;
Heiner Kallweitf05689a2016-10-27 21:27:35 +0200296 rx_fifo_avail -= 1;
297 }
298}
299
Heiner Kallweitea616ee2016-08-25 06:44:42 +0200300static void fsl_espi_setup_transfer(struct spi_device *spi,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800301 struct spi_transfer *t)
302{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100303 struct fsl_espi *espi = spi_master_get_devdata(spi->master);
Heiner Kallweitd198ebf2016-09-13 23:15:45 +0200304 int bits_per_word = t ? t->bits_per_word : spi->bits_per_word;
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200305 u32 pm, hz = t ? t->speed_hz : spi->max_speed_hz;
Heiner Kallweit219b5e32016-11-13 14:38:05 +0100306 struct fsl_espi_cs *cs = spi_get_ctldata(spi);
Heiner Kallweit8f3086d2016-11-04 21:01:12 +0100307 u32 hw_mode_old = cs->hw_mode;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800308
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800309 /* mask out bits we are going to set */
310 cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF));
311
Heiner Kallweita755af52016-09-04 09:56:57 +0200312 cs->hw_mode |= CSMODE_LEN(bits_per_word - 1);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800313
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100314 pm = DIV_ROUND_UP(espi->spibrg, hz * 4) - 1;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800315
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200316 if (pm > 15) {
317 cs->hw_mode |= CSMODE_DIV16;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100318 pm = DIV_ROUND_UP(espi->spibrg, hz * 16 * 4) - 1;
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200319
320 WARN_ONCE(pm > 15,
321 "%s: Requested speed is too low: %u Hz. Will use %u Hz instead.\n",
322 dev_name(&spi->dev), hz,
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100323 espi->spibrg / (4 * 16 * (15 + 1)));
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200324 if (pm > 15)
325 pm = 15;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800326 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800327
328 cs->hw_mode |= CSMODE_PM(pm);
329
Heiner Kallweit8f3086d2016-11-04 21:01:12 +0100330 /* don't write the mode register if the mode doesn't change */
331 if (cs->hw_mode != hw_mode_old)
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100332 fsl_espi_write_reg(espi, ESPI_SPMODEx(spi->chip_select),
Heiner Kallweit8f3086d2016-11-04 21:01:12 +0100333 cs->hw_mode);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800334}
335
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800336static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
337{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100338 struct fsl_espi *espi = spi_master_get_devdata(spi->master);
Heiner Kallweitaca75152016-11-09 22:58:01 +0100339 u32 mask, spcom;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800340 int ret;
341
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100342 espi->rx_len = t->len;
343 espi->tx_len = t->len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800344
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100345 espi->tx = t->tx_buf;
346 espi->rx = t->rx_buf;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800347
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100348 reinit_completion(&espi->done);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800349
350 /* Set SPCOM[CS] and SPCOM[TRANLEN] field */
Heiner Kallweitaca75152016-11-09 22:58:01 +0100351 spcom = SPCOM_CS(spi->chip_select);
352 spcom |= SPCOM_TRANLEN(t->len - 1);
353
354 /* configure RXSKIP mode */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100355 if (espi->rxskip) {
356 spcom |= SPCOM_RXSKIP(espi->rxskip);
357 espi->tx_len = espi->rxskip;
358 espi->rx_len = t->len - espi->rxskip;
359 espi->rx = t->rx_buf + espi->rxskip;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100360 if (t->rx_nbits == SPI_NBITS_DUAL)
361 spcom |= SPCOM_DO;
Heiner Kallweitaca75152016-11-09 22:58:01 +0100362 }
363
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100364 fsl_espi_write_reg(espi, ESPI_SPCOM, spcom);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800365
Heiner Kallweite508cea2016-10-27 21:27:56 +0200366 /* enable interrupts */
367 mask = SPIM_DON;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100368 if (espi->rx_len > FSL_ESPI_FIFO_SIZE)
Heiner Kallweite508cea2016-10-27 21:27:56 +0200369 mask |= SPIM_RXT;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100370 fsl_espi_write_reg(espi, ESPI_SPIM, mask);
Heiner Kallweit5bcc6a22016-09-07 22:53:01 +0200371
Heiner Kallweit54731262016-10-27 21:25:58 +0200372 /* Prevent filling the fifo from getting interrupted */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100373 spin_lock_irq(&espi->lock);
374 fsl_espi_fill_tx_fifo(espi, 0);
375 spin_unlock_irq(&espi->lock);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800376
Nobuteru Hayashiaa70e562016-03-18 11:35:21 +0000377 /* Won't hang up forever, SPI bus sometimes got lost interrupts... */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100378 ret = wait_for_completion_timeout(&espi->done, 2 * HZ);
Nobuteru Hayashiaa70e562016-03-18 11:35:21 +0000379 if (ret == 0)
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100380 dev_err(espi->dev,
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200381 "Transaction hanging up (left %u tx bytes, %u rx bytes)\n",
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100382 espi->tx_len, espi->rx_len);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800383
384 /* disable rx ints */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100385 fsl_espi_write_reg(espi, ESPI_SPIM, 0);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800386
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200387 return ret == 0 ? -ETIMEDOUT : 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800388}
389
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200390static int fsl_espi_trans(struct spi_message *m, struct spi_transfer *trans)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800391{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100392 struct fsl_espi *espi = spi_master_get_devdata(m->spi->master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800393 struct spi_device *spi = m->spi;
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200394 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800395
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100396 espi->rxskip = fsl_espi_check_rxskip_mode(m);
397 if (trans->rx_nbits == SPI_NBITS_DUAL && !espi->rxskip) {
398 dev_err(espi->dev, "Dual output mode requires RXSKIP mode!\n");
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100399 return -EINVAL;
400 }
401
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100402 fsl_espi_copy_to_buf(m, espi);
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200403 fsl_espi_setup_transfer(spi, trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800404
Heiner Kallweit06af1152016-09-07 22:54:35 +0200405 ret = fsl_espi_bufs(spi, trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800406
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200407 if (trans->delay_usecs)
408 udelay(trans->delay_usecs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800409
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200410 if (!ret)
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100411 fsl_espi_copy_from_buf(m, espi);
Heiner Kallweite33a3ad2016-09-07 22:51:10 +0200412
413 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800414}
415
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100416static int fsl_espi_do_one_msg(struct spi_master *master,
417 struct spi_message *m)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800418{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100419 struct fsl_espi *espi = spi_master_get_devdata(m->spi->master);
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100420 unsigned int delay_usecs = 0, rx_nbits = 0;
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200421 struct spi_transfer *t, trans = {};
Heiner Kallweite33a3ad2016-09-07 22:51:10 +0200422 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800423
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200424 ret = fsl_espi_check_message(m);
425 if (ret)
426 goto out;
427
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800428 list_for_each_entry(t, &m->transfers, transfer_list) {
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200429 if (t->delay_usecs > delay_usecs)
430 delay_usecs = t->delay_usecs;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100431 if (t->rx_nbits > rx_nbits)
432 rx_nbits = t->rx_nbits;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800433 }
434
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200435 t = list_first_entry(&m->transfers, struct spi_transfer,
436 transfer_list);
437
Heiner Kallweit06af1152016-09-07 22:54:35 +0200438 trans.len = m->frame_length;
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200439 trans.speed_hz = t->speed_hz;
440 trans.bits_per_word = t->bits_per_word;
441 trans.delay_usecs = delay_usecs;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100442 trans.tx_buf = espi->local_buf;
443 trans.rx_buf = espi->local_buf;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100444 trans.rx_nbits = rx_nbits;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800445
Heiner Kallweit06af1152016-09-07 22:54:35 +0200446 if (trans.len)
447 ret = fsl_espi_trans(m, &trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800448
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200449 m->actual_length = ret ? 0 : trans.len;
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200450out:
Heiner Kallweit0319d492016-09-07 22:51:29 +0200451 if (m->status == -EINPROGRESS)
452 m->status = ret;
453
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100454 spi_finalize_current_message(master);
Heiner Kallweit0319d492016-09-07 22:51:29 +0200455
456 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800457}
458
459static int fsl_espi_setup(struct spi_device *spi)
460{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100461 struct fsl_espi *espi;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800462 u32 loop_mode;
Heiner Kallweit219b5e32016-11-13 14:38:05 +0100463 struct fsl_espi_cs *cs = spi_get_ctldata(spi);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800464
465 if (!spi->max_speed_hz)
466 return -EINVAL;
467
468 if (!cs) {
Axel Lind9f26742014-08-31 12:44:09 +0800469 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800470 if (!cs)
471 return -ENOMEM;
Axel Lind9f26742014-08-31 12:44:09 +0800472 spi_set_ctldata(spi, cs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800473 }
474
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100475 espi = spi_master_get_devdata(spi->master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800476
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100477 pm_runtime_get_sync(espi->dev);
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200478
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100479 cs->hw_mode = fsl_espi_read_reg(espi, ESPI_SPMODEx(spi->chip_select));
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800480 /* mask out bits we are going to set */
481 cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH
482 | CSMODE_REV);
483
484 if (spi->mode & SPI_CPHA)
485 cs->hw_mode |= CSMODE_CP_BEGIN_EDGECLK;
486 if (spi->mode & SPI_CPOL)
487 cs->hw_mode |= CSMODE_CI_INACTIVEHIGH;
488 if (!(spi->mode & SPI_LSB_FIRST))
489 cs->hw_mode |= CSMODE_REV;
490
491 /* Handle the loop mode */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100492 loop_mode = fsl_espi_read_reg(espi, ESPI_SPMODE);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800493 loop_mode &= ~SPMODE_LOOP;
494 if (spi->mode & SPI_LOOP)
495 loop_mode |= SPMODE_LOOP;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100496 fsl_espi_write_reg(espi, ESPI_SPMODE, loop_mode);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800497
Heiner Kallweitea616ee2016-08-25 06:44:42 +0200498 fsl_espi_setup_transfer(spi, NULL);
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200499
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100500 pm_runtime_mark_last_busy(espi->dev);
501 pm_runtime_put_autosuspend(espi->dev);
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200502
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800503 return 0;
504}
505
Axel Lind9f26742014-08-31 12:44:09 +0800506static void fsl_espi_cleanup(struct spi_device *spi)
507{
Heiner Kallweit219b5e32016-11-13 14:38:05 +0100508 struct fsl_espi_cs *cs = spi_get_ctldata(spi);
Axel Lind9f26742014-08-31 12:44:09 +0800509
510 kfree(cs);
511 spi_set_ctldata(spi, NULL);
512}
513
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100514static void fsl_espi_cpu_irq(struct fsl_espi *espi, u32 events)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800515{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100516 if (espi->rx_len)
517 fsl_espi_read_rx_fifo(espi, events);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800518
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100519 if (espi->tx_len)
520 fsl_espi_fill_tx_fifo(espi, events);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800521
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100522 if (espi->tx_len || espi->rx_len)
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200523 return;
524
525 /* we're done, but check for errors before returning */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100526 events = fsl_espi_read_reg(espi, ESPI_SPIE);
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200527
528 if (!(events & SPIE_DON))
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100529 dev_err(espi->dev,
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200530 "Transfer done but SPIE_DON isn't set!\n");
531
532 if (SPIE_RXCNT(events) || SPIE_TXCNT(events) != FSL_ESPI_FIFO_SIZE)
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100533 dev_err(espi->dev, "Transfer done but rx/tx fifo's aren't empty!\n");
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200534
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100535 complete(&espi->done);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800536}
537
538static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
539{
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100540 struct fsl_espi *espi = context_data;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800541 u32 events;
542
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100543 spin_lock(&espi->lock);
Heiner Kallweit54731262016-10-27 21:25:58 +0200544
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800545 /* Get interrupt events(tx/rx) */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100546 events = fsl_espi_read_reg(espi, ESPI_SPIE);
Heiner Kallweit54731262016-10-27 21:25:58 +0200547 if (!events) {
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100548 spin_unlock(&espi->lock);
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200549 return IRQ_NONE;
Heiner Kallweit54731262016-10-27 21:25:58 +0200550 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800551
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100552 dev_vdbg(espi->dev, "%s: events %x\n", __func__, events);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800553
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100554 fsl_espi_cpu_irq(espi, events);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800555
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200556 /* Clear the events */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100557 fsl_espi_write_reg(espi, ESPI_SPIE, events);
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200558
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100559 spin_unlock(&espi->lock);
Heiner Kallweit54731262016-10-27 21:25:58 +0200560
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200561 return IRQ_HANDLED;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800562}
563
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200564#ifdef CONFIG_PM
565static int fsl_espi_runtime_suspend(struct device *dev)
Heiner Kallweit75506d02014-12-03 07:56:19 +0100566{
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200567 struct spi_master *master = dev_get_drvdata(dev);
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100568 struct fsl_espi *espi = spi_master_get_devdata(master);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100569 u32 regval;
570
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100571 regval = fsl_espi_read_reg(espi, ESPI_SPMODE);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100572 regval &= ~SPMODE_ENABLE;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100573 fsl_espi_write_reg(espi, ESPI_SPMODE, regval);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100574
575 return 0;
576}
577
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200578static int fsl_espi_runtime_resume(struct device *dev)
Heiner Kallweit75506d02014-12-03 07:56:19 +0100579{
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200580 struct spi_master *master = dev_get_drvdata(dev);
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100581 struct fsl_espi *espi = spi_master_get_devdata(master);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100582 u32 regval;
583
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100584 regval = fsl_espi_read_reg(espi, ESPI_SPMODE);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100585 regval |= SPMODE_ENABLE;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100586 fsl_espi_write_reg(espi, ESPI_SPMODE, regval);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100587
588 return 0;
589}
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200590#endif
Heiner Kallweit75506d02014-12-03 07:56:19 +0100591
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200592static size_t fsl_espi_max_message_size(struct spi_device *spi)
Michal Suchanekb541eef2015-12-02 10:38:21 +0000593{
594 return SPCOM_TRANLEN_MAX;
595}
596
Heiner Kallweit456c7422016-11-13 14:40:18 +0100597static void fsl_espi_init_regs(struct device *dev, bool initial)
598{
599 struct spi_master *master = dev_get_drvdata(dev);
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100600 struct fsl_espi *espi = spi_master_get_devdata(master);
Heiner Kallweit456c7422016-11-13 14:40:18 +0100601 struct device_node *nc;
602 u32 csmode, cs, prop;
603 int ret;
604
605 /* SPI controller initializations */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100606 fsl_espi_write_reg(espi, ESPI_SPMODE, 0);
607 fsl_espi_write_reg(espi, ESPI_SPIM, 0);
608 fsl_espi_write_reg(espi, ESPI_SPCOM, 0);
609 fsl_espi_write_reg(espi, ESPI_SPIE, 0xffffffff);
Heiner Kallweit456c7422016-11-13 14:40:18 +0100610
611 /* Init eSPI CS mode register */
612 for_each_available_child_of_node(master->dev.of_node, nc) {
613 /* get chip select */
614 ret = of_property_read_u32(nc, "reg", &cs);
615 if (ret || cs >= master->num_chipselect)
616 continue;
617
618 csmode = CSMODE_INIT_VAL;
619
620 /* check if CSBEF is set in device tree */
621 ret = of_property_read_u32(nc, "fsl,csbef", &prop);
622 if (!ret) {
623 csmode &= ~(CSMODE_BEF(0xf));
624 csmode |= CSMODE_BEF(prop);
625 }
626
627 /* check if CSAFT is set in device tree */
628 ret = of_property_read_u32(nc, "fsl,csaft", &prop);
629 if (!ret) {
630 csmode &= ~(CSMODE_AFT(0xf));
631 csmode |= CSMODE_AFT(prop);
632 }
633
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100634 fsl_espi_write_reg(espi, ESPI_SPMODEx(cs), csmode);
Heiner Kallweit456c7422016-11-13 14:40:18 +0100635
636 if (initial)
637 dev_info(dev, "cs=%u, init_csmode=0x%x\n", cs, csmode);
638 }
639
640 /* Enable SPI interface */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100641 fsl_espi_write_reg(espi, ESPI_SPMODE, SPMODE_INIT_VAL | SPMODE_ENABLE);
Heiner Kallweit456c7422016-11-13 14:40:18 +0100642}
643
Heiner Kallweit604042a2016-09-17 15:43:31 +0200644static int fsl_espi_probe(struct device *dev, struct resource *mem,
Heiner Kallweit74543462016-11-13 14:36:39 +0100645 unsigned int irq, unsigned int num_cs)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800646{
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800647 struct spi_master *master;
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100648 struct fsl_espi *espi;
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200649 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800650
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100651 master = spi_alloc_master(dev, sizeof(struct fsl_espi));
Heiner Kallweit604042a2016-09-17 15:43:31 +0200652 if (!master)
653 return -ENOMEM;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800654
655 dev_set_drvdata(dev, master);
656
Heiner Kallweit7cb55572016-11-13 14:37:41 +0100657 master->mode_bits = SPI_RX_DUAL | SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
658 SPI_LSB_FIRST | SPI_LOOP;
659 master->dev.of_node = dev->of_node;
Stephen Warren24778be2013-05-21 20:36:35 -0600660 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800661 master->setup = fsl_espi_setup;
Axel Lind9f26742014-08-31 12:44:09 +0800662 master->cleanup = fsl_espi_cleanup;
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100663 master->transfer_one_message = fsl_espi_do_one_msg;
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200664 master->auto_runtime_pm = true;
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200665 master->max_message_size = fsl_espi_max_message_size;
Heiner Kallweit74543462016-11-13 14:36:39 +0100666 master->num_chipselect = num_cs;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800667
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100668 espi = spi_master_get_devdata(master);
669 spin_lock_init(&espi->lock);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800670
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100671 espi->dev = dev;
672 espi->spibrg = fsl_get_sys_freq();
673 if (espi->spibrg == -1) {
Heiner Kallweit7cb55572016-11-13 14:37:41 +0100674 dev_err(dev, "Can't get sys frequency!\n");
675 ret = -EINVAL;
676 goto err_probe;
677 }
678
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100679 init_completion(&espi->done);
Heiner Kallweit7cb55572016-11-13 14:37:41 +0100680
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100681 espi->local_buf = devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL);
682 if (!espi->local_buf) {
Heiner Kallweit14238772016-09-07 22:50:22 +0200683 ret = -ENOMEM;
684 goto err_probe;
685 }
686
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100687 espi->reg_base = devm_ioremap_resource(dev, mem);
688 if (IS_ERR(espi->reg_base)) {
689 ret = PTR_ERR(espi->reg_base);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800690 goto err_probe;
691 }
692
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800693 /* Register for SPI Interrupt */
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100694 ret = devm_request_irq(dev, irq, fsl_espi_irq, 0, "fsl_espi", espi);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800695 if (ret)
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200696 goto err_probe;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800697
Heiner Kallweit456c7422016-11-13 14:40:18 +0100698 fsl_espi_init_regs(dev, true);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800699
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200700 pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
701 pm_runtime_use_autosuspend(dev);
702 pm_runtime_set_active(dev);
703 pm_runtime_enable(dev);
704 pm_runtime_get_sync(dev);
705
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200706 ret = devm_spi_register_master(dev, master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800707 if (ret < 0)
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200708 goto err_pm;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800709
Heiner Kallweit35ab0462016-11-13 14:40:51 +0100710 dev_info(dev, "at 0x%p (irq = %u)\n", espi->reg_base, irq);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800711
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200712 pm_runtime_mark_last_busy(dev);
713 pm_runtime_put_autosuspend(dev);
714
Heiner Kallweit604042a2016-09-17 15:43:31 +0200715 return 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800716
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200717err_pm:
718 pm_runtime_put_noidle(dev);
719 pm_runtime_disable(dev);
720 pm_runtime_set_suspended(dev);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800721err_probe:
722 spi_master_put(master);
Heiner Kallweit604042a2016-09-17 15:43:31 +0200723 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800724}
725
726static int of_fsl_espi_get_chipselects(struct device *dev)
727{
728 struct device_node *np = dev->of_node;
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200729 u32 num_cs;
730 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800731
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200732 ret = of_property_read_u32(np, "fsl,espi-num-chipselects", &num_cs);
733 if (ret) {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800734 dev_err(dev, "No 'fsl,espi-num-chipselects' property\n");
Heiner Kallweit74543462016-11-13 14:36:39 +0100735 return 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800736 }
737
Heiner Kallweit74543462016-11-13 14:36:39 +0100738 return num_cs;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800739}
740
Grant Likelyfd4a3192012-12-07 16:57:14 +0000741static int of_fsl_espi_probe(struct platform_device *ofdev)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800742{
743 struct device *dev = &ofdev->dev;
744 struct device_node *np = ofdev->dev.of_node;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800745 struct resource mem;
Heiner Kallweit74543462016-11-13 14:36:39 +0100746 unsigned int irq, num_cs;
Heiner Kallweitacf69212016-09-17 15:43:00 +0200747 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800748
Heiner Kallweite3ce4f42016-11-13 14:37:18 +0100749 if (of_property_read_bool(np, "mode")) {
750 dev_err(dev, "mode property is not supported on ESPI!\n");
751 return -EINVAL;
752 }
753
Heiner Kallweit74543462016-11-13 14:36:39 +0100754 num_cs = of_fsl_espi_get_chipselects(dev);
755 if (!num_cs)
756 return -EINVAL;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800757
758 ret = of_address_to_resource(np, 0, &mem);
759 if (ret)
Heiner Kallweitacf69212016-09-17 15:43:00 +0200760 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800761
Thierry Redingf7578492013-09-18 15:24:44 +0200762 irq = irq_of_parse_and_map(np, 0);
Heiner Kallweitacf69212016-09-17 15:43:00 +0200763 if (!irq)
764 return -EINVAL;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800765
Heiner Kallweit74543462016-11-13 14:36:39 +0100766 return fsl_espi_probe(dev, &mem, irq, num_cs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800767}
768
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200769static int of_fsl_espi_remove(struct platform_device *dev)
770{
771 pm_runtime_disable(&dev->dev);
772
773 return 0;
774}
775
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800776#ifdef CONFIG_PM_SLEEP
777static int of_fsl_espi_suspend(struct device *dev)
778{
779 struct spi_master *master = dev_get_drvdata(dev);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800780 int ret;
781
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800782 ret = spi_master_suspend(master);
783 if (ret) {
784 dev_warn(dev, "cannot suspend master\n");
785 return ret;
786 }
787
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200788 ret = pm_runtime_force_suspend(dev);
789 if (ret < 0)
790 return ret;
791
792 return 0;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800793}
794
795static int of_fsl_espi_resume(struct device *dev)
796{
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800797 struct spi_master *master = dev_get_drvdata(dev);
Heiner Kallweit456c7422016-11-13 14:40:18 +0100798 int ret;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800799
Heiner Kallweit456c7422016-11-13 14:40:18 +0100800 fsl_espi_init_regs(dev, false);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800801
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200802 ret = pm_runtime_force_resume(dev);
803 if (ret < 0)
804 return ret;
805
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800806 return spi_master_resume(master);
807}
808#endif /* CONFIG_PM_SLEEP */
809
810static const struct dev_pm_ops espi_pm = {
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200811 SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend,
812 fsl_espi_runtime_resume, NULL)
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800813 SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume)
814};
815
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800816static const struct of_device_id of_fsl_espi_match[] = {
817 { .compatible = "fsl,mpc8536-espi" },
818 {}
819};
820MODULE_DEVICE_TABLE(of, of_fsl_espi_match);
821
Grant Likely18d306d2011-02-22 21:02:43 -0700822static struct platform_driver fsl_espi_driver = {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800823 .driver = {
824 .name = "fsl_espi",
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800825 .of_match_table = of_fsl_espi_match,
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800826 .pm = &espi_pm,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800827 },
828 .probe = of_fsl_espi_probe,
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200829 .remove = of_fsl_espi_remove,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800830};
Grant Likely940ab882011-10-05 11:29:49 -0600831module_platform_driver(fsl_espi_driver);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800832
833MODULE_AUTHOR("Mingkai Hu");
834MODULE_DESCRIPTION("Enhanced Freescale SPI Driver");
835MODULE_LICENSE("GPL");