blob: 639c9cdac51656ca259f574573aa71a5330d15e0 [file] [log] [blame]
Viresh Kumar15d651b2015-01-23 13:07:45 +05301/*
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
18struct 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 */
80struct gb_spi_mode_response {
81 __le16 mode;
82};
83
84/* flags request has no payload */
85struct gb_spi_flags_response {
86 __le16 flags;
87};
88
89/* bits-per-word request has no payload */
90struct gb_spi_bpw_response {
91 __le32 bits_per_word_mask;
92};
93
94/* num-chipselects request has no payload */
95struct 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 */
111struct 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
119struct 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
126struct gb_spi_transfer_response {
127 __u8 data[0]; /* inbound data */
128};
129
130/* Routines to transfer data */
131static struct gb_operation *
132gb_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) {
Johan Hovold25eb7322015-03-19 16:46:17 +0100146 dev_err(&connection->dev,
147 "bufferless transfer, length %u\n", xfer->len);
Viresh Kumar15d651b2015-01-23 13:07:45 +0530148 return NULL;
149 }
150
151 if (xfer->tx_buf)
152 tx_size += xfer->len;
153 if (xfer->rx_buf)
154 rx_size += xfer->len;
155
156 *total_len += xfer->len;
157 count++;
158 }
159
160 /* Too many transfers ? */
161 if (count > (u32)U16_MAX) {
Johan Hovold25eb7322015-03-19 16:46:17 +0100162 dev_err(&connection->dev, "transfer count (%u) too big\n",
163 count);
Viresh Kumar15d651b2015-01-23 13:07:45 +0530164 return NULL;
165 }
166
167 /*
168 * In addition to space for all message descriptors we need
169 * to have enough to hold all tx data.
170 */
171 request_size = sizeof(*request);
172 request_size += count * sizeof(*gb_xfer);
173 request_size += tx_size;
174
175 /* Response consists only of incoming data */
176 operation = gb_operation_create(connection, GB_SPI_TYPE_TRANSFER,
177 request_size, rx_size);
178 if (!operation)
179 return NULL;
180
181 request = operation->request->payload;
Viresh Kumarf4e6c812015-01-27 09:08:04 +0530182 request->count = cpu_to_le16(count);
Viresh Kumar15d651b2015-01-23 13:07:45 +0530183 request->mode = dev->mode;
184 request->chip_select = dev->chip_select;
185
186 gb_xfer = &request->transfers[0];
187 tx_data = gb_xfer + count; /* place tx data after last gb_xfer */
188
189 /* Fill in the transfers array */
190 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Viresh Kumarf4e6c812015-01-27 09:08:04 +0530191 gb_xfer->speed_hz = cpu_to_le32(xfer->speed_hz);
Viresh Kumar15d651b2015-01-23 13:07:45 +0530192 gb_xfer->len = cpu_to_le32(xfer->len);
193 gb_xfer->delay_usecs = cpu_to_le16(xfer->delay_usecs);
194 gb_xfer->cs_change = xfer->cs_change;
195 gb_xfer->bits_per_word = xfer->bits_per_word;
196 gb_xfer++;
197
198 /* Copy tx data */
199 if (xfer->tx_buf) {
200 memcpy(tx_data, xfer->tx_buf, xfer->len);
201 tx_data += xfer->len;
202 }
203 }
204
205 return operation;
206}
207
208static void gb_spi_decode_response(struct spi_message *msg,
209 struct gb_spi_transfer_response *response)
210{
211 struct spi_transfer *xfer;
212 void *rx_data = response->data;
213
214 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
215 /* Copy rx data */
216 if (xfer->rx_buf) {
217 memcpy(xfer->rx_buf, rx_data, xfer->len);
218 rx_data += xfer->len;
219 }
220 }
221}
222
223static int gb_spi_transfer_one_message(struct spi_master *master,
224 struct spi_message *msg)
225{
226 struct gb_spi *spi = spi_master_get_devdata(master);
227 struct gb_connection *connection = spi->connection;
228 struct gb_spi_transfer_response *response;
229 struct gb_operation *operation;
230 u32 len = 0;
231 int ret;
232
233 operation = gb_spi_operation_create(connection, msg, &len);
234 if (!operation)
235 return -ENOMEM;
236
237 ret = gb_operation_request_send_sync(operation);
238 if (!ret) {
239 response = operation->response->payload;
240 if (response)
241 gb_spi_decode_response(msg, response);
242 } else {
243 pr_err("transfer operation failed (%d)\n", ret);
244 }
245 gb_operation_destroy(operation);
246
247 msg->actual_length = len;
248 msg->status = 0;
249 spi_finalize_current_message(master);
250
251 return ret;
252}
253
254static int gb_spi_setup(struct spi_device *spi)
255{
256 /* Nothing to do for now */
257 return 0;
258}
259
260static void gb_spi_cleanup(struct spi_device *spi)
261{
262 /* Nothing to do for now */
263}
264
265
266/* Routines to get controller infomation */
267
268/* Define get_version() routine */
269define_get_version(gb_spi, SPI);
270
271/*
272 * Map Greybus spi mode bits/flags/bpw into Linux ones.
273 * All bits are same for now and so these macro's return same values.
274 */
275#define gb_spi_mode_map(mode) mode
276#define gb_spi_flags_map(flags) flags
277
278static int gb_spi_mode_operation(struct gb_spi *spi)
279{
280 struct gb_spi_mode_response response;
281 u16 mode;
282 int ret;
283
284 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_MODE,
285 NULL, 0, &response, sizeof(response));
286 if (ret)
287 return ret;
288
289 mode = le16_to_cpu(response.mode);
290 spi->mode = gb_spi_mode_map(mode);
291
292 return 0;
293}
294
295static int gb_spi_flags_operation(struct gb_spi *spi)
296{
297 struct gb_spi_flags_response response;
298 u16 flags;
299 int ret;
300
301 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_FLAGS,
302 NULL, 0, &response, sizeof(response));
303 if (ret)
304 return ret;
305
306 flags = le16_to_cpu(response.flags);
307 spi->flags = gb_spi_flags_map(flags);
308
309 return 0;
310}
311
312static int gb_spi_bpw_operation(struct gb_spi *spi)
313{
314 struct gb_spi_bpw_response response;
315 int ret;
316
317 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_BITS_PER_WORD_MASK,
318 NULL, 0, &response, sizeof(response));
319 if (ret)
320 return ret;
321
322 spi->bits_per_word_mask = le32_to_cpu(response.bits_per_word_mask);
323
324 return 0;
325}
326
327static int gb_spi_chipselect_operation(struct gb_spi *spi)
328{
329 struct gb_spi_chipselect_response response;
330 int ret;
331
332 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_NUM_CHIPSELECT,
333 NULL, 0, &response, sizeof(response));
334 if (ret)
335 return ret;
336
Viresh Kumarf4e6c812015-01-27 09:08:04 +0530337 spi->num_chipselect = le16_to_cpu(response.num_chipselect);
Viresh Kumar15d651b2015-01-23 13:07:45 +0530338
339 return 0;
340}
341
342/*
343 * Initialize the spi device. This includes verifying we can support it (based
344 * on the protocol version it advertises). If that's OK, we get and cached its
345 * mode bits & flags.
346 */
347static int gb_spi_init(struct gb_spi *spi)
348{
349 int ret;
350
351 /* First thing we need to do is check the version */
352 ret = get_version(spi);
353 if (ret)
354 return ret;
355
356 /* mode never changes, just get it once */
357 ret = gb_spi_mode_operation(spi);
358 if (ret)
359 return ret;
360
361 /* flags never changes, just get it once */
362 ret = gb_spi_flags_operation(spi);
363 if (ret)
364 return ret;
365
366 /* total number of chipselects never changes, just get it once */
367 ret = gb_spi_chipselect_operation(spi);
368 if (ret)
369 return ret;
370
371 /* bits-per-word-mask never changes, just get it once */
372 return gb_spi_bpw_operation(spi);
373}
374
375static int gb_spi_connection_init(struct gb_connection *connection)
376{
377 struct gb_spi *spi;
378 struct spi_master *master;
379 int ret;
380
381 /* Allocate master with space for data */
382 master = spi_alloc_master(&connection->dev, sizeof(*spi));
383 if (!master) {
Johan Hovold25eb7322015-03-19 16:46:17 +0100384 dev_err(&connection->dev, "cannot alloc SPI master\n");
Viresh Kumar15d651b2015-01-23 13:07:45 +0530385 return -ENOMEM;
386 }
387
388 spi = spi_master_get_devdata(master);
389 spi->connection = connection;
390 connection->private = master;
391
392 ret = gb_spi_init(spi);
393 if (ret)
394 goto out_err;
395
396 master->bus_num = 0; /* How do we get controller id here? */
397 master->num_chipselect = spi->num_chipselect;
398 master->mode_bits = spi->mode;
399 master->flags = spi->flags;
400 master->bits_per_word_mask = spi->bits_per_word_mask;
401
402 /* Attach methods */
403 master->cleanup = gb_spi_cleanup;
404 master->setup = gb_spi_setup;
405 master->transfer_one_message = gb_spi_transfer_one_message;
406
407 ret = spi_register_master(master);
408 if (!ret)
409 return 0;
410
411out_err:
412 spi_master_put(master);
413
414 return ret;
415}
416
417static void gb_spi_connection_exit(struct gb_connection *connection)
418{
419 struct spi_master *master = connection->private;
420
421 spi_unregister_master(master);
422}
423
424static struct gb_protocol spi_protocol = {
425 .name = "spi",
426 .id = GREYBUS_PROTOCOL_SPI,
427 .major = 0,
428 .minor = 1,
429 .connection_init = gb_spi_connection_init,
430 .connection_exit = gb_spi_connection_exit,
431 .request_recv = NULL,
432};
433
434int gb_spi_protocol_init(void)
435{
436 return gb_protocol_register(&spi_protocol);
437}
438
439void gb_spi_protocol_exit(void)
440{
441 gb_protocol_deregister(&spi_protocol);
442}