Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 3 | * Copyright (C) 2013, Intel Corporation |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
Jarkko Nikula | 8b136ba | 2015-10-28 15:13:41 +0200 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/ioport.h> |
| 21 | #include <linux/errno.h> |
Sachin Kamat | cbfd6a2 | 2013-04-08 15:49:33 +0530 | [diff] [blame] | 22 | #include <linux/err.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 24 | #include <linux/kernel.h> |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 25 | #include <linux/pci.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Sebastian Andrzej Siewior | 8348c25 | 2010-11-22 17:12:15 -0800 | [diff] [blame] | 27 | #include <linux/spi/pxa2xx_spi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 28 | #include <linux/spi/spi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 29 | #include <linux/delay.h> |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 30 | #include <linux/gpio.h> |
Mika Westerberg | 089bd46 | 2016-09-29 09:45:20 +0300 | [diff] [blame] | 31 | #include <linux/gpio/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 33 | #include <linux/clk.h> |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 34 | #include <linux/pm_runtime.h> |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 35 | #include <linux/acpi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 36 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 37 | #include "spi-pxa2xx.h" |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 38 | |
| 39 | MODULE_AUTHOR("Stephen Street"); |
Will Newton | 037cdaf | 2007-12-10 15:49:25 -0800 | [diff] [blame] | 40 | MODULE_DESCRIPTION("PXA2xx SSP SPI Controller"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 41 | MODULE_LICENSE("GPL"); |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 42 | MODULE_ALIAS("platform:pxa2xx-spi"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 43 | |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 44 | #define TIMOUT_DFLT 1000 |
| 45 | |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 46 | /* |
| 47 | * for testing SSCR1 changes that require SSP restart, basically |
| 48 | * everything except the service and interrupt enables, the pxa270 developer |
| 49 | * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this |
| 50 | * list, but the PXA255 dev man says all bits without really meaning the |
| 51 | * service and interrupt enables |
| 52 | */ |
| 53 | #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 54 | | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 55 | | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ |
| 56 | | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ |
| 57 | | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ |
| 58 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 59 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 60 | #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \ |
| 61 | | QUARK_X1000_SSCR1_EFWR \ |
| 62 | | QUARK_X1000_SSCR1_RFT \ |
| 63 | | QUARK_X1000_SSCR1_TFT \ |
| 64 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) |
| 65 | |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 66 | #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ |
| 67 | | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ |
| 68 | | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ |
| 69 | | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ |
| 70 | | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \ |
| 71 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) |
| 72 | |
Jarkko Nikula | 624ea72 | 2015-10-28 15:13:39 +0200 | [diff] [blame] | 73 | #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24) |
| 74 | #define LPSS_CS_CONTROL_SW_MODE BIT(0) |
| 75 | #define LPSS_CS_CONTROL_CS_HIGH BIT(1) |
Jarkko Nikula | 8b136ba | 2015-10-28 15:13:41 +0200 | [diff] [blame] | 76 | #define LPSS_CAPS_CS_EN_SHIFT 9 |
| 77 | #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT) |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 78 | |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 79 | struct lpss_config { |
| 80 | /* LPSS offset from drv_data->ioaddr */ |
| 81 | unsigned offset; |
| 82 | /* Register offsets from drv_data->lpss_base or -1 */ |
| 83 | int reg_general; |
| 84 | int reg_ssp; |
| 85 | int reg_cs_ctrl; |
Jarkko Nikula | 8b136ba | 2015-10-28 15:13:41 +0200 | [diff] [blame] | 86 | int reg_capabilities; |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 87 | /* FIFO thresholds */ |
| 88 | u32 rx_threshold; |
| 89 | u32 tx_threshold_lo; |
| 90 | u32 tx_threshold_hi; |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 91 | /* Chip select control */ |
| 92 | unsigned cs_sel_shift; |
| 93 | unsigned cs_sel_mask; |
Mika Westerberg | 30f3a6a | 2016-02-08 17:14:31 +0200 | [diff] [blame] | 94 | unsigned cs_num; |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /* Keep these sorted with enum pxa_ssp_type */ |
| 98 | static const struct lpss_config lpss_platforms[] = { |
| 99 | { /* LPSS_LPT_SSP */ |
| 100 | .offset = 0x800, |
| 101 | .reg_general = 0x08, |
| 102 | .reg_ssp = 0x0c, |
| 103 | .reg_cs_ctrl = 0x18, |
Jarkko Nikula | 8b136ba | 2015-10-28 15:13:41 +0200 | [diff] [blame] | 104 | .reg_capabilities = -1, |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 105 | .rx_threshold = 64, |
| 106 | .tx_threshold_lo = 160, |
| 107 | .tx_threshold_hi = 224, |
| 108 | }, |
| 109 | { /* LPSS_BYT_SSP */ |
| 110 | .offset = 0x400, |
| 111 | .reg_general = 0x08, |
| 112 | .reg_ssp = 0x0c, |
| 113 | .reg_cs_ctrl = 0x18, |
Jarkko Nikula | 8b136ba | 2015-10-28 15:13:41 +0200 | [diff] [blame] | 114 | .reg_capabilities = -1, |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 115 | .rx_threshold = 64, |
| 116 | .tx_threshold_lo = 160, |
| 117 | .tx_threshold_hi = 224, |
| 118 | }, |
Mika Westerberg | 30f3a6a | 2016-02-08 17:14:31 +0200 | [diff] [blame] | 119 | { /* LPSS_BSW_SSP */ |
| 120 | .offset = 0x400, |
| 121 | .reg_general = 0x08, |
| 122 | .reg_ssp = 0x0c, |
| 123 | .reg_cs_ctrl = 0x18, |
| 124 | .reg_capabilities = -1, |
| 125 | .rx_threshold = 64, |
| 126 | .tx_threshold_lo = 160, |
| 127 | .tx_threshold_hi = 224, |
| 128 | .cs_sel_shift = 2, |
| 129 | .cs_sel_mask = 1 << 2, |
| 130 | .cs_num = 2, |
| 131 | }, |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 132 | { /* LPSS_SPT_SSP */ |
| 133 | .offset = 0x200, |
| 134 | .reg_general = -1, |
| 135 | .reg_ssp = 0x20, |
| 136 | .reg_cs_ctrl = 0x24, |
Jarkko Nikula | 66ec246 | 2016-04-26 10:08:26 +0300 | [diff] [blame] | 137 | .reg_capabilities = -1, |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 138 | .rx_threshold = 1, |
| 139 | .tx_threshold_lo = 32, |
| 140 | .tx_threshold_hi = 56, |
| 141 | }, |
Jarkko Nikula | b7c08cf | 2015-10-28 15:13:42 +0200 | [diff] [blame] | 142 | { /* LPSS_BXT_SSP */ |
| 143 | .offset = 0x200, |
| 144 | .reg_general = -1, |
| 145 | .reg_ssp = 0x20, |
| 146 | .reg_cs_ctrl = 0x24, |
| 147 | .reg_capabilities = 0xfc, |
| 148 | .rx_threshold = 1, |
| 149 | .tx_threshold_lo = 16, |
| 150 | .tx_threshold_hi = 48, |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 151 | .cs_sel_shift = 8, |
| 152 | .cs_sel_mask = 3 << 8, |
Jarkko Nikula | b7c08cf | 2015-10-28 15:13:42 +0200 | [diff] [blame] | 153 | }, |
Jarkko Nikula | fc0b2ac | 2017-05-30 17:31:21 +0300 | [diff] [blame] | 154 | { /* LPSS_CNL_SSP */ |
| 155 | .offset = 0x200, |
| 156 | .reg_general = -1, |
| 157 | .reg_ssp = 0x20, |
| 158 | .reg_cs_ctrl = 0x24, |
| 159 | .reg_capabilities = 0xfc, |
| 160 | .rx_threshold = 1, |
| 161 | .tx_threshold_lo = 32, |
| 162 | .tx_threshold_hi = 56, |
| 163 | .cs_sel_shift = 8, |
| 164 | .cs_sel_mask = 3 << 8, |
| 165 | }, |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | static inline const struct lpss_config |
| 169 | *lpss_get_config(const struct driver_data *drv_data) |
| 170 | { |
| 171 | return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP]; |
| 172 | } |
| 173 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 174 | static bool is_lpss_ssp(const struct driver_data *drv_data) |
| 175 | { |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 176 | switch (drv_data->ssp_type) { |
| 177 | case LPSS_LPT_SSP: |
| 178 | case LPSS_BYT_SSP: |
Mika Westerberg | 30f3a6a | 2016-02-08 17:14:31 +0200 | [diff] [blame] | 179 | case LPSS_BSW_SSP: |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 180 | case LPSS_SPT_SSP: |
Jarkko Nikula | b7c08cf | 2015-10-28 15:13:42 +0200 | [diff] [blame] | 181 | case LPSS_BXT_SSP: |
Jarkko Nikula | fc0b2ac | 2017-05-30 17:31:21 +0300 | [diff] [blame] | 182 | case LPSS_CNL_SSP: |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 183 | return true; |
| 184 | default: |
| 185 | return false; |
| 186 | } |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 189 | static bool is_quark_x1000_ssp(const struct driver_data *drv_data) |
| 190 | { |
| 191 | return drv_data->ssp_type == QUARK_X1000_SSP; |
| 192 | } |
| 193 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 194 | static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data) |
| 195 | { |
| 196 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 197 | case QUARK_X1000_SSP: |
| 198 | return QUARK_X1000_SSCR1_CHANGE_MASK; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 199 | case CE4100_SSP: |
| 200 | return CE4100_SSCR1_CHANGE_MASK; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 201 | default: |
| 202 | return SSCR1_CHANGE_MASK; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static u32 |
| 207 | pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data) |
| 208 | { |
| 209 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 210 | case QUARK_X1000_SSP: |
| 211 | return RX_THRESH_QUARK_X1000_DFLT; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 212 | case CE4100_SSP: |
| 213 | return RX_THRESH_CE4100_DFLT; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 214 | default: |
| 215 | return RX_THRESH_DFLT; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data) |
| 220 | { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 221 | u32 mask; |
| 222 | |
| 223 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 224 | case QUARK_X1000_SSP: |
| 225 | mask = QUARK_X1000_SSSR_TFL_MASK; |
| 226 | break; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 227 | case CE4100_SSP: |
| 228 | mask = CE4100_SSSR_TFL_MASK; |
| 229 | break; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 230 | default: |
| 231 | mask = SSSR_TFL_MASK; |
| 232 | break; |
| 233 | } |
| 234 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 235 | return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data, |
| 239 | u32 *sccr1_reg) |
| 240 | { |
| 241 | u32 mask; |
| 242 | |
| 243 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 244 | case QUARK_X1000_SSP: |
| 245 | mask = QUARK_X1000_SSCR1_RFT; |
| 246 | break; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 247 | case CE4100_SSP: |
| 248 | mask = CE4100_SSCR1_RFT; |
| 249 | break; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 250 | default: |
| 251 | mask = SSCR1_RFT; |
| 252 | break; |
| 253 | } |
| 254 | *sccr1_reg &= ~mask; |
| 255 | } |
| 256 | |
| 257 | static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data, |
| 258 | u32 *sccr1_reg, u32 threshold) |
| 259 | { |
| 260 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 261 | case QUARK_X1000_SSP: |
| 262 | *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold); |
| 263 | break; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 264 | case CE4100_SSP: |
| 265 | *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold); |
| 266 | break; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 267 | default: |
| 268 | *sccr1_reg |= SSCR1_RxTresh(threshold); |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data, |
| 274 | u32 clk_div, u8 bits) |
| 275 | { |
| 276 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 277 | case QUARK_X1000_SSP: |
| 278 | return clk_div |
| 279 | | QUARK_X1000_SSCR0_Motorola |
| 280 | | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits) |
| 281 | | SSCR0_SSE; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 282 | default: |
| 283 | return clk_div |
| 284 | | SSCR0_Motorola |
| 285 | | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) |
| 286 | | SSCR0_SSE |
| 287 | | (bits > 16 ? SSCR0_EDSS : 0); |
| 288 | } |
| 289 | } |
| 290 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 291 | /* |
| 292 | * Read and write LPSS SSP private registers. Caller must first check that |
| 293 | * is_lpss_ssp() returns true before these can be called. |
| 294 | */ |
| 295 | static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset) |
| 296 | { |
| 297 | WARN_ON(!drv_data->lpss_base); |
| 298 | return readl(drv_data->lpss_base + offset); |
| 299 | } |
| 300 | |
| 301 | static void __lpss_ssp_write_priv(struct driver_data *drv_data, |
| 302 | unsigned offset, u32 value) |
| 303 | { |
| 304 | WARN_ON(!drv_data->lpss_base); |
| 305 | writel(value, drv_data->lpss_base + offset); |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * lpss_ssp_setup - perform LPSS SSP specific setup |
| 310 | * @drv_data: pointer to the driver private data |
| 311 | * |
| 312 | * Perform LPSS SSP specific setup. This function must be called first if |
| 313 | * one is going to use LPSS SSP private registers. |
| 314 | */ |
| 315 | static void lpss_ssp_setup(struct driver_data *drv_data) |
| 316 | { |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 317 | const struct lpss_config *config; |
| 318 | u32 value; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 319 | |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 320 | config = lpss_get_config(drv_data); |
| 321 | drv_data->lpss_base = drv_data->ioaddr + config->offset; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 322 | |
| 323 | /* Enable software chip select control */ |
Jarkko Nikula | 0e89721 | 2015-10-22 16:44:42 +0300 | [diff] [blame] | 324 | value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); |
Jarkko Nikula | 624ea72 | 2015-10-28 15:13:39 +0200 | [diff] [blame] | 325 | value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH); |
| 326 | value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH; |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 327 | __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); |
Mika Westerberg | 0054e28 | 2013-03-05 12:05:17 +0200 | [diff] [blame] | 328 | |
| 329 | /* Enable multiblock DMA transfers */ |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame] | 330 | if (drv_data->master_info->enable_dma) { |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 331 | __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1); |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame] | 332 | |
Jarkko Nikula | 82ba2c2 | 2015-06-04 16:55:12 +0300 | [diff] [blame] | 333 | if (config->reg_general >= 0) { |
| 334 | value = __lpss_ssp_read_priv(drv_data, |
| 335 | config->reg_general); |
Jarkko Nikula | 624ea72 | 2015-10-28 15:13:39 +0200 | [diff] [blame] | 336 | value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE; |
Jarkko Nikula | 82ba2c2 | 2015-06-04 16:55:12 +0300 | [diff] [blame] | 337 | __lpss_ssp_write_priv(drv_data, |
| 338 | config->reg_general, value); |
| 339 | } |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame] | 340 | } |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 343 | static void lpss_ssp_select_cs(struct spi_device *spi, |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 344 | const struct lpss_config *config) |
| 345 | { |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 346 | struct driver_data *drv_data = |
| 347 | spi_controller_get_devdata(spi->controller); |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 348 | u32 value, cs; |
| 349 | |
| 350 | if (!config->cs_sel_mask) |
| 351 | return; |
| 352 | |
| 353 | value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); |
| 354 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 355 | cs = spi->chip_select; |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 356 | cs <<= config->cs_sel_shift; |
| 357 | if (cs != (value & config->cs_sel_mask)) { |
| 358 | /* |
| 359 | * When switching another chip select output active the |
| 360 | * output must be selected first and wait 2 ssp_clk cycles |
| 361 | * before changing state to active. Otherwise a short |
| 362 | * glitch will occur on the previous chip select since |
| 363 | * output select is latched but state control is not. |
| 364 | */ |
| 365 | value &= ~config->cs_sel_mask; |
| 366 | value |= cs; |
| 367 | __lpss_ssp_write_priv(drv_data, |
| 368 | config->reg_cs_ctrl, value); |
| 369 | ndelay(1000000000 / |
| 370 | (drv_data->master->max_speed_hz / 2)); |
| 371 | } |
| 372 | } |
| 373 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 374 | static void lpss_ssp_cs_control(struct spi_device *spi, bool enable) |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 375 | { |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 376 | struct driver_data *drv_data = |
| 377 | spi_controller_get_devdata(spi->controller); |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 378 | const struct lpss_config *config; |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 379 | u32 value; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 380 | |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 381 | config = lpss_get_config(drv_data); |
| 382 | |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 383 | if (enable) |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 384 | lpss_ssp_select_cs(spi, config); |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 385 | |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 386 | value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 387 | if (enable) |
Jarkko Nikula | 624ea72 | 2015-10-28 15:13:39 +0200 | [diff] [blame] | 388 | value &= ~LPSS_CS_CONTROL_CS_HIGH; |
Mika Westerberg | c1e4a53 | 2016-02-08 17:14:30 +0200 | [diff] [blame] | 389 | else |
Jarkko Nikula | 624ea72 | 2015-10-28 15:13:39 +0200 | [diff] [blame] | 390 | value |= LPSS_CS_CONTROL_CS_HIGH; |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 391 | __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 394 | static void cs_assert(struct spi_device *spi) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 395 | { |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 396 | struct chip_data *chip = spi_get_ctldata(spi); |
| 397 | struct driver_data *drv_data = |
| 398 | spi_controller_get_devdata(spi->controller); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 399 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 400 | if (drv_data->ssp_type == CE4100_SSP) { |
Jarkko Nikula | 96579a4 | 2016-09-07 17:04:07 +0300 | [diff] [blame] | 401 | pxa2xx_spi_write(drv_data, SSSR, chip->frm); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 405 | if (chip->cs_control) { |
| 406 | chip->cs_control(PXA2XX_CS_ASSERT); |
| 407 | return; |
| 408 | } |
| 409 | |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 410 | if (chip->gpiod_cs) { |
| 411 | gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
Jarkko Nikula | 7566bcc | 2014-12-18 15:04:20 +0200 | [diff] [blame] | 415 | if (is_lpss_ssp(drv_data)) |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 416 | lpss_ssp_cs_control(spi, true); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 419 | static void cs_deassert(struct spi_device *spi) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 420 | { |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 421 | struct chip_data *chip = spi_get_ctldata(spi); |
| 422 | struct driver_data *drv_data = |
| 423 | spi_controller_get_devdata(spi->controller); |
Jarkko Nikula | 104e51a | 2018-02-09 16:31:07 +0200 | [diff] [blame] | 424 | unsigned long timeout; |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 425 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 426 | if (drv_data->ssp_type == CE4100_SSP) |
| 427 | return; |
| 428 | |
Jarkko Nikula | 104e51a | 2018-02-09 16:31:07 +0200 | [diff] [blame] | 429 | /* Wait until SSP becomes idle before deasserting the CS */ |
| 430 | timeout = jiffies + msecs_to_jiffies(10); |
| 431 | while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY && |
| 432 | !time_after(jiffies, timeout)) |
| 433 | cpu_relax(); |
| 434 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 435 | if (chip->cs_control) { |
Daniel Ribeiro | 2b2562d | 2009-04-08 22:48:03 -0300 | [diff] [blame] | 436 | chip->cs_control(PXA2XX_CS_DEASSERT); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 440 | if (chip->gpiod_cs) { |
| 441 | gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | |
Jarkko Nikula | 7566bcc | 2014-12-18 15:04:20 +0200 | [diff] [blame] | 445 | if (is_lpss_ssp(drv_data)) |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 446 | lpss_ssp_cs_control(spi, false); |
| 447 | } |
| 448 | |
| 449 | static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level) |
| 450 | { |
| 451 | if (level) |
| 452 | cs_deassert(spi); |
| 453 | else |
| 454 | cs_assert(spi); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 457 | int pxa2xx_spi_flush(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 458 | { |
| 459 | unsigned long limit = loops_per_jiffy << 1; |
| 460 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 461 | do { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 462 | while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 463 | pxa2xx_spi_read(drv_data, SSDR); |
| 464 | } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 465 | write_SSSR_CS(drv_data, SSSR_ROR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 466 | |
| 467 | return limit; |
| 468 | } |
| 469 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 470 | static int null_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 471 | { |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 472 | u8 n_bytes = drv_data->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 473 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 474 | if (pxa2xx_spi_txfifo_full(drv_data) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 475 | || (drv_data->tx == drv_data->tx_end)) |
| 476 | return 0; |
| 477 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 478 | pxa2xx_spi_write(drv_data, SSDR, 0); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 479 | drv_data->tx += n_bytes; |
| 480 | |
| 481 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 484 | static int null_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 485 | { |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 486 | u8 n_bytes = drv_data->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 487 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 488 | while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 489 | && (drv_data->rx < drv_data->rx_end)) { |
| 490 | pxa2xx_spi_read(drv_data, SSDR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 491 | drv_data->rx += n_bytes; |
| 492 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 493 | |
| 494 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 497 | static int u8_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 498 | { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 499 | if (pxa2xx_spi_txfifo_full(drv_data) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 500 | || (drv_data->tx == drv_data->tx_end)) |
| 501 | return 0; |
| 502 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 503 | pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx)); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 504 | ++drv_data->tx; |
| 505 | |
| 506 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 509 | static int u8_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 510 | { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 511 | while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 512 | && (drv_data->rx < drv_data->rx_end)) { |
| 513 | *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 514 | ++drv_data->rx; |
| 515 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 516 | |
| 517 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 520 | static int u16_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 521 | { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 522 | if (pxa2xx_spi_txfifo_full(drv_data) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 523 | || (drv_data->tx == drv_data->tx_end)) |
| 524 | return 0; |
| 525 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 526 | pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx)); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 527 | drv_data->tx += 2; |
| 528 | |
| 529 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 532 | static int u16_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 533 | { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 534 | while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 535 | && (drv_data->rx < drv_data->rx_end)) { |
| 536 | *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 537 | drv_data->rx += 2; |
| 538 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 539 | |
| 540 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 541 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 542 | |
| 543 | static int u32_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 544 | { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 545 | if (pxa2xx_spi_txfifo_full(drv_data) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 546 | || (drv_data->tx == drv_data->tx_end)) |
| 547 | return 0; |
| 548 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 549 | pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx)); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 550 | drv_data->tx += 4; |
| 551 | |
| 552 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 555 | static int u32_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 556 | { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 557 | while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 558 | && (drv_data->rx < drv_data->rx_end)) { |
| 559 | *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 560 | drv_data->rx += 4; |
| 561 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 562 | |
| 563 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 566 | static void reset_sccr1(struct driver_data *drv_data) |
| 567 | { |
Jarkko Nikula | 96579a4 | 2016-09-07 17:04:07 +0300 | [diff] [blame] | 568 | struct chip_data *chip = |
| 569 | spi_get_ctldata(drv_data->master->cur_msg->spi); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 570 | u32 sccr1_reg; |
| 571 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 572 | sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1; |
Andy Shevchenko | 152bc19 | 2016-07-06 12:08:11 +0300 | [diff] [blame] | 573 | switch (drv_data->ssp_type) { |
| 574 | case QUARK_X1000_SSP: |
| 575 | sccr1_reg &= ~QUARK_X1000_SSCR1_RFT; |
| 576 | break; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 577 | case CE4100_SSP: |
| 578 | sccr1_reg &= ~CE4100_SSCR1_RFT; |
| 579 | break; |
Andy Shevchenko | 152bc19 | 2016-07-06 12:08:11 +0300 | [diff] [blame] | 580 | default: |
| 581 | sccr1_reg &= ~SSCR1_RFT; |
| 582 | break; |
| 583 | } |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 584 | sccr1_reg |= chip->threshold; |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 585 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 588 | static void int_error_stop(struct driver_data *drv_data, const char* msg) |
| 589 | { |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 590 | /* Stop and reset SSP */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 591 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 592 | reset_sccr1(drv_data); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 593 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 594 | pxa2xx_spi_write(drv_data, SSTO, 0); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 595 | pxa2xx_spi_flush(drv_data); |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 596 | pxa2xx_spi_write(drv_data, SSCR0, |
| 597 | pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 598 | |
| 599 | dev_err(&drv_data->pdev->dev, "%s\n", msg); |
| 600 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 601 | drv_data->master->cur_msg->status = -EIO; |
| 602 | spi_finalize_current_transfer(drv_data->master); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static void int_transfer_complete(struct driver_data *drv_data) |
| 606 | { |
Jarkko Nikula | 07550df | 2016-02-04 12:30:56 +0200 | [diff] [blame] | 607 | /* Clear and disable interrupts */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 608 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 609 | reset_sccr1(drv_data); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 610 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 611 | pxa2xx_spi_write(drv_data, SSTO, 0); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 612 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 613 | spi_finalize_current_transfer(drv_data->master); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 616 | static irqreturn_t interrupt_transfer(struct driver_data *drv_data) |
| 617 | { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 618 | u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ? |
| 619 | drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 620 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 621 | u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 622 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 623 | if (irq_status & SSSR_ROR) { |
| 624 | int_error_stop(drv_data, "interrupt_transfer: fifo overrun"); |
| 625 | return IRQ_HANDLED; |
| 626 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 627 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 628 | if (irq_status & SSSR_TINT) { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 629 | pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 630 | if (drv_data->read(drv_data)) { |
| 631 | int_transfer_complete(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 632 | return IRQ_HANDLED; |
| 633 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 634 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 635 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 636 | /* Drain rx fifo, Fill tx fifo and prevent overruns */ |
| 637 | do { |
| 638 | if (drv_data->read(drv_data)) { |
| 639 | int_transfer_complete(drv_data); |
| 640 | return IRQ_HANDLED; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 641 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 642 | } while (drv_data->write(drv_data)); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 643 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 644 | if (drv_data->read(drv_data)) { |
| 645 | int_transfer_complete(drv_data); |
| 646 | return IRQ_HANDLED; |
| 647 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 648 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 649 | if (drv_data->tx == drv_data->tx_end) { |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 650 | u32 bytes_left; |
| 651 | u32 sccr1_reg; |
| 652 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 653 | sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 654 | sccr1_reg &= ~SSCR1_TIE; |
| 655 | |
| 656 | /* |
| 657 | * PXA25x_SSP has no timeout, set up rx threshould for the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 658 | * remaining RX bytes. |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 659 | */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 660 | if (pxa25x_ssp_comp(drv_data)) { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 661 | u32 rx_thre; |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 662 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 663 | pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 664 | |
| 665 | bytes_left = drv_data->rx_end - drv_data->rx; |
| 666 | switch (drv_data->n_bytes) { |
| 667 | case 4: |
| 668 | bytes_left >>= 1; |
| 669 | case 2: |
| 670 | bytes_left >>= 1; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 671 | } |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 672 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 673 | rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data); |
| 674 | if (rx_thre > bytes_left) |
| 675 | rx_thre = bytes_left; |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 676 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 677 | pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 678 | } |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 679 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 682 | /* We did something */ |
| 683 | return IRQ_HANDLED; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Jan Kiszka | b031248 | 2017-01-16 19:44:54 +0100 | [diff] [blame] | 686 | static void handle_bad_msg(struct driver_data *drv_data) |
| 687 | { |
| 688 | pxa2xx_spi_write(drv_data, SSCR0, |
| 689 | pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); |
| 690 | pxa2xx_spi_write(drv_data, SSCR1, |
| 691 | pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1); |
| 692 | if (!pxa25x_ssp_comp(drv_data)) |
| 693 | pxa2xx_spi_write(drv_data, SSTO, 0); |
| 694 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
| 695 | |
| 696 | dev_err(&drv_data->pdev->dev, |
| 697 | "bad message state in interrupt handler\n"); |
| 698 | } |
| 699 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 700 | static irqreturn_t ssp_int(int irq, void *dev_id) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 701 | { |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 702 | struct driver_data *drv_data = dev_id; |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 703 | u32 sccr1_reg; |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 704 | u32 mask = drv_data->mask_sr; |
| 705 | u32 status; |
| 706 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 707 | /* |
| 708 | * The IRQ might be shared with other peripherals so we must first |
| 709 | * check that are we RPM suspended or not. If we are we assume that |
| 710 | * the IRQ was not for us (we shouldn't be RPM suspended when the |
| 711 | * interrupt is enabled). |
| 712 | */ |
| 713 | if (pm_runtime_suspended(&drv_data->pdev->dev)) |
| 714 | return IRQ_NONE; |
| 715 | |
Mika Westerberg | 269e4a4 | 2013-09-04 13:37:43 +0300 | [diff] [blame] | 716 | /* |
| 717 | * If the device is not yet in RPM suspended state and we get an |
| 718 | * interrupt that is meant for another device, check if status bits |
| 719 | * are all set to one. That means that the device is already |
| 720 | * powered off. |
| 721 | */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 722 | status = pxa2xx_spi_read(drv_data, SSSR); |
Mika Westerberg | 269e4a4 | 2013-09-04 13:37:43 +0300 | [diff] [blame] | 723 | if (status == ~0) |
| 724 | return IRQ_NONE; |
| 725 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 726 | sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 727 | |
| 728 | /* Ignore possible writes if we don't need to write */ |
| 729 | if (!(sccr1_reg & SSCR1_TIE)) |
| 730 | mask &= ~SSSR_TFS; |
| 731 | |
Tan, Jui Nee | 02bc933 | 2015-09-01 10:22:51 +0800 | [diff] [blame] | 732 | /* Ignore RX timeout interrupt if it is disabled */ |
| 733 | if (!(sccr1_reg & SSCR1_TINTE)) |
| 734 | mask &= ~SSSR_TINT; |
| 735 | |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 736 | if (!(status & mask)) |
| 737 | return IRQ_NONE; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 738 | |
Jan Kiszka | e51e9b9 | 2017-01-21 10:06:38 +0100 | [diff] [blame] | 739 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1); |
| 740 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); |
| 741 | |
Jarkko Nikula | 4fc0caa | 2016-09-07 17:04:06 +0300 | [diff] [blame] | 742 | if (!drv_data->master->cur_msg) { |
Jan Kiszka | b031248 | 2017-01-16 19:44:54 +0100 | [diff] [blame] | 743 | handle_bad_msg(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 744 | /* Never fail */ |
| 745 | return IRQ_HANDLED; |
| 746 | } |
| 747 | |
| 748 | return drv_data->transfer_handler(drv_data); |
| 749 | } |
| 750 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 751 | /* |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 752 | * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply |
| 753 | * input frequency by fractions of 2^24. It also has a divider by 5. |
| 754 | * |
| 755 | * There are formulas to get baud rate value for given input frequency and |
| 756 | * divider parameters, such as DDS_CLK_RATE and SCR: |
| 757 | * |
| 758 | * Fsys = 200MHz |
| 759 | * |
| 760 | * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1) |
| 761 | * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2) |
| 762 | * |
| 763 | * DDS_CLK_RATE either 2^n or 2^n / 5. |
| 764 | * SCR is in range 0 .. 255 |
| 765 | * |
| 766 | * Divisor = 5^i * 2^j * 2 * k |
| 767 | * i = [0, 1] i = 1 iff j = 0 or j > 3 |
| 768 | * j = [0, 23] j = 0 iff i = 1 |
| 769 | * k = [1, 256] |
| 770 | * Special case: j = 0, i = 1: Divisor = 2 / 5 |
| 771 | * |
| 772 | * Accordingly to the specification the recommended values for DDS_CLK_RATE |
| 773 | * are: |
| 774 | * Case 1: 2^n, n = [0, 23] |
| 775 | * Case 2: 2^24 * 2 / 5 (0x666666) |
| 776 | * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333) |
| 777 | * |
| 778 | * In all cases the lowest possible value is better. |
| 779 | * |
| 780 | * The function calculates parameters for all cases and chooses the one closest |
| 781 | * to the asked baud rate. |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 782 | */ |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 783 | static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds) |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 784 | { |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 785 | unsigned long xtal = 200000000; |
| 786 | unsigned long fref = xtal / 2; /* mandatory division by 2, |
| 787 | see (2) */ |
| 788 | /* case 3 */ |
| 789 | unsigned long fref1 = fref / 2; /* case 1 */ |
| 790 | unsigned long fref2 = fref * 2 / 5; /* case 2 */ |
| 791 | unsigned long scale; |
| 792 | unsigned long q, q1, q2; |
| 793 | long r, r1, r2; |
| 794 | u32 mul; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 795 | |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 796 | /* Case 1 */ |
| 797 | |
| 798 | /* Set initial value for DDS_CLK_RATE */ |
| 799 | mul = (1 << 24) >> 1; |
| 800 | |
| 801 | /* Calculate initial quot */ |
Andy Shevchenko | 3ad4806 | 2015-10-13 17:09:14 +0300 | [diff] [blame] | 802 | q1 = DIV_ROUND_UP(fref1, rate); |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 803 | |
| 804 | /* Scale q1 if it's too big */ |
| 805 | if (q1 > 256) { |
| 806 | /* Scale q1 to range [1, 512] */ |
| 807 | scale = fls_long(q1 - 1); |
| 808 | if (scale > 9) { |
| 809 | q1 >>= scale - 9; |
| 810 | mul >>= scale - 9; |
| 811 | } |
| 812 | |
| 813 | /* Round the result if we have a remainder */ |
| 814 | q1 += q1 & 1; |
| 815 | } |
| 816 | |
| 817 | /* Decrease DDS_CLK_RATE as much as we can without loss in precision */ |
| 818 | scale = __ffs(q1); |
| 819 | q1 >>= scale; |
| 820 | mul >>= scale; |
| 821 | |
| 822 | /* Get the remainder */ |
| 823 | r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate); |
| 824 | |
| 825 | /* Case 2 */ |
| 826 | |
Andy Shevchenko | 3ad4806 | 2015-10-13 17:09:14 +0300 | [diff] [blame] | 827 | q2 = DIV_ROUND_UP(fref2, rate); |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 828 | r2 = abs(fref2 / q2 - rate); |
| 829 | |
| 830 | /* |
| 831 | * Choose the best between two: less remainder we have the better. We |
| 832 | * can't go case 2 if q2 is greater than 256 since SCR register can |
| 833 | * hold only values 0 .. 255. |
| 834 | */ |
| 835 | if (r2 >= r1 || q2 > 256) { |
| 836 | /* case 1 is better */ |
| 837 | r = r1; |
| 838 | q = q1; |
| 839 | } else { |
| 840 | /* case 2 is better */ |
| 841 | r = r2; |
| 842 | q = q2; |
| 843 | mul = (1 << 24) * 2 / 5; |
| 844 | } |
| 845 | |
Andy Shevchenko | 3ad4806 | 2015-10-13 17:09:14 +0300 | [diff] [blame] | 846 | /* Check case 3 only if the divisor is big enough */ |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 847 | if (fref / rate >= 80) { |
| 848 | u64 fssp; |
| 849 | u32 m; |
| 850 | |
| 851 | /* Calculate initial quot */ |
Andy Shevchenko | 3ad4806 | 2015-10-13 17:09:14 +0300 | [diff] [blame] | 852 | q1 = DIV_ROUND_UP(fref, rate); |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 853 | m = (1 << 24) / q1; |
| 854 | |
| 855 | /* Get the remainder */ |
| 856 | fssp = (u64)fref * m; |
| 857 | do_div(fssp, 1 << 24); |
| 858 | r1 = abs(fssp - rate); |
| 859 | |
| 860 | /* Choose this one if it suits better */ |
| 861 | if (r1 < r) { |
| 862 | /* case 3 is better */ |
| 863 | q = 1; |
| 864 | mul = m; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 868 | *dds = mul; |
| 869 | return q - 1; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 870 | } |
| 871 | |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 872 | static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate) |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 873 | { |
Jarkko Nikula | 0eca7cf | 2015-09-25 10:27:17 +0300 | [diff] [blame] | 874 | unsigned long ssp_clk = drv_data->master->max_speed_hz; |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 875 | const struct ssp_device *ssp = drv_data->ssp; |
| 876 | |
| 877 | rate = min_t(int, ssp_clk, rate); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 878 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 879 | if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP) |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 880 | return (ssp_clk / (2 * rate) - 1) & 0xff; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 881 | else |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 882 | return (ssp_clk / rate - 1) & 0xfff; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 883 | } |
| 884 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 885 | static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, |
Andy Shevchenko | d2c2f6a | 2015-10-22 16:44:40 +0300 | [diff] [blame] | 886 | int rate) |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 887 | { |
Jarkko Nikula | 96579a4 | 2016-09-07 17:04:07 +0300 | [diff] [blame] | 888 | struct chip_data *chip = |
| 889 | spi_get_ctldata(drv_data->master->cur_msg->spi); |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 890 | unsigned int clk_div; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 891 | |
| 892 | switch (drv_data->ssp_type) { |
| 893 | case QUARK_X1000_SSP: |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 894 | clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate); |
Dan Carpenter | eecacf7 | 2015-03-31 16:49:38 +0300 | [diff] [blame] | 895 | break; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 896 | default: |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 897 | clk_div = ssp_get_clk_div(drv_data, rate); |
Dan Carpenter | eecacf7 | 2015-03-31 16:49:38 +0300 | [diff] [blame] | 898 | break; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 899 | } |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 900 | return clk_div << 8; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 903 | static bool pxa2xx_spi_can_dma(struct spi_controller *master, |
Jarkko Nikula | b6ced29 | 2016-06-21 13:21:34 +0300 | [diff] [blame] | 904 | struct spi_device *spi, |
| 905 | struct spi_transfer *xfer) |
| 906 | { |
| 907 | struct chip_data *chip = spi_get_ctldata(spi); |
| 908 | |
| 909 | return chip->enable_dma && |
| 910 | xfer->len <= MAX_DMA_LEN && |
| 911 | xfer->len >= chip->dma_burst_size; |
| 912 | } |
| 913 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 914 | int pxa2xx_spi_transfer_one(struct spi_controller *master, |
| 915 | struct spi_device *spi, |
| 916 | struct spi_transfer *transfer) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 917 | { |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 918 | struct driver_data *drv_data = spi_controller_get_devdata(master); |
Jarkko Nikula | 4fc0caa | 2016-09-07 17:04:06 +0300 | [diff] [blame] | 919 | struct spi_message *message = master->cur_msg; |
Jarkko Nikula | 96579a4 | 2016-09-07 17:04:07 +0300 | [diff] [blame] | 920 | struct chip_data *chip = spi_get_ctldata(message->spi); |
| 921 | u32 dma_thresh = chip->dma_threshold; |
| 922 | u32 dma_burst = chip->dma_burst_size; |
| 923 | u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data); |
Jarkko Nikula | bffc967 | 2016-09-07 17:04:05 +0300 | [diff] [blame] | 924 | u32 clk_div; |
| 925 | u8 bits; |
| 926 | u32 speed; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 927 | u32 cr0; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 928 | u32 cr1; |
Andy Shevchenko | 7d1f1bf | 2016-03-24 15:35:42 +0200 | [diff] [blame] | 929 | int err; |
Jarkko Nikula | b6ced29 | 2016-06-21 13:21:34 +0300 | [diff] [blame] | 930 | int dma_mapped; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 931 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 932 | /* Check if we can DMA this transfer */ |
Jarkko Nikula | b6ced29 | 2016-06-21 13:21:34 +0300 | [diff] [blame] | 933 | if (transfer->len > MAX_DMA_LEN && chip->enable_dma) { |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 934 | |
| 935 | /* reject already-mapped transfers; PIO won't always work */ |
| 936 | if (message->is_dma_mapped |
| 937 | || transfer->rx_dma || transfer->tx_dma) { |
| 938 | dev_err(&drv_data->pdev->dev, |
Jarkko Nikula | 8ae55af | 2018-04-17 17:20:01 +0300 | [diff] [blame] | 939 | "Mapped transfer length of %u is greater than %d\n", |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 940 | transfer->len, MAX_DMA_LEN); |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 941 | return -EINVAL; |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* warn ... we force this to PIO mode */ |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 945 | dev_warn_ratelimited(&message->spi->dev, |
Jarkko Nikula | 8ae55af | 2018-04-17 17:20:01 +0300 | [diff] [blame] | 946 | "DMA disabled for transfer length %ld greater than %d\n", |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 947 | (long)transfer->len, MAX_DMA_LEN); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 948 | } |
| 949 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 950 | /* Setup the transfer state based on the type of transfer */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 951 | if (pxa2xx_spi_flush(drv_data) == 0) { |
Jarkko Nikula | 8ae55af | 2018-04-17 17:20:01 +0300 | [diff] [blame] | 952 | dev_err(&drv_data->pdev->dev, "Flush failed\n"); |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 953 | return -EIO; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 954 | } |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 955 | drv_data->n_bytes = chip->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 956 | drv_data->tx = (void *)transfer->tx_buf; |
| 957 | drv_data->tx_end = drv_data->tx + transfer->len; |
| 958 | drv_data->rx = transfer->rx_buf; |
| 959 | drv_data->rx_end = drv_data->rx + transfer->len; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 960 | drv_data->write = drv_data->tx ? chip->write : null_writer; |
| 961 | drv_data->read = drv_data->rx ? chip->read : null_reader; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 962 | |
| 963 | /* Change speed and bit per word on a per transfer */ |
Jarkko Nikula | 196b0e2 | 2015-09-15 16:26:27 +0300 | [diff] [blame] | 964 | bits = transfer->bits_per_word; |
| 965 | speed = transfer->speed_hz; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 966 | |
Andy Shevchenko | d2c2f6a | 2015-10-22 16:44:40 +0300 | [diff] [blame] | 967 | clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 968 | |
Jarkko Nikula | 196b0e2 | 2015-09-15 16:26:27 +0300 | [diff] [blame] | 969 | if (bits <= 8) { |
| 970 | drv_data->n_bytes = 1; |
| 971 | drv_data->read = drv_data->read != null_reader ? |
| 972 | u8_reader : null_reader; |
| 973 | drv_data->write = drv_data->write != null_writer ? |
| 974 | u8_writer : null_writer; |
| 975 | } else if (bits <= 16) { |
| 976 | drv_data->n_bytes = 2; |
| 977 | drv_data->read = drv_data->read != null_reader ? |
| 978 | u16_reader : null_reader; |
| 979 | drv_data->write = drv_data->write != null_writer ? |
| 980 | u16_writer : null_writer; |
| 981 | } else if (bits <= 32) { |
| 982 | drv_data->n_bytes = 4; |
| 983 | drv_data->read = drv_data->read != null_reader ? |
| 984 | u32_reader : null_reader; |
| 985 | drv_data->write = drv_data->write != null_writer ? |
| 986 | u32_writer : null_writer; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 987 | } |
Jarkko Nikula | 196b0e2 | 2015-09-15 16:26:27 +0300 | [diff] [blame] | 988 | /* |
| 989 | * if bits/word is changed in dma mode, then must check the |
| 990 | * thresholds and burst also |
| 991 | */ |
| 992 | if (chip->enable_dma) { |
| 993 | if (pxa2xx_spi_set_dma_burst_and_threshold(chip, |
| 994 | message->spi, |
| 995 | bits, &dma_burst, |
| 996 | &dma_thresh)) |
| 997 | dev_warn_ratelimited(&message->spi->dev, |
Jarkko Nikula | 8ae55af | 2018-04-17 17:20:01 +0300 | [diff] [blame] | 998 | "DMA burst size reduced to match bits_per_word\n"); |
Jarkko Nikula | 196b0e2 | 2015-09-15 16:26:27 +0300 | [diff] [blame] | 999 | } |
| 1000 | |
Jarkko Nikula | b6ced29 | 2016-06-21 13:21:34 +0300 | [diff] [blame] | 1001 | dma_mapped = master->can_dma && |
| 1002 | master->can_dma(master, message->spi, transfer) && |
| 1003 | master->cur_msg_mapped; |
| 1004 | if (dma_mapped) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1005 | |
| 1006 | /* Ensure we have the correct interrupt handler */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1007 | drv_data->transfer_handler = pxa2xx_spi_dma_transfer; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1008 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 1009 | err = pxa2xx_spi_dma_prepare(drv_data, transfer); |
| 1010 | if (err) |
| 1011 | return err; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1012 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1013 | /* Clear status and start DMA engine */ |
| 1014 | cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1015 | pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1016 | |
| 1017 | pxa2xx_spi_dma_start(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1018 | } else { |
| 1019 | /* Ensure we have the correct interrupt handler */ |
| 1020 | drv_data->transfer_handler = interrupt_transfer; |
| 1021 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1022 | /* Clear status */ |
| 1023 | cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1024 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Jarkko Nikula | ee03672 | 2016-01-26 15:33:21 +0200 | [diff] [blame] | 1027 | /* NOTE: PXA25x_SSP _could_ use external clocking ... */ |
| 1028 | cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits); |
| 1029 | if (!pxa25x_ssp_comp(drv_data)) |
| 1030 | dev_dbg(&message->spi->dev, "%u Hz actual, %s\n", |
Jarkko Nikula | 2d7537d | 2016-06-21 13:21:33 +0300 | [diff] [blame] | 1031 | master->max_speed_hz |
Jarkko Nikula | ee03672 | 2016-01-26 15:33:21 +0200 | [diff] [blame] | 1032 | / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)), |
Jarkko Nikula | b6ced29 | 2016-06-21 13:21:34 +0300 | [diff] [blame] | 1033 | dma_mapped ? "DMA" : "PIO"); |
Jarkko Nikula | ee03672 | 2016-01-26 15:33:21 +0200 | [diff] [blame] | 1034 | else |
| 1035 | dev_dbg(&message->spi->dev, "%u Hz actual, %s\n", |
Jarkko Nikula | 2d7537d | 2016-06-21 13:21:33 +0300 | [diff] [blame] | 1036 | master->max_speed_hz / 2 |
Jarkko Nikula | ee03672 | 2016-01-26 15:33:21 +0200 | [diff] [blame] | 1037 | / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)), |
Jarkko Nikula | b6ced29 | 2016-06-21 13:21:34 +0300 | [diff] [blame] | 1038 | dma_mapped ? "DMA" : "PIO"); |
Jarkko Nikula | ee03672 | 2016-01-26 15:33:21 +0200 | [diff] [blame] | 1039 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1040 | if (is_lpss_ssp(drv_data)) { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1041 | if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff) |
| 1042 | != chip->lpss_rx_threshold) |
| 1043 | pxa2xx_spi_write(drv_data, SSIRF, |
| 1044 | chip->lpss_rx_threshold); |
| 1045 | if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff) |
| 1046 | != chip->lpss_tx_threshold) |
| 1047 | pxa2xx_spi_write(drv_data, SSITF, |
| 1048 | chip->lpss_tx_threshold); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1051 | if (is_quark_x1000_ssp(drv_data) && |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1052 | (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate)) |
| 1053 | pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1054 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1055 | /* see if we need to reload the config registers */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1056 | if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0) |
| 1057 | || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask) |
| 1058 | != (cr1 & change_mask)) { |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1059 | /* stop the SSP, and update the other bits */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1060 | pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1061 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1062 | pxa2xx_spi_write(drv_data, SSTO, chip->timeout); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1063 | /* first set CR1 without interrupt and service enables */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1064 | pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1065 | /* restart the SSP */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1066 | pxa2xx_spi_write(drv_data, SSCR0, cr0); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1067 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1068 | } else { |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1069 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1070 | pxa2xx_spi_write(drv_data, SSTO, chip->timeout); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1071 | } |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1072 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 1073 | /* |
| 1074 | * Release the data by enabling service requests and interrupts, |
| 1075 | * without changing any mode bits |
| 1076 | */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1077 | pxa2xx_spi_write(drv_data, SSCR1, cr1); |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 1078 | |
| 1079 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 1082 | static void pxa2xx_spi_handle_err(struct spi_controller *master, |
| 1083 | struct spi_message *msg) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1084 | { |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1085 | struct driver_data *drv_data = spi_controller_get_devdata(master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1086 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 1087 | /* Disable the SSP */ |
| 1088 | pxa2xx_spi_write(drv_data, SSCR0, |
| 1089 | pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); |
| 1090 | /* Clear and disable interrupts and service requests */ |
| 1091 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
| 1092 | pxa2xx_spi_write(drv_data, SSCR1, |
| 1093 | pxa2xx_spi_read(drv_data, SSCR1) |
| 1094 | & ~(drv_data->int_cr1 | drv_data->dma_cr1)); |
| 1095 | if (!pxa25x_ssp_comp(drv_data)) |
| 1096 | pxa2xx_spi_write(drv_data, SSTO, 0); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1097 | |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 1098 | /* |
| 1099 | * Stop the DMA if running. Note DMA callback handler may have unset |
| 1100 | * the dma_running already, which is fine as stopping is not needed |
| 1101 | * then but we shouldn't rely this flag for anything else than |
| 1102 | * stopping. For instance to differentiate between PIO and DMA |
| 1103 | * transfers. |
| 1104 | */ |
| 1105 | if (atomic_read(&drv_data->dma_running)) |
| 1106 | pxa2xx_spi_dma_stop(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1109 | static int pxa2xx_spi_unprepare_transfer(struct spi_controller *master) |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1110 | { |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1111 | struct driver_data *drv_data = spi_controller_get_devdata(master); |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1112 | |
| 1113 | /* Disable the SSP now */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1114 | pxa2xx_spi_write(drv_data, SSCR0, |
| 1115 | pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1116 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1117 | return 0; |
| 1118 | } |
| 1119 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1120 | static int setup_cs(struct spi_device *spi, struct chip_data *chip, |
| 1121 | struct pxa2xx_spi_chip *chip_info) |
| 1122 | { |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1123 | struct driver_data *drv_data = |
| 1124 | spi_controller_get_devdata(spi->controller); |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 1125 | struct gpio_desc *gpiod; |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1126 | int err = 0; |
| 1127 | |
Mika Westerberg | 99f499c | 2016-09-26 15:19:50 +0300 | [diff] [blame] | 1128 | if (chip == NULL) |
| 1129 | return 0; |
| 1130 | |
Andy Shevchenko | 6ac5a43 | 2017-07-27 14:37:08 +0300 | [diff] [blame] | 1131 | if (drv_data->cs_gpiods) { |
Andy Shevchenko | 6ac5a43 | 2017-07-27 14:37:08 +0300 | [diff] [blame] | 1132 | gpiod = drv_data->cs_gpiods[spi->chip_select]; |
| 1133 | if (gpiod) { |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 1134 | chip->gpiod_cs = gpiod; |
Andy Shevchenko | 6ac5a43 | 2017-07-27 14:37:08 +0300 | [diff] [blame] | 1135 | chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; |
| 1136 | gpiod_set_value(gpiod, chip->gpio_cs_inverted); |
Mika Westerberg | 99f499c | 2016-09-26 15:19:50 +0300 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | if (chip_info == NULL) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1143 | return 0; |
| 1144 | |
| 1145 | /* NOTE: setup() can be called multiple times, possibly with |
| 1146 | * different chip_info, release previously requested GPIO |
| 1147 | */ |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 1148 | if (chip->gpiod_cs) { |
Mark Brown | a885eeb | 2017-12-22 16:15:36 +0000 | [diff] [blame] | 1149 | gpiod_put(chip->gpiod_cs); |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 1150 | chip->gpiod_cs = NULL; |
| 1151 | } |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1152 | |
| 1153 | /* If (*cs_control) is provided, ignore GPIO chip select */ |
| 1154 | if (chip_info->cs_control) { |
| 1155 | chip->cs_control = chip_info->cs_control; |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | if (gpio_is_valid(chip_info->gpio_cs)) { |
| 1160 | err = gpio_request(chip_info->gpio_cs, "SPI_CS"); |
| 1161 | if (err) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1162 | dev_err(&spi->dev, "failed to request chip select GPIO%d\n", |
| 1163 | chip_info->gpio_cs); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1164 | return err; |
| 1165 | } |
| 1166 | |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 1167 | gpiod = gpio_to_desc(chip_info->gpio_cs); |
| 1168 | chip->gpiod_cs = gpiod; |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1169 | chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; |
| 1170 | |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 1171 | err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | return err; |
| 1175 | } |
| 1176 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1177 | static int setup(struct spi_device *spi) |
| 1178 | { |
Jarkko Nikula | bffc967 | 2016-09-07 17:04:05 +0300 | [diff] [blame] | 1179 | struct pxa2xx_spi_chip *chip_info; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1180 | struct chip_data *chip; |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 1181 | const struct lpss_config *config; |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1182 | struct driver_data *drv_data = |
| 1183 | spi_controller_get_devdata(spi->controller); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1184 | uint tx_thres, tx_hi_thres, rx_thres; |
| 1185 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1186 | switch (drv_data->ssp_type) { |
| 1187 | case QUARK_X1000_SSP: |
| 1188 | tx_thres = TX_THRESH_QUARK_X1000_DFLT; |
| 1189 | tx_hi_thres = 0; |
| 1190 | rx_thres = RX_THRESH_QUARK_X1000_DFLT; |
| 1191 | break; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 1192 | case CE4100_SSP: |
| 1193 | tx_thres = TX_THRESH_CE4100_DFLT; |
| 1194 | tx_hi_thres = 0; |
| 1195 | rx_thres = RX_THRESH_CE4100_DFLT; |
| 1196 | break; |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1197 | case LPSS_LPT_SSP: |
| 1198 | case LPSS_BYT_SSP: |
Mika Westerberg | 30f3a6a | 2016-02-08 17:14:31 +0200 | [diff] [blame] | 1199 | case LPSS_BSW_SSP: |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1200 | case LPSS_SPT_SSP: |
Jarkko Nikula | b7c08cf | 2015-10-28 15:13:42 +0200 | [diff] [blame] | 1201 | case LPSS_BXT_SSP: |
Jarkko Nikula | fc0b2ac | 2017-05-30 17:31:21 +0300 | [diff] [blame] | 1202 | case LPSS_CNL_SSP: |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 1203 | config = lpss_get_config(drv_data); |
| 1204 | tx_thres = config->tx_threshold_lo; |
| 1205 | tx_hi_thres = config->tx_threshold_hi; |
| 1206 | rx_thres = config->rx_threshold; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1207 | break; |
| 1208 | default: |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1209 | tx_thres = TX_THRESH_DFLT; |
| 1210 | tx_hi_thres = 0; |
| 1211 | rx_thres = RX_THRESH_DFLT; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1212 | break; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1213 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1214 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1215 | /* Only alloc on first setup */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1216 | chip = spi_get_ctldata(spi); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1217 | if (!chip) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1218 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
Jingoo Han | 9deae45 | 2014-04-29 17:19:38 +0900 | [diff] [blame] | 1219 | if (!chip) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1220 | return -ENOMEM; |
| 1221 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1222 | if (drv_data->ssp_type == CE4100_SSP) { |
| 1223 | if (spi->chip_select > 4) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1224 | dev_err(&spi->dev, |
| 1225 | "failed setup: cs number must not be > 4.\n"); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1226 | kfree(chip); |
| 1227 | return -EINVAL; |
| 1228 | } |
| 1229 | |
| 1230 | chip->frm = spi->chip_select; |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 1231 | } |
Dan O'Donovan | c64e126 | 2016-05-27 19:57:48 +0100 | [diff] [blame] | 1232 | chip->enable_dma = drv_data->master_info->enable_dma; |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1233 | chip->timeout = TIMOUT_DFLT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1236 | /* protocol drivers may change the chip settings, so... |
| 1237 | * if chip_info exists, use it */ |
| 1238 | chip_info = spi->controller_data; |
| 1239 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1240 | /* chip_info isn't always needed */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1241 | chip->cr1 = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1242 | if (chip_info) { |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1243 | if (chip_info->timeout) |
| 1244 | chip->timeout = chip_info->timeout; |
| 1245 | if (chip_info->tx_threshold) |
| 1246 | tx_thres = chip_info->tx_threshold; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1247 | if (chip_info->tx_hi_threshold) |
| 1248 | tx_hi_thres = chip_info->tx_hi_threshold; |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1249 | if (chip_info->rx_threshold) |
| 1250 | rx_thres = chip_info->rx_threshold; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1251 | chip->dma_threshold = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1252 | if (chip_info->enable_loopback) |
| 1253 | chip->cr1 = SSCR1_LBM; |
| 1254 | } |
| 1255 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1256 | chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres); |
| 1257 | chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
| 1258 | | SSITF_TxHiThresh(tx_hi_thres); |
| 1259 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1260 | /* set dma burst and threshold outside of chip_info path so that if |
| 1261 | * chip_info goes away after setting chip->enable_dma, the |
| 1262 | * burst and threshold can still respond to changes in bits_per_word */ |
| 1263 | if (chip->enable_dma) { |
| 1264 | /* set up legal burst and threshold for dma */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1265 | if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi, |
| 1266 | spi->bits_per_word, |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1267 | &chip->dma_burst_size, |
| 1268 | &chip->dma_threshold)) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1269 | dev_warn(&spi->dev, |
| 1270 | "in setup: DMA burst size reduced to match bits_per_word\n"); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1274 | switch (drv_data->ssp_type) { |
| 1275 | case QUARK_X1000_SSP: |
| 1276 | chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres) |
| 1277 | & QUARK_X1000_SSCR1_RFT) |
| 1278 | | (QUARK_X1000_SSCR1_TxTresh(tx_thres) |
| 1279 | & QUARK_X1000_SSCR1_TFT); |
| 1280 | break; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 1281 | case CE4100_SSP: |
| 1282 | chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) | |
| 1283 | (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT); |
| 1284 | break; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1285 | default: |
| 1286 | chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | |
| 1287 | (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); |
| 1288 | break; |
| 1289 | } |
| 1290 | |
Justin Clacherty | 7f6ee1a | 2007-01-26 00:56:44 -0800 | [diff] [blame] | 1291 | chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); |
| 1292 | chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) |
| 1293 | | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1294 | |
Mika Westerberg | b833172 | 2013-01-22 12:26:31 +0200 | [diff] [blame] | 1295 | if (spi->mode & SPI_LOOP) |
| 1296 | chip->cr1 |= SSCR1_LBM; |
| 1297 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1298 | if (spi->bits_per_word <= 8) { |
| 1299 | chip->n_bytes = 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1300 | chip->read = u8_reader; |
| 1301 | chip->write = u8_writer; |
| 1302 | } else if (spi->bits_per_word <= 16) { |
| 1303 | chip->n_bytes = 2; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1304 | chip->read = u16_reader; |
| 1305 | chip->write = u16_writer; |
| 1306 | } else if (spi->bits_per_word <= 32) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1307 | chip->n_bytes = 4; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1308 | chip->read = u32_reader; |
| 1309 | chip->write = u32_writer; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1310 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1311 | |
| 1312 | spi_set_ctldata(spi, chip); |
| 1313 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1314 | if (drv_data->ssp_type == CE4100_SSP) |
| 1315 | return 0; |
| 1316 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1317 | return setup_cs(spi, chip, chip_info); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 1320 | static void cleanup(struct spi_device *spi) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1321 | { |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 1322 | struct chip_data *chip = spi_get_ctldata(spi); |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1323 | struct driver_data *drv_data = |
| 1324 | spi_controller_get_devdata(spi->controller); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1325 | |
Daniel Ribeiro | 7348d82 | 2009-05-12 13:19:36 -0700 | [diff] [blame] | 1326 | if (!chip) |
| 1327 | return; |
| 1328 | |
Andy Shevchenko | 6ac5a43 | 2017-07-27 14:37:08 +0300 | [diff] [blame] | 1329 | if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods && |
Jan Kiszka | c18d925 | 2017-08-03 13:40:32 +0200 | [diff] [blame] | 1330 | chip->gpiod_cs) |
Mark Brown | a885eeb | 2017-12-22 16:15:36 +0000 | [diff] [blame] | 1331 | gpiod_put(chip->gpiod_cs); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1332 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1333 | kfree(chip); |
| 1334 | } |
| 1335 | |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1336 | #ifdef CONFIG_PCI |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1337 | #ifdef CONFIG_ACPI |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1338 | |
Mathias Krause | 8422ddf | 2015-06-13 14:22:14 +0200 | [diff] [blame] | 1339 | static const struct acpi_device_id pxa2xx_spi_acpi_match[] = { |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1340 | { "INT33C0", LPSS_LPT_SSP }, |
| 1341 | { "INT33C1", LPSS_LPT_SSP }, |
| 1342 | { "INT3430", LPSS_LPT_SSP }, |
| 1343 | { "INT3431", LPSS_LPT_SSP }, |
| 1344 | { "80860F0E", LPSS_BYT_SSP }, |
Mika Westerberg | 30f3a6a | 2016-02-08 17:14:31 +0200 | [diff] [blame] | 1345 | { "8086228E", LPSS_BSW_SSP }, |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1346 | { }, |
| 1347 | }; |
| 1348 | MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match); |
| 1349 | |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1350 | static int pxa2xx_spi_get_port_id(struct acpi_device *adev) |
| 1351 | { |
| 1352 | unsigned int devid; |
| 1353 | int port_id = -1; |
| 1354 | |
| 1355 | if (adev && adev->pnp.unique_id && |
| 1356 | !kstrtouint(adev->pnp.unique_id, 0, &devid)) |
| 1357 | port_id = devid; |
| 1358 | return port_id; |
| 1359 | } |
| 1360 | #else /* !CONFIG_ACPI */ |
| 1361 | static int pxa2xx_spi_get_port_id(struct acpi_device *adev) |
| 1362 | { |
| 1363 | return -1; |
| 1364 | } |
| 1365 | #endif |
| 1366 | |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1367 | /* |
| 1368 | * PCI IDs of compound devices that integrate both host controller and private |
| 1369 | * integrated DMA engine. Please note these are not used in module |
| 1370 | * autoloading and probing in this module but matching the LPSS SSP type. |
| 1371 | */ |
| 1372 | static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = { |
| 1373 | /* SPT-LP */ |
| 1374 | { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP }, |
| 1375 | { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP }, |
| 1376 | /* SPT-H */ |
| 1377 | { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP }, |
| 1378 | { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP }, |
Mika Westerberg | 704d2b0 | 2016-07-04 13:21:07 +0300 | [diff] [blame] | 1379 | /* KBL-H */ |
| 1380 | { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP }, |
| 1381 | { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP }, |
Jarkko Nikula | c1b03f1 | 2016-03-02 09:54:14 +0200 | [diff] [blame] | 1382 | /* BXT A-Step */ |
Jarkko Nikula | b7c08cf | 2015-10-28 15:13:42 +0200 | [diff] [blame] | 1383 | { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP }, |
| 1384 | { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP }, |
| 1385 | { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP }, |
Jarkko Nikula | c1b03f1 | 2016-03-02 09:54:14 +0200 | [diff] [blame] | 1386 | /* BXT B-Step */ |
| 1387 | { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP }, |
| 1388 | { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP }, |
| 1389 | { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP }, |
David E. Box | e18a80a | 2017-01-19 16:25:21 +0200 | [diff] [blame] | 1390 | /* GLK */ |
| 1391 | { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP }, |
| 1392 | { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP }, |
| 1393 | { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP }, |
Jarkko Nikula | b7c08cf | 2015-10-28 15:13:42 +0200 | [diff] [blame] | 1394 | /* APL */ |
| 1395 | { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP }, |
| 1396 | { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP }, |
| 1397 | { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP }, |
Jarkko Nikula | fc0b2ac | 2017-05-30 17:31:21 +0300 | [diff] [blame] | 1398 | /* CNL-LP */ |
| 1399 | { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP }, |
| 1400 | { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP }, |
| 1401 | { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP }, |
| 1402 | /* CNL-H */ |
| 1403 | { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP }, |
| 1404 | { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP }, |
| 1405 | { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP }, |
Axel Lin | 94e5c23 | 2015-08-04 13:52:22 +0800 | [diff] [blame] | 1406 | { }, |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1407 | }; |
| 1408 | |
| 1409 | static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param) |
| 1410 | { |
| 1411 | struct device *dev = param; |
| 1412 | |
| 1413 | if (dev != chan->device->dev->parent) |
| 1414 | return false; |
| 1415 | |
| 1416 | return true; |
| 1417 | } |
| 1418 | |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1419 | static struct pxa2xx_spi_master * |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1420 | pxa2xx_spi_init_pdata(struct platform_device *pdev) |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1421 | { |
| 1422 | struct pxa2xx_spi_master *pdata; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1423 | struct acpi_device *adev; |
| 1424 | struct ssp_device *ssp; |
| 1425 | struct resource *res; |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1426 | const struct acpi_device_id *adev_id = NULL; |
| 1427 | const struct pci_device_id *pcidev_id = NULL; |
Jarkko Nikula | 3b8b6d0 | 2015-10-22 16:44:41 +0300 | [diff] [blame] | 1428 | int type; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1429 | |
Jarkko Nikula | b9f6940 | 2015-09-25 10:27:18 +0300 | [diff] [blame] | 1430 | adev = ACPI_COMPANION(&pdev->dev); |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1431 | |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1432 | if (dev_is_pci(pdev->dev.parent)) |
| 1433 | pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, |
| 1434 | to_pci_dev(pdev->dev.parent)); |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1435 | else if (adev) |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1436 | adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table, |
| 1437 | &pdev->dev); |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1438 | else |
| 1439 | return NULL; |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1440 | |
| 1441 | if (adev_id) |
| 1442 | type = (int)adev_id->driver_data; |
| 1443 | else if (pcidev_id) |
| 1444 | type = (int)pcidev_id->driver_data; |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1445 | else |
| 1446 | return NULL; |
| 1447 | |
Mika Westerberg | cc0ee98 | 2013-06-20 17:44:22 +0300 | [diff] [blame] | 1448 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
Jingoo Han | 9deae45 | 2014-04-29 17:19:38 +0900 | [diff] [blame] | 1449 | if (!pdata) |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1450 | return NULL; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1451 | |
| 1452 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1453 | if (!res) |
| 1454 | return NULL; |
| 1455 | |
| 1456 | ssp = &pdata->ssp; |
| 1457 | |
| 1458 | ssp->phys_base = res->start; |
Sachin Kamat | cbfd6a2 | 2013-04-08 15:49:33 +0530 | [diff] [blame] | 1459 | ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res); |
| 1460 | if (IS_ERR(ssp->mmio_base)) |
Mika Westerberg | 6dc81f6 | 2013-05-13 13:45:09 +0300 | [diff] [blame] | 1461 | return NULL; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1462 | |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1463 | if (pcidev_id) { |
| 1464 | pdata->tx_param = pdev->dev.parent; |
| 1465 | pdata->rx_param = pdev->dev.parent; |
| 1466 | pdata->dma_filter = pxa2xx_spi_idma_filter; |
| 1467 | } |
| 1468 | |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1469 | ssp->clk = devm_clk_get(&pdev->dev, NULL); |
| 1470 | ssp->irq = platform_get_irq(pdev, 0); |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1471 | ssp->type = type; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1472 | ssp->pdev = pdev; |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1473 | ssp->port_id = pxa2xx_spi_get_port_id(adev); |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1474 | |
| 1475 | pdata->num_chipselect = 1; |
Mika Westerberg | cddb339 | 2013-05-13 13:45:10 +0300 | [diff] [blame] | 1476 | pdata->enable_dma = true; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1477 | |
| 1478 | return pdata; |
| 1479 | } |
| 1480 | |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1481 | #else /* !CONFIG_PCI */ |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1482 | static inline struct pxa2xx_spi_master * |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1483 | pxa2xx_spi_init_pdata(struct platform_device *pdev) |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1484 | { |
| 1485 | return NULL; |
| 1486 | } |
| 1487 | #endif |
| 1488 | |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1489 | static int pxa2xx_spi_fw_translate_cs(struct spi_controller *master, |
| 1490 | unsigned int cs) |
Mika Westerberg | 0c27d9c | 2016-02-08 17:14:29 +0200 | [diff] [blame] | 1491 | { |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1492 | struct driver_data *drv_data = spi_controller_get_devdata(master); |
Mika Westerberg | 0c27d9c | 2016-02-08 17:14:29 +0200 | [diff] [blame] | 1493 | |
| 1494 | if (has_acpi_companion(&drv_data->pdev->dev)) { |
| 1495 | switch (drv_data->ssp_type) { |
| 1496 | /* |
| 1497 | * For Atoms the ACPI DeviceSelection used by the Windows |
| 1498 | * driver starts from 1 instead of 0 so translate it here |
| 1499 | * to match what Linux expects. |
| 1500 | */ |
| 1501 | case LPSS_BYT_SSP: |
Mika Westerberg | 30f3a6a | 2016-02-08 17:14:31 +0200 | [diff] [blame] | 1502 | case LPSS_BSW_SSP: |
Mika Westerberg | 0c27d9c | 2016-02-08 17:14:29 +0200 | [diff] [blame] | 1503 | return cs - 1; |
| 1504 | |
| 1505 | default: |
| 1506 | break; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | return cs; |
| 1511 | } |
| 1512 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1513 | static int pxa2xx_spi_probe(struct platform_device *pdev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1514 | { |
| 1515 | struct device *dev = &pdev->dev; |
| 1516 | struct pxa2xx_spi_master *platform_info; |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1517 | struct spi_controller *master; |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1518 | struct driver_data *drv_data; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1519 | struct ssp_device *ssp; |
Jarkko Nikula | 8b136ba | 2015-10-28 15:13:41 +0200 | [diff] [blame] | 1520 | const struct lpss_config *config; |
Mika Westerberg | 99f499c | 2016-09-26 15:19:50 +0300 | [diff] [blame] | 1521 | int status, count; |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1522 | u32 tmp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1523 | |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1524 | platform_info = dev_get_platdata(dev); |
| 1525 | if (!platform_info) { |
Jarkko Nikula | 0db6421 | 2015-10-28 15:13:43 +0200 | [diff] [blame] | 1526 | platform_info = pxa2xx_spi_init_pdata(pdev); |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1527 | if (!platform_info) { |
| 1528 | dev_err(&pdev->dev, "missing platform data\n"); |
| 1529 | return -ENODEV; |
| 1530 | } |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1531 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1532 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1533 | ssp = pxa_ssp_request(pdev->id, pdev->name); |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1534 | if (!ssp) |
| 1535 | ssp = &platform_info->ssp; |
| 1536 | |
| 1537 | if (!ssp->mmio_base) { |
| 1538 | dev_err(&pdev->dev, "failed to get ssp\n"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1539 | return -ENODEV; |
| 1540 | } |
| 1541 | |
Jarkko Nikula | 757fe8d | 2015-08-05 10:04:05 +0300 | [diff] [blame] | 1542 | master = spi_alloc_master(dev, sizeof(struct driver_data)); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1543 | if (!master) { |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1544 | dev_err(&pdev->dev, "cannot alloc spi_master\n"); |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1545 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1546 | return -ENOMEM; |
| 1547 | } |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1548 | drv_data = spi_controller_get_devdata(master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1549 | drv_data->master = master; |
| 1550 | drv_data->master_info = platform_info; |
| 1551 | drv_data->pdev = pdev; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1552 | drv_data->ssp = ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1553 | |
Sebastian Andrzej Siewior | 21486af | 2010-10-08 18:11:19 +0200 | [diff] [blame] | 1554 | master->dev.of_node = pdev->dev.of_node; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1555 | /* the spi->mode bits understood by this driver: */ |
Mika Westerberg | b833172 | 2013-01-22 12:26:31 +0200 | [diff] [blame] | 1556 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1557 | |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1558 | master->bus_num = ssp->port_id; |
Mike Rapoport | 7ad0ba9 | 2009-04-06 19:00:57 -0700 | [diff] [blame] | 1559 | master->dma_alignment = DMA_ALIGNMENT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1560 | master->cleanup = cleanup; |
| 1561 | master->setup = setup; |
Jarkko Nikula | d5898e1 | 2018-04-17 17:20:02 +0300 | [diff] [blame^] | 1562 | master->set_cs = pxa2xx_spi_set_cs; |
| 1563 | master->transfer_one = pxa2xx_spi_transfer_one; |
| 1564 | master->handle_err = pxa2xx_spi_handle_err; |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1565 | master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer; |
Mika Westerberg | 0c27d9c | 2016-02-08 17:14:29 +0200 | [diff] [blame] | 1566 | master->fw_translate_cs = pxa2xx_spi_fw_translate_cs; |
Mark Brown | 7dd6278 | 2013-07-28 15:35:21 +0100 | [diff] [blame] | 1567 | master->auto_runtime_pm = true; |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1568 | master->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1569 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1570 | drv_data->ssp_type = ssp->type; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1571 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1572 | drv_data->ioaddr = ssp->mmio_base; |
| 1573 | drv_data->ssdr_physical = ssp->phys_base + SSDR; |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1574 | if (pxa25x_ssp_comp(drv_data)) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1575 | switch (drv_data->ssp_type) { |
| 1576 | case QUARK_X1000_SSP: |
| 1577 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
| 1578 | break; |
| 1579 | default: |
| 1580 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
| 1581 | break; |
| 1582 | } |
| 1583 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1584 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; |
| 1585 | drv_data->dma_cr1 = 0; |
| 1586 | drv_data->clear_sr = SSSR_ROR; |
| 1587 | drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; |
| 1588 | } else { |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1589 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1590 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; |
Mika Westerberg | 5928808 | 2013-01-22 12:26:29 +0200 | [diff] [blame] | 1591 | drv_data->dma_cr1 = DEFAULT_DMA_CR1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1592 | drv_data->clear_sr = SSSR_ROR | SSSR_TINT; |
| 1593 | drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR; |
| 1594 | } |
| 1595 | |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 1596 | status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev), |
| 1597 | drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1598 | if (status < 0) { |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1599 | dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1600 | goto out_error_master_alloc; |
| 1601 | } |
| 1602 | |
| 1603 | /* Setup DMA if requested */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1604 | if (platform_info->enable_dma) { |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1605 | status = pxa2xx_spi_dma_setup(drv_data); |
| 1606 | if (status) { |
Mika Westerberg | cddb339 | 2013-05-13 13:45:10 +0300 | [diff] [blame] | 1607 | dev_dbg(dev, "no DMA channels available, using PIO\n"); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1608 | platform_info->enable_dma = false; |
Jarkko Nikula | b6ced29 | 2016-06-21 13:21:34 +0300 | [diff] [blame] | 1609 | } else { |
| 1610 | master->can_dma = pxa2xx_spi_can_dma; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1611 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | /* Enable SOC clock */ |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1615 | clk_prepare_enable(ssp->clk); |
| 1616 | |
Jarkko Nikula | 0eca7cf | 2015-09-25 10:27:17 +0300 | [diff] [blame] | 1617 | master->max_speed_hz = clk_get_rate(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1618 | |
| 1619 | /* Load default SSP configuration */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1620 | pxa2xx_spi_write(drv_data, SSCR0, 0); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1621 | switch (drv_data->ssp_type) { |
| 1622 | case QUARK_X1000_SSP: |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 1623 | tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) | |
| 1624 | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT); |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1625 | pxa2xx_spi_write(drv_data, SSCR1, tmp); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1626 | |
| 1627 | /* using the Motorola SPI protocol and use 8 bit frame */ |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 1628 | tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8); |
| 1629 | pxa2xx_spi_write(drv_data, SSCR0, tmp); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1630 | break; |
Andy Shevchenko | 7c7289a | 2016-09-07 15:43:22 +0300 | [diff] [blame] | 1631 | case CE4100_SSP: |
| 1632 | tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) | |
| 1633 | CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT); |
| 1634 | pxa2xx_spi_write(drv_data, SSCR1, tmp); |
| 1635 | tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); |
| 1636 | pxa2xx_spi_write(drv_data, SSCR0, tmp); |
Andy Shevchenko | a2dd8af | 2017-01-02 13:44:28 +0200 | [diff] [blame] | 1637 | break; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1638 | default: |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1639 | tmp = SSCR1_RxTresh(RX_THRESH_DFLT) | |
| 1640 | SSCR1_TxTresh(TX_THRESH_DFLT); |
| 1641 | pxa2xx_spi_write(drv_data, SSCR1, tmp); |
| 1642 | tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); |
| 1643 | pxa2xx_spi_write(drv_data, SSCR0, tmp); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1644 | break; |
| 1645 | } |
| 1646 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1647 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1648 | pxa2xx_spi_write(drv_data, SSTO, 0); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1649 | |
| 1650 | if (!is_quark_x1000_ssp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1651 | pxa2xx_spi_write(drv_data, SSPSP, 0); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1652 | |
Jarkko Nikula | 8b136ba | 2015-10-28 15:13:41 +0200 | [diff] [blame] | 1653 | if (is_lpss_ssp(drv_data)) { |
| 1654 | lpss_ssp_setup(drv_data); |
| 1655 | config = lpss_get_config(drv_data); |
| 1656 | if (config->reg_capabilities >= 0) { |
| 1657 | tmp = __lpss_ssp_read_priv(drv_data, |
| 1658 | config->reg_capabilities); |
| 1659 | tmp &= LPSS_CAPS_CS_EN_MASK; |
| 1660 | tmp >>= LPSS_CAPS_CS_EN_SHIFT; |
| 1661 | platform_info->num_chipselect = ffz(tmp); |
Mika Westerberg | 30f3a6a | 2016-02-08 17:14:31 +0200 | [diff] [blame] | 1662 | } else if (config->cs_num) { |
| 1663 | platform_info->num_chipselect = config->cs_num; |
Jarkko Nikula | 8b136ba | 2015-10-28 15:13:41 +0200 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | master->num_chipselect = platform_info->num_chipselect; |
| 1667 | |
Mika Westerberg | 99f499c | 2016-09-26 15:19:50 +0300 | [diff] [blame] | 1668 | count = gpiod_count(&pdev->dev, "cs"); |
Andy Shevchenko | 6ac5a43 | 2017-07-27 14:37:08 +0300 | [diff] [blame] | 1669 | if (count > 0) { |
| 1670 | int i; |
| 1671 | |
Mika Westerberg | 99f499c | 2016-09-26 15:19:50 +0300 | [diff] [blame] | 1672 | master->num_chipselect = max_t(int, count, |
| 1673 | master->num_chipselect); |
| 1674 | |
Andy Shevchenko | 6ac5a43 | 2017-07-27 14:37:08 +0300 | [diff] [blame] | 1675 | drv_data->cs_gpiods = devm_kcalloc(&pdev->dev, |
| 1676 | master->num_chipselect, sizeof(struct gpio_desc *), |
| 1677 | GFP_KERNEL); |
| 1678 | if (!drv_data->cs_gpiods) { |
| 1679 | status = -ENOMEM; |
| 1680 | goto out_error_clock_enabled; |
| 1681 | } |
| 1682 | |
| 1683 | for (i = 0; i < master->num_chipselect; i++) { |
| 1684 | struct gpio_desc *gpiod; |
| 1685 | |
Andy Shevchenko | d35f2dc | 2017-07-27 18:49:33 +0300 | [diff] [blame] | 1686 | gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS); |
Andy Shevchenko | 6ac5a43 | 2017-07-27 14:37:08 +0300 | [diff] [blame] | 1687 | if (IS_ERR(gpiod)) { |
| 1688 | /* Means use native chip select */ |
| 1689 | if (PTR_ERR(gpiod) == -ENOENT) |
| 1690 | continue; |
| 1691 | |
| 1692 | status = (int)PTR_ERR(gpiod); |
| 1693 | goto out_error_clock_enabled; |
| 1694 | } else { |
| 1695 | drv_data->cs_gpiods[i] = gpiod; |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
Antonio Ospite | 836d1a22 | 2014-05-30 18:18:09 +0200 | [diff] [blame] | 1700 | pm_runtime_set_autosuspend_delay(&pdev->dev, 50); |
| 1701 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1702 | pm_runtime_set_active(&pdev->dev); |
| 1703 | pm_runtime_enable(&pdev->dev); |
| 1704 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1705 | /* Register with the SPI framework */ |
| 1706 | platform_set_drvdata(pdev, drv_data); |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1707 | status = devm_spi_register_controller(&pdev->dev, master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1708 | if (status != 0) { |
| 1709 | dev_err(&pdev->dev, "problem registering spi master\n"); |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1710 | goto out_error_clock_enabled; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | return status; |
| 1714 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1715 | out_error_clock_enabled: |
Jarkko Nikula | e2b714a | 2018-03-07 17:05:04 +0200 | [diff] [blame] | 1716 | pm_runtime_put_noidle(&pdev->dev); |
| 1717 | pm_runtime_disable(&pdev->dev); |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1718 | clk_disable_unprepare(ssp->clk); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1719 | pxa2xx_spi_dma_release(drv_data); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1720 | free_irq(ssp->irq, drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1721 | |
| 1722 | out_error_master_alloc: |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1723 | spi_controller_put(master); |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1724 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1725 | return status; |
| 1726 | } |
| 1727 | |
| 1728 | static int pxa2xx_spi_remove(struct platform_device *pdev) |
| 1729 | { |
| 1730 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
Julia Lawall | 51e911e | 2009-01-06 14:41:45 -0800 | [diff] [blame] | 1731 | struct ssp_device *ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1732 | |
| 1733 | if (!drv_data) |
| 1734 | return 0; |
Julia Lawall | 51e911e | 2009-01-06 14:41:45 -0800 | [diff] [blame] | 1735 | ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1736 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1737 | pm_runtime_get_sync(&pdev->dev); |
| 1738 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1739 | /* Disable the SSP at the peripheral and SOC level */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1740 | pxa2xx_spi_write(drv_data, SSCR0, 0); |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1741 | clk_disable_unprepare(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1742 | |
| 1743 | /* Release DMA */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1744 | if (drv_data->master_info->enable_dma) |
| 1745 | pxa2xx_spi_dma_release(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1746 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1747 | pm_runtime_put_noidle(&pdev->dev); |
| 1748 | pm_runtime_disable(&pdev->dev); |
| 1749 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1750 | /* Release IRQ */ |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1751 | free_irq(ssp->irq, drv_data); |
| 1752 | |
| 1753 | /* Release SSP */ |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1754 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1755 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | static void pxa2xx_spi_shutdown(struct platform_device *pdev) |
| 1760 | { |
| 1761 | int status = 0; |
| 1762 | |
| 1763 | if ((status = pxa2xx_spi_remove(pdev)) != 0) |
| 1764 | dev_err(&pdev->dev, "shutdown failed with %d\n", status); |
| 1765 | } |
| 1766 | |
Mika Westerberg | 382cebb | 2014-01-16 14:50:55 +0200 | [diff] [blame] | 1767 | #ifdef CONFIG_PM_SLEEP |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1768 | static int pxa2xx_spi_suspend(struct device *dev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1769 | { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1770 | struct driver_data *drv_data = dev_get_drvdata(dev); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1771 | struct ssp_device *ssp = drv_data->ssp; |
Jarkko Nikula | bffc967 | 2016-09-07 17:04:05 +0300 | [diff] [blame] | 1772 | int status; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1773 | |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1774 | status = spi_controller_suspend(drv_data->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1775 | if (status != 0) |
| 1776 | return status; |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1777 | pxa2xx_spi_write(drv_data, SSCR0, 0); |
Dmitry Eremin-Solenikov | 2b9375b | 2014-11-06 14:08:29 +0300 | [diff] [blame] | 1778 | |
| 1779 | if (!pm_runtime_suspended(dev)) |
| 1780 | clk_disable_unprepare(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1781 | |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1785 | static int pxa2xx_spi_resume(struct device *dev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1786 | { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1787 | struct driver_data *drv_data = dev_get_drvdata(dev); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1788 | struct ssp_device *ssp = drv_data->ssp; |
Jarkko Nikula | bffc967 | 2016-09-07 17:04:05 +0300 | [diff] [blame] | 1789 | int status; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1790 | |
| 1791 | /* Enable the SSP clock */ |
Dmitry Eremin-Solenikov | 2b9375b | 2014-11-06 14:08:29 +0300 | [diff] [blame] | 1792 | if (!pm_runtime_suspended(dev)) |
| 1793 | clk_prepare_enable(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1794 | |
Chew, Chiau Ee | c50325f | 2013-11-29 02:13:11 +0800 | [diff] [blame] | 1795 | /* Restore LPSS private register bits */ |
Jarkko Nikula | 48421ad | 2015-01-28 10:09:42 +0200 | [diff] [blame] | 1796 | if (is_lpss_ssp(drv_data)) |
| 1797 | lpss_ssp_setup(drv_data); |
Chew, Chiau Ee | c50325f | 2013-11-29 02:13:11 +0800 | [diff] [blame] | 1798 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1799 | /* Start the queue running */ |
Jarkko Nikula | 3cc7b0e | 2018-02-01 17:17:30 +0200 | [diff] [blame] | 1800 | status = spi_controller_resume(drv_data->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1801 | if (status != 0) { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1802 | dev_err(dev, "problem starting queue (%d)\n", status); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1803 | return status; |
| 1804 | } |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1808 | #endif |
| 1809 | |
Rafael J. Wysocki | ec83305 | 2014-12-13 00:41:15 +0100 | [diff] [blame] | 1810 | #ifdef CONFIG_PM |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1811 | static int pxa2xx_spi_runtime_suspend(struct device *dev) |
| 1812 | { |
| 1813 | struct driver_data *drv_data = dev_get_drvdata(dev); |
| 1814 | |
| 1815 | clk_disable_unprepare(drv_data->ssp->clk); |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
| 1819 | static int pxa2xx_spi_runtime_resume(struct device *dev) |
| 1820 | { |
| 1821 | struct driver_data *drv_data = dev_get_drvdata(dev); |
| 1822 | |
| 1823 | clk_prepare_enable(drv_data->ssp->clk); |
| 1824 | return 0; |
| 1825 | } |
| 1826 | #endif |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1827 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1828 | static const struct dev_pm_ops pxa2xx_spi_pm_ops = { |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1829 | SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume) |
| 1830 | SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, |
| 1831 | pxa2xx_spi_runtime_resume, NULL) |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1832 | }; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1833 | |
| 1834 | static struct platform_driver driver = { |
| 1835 | .driver = { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1836 | .name = "pxa2xx-spi", |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1837 | .pm = &pxa2xx_spi_pm_ops, |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1838 | .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match), |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1839 | }, |
Sebastian Andrzej Siewior | fbd29a1 | 2010-11-19 09:00:11 -0800 | [diff] [blame] | 1840 | .probe = pxa2xx_spi_probe, |
David Brownell | d1e44d9 | 2007-10-16 01:27:46 -0700 | [diff] [blame] | 1841 | .remove = pxa2xx_spi_remove, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1842 | .shutdown = pxa2xx_spi_shutdown, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1843 | }; |
| 1844 | |
| 1845 | static int __init pxa2xx_spi_init(void) |
| 1846 | { |
Sebastian Andrzej Siewior | fbd29a1 | 2010-11-19 09:00:11 -0800 | [diff] [blame] | 1847 | return platform_driver_register(&driver); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1848 | } |
Antonio Ospite | 5b61a74 | 2009-09-22 16:46:10 -0700 | [diff] [blame] | 1849 | subsys_initcall(pxa2xx_spi_init); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1850 | |
| 1851 | static void __exit pxa2xx_spi_exit(void) |
| 1852 | { |
| 1853 | platform_driver_unregister(&driver); |
| 1854 | } |
| 1855 | module_exit(pxa2xx_spi_exit); |