From patchwork Tue Jan 30 23:30:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Corona, Ernesto" X-Patchwork-Id: 1974059 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=l/6IV3Iw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=linux-aspeed-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=patchwork.ozlabs.org) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WnqJY1xbbz1yXf for ; Tue, 20 Aug 2024 09:57:01 +1000 (AEST) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4WnqHy5FRQz3gBB for ; Tue, 20 Aug 2024 09:56:30 +1000 (AEST) X-Original-To: linux-aspeed@lists.ozlabs.org Delivered-To: linux-aspeed@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=l/6IV3Iw; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=intel.com (client-ip=192.198.163.11; helo=mgamail.intel.com; envelope-from=ernesto.corona@intel.com; receiver=lists.ozlabs.org) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 4TPhHW1VY0z30g6 for ; Wed, 31 Jan 2024 10:30:46 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706657447; x=1738193447; h=from:to:cc:subject:date:message-id: content-transfer-encoding:mime-version; bh=gZSmjnhj1cKidIcN0tr3F52VAZUFTwNtRcBhvHLCFHQ=; b=l/6IV3IwUpRDuL4UN8F0GW+8UYRR++bGTAImk6qTBcEFScrWA/9g0Dzc 9Hv3VKNUZIABw4aX2+03duLxQWv2DcqVr//bjBQ39Zh7xYGuYWEGwerIZ ROZYqDILQkDu2fnewcADR5huSvzrFPfJpF/HIfZ3KF/+5A9uAfTp7lUuW Hsg0IV6voKC9hGqfcbSfNNoOCSAOnKwtsgPY2BYut8hg4BzR7I9Jhp+4s 7H71qKqaOYTPb3VeMPaCOcZqbI99PzIGA0c4TsWJmdKzi1WdXDpgdjvSD mM/HGi3jo8GaqN7KtlrkF9g+NW6Rmy33V8/lUjGnT7L7Ut8j/zyuVpK7m Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="10100736" X-IronPort-AV: E=Sophos;i="6.05,230,1701158400"; d="scan'208";a="10100736" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2024 15:30:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,230,1701158400"; d="scan'208";a="36670166" Received: from fmsmsx603.amr.corp.intel.com ([10.18.126.83]) by orviesa001.jf.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 30 Jan 2024 15:30:44 -0800 Received: from fmsmsx612.amr.corp.intel.com (10.18.126.92) by fmsmsx603.amr.corp.intel.com (10.18.126.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Tue, 30 Jan 2024 15:30:42 -0800 Received: from fmsedg602.ED.cps.intel.com (10.1.192.136) by fmsmsx612.amr.corp.intel.com (10.18.126.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35 via Frontend Transport; Tue, 30 Jan 2024 15:30:42 -0800 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (104.47.74.41) by edgegateway.intel.com (192.55.55.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Tue, 30 Jan 2024 15:30:42 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KN8dX0x+fYjv5+GtUYcddUgRJl6k9eOADA12FIl7QuSXBz5g+wjvuwL8lYkOGykZtJ2qO8/vDSSJ+Buhq6HEpwrd5za/C2BrR/MorlWJXX94ZEHVeMS5+xeC0lr6DQse26cNT0ulDkfjSI9v75T1CLIaI5fzYvu3TqyVEiDE10pi8SlR2Nr2pMY+VekN7fDjGRJoRQQnVxFYdYgMRahJwMLzUuN5VtsxMq1wMbW4mbFovX1evNqGzQ6cfltqb5sxdcaO1Ng0NMyRQmK7q3wYUWw+46WK3/TgY7H1nCWoM7F7rls/RZPietdsBFqBmEA0WLJwHmPvkndo+MHQ6LdgCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SYJpYq7rYgy2OSQn16cPR/u196rYFqEAOOjUvnXmCvI=; b=iqUUuCNkcG1O0gJjQpQpw6wbZasBP47kJr5LK+Mi+8mde3F4ZsJwyzE9vC1pktMhKryLs9g4Dc9rOmL6NKg/tyMCXyVpQIuXe6FM+DNmqCqZ0x4di2wYxTCKkZxg5Xt1A15C3v1AG579t6DpiabyexeMH+X13/WjGYujncWUOOX5ekR2z1UhZSuzl6bJjljf77pCkNPGZWl2ljIWf3CSVSZ5LrHvRmhjS/qHpm6PoAYX3UPVzFOeXuwRb9env/ZFKo9vuEh4t7GQYudmzxn5VCUyQOHkCpJt0wAcxqWu8ujuHkH3Zc8P17JUy7xmK9juOCcW50wv4SzrMdzDGWAY3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from LV8PR11MB8463.namprd11.prod.outlook.com (2603:10b6:408:1ed::6) by DM6PR11MB4689.namprd11.prod.outlook.com (2603:10b6:5:2a0::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.22; Tue, 30 Jan 2024 23:30:31 +0000 Received: from LV8PR11MB8463.namprd11.prod.outlook.com ([fe80::5262:6eb1:2787:8cb9]) by LV8PR11MB8463.namprd11.prod.outlook.com ([fe80::5262:6eb1:2787:8cb9%3]) with mapi id 15.20.7228.029; Tue, 30 Jan 2024 23:30:31 +0000 From: "Corona, Ernesto" To: "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-aspeed@lists.ozlabs.org" Subject: [PATCH 30 3/7] Add Aspeed SOC 24xx, 25xx and 26xx JTAG support Thread-Topic: [PATCH 30 3/7] Add Aspeed SOC 24xx, 25xx and 26xx JTAG support Thread-Index: AdpTxJb+u3okvsebTzmSUh9xTAF4Cw== Date: Tue, 30 Jan 2024 23:30:31 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: LV8PR11MB8463:EE_|DM6PR11MB4689:EE_ x-ms-office365-filtering-correlation-id: 6ae39b73-2c1b-435d-3fdb-08dc21eb731e x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: GOyk2OMGqRZhK4mz0Fcm+dwfMN1MOPtUvaQ9vNs+NOcIcvJjpyHgcKY/72eoVHqY6cPaPbvBti8n3cJ9HO5H8o/bmEnZifDa94k+9HO0ggbaVbrcPwK/uoA4mUyTAcHv4/llKwgNaOjn/dfGLWIXueYv5TNXKbcdbWtFnsFxrIFTCJzI+444zVDe9Vb6TrI9bZu3zt31wzy2zbUqvh5DOnhA6Wqsr6jSsLHVYSJFEu0vBh+dp5hYjgGf9lNPEEzZqO/CbE7m8QlS0CX6EoPst7eT0O++Bj3eqm9o15vDIovO65kypRxJ8+zsd45cC3VOA3V0M/cvOPVBM2GM1DwBhSiaXiYWrWC3njk0DBpArho4mB3RM1sPj7apsui/vsdWbaySlluTSgSGVVNr+i77OFFHInzQtynv9tZGc9+06ZoYfZ6tvV5wckpUd9D2cJ7Z7jEKR1ksIq+1cCBKnkRa06qyLKF3qRgt4yQb8fnEGnw8O6GyjkLjDGsoKxXyrESXRBiQOpLGV6UFRHnSpu8zU/af9SNK+xe1kevlBtUn9xH1bDxrd8kkiZzt2Q7Jr8Fyi1+v1vNvkJtneW5JW9BfK1VO8hTUQA5qkXB5pw9dElWGjUBH5z2Zijlm22InwBWn x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV8PR11MB8463.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376002)(346002)(136003)(396003)(366004)(39860400002)(230922051799003)(186009)(64100799003)(451199024)(1800799012)(83380400001)(9686003)(38100700002)(26005)(30864003)(122000001)(8676002)(52536014)(8936002)(4326008)(7416002)(478600001)(76116006)(7696005)(64756008)(54906003)(6506007)(2906002)(5660300002)(66556008)(66476007)(71200400001)(66946007)(66446008)(110136005)(41300700001)(316002)(38070700009)(82960400001)(86362001)(33656002)(55016003)(579004)(559001);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: rCz/0E8eQxSG0tRWDEC4Pr4Tz1yDsDlfC1kw9fGcbMMrTqUK7llw8w6ssCbXpWHdv1+XhPa77hDQrmsBEVV9RXJCWZRsy488V7JgVc6q8YKjAqqVeCIhlTdxYxUuB4gv4TVaZA7A0Rn279GgHtMLqoUxo7dWcqxrHP1kBvIIofG258tm7tMr5GFiWv5ECBHKGNaJeFcLPaCUvTNadoUx5nsAK/Dzgz9WvQpLHrYpIyAIQR2uKJNW/ZLlFMPmeoiFrrkHAq7io6laslE8dchB1ARGT+3r0+Y6pgQi9/OvOa1ErmYo6rwLWjKQL4hYCy/9IuOrLdO8DxJvlPtxNLQ4v0XKhFjb6b/mNQZErWCck99mVkZ4sSQ0Yj20XBVY/geUyzjDS7fqsiPIjC/pxeXjgP3MdjmLLnyV0tJdORIIMkaNVaXLdvE1HMQ63CA7M/z1LwU7kMQjdoeIveALdCZ14OvvHHrvzmhVbaNt5UHMtjx9S2e0a6qmDtaNv8dhcG8oqNQHI8Hk7dan4VypKDegK01yt2Vmlc9Uf1Xy/Wp/+FLAzke+kE2WK7LguJSXOUN2cNUKux6EK3QhGF/Tv7bx36Y4bwg7aQr9ebZzCFbRI+mmQnh6zkODNJ4WwPfOiZkiIhv3PQZBEOjydT1iDMig40F8Crz4O+Oy0bX+XwUN/h1iUFeqyBZoge9lMkJVv4Z0b1JO1Xl3gWG88MtR30fqNa/oepGAbEqvP0MwWJMJv5RJchxrYaj60IUmt6YGtQsUgr3mkOJlwYdQLh/7KH/EdsAA6du7Ynkua/jogsPm1GXoMPTXw7xI73O/ajl6CeKVeCVdn+UZp5P4YHW8lDtsSpeQfljPKxNgb4Y+7bt3VTnwpeUOHMqFPI9tA3h3XzzXAQU8mq37OB2ORc8cCMcWHEc4wCaj0TE0jVb30hvgeYc2auJxkCcQBKmqF3WcdZTU74cnqKmKW0COfSPyOVu+VCrc90h/CjHPYuc2xBAynV9c3/o/HjiILw4Oehyg9wjxrjBwownUffaAsEmvrNsyXSu0G91TF4vdM8LBvZGfkJI9YMIdK58m+lBC4CJX1Hyrt5ZVRq8QPztrZu/bSTJWwRDH6JJ9zqUom+yCjPEEDbb4DFm7ZujPrUdW5/yW9trdu9qtg/pDbVyjj8zqgnTxVzgxaWGkr6ji+Oq7XwZ3Ke4ZNAaWmPwwMxLbgvGuW4836A7VlDa0Gsru7ZY90Ud/TZ8pT2QZABbSCsMIsoDtr1ezVt19oRoS53gifXH4dK5RmoJZ46EfFWQXAaKwrSZY3p7F4W4hnHHp61aKNr6kBY4OLm0aiSt6YcEBM1jSxwKf1iDU0+MTzFxaWIRIRjGsFXmNGWAXw1kSyz8zBNWgSkDzcGQxybEx8nGtnfIDNgvqzoD7fX5gn1IGW6v7yzgHZVsXlByTkiVYDV2x1haZtXaCytUUrQIh97aZ4GAFAPZAn3yNsmjRmAXV3uY2T5dViLeffijNTScgnf44jFiwTIywJ/fMewGBpqrq96KZS83iODbiQaaYcYMaIpBcjQr/NhNvdidXY7Uy7xJgefG2LZeP65YEIHQJpPhcNcg0xxsQ MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: LV8PR11MB8463.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6ae39b73-2c1b-435d-3fdb-08dc21eb731e X-MS-Exchange-CrossTenant-originalarrivaltime: 30 Jan 2024 23:30:31.2037 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 2B0XqnAa+JoDjYxwN97FNJhhq5F8VuTonx5bYDp64RnQMBOQJgicrCYvAV/OkVHPcGJKyUFQQyCnfLDbTOHjQdxh5ThwM5O0R8+YDUOeRKE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR11MB4689 X-OriginatorOrg: intel.com X-Mailman-Approved-At: Tue, 20 Aug 2024 09:56:01 +1000 X-BeenThere: linux-aspeed@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux ASPEED SoC development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "'rgrs@protonmail.com'" , "'arnd@arndb.de'" , "'vadimp@mellanox.com'" , "'pombredanne@nexb.com'" , "Filary, Steven A" , "'jiri@nvidia.com'" , "'omar.eduardo.castro@linux.intel.com'" , "'p.zabel@pengutronix.de'" , "Castro, Omar Eduardo" , "Corona, Ernesto" , "'patrickw3@fb.com'" , "'oleksandrs@mellanox.com'" , "Chen, Luke" Errors-To: linux-aspeed-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Linux-aspeed" Driver adds support of Aspeed 2400-2600 series SOC JTAG controller. Driver implements the following jtag ops: - freq_get; - freq_set; - status_get; - status_set - xfer; - mode_set; - bitbang; - enable; - disable; It has been tested on Mellanox system with BMC equipped with Aspeed 2520 SoC for programming CPLD devices. It has also been tested on Intel system using Aspeed 25xx SoC for JTAG communication. Tested on Intel system using Aspeed 26xx SoC for JTAG communication. Signed-off-by: Oleksandr Shamray Signed-off-by: Jiri Pirko Signed-off-by: Ernesto Corona Signed-off-by: Omar Castro Acked-by: Arnd Bergmann Acked-by: Philippe Ombredanne Acked-by: Joel Stanley Cc: Joel Stanley Cc: Andrew Jeffery Cc: Philipp Zabel Cc: Steven Filary Cc: Vadim Pasternak Cc: Amithash Prasad Cc: Patrick Williams Cc: Luke Chen Cc: Billy Tsai Cc: Rgrs --- v29->v30 Comments pointed by Steven Filary - Add Suport for 26xx series Software mode Hardware mode 1 (disabled by default) Hardware mode 2 (enabled by default) up to 512 bit lenght transfers - clang jtag-aspeed.c - Add pre and post padding support for JTAG transfers. - For 26xx series add TRST_N pin transition from low to high during TLR jtag state transition in HW and SW modes. Comments pointed by Moritz Fischer - Moved tms_cycle_loopup to jtag.hardware Comments pointed by Paul Fertser - Removed scu_base from aspeed_jtag structure. - Fixed Frequency calculation based on Aspeed 2600,2500 specs. - Removed tck as it is ignored. v28->v29 Comments pointed by Steven Filary - Expand bitbang function to accept multiples bitbang operations within a single JTAG_IOCBITBANG call. It will receive a buffer with TDI and TMS values and it is expected that driver fills TDO fields with its corresponding output value for every transaction. - Always setup JTAG controller to host mode but disable JTAG output when the driver is not in use to allow other HW to own the JTAG bus. Remove SCU register accesses. This register controls the JTAG controller mode (host controller/target). - Encansulate dev_dgb message into DEBUG_JTAG macros to improve driver's JTAG performace. - Add support for multichain. Set tap state and xfer operations now include two tap state arguments: current state and end state. v27->v28 Comments pointed by Steven Filary - Replace JTAG_IOCRUNTEST with JTAG_SIOCSTATE adding support for all TAPC end states in SW mode using a lookup table to navigate across states. - Add support for simultaneous READ/WRITE transfers (JTAG_READ_WRITE_XFER). - Support for switching JTAG controller mode between target and controller mode. - Setup JTAG controller mode to host only when the driver is opened, letting other HW to own the JTAG bus when it isn't in use. - Include JTAG bit bang IOCTL for low level JTAG control usage (JTAG_IOCBITBANG). - Add debug traces. - Add support for register polling (default) due it is 3 times faster than interrupt mode. Define USE_INTERRUPTS macro to enable interrupt usage. - Remove unnecessary delays for aspeed_jtag_status_set function. It makes SW mode 4 times faster. - Clean data buffer on aspeed_jtag_xfer_sw before tdo writes to avoid data output corruption for read operations in SW mode. - Correct register settings for HW mode transfer operations. - Propagate ret codes all the way from low level functions up to JTAG_IOCXFER call. - Support for partitioned transfers. Single JTAG transfer through multiples JTAG_IOCXFER calls. Now end transmission(scan_end) also evaluates transfer end state. v26->v27 Changes made by Oleksandr Shamray - change aspeed_jtag_sw_delay to udelay function in bit-bang operation v25->v26 v24->v25 Comments pointed by Greg KH - reduced debug printouts v23->v24 v22->v23 v21->v22 Comments pointed by Andy Shevchenko - rearrange ASPEED register defines - simplified JTAG divider calculation formula - change delay function in bit-bang operation - add helper functions for TAP states switching - remove unnecessary comments - remove redundant debug messages - make dines for repetative register bit sets - fixed indentation - change checks from negative to positive - add error check for clk_prepare_enable - rework driver alloc/register to devm_ variant - Increasing line length up to 85 in order to improve readability v20->v21 v19->v20 Notifications from kbuild test robot - add static declaration to 'aspeed_jtag_init' and 'aspeed_jtag_deinit' functions v18->v19 v17->v18 v16->v17 v15->v16 Comments pointed by Joel Stanley - Add reset_control on Jtag init/deinit v14->v15 Comments pointed by Joel Stanley - Add ARCH_ASPEED || COMPILE_TEST to Kconfig - remove unused offset variable - remove "aspeed_jtag" from dev_err and dev_dbg messages - change clk_prepare_enable initialisation order v13->v14 Comments pointed by Philippe Ombredanne - Change style of head block comment from /**/ to // v12->v13 Comments pointed by Philippe Ombredanne - Change jtag-aspeed.c licence type to SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note and reorder line with license- add reset descriptions in bndings file in description Comments pointed by Kun Yi - Changed capability check for aspeed,ast2400-jtag/ast200-jtag v11->v12 Comments pointed by Chip Bilbrey - Remove access mode from xfer and idle transactions - Add new ioctl JTAG_SIOCMODE for set hw mode v10->v11 v9->v10 V8->v9 Comments pointed by Arnd Bergmann - add *data parameter to xfer function prototype v7->v8 Comments pointed by Joel Stanley - aspeed_jtag_init replace goto to return; - change input variables type from __u32 to u32 in functios freq_get, freq_set, status_get - change sm_ variables type from char to u8 - in jatg_init add disable clocks on error case - remove release_mem_region on error case - remove devm_free_irq on jtag_deinit - Fix misspelling Disabe/Disable - Change compatible string to ast2400 and ast2000 v6->v7 Notifications from kbuild test robot - Add include to jtag-asapeed.c v5->v6 v4->v5 Comments pointed by Arnd Bergmann - Added HAS_IOMEM dependence in Kconfig to avoid "undefined reference to `devm_ioremap_resource'" error, because in some arch this not supported v3->v4 Comments pointed by Arnd Bergmann - change transaction pointer tdio type to __u64 - change internal status type from enum to __u32 v2->v3 v1->v2 Comments pointed by Greg KH - change license type from GPLv2/BSD to GPLv2 Comments pointed by Neil Armstrong - Add clk_prepare_enable/clk_disable_unprepare in clock init/deinit - Change .compatible to soc-specific compatible names aspeed,aspeed4000-jtag/aspeed5000-jtag - Added dt-bindings Comments pointed by Arnd Bergmann - Reorder functions and removed the forward declarations - Add static const qualifier to state machine states transitions - Change .compatible to soc-specific compatible names aspeed,aspeed4000-jtag/aspeed5000-jtag - Add dt-bindings Comments pointed by Randy Dunlap - Change module name jtag-aspeed in description in Kconfig Comments pointed by kbuild test robot - Remove invalid include - add resource_size instead of calculation --- drivers/jtag/Kconfig | 29 + drivers/jtag/Makefile | 1 + drivers/jtag/jtag-aspeed.c | 1666 ++++++++++++++++++++++++++++++++++++ 3 files changed, 1696 insertions(+) create mode 100644 drivers/jtag/jtag-aspeed.c diff --git a/drivers/jtag/Kconfig b/drivers/jtag/Kconfig index 47771fcd3c5b..d66b60448da0 100644 --- a/drivers/jtag/Kconfig +++ b/drivers/jtag/Kconfig @@ -15,3 +15,32 @@ menuconfig JTAG To compile this driver as a module, choose M here: the module will be called jtag. + +menuconfig JTAG_ASPEED + tristate "Aspeed SoC JTAG controller support" + depends on JTAG && HAS_IOMEM + depends on ARCH_ASPEED || COMPILE_TEST + help + This provides support for Aspeed JTAG devices equipped on Aspeed + SoC 24xx, 25xx and 26xx families. Driver allows programming of + hardware devices, connected to SoC through the JTAG interface. + + If you want this support, you should say Y here. + + To compile this driver as a module, choose M here: the module will + be called jtag-aspeed. + +config USE_INTERRUPTS + bool "Use interrupts as event wait mechanism" + depends on JTAG_ASPEED + default n + help + Aspeed SoC 24xx, 25xx and 26xx driver monitors the JTAG shift + operation completion by either polling or waiting for an interrupt. + + This flag is used by Aspeed driver to select from either interrupt or + polling as wait mechanism for JTAG controller completion events. + + If you want interrupt support, you should say Y here. + + If you want polling support, you should say N here. diff --git a/drivers/jtag/Makefile b/drivers/jtag/Makefile index af374939a9e6..04a855e2df28 100644 --- a/drivers/jtag/Makefile +++ b/drivers/jtag/Makefile @@ -1 +1,2 @@ obj-$(CONFIG_JTAG) += jtag.o +obj-$(CONFIG_JTAG_ASPEED) += jtag-aspeed.o diff --git a/drivers/jtag/jtag-aspeed.c b/drivers/jtag/jtag-aspeed.c new file mode 100644 index 000000000000..f2e8c20beab4 --- /dev/null +++ b/drivers/jtag/jtag-aspeed.c @@ -0,0 +1,1666 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2018 Mellanox Technologies. All rights reserved. +// Copyright (c) 2018 Oleksandr Shamray +// Copyright (c) 2019 Intel Corporation + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ASPEED_JTAG_DATA 0x00 +#define ASPEED_JTAG_INST 0x04 +#define ASPEED_JTAG_CTRL 0x08 +#define ASPEED_JTAG_ISR 0x0C +#define ASPEED_JTAG_SW 0x10 +#define ASPEED_JTAG_TCK 0x14 +#define ASPEED_JTAG_EC 0x18 + +#define ASPEED_JTAG_DATA_MSB 0x01 +#define ASPEED_JTAG_DATA_CHUNK_SIZE 0x20 +#define ASPEED_JTAG_HW2_DATA_CHUNK_SIZE 512 + +/* ASPEED_JTAG_CTRL: Engine Control 24xx and 25xx series*/ +#define ASPEED_JTAG_CTL_ENG_EN BIT(31) +#define ASPEED_JTAG_CTL_ENG_OUT_EN BIT(30) +#define ASPEED_JTAG_CTL_FORCE_TMS BIT(29) +#define ASPEED_JTAG_CTL_IR_UPDATE BIT(26) +#define ASPEED_JTAG_CTL_INST_LEN(x) ((x) << 20) +#define ASPEED_JTAG_CTL_LASPEED_INST BIT(17) +#define ASPEED_JTAG_CTL_INST_EN BIT(16) +#define ASPEED_JTAG_CTL_DR_UPDATE BIT(10) +#define ASPEED_JTAG_CTL_DATA_LEN(x) ((x) << 4) +#define ASPEED_JTAG_CTL_LASPEED_DATA BIT(1) +#define ASPEED_JTAG_CTL_DATA_EN BIT(0) + +/* ASPEED_JTAG_CTRL: Engine Control 26xx series*/ +#define ASPEED_JTAG_CTL_26XX_RESET_FIFO BIT(21) +#define ASPEED_JTAG_CTL_26XX_FIFO_MODE_CTRL BIT(20) +#define ASPEED_JTAG_CTL_26XX_TRANS_LEN(x) ((x) << 8) +#define ASPEED_JTAG_CTL_26XX_TRANS_MASK GENMASK(17, 8) +#define ASPEED_JTAG_CTL_26XX_MSB_FIRST BIT(6) +#define ASPEED_JTAG_CTL_26XX_TERM_TRANS BIT(5) +#define ASPEED_JTAG_CTL_26XX_LASPEED_TRANS BIT(4) +#define ASPEED_JTAG_CTL_26XX_INST_EN BIT(1) + +/* ASPEED_JTAG_ISR : Interrupt status and enable */ +#define ASPEED_JTAG_ISR_INST_PAUSE BIT(19) +#define ASPEED_JTAG_ISR_INST_COMPLETE BIT(18) +#define ASPEED_JTAG_ISR_DATA_PAUSE BIT(17) +#define ASPEED_JTAG_ISR_DATA_COMPLETE BIT(16) +#define ASPEED_JTAG_ISR_INST_PAUSE_EN BIT(3) +#define ASPEED_JTAG_ISR_INST_COMPLETE_EN BIT(2) +#define ASPEED_JTAG_ISR_DATA_PAUSE_EN BIT(1) +#define ASPEED_JTAG_ISR_DATA_COMPLETE_EN BIT(0) +#define ASPEED_JTAG_ISR_INT_EN_MASK GENMASK(3, 0) +#define ASPEED_JTAG_ISR_INT_MASK GENMASK(19, 16) + +/* ASPEED_JTAG_SW : Software Mode and Status */ +#define ASPEED_JTAG_SW_MODE_EN BIT(19) +#define ASPEED_JTAG_SW_MODE_TCK BIT(18) +#define ASPEED_JTAG_SW_MODE_TMS BIT(17) +#define ASPEED_JTAG_SW_MODE_TDIO BIT(16) + +/* ASPEED_JTAG_TCK : TCK Control */ +#define ASPEED_JTAG_TCK_DIVISOR_MASK GENMASK(10, 0) +#define ASPEED_JTAG_TCK_GET_DIV(x) ((x) & ASPEED_JTAG_TCK_DIVISOR_MASK) + +/* ASPEED_JTAG_EC : Controller set for go to IDLE */ +#define ASPEED_JTAG_EC_TRST BIT(31) +#define ASPEED_JTAG_EC_GO_IDLE BIT(0) + +#define ASPEED_JTAG_IOUT_LEN(len) \ + (ASPEED_JTAG_CTL_ENG_EN | \ + ASPEED_JTAG_CTL_ENG_OUT_EN | \ + ASPEED_JTAG_CTL_INST_LEN(len)) + +#define ASPEED_JTAG_DOUT_LEN(len) \ + (ASPEED_JTAG_CTL_ENG_EN | \ + ASPEED_JTAG_CTL_ENG_OUT_EN | \ + ASPEED_JTAG_CTL_DATA_LEN(len)) + +#define ASPEED_JTAG_TRANS_LEN(len) \ + (ASPEED_JTAG_CTL_ENG_EN | \ + ASPEED_JTAG_CTL_ENG_OUT_EN | \ + ASPEED_JTAG_CTL_26XX_TRANS_LEN(len)) + +#define ASPEED_JTAG_SW_TDIO (ASPEED_JTAG_SW_MODE_EN | ASPEED_JTAG_SW_MODE_TDIO) + +#define ASPEED_JTAG_GET_TDI(direction, byte) \ + (((direction) & JTAG_WRITE_XFER) ? byte : UINT_MAX) + +#define ASPEED_JTAG_TCK_WAIT 10 +#define ASPEED_JTAG_RESET_CNTR 10 +#define WAIT_ITERATIONS 300 + +/* Use this macro to switch between HW mode 1(comment out) and 2(defined) */ +#define ASPEED_JTAG_HW_MODE_2_ENABLE 1 + +/* ASPEED JTAG HW MODE 2 (Only supported in AST26xx series) */ +#define ASPEED_JTAG_SHDATA 0x20 +#define ASPEED_JTAG_SHINST 0x24 +#define ASPEED_JTAG_PADCTRL0 0x28 +#define ASPEED_JTAG_PADCTRL1 0x2C +#define ASPEED_JTAG_SHCTRL 0x30 +#define ASPEED_JTAG_GBLCTRL 0x34 +#define ASPEED_JTAG_INTCTRL 0x38 +#define ASPEED_JTAG_STAT 0x3C + +/* ASPEED_JTAG_PADCTRLx : Padding control 0 and 1 */ +#define ASPEED_JTAG_PADCTRL_PAD_DATA BIT(24) +#define ASPEED_JTAG_PADCTRL_POSTPAD(x) (((x) & GENMASK(8, 0)) << 12) +#define ASPEED_JTAG_PADCTRL_PREPAD(x) (((x) & GENMASK(8, 0)) << 0) + +/* ASPEED_JTAG_SHCTRL: Shift Control */ +#define ASPEED_JTAG_SHCTRL_FRUN_TCK_EN BIT(31) +#define ASPEED_JTAG_SHCTRL_STSHIFT_EN BIT(30) +#define ASPEED_JTAG_SHCTRL_TMS(x) (((x) & GENMASK(13, 0)) << 16) +#define ASPEED_JTAG_SHCTRL_POST_TMS(x) (((x) & GENMASK(2, 0)) << 13) +#define ASPEED_JTAG_SHCTRL_PRE_TMS(x) (((x) & GENMASK(2, 0)) << 10) +#define ASPEED_JTAG_SHCTRL_PAD_SEL0 (0) +#define ASPEED_JTAG_SHCTRL_PAD_SEL1 BIT(9) +#define ASPEED_JTAG_SHCTRL_END_SHIFT BIT(8) +#define ASPEED_JTAG_SHCTRL_START_SHIFT BIT(7) +#define ASPEED_JTAG_SHCTRL_LWRDT_SHIFT(x) ((x) & GENMASK(6, 0)) + +#define ASPEED_JTAG_END_SHIFT_DISABLED 0 + +/* ASPEED_JTAG_GBLCTRL : Global Control */ +#define ASPEED_JTAG_GBLCTRL_ENG_MODE_EN BIT(31) +#define ASPEED_JTAG_GBLCTRL_ENG_OUT_EN BIT(30) +#define ASPEED_JTAG_GBLCTRL_FORCE_TMS BIT(29) +#define ASPEED_JTAG_GBLCTRL_SHIFT_COMPLETE BIT(28) +#define ASPEED_JTAG_GBLCTRL_RESET_FIFO BIT(25) +#define ASPEED_JTAG_GBLCTRL_FIFO_CTRL_MODE BIT(24) +#define ASPEED_JTAG_GBLCTRL_UPDT_SHIFT(x) (((x) & GENMASK(9, 7)) << 13) +#define ASPEED_JTAG_GBLCTRL_STSHIFT(x) (((x) & GENMASK(0, 0)) << 16) +#define ASPEED_JTAG_GBLCTRL_TRST BIT(15) +#define ASPEED_JTAG_CLK_DIVISOR_MASK GENMASK(11, 0) +#define ASPEED_JTAG_CLK_GET_DIV(x) ((x) & ASPEED_JTAG_CLK_DIVISOR_MASK) + +/* ASPEED_JTAG_INTCTRL: Interrupt Control */ +#define ASPEED_JTAG_INTCTRL_SHCPL_IRQ_EN BIT(16) +#define ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT BIT(0) + +/* ASPEED_JTAG_STAT: JTAG HW mode 2 status */ +#define ASPEED_JTAG_STAT_ENG_IDLE BIT(0) + +#define ASPEED_JTAG_MAX_PAD_SIZE 512 + +/* Use this macro to set us delay to WA the intensive R/W FIFO usage issue */ +#define AST26XX_FIFO_UDELAY 2 + +/* Use this macro to set us delay for JTAG Controller to be programmed */ +#define AST26XX_JTAG_CTRL_UDELAY 2 + +/*#define CONFIG_USE_INTERRUPTS*/ +#define DEBUG_JTAG + +static const char * const regnames[] = { + [ASPEED_JTAG_DATA] = "ASPEED_JTAG_DATA", + [ASPEED_JTAG_INST] = "ASPEED_JTAG_INST", + [ASPEED_JTAG_CTRL] = "ASPEED_JTAG_CTRL", + [ASPEED_JTAG_ISR] = "ASPEED_JTAG_ISR", + [ASPEED_JTAG_SW] = "ASPEED_JTAG_SW", + [ASPEED_JTAG_TCK] = "ASPEED_JTAG_TCK", + [ASPEED_JTAG_EC] = "ASPEED_JTAG_EC", + [ASPEED_JTAG_SHDATA] = "ASPEED_JTAG_SHDATA", + [ASPEED_JTAG_SHINST] = "ASPEED_JTAG_SHINST", + [ASPEED_JTAG_PADCTRL0] = "ASPEED_JTAG_PADCTRL0", + [ASPEED_JTAG_PADCTRL1] = "ASPEED_JTAG_PADCTRL1", + [ASPEED_JTAG_SHCTRL] = "ASPEED_JTAG_SHCTRL", + [ASPEED_JTAG_GBLCTRL] = "ASPEED_JTAG_GBLCTRL", + [ASPEED_JTAG_INTCTRL] = "ASPEED_JTAG_INTCTRL", + [ASPEED_JTAG_STAT] = "ASPEED_JTAG_STAT", +}; + +#define ASPEED_JTAG_NAME "jtag-aspeed" + +struct aspeed_jtag { + void __iomem *reg_base; + struct device *dev; + struct clk *pclk; + enum jtag_tapstate status; + int irq; + struct reset_control *rst; + u32 flag; + wait_queue_head_t jtag_wq; + u32 mode; + enum jtag_tapstate current_state; + u32 tck_period; + u32 tck_cycle_delay_count; + const struct jtag_low_level_functions *llops; + u32 pad_data_one[ASPEED_JTAG_MAX_PAD_SIZE / 32]; + u32 pad_data_zero[ASPEED_JTAG_MAX_PAD_SIZE / 32]; +}; + +/* + * Multi generation support is enabled by fops and low level assped function + * mapping using asped_jtag_functions struct as config mechanism. + */ + +struct jtag_low_level_functions { + void (*output_disable)(struct aspeed_jtag *aspeed_jtag); + void (*controller_enable)(struct aspeed_jtag *aspeed_jtag); + int (*xfer_push_data)(struct aspeed_jtag *aspeed_jtag, + enum jtag_xfer_type type, u32 bits_len); + int (*xfer_push_data_last)(struct aspeed_jtag *aspeed_jtag, + enum jtag_xfer_type type, u32 bits_len); + void (*xfer_sw)(struct aspeed_jtag *aspeed_jtag, struct jtag_xfer *xfer, + u32 *data); + int (*xfer_hw)(struct aspeed_jtag *aspeed_jtag, struct jtag_xfer *xfer, + u32 *data); + void (*xfer_hw_fifo_delay)(void); + void (*xfer_sw_delay)(struct aspeed_jtag *aspeed_jtag); + void (*xfer_tck_cycle_delay)(struct aspeed_jtag *aspeed_jtag); + irqreturn_t (*jtag_interrupt)(s32 this_irq, void *dev_id); +}; + +struct aspeed_jtag_functions { + const struct jtag_ops *aspeed_jtag_ops; + const struct jtag_low_level_functions *aspeed_jtag_llops; +}; + +#ifdef DEBUG_JTAG +static char *end_status_str[] = { "tlr", "idle", "selDR", "capDR", + "sDR", "ex1DR", "pDR", "ex2DR", + "updDR", "selIR", "capIR", "sIR", + "ex1IR", "pIR", "ex2IR", "updIR", + "current"}; +#endif + +static u32 aspeed_jtag_read(struct aspeed_jtag *aspeed_jtag, u32 reg) +{ + u32 val = readl(aspeed_jtag->reg_base + reg); + +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, "read:%s val = 0x%08x\n", regnames[reg], val); +#endif + return val; +} + +static void aspeed_jtag_write(struct aspeed_jtag *aspeed_jtag, u32 val, u32 reg) +{ +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, "write:%s val = 0x%08x\n", regnames[reg], + val); +#endif + writel(val, aspeed_jtag->reg_base + reg); +} + +static int aspeed_jtag_freq_set(struct jtag *jtag, u32 freq) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + unsigned long apb_frq; + u32 tck_val; + u16 div; + + if (!freq) + return -EINVAL; + + apb_frq = clk_get_rate(aspeed_jtag->pclk); + if (!apb_frq) + return -EOPNOTSUPP; + + div = (apb_frq - 1) / freq; + tck_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_TCK); + aspeed_jtag_write(aspeed_jtag, + (tck_val & ~ASPEED_JTAG_TCK_DIVISOR_MASK) | div, + ASPEED_JTAG_TCK); + aspeed_jtag->tck_period = + DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * (div + 1), apb_frq); + return 0; +} + +static int aspeed_jtag_freq_set_26xx(struct jtag *jtag, u32 freq) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + unsigned long apb_frq; + u32 tck_val; + u16 div; + + if (!freq) + return -EINVAL; + + apb_frq = clk_get_rate(aspeed_jtag->pclk); + if (!apb_frq) + return -EOPNOTSUPP; + + div = (apb_frq - 1) / freq; + tck_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL); + aspeed_jtag_write(aspeed_jtag, + (tck_val & ~ASPEED_JTAG_CLK_DIVISOR_MASK) | div, + ASPEED_JTAG_GBLCTRL); + return 0; +} + +static int aspeed_jtag_freq_get(struct jtag *jtag, u32 *frq) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + u32 pclk; + u32 tck; + + pclk = clk_get_rate(aspeed_jtag->pclk); + tck = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_TCK); + *frq = pclk / (ASPEED_JTAG_TCK_GET_DIV(tck) + 1); + + return 0; +} + +static int aspeed_jtag_freq_get_26xx(struct jtag *jtag, u32 *frq) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + u32 pclk; + u32 tck; + + pclk = clk_get_rate(aspeed_jtag->pclk); + tck = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL); + *frq = pclk / (ASPEED_JTAG_CLK_GET_DIV(tck) + 1); + + return 0; +} + +static inline void aspeed_jtag_output_disable(struct aspeed_jtag *aspeed_jtag) +{ + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_CTRL); +} + +static inline void +aspeed_jtag_output_disable_26xx(struct aspeed_jtag *aspeed_jtag) +{ + u32 reg_val; + + reg_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL) & + ASPEED_JTAG_CLK_DIVISOR_MASK; + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_CTRL); + aspeed_jtag_write(aspeed_jtag, reg_val, ASPEED_JTAG_GBLCTRL); +} + +static inline void aspeed_jtag_controller(struct aspeed_jtag *aspeed_jtag) +{ + aspeed_jtag_write(aspeed_jtag, + (ASPEED_JTAG_CTL_ENG_EN | ASPEED_JTAG_CTL_ENG_OUT_EN), + ASPEED_JTAG_CTRL); + + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_SW_MODE_EN | ASPEED_JTAG_SW_MODE_TDIO, + ASPEED_JTAG_SW); + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_ISR_INST_PAUSE | + ASPEED_JTAG_ISR_INST_COMPLETE | + ASPEED_JTAG_ISR_DATA_PAUSE | + ASPEED_JTAG_ISR_DATA_COMPLETE | + ASPEED_JTAG_ISR_INST_PAUSE_EN | + ASPEED_JTAG_ISR_INST_COMPLETE_EN | + ASPEED_JTAG_ISR_DATA_PAUSE_EN | + ASPEED_JTAG_ISR_DATA_COMPLETE_EN, + ASPEED_JTAG_ISR); /* Enable Interrupt */ +} + +static inline void aspeed_jtag_controller_26xx(struct aspeed_jtag *aspeed_jtag) +{ + u32 reg_val; + + reg_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL) & + ASPEED_JTAG_CLK_DIVISOR_MASK; + if (aspeed_jtag->mode & JTAG_XFER_HW_MODE) { + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_CTRL); + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_SW); + } else { + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_SW_MODE_EN | + ASPEED_JTAG_SW_MODE_TDIO, + ASPEED_JTAG_SW); + } + /* + * For the software mode, it's still necessary to enable out_en and + * select the out_en in the hw2 register to maintain control of the + * TRST bit same as hw2. + */ + aspeed_jtag_write(aspeed_jtag, + reg_val | ASPEED_JTAG_GBLCTRL_ENG_MODE_EN | + ASPEED_JTAG_GBLCTRL_ENG_OUT_EN | + ASPEED_JTAG_GBLCTRL_TRST, + ASPEED_JTAG_GBLCTRL); + reg_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL); + dev_dbg(aspeed_jtag->dev, "ASPEED_JTAG_GBLCTRL:val = 0x%08x\n", reg_val); + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_INTCTRL_SHCPL_IRQ_EN | + ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT, + ASPEED_JTAG_INTCTRL); /* Enable HW2 IRQ */ + + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_ISR_INST_PAUSE | + ASPEED_JTAG_ISR_INST_COMPLETE | + ASPEED_JTAG_ISR_DATA_PAUSE | + ASPEED_JTAG_ISR_DATA_COMPLETE | + ASPEED_JTAG_ISR_INST_PAUSE_EN | + ASPEED_JTAG_ISR_INST_COMPLETE_EN | + ASPEED_JTAG_ISR_DATA_PAUSE_EN | + ASPEED_JTAG_ISR_DATA_COMPLETE_EN, + ASPEED_JTAG_ISR); /* Enable HW1 Interrupts */ +} + +static void aspeed_jtag_tck_cycle_delay(struct aspeed_jtag *aspeed_jtag) +{ + int i = 0; + + for (i = 0; i < aspeed_jtag->tck_cycle_delay_count; i++) + ndelay(aspeed_jtag->tck_period >> 1); +} + +static int aspeed_jtag_mode_set(struct jtag *jtag, struct jtag_mode *jtag_mode) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + + switch (jtag_mode->feature) { + case JTAG_XFER_MODE: + aspeed_jtag->mode = jtag_mode->mode; + aspeed_jtag->llops->controller_enable(aspeed_jtag); + break; + case JTAG_CONTROL_MODE: + if (jtag_mode->mode == JTAG_CONTROLLER_OUTPUT_DISABLE) + aspeed_jtag->llops->output_disable(aspeed_jtag); + else if (jtag_mode->mode == JTAG_CONTROLLER_MODE) + aspeed_jtag->llops->controller_enable(aspeed_jtag); + break; + case JTAG_TCK_CYCLE_DELAY_COUNT: + aspeed_jtag->tck_cycle_delay_count = jtag_mode->mode; + break; + default: + return -EINVAL; + } + return 0; +} + +/* + * We read and write from an unused JTAG controller register in SW mode to + * create a delay in xfers. + * We found this mechanism better than any udelay or usleep option. + */ +static inline void aspeed_jtag_sw_delay_26xx(struct aspeed_jtag *aspeed_jtag) +{ + u32 read_reg = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_PADCTRL1); + + aspeed_jtag_write(aspeed_jtag, read_reg, ASPEED_JTAG_PADCTRL1); +} + +static char aspeed_jtag_tck_cycle(struct aspeed_jtag *aspeed_jtag, u8 tms, + u8 tdi) +{ + char tdo = 0; + + /* TCK = 0 */ + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_SW_MODE_EN | + (tms * ASPEED_JTAG_SW_MODE_TMS) | + (tdi * ASPEED_JTAG_SW_MODE_TDIO), + ASPEED_JTAG_SW); + + /* Wait until JTAG controller finishes the operation */ + if (aspeed_jtag->llops->xfer_sw_delay) + aspeed_jtag->llops->xfer_sw_delay(aspeed_jtag); + else + aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_SW); + + if (aspeed_jtag->llops->xfer_tck_cycle_delay && + aspeed_jtag->tck_cycle_delay_count) + aspeed_jtag->llops->xfer_tck_cycle_delay(aspeed_jtag); + + /* TCK = 1 */ + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_SW_MODE_EN | ASPEED_JTAG_SW_MODE_TCK | + (tms * ASPEED_JTAG_SW_MODE_TMS) | + (tdi * ASPEED_JTAG_SW_MODE_TDIO), + ASPEED_JTAG_SW); + + /* Wait until JTAG controller finishes the operation */ + if (aspeed_jtag->llops->xfer_sw_delay) + aspeed_jtag->llops->xfer_sw_delay(aspeed_jtag); + + if (aspeed_jtag->llops->xfer_tck_cycle_delay && + aspeed_jtag->tck_cycle_delay_count) + aspeed_jtag->llops->xfer_tck_cycle_delay(aspeed_jtag); + + if (aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_SW) & + ASPEED_JTAG_SW_MODE_TDIO) + tdo = 1; + + return tdo; +} + +static int aspeed_jtag_bitbang(struct jtag *jtag, + struct bitbang_packet *bitbang, + struct tck_bitbang *bitbang_data) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + int i = 0; + + for (i = 0; i < bitbang->length; i++) { + bitbang_data[i].tdo = + aspeed_jtag_tck_cycle(aspeed_jtag, bitbang_data[i].tms, + bitbang_data[i].tdi); + } + return 0; +} + +static inline void aspeed_jtag_xfer_hw_fifo_delay_26xx(void) +{ + udelay(AST26XX_FIFO_UDELAY); +} + +static int aspeed_jtag_isr_wait(struct aspeed_jtag *aspeed_jtag, u32 bit) +{ + int res = 0; +#ifdef CONFIG_USE_INTERRUPTS + res = wait_event_interruptible(aspeed_jtag->jtag_wq, + aspeed_jtag->flag & bit); + aspeed_jtag->flag &= ~bit; +#else + u32 status = 0; + u32 iterations = 0; + + while ((status & bit) == 0) { + status = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_ISR); +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, "%s = 0x%08x\n", __func__, status); +#endif + iterations++; + if (iterations > WAIT_ITERATIONS) { + dev_err(aspeed_jtag->dev, "%s %d in ASPEED_JTAG_ISR\n", + "aspeed_jtag driver timed out waiting for bit", + bit); + res = -EFAULT; + break; + } + if ((status & ASPEED_JTAG_ISR_DATA_COMPLETE) == 0) { + if (iterations % 25 == 0) + usleep_range(1, 5); + else + udelay(1); + } + } + aspeed_jtag_write(aspeed_jtag, bit | (status & 0xf), ASPEED_JTAG_ISR); +#endif + return res; +} + +static int aspeed_jtag_wait_shift_complete(struct aspeed_jtag *aspeed_jtag) +{ + int res = 0; +#ifdef CONFIG_USE_INTERRUPTS + res = wait_event_interruptible(aspeed_jtag->jtag_wq, + aspeed_jtag->flag & + ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT); + aspeed_jtag->flag &= ~ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT; +#else + u32 status = 0; + u32 iterations = 0; + + while ((status & ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT) == 0) { + status = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_INTCTRL); +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, "%s = 0x%08x\n", __func__, status); +#endif + iterations++; + if (iterations > WAIT_ITERATIONS) { + dev_err(aspeed_jtag->dev, + "aspeed_jtag driver timed out waiting for shift completed\n"); + res = -EFAULT; + break; + } + if (iterations % 25 == 0) + usleep_range(1, 5); + else + udelay(1); + } + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT | + ASPEED_JTAG_INTCTRL_SHCPL_IRQ_EN, + ASPEED_JTAG_INTCTRL); +#endif + return res; +} + +static void aspeed_jtag_set_tap_state(struct aspeed_jtag *aspeed_jtag, + enum jtag_tapstate from_state, + enum jtag_tapstate end_state) +{ + int i = 0; + enum jtag_tapstate from, to; + + from = from_state; + to = end_state; + + if (from == JTAG_STATE_CURRENT) + from = aspeed_jtag->current_state; + + for (i = 0; i < _tms_cycle_lookup[from][to].count; i++) + aspeed_jtag_tck_cycle(aspeed_jtag, + ((_tms_cycle_lookup[from][to].tmsbits + >> i) & 0x1), 0); + aspeed_jtag->current_state = end_state; +} + +static void aspeed_jtag_set_tap_state_sw(struct aspeed_jtag *aspeed_jtag, + struct jtag_tap_state *tapstate) +{ + int i; + + /* SW mode from curent tap state -> to end_state */ + if (tapstate->reset || tapstate->endstate == JTAG_STATE_TLRESET) { + for (i = 0; i < ASPEED_JTAG_RESET_CNTR; i++) + aspeed_jtag_tck_cycle(aspeed_jtag, 1, 0); + aspeed_jtag->current_state = JTAG_STATE_TLRESET; + } + + aspeed_jtag_set_tap_state(aspeed_jtag, tapstate->from, + tapstate->endstate); + if (tapstate->endstate == JTAG_STATE_TLRESET || + tapstate->endstate == JTAG_STATE_IDLE || + tapstate->endstate == JTAG_STATE_PAUSEDR || + tapstate->endstate == JTAG_STATE_PAUSEIR) + for (i = 0; i < tapstate->tck; i++) + aspeed_jtag_tck_cycle(aspeed_jtag, 0, 0); +} + +static int aspeed_jtag_status_set(struct jtag *jtag, + struct jtag_tap_state *tapstate) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + int i; + +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, "Set TAP state: %s\n", + end_status_str[tapstate->endstate]); +#endif + + if (!(aspeed_jtag->mode & JTAG_XFER_HW_MODE)) { + aspeed_jtag_set_tap_state_sw(aspeed_jtag, tapstate); + return 0; + } + + /* x TMS high + 1 TMS low */ + if (tapstate->reset) { + /* Disable sw mode */ + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_SW); + mdelay(1); + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_CTL_ENG_EN | + ASPEED_JTAG_CTL_ENG_OUT_EN | + ASPEED_JTAG_CTL_FORCE_TMS, + ASPEED_JTAG_CTRL); + mdelay(1); + aspeed_jtag_write(aspeed_jtag, ASPEED_JTAG_SW_TDIO, + ASPEED_JTAG_SW); + aspeed_jtag->current_state = JTAG_STATE_TLRESET; + } + for (i = 0; i < tapstate->tck; i++) + ndelay(aspeed_jtag->tck_period); + + return 0; +} + +static int aspeed_jtag_shctrl_tms_mask(enum jtag_tapstate from, + enum jtag_tapstate to, + enum jtag_tapstate there, + enum jtag_tapstate endstate, + u32 start_shift, u32 end_shift, + u32 *tms_mask) +{ + u32 pre_tms = start_shift ? _tms_cycle_lookup[from][to].count : 0; + u32 post_tms = end_shift ? _tms_cycle_lookup[there][endstate].count : 0; + u32 tms_value = start_shift ? _tms_cycle_lookup[from][to].tmsbits : 0; + + tms_value |= end_shift ? _tms_cycle_lookup[there][endstate].tmsbits + << pre_tms : + 0; + if (pre_tms > GENMASK(2, 0) || post_tms > GENMASK(2, 0)) { + pr_err("pre/port tms count is greater than hw limit"); + return -EINVAL; + } + *tms_mask = start_shift | ASPEED_JTAG_SHCTRL_PRE_TMS(pre_tms) | + end_shift | ASPEED_JTAG_SHCTRL_POST_TMS(post_tms) | + ASPEED_JTAG_SHCTRL_TMS(tms_value); + return 0; +} + +static void aspeed_jtag_set_tap_state_hw2(struct aspeed_jtag *aspeed_jtag, + struct jtag_tap_state *tapstate) +{ + u32 reg_val; + + /* x TMS high + 1 TMS low */ + if (tapstate->reset || tapstate->endstate == JTAG_STATE_TLRESET) { + /* Disable sw mode */ + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_SW); + udelay(AST26XX_JTAG_CTRL_UDELAY); + reg_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL); + aspeed_jtag_write(aspeed_jtag, + reg_val | ASPEED_JTAG_GBLCTRL_ENG_MODE_EN | + ASPEED_JTAG_GBLCTRL_ENG_OUT_EN | + ASPEED_JTAG_GBLCTRL_RESET_FIFO | + ASPEED_JTAG_GBLCTRL_FORCE_TMS, + ASPEED_JTAG_GBLCTRL); + udelay(AST26XX_JTAG_CTRL_UDELAY); + while (aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL) & + ASPEED_JTAG_GBLCTRL_FORCE_TMS) + ; + aspeed_jtag->current_state = JTAG_STATE_TLRESET; + } else if (tapstate->endstate == JTAG_STATE_IDLE && + aspeed_jtag->current_state != JTAG_STATE_IDLE) { + /* Always go to RTI, do not wait for shift operation */ + aspeed_jtag_set_tap_state(aspeed_jtag, + aspeed_jtag->current_state, + JTAG_STATE_IDLE); + aspeed_jtag->current_state = JTAG_STATE_IDLE; + } + /* Run TCK */ + if (tapstate->tck) { + /* Disable sw mode */ + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_SW); + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_PADCTRL0); + reg_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL); + reg_val = reg_val & ~(GENMASK(22, 20)); + aspeed_jtag_write(aspeed_jtag, + reg_val | ASPEED_JTAG_GBLCTRL_FIFO_CTRL_MODE | + ASPEED_JTAG_GBLCTRL_STSHIFT(0) | + ASPEED_JTAG_GBLCTRL_UPDT_SHIFT(tapstate->tck), + ASPEED_JTAG_GBLCTRL); + + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_SHCTRL_STSHIFT_EN | + ASPEED_JTAG_SHCTRL_LWRDT_SHIFT(tapstate->tck), + ASPEED_JTAG_SHCTRL); + aspeed_jtag_wait_shift_complete(aspeed_jtag); + } +} + +static int aspeed_jtag_status_set_26xx(struct jtag *jtag, + struct jtag_tap_state *tapstate) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, "Set TAP state: status %s from %s to %s\n", + end_status_str[aspeed_jtag->current_state], + end_status_str[tapstate->from], + end_status_str[tapstate->endstate]); +#endif + + if (!(aspeed_jtag->mode & JTAG_XFER_HW_MODE)) { + aspeed_jtag_set_tap_state_sw(aspeed_jtag, tapstate); + return 0; + } + + aspeed_jtag_set_tap_state_hw2(aspeed_jtag, tapstate); + return 0; +} + +static void aspeed_jtag_xfer_sw(struct aspeed_jtag *aspeed_jtag, + struct jtag_xfer *xfer, u32 *data) +{ + unsigned long remain_xfer = xfer->length; + unsigned long shift_bits = 0; + unsigned long index = 0; + unsigned long tdi; + char tdo; + +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, "SW JTAG SHIFT %s, length = %d\n", + (xfer->type == JTAG_SIR_XFER) ? "IR" : "DR", xfer->length); +#endif + + if (xfer->type == JTAG_SIR_XFER) + aspeed_jtag_set_tap_state(aspeed_jtag, xfer->from, + JTAG_STATE_SHIFTIR); + else + aspeed_jtag_set_tap_state(aspeed_jtag, xfer->from, + JTAG_STATE_SHIFTDR); + + tdi = ASPEED_JTAG_GET_TDI(xfer->direction, data[index]); + data[index] = 0; + while (remain_xfer > 1) { + tdo = aspeed_jtag_tck_cycle(aspeed_jtag, 0, + tdi & ASPEED_JTAG_DATA_MSB); + data[index] |= tdo + << (shift_bits % ASPEED_JTAG_DATA_CHUNK_SIZE); + tdi >>= 1; + shift_bits++; + remain_xfer--; + + if (shift_bits % ASPEED_JTAG_DATA_CHUNK_SIZE == 0) { + tdo = 0; + index++; + tdi = ASPEED_JTAG_GET_TDI(xfer->direction, data[index]); + data[index] = 0; + } + } + + if ((xfer->endstate == (xfer->type == JTAG_SIR_XFER ? + JTAG_STATE_SHIFTIR : + JTAG_STATE_SHIFTDR))) { + /* Stay in Shift IR/DR*/ + tdo = aspeed_jtag_tck_cycle(aspeed_jtag, 0, + tdi & ASPEED_JTAG_DATA_MSB); + data[index] |= tdo + << (shift_bits % ASPEED_JTAG_DATA_CHUNK_SIZE); + } else { + /* Goto end state */ + tdo = aspeed_jtag_tck_cycle(aspeed_jtag, 1, + tdi & ASPEED_JTAG_DATA_MSB); + data[index] |= tdo + << (shift_bits % ASPEED_JTAG_DATA_CHUNK_SIZE); + aspeed_jtag->status = (xfer->type == JTAG_SIR_XFER) ? + JTAG_STATE_EXIT1IR : + JTAG_STATE_EXIT1DR; + aspeed_jtag_set_tap_state(aspeed_jtag, aspeed_jtag->status, + xfer->endstate); + } +} + +static int aspeed_jtag_xfer_push_data_26xx(struct aspeed_jtag *aspeed_jtag, + enum jtag_xfer_type type, + u32 bits_len) +{ + int res = 0; + + aspeed_jtag_write(aspeed_jtag, ASPEED_JTAG_TRANS_LEN(bits_len), + ASPEED_JTAG_CTRL); + if (type == JTAG_SIR_XFER) { + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_TRANS_LEN(bits_len) | + ASPEED_JTAG_CTL_26XX_INST_EN, + ASPEED_JTAG_CTRL); + res = aspeed_jtag_isr_wait(aspeed_jtag, + ASPEED_JTAG_ISR_INST_PAUSE); + } else { + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_TRANS_LEN(bits_len) | + ASPEED_JTAG_CTL_DATA_EN, + ASPEED_JTAG_CTRL); + res = aspeed_jtag_isr_wait(aspeed_jtag, + ASPEED_JTAG_ISR_DATA_PAUSE); + } + return res; +} + +static int aspeed_jtag_xfer_push_data(struct aspeed_jtag *aspeed_jtag, + enum jtag_xfer_type type, u32 bits_len) +{ + int res = 0; + + if (type == JTAG_SIR_XFER) { + aspeed_jtag_write(aspeed_jtag, ASPEED_JTAG_IOUT_LEN(bits_len), + ASPEED_JTAG_CTRL); + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_IOUT_LEN(bits_len) | + ASPEED_JTAG_CTL_INST_EN, + ASPEED_JTAG_CTRL); + res = aspeed_jtag_isr_wait(aspeed_jtag, + ASPEED_JTAG_ISR_INST_PAUSE); + } else { + aspeed_jtag_write(aspeed_jtag, ASPEED_JTAG_DOUT_LEN(bits_len), + ASPEED_JTAG_CTRL); + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_DOUT_LEN(bits_len) | + ASPEED_JTAG_CTL_DATA_EN, + ASPEED_JTAG_CTRL); + res = aspeed_jtag_isr_wait(aspeed_jtag, + ASPEED_JTAG_ISR_DATA_PAUSE); + } + return res; +} + +static int aspeed_jtag_xfer_push_data_last_26xx(struct aspeed_jtag *aspeed_jtag, + enum jtag_xfer_type type, + u32 shift_bits) +{ + int res = 0; + + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_TRANS_LEN(shift_bits) | + ASPEED_JTAG_CTL_26XX_LASPEED_TRANS, + ASPEED_JTAG_CTRL); + if (type == JTAG_SIR_XFER) { + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_TRANS_LEN(shift_bits) | + ASPEED_JTAG_CTL_26XX_LASPEED_TRANS | + ASPEED_JTAG_CTL_26XX_INST_EN, + ASPEED_JTAG_CTRL); + res = aspeed_jtag_isr_wait(aspeed_jtag, + ASPEED_JTAG_ISR_INST_COMPLETE); + } else { + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_TRANS_LEN(shift_bits) | + ASPEED_JTAG_CTL_26XX_LASPEED_TRANS | + ASPEED_JTAG_CTL_DATA_EN, + ASPEED_JTAG_CTRL); + res = aspeed_jtag_isr_wait(aspeed_jtag, + ASPEED_JTAG_ISR_DATA_COMPLETE); + } + return res; +} + +static int aspeed_jtag_xfer_push_data_last(struct aspeed_jtag *aspeed_jtag, + enum jtag_xfer_type type, + u32 shift_bits) +{ + int res = 0; + + if (type == JTAG_SIR_XFER) { + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_IOUT_LEN(shift_bits) | + ASPEED_JTAG_CTL_LASPEED_INST, + ASPEED_JTAG_CTRL); + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_IOUT_LEN(shift_bits) | + ASPEED_JTAG_CTL_LASPEED_INST | + ASPEED_JTAG_CTL_INST_EN, + ASPEED_JTAG_CTRL); + res = aspeed_jtag_isr_wait(aspeed_jtag, + ASPEED_JTAG_ISR_INST_COMPLETE); + } else { + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_DOUT_LEN(shift_bits) | + ASPEED_JTAG_CTL_LASPEED_DATA, + ASPEED_JTAG_CTRL); + aspeed_jtag_write(aspeed_jtag, + ASPEED_JTAG_DOUT_LEN(shift_bits) | + ASPEED_JTAG_CTL_LASPEED_DATA | + ASPEED_JTAG_CTL_DATA_EN, + ASPEED_JTAG_CTRL); + res = aspeed_jtag_isr_wait(aspeed_jtag, + ASPEED_JTAG_ISR_DATA_COMPLETE); + } + return res; +} + +static int aspeed_jtag_xfer_hw(struct aspeed_jtag *aspeed_jtag, + struct jtag_xfer *xfer, u32 *data) +{ + unsigned long remain_xfer = xfer->length; + unsigned long index = 0; + char shift_bits; + u32 data_reg; + u32 scan_end; + union pad_config padding; + int retval = 0; + + padding.int_value = xfer->padding; + +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, "HW JTAG SHIFT %s, length = %d pad = 0x%x\n", + (xfer->type == JTAG_SIR_XFER) ? "IR" : "DR", xfer->length, + xfer->padding); +#endif + data_reg = xfer->type == JTAG_SIR_XFER ? ASPEED_JTAG_INST : + ASPEED_JTAG_DATA; + if (xfer->endstate == JTAG_STATE_SHIFTIR || + xfer->endstate == JTAG_STATE_SHIFTDR || + xfer->endstate == JTAG_STATE_PAUSEIR || + xfer->endstate == JTAG_STATE_PAUSEDR) { + scan_end = 0; + } else { + if (padding.post_pad_number) + scan_end = 0; + else + scan_end = 1; + } + + /* Perform pre padding */ + if (padding.pre_pad_number) { + struct jtag_xfer pre_xfer = { + .type = xfer->type, + .direction = JTAG_WRITE_XFER, + .from = xfer->from, + .endstate = xfer->type == JTAG_SIR_XFER ? + JTAG_STATE_SHIFTIR : JTAG_STATE_SHIFTDR, + .padding = 0, + .length = padding.pre_pad_number, + }; + if (padding.pre_pad_number > ASPEED_JTAG_MAX_PAD_SIZE) + return -EINVAL; + retval = aspeed_jtag_xfer_hw(aspeed_jtag, &pre_xfer, + padding.pad_data ? + aspeed_jtag->pad_data_one : + aspeed_jtag->pad_data_zero); + if (retval) + return retval; + } + + while (remain_xfer) { + if (xfer->direction & JTAG_WRITE_XFER) + aspeed_jtag_write(aspeed_jtag, data[index], data_reg); + else + aspeed_jtag_write(aspeed_jtag, 0, data_reg); + if (aspeed_jtag->llops->xfer_hw_fifo_delay) + aspeed_jtag->llops->xfer_hw_fifo_delay(); + + if (remain_xfer > ASPEED_JTAG_DATA_CHUNK_SIZE) { +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, + "Chunk len=%d chunk_size=%d remain_xfer=%lu\n", + xfer->length, ASPEED_JTAG_DATA_CHUNK_SIZE, + remain_xfer); +#endif + shift_bits = ASPEED_JTAG_DATA_CHUNK_SIZE; + + /* + * Transmit bytes that were not equals to column length + * and after the transfer go to Pause IR/DR. + */ + if (aspeed_jtag->llops->xfer_push_data(aspeed_jtag, + xfer->type, + shift_bits) + != 0) { + return -EFAULT; + } + } else { + /* + * Read bytes equals to column length + */ + shift_bits = remain_xfer; + if (scan_end) { + /* + * If this data is the end of the transmission + * send remaining bits and go to endstate + */ +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, + "Last len=%d chunk_size=%d remain_xfer=%lu\n", + xfer->length, + ASPEED_JTAG_DATA_CHUNK_SIZE, + remain_xfer); +#endif + if (aspeed_jtag->llops->xfer_push_data_last( + aspeed_jtag, xfer->type, + shift_bits) != 0) { + return -EFAULT; + } + } else { + /* + * If transmission is waiting for additional + * data send remaining bits and then go to + * Pause IR/DR. + */ +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, + "Tail len=%d chunk_size=%d remain_xfer=%lu\n", + xfer->length, + ASPEED_JTAG_DATA_CHUNK_SIZE, + remain_xfer); +#endif + if (aspeed_jtag->llops->xfer_push_data( + aspeed_jtag, xfer->type, + shift_bits) != 0) { + return -EFAULT; + } + } + } + + if (xfer->direction & JTAG_READ_XFER) { + if (shift_bits < ASPEED_JTAG_DATA_CHUNK_SIZE) { + data[index] = + aspeed_jtag_read(aspeed_jtag, data_reg); + + data[index] >>= ASPEED_JTAG_DATA_CHUNK_SIZE - + shift_bits; + } else { + data[index] = + aspeed_jtag_read(aspeed_jtag, data_reg); + } + if (aspeed_jtag->llops->xfer_hw_fifo_delay) + aspeed_jtag->llops->xfer_hw_fifo_delay(); + } + + remain_xfer = remain_xfer - shift_bits; + index++; + } + + /* Perform post padding */ + if (padding.post_pad_number) { + struct jtag_xfer post_xfer = { + .type = xfer->type, + .direction = JTAG_WRITE_XFER, + .from = xfer->from, + .endstate = xfer->endstate, + .padding = 0, + .length = padding.post_pad_number, + }; + if (padding.post_pad_number > ASPEED_JTAG_MAX_PAD_SIZE) + return -EINVAL; + retval = aspeed_jtag_xfer_hw(aspeed_jtag, &post_xfer, + padding.pad_data ? + aspeed_jtag->pad_data_one : + aspeed_jtag->pad_data_zero); + if (retval) + return retval; + } + return 0; +} + +static int aspeed_jtag_xfer(struct jtag *jtag, struct jtag_xfer *xfer, + u8 *xfer_data) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + + if (!(aspeed_jtag->mode & JTAG_XFER_HW_MODE)) { + /* SW mode */ + aspeed_jtag_write(aspeed_jtag, ASPEED_JTAG_SW_TDIO, + ASPEED_JTAG_SW); + + aspeed_jtag->llops->xfer_sw(aspeed_jtag, xfer, + (u32 *)xfer_data); + } else { + /* HW mode */ + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_SW); + if (aspeed_jtag->llops->xfer_hw(aspeed_jtag, xfer, + (u32 *)xfer_data) != 0) + return -EFAULT; + } + + aspeed_jtag->status = xfer->endstate; + return 0; +} + +static int aspeed_jtag_xfer_hw2(struct aspeed_jtag *aspeed_jtag, + struct jtag_xfer *xfer, u32 *data) +{ + unsigned long remain_xfer = xfer->length; + unsigned long partial_xfer_size = 0; + unsigned long index = 0; + u32 shift_bits; + u32 data_reg; + u32 reg_val; + enum jtag_tapstate shift; + enum jtag_tapstate exit; + enum jtag_tapstate exitx; + enum jtag_tapstate pause; + enum jtag_tapstate endstate; + u32 start_shift; + u32 end_shift; + u32 tms_mask; + int ret; + + if (xfer->type == JTAG_SIR_XFER) { + data_reg = ASPEED_JTAG_SHDATA; + shift = JTAG_STATE_SHIFTIR; + pause = JTAG_STATE_PAUSEIR; + exit = JTAG_STATE_EXIT1IR; + exitx = JTAG_STATE_EXIT1DR; + } else { + data_reg = ASPEED_JTAG_SHDATA; + shift = JTAG_STATE_SHIFTDR; + pause = JTAG_STATE_PAUSEDR; + exit = JTAG_STATE_EXIT1DR; + exitx = JTAG_STATE_EXIT1IR; + } +#ifdef DEBUG_JTAG + dev_dbg(aspeed_jtag->dev, + "HW2 JTAG SHIFT %s, length %d status %s from %s to %s then %s pad 0x%x\n", + (xfer->type == JTAG_SIR_XFER) ? "IR" : "DR", xfer->length, + end_status_str[aspeed_jtag->current_state], + end_status_str[xfer->from], + end_status_str[shift], + end_status_str[xfer->endstate], xfer->padding); +#endif + + if (aspeed_jtag->current_state == shift) + start_shift = 0; + else + start_shift = ASPEED_JTAG_SHCTRL_START_SHIFT; + + if (xfer->endstate == shift) { + /* + * In the case of shifting 1 bit of data and attempting to stay + * in the SHIFT state, the AST2600 JTAG Controller in Hardware + * mode 2 has been observed to go to EXIT1 IR/DR instead of + * staying in the SHIFT IR/DR state. The following code special + * cases this one bit shift and directs the state machine to go + * to the PAUSE IR/DR state instead. + * Alternatively, the application making driver calls can avoid + * this situation as follows: + * 1.) Bundle all of the shift bits together into one call + * AND/OR + * 2.) Direct all partial shifts to move to the PAUSE-IR/DR + * state. + */ + if (xfer->length == 1) { +#ifdef DEBUG_JTAG + dev_warn(aspeed_jtag->dev, "JTAG Silicon WA: going to pause instead of shift"); +#endif + end_shift = ASPEED_JTAG_SHCTRL_END_SHIFT; + endstate = pause; + } else { + end_shift = 0; + endstate = shift; + } + } else { + endstate = xfer->endstate; + end_shift = ASPEED_JTAG_SHCTRL_END_SHIFT; + } + + aspeed_jtag_write(aspeed_jtag, xfer->padding, ASPEED_JTAG_PADCTRL0); + + while (remain_xfer) { + unsigned long partial_xfer; + unsigned long partial_index; + + if (remain_xfer > ASPEED_JTAG_HW2_DATA_CHUNK_SIZE) + partial_xfer_size = ASPEED_JTAG_HW2_DATA_CHUNK_SIZE; + else + partial_xfer_size = remain_xfer; + + partial_index = index; + partial_xfer = partial_xfer_size; + + reg_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL); + aspeed_jtag_write(aspeed_jtag, reg_val | + ASPEED_JTAG_GBLCTRL_RESET_FIFO, + ASPEED_JTAG_GBLCTRL); + + /* Switch internal FIFO into CPU mode */ + reg_val = reg_val & ~BIT(24); + aspeed_jtag_write(aspeed_jtag, reg_val, + ASPEED_JTAG_GBLCTRL); + + while (partial_xfer) { + if (partial_xfer > ASPEED_JTAG_DATA_CHUNK_SIZE) + shift_bits = ASPEED_JTAG_DATA_CHUNK_SIZE; + else + shift_bits = partial_xfer; + + if (xfer->direction & JTAG_WRITE_XFER) + aspeed_jtag_write(aspeed_jtag, + data[partial_index++], + data_reg); + else + aspeed_jtag_write(aspeed_jtag, 0, data_reg); + if (aspeed_jtag->llops->xfer_hw_fifo_delay) + aspeed_jtag->llops->xfer_hw_fifo_delay(); + partial_xfer = partial_xfer - shift_bits; + } + if (remain_xfer > ASPEED_JTAG_HW2_DATA_CHUNK_SIZE) { + shift_bits = ASPEED_JTAG_HW2_DATA_CHUNK_SIZE; + + /* + * Transmit bytes that were not equals to column length + * and after the transfer go to Pause IR/DR. + */ + + ret = aspeed_jtag_shctrl_tms_mask(aspeed_jtag->current_state, shift, exit, + endstate, start_shift, 0, &tms_mask); + if (ret) + return ret; + + reg_val = aspeed_jtag_read(aspeed_jtag, + ASPEED_JTAG_GBLCTRL); + reg_val = reg_val & ~(GENMASK(22, 20)); + aspeed_jtag_write(aspeed_jtag, reg_val | + ASPEED_JTAG_GBLCTRL_FIFO_CTRL_MODE | + ASPEED_JTAG_GBLCTRL_UPDT_SHIFT( + shift_bits), + ASPEED_JTAG_GBLCTRL); + + aspeed_jtag_write(aspeed_jtag, tms_mask | + ASPEED_JTAG_SHCTRL_LWRDT_SHIFT(shift_bits), + ASPEED_JTAG_SHCTRL); + aspeed_jtag_wait_shift_complete(aspeed_jtag); + } else { + /* + * Read bytes equals to column length + */ + shift_bits = remain_xfer; + ret = aspeed_jtag_shctrl_tms_mask(aspeed_jtag->current_state, shift, exit, + endstate, start_shift, end_shift, + &tms_mask); + if (ret) + return ret; + + reg_val = aspeed_jtag_read(aspeed_jtag, + ASPEED_JTAG_GBLCTRL); + reg_val = reg_val & ~(GENMASK(22, 20)); + aspeed_jtag_write(aspeed_jtag, reg_val | + ASPEED_JTAG_GBLCTRL_FIFO_CTRL_MODE | + ASPEED_JTAG_GBLCTRL_UPDT_SHIFT( + shift_bits), + ASPEED_JTAG_GBLCTRL); + + aspeed_jtag_write(aspeed_jtag, tms_mask | + ASPEED_JTAG_SHCTRL_LWRDT_SHIFT( + shift_bits), + ASPEED_JTAG_SHCTRL); + + aspeed_jtag_wait_shift_complete(aspeed_jtag); + } + + partial_index = index; + partial_xfer = partial_xfer_size; + while (partial_xfer) { + if (partial_xfer > + ASPEED_JTAG_DATA_CHUNK_SIZE) { + shift_bits = + ASPEED_JTAG_DATA_CHUNK_SIZE; + data[partial_index++] = + aspeed_jtag_read(aspeed_jtag, + data_reg); + + } else { + shift_bits = partial_xfer; + data[partial_index++] = + aspeed_jtag_read(aspeed_jtag, + data_reg); + } + if (aspeed_jtag->llops->xfer_hw_fifo_delay) + aspeed_jtag->llops->xfer_hw_fifo_delay(); + partial_xfer = partial_xfer - shift_bits; + } + + remain_xfer = remain_xfer - partial_xfer_size; + index = partial_index; + start_shift = 0; + } + aspeed_jtag->current_state = endstate; + return 0; +} + +static int aspeed_jtag_status_get(struct jtag *jtag, u32 *status) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + + *status = aspeed_jtag->current_state; + return 0; +} + +static irqreturn_t aspeed_jtag_interrupt(s32 this_irq, void *dev_id) +{ + struct aspeed_jtag *aspeed_jtag = dev_id; + irqreturn_t ret; + u32 status; + + status = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_ISR); + + if (status & ASPEED_JTAG_ISR_INT_MASK) { + aspeed_jtag_write(aspeed_jtag, + (status & ASPEED_JTAG_ISR_INT_MASK) | + (status & + ASPEED_JTAG_ISR_INT_EN_MASK), + ASPEED_JTAG_ISR); + aspeed_jtag->flag |= status & ASPEED_JTAG_ISR_INT_MASK; + } + + if (aspeed_jtag->flag) { + wake_up_interruptible(&aspeed_jtag->jtag_wq); + ret = IRQ_HANDLED; + } else { + dev_err(aspeed_jtag->dev, "irq status:%x\n", status); + ret = IRQ_NONE; + } + return ret; +} + +static irqreturn_t aspeed_jtag_interrupt_hw2(s32 this_irq, void *dev_id) +{ + struct aspeed_jtag *aspeed_jtag = dev_id; + irqreturn_t ret; + u32 status; + + status = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_INTCTRL); + + if (status & ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT) { + aspeed_jtag_write(aspeed_jtag, + status | ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT, + ASPEED_JTAG_INTCTRL); + aspeed_jtag->flag |= status & ASPEED_JTAG_INTCTRL_SHCPL_IRQ_STAT; + } + + if (aspeed_jtag->flag) { + wake_up_interruptible(&aspeed_jtag->jtag_wq); + ret = IRQ_HANDLED; + } else { + dev_err(aspeed_jtag->dev, "irq status:%x\n", status); + ret = IRQ_NONE; + } + return ret; +} + +static int aspeed_jtag_enable(struct jtag *jtag) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + + aspeed_jtag->llops->controller_enable(aspeed_jtag); + return 0; +} + +static int aspeed_jtag_disable(struct jtag *jtag) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + + aspeed_jtag->llops->output_disable(aspeed_jtag); + return 0; +} + +static int aspeed_jtag_init(struct platform_device *pdev, + struct aspeed_jtag *aspeed_jtag) +{ + struct resource *res; +#ifdef CONFIG_USE_INTERRUPTS + int err; +#endif + memset(aspeed_jtag->pad_data_one, ~0, + sizeof(aspeed_jtag->pad_data_one)); + memset(aspeed_jtag->pad_data_zero, 0, + sizeof(aspeed_jtag->pad_data_zero)); + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + aspeed_jtag->reg_base = devm_ioremap_resource(aspeed_jtag->dev, res); + if (IS_ERR(aspeed_jtag->reg_base)) + return -ENOMEM; + + aspeed_jtag->pclk = devm_clk_get(aspeed_jtag->dev, NULL); + if (IS_ERR(aspeed_jtag->pclk)) { + dev_err(aspeed_jtag->dev, "devm_clk_get failed\n"); + return PTR_ERR(aspeed_jtag->pclk); + } + +#ifdef CONFIG_USE_INTERRUPTS + aspeed_jtag->irq = platform_get_irq(pdev, 0); + if (aspeed_jtag->irq < 0) { + dev_err(aspeed_jtag->dev, "no irq specified\n"); + return -ENOENT; + } +#endif + + if (clk_prepare_enable(aspeed_jtag->pclk)) { + dev_err(aspeed_jtag->dev, "no irq specified\n"); + return -ENOENT; + } + + aspeed_jtag->rst = devm_reset_control_get_shared(&pdev->dev, NULL); + if (IS_ERR(aspeed_jtag->rst)) { + dev_err(aspeed_jtag->dev, + "missing or invalid reset controller device tree entry"); + return PTR_ERR(aspeed_jtag->rst); + } + reset_control_deassert(aspeed_jtag->rst); + +#ifdef CONFIG_USE_INTERRUPTS + err = devm_request_irq(aspeed_jtag->dev, aspeed_jtag->irq, + aspeed_jtag->llops->jtag_interrupt, 0, + "aspeed-jtag", aspeed_jtag); + if (err) { + dev_err(aspeed_jtag->dev, "unable to get IRQ"); + clk_disable_unprepare(aspeed_jtag->pclk); + return err; + } +#endif + + aspeed_jtag->llops->output_disable(aspeed_jtag); + + aspeed_jtag->flag = 0; + aspeed_jtag->mode = 0; + aspeed_jtag->tck_cycle_delay_count = 0; + init_waitqueue_head(&aspeed_jtag->jtag_wq); + return 0; +} + +static int aspeed_jtag_deinit(struct platform_device *pdev, + struct aspeed_jtag *aspeed_jtag) +{ + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_ISR); + /* Disable clock */ + aspeed_jtag_write(aspeed_jtag, 0, ASPEED_JTAG_CTRL); + reset_control_assert(aspeed_jtag->rst); + clk_disable_unprepare(aspeed_jtag->pclk); + return 0; +} + +static int aspeed_jtag_trst_set_hw1(struct jtag *jtag, u32 active) +{ + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + + aspeed_jtag_write(aspeed_jtag, active ? 0 : ASPEED_JTAG_EC_TRST, + ASPEED_JTAG_EC); + return 0; +} + +static int aspeed_jtag_trst_set_hw2(struct jtag *jtag, u32 active) +{ + u32 reg_val; + struct aspeed_jtag *aspeed_jtag = jtag_priv(jtag); + + reg_val = aspeed_jtag_read(aspeed_jtag, ASPEED_JTAG_GBLCTRL); + if (active) + reg_val |= ASPEED_JTAG_GBLCTRL_TRST; + else + reg_val &= ~ASPEED_JTAG_GBLCTRL_TRST; + aspeed_jtag_write(aspeed_jtag, reg_val, ASPEED_JTAG_GBLCTRL); + return 0; +} + +static const struct jtag_ops aspeed_jtag_ops = { + .freq_get = aspeed_jtag_freq_get, + .freq_set = aspeed_jtag_freq_set, + .status_get = aspeed_jtag_status_get, + .status_set = aspeed_jtag_status_set, + .xfer = aspeed_jtag_xfer, + .mode_set = aspeed_jtag_mode_set, + .trst_set = aspeed_jtag_trst_set_hw1, + .bitbang = aspeed_jtag_bitbang, + .enable = aspeed_jtag_enable, + .disable = aspeed_jtag_disable +}; + +static const struct jtag_ops aspeed_jtag_ops_26xx = { +#ifdef ASPEED_JTAG_HW_MODE_2_ENABLE + .freq_get = aspeed_jtag_freq_get_26xx, + .freq_set = aspeed_jtag_freq_set_26xx, + .status_get = aspeed_jtag_status_get, + .status_set = aspeed_jtag_status_set_26xx, + .trst_set = aspeed_jtag_trst_set_hw2, +#else + .freq_get = aspeed_jtag_freq_get, + .freq_set = aspeed_jtag_freq_set, + .status_get = aspeed_jtag_status_get, + .status_set = aspeed_jtag_status_set, + .trst_set = aspeed_jtag_trst_set_hw1, +#endif + .xfer = aspeed_jtag_xfer, + .mode_set = aspeed_jtag_mode_set, + .bitbang = aspeed_jtag_bitbang, + .enable = aspeed_jtag_enable, + .disable = aspeed_jtag_disable +}; + +static const struct jtag_low_level_functions ast25xx_llops = { + .controller_enable = aspeed_jtag_controller, + .output_disable = aspeed_jtag_output_disable, + .xfer_push_data = aspeed_jtag_xfer_push_data, + .xfer_push_data_last = aspeed_jtag_xfer_push_data_last, + .xfer_sw = aspeed_jtag_xfer_sw, + .xfer_hw = aspeed_jtag_xfer_hw, + .xfer_hw_fifo_delay = NULL, + .xfer_sw_delay = NULL, + .xfer_tck_cycle_delay = aspeed_jtag_tck_cycle_delay, + .jtag_interrupt = aspeed_jtag_interrupt +}; + +static const struct aspeed_jtag_functions ast25xx_functions = { + .aspeed_jtag_ops = &aspeed_jtag_ops, + .aspeed_jtag_llops = &ast25xx_llops +}; + +static const struct jtag_low_level_functions ast26xx_llops = { +#ifdef ASPEED_JTAG_HW_MODE_2_ENABLE + .controller_enable = aspeed_jtag_controller_26xx, + .output_disable = aspeed_jtag_output_disable_26xx, + .xfer_push_data = aspeed_jtag_xfer_push_data_26xx, + .xfer_push_data_last = aspeed_jtag_xfer_push_data_last_26xx, + .xfer_sw = aspeed_jtag_xfer_sw, + .xfer_hw = aspeed_jtag_xfer_hw2, + .xfer_hw_fifo_delay = aspeed_jtag_xfer_hw_fifo_delay_26xx, + .xfer_sw_delay = aspeed_jtag_sw_delay_26xx, + .xfer_tck_cycle_delay = aspeed_jtag_tck_cycle_delay, + .jtag_interrupt = aspeed_jtag_interrupt_hw2 +#else + .controller_enable = aspeed_jtag_controller, + .output_disable = aspeed_jtag_output_disable, + .xfer_push_data = aspeed_jtag_xfer_push_data_26xx, + .xfer_push_data_last = aspeed_jtag_xfer_push_data_last_26xx, + .xfer_sw = aspeed_jtag_xfer_sw, + .xfer_hw = aspeed_jtag_xfer_hw, + .xfer_hw_fifo_delay = aspeed_jtag_xfer_hw_fifo_delay_26xx, + .xfer_sw_delay = aspeed_jtag_sw_delay_26xx, + .xfer_tck_cycle_delay = aspeed_jtag_tck_cycle_delay, + .jtag_interrupt = aspeed_jtag_interrupt +#endif +}; + +static const struct aspeed_jtag_functions ast26xx_functions = { + .aspeed_jtag_ops = &aspeed_jtag_ops_26xx, + .aspeed_jtag_llops = &ast26xx_llops +}; + +static const struct of_device_id aspeed_jtag_of_match[] = { + { .compatible = "aspeed,ast2400-jtag", .data = &ast25xx_functions }, + { .compatible = "aspeed,ast2500-jtag", .data = &ast25xx_functions }, + { .compatible = "aspeed,ast2600-jtag", .data = &ast26xx_functions }, + {} +}; + +static int aspeed_jtag_probe(struct platform_device *pdev) +{ + struct aspeed_jtag *aspeed_jtag; + struct jtag *jtag; + const struct of_device_id *match; + const struct aspeed_jtag_functions *jtag_functions; + int err; + + match = of_match_node(aspeed_jtag_of_match, pdev->dev.of_node); + if (!match) + return -ENODEV; + jtag_functions = match->data; + + jtag = jtag_alloc(&pdev->dev, sizeof(*aspeed_jtag), + jtag_functions->aspeed_jtag_ops); + if (!jtag) + return -ENOMEM; + + platform_set_drvdata(pdev, jtag); + aspeed_jtag = jtag_priv(jtag); + aspeed_jtag->dev = &pdev->dev; + + aspeed_jtag->llops = jtag_functions->aspeed_jtag_llops; + + /* Initialize device*/ + err = aspeed_jtag_init(pdev, aspeed_jtag); + if (err) + goto err_jtag_init; + + /* Initialize JTAG core structure*/ + err = devm_jtag_register(aspeed_jtag->dev, jtag); + if (err) + goto err_jtag_register; + + jtag_functions->aspeed_jtag_ops->freq_set(jtag, 1000000); + + return 0; + +err_jtag_register: + aspeed_jtag_deinit(pdev, aspeed_jtag); +err_jtag_init: + jtag_free(jtag); + return err; +} + +static int aspeed_jtag_remove(struct platform_device *pdev) +{ + struct jtag *jtag = platform_get_drvdata(pdev); + + aspeed_jtag_deinit(pdev, jtag_priv(jtag)); + return 0; +} + +static struct platform_driver aspeed_jtag_driver = { + .probe = aspeed_jtag_probe, + .remove = aspeed_jtag_remove, + .driver = { + .name = ASPEED_JTAG_NAME, + .of_match_table = aspeed_jtag_of_match, + }, +}; +module_platform_driver(aspeed_jtag_driver); + +MODULE_AUTHOR("Oleksandr Shamray "); +MODULE_DESCRIPTION("ASPEED JTAG driver"); +MODULE_LICENSE("GPL v2");