xiaoxiang781216 commented on code in PR #10503:
URL: https://github.com/apache/nuttx/pull/10503#discussion_r1318918728


##########
drivers/net/ksz9477_i2c.c:
##########
@@ -0,0 +1,120 @@
+/****************************************************************************
+ * drivers/net/ksz9477_i2c.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/net/ksz9477.h>
+#include <stdbool.h>
+#include "ksz9477_reg.h"
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+int ksz9477_init(ksz9477_port_t master_port);
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct i2c_master_s *ksz9477_i2c_bus;

Review Comment:
   g_ksz9477_i2c_bus



##########
drivers/net/ksz9477_i2c.c:
##########
@@ -0,0 +1,120 @@
+/****************************************************************************
+ * drivers/net/ksz9477_i2c.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/net/ksz9477.h>
+#include <stdbool.h>
+#include "ksz9477_reg.h"
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+int ksz9477_init(ksz9477_port_t master_port);

Review Comment:
   remove



##########
drivers/net/ksz9477_i2c.c:
##########
@@ -0,0 +1,120 @@
+/****************************************************************************
+ * drivers/net/ksz9477_i2c.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/net/ksz9477.h>
+#include <stdbool.h>
+#include "ksz9477_reg.h"
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+int ksz9477_init(ksz9477_port_t master_port);
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct i2c_master_s *ksz9477_i2c_bus;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void setup_i2c_transfer(struct i2c_msg_s *msg, uint8_t *data,
+                               size_t len, bool read)
+{
+  msg[0].frequency = KSZ9477_I2C_SPEED;
+  msg[0].addr      = KSZ9477_I2C_ADDRESS;
+  msg[0].flags     = read ? I2C_M_READ : 0;
+  msg[0].buffer    = data;
+  msg[0].length    = len;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int ksz9477_read(struct ksz9477_transfer_s *read_msg)
+{
+  struct i2c_msg_s msg[2];
+
+  /* Set up to write the address */
+
+  setup_i2c_transfer(&msg[0], (uint8_t *)&read_msg->reg,
+                     sizeof(read_msg->reg), false);
+
+  /* Followed by the read data */
+
+  setup_i2c_transfer(&msg[1], (uint8_t *)&read_msg->data,
+                     read_msg->len - sizeof(read_msg->reg), true);
+
+  return I2C_TRANSFER(ksz9477_i2c_bus, msg, 2);
+}
+
+int ksz9477_write(struct ksz9477_transfer_s *write_msg)
+{
+  struct i2c_msg_s msg;
+
+  /* Set up to write the address and data */
+
+  setup_i2c_transfer(&msg, (uint8_t *)&write_msg->reg,
+                     write_msg->len, false);
+
+  return I2C_TRANSFER(ksz9477_i2c_bus, &msg, 1);
+}
+
+/****************************************************************************
+ * Name: ksz9477_i2c_init
+ *
+ * Description:
+ *   Stores the configured i2c_master and calls the main init function
+ *
+ * Input Parameters:
+ *   i2c_bus:     The i2c master used as a control interface
+ *   master_port: The switch port connected to the host MAC
+ * Returned Value:
+ *   OK or ERROR
+ *
+ ****************************************************************************/
+
+int ksz9477_i2c_init(struct i2c_master_s *i2c_bus,
+                     ksz9477_port_t master_port)
+{
+  if (!i2c_bus)
+    {
+      return ERROR;

Review Comment:
   -EINVAL



##########
arch/risc-v/src/mpfs/mpfs_ethernet.c:
##########
@@ -3284,7 +3316,9 @@ static void mpfs_mdcclock(struct mpfs_ethmac_s *priv)
 
 static int mpfs_phyinit(struct mpfs_ethmac_s *priv)
 {
-  int ret;
+  int ret = ERROR;

Review Comment:
    EROR->-EINVAL



##########
drivers/net/ksz9477.c:
##########
@@ -0,0 +1,259 @@
+/****************************************************************************
+ * drivers/net/ksz9477.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <debug.h>
+#include <nuttx/net/ksz9477.h>
+#include "ksz9477_reg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static uint16_t bswap16(uint16_t data)
+{
+  return (data << 8) | (data >> 8);
+}
+
+static uint32_t bswap32(uint32_t data)
+{
+  return (data << 24) | (data >> 24) | ((data << 8) & 0xff0000) |
+    ((data >> 8) & 0xff00);
+}
+
+#if 0 /* Enable when needed */
+static int ksz9477_reg_read8(uint16_t reg, uint8_t *data)
+{
+  int ret;
+  struct ksz9477_transfer_s read_msg;
+  read_msg.len = 3;
+  read_msg.reg = bswap16(reg);
+  ret = ksz9477_read(&read_msg);
+  *data = read_msg.data;
+  return ret;
+}
+#endif
+
+static int ksz9477_reg_read16(uint16_t reg, uint16_t *data)
+{
+  int ret;
+  struct ksz9477_transfer_s read_msg;
+  read_msg.len = 4;
+  read_msg.reg = bswap16(reg);
+  ret = ksz9477_read(&read_msg);
+  *data = bswap16(read_msg.data);
+  return ret;
+}
+
+static int ksz9477_reg_read32(uint16_t reg, uint32_t *data)
+{
+  int ret;
+  struct ksz9477_transfer_s read_msg;
+  read_msg.len = 6;
+  read_msg.reg = bswap16(reg);
+  ret = ksz9477_read(&read_msg);
+  *data = bswap32(read_msg.data);
+  return ret;
+}
+
+#if 0  /* Enable when needed */
+static int ksz9477_reg_write8(uint16_t reg, uint8_t data)
+{
+  struct ksz9477_transfer_s write_msg;
+  write_msg.len = 3;
+  write_msg.reg = bswap16(reg);
+  write_msg.data = data;
+  return ksz9477_write(&write_msg);
+}
+#endif
+
+static int ksz9477_reg_write32(uint16_t reg, uint32_t data)
+{
+  struct ksz9477_transfer_s write_msg;
+  write_msg.len = 6;
+  write_msg.reg = bswap16(reg);
+  write_msg.data = bswap32(data);
+  return ksz9477_write(&write_msg);
+}
+
+#if 0  /* Enable when needed */
+static int ksz9477_reg_write16(uint16_t reg, uint16_t data)
+{
+  int ret;
+  struct ksz9477_transfer_s write_msg;
+  uint16_t addr = reg;
+  uint32_t data32;
+
+  /* Errata: 16-bit writes to registers 0xN120-0xN13f will corrupt the
+   * adjacent regsters. Workaround: perform only 32-bit writes to this
+   * area
+   */
+
+  if ((reg & 0xfff) >= 0x120 && (reg & 0xfff) <= 0x13f)
+    {
+      /* Align write on lower 16-byte boundary */
+
+      addr = reg & (~1);
+
+      /* Read the full 32-bit register */
+
+      ret = ksz9477_reg_read32(addr, &data32);
+      if (ret != OK)
+        {
+          return ret;
+        }
+
+      /* Inject the data to the 32-bit write data */
+
+      if (reg & 1)
+        {
+          data32 = (data32 & 0xff0000ff) | ((uint32_t)data << 8);
+        }
+      else
+        {
+          data32 = (data32 & 0xffff0000) | data;
+        }
+
+      write_msg.len = 6;
+      write_msg.data = bswap32(data32);
+    }
+  else
+    {
+      write_msg.len = 4;
+      write_msg.data = bswap16(data);
+    }
+
+  write_msg.reg = bswap16(reg);
+
+  return ksz9477_write(&write_msg);
+}
+#endif
+
+static int ksz9477_sgmii_read_indirect(uint32_t address, uint16_t *value,
+                                       unsigned len)
+{
+  int ret;
+  uint32_t data;
+
+  address |= SGMII_PORT_ADDRESS_AUTO_INC_ENB;
+  ret = ksz9477_reg_write32(KSZ9477_SGMII_PORT_ADDRESS, address);
+  while (len-- && ret == OK)
+    {
+      ret = ksz9477_reg_read32(KSZ9477_SGMII_PORT_DATA, &data);
+      *value++ = (uint16_t)data;
+    }
+
+  return ret;
+}
+
+static int ksz9477_sgmii_write_indirect(uint32_t address, uint16_t *value,
+                                        unsigned len)
+{
+  int ret;
+  uint32_t data;
+
+  address |= SGMII_PORT_ADDRESS_AUTO_INC_ENB;
+  ret = ksz9477_reg_write32(KSZ9477_SGMII_PORT_ADDRESS, address);
+  while (len-- && ret == OK)
+    {
+      data = *value++;
+      ret = ksz9477_reg_write32(KSZ9477_SGMII_PORT_DATA, data);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ksz9477_init
+ *
+ * Description:
+ *   Switches the ksz9477's SGMII port into PHY mode and sets the
+ *   default settings to work directly with an external MAC
+ *
+ * Input Parameters:
+ *   master_port: Port connected to the host MAC
+ *
+ * Returned Value:
+ *   OK or ERROR
+ *
+ ****************************************************************************/
+
+int ksz9477_init(ksz9477_port_t master_port)
+{
+  int ret;
+  uint16_t regval16;
+  uint32_t regval32;
+
+  /* Read the ID registers */
+
+  ret = ksz9477_reg_read16(KSZ9477_ID1, &regval16);
+  if (ret != OK || regval16 != KSZ9477_ID)
+    {
+      nerr("Device not found, id %x, ret %d\n", regval16, ret);
+      return ERROR;

Review Comment:
   ALL ERROR->ret



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to