blob: 5d84694b73230c5218024aa7dcba2cf8f9f38a02 [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 Kallweit219b5e32016-11-13 14:38:05 +010096struct fsl_espi_cs {
97 u32 hw_mode;
98};
99
Heiner Kallweit46afd382016-09-13 23:16:02 +0200100static inline u32 fsl_espi_read_reg(struct mpc8xxx_spi *mspi, int offset)
101{
102 return ioread32be(mspi->reg_base + offset);
103}
104
105static inline u8 fsl_espi_read_reg8(struct mpc8xxx_spi *mspi, int offset)
106{
107 return ioread8(mspi->reg_base + offset);
108}
109
110static inline void fsl_espi_write_reg(struct mpc8xxx_spi *mspi, int offset,
111 u32 val)
112{
113 iowrite32be(val, mspi->reg_base + offset);
114}
115
116static inline void fsl_espi_write_reg8(struct mpc8xxx_spi *mspi, int offset,
117 u8 val)
118{
119 iowrite8(val, mspi->reg_base + offset);
120}
121
Heiner Kallweit923ab152016-10-02 14:22:57 +0200122static void fsl_espi_memcpy_swab(void *to, const void *from,
123 struct spi_message *m,
124 struct spi_transfer *t)
125{
126 unsigned int len = t->len;
127
128 if (!(m->spi->mode & SPI_LSB_FIRST) || t->bits_per_word <= 8) {
129 memcpy(to, from, len);
130 return;
131 }
132
133 /* In case of LSB-first and bits_per_word > 8 byte-swap all words */
134 while (len)
135 if (len >= 4) {
136 *(u32 *)to = swahb32p(from);
137 to += 4;
138 from += 4;
139 len -= 4;
140 } else {
141 *(u16 *)to = swab16p(from);
142 to += 2;
143 from += 2;
144 len -= 2;
145 }
146}
147
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200148static void fsl_espi_copy_to_buf(struct spi_message *m,
149 struct mpc8xxx_spi *mspi)
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200150{
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200151 struct spi_transfer *t;
152 u8 *buf = mspi->local_buf;
153
154 list_for_each_entry(t, &m->transfers, transfer_list) {
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200155 if (t->tx_buf)
Heiner Kallweit923ab152016-10-02 14:22:57 +0200156 fsl_espi_memcpy_swab(buf, t->tx_buf, m, t);
Heiner Kallweitaca75152016-11-09 22:58:01 +0100157 /* In RXSKIP mode controller shifts out zeros internally */
158 else if (!mspi->rxskip)
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200159 memset(buf, 0, t->len);
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200160 buf += t->len;
161 }
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200162}
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200163
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200164static void fsl_espi_copy_from_buf(struct spi_message *m,
165 struct mpc8xxx_spi *mspi)
166{
167 struct spi_transfer *t;
168 u8 *buf = mspi->local_buf;
169
170 list_for_each_entry(t, &m->transfers, transfer_list) {
171 if (t->rx_buf)
Heiner Kallweit923ab152016-10-02 14:22:57 +0200172 fsl_espi_memcpy_swab(t->rx_buf, buf, m, t);
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200173 buf += t->len;
174 }
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200175}
176
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200177static int fsl_espi_check_message(struct spi_message *m)
178{
179 struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
180 struct spi_transfer *t, *first;
181
182 if (m->frame_length > SPCOM_TRANLEN_MAX) {
183 dev_err(mspi->dev, "message too long, size is %u bytes\n",
184 m->frame_length);
185 return -EMSGSIZE;
186 }
187
188 first = list_first_entry(&m->transfers, struct spi_transfer,
189 transfer_list);
Heiner Kallweite4be7052016-10-02 14:22:35 +0200190
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200191 list_for_each_entry(t, &m->transfers, transfer_list) {
192 if (first->bits_per_word != t->bits_per_word ||
193 first->speed_hz != t->speed_hz) {
194 dev_err(mspi->dev, "bits_per_word/speed_hz should be the same for all transfers\n");
195 return -EINVAL;
196 }
197 }
198
Heiner Kallweite4be7052016-10-02 14:22:35 +0200199 /* ESPI supports MSB-first transfers for word size 8 / 16 only */
200 if (!(m->spi->mode & SPI_LSB_FIRST) && first->bits_per_word != 8 &&
201 first->bits_per_word != 16) {
202 dev_err(mspi->dev,
203 "MSB-first transfer not supported for wordsize %u\n",
204 first->bits_per_word);
205 return -EINVAL;
206 }
207
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200208 return 0;
209}
210
Heiner Kallweitaca75152016-11-09 22:58:01 +0100211static unsigned int fsl_espi_check_rxskip_mode(struct spi_message *m)
212{
213 struct spi_transfer *t;
214 unsigned int i = 0, rxskip = 0;
215
216 /*
217 * prerequisites for ESPI rxskip mode:
218 * - message has two transfers
219 * - first transfer is a write and second is a read
220 *
221 * In addition the current low-level transfer mechanism requires
222 * that the rxskip bytes fit into the TX FIFO. Else the transfer
223 * would hang because after the first FSL_ESPI_FIFO_SIZE bytes
224 * the TX FIFO isn't re-filled.
225 */
226 list_for_each_entry(t, &m->transfers, transfer_list) {
227 if (i == 0) {
228 if (!t->tx_buf || t->rx_buf ||
229 t->len > FSL_ESPI_FIFO_SIZE)
230 return 0;
231 rxskip = t->len;
232 } else if (i == 1) {
233 if (t->tx_buf || !t->rx_buf)
234 return 0;
235 }
236 i++;
237 }
238
239 return i == 2 ? rxskip : 0;
240}
241
Heiner Kallweit54731262016-10-27 21:25:58 +0200242static void fsl_espi_fill_tx_fifo(struct mpc8xxx_spi *mspi, u32 events)
243{
244 u32 tx_fifo_avail;
245
246 /* if events is zero transfer has not started and tx fifo is empty */
247 tx_fifo_avail = events ? SPIE_TXCNT(events) : FSL_ESPI_FIFO_SIZE;
248
249 while (tx_fifo_avail >= min(4U, mspi->tx_len) && mspi->tx_len)
250 if (mspi->tx_len >= 4) {
251 fsl_espi_write_reg(mspi, ESPI_SPITF, *(u32 *)mspi->tx);
252 mspi->tx += 4;
253 mspi->tx_len -= 4;
254 tx_fifo_avail -= 4;
255 } else {
256 fsl_espi_write_reg8(mspi, ESPI_SPITF, *(u8 *)mspi->tx);
257 mspi->tx += 1;
258 mspi->tx_len -= 1;
259 tx_fifo_avail -= 1;
260 }
261}
262
Heiner Kallweitf05689a2016-10-27 21:27:35 +0200263static void fsl_espi_read_rx_fifo(struct mpc8xxx_spi *mspi, u32 events)
264{
265 u32 rx_fifo_avail = SPIE_RXCNT(events);
266
267 while (rx_fifo_avail >= min(4U, mspi->rx_len) && mspi->rx_len)
268 if (mspi->rx_len >= 4) {
269 *(u32 *)mspi->rx = fsl_espi_read_reg(mspi, ESPI_SPIRF);
270 mspi->rx += 4;
271 mspi->rx_len -= 4;
272 rx_fifo_avail -= 4;
273 } else {
274 *(u8 *)mspi->rx = fsl_espi_read_reg8(mspi, ESPI_SPIRF);
275 mspi->rx += 1;
276 mspi->rx_len -= 1;
277 rx_fifo_avail -= 1;
278 }
279}
280
Heiner Kallweitea616ee2016-08-25 06:44:42 +0200281static void fsl_espi_setup_transfer(struct spi_device *spi,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800282 struct spi_transfer *t)
283{
284 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Heiner Kallweitd198ebf2016-09-13 23:15:45 +0200285 int bits_per_word = t ? t->bits_per_word : spi->bits_per_word;
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200286 u32 pm, hz = t ? t->speed_hz : spi->max_speed_hz;
Heiner Kallweit219b5e32016-11-13 14:38:05 +0100287 struct fsl_espi_cs *cs = spi_get_ctldata(spi);
Heiner Kallweit8f3086d2016-11-04 21:01:12 +0100288 u32 hw_mode_old = cs->hw_mode;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800289
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800290 /* mask out bits we are going to set */
291 cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF));
292
Heiner Kallweita755af52016-09-04 09:56:57 +0200293 cs->hw_mode |= CSMODE_LEN(bits_per_word - 1);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800294
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200295 pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 4) - 1;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800296
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200297 if (pm > 15) {
298 cs->hw_mode |= CSMODE_DIV16;
299 pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 16 * 4) - 1;
300
301 WARN_ONCE(pm > 15,
302 "%s: Requested speed is too low: %u Hz. Will use %u Hz instead.\n",
303 dev_name(&spi->dev), hz,
304 mpc8xxx_spi->spibrg / (4 * 16 * (15 + 1)));
305 if (pm > 15)
306 pm = 15;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800307 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800308
309 cs->hw_mode |= CSMODE_PM(pm);
310
Heiner Kallweit8f3086d2016-11-04 21:01:12 +0100311 /* don't write the mode register if the mode doesn't change */
312 if (cs->hw_mode != hw_mode_old)
313 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(spi->chip_select),
314 cs->hw_mode);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800315}
316
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800317static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
318{
319 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Heiner Kallweitaca75152016-11-09 22:58:01 +0100320 u32 mask, spcom;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800321 int ret;
322
Heiner Kallweitf895e272016-10-27 21:26:08 +0200323 mpc8xxx_spi->rx_len = t->len;
Heiner Kallweit54731262016-10-27 21:25:58 +0200324 mpc8xxx_spi->tx_len = t->len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800325
326 mpc8xxx_spi->tx = t->tx_buf;
327 mpc8xxx_spi->rx = t->rx_buf;
328
Wolfram Sang16735d02013-11-14 14:32:02 -0800329 reinit_completion(&mpc8xxx_spi->done);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800330
331 /* Set SPCOM[CS] and SPCOM[TRANLEN] field */
Heiner Kallweitaca75152016-11-09 22:58:01 +0100332 spcom = SPCOM_CS(spi->chip_select);
333 spcom |= SPCOM_TRANLEN(t->len - 1);
334
335 /* configure RXSKIP mode */
336 if (mpc8xxx_spi->rxskip) {
337 spcom |= SPCOM_RXSKIP(mpc8xxx_spi->rxskip);
338 mpc8xxx_spi->tx_len = mpc8xxx_spi->rxskip;
339 mpc8xxx_spi->rx_len = t->len - mpc8xxx_spi->rxskip;
340 mpc8xxx_spi->rx = t->rx_buf + mpc8xxx_spi->rxskip;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100341 if (t->rx_nbits == SPI_NBITS_DUAL)
342 spcom |= SPCOM_DO;
Heiner Kallweitaca75152016-11-09 22:58:01 +0100343 }
344
345 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, spcom);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800346
Heiner Kallweite508cea2016-10-27 21:27:56 +0200347 /* enable interrupts */
348 mask = SPIM_DON;
349 if (mpc8xxx_spi->rx_len > FSL_ESPI_FIFO_SIZE)
350 mask |= SPIM_RXT;
351 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, mask);
Heiner Kallweit5bcc6a22016-09-07 22:53:01 +0200352
Heiner Kallweit54731262016-10-27 21:25:58 +0200353 /* Prevent filling the fifo from getting interrupted */
354 spin_lock_irq(&mpc8xxx_spi->lock);
355 fsl_espi_fill_tx_fifo(mpc8xxx_spi, 0);
356 spin_unlock_irq(&mpc8xxx_spi->lock);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800357
Nobuteru Hayashiaa70e562016-03-18 11:35:21 +0000358 /* Won't hang up forever, SPI bus sometimes got lost interrupts... */
359 ret = wait_for_completion_timeout(&mpc8xxx_spi->done, 2 * HZ);
360 if (ret == 0)
361 dev_err(mpc8xxx_spi->dev,
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200362 "Transaction hanging up (left %u tx bytes, %u rx bytes)\n",
363 mpc8xxx_spi->tx_len, mpc8xxx_spi->rx_len);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800364
365 /* disable rx ints */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200366 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800367
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200368 return ret == 0 ? -ETIMEDOUT : 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800369}
370
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200371static int fsl_espi_trans(struct spi_message *m, struct spi_transfer *trans)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800372{
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200373 struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800374 struct spi_device *spi = m->spi;
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200375 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800376
Heiner Kallweitaca75152016-11-09 22:58:01 +0100377 mspi->rxskip = fsl_espi_check_rxskip_mode(m);
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100378 if (trans->rx_nbits == SPI_NBITS_DUAL && !mspi->rxskip) {
379 dev_err(mspi->dev, "Dual output mode requires RXSKIP mode!\n");
380 return -EINVAL;
381 }
382
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200383 fsl_espi_copy_to_buf(m, mspi);
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200384 fsl_espi_setup_transfer(spi, trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800385
Heiner Kallweit06af1152016-09-07 22:54:35 +0200386 ret = fsl_espi_bufs(spi, trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800387
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200388 if (trans->delay_usecs)
389 udelay(trans->delay_usecs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800390
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200391 if (!ret)
392 fsl_espi_copy_from_buf(m, mspi);
Heiner Kallweite33a3ad2016-09-07 22:51:10 +0200393
394 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800395}
396
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100397static int fsl_espi_do_one_msg(struct spi_master *master,
398 struct spi_message *m)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800399{
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200400 struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100401 unsigned int delay_usecs = 0, rx_nbits = 0;
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200402 struct spi_transfer *t, trans = {};
Heiner Kallweite33a3ad2016-09-07 22:51:10 +0200403 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800404
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200405 ret = fsl_espi_check_message(m);
406 if (ret)
407 goto out;
408
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800409 list_for_each_entry(t, &m->transfers, transfer_list) {
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200410 if (t->delay_usecs > delay_usecs)
411 delay_usecs = t->delay_usecs;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100412 if (t->rx_nbits > rx_nbits)
413 rx_nbits = t->rx_nbits;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800414 }
415
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200416 t = list_first_entry(&m->transfers, struct spi_transfer,
417 transfer_list);
418
Heiner Kallweit06af1152016-09-07 22:54:35 +0200419 trans.len = m->frame_length;
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200420 trans.speed_hz = t->speed_hz;
421 trans.bits_per_word = t->bits_per_word;
422 trans.delay_usecs = delay_usecs;
423 trans.tx_buf = mspi->local_buf;
424 trans.rx_buf = mspi->local_buf;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100425 trans.rx_nbits = rx_nbits;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800426
Heiner Kallweit06af1152016-09-07 22:54:35 +0200427 if (trans.len)
428 ret = fsl_espi_trans(m, &trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800429
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200430 m->actual_length = ret ? 0 : trans.len;
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200431out:
Heiner Kallweit0319d492016-09-07 22:51:29 +0200432 if (m->status == -EINPROGRESS)
433 m->status = ret;
434
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100435 spi_finalize_current_message(master);
Heiner Kallweit0319d492016-09-07 22:51:29 +0200436
437 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800438}
439
440static int fsl_espi_setup(struct spi_device *spi)
441{
442 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800443 u32 loop_mode;
Heiner Kallweit219b5e32016-11-13 14:38:05 +0100444 struct fsl_espi_cs *cs = spi_get_ctldata(spi);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800445
446 if (!spi->max_speed_hz)
447 return -EINVAL;
448
449 if (!cs) {
Axel Lind9f26742014-08-31 12:44:09 +0800450 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800451 if (!cs)
452 return -ENOMEM;
Axel Lind9f26742014-08-31 12:44:09 +0800453 spi_set_ctldata(spi, cs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800454 }
455
456 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800457
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200458 pm_runtime_get_sync(mpc8xxx_spi->dev);
459
Heiner Kallweit46afd382016-09-13 23:16:02 +0200460 cs->hw_mode = fsl_espi_read_reg(mpc8xxx_spi,
461 ESPI_SPMODEx(spi->chip_select));
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800462 /* mask out bits we are going to set */
463 cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH
464 | CSMODE_REV);
465
466 if (spi->mode & SPI_CPHA)
467 cs->hw_mode |= CSMODE_CP_BEGIN_EDGECLK;
468 if (spi->mode & SPI_CPOL)
469 cs->hw_mode |= CSMODE_CI_INACTIVEHIGH;
470 if (!(spi->mode & SPI_LSB_FIRST))
471 cs->hw_mode |= CSMODE_REV;
472
473 /* Handle the loop mode */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200474 loop_mode = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800475 loop_mode &= ~SPMODE_LOOP;
476 if (spi->mode & SPI_LOOP)
477 loop_mode |= SPMODE_LOOP;
Heiner Kallweit46afd382016-09-13 23:16:02 +0200478 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, loop_mode);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800479
Heiner Kallweitea616ee2016-08-25 06:44:42 +0200480 fsl_espi_setup_transfer(spi, NULL);
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200481
482 pm_runtime_mark_last_busy(mpc8xxx_spi->dev);
483 pm_runtime_put_autosuspend(mpc8xxx_spi->dev);
484
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800485 return 0;
486}
487
Axel Lind9f26742014-08-31 12:44:09 +0800488static void fsl_espi_cleanup(struct spi_device *spi)
489{
Heiner Kallweit219b5e32016-11-13 14:38:05 +0100490 struct fsl_espi_cs *cs = spi_get_ctldata(spi);
Axel Lind9f26742014-08-31 12:44:09 +0800491
492 kfree(cs);
493 spi_set_ctldata(spi, NULL);
494}
495
Heiner Kallweit10ed1e62016-08-25 06:45:16 +0200496static void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800497{
Heiner Kallweitf05689a2016-10-27 21:27:35 +0200498 if (mspi->rx_len)
499 fsl_espi_read_rx_fifo(mspi, events);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800500
Heiner Kallweit54731262016-10-27 21:25:58 +0200501 if (mspi->tx_len)
502 fsl_espi_fill_tx_fifo(mspi, events);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800503
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200504 if (mspi->tx_len || mspi->rx_len)
505 return;
506
507 /* we're done, but check for errors before returning */
508 events = fsl_espi_read_reg(mspi, ESPI_SPIE);
509
510 if (!(events & SPIE_DON))
511 dev_err(mspi->dev,
512 "Transfer done but SPIE_DON isn't set!\n");
513
514 if (SPIE_RXCNT(events) || SPIE_TXCNT(events) != FSL_ESPI_FIFO_SIZE)
515 dev_err(mspi->dev, "Transfer done but rx/tx fifo's aren't empty!\n");
516
517 complete(&mspi->done);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800518}
519
520static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
521{
522 struct mpc8xxx_spi *mspi = context_data;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800523 u32 events;
524
Heiner Kallweit54731262016-10-27 21:25:58 +0200525 spin_lock(&mspi->lock);
526
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800527 /* Get interrupt events(tx/rx) */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200528 events = fsl_espi_read_reg(mspi, ESPI_SPIE);
Heiner Kallweit54731262016-10-27 21:25:58 +0200529 if (!events) {
Heiner Kallweit66b80532016-10-29 10:53:19 +0200530 spin_unlock(&mspi->lock);
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200531 return IRQ_NONE;
Heiner Kallweit54731262016-10-27 21:25:58 +0200532 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800533
534 dev_vdbg(mspi->dev, "%s: events %x\n", __func__, events);
535
536 fsl_espi_cpu_irq(mspi, events);
537
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200538 /* Clear the events */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200539 fsl_espi_write_reg(mspi, ESPI_SPIE, events);
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200540
Heiner Kallweit54731262016-10-27 21:25:58 +0200541 spin_unlock(&mspi->lock);
542
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200543 return IRQ_HANDLED;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800544}
545
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200546#ifdef CONFIG_PM
547static int fsl_espi_runtime_suspend(struct device *dev)
Heiner Kallweit75506d02014-12-03 07:56:19 +0100548{
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200549 struct spi_master *master = dev_get_drvdata(dev);
550 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100551 u32 regval;
552
Heiner Kallweit46afd382016-09-13 23:16:02 +0200553 regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100554 regval &= ~SPMODE_ENABLE;
Heiner Kallweit46afd382016-09-13 23:16:02 +0200555 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100556
557 return 0;
558}
559
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200560static int fsl_espi_runtime_resume(struct device *dev)
Heiner Kallweit75506d02014-12-03 07:56:19 +0100561{
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200562 struct spi_master *master = dev_get_drvdata(dev);
563 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100564 u32 regval;
565
Heiner Kallweit46afd382016-09-13 23:16:02 +0200566 regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100567 regval |= SPMODE_ENABLE;
Heiner Kallweit46afd382016-09-13 23:16:02 +0200568 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100569
570 return 0;
571}
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200572#endif
Heiner Kallweit75506d02014-12-03 07:56:19 +0100573
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200574static size_t fsl_espi_max_message_size(struct spi_device *spi)
Michal Suchanekb541eef2015-12-02 10:38:21 +0000575{
576 return SPCOM_TRANLEN_MAX;
577}
578
Heiner Kallweit604042a2016-09-17 15:43:31 +0200579static int fsl_espi_probe(struct device *dev, struct resource *mem,
Heiner Kallweit74543462016-11-13 14:36:39 +0100580 unsigned int irq, unsigned int num_cs)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800581{
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800582 struct spi_master *master;
583 struct mpc8xxx_spi *mpc8xxx_spi;
Jane Wand0fb47a52014-04-16 13:09:39 -0700584 struct device_node *nc;
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200585 u32 regval, csmode, cs, prop;
586 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800587
588 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
Heiner Kallweit604042a2016-09-17 15:43:31 +0200589 if (!master)
590 return -ENOMEM;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800591
592 dev_set_drvdata(dev, master);
593
Heiner Kallweit7cb55572016-11-13 14:37:41 +0100594 master->mode_bits = SPI_RX_DUAL | SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
595 SPI_LSB_FIRST | SPI_LOOP;
596 master->dev.of_node = dev->of_node;
Stephen Warren24778be2013-05-21 20:36:35 -0600597 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800598 master->setup = fsl_espi_setup;
Axel Lind9f26742014-08-31 12:44:09 +0800599 master->cleanup = fsl_espi_cleanup;
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100600 master->transfer_one_message = fsl_espi_do_one_msg;
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200601 master->auto_runtime_pm = true;
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200602 master->max_message_size = fsl_espi_max_message_size;
Heiner Kallweit74543462016-11-13 14:36:39 +0100603 master->num_chipselect = num_cs;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800604
605 mpc8xxx_spi = spi_master_get_devdata(master);
Heiner Kallweit54731262016-10-27 21:25:58 +0200606 spin_lock_init(&mpc8xxx_spi->lock);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800607
Heiner Kallweit7cb55572016-11-13 14:37:41 +0100608 mpc8xxx_spi->dev = dev;
609 mpc8xxx_spi->spibrg = fsl_get_sys_freq();
610 if (mpc8xxx_spi->spibrg == -1) {
611 dev_err(dev, "Can't get sys frequency!\n");
612 ret = -EINVAL;
613 goto err_probe;
614 }
615
616 init_completion(&mpc8xxx_spi->done);
617
Heiner Kallweit14238772016-09-07 22:50:22 +0200618 mpc8xxx_spi->local_buf =
619 devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL);
620 if (!mpc8xxx_spi->local_buf) {
621 ret = -ENOMEM;
622 goto err_probe;
623 }
624
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200625 mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
Axel Lin37c5db72015-08-30 18:35:51 +0800626 if (IS_ERR(mpc8xxx_spi->reg_base)) {
627 ret = PTR_ERR(mpc8xxx_spi->reg_base);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800628 goto err_probe;
629 }
630
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800631 /* Register for SPI Interrupt */
Heiner Kallweitcdb2f772016-11-13 14:36:47 +0100632 ret = devm_request_irq(dev, irq, fsl_espi_irq, 0, "fsl_espi",
633 mpc8xxx_spi);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800634 if (ret)
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200635 goto err_probe;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800636
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800637 /* SPI controller initializations */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200638 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, 0);
639 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
640 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, 0);
641 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIE, 0xffffffff);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800642
643 /* Init eSPI CS mode register */
Jane Wand0fb47a52014-04-16 13:09:39 -0700644 for_each_available_child_of_node(master->dev.of_node, nc) {
645 /* get chip select */
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200646 ret = of_property_read_u32(nc, "reg", &cs);
Heiner Kallweit74543462016-11-13 14:36:39 +0100647 if (ret || cs >= num_cs)
Jane Wand0fb47a52014-04-16 13:09:39 -0700648 continue;
649
650 csmode = CSMODE_INIT_VAL;
Jane Wand0fb47a52014-04-16 13:09:39 -0700651
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200652 /* check if CSBEF is set in device tree */
653 ret = of_property_read_u32(nc, "fsl,csbef", &prop);
654 if (!ret) {
655 csmode &= ~(CSMODE_BEF(0xf));
656 csmode |= CSMODE_BEF(prop);
657 }
658
659 /* check if CSAFT is set in device tree */
660 ret = of_property_read_u32(nc, "fsl,csaft", &prop);
661 if (!ret) {
662 csmode &= ~(CSMODE_AFT(0xf));
663 csmode |= CSMODE_AFT(prop);
664 }
665
666 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(cs), csmode);
667
668 dev_info(dev, "cs=%u, init_csmode=0x%x\n", cs, csmode);
Jane Wand0fb47a52014-04-16 13:09:39 -0700669 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800670
671 /* Enable SPI interface */
Heiner Kallweit689d41f2016-11-13 14:36:01 +0100672 regval = SPMODE_INIT_VAL | SPMODE_ENABLE;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800673
Heiner Kallweit46afd382016-09-13 23:16:02 +0200674 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800675
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200676 pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
677 pm_runtime_use_autosuspend(dev);
678 pm_runtime_set_active(dev);
679 pm_runtime_enable(dev);
680 pm_runtime_get_sync(dev);
681
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200682 ret = devm_spi_register_master(dev, master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800683 if (ret < 0)
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200684 goto err_pm;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800685
Heiner Kallweitcdb2f772016-11-13 14:36:47 +0100686 dev_info(dev, "at 0x%p (irq = %u)\n", mpc8xxx_spi->reg_base, irq);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800687
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200688 pm_runtime_mark_last_busy(dev);
689 pm_runtime_put_autosuspend(dev);
690
Heiner Kallweit604042a2016-09-17 15:43:31 +0200691 return 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800692
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200693err_pm:
694 pm_runtime_put_noidle(dev);
695 pm_runtime_disable(dev);
696 pm_runtime_set_suspended(dev);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800697err_probe:
698 spi_master_put(master);
Heiner Kallweit604042a2016-09-17 15:43:31 +0200699 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800700}
701
702static int of_fsl_espi_get_chipselects(struct device *dev)
703{
704 struct device_node *np = dev->of_node;
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200705 u32 num_cs;
706 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800707
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200708 ret = of_property_read_u32(np, "fsl,espi-num-chipselects", &num_cs);
709 if (ret) {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800710 dev_err(dev, "No 'fsl,espi-num-chipselects' property\n");
Heiner Kallweit74543462016-11-13 14:36:39 +0100711 return 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800712 }
713
Heiner Kallweit74543462016-11-13 14:36:39 +0100714 return num_cs;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800715}
716
Grant Likelyfd4a3192012-12-07 16:57:14 +0000717static int of_fsl_espi_probe(struct platform_device *ofdev)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800718{
719 struct device *dev = &ofdev->dev;
720 struct device_node *np = ofdev->dev.of_node;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800721 struct resource mem;
Heiner Kallweit74543462016-11-13 14:36:39 +0100722 unsigned int irq, num_cs;
Heiner Kallweitacf69212016-09-17 15:43:00 +0200723 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800724
Heiner Kallweite3ce4f42016-11-13 14:37:18 +0100725 if (of_property_read_bool(np, "mode")) {
726 dev_err(dev, "mode property is not supported on ESPI!\n");
727 return -EINVAL;
728 }
729
Heiner Kallweit74543462016-11-13 14:36:39 +0100730 num_cs = of_fsl_espi_get_chipselects(dev);
731 if (!num_cs)
732 return -EINVAL;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800733
734 ret = of_address_to_resource(np, 0, &mem);
735 if (ret)
Heiner Kallweitacf69212016-09-17 15:43:00 +0200736 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800737
Thierry Redingf7578492013-09-18 15:24:44 +0200738 irq = irq_of_parse_and_map(np, 0);
Heiner Kallweitacf69212016-09-17 15:43:00 +0200739 if (!irq)
740 return -EINVAL;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800741
Heiner Kallweit74543462016-11-13 14:36:39 +0100742 return fsl_espi_probe(dev, &mem, irq, num_cs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800743}
744
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200745static int of_fsl_espi_remove(struct platform_device *dev)
746{
747 pm_runtime_disable(&dev->dev);
748
749 return 0;
750}
751
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800752#ifdef CONFIG_PM_SLEEP
753static int of_fsl_espi_suspend(struct device *dev)
754{
755 struct spi_master *master = dev_get_drvdata(dev);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800756 int ret;
757
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800758 ret = spi_master_suspend(master);
759 if (ret) {
760 dev_warn(dev, "cannot suspend master\n");
761 return ret;
762 }
763
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200764 ret = pm_runtime_force_suspend(dev);
765 if (ret < 0)
766 return ret;
767
768 return 0;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800769}
770
771static int of_fsl_espi_resume(struct device *dev)
772{
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800773 struct spi_master *master = dev_get_drvdata(dev);
774 struct mpc8xxx_spi *mpc8xxx_spi;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800775 u32 regval;
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200776 int i, ret;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800777
778 mpc8xxx_spi = spi_master_get_devdata(master);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800779
780 /* SPI controller initializations */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200781 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, 0);
782 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
783 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, 0);
784 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIE, 0xffffffff);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800785
786 /* Init eSPI CS mode register */
Heiner Kallweit74543462016-11-13 14:36:39 +0100787 for (i = 0; i < master->num_chipselect; i++)
Heiner Kallweit46afd382016-09-13 23:16:02 +0200788 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(i),
789 CSMODE_INIT_VAL);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800790
791 /* Enable SPI interface */
Heiner Kallweit689d41f2016-11-13 14:36:01 +0100792 regval = SPMODE_INIT_VAL | SPMODE_ENABLE;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800793
Heiner Kallweit46afd382016-09-13 23:16:02 +0200794 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800795
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200796 ret = pm_runtime_force_resume(dev);
797 if (ret < 0)
798 return ret;
799
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800800 return spi_master_resume(master);
801}
802#endif /* CONFIG_PM_SLEEP */
803
804static const struct dev_pm_ops espi_pm = {
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200805 SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend,
806 fsl_espi_runtime_resume, NULL)
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800807 SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume)
808};
809
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800810static const struct of_device_id of_fsl_espi_match[] = {
811 { .compatible = "fsl,mpc8536-espi" },
812 {}
813};
814MODULE_DEVICE_TABLE(of, of_fsl_espi_match);
815
Grant Likely18d306d2011-02-22 21:02:43 -0700816static struct platform_driver fsl_espi_driver = {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800817 .driver = {
818 .name = "fsl_espi",
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800819 .of_match_table = of_fsl_espi_match,
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800820 .pm = &espi_pm,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800821 },
822 .probe = of_fsl_espi_probe,
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200823 .remove = of_fsl_espi_remove,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800824};
Grant Likely940ab882011-10-05 11:29:49 -0600825module_platform_driver(fsl_espi_driver);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800826
827MODULE_AUTHOR("Mingkai Hu");
828MODULE_DESCRIPTION("Enhanced Freescale SPI Driver");
829MODULE_LICENSE("GPL");