Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2016 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 17 | #include <assert.h> |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 18 | #include <spi-generic.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | int spi_claim_bus(const struct spi_slave *slave) |
| 22 | { |
| 23 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 24 | if (ctrlr && ctrlr->claim_bus) |
| 25 | return ctrlr->claim_bus(slave); |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | void spi_release_bus(const struct spi_slave *slave) |
| 30 | { |
| 31 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 32 | if (ctrlr && ctrlr->release_bus) |
| 33 | ctrlr->release_bus(slave); |
| 34 | } |
| 35 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 36 | static int spi_xfer_single_op(const struct spi_slave *slave, |
| 37 | struct spi_op *op) |
| 38 | { |
| 39 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 40 | int ret; |
| 41 | |
| 42 | if (!ctrlr || !ctrlr->xfer) |
| 43 | return -1; |
| 44 | |
| 45 | ret = ctrlr->xfer(slave, op->dout, op->bytesout, op->din, op->bytesin); |
| 46 | if (ret) |
| 47 | op->status = SPI_OP_FAILURE; |
| 48 | else |
| 49 | op->status = SPI_OP_SUCCESS; |
| 50 | |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | static int spi_xfer_vector_default(const struct spi_slave *slave, |
| 55 | struct spi_op vectors[], size_t count) |
| 56 | { |
| 57 | size_t i; |
| 58 | int ret; |
| 59 | |
| 60 | for (i = 0; i < count; i++) { |
| 61 | ret = spi_xfer_single_op(slave, &vectors[i]); |
| 62 | if (ret) |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int spi_xfer_vector(const struct spi_slave *slave, |
| 70 | struct spi_op vectors[], size_t count) |
| 71 | { |
| 72 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 73 | |
| 74 | if (ctrlr && ctrlr->xfer_vector) |
| 75 | return ctrlr->xfer_vector(slave, vectors, count); |
| 76 | |
| 77 | return spi_xfer_vector_default(slave, vectors, count); |
| 78 | } |
| 79 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 80 | int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout, |
| 81 | void *din, size_t bytesin) |
| 82 | { |
| 83 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 84 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 85 | if (ctrlr && ctrlr->xfer) |
| 86 | return ctrlr->xfer(slave, dout, bytesout, din, bytesin); |
| 87 | |
| 88 | return -1; |
| 89 | } |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 90 | |
Furquan Shaikh | 3e01b63 | 2017-01-08 13:32:30 -0800 | [diff] [blame] | 91 | int spi_get_config(const struct spi_slave *slave, struct spi_cfg *cfg) |
| 92 | { |
| 93 | const struct spi_ctrlr *ctrlr = slave->ctrlr; |
| 94 | |
| 95 | if (ctrlr && ctrlr->get_config) |
| 96 | return ctrlr->get_config(slave, cfg); |
| 97 | |
| 98 | return -1; |
| 99 | } |
| 100 | |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 101 | void __attribute__((weak)) spi_init(void) |
| 102 | { |
| 103 | /* Default weak implementation - do nothing. */ |
| 104 | } |
| 105 | |
| 106 | const struct spi_ctrlr_buses spi_ctrlr_bus_map[0] __attribute__((weak)); |
| 107 | const size_t spi_ctrlr_bus_map_count __attribute__((weak)); |
| 108 | |
| 109 | int __attribute__((weak)) spi_setup_slave(unsigned int bus, unsigned int cs, |
| 110 | struct spi_slave *slave) |
| 111 | { |
| 112 | size_t i; |
| 113 | |
| 114 | memset(slave, 0, sizeof(*slave)); |
| 115 | |
| 116 | for (i = 0; i < spi_ctrlr_bus_map_count; i++) { |
| 117 | if ((spi_ctrlr_bus_map[i].bus_start <= bus) && |
| 118 | (spi_ctrlr_bus_map[i].bus_end >= bus)) { |
| 119 | slave->ctrlr = spi_ctrlr_bus_map[i].ctrlr; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (slave->ctrlr == NULL) |
| 125 | return -1; |
| 126 | |
| 127 | slave->bus = bus; |
| 128 | slave->cs = cs; |
| 129 | |
| 130 | if (slave->ctrlr->setup) |
| 131 | return slave->ctrlr->setup(slave); |
| 132 | |
| 133 | return 0; |
| 134 | } |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 135 | |
| 136 | static int spi_xfer_combine_two_vectors(const struct spi_slave *slave, |
| 137 | struct spi_op *v1, struct spi_op *v2) |
| 138 | { |
| 139 | struct spi_op op = { |
| 140 | .dout = v1->dout, .bytesout = v1->bytesout, |
| 141 | .din = v2->din, .bytesin = v2->bytesin, |
| 142 | }; |
| 143 | int ret; |
| 144 | |
| 145 | /* |
| 146 | * Combine two vectors only if: |
| 147 | * v1 has non-NULL dout and NULL din and |
| 148 | * v2 has non-NULL din and NULL dout and |
| 149 | * |
| 150 | * In all other cases, do not combine the two vectors. |
| 151 | */ |
| 152 | if ((!v1->dout || v1->din) || (v2->dout || !v2->din)) |
| 153 | return -1; |
| 154 | |
| 155 | ret = spi_xfer_single_op(slave, &op); |
| 156 | v1->status = v2->status = op.status; |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Helper function to allow chipsets to combine two vectors if possible. This |
| 163 | * function can only handle upto 2 vectors. |
| 164 | * |
| 165 | * Two vectors are combined if first vector has a non-NULL dout and NULL din and |
| 166 | * second vector has a non-NULL din and NULL dout. Otherwise, each vector is |
| 167 | * operated upon one at a time. |
| 168 | * |
| 169 | * Returns 0 on success and non-zero on failure. |
| 170 | */ |
| 171 | int spi_xfer_two_vectors(const struct spi_slave *slave, |
| 172 | struct spi_op vectors[], size_t count) |
| 173 | { |
| 174 | int ret; |
| 175 | |
| 176 | assert (count <= 2); |
| 177 | |
| 178 | if (count == 2) { |
| 179 | ret = spi_xfer_combine_two_vectors(slave, &vectors[0], |
| 180 | &vectors[1]); |
| 181 | |
| 182 | if (!ret || (vectors[0].status != SPI_OP_NOT_EXECUTED)) |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | return spi_xfer_vector_default(slave, vectors, count); |
| 187 | } |