paul@0 | 1 | /* |
paul@0 | 2 | * Export JZ4740 GPIO pins as a SPI server. |
paul@0 | 3 | * |
paul@152 | 4 | * Copyright (C) 2018, 2020, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/devices/gpio-jz4740.h> |
paul@199 | 23 | #include <l4/devices/spi-gpio.h> |
paul@105 | 24 | #include <l4/devices/memory.h> |
paul@0 | 25 | |
paul@105 | 26 | #include <l4/re/env.h> |
paul@0 | 27 | |
paul@0 | 28 | #include <errno.h> |
paul@0 | 29 | #include <stdint.h> |
paul@0 | 30 | #include <stdlib.h> |
paul@0 | 31 | |
paul@105 | 32 | #include <ipc/server.h> |
paul@152 | 33 | |
paul@105 | 34 | #include "spi_server.h" |
paul@0 | 35 | |
paul@152 | 36 | |
paul@152 | 37 | |
paul@0 | 38 | /* Virtual addresses for the GPIO register block. */ |
paul@0 | 39 | |
paul@0 | 40 | static l4_addr_t gpio_virt_base = 0, gpio_virt_base_end = 0; |
paul@0 | 41 | |
paul@0 | 42 | |
paul@0 | 43 | |
paul@0 | 44 | static int setup_memory(void) |
paul@0 | 45 | { |
paul@0 | 46 | if (get_memory("jz4740-gpio", &gpio_virt_base, &gpio_virt_base_end)) |
paul@0 | 47 | return 1; |
paul@0 | 48 | |
paul@0 | 49 | return 0; |
paul@0 | 50 | } |
paul@0 | 51 | |
paul@0 | 52 | |
paul@0 | 53 | |
paul@0 | 54 | /* SPI peripheral device. */ |
paul@0 | 55 | |
paul@152 | 56 | class SPI_server : public SPI |
paul@0 | 57 | { |
paul@199 | 58 | Spi_gpio *_spi; |
paul@0 | 59 | |
paul@0 | 60 | public: |
paul@0 | 61 | /* Associate the device with a particular memory region. */ |
paul@0 | 62 | |
paul@199 | 63 | explicit SPI_server(Spi_gpio *spi) |
paul@199 | 64 | : _spi(spi) |
paul@0 | 65 | { |
paul@0 | 66 | } |
paul@0 | 67 | |
paul@0 | 68 | /* Send a SPI command. */ |
paul@0 | 69 | |
paul@105 | 70 | long send(int bits, int data) |
paul@0 | 71 | { |
paul@222 | 72 | uint32_t bytes = (bits + 7) / 8; |
paul@199 | 73 | uint8_t buffer[bytes]; |
paul@0 | 74 | |
paul@199 | 75 | /* Convert the data into a sequence of bytes. */ |
paul@0 | 76 | |
paul@199 | 77 | for (int byte = bytes; byte > 0; byte--) |
paul@199 | 78 | buffer[byte - 1] = (data >> ((byte - 1) * 8)) & 0xff; |
paul@0 | 79 | |
paul@199 | 80 | _spi->send(bytes, buffer); |
paul@105 | 81 | return L4_EOK; |
paul@0 | 82 | } |
paul@0 | 83 | }; |
paul@0 | 84 | |
paul@0 | 85 | |
paul@0 | 86 | |
paul@0 | 87 | /* |
paul@0 | 88 | Parse a string of the form "<port><pin>" where <port> is a single character |
paul@0 | 89 | whose position in the ports string indicates the port number, and where <pin> is |
paul@0 | 90 | either a base 10 number, a base 8 number preceded by "0", or a base 16 number |
paul@0 | 91 | preceded by "0x" or "0X", defined by the strtol library function. |
paul@0 | 92 | */ |
paul@0 | 93 | |
paul@0 | 94 | static int parse_pin(const char *s, const char *ports, int *port, int *pin) |
paul@0 | 95 | { |
paul@0 | 96 | int i = 0; |
paul@0 | 97 | |
paul@0 | 98 | if (!s || !(*s)) return 0; |
paul@0 | 99 | |
paul@0 | 100 | /* Parse prefix in character range. */ |
paul@0 | 101 | |
paul@0 | 102 | while (*ports) |
paul@0 | 103 | { |
paul@0 | 104 | if (s[0] == *ports) |
paul@0 | 105 | { |
paul@0 | 106 | *port = i; |
paul@0 | 107 | |
paul@0 | 108 | /* Parse pin number. */ |
paul@0 | 109 | |
paul@0 | 110 | *pin = (int) strtol(s+1, NULL, 0); |
paul@0 | 111 | return !errno; |
paul@0 | 112 | } |
paul@0 | 113 | ports++; i++; |
paul@0 | 114 | } |
paul@0 | 115 | |
paul@0 | 116 | return 0; |
paul@0 | 117 | } |
paul@0 | 118 | |
paul@0 | 119 | /* Arguments: <SPI clock pin> <SPI data pin> <SPI enable pin> */ |
paul@0 | 120 | |
paul@0 | 121 | int main(int argc, char *argv[]) |
paul@0 | 122 | { |
paul@0 | 123 | int clock_port, clock_pin, data_port, data_pin, enable_port, enable_pin; |
paul@0 | 124 | |
paul@152 | 125 | if (argc < 4) |
paul@152 | 126 | return 1; |
paul@0 | 127 | |
paul@0 | 128 | /* Interpret the pin details. */ |
paul@0 | 129 | |
paul@152 | 130 | if (!parse_pin(argv[1], "ABCD", &clock_port, &clock_pin)) |
paul@152 | 131 | return 1; |
paul@152 | 132 | if (!parse_pin(argv[2], "ABCD", &data_port, &data_pin)) |
paul@152 | 133 | return 1; |
paul@152 | 134 | if (!parse_pin(argv[3], "ABCD", &enable_port, &enable_pin)) |
paul@152 | 135 | return 1; |
paul@0 | 136 | |
paul@0 | 137 | /* Obtain access to peripheral memory. */ |
paul@0 | 138 | |
paul@152 | 139 | if (setup_memory()) |
paul@152 | 140 | return 1; |
paul@0 | 141 | |
paul@0 | 142 | /* Configure the clock pin. */ |
paul@0 | 143 | |
paul@0 | 144 | Gpio_jz4740_chip gpio_port_clock(gpio_virt_base + clock_port * 0x100, |
paul@0 | 145 | gpio_virt_base + (clock_port + 1) * 0x100, 32); |
paul@0 | 146 | |
paul@0 | 147 | gpio_port_clock.setup(clock_pin, Hw::Gpio_chip::Output, 0); |
paul@0 | 148 | |
paul@0 | 149 | /* Configure the data pin. */ |
paul@0 | 150 | |
paul@0 | 151 | Gpio_jz4740_chip gpio_port_data(gpio_virt_base + data_port * 0x100, |
paul@0 | 152 | gpio_virt_base + (data_port + 1) * 0x100, 32); |
paul@0 | 153 | |
paul@0 | 154 | gpio_port_data.setup(data_pin, Hw::Gpio_chip::Output, 0); |
paul@0 | 155 | |
paul@0 | 156 | /* Configure the enable pin. */ |
paul@0 | 157 | |
paul@0 | 158 | Gpio_jz4740_chip gpio_port_enable(gpio_virt_base + enable_port * 0x100, |
paul@0 | 159 | gpio_virt_base + (enable_port + 1) * 0x100, 32); |
paul@0 | 160 | |
paul@0 | 161 | gpio_port_enable.setup(enable_pin, Hw::Gpio_chip::Output, 0); |
paul@0 | 162 | |
paul@222 | 163 | /* Create an object for SPI communication omitting any specific frequency. */ |
paul@199 | 164 | |
paul@199 | 165 | Spi_gpio spi(&gpio_port_clock, clock_pin, &gpio_port_data, data_pin, |
paul@199 | 166 | &gpio_port_enable, enable_pin); |
paul@199 | 167 | |
paul@0 | 168 | /* Initialise and register a new server object. */ |
paul@0 | 169 | |
paul@199 | 170 | SPI_server obj(&spi); |
paul@0 | 171 | |
paul@152 | 172 | /* Bind and start the IPC server loop. */ |
paul@0 | 173 | |
paul@152 | 174 | if (ipc_server_loop_for(SPI, &obj, "spi")) |
paul@152 | 175 | return 1; |
paul@105 | 176 | |
paul@0 | 177 | return 0; |
paul@0 | 178 | } |