Viresh Kumar | 15d651b | 2015-01-23 13:07:45 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPI bridge driver for the Greybus "generic" SPI module. |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * Copyright 2014 Linaro Ltd. |
| 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/bitops.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/spi/spi.h> |
| 15 | |
| 16 | #include "greybus.h" |
| 17 | |
| 18 | struct gb_spi { |
| 19 | struct gb_connection *connection; |
| 20 | u8 version_major; |
| 21 | u8 version_minor; |
| 22 | |
| 23 | /* Modes supported by spi controller */ |
| 24 | u16 mode; |
| 25 | /* constraints of the spi controller */ |
| 26 | u16 flags; |
| 27 | |
| 28 | /* |
| 29 | * copied from kernel: |
| 30 | * |
| 31 | * A mask indicating which values of bits_per_word are supported by the |
| 32 | * controller. Bit n indicates that a bits_per_word n+1 is suported. If |
| 33 | * set, the SPI core will reject any transfer with an unsupported |
| 34 | * bits_per_word. If not set, this value is simply ignored, and it's up |
| 35 | * to the individual driver to perform any validation. |
| 36 | */ |
| 37 | u32 bits_per_word_mask; |
| 38 | |
| 39 | /* |
| 40 | * chipselects will be integral to many controllers; some others might |
| 41 | * use board-specific GPIOs. |
| 42 | */ |
| 43 | u16 num_chipselect; |
| 44 | }; |
| 45 | |
| 46 | /* Version of the Greybus spi protocol we support */ |
| 47 | #define GB_SPI_VERSION_MAJOR 0x00 |
| 48 | #define GB_SPI_VERSION_MINOR 0x01 |
| 49 | |
| 50 | /* Should match up with modes in linux/spi/spi.h */ |
| 51 | #define GB_SPI_MODE_CPHA 0x01 /* clock phase */ |
| 52 | #define GB_SPI_MODE_CPOL 0x02 /* clock polarity */ |
| 53 | #define GB_SPI_MODE_MODE_0 (0|0) /* (original MicroWire) */ |
| 54 | #define GB_SPI_MODE_MODE_1 (0|GB_SPI_MODE_CPHA) |
| 55 | #define GB_SPI_MODE_MODE_2 (GB_SPI_MODE_CPOL|0) |
| 56 | #define GB_SPI_MODE_MODE_3 (GB_SPI_MODE_CPOL|GB_SPI_MODE_CPHA) |
| 57 | #define GB_SPI_MODE_CS_HIGH 0x04 /* chipselect active high? */ |
| 58 | #define GB_SPI_MODE_LSB_FIRST 0x08 /* per-word bits-on-wire */ |
| 59 | #define GB_SPI_MODE_3WIRE 0x10 /* SI/SO signals shared */ |
| 60 | #define GB_SPI_MODE_LOOP 0x20 /* loopback mode */ |
| 61 | #define GB_SPI_MODE_NO_CS 0x40 /* 1 dev/bus, no chipselect */ |
| 62 | #define GB_SPI_MODE_READY 0x80 /* slave pulls low to pause */ |
| 63 | |
| 64 | /* Should match up with flags in linux/spi/spi.h */ |
| 65 | #define GB_SPI_FLAG_HALF_DUPLEX BIT(0) /* can't do full duplex */ |
| 66 | #define GB_SPI_FLAG_NO_RX BIT(1) /* can't do buffer read */ |
| 67 | #define GB_SPI_FLAG_NO_TX BIT(2) /* can't do buffer write */ |
| 68 | |
| 69 | /* Greybus spi request types */ |
| 70 | #define GB_SPI_TYPE_INVALID 0x00 |
| 71 | #define GB_SPI_TYPE_PROTOCOL_VERSION 0x01 |
| 72 | #define GB_SPI_TYPE_MODE 0x02 |
| 73 | #define GB_SPI_TYPE_FLAGS 0x03 |
| 74 | #define GB_SPI_TYPE_BITS_PER_WORD_MASK 0x04 |
| 75 | #define GB_SPI_TYPE_NUM_CHIPSELECT 0x05 |
| 76 | #define GB_SPI_TYPE_TRANSFER 0x06 |
| 77 | #define GB_SPI_TYPE_RESPONSE 0x80 /* OR'd with rest */ |
| 78 | |
| 79 | /* mode request has no payload */ |
| 80 | struct gb_spi_mode_response { |
| 81 | __le16 mode; |
| 82 | }; |
| 83 | |
| 84 | /* flags request has no payload */ |
| 85 | struct gb_spi_flags_response { |
| 86 | __le16 flags; |
| 87 | }; |
| 88 | |
| 89 | /* bits-per-word request has no payload */ |
| 90 | struct gb_spi_bpw_response { |
| 91 | __le32 bits_per_word_mask; |
| 92 | }; |
| 93 | |
| 94 | /* num-chipselects request has no payload */ |
| 95 | struct gb_spi_chipselect_response { |
| 96 | __le16 num_chipselect; |
| 97 | }; |
| 98 | |
| 99 | /** |
| 100 | * struct gb_spi_transfer - a read/write buffer pair |
| 101 | * @speed_hz: Select a speed other than the device default for this transfer. If |
| 102 | * 0 the default (from @spi_device) is used. |
| 103 | * @len: size of rx and tx buffers (in bytes) |
| 104 | * @delay_usecs: microseconds to delay after this transfer before (optionally) |
| 105 | * changing the chipselect status, then starting the next transfer or |
| 106 | * completing this spi_message. |
| 107 | * @cs_change: affects chipselect after this transfer completes |
| 108 | * @bits_per_word: select a bits_per_word other than the device default for this |
| 109 | * transfer. If 0 the default (from @spi_device) is used. |
| 110 | */ |
| 111 | struct gb_spi_transfer { |
| 112 | __le32 speed_hz; |
| 113 | __le32 len; |
| 114 | __le16 delay_usecs; |
| 115 | __u8 cs_change; |
| 116 | __u8 bits_per_word; |
| 117 | }; |
| 118 | |
| 119 | struct gb_spi_transfer_request { |
| 120 | __u8 chip_select; /* of the spi device */ |
| 121 | __u8 mode; /* of the spi device */ |
| 122 | __le16 count; |
| 123 | struct gb_spi_transfer transfers[0]; /* trnasfer_count of these */ |
| 124 | }; |
| 125 | |
| 126 | struct gb_spi_transfer_response { |
| 127 | __u8 data[0]; /* inbound data */ |
| 128 | }; |
| 129 | |
| 130 | /* Routines to transfer data */ |
| 131 | static struct gb_operation * |
| 132 | gb_spi_operation_create(struct gb_connection *connection, |
| 133 | struct spi_message *msg, u32 *total_len) |
| 134 | { |
| 135 | struct gb_spi_transfer_request *request; |
| 136 | struct spi_device *dev = msg->spi; |
| 137 | struct spi_transfer *xfer; |
| 138 | struct gb_spi_transfer *gb_xfer; |
| 139 | struct gb_operation *operation; |
| 140 | u32 tx_size = 0, rx_size = 0, count = 0, request_size; |
| 141 | void *tx_data; |
| 142 | |
| 143 | /* Find number of transfers queued and tx/rx length in the message */ |
| 144 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 145 | if (!xfer->tx_buf && !xfer->rx_buf) { |
| 146 | gb_connection_err(connection, |
| 147 | "Bufferless transfer, length %u\n", |
| 148 | xfer->len); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | if (xfer->tx_buf) |
| 153 | tx_size += xfer->len; |
| 154 | if (xfer->rx_buf) |
| 155 | rx_size += xfer->len; |
| 156 | |
| 157 | *total_len += xfer->len; |
| 158 | count++; |
| 159 | } |
| 160 | |
| 161 | /* Too many transfers ? */ |
| 162 | if (count > (u32)U16_MAX) { |
| 163 | gb_connection_err(connection, "transfer count (%u) too big", |
| 164 | count); |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * In addition to space for all message descriptors we need |
| 170 | * to have enough to hold all tx data. |
| 171 | */ |
| 172 | request_size = sizeof(*request); |
| 173 | request_size += count * sizeof(*gb_xfer); |
| 174 | request_size += tx_size; |
| 175 | |
| 176 | /* Response consists only of incoming data */ |
| 177 | operation = gb_operation_create(connection, GB_SPI_TYPE_TRANSFER, |
| 178 | request_size, rx_size); |
| 179 | if (!operation) |
| 180 | return NULL; |
| 181 | |
| 182 | request = operation->request->payload; |
Viresh Kumar | f4e6c81 | 2015-01-27 09:08:04 +0530 | [diff] [blame^] | 183 | request->count = cpu_to_le16(count); |
Viresh Kumar | 15d651b | 2015-01-23 13:07:45 +0530 | [diff] [blame] | 184 | request->mode = dev->mode; |
| 185 | request->chip_select = dev->chip_select; |
| 186 | |
| 187 | gb_xfer = &request->transfers[0]; |
| 188 | tx_data = gb_xfer + count; /* place tx data after last gb_xfer */ |
| 189 | |
| 190 | /* Fill in the transfers array */ |
| 191 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Viresh Kumar | f4e6c81 | 2015-01-27 09:08:04 +0530 | [diff] [blame^] | 192 | gb_xfer->speed_hz = cpu_to_le32(xfer->speed_hz); |
Viresh Kumar | 15d651b | 2015-01-23 13:07:45 +0530 | [diff] [blame] | 193 | gb_xfer->len = cpu_to_le32(xfer->len); |
| 194 | gb_xfer->delay_usecs = cpu_to_le16(xfer->delay_usecs); |
| 195 | gb_xfer->cs_change = xfer->cs_change; |
| 196 | gb_xfer->bits_per_word = xfer->bits_per_word; |
| 197 | gb_xfer++; |
| 198 | |
| 199 | /* Copy tx data */ |
| 200 | if (xfer->tx_buf) { |
| 201 | memcpy(tx_data, xfer->tx_buf, xfer->len); |
| 202 | tx_data += xfer->len; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return operation; |
| 207 | } |
| 208 | |
| 209 | static void gb_spi_decode_response(struct spi_message *msg, |
| 210 | struct gb_spi_transfer_response *response) |
| 211 | { |
| 212 | struct spi_transfer *xfer; |
| 213 | void *rx_data = response->data; |
| 214 | |
| 215 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 216 | /* Copy rx data */ |
| 217 | if (xfer->rx_buf) { |
| 218 | memcpy(xfer->rx_buf, rx_data, xfer->len); |
| 219 | rx_data += xfer->len; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static int gb_spi_transfer_one_message(struct spi_master *master, |
| 225 | struct spi_message *msg) |
| 226 | { |
| 227 | struct gb_spi *spi = spi_master_get_devdata(master); |
| 228 | struct gb_connection *connection = spi->connection; |
| 229 | struct gb_spi_transfer_response *response; |
| 230 | struct gb_operation *operation; |
| 231 | u32 len = 0; |
| 232 | int ret; |
| 233 | |
| 234 | operation = gb_spi_operation_create(connection, msg, &len); |
| 235 | if (!operation) |
| 236 | return -ENOMEM; |
| 237 | |
| 238 | ret = gb_operation_request_send_sync(operation); |
| 239 | if (!ret) { |
| 240 | response = operation->response->payload; |
| 241 | if (response) |
| 242 | gb_spi_decode_response(msg, response); |
| 243 | } else { |
| 244 | pr_err("transfer operation failed (%d)\n", ret); |
| 245 | } |
| 246 | gb_operation_destroy(operation); |
| 247 | |
| 248 | msg->actual_length = len; |
| 249 | msg->status = 0; |
| 250 | spi_finalize_current_message(master); |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | static int gb_spi_setup(struct spi_device *spi) |
| 256 | { |
| 257 | /* Nothing to do for now */ |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static void gb_spi_cleanup(struct spi_device *spi) |
| 262 | { |
| 263 | /* Nothing to do for now */ |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /* Routines to get controller infomation */ |
| 268 | |
| 269 | /* Define get_version() routine */ |
| 270 | define_get_version(gb_spi, SPI); |
| 271 | |
| 272 | /* |
| 273 | * Map Greybus spi mode bits/flags/bpw into Linux ones. |
| 274 | * All bits are same for now and so these macro's return same values. |
| 275 | */ |
| 276 | #define gb_spi_mode_map(mode) mode |
| 277 | #define gb_spi_flags_map(flags) flags |
| 278 | |
| 279 | static int gb_spi_mode_operation(struct gb_spi *spi) |
| 280 | { |
| 281 | struct gb_spi_mode_response response; |
| 282 | u16 mode; |
| 283 | int ret; |
| 284 | |
| 285 | ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_MODE, |
| 286 | NULL, 0, &response, sizeof(response)); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | |
| 290 | mode = le16_to_cpu(response.mode); |
| 291 | spi->mode = gb_spi_mode_map(mode); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int gb_spi_flags_operation(struct gb_spi *spi) |
| 297 | { |
| 298 | struct gb_spi_flags_response response; |
| 299 | u16 flags; |
| 300 | int ret; |
| 301 | |
| 302 | ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_FLAGS, |
| 303 | NULL, 0, &response, sizeof(response)); |
| 304 | if (ret) |
| 305 | return ret; |
| 306 | |
| 307 | flags = le16_to_cpu(response.flags); |
| 308 | spi->flags = gb_spi_flags_map(flags); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int gb_spi_bpw_operation(struct gb_spi *spi) |
| 314 | { |
| 315 | struct gb_spi_bpw_response response; |
| 316 | int ret; |
| 317 | |
| 318 | ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_BITS_PER_WORD_MASK, |
| 319 | NULL, 0, &response, sizeof(response)); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | |
| 323 | spi->bits_per_word_mask = le32_to_cpu(response.bits_per_word_mask); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int gb_spi_chipselect_operation(struct gb_spi *spi) |
| 329 | { |
| 330 | struct gb_spi_chipselect_response response; |
| 331 | int ret; |
| 332 | |
| 333 | ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_NUM_CHIPSELECT, |
| 334 | NULL, 0, &response, sizeof(response)); |
| 335 | if (ret) |
| 336 | return ret; |
| 337 | |
Viresh Kumar | f4e6c81 | 2015-01-27 09:08:04 +0530 | [diff] [blame^] | 338 | spi->num_chipselect = le16_to_cpu(response.num_chipselect); |
Viresh Kumar | 15d651b | 2015-01-23 13:07:45 +0530 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Initialize the spi device. This includes verifying we can support it (based |
| 345 | * on the protocol version it advertises). If that's OK, we get and cached its |
| 346 | * mode bits & flags. |
| 347 | */ |
| 348 | static int gb_spi_init(struct gb_spi *spi) |
| 349 | { |
| 350 | int ret; |
| 351 | |
| 352 | /* First thing we need to do is check the version */ |
| 353 | ret = get_version(spi); |
| 354 | if (ret) |
| 355 | return ret; |
| 356 | |
| 357 | /* mode never changes, just get it once */ |
| 358 | ret = gb_spi_mode_operation(spi); |
| 359 | if (ret) |
| 360 | return ret; |
| 361 | |
| 362 | /* flags never changes, just get it once */ |
| 363 | ret = gb_spi_flags_operation(spi); |
| 364 | if (ret) |
| 365 | return ret; |
| 366 | |
| 367 | /* total number of chipselects never changes, just get it once */ |
| 368 | ret = gb_spi_chipselect_operation(spi); |
| 369 | if (ret) |
| 370 | return ret; |
| 371 | |
| 372 | /* bits-per-word-mask never changes, just get it once */ |
| 373 | return gb_spi_bpw_operation(spi); |
| 374 | } |
| 375 | |
| 376 | static int gb_spi_connection_init(struct gb_connection *connection) |
| 377 | { |
| 378 | struct gb_spi *spi; |
| 379 | struct spi_master *master; |
| 380 | int ret; |
| 381 | |
| 382 | /* Allocate master with space for data */ |
| 383 | master = spi_alloc_master(&connection->dev, sizeof(*spi)); |
| 384 | if (!master) { |
| 385 | gb_connection_err(connection, "cannot alloc SPI master\n"); |
| 386 | return -ENOMEM; |
| 387 | } |
| 388 | |
| 389 | spi = spi_master_get_devdata(master); |
| 390 | spi->connection = connection; |
| 391 | connection->private = master; |
| 392 | |
| 393 | ret = gb_spi_init(spi); |
| 394 | if (ret) |
| 395 | goto out_err; |
| 396 | |
| 397 | master->bus_num = 0; /* How do we get controller id here? */ |
| 398 | master->num_chipselect = spi->num_chipselect; |
| 399 | master->mode_bits = spi->mode; |
| 400 | master->flags = spi->flags; |
| 401 | master->bits_per_word_mask = spi->bits_per_word_mask; |
| 402 | |
| 403 | /* Attach methods */ |
| 404 | master->cleanup = gb_spi_cleanup; |
| 405 | master->setup = gb_spi_setup; |
| 406 | master->transfer_one_message = gb_spi_transfer_one_message; |
| 407 | |
| 408 | ret = spi_register_master(master); |
| 409 | if (!ret) |
| 410 | return 0; |
| 411 | |
| 412 | out_err: |
| 413 | spi_master_put(master); |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static void gb_spi_connection_exit(struct gb_connection *connection) |
| 419 | { |
| 420 | struct spi_master *master = connection->private; |
| 421 | |
| 422 | spi_unregister_master(master); |
| 423 | } |
| 424 | |
| 425 | static struct gb_protocol spi_protocol = { |
| 426 | .name = "spi", |
| 427 | .id = GREYBUS_PROTOCOL_SPI, |
| 428 | .major = 0, |
| 429 | .minor = 1, |
| 430 | .connection_init = gb_spi_connection_init, |
| 431 | .connection_exit = gb_spi_connection_exit, |
| 432 | .request_recv = NULL, |
| 433 | }; |
| 434 | |
| 435 | int gb_spi_protocol_init(void) |
| 436 | { |
| 437 | return gb_protocol_register(&spi_protocol); |
| 438 | } |
| 439 | |
| 440 | void gb_spi_protocol_exit(void) |
| 441 | { |
| 442 | gb_protocol_deregister(&spi_protocol); |
| 443 | } |