Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 2 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 3 | #include <assert.h> |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 4 | #include <commonlib/helpers.h> |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 5 | #include <spi-generic.h> |
Elyes HAOUAS | ede8dd0 | 2019-06-23 06:57:53 +0200 | [diff] [blame] | 6 | #include <stddef.h> |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 7 | #include <string.h> |
| 8 | |
| 9 | int spi_claim_bus(const struct spi_slave *slave) |
| 10 | { |
| 11 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 12 | if (ctrlr && ctrlr->claim_bus) |
| 13 | return ctrlr->claim_bus(slave); |
| 14 | return 0; |
| 15 | } |
| 16 | |
| 17 | void spi_release_bus(const struct spi_slave *slave) |
| 18 | { |
| 19 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 20 | if (ctrlr && ctrlr->release_bus) |
| 21 | ctrlr->release_bus(slave); |
| 22 | } |
| 23 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 24 | static int spi_xfer_single_op(const struct spi_slave *slave, |
| 25 | struct spi_op *op) |
| 26 | { |
| 27 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 28 | int ret; |
| 29 | |
| 30 | if (!ctrlr || !ctrlr->xfer) |
| 31 | return -1; |
| 32 | |
| 33 | ret = ctrlr->xfer(slave, op->dout, op->bytesout, op->din, op->bytesin); |
| 34 | if (ret) |
| 35 | op->status = SPI_OP_FAILURE; |
| 36 | else |
| 37 | op->status = SPI_OP_SUCCESS; |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | static int spi_xfer_vector_default(const struct spi_slave *slave, |
| 43 | struct spi_op vectors[], size_t count) |
| 44 | { |
| 45 | size_t i; |
| 46 | int ret; |
| 47 | |
| 48 | for (i = 0; i < count; i++) { |
| 49 | ret = spi_xfer_single_op(slave, &vectors[i]); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | int spi_xfer_vector(const struct spi_slave *slave, |
| 58 | struct spi_op vectors[], size_t count) |
| 59 | { |
| 60 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 61 | |
| 62 | if (ctrlr && ctrlr->xfer_vector) |
| 63 | return ctrlr->xfer_vector(slave, vectors, count); |
| 64 | |
| 65 | return spi_xfer_vector_default(slave, vectors, count); |
| 66 | } |
| 67 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 68 | int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout, |
| 69 | void *din, size_t bytesin) |
| 70 | { |
| 71 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 72 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 73 | if (ctrlr && ctrlr->xfer) |
| 74 | return ctrlr->xfer(slave, dout, bytesout, din, bytesin); |
| 75 | |
| 76 | return -1; |
| 77 | } |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 78 | |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 79 | unsigned int spi_crop_chunk(const struct spi_slave *slave, unsigned int cmd_len, |
| 80 | unsigned int buf_len) |
| 81 | { |
| 82 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 83 | unsigned int ctrlr_max; |
Aaron Durbin | 1fcc9f3 | 2018-01-29 11:30:17 -0700 | [diff] [blame] | 84 | bool deduct_cmd_len; |
| 85 | bool deduct_opcode_len; |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 86 | |
| 87 | if (!ctrlr) |
| 88 | return 0; |
| 89 | |
Aaron Durbin | 1fcc9f3 | 2018-01-29 11:30:17 -0700 | [diff] [blame] | 90 | deduct_cmd_len = !!(ctrlr->flags & SPI_CNTRLR_DEDUCT_CMD_LEN); |
| 91 | deduct_opcode_len = !!(ctrlr->flags & SPI_CNTRLR_DEDUCT_OPCODE_LEN); |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 92 | ctrlr_max = ctrlr->max_xfer_size; |
| 93 | |
Elyes Haouas | 78d2469 | 2023-09-10 10:41:55 +0200 | [diff] [blame^] | 94 | assert(ctrlr_max != 0); |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 95 | |
Aaron Durbin | 1fcc9f3 | 2018-01-29 11:30:17 -0700 | [diff] [blame] | 96 | /* Assume opcode is always one byte and deduct it from the cmd_len |
| 97 | as the hardware has a separate register for the opcode. */ |
| 98 | if (deduct_opcode_len) |
| 99 | cmd_len--; |
| 100 | |
Elyes Haouas | 7865ce8 | 2023-08-26 16:52:56 +0200 | [diff] [blame] | 101 | /* Subtract command length from usable buffer size. If |
Felix Held | 55dce1d | 2021-12-10 18:38:16 +0100 | [diff] [blame] | 102 | deduct_opcode_len is set, only subtract the number command bytes |
| 103 | after the opcode. If the adjusted cmd_len is larger than ctrlr_max |
| 104 | return 0 to inidicate an error. */ |
Felix Held | e3ae755 | 2021-12-10 18:38:16 +0100 | [diff] [blame] | 105 | if (deduct_cmd_len) { |
| 106 | if (ctrlr_max >= cmd_len) { |
| 107 | ctrlr_max -= cmd_len; |
| 108 | } else { |
| 109 | ctrlr_max = 0; |
| 110 | printk(BIOS_WARNING, "%s: Command longer than buffer\n", __func__); |
| 111 | } |
| 112 | } |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 113 | |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 114 | return MIN(ctrlr_max, buf_len); |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 117 | void __weak spi_init(void) |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 118 | { |
| 119 | /* Default weak implementation - do nothing. */ |
| 120 | } |
| 121 | |
Furquan Shaikh | dd8d247 | 2017-05-18 15:08:51 -0700 | [diff] [blame] | 122 | int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave) |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 123 | { |
| 124 | size_t i; |
| 125 | |
| 126 | memset(slave, 0, sizeof(*slave)); |
| 127 | |
| 128 | for (i = 0; i < spi_ctrlr_bus_map_count; i++) { |
| 129 | if ((spi_ctrlr_bus_map[i].bus_start <= bus) && |
| 130 | (spi_ctrlr_bus_map[i].bus_end >= bus)) { |
| 131 | slave->ctrlr = spi_ctrlr_bus_map[i].ctrlr; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
Julius Werner | c1b98a4 | 2021-09-10 19:08:48 -0700 | [diff] [blame] | 136 | if (slave->ctrlr == NULL) { |
| 137 | printk(BIOS_ERR, "Can't find SPI bus %u\n", bus); |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 138 | return -1; |
Julius Werner | c1b98a4 | 2021-09-10 19:08:48 -0700 | [diff] [blame] | 139 | } |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 140 | |
| 141 | slave->bus = bus; |
| 142 | slave->cs = cs; |
| 143 | |
| 144 | if (slave->ctrlr->setup) |
| 145 | return slave->ctrlr->setup(slave); |
| 146 | |
| 147 | return 0; |
| 148 | } |