1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Realtek Simple Management Interface (SMI) driver 3 * It can be discussed how "simple" this interface is. 4 * 5 * The SMI protocol piggy-backs the MDIO MDC and MDIO signals levels 6 * but the protocol is not MDIO at all. Instead it is a Realtek 7 * pecularity that need to bit-bang the lines in a special way to 8 * communicate with the switch. 9 * 10 * ASICs we intend to support with this driver: 11 * 12 * RTL8366 - The original version, apparently 13 * RTL8369 - Similar enough to have the same datsheet as RTL8366 14 * RTL8366RB - Probably reads out "RTL8366 revision B", has a quite 15 * different register layout from the other two 16 * RTL8366S - Is this "RTL8366 super"? 17 * RTL8367 - Has an OpenWRT driver as well 18 * RTL8368S - Seems to be an alternative name for RTL8366RB 19 * RTL8370 - Also uses SMI 20 * 21 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org> 22 * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com> 23 * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv> 24 * Copyright (C) 2011 Colin Leitner <colin.leitner@googlemail.com> 25 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org> 26 */ 27 28 #include <linux/kernel.h> 29 #include <linux/module.h> 30 #include <linux/device.h> 31 #include <linux/spinlock.h> 32 #include <linux/skbuff.h> 33 #include <linux/of.h> 34 #include <linux/of_mdio.h> 35 #include <linux/delay.h> 36 #include <linux/gpio/consumer.h> 37 #include <linux/platform_device.h> 38 #include <linux/regmap.h> 39 #include <linux/bitops.h> 40 #include <linux/if_bridge.h> 41 42 #include "realtek.h" 43 #include "realtek-smi.h" 44 #include "rtl83xx.h" 45 46 #define REALTEK_SMI_ACK_RETRY_COUNT 5 47 48 static inline void realtek_smi_clk_delay(struct realtek_priv *priv) 49 { 50 ndelay(priv->variant->clk_delay); 51 } 52 53 static void realtek_smi_start(struct realtek_priv *priv) 54 { 55 /* Set GPIO pins to output mode, with initial state: 56 * SCK = 0, SDA = 1 57 */ 58 gpiod_direction_output(priv->mdc, 0); 59 gpiod_direction_output(priv->mdio, 1); 60 realtek_smi_clk_delay(priv); 61 62 /* CLK 1: 0 -> 1, 1 -> 0 */ 63 gpiod_set_value(priv->mdc, 1); 64 realtek_smi_clk_delay(priv); 65 gpiod_set_value(priv->mdc, 0); 66 realtek_smi_clk_delay(priv); 67 68 /* CLK 2: */ 69 gpiod_set_value(priv->mdc, 1); 70 realtek_smi_clk_delay(priv); 71 gpiod_set_value(priv->mdio, 0); 72 realtek_smi_clk_delay(priv); 73 gpiod_set_value(priv->mdc, 0); 74 realtek_smi_clk_delay(priv); 75 gpiod_set_value(priv->mdio, 1); 76 } 77 78 static void realtek_smi_stop(struct realtek_priv *priv) 79 { 80 realtek_smi_clk_delay(priv); 81 gpiod_set_value(priv->mdio, 0); 82 gpiod_set_value(priv->mdc, 1); 83 realtek_smi_clk_delay(priv); 84 gpiod_set_value(priv->mdio, 1); 85 realtek_smi_clk_delay(priv); 86 gpiod_set_value(priv->mdc, 1); 87 realtek_smi_clk_delay(priv); 88 gpiod_set_value(priv->mdc, 0); 89 realtek_smi_clk_delay(priv); 90 gpiod_set_value(priv->mdc, 1); 91 92 /* Add a click */ 93 realtek_smi_clk_delay(priv); 94 gpiod_set_value(priv->mdc, 0); 95 realtek_smi_clk_delay(priv); 96 gpiod_set_value(priv->mdc, 1); 97 98 /* Set GPIO pins to input mode */ 99 gpiod_direction_input(priv->mdio); 100 gpiod_direction_input(priv->mdc); 101 } 102 103 static void realtek_smi_write_bits(struct realtek_priv *priv, u32 data, u32 len) 104 { 105 for (; len > 0; len--) { 106 realtek_smi_clk_delay(priv); 107 108 /* Prepare data */ 109 gpiod_set_value(priv->mdio, !!(data & (1 << (len - 1)))); 110 realtek_smi_clk_delay(priv); 111 112 /* Clocking */ 113 gpiod_set_value(priv->mdc, 1); 114 realtek_smi_clk_delay(priv); 115 gpiod_set_value(priv->mdc, 0); 116 } 117 } 118 119 static void realtek_smi_read_bits(struct realtek_priv *priv, u32 len, u32 *data) 120 { 121 gpiod_direction_input(priv->mdio); 122 123 for (*data = 0; len > 0; len--) { 124 u32 u; 125 126 realtek_smi_clk_delay(priv); 127 128 /* Clocking */ 129 gpiod_set_value(priv->mdc, 1); 130 realtek_smi_clk_delay(priv); 131 u = !!gpiod_get_value(priv->mdio); 132 gpiod_set_value(priv->mdc, 0); 133 134 *data |= (u << (len - 1)); 135 } 136 137 gpiod_direction_output(priv->mdio, 0); 138 } 139 140 static int realtek_smi_wait_for_ack(struct realtek_priv *priv) 141 { 142 int retry_cnt; 143 144 retry_cnt = 0; 145 do { 146 u32 ack; 147 148 realtek_smi_read_bits(priv, 1, &ack); 149 if (ack == 0) 150 break; 151 152 if (++retry_cnt > REALTEK_SMI_ACK_RETRY_COUNT) { 153 dev_err(priv->dev, "ACK timeout\n"); 154 return -ETIMEDOUT; 155 } 156 } while (1); 157 158 return 0; 159 } 160 161 static int realtek_smi_write_byte(struct realtek_priv *priv, u8 data) 162 { 163 realtek_smi_write_bits(priv, data, 8); 164 return realtek_smi_wait_for_ack(priv); 165 } 166 167 static int realtek_smi_write_byte_noack(struct realtek_priv *priv, u8 data) 168 { 169 realtek_smi_write_bits(priv, data, 8); 170 return 0; 171 } 172 173 static int realtek_smi_read_byte0(struct realtek_priv *priv, u8 *data) 174 { 175 u32 t; 176 177 /* Read data */ 178 realtek_smi_read_bits(priv, 8, &t); 179 *data = (t & 0xff); 180 181 /* Send an ACK */ 182 realtek_smi_write_bits(priv, 0x00, 1); 183 184 return 0; 185 } 186 187 static int realtek_smi_read_byte1(struct realtek_priv *priv, u8 *data) 188 { 189 u32 t; 190 191 /* Read data */ 192 realtek_smi_read_bits(priv, 8, &t); 193 *data = (t & 0xff); 194 195 /* Send an ACK */ 196 realtek_smi_write_bits(priv, 0x01, 1); 197 198 return 0; 199 } 200 201 static int realtek_smi_read_reg(struct realtek_priv *priv, u32 addr, u32 *data) 202 { 203 unsigned long flags; 204 u8 lo = 0; 205 u8 hi = 0; 206 int ret; 207 208 spin_lock_irqsave(&priv->lock, flags); 209 210 realtek_smi_start(priv); 211 212 /* Send READ command */ 213 ret = realtek_smi_write_byte(priv, priv->variant->cmd_read); 214 if (ret) 215 goto out; 216 217 /* Set ADDR[7:0] */ 218 ret = realtek_smi_write_byte(priv, addr & 0xff); 219 if (ret) 220 goto out; 221 222 /* Set ADDR[15:8] */ 223 ret = realtek_smi_write_byte(priv, addr >> 8); 224 if (ret) 225 goto out; 226 227 /* Read DATA[7:0] */ 228 realtek_smi_read_byte0(priv, &lo); 229 /* Read DATA[15:8] */ 230 realtek_smi_read_byte1(priv, &hi); 231 232 *data = ((u32)lo) | (((u32)hi) << 8); 233 234 ret = 0; 235 236 out: 237 realtek_smi_stop(priv); 238 spin_unlock_irqrestore(&priv->lock, flags); 239 240 return ret; 241 } 242 243 static int realtek_smi_write_reg(struct realtek_priv *priv, 244 u32 addr, u32 data, bool ack) 245 { 246 unsigned long flags; 247 int ret; 248 249 spin_lock_irqsave(&priv->lock, flags); 250 251 realtek_smi_start(priv); 252 253 /* Send WRITE command */ 254 ret = realtek_smi_write_byte(priv, priv->variant->cmd_write); 255 if (ret) 256 goto out; 257 258 /* Set ADDR[7:0] */ 259 ret = realtek_smi_write_byte(priv, addr & 0xff); 260 if (ret) 261 goto out; 262 263 /* Set ADDR[15:8] */ 264 ret = realtek_smi_write_byte(priv, addr >> 8); 265 if (ret) 266 goto out; 267 268 /* Write DATA[7:0] */ 269 ret = realtek_smi_write_byte(priv, data & 0xff); 270 if (ret) 271 goto out; 272 273 /* Write DATA[15:8] */ 274 if (ack) 275 ret = realtek_smi_write_byte(priv, data >> 8); 276 else 277 ret = realtek_smi_write_byte_noack(priv, data >> 8); 278 if (ret) 279 goto out; 280 281 ret = 0; 282 283 out: 284 realtek_smi_stop(priv); 285 spin_unlock_irqrestore(&priv->lock, flags); 286 287 return ret; 288 } 289 290 /* There is one single case when we need to use this accessor and that 291 * is when issueing soft reset. Since the device reset as soon as we write 292 * that bit, no ACK will come back for natural reasons. 293 */ 294 static int realtek_smi_write_reg_noack(void *ctx, u32 reg, u32 val) 295 { 296 return realtek_smi_write_reg(ctx, reg, val, false); 297 } 298 299 /* Regmap accessors */ 300 301 static int realtek_smi_write(void *ctx, u32 reg, u32 val) 302 { 303 struct realtek_priv *priv = ctx; 304 305 return realtek_smi_write_reg(priv, reg, val, true); 306 } 307 308 static int realtek_smi_read(void *ctx, u32 reg, u32 *val) 309 { 310 struct realtek_priv *priv = ctx; 311 312 return realtek_smi_read_reg(priv, reg, val); 313 } 314 315 static int realtek_smi_mdio_read(struct mii_bus *bus, int addr, int regnum) 316 { 317 struct realtek_priv *priv = bus->priv; 318 319 return priv->ops->phy_read(priv, addr, regnum); 320 } 321 322 static int realtek_smi_mdio_write(struct mii_bus *bus, int addr, int regnum, 323 u16 val) 324 { 325 struct realtek_priv *priv = bus->priv; 326 327 return priv->ops->phy_write(priv, addr, regnum, val); 328 } 329 330 static int realtek_smi_setup_mdio(struct dsa_switch *ds) 331 { 332 struct realtek_priv *priv = ds->priv; 333 struct device_node *mdio_np; 334 int ret = 0; 335 336 mdio_np = of_get_child_by_name(priv->dev->of_node, "mdio"); 337 if (!mdio_np) { 338 dev_err(priv->dev, "no MDIO bus node\n"); 339 return -ENODEV; 340 } 341 342 priv->user_mii_bus = devm_mdiobus_alloc(priv->dev); 343 if (!priv->user_mii_bus) { 344 ret = -ENOMEM; 345 goto err_put_node; 346 } 347 348 priv->user_mii_bus->priv = priv; 349 priv->user_mii_bus->name = "SMI user MII"; 350 priv->user_mii_bus->read = realtek_smi_mdio_read; 351 priv->user_mii_bus->write = realtek_smi_mdio_write; 352 snprintf(priv->user_mii_bus->id, MII_BUS_ID_SIZE, "SMI-%d", 353 ds->index); 354 priv->user_mii_bus->parent = priv->dev; 355 356 ret = devm_of_mdiobus_register(priv->dev, priv->user_mii_bus, mdio_np); 357 if (ret) { 358 dev_err(priv->dev, "unable to register MDIO bus %s\n", 359 priv->user_mii_bus->id); 360 goto err_put_node; 361 } 362 363 err_put_node: 364 of_node_put(mdio_np); 365 366 return ret; 367 } 368 369 static const struct realtek_interface_info realtek_smi_info = { 370 .reg_read = realtek_smi_read, 371 .reg_write = realtek_smi_write, 372 }; 373 374 /** 375 * realtek_smi_probe() - Probe a platform device for an SMI-connected switch 376 * @pdev: platform_device to probe on. 377 * 378 * This function should be used as the .probe in a platform_driver. After 379 * calling the common probe function for both interfaces, it initializes the 380 * values specific for SMI-connected devices. Finally, it calls a common 381 * function to register the DSA switch. 382 * 383 * Context: Can sleep. Takes and releases priv->map_lock. 384 * Return: Returns 0 on success, a negative error on failure. 385 */ 386 int realtek_smi_probe(struct platform_device *pdev) 387 { 388 struct device *dev = &pdev->dev; 389 struct realtek_priv *priv; 390 int ret; 391 392 priv = rtl83xx_probe(dev, &realtek_smi_info); 393 if (IS_ERR(priv)) 394 return PTR_ERR(priv); 395 396 /* Fetch MDIO pins */ 397 priv->mdc = devm_gpiod_get_optional(dev, "mdc", GPIOD_OUT_LOW); 398 if (IS_ERR(priv->mdc)) { 399 rtl83xx_remove(priv); 400 return PTR_ERR(priv->mdc); 401 } 402 403 priv->mdio = devm_gpiod_get_optional(dev, "mdio", GPIOD_OUT_LOW); 404 if (IS_ERR(priv->mdio)) { 405 rtl83xx_remove(priv); 406 return PTR_ERR(priv->mdio); 407 } 408 409 priv->write_reg_noack = realtek_smi_write_reg_noack; 410 priv->setup_interface = realtek_smi_setup_mdio; 411 priv->ds_ops = priv->variant->ds_ops_smi; 412 413 ret = rtl83xx_register_switch(priv); 414 if (ret) { 415 rtl83xx_remove(priv); 416 return ret; 417 } 418 419 return 0; 420 } 421 EXPORT_SYMBOL_NS_GPL(realtek_smi_probe, REALTEK_DSA); 422 423 /** 424 * realtek_smi_remove() - Remove the driver of a SMI-connected switch 425 * @pdev: platform_device to be removed. 426 * 427 * This function should be used as the .remove_new in a platform_driver. First 428 * it unregisters the DSA switch and then it calls the common remove function. 429 * 430 * Context: Can sleep. 431 * Return: Nothing. 432 */ 433 void realtek_smi_remove(struct platform_device *pdev) 434 { 435 struct realtek_priv *priv = platform_get_drvdata(pdev); 436 437 if (!priv) 438 return; 439 440 rtl83xx_unregister_switch(priv); 441 442 rtl83xx_remove(priv); 443 } 444 EXPORT_SYMBOL_NS_GPL(realtek_smi_remove, REALTEK_DSA); 445 446 /** 447 * realtek_smi_shutdown() - Shutdown the driver of a SMI-connected switch 448 * @pdev: platform_device shutting down. 449 * 450 * This function should be used as the .shutdown in a platform_driver. It calls 451 * the common shutdown function. 452 * 453 * Context: Can sleep. 454 * Return: Nothing. 455 */ 456 void realtek_smi_shutdown(struct platform_device *pdev) 457 { 458 struct realtek_priv *priv = platform_get_drvdata(pdev); 459 460 if (!priv) 461 return; 462 463 rtl83xx_shutdown(priv); 464 } 465 EXPORT_SYMBOL_NS_GPL(realtek_smi_shutdown, REALTEK_DSA); 466