From patchwork Sun Aug 28 13:33:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2315 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id AEAA740F47 for ; Sun, 28 Aug 2022 15:33:43 +0200 (CEST) Received: by mail-ed1-f71.google.com with SMTP id z20-20020a05640235d400b0043e1e74a495sf4069898edc.11 for ; Sun, 28 Aug 2022 06:33:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1661693623; cv=pass; d=google.com; s=arc-20160816; b=LsZDV3jdqCTm+pv65+MYIkvFxBHdft6D/gSlklXqsiTWZvWg/BFw9fgv+QADc2LQDm GV9NvHbsmVM2rRGscuKe86dfmEHVMDTUMQvIlIdG7kSOcohTAjYkU+R1EmFQG1K6OguJ 4JrPMe4cEaO6NnkrTGPdWLuXKyZ7RhfW10AXqh/yu0mhEGHHFrEUTYZFKAx4G2MkK8ai f0IGzWRt3PPzhkt88OJALZhIcyY45tHIxT9YOsYkVL2F8AyMi1RDROCrUoPQBbrWqrmW XoN60xTb9NPuMoxNSJSgZQ8BSkWHgELClMi1xVyi00M09zIgMziP+XGxKrGmCbcEYp9x g8tA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=TAhZSbIGc69MS57ANMCvu+zLEXnmmAtyMUVN9Slx0nU=; b=pxHv5xdvL/k5ue5qfvCUHX8VMxe2v8NmJJEeBX3/Dn4pR1K5NKu20wjS6l/wZ/9LvY IqYBMOZbHRFb+Zg/x2j9EkHx3nmEm12Oz+tyWavcXg/xMoPma5vRZyNNsZDn7OxS983z 9y9zSO3H2uMHGSgGzx7LvXH5Cl9SZ6EPJ8MMzon1InSyY5HfvyfSHYK6l5zA8afYCC0y n9orYxRLQv55YGD+Lqx9IuD+21WbrdrZp609dXXb+gw6Gln+mlAexX0D91RlAM20Lnu7 pkLR8z6s0SXBjNKy3QFLuJEguUglDQMUB82txx60PzsK3pXqze1AFkomdm13VZOAXUNj CTTQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=mGwOSs8u; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:from:to:cc; bh=TAhZSbIGc69MS57ANMCvu+zLEXnmmAtyMUVN9Slx0nU=; b=c8W4SkJuJioX0g+mqRi6DoRlKgeE6ob4r9zsOB35NFcZRLH5r+wDp9Nwh/sGLpFLuk Zf22P3gcTc2xlOKrUUD3uzMXrqsydC2ERzRxCpZsLhRYn++OZmHZROwL8ZJPr60gsBCk Ly7AKlhLT9NoBMaRTapPrOPh7tjSAoOs2IrXM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=list-unsubscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc; bh=TAhZSbIGc69MS57ANMCvu+zLEXnmmAtyMUVN9Slx0nU=; b=ahSZHAvmdP2OPpq5ti/7rX+DiRDUs2HM1FRN5JKKsyQxL057YzLb2Q6a1hCinAcGmm gzimbH/Qs8ZQyQMBqSZaXhPmHbwyI7JxIzG2KvimtKtt2uDZbwCwAJ9yvRd0lIbUe9An HZAfI7eLZiitknZV5WjSqIC17e6HufVYcl1ZE7Zz/3vZrqbCMY033ioQRHqZTZORMIUQ eAG/oYrsPGsc0OJliV+8kJnHhvT6zBQuK3OKSJXAbtsN60WxC4IYfYhE4Vlojk2dzOP7 ixUAnchfzkIr80ueNtLbmHrPJeXwO7a43/bfwTeM7uk97knnKA7cc9xRVgRBc029YAQU cAAA== X-Gm-Message-State: ACgBeo3zjqavUHslRrc47sLVSmVZEYMlwT8xc4OfXQnP5MFajs0B6jL+ Cd9mFuv9rIyqOkHeH4VdAxUEZ8fB X-Google-Smtp-Source: AA6agR6aYNZbFDu/84bf8rtRGpGnHknEVLgieng6UHTJeRMXUdY/hNzJrY381kBerhuTIRm0N2fOjw== X-Received: by 2002:a05:6402:2751:b0:443:d90a:43d4 with SMTP id z17-20020a056402275100b00443d90a43d4mr13749316edd.368.1661693623461; Sun, 28 Aug 2022 06:33:43 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:906:2e8b:b0:726:abf9:5f2e with SMTP id o11-20020a1709062e8b00b00726abf95f2els3894797eji.9.-pod-prod-gmail; Sun, 28 Aug 2022 06:33:42 -0700 (PDT) X-Received: by 2002:a17:906:c153:b0:741:78ac:853e with SMTP id dp19-20020a170906c15300b0074178ac853emr1468349ejc.191.1661693619300; Sun, 28 Aug 2022 06:33:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661693619; cv=none; d=google.com; s=arc-20160816; b=WCwIctWd8I4ouj0mh+6vK3ulCWiZ9ETV9cnty5hM4U6kV24tE6ceawwdbRhbrsLQPg 8TS/74ektY1Ap22FPGduwZAnM52HuLCLQDHEF1amYy1sDrGm8YyHBooLntrFUpg0nNDl 2rypX5QTWDrQxPKQrN/UYWW54LAnhCuIwZ7yrAsaX003KBs9omu1uXCJB8uibyzd+OoT mE5jlP2dbbmFtfnESNU1+Rg8pCdssokVJWkwyJKKbo+WSiG8W+Vnsa+LPFOysHDJpGat dhmEIf87moDtSOSOVXJe+KQ/qv+I2UZrMNkI/u9pcYgOIMz4ZYBtVUkd2V4xBepL6DgL +O6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=b/btFehREFVzIjnfwZ0fR/od68XSqMjGfHOju/rQyoU=; b=JC/59tK6DHS/PMsxPF6gqZ/JhvVOuJtkW5/H3T+BE65EhVmC5M89iZLk7aMkBTN+qO r/mJqdWI7v2I9Vw/k8iELCfxzethr/3KKrRQISYuQGAm2xeDbN5Rh1xqz+pcqIQHT7rb ZkSTyIzgtjhgjg9FJrk+aONBojblr4UqWVv+M0HYKTqogZLYuPnpkJYBg/rlEp6d/82d cP4OzxmPlrZufdMRKuB47ttkaIEazXqZrCQ9bKX/2HElRfA5djVkEOalJ/Bu76kcAxLG IbMlj2x28OsPRJ81bRZzYWnQW5ik3w/IhTg3M3IupNiOdgrowgxm1PCWFe6YFOjsTcs9 C87w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=mGwOSs8u; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id se19-20020a170906ce5300b0073e484fcbcfsor2276642ejb.17.2022.08.28.06.33.39 for (Google Transport Security); Sun, 28 Aug 2022 06:33:39 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a17:906:eec7:b0:733:189f:b07a with SMTP id wu7-20020a170906eec700b00733189fb07amr10600791ejb.230.1661693619043; Sun, 28 Aug 2022 06:33:39 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-79-31-31-9.retail.telecomitalia.it. [79.31.31.9]) by smtp.gmail.com with ESMTPSA id u26-20020a1709064ada00b007313a25e56esm3247669ejt.29.2022.08.28.06.33.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Aug 2022 06:33:38 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Marc Kleine-Budde , Alexandre Torgue , michael@amarulasolutions.com, Amarula patchwork , Vincent Mailhol , Krzysztof Kozlowski , Rob Herring , Dario Binacchi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Krzysztof Kozlowski , Maxime Coquelin , Paolo Abeni , Rob Herring , Wolfgang Grandegger , devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-can@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, netdev@vger.kernel.org Subject: [RFC PATCH v3 1/4] dt-bindings: net: can: add STM32 bxcan DT bindings Date: Sun, 28 Aug 2022 15:33:26 +0200 Message-Id: <20220828133329.793324-2-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220828133329.793324-1-dario.binacchi@amarulasolutions.com> References: <20220828133329.793324-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=mGwOSs8u; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Add documentation of device tree bindings for the STM32 basic extended CAN (bxcan) controller. Signed-off-by: Dario Binacchi --- Changes in v3: - Remove 'Dario Binacchi ' SOB. - Add description to the parent of the two child nodes. - Move "patterProperties:" after "properties: in top level before "required". - Add "clocks" to the "required:" list of the child nodes. Changes in v2: - Change the file name into 'st,stm32-bxcan-core.yaml'. - Rename compatibles: - st,stm32-bxcan-core -> st,stm32f4-bxcan-core - st,stm32-bxcan -> st,stm32f4-bxcan - Rename master property to st,can-master. - Remove the status property from the example. - Put the node child properties as required. .../bindings/net/can/st,stm32-bxcan.yaml | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/can/st,stm32-bxcan.yaml diff --git a/Documentation/devicetree/bindings/net/can/st,stm32-bxcan.yaml b/Documentation/devicetree/bindings/net/can/st,stm32-bxcan.yaml new file mode 100644 index 000000000000..3278c724e6f5 --- /dev/null +++ b/Documentation/devicetree/bindings/net/can/st,stm32-bxcan.yaml @@ -0,0 +1,142 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/net/can/st,stm32-bxcan.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: STMicroelectronics bxCAN controller + +description: STMicroelectronics BxCAN controller for CAN bus + +maintainers: + - Dario Binacchi + +allOf: + - $ref: can-controller.yaml# + +properties: + compatible: + description: + It manages the access to the 512-bytes SRAM memory shared by the + two bxCAN cells (CAN1 master and CAN2 slave) in dual CAN peripheral + configuration. + enum: + - st,stm32f4-bxcan-core + + reg: + maxItems: 1 + + resets: + maxItems: 1 + + clocks: + description: + Input clock for registers access + maxItems: 1 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + +patternProperties: + "^can@[0-9]+$": + type: object + description: + A CAN block node contains two subnodes, representing each one a CAN + instance available on the machine. + + properties: + compatible: + enum: + - st,stm32f4-bxcan + + st,can-master: + description: + Master and slave mode of the bxCAN peripheral is only relevant + if the chip has two CAN peripherals. In that case they share + some of the required logic, and that means you cannot use the + slave CAN without the master CAN. + type: boolean + + reg: + description: | + Offset of CAN instance in CAN block. Valid values are: + - 0x0: CAN1 + - 0x400: CAN2 + maxItems: 1 + + interrupts: + items: + - description: transmit interrupt + - description: FIFO 0 receive interrupt + - description: FIFO 1 receive interrupt + - description: status change error interrupt + + interrupt-names: + items: + - const: tx + - const: rx0 + - const: rx1 + - const: sce + + resets: + maxItems: 1 + + clocks: + description: + Input clock for registers access + maxItems: 1 + + additionalProperties: false + + required: + - compatible + - reg + - interrupts + - resets + - clocks + +required: + - compatible + - reg + - resets + - clocks + - '#address-cells' + - '#size-cells' + +additionalProperties: false + +examples: + - | + #include + #include + + can: can@40006400 { + compatible = "st,stm32f4-bxcan-core"; + reg = <0x40006400 0x800>; + resets = <&rcc STM32F4_APB1_RESET(CAN1)>; + clocks = <&rcc 0 STM32F4_APB1_CLOCK(CAN1)>; + #address-cells = <1>; + #size-cells = <0>; + + can1: can@0 { + compatible = "st,stm32f4-bxcan"; + reg = <0x0>; + interrupts = <19>, <20>, <21>, <22>; + interrupt-names = "tx", "rx0", "rx1", "sce"; + resets = <&rcc STM32F4_APB1_RESET(CAN1)>; + clocks = <&rcc 0 STM32F4_APB1_CLOCK(CAN1)>; + st,can-master; + }; + + can2: can@400 { + compatible = "st,stm32f4-bxcan"; + reg = <0x400>; + interrupts = <63>, <64>, <65>, <66>; + interrupt-names = "tx", "rx0", "rx1", "sce"; + resets = <&rcc STM32F4_APB1_RESET(CAN2)>; + clocks = <&rcc 0 STM32F4_APB1_CLOCK(CAN2)>; + }; + }; From patchwork Sun Aug 28 13:33:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2314 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 8D46740F47 for ; Sun, 28 Aug 2022 15:33:42 +0200 (CEST) Received: by mail-ej1-f69.google.com with SMTP id gn30-20020a1709070d1e00b0074144af99d1sf453154ejc.17 for ; Sun, 28 Aug 2022 06:33:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1661693622; cv=pass; d=google.com; s=arc-20160816; b=XziIgmahce00Z/V6KdmrLnQDHR89vsEJ0diAkUtaHZ109DmyTmTANxHstRYBOAQ8Qj 7WLbWEvIjbGEDtvZkJ/f8CKpuvnDdfDTXw+fadIj4Hum57oDMXs2S6hjYtsBncsPVnvl nfDxyp/kGa/fq7xJ729hYvmE4IPoqaEqTeUdc07E51Hd6Ke2KdNMerfbMl7079GexlnS EpFsQlElC7PH0I6tezkbRcdApsx6aw7Q372pkT3R41zBowvkx0Z5RKIOnOnY2i8V0aRM 9jJzeZ7UOy6RG1LqOMjei6oqIs/gzmfg3kCuSfzWD0Ye5LaZOm5IrvMtQ7aCjzVyJVOQ Ierw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=Zk56aSo+9z4P7U+osQTtGljp9np4/I1jB685lUEYwr0=; b=MGGrmhGuj11a9PvVccM0h1UMNpTjiw3ddcAJayMepC6KEXc3wP/GclS6l9jotQMKrL pG6BchxVTLV/tkCusafWtICazKI2WrchyL77dkW7bJ2PXYbQLWm+TP3LKLu6rb6PSt1W DMEgquf6bMV8oRfL/b5UcbT96RZU52cxDkoaSGO3mI5Hi5S1IDEhH7Fa9Z/L4WJjNuun BJ1C80uEgn+TgVAMmxui2c3Ik/25ush1LlNcHTBeJi01cZjLXRUff0VkdvlA3sEPS2lz WdWxpCHbEdfDfpDGrjCTAKcR7wMq5tdmsHPBTLY+EN0vtsJhJsr/4vf9lorU8QvKykak A8JQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=J46e2leN; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:from:to:cc; bh=Zk56aSo+9z4P7U+osQTtGljp9np4/I1jB685lUEYwr0=; b=lnyxAHfHsTBF+g7St+wh37N39SmDlsegNB2ugTQ9zUFM57DYoTmm9DMfXcAz0uK0Ti hYEZuItBsefUwapsb50+j6kKlNohtMmu37KHLoJonGc7/46dji0oT6PPQEPH/OZbJ46Q QmPvASjEQDEFwmOYgx1lmlsSeIcaimrMoldZI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=list-unsubscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc; bh=Zk56aSo+9z4P7U+osQTtGljp9np4/I1jB685lUEYwr0=; b=GZqZ5/edd5oxssN/x8bh1ouecFrjNEobHofhnKCEGPX1Kbg2PVL+29QZ5NH3OYI+ZD ZLrmDdJG4+Wxf/cXk9ojI32sZk1o5dVg9YkihOtQ63eQzszCssdLpb2URZ9xz+uXTorx fOPYzQsXtWUI0Aj7eG87thzndFbidIuWUt8fNp0HwgCwIHy//6LFP/XOQu63CnuCTUHe OmpAh8oaTl7BYxPhJGLkXAojwhXVEYRJYD3weqvVHYFM8cFI4EAQpKMvvrC+aXdakRNz 3ntFptk/s5kOvaRBZk73A2mPpSUmn1BvGE5z13GfybZrNns/BwMRoJlliQ7VxN59QcAI waKA== X-Gm-Message-State: ACgBeo2VmSXoP/0N57VczEjBbhy74GxBxtwxi3xdNjNdBhJUL5HHXWwk On+yXdJEutBYZ2zQCvnPpOiTBl+L X-Google-Smtp-Source: AA6agR5UX9MTPNunHpF7fHcvElMbI9jTAcsuocgoCxGa4MYLFEULmd03pwvwZrY8KCiBXU7v2eOwQQ== X-Received: by 2002:a17:906:ee8e:b0:730:3646:d178 with SMTP id wt14-20020a170906ee8e00b007303646d178mr10849113ejb.426.1661693622181; Sun, 28 Aug 2022 06:33:42 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:906:a409:b0:73d:11ed:5035 with SMTP id l9-20020a170906a40900b0073d11ed5035ls3919480ejz.0.-pod-prod-gmail; Sun, 28 Aug 2022 06:33:41 -0700 (PDT) X-Received: by 2002:a17:906:9c82:b0:6e1:1d6c:914c with SMTP id fj2-20020a1709069c8200b006e11d6c914cmr10431591ejc.769.1661693620941; Sun, 28 Aug 2022 06:33:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661693620; cv=none; d=google.com; s=arc-20160816; b=WnjOTVZlvK89bjFJuXWerPqQ06C4MzoSK7a4RN47dJ/bfo/osWD90oTLv66W4fW5jB 5W4WBgmj4uwGnydXbarwxAtMHDThDxvCkq6Z/hVA4nLB7GiqvIdw11hdKUBxSlOcYkA8 NxcOoaTXT4B2ywdqBteNou5Ux7FBUcWcGGtuZ/+yB3pMbOwTcI4+W1BoYZTm+PHYpd1x /ACsp8IvTKx7KsDHcbJvBZZVWhEtti/fIgvA0qAGQUWwcPsqEM6TdLadXggvf5QAcqJ3 h9gODhnwiDluVENykYCiXyUL1nVlofAYJUHLCHKi9k2AInYopogLNW3TpoaEPXhlLxCK 4teA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=RJr2H0obYN3pbdXZsdt8e1JbQn4d3baYnjPncxC8Sds=; b=sdNFSl7GFvVvkqchl82wTJF6ISMVyDJ22Y4SGdo3glaZuypiX1UHLSircIybNP/D4y 1EovE99ymoaiTpbqNLCKGgNu7qundiO2Jb6BDIk0KkoDYfBszOSYYHPhwEIzYSfG9pNe FTPSfGv1o3eA4NhKGN88rykd5xQa49ljJ8QwGJgXrXRGux4Eg+V/9JX39JTuX1Yehr9I 3oc+5i3cecJ2FPjThHojF70mEhFN9Ra8r2Qw9NgIxI3GnwijEf6ItJQcd/Twa3XPCl+e 5cZUkoZNubZtEzL3Rd9LfTtY6grDCehOig0+tqRR8aaoRxpcyDyA8r1w/oMieVJ08Axc kIVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=J46e2leN; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id n24-20020a5099d8000000b00446ad76b8b3sor3087892edb.25.2022.08.28.06.33.40 for (Google Transport Security); Sun, 28 Aug 2022 06:33:40 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:aa7:cfcb:0:b0:447:b4e5:22fb with SMTP id r11-20020aa7cfcb000000b00447b4e522fbmr12466111edy.190.1661693620710; Sun, 28 Aug 2022 06:33:40 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-79-31-31-9.retail.telecomitalia.it. [79.31.31.9]) by smtp.gmail.com with ESMTPSA id u26-20020a1709064ada00b007313a25e56esm3247669ejt.29.2022.08.28.06.33.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Aug 2022 06:33:40 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Marc Kleine-Budde , Alexandre Torgue , michael@amarulasolutions.com, Amarula patchwork , Vincent Mailhol , Krzysztof Kozlowski , Rob Herring , Dario Binacchi , Krzysztof Kozlowski , Maxime Coquelin , Rob Herring , devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-stm32@st-md-mailman.stormreply.com Subject: [RFC PATCH v3 2/4] ARM: dts: stm32: add CAN support on stm32f429 Date: Sun, 28 Aug 2022 15:33:27 +0200 Message-Id: <20220828133329.793324-3-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220828133329.793324-1-dario.binacchi@amarulasolutions.com> References: <20220828133329.793324-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=J46e2leN; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Add support for bxcan (Basic eXtended CAN controller) to STM32F429. The chip contains two CAN peripherals, CAN1 the master and CAN2 the slave, that share some of the required logic like clock and filters. This means that the slave CAN can't be used without the master CAN. Signed-off-by: Dario Binacchi --- Changes in v3: - Remove 'Dario Binacchi ' SOB. - Add "clocks" to can@0 node. arch/arm/boot/dts/stm32f429.dtsi | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/arch/arm/boot/dts/stm32f429.dtsi b/arch/arm/boot/dts/stm32f429.dtsi index c31ceb821231..e04cf73a8caa 100644 --- a/arch/arm/boot/dts/stm32f429.dtsi +++ b/arch/arm/boot/dts/stm32f429.dtsi @@ -362,6 +362,37 @@ i2c3: i2c@40005c00 { status = "disabled"; }; + can: can@40006400 { + compatible = "st,stm32f4-bxcan-core"; + reg = <0x40006400 0x800>; + resets = <&rcc STM32F4_APB1_RESET(CAN1)>; + clocks = <&rcc 0 STM32F4_APB1_CLOCK(CAN1)>; + #address-cells = <1>; + #size-cells = <0>; + status = "disabled"; + + can1: can@0 { + compatible = "st,stm32f4-bxcan"; + reg = <0x0>; + interrupts = <19>, <20>, <21>, <22>; + interrupt-names = "tx", "rx0", "rx1", "sce"; + resets = <&rcc STM32F4_APB1_RESET(CAN1)>; + clocks = <&rcc 0 STM32F4_APB1_CLOCK(CAN1)>; + st,can-master; + status = "disabled"; + }; + + can2: can@400 { + compatible = "st,stm32f4-bxcan"; + reg = <0x400>; + interrupts = <63>, <64>, <65>, <66>; + interrupt-names = "tx", "rx0", "rx1", "sce"; + resets = <&rcc STM32F4_APB1_RESET(CAN2)>; + clocks = <&rcc 0 STM32F4_APB1_CLOCK(CAN2)>; + status = "disabled"; + }; + }; + dac: dac@40007400 { compatible = "st,stm32f4-dac-core"; reg = <0x40007400 0x400>; From patchwork Sun Aug 28 13:33:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2316 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 0110742016 for ; Sun, 28 Aug 2022 15:33:44 +0200 (CEST) Received: by mail-ed1-f69.google.com with SMTP id m18-20020a056402511200b0044862412596sf760087edd.3 for ; Sun, 28 Aug 2022 06:33:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1661693623; cv=pass; d=google.com; s=arc-20160816; b=UrZZORfNuY2qmNyV3ifOxRE3uaZh56FrPJMiilbeF+fUEOvyQCzpXmHsBWKGqnipOn AteSCicdh8tDA//lQylq4oSUH9bITOTuW15YdqZ1jo8ypc8FLhSK4Pzos1o85v00R7FV y672Kxb66Elv3Whu6D/QFiz4IEikd83KY5qg0NiSKb6Ff3eKETdBkuLAUTbM0G8Z/P9n xYWzlUPPVM1PrsB4Sr/wvm6RV8/zUG7mJaszLyLN82YFWaKbDOULi+P4K3/HmflW8XG+ S0MNfZBrM9eQ7rnPSoS5eqJTkCZ/qQDkiIWyk1zkcWJ2I2KVdSxE1eulNJlQHmEVESMD rl3g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=sPhXXaHW35RUGOBNBzWh6+r/nrIXt/bm3ZUDfsCdJPc=; b=JkFF9XQ/BmyyGAhmmZjtRQqNIrKONigsBU/HTTstHDfzejO1HhGqbl7JQnrMBUtXqv ptsnPdI/TzojT70/9e2X+lktakHP5E8l1vBT/si+slYKQj49qOlKN3t4d2Arg/FAP+1T J+gZb4kSZPs1HSiTEnWzVjajx5qOhZfwdYao+EByv0aykuzlceXdCJmxpw7oLpoHFMKD o3k5+fJLZM3pGrA/ceUqwzT47gLis/URAq1rZAUaiV64M62FJ5smnjdkqk5Dj+jFgg/c rGfr/Ii9BFH9q8o3/lATZyMqGP5A7C0ewlOuj91eTJLi5EALWre9nQ6gvR2OCCUE4pYI ef4Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=ptcQCXsH; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:from:to:cc; bh=sPhXXaHW35RUGOBNBzWh6+r/nrIXt/bm3ZUDfsCdJPc=; b=jgSNrMl8ze2I63WHWF28pcuEWEE+svAmSDiXP3E2B7i28Vbn6QMI3DRybc6FNcJ7Mi zHRhMNb7B9y1sgGQM2czkB/yjLYsApHa0DamOnY4PqfWnRQcz0vj26i2SCX0GdsorW1T YTK2Peg/RFqJS38h8R29wzysliSWwAi3fPRUA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=list-unsubscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc; bh=sPhXXaHW35RUGOBNBzWh6+r/nrIXt/bm3ZUDfsCdJPc=; b=4cDd3T7hp9IXECfHLBlLzpXmsw9J8nUHtzmfZR1HubhQrZ8zAZAgxV0lf7ghFwyTQ2 e1aiH30HdMcGOrFJ/55EJwSmit5ldaE/vntaQbu1bZn/NuAg2KyiNMqLJaa1JUKBxzFP +j+Bn4LqBfTuBiN7+JRQCl+Ff+5ydv3o1RzUZM8Jy23m7DWvEVe2B9Pso4RuIFeMvjaF cMO1Fb2zCdGADRU5iUE4+N6KZoMnOj7NEQLnu/TBFMFikFyPZ4BipUp+1w7hrDEIt6LH IkFyS7/CC9EXFB+F/iDVtBlJhTVoLtXyJn8K0Kyrz4pkFrzUSFJYf3JrtQqb5Mmu6XAL ZLgQ== X-Gm-Message-State: ACgBeo1KjfIM9wUOufKG+h7DLq52A2fmEmf4kqef/2A90bk1SzPoOGdd OcJpf65XEsISmMopeRKlSM/TyI8S X-Google-Smtp-Source: AA6agR5bWg7r/l4FL8M5LTxWlG7ohQU+zkHicwFo2Fo9ZIMwZOFfxs5COHPSj1a9WbMlblOtyC57mA== X-Received: by 2002:aa7:d385:0:b0:447:d5ec:d452 with SMTP id x5-20020aa7d385000000b00447d5ecd452mr9956055edq.231.1661693623743; Sun, 28 Aug 2022 06:33:43 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:6402:5241:b0:448:77f2:6859 with SMTP id t1-20020a056402524100b0044877f26859ls192511edd.3.-pod-prod-gmail; Sun, 28 Aug 2022 06:33:42 -0700 (PDT) X-Received: by 2002:a05:6402:31e7:b0:447:d664:83eb with SMTP id dy7-20020a05640231e700b00447d66483ebmr9698288edb.276.1661693622580; Sun, 28 Aug 2022 06:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661693622; cv=none; d=google.com; s=arc-20160816; b=GdPeomS2X9e3R6i5gemdRVtVQGR2zZuxHYKzMtX2/wUaC59TkC48Bq8u/kTlx4MU8N kwCXUhdy/ozTZQ9npDT58LUOQKq9tKJXlOQqGviXH7x9ckvf426EDOAO9uSk59u8KWfw MI77X/Mie1NU0TVDtApZQ/+o4+gST4entQwpoe/RYuIagEk4GNo0enGhGLL+9721phEN H5vnen13VzXxazv2lNBl1XhNBMOgylayLDM1Nnj92ICCsGmOwZhRSXdesWmLGfguJn/4 WBpOdO8395D+FCd9XyBDF3cDj1yrHgC6Kna+zlMJyBaJ88xvAshsNHnSAmZP3b0ACRr5 AzoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=olNapHSIxEXV/j68X2DSb6GA5s0CD+ILvvqYswhlRrM=; b=jT5+xH0ogIMc3LelT040QveX3JWas+XQLekY0NOP7a9JQs52T3wo1Pnhc9z2Co3c6n ojJoVNixzWvQ6LN+hZOrI12j1GKDRQLh70s4DHi2Spp69mUHUDiQqb7RFk6BOFnQv0iH bcbOziX4jBmpAeDyd1HrH42lN7hKY+2HPjP542IvbS6un8MWVqEqGGs1HYWzdqqJkYrt b6Urbtw4FP/idt1BkqyYUstSSbhpYc2v1dgDbr0FRieLOR5wvK7JIbXopEzLn/LhxdVT zGNK+GxGhJEtDtomxTFJQPB2Nc3ITTgYbPqJ89IPDcShWsUxplf6+v51ZUPoMHOQzPLE yMjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=ptcQCXsH; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id m3-20020a509303000000b004477895bafbsor2972292eda.7.2022.08.28.06.33.42 for (Google Transport Security); Sun, 28 Aug 2022 06:33:42 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a05:6402:3596:b0:447:11ea:362d with SMTP id y22-20020a056402359600b0044711ea362dmr13480103edc.117.1661693622388; Sun, 28 Aug 2022 06:33:42 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-79-31-31-9.retail.telecomitalia.it. [79.31.31.9]) by smtp.gmail.com with ESMTPSA id u26-20020a1709064ada00b007313a25e56esm3247669ejt.29.2022.08.28.06.33.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Aug 2022 06:33:41 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Marc Kleine-Budde , Alexandre Torgue , michael@amarulasolutions.com, Amarula patchwork , Vincent Mailhol , Krzysztof Kozlowski , Rob Herring , Dario Binacchi , Krzysztof Kozlowski , Maxime Coquelin , Rob Herring , devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-stm32@st-md-mailman.stormreply.com Subject: [RFC PATCH v3 3/4] ARM: dts: stm32: add pin map for CAN controller on stm32f4 Date: Sun, 28 Aug 2022 15:33:28 +0200 Message-Id: <20220828133329.793324-4-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220828133329.793324-1-dario.binacchi@amarulasolutions.com> References: <20220828133329.793324-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=ptcQCXsH; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Add pin configurations for using CAN controller on stm32f469-disco board. They are located on the Arduino compatible connector CN5 (CAN1) and on the extension connector CN12 (CAN2). Signed-off-by: Dario Binacchi --- Changes in v3: - Remove 'Dario Binacchi ' SOB. - Remove a blank line. Changes in v2: - Remove a blank line. arch/arm/boot/dts/stm32f4-pinctrl.dtsi | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/arch/arm/boot/dts/stm32f4-pinctrl.dtsi b/arch/arm/boot/dts/stm32f4-pinctrl.dtsi index 500bcc302d42..8a4d51f97248 100644 --- a/arch/arm/boot/dts/stm32f4-pinctrl.dtsi +++ b/arch/arm/boot/dts/stm32f4-pinctrl.dtsi @@ -448,6 +448,36 @@ pins2 { slew-rate = <2>; }; }; + + can1_pins_a: can1-0 { + pins1 { + pinmux = ; /* CAN1_TX */ + }; + pins2 { + pinmux = ; /* CAN1_RX */ + bias-pull-up; + }; + }; + + can2_pins_a: can2-0 { + pins1 { + pinmux = ; /* CAN2_TX */ + }; + pins2 { + pinmux = ; /* CAN2_RX */ + bias-pull-up; + }; + }; + + can2_pins_b: can2-1 { + pins1 { + pinmux = ; /* CAN2_TX */ + }; + pins2 { + pinmux = ; /* CAN2_RX */ + bias-pull-up; + }; + }; }; }; }; From patchwork Sun Aug 28 13:33:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2317 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 48C9A40F47 for ; Sun, 28 Aug 2022 15:33:46 +0200 (CEST) Received: by mail-ed1-f72.google.com with SMTP id r11-20020a05640251cb00b004484ec7e3a4sf1188784edd.8 for ; Sun, 28 Aug 2022 06:33:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1661693626; cv=pass; d=google.com; s=arc-20160816; b=nkYjUkLhx4Y4nUyuABFVC7pV73Rd25cEykqi3YlFqpyAtN911OVQmt3lKEvSRMusTa eZKu/Mc5R3oOCctpmt2Tjwl8pmJHTZsn/iVcPqnPG4orFP79nwe07ZZnqwVa+g7fauBl ry9jSqVCCXTAU/KgjRdoZ3wS1uhutzlcX5knVP68fCcgNzLFgc8ItHNCkL2VcnNP7vUt AHZvTb/kTox+XvTWj20G5MMSVDyRY1DVd9wu3inyBP6mDPnINL1XxpoMEEhwbiIJBokV zEpYJgykmwEhRKM6QgQi3QTA9NVOcwpb+MVa08zNMVDj0+C+NJX8AZImpVHTSO8TRqqc luXA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=uvbP67EDV4b4mEJ4gy/KCqsSL1FZWbQ8SAI5wO4F2q0=; b=oOTy+vvr5q0WvBs2jmgq5L+ZAoMmX79WfZB4ESIio0bnKc46RnkgCh4fxRUsIvB3Gk hps7hJn2aWeQr84hL/dh8X2ga0fmdxUlFo9idbokuCf1akOwUFIKGBZ78g2tQVtu6AXb 6Hgn+oT8+0JOtRcJuaIY8D/H3ukOSQguF4ItMFBhPWylbrYDxkONhXR4oS3DBrfZVtSY t8p/4f0GOi7y0Ifxla0b5yIpZPrIYzDuMfWVWYaT6eJ9VHBolvb/7pl8xPbyd1fCCpAN Db5vdxNi+BnOdyC0K6ojNBEG88PcnTbyGKZMrnWD2+5OHDiI9JMkp4O3oKL1nuLmVXej Ap6g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=Yf1WssKo; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:from:to:cc; bh=uvbP67EDV4b4mEJ4gy/KCqsSL1FZWbQ8SAI5wO4F2q0=; b=dPaJ+uHFPrDxT5ezbfNU/ah5nYlkQQzDrwXwXjgNALW7ZNRThSR7UWAsTuaopV11Vk ukuIcOhufWA/oCC0DwatWHCRsJwG9ukurYfjZtZiVwL7aZ6nBnfbyJbBiS7D4Py0GgCv K3go9SDJLoi37O7itgI108LmyJkNbgP13evJ4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=list-unsubscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc; bh=uvbP67EDV4b4mEJ4gy/KCqsSL1FZWbQ8SAI5wO4F2q0=; b=5Gk8BqRMNA0iTwQUyosbuvI2CEgkWILomBPCTDmSDNxwOMbU9iRcX3KX8zKOglJeRW WDYrAyQL3r55edHh039x1Bfg+UIfsAWD4//KoA3AHUjKhGda45zoJrWk/UoCDWPeLrNL 0kH5o7SMkWzJ2RW7mZWKBJxiReAgbQSv6fcORLYg31ZoXwyxDrCwjCuBHgMVNxK3t973 IvkDDt4khyNNdMkM0Yu0LgUukhpI8HP5xkoRmTkspvyP34JINIIEmQm0rG8JjW3rkZM8 J9G3IDMdLjj/iOjUG7OkCuA53x3hldrIB3l1B4EKNdBJeUfFnjxpudHhF/HRMY2hoEU/ XziA== X-Gm-Message-State: ACgBeo2QW0BMklmvE7GiG9y1FARSe03GFWJvfxVI291mUbRfo9WdbNUa 0e7PP7gDHtwscNFoyBuMX7m06NYT X-Google-Smtp-Source: AA6agR68BYnRfAlzt4lQ6LKEyM5L/QxSkEkvsJnfOIUP3AlNIY7lVBtBbw4Aqk/J+JqY3zjVn4nhHg== X-Received: by 2002:a17:907:3e90:b0:73c:fdd2:3904 with SMTP id hs16-20020a1709073e9000b0073cfdd23904mr10520526ejc.455.1661693625903; Sun, 28 Aug 2022 06:33:45 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:6402:4408:b0:43d:b3c4:cd21 with SMTP id y8-20020a056402440800b0043db3c4cd21ls8171683eda.2.-pod-prod-gmail; Sun, 28 Aug 2022 06:33:44 -0700 (PDT) X-Received: by 2002:a05:6402:2756:b0:443:4a6d:f05a with SMTP id z22-20020a056402275600b004434a6df05amr13314521edd.396.1661693624591; Sun, 28 Aug 2022 06:33:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661693624; cv=none; d=google.com; s=arc-20160816; b=XbxRLRTiY2MErqlc+FisOXVuCEh9IDkUu8eCrjwn+T9x0qzCHhK5rHNQ/y+ntuXEhC PQYAck2vrlZ+1n+yXEbl4T5Po+KpvfJ9uP6zdRzIcAjxFJi1AZEC/Q6uQpTT1Kn/Z8c0 5lbtWVpRgjYZ4FBc6XxXbPtBYKUXWzA/sXZ1+pLMOk4P0FPx2gK/mCyolbWSqJAsQAfz CObCKvS3ZZjb+BnhtIST8PUW6JT3ARm/5fd9c33TznLECEx/MA75qtg3INFaN3UIxipw ZuqU9w4f8wppSnIAGSkx/3b6LSJImc9q40KIrc7o6HEqOliGnm1p3QpQY70D5qi9XXR2 iRpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=5H8sdvtmhWZcDHoOxrgCqHRgQ8WlFmxVwoPgrCaYZbY=; b=m14acJ2247HYp2krrAK/DReEcVfBN2z1669Djjim8VWWMMY3jsQXx6gcLpNM1aJaMA MHmXqJls4gZbZKR34/DAvQJhomm79Mi1VETaSarJY3OjeY3QlbxyIPql2Cd1ZacXbhrP z4QW71eF7rr2BQXfHrWD1WY7Y4FFCDrQHqgBUem4Bnd4egM+R5gXI9//+Tu06uibX0bq uzeoauOD1BzFd+QGKEwZTBoiCIUtnreglG5mcDPYULatpiXOeltj+Hg2xzBLOaV5KrnN wEUro+qJ8df981LqSXFE57E5nBD8Kq7uo4+7tiptq9yzk1wTCSBF/YK4xkKx6uAVIkb8 L1dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=Yf1WssKo; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id dk13-20020a170906f0cd00b0073d84b10685sor2288975ejb.87.2022.08.28.06.33.44 for (Google Transport Security); Sun, 28 Aug 2022 06:33:44 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a17:907:2ccd:b0:741:4343:80d1 with SMTP id hg13-20020a1709072ccd00b00741434380d1mr4549272ejc.572.1661693624164; Sun, 28 Aug 2022 06:33:44 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-79-31-31-9.retail.telecomitalia.it. [79.31.31.9]) by smtp.gmail.com with ESMTPSA id u26-20020a1709064ada00b007313a25e56esm3247669ejt.29.2022.08.28.06.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Aug 2022 06:33:43 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Marc Kleine-Budde , Alexandre Torgue , michael@amarulasolutions.com, Amarula patchwork , Vincent Mailhol , Krzysztof Kozlowski , Rob Herring , Dario Binacchi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [RFC PATCH v3 4/4] can: bxcan: add support for ST bxCAN controller Date: Sun, 28 Aug 2022 15:33:29 +0200 Message-Id: <20220828133329.793324-5-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220828133329.793324-1-dario.binacchi@amarulasolutions.com> References: <20220828133329.793324-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=Yf1WssKo; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Add support for the basic extended CAN controller (bxCAN) found in many low- to middle-end STM32 SoCs. It supports the Basic Extended CAN protocol versions 2.0A and B with a maximum bit rate of 1 Mbit/s. The controller supports two channels (CAN1 as master and CAN2 as slave) and the driver can enable either or both of the channels. They share some of the required logic (e. g. clocks and filters), and that means you cannot use the slave CAN without enabling some hardware resources managed by the master CAN. Each channel has 3 transmit mailboxes, 2 receive FIFOs with 3 stages and 28 scalable filter banks. It also manages 4 dedicated interrupt vectors: - transmit interrupt - FIFO 0 receive interrupt - FIFO 1 receive interrupt - status change error interrupt Driver uses all 3 available mailboxes for transmission and FIFO 0 for reception. Rx filter rules are configured to the minimum. They accept all messages and assign filter 0 to CAN1 and filter 14 to CAN2 in identifier mask mode with 32 bits width. It enables and uses transmit, receive buffers for FIFO 0 and error and status change interrupts. Signed-off-by: Dario Binacchi --- Changes in v3: - Remove 'Dario Binacchi ' SOB. - Fix the documentation file path in the MAINTAINERS entry. - Do not increment the "stats->rx_bytes" if the frame is remote. - Remove pr_debug() call from bxcan_rmw(). Changes in v2: - Fix sparse errors. - Create a MAINTAINERS entry. - Remove the print of the registers address. - Remove the volatile keyword from bxcan_rmw(). - Use tx ring algorithm to manage tx mailboxes. - Use can_{get|put}_echo_skb(). - Update DT properties. MAINTAINERS | 7 + drivers/net/can/Kconfig | 1 + drivers/net/can/Makefile | 1 + drivers/net/can/bxcan/Kconfig | 34 + drivers/net/can/bxcan/Makefile | 4 + drivers/net/can/bxcan/bxcan-core.c | 200 ++++++ drivers/net/can/bxcan/bxcan-core.h | 31 + drivers/net/can/bxcan/bxcan-drv.c | 1045 ++++++++++++++++++++++++++++ 8 files changed, 1323 insertions(+) create mode 100644 drivers/net/can/bxcan/Kconfig create mode 100644 drivers/net/can/bxcan/Makefile create mode 100644 drivers/net/can/bxcan/bxcan-core.c create mode 100644 drivers/net/can/bxcan/bxcan-core.h create mode 100644 drivers/net/can/bxcan/bxcan-drv.c diff --git a/MAINTAINERS b/MAINTAINERS index 07403024f3f2..609327f5cb35 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4461,6 +4461,13 @@ S: Maintained F: drivers/scsi/BusLogic.* F: drivers/scsi/FlashPoint.* +BXCAN CAN NETWORK DRIVER +M: Dario Binacchi +L: linux-can@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/net/can/st,stm32-bxcan.yaml +F: drivers/net/can/bxcan/ + C-MEDIA CMI8788 DRIVER M: Clemens Ladisch L: alsa-devel@alsa-project.org (moderated for non-subscribers) diff --git a/drivers/net/can/Kconfig b/drivers/net/can/Kconfig index 3048ad77edb3..d55355a0e583 100644 --- a/drivers/net/can/Kconfig +++ b/drivers/net/can/Kconfig @@ -206,6 +206,7 @@ config PCH_CAN is an IOH for x86 embedded processor (Intel Atom E6xx series). This driver can access CAN bus. +source "drivers/net/can/bxcan/Kconfig" source "drivers/net/can/c_can/Kconfig" source "drivers/net/can/cc770/Kconfig" source "drivers/net/can/ctucanfd/Kconfig" diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile index 61c75ce9d500..373f2c99689a 100644 --- a/drivers/net/can/Makefile +++ b/drivers/net/can/Makefile @@ -14,6 +14,7 @@ obj-y += usb/ obj-y += softing/ obj-$(CONFIG_CAN_AT91) += at91_can.o +obj-$(CONFIG_CAN_BXCAN) += bxcan/ obj-$(CONFIG_CAN_CAN327) += can327.o obj-$(CONFIG_CAN_CC770) += cc770/ obj-$(CONFIG_CAN_C_CAN) += c_can/ diff --git a/drivers/net/can/bxcan/Kconfig b/drivers/net/can/bxcan/Kconfig new file mode 100644 index 000000000000..df34c212bf51 --- /dev/null +++ b/drivers/net/can/bxcan/Kconfig @@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# bxCAN driver configuration +# +menuconfig CAN_BXCAN + tristate "STMicroelectronics STM32 Basic Extended CAN (bxCAN) devices" + depends on ARCH_STM32 || COMPILE_TEST + depends on OF + depends on HAS_IOMEM + help + Say Y here if you want support for ST bxCAN controller framework. + This is common support for devices that embed the ST bxCAN IP. + +if CAN_BXCAN + +config CAN_BXCAN_CORE + tristate "STMicroelectronics STM32 bxCAN core" + help + Select this option to enable the core driver for STMicroelectronics + STM32 basic extended CAN controller (bxCAN). + + This driver can also be built as a module. If so, the module + will be called bxcan-core. + +config CAN_BXCAN_DRV + tristate "STMicroelectronics STM32 bxCAN driver" + depends on CAN_BXCAN_CORE + help + Say yes here to build support for the STMicroelectronics STM32 basic + extended CAN Controller (bxCAN). + + This driver can also be built as a module. If so, the module + will be called bxcan-drv. +endif diff --git a/drivers/net/can/bxcan/Makefile b/drivers/net/can/bxcan/Makefile new file mode 100644 index 000000000000..60350f055271 --- /dev/null +++ b/drivers/net/can/bxcan/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_CAN_BXCAN_CORE) += bxcan-core.o +obj-$(CONFIG_CAN_BXCAN_DRV) += bxcan-drv.o diff --git a/drivers/net/can/bxcan/bxcan-core.c b/drivers/net/can/bxcan/bxcan-core.c new file mode 100644 index 000000000000..3644449095e9 --- /dev/null +++ b/drivers/net/can/bxcan/bxcan-core.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0 +/* bxcan-core.c - STM32 Basic Extended CAN core controller driver + * + * This file is part of STM32 bxcan driver + * + * Copyright (c) 2022 Dario Binacchi + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include + +#include "bxcan-core.h" + +#define BXCAN_FILTER_ID(master) (master ? 0 : 14) + +/* Filter master register (FMR) bits */ +#define BXCAN_FMR_CANSB_MASK GENMASK(13, 8) +#define BXCAN_FMR_CANSB(x) (((x) & 0x3f) << 8) +#define BXCAN_FMR_FINIT BIT(0) + +/* Structure of the filter bank */ +struct bxcan_fb { + u32 fr1; /* filter 1 */ + u32 fr2; /* filter 2 */ +}; + +/* Structure of the hardware filter registers */ +struct bxcan_fregs { + u32 fmr; /* 0x00 - filter master */ + u32 fm1r; /* 0x04 - filter mode */ + u32 reserved2; /* 0x08 */ + u32 fs1r; /* 0x0c - filter scale */ + u32 reserved3; /* 0x10 */ + u32 ffa1r; /* 0x14 - filter FIFO assignment */ + u32 reserved4; /* 0x18 */ + u32 fa1r; /* 0x1c - filter activation */ + u32 reserved5[8]; /* 0x20 */ + struct bxcan_fb fb[28]; /* 0x40 - filter banks */ +}; + +struct bxcan_core_priv { + void __iomem *base; + struct bxcan_fregs __iomem *fregs; + struct clk *clk_master; + unsigned int clk_master_ref; +}; + +void bxcan_disable_filters(struct device *dev, bool master) +{ + struct bxcan_core_priv *priv = dev_get_drvdata(dev); + unsigned int fid = BXCAN_FILTER_ID(master); + u32 fmask = BIT(fid); + + bxcan_rmw(&priv->fregs->fa1r, fmask, 0); +} + +void bxcan_enable_filters(struct device *dev, bool master) +{ + struct bxcan_core_priv *priv = dev_get_drvdata(dev); + unsigned int fid = BXCAN_FILTER_ID(master); + u32 fmask = BIT(fid); + + /* Filter settings: + * + * Accept all messages. + * Assign filter 0 to CAN1 and filter 14 to CAN2 in identifier + * mask mode with 32 bits width. + */ + + /* Enter filter initialization mode and assing filters to CAN + * controllers. + */ + bxcan_rmw(&priv->fregs->fmr, BXCAN_FMR_CANSB_MASK, + BXCAN_FMR_CANSB(14) | BXCAN_FMR_FINIT); + + /* Deactivate filter */ + bxcan_rmw(&priv->fregs->fa1r, fmask, 0); + + /* Two 32-bit registers in identifier mask mode */ + bxcan_rmw(&priv->fregs->fm1r, fmask, 0); + + /* Single 32-bit scale configuration */ + bxcan_rmw(&priv->fregs->fs1r, 0, fmask); + + /* Assign filter to FIFO 0 */ + bxcan_rmw(&priv->fregs->ffa1r, fmask, 0); + + /* Accept all messages */ + writel(0, &priv->fregs->fb[fid].fr1); + writel(0, &priv->fregs->fb[fid].fr2); + + /* Activate filter */ + bxcan_rmw(&priv->fregs->fa1r, 0, fmask); + + /* Exit filter initialization mode */ + bxcan_rmw(&priv->fregs->fmr, BXCAN_FMR_FINIT, 0); +} + +int bxcan_enable_master_clk(struct device *dev) +{ + struct bxcan_core_priv *priv = dev_get_drvdata(dev); + int err; + + if (priv->clk_master_ref == 0) { + err = clk_prepare_enable(priv->clk_master); + if (err) + return err; + } + + priv->clk_master_ref++; + return 0; +} + +void bxcan_disable_master_clk(struct device *dev) +{ + struct bxcan_core_priv *priv = dev_get_drvdata(dev); + + if (priv->clk_master_ref == 0) + return; + + if (priv->clk_master_ref == 1) + clk_disable_unprepare(priv->clk_master); + + priv->clk_master_ref--; +} + +unsigned long bxcan_get_master_clk_rate(struct device *dev) +{ + struct bxcan_core_priv *priv = dev_get_drvdata(dev); + + return clk_get_rate(priv->clk_master); +} + +void __iomem *bxcan_get_base_addr(struct device *dev) +{ + struct bxcan_core_priv *priv = dev_get_drvdata(dev); + + return priv->base; +} + +static const struct of_device_id bxcan_core_of_match[] = { + {.compatible = "st,stm32f4-bxcan-core"}, + { /* sentinel */ }, +}; + +MODULE_DEVICE_TABLE(of, bxcan_core_of_match); + +static int bxcan_core_probe(struct platform_device *pdev) +{ + struct bxcan_core_priv *priv; + struct device *dev = &pdev->dev; + struct device_node *np = pdev->dev.of_node; + void __iomem *regs; + struct clk *clk; + int ret; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + platform_set_drvdata(pdev, priv); + regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(regs)) + return PTR_ERR(regs); + + clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(clk)) { + dev_err(dev, "failed to get clock\n"); + return PTR_ERR(clk); + } + + priv->base = regs; + priv->fregs = regs + 0x200; + priv->clk_master = clk; + + ret = of_platform_populate(np, NULL, NULL, dev); + if (ret < 0) { + dev_err(dev, "failed to populate DT children\n"); + return ret; + } + + return 0; +} + +static struct platform_driver bxcan_core_driver = { + .driver = { + .name = KBUILD_MODNAME, + .of_match_table = bxcan_core_of_match, + }, + .probe = bxcan_core_probe, +}; + +module_platform_driver(bxcan_core_driver); + +MODULE_AUTHOR("Dario Binacchi "); +MODULE_DESCRIPTION("STMicroelectronics Basic Extended CAN core driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/net/can/bxcan/bxcan-core.h b/drivers/net/can/bxcan/bxcan-core.h new file mode 100644 index 000000000000..925449cbc3bd --- /dev/null +++ b/drivers/net/can/bxcan/bxcan-core.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * bxcan-core - STM32 Basic Extended CAN core controller driver + * + * Copyright (c) 2022 Dario Binacchi + */ + +#ifndef __BXCAN_CORE_H +#define __BXCAN_CORE_H + +#include +#include + +int bxcan_enable_master_clk(struct device *dev); +void bxcan_disable_master_clk(struct device *dev); +unsigned long bxcan_get_master_clk_rate(struct device *dev); +void __iomem *bxcan_get_base_addr(struct device *dev); +void bxcan_enable_filters(struct device *dev, bool master); +void bxcan_disable_filters(struct device *dev, bool master); + +static inline void bxcan_rmw(void __iomem *addr, u32 clear, u32 set) +{ + u32 old, val; + + old = readl(addr); + val = (old & ~clear) | set; + if (val != old) + writel(val, addr); +} + +#endif diff --git a/drivers/net/can/bxcan/bxcan-drv.c b/drivers/net/can/bxcan/bxcan-drv.c new file mode 100644 index 000000000000..7ac1ee7a4269 --- /dev/null +++ b/drivers/net/can/bxcan/bxcan-drv.c @@ -0,0 +1,1045 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// bxcan-drv.c - STM32 Basic Extended CAN controller driver +// +// Copyright (c) 2022 Dario Binacchi +// + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bxcan-core.h" + +#define BXCAN_NAPI_WEIGHT 3 +#define BXCAN_TIMEOUT_US 10000 + +#define BXCAN_TX_MB_NUM 3 + +/* Master control register (MCR) bits */ +#define BXCAN_MCR_DBF BIT(16) +#define BXCAN_MCR_RESET BIT(15) +#define BXCAN_MCR_TTCM BIT(7) +#define BXCAN_MCR_ABOM BIT(6) +#define BXCAN_MCR_AWUM BIT(5) +#define BXCAN_MCR_NART BIT(4) +#define BXCAN_MCR_RFLM BIT(3) +#define BXCAN_MCR_TXFP BIT(2) +#define BXCAN_MCR_SLEEP BIT(1) +#define BXCAN_MCR_INRQ BIT(0) + +/* Master status register (MSR) bits */ +#define BXCAN_MSR_RX BIT(11) +#define BXCAN_MSR_SAMP BIT(10) +#define BXCAN_MSR_RXM BIT(9) +#define BXCAN_MSR_TXM BIT(8) +#define BXCAN_MSR_SLAKI BIT(4) +#define BXCAN_MSR_WKUI BIT(3) +#define BXCAN_MSR_ERRI BIT(2) +#define BXCAN_MSR_SLAK BIT(1) +#define BXCAN_MSR_INAK BIT(0) + +/* Transmit status register (TSR) bits */ +#define BXCAN_TSR_LOW2 BIT(31) +#define BXCAN_TSR_LOW1 BIT(30) +#define BXCAN_TSR_LOW0 BIT(29) +#define BXCAN_TSR_TME GENMASK(28, 26) +#define BXCAN_TSR_TME_SHIFT (26) +#define BXCAN_TSR_TME2 BIT(28) +#define BXCAN_TSR_TME1 BIT(27) +#define BXCAN_TSR_TME0 BIT(26) +#define BXCAN_TSR_CODE GENMASK(25, 24) +#define BXCAN_TSR_ABRQ2 BIT(23) +#define BXCAN_TSR_TERR2 BIT(19) +#define BXCAN_TSR_ALST2 BIT(18) +#define BXCAN_TSR_TXOK2 BIT(17) +#define BXCAN_TSR_RQCP2 BIT(16) +#define BXCAN_TSR_ABRQ1 BIT(15) +#define BXCAN_TSR_TERR1 BIT(11) +#define BXCAN_TSR_ALST1 BIT(10) +#define BXCAN_TSR_TXOK1 BIT(9) +#define BXCAN_TSR_RQCP1 BIT(8) +#define BXCAN_TSR_ABRQ0 BIT(7) +#define BXCAN_TSR_TERR0 BIT(3) +#define BXCAN_TSR_ALST0 BIT(2) +#define BXCAN_TSR_TXOK0 BIT(1) +#define BXCAN_TSR_RQCP0 BIT(0) + +/* Receive FIFO 0 register (RF0R) bits */ +#define BXCAN_RF0R_RFOM0 BIT(5) +#define BXCAN_RF0R_FOVR0 BIT(4) +#define BXCAN_RF0R_FULL0 BIT(3) +#define BXCAN_RF0R_FMP0 GENMASK(1, 0) + +/* Interrupt enable register (IER) bits */ +#define BXCAN_IER_SLKIE BIT(17) +#define BXCAN_IER_WKUIE BIT(16) +#define BXCAN_IER_ERRIE BIT(15) +#define BXCAN_IER_LECIE BIT(11) +#define BXCAN_IER_BOFIE BIT(10) +#define BXCAN_IER_EPVIE BIT(9) +#define BXCAN_IER_EWGIE BIT(8) +#define BXCAN_IER_FOVIE1 BIT(6) +#define BXCAN_IER_FFIE1 BIT(5) +#define BXCAN_IER_FMPIE1 BIT(4) +#define BXCAN_IER_FOVIE0 BIT(3) +#define BXCAN_IER_FFIE0 BIT(2) +#define BXCAN_IER_FMPIE0 BIT(1) +#define BXCAN_IER_TMEIE BIT(0) + +/* Error status register (ESR) bits */ +#define BXCAN_ESR_REC_SHIFT (24) +#define BXCAN_ESR_REC GENMASK(31, 24) +#define BXCAN_ESR_TEC_SHIFT (16) +#define BXCAN_ESR_TEC GENMASK(23, 16) +#define BXCAN_ESR_LEC_SHIFT (4) +#define BXCAN_ESR_LEC GENMASK(6, 4) +#define BXCAN_ESR_BOFF BIT(1) +#define BXCAN_ESR_EPVF BIT(1) +#define BXCAN_ESR_EWGF BIT(0) +#define BXCAN_TEC(esr) (((esr) & BXCAN_ESR_TEC) >> \ + BXCAN_ESR_TEC_SHIFT) +#define BXCAN_REC(esr) (((esr) & BXCAN_ESR_REC) >> \ + BXCAN_ESR_REC_SHIFT) + +/* Bit timing register (BTR) bits */ +#define BXCAN_BTR_SILM BIT(31) +#define BXCAN_BTR_LBKM BIT(30) +#define BXCAN_BTR_SJW_MASK GENMASK(25, 24) +#define BXCAN_BTR_SJW(x) (((x) & 0x03) << 24) +#define BXCAN_BTR_TS2_MASK GENMASK(22, 20) +#define BXCAN_BTR_TS2(x) (((x) & 0x07) << 20) +#define BXCAN_BTR_TS1_MASK GENMASK(19, 16) +#define BXCAN_BTR_TS1(x) (((x) & 0x0f) << 16) +#define BXCAN_BTR_BRP_MASK GENMASK(9, 0) +#define BXCAN_BTR_BRP(x) ((x) & 0x3ff) + +/* TX mailbox identifier register (TIxR, x = 0..2) bits */ +#define BXCAN_TIxR_STID(x) (((x) & 0x7ff) << 21) +#define BXCAN_TIxR_EXID(x) ((x) << 3) +#define BXCAN_TIxR_IDE BIT(2) +#define BXCAN_TIxR_RTR BIT(1) +#define BXCAN_TIxR_TXRQ BIT(0) + +/* TX mailbox data length and time stamp register (TDTxR, x = 0..2 bits */ +#define BXCAN_TDTxR_TIME(x) (((x) & 0x0f) << 16) +#define BXCAN_TDTxR_TGT BIT(8) +#define BXCAN_TDTxR_DLC_MASK GENMASK(3, 0) +#define BXCAN_TDTxR_DLC(x) ((x) & 0x0f) + +/* RX FIFO mailbox identifier register (RIxR, x = 0..1 */ +#define BXCAN_RIxR_STID_SHIFT (21) +#define BXCAN_RIxR_EXID_SHIFT (3) +#define BXCAN_RIxR_IDE BIT(2) +#define BXCAN_RIxR_RTR BIT(1) + +/* RX FIFO mailbox data length and timestamp register (RDTxR, x = 0..1) bits */ +#define BXCAN_RDTxR_TIME GENMASK(31, 16) +#define BXCAN_RDTxR_FMI GENMASK(15, 8) +#define BXCAN_RDTxR_DLC GENMASK(3, 0) + +enum bxcan_lec_code { + LEC_NO_ERROR = 0, + LEC_STUFF_ERROR, + LEC_FORM_ERROR, + LEC_ACK_ERROR, + LEC_BIT1_ERROR, + LEC_BIT0_ERROR, + LEC_CRC_ERROR, + LEC_UNUSED +}; + +/* Structure of the message buffer */ +struct bxcan_mb { + u32 id; /* can identifier */ + u32 dlc; /* data length control and timestamp */ + u32 data[2]; /* data */ +}; + +/* Structure of the hardware registers */ +struct bxcan_regs { + u32 mcr; /* 0x00 - master control */ + u32 msr; /* 0x04 - master status */ + u32 tsr; /* 0x08 - transmit status */ + u32 rf0r; /* 0x0c - FIFO 0 */ + u32 rf1r; /* 0x10 - FIFO 1 */ + u32 ier; /* 0x14 - interrupt enable */ + u32 esr; /* 0x18 - error status */ + u32 btr; /* 0x1c - bit timing*/ + u32 reserved0[88]; /* 0x20 */ + struct bxcan_mb tx_mb[BXCAN_TX_MB_NUM]; /* 0x180 - tx mailbox */ + struct bxcan_mb rx_mb[2]; /* 0x1b0 - rx mailbox */ +}; + +struct bxcan_priv { + struct can_priv can; + struct device *dev; + struct net_device *ndev; + struct napi_struct napi; + + struct bxcan_regs __iomem *regs; + int tx_irq; + int sce_irq; + u8 tx_dlc[BXCAN_TX_MB_NUM]; + bool master; + struct clk *clk; + unsigned int tx_head; + unsigned int tx_tail; +}; + +static const struct can_bittiming_const bxcan_bittiming_const = { + .name = KBUILD_MODNAME, + .tseg1_min = 1, + .tseg1_max = 16, + .tseg2_min = 1, + .tseg2_max = 8, + .sjw_max = 4, + .brp_min = 1, + .brp_max = 1024, + .brp_inc = 1, +}; + +static inline u8 bxcan_get_tx_head(const struct bxcan_priv *priv) +{ + return priv->tx_head % BXCAN_TX_MB_NUM; +} + +static inline u8 bxcan_get_tx_tail(const struct bxcan_priv *priv) +{ + return priv->tx_tail % BXCAN_TX_MB_NUM; +} + +static inline u8 bxcan_get_tx_free(const struct bxcan_priv *priv) +{ + return BXCAN_TX_MB_NUM - (priv->tx_head - priv->tx_tail); +} + +static bool bxcan_tx_busy(const struct bxcan_priv *priv) +{ + if (bxcan_get_tx_free(priv) > 0) + return false; + + netif_stop_queue(priv->ndev); + + /* Memory barrier before checking tx_free (head and tail) */ + smp_mb(); + + if (bxcan_get_tx_free(priv) == 0) { + netdev_dbg(priv->ndev, + "Stopping tx-queue (tx_head=0x%08x, tx_tail=0x%08x, len=%d).\n", + priv->tx_head, priv->tx_tail, + priv->tx_head - priv->tx_tail); + + return true; + } + + netif_start_queue(priv->ndev); + + return false; +} + +static int bxcan_chip_softreset(struct bxcan_priv *priv) +{ + struct bxcan_regs __iomem *regs = priv->regs; + unsigned int timeout = BXCAN_TIMEOUT_US / 10; + + bxcan_rmw(®s->mcr, 0, BXCAN_MCR_RESET); + while (timeout-- && !(readl(®s->msr) & BXCAN_MSR_SLAK)) + udelay(10); + + if (!(readl(®s->msr) & BXCAN_MSR_SLAK)) + return -ETIMEDOUT; + + return 0; +} + +static int bxcan_enter_init_mode(struct bxcan_priv *priv) +{ + struct bxcan_regs __iomem *regs = priv->regs; + unsigned int timeout = BXCAN_TIMEOUT_US / 10; + + bxcan_rmw(®s->mcr, 0, BXCAN_MCR_INRQ); + while (timeout-- && !(readl(®s->msr) & BXCAN_MSR_INAK)) + udelay(100); + + if (!(readl(®s->msr) & BXCAN_MSR_INAK)) + return -ETIMEDOUT; + + return 0; +} + +static int bxcan_leave_init_mode(struct bxcan_priv *priv) +{ + struct bxcan_regs __iomem *regs = priv->regs; + unsigned int timeout = BXCAN_TIMEOUT_US / 10; + + bxcan_rmw(®s->mcr, BXCAN_MCR_INRQ, 0); + while (timeout-- && (readl(®s->msr) & BXCAN_MSR_INAK)) + udelay(100); + + if (readl(®s->msr) & BXCAN_MSR_INAK) + return -ETIMEDOUT; + + return 0; +} + +static int bxcan_leave_sleep_mode(struct bxcan_priv *priv) +{ + struct bxcan_regs __iomem *regs = priv->regs; + unsigned int timeout = BXCAN_TIMEOUT_US / 10; + + bxcan_rmw(®s->mcr, BXCAN_MCR_SLEEP, 0); + while (timeout-- && (readl(®s->msr) & BXCAN_MSR_SLAK)) + udelay(100); + + if (readl(®s->msr) & BXCAN_MSR_SLAK) + return -ETIMEDOUT; + + return 0; +} + +static int bxcan_enter_sleep_mode(struct bxcan_priv *priv) +{ + struct bxcan_regs __iomem *regs = priv->regs; + unsigned int timeout = BXCAN_TIMEOUT_US / 10; + + bxcan_rmw(®s->mcr, 0, BXCAN_MCR_SLEEP); + while (timeout-- && !(readl(®s->msr) & BXCAN_MSR_SLAK)) + udelay(100); + + if (!(readl(®s->msr) & BXCAN_MSR_SLAK)) + return -ETIMEDOUT; + + return 0; +} + +static irqreturn_t bxcan_rx_isr(int irq, void *dev_id) +{ + struct net_device *ndev = dev_id; + struct bxcan_priv *priv = netdev_priv(ndev); + struct bxcan_regs __iomem *regs = priv->regs; + + if (napi_schedule_prep(&priv->napi)) { + /* Disable Rx FIFO message pending interrupt */ + bxcan_rmw(®s->ier, BXCAN_IER_FMPIE0, 0); + __napi_schedule(&priv->napi); + } + + return IRQ_HANDLED; +} + +static irqreturn_t bxcan_tx_isr(int irq, void *dev_id) +{ + struct net_device *ndev = dev_id; + struct bxcan_priv *priv = netdev_priv(ndev); + struct bxcan_regs __iomem *regs = priv->regs; + struct net_device_stats *stats = &ndev->stats; + u32 tsr, rqcp_bit, bytes = 0, pkts = 0; + int n, idx; + + tsr = readl(®s->tsr); + for (n = 0, idx = bxcan_get_tx_tail(priv); n < BXCAN_TX_MB_NUM; n++) { + rqcp_bit = BXCAN_TSR_RQCP0 << (idx << 3); + if (tsr & rqcp_bit) { + pkts++; + bytes += can_get_echo_skb(ndev, idx, NULL); + } + + idx += 1; + if (idx == BXCAN_TX_MB_NUM) + idx = 0; + } + + if (!pkts) + return IRQ_HANDLED; + + writel(tsr, ®s->tsr); + + priv->tx_tail += pkts; + if (bxcan_get_tx_free(priv)) { + /* Make sure that anybody stopping the queue after + * this sees the new tx_ring->tail. + */ + smp_mb(); + netif_wake_queue(ndev); + } + + stats->tx_bytes += bytes; + stats->tx_packets += pkts; + + return IRQ_HANDLED; +} + +static void bxcan_handle_state_change(struct net_device *ndev, u32 esr) +{ + struct bxcan_priv *priv = netdev_priv(ndev); + struct net_device_stats *stats = &ndev->stats; + enum can_state new_state = priv->can.state; + struct can_berr_counter bec; + enum can_state rx_state, tx_state; + struct sk_buff *skb; + struct can_frame *cf; + + /* Early exit if no error flag is set */ + if (!(esr & (BXCAN_ESR_EWGF | BXCAN_ESR_EPVF | BXCAN_ESR_BOFF))) + return; + + bec.txerr = BXCAN_TEC(esr); + bec.rxerr = BXCAN_REC(esr); + + if (esr & BXCAN_ESR_BOFF) + new_state = CAN_STATE_BUS_OFF; + else if (esr & BXCAN_ESR_EPVF) + new_state = CAN_STATE_ERROR_PASSIVE; + else if (esr & BXCAN_ESR_EWGF) + new_state = CAN_STATE_ERROR_WARNING; + + /* state hasn't changed */ + if (unlikely(new_state == priv->can.state)) + return; + + skb = alloc_can_err_skb(ndev, &cf); + if (unlikely(!skb)) + return; + + tx_state = bec.txerr >= bec.rxerr ? new_state : 0; + rx_state = bec.txerr <= bec.rxerr ? new_state : 0; + can_change_state(ndev, cf, tx_state, rx_state); + + if (new_state == CAN_STATE_BUS_OFF) + can_bus_off(ndev); + + stats->rx_bytes += cf->len; + stats->rx_packets++; + netif_rx(skb); +} + +static void bxcan_handle_bus_err(struct net_device *ndev, u32 esr) +{ + struct bxcan_priv *priv = netdev_priv(ndev); + enum bxcan_lec_code lec_code; + struct can_frame *cf; + struct sk_buff *skb; + + lec_code = (esr & BXCAN_ESR_LEC_SHIFT) >> BXCAN_ESR_LEC_SHIFT; + + /* Early exit if no lec update or no error. + * No lec update means that no CAN bus event has been detected + * since CPU wrote LEC_UNUSED value to status reg. + */ + if (lec_code == LEC_UNUSED || lec_code == LEC_NO_ERROR) + return; + + if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) + return; + + /* Common for all type of bus errors */ + priv->can.can_stats.bus_error++; + + /* Propagate the error condition to the CAN stack */ + skb = alloc_can_err_skb(ndev, &cf); + if (unlikely(!skb)) + return; + + ndev->stats.rx_bytes += cf->len; + + /* Check for 'last error code' which tells us the + * type of the last error to occur on the CAN bus + */ + cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; + + switch (lec_code) { + case LEC_STUFF_ERROR: + netdev_dbg(ndev, "Stuff error\n"); + ndev->stats.rx_errors++; + cf->data[2] |= CAN_ERR_PROT_STUFF; + break; + case LEC_FORM_ERROR: + netdev_dbg(ndev, "Form error\n"); + ndev->stats.rx_errors++; + cf->data[2] |= CAN_ERR_PROT_FORM; + break; + case LEC_ACK_ERROR: + netdev_dbg(ndev, "Ack error\n"); + ndev->stats.tx_errors++; + cf->data[3] = CAN_ERR_PROT_LOC_ACK; + break; + case LEC_BIT1_ERROR: + netdev_dbg(ndev, "Bit error (recessive)\n"); + ndev->stats.tx_errors++; + cf->data[2] |= CAN_ERR_PROT_BIT1; + break; + case LEC_BIT0_ERROR: + netdev_dbg(ndev, "Bit error (dominant)\n"); + ndev->stats.tx_errors++; + cf->data[2] |= CAN_ERR_PROT_BIT0; + break; + case LEC_CRC_ERROR: + netdev_dbg(ndev, "CRC error\n"); + ndev->stats.rx_errors++; + cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; + break; + default: + break; + } + + netif_rx(skb); +} + +static irqreturn_t bxcan_state_change_isr(int irq, void *dev_id) +{ + struct net_device *ndev = dev_id; + struct bxcan_priv *priv = netdev_priv(ndev); + struct bxcan_regs __iomem *regs = priv->regs; + u32 msr, esr; + + msr = readl(®s->msr); + if (!(msr & BXCAN_MSR_ERRI)) + return IRQ_NONE; + + esr = readl(®s->esr); + bxcan_handle_state_change(ndev, esr); + bxcan_handle_bus_err(ndev, esr); + + msr |= BXCAN_MSR_ERRI; + writel(msr, ®s->msr); + return IRQ_HANDLED; +} + +static int bxcan_start(struct net_device *ndev) +{ + struct bxcan_priv *priv = netdev_priv(ndev); + struct bxcan_regs __iomem *regs = priv->regs; + struct can_bittiming *bt = &priv->can.bittiming; + u32 set; + int err; + + err = bxcan_chip_softreset(priv); + if (err) { + netdev_err(ndev, "failed to reset chip, error %d\n", err); + return err; + } + + err = bxcan_leave_sleep_mode(priv); + if (err) { + netdev_err(ndev, "failed to leave sleep mode, error %d\n", err); + goto failed_leave_sleep; + } + + err = bxcan_enter_init_mode(priv); + if (err) { + netdev_err(ndev, "failed to enter init mode, error %d\n", err); + goto failed_enter_init; + } + + /* MCR + * + * select request order priority + * disable time triggered mode + * bus-off state left on sw request + * sleep mode left on sw request + * retransmit automatically on error + * do not lock RX FIFO on overrun + */ + bxcan_rmw(®s->mcr, BXCAN_MCR_TTCM | BXCAN_MCR_ABOM | BXCAN_MCR_AWUM | + BXCAN_MCR_NART | BXCAN_MCR_RFLM, BXCAN_MCR_TXFP); + + /* Bit timing register settings */ + set = BXCAN_BTR_BRP(bt->brp - 1) | + BXCAN_BTR_TS1(bt->phase_seg1 + bt->prop_seg - 1) | + BXCAN_BTR_TS2(bt->phase_seg2 - 1) | BXCAN_BTR_SJW(bt->sjw - 1); + + /* loopback + silent mode put the controller in test mode, + * useful for hot self-test + */ + if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) + set |= BXCAN_BTR_LBKM; + + if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) + set |= BXCAN_BTR_SILM; + + netdev_dbg(ndev, + "TQ[ns]: %d, PrS: %d, PhS1: %d, PhS2: %d, SJW: %d, BRP: %d, CAN_BTR: 0x%08x\n", + bt->tq, bt->prop_seg, bt->phase_seg1, bt->phase_seg2, + bt->sjw, bt->brp, set); + bxcan_rmw(®s->btr, BXCAN_BTR_SILM | BXCAN_BTR_LBKM | + BXCAN_BTR_BRP_MASK | BXCAN_BTR_TS1_MASK | BXCAN_BTR_TS2_MASK | + BXCAN_BTR_SJW_MASK, set); + + bxcan_enable_filters(priv->dev->parent, priv->master); + + /* Clear all internal status */ + priv->tx_head = 0; + priv->tx_tail = 0; + + err = bxcan_leave_init_mode(priv); + if (err) { + netdev_err(ndev, "failed to leave init mode, error %d\n", err); + goto failed_leave_init; + } + + /* Set a `lec` value so that we can check for updates later */ + bxcan_rmw(®s->esr, BXCAN_ESR_LEC, LEC_UNUSED << BXCAN_ESR_LEC_SHIFT); + + /* IER + * + * Enable interrupt for: + * bus-off + * passive error + * warning error + * last error code + * RX FIFO pending message + * TX mailbox empty + */ + bxcan_rmw(®s->ier, BXCAN_IER_WKUIE | BXCAN_IER_SLKIE | + BXCAN_IER_FOVIE1 | BXCAN_IER_FFIE1 | BXCAN_IER_FMPIE1 | + BXCAN_IER_FOVIE0 | BXCAN_IER_FFIE0, + BXCAN_IER_ERRIE | BXCAN_IER_LECIE | BXCAN_IER_BOFIE | + BXCAN_IER_EPVIE | BXCAN_IER_EWGIE | BXCAN_IER_FMPIE0 | + BXCAN_IER_TMEIE); + + priv->can.state = CAN_STATE_ERROR_ACTIVE; + return 0; + +failed_leave_init: +failed_enter_init: +failed_leave_sleep: + bxcan_chip_softreset(priv); + return err; +} + +static int bxcan_open(struct net_device *ndev) +{ + struct bxcan_priv *priv = netdev_priv(ndev); + int err; + + err = open_candev(ndev); + if (err) { + netdev_err(ndev, "open_candev() failed, error %d\n", err); + goto failed_open; + } + + napi_enable(&priv->napi); + err = request_irq(ndev->irq, bxcan_rx_isr, IRQF_SHARED, ndev->name, + ndev); + if (err) { + netdev_err(ndev, "failed to register rx irq(%d), error %d\n", + ndev->irq, err); + goto failed_rx_irq_request; + } + + err = request_irq(priv->tx_irq, bxcan_tx_isr, IRQF_SHARED, ndev->name, + ndev); + if (err) { + netdev_err(ndev, "failed to register tx irq(%d), error %d\n", + priv->tx_irq, err); + goto failed_tx_irq_request; + } + + err = request_irq(priv->sce_irq, bxcan_state_change_isr, IRQF_SHARED, + ndev->name, ndev); + if (err) { + netdev_err(ndev, "failed to register sce irq(%d), error %d\n", + priv->sce_irq, err); + goto failed_sce_irq_request; + } + + err = bxcan_start(ndev); + if (err) + goto failed_start; + + netif_start_queue(ndev); + return 0; + +failed_start: +failed_sce_irq_request: + free_irq(priv->tx_irq, ndev); +failed_tx_irq_request: + free_irq(ndev->irq, ndev); +failed_rx_irq_request: + napi_disable(&priv->napi); + close_candev(ndev); +failed_open: + return err; +} + +static void bxcan_stop(struct net_device *ndev) +{ + struct bxcan_priv *priv = netdev_priv(ndev); + + bxcan_disable_filters(priv->dev->parent, priv->master); + bxcan_enter_sleep_mode(priv); + priv->can.state = CAN_STATE_STOPPED; +} + +static int bxcan_close(struct net_device *ndev) +{ + struct bxcan_priv *priv = netdev_priv(ndev); + + netif_stop_queue(ndev); + bxcan_stop(ndev); + free_irq(ndev->irq, ndev); + free_irq(priv->tx_irq, ndev); + free_irq(priv->sce_irq, ndev); + napi_disable(&priv->napi); + close_candev(ndev); + return 0; +} + +static netdev_tx_t bxcan_start_xmit(struct sk_buff *skb, + struct net_device *ndev) +{ + struct bxcan_priv *priv = netdev_priv(ndev); + struct can_frame *cf = (struct can_frame *)skb->data; + struct bxcan_regs __iomem *regs = priv->regs; + struct bxcan_mb __iomem *mb_regs; + unsigned int idx; + u32 id; + int i, j; + + if (can_dropped_invalid_skb(ndev, skb)) + return NETDEV_TX_OK; + + if (bxcan_tx_busy(priv)) + return NETDEV_TX_BUSY; + + idx = bxcan_get_tx_head(priv); + priv->tx_head++; + if (bxcan_get_tx_free(priv) == 0) + netif_stop_queue(ndev); + + mb_regs = ®s->tx_mb[idx]; + if (cf->can_id & CAN_EFF_FLAG) + id = BXCAN_TIxR_EXID(cf->can_id & CAN_EFF_MASK) | + BXCAN_TIxR_IDE; + else + id = BXCAN_TIxR_STID(cf->can_id & CAN_SFF_MASK); + + if (cf->can_id & CAN_RTR_FLAG) + id |= BXCAN_TIxR_RTR; + + bxcan_rmw(&mb_regs->dlc, BXCAN_TDTxR_DLC_MASK, + BXCAN_TDTxR_DLC(cf->len)); + can_put_echo_skb(skb, ndev, idx, 0); + + for (i = 0, j = 0; i < cf->len; i += 4, j++) + writel(*(u32 *)(cf->data + i), &mb_regs->data[j]); + + /* Start transmission */ + writel(id | BXCAN_TIxR_TXRQ, &mb_regs->id); + + return NETDEV_TX_OK; +} + +static const struct net_device_ops bxcan_netdev_ops = { + .ndo_open = bxcan_open, + .ndo_stop = bxcan_close, + .ndo_start_xmit = bxcan_start_xmit, + .ndo_change_mtu = can_change_mtu, +}; + +static void bxcan_rx_pkt(struct net_device *ndev, + struct bxcan_mb __iomem *mb_regs) +{ + struct net_device_stats *stats = &ndev->stats; + struct can_frame *cf; + struct sk_buff *skb; + u32 id, dlc; + + skb = alloc_can_skb(ndev, &cf); + if (!skb) { + stats->rx_dropped++; + return; + } + + id = readl(&mb_regs->id); + if (id & BXCAN_RIxR_IDE) + cf->can_id = (id >> BXCAN_RIxR_EXID_SHIFT) | CAN_EFF_FLAG; + else + cf->can_id = (id >> BXCAN_RIxR_STID_SHIFT) & CAN_SFF_MASK; + + dlc = readl(&mb_regs->dlc) & BXCAN_RDTxR_DLC; + cf->len = can_cc_dlc2len(dlc); + + if (id & BXCAN_RIxR_RTR) { + cf->can_id |= CAN_RTR_FLAG; + } else { + int i, j; + + for (i = 0, j = 0; i < cf->len; i += 4, j++) + *(u32 *)(cf->data + i) = readl(&mb_regs->data[j]); + + stats->rx_bytes += cf->len; + } + + stats->rx_packets++; + netif_receive_skb(skb); +} + +static int bxcan_rx_poll(struct napi_struct *napi, int quota) +{ + struct net_device *ndev = napi->dev; + struct bxcan_priv *priv = netdev_priv(ndev); + struct bxcan_regs __iomem *regs = priv->regs; + int num_pkts; + u32 rf0r; + + for (num_pkts = 0; num_pkts < quota; num_pkts++) { + rf0r = readl(®s->rf0r); + if (!(rf0r & BXCAN_RF0R_FMP0)) + break; + + bxcan_rx_pkt(ndev, ®s->rx_mb[0]); + + rf0r |= BXCAN_RF0R_RFOM0; + writel(rf0r, ®s->rf0r); + } + + if (num_pkts < quota) { + napi_complete_done(napi, num_pkts); + bxcan_rmw(®s->ier, 0, BXCAN_IER_FMPIE0); + } + + return num_pkts; +} + +static int bxcan_do_set_mode(struct net_device *ndev, enum can_mode mode) +{ + int err; + + switch (mode) { + case CAN_MODE_START: + err = bxcan_start(ndev); + if (err) + return err; + + netif_wake_queue(ndev); + break; + default: + return -EOPNOTSUPP; + } + + return 0; +} + +static int bxcan_disable_clks(struct bxcan_priv *priv) +{ + if (priv->clk) + clk_disable_unprepare(priv->clk); + + bxcan_disable_master_clk(priv->dev->parent); + return 0; +} + +static int bxcan_enable_clks(struct bxcan_priv *priv) +{ + int err; + + err = bxcan_enable_master_clk(priv->dev->parent); + if (err) + return err; + + if (priv->clk) { + err = clk_prepare_enable(priv->clk); + if (err) + bxcan_disable_master_clk(priv->dev->parent); + } + + return err; +} + +static int bxcan_get_berr_counter(const struct net_device *ndev, + struct can_berr_counter *bec) +{ + struct bxcan_priv *priv = netdev_priv(ndev); + struct bxcan_regs __iomem *regs = priv->regs; + u32 esr; + int err; + + err = bxcan_enable_clks(priv); + if (err) + return err; + + esr = readl(®s->esr); + bec->txerr = BXCAN_TEC(esr); + bec->rxerr = BXCAN_REC(esr); + err = bxcan_disable_clks(priv); + return 0; +} + +static int bxcan_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct device *dev = &pdev->dev; + struct net_device *ndev; + struct bxcan_priv *priv; + struct clk *clk = NULL; + bool master; + u32 offset; + int err, rx_irq, tx_irq, sce_irq; + + master = of_property_read_bool(np, "st,can-master"); + if (!master) { + clk = devm_clk_get(dev, NULL); + if (IS_ERR(clk)) { + dev_err(dev, "failed to get clock\n"); + return PTR_ERR(clk); + } + } + + rx_irq = platform_get_irq_byname(pdev, "rx0"); + if (rx_irq < 0) { + dev_err(dev, "failed to get rx0 irq\n"); + return rx_irq; + } + + tx_irq = platform_get_irq_byname(pdev, "tx"); + if (tx_irq < 0) { + dev_err(dev, "failed to get tx irq\n"); + return tx_irq; + } + + sce_irq = platform_get_irq_byname(pdev, "sce"); + if (sce_irq < 0) { + dev_err(dev, "failed to get sce irq\n"); + return sce_irq; + } + + err = of_property_read_u32(np, "reg", &offset); + if (err) { + dev_err(dev, "failed to get reg property\n"); + return err; + } + + ndev = alloc_candev(sizeof(struct bxcan_priv), BXCAN_TX_MB_NUM); + if (!ndev) { + dev_err(dev, "alloc_candev() failed\n"); + return -ENOMEM; + } + + priv = netdev_priv(ndev); + platform_set_drvdata(pdev, ndev); + SET_NETDEV_DEV(ndev, dev); + ndev->netdev_ops = &bxcan_netdev_ops; + ndev->irq = rx_irq; + ndev->flags |= IFF_ECHO; + + priv->dev = dev; + priv->ndev = ndev; + priv->regs = bxcan_get_base_addr(dev->parent) + offset; + priv->clk = clk; + priv->tx_irq = tx_irq; + priv->sce_irq = sce_irq; + priv->master = master; + priv->can.clock.freq = + master ? bxcan_get_master_clk_rate(dev->parent) : + clk_get_rate(clk); + priv->tx_head = 0; + priv->tx_tail = 0; + priv->can.bittiming_const = &bxcan_bittiming_const; + priv->can.do_set_mode = bxcan_do_set_mode; + priv->can.do_get_berr_counter = bxcan_get_berr_counter; + priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | + CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING; + netif_napi_add(ndev, &priv->napi, bxcan_rx_poll, BXCAN_NAPI_WEIGHT); + + err = bxcan_enable_clks(priv); + if (err) { + dev_err(dev, "failed to enable clocks\n"); + return err; + } + + err = register_candev(ndev); + if (err) { + dev_err(dev, "failed to register netdev\n"); + goto failed_register; + } + + dev_info(dev, "clk: %d Hz, IRQs: %d, %d, %d\n", priv->can.clock.freq, + tx_irq, rx_irq, sce_irq); + return 0; + +failed_register: + netif_napi_del(&priv->napi); + free_candev(ndev); + return err; +} + +static int bxcan_remove(struct platform_device *pdev) +{ + struct net_device *ndev = platform_get_drvdata(pdev); + struct bxcan_priv *priv = netdev_priv(ndev); + + unregister_candev(ndev); + bxcan_disable_clks(priv); + netif_napi_del(&priv->napi); + free_candev(ndev); + return 0; +} + +static int __maybe_unused bxcan_suspend(struct device *dev) +{ + struct net_device *ndev = dev_get_drvdata(dev); + struct bxcan_priv *priv = netdev_priv(ndev); + + if (!netif_running(ndev)) + return 0; + + netif_stop_queue(ndev); + netif_device_detach(ndev); + + bxcan_enter_sleep_mode(priv); + priv->can.state = CAN_STATE_SLEEPING; + bxcan_disable_clks(priv); + return 0; +} + +static int __maybe_unused bxcan_resume(struct device *dev) +{ + struct net_device *ndev = dev_get_drvdata(dev); + struct bxcan_priv *priv = netdev_priv(ndev); + + if (!netif_running(ndev)) + return 0; + + bxcan_enable_clks(priv); + bxcan_leave_sleep_mode(priv); + priv->can.state = CAN_STATE_ERROR_ACTIVE; + + netif_device_attach(ndev); + netif_start_queue(ndev); + return 0; +} + +static SIMPLE_DEV_PM_OPS(bxcan_pm_ops, bxcan_suspend, bxcan_resume); + +static const struct of_device_id bxcan_of_match[] = { + {.compatible = "st,stm32f4-bxcan"}, + { /* sentinel */ }, +}; +MODULE_DEVICE_TABLE(of, bxcan_of_match); + +static struct platform_driver bxcan_driver = { + .driver = { + .name = KBUILD_MODNAME, + .pm = &bxcan_pm_ops, + .of_match_table = bxcan_of_match, + }, + .probe = bxcan_probe, + .remove = bxcan_remove, +}; + +module_platform_driver(bxcan_driver); + +MODULE_AUTHOR("Dario Binacchi "); +MODULE_DESCRIPTION("STMicroelectronics Basic Extended CAN controller driver"); +MODULE_LICENSE("GPL");