Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 1 | /* |
| 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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 11 | #include <linux/delay.h> |
Xiubo Li | a310836 | 2014-09-29 10:57:06 +0800 | [diff] [blame] | 12 | #include <linux/err.h> |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 13 | #include <linux/fsl_devices.h> |
Xiubo Li | a310836 | 2014-09-29 10:57:06 +0800 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
Xiubo Li | a310836 | 2014-09-29 10:57:06 +0800 | [diff] [blame] | 15 | #include <linux/module.h> |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 16 | #include <linux/mm.h> |
| 17 | #include <linux/of.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 18 | #include <linux/of_address.h> |
| 19 | #include <linux/of_irq.h> |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 20 | #include <linux/of_platform.h> |
Xiubo Li | a310836 | 2014-09-29 10:57:06 +0800 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/spi/spi.h> |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 23 | #include <linux/pm_runtime.h> |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 24 | #include <sysdev/fsl_soc.h> |
| 25 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 26 | #include "spi-fsl-lib.h" |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 27 | |
| 28 | /* eSPI Controller registers */ |
| 29 | struct 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 | |
| 40 | struct fsl_espi_transfer { |
| 41 | const void *tx_buf; |
| 42 | void *rx_buf; |
| 43 | unsigned len; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 44 | 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 Zhiqiang | 5cfa1e4 | 2016-01-22 18:58:26 +0800 | [diff] [blame] | 84 | #define SPCOM_TRANLEN_MAX 0x10000 /* Max transaction length */ |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 85 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 86 | #define AUTOSUSPEND_TIMEOUT 2000 |
| 87 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 88 | static 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 = ®_base->csmode[spi->chip_select]; |
| 94 | __be32 __iomem *espi_mode = ®_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 | |
| 110 | static 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 Kallweit | ea616ee | 2016-08-25 06:44:42 +0200 | [diff] [blame] | 131 | static void fsl_espi_setup_transfer(struct spi_device *spi, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 132 | 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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 149 | 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 Warren | 51faed6 | 2013-05-30 09:59:41 -0600 | [diff] [blame] | 158 | } else { |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 159 | cs->rx_shift = 16 - bits_per_word; |
| 160 | if (spi->mode & SPI_LSB_FIRST) |
| 161 | cs->get_tx = fsl_espi_tx_buf_lsb; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 162 | } |
| 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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 169 | /* mask out bits we are going to set */ |
| 170 | cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF)); |
| 171 | |
Heiner Kallweit | a755af5 | 2016-09-04 09:56:57 +0200 | [diff] [blame] | 172 | cs->hw_mode |= CSMODE_LEN(bits_per_word - 1); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 173 | |
| 174 | if ((mpc8xxx_spi->spibrg / hz) > 64) { |
| 175 | cs->hw_mode |= CSMODE_DIV16; |
Sebastian Andrzej Siewior | 35faa55 | 2012-03-15 18:42:31 +0100 | [diff] [blame] | 176 | pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 16 * 4); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 177 | |
Sebastian Andrzej Siewior | 87bf5ab | 2012-03-15 18:42:32 +0100 | [diff] [blame] | 178 | WARN_ONCE(pm > 33, "%s: Requested speed is too low: %d Hz. " |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 179 | "Will use %d Hz instead.\n", dev_name(&spi->dev), |
Sebastian Andrzej Siewior | 87bf5ab | 2012-03-15 18:42:32 +0100 | [diff] [blame] | 180 | hz, mpc8xxx_spi->spibrg / (4 * 16 * (32 + 1))); |
| 181 | if (pm > 33) |
| 182 | pm = 33; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 183 | } else { |
Sebastian Andrzej Siewior | 35faa55 | 2012-03-15 18:42:31 +0100 | [diff] [blame] | 184 | pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 4); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 185 | } |
| 186 | if (pm) |
| 187 | pm--; |
Sebastian Andrzej Siewior | 87bf5ab | 2012-03-15 18:42:32 +0100 | [diff] [blame] | 188 | if (pm < 2) |
| 189 | pm = 2; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 190 | |
| 191 | cs->hw_mode |= CSMODE_PM(pm); |
| 192 | |
| 193 | fsl_espi_change_mode(spi); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 194 | } |
| 195 | |
Heiner Kallweit | bbb55f6 | 2016-08-25 06:44:58 +0200 | [diff] [blame] | 196 | static void fsl_espi_cpu_bufs(struct mpc8xxx_spi *mspi, struct spi_transfer *t, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 197 | 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(®_base->mask, SPIM_NE); |
| 206 | |
| 207 | /* transmit word */ |
| 208 | word = mspi->get_tx(mspi); |
| 209 | mpc8xxx_spi_write_reg(®_base->transmit, word); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static 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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 217 | int ret; |
| 218 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 219 | 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 Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 225 | reinit_completion(&mpc8xxx_spi->done); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 226 | |
| 227 | /* Set SPCOM[CS] and SPCOM[TRANLEN] field */ |
Hou Zhiqiang | 5cfa1e4 | 2016-01-22 18:58:26 +0800 | [diff] [blame] | 228 | if (t->len > SPCOM_TRANLEN_MAX) { |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 229 | 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(®_base->command, |
| 234 | (SPCOM_CS(spi->chip_select) | SPCOM_TRANLEN(t->len - 1))); |
| 235 | |
Heiner Kallweit | bbb55f6 | 2016-08-25 06:44:58 +0200 | [diff] [blame] | 236 | fsl_espi_cpu_bufs(mpc8xxx_spi, t, len); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 237 | |
Nobuteru Hayashi | aa70e56 | 2016-03-18 11:35:21 +0000 | [diff] [blame] | 238 | /* 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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 244 | |
| 245 | /* disable rx ints */ |
| 246 | mpc8xxx_spi_write_reg(®_base->mask, 0); |
| 247 | |
| 248 | return mpc8xxx_spi->count; |
| 249 | } |
| 250 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 251 | static 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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 257 | struct spi_transfer *t, *first, trans; |
| 258 | int status = 0; |
| 259 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 260 | 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 Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 268 | dev_err(mspi->dev, |
| 269 | "bits_per_word/speed_hz should be same for the same SPI transfer\n"); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 270 | 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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 281 | |
Heiner Kallweit | 71581a15 | 2016-09-04 09:57:18 +0200 | [diff] [blame] | 282 | fsl_espi_setup_transfer(spi, &trans); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 283 | |
Heiner Kallweit | dbd4fef | 2016-08-25 06:45:55 +0200 | [diff] [blame] | 284 | if (trans.len) |
| 285 | status = fsl_espi_bufs(spi, &trans); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 286 | |
Heiner Kallweit | dbd4fef | 2016-08-25 06:45:55 +0200 | [diff] [blame] | 287 | if (status) |
| 288 | status = -EMSGSIZE; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 289 | |
Heiner Kallweit | dbd4fef | 2016-08-25 06:45:55 +0200 | [diff] [blame] | 290 | if (trans.delay_usecs) |
| 291 | udelay(trans.delay_usecs); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 292 | |
| 293 | espi_trans->status = status; |
| 294 | fsl_espi_setup_transfer(spi, NULL); |
| 295 | } |
| 296 | |
| 297 | static void fsl_espi_cmd_trans(struct spi_message *m, |
| 298 | struct fsl_espi_transfer *trans, u8 *rx_buff) |
| 299 | { |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 300 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 301 | struct spi_transfer *t; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 302 | int i = 0; |
| 303 | struct fsl_espi_transfer *espi_trans = trans; |
| 304 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 305 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 306 | if (t->tx_buf) { |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 307 | memcpy(mspi->local_buf + i, t->tx_buf, t->len); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 308 | i += t->len; |
| 309 | } |
| 310 | } |
| 311 | |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 312 | espi_trans->tx_buf = mspi->local_buf; |
| 313 | espi_trans->rx_buf = mspi->local_buf; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 314 | fsl_espi_do_trans(m, espi_trans); |
| 315 | |
| 316 | espi_trans->actual_length = espi_trans->len; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static void fsl_espi_rw_trans(struct spi_message *m, |
| 320 | struct fsl_espi_transfer *trans, u8 *rx_buff) |
| 321 | { |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 322 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 323 | struct spi_transfer *t; |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 324 | unsigned int tx_only = 0; |
| 325 | int i = 0; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 326 | |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 327 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 328 | if (t->tx_buf) { |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 329 | memcpy(mspi->local_buf + i, t->tx_buf, t->len); |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 330 | i += t->len; |
| 331 | if (!t->rx_buf) |
| 332 | tx_only += t->len; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 333 | } |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 334 | } |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 335 | |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 336 | trans->tx_buf = mspi->local_buf; |
| 337 | trans->rx_buf = mspi->local_buf; |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 338 | fsl_espi_do_trans(m, trans); |
Jonatas Rech | 2000058 | 2015-04-15 12:23:18 -0300 | [diff] [blame] | 339 | |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 340 | 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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 346 | } |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 347 | } |
| 348 | |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 349 | static int fsl_espi_do_one_msg(struct spi_master *master, |
| 350 | struct spi_message *m) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 351 | { |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 352 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(master); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 353 | struct spi_transfer *t; |
| 354 | u8 *rx_buf = NULL; |
Jonatas Rech | 2000058 | 2015-04-15 12:23:18 -0300 | [diff] [blame] | 355 | unsigned int xfer_len = 0; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 356 | struct fsl_espi_transfer espi_trans; |
| 357 | |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 358 | memset(mspi->local_buf, 0, SPCOM_TRANLEN_MAX); |
| 359 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 360 | list_for_each_entry(t, &m->transfers, transfer_list) { |
Heiner Kallweit | daae020 | 2016-09-04 09:53:01 +0200 | [diff] [blame] | 361 | if (t->rx_buf) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 362 | rx_buf = t->rx_buf; |
Jonatas Rech | 2000058 | 2015-04-15 12:23:18 -0300 | [diff] [blame] | 363 | if ((t->tx_buf) || (t->rx_buf)) |
| 364 | xfer_len += t->len; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 365 | } |
| 366 | |
Jonatas Rech | 2000058 | 2015-04-15 12:23:18 -0300 | [diff] [blame] | 367 | espi_trans.len = xfer_len; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 368 | 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 Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 378 | spi_finalize_current_message(master); |
| 379 | return 0; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int fsl_espi_setup(struct spi_device *spi) |
| 383 | { |
| 384 | struct mpc8xxx_spi *mpc8xxx_spi; |
| 385 | struct fsl_espi_reg *reg_base; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 386 | u32 hw_mode; |
| 387 | u32 loop_mode; |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 388 | struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 389 | |
| 390 | if (!spi->max_speed_hz) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | if (!cs) { |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 394 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 395 | if (!cs) |
| 396 | return -ENOMEM; |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 397 | spi_set_ctldata(spi, cs); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
| 401 | reg_base = mpc8xxx_spi->reg_base; |
| 402 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 403 | pm_runtime_get_sync(mpc8xxx_spi->dev); |
| 404 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 405 | hw_mode = cs->hw_mode; /* Save original settings */ |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 406 | cs->hw_mode = mpc8xxx_spi_read_reg( |
| 407 | ®_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(®_base->mode); |
| 421 | loop_mode &= ~SPMODE_LOOP; |
| 422 | if (spi->mode & SPI_LOOP) |
| 423 | loop_mode |= SPMODE_LOOP; |
| 424 | mpc8xxx_spi_write_reg(®_base->mode, loop_mode); |
| 425 | |
Heiner Kallweit | ea616ee | 2016-08-25 06:44:42 +0200 | [diff] [blame] | 426 | fsl_espi_setup_transfer(spi, NULL); |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 427 | |
| 428 | pm_runtime_mark_last_busy(mpc8xxx_spi->dev); |
| 429 | pm_runtime_put_autosuspend(mpc8xxx_spi->dev); |
| 430 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 434 | static 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 Kallweit | 10ed1e6 | 2016-08-25 06:45:16 +0200 | [diff] [blame] | 442 | static void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 443 | { |
| 444 | struct fsl_espi_reg *reg_base = mspi->reg_base; |
| 445 | |
| 446 | /* We need handle RX first */ |
| 447 | if (events & SPIE_NE) { |
Mingkai Hu | e6289d6 | 2010-12-21 09:26:07 +0800 | [diff] [blame] | 448 | u32 rx_data, tmp; |
| 449 | u8 rx_data_8; |
Nobuteru Hayashi | 6319a68 | 2016-03-18 11:35:21 +0000 | [diff] [blame] | 450 | int rx_nr_bytes = 4; |
Nobuteru Hayashi | a12ddd6 | 2016-03-18 11:35:21 +0000 | [diff] [blame] | 451 | int ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 452 | |
| 453 | /* Spin until RX is done */ |
Nobuteru Hayashi | a12ddd6 | 2016-03-18 11:35:21 +0000 | [diff] [blame] | 454 | if (SPIE_RXCNT(events) < min(4, mspi->len)) { |
| 455 | ret = spin_event_timeout( |
| 456 | !(SPIE_RXCNT(events = |
| 457 | mpc8xxx_spi_read_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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 463 | } |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 464 | |
Mingkai Hu | e6289d6 | 2010-12-21 09:26:07 +0800 | [diff] [blame] | 465 | if (mspi->len >= 4) { |
| 466 | rx_data = mpc8xxx_spi_read_reg(®_base->receive); |
Nobuteru Hayashi | 6319a68 | 2016-03-18 11:35:21 +0000 | [diff] [blame] | 467 | } 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 Hu | e6289d6 | 2010-12-21 09:26:07 +0800 | [diff] [blame] | 473 | } else { |
Nobuteru Hayashi | 6319a68 | 2016-03-18 11:35:21 +0000 | [diff] [blame] | 474 | rx_nr_bytes = mspi->len; |
Mingkai Hu | e6289d6 | 2010-12-21 09:26:07 +0800 | [diff] [blame] | 475 | tmp = mspi->len; |
| 476 | rx_data = 0; |
| 477 | while (tmp--) { |
| 478 | rx_data_8 = in_8((u8 *)®_base->receive); |
| 479 | rx_data |= (rx_data_8 << (tmp * 8)); |
| 480 | } |
| 481 | |
| 482 | rx_data <<= (4 - mspi->len) * 8; |
| 483 | } |
| 484 | |
Nobuteru Hayashi | 6319a68 | 2016-03-18 11:35:21 +0000 | [diff] [blame] | 485 | mspi->len -= rx_nr_bytes; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 486 | |
| 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 Wan | 7a0a175 | 2015-05-01 16:37:42 -0700 | [diff] [blame] | 496 | ®_base->event)) & SPIE_NF), 1000, 0); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 497 | if (!ret) { |
| 498 | dev_err(mspi->dev, "tired waiting for SPIE_NF\n"); |
Jane Wan | 7a0a175 | 2015-05-01 16:37:42 -0700 | [diff] [blame] | 499 | |
| 500 | /* Clear the SPIE bits */ |
| 501 | mpc8xxx_spi_write_reg(®_base->event, events); |
| 502 | complete(&mspi->done); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /* Clear the events */ |
| 508 | mpc8xxx_spi_write_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(®_base->transmit, word); |
| 515 | } else { |
| 516 | complete(&mspi->done); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | static 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(®_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 Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 539 | #ifdef CONFIG_PM |
| 540 | static int fsl_espi_runtime_suspend(struct device *dev) |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 541 | { |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 542 | 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 Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 545 | u32 regval; |
| 546 | |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 547 | regval = mpc8xxx_spi_read_reg(®_base->mode); |
| 548 | regval &= ~SPMODE_ENABLE; |
| 549 | mpc8xxx_spi_write_reg(®_base->mode, regval); |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 554 | static int fsl_espi_runtime_resume(struct device *dev) |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 555 | { |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 556 | 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 Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 559 | u32 regval; |
| 560 | |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 561 | regval = mpc8xxx_spi_read_reg(®_base->mode); |
| 562 | regval |= SPMODE_ENABLE; |
| 563 | mpc8xxx_spi_write_reg(®_base->mode, regval); |
| 564 | |
| 565 | return 0; |
| 566 | } |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 567 | #endif |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 568 | |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 569 | static size_t fsl_espi_max_message_size(struct spi_device *spi) |
Michal Suchanek | b541eef | 2015-12-02 10:38:21 +0000 | [diff] [blame] | 570 | { |
| 571 | return SPCOM_TRANLEN_MAX; |
| 572 | } |
| 573 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 574 | static struct spi_master * fsl_espi_probe(struct device *dev, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 575 | struct resource *mem, unsigned int irq) |
| 576 | { |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 577 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 578 | struct spi_master *master; |
| 579 | struct mpc8xxx_spi *mpc8xxx_spi; |
| 580 | struct fsl_espi_reg *reg_base; |
Jane Wan | d0fb47a5 | 2014-04-16 13:09:39 -0700 | [diff] [blame] | 581 | struct device_node *nc; |
| 582 | const __be32 *prop; |
| 583 | u32 regval, csmode; |
| 584 | int i, len, ret = 0; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 585 | |
| 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 Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 594 | mpc8xxx_spi_probe(dev, mem, irq); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 595 | |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 596 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 597 | master->setup = fsl_espi_setup; |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 598 | master->cleanup = fsl_espi_cleanup; |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 599 | master->transfer_one_message = fsl_espi_do_one_msg; |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 600 | master->auto_runtime_pm = true; |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 601 | master->max_message_size = fsl_espi_max_message_size; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 602 | |
| 603 | mpc8xxx_spi = spi_master_get_devdata(master); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 604 | |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame^] | 605 | 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 Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 612 | mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem); |
Axel Lin | 37c5db7 | 2015-08-30 18:35:51 +0800 | [diff] [blame] | 613 | if (IS_ERR(mpc8xxx_spi->reg_base)) { |
| 614 | ret = PTR_ERR(mpc8xxx_spi->reg_base); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 615 | goto err_probe; |
| 616 | } |
| 617 | |
| 618 | reg_base = mpc8xxx_spi->reg_base; |
| 619 | |
| 620 | /* Register for SPI Interrupt */ |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 621 | ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_espi_irq, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 622 | 0, "fsl_espi", mpc8xxx_spi); |
| 623 | if (ret) |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 624 | goto err_probe; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 625 | |
| 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(®_base->mode, 0); |
| 633 | mpc8xxx_spi_write_reg(®_base->mask, 0); |
| 634 | mpc8xxx_spi_write_reg(®_base->command, 0); |
| 635 | mpc8xxx_spi_write_reg(®_base->event, 0xffffffff); |
| 636 | |
| 637 | /* Init eSPI CS mode register */ |
Jane Wan | d0fb47a5 | 2014-04-16 13:09:39 -0700 | [diff] [blame] | 638 | 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(®_base->csmode[i], csmode); |
| 661 | |
| 662 | dev_info(dev, "cs=%d, init_csmode=0x%x\n", i, csmode); |
| 663 | } |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 664 | |
| 665 | /* Enable SPI interface */ |
| 666 | regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE; |
| 667 | |
| 668 | mpc8xxx_spi_write_reg(®_base->mode, regval); |
| 669 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 670 | 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 Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 676 | ret = devm_spi_register_master(dev, master); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 677 | if (ret < 0) |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 678 | goto err_pm; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 679 | |
| 680 | dev_info(dev, "at 0x%p (irq = %d)\n", reg_base, mpc8xxx_spi->irq); |
| 681 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 682 | pm_runtime_mark_last_busy(dev); |
| 683 | pm_runtime_put_autosuspend(dev); |
| 684 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 685 | return master; |
| 686 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 687 | err_pm: |
| 688 | pm_runtime_put_noidle(dev); |
| 689 | pm_runtime_disable(dev); |
| 690 | pm_runtime_set_suspended(dev); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 691 | err_probe: |
| 692 | spi_master_put(master); |
| 693 | err: |
| 694 | return ERR_PTR(ret); |
| 695 | } |
| 696 | |
| 697 | static int of_fsl_espi_get_chipselects(struct device *dev) |
| 698 | { |
| 699 | struct device_node *np = dev->of_node; |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 700 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 701 | 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 Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 716 | static int of_fsl_espi_probe(struct platform_device *ofdev) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 717 | { |
| 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 Reding | f757849 | 2013-09-18 15:24:44 +0200 | [diff] [blame] | 722 | unsigned int irq; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 723 | int ret = -ENOMEM; |
| 724 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 725 | ret = of_mpc8xxx_spi_probe(ofdev); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 726 | 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 Reding | f757849 | 2013-09-18 15:24:44 +0200 | [diff] [blame] | 737 | irq = irq_of_parse_and_map(np, 0); |
Hou Zhiqiang | 7227cd1 | 2013-12-11 13:09:40 +0800 | [diff] [blame] | 738 | if (!irq) { |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 739 | ret = -EINVAL; |
| 740 | goto err; |
| 741 | } |
| 742 | |
Thierry Reding | f757849 | 2013-09-18 15:24:44 +0200 | [diff] [blame] | 743 | master = fsl_espi_probe(dev, &mem, irq); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 744 | if (IS_ERR(master)) { |
| 745 | ret = PTR_ERR(master); |
| 746 | goto err; |
| 747 | } |
| 748 | |
| 749 | return 0; |
| 750 | |
| 751 | err: |
| 752 | return ret; |
| 753 | } |
| 754 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 755 | static int of_fsl_espi_remove(struct platform_device *dev) |
| 756 | { |
| 757 | pm_runtime_disable(&dev->dev); |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 762 | #ifdef CONFIG_PM_SLEEP |
| 763 | static int of_fsl_espi_suspend(struct device *dev) |
| 764 | { |
| 765 | struct spi_master *master = dev_get_drvdata(dev); |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 766 | int ret; |
| 767 | |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 768 | ret = spi_master_suspend(master); |
| 769 | if (ret) { |
| 770 | dev_warn(dev, "cannot suspend master\n"); |
| 771 | return ret; |
| 772 | } |
| 773 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 774 | ret = pm_runtime_force_suspend(dev); |
| 775 | if (ret < 0) |
| 776 | return ret; |
| 777 | |
| 778 | return 0; |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static 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 Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 788 | int i, ret; |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 789 | |
| 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(®_base->mode, 0); |
| 795 | mpc8xxx_spi_write_reg(®_base->mask, 0); |
| 796 | mpc8xxx_spi_write_reg(®_base->command, 0); |
| 797 | mpc8xxx_spi_write_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(®_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(®_base->mode, regval); |
| 807 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 808 | ret = pm_runtime_force_resume(dev); |
| 809 | if (ret < 0) |
| 810 | return ret; |
| 811 | |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 812 | return spi_master_resume(master); |
| 813 | } |
| 814 | #endif /* CONFIG_PM_SLEEP */ |
| 815 | |
| 816 | static const struct dev_pm_ops espi_pm = { |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 817 | SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend, |
| 818 | fsl_espi_runtime_resume, NULL) |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 819 | SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume) |
| 820 | }; |
| 821 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 822 | static const struct of_device_id of_fsl_espi_match[] = { |
| 823 | { .compatible = "fsl,mpc8536-espi" }, |
| 824 | {} |
| 825 | }; |
| 826 | MODULE_DEVICE_TABLE(of, of_fsl_espi_match); |
| 827 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 828 | static struct platform_driver fsl_espi_driver = { |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 829 | .driver = { |
| 830 | .name = "fsl_espi", |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 831 | .of_match_table = of_fsl_espi_match, |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 832 | .pm = &espi_pm, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 833 | }, |
| 834 | .probe = of_fsl_espi_probe, |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 835 | .remove = of_fsl_espi_remove, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 836 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 837 | module_platform_driver(fsl_espi_driver); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 838 | |
| 839 | MODULE_AUTHOR("Mingkai Hu"); |
| 840 | MODULE_DESCRIPTION("Enhanced Freescale SPI Driver"); |
| 841 | MODULE_LICENSE("GPL"); |