blob: dea8dcda5b08bd0875aa98551376184f08fc2735 [file] [log] [blame]
Simon Glassaaca7762013-03-11 06:08:00 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glassaaca7762013-03-11 06:08:00 +00005 */
6
7#include <common.h>
Simon Glass49e9d2c2013-12-03 16:43:24 -07008#include <fdtdec.h>
Simon Glassaaca7762013-03-11 06:08:00 +00009#include <malloc.h>
10#include <spi.h>
11
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030012int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen)
13{
14 if (wordlen == 0 || wordlen > 32) {
Mario Six2eb66c22018-01-15 11:08:35 +010015 printf("spi: invalid wordlen %u\n", wordlen);
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030016 return -1;
17 }
18
19 slave->wordlen = wordlen;
20
21 return 0;
22}
23
Simon Glassaaca7762013-03-11 06:08:00 +000024void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
25 unsigned int cs)
26{
Mario Six2eb66c22018-01-15 11:08:35 +010027 u8 *ptr;
Simon Glassaaca7762013-03-11 06:08:00 +000028
29 ptr = malloc(size);
30 if (ptr) {
Mario Six2eb66c22018-01-15 11:08:35 +010031 struct spi_slave *slave;
32
Simon Glassaaca7762013-03-11 06:08:00 +000033 memset(ptr, '\0', size);
34 slave = (struct spi_slave *)(ptr + offset);
35 slave->bus = bus;
36 slave->cs = cs;
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030037 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glassaaca7762013-03-11 06:08:00 +000038 }
39
40 return ptr;
41}
Simon Glass49e9d2c2013-12-03 16:43:24 -070042
43#ifdef CONFIG_OF_SPI
44struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
45 int node)
46{
47 int cs, max_hz, mode = 0;
48
49 cs = fdtdec_get_int(blob, node, "reg", -1);
50 max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 100000);
51 if (fdtdec_get_bool(blob, node, "spi-cpol"))
52 mode |= SPI_CPOL;
53 if (fdtdec_get_bool(blob, node, "spi-cpha"))
54 mode |= SPI_CPHA;
55 if (fdtdec_get_bool(blob, node, "spi-cs-high"))
56 mode |= SPI_CS_HIGH;
Simon Glassec2a3232014-07-07 10:16:39 -060057 if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
58 mode |= SPI_PREAMBLE;
Simon Glass49e9d2c2013-12-03 16:43:24 -070059 return spi_setup_slave(busnum, cs, max_hz, mode);
60}
61#endif