From patchwork Fri Apr 5 05:57:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rashmica Gupta X-Patchwork-Id: 1078050 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44b8HG4BdDz9sPd for ; Fri, 5 Apr 2019 16:57:46 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="QG0V58ia"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 44b8HG39K2zDqR3 for ; Fri, 5 Apr 2019 16:57:46 +1100 (AEDT) X-Original-To: pdbg@lists.ozlabs.org Delivered-To: pdbg@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:4864:20::544; helo=mail-pg1-x544.google.com; envelope-from=rashmica.g@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="QG0V58ia"; dkim-atps=neutral Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 44b8H13tmvzDqQV for ; Fri, 5 Apr 2019 16:57:33 +1100 (AEDT) Received: by mail-pg1-x544.google.com with SMTP id z9so2469245pgu.10 for ; Thu, 04 Apr 2019 22:57:33 -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=aIURtTLiwITc6F5RsUXDKtxvqhXcmUxGyYZGtreWBTY=; b=QG0V58iapOiAZML4s9tcq9cTHBhFm4okrlE2n9ithi9Dj6vSJn+Tb1FSG5U9GZr+uG anaE/M692TxY1nLdtDMVg3rF2y/4ANhO98kJvOAajI/SKXKATd3+pjIuEVM8d3Rkc3Jc eUqRYd4d4viwzov/y3scDfUCfXv2GhFObmLfqVnhqL/WxS0q1u2mWB/o4zaPZS3dP2t1 qsyrYhJKl4o7EjpDmIPnp1tX5ZKHu9xJstFOXISoAqdLOiQ7Xk9A31ZmfywpjScEchJC XrbCb4vfsIu26WwW03zn/VkmFL2hB/X+vHcfrqs79Hl52hYC8uERbj2sAxiZE0ngPjA/ 0qMQ== 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=aIURtTLiwITc6F5RsUXDKtxvqhXcmUxGyYZGtreWBTY=; b=Y3CTizUyK4zXalU/PyRineYmm7l7UjbMb+QYRpVsAVV/ccm/NsQMdK2Y/yPVQTei84 3SL/IabXDGB42njzrGvOxgzpMrJ3qAN29x+mRj8fhZsJzLLwVO76gqaLCnhc2FS/Vr1W c2xINm+xBbVvkdrO2wzUNTW2FqjvCOyMrjHGJ+m9e+KrOzlwZjdK5iN6o0n8/VFM60jc FSKViStuW7YmsI0Y33U1mk9ECLcs4IpX6DzrAbUMDHZyup7ESvt47jupqlvsGv9cF1+8 Ua7C8dsus3D2j1LQgSXT3ZocsjyB/LJ1VmVcFGxFKRPMsviZe2xod7WPECqTS4Y0SCN3 +Hhg== X-Gm-Message-State: APjAAAVWCtgZEJuJ6HkkmiUgfE7RfxBw4/9m6cNgxQ6sQFZyOaCW8Aqy 4V6OHX8/47sbHm8E3eZgtUlmnSBN X-Google-Smtp-Source: APXvYqxyJ/qS3JGSJBba8OkPp7MZ3+edMjtyFDuga3ITJT6dhbmMYbNZ5l2EEwUirK/jRQGpvbztHQ== X-Received: by 2002:a63:d302:: with SMTP id b2mr9949402pgg.13.1554443850804; Thu, 04 Apr 2019 22:57:30 -0700 (PDT) Received: from rashmica.ozlabs.ibm.com ([122.99.82.10]) by smtp.gmail.com with ESMTPSA id y15sm10810074pfo.22.2019.04.04.22.57.28 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 04 Apr 2019 22:57:30 -0700 (PDT) From: Rashmica Gupta To: pdbg@lists.ozlabs.org, alistair@popple.id.au, joel@jms.id.au Date: Fri, 5 Apr 2019 16:57:15 +1100 Message-Id: <20190405055717.6424-2-rashmica.g@gmail.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190405055717.6424-1-rashmica.g@gmail.com> References: <20190405055717.6424-1-rashmica.g@gmail.com> Subject: [Pdbg] [PATCH 1/3] enable libpdbg to use i2ctools lib X-BeenThere: pdbg@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "mailing list for https://github.com/open-power/pdbg development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: pdbg-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Pdbg" Signed-off-by: Rashmica Gupta --- Makefile.am | 7 +++ configure.ac | 5 +++ libpdbg/i2cm.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++ libpdbg/libpdbg.h | 3 ++ libpdbg/target.c | 11 +++++ libpdbg/target.h | 9 ++++ p9-i2c.dts.m4 | 21 +++++++++ p9-kernel.dts.m4 | 8 ++++ src/i2c.c | 44 +++++++++++++++++++ src/main.c | 5 ++- 10 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 libpdbg/i2cm.c create mode 100644 p9-i2c.dts.m4 create mode 100644 src/i2c.c diff --git a/Makefile.am b/Makefile.am index f1e06f6..95092d3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -71,6 +71,7 @@ pdbg_SOURCES = \ src/cfam.c \ src/htm.c \ src/htm.h \ + src/i2c.c \ src/main.c \ src/main.h \ src/mem.c \ @@ -128,6 +129,11 @@ lib_LTLIBRARIES = libfdt.la libpdbg.la libfdt_la_CFLAGS = -I$(top_srcdir)/libfdt libpdbg_la_CFLAGS = -I$(top_srcdir)/libfdt -Wall -Werror +if I2CLIB +pdbg_LDADD += -li2c +else +libpdbg_la_CFLAGS += -DDISABLE_I2CLIB +endif libfdt_la_SOURCES = \ libfdt/fdt_addresses.c \ @@ -160,6 +166,7 @@ libpdbg_la_SOURCES = \ libpdbg/host.c \ libpdbg/htm.c \ libpdbg/i2c.c \ + libpdbg/i2cm.c \ libpdbg/kernel.c \ libpdbg/libpdbg.c \ libpdbg/libpdbg.h \ diff --git a/configure.ac b/configure.ac index a52e1ed..962be3a 100644 --- a/configure.ac +++ b/configure.ac @@ -42,4 +42,9 @@ want_gdbserver=false, want_gdbserver=true) AM_CONDITIONAL([GDBSERVER], [test x$want_gdbserver = xtrue]) +AC_ARG_ENABLE(i2clib, +AC_HELP_STRING([--disable-i2clib], [disables looking for the i2c lib]), +want_i2clib=false, +want_i2clib=true) +AM_CONDITIONAL([I2CLIB], [test x$want_i2clib = xtrue]) AC_OUTPUT diff --git a/libpdbg/i2cm.c b/libpdbg/i2cm.c new file mode 100644 index 0000000..a6b79c8 --- /dev/null +++ b/libpdbg/i2cm.c @@ -0,0 +1,107 @@ +/* Copyright 2019 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bitutils.h" +#include "operations.h" +#include "debug.h" + + +#include +#include +#include + +#ifndef DISABLE_I2CLIB +#include + +#define MAX_I2C_BUSES 100 +static int i2c_buses[100] = {0}; + +static int kernel_i2c_get(struct i2cm *i2cm, uint32_t addr, uint32_t offset, uint16_t size, uint64_t *data) +{ + int res = 0; + char i2c_path[NAME_MAX]; + int i2c_fd; + int bus; + + // if no device specified, find first bus that gives us data + // just read one byte for now + for (bus = 0; bus < MAX_I2C_BUSES; bus++) { + if (!i2c_buses[bus]) + continue; + + int len = snprintf(i2c_path, sizeof(i2c_path), "/dev/i2c-%d", bus); + if (len >= NAME_MAX) + continue; + + i2c_fd = 0; + i2c_fd = open(i2c_path, O_RDWR); + if (!i2c_fd) + continue; + + + if (ioctl(i2c_fd, I2C_SLAVE, addr) < 0) + continue; + + res = i2c_smbus_read_byte_data(i2c_fd, offset); + if (res >= 0) { + PR_DEBUG("read %x from device %x on bus %s\n", res, addr, i2c_path); + *data = (uint64_t)res; + return res; + } + } + return -1; +} + +static int i2cbus_probe(struct pdbg_target *target) +{ + FILE *f = NULL; + int len; + char n[NAME_MAX]; + + if (!target->index) + return -1; + + len = snprintf(n, NAME_MAX, "/dev/i2c-%i", target->index); + if (len >= NAME_MAX) + return -1; + f = fopen(n, "r"); + if (f == NULL) + return -1; + i2c_buses[target->index] = 1; + return 0; +} + +static struct i2cm i2c_bus = { + .target = { + .name = "I2C Bus", + .compatible = "ibm,power9-i2c-port", + .class = "i2cm", + .probe = i2cbus_probe, + }, + .read = kernel_i2c_get, +}; +DECLARE_HW_UNIT(i2c_bus); +#endif diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h index 4fad158..c9e6569 100644 --- a/libpdbg/libpdbg.h +++ b/libpdbg/libpdbg.h @@ -123,6 +123,9 @@ struct pdbg_target *pdbg_address_absolute(struct pdbg_target *target, uint64_t * int fsi_read(struct pdbg_target *target, uint32_t addr, uint32_t *val); int fsi_write(struct pdbg_target *target, uint32_t addr, uint32_t val); +int i2c_read(struct pdbg_target *target, uint16_t port, uint32_t addr, + uint32_t offset, uint16_t size, uint64_t *data); + int pib_read(struct pdbg_target *target, uint64_t addr, uint64_t *val); int pib_write(struct pdbg_target *target, uint64_t addr, uint64_t val); int pib_wait(struct pdbg_target *pib_dt, uint64_t addr, uint64_t mask, uint64_t data); diff --git a/libpdbg/target.c b/libpdbg/target.c index e678470..1bf252e 100644 --- a/libpdbg/target.c +++ b/libpdbg/target.c @@ -195,6 +195,17 @@ int opb_write(struct pdbg_target *opb_dt, uint32_t addr, uint32_t data) return opb->write(opb, addr64, data); } +int i2c_read(struct pdbg_target *i2cm_dt, uint16_t port, uint32_t addr, uint32_t offset, uint16_t size, uint64_t *data) +{ + struct i2cm *i2cm; + uint64_t addr64 = addr; + + i2cm_dt = get_class_target_addr(i2cm_dt, "i2cm", &addr64); + i2cm = target_to_i2cm(i2cm_dt); + + return i2cm->read(i2cm, port, addr, offset, size, data); +} + int fsi_read(struct pdbg_target *fsi_dt, uint32_t addr, uint32_t *data) { struct fsi *fsi; diff --git a/libpdbg/target.h b/libpdbg/target.h index 04897ed..eb22377 100644 --- a/libpdbg/target.h +++ b/libpdbg/target.h @@ -138,6 +138,15 @@ struct fsi { }; #define target_to_fsi(x) container_of(x, struct fsi, target) +struct i2cm { + struct pdbg_target target; + int (*read)(struct i2cm *, uint16_t, uint32_t, uint32_t, uint16_t, uint64_t *); + int (*write)(struct i2cm *, uint16_t, uint32_t, uint32_t, uint16_t, uint64_t); + bool host; +}; +#define target_to_i2cm(x) container_of(x, struct i2cm, target) + + struct core { struct pdbg_target target; bool release_spwkup; diff --git a/p9-i2c.dts.m4 b/p9-i2c.dts.m4 new file mode 100644 index 0000000..8e8270f --- /dev/null +++ b/p9-i2c.dts.m4 @@ -0,0 +1,21 @@ +define(`I2CBUS', `i2c-bus@$1 { +bus-frequency = <0x61a80>; +compatible = "ibm,opal-i2c", "ibm,power8-i2c-port", "ibm,power9-i2c-port"; +index = <$1>; +reg = <0x0 0x0 $1>; +}')dnl + +I2CBUS(0); +I2CBUS(1); +I2CBUS(2); +I2CBUS(3); +I2CBUS(4); +I2CBUS(5); +I2CBUS(6); +I2CBUS(7); +I2CBUS(8); +I2CBUS(9); +I2CBUS(10); +I2CBUS(11); +I2CBUS(12); +I2CBUS(13); diff --git a/p9-kernel.dts.m4 b/p9-kernel.dts.m4 index 195be59..474beca 100644 --- a/p9-kernel.dts.m4 +++ b/p9-kernel.dts.m4 @@ -22,6 +22,14 @@ include(p9-pib.dts.m4)dnl }; + i2cm@1800 { + #address-cells = <0x2>; + #size-cells = <0x1>; + reg = <0x0 0x1800 0x400>; + compatible = "ibm,kernel-i2c-master"; + include(p9-i2c.dts.m4)dnl + }; + hmfsi@100000 { #address-cells = <0x2>; #size-cells = <0x1>; diff --git a/src/i2c.c b/src/i2c.c new file mode 100644 index 0000000..2a32ec2 --- /dev/null +++ b/src/i2c.c @@ -0,0 +1,44 @@ +/* Copyright 2019 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "main.h" +#include "optcmd.h" +#include "path.h" +#include "target.h" + +static int geti2c(uint16_t port, uint32_t addr, uint32_t offset, uint16_t size) +{ + uint64_t data = 0xc0ffee; + struct pdbg_target *target, *selected = NULL; + + for_each_path_target_class("i2cbus", target) { + if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED) + continue; + selected = target; + if (i2c_read(target, port, addr, offset, size, &data) == 0) + break; + break; + } + + if (selected == NULL) + return -1; + printf("data read: 0x%016" PRIx64 "\n", data); + return 0; +} +OPTCMD_DEFINE_CMD_WITH_ARGS(geti2c, geti2c, (DATA16, ADDRESS32, ADDRESS32, DATA16)); diff --git a/src/main.c b/src/main.c index d5f9385..1702efa 100644 --- a/src/main.c +++ b/src/main.c @@ -93,7 +93,7 @@ extern struct optcmd_cmd optcmd_threadstatus, optcmd_sreset, optcmd_regs, optcmd_probe, optcmd_getmem, optcmd_putmem, optcmd_getmemio, optcmd_putmemio, optcmd_getxer, optcmd_putxer, optcmd_getcr, optcmd_putcr, - optcmd_gdbserver; + optcmd_gdbserver, optcmd_geti2c; static struct optcmd_cmd *cmds[] = { &optcmd_getscom, &optcmd_putscom, &optcmd_getcfam, &optcmd_putcfam, @@ -103,7 +103,7 @@ static struct optcmd_cmd *cmds[] = { &optcmd_threadstatus, &optcmd_sreset, &optcmd_regs, &optcmd_probe, &optcmd_getmem, &optcmd_putmem, &optcmd_getmemio, &optcmd_putmemio, &optcmd_getxer, &optcmd_putxer, &optcmd_getcr, &optcmd_putcr, - &optcmd_gdbserver, + &optcmd_gdbserver, &optcmd_geti2c, }; /* Purely for printing usage text. We could integrate printing argument and flag @@ -145,6 +145,7 @@ static struct action actions[] = { { "sreset", "", "Reset" }, { "regs", "[--backtrace]", "State (optionally display backtrace)" }, { "gdbserver", "", "Start a gdb server" }, + { "geti2c", " ", "Read size bytes from the offset of specified device" }, }; static void print_usage(void)