From patchwork Sun Apr 23 15:20:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Pisati X-Patchwork-Id: 9694863 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 55F0E601E9 for ; Sun, 23 Apr 2017 15:21:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 578FC2621B for ; Sun, 23 Apr 2017 15:21:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4BA8A26C9B; Sun, 23 Apr 2017 15:21:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ACD562621B for ; Sun, 23 Apr 2017 15:21:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161942AbdDWPU6 (ORCPT ); Sun, 23 Apr 2017 11:20:58 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:32985 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1045746AbdDWPUu (ORCPT ); Sun, 23 Apr 2017 11:20:50 -0400 Received: by mail-wm0-f66.google.com with SMTP id y10so1805704wmh.0; Sun, 23 Apr 2017 08:20:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=EVKuufNczWDCqAyXntfyza/EoFBgBfDf00Gj+dIVvlA=; b=Xhyfi7uRqF7QEHdGK8dw/c0Kxgl705xz4RuPcXHIpXUNzVmQ5AUdpRWsBWXZ+BM9FV AoTOYCxgdBDLEBmOVI7UBJWZ3GPardjVG99N7/JymNXFNXSAcUFRPGhxVl5UT5B0f6Am Ck16UeXCjbUAuXl4Rb/0z5jDOoVHeOcSzaPLR4wlElHOqPe5pgwH5Al24bYy0475+Nud rCLtJrHoyzWMoSuV3yBnrXT1BjkVdM64ljj8Fvysf/VEAiqi2x2xgOl1kaZczr8teZxA opqHsQQLh9qxBO7AC8rcQcZemDHkNheBcFsQIGRyfPmzsoAxMLrVX4Wktp8mPoBHBVqd NBrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=EVKuufNczWDCqAyXntfyza/EoFBgBfDf00Gj+dIVvlA=; b=l4CQdBqoVsFezClzdaMFNNZvUeXJbvcLhNegKV/cbTJ9n8aSourSNSWXx3Wj5KSkHV 0KAmBNkwtFS96TZ2ySSjYgCeWTYDYOTsAeco3v/8JGqscI/TKpQm9QDDFyAFiBi/0E5H REU0R5I69Gh4/d3aTFYwxbPO7of2Y7DlQcrs7a8UA/YnCjaIz8g87v/f1MSD9WLM0S77 WQ4JnthtFvcK5p6gwpp68xNnz3EZ/iFxbt0ktM7ZeFI3rDfHhhn2v6fyLpNpy6OrnBb+ 6eWDZ/uA7BnOBNrzyNwHAiVARC8w9CmazrfnvRosFlyAmKH2PHHQcMLFRZEgrS1UnJGh bp1w== X-Gm-Message-State: AN3rC/5OV1Ej1YdZrrwDjIX15x1VzJJr4z4QNMcIolMBAzk8rdFEr9oY y9vwETK5nfwXkcMzN67m6A== X-Received: by 10.28.175.209 with SMTP id y200mr6822446wme.81.1492960848350; Sun, 23 Apr 2017 08:20:48 -0700 (PDT) Received: from gmail.com (2-235-152-247.ip228.fastwebnet.it. [2.235.152.247]) by smtp.gmail.com with ESMTPSA id j26sm19023909wrb.19.2017.04.23.08.20.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 23 Apr 2017 08:20:47 -0700 (PDT) From: Paolo Pisati To: Rob Herring , Mark Rutland , Alan Tull , Moritz Fischer Cc: devicetree@vger.kernel.org, linux-fpga@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] fpga: lattice machxo2: Add Lattice MachXO2 support Date: Sun, 23 Apr 2017 17:20:45 +0200 Message-Id: <1492960845-342-3-git-send-email-p.pisati@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1492960845-342-1-git-send-email-p.pisati@gmail.com> References: <1492960845-342-1-git-send-email-p.pisati@gmail.com> Sender: linux-fpga-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fpga@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add support for the Lattice MachXO2 FPGA chip in Slave SPI configuration. Signed-off-by: Paolo Pisati --- drivers/fpga/Kconfig | 7 ++ drivers/fpga/Makefile | 1 + drivers/fpga/machxo2-spi.c | 199 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+) create mode 100644 drivers/fpga/machxo2-spi.c diff --git a/drivers/fpga/Kconfig b/drivers/fpga/Kconfig index c81cb7d..cce135b 100644 --- a/drivers/fpga/Kconfig +++ b/drivers/fpga/Kconfig @@ -26,6 +26,13 @@ config FPGA_MGR_ICE40_SPI help FPGA manager driver support for Lattice iCE40 FPGAs over SPI. +config FPGA_MGR_MACHXO2_SPI + tristate "Lattice MachXO2 SPI" + depends on SPI + help + FPGA manager driver support for Lattice MachXO2 configuration + over slave SPI interface. + config FPGA_MGR_SOCFPGA tristate "Altera SOCFPGA FPGA Manager" depends on ARCH_SOCFPGA || COMPILE_TEST diff --git a/drivers/fpga/Makefile b/drivers/fpga/Makefile index c6f5d74..cdab1fe 100644 --- a/drivers/fpga/Makefile +++ b/drivers/fpga/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_FPGA) += fpga-mgr.o # FPGA Manager Drivers obj-$(CONFIG_FPGA_MGR_ICE40_SPI) += ice40-spi.o +obj-$(CONFIG_FPGA_MGR_MACHXO2_SPI) += machxo2-spi.o obj-$(CONFIG_FPGA_MGR_SOCFPGA) += socfpga.o obj-$(CONFIG_FPGA_MGR_SOCFPGA_A10) += socfpga-a10.o obj-$(CONFIG_FPGA_MGR_TS73XX) += ts73xx-fpga.o diff --git a/drivers/fpga/machxo2-spi.c b/drivers/fpga/machxo2-spi.c new file mode 100644 index 0000000..5ee56bd --- /dev/null +++ b/drivers/fpga/machxo2-spi.c @@ -0,0 +1,199 @@ +/** + * Lattice MachXO2 Slave SPI Driver + * + * Copyright (C) 2017 Paolo Pisati + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * Manage Lattice FPGA firmware that is loaded over SPI using + * the slave serial configuration interface. + */ + +#include +#include +#include +#include +#include +#include + + +/* MachXO2 Programming Guide - sysCONFIG Programming Commands */ + +#define ISC_ENABLE 0x000008c6 +#define ISC_ERASE 0x0000040e +#define ISC_PROGRAMDONE 0x0000005e +#define LSC_CHECKBUSY 0x000000f0 +#define LSC_INITADDRESS 0x00000046 +#define LSC_PROGINCRNV 0x01000070 +#define LSC_REFRESH 0x00000079 + +/* + * Max CCLK in Slave SPI mode according to 'MachXO2 Family Data + * Sheet' sysCONFIG Port Timing Specifications (3-36) + */ +#define MACHXO2_MAX_SPEED 66000000 + +#define MACHXO2_LOW_DELAY 5 /* us */ +#define MACHXO2_HIGH_DELAY 200 /* us */ + +#define MACHXO2_OP_SIZE sizeof(uint32_t) +#define MACHXO2_PAGE_SIZE 16 +#define MACHXO2_BUF_SIZE (MACHXO2_OP_SIZE + MACHXO2_PAGE_SIZE) + + +static int waituntilnotbusy(struct spi_device *spi) +{ + uint8_t rx, busyflag = 0x80; + uint32_t checkbusy = LSC_CHECKBUSY; + + do { + if (spi_write_then_read(spi, &checkbusy, MACHXO2_OP_SIZE, + &rx, sizeof(rx))) + return -EIO; + } while (rx & busyflag); + return 0; +} + +static enum fpga_mgr_states machxo2_spi_state(struct fpga_manager *mgr) +{ + return FPGA_MGR_STATE_UNKNOWN; +} + +static int machxo2_write_init(struct fpga_manager *mgr, + struct fpga_image_info *info, + const char *buf, size_t count) +{ + struct spi_device *spi = mgr->priv; + uint32_t enable = ISC_ENABLE; + uint32_t erase = ISC_ERASE; + uint32_t initaddr = LSC_INITADDRESS; + + if ((info->flags & FPGA_MGR_PARTIAL_RECONFIG)) { + dev_err(&mgr->dev, + "Partial reconfiguration is not supported\n"); + return -ENOTSUPP; + } + + if (spi_write(spi, &enable, MACHXO2_OP_SIZE)) + goto fail; + udelay(MACHXO2_LOW_DELAY); + if (spi_write(spi, &erase, MACHXO2_OP_SIZE)) + goto fail; + waituntilnotbusy(spi); + if (spi_write(spi, &initaddr, MACHXO2_OP_SIZE)) + goto fail; + return 0; + +fail: + dev_err(&mgr->dev, "Error during FPGA init.\n"); + return -EIO; +} + +static int machxo2_write(struct fpga_manager *mgr, const char *buf, + size_t count) +{ + struct spi_device *spi = mgr->priv; + uint32_t progincr = LSC_PROGINCRNV; + uint8_t payload[MACHXO2_BUF_SIZE]; + int i; + + if (count % MACHXO2_PAGE_SIZE != 0) { + dev_err(&mgr->dev, "Malformed payload.\n"); + return -EINVAL; + } + + memcpy(payload, &progincr, MACHXO2_OP_SIZE); + for (i = 0; i < count; i += MACHXO2_PAGE_SIZE) { + memcpy(&payload[MACHXO2_OP_SIZE], &buf[i], MACHXO2_PAGE_SIZE); + if (spi_write(spi, payload, MACHXO2_BUF_SIZE)) { + dev_err(&mgr->dev, "Error loading the bitstream.\n"); + return -EIO; + } + udelay(MACHXO2_HIGH_DELAY); + } + + return 0; +} + +static int machxo2_write_complete(struct fpga_manager *mgr, + struct fpga_image_info *info) +{ + struct spi_device *spi = mgr->priv; + uint32_t progdone = ISC_PROGRAMDONE; + uint32_t refresh = LSC_REFRESH; + + if (spi_write(spi, &progdone, MACHXO2_OP_SIZE)) + goto fail; + /* yep, LSC_REFRESH is 3 bytes long actually */ + if (spi_write(spi, &refresh, MACHXO2_OP_SIZE-1)) + goto fail; + return 0; + +fail: + dev_err(&mgr->dev, "Refresh failed.\n"); + return -EIO; +} + +static const struct fpga_manager_ops machxo2_ops = { + .state = machxo2_spi_state, + .write_init = machxo2_write_init, + .write = machxo2_write, + .write_complete = machxo2_write_complete, +}; + +static int machxo2_spi_probe(struct spi_device *spi) +{ + struct device *dev = &spi->dev; + int ret = 0; + + if (spi->max_speed_hz > MACHXO2_MAX_SPEED) { + dev_err(dev, "Speed is too high\n"); + return -EINVAL; + } + + ret = fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager", + &machxo2_ops, spi); + if (ret) + dev_err(dev, "Unable to register FPGA manager"); + + return ret; +} + +static int machxo2_spi_remove(struct spi_device *spi) +{ + struct device *dev = &spi->dev; + + fpga_mgr_unregister(dev); + return 0; +} + +static const struct of_device_id of_match[] = { + { .compatible = "lattice,machxo2-slave-spi", }, + {} +}; +MODULE_DEVICE_TABLE(of, of_match); + +static const struct spi_device_id lattice_ids[] = { + { "machxo2-slave-spi", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(spi, lattice_ids); + +static struct spi_driver machxo2_spi_driver = { + .driver = { + .name = "machxo2-slave-spi", + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(of_match), + }, + .probe = machxo2_spi_probe, + .remove = machxo2_spi_remove, + .id_table = lattice_ids, +}; + +module_spi_driver(machxo2_spi_driver) + +MODULE_AUTHOR("Paolo Pisati "); +MODULE_DESCRIPTION("Load Lattice FPGA firmware over SPI"); +MODULE_LICENSE("GPL v2");