Simon Glass | aaca776 | 2013-03-11 06:08:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | aaca776 | 2013-03-11 06:08:00 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 8 | #include <fdtdec.h> |
Simon Glass | aaca776 | 2013-03-11 06:08:00 +0000 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <spi.h> |
| 11 | |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 12 | int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen) |
| 13 | { |
| 14 | if (wordlen == 0 || wordlen > 32) { |
| 15 | printf("spi: invalid wordlen %d\n", wordlen); |
| 16 | return -1; |
| 17 | } |
| 18 | |
| 19 | slave->wordlen = wordlen; |
| 20 | |
| 21 | return 0; |
| 22 | } |
| 23 | |
Simon Glass | aaca776 | 2013-03-11 06:08:00 +0000 | [diff] [blame] | 24 | void *spi_do_alloc_slave(int offset, int size, unsigned int bus, |
| 25 | unsigned int cs) |
| 26 | { |
| 27 | struct spi_slave *slave; |
| 28 | void *ptr; |
| 29 | |
| 30 | ptr = malloc(size); |
| 31 | if (ptr) { |
| 32 | memset(ptr, '\0', size); |
| 33 | slave = (struct spi_slave *)(ptr + offset); |
| 34 | slave->bus = bus; |
| 35 | slave->cs = cs; |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 36 | slave->wordlen = SPI_DEFAULT_WORDLEN; |
Simon Glass | aaca776 | 2013-03-11 06:08:00 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | return ptr; |
| 40 | } |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 41 | |
| 42 | #ifdef CONFIG_OF_SPI |
| 43 | struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum, |
| 44 | int node) |
| 45 | { |
| 46 | int cs, max_hz, mode = 0; |
| 47 | |
| 48 | cs = fdtdec_get_int(blob, node, "reg", -1); |
| 49 | max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 100000); |
| 50 | if (fdtdec_get_bool(blob, node, "spi-cpol")) |
| 51 | mode |= SPI_CPOL; |
| 52 | if (fdtdec_get_bool(blob, node, "spi-cpha")) |
| 53 | mode |= SPI_CPHA; |
| 54 | if (fdtdec_get_bool(blob, node, "spi-cs-high")) |
| 55 | mode |= SPI_CS_HIGH; |
Simon Glass | ec2a323 | 2014-07-07 10:16:39 -0600 | [diff] [blame] | 56 | if (fdtdec_get_bool(blob, node, "spi-half-duplex")) |
| 57 | mode |= SPI_PREAMBLE; |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 58 | return spi_setup_slave(busnum, cs, max_hz, mode); |
| 59 | } |
| 60 | #endif |