blob: 81d7416668b868807e377790d668463ccf303296 [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 */
29struct fsl_espi_reg {
30 __be32 mode; /* 0x000 - eSPI mode register */
31 __be32 event; /* 0x004 - eSPI event register */
32 __be32 mask; /* 0x008 - eSPI mask register */
33 __be32 command; /* 0x00c - eSPI command register */
34 __be32 transmit; /* 0x010 - eSPI transmit FIFO access register*/
35 __be32 receive; /* 0x014 - eSPI receive FIFO access register*/
36 u8 res[8]; /* 0x018 - 0x01c reserved */
37 __be32 csmode[4]; /* 0x020 - 0x02c eSPI cs mode register */
38};
39
40struct fsl_espi_transfer {
41 const void *tx_buf;
42 void *rx_buf;
43 unsigned len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080044 unsigned actual_length;
45 int status;
46};
47
48/* eSPI Controller mode register definitions */
49#define SPMODE_ENABLE (1 << 31)
50#define SPMODE_LOOP (1 << 30)
51#define SPMODE_TXTHR(x) ((x) << 8)
52#define SPMODE_RXTHR(x) ((x) << 0)
53
54/* eSPI Controller CS mode register definitions */
55#define CSMODE_CI_INACTIVEHIGH (1 << 31)
56#define CSMODE_CP_BEGIN_EDGECLK (1 << 30)
57#define CSMODE_REV (1 << 29)
58#define CSMODE_DIV16 (1 << 28)
59#define CSMODE_PM(x) ((x) << 24)
60#define CSMODE_POL_1 (1 << 20)
61#define CSMODE_LEN(x) ((x) << 16)
62#define CSMODE_BEF(x) ((x) << 12)
63#define CSMODE_AFT(x) ((x) << 8)
64#define CSMODE_CG(x) ((x) << 3)
65
66/* Default mode/csmode for eSPI controller */
67#define SPMODE_INIT_VAL (SPMODE_TXTHR(4) | SPMODE_RXTHR(3))
68#define CSMODE_INIT_VAL (CSMODE_POL_1 | CSMODE_BEF(0) \
69 | CSMODE_AFT(0) | CSMODE_CG(1))
70
71/* SPIE register values */
72#define SPIE_NE 0x00000200 /* Not empty */
73#define SPIE_NF 0x00000100 /* Not full */
74
75/* SPIM register values */
76#define SPIM_NE 0x00000200 /* Not empty */
77#define SPIM_NF 0x00000100 /* Not full */
78#define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F)
79#define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F)
80
81/* SPCOM register values */
82#define SPCOM_CS(x) ((x) << 30)
83#define SPCOM_TRANLEN(x) ((x) << 0)
Hou Zhiqiang5cfa1e42016-01-22 18:58:26 +080084#define SPCOM_TRANLEN_MAX 0x10000 /* Max transaction length */
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080085
Heiner Kallweite9abb4d2015-08-26 21:21:55 +020086#define AUTOSUSPEND_TIMEOUT 2000
87
Mingkai Hu8b60d6c2010-10-12 18:18:32 +080088static void fsl_espi_change_mode(struct spi_device *spi)
89{
90 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
91 struct spi_mpc8xxx_cs *cs = spi->controller_state;
92 struct fsl_espi_reg *reg_base = mspi->reg_base;
93 __be32 __iomem *mode = &reg_base->csmode[spi->chip_select];
94 __be32 __iomem *espi_mode = &reg_base->mode;
95 u32 tmp;
96 unsigned long flags;
97
98 /* Turn off IRQs locally to minimize time that SPI is disabled. */
99 local_irq_save(flags);
100
101 /* Turn off SPI unit prior changing mode */
102 tmp = mpc8xxx_spi_read_reg(espi_mode);
103 mpc8xxx_spi_write_reg(espi_mode, tmp & ~SPMODE_ENABLE);
104 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
105 mpc8xxx_spi_write_reg(espi_mode, tmp);
106
107 local_irq_restore(flags);
108}
109
110static u32 fsl_espi_tx_buf_lsb(struct mpc8xxx_spi *mpc8xxx_spi)
111{
112 u32 data;
113 u16 data_h;
114 u16 data_l;
115 const u32 *tx = mpc8xxx_spi->tx;
116
117 if (!tx)
118 return 0;
119
120 data = *tx++ << mpc8xxx_spi->tx_shift;
121 data_l = data & 0xffff;
122 data_h = (data >> 16) & 0xffff;
123 swab16s(&data_l);
124 swab16s(&data_h);
125 data = data_h | data_l;
126
127 mpc8xxx_spi->tx = tx;
128 return data;
129}
130
Heiner Kallweitea616ee2016-08-25 06:44:42 +0200131static void fsl_espi_setup_transfer(struct spi_device *spi,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800132 struct spi_transfer *t)
133{
134 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
135 int bits_per_word = 0;
136 u8 pm;
137 u32 hz = 0;
138 struct spi_mpc8xxx_cs *cs = spi->controller_state;
139
140 if (t) {
141 bits_per_word = t->bits_per_word;
142 hz = t->speed_hz;
143 }
144
145 /* spi_transfer level calls that work per-word */
146 if (!bits_per_word)
147 bits_per_word = spi->bits_per_word;
148
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800149 if (!hz)
150 hz = spi->max_speed_hz;
151
152 cs->rx_shift = 0;
153 cs->tx_shift = 0;
154 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
155 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
156 if (bits_per_word <= 8) {
157 cs->rx_shift = 8 - bits_per_word;
Stephen Warren51faed62013-05-30 09:59:41 -0600158 } else {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800159 cs->rx_shift = 16 - bits_per_word;
160 if (spi->mode & SPI_LSB_FIRST)
161 cs->get_tx = fsl_espi_tx_buf_lsb;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800162 }
163
164 mpc8xxx_spi->rx_shift = cs->rx_shift;
165 mpc8xxx_spi->tx_shift = cs->tx_shift;
166 mpc8xxx_spi->get_rx = cs->get_rx;
167 mpc8xxx_spi->get_tx = cs->get_tx;
168
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800169 /* mask out bits we are going to set */
170 cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF));
171
Heiner Kallweita755af52016-09-04 09:56:57 +0200172 cs->hw_mode |= CSMODE_LEN(bits_per_word - 1);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800173
174 if ((mpc8xxx_spi->spibrg / hz) > 64) {
175 cs->hw_mode |= CSMODE_DIV16;
Sebastian Andrzej Siewior35faa552012-03-15 18:42:31 +0100176 pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 16 * 4);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800177
Sebastian Andrzej Siewior87bf5ab2012-03-15 18:42:32 +0100178 WARN_ONCE(pm > 33, "%s: Requested speed is too low: %d Hz. "
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800179 "Will use %d Hz instead.\n", dev_name(&spi->dev),
Sebastian Andrzej Siewior87bf5ab2012-03-15 18:42:32 +0100180 hz, mpc8xxx_spi->spibrg / (4 * 16 * (32 + 1)));
181 if (pm > 33)
182 pm = 33;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800183 } else {
Sebastian Andrzej Siewior35faa552012-03-15 18:42:31 +0100184 pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 4);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800185 }
186 if (pm)
187 pm--;
Sebastian Andrzej Siewior87bf5ab2012-03-15 18:42:32 +0100188 if (pm < 2)
189 pm = 2;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800190
191 cs->hw_mode |= CSMODE_PM(pm);
192
193 fsl_espi_change_mode(spi);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800194}
195
Heiner Kallweitbbb55f62016-08-25 06:44:58 +0200196static void fsl_espi_cpu_bufs(struct mpc8xxx_spi *mspi, struct spi_transfer *t,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800197 unsigned int len)
198{
199 u32 word;
200 struct fsl_espi_reg *reg_base = mspi->reg_base;
201
202 mspi->count = len;
203
204 /* enable rx ints */
205 mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
206
207 /* transmit word */
208 word = mspi->get_tx(mspi);
209 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800210}
211
212static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
213{
214 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
215 struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base;
216 unsigned int len = t->len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800217 int ret;
218
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800219 mpc8xxx_spi->len = t->len;
220 len = roundup(len, 4) / 4;
221
222 mpc8xxx_spi->tx = t->tx_buf;
223 mpc8xxx_spi->rx = t->rx_buf;
224
Wolfram Sang16735d02013-11-14 14:32:02 -0800225 reinit_completion(&mpc8xxx_spi->done);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800226
227 /* Set SPCOM[CS] and SPCOM[TRANLEN] field */
Hou Zhiqiang5cfa1e42016-01-22 18:58:26 +0800228 if (t->len > SPCOM_TRANLEN_MAX) {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800229 dev_err(mpc8xxx_spi->dev, "Transaction length (%d)"
230 " beyond the SPCOM[TRANLEN] field\n", t->len);
231 return -EINVAL;
232 }
233 mpc8xxx_spi_write_reg(&reg_base->command,
234 (SPCOM_CS(spi->chip_select) | SPCOM_TRANLEN(t->len - 1)));
235
Heiner Kallweitbbb55f62016-08-25 06:44:58 +0200236 fsl_espi_cpu_bufs(mpc8xxx_spi, t, len);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800237
Nobuteru Hayashiaa70e562016-03-18 11:35:21 +0000238 /* Won't hang up forever, SPI bus sometimes got lost interrupts... */
239 ret = wait_for_completion_timeout(&mpc8xxx_spi->done, 2 * HZ);
240 if (ret == 0)
241 dev_err(mpc8xxx_spi->dev,
242 "Transaction hanging up (left %d bytes)\n",
243 mpc8xxx_spi->count);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800244
245 /* disable rx ints */
246 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
247
248 return mpc8xxx_spi->count;
249}
250
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800251static void fsl_espi_do_trans(struct spi_message *m,
252 struct fsl_espi_transfer *tr)
253{
254 struct spi_device *spi = m->spi;
255 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
256 struct fsl_espi_transfer *espi_trans = tr;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800257 struct spi_transfer *t, *first, trans;
258 int status = 0;
259
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800260 memset(&trans, 0, sizeof(trans));
261
262 first = list_first_entry(&m->transfers, struct spi_transfer,
263 transfer_list);
264 list_for_each_entry(t, &m->transfers, transfer_list) {
265 if ((first->bits_per_word != t->bits_per_word) ||
266 (first->speed_hz != t->speed_hz)) {
267 espi_trans->status = -EINVAL;
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300268 dev_err(mspi->dev,
269 "bits_per_word/speed_hz should be same for the same SPI transfer\n");
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800270 return;
271 }
272
273 trans.speed_hz = t->speed_hz;
274 trans.bits_per_word = t->bits_per_word;
275 trans.delay_usecs = max(first->delay_usecs, t->delay_usecs);
276 }
277
278 trans.len = espi_trans->len;
279 trans.tx_buf = espi_trans->tx_buf;
280 trans.rx_buf = espi_trans->rx_buf;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800281
Heiner Kallweit71581a152016-09-04 09:57:18 +0200282 fsl_espi_setup_transfer(spi, &trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800283
Heiner Kallweitdbd4fef2016-08-25 06:45:55 +0200284 if (trans.len)
285 status = fsl_espi_bufs(spi, &trans);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800286
Heiner Kallweitdbd4fef2016-08-25 06:45:55 +0200287 if (status)
288 status = -EMSGSIZE;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800289
Heiner Kallweitdbd4fef2016-08-25 06:45:55 +0200290 if (trans.delay_usecs)
291 udelay(trans.delay_usecs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800292
293 espi_trans->status = status;
294 fsl_espi_setup_transfer(spi, NULL);
295}
296
297static void fsl_espi_cmd_trans(struct spi_message *m,
298 struct fsl_espi_transfer *trans, u8 *rx_buff)
299{
Heiner Kallweit14238772016-09-07 22:50:22 +0200300 struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800301 struct spi_transfer *t;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800302 int i = 0;
303 struct fsl_espi_transfer *espi_trans = trans;
304
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800305 list_for_each_entry(t, &m->transfers, transfer_list) {
306 if (t->tx_buf) {
Heiner Kallweit14238772016-09-07 22:50:22 +0200307 memcpy(mspi->local_buf + i, t->tx_buf, t->len);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800308 i += t->len;
309 }
310 }
311
Heiner Kallweit14238772016-09-07 22:50:22 +0200312 espi_trans->tx_buf = mspi->local_buf;
313 espi_trans->rx_buf = mspi->local_buf;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800314 fsl_espi_do_trans(m, espi_trans);
315
316 espi_trans->actual_length = espi_trans->len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800317}
318
319static void fsl_espi_rw_trans(struct spi_message *m,
320 struct fsl_espi_transfer *trans, u8 *rx_buff)
321{
Heiner Kallweit14238772016-09-07 22:50:22 +0200322 struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800323 struct spi_transfer *t;
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200324 unsigned int tx_only = 0;
325 int i = 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800326
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200327 list_for_each_entry(t, &m->transfers, transfer_list) {
328 if (t->tx_buf) {
Heiner Kallweit14238772016-09-07 22:50:22 +0200329 memcpy(mspi->local_buf + i, t->tx_buf, t->len);
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200330 i += t->len;
331 if (!t->rx_buf)
332 tx_only += t->len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800333 }
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200334 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800335
Heiner Kallweit14238772016-09-07 22:50:22 +0200336 trans->tx_buf = mspi->local_buf;
337 trans->rx_buf = mspi->local_buf;
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200338 fsl_espi_do_trans(m, trans);
Jonatas Rech20000582015-04-15 12:23:18 -0300339
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200340 if (!trans->status) {
341 /* If there is at least one RX byte then copy it to rx_buff */
342 if (trans->len > tx_only)
343 memcpy(rx_buff, trans->rx_buf + tx_only,
344 trans->len - tx_only);
345 trans->actual_length += trans->len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800346 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800347}
348
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100349static int fsl_espi_do_one_msg(struct spi_master *master,
350 struct spi_message *m)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800351{
Heiner Kallweit14238772016-09-07 22:50:22 +0200352 struct mpc8xxx_spi *mspi = spi_master_get_devdata(master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800353 struct spi_transfer *t;
354 u8 *rx_buf = NULL;
Jonatas Rech20000582015-04-15 12:23:18 -0300355 unsigned int xfer_len = 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800356 struct fsl_espi_transfer espi_trans;
357
Heiner Kallweit14238772016-09-07 22:50:22 +0200358 memset(mspi->local_buf, 0, SPCOM_TRANLEN_MAX);
359
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800360 list_for_each_entry(t, &m->transfers, transfer_list) {
Heiner Kallweitdaae0202016-09-04 09:53:01 +0200361 if (t->rx_buf)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800362 rx_buf = t->rx_buf;
Jonatas Rech20000582015-04-15 12:23:18 -0300363 if ((t->tx_buf) || (t->rx_buf))
364 xfer_len += t->len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800365 }
366
Jonatas Rech20000582015-04-15 12:23:18 -0300367 espi_trans.len = xfer_len;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800368 espi_trans.actual_length = 0;
369 espi_trans.status = 0;
370
371 if (!rx_buf)
372 fsl_espi_cmd_trans(m, &espi_trans, NULL);
373 else
374 fsl_espi_rw_trans(m, &espi_trans, rx_buf);
375
376 m->actual_length = espi_trans.actual_length;
377 m->status = espi_trans.status;
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100378 spi_finalize_current_message(master);
379 return 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800380}
381
382static int fsl_espi_setup(struct spi_device *spi)
383{
384 struct mpc8xxx_spi *mpc8xxx_spi;
385 struct fsl_espi_reg *reg_base;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800386 u32 hw_mode;
387 u32 loop_mode;
Axel Lind9f26742014-08-31 12:44:09 +0800388 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800389
390 if (!spi->max_speed_hz)
391 return -EINVAL;
392
393 if (!cs) {
Axel Lind9f26742014-08-31 12:44:09 +0800394 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800395 if (!cs)
396 return -ENOMEM;
Axel Lind9f26742014-08-31 12:44:09 +0800397 spi_set_ctldata(spi, cs);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800398 }
399
400 mpc8xxx_spi = spi_master_get_devdata(spi->master);
401 reg_base = mpc8xxx_spi->reg_base;
402
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200403 pm_runtime_get_sync(mpc8xxx_spi->dev);
404
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300405 hw_mode = cs->hw_mode; /* Save original settings */
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800406 cs->hw_mode = mpc8xxx_spi_read_reg(
407 &reg_base->csmode[spi->chip_select]);
408 /* mask out bits we are going to set */
409 cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH
410 | CSMODE_REV);
411
412 if (spi->mode & SPI_CPHA)
413 cs->hw_mode |= CSMODE_CP_BEGIN_EDGECLK;
414 if (spi->mode & SPI_CPOL)
415 cs->hw_mode |= CSMODE_CI_INACTIVEHIGH;
416 if (!(spi->mode & SPI_LSB_FIRST))
417 cs->hw_mode |= CSMODE_REV;
418
419 /* Handle the loop mode */
420 loop_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
421 loop_mode &= ~SPMODE_LOOP;
422 if (spi->mode & SPI_LOOP)
423 loop_mode |= SPMODE_LOOP;
424 mpc8xxx_spi_write_reg(&reg_base->mode, loop_mode);
425
Heiner Kallweitea616ee2016-08-25 06:44:42 +0200426 fsl_espi_setup_transfer(spi, NULL);
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200427
428 pm_runtime_mark_last_busy(mpc8xxx_spi->dev);
429 pm_runtime_put_autosuspend(mpc8xxx_spi->dev);
430
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800431 return 0;
432}
433
Axel Lind9f26742014-08-31 12:44:09 +0800434static void fsl_espi_cleanup(struct spi_device *spi)
435{
436 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
437
438 kfree(cs);
439 spi_set_ctldata(spi, NULL);
440}
441
Heiner Kallweit10ed1e62016-08-25 06:45:16 +0200442static void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800443{
444 struct fsl_espi_reg *reg_base = mspi->reg_base;
445
446 /* We need handle RX first */
447 if (events & SPIE_NE) {
Mingkai Hue6289d62010-12-21 09:26:07 +0800448 u32 rx_data, tmp;
449 u8 rx_data_8;
Nobuteru Hayashi6319a682016-03-18 11:35:21 +0000450 int rx_nr_bytes = 4;
Nobuteru Hayashia12ddd62016-03-18 11:35:21 +0000451 int ret;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800452
453 /* Spin until RX is done */
Nobuteru Hayashia12ddd62016-03-18 11:35:21 +0000454 if (SPIE_RXCNT(events) < min(4, mspi->len)) {
455 ret = spin_event_timeout(
456 !(SPIE_RXCNT(events =
457 mpc8xxx_spi_read_reg(&reg_base->event)) <
458 min(4, mspi->len)),
459 10000, 0); /* 10 msec */
460 if (!ret)
461 dev_err(mspi->dev,
462 "tired waiting for SPIE_RXCNT\n");
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800463 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800464
Mingkai Hue6289d62010-12-21 09:26:07 +0800465 if (mspi->len >= 4) {
466 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
Nobuteru Hayashi6319a682016-03-18 11:35:21 +0000467 } else if (mspi->len <= 0) {
468 dev_err(mspi->dev,
469 "unexpected RX(SPIE_NE) interrupt occurred,\n"
470 "(local rxlen %d bytes, reg rxlen %d bytes)\n",
471 min(4, mspi->len), SPIE_RXCNT(events));
472 rx_nr_bytes = 0;
Mingkai Hue6289d62010-12-21 09:26:07 +0800473 } else {
Nobuteru Hayashi6319a682016-03-18 11:35:21 +0000474 rx_nr_bytes = mspi->len;
Mingkai Hue6289d62010-12-21 09:26:07 +0800475 tmp = mspi->len;
476 rx_data = 0;
477 while (tmp--) {
478 rx_data_8 = in_8((u8 *)&reg_base->receive);
479 rx_data |= (rx_data_8 << (tmp * 8));
480 }
481
482 rx_data <<= (4 - mspi->len) * 8;
483 }
484
Nobuteru Hayashi6319a682016-03-18 11:35:21 +0000485 mspi->len -= rx_nr_bytes;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800486
487 if (mspi->rx)
488 mspi->get_rx(rx_data, mspi);
489 }
490
491 if (!(events & SPIE_NF)) {
492 int ret;
493
494 /* spin until TX is done */
495 ret = spin_event_timeout(((events = mpc8xxx_spi_read_reg(
Jane Wan7a0a1752015-05-01 16:37:42 -0700496 &reg_base->event)) & SPIE_NF), 1000, 0);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800497 if (!ret) {
498 dev_err(mspi->dev, "tired waiting for SPIE_NF\n");
Jane Wan7a0a1752015-05-01 16:37:42 -0700499
500 /* Clear the SPIE bits */
501 mpc8xxx_spi_write_reg(&reg_base->event, events);
502 complete(&mspi->done);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800503 return;
504 }
505 }
506
507 /* Clear the events */
508 mpc8xxx_spi_write_reg(&reg_base->event, events);
509
510 mspi->count -= 1;
511 if (mspi->count) {
512 u32 word = mspi->get_tx(mspi);
513
514 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
515 } else {
516 complete(&mspi->done);
517 }
518}
519
520static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
521{
522 struct mpc8xxx_spi *mspi = context_data;
523 struct fsl_espi_reg *reg_base = mspi->reg_base;
524 irqreturn_t ret = IRQ_NONE;
525 u32 events;
526
527 /* Get interrupt events(tx/rx) */
528 events = mpc8xxx_spi_read_reg(&reg_base->event);
529 if (events)
530 ret = IRQ_HANDLED;
531
532 dev_vdbg(mspi->dev, "%s: events %x\n", __func__, events);
533
534 fsl_espi_cpu_irq(mspi, events);
535
536 return ret;
537}
538
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200539#ifdef CONFIG_PM
540static int fsl_espi_runtime_suspend(struct device *dev)
Heiner Kallweit75506d02014-12-03 07:56:19 +0100541{
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200542 struct spi_master *master = dev_get_drvdata(dev);
543 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
544 struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base;
Heiner Kallweit75506d02014-12-03 07:56:19 +0100545 u32 regval;
546
Heiner Kallweit75506d02014-12-03 07:56:19 +0100547 regval = mpc8xxx_spi_read_reg(&reg_base->mode);
548 regval &= ~SPMODE_ENABLE;
549 mpc8xxx_spi_write_reg(&reg_base->mode, regval);
550
551 return 0;
552}
553
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200554static int fsl_espi_runtime_resume(struct device *dev)
Heiner Kallweit75506d02014-12-03 07:56:19 +0100555{
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200556 struct spi_master *master = dev_get_drvdata(dev);
557 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
558 struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base;
Heiner Kallweit75506d02014-12-03 07:56:19 +0100559 u32 regval;
560
Heiner Kallweit75506d02014-12-03 07:56:19 +0100561 regval = mpc8xxx_spi_read_reg(&reg_base->mode);
562 regval |= SPMODE_ENABLE;
563 mpc8xxx_spi_write_reg(&reg_base->mode, regval);
564
565 return 0;
566}
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200567#endif
Heiner Kallweit75506d02014-12-03 07:56:19 +0100568
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200569static size_t fsl_espi_max_message_size(struct spi_device *spi)
Michal Suchanekb541eef2015-12-02 10:38:21 +0000570{
571 return SPCOM_TRANLEN_MAX;
572}
573
Grant Likelyfd4a3192012-12-07 16:57:14 +0000574static struct spi_master * fsl_espi_probe(struct device *dev,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800575 struct resource *mem, unsigned int irq)
576{
Jingoo Han8074cf02013-07-30 16:58:59 +0900577 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800578 struct spi_master *master;
579 struct mpc8xxx_spi *mpc8xxx_spi;
580 struct fsl_espi_reg *reg_base;
Jane Wand0fb47a52014-04-16 13:09:39 -0700581 struct device_node *nc;
582 const __be32 *prop;
583 u32 regval, csmode;
584 int i, len, ret = 0;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800585
586 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
587 if (!master) {
588 ret = -ENOMEM;
589 goto err;
590 }
591
592 dev_set_drvdata(dev, master);
593
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100594 mpc8xxx_spi_probe(dev, mem, irq);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800595
Stephen Warren24778be2013-05-21 20:36:35 -0600596 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800597 master->setup = fsl_espi_setup;
Axel Lind9f26742014-08-31 12:44:09 +0800598 master->cleanup = fsl_espi_cleanup;
Heiner Kallweitc592bec2014-12-03 07:56:17 +0100599 master->transfer_one_message = fsl_espi_do_one_msg;
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200600 master->auto_runtime_pm = true;
Heiner Kallweit02a595d2016-08-17 21:11:01 +0200601 master->max_message_size = fsl_espi_max_message_size;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800602
603 mpc8xxx_spi = spi_master_get_devdata(master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800604
Heiner Kallweit14238772016-09-07 22:50:22 +0200605 mpc8xxx_spi->local_buf =
606 devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL);
607 if (!mpc8xxx_spi->local_buf) {
608 ret = -ENOMEM;
609 goto err_probe;
610 }
611
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200612 mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
Axel Lin37c5db72015-08-30 18:35:51 +0800613 if (IS_ERR(mpc8xxx_spi->reg_base)) {
614 ret = PTR_ERR(mpc8xxx_spi->reg_base);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800615 goto err_probe;
616 }
617
618 reg_base = mpc8xxx_spi->reg_base;
619
620 /* Register for SPI Interrupt */
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200621 ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_espi_irq,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800622 0, "fsl_espi", mpc8xxx_spi);
623 if (ret)
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200624 goto err_probe;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800625
626 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
627 mpc8xxx_spi->rx_shift = 16;
628 mpc8xxx_spi->tx_shift = 24;
629 }
630
631 /* SPI controller initializations */
632 mpc8xxx_spi_write_reg(&reg_base->mode, 0);
633 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
634 mpc8xxx_spi_write_reg(&reg_base->command, 0);
635 mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
636
637 /* Init eSPI CS mode register */
Jane Wand0fb47a52014-04-16 13:09:39 -0700638 for_each_available_child_of_node(master->dev.of_node, nc) {
639 /* get chip select */
640 prop = of_get_property(nc, "reg", &len);
641 if (!prop || len < sizeof(*prop))
642 continue;
643 i = be32_to_cpup(prop);
644 if (i < 0 || i >= pdata->max_chipselect)
645 continue;
646
647 csmode = CSMODE_INIT_VAL;
648 /* check if CSBEF is set in device tree */
649 prop = of_get_property(nc, "fsl,csbef", &len);
650 if (prop && len >= sizeof(*prop)) {
651 csmode &= ~(CSMODE_BEF(0xf));
652 csmode |= CSMODE_BEF(be32_to_cpup(prop));
653 }
654 /* check if CSAFT is set in device tree */
655 prop = of_get_property(nc, "fsl,csaft", &len);
656 if (prop && len >= sizeof(*prop)) {
657 csmode &= ~(CSMODE_AFT(0xf));
658 csmode |= CSMODE_AFT(be32_to_cpup(prop));
659 }
660 mpc8xxx_spi_write_reg(&reg_base->csmode[i], csmode);
661
662 dev_info(dev, "cs=%d, init_csmode=0x%x\n", i, csmode);
663 }
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800664
665 /* Enable SPI interface */
666 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
667
668 mpc8xxx_spi_write_reg(&reg_base->mode, regval);
669
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200670 pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
671 pm_runtime_use_autosuspend(dev);
672 pm_runtime_set_active(dev);
673 pm_runtime_enable(dev);
674 pm_runtime_get_sync(dev);
675
Heiner Kallweit4178b6b2015-08-26 21:21:50 +0200676 ret = devm_spi_register_master(dev, master);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800677 if (ret < 0)
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200678 goto err_pm;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800679
680 dev_info(dev, "at 0x%p (irq = %d)\n", reg_base, mpc8xxx_spi->irq);
681
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200682 pm_runtime_mark_last_busy(dev);
683 pm_runtime_put_autosuspend(dev);
684
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800685 return master;
686
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200687err_pm:
688 pm_runtime_put_noidle(dev);
689 pm_runtime_disable(dev);
690 pm_runtime_set_suspended(dev);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800691err_probe:
692 spi_master_put(master);
693err:
694 return ERR_PTR(ret);
695}
696
697static int of_fsl_espi_get_chipselects(struct device *dev)
698{
699 struct device_node *np = dev->of_node;
Jingoo Han8074cf02013-07-30 16:58:59 +0900700 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800701 const u32 *prop;
702 int len;
703
704 prop = of_get_property(np, "fsl,espi-num-chipselects", &len);
705 if (!prop || len < sizeof(*prop)) {
706 dev_err(dev, "No 'fsl,espi-num-chipselects' property\n");
707 return -EINVAL;
708 }
709
710 pdata->max_chipselect = *prop;
711 pdata->cs_control = NULL;
712
713 return 0;
714}
715
Grant Likelyfd4a3192012-12-07 16:57:14 +0000716static int of_fsl_espi_probe(struct platform_device *ofdev)
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800717{
718 struct device *dev = &ofdev->dev;
719 struct device_node *np = ofdev->dev.of_node;
720 struct spi_master *master;
721 struct resource mem;
Thierry Redingf7578492013-09-18 15:24:44 +0200722 unsigned int irq;
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800723 int ret = -ENOMEM;
724
Grant Likely18d306d2011-02-22 21:02:43 -0700725 ret = of_mpc8xxx_spi_probe(ofdev);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800726 if (ret)
727 return ret;
728
729 ret = of_fsl_espi_get_chipselects(dev);
730 if (ret)
731 goto err;
732
733 ret = of_address_to_resource(np, 0, &mem);
734 if (ret)
735 goto err;
736
Thierry Redingf7578492013-09-18 15:24:44 +0200737 irq = irq_of_parse_and_map(np, 0);
Hou Zhiqiang7227cd12013-12-11 13:09:40 +0800738 if (!irq) {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800739 ret = -EINVAL;
740 goto err;
741 }
742
Thierry Redingf7578492013-09-18 15:24:44 +0200743 master = fsl_espi_probe(dev, &mem, irq);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800744 if (IS_ERR(master)) {
745 ret = PTR_ERR(master);
746 goto err;
747 }
748
749 return 0;
750
751err:
752 return ret;
753}
754
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200755static int of_fsl_espi_remove(struct platform_device *dev)
756{
757 pm_runtime_disable(&dev->dev);
758
759 return 0;
760}
761
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800762#ifdef CONFIG_PM_SLEEP
763static int of_fsl_espi_suspend(struct device *dev)
764{
765 struct spi_master *master = dev_get_drvdata(dev);
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800766 int ret;
767
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800768 ret = spi_master_suspend(master);
769 if (ret) {
770 dev_warn(dev, "cannot suspend master\n");
771 return ret;
772 }
773
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200774 ret = pm_runtime_force_suspend(dev);
775 if (ret < 0)
776 return ret;
777
778 return 0;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800779}
780
781static int of_fsl_espi_resume(struct device *dev)
782{
783 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
784 struct spi_master *master = dev_get_drvdata(dev);
785 struct mpc8xxx_spi *mpc8xxx_spi;
786 struct fsl_espi_reg *reg_base;
787 u32 regval;
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200788 int i, ret;
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800789
790 mpc8xxx_spi = spi_master_get_devdata(master);
791 reg_base = mpc8xxx_spi->reg_base;
792
793 /* SPI controller initializations */
794 mpc8xxx_spi_write_reg(&reg_base->mode, 0);
795 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
796 mpc8xxx_spi_write_reg(&reg_base->command, 0);
797 mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
798
799 /* Init eSPI CS mode register */
800 for (i = 0; i < pdata->max_chipselect; i++)
801 mpc8xxx_spi_write_reg(&reg_base->csmode[i], CSMODE_INIT_VAL);
802
803 /* Enable SPI interface */
804 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
805
806 mpc8xxx_spi_write_reg(&reg_base->mode, regval);
807
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200808 ret = pm_runtime_force_resume(dev);
809 if (ret < 0)
810 return ret;
811
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800812 return spi_master_resume(master);
813}
814#endif /* CONFIG_PM_SLEEP */
815
816static const struct dev_pm_ops espi_pm = {
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200817 SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend,
818 fsl_espi_runtime_resume, NULL)
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800819 SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume)
820};
821
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800822static const struct of_device_id of_fsl_espi_match[] = {
823 { .compatible = "fsl,mpc8536-espi" },
824 {}
825};
826MODULE_DEVICE_TABLE(of, of_fsl_espi_match);
827
Grant Likely18d306d2011-02-22 21:02:43 -0700828static struct platform_driver fsl_espi_driver = {
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800829 .driver = {
830 .name = "fsl_espi",
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800831 .of_match_table = of_fsl_espi_match,
Hou Zhiqiang714bb652013-12-12 12:53:52 +0800832 .pm = &espi_pm,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800833 },
834 .probe = of_fsl_espi_probe,
Heiner Kallweite9abb4d2015-08-26 21:21:55 +0200835 .remove = of_fsl_espi_remove,
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800836};
Grant Likely940ab882011-10-05 11:29:49 -0600837module_platform_driver(fsl_espi_driver);
Mingkai Hu8b60d6c2010-10-12 18:18:32 +0800838
839MODULE_AUTHOR("Mingkai Hu");
840MODULE_DESCRIPTION("Enhanced Freescale SPI Driver");
841MODULE_LICENSE("GPL");