11 #include <linux/module.h>
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)
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)
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))
72 #define SPIE_NE 0x00000200
73 #define SPIE_NF 0x00000100
76 #define SPIM_NE 0x00000200
77 #define SPIM_NF 0x00000100
78 #define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F)
79 #define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F)
82 #define SPCOM_CS(x) ((x) << 30)
83 #define SPCOM_TRANLEN(x) ((x) << 0)
84 #define SPCOM_TRANLEN_MAX 0xFFFF
100 tmp = mpc8xxx_spi_read_reg(espi_mode);
102 mpc8xxx_spi_write_reg(mode, cs->
hw_mode);
103 mpc8xxx_spi_write_reg(espi_mode, tmp);
113 const u32 *
tx = mpc8xxx_spi->
tx;
118 data = *tx++ << mpc8xxx_spi->
tx_shift;
119 data_l = data & 0xffff;
120 data_h = (data >> 16) & 0xffff;
123 data = data_h | data_l;
125 mpc8xxx_spi->
tx =
tx;
132 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->
master);
133 int bits_per_word = 0;
148 if ((bits_per_word < 4) || (bits_per_word > 16))
158 if (bits_per_word <= 8) {
160 }
else if (bits_per_word <= 16) {
163 cs->
get_tx = fsl_espi_tx_buf_lsb;
173 bits_per_word = bits_per_word - 1;
180 if ((mpc8xxx_spi->
spibrg / hz) > 64) {
184 WARN_ONCE(pm > 33,
"%s: Requested speed is too low: %d Hz. "
185 "Will use %d Hz instead.\n", dev_name(&spi->
dev),
186 hz, mpc8xxx_spi->
spibrg / (4 * 16 * (32 + 1)));
199 fsl_espi_change_mode(spi);
203 static int fsl_espi_cpu_bufs(
struct mpc8xxx_spi *mspi,
struct spi_transfer *t,
215 word = mspi->
get_tx(mspi);
216 mpc8xxx_spi_write_reg(®_base->
transmit, word);
223 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->
master);
225 unsigned int len = t->
len;
233 mpc8xxx_spi->len = t->
len;
243 dev_err(mpc8xxx_spi->
dev,
"Transaction length (%d)"
244 " beyond the SPCOM[TRANLEN] field\n", t->
len);
247 mpc8xxx_spi_write_reg(®_base->
command,
250 ret = fsl_espi_cpu_bufs(mpc8xxx_spi, t, len);
257 mpc8xxx_spi_write_reg(®_base->
mask, 0);
259 return mpc8xxx_spi->
count;
262 static inline void fsl_espi_addr2cmd(
unsigned int addr,
u8 *
cmd)
265 cmd[1] = (
u8)(addr >> 16);
266 cmd[2] = (
u8)(addr >> 8);
267 cmd[3] = (
u8)(addr >> 0);
271 static inline unsigned int fsl_espi_cmd2addr(
u8 *cmd)
274 return cmd[1] << 16 | cmd[2] << 8 | cmd[3] << 0;
283 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->
master);
290 memset(&trans, 0,
sizeof(trans));
298 dev_err(mspi->
dev,
"bits_per_word/speed_hz should be"
299 " same for the same SPI transfer\n");
308 trans.
len = espi_trans->
len;
311 spi_message_add_tail(&trans, &
message);
317 status = fsl_espi_setup_transfer(spi, t);
323 status = fsl_espi_bufs(spi, t);
335 fsl_espi_setup_transfer(spi,
NULL);
338 static void fsl_espi_cmd_trans(
struct spi_message *m,
359 espi_trans->
tx_buf = local_buf;
360 espi_trans->
rx_buf = local_buf + espi_trans->
n_tx;
361 fsl_espi_do_trans(m, espi_trans);
367 static void fsl_espi_rw_trans(
struct spi_message *m,
371 unsigned int n_tx = espi_trans->
n_tx;
372 unsigned int n_rx = espi_trans->
n_rx;
376 unsigned int trans_len;
386 for (pos = 0, loop = 0; pos < n_rx; pos += trans_len, loop++) {
387 trans_len = n_rx -
pos;
400 addr = fsl_espi_cmd2addr(local_buf);
402 fsl_espi_addr2cmd(addr, local_buf);
405 espi_trans->
n_tx = n_tx;
406 espi_trans->
n_rx = trans_len;
407 espi_trans->
len = trans_len + n_tx;
408 espi_trans->
tx_buf = local_buf;
409 espi_trans->
rx_buf = local_buf + n_tx;
410 fsl_espi_do_trans(m, espi_trans);
412 memcpy(rx_buf + pos, espi_trans->
rx_buf + n_tx, trans_len);
423 static void fsl_espi_do_one_msg(
struct spi_message *m)
427 unsigned int n_tx = 0;
428 unsigned int n_rx = 0;
447 fsl_espi_cmd_trans(m, &espi_trans,
NULL);
449 fsl_espi_rw_trans(m, &espi_trans, rx_buf);
456 static int fsl_espi_setup(
struct spi_device *spi)
458 struct mpc8xxx_spi *mpc8xxx_spi;
475 mpc8xxx_spi = spi_master_get_devdata(spi->
master);
479 cs->
hw_mode = mpc8xxx_spi_read_reg(
493 loop_mode = mpc8xxx_spi_read_reg(®_base->
mode);
497 mpc8xxx_spi_write_reg(®_base->
mode, loop_mode);
499 retval = fsl_espi_setup_transfer(spi,
NULL);
519 events = mpc8xxx_spi_read_reg(®_base->
event);
522 if (mspi->len >= 4) {
523 rx_data = mpc8xxx_spi_read_reg(®_base->
receive);
529 rx_data |= (rx_data_8 << (tmp * 8));
532 rx_data <<= (4 - mspi->len) * 8;
538 mspi->
get_rx(rx_data, mspi);
545 ret = spin_event_timeout(((events = mpc8xxx_spi_read_reg(
546 ®_base->
event)) & SPIE_NF) == 0, 1000, 0);
548 dev_err(mspi->
dev,
"tired waiting for SPIE_NF\n");
554 mpc8xxx_spi_write_reg(®_base->
event, events);
560 mpc8xxx_spi_write_reg(®_base->
transmit, word);
568 struct mpc8xxx_spi *mspi = context_data;
574 events = mpc8xxx_spi_read_reg(®_base->
event);
578 dev_vdbg(mspi->
dev,
"%s: events %x\n", __func__, events);
585 static void fsl_espi_remove(
struct mpc8xxx_spi *mspi)
595 struct mpc8xxx_spi *mpc8xxx_spi;
612 master->
setup = fsl_espi_setup;
614 mpc8xxx_spi = spi_master_get_devdata(master);
628 0,
"fsl_espi", mpc8xxx_spi);
638 mpc8xxx_spi_write_reg(®_base->
mode, 0);
639 mpc8xxx_spi_write_reg(®_base->
mask, 0);
640 mpc8xxx_spi_write_reg(®_base->
command, 0);
641 mpc8xxx_spi_write_reg(®_base->
event, 0xffffffff);
650 mpc8xxx_spi_write_reg(®_base->
mode, regval);
656 dev_info(dev,
"at 0x%p (irq = %d)\n", reg_base, mpc8xxx_spi->
irq);
665 spi_master_put(master);
670 static int of_fsl_espi_get_chipselects(
struct device *dev)
678 if (!prop || len <
sizeof(*prop)) {
679 dev_err(dev,
"No 'fsl,espi-num-chipselects' property\n");
702 ret = of_fsl_espi_get_chipselects(dev);
716 master = fsl_espi_probe(dev, &mem, irq.
start);
717 if (IS_ERR(master)) {
718 ret = PTR_ERR(master);
733 static const struct of_device_id of_fsl_espi_match[] = {
734 { .compatible =
"fsl,mpc8536-espi" },
743 .of_match_table = of_fsl_espi_match,
745 .probe = of_fsl_espi_probe,