From patchwork Sun Mar 21 11:38:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magnus Damm X-Patchwork-Id: 12153003 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3392EC433C1 for ; Sun, 21 Mar 2021 12:13:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 07CB261944 for ; Sun, 21 Mar 2021 12:13:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229886AbhCUMMl (ORCPT ); Sun, 21 Mar 2021 08:12:41 -0400 Received: from lists.levonline.com ([217.70.33.37]:46241 "EHLO lists.levonline.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229863AbhCUMMN (ORCPT ); Sun, 21 Mar 2021 08:12:13 -0400 Received: from exc-halon1.levonline.com (exc-halon1.levonline.com [217.70.32.123]) by lists.levonline.com (Postfix) with ESMTP id 6F27C3A12E2 for ; Sun, 21 Mar 2021 13:12:11 +0100 (CET) X-SA-score: -1 X-Halon-ID: a9ff634c-8a3e-11eb-a51d-0050568168d4 Received: from ormen1.djurnet.levonline.com (ormen1.djurnet.levonline.com [192.168.17.31]) by exc-halon1.levonline.com (Halon) with ESMTPS id a9ff634c-8a3e-11eb-a51d-0050568168d4; Sun, 21 Mar 2021 13:12:11 +0100 (CET) Received: from [127.0.0.1] (l193216.ppp.asahi-net.or.jp [218.219.193.216]) (authenticated bits=0) by ormen1.djurnet.levonline.com (8.13.8/8.13.8) with ESMTP id 12LCC9LU028139; Sun, 21 Mar 2021 13:12:10 +0100 X-Origin-Levonline: b0359001 From: Magnus Damm To: linux-renesas-soc@vger.kernel.org Cc: Magnus Damm , geert+renesas@glider.be Date: Sun, 21 Mar 2021 20:38:43 +0900 Message-Id: <161632672360.9191.17012441985597645272.sendpatchset@octo> In-Reply-To: <161632671592.9191.18170615656272399147.sendpatchset@octo> References: <161632671592.9191.18170615656272399147.sendpatchset@octo> Subject: [PATCH 1/2] sh73a0 CMT1 device exposed via UIO Precedence: bulk List-ID: X-Mailing-List: linux-renesas-soc@vger.kernel.org From: Magnus Damm Modify the kernel slightly to expose CMT1 via UIO and print some IRQ-related messages from UIO for debugging purpose. Not-Yet-Signed-off-by: Magnus Damm --- arch/arm/boot/dts/sh73a0.dtsi | 2 +- drivers/uio/uio.c | 3 ++- drivers/uio/uio_pdrv_genirq.c | 10 ++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) --- 0001/arch/arm/boot/dts/sh73a0.dtsi +++ work/arch/arm/boot/dts/sh73a0.dtsi 2021-03-21 18:15:44.683760186 +0900 @@ -99,7 +99,7 @@ }; cmt1: timer@e6138000 { - compatible = "renesas,sh73a0-cmt1"; + compatible = "uio_pdrv_genirq"; reg = <0xe6138000 0x200>; interrupts = ; clocks = <&mstp3_clks SH73A0_CLK_CMT1>; --- 0001/drivers/uio/uio.c +++ work/drivers/uio/uio.c 2021-03-21 18:14:34.880755790 +0900 @@ -11,7 +11,7 @@ * * Base Functions */ - +#define DEBUG #include #include #include @@ -975,6 +975,7 @@ int __uio_register_device(struct module * FDs at the time of unregister and therefore may not be * freed until they are released. */ + pr_debug("uio request_irq %lu\n", info->irq); ret = request_irq(info->irq, uio_interrupt, info->irq_flags, info->name, idev); if (ret) { --- 0001/drivers/uio/uio_pdrv_genirq.c +++ work/drivers/uio/uio_pdrv_genirq.c 2021-03-21 18:15:03.120757568 +0900 @@ -10,7 +10,7 @@ * Copyright (C) 2008 by Digi International Inc. * All rights reserved. */ - +#define DEBUG #include #include #include @@ -66,6 +66,8 @@ static irqreturn_t uio_pdrv_genirq_handl * remember the state so we can allow user space to enable it later. */ + pr_debug("irqhandler %d\n", irq); + spin_lock(&priv->lock); if (!__test_and_set_bit(UIO_IRQ_DISABLED, &priv->flags)) disable_irq_nosync(irq); @@ -87,6 +89,8 @@ static int uio_pdrv_genirq_irqcontrol(st * with irq handler on SMP systems. */ + pr_debug("irqcontrol %d\n", irq_on); + spin_lock_irqsave(&priv->lock, flags); if (irq_on) { if (__test_and_clear_bit(UIO_IRQ_DISABLED, &priv->flags)) @@ -172,6 +176,8 @@ static int uio_pdrv_genirq_probe(struct } } + pr_debug("uio irq %lu\n", uioinfo->irq); + if (uioinfo->irq) { struct irq_data *irq_data = irq_get_irq_data(uioinfo->irq); @@ -276,7 +282,7 @@ static const struct dev_pm_ops uio_pdrv_ #ifdef CONFIG_OF static struct of_device_id uio_of_genirq_match[] = { - { /* This is filled with module_parm */ }, + { .compatible = "uio_pdrv_genirq", }, { /* Sentinel */ }, }; MODULE_DEVICE_TABLE(of, uio_of_genirq_match); From patchwork Sun Mar 21 11:38:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magnus Damm X-Patchwork-Id: 12153001 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47F89C433DB for ; Sun, 21 Mar 2021 12:13:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E57F61947 for ; Sun, 21 Mar 2021 12:13:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229784AbhCUMMn (ORCPT ); Sun, 21 Mar 2021 08:12:43 -0400 Received: from lists.levonline.com ([217.70.33.37]:46250 "EHLO lists.levonline.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229897AbhCUMMU (ORCPT ); Sun, 21 Mar 2021 08:12:20 -0400 Received: from exc-halon1.levonline.com (exc-halon1.levonline.com [217.70.32.123]) by lists.levonline.com (Postfix) with ESMTP id 32E5A3A12E2 for ; Sun, 21 Mar 2021 13:12:19 +0100 (CET) X-SA-score: -1 X-Halon-ID: ae9e0758-8a3e-11eb-a51d-0050568168d4 Received: from ormen1.djurnet.levonline.com (ormen1.djurnet.levonline.com [192.168.17.31]) by exc-halon1.levonline.com (Halon) with ESMTPS id ae9e0758-8a3e-11eb-a51d-0050568168d4; Sun, 21 Mar 2021 13:12:18 +0100 (CET) Received: from [127.0.0.1] (l193216.ppp.asahi-net.or.jp [218.219.193.216]) (authenticated bits=0) by ormen1.djurnet.levonline.com (8.13.8/8.13.8) with ESMTP id 12LCCGxT028162; Sun, 21 Mar 2021 13:12:17 +0100 X-Origin-Levonline: b0359001 From: Magnus Damm To: linux-renesas-soc@vger.kernel.org Cc: Magnus Damm , geert+renesas@glider.be Date: Sun, 21 Mar 2021 20:38:51 +0900 Message-Id: <161632673124.9191.8069161888906800635.sendpatchset@octo> In-Reply-To: <161632671592.9191.18170615656272399147.sendpatchset@octo> References: <161632671592.9191.18170615656272399147.sendpatchset@octo> Subject: [PATCH 2/2] UIO CMT test program Precedence: bulk List-ID: X-Mailing-List: linux-renesas-soc@vger.kernel.org --- /dev/null 2019-10-16 00:27:13.659405289 +0900 +++ uio-cmt-test-20210321.c 2021-03-21 19:41:24.469083859 +0900 @@ -0,0 +1,179 @@ +/* + * uio-cmt-test-20210321.c - UIO CMT example test code, 20210321 Magnus Damm + * + * A small linux program that programs the CMT timer and waits for IRQs + * + * Compile for Linux using: + * $ cross-gcc -o uio-cmt-test uio-cmt-test.c + * + * Designed to work with the Linux UIO kernel driver uio_pdrv_genirq.c + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static int fgets_with_openclose(char *fname, char *buf, size_t maxlen) { + FILE *fp; + + if ((fp = fopen(fname, "r")) != NULL) { + fgets(buf, maxlen, fp); + fclose(fp); + return strlen(buf); + } else { + return -1; + } +} + +struct uio_device { + char *name; + char *path; + int fd; +}; + +#define MAXUIOIDS 100 +#define MAXNAMELEN 256 + +static int locate_uio_device(char *name, struct uio_device *udp) +{ + char fname[MAXNAMELEN], buf[MAXNAMELEN]; + int uio_id, i; + + for (uio_id = 0; uio_id < MAXUIOIDS; uio_id++) { + sprintf(fname, "/sys/class/uio/uio%d/name", uio_id); + if (fgets_with_openclose(fname, buf, MAXNAMELEN) < 0) + continue; + if (strncmp(name, buf, strlen(name)) == 0) + break; + } + + if (uio_id >= MAXUIOIDS) + return -1; + + udp->name = strdup(buf); + udp->path = strdup(fname); + udp->path[strlen(udp->path) - 4] = '\0'; + + sprintf(buf, "/dev/uio%d", uio_id); + udp->fd = open(buf, O_RDWR|O_SYNC /*| O_NONBLOCK*/); + + if (udp->fd < 0) { + perror("open"); + return -1; + } + + return 0; +} + +/* offsets apply to sh73a0 */ +#define CMT_CMSTR 0x000 /* start/stop register, some bits reserved as 1 */ +#define CMT_CMSTR_CH0 (1 << 0) /* set bit to one to start channel */ +#define CMT_CMCSR 0x10 /* 0x124 enables interrupts and selects CLK/8 */ +#define CMT_CMCSR_CMF (1 << 15) /* clear bit to ack compare match event */ +#define CMT_CMCNT 0x14 /* counting up, set to 0 */ +#define CMT_CMCOR 0x18 /* match value, set to ~0 */ + +struct uio_map { + unsigned long address; + unsigned long size; + void *iomem; +}; + +static int setup_uio_map(struct uio_device *udp, int nr, struct uio_map *ump) +{ + char fname[MAXNAMELEN], buf[MAXNAMELEN]; + + sprintf(fname, "%s/maps/map%d/addr", udp->path, nr); + if (fgets_with_openclose(fname, buf, MAXNAMELEN) <= 0) + return -1; + + ump->address = strtoul(buf, NULL, 0); + + sprintf(fname, "%s/maps/map%d/size", udp->path, nr); + if (fgets_with_openclose(fname, buf, MAXNAMELEN) <= 0) + return -1; + + ump->size = strtoul(buf, NULL, 0); + + ump->iomem = mmap(0, ump->size, + PROT_READ|PROT_WRITE, MAP_SHARED, + udp->fd, nr * getpagesize()); + + if (ump->iomem == MAP_FAILED) + return -1; + + return 0; +} + +struct uio_device uio_dev; +struct uio_map uio_mmio; + +int main(int argc, char *argv[]) +{ + int k; + int ret; + + ret = locate_uio_device("timer", &uio_dev); + if (ret < 0) + return ret; + + printf("found matching UIO device at %s\n", uio_dev.path); + + ret = setup_uio_map(&uio_dev, 0, &uio_mmio); + if (ret < 0) + return ret; + + { + unsigned long *cmstr = (uio_mmio.iomem + CMT_CMSTR); + unsigned short *cmcsr = (uio_mmio.iomem + CMT_CMCSR); + unsigned long *cmcor = (uio_mmio.iomem + CMT_CMCOR); + + /* Stop timer channel */ + *cmstr &= ~CMT_CMSTR_CH0; + + /* Initialize CMCSR */ + *cmcsr = 0x124; + + /* Initialize CMCOR */ + *cmcor = (32768 / 8) * 2; /* interrupt after about 2s */ + + /* Enable interrupt in UIO driver */ + { + unsigned long enable = 1; + write(uio_dev.fd, &enable, sizeof(u_long)); + } + + /* Start timer channel */ + *cmstr |= CMT_CMSTR_CH0; + + /* test by processing 3 interrupts */ + for (k = 0; k < 3; k++) { + /* Wait for interrupt */ + { + unsigned long n_pending; + read(uio_dev.fd, &n_pending, sizeof(u_long)); + } + + printf("IRQ nr %d\n", k); + + /* ack match in CMCSR */ + *cmcsr &= ~CMT_CMCSR_CMF; + + /* Enable interrupt in UIO driver */ + { + unsigned long enable = 1; + write(uio_dev.fd, &enable, sizeof(u_long)); + } + } + + /* Stop timer channel */ + *cmstr &= ~CMT_CMSTR_CH0; + } + + return 0; +}