Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1 | /* ////////////////////////////////////////////////////////////////////////// */ |
| 2 | /* */ |
| 3 | /* Copyright (c) Atmel Corporation. All rights reserved. */ |
| 4 | /* */ |
| 5 | /* Module Name: wilc_spi.c */ |
| 6 | /* */ |
| 7 | /* */ |
| 8 | /* //////////////////////////////////////////////////////////////////////////// */ |
Glen Lee | 43a7622 | 2015-12-21 14:18:33 +0900 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/cdev.h> |
| 16 | #include <linux/uaccess.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/spi/spi.h> |
| 19 | #include <linux/of_gpio.h> |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 20 | |
Chaehyun Lim | 0c9fc33 | 2015-09-30 17:52:14 +0900 | [diff] [blame] | 21 | #include <linux/string.h> |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 22 | #include "wilc_wlan_if.h" |
| 23 | #include "wilc_wlan.h" |
Glen Lee | 9c80032 | 2015-11-06 18:40:22 +0900 | [diff] [blame] | 24 | #include "wilc_wfi_netdevice.h" |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 25 | |
Bhumika Goyal | 6a707a9 | 2016-02-21 03:12:11 +0530 | [diff] [blame] | 26 | struct wilc_spi { |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 27 | int crc_off; |
| 28 | int nint; |
| 29 | int has_thrpt_enh; |
Bhumika Goyal | 6a707a9 | 2016-02-21 03:12:11 +0530 | [diff] [blame] | 30 | }; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 31 | |
Bhumika Goyal | 6a707a9 | 2016-02-21 03:12:11 +0530 | [diff] [blame] | 32 | static struct wilc_spi g_spi; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 33 | |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 34 | static int wilc_spi_read(struct wilc *wilc, u32, u8 *, u32); |
| 35 | static int wilc_spi_write(struct wilc *wilc, u32, u8 *, u32); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 36 | |
| 37 | /******************************************** |
| 38 | * |
| 39 | * Crc7 |
| 40 | * |
| 41 | ********************************************/ |
| 42 | |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 43 | static const u8 crc7_syndrome_table[256] = { |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 44 | 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, |
| 45 | 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
| 46 | 0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26, |
| 47 | 0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e, |
| 48 | 0x32, 0x3b, 0x20, 0x29, 0x16, 0x1f, 0x04, 0x0d, |
| 49 | 0x7a, 0x73, 0x68, 0x61, 0x5e, 0x57, 0x4c, 0x45, |
| 50 | 0x2b, 0x22, 0x39, 0x30, 0x0f, 0x06, 0x1d, 0x14, |
| 51 | 0x63, 0x6a, 0x71, 0x78, 0x47, 0x4e, 0x55, 0x5c, |
| 52 | 0x64, 0x6d, 0x76, 0x7f, 0x40, 0x49, 0x52, 0x5b, |
| 53 | 0x2c, 0x25, 0x3e, 0x37, 0x08, 0x01, 0x1a, 0x13, |
| 54 | 0x7d, 0x74, 0x6f, 0x66, 0x59, 0x50, 0x4b, 0x42, |
| 55 | 0x35, 0x3c, 0x27, 0x2e, 0x11, 0x18, 0x03, 0x0a, |
| 56 | 0x56, 0x5f, 0x44, 0x4d, 0x72, 0x7b, 0x60, 0x69, |
| 57 | 0x1e, 0x17, 0x0c, 0x05, 0x3a, 0x33, 0x28, 0x21, |
| 58 | 0x4f, 0x46, 0x5d, 0x54, 0x6b, 0x62, 0x79, 0x70, |
| 59 | 0x07, 0x0e, 0x15, 0x1c, 0x23, 0x2a, 0x31, 0x38, |
| 60 | 0x41, 0x48, 0x53, 0x5a, 0x65, 0x6c, 0x77, 0x7e, |
| 61 | 0x09, 0x00, 0x1b, 0x12, 0x2d, 0x24, 0x3f, 0x36, |
| 62 | 0x58, 0x51, 0x4a, 0x43, 0x7c, 0x75, 0x6e, 0x67, |
| 63 | 0x10, 0x19, 0x02, 0x0b, 0x34, 0x3d, 0x26, 0x2f, |
| 64 | 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
| 65 | 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, |
| 66 | 0x6a, 0x63, 0x78, 0x71, 0x4e, 0x47, 0x5c, 0x55, |
| 67 | 0x22, 0x2b, 0x30, 0x39, 0x06, 0x0f, 0x14, 0x1d, |
| 68 | 0x25, 0x2c, 0x37, 0x3e, 0x01, 0x08, 0x13, 0x1a, |
| 69 | 0x6d, 0x64, 0x7f, 0x76, 0x49, 0x40, 0x5b, 0x52, |
| 70 | 0x3c, 0x35, 0x2e, 0x27, 0x18, 0x11, 0x0a, 0x03, |
| 71 | 0x74, 0x7d, 0x66, 0x6f, 0x50, 0x59, 0x42, 0x4b, |
| 72 | 0x17, 0x1e, 0x05, 0x0c, 0x33, 0x3a, 0x21, 0x28, |
| 73 | 0x5f, 0x56, 0x4d, 0x44, 0x7b, 0x72, 0x69, 0x60, |
| 74 | 0x0e, 0x07, 0x1c, 0x15, 0x2a, 0x23, 0x38, 0x31, |
| 75 | 0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79 |
| 76 | }; |
| 77 | |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 78 | static u8 crc7_byte(u8 crc, u8 data) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 79 | { |
| 80 | return crc7_syndrome_table[(crc << 1) ^ data]; |
| 81 | } |
| 82 | |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 83 | static u8 crc7(u8 crc, const u8 *buffer, u32 len) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 84 | { |
| 85 | while (len--) |
| 86 | crc = crc7_byte(crc, *buffer++); |
| 87 | return crc; |
| 88 | } |
| 89 | |
| 90 | /******************************************** |
| 91 | * |
| 92 | * Spi protocol Function |
| 93 | * |
| 94 | ********************************************/ |
| 95 | |
| 96 | #define CMD_DMA_WRITE 0xc1 |
| 97 | #define CMD_DMA_READ 0xc2 |
| 98 | #define CMD_INTERNAL_WRITE 0xc3 |
| 99 | #define CMD_INTERNAL_READ 0xc4 |
| 100 | #define CMD_TERMINATE 0xc5 |
| 101 | #define CMD_REPEAT 0xc6 |
| 102 | #define CMD_DMA_EXT_WRITE 0xc7 |
| 103 | #define CMD_DMA_EXT_READ 0xc8 |
| 104 | #define CMD_SINGLE_WRITE 0xc9 |
| 105 | #define CMD_SINGLE_READ 0xca |
| 106 | #define CMD_RESET 0xcf |
| 107 | |
| 108 | #define N_OK 1 |
| 109 | #define N_FAIL 0 |
| 110 | #define N_RESET -1 |
| 111 | #define N_RETRY -2 |
| 112 | |
| 113 | #define DATA_PKT_SZ_256 256 |
| 114 | #define DATA_PKT_SZ_512 512 |
| 115 | #define DATA_PKT_SZ_1K 1024 |
| 116 | #define DATA_PKT_SZ_4K (4 * 1024) |
| 117 | #define DATA_PKT_SZ_8K (8 * 1024) |
| 118 | #define DATA_PKT_SZ DATA_PKT_SZ_8K |
| 119 | |
Glen Lee | 43a7622 | 2015-12-21 14:18:33 +0900 | [diff] [blame] | 120 | #define USE_SPI_DMA 0 |
| 121 | |
Glen Lee | 43a7622 | 2015-12-21 14:18:33 +0900 | [diff] [blame] | 122 | static int wilc_bus_probe(struct spi_device *spi) |
| 123 | { |
| 124 | int ret, gpio; |
| 125 | struct wilc *wilc; |
| 126 | |
| 127 | gpio = of_get_gpio(spi->dev.of_node, 0); |
| 128 | if (gpio < 0) |
| 129 | gpio = GPIO_NUM; |
| 130 | |
| 131 | ret = wilc_netdev_init(&wilc, NULL, HIF_SPI, GPIO_NUM, &wilc_hif_spi); |
| 132 | if (ret) |
| 133 | return ret; |
| 134 | |
| 135 | spi_set_drvdata(spi, wilc); |
| 136 | wilc->dev = &spi->dev; |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int wilc_bus_remove(struct spi_device *spi) |
| 142 | { |
| 143 | wilc_netdev_cleanup(spi_get_drvdata(spi)); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static const struct of_device_id wilc1000_of_match[] = { |
| 148 | { .compatible = "atmel,wilc_spi", }, |
| 149 | {} |
| 150 | }; |
| 151 | MODULE_DEVICE_TABLE(of, wilc1000_of_match); |
| 152 | |
Hugo Camboulive | d27afda | 2016-01-04 22:02:23 +0000 | [diff] [blame] | 153 | static struct spi_driver wilc1000_spi_driver = { |
Glen Lee | 43a7622 | 2015-12-21 14:18:33 +0900 | [diff] [blame] | 154 | .driver = { |
| 155 | .name = MODALIAS, |
| 156 | .of_match_table = wilc1000_of_match, |
| 157 | }, |
| 158 | .probe = wilc_bus_probe, |
| 159 | .remove = wilc_bus_remove, |
| 160 | }; |
| 161 | module_spi_driver(wilc1000_spi_driver); |
| 162 | MODULE_LICENSE("GPL"); |
| 163 | |
| 164 | static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len) |
| 165 | { |
| 166 | struct spi_device *spi = to_spi_device(wilc->dev); |
| 167 | int ret; |
| 168 | struct spi_message msg; |
| 169 | |
| 170 | if (len > 0 && b) { |
| 171 | struct spi_transfer tr = { |
| 172 | .tx_buf = b, |
| 173 | .len = len, |
| 174 | .delay_usecs = 0, |
| 175 | }; |
| 176 | char *r_buffer = kzalloc(len, GFP_KERNEL); |
| 177 | |
| 178 | if (!r_buffer) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | tr.rx_buf = r_buffer; |
| 182 | dev_dbg(&spi->dev, "Request writing %d bytes\n", len); |
| 183 | |
| 184 | memset(&msg, 0, sizeof(msg)); |
| 185 | spi_message_init(&msg); |
| 186 | msg.spi = spi; |
| 187 | msg.is_dma_mapped = USE_SPI_DMA; |
| 188 | spi_message_add_tail(&tr, &msg); |
| 189 | |
| 190 | ret = spi_sync(spi, &msg); |
| 191 | if (ret < 0) |
| 192 | dev_err(&spi->dev, "SPI transaction failed\n"); |
| 193 | |
| 194 | kfree(r_buffer); |
| 195 | } else { |
| 196 | dev_err(&spi->dev, |
| 197 | "can't write data with the following length: %d\n", |
| 198 | len); |
Glen Lee | 43a7622 | 2015-12-21 14:18:33 +0900 | [diff] [blame] | 199 | ret = -EINVAL; |
| 200 | } |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen) |
| 206 | { |
| 207 | struct spi_device *spi = to_spi_device(wilc->dev); |
| 208 | int ret; |
| 209 | |
| 210 | if (rlen > 0) { |
| 211 | struct spi_message msg; |
| 212 | struct spi_transfer tr = { |
| 213 | .rx_buf = rb, |
| 214 | .len = rlen, |
| 215 | .delay_usecs = 0, |
| 216 | |
| 217 | }; |
| 218 | char *t_buffer = kzalloc(rlen, GFP_KERNEL); |
| 219 | |
| 220 | if (!t_buffer) |
| 221 | return -ENOMEM; |
| 222 | |
| 223 | tr.tx_buf = t_buffer; |
| 224 | |
| 225 | memset(&msg, 0, sizeof(msg)); |
| 226 | spi_message_init(&msg); |
| 227 | msg.spi = spi; |
| 228 | msg.is_dma_mapped = USE_SPI_DMA; |
| 229 | spi_message_add_tail(&tr, &msg); |
| 230 | |
| 231 | ret = spi_sync(spi, &msg); |
| 232 | if (ret < 0) |
| 233 | dev_err(&spi->dev, "SPI transaction failed\n"); |
| 234 | kfree(t_buffer); |
| 235 | } else { |
| 236 | dev_err(&spi->dev, |
| 237 | "can't read data with the following length: %u\n", |
| 238 | rlen); |
| 239 | ret = -EINVAL; |
| 240 | } |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen) |
| 246 | { |
| 247 | struct spi_device *spi = to_spi_device(wilc->dev); |
| 248 | int ret; |
| 249 | |
| 250 | if (rlen > 0) { |
| 251 | struct spi_message msg; |
| 252 | struct spi_transfer tr = { |
| 253 | .rx_buf = rb, |
| 254 | .tx_buf = wb, |
| 255 | .len = rlen, |
| 256 | .bits_per_word = 8, |
| 257 | .delay_usecs = 0, |
| 258 | |
| 259 | }; |
| 260 | |
| 261 | memset(&msg, 0, sizeof(msg)); |
| 262 | spi_message_init(&msg); |
| 263 | msg.spi = spi; |
| 264 | msg.is_dma_mapped = USE_SPI_DMA; |
| 265 | |
| 266 | spi_message_add_tail(&tr, &msg); |
| 267 | ret = spi_sync(spi, &msg); |
| 268 | if (ret < 0) |
| 269 | dev_err(&spi->dev, "SPI transaction failed\n"); |
| 270 | } else { |
| 271 | dev_err(&spi->dev, |
| 272 | "can't read data with the following length: %u\n", |
| 273 | rlen); |
| 274 | ret = -EINVAL; |
| 275 | } |
| 276 | |
| 277 | return ret; |
| 278 | } |
| 279 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 280 | static int spi_cmd_complete(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz, |
| 281 | u8 clockless) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 282 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 283 | struct spi_device *spi = to_spi_device(wilc->dev); |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 284 | u8 wb[32], rb[32]; |
| 285 | u8 wix, rix; |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 286 | u32 len2; |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 287 | u8 rsp; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 288 | int len = 0; |
| 289 | int result = N_OK; |
| 290 | |
| 291 | wb[0] = cmd; |
| 292 | switch (cmd) { |
| 293 | case CMD_SINGLE_READ: /* single word (4 bytes) read */ |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 294 | wb[1] = (u8)(adr >> 16); |
| 295 | wb[2] = (u8)(adr >> 8); |
| 296 | wb[3] = (u8)adr; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 297 | len = 5; |
| 298 | break; |
| 299 | |
| 300 | case CMD_INTERNAL_READ: /* internal register read */ |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 301 | wb[1] = (u8)(adr >> 8); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 302 | if (clockless == 1) |
Anish Bhatt | ffda203 | 2015-09-29 12:15:49 -0700 | [diff] [blame] | 303 | wb[1] |= BIT(7); |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 304 | wb[2] = (u8)adr; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 305 | wb[3] = 0x00; |
| 306 | len = 5; |
| 307 | break; |
| 308 | |
| 309 | case CMD_TERMINATE: /* termination */ |
| 310 | wb[1] = 0x00; |
| 311 | wb[2] = 0x00; |
| 312 | wb[3] = 0x00; |
| 313 | len = 5; |
| 314 | break; |
| 315 | |
| 316 | case CMD_REPEAT: /* repeat */ |
| 317 | wb[1] = 0x00; |
| 318 | wb[2] = 0x00; |
| 319 | wb[3] = 0x00; |
| 320 | len = 5; |
| 321 | break; |
| 322 | |
| 323 | case CMD_RESET: /* reset */ |
| 324 | wb[1] = 0xff; |
| 325 | wb[2] = 0xff; |
| 326 | wb[3] = 0xff; |
| 327 | len = 5; |
| 328 | break; |
| 329 | |
| 330 | case CMD_DMA_WRITE: /* dma write */ |
| 331 | case CMD_DMA_READ: /* dma read */ |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 332 | wb[1] = (u8)(adr >> 16); |
| 333 | wb[2] = (u8)(adr >> 8); |
| 334 | wb[3] = (u8)adr; |
| 335 | wb[4] = (u8)(sz >> 8); |
| 336 | wb[5] = (u8)(sz); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 337 | len = 7; |
| 338 | break; |
| 339 | |
| 340 | case CMD_DMA_EXT_WRITE: /* dma extended write */ |
| 341 | case CMD_DMA_EXT_READ: /* dma extended read */ |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 342 | wb[1] = (u8)(adr >> 16); |
| 343 | wb[2] = (u8)(adr >> 8); |
| 344 | wb[3] = (u8)adr; |
| 345 | wb[4] = (u8)(sz >> 16); |
| 346 | wb[5] = (u8)(sz >> 8); |
| 347 | wb[6] = (u8)(sz); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 348 | len = 8; |
| 349 | break; |
| 350 | |
| 351 | case CMD_INTERNAL_WRITE: /* internal register write */ |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 352 | wb[1] = (u8)(adr >> 8); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 353 | if (clockless == 1) |
Anish Bhatt | ffda203 | 2015-09-29 12:15:49 -0700 | [diff] [blame] | 354 | wb[1] |= BIT(7); |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 355 | wb[2] = (u8)(adr); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 356 | wb[3] = b[3]; |
| 357 | wb[4] = b[2]; |
| 358 | wb[5] = b[1]; |
| 359 | wb[6] = b[0]; |
| 360 | len = 8; |
| 361 | break; |
| 362 | |
| 363 | case CMD_SINGLE_WRITE: /* single word write */ |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 364 | wb[1] = (u8)(adr >> 16); |
| 365 | wb[2] = (u8)(adr >> 8); |
| 366 | wb[3] = (u8)(adr); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 367 | wb[4] = b[3]; |
| 368 | wb[5] = b[2]; |
| 369 | wb[6] = b[1]; |
| 370 | wb[7] = b[0]; |
| 371 | len = 9; |
| 372 | break; |
| 373 | |
| 374 | default: |
| 375 | result = N_FAIL; |
| 376 | break; |
| 377 | } |
| 378 | |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 379 | if (result != N_OK) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 380 | return result; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 381 | |
Chandra S Gorentla | 78174ad | 2015-08-08 17:41:36 +0530 | [diff] [blame] | 382 | if (!g_spi.crc_off) |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 383 | wb[len - 1] = (crc7(0x7f, (const u8 *)&wb[0], len - 1)) << 1; |
Chandra S Gorentla | 78174ad | 2015-08-08 17:41:36 +0530 | [diff] [blame] | 384 | else |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 385 | len -= 1; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 386 | |
| 387 | #define NUM_SKIP_BYTES (1) |
| 388 | #define NUM_RSP_BYTES (2) |
| 389 | #define NUM_DATA_HDR_BYTES (1) |
| 390 | #define NUM_DATA_BYTES (4) |
| 391 | #define NUM_CRC_BYTES (2) |
| 392 | #define NUM_DUMMY_BYTES (3) |
| 393 | if ((cmd == CMD_RESET) || |
| 394 | (cmd == CMD_TERMINATE) || |
| 395 | (cmd == CMD_REPEAT)) { |
| 396 | len2 = len + (NUM_SKIP_BYTES + NUM_RSP_BYTES + NUM_DUMMY_BYTES); |
| 397 | } else if ((cmd == CMD_INTERNAL_READ) || (cmd == CMD_SINGLE_READ)) { |
| 398 | if (!g_spi.crc_off) { |
| 399 | len2 = len + (NUM_RSP_BYTES + NUM_DATA_HDR_BYTES + NUM_DATA_BYTES |
| 400 | + NUM_CRC_BYTES + NUM_DUMMY_BYTES); |
| 401 | } else { |
| 402 | len2 = len + (NUM_RSP_BYTES + NUM_DATA_HDR_BYTES + NUM_DATA_BYTES |
| 403 | + NUM_DUMMY_BYTES); |
| 404 | } |
| 405 | } else { |
| 406 | len2 = len + (NUM_RSP_BYTES + NUM_DUMMY_BYTES); |
| 407 | } |
| 408 | #undef NUM_DUMMY_BYTES |
| 409 | |
Chaehyun Lim | 5cc59d2 | 2015-10-02 16:41:16 +0900 | [diff] [blame] | 410 | if (len2 > ARRAY_SIZE(wb)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 411 | dev_err(&spi->dev, "spi buffer size too small (%d) (%zu)\n", |
Chaehyun Lim | 5cc59d2 | 2015-10-02 16:41:16 +0900 | [diff] [blame] | 412 | len2, ARRAY_SIZE(wb)); |
Rehas Sachdeva | d37843d | 2016-09-20 13:18:40 +0530 | [diff] [blame] | 413 | return N_FAIL; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 414 | } |
| 415 | /* zero spi write buffers. */ |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 416 | for (wix = len; wix < len2; wix++) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 417 | wb[wix] = 0; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 418 | rix = len; |
| 419 | |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 420 | if (wilc_spi_tx_rx(wilc, wb, rb, len2)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 421 | dev_err(&spi->dev, "Failed cmd write, bus error...\n"); |
Namrata A Shettar | 8244d26 | 2016-09-30 19:14:54 +0530 | [diff] [blame] | 422 | return N_FAIL; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 423 | } |
| 424 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 425 | /** |
| 426 | * Command/Control response |
| 427 | **/ |
| 428 | if ((cmd == CMD_RESET) || |
| 429 | (cmd == CMD_TERMINATE) || |
| 430 | (cmd == CMD_REPEAT)) { |
| 431 | rix++; /* skip 1 byte */ |
| 432 | } |
| 433 | |
| 434 | /* do { */ |
| 435 | rsp = rb[rix++]; |
| 436 | /* if(rsp == cmd) break; */ |
| 437 | /* } while(&rptr[1] <= &rb[len2]); */ |
| 438 | |
| 439 | if (rsp != cmd) { |
Anchal Jain | 8779bf8 | 2016-02-21 22:19:05 +0530 | [diff] [blame] | 440 | dev_err(&spi->dev, |
| 441 | "Failed cmd response, cmd (%02x), resp (%02x)\n", |
| 442 | cmd, rsp); |
Namrata A Shettar | 8244d26 | 2016-09-30 19:14:54 +0530 | [diff] [blame] | 443 | return N_FAIL; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
| 447 | * State response |
| 448 | **/ |
| 449 | rsp = rb[rix++]; |
| 450 | if (rsp != 0x00) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 451 | dev_err(&spi->dev, "Failed cmd state response state (%02x)\n", |
| 452 | rsp); |
Namrata A Shettar | 8244d26 | 2016-09-30 19:14:54 +0530 | [diff] [blame] | 453 | return N_FAIL; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | if ((cmd == CMD_INTERNAL_READ) || (cmd == CMD_SINGLE_READ) |
| 457 | || (cmd == CMD_DMA_READ) || (cmd == CMD_DMA_EXT_READ)) { |
| 458 | int retry; |
Chaehyun Lim | ec53adf | 2015-09-15 14:06:15 +0900 | [diff] [blame] | 459 | /* u16 crc1, crc2; */ |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 460 | u8 crc[2]; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 461 | /** |
| 462 | * Data Respnose header |
| 463 | **/ |
| 464 | retry = 100; |
| 465 | do { |
| 466 | /* ensure there is room in buffer later to read data and crc */ |
| 467 | if (rix < len2) { |
| 468 | rsp = rb[rix++]; |
| 469 | } else { |
| 470 | retry = 0; |
| 471 | break; |
| 472 | } |
| 473 | if (((rsp >> 4) & 0xf) == 0xf) |
| 474 | break; |
| 475 | } while (retry--); |
| 476 | |
| 477 | if (retry <= 0) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 478 | dev_err(&spi->dev, |
| 479 | "Error, data read response (%02x)\n", rsp); |
Namrata A Shettar | 8244d26 | 2016-09-30 19:14:54 +0530 | [diff] [blame] | 480 | return N_RESET; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | if ((cmd == CMD_INTERNAL_READ) || (cmd == CMD_SINGLE_READ)) { |
| 484 | /** |
| 485 | * Read bytes |
| 486 | **/ |
| 487 | if ((rix + 3) < len2) { |
| 488 | b[0] = rb[rix++]; |
| 489 | b[1] = rb[rix++]; |
| 490 | b[2] = rb[rix++]; |
| 491 | b[3] = rb[rix++]; |
| 492 | } else { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 493 | dev_err(&spi->dev, |
| 494 | "buffer overrun when reading data.\n"); |
Namrata A Shettar | 8244d26 | 2016-09-30 19:14:54 +0530 | [diff] [blame] | 495 | return N_FAIL; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | if (!g_spi.crc_off) { |
| 499 | /** |
| 500 | * Read Crc |
| 501 | **/ |
| 502 | if ((rix + 1) < len2) { |
| 503 | crc[0] = rb[rix++]; |
| 504 | crc[1] = rb[rix++]; |
| 505 | } else { |
Roger H. Newell | 5142a14 | 2016-02-16 09:24:56 -0330 | [diff] [blame] | 506 | dev_err(&spi->dev, "buffer overrun when reading crc.\n"); |
Namrata A Shettar | 8244d26 | 2016-09-30 19:14:54 +0530 | [diff] [blame] | 507 | return N_FAIL; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } else if ((cmd == CMD_DMA_READ) || (cmd == CMD_DMA_EXT_READ)) { |
| 511 | int ix; |
| 512 | |
| 513 | /* some data may be read in response to dummy bytes. */ |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 514 | for (ix = 0; (rix < len2) && (ix < sz); ) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 515 | b[ix++] = rb[rix++]; |
Chaehyun Lim | 61500fb | 2015-06-18 14:46:05 +0900 | [diff] [blame] | 516 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 517 | sz -= ix; |
| 518 | |
| 519 | if (sz > 0) { |
| 520 | int nbytes; |
| 521 | |
Chandra S Gorentla | 78174ad | 2015-08-08 17:41:36 +0530 | [diff] [blame] | 522 | if (sz <= (DATA_PKT_SZ - ix)) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 523 | nbytes = sz; |
Chandra S Gorentla | 78174ad | 2015-08-08 17:41:36 +0530 | [diff] [blame] | 524 | else |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 525 | nbytes = DATA_PKT_SZ - ix; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 526 | |
| 527 | /** |
| 528 | * Read bytes |
| 529 | **/ |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 530 | if (wilc_spi_rx(wilc, &b[ix], nbytes)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 531 | dev_err(&spi->dev, "Failed data block read, bus error...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 532 | result = N_FAIL; |
| 533 | goto _error_; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Read Crc |
| 538 | **/ |
| 539 | if (!g_spi.crc_off) { |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 540 | if (wilc_spi_rx(wilc, crc, 2)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 541 | dev_err(&spi->dev, "Failed data block crc read, bus error...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 542 | result = N_FAIL; |
| 543 | goto _error_; |
| 544 | } |
| 545 | } |
| 546 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 547 | ix += nbytes; |
| 548 | sz -= nbytes; |
| 549 | } |
| 550 | |
| 551 | /* if any data in left unread, then read the rest using normal DMA code.*/ |
| 552 | while (sz > 0) { |
| 553 | int nbytes; |
| 554 | |
Chandra S Gorentla | 78174ad | 2015-08-08 17:41:36 +0530 | [diff] [blame] | 555 | if (sz <= DATA_PKT_SZ) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 556 | nbytes = sz; |
Chandra S Gorentla | 78174ad | 2015-08-08 17:41:36 +0530 | [diff] [blame] | 557 | else |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 558 | nbytes = DATA_PKT_SZ; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 559 | |
| 560 | /** |
| 561 | * read data response only on the next DMA cycles not |
| 562 | * the first DMA since data response header is already |
| 563 | * handled above for the first DMA. |
| 564 | **/ |
| 565 | /** |
| 566 | * Data Respnose header |
| 567 | **/ |
| 568 | retry = 10; |
| 569 | do { |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 570 | if (wilc_spi_rx(wilc, &rsp, 1)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 571 | dev_err(&spi->dev, "Failed data response read, bus error...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 572 | result = N_FAIL; |
| 573 | break; |
| 574 | } |
| 575 | if (((rsp >> 4) & 0xf) == 0xf) |
| 576 | break; |
| 577 | } while (retry--); |
| 578 | |
| 579 | if (result == N_FAIL) |
| 580 | break; |
| 581 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 582 | /** |
| 583 | * Read bytes |
| 584 | **/ |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 585 | if (wilc_spi_rx(wilc, &b[ix], nbytes)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 586 | dev_err(&spi->dev, "Failed data block read, bus error...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 587 | result = N_FAIL; |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Read Crc |
| 593 | **/ |
| 594 | if (!g_spi.crc_off) { |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 595 | if (wilc_spi_rx(wilc, crc, 2)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 596 | dev_err(&spi->dev, "Failed data block crc read, bus error...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 597 | result = N_FAIL; |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | ix += nbytes; |
| 603 | sz -= nbytes; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | _error_: |
| 608 | return result; |
| 609 | } |
| 610 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 611 | static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 612 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 613 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 614 | int ix, nbytes; |
| 615 | int result = 1; |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 616 | u8 cmd, order, crc[2] = {0}; |
| 617 | /* u8 rsp; */ |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 618 | |
| 619 | /** |
| 620 | * Data |
| 621 | **/ |
| 622 | ix = 0; |
| 623 | do { |
| 624 | if (sz <= DATA_PKT_SZ) |
| 625 | nbytes = sz; |
| 626 | else |
| 627 | nbytes = DATA_PKT_SZ; |
| 628 | |
| 629 | /** |
| 630 | * Write command |
| 631 | **/ |
| 632 | cmd = 0xf0; |
| 633 | if (ix == 0) { |
| 634 | if (sz <= DATA_PKT_SZ) |
| 635 | |
| 636 | order = 0x3; |
| 637 | else |
| 638 | order = 0x1; |
| 639 | } else { |
| 640 | if (sz <= DATA_PKT_SZ) |
| 641 | order = 0x3; |
| 642 | else |
| 643 | order = 0x2; |
| 644 | } |
| 645 | cmd |= order; |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 646 | if (wilc_spi_tx(wilc, &cmd, 1)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 647 | dev_err(&spi->dev, |
| 648 | "Failed data block cmd write, bus error...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 649 | result = N_FAIL; |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Write data |
| 655 | **/ |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 656 | if (wilc_spi_tx(wilc, &b[ix], nbytes)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 657 | dev_err(&spi->dev, |
| 658 | "Failed data block write, bus error...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 659 | result = N_FAIL; |
| 660 | break; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Write Crc |
| 665 | **/ |
| 666 | if (!g_spi.crc_off) { |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 667 | if (wilc_spi_tx(wilc, crc, 2)) { |
Roger H. Newell | 5142a14 | 2016-02-16 09:24:56 -0330 | [diff] [blame] | 668 | dev_err(&spi->dev, "Failed data block crc write, bus error...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 669 | result = N_FAIL; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * No need to wait for response |
| 676 | **/ |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 677 | ix += nbytes; |
| 678 | sz -= nbytes; |
| 679 | } while (sz); |
| 680 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 681 | return result; |
| 682 | } |
| 683 | |
| 684 | /******************************************** |
| 685 | * |
| 686 | * Spi Internal Read/Write Function |
| 687 | * |
| 688 | ********************************************/ |
| 689 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 690 | static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 691 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 692 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 693 | int result; |
| 694 | |
Glen Lee | 9e6627a | 2015-12-21 14:18:08 +0900 | [diff] [blame] | 695 | dat = cpu_to_le32(dat); |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 696 | result = spi_cmd_complete(wilc, CMD_INTERNAL_WRITE, adr, (u8 *)&dat, 4, |
| 697 | 0); |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 698 | if (result != N_OK) |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 699 | dev_err(&spi->dev, "Failed internal write cmd...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 700 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 701 | return result; |
| 702 | } |
| 703 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 704 | static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 705 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 706 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 707 | int result; |
| 708 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 709 | result = spi_cmd_complete(wilc, CMD_INTERNAL_READ, adr, (u8 *)data, 4, |
| 710 | 0); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 711 | if (result != N_OK) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 712 | dev_err(&spi->dev, "Failed internal read cmd...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 713 | return 0; |
| 714 | } |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 715 | |
Glen Lee | 9e6627a | 2015-12-21 14:18:08 +0900 | [diff] [blame] | 716 | *data = cpu_to_le32(*data); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 717 | |
| 718 | return 1; |
| 719 | } |
| 720 | |
| 721 | /******************************************** |
| 722 | * |
| 723 | * Spi interfaces |
| 724 | * |
| 725 | ********************************************/ |
| 726 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 727 | static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 728 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 729 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 730 | int result = N_OK; |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 731 | u8 cmd = CMD_SINGLE_WRITE; |
| 732 | u8 clockless = 0; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 733 | |
Glen Lee | 9e6627a | 2015-12-21 14:18:08 +0900 | [diff] [blame] | 734 | data = cpu_to_le32(data); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 735 | if (addr < 0x30) { |
| 736 | /* Clockless register*/ |
| 737 | cmd = CMD_INTERNAL_WRITE; |
| 738 | clockless = 1; |
| 739 | } |
| 740 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 741 | result = spi_cmd_complete(wilc, cmd, addr, (u8 *)&data, 4, clockless); |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 742 | if (result != N_OK) |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 743 | dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 744 | |
| 745 | return result; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 746 | } |
| 747 | |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 748 | static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 749 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 750 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 751 | int result; |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 752 | u8 cmd = CMD_DMA_EXT_WRITE; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 753 | |
| 754 | /** |
| 755 | * has to be greated than 4 |
| 756 | **/ |
| 757 | if (size <= 4) |
| 758 | return 0; |
| 759 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 760 | result = spi_cmd_complete(wilc, cmd, addr, NULL, size, 0); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 761 | if (result != N_OK) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 762 | dev_err(&spi->dev, |
| 763 | "Failed cmd, write block (%08x)...\n", addr); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 764 | return 0; |
| 765 | } |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 766 | |
| 767 | /** |
| 768 | * Data |
| 769 | **/ |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 770 | result = spi_data_write(wilc, buf, size); |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 771 | if (result != N_OK) |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 772 | dev_err(&spi->dev, "Failed block data write...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 773 | |
| 774 | return 1; |
| 775 | } |
| 776 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 777 | static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 778 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 779 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 780 | int result = N_OK; |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 781 | u8 cmd = CMD_SINGLE_READ; |
| 782 | u8 clockless = 0; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 783 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 784 | if (addr < 0x30) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 785 | /* dev_err(&spi->dev, "***** read addr %d\n\n", addr); */ |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 786 | /* Clockless register*/ |
| 787 | cmd = CMD_INTERNAL_READ; |
| 788 | clockless = 1; |
| 789 | } |
| 790 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 791 | result = spi_cmd_complete(wilc, cmd, addr, (u8 *)data, 4, clockless); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 792 | if (result != N_OK) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 793 | dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 794 | return 0; |
| 795 | } |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 796 | |
Glen Lee | 9e6627a | 2015-12-21 14:18:08 +0900 | [diff] [blame] | 797 | *data = cpu_to_le32(*data); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 798 | |
| 799 | return 1; |
| 800 | } |
| 801 | |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 802 | static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 803 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 804 | struct spi_device *spi = to_spi_device(wilc->dev); |
Chaehyun Lim | 51e825f | 2015-09-15 14:06:14 +0900 | [diff] [blame] | 805 | u8 cmd = CMD_DMA_EXT_READ; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 806 | int result; |
| 807 | |
| 808 | if (size <= 4) |
| 809 | return 0; |
| 810 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 811 | result = spi_cmd_complete(wilc, cmd, addr, buf, size, 0); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 812 | if (result != N_OK) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 813 | dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 814 | return 0; |
| 815 | } |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 816 | |
| 817 | return 1; |
| 818 | } |
| 819 | |
| 820 | /******************************************** |
| 821 | * |
| 822 | * Bus interfaces |
| 823 | * |
| 824 | ********************************************/ |
| 825 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 826 | static int _wilc_spi_deinit(struct wilc *wilc) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 827 | { |
| 828 | /** |
| 829 | * TODO: |
| 830 | **/ |
| 831 | return 1; |
| 832 | } |
| 833 | |
Glen Lee | 5397cbc | 2016-01-25 16:35:09 +0900 | [diff] [blame] | 834 | static int wilc_spi_init(struct wilc *wilc, bool resume) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 835 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 836 | struct spi_device *spi = to_spi_device(wilc->dev); |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 837 | u32 reg; |
| 838 | u32 chipid; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 839 | |
| 840 | static int isinit; |
| 841 | |
| 842 | if (isinit) { |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 843 | if (!wilc_spi_read_reg(wilc, 0x1000, &chipid)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 844 | dev_err(&spi->dev, "Fail cmd read chip id...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | return 1; |
| 848 | } |
| 849 | |
Bhumika Goyal | 6a707a9 | 2016-02-21 03:12:11 +0530 | [diff] [blame] | 850 | memset(&g_spi, 0, sizeof(struct wilc_spi)); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 851 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 852 | /** |
| 853 | * configure protocol |
| 854 | **/ |
| 855 | g_spi.crc_off = 0; |
| 856 | |
| 857 | /* TODO: We can remove the CRC trials if there is a definite way to reset */ |
| 858 | /* the SPI to it's initial value. */ |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 859 | if (!spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®)) { |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 860 | /* Read failed. Try with CRC off. This might happen when module |
| 861 | * is removed but chip isn't reset*/ |
| 862 | g_spi.crc_off = 1; |
Colin Ian King | 2218b8f | 2016-08-28 12:28:41 +0100 | [diff] [blame] | 863 | dev_err(&spi->dev, "Failed internal read protocol with CRC on, retrying with CRC off...\n"); |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 864 | if (!spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®)) { |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 865 | /* Reaad failed with both CRC on and off, something went bad */ |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 866 | dev_err(&spi->dev, |
| 867 | "Failed internal read protocol...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | } |
| 871 | if (g_spi.crc_off == 0) { |
| 872 | reg &= ~0xc; /* disable crc checking */ |
| 873 | reg &= ~0x70; |
| 874 | reg |= (0x5 << 4); |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 875 | if (!spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 876 | dev_err(&spi->dev, "[wilc spi %d]: Failed internal write protocol reg...\n", __LINE__); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | g_spi.crc_off = 1; |
| 880 | } |
| 881 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 882 | /** |
| 883 | * make sure can read back chip id correctly |
| 884 | **/ |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 885 | if (!wilc_spi_read_reg(wilc, 0x1000, &chipid)) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 886 | dev_err(&spi->dev, "Fail cmd read chip id...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 887 | return 0; |
| 888 | } |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 889 | /* dev_err(&spi->dev, "chipid (%08x)\n", chipid); */ |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 890 | |
| 891 | g_spi.has_thrpt_enh = 1; |
| 892 | |
| 893 | isinit = 1; |
| 894 | |
| 895 | return 1; |
| 896 | } |
| 897 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 898 | static int wilc_spi_read_size(struct wilc *wilc, u32 *size) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 899 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 900 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 901 | int ret; |
Chaehyun Lim | 8dfaafd | 2015-08-18 23:18:11 +0900 | [diff] [blame] | 902 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 903 | if (g_spi.has_thrpt_enh) { |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 904 | ret = spi_internal_read(wilc, 0xe840 - WILC_SPI_REG_BASE, |
| 905 | size); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 906 | *size = *size & IRQ_DMA_WD_CNT_MASK; |
| 907 | } else { |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 908 | u32 tmp; |
| 909 | u32 byte_cnt; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 910 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 911 | ret = wilc_spi_read_reg(wilc, WILC_VMM_TO_HOST_SIZE, |
| 912 | &byte_cnt); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 913 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 914 | dev_err(&spi->dev, |
| 915 | "Failed read WILC_VMM_TO_HOST_SIZE ...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 916 | goto _fail_; |
| 917 | } |
| 918 | tmp = (byte_cnt >> 2) & IRQ_DMA_WD_CNT_MASK; |
| 919 | *size = tmp; |
| 920 | } |
| 921 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 922 | _fail_: |
| 923 | return ret; |
| 924 | } |
| 925 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 926 | static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 927 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 928 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 929 | int ret; |
Chaehyun Lim | 8dfaafd | 2015-08-18 23:18:11 +0900 | [diff] [blame] | 930 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 931 | if (g_spi.has_thrpt_enh) { |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 932 | ret = spi_internal_read(wilc, 0xe840 - WILC_SPI_REG_BASE, |
| 933 | int_status); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 934 | } else { |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 935 | u32 tmp; |
| 936 | u32 byte_cnt; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 937 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 938 | ret = wilc_spi_read_reg(wilc, WILC_VMM_TO_HOST_SIZE, |
| 939 | &byte_cnt); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 940 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 941 | dev_err(&spi->dev, |
| 942 | "Failed read WILC_VMM_TO_HOST_SIZE ...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 943 | goto _fail_; |
| 944 | } |
| 945 | tmp = (byte_cnt >> 2) & IRQ_DMA_WD_CNT_MASK; |
| 946 | |
| 947 | { |
| 948 | int happended, j; |
| 949 | |
| 950 | j = 0; |
| 951 | do { |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 952 | u32 irq_flags; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 953 | |
| 954 | happended = 0; |
| 955 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 956 | wilc_spi_read_reg(wilc, 0x1a90, &irq_flags); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 957 | tmp |= ((irq_flags >> 27) << IRG_FLAGS_OFFSET); |
| 958 | |
| 959 | if (g_spi.nint > 5) { |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 960 | wilc_spi_read_reg(wilc, 0x1a94, |
| 961 | &irq_flags); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 962 | tmp |= (((irq_flags >> 0) & 0x7) << (IRG_FLAGS_OFFSET + 5)); |
| 963 | } |
| 964 | |
| 965 | { |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 966 | u32 unkmown_mask; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 967 | |
| 968 | unkmown_mask = ~((1ul << g_spi.nint) - 1); |
| 969 | |
| 970 | if ((tmp >> IRG_FLAGS_OFFSET) & unkmown_mask) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 971 | dev_err(&spi->dev, "Unexpected interrupt (2): j=%d, tmp=%x, mask=%x\n", j, tmp, unkmown_mask); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 972 | happended = 1; |
| 973 | } |
| 974 | } |
| 975 | j++; |
| 976 | } while (happended); |
| 977 | } |
| 978 | |
| 979 | *int_status = tmp; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | _fail_: |
| 983 | return ret; |
| 984 | } |
| 985 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 986 | static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 987 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 988 | struct spi_device *spi = to_spi_device(wilc->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 989 | int ret; |
| 990 | |
| 991 | if (g_spi.has_thrpt_enh) { |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 992 | ret = spi_internal_write(wilc, 0xe844 - WILC_SPI_REG_BASE, |
| 993 | val); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 994 | } else { |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 995 | u32 flags; |
Chaehyun Lim | 8dfaafd | 2015-08-18 23:18:11 +0900 | [diff] [blame] | 996 | |
Anish Bhatt | ffda203 | 2015-09-29 12:15:49 -0700 | [diff] [blame] | 997 | flags = val & (BIT(MAX_NUM_INT) - 1); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 998 | if (flags) { |
| 999 | int i; |
| 1000 | |
| 1001 | ret = 1; |
| 1002 | for (i = 0; i < g_spi.nint; i++) { |
| 1003 | /* No matter what you write 1 or 0, it will clear interrupt. */ |
| 1004 | if (flags & 1) |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1005 | ret = wilc_spi_write_reg(wilc, 0x10c8 + i * 4, 1); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1006 | if (!ret) |
| 1007 | break; |
| 1008 | flags >>= 1; |
| 1009 | } |
| 1010 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1011 | dev_err(&spi->dev, |
| 1012 | "Failed wilc_spi_write_reg, set reg %x ...\n", |
| 1013 | 0x10c8 + i * 4); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1014 | goto _fail_; |
| 1015 | } |
| 1016 | for (i = g_spi.nint; i < MAX_NUM_INT; i++) { |
| 1017 | if (flags & 1) |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1018 | dev_err(&spi->dev, |
| 1019 | "Unexpected interrupt cleared %d...\n", |
| 1020 | i); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1021 | flags >>= 1; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | { |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 1026 | u32 tbl_ctl; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1027 | |
| 1028 | tbl_ctl = 0; |
| 1029 | /* select VMM table 0 */ |
| 1030 | if ((val & SEL_VMM_TBL0) == SEL_VMM_TBL0) |
Anish Bhatt | ffda203 | 2015-09-29 12:15:49 -0700 | [diff] [blame] | 1031 | tbl_ctl |= BIT(0); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1032 | /* select VMM table 1 */ |
| 1033 | if ((val & SEL_VMM_TBL1) == SEL_VMM_TBL1) |
Anish Bhatt | ffda203 | 2015-09-29 12:15:49 -0700 | [diff] [blame] | 1034 | tbl_ctl |= BIT(1); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1035 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1036 | ret = wilc_spi_write_reg(wilc, WILC_VMM_TBL_CTL, |
| 1037 | tbl_ctl); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1038 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1039 | dev_err(&spi->dev, |
| 1040 | "fail write reg vmm_tbl_ctl...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1041 | goto _fail_; |
| 1042 | } |
| 1043 | |
| 1044 | if ((val & EN_VMM) == EN_VMM) { |
| 1045 | /** |
| 1046 | * enable vmm transfer. |
| 1047 | **/ |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1048 | ret = wilc_spi_write_reg(wilc, |
| 1049 | WILC_VMM_CORE_CTL, 1); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1050 | if (!ret) { |
Roger H. Newell | 5142a14 | 2016-02-16 09:24:56 -0330 | [diff] [blame] | 1051 | dev_err(&spi->dev, "fail write reg vmm_core_ctl...\n"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1052 | goto _fail_; |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | _fail_: |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1061 | static int wilc_spi_sync_ext(struct wilc *wilc, int nint) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1062 | { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1063 | struct spi_device *spi = to_spi_device(wilc->dev); |
Chaehyun Lim | fbc2fe1 | 2015-09-15 14:06:16 +0900 | [diff] [blame] | 1064 | u32 reg; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1065 | int ret, i; |
| 1066 | |
| 1067 | if (nint > MAX_NUM_INT) { |
Colin Ian King | 95b8cb8 | 2016-06-23 14:14:07 +0100 | [diff] [blame] | 1068 | dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | g_spi.nint = nint; |
| 1073 | |
| 1074 | /** |
| 1075 | * interrupt pin mux select |
| 1076 | **/ |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1077 | ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1078 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1079 | dev_err(&spi->dev, "Failed read reg (%08x)...\n", |
| 1080 | WILC_PIN_MUX_0); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1081 | return 0; |
| 1082 | } |
Anish Bhatt | ffda203 | 2015-09-29 12:15:49 -0700 | [diff] [blame] | 1083 | reg |= BIT(8); |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1084 | ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1085 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1086 | dev_err(&spi->dev, "Failed write reg (%08x)...\n", |
| 1087 | WILC_PIN_MUX_0); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * interrupt enable |
| 1093 | **/ |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1094 | ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1095 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1096 | dev_err(&spi->dev, "Failed read reg (%08x)...\n", |
| 1097 | WILC_INTR_ENABLE); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1098 | return 0; |
| 1099 | } |
| 1100 | |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 1101 | for (i = 0; (i < 5) && (nint > 0); i++, nint--) |
Anish Bhatt | ffda203 | 2015-09-29 12:15:49 -0700 | [diff] [blame] | 1102 | reg |= (BIT((27 + i))); |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 1103 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1104 | ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1105 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1106 | dev_err(&spi->dev, "Failed write reg (%08x)...\n", |
| 1107 | WILC_INTR_ENABLE); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | if (nint) { |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1111 | ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1112 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1113 | dev_err(&spi->dev, "Failed read reg (%08x)...\n", |
| 1114 | WILC_INTR2_ENABLE); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
Roger H. Newell | e0a3000 | 2016-02-16 09:25:42 -0330 | [diff] [blame] | 1118 | for (i = 0; (i < 3) && (nint > 0); i++, nint--) |
Anish Bhatt | ffda203 | 2015-09-29 12:15:49 -0700 | [diff] [blame] | 1119 | reg |= BIT(i); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1120 | |
Glen Lee | 49dcd0d | 2015-11-18 15:11:26 +0900 | [diff] [blame] | 1121 | ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1122 | if (!ret) { |
Glen Lee | ac1da16 | 2015-12-21 14:18:13 +0900 | [diff] [blame] | 1123 | dev_err(&spi->dev, "Failed write reg (%08x)...\n", |
| 1124 | WILC_INTR2_ENABLE); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1125 | return 0; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | return 1; |
| 1130 | } |
| 1131 | /******************************************** |
| 1132 | * |
| 1133 | * Global spi HIF function table |
| 1134 | * |
| 1135 | ********************************************/ |
Arnd Bergmann | 7d37a4a | 2015-11-16 15:05:05 +0100 | [diff] [blame] | 1136 | const struct wilc_hif_func wilc_hif_spi = { |
Glen Lee | 2769d94 | 2015-12-21 14:18:32 +0900 | [diff] [blame] | 1137 | .hif_init = wilc_spi_init, |
Arnd Bergmann | 7d37a4a | 2015-11-16 15:05:05 +0100 | [diff] [blame] | 1138 | .hif_deinit = _wilc_spi_deinit, |
| 1139 | .hif_read_reg = wilc_spi_read_reg, |
| 1140 | .hif_write_reg = wilc_spi_write_reg, |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 1141 | .hif_block_rx = wilc_spi_read, |
| 1142 | .hif_block_tx = wilc_spi_write, |
Arnd Bergmann | 7d37a4a | 2015-11-16 15:05:05 +0100 | [diff] [blame] | 1143 | .hif_read_int = wilc_spi_read_int, |
| 1144 | .hif_clear_int_ext = wilc_spi_clear_int_ext, |
| 1145 | .hif_read_size = wilc_spi_read_size, |
Glen Lee | d4312b6 | 2015-12-21 14:18:31 +0900 | [diff] [blame] | 1146 | .hif_block_tx_ext = wilc_spi_write, |
| 1147 | .hif_block_rx_ext = wilc_spi_read, |
Arnd Bergmann | 7d37a4a | 2015-11-16 15:05:05 +0100 | [diff] [blame] | 1148 | .hif_sync_ext = wilc_spi_sync_ext, |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1149 | }; |