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 */ |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 29 | #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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 38 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 39 | /* eSPI Controller mode register definitions */ |
Heiner Kallweit | 81abc2e | 2016-09-13 23:16:06 +0200 | [diff] [blame] | 40 | #define SPMODE_ENABLE BIT(31) |
| 41 | #define SPMODE_LOOP BIT(30) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 42 | #define SPMODE_TXTHR(x) ((x) << 8) |
| 43 | #define SPMODE_RXTHR(x) ((x) << 0) |
| 44 | |
| 45 | /* eSPI Controller CS mode register definitions */ |
Heiner Kallweit | 81abc2e | 2016-09-13 23:16:06 +0200 | [diff] [blame] | 46 | #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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 50 | #define CSMODE_PM(x) ((x) << 24) |
Heiner Kallweit | 81abc2e | 2016-09-13 23:16:06 +0200 | [diff] [blame] | 51 | #define CSMODE_POL_1 BIT(20) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 52 | #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 Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 57 | #define FSL_ESPI_FIFO_SIZE 32 |
Heiner Kallweit | e508cea | 2016-10-27 21:27:56 +0200 | [diff] [blame] | 58 | #define FSL_ESPI_RXTHR 15 |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 59 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 60 | /* Default mode/csmode for eSPI controller */ |
Heiner Kallweit | e508cea | 2016-10-27 21:27:56 +0200 | [diff] [blame] | 61 | #define SPMODE_INIT_VAL (SPMODE_TXTHR(4) | SPMODE_RXTHR(FSL_ESPI_RXTHR)) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 62 | #define CSMODE_INIT_VAL (CSMODE_POL_1 | CSMODE_BEF(0) \ |
| 63 | | CSMODE_AFT(0) | CSMODE_CG(1)) |
| 64 | |
| 65 | /* SPIE register values */ |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 66 | #define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F) |
| 67 | #define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F) |
Heiner Kallweit | 81abc2e | 2016-09-13 23:16:06 +0200 | [diff] [blame] | 68 | #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 Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 84 | |
| 85 | /* SPCOM register values */ |
| 86 | #define SPCOM_CS(x) ((x) << 30) |
Heiner Kallweit | 81abc2e | 2016-09-13 23:16:06 +0200 | [diff] [blame] | 87 | #define SPCOM_DO BIT(28) /* Dual output */ |
| 88 | #define SPCOM_TO BIT(27) /* TX only */ |
| 89 | #define SPCOM_RXSKIP(x) ((x) << 16) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 90 | #define SPCOM_TRANLEN(x) ((x) << 0) |
Heiner Kallweit | 81abc2e | 2016-09-13 23:16:06 +0200 | [diff] [blame] | 91 | |
Hou Zhiqiang | 5cfa1e4 | 2016-01-22 18:58:26 +0800 | [diff] [blame] | 92 | #define SPCOM_TRANLEN_MAX 0x10000 /* Max transaction length */ |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 93 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 94 | #define AUTOSUSPEND_TIMEOUT 2000 |
| 95 | |
Heiner Kallweit | 219b5e3 | 2016-11-13 14:38:05 +0100 | [diff] [blame] | 96 | struct fsl_espi_cs { |
| 97 | u32 hw_mode; |
| 98 | }; |
| 99 | |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 100 | static inline u32 fsl_espi_read_reg(struct mpc8xxx_spi *mspi, int offset) |
| 101 | { |
| 102 | return ioread32be(mspi->reg_base + offset); |
| 103 | } |
| 104 | |
| 105 | static inline u8 fsl_espi_read_reg8(struct mpc8xxx_spi *mspi, int offset) |
| 106 | { |
| 107 | return ioread8(mspi->reg_base + offset); |
| 108 | } |
| 109 | |
| 110 | static inline void fsl_espi_write_reg(struct mpc8xxx_spi *mspi, int offset, |
| 111 | u32 val) |
| 112 | { |
| 113 | iowrite32be(val, mspi->reg_base + offset); |
| 114 | } |
| 115 | |
| 116 | static inline void fsl_espi_write_reg8(struct mpc8xxx_spi *mspi, int offset, |
| 117 | u8 val) |
| 118 | { |
| 119 | iowrite8(val, mspi->reg_base + offset); |
| 120 | } |
| 121 | |
Heiner Kallweit | 923ab15 | 2016-10-02 14:22:57 +0200 | [diff] [blame] | 122 | static void fsl_espi_memcpy_swab(void *to, const void *from, |
| 123 | struct spi_message *m, |
| 124 | struct spi_transfer *t) |
| 125 | { |
| 126 | unsigned int len = t->len; |
| 127 | |
| 128 | if (!(m->spi->mode & SPI_LSB_FIRST) || t->bits_per_word <= 8) { |
| 129 | memcpy(to, from, len); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | /* In case of LSB-first and bits_per_word > 8 byte-swap all words */ |
| 134 | while (len) |
| 135 | if (len >= 4) { |
| 136 | *(u32 *)to = swahb32p(from); |
| 137 | to += 4; |
| 138 | from += 4; |
| 139 | len -= 4; |
| 140 | } else { |
| 141 | *(u16 *)to = swab16p(from); |
| 142 | to += 2; |
| 143 | from += 2; |
| 144 | len -= 2; |
| 145 | } |
| 146 | } |
| 147 | |
Heiner Kallweit | cce7e3a | 2016-09-07 22:54:18 +0200 | [diff] [blame] | 148 | static void fsl_espi_copy_to_buf(struct spi_message *m, |
| 149 | struct mpc8xxx_spi *mspi) |
Heiner Kallweit | 7c159aa | 2016-09-07 22:50:53 +0200 | [diff] [blame] | 150 | { |
Heiner Kallweit | 7c159aa | 2016-09-07 22:50:53 +0200 | [diff] [blame] | 151 | struct spi_transfer *t; |
| 152 | u8 *buf = mspi->local_buf; |
| 153 | |
| 154 | list_for_each_entry(t, &m->transfers, transfer_list) { |
Heiner Kallweit | cce7e3a | 2016-09-07 22:54:18 +0200 | [diff] [blame] | 155 | if (t->tx_buf) |
Heiner Kallweit | 923ab15 | 2016-10-02 14:22:57 +0200 | [diff] [blame] | 156 | fsl_espi_memcpy_swab(buf, t->tx_buf, m, t); |
Heiner Kallweit | aca7515 | 2016-11-09 22:58:01 +0100 | [diff] [blame] | 157 | /* In RXSKIP mode controller shifts out zeros internally */ |
| 158 | else if (!mspi->rxskip) |
Heiner Kallweit | 7c159aa | 2016-09-07 22:50:53 +0200 | [diff] [blame] | 159 | memset(buf, 0, t->len); |
Heiner Kallweit | 7c159aa | 2016-09-07 22:50:53 +0200 | [diff] [blame] | 160 | buf += t->len; |
| 161 | } |
Heiner Kallweit | cce7e3a | 2016-09-07 22:54:18 +0200 | [diff] [blame] | 162 | } |
Heiner Kallweit | 7c159aa | 2016-09-07 22:50:53 +0200 | [diff] [blame] | 163 | |
Heiner Kallweit | cce7e3a | 2016-09-07 22:54:18 +0200 | [diff] [blame] | 164 | static void fsl_espi_copy_from_buf(struct spi_message *m, |
| 165 | struct mpc8xxx_spi *mspi) |
| 166 | { |
| 167 | struct spi_transfer *t; |
| 168 | u8 *buf = mspi->local_buf; |
| 169 | |
| 170 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 171 | if (t->rx_buf) |
Heiner Kallweit | 923ab15 | 2016-10-02 14:22:57 +0200 | [diff] [blame] | 172 | fsl_espi_memcpy_swab(t->rx_buf, buf, m, t); |
Heiner Kallweit | cce7e3a | 2016-09-07 22:54:18 +0200 | [diff] [blame] | 173 | buf += t->len; |
| 174 | } |
Heiner Kallweit | 7c159aa | 2016-09-07 22:50:53 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Heiner Kallweit | d3152cf1 | 2016-09-07 22:53:38 +0200 | [diff] [blame] | 177 | static int fsl_espi_check_message(struct spi_message *m) |
| 178 | { |
| 179 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); |
| 180 | struct spi_transfer *t, *first; |
| 181 | |
| 182 | if (m->frame_length > SPCOM_TRANLEN_MAX) { |
| 183 | dev_err(mspi->dev, "message too long, size is %u bytes\n", |
| 184 | m->frame_length); |
| 185 | return -EMSGSIZE; |
| 186 | } |
| 187 | |
| 188 | first = list_first_entry(&m->transfers, struct spi_transfer, |
| 189 | transfer_list); |
Heiner Kallweit | e4be705 | 2016-10-02 14:22:35 +0200 | [diff] [blame] | 190 | |
Heiner Kallweit | d3152cf1 | 2016-09-07 22:53:38 +0200 | [diff] [blame] | 191 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 192 | if (first->bits_per_word != t->bits_per_word || |
| 193 | first->speed_hz != t->speed_hz) { |
| 194 | dev_err(mspi->dev, "bits_per_word/speed_hz should be the same for all transfers\n"); |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | } |
| 198 | |
Heiner Kallweit | e4be705 | 2016-10-02 14:22:35 +0200 | [diff] [blame] | 199 | /* ESPI supports MSB-first transfers for word size 8 / 16 only */ |
| 200 | if (!(m->spi->mode & SPI_LSB_FIRST) && first->bits_per_word != 8 && |
| 201 | first->bits_per_word != 16) { |
| 202 | dev_err(mspi->dev, |
| 203 | "MSB-first transfer not supported for wordsize %u\n", |
| 204 | first->bits_per_word); |
| 205 | return -EINVAL; |
| 206 | } |
| 207 | |
Heiner Kallweit | d3152cf1 | 2016-09-07 22:53:38 +0200 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
Heiner Kallweit | aca7515 | 2016-11-09 22:58:01 +0100 | [diff] [blame] | 211 | static unsigned int fsl_espi_check_rxskip_mode(struct spi_message *m) |
| 212 | { |
| 213 | struct spi_transfer *t; |
| 214 | unsigned int i = 0, rxskip = 0; |
| 215 | |
| 216 | /* |
| 217 | * prerequisites for ESPI rxskip mode: |
| 218 | * - message has two transfers |
| 219 | * - first transfer is a write and second is a read |
| 220 | * |
| 221 | * In addition the current low-level transfer mechanism requires |
| 222 | * that the rxskip bytes fit into the TX FIFO. Else the transfer |
| 223 | * would hang because after the first FSL_ESPI_FIFO_SIZE bytes |
| 224 | * the TX FIFO isn't re-filled. |
| 225 | */ |
| 226 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 227 | if (i == 0) { |
| 228 | if (!t->tx_buf || t->rx_buf || |
| 229 | t->len > FSL_ESPI_FIFO_SIZE) |
| 230 | return 0; |
| 231 | rxskip = t->len; |
| 232 | } else if (i == 1) { |
| 233 | if (t->tx_buf || !t->rx_buf) |
| 234 | return 0; |
| 235 | } |
| 236 | i++; |
| 237 | } |
| 238 | |
| 239 | return i == 2 ? rxskip : 0; |
| 240 | } |
| 241 | |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 242 | static void fsl_espi_fill_tx_fifo(struct mpc8xxx_spi *mspi, u32 events) |
| 243 | { |
| 244 | u32 tx_fifo_avail; |
| 245 | |
| 246 | /* if events is zero transfer has not started and tx fifo is empty */ |
| 247 | tx_fifo_avail = events ? SPIE_TXCNT(events) : FSL_ESPI_FIFO_SIZE; |
| 248 | |
| 249 | while (tx_fifo_avail >= min(4U, mspi->tx_len) && mspi->tx_len) |
| 250 | if (mspi->tx_len >= 4) { |
| 251 | fsl_espi_write_reg(mspi, ESPI_SPITF, *(u32 *)mspi->tx); |
| 252 | mspi->tx += 4; |
| 253 | mspi->tx_len -= 4; |
| 254 | tx_fifo_avail -= 4; |
| 255 | } else { |
| 256 | fsl_espi_write_reg8(mspi, ESPI_SPITF, *(u8 *)mspi->tx); |
| 257 | mspi->tx += 1; |
| 258 | mspi->tx_len -= 1; |
| 259 | tx_fifo_avail -= 1; |
| 260 | } |
| 261 | } |
| 262 | |
Heiner Kallweit | f05689a | 2016-10-27 21:27:35 +0200 | [diff] [blame] | 263 | static void fsl_espi_read_rx_fifo(struct mpc8xxx_spi *mspi, u32 events) |
| 264 | { |
| 265 | u32 rx_fifo_avail = SPIE_RXCNT(events); |
| 266 | |
| 267 | while (rx_fifo_avail >= min(4U, mspi->rx_len) && mspi->rx_len) |
| 268 | if (mspi->rx_len >= 4) { |
| 269 | *(u32 *)mspi->rx = fsl_espi_read_reg(mspi, ESPI_SPIRF); |
| 270 | mspi->rx += 4; |
| 271 | mspi->rx_len -= 4; |
| 272 | rx_fifo_avail -= 4; |
| 273 | } else { |
| 274 | *(u8 *)mspi->rx = fsl_espi_read_reg8(mspi, ESPI_SPIRF); |
| 275 | mspi->rx += 1; |
| 276 | mspi->rx_len -= 1; |
| 277 | rx_fifo_avail -= 1; |
| 278 | } |
| 279 | } |
| 280 | |
Heiner Kallweit | ea616ee | 2016-08-25 06:44:42 +0200 | [diff] [blame] | 281 | static void fsl_espi_setup_transfer(struct spi_device *spi, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 282 | struct spi_transfer *t) |
| 283 | { |
| 284 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Heiner Kallweit | d198ebf | 2016-09-13 23:15:45 +0200 | [diff] [blame] | 285 | int bits_per_word = t ? t->bits_per_word : spi->bits_per_word; |
Paulo Zaneti | 73aaf15 | 2016-10-29 11:02:19 +0200 | [diff] [blame] | 286 | u32 pm, hz = t ? t->speed_hz : spi->max_speed_hz; |
Heiner Kallweit | 219b5e3 | 2016-11-13 14:38:05 +0100 | [diff] [blame] | 287 | struct fsl_espi_cs *cs = spi_get_ctldata(spi); |
Heiner Kallweit | 8f3086d | 2016-11-04 21:01:12 +0100 | [diff] [blame] | 288 | u32 hw_mode_old = cs->hw_mode; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 289 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 290 | /* mask out bits we are going to set */ |
| 291 | cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF)); |
| 292 | |
Heiner Kallweit | a755af5 | 2016-09-04 09:56:57 +0200 | [diff] [blame] | 293 | cs->hw_mode |= CSMODE_LEN(bits_per_word - 1); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 294 | |
Paulo Zaneti | 73aaf15 | 2016-10-29 11:02:19 +0200 | [diff] [blame] | 295 | pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 4) - 1; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 296 | |
Paulo Zaneti | 73aaf15 | 2016-10-29 11:02:19 +0200 | [diff] [blame] | 297 | if (pm > 15) { |
| 298 | cs->hw_mode |= CSMODE_DIV16; |
| 299 | pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 16 * 4) - 1; |
| 300 | |
| 301 | WARN_ONCE(pm > 15, |
| 302 | "%s: Requested speed is too low: %u Hz. Will use %u Hz instead.\n", |
| 303 | dev_name(&spi->dev), hz, |
| 304 | mpc8xxx_spi->spibrg / (4 * 16 * (15 + 1))); |
| 305 | if (pm > 15) |
| 306 | pm = 15; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 307 | } |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 308 | |
| 309 | cs->hw_mode |= CSMODE_PM(pm); |
| 310 | |
Heiner Kallweit | 8f3086d | 2016-11-04 21:01:12 +0100 | [diff] [blame] | 311 | /* don't write the mode register if the mode doesn't change */ |
| 312 | if (cs->hw_mode != hw_mode_old) |
| 313 | fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(spi->chip_select), |
| 314 | cs->hw_mode); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 315 | } |
| 316 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 317 | static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t) |
| 318 | { |
| 319 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Heiner Kallweit | aca7515 | 2016-11-09 22:58:01 +0100 | [diff] [blame] | 320 | u32 mask, spcom; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 321 | int ret; |
| 322 | |
Heiner Kallweit | f895e27 | 2016-10-27 21:26:08 +0200 | [diff] [blame] | 323 | mpc8xxx_spi->rx_len = t->len; |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 324 | mpc8xxx_spi->tx_len = t->len; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 325 | |
| 326 | mpc8xxx_spi->tx = t->tx_buf; |
| 327 | mpc8xxx_spi->rx = t->rx_buf; |
| 328 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 329 | reinit_completion(&mpc8xxx_spi->done); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 330 | |
| 331 | /* Set SPCOM[CS] and SPCOM[TRANLEN] field */ |
Heiner Kallweit | aca7515 | 2016-11-09 22:58:01 +0100 | [diff] [blame] | 332 | spcom = SPCOM_CS(spi->chip_select); |
| 333 | spcom |= SPCOM_TRANLEN(t->len - 1); |
| 334 | |
| 335 | /* configure RXSKIP mode */ |
| 336 | if (mpc8xxx_spi->rxskip) { |
| 337 | spcom |= SPCOM_RXSKIP(mpc8xxx_spi->rxskip); |
| 338 | mpc8xxx_spi->tx_len = mpc8xxx_spi->rxskip; |
| 339 | mpc8xxx_spi->rx_len = t->len - mpc8xxx_spi->rxskip; |
| 340 | mpc8xxx_spi->rx = t->rx_buf + mpc8xxx_spi->rxskip; |
Heiner Kallweit | 8263cb3 | 2016-11-09 22:58:34 +0100 | [diff] [blame] | 341 | if (t->rx_nbits == SPI_NBITS_DUAL) |
| 342 | spcom |= SPCOM_DO; |
Heiner Kallweit | aca7515 | 2016-11-09 22:58:01 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, spcom); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 346 | |
Heiner Kallweit | e508cea | 2016-10-27 21:27:56 +0200 | [diff] [blame] | 347 | /* enable interrupts */ |
| 348 | mask = SPIM_DON; |
| 349 | if (mpc8xxx_spi->rx_len > FSL_ESPI_FIFO_SIZE) |
| 350 | mask |= SPIM_RXT; |
| 351 | fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, mask); |
Heiner Kallweit | 5bcc6a2 | 2016-09-07 22:53:01 +0200 | [diff] [blame] | 352 | |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 353 | /* Prevent filling the fifo from getting interrupted */ |
| 354 | spin_lock_irq(&mpc8xxx_spi->lock); |
| 355 | fsl_espi_fill_tx_fifo(mpc8xxx_spi, 0); |
| 356 | spin_unlock_irq(&mpc8xxx_spi->lock); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 357 | |
Nobuteru Hayashi | aa70e56 | 2016-03-18 11:35:21 +0000 | [diff] [blame] | 358 | /* Won't hang up forever, SPI bus sometimes got lost interrupts... */ |
| 359 | ret = wait_for_completion_timeout(&mpc8xxx_spi->done, 2 * HZ); |
| 360 | if (ret == 0) |
| 361 | dev_err(mpc8xxx_spi->dev, |
Heiner Kallweit | db1b049 | 2016-10-27 21:28:02 +0200 | [diff] [blame] | 362 | "Transaction hanging up (left %u tx bytes, %u rx bytes)\n", |
| 363 | mpc8xxx_spi->tx_len, mpc8xxx_spi->rx_len); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 364 | |
| 365 | /* disable rx ints */ |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 366 | fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 367 | |
Heiner Kallweit | db1b049 | 2016-10-27 21:28:02 +0200 | [diff] [blame] | 368 | return ret == 0 ? -ETIMEDOUT : 0; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 369 | } |
| 370 | |
Heiner Kallweit | 38d003f | 2016-09-07 22:54:51 +0200 | [diff] [blame] | 371 | static int fsl_espi_trans(struct spi_message *m, struct spi_transfer *trans) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 372 | { |
Heiner Kallweit | 38d003f | 2016-09-07 22:54:51 +0200 | [diff] [blame] | 373 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 374 | struct spi_device *spi = m->spi; |
Heiner Kallweit | 38d003f | 2016-09-07 22:54:51 +0200 | [diff] [blame] | 375 | int ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 376 | |
Heiner Kallweit | aca7515 | 2016-11-09 22:58:01 +0100 | [diff] [blame] | 377 | mspi->rxskip = fsl_espi_check_rxskip_mode(m); |
Heiner Kallweit | 8263cb3 | 2016-11-09 22:58:34 +0100 | [diff] [blame] | 378 | if (trans->rx_nbits == SPI_NBITS_DUAL && !mspi->rxskip) { |
| 379 | dev_err(mspi->dev, "Dual output mode requires RXSKIP mode!\n"); |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
Heiner Kallweit | 38d003f | 2016-09-07 22:54:51 +0200 | [diff] [blame] | 383 | fsl_espi_copy_to_buf(m, mspi); |
Heiner Kallweit | faceef3 | 2016-09-07 22:52:06 +0200 | [diff] [blame] | 384 | fsl_espi_setup_transfer(spi, trans); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 385 | |
Heiner Kallweit | 06af115 | 2016-09-07 22:54:35 +0200 | [diff] [blame] | 386 | ret = fsl_espi_bufs(spi, trans); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 387 | |
Heiner Kallweit | faceef3 | 2016-09-07 22:52:06 +0200 | [diff] [blame] | 388 | if (trans->delay_usecs) |
| 389 | udelay(trans->delay_usecs); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 390 | |
Heiner Kallweit | cce7e3a | 2016-09-07 22:54:18 +0200 | [diff] [blame] | 391 | if (!ret) |
| 392 | fsl_espi_copy_from_buf(m, mspi); |
Heiner Kallweit | e33a3ad | 2016-09-07 22:51:10 +0200 | [diff] [blame] | 393 | |
| 394 | return ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 395 | } |
| 396 | |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 397 | static int fsl_espi_do_one_msg(struct spi_master *master, |
| 398 | struct spi_message *m) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 399 | { |
Heiner Kallweit | 96361faf | 2016-09-07 22:54:00 +0200 | [diff] [blame] | 400 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); |
Heiner Kallweit | 8263cb3 | 2016-11-09 22:58:34 +0100 | [diff] [blame] | 401 | unsigned int delay_usecs = 0, rx_nbits = 0; |
Heiner Kallweit | faceef3 | 2016-09-07 22:52:06 +0200 | [diff] [blame] | 402 | struct spi_transfer *t, trans = {}; |
Heiner Kallweit | e33a3ad | 2016-09-07 22:51:10 +0200 | [diff] [blame] | 403 | int ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 404 | |
Heiner Kallweit | d3152cf1 | 2016-09-07 22:53:38 +0200 | [diff] [blame] | 405 | ret = fsl_espi_check_message(m); |
| 406 | if (ret) |
| 407 | goto out; |
| 408 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 409 | list_for_each_entry(t, &m->transfers, transfer_list) { |
Heiner Kallweit | 96361faf | 2016-09-07 22:54:00 +0200 | [diff] [blame] | 410 | if (t->delay_usecs > delay_usecs) |
| 411 | delay_usecs = t->delay_usecs; |
Heiner Kallweit | 8263cb3 | 2016-11-09 22:58:34 +0100 | [diff] [blame] | 412 | if (t->rx_nbits > rx_nbits) |
| 413 | rx_nbits = t->rx_nbits; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 414 | } |
| 415 | |
Heiner Kallweit | 96361faf | 2016-09-07 22:54:00 +0200 | [diff] [blame] | 416 | t = list_first_entry(&m->transfers, struct spi_transfer, |
| 417 | transfer_list); |
| 418 | |
Heiner Kallweit | 06af115 | 2016-09-07 22:54:35 +0200 | [diff] [blame] | 419 | trans.len = m->frame_length; |
Heiner Kallweit | 96361faf | 2016-09-07 22:54:00 +0200 | [diff] [blame] | 420 | trans.speed_hz = t->speed_hz; |
| 421 | trans.bits_per_word = t->bits_per_word; |
| 422 | trans.delay_usecs = delay_usecs; |
| 423 | trans.tx_buf = mspi->local_buf; |
| 424 | trans.rx_buf = mspi->local_buf; |
Heiner Kallweit | 8263cb3 | 2016-11-09 22:58:34 +0100 | [diff] [blame] | 425 | trans.rx_nbits = rx_nbits; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 426 | |
Heiner Kallweit | 06af115 | 2016-09-07 22:54:35 +0200 | [diff] [blame] | 427 | if (trans.len) |
| 428 | ret = fsl_espi_trans(m, &trans); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 429 | |
Heiner Kallweit | faceef3 | 2016-09-07 22:52:06 +0200 | [diff] [blame] | 430 | m->actual_length = ret ? 0 : trans.len; |
Heiner Kallweit | d3152cf1 | 2016-09-07 22:53:38 +0200 | [diff] [blame] | 431 | out: |
Heiner Kallweit | 0319d49 | 2016-09-07 22:51:29 +0200 | [diff] [blame] | 432 | if (m->status == -EINPROGRESS) |
| 433 | m->status = ret; |
| 434 | |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 435 | spi_finalize_current_message(master); |
Heiner Kallweit | 0319d49 | 2016-09-07 22:51:29 +0200 | [diff] [blame] | 436 | |
| 437 | return ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static int fsl_espi_setup(struct spi_device *spi) |
| 441 | { |
| 442 | struct mpc8xxx_spi *mpc8xxx_spi; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 443 | u32 loop_mode; |
Heiner Kallweit | 219b5e3 | 2016-11-13 14:38:05 +0100 | [diff] [blame] | 444 | struct fsl_espi_cs *cs = spi_get_ctldata(spi); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 445 | |
| 446 | if (!spi->max_speed_hz) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | if (!cs) { |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 450 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 451 | if (!cs) |
| 452 | return -ENOMEM; |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 453 | spi_set_ctldata(spi, cs); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 457 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 458 | pm_runtime_get_sync(mpc8xxx_spi->dev); |
| 459 | |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 460 | cs->hw_mode = fsl_espi_read_reg(mpc8xxx_spi, |
| 461 | ESPI_SPMODEx(spi->chip_select)); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 462 | /* mask out bits we are going to set */ |
| 463 | cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH |
| 464 | | CSMODE_REV); |
| 465 | |
| 466 | if (spi->mode & SPI_CPHA) |
| 467 | cs->hw_mode |= CSMODE_CP_BEGIN_EDGECLK; |
| 468 | if (spi->mode & SPI_CPOL) |
| 469 | cs->hw_mode |= CSMODE_CI_INACTIVEHIGH; |
| 470 | if (!(spi->mode & SPI_LSB_FIRST)) |
| 471 | cs->hw_mode |= CSMODE_REV; |
| 472 | |
| 473 | /* Handle the loop mode */ |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 474 | loop_mode = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 475 | loop_mode &= ~SPMODE_LOOP; |
| 476 | if (spi->mode & SPI_LOOP) |
| 477 | loop_mode |= SPMODE_LOOP; |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 478 | fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, loop_mode); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 479 | |
Heiner Kallweit | ea616ee | 2016-08-25 06:44:42 +0200 | [diff] [blame] | 480 | fsl_espi_setup_transfer(spi, NULL); |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 481 | |
| 482 | pm_runtime_mark_last_busy(mpc8xxx_spi->dev); |
| 483 | pm_runtime_put_autosuspend(mpc8xxx_spi->dev); |
| 484 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 488 | static void fsl_espi_cleanup(struct spi_device *spi) |
| 489 | { |
Heiner Kallweit | 219b5e3 | 2016-11-13 14:38:05 +0100 | [diff] [blame] | 490 | struct fsl_espi_cs *cs = spi_get_ctldata(spi); |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 491 | |
| 492 | kfree(cs); |
| 493 | spi_set_ctldata(spi, NULL); |
| 494 | } |
| 495 | |
Heiner Kallweit | 10ed1e6 | 2016-08-25 06:45:16 +0200 | [diff] [blame] | 496 | static void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 497 | { |
Heiner Kallweit | f05689a | 2016-10-27 21:27:35 +0200 | [diff] [blame] | 498 | if (mspi->rx_len) |
| 499 | fsl_espi_read_rx_fifo(mspi, events); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 500 | |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 501 | if (mspi->tx_len) |
| 502 | fsl_espi_fill_tx_fifo(mspi, events); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 503 | |
Heiner Kallweit | db1b049 | 2016-10-27 21:28:02 +0200 | [diff] [blame] | 504 | if (mspi->tx_len || mspi->rx_len) |
| 505 | return; |
| 506 | |
| 507 | /* we're done, but check for errors before returning */ |
| 508 | events = fsl_espi_read_reg(mspi, ESPI_SPIE); |
| 509 | |
| 510 | if (!(events & SPIE_DON)) |
| 511 | dev_err(mspi->dev, |
| 512 | "Transfer done but SPIE_DON isn't set!\n"); |
| 513 | |
| 514 | if (SPIE_RXCNT(events) || SPIE_TXCNT(events) != FSL_ESPI_FIFO_SIZE) |
| 515 | dev_err(mspi->dev, "Transfer done but rx/tx fifo's aren't empty!\n"); |
| 516 | |
| 517 | complete(&mspi->done); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static irqreturn_t fsl_espi_irq(s32 irq, void *context_data) |
| 521 | { |
| 522 | struct mpc8xxx_spi *mspi = context_data; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 523 | u32 events; |
| 524 | |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 525 | spin_lock(&mspi->lock); |
| 526 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 527 | /* Get interrupt events(tx/rx) */ |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 528 | events = fsl_espi_read_reg(mspi, ESPI_SPIE); |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 529 | if (!events) { |
Heiner Kallweit | 66b8053 | 2016-10-29 10:53:19 +0200 | [diff] [blame] | 530 | spin_unlock(&mspi->lock); |
Heiner Kallweit | 35f5d71 | 2016-09-13 23:15:57 +0200 | [diff] [blame] | 531 | return IRQ_NONE; |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 532 | } |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 533 | |
| 534 | dev_vdbg(mspi->dev, "%s: events %x\n", __func__, events); |
| 535 | |
| 536 | fsl_espi_cpu_irq(mspi, events); |
| 537 | |
Heiner Kallweit | 35f5d71 | 2016-09-13 23:15:57 +0200 | [diff] [blame] | 538 | /* Clear the events */ |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 539 | fsl_espi_write_reg(mspi, ESPI_SPIE, events); |
Heiner Kallweit | 35f5d71 | 2016-09-13 23:15:57 +0200 | [diff] [blame] | 540 | |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 541 | spin_unlock(&mspi->lock); |
| 542 | |
Heiner Kallweit | 35f5d71 | 2016-09-13 23:15:57 +0200 | [diff] [blame] | 543 | return IRQ_HANDLED; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 544 | } |
| 545 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 546 | #ifdef CONFIG_PM |
| 547 | static int fsl_espi_runtime_suspend(struct device *dev) |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 548 | { |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 549 | struct spi_master *master = dev_get_drvdata(dev); |
| 550 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 551 | u32 regval; |
| 552 | |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 553 | regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE); |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 554 | regval &= ~SPMODE_ENABLE; |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 555 | fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval); |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 560 | static int fsl_espi_runtime_resume(struct device *dev) |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 561 | { |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 562 | struct spi_master *master = dev_get_drvdata(dev); |
| 563 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 564 | u32 regval; |
| 565 | |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 566 | regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE); |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 567 | regval |= SPMODE_ENABLE; |
Heiner Kallweit | 46afd38 | 2016-09-13 23:16:02 +0200 | [diff] [blame] | 568 | fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval); |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 572 | #endif |
Heiner Kallweit | 75506d0 | 2014-12-03 07:56:19 +0100 | [diff] [blame] | 573 | |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 574 | static size_t fsl_espi_max_message_size(struct spi_device *spi) |
Michal Suchanek | b541eef | 2015-12-02 10:38:21 +0000 | [diff] [blame] | 575 | { |
| 576 | return SPCOM_TRANLEN_MAX; |
| 577 | } |
| 578 | |
Heiner Kallweit | 456c742 | 2016-11-13 14:40:18 +0100 | [diff] [blame^] | 579 | static void fsl_espi_init_regs(struct device *dev, bool initial) |
| 580 | { |
| 581 | struct spi_master *master = dev_get_drvdata(dev); |
| 582 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(master); |
| 583 | struct device_node *nc; |
| 584 | u32 csmode, cs, prop; |
| 585 | int ret; |
| 586 | |
| 587 | /* SPI controller initializations */ |
| 588 | fsl_espi_write_reg(mspi, ESPI_SPMODE, 0); |
| 589 | fsl_espi_write_reg(mspi, ESPI_SPIM, 0); |
| 590 | fsl_espi_write_reg(mspi, ESPI_SPCOM, 0); |
| 591 | fsl_espi_write_reg(mspi, ESPI_SPIE, 0xffffffff); |
| 592 | |
| 593 | /* Init eSPI CS mode register */ |
| 594 | for_each_available_child_of_node(master->dev.of_node, nc) { |
| 595 | /* get chip select */ |
| 596 | ret = of_property_read_u32(nc, "reg", &cs); |
| 597 | if (ret || cs >= master->num_chipselect) |
| 598 | continue; |
| 599 | |
| 600 | csmode = CSMODE_INIT_VAL; |
| 601 | |
| 602 | /* check if CSBEF is set in device tree */ |
| 603 | ret = of_property_read_u32(nc, "fsl,csbef", &prop); |
| 604 | if (!ret) { |
| 605 | csmode &= ~(CSMODE_BEF(0xf)); |
| 606 | csmode |= CSMODE_BEF(prop); |
| 607 | } |
| 608 | |
| 609 | /* check if CSAFT is set in device tree */ |
| 610 | ret = of_property_read_u32(nc, "fsl,csaft", &prop); |
| 611 | if (!ret) { |
| 612 | csmode &= ~(CSMODE_AFT(0xf)); |
| 613 | csmode |= CSMODE_AFT(prop); |
| 614 | } |
| 615 | |
| 616 | fsl_espi_write_reg(mspi, ESPI_SPMODEx(cs), csmode); |
| 617 | |
| 618 | if (initial) |
| 619 | dev_info(dev, "cs=%u, init_csmode=0x%x\n", cs, csmode); |
| 620 | } |
| 621 | |
| 622 | /* Enable SPI interface */ |
| 623 | fsl_espi_write_reg(mspi, ESPI_SPMODE, SPMODE_INIT_VAL | SPMODE_ENABLE); |
| 624 | } |
| 625 | |
Heiner Kallweit | 604042a | 2016-09-17 15:43:31 +0200 | [diff] [blame] | 626 | static int fsl_espi_probe(struct device *dev, struct resource *mem, |
Heiner Kallweit | 7454346 | 2016-11-13 14:36:39 +0100 | [diff] [blame] | 627 | unsigned int irq, unsigned int num_cs) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 628 | { |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 629 | struct spi_master *master; |
| 630 | struct mpc8xxx_spi *mpc8xxx_spi; |
Heiner Kallweit | b497eb0 | 2016-10-01 21:07:52 +0200 | [diff] [blame] | 631 | int ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 632 | |
| 633 | master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi)); |
Heiner Kallweit | 604042a | 2016-09-17 15:43:31 +0200 | [diff] [blame] | 634 | if (!master) |
| 635 | return -ENOMEM; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 636 | |
| 637 | dev_set_drvdata(dev, master); |
| 638 | |
Heiner Kallweit | 7cb5557 | 2016-11-13 14:37:41 +0100 | [diff] [blame] | 639 | master->mode_bits = SPI_RX_DUAL | SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | |
| 640 | SPI_LSB_FIRST | SPI_LOOP; |
| 641 | master->dev.of_node = dev->of_node; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 642 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 643 | master->setup = fsl_espi_setup; |
Axel Lin | d9f2674 | 2014-08-31 12:44:09 +0800 | [diff] [blame] | 644 | master->cleanup = fsl_espi_cleanup; |
Heiner Kallweit | c592bec | 2014-12-03 07:56:17 +0100 | [diff] [blame] | 645 | master->transfer_one_message = fsl_espi_do_one_msg; |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 646 | master->auto_runtime_pm = true; |
Heiner Kallweit | 02a595d | 2016-08-17 21:11:01 +0200 | [diff] [blame] | 647 | master->max_message_size = fsl_espi_max_message_size; |
Heiner Kallweit | 7454346 | 2016-11-13 14:36:39 +0100 | [diff] [blame] | 648 | master->num_chipselect = num_cs; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 649 | |
| 650 | mpc8xxx_spi = spi_master_get_devdata(master); |
Heiner Kallweit | 5473126 | 2016-10-27 21:25:58 +0200 | [diff] [blame] | 651 | spin_lock_init(&mpc8xxx_spi->lock); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 652 | |
Heiner Kallweit | 7cb5557 | 2016-11-13 14:37:41 +0100 | [diff] [blame] | 653 | mpc8xxx_spi->dev = dev; |
| 654 | mpc8xxx_spi->spibrg = fsl_get_sys_freq(); |
| 655 | if (mpc8xxx_spi->spibrg == -1) { |
| 656 | dev_err(dev, "Can't get sys frequency!\n"); |
| 657 | ret = -EINVAL; |
| 658 | goto err_probe; |
| 659 | } |
| 660 | |
| 661 | init_completion(&mpc8xxx_spi->done); |
| 662 | |
Heiner Kallweit | 1423877 | 2016-09-07 22:50:22 +0200 | [diff] [blame] | 663 | mpc8xxx_spi->local_buf = |
| 664 | devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL); |
| 665 | if (!mpc8xxx_spi->local_buf) { |
| 666 | ret = -ENOMEM; |
| 667 | goto err_probe; |
| 668 | } |
| 669 | |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 670 | mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem); |
Axel Lin | 37c5db7 | 2015-08-30 18:35:51 +0800 | [diff] [blame] | 671 | if (IS_ERR(mpc8xxx_spi->reg_base)) { |
| 672 | ret = PTR_ERR(mpc8xxx_spi->reg_base); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 673 | goto err_probe; |
| 674 | } |
| 675 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 676 | /* Register for SPI Interrupt */ |
Heiner Kallweit | cdb2f77 | 2016-11-13 14:36:47 +0100 | [diff] [blame] | 677 | ret = devm_request_irq(dev, irq, fsl_espi_irq, 0, "fsl_espi", |
| 678 | mpc8xxx_spi); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 679 | if (ret) |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 680 | goto err_probe; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 681 | |
Heiner Kallweit | 456c742 | 2016-11-13 14:40:18 +0100 | [diff] [blame^] | 682 | fsl_espi_init_regs(dev, true); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 683 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 684 | pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT); |
| 685 | pm_runtime_use_autosuspend(dev); |
| 686 | pm_runtime_set_active(dev); |
| 687 | pm_runtime_enable(dev); |
| 688 | pm_runtime_get_sync(dev); |
| 689 | |
Heiner Kallweit | 4178b6b | 2015-08-26 21:21:50 +0200 | [diff] [blame] | 690 | ret = devm_spi_register_master(dev, master); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 691 | if (ret < 0) |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 692 | goto err_pm; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 693 | |
Heiner Kallweit | cdb2f77 | 2016-11-13 14:36:47 +0100 | [diff] [blame] | 694 | dev_info(dev, "at 0x%p (irq = %u)\n", mpc8xxx_spi->reg_base, irq); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 695 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 696 | pm_runtime_mark_last_busy(dev); |
| 697 | pm_runtime_put_autosuspend(dev); |
| 698 | |
Heiner Kallweit | 604042a | 2016-09-17 15:43:31 +0200 | [diff] [blame] | 699 | return 0; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 700 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 701 | err_pm: |
| 702 | pm_runtime_put_noidle(dev); |
| 703 | pm_runtime_disable(dev); |
| 704 | pm_runtime_set_suspended(dev); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 705 | err_probe: |
| 706 | spi_master_put(master); |
Heiner Kallweit | 604042a | 2016-09-17 15:43:31 +0200 | [diff] [blame] | 707 | return ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | static int of_fsl_espi_get_chipselects(struct device *dev) |
| 711 | { |
| 712 | struct device_node *np = dev->of_node; |
Heiner Kallweit | b497eb0 | 2016-10-01 21:07:52 +0200 | [diff] [blame] | 713 | u32 num_cs; |
| 714 | int ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 715 | |
Heiner Kallweit | b497eb0 | 2016-10-01 21:07:52 +0200 | [diff] [blame] | 716 | ret = of_property_read_u32(np, "fsl,espi-num-chipselects", &num_cs); |
| 717 | if (ret) { |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 718 | dev_err(dev, "No 'fsl,espi-num-chipselects' property\n"); |
Heiner Kallweit | 7454346 | 2016-11-13 14:36:39 +0100 | [diff] [blame] | 719 | return 0; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 720 | } |
| 721 | |
Heiner Kallweit | 7454346 | 2016-11-13 14:36:39 +0100 | [diff] [blame] | 722 | return num_cs; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 723 | } |
| 724 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 725 | static int of_fsl_espi_probe(struct platform_device *ofdev) |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 726 | { |
| 727 | struct device *dev = &ofdev->dev; |
| 728 | struct device_node *np = ofdev->dev.of_node; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 729 | struct resource mem; |
Heiner Kallweit | 7454346 | 2016-11-13 14:36:39 +0100 | [diff] [blame] | 730 | unsigned int irq, num_cs; |
Heiner Kallweit | acf6921 | 2016-09-17 15:43:00 +0200 | [diff] [blame] | 731 | int ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 732 | |
Heiner Kallweit | e3ce4f4 | 2016-11-13 14:37:18 +0100 | [diff] [blame] | 733 | if (of_property_read_bool(np, "mode")) { |
| 734 | dev_err(dev, "mode property is not supported on ESPI!\n"); |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | |
Heiner Kallweit | 7454346 | 2016-11-13 14:36:39 +0100 | [diff] [blame] | 738 | num_cs = of_fsl_espi_get_chipselects(dev); |
| 739 | if (!num_cs) |
| 740 | return -EINVAL; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 741 | |
| 742 | ret = of_address_to_resource(np, 0, &mem); |
| 743 | if (ret) |
Heiner Kallweit | acf6921 | 2016-09-17 15:43:00 +0200 | [diff] [blame] | 744 | return ret; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 745 | |
Thierry Reding | f757849 | 2013-09-18 15:24:44 +0200 | [diff] [blame] | 746 | irq = irq_of_parse_and_map(np, 0); |
Heiner Kallweit | acf6921 | 2016-09-17 15:43:00 +0200 | [diff] [blame] | 747 | if (!irq) |
| 748 | return -EINVAL; |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 749 | |
Heiner Kallweit | 7454346 | 2016-11-13 14:36:39 +0100 | [diff] [blame] | 750 | return fsl_espi_probe(dev, &mem, irq, num_cs); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 751 | } |
| 752 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 753 | static int of_fsl_espi_remove(struct platform_device *dev) |
| 754 | { |
| 755 | pm_runtime_disable(&dev->dev); |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 760 | #ifdef CONFIG_PM_SLEEP |
| 761 | static int of_fsl_espi_suspend(struct device *dev) |
| 762 | { |
| 763 | struct spi_master *master = dev_get_drvdata(dev); |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 764 | int ret; |
| 765 | |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 766 | ret = spi_master_suspend(master); |
| 767 | if (ret) { |
| 768 | dev_warn(dev, "cannot suspend master\n"); |
| 769 | return ret; |
| 770 | } |
| 771 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 772 | ret = pm_runtime_force_suspend(dev); |
| 773 | if (ret < 0) |
| 774 | return ret; |
| 775 | |
| 776 | return 0; |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | static int of_fsl_espi_resume(struct device *dev) |
| 780 | { |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 781 | struct spi_master *master = dev_get_drvdata(dev); |
Heiner Kallweit | 456c742 | 2016-11-13 14:40:18 +0100 | [diff] [blame^] | 782 | int ret; |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 783 | |
Heiner Kallweit | 456c742 | 2016-11-13 14:40:18 +0100 | [diff] [blame^] | 784 | fsl_espi_init_regs(dev, false); |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 785 | |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 786 | ret = pm_runtime_force_resume(dev); |
| 787 | if (ret < 0) |
| 788 | return ret; |
| 789 | |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 790 | return spi_master_resume(master); |
| 791 | } |
| 792 | #endif /* CONFIG_PM_SLEEP */ |
| 793 | |
| 794 | static const struct dev_pm_ops espi_pm = { |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 795 | SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend, |
| 796 | fsl_espi_runtime_resume, NULL) |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 797 | SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume) |
| 798 | }; |
| 799 | |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 800 | static const struct of_device_id of_fsl_espi_match[] = { |
| 801 | { .compatible = "fsl,mpc8536-espi" }, |
| 802 | {} |
| 803 | }; |
| 804 | MODULE_DEVICE_TABLE(of, of_fsl_espi_match); |
| 805 | |
Grant Likely | 18d306d | 2011-02-22 21:02:43 -0700 | [diff] [blame] | 806 | static struct platform_driver fsl_espi_driver = { |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 807 | .driver = { |
| 808 | .name = "fsl_espi", |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 809 | .of_match_table = of_fsl_espi_match, |
Hou Zhiqiang | 714bb65 | 2013-12-12 12:53:52 +0800 | [diff] [blame] | 810 | .pm = &espi_pm, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 811 | }, |
| 812 | .probe = of_fsl_espi_probe, |
Heiner Kallweit | e9abb4d | 2015-08-26 21:21:55 +0200 | [diff] [blame] | 813 | .remove = of_fsl_espi_remove, |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 814 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 815 | module_platform_driver(fsl_espi_driver); |
Mingkai Hu | 8b60d6c | 2010-10-12 18:18:32 +0800 | [diff] [blame] | 816 | |
| 817 | MODULE_AUTHOR("Mingkai Hu"); |
| 818 | MODULE_DESCRIPTION("Enhanced Freescale SPI Driver"); |
| 819 | MODULE_LICENSE("GPL"); |