Browse Source

projects/riscv_usb: Add driver for the SPI IP

along with some basic flash commands.

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
Sylvain Munaut 5 years ago
parent
commit
8d8eee979f

+ 2 - 0
projects/riscv_usb/fw/Makefile

@@ -10,6 +10,7 @@ HEADERS=\
 	console.h \
 	console.h \
 	led.h \
 	led.h \
 	mini-printf.h \
 	mini-printf.h \
+	spi.h \
 	usb_priv.h \
 	usb_priv.h \
 	usb_desc_data.h
 	usb_desc_data.h
 
 
@@ -19,6 +20,7 @@ SOURCES=\
 	firmware.c \
 	firmware.c \
 	led.c \
 	led.c \
 	mini-printf.c  \
 	mini-printf.c  \
+	spi.c \
 	usb.c \
 	usb.c \
 	usb_ep0.c \
 	usb_ep0.c \
 	usb_desc.c
 	usb_desc.c

+ 1 - 0
projects/riscv_usb/fw/config.h

@@ -24,6 +24,7 @@
 #pragma once
 #pragma once
 
 
 #define UART_BASE	0x81000000
 #define UART_BASE	0x81000000
+#define SPI_BASE	0x82000000
 #define LED_BASE	0x83000000
 #define LED_BASE	0x83000000
 #define USB_CORE_BASE	0x84000000
 #define USB_CORE_BASE	0x84000000
 #define USB_DATA_BASE	0x85000000
 #define USB_DATA_BASE	0x85000000

+ 4 - 0
projects/riscv_usb/fw/firmware.c

@@ -46,6 +46,10 @@ void main()
 	led_breathe(true, 100, 200);
 	led_breathe(true, 100, 200);
 	led_state(true);
 	led_state(true);
 
 
+	/* SPI */
+	spi_init();
+	printf("Flash ID: %08x\n", flash_id());
+
 	/* Main loop */
 	/* Main loop */
 	while (1)
 	while (1)
 	{
 	{

+ 134 - 0
projects/riscv_usb/fw/spi.c

@@ -0,0 +1,134 @@
+/*
+ * spi.c
+ *
+ * Copyright (C) 2019 Sylvain Munaut
+ * All rights reserved.
+ *
+ * LGPL v3+, see LICENSE.lgpl3
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "config.h"
+#include "spi.h"
+
+
+struct spi {
+	uint32_t _rsvd0[6];
+	uint32_t irq;		/* 0110 - SPIIRQ   - Interrupt Status Register  */
+	uint32_t irqen;		/* 0111 - SPIIRQEN - Interrupt Control Register */
+	uint32_t cr0;		/* 1000 - CR0      - Control Register 0 */
+	uint32_t cr1;		/* 1001 - CR1      - Control Register 1 */
+	uint32_t cr2;		/* 1010 - CR2      - Control Register 2 */
+	uint32_t br;		/* 1011 - BR       - Baud Rate Register */
+	uint32_t sr;		/* 1100 - SR       - Status Register    */
+	uint32_t txdr;		/* 1101 - TXDR     - Transmit Data Register */
+	uint32_t rxdr;		/* 1110 - RXDR     - Receive Data Register  */
+	uint32_t csr;		/* 1111 - CSR      - Chip Select Register   */
+} __attribute__((packed,aligned(4)));
+
+#define SPI_CR0_TIDLE(xcnt)	(((xcnt) & 3) << 6)
+#define SPI_CR0_TTRAIL(xcnt)	(((xcnt) & 7) << 3)
+#define SPI_CR0_TLEAD(xcnt)	(((xcnt) & 7) << 0)
+
+#define SPI_CR1_ENABLE		(1 << 7)
+#define SPI_CR1_WKUPEN_USER	(1 << 6)
+#define SPI_CR1_TXEDGE		(1 << 4)
+
+#define SPI_CR2_MASTER		(1 << 7)
+#define SPI_CR2_MCSH		(1 << 6)
+#define SPI_CR2_SDBRE		(1 << 5)
+#define SPI_CR2_CPOL		(1 << 2)
+#define SPI_CR2_CPHA		(1 << 1)
+#define SPI_CR2_LSBF		(1 << 0)
+
+#define SPI_SR_TIP		(1 << 7)
+#define SPI_SR_BUSY		(1 << 6)
+#define SPI_SR_TRDY		(1 << 4)
+#define SPI_SR_RRDY		(1 << 3)
+#define SPI_SR_TOE		(1 << 2)
+#define SPI_SR_ROE		(1 << 1)
+#define SPI_SR_MDF		(1 << 0)
+
+
+static volatile struct spi * const spi_regs = (void*)(SPI_BASE);
+
+
+void
+spi_init(void)
+{
+	spi_regs->cr0 = SPI_CR0_TIDLE(3) |
+	                SPI_CR0_TTRAIL(7) |
+	                SPI_CR0_TLEAD(7);
+	spi_regs->cr1 = SPI_CR1_ENABLE;
+	spi_regs->cr2 = SPI_CR2_MASTER | SPI_CR2_MCSH;
+	spi_regs->br  = 3;
+	spi_regs->csr = 0xf;
+}
+
+void
+spi_xfer(unsigned cs, struct spi_xfer_chunk *xfer, unsigned n)
+{
+	/* Setup CS */
+	spi_regs->csr = 0xf ^ (1 << cs);
+
+	/* Run the chunks */
+	while (n--) {
+		for (int i=0; i<xfer->len; i++)
+		{
+			spi_regs->txdr = xfer->write ? xfer->data[i] : 0x00;
+			while (!(spi_regs->sr & SPI_SR_RRDY));
+			if (xfer->read)
+				xfer->data[i] = spi_regs->rxdr;
+		}
+		xfer++;
+	}
+
+	/* Clear CS */
+	spi_regs->csr = 0xf ^ (1 << cs);
+}
+
+
+void flash_cmd(uint8_t cmd)
+{
+	struct spi_xfer_chunk xfer[1] = {
+		{ .data = (void*)&cmd, .len = 1, .read = false, .write = true,  },
+	};
+	spi_xfer(SPI_CS_FLASH, xfer, 1);
+}
+
+uint32_t flash_id(void)
+{
+	uint32_t buf = 0x9f;
+	struct spi_xfer_chunk xfer[2] = {
+		{ .data = (void*)&buf, .len = 1, .read = false, .write = true,  },
+		{ .data = (void*)&buf, .len = 3, .read = true,  .write = false, },
+	};
+	spi_xfer(SPI_CS_FLASH, xfer, 2);
+	return buf;
+}
+
+void flash_read(void *dst, uint32_t addr, unsigned len)
+{
+	uint8_t cmd[4] = { 0x03, ((addr >> 16) & 0xff), ((addr >> 8) & 0xff), (addr & 0xff)  };
+	struct spi_xfer_chunk xfer[2] = {
+		{ .data = (void*)cmd, .len = 4,   .read = false, .write = true,  },
+		{ .data = (void*)dst, .len = len, .read = true,  .write = false, },
+	};
+	spi_xfer(SPI_CS_FLASH, xfer, 2);
+}

+ 46 - 0
projects/riscv_usb/fw/spi.h

@@ -0,0 +1,46 @@
+/*
+ * spi.h
+ *
+ * Copyright (C) 2019 Sylvain Munaut
+ * All rights reserved.
+ *
+ * LGPL v3+, see LICENSE.lgpl3
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include <stdbool.h>
+
+struct spi_xfer_chunk {
+	uint8_t *data;
+	unsigned len;
+	bool write;
+	bool read; 
+};
+
+#define SPI_CS_FLASH	0
+#define SPI_CS_SRAM	1
+
+void spi_init(void);
+void spi_xfer(unsigned cs, struct spi_xfer_chunk *xfer, unsigned n);
+
+void flash_cmd(uint8_t cmd);
+uint32_t flash_id(void);
+void flash_read(void *dst, uint32_t addr, unsigned len);
+
+static inline void flash_power_up(void)   { flash_cmd(0xab); };
+static inline void flash_power_down(void) { flash_cmd(0xb9); };