| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 3 | | - * under the terms of the GNU General Public License version 2 as published |
|---|
| 4 | | - * by the Free Software Foundation. |
|---|
| 5 | 3 | * |
|---|
| 6 | 4 | * Copyright (C) 2012 John Crispin <john@phrozen.org> |
|---|
| 7 | | - * |
|---|
| 8 | 5 | */ |
|---|
| 9 | 6 | |
|---|
| 10 | 7 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 17 | 14 | #include <linux/io.h> |
|---|
| 18 | 15 | #include <linux/clk.h> |
|---|
| 19 | 16 | #include <linux/err.h> |
|---|
| 20 | | - |
|---|
| 21 | | -#include <lantiq_soc.h> |
|---|
| 22 | 17 | |
|---|
| 23 | 18 | /* |
|---|
| 24 | 19 | * The Serial To Parallel (STP) is found on MIPS based Lantiq socs. It is a |
|---|
| .. | .. |
|---|
| 46 | 41 | #define XWAY_STP_4HZ BIT(23) |
|---|
| 47 | 42 | #define XWAY_STP_8HZ BIT(24) |
|---|
| 48 | 43 | #define XWAY_STP_10HZ (BIT(24) | BIT(23)) |
|---|
| 49 | | -#define XWAY_STP_SPEED_MASK (0xf << 23) |
|---|
| 44 | +#define XWAY_STP_SPEED_MASK (BIT(23) | BIT(24) | BIT(25) | BIT(26) | BIT(27)) |
|---|
| 45 | + |
|---|
| 46 | +#define XWAY_STP_FPIS_VALUE BIT(21) |
|---|
| 47 | +#define XWAY_STP_FPIS_MASK (BIT(20) | BIT(21)) |
|---|
| 50 | 48 | |
|---|
| 51 | 49 | /* clock source for automatic update */ |
|---|
| 52 | 50 | #define XWAY_STP_UPD_FPI BIT(31) |
|---|
| .. | .. |
|---|
| 59 | 57 | /* 2 groups of 3 bits can be driven by the phys */ |
|---|
| 60 | 58 | #define XWAY_STP_PHY_MASK 0x7 |
|---|
| 61 | 59 | #define XWAY_STP_PHY1_SHIFT 27 |
|---|
| 62 | | -#define XWAY_STP_PHY2_SHIFT 15 |
|---|
| 60 | +#define XWAY_STP_PHY2_SHIFT 3 |
|---|
| 61 | +#define XWAY_STP_PHY3_SHIFT 6 |
|---|
| 62 | +#define XWAY_STP_PHY4_SHIFT 15 |
|---|
| 63 | 63 | |
|---|
| 64 | 64 | /* STP has 3 groups of 8 bits */ |
|---|
| 65 | 65 | #define XWAY_STP_GROUP0 BIT(0) |
|---|
| .. | .. |
|---|
| 74 | 74 | #define xway_stp_r32(m, reg) __raw_readl(m + reg) |
|---|
| 75 | 75 | #define xway_stp_w32(m, val, reg) __raw_writel(val, m + reg) |
|---|
| 76 | 76 | #define xway_stp_w32_mask(m, clear, set, reg) \ |
|---|
| 77 | | - ltq_w32((ltq_r32(m + reg) & ~(clear)) | (set), \ |
|---|
| 78 | | - m + reg) |
|---|
| 77 | + xway_stp_w32(m, (xway_stp_r32(m, reg) & ~(clear)) | (set), reg) |
|---|
| 79 | 78 | |
|---|
| 80 | 79 | struct xway_stp { |
|---|
| 81 | 80 | struct gpio_chip gc; |
|---|
| .. | .. |
|---|
| 86 | 85 | u8 dsl; /* the 2 LSBs can be driven by the dsl core */ |
|---|
| 87 | 86 | u8 phy1; /* 3 bits can be driven by phy1 */ |
|---|
| 88 | 87 | u8 phy2; /* 3 bits can be driven by phy2 */ |
|---|
| 88 | + u8 phy3; /* 3 bits can be driven by phy3 */ |
|---|
| 89 | + u8 phy4; /* 3 bits can be driven by phy4 */ |
|---|
| 89 | 90 | u8 reserved; /* mask out the hw driven bits in gpio_request */ |
|---|
| 90 | 91 | }; |
|---|
| 91 | 92 | |
|---|
| .. | .. |
|---|
| 120 | 121 | else |
|---|
| 121 | 122 | chip->shadow &= ~BIT(gpio); |
|---|
| 122 | 123 | xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0); |
|---|
| 123 | | - xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0); |
|---|
| 124 | + if (!chip->reserved) |
|---|
| 125 | + xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0); |
|---|
| 124 | 126 | } |
|---|
| 125 | 127 | |
|---|
| 126 | 128 | /** |
|---|
| .. | .. |
|---|
| 159 | 161 | |
|---|
| 160 | 162 | /** |
|---|
| 161 | 163 | * xway_stp_hw_init() - Configure the STP unit and enable the clock gate |
|---|
| 162 | | - * @virt: pointer to the remapped register range |
|---|
| 164 | + * @chip: Pointer to the xway_stp chip structure |
|---|
| 163 | 165 | */ |
|---|
| 164 | | -static int xway_stp_hw_init(struct xway_stp *chip) |
|---|
| 166 | +static void xway_stp_hw_init(struct xway_stp *chip) |
|---|
| 165 | 167 | { |
|---|
| 166 | 168 | /* sane defaults */ |
|---|
| 167 | 169 | xway_stp_w32(chip->virt, 0, XWAY_STP_AR); |
|---|
| .. | .. |
|---|
| 194 | 196 | chip->phy2 << XWAY_STP_PHY2_SHIFT, |
|---|
| 195 | 197 | XWAY_STP_CON1); |
|---|
| 196 | 198 | |
|---|
| 199 | + if (of_machine_is_compatible("lantiq,grx390") |
|---|
| 200 | + || of_machine_is_compatible("lantiq,ar10")) { |
|---|
| 201 | + xway_stp_w32_mask(chip->virt, |
|---|
| 202 | + XWAY_STP_PHY_MASK << XWAY_STP_PHY3_SHIFT, |
|---|
| 203 | + chip->phy3 << XWAY_STP_PHY3_SHIFT, |
|---|
| 204 | + XWAY_STP_CON1); |
|---|
| 205 | + } |
|---|
| 206 | + |
|---|
| 207 | + if (of_machine_is_compatible("lantiq,grx390")) { |
|---|
| 208 | + xway_stp_w32_mask(chip->virt, |
|---|
| 209 | + XWAY_STP_PHY_MASK << XWAY_STP_PHY4_SHIFT, |
|---|
| 210 | + chip->phy4 << XWAY_STP_PHY4_SHIFT, |
|---|
| 211 | + XWAY_STP_CON1); |
|---|
| 212 | + } |
|---|
| 213 | + |
|---|
| 197 | 214 | /* mask out the hw driven bits in gpio_request */ |
|---|
| 198 | | - chip->reserved = (chip->phy2 << 5) | (chip->phy1 << 2) | chip->dsl; |
|---|
| 215 | + chip->reserved = (chip->phy4 << 11) | (chip->phy3 << 8) | (chip->phy2 << 5) |
|---|
| 216 | + | (chip->phy1 << 2) | chip->dsl; |
|---|
| 199 | 217 | |
|---|
| 200 | 218 | /* |
|---|
| 201 | 219 | * if we have pins that are driven by hw, we need to tell the stp what |
|---|
| 202 | 220 | * clock to use as a timer. |
|---|
| 203 | 221 | */ |
|---|
| 204 | | - if (chip->reserved) |
|---|
| 222 | + if (chip->reserved) { |
|---|
| 205 | 223 | xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK, |
|---|
| 206 | 224 | XWAY_STP_UPD_FPI, XWAY_STP_CON1); |
|---|
| 207 | | - |
|---|
| 208 | | - return 0; |
|---|
| 225 | + xway_stp_w32_mask(chip->virt, XWAY_STP_SPEED_MASK, |
|---|
| 226 | + XWAY_STP_10HZ, XWAY_STP_CON1); |
|---|
| 227 | + xway_stp_w32_mask(chip->virt, XWAY_STP_FPIS_MASK, |
|---|
| 228 | + XWAY_STP_FPIS_VALUE, XWAY_STP_CON1); |
|---|
| 229 | + } |
|---|
| 209 | 230 | } |
|---|
| 210 | 231 | |
|---|
| 211 | 232 | static int xway_stp_probe(struct platform_device *pdev) |
|---|
| 212 | 233 | { |
|---|
| 213 | | - struct resource *res; |
|---|
| 214 | 234 | u32 shadow, groups, dsl, phy; |
|---|
| 215 | 235 | struct xway_stp *chip; |
|---|
| 216 | 236 | struct clk *clk; |
|---|
| .. | .. |
|---|
| 220 | 240 | if (!chip) |
|---|
| 221 | 241 | return -ENOMEM; |
|---|
| 222 | 242 | |
|---|
| 223 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 224 | | - chip->virt = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 243 | + chip->virt = devm_platform_ioremap_resource(pdev, 0); |
|---|
| 225 | 244 | if (IS_ERR(chip->virt)) |
|---|
| 226 | 245 | return PTR_ERR(chip->virt); |
|---|
| 227 | 246 | |
|---|
| .. | .. |
|---|
| 252 | 271 | /* find out which gpios are controlled by the phys */ |
|---|
| 253 | 272 | if (of_machine_is_compatible("lantiq,ar9") || |
|---|
| 254 | 273 | of_machine_is_compatible("lantiq,gr9") || |
|---|
| 255 | | - of_machine_is_compatible("lantiq,vr9")) { |
|---|
| 274 | + of_machine_is_compatible("lantiq,vr9") || |
|---|
| 275 | + of_machine_is_compatible("lantiq,ar10") || |
|---|
| 276 | + of_machine_is_compatible("lantiq,grx390")) { |
|---|
| 256 | 277 | if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy)) |
|---|
| 257 | 278 | chip->phy1 = phy & XWAY_STP_PHY_MASK; |
|---|
| 258 | 279 | if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy)) |
|---|
| 259 | 280 | chip->phy2 = phy & XWAY_STP_PHY_MASK; |
|---|
| 260 | 281 | } |
|---|
| 261 | 282 | |
|---|
| 283 | + if (of_machine_is_compatible("lantiq,ar10") || |
|---|
| 284 | + of_machine_is_compatible("lantiq,grx390")) { |
|---|
| 285 | + if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy3", &phy)) |
|---|
| 286 | + chip->phy3 = phy & XWAY_STP_PHY_MASK; |
|---|
| 287 | + } |
|---|
| 288 | + |
|---|
| 289 | + if (of_machine_is_compatible("lantiq,grx390")) { |
|---|
| 290 | + if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy4", &phy)) |
|---|
| 291 | + chip->phy4 = phy & XWAY_STP_PHY_MASK; |
|---|
| 292 | + } |
|---|
| 293 | + |
|---|
| 262 | 294 | /* check which edge trigger we should use, default to a falling edge */ |
|---|
| 263 | 295 | if (!of_find_property(pdev->dev.of_node, "lantiq,rising", NULL)) |
|---|
| 264 | 296 | chip->edge = XWAY_STP_FALLING; |
|---|
| 265 | 297 | |
|---|
| 266 | | - clk = clk_get(&pdev->dev, NULL); |
|---|
| 298 | + clk = devm_clk_get(&pdev->dev, NULL); |
|---|
| 267 | 299 | if (IS_ERR(clk)) { |
|---|
| 268 | 300 | dev_err(&pdev->dev, "Failed to get clock\n"); |
|---|
| 269 | 301 | return PTR_ERR(clk); |
|---|
| 270 | 302 | } |
|---|
| 271 | | - clk_enable(clk); |
|---|
| 272 | 303 | |
|---|
| 273 | | - ret = xway_stp_hw_init(chip); |
|---|
| 274 | | - if (!ret) |
|---|
| 275 | | - ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip); |
|---|
| 304 | + ret = clk_prepare_enable(clk); |
|---|
| 305 | + if (ret) |
|---|
| 306 | + return ret; |
|---|
| 276 | 307 | |
|---|
| 277 | | - if (!ret) |
|---|
| 278 | | - dev_info(&pdev->dev, "Init done\n"); |
|---|
| 308 | + xway_stp_hw_init(chip); |
|---|
| 279 | 309 | |
|---|
| 280 | | - return ret; |
|---|
| 310 | + ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip); |
|---|
| 311 | + if (ret) { |
|---|
| 312 | + clk_disable_unprepare(clk); |
|---|
| 313 | + return ret; |
|---|
| 314 | + } |
|---|
| 315 | + |
|---|
| 316 | + dev_info(&pdev->dev, "Init done\n"); |
|---|
| 317 | + |
|---|
| 318 | + return 0; |
|---|
| 281 | 319 | } |
|---|
| 282 | 320 | |
|---|
| 283 | 321 | static const struct of_device_id xway_stp_match[] = { |
|---|