blob: 4fcd04c13a9c3e507f62422419ccebc915012c1e [file] [log] [blame]
Furquan Shaikh94f86992016-12-01 07:12:32 -08001/*
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
17#include <spi-generic.h>
18#include <string.h>
19
20int spi_claim_bus(const struct spi_slave *slave)
21{
22 const struct spi_ctrlr *ctrlr = slave->ctrlr;
23 if (ctrlr && ctrlr->claim_bus)
24 return ctrlr->claim_bus(slave);
25 return 0;
26}
27
28void spi_release_bus(const struct spi_slave *slave)
29{
30 const struct spi_ctrlr *ctrlr = slave->ctrlr;
31 if (ctrlr && ctrlr->release_bus)
32 ctrlr->release_bus(slave);
33}
34
35int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout,
36 void *din, size_t bytesin)
37{
38 const struct spi_ctrlr *ctrlr = slave->ctrlr;
39 if (ctrlr && ctrlr->xfer)
40 return ctrlr->xfer(slave, dout, bytesout, din, bytesin);
41
42 return -1;
43}
Furquan Shaikhb5d41cb2016-12-01 07:25:31 -080044
45void __attribute__((weak)) spi_init(void)
46{
47 /* Default weak implementation - do nothing. */
48}
49
50const struct spi_ctrlr_buses spi_ctrlr_bus_map[0] __attribute__((weak));
51const size_t spi_ctrlr_bus_map_count __attribute__((weak));
52
53int __attribute__((weak)) spi_setup_slave(unsigned int bus, unsigned int cs,
54 struct spi_slave *slave)
55{
56 size_t i;
57
58 memset(slave, 0, sizeof(*slave));
59
60 for (i = 0; i < spi_ctrlr_bus_map_count; i++) {
61 if ((spi_ctrlr_bus_map[i].bus_start <= bus) &&
62 (spi_ctrlr_bus_map[i].bus_end >= bus)) {
63 slave->ctrlr = spi_ctrlr_bus_map[i].ctrlr;
64 break;
65 }
66 }
67
68 if (slave->ctrlr == NULL)
69 return -1;
70
71 slave->bus = bus;
72 slave->cs = cs;
73
74 if (slave->ctrlr->setup)
75 return slave->ctrlr->setup(slave);
76
77 return 0;
78}