Received: by 10.213.65.68 with SMTP id h4csp1223658imn; Wed, 14 Mar 2018 13:24:41 -0700 (PDT) X-Google-Smtp-Source: AG47ELtaOmJ08Dz594p1mX5diQCaWzF6raYiyzOzSHm/83Hw8QcbmXllAXg1MScnvG9k4ZKQkPQW X-Received: by 2002:a17:902:60c4:: with SMTP id k4-v6mr5248314pln.44.1521059081488; Wed, 14 Mar 2018 13:24:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521059081; cv=none; d=google.com; s=arc-20160816; b=Ypz9BVuyvrVredrgRytUyW/QjaFR1l8sVbrwiN09GudFkU9eXZDKvuwccK0RDyHxkG Hb9Mlag11skEEEckN9odaZzvTDIAyblaFpQwO7s0/Wh67WTA1f8/4GpG2EPeDKlqguvW GzOrNhZek09aDCVN6/M9yYhGdvIUWrK3kZplKv9EmEDsxUykK1uxDa2mEzkiVJc8Ji0H wx0YiTMksns13qSKLin+Ft/Rf39qVVyZFND/LltAxFHuwrjMO33gFtbtGRSXE4TQQEqu jjgTmlQpxLwgkUMUak08S0luGxhCOJQqHQUMNAUfdakwZa6Js+H+03f65QeqnaYlAvTu EhQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:cc:subject:date:to :from:arc-authentication-results; bh=7OLHN15+gLqzD2V27g0KN7tzoHjthjVj9umcFB67CdM=; b=Mz4PwxH0vF3eFJl97Gn25cMuVhbSSlDgvC06u7bUEW941GfksCxHjh0G5GtLdWaAw2 WfUV1Qn8KGPumP5p6DTGWgJy3+0JfunUmrKTdj39tIM4xoMkPybcH+T4PkrK98C6DYKk yilSXsyx4Eqz+zZppshAV6qBfac9Vo0VrJJKsb05arWqhnbeq+VDfzxFnODOdLcAtsOo avg4uXDiG7OYye+b7cMaYsitOjEHwRX/PgVGyW1EYPjxN/eDbx3nSqnOige77U6hnsql 4+m1X8yk5R/kMHBfS5CANckX0vf7i9udxnLEkA18U1VGpIedC697amkMAfxQbFukhof4 sdBQ== 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 26si2589422pfl.261.2018.03.14.13.24.26; Wed, 14 Mar 2018 13:24:41 -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 S932122AbeCNUXa (ORCPT + 99 others); Wed, 14 Mar 2018 16:23:30 -0400 Received: from mx2.suse.de ([195.135.220.15]:38550 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752278AbeCNUX0 (ORCPT ); Wed, 14 Mar 2018 16:23:26 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 4E133B015; Wed, 14 Mar 2018 20:23:25 +0000 (UTC) From: NeilBrown To: Greg Kroah-Hartman Date: Thu, 15 Mar 2018 07:22:35 +1100 Subject: [PATCH 04/13] staging: mt7621-spi: add mt7621 support Cc: devel@driverdev.osuosl.org, lkml , John Crispin Message-ID: <152105895549.22262.12542208654106821911.stgit@noble> In-Reply-To: <152105892255.22262.1902152685410223215.stgit@noble> References: <152105892255.22262.1902152685410223215.stgit@noble> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: John Crispin NeilBrown: The code will fail with a warning if asked to transfer more than 32 bytes at a time. So used max_transfer_size interface to tell users about this. Signed-off-by: John Crispin Signed-off-by: NeilBrown --- drivers/staging/Kconfig | 2 drivers/staging/Makefile | 1 drivers/staging/mt7621-spi/Kconfig | 6 drivers/staging/mt7621-spi/Makefile | 1 drivers/staging/mt7621-spi/TODO | 5 drivers/staging/mt7621-spi/spi-mt7621.c | 489 +++++++++++++++++++++++++++++++ 6 files changed, 504 insertions(+) create mode 100644 drivers/staging/mt7621-spi/Kconfig create mode 100644 drivers/staging/mt7621-spi/Makefile create mode 100644 drivers/staging/mt7621-spi/TODO create mode 100644 drivers/staging/mt7621-spi/spi-mt7621.c diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig index 7568e10399fa..bcc6da1f656a 100644 --- a/drivers/staging/Kconfig +++ b/drivers/staging/Kconfig @@ -126,4 +126,6 @@ source "drivers/staging/mt7621-pinctrl/Kconfig" source "drivers/staging/mt7621-gpio/Kconfig" +source "drivers/staging/mt7621-spi/Kconfig" + endif # STAGING diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile index 609fdb218985..051af9965549 100644 --- a/drivers/staging/Makefile +++ b/drivers/staging/Makefile @@ -55,3 +55,4 @@ obj-$(CONFIG_PI433) += pi433/ obj-$(CONFIG_SOC_MT7621) += mt7621-pci/ obj-$(CONFIG_SOC_MT7621) += mt7621-pinctrl/ obj-$(CONFIG_SOC_MT7621) += mt7621-gpio/ +obj-$(CONFIG_SOC_MT7621) += mt7621-spi/ diff --git a/drivers/staging/mt7621-spi/Kconfig b/drivers/staging/mt7621-spi/Kconfig new file mode 100644 index 000000000000..0b90f4cfa426 --- /dev/null +++ b/drivers/staging/mt7621-spi/Kconfig @@ -0,0 +1,6 @@ +config SPI_MT7621 + tristate "MediaTek MT7621 SPI Controller" + depends on RALINK + help + This selects a driver for the MediaTek MT7621 SPI Controller. + diff --git a/drivers/staging/mt7621-spi/Makefile b/drivers/staging/mt7621-spi/Makefile new file mode 100644 index 000000000000..3be508f63bac --- /dev/null +++ b/drivers/staging/mt7621-spi/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_SPI_MT7621) += spi-mt7621.o diff --git a/drivers/staging/mt7621-spi/TODO b/drivers/staging/mt7621-spi/TODO new file mode 100644 index 000000000000..fdbc5002c32a --- /dev/null +++ b/drivers/staging/mt7621-spi/TODO @@ -0,0 +1,5 @@ + +- general code review and clean up +- ensure device-tree requirements are documented + +Cc: NeilBrown diff --git a/drivers/staging/mt7621-spi/spi-mt7621.c b/drivers/staging/mt7621-spi/spi-mt7621.c new file mode 100644 index 000000000000..d95e0b32f1f0 --- /dev/null +++ b/drivers/staging/mt7621-spi/spi-mt7621.c @@ -0,0 +1,489 @@ +/* + * spi-mt7621.c -- MediaTek MT7621 SPI controller driver + * + * Copyright (C) 2011 Sergiy + * Copyright (C) 2011-2013 Gabor Juhos + * Copyright (C) 2014-2015 Felix Fietkau + * + * Some parts are based on spi-orion.c: + * Author: Shadi Ammouri + * Copyright (C) 2007-2008 Marvell Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define SPI_BPW_MASK(bits) BIT((bits) - 1) + +#define DRIVER_NAME "spi-mt7621" +/* in usec */ +#define RALINK_SPI_WAIT_MAX_LOOP 2000 + +/* SPISTAT register bit field */ +#define SPISTAT_BUSY BIT(0) + +#define MT7621_SPI_TRANS 0x00 +#define SPITRANS_BUSY BIT(16) + +#define MT7621_SPI_OPCODE 0x04 +#define MT7621_SPI_DATA0 0x08 +#define MT7621_SPI_DATA4 0x18 +#define SPI_CTL_TX_RX_CNT_MASK 0xff +#define SPI_CTL_START BIT(8) + +#define MT7621_SPI_POLAR 0x38 +#define MT7621_SPI_MASTER 0x28 +#define MT7621_SPI_MOREBUF 0x2c +#define MT7621_SPI_SPACE 0x3c + +#define MT7621_CPHA BIT(5) +#define MT7621_CPOL BIT(4) +#define MT7621_LSB_FIRST BIT(3) + +#define RT2880_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH) + +struct mt7621_spi; + +struct mt7621_spi { + struct spi_master *master; + void __iomem *base; + unsigned int sys_freq; + unsigned int speed; + struct clk *clk; + spinlock_t lock; + + struct mt7621_spi_ops *ops; +}; + +static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi) +{ + return spi_master_get_devdata(spi->master); +} + +static inline u32 mt7621_spi_read(struct mt7621_spi *rs, u32 reg) +{ + return ioread32(rs->base + reg); +} + +static inline void mt7621_spi_write(struct mt7621_spi *rs, u32 reg, u32 val) +{ + iowrite32(val, rs->base + reg); +} + +static void mt7621_spi_reset(struct mt7621_spi *rs, int duplex) +{ + u32 master = mt7621_spi_read(rs, MT7621_SPI_MASTER); + + master |= 7 << 29; + master |= 1 << 2; + if (duplex) + master |= 1 << 10; + else + master &= ~(1 << 10); + + mt7621_spi_write(rs, MT7621_SPI_MASTER, master); +} + +static void mt7621_spi_set_cs(struct spi_device *spi, int enable) +{ + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi); + int cs = spi->chip_select; + u32 polar = 0; + + mt7621_spi_reset(rs, cs); + if (enable) + polar = BIT(cs); + mt7621_spi_write(rs, MT7621_SPI_POLAR, polar); +} + +static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed) +{ + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi); + u32 rate; + u32 reg; + + dev_dbg(&spi->dev, "speed:%u\n", speed); + + rate = DIV_ROUND_UP(rs->sys_freq, speed); + dev_dbg(&spi->dev, "rate-1:%u\n", rate); + + if (rate > 4097) + return -EINVAL; + + if (rate < 2) + rate = 2; + + reg = mt7621_spi_read(rs, MT7621_SPI_MASTER); + reg &= ~(0xfff << 16); + reg |= (rate - 2) << 16; + rs->speed = speed; + + reg &= ~MT7621_LSB_FIRST; + if (spi->mode & SPI_LSB_FIRST) + reg |= MT7621_LSB_FIRST; + + reg &= ~(MT7621_CPHA | MT7621_CPOL); + switch(spi->mode & (SPI_CPOL | SPI_CPHA)) { + case SPI_MODE_0: + break; + case SPI_MODE_1: + reg |= MT7621_CPHA; + break; + case SPI_MODE_2: + reg |= MT7621_CPOL; + break; + case SPI_MODE_3: + reg |= MT7621_CPOL | MT7621_CPHA; + break; + } + mt7621_spi_write(rs, MT7621_SPI_MASTER, reg); + + return 0; +} + +static inline int mt7621_spi_wait_till_ready(struct spi_device *spi) +{ + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi); + int i; + + for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) { + u32 status; + + status = mt7621_spi_read(rs, MT7621_SPI_TRANS); + if ((status & SPITRANS_BUSY) == 0) { + return 0; + } + cpu_relax(); + udelay(1); + } + + return -ETIMEDOUT; +} + +static int mt7621_spi_transfer_half_duplex(struct spi_master *master, + struct spi_message *m) +{ + struct mt7621_spi *rs = spi_master_get_devdata(master); + struct spi_device *spi = m->spi; + unsigned int speed = spi->max_speed_hz; + struct spi_transfer *t = NULL; + int status = 0; + int i, len = 0; + int rx_len = 0; + u32 data[9] = { 0 }; + u32 val; + + mt7621_spi_wait_till_ready(spi); + + list_for_each_entry(t, &m->transfers, transfer_list) { + const u8 *buf = t->tx_buf; + + if (t->rx_buf) + rx_len += t->len; + + if (!buf) + continue; + + if (t->speed_hz < speed) + speed = t->speed_hz; + + if (WARN_ON(len + t->len > 36)) { + status = -EIO; + goto msg_done; + } + + for (i = 0; i < t->len; i++, len++) + data[len / 4] |= buf[i] << (8 * (len & 3)); + } + + if (WARN_ON(rx_len > 32)) { + status = -EIO; + goto msg_done; + } + + if (mt7621_spi_prepare(spi, speed)) { + status = -EIO; + goto msg_done; + } + data[0] = swab32(data[0]); + if (len < 4) + data[0] >>= (4 - len) * 8; + + for (i = 0; i < len; i += 4) + mt7621_spi_write(rs, MT7621_SPI_OPCODE + i, data[i / 4]); + + val = (min_t(int, len, 4) * 8) << 24; + if (len > 4) + val |= (len - 4) * 8; + val |= (rx_len * 8) << 12; + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val); + + mt7621_spi_set_cs(spi, 1); + + val = mt7621_spi_read(rs, MT7621_SPI_TRANS); + val |= SPI_CTL_START; + mt7621_spi_write(rs, MT7621_SPI_TRANS, val); + + mt7621_spi_wait_till_ready(spi); + + mt7621_spi_set_cs(spi, 0); + + for (i = 0; i < rx_len; i += 4) + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA0 + i); + + m->actual_length = len + rx_len; + + len = 0; + list_for_each_entry(t, &m->transfers, transfer_list) { + u8 *buf = t->rx_buf; + + if (!buf) + continue; + + for (i = 0; i < t->len; i++, len++) + buf[i] = data[len / 4] >> (8 * (len & 3)); + } + +msg_done: + m->status = status; + spi_finalize_current_message(master); + + return 0; +} + +static int mt7621_spi_transfer_full_duplex(struct spi_master *master, + struct spi_message *m) +{ + struct mt7621_spi *rs = spi_master_get_devdata(master); + struct spi_device *spi = m->spi; + unsigned int speed = spi->max_speed_hz; + struct spi_transfer *t = NULL; + int status = 0; + int i, len = 0; + int rx_len = 0; + u32 data[9] = { 0 }; + u32 val = 0; + + mt7621_spi_wait_till_ready(spi); + + list_for_each_entry(t, &m->transfers, transfer_list) { + const u8 *buf = t->tx_buf; + + if (t->rx_buf) + rx_len += t->len; + + if (!buf) + continue; + + if (WARN_ON(len + t->len > 16)) { + status = -EIO; + goto msg_done; + } + + for (i = 0; i < t->len; i++, len++) + data[len / 4] |= buf[i] << (8 * (len & 3)); + if (speed > t->speed_hz) + speed = t->speed_hz; + } + + if (WARN_ON(rx_len > 16)) { + status = -EIO; + goto msg_done; + } + + if (mt7621_spi_prepare(spi, speed)) { + status = -EIO; + goto msg_done; + } + + for (i = 0; i < len; i += 4) + mt7621_spi_write(rs, MT7621_SPI_DATA0 + i, data[i / 4]); + + val |= len * 8; + val |= (rx_len * 8) << 12; + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val); + + mt7621_spi_set_cs(spi, 1); + + val = mt7621_spi_read(rs, MT7621_SPI_TRANS); + val |= SPI_CTL_START; + mt7621_spi_write(rs, MT7621_SPI_TRANS, val); + + mt7621_spi_wait_till_ready(spi); + + mt7621_spi_set_cs(spi, 0); + + for (i = 0; i < rx_len; i += 4) + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA4 + i); + + m->actual_length = rx_len; + + len = 0; + list_for_each_entry(t, &m->transfers, transfer_list) { + u8 *buf = t->rx_buf; + + if (!buf) + continue; + + for (i = 0; i < t->len; i++, len++) + buf[i] = data[len / 4] >> (8 * (len & 3)); + } + +msg_done: + m->status = status; + spi_finalize_current_message(master); + + return 0; +} + +static int mt7621_spi_transfer_one_message(struct spi_master *master, + struct spi_message *m) +{ + struct spi_device *spi = m->spi; + int cs = spi->chip_select; + + if (cs) + return mt7621_spi_transfer_full_duplex(master, m); + return mt7621_spi_transfer_half_duplex(master, m); +} + +static int mt7621_spi_setup(struct spi_device *spi) +{ + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi); + + if ((spi->max_speed_hz == 0) || + (spi->max_speed_hz > (rs->sys_freq / 2))) + spi->max_speed_hz = (rs->sys_freq / 2); + + if (spi->max_speed_hz < (rs->sys_freq / 4097)) { + dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n", + spi->max_speed_hz); + return -EINVAL; + } + + return 0; +} + +static const struct of_device_id mt7621_spi_match[] = { + { .compatible = "ralink,mt7621-spi" }, + {}, +}; +MODULE_DEVICE_TABLE(of, mt7621_spi_match); + +static size_t max_transfer_size(struct spi_device *spi) +{ + return 32; +} + +static int mt7621_spi_probe(struct platform_device *pdev) +{ + const struct of_device_id *match; + struct spi_master *master; + struct mt7621_spi *rs; + unsigned long flags; + void __iomem *base; + struct resource *r; + int status = 0; + struct clk *clk; + struct mt7621_spi_ops *ops; + + match = of_match_device(mt7621_spi_match, &pdev->dev); + if (!match) + return -EINVAL; + ops = (struct mt7621_spi_ops *)match->data; + + r = platform_get_resource(pdev, IORESOURCE_MEM, 0); + base = devm_ioremap_resource(&pdev->dev, r); + if (IS_ERR(base)) + return PTR_ERR(base); + + clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(clk)) { + dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n", + status); + return PTR_ERR(clk); + } + + status = clk_prepare_enable(clk); + if (status) + return status; + + master = spi_alloc_master(&pdev->dev, sizeof(*rs)); + if (master == NULL) { + dev_info(&pdev->dev, "master allocation failed\n"); + return -ENOMEM; + } + + master->mode_bits = RT2880_SPI_MODE_BITS; + + master->setup = mt7621_spi_setup; + master->transfer_one_message = mt7621_spi_transfer_one_message; + master->bits_per_word_mask = SPI_BPW_MASK(8); + master->dev.of_node = pdev->dev.of_node; + master->num_chipselect = 2; + master->max_transfer_size = max_transfer_size; + + dev_set_drvdata(&pdev->dev, master); + + rs = spi_master_get_devdata(master); + rs->base = base; + rs->clk = clk; + rs->master = master; + rs->sys_freq = clk_get_rate(rs->clk); + rs->ops = ops; + dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq); + spin_lock_irqsave(&rs->lock, flags); + + device_reset(&pdev->dev); + + mt7621_spi_reset(rs, 0); + + return spi_register_master(master); +} + +static int mt7621_spi_remove(struct platform_device *pdev) +{ + struct spi_master *master; + struct mt7621_spi *rs; + + master = dev_get_drvdata(&pdev->dev); + rs = spi_master_get_devdata(master); + + clk_disable(rs->clk); + spi_unregister_master(master); + + return 0; +} + +MODULE_ALIAS("platform:" DRIVER_NAME); + +static struct platform_driver mt7621_spi_driver = { + .driver = { + .name = DRIVER_NAME, + .owner = THIS_MODULE, + .of_match_table = mt7621_spi_match, + }, + .probe = mt7621_spi_probe, + .remove = mt7621_spi_remove, +}; + +module_platform_driver(mt7621_spi_driver); + +MODULE_DESCRIPTION("MT7621 SPI driver"); +MODULE_AUTHOR("Felix Fietkau "); +MODULE_LICENSE("GPL");