Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1750509pxb; Wed, 9 Feb 2022 03:49:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJxyHuQMwqxSHFXomg1UEIzYeramTevKrxToFnt53bJnJwcR/aE4qRsFOJkFn8zmybsbiZHA X-Received: by 2002:a17:90b:390f:: with SMTP id ob15mr2133329pjb.160.1644407386369; Wed, 09 Feb 2022 03:49:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644407386; cv=none; d=google.com; s=arc-20160816; b=SSYlO9olG8oOXMY5MgmmtcYhXhYrnAghZEWDa7KCyrpMYl5KeMwD/50YDGq7rQcvA7 QxGhahShvwjzbFCt8NobQ0w5+K3YtqZ8BK664q5CXOtceuT7zieidEbRxSPLnhKSyx5s PsHfmZhE8qvxPugsGXcj+uh9vO+7JMLZhRHGh3WjaSI3CPl0o5Iggr7TesPudBUb72/D 3Bufu26afiv8bQ+MVZtOq/xR97tCwBcmMt2h0onRODdtK72QbDtDKqJeZ7MwkS5i+KoR PmquqRZI/f6IB7OUUj1X1l+N6JzViELaWxdQhI+CkgH72lxvi68VZwsmW0CF5Q+NOAAi Co8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=xW8dsqVCTbHpGN1HtDYyawjXyg1vMQtfluuNijuegrM=; b=mF94kOL0gqEbbe3mNCRbXNkYZIPwxMFfTXa9qt6Wbg5ESf1gMRn7KlnOPsYgdQhWa6 AsCeAdoJbHlO+Q5UzFjySCvUm37ih84kD5OxFq1ASH+eOPp1D+zz7SXzwlTceYwALIRM 3cFHZg/Q/EjzvfXwzKfscxie3V/tQq6d/QA7L6AGb2Qw+JWFfFstN79/JDxP2Gz1zQrD NiYgcVfMQIEkh2oR6PW31E0W76VxQDn+oqTHfIQviH2NU4Mve15nLrqRkrvSIrW4pSZC DKUtWLkGKIFADXV5q8SVbKGRbuJ5lg89piE3ZGPlVGlC/Vov1xfAoC8NCeJ739gBm3s6 sdsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ln+wySWT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 136si15242533pgb.48.2022.02.09.03.49.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 03:49:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ln+wySWT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 446A5E0542A5; Wed, 9 Feb 2022 01:59:43 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244564AbiBGGga (ORCPT + 99 others); Mon, 7 Feb 2022 01:36:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232859AbiBGGaA (ORCPT ); Mon, 7 Feb 2022 01:30:00 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BBF1C043185; Sun, 6 Feb 2022 22:29:59 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id v5-20020a17090a4ec500b001b8b702df57so2085797pjl.2; Sun, 06 Feb 2022 22:29:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=xW8dsqVCTbHpGN1HtDYyawjXyg1vMQtfluuNijuegrM=; b=Ln+wySWTGie2chmEdVdFeSm5V+roiWuVua4yj6Fx0FspGnTNklbAckS+FZ/nA8Tyrn zx+wHFSK+0pqYiV3UZC7xLOiiEXAoSif63Ge1ZX9VwHtPxdZBn4JuVYwB8RjQ03bmXnH xURZlWb3auZdHwpzFRPJN8B7RLhxlHQIHSDQSXDwMDTl9LFo5zcP8OxgMw+9zFTnrSQH PHNxNPQxCMR/AenHMyyPoSi/4Q3/RVoYh3esOrMn5p2eutX62b9l/R4pxF+5L60akcNO ivcjeNQm1V1WNFjYBaG27MlF8zWu+j07U6ByBptc5Q0WY+xkSEcMT/cjfP4HHCxHFkU1 ak5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=xW8dsqVCTbHpGN1HtDYyawjXyg1vMQtfluuNijuegrM=; b=srdq73Y3d9fRLEvqmoixGZVQwwiMjQxVXbCIXx38oM6paQG2IAIEgjPSrgHVQgN2tC kfonI/oJxN0MZcYW9Qn3bQerixA3LIh4zQirG69jWevKHOTNCi86dpVlri6OlWXylRGE hRIdxYc3wJseXNsuli7rnIgv/7zEl+Agra4LVBSRSzaGquupDNg0QynY2jQX39c2NN4F i9k2xIaGU7S+WpueqHKvJI99SmdpBTpRbIHYvXGE1qKHgRQFmSUycDHJhhfcvMzSWXxj yrNLanT4idM+COtRqjR6Gq+7zDT2PFch0QJuiJR14izavmvDoK9B71kzFDmXrGPfdOyx REZw== X-Gm-Message-State: AOAM533iX21LBnHy3FI6qYrw296IthCBmu7mmsDo4RpN1ujgIKOD1e3q SV/GZBAeAIBk8MEyT0VZAcQ= X-Received: by 2002:a17:902:9a06:: with SMTP id v6mr14708423plp.109.1644215398342; Sun, 06 Feb 2022 22:29:58 -0800 (PST) Received: from scdiu3.sunplus.com ([113.196.136.192]) by smtp.googlemail.com with ESMTPSA id j8sm3994386pjc.11.2022.02.06.22.29.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 06 Feb 2022 22:29:57 -0800 (PST) From: Tony Huang To: robh+dt@kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, derek.kiernan@xilinx.com, dragan.cvetic@xilinx.com, arnd@arndb.de, gregkh@linuxfoundation.org, tony.huang@sunplus.com, wells.lu@sunplus.com Cc: Tony Huang Subject: [PATCH v8 2/2] misc: Add iop driver for Sunplus SP7021 Date: Mon, 7 Feb 2022 14:30:06 +0800 Message-Id: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org IOP(8051) embedded inside SP7021 which is used as Processor for I/O control, monitor RTC interrupt and cooperation with CPU & PMC in power management purpose. The IOP core is DQ8051, so also named IOP8051, it supports dedicated JTAG debug pins which share with SP7021. In standby mode operation, the power spec reach 400uA. Signed-off-by: Tony Huang --- Changes in v8: - Addressed comments from Greg KH. Documentation/ABI/testing/sysfs-platform-soc@B | 28 ++ MAINTAINERS | 2 + drivers/misc/Kconfig | 20 ++ drivers/misc/Makefile | 1 + drivers/misc/sunplus_iop.c | 463 +++++++++++++++++++++++++ 5 files changed, 514 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-platform-soc@B create mode 100644 drivers/misc/sunplus_iop.c diff --git a/Documentation/ABI/testing/sysfs-platform-soc@B b/Documentation/ABI/testing/sysfs-platform-soc@B new file mode 100644 index 0000000..d26d6f5 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-platform-soc@B @@ -0,0 +1,28 @@ +What: /sys/devices/platform/soc@B/9c000400.iop/sp_iop_mailbox +Date: January 2022 +KernelVersion: 5.16 +Contact: Tony Huang +Description: + Show IOP's mailbox0 register data. + Format: %x + +What: /sys/devices/platform/soc@B/9c000400.iop/sp_iop_mode +Date: January 2022 +KernelVersion: 5.16 +Contact: Tony Huang +Description: + Read-Write. + + Write this file. + Operation mode of IOP is switched to standby mode by writing + "1" to sysfs. + Operation mode of IOP is switched to normal mode by writing + "0" to sysfs. + Writing of other values is invalid. + + Read this file. + Show operation mode of IOP. "0" is normal mode. "1" is standby + mode. + Format: %x + + diff --git a/MAINTAINERS b/MAINTAINERS index 6f336c9..cbc8dff 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -18245,7 +18245,9 @@ F: drivers/net/ethernet/dlink/sundance.c SUNPLUS IOP DRIVER M: Tony Huang S: Maintained +F: Documentation/ABI/testing/sysfs-platform-soc@B F: Documentation/devicetree/bindings/misc/sunplu-iop.yaml +F: drivers/misc/sunplus_iop.c SUPERH M: Yoshinori Sato diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 0f5a49f..c1d790e 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -470,6 +470,26 @@ config HISI_HIKEY_USB switching between the dual-role USB-C port and the USB-A host ports using only one USB controller. +config SUNPLUS_IOP + tristate "Sunplus IOP support" + default ARCH_SUNPLUS + help + Sunplus I/O processor (8051) driver. + Processor for I/O control, monitor RTC interrupt + and cooperation with CPU&PMC in power management. + + The IOP core is DQ8051, so also named IOP8051. + Need Install DQ8051, The DQ8051 bin file generated by keil C. + We will provide users with 8051 normal and standby source code. + Path: https://sunplus.atlassian.net/wiki/spaces/doc/pages/610172933/ + How+to+use+I+O+processor+8051+of+SP7021#5.-Write-C-or-assembly-source-files-for-IOP + Users can follow the operation steps to generate normal.bin and standby.bin. + Path: https://sunplus.atlassian.net/wiki/spaces/doc/pages/466190338/26.+IOP8051 + 26.5?How To Create 8051 bin file + + This driver can also be built as a module. If so, the module + will be called sunplus_iop. + source "drivers/misc/c2port/Kconfig" source "drivers/misc/eeprom/Kconfig" source "drivers/misc/cb710/Kconfig" diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index a086197..eafeab6 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -52,6 +52,7 @@ obj-$(CONFIG_DW_XDATA_PCIE) += dw-xdata-pcie.o obj-$(CONFIG_PCI_ENDPOINT_TEST) += pci_endpoint_test.o obj-$(CONFIG_OCXL) += ocxl/ obj-$(CONFIG_BCM_VK) += bcm-vk/ +obj-$(CONFIG_SUNPLUS_IOP) += sunplus_iop.o obj-y += cardreader/ obj-$(CONFIG_PVPANIC) += pvpanic/ obj-$(CONFIG_HABANA_AI) += habanalabs/ diff --git a/drivers/misc/sunplus_iop.c b/drivers/misc/sunplus_iop.c new file mode 100644 index 0000000..6ef2de7 --- /dev/null +++ b/drivers/misc/sunplus_iop.c @@ -0,0 +1,463 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * The IOP driver for Sunplus SP7021 + * + * Copyright (C) 2021 Sunplus Technology Inc. + * + * All Rights Reserved. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +enum IOP_Status_e { + IOP_SUCCESS, /* successful */ + IOP_ERR_IOP_BUSY, /* IOP is busy */ +}; + +/* moon0 register offset */ +#define IOP_CLKEN0 0x04 +#define IOP_RESET0 0x54 + +/* IOP register offset */ +#define IOP_CONTROL 0x00 +#define IOP_DATA0 0x20 +#define IOP_DATA1 0x24 +#define IOP_DATA2 0x28 +#define IOP_DATA3 0x2c +#define IOP_DATA4 0x30 +#define IOP_DATA5 0x34 +#define IOP_DATA6 0x38 +#define IOP_DATA7 0x3c +#define IOP_DATA8 0x40 +#define IOP_DATA9 0x44 +#define IOP_DATA10 0x48 +#define IOP_DATA11 0x4c +#define IOP_BASE_ADR_L 0x50 +#define IOP_BASE_ADR_H 0x54 + +/* PMC register offset */ +#define IOP_PMC_TIMER 0x00 +#define IOP_PMC_CTRL 0x04 +#define IOP_XTAL27M_PASSWORD_I 0x08 +#define IOP_XTAL27M_PASSWORD_II 0x0c +#define IOP_XTAL32K_PASSWORD_I 0x10 +#define IOP_XTAL32K_PASSWORD_II 0x14 +#define IOP_CLK27M_PASSWORD_I 0x18 +#define IOP_CLK27M_PASSWORD_II 0x1c +#define IOP_PMC_TIMER2 0x20 + +#define NORMAL_CODE_MAX_SIZE 0X1000 /* Max size of normal.bin that can be received */ +#define STANDBY_CODE_MAX_SIZE 0x4000 /* Max size of standby.bin that can be received */ +struct sp_iop { + struct miscdevice dev; + struct mutex write_lock; /* avoid parallel access */ + void __iomem *iop_regs; + void __iomem *pmc_regs; + void __iomem *moon0_regs; + int irq; + int gpio_wakeup; + unsigned char iop_normal_code[NORMAL_CODE_MAX_SIZE]; + unsigned char iop_standby_code[STANDBY_CODE_MAX_SIZE]; + resource_size_t iop_mem_start; + resource_size_t iop_mem_size; + unsigned char bin_code_mode; +}; + +static void sp_iop_normal_mode(struct sp_iop *iop) +{ + void __iomem *iop_kernel_base; + unsigned int reg; + + iop_kernel_base = ioremap(iop->iop_mem_start, NORMAL_CODE_MAX_SIZE); + memset(iop_kernel_base, 0, NORMAL_CODE_MAX_SIZE); + memcpy(iop_kernel_base, iop->iop_normal_code, NORMAL_CODE_MAX_SIZE); + + writel(0x00100010, iop->moon0_regs + IOP_CLKEN0); + + reg = readl(iop->iop_regs + IOP_CONTROL); + reg |= 0x01; + writel(reg, iop->iop_regs + IOP_CONTROL); + + reg = readl(iop->iop_regs + IOP_CONTROL); + reg &= ~(0x8000); + writel(reg, iop->iop_regs + IOP_CONTROL); + + reg = readl(iop->iop_regs + IOP_CONTROL); + reg |= 0x0200;// disable watchdog event reset IOP + writel(reg, iop->iop_regs + IOP_CONTROL); + + reg = (iop->iop_mem_start & 0xFFFF); + writel(reg, iop->iop_regs + IOP_BASE_ADR_L); + reg = (iop->iop_mem_start >> 16); + writel(reg, iop->iop_regs + IOP_BASE_ADR_H); + + reg = readl(iop->iop_regs + IOP_CONTROL); + reg &= ~(0x01); + writel(reg, iop->iop_regs + IOP_CONTROL); + iop->bin_code_mode = 0; +} + +static void sp_iop_standby_mode(struct sp_iop *iop) +{ + void __iomem *iop_kernel_base; + unsigned long reg; + + iop_kernel_base = ioremap(iop->iop_mem_start, STANDBY_CODE_MAX_SIZE); + memset(iop_kernel_base, 0, STANDBY_CODE_MAX_SIZE); + memcpy(iop_kernel_base, iop->iop_standby_code, STANDBY_CODE_MAX_SIZE); + + writel(0x00100010, iop->moon0_regs + IOP_CLKEN0); + + reg = readl(iop->iop_regs + IOP_CONTROL); + reg |= 0x01; + writel(reg, iop->iop_regs + IOP_CONTROL); + + reg = readl(iop->iop_regs + IOP_CONTROL); + reg &= ~(0x8000); + writel(reg, iop->iop_regs + IOP_CONTROL); + + reg = readl(iop->iop_regs + IOP_CONTROL); + reg |= 0x0200;// disable watchdog event reset IOP + writel(reg, iop->iop_regs + IOP_CONTROL); + + reg = (iop->iop_mem_start & 0xFFFF); + writel(reg, iop->iop_regs + IOP_BASE_ADR_L); + reg = (iop->iop_mem_start >> 16); + writel(reg, iop->iop_regs + IOP_BASE_ADR_H); + + reg = readl(iop->iop_regs + IOP_CONTROL); + reg &= ~(0x01); + writel(reg, iop->iop_regs + IOP_CONTROL); + iop->bin_code_mode = 1; +} + +/* 8051 informs linux kerenl. 8051 has been switched to standby.bin code. */ +#define IOP_READY 0x0004 +#define RISC_READY 0x0008 + +/* System linux kernel tells 8051 which gpio pin to wake-up through. */ +#define WAKEUP_PIN 0xFE02 + +/* System linux kernel tells 8051 to execute S1 or S3 mode. */ +#define S1 0x5331 +#define S3 0x5333 + +static int sp_iop_s3mode(struct device *dev, struct sp_iop *iop) +{ + unsigned int reg; + int ret, value; + + /* PMC set */ + writel(0x00010001, iop->pmc_regs + IOP_PMC_TIMER); + reg = readl(iop->pmc_regs + IOP_PMC_CTRL); + /* disable system reset PMC, enalbe power down IOP Domain, enable gating clock 27Mhz */ + reg |= 0x23; + writel(reg, iop->pmc_regs + IOP_PMC_CTRL); + + writel(0x55aa00ff, iop->pmc_regs + IOP_XTAL27M_PASSWORD_I); + writel(0x00ff55aa, iop->pmc_regs + IOP_XTAL27M_PASSWORD_II); + writel(0xaa00ff55, iop->pmc_regs + IOP_XTAL32K_PASSWORD_I); + writel(0xff55aa00, iop->pmc_regs + IOP_XTAL32K_PASSWORD_II); + writel(0xaaff0055, iop->pmc_regs + IOP_CLK27M_PASSWORD_I); + writel(0x5500aaff, iop->pmc_regs + IOP_CLK27M_PASSWORD_II); + writel(0x01000100, iop->pmc_regs + IOP_PMC_TIMER2); + + /* IOP Hardware IP reset */ + reg = readl(iop->moon0_regs + IOP_RESET0); + reg |= 0x10; + writel(reg, (iop->moon0_regs + IOP_RESET0)); + reg &= ~(0x10); + writel(reg, (iop->moon0_regs + IOP_RESET0)); + + writel(0x00ff0085, (iop->moon0_regs + 32 * 4 * 1 + 4 * 1)); + + reg = readl(iop->moon0_regs + 32 * 4 * 1 + 4 * 2); + reg |= 0x08000800; + writel(reg, (iop->moon0_regs + 32 * 4 * 1 + 4 * 2)); + + writel(WAKEUP_PIN, iop->iop_regs + IOP_DATA0); + writel(iop->gpio_wakeup, iop->iop_regs + IOP_DATA1); + + ret = readl_poll_timeout(iop->iop_regs + IOP_DATA2, value, + (value & IOP_READY) == IOP_READY, 1000, 10000); + if (ret) { + dev_err(dev, "timed out\n"); + return ret; + } + + reg = RISC_READY; + writel(reg, iop->iop_regs + IOP_DATA2); + reg = 0x0000; + writel(reg, iop->iop_regs + IOP_DATA5); + reg = 0x0060; + writel(reg, iop->iop_regs + IOP_DATA6); + + ret = readl_poll_timeout(iop->iop_regs + IOP_DATA7, value, + value == 0xaaaa, 1000, 10000); + if (ret) { + dev_err(dev, "timed out\n"); + return ret; + } + + /* 8051 bin file call Ultra low function. */ + writel(0xdd, iop->iop_regs + IOP_DATA1); + /* + * When the execution is here, the system linux kernel + * is about to be powered off + * The purpose of mdelay(10): Do not let the system linux + * kernel continue to run other programs. + */ + mdelay(10); + return 0; +} + +static int sp_iop_s1mode(struct device *dev, struct sp_iop *iop) +{ + int ret, value; + + ret = readl_poll_timeout(iop->iop_regs + IOP_DATA2, value, + (value & IOP_READY) == IOP_READY, 1000, 10000); + if (ret) { + dev_err(dev, "timed out\n"); + return ret; + } + + writel(RISC_READY, iop->iop_regs + IOP_DATA2); + writel(0x0000, iop->iop_regs + IOP_DATA5); + writel(0x0060, iop->iop_regs + IOP_DATA6); + + ret = readl_poll_timeout(iop->iop_regs + IOP_DATA7, value, + value == 0xaaaa, 1000, 10000); + if (ret) { + dev_err(dev, "timed out\n"); + return ret; + } + + /* 8051 bin file call S1_mode function. */ + writel(0xee, iop->iop_regs + IOP_DATA1); + /* + * When the execution is here, the system linux kernel + * is about to be powered off + * The purpose of mdelay(10): Do not let the system linux + * kernel continue to run other programs. + */ + mdelay(10); + return 0; +} + +static ssize_t mailbox_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct sp_iop *iop = dev_get_drvdata(dev); + unsigned int mailbox; + + mailbox = readl(iop->iop_regs + IOP_DATA0); + return sysfs_emit(buf, "%x\n", mailbox); +} + +static DEVICE_ATTR_RO(mailbox); + +static ssize_t mode_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct sp_iop *iop = dev_get_drvdata(dev); + + return sysfs_emit(buf, "%x\n", iop->bin_code_mode); +} + +static ssize_t mode_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct sp_iop *iop = dev_get_drvdata(dev); + + if (sysfs_streq(buf, "0")) + sp_iop_normal_mode(iop); + else if (sysfs_streq(buf, "1")) + sp_iop_standby_mode(iop); + else + return -EINVAL; + return count; +} + +static DEVICE_ATTR_RW(mode); + +static struct attribute *sp_iop_attrs[] = { + &dev_attr_mailbox.attr, + &dev_attr_mode.attr, + NULL, +}; + +ATTRIBUTE_GROUPS(sp_iop); + +static int sp_iop_get_normal_code(struct device *dev, struct sp_iop *iop) +{ + const struct firmware *fw; + static const char file[] = "normal.bin"; + unsigned int err, i; + + err = request_firmware(&fw, file, dev); + if (err) { + dev_err(dev, "get bin file error\n"); + return err; + } + + for (i = 0; i < NORMAL_CODE_MAX_SIZE; i++) { + char temp; + + temp = fw->data[i]; + iop->iop_normal_code[i] = temp; + } + release_firmware(fw); + return err; +} + +static int sp_iop_get_standby_code(struct device *dev, struct sp_iop *iop) +{ + const struct firmware *fw; + static const char file[] = "standby.bin"; + unsigned int err, i; + + err = request_firmware(&fw, file, dev); + if (err) { + dev_err(dev, "get bin file error\n"); + return err; + } + + for (i = 0; i < STANDBY_CODE_MAX_SIZE; i++) { + char temp; + + temp = fw->data[i]; + iop->iop_standby_code[i] = temp; + } + release_firmware(fw); + return err; +} + +static int sp_iop_get_resources(struct platform_device *pdev, struct sp_iop *p_sp_iop_info) +{ + struct resource *r; + + r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "iop"); + p_sp_iop_info->iop_regs = devm_ioremap_resource(&pdev->dev, r); + if (IS_ERR(p_sp_iop_info->iop_regs)) { + dev_err(&pdev->dev, "ioremap fail\n"); + return PTR_ERR(p_sp_iop_info->iop_regs); + } + + r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "iop_pmc"); + p_sp_iop_info->pmc_regs = devm_ioremap_resource(&pdev->dev, r); + if (IS_ERR(p_sp_iop_info->pmc_regs)) { + dev_err(&pdev->dev, "ioremap fail\n"); + return PTR_ERR(p_sp_iop_info->pmc_regs); + } + + r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "moon0"); + p_sp_iop_info->moon0_regs = devm_ioremap_resource(&pdev->dev, r); + if (IS_ERR(p_sp_iop_info->moon0_regs)) { + dev_err(&pdev->dev, "ioremap fail\n"); + return PTR_ERR(p_sp_iop_info->moon0_regs); + } + return IOP_SUCCESS; +} + +static int sp_iop_platform_driver_probe(struct platform_device *pdev) +{ + int ret = -ENXIO; + int rc; + struct sp_iop *iop; + struct device_node *memnp; + struct resource mem_res; + + iop = devm_kzalloc(&pdev->dev, sizeof(struct sp_iop), GFP_KERNEL); + if (!iop) { + ret = -ENOMEM; + goto fail_kmalloc; + } + /* init */ + mutex_init(&iop->write_lock); + ret = sp_iop_get_resources(pdev, iop); + + // Get reserve address + memnp = of_parse_phandle(pdev->dev.of_node, "memory-region", 0); + if (!memnp) { + dev_err(&pdev->dev, "no memory-region node\n"); + return -EINVAL; + } + + rc = of_address_to_resource(memnp, 0, &mem_res); + of_node_put(memnp); + if (rc) { + dev_err(&pdev->dev, "failed to translate memory-region to a resource\n"); + return -EINVAL; + } + + iop->iop_mem_start = mem_res.start; + iop->iop_mem_size = resource_size(&mem_res); + + ret = sp_iop_get_normal_code(&pdev->dev, iop); + if (ret != 0) { + dev_err(&pdev->dev, "get normal code err=%d\n", ret); + return ret; + } + + ret = sp_iop_get_standby_code(&pdev->dev, iop); + if (ret != 0) { + dev_err(&pdev->dev, "get standby code err=%d\n", ret); + return ret; + } + + sp_iop_normal_mode(iop); + platform_set_drvdata(pdev, iop); + iop->gpio_wakeup = of_get_named_gpio(pdev->dev.of_node, "iop-wakeup", 0); + return 0; + +fail_kmalloc: + return ret; +} + +static void sp_iop_platform_driver_shutdown(struct platform_device *pdev) +{ + struct sp_iop *iop = platform_get_drvdata(pdev); + unsigned int ret, value; + + value = readl(iop->iop_regs + IOP_DATA11); + sp_iop_standby_mode(iop); + + ret = readl_poll_timeout(iop->iop_regs + IOP_DATA0, value, + value == 0x2222, 1000, 100000); + if (ret) + dev_warn(&pdev->dev, "timed out\n"); + + if (value == S1) + sp_iop_s1mode(&pdev->dev, iop); + else + sp_iop_s3mode(&pdev->dev, iop); +} + +static const struct of_device_id sp_iop_of_match[] = { + { .compatible = "sunplus,sp7021-iop" }, + { /* sentinel */ }, +}; + +MODULE_DEVICE_TABLE(of, sp_iop_of_match); + +static struct platform_driver sp_iop_platform_driver = { + .probe = sp_iop_platform_driver_probe, + .shutdown = sp_iop_platform_driver_shutdown, + .driver = { + .name = "sunplus,sp7021-iop", + .of_match_table = sp_iop_of_match, + .dev_groups = sp_iop_groups, + } +}; + +module_platform_driver(sp_iop_platform_driver); + +MODULE_AUTHOR("Tony Huang "); +MODULE_DESCRIPTION("Sunplus IOP Driver"); +MODULE_LICENSE("GPL v2"); -- 2.7.4