Received: by 10.213.65.68 with SMTP id h4csp495634imn; Fri, 16 Mar 2018 09:29:02 -0700 (PDT) X-Google-Smtp-Source: AG47ELtH1+Fqg273Hr+cTlZrPqPVvVJUDF75toM4hpve7PuKbHyLfB+iUvttq1zHXGrdu1raGMS5 X-Received: by 2002:a17:902:8f8e:: with SMTP id z14-v6mr2745543plo.271.1521217742565; Fri, 16 Mar 2018 09:29:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521217742; cv=none; d=google.com; s=arc-20160816; b=Wiy0c/rt5BPEfBXnV9EHAgX9p7EY+Upyr31Qz/d1OgRdR/LpiP1kHbiFMwc6M5JaU0 FmlRbuOLZ3KPF56DhVnIvAA6T9N9DevEnA+JIeJ5QLjPbGtdnDoWUnq8Zlk6iAvdlL3v jKdvuZ0HIkkOp97AYiEVq+leQ0VtUFsi9p+Fl3SfTdjDA31nu9X7M9AM3tifoPWW6vbL wVD1tAmAp0wTTZKuO3EZH7ktTz04np3O/jsh+webQ2F6QpV3hP7T7S5dedc1CwzSOW7z 4ulVNYUhsryc3TYF2EdWPlhOyEQyGwAVNPewRI4VZoBu66HtdHmb+9KN0at6iaroAm/w p2hQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date :arc-authentication-results; bh=XzuZUWRzniB1Rkx5b2JhIykFXOtOSD808MVEK02S7NA=; b=wOPaGYBIXFnHil+5AZqwaA/cza9/Ub40rk3jayRB+/BNYlW6disVAKFcZkYuPNZpfg tOU2HNy4k7tHl3xLAyoPI5pKbBcVJWqYZ7/zOIz4QyCStX7M7YHT2OUZmh8vZU3XKKzs 9temBMYNtEdVtlY+cpQ/b9TyjQ6YFCBjhnEjBBabBNg2vsYSP2yD3NcHHauMpVJmHiWr Yf6m3QKb/pHGdAhOB9quZjc85evumtiyYZxQzpzqZIqcVYfwVF4Q60zUVU27kMdmjUhp Cc+EcUZI1oW0UfI4Ut74jg43r3TB9jCDJt2RW0SeurG6V5LL69dYXKOWrmPIT/qJhRay Lupw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e23si5668720pfn.228.2018.03.16.09.28.47; Fri, 16 Mar 2018 09:29:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752465AbeCPQ07 (ORCPT + 99 others); Fri, 16 Mar 2018 12:26:59 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:35451 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752484AbeCPQ0z (ORCPT ); Fri, 16 Mar 2018 12:26:55 -0400 Received: by mail-pg0-f65.google.com with SMTP id d1so4269075pgv.2 for ; Fri, 16 Mar 2018 09:26:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=XzuZUWRzniB1Rkx5b2JhIykFXOtOSD808MVEK02S7NA=; b=Fhr+y3Dyc2POI6blN+XQoTzmOv9TAuXLFZpSDbieCyr08GaYdUpncgDMgYEPq//PTY 1C7ZgvJUZGPSgEAu8LWsEFrKUyieQJwQ74eQU/B/Z0oz4BFtisMu2071kQBRwnnlYeRG RLHWsxt+2sPuAFL9fumAzindgD3FYRN7lmd1aUefQ3YzYxd9lyiGZDAbi/+mP7kvoNzc lF7fwOTExWxaErbl6DQ+S8yPZcKMRb94EtgUI4l93zSf1UuInde0ItZZCjH5Xk2RqBnG u0qH12UR8Eg0du/sRxWOjpPa7mduXM5r3dI0jM1v2tqlbyAyWbSOMrPNzybta450yjNI dToA== X-Gm-Message-State: AElRT7EAQZFQERzChAAAdgmU6/jj96DSoqj50vrQ6J8l+oHF+3ZHGRsg 98drNhqXMLfPssGHD00v0nHwkw== X-Received: by 10.99.123.80 with SMTP id k16mr1926028pgn.173.1521217614788; Fri, 16 Mar 2018 09:26:54 -0700 (PDT) Received: from localhost (207-114-172-147.static.twtelecom.net. [207.114.172.147]) by smtp.gmail.com with ESMTPSA id u5sm4545994pfg.184.2018.03.16.09.26.53 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 16 Mar 2018 09:26:54 -0700 (PDT) Date: Fri, 16 Mar 2018 09:26:54 -0700 From: Moritz Fischer To: Paolo Pisati Cc: Alan Tull , Moritz Fischer , Rob Herring , Mark Rutland , linux-fpga@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] fpga: lattice machxo2: Add Lattice MachXO2 support Message-ID: <20180316162654.bcr7d46om2tnhm7l@us-sjc-wireless-10-16-34-48.amer.corp.natinst.com> References: <1521215669-23874-1-git-send-email-p.pisati@gmail.com> <1521215669-23874-3-git-send-email-p.pisati@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1521215669-23874-3-git-send-email-p.pisati@gmail.com> User-Agent: NeoMutt/20171215 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 16, 2018 at 04:54:29PM +0100, Paolo Pisati wrote: > This patch adds support to the FPGA manager for programming > MachXO2 device’s internal flash memory, via slave SPI. > > Signed-off-by: Paolo Pisati > --- > drivers/fpga/Kconfig | 8 + > drivers/fpga/Makefile | 1 + > drivers/fpga/machxo2-spi.c | 410 +++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 419 insertions(+) > create mode 100644 drivers/fpga/machxo2-spi.c > > diff --git a/drivers/fpga/Kconfig b/drivers/fpga/Kconfig > index ad5448f..65ccbc5 100644 > --- a/drivers/fpga/Kconfig > +++ b/drivers/fpga/Kconfig > @@ -38,6 +38,14 @@ config FPGA_MGR_ALTERA_PS_SPI > FPGA manager driver support for Altera Arria/Cyclone/Stratix > using the passive serial interface 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 f98dcf1..38abb08 100644 > --- a/drivers/fpga/Makefile > +++ b/drivers/fpga/Makefile > @@ -10,6 +10,7 @@ obj-$(CONFIG_FPGA) += fpga-mgr.o > obj-$(CONFIG_FPGA_MGR_ALTERA_CVP) += altera-cvp.o > obj-$(CONFIG_FPGA_MGR_ALTERA_PS_SPI) += altera-ps-spi.o > 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..3c7fc3e > --- /dev/null > +++ b/drivers/fpga/machxo2-spi.c > @@ -0,0 +1,410 @@ > +/** > + * Lattice MachXO2 Slave SPI Driver > + * > + * Copyright (C) 2018 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. SPDX please. > + * > + * Manage Lattice FPGA firmware that is loaded over SPI using > + * the slave serial configuration interface. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +//#define MACHXO2_DEBUG ?! No ;-) > + > +/* MachXO2 Programming Guide - sysCONFIG Programming Commands */ > +#define IDCODE_PUB {0xe0, 0x00, 0x00, 0x00} > +#define ISC_ENABLE {0xc6, 0x08, 0x00, 0x00} > +#define ISC_ERASE {0x0e, 0x04, 0x00, 0x00} > +#define ISC_PROGRAMDONE {0x5e, 0x00, 0x00, 0x00} > +#define LSC_INITADDRESS {0x46, 0x00, 0x00, 0x00} > +#define LSC_PROGINCRNV {0x70, 0x00, 0x00, 0x01} > +#define LSC_READ_STATUS {0x3c, 0x00, 0x00, 0x00} > +#define LSC_REFRESH {0x79, 0x00, 0x00, 0x00} > + > +/* > + * 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_REFRESH 4800 /* us */ > +#define MACHXO2_MAX_BUSY_LOOP 128 > +#define MACHXO2_MAX_REFRESH_LOOP 16 > + > +#define MACHXO2_PAGE_SIZE 16 > +#define MACHXO2_BUF_SIZE (MACHXO2_PAGE_SIZE + 4) > + > +/* Status register bits, errors and error mask */ > +#define BUSY 12 > +#define DONE 8 > +#define DVER 27 > +#define ENAB 9 > +#define ERRBITS 23 > +#define ERRMASK 7 > +#define FAIL 13 > + > +#define ENOERR 0 /* no error */ > +#define EID 1 > +#define ECMD 2 > +#define ECRC 3 > +#define EPREAM 4 /* preamble error */ > +#define EABRT 5 /* abort error */ > +#define EOVERFL 6 /* overflow error */ > +#define ESDMEOF 7 /* SDM EOF */ > + > +static inline u8 get_err(unsigned long *status) > +{ > + return (*status >> ERRBITS) & ERRMASK; > +} > + > +static int get_status(struct spi_device *spi, unsigned long *status) > +{ > + struct spi_message msg; > + struct spi_transfer rx, tx; > + u8 cmd[] = LSC_READ_STATUS; > + int ret; > + > + memset(&rx, 0, sizeof(rx)); > + memset(&tx, 0, sizeof(tx)); > + tx.tx_buf = cmd; > + tx.len = sizeof(cmd); > + rx.rx_buf = status; > + rx.len = 4; > + spi_message_init(&msg); > + spi_message_add_tail(&tx, &msg); > + spi_message_add_tail(&rx, &msg); > + ret = spi_sync(spi, &msg); > + if (ret) > + return ret; > + > + *status = be32_to_cpu(*status); > + return 0; > +} > + > +#ifdef MACHXO2_DEBUG Not a fan of this ... > +static void dump_status_reg(unsigned long *status) > +{ > + char *ferr; > + > + switch (get_err(status)) { > + case ENOERR: > + ferr = "No Error"; > + break; > + case EID: > + ferr = "ID ERR"; > + break; > + case ECMD: > + ferr = "CMD ERR"; > + break; > + case ECRC: > + ferr = "CRC ERR"; > + break; > + case EPREAM: > + ferr = "Preamble ERR"; > + break; > + case EABRT: > + ferr = "Abort ERR"; > + break; > + case EOVERFL: > + ferr = "Overflow ERR"; > + break; > + case ESDMEOF: > + ferr = "SDM EOF"; > + break; > + default: > + ferr = "Default switch case"; > + } > + printk(KERN_DEBUG "machxo2 status: 0x%08lX - done=%d, cfgena=%d, busy=%d, fail=%d, devver=%d, err=%s\n", > + *status, test_bit(DONE, status), test_bit(ENAB, status), > + test_bit(BUSY, status), test_bit(FAIL, status), > + test_bit(DVER, status), ferr); Not sure we need this function in the first place, if you really think you need it at least use pr_dbg() or pr_err() here. > +} > +#else > +static void dump_status_reg(unsigned long *status) {} > +#endif > + > +static int wait_until_not_busy(struct spi_device *spi) > +{ > + unsigned long status; > + int ret, loop = 0; > + > + do { > + ret = get_status(spi, &status); > + if (ret) > + break; > + if (++loop >= MACHXO2_MAX_BUSY_LOOP) { > + ret = -EBUSY; > + break; > + } > + } while (test_bit(BUSY, &status)); > + > + return ret; > +} > + > +static int machxo2_cleanup(struct fpga_manager *mgr) > +{ > + struct spi_device *spi = mgr->priv; > + struct spi_message msg; > + struct spi_transfer tx[2]; > + u8 erase[] = ISC_ERASE; > + u8 refresh[] = LSC_REFRESH; > + int ret; > + > + memset(tx, 0, sizeof(tx)); > + spi_message_init(&msg); > + tx[0].tx_buf = &erase; > + tx[0].len = sizeof(erase); > + spi_message_add_tail(&tx[0], &msg); > + ret = spi_sync(spi, &msg); > + if (ret) > + goto fail; > + > + ret = wait_until_not_busy(spi); > + if (ret) > + goto fail; > + > + spi_message_init(&msg); > + tx[1].tx_buf = &refresh; > + tx[1].len = sizeof(refresh); > + tx[1].delay_usecs = MACHXO2_REFRESH; > + spi_message_add_tail(&tx[1], &msg); > + ret = spi_sync(spi, &msg); > + if (ret) > + goto fail; > + > + return 0; > +fail: > + dev_err(&mgr->dev, "Cleanup failed\n"); > + return ret; > +} > + > +static enum fpga_mgr_states machxo2_spi_state(struct fpga_manager *mgr) > +{ > + return FPGA_MGR_STATE_UNKNOWN; > +} To understand this correctly: Further up you have a way to determine the state you're in, but then you only use that for debug printout, while here where you should provide the status you say FPGA_MGR_STATE_UNKNOWN? Am I missing something here? > + > +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; > + struct spi_message msg; > + struct spi_transfer tx[3]; > + u8 enable[] = ISC_ENABLE; > + u8 erase[] = ISC_ERASE; > + u8 initaddr[] = LSC_INITADDRESS; > + unsigned long status; > + int ret; > + > + if ((info->flags & FPGA_MGR_PARTIAL_RECONFIG)) { > + dev_err(&mgr->dev, > + "Partial reconfiguration is not supported\n"); > + return -ENOTSUPP; > + } > + > + get_status(spi, &status); > + dump_status_reg(&status); > + memset(tx, 0, sizeof(tx)); > + spi_message_init(&msg); > + tx[0].tx_buf = &enable; > + tx[0].len = sizeof(enable); > + tx[0].delay_usecs = MACHXO2_LOW_DELAY; > + spi_message_add_tail(&tx[0], &msg); > + > + tx[1].tx_buf = &erase; > + tx[1].len = sizeof(erase); > + spi_message_add_tail(&tx[1], &msg); > + ret = spi_sync(spi, &msg); > + if (ret) > + goto fail; > + > + ret = wait_until_not_busy(spi); > + if (ret) > + goto fail; > + > + get_status(spi, &status); > + if (test_bit(FAIL, &status)) > + goto fail; > + dump_status_reg(&status); > + > + spi_message_init(&msg); > + tx[2].tx_buf = &initaddr; > + tx[2].len = sizeof(initaddr); > + spi_message_add_tail(&tx[2], &msg); > + ret = spi_sync(spi, &msg); > + if (ret) > + goto fail; > + > + get_status(spi, &status); > + dump_status_reg(&status); > + return 0; > +fail: > + dev_err(&mgr->dev, "Error during FPGA init.\n"); > + return ret; > +} > + > +static int machxo2_write(struct fpga_manager *mgr, const char *buf, > + size_t count) > +{ > + struct spi_device *spi = mgr->priv; > + struct spi_message msg; > + struct spi_transfer tx; > + u8 progincr[] = LSC_PROGINCRNV; > + u8 payload[MACHXO2_BUF_SIZE]; > + unsigned long status; > + int i, ret; > + > + if (count % MACHXO2_PAGE_SIZE != 0) { > + dev_err(&mgr->dev, "Malformed payload.\n"); > + return -EINVAL; > + } > + get_status(spi, &status); > + dump_status_reg(&status); > + memcpy(payload, &progincr, sizeof(progincr)); > + for (i = 0; i < count; i += MACHXO2_PAGE_SIZE) { > + memcpy(&payload[sizeof(progincr)], &buf[i], MACHXO2_PAGE_SIZE); > + memset(&tx, 0, sizeof(tx)); > + spi_message_init(&msg); > + tx.tx_buf = payload; > + tx.len = MACHXO2_BUF_SIZE; > + tx.delay_usecs = MACHXO2_HIGH_DELAY; > + spi_message_add_tail(&tx, &msg); > + ret = spi_sync(spi, &msg); > + if (ret) { > + dev_err(&mgr->dev, "Error loading the bitstream.\n"); > + return ret; > + } > + } > + get_status(spi, &status); > + dump_status_reg(&status); > + > + return 0; > +} > + > +static int machxo2_write_complete(struct fpga_manager *mgr, > + struct fpga_image_info *info) > +{ > + struct spi_device *spi = mgr->priv; > + struct spi_message msg; > + struct spi_transfer tx[2]; > + u8 progdone[] = ISC_PROGRAMDONE; > + u8 refresh[] = LSC_REFRESH; > + unsigned long status; > + int ret, refreshloop = 0; > + > + memset(tx, 0, sizeof(tx)); > + spi_message_init(&msg); > + tx[0].tx_buf = &progdone; > + tx[0].len = sizeof(progdone); > + spi_message_add_tail(&tx[0], &msg); > + ret = spi_sync(spi, &msg); > + if (ret) > + goto fail; > + ret = wait_until_not_busy(spi); > + if (ret) > + goto fail; > + > + get_status(spi, &status); > + dump_status_reg(&status); > + if (!test_bit(DONE, &status)) { > + machxo2_cleanup(mgr); > + goto fail; > + } > + > + do { > + spi_message_init(&msg); > + tx[1].tx_buf = &refresh; > + tx[1].len = sizeof(refresh); > + tx[1].delay_usecs = MACHXO2_REFRESH; > + spi_message_add_tail(&tx[1], &msg); > + ret = spi_sync(spi, &msg); > + if (ret) > + goto fail; > + > + /* check refresh status */ > + get_status(spi, &status); > + dump_status_reg(&status); > + if (!test_bit(BUSY, &status) && test_bit(DONE, &status) && > + get_err(&status) == ENOERR) > + break; > + if (++refreshloop == MACHXO2_MAX_REFRESH_LOOP) { > + machxo2_cleanup(mgr); > + goto fail; > + } > + } while (1); > + > + get_status(spi, &status); > + dump_status_reg(&status); > + return 0; > +fail: > + dev_err(&mgr->dev, "Refresh failed.\n"); > + return ret; > +} > + > +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; > + > + if (spi->max_speed_hz > MACHXO2_MAX_SPEED) { > + dev_err(dev, "Speed is too high\n"); > + return -EINVAL; > + } > + > + return fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager", > + &machxo2_ops, spi); > +} > + > +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", > + .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"); > -- > 2.7.4 >