blob: 38d3a9393e23ede22eb1aeaf26bc2d063665917f [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 Kallweit46afd382016-09-13 23:16:02 +020096static inline u32 fsl_espi_read_reg(struct mpc8xxx_spi *mspi, int offset)
97{
98 return ioread32be(mspi->reg_base + offset);
99}
100
101static inline u8 fsl_espi_read_reg8(struct mpc8xxx_spi *mspi, int offset)
102{
103 return ioread8(mspi->reg_base + offset);
104}
105
106static inline void fsl_espi_write_reg(struct mpc8xxx_spi *mspi, int offset,
107 u32 val)
108{
109 iowrite32be(val, mspi->reg_base + offset);
110}
111
112static inline void fsl_espi_write_reg8(struct mpc8xxx_spi *mspi, int offset,
113 u8 val)
114{
115 iowrite8(val, mspi->reg_base + offset);
116}
117
Heiner Kallweit923ab152016-10-02 14:22:57 +0200118static void fsl_espi_memcpy_swab(void *to, const void *from,
119 struct spi_message *m,
120 struct spi_transfer *t)
121{
122 unsigned int len = t->len;
123
124 if (!(m->spi->mode & SPI_LSB_FIRST) || t->bits_per_word <= 8) {
125 memcpy(to, from, len);
126 return;
127 }
128
129 /* In case of LSB-first and bits_per_word > 8 byte-swap all words */
130 while (len)
131 if (len >= 4) {
132 *(u32 *)to = swahb32p(from);
133 to += 4;
134 from += 4;
135 len -= 4;
136 } else {
137 *(u16 *)to = swab16p(from);
138 to += 2;
139 from += 2;
140 len -= 2;
141 }
142}
143
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200144static void fsl_espi_copy_to_buf(struct spi_message *m,
145 struct mpc8xxx_spi *mspi)
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200146{
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200147 struct spi_transfer *t;
148 u8 *buf = mspi->local_buf;
149
150 list_for_each_entry(t, &m->transfers, transfer_list) {
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200151 if (t->tx_buf)
Heiner Kallweit923ab152016-10-02 14:22:57 +0200152 fsl_espi_memcpy_swab(buf, t->tx_buf, m, t);
Heiner Kallweitaca75152016-11-09 22:58:01 +0100153 /* In RXSKIP mode controller shifts out zeros internally */
154 else if (!mspi->rxskip)
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200155 memset(buf, 0, t->len);
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200156 buf += t->len;
157 }
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200158}
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200159
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200160static void fsl_espi_copy_from_buf(struct spi_message *m,
161 struct mpc8xxx_spi *mspi)
162{
163 struct spi_transfer *t;
164 u8 *buf = mspi->local_buf;
165
166 list_for_each_entry(t, &m->transfers, transfer_list) {
167 if (t->rx_buf)
Heiner Kallweit923ab152016-10-02 14:22:57 +0200168 fsl_espi_memcpy_swab(t->rx_buf, buf, m, t);
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200169 buf += t->len;
170 }
Heiner Kallweit7c159aa2016-09-07 22:50:53 +0200171}
172
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200173static int fsl_espi_check_message(struct spi_message *m)
174{
175 struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
176 struct spi_transfer *t, *first;
177
178 if (m->frame_length > SPCOM_TRANLEN_MAX) {
179 dev_err(mspi->dev, "message too long, size is %u bytes\n",
180 m->frame_length);
181 return -EMSGSIZE;
182 }
183
184 first = list_first_entry(&m->transfers, struct spi_transfer,
185 transfer_list);
Heiner Kallweite4be7052016-10-02 14:22:35 +0200186
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200187 list_for_each_entry(t, &m->transfers, transfer_list) {
188 if (first->bits_per_word != t->bits_per_word ||
189 first->speed_hz != t->speed_hz) {
190 dev_err(mspi->dev, "bits_per_word/speed_hz should be the same for all transfers\n");
191 return -EINVAL;
192 }
193 }
194
Heiner Kallweite4be7052016-10-02 14:22:35 +0200195 /* ESPI supports MSB-first transfers for word size 8 / 16 only */
196 if (!(m->spi->mode & SPI_LSB_FIRST) && first->bits_per_word != 8 &&
197 first->bits_per_word != 16) {
198 dev_err(mspi->dev,
199 "MSB-first transfer not supported for wordsize %u\n",
200 first->bits_per_word);
201 return -EINVAL;
202 }
203
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200204 return 0;
205}
206
Heiner Kallweitaca75152016-11-09 22:58:01 +0100207static unsigned int fsl_espi_check_rxskip_mode(struct spi_message *m)
208{
209 struct spi_transfer *t;
210 unsigned int i = 0, rxskip = 0;
211
212 /*
213 * prerequisites for ESPI rxskip mode:
214 * - message has two transfers
215 * - first transfer is a write and second is a read
216 *
217 * In addition the current low-level transfer mechanism requires
218 * that the rxskip bytes fit into the TX FIFO. Else the transfer
219 * would hang because after the first FSL_ESPI_FIFO_SIZE bytes
220 * the TX FIFO isn't re-filled.
221 */
222 list_for_each_entry(t, &m->transfers, transfer_list) {
223 if (i == 0) {
224 if (!t->tx_buf || t->rx_buf ||
225 t->len > FSL_ESPI_FIFO_SIZE)
226 return 0;
227 rxskip = t->len;
228 } else if (i == 1) {
229 if (t->tx_buf || !t->rx_buf)
230 return 0;
231 }
232 i++;
233 }
234
235 return i == 2 ? rxskip : 0;
236}
237
Heiner Kallweit54731262016-10-27 21:25:58 +0200238static void fsl_espi_fill_tx_fifo(struct mpc8xxx_spi *mspi, u32 events)
239{
240 u32 tx_fifo_avail;
241
242 /* if events is zero transfer has not started and tx fifo is empty */
243 tx_fifo_avail = events ? SPIE_TXCNT(events) : FSL_ESPI_FIFO_SIZE;
244
245 while (tx_fifo_avail >= min(4U, mspi->tx_len) && mspi->tx_len)
246 if (mspi->tx_len >= 4) {
247 fsl_espi_write_reg(mspi, ESPI_SPITF, *(u32 *)mspi->tx);
248 mspi->tx += 4;
249 mspi->tx_len -= 4;
250 tx_fifo_avail -= 4;
251 } else {
252 fsl_espi_write_reg8(mspi, ESPI_SPITF, *(u8 *)mspi->tx);
253 mspi->tx += 1;
254 mspi->tx_len -= 1;
255 tx_fifo_avail -= 1;
256 }
257}
258
Heiner Kallweitf05689a2016-10-27 21:27:35 +0200259static void fsl_espi_read_rx_fifo(struct mpc8xxx_spi *mspi, u32 events)
260{
261 u32 rx_fifo_avail = SPIE_RXCNT(events);
262
263 while (rx_fifo_avail >= min(4U, mspi->rx_len) && mspi->rx_len)
264 if (mspi->rx_len >= 4) {
265 *(u32 *)mspi->rx = fsl_espi_read_reg(mspi, ESPI_SPIRF);
266 mspi->rx += 4;
267 mspi->rx_len -= 4;
268 rx_fifo_avail -= 4;
269 } else {
270 *(u8 *)mspi->rx = fsl_espi_read_reg8(mspi, ESPI_SPIRF);
271 mspi->rx += 1;
272 mspi->rx_len -= 1;
273 rx_fifo_avail -= 1;
274 }
275}
276
Heiner Kallweitea616ee2016-08-25 06:44:42 +0200277static void fsl_espi_setup_transfer(struct spi_device *spi,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800278 struct spi_transfer *t)
279{
280 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Heiner Kallweitd198ebf2016-09-13 23:15:45 +0200281 int bits_per_word = t ? t->bits_per_word : spi->bits_per_word;
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200282 u32 pm, hz = t ? t->speed_hz : spi->max_speed_hz;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800283 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Heiner Kallweit8f3086d2016-11-04 21:01:12 +0100284 u32 hw_mode_old = cs->hw_mode;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800285
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800286 /* mask out bits we are going to set */
287 cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF));
288
Heiner Kallweita755af52016-09-04 09:56:57 +0200289 cs->hw_mode |= CSMODE_LEN(bits_per_word - 1);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800290
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200291 pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 4) - 1;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800292
Paulo Zaneti73aaf152016-10-29 11:02:19 +0200293 if (pm > 15) {
294 cs->hw_mode |= CSMODE_DIV16;
295 pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 16 * 4) - 1;
296
297 WARN_ONCE(pm > 15,
298 "%s: Requested speed is too low: %u Hz. Will use %u Hz instead.\n",
299 dev_name(&spi->dev), hz,
300 mpc8xxx_spi->spibrg / (4 * 16 * (15 + 1)));
301 if (pm > 15)
302 pm = 15;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800303 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800304
305 cs->hw_mode |= CSMODE_PM(pm);
306
Heiner Kallweit8f3086d2016-11-04 21:01:12 +0100307 /* don't write the mode register if the mode doesn't change */
308 if (cs->hw_mode != hw_mode_old)
309 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(spi->chip_select),
310 cs->hw_mode);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800311}
312
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800313static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
314{
315 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Heiner Kallweitaca75152016-11-09 22:58:01 +0100316 u32 mask, spcom;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800317 int ret;
318
Heiner Kallweitf895e272016-10-27 21:26:08 +0200319 mpc8xxx_spi->rx_len = t->len;
Heiner Kallweit54731262016-10-27 21:25:58 +0200320 mpc8xxx_spi->tx_len = t->len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800321
322 mpc8xxx_spi->tx = t->tx_buf;
323 mpc8xxx_spi->rx = t->rx_buf;
324
Wolfram Sang16735d02013-11-14 14:32:02 -0800325 reinit_completion(&mpc8xxx_spi->done);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800326
327 /* Set SPCOM[CS] and SPCOM[TRANLEN] field */
Heiner Kallweitaca75152016-11-09 22:58:01 +0100328 spcom = SPCOM_CS(spi->chip_select);
329 spcom |= SPCOM_TRANLEN(t->len - 1);
330
331 /* configure RXSKIP mode */
332 if (mpc8xxx_spi->rxskip) {
333 spcom |= SPCOM_RXSKIP(mpc8xxx_spi->rxskip);
334 mpc8xxx_spi->tx_len = mpc8xxx_spi->rxskip;
335 mpc8xxx_spi->rx_len = t->len - mpc8xxx_spi->rxskip;
336 mpc8xxx_spi->rx = t->rx_buf + mpc8xxx_spi->rxskip;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100337 if (t->rx_nbits == SPI_NBITS_DUAL)
338 spcom |= SPCOM_DO;
Heiner Kallweitaca75152016-11-09 22:58:01 +0100339 }
340
341 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, spcom);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800342
Heiner Kallweite508cea2016-10-27 21:27:56 +0200343 /* enable interrupts */
344 mask = SPIM_DON;
345 if (mpc8xxx_spi->rx_len > FSL_ESPI_FIFO_SIZE)
346 mask |= SPIM_RXT;
347 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, mask);
Heiner Kallweit5bcc6a22016-09-07 22:53:01 +0200348
Heiner Kallweit54731262016-10-27 21:25:58 +0200349 /* Prevent filling the fifo from getting interrupted */
350 spin_lock_irq(&mpc8xxx_spi->lock);
351 fsl_espi_fill_tx_fifo(mpc8xxx_spi, 0);
352 spin_unlock_irq(&mpc8xxx_spi->lock);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800353
Nobuteru Hayashiaa70e562016-03-18 11:35:21 +0000354 /* Won't hang up forever, SPI bus sometimes got lost interrupts... */
355 ret = wait_for_completion_timeout(&mpc8xxx_spi->done, 2 * HZ);
356 if (ret == 0)
357 dev_err(mpc8xxx_spi->dev,
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200358 "Transaction hanging up (left %u tx bytes, %u rx bytes)\n",
359 mpc8xxx_spi->tx_len, mpc8xxx_spi->rx_len);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800360
361 /* disable rx ints */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200362 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800363
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200364 return ret == 0 ? -ETIMEDOUT : 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800365}
366
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200367static int fsl_espi_trans(struct spi_message *m, struct spi_transfer *trans)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800368{
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200369 struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800370 struct spi_device *spi = m->spi;
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200371 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800372
Heiner Kallweitaca75152016-11-09 22:58:01 +0100373 mspi->rxskip = fsl_espi_check_rxskip_mode(m);
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100374 if (trans->rx_nbits == SPI_NBITS_DUAL && !mspi->rxskip) {
375 dev_err(mspi->dev, "Dual output mode requires RXSKIP mode!\n");
376 return -EINVAL;
377 }
378
Heiner Kallweit38d003f2016-09-07 22:54:51 +0200379 fsl_espi_copy_to_buf(m, mspi);
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200380 fsl_espi_setup_transfer(spi, trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800381
Heiner Kallweit06af1152016-09-07 22:54:35 +0200382 ret = fsl_espi_bufs(spi, trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800383
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200384 if (trans->delay_usecs)
385 udelay(trans->delay_usecs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800386
Heiner Kallweitcce7e3a2016-09-07 22:54:18 +0200387 if (!ret)
388 fsl_espi_copy_from_buf(m, mspi);
Heiner Kallweite33a3ad2016-09-07 22:51:10 +0200389
390 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800391}
392
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100393static int fsl_espi_do_one_msg(struct spi_master *master,
394 struct spi_message *m)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800395{
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200396 struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100397 unsigned int delay_usecs = 0, rx_nbits = 0;
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200398 struct spi_transfer *t, trans = {};
Heiner Kallweite33a3ad2016-09-07 22:51:10 +0200399 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800400
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200401 ret = fsl_espi_check_message(m);
402 if (ret)
403 goto out;
404
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800405 list_for_each_entry(t, &m->transfers, transfer_list) {
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200406 if (t->delay_usecs > delay_usecs)
407 delay_usecs = t->delay_usecs;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100408 if (t->rx_nbits > rx_nbits)
409 rx_nbits = t->rx_nbits;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800410 }
411
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200412 t = list_first_entry(&m->transfers, struct spi_transfer,
413 transfer_list);
414
Heiner Kallweit06af1152016-09-07 22:54:35 +0200415 trans.len = m->frame_length;
Heiner Kallweit96361faf2016-09-07 22:54:00 +0200416 trans.speed_hz = t->speed_hz;
417 trans.bits_per_word = t->bits_per_word;
418 trans.delay_usecs = delay_usecs;
419 trans.tx_buf = mspi->local_buf;
420 trans.rx_buf = mspi->local_buf;
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100421 trans.rx_nbits = rx_nbits;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800422
Heiner Kallweit06af1152016-09-07 22:54:35 +0200423 if (trans.len)
424 ret = fsl_espi_trans(m, &trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800425
Heiner Kallweitfaceef32016-09-07 22:52:06 +0200426 m->actual_length = ret ? 0 : trans.len;
Heiner Kallweitd3152cf12016-09-07 22:53:38 +0200427out:
Heiner Kallweit0319d492016-09-07 22:51:29 +0200428 if (m->status == -EINPROGRESS)
429 m->status = ret;
430
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100431 spi_finalize_current_message(master);
Heiner Kallweit0319d492016-09-07 22:51:29 +0200432
433 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800434}
435
436static int fsl_espi_setup(struct spi_device *spi)
437{
438 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800439 u32 loop_mode;
Axel Lind9f26742014-08-31 12:44:09 +0800440 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800441
442 if (!spi->max_speed_hz)
443 return -EINVAL;
444
445 if (!cs) {
Axel Lind9f26742014-08-31 12:44:09 +0800446 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800447 if (!cs)
448 return -ENOMEM;
Axel Lind9f26742014-08-31 12:44:09 +0800449 spi_set_ctldata(spi, cs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800450 }
451
452 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800453
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200454 pm_runtime_get_sync(mpc8xxx_spi->dev);
455
Heiner Kallweit46afd382016-09-13 23:16:02 +0200456 cs->hw_mode = fsl_espi_read_reg(mpc8xxx_spi,
457 ESPI_SPMODEx(spi->chip_select));
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800458 /* mask out bits we are going to set */
459 cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH
460 | CSMODE_REV);
461
462 if (spi->mode & SPI_CPHA)
463 cs->hw_mode |= CSMODE_CP_BEGIN_EDGECLK;
464 if (spi->mode & SPI_CPOL)
465 cs->hw_mode |= CSMODE_CI_INACTIVEHIGH;
466 if (!(spi->mode & SPI_LSB_FIRST))
467 cs->hw_mode |= CSMODE_REV;
468
469 /* Handle the loop mode */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200470 loop_mode = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800471 loop_mode &= ~SPMODE_LOOP;
472 if (spi->mode & SPI_LOOP)
473 loop_mode |= SPMODE_LOOP;
Heiner Kallweit46afd382016-09-13 23:16:02 +0200474 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, loop_mode);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800475
Heiner Kallweitea616ee2016-08-25 06:44:42 +0200476 fsl_espi_setup_transfer(spi, NULL);
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200477
478 pm_runtime_mark_last_busy(mpc8xxx_spi->dev);
479 pm_runtime_put_autosuspend(mpc8xxx_spi->dev);
480
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800481 return 0;
482}
483
Axel Lind9f26742014-08-31 12:44:09 +0800484static void fsl_espi_cleanup(struct spi_device *spi)
485{
486 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
487
488 kfree(cs);
489 spi_set_ctldata(spi, NULL);
490}
491
Heiner Kallweit10ed1e62016-08-25 06:45:16 +0200492static void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800493{
Heiner Kallweitf05689a2016-10-27 21:27:35 +0200494 if (mspi->rx_len)
495 fsl_espi_read_rx_fifo(mspi, events);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800496
Heiner Kallweit54731262016-10-27 21:25:58 +0200497 if (mspi->tx_len)
498 fsl_espi_fill_tx_fifo(mspi, events);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800499
Heiner Kallweitdb1b0492016-10-27 21:28:02 +0200500 if (mspi->tx_len || mspi->rx_len)
501 return;
502
503 /* we're done, but check for errors before returning */
504 events = fsl_espi_read_reg(mspi, ESPI_SPIE);
505
506 if (!(events & SPIE_DON))
507 dev_err(mspi->dev,
508 "Transfer done but SPIE_DON isn't set!\n");
509
510 if (SPIE_RXCNT(events) || SPIE_TXCNT(events) != FSL_ESPI_FIFO_SIZE)
511 dev_err(mspi->dev, "Transfer done but rx/tx fifo's aren't empty!\n");
512
513 complete(&mspi->done);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800514}
515
516static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
517{
518 struct mpc8xxx_spi *mspi = context_data;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800519 u32 events;
520
Heiner Kallweit54731262016-10-27 21:25:58 +0200521 spin_lock(&mspi->lock);
522
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800523 /* Get interrupt events(tx/rx) */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200524 events = fsl_espi_read_reg(mspi, ESPI_SPIE);
Heiner Kallweit54731262016-10-27 21:25:58 +0200525 if (!events) {
Heiner Kallweit66b80532016-10-29 10:53:19 +0200526 spin_unlock(&mspi->lock);
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200527 return IRQ_NONE;
Heiner Kallweit54731262016-10-27 21:25:58 +0200528 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800529
530 dev_vdbg(mspi->dev, "%s: events %x\n", __func__, events);
531
532 fsl_espi_cpu_irq(mspi, events);
533
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200534 /* Clear the events */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200535 fsl_espi_write_reg(mspi, ESPI_SPIE, events);
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200536
Heiner Kallweit54731262016-10-27 21:25:58 +0200537 spin_unlock(&mspi->lock);
538
Heiner Kallweit35f5d712016-09-13 23:15:57 +0200539 return IRQ_HANDLED;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800540}
541
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200542#ifdef CONFIG_PM
543static int fsl_espi_runtime_suspend(struct device *dev)
Heiner Kallweit75506d02014-12-03 07:56:19 +0100544{
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200545 struct spi_master *master = dev_get_drvdata(dev);
546 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100547 u32 regval;
548
Heiner Kallweit46afd382016-09-13 23:16:02 +0200549 regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100550 regval &= ~SPMODE_ENABLE;
Heiner Kallweit46afd382016-09-13 23:16:02 +0200551 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100552
553 return 0;
554}
555
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200556static int fsl_espi_runtime_resume(struct device *dev)
Heiner Kallweit75506d02014-12-03 07:56:19 +0100557{
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200558 struct spi_master *master = dev_get_drvdata(dev);
559 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100560 u32 regval;
561
Heiner Kallweit46afd382016-09-13 23:16:02 +0200562 regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100563 regval |= SPMODE_ENABLE;
Heiner Kallweit46afd382016-09-13 23:16:02 +0200564 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
Heiner Kallweit75506d02014-12-03 07:56:19 +0100565
566 return 0;
567}
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200568#endif
Heiner Kallweit75506d02014-12-03 07:56:19 +0100569
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200570static size_t fsl_espi_max_message_size(struct spi_device *spi)
Michal Suchanekb541eef2015-12-02 10:38:21 +0000571{
572 return SPCOM_TRANLEN_MAX;
573}
574
Heiner Kallweit604042a2016-09-17 15:43:31 +0200575static int fsl_espi_probe(struct device *dev, struct resource *mem,
Heiner Kallweit74543462016-11-13 14:36:39 +0100576 unsigned int irq, unsigned int num_cs)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800577{
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800578 struct spi_master *master;
579 struct mpc8xxx_spi *mpc8xxx_spi;
Jane Wand0fb47a52014-04-16 13:09:39 -0700580 struct device_node *nc;
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200581 u32 regval, csmode, cs, prop;
582 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800583
584 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
Heiner Kallweit604042a2016-09-17 15:43:31 +0200585 if (!master)
586 return -ENOMEM;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800587
588 dev_set_drvdata(dev, master);
589
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100590 mpc8xxx_spi_probe(dev, mem, irq);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800591
Heiner Kallweit8263cb32016-11-09 22:58:34 +0100592 master->mode_bits |= SPI_RX_DUAL;
Stephen Warren24778be2013-05-21 20:36:35 -0600593 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800594 master->setup = fsl_espi_setup;
Axel Lind9f26742014-08-31 12:44:09 +0800595 master->cleanup = fsl_espi_cleanup;
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100596 master->transfer_one_message = fsl_espi_do_one_msg;
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200597 master->auto_runtime_pm = true;
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200598 master->max_message_size = fsl_espi_max_message_size;
Heiner Kallweit74543462016-11-13 14:36:39 +0100599 master->num_chipselect = num_cs;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800600
601 mpc8xxx_spi = spi_master_get_devdata(master);
Heiner Kallweit54731262016-10-27 21:25:58 +0200602 spin_lock_init(&mpc8xxx_spi->lock);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800603
Heiner Kallweit14238772016-09-07 22:50:22 +0200604 mpc8xxx_spi->local_buf =
605 devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL);
606 if (!mpc8xxx_spi->local_buf) {
607 ret = -ENOMEM;
608 goto err_probe;
609 }
610
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200611 mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
Axel Lin37c5db72015-08-30 18:35:51 +0800612 if (IS_ERR(mpc8xxx_spi->reg_base)) {
613 ret = PTR_ERR(mpc8xxx_spi->reg_base);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800614 goto err_probe;
615 }
616
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800617 /* Register for SPI Interrupt */
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200618 ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_espi_irq,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800619 0, "fsl_espi", mpc8xxx_spi);
620 if (ret)
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200621 goto err_probe;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800622
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800623 /* SPI controller initializations */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200624 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, 0);
625 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
626 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, 0);
627 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIE, 0xffffffff);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800628
629 /* Init eSPI CS mode register */
Jane Wand0fb47a52014-04-16 13:09:39 -0700630 for_each_available_child_of_node(master->dev.of_node, nc) {
631 /* get chip select */
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200632 ret = of_property_read_u32(nc, "reg", &cs);
Heiner Kallweit74543462016-11-13 14:36:39 +0100633 if (ret || cs >= num_cs)
Jane Wand0fb47a52014-04-16 13:09:39 -0700634 continue;
635
636 csmode = CSMODE_INIT_VAL;
Jane Wand0fb47a52014-04-16 13:09:39 -0700637
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200638 /* check if CSBEF is set in device tree */
639 ret = of_property_read_u32(nc, "fsl,csbef", &prop);
640 if (!ret) {
641 csmode &= ~(CSMODE_BEF(0xf));
642 csmode |= CSMODE_BEF(prop);
643 }
644
645 /* check if CSAFT is set in device tree */
646 ret = of_property_read_u32(nc, "fsl,csaft", &prop);
647 if (!ret) {
648 csmode &= ~(CSMODE_AFT(0xf));
649 csmode |= CSMODE_AFT(prop);
650 }
651
652 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(cs), csmode);
653
654 dev_info(dev, "cs=%u, init_csmode=0x%x\n", cs, csmode);
Jane Wand0fb47a52014-04-16 13:09:39 -0700655 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800656
657 /* Enable SPI interface */
Heiner Kallweit689d41f2016-11-13 14:36:01 +0100658 regval = SPMODE_INIT_VAL | SPMODE_ENABLE;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800659
Heiner Kallweit46afd382016-09-13 23:16:02 +0200660 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800661
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200662 pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
663 pm_runtime_use_autosuspend(dev);
664 pm_runtime_set_active(dev);
665 pm_runtime_enable(dev);
666 pm_runtime_get_sync(dev);
667
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200668 ret = devm_spi_register_master(dev, master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800669 if (ret < 0)
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200670 goto err_pm;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800671
Heiner Kallweit46afd382016-09-13 23:16:02 +0200672 dev_info(dev, "at 0x%p (irq = %d)\n", mpc8xxx_spi->reg_base,
673 mpc8xxx_spi->irq);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800674
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200675 pm_runtime_mark_last_busy(dev);
676 pm_runtime_put_autosuspend(dev);
677
Heiner Kallweit604042a2016-09-17 15:43:31 +0200678 return 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800679
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200680err_pm:
681 pm_runtime_put_noidle(dev);
682 pm_runtime_disable(dev);
683 pm_runtime_set_suspended(dev);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800684err_probe:
685 spi_master_put(master);
Heiner Kallweit604042a2016-09-17 15:43:31 +0200686 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800687}
688
689static int of_fsl_espi_get_chipselects(struct device *dev)
690{
691 struct device_node *np = dev->of_node;
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200692 u32 num_cs;
693 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800694
Heiner Kallweitb497eb02016-10-01 21:07:52 +0200695 ret = of_property_read_u32(np, "fsl,espi-num-chipselects", &num_cs);
696 if (ret) {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800697 dev_err(dev, "No 'fsl,espi-num-chipselects' property\n");
Heiner Kallweit74543462016-11-13 14:36:39 +0100698 return 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800699 }
700
Heiner Kallweit74543462016-11-13 14:36:39 +0100701 return num_cs;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800702}
703
Grant Likelyfd4a3192012-12-07 16:57:14 +0000704static int of_fsl_espi_probe(struct platform_device *ofdev)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800705{
706 struct device *dev = &ofdev->dev;
707 struct device_node *np = ofdev->dev.of_node;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800708 struct resource mem;
Heiner Kallweit74543462016-11-13 14:36:39 +0100709 unsigned int irq, num_cs;
Heiner Kallweitacf69212016-09-17 15:43:00 +0200710 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800711
Heiner Kallweite3ce4f42016-11-13 14:37:18 +0100712 if (of_property_read_bool(np, "mode")) {
713 dev_err(dev, "mode property is not supported on ESPI!\n");
714 return -EINVAL;
715 }
716
Grant Likely18d306d2011-02-22 21:02:43 -0700717 ret = of_mpc8xxx_spi_probe(ofdev);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800718 if (ret)
719 return ret;
720
Heiner Kallweit74543462016-11-13 14:36:39 +0100721 num_cs = of_fsl_espi_get_chipselects(dev);
722 if (!num_cs)
723 return -EINVAL;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800724
725 ret = of_address_to_resource(np, 0, &mem);
726 if (ret)
Heiner Kallweitacf69212016-09-17 15:43:00 +0200727 return ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800728
Thierry Redingf7578492013-09-18 15:24:44 +0200729 irq = irq_of_parse_and_map(np, 0);
Heiner Kallweitacf69212016-09-17 15:43:00 +0200730 if (!irq)
731 return -EINVAL;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800732
Heiner Kallweit74543462016-11-13 14:36:39 +0100733 return fsl_espi_probe(dev, &mem, irq, num_cs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800734}
735
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200736static int of_fsl_espi_remove(struct platform_device *dev)
737{
738 pm_runtime_disable(&dev->dev);
739
740 return 0;
741}
742
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800743#ifdef CONFIG_PM_SLEEP
744static int of_fsl_espi_suspend(struct device *dev)
745{
746 struct spi_master *master = dev_get_drvdata(dev);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800747 int ret;
748
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800749 ret = spi_master_suspend(master);
750 if (ret) {
751 dev_warn(dev, "cannot suspend master\n");
752 return ret;
753 }
754
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200755 ret = pm_runtime_force_suspend(dev);
756 if (ret < 0)
757 return ret;
758
759 return 0;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800760}
761
762static int of_fsl_espi_resume(struct device *dev)
763{
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800764 struct spi_master *master = dev_get_drvdata(dev);
765 struct mpc8xxx_spi *mpc8xxx_spi;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800766 u32 regval;
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200767 int i, ret;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800768
769 mpc8xxx_spi = spi_master_get_devdata(master);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800770
771 /* SPI controller initializations */
Heiner Kallweit46afd382016-09-13 23:16:02 +0200772 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, 0);
773 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
774 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, 0);
775 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIE, 0xffffffff);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800776
777 /* Init eSPI CS mode register */
Heiner Kallweit74543462016-11-13 14:36:39 +0100778 for (i = 0; i < master->num_chipselect; i++)
Heiner Kallweit46afd382016-09-13 23:16:02 +0200779 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(i),
780 CSMODE_INIT_VAL);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800781
782 /* Enable SPI interface */
Heiner Kallweit689d41f2016-11-13 14:36:01 +0100783 regval = SPMODE_INIT_VAL | SPMODE_ENABLE;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800784
Heiner Kallweit46afd382016-09-13 23:16:02 +0200785 fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800786
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200787 ret = pm_runtime_force_resume(dev);
788 if (ret < 0)
789 return ret;
790
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800791 return spi_master_resume(master);
792}
793#endif /* CONFIG_PM_SLEEP */
794
795static const struct dev_pm_ops espi_pm = {
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200796 SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend,
797 fsl_espi_runtime_resume, NULL)
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800798 SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume)
799};
800
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800801static const struct of_device_id of_fsl_espi_match[] = {
802 { .compatible = "fsl,mpc8536-espi" },
803 {}
804};
805MODULE_DEVICE_TABLE(of, of_fsl_espi_match);
806
Grant Likely18d306d2011-02-22 21:02:43 -0700807static struct platform_driver fsl_espi_driver = {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800808 .driver = {
809 .name = "fsl_espi",
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800810 .of_match_table = of_fsl_espi_match,
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800811 .pm = &espi_pm,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800812 },
813 .probe = of_fsl_espi_probe,
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200814 .remove = of_fsl_espi_remove,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800815};
Grant Likely940ab882011-10-05 11:29:49 -0600816module_platform_driver(fsl_espi_driver);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800817
818MODULE_AUTHOR("Mingkai Hu");
819MODULE_DESCRIPTION("Enhanced Freescale SPI Driver");
820MODULE_LICENSE("GPL");