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